Reroll of sb/diff-color-moved.
PATCH 2/25 diff.c: move line ending check into emit_hunk_header
In reply to Stefan BellerThe emit_hunk_header() function is responsible for assembling a hunk header and calling emit_line() to send the hunk header to the output file. Its only caller fn_out_consume() needs to prepare for a case where the function emits an incomplete line and add the terminating LF.
Instead make sure emit_hunk_header() to always send a completed line to emit_line().
--- diff.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
diff.c +2 -2
@@ -678,6 +678,8 @@ static void emit_hunk_header(struct emit_callback *ecbdata, }strbuf_add (&msgbuf, line + len, org_len - len); +strbuf_complete_line (&msgbuf); +emit_line (ecbdata->opt,"" ,"" , msgbuf.buf, msgbuf.len);strbuf_release (&msgbuf); }
@@ -1315,8 +1317,6 @@ static void fn_out_consume(void *priv, char *line, unsigned long len) len =sane_truncate_line (ecbdata, line, len);find_lno (line, ecbdata);emit_hunk_header (ecbdata, line, len); -if (line[len-1 ] !='\n' ) -putc ('\n' , o->file);return ; }
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH 12/25 diff.c: emit_diff_symbol learns DIFF_SYMBOL_HEADER
In reply to Stefan BellerThe header is constructed lazily including line breaks, so just emit the raw string as is.
--- diff.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-)
diff.c +20 -8
@@ -561,6 +561,7 @@ static void emit_line(struct diff_options *o, const char *set, const char *reset }enum diff_symbol { +DIFF_SYMBOL_HEADER ,DIFF_SYMBOL_FILEPAIR_PLUS ,DIFF_SYMBOL_FILEPAIR_MINUS ,DIFF_SYMBOL_WORDS_PORCELAIN ,
@@ -689,6 +690,9 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s, line, reset, strchr(line, ' ') ? "\t" : "");break ; +case DIFF_SYMBOL_HEADER : +fprintf (o->file,"%s" , line); +break ;default :die ("BUG: unknown diff symbol" ); }
@@ -1385,7 +1389,8 @@ static void fn_out_consume(void *priv, char *line, unsigned long len) o->found_changes =1 ;if (ecbdata->header) { -fprintf (o->file,"%s" , ecbdata->header->buf); +emit_diff_symbol (o,DIFF_SYMBOL_HEADER , + ecbdata->header->buf, ecbdata->header->len,0 );strbuf_reset (ecbdata->header); ecbdata->header =NULL ; }
@@ -2519,7 +2524,8 @@ static void builtin_diff(const char *name_a,if (complete_rewrite && (textconv_one || !diff_filespec_is_binary (one)) && (textconv_two || !diff_filespec_is_binary (two))) { -fprintf (o->file,"%s" , header.buf); +emit_diff_symbol (o,DIFF_SYMBOL_HEADER , + header.buf, header.len,0 );strbuf_reset (&header);emit_rewrite_diff (name_a, name_b, one, two, textconv_one, textconv_two, o);
@@ -2529,7 +2535,8 @@ static void builtin_diff(const char *name_a, }if (o->irreversible_delete && lbl[1 ][0 ] =='/' ) { -fprintf (o->file,"%s" , header.buf); +emit_diff_symbol (o,DIFF_SYMBOL_HEADER , header.buf, + header.len,0 );strbuf_reset (&header); goto free_ab_and_return; } else if (!DIFF_OPT_TST(o, TEXT) &&
@@ -2540,10 +2547,13 @@ static void builtin_diff(const char *name_a, !DIFF_OPT_TST(o, BINARY)) {if (!oidcmp (&one->oid, &two->oid)) {if (must_show_header) -fprintf (o->file,"%s" , header.buf); +emit_diff_symbol (o,DIFF_SYMBOL_HEADER , + header.buf, header.len, +0 ); goto free_ab_and_return; } -fprintf (o->file,"%s" , header.buf); +emit_diff_symbol (o,DIFF_SYMBOL_HEADER , + header.buf, header.len,0 );fprintf (o->file,"%sBinary files %s and %s differ\n" , line_prefix, lbl[0 ], lbl[1 ]); goto free_ab_and_return;
@@ -2554,10 +2564,11 @@ static void builtin_diff(const char *name_a,if (mf1.size == mf2.size && !memcmp (mf1.ptr, mf2.ptr, mf1.size)) {if (must_show_header) -fprintf (o->file,"%s" , header.buf); +emit_diff_symbol (o,DIFF_SYMBOL_HEADER , + header.buf, header.len,0 ); goto free_ab_and_return; } -fprintf (o->file,"%s" , header.buf); +emit_diff_symbol (o,DIFF_SYMBOL_HEADER , header.buf, header.len,0 );strbuf_reset (&header);if (DIFF_OPT_TST (o,BINARY ))emit_binary_diff (o->file, &mf1, &mf2, line_prefix);
@@ -2575,7 +2586,8 @@ static void builtin_diff(const char *name_a,const struct userdiff_funcname *pe;if (must_show_header) { -fprintf (o->file,"%s" , header.buf); +emit_diff_symbol (o,DIFF_SYMBOL_HEADER , + header.buf, header.len,0 );strbuf_reset (&header); }
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH 4/25 diff.c: introduce emit_diff_symbol
In reply to Stefan BellerIn a later patch we want to buffer all output before emitting it as a new feature ("markup moved lines") conceptually cannot be implemented in a single pass over the output.
There are different approaches to buffer all output such as:
* Buffering on the char level, i.e. we'd have a char[] which would grow at approximately 80 characters a line. This would keep the output completely unstructured, but might be very easy to implement, such as redirecting all output to a temporary file and working off that. The later passes over the buffer are quite complicated though, because we have to parse back any output and then decide if it should be modified.
* Buffer on a line level. As the output is mostly line oriented already, this would make sense, but it still is a bit awkward as we'd have to make sense of it again by looking at the first characters of a line to decide what part of a diff a line is.
* Buffer semantically. Imagine there is a formal grammar for the diff output and we'd keep the symbols of this grammar around. This keeps the highest level of structure in the buffered data, such that the actual memory requirements are less than say the first option. Instead of buffering the characters of the line, we'll buffer what we intend to do plus additional information for the specifics. An output of
diff --git a/new.txt b/new.txt
index fa69b07..412428c 100644
Binary files a/new.txt and b/new.txt differ could be buffered as
DIFF_SYMBOL_DIFF_START + new.txt DIFF_SYMBOL_INDEX_MODE + fa69b07 412428c "non-executable" flag DIFF_SYMBOL_BINARY_FILES + new.txt
This and the following patches introduce the third option of buffering by first moving any output to emit_diff_symbol, and then introducing the buffering in this function.
--- diff.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-)
diff.c +19 -3
@@ -560,6 +560,24 @@ static void emit_line(struct diff_options *o, const char *set, const char *resetemit_line_0 (o, set, reset, line[0 ], line+1 , len-1 ); } +enum diff_symbol { +DIFF_SYMBOL_SEPARATOR +}; + +static void emit_diff_symbol (struct diff_options *o,enum diff_symbol s, +const char *line,int len) +{ +switch (s) { +case DIFF_SYMBOL_SEPARATOR : +fprintf (o->file,"%s%c" , +diff_line_prefix (o), + o->line_termination); +break ; +default : +die ("BUG: unknown diff symbol" ); + } +} +static int new_blank_line_at_eof (struct emit_callback *ecbdata,const char *line,int len) { if (!((ecbdata->ws_rule & WS_BLANK_AT_EOF) &&
@@ -4828,9 +4846,7 @@ void diff_flush(struct diff_options *options)if (output_format &DIFF_FORMAT_PATCH ) {if (separator) { -fprintf (options->file,"%s%c" , -diff_line_prefix (options), - options->line_termination); +emit_diff_symbol (options,DIFF_SYMBOL_SEPARATOR ,NULL ,0 );if (options->stat_sep) {/* attach patch instead of inline */ fputs (options->stat_sep, options->file);
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH 6/25 diff.c: emit_diff_symbol learns DIFF_SYMBOL_CONTEXT_FRAGINFO
In reply to Stefan Beller--- diff.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
diff.c +6 -2
@@ -561,6 +561,7 @@ static void emit_line(struct diff_options *o, const char *set, const char *reset }enum diff_symbol { +DIFF_SYMBOL_CONTEXT_FRAGINFO ,DIFF_SYMBOL_CONTEXT_MARKER ,DIFF_SYMBOL_SEPARATOR };
@@ -570,6 +571,9 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s, {const char *context, *reset;switch (s) { +case DIFF_SYMBOL_CONTEXT_FRAGINFO : +emit_line (o,"" ,"" , line, len); +break ;case DIFF_SYMBOL_CONTEXT_MARKER : context =diff_get_color_opt (o,DIFF_CONTEXT ); reset =diff_get_color_opt (o,DIFF_RESET );
@@ -705,8 +709,8 @@ static void emit_hunk_header(struct emit_callback *ecbdata,strbuf_add (&msgbuf, line + len, org_len - len);strbuf_complete_line (&msgbuf); - -emit_line (ecbdata->opt,"" ,"" , msgbuf.buf, msgbuf.len); +emit_diff_symbol (ecbdata->opt, +DIFF_SYMBOL_CONTEXT_FRAGINFO , msgbuf.buf, msgbuf.len);strbuf_release (&msgbuf); }
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH 1/25 diff.c: readability fix
In reply to Stefan BellerWe already have dereferenced 'p->two' into a local variable 'two'. Use that.
--- diff.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
diff.c +2 -2
@@ -3282,8 +3282,8 @@ static void run_diff(struct diff_filepair *p, struct diff_options *o)const char *other;const char *attr_path; - name = p->one->path; - other = (strcmp (name, p->two->path) ? p->two->path :NULL ); + name = one->path; + other = (strcmp (name, two->path) ? two->path :NULL ); attr_path = name;if (o->prefix_length)strip_prefix (o->prefix_length, &name, &other);
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH 5/25 diff.c: emit_diff_symbol learns DIFF_SYMBOL_CONTEXT_MARKER
In reply to Stefan Beller--- diff.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
diff.c +9 -1
@@ -561,13 +561,20 @@ static void emit_line(struct diff_options *o, const char *set, const char *reset }enum diff_symbol { +DIFF_SYMBOL_CONTEXT_MARKER ,DIFF_SYMBOL_SEPARATOR };static void emit_diff_symbol (struct diff_options *o,enum diff_symbol s,const char *line,int len) { +const char *context, *reset;switch (s) { +case DIFF_SYMBOL_CONTEXT_MARKER : + context =diff_get_color_opt (o,DIFF_CONTEXT ); + reset =diff_get_color_opt (o,DIFF_RESET ); +emit_line (o, context, reset, line, len); +break ;case DIFF_SYMBOL_SEPARATOR : fprintf(o->file, "%s%c", diff_line_prefix(o),
@@ -662,7 +669,8 @@ static void emit_hunk_header(struct emit_callback *ecbdata,if (len <10 ||memcmp (line, atat,2 ) || !(ep =memmem (line +2 , len -2 , atat,2 ))) { -emit_line (ecbdata->opt, context, reset, line, len); +emit_diff_symbol (ecbdata->opt, +DIFF_SYMBOL_CONTEXT_MARKER , line, len);return ; } ep +=2 ;/* skip over @@ */
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH 3/25 diff.c: factor out diff_flush_patch_all_file_pairs
In reply to Stefan BellerIn a later patch we want to do more things before and after all filepairs are flushed. So factor flushing out all file pairs into its own function that the new code can be plugged in easily.
--- diff.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)
diff.c +12 -5
@@ -4734,6 +4734,17 @@ void diff_warn_rename_limit(const char *varname, int needed, int degraded_cc)warning (_ (rename_limit_advice), varname, needed); } +static void diff_flush_patch_all_file_pairs (struct diff_options *o) +{ +int i; +struct diff_queue_struct *q = &diff_queued_diff; +for (i =0 ; i < q->nr; i++) { +struct diff_filepair *p = q->queue[i]; +if (check_pair_status (p)) +diff_flush_patch (p, o); + } +} +void diff_flush (struct diff_options *options) {struct diff_queue_struct *q = &diff_queued_diff;
@@ -4826,11 +4837,7 @@ void diff_flush(struct diff_options *options) } } -for (i =0 ; i < q->nr; i++) { -struct diff_filepair *p = q->queue[i]; -if (check_pair_status (p)) -diff_flush_patch (p, options); - } +diff_flush_patch_all_file_pairs (options); }if (output_format &DIFF_FORMAT_CALLBACK )
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH 7/25 diff.c: emit_diff_symbol learns DIFF_SYMBOL_NO_LF_EOF
In reply to Stefan Beller--- diff.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-)
diff.c +11 -8
@@ -561,6 +561,7 @@ static void emit_line(struct diff_options *o, const char *set, const char *reset }enum diff_symbol { +DIFF_SYMBOL_NO_LF_EOF ,DIFF_SYMBOL_CONTEXT_FRAGINFO ,DIFF_SYMBOL_CONTEXT_MARKER ,DIFF_SYMBOL_SEPARATOR
@@ -569,8 +570,16 @@ enum diff_symbol {static void emit_diff_symbol (struct diff_options *o,enum diff_symbol s,const char *line,int len) { +static const char *nneof =" No newline at end of file\n" ;const char *context, *reset;switch (s) { +case DIFF_SYMBOL_NO_LF_EOF : + context =diff_get_color_opt (o,DIFF_CONTEXT ); + reset =diff_get_color_opt (o,DIFF_RESET ); +putc ('\n' , o->file); +emit_line_0 (o, context, reset,'\\' , + nneof,strlen (nneof)); +break ;case DIFF_SYMBOL_CONTEXT_FRAGINFO :emit_line (o,"" ,"" , line, len);break ;
@@ -751,7 +760,6 @@ static void emit_rewrite_lines(struct emit_callback *ecb, int prefix, const char *data, int size) { const char *endp = NULL; -static const char *nneof =" No newline at end of file\n" ;const char *reset =diff_get_color (ecb->color_diff,DIFF_RESET );while (0 < size) {
@@ -769,13 +777,8 @@ static void emit_rewrite_lines(struct emit_callback *ecb, size -= len; data += len; } -if (!endp) { -const char *context =diff_get_color (ecb->color_diff, -DIFF_CONTEXT ); -putc ('\n' , ecb->opt->file); -emit_line_0 (ecb->opt, context, reset,'\\' , - nneof,strlen (nneof)); - } +if (!endp) +emit_diff_symbol (ecb->opt,DIFF_SYMBOL_NO_LF_EOF ,NULL ,0 ); } static void emit_rewrite_diff(const char *name_a,
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH 18/25 diff.c: convert word diffing to use emit_diff_symbol
In reply to Stefan BellerThe word diffing is not line oriented and would need some serious effort to be transformed into a line oriented approach, so just go with a symbol DIFF_SYMBOL_WORD_DIFF that is a partial line.
--- diff.c | 79 ++++++++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 46 insertions(+), 33 deletions(-)
diff.c +46 -33
@@ -570,6 +570,7 @@ enum diff_symbol {DIFF_SYMBOL_STATS_SUMMARY_ABBREV ,DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES ,DIFF_SYMBOL_STATS_LINE , +DIFF_SYMBOL_WORD_DIFF ,DIFF_SYMBOL_SUBMODULE_ADD ,DIFF_SYMBOL_SUBMODULE_DEL , DIFF_SYMBOL_SUBMODULE_UNTRACKED,
@@ -762,6 +763,9 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,case DIFF_SYMBOL_STATS_SUMMARY_ABBREV :emit_line (o,"" ,"" ," ...\n" ,strlen (" ...\n" ));break ; +case DIFF_SYMBOL_WORD_DIFF : +fprintf (o->file,"%.*s" , len, line); +break ;default :die ("BUG: unknown diff symbol" ); }
@@ -1092,37 +1096,49 @@ struct diff_words_data {struct diff_words_style *style; }; -static int fn_out_diff_words_write_helper (FILE *fp, +static int fn_out_diff_words_write_helper (struct diff_options *o,struct diff_words_style_elem *st_el,const char *newline, -size_t count,const char *buf, -const char *line_prefix) +size_t count,const char *buf) {int print =0 ; +struct strbuf sb =STRBUF_INIT ;while (count) {char *p =memchr (buf,'\n' , count);if (print) -fputs (line_prefix, fp); +strbuf_addstr (&sb,diff_line_prefix (o)); +if (p != buf) { -if (st_el->color &&fputs (st_el->color, fp) <0 ) -return -1 ; -if (fputs (st_el->prefix, fp) <0 || -fwrite (buf, p ? p - buf : count,1 , fp) !=1 || -fputs (st_el->suffix, fp) <0 ) -return -1 ; -if (st_el->color && *st_el->color - &&fputs (GIT_COLOR_RESET , fp) <0 ) -return -1 ; +const char *reset = st_el->color && *st_el->color ? +GIT_COLOR_RESET :NULL ; +if (st_el->color && *st_el->color) +strbuf_addstr (&sb, st_el->color); +strbuf_addstr (&sb, st_el->prefix); +strbuf_add (&sb, buf, p ? p - buf : count); +strbuf_addstr (&sb, st_el->suffix); +if (reset) +strbuf_addstr (&sb, reset); }if (!p) -return 0 ; -if (fputs (newline, fp) <0 ) -return -1 ; + goto out; + +strbuf_addstr (&sb, newline); count -= p +1 - buf; buf = p +1 ; print =1 ; +if (count) { +emit_diff_symbol (o,DIFF_SYMBOL_WORD_DIFF , + sb.buf, sb.len,0 ); +strbuf_reset (&sb); + } } + +out: +if (sb.len) +emit_diff_symbol (o,DIFF_SYMBOL_WORD_DIFF , + sb.buf, sb.len,0 ); +strbuf_release (&sb);return 0 ; }
@@ -1204,24 +1220,20 @@ static void fn_out_diff_words_aux(void *priv, char *line, unsigned long len)fputs (line_prefix, diff_words->opt->file); }if (diff_words->current_plus != plus_begin) { -fn_out_diff_words_write_helper (diff_words->opt->file, +fn_out_diff_words_write_helper (diff_words->opt, &style->ctx, style->newline, plus_begin - diff_words->current_plus, - diff_words->current_plus, line_prefix); -if (*(plus_begin -1 ) =='\n' ) -fputs (line_prefix, diff_words->opt->file); + diff_words->current_plus); }if (minus_begin != minus_end) { -fn_out_diff_words_write_helper (diff_words->opt->file, +fn_out_diff_words_write_helper (diff_words->opt, &style->old, style->newline, - minus_end - minus_begin, minus_begin, - line_prefix); + minus_end - minus_begin, minus_begin); }if (plus_begin != plus_end) { -fn_out_diff_words_write_helper (diff_words->opt->file, +fn_out_diff_words_write_helper (diff_words->opt, &style->new, style->newline, - plus_end - plus_begin, plus_begin, - line_prefix); + plus_end - plus_begin, plus_begin); } diff_words->current_plus = plus_end;
@@ -1315,11 +1327,12 @@ static void diff_words_show(struct diff_words_data *diff_words)/* special case: only removal */ if (!diff_words->plus.text.size) { -fputs (line_prefix, diff_words->opt->file); -fn_out_diff_words_write_helper (diff_words->opt->file, +emit_diff_symbol (diff_words->opt,DIFF_SYMBOL_WORD_DIFF , + line_prefix,strlen (line_prefix),0 ); +fn_out_diff_words_write_helper (diff_words->opt, &style->old, style->newline, diff_words->minus.text.size, - diff_words->minus.text.ptr, line_prefix); + diff_words->minus.text.ptr); diff_words->minus.text.size =0 ;return ; }
@@ -1342,12 +1355,12 @@ static void diff_words_show(struct diff_words_data *diff_words)if (diff_words->current_plus != diff_words->plus.text.ptr + diff_words->plus.text.size) {if (color_words_output_graph_prefix (diff_words)) -fputs (line_prefix, diff_words->opt->file); -fn_out_diff_words_write_helper (diff_words->opt->file, +emit_diff_symbol (diff_words->opt,DIFF_SYMBOL_WORD_DIFF , + line_prefix,strlen (line_prefix),0 ); +fn_out_diff_words_write_helper (diff_words->opt, &style->ctx, style->newline, diff_words->plus.text.ptr + diff_words->plus.text.size - - diff_words->current_plus, diff_words->current_plus, - line_prefix); + - diff_words->current_plus, diff_words->current_plus); } diff_words->minus.text.size = diff_words->plus.text.size =0 ; }
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH 13/25 diff.c: emit_diff_symbol learns about DIFF_SYMBOL_BINARY_FILES
In reply to Stefan Bellerwe could save a little bit of memory when buffering in a later mode by just passing the inner part ("%s and %s", file1, file 2), but those a just a few bytes, so instead let's reuse the implementation from DIFF_SYMBOL_HEADER and keep the whole line around.
--- diff.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-)
diff.c +15 -5
@@ -561,6 +561,7 @@ static void emit_line(struct diff_options *o, const char *set, const char *reset }enum diff_symbol { +DIFF_SYMBOL_BINARY_FILES ,DIFF_SYMBOL_HEADER ,DIFF_SYMBOL_FILEPAIR_PLUS ,DIFF_SYMBOL_FILEPAIR_MINUS ,
@@ -690,6 +691,7 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s, line, reset, strchr(line, ' ') ? "\t" : "");break ; +case DIFF_SYMBOL_BINARY_FILES :case DIFF_SYMBOL_HEADER :fprintf (o->file,"%s" , line);break ;
@@ -2542,6 +2544,7 @@ static void builtin_diff(const char *name_a, }else if (!DIFF_OPT_TST (o,TEXT ) && ( (!textconv_one &&diff_filespec_is_binary (one)) || (!textconv_two &&diff_filespec_is_binary (two)) )) { +struct strbuf sb =STRBUF_INIT ;if (!one->data && !two->data &&S_ISREG (one->mode) &&S_ISREG (two->mode) && !DIFF_OPT_TST (o,BINARY )) {
@@ -2554,8 +2557,11 @@ static void builtin_diff(const char *name_a, }emit_diff_symbol (o,DIFF_SYMBOL_HEADER , header.buf, header.len,0 ); -fprintf (o->file,"%sBinary files %s and %s differ\n" , - line_prefix, lbl[0 ], lbl[1 ]); +strbuf_addf (&sb,"%sBinary files %s and %s differ\n" , +diff_line_prefix (o), lbl[0 ], lbl[1 ]); +emit_diff_symbol (o,DIFF_SYMBOL_BINARY_FILES , + sb.buf, sb.len,0 ); +strbuf_release (&sb); goto free_ab_and_return; }if (fill_mmfile (&mf1, one) <0 ||fill_mmfile (&mf2, two) <0 )
@@ -2572,9 +2578,13 @@ static void builtin_diff(const char *name_a,strbuf_reset (&header);if (DIFF_OPT_TST (o,BINARY ))emit_binary_diff (o->file, &mf1, &mf2, line_prefix); -else -fprintf (o->file,"%sBinary files %s and %s differ\n" , - line_prefix, lbl[0 ], lbl[1 ]); +else { +strbuf_addf (&sb,"%sBinary files %s and %s differ\n" , +diff_line_prefix (o), lbl[0 ], lbl[1 ]); +emit_diff_symbol (o,DIFF_SYMBOL_BINARY_FILES , + sb.buf, sb.len,0 ); +strbuf_release (&sb); + } o->found_changes =1 ; }else {/* Crazy xdl interfaces.. */
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH 21/25 diff.c: buffer all output if asked to
In reply to Stefan BellerIntroduce a new option 'emitted_symbols' in the struct diff_options which controls whether all output is buffered up until all output is available. It is set internally in diff.c when necessary.
We'll have a new struct 'emitted_string' in diff.c which will be used to buffer each line. The emitted_string will duplicate the memory of the line to buffer as that is easiest to reason about for now. In a future patch we may want to decrease the memory usage by not duplicating all output for buffering but rather we may want to store offsets into the file or in case of hunk descriptions such as the similarity score, we could just store the relevant number and reproduce the text later on.
This approach was chosen as a first step because it is quite simple compared to the alternative with less memory footprint.
emit_diff_symbol factors out the emission part and depending on the diff_options->emitted_symbols the emission will be performed directly when calling emit_diff_symbol or after the whole process is done, i.e. by buffering we have add the possibility for a second pass over the whole output before doing the actual output.
In 6440d34 (2012-03-14, diff: tweak a _copy_ of diff_options with word-diff) we introduced a duplicate diff options struct for word emissions as we may have different regex settings in there. When buffering the output, we need to operate on just one buffer, so we have to copy back the emissions of the word buffer into the main buffer.
Unconditionally enable output via buffer in this patch as it yields a great opportunity for testing, i.e. all the diff tests from the test suite pass without having reordering issues (i.e. only parts of the output got buffered, and we forgot to buffer other parts). The test suite passes, which gives confidence that we converted all functions to use emit_string for output.
--- diff.c | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- diff.h | 2 ++ 2 files changed, 109 insertions(+), 2 deletions(-)
diff.c +107 -2
@@ -605,6 +605,47 @@ enum diff_symbol {#define DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF (1<<16)#define DIFF_SYMBOL_CONTENT_WS_MASK (WSEH_NEW | WSEH_OLD | WSEH_CONTEXT | WS_RULE_MASK) +/* +* This struct is used when we need to buffer the output of the diff output. +* +* NEEDSWORK: Instead of storing a copy of the line, add an offset pointer +* into the pre/post image file. This pointer could be a union with the +* line pointer. By storing an offset into the file instead of the literal line, +* we can decrease the memory footprint for the buffered output. At first we +* may want to only have indirection for the content lines, but we could also +* enhance the state for emitting prefabricated lines, e.g. the similarity +* score line or hunk/file headers would only need to store a number or path +* and then the output can be constructed later on depending on state. +*/ +struct emitted_diff_symbol { +const char *line; +int len; +int flags; +enum diff_symbol s; +}; +#define EMITTED_DIFF_SYMBOL_INIT {NULL} + +struct emitted_diff_symbols { +struct emitted_diff_symbol *buf; +int nr, alloc; +}; +#define EMITTED_DIFF_SYMBOLS_INIT {NULL, 0, 0} + +static void append_emitted_diff_symbol (struct diff_options *o, +struct emitted_diff_symbol *e) +{ +struct emitted_diff_symbol *f; + +ALLOC_GROW (o->emitted_symbols->buf, + o->emitted_symbols->nr +1 , + o->emitted_symbols->alloc); + f = &o->emitted_symbols->buf[o->emitted_symbols->nr++]; + +memcpy (f, e,sizeof (struct emitted_diff_symbol )); + f->line = e->line ?xmemdupz (e->line, e->len) :NULL ; +} + + static void emit_line_ws_markup(struct diff_options *o, const char *set, const char *reset, const char *line, int len, char sign,
@@ -631,12 +672,18 @@ static void emit_line_ws_markup(struct diff_options *o, } } -static void emit_diff_symbol (struct diff_options *o,enum diff_symbol s, -const char *line,int len,unsigned flags) +static void emit_diff_symbol_from_struct (struct diff_options *o, +struct emitted_diff_symbol *eds) {static const char *nneof =" No newline at end of file\n" ;const char *context, *reset, *set, *meta, *fraginfo;struct strbuf sb =STRBUF_INIT ; + +enum diff_symbol s = eds->s; +const char *line = eds->line; +int len = eds->len; +unsigned flags = eds->flags; +switch (s) {case DIFF_SYMBOL_NO_LF_EOF : context =diff_get_color_opt (o,DIFF_CONTEXT );
@@ -778,6 +825,17 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,strbuf_release (&sb); } +static void emit_diff_symbol (struct diff_options *o,enum diff_symbol s, +const char *line,int len,unsigned flags) +{ +struct emitted_diff_symbol e = {line, len, flags, s}; + +if (o->emitted_symbols) +append_emitted_diff_symbol (o, &e); +else +emit_diff_symbol_from_struct (o, &e); +} +void diff_emit_submodule_del (struct diff_options *o,const char *line) {emit_diff_symbol (o,DIFF_SYMBOL_SUBMODULE_DEL , line,strlen (line),0 );
@@ -1374,9 +1432,29 @@ static void diff_words_show(struct diff_words_data *diff_words)/* In "color-words" mode, show word-diff of words accumulated in the buffer */ static void diff_words_flush (struct emit_callback *ecbdata) { +struct diff_options *wo = ecbdata->diff_words->opt; +if (ecbdata->diff_words->minus.text.size || ecbdata->diff_words->plus.text.size)diff_words_show (ecbdata->diff_words); + +if (wo->emitted_symbols) { +struct diff_options *o = ecbdata->opt; +struct emitted_diff_symbols *wol = wo->emitted_symbols; +int i; + +/* +* NEEDSWORK: +* Instead of appending each, concat all words to a line? +*/ +for (i =0 ; i < wol->nr; i++) +append_emitted_diff_symbol (o, &wol->buf[i]); + +for (i =0 ; i < wol->nr; i++) +free ((void *)wol->buf[i].line); + + wol->nr =0 ; + } }static void diff_filespec_load_driver (struct diff_filespec *one)
@@ -1412,6 +1490,11 @@ static void init_diff_words_data(struct emit_callback *ecbdata,xcalloc (1 ,sizeof (struct diff_words_data )); ecbdata->diff_words->type = o->word_diff; ecbdata->diff_words->opt = o; + +if (orig_opts->emitted_symbols) + o->emitted_symbols = +xcalloc (1 ,sizeof (struct emitted_diff_symbols )); +if (!o->word_regex) o->word_regex =userdiff_word_regex (one);if (!o->word_regex)
@@ -1446,6 +1529,7 @@ static void free_diff_words_data(struct emit_callback *ecbdata) {if (ecbdata->diff_words) {diff_words_flush (ecbdata); +free (ecbdata->diff_words->opt->emitted_symbols);free (ecbdata->diff_words->opt);free (ecbdata->diff_words->minus.text.ptr);free (ecbdata->diff_words->minus.orig);
@@ -4996,16 +5080,37 @@ void diff_warn_rename_limit(const char *varname, int needed, int degraded_cc)static void diff_flush_patch_all_file_pairs (struct diff_options *o) {int i; +static struct emitted_diff_symbols esm =EMITTED_DIFF_SYMBOLS_INIT ;struct diff_queue_struct *q = &diff_queued_diff;if (WSEH_NEW &WS_RULE_MASK )die ("BUG: WS rules bit mask overlaps with diff symbol flags" ); +/* +* For testing purposes we want to make sure the diff machinery +* works completely with the buffer. If there is anything emitted +* outside the emit_string, then the order is screwed +* up and the tests will fail. +* +* TODO (later in this series): +* We'll unset this pointer in a later patch. +*/ + o->emitted_symbols = &esm; +for (i =0 ; i < q->nr; i++) {struct diff_filepair *p = q->queue[i];if (check_pair_status (p))diff_flush_patch (p, o); } + +if (o->emitted_symbols) { +for (i =0 ; i < esm.nr; i++) +emit_diff_symbol_from_struct (o, &esm.buf[i]); + +for (i =0 ; i < esm.nr; i++) +free ((void *)esm.buf[i].line); + } + esm.nr =0 ; }void diff_flush (struct diff_options *options)
diff.h +2 -0
@@ -186,6 +186,8 @@ struct diff_options {void *output_prefix_data;int diff_path_counter; + +struct emitted_diff_symbols *emitted_symbols; };void diff_emit_submodule_del (struct diff_options *o,const char *line);
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH 20/25 diff.c: emit_diff_symbol learns about DIFF_SYMBOL_SUMMARY
In reply to Stefan Beller--- diff.c | 71 ++++++++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 41 insertions(+), 30 deletions(-)
diff.c +41 -30
@@ -572,6 +572,7 @@ enum diff_symbol {DIFF_SYMBOL_STATS_LINE ,DIFF_SYMBOL_WORD_DIFF ,DIFF_SYMBOL_STAT_SEP , +DIFF_SYMBOL_SUMMARY ,DIFF_SYMBOL_SUBMODULE_ADD ,DIFF_SYMBOL_SUBMODULE_DEL , DIFF_SYMBOL_SUBMODULE_UNTRACKED,
@@ -648,6 +649,7 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,case DIFF_SYMBOL_SUBMODULE_ERROR :case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH :case DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES : +case DIFF_SYMBOL_SUMMARY :case DIFF_SYMBOL_STATS_LINE :case DIFF_SYMBOL_BINARY_DIFF_BODY :case DIFF_SYMBOL_CONTEXT_FRAGINFO :
@@ -4717,67 +4719,76 @@ static void flush_one_pair(struct diff_filepair *p, struct diff_options *opt) } } -static void show_file_mode_name (FILE *file,const char *newdelete,struct diff_filespec *fs) +static void show_file_mode_name (struct diff_options *opt,const char *newdelete,struct diff_filespec *fs) { +struct strbuf sb =STRBUF_INIT ;if (fs->mode) -fprintf (file," %s mode %06o " , newdelete, fs->mode); +strbuf_addf (&sb," %s mode %06o " , newdelete, fs->mode);else -fprintf (file," %s " , newdelete); -write_name_quoted (fs->path, file,'\n' ); -} +strbuf_addf (&sb," %s " , newdelete); +quote_c_style (fs->path, &sb,NULL ,0 ); +strbuf_addch (&sb,'\n' ); +emit_diff_symbol (opt,DIFF_SYMBOL_SUMMARY , + sb.buf, sb.len,0 ); +strbuf_release (&sb); +} -static void show_mode_change (FILE *file,struct diff_filepair *p,int show_name, -const char *line_prefix) +static void show_mode_change (struct diff_options *opt,struct diff_filepair *p, +int show_name) {if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) { -fprintf (file,"%s mode change %06o => %06o%c" , line_prefix, p->one->mode, - p->two->mode, show_name ?' ' :'\n' ); +struct strbuf sb =STRBUF_INIT ; +strbuf_addf (&sb," mode change %06o => %06o" , + p->one->mode, p->two->mode);if (show_name) { -write_name_quoted (p->two->path, file,'\n' ); +strbuf_addch (&sb,' ' ); +quote_c_style (p->two->path, &sb,NULL ,0 ); } +emit_diff_symbol (opt,DIFF_SYMBOL_SUMMARY , + sb.buf, sb.len,0 ); +strbuf_release (&sb); } } -static void show_rename_copy (FILE *file,const char *renamecopy,struct diff_filepair *p, -const char *line_prefix) +static void show_rename_copy (struct diff_options *opt,const char *renamecopy, +struct diff_filepair *p) { +struct strbuf sb =STRBUF_INIT ;char *names =pprint_rename (p->one->path, p->two->path); - -fprintf (file," %s %s (%d%%)\n" , renamecopy, names,similarity_index (p)); +strbuf_addf (&sb," %s %s (%d%%)\n" , + renamecopy, names,similarity_index (p));free (names); -show_mode_change (file, p,0 , line_prefix); +emit_diff_symbol (opt,DIFF_SYMBOL_SUMMARY , + sb.buf, sb.len,0 ); +show_mode_change (opt, p,0 ); }static void diff_summary (struct diff_options *opt,struct diff_filepair *p) { -FILE *file = opt->file; -const char *line_prefix =diff_line_prefix (opt); -switch (p->status) {case DIFF_STATUS_DELETED : -fputs (line_prefix, file); -show_file_mode_name (file,"delete" , p->one); +show_file_mode_name (opt,"delete" , p->one);break ;case DIFF_STATUS_ADDED : -fputs (line_prefix, file); -show_file_mode_name (file,"create" , p->two); +show_file_mode_name (opt,"create" , p->two);break ;case DIFF_STATUS_COPIED : -fputs (line_prefix, file); -show_rename_copy (file,"copy" , p, line_prefix); +show_rename_copy (opt,"copy" , p);break ;case DIFF_STATUS_RENAMED : -fputs (line_prefix, file); -show_rename_copy (file,"rename" , p, line_prefix); +show_rename_copy (opt,"rename" , p);break ;default :if (p->score) { -fprintf (file,"%s rewrite " , line_prefix); -write_name_quoted (p->two->path, file,' ' ); -fprintf (file,"(%d%%)\n" ,similarity_index (p)); +struct strbuf sb =STRBUF_INIT ; +strbuf_addstr (&sb," rewrite " ); +quote_c_style (p->two->path, &sb,NULL ,0 ); +strbuf_addf (&sb," (%d%%)\n" ,similarity_index (p)); +emit_diff_symbol (opt,DIFF_SYMBOL_SUMMARY , + sb.buf, sb.len,0 ); } -show_mode_change (file, p, !p->score, line_prefix); +show_mode_change (opt, p, !p->score);break ; } }
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH 22/25 diff.c: color moved lines differently
In reply to Stefan BellerWhen a patch consists mostly of moving blocks of code around, it can be quite tedious to ensure that the blocks are moved verbatim, and not undesirably modified in the move. To that end, color blocks that are moved within the same patch differently. For example (OM, del, add, and NM are different colors):
[OM] -void sensitive_stuff(void)
[OM] -{
[OM] - if (!is_authorized_user())
[OM] - die("unauthorized");
[OM] - sensitive_stuff(spanning,
[OM] - multiple,
[OM] - lines);
[OM] -} void another_function()
{
[del] - printf("foo");
[add] + printf("bar");
}
[NM] +void sensitive_stuff(void)
[NM] +{
[NM] + if (!is_authorized_user())
[NM] + die("unauthorized");
[NM] + sensitive_stuff(spanning,
[NM] + multiple,
[NM] + lines);
[NM] +}However adjacent blocks may be problematic. For example, in this potentially malicious patch, the swapping of blocks can be spotted:
[OM] -void sensitive_stuff(void)
[OM] -{
[OMA] - if (!is_authorized_user())
[OMA] - die("unauthorized");
[OM] - sensitive_stuff(spanning,
[OM] - multiple,
[OM] - lines);
[OMA] -}
void another_function()
{
[del] - printf("foo");
[add] + printf("bar");
}
[NM] +void sensitive_stuff(void)
[NM] +{
[NMA] + sensitive_stuff(spanning,
[NMA] + multiple,
[NMA] + lines);
[NM] + if (!is_authorized_user())
[NM] + die("unauthorized");
[NMA] +}If the moved code is larger, it is easier to hide some permutation in the code, which is why some alternative coloring is needed.
This patch implements the first mode:
* basic alternating 'Zebra' mode
This conveys all information needed to the user. Defer customization to later patches.
First I implemented an alternative design, which would try to fingerprint a line by its neighbors to detect if we are in a block or at the boundary. This idea iss error prone as it inspected each line and its neighboring lines to determine if the line was (a) moved and (b) if was deep inside a hunk by having matching neighboring lines. This is unreliable as the we can construct hunks which have equal neighbors that just exceed the number of lines inspected. (Think of 'AXYZBXYZCXYZD..' with each letter as a line, that is permutated to AXYZCXYZBXYZD..').
Instead this provides a dynamic programming greedy algorithm that finds the largest moved hunk and then has several modes on highlighting bounds.
A note on the options '--submodule=diff' and '--color-words/--word-diff':
In the conversion to use emit_line in the prior patches both submodules as well as word diff output carefully chose to call emit_line with sign=0. All output with sign=0 is ignored for move detection purposes in this patch, such that no weird looking output will be generated for these cases. This leads to another thought: We could pass on '--color-moved' to submodules such that they color up moved lines for themselves. If we'd do so only line moves within a repository boundary are marked up.
It is useful to have moved lines colored, but there are annoying corner cases, such as a single line moved, that is very common. For example in a typical patch of C code, we have closing braces that end statement blocks or functions.
While it is technically true that these lines are moved as they show up elsewhere, it is harmful for the review as the reviewers attention is drawn to such a minor side annoyance.
For now let's have a simple solution of hardcoding the number of moved lines to be at least 3 before coloring them. Note, that the length is applied across all blocks to find the 'lonely' blocks that pollute new code, but do not interfere with a permutated block where each permutation has less lines than 3.
--- diff.c | 341 +++++++++++++++++++++++++++++++++++++++++++-- diff.h | 12 +- t/t4015-diff-whitespace.sh | 261 ++++++++++++++++++++++++++++++++++ 3 files changed, 599 insertions(+), 15 deletions(-)
diff.c +327 -14
@@ -16,6 +16,7 @@#include "userdiff.h" #include "submodule-config.h" #include "submodule.h" +#include "hashmap.h" #include "ll-merge.h" #include "string-list.h" #include "argv-array.h"
@@ -32,6 +33,7 @@ static int diff_indent_heuristic = 1;static int diff_rename_limit_default =400 ;static int diff_suppress_blank_empty;static int diff_use_color_default =-1 ; +static int diff_color_moved_default;static int diff_context_default =3 ;static int diff_interhunk_context_default;static const char *diff_word_regex_cfg;
@@ -56,6 +58,10 @@ static char diff_colors[][COLOR_MAXLEN] = {GIT_COLOR_YELLOW ,/* COMMIT */ GIT_COLOR_BG_RED ,/* WHITESPACE */ GIT_COLOR_NORMAL ,/* FUNCINFO */ +GIT_COLOR_MAGENTA ,/* OLD_MOVED */ +GIT_COLOR_BLUE ,/* OLD_MOVED ALTERNATIVE */ +GIT_COLOR_CYAN ,/* NEW_MOVED */ + GIT_COLOR_YELLOW, /* NEW_MOVED ALTERNATIVE */ }; static NORETURN void die_want_option(const char *option_name)
@@ -81,6 +87,14 @@ static int parse_diff_color_slot(const char *var)return DIFF_WHITESPACE ;if (!strcasecmp (var,"func" ))return DIFF_FUNCINFO ; +if (!strcasecmp (var,"oldmoved" )) +return DIFF_FILE_OLD_MOVED ; +if (!strcasecmp (var,"oldmovedalternative" )) +return DIFF_FILE_OLD_MOVED_ALT ; +if (!strcasecmp (var,"newmoved" )) +return DIFF_FILE_NEW_MOVED ; +if (!strcasecmp (var,"newmovedalternative" )) +return DIFF_FILE_NEW_MOVED_ALT ;return -1 ; }
@@ -229,12 +243,40 @@ int git_diff_heuristic_config(const char *var, const char *value, void *cb)return 0 ; } +static int parse_color_moved (const char *arg) +{ +switch (git_parse_maybe_bool (arg)) { +case 0 : +return COLOR_MOVED_NO ; +case 1 : +return COLOR_MOVED_DEFAULT ; +default : +break ; + } + +if (!strcmp (arg,"no" )) +return COLOR_MOVED_NO ; +else if (!strcmp (arg,"zebra" )) +return COLOR_MOVED_ZEBRA ; +else if (!strcmp (arg,"default" )) +return COLOR_MOVED_DEFAULT ; +else +return error (_ ("color moved setting must be one of 'no', 'default', 'zebra'" )); +} +int git_diff_ui_config (const char *var,const char *value,void *cb) {if (!strcmp (var,"diff.color" ) || !strcmp (var,"color.diff" )) { diff_use_color_default =git_config_colorbool (var, value);return 0 ; } +if (!strcmp (var,"diff.colormoved" )) { +int cm =parse_color_moved (value); +if (cm <0 ) +return -1 ; + diff_color_moved_default = cm; +return 0 ; + }if (!strcmp (var,"diff.context" )) { diff_context_default =git_config_int (var, value);if (diff_context_default <0 )
@@ -602,7 +644,9 @@ enum diff_symbol {* 13-15 are WSEH_NEW | WSEH_OLD | WSEH_CONTEXT * 16 is marking if the line is blank at EOF */ -#define DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF (1<<16) +#define DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF (1<<16) +#define DIFF_SYMBOL_MOVED_LINE (1<<17) +#define DIFF_SYMBOL_MOVED_LINE_ALT (1<<18)#define DIFF_SYMBOL_CONTENT_WS_MASK (WSEH_NEW | WSEH_OLD | WSEH_CONTEXT | WS_RULE_MASK)/*
@@ -645,6 +689,240 @@ static void append_emitted_diff_symbol(struct diff_options *o, f->line = e->line ?xmemdupz (e->line, e->len) :NULL ; } +struct moved_entry { +struct hashmap_entry ent; +const struct emitted_diff_symbol *es; +struct moved_entry *next_line; +}; + +static int next_byte (const char **cp,const char **endp, +const struct diff_options *diffopt) +{ +int retval; + +if (DIFF_XDL_TST (diffopt,IGNORE_WHITESPACE_AT_EOL )) { +while (*endp > *cp &&isspace (**endp)) + (*endp)--; + } + +if (*cp > *endp) +return -1 ; + +if (DIFF_XDL_TST (diffopt,IGNORE_WHITESPACE_CHANGE )) { +while (*cp < *endp &&isspace (**cp)) + (*cp)++; +/* +* After skipping a couple of whitespaces, we still have to +* account for one space. +*/ +return (int )' ' ; + } + +if (DIFF_XDL_TST (diffopt,IGNORE_WHITESPACE )) { +while (*cp < *endp &&isspace (**cp)) + (*cp)++; +/* return the first non-ws character via the usual below */ + } + + retval = **cp; + (*cp)++;/* advance */ +return retval; +} + +static int moved_entry_cmp (const struct moved_entry *a, +const struct moved_entry *b, +const void *keydata, +const void *data) +{ +const struct diff_options *diffopt = data; +const char *ap = a->es->line, *ae = a->es->line + a->es->len; +const char *bp = b->es->line, *be = b->es->line + b->es->len; + +if (!(diffopt->xdl_opts &XDF_WHITESPACE_FLAGS )) +return a->es->len != b->es->len ||memcmp (ap, bp, a->es->len); + +while (1 ) { +int ca, cb; + ca =next_byte (&ap, &ae, diffopt); + cb =next_byte (&bp, &be, diffopt); +if (ca != cb) +return 1 ;/* differs */ +if (!ca) +return 0 ; + }; +} + +static unsigned get_string_hash (struct emitted_diff_symbol *es,struct diff_options *o) +{ +if (o->xdl_opts &XDF_WHITESPACE_FLAGS ) { +static struct strbuf sb =STRBUF_INIT ; +const char *ap = es->line, *ae = es->line + es->len; +int c; + +strbuf_reset (&sb); +while ((c =next_byte (&ap, &ae, o)) >0 ) +strbuf_addch (&sb, c); + +return memhash (sb.buf, sb.len); + }else { +return memhash (es->line, es->len); + } +} + +static struct moved_entry *prepare_entry (struct diff_options *o, +int line_no) +{ +struct moved_entry *ret =xmalloc (sizeof (*ret)); +struct emitted_diff_symbol *l = &o->emitted_symbols->buf[line_no]; + + ret->ent.hash =get_string_hash (l, o); + ret->es = l; + ret->next_line =NULL ; + +return ret; +} + +static void add_lines_to_move_detection (struct diff_options *o, +struct hashmap *add_lines, +struct hashmap *del_lines) +{ +struct moved_entry *prev_line =NULL ; + +int n; +for (n =0 ; n < o->emitted_symbols->nr; n++) { +struct hashmap *hm; +struct moved_entry *key; + +switch (o->emitted_symbols->buf[n].s) { +case DIFF_SYMBOL_PLUS : + hm = add_lines; +break ; +case DIFF_SYMBOL_MINUS : + hm = del_lines; +break ; +default : + prev_line =NULL ; +continue ; + } + + key =prepare_entry (o, n); +if (prev_line && prev_line->es->s == o->emitted_symbols->buf[n].s) + prev_line->next_line = key; + +hashmap_add (hm, key); + prev_line = key; + } +} + +static int shrink_potential_moved_blocks (struct moved_entry **pmb, +int pmb_nr) +{ +int lp, rp; + +/* Shrink the set of potential block to the remaining running */ +for (lp =0 , rp = pmb_nr -1 ; lp <= rp;) { +while (lp < pmb_nr && pmb[lp]) + lp++; +/* lp points at the first NULL now */ + +while (rp >-1 && !pmb[rp]) + rp--; +/* rp points at the last non-NULL */ + +if (lp < pmb_nr && rp >-1 && lp < rp) { + pmb[lp] = pmb[rp]; + pmb[rp] =NULL ; + rp--; + lp++; + } + } + +/* Remember the number of running sets */ +return rp +1 ; +} + +/* Find blocks of moved code, delegate actual coloring decision to helper */ +static void mark_color_as_moved (struct diff_options *o, +struct hashmap *add_lines, +struct hashmap *del_lines) +{ +struct moved_entry **pmb =NULL ;/* potentially moved blocks */ +int pmb_nr =0 , pmb_alloc =0 ; +int n, flipped_block =1 , block_length =0 ; + + +for (n =0 ; n < o->emitted_symbols->nr; n++) { +struct hashmap *hm =NULL ; +struct moved_entry *key; +struct moved_entry *match =NULL ; +struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n]; +int i; + +switch (l->s) { +case DIFF_SYMBOL_PLUS : + hm = del_lines; + key =prepare_entry (o, n); + match =hashmap_get (hm, key, o); +free (key); +break ; +case DIFF_SYMBOL_MINUS : + hm = add_lines; + key =prepare_entry (o, n); + match =hashmap_get (hm, key, o); +free (key); +break ; +default : + flipped_block =1 ; + } + +if (!match) { +if (block_length <COLOR_MOVED_MIN_BLOCK_LENGTH ) { +for (i =0 ; i < block_length +1 ; i++) { + l = &o->emitted_symbols->buf[n - i]; + l->flags &= ~DIFF_SYMBOL_MOVED_LINE ; + } + } + pmb_nr =0 ; + block_length =0 ; +continue ; + } + + l->flags |=DIFF_SYMBOL_MOVED_LINE ; + block_length++; + +/* Check any potential block runs, advance each or nullify */ +for (i =0 ; i < pmb_nr; i++) { +struct moved_entry *p = pmb[i]; +struct moved_entry *pnext = (p && p->next_line) ? + p->next_line :NULL ; +if (pnext && !hm->cmpfn (pnext, match,NULL , o)) { + pmb[i] = p->next_line; + }else { + pmb[i] =NULL ; + } + } + + pmb_nr =shrink_potential_moved_blocks (pmb, pmb_nr); + +if (pmb_nr ==0 ) { +/* +* The current line is the start of a new block. +* Setup the set of potential blocks. +*/ +for (; match; match =hashmap_get_next (hm, match)) { +ALLOC_GROW (pmb, pmb_nr +1 , pmb_alloc); + pmb[pmb_nr++] = match; + } + + flipped_block = (flipped_block +1 ) %2 ; + } + +if (flipped_block) + l->flags |=DIFF_SYMBOL_MOVED_LINE_ALT ; + } + +free (pmb); +} static void emit_line_ws_markup(struct diff_options *o, const char *set, const char *reset,
@@ -720,14 +998,24 @@ static void emit_diff_symbol_from_struct(struct diff_options *o, flags & (DIFF_SYMBOL_CONTENT_WS_MASK), 0);break ;case DIFF_SYMBOL_PLUS : - set =diff_get_color_opt (o,DIFF_FILE_NEW ); +if (flags &DIFF_SYMBOL_MOVED_LINE_ALT ) + set =diff_get_color_opt (o,DIFF_FILE_NEW_MOVED_ALT ); +else if (flags &DIFF_SYMBOL_MOVED_LINE ) + set =diff_get_color_opt (o,DIFF_FILE_NEW_MOVED ); +else + set =diff_get_color_opt (o,DIFF_FILE_NEW ); reset =diff_get_color_opt (o,DIFF_RESET );emit_line_ws_markup (o, set, reset, line, len,'+' , flags &DIFF_SYMBOL_CONTENT_WS_MASK , flags &DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF );break ;case DIFF_SYMBOL_MINUS : - set =diff_get_color_opt (o,DIFF_FILE_OLD ); +if (flags &DIFF_SYMBOL_MOVED_LINE_ALT ) + set =diff_get_color_opt (o,DIFF_FILE_OLD_MOVED_ALT ); +else if (flags &DIFF_SYMBOL_MOVED_LINE ) + set =diff_get_color_opt (o,DIFF_FILE_OLD_MOVED ); +else + set =diff_get_color_opt (o,DIFF_FILE_OLD ); reset =diff_get_color_opt (o,DIFF_RESET );emit_line_ws_markup (o, set, reset, line, len,'-' , flags &DIFF_SYMBOL_CONTENT_WS_MASK ,0 );
@@ -3740,6 +4028,8 @@ void diff_setup(struct diff_options *options) options->a_prefix ="a/" ; options->b_prefix ="b/" ; } + + options->color_moved = diff_color_moved_default; }void diff_setup_done (struct diff_options *options)
@@ -3849,6 +4139,9 @@ void diff_setup_done(struct diff_options *options)if (DIFF_OPT_TST (options,FOLLOW_RENAMES ) && options->pathspec.nr !=1 )die (_ ("--follow requires exactly one pathspec" )); + +if (!options->use_color ||external_diff ()) + options->color_moved =0 ; }static int opt_arg (const char *arg,int arg_short,const char *arg_long,int *val)
@@ -4273,7 +4566,19 @@ int diff_opt_parse(struct diff_options *options, }else if (!strcmp (arg,"--no-color" )) options->use_color =0 ; -else if (!strcmp (arg,"--color-words" )) { +else if (!strcmp (arg,"--color-moved" )) { +if (diff_color_moved_default) + options->color_moved = diff_color_moved_default; +if (options->color_moved ==COLOR_MOVED_NO ) + options->color_moved =COLOR_MOVED_DEFAULT ; + }else if (!strcmp (arg,"--no-color-moved" )) + options->color_moved =COLOR_MOVED_NO ; +else if (skip_prefix (arg,"--color-moved=" , &arg)) { +int cm =parse_color_moved (arg); +if (cm <0 ) +die ("bad --color-moved argument: %s" , arg); + options->color_moved = cm; + }else if (!strcmp (arg,"--color-words" )) { options->use_color =1 ; options->word_diff =DIFF_WORDS_COLOR ; }
@@ -5086,16 +5391,8 @@ static void diff_flush_patch_all_file_pairs(struct diff_options *o)if (WSEH_NEW &WS_RULE_MASK )die ("BUG: WS rules bit mask overlaps with diff symbol flags" ); -/* -* For testing purposes we want to make sure the diff machinery -* works completely with the buffer. If there is anything emitted -* outside the emit_string, then the order is screwed -* up and the tests will fail. -* -* TODO (later in this series): -* We'll unset this pointer in a later patch. -*/ - o->emitted_symbols = &esm; +if (o->color_moved) + o->emitted_symbols = &esm;for (i =0 ; i < q->nr; i++) {struct diff_filepair *p = q->queue[i];
@@ -5104,6 +5401,21 @@ static void diff_flush_patch_all_file_pairs(struct diff_options *o) }if (o->emitted_symbols) { +if (o->color_moved) { +struct hashmap add_lines, del_lines; + +hashmap_init (&del_lines, + (hashmap_cmp_fn )moved_entry_cmp, o,0 ); +hashmap_init (&add_lines, + (hashmap_cmp_fn )moved_entry_cmp, o,0 ); + +add_lines_to_move_detection (o, &add_lines, &del_lines); +mark_color_as_moved (o, &add_lines, &del_lines); + +hashmap_free (&add_lines,0 ); +hashmap_free (&del_lines,0 ); + } +for (i =0 ; i < esm.nr; i++)emit_diff_symbol_from_struct (o, &esm.buf[i]);
@@ -5185,6 +5497,7 @@ void diff_flush(struct diff_options *options)fclose (options->file); options->file =xfopen ("/dev/null" ,"w" ); options->close_file =1 ; + options->color_moved =0 ;for (i =0 ; i < q->nr; i++) {struct diff_filepair *p = q->queue[i];if (check_pair_status (p))
diff.h +11 -1
@@ -188,6 +188,12 @@ struct diff_options {int diff_path_counter;struct emitted_diff_symbols *emitted_symbols; +enum { +COLOR_MOVED_NO =0 , +COLOR_MOVED_ZEBRA =2 , + } color_moved; +#define COLOR_MOVED_DEFAULT COLOR_MOVED_ZEBRA +#define COLOR_MOVED_MIN_BLOCK_LENGTH 3 };void diff_emit_submodule_del (struct diff_options *o,const char *line);
@@ -208,7 +214,11 @@ enum color_diff {DIFF_FILE_NEW =5 ,DIFF_COMMIT =6 ,DIFF_WHITESPACE =7 , - DIFF_FUNCINFO = 8 +DIFF_FUNCINFO =8 , +DIFF_FILE_OLD_MOVED =9 , +DIFF_FILE_OLD_MOVED_ALT =10 , +DIFF_FILE_NEW_MOVED =11 , + DIFF_FILE_NEW_MOVED_ALT = 12 };const char *diff_get_color (int diff_use_color,enum color_diff ix);#define diff_get_color_opt (o, ix) \
t/t4015-diff-whitespace.sh +261 -0
265 lines not on this page · raw message
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH 24/25 diff.c: add dimming to moved line detection
In reply to Stefan BellerAny lines inside a moved block of code are not interesting. Boundaries of blocks are only interesting if they are next to another block of moved code.
--- color.h | 2 + diff.c | 132 +++++++++++++++++++++++++++++++++++++++++---- diff.h | 9 +++- t/t4015-diff-whitespace.sh | 124 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 254 insertions(+), 13 deletions(-)
color.h +2 -0
@@ -42,6 +42,8 @@ struct strbuf;#define GIT_COLOR_BG_BLUE "\033[44m"#define GIT_COLOR_BG_MAGENTA "\033[45m"#define GIT_COLOR_BG_CYAN "\033[46m" +#define GIT_COLOR_FAINT "\033[2m" +#define GIT_COLOR_FAINT_ITALIC "\033[2;3m"/* A special value meaning "no color selected" */ #define GIT_COLOR_NIL "NIL"
diff.c +121 -11
191 lines not on this page · raw message
diff.h +7 -2
@@ -192,6 +192,7 @@ struct diff_options {COLOR_MOVED_NO =0 ,COLOR_MOVED_PLAIN =1 ,COLOR_MOVED_ZEBRA =2 , +COLOR_MOVED_ZEBRA_DIM =3 , } color_moved;#define COLOR_MOVED_DEFAULT COLOR_MOVED_ZEBRA#define COLOR_MOVED_MIN_BLOCK_LENGTH 3
@@ -218,8 +219,12 @@ enum color_diff {DIFF_FUNCINFO =8 ,DIFF_FILE_OLD_MOVED =9 ,DIFF_FILE_OLD_MOVED_ALT =10 , -DIFF_FILE_NEW_MOVED =11 , - DIFF_FILE_NEW_MOVED_ALT = 12 +DIFF_FILE_OLD_MOVED_DIM =11 , +DIFF_FILE_OLD_MOVED_ALT_DIM =12 , +DIFF_FILE_NEW_MOVED =13 , +DIFF_FILE_NEW_MOVED_ALT =14 , +DIFF_FILE_NEW_MOVED_DIM =15 , +DIFF_FILE_NEW_MOVED_ALT_DIM =16 };const char *diff_get_color (int diff_use_color,enum color_diff ix);#define diff_get_color_opt (o, ix) \
t/t4015-diff-whitespace.sh +124 -0
130 lines not on this page · raw message
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH 25/25 diff: document the new --color-moved setting
In reply to Stefan Beller--- Documentation/config.txt | 15 +++++++++++++-- Documentation/diff-options.txt | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-)
Documentation/config.txt +13 -2
@@ -1077,14 +1077,25 @@ This does not affect linkgit:git-format-patch[1] or the 'git-diff-{asterisk}' plumbing commands. Can be overridden on the command line with the `--color[=<when>]` option. +diff.colorMoved:: + If set to either a valid `<mode>` or a true value, moved lines + in a diff are colored differently, for details of valid modes + see '--color-moved' in linkgit:git-diff[1]. If simply set to + true the default color mode will be used. When set to false, + moved lines are not colored. + color.diff.<slot>:: Use customized color for diff colorization. `<slot>` specifies which part of the patch to use the specified color, and is one 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), `oldMoved` (deleted lines), + `newMoved` (added lines), `oldMovedDimmed`, `oldMovedAlternative`, + `oldMovedAlternativeDimmed`, `newMovedDimmed`, `newMovedAlternative` + and `newMovedAlternativeDimmed` (See the '<mode>' + setting of '--color-moved' in linkgit:git-diff[1] for details). color.decorate.<slot>:: Use customized color for 'git log --decorate' output. `<slot>` is one
Documentation/diff-options.txt +34 -0
@@ -231,6 +231,40 @@ ifdef::git-diff[] endif::git-diff[] It is the same as `--color=never`. +--color-moved[=<mode>]:: + Moved lines of code are colored differently. +ifdef::git-diff[] + It can be changed by the `diff.colorMoved` configuration setting. +endif::git-diff[] + 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 code 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. + Small blocks of 3 moved lines or fewer are skipped. +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. +-- + --word-diff[=<mode>]:: Show a word diff, using the <mode> to delimit changed words. By default, words are delimited by whitespace; see
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH 19/25 diff.c: emit_diff_symbol learns about DIFF_SYMBOL_STAT_SEP
In reply to Stefan Beller--- diff.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
diff.c +7 -3
@@ -571,6 +571,7 @@ enum diff_symbol {DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES ,DIFF_SYMBOL_STATS_LINE ,DIFF_SYMBOL_WORD_DIFF , +DIFF_SYMBOL_STAT_SEP ,DIFF_SYMBOL_SUBMODULE_ADD ,DIFF_SYMBOL_SUBMODULE_DEL , DIFF_SYMBOL_SUBMODULE_UNTRACKED,
@@ -766,6 +767,9 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,case DIFF_SYMBOL_WORD_DIFF :fprintf (o->file,"%.*s" , len, line);break ; +case DIFF_SYMBOL_STAT_SEP : +fputs (o->stat_sep, o->file); +break ;default :die ("BUG: unknown diff symbol" ); }
@@ -5077,10 +5081,10 @@ void diff_flush(struct diff_options *options)if (output_format &DIFF_FORMAT_PATCH ) {if (separator) {emit_diff_symbol (options,DIFF_SYMBOL_SEPARATOR ,NULL ,0 ,0 ); -if (options->stat_sep) { +if (options->stat_sep)/* attach patch instead of inline */ -fputs (options->stat_sep, options->file); - } +emit_diff_symbol (options,DIFF_SYMBOL_STAT_SEP , +NULL ,0 ,0 ); }diff_flush_patch_all_file_pairs (options);
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH 11/25 diff.c: emit_diff_symbol learns DIFF_SYMBOL_FILEPAIR_{PLUS, MINUS}
In reply to Stefan BellerWe have to use fprintf instead of emit_line, because we want to emit the tab after the color. This is important for ancient versions of gnu patch AFAICT, although we probably do not want to feed colored output to the patch utility, such that it would not matter if the trailing tab is colored. Keep the corner case as-is though.
--- diff.c | 51 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 21 deletions(-)
diff.c +30 -21
@@ -561,6 +561,8 @@ static void emit_line(struct diff_options *o, const char *set, const char *reset }enum diff_symbol { +DIFF_SYMBOL_FILEPAIR_PLUS , +DIFF_SYMBOL_FILEPAIR_MINUS ,DIFF_SYMBOL_WORDS_PORCELAIN ,DIFF_SYMBOL_WORDS ,DIFF_SYMBOL_CONTEXT ,
@@ -611,7 +613,7 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s, const char *line, int len, unsigned flags) { static const char *nneof = " No newline at end of file\n"; -const char *context, *reset, *set; +const char *context, *reset, *set, *meta;switch (s) {case DIFF_SYMBOL_NO_LF_EOF : context =diff_get_color_opt (o,DIFF_CONTEXT );
@@ -673,6 +675,20 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s, }emit_line (o, context, reset, line, len);break ; +case DIFF_SYMBOL_FILEPAIR_PLUS : + meta =diff_get_color_opt (o,DIFF_METAINFO ); + reset =diff_get_color_opt (o,DIFF_RESET ); +fprintf (o->file,"%s%s+++ %s%s%s\n" ,diff_line_prefix (o), meta, + line, reset, +strchr (line,' ' ) ?"\t" :"" ); +break ; +case DIFF_SYMBOL_FILEPAIR_MINUS : + meta =diff_get_color_opt (o,DIFF_METAINFO ); + reset =diff_get_color_opt (o,DIFF_RESET ); +fprintf (o->file,"%s%s--- %s%s%s\n" ,diff_line_prefix (o), meta, + line, reset, +strchr (line,' ' ) ?"\t" :"" ); +break ;default :die ("BUG: unknown diff symbol" ); }
@@ -844,8 +860,6 @@ static void emit_rewrite_diff(const char *name_a, struct diff_options *o) {int lc_a, lc_b; -const char *name_a_tab, *name_b_tab; -const char *metainfo =diff_get_color (o->use_color,DIFF_METAINFO );const char *fraginfo =diff_get_color (o->use_color,DIFF_FRAGINFO );const char *reset =diff_get_color (o->use_color,DIFF_RESET );static struct strbuf a_name =STRBUF_INIT , b_name =STRBUF_INIT ;
@@ -865,8 +879,6 @@ static void emit_rewrite_diff(const char *name_a, name_a += (*name_a =='/' ); name_b += (*name_b =='/' ); - name_a_tab =strchr (name_a,' ' ) ?"\t" :"" ; - name_b_tab =strchr (name_b,' ' ) ?"\t" :"" ;strbuf_reset (&a_name);strbuf_reset (&b_name);
@@ -893,11 +905,13 @@ static void emit_rewrite_diff(const char *name_a, lc_a =count_lines (data_one, size_one); lc_b =count_lines (data_two, size_two); -fprintf (o->file, -"%s%s--- %s%s%s\n%s%s+++ %s%s%s\n%s%s@@ -" , - line_prefix, metainfo, a_name.buf, name_a_tab, reset, - line_prefix, metainfo, b_name.buf, name_b_tab, reset, - line_prefix, fraginfo); + +emit_diff_symbol (o,DIFF_SYMBOL_FILEPAIR_MINUS , + a_name.buf, a_name.len,0 ); +emit_diff_symbol (o,DIFF_SYMBOL_FILEPAIR_PLUS , + b_name.buf, b_name.len,0 ); + +fprintf (o->file,"%s%s@@ -" , line_prefix, fraginfo);if (!o->irreversible_delete)print_line_count (o->file, lc_a);else
@@ -1365,10 +1379,8 @@ static void find_lno(const char *line, struct emit_callback *ecbdata)static void fn_out_consume (void *priv,char *line,unsigned long len) {struct emit_callback *ecbdata = priv; -const char *meta =diff_get_color (ecbdata->color_diff,DIFF_METAINFO );const char *reset =diff_get_color (ecbdata->color_diff,DIFF_RESET );struct diff_options *o = ecbdata->opt; -const char *line_prefix =diff_line_prefix (o); o->found_changes =1 ;
@@ -1379,15 +1391,12 @@ static void fn_out_consume(void *priv, char *line, unsigned long len) }if (ecbdata->label_path[0 ]) { -const char *name_a_tab, *name_b_tab; - - name_a_tab =strchr (ecbdata->label_path[0 ],' ' ) ?"\t" :"" ; - name_b_tab =strchr (ecbdata->label_path[1 ],' ' ) ?"\t" :"" ; - -fprintf (o->file,"%s%s--- %s%s%s\n" , - line_prefix, meta, ecbdata->label_path[0 ], reset, name_a_tab); -fprintf (o->file,"%s%s+++ %s%s%s\n" , - line_prefix, meta, ecbdata->label_path[1 ], reset, name_b_tab); +emit_diff_symbol (o,DIFF_SYMBOL_FILEPAIR_MINUS , + ecbdata->label_path[0 ], +strlen (ecbdata->label_path[0 ]),0 ); +emit_diff_symbol (o,DIFF_SYMBOL_FILEPAIR_PLUS , + ecbdata->label_path[1 ], +strlen (ecbdata->label_path[1 ]),0 ); ecbdata->label_path[0 ] = ecbdata->label_path[1 ] =NULL ; }
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH 23/25 diff.c: color moved lines differently, plain mode
In reply to Stefan BellerAdd the 'plain' mode for move detection of code. This omits the checking for adjacent blocks, so it is not as useful. If you have a lot of the same blocks moved in the same patch, the 'Zebra' would end up slow as it is O(n^2) (n is number of same blocks). So this may be useful there and is generally easy to add. Instead be very literal at the move detection, do not skip over short blocks here.
--- diff.c | 10 +++++++-- diff.h | 1 + t/t4015-diff-whitespace.sh | 51 +++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 59 insertions(+), 3 deletions(-)
diff.c +8 -2
@@ -256,12 +256,14 @@ static int parse_color_moved(const char *arg)if (!strcmp (arg,"no" ))return COLOR_MOVED_NO ; +else if (!strcmp (arg,"plain" )) +return COLOR_MOVED_PLAIN ;else if (!strcmp (arg,"zebra" ))return COLOR_MOVED_ZEBRA ;else if (!strcmp (arg,"default" ))return COLOR_MOVED_DEFAULT ;else -return error (_ ("color moved setting must be one of 'no', 'default', 'zebra'" )); +return error (_ ("color moved setting must be one of 'no', 'default', 'zebra', 'plain'" )); }int git_diff_ui_config (const char *var,const char *value,void *cb)
@@ -876,7 +878,8 @@ static void mark_color_as_moved(struct diff_options *o, }if (!match) { -if (block_length <COLOR_MOVED_MIN_BLOCK_LENGTH ) { +if (block_length <COLOR_MOVED_MIN_BLOCK_LENGTH && + o->color_moved !=COLOR_MOVED_PLAIN ) {for (i =0 ; i < block_length +1 ; i++) { l = &o->emitted_symbols->buf[n - i]; l->flags &= ~DIFF_SYMBOL_MOVED_LINE ;
@@ -890,6 +893,9 @@ static void mark_color_as_moved(struct diff_options *o, l->flags |=DIFF_SYMBOL_MOVED_LINE ; block_length++; +if (o->color_moved ==COLOR_MOVED_PLAIN ) +continue ; +/* Check any potential block runs, advance each or nullify */ for (i =0 ; i < pmb_nr; i++) {struct moved_entry *p = pmb[i];
diff.h +1 -0
@@ -190,6 +190,7 @@ struct diff_options {struct emitted_diff_symbols *emitted_symbols;enum {COLOR_MOVED_NO =0 , +COLOR_MOVED_PLAIN =1 ,COLOR_MOVED_ZEBRA =2 , } color_moved;#define COLOR_MOVED_DEFAULT COLOR_MOVED_ZEBRA
t/t4015-diff-whitespace.sh +50 -1
63 lines not on this page · raw message
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH 15/25 submodule.c: migrate diff output to use emit_diff_symbol
In reply to Stefan BellerAs the submodule process is no longer attached to the same file pointer 'o->file' as the superprojects process, there is a different result in color.c::check_auto_color. That is why we need to pass coloring explicitly, such that the submodule coloring decision will be made by the child process processing the submodule. Only DIFF_SYMBOL_SUBMODULE_PIPETHROUGH contains color, the other symbols are for embedding the submodule output into the superprojects output.
Remove the colors from the function signatures, as all the coloring decisions will be made either inside the child process or the final emit_diff_symbol, but not in the functions driving the submodule diff.
--- diff.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++--------- diff.h | 9 +++++++ submodule.c | 84 +++++++++++++++++++++++++++---------------------------------- submodule.h | 13 +++------- 4 files changed, 121 insertions(+), 68 deletions(-)
diff.c +71 -12
@@ -561,6 +561,13 @@ static void emit_line(struct diff_options *o, const char *set, const char *reset }enum diff_symbol { +DIFF_SYMBOL_SUBMODULE_ADD , +DIFF_SYMBOL_SUBMODULE_DEL , +DIFF_SYMBOL_SUBMODULE_UNTRACKED , +DIFF_SYMBOL_SUBMODULE_MODIFIED , +DIFF_SYMBOL_SUBMODULE_HEADER , +DIFF_SYMBOL_SUBMODULE_ERROR , +DIFF_SYMBOL_SUBMODULE_PIPETHROUGH ,DIFF_SYMBOL_REWRITE_DIFF ,DIFF_SYMBOL_BINARY_FILES ,DIFF_SYMBOL_HEADER ,
@@ -625,6 +632,9 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,emit_line_0 (o, context, reset,'\\' , nneof,strlen (nneof));break ; +case DIFF_SYMBOL_SUBMODULE_HEADER : +case DIFF_SYMBOL_SUBMODULE_ERROR : +case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH :case DIFF_SYMBOL_CONTEXT_FRAGINFO :emit_line (o,"" ,"" , line, len);break ;
@@ -701,11 +711,68 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s, reset =diff_get_color_opt (o,DIFF_RESET );emit_line (o, fraginfo, reset, line, len);break ; +case DIFF_SYMBOL_SUBMODULE_ADD : + set =diff_get_color_opt (o,DIFF_FILE_NEW ); + reset =diff_get_color_opt (o,DIFF_RESET ); +emit_line (o, set, reset, line, len); +break ; +case DIFF_SYMBOL_SUBMODULE_DEL : + set =diff_get_color_opt (o,DIFF_FILE_OLD ); + reset =diff_get_color_opt (o,DIFF_RESET ); +emit_line (o, set, reset, line, len); +break ; +case DIFF_SYMBOL_SUBMODULE_UNTRACKED : +fprintf (o->file,"%sSubmodule %s contains untracked content\n" , +diff_line_prefix (o), line); +break ; +case DIFF_SYMBOL_SUBMODULE_MODIFIED : +fprintf (o->file,"%sSubmodule %s contains modified content\n" , +diff_line_prefix (o), line); +break ;default :die ("BUG: unknown diff symbol" ); } } +void diff_emit_submodule_del (struct diff_options *o,const char *line) +{ +emit_diff_symbol (o,DIFF_SYMBOL_SUBMODULE_DEL , line,strlen (line),0 ); +} + +void diff_emit_submodule_add (struct diff_options *o,const char *line) +{ +emit_diff_symbol (o,DIFF_SYMBOL_SUBMODULE_ADD , line,strlen (line),0 ); +} + +void diff_emit_submodule_untracked (struct diff_options *o,const char *path) +{ +emit_diff_symbol (o,DIFF_SYMBOL_SUBMODULE_UNTRACKED , + path,strlen (path),0 ); +} + +void diff_emit_submodule_modified (struct diff_options *o,const char *path) +{ +emit_diff_symbol (o,DIFF_SYMBOL_SUBMODULE_MODIFIED , + path,strlen (path),0 ); +} + +void diff_emit_submodule_header (struct diff_options *o,const char *header) +{ +emit_diff_symbol (o,DIFF_SYMBOL_SUBMODULE_HEADER , + header,strlen (header),0 ); +} + +void diff_emit_submodule_error (struct diff_options *o,const char *err) +{ +emit_diff_symbol (o,DIFF_SYMBOL_SUBMODULE_ERROR , err,strlen (err),0 ); +} + +void diff_emit_submodule_pipethrough (struct diff_options *o, +const char *line,int len) +{ +emit_diff_symbol (o,DIFF_SYMBOL_SUBMODULE_PIPETHROUGH , line, len,0 ); +} +static int new_blank_line_at_eof (struct emit_callback *ecbdata,const char *line,int len) { if (!((ecbdata->ws_rule & WS_BLANK_AT_EOF) &&
@@ -2467,24 +2534,16 @@ static void builtin_diff(const char *name_a,if (o->submodule_format ==DIFF_SUBMODULE_LOG && (!one->mode ||S_ISGITLINK (one->mode)) && (!two->mode ||S_ISGITLINK (two->mode))) { -const char *del =diff_get_color_opt (o,DIFF_FILE_OLD ); -const char *add =diff_get_color_opt (o,DIFF_FILE_NEW ); -show_submodule_summary (o->file, one->path ? one->path : two->path, - line_prefix, +show_submodule_summary (o, one->path ? one->path : two->path, &one->oid, &two->oid, - two->dirty_submodule, - meta, del, add, reset); + two->dirty_submodule);return ; }else if (o->submodule_format ==DIFF_SUBMODULE_INLINE_DIFF && (!one->mode ||S_ISGITLINK (one->mode)) && (!two->mode ||S_ISGITLINK (two->mode))) { -const char *del =diff_get_color_opt (o,DIFF_FILE_OLD ); -const char *add =diff_get_color_opt (o,DIFF_FILE_NEW ); -show_submodule_inline_diff (o->file, one->path ? one->path : two->path, - line_prefix, +show_submodule_inline_diff (o, one->path ? one->path : two->path, &one->oid, &two->oid, - two->dirty_submodule, - meta, del, add, reset, o); + two->dirty_submodule);return ; }
diff.h +9 -0
@@ -188,6 +188,15 @@ struct diff_options {int diff_path_counter; }; +void diff_emit_submodule_del (struct diff_options *o,const char *line); +void diff_emit_submodule_add (struct diff_options *o,const char *line); +void diff_emit_submodule_untracked (struct diff_options *o,const char *path); +void diff_emit_submodule_modified (struct diff_options *o,const char *path); +void diff_emit_submodule_header (struct diff_options *o,const char *header); +void diff_emit_submodule_error (struct diff_options *o,const char *err); +void diff_emit_submodule_pipethrough (struct diff_options *o, +const char *line,int len); +enum color_diff {DIFF_RESET =0 ,DIFF_CONTEXT =1 ,
submodule.c +37 -47
176 lines not on this page · raw message
submodule.h +4 -9
@@ -63,17 +63,12 @@ extern int parse_submodule_update_strategy(const char *value, struct submodule_update_strategy *dst);extern const char *submodule_strategy_to_string (const struct submodule_update_strategy *s);extern void handle_ignore_submodules_arg (struct diff_options *,const char *); -extern void show_submodule_summary (FILE *f,const char *path, -const char *line_prefix, +extern void show_submodule_summary (struct diff_options *o,const char *path,struct object_id *one,struct object_id *two, -unsigned dirty_submodule,const char *meta, -const char *del,const char *add,const char *reset); -extern void show_submodule_inline_diff (FILE *f,const char *path, -const char *line_prefix, +unsigned dirty_submodule); +extern void show_submodule_inline_diff (struct diff_options *o,const char *path,struct object_id *one,struct object_id *two, -unsigned dirty_submodule,const char *meta, -const char *del,const char *add,const char *reset, -const struct diff_options *opt); +unsigned dirty_submodule);extern void set_config_fetch_recurse_submodules (int value);/* Check if we want to update any submodule.*/ extern int should_update_submodules (void );
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH 14/25 diff.c: emit_diff_symbol learns DIFF_SYMBOL_REWRITE_DIFF
In reply to Stefan Beller--- diff.c | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-)
diff.c +21 -14
@@ -561,6 +561,7 @@ static void emit_line(struct diff_options *o, const char *set, const char *reset }enum diff_symbol { +DIFF_SYMBOL_REWRITE_DIFF ,DIFF_SYMBOL_BINARY_FILES ,DIFF_SYMBOL_HEADER ,DIFF_SYMBOL_FILEPAIR_PLUS ,
@@ -615,7 +616,7 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s, const char *line, int len, unsigned flags) { static const char *nneof = " No newline at end of file\n"; -const char *context, *reset, *set, *meta; +const char *context, *reset, *set, *meta, *fraginfo;switch (s) {case DIFF_SYMBOL_NO_LF_EOF : context =diff_get_color_opt (o,DIFF_CONTEXT );
@@ -695,6 +696,11 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,case DIFF_SYMBOL_HEADER :fprintf (o->file,"%s" , line);break ; +case DIFF_SYMBOL_REWRITE_DIFF : + fraginfo =diff_get_color (o->use_color,DIFF_FRAGINFO ); + reset =diff_get_color_opt (o,DIFF_RESET ); +emit_line (o, fraginfo, reset, line, len); +break ;default :die ("BUG: unknown diff symbol" ); }
@@ -817,17 +823,17 @@ static void remove_tempfile(void) } } -static void print_line_count (FILE *file,int count) +static void add_line_count (struct strbuf *out,int count) {switch (count) {case 0 : -fprintf (file,"0,0" ); +strbuf_addstr (out,"0,0" );break ;case 1 : -fprintf (file,"1" ); +strbuf_addstr (out,"1" );break ;default : -fprintf (file,"1,%d" , count); +strbuf_addf (out,"1,%d" , count);break ; } }
@@ -866,14 +872,12 @@ static void emit_rewrite_diff(const char *name_a, struct diff_options *o) {int lc_a, lc_b; -const char *fraginfo =diff_get_color (o->use_color,DIFF_FRAGINFO ); -const char *reset =diff_get_color (o->use_color,DIFF_RESET );static struct strbuf a_name =STRBUF_INIT , b_name =STRBUF_INIT ;const char *a_prefix, *b_prefix;char *data_one, *data_two;size_t size_one, size_two;struct emit_callback ecbdata; -const char *line_prefix =diff_line_prefix (o); +struct strbuf out =STRBUF_INIT ;if (diff_mnemonic_prefix &&DIFF_OPT_TST (o,REVERSE_DIFF )) { a_prefix = o->b_prefix;
@@ -917,14 +921,17 @@ static void emit_rewrite_diff(const char *name_a,emit_diff_symbol (o,DIFF_SYMBOL_FILEPAIR_PLUS , b_name.buf, b_name.len,0 ); -fprintf (o->file,"%s%s@@ -" , line_prefix, fraginfo); +strbuf_addstr (&out,"@@ -" );if (!o->irreversible_delete) -print_line_count (o->file, lc_a); +add_line_count (&out, lc_a);else -fprintf (o->file,"?,?" ); -fprintf (o->file," +" ); -print_line_count (o->file, lc_b); -fprintf (o->file," @@%s\n" , reset); +strbuf_addstr (&out,"?,?" ); +strbuf_addstr (&out," +" ); +add_line_count (&out, lc_b); +strbuf_addstr (&out," @@\n" ); +emit_diff_symbol (o,DIFF_SYMBOL_REWRITE_DIFF , out.buf, out.len,0 ); +strbuf_release (&out); +if (lc_a && !o->irreversible_delete)emit_rewrite_lines (&ecbdata,'-' , data_one, size_one);if (lc_b)
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH 16/25 diff.c: convert emit_binary_diff_body to use emit_diff_symbol
In reply to Stefan Beller--- diff.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 46 insertions(+), 17 deletions(-)
diff.c +46 -17
@@ -561,6 +561,11 @@ static void emit_line(struct diff_options *o, const char *set, const char *reset }enum diff_symbol { +DIFF_SYMBOL_BINARY_DIFF_HEADER , +DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA , +DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL , +DIFF_SYMBOL_BINARY_DIFF_BODY , +DIFF_SYMBOL_BINARY_DIFF_FOOTER ,DIFF_SYMBOL_SUBMODULE_ADD ,DIFF_SYMBOL_SUBMODULE_DEL ,DIFF_SYMBOL_SUBMODULE_UNTRACKED ,
@@ -635,6 +640,7 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,case DIFF_SYMBOL_SUBMODULE_HEADER :case DIFF_SYMBOL_SUBMODULE_ERROR :case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH : +case DIFF_SYMBOL_BINARY_DIFF_BODY :case DIFF_SYMBOL_CONTEXT_FRAGINFO :emit_line (o,"" ,"" , line, len);break ;
@@ -706,6 +712,19 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,case DIFF_SYMBOL_HEADER :fprintf (o->file,"%s" , line);break ; +case DIFF_SYMBOL_BINARY_DIFF_HEADER : +fprintf (o->file,"%sGIT binary patch\n" ,diff_line_prefix (o)); +break ; +case DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA : +fprintf (o->file,"%sdelta %s\n" ,diff_line_prefix (o), line); +break ; +case DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL : +fprintf (o->file,"%sliteral %s\n" ,diff_line_prefix (o), line); +break ; +case DIFF_SYMBOL_BINARY_DIFF_FOOTER : +fputs (diff_line_prefix (o), o->file); +fputc ('\n' , o->file); +break ;case DIFF_SYMBOL_REWRITE_DIFF : fraginfo =diff_get_color (o->use_color,DIFF_FRAGINFO ); reset =diff_get_color_opt (o,DIFF_RESET );
@@ -2390,8 +2409,8 @@ static unsigned char *deflate_it(char *data,return deflated; } -static void emit_binary_diff_body (FILE *file,mmfile_t *one,mmfile_t *two, -const char *prefix) +static void emit_binary_diff_body (struct diff_options *o, +mmfile_t *one,mmfile_t *two) {void *cp;void *delta;
@@ -2420,13 +2439,18 @@ static void emit_binary_diff_body(FILE *file, mmfile_t *one, mmfile_t *two, }if (delta && delta_size < deflate_size) { -fprintf (file,"%sdelta %lu\n" , prefix, orig_size); +char *s =xstrfmt ("%lu" , orig_size); +emit_diff_symbol (o,DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA , + s,strlen (s),0 ); +free (s);free (deflated); data = delta; data_size = delta_size; - } -else { -fprintf (file,"%sliteral %lu\n" , prefix, two->size); + }else { +char *s =xstrfmt ("%lu" , two->size); +emit_diff_symbol (o,DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL , + s,strlen (s),0 ); +free (s);free (delta); data = deflated; data_size = deflate_size;
@@ -2435,8 +2459,9 @@ static void emit_binary_diff_body(FILE *file, mmfile_t *one, mmfile_t *two,/* emit data encoded in base85 */ cp = data;while (data_size) { +int len;int bytes = (52 < data_size) ?52 : data_size; -char line[70 ]; +char line[71 ]; data_size -= bytes;if (bytes <=26 ) line[0 ] = bytes +'A' -1 ;
@@ -2444,20 +2469,24 @@ static void emit_binary_diff_body(FILE *file, mmfile_t *one, mmfile_t *two, line[0 ] = bytes -26 +'a' -1 ;encode_85 (line +1 , cp, bytes); cp = (char *) cp + bytes; -fprintf (file,"%s" , prefix); -fputs (line, file); -fputc ('\n' , file); + + len =strlen (line); + line[len++] ='\n' ; + line[len] ='\0' ; + +emit_diff_symbol (o,DIFF_SYMBOL_BINARY_DIFF_BODY , + line, len,0 ); } -fprintf (file,"%s\n" , prefix); +emit_diff_symbol (o,DIFF_SYMBOL_BINARY_DIFF_FOOTER ,NULL ,0 ,0 );free (data); } -static void emit_binary_diff (FILE *file,mmfile_t *one,mmfile_t *two, -const char *prefix) +static void emit_binary_diff (struct diff_options *o, +mmfile_t *one,mmfile_t *two) { -fprintf (file,"%sGIT binary patch\n" , prefix); -emit_binary_diff_body (file, one, two, prefix); -emit_binary_diff_body (file, two, one, prefix); +emit_diff_symbol (o,DIFF_SYMBOL_BINARY_DIFF_HEADER ,NULL ,0 ,0 ); +emit_binary_diff_body (o, one, two); +emit_binary_diff_body (o, two, one); }int diff_filespec_is_binary (struct diff_filespec *one)
@@ -2643,7 +2672,7 @@ static void builtin_diff(const char *name_a,emit_diff_symbol (o,DIFF_SYMBOL_HEADER , header.buf, header.len,0 );strbuf_reset (&header);if (DIFF_OPT_TST (o,BINARY )) -emit_binary_diff (o->file, &mf1, &mf2, line_prefix); +emit_binary_diff (o, &mf1, &mf2);else {strbuf_addf (&sb,"%sBinary files %s and %s differ\n" ,diff_line_prefix (o), lbl[0 ], lbl[1 ]);
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH 10/25 diff.c: emit_diff_symbol learns DIFF_SYMBOL_CONTEXT_INCOMPLETE
In reply to Stefan BellerThe context marker use the exact same output pattern, so reuse it.
--- diff.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
diff.c +4 -2
@@ -564,6 +564,7 @@ enum diff_symbol {DIFF_SYMBOL_WORDS_PORCELAIN ,DIFF_SYMBOL_WORDS ,DIFF_SYMBOL_CONTEXT , +DIFF_SYMBOL_CONTEXT_INCOMPLETE ,DIFF_SYMBOL_PLUS ,DIFF_SYMBOL_MINUS , DIFF_SYMBOL_NO_LF_EOF,
@@ -622,6 +623,7 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,case DIFF_SYMBOL_CONTEXT_FRAGINFO :emit_line (o,"" ,"" , line, len);break ; +case DIFF_SYMBOL_CONTEXT_INCOMPLETE :case DIFF_SYMBOL_CONTEXT_MARKER : context =diff_get_color_opt (o,DIFF_CONTEXT ); reset =diff_get_color_opt (o,DIFF_RESET );
@@ -1448,8 +1450,8 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)default :/* incomplete line at the end */ ecbdata->lno_in_preimage++; -emit_line (o,diff_get_color (ecbdata->color_diff,DIFF_CONTEXT ), - reset, line, len); +emit_diff_symbol (o,DIFF_SYMBOL_CONTEXT_INCOMPLETE , + line, len,0 );break ; } }
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH 17/25 diff.c: convert show_stats to use emit_diff_symbol
In reply to Stefan BellerWe call print_stat_summary from builtin/apply, so we still need the version with a file pointer, so introduce print_stat_summary_0 that uses emit_string machinery and keep print_stat_summary with the same arguments around.
--- diff.c | 114 +++++++++++++++++++++++++++++++++++++++++------------------------ diff.h | 4 +-- 2 files changed, 74 insertions(+), 44 deletions(-)
diff.c +72 -42
@@ -566,6 +566,10 @@ enum diff_symbol {DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL ,DIFF_SYMBOL_BINARY_DIFF_BODY ,DIFF_SYMBOL_BINARY_DIFF_FOOTER , +DIFF_SYMBOL_STATS_SUMMARY_NO_FILES , +DIFF_SYMBOL_STATS_SUMMARY_ABBREV , +DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES , +DIFF_SYMBOL_STATS_LINE ,DIFF_SYMBOL_SUBMODULE_ADD ,DIFF_SYMBOL_SUBMODULE_DEL , DIFF_SYMBOL_SUBMODULE_UNTRACKED,
@@ -629,6 +633,7 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s, {static const char *nneof =" No newline at end of file\n" ;const char *context, *reset, *set, *meta, *fraginfo; +struct strbuf sb =STRBUF_INIT ;switch (s) {case DIFF_SYMBOL_NO_LF_EOF : context =diff_get_color_opt (o,DIFF_CONTEXT );
@@ -640,6 +645,8 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,case DIFF_SYMBOL_SUBMODULE_HEADER :case DIFF_SYMBOL_SUBMODULE_ERROR :case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH : +case DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES : +case DIFF_SYMBOL_STATS_LINE :case DIFF_SYMBOL_BINARY_DIFF_BODY :case DIFF_SYMBOL_CONTEXT_FRAGINFO :emit_line (o,"" ,"" , line, len);
@@ -748,9 +755,17 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,fprintf (o->file,"%sSubmodule %s contains modified content\n" ,diff_line_prefix (o), line);break ; +case DIFF_SYMBOL_STATS_SUMMARY_NO_FILES : +emit_line (o,"" ,"" ," 0 files changed\n" , +strlen (" 0 files changed\n" )); +break ; +case DIFF_SYMBOL_STATS_SUMMARY_ABBREV : +emit_line (o,"" ,"" ," ...\n" ,strlen (" ...\n" )); +break ;default :die ("BUG: unknown diff symbol" ); } +strbuf_release (&sb); }void diff_emit_submodule_del (struct diff_options *o,const char *line)
@@ -1705,20 +1720,14 @@ static int scale_linear(int it, int width, int max_change)return 1 + (it * (width -1 ) / max_change); } -static void show_name (FILE *file, -const char *prefix,const char *name,int len) -{ -fprintf (file," %s%-*s |" , prefix, len, name); -} - -static void show_graph (FILE *file,char ch,int cnt,const char *set,const char *reset) +static void show_graph (struct strbuf *out,char ch,int cnt, +const char *set,const char *reset) {if (cnt <=0 )return ; -fprintf (file,"%s" , set); -while (cnt--) -putc (ch, file); -fprintf (file,"%s" , reset); +strbuf_addstr (out, set); +strbuf_addchars (out, ch, cnt); +strbuf_addstr (out, reset); }static void fill_print_name (struct diffstat_file *file)
@@ -1742,14 +1751,16 @@ static void fill_print_name(struct diffstat_file *file) file->print_name = pname; } -int print_stat_summary (FILE *fp,int files,int insertions,int deletions) +static void print_stat_summary_inserts_deletes (struct diff_options *options, +int files,int insertions,int deletions) {struct strbuf sb =STRBUF_INIT ; -int ret;if (!files) {assert (insertions ==0 && deletions ==0 ); -return fprintf (fp,"%s\n" ," 0 files changed" ); +emit_diff_symbol (options,DIFF_SYMBOL_STATS_SUMMARY_NO_FILES , +NULL ,0 ,0 ); +return ; } strbuf_addf(&sb,
@@ -1776,9 +1787,19 @@ int print_stat_summary(FILE *fp, int files, int insertions, int deletions) deletions); }strbuf_addch (&sb,'\n' ); - ret =fputs (sb.buf, fp); +emit_diff_symbol (options,DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES , + sb.buf, sb.len,0 );strbuf_release (&sb); -return ret; +} + +void print_stat_summary (FILE *fp,int files, +int insertions,int deletions) +{ +struct diff_options o; +memset (&o,0 ,sizeof (o)); + o.file = fp; + +print_stat_summary_inserts_deletes (&o, files, insertions, deletions); }static void show_stats (struct diffstat_t *data,struct diff_options *options)
@@ -1788,13 +1809,13 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)int total_files = data->nr, count;int width, name_width, graph_width, number_width =0 , bin_width =0 ;const char *reset, *add_c, *del_c; -const char *line_prefix ="" ;int extra_shown =0 ; +const char *line_prefix =diff_line_prefix (options); +struct strbuf out =STRBUF_INIT ;if (data->nr ==0 )return ; - line_prefix =diff_line_prefix (options); count = options->stat_count ? options->stat_count : data->nr; reset =diff_get_color_opt (options,DIFF_RESET );
@@ -1948,26 +1969,32 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options) }if (file->is_binary) { -fprintf (options->file,"%s" , line_prefix); -show_name (options->file, prefix, name, len); -fprintf (options->file," %*s" , number_width,"Bin" ); +strbuf_addf (&out," %s%-*s |" , prefix, len, name); +strbuf_addf (&out," %*s" , number_width,"Bin" );if (!added && !deleted) { -putc ('\n' , options->file); +strbuf_addch (&out,'\n' ); +emit_diff_symbol (options,DIFF_SYMBOL_STATS_LINE , + out.buf, out.len,0 ); +strbuf_reset (&out);continue ; } -fprintf (options->file," %s%" PRIuMAX"%s" , +strbuf_addf (&out," %s%" PRIuMAX"%s" , del_c, deleted, reset); -fprintf (options->file," -> " ); -fprintf (options->file,"%s%" PRIuMAX"%s" , +strbuf_addstr (&out," -> " ); +strbuf_addf (&out,"%s%" PRIuMAX"%s" , add_c, added, reset); -fprintf (options->file," bytes" ); -fprintf (options->file,"\n" ); +strbuf_addstr (&out," bytes\n" ); +emit_diff_symbol (options,DIFF_SYMBOL_STATS_LINE , + out.buf, out.len,0 ); +strbuf_reset (&out);continue ; }else if (file->is_unmerged) { -fprintf (options->file,"%s" , line_prefix); -show_name (options->file, prefix, name, len); -fprintf (options->file," Unmerged\n" ); +strbuf_addf (&out," %s%-*s |" , prefix, len, name); +strbuf_addstr (&out," Unmerged\n" ); +emit_diff_symbol (options,DIFF_SYMBOL_STATS_LINE , + out.buf, out.len,0 ); +strbuf_reset (&out);continue ; }
@@ -1990,14 +2017,16 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options) add = total - del; } } -fprintf (options->file,"%s" , line_prefix); -show_name (options->file, prefix, name, len); -fprintf (options->file," %*" PRIuMAX"%s" , +strbuf_addf (&out," %s%-*s |" , prefix, len, name); +strbuf_addf (&out," %*" PRIuMAX"%s" , number_width, added + deleted, added + deleted ?" " :"" ); -show_graph (options->file,'+' , add, add_c, reset); -show_graph (options->file,'-' , del, del_c, reset); -fprintf (options->file,"\n" ); +show_graph (&out,'+' , add, add_c, reset); +show_graph (&out,'-' , del, del_c, reset); +strbuf_addch (&out,'\n' ); +emit_diff_symbol (options,DIFF_SYMBOL_STATS_LINE , + out.buf, out.len,0 ); +strbuf_reset (&out); }for (i =0 ; i < data->nr; i++) {
@@ -2018,11 +2047,13 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)if (i < count)continue ;if (!extra_shown) -fprintf (options->file,"%s ...\n" , line_prefix); +emit_diff_symbol (options, +DIFF_SYMBOL_STATS_SUMMARY_ABBREV , +NULL ,0 ,0 ); extra_shown =1 ; } -fprintf (options->file,"%s" , line_prefix); -print_stat_summary (options->file, total_files, adds, dels); + +print_stat_summary_inserts_deletes (options, total_files, adds, dels); }static void show_shortstats (struct diffstat_t *data,struct diff_options *options)
@@ -2034,7 +2065,7 @@ static void show_shortstats(struct diffstat_t *data, struct diff_options *optionfor (i =0 ; i < data->nr; i++) {int added = data->files[i]->added; -int deleted= data->files[i]->deleted; +int deleted = data->files[i]->deleted;if (data->files[i]->is_unmerged || (!data->files[i]->is_interesting && (added + deleted ==0 ))) {
@@ -2044,8 +2075,7 @@ static void show_shortstats(struct diffstat_t *data, struct diff_options *option dels += deleted; } } -fprintf (options->file,"%s" ,diff_line_prefix (options)); -print_stat_summary (options->file, total_files, adds, dels); +print_stat_summary_inserts_deletes (options, total_files, adds, dels); }static void show_numstat (struct diffstat_t *data,struct diff_options *options)
diff.h +2 -2
@@ -405,8 +405,8 @@ extern int parse_rename_score(const char **cp_p);extern long parse_algorithm_value (const char *value); -extern int print_stat_summary (FILE *fp,int files, -int insertions,int deletions); +extern void print_stat_summary (FILE *fp,int files, +int insertions,int deletions);extern void setup_diff_pager (struct diff_options *);#endif /* DIFF_H */
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH 9/25 diff.c: emit_diff_symbol learns DIFF_SYMBOL_WORDS[_PORCELAIN]
In reply to Stefan Beller--- diff.c | 42 ++++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-)
diff.c +26 -16
@@ -561,6 +561,8 @@ static void emit_line(struct diff_options *o, const char *set, const char *reset }enum diff_symbol { +DIFF_SYMBOL_WORDS_PORCELAIN , +DIFF_SYMBOL_WORDS ,DIFF_SYMBOL_CONTEXT ,DIFF_SYMBOL_PLUS ,DIFF_SYMBOL_MINUS ,
@@ -649,6 +651,26 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,emit_line_ws_markup (o, set, reset, line, len,'-' , flags &DIFF_SYMBOL_CONTENT_WS_MASK ,0 );break ; +case DIFF_SYMBOL_WORDS_PORCELAIN : + context =diff_get_color_opt (o,DIFF_CONTEXT ); + reset =diff_get_color_opt (o,DIFF_RESET ); +emit_line (o, context, reset, line, len); +fputs ("~\n" , o->file); +break ; +case DIFF_SYMBOL_WORDS : + context =diff_get_color_opt (o,DIFF_CONTEXT ); + reset =diff_get_color_opt (o,DIFF_RESET ); +/* +* Skip the prefix character, if any. With +* diff_suppress_blank_empty, there may be +* none. +*/ +if (line[0 ] !='\n' ) { + line++; + len--; + } +emit_line (o, context, reset, line, len); +break ;default :die ("BUG: unknown diff symbol" ); }
@@ -1342,7 +1364,6 @@ static void fn_out_consume(void *priv, char *line, unsigned long len) {struct emit_callback *ecbdata = priv;const char *meta =diff_get_color (ecbdata->color_diff,DIFF_METAINFO ); -const char *context =diff_get_color (ecbdata->color_diff,DIFF_CONTEXT );const char *reset =diff_get_color (ecbdata->color_diff,DIFF_RESET );struct diff_options *o = ecbdata->opt;const char *line_prefix =diff_line_prefix (o);
@@ -1384,6 +1405,9 @@ static void fn_out_consume(void *priv, char *line, unsigned long len) }if (ecbdata->diff_words) { +enum diff_symbol s = + ecbdata->diff_words->type ==DIFF_WORDS_PORCELAIN ? +DIFF_SYMBOL_WORDS_PORCELAIN :DIFF_SYMBOL_WORDS ;if (line[0 ] =='-' ) {diff_words_append (line, len, &ecbdata->diff_words->minus);
@@ -1403,21 +1427,7 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)return ; }diff_words_flush (ecbdata); -if (ecbdata->diff_words->type ==DIFF_WORDS_PORCELAIN ) { -emit_line (o, context, reset, line, len); -fputs ("~\n" , o->file); - }else { -/* -* Skip the prefix character, if any. With -* diff_suppress_blank_empty, there may be -* none. -*/ -if (line[0 ] !='\n' ) { - line++; - len--; - } -emit_line (o, context, reset, line, len); - } +emit_diff_symbol (o, s, line, len,0 );return ; }
Signature · 1 line
2.13.0.31.g9b732c453e
cover Reroll of sb/diff-color-moved.
Reroll of sb/diff-color-moved, applies on top of the hashmap patches[1].
* Supports different whitespace modes correctly now (and has a test for trivial white space changes)
* The fixups[2] have been squashed.
Thanks,
Stefan
[1] https://public-inbox.org/git/[email protected]/
[2] https://public-inbox.org/git/[email protected]/
Stefan Beller (25):
diff.c: readability fix
diff.c: move line ending check into emit_hunk_header diff.c: factor out diff_flush_patch_all_file_pairs diff.c: introduce emit_diff_symbol
diff.c: emit_diff_symbol learns DIFF_SYMBOL_CONTEXT_MARKER diff.c: emit_diff_symbol learns DIFF_SYMBOL_CONTEXT_FRAGINFO diff.c: emit_diff_symbol learns DIFF_SYMBOL_NO_LF_EOF diff.c: migrate emit_line_checked to use emit_diff_symbol diff.c: emit_diff_symbol learns DIFF_SYMBOL_WORDS[_PORCELAIN] diff.c: emit_diff_symbol learns DIFF_SYMBOL_CONTEXT_INCOMPLETE diff.c: emit_diff_symbol learns DIFF_SYMBOL_FILEPAIR_{PLUS, MINUS} diff.c: emit_diff_symbol learns DIFF_SYMBOL_HEADER diff.c: emit_diff_symbol learns about DIFF_SYMBOL_BINARY_FILES diff.c: emit_diff_symbol learns DIFF_SYMBOL_REWRITE_DIFF submodule.c: migrate diff output to use emit_diff_symbol diff.c: convert emit_binary_diff_body to use emit_diff_symbol diff.c: convert show_stats to use emit_diff_symbol diff.c: convert word diffing to use emit_diff_symbol diff.c: emit_diff_symbol learns about DIFF_SYMBOL_STAT_SEP diff.c: emit_diff_symbol learns about DIFF_SYMBOL_SUMMARY diff.c: buffer all output if asked to
diff.c: color moved lines differently
diff.c: color moved lines differently, plain mode diff.c: add dimming to moved line detection
diff: document the new --color-moved setting
Documentation/config.txt | 15 +- Documentation/diff-options.txt | 34 ++ cache.h | 2 + color.h | 2 + diff.c | 1306 ++++++++++++++++++++++++++++++++-------- diff.h | 39 +- submodule.c | 84 ++- submodule.h | 13 +- t/t4015-diff-whitespace.sh | 434 +++++++++++++ 9 files changed, 1613 insertions(+), 316 deletions(-)
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH 8/25 diff.c: migrate emit_line_checked to use emit_diff_symbol
Add a new flags field to emit_diff_symbol, that will be used by context lines for:
* white space rules that are applicable (The first 12 bits) Take a note in cahe.c as well, when this ws rules are extended we have to fix the bits in the flags field.
* how the rules are evaluated (actually this double encodes the sign of the line, but the code is easier to keep this way, bits 13,14,15)
* if the line a blank line at EOF (bit 16)
The check if new lines need to be marked up as extra lines at the end of file, is now done unconditionally. That should be ok, as 'new_blank_line_at_eof' has a quick early return.
--- cache.h | 2 ++ diff.c | 116 +++++++++++++++++++++++++++++++++++++++++----------------------- diff.h | 6 ++-- 3 files changed, 80 insertions(+), 44 deletions(-)
cache.h +2 -0
@@ -1996,6 +1996,8 @@ void shift_tree_by(const struct object_id *, const struct object_id *, struct ob#define WS_TRAILING_SPACE (WS_BLANK_AT_EOL|WS_BLANK_AT_EOF)#define WS_DEFAULT_RULE (WS_TRAILING_SPACE|WS_SPACE_BEFORE_TAB|8)#define WS_TAB_WIDTH_MASK 077 +/* All WS_* -- when extended, adapt diff.c emit_symbol */ +#define WS_RULE_MASK 07777extern unsigned whitespace_rule_cfg;extern unsigned whitespace_rule (const char *);extern unsigned parse_whitespace_rule (const char *);
diff.c +75 -41
189 lines not on this page · raw message
diff.h +3 -3
@@ -148,9 +148,9 @@ struct diff_options {int abbrev;int ita_invisible_in_index;/* white-space error highlighting */ -#define WSEH_NEW 1 -#define WSEH_CONTEXT 2 -#define WSEH_OLD 4 +#define WSEH_NEW (1<<12) +#define WSEH_CONTEXT (1<<13) +#define WSEH_OLD (1<<14)unsigned ws_error_highlight;const char *prefix;int prefix_length;
Signature · 1 line
2.13.0.31.g9b732c453e
Re: [PATCH 25/25] diff: document the new --color-moved setting
In reply to Stefan BellerDocumentation/diff-options.txt ifdef::git-diff[] · line 261
Quoted 1 line · Stefan Beller
Documentation/diff-options.txt +1
-0+ Small blocks of 3 moved lines or fewer are skipped.
If I read the commit messages correctly, this "skipping" process applies to the move detection in general for those smaller blocks and therefore doesn't mean a malicious move can hide smaller changes, correct? If so, I find this sentence misleading. Maybe something like:
Small blocks of 3 moved lines or fewer are excluded from move detection and colored as regular diff.
Regards
Simon
Signature · 3 lines
+ privacy is necessary + using gnupg http://gnupg.org + public key id: 0x92FEFDB7E44C32F9
Re: [PATCH 25/25] diff: document the new --color-moved setting
Quoted 11 lines · Simon Ruderich
Quoted 1 line · Stefan Beller
+ Small blocks of 3 moved lines or fewer are skipped.
If I read the commit messages correctly, this "skipping" process applies to the move detection in general for those smaller blocks and therefore doesn't mean a malicious move can hide smaller changes, correct? If so, I find this sentence misleading. Maybe something like:
Small blocks of 3 moved lines or fewer are excluded from move detection and colored as regular diff.
Well, this reads as if "blocks of 3 lines" are excluded, but what I mean is "if all adjacent blocks combined are 3 lines or fewer"
Example of how I understand the code:
context
+ moved line, block A
+ moved line, block A
+ moved line, block B
+ moved line, block A
+ moved line, block A
context
These five lines are colored, because 5>3, but each individual block is smaller than 3 lines. However we already want to tell the reviewer that the middle line is not part of a contiguous 5 line block, so we have to use alternative color in the middle.
However
context
+ moved line, block A or B
+ moved line, block A or B
context
is omitted, because the number of lines
here is fewer than 3 ignoring the block
type.
Maybe
If there are fewer than 3 adjacent lines of
moved code, they are skipped.
Thanks,
Stefan
Quoted 7 lines
Regards
Simon
--
+ privacy is necessary
+ using gnupg http://gnupg.org
+ public key id:0x92FEFDB7E44C32F9
Re: [PATCH 22/25] diff.c: color moved lines differently
In reply to Stefan Bellerdiff.c static void append_emitted_diff_symbol(struct diff_options *o, · line 706
Quoted 9 lines · Stefan Beller
diff.c +9
-0+static int next_byte (const char **cp,const char **endp, +const struct diff_options *diffopt) +{ +int retval; + +if (DIFF_XDL_TST (diffopt,IGNORE_WHITESPACE_AT_EOL )) { +while (*endp > *cp &&isspace (**endp)) + (*endp)--; + }
This should be done by the callers (both moved_entry_cmp() and get_string_hash()) before starting to iterate over the bytes from the beginning, no?
Quoted 10 lines
diff.c +10
-0+if (*cp > *endp) +return -1 ; + +if (DIFF_XDL_TST (diffopt,IGNORE_WHITESPACE_CHANGE )) { +while (*cp < *endp &&isspace (**cp)) + (*cp)++; +/* +* After skipping a couple of whitespaces, we still have to +* account for one space. +*/
diff.c static void append_emitted_diff_symbol(struct diff_options *o, · line 727
Quoted 10 lines
diff.c +10
-0+return (int )' ' ; + } + +if (DIFF_XDL_TST (diffopt,IGNORE_WHITESPACE )) { +while (*cp < *endp &&isspace (**cp)) + (*cp)++; +/* return the first non-ws character via the usual below */ + } + + retval = **cp;
The char could be signed, and byte 0xff may become indistinguishable from the EOF (i.e. -1) you returned earlier.
Quoted 13 lines
diff.c +13
-0+ (*cp)++;/* advance */ +return retval; +} + +static int moved_entry_cmp (const struct moved_entry *a, +const struct moved_entry *b, +const void *keydata, +const void *data) +{ +const struct diff_options *diffopt = data; +const char *ap = a->es->line, *ae = a->es->line + a->es->len; +const char *bp = b->es->line, *be = b->es->line + b->es->len; +
diff.c static void append_emitted_diff_symbol(struct diff_options *o, · line 750
Quoted 10 lines
diff.c +10
-0+if (!(diffopt->xdl_opts &XDF_WHITESPACE_FLAGS )) +return a->es->len != b->es->len ||memcmp (ap, bp, a->es->len); + +while (1 ) { +int ca, cb; + ca =next_byte (&ap, &ae, diffopt); + cb =next_byte (&bp, &be, diffopt); +if (ca != cb) +return 1 ;/* differs */ +if (!ca)
Shouldn't this check for "ca == -1", as we are not dealing with NUL terminated string but a <ptr, len> thing?
Quoted 20 lines
diff.c +20
-0+return 0 ; + }; +} + +static unsigned get_string_hash (struct emitted_diff_symbol *es,struct diff_options *o) +{ +if (o->xdl_opts &XDF_WHITESPACE_FLAGS ) { +static struct strbuf sb =STRBUF_INIT ; +const char *ap = es->line, *ae = es->line + es->len; +int c; + +strbuf_reset (&sb); +while ((c =next_byte (&ap, &ae, o)) >0 ) +strbuf_addch (&sb, c); + +return memhash (sb.buf, sb.len); + }else { +return memhash (es->line, es->len); + } +}
Re: [PATCH 22/25] diff.c: color moved lines differently
Quoted 15 lines · Junio C Hamano
diff.c static void append_emitted_diff_symbol(struct diff_options *o, · line 706
Quoted 9 lines · Stefan Beller
diff.c +9
-0+static int next_byte (const char **cp,const char **endp, +const struct diff_options *diffopt) +{ +int retval; + +if (DIFF_XDL_TST (diffopt,IGNORE_WHITESPACE_AT_EOL )) { +while (*endp > *cp &&isspace (**endp)) + (*endp)--; + }This should be done by the callers (both
moved_entry_cmp()andget_string_hash()) before starting to iterate over the bytes from the beginning, no?
Good point.
Quoted 5 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ + retval = **cp;The char could be signed, and byte
0xffmay become indistinguishable from the EOF (i.e. -1) you returned earlier.
Ah, I messed up there. I think EOF is wrong, too.
So maybe we'll just return 256 to indicate the end of memory chunk to not have to deal with signedness
Quoted 6 lines
diff.c static void append_emitted_diff_symbol(struct diff_options *o, · line 750
Quoted 3 lines
diff.c +3
-0+if (ca != cb) +return 1 ;/* differs */ +if (!ca)Shouldn't this check for "ca == -1", as we are not dealing with NUL terminated string but a <ptr, len> thing?
Yes, we'd check for the ending symbol instead of 0.
Re: [PATCH 22/25] diff.c: color moved lines differently
Quoted 22 lines · Stefan Beller
Quoted 15 lines · Junio C Hamano
diff.c static void append_emitted_diff_symbol(struct diff_options *o, · line 706
Quoted 9 lines · Stefan Beller
diff.c +9
-0+static int next_byte (const char **cp,const char **endp, +const struct diff_options *diffopt) +{ +int retval; + +if (DIFF_XDL_TST (diffopt,IGNORE_WHITESPACE_AT_EOL )) { +while (*endp > *cp &&isspace (**endp)) + (*endp)--; + }This should be done by the callers (both
moved_entry_cmp()andget_string_hash()) before starting to iterate over the bytes from the beginning, no?Good point.
Quoted 3 lines
Quoted 2 lines
(quoted diff) +2
-0+ + retval = **cp;
Quoted 6 lines
Quoted 2 lines
The char could be signed, and byte
0xffmay become indistinguishable from the EOF (i.e. -1) you returned earlier.Ah, I messed up there. I think EOF is wrong, too.
So maybe we'll just return 256 to indicate the end of memory chunk to not have to deal with signedness
I would just say that next_byte() returns -1 (at end of string) or 0-255 if it is returning a byte that matters.
Re: [PATCH 25/25] diff: document the new --color-moved setting
In reply to Stefan BellerQuoted 9 lines · Stefan Beller
[snip]
However
context
+ moved line, block A or B
+ moved line, block A or B
context
Quoted 8 lines
is omitted, because the number of lines
here is fewer than 3 ignoring the block
type.Maybe
If there are fewer than 3 adjacent lines of
moved code, they are skipped.
My issue with "skipped" is that it's not clear what exactly is skipped. Move detection? Inclusion in the diff? Something else? That's why I tried to add the "excluded from move detection" to make it clear that the change is still shown to the user, just not handled by move detection and using the usual diff colors.
In your example above, what exactly is "omitted"? The complete hunk from the output or the special coloring? That's what confuses me and what might confuse a future reader of the documentation.
Regards
Simon
Signature · 3 lines
+ privacy is necessary + using gnupg http://gnupg.org + public key id: 0x92FEFDB7E44C32F9
Re: [PATCH 25/25] diff: document the new --color-moved setting
Quoted 25 lines · Simon Ruderich
Quoted 9 lines · Stefan Beller
[snip]
However
context
+ moved line, block A or B
+ moved line, block A or B
contextQuoted 8 lines
is omitted, because the number of lines
here is fewer than 3 ignoring the block
type.Maybe
If there are fewer than 3 adjacent lines of
moved code, they are skipped.My issue with "skipped" is that it's not clear what exactly is skipped. Move detection? Inclusion in the diff? Something else? That's why I tried to add the "excluded from move detection" to make it clear that the change is still shown to the user, just not handled by move detection and using the usual diff colors.
Quoted 4 lines
In your example above, what exactly is "omitted"? The complete hunk from the output or the special coloring? That's what confuses me and what might confuse a future reader of the documentation.
Oh. I see. That was not the point of confusion that I thought.
skipped/omitted both refer to the move detection and the lines will be included in the diff, but just as regular added/removed lines.
Will fix.
Thanks for bearing with me,
Stefan
v2 cover Reroll of sb/diff-color-moved.
In reply to Stefan Bellerv2:
* on top of the new hashmap patches[3]. It compiles when put on older versions of the hashmap series, as the hashmap compare function just swapped void pointers in the definition.
* better documentation, Thanks Simon!
... If there
are fewer than 3 adjacent moved lines, they are not marked up as moved, but the regular colors 'color.diff.{old,new}' will be used. ...
* clarified the compare function (casting the return to unsigned char, such that 0-255 is from the string, -1 indicated EOF. Adapted the checks for that.
Thanks,
Stefan
[3] https://public-inbox.org/git/[email protected]/
v1:
Reroll of sb/diff-color-moved, applies on top of the hashmap patches[1].
* Supports different whitespace modes correctly now (and has a test for trivial white space changes)
* The fixups[2] have been squashed.
Thanks,
Stefan
[1] https://public-inbox.org/git/[email protected]/
[2] https://public-inbox.org/git/[email protected]/
Stefan Beller (25):
diff.c: readability fix
diff.c: move line ending check into emit_hunk_header diff.c: factor out diff_flush_patch_all_file_pairs diff.c: introduce emit_diff_symbol
diff.c: emit_diff_symbol learns DIFF_SYMBOL_CONTEXT_MARKER diff.c: emit_diff_symbol learns DIFF_SYMBOL_CONTEXT_FRAGINFO diff.c: emit_diff_symbol learns DIFF_SYMBOL_NO_LF_EOF diff.c: migrate emit_line_checked to use emit_diff_symbol diff.c: emit_diff_symbol learns DIFF_SYMBOL_WORDS[_PORCELAIN] diff.c: emit_diff_symbol learns DIFF_SYMBOL_CONTEXT_INCOMPLETE diff.c: emit_diff_symbol learns DIFF_SYMBOL_FILEPAIR_{PLUS, MINUS} diff.c: emit_diff_symbol learns DIFF_SYMBOL_HEADER diff.c: emit_diff_symbol learns about DIFF_SYMBOL_BINARY_FILES diff.c: emit_diff_symbol learns DIFF_SYMBOL_REWRITE_DIFF submodule.c: migrate diff output to use emit_diff_symbol diff.c: convert emit_binary_diff_body to use emit_diff_symbol diff.c: convert show_stats to use emit_diff_symbol diff.c: convert word diffing to use emit_diff_symbol diff.c: emit_diff_symbol learns about DIFF_SYMBOL_STAT_SEP diff.c: emit_diff_symbol learns about DIFF_SYMBOL_SUMMARY diff.c: buffer all output if asked to
diff.c: color moved lines differently
diff.c: color moved lines differently, plain mode diff.c: add dimming to moved line detection
diff: document the new --color-moved setting
Documentation/config.txt | 15 +- Documentation/diff-options.txt | 36 ++ cache.h | 2 + color.h | 2 + diff.c | 1309 ++++++++++++++++++++++++++++++++-------- diff.h | 39 +- submodule.c | 84 ++- submodule.h | 13 +- t/t4015-diff-whitespace.sh | 434 +++++++++++++ 9 files changed, 1618 insertions(+), 316 deletions(-)
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH v2 1/25 diff.c: readability fix
We already have dereferenced 'p->two' into a local variable 'two'. Use that.
--- diff.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
diff.c +2 -2
@@ -3282,8 +3282,8 @@ static void run_diff(struct diff_filepair *p, struct diff_options *o)const char *other;const char *attr_path; - name = p->one->path; - other = (strcmp (name, p->two->path) ? p->two->path :NULL ); + name = one->path; + other = (strcmp (name, two->path) ? two->path :NULL ); attr_path = name;if (o->prefix_length)strip_prefix (o->prefix_length, &name, &other);
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH v2 2/25 diff.c: move line ending check into emit_hunk_header
In reply to Stefan BellerThe emit_hunk_header() function is responsible for assembling a hunk header and calling emit_line() to send the hunk header to the output file. Its only caller fn_out_consume() needs to prepare for a case where the function emits an incomplete line and add the terminating LF.
Instead make sure emit_hunk_header() to always send a completed line to emit_line().
--- diff.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
diff.c +2 -2
@@ -678,6 +678,8 @@ static void emit_hunk_header(struct emit_callback *ecbdata, }strbuf_add (&msgbuf, line + len, org_len - len); +strbuf_complete_line (&msgbuf); +emit_line (ecbdata->opt,"" ,"" , msgbuf.buf, msgbuf.len);strbuf_release (&msgbuf); }
@@ -1315,8 +1317,6 @@ static void fn_out_consume(void *priv, char *line, unsigned long len) len =sane_truncate_line (ecbdata, line, len);find_lno (line, ecbdata);emit_hunk_header (ecbdata, line, len); -if (line[len-1 ] !='\n' ) -putc ('\n' , o->file);return ; }
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH v2 6/25 diff.c: emit_diff_symbol learns DIFF_SYMBOL_CONTEXT_FRAGINFO
In reply to Stefan Beller--- diff.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
diff.c +6 -2
@@ -561,6 +561,7 @@ static void emit_line(struct diff_options *o, const char *set, const char *reset }enum diff_symbol { +DIFF_SYMBOL_CONTEXT_FRAGINFO ,DIFF_SYMBOL_CONTEXT_MARKER ,DIFF_SYMBOL_SEPARATOR };
@@ -570,6 +571,9 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s, {const char *context, *reset;switch (s) { +case DIFF_SYMBOL_CONTEXT_FRAGINFO : +emit_line (o,"" ,"" , line, len); +break ;case DIFF_SYMBOL_CONTEXT_MARKER : context =diff_get_color_opt (o,DIFF_CONTEXT ); reset =diff_get_color_opt (o,DIFF_RESET );
@@ -705,8 +709,8 @@ static void emit_hunk_header(struct emit_callback *ecbdata,strbuf_add (&msgbuf, line + len, org_len - len);strbuf_complete_line (&msgbuf); - -emit_line (ecbdata->opt,"" ,"" , msgbuf.buf, msgbuf.len); +emit_diff_symbol (ecbdata->opt, +DIFF_SYMBOL_CONTEXT_FRAGINFO , msgbuf.buf, msgbuf.len);strbuf_release (&msgbuf); }
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH v2 7/25 diff.c: emit_diff_symbol learns DIFF_SYMBOL_NO_LF_EOF
In reply to Stefan Beller--- diff.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-)
diff.c +11 -8
@@ -561,6 +561,7 @@ static void emit_line(struct diff_options *o, const char *set, const char *reset }enum diff_symbol { +DIFF_SYMBOL_NO_LF_EOF ,DIFF_SYMBOL_CONTEXT_FRAGINFO ,DIFF_SYMBOL_CONTEXT_MARKER ,DIFF_SYMBOL_SEPARATOR
@@ -569,8 +570,16 @@ enum diff_symbol {static void emit_diff_symbol (struct diff_options *o,enum diff_symbol s,const char *line,int len) { +static const char *nneof =" No newline at end of file\n" ;const char *context, *reset;switch (s) { +case DIFF_SYMBOL_NO_LF_EOF : + context =diff_get_color_opt (o,DIFF_CONTEXT ); + reset =diff_get_color_opt (o,DIFF_RESET ); +putc ('\n' , o->file); +emit_line_0 (o, context, reset,'\\' , + nneof,strlen (nneof)); +break ;case DIFF_SYMBOL_CONTEXT_FRAGINFO :emit_line (o,"" ,"" , line, len);break ;
@@ -751,7 +760,6 @@ static void emit_rewrite_lines(struct emit_callback *ecb, int prefix, const char *data, int size) { const char *endp = NULL; -static const char *nneof =" No newline at end of file\n" ;const char *reset =diff_get_color (ecb->color_diff,DIFF_RESET );while (0 < size) {
@@ -769,13 +777,8 @@ static void emit_rewrite_lines(struct emit_callback *ecb, size -= len; data += len; } -if (!endp) { -const char *context =diff_get_color (ecb->color_diff, -DIFF_CONTEXT ); -putc ('\n' , ecb->opt->file); -emit_line_0 (ecb->opt, context, reset,'\\' , - nneof,strlen (nneof)); - } +if (!endp) +emit_diff_symbol (ecb->opt,DIFF_SYMBOL_NO_LF_EOF ,NULL ,0 ); } static void emit_rewrite_diff(const char *name_a,
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH v2 5/25 diff.c: emit_diff_symbol learns DIFF_SYMBOL_CONTEXT_MARKER
In reply to Stefan Beller--- diff.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
diff.c +9 -1
@@ -561,13 +561,20 @@ static void emit_line(struct diff_options *o, const char *set, const char *reset }enum diff_symbol { +DIFF_SYMBOL_CONTEXT_MARKER ,DIFF_SYMBOL_SEPARATOR };static void emit_diff_symbol (struct diff_options *o,enum diff_symbol s,const char *line,int len) { +const char *context, *reset;switch (s) { +case DIFF_SYMBOL_CONTEXT_MARKER : + context =diff_get_color_opt (o,DIFF_CONTEXT ); + reset =diff_get_color_opt (o,DIFF_RESET ); +emit_line (o, context, reset, line, len); +break ;case DIFF_SYMBOL_SEPARATOR : fprintf(o->file, "%s%c", diff_line_prefix(o),
@@ -662,7 +669,8 @@ static void emit_hunk_header(struct emit_callback *ecbdata,if (len <10 ||memcmp (line, atat,2 ) || !(ep =memmem (line +2 , len -2 , atat,2 ))) { -emit_line (ecbdata->opt, context, reset, line, len); +emit_diff_symbol (ecbdata->opt, +DIFF_SYMBOL_CONTEXT_MARKER , line, len);return ; } ep +=2 ;/* skip over @@ */
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH v2 12/25 diff.c: emit_diff_symbol learns DIFF_SYMBOL_HEADER
In reply to Stefan BellerThe header is constructed lazily including line breaks, so just emit the raw string as is.
--- diff.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-)
diff.c +20 -8
@@ -561,6 +561,7 @@ static void emit_line(struct diff_options *o, const char *set, const char *reset }enum diff_symbol { +DIFF_SYMBOL_HEADER ,DIFF_SYMBOL_FILEPAIR_PLUS ,DIFF_SYMBOL_FILEPAIR_MINUS ,DIFF_SYMBOL_WORDS_PORCELAIN ,
@@ -689,6 +690,9 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s, line, reset, strchr(line, ' ') ? "\t" : "");break ; +case DIFF_SYMBOL_HEADER : +fprintf (o->file,"%s" , line); +break ;default :die ("BUG: unknown diff symbol" ); }
@@ -1385,7 +1389,8 @@ static void fn_out_consume(void *priv, char *line, unsigned long len) o->found_changes =1 ;if (ecbdata->header) { -fprintf (o->file,"%s" , ecbdata->header->buf); +emit_diff_symbol (o,DIFF_SYMBOL_HEADER , + ecbdata->header->buf, ecbdata->header->len,0 );strbuf_reset (ecbdata->header); ecbdata->header =NULL ; }
@@ -2519,7 +2524,8 @@ static void builtin_diff(const char *name_a,if (complete_rewrite && (textconv_one || !diff_filespec_is_binary (one)) && (textconv_two || !diff_filespec_is_binary (two))) { -fprintf (o->file,"%s" , header.buf); +emit_diff_symbol (o,DIFF_SYMBOL_HEADER , + header.buf, header.len,0 );strbuf_reset (&header);emit_rewrite_diff (name_a, name_b, one, two, textconv_one, textconv_two, o);
@@ -2529,7 +2535,8 @@ static void builtin_diff(const char *name_a, }if (o->irreversible_delete && lbl[1 ][0 ] =='/' ) { -fprintf (o->file,"%s" , header.buf); +emit_diff_symbol (o,DIFF_SYMBOL_HEADER , header.buf, + header.len,0 );strbuf_reset (&header); goto free_ab_and_return; } else if (!DIFF_OPT_TST(o, TEXT) &&
@@ -2540,10 +2547,13 @@ static void builtin_diff(const char *name_a, !DIFF_OPT_TST(o, BINARY)) {if (!oidcmp (&one->oid, &two->oid)) {if (must_show_header) -fprintf (o->file,"%s" , header.buf); +emit_diff_symbol (o,DIFF_SYMBOL_HEADER , + header.buf, header.len, +0 ); goto free_ab_and_return; } -fprintf (o->file,"%s" , header.buf); +emit_diff_symbol (o,DIFF_SYMBOL_HEADER , + header.buf, header.len,0 );fprintf (o->file,"%sBinary files %s and %s differ\n" , line_prefix, lbl[0 ], lbl[1 ]); goto free_ab_and_return;
@@ -2554,10 +2564,11 @@ static void builtin_diff(const char *name_a,if (mf1.size == mf2.size && !memcmp (mf1.ptr, mf2.ptr, mf1.size)) {if (must_show_header) -fprintf (o->file,"%s" , header.buf); +emit_diff_symbol (o,DIFF_SYMBOL_HEADER , + header.buf, header.len,0 ); goto free_ab_and_return; } -fprintf (o->file,"%s" , header.buf); +emit_diff_symbol (o,DIFF_SYMBOL_HEADER , header.buf, header.len,0 );strbuf_reset (&header);if (DIFF_OPT_TST (o,BINARY ))emit_binary_diff (o->file, &mf1, &mf2, line_prefix);
@@ -2575,7 +2586,8 @@ static void builtin_diff(const char *name_a,const struct userdiff_funcname *pe;if (must_show_header) { -fprintf (o->file,"%s" , header.buf); +emit_diff_symbol (o,DIFF_SYMBOL_HEADER , + header.buf, header.len,0 );strbuf_reset (&header); }
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH v2 13/25 diff.c: emit_diff_symbol learns about DIFF_SYMBOL_BINARY_FILES
In reply to Stefan Bellerwe could save a little bit of memory when buffering in a later mode by just passing the inner part ("%s and %s", file1, file 2), but those a just a few bytes, so instead let's reuse the implementation from DIFF_SYMBOL_HEADER and keep the whole line around.
--- diff.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-)
diff.c +15 -5
@@ -561,6 +561,7 @@ static void emit_line(struct diff_options *o, const char *set, const char *reset }enum diff_symbol { +DIFF_SYMBOL_BINARY_FILES ,DIFF_SYMBOL_HEADER ,DIFF_SYMBOL_FILEPAIR_PLUS ,DIFF_SYMBOL_FILEPAIR_MINUS ,
@@ -690,6 +691,7 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s, line, reset, strchr(line, ' ') ? "\t" : "");break ; +case DIFF_SYMBOL_BINARY_FILES :case DIFF_SYMBOL_HEADER :fprintf (o->file,"%s" , line);break ;
@@ -2542,6 +2544,7 @@ static void builtin_diff(const char *name_a, }else if (!DIFF_OPT_TST (o,TEXT ) && ( (!textconv_one &&diff_filespec_is_binary (one)) || (!textconv_two &&diff_filespec_is_binary (two)) )) { +struct strbuf sb =STRBUF_INIT ;if (!one->data && !two->data &&S_ISREG (one->mode) &&S_ISREG (two->mode) && !DIFF_OPT_TST (o,BINARY )) {
@@ -2554,8 +2557,11 @@ static void builtin_diff(const char *name_a, }emit_diff_symbol (o,DIFF_SYMBOL_HEADER , header.buf, header.len,0 ); -fprintf (o->file,"%sBinary files %s and %s differ\n" , - line_prefix, lbl[0 ], lbl[1 ]); +strbuf_addf (&sb,"%sBinary files %s and %s differ\n" , +diff_line_prefix (o), lbl[0 ], lbl[1 ]); +emit_diff_symbol (o,DIFF_SYMBOL_BINARY_FILES , + sb.buf, sb.len,0 ); +strbuf_release (&sb); goto free_ab_and_return; }if (fill_mmfile (&mf1, one) <0 ||fill_mmfile (&mf2, two) <0 )
@@ -2572,9 +2578,13 @@ static void builtin_diff(const char *name_a,strbuf_reset (&header);if (DIFF_OPT_TST (o,BINARY ))emit_binary_diff (o->file, &mf1, &mf2, line_prefix); -else -fprintf (o->file,"%sBinary files %s and %s differ\n" , - line_prefix, lbl[0 ], lbl[1 ]); +else { +strbuf_addf (&sb,"%sBinary files %s and %s differ\n" , +diff_line_prefix (o), lbl[0 ], lbl[1 ]); +emit_diff_symbol (o,DIFF_SYMBOL_BINARY_FILES , + sb.buf, sb.len,0 ); +strbuf_release (&sb); + } o->found_changes =1 ; }else {/* Crazy xdl interfaces.. */
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH v2 14/25 diff.c: emit_diff_symbol learns DIFF_SYMBOL_REWRITE_DIFF
In reply to Stefan Beller--- diff.c | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-)
diff.c +21 -14
@@ -561,6 +561,7 @@ static void emit_line(struct diff_options *o, const char *set, const char *reset }enum diff_symbol { +DIFF_SYMBOL_REWRITE_DIFF ,DIFF_SYMBOL_BINARY_FILES ,DIFF_SYMBOL_HEADER ,DIFF_SYMBOL_FILEPAIR_PLUS ,
@@ -615,7 +616,7 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s, const char *line, int len, unsigned flags) { static const char *nneof = " No newline at end of file\n"; -const char *context, *reset, *set, *meta; +const char *context, *reset, *set, *meta, *fraginfo;switch (s) {case DIFF_SYMBOL_NO_LF_EOF : context =diff_get_color_opt (o,DIFF_CONTEXT );
@@ -695,6 +696,11 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,case DIFF_SYMBOL_HEADER :fprintf (o->file,"%s" , line);break ; +case DIFF_SYMBOL_REWRITE_DIFF : + fraginfo =diff_get_color (o->use_color,DIFF_FRAGINFO ); + reset =diff_get_color_opt (o,DIFF_RESET ); +emit_line (o, fraginfo, reset, line, len); +break ;default :die ("BUG: unknown diff symbol" ); }
@@ -817,17 +823,17 @@ static void remove_tempfile(void) } } -static void print_line_count (FILE *file,int count) +static void add_line_count (struct strbuf *out,int count) {switch (count) {case 0 : -fprintf (file,"0,0" ); +strbuf_addstr (out,"0,0" );break ;case 1 : -fprintf (file,"1" ); +strbuf_addstr (out,"1" );break ;default : -fprintf (file,"1,%d" , count); +strbuf_addf (out,"1,%d" , count);break ; } }
@@ -866,14 +872,12 @@ static void emit_rewrite_diff(const char *name_a, struct diff_options *o) {int lc_a, lc_b; -const char *fraginfo =diff_get_color (o->use_color,DIFF_FRAGINFO ); -const char *reset =diff_get_color (o->use_color,DIFF_RESET );static struct strbuf a_name =STRBUF_INIT , b_name =STRBUF_INIT ;const char *a_prefix, *b_prefix;char *data_one, *data_two;size_t size_one, size_two;struct emit_callback ecbdata; -const char *line_prefix =diff_line_prefix (o); +struct strbuf out =STRBUF_INIT ;if (diff_mnemonic_prefix &&DIFF_OPT_TST (o,REVERSE_DIFF )) { a_prefix = o->b_prefix;
@@ -917,14 +921,17 @@ static void emit_rewrite_diff(const char *name_a,emit_diff_symbol (o,DIFF_SYMBOL_FILEPAIR_PLUS , b_name.buf, b_name.len,0 ); -fprintf (o->file,"%s%s@@ -" , line_prefix, fraginfo); +strbuf_addstr (&out,"@@ -" );if (!o->irreversible_delete) -print_line_count (o->file, lc_a); +add_line_count (&out, lc_a);else -fprintf (o->file,"?,?" ); -fprintf (o->file," +" ); -print_line_count (o->file, lc_b); -fprintf (o->file," @@%s\n" , reset); +strbuf_addstr (&out,"?,?" ); +strbuf_addstr (&out," +" ); +add_line_count (&out, lc_b); +strbuf_addstr (&out," @@\n" ); +emit_diff_symbol (o,DIFF_SYMBOL_REWRITE_DIFF , out.buf, out.len,0 ); +strbuf_release (&out); +if (lc_a && !o->irreversible_delete)emit_rewrite_lines (&ecbdata,'-' , data_one, size_one);if (lc_b)
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH v2 9/25 diff.c: emit_diff_symbol learns DIFF_SYMBOL_WORDS[_PORCELAIN]
In reply to Stefan Beller--- diff.c | 42 ++++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-)
diff.c +26 -16
@@ -561,6 +561,8 @@ static void emit_line(struct diff_options *o, const char *set, const char *reset }enum diff_symbol { +DIFF_SYMBOL_WORDS_PORCELAIN , +DIFF_SYMBOL_WORDS ,DIFF_SYMBOL_CONTEXT ,DIFF_SYMBOL_PLUS ,DIFF_SYMBOL_MINUS ,
@@ -649,6 +651,26 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,emit_line_ws_markup (o, set, reset, line, len,'-' , flags &DIFF_SYMBOL_CONTENT_WS_MASK ,0 );break ; +case DIFF_SYMBOL_WORDS_PORCELAIN : + context =diff_get_color_opt (o,DIFF_CONTEXT ); + reset =diff_get_color_opt (o,DIFF_RESET ); +emit_line (o, context, reset, line, len); +fputs ("~\n" , o->file); +break ; +case DIFF_SYMBOL_WORDS : + context =diff_get_color_opt (o,DIFF_CONTEXT ); + reset =diff_get_color_opt (o,DIFF_RESET ); +/* +* Skip the prefix character, if any. With +* diff_suppress_blank_empty, there may be +* none. +*/ +if (line[0 ] !='\n' ) { + line++; + len--; + } +emit_line (o, context, reset, line, len); +break ;default :die ("BUG: unknown diff symbol" ); }
@@ -1342,7 +1364,6 @@ static void fn_out_consume(void *priv, char *line, unsigned long len) {struct emit_callback *ecbdata = priv;const char *meta =diff_get_color (ecbdata->color_diff,DIFF_METAINFO ); -const char *context =diff_get_color (ecbdata->color_diff,DIFF_CONTEXT );const char *reset =diff_get_color (ecbdata->color_diff,DIFF_RESET );struct diff_options *o = ecbdata->opt;const char *line_prefix =diff_line_prefix (o);
@@ -1384,6 +1405,9 @@ static void fn_out_consume(void *priv, char *line, unsigned long len) }if (ecbdata->diff_words) { +enum diff_symbol s = + ecbdata->diff_words->type ==DIFF_WORDS_PORCELAIN ? +DIFF_SYMBOL_WORDS_PORCELAIN :DIFF_SYMBOL_WORDS ;if (line[0 ] =='-' ) {diff_words_append (line, len, &ecbdata->diff_words->minus);
@@ -1403,21 +1427,7 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)return ; }diff_words_flush (ecbdata); -if (ecbdata->diff_words->type ==DIFF_WORDS_PORCELAIN ) { -emit_line (o, context, reset, line, len); -fputs ("~\n" , o->file); - }else { -/* -* Skip the prefix character, if any. With -* diff_suppress_blank_empty, there may be -* none. -*/ -if (line[0 ] !='\n' ) { - line++; - len--; - } -emit_line (o, context, reset, line, len); - } +emit_diff_symbol (o, s, line, len,0 );return ; }
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH v2 20/25 diff.c: emit_diff_symbol learns about DIFF_SYMBOL_SUMMARY
In reply to Stefan Beller--- diff.c | 71 ++++++++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 41 insertions(+), 30 deletions(-)
diff.c +41 -30
@@ -572,6 +572,7 @@ enum diff_symbol {DIFF_SYMBOL_STATS_LINE ,DIFF_SYMBOL_WORD_DIFF ,DIFF_SYMBOL_STAT_SEP , +DIFF_SYMBOL_SUMMARY ,DIFF_SYMBOL_SUBMODULE_ADD ,DIFF_SYMBOL_SUBMODULE_DEL , DIFF_SYMBOL_SUBMODULE_UNTRACKED,
@@ -648,6 +649,7 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,case DIFF_SYMBOL_SUBMODULE_ERROR :case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH :case DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES : +case DIFF_SYMBOL_SUMMARY :case DIFF_SYMBOL_STATS_LINE :case DIFF_SYMBOL_BINARY_DIFF_BODY :case DIFF_SYMBOL_CONTEXT_FRAGINFO :
@@ -4717,67 +4719,76 @@ static void flush_one_pair(struct diff_filepair *p, struct diff_options *opt) } } -static void show_file_mode_name (FILE *file,const char *newdelete,struct diff_filespec *fs) +static void show_file_mode_name (struct diff_options *opt,const char *newdelete,struct diff_filespec *fs) { +struct strbuf sb =STRBUF_INIT ;if (fs->mode) -fprintf (file," %s mode %06o " , newdelete, fs->mode); +strbuf_addf (&sb," %s mode %06o " , newdelete, fs->mode);else -fprintf (file," %s " , newdelete); -write_name_quoted (fs->path, file,'\n' ); -} +strbuf_addf (&sb," %s " , newdelete); +quote_c_style (fs->path, &sb,NULL ,0 ); +strbuf_addch (&sb,'\n' ); +emit_diff_symbol (opt,DIFF_SYMBOL_SUMMARY , + sb.buf, sb.len,0 ); +strbuf_release (&sb); +} -static void show_mode_change (FILE *file,struct diff_filepair *p,int show_name, -const char *line_prefix) +static void show_mode_change (struct diff_options *opt,struct diff_filepair *p, +int show_name) {if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) { -fprintf (file,"%s mode change %06o => %06o%c" , line_prefix, p->one->mode, - p->two->mode, show_name ?' ' :'\n' ); +struct strbuf sb =STRBUF_INIT ; +strbuf_addf (&sb," mode change %06o => %06o" , + p->one->mode, p->two->mode);if (show_name) { -write_name_quoted (p->two->path, file,'\n' ); +strbuf_addch (&sb,' ' ); +quote_c_style (p->two->path, &sb,NULL ,0 ); } +emit_diff_symbol (opt,DIFF_SYMBOL_SUMMARY , + sb.buf, sb.len,0 ); +strbuf_release (&sb); } } -static void show_rename_copy (FILE *file,const char *renamecopy,struct diff_filepair *p, -const char *line_prefix) +static void show_rename_copy (struct diff_options *opt,const char *renamecopy, +struct diff_filepair *p) { +struct strbuf sb =STRBUF_INIT ;char *names =pprint_rename (p->one->path, p->two->path); - -fprintf (file," %s %s (%d%%)\n" , renamecopy, names,similarity_index (p)); +strbuf_addf (&sb," %s %s (%d%%)\n" , + renamecopy, names,similarity_index (p));free (names); -show_mode_change (file, p,0 , line_prefix); +emit_diff_symbol (opt,DIFF_SYMBOL_SUMMARY , + sb.buf, sb.len,0 ); +show_mode_change (opt, p,0 ); }static void diff_summary (struct diff_options *opt,struct diff_filepair *p) { -FILE *file = opt->file; -const char *line_prefix =diff_line_prefix (opt); -switch (p->status) {case DIFF_STATUS_DELETED : -fputs (line_prefix, file); -show_file_mode_name (file,"delete" , p->one); +show_file_mode_name (opt,"delete" , p->one);break ;case DIFF_STATUS_ADDED : -fputs (line_prefix, file); -show_file_mode_name (file,"create" , p->two); +show_file_mode_name (opt,"create" , p->two);break ;case DIFF_STATUS_COPIED : -fputs (line_prefix, file); -show_rename_copy (file,"copy" , p, line_prefix); +show_rename_copy (opt,"copy" , p);break ;case DIFF_STATUS_RENAMED : -fputs (line_prefix, file); -show_rename_copy (file,"rename" , p, line_prefix); +show_rename_copy (opt,"rename" , p);break ;default :if (p->score) { -fprintf (file,"%s rewrite " , line_prefix); -write_name_quoted (p->two->path, file,' ' ); -fprintf (file,"(%d%%)\n" ,similarity_index (p)); +struct strbuf sb =STRBUF_INIT ; +strbuf_addstr (&sb," rewrite " ); +quote_c_style (p->two->path, &sb,NULL ,0 ); +strbuf_addf (&sb," (%d%%)\n" ,similarity_index (p)); +emit_diff_symbol (opt,DIFF_SYMBOL_SUMMARY , + sb.buf, sb.len,0 ); } -show_mode_change (file, p, !p->score, line_prefix); +show_mode_change (opt, p, !p->score);break ; } }
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH v2 18/25 diff.c: convert word diffing to use emit_diff_symbol
In reply to Stefan BellerThe word diffing is not line oriented and would need some serious effort to be transformed into a line oriented approach, so just go with a symbol DIFF_SYMBOL_WORD_DIFF that is a partial line.
--- diff.c | 79 ++++++++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 46 insertions(+), 33 deletions(-)
diff.c +46 -33
@@ -570,6 +570,7 @@ enum diff_symbol {DIFF_SYMBOL_STATS_SUMMARY_ABBREV ,DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES ,DIFF_SYMBOL_STATS_LINE , +DIFF_SYMBOL_WORD_DIFF ,DIFF_SYMBOL_SUBMODULE_ADD ,DIFF_SYMBOL_SUBMODULE_DEL , DIFF_SYMBOL_SUBMODULE_UNTRACKED,
@@ -762,6 +763,9 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,case DIFF_SYMBOL_STATS_SUMMARY_ABBREV :emit_line (o,"" ,"" ," ...\n" ,strlen (" ...\n" ));break ; +case DIFF_SYMBOL_WORD_DIFF : +fprintf (o->file,"%.*s" , len, line); +break ;default :die ("BUG: unknown diff symbol" ); }
@@ -1092,37 +1096,49 @@ struct diff_words_data {struct diff_words_style *style; }; -static int fn_out_diff_words_write_helper (FILE *fp, +static int fn_out_diff_words_write_helper (struct diff_options *o,struct diff_words_style_elem *st_el,const char *newline, -size_t count,const char *buf, -const char *line_prefix) +size_t count,const char *buf) {int print =0 ; +struct strbuf sb =STRBUF_INIT ;while (count) {char *p =memchr (buf,'\n' , count);if (print) -fputs (line_prefix, fp); +strbuf_addstr (&sb,diff_line_prefix (o)); +if (p != buf) { -if (st_el->color &&fputs (st_el->color, fp) <0 ) -return -1 ; -if (fputs (st_el->prefix, fp) <0 || -fwrite (buf, p ? p - buf : count,1 , fp) !=1 || -fputs (st_el->suffix, fp) <0 ) -return -1 ; -if (st_el->color && *st_el->color - &&fputs (GIT_COLOR_RESET , fp) <0 ) -return -1 ; +const char *reset = st_el->color && *st_el->color ? +GIT_COLOR_RESET :NULL ; +if (st_el->color && *st_el->color) +strbuf_addstr (&sb, st_el->color); +strbuf_addstr (&sb, st_el->prefix); +strbuf_add (&sb, buf, p ? p - buf : count); +strbuf_addstr (&sb, st_el->suffix); +if (reset) +strbuf_addstr (&sb, reset); }if (!p) -return 0 ; -if (fputs (newline, fp) <0 ) -return -1 ; + goto out; + +strbuf_addstr (&sb, newline); count -= p +1 - buf; buf = p +1 ; print =1 ; +if (count) { +emit_diff_symbol (o,DIFF_SYMBOL_WORD_DIFF , + sb.buf, sb.len,0 ); +strbuf_reset (&sb); + } } + +out: +if (sb.len) +emit_diff_symbol (o,DIFF_SYMBOL_WORD_DIFF , + sb.buf, sb.len,0 ); +strbuf_release (&sb);return 0 ; }
@@ -1204,24 +1220,20 @@ static void fn_out_diff_words_aux(void *priv, char *line, unsigned long len)fputs (line_prefix, diff_words->opt->file); }if (diff_words->current_plus != plus_begin) { -fn_out_diff_words_write_helper (diff_words->opt->file, +fn_out_diff_words_write_helper (diff_words->opt, &style->ctx, style->newline, plus_begin - diff_words->current_plus, - diff_words->current_plus, line_prefix); -if (*(plus_begin -1 ) =='\n' ) -fputs (line_prefix, diff_words->opt->file); + diff_words->current_plus); }if (minus_begin != minus_end) { -fn_out_diff_words_write_helper (diff_words->opt->file, +fn_out_diff_words_write_helper (diff_words->opt, &style->old, style->newline, - minus_end - minus_begin, minus_begin, - line_prefix); + minus_end - minus_begin, minus_begin); }if (plus_begin != plus_end) { -fn_out_diff_words_write_helper (diff_words->opt->file, +fn_out_diff_words_write_helper (diff_words->opt, &style->new, style->newline, - plus_end - plus_begin, plus_begin, - line_prefix); + plus_end - plus_begin, plus_begin); } diff_words->current_plus = plus_end;
@@ -1315,11 +1327,12 @@ static void diff_words_show(struct diff_words_data *diff_words)/* special case: only removal */ if (!diff_words->plus.text.size) { -fputs (line_prefix, diff_words->opt->file); -fn_out_diff_words_write_helper (diff_words->opt->file, +emit_diff_symbol (diff_words->opt,DIFF_SYMBOL_WORD_DIFF , + line_prefix,strlen (line_prefix),0 ); +fn_out_diff_words_write_helper (diff_words->opt, &style->old, style->newline, diff_words->minus.text.size, - diff_words->minus.text.ptr, line_prefix); + diff_words->minus.text.ptr); diff_words->minus.text.size =0 ;return ; }
@@ -1342,12 +1355,12 @@ static void diff_words_show(struct diff_words_data *diff_words)if (diff_words->current_plus != diff_words->plus.text.ptr + diff_words->plus.text.size) {if (color_words_output_graph_prefix (diff_words)) -fputs (line_prefix, diff_words->opt->file); -fn_out_diff_words_write_helper (diff_words->opt->file, +emit_diff_symbol (diff_words->opt,DIFF_SYMBOL_WORD_DIFF , + line_prefix,strlen (line_prefix),0 ); +fn_out_diff_words_write_helper (diff_words->opt, &style->ctx, style->newline, diff_words->plus.text.ptr + diff_words->plus.text.size - - diff_words->current_plus, diff_words->current_plus, - line_prefix); + - diff_words->current_plus, diff_words->current_plus); } diff_words->minus.text.size = diff_words->plus.text.size =0 ; }
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH v2 17/25 diff.c: convert show_stats to use emit_diff_symbol
In reply to Stefan BellerWe call print_stat_summary from builtin/apply, so we still need the version with a file pointer, so introduce print_stat_summary_0 that uses emit_string machinery and keep print_stat_summary with the same arguments around.
--- diff.c | 114 +++++++++++++++++++++++++++++++++++++++++------------------------ diff.h | 4 +-- 2 files changed, 74 insertions(+), 44 deletions(-)
diff.c +72 -42
@@ -566,6 +566,10 @@ enum diff_symbol {DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL ,DIFF_SYMBOL_BINARY_DIFF_BODY ,DIFF_SYMBOL_BINARY_DIFF_FOOTER , +DIFF_SYMBOL_STATS_SUMMARY_NO_FILES , +DIFF_SYMBOL_STATS_SUMMARY_ABBREV , +DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES , +DIFF_SYMBOL_STATS_LINE ,DIFF_SYMBOL_SUBMODULE_ADD ,DIFF_SYMBOL_SUBMODULE_DEL , DIFF_SYMBOL_SUBMODULE_UNTRACKED,
@@ -629,6 +633,7 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s, {static const char *nneof =" No newline at end of file\n" ;const char *context, *reset, *set, *meta, *fraginfo; +struct strbuf sb =STRBUF_INIT ;switch (s) {case DIFF_SYMBOL_NO_LF_EOF : context =diff_get_color_opt (o,DIFF_CONTEXT );
@@ -640,6 +645,8 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,case DIFF_SYMBOL_SUBMODULE_HEADER :case DIFF_SYMBOL_SUBMODULE_ERROR :case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH : +case DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES : +case DIFF_SYMBOL_STATS_LINE :case DIFF_SYMBOL_BINARY_DIFF_BODY :case DIFF_SYMBOL_CONTEXT_FRAGINFO :emit_line (o,"" ,"" , line, len);
@@ -748,9 +755,17 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,fprintf (o->file,"%sSubmodule %s contains modified content\n" ,diff_line_prefix (o), line);break ; +case DIFF_SYMBOL_STATS_SUMMARY_NO_FILES : +emit_line (o,"" ,"" ," 0 files changed\n" , +strlen (" 0 files changed\n" )); +break ; +case DIFF_SYMBOL_STATS_SUMMARY_ABBREV : +emit_line (o,"" ,"" ," ...\n" ,strlen (" ...\n" )); +break ;default :die ("BUG: unknown diff symbol" ); } +strbuf_release (&sb); }void diff_emit_submodule_del (struct diff_options *o,const char *line)
@@ -1705,20 +1720,14 @@ static int scale_linear(int it, int width, int max_change)return 1 + (it * (width -1 ) / max_change); } -static void show_name (FILE *file, -const char *prefix,const char *name,int len) -{ -fprintf (file," %s%-*s |" , prefix, len, name); -} - -static void show_graph (FILE *file,char ch,int cnt,const char *set,const char *reset) +static void show_graph (struct strbuf *out,char ch,int cnt, +const char *set,const char *reset) {if (cnt <=0 )return ; -fprintf (file,"%s" , set); -while (cnt--) -putc (ch, file); -fprintf (file,"%s" , reset); +strbuf_addstr (out, set); +strbuf_addchars (out, ch, cnt); +strbuf_addstr (out, reset); }static void fill_print_name (struct diffstat_file *file)
@@ -1742,14 +1751,16 @@ static void fill_print_name(struct diffstat_file *file) file->print_name = pname; } -int print_stat_summary (FILE *fp,int files,int insertions,int deletions) +static void print_stat_summary_inserts_deletes (struct diff_options *options, +int files,int insertions,int deletions) {struct strbuf sb =STRBUF_INIT ; -int ret;if (!files) {assert (insertions ==0 && deletions ==0 ); -return fprintf (fp,"%s\n" ," 0 files changed" ); +emit_diff_symbol (options,DIFF_SYMBOL_STATS_SUMMARY_NO_FILES , +NULL ,0 ,0 ); +return ; } strbuf_addf(&sb,
@@ -1776,9 +1787,19 @@ int print_stat_summary(FILE *fp, int files, int insertions, int deletions) deletions); }strbuf_addch (&sb,'\n' ); - ret =fputs (sb.buf, fp); +emit_diff_symbol (options,DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES , + sb.buf, sb.len,0 );strbuf_release (&sb); -return ret; +} + +void print_stat_summary (FILE *fp,int files, +int insertions,int deletions) +{ +struct diff_options o; +memset (&o,0 ,sizeof (o)); + o.file = fp; + +print_stat_summary_inserts_deletes (&o, files, insertions, deletions); }static void show_stats (struct diffstat_t *data,struct diff_options *options)
@@ -1788,13 +1809,13 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)int total_files = data->nr, count;int width, name_width, graph_width, number_width =0 , bin_width =0 ;const char *reset, *add_c, *del_c; -const char *line_prefix ="" ;int extra_shown =0 ; +const char *line_prefix =diff_line_prefix (options); +struct strbuf out =STRBUF_INIT ;if (data->nr ==0 )return ; - line_prefix =diff_line_prefix (options); count = options->stat_count ? options->stat_count : data->nr; reset =diff_get_color_opt (options,DIFF_RESET );
@@ -1948,26 +1969,32 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options) }if (file->is_binary) { -fprintf (options->file,"%s" , line_prefix); -show_name (options->file, prefix, name, len); -fprintf (options->file," %*s" , number_width,"Bin" ); +strbuf_addf (&out," %s%-*s |" , prefix, len, name); +strbuf_addf (&out," %*s" , number_width,"Bin" );if (!added && !deleted) { -putc ('\n' , options->file); +strbuf_addch (&out,'\n' ); +emit_diff_symbol (options,DIFF_SYMBOL_STATS_LINE , + out.buf, out.len,0 ); +strbuf_reset (&out);continue ; } -fprintf (options->file," %s%" PRIuMAX"%s" , +strbuf_addf (&out," %s%" PRIuMAX"%s" , del_c, deleted, reset); -fprintf (options->file," -> " ); -fprintf (options->file,"%s%" PRIuMAX"%s" , +strbuf_addstr (&out," -> " ); +strbuf_addf (&out,"%s%" PRIuMAX"%s" , add_c, added, reset); -fprintf (options->file," bytes" ); -fprintf (options->file,"\n" ); +strbuf_addstr (&out," bytes\n" ); +emit_diff_symbol (options,DIFF_SYMBOL_STATS_LINE , + out.buf, out.len,0 ); +strbuf_reset (&out);continue ; }else if (file->is_unmerged) { -fprintf (options->file,"%s" , line_prefix); -show_name (options->file, prefix, name, len); -fprintf (options->file," Unmerged\n" ); +strbuf_addf (&out," %s%-*s |" , prefix, len, name); +strbuf_addstr (&out," Unmerged\n" ); +emit_diff_symbol (options,DIFF_SYMBOL_STATS_LINE , + out.buf, out.len,0 ); +strbuf_reset (&out);continue ; }
@@ -1990,14 +2017,16 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options) add = total - del; } } -fprintf (options->file,"%s" , line_prefix); -show_name (options->file, prefix, name, len); -fprintf (options->file," %*" PRIuMAX"%s" , +strbuf_addf (&out," %s%-*s |" , prefix, len, name); +strbuf_addf (&out," %*" PRIuMAX"%s" , number_width, added + deleted, added + deleted ?" " :"" ); -show_graph (options->file,'+' , add, add_c, reset); -show_graph (options->file,'-' , del, del_c, reset); -fprintf (options->file,"\n" ); +show_graph (&out,'+' , add, add_c, reset); +show_graph (&out,'-' , del, del_c, reset); +strbuf_addch (&out,'\n' ); +emit_diff_symbol (options,DIFF_SYMBOL_STATS_LINE , + out.buf, out.len,0 ); +strbuf_reset (&out); }for (i =0 ; i < data->nr; i++) {
@@ -2018,11 +2047,13 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)if (i < count)continue ;if (!extra_shown) -fprintf (options->file,"%s ...\n" , line_prefix); +emit_diff_symbol (options, +DIFF_SYMBOL_STATS_SUMMARY_ABBREV , +NULL ,0 ,0 ); extra_shown =1 ; } -fprintf (options->file,"%s" , line_prefix); -print_stat_summary (options->file, total_files, adds, dels); + +print_stat_summary_inserts_deletes (options, total_files, adds, dels); }static void show_shortstats (struct diffstat_t *data,struct diff_options *options)
@@ -2034,7 +2065,7 @@ static void show_shortstats(struct diffstat_t *data, struct diff_options *optionfor (i =0 ; i < data->nr; i++) {int added = data->files[i]->added; -int deleted= data->files[i]->deleted; +int deleted = data->files[i]->deleted;if (data->files[i]->is_unmerged || (!data->files[i]->is_interesting && (added + deleted ==0 ))) {
@@ -2044,8 +2075,7 @@ static void show_shortstats(struct diffstat_t *data, struct diff_options *option dels += deleted; } } -fprintf (options->file,"%s" ,diff_line_prefix (options)); -print_stat_summary (options->file, total_files, adds, dels); +print_stat_summary_inserts_deletes (options, total_files, adds, dels); }static void show_numstat (struct diffstat_t *data,struct diff_options *options)
diff.h +2 -2
@@ -405,8 +405,8 @@ extern int parse_rename_score(const char **cp_p);extern long parse_algorithm_value (const char *value); -extern int print_stat_summary (FILE *fp,int files, -int insertions,int deletions); +extern void print_stat_summary (FILE *fp,int files, +int insertions,int deletions);extern void setup_diff_pager (struct diff_options *);#endif /* DIFF_H */
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH v2 15/25 submodule.c: migrate diff output to use emit_diff_symbol
In reply to Stefan BellerAs the submodule process is no longer attached to the same file pointer 'o->file' as the superprojects process, there is a different result in color.c::check_auto_color. That is why we need to pass coloring explicitly, such that the submodule coloring decision will be made by the child process processing the submodule. Only DIFF_SYMBOL_SUBMODULE_PIPETHROUGH contains color, the other symbols are for embedding the submodule output into the superprojects output.
Remove the colors from the function signatures, as all the coloring decisions will be made either inside the child process or the final emit_diff_symbol, but not in the functions driving the submodule diff.
--- diff.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++--------- diff.h | 9 +++++++ submodule.c | 84 +++++++++++++++++++++++++++---------------------------------- submodule.h | 13 +++------- 4 files changed, 121 insertions(+), 68 deletions(-)
diff.c +71 -12
@@ -561,6 +561,13 @@ static void emit_line(struct diff_options *o, const char *set, const char *reset }enum diff_symbol { +DIFF_SYMBOL_SUBMODULE_ADD , +DIFF_SYMBOL_SUBMODULE_DEL , +DIFF_SYMBOL_SUBMODULE_UNTRACKED , +DIFF_SYMBOL_SUBMODULE_MODIFIED , +DIFF_SYMBOL_SUBMODULE_HEADER , +DIFF_SYMBOL_SUBMODULE_ERROR , +DIFF_SYMBOL_SUBMODULE_PIPETHROUGH ,DIFF_SYMBOL_REWRITE_DIFF ,DIFF_SYMBOL_BINARY_FILES ,DIFF_SYMBOL_HEADER ,
@@ -625,6 +632,9 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,emit_line_0 (o, context, reset,'\\' , nneof,strlen (nneof));break ; +case DIFF_SYMBOL_SUBMODULE_HEADER : +case DIFF_SYMBOL_SUBMODULE_ERROR : +case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH :case DIFF_SYMBOL_CONTEXT_FRAGINFO :emit_line (o,"" ,"" , line, len);break ;
@@ -701,11 +711,68 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s, reset =diff_get_color_opt (o,DIFF_RESET );emit_line (o, fraginfo, reset, line, len);break ; +case DIFF_SYMBOL_SUBMODULE_ADD : + set =diff_get_color_opt (o,DIFF_FILE_NEW ); + reset =diff_get_color_opt (o,DIFF_RESET ); +emit_line (o, set, reset, line, len); +break ; +case DIFF_SYMBOL_SUBMODULE_DEL : + set =diff_get_color_opt (o,DIFF_FILE_OLD ); + reset =diff_get_color_opt (o,DIFF_RESET ); +emit_line (o, set, reset, line, len); +break ; +case DIFF_SYMBOL_SUBMODULE_UNTRACKED : +fprintf (o->file,"%sSubmodule %s contains untracked content\n" , +diff_line_prefix (o), line); +break ; +case DIFF_SYMBOL_SUBMODULE_MODIFIED : +fprintf (o->file,"%sSubmodule %s contains modified content\n" , +diff_line_prefix (o), line); +break ;default :die ("BUG: unknown diff symbol" ); } } +void diff_emit_submodule_del (struct diff_options *o,const char *line) +{ +emit_diff_symbol (o,DIFF_SYMBOL_SUBMODULE_DEL , line,strlen (line),0 ); +} + +void diff_emit_submodule_add (struct diff_options *o,const char *line) +{ +emit_diff_symbol (o,DIFF_SYMBOL_SUBMODULE_ADD , line,strlen (line),0 ); +} + +void diff_emit_submodule_untracked (struct diff_options *o,const char *path) +{ +emit_diff_symbol (o,DIFF_SYMBOL_SUBMODULE_UNTRACKED , + path,strlen (path),0 ); +} + +void diff_emit_submodule_modified (struct diff_options *o,const char *path) +{ +emit_diff_symbol (o,DIFF_SYMBOL_SUBMODULE_MODIFIED , + path,strlen (path),0 ); +} + +void diff_emit_submodule_header (struct diff_options *o,const char *header) +{ +emit_diff_symbol (o,DIFF_SYMBOL_SUBMODULE_HEADER , + header,strlen (header),0 ); +} + +void diff_emit_submodule_error (struct diff_options *o,const char *err) +{ +emit_diff_symbol (o,DIFF_SYMBOL_SUBMODULE_ERROR , err,strlen (err),0 ); +} + +void diff_emit_submodule_pipethrough (struct diff_options *o, +const char *line,int len) +{ +emit_diff_symbol (o,DIFF_SYMBOL_SUBMODULE_PIPETHROUGH , line, len,0 ); +} +static int new_blank_line_at_eof (struct emit_callback *ecbdata,const char *line,int len) { if (!((ecbdata->ws_rule & WS_BLANK_AT_EOF) &&
@@ -2467,24 +2534,16 @@ static void builtin_diff(const char *name_a,if (o->submodule_format ==DIFF_SUBMODULE_LOG && (!one->mode ||S_ISGITLINK (one->mode)) && (!two->mode ||S_ISGITLINK (two->mode))) { -const char *del =diff_get_color_opt (o,DIFF_FILE_OLD ); -const char *add =diff_get_color_opt (o,DIFF_FILE_NEW ); -show_submodule_summary (o->file, one->path ? one->path : two->path, - line_prefix, +show_submodule_summary (o, one->path ? one->path : two->path, &one->oid, &two->oid, - two->dirty_submodule, - meta, del, add, reset); + two->dirty_submodule);return ; }else if (o->submodule_format ==DIFF_SUBMODULE_INLINE_DIFF && (!one->mode ||S_ISGITLINK (one->mode)) && (!two->mode ||S_ISGITLINK (two->mode))) { -const char *del =diff_get_color_opt (o,DIFF_FILE_OLD ); -const char *add =diff_get_color_opt (o,DIFF_FILE_NEW ); -show_submodule_inline_diff (o->file, one->path ? one->path : two->path, - line_prefix, +show_submodule_inline_diff (o, one->path ? one->path : two->path, &one->oid, &two->oid, - two->dirty_submodule, - meta, del, add, reset, o); + two->dirty_submodule);return ; }
diff.h +9 -0
@@ -188,6 +188,15 @@ struct diff_options {int diff_path_counter; }; +void diff_emit_submodule_del (struct diff_options *o,const char *line); +void diff_emit_submodule_add (struct diff_options *o,const char *line); +void diff_emit_submodule_untracked (struct diff_options *o,const char *path); +void diff_emit_submodule_modified (struct diff_options *o,const char *path); +void diff_emit_submodule_header (struct diff_options *o,const char *header); +void diff_emit_submodule_error (struct diff_options *o,const char *err); +void diff_emit_submodule_pipethrough (struct diff_options *o, +const char *line,int len); +enum color_diff {DIFF_RESET =0 ,DIFF_CONTEXT =1 ,
submodule.c +37 -47
176 lines not on this page · raw message
submodule.h +4 -9
@@ -63,17 +63,12 @@ extern int parse_submodule_update_strategy(const char *value, struct submodule_update_strategy *dst);extern const char *submodule_strategy_to_string (const struct submodule_update_strategy *s);extern void handle_ignore_submodules_arg (struct diff_options *,const char *); -extern void show_submodule_summary (FILE *f,const char *path, -const char *line_prefix, +extern void show_submodule_summary (struct diff_options *o,const char *path,struct object_id *one,struct object_id *two, -unsigned dirty_submodule,const char *meta, -const char *del,const char *add,const char *reset); -extern void show_submodule_inline_diff (FILE *f,const char *path, -const char *line_prefix, +unsigned dirty_submodule); +extern void show_submodule_inline_diff (struct diff_options *o,const char *path,struct object_id *one,struct object_id *two, -unsigned dirty_submodule,const char *meta, -const char *del,const char *add,const char *reset, -const struct diff_options *opt); +unsigned dirty_submodule);extern void set_config_fetch_recurse_submodules (int value);/* Check if we want to update any submodule.*/ extern int should_update_submodules (void );
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH v2 21/25 diff.c: buffer all output if asked to
In reply to Stefan BellerIntroduce a new option 'emitted_symbols' in the struct diff_options which controls whether all output is buffered up until all output is available. It is set internally in diff.c when necessary.
We'll have a new struct 'emitted_string' in diff.c which will be used to buffer each line. The emitted_string will duplicate the memory of the line to buffer as that is easiest to reason about for now. In a future patch we may want to decrease the memory usage by not duplicating all output for buffering but rather we may want to store offsets into the file or in case of hunk descriptions such as the similarity score, we could just store the relevant number and reproduce the text later on.
This approach was chosen as a first step because it is quite simple compared to the alternative with less memory footprint.
emit_diff_symbol factors out the emission part and depending on the diff_options->emitted_symbols the emission will be performed directly when calling emit_diff_symbol or after the whole process is done, i.e. by buffering we have add the possibility for a second pass over the whole output before doing the actual output.
In 6440d34 (2012-03-14, diff: tweak a _copy_ of diff_options with word-diff) we introduced a duplicate diff options struct for word emissions as we may have different regex settings in there. When buffering the output, we need to operate on just one buffer, so we have to copy back the emissions of the word buffer into the main buffer.
Unconditionally enable output via buffer in this patch as it yields a great opportunity for testing, i.e. all the diff tests from the test suite pass without having reordering issues (i.e. only parts of the output got buffered, and we forgot to buffer other parts). The test suite passes, which gives confidence that we converted all functions to use emit_string for output.
--- diff.c | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- diff.h | 2 ++ 2 files changed, 109 insertions(+), 2 deletions(-)
diff.c +107 -2
@@ -605,6 +605,47 @@ enum diff_symbol {#define DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF (1<<16)#define DIFF_SYMBOL_CONTENT_WS_MASK (WSEH_NEW | WSEH_OLD | WSEH_CONTEXT | WS_RULE_MASK) +/* +* This struct is used when we need to buffer the output of the diff output. +* +* NEEDSWORK: Instead of storing a copy of the line, add an offset pointer +* into the pre/post image file. This pointer could be a union with the +* line pointer. By storing an offset into the file instead of the literal line, +* we can decrease the memory footprint for the buffered output. At first we +* may want to only have indirection for the content lines, but we could also +* enhance the state for emitting prefabricated lines, e.g. the similarity +* score line or hunk/file headers would only need to store a number or path +* and then the output can be constructed later on depending on state. +*/ +struct emitted_diff_symbol { +const char *line; +int len; +int flags; +enum diff_symbol s; +}; +#define EMITTED_DIFF_SYMBOL_INIT {NULL} + +struct emitted_diff_symbols { +struct emitted_diff_symbol *buf; +int nr, alloc; +}; +#define EMITTED_DIFF_SYMBOLS_INIT {NULL, 0, 0} + +static void append_emitted_diff_symbol (struct diff_options *o, +struct emitted_diff_symbol *e) +{ +struct emitted_diff_symbol *f; + +ALLOC_GROW (o->emitted_symbols->buf, + o->emitted_symbols->nr +1 , + o->emitted_symbols->alloc); + f = &o->emitted_symbols->buf[o->emitted_symbols->nr++]; + +memcpy (f, e,sizeof (struct emitted_diff_symbol )); + f->line = e->line ?xmemdupz (e->line, e->len) :NULL ; +} + + static void emit_line_ws_markup(struct diff_options *o, const char *set, const char *reset, const char *line, int len, char sign,
@@ -631,12 +672,18 @@ static void emit_line_ws_markup(struct diff_options *o, } } -static void emit_diff_symbol (struct diff_options *o,enum diff_symbol s, -const char *line,int len,unsigned flags) +static void emit_diff_symbol_from_struct (struct diff_options *o, +struct emitted_diff_symbol *eds) {static const char *nneof =" No newline at end of file\n" ;const char *context, *reset, *set, *meta, *fraginfo;struct strbuf sb =STRBUF_INIT ; + +enum diff_symbol s = eds->s; +const char *line = eds->line; +int len = eds->len; +unsigned flags = eds->flags; +switch (s) {case DIFF_SYMBOL_NO_LF_EOF : context =diff_get_color_opt (o,DIFF_CONTEXT );
@@ -778,6 +825,17 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,strbuf_release (&sb); } +static void emit_diff_symbol (struct diff_options *o,enum diff_symbol s, +const char *line,int len,unsigned flags) +{ +struct emitted_diff_symbol e = {line, len, flags, s}; + +if (o->emitted_symbols) +append_emitted_diff_symbol (o, &e); +else +emit_diff_symbol_from_struct (o, &e); +} +void diff_emit_submodule_del (struct diff_options *o,const char *line) {emit_diff_symbol (o,DIFF_SYMBOL_SUBMODULE_DEL , line,strlen (line),0 );
@@ -1374,9 +1432,29 @@ static void diff_words_show(struct diff_words_data *diff_words)/* In "color-words" mode, show word-diff of words accumulated in the buffer */ static void diff_words_flush (struct emit_callback *ecbdata) { +struct diff_options *wo = ecbdata->diff_words->opt; +if (ecbdata->diff_words->minus.text.size || ecbdata->diff_words->plus.text.size)diff_words_show (ecbdata->diff_words); + +if (wo->emitted_symbols) { +struct diff_options *o = ecbdata->opt; +struct emitted_diff_symbols *wol = wo->emitted_symbols; +int i; + +/* +* NEEDSWORK: +* Instead of appending each, concat all words to a line? +*/ +for (i =0 ; i < wol->nr; i++) +append_emitted_diff_symbol (o, &wol->buf[i]); + +for (i =0 ; i < wol->nr; i++) +free ((void *)wol->buf[i].line); + + wol->nr =0 ; + } }static void diff_filespec_load_driver (struct diff_filespec *one)
@@ -1412,6 +1490,11 @@ static void init_diff_words_data(struct emit_callback *ecbdata,xcalloc (1 ,sizeof (struct diff_words_data )); ecbdata->diff_words->type = o->word_diff; ecbdata->diff_words->opt = o; + +if (orig_opts->emitted_symbols) + o->emitted_symbols = +xcalloc (1 ,sizeof (struct emitted_diff_symbols )); +if (!o->word_regex) o->word_regex =userdiff_word_regex (one);if (!o->word_regex)
@@ -1446,6 +1529,7 @@ static void free_diff_words_data(struct emit_callback *ecbdata) {if (ecbdata->diff_words) {diff_words_flush (ecbdata); +free (ecbdata->diff_words->opt->emitted_symbols);free (ecbdata->diff_words->opt);free (ecbdata->diff_words->minus.text.ptr);free (ecbdata->diff_words->minus.orig);
@@ -4996,16 +5080,37 @@ void diff_warn_rename_limit(const char *varname, int needed, int degraded_cc)static void diff_flush_patch_all_file_pairs (struct diff_options *o) {int i; +static struct emitted_diff_symbols esm =EMITTED_DIFF_SYMBOLS_INIT ;struct diff_queue_struct *q = &diff_queued_diff;if (WSEH_NEW &WS_RULE_MASK )die ("BUG: WS rules bit mask overlaps with diff symbol flags" ); +/* +* For testing purposes we want to make sure the diff machinery +* works completely with the buffer. If there is anything emitted +* outside the emit_string, then the order is screwed +* up and the tests will fail. +* +* TODO (later in this series): +* We'll unset this pointer in a later patch. +*/ + o->emitted_symbols = &esm; +for (i =0 ; i < q->nr; i++) {struct diff_filepair *p = q->queue[i];if (check_pair_status (p))diff_flush_patch (p, o); } + +if (o->emitted_symbols) { +for (i =0 ; i < esm.nr; i++) +emit_diff_symbol_from_struct (o, &esm.buf[i]); + +for (i =0 ; i < esm.nr; i++) +free ((void *)esm.buf[i].line); + } + esm.nr =0 ; }void diff_flush (struct diff_options *options)
diff.h +2 -0
@@ -186,6 +186,8 @@ struct diff_options {void *output_prefix_data;int diff_path_counter; + +struct emitted_diff_symbols *emitted_symbols; };void diff_emit_submodule_del (struct diff_options *o,const char *line);
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH v2 25/25 diff: document the new --color-moved setting
In reply to Stefan Beller--- Documentation/config.txt | 15 +++++++++++++-- Documentation/diff-options.txt | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-)
Documentation/config.txt +13 -2
@@ -1077,14 +1077,25 @@ This does not affect linkgit:git-format-patch[1] or the 'git-diff-{asterisk}' plumbing commands. Can be overridden on the command line with the `--color[=<when>]` option. +diff.colorMoved:: + If set to either a valid `<mode>` or a true value, moved lines + in a diff are colored differently, for details of valid modes + see '--color-moved' in linkgit:git-diff[1]. If simply set to + true the default color mode will be used. When set to false, + moved lines are not colored. + color.diff.<slot>:: Use customized color for diff colorization. `<slot>` specifies which part of the patch to use the specified color, and is one 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), `oldMoved` (deleted lines), + `newMoved` (added lines), `oldMovedDimmed`, `oldMovedAlternative`, + `oldMovedAlternativeDimmed`, `newMovedDimmed`, `newMovedAlternative` + and `newMovedAlternativeDimmed` (See the '<mode>' + setting of '--color-moved' in linkgit:git-diff[1] for details). color.decorate.<slot>:: Use customized color for 'git log --decorate' output. `<slot>` is one
Documentation/diff-options.txt +36 -0
@@ -231,6 +231,42 @@ ifdef::git-diff[] endif::git-diff[] It is the same as `--color=never`. +--color-moved[=<mode>]:: + Moved lines of code are colored differently. +ifdef::git-diff[] + It can be changed by the `diff.colorMoved` configuration setting. +endif::git-diff[] + 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 code 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. If there + are fewer than 3 adjacent moved lines, they are not marked up + as moved, but the regular colors 'color.diff.{old,new}' will be + used. +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. +-- + --word-diff[=<mode>]:: Show a word diff, using the <mode> to delimit changed words. By default, words are delimited by whitespace; see
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH v2 23/25 diff.c: color moved lines differently, plain mode
In reply to Stefan BellerAdd the 'plain' mode for move detection of code. This omits the checking for adjacent blocks, so it is not as useful. If you have a lot of the same blocks moved in the same patch, the 'Zebra' would end up slow as it is O(n^2) (n is number of same blocks). So this may be useful there and is generally easy to add. Instead be very literal at the move detection, do not skip over short blocks here.
--- diff.c | 10 +++++++-- diff.h | 1 + t/t4015-diff-whitespace.sh | 51 +++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 59 insertions(+), 3 deletions(-)
diff.c +8 -2
@@ -256,12 +256,14 @@ static int parse_color_moved(const char *arg)if (!strcmp (arg,"no" ))return COLOR_MOVED_NO ; +else if (!strcmp (arg,"plain" )) +return COLOR_MOVED_PLAIN ;else if (!strcmp (arg,"zebra" ))return COLOR_MOVED_ZEBRA ;else if (!strcmp (arg,"default" ))return COLOR_MOVED_DEFAULT ;else -return error (_ ("color moved setting must be one of 'no', 'default', 'zebra'" )); +return error (_ ("color moved setting must be one of 'no', 'default', 'zebra', 'plain'" )); }int git_diff_ui_config (const char *var,const char *value,void *cb)
@@ -879,7 +881,8 @@ static void mark_color_as_moved(struct diff_options *o, }if (!match) { -if (block_length <COLOR_MOVED_MIN_BLOCK_LENGTH ) { +if (block_length <COLOR_MOVED_MIN_BLOCK_LENGTH && + o->color_moved !=COLOR_MOVED_PLAIN ) {for (i =0 ; i < block_length +1 ; i++) { l = &o->emitted_symbols->buf[n - i]; l->flags &= ~DIFF_SYMBOL_MOVED_LINE ;
@@ -893,6 +896,9 @@ static void mark_color_as_moved(struct diff_options *o, l->flags |=DIFF_SYMBOL_MOVED_LINE ; block_length++; +if (o->color_moved ==COLOR_MOVED_PLAIN ) +continue ; +/* Check any potential block runs, advance each or nullify */ for (i =0 ; i < pmb_nr; i++) {struct moved_entry *p = pmb[i];
diff.h +1 -0
@@ -190,6 +190,7 @@ struct diff_options {struct emitted_diff_symbols *emitted_symbols;enum {COLOR_MOVED_NO =0 , +COLOR_MOVED_PLAIN =1 ,COLOR_MOVED_ZEBRA =2 , } color_moved;#define COLOR_MOVED_DEFAULT COLOR_MOVED_ZEBRA
t/t4015-diff-whitespace.sh +50 -1
63 lines not on this page · raw message
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH v2 22/25 diff.c: color moved lines differently
In reply to Stefan BellerWhen a patch consists mostly of moving blocks of code around, it can be quite tedious to ensure that the blocks are moved verbatim, and not undesirably modified in the move. To that end, color blocks that are moved within the same patch differently. For example (OM, del, add, and NM are different colors):
[OM] -void sensitive_stuff(void)
[OM] -{
[OM] - if (!is_authorized_user())
[OM] - die("unauthorized");
[OM] - sensitive_stuff(spanning,
[OM] - multiple,
[OM] - lines);
[OM] -} void another_function()
{
[del] - printf("foo");
[add] + printf("bar");
}
[NM] +void sensitive_stuff(void)
[NM] +{
[NM] + if (!is_authorized_user())
[NM] + die("unauthorized");
[NM] + sensitive_stuff(spanning,
[NM] + multiple,
[NM] + lines);
[NM] +}However adjacent blocks may be problematic. For example, in this potentially malicious patch, the swapping of blocks can be spotted:
[OM] -void sensitive_stuff(void)
[OM] -{
[OMA] - if (!is_authorized_user())
[OMA] - die("unauthorized");
[OM] - sensitive_stuff(spanning,
[OM] - multiple,
[OM] - lines);
[OMA] -}
void another_function()
{
[del] - printf("foo");
[add] + printf("bar");
}
[NM] +void sensitive_stuff(void)
[NM] +{
[NMA] + sensitive_stuff(spanning,
[NMA] + multiple,
[NMA] + lines);
[NM] + if (!is_authorized_user())
[NM] + die("unauthorized");
[NMA] +}If the moved code is larger, it is easier to hide some permutation in the code, which is why some alternative coloring is needed.
This patch implements the first mode:
* basic alternating 'Zebra' mode
This conveys all information needed to the user. Defer customization to later patches.
First I implemented an alternative design, which would try to fingerprint a line by its neighbors to detect if we are in a block or at the boundary. This idea iss error prone as it inspected each line and its neighboring lines to determine if the line was (a) moved and (b) if was deep inside a hunk by having matching neighboring lines. This is unreliable as the we can construct hunks which have equal neighbors that just exceed the number of lines inspected. (Think of 'AXYZBXYZCXYZD..' with each letter as a line, that is permutated to AXYZCXYZBXYZD..').
Instead this provides a dynamic programming greedy algorithm that finds the largest moved hunk and then has several modes on highlighting bounds.
A note on the options '--submodule=diff' and '--color-words/--word-diff':
In the conversion to use emit_line in the prior patches both submodules as well as word diff output carefully chose to call emit_line with sign=0. All output with sign=0 is ignored for move detection purposes in this patch, such that no weird looking output will be generated for these cases. This leads to another thought: We could pass on '--color-moved' to submodules such that they color up moved lines for themselves. If we'd do so only line moves within a repository boundary are marked up.
It is useful to have moved lines colored, but there are annoying corner cases, such as a single line moved, that is very common. For example in a typical patch of C code, we have closing braces that end statement blocks or functions.
While it is technically true that these lines are moved as they show up elsewhere, it is harmful for the review as the reviewers attention is drawn to such a minor side annoyance.
For now let's have a simple solution of hardcoding the number of moved lines to be at least 3 before coloring them. Note, that the length is applied across all blocks to find the 'lonely' blocks that pollute new code, but do not interfere with a permutated block where each permutation has less lines than 3.
--- diff.c | 344 +++++++++++++++++++++++++++++++++++++++++++-- diff.h | 12 +- t/t4015-diff-whitespace.sh | 261 ++++++++++++++++++++++++++++++++++ 3 files changed, 602 insertions(+), 15 deletions(-)
diff.c +330 -14
@@ -16,6 +16,7 @@#include "userdiff.h" #include "submodule-config.h" #include "submodule.h" +#include "hashmap.h" #include "ll-merge.h" #include "string-list.h" #include "argv-array.h"
@@ -32,6 +33,7 @@ static int diff_indent_heuristic = 1;static int diff_rename_limit_default =400 ;static int diff_suppress_blank_empty;static int diff_use_color_default =-1 ; +static int diff_color_moved_default;static int diff_context_default =3 ;static int diff_interhunk_context_default;static const char *diff_word_regex_cfg;
@@ -56,6 +58,10 @@ static char diff_colors[][COLOR_MAXLEN] = {GIT_COLOR_YELLOW ,/* COMMIT */ GIT_COLOR_BG_RED ,/* WHITESPACE */ GIT_COLOR_NORMAL ,/* FUNCINFO */ +GIT_COLOR_MAGENTA ,/* OLD_MOVED */ +GIT_COLOR_BLUE ,/* OLD_MOVED ALTERNATIVE */ +GIT_COLOR_CYAN ,/* NEW_MOVED */ + GIT_COLOR_YELLOW, /* NEW_MOVED ALTERNATIVE */ }; static NORETURN void die_want_option(const char *option_name)
@@ -81,6 +87,14 @@ static int parse_diff_color_slot(const char *var)return DIFF_WHITESPACE ;if (!strcasecmp (var,"func" ))return DIFF_FUNCINFO ; +if (!strcasecmp (var,"oldmoved" )) +return DIFF_FILE_OLD_MOVED ; +if (!strcasecmp (var,"oldmovedalternative" )) +return DIFF_FILE_OLD_MOVED_ALT ; +if (!strcasecmp (var,"newmoved" )) +return DIFF_FILE_NEW_MOVED ; +if (!strcasecmp (var,"newmovedalternative" )) +return DIFF_FILE_NEW_MOVED_ALT ;return -1 ; }
@@ -229,12 +243,40 @@ int git_diff_heuristic_config(const char *var, const char *value, void *cb)return 0 ; } +static int parse_color_moved (const char *arg) +{ +switch (git_parse_maybe_bool (arg)) { +case 0 : +return COLOR_MOVED_NO ; +case 1 : +return COLOR_MOVED_DEFAULT ; +default : +break ; + } + +if (!strcmp (arg,"no" )) +return COLOR_MOVED_NO ; +else if (!strcmp (arg,"zebra" )) +return COLOR_MOVED_ZEBRA ; +else if (!strcmp (arg,"default" )) +return COLOR_MOVED_DEFAULT ; +else +return error (_ ("color moved setting must be one of 'no', 'default', 'zebra'" )); +} +int git_diff_ui_config (const char *var,const char *value,void *cb) {if (!strcmp (var,"diff.color" ) || !strcmp (var,"color.diff" )) { diff_use_color_default =git_config_colorbool (var, value);return 0 ; } +if (!strcmp (var,"diff.colormoved" )) { +int cm =parse_color_moved (value); +if (cm <0 ) +return -1 ; + diff_color_moved_default = cm; +return 0 ; + }if (!strcmp (var,"diff.context" )) { diff_context_default =git_config_int (var, value);if (diff_context_default <0 )
@@ -602,7 +644,9 @@ enum diff_symbol {* 13-15 are WSEH_NEW | WSEH_OLD | WSEH_CONTEXT * 16 is marking if the line is blank at EOF */ -#define DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF (1<<16) +#define DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF (1<<16) +#define DIFF_SYMBOL_MOVED_LINE (1<<17) +#define DIFF_SYMBOL_MOVED_LINE_ALT (1<<18)#define DIFF_SYMBOL_CONTENT_WS_MASK (WSEH_NEW | WSEH_OLD | WSEH_CONTEXT | WS_RULE_MASK)/*
@@ -645,6 +689,243 @@ static void append_emitted_diff_symbol(struct diff_options *o, f->line = e->line ?xmemdupz (e->line, e->len) :NULL ; } +struct moved_entry { +struct hashmap_entry ent; +const struct emitted_diff_symbol *es; +struct moved_entry *next_line; +}; + +static int next_byte (const char **cp,const char **endp, +const struct diff_options *diffopt) +{ +int retval; + +if (*cp > *endp) +return -1 ; + +if (DIFF_XDL_TST (diffopt,IGNORE_WHITESPACE_CHANGE )) { +while (*cp < *endp &&isspace (**cp)) + (*cp)++; +/* +* After skipping a couple of whitespaces, we still have to +* account for one space. +*/ +return (int )' ' ; + } + +if (DIFF_XDL_TST (diffopt,IGNORE_WHITESPACE )) { +while (*cp < *endp &&isspace (**cp)) + (*cp)++; +/* return the first non-ws character via the usual below */ + } + + retval = (unsigned char )(**cp); + (*cp)++; +return retval; +} + +static int moved_entry_cmp (const struct diff_options *diffopt, +const struct moved_entry *a, +const struct moved_entry *b, +const void *keydata) +{ +const char *ap = a->es->line, *ae = a->es->line + a->es->len; +const char *bp = b->es->line, *be = b->es->line + b->es->len; + +if (!(diffopt->xdl_opts &XDF_WHITESPACE_FLAGS )) +return a->es->len != b->es->len ||memcmp (ap, bp, a->es->len); + +if (DIFF_XDL_TST (diffopt,IGNORE_WHITESPACE_AT_EOL )) { +while (ae > ap &&isspace (*ae)) + ae--; +while (be > bp &&isspace (*be)) + be--; + } + +while (1 ) { +int ca, cb; + ca =next_byte (&ap, &ae, diffopt); + cb =next_byte (&bp, &be, diffopt); +if (ca != cb) +return 1 ; +if (ca <0 ) +return 0 ; + } +} + +static unsigned get_string_hash (struct emitted_diff_symbol *es,struct diff_options *o) +{ +if (o->xdl_opts &XDF_WHITESPACE_FLAGS ) { +static struct strbuf sb =STRBUF_INIT ; +const char *ap = es->line, *ae = es->line + es->len; +int c; + +strbuf_reset (&sb); +while (ae > ap &&isspace (*ae)) + ae--; +while ((c =next_byte (&ap, &ae, o)) >0 ) +strbuf_addch (&sb, c); + +return memhash (sb.buf, sb.len); + }else { +return memhash (es->line, es->len); + } +} + +static struct moved_entry *prepare_entry (struct diff_options *o, +int line_no) +{ +struct moved_entry *ret =xmalloc (sizeof (*ret)); +struct emitted_diff_symbol *l = &o->emitted_symbols->buf[line_no]; + + ret->ent.hash =get_string_hash (l, o); + ret->es = l; + ret->next_line =NULL ; + +return ret; +} + +static void add_lines_to_move_detection (struct diff_options *o, +struct hashmap *add_lines, +struct hashmap *del_lines) +{ +struct moved_entry *prev_line =NULL ; + +int n; +for (n =0 ; n < o->emitted_symbols->nr; n++) { +struct hashmap *hm; +struct moved_entry *key; + +switch (o->emitted_symbols->buf[n].s) { +case DIFF_SYMBOL_PLUS : + hm = add_lines; +break ; +case DIFF_SYMBOL_MINUS : + hm = del_lines; +break ; +default : + prev_line =NULL ; +continue ; + } + + key =prepare_entry (o, n); +if (prev_line && prev_line->es->s == o->emitted_symbols->buf[n].s) + prev_line->next_line = key; + +hashmap_add (hm, key); + prev_line = key; + } +} + +static int shrink_potential_moved_blocks (struct moved_entry **pmb, +int pmb_nr) +{ +int lp, rp; + +/* Shrink the set of potential block to the remaining running */ +for (lp =0 , rp = pmb_nr -1 ; lp <= rp;) { +while (lp < pmb_nr && pmb[lp]) + lp++; +/* lp points at the first NULL now */ + +while (rp >-1 && !pmb[rp]) + rp--; +/* rp points at the last non-NULL */ + +if (lp < pmb_nr && rp >-1 && lp < rp) { + pmb[lp] = pmb[rp]; + pmb[rp] =NULL ; + rp--; + lp++; + } + } + +/* Remember the number of running sets */ +return rp +1 ; +} + +/* Find blocks of moved code, delegate actual coloring decision to helper */ +static void mark_color_as_moved (struct diff_options *o, +struct hashmap *add_lines, +struct hashmap *del_lines) +{ +struct moved_entry **pmb =NULL ;/* potentially moved blocks */ +int pmb_nr =0 , pmb_alloc =0 ; +int n, flipped_block =1 , block_length =0 ; + + +for (n =0 ; n < o->emitted_symbols->nr; n++) { +struct hashmap *hm =NULL ; +struct moved_entry *key; +struct moved_entry *match =NULL ; +struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n]; +int i; + +switch (l->s) { +case DIFF_SYMBOL_PLUS : + hm = del_lines; + key =prepare_entry (o, n); + match =hashmap_get (hm, key, o); +free (key); +break ; +case DIFF_SYMBOL_MINUS : + hm = add_lines; + key =prepare_entry (o, n); + match =hashmap_get (hm, key, o); +free (key); +break ; +default : + flipped_block =1 ; + } + +if (!match) { +if (block_length <COLOR_MOVED_MIN_BLOCK_LENGTH ) { +for (i =0 ; i < block_length +1 ; i++) { + l = &o->emitted_symbols->buf[n - i]; + l->flags &= ~DIFF_SYMBOL_MOVED_LINE ; + } + } + pmb_nr =0 ; + block_length =0 ; +continue ; + } + + l->flags |=DIFF_SYMBOL_MOVED_LINE ; + block_length++; + +/* Check any potential block runs, advance each or nullify */ +for (i =0 ; i < pmb_nr; i++) { +struct moved_entry *p = pmb[i]; +struct moved_entry *pnext = (p && p->next_line) ? + p->next_line :NULL ; +if (pnext && !hm->cmpfn (o, pnext, match,NULL )) { + pmb[i] = p->next_line; + }else { + pmb[i] =NULL ; + } + } + + pmb_nr =shrink_potential_moved_blocks (pmb, pmb_nr); + +if (pmb_nr ==0 ) { +/* +* The current line is the start of a new block. +* Setup the set of potential blocks. +*/ +for (; match; match =hashmap_get_next (hm, match)) { +ALLOC_GROW (pmb, pmb_nr +1 , pmb_alloc); + pmb[pmb_nr++] = match; + } + + flipped_block = (flipped_block +1 ) %2 ; + } + +if (flipped_block) + l->flags |=DIFF_SYMBOL_MOVED_LINE_ALT ; + } + +free (pmb); +} static void emit_line_ws_markup(struct diff_options *o, const char *set, const char *reset,
@@ -720,14 +1001,24 @@ static void emit_diff_symbol_from_struct(struct diff_options *o, flags & (DIFF_SYMBOL_CONTENT_WS_MASK), 0);break ;case DIFF_SYMBOL_PLUS : - set =diff_get_color_opt (o,DIFF_FILE_NEW ); +if (flags &DIFF_SYMBOL_MOVED_LINE_ALT ) + set =diff_get_color_opt (o,DIFF_FILE_NEW_MOVED_ALT ); +else if (flags &DIFF_SYMBOL_MOVED_LINE ) + set =diff_get_color_opt (o,DIFF_FILE_NEW_MOVED ); +else + set =diff_get_color_opt (o,DIFF_FILE_NEW ); reset =diff_get_color_opt (o,DIFF_RESET );emit_line_ws_markup (o, set, reset, line, len,'+' , flags &DIFF_SYMBOL_CONTENT_WS_MASK , flags &DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF );break ;case DIFF_SYMBOL_MINUS : - set =diff_get_color_opt (o,DIFF_FILE_OLD ); +if (flags &DIFF_SYMBOL_MOVED_LINE_ALT ) + set =diff_get_color_opt (o,DIFF_FILE_OLD_MOVED_ALT ); +else if (flags &DIFF_SYMBOL_MOVED_LINE ) + set =diff_get_color_opt (o,DIFF_FILE_OLD_MOVED ); +else + set =diff_get_color_opt (o,DIFF_FILE_OLD ); reset =diff_get_color_opt (o,DIFF_RESET );emit_line_ws_markup (o, set, reset, line, len,'-' , flags &DIFF_SYMBOL_CONTENT_WS_MASK ,0 );
@@ -3740,6 +4031,8 @@ void diff_setup(struct diff_options *options) options->a_prefix ="a/" ; options->b_prefix ="b/" ; } + + options->color_moved = diff_color_moved_default; }void diff_setup_done (struct diff_options *options)
@@ -3849,6 +4142,9 @@ void diff_setup_done(struct diff_options *options)if (DIFF_OPT_TST (options,FOLLOW_RENAMES ) && options->pathspec.nr !=1 )die (_ ("--follow requires exactly one pathspec" )); + +if (!options->use_color ||external_diff ()) + options->color_moved =0 ; }static int opt_arg (const char *arg,int arg_short,const char *arg_long,int *val)
@@ -4273,7 +4569,19 @@ int diff_opt_parse(struct diff_options *options, }else if (!strcmp (arg,"--no-color" )) options->use_color =0 ; -else if (!strcmp (arg,"--color-words" )) { +else if (!strcmp (arg,"--color-moved" )) { +if (diff_color_moved_default) + options->color_moved = diff_color_moved_default; +if (options->color_moved ==COLOR_MOVED_NO ) + options->color_moved =COLOR_MOVED_DEFAULT ; + }else if (!strcmp (arg,"--no-color-moved" )) + options->color_moved =COLOR_MOVED_NO ; +else if (skip_prefix (arg,"--color-moved=" , &arg)) { +int cm =parse_color_moved (arg); +if (cm <0 ) +die ("bad --color-moved argument: %s" , arg); + options->color_moved = cm; + }else if (!strcmp (arg,"--color-words" )) { options->use_color =1 ; options->word_diff =DIFF_WORDS_COLOR ; }
@@ -5086,16 +5394,8 @@ static void diff_flush_patch_all_file_pairs(struct diff_options *o)if (WSEH_NEW &WS_RULE_MASK )die ("BUG: WS rules bit mask overlaps with diff symbol flags" ); -/* -* For testing purposes we want to make sure the diff machinery -* works completely with the buffer. If there is anything emitted -* outside the emit_string, then the order is screwed -* up and the tests will fail. -* -* TODO (later in this series): -* We'll unset this pointer in a later patch. -*/ - o->emitted_symbols = &esm; +if (o->color_moved) + o->emitted_symbols = &esm;for (i =0 ; i < q->nr; i++) {struct diff_filepair *p = q->queue[i];
@@ -5104,6 +5404,21 @@ static void diff_flush_patch_all_file_pairs(struct diff_options *o) }if (o->emitted_symbols) { +if (o->color_moved) { +struct hashmap add_lines, del_lines; + +hashmap_init (&del_lines, + (hashmap_cmp_fn )moved_entry_cmp, o,0 ); +hashmap_init (&add_lines, + (hashmap_cmp_fn )moved_entry_cmp, o,0 ); + +add_lines_to_move_detection (o, &add_lines, &del_lines); +mark_color_as_moved (o, &add_lines, &del_lines); + +hashmap_free (&add_lines,0 ); +hashmap_free (&del_lines,0 ); + } +for (i =0 ; i < esm.nr; i++)emit_diff_symbol_from_struct (o, &esm.buf[i]);
@@ -5185,6 +5500,7 @@ void diff_flush(struct diff_options *options)fclose (options->file); options->file =xfopen ("/dev/null" ,"w" ); options->close_file =1 ; + options->color_moved =0 ;for (i =0 ; i < q->nr; i++) {struct diff_filepair *p = q->queue[i];if (check_pair_status (p))
diff.h +11 -1
@@ -188,6 +188,12 @@ struct diff_options {int diff_path_counter;struct emitted_diff_symbols *emitted_symbols; +enum { +COLOR_MOVED_NO =0 , +COLOR_MOVED_ZEBRA =2 , + } color_moved; +#define COLOR_MOVED_DEFAULT COLOR_MOVED_ZEBRA +#define COLOR_MOVED_MIN_BLOCK_LENGTH 3 };void diff_emit_submodule_del (struct diff_options *o,const char *line);
@@ -208,7 +214,11 @@ enum color_diff {DIFF_FILE_NEW =5 ,DIFF_COMMIT =6 ,DIFF_WHITESPACE =7 , - DIFF_FUNCINFO = 8 +DIFF_FUNCINFO =8 , +DIFF_FILE_OLD_MOVED =9 , +DIFF_FILE_OLD_MOVED_ALT =10 , +DIFF_FILE_NEW_MOVED =11 , + DIFF_FILE_NEW_MOVED_ALT = 12 };const char *diff_get_color (int diff_use_color,enum color_diff ix);#define diff_get_color_opt (o, ix) \
t/t4015-diff-whitespace.sh +261 -0
265 lines not on this page · raw message
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH v2 24/25 diff.c: add dimming to moved line detection
In reply to Stefan BellerAny lines inside a moved block of code are not interesting. Boundaries of blocks are only interesting if they are next to another block of moved code.
--- color.h | 2 + diff.c | 132 +++++++++++++++++++++++++++++++++++++++++---- diff.h | 9 +++- t/t4015-diff-whitespace.sh | 124 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 254 insertions(+), 13 deletions(-)
color.h +2 -0
@@ -42,6 +42,8 @@ struct strbuf;#define GIT_COLOR_BG_BLUE "\033[44m"#define GIT_COLOR_BG_MAGENTA "\033[45m"#define GIT_COLOR_BG_CYAN "\033[46m" +#define GIT_COLOR_FAINT "\033[2m" +#define GIT_COLOR_FAINT_ITALIC "\033[2;3m"/* A special value meaning "no color selected" */ #define GIT_COLOR_NIL "NIL"
diff.c +121 -11
191 lines not on this page · raw message
diff.h +7 -2
@@ -192,6 +192,7 @@ struct diff_options {COLOR_MOVED_NO =0 ,COLOR_MOVED_PLAIN =1 ,COLOR_MOVED_ZEBRA =2 , +COLOR_MOVED_ZEBRA_DIM =3 , } color_moved;#define COLOR_MOVED_DEFAULT COLOR_MOVED_ZEBRA#define COLOR_MOVED_MIN_BLOCK_LENGTH 3
@@ -218,8 +219,12 @@ enum color_diff {DIFF_FUNCINFO =8 ,DIFF_FILE_OLD_MOVED =9 ,DIFF_FILE_OLD_MOVED_ALT =10 , -DIFF_FILE_NEW_MOVED =11 , - DIFF_FILE_NEW_MOVED_ALT = 12 +DIFF_FILE_OLD_MOVED_DIM =11 , +DIFF_FILE_OLD_MOVED_ALT_DIM =12 , +DIFF_FILE_NEW_MOVED =13 , +DIFF_FILE_NEW_MOVED_ALT =14 , +DIFF_FILE_NEW_MOVED_DIM =15 , +DIFF_FILE_NEW_MOVED_ALT_DIM =16 };const char *diff_get_color (int diff_use_color,enum color_diff ix);#define diff_get_color_opt (o, ix) \
t/t4015-diff-whitespace.sh +124 -0
130 lines not on this page · raw message
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH v2 19/25 diff.c: emit_diff_symbol learns about DIFF_SYMBOL_STAT_SEP
In reply to Stefan Beller--- diff.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
diff.c +7 -3
@@ -571,6 +571,7 @@ enum diff_symbol {DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES ,DIFF_SYMBOL_STATS_LINE ,DIFF_SYMBOL_WORD_DIFF , +DIFF_SYMBOL_STAT_SEP ,DIFF_SYMBOL_SUBMODULE_ADD ,DIFF_SYMBOL_SUBMODULE_DEL , DIFF_SYMBOL_SUBMODULE_UNTRACKED,
@@ -766,6 +767,9 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,case DIFF_SYMBOL_WORD_DIFF :fprintf (o->file,"%.*s" , len, line);break ; +case DIFF_SYMBOL_STAT_SEP : +fputs (o->stat_sep, o->file); +break ;default :die ("BUG: unknown diff symbol" ); }
@@ -5077,10 +5081,10 @@ void diff_flush(struct diff_options *options)if (output_format &DIFF_FORMAT_PATCH ) {if (separator) {emit_diff_symbol (options,DIFF_SYMBOL_SEPARATOR ,NULL ,0 ,0 ); -if (options->stat_sep) { +if (options->stat_sep)/* attach patch instead of inline */ -fputs (options->stat_sep, options->file); - } +emit_diff_symbol (options,DIFF_SYMBOL_STAT_SEP , +NULL ,0 ,0 ); }diff_flush_patch_all_file_pairs (options);
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH v2 16/25 diff.c: convert emit_binary_diff_body to use emit_diff_symbol
In reply to Stefan Beller--- diff.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 46 insertions(+), 17 deletions(-)
diff.c +46 -17
@@ -561,6 +561,11 @@ static void emit_line(struct diff_options *o, const char *set, const char *reset }enum diff_symbol { +DIFF_SYMBOL_BINARY_DIFF_HEADER , +DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA , +DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL , +DIFF_SYMBOL_BINARY_DIFF_BODY , +DIFF_SYMBOL_BINARY_DIFF_FOOTER ,DIFF_SYMBOL_SUBMODULE_ADD ,DIFF_SYMBOL_SUBMODULE_DEL ,DIFF_SYMBOL_SUBMODULE_UNTRACKED ,
@@ -635,6 +640,7 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,case DIFF_SYMBOL_SUBMODULE_HEADER :case DIFF_SYMBOL_SUBMODULE_ERROR :case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH : +case DIFF_SYMBOL_BINARY_DIFF_BODY :case DIFF_SYMBOL_CONTEXT_FRAGINFO :emit_line (o,"" ,"" , line, len);break ;
@@ -706,6 +712,19 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,case DIFF_SYMBOL_HEADER :fprintf (o->file,"%s" , line);break ; +case DIFF_SYMBOL_BINARY_DIFF_HEADER : +fprintf (o->file,"%sGIT binary patch\n" ,diff_line_prefix (o)); +break ; +case DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA : +fprintf (o->file,"%sdelta %s\n" ,diff_line_prefix (o), line); +break ; +case DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL : +fprintf (o->file,"%sliteral %s\n" ,diff_line_prefix (o), line); +break ; +case DIFF_SYMBOL_BINARY_DIFF_FOOTER : +fputs (diff_line_prefix (o), o->file); +fputc ('\n' , o->file); +break ;case DIFF_SYMBOL_REWRITE_DIFF : fraginfo =diff_get_color (o->use_color,DIFF_FRAGINFO ); reset =diff_get_color_opt (o,DIFF_RESET );
@@ -2390,8 +2409,8 @@ static unsigned char *deflate_it(char *data,return deflated; } -static void emit_binary_diff_body (FILE *file,mmfile_t *one,mmfile_t *two, -const char *prefix) +static void emit_binary_diff_body (struct diff_options *o, +mmfile_t *one,mmfile_t *two) {void *cp;void *delta;
@@ -2420,13 +2439,18 @@ static void emit_binary_diff_body(FILE *file, mmfile_t *one, mmfile_t *two, }if (delta && delta_size < deflate_size) { -fprintf (file,"%sdelta %lu\n" , prefix, orig_size); +char *s =xstrfmt ("%lu" , orig_size); +emit_diff_symbol (o,DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA , + s,strlen (s),0 ); +free (s);free (deflated); data = delta; data_size = delta_size; - } -else { -fprintf (file,"%sliteral %lu\n" , prefix, two->size); + }else { +char *s =xstrfmt ("%lu" , two->size); +emit_diff_symbol (o,DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL , + s,strlen (s),0 ); +free (s);free (delta); data = deflated; data_size = deflate_size;
@@ -2435,8 +2459,9 @@ static void emit_binary_diff_body(FILE *file, mmfile_t *one, mmfile_t *two,/* emit data encoded in base85 */ cp = data;while (data_size) { +int len;int bytes = (52 < data_size) ?52 : data_size; -char line[70 ]; +char line[71 ]; data_size -= bytes;if (bytes <=26 ) line[0 ] = bytes +'A' -1 ;
@@ -2444,20 +2469,24 @@ static void emit_binary_diff_body(FILE *file, mmfile_t *one, mmfile_t *two, line[0 ] = bytes -26 +'a' -1 ;encode_85 (line +1 , cp, bytes); cp = (char *) cp + bytes; -fprintf (file,"%s" , prefix); -fputs (line, file); -fputc ('\n' , file); + + len =strlen (line); + line[len++] ='\n' ; + line[len] ='\0' ; + +emit_diff_symbol (o,DIFF_SYMBOL_BINARY_DIFF_BODY , + line, len,0 ); } -fprintf (file,"%s\n" , prefix); +emit_diff_symbol (o,DIFF_SYMBOL_BINARY_DIFF_FOOTER ,NULL ,0 ,0 );free (data); } -static void emit_binary_diff (FILE *file,mmfile_t *one,mmfile_t *two, -const char *prefix) +static void emit_binary_diff (struct diff_options *o, +mmfile_t *one,mmfile_t *two) { -fprintf (file,"%sGIT binary patch\n" , prefix); -emit_binary_diff_body (file, one, two, prefix); -emit_binary_diff_body (file, two, one, prefix); +emit_diff_symbol (o,DIFF_SYMBOL_BINARY_DIFF_HEADER ,NULL ,0 ,0 ); +emit_binary_diff_body (o, one, two); +emit_binary_diff_body (o, two, one); }int diff_filespec_is_binary (struct diff_filespec *one)
@@ -2643,7 +2672,7 @@ static void builtin_diff(const char *name_a,emit_diff_symbol (o,DIFF_SYMBOL_HEADER , header.buf, header.len,0 );strbuf_reset (&header);if (DIFF_OPT_TST (o,BINARY )) -emit_binary_diff (o->file, &mf1, &mf2, line_prefix); +emit_binary_diff (o, &mf1, &mf2);else {strbuf_addf (&sb,"%sBinary files %s and %s differ\n" ,diff_line_prefix (o), lbl[0 ], lbl[1 ]);
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH v2 3/25 diff.c: factor out diff_flush_patch_all_file_pairs
In reply to Stefan BellerIn a later patch we want to do more things before and after all filepairs are flushed. So factor flushing out all file pairs into its own function that the new code can be plugged in easily.
--- diff.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)
diff.c +12 -5
@@ -4734,6 +4734,17 @@ void diff_warn_rename_limit(const char *varname, int needed, int degraded_cc)warning (_ (rename_limit_advice), varname, needed); } +static void diff_flush_patch_all_file_pairs (struct diff_options *o) +{ +int i; +struct diff_queue_struct *q = &diff_queued_diff; +for (i =0 ; i < q->nr; i++) { +struct diff_filepair *p = q->queue[i]; +if (check_pair_status (p)) +diff_flush_patch (p, o); + } +} +void diff_flush (struct diff_options *options) {struct diff_queue_struct *q = &diff_queued_diff;
@@ -4826,11 +4837,7 @@ void diff_flush(struct diff_options *options) } } -for (i =0 ; i < q->nr; i++) { -struct diff_filepair *p = q->queue[i]; -if (check_pair_status (p)) -diff_flush_patch (p, options); - } +diff_flush_patch_all_file_pairs (options); }if (output_format &DIFF_FORMAT_CALLBACK )
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH v2 11/25 diff.c: emit_diff_symbol learns DIFF_SYMBOL_FILEPAIR_{PLUS, MINUS}
In reply to Stefan BellerWe have to use fprintf instead of emit_line, because we want to emit the tab after the color. This is important for ancient versions of gnu patch AFAICT, although we probably do not want to feed colored output to the patch utility, such that it would not matter if the trailing tab is colored. Keep the corner case as-is though.
--- diff.c | 51 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 21 deletions(-)
diff.c +30 -21
@@ -561,6 +561,8 @@ static void emit_line(struct diff_options *o, const char *set, const char *reset }enum diff_symbol { +DIFF_SYMBOL_FILEPAIR_PLUS , +DIFF_SYMBOL_FILEPAIR_MINUS ,DIFF_SYMBOL_WORDS_PORCELAIN ,DIFF_SYMBOL_WORDS ,DIFF_SYMBOL_CONTEXT ,
@@ -611,7 +613,7 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s, const char *line, int len, unsigned flags) { static const char *nneof = " No newline at end of file\n"; -const char *context, *reset, *set; +const char *context, *reset, *set, *meta;switch (s) {case DIFF_SYMBOL_NO_LF_EOF : context =diff_get_color_opt (o,DIFF_CONTEXT );
@@ -673,6 +675,20 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s, }emit_line (o, context, reset, line, len);break ; +case DIFF_SYMBOL_FILEPAIR_PLUS : + meta =diff_get_color_opt (o,DIFF_METAINFO ); + reset =diff_get_color_opt (o,DIFF_RESET ); +fprintf (o->file,"%s%s+++ %s%s%s\n" ,diff_line_prefix (o), meta, + line, reset, +strchr (line,' ' ) ?"\t" :"" ); +break ; +case DIFF_SYMBOL_FILEPAIR_MINUS : + meta =diff_get_color_opt (o,DIFF_METAINFO ); + reset =diff_get_color_opt (o,DIFF_RESET ); +fprintf (o->file,"%s%s--- %s%s%s\n" ,diff_line_prefix (o), meta, + line, reset, +strchr (line,' ' ) ?"\t" :"" ); +break ;default :die ("BUG: unknown diff symbol" ); }
@@ -844,8 +860,6 @@ static void emit_rewrite_diff(const char *name_a, struct diff_options *o) {int lc_a, lc_b; -const char *name_a_tab, *name_b_tab; -const char *metainfo =diff_get_color (o->use_color,DIFF_METAINFO );const char *fraginfo =diff_get_color (o->use_color,DIFF_FRAGINFO );const char *reset =diff_get_color (o->use_color,DIFF_RESET );static struct strbuf a_name =STRBUF_INIT , b_name =STRBUF_INIT ;
@@ -865,8 +879,6 @@ static void emit_rewrite_diff(const char *name_a, name_a += (*name_a =='/' ); name_b += (*name_b =='/' ); - name_a_tab =strchr (name_a,' ' ) ?"\t" :"" ; - name_b_tab =strchr (name_b,' ' ) ?"\t" :"" ;strbuf_reset (&a_name);strbuf_reset (&b_name);
@@ -893,11 +905,13 @@ static void emit_rewrite_diff(const char *name_a, lc_a =count_lines (data_one, size_one); lc_b =count_lines (data_two, size_two); -fprintf (o->file, -"%s%s--- %s%s%s\n%s%s+++ %s%s%s\n%s%s@@ -" , - line_prefix, metainfo, a_name.buf, name_a_tab, reset, - line_prefix, metainfo, b_name.buf, name_b_tab, reset, - line_prefix, fraginfo); + +emit_diff_symbol (o,DIFF_SYMBOL_FILEPAIR_MINUS , + a_name.buf, a_name.len,0 ); +emit_diff_symbol (o,DIFF_SYMBOL_FILEPAIR_PLUS , + b_name.buf, b_name.len,0 ); + +fprintf (o->file,"%s%s@@ -" , line_prefix, fraginfo);if (!o->irreversible_delete)print_line_count (o->file, lc_a);else
@@ -1365,10 +1379,8 @@ static void find_lno(const char *line, struct emit_callback *ecbdata)static void fn_out_consume (void *priv,char *line,unsigned long len) {struct emit_callback *ecbdata = priv; -const char *meta =diff_get_color (ecbdata->color_diff,DIFF_METAINFO );const char *reset =diff_get_color (ecbdata->color_diff,DIFF_RESET );struct diff_options *o = ecbdata->opt; -const char *line_prefix =diff_line_prefix (o); o->found_changes =1 ;
@@ -1379,15 +1391,12 @@ static void fn_out_consume(void *priv, char *line, unsigned long len) }if (ecbdata->label_path[0 ]) { -const char *name_a_tab, *name_b_tab; - - name_a_tab =strchr (ecbdata->label_path[0 ],' ' ) ?"\t" :"" ; - name_b_tab =strchr (ecbdata->label_path[1 ],' ' ) ?"\t" :"" ; - -fprintf (o->file,"%s%s--- %s%s%s\n" , - line_prefix, meta, ecbdata->label_path[0 ], reset, name_a_tab); -fprintf (o->file,"%s%s+++ %s%s%s\n" , - line_prefix, meta, ecbdata->label_path[1 ], reset, name_b_tab); +emit_diff_symbol (o,DIFF_SYMBOL_FILEPAIR_MINUS , + ecbdata->label_path[0 ], +strlen (ecbdata->label_path[0 ]),0 ); +emit_diff_symbol (o,DIFF_SYMBOL_FILEPAIR_PLUS , + ecbdata->label_path[1 ], +strlen (ecbdata->label_path[1 ]),0 ); ecbdata->label_path[0 ] = ecbdata->label_path[1 ] =NULL ; }
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH v2 10/25 diff.c: emit_diff_symbol learns DIFF_SYMBOL_CONTEXT_INCOMPLETE
In reply to Stefan BellerThe context marker use the exact same output pattern, so reuse it.
--- diff.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
diff.c +4 -2
@@ -564,6 +564,7 @@ enum diff_symbol {DIFF_SYMBOL_WORDS_PORCELAIN ,DIFF_SYMBOL_WORDS ,DIFF_SYMBOL_CONTEXT , +DIFF_SYMBOL_CONTEXT_INCOMPLETE ,DIFF_SYMBOL_PLUS ,DIFF_SYMBOL_MINUS , DIFF_SYMBOL_NO_LF_EOF,
@@ -622,6 +623,7 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,case DIFF_SYMBOL_CONTEXT_FRAGINFO :emit_line (o,"" ,"" , line, len);break ; +case DIFF_SYMBOL_CONTEXT_INCOMPLETE :case DIFF_SYMBOL_CONTEXT_MARKER : context =diff_get_color_opt (o,DIFF_CONTEXT ); reset =diff_get_color_opt (o,DIFF_RESET );
@@ -1448,8 +1450,8 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)default :/* incomplete line at the end */ ecbdata->lno_in_preimage++; -emit_line (o,diff_get_color (ecbdata->color_diff,DIFF_CONTEXT ), - reset, line, len); +emit_diff_symbol (o,DIFF_SYMBOL_CONTEXT_INCOMPLETE , + line, len,0 );break ; } }
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH v2 8/25 diff.c: migrate emit_line_checked to use emit_diff_symbol
In reply to Stefan BellerAdd a new flags field to emit_diff_symbol, that will be used by context lines for:
* white space rules that are applicable (The first 12 bits) Take a note in cahe.c as well, when this ws rules are extended we have to fix the bits in the flags field.
* how the rules are evaluated (actually this double encodes the sign of the line, but the code is easier to keep this way, bits 13,14,15)
* if the line a blank line at EOF (bit 16)
The check if new lines need to be marked up as extra lines at the end of file, is now done unconditionally. That should be ok, as 'new_blank_line_at_eof' has a quick early return.
--- cache.h | 2 ++ diff.c | 116 +++++++++++++++++++++++++++++++++++++++++----------------------- diff.h | 6 ++-- 3 files changed, 80 insertions(+), 44 deletions(-)
cache.h +2 -0
@@ -1996,6 +1996,8 @@ void shift_tree_by(const struct object_id *, const struct object_id *, struct ob#define WS_TRAILING_SPACE (WS_BLANK_AT_EOL|WS_BLANK_AT_EOF)#define WS_DEFAULT_RULE (WS_TRAILING_SPACE|WS_SPACE_BEFORE_TAB|8)#define WS_TAB_WIDTH_MASK 077 +/* All WS_* -- when extended, adapt diff.c emit_symbol */ +#define WS_RULE_MASK 07777extern unsigned whitespace_rule_cfg;extern unsigned whitespace_rule (const char *);extern unsigned parse_whitespace_rule (const char *);
diff.c +75 -41
189 lines not on this page · raw message
diff.h +3 -3
@@ -148,9 +148,9 @@ struct diff_options {int abbrev;int ita_invisible_in_index;/* white-space error highlighting */ -#define WSEH_NEW 1 -#define WSEH_CONTEXT 2 -#define WSEH_OLD 4 +#define WSEH_NEW (1<<12) +#define WSEH_CONTEXT (1<<13) +#define WSEH_OLD (1<<14)unsigned ws_error_highlight;const char *prefix;int prefix_length;
Signature · 1 line
2.13.0.31.g9b732c453e
PATCH v2 4/25 diff.c: introduce emit_diff_symbol
In reply to Stefan BellerIn a later patch we want to buffer all output before emitting it as a new feature ("markup moved lines") conceptually cannot be implemented in a single pass over the output.
There are different approaches to buffer all output such as:
* Buffering on the char level, i.e. we'd have a char[] which would grow at approximately 80 characters a line. This would keep the output completely unstructured, but might be very easy to implement, such as redirecting all output to a temporary file and working off that. The later passes over the buffer are quite complicated though, because we have to parse back any output and then decide if it should be modified.
* Buffer on a line level. As the output is mostly line oriented already, this would make sense, but it still is a bit awkward as we'd have to make sense of it again by looking at the first characters of a line to decide what part of a diff a line is.
* Buffer semantically. Imagine there is a formal grammar for the diff output and we'd keep the symbols of this grammar around. This keeps the highest level of structure in the buffered data, such that the actual memory requirements are less than say the first option. Instead of buffering the characters of the line, we'll buffer what we intend to do plus additional information for the specifics. An output of
diff --git a/new.txt b/new.txt
index fa69b07..412428c 100644
Binary files a/new.txt and b/new.txt differ could be buffered as
DIFF_SYMBOL_DIFF_START + new.txt DIFF_SYMBOL_INDEX_MODE + fa69b07 412428c "non-executable" flag DIFF_SYMBOL_BINARY_FILES + new.txt
This and the following patches introduce the third option of buffering by first moving any output to emit_diff_symbol, and then introducing the buffering in this function.
--- diff.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-)
diff.c +19 -3
@@ -560,6 +560,24 @@ static void emit_line(struct diff_options *o, const char *set, const char *resetemit_line_0 (o, set, reset, line[0 ], line+1 , len-1 ); } +enum diff_symbol { +DIFF_SYMBOL_SEPARATOR +}; + +static void emit_diff_symbol (struct diff_options *o,enum diff_symbol s, +const char *line,int len) +{ +switch (s) { +case DIFF_SYMBOL_SEPARATOR : +fprintf (o->file,"%s%c" , +diff_line_prefix (o), + o->line_termination); +break ; +default : +die ("BUG: unknown diff symbol" ); + } +} +static int new_blank_line_at_eof (struct emit_callback *ecbdata,const char *line,int len) { if (!((ecbdata->ws_rule & WS_BLANK_AT_EOF) &&
@@ -4828,9 +4846,7 @@ void diff_flush(struct diff_options *options)if (output_format &DIFF_FORMAT_PATCH ) {if (separator) { -fprintf (options->file,"%s%c" , -diff_line_prefix (options), - options->line_termination); +emit_diff_symbol (options,DIFF_SYMBOL_SEPARATOR ,NULL ,0 );if (options->stat_sep) {/* attach patch instead of inline */ fputs (options->stat_sep, options->file);
Signature · 1 line
2.13.0.31.g9b732c453e
Re: [PATCHv2 22/25] diff.c: color moved lines differently
In reply to Stefan BellerQuoted 6 lines · Stefan Beller
diff.c +6
-0+static int next_byte (const char **cp,const char **endp, +const struct diff_options *diffopt) +{ +int retval; + +if (*cp > *endp)
diff.c static void append_emitted_diff_symbol(struct diff_options *o, · line 713
Quoted 10 lines
diff.c +10
-0+return -1 ; + +if (DIFF_XDL_TST (diffopt,IGNORE_WHITESPACE_CHANGE )) { +while (*cp < *endp &&isspace (**cp)) + (*cp)++; +/* +* After skipping a couple of whitespaces, we still have to +* account for one space. +*/ +return (int )' ' ;
Do we need a cast here?
diff.c static void append_emitted_diff_symbol(struct diff_options *o, · line 765
Quoted 10 lines
diff.c +10
-0+static unsigned get_string_hash (struct emitted_diff_symbol *es,struct diff_options *o) +{ +if (o->xdl_opts &XDF_WHITESPACE_FLAGS ) { +static struct strbuf sb =STRBUF_INIT ; +const char *ap = es->line, *ae = es->line + es->len; +int c; + +strbuf_reset (&sb); +while (ae > ap &&isspace (*ae)) + ae--;
Not testing for the AT_EOL option here?
It does not make a difference in correctness; two lines that differ only by their trailing whitespaces will be hashed into the same bin even when we are not using ignore-whitespace-at-eol, making the hashmap a bit less efficient than necessary.
By the way, this is an unrelated tangent because I think you inherited this pattern by copying and pasting from elsewhere, but I think it would be better if we avoid casting the function pointer type like this:
diff.c static void diff_flush_patch_all_file_pairs(struct diff_options *o) · line 5413
Quoted 7 lines
diff.c +7
-0+if (o->color_moved) { +struct hashmap add_lines, del_lines; + +hashmap_init (&del_lines, + (hashmap_cmp_fn )moved_entry_cmp, o,0 ); +hashmap_init (&add_lines, + (hashmap_cmp_fn )moved_entry_cmp, o,0 );
When hashmap_cmp_fn's definition changes, these two calling sites won't be caught as passing a incorrectly typed callback function by the compiler.
Instead, we can match the actual implementation of the callback function, e.g.
diff.c static void append_emitted_diff_symbol(struct diff_options *o, · line 731
Quoted 5 lines
diff.c +5
-0+static int moved_entry_cmp (const struct diff_options *diffopt, +const struct moved_entry *a, +const struct moved_entry *b, +const void *keydata) +{
to the expected function type, i.e.
static int moved_entry_cmp(const void *fndata,
const void *entry, const void *entry_or_key, const void *keydata)
{
const struct diff_options *diffopt = fndata;
const struct moved_entry *a = entry;
const struct moved_entry *b = entry_or_key;
...by casting the parameters.
Re: [PATCHv2 22/25] diff.c: color moved lines differently
Quoted 3 lines · Junio C Hamano
Quoted 1 line
+ return (int)' ';
Do we need a cast here?
No, I figured it is good to have it here explicitly, though. We can drop that if you have strong preferences one way or another.
Quoted 13 lines
diff.c static void append_emitted_diff_symbol(struct diff_options *o, · line 765
Quoted 11 lines
diff.c +10
-0+static unsigned get_string_hash (struct emitted_diff_symbol *es,struct diff_options *o) +{ +if (o->xdl_opts &XDF_WHITESPACE_FLAGS ) { +static struct strbuf sb =STRBUF_INIT ; +const char *ap = es->line, *ae = es->line + es->len; +int c; + +strbuf_reset (&sb); +while (ae > ap &&isspace (*ae)) + ae--;Not testing for the
AT_EOLoption here?
No, because the other options are stronger than the AT_EOL, such that as you note it is still correct.
If in the future, we'd have another new option e.g. IGNORE_TAB_BLANK_CONVERSION_BUT_WARN_ON_LENGTH_DIFF (useful for python programmers ;)
this would break.
Quoted 32 lines
By the way, this is an unrelated tangent because I think you inherited this pattern by copying and pasting from elsewhere, but I think it would be better if we avoid casting the function pointer type like this:
diff.c static void diff_flush_patch_all_file_pairs(struct diff_options *o) · line 5413
Quoted 7 lines
diff.c +7
-0+if (o->color_moved) { +struct hashmap add_lines, del_lines; + +hashmap_init (&del_lines, + (hashmap_cmp_fn )moved_entry_cmp, o,0 ); +hashmap_init (&add_lines, + (hashmap_cmp_fn )moved_entry_cmp, o,0 );When hashmap_cmp_fn's definition changes, these two calling sites won't be caught as passing a incorrectly typed callback function by the compiler.
Instead, we can match the actual implementation of the callback function, e.g.
diff.c static void append_emitted_diff_symbol(struct diff_options *o, · line 731
Quoted 5 lines
diff.c +5
-0+static int moved_entry_cmp (const struct diff_options *diffopt, +const struct moved_entry *a, +const struct moved_entry *b, +const void *keydata) +{to the expected function type, i.e.
static int moved_entry_cmp(const void *fndata,
const void *entry, const void*entry_or_key, const void *keydata)
Quoted 7 lines
{ const struct diff_options *diffopt = fndata; const struct moved_entry *a = entry; const struct moved_entry *b = entry_or_key; ...by casting the parameters.
I agree. I can make a cleanup throughout the whole code base, but I would prefer if that is done in a separate series, as this is already slightly lengthy.
Thanks,
Stefan
Re: [PATCHv2 22/25] diff.c: color moved lines differently
Quoted 7 lines · Stefan Beller
Quoted 3 lines · Junio C Hamano
Quoted 1 line
+ return (int)' ';
Do we need a cast here?
No, I figured it is good to have it here explicitly, though. We can drop that if you have strong preferences one way or another.
I was merely wondering if you were protecting against a funny platform where ' ' is a negative number ;-)
Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
diff.c +4
-0+static unsigned get_string_hash (struct emitted_diff_symbol *es,struct diff_options *o) +{ +if (o->xdl_opts &XDF_WHITESPACE_FLAGS ) { +static struct strbuf sb =STRBUF_INIT ;Quoted 8 lines
diff.c static void append_emitted_diff_symbol(struct diff_options *o, · line 765
Quoted 6 lines
diff.c +6
-0+const char *ap = es->line, *ae = es->line + es->len; +int c; + +strbuf_reset (&sb); +while (ae > ap &&isspace (*ae)) + ae--;Not testing for the
AT_EOLoption here?
Quoted 7 lines
No, because the other options are stronger than the
AT_EOL, such that as you note it is still correct.If in the future, we'd have another new option e.g.
IGNORE_TAB_BLANK_CONVERSION_BUT_WARN_ON_LENGTH_DIFF(useful for python programmers ;)
this would break.
That remark makes it sound as if this is a timebomb ticking, but I do not think that is the case. This is merely stripping whitespaces at the end; mixing tabs and spaces without changing the indentation level matters only when you have something that is !isspace() to indent to begin with ;-)
So, the effect of not checking is only a hashmap that is bit less efficient than necessary, but is there an undue cost of actually checking and doing this skipping only when we are ignoring the whitespaces at the end of lines?
Quoted 5 lines
Quoted 1 line
By the way, this is an unrelated tangent because I think you
...
I agree. I can make a cleanup throughout the whole code base, but I would prefer if that is done in a separate series, as this is already slightly lengthy.
Oh, I 100% agree that it is an unrelated tangent.
This (obviously) passes the failing tests.
I should have spotted why the rebase of the previous one did not work myself before bugging you X-<.
Thanks. Queued.