![]() |
Versioning and document comparison | ![]() |
TeXmacs has three different notions of “versions”, which should not be confused:
The undo/redo history of a buffer, maintained in C++ by the archiver class (
Data/History/archiver.hpp
). It lives in memory only and is described together with patches in Live documents and shared editing.
Versions of files managed by an external or remote system: the version control systems Subversion and Git for local files, and the server side version lists for remote files (see the remote file system). TeXmacs gives a uniform interface to browse histories, open old revisions and commit.
The structural comparison of two documents, which produces a document with special markup for the differences, and the commands for navigating through the differences and retaining one of the versions.
This page covers the last two points. All code lives in
progs/version/
.
version/version-tmfs.scm
defines a set of generic functions which are overloaded, using tm-define with a :require clause, by each back-end:
Returns "svn" if a .svn directory exists in one of the ancestor directories of name, "git" if a .git directory exists, "wrap" if the URL wraps a versioned URL (url-wrap), and #f otherwise. The result is cached per file in version-tool-table, and the corresponding back-end module (version-svn or version-git) is loaded the first time it is needed. (versioned? name) tests whether there is a tool.
The functions to be provided by a back-end are:
One of "unknown", "modified" or "unmodified".
A list of (rev by date msg), or #f.
The contents of the file at revision rev, as a string.
A short form of a revision for display (Git hashes are truncated to seven characters).
Operations of the Subversion style interface; they return a message to be displayed.
Which of the two interfaces the menu should offer.
The buffer level commands update-buffer, register-buffer, commit-buffer and commit-buffer-message call these functions and display the result; version-interactive-update and version-interactive-commit first save the buffer (save-buffer with the options :update and :commit). The menus are in
version/version-menu.scm
(version-menu, version-compare-menu).
A generated page with the history of a file (version-show-history), with a link to each revision.
A file at a given revision; its load handler returns (version-revision u rev). version-revision?, version-get-revision and version-head decompose such URLs, and version-revision-url builds them.
Pages for a Git commit (message, parents and diff statistics) and for the global status and log of a Git repository.
For remote files,
client/client-tmfs.scm
overloads the generic functions so that the history is the server side version list and revisions are time= URLs.
Both back-ends simply run the command line tools with eval-system and parse their output.
version-svn.scm
uses svn status, svn log, svn cat -r, svn up --accept theirs-full, svn add, svn remove --force and svn commit -m. Note that updating accepts the repository version in case of conflicts.
version-git.scm
uses git status --porcelain, git log --follow (at most 1000 entries) for histories, git show rev:path for revisions, git add and git reset HEAD for registration, and git commit -m for the global commit (git-interactive-commit). It supports the Git style interface only: there is no per-file commit or update.
To support another system, add a detection predicate to the cond in version-tool together with the name of the module to load, and write a module which overloads the generic functions above with (:require (== (version-tool name) "tool")). The history, revision and comparison facilities then work unchanged.
The result of a comparison is an ordinary document in which each difference is represented by one of the tags of the group version-tag (
version/version-drd.scm
):
Two arguments, the old and the new content, shown one after the other (using version-both-small for inline content and version-both-big when the old content is a document).
The same two arguments, but only the old resp. new content is shown; the other one appears in a balloon when the cursor enters the tag.
A placeholder for an absent content (inserted or deleted material), rendered as a cross.
The macros are defined in
packages/standard/std-fold.ts
, and the colors are given by the environment variables old-version-color and new-version-color. Since the three tags form a variant group, the user can cycle between them.
(compare-versions t1 t2) in
version/version-compare.scm
takes two Scheme trees and returns a merged tree. It works recursively:
Identical trees are returned as is. With the grain "rough", any difference gives (version-both t1 t2) for the whole trees; with the grain "block", differences inside a paragraph mark the whole paragraph. The grain is the preference "versioning grain" (version-set-grain), "detailed" by default.
Strings and concat are first denormalized: a concat is split into a sequence of words and spaces, so that differences are computed at the level of words. Sequences of words (concat) and of paragraphs (document) are compared by compare-versions-list, which looks for a long common subsequence (longest-common, truncated at 25 elements for efficiency and never breaking at spaces or empty paragraphs), recursively compares the parts before and after it, and falls back to comparing “skeletons” (the labels and arities of the elements) when no common element exists. Two single paragraphs with enough words in common (a quarter of the shorter one, long-common?) are compared word by word. Finally the result is normalized again.
Other tags with the same label and arity are compared argument by argument, unless an argument which differs is not accessible (e.g. a hidden parameter), in which case the whole trees are shown as different. Tags with different labels or arities, graphics, and tables whose formats differ (similar-tables?) are treated as atomic. There are special cases for hide-preamble, shared/mirror and bibliographies (bib-list, whose items are matched by their contents).
The comparison is a heuristic tuned for text documents; it is not an optimal edit distance and it does not detect moved blocks.
Load the other version with tree-load-inclusion, compare, replace the current buffer by the merged document, and move to the first difference. compare-with-newer* compares with an already open buffer. For versioned files and revisions, the submenu Compare with (version-compare-menu) lists the revisions of the history and chooses between the two functions with version-newer?.
Move to the first, previous, next or last tag of the group version-tag.
Replace the innermost difference, the differences in the selection, in the current paragraph (version-show-paragraph) or in the whole buffer by version-old, version-new or version-both.
Replace differences by one of their versions: 0 for the old one, 1 for the new one, or 'current for the one which is displayed (the new one for version-both). Suppressed content is removed, and the surrounding concat and document are normalized. This is the only form of “merging”: there is no automatic three-way merge.
reactualize-differences recomputes the comparison inside the current difference or selection, for instance after the user edited one of the versions or changed the grain. The keyboard shortcuts are defined in
version/version-kbd.scm
.