Compare

community mirrored

A copy, fork or mirror, beside its upstream file as it is at a head, in the Upstream view: what changed, whether the copy is edited, and what to do about it.

Open

Compare (check view: the latest copy, or the mirror under Mirrors changed upstream, at the check’s head; Preview view: the latest copy, or the mirror with Compare mirror, at the preview’s head) or Compare this fork with upstream (active note; reads head itself; “Nothing new upstream.” only when fork_blob is head’s blob, so a version merely seen is still compared). One layout, whichever way in: the note where it is open, or in the current pane, and the Upstream view (publictxt-upstream), read-only, in a group to its right; one Upstream pane is reused. Side by side, never two tabs of one group: an Upstream pane among the note’s tabs moves to its right.

The view’s header: source, branch, the note, then

  • What changed: “12 lines added, 3 removed since you forked.” (diffStat from fork_blob to head), or that the forked version is gone upstream.
  • Edited? kept current as the note saves: edited / as forked / can’t tell (forked blob unreadable).

Edited (isEdited(), mirrors too): the body differs, ignoring leading blank lines, line endings, and links showing the same words to the same subpath (matchesExceptLinks(): converting a link, or Obsidian updating one whose target moved, is no edit); or the parsed front matter differs, fork_* and mirror_* aside (parsed, because Obsidian rewrites the YAML).

Below the header, a switch: Changes (the default), Rendered (front matter hidden) or Source (raw text). Rendered safely, with alerts, and cut when too long, as a preview is (preview.md).

Changes

A diff as a git client shows one: old and new line numbers, removed lines red and added green, the words that changed in them marked stronger, three unchanged lines around each change, longer unchanged stretches folded (selecting one shows it). +12 −3 beside the switch. Text only, never rendered: nothing in it runs or loads. Two comparisons, the first shown first:

  • Upstream changes: the forked (or mirrored) version to this one. Not offered when the copy is from this version, or the forked version is gone upstream.
  • Yours → upstream: the note as saved now to this version, what Overwrite would do. The plugin’s fork_* and mirror_* keys are left out of its front matter (withoutProvenance()), and front matter left empty goes. Drawn again, where it was scrolled to, as the note is saved.

The diff: Myers’ O(ND) algorithm in linear space, regions on a stack, so a long file can’t overflow the call stack; a region needing over 4,000 edits is shown replaced whole, so very different texts stay fast. Lines only added or removed slide to where they read best, as git’s do: ending on a blank line, so a paragraph added is that paragraph. Within a block of changed lines, words are diffed the same way: not marked when under 35% of the text is in common (rewritten, marks would be noise) or past 8,000 words; a gap of spaces or one punctuation mark between two changed words is part of the change. Line endings and a last line break don’t count. Over 2,000,000 characters together (DIFF_LIMIT) it isn’t diffed: Source shows the text.

Actions

  • A fork: Overwrite, Write alongside, Keep mine (fork-update.md).
  • A mirror: the same header, said of the mirror (“since you mirrored”, “Your mirror is as you mirrored it”), and one action, Update: as Update in the check view (mirror.md), to this version, not asked about content that runs (the alert was on screen), asked when edited. A mirror is upstream’s: nothing to write alongside or keep.

Code

Pure: src/compare/compare.ts (what a compare is of, the header’s sentences), src/copy/edited.ts, src/compare/myers.ts, diff.ts (lines, sliding, folding), words.ts (words that changed). Vault: src/compare/run.ts (opening it, reading what it shows). Its entry points: compareChange() there (check view), src/fork/compare-run.ts (command), src/ui/preview-view.ts. Its actions: src/fork/update-run.ts, src/mirror/update-run.ts. UI: src/ui/upstream-view.ts (its comparisons) on src/ui/file-view.ts (shared with the Preview view: the switch, drawing changes), src/ui/diff-view.ts (a diff’s DOM). compare/ imports neither fork/ nor mirror/: the view joins them.