Skip to contentSkip to conversation

diff --ignore-case

PATCH In review Junio C Hamano 5 participants 14 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

cover diff --ignore-case

This teaches our "diff" machinery to optionally consider lines that are different only in upper/lower-cases to be equivalent.

Junio C Hamano (5):
xdiff: remove XDL_PATCH_* macros
xdiff: PATIENCE/HISTOGRAM are not independent option bits
xdiff: introduce XDF_INEXACT_MATCH
xdiff: introduce XDF_IGNORE_CASE
diff: --ignore-case

 Documentation/diff-options.txt |    4 +++
 diff.c                         |    8 +++---
 diff.h                         |    2 ++
 merge-recursive.c              |    4 +--
 t/lib-diff-alternative.sh      |   45 +++++++++++++++++++++++++++++++--
 t/t4033-diff-patience.sh       |    6 +++++
 t/t4050-diff-histogram.sh      |    2 ++
 xdiff/xdiff.h                  |   19 ++++++++------
 xdiff/xdiffi.c                 |    4 +--
 xdiff/xhistogram.c             |    2 +-
 xdiff/xpatience.c              |    2 +-
 xdiff/xprepare.c               |   21 ++++++++--------
 xdiff/xutils.c                 |   54 ++++++++++++++++++++++++++++++++--------
 13 files changed, 133 insertions(+), 40 deletions(-)
Signature · 1 line
1.7.9.1.265.g25f75
#1 lore raw

PATCH 1/5 xdiff: remove XDL_PATCH_* macros

These are not used anywhere in our codebase, and the bit assignment definition is merely confusing.

---
 xdiff/xdiff.h |    5 -----
 1 file changed, 5 deletions(-)
