diff.c: emit moved lines with a different color
PATCH v3 1/1 diff.c: emit moved lines with a different color
When we color the diff, we'll mark moved lines with a different color.
This is achieved by doing a two passes over the diff. The first pass will inspect each line of the diff and store the removed lines and the added lines in its own hash map.
The second pass will check for each added line if that is found in the set of removed lines. If so it will color the added line differently as with the new moved-new color mode. For each removed line we check the set of added lines and if found emit that with the new color moved-old.
When detecting the moved lines, we cannot just rely on a line being equal, but we need to take the context into account to detect when the moves were reordered as we do not want to color moved but per-mutated lines. To do that we use the hash of the preceding line.
This patch was motivated by e.g. reviewing 3b0c4200 ("apply: move libified code from builtin/apply.c to apply.{c,h}", 2016-08-08)
---
* fixed memleaks
* only run the new code when needed.
* renamed variables to fit in better.
Documentation/config.txt | 5 +- contrib/completion/git-completion.bash | 2 + diff.c | 200 +++++++++++++++++++++++++++++++-- diff.h | 5 +- 4 files changed, 202 insertions(+), 10 deletions(-)
Documentation/config.txt +3 -2
@@ -980,8 +980,9 @@ color.diff.<slot>:: of `context` (context text - `plain` is a historical synonym), `meta` (metainformation), `frag` (hunk header), 'func' (function in hunk header), `old` (removed lines), - `new` (added lines), `commit` (commit headers), or `whitespace` - (highlighting whitespace errors). + `new` (added lines), `commit` (commit headers), `whitespace` + (highlighting whitespace errors), `moved-old` (removed lines that + reappear), `moved-new` (added lines that were removed elsewhere). color.decorate.<slot>:: Use customized color for 'git log --decorate' output. `<slot>` is one
contrib/completion/git-completion.bash +2 -0
@@ -2115,6 +2115,8 @@ _git_config () color.diff.old color.diff.plain color.diff.whitespace + color.diff.moved-old + color.diff.moved-new color.grep color.grep.context color.grep.filename
diff.c +193 -7
292 lines not on this page · raw message
diff.h +4 -1
@@ -122,6 +122,7 @@ struct diff_options {unsigned int filter;int use_color; +int color_moved;int context;int interhunkcontext;int break_opt;
@@ -189,7 +190,9 @@ enum color_diff {DIFF_FILE_NEW =5 ,DIFF_COMMIT =6 ,DIFF_WHITESPACE =7 , - DIFF_FUNCINFO = 8 +DIFF_FUNCINFO =8 , +DIFF_FILE_MOVED_NEW =9 , + DIFF_FILE_MOVED_OLD = 10 };const char *diff_get_color (int diff_use_color,enum color_diff ix);#define diff_get_color_opt (o, ix) \
Signature · 1 line
2.10.0.1.ga424fb3
Quoted 2 lines · Stefan Beller
When we color the diff, we'll mark moved lines with a different color.
Excellent idea. This is a very neat way to show extra information without cluttering the diff output.
Quoted 8 lines
This is achieved by doing a two passes over the diff. The first pass will inspect each line of the diff and store the removed lines and the added lines in its own hash map.
The second pass will check for each added line if that is found in the set of removed lines. If so it will color the added line differently as with the newmoved-newcolor mode. For each removed line we check the set of added lines and if found emit that with the new colormoved-old.
Makes sense.
Quoted 4 lines
When detecting the moved lines, we cannot just rely on a line being equal, but we need to take the context into account to detect when the moves were reordered as we do not want to color moved but per-mutated lines. To do that we use the hash of the preceding line.
Also makes sense.
Quoted 4 lines
This patch was motivated by e.g. reviewing
3b0c4200("apply: move libified code frombuiltin/apply.cto apply.{c,h}", 2016-08-08)
Yes, this would be quite helpful.
Quoted 5 lines
Documentation/config.txt +0
-0
Documentation/config.txt color.diff.<slot>:: · line 986
Quoted 9 lines
Documentation/config.txt +3
-2@@ -980,8 +980,9 @@ color.diff.<slot>:: of `context` (context text - `plain` is a historical synonym), `meta` (metainformation), `frag` (hunk header), 'func' (function in hunk header), `old` (removed lines), - `new` (added lines), `commit` (commit headers), or `whitespace` - (highlighting whitespace errors). + `new` (added lines), `commit` (commit headers), `whitespace` + (highlighting whitespace errors), `moved-old` (removed lines that + reappear), `moved-new` (added lines that were removed elsewhere).
I liked Junio's "Moved from" and "moved to" but I think moved old and moved new are ok as well.
Documentation/config.txt int git_diff_basic_config(const char *var, const char *value, void *cb) · line 312
Quoted 10 lines
Documentation/config.txt +6
-0@@ -287,6 +304,25 @@ int git_diff_basic_config(const char *var, const char *value, void *cb) return git_default_config(var, value, cb); } +static int moved_entry_cmp(const struct moved_entry *a, + const struct moved_entry *b, + const void *unused) +{ + return strcmp(a->line, b->line) && + a->hash_prev_line == b->hash_prev_line;
So we're only comparing them if they match and have a matching previous line? That seems pretty reasonable to reduce the cost of computing exact copied sequences.
Documentation/config.txt int git_diff_basic_config(const char *var, const char *value, void *cb) · line 318
Quoted 6 lines
Documentation/config.txt +5
-0+ if (ecbdata->opt->color_moved) { + int h = memhash(line, len); + hash_prev_removed = h; + hash_prev_added = h; + } }
If I understand, this is to ensure that we don't keep re-hashing each line right?
Thanks,
Jake
Quoted 41 lines · Jacob Keller
Quoted 2 lines · Stefan Beller
When we color the diff, we'll mark moved lines with a different color.
Excellent idea. This is a very neat way to show extra information without cluttering the diff output.
Quoted 8 lines
This is achieved by doing a two passes over the diff. The first pass will inspect each line of the diff and store the removed lines and the added lines in its own hash map.
The second pass will check for each added line if that is found in the set of removed lines. If so it will color the added line differently as with the newmoved-newcolor mode. For each removed line we check the set of added lines and if found emit that with the new colormoved-old.Makes sense.
Quoted 4 lines
When detecting the moved lines, we cannot just rely on a line being equal, but we need to take the context into account to detect when the moves were reordered as we do not want to color moved but per-mutated lines. To do that we use the hash of the preceding line.
Also makes sense.
Quoted 4 lines
This patch was motivated by e.g. reviewing
3b0c4200("apply: move libified code frombuiltin/apply.cto apply.{c,h}", 2016-08-08)Yes, this would be quite helpful.
Quoted 9 lines
Documentation/config.txt +0
-1@@ -980,8 +980,9 @@ color.diff.<slot>:: of `context` (context text - `plain` is a historical synonym), `meta` (metainformation), `frag` (hunk header), 'func' (function in hunk header), `old` (removed lines), - `new` (added lines), `commit` (commit headers), or `whitespace`
Quoted 8 lines
Documentation/config.txt color.diff.<slot>:: · line 986
Quoted 5 lines
Documentation/config.txt +3
-1- (highlighting whitespace errors). + `new` (added lines), `commit` (commit headers), `whitespace` + (highlighting whitespace errors), `moved-old` (removed lines that + reappear), `moved-new` (added lines that were removed elsewhere).I liked Junio's "Moved from" and "moved to" but I think moved old and moved new are ok as well.
as we do not want to see dashes ('moved-old'), I think I'l go with "movedfrom" and "movedto".
Quoted 23 lines
Documentation/config.txt int git_diff_basic_config(const char *var, const char *value, void *cb) · line 312
Quoted 11 lines
Documentation/config.txt +6
-0@@ -287,6 +304,25 @@ int git_diff_basic_config(const char *var, const char *value, void *cb) return git_default_config(var, value, cb); } +static int moved_entry_cmp(const struct moved_entry *a, + const struct moved_entry *b, + const void *unused) +{ + return strcmp(a->line, b->line) && + a->hash_prev_line == b->hash_prev_line;So we're only comparing them if they match and have a matching previous line? That seems pretty reasonable to reduce the cost of computing exact copied sequences.
Documentation/config.txt int git_diff_basic_config(const char *var, const char *value, void *cb) · line 318
Quoted 6 lines
Documentation/config.txt +5
-0+ if (ecbdata->opt->color_moved) { + int h = memhash(line, len); + hash_prev_removed = h; + hash_prev_added = h; + } }
Quoted 2 lines
If I understand, this is to ensure that we don't keep re-hashing each line right?
No, this is to ensure we have the context sensitivity of one prior line.
In the collection phase we look at each line of the patch and make a hash of it. Then we store the hash temporarily (think of a state machine that goes line by line and always keeps the hash of the last line)
What we store in the hashmaps is the hash(current line) ^ hash(previous applicable line).
With previous applicable line I mean any line starting with " " or "+" when the current
line starts with "+" and " " or "-" when the current line starts with "-".
When going through the second pass and actually emitting colored lines we only find matches in the hash map if the current line AND the previous line match as we lookup by hash code, i.e. if we have a moved line, but the previous line
changed we do not find it in the hashmap and we don't color it, such that the reviewer
can spot a permutation.
So in the second phase we also need to have access to previous line, so maybe we could also go with just taking the line with us instead of 2 hash codes. But that implementation detail seems like a trade off to me, where I'd lean on keeping the hashes around as lines may be very long in bad cases, whereas the hashcode is short and it is a cheap hash.
(I am referring to http://i.imgur.com/MnaSZ1D.png where in the malicious case all lines were moved to there as well, but permutated)
Quoted 23 lines · Stefan Beller
Quoted 2 lines
If I understand, this is to ensure that we don't keep re-hashing each line right?
No, this is to ensure we have the context sensitivity of one prior line.
In the collection phase we look at each line of the patch and make a hash of it. Then we store the hash temporarily (think of a state machine that goes line by line and always keeps the hash of the last line)
What we store in the hashmaps is the hash(current line) ^ hash(previous applicable line).
With previous applicable line I mean any line starting with " " or "+" when the current
line starts with "+" and " " or "-" when the current line starts with "-".When going through the second pass and actually emitting colored lines we only find matches in the hash map if the current line AND the previous line match as we lookup by hash code, i.e. if we have a moved line, but the previous line
changed we do not find it in the hashmap and we don't color it, such that the reviewer
can spot a permutation.
Quoted 8 lines
So in the second phase we also need to have access to previous line, so maybe we could also go with just taking the line with us instead of 2 hash codes. But that implementation detail seems like a trade off to me, where I'd lean on keeping the hashes around as lines may be very long in bad cases, whereas the hashcode is short and it is a cheap hash.
(I am referring to http://i.imgur.com/MnaSZ1D.png where in the malicious case all lines were moved to there as well, but permutated)
Good that clears up a lot of my questions. It seems like a reasonable algorithm, and I like the example of per-muted lines.
I think if we can find some real-world data on how much slower this is, we can find out what kind of configuration we want to give it? It seems like something incredibly helpful to me, but having a way out is nice incase it ends up being too costly. I suspect that will only be the case for a large patch.
Thanks,
Jake
Quoted 2 lines · Stefan Beller
as we do not want to see dashes ('moved-old'), I think I'l go with "movedfrom" and "movedto".
OK. They would be color.diff.movedFrom and color.diff.movedTo in the doc, and "movedfrom" and "movedto" in the code (as the caller already downcased them for you to strcmp()).
Quoted 7 lines
When going through the second pass and actually emitting colored lines we only find matches in the hash map if the current line AND the previous line match as we lookup by hash code, i.e. if we have a moved line, but the previous line
changed we do not find it in the hashmap and we don't color it, such that the reviewer
can spot a permutation.
Hmph. Does this have impact on a line that was at the beginning or the end of a file that got moved to the beginning or the end of a file (four permutations, of 9 if you throw in "middle"), because some cases it does not have a "previous" line?
Quoted 17 lines · Junio C Hamano
Quoted 2 lines · Stefan Beller
as we do not want to see dashes ('moved-old'), I think I'l go with "movedfrom" and "movedto".
OK. They would be color.diff.movedFrom and color.diff.movedTo in the doc, and "movedfrom" and "movedto" in the code (as the caller already downcased them for you to
strcmp()).Quoted 7 lines
When going through the second pass and actually emitting colored lines we only find matches in the hash map if the current line AND the previous line match as we lookup by hash code, i.e. if we have a moved line, but the previous line
changed we do not find it in the hashmap and we don't color it, such that the reviewer
can spot a permutation.
Quoted 4 lines
Hmph. Does this have impact on a line that was at the beginning or the end of a file that got moved to the beginning or the end of a file (four permutations, of 9 if you throw in "middle"), because some cases it does not have a "previous" line?
I spotted that problem as well. We need to adapt the algorithm a bit more:
If the previous line is of the same kind (i.e. starting with + or -),
then we have to take it into account,
otherwise (i.e. previous line is different, such as header, hunk header, or different sign)
then ignore previous line, as the previous line is already having a different color.
That works for the very first line of a file as well.
Quoted 25 lines · Stefan Beller
Quoted 17 lines · Junio C Hamano
Quoted 2 lines · Stefan Beller
as we do not want to see dashes ('moved-old'), I think I'l go with "movedfrom" and "movedto".
OK. They would be color.diff.movedFrom and color.diff.movedTo in the doc, and "movedfrom" and "movedto" in the code (as the caller already downcased them for you to
strcmp()).Quoted 7 lines
When going through the second pass and actually emitting colored lines we only find matches in the hash map if the current line AND the previous line match as we lookup by hash code, i.e. if we have a moved line, but the previous line
changed we do not find it in the hashmap and we don't color it, such that the reviewer
can spot a permutation.Quoted 4 lines
Hmph. Does this have impact on a line that was at the beginning or the end of a file that got moved to the beginning or the end of a file (four permutations, of 9 if you throw in "middle"), because some cases it does not have a "previous" line?
I spotted that problem as well. We need to adapt the algorithm a bit more:
Quoted 8 lines
If the previous line is of the same kind (i.e. starting with + or -),
then we have to take it into account,
otherwise (i.e. previous line is different, such as header, hunk header, or different sign)
then ignore previous line, as the previous line is already having a different color.That works for the very first line of a file as well.
That should work to resolve some issues, and ensure the first line of a hunk works correctly.
Thanks,
Jake