Fossil SCM

fossil-scm / www / colordiff.md
Source Blame History 104 lines
7c99ebc… wyoung 1 # Colorized Diffs
7c99ebc… wyoung 2
7c99ebc… wyoung 3 The oldest and most widely compatible method to get colorized diffs in
7c99ebc… wyoung 4 Fossil is to use its web UI:
7c99ebc… wyoung 5
7c99ebc… wyoung 6 fossil ui --page '/vdiff?from=2024-04-01&to=trunk'
7c99ebc… wyoung 7
7c99ebc… wyoung 8 That syntax is admittedly awkward, and it doesn’t work where “from” is
7c99ebc… wyoung 9 the current checkout. Fortunately, there are many other methods to get
7c99ebc… wyoung 10 colorized `diff` output from Fossil.
7c99ebc… wyoung 11
7c99ebc… wyoung 12
7c99ebc… wyoung 13 <a id="ui"></a>
7c99ebc… wyoung 14 ## `fossil diff -b`
7c99ebc… wyoung 15
7c99ebc… wyoung 16 This produces a graphical diff in HTML format and sends it to the
7c99ebc… wyoung 17 user’s default web browser for viewing.
7c99ebc… wyoung 18
7c99ebc… wyoung 19
7c99ebc… wyoung 20
7c99ebc… wyoung 21 <a id="ui"></a>
7c99ebc… wyoung 22 ## `fossil diff -tk`
7c99ebc… wyoung 23
7c99ebc… wyoung 24 You may be surprised to learn that the prior feature doesn’t use any of
7c99ebc… wyoung 25 the skinning or chrome from Fossil UI. This is because it is meant as a
7c99ebc… wyoung 26 functional replacement for an older method of getting colorized diffs,
7c99ebc… wyoung 27 “`fossil diff -tk`”. The feature was added after Apple stopped shipping
7c99ebc… wyoung 28 Tcl/Tk in macOS, and the third-party replacements often failed to work
7c99ebc… wyoung 29 correctly. It’s useful on other platforms as well.
7c99ebc… wyoung 30
7c99ebc… wyoung 31
7c99ebc… wyoung 32 <a id="git"></a>
7c99ebc… wyoung 33 ## Delegate to Git
7c99ebc… wyoung 34
7c99ebc… wyoung 35 It may be considered sacrilege by some, but the most direct method for
7c99ebc… wyoung 36 those who want Git-like diff behavior may be to delegate diff behavior
7c99ebc… wyoung 37 to Git:
7c99ebc… wyoung 38
7c99ebc… wyoung 39 fossil set --global diff-command 'git diff --no-index'
7c99ebc… wyoung 40
7c99ebc… wyoung 41 The flag permits it to diff files that aren’t inside a Git repository.
7c99ebc… wyoung 42
7c99ebc… wyoung 43
7c99ebc… wyoung 44 <a id="diffutils"></a>
7c99ebc… wyoung 45 ## GNU Diffutils
7c99ebc… wyoung 46
7c99ebc… wyoung 47 If your system is from 2016 or later, it may include [GNU Diffutils][gd]
7c99ebc… wyoung 48 3.4 or newer, which lets you say:
7c99ebc… wyoung 49
7c99ebc… wyoung 50 fossil set --global diff-command 'diff -dwu --color=always'
7c99ebc… wyoung 51
7c99ebc… wyoung 52 You might think you could give `--color=auto`, but that fails with
7c99ebc… wyoung 53 commands like “`fossil diff | less`” since the pipe turns the output
7c99ebc… wyoung 54 non-interactive from the perspective of the underlying `diff` instance.
7c99ebc… wyoung 55
7c99ebc… wyoung 56 This use of unconditional colorization means you will then have to
7c99ebc… wyoung 57 remember to add the `-i` option to `fossil diff` commands when producing
7c99ebc… wyoung 58 `patch(1)` files or piping diff output to another command that doesn’t
7c99ebc… wyoung 59 understand ANSI escape sequences, such as [`diffstat`][ds].
7c99ebc… wyoung 60
7c99ebc… wyoung 61 [ds]: https://invisible-island.net/diffstat/
7c99ebc… wyoung 62 [gd]: https://www.gnu.org/software/diffutils/
7c99ebc… wyoung 63
7c99ebc… wyoung 64
7c99ebc… wyoung 65 <a id="bat"></a>
7c99ebc… wyoung 66 ## Bat, the Cat with Wings
7c99ebc… wyoung 67
7c99ebc… wyoung 68 We can work around the `--color=auto` problem by switching from GNU less
7c99ebc… wyoung 69 as our pager to [`bat`][bat], as it can detect GNU diff output and
7c99ebc… wyoung 70 colorize it for you:
7c99ebc… wyoung 71
7c99ebc… wyoung 72 fossil set --global diff-command 'diff -dwu --color=auto'
9fca91e… wyoung 73 fossil diff | bat
7c99ebc… wyoung 74
7c99ebc… wyoung 75 In this author’s experience, that works a lot more reliably than GNU
7c99ebc… wyoung 76 less’s ANSI color escape code handling, even when you set `LESS=-R` in
7c99ebc… wyoung 77 your environment.
9fca91e… wyoung 78
9fca91e… wyoung 79 The reason we don’t leave the `diff-command` unset in this case is that
9fca91e… wyoung 80 Fossil produces additional lines at the start which confuse the diff
9fca91e… wyoung 81 format detection in `bat`. Forcing output through an external diff
9fca91e… wyoung 82 command solves that. It also means that if you forget to pipe the output
9fca91e… wyoung 83 through `bat`, you still get colorized output from GNU diff.
7c99ebc… wyoung 84
7c99ebc… wyoung 85 [bat]: https://github.com/sharkdp/bat
7c99ebc… wyoung 86
7c99ebc… wyoung 87
7c99ebc… wyoung 88 <a id="colordiff"></a>
7c99ebc… wyoung 89 ## Colordiff
7c99ebc… wyoung 90
7c99ebc… wyoung 91 A method that works on systems predating GNU diffutils 3.4 or the
4498791… wyoung 92 widespread availability of `bat` is to install [`colordiff`][cdurl], as
7c99ebc… wyoung 93 it is included in [many package systems][cdpkg], including ones for
7c99ebc… wyoung 94 outdated OSes. That then lets you say:
7c99ebc… wyoung 95
7c99ebc… wyoung 96 fossil set --global diff-command 'colordiff -dwu'
7c99ebc… wyoung 97
7c99ebc… wyoung 98 The main reason we list this alternative last is that it has the same
7c99ebc… wyoung 99 limitation of unconditional color as [above](#diffutils).
7c99ebc… wyoung 100
4498791… wyoung 101 [cdurl]: https://www.colordiff.org/
4498791… wyoung 102 [cdpkg]: https://repology.org/project/colordiff/versions
fdd8057… wyoung 103
fdd8057… wyoung 104 <div style="height:50em" id="this-space-intentionally-left-blank"></div>

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button