gitgit-diff

why moved code is not colored in git diff?


I install latest git and configure it to highlight moved code:

$ git config diff.colormoved default

Here is how it looks when code is moved (see 1->2) enter image description here

But 3-4 is not highlighted as moved code.

Here is standalone changes:

enter image description here


Solution

  • See the documentation for --color-moved/colormoved in git-diff(1):

    --color-moved[=<mode>]

    Moved lines of code are colored differently. It can be changed by the diff.colorMoved configuration setting. The <mode> defaults to no if the option is not given and to zebra if the option with no mode is given. The mode must be one of:

    • no
      Moved lines are not highlighted.

    • default
      Is a synonym for zebra. This may change to a more sensible mode in the future.

    • plain
      Any line that is added in one location and was removed in another location will be colored with color.diff.newMoved. Similarly color.diff.oldMoved will be used for removed lines that are added somewhere else in the diff. This mode picks up any moved line, but it is not very useful in a review to determine if a block of code was moved without permutation.

    • zebra
      Blocks of moved text of at least 20 alphanumeric characters are detected greedily. The detected blocks are painted using either the color.diff.{old,new}Moved color or color.diff.{old,new}MovedAlternative. The change between the two colors indicates that a new block was detected.

    • dimmed_zebra
      Similar to zebra, but additional dimming of uninteresting parts of moved code is performed. The bordering lines of two adjacent blocks are considered interesting, the rest is uninteresting.

    specifically, that the default is zebra and that it detects

    Blocks of moved text of at least 20 alphanumeric characters

    . my $ctx = shift; doesn’t contain at least 20 alphanumeric characters. If you use git diff --color-moved=plain, or add # ten more ANs to the end of the line, your example will be highlighted as moved.