xdiff/xdiff.h +0 -5
index 00d36c3..70c8b87 100644
@@ -36,11 +36,6 @@ extern "C" {
 #define XDF_HISTOGRAM_DIFF (1 << 6)
 #define XDF_WHITESPACE_FLAGS (XDF_IGNORE_WHITESPACE | XDF_IGNORE_WHITESPACE_CHANGE | XDF_IGNORE_WHITESPACE_AT_EOL)
 
-#define XDL_PATCH_NORMAL '-'
-#define XDL_PATCH_REVERSE '+'
-#define XDL_PATCH_MODEMASK ((1 << 8) - 1)
-#define XDL_PATCH_IGNOREBSPACE (1 << 8)
-
 #define XDL_EMIT_FUNCNAMES (1 << 0)
 #define XDL_EMIT_COMMON (1 << 1)
 #define XDL_EMIT_FUNCCONTEXT (1 << 2)
Signature · 1 line
1.7.9.1.265.g25f75
#2 lore raw

PATCH 2/5 xdiff: PATIENCE/HISTOGRAM are not independent option bits

In reply to Junio C Hamano

Because the default Myers, patience and histogram algorithms cannot be in effect at the same time, XDL_PATIENCE_DIFF and XDL_HISTOGRAM_DIFF are not independent bits. Instead of wasting one bit per algorithm, define a few macros to access the few bits they occupy and update the code that access them.

---
 diff.c             |    4 ++--
 diff.h             |    2 ++
 merge-recursive.c  |    4 ++--
 xdiff/xdiff.h      |    5 ++++-
 xdiff/xdiffi.c     |    4 ++--
 xdiff/xhistogram.c |    2 +-
 xdiff/xpatience.c  |    2 +-
 xdiff/xprepare.c   |   21 ++++++++++-----------
 8 files changed, 24 insertions(+), 20 deletions(-)
diff.c +2 -2
index 374ecf3..52cda7a 100644
@@ -3400,9 +3400,9 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
 	else if (!strcmp(arg, "--ignore-space-at-eol"))
 		DIFF_XDL_SET(options, IGNORE_WHITESPACE_AT_EOL);
 	else if (!strcmp(arg, "--patience"))
-		DIFF_XDL_SET(options, PATIENCE_DIFF);
+		options->xdl_opts = DIFF_WITH_ALG(options, PATIENCE_DIFF);
 	else if (!strcmp(arg, "--histogram"))
-		DIFF_XDL_SET(options, HISTOGRAM_DIFF);
+		options->xdl_opts = DIFF_WITH_ALG(options, HISTOGRAM_DIFF);
 
 	/* flags options */
 	else if (!strcmp(arg, "--binary")) {
diff.h +2 -0
index 0c51724..e688a48 100644
@@ -88,6 +88,8 @@ typedef struct strbuf *(*diff_prefix_fn_t)(struct diff_options *opt, void *data)
 #define DIFF_XDL_SET(opts, flag)    ((opts)->xdl_opts |= XDF_##flag)
 #define DIFF_XDL_CLR(opts, flag)    ((opts)->xdl_opts &= ~XDF_##flag)
 
+#define DIFF_WITH_ALG(opts, flag)   (((opts)->xdl_opts & ~XDF_DIFF_ALGORITHM_MASK) | XDF_##flag)
+
 enum diff_words_type {
 	DIFF_WORDS_NONE = 0,
 	DIFF_WORDS_PORCELAIN,
merge-recursive.c +2 -2
index cc664c3..1d574fe 100644
@@ -2069,9 +2069,9 @@ int parse_merge_opt(struct merge_options *o, const char *s)
 	else if (!prefixcmp(s, "subtree="))
 		o->subtree_shift = s + strlen("subtree=");
 	else if (!strcmp(s, "patience"))
-		o->xdl_opts |= XDF_PATIENCE_DIFF;
+		o->xdl_opts = DIFF_WITH_ALG(o, PATIENCE_DIFF);
 	else if (!strcmp(s, "histogram"))
-		o->xdl_opts |= XDF_HISTOGRAM_DIFF;
+		o->xdl_opts = DIFF_WITH_ALG(o, HISTOGRAM_DIFF);
 	else if (!strcmp(s, "ignore-space-change"))
 		o->xdl_opts |= XDF_IGNORE_WHITESPACE_CHANGE;
 	else if (!strcmp(s, "ignore-all-space"))
xdiff/xdiff.h +4 -1
index 70c8b87..09215af 100644
@@ -32,9 +32,12 @@ extern "C" {
 #define XDF_IGNORE_WHITESPACE (1 << 2)
 #define XDF_IGNORE_WHITESPACE_CHANGE (1 << 3)
 #define XDF_IGNORE_WHITESPACE_AT_EOL (1 << 4)
+#define XDF_WHITESPACE_FLAGS (XDF_IGNORE_WHITESPACE | XDF_IGNORE_WHITESPACE_CHANGE | XDF_IGNORE_WHITESPACE_AT_EOL)
+
 #define XDF_PATIENCE_DIFF (1 << 5)
 #define XDF_HISTOGRAM_DIFF (1 << 6)
-#define XDF_WHITESPACE_FLAGS (XDF_IGNORE_WHITESPACE | XDF_IGNORE_WHITESPACE_CHANGE | XDF_IGNORE_WHITESPACE_AT_EOL)
+#define XDF_DIFF_ALGORITHM_MASK (XDF_PATIENCE_DIFF | XDF_HISTOGRAM_DIFF)
+#define XDF_DIFF_ALG(x) ((x) & XDF_DIFF_ALGORITHM_MASK)
 
 #define XDL_EMIT_FUNCNAMES (1 << 0)
 #define XDL_EMIT_COMMON (1 << 1)
xdiff/xdiffi.c +2 -2
index 75a3922..bc889e8 100644
@@ -328,10 +328,10 @@ int xdl_do_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
 	xdalgoenv_t xenv;
 	diffdata_t dd1, dd2;
 
-	if (xpp->flags & XDF_PATIENCE_DIFF)
+	if (XDF_DIFF_ALG(xpp->flags) == XDF_PATIENCE_DIFF)
 		return xdl_do_patience_diff(mf1, mf2, xpp, xe);
 
-	if (xpp->flags & XDF_HISTOGRAM_DIFF)
+	if (XDF_DIFF_ALG(xpp->flags) == XDF_HISTOGRAM_DIFF)
 		return xdl_do_histogram_diff(mf1, mf2, xpp, xe);
 
 	if (xdl_prepare_env(mf1, mf2, xpp, xe) < 0) {
xdiff/xhistogram.c +1 -1
index 18f6f99..bf99787 100644
@@ -252,7 +252,7 @@ static int fall_back_to_classic_diff(struct histindex *index,
 		int line1, int count1, int line2, int count2)
 {
 	xpparam_t xpp;
-	xpp.flags = index->xpp->flags & ~XDF_HISTOGRAM_DIFF;
+	xpp.flags = index->xpp->flags & ~XDF_DIFF_ALGORITHM_MASK;
 
 	return xdl_fall_back_diff(index->env, &xpp,
 				  line1, count1, line2, count2);
xdiff/xpatience.c +1 -1
index fdd7d02..04e1a1a 100644
@@ -288,7 +288,7 @@ static int fall_back_to_classic_diff(struct hashmap *map,
 		int line1, int count1, int line2, int count2)
 {
 	xpparam_t xpp;
-	xpp.flags = map->xpp->flags & ~XDF_PATIENCE_DIFF;
+	xpp.flags = map->xpp->flags & ~XDF_DIFF_ALGORITHM_MASK;
 
 	return xdl_fall_back_diff(map->env, &xpp,
 				  line1, count1, line2, count2);
xdiff/xprepare.c +10 -11
index e419f4f..63a22c6 100644
@@ -181,7 +181,7 @@ static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_
 	if (!(recs = (xrecord_t **) xdl_malloc(narec * sizeof(xrecord_t *))))
 		goto abort;
 
-	if (xpp->flags & XDF_HISTOGRAM_DIFF)
+	if (XDF_DIFF_ALG(xpp->flags) == XDF_HISTOGRAM_DIFF)
 		hbits = hsize = 0;
 	else {
 		hbits = xdl_hashbits((unsigned int) narec);
@@ -209,8 +209,8 @@ static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_
 			crec->ha = hav;
 			recs[nrec++] = crec;
 
-			if (!(xpp->flags & XDF_HISTOGRAM_DIFF) &&
-				xdl_classify_record(pass, cf, rhash, hbits, crec) < 0)
+			if ((XDF_DIFF_ALG(xpp->flags) != XDF_HISTOGRAM_DIFF) &&
+			    xdl_classify_record(pass, cf, rhash, hbits, crec) < 0)
 				goto abort;
 		}
 	}
@@ -273,16 +273,15 @@ int xdl_prepare_env(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
 	 * (nrecs) will be updated correctly anyway by
 	 * xdl_prepare_ctx().
 	 */
-	sample = xpp->flags & XDF_HISTOGRAM_DIFF ? XDL_GUESS_NLINES2 : XDL_GUESS_NLINES1;
+	sample = (XDF_DIFF_ALG(xpp->flags) == XDF_HISTOGRAM_DIFF
+		  ? XDL_GUESS_NLINES2 : XDL_GUESS_NLINES1);
 
 	enl1 = xdl_guess_lines(mf1, sample) + 1;
 	enl2 = xdl_guess_lines(mf2, sample) + 1;
 
-	if (!(xpp->flags & XDF_HISTOGRAM_DIFF) &&
-		xdl_init_classifier(&cf, enl1 + enl2 + 1, xpp->flags) < 0) {
-
+	if (XDF_DIFF_ALG(xpp->flags) != XDF_HISTOGRAM_DIFF &&
+	    xdl_init_classifier(&cf, enl1 + enl2 + 1, xpp->flags) < 0)
 		return -1;
-	}
 
 	if (xdl_prepare_ctx(1, mf1, enl1, xpp, &cf, &xe->xdf1) < 0) {
 
@@ -296,9 +295,9 @@ int xdl_prepare_env(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
 		return -1;
 	}
 
-	if (!(xpp->flags & XDF_PATIENCE_DIFF) &&
-			!(xpp->flags & XDF_HISTOGRAM_DIFF) &&
-			xdl_optimize_ctxs(&cf, &xe->xdf1, &xe->xdf2) < 0) {
+	if ((XDF_DIFF_ALG(xpp->flags) != XDF_PATIENCE_DIFF) &&
+	    (XDF_DIFF_ALG(xpp->flags) != XDF_HISTOGRAM_DIFF) &&
+	    xdl_optimize_ctxs(&cf, &xe->xdf1, &xe->xdf2) < 0) {
 
 		xdl_free_ctx(&xe->xdf2);
 		xdl_free_ctx(&xe->xdf1);
Signature · 1 line
1.7.9.1.265.g25f75
#3 lore raw

PATCH 3/5 xdiff: introduce XDF_INEXACT_MATCH

In reply to Junio C Hamano

The XDF_WHITESPACE_FLAGS mask is used to decide if a simple memcmp() is sufficient to tell if two lines are the same for the purpose of producing diff. In the current code, only options in the ignore-whitespace family happen to call for a matching logic that is more complex than memcmp(), but when we add new options, e.g. ignore-case, it will become apparent that the mask is misnamed.

Introduce XDF_INEXACT_MATCH mask to express its true meaning: when any of these bits are set, we may need to inspect the contents of the two lines that are not exactly the same, as we may still consider them "matching".

The XDF_WHITESPACE_FLAGS will become useful again in a later patch in the series and its definition is kept.

---
 diff.c         |    2 +-
 xdiff/xdiff.h  |    6 +++++-
 xdiff/xutils.c |    4 ++--
 3 files changed, 8 insertions(+), 4 deletions(-)
diff.c +1 -1
index 52cda7a..87b2ec1 100644
@@ -2142,7 +2142,7 @@ static void builtin_diff(const char *name_a,
 		struct emit_callback ecbdata;
 		const struct userdiff_funcname *pe;
 
-		if (!DIFF_XDL_TST(o, WHITESPACE_FLAGS) || must_show_header) {
+		if (!DIFF_XDL_TST(o, INEXACT_MATCH) || must_show_header) {
 			fprintf(o->file, "%s", header.buf);
 			strbuf_reset(&header);
 		}
xdiff/xdiff.h +5 -1
index 09215af..48793f9 100644
@@ -32,7 +32,11 @@ extern "C" {
 #define XDF_IGNORE_WHITESPACE (1 << 2)
 #define XDF_IGNORE_WHITESPACE_CHANGE (1 << 3)
 #define XDF_IGNORE_WHITESPACE_AT_EOL (1 << 4)
-#define XDF_WHITESPACE_FLAGS (XDF_IGNORE_WHITESPACE | XDF_IGNORE_WHITESPACE_CHANGE | XDF_IGNORE_WHITESPACE_AT_EOL)
+#define XDF_WHITESPACE_FLAGS \
+	(XDF_IGNORE_WHITESPACE | \
+	 XDF_IGNORE_WHITESPACE_CHANGE | \
+	 XDF_IGNORE_WHITESPACE_AT_EOL)
+#define XDF_INEXACT_MATCH XDF_WHITESPACE_FLAGS
 
 #define XDF_PATIENCE_DIFF (1 << 5)
 #define XDF_HISTOGRAM_DIFF (1 << 6)
xdiff/xutils.c +2 -2
index 0de084e..aa6de74 100644
@@ -176,7 +176,7 @@ int xdl_recmatch(const char *l1, long s1, const char *l2, long s2, long flags)
 
 	if (s1 == s2 && !memcmp(l1, l2, s1))
 		return 1;
-	if (!(flags & XDF_WHITESPACE_FLAGS))
+	if (!(flags & XDF_INEXACT_MATCH))
 		return 0;
 
 	i1 = 0;
@@ -281,7 +281,7 @@ unsigned long xdl_hash_record(char const **data, char const *top, long flags) {
 	unsigned long ha = 5381;
 	char const *ptr = *data;
 
-	if (flags & XDF_WHITESPACE_FLAGS)
+	if (flags & XDF_INEXACT_MATCH)
 		return xdl_hash_record_with_whitespace(data, top, flags);
 
 	for (; ptr < top && *ptr != '\n'; ptr++) {
Signature · 1 line
1.7.9.1.265.g25f75
#4 lore raw

PATCH 4/5 xdiff: introduce XDF_IGNORE_CASE

In reply to Junio C Hamano

Teach the hash function and per-line comparison logic to compare lines while ignoring the differences in case. It is not an ignore-whitespace option but still needs to trigger the inexact match logic, and that is why the previous step introduced XDF_INEXACT_MATCH mask.

Assign the 7th bit for this option, and move the bits to select diff algorithms out of the way in order to leave room for a few bits to add more variants of ignore-whitespace, such as --ignore-tab-expansion, if somebody else is inclined to do so later.

We would still need to teach the front-end to flip this bit, for this change to be any useful.

---
 xdiff/xdiff.h  |    7 ++++---
 xdiff/xutils.c |   50 ++++++++++++++++++++++++++++++++++++++++++--------
 2 files changed, 46 insertions(+), 11 deletions(-)
xdiff/xdiff.h +4 -3
index 48793f9..5a01a0e 100644
@@ -36,10 +36,11 @@ extern "C" {
 	(XDF_IGNORE_WHITESPACE | \
 	 XDF_IGNORE_WHITESPACE_CHANGE | \
 	 XDF_IGNORE_WHITESPACE_AT_EOL)
-#define XDF_INEXACT_MATCH XDF_WHITESPACE_FLAGS
+#define XDF_IGNORE_CASE (1 << 7)
+#define XDF_INEXACT_MATCH (XDF_WHITESPACE_FLAGS | XDF_IGNORE_CASE)
 
-#define XDF_PATIENCE_DIFF (1 << 5)
-#define XDF_HISTOGRAM_DIFF (1 << 6)
+#define XDF_PATIENCE_DIFF (1 << 8)
+#define XDF_HISTOGRAM_DIFF (1 << 9)
 #define XDF_DIFF_ALGORITHM_MASK (XDF_PATIENCE_DIFF | XDF_HISTOGRAM_DIFF)
 #define XDF_DIFF_ALG(x) ((x) & XDF_DIFF_ALGORITHM_MASK)
 
xdiff/xutils.c +42 -8

103 lines not on this page · raw message

Signature · 1 line
1.7.9.1.265.g25f75
#6 lore raw

PATCH 5/5 diff: --ignore-case

In reply to Junio C Hamano

Teach the front-end to flip XDF_IGNORE_CASE bit with the options GNU diff uses.

---
 Documentation/diff-options.txt |    4 ++++
 diff.c                         |    2 ++
 t/lib-diff-alternative.sh      |   45 ++++++++++++++++++++++++++++++++++++++--
 t/t4033-diff-patience.sh       |    6 ++++++
 t/t4050-diff-histogram.sh      |    2 ++
 5 files changed, 57 insertions(+), 2 deletions(-)
Documentation/diff-options.txt +4 -0
index 9f7cba2..791e07f 100644
@@ -404,6 +404,10 @@ endif::git-format-patch[]
 	differences even if one line has whitespace where the other
 	line has none.
 
+--ignore-case::
+	Ignore changes in case only; only ASCII alphabet is supported for
+	now.
+
 --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
index 87b2ec1..d7604b7 100644
@@ -3399,6 +3399,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-case"))
+		DIFF_XDL_SET(options, IGNORE_CASE);
 	else if (!strcmp(arg, "--patience"))
 		options->xdl_opts = DIFF_WITH_ALG(options, PATIENCE_DIFF);
 	else if (!strcmp(arg, "--histogram"))
t/lib-diff-alternative.sh +43 -2

60 lines not on this page · raw message

t/t4033-diff-patience.sh +6 -0
index 3c9932e..c7f8c6c 100755
@@ -5,8 +5,14 @@ test_description='patience diff algorithm'
 . ./test-lib.sh
 . "$TEST_DIRECTORY"/lib-diff-alternative.sh
 
+# baseline
+test_diff_unique ""
+test_diff_ignore ""
+
 test_diff_frobnitz "patience"
 
 test_diff_unique "patience"
 
+test_diff_ignore "patience"
+
 test_done
t/t4050-diff-histogram.sh +2 -0
index fd3e86a..98c6686 100755
@@ -9,4 +9,6 @@ test_diff_frobnitz "histogram"
 
 test_diff_unique "histogram"
 
+test_diff_ignore "histogram"
+
 test_done
Signature · 1 line
1.7.9.1.265.g25f75
#7 lore raw
In reply to Junio C Hamano

Am 20.02.2012 03:16, schrieb Junio C Hamano:

Quoted 2 lines

This teaches our "diff" machinery to optionally consider lines that are different only in upper/lower-cases to be equivalent.

When would I want to use --ignore-case? And how useful is it that it does not obey the current locale, as in your implementation?

-- Hannes

#8 lore raw
Quoted 5 lines · Johannes Sixt

Am 20.02.2012 03:16, schrieb Junio C Hamano:

Quoted 2 lines

This teaches our "diff" machinery to optionally consider lines that are different only in upper/lower-cases to be equivalent.

When would I want to use --ignore-case?

I wouldn't myself; it was just I saw somebody asked if -G can be used to do case insensitive and then I realized we do not even support such in the diff machinery to begin with, but now this opens a way to do so by copying the xdl options when the pickaxe-grep codepath runs a subdiff.

You can update the hash and recmatch to be locale-aware, which would be an internal update to xdiff/ directory that is independent of the rest of the series, and/or the change to plug the machinery to -G.

#9 lore raw
Quoted 6 lines · Junio C Hamano
Quoted 4 lines · Johannes Sixt

Am 20.02.2012 03:16, schrieb Junio C Hamano:

Quoted 2 lines

This teaches our "diff" machinery to optionally consider lines that are different only in upper/lower-cases to be equivalent.

Quoted 6 lines
Quoted 1 line

When would I want to use --ignore-case?

I wouldn't myself; it was just I saw somebody asked if -G can be used to do case insensitive and then I realized we do not even support such in the diff machinery to begin with, but now this opens a way to do so by copying the xdl options when the pickaxe-grep codepath runs a subdiff.

I wonder which one of us misunderstood the original request ;-)

It was

} Is there any way to run diff -G with a case insensitivity flag?

and I took that to mean "I want to find addition/removal of a string like -G does, but I don't know how it was capitalized". OTOH you interpreted it as "I want to run -G but ignore changes in case while diffing".

So maybe Chris can comment on what was intended?

Signature · 2 lines
Thomas Rast
trast@{inf,student}.ethz.ch
#10 lore raw
Quoted 1 line · Thomas Rast

I wonder which one of us misunderstood the original request ;-)

Heh, I did ;-)

Quoted 6 lines

It was

} Is there any way to run diff -G with a case insensitivity flag?

and I took that to mean "I want to find addition/removal of a string like -G does, but I don't know how it was capitalized".

I think it is just the matter of checking REG_ICASE that may be set in revs->grep_filter.regflags, and propagating it down to the regcomp at the beginning of diffcore_pickaxe_grep().

Want to try and see how well it works?

#11 lore raw

Re* [PATCH 0/5] diff --ignore-case

Quoted 10 lines · Junio C Hamano
Quoted 1 line · Thomas Rast

I wonder which one of us misunderstood the original request ;-)

Heh, I did ;-)

Quoted 4 lines

It was

} Is there any way to run diff -G with a case insensitivity flag?

Quoted 8 lines
Quoted 2 lines

and I took that to mean "I want to find addition/removal of a string like -G does, but I don't know how it was capitalized".

I think it is just the matter of checking REG_ICASE that may be set in revs->grep_filter.regflags, and propagating it down to the regcomp at the beginning of diffcore_pickaxe_grep().

Want to try and see how well it works?

The gist of the patch should look like this.

Even though I haven't done anything more than just to compile and run "git log -p -i -G search 233054d", it looks obviously correct ;-)

Note that this does not depend on any of the "diff --ignore-case" topic, and it may conflict with [PATCH 6/5]. Among the two "else if" that flips the REG_ICASE in revision.c, it needs to be decided if only one of them, or both of them should set the new option. I haven't thought things through to decide which.

 diff.h             |    1 +
 diffcore-pickaxe.c |    6 +++++-
 revision.c         |    1 +
 3 files changed, 7 insertions(+), 1 deletion(-)
diff.h +1 -0
index 7af5f1e..182cb0e 100644
@@ -82,6 +82,7 @@ typedef struct strbuf *(*diff_prefix_fn_t)(struct diff_options *opt, void *data)
 #define DIFF_OPT_OVERRIDE_SUBMODULE_CONFIG (1 << 27)
 #define DIFF_OPT_DIRSTAT_BY_LINE     (1 << 28)
 #define DIFF_OPT_FUNCCONTEXT         (1 << 29)
+#define DIFF_OPT_PICKAXE_IGNORE_CASE (1 << 30)
 
 #define DIFF_OPT_TST(opts, flag)    ((opts)->flags & DIFF_OPT_##flag)
 #define DIFF_OPT_SET(opts, flag)    ((opts)->flags |= DIFF_OPT_##flag)
diffcore-pickaxe.c +5 -1
index 380a837..8ffb741 100644
@@ -138,8 +138,12 @@ static void diffcore_pickaxe_grep(struct diff_options *o)
 {
 	int err;
 	regex_t regex;
+	int cflags = REG_EXTENDED | REG_NEWLINE;
 
-	err = regcomp(&regex, o->pickaxe, REG_EXTENDED | REG_NEWLINE);
+	if (DIFF_OPT_TST(o, PICKAXE_IGNORE_CASE))
+		cflags |= REG_ICASE;
+
+	err = regcomp(&regex, o->pickaxe, cflags);
 	if (err) {
 		char errbuf[1024];
 		regerror(err, &regex, errbuf, 1024);
revision.c +1 -0
index 819ff01..b3554ed 100644
@@ -1582,6 +1582,7 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
 		revs->grep_filter.regflags |= REG_EXTENDED;
 	} else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
 		revs->grep_filter.regflags |= REG_ICASE;
+		DIFF_OPT_SET(&revs->diffopt, PICKAXE_IGNORE_CASE);
 	} else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
 		revs->grep_filter.fixed = 1;
 	} else if (!strcmp(arg, "--all-match")) {
#13 lore raw

[PATCH 6/5] diff -i

In reply to Junio C Hamano

The previous patch to teach "--ignore-case" option to our "diff" machinery deliberately omitted a short-and-sweet "-i" that GNU diff uses to ask for "--ignore-case". This is because our diff machinery are often used by and shares the command line options with the commands in the "git log" family, where the short option already means something entirely different. Namely, it instructs to use case-insensitive match when looking for commits that match information that appear in the commit object itself, e.g. --author and --grep.

Tweak the option parser so that "-i" means both, so that

	$ git log --grep=frotz -i -p

first picks commits that have string "frotz" in any combination of cases, and then shows patches that ignore case-only changes for the chosen commits, while "--ignore-case" and "--regexp-ignore-case" can be used to mean only one, i.e.

	$ git log --grep=frotz --regexp-ignore-case -p

would pick the same set of commits, but the patches shown by it does not ignore case-only changes while

	$ git log --grep=frotz --ignore-case -p

would pick commits that has "frotz" in all lowercase, but shows patches that ignore case-only changes for the chosen commits.

---
 Documentation/diff-options.txt |    1 +
 diff.c                         |    2 +-
 revision.c                     |    6 +++++-
 t/lib-diff-alternative.sh      |    3 ++-
 4 files changed, 9 insertions(+), 3 deletions(-)
Documentation/diff-options.txt +1 -0
index 791e07f..9ed78c9 100644
@@ -404,6 +404,7 @@ endif::git-format-patch[]
 	differences even if one line has whitespace where the other
 	line has none.
 
+-i::
 --ignore-case::
 	Ignore changes in case only; only ASCII alphabet is supported for
 	now.
diff.c +1 -1
index d7604b7..9d1584e 100644
@@ -3399,7 +3399,7 @@ 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-case"))
+	else if (!strcmp(arg, "--ignore-case") || !strcmp(arg, "-i"))
 		DIFF_XDL_SET(options, IGNORE_CASE);
 	else if (!strcmp(arg, "--patience"))
 		options->xdl_opts = DIFF_WITH_ALG(options, PATIENCE_DIFF);
revision.c +5 -1
index 8764dde..f1a1354 100644
@@ -13,6 +13,7 @@
 #include "decorate.h"
 #include "log-tree.h"
 #include "string-list.h"
+#include "xdiff-interface.h"
 
 volatile show_early_output_fn_t show_early_output;
 
@@ -1557,7 +1558,10 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
 		return argcount;
 	} else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
 		revs->grep_filter.regflags |= REG_EXTENDED;
-	} else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
+	} else if (!strcmp(arg, "-i")) {
+		DIFF_XDL_SET(&revs->diffopt, IGNORE_CASE);
+		revs->grep_filter.regflags |= REG_ICASE;
+	} else if (!strcmp(arg, "--regexp-ignore-case")) {
 		revs->grep_filter.regflags |= REG_ICASE;
 	} else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
 		revs->grep_filter.fixed = 1;
t/lib-diff-alternative.sh +2 -1
index 45c665e..81609f8 100644
@@ -175,9 +175,10 @@ test_diff_ignore () {
 	echo " A quick brownfox" >test-w
 	echo "A quick brown fox " >test--ignore-space-at-eol
 	echo "A Quick Brown Fox" >test--ignore-case
+	echo "A Quick Brown FoX" >test-i
 	echo "A Quick  Brown Fox" >test--ignore-case-b
 	echo "A quick brown fox jumps" >test
-	cases="-b -w --ignore-space-at-eol --ignore-case"
+	cases="-b -w --ignore-space-at-eol --ignore-case -i"
 
 	if test -z "$STRATEGY"
 	then
#5 lore raw

Re: [PATCH 4/5] xdiff: introduce XDF_IGNORE_CASE

In reply to Junio C Hamano
Quoted 4 lines · Junio C Hamano

Teach the hash function and per-line comparison logic to compare lines while ignoring the differences in case. It is not an ignore-whitespace option but still needs to trigger the inexact match logic, and that is why the previous step introduced XDF_INEXACT_MATCH mask.

Nb. how it compares with ignore case in filesystem paths?

Quoted 4 lines

Assign the 7th bit for this option, and move the bits to select diff algorithms out of the way in order to leave room for a few bits to add more variants of ignore-whitespace, such as --ignore-tab-expansion, if somebody else is inclined to do so later.

Or do a proper Unicode sorting / collation algorithm, with different levels

(4.3 Form a sort key for each string, UTS #10.):

Level 1: alphabetic ordering
Level 2: diacritic ordering
Level 3: case ordering
Level 4: tie-breaking (e.g. in the case when variable is 'shifted')

Quoted 8 lines

We would still need to teach the front-end to flip this bit, for this change to be any useful.

---

xdiff/xutils.c +2 -0
+static inline int match_a_byte(char ch1, char ch2, long flags)
+{

xdiff/xutils.c long xdl_guess_lines(mmfile_t *mf, long sample) { · line 184

Quoted 10 lines
xdiff/xutils.c +10 -0
+	if (ch1 == ch2)
+		return 1;
+	if (!(flags & XDF_IGNORE_CASE) || ((ch1 | ch2) & 0x80))
+		return 0;
+	if (isupper(ch1))
+		ch1 = tolower(ch1);
+	if (isupper(ch2))
+		ch2 = tolower(ch2);
+	return (ch1 == ch2);
+}

<del>
Wouldn't a better solution be a collate algorithm rather than changing a sorting function? Or is it a performance hack on typical body of text under version control (mainly lowercase)?
</del>

"(libc.info)Collation Fuctions" says:

The functions strcoll' and wcscoll' perform this translation
implicitly, in order to do one comparison. By contrast, strxfrm' and wcsxfrm' perform the mapping explicitly. If you are making multiple comparisons using the same string or set of strings, it is likely to be more efficient to use strxfrm' or wcsxfrm' to transform all the strings just once, and subsequently compare the transformed strings with strcmp' or wcscmp'.

The function match_a_byte (memcoll?) defined here is similar to strcoll; do we compare single line with more than one other line?

xdiff/xutils.c int xdl_recmatch(const char *l1, long s1, const char *l2, long s2, long flags) · line 274

Quoted 8 lines
xdiff/xutils.c +8 -0
+static inline unsigned long hash_a_byte(const char ch_, long flags)
+{
+	unsigned long ch = ch_ & 0xFF;
+	if ((flags & XDF_IGNORE_CASE) && !(ch & 0x80) && isupper(ch))
+		ch = tolower(ch);
+	return ch;
+}
+

Hmmm... hash_a_byte (memxfrm?) is similar to strxfrm, so you do use one or the other...

Signature · 1 line
Jakub Narebski