From: Stefan Beller Date: Tue, 24 Oct 2017 00:09:31 GMT Subject: [WIP PATCH] diff: add option to ignore whitespaces for move detection only Message-ID: <20171024000931.14814-1-sbeller@google.com> In-Reply-To: Signed-off-by: Stefan Beller --- diff.c | 10 ++-- diff.h | 1 + t/t4015-diff-whitespace.sh | 114 ++++++++++++++++++++++++++++++++++++++++++++- See, only 10 lines of code! (and a few more for tests) We have run out of space in diff_options.flags,touched_flags. as we 1<color_moved_ignore_space) { while (*cp < *endp && isspace(**cp)) (*cp)++; /* @@ -751,7 +752,8 @@ static int moved_entry_cmp(const struct diff_options *diffopt, 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)) + if (!(diffopt->xdl_opts & XDF_WHITESPACE_FLAGS) && + !diffopt->color_moved_ignore_space) return a->es->len != b->es->len || memcmp(ap, bp, a->es->len); if (DIFF_XDL_TST(diffopt, IGNORE_WHITESPACE_AT_EOL)) { @@ -774,7 +776,7 @@ static int moved_entry_cmp(const struct diff_options *diffopt, static unsigned get_string_hash(struct emitted_diff_symbol *es, struct diff_options *o) { - if (o->xdl_opts & XDF_WHITESPACE_FLAGS) { + if ((o->xdl_opts & XDF_WHITESPACE_FLAGS) || o->color_moved_ignore_space) { static struct strbuf sb = STRBUF_INIT; const char *ap = es->line, *ae = es->line + es->len; int c; @@ -4660,6 +4662,8 @@ int diff_opt_parse(struct diff_options *options, DIFF_XDL_CLR(options, NEED_MINIMAL); else if (!strcmp(arg, "-w") || !strcmp(arg, "--ignore-all-space")) DIFF_XDL_SET(options, IGNORE_WHITESPACE); + else if (!strcmp(arg, "--ignore-all-space-in-move-detection")) + options->color_moved_ignore_space = 1; else if (!strcmp(arg, "-b") || !strcmp(arg, "--ignore-space-change")) DIFF_XDL_SET(options, IGNORE_WHITESPACE_CHANGE); else if (!strcmp(arg, "--ignore-space-at-eol")) diff --git a/diff.h b/diff.h index aca150ba2e..6ba3f53bbd 100644 --- a/diff.h +++ b/diff.h @@ -196,6 +196,7 @@ struct diff_options { } color_moved; #define COLOR_MOVED_DEFAULT COLOR_MOVED_ZEBRA #define COLOR_MOVED_MIN_ALNUM_COUNT 20 + int color_moved_ignore_space; }; void diff_emit_submodule_del(struct diff_options *o, const char *line); diff --git a/t/t4015-diff-whitespace.sh b/t/t4015-diff-whitespace.sh index 6c9a93b734..d7ee3aabf2 100755 --- a/t/t4015-diff-whitespace.sh +++ b/t/t4015-diff-whitespace.sh @@ -1677,7 +1677,119 @@ test_expect_success 'move detection with submodules' ' # nor did we mess with it another way git diff --submodule=diff --color | test_decode_color >expect && - test_cmp expect decoded_actual + test_cmp expect decoded_actual && + rm -rf bananas && + git submodule deinit bananas +' + +test_expect_success 'move detection only ignores white spaces' ' + git reset --hard && + q_to_tab <<-\EOF >function.c && + int func() + { + Qif (foo) { + QQ// this part of the function + QQ// function will be very long + QQ// indeed. We must exceed both + QQ// per-line and number of line + QQ// minimums + QQ; + Q} + Qbaz(); + Qbar(); + Q// more unrelated stuff + } + EOF + git add function.c && + git commit -m "add function.c" && + q_to_tab <<-\EOF >function.c && + int do_foo() + { + Q// this part of the function + Q// function will be very long + Q// indeed. We must exceed both + Q// per-line and number of line + Q// minimums + Q; + } + + int func() + { + Qif (foo) + QQdo_foo(); + Qbaz(); + Qbar(); + Q// more unrelated stuff + } + EOF + + # Make sure we get a different diff using -w ("moved function header") + git diff --color --color-moved -w | + grep -v "index" | + test_decode_color >actual && + q_to_tab <<-\EOF >expected && + diff --git a/function.c b/function.c + --- a/function.c + +++ b/function.c + @@ -1,6 +1,5 @@ + -int func() + +int do_foo() + { + - if (foo) { + Q// this part of the function + Q// function will be very long + Q// indeed. We must exceed both + @@ -8,6 +7,11 @@ int func() + Q// minimums + Q; + } + + + +int func() + +{ + +Qif (foo) + +QQdo_foo(); + Qbaz(); + Qbar(); + Q// more unrelated stuff + EOF + test_cmp expected actual && + + # And now ignoring white space only in the move detection + git diff --color --color-moved --ignore-all-space-in-move-detection | + grep -v "index" | + test_decode_color >actual && + q_to_tab <<-\EOF >expected && + diff --git a/function.c b/function.c + --- a/function.c + +++ b/function.c + @@ -1,13 +1,17 @@ + +int do_foo() + +{ + +Q// this part of the function + +Q// function will be very long + +Q// indeed. We must exceed both + +Q// per-line and number of line + +Q// minimums + +Q; + +} + + + int func() + { + -Qif (foo) { + -QQ// this part of the function + -QQ// function will be very long + -QQ// indeed. We must exceed both + -QQ// per-line and number of line + -QQ// minimums + -QQ; + -Q} + +Qif (foo) + +QQdo_foo(); + Qbaz(); + Qbar(); + Q// more unrelated stuff + EOF + test_cmp expected actual ' test_done -- 2.15.0.rc2.6.g953226eb5f