Color moved code differently
cover Color moved code differently
When moving code (e.g. a function is moved to another part of the file or to a different file), the review process is different than reviewing new code. When reviewing moved code we are only interested in the diff as where there are differences in the moved code, e.g. namespace changes.
However the inner part of these moved texts should not change. To aid a developer reviewing such code, we'll color pure moved stuff differently.
A line is colored differently if that line and the surroundign 2 lines appear as-is in the opposite part of the diff.
Example:
http://i.imgur.com/ay84q0q.png
Or apply these patches and
git show e28eae3184b26d3cf3293e69403babb5c575342c
git show bc9204d4ef6e0672389fdfb0d398fa9a39dba3d5
git show 8465541e8ce8eaf16e66ab847086779768c18f2dThe code quality sucks though, hence RFC.
Thanks,
Stefan
Stefan Beller (2):
diff.c: emit duplicate lines with a different color WIP xdiff: markup duplicates differently
diff.c | 26 ++++++++++++ diff.h | 4 +- xdiff/xdiff.h | 1 + xdiff/xemit.c | 128 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 156 insertions(+), 3 deletions(-)
Signature · 1 line
2.10.0.rc2.23.gf336a1a.dirty
PATCH 1/2 diff.c: emit duplicate lines with a different color
Color is WIP, I just make space for a different case.
--- diff.c | 26 ++++++++++++++++++++++++++ diff.h | 4 +++- 2 files changed, 29 insertions(+), 1 deletion(-)
diff.c +26 -0
@@ -52,6 +52,8 @@ static char diff_colors[][COLOR_MAXLEN] = {GIT_COLOR_YELLOW ,/* COMMIT */ GIT_COLOR_BG_RED ,/* WHITESPACE */ GIT_COLOR_NORMAL ,/* FUNCINFO */ +GIT_COLOR_BLUE ,/* NEW DUPLICATE */ + GIT_COLOR_CYAN, /* OLD DUPLICATE */ };static int parse_diff_color_slot (const char *var)
@@ -541,6 +543,14 @@ static void emit_add_line(const char *reset, DIFF_FILE_NEW, WSEH_NEW, '+'); } +static void emit_add_line_dup (const char *reset, +struct emit_callback *ecbdata, +const char *line,int len) +{ +emit_line_checked (reset, ecbdata, line, len, +DIFF_FILE_DUPLICATE_NEW ,WSEH_NEW ,'+' ); +} +static void emit_del_line (const char *reset,struct emit_callback *ecbdata,const char *line,int len)
@@ -549,6 +559,14 @@ static void emit_del_line(const char *reset, DIFF_FILE_OLD, WSEH_OLD, '-'); } +static void emit_del_line_dup (const char *reset, +struct emit_callback *ecbdata, +const char *line,int len) +{ +emit_line_checked (reset, ecbdata, line, len, +DIFF_FILE_DUPLICATE_OLD ,WSEH_OLD ,'-' ); +} +static void emit_context_line (const char *reset,struct emit_callback *ecbdata,const char *line,int len)
@@ -1300,6 +1318,10 @@ static void fn_out_consume(void *priv, char *line, unsigned long len) }switch (line[0 ]) { +case '*' : + ecbdata->lno_in_postimage++; +emit_add_line_dup (reset, ecbdata, line +1 , len -1 ); +break ;case '+' : ecbdata->lno_in_postimage++;emit_add_line (reset, ecbdata, line +1 , len -1 );
@@ -1308,6 +1330,10 @@ static void fn_out_consume(void *priv, char *line, unsigned long len) ecbdata->lno_in_preimage++;emit_del_line (reset, ecbdata, line +1 , len -1 );break ; +case '~' : + ecbdata->lno_in_preimage++; +emit_del_line_dup (reset, ecbdata, line +1 , len -1 ); +break ;case ' ' : ecbdata->lno_in_postimage++; ecbdata->lno_in_preimage++;
diff.h +3 -1
@@ -189,7 +189,9 @@ enum color_diff {DIFF_FILE_NEW =5 ,DIFF_COMMIT =6 ,DIFF_WHITESPACE =7 , - DIFF_FUNCINFO = 8 +DIFF_FUNCINFO =8 , +DIFF_FILE_DUPLICATE_NEW =9 , + DIFF_FILE_DUPLICATE_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.rc2.23.gf336a1a.dirty
PATCH 2/2 WIP xdiff: markup duplicates differently
In reply to Stefan BellerWhen moving code (e.g. a function is moved to another part of the file or to a different file), the review process is different than reviewing new code. When reviewing moved code we are only interested in the diff as where there are differences in the moved code, e.g. namespace changes.
However the inner part of these moved texts should not change. To aid a developer reviewing such code, emit it with a different prefix than the usual +,- to indicate it is overlapping code.
Examples from recent history:
git show e28eae3184b26d3cf3293e69403babb5c575342c
git show bc9204d4ef6e0672389fdfb0d398fa9a39dba3d5
git show 8465541e8ce8eaf16e66ab847086779768c18f2dThis doesn't work yet, but we should make this patch series work to ignore white space changes:9d1ca1dac0ebfd6e17d73e33b2d173926c139c2d
--- xdiff/xdiff.h | 1 + xdiff/xemit.c | 128 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 127 insertions(+), 2 deletions(-)
xdiff/xdiff.h +1 -0
@@ -45,6 +45,7 @@ extern "C" {#define XDL_EMIT_FUNCNAMES (1 << 0)#define XDL_EMIT_FUNCCONTEXT (1 << 2) +#define XDL_EMIT_DUPLICATE (1 << 3)#define XDL_MMB_READONLY (1 << 0)
xdiff/xemit.c +126 -2
158 lines not on this page · raw message
Signature · 1 line
2.10.0.rc2.23.gf336a1a.dirty
Quoted 10 lines · Stefan Beller
A line is colored differently if that line and the surroundign 2 lines appear as-is in the opposite part of the diff.
Example:
http://i.imgur.com/ay84q0q.pngOr apply these patches and
git show e28eae3184b26d3cf3293e69403babb5c575342c git show bc9204d4ef6e0672389fdfb0d398fa9a39dba3d5 git show 8465541e8ce8eaf16e66ab847086779768c18f2d
I like this as a concept. Two quick comments are
* On 1/2, you would also need to teach diff-color-slot the correspondence between the name used by configuration and the enum used as the index into the diff_colors[] array. I think these are not "DUPLICATE", but "MOVE", so I'd suggest renaming dup-new and dup-old to some words or phrases that have "MOVED" and "TO" or "FROM" in them (e.g. "DIFF_MOVED_FROM", "DIFF_MOVED_TO").
* On 2/2, doing it at xdiff.c level may be limiting this good idea to flourish to its full potential, as the interface is fed only one diff_filepair at a time. All the examples you pointed at above have line movement within a single path because of this design limitation. I do not think 2/2 would serve as a small but good first step to build on top of to enhance the feature to notice line movements across files and the design (not the implementation) needs rethinking.
The idea has a potential to help reviewing inter-file movement of lines in 3b0c4200 ("apply: move libified code from builtin/apply.c to apply.{c,h}", 2016-08-08). You can see what was _changed_ in the part that has been moved across files with "show -B -M", and sometimes that is useful, but at the same time, you cannot see what was moved without changing, which often is necessary to understand the changes and notice things like "you moved this across files without changing, but this and that you did not change need to be adjusted".
The coloring of "these are moved verbatim" in the style this series gives would be very helpful for reviewing such a change.
Re: [RFC/PATCH 2/2] WIP xdiff: markup duplicates differently
In reply to Stefan BellerW dniu 03.09.2016 o 05:31, Stefan Beller pisze:
Quoted 8 lines
When moving code (e.g. a function is moved to another part of the file or to a different file), the review process is different than reviewing new code. When reviewing moved code we are only interested in the diff as where there are differences in the moved code, e.g. namespace changes.
However the inner part of these moved texts should not change. To aid a developer reviewing such code, emit it with a different prefix than the usual +,- to indicate it is overlapping code.
What would be this different prefix?
Side note: I wonder if the cousin of unified diff, namely context diff[1], is something that we can and should support.
[1]: https://www.gnu.org/software/diffutils/manual/html_node/Context-Format.html
https://www.gnu.org/software/diffutils/manual/html_node/Detailed-Context.html
*** lao 2002-02-21 23:30:39.942229878 -0800
--- tzu 2002-02-21 23:30:50.442260588 -0800
***************
*** 1,7 ****
- The Way that can be told of is not the eternal Way;
- The name that can be named is not the eternal name. The Nameless is the origin of Heaven and Earth;
! The Named is the mother of all things.
Therefore let there always be non-being,
so we may see their subtlety,
And let there always be being,
--- 1,6 ----
The Nameless is the origin of Heaven and Earth;
! The named is the mother of all things.
!
Therefore let there always be non-being,
so we may see their subtlety,
And let there always be being,
***************
*** 9,11 ****
--- 8,13 ----
The two are the same,
But after they are produced,
they have different names.
+ They both may be called deep and profound.
+ Deeper and more profound,
+ The door of all subtleties!
Quoted 8 lines · Junio C Hamano
Quoted 6 lines · Stefan Beller
A line is colored differently if that line and the surroundign 2 lines appear as-is in the opposite part of the diff.
Example:
http://i.imgur.com/ay84q0q.png
Quoted 6 lines
Quoted 4 lines
Or apply these patches and
git show e28eae3184b26d3cf3293e69403babb5c575342c git show bc9204d4ef6e0672389fdfb0d398fa9a39dba3d5 git show 8465541e8ce8eaf16e66ab847086779768c18f2dI like this as a concept. Two quick comments are
Great!
Quoted 4 lines
* On 1/2, you would also need to teach diff-color-slot the correspondence between the name used by configuration and the enum used as the index into the diff_colors[] array.
So I would need to add code to diff_parse_color_slot just below the definition of the struct.
Quoted 5 lines
I think
these are not "DUPLICATE", but "MOVE", so I'd suggest renaming dup-new and dup-old to some words or phrases that have "MOVED" and "TO" or "FROM" in them (e.g. "DIFF_MOVED_FROM", "DIFF_MOVED_TO").
Ok, sounds sensible.
Quoted 4 lines
* On 2/2, doing it at
xdiff.clevel may be limiting this good idea to flourish to its full potential, as the interface is fed only onediff_filepairat a time.
I realized that after I implemented it. I agree we would want to have it function cross file.
So from my current understanding of the code,
* diffcore_std would call a new function diffcore_detect_moved(void) just before diffcore_apply_filter is called.
* The new function diffcore_detect_moved would then check if the diff is a valid textual diff (i.e. real files, not submodules, but deletion/creation of one file is allowed)
If so we generate the diff internally and as in 2/2 would hash all added/removed lines with context and store it.
* Instead checking for a different symbol in fn_out_consume, we consult the hashmap whether we want to color the line as a "moved" line.
Quoted 6 lines
All the examples you pointed at
above have line movement within a single path because of this design limitation. I do not think 2/2 would serve as a small but good first step to build on top of to enhance the feature to notice line movements across files and the design (not the implementation) needs rethinking.
After reading the code more closely I agree. I initially put it there to see if it is feasible or just messing up the diff. And as I have a bit of knowledge of the xdl internals due to the first version of the diff slider heuristic, I went with that.
From a cursory look at the history, you seemed to be very involved in the implementation of diff.c, so I'd appreciate strong guidance on the design level.
Quoted 11 lines
The idea has a potential to help reviewing inter-file movement of lines in
3b0c4200("apply: move libified code frombuiltin/apply.cto apply.{c,h}", 2016-08-08). You can see what was _changed_ in the part that has been moved across files with "show -B -M", and sometimes that is useful, but at the same time, you cannot see what was moved without changing, which often is necessary to understand the changes and notice things like "you moved this across files without changing, but this and that you did not change need to be adjusted".
Quoted 3 lines
The coloring of "these are moved verbatim" in the style this series gives would be very helpful for reviewing such a change.
Right, that is what triggered me to implement it. Another such case that I reviewed was
https://git.eclipse.org/r/#/c/78645/
and I felt very insecure about reviewing thousands lines of code that was "just moved".
Thanks,
Stefan
Re: [RFC/PATCH 2/2] WIP xdiff: markup duplicates differently
In reply to Jakub NarębskiQuoted 7 lines · Jakub Narębski
W dniu 03.09.2016 o 05:31, Stefan Beller pisze:
Quoted 5 lines
When moving code (e.g. a function is moved to another part of the file or to a different file), the review process is different than reviewing new code. When reviewing moved code we are only interested in the diff as where there are differences in the moved code, e.g. namespace changes.
Quoted 5 lines
Quoted 3 lines
However the inner part of these moved texts should not change. To aid a developer reviewing such code, emit it with a different prefix than the usual +,- to indicate it is overlapping code.
What would be this different prefix?
I will discard the part of the different prefix as the design of 2/2 will change.
Quoted 11 lines
Side note: I wonder if the cousin of unified diff, namely context diff[1], is something that we can and should support.
[1]: https://www.gnu.org/software/diffutils/manual/html_node/Context-Format.html
https://www.gnu.org/software/diffutils/manual/html_node/Detailed-Context.html
Quoted 15 lines
*** lao 2002-02-21 23:30:39.942229878 -0800 --- tzu 2002-02-21 23:30:50.442260588 -0800
***************
*** 1,7 ****
- The Way that can be told of is not the eternal Way;
- The name that can be named is not the eternal name. The Nameless is the origin of Heaven and Earth;
! The Named is the mother of all things.
Therefore let there always be non-being,
so we may see their subtlety,
And let there always be being,
--- 1,6 ----
The Nameless is the origin of Heaven and Earth;
! The named is the mother of all things.
!
So the line moved here?
Is it intentional that the line differs though?
(capitalisation of 'named")
Not sure I can read this diff correctly.
I think for this small side project I'd rather want to 'just' support colors of moved code;)
Quoted 12 lines
Therefore let there always be non-being,
so we may see their subtlety,
And let there always be being,
***************
*** 9,11 ****
--- 8,13 ----
The two are the same,
But after they are produced,
they have different names.
+ They both may be called deep and profound.
+ Deeper and more profound,
+ The door of all subtleties!
Quoted 7 lines · Stefan Beller
Quoted 3 lines
* On 2/2, doing it at
xdiff.clevel may be limiting this good idea to flourish to its full potential, as the interface is fed only onediff_filepairat a time.I realized that after I implemented it. I agree we would want to have it function cross file.
Quoted 8 lines
So from my current understanding of the code,
*diffcore_stdwould call a new functiondiffcore_detect_moved(void)just beforediffcore_apply_filteris called.
* The new functiondiffcore_detect_movedwould then check if the diff is a valid textual diff (i.e. real files, not submodules, but deletion/creation of one file is allowed)
If so we generate the diff internally and as in 2/2 would hash all added/removed lines with context and store it.
I do not think you should step outside diff_flush(). Only when producing textual diff, you would have to run the textual diff twice by going over the q twice:
* The first pass would run diff_flush_patch(), which would call into xdiff the usual way, but the callback from xdiff would capture the removed lines and the added lines without making any output.
* The second pass would run diff_flush_patch(), but the callback from xdiff would be called with additional information, namely, the removed and the added lines captured in the first pass.
* I suspect that the fn_out_consume() function that is used for a normal case (i.e. when we are not doing this more expensive "moved to/moved from" coloring) can be used for the second pass above (i.e. the "priv" aka "ecbdata" may need to be extended so that it can tell which mode of operation it is asked to perform), but if there is not enough similarity between the second pass of this "moved from/moved to" mode and the normal mode of output, it is also OK to have two different callback functions, i.e. the original one to be used in the normal mode, the second one that knows the "these are moved without modification" coloring. The callback for the first pass is sufficiently different and I think it is better to invent a new callback function to be used in the first pass, instead of reusing fn_out_consume().
The fn_out_consume() function working in the "second pass of moved from/moved to mode" would inspect line[] and see if it is an added or a removed line, and then:
- if it is an added line, and it appears as a removed line elsewhere in the patchset (you obtained the information in the first pass), you show it as "this was moved from elsewhere".
- if it is a removed line, and it appears as an added line elsewhere in the patchset (you obtained the information in the first pass), you show it as "this was moved to elsewhere".
Or something like that.
Re: [RFC/PATCH 2/2] WIP xdiff: markup duplicates differently
In reply to Jakub NarębskiQuoted 2 lines · Jakub Narębski
Side note: I wonder if the cousin of unified diff, namely context diff[1], is something that we can and should support.
Yes, the lack of support for the copied context (instead of the unified context) diff format has bugged me over the years. Reading copied context diff format however is rapidly becoming a lost art, unfortunately, partly due to our popularity, and the need for the format has become a lessor issue.
I wonder if a pair of a pre-processor (on the input side, before running 'git apply') and a post-processor (on the output side, munging the output from 'git show/diff') would be sufficient.
In any case, I agree with you that it has nothing to do with what Stefan is doing here.
Quoted 18 lines · Junio C Hamano
Quoted 7 lines · Stefan Beller
Quoted 3 lines
* On 2/2, doing it at
xdiff.clevel may be limiting this good idea to flourish to its full potential, as the interface is fed only onediff_filepairat a time.I realized that after I implemented it. I agree we would want to have it function cross file.
Quoted 8 lines
So from my current understanding of the code,
*diffcore_stdwould call a new functiondiffcore_detect_moved(void)just beforediffcore_apply_filteris called.
* The new functiondiffcore_detect_movedwould then check if the diff is a valid textual diff (i.e. real files, not submodules, but deletion/creation of one file is allowed)
If so we generate the diff internally and as in 2/2 would hash all added/removed lines with context and store it.
Quoted 1 line
I do not think you should step outside
diff_flush().
After looking further into it, I think we need to step outside or at the very least deep down in the xdl cellar from there, because we need the context around the line both in the pre and after image.
Quoted 8 lines
Only when
producing textual diff, you would have to run the textual diff twice by going over the q twice:* The first pass would run
diff_flush_patch(), which would call into xdiff the usual way, but the callback from xdiff would capture the removed lines and the added lines without making any output.
The callback doesn't have the context around one line as easily accessible as the xdl emit function, so rather we'd pass in a flag, i.e. the hashmap where to store the added/removed lines or NULL.
Both passes (the first to find and the second enhanced pass that outputs the lines) need to have access to the context of the respective file.
Quoted 5 lines
* The second pass would run
diff_flush_patch(), but the callback from xdiff would be called with additional information, namely, the removed and the added lines captured in the first pass.
Quoted 13 lines
* I suspect that the
fn_out_consume()function that is used for a normal case (i.e. when we are not doing this more expensive "moved to/moved from" coloring) can be used for the second pass above (i.e. the "priv" aka "ecbdata" may need to be extended so that it can tell which mode of operation it is asked to perform), but if there is not enough similarity between the second pass of this "moved from/moved to" mode and the normal mode of output, it is also OK to have two different callback functions, i.e. the original one to be used in the normal mode, the second one that knows the "these are moved without modification" coloring. The callback for the first pass is sufficiently different and I think it is better to invent a new callback function to be used in the first pass, instead of reusingfn_out_consume().
The callback doesn't have easy accessible data IMHO, e.g. we may get these lines in the callback:
(function context not required:)
---8<---
{
int i;
for (i = 0; i < GIT_SHA1_RAWSZ; i++) {
- unsigned int val;
- /*
- * hex[1]=='\0' is caught when val is checked below,
- * but if hex[0] is NUL we have to avoid reading
- * past the end of the string:
- */
- if (!hex[0])
- return -1;
- val = (hexval(hex[0]) << 4) | hexval(hex[1]);
- if (val & ~0xff)
+ int val = hex2chr(hex);
+ if (val < 0)
return -1;
*sha1++ = val;
hex += 2;---8<---
And for the first + line (int val = hex2chr(hex);) we need to hash that line and 2 (made up threshold) before and after:
---8<---
int i;
for (i = 0; i < GIT_SHA1_RAWSZ; i++) {
int val = hex2chr(hex);
if (val < 0)
return -1;---8<---
and this is hard to reconstruct from the above diff fed into the callback. So we have to do it in the xdl emit phase.
So I think the wrong part of the initial patch 2/2 is the too late recording of the moved lines.
So rather whenever we call diff_flush_patch(), we have to first call diff_prepare_moved_line_detection() first for all file pairs.
Quoted 14 lines
The
fn_out_consume()function working in the "second pass of moved from/moved to mode" would inspect line[] and see if it is an added or a removed line, and then:- if it is an added line, and it appears as a removed line elsewhere in the patchset (you obtained the information in the first pass), you show it as "this was moved from elsewhere".
- if it is a removed line, and it appears as an added line elsewhere in the patchset (you obtained the information in the first pass), you show it as "this was moved to elsewhere".
Or something like that.
Quoted 9 lines · Stefan Beller
When moving code (e.g. a function is moved to another part of the file or to a different file), the review process is different than reviewing new code. When reviewing moved code we are only interested in the diff as where there are differences in the moved code, e.g. namespace changes.
However the inner part of these moved texts should not change. To aid a developer reviewing such code, we'll color pure moved stuff differently.
Quoted 6 lines
A line is colored differently if that line and the surroundign 2 lines appear as-is in the opposite part of the diff.
Example:
http://i.imgur.com/ay84q0q.png
In the example, the first and last lines of duplicate copies don't get colored differently, and that threw me off. I feel like that was maybe not intentional? If it was, can you explain why?
Thanks,
Jake
Re: [RFC/PATCH 2/2] WIP xdiff: markup duplicates differently
In reply to Stefan BellerW dniu 04.09.2016 o 07:31, Stefan Beller pisze:
Quoted 8 lines · Jakub Narębski
Quoted 7 lines
W dniu 03.09.2016 o 05:31, Stefan Beller pisze:
Quoted 5 lines
When moving code (e.g. a function is moved to another part of the file or to a different file), the review process is different than reviewing new code. When reviewing moved code we are only interested in the diff as where there are differences in the moved code, e.g. namespace changes.
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
However the inner part of these moved texts should not change. To aid a developer reviewing such code, emit it with a different prefix than the usual +,- to indicate it is overlapping code.
What would be this different prefix?
I will discard the part of the different prefix as the design of 2/2 will change.
It would be nice to have at least an option of using different prefix (or pair of prefixes), as not always it is possible to use color to markup duplicates.
P.S. BTW. does this work with word-diff?
Best regards,
Signature · 1 line
Jakub Narębski
Quoted 21 lines · Junio C Hamano
I do not think you should step outside
diff_flush(). Only when producing textual diff, you would have to run the textual diff twice by going over the q twice:* The first pass would run
diff_flush_patch(), which would call into xdiff the usual way, but the callback from xdiff would capture the removed lines and the added lines without making any output.* The second pass would run
diff_flush_patch(), but the callback from xdiff would be called with additional information, namely, the removed and the added lines captured in the first pass.
...
Thefn_out_consume()function working in the "second pass of moved from/moved to mode" would inspect line[] and see if it is an added or a removed line, and then:- if it is an added line, and it appears as a removed line elsewhere in the patchset (you obtained the information in the first pass), you show it as "this was moved from elsewhere".
Quoted 5 lines
- if it is a removed line, and it appears as an added line elsewhere in the patchset (you obtained the information in the first pass), you show it as "this was moved to elsewhere".
Or something like that.
Actually, the first pass above ends up queuing majority of the diff output in-core _anyway_, so it would make much more sense not to rely on the reproducibility of xdiff machinery by not calling it twice. Instead, I would imagine that the first pass can queue all what it receives in the callback, then with the whole-patch picture at hand, you can call updated fn_out_consume() yourself (instead of letting xdiff machinery to call you), and that would become the "second pass".