interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / contrib / diff-highlight / README

248 lines · 9048 bytes Raw Blame History
1diff-highlight
2==============
3
4Line oriented diffs are great for reviewing code, because for most
5hunks, you want to see the old and the new segments of code next to each
6other. Sometimes, though, when an old line and a new line are very
7similar, it's hard to immediately see the difference.
8
9You can use "--color-words" to highlight only the changed portions of
10lines. However, this can often be hard to read for code, as it loses
11the line structure, and you end up with oddly formatted bits.
12
13Instead, this script post-processes the line-oriented diff, finds pairs
14of lines, and highlights the differing segments. It's currently very
15simple and stupid about doing these tasks. In particular:
16
17 1. It will only highlight hunks in which the number of removed and
18 added lines is the same, and it will pair lines within the hunk by
19 position (so the first removed line is compared to the first added
20 line, and so forth). This is simple and tends to work well in
21 practice. More complex changes don't highlight well, so we tend to
22 exclude them due to the "same number of removed and added lines"
23 restriction. Or even if we do try to highlight them, they end up
24 not highlighting because of our "don't highlight if the whole line
25 would be highlighted" rule.
26
27 2. It will find the common prefix and suffix of two lines, and
28 consider everything in the middle to be "different". It could
29 instead do a real diff of the characters between the two lines and
30 find common subsequences. However, the point of the highlight is to
31 call attention to a certain area. Even if some small subset of the
32 highlighted area actually didn't change, that's OK. In practice it
33 ends up being more readable to just have a single blob on the line
34 showing the interesting bit.
35
36The goal of the script is therefore not to be exact about highlighting
37changes, but to call attention to areas of interest without being
38visually distracting. Non-diff lines and existing diff coloration is
39preserved; the intent is that the output should look exactly the same as
40the input, except for the occasional highlight.
41
42Build/Install
43-------------
44
45You can build the `diff-highlight` script by running `make` from within
46the diff-highlight directory. There is no `make install` target; you can
47copy the built script to your $PATH.
48
49You can run diff-highlight's internal tests by running `make test`. Note
50that you must also build Git itself first (by running `make` from the
51top-level of the project).
52
53Use
54---
55
56You can try out the built diff-highlight program with:
57
58---------------------------------------------
59git log -p --color | /path/to/diff-highlight
60---------------------------------------------
61
62If you want to use it all the time, drop it in your $PATH and put the
63following in your git configuration:
64
65---------------------------------------------
66[pager]
67 log = diff-highlight | less
68 show = diff-highlight | less
69 diff = diff-highlight | less
70---------------------------------------------
71
72If you use the interactive patch mode of `git add -p`, `git checkout
73-p`, etc, you may also want to configure it to be used there:
74
75---------------------------------------------
76[interactive]
77 diffFilter = diff-highlight
78---------------------------------------------
79
80
81Color Config
82------------
83
84You can configure the highlight colors and attributes using git's
85config. The colors for "old" and "new" lines can be specified
86independently. There are two "modes" of configuration:
87
88 1. You can specify a "highlight" color and a matching "reset" color.
89 This will retain any existing colors in the diff, and apply the
90 "highlight" and "reset" colors before and after the highlighted
91 portion.
92
93 2. You can specify a "normal" color and a "highlight" color. In this
94 case, existing colors are dropped from that line. The non-highlighted
95 bits of the line get the "normal" color, and the highlights get the
96 "highlight" color.
97
98If no "new" colors are specified, they default to the "old" colors. If
99no "old" colors are specified, the default is to reverse the foreground
100and background for highlighted portions.
101
102Examples:
103
104---------------------------------------------
105# Underline highlighted portions
106[color "diff-highlight"]
107oldHighlight = ul
108oldReset = noul
109---------------------------------------------
110
111---------------------------------------------
112# Varying background intensities
113[color "diff-highlight"]
114oldNormal = "black #f8cbcb"
115oldHighlight = "black #ffaaaa"
116newNormal = "black #cbeecb"
117newHighlight = "black #aaffaa"
118---------------------------------------------
119
120
121Using diff-highlight as a module
122--------------------------------
123
124If you want to pre- or post- process the highlighted lines as part of
125another perl script, you can use the DiffHighlight module. You can
126either "require" it or just cat the module together with your script (to
127avoid run-time dependencies).
128
129Your script may set up one or more of the following variables:
130
131 - $DiffHighlight::line_cb - this should point to a function which is
132 called whenever DiffHighlight has lines (which may contain
133 highlights) to output. The default function prints each line to
134 stdout. Note that the function may be called with multiple lines.
135
136 - $DiffHighlight::flush_cb - this should point to a function which
137 flushes the output (because DiffHighlight believes it has completed
138 processing a logical chunk of input). The default function flushes
139 stdout.
140
141 - @DiffHighlight::OLD_HIGHLIGHT and @DiffHighlight::NEW_HIGHLIGHT - these
142 arrays specify the normal, highlighted, and reset colors (in that order)
143 for old/new lines. If unset, values will be retrieved by calling `git
144 config` (see "Color Config" above). Note that these should be the literal
145 color bytes (starting with an ANSI escape code), not color names.
146
147The script may then feed lines, one at a time, to DiffHighlight::handle_line().
148When lines are done processing, they will be fed to $line_cb. Note that
149DiffHighlight may queue up many input lines (to analyze a whole hunk)
150before calling $line_cb. After providing all lines, call
151DiffHighlight::flush() to flush any unprocessed lines.
152
153If you just want to process stdin, DiffHighlight::highlight_stdin()
154is a convenience helper which will loop and flush for you.
155
156
157Bugs
158----
159
160Because diff-highlight relies on heuristics to guess which parts of
161changes are important, there are some cases where the highlighting is
162more distracting than useful. Fortunately, these cases are rare in
163practice, and when they do occur, the worst case is simply a little
164extra highlighting. This section documents some cases known to be
165sub-optimal, in case somebody feels like working on improving the
166heuristics.
167
1681. Two changes on the same line get highlighted in a blob. For example,
169 highlighting:
170
171----------------------------------------------
172-foo(buf, size);
173+foo(obj->buf, obj->size);
174----------------------------------------------
175
176 yields (where the inside of "+{}" would be highlighted):
177
178----------------------------------------------
179-foo(buf, size);
180+foo(+{obj->buf, obj->}size);
181----------------------------------------------
182
183 whereas a more semantically meaningful output would be:
184
185----------------------------------------------
186-foo(buf, size);
187+foo(+{obj->}buf, +{obj->}size);
188----------------------------------------------
189
190 Note that doing this right would probably involve a set of
191 content-specific boundary patterns, similar to word-diff. Otherwise
192 you get junk like:
193
194-----------------------------------------------------
195-this line has some -{i}nt-{ere}sti-{ng} text on it
196+this line has some +{fa}nt+{a}sti+{c} text on it
197-----------------------------------------------------
198
199 which is less readable than the current output.
200
2012. The multi-line matching assumes that lines in the pre- and post-image
202 match by position. This is often the case, but can be fooled when a
203 line is removed from the top and a new one added at the bottom (or
204 vice versa). Unless the lines in the middle are also changed, diffs
205 will show this as two hunks, and it will not get highlighted at all
206 (which is good). But if the lines in the middle are changed, the
207 highlighting can be misleading. Here's a pathological case:
208
209-----------------------------------------------------
210-one
211-two
212-three
213-four
214+two 2
215+three 3
216+four 4
217+five 5
218-----------------------------------------------------
219
220 which gets highlighted as:
221
222-----------------------------------------------------
223-one
224-t-{wo}
225-three
226-f-{our}
227+two 2
228+t+{hree 3}
229+four 4
230+f+{ive 5}
231-----------------------------------------------------
232
233 because it matches "two" to "three 3", and so forth. It would be
234 nicer as:
235
236-----------------------------------------------------
237-one
238-two
239-three
240-four
241+two +{2}
242+three +{3}
243+four +{4}
244+five 5
245-----------------------------------------------------
246
247 which would probably involve pre-matching the lines into pairs
248 according to some heuristic.