diff.c: color moved lines differently

When a patch consists mostly of moving blocks of code around, it can
be quite tedious to ensure that the blocks are moved verbatim, and not
undesirably modified in the move. To that end, color blocks that are
moved within the same patch differently. For example (OM, del, add,
and NM are different colors):

    [OM]  -void sensitive_stuff(void)
    [OM]  -{
    [OM]  -        if (!is_authorized_user())
    [OM]  -                die("unauthorized");
    [OM]  -        sensitive_stuff(spanning,
    [OM]  -                        multiple,
    [OM]  -                        lines);
    [OM]  -}

           void another_function()
           {
    [del] -        printf("foo");
    [add] +        printf("bar");
           }

    [NM]  +void sensitive_stuff(void)
    [NM]  +{
    [NM]  +        if (!is_authorized_user())
    [NM]  +                die("unauthorized");
    [NM]  +        sensitive_stuff(spanning,
    [NM]  +                        multiple,
    [NM]  +                        lines);
    [NM]  +}

However adjacent blocks may be problematic. For example, in this
potentially malicious patch, the swapping of blocks can be spotted:

    [OM]  -void sensitive_stuff(void)
    [OM]  -{
    [OMA] -        if (!is_authorized_user())
    [OMA] -                die("unauthorized");
    [OM]  -        sensitive_stuff(spanning,
    [OM]  -                        multiple,
    [OM]  -                        lines);
    [OMA] -}

           void another_function()
           {
    [del] -        printf("foo");
    [add] +        printf("bar");
           }

    [NM]  +void sensitive_stuff(void)
    [NM]  +{
    [NMA] +        sensitive_stuff(spanning,
    [NMA] +                        multiple,
    [NMA] +                        lines);
    [NM]  +        if (!is_authorized_user())
    [NM]  +                die("unauthorized");
    [NMA] +}

If the moved code is larger, it is easier to hide some permutation in the
code, which is why some alternative coloring is needed.

This patch implements the first mode:
* basic alternating 'Zebra' mode
  This conveys all information needed to the user.  Defer customization to
  later patches.

