diff: add --ignore-blank-lines option
PATCH 1/1 diff: add --ignore-blank-lines option
The goal of the patch is to introduce the GNU diff -B/--ignore-blank-lines as closely as possible. The short option is not available because it's already used for "break-rewrites".
When this option is used, git-diff will not create hunks that simply adds or removes empty lines, but will still show empty lines addition/suppression if they are close enough to "valuable" changes.
--- Documentation/diff-options.txt | 3 ++ diff.c | 2 ++ t/t4015-diff-whitespace.sh | 65 ++++++++++++++++++++++++++++++++++++++++ xdiff/xdiff.h | 2 ++ xdiff/xdiffi.c | 29 +++++++++++++++++- xdiff/xdiffi.h | 1 + xdiff/xemit.c | 32 ++++++++++++++++---- xdiff/xemit.h | 2 +- xdiff/xutils.c | 13 ++++++++ xdiff/xutils.h | 1 + 10 files changed, 143 insertions(+), 7 deletions(-)
Documentation/diff-options.txt +3 -0
@@ -439,6 +439,9 @@ endif::git-format-patch[] differences even if one line has whitespace where the other line has none. +--ignore-blank-lines:: + Ignore changes whose lines are all blank. + --inter-hunk-context=<lines>:: Show the context between diff hunks, up to the specified number of lines, thereby fusing hunks that are close to each other.
diff.c +2 -0
@@ -3593,6 +3593,8 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)DIFF_XDL_SET (options,IGNORE_WHITESPACE_CHANGE ); else if (!strcmp(arg, "--ignore-space-at-eol"))DIFF_XDL_SET (options,IGNORE_WHITESPACE_AT_EOL ); + else if (!strcmp(arg, "--ignore-blank-lines")) +DIFF_XDL_SET (options,IGNORE_BLANK_LINES ); else if (!strcmp(arg, "--patience")) options->xdl_opts =DIFF_WITH_ALG (options,PATIENCE_DIFF ); else if (!strcmp(arg, "--histogram"))
t/t4015-diff-whitespace.sh +65 -0
71 lines not on this page · raw message
xdiff/xdiff.h +2 -0
@@ -39,6 +39,8 @@ extern "C" {#define XDF_DIFF_ALGORITHM_MASK (XDF_PATIENCE_DIFF | XDF_HISTOGRAM_DIFF)#define XDF_DIFF_ALG (x) ((x) & XDF_DIFF_ALGORITHM_MASK) +#define XDF_IGNORE_BLANK_LINES (1 << 7) +#define XDL_EMIT_FUNCNAMES (1 << 0)#define XDL_EMIT_COMMON (1 << 1)#define XDL_EMIT_FUNCCONTEXT (1 << 2)
xdiff/xdiffi.c +28 -1
@@ -394,6 +394,7 @@ static xdchange_t *xdl_add_change(xdchange_t *xscr, long i1, long i2, long chg1, xch->i2 = i2; xch->chg1 = chg1; xch->chg2 = chg2; + xch->ignore =0 ;return xch; }
@@ -544,7 +545,9 @@ static int xdl_call_hunk_func(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,xdchange_t *xch, *xche;for (xch = xscr; xch; xch = xche->next) { - xche =xdl_get_hunk (xch, xecfg); + xche =xdl_get_hunk (&xch, xecfg); +if (!xch) +break ;if (xecfg->hunk_func (xch->i1, xche->i1 + xche->chg1 - xch->i1, xch->i2, xche->i2 + xche->chg2 - xch->i2, ecb->priv) <0 )
@@ -553,6 +556,27 @@ static int xdl_call_hunk_func(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,return 0 ; } +static void xdl_mark_ignorable (xdchange_t *xscr,xdfenv_t *xe,long flags) +{ +xdchange_t *xch; + +for (xch = xscr; xch; xch = xch->next) { +int ignore =1 ; +xrecord_t **rec; +long i; + + rec = &xe->xdf1.recs[xch->i1]; +for (i =0 ; i < xch->chg1 && ignore; i++) + ignore =xdl_blankline (rec[i]->ptr, flags); + + rec = &xe->xdf2.recs[xch->i2]; +for (i =0 ; i < xch->chg2 && ignore; i++) + ignore =xdl_blankline (rec[i]->ptr, flags); + + xch->ignore = ignore; + } +} +int xdl_diff (mmfile_t *mf1,mmfile_t *mf2,xpparam_t const *xpp,xdemitconf_t const *xecfg,xdemitcb_t *ecb) {xdchange_t *xscr;
@@ -571,6 +595,9 @@ int xdl_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,return -1 ; }if (xscr) { +if (xpp->flags &XDF_IGNORE_BLANK_LINES ) +xdl_mark_ignorable (xscr, &xe, xpp->flags); +if (ef (&xe, xscr, ecb, xecfg) <0 ) {xdl_free_script (xscr);
xdiff/xdiffi.h +1 -0
@@ -41,6 +41,7 @@ typedef struct s_xdchange {struct s_xdchange *next;long i1, i2;long chg1, chg2; +int ignore; } xdchange_t;
xdiff/xemit.c +27 -5
@@ -56,14 +56,34 @@ static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t */* * Starting at the passed change atom, find the latest change atom to be included * inside the differential hunk according to the specified configuration. +* Also advance xscr if the first changes must be discareded. */ -xdchange_t *xdl_get_hunk (xdchange_t *xscr,xdemitconf_t const *xecfg) { +xdchange_t *xdl_get_hunk (xdchange_t **xscr,xdemitconf_t const *xecfg) {xdchange_t *xch, *xchp;long max_common =2 * xecfg->ctxlen + xecfg->interhunkctxlen; +long ignorable_context = max_common /2 -1 ; +int interesting =0 ; -for (xchp = xscr, xch = xscr->next; xch; xchp = xch, xch = xch->next) -if (xch->i1 - (xchp->i1 + xchp->chg1) > max_common) -break ; +for (xchp = *xscr, xch = (*xscr)->next; xch; xchp = xch, xch = xch->next) { +long thresh; +if (xchp->ignore || xch->ignore) + thresh = ignorable_context; +else + thresh = max_common; + +if (!xchp->ignore) + interesting =1 ; + +if (xch->i1 - (xchp->i1 + xchp->chg1) > thresh) { +if (interesting) +break ; +else + *xscr = xch; + } + } + +if (!interesting && xchp->ignore) + *xscr =NULL ;return xchp; }
@@ -139,7 +159,9 @@ int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,return xdl_emit_common (xe, xscr, ecb, xecfg);for (xch = xscr; xch; xch = xche->next) { - xche =xdl_get_hunk (xch, xecfg); + xche =xdl_get_hunk (&xch, xecfg); +if (!xch) +break ; s1 =XDL_MAX (xch->i1 - xecfg->ctxlen,0 ); s2 =XDL_MAX (xch->i2 - xecfg->ctxlen,0 );
xdiff/xemit.h +1 -1
@@ -27,7 +27,7 @@typedef int (*emit_func_t )(xdfenv_t *xe,xdchange_t *xscr,xdemitcb_t *ecb,xdemitconf_t const *xecfg); -xdchange_t *xdl_get_hunk (xdchange_t *xscr,xdemitconf_t const *xecfg); +xdchange_t *xdl_get_hunk (xdchange_t **xscr,xdemitconf_t const *xecfg);int xdl_emit_diff (xdfenv_t *xe,xdchange_t *xscr,xdemitcb_t *ecb,xdemitconf_t const *xecfg);
xdiff/xutils.c +13 -0
@@ -143,6 +143,19 @@ long xdl_guess_lines(mmfile_t *mf, long sample) {return nl +1 ; } +int xdl_blankline (const char *line,long flags) +{ +long i; + +if (!(flags &XDF_WHITESPACE_FLAGS )) +return (*line =='\n' ); + +for (i =0 ; line[i] !='\n' &&XDL_ISSPACE (line[i]); i++) + ; + +return (line[i] =='\n' ); +} +int xdl_recmatch (const char *l1,long s1,const char *l2,long s2,long flags) {int i1, i2;
xdiff/xutils.h +1 -0
@@ -32,6 +32,7 @@ int xdl_cha_init(chastore_t *cha, long isize, long icount);void xdl_cha_free (chastore_t *cha);void *xdl_cha_alloc (chastore_t *cha);long xdl_guess_lines (mmfile_t *mf,long sample); +int xdl_blankline (const char *line,long flags);int xdl_recmatch (const char *l1,long s1,const char *l2,long s2,long flags);unsigned long xdl_hash_record (char const **data,char const *top,long flags);unsigned int xdl_hashbits (unsigned int size);
--
1.7.9.5
Am 26.05.2013 19:58, schrieb Antoine Pelisse:
Quoted 7 lines
The goal of the patch is to introduce the GNU diff -B/--ignore-blank-lines as closely as possible. The short option is not available because it's already used for "break-rewrites".
When this option is used, git-diff will not create hunks that simply adds or removes empty lines, but will still show empty lines addition/suppression if they are close enough to "valuable" changes.
So when an addition or removal of a blank line appears in a hunk that also has non-blank-line changes, the addition or removal is not treated specially?
How is a blank line defined? What happens if a line that has only whitespace is added or removed? I'm thinking of diffs of files with CRLF line breaks, where the CR would count as whitespace in the line, I think.
Documentation/diff-options.txt endif::git-format-patch[] · line 443
Quoted 2 lines
+--ignore-blank-lines::
Documentation/diff-options.txt +1
-0+ Ignore changes whose lines are all blank.
I think this is too terse and does not convey what the option really does.
Review
Quoted 2 lines
(quoted diff) +2
-0+test_expect_success 'ignore-blank-lines: only new lines' ' + seq 5 >x &&
Please use test_seq instead of seq in all new tests.
-- Hannes
Quoted 5 lines · Johannes Sixt
Am 26.05.2013 19:58, schrieb Antoine Pelisse:
Quoted 4 lines
The goal of the patch is to introduce the GNU diff -B/--ignore-blank-lines as closely as possible. The short option is not available because it's already used for "break-rewrites".
Quoted 7 lines
Quoted 3 lines
When this option is used, git-diff will not create hunks that simply adds or removes empty lines, but will still show empty lines addition/suppression if they are close enough to "valuable" changes.
So when an addition or removal of a blank line appears in a hunk that also has non-blank-line changes, the addition or removal is not treated specially?
Exactly.
Quoted 2 lines
How is a blank line defined? What happens if a line that has only whitespace is added or removed?
xdl_blankline() is the best description of what I considered a blank line. If no --ignore-space-* option is given, it's a line that starts and ends with '\n'.
If any --ignore-space-* option is given, it's a line that has any number of isspace(3)-defined characters, followed by '\n'.
Quoted 1 line
I'm thinking of diffs of files with CRLF
Good you did, because I didn't ;-)
Quoted 1 line
line breaks, where the CR would count as whitespace in the line, I think.
With the current implementation, an empty line with CRLF will not show as a blank line if no space option is given. As CR is a space according to isspace(3), the line will be removed with any space option.
Quoted 4 lines
Quoted 2 lines
+--ignore-blank-lines:: + Ignore changes whose lines are all blank.I think this is too terse and does not convey what the option really does.
That's the description from GNU diff man page. But indeed it could be more precise.
Quoted 4 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+test_expect_success 'ignore-blank-lines: only new lines' ' + seq 5 >x &&Please use
test_seqinstead of seq in all new tests.
Will fix.
Quoted 2 lines
-- Hannes
Quoted 11 lines · Antoine Pelisse
Quoted 1 line · Johannes Sixt
I'm thinking of diffs of files with CRLF
Good you did, because I didn't ;-)
Quoted 1 line
line breaks, where the CR would count as whitespace in the line, I think.
With the current implementation, an empty line with CRLF will not show as a blank line if no space option is given. As CR is a space according to isspace(3), the line will be removed with any space option.
Maybe it would be worth adding the diff(1) "--strip-trailing-cr" option. So that you could remove empty lines if you have dos line endings while caring about eol space-changes (or other space changes).
xdiff/xemit.c static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t * · line 64
Quoted 4 lines · Antoine Pelisse
xdiff/xemit.c +2
-0+xdchange_t *xdl_get_hunk (xdchange_t **xscr,xdemitconf_t const *xecfg) {xdchange_t *xch, *xchp;long max_common =2 * xecfg->ctxlen + xecfg->interhunkctxlen; +long ignorable_context = max_common /2 -1 ;
Could you explain how this math works? Also the logic to use it when either the previous or the current one is "blank only" (as opposed to having two different settings for "both are blank only" and "only one of them is, and the other is not", for example)?
The normal case when neither is blank only, we refrain from collapsing two adjacent xdchanges if the end of xchp (i.e. the previous one) is before the beginning of xch (i.e. the current one we are looking at) by more than max_common lines, which makes sense to me because we count one ctxlen for the trailing context for xchp, interhunkctxlen to force collapsing, and another ctxlen for the leading context for xch.
When we have
- zero or more "blank only" changes, followed by
- a meaningful change A, followed by
- zero or more blank-only changes, followed by
- a meaningful change C,
we may want to have either two hunks (A with context around it, and C with context around it) or a single hunk (precontext before A, all the lines from the beginning of A to the end of C, and postcontext after C). In either case, we want to discard the leading "blank only" changes.
I can sort-of see how the leading "blank only" changes are discarded in the loop (but not quite---you can ignore everything without any "thresh", until you set "interesting" to true, i.e. seeing A, no?).
It is not clear to me how you are counting the distance between the end of A and the beginning of C, which I think is all that matters, to make the decision to coalesce (or not to coalesce) the above into a single hunk, without looking ahead to find the next xdchange that is not marked with xch->ignore flag (that is, when looking at A, find the beginning of C to see if C.begin-A.end is within the usual max_common).
Confused...
Quoted 90 lines
xdiff/xemit.c +24
-4+int interesting =0 ; -for (xchp = xscr, xch = xscr->next; xch; xchp = xch, xch = xch->next) -if (xch->i1 - (xchp->i1 + xchp->chg1) > max_common) -break ; +for (xchp = *xscr, xch = (*xscr)->next; xch; xchp = xch, xch = xch->next) { +long thresh; +if (xchp->ignore || xch->ignore) + thresh = ignorable_context; +else + thresh = max_common; + +if (!xchp->ignore) + interesting =1 ; + +if (xch->i1 - (xchp->i1 + xchp->chg1) > thresh) { +if (interesting) +break ; +else + *xscr = xch; + } + } + +if (!interesting && xchp->ignore) + *xscr =NULL ;return xchp; }@@ -139,7 +159,9 @@ int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,return xdl_emit_common (xe, xscr, ecb, xecfg);for (xch = xscr; xch; xch = xche->next) { - xche =xdl_get_hunk (xch, xecfg); + xche =xdl_get_hunk (&xch, xecfg); +if (!xch) +break ; s1 =XDL_MAX (xch->i1 - xecfg->ctxlen,0 ); s2 =XDL_MAX (xch->i2 - xecfg->ctxlen,0 );xdiff/xemit.h +1
-1@@ -27,7 +27,7 @@typedef int (*emit_func_t )(xdfenv_t *xe,xdchange_t *xscr,xdemitcb_t *ecb,xdemitconf_t const *xecfg); -xdchange_t *xdl_get_hunk (xdchange_t *xscr,xdemitconf_t const *xecfg); +xdchange_t *xdl_get_hunk (xdchange_t **xscr,xdemitconf_t const *xecfg);int xdl_emit_diff (xdfenv_t *xe,xdchange_t *xscr,xdemitcb_t *ecb,xdemitconf_t const *xecfg);xdiff/xutils.c +13
-0@@ -143,6 +143,19 @@ long xdl_guess_lines(mmfile_t *mf, long sample) {return nl +1 ; } +int xdl_blankline (const char *line,long flags) +{ +long i; + +if (!(flags &XDF_WHITESPACE_FLAGS )) +return (*line =='\n' ); + +for (i =0 ; line[i] !='\n' &&XDL_ISSPACE (line[i]); i++) + ; + +return (line[i] =='\n' ); +} +int xdl_recmatch (const char *l1,long s1,const char *l2,long s2,long flags) {int i1, i2;xdiff/xutils.h +1
-1@@ -32,6 +32,7 @@ int xdl_cha_init(chastore_t *cha, long isize, long icount);void xdl_cha_free (chastore_t *cha);void *xdl_cha_alloc (chastore_t *cha);long xdl_guess_lines (mmfile_t *mf,long sample); +int xdl_blankline (const char *line,long flags);int xdl_recmatch (const char *l1,long s1,const char *l2,long s2,long flags);unsigned long xdl_hash_record (char const **data,char const *top,long flags);unsigned int xdl_hashbits (unsigned int size); --1.7.9.5
Quoted 8 lines · Junio C Hamano
xdiff/xemit.c static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t * · line 64
Quoted 4 lines · Antoine Pelisse
xdiff/xemit.c +2
-0+xdchange_t *xdl_get_hunk (xdchange_t **xscr,xdemitconf_t const *xecfg) {xdchange_t *xch, *xchp;long max_common =2 * xecfg->ctxlen + xecfg->interhunkctxlen; +long ignorable_context = max_common /2 -1 ;Could you explain how this math works?
I think it doesn't, mostly because I misinterpreted the "interhunkctxlen". I will try to think about that and provide a reroll.
Thanks for the review and analysis.
Antoine,
Quoted 8 lines · Antoine Pelisse
Quoted 7 lines · Junio C Hamano
Quoted 4 lines · Antoine Pelisse
xdiff/xemit.c +2
-0+xdchange_t *xdl_get_hunk (xdchange_t **xscr,xdemitconf_t const *xecfg) {xdchange_t *xch, *xchp;long max_common =2 * xecfg->ctxlen + xecfg->interhunkctxlen; +long ignorable_context = max_common /2 -1 ;
Quoted 4 lines
Quoted 1 line
Could you explain how this math works?
I think it doesn't, mostly because I misinterpreted the "interhunkctxlen". I will try to think about that and provide a reroll.
OK. Thanks.
I think the logic would be more like:
1. Start from xscr, find the first xchp that is !xchp->ignore; if there is none, we are done. There is no more to show.
2. Remember the xchp as the beginning.
3. Tangle ->next pointer to find the next xch that is !xch->ignore; if there is none, we are also done. xdchanges between the beginning you remembered in the step 2. and your current xchp are the only things we want to show.
4. Measure the distance between the end of xchp and the beginning of xch.
- If it is larger than max_common, xdchanges between the beginning you remembered in the step 2. and your current xchp are the only things we want to show. The next iteration will start by skipping the blank-only changes between xchp and xch.
- If it is short enough, assign xchp = xch and go back to 3. to find more interesting hunks (that is why we remembered the real "beginning" in step 2.).
Quoted 17 lines · Junio C Hamano
OK. Thanks.
I think the logic would be more like:
1. Start from xscr, find the first xchp that is !xchp->ignore; if there is none, we are done. There is no more to show.
2. Remember the xchp as the beginning.
3. Tangle ->next pointer to find the next xch that is !xch->ignore; if there is none, we are also done. xdchanges between the beginning you remembered in the step 2. and your current xchp are the only things we want to show.
4. Measure the distance between the end of xchp and the beginning of xch.
Quoted 8 lines
- If it is larger than
max_common, xdchanges between the beginning you remembered in the step 2. and your current xchp are the only things we want to show. The next iteration will start by skipping the blank-only changes between xchp and xch.- If it is short enough, assign xchp = xch and go back to 3. to find more interesting hunks (that is why we remembered the real "beginning" in step 2.).
Yeah, I'm doing something pretty much like that right now (though I will have to eventually sleep).
I decided that it would indeed be easier to split the logic rather than do everything in one loop.
Thanks for the help !
PATCH 1/1 diff: add --ignore-blank-lines option
In reply to Junio C HamanoThe goal of the patch is to introduce the GNU diff -B/--ignore-blank-lines as closely as possible. The short option is not available because it's already used for "break-rewrites".
When this option is used, git-diff will not create hunks that simply adds or removes empty lines, but will still show empty lines addition/suppression if they are close enough to "valuable" changes.
There are two differences between this option and GNU diff -B option:
- GNU diff doesn't have "--inter-hunk-context", so this must be handled
- The following sequence looks like a bug (context is displayed twice):
$ seq 5 >file1
$ cat <<EOF >file2
change
1
2 3
4
5
change
EOF
$ diff -u -B file1 file2
--- file1 2013-06-08 22:13:04.471517834 +0200
+++ file2 2013-06-08 22:13:23.275517855 +0200
@@ -1,5 +1,7 @@
+change
1
2
+
3
4
5
@@ -3,3 +5,4 @@
3
4
5
+changeSo here is a more thorough description of the option:
- real changes are interesting
- blank lines that are close enough (less than context size) to interesting changes are considered interesting (recursive definition)
- "context" lines are used around each hunk of interesting changes
- If two hunks are separated by less than "inter-hunk-context", they will be merged into one.
The current implementation does the "interesting changes selection" in a single pass.
---
Hi,
Quoted 17 lines · Junio C Hamano
OK. Thanks.
I think the logic would be more like:
1. Start from xscr, find the first xchp that is !xchp->ignore; if there is none, we are done. There is no more to show.
2. Remember the xchp as the beginning.
3. Tangle ->next pointer to find the next xch that is !xch->ignore; if there is none, we are also done. xdchanges between the beginning you remembered in the step 2. and your current xchp are the only things we want to show.
4. Measure the distance between the end of xchp and the beginning of xch.
Quoted 8 lines
- If it is larger than
max_common, xdchanges between the beginning you remembered in the step 2. and your current xchp are the only things we want to show. The next iteration will start by skipping the blank-only changes between xchp and xch.- If it is short enough, assign xchp = xch and go back to 3. to find more interesting hunks (that is why we remembered the real "beginning" in step 2.).
Actually it doesn't quite work like that because we don't totally ignore "blank lines". We want to keep them if they are close enough to other changes.
I've tried to improve the number of tests as it helped me during implementation, and to give a better description of the feature.
The initial reroll was meant to simplify xdl_get_hunk() but I'm afraid it became kind of "voodoo code". I'm not sure if I should provide some more comments about it and where.
Please let me know if something is not working as expected.
Cheers, Antoine
Documentation/diff-options.txt | 3 + diff.c | 2 + t/t4015-diff-whitespace.sh | 282 ++++++++++++++++++++++++++++++++++++++++ xdiff/xdiff.h | 2 + xdiff/xdiffi.c | 29 ++++- xdiff/xdiffi.h | 1 + xdiff/xemit.c | 47 ++++++- xdiff/xemit.h | 2 +- xdiff/xutils.c | 13 ++ xdiff/xutils.h | 1 + 10 files changed, 374 insertions(+), 8 deletions(-)
Documentation/diff-options.txt +3 -0
@@ -439,6 +439,9 @@ endif::git-format-patch[] differences even if one line has whitespace where the other line has none. +--ignore-blank-lines:: + Ignore changes whose lines are all blank. + --inter-hunk-context=<lines>:: Show the context between diff hunks, up to the specified number of lines, thereby fusing hunks that are close to each other.
diff.c +2 -0
@@ -3593,6 +3593,8 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)DIFF_XDL_SET (options,IGNORE_WHITESPACE_CHANGE ); else if (!strcmp(arg, "--ignore-space-at-eol"))DIFF_XDL_SET (options,IGNORE_WHITESPACE_AT_EOL ); + else if (!strcmp(arg, "--ignore-blank-lines")) +DIFF_XDL_SET (options,IGNORE_BLANK_LINES ); else if (!strcmp(arg, "--patience")) options->xdl_opts =DIFF_WITH_ALG (options,PATIENCE_DIFF ); else if (!strcmp(arg, "--histogram"))
t/t4015-diff-whitespace.sh +282 -0
288 lines not on this page · raw message
xdiff/xdiff.h +2 -0
@@ -39,6 +39,8 @@ extern "C" {#define XDF_DIFF_ALGORITHM_MASK (XDF_PATIENCE_DIFF | XDF_HISTOGRAM_DIFF)#define XDF_DIFF_ALG (x) ((x) & XDF_DIFF_ALGORITHM_MASK) +#define XDF_IGNORE_BLANK_LINES (1 << 7) +#define XDL_EMIT_FUNCNAMES (1 << 0)#define XDL_EMIT_COMMON (1 << 1)#define XDL_EMIT_FUNCCONTEXT (1 << 2)
xdiff/xdiffi.c +28 -1
@@ -394,6 +394,7 @@ static xdchange_t *xdl_add_change(xdchange_t *xscr, long i1, long i2, long chg1, xch->i2 = i2; xch->chg1 = chg1; xch->chg2 = chg2; + xch->ignore =0 ;return xch; }
@@ -544,7 +545,9 @@ static int xdl_call_hunk_func(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,xdchange_t *xch, *xche;for (xch = xscr; xch; xch = xche->next) { - xche =xdl_get_hunk (xch, xecfg); + xche =xdl_get_hunk (&xch, xecfg); +if (!xch) +break ;if (xecfg->hunk_func (xch->i1, xche->i1 + xche->chg1 - xch->i1, xch->i2, xche->i2 + xche->chg2 - xch->i2, ecb->priv) <0 )
@@ -553,6 +556,27 @@ static int xdl_call_hunk_func(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,return 0 ; } +static void xdl_mark_ignorable (xdchange_t *xscr,xdfenv_t *xe,long flags) +{ +xdchange_t *xch; + +for (xch = xscr; xch; xch = xch->next) { +int ignore =1 ; +xrecord_t **rec; +long i; + + rec = &xe->xdf1.recs[xch->i1]; +for (i =0 ; i < xch->chg1 && ignore; i++) + ignore =xdl_blankline (rec[i]->ptr, rec[i]->size, flags); + + rec = &xe->xdf2.recs[xch->i2]; +for (i =0 ; i < xch->chg2 && ignore; i++) + ignore =xdl_blankline (rec[i]->ptr, rec[i]->size, flags); + + xch->ignore = ignore; + } +} +int xdl_diff (mmfile_t *mf1,mmfile_t *mf2,xpparam_t const *xpp,xdemitconf_t const *xecfg,xdemitcb_t *ecb) {xdchange_t *xscr;
@@ -571,6 +595,9 @@ int xdl_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,return -1 ; }if (xscr) { +if (xpp->flags &XDF_IGNORE_BLANK_LINES ) +xdl_mark_ignorable (xscr, &xe, xpp->flags); +if (ef (&xe, xscr, ecb, xecfg) <0 ) {xdl_free_script (xscr);
xdiff/xdiffi.h +1 -0
@@ -41,6 +41,7 @@ typedef struct s_xdchange {struct s_xdchange *next;long i1, i2;long chg1, chg2; +int ignore; } xdchange_t;
xdiff/xemit.c +41 -6
64 lines not on this page · raw message
xdiff/xemit.h +1 -1
@@ -27,7 +27,7 @@typedef int (*emit_func_t )(xdfenv_t *xe,xdchange_t *xscr,xdemitcb_t *ecb,xdemitconf_t const *xecfg); -xdchange_t *xdl_get_hunk (xdchange_t *xscr,xdemitconf_t const *xecfg); +xdchange_t *xdl_get_hunk (xdchange_t **xscr,xdemitconf_t const *xecfg);int xdl_emit_diff (xdfenv_t *xe,xdchange_t *xscr,xdemitcb_t *ecb,xdemitconf_t const *xecfg);
xdiff/xutils.c +13 -0
@@ -143,6 +143,19 @@ long xdl_guess_lines(mmfile_t *mf, long sample) {return nl +1 ; } +int xdl_blankline (const char *line,long size,long flags) +{ +long i; + +if (!(flags &XDF_WHITESPACE_FLAGS )) +return (size <=1 ); + +for (i =0 ; i < size &&XDL_ISSPACE (line[i]); i++) + ; + +return (i == size); +} +int xdl_recmatch (const char *l1,long s1,const char *l2,long s2,long flags) {int i1, i2;
xdiff/xutils.h +1 -0
@@ -32,6 +32,7 @@ int xdl_cha_init(chastore_t *cha, long isize, long icount);void xdl_cha_free (chastore_t *cha);void *xdl_cha_alloc (chastore_t *cha);long xdl_guess_lines (mmfile_t *mf,long sample); +int xdl_blankline (const char *line,long size,long flags);int xdl_recmatch (const char *l1,long s1,const char *l2,long s2,long flags);unsigned long xdl_hash_record (char const **data,char const *top,long flags);unsigned int xdl_hashbits (unsigned int size);
--
1.7.9.5
Quoted 6 lines · Antoine Pelisse
The goal of the patch is to introduce the GNU diff -B/--ignore-blank-lines as closely as possible. The short option is not available because it's already used for "break-rewrites".
When this option is used, git-diff will not create hunks that simply adds or removes empty lines, but will still show empty lines
s/hunks/a hunk/
...or...
s/adds or removes/add or remove/
xdiff/xemit.c static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t * · line 60
Quoted 12 lines
addition/suppression if they are close enough to "valuable" changes.
xdiff/xemit.c +1
-0@@ -56,16 +56,49 @@ static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t */* * Starting at the passed change atom, find the latest change atom to be included * inside the differential hunk according to the specified configuration. +* Also advance xscr if the first changes must be discareded. */
s/discareded/discarded/
Quoted 31 lines · Antoine Pelisse
The goal of the patch is to introduce the GNU diff -B/--ignore-blank-lines as closely as possible. The short option is not available because it's already used for "break-rewrites".
When this option is used, git-diff will not create hunks that simply adds or removes empty lines, but will still show empty lines addition/suppression if they are close enough to "valuable" changes.
There are two differences between this option and GNU diff -B option:
- GNU diff doesn't have "--inter-hunk-context", so this must be handled
- The following sequence looks like a bug (context is displayed twice):$ seq 5 >file1 $ cat <<EOF >file2 change 1 23 4 5 change EOF $ diff -u -B file1 file2 --- file1 2013-06-08 22:13:04.471517834 +0200 +++ file2 2013-06-08 22:13:23.275517855 +0200 @@ -1,5 +1,7 @@ +change 1 2 +
Quoted 8 lines
3
4
5
@@ -3,3 +5,4 @@
3
4
5
+changeYes, this is a bug in the previous round, and the approach I outlined in the previous message was also designed to address it by coalescing adjacent hunks by measuring the distance correctly.
Quoted 3 lines
Actually it doesn't quite work like that because we don't totally ignore "blank lines". We want to keep them if they are close enough to other changes.
A new test vector in your patch is a good illustration of this.
Quoted 6 lines
t/t4015-diff-whitespace.sh +6
-0+test_expect_success 'ignore-blank-lines: after change' ' + test_seq 7 >x && + git update-index x && + cat <<-\EOF >x && + change + 1
t/t4015-diff-whitespace.sh EOF · line 179
Quoted 10 lines
t/t4015-diff-whitespace.sh +10
-0+ 2 + + 3 + 4 + + 5 + 6 + 7 + + EOF
The test makes the original with 1 thru 7 to the above shape. The argument for the behaviour in this patch is that additions of these new blank lines are close enough to the real change of inserting the first line with "change".
If you are not interested in changes in additions of blank lines (by the way, do we also handle deletions and do your new tests check them?), one could however argue that the user would want not to see the addition of the blank between 4 and 5 or after 7.
At first glance, it seems impossible to express that if we need to show three lines of context, in other words, this output
@@ -1,2 +1,3 @@
+change
1
2cannot be a correct patch output --ignore-blank-lines-change output because it does not show enough context lines after the real change (we want 3 lines).
However, let's step back and think what other ignore blank options do.
When any ignore blank option is used, there will be lines that actually has changes (hence should be shown with +/-) but we deliberately ignore their changes (hence, if they ever appear in the hunk, they do so as context lines prefixed with SP not +/-). When we do so, we show the lines from the postimage in the context.
So in that sense, showing this would actually be acceptable (the last postcontext line in this hunk is a blank line).
@@ -1,3 +1,4 @@
+change
1
2We are showing the new blank line the change added after 2 as a shared context, following the same principle to show from the postimage when we turned a line with a real change into a non-change.
Quoted 9 lines
t/t4015-diff-whitespace.sh +9
-0+ git diff --inter-hunk-context=100 --ignore-blank-lines >out.tmp && + cat <<-\EOF >expected && + diff --git a/x b/x + --- a/x + +++ b/x + @@ -1,7 +1,10 @@ + +change + 1 + 2
t/t4015-diff-whitespace.sh EOF · line 198
Quoted 10 lines
t/t4015-diff-whitespace.sh +10
-0+ + + 3 + 4 + + + 5 + 6 + 7 + EOF + compare_diff_patch expected out.tmp +'
And from that point of view, this expected output may be excessively noisy.
So I dunno.
Quoted 3 lines · Junio C Hamano
by
the way, do we also handle deletions and do your new tests check them?
As stated in the commit message, yes we should, but we don't have tests for that.
I will need to add some as I think I found a bug when removing blank lines.
Quoted 20 lines
Quoted 9 lines
t/t4015-diff-whitespace.sh +9
-0+ git diff --inter-hunk-context=100 --ignore-blank-lines >out.tmp && + cat <<-\EOF >expected && + diff --git a/x b/x + --- a/x + +++ b/x + @@ -1,7 +1,10 @@ + +change + 1 + 2t/t4015-diff-whitespace.sh EOF · line 198
Quoted 10 lines
t/t4015-diff-whitespace.sh +10
-0+ + + 3 + 4 + + + 5 + 6 + 7 + EOF + compare_diff_patch expected out.tmp +'
Quoted 4 lines
And from that point of view, this expected output may be excessively noisy.
So I dunno.
It might be kind of noisy, but I think trying to improve the solution might lead to over-engineering.
How would we compute the "minimal distance between interesting and blank" so that the blank becomes interesting ?
Using the context size for that is quite convenient, while creating another variable would probably become overkill..
The original goal is to remove hunks created solely for addition/suppression, and I think it's what it should do for the moment.
But of course, I have no strong opinion about that.
And by the way, I have also another bug, so you can expect another re-roll (sorry about that, it's more complex than I initially thought).
Thanks a lot,
Antoine
Quoted 10 lines · Antoine Pelisse
It might be kind of noisy, but I think trying to improve the solution might lead to over-engineering.
How would we compute the "minimal distance between interesting and blank" so that the blank becomes interesting ?
Using the context size for that is quite convenient, while creating another variable would probably become overkill..The original goal is to remove hunks created solely for addition/suppression, and I think it's what it should do for the moment.
Something like this on top of your original one is what I had in mind as a starting point.
t/t4015-diff-whitespace.sh | 5 +---- xdiff/xemit.c | 45 ++++++++++++++++++++++++++------------------- 2 files changed, 27 insertions(+), 23 deletions(-)
t/t4015-diff-whitespace.sh +1 -4
@@ -185,7 +185,7 @@ test_expect_success 'ignore-blank-lines: with changes' ' git diff --ignore-blank-lines >out.tmp && sed -e "1,/^+++ b\/x/d" <out.tmp >out && cat <<-\EOF >expect && - @@ -1,6 +2,7 @@ + @@ -1,11 +2,14 @@ 1 2 3
@@ -193,9 +193,6 @@ test_expect_success 'ignore-blank-lines: with changes' ' 4 5 6 - @@ -5,7 +7,9 @@ - 5 - 6 7 + 8
xdiff/xemit.c +26 -19
58 lines not on this page · raw message
Signature · 1 line
1.8.3-477-gc2fede3
Quoted 5 lines · Junio C Hamano
When any ignore blank option is used, there will be lines that actually has changes (hence should be shown with +/-) but we deliberately ignore their changes (hence, if they ever appear in the hunk, they do so as context lines prefixed with SP not +/-). When we do so, we show the lines from the postimage in the context.
Don't we actually use preimage (see below) ? I think using pre-image allows the patch to be applicable to another tree (but ignoring the space changes).
If we actually hide new blank lines that are in the context, it means that we won't be able to apply a patch with 2 new blank lines in the 3 line context.
Anyway, I'm starting to think that "show blank lines changes near other changes" makes sense more and more sense.
By the way I have a patch I *think* is working, but I will check it another thousand times before sending.
Cheers,
Antoine
$ git diff
x +5 -4
@@ -4,8 +4,9 @@ change 3 4 5 -6 -7 -8 -9 + 6 +7 +change + 8 +9 10
$ git diff -w
x +1 -0
@@ -6,6 +6,7 @@ change 5 6 7 +change 8 9 10
Quoted 10 lines · Antoine Pelisse
Quoted 5 lines · Junio C Hamano
When any ignore blank option is used, there will be lines that actually has changes (hence should be shown with +/-) but we deliberately ignore their changes (hence, if they ever appear in the hunk, they do so as context lines prefixed with SP not +/-). When we do so, we show the lines from the postimage in the context.
Don't we actually use preimage (see below) ? I think using pre-image allows the patch to be applicable to another tree (but ignoring the space changes).
But the result of such patch application is not usually what you want to use. If we use postimage (which by the way was a deliberate design decision we made earlier), at least the review of the patch is easier because you would see the end result more clearly.
Quoted 3 lines
If we actually hide new blank lines that are in the context, it means that we won't be able to apply a patch with 2 new blank lines in the 3 line context.
Yes, but I do not think the point of --ignore-blank-lines is to produce a patch that can be applied in the first place. It is to allow easier eyeballing.
Quoted 2 lines
Anyway, I'm starting to think that "show blank lines changes near other changes" makes sense more and more sense.
Probably.
Quoted 9 lines · Junio C Hamano
Quoted 7 lines · Antoine Pelisse
Quoted 5 lines · Junio C Hamano
When any ignore blank option is used, there will be lines that actually has changes (hence should be shown with +/-) but we deliberately ignore their changes (hence, if they ever appear in the hunk, they do so as context lines prefixed with SP not +/-). When we do so, we show the lines from the postimage in the context.
Quoted 3 lines
Quoted 3 lines
Don't we actually use preimage (see below) ? I think using pre-image allows the patch to be applicable to another tree (but ignoring the space changes).
Answering to myself: OK, my package version of git is 1.7.9.5 while the post-image is used since 1.7.10 or something. That explains my confusion.
Quoted 4 lines
But the result of such patch application is not usually what you want to use. If we use postimage (which by the way was a deliberate design decision we made earlier), at least the review of the patch is easier because you would see the end result more clearly.
I've found the patch and discussion [1] about that switch from pre-image to post-image, so I can understand the motives (and see that you actually considered problems for applying such a patch). I always felt confident that running "git send-email -w" would send a patch (that can be applied) without the potential space errors/changes I would have added.
I think it's unfortunate that Git does generate patches with git-diff that can't be applied if any space option is used. I'm still not really convinced by the pre-image to post-image change, and maybe I would have made it a non-default option. What is done is done, but I'd rather like not do the same here, if possible.
Quoted 7 lines
Quoted 3 lines
If we actually hide new blank lines that are in the context, it means that we won't be able to apply a patch with 2 new blank lines in the 3 line context.
Yes, but I do not think the point of --ignore-blank-lines is to produce a patch that can be applied in the first place. It is to allow easier eyeballing.
I think it can not be applied because it's *hard* for a computer to actually find the correct location, and it may be equally hard for the reader to evaluate the change with removed/different context.
Quoted 4 lines
Quoted 2 lines
Anyway, I'm starting to think that "show blank lines changes near other changes" makes sense more and more sense.
Probably.
I'm glad to see how convinced you are ;)
I will send my patch and see what makes more sense.
[1]: $gmane/188305
Quoted 6 lines · Antoine Pelisse
Quoted 4 lines
Quoted 2 lines
Anyway, I'm starting to think that "show blank lines changes near other changes" makes sense more and more sense.
Probably.
I'm glad to see how convinced you are ;)
That is not me "not convinced".
It is merely "I do not have a strong conviction that you are wrong; I'd have to think it (again) after re-reading your patch."
Thanks.
PATCH 1/1 diff: add --ignore-blank-lines option
The goal of the patch is to introduce the GNU diff -B/--ignore-blank-lines as closely as possible. The short option is not available because it's already used for "break-rewrites".
When this option is used, git-diff will not create hunks that simply add or remove empty lines, but will still show empty lines addition/suppression if they are close enough to "valuable" changes.
There are two differences between this option and GNU diff -B option:
- GNU diff doesn't have "--inter-hunk-context", so this must be handled
- The following sequence looks like a bug (context is displayed twice):
$ seq 5 >file1
$ cat <<EOF >file2
change
1
2 3
4
5
change
EOF
$ diff -u -B file1 file2
--- file1 2013-06-08 22:13:04.471517834 +0200
+++ file2 2013-06-08 22:13:23.275517855 +0200
@@ -1,5 +1,7 @@
+change
1
2
+
3
4
5
@@ -3,3 +5,4 @@
3
4
5
+changeSo here is a more thorough description of the option:
- real changes are interesting
- blank lines that are close enough (less than context size) to interesting changes are considered interesting (recursive definition)
- "context" lines are used around each hunk of interesting changes
- If two hunks are separated by less than "inter-hunk-context", they will be merged into one.
The current implementation does the "interesting changes selection" in a single pass.
---
Hey,
OK I think this version should be fine (I don't have any bug that comes to mind).
Please review the "between changes" test as I made a choice there that can be controversial.
Thanks,
Antoine
Documentation/diff-options.txt | 3 + diff.c | 2 + t/t4015-diff-whitespace.sh | 308 ++++++++++++++++++++++++++++++++++++++++ xdiff/xdiff.h | 2 + xdiff/xdiffi.c | 29 +++- xdiff/xdiffi.h | 1 + xdiff/xemit.c | 50 ++++++- xdiff/xemit.h | 2 +- xdiff/xutils.c | 13 ++ xdiff/xutils.h | 1 + 10 files changed, 403 insertions(+), 8 deletions(-)
Documentation/diff-options.txt +3 -0
@@ -439,6 +439,9 @@ endif::git-format-patch[] differences even if one line has whitespace where the other line has none. +--ignore-blank-lines:: + Ignore changes whose lines are all blank. + --inter-hunk-context=<lines>:: Show the context between diff hunks, up to the specified number of lines, thereby fusing hunks that are close to each other.
diff.c +2 -0
@@ -3593,6 +3593,8 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)DIFF_XDL_SET (options,IGNORE_WHITESPACE_CHANGE ); else if (!strcmp(arg, "--ignore-space-at-eol"))DIFF_XDL_SET (options,IGNORE_WHITESPACE_AT_EOL ); + else if (!strcmp(arg, "--ignore-blank-lines")) +DIFF_XDL_SET (options,IGNORE_BLANK_LINES ); else if (!strcmp(arg, "--patience")) options->xdl_opts =DIFF_WITH_ALG (options,PATIENCE_DIFF ); else if (!strcmp(arg, "--histogram"))
t/t4015-diff-whitespace.sh +308 -0
314 lines not on this page · raw message
xdiff/xdiff.h +2 -0
@@ -39,6 +39,8 @@ extern "C" {#define XDF_DIFF_ALGORITHM_MASK (XDF_PATIENCE_DIFF | XDF_HISTOGRAM_DIFF)#define XDF_DIFF_ALG (x) ((x) & XDF_DIFF_ALGORITHM_MASK) +#define XDF_IGNORE_BLANK_LINES (1 << 7) +#define XDL_EMIT_FUNCNAMES (1 << 0)#define XDL_EMIT_COMMON (1 << 1)#define XDL_EMIT_FUNCCONTEXT (1 << 2)
xdiff/xdiffi.c +28 -1
@@ -394,6 +394,7 @@ static xdchange_t *xdl_add_change(xdchange_t *xscr, long i1, long i2, long chg1, xch->i2 = i2; xch->chg1 = chg1; xch->chg2 = chg2; + xch->ignore =0 ;return xch; }
@@ -544,7 +545,9 @@ static int xdl_call_hunk_func(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,xdchange_t *xch, *xche;for (xch = xscr; xch; xch = xche->next) { - xche =xdl_get_hunk (xch, xecfg); + xche =xdl_get_hunk (&xch, xecfg); +if (!xch) +break ;if (xecfg->hunk_func (xch->i1, xche->i1 + xche->chg1 - xch->i1, xch->i2, xche->i2 + xche->chg2 - xch->i2, ecb->priv) <0 )
@@ -553,6 +556,27 @@ static int xdl_call_hunk_func(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,return 0 ; } +static void xdl_mark_ignorable (xdchange_t *xscr,xdfenv_t *xe,long flags) +{ +xdchange_t *xch; + +for (xch = xscr; xch; xch = xch->next) { +int ignore =1 ; +xrecord_t **rec; +long i; + + rec = &xe->xdf1.recs[xch->i1]; +for (i =0 ; i < xch->chg1 && ignore; i++) + ignore =xdl_blankline (rec[i]->ptr, rec[i]->size, flags); + + rec = &xe->xdf2.recs[xch->i2]; +for (i =0 ; i < xch->chg2 && ignore; i++) + ignore =xdl_blankline (rec[i]->ptr, rec[i]->size, flags); + + xch->ignore = ignore; + } +} +int xdl_diff (mmfile_t *mf1,mmfile_t *mf2,xpparam_t const *xpp,xdemitconf_t const *xecfg,xdemitcb_t *ecb) {xdchange_t *xscr;
@@ -571,6 +595,9 @@ int xdl_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,return -1 ; }if (xscr) { +if (xpp->flags &XDF_IGNORE_BLANK_LINES ) +xdl_mark_ignorable (xscr, &xe, xpp->flags); +if (ef (&xe, xscr, ecb, xecfg) <0 ) {xdl_free_script (xscr);
xdiff/xdiffi.h +1 -0
@@ -41,6 +41,7 @@ typedef struct s_xdchange {struct s_xdchange *next;long i1, i2;long chg1, chg2; +int ignore; } xdchange_t;
xdiff/xemit.c +44 -6
67 lines not on this page · raw message
xdiff/xemit.h +1 -1
@@ -27,7 +27,7 @@typedef int (*emit_func_t )(xdfenv_t *xe,xdchange_t *xscr,xdemitcb_t *ecb,xdemitconf_t const *xecfg); -xdchange_t *xdl_get_hunk (xdchange_t *xscr,xdemitconf_t const *xecfg); +xdchange_t *xdl_get_hunk (xdchange_t **xscr,xdemitconf_t const *xecfg);int xdl_emit_diff (xdfenv_t *xe,xdchange_t *xscr,xdemitcb_t *ecb,xdemitconf_t const *xecfg);
xdiff/xutils.c +13 -0
@@ -143,6 +143,19 @@ long xdl_guess_lines(mmfile_t *mf, long sample) {return nl +1 ; } +int xdl_blankline (const char *line,long size,long flags) +{ +long i; + +if (!(flags &XDF_WHITESPACE_FLAGS )) +return (size <=1 ); + +for (i =0 ; i < size &&XDL_ISSPACE (line[i]); i++) + ; + +return (i == size); +} +int xdl_recmatch (const char *l1,long s1,const char *l2,long s2,long flags) {int i1, i2;
xdiff/xutils.h +1 -0
@@ -32,6 +32,7 @@ int xdl_cha_init(chastore_t *cha, long isize, long icount);void xdl_cha_free (chastore_t *cha);void *xdl_cha_alloc (chastore_t *cha);long xdl_guess_lines (mmfile_t *mf,long sample); +int xdl_blankline (const char *line,long size,long flags);int xdl_recmatch (const char *l1,long s1,const char *l2,long s2,long flags);unsigned long xdl_hash_record (char const **data,char const *top,long flags);unsigned int xdl_hashbits (unsigned int size);
--
1.7.9.5
Quoted 3 lines · Antoine Pelisse
So here is a more thorough description of the option:
- real changes are interesting
OK, I think I can understand it.
Quoted 2 lines
- blank lines that are close enough (less than context size) to interesting changes are considered interesting (recursive definition)
OK.
Quoted 1 line
- "context" lines are used around each hunk of interesting changes
OK.
Quoted 2 lines
- If two hunks are separated by less than "inter-hunk-context", they will be merged into one.
Makes sense.
Quoted 2 lines
The current implementation does the "interesting changes selection" in a single pass.
"current" meaning "the code after this patch is applied"? Is there a possible future enhancement hinted here?
Quoted 5 lines
xdiff/xemit.c +4
-0+xdchange_t *xdl_get_hunk (xdchange_t **xscr,xdemitconf_t const *xecfg) +{ +xdchange_t *xch, *xchp, *lxch;long max_common =2 * xecfg->ctxlen + xecfg->interhunkctxlen; +long max_ignorable = xecfg->ctxlen;
xdiff/xemit.c static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t * · line 75
Quoted 10 lines
xdiff/xemit.c +10
-0+unsigned long changes =ULONG_MAX ; + +/* remove ignorable changes that are too far before other changes */ +for (xchp = *xscr; xchp && xchp->ignore; xchp = xchp->next) { + xch = xchp->next; + +if (xch ==NULL || + xch->i1 - (xchp->i1 + xchp->chg1) >= max_ignorable) + *xscr = xch; + }
This strips leading ignorable ones away until we see an unignorable one. Looks sane.
xdiff/xemit.c static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t * · line 80
Quoted 4 lines
xdiff/xemit.c +4
-0+if (*xscr ==NULL ) +return NULL ; + + lxch = *xscr;
"lxch" remembers the last one that is "interesting".
xdiff/xemit.c static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t * · line 85
Quoted 4 lines
xdiff/xemit.c +3
-0+for (xchp = *xscr, xch = xchp->next; xch; xchp = xch, xch = xch->next) { +long distance = xch->i1 - (xchp->i1 + xchp->chg1); +if (distance > max_common)break ;
If we see large-enough gap, the one we processed last (in xchp) is the end of the current hunk. Looks sane.
xdiff/xemit.c static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t * · line 90
Quoted 4 lines
xdiff/xemit.c +4
-0+if (distance < max_ignorable && + (!xch->ignore || changes ==ULONG_MAX )) { + lxch = xch; + changes =ULONG_MAX ;
The current one is made into the "last interesting one we have seen" and the hunk continues, if either (1) the current one is interesting by itself, or (2) the last one we saw does not match some unexplainable criteria to cause changes set to not ULONG_MAX.
Puzzling.
xdiff/xemit.c static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t * · line 93
Quoted 3 lines
xdiff/xemit.c +3
-0+ }else if (changes !=ULONG_MAX && + xch->i1 + changes - (lxch->i1 + lxch->chg1) > max_common) { +break ;
If the last one we saw does not match some unexplainable criteria to cause changes set to not ULONG_MAX, and the distance between this one and the last "intersting" one is further than the context, this one will not be a part of the current hunk.
Puzzling.
Could you add comment to the "changes" variable and explain what the variable means?
xdiff/xemit.c static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t * · line 96
Quoted 3 lines
xdiff/xemit.c +3
-0+ }else if (!xch->ignore) { + lxch = xch; + changes =ULONG_MAX ;
When this change by itself is interesting, it becomes the "last interesting one" and the hunk continues.
xdiff/xemit.c static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t * · line 100
Quoted 4 lines
xdiff/xemit.c +4
-0+ }else { +if (changes ==ULONG_MAX ) + changes =0 ; + changes += xch->chg2;
Puzzled beyond guessing. Also it is curious why here and only here we look at chg2 side of the things, not i1/chg1 in this whole thing.
Quoted 102 lines · Junio C Hamano
Quoted 3 lines · Antoine Pelisse
So here is a more thorough description of the option:
- real changes are interesting
OK, I think I can understand it.
Quoted 2 lines
- blank lines that are close enough (less than context size) to interesting changes are considered interesting (recursive definition)
OK.
Quoted 1 line
- "context" lines are used around each hunk of interesting changes
OK.
Quoted 2 lines
- If two hunks are separated by less than "inter-hunk-context", they will be merged into one.
Makes sense.
Quoted 2 lines
The current implementation does the "interesting changes selection" in a single pass.
"current" meaning "the code after this patch is applied"? Is there a possible future enhancement hinted here?
Quoted 5 lines
xdiff/xemit.c +4
-0+xdchange_t *xdl_get_hunk (xdchange_t **xscr,xdemitconf_t const *xecfg) +{ +xdchange_t *xch, *xchp, *lxch;long max_common =2 * xecfg->ctxlen + xecfg->interhunkctxlen; +long max_ignorable = xecfg->ctxlen;xdiff/xemit.c static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t * · line 75
Quoted 10 lines
xdiff/xemit.c +10
-0+unsigned long changes =ULONG_MAX ; + +/* remove ignorable changes that are too far before other changes */ +for (xchp = *xscr; xchp && xchp->ignore; xchp = xchp->next) { + xch = xchp->next; + +if (xch ==NULL || + xch->i1 - (xchp->i1 + xchp->chg1) >= max_ignorable) + *xscr = xch; + }This strips leading ignorable ones away until we see an unignorable one. Looks sane.
xdiff/xemit.c static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t * · line 80
Quoted 4 lines
xdiff/xemit.c +4
-0+if (*xscr ==NULL ) +return NULL ; + + lxch = *xscr;"lxch" remembers the last one that is "interesting".
xdiff/xemit.c static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t * · line 85
Quoted 4 lines
xdiff/xemit.c +3
-0+for (xchp = *xscr, xch = xchp->next; xch; xchp = xch, xch = xch->next) { +long distance = xch->i1 - (xchp->i1 + xchp->chg1); +if (distance > max_common)break ;If we see large-enough gap, the one we processed last (in xchp) is the end of the current hunk. Looks sane.
xdiff/xemit.c static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t * · line 90
Quoted 4 lines
xdiff/xemit.c +4
-0+if (distance < max_ignorable && + (!xch->ignore || changes ==ULONG_MAX )) { + lxch = xch; + changes =ULONG_MAX ;The current one is made into the "last interesting one we have seen" and the hunk continues, if either (1) the current one is interesting by itself, or (2) the last one we saw does not match some unexplainable criteria to cause changes set to not
ULONG_MAX.Puzzling.
xdiff/xemit.c static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t * · line 93
Quoted 3 lines
xdiff/xemit.c +3
-0+ }else if (changes !=ULONG_MAX && + xch->i1 + changes - (lxch->i1 + lxch->chg1) > max_common) { +break ;If the last one we saw does not match some unexplainable criteria to cause changes set to not
ULONG_MAX, and the distance between this one and the last "intersting" one is further than the context, this one will not be a part of the current hunk.Puzzling.
Could you add comment to the "changes" variable and explain what the variable means?
xdiff/xemit.c static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t * · line 96
Quoted 3 lines
xdiff/xemit.c +3
-0+ }else if (!xch->ignore) { + lxch = xch; + changes =ULONG_MAX ;When this change by itself is interesting, it becomes the "last interesting one" and the hunk continues.
xdiff/xemit.c static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t * · line 100
Quoted 4 lines
xdiff/xemit.c +4
-0+ }else { +if (changes ==ULONG_MAX ) + changes =0 ; + changes += xch->chg2;Puzzled beyond guessing. Also it is curious why here and only here we look at chg2 side of the things, not i1/chg1 in this whole thing.
Quoted 20 lines · Junio C Hamano
Quoted 3 lines · Antoine Pelisse
So here is a more thorough description of the option:
- real changes are interesting
OK, I think I can understand it.
Quoted 2 lines
- blank lines that are close enough (less than context size) to interesting changes are considered interesting (recursive definition)
OK.
Quoted 1 line
- "context" lines are used around each hunk of interesting changes
OK.
Quoted 2 lines
- If two hunks are separated by less than "inter-hunk-context", they will be merged into one.
Quoted 7 lines
Makes sense.
Quoted 2 lines
The current implementation does the "interesting changes selection" in a single pass.
"current" meaning "the code after this patch is applied"? Is there a possible future enhancement hinted here?
No. There might be, but I'm not sure it should be discussed right now (In case you're curious, I'm thinking about interaction with combined diff). I will take the hint and rephrase.
Quoted 6 lines
xdiff/xemit.c static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t * · line 66
Quoted 6 lines
xdiff/xemit.c +5
-0+xdchange_t *xdl_get_hunk (xdchange_t **xscr,xdemitconf_t const *xecfg) +{ +xdchange_t *xch, *xchp, *lxch;long max_common =2 * xecfg->ctxlen + xecfg->interhunkctxlen; +long max_ignorable = xecfg->ctxlen; +unsigned long changes =ULONG_MAX ;
Let me explain what "changes" means, as I know it will help the rest of the message:
It counts the number of *added* blank lines we have ignored since "lxch" (needed to calculate the distance between lxch and xch) It also has the meaning of what was called "interesting" before. If changes == ULONG_MAX, we are still in interesting zone, otherwise it means we have ignored "changes" *added* blank lines (0 being a valid value).
(Actually, After rereading this part, it looks like I could check that lxch == xchp rather than setting changes to ULONG_MAX).
Quoted 33 lines
xdiff/xemit.c static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t * · line 75
Quoted 9 lines
xdiff/xemit.c +9
-0+ +/* remove ignorable changes that are too far before other changes */ +for (xchp = *xscr; xchp && xchp->ignore; xchp = xchp->next) { + xch = xchp->next; + +if (xch ==NULL || + xch->i1 - (xchp->i1 + xchp->chg1) >= max_ignorable) + *xscr = xch; + }This strips leading ignorable ones away until we see an unignorable one. Looks sane.
xdiff/xemit.c static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t * · line 80
Quoted 4 lines
xdiff/xemit.c +4
-0+if (*xscr ==NULL ) +return NULL ; + + lxch = *xscr;"lxch" remembers the last one that is "interesting".
xdiff/xemit.c static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t * · line 85
Quoted 4 lines
xdiff/xemit.c +3
-0+for (xchp = *xscr, xch = xchp->next; xch; xchp = xch, xch = xch->next) { +long distance = xch->i1 - (xchp->i1 + xchp->chg1); +if (distance > max_common)break ;If we see large-enough gap, the one we processed last (in xchp) is the end of the current hunk. Looks sane.
xdiff/xemit.c static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t * · line 90
Quoted 4 lines
xdiff/xemit.c +4
-0+if (distance < max_ignorable && + (!xch->ignore || changes ==ULONG_MAX )) { + lxch = xch; + changes =ULONG_MAX ;
Quoted 6 lines
The current one is made into the "last interesting one we have seen" and the hunk continues, if either (1) the current one is interesting by itself, or (2) the last one we saw does not match some unexplainable criteria to cause changes set to not
ULONG_MAX.Puzzling.
- If we are still in interesting zone, we take it, even if it's ignorable change. Because it's close enough.
- Otherwise, only take real changes. We are close to another change, and we are still in the loop, so it must be interesting.
Quoted 10 lines
xdiff/xemit.c static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t * · line 93
Quoted 3 lines
xdiff/xemit.c +3
-0+ }else if (changes !=ULONG_MAX && + xch->i1 + changes - (lxch->i1 + lxch->chg1) > max_common) { +break ;If the last one we saw does not match some unexplainable criteria to cause changes set to not
ULONG_MAX, and the distance between this one and the last "intersting" one is further than the context, this one will not be a part of the current hunk.Puzzling.
If we are no longer in "interesting zone" (changes != ULONG_MAX), it means we will stop if the distance is too big.
"changes" is used in the calculation to consider the changes we have already ignored (xch->i1 - (lxch->i1 + lxch->chg1) will only work if xch and lxch are consecutive, we need to add the blank lines we ignored).
Quoted 9 lines
Could you add comment to the "changes" variable and explain what the variable means?
xdiff/xemit.c static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t * · line 96
Quoted 3 lines
xdiff/xemit.c +3
-0+ }else if (!xch->ignore) { + lxch = xch; + changes =ULONG_MAX ;When this change by itself is interesting, it becomes the "last interesting one" and the hunk continues.
Exactly, and changes goes back to "interesting".
Quoted 7 lines
xdiff/xemit.c static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t * · line 100
Quoted 4 lines
xdiff/xemit.c +4
-0+ }else { +if (changes ==ULONG_MAX ) + changes =0 ; + changes += xch->chg2;Puzzled beyond guessing. Also it is curious why here and only here we look at chg2 side of the things, not i1/chg1 in this whole thing.
chg2 being the number of blank line *additions*.
I don't want to coalesce two hunks because some blank lines have been removed between the two, so we must not change the distance calculation because of a blank line removal. That behavior can be seen in "ignore-blank-lines: between changes" test.
Hope that makes things clearer,
Thanks again for the thorough reading,
Antoine
Quoted 10 lines · Antoine Pelisse
Quoted 1 line
Quoted 1 line
+ unsigned long changes =
ULONG_MAX;Let me explain what "changes" means, as I know it will help the rest of the message:
It counts the number of *added* blank lines we have ignored since "lxch" (needed to calculate the distance between lxch and xch) It also has the meaning of what was called "interesting" before. If changes ==ULONG_MAX, we are still in interesting zone, otherwise it means we have ignored "changes" *added* blank lines (0 being a valid value).
OK. That deserves a comment next to this variable.
Quoted 2 lines
(Actually, After rereading this part, it looks like I could check that lxch == xchp rather than setting changes to
ULONG_MAX).
Yeah, I think so.
Quoted 9 lines
Quoted 5 lines
xdiff/xemit.c static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t * · line 90
Quoted 4 lines
xdiff/xemit.c +4
-0+if (distance < max_ignorable && + (!xch->ignore || changes ==ULONG_MAX )) { + lxch = xch; + changes =ULONG_MAX ;- If we are still in interesting zone, we take it, even if it's ignorable change. Because it's close enough.
- Otherwise, only take real changes. We are close to another change, and we are still in the loop, so it must be interesting.
OK.
Quoted 10 lines
Quoted 3 lines
xdiff/xemit.c static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t * · line 93
Quoted 3 lines
xdiff/xemit.c +3
-0+ }else if (changes !=ULONG_MAX && + xch->i1 + changes - (lxch->i1 + lxch->chg1) > max_common) { +break ;If we are no longer in "interesting zone" (changes !=
ULONG_MAX), it means we will stop if the distance is too big.
"changes" is used in the calculation to consider the changes we have already ignored (xch->i1- (lxch->i1+lxch->chg1) will only work if xch and lxch are consecutive, we need to add the blank lines we ignored).
And this uses max_common that is much larger than max_ignorable because...?
The last interesting change, with its post context and inter hunk gap, together with precontext for this one, is close enough to the beginning of this one. So it is understandable if xch by itself is intereseting to use max_common. Even an interesting one, if that is so far from the last interesting one, should not be part of this hunk.
However, if the current one is by itself uninteresting, should we still use the max_common, or should this be compared with max_ignorable?
Quoted 17 lines
Quoted 9 lines
Could you add comment to the "changes" variable and explain what the variable means?
xdiff/xemit.c static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t * · line 96
Quoted 3 lines
xdiff/xemit.c +3
-0+ }else if (!xch->ignore) { + lxch = xch; + changes =ULONG_MAX ;When this change by itself is interesting, it becomes the "last interesting one" and the hunk continues.
Exactly, and changes goes back to "interesting".
Quoted 5 lines
Quoted 4 lines
xdiff/xemit.c +4
-0+ }else { +if (changes ==ULONG_MAX ) + changes =0 ; + changes += xch->chg2;
Quoted 4 lines
Quoted 2 lines
Puzzled beyond guessing. Also it is curious why here and only here we look at chg2 side of the things, not i1/chg1 in this whole thing.
chg2 being the number of blank line *additions*.
This is on the else side of if (!xch->ignore), so we are looking at ignored hunk, which means there is only blank line change. Can chg2 be 0 while chg1 is not zero, i.e. xch being a blank line removal?
What should happen in that case? Don't we want to show it, for the same reason we want to keep removal, as long as it is close enough to the interesting zone?
Quoted 1 line
Hope that makes things clearer,
Yes, it helped quite a bit.
Quoted 21 lines
Quoted 10 lines
Quoted 3 lines
xdiff/xemit.c static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t * · line 93
Quoted 3 lines
xdiff/xemit.c +3
-0+ }else if (changes !=ULONG_MAX && + xch->i1 + changes - (lxch->i1 + lxch->chg1) > max_common) { +break ;If we are no longer in "interesting zone" (changes !=
ULONG_MAX), it means we will stop if the distance is too big.
"changes" is used in the calculation to consider the changes we have already ignored (xch->i1- (lxch->i1+lxch->chg1) will only work if xch and lxch are consecutive, we need to add the blank lines we ignored).And this uses
max_commonthat is much larger thanmax_ignorablebecause...?The last interesting change, with its post context and inter hunk gap, together with precontext for this one, is close enough to the beginning of this one. So it is understandable if xch by itself is intereseting to use
max_common. Even an interesting one, if that is so far from the last interesting one, should not be part of this hunk.
Quoted 3 lines
However, if the current one is by itself uninteresting, should we still use the
max_common, or should this be compared withmax_ignorable?
Because of the "recursive definition", we don't know yet if an ignorable change will be interesting or not.
We need to make sure it will be close to another interesting change first. If it is, it will fall in the first if part, and lxch will catch-up. If not, we will eventually be too far and break.
Re-reading note: OK, This last sentence ("If not we will eventually be too far and break") is actually a bug. We might break before we find something interesting while we should keep going. For example in such a case, we should display like this, but won't:
@@ -x,x +x,x @@
+change <--- That is lxch
1
2
3
+ <--- Here we leave "interesting"
4
5
+ <--- We are too far and quit searching
6
7
+
8
9
+
10
11
+change
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
xdiff/xemit.c +4
-0+ }else { +if (changes ==ULONG_MAX ) + changes =0 ; + changes += xch->chg2;
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
Puzzled beyond guessing. Also it is curious why here and only here we look at chg2 side of the things, not i1/chg1 in this whole thing.
chg2 being the number of blank line *additions*.
This is on the else side of if (!xch->ignore), so we are looking at ignored hunk, which means there is only blank line change. Can chg2 be 0 while chg1 is not zero, i.e. xch being a blank line removal?
Exactly. It can be a blank line removal. But I don't want to consider it in the calculation.
Here's why:
We have:
1
2
3
4
5
6
and change it to:
change
1
2
3
4
5
6
change
What should be the output of diff --ignore-blank-lines ?
I chose this alternative:
@@ -1,3 +1,4 @@
+change
1
2
3
@@ -7,3 +5,4 @@
4
5
6
+change
While one could have chosen:
@@ -1,10 +1,8 @@
+change
1
2
3
-
-
-
-
4
5
6
+change
Quoted 3 lines
What should happen in that case? Don't we want to show it, for the same reason we want to keep removal, as long as it is close enough to the interesting zone?
Nothing is interesting here, we just leave the interesting zone (if not already left) because everything else failed.
Quoted 4 lines · Antoine Pelisse
Re-reading note: OK, This last sentence ("If not we will eventually be too far and break") is actually a bug. We might break before we find something interesting while we should keep going. For example in such a case, we should display like this, but won't:
Glad to see that my question has helped ;-)
Quoted 37 lines
Quoted 3 lines
This is on the else side of if (!xch->ignore), so we are looking at ignored hunk, which means there is only blank line change. Can chg2 be 0 while chg1 is not zero, i.e. xch being a blank line removal?
Exactly. It can be a blank line removal. But I don't want to consider it in the calculation.
Here's why:
...
What should be the output of diff --ignore-blank-lines ?I chose this alternative:
(quoted diff) +2
-0@@ -1,3 +1,4 @@ +change 1 2 3@@ -7,3 +5,4 @@ 4 5 6 +changeWhile one could have chosen:
(quoted diff) +2
-4@@ -1,10 +1,8 @@ +change 1 2 3 - - - - 4 5 6 +change
Quoted 2 lines
Nothing is interesting here, we just leave the interesting zone (if not already left) because everything else failed.
Yes, that asymmetry is what I was wondering if we want to have. If we show additional blanks as a significant event, I am not so sure we can say "Nothing is interesting here".
I do not feel strongly either way, but it just felt somewhat inconsistent.
Thanks.
PATCH 1/1 diff: add --ignore-blank-lines option
The goal of the patch is to introduce the GNU diff -B/--ignore-blank-lines as closely as possible. The short option is not available because it's already used for "break-rewrites".
When this option is used, git-diff will not create hunks that simply add or remove empty lines, but will still show empty lines addition/suppression if they are close enough to "valuable" changes.
There are two differences between this option and GNU diff -B option:
- GNU diff doesn't have "--inter-hunk-context", so this must be handled
- The following sequence looks like a bug (context is displayed twice):
$ seq 5 >file1
$ cat <<EOF >file2
change
1
2 3
4
5
change
EOF
$ diff -u -B file1 file2
--- file1 2013-06-08 22:13:04.471517834 +0200
+++ file2 2013-06-08 22:13:23.275517855 +0200
@@ -1,5 +1,7 @@
+change
1
2
+
3
4
5
@@ -3,3 +5,4 @@
3
4
5
+changeSo here is a more thorough description of the option:
- real changes are interesting
- blank lines that are close enough (less than context size) to interesting changes are considered interesting (recursive definition)
- "context" lines are used around each hunk of interesting changes
- If two hunks are separated by less than "inter-hunk-context", they will be merged into one.
The implementation does the "interesting changes selection" in a single pass.
---
Changes since last version:
- "changes" variable has been renamed to "ignored"
- "lxch == xchp" is used instead of setting "changes" to a specific value (ULONG_MAX)
- Fixed commit message
- Fixed bug we discussed earlier:
I added a new "else if" branch to handle that specific case (short-cut the bug) I'm unfortunately duplicating some logic, but I don't see any way out of this.
- Added a test for the bug
Documentation/diff-options.txt | 3 + diff.c | 2 + t/t4015-diff-whitespace.sh | 345 ++++++++++++++++++++++++++++++++++++++++ xdiff/xdiff.h | 2 + xdiff/xdiffi.c | 29 +++- xdiff/xdiffi.h | 1 + xdiff/xemit.c | 49 +++++- xdiff/xemit.h | 2 +- xdiff/xutils.c | 13 ++ xdiff/xutils.h | 1 + 10 files changed, 439 insertions(+), 8 deletions(-)
Documentation/diff-options.txt +3 -0
@@ -439,6 +439,9 @@ endif::git-format-patch[] differences even if one line has whitespace where the other line has none. +--ignore-blank-lines:: + Ignore changes whose lines are all blank. + --inter-hunk-context=<lines>:: Show the context between diff hunks, up to the specified number of lines, thereby fusing hunks that are close to each other.
diff.c +2 -0
@@ -3593,6 +3593,8 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)DIFF_XDL_SET (options,IGNORE_WHITESPACE_CHANGE ); else if (!strcmp(arg, "--ignore-space-at-eol"))DIFF_XDL_SET (options,IGNORE_WHITESPACE_AT_EOL ); + else if (!strcmp(arg, "--ignore-blank-lines")) +DIFF_XDL_SET (options,IGNORE_BLANK_LINES ); else if (!strcmp(arg, "--patience")) options->xdl_opts =DIFF_WITH_ALG (options,PATIENCE_DIFF ); else if (!strcmp(arg, "--histogram"))
t/t4015-diff-whitespace.sh +345 -0
351 lines not on this page · raw message
xdiff/xdiff.h +2 -0
@@ -39,6 +39,8 @@ extern "C" {#define XDF_DIFF_ALGORITHM_MASK (XDF_PATIENCE_DIFF | XDF_HISTOGRAM_DIFF)#define XDF_DIFF_ALG (x) ((x) & XDF_DIFF_ALGORITHM_MASK) +#define XDF_IGNORE_BLANK_LINES (1 << 7) +#define XDL_EMIT_FUNCNAMES (1 << 0)#define XDL_EMIT_COMMON (1 << 1)#define XDL_EMIT_FUNCCONTEXT (1 << 2)
xdiff/xdiffi.c +28 -1
@@ -394,6 +394,7 @@ static xdchange_t *xdl_add_change(xdchange_t *xscr, long i1, long i2, long chg1, xch->i2 = i2; xch->chg1 = chg1; xch->chg2 = chg2; + xch->ignore =0 ;return xch; }
@@ -544,7 +545,9 @@ static int xdl_call_hunk_func(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,xdchange_t *xch, *xche;for (xch = xscr; xch; xch = xche->next) { - xche =xdl_get_hunk (xch, xecfg); + xche =xdl_get_hunk (&xch, xecfg); +if (!xch) +break ;if (xecfg->hunk_func (xch->i1, xche->i1 + xche->chg1 - xch->i1, xch->i2, xche->i2 + xche->chg2 - xch->i2, ecb->priv) <0 )
@@ -553,6 +556,27 @@ static int xdl_call_hunk_func(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,return 0 ; } +static void xdl_mark_ignorable (xdchange_t *xscr,xdfenv_t *xe,long flags) +{ +xdchange_t *xch; + +for (xch = xscr; xch; xch = xch->next) { +int ignore =1 ; +xrecord_t **rec; +long i; + + rec = &xe->xdf1.recs[xch->i1]; +for (i =0 ; i < xch->chg1 && ignore; i++) + ignore =xdl_blankline (rec[i]->ptr, rec[i]->size, flags); + + rec = &xe->xdf2.recs[xch->i2]; +for (i =0 ; i < xch->chg2 && ignore; i++) + ignore =xdl_blankline (rec[i]->ptr, rec[i]->size, flags); + + xch->ignore = ignore; + } +} +int xdl_diff (mmfile_t *mf1,mmfile_t *mf2,xpparam_t const *xpp,xdemitconf_t const *xecfg,xdemitcb_t *ecb) {xdchange_t *xscr;
@@ -571,6 +595,9 @@ int xdl_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,return -1 ; }if (xscr) { +if (xpp->flags &XDF_IGNORE_BLANK_LINES ) +xdl_mark_ignorable (xscr, &xe, xpp->flags); +if (ef (&xe, xscr, ecb, xecfg) <0 ) {xdl_free_script (xscr);
xdiff/xdiffi.h +1 -0
@@ -41,6 +41,7 @@ typedef struct s_xdchange {struct s_xdchange *next;long i1, i2;long chg1, chg2; +int ignore; } xdchange_t;
xdiff/xemit.c +43 -6
66 lines not on this page · raw message
xdiff/xemit.h +1 -1
@@ -27,7 +27,7 @@typedef int (*emit_func_t )(xdfenv_t *xe,xdchange_t *xscr,xdemitcb_t *ecb,xdemitconf_t const *xecfg); -xdchange_t *xdl_get_hunk (xdchange_t *xscr,xdemitconf_t const *xecfg); +xdchange_t *xdl_get_hunk (xdchange_t **xscr,xdemitconf_t const *xecfg);int xdl_emit_diff (xdfenv_t *xe,xdchange_t *xscr,xdemitcb_t *ecb,xdemitconf_t const *xecfg);
xdiff/xutils.c +13 -0
@@ -143,6 +143,19 @@ long xdl_guess_lines(mmfile_t *mf, long sample) {return nl +1 ; } +int xdl_blankline (const char *line,long size,long flags) +{ +long i; + +if (!(flags &XDF_WHITESPACE_FLAGS )) +return (size <=1 ); + +for (i =0 ; i < size &&XDL_ISSPACE (line[i]); i++) + ; + +return (i == size); +} +int xdl_recmatch (const char *l1,long s1,const char *l2,long s2,long flags) {int i1, i2;
xdiff/xutils.h +1 -0
@@ -32,6 +32,7 @@ int xdl_cha_init(chastore_t *cha, long isize, long icount);void xdl_cha_free (chastore_t *cha);void *xdl_cha_alloc (chastore_t *cha);long xdl_guess_lines (mmfile_t *mf,long sample); +int xdl_blankline (const char *line,long size,long flags);int xdl_recmatch (const char *l1,long s1,const char *l2,long s2,long flags);unsigned long xdl_hash_record (char const **data,char const *top,long flags);unsigned int xdl_hashbits (unsigned int size);
--
1.7.9.5
Quoted 7 lines · Antoine Pelisse
So here is a more thorough description of the option:
- real changes are interesting
- blank lines that are close enough (less than context size) to interesting changes are considered interesting (recursive definition)
- "context" lines are used around each hunk of interesting changes
- If two hunks are separated by less than "inter-hunk-context", they will be merged into one.
Thanks; will replace what is queued in 'pu'. Let's advance this version to 'next' soonish.