Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why moved code is not colored in git diff?

Tags:

git

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

like image 257
Eugen Konkov Avatar asked Jan 09 '18 10:01

Eugen Konkov


People also ask

What do the colors mean in git diff?

In the diff view there is a file tree showing all of the changes for that pull request. The colour of a particular file indicates the file change type: Green - ADDED (a new file was added) Blue - MODIFIED (an existing file was modified) Brown - COPIED (an existing file was copied to create a new file)

How does git diff change if you add the -- color words option to the command?

git diff --color-words git diff also has a special mode for highlighting changes with much better granularity: ‐‐color-words . This mode tokenizes added and removed lines by whitespace and then diffs those. Now the output displays only the color-coded words that have changed.

When might git diff head yield no results?

There is no output to git diff because Git doesn't see any changes inside your repository, only files outside the repository, which it considers 'untracked' and so ignores when generating a diff.

How does git diff work?

Diff command is used in git to track the difference between the changes made on a file. Since Git is a version control system, tracking changes are something very vital to it. Diff command takes two inputs and reflects the differences between them. It is not necessary that these inputs are files only.


1 Answers

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.

like image 134
Ry- Avatar answered Nov 27 '22 13:11

Ry-