150 lines
8.3 KiB
Markdown
150 lines
8.3 KiB
Markdown
# hdc -- HedgeDoc as a tag-organized local folder
|
|
|
|
Run `../install.sh` from the repo root first (builds patched JOE, sets up a
|
|
venv, links `hdc` and `hd-joe` onto your PATH). This README covers `hdc`
|
|
specifically once that's done; see the top-level README for the whole repo.
|
|
|
|
## Setup
|
|
|
|
hdc init https://your-hedgedoc.example --dir ~/notes
|
|
|
|
Interactive dialog: email, then password (not echoed), logs in against
|
|
`/login`, keeps only the resulting session cookie. SSO/LDAP servers: use
|
|
`--cookie 'connect.sid=...'` instead (copied from your browser).
|
|
|
|
hdc sync # one-shot pull + reconcile
|
|
hdc sync --watch # poll every 30s (--interval to change)
|
|
hdc tag <note> <tag> # add a tag -- writes to HedgeDoc
|
|
hdc untag <note> <tag> # remove a tag -- writes to HedgeDoc
|
|
|
|
`--dir` is optional on `sync`/`tag`/`untag`: standing anywhere inside an
|
|
hd-managed folder (not just at its root) is enough -- `hdc` walks up
|
|
looking for `.hd/`, the same discovery `hd-joe` already does, falling
|
|
back to `~/notes` if that finds nothing. Pass `--dir` explicitly to
|
|
override this (e.g. managing more than one hd-managed folder).
|
|
|
|
`<note>` in `tag`/`untag` accepts a note id, or a path to one of its
|
|
existing symlinks -- resolved from wherever you're actually standing, the
|
|
same way a shell resolves any other path: a bare `FOI` means the symlink
|
|
named `FOI` right here, `../work/FOI` means the one in `../work`. This
|
|
takes priority over title matching, so a same-named note elsewhere never
|
|
gets picked by accident. Only if no symlink matches at all does it fall
|
|
back to searching by title (must be unique) across every note.
|
|
|
|
## Layout under --dir (default: auto-detected, see above; ~/notes if nothing is found)
|
|
|
|
.hd/config.json server URL (+ email if you logged in). No secrets.
|
|
.hd/cookies.json session cookie, mode 0600.
|
|
.hd/state.json what the reconciler believes is true right now.
|
|
.hd/problems.json things sync noticed but can't safely fix itself.
|
|
.hd/.lock real interprocess lock (fcntl.flock) shared by
|
|
every command that touches state/symlinks.
|
|
.hd/notes/<id>.md cached content -- read-only, do not edit directly.
|
|
<tag>/<title> symlink into .hd/notes/<id>.md, one per tag.
|
|
No .md on the symlink itself -- it's named
|
|
after the note's title, not a file.
|
|
untagged/<title> symlink for notes with no tags.
|
|
|
|
## What `sync` does
|
|
|
|
`GET /history` (cookie-authenticated) is the only source of truth for which
|
|
notes exist and what tags they have -- confirmed against HedgeDoc 1.12.0's
|
|
own `lib/history.js`, not guessed. Content is only re-fetched via
|
|
`/<id>/download` when `/history`'s `time` field has actually moved since
|
|
last sync (skips the network round trip entirely otherwise). Every sync
|
|
pass also runs a cheap structural check over the tag directories and writes
|
|
whatever it finds to `.hd/problems.json`:
|
|
|
|
- a real directory where a tag symlink was expected (nested tag folders
|
|
aren't supported -- one tag is always exactly one directory level)
|
|
- a plain file where a symlink was expected (the classic `cp` without `-P`
|
|
mistake) -- its checksum is looked up against the cache; a match names
|
|
the note and the exact fix command, no match says so plainly
|
|
- a broken symlink (target no longer exists)
|
|
- a symlink pointing outside `.hd/notes/` entirely (created by hand)
|
|
|
|
## What `tag`/`untag` do
|
|
|
|
There is no plain HTTP save endpoint for an existing note's content
|
|
(checked against the router source) -- the only way to change a note at
|
|
all is the realtime Socket.IO channel, same as any live edit. `hdc tag`/`hdc
|
|
untag` open a short-lived connection, fetch the document **fresh** (never
|
|
from the local cache, which can be hours old), parse the YAML frontmatter
|
|
exactly the way HedgeDoc itself does (`lib/models/note.js`'s
|
|
`splitFrontmatter`: must start with literal `---`, closed by the first
|
|
`\n---` or `\n...`), edit only the `tags:` line, and send the minimal patch
|
|
-- title and everything else in the frontmatter, and the entire body, are
|
|
left untouched. Tags are always written as a comma-separated string, never
|
|
a YAML list -- a list is silently ignored by the real server's own tag
|
|
parser. Untagging down to zero removes the `tags` key entirely rather than
|
|
leaving it empty. A note with no frontmatter at all gets a fresh block
|
|
inserted at the very top.
|
|
|
|
If the existing frontmatter doesn't parse as valid YAML, or parses to
|
|
something that isn't a mapping, `hdc tag`/`hdc untag` refuse to touch it
|
|
rather than risk destroying whatever's actually there.
|
|
|
|
After a successful write, the command immediately runs a normal sync pass
|
|
so the local symlinks catch up right away instead of waiting for the next
|
|
scheduled sync.
|
|
|
|
## Known gaps
|
|
|
|
- **No filesystem watcher.** Moving a symlink between tag folders yourself
|
|
doesn't do anything -- only the explicit `hdc tag`/`hdc untag` commands,
|
|
and hd-joe's note creation, write to the server. `ReconcileLock` (a real
|
|
`fcntl.flock` on `.hd/.lock`) is already shared by all three, so a future
|
|
watcher has something correct to coordinate with -- the watcher itself
|
|
isn't built.
|
|
- **`/history` is the most fragile dependency in this whole system** -- it's
|
|
HedgeDoc's internal "recently visited" feature, not an API designed for
|
|
this purpose. It only lists notes the account has opened at least once,
|
|
and a note's tags there only refresh while someone is actively viewing
|
|
or editing it server-side.
|
|
- **No protection against two truly simultaneous tag edits** to the same
|
|
note landing at the same moment -- the OT layer resolves the byte
|
|
positions correctly either way, but a genuine semantic race (both edits
|
|
reading the same "current tags" before either's write lands) isn't
|
|
specially detected. Rare in practice for a single-user tool; worth
|
|
revisiting if `hdc tag` ever gets called from automation.
|
|
|
|
## Tests
|
|
|
|
cd hd-cli
|
|
python3 test_hd_core.py # reconciliation: create/retag/rename/
|
|
# collision/deletion/idempotency (34)
|
|
python3 test_hd_cli.py # hdc init/sync via subprocess, login flow,
|
|
# file permissions, --dir auto-detection (30)
|
|
python3 test_hd_tags.py # frontmatter parsing/patching, note
|
|
# reference resolution incl. cwd-relative
|
|
# path priority over title matching (31)
|
|
python3 test_hd_problems.py # stray/nested/broken/foreign detection,
|
|
# real flock mutual exclusion (16)
|
|
python3 test_hd_write.py # write_tags against a fake realtime
|
|
# server: minimal edits, no-op skip (13)
|
|
python3 test_hd_tag_cli.py # hdc tag/untag via subprocess against a
|
|
# combined history+download+realtime
|
|
# fake server (13)
|
|
python3 test_hd_tag_stale_history.py # tag/untag against a fake server
|
|
# that deliberately delays /history's
|
|
# view of a write, like real HedgeDoc's
|
|
# own DB-flush lag (9)
|
|
|
|
148 checks total. Three real bugs were found and fixed by these tests
|
|
during development, not just by reading the code: `write_tags` originally
|
|
had a "retry if revision moved" check that couldn't tell its own
|
|
successful write's natural revision bump apart from a genuine concurrent
|
|
edit, `hdc tag`/`hdc untag`'s immediate re-sync could be fooled by
|
|
`/history`'s own delayed view of the write it had just made (found only
|
|
once a fake server was built that actually reproduces that delay, instead
|
|
of the instant consistency every other fake server here gives for free --
|
|
see CHANGES-0.7.28.md), and
|
|
replayed the tag mutation a second time on top of itself -- caught by
|
|
`test_hd_write.py` producing `['urgent', 'urgent']` instead of `['urgent']`.
|
|
Separately, `test_hd_tag_cli.py`'s own fake server hung on every `hdc tag`
|
|
call at first, from constructing a fresh `socketio.WSGIApp` per HTTP
|
|
request and from parsing `QUERY_STRING` through `urlparse()` a second time
|
|
(which returns nothing, since `QUERY_STRING` isn't a full URL) -- both bugs
|
|
were in the test's own scaffolding, not in `hdc`, but neither test would
|
|
have caught anything real until both were fixed.
|