Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the purpose of dimmed-zebra?

Tags:

git

git diff has this feature --color-moved=dimmed-zebra which sounds really nifty but I just don't get it. If found zebra to be very useful since it shows moved blocks. But dimmed_zebra seems completely arbitrary to me. Why are "the bordering lines of two adjacent blocks...considered interesting"? How does this feature help me analyze the diff more effectively? I feel like this feature would be useful if I understood its intended purpose. A concrete example might be helpful.

Here's the snippet from the manpage.

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.

like image 940
cambunctious Avatar asked Jan 25 '23 21:01

cambunctious


2 Answers

The dimmed-zebra mode is useful when the patch consists mostly of moved or copied code with a few lines changed in the middle of the copied code. Take, for example, a particular commit from Git (this link won't show the mode, however).

The zebra mode shows that there are two separate moved blocks: the former and the latter, colored in purple and blue, both of which are moved later with a single line inserted between them. Here's an example of one moved stanza:

The old location of code with zebra highlighting, in blue and purple. The new location of code with zebra highlighting, in light blue with a single added line in green.

It's a little hard to notice the added line; it doesn't really stand out.

With dimmed-zebra, almost all of the moved code is grey because it's ultimately uninteresting. The only things that are highlighted in colors are the end of the first block, the beginning of the second block (the next line), and the lines that's added in the destination. This mode lets you focus on (a) the added line and (b) the borders of where they came from, without caring about any of the moved code.

Here's what that code looks like in dimmed-zebra mode. Notice how your eye is drawn to the relevant portions only:

The old code in dimmed-zebra highlighting, with most of the data in grey. The new code in dimmed-zebra highlighting, with only the added line in green.

So basically, this is just an optimization for human vision to make it easier to notice what really matters.

like image 154
bk2204 Avatar answered Jan 28 '23 10:01

bk2204


@bk2204's answer helped me understand with a nice example and now I want to offer another answer.

What does it mean for moved lines to be "interesting", and not dimmed?

When you see two highlighted lines that are at the border of two removed and moved blocks of code, you should think, "These lines are not adjacent anymore; Some code was inserted here or these blocks were completely separated."

Likewise, when you see two highlighted lines that are at the border of two added and moved blocks of code, you should think, "These lines did not used to be adjacent; Some code was removed here or these blocks came from completely different places".

like image 40
cambunctious Avatar answered Jan 28 '23 11:01

cambunctious