First I implemented an alternative design, which would try to fingerprint
a line by its neighbors to detect if we are in a block or at the boundary.
This idea iss error prone as it inspected each line and its neighboring
lines to determine if the line was (a) moved and (b) if was deep inside
a hunk by having matching neighboring lines. This is unreliable as the
we can construct hunks which have equal neighbors that just exceed the
number of lines inspected. (Think of 'AXYZBXYZCXYZD..' with each letter
as a line, that is permutated to AXYZCXYZBXYZD..').

Instead this provides a dynamic programming greedy algorithm that finds
the largest moved hunk and then has several modes on highlighting bounds.

A note on the options '--submodule=diff' and '--color-words/--word-diff':
In the conversion to use emit_line in the prior patches both submodules
as well as word diff output carefully chose to call emit_line with sign=0.
All output with sign=0 is ignored for move detection purposes in this
patch, such that no weird looking output will be generated for these
cases. This leads to another thought: We could pass on '--color-moved' to
submodules such that they color up moved lines for themselves. If we'd do
so only line moves within a repository boundary are marked up.

It is useful to have moved lines colored, but there are annoying corner
cases, such as a single line moved, that is very common. For example
in a typical patch of C code, we have closing braces that end statement
blocks or functions.

While it is technically true that these lines are moved as they show up
elsewhere, it is harmful for the review as the reviewers attention is
drawn to such a minor side annoyance.

For now let's have a simple solution of hardcoding the number of
moved lines to be at least 3 before coloring them. Note, that the
length is applied across all blocks to find the 'lonely' blocks
that pollute new code, but do not interfere with a permutated
block where each permutation has less lines than 3.

Helped-by: Jonathan Tan <jonathantanmy@google.com>
Signed-off-by: Stefan Beller <sbeller@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
3 files changed
tree: 7000d0063d6eacf35fab609487416284da838b05
  1. .github/
  2. block-sha1/
  3. builtin/
  4. ci/
  5. compat/
  6. contrib/
  7. Documentation/
  8. ewah/
  9. git-gui/
  10. gitk-git/
  11. gitweb/
  12. mergetools/
  13. perl/
  14. po/
  15. ppc/
  16. refs/
  17. sha1dc/
  18. t/
  19. templates/
  20. vcs-svn/
  21. xdiff/
  22. .gitattributes
  23. .gitignore
  24. .mailmap
  25. .travis.yml
  26. abspath.c
  27. aclocal.m4
  28. advice.c
  29. advice.h
  30. alias.c
  31. alloc.c
  32. apply.c
  33. apply.h
  34. archive-tar.c
  35. archive-zip.c
  36. archive.c
  37. archive.h
  38. argv-array.c
  39. argv-array.h
  40. attr.c
  41. attr.h
  42. base85.c
  43. bisect.c
  44. bisect.h
  45. blame.c
  46. blame.h
  47. blob.c
  48. blob.h
  49. branch.c
  50. branch.h
  51. builtin.h
  52. bulk-checkin.c
  53. bulk-checkin.h
  54. bundle.c
  55. bundle.h
  56. cache-tree.c
  57. cache-tree.h
  58. cache.h
  59. check-builtins.sh
  60. check-racy.c
  61. check_bindir
  62. color.c
  63. color.h
  64. column.c
  65. column.h
  66. combine-diff.c
  67. command-list.txt
  68. commit-slab.h
  69. commit.c
  70. commit.h
  71. common-main.c
  72. config.c
  73. config.h
  74. config.mak.in
  75. config.mak.uname
  76. configure.ac
  77. connect.c
  78. connect.h
  79. connected.c
  80. connected.h
  81. convert.c
  82. convert.h
  83. copy.c
  84. COPYING
  85. credential-cache--daemon.c
  86. credential-cache.c
  87. credential-store.c
  88. credential.c
  89. credential.h
  90. csum-file.c
  91. csum-file.h
  92. ctype.c
  93. daemon.c
  94. date.c
  95. decorate.c
  96. decorate.h
  97. delta.h
  98. diff-delta.c
  99. diff-lib.c
  100. diff-no-index.c
  101. diff.c
  102. diff.h
  103. diffcore-break.c
  104. diffcore-delta.c
  105. diffcore-order.c
  106. diffcore-pickaxe.c
  107. diffcore-rename.c
  108. diffcore.h
  109. dir-iterator.c
  110. dir-iterator.h
  111. dir.c
  112. dir.h
  113. editor.c
  114. entry.c
  115. environment.c
  116. exec_cmd.c
  117. exec_cmd.h
  118. fast-import.c
  119. fetch-pack.c
  120. fetch-pack.h
  121. fmt-merge-msg.h
  122. fsck.c
  123. fsck.h
  124. generate-cmdlist.sh
  125. gettext.c
  126. gettext.h
  127. git-add--interactive.perl
  128. git-archimport.perl
  129. git-bisect.sh
  130. git-compat-util.h
  131. git-cvsexportcommit.perl
  132. git-cvsimport.perl
  133. git-cvsserver.perl
  134. git-difftool--helper.sh
  135. git-filter-branch.sh
  136. git-instaweb.sh
  137. git-merge-octopus.sh
  138. git-merge-one-file.sh
  139. git-merge-resolve.sh
  140. git-mergetool--lib.sh
  141. git-mergetool.sh
  142. git-p4.py
  143. git-parse-remote.sh
  144. git-quiltimport.sh
  145. git-rebase--am.sh
  146. git-rebase--interactive.sh
  147. git-rebase--merge.sh
  148. git-rebase.sh
  149. git-remote-testgit.sh
  150. git-request-pull.sh
  151. git-send-email.perl
  152. git-sh-i18n.sh
  153. git-sh-setup.sh
  154. git-stash.sh
  155. git-submodule.sh
  156. git-svn.perl
  157. GIT-VERSION-GEN
  158. git-web--browse.sh
  159. git.c
  160. git.rc
  161. gpg-interface.c
  162. gpg-interface.h
  163. graph.c
  164. graph.h
  165. grep.c
  166. grep.h
  167. hash.h
  168. hashmap.c
  169. hashmap.h
  170. help.c
  171. help.h
  172. hex.c
  173. http-backend.c
  174. http-fetch.c
  175. http-push.c
  176. http-walker.c
  177. http.c
  178. http.h
  179. ident.c
  180. imap-send.c
  181. INSTALL
  182. iterator.h
  183. khash.h
  184. kwset.c
  185. kwset.h
  186. levenshtein.c
  187. levenshtein.h
  188. LGPL-2.1
  189. line-log.c
  190. line-log.h
  191. line-range.c
  192. line-range.h
  193. list-objects.c
  194. list-objects.h
  195. list.h
  196. ll-merge.c
  197. ll-merge.h
  198. lockfile.c
  199. lockfile.h
  200. log-tree.c
  201. log-tree.h
  202. mailinfo.c
  203. mailinfo.h
  204. mailmap.c
  205. mailmap.h
  206. Makefile
  207. match-trees.c
  208. merge-blobs.c
  209. merge-blobs.h
  210. merge-recursive.c
  211. merge-recursive.h
  212. merge.c
  213. mergesort.c
  214. mergesort.h
  215. mru.c
  216. mru.h
  217. name-hash.c
  218. notes-cache.c
  219. notes-cache.h
  220. notes-merge.c
  221. notes-merge.h
  222. notes-utils.c
  223. notes-utils.h
  224. notes.c
  225. notes.h
  226. object.c
  227. object.h
  228. oidset.c
  229. oidset.h
  230. pack-bitmap-write.c
  231. pack-bitmap.c
  232. pack-bitmap.h
  233. pack-check.c
  234. pack-objects.c
  235. pack-objects.h
  236. pack-revindex.c
  237. pack-revindex.h
  238. pack-write.c
  239. pack.h
  240. pager.c
  241. parse-options-cb.c
  242. parse-options.c
  243. parse-options.h
  244. patch-delta.c
  245. patch-ids.c
  246. patch-ids.h
  247. path.c
  248. pathspec.c
  249. pathspec.h
  250. pkt-line.c
  251. pkt-line.h
  252. preload-index.c
  253. pretty.c
  254. prio-queue.c
  255. prio-queue.h
  256. progress.c
  257. progress.h
  258. prompt.c
  259. prompt.h
  260. quote.c
  261. quote.h
  262. reachable.c
  263. reachable.h
  264. read-cache.c
  265. README.md
  266. ref-filter.c
  267. ref-filter.h
  268. reflog-walk.c
  269. reflog-walk.h
  270. refs.c
  271. refs.h
  272. remote-curl.c
  273. remote-testsvn.c
  274. remote.c
  275. remote.h
  276. replace_object.c
  277. rerere.c
  278. rerere.h
  279. resolve-undo.c
  280. resolve-undo.h
  281. revision.c
  282. revision.h
  283. run-command.c
  284. run-command.h
  285. send-pack.c
  286. send-pack.h
  287. sequencer.c
  288. sequencer.h
  289. server-info.c
  290. setup.c
  291. sh-i18n--envsubst.c
  292. sha1-array.c
  293. sha1-array.h
  294. sha1-lookup.c
  295. sha1-lookup.h
  296. sha1_file.c
  297. sha1_name.c
  298. sha1dc_git.c
  299. sha1dc_git.h
  300. shallow.c
  301. shell.c
  302. shortlog.h
  303. show-index.c
  304. sideband.c
  305. sideband.h
  306. sigchain.c
  307. sigchain.h
  308. split-index.c
  309. split-index.h
  310. strbuf.c
  311. strbuf.h
  312. streaming.c
  313. streaming.h
  314. string-list.c
  315. string-list.h
  316. sub-process.c
  317. sub-process.h
  318. submodule-config.c
  319. submodule-config.h
  320. submodule.c
  321. submodule.h
  322. symlinks.c
  323. tag.c
  324. tag.h
  325. tar.h
  326. tempfile.c
  327. tempfile.h
  328. thread-utils.c
  329. thread-utils.h
  330. tmp-objdir.c
  331. tmp-objdir.h
  332. trace.c
  333. trace.h
  334. trailer.c
  335. trailer.h
  336. transport-helper.c
  337. transport.c
  338. transport.h
  339. tree-diff.c
  340. tree-walk.c
  341. tree-walk.h
  342. tree.c
  343. tree.h
  344. unicode_width.h
  345. unimplemented.sh
  346. unix-socket.c
  347. unix-socket.h
  348. unpack-trees.c
  349. unpack-trees.h
  350. upload-pack.c
  351. url.c
  352. url.h
  353. urlmatch.c
  354. urlmatch.h
  355. usage.c
  356. userdiff.c
  357. userdiff.h
  358. utf8.c
  359. utf8.h
  360. varint.c
  361. varint.h
  362. version.c
  363. version.h
  364. versioncmp.c
  365. walker.c
  366. walker.h
  367. wildmatch.c
  368. wildmatch.h
  369. worktree.c
  370. worktree.h
  371. wrap-for-bin.sh
  372. wrapper.c
  373. write_or_die.c
  374. ws.c
  375. wt-status.c
  376. wt-status.h
  377. xdiff-interface.c
  378. xdiff-interface.h
  379. zlib.c
README.md

Git - fast, scalable, distributed revision control system

Git is a fast, scalable, distributed revision control system with an unusually rich command set that provides both high-level operations and full access to internals.

Git is an Open Source project covered by the GNU General Public License version 2 (some parts of it are under different licenses, compatible with the GPLv2). It was originally written by Linus Torvalds with help of a group of hackers around the net.

Please read the file INSTALL for installation instructions.

Many Git online resources are accessible from https://git-scm.com/ including full documentation and Git related tools.

See Documentation/gittutorial.txt to get started, then see Documentation/giteveryday.txt for a useful minimum set of commands, and Documentation/git-.txt for documentation of each command. If git has been correctly installed, then the tutorial can also be read with man gittutorial or git help tutorial, and the documentation of each command with man git-<commandname> or git help <commandname>.

CVS users may also want to read Documentation/gitcvs-migration.txt (man gitcvs-migration or git help cvs-migration if git is installed).

The user discussion and development of Git take place on the Git mailing list -- everyone is welcome to post bug reports, feature requests, comments and patches to git@vger.kernel.org (read Documentation/SubmittingPatches for instructions on patch submission). To subscribe to the list, send an email with just “subscribe git” in the body to majordomo@vger.kernel.org. The mailing list archives are available at https://public-inbox.org/git/, http://marc.info/?l=git and other archival sites.

The maintainer frequently sends the “What's cooking” reports that list the current status of various development topics to the mailing list. The discussion following them give a good reference for project status, development direction and remaining tasks.

The name “git” was given by Linus Torvalds when he wrote the very first version. He described the tool as “the stupid content tracker” and the name as (depending on your mood):

  • random three-letter combination that is pronounceable, and not actually used by any common UNIX command. The fact that it is a mispronunciation of “get” may or may not be relevant.
  • stupid. contemptible and despicable. simple. Take your pick from the dictionary of slang.
  • “global information tracker”: you're in a good mood, and it actually works for you. Angels sing, and a light suddenly fills the room.
  • “goddamn idiotic truckload of sh*t”: when it breaks