Skip to contentSkip to conversation

grep: PCREv2 fixes, remove kwset.[ch]

PATCH Superseded Ævar Arnfjörð Bjarmason 3 participants 99 messages Feb 3, 2021 – Apr 12, 2021 2 messages not in the archive Kbuild

lore
Share & tools
lore
#0 lore raw

cover grep: PCREv2 fixes, remove kwset.[ch]

This is based on next. I was wondering if/how to split this up, it should arguably be a few serieses, but let's see if it gets some traction in reviews like this. Comments below:

Ævar Arnfjörð Bjarmason (25):
grep/pcre2 tests: reword comments referring to kwset grep/pcre2: drop needless assignment + assert() on opt->pcre2 grep/pcre2: drop needless assignment to NULL grep/pcre2: correct reference to grep_init() in comment grep/pcre2: prepare to add debugging to pcre2_malloc() grep/pcre2: add GREP_PCRE2_DEBUG_MALLOC debug mode grep/pcre2: use compile-time PCREv2 version test grep/pcre2: use pcre2_maketables_free() function grep/pcre2: actually make pcre2 use custom allocator grep/pcre2: move back to thread-only PCREv2 structures grep/pcre2: move definitions of pcre2_{malloc,free}

PCRE v2 code cleanups, and fix up bugs in our pcre2_{malloc,free}() handling.

pickaxe tests: refactor to use test_commit --append pickaxe -S: support content with NULs under --pickaxe-regex pickaxe -S: remove redundant "sz" check in while-loop pickaxe/style: consolidate declarations and assignments pickaxe tests: add test for diffgrep_consume() internals pickaxe tests: add test for "log -S" not being a regex
perf: add performance test for pickaxe

Various test prep for pickaxe.

pickaxe -G: set -U0 for diff generation

Turns out feeding "log -G" -U10 output makes it faster.

grep.h: make patmatch() a public function
pickaxe: use PCREv2 for -G and -S
Remove unused kwset.[ch]

At long last, kwset.[ch] is gone!

xdiff-interface: allow early return from xdiff_emit_{line,hunk}_fn
xdiff-interface: support early exit in xdiff_outf() pickaxe -G: terminate early on matching lines

Solve an ancient todo item in pickaxe by extending our xdiff interface so you can early exit from hunk/line handlers.

 Makefile                       |   2 -
 builtin/grep.c                 |   1 -
 combine-diff.c                 |   9 +-
 compat/obstack.c               | 413 ------------------
 compat/obstack.h               | 511 ----------------------
 ctype.c                        |  36 --
 diff.c                         |  39 +-
 diff.h                         |   4 +
 diffcore-pickaxe.c             | 184 ++++----
 git-compat-util.h              |   3 -
 grep.c                         | 103 ++---
 grep.h                         |  11 +-
 kwset.c                        | 775 ---------------------------------
 kwset.h                        |  65 ---
 range-diff.c                   |   8 +-
 t/perf/p4209-pickaxe.sh        |  82 ++++
 t/t4209-log-pickaxe.sh         |  64 ++-
 t/t7816-grep-binary-pattern.sh |   4 +-
 xdiff-interface.c              |  26 +-
 xdiff-interface.h              |  36 +-
 20 files changed, 351 insertions(+), 2025 deletions(-)
 delete mode 100644 compat/obstack.c
 delete mode 100644 compat/obstack.h
 delete mode 100644 kwset.c
 delete mode 100644 kwset.h
 create mode 100755 t/perf/p4209-pickaxe.sh
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#1 lore raw

PATCH 1/25 grep/pcre2 tests: reword comments referring to kwset

The kwset optimization has not been used by grep since 48de2a768cf (grep: remove the kwset optimization, 2019-07-01).

---
 t/t7816-grep-binary-pattern.sh | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
t/t7816-grep-binary-pattern.sh +2 -2
index 60bab291e4..9d67a5fc4c 100755
@@ -59,7 +59,7 @@ test_expect_success 'setup' "
 	git commit -m.
 "
 
-# Simple fixed-string matching that can use kwset (no -i && non-ASCII)
+# Simple fixed-string matching
 nul_match P P P '-F' 'yQf'
 nul_match P P P '-F' 'yQx'
 nul_match P P P '-Fi' 'YQf'
@@ -78,7 +78,7 @@ nul_match P P P '-Fi' '[Y]QF'
 nul_match P P P '-F' 'æQ[ð]'
 nul_match P P P '-F' '[æ]Qð'
 
-# The -F kwset codepath can't handle -i && non-ASCII...
+# Matching pattern and subject case with -i
 nul_match P 1 1 '-i' '[æ]Qð'
 
 # ...PCRE v2 only matches non-ASCII with -i casefolding under UTF-8
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#2 lore raw

PATCH 2/25 grep/pcre2: drop needless assignment + assert() on opt->pcre2

In reply to Ævar Arnfjörð Bjarmason

Drop an assignment added in b65abcafc7a (grep: use PCRE v2 for optimized fixed-string search, 2019-07-01) and the overly cautious assert() I added in 94da9193a6e (grep: add support for PCRE v2, 2017-06-01).

There was never a good reason for this, it's just a relic from when I initially wrote the PCREv2 support. We're not going to have confusion about compile_pcre2_pattern() being called when it shouldn't just because we forgot to cargo-cult this opt->pcre2 option, and "opt" is (mostly) used for the options the user supplied, let's avoid the pattern of needlessly assigning to it.

With my in-flight removal of PCREv1 [1] ("Remove support for v1 of the PCRE library", 2021-01-24) there'll be even less confusion around what we call where in these codepaths, which is one more reason to remove this.

1. https://lore.kernel.org/git/[email protected]/

---
 grep.c | 3 ---
 1 file changed, 3 deletions(-)
grep.c +0 -3
index aabfaaa4c3..816e23f17e 100644
@@ -373,8 +373,6 @@ static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt
 	int patinforet;
 	size_t jitsizearg;
 
-	assert(opt->pcre2);
-
 	p->pcre2_compile_context = NULL;
 
 	/* pcre2_global_context is initialized in append_grep_pattern */
@@ -555,7 +553,6 @@ static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
 #endif
 	if (p->fixed || p->is_fixed) {
 #ifdef USE_LIBPCRE2
-		opt->pcre2 = 1;
 		if (p->is_fixed) {
 			compile_pcre2_pattern(p, opt);
 		} else {
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#3 lore raw

PATCH 3/25 grep/pcre2: drop needless assignment to NULL

In reply to Ævar Arnfjörð Bjarmason

Remove a redundant assignment of pcre2_compile_context dating back to my 94da9193a6e (grep: add support for PCRE v2, 2017-06-01). In create_grep_pat() we xcalloc() the "grep_pat" struct, so there's no need to NULL out individual members here.

I think this was probably something left over from an earlier development version of mine.

---
 grep.c | 2 --
 1 file changed, 2 deletions(-)
grep.c +0 -2
index 816e23f17e..f27c5de7f5 100644
@@ -373,8 +373,6 @@ static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt
 	int patinforet;
 	size_t jitsizearg;
 
-	p->pcre2_compile_context = NULL;
-
 	/* pcre2_global_context is initialized in append_grep_pattern */
 	if (opt->ignore_case) {
 		if (!opt->ignore_locale && has_non_ascii(p->pattern)) {
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#4 lore raw

PATCH 4/25 grep/pcre2: correct reference to grep_init() in comment

In reply to Ævar Arnfjörð Bjarmason

Correct a comment added in 513f2b0bbd4 (grep: make PCRE2 aware of custom allocator, 2019-10-16). This comment was never correct in git.git, but was consistent with an older version of the patch[1].

1. https://lore.kernel.org/git/[email protected]/

---
 grep.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
grep.c +1 -1
index f27c5de7f5..b9adcd83e7 100644
@@ -373,7 +373,7 @@ static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt
 	int patinforet;
 	size_t jitsizearg;
 
-	/* pcre2_global_context is initialized in append_grep_pattern */
+	/* pcre2_global_context is initialized in grep_init */
 	if (opt->ignore_case) {
 		if (!opt->ignore_locale && has_non_ascii(p->pattern)) {
 			if (!pcre2_global_context)
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#5 lore raw

PATCH 5/25 grep/pcre2: prepare to add debugging to pcre2_malloc()

In reply to Ævar Arnfjörð Bjarmason

Change pcre2_malloc() in a way that'll make it easier for a debugging fprintf() to spew out the allocated pointer. This doesn't introduce any functional change, it just makes a subsequent commit's diff easier to read. Changes code added in 513f2b0bbd4 (grep: make PCRE2 aware of custom allocator, 2019-10-16).

---
 grep.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
grep.c +2 -1
index b9adcd83e7..f96d86c929 100644
@@ -45,7 +45,8 @@ static pcre2_general_context *pcre2_global_context;
 
 static void *pcre2_malloc(PCRE2_SIZE size, MAYBE_UNUSED void *memory_data)
 {
-	return malloc(size);
+	void *pointer = malloc(size);
+	return pointer;
 }
 
 static void pcre2_free(void *pointer, MAYBE_UNUSED void *memory_data)
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#6 lore raw

PATCH 6/25 grep/pcre2: add GREP_PCRE2_DEBUG_MALLOC debug mode

In reply to Ævar Arnfjörð Bjarmason

Add optional printing of PCREv2 allocations to stderr for a developer who manually changes the GREP_PCRE2_DEBUG_MALLOC definition to "1".

This will be referenced a subsequent commit, and is generally useful to manually see what's going on with PCREv2 allocations while working on that code.

---
 grep.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
grep.c +10 -0
index f96d86c929..7d262a23d8 100644
@@ -42,15 +42,25 @@ static struct grep_opt grep_defaults = {
 
 #ifdef USE_LIBPCRE2
 static pcre2_general_context *pcre2_global_context;
+#define GREP_PCRE2_DEBUG_MALLOC 0
 
 static void *pcre2_malloc(PCRE2_SIZE size, MAYBE_UNUSED void *memory_data)
 {
 	void *pointer = malloc(size);
+#if GREP_PCRE2_DEBUG_MALLOC
+	static int count = 1;
+	fprintf(stderr, "PCRE2:%p -> #%02d: alloc(%lu)\n", pointer, count++, size);
+#endif
 	return pointer;
 }
 
 static void pcre2_free(void *pointer, MAYBE_UNUSED void *memory_data)
 {
+#if GREP_PCRE2_DEBUG_MALLOC
+	static int count = 1;
+	if (pointer)
+		fprintf(stderr, "PCRE2:%p -> #%02d: free()\n", pointer, count++);
+#endif
 	free(pointer);
 }
 #endif
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#7 lore raw

PATCH 7/25 grep/pcre2: use compile-time PCREv2 version test

In reply to Ævar Arnfjörð Bjarmason

Replace a use of pcre2_config(PCRE2_CONFIG_VERSION, ...) which I added in 95ca1f987ed (grep/pcre2: better support invalid UTF-8 haystacks, 2021-01-24) with the same test done at compile-time.

It might be cuter to do this at runtime since we don't have to do the "major >= 11 || (major >= 10 && ...)" test. But in the next commit we'll add another version comparison that absolutely needs to be done at compile-time, so we're better of being consistent across the board.

---
 grep.c | 18 ++++--------------
 grep.h |  3 +++
 2 files changed, 7 insertions(+), 14 deletions(-)
grep.c +4 -14
index 7d262a23d8..e58044474d 100644
@@ -400,21 +400,11 @@ static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt
 	    !(!opt->ignore_case && (p->fixed || p->is_fixed)))
 		options |= (PCRE2_UTF | PCRE2_MATCH_INVALID_UTF);
 
+#ifdef GIT_PCRE2_VERSION_10_36_OR_HIGHER
 	/* Work around https://bugs.exim.org/show_bug.cgi?id=2642 fixed in 10.36 */
-	if (PCRE2_MATCH_INVALID_UTF && options & (PCRE2_UTF | PCRE2_CASELESS)) {
-		struct strbuf buf;
-		int len;
-		int err;
-
-		if ((len = pcre2_config(PCRE2_CONFIG_VERSION, NULL)) < 0)
-			BUG("pcre2_config(..., NULL) failed: %d", len);
-		strbuf_init(&buf, len + 1);
-		if ((err = pcre2_config(PCRE2_CONFIG_VERSION, buf.buf)) < 0)
-			BUG("pcre2_config(..., buf.buf) failed: %d", err);
-		if (versioncmp(buf.buf, "10.36") < 0)
-			options |= PCRE2_NO_START_OPTIMIZE;
-		strbuf_release(&buf);
-	}
+	if (PCRE2_MATCH_INVALID_UTF && options & (PCRE2_UTF | PCRE2_CASELESS))
+		options |= PCRE2_NO_START_OPTIMIZE;
+#endif
 
 	p->pcre2_pattern = pcre2_compile((PCRE2_SPTR)p->pattern,
 					 p->patternlen, options, &error, &erroffset,
grep.h +3 -0
index ae89d6254b..54e52042cb 100644
@@ -4,6 +4,9 @@
 #ifdef USE_LIBPCRE2
 #define PCRE2_CODE_UNIT_WIDTH 8
 #include <pcre2.h>
+#if (PCRE2_MAJOR >= 10 && PCRE2_MINOR >= 36) || PCRE2_MAJOR >= 11
+#define GIT_PCRE2_VERSION_10_36_OR_HIGHER
+#endif
 #else
 typedef int pcre2_code;
 typedef int pcre2_match_data;
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#8 lore raw

PATCH 8/25 grep/pcre2: use pcre2_maketables_free() function

In reply to Ævar Arnfjörð Bjarmason

Make use of the pcre2_maketables_free() function to free the memory allocated by pcre2_maketables(). At first sight it's strange that 10da030ab75 (grep: avoid leak of chartables in PCRE2, 2019-10-16) which added the free() call here doesn't make use of the pcre2_free() the author introduced in the preceding commit in 513f2b0bbd4 (grep:
make PCRE2 aware of custom allocator, 2019-10-16).

The reason is that at the time the function didn't exist. It was first introduced in PCREv2 version 10.34, released on 2019-11-21.

Let's make use of it behind a macro. I don't think this matters for anything to do with custom allocators, but it makes our use of PCREv2 more discoverable. At some distant point in the future we'll be able to drop the version guard, as nobody will be running a version older than 10.34.

---
 grep.c | 4 ++++
 grep.h | 3 +++
 2 files changed, 7 insertions(+)
grep.c +4 -0
index e58044474d..c63dbff4b2 100644
@@ -490,7 +490,11 @@ static void free_pcre2_pattern(struct grep_pat *p)
 	pcre2_compile_context_free(p->pcre2_compile_context);
 	pcre2_code_free(p->pcre2_pattern);
 	pcre2_match_data_free(p->pcre2_match_data);
+#ifdef GIT_PCRE2_VERSION_10_34_OR_HIGHER
+	pcre2_maketables_free(pcre2_global_context, p->pcre2_tables);
+#else
 	free((void *)p->pcre2_tables);
+#endif
 }
 #else /* !USE_LIBPCRE2 */
 static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt)
grep.h +3 -0
index 54e52042cb..64666e9204 100644
@@ -7,6 +7,9 @@
 #if (PCRE2_MAJOR >= 10 && PCRE2_MINOR >= 36) || PCRE2_MAJOR >= 11
 #define GIT_PCRE2_VERSION_10_36_OR_HIGHER
 #endif
+#if (PCRE2_MAJOR >= 10 && PCRE2_MINOR >= 34) || PCRE2_MAJOR >= 11
+#define GIT_PCRE2_VERSION_10_34_OR_HIGHER
+#endif
 #else
 typedef int pcre2_code;
 typedef int pcre2_match_data;
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#9 lore raw

PATCH 9/25 grep/pcre2: actually make pcre2 use custom allocator

In reply to Ævar Arnfjörð Bjarmason

Continue work started in 513f2b0bbd4 (grep: make PCRE2 aware of custom allocator, 2019-10-16) and make PCREv2 use our pcre2_{malloc,free}(). functions for allocation. We'll now use it for all PCREv2 allocations.

The reason 513f2b0bbd4 worked as a bugfix for the USE_NED_ALLOCATOR issue is because it managed to target pretty much the allocation freed via free(), as opposed to by a pcre2_*free() function. I.e. the pcre2_maketables() and pcre2_maketables_free() pair. For most of the rest we continued allocating with stock malloc() inside PCREv2 itself, but didn't segfault because we'd use its corresponding free().

In a preceding commit of mine I changed the free() to pcre2_maketables_free() on versions of PCREv2 10.34 and newer. So as far as fixing the segfault goes we could revert 513f2b0bbd4. But then we wouldn't use the desired allocator, let's just use it instead.

Before this patch we'd on e.g.:

    grep --threads=1 -iP æ.*var.*xyz

Only use pcre2_{malloc,free}() for 2 malloc() calls and 2 corresponding free() call. Now it's 12 calls to each. This can be observed with the GREP_PCRE2_DEBUG_MALLOC debug mode.

Reading the history of how this bug got introduced it wasn't present in Johannes's original patch[1] to fix the issue.

My reading of that thread is that the approach the follow-up patches to Johannes's original pursued were based on misunderstanding of how the PCREv2 API works. In particular this part of [2]:

"most of the time (like when using UTF-8) the chartable (and therefore the global context) is not needed (even when using alternate allocators)"

That's simply not how PCREv2 memory allocation works. It's easy to see how the misunderstanding came about. It's because (as noted above) the issue was noticed because of our use of free() in our own grep.c for freeing the memory allocated by pcre2_maketables().

Thus the misunderstanding that PCREv2's compile context is something only needed for pcre2_maketables(), and e.g. an aborted earlier attempt[3] to only set it up when we ourselves called pcre2_maketables().

That's not what PCREv2's compile context is. To quote PCREv2's documentation:

"This context just contains pointers to (and data for) external memory management functions that are called from several places in the PCRE2 library."

Thus the failed attempts to go down the route of only creating the general context in cases where we ourselves call pcre2_maketables(), before finally settling on the approach 513f2b0bbd4 took of always creating it.

Instead we should always create it, and then pass the general context to those functions that accept it, so that they'll consistently use our preferred memory allocation functions.

1. https://public-inbox.org/git/3397e6797f872aedd18c6d795f4976e1c579514b.1565005867.git.gitgitgadget@gmail.com/
2. https://lore.kernel.org/git/CAPUEsphMh_ZqcH3M7PXC9jHTfEdQN3mhTAK2JDkdvKBp53YBoA@mail.gmail.com/
3. https://lore.kernel.org/git/[email protected]/

---
 grep.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
grep.c +2 -2
index c63dbff4b2..0116ff5f09 100644
@@ -390,7 +390,7 @@ static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt
 			if (!pcre2_global_context)
 				BUG("pcre2_global_context uninitialized");
 			p->pcre2_tables = pcre2_maketables(pcre2_global_context);
-			p->pcre2_compile_context = pcre2_compile_context_create(NULL);
+			p->pcre2_compile_context = pcre2_compile_context_create(pcre2_global_context);
 			pcre2_set_character_tables(p->pcre2_compile_context,
 							p->pcre2_tables);
 		}
@@ -411,7 +411,7 @@ static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt
 					 p->pcre2_compile_context);
 
 	if (p->pcre2_pattern) {
-		p->pcre2_match_data = pcre2_match_data_create_from_pattern(p->pcre2_pattern, NULL);
+		p->pcre2_match_data = pcre2_match_data_create_from_pattern(p->pcre2_pattern, pcre2_global_context);
 		if (!p->pcre2_match_data)
 			die("Couldn't allocate PCRE2 match data");
 	} else {
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#10 lore raw

PATCH 10/25 grep/pcre2: move back to thread-only PCREv2 structures

In reply to Ævar Arnfjörð Bjarmason

Change the setup of the "pcre2_general_context" to happen per-thread in compile_pcre2_pattern() instead of in grep_init(), as happens with all the rest of the pcre2_* members of the grep_pat structure.

As noted in the preceding commit the approach 513f2b0bbd4 (grep: make PCRE2 aware of custom allocator, 2019-10-16) took to allocate the pcre2_general_context seems to have been initially based on a misunderstanding of how PCREv2 memory allocation works.

This approach of creating a global context is just added complexity for almost zero gain. On my system it's 24 bytes saved per-thread, for context PCREv2 will then go on to some kilobytes for its own thread-local state.

As noted in 6d423dd542f (grep: don't redundantly compile throwaway patterns under threading, 2017-05-25) the grep code is intentionally not trying to micro-optimize allocations by e.g. sharing some PCREv2 structures globally, while making others thread-local.

So let's remove this special case and make all of them thread-local for simplicity again.

See also the discussion in 94da9193a6 (grep: add support for PCRE v2, 2017-06-01) about thread safety, and Johannes's comments[1] to the effect that we should be doing what this patch is doing.

1. https://lore.kernel.org/git/[email protected]/

---
 builtin/grep.c |  1 -
 grep.c         | 41 +++++++++++++++--------------------------
 grep.h         |  3 ++-
 3 files changed, 17 insertions(+), 28 deletions(-)
builtin/grep.c +0 -1
index 55d06c9513..c69fe99340 100644
@@ -1175,6 +1175,5 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 		run_pager(&opt, prefix);
 	clear_pathspec(&pathspec);
 	free_grep_patterns(&opt);
-	grep_destroy();
 	return !hit;
 }
grep.c +15 -26

85 lines not on this page · raw message

grep.h +2 -1
index 64666e9204..72f82b1e30 100644
@@ -14,6 +14,7 @@
 typedef int pcre2_code;
 typedef int pcre2_match_data;
 typedef int pcre2_compile_context;
+typedef int pcre2_general_context;
 #endif
 #ifndef PCRE2_MATCH_INVALID_UTF
 /* PCRE2_MATCH_* dummy also with !USE_LIBPCRE2, for test-pcre2-config.c */
@@ -75,6 +76,7 @@ struct grep_pat {
 	pcre2_code *pcre2_pattern;
 	pcre2_match_data *pcre2_match_data;
 	pcre2_compile_context *pcre2_compile_context;
+	pcre2_general_context *pcre2_general_context;
 	const uint8_t *pcre2_tables;
 	uint32_t pcre2_jit_on;
 	unsigned fixed:1;
@@ -167,7 +169,6 @@ struct grep_opt {
 
 int grep_config(const char *var, const char *value, void *);
 void grep_init(struct grep_opt *, struct repository *repo, const char *prefix);
-void grep_destroy(void);
 void grep_commit_pattern_type(enum grep_pattern_type, struct grep_opt *opt);
 
 void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen, const char *origin, int no, enum grep_pat_token t);
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#12 lore raw

PATCH 12/25 pickaxe tests: refactor to use test_commit --append

In reply to Ævar Arnfjörð Bjarmason

Refactor existing tests added in e0e7cb8080c (log -G: ignore binary files, 2018-12-14) to use the --append option I added in 3373518cc8b (test-lib functions: add an --append option to test_commit, 2021-01-12).

See also f5d79bf7dd6 (tests: refactor a few tests to use "test_commit --append", 2021-01-12) for prior similar refactoring.

---
 t/t4209-log-pickaxe.sh | 30 ++++++++++++++----------------
 1 file changed, 14 insertions(+), 16 deletions(-)
t/t4209-log-pickaxe.sh +14 -16
index 5d06f5f45e..21d22b2a18 100755
@@ -107,37 +107,35 @@ test_expect_success 'log -S --no-textconv (missing textconv tool)' '
 '
 
 test_expect_success 'setup log -[GS] binary & --text' '
-	git checkout --orphan GS-binary-and-text &&
-	git read-tree --empty &&
-	printf "a\na\0a\n" >data.bin &&
-	git add data.bin &&
-	git commit -m "create binary file" data.bin &&
-	printf "a\na\0a\n" >>data.bin &&
-	git commit -m "modify binary file" data.bin &&
-	git rm data.bin &&
-	git commit -m "delete binary file" data.bin &&
-	git log >full-log
+	test_create_repo GS-bin-txt &&
+	test_commit -C GS-bin-txt --append A data.bin "a\na\0a\n" &&
+	test_commit -C GS-bin-txt --append B data.bin "a\na\0a\n" &&
+	test_commit -C GS-bin-txt C data.bin "" &&
+	git -C GS-bin-txt log >full-log
 '
 
 test_expect_success 'log -G ignores binary files' '
-	git log -Ga >log &&
+	git -C GS-bin-txt log -Ga >log &&
 	test_must_be_empty log
 '
 
 test_expect_success 'log -G looks into binary files with -a' '
-	git log -a -Ga >log &&
+	git -C GS-bin-txt log -a -Ga >log &&
 	test_cmp log full-log
 '
 
 test_expect_success 'log -G looks into binary files with textconv filter' '
-	test_when_finished "rm .gitattributes" &&
-	echo "* diff=bin" >.gitattributes &&
-	git -c diff.bin.textconv=cat log -Ga >log &&
+	(
+		cd GS-bin-txt &&
+		test_when_finished "rm .gitattributes" &&
+		echo "* diff=bin" >.gitattributes &&
+		git -c diff.bin.textconv=cat log -Ga >../log
+	) &&
 	test_cmp log full-log
 '
 
 test_expect_success 'log -S looks into binary files' '
-	git log -Sa >log &&
+	git -C GS-bin-txt log -Sa >log &&
 	test_cmp log full-log
 '
 
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#11 lore raw

PATCH 11/25 grep/pcre2: move definitions of pcre2_{malloc,free}

In reply to Ævar Arnfjörð Bjarmason

Move the definitions of the pcre2_{malloc,free} functions above the compile_pcre2_pattern() function they're used it. Before the preceding commit they used to be needed earlier, but now we can move them to be adjacent to the other PCREv2 functions.

---
 grep.c | 46 ++++++++++++++++++++++------------------------
 1 file changed, 22 insertions(+), 24 deletions(-)
grep.c +22 -24
index 2599f329cd..636ac48bf0 100644
@@ -40,30 +40,6 @@ static struct grep_opt grep_defaults = {
 	.output = std_output,
 };
 
-#ifdef USE_LIBPCRE2
-#define GREP_PCRE2_DEBUG_MALLOC 0
-
-static void *pcre2_malloc(PCRE2_SIZE size, MAYBE_UNUSED void *memory_data)
-{
-	void *pointer = malloc(size);
-#if GREP_PCRE2_DEBUG_MALLOC
-	static int count = 1;
-	fprintf(stderr, "PCRE2:%p -> #%02d: alloc(%lu)\n", pointer, count++, size);
-#endif
-	return pointer;
-}
-
-static void pcre2_free(void *pointer, MAYBE_UNUSED void *memory_data)
-{
-#if GREP_PCRE2_DEBUG_MALLOC
-	static int count = 1;
-	if (pointer)
-		fprintf(stderr, "PCRE2:%p -> #%02d: free()\n", pointer, count++);
-#endif
-	free(pointer);
-}
-#endif
-
 static const char *color_grep_slots[] = {
 	[GREP_COLOR_CONTEXT]	    = "context",
 	[GREP_COLOR_FILENAME]	    = "filename",
@@ -355,6 +331,28 @@ static int is_fixed(const char *s, size_t len)
 }
 
 #ifdef USE_LIBPCRE2
+#define GREP_PCRE2_DEBUG_MALLOC 0
+
+static void *pcre2_malloc(PCRE2_SIZE size, MAYBE_UNUSED void *memory_data)
+{
+	void *pointer = malloc(size);
+#if GREP_PCRE2_DEBUG_MALLOC
+	static int count = 1;
+	fprintf(stderr, "PCRE2:%p -> #%02d: alloc(%lu)\n", pointer, count++, size);
+#endif
+	return pointer;
+}
+
+static void pcre2_free(void *pointer, MAYBE_UNUSED void *memory_data)
+{
+#if GREP_PCRE2_DEBUG_MALLOC
+	static int count = 1;
+	if (pointer)
+		fprintf(stderr, "PCRE2:%p -> #%02d: free()\n", pointer, count++);
+#endif
+	free(pointer);
+}
+
 static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt)
 {
 	int error;
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#13 lore raw

PATCH 13/25 pickaxe -S: support content with NULs under --pickaxe-regex

In reply to Ævar Arnfjörð Bjarmason

Fix a bug in the matching routine powering -S<rx> --pickaxe-regex so that we won't abort early on content that has NULs in it.

We've had a hard requirement on REG_STARTEND since 2f8952250a8 (regex:
add regexec_buf() that can work on a non NUL-terminated string, 2016-09-21), but this sanity check dates back to d01d8c67828 (Support for pickaxe matching regular expressions, 2006-03-29).

It wasn't needed anymore, and as the now-passing test shows, actively getting in our way.

---
 diffcore-pickaxe.c     | 4 ++--
 t/t4209-log-pickaxe.sh | 8 ++++++++
 2 files changed, 10 insertions(+), 2 deletions(-)
diffcore-pickaxe.c +2 -2
index a9c6d60df2..208177bb40 100644
@@ -82,12 +82,12 @@ static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws)
 		regmatch_t regmatch;
 		int flags = 0;
 
-		while (sz && *data &&
+		while (sz &&
 		       !regexec_buf(regexp, data, sz, 1, &regmatch, flags)) {
 			flags |= REG_NOTBOL;
 			data += regmatch.rm_eo;
 			sz -= regmatch.rm_eo;
-			if (sz && *data && regmatch.rm_so == regmatch.rm_eo) {
+			if (sz && regmatch.rm_so == regmatch.rm_eo) {
 				data++;
 				sz--;
 			}
t/t4209-log-pickaxe.sh +8 -0
index 21d22b2a18..bd42848871 100755
@@ -139,4 +139,12 @@ test_expect_success 'log -S looks into binary files' '
 	test_cmp log full-log
 '
 
+test_expect_success 'log -S --pickaxe-regex looks into binary files' '
+	git -C GS-bin-txt log --pickaxe-regex -Sa >log &&
+	test_cmp log full-log &&
+
+	git -C GS-bin-txt log --pickaxe-regex -S[a] >log &&
+	test_cmp log full-log
+'
+
 test_done
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#14 lore raw

PATCH 14/25 pickaxe -S: remove redundant "sz" check in while-loop

In reply to Ævar Arnfjörð Bjarmason

If we walk to the end of the string we just won't match the rest of the regex. This removes an optimization for simplicity's sake. In subsequent commits we'll alter this code more, and not having to think about this condition makes it easier to read.

If we look at the context of what we're doing here the last thing we need to be worried about is one extra regex match. The real problem is that we keep matching after it's clear that the number of contains() for "A" and "B" is different. So we could be much smarter here.

---
 diffcore-pickaxe.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
diffcore-pickaxe.c +2 -3
index 208177bb40..8df76afb6e 100644
@@ -82,12 +82,11 @@ static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws)
 		regmatch_t regmatch;
 		int flags = 0;
 
-		while (sz &&
-		       !regexec_buf(regexp, data, sz, 1, &regmatch, flags)) {
+		while (!regexec_buf(regexp, data, sz, 1, &regmatch, flags)) {
 			flags |= REG_NOTBOL;
 			data += regmatch.rm_eo;
 			sz -= regmatch.rm_eo;
-			if (sz && regmatch.rm_so == regmatch.rm_eo) {
+			if (regmatch.rm_so == regmatch.rm_eo) {
 				data++;
 				sz--;
 			}
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#19 lore raw

PATCH 16/25 pickaxe tests: add test for diffgrep_consume() internals

In reply to Ævar Arnfjörð Bjarmason

In diffgrep_consume() we generate a diff, and then advance past the "+" or "-" at the start of the line for matching. This has been done ever since the code was added in f506b8e8b5f (git log/diff: add -G<regexp> that greps in the patch text, 2010-08-23).

If we match "line" instead of "line + 1" no tests fail, i.e. we've got zero coverage for whether any of our searches match the beginning of the line or not. Let's add a test for this.

---
 t/t4209-log-pickaxe.sh | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
t/t4209-log-pickaxe.sh +15 -0
index bd42848871..ebd51f498b 100755
@@ -106,6 +106,21 @@ test_expect_success 'log -S --no-textconv (missing textconv tool)' '
 	rm .gitattributes
 '
 
+test_expect_success 'setup log -[GS] plain' '
+	test_create_repo GS-plain &&
+	test_commit -C GS-plain --append A data.bin "a" &&
+	test_commit -C GS-plain --append B data.bin "a a" &&
+	test_commit -C GS-plain C data.bin "" &&
+	git -C GS-plain log >full-log
+'
+
+test_expect_success 'log -G trims diff new/old [-+]' '
+	git -C GS-plain log -G"[+-]a" >log &&
+	test_must_be_empty log &&
+	git -C GS-plain log -G"^a" >log &&
+	test_cmp log full-log
+'
+
 test_expect_success 'setup log -[GS] binary & --text' '
 	test_create_repo GS-bin-txt &&
 	test_commit -C GS-bin-txt --append A data.bin "a\na\0a\n" &&
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#21 lore raw

PATCH 18/25 perf: add performance test for pickaxe

In reply to Ævar Arnfjörð Bjarmason

Add a test for the -G and -S pickaxe options and related options. This test supports being run with GIT_PERF_EXTRA=1 to turn on the full set of tests, as well as GIT_TEST_LONG=1 to opt-in a full history walk. By default I'm limiting the walk to 500 commits, which seems to hit a good spot on git.git of around 0.5s per iteration.

---
 t/perf/p4209-pickaxe.sh | 82 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 82 insertions(+)
 create mode 100755 t/perf/p4209-pickaxe.sh
t/perf/p4209-pickaxe.sh new file +82 -0
new file mode 100755
index 0000000000..011a287d3b
@@ -0,0 +1,82 @@
+#!/bin/sh
+
+test_description="Test pickaxe performance"
+
+. ./perf-lib.sh
+
+test_perf_default_repo
+
+# Not --max-count, as that's the number of matching commit, so it's
+# unbounded. We want to limit our revision walk here.
+from_rev_desc=
+from_rev=
+if ! test_have_prereq EXPENSIVE
+then
+	max_count=500
+	from_rev=" $(git rev-list HEAD | head -n $max_count | tail -n 1).."
+	from_rev_desc=" <limit-rev>.."
+fi
+
+for icase in \
+	'' \
+	'-i '
+do
+	# -S (no regex)
+	for pattern in \
+		'a' \
+		'uncommon'\
+		'ö'
+	do
+		for opts in \
+			'-S'
+		do
+			continue
+			test_perf "git log $icase$opts'$pattern'$from_rev_desc" "
+				git log --pretty=format:%H $icase$opts'$pattern'$from_rev
+			"
+		done
+	done
+
+	# -S (regex)
+	for pattern in  \
+		'[þæö]'
+	do
+		for opts in \
+			'--pickaxe-regex -S'
+		do
+			test_perf "git log $icase$opts'$pattern'$from_rev_desc" "
+				git log --pretty=format:%H $icase$opts'$pattern'$from_rev
+			"
+		done
+	done
+
+	# -G
+	for pattern in  \
+		'a' \
+		'uncommon' \
+		'[þæö]'
+	do
+		for opts in \
+			'-G' \
+			'--pickaxe-regex -S'
+		do
+			test_perf "git log $icase$opts'$pattern'$from_rev_desc" "
+				git log --pretty=format:%H $icase$opts'$pattern'$from_rev
+			"
+		done
+
+		# -G extra
+		for opts in \
+			'--text -G' \
+			'--text --pickaxe-all -G' \
+			'--pickaxe-all -G' \
+			'--pickaxe-all --pickaxe-regex -S'
+		do
+			test_perf PERF_EXTRA "git log $icase$opts'$pattern'$from_rev_desc" "
+				git log --pretty=format:%H $icase$opts'$pattern'$from_rev
+			"
+		done
+	done
+done
+
+test_done
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#25 lore raw

PATCH 20/25 grep.h: make patmatch() a public function

In reply to Ævar Arnfjörð Bjarmason

I'd like to use the PCRE & ERE etc. code in grep.c for more things in git, starting with diffcore-pickaxe.c.

The current API just exposes grep_{source,buffer}() for that purpose. I could use those, but they're very fat entry points into the entire set of bells and whistles that grep.c supports for "git grep". I just want the equivalent of a light regexec() wrapper for my compiled patterns.

So let's expose patmatch() for that purpose. It's not perfect, in particular it's a bit ugly that we need to pop a pattern off the opt->pattern_list if all we've got is the "grep_opt" wrapper struct, but it'll do for now.

---
 grep.c | 4 ++--
 grep.h | 2 ++
 2 files changed, 4 insertions(+), 2 deletions(-)
grep.c +2 -2
index 636ac48bf0..8d84313d6e 100644
@@ -906,8 +906,8 @@ static void show_name(struct grep_opt *opt, const char *name)
 	opt->output(opt, opt->null_following_name ? "\0" : "\n", 1);
 }
 
-static int patmatch(struct grep_pat *p, char *line, char *eol,
-		    regmatch_t *match, int eflags)
+int patmatch(struct grep_pat *p, char *line, char *eol,
+	     regmatch_t *match, int eflags)
 {
 	int hit;
 
grep.h +2 -0
index 72f82b1e30..66e2ee37f3 100644
@@ -205,6 +205,8 @@ void grep_source_load_driver(struct grep_source *gs,
 
 
 int grep_source(struct grep_opt *opt, struct grep_source *gs);
+int patmatch(struct grep_pat *p, char *line, char *eol,
+	     regmatch_t *match, int eflags);
 
 struct grep_opt *grep_opt_dup(const struct grep_opt *opt);
 int grep_threads_ok(const struct grep_opt *opt);
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#35 lore raw

PATCH 23/25 xdiff-interface: allow early return from xdiff_emit_{line,hunk}_fn

In reply to Ævar Arnfjörð Bjarmason

Change the function prototype of xdiff_emit_{line,hunk}_fn to return an int instead of void. This will allow for returning early from hunk & diff consumers that want some of the data, but not all of it.

No behavior is being changed here, just replacing the equivalent of "return" with "return 0", nothing acts on the changed return values yet.

There was some work in this area of xdiff-interface.[ch] recently with 3b40a090fd4 (diff: avoid generating unused hunk header lines, 2018-11-02) and 7c61e25fbf1 (diff: use hunk callback for word-diff, 2018-11-02).

In combination those two changes allow us to not do any work on the hunks and diff at all, but didn't change the status quo with regards to consumers that e.g. want the diff lines, but might want to abort early.

Whereas now we can abort e.g. on the first "-line" of a 1000 line diff if that's all we needed.

This interface is rather scary as noted in the comment to xdiff-interface.h being added here, but it will be useful for diffcore-pickaxe.c in a subsequent commit. A future change could e.g. add more exit codes, and hack xdl_emit_diff() and friends to ignore or skip things more selectively as a result.

---
 combine-diff.c     |  9 ++++++---
 diff.c             | 39 +++++++++++++++++++++++----------------
 diffcore-pickaxe.c |  7 ++++---
 range-diff.c       |  8 +++++---
 xdiff-interface.c  | 10 ++++++----
 xdiff-interface.h  | 31 +++++++++++++++++++++++--------
 6 files changed, 67 insertions(+), 37 deletions(-)
combine-diff.c +6 -3
index 9228aebc16..6590c4b5fb 100644
@@ -369,7 +369,7 @@ struct combine_diff_state {
 	struct sline *lost_bucket;
 };
 
-static void consume_hunk(void *state_,
+static int consume_hunk(void *state_,
 			 long ob, long on,
 			 long nb, long nn,
 			 const char *funcline, long funclen)
@@ -401,13 +401,15 @@ static void consume_hunk(void *state_,
 		state->sline[state->nb-1].p_lno =
 			xcalloc(state->num_parent, sizeof(unsigned long));
 	state->sline[state->nb-1].p_lno[state->n] = state->ob;
+
+	return 0;
 }
 
-static void consume_line(void *state_, char *line, unsigned long len)
+static int consume_line(void *state_, char *line, unsigned long len)
 {
 	struct combine_diff_state *state = state_;
 	if (!state->lost_bucket)
-		return; /* not in any hunk yet */
+		return 0; /* not in any hunk yet */
 	switch (line[0]) {
 	case '-':
 		append_lost(state->lost_bucket, state->n, line+1, len-1);
@@ -417,6 +419,7 @@ static void consume_line(void *state_, char *line, unsigned long len)
 		state->lno++;
 		break;
 	}
+	return 0;
 }
 
 static void combine_diff(struct repository *r,
diff.c +23 -16
index 69e3bc00ed..bdedf9fdfa 100644
@@ -1996,10 +1996,10 @@ static int color_words_output_graph_prefix(struct diff_words_data *diff_words)
 	}
 }
 
-static void fn_out_diff_words_aux(void *priv,
-				  long minus_first, long minus_len,
-				  long plus_first, long plus_len,
-				  const char *func, long funclen)
+static int fn_out_diff_words_aux(void *priv,
+				 long minus_first, long minus_len,
+				 long plus_first, long plus_len,
+				 const char *func, long funclen)
 {
 	struct diff_words_data *diff_words = priv;
 	struct diff_words_style *style = diff_words->style;
@@ -2047,6 +2047,8 @@ static void fn_out_diff_words_aux(void *priv,
 
 	diff_words->current_plus = plus_end;
 	diff_words->last_minus = minus_first;
+
+	return 0;
 }
 
 /* This function starts looking at *begin, and returns 0 iff a word was found. */
@@ -2338,7 +2340,7 @@ static void find_lno(const char *line, struct emit_callback *ecbdata)
 	ecbdata->lno_in_postimage = strtol(p + 1, NULL, 10);
 }
 
-static void fn_out_consume(void *priv, char *line, unsigned long len)
+static int fn_out_consume(void *priv, char *line, unsigned long len)
 {
 	struct emit_callback *ecbdata = priv;
 	struct diff_options *o = ecbdata->opt;
@@ -2374,7 +2376,7 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
 		len = sane_truncate_line(line, len);
 		find_lno(line, ecbdata);
 		emit_hunk_header(ecbdata, line, len);
-		return;
+		return 0;
 	}
 
 	if (ecbdata->diff_words) {
@@ -2384,11 +2386,11 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
 		if (line[0] == '-') {
 			diff_words_append(line, len,
 					  &ecbdata->diff_words->minus);
-			return;
+			return 0;
 		} else if (line[0] == '+') {
 			diff_words_append(line, len,
 					  &ecbdata->diff_words->plus);
-			return;
+			return 0;
 		} else if (starts_with(line, "\\ ")) {
 			/*
 			 * Eat the "no newline at eof" marker as if we
@@ -2397,11 +2399,11 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
 			 * defer processing. If this is the end of
 			 * preimage, more "+" lines may come after it.
 			 */
-			return;
+			return 0;
 		}
 		diff_words_flush(ecbdata);
 		emit_diff_symbol(o, s, line, len, 0);
-		return;
+		return 0;
 	}
 
 	switch (line[0]) {
@@ -2425,6 +2427,7 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
 				 line, len, 0);
 		break;
 	}
+	return 0;
 }
 
 static void pprint_rename(struct strbuf *name, const char *a, const char *b)
@@ -2524,7 +2527,7 @@ static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
 	return x;
 }
 
-static void diffstat_consume(void *priv, char *line, unsigned long len)
+static int diffstat_consume(void *priv, char *line, unsigned long len)
 {
 	struct diffstat_t *diffstat = priv;
 	struct diffstat_file *x = diffstat->files[diffstat->nr - 1];
@@ -2533,6 +2536,7 @@ static void diffstat_consume(void *priv, char *line, unsigned long len)
 		x->added++;
 	else if (line[0] == '-')
 		x->deleted++;
+	return 0;
 }
 
 const char mime_boundary_leader[] = "------------";
@@ -3201,16 +3205,17 @@ static int is_conflict_marker(const char *line, int marker_size, unsigned long l
 	return 1;
 }
 
-static void checkdiff_consume_hunk(void *priv,
+static int checkdiff_consume_hunk(void *priv,
 				   long ob, long on, long nb, long nn,
 				   const char *func, long funclen)
 
 {
 	struct checkdiff_t *data = priv;
 	data->lineno = nb - 1;
+	return 0;
 }
 
-static void checkdiff_consume(void *priv, char *line, unsigned long len)
+static int checkdiff_consume(void *priv, char *line, unsigned long len)
 {
 	struct checkdiff_t *data = priv;
 	int marker_size = data->conflict_marker_size;
@@ -3234,7 +3239,7 @@ static void checkdiff_consume(void *priv, char *line, unsigned long len)
 		}
 		bad = ws_check(line + 1, len - 1, data->ws_rule);
 		if (!bad)
-			return;
+			return 0;
 		data->status |= bad;
 		err = whitespace_error_string(bad);
 		fprintf(data->o->file, "%s%s:%d: %s.\n",
@@ -3246,6 +3251,7 @@ static void checkdiff_consume(void *priv, char *line, unsigned long len)
 	} else if (line[0] == ' ') {
 		data->lineno++;
 	}
+	return 0;
 }
 
 static unsigned char *deflate_it(char *data,
@@ -6098,17 +6104,18 @@ void flush_one_hunk(struct object_id *result, git_hash_ctx *ctx)
 	}
 }
 
-static void patch_id_consume(void *priv, char *line, unsigned long len)
+static int patch_id_consume(void *priv, char *line, unsigned long len)
 {
 	struct patch_id_t *data = priv;
 	int new_len;
 
 	if (len > 12 && starts_with(line, "\\ "))
-		return;
+		return 0;
 	new_len = remove_space(line, len);
 
 	the_hash_algo->update_fn(data->ctx, line, new_len);
 	data->patchlen += new_len;
+	return 0;
 }
 
 static void patch_id_add_string(git_hash_ctx *ctx, const char *str)
diffcore-pickaxe.c +4 -3
index 25ab1b2427..21f9d66b6a 100644
@@ -19,7 +19,7 @@ struct diffgrep_cb {
 	int hit;
 };
 
-static void diffgrep_consume(void *priv, char *line, unsigned long len)
+static int diffgrep_consume(void *priv, char *line, unsigned long len)
 {
 	struct diffgrep_cb *data = priv;
 	regmatch_t regmatch;
@@ -27,14 +27,15 @@ static void diffgrep_consume(void *priv, char *line, unsigned long len)
 	struct grep_pat *grep_pat = grep_filter->pattern_list;
 
 	if (line[0] != '+' && line[0] != '-')
-		return;
+		return 0;
 	if (data->hit)
 		/*
 		 * NEEDSWORK: we should have a way to terminate the
 		 * caller early.
 		 */
-		return;
+		return 0;
 	data->hit = patmatch(grep_pat, line + 1, line + len + 1, &regmatch, 0);
+	return 0;
 }
 
 static int diff_grep(mmfile_t *one, mmfile_t *two,
range-diff.c +5 -3
index b9950f10c8..cb40f67a26 100644
@@ -267,15 +267,17 @@ static void find_exact_matches(struct string_list *a, struct string_list *b)
 	hashmap_clear(&map);
 }
 
-static void diffsize_consume(void *data, char *line, unsigned long len)
+static int diffsize_consume(void *data, char *line, unsigned long len)
 {
 	(*(int *)data)++;
+	return 0;
 }
 
-static void diffsize_hunk(void *data, long ob, long on, long nb, long nn,
-			  const char *funcline, long funclen)
+static int diffsize_hunk(void *data, long ob, long on, long nb, long nn,
+			 const char *funcline, long funclen)
 {
 	diffsize_consume(data, NULL, 0);
+	return 0;
 }
 
 static int diffsize(const char *a, const char *b)
xdiff-interface.c +6 -4
index 4d20069302..ef557dc4e6 100644
@@ -31,7 +31,7 @@ static int xdiff_out_hunk(void *priv_,
 	return 0;
 }
 
-static void consume_one(void *priv_, char *s, unsigned long size)
+static int consume_one(void *priv_, char *s, unsigned long size)
 {
 	struct xdiff_emit_state *priv = priv_;
 	char *ep;
@@ -43,6 +43,7 @@ static void consume_one(void *priv_, char *s, unsigned long size)
 		size -= this_size;
 		s += this_size;
 	}
+	return 0;
 }
 
 static int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
@@ -115,10 +116,11 @@ int xdi_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t co
 	return xdl_diff(&a, &b, xpp, xecfg, xecb);
 }
 
-void discard_hunk_line(void *priv,
-		       long ob, long on, long nb, long nn,
-		       const char *func, long funclen)
+int discard_hunk_line(void *priv,
+		      long ob, long on, long nb, long nn,
+		      const char *func, long funclen)
 {
+	return 0;
 }
 
 int xdi_diff_outf(mmfile_t *mf1, mmfile_t *mf2,
xdiff-interface.h +23 -8
index 93df26900c..1b27d6104c 100644
@@ -11,11 +11,26 @@
  */
 #define MAX_XDIFF_SIZE (1024UL * 1024 * 1023)
 
-typedef void (*xdiff_emit_line_fn)(void *, char *, unsigned long);
-typedef void (*xdiff_emit_hunk_fn)(void *data,
-				   long old_begin, long old_nr,
-				   long new_begin, long new_nr,
-				   const char *func, long funclen);
+/*
+ * The xdiff_emit_{line,hunk}_fn consumers can return -1 to abort
+ * early, or 0 to continue processing. Note that doing so is an
+ * all-or-nothing affair, as returning -1 will return all the way to
+ * the top-level, e.g. the xdi_diff_outf() call to generate the diff.
+ *
+ * Thus returning -1 from a hunk header callback means you won't be
+ * getting any more hunks, or diffs, and likewise returning from a
+ * line callback means you won't be getting anymore lines.
+ *
+ * We may extend the interface in the future to understand other more
+ * granular return values, but for now use it carefully, or consider
+ * e.g. using discard_hunk_line() if you say just don't care about
+ * hunk headers.
+ */
+typedef int (*xdiff_emit_line_fn)(void *, char *, unsigned long);
+typedef int (*xdiff_emit_hunk_fn)(void *data,
+				  long old_begin, long old_nr,
+				  long new_begin, long new_nr,
+				  const char *func, long funclen);
 
 int xdi_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t const *xecfg, xdemitcb_t *ecb);
 int xdi_diff_outf(mmfile_t *mf1, mmfile_t *mf2,
@@ -36,9 +51,9 @@ extern int git_xmerge_style;
  * Can be used as a no-op hunk_fn for xdi_diff_outf(), since a NULL
  * one just sends the hunk line to the line_fn callback).
  */
-void discard_hunk_line(void *priv,
-		       long ob, long on, long nb, long nn,
-		       const char *func, long funclen);
+int discard_hunk_line(void *priv,
+		      long ob, long on, long nb, long nn,
+		      const char *func, long funclen);
 
 /*
  * Compare the strings l1 with l2 which are of size s1 and s2 respectively.
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#36 lore raw

PATCH 24/25 xdiff-interface: support early exit in xdiff_outf()

In reply to Ævar Arnfjörð Bjarmason

Bridge the gap between the preceding "xdiff-interface: allow early return from xdiff_emit_{line,hunk}_fn" change and the public interface. This change was split off from the rest as it wasn't a purely mechanical addition of "return 0".

Here we want to be able to abort early, but do so in a way that doesn't skip the appropriate strbuf_reset() invocations.

The use of -1 as a return value in the xdiff codebase for early return, as we'll see more of in subsequent commits.

---
 xdiff-interface.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
xdiff-interface.c +12 -4
index ef557dc4e6..d066442470 100644
@@ -39,7 +39,8 @@ static int consume_one(void *priv_, char *s, unsigned long size)
 		unsigned long this_size;
 		ep = memchr(s, '\n', size);
 		this_size = (ep == NULL) ? size : (ep - s + 1);
-		priv->line_fn(priv->consume_callback_data, s, this_size);
+		if (priv->line_fn(priv->consume_callback_data, s, this_size))
+			return -1;
 		size -= this_size;
 		s += this_size;
 	}
@@ -50,11 +51,14 @@ static int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
 {
 	struct xdiff_emit_state *priv = priv_;
 	int i;
+	int stop = 0;
 
 	if (!priv->line_fn)
 		return 0;
 
 	for (i = 0; i < nbuf; i++) {
+		if (stop)
+			return -1;
 		if (mb[i].ptr[mb[i].size-1] != '\n') {
 			/* Incomplete line */
 			strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
@@ -63,17 +67,21 @@ static int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
 
 		/* we have a complete line */
 		if (!priv->remainder.len) {
-			consume_one(priv, mb[i].ptr, mb[i].size);
+			stop = consume_one(priv, mb[i].ptr, mb[i].size);
 			continue;
 		}
 		strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
-		consume_one(priv, priv->remainder.buf, priv->remainder.len);
+		stop = consume_one(priv, priv->remainder.buf, priv->remainder.len);
 		strbuf_reset(&priv->remainder);
 	}
+	if (stop)
+		return -1;
 	if (priv->remainder.len) {
-		consume_one(priv, priv->remainder.buf, priv->remainder.len);
+		stop = consume_one(priv, priv->remainder.buf, priv->remainder.len);
 		strbuf_reset(&priv->remainder);
 	}
+	if (stop)
+		return -1;
 	return 0;
 }
 
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#20 lore raw

PATCH 17/25 pickaxe tests: add test for "log -S" not being a regex

In reply to Ævar Arnfjörð Bjarmason

No test in our test suite checked for "log -S<pat>" being a fixed string, as opposed to "log -S<pat> --pickaxe-regex". Let's test for it.

---
 t/t4209-log-pickaxe.sh | 11 +++++++++++
 1 file changed, 11 insertions(+)
t/t4209-log-pickaxe.sh +11 -0
index ebd51f498b..b59aaecc68 100755
@@ -121,6 +121,17 @@ test_expect_success 'log -G trims diff new/old [-+]' '
 	test_cmp log full-log
 '
 
+test_expect_success 'log -S<pat> is not a regex, but -S<pat> --pickaxe-regex is' '
+	git -C GS-plain log -S"a" >log &&
+	test_cmp log full-log &&
+
+	git -C GS-plain log -S"[a]" >log &&
+	test_must_be_empty log &&
+
+	git -C GS-plain log -S"[a]" --pickaxe-regex >log &&
+	test_cmp log full-log
+'
+
 test_expect_success 'setup log -[GS] binary & --text' '
 	test_create_repo GS-bin-txt &&
 	test_commit -C GS-bin-txt --append A data.bin "a\na\0a\n" &&
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#22 lore raw

PATCH 19/25 pickaxe -G: set -U0 for diff generation

In reply to Ævar Arnfjörð Bjarmason

Set the equivalent of -U0 when generating diffs for "git log -G". As seen in diffgrep_consume() we ignore any lines that aren't the "+" and "-" lines, so the rest of the output wasn't being used.

It turns out that we spent quite a bit of CPU just on this[1]:

    Test                                             HEAD~             HEAD
    -----------------------------------------------------------------------------------------
    4209.2: git log -G'a' <limit-rev>..              0.60(0.54+0.06)   0.52(0.46+0.05) -13.3%
    4209.8: git log -G'uncommon' <limit-rev>..       0.61(0.54+0.07)   0.53(0.47+0.06) -13.1%
    4209.14: git log -G'[þæö]' <limit-rev>..         0.60(0.55+0.04)   0.56(0.48+0.04) -6.7%
    4209.21: git log -i -G'a' <limit-rev>..          0.63(0.56+0.03)   0.54(0.48+0.05) -14.3%
    4209.27: git log -i -G'uncommon' <limit-rev>..   0.61(0.55+0.05)   0.53(0.47+0.06) -13.1%
    4209.33: git log -i -G'[þæö]' <limit-rev>..      0.61(0.53+0.07)   0.53(0.47+0.05) -13.1%

I also experimented with setting diff.interHunkContext to 10, 100 etc. As noted above it's useless for -G to have non-"+" and non-"-" lines for the matching itself, but there's going to be some sweet spot where if we can be handed bigger hunks at a time our matching might be faster.

But alas, the results of that were:

    Test                                             HEAD~2            HEAD~                    HEAD
    ------------------------------------------------------------------------------------------------------------------
    4209.2: git log -G'a' <limit-rev>..              0.61(0.53+0.07)   0.51(0.46+0.05) -16.4%   0.51(0.46+0.05) -16.4%
    4209.8: git log -G'uncommon' <limit-rev>..       0.66(0.55+0.05)   0.53(0.48+0.04) -19.7%   0.52(0.49+0.03) -21.2%
    4209.14: git log -G'[þæö]' <limit-rev>..         0.63(0.54+0.06)   0.51(0.44+0.07) -19.0%   0.52(0.46+0.06) -17.5%
    4209.21: git log -i -G'a' <limit-rev>..          0.62(0.54+0.07)   0.51(0.46+0.04) -17.7%   0.53(0.45+0.07) -14.5%
    4209.27: git log -i -G'uncommon' <limit-rev>..   0.62(0.56+0.06)   0.53(0.48+0.05) -14.5%   0.53(0.46+0.07) -14.5%
    4209.33: git log -i -G'[þæö]' <limit-rev>..      0.63(0.57+0.03)   0.58(0.46+0.06) -7.9%    0.53(0.46+0.06) -15.9%

I.e. maybe it's faster in some cases, but probably slower in general.

Those results are going to be crappy because we're matching a line at a time, as opposed to some version of /m matching across the whole diff (if possible). So that approach might be worth revisiting in the future.

1. GIT_SKIP_TESTS="p4209.[1379] p4209.15 p4209.2[028] p4209.34" GIT_PERF_EXTRA= GIT_PERF_REPO=~/g/git/ GIT_PERF_REPEAT_COUNT=5 GIT_PERF_MAKE_OPTS='-j8 USE_LIBPCRE=Y CFLAGS=-O3 LIBPCREDIR=/home/avar/g/pcre2/inst' ./run HEAD~ HEAD -- p4209-pickaxe.sh

---
 diffcore-pickaxe.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diffcore-pickaxe.c +1 -1
index cb865c8b29..5161c81057 100644
@@ -60,7 +60,7 @@ static int diff_grep(mmfile_t *one, mmfile_t *two,
 	memset(&xecfg, 0, sizeof(xecfg));
 	ecbdata.regexp = regexp;
 	ecbdata.hit = 0;
-	xecfg.ctxlen = o->context;
+	xecfg.ctxlen = 0;
 	xecfg.interhunkctxlen = o->interhunkcontext;
 	if (xdi_diff_outf(one, two, discard_hunk_line, diffgrep_consume,
 			  &ecbdata, &xpp, &xecfg))
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#18 lore raw

PATCH 15/25 pickaxe/style: consolidate declarations and assignments

In reply to Ævar Arnfjörð Bjarmason

Refactor contains() to do its assignments at the same time that it does its declarations.

This code could have been refactored in ef90ab66e8e (pickaxe: use textconv for -S counting, 2012-10-28) when a function call between the declarations and assignments was removed.

---
 diffcore-pickaxe.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
diffcore-pickaxe.c +3 -7
index 8df76afb6e..cb865c8b29 100644
@@ -70,13 +70,9 @@ static int diff_grep(mmfile_t *one, mmfile_t *two,
 
 static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws)
 {
-	unsigned int cnt;
-	unsigned long sz;
-	const char *data;
-
-	sz = mf->size;
-	data = mf->ptr;
-	cnt = 0;
+	unsigned int cnt = 0;
+	unsigned long sz = mf->size;
+	const char *data = mf->ptr;
 
 	if (regexp) {
 		regmatch_t regmatch;
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#26 lore raw

PATCH 21/25 pickaxe: use PCREv2 for -G and -S

In reply to Ævar Arnfjörð Bjarmason

Follow-up b65abcafc7a (grep: use PCRE v2 for optimized fixed-string search, 2019-07-01) and remove the use of kwset in the pickaxe code for fixed-string search, in favor of optimistically using PCRE v2.

This does mean that the semantics of the -G option subtly change, before it's an ERE, whereas now it'll be a PCRE if we're compiled with PCRE. Since PCRE is almost entirely a strict superset of ERE syntax I think this is OK.

Now when running the newly added t/perf/p4209-pickaxe.sh[1] and the latest PCRE v2 we'll get the following performance improvements (well, mostly improvements):

    Test                                                                           origin/next       HEAD
    -----------------------------------------------------------------------------------------------------------------------
    4209.1: git log --pickaxe-regex -S'[þæö]' <limit-rev>..                        0.41(0.37+0.03)   0.42(0.37+0.05) +2.4%
    4209.2: git log -G'a' <limit-rev>..                                            0.61(0.52+0.08)   0.50(0.45+0.05) -18.0%
    4209.3: git log --pickaxe-regex -S'a' <limit-rev>..                            0.73(0.66+0.07)   0.42(0.37+0.05) -42.5%
    4209.4: git log --text -G'a' <limit-rev>..                                     0.61(0.54+0.06)   0.50(0.44+0.05) -18.0%
    4209.5: git log --text --pickaxe-all -G'a' <limit-rev>..                       0.44(0.37+0.06)   0.46(0.36+0.06) +4.5%
    4209.6: git log --pickaxe-all -G'a' <limit-rev>..                              0.46(0.39+0.07)   0.38(0.33+0.05) -17.4%
    4209.7: git log --pickaxe-all --pickaxe-regex -S'a' <limit-rev>..              0.55(0.48+0.06)   0.35(0.30+0.04) -36.4%
    4209.8: git log -G'uncommon' <limit-rev>..                                     0.68(0.60+0.07)   0.59(0.53+0.05) -13.2%
    4209.9: git log --pickaxe-regex -S'uncommon' <limit-rev>..                     0.48(0.45+0.03)   0.36(0.31+0.05) -25.0%
    4209.10: git log --text -G'uncommon' <limit-rev>..                             0.66(0.58+0.07)   0.58(0.52+0.04) -12.1%
    4209.11: git log --text --pickaxe-all -G'uncommon' <limit-rev>..               0.67(0.61+0.05)   0.57(0.52+0.05) -14.9%
    4209.12: git log --pickaxe-all -G'uncommon' <limit-rev>..                      0.62(0.55+0.06)   0.52(0.46+0.06) -16.1%
    4209.13: git log --pickaxe-all --pickaxe-regex -S'uncommon' <limit-rev>..      0.49(0.43+0.06)   0.31(0.26+0.04) -36.7%
    4209.14: git log -G'[þæö]' <limit-rev>..                                       0.71(0.64+0.07)   0.51(0.47+0.04) -28.2%
    4209.15: git log --pickaxe-regex -S'[þæö]' <limit-rev>..                       0.45(0.40+0.05)   0.42(0.37+0.04) -6.7%
    4209.16: git log --text -G'[þæö]' <limit-rev>..                                0.64(0.56+0.07)   0.50(0.44+0.06) -21.9%
    4209.17: git log --text --pickaxe-all -G'[þæö]' <limit-rev>..                  0.64(0.54+0.09)   0.50(0.47+0.03) -21.9%
    4209.18: git log --pickaxe-all -G'[þæö]' <limit-rev>..                         0.66(0.59+0.07)   0.50(0.45+0.05) -24.2%
    4209.19: git log --pickaxe-all --pickaxe-regex -S'[þæö]' <limit-rev>..         0.42(0.38+0.04)   0.41(0.37+0.04) -2.4%
    4209.20: git log -i --pickaxe-regex -S'[þæö]' <limit-rev>..                    0.41(0.38+0.03)   0.49(0.43+0.05) +19.5%
    4209.21: git log -i -G'a' <limit-rev>..                                        0.63(0.61+0.02)   0.50(0.45+0.05) -20.6%
    4209.22: git log -i --pickaxe-regex -S'a' <limit-rev>..                        0.81(0.75+0.06)   0.60(0.58+0.02) -25.9%
    4209.23: git log -i --text -G'a' <limit-rev>..                                 0.64(0.54+0.10)   0.49(0.44+0.05) -23.4%
    4209.24: git log -i --text --pickaxe-all -G'a' <limit-rev>..                   0.47(0.43+0.04)   0.37(0.31+0.06) -21.3%
    4209.25: git log -i --pickaxe-all -G'a' <limit-rev>..                          0.51(0.43+0.08)   0.37(0.31+0.06) -27.5%
    4209.26: git log -i --pickaxe-all --pickaxe-regex -S'a' <limit-rev>..          0.62(0.55+0.05)   0.46(0.41+0.04) -25.8%
    4209.27: git log -i -G'uncommon' <limit-rev>..                                 0.64(0.58+0.05)   0.51(0.48+0.03) -20.3%
    4209.28: git log -i --pickaxe-regex -S'uncommon' <limit-rev>..                 0.48(0.44+0.04)   0.47(0.42+0.05) -2.1%
    4209.29: git log -i --text -G'uncommon' <limit-rev>..                          0.62(0.53+0.08)   0.51(0.44+0.07) -17.7%
    4209.30: git log -i --text --pickaxe-all -G'uncommon' <limit-rev>..            0.61(0.53+0.08)   0.51(0.45+0.06) -16.4%
    4209.31: git log -i --pickaxe-all -G'uncommon' <limit-rev>..                   0.63(0.57+0.05)   0.51(0.47+0.04) -19.0%
    4209.32: git log -i --pickaxe-all --pickaxe-regex -S'uncommon' <limit-rev>..   0.47(0.42+0.04)   0.47(0.45+0.01) +0.0%
    4209.33: git log -i -G'[þæö]' <limit-rev>..                                    0.60(0.54+0.05)   0.51(0.47+0.04) -15.0%
    4209.34: git log -i --pickaxe-regex -S'[þæö]' <limit-rev>..                    0.39(0.33+0.05)   0.49(0.45+0.03) +25.6%
    4209.35: git log -i --text -G'[þæö]' <limit-rev>..                             0.59(0.53+0.06)   0.51(0.47+0.02) -13.6%
    4209.36: git log -i --text --pickaxe-all -G'[þæö]' <limit-rev>..               0.62(0.56+0.05)   0.51(0.45+0.04) -17.7%
    4209.37: git log -i --pickaxe-all -G'[þæö]' <limit-rev>..                      0.61(0.55+0.06)   0.51(0.47+0.04) -16.4%
    4209.38: git log -i --pickaxe-all --pickaxe-regex -S'[þæö]' <limit-rev>..      0.42(0.39+0.02)   0.49(0.46+0.03) +16.7%

1. With these options:

GIT_PERF_EXTRA=1 GIT_PERF_REPEAT_COUNT=10 GIT_PERF_MAKE_OPTS='-j8 USE_LIBPCRE=Y CFLAGS=-O3 LIBPCREDIR=/home/avar/g/pcre2/inst' ./run origin/next HEAD -- p4209-pickaxe.sh

---
 diff.h             |   4 ++
 diffcore-pickaxe.c | 143 ++++++++++++++++++---------------------------
 2 files changed, 61 insertions(+), 86 deletions(-)
diff.h +4 -0
index 2ff2b1c7f2..2f369c162b 100644
@@ -365,6 +365,8 @@ struct diff_options {
 
 	struct repository *repo;
 	struct option *parseopts;
+
+	struct grep_opt *grep_filter;
 };
 
 unsigned diff_filter_bit(char status);
@@ -520,6 +522,8 @@ int git_config_rename(const char *var, const char *value);
 #define DIFF_PICKAXE_KIND_G	8 /* grep in the patch */
 #define DIFF_PICKAXE_KIND_OBJFIND	16 /* specific object IDs */
 
+#define DIFF_PICKAXE_KIND_GS_MASK (DIFF_PICKAXE_KIND_S | \
+				   DIFF_PICKAXE_KIND_G)
 #define DIFF_PICKAXE_KINDS_MASK (DIFF_PICKAXE_KIND_S | \
 				 DIFF_PICKAXE_KIND_G | \
 				 DIFF_PICKAXE_KIND_OBJFIND)
diffcore-pickaxe.c +57 -86

245 lines not on this page · raw message

Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#30 lore raw

PATCH 22/25 Remove unused kwset.[ch]

In reply to Ævar Arnfjörð Bjarmason

Remove the unused kwset matching engine. It has not been used by grep itself since 48de2a768cf (grep: remove the kwset optimization, 2019-07-01), and in a previous commit we removed its last remaining use in diffcore-pickaxe.c.

This matching engine is much faster than the C library's regex matching for fixed strings, but slower than PCREv2, which we can make use of in the common case.

It's also an increasingly bitrotting version of the GPLv2 code, whose upstream has moved onto the incompatible GPLv3, so getting rid of the need to maintain a permanent fork of it is a good thing.

---
 Makefile          |   2 -
 compat/obstack.c  | 413 ------------------------
 compat/obstack.h  | 511 ------------------------------
 ctype.c           |  36 ---
 git-compat-util.h |   3 -
 kwset.c           | 775 ----------------------------------------------
 kwset.h           |  65 ----
 7 files changed, 1805 deletions(-)
 delete mode 100644 compat/obstack.c
 delete mode 100644 compat/obstack.h
 delete mode 100644 kwset.c
 delete mode 100644 kwset.h
Makefile +0 -2
index 5a239cac20..570b78a528 100644
@@ -840,7 +840,6 @@ LIB_OBJS += combine-diff.o
 LIB_OBJS += commit-graph.o
 LIB_OBJS += commit-reach.o
 LIB_OBJS += commit.o
-LIB_OBJS += compat/obstack.o
 LIB_OBJS += compat/terminal.o
 LIB_OBJS += config.o
 LIB_OBJS += connect.o
@@ -888,7 +887,6 @@ LIB_OBJS += help.o
 LIB_OBJS += hex.o
 LIB_OBJS += ident.o
 LIB_OBJS += json-writer.o
-LIB_OBJS += kwset.o
 LIB_OBJS += levenshtein.o
 LIB_OBJS += line-log.o
 LIB_OBJS += line-range.o
compat/obstack.c deleted +0 -413

413 lines not on this page · raw message

compat/obstack.h deleted +0 -511

511 lines not on this page · raw message

ctype.c +0 -36
index fc0225cebd..3451745550 100644
@@ -28,39 +28,3 @@ const unsigned char sane_ctype[256] = {
 	A, A, A, A, A, A, A, A, A, A, A, R, R, U, P, X,		/* 112..127 */
 	/* Nothing in the 128.. range */
 };
-
-/* For case-insensitive kwset */
-const unsigned char tolower_trans_tbl[256] = {
-	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
-	0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
-	0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
-	0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
-	 ' ',  '!',  '"',  '#',  '$',  '%',  '&', 0x27,
-	 '(',  ')',  '*',  '+',  ',',  '-',  '.',  '/',
-	 '0',  '1',  '2',  '3',  '4',  '5',  '6',  '7',
-	 '8',  '9',  ':',  ';',  '<',  '=',  '>',  '?',
-	 '@',  'a',  'b',  'c',  'd',  'e',  'f',  'g',
-	 'h',  'i',  'j',  'k',  'l',  'm',  'n',  'o',
-	 'p',  'q',  'r',  's',  't',  'u',  'v',  'w',
-	 'x',  'y',  'z',  '[', 0x5c,  ']',  '^',  '_',
-	 '`',  'a',  'b',  'c',  'd',  'e',  'f',  'g',
-	 'h',  'i',  'j',  'k',  'l',  'm',  'n',  'o',
-	 'p',  'q',  'r',  's',  't',  'u',  'v',  'w',
-	 'x',  'y',  'z',  '{',  '|',  '}',  '~', 0x7f,
-	0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
-	0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
-	0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
-	0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
-	0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
-	0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
-	0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
-	0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
-	0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
-	0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
-	0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
-	0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
-	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
-	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
-	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
-	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
-};
git-compat-util.h +0 -3
index 5d5e47fbe2..1f4e740773 100644
@@ -986,9 +986,6 @@ int xsnprintf(char *dst, size_t max, const char *fmt, ...);
 
 int xgethostname(char *buf, size_t len);
 
-/* in ctype.c, for kwset users */
-extern const unsigned char tolower_trans_tbl[256];
-
 /* Sane ctype - no locale, and works with signed chars */
 #undef isascii
 #undef isspace
kwset.c deleted +0 -775

775 lines not on this page · raw message

kwset.h deleted +0 -65

65 lines not on this page · raw message

Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#38 lore raw

PATCH 25/25 pickaxe -G: terminate early on matching lines

In reply to Ævar Arnfjörð Bjarmason

Solve a long-standing item for "git log -Grx" of us e.g. finding "+ str" in the diff context and noting that we had a "hit", but xdiff diligently continuing to generate and spew the rest of the diff at us.

The TODO item has been there since "git log -G" was implemented. See f506b8e8b5f (git log/diff: add -G<regexp> that greps in the patch text, 2010-08-23).

Our xdiff interface also had the limitation of not being able to abort early since the beginning, see d9ea73e0564 (combine-diff: refactor built-in xdiff interface., 2006-04-05). Although at that time "xdiff_emit_line_fn" was called "xdiff_emit_consume_fn", and "xdiff_emit_hunk_fn" didn't exist yet.

But now with the support added in the preceding ""xdiff-interface:
allow early return from xdiff_emit_{line,hunk}_fn" commit we can return early, and furthermore test the functionality of the new early-exit xdiff-interface by having a BUG() call here to die if it ever starts handing us needless work again.

---
 diffcore-pickaxe.c | 29 +++++++++++++++++++----------
 xdiff-interface.h  |  5 +++++
 2 files changed, 24 insertions(+), 10 deletions(-)
diffcore-pickaxe.c +19 -10
index 21f9d66b6a..e773fa69a2 100644
@@ -29,12 +29,11 @@ static int diffgrep_consume(void *priv, char *line, unsigned long len)
 	if (line[0] != '+' && line[0] != '-')
 		return 0;
 	if (data->hit)
-		/*
-		 * NEEDSWORK: we should have a way to terminate the
-		 * caller early.
-		 */
-		return 0;
-	data->hit = patmatch(grep_pat, line + 1, line + len + 1, &regmatch, 0);
+		BUG("Already matched in diffgrep_consume! Broken xdiff_emit_line_fn?");
+	if (patmatch(grep_pat, line + 1, line + len + 1, &regmatch, 0)) {
+		data->hit = 1;
+		return -1;
+	}
 	return 0;
 }
 
@@ -47,6 +46,7 @@ static int diff_grep(mmfile_t *one, mmfile_t *two,
 	xdemitconf_t xecfg;
 	regmatch_t regmatch;
 	struct grep_pat *grep_pat = grep_filter->pattern_list;
+	int ret;
 
 	if (!one)
 		return patmatch(grep_pat, two->ptr, two->ptr + two->size,
@@ -65,10 +65,19 @@ static int diff_grep(mmfile_t *one, mmfile_t *two,
 	ecbdata.hit = 0;
 	xecfg.ctxlen = 0;
 	xecfg.interhunkctxlen = o->interhunkcontext;
-	if (xdi_diff_outf(one, two, discard_hunk_line, diffgrep_consume,
-			  &ecbdata, &xpp, &xecfg))
-		return 0;
-	return ecbdata.hit;
+
+	/*
+	 * An xdiff error might be our "data->hit" from above. See the
+	 * comment for xdiff_emit_{line,hunk}_fn in xdiff-interface.h
+	 * for why.
+	 */
+	ret = xdi_diff_outf(one, two, discard_hunk_line, diffgrep_consume,
+			    &ecbdata, &xpp, &xecfg);
+	if (ecbdata.hit)
+		return 1;
+	if (ret)
+		return ret;
+	return 0;
 }
 
 static unsigned int contains(mmfile_t *mf, struct grep_opt *grep_filter)
xdiff-interface.h +5 -0
index 1b27d6104c..347d8a4425 100644
@@ -25,6 +25,11 @@
  * granular return values, but for now use it carefully, or consider
  * e.g. using discard_hunk_line() if you say just don't care about
  * hunk headers.
+ *
+ * Note that just returning -1 will make your early return
+ * indistinguishable from an error internal to xdiff. See "diff_grep"
+ * in diffcore-pickaxe.c for a trick to work around this, i.e. using
+ * the "consume_callback_data" to note the desired early return.
  */
 typedef int (*xdiff_emit_line_fn)(void *, char *, unsigned long);
 typedef int (*xdiff_emit_hunk_fn)(void *data,
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#39 lore raw
In reply to Ævar Arnfjörð Bjarmason

I'm aware of a CI failure related to this series:

Quoted 1 line · Ævar Arnfjörð Bjarmason

pickaxe tests: refactor to use test_commit --append

It's because here I fed "\0" etc. to "echo" instead of "printf", which isn't portable. I've got a fix for this locally, but want to wait for more comments before sending a re-roll.

#32 lore raw

Re: [PATCH 22/25] Remove unused kwset.[ch]

In reply to a message not in the archive
Quoted 4 lines · Carlo Arenas

this is still being used in Linux without Glibc (ex: alpine or void that use musl), and other OS that rely on the compat layer by default for legacy reasons (ex: macOS and Windows) or where PCRE2 is not widely used/available (ex: OpenBSD, NetBSD)

Are you perhaps confusing kwset with NO_REGEX=1 and compat/regex/*? I just say that because that's a common "Linux without Glibc" && musl fallback.

The kwset is not a fallback, we use it unconditionally on all platforms regardless of libc etc., until this series.

Anyway, as far as PCRE v2 and compatibility go there's no compatibility concerns here we didn't address already & have in in-the-wild git releases since Since 48de2a768c (grep: remove the kwset optimization, 2019-07-01) and b65abcafc7 (grep: use PCRE v2 for optimized fixed-string search, 2019-07-01).

I.e. we haven't used kwset at all in the more commonly used grep code, just C library regex + PCRE if it's available.

FWIW I think the commonly used packages on Windows and MacOS build with PCRE v2, but that's just from memory, I don't use those myself.

Quoted 3 lines

PS. hadn't yet tested this series, but thought it was a good idea to at least mention this as a FYI to make sure tradeoffs are well known and testing done as well.

Indeed, and a glance at the log & past list traffic shows we had a lot of such portability fixes around pcre2 in the past.

But in this case we're just directing the pickaxe to use the existing grep codepath, so I think we should be fine, sans small stuff like (sent a reply to the cover letter about this) me screwing up shell portability in the v1 etc.

#23 lore raw

Re: [PATCH 19/25] pickaxe -G: set -U0 for diff generation

In reply to Ævar Arnfjörð Bjarmason
Quoted 51 lines · Ævar Arnfjörð Bjarmason

Set the equivalent of -U0 when generating diffs for "git log -G". As seen in diffgrep_consume() we ignore any lines that aren't the "+" and "-" lines, so the rest of the output wasn't being used.

It turns out that we spent quite a bit of CPU just on this[1]:

    Test                                             HEAD~             HEAD
    -----------------------------------------------------------------------------------------
    4209.2: git log -G'a' <limit-rev>..              0.60(0.54+0.06)   0.52(0.46+0.05) -13.3%
    4209.8: git log -G'uncommon' <limit-rev>..       0.61(0.54+0.07)   0.53(0.47+0.06) -13.1%
    4209.14: git log -G'[þæö]' <limit-rev>..         0.60(0.55+0.04)   0.56(0.48+0.04) -6.7%
    4209.21: git log -i -G'a' <limit-rev>..          0.63(0.56+0.03)   0.54(0.48+0.05) -14.3%
    4209.27: git log -i -G'uncommon' <limit-rev>..   0.61(0.55+0.05)   0.53(0.47+0.06) -13.1%
    4209.33: git log -i -G'[þæö]' <limit-rev>..      0.61(0.53+0.07)   0.53(0.47+0.05) -13.1%

I also experimented with setting diff.interHunkContext to 10, 100 etc. As noted above it's useless for -G to have non-"+" and non-"-" lines for the matching itself, but there's going to be some sweet spot where if we can be handed bigger hunks at a time our matching might be faster.

But alas, the results of that were:

    Test                                             HEAD~2            HEAD~                    HEAD
    ------------------------------------------------------------------------------------------------------------------
    4209.2: git log -G'a' <limit-rev>..              0.61(0.53+0.07)   0.51(0.46+0.05) -16.4%   0.51(0.46+0.05) -16.4%
    4209.8: git log -G'uncommon' <limit-rev>..       0.66(0.55+0.05)   0.53(0.48+0.04) -19.7%   0.52(0.49+0.03) -21.2%
    4209.14: git log -G'[þæö]' <limit-rev>..         0.63(0.54+0.06)   0.51(0.44+0.07) -19.0%   0.52(0.46+0.06) -17.5%
    4209.21: git log -i -G'a' <limit-rev>..          0.62(0.54+0.07)   0.51(0.46+0.04) -17.7%   0.53(0.45+0.07) -14.5%
    4209.27: git log -i -G'uncommon' <limit-rev>..   0.62(0.56+0.06)   0.53(0.48+0.05) -14.5%   0.53(0.46+0.07) -14.5%
    4209.33: git log -i -G'[þæö]' <limit-rev>..      0.63(0.57+0.03)   0.58(0.46+0.06) -7.9%    0.53(0.46+0.06) -15.9%

I.e. maybe it's faster in some cases, but probably slower in general.

Those results are going to be crappy because we're matching a line at a time, as opposed to some version of /m matching across the whole diff (if possible). So that approach might be worth revisiting in the future.

1. GIT_SKIP_TESTS="p4209.[1379] p4209.15 p4209.2[028] p4209.34" GIT_PERF_EXTRA= GIT_PERF_REPO=~/g/git/ GIT_PERF_REPEAT_COUNT=5 GIT_PERF_MAKE_OPTS='-j8 USE_LIBPCRE=Y CFLAGS=-O3 LIBPCREDIR=/home/avar/g/pcre2/inst' ./run HEAD~ HEAD -- p4209-pickaxe.sh

---
 diffcore-pickaxe.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diffcore-pickaxe.c +0 -0
index cb865c8b29..5161c81057 100644

diffcore-pickaxe.c static int diff_grep(mmfile_t *one, mmfile_t *two, · line 66

Quoted 8 lines
diffcore-pickaxe.c +1 -1
@@ -60,7 +60,7 @@ static int diff_grep(mmfile_t *one, mmfile_t *two,
 	memset(&xecfg, 0, sizeof(xecfg));
 	ecbdata.regexp = regexp;
 	ecbdata.hit = 0;
-	xecfg.ctxlen = o->context;
+	xecfg.ctxlen = 0;
 	xecfg.interhunkctxlen = o->interhunkcontext;
 	if (xdi_diff_outf(one, two, discard_hunk_line, diffgrep_consume,
 			  &ecbdata, &xpp, &xecfg))

I since discovered Junio's f01cae918f (diff: teach --stat/--numstat to honor -U$num, 2011-09-22) (as an aside we have no test for that behavior).

I haven't looked carefully, but I don't think we'll have the same issue here, as pickaxe currently doesn't care about whether something is on the + or - line, when briefly looking at the diffstat edge cases it seems that's what differs based on -U<n> for the diffstat.

#34 lore raw

Re: [PATCH 22/25] Remove unused kwset.[ch]

In reply to a message not in the archive
Quoted 18 lines · Carlo Arenas
Quoted 5 lines · Ævar Arnfjörð Bjarmason
Quoted 2 lines · Carlo Arenas

this is still being used in Linux without Glibc (ex: alpine or void that use musl), and other OS that rely on the compat layer by default for

Quoted 9 lines

legacy

Quoted 1 line

reasons (ex: macOS and Windows) or where PCRE2 is not widely

used/available

Quoted 1 line

(ex: OpenBSD, NetBSD)

Are you perhaps confusing kwset with NO_REGEX=1 and compat/regex/*? I just say that because that's a common "Linux without Glibc" && musl fallback.

Quoted 6 lines

Indeed, my bad!

FWIW I was not arguing against this patchset with my comment, and I agree with you that the less old unmaintainable code git has, the better. will give this patchset a spin as time allows and hopefully report back with some more useful feedback.

No worries, the whole fallback mechanism is quite confusing. One thing we've got going for us is a hard dependency on REG_STARTEND.

I'm not doing this in this series, but I do have my eye on eventually migrating other things that use regexec_buf() (and thus might need to match across a \0) to the grep API, and dropping compat/regex/ in favor of some version of compat/pcre2/.

#24 lore raw

Re: [PATCH 19/25] pickaxe -G: set -U0 for diff generation

In reply to Ævar Arnfjörð Bjarmason
Quoted 8 lines · Ævar Arnfjörð Bjarmason

I since discovered Junio's f01cae918f (diff: teach --stat/--numstat to honor -U$num, 2011-09-22) (as an aside we have no test for that behavior).

I haven't looked carefully, but I don't think we'll have the same issue here, as pickaxe currently doesn't care about whether something is on the + or - line, when briefly looking at the diffstat edge cases it seems that's what differs based on -U<n> for the diffstat.

With -U0 or different <n> in general, the matching between preimage and postimage may become different, and both -U3 (usual) and -U0 may express the same change "correctly" from the point of view of a program like "git apply", but humans would see them as different patches, and "diffstat" that counts number of +/- would give different results. The patch IDs may also be different. The old commit was to pessimize the logic (because we do not need context just to count +/- lines for the purpose of diffstat) to match human expectations. They expect "'diffstat' must be counting 'diff -p' output" and we were counting "diff -p -U0" instead, resulting in different numbers.

With internally using -U0, the updated "pickaxe -G" is likely to get the same complaints: "'pickaxe -G<token>' found this commit, but in the 'git show' output, the token does not seem to be affected".

You'd respond to "try 'git show -U0' and now you'd see the <token>", but again that is probably breaking human expectations.

#27 lore raw

Re: [PATCH 21/25] pickaxe: use PCREv2 for -G and -S

In reply to Ævar Arnfjörð Bjarmason
Quoted 50 lines · Ævar Arnfjörð Bjarmason

void diffcore_pickaxe(struct diff_options *o)
{

diffcore-pickaxe.c +17 -24
 	const char *needle = o->pickaxe;
 	int opts = o->pickaxe_opts;
-	regex_t regex, *regexp = NULL;
-	kwset_t kws = NULL;
+	struct grep_opt opt;
+
+	if (opts & (DIFF_PICKAXE_REGEX | DIFF_PICKAXE_KIND_GS_MASK)) {
+		grep_init(&opt, the_repository, NULL);
+#ifdef USE_LIBPCRE2
+		grep_commit_pattern_type(GREP_PATTERN_TYPE_PCRE, &opt);
+#else
+		grep_commit_pattern_type(GREP_PATTERN_TYPE_ERE, &opt);
+#endif
 
-	if (opts & (DIFF_PICKAXE_REGEX | DIFF_PICKAXE_KIND_G)) {
-		int cflags = REG_EXTENDED | REG_NEWLINE;
 		if (o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE)
-			cflags |= REG_ICASE;
-		regcomp_or_die(&regex, needle, cflags);
-		regexp = &regex;
-	} else if (opts & DIFF_PICKAXE_KIND_S) {
-		if (o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE &&
-		    has_non_ascii(needle)) {
-			struct strbuf sb = STRBUF_INIT;
-			int cflags = REG_NEWLINE | REG_ICASE;
-
-			basic_regex_quote_buf(&sb, needle);
-			regcomp_or_die(&regex, sb.buf, cflags);
-			strbuf_release(&sb);
-			regexp = &regex;
-		} else {
-			kws = kwsalloc(o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE
-				       ? tolower_trans_tbl : NULL);
-			kwsincr(kws, needle, strlen(needle));
-			kwsprep(kws);
-		}
+			opt.ignore_case = 1;
+		if (opts & DIFF_PICKAXE_KIND_S &&
+		    !(opts & DIFF_PICKAXE_REGEX))
+			opt.fixed = 1;
+
+		append_grep_pattern(&opt, needle, "diffcore-pickaxe", 0, GREP_PATTERN);
+		compile_grep_patterns(&opt);
 	}
 
-	pickaxe(&diff_queued_diff, o, regexp, kws,
+	pickaxe(&diff_queued_diff, o, &opt,
 		(opts & DIFF_PICKAXE_KIND_G) ? diff_grep : has_changes);

diffcore-pickaxe.c static void pickaxe(struct diff_queue_struct *q, struct diff_options *o, · line 234

Quoted 10 lines
diffcore-pickaxe.c +3 -4
 
-	if (regexp)
-		regfree(regexp);
-	if (kws)
-		kwsfree(kws);
+	if (opts & ~DIFF_PICKAXE_KIND_OBJFIND)
+		free_grep_patterns(&opt);
+
 	return;
 }

There's a bug here where now different things are dispatched to either the -S or -G codepath wrongly, I've fixed it in my local version.

Anyway, it's interesting between this and the -U0 change that we have little/no coverage for some/all of this. I'm trying to address that in preceding patches in v2.

#15 lore raw

Re: [PATCH 14/25] pickaxe -S: remove redundant "sz" check in while-loop

In reply to Ævar Arnfjörð Bjarmason

Am 03.02.21 um 04:28 schrieb Ævar Arnfjörð Bjarmason:

Quoted 20 lines

If we walk to the end of the string we just won't match the rest of the regex. This removes an optimization for simplicity's sake. In subsequent commits we'll alter this code more, and not having to think about this condition makes it easier to read.

If we look at the context of what we're doing here the last thing we need to be worried about is one extra regex match. The real problem is that we keep matching after it's clear that the number of contains() for "A" and "B" is different. So we could be much smarter here.

---
 diffcore-pickaxe.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
diffcore-pickaxe.c +0 -0
index 208177bb40..8df76afb6e 100644

diffcore-pickaxe.c static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws) · line 85

Quoted 6 lines
diffcore-pickaxe.c +1 -2
@@ -82,12 +82,11 @@ static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws)
 		regmatch_t regmatch;
 		int flags = 0;
 
-		while (sz &&
-		       !regexec_buf(regexp, data, sz, 1, &regmatch, flags)) {
+		while (!regexec_buf(regexp, data, sz, 1, &regmatch, flags)) {

This will loop forever for regexes that match an empty string. An example would be /$/. Silly, perhaps, but still I understand this check less as an optimization and more as a correctness/robustness thing.

diffcore-pickaxe.c static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws) · line 92

Quoted 8 lines
diffcore-pickaxe.c +1 -1
 			flags |= REG_NOTBOL;
 			data += regmatch.rm_eo;
 			sz -= regmatch.rm_eo;
-			if (sz && regmatch.rm_so == regmatch.rm_eo) {
+			if (regmatch.rm_so == regmatch.rm_eo) {
 				data++;
 				sz--;
 			}

Before, if the match was an empty string and there was more data after it, then the code would consume a character anyway, in order to avoid matching the same empty string again. With the patch, that character is consumed even if there is no more data. This leaves 'data' pointing beyond the buffer and 'sz' rolls over to ULONG_MAX. Oops. :(

René

#16 lore raw

Re: [PATCH 14/25] pickaxe -S: remove redundant "sz" check in while-loop

Quoted 8 lines · René Scharfe

diffcore-pickaxe.c static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws) · line 85

Quoted 3 lines
diffcore-pickaxe.c +1 -2
-		while (sz &&
-		       !regexec_buf(regexp, data, sz, 1, &regmatch, flags)) {
+		while (!regexec_buf(regexp, data, sz, 1, &regmatch, flags)) {

This will loop forever for regexes that match an empty string. An example would be /$/. Silly, perhaps, but still I understand this check less as an optimization and more as a correctness/robustness thing.

Quoted 14 lines

diffcore-pickaxe.c static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws) · line 92

Quoted 8 lines
diffcore-pickaxe.c +1 -1
 			flags |= REG_NOTBOL;
 			data += regmatch.rm_eo;
 			sz -= regmatch.rm_eo;
-			if (sz && regmatch.rm_so == regmatch.rm_eo) {
+			if (regmatch.rm_so == regmatch.rm_eo) {
 				data++;
 				sz--;
 			}

Before, if the match was an empty string and there was more data after it, then the code would consume a character anyway, in order to avoid matching the same empty string again. With the patch, that character is consumed even if there is no more data. This leaves 'data' pointing beyond the buffer and 'sz' rolls over to ULONG_MAX. Oops. :(

While I do not care too much about NUL in the haystack, I do not mind [13/25] either. But this is bad.

This whole thing reminds me of f53c5de2 (pickaxe: fix segfault with '-S<...> --pickaxe-regex', 2017-03-18), by the way.

Thanks.

#28 lore raw

Re: [PATCH 21/25] pickaxe: use PCREv2 for -G and -S

In reply to Ævar Arnfjörð Bjarmason
Quoted 6 lines · Ævar Arnfjörð Bjarmason

There's a bug here where now different things are dispatched to either the -S or -G codepath wrongly, I've fixed it in my local version.

Anyway, it's interesting between this and the -U0 change that we have little/no coverage for some/all of this. I'm trying to address that in preceding patches in v2.

Back when I did the original -S<fixed>, we weren't as focused on good test coverage as we are now. We may have added a lot more by the time when we introduced -G and -S<regexp>, but your effort that resulted in noticing the lack of basic coverage and rectifying the situation is very much appreciated.

Thanks.

#37 lore raw

Re: [PATCH 24/25] xdiff-interface: support early exit in xdiff_outf()

In reply to Ævar Arnfjörð Bjarmason
Quoted 7 lines · Ævar Arnfjörð Bjarmason

Bridge the gap between the preceding "xdiff-interface: allow early return from xdiff_emit_{line,hunk}_fn" change and the public interface. This change was split off from the rest as it wasn't a purely mechanical addition of "return 0".

Here we want to be able to abort early, but do so in a way that doesn't skip the appropriate strbuf_reset() invocations.

Nice.

Quoted 2 lines

The use of -1 as a return value in the xdiff codebase for early return, as we'll see more of in subsequent commits.

This is a non-sentence without a verb.

I started reading the sentence and expected to see "The use of -1, as opposed to something else, is because of thess deep reasons" explained, but perhaps you forgot to conclude the sentence with such an explanation?

#29 lore raw

Re: [PATCH 21/25] pickaxe: use PCREv2 for -G and -S

In reply to Ævar Arnfjörð Bjarmason
Quoted 8 lines · Ævar Arnfjörð Bjarmason

Follow-up b65abcafc7a (grep: use PCRE v2 for optimized fixed-string search, 2019-07-01) and remove the use of kwset in the pickaxe code for fixed-string search, in favor of optimistically using PCRE v2.

This does mean that the semantics of the -G option subtly change, before it's an ERE, whereas now it'll be a PCRE if we're compiled with PCRE. Since PCRE is almost entirely a strict superset of ERE syntax I think this is OK.

Since Git is no longer a tool shared among 100 developers who are at least acquaintances, there will be people who are bothered by the differences, and such a change deserves a backward-compatibility warning in the release notes, at least.

Recently, I discovered that I've been building my personal copy of Git without LIBPCRE support at all for a long time. It is possible I've never built with LIBPCRE. This series may give me incentive to start using "git grep -P" ;-)

Thanks.

#17 lore raw

Re: [PATCH 14/25] pickaxe -S: remove redundant "sz" check in while-loop

In reply to Junio C Hamano
Quoted 25 lines · Junio C Hamano
Quoted 8 lines · René Scharfe

diffcore-pickaxe.c static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws) · line 85

Quoted 3 lines
diffcore-pickaxe.c +1 -2
-		while (sz &&
-		       !regexec_buf(regexp, data, sz, 1, &regmatch, flags)) {
+		while (!regexec_buf(regexp, data, sz, 1, &regmatch, flags)) {

This will loop forever for regexes that match an empty string. An example would be /$/. Silly, perhaps, but still I understand this check less as an optimization and more as a correctness/robustness thing.

Quoted 14 lines

diffcore-pickaxe.c static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws) · line 92

Quoted 8 lines
diffcore-pickaxe.c +1 -1
 			flags |= REG_NOTBOL;
 			data += regmatch.rm_eo;
 			sz -= regmatch.rm_eo;
-			if (sz && regmatch.rm_so == regmatch.rm_eo) {
+			if (regmatch.rm_so == regmatch.rm_eo) {
 				data++;
 				sz--;
 			}

Before, if the match was an empty string and there was more data after it, then the code would consume a character anyway, in order to avoid matching the same empty string again. With the patch, that character is consumed even if there is no more data. This leaves 'data' pointing beyond the buffer and 'sz' rolls over to ULONG_MAX. Oops. :(

Quoted 5 lines

While I do not care too much about NUL in the haystack, I do not mind [13/25] either. But this is bad.

This whole thing reminds me of f53c5de2 (pickaxe: fix segfault with '-S<...> --pickaxe-regex', 2017-03-18), by the way.

René: Well spotted, thanks, and Oops.

I've just sent a separate series with 01-10 of this one. I'm sitting on the diffcore-pickaxe patches for a while. I've got local fixes for a lot of issues in it, will fix this one too.

I've optimized the PCRE v2 codepath a lot more in my local version. Current results are:

    4209.1: git log -S'int main' <limit-rev>..                                0.38(0.36+0.01)   0.37(0.33+0.04) -2.6%
    4209.2: git log -S'æ' <limit-rev>..                                       0.51(0.47+0.04)   0.32(0.27+0.05) -37.3%
    4209.3: git log --pickaxe-regex -S'(int|void|null)' <limit-rev>..         0.72(0.68+0.03)   0.57(0.54+0.03) -20.8%
    4209.4: git log --pickaxe-regex -S'if *\([^ ]+ & ' <limit-rev>..          0.60(0.55+0.02)   0.39(0.34+0.05) -35.0%
    4209.5: git log --pickaxe-regex -S'[àáâãäåæñøùúûüýþ]' <limit-rev>..       0.43(0.40+0.03)   0.50(0.44+0.06) +16.3%
    4209.6: git log -G'(int|void|null)' <limit-rev>..                         0.64(0.55+0.09)   0.63(0.56+0.05) -1.6%
    4209.7: git log -G'if *\([^ ]+ & ' <limit-rev>..                          0.64(0.59+0.05)   0.63(0.56+0.06) -1.6%
    4209.8: git log -G'[àáâãäåæñøùúûüýþ]' <limit-rev>..                       0.63(0.54+0.08)   0.62(0.55+0.06) -1.6%
    4209.9: git log -i -S'int main' <limit-rev>..                             0.39(0.35+0.03)   0.38(0.35+0.02) -2.6%
    4209.10: git log -i -S'æ' <limit-rev>..                                   0.39(0.33+0.06)   0.32(0.28+0.04) -17.9%
    4209.11: git log -i --pickaxe-regex -S'(int|void|null)' <limit-rev>..     0.90(0.84+0.05)   0.58(0.53+0.04) -35.6%
    4209.12: git log -i --pickaxe-regex -S'if *\([^ ]+ & ' <limit-rev>..      0.71(0.64+0.06)   0.40(0.37+0.03) -43.7%
    4209.13: git log -i --pickaxe-regex -S'[àáâãäåæñøùúûüýþ]' <limit-rev>..   0.43(0.40+0.03)   0.50(0.46+0.04) +16.3%
    4209.14: git log -i -G'(int|void|null)' <limit-rev>..                     0.64(0.57+0.06)   0.62(0.56+0.05) -3.1%
    4209.15: git log -i -G'if *\([^ ]+ & ' <limit-rev>..                      0.65(0.59+0.06)   0.63(0.54+0.08) -3.1%
    4209.16: git log -i -G'[àáâãäåæñøùúûüýþ]' <limit-rev>..                   0.63(0.55+0.08)   0.62(0.56+0.05) -1.6%

The main optimization was just moving the compilation of the pattern up the stack into the diff_options struct, the current version in this thread re-compiles it every time.

#40 lore raw

v2 cover pickaxe: test and refactoring for follow-up changes

In reply to Ævar Arnfjörð Bjarmason

This is a smaller v2 of the series to remove the kwset backend and make pickaxe use PCRE v2[1].

That's not being done here yet. These are mostly small refactoring/test fixes. The most significant work is a new xdiff interface at the end of the series.

It's based on next where some preparatory work already landed[2].

The endless loop bug in v1 pointed out by René Scharfe[3] is gone. We should still have a test for that, I didn't have time to do more on that, and figured this was already getting large enough.

I'll do some more improvements of test coverage in a follow-up series. I'm aware of various blind spots in pickaxe test coverage, but none of it should hide a bug in this refactoring from us.

It's things like how we deal with REG_NEWLINE, "^" matches etc., but all the matching logic for that stays the same in this series.

1. https://lore.kernel.org/git/[email protected]/
2. https://lore.kernel.org/git/[email protected]/
3. https://lore.kernel.org/git/[email protected]/

Ævar Arnfjörð Bjarmason (22):
grep/pcre2 tests: reword comments referring to kwset
test-lib-functions: document and test test_commit --no-tag
test-lib-functions: reword "test_commit --append" docs test-lib functions: add --printf option to test_commit pickaxe tests: refactor to use test_commit --append --printf pickaxe tests: add test for diffgrep_consume() internals pickaxe tests: add test for "log -S" not being a regex pickaxe tests: test for -G, -S and --find-object incompatibility
pickaxe: die when -G and --pickaxe-regex are combined
pickaxe: die when --find-object and --pickaxe-all are combined diff.h: move pickaxe fields together again pickaxe/style: consolidate declarations and assignments
perf: add performance test for pickaxe
pickaxe: refactor function selection in diffcore-pickaxe()
pickaxe: assert that we must have a needle under -G or -S pickaxe -S: support content with NULs under --pickaxe-regex
pickaxe: rename variables in has_changes() for brevity pickaxe -S: slightly optimize contains()
xdiff-interface: allow early return from xdiff_emit_{line,hunk}_fn
xdiff-interface: support early exit in xdiff_outf() pickaxe -G: terminate early on matching lines
pickaxe -G: don't special-case create/delete

 combine-diff.c                 |   9 ++-
 diff.c                         |  45 +++++++++------
 diff.h                         |   7 ++-
 diffcore-pickaxe.c             |  99 ++++++++++++++++++--------------
 range-diff.c                   |   8 ++-
 t/perf/p4209-pickaxe.sh        |  70 +++++++++++++++++++++++
 t/t0000-basic.sh               |  19 +++++++
 t/t1307-config-blob.sh         |   4 +-
 t/t2030-unresolve-info.sh      |   3 +-
 t/t4006-diff-mode.sh           |   6 +-
 t/t4030-diff-textconv.sh       |   8 +--
 t/t4209-log-pickaxe.sh         | 100 +++++++++++++++++++++++++++------
 t/t5520-pull.sh                |  10 +---
 t/t7816-grep-binary-pattern.sh |   4 +-
 t/test-lib-functions.sh        |  23 ++++++--
 xdiff-interface.c              |  26 ++++++---
 xdiff-interface.h              |  36 +++++++++---
 17 files changed, 349 insertions(+), 128 deletions(-)
 create mode 100755 t/perf/p4209-pickaxe.sh
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#66 lore raw

PATCH v2 1/22 grep/pcre2 tests: reword comments referring to kwset

In reply to Ævar Arnfjörð Bjarmason

The kwset optimization has not been used by grep since 48de2a768cf (grep: remove the kwset optimization, 2019-07-01).

---
 t/t7816-grep-binary-pattern.sh | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
t/t7816-grep-binary-pattern.sh +2 -2
index 60bab291e49..9d67a5fc4cf 100755
@@ -59,7 +59,7 @@ test_expect_success 'setup' "
 	git commit -m.
 "
 
-# Simple fixed-string matching that can use kwset (no -i && non-ASCII)
+# Simple fixed-string matching
 nul_match P P P '-F' 'yQf'
 nul_match P P P '-F' 'yQx'
 nul_match P P P '-Fi' 'YQf'
@@ -78,7 +78,7 @@ nul_match P P P '-Fi' '[Y]QF'
 nul_match P P P '-F' 'æQ[ð]'
 nul_match P P P '-F' '[æ]Qð'
 
-# The -F kwset codepath can't handle -i && non-ASCII...
+# Matching pattern and subject case with -i
 nul_match P 1 1 '-i' '[æ]Qð'
 
 # ...PCRE v2 only matches non-ASCII with -i casefolding under UTF-8
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#67 lore raw

PATCH v2 2/22 test-lib-functions: document and test test_commit --no-tag

In reply to Ævar Arnfjörð Bjarmason

In 76b8b8d05c (test-lib functions: document arguments to test_commit, 2021-01-12) I added missing documentation to test_commit, but in less than a month later in 3803a3a099 (t: add --no-tag option to test_commit, 2021-02-09) we got another undocumented option.

Let's fix that, and while we're at it expand on my e7884b353b (test-lib-functions: assert correct parameter count, 2021-02-12) and assert that you shouldn't be passing the optional "<tag>" argument under "test_commit --no-tag".

---
 t/t0000-basic.sh        | 19 +++++++++++++++++++
 t/test-lib-functions.sh |  8 +++++++-
 2 files changed, 26 insertions(+), 1 deletion(-)
t/t0000-basic.sh +19 -0
index a6e570d674a..6ee98fd0695 100755
@@ -1324,4 +1324,23 @@ test_expect_success 'test_must_fail rejects a non-git command with env' '
 	grep -F "test_must_fail: only '"'"'git'"'"' is allowed" err
 '
 
+test_expect_success 'test_commit --no-tag fails with a <tag> argument' '
+	run_sub_test_lib_test_err \
+		test_commit-bug "test_commit-bug with --no-tag" <<-\EOF &&
+	test_expect_success "setup #1" "test_commit message1 file1 contents1"
+	test_expect_success "setup #2" "test_commit message2 file2 contents2 tag2"
+	test_expect_success "setup #3" "test_commit --no-tag message3 file3 contents3"
+	test_expect_success "setup #4" "test_commit --no-tag message4 file4 contents4 tag4"
+	test_done
+	EOF
+	check_sub_test_lib_test_err test_commit-bug \
+		<<-\EOF_OUT 3<<-\EOF_ERR
+	ok 1 - setup #1
+	ok 2 - setup #2
+	ok 3 - setup #3
+	EOF_OUT
+	error: bug in the test script: expect no <tag> parameter with --no-tag
+	EOF_ERR
+'
+
 test_done
t/test-lib-functions.sh +7 -1
index 6348e8d7339..1eb75d0d733 100644
@@ -178,6 +178,9 @@ debug () {
 #	Invoke "git commit" with --signoff
 #   --author <author>
 #	Invoke "git commit" with --author <author>
+#   --no-tag
+#	Do not tag the resulting commit, if supplied giving the
+#	optional "<tag>" argument is an error.
 #
 # This will commit a file with the given contents and the given commit
 # message, and tag the resulting commit with the given tag name.
@@ -242,7 +245,10 @@ test_commit () {
 	git ${indir:+ -C "$indir"} commit \
 	    ${author:+ --author "$author"} \
 	    $signoff -m "$1" &&
-	if test -z "$no_tag"
+	if test -n "$no_tag" -a $# -eq 4
+	then
+		BUG "expect no <tag> parameter with --no-tag"
+	elif test -z "$no_tag"
 	then
 		git ${indir:+ -C "$indir"} tag "${4:-$1}"
 	fi
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#69 lore raw

PATCH v2 3/22 test-lib-functions: reword "test_commit --append" docs

In reply to Ævar Arnfjörð Bjarmason

Reword the documentation for "test_commit --append" added in my 3373518cc8 (test-lib functions: add an --append option to test_commit, 2021-01-12).

A follow-up commit will make the "echo" part of this configurable, and in any case saying "echo >>" rather than ">>" was redundant.

---
 t/test-lib-functions.sh | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
t/test-lib-functions.sh +1 -2
index 1eb75d0d733..5af92347123 100644
@@ -172,8 +172,7 @@ debug () {
 #   --notick
 #	Do not call test_tick before making a commit
 #   --append
-#	Use "echo >>" instead of "echo >" when writing "<contents>" to
-#	"<file>"
+#	Use ">>" instead of ">" when writing "<contents>" to "<file>"
 #   --signoff
 #	Invoke "git commit" with --signoff
 #   --author <author>
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#70 lore raw

PATCH v2 4/22 test-lib functions: add --printf option to test_commit

In reply to Ævar Arnfjörð Bjarmason

Add a --printf option to test_commit to allow writing to the file with "printf" instead of "echo".

This is useful for writing "\n", "\0" etc., in particular in combination with the --append option added in 3373518cc8 (test-lib
functions: add an --append option to test_commit, 2021-01-12).

I'm converting a few tests to use the new option rather than a manual printf/add/commit combination to demonstrate its usefulness. While I'm at it use "test_create_repo" where appropriate, and give the first/second commit a meaningful/more conventional log message in cases where no test cared about that message.

---
 t/t1307-config-blob.sh    |  4 +---
 t/t2030-unresolve-info.sh |  3 +--
 t/t4006-diff-mode.sh      |  6 ++----
 t/t4030-diff-textconv.sh  |  8 ++------
 t/t5520-pull.sh           | 10 ++--------
 t/test-lib-functions.sh   | 12 ++++++++++--
 6 files changed, 18 insertions(+), 25 deletions(-)
t/t1307-config-blob.sh +1 -3
index 002e6d3388e..930dce06f0f 100755
@@ -65,9 +65,7 @@ test_expect_success 'parse errors in blobs are properly attributed' '
 '
 
 test_expect_success 'can parse blob ending with CR' '
-	printf "[some]key = value\\r" >config &&
-	git add config &&
-	git commit -m CR &&
+	test_commit --printf CR config "[some]key = value\\r" &&
 	echo value >expect &&
 	git config --blob=HEAD:config some.key >actual &&
 	test_cmp expect actual
t/t2030-unresolve-info.sh +1 -2
index be6c84c52a2..bad28d29de5 100755
@@ -179,8 +179,7 @@ test_expect_success 'rerere and rerere forget (subdirectory)' '
 
 test_expect_success 'rerere forget (binary)' '
 	git checkout -f side &&
-	printf "a\0c" >binary &&
-	git commit -a -m binary &&
+	test_commit binary binary "a\0c" &&
 	test_must_fail git merge second &&
 	git rerere forget binary
 '
t/t4006-diff-mode.sh +2 -4
index 03489aff14e..8fd60424142 100755
@@ -26,10 +26,8 @@ test_expect_success 'chmod' '
 '
 
 test_expect_success 'prepare binary file' '
-	git commit -m rezrov &&
-	printf "\00\01\02\03\04\05\06" >binbin &&
-	git add binbin &&
-	git commit -m binbin
+	git commit -m one &&
+	test_commit --printf two binbin "\00\01\02\03\04\05\06"
 '
 
 test_expect_success '--stat output after text chmod' '
t/t4030-diff-textconv.sh +2 -6
index 4cb9f0e523d..6e1e7e38ff4 100755
@@ -26,12 +26,8 @@ EOF
 chmod +x hexdump
 
 test_expect_success 'setup binary file with history' '
-	printf "\\0\\n" >file &&
-	git add file &&
-	git commit -m one &&
-	printf "\\01\\n" >>file &&
-	git add file &&
-	git commit -m two
+	test_commit --printf one file "\\0\\n" &&
+	test_commit --printf --append two file "\\01\\n"
 '
 
 test_expect_success 'file is considered binary by porcelain' '
t/t5520-pull.sh +2 -8
index a09411327f9..e2c0c510222 100755
@@ -746,14 +746,8 @@ test_expect_success 'pull --rebase fails on corrupt HEAD' '
 '
 
 test_expect_success 'setup for detecting upstreamed changes' '
-	mkdir src &&
-	(
-		cd src &&
-		git init &&
-		printf "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" > stuff &&
-		git add stuff &&
-		git commit -m "Initial revision"
-	) &&
+	test_create_repo src &&
+	test_commit -C src --printf one stuff "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" &&
 	git clone src dst &&
 	(
 		cd src &&
t/test-lib-functions.sh +10 -2
index 5af92347123..5e49dd6b864 100644
@@ -173,6 +173,10 @@ debug () {
 #	Do not call test_tick before making a commit
 #   --append
 #	Use ">>" instead of ">" when writing "<contents>" to "<file>"
+#   --printf
+#       Use "printf" instead of "echo" when writing "<contents>" to
+#       "<file>". You will need to provide your own trailing "\n". You
+#       can only supply the FORMAT for the printf(1), not its ARGUMENT(s).
 #   --signoff
 #	Invoke "git commit" with --signoff
 #   --author <author>
@@ -188,6 +192,7 @@ debug () {
 
 test_commit () {
 	notick= &&
+	echo=echo &&
 	append= &&
 	author= &&
 	signoff= &&
@@ -199,6 +204,9 @@ test_commit () {
 		--notick)
 			notick=yes
 			;;
+		--printf)
+			echo=printf
+			;;
 		--append)
 			append=yes
 			;;
@@ -232,9 +240,9 @@ test_commit () {
 	file=${2:-"$1.t"} &&
 	if test -n "$append"
 	then
-		echo "${3-$1}" >>"$indir$file"
+		$echo "${3-$1}" >>"$indir$file"
 	else
-		echo "${3-$1}" >"$indir$file"
+		$echo "${3-$1}" >"$indir$file"
 	fi &&
 	git ${indir:+ -C "$indir"} add "$file" &&
 	if test -z "$notick"
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#73 lore raw

PATCH v2 5/22 pickaxe tests: refactor to use test_commit --append --printf

In reply to Ævar Arnfjörð Bjarmason

Refactor existing tests added in e0e7cb8080c (log -G: ignore binary files, 2018-12-14) to use the --append option I added in 3373518cc8b (test-lib functions: add an --append option to test_commit, 2021-01-12) and the --printf option added in a preceding commit.

See also f5d79bf7dd6 (tests: refactor a few tests to use "test_commit --append", 2021-01-12) for prior similar refactoring.

---
 t/t4209-log-pickaxe.sh | 30 ++++++++++++++----------------
 1 file changed, 14 insertions(+), 16 deletions(-)
t/t4209-log-pickaxe.sh +14 -16
index 5d06f5f45ea..298b25265f4 100755
@@ -107,37 +107,35 @@ test_expect_success 'log -S --no-textconv (missing textconv tool)' '
 '
 
 test_expect_success 'setup log -[GS] binary & --text' '
-	git checkout --orphan GS-binary-and-text &&
-	git read-tree --empty &&
-	printf "a\na\0a\n" >data.bin &&
-	git add data.bin &&
-	git commit -m "create binary file" data.bin &&
-	printf "a\na\0a\n" >>data.bin &&
-	git commit -m "modify binary file" data.bin &&
-	git rm data.bin &&
-	git commit -m "delete binary file" data.bin &&
-	git log >full-log
+	test_create_repo GS-bin-txt &&
+	test_commit -C GS-bin-txt --append --printf A data.bin "a\na\0a\n" &&
+	test_commit -C GS-bin-txt --append --printf B data.bin "a\na\0a\n" &&
+	test_commit -C GS-bin-txt C data.bin "" &&
+	git -C GS-bin-txt log >full-log
 '
 
 test_expect_success 'log -G ignores binary files' '
-	git log -Ga >log &&
+	git -C GS-bin-txt log -Ga >log &&
 	test_must_be_empty log
 '
 
 test_expect_success 'log -G looks into binary files with -a' '
-	git log -a -Ga >log &&
+	git -C GS-bin-txt log -a -Ga >log &&
 	test_cmp log full-log
 '
 
 test_expect_success 'log -G looks into binary files with textconv filter' '
-	test_when_finished "rm .gitattributes" &&
-	echo "* diff=bin" >.gitattributes &&
-	git -c diff.bin.textconv=cat log -Ga >log &&
+	test_when_finished "rm GS-bin-txt/.gitattributes" &&
+	(
+		cd GS-bin-txt &&
+		echo "* diff=bin" >.gitattributes &&
+		git -c diff.bin.textconv=cat log -Ga >../log
+	) &&
 	test_cmp log full-log
 '
 
 test_expect_success 'log -S looks into binary files' '
-	git log -Sa >log &&
+	git -C GS-bin-txt log -Sa >log &&
 	test_cmp log full-log
 '
 
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#75 lore raw

PATCH v2 6/22 pickaxe tests: add test for diffgrep_consume() internals

In reply to Ævar Arnfjörð Bjarmason

In diffgrep_consume() we generate a diff, and then advance past the "+" or "-" at the start of the line for matching. This has been done ever since the code was added in f506b8e8b5f (git log/diff: add -G<regexp> that greps in the patch text, 2010-08-23).

If we match "line" instead of "line + 1" no tests fail, i.e. we've got zero coverage for whether any of our searches match the beginning of the line or not. Let's add a test for this.

---
 t/t4209-log-pickaxe.sh | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
t/t4209-log-pickaxe.sh +15 -0
index 298b25265f4..e5fa84816a5 100755
@@ -106,6 +106,21 @@ test_expect_success 'log -S --no-textconv (missing textconv tool)' '
 	rm .gitattributes
 '
 
+test_expect_success 'setup log -[GS] plain' '
+	test_create_repo GS-plain &&
+	test_commit -C GS-plain --append A data.txt "a" &&
+	test_commit -C GS-plain --append B data.txt "a a" &&
+	test_commit -C GS-plain C data.txt "" &&
+	git -C GS-plain log >full-log
+'
+
+test_expect_success 'log -G trims diff new/old [-+]' '
+	git -C GS-plain log -G"[+-]a" >log &&
+	test_must_be_empty log &&
+	git -C GS-plain log -G"^a" >log &&
+	test_cmp log full-log
+'
+
 test_expect_success 'setup log -[GS] binary & --text' '
 	test_create_repo GS-bin-txt &&
 	test_commit -C GS-bin-txt --append --printf A data.bin "a\na\0a\n" &&
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#77 lore raw

PATCH v2 8/22 pickaxe tests: test for -G, -S and --find-object incompatibility

In reply to Ævar Arnfjörð Bjarmason

Add a test for the options sanity check added in 5e505257f2 (diff:
properly error out when combining multiple pickaxe options, 2018-01-04).

---
 t/t4209-log-pickaxe.sh | 11 +++++++++++
 1 file changed, 11 insertions(+)
t/t4209-log-pickaxe.sh +11 -0
index c6b4751d5b6..5ad4fad964c 100755
@@ -55,6 +55,17 @@ test_expect_success setup '
 	git rev-parse --verify HEAD >expect_second
 '
 
+test_expect_success 'usage' '
+	test_expect_code 128 git log -Gregex -Sstring 2>err &&
+	test_i18ngrep "mutually exclusive" err &&
+
+	test_expect_code 128 git log -Gregex --find-object=HEAD 2>err &&
+	test_i18ngrep "mutually exclusive" err &&
+
+	test_expect_code 128 git log -Gstring --find-object=HEAD 2>err &&
+	test_i18ngrep "mutually exclusive" err
+'
+
 test_log	expect_initial	--grep initial
 test_log	expect_nomatch	--grep InItial
 test_log_icase	expect_initial	--grep InItial
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#76 lore raw

PATCH v2 7/22 pickaxe tests: add test for "log -S" not being a regex

In reply to Ævar Arnfjörð Bjarmason

No test in our test suite checked for "log -S<pat>" being a fixed string, as opposed to "log -S<pat> --pickaxe-regex". Let's test for it.

---
 t/t4209-log-pickaxe.sh | 30 +++++++++++++++++++++++++++---
 1 file changed, 27 insertions(+), 3 deletions(-)
t/t4209-log-pickaxe.sh +27 -3
index e5fa84816a5..c6b4751d5b6 100755
@@ -106,11 +106,18 @@ test_expect_success 'log -S --no-textconv (missing textconv tool)' '
 	rm .gitattributes
 '
 
-test_expect_success 'setup log -[GS] plain' '
+test_expect_success 'setup log -[GS] plain & regex' '
 	test_create_repo GS-plain &&
 	test_commit -C GS-plain --append A data.txt "a" &&
 	test_commit -C GS-plain --append B data.txt "a a" &&
-	test_commit -C GS-plain C data.txt "" &&
+	test_commit -C GS-plain --append C data.txt "b" &&
+	test_commit -C GS-plain --append D data.txt "[b]" &&
+	test_commit -C GS-plain E data.txt "" &&
+
+	# We also include E, the deletion commit
+	git -C GS-plain log --grep="[ABE]" >A-to-B-then-E-log &&
+	git -C GS-plain log --grep="[CDE]" >C-to-D-then-E-log &&
+	git -C GS-plain log --grep="[DE]" >D-then-E-log &&
 	git -C GS-plain log >full-log
 '
 
@@ -118,7 +125,24 @@ test_expect_success 'log -G trims diff new/old [-+]' '
 	git -C GS-plain log -G"[+-]a" >log &&
 	test_must_be_empty log &&
 	git -C GS-plain log -G"^a" >log &&
-	test_cmp log full-log
+	test_cmp log A-to-B-then-E-log
+'
+
+test_expect_success 'log -S<pat> is not a regex, but -S<pat> --pickaxe-regex is' '
+	git -C GS-plain log -S"a" >log &&
+	test_cmp log A-to-B-then-E-log &&
+
+	git -C GS-plain log -S"[a]" >log &&
+	test_must_be_empty log &&
+
+	git -C GS-plain log -S"[a]" --pickaxe-regex >log &&
+	test_cmp log A-to-B-then-E-log &&
+
+	git -C GS-plain log -S"[b]" >log &&
+	test_cmp log D-then-E-log &&
+
+	git -C GS-plain log -S"[b]" --pickaxe-regex >log &&
+	test_cmp log C-to-D-then-E-log
 '
 
 test_expect_success 'setup log -[GS] binary & --text' '
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#79 lore raw

PATCH v2 9/22 pickaxe: die when -G and --pickaxe-regex are combined

In reply to Ævar Arnfjörð Bjarmason

When the -G and --pickaxe-regex options are combined we simply ignore the --pickaxe-regex option. Let's die instead as suggested by our documentation, since -G is always a regex.

When --pickaxe-regex was added in d01d8c6782 (Support for pickaxe matching regular expressions, 2006-03-29) only the -S option existed. Then when -G was added in f506b8e8b5 (git log/diff: add -G<regexp> that greps in the patch text, 2010-08-23) neither the documentation for --pickaxe-regex was updater accordingly, nor was something like this assertion added.

Since 5bc3f0b567 (diffcore-pickaxe doc: document -S and -G properly, 2013-05-31) we've claimed that --pickaxe-regex should only be used with -S, but have silently toileted combining it with -G, let's die instead.

---
 diff.c                 | 3 +++
 diff.h                 | 2 ++
 t/t4209-log-pickaxe.sh | 3 +++
 3 files changed, 8 insertions(+)
diff.c +3 -0
index bf2cbf15e77..96da1fe6167 100644
@@ -4630,6 +4630,9 @@ void diff_setup_done(struct diff_options *options)
 	if (HAS_MULTI_BITS(options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK))
 		die(_("-G, -S and --find-object are mutually exclusive"));
 
+	if (HAS_MULTI_BITS(options->pickaxe_opts & DIFF_PICKAXE_KINDS_G_REGEX_MASK))
+		die(_("-G and --pickaxe-regex are mutually exclusive, use --pickaxe-regex with -S"));
+
 	/*
 	 * Most of the time we can say "there are changes"
 	 * only by checking if there are changed paths, but
diff.h +2 -0
index 527fb56d851..668d496d7a5 100644
@@ -535,6 +535,8 @@ int git_config_rename(const char *var, const char *value);
 #define DIFF_PICKAXE_KINDS_MASK (DIFF_PICKAXE_KIND_S | \
 				 DIFF_PICKAXE_KIND_G | \
 				 DIFF_PICKAXE_KIND_OBJFIND)
+#define DIFF_PICKAXE_KINDS_G_REGEX_MASK (DIFF_PICKAXE_KIND_G | \
+					 DIFF_PICKAXE_REGEX)
 
 #define DIFF_PICKAXE_IGNORE_CASE	32
 
t/t4209-log-pickaxe.sh +3 -0
index 5ad4fad964c..46dc5f14b3b 100755
@@ -59,6 +59,9 @@ test_expect_success 'usage' '
 	test_expect_code 128 git log -Gregex -Sstring 2>err &&
 	test_i18ngrep "mutually exclusive" err &&
 
+	test_expect_code 128 git log -Gregex --pickaxe-regex 2>err &&
+	test_i18ngrep "mutually exclusive" err &&
+
 	test_expect_code 128 git log -Gregex --find-object=HEAD 2>err &&
 	test_i18ngrep "mutually exclusive" err &&
 
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#82 lore raw

PATCH v2 11/22 diff.h: move pickaxe fields together again

In reply to Ævar Arnfjörð Bjarmason

Move the pickaxe and pickaxe_opts fields next to each other again. In a past life they'd been on adjacent lines, but when they got moved from a global variable to the diff_options struct in 6b5ee137e5 (Diff clean-up., 2005-09-21) they got split apart.

That split made sense at the time, the "char*" and "int" (flags) options were being grouped, but we've long since abandoned that pattern in the diff_options struct, and now it makes more sense to group these together again.

---
 diff.h | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
diff.h +1 -2
index 8f0dc7ef43b..5f33e7e4f76 100644
@@ -244,6 +244,7 @@ struct diff_options {
 	 * postimage of the diff_queue.
 	 */
 	const char *pickaxe;
+	unsigned pickaxe_opts;
 
 	/* -I<regex> */
 	regex_t **ignore_regex;
@@ -283,8 +284,6 @@ struct diff_options {
 	/* The output format used when `diff_flush()` is run. */
 	int output_format;
 
-	unsigned pickaxe_opts;
-
 	/* Affects the way detection logic for complete rewrites, renames and
 	 * copies.
 	 */
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#81 lore raw

PATCH v2 10/22 pickaxe: die when --find-object and --pickaxe-all are combined

In reply to Ævar Arnfjörð Bjarmason

Neither the --pickaxe-all documentation nor --find-object's has ever suggested that you can combine the two. See f506b8e8b5 (git log/diff:
add -G<regexp> that greps in the patch text, 2010-08-23) and 15af58c1ad (diffcore: add a pickaxe option to find a specific blob, 2018-01-04).

But we've silently tolerated it, which makes the logic in diffcore_pickaxe() harder to reason about. Let's assert that we won't have the two combined.

---
 diff.c                 | 3 +++
 diff.h                 | 2 ++
 t/t4209-log-pickaxe.sh | 3 +++
 3 files changed, 8 insertions(+)
diff.c +3 -0
index 96da1fe6167..63c49ecaef1 100644
@@ -4633,6 +4633,9 @@ void diff_setup_done(struct diff_options *options)
 	if (HAS_MULTI_BITS(options->pickaxe_opts & DIFF_PICKAXE_KINDS_G_REGEX_MASK))
 		die(_("-G and --pickaxe-regex are mutually exclusive, use --pickaxe-regex with -S"));
 
+	if (HAS_MULTI_BITS(options->pickaxe_opts & DIFF_PICKAXE_KINDS_ALL_OBJFIND_MASK))
+		die(_("---pickaxe-all and --find-object are mutually exclusive, use --pickaxe-all with -G and -S"));
+
 	/*
 	 * Most of the time we can say "there are changes"
 	 * only by checking if there are changed paths, but
diff.h +2 -0
index 668d496d7a5..8f0dc7ef43b 100644
@@ -537,6 +537,8 @@ int git_config_rename(const char *var, const char *value);
 				 DIFF_PICKAXE_KIND_OBJFIND)
 #define DIFF_PICKAXE_KINDS_G_REGEX_MASK (DIFF_PICKAXE_KIND_G | \
 					 DIFF_PICKAXE_REGEX)
+#define DIFF_PICKAXE_KINDS_ALL_OBJFIND_MASK (DIFF_PICKAXE_ALL | \
+					     DIFF_PICKAXE_KIND_OBJFIND)
 
 #define DIFF_PICKAXE_IGNORE_CASE	32
 
t/t4209-log-pickaxe.sh +3 -0
index 46dc5f14b3b..bcaca7e882c 100755
@@ -66,6 +66,9 @@ test_expect_success 'usage' '
 	test_i18ngrep "mutually exclusive" err &&
 
 	test_expect_code 128 git log -Gstring --find-object=HEAD 2>err &&
+	test_i18ngrep "mutually exclusive" err &&
+
+	test_expect_code 128 git log --pickaxe-all --find-object=HEAD 2>err &&
 	test_i18ngrep "mutually exclusive" err
 '
 
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#85 lore raw

PATCH v2 14/22 pickaxe: refactor function selection in diffcore-pickaxe()

In reply to Ævar Arnfjörð Bjarmason

It's hard to read this codepath at a glance and reason about exactly what combination of -G and -S will compile either regexes or kwset, and whether we'll then dispatch to "diff_grep" or "has_changes".

Then in the "--find-object" case we aren't using the callback function, but were previously passing down "has_changes".

Refactor this code to exhaustively check "opts", it's now more obvious what callback function (or none) we want under what mode.

---
 diffcore-pickaxe.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
diffcore-pickaxe.c +15 -2
index a278b9b71d9..cff46f9f8f7 100644
@@ -228,6 +228,7 @@ void diffcore_pickaxe(struct diff_options *o)
 	int opts = o->pickaxe_opts;
 	regex_t regex, *regexp = NULL;
 	kwset_t kws = NULL;
+	pickaxe_fn fn;
 
 	if (opts & (DIFF_PICKAXE_REGEX | DIFF_PICKAXE_KIND_G)) {
 		int cflags = REG_EXTENDED | REG_NEWLINE;
@@ -235,6 +236,14 @@ void diffcore_pickaxe(struct diff_options *o)
 			cflags |= REG_ICASE;
 		regcomp_or_die(&regex, needle, cflags);
 		regexp = &regex;
+
+		/* diff.c errors on -G and --pickaxe-regex for us */
+		if (opts & DIFF_PICKAXE_KIND_G)
+			fn = diff_grep;
+		else if (opts & DIFF_PICKAXE_REGEX)
+			fn = has_changes;
+		else
+			BUG("unreachable");
 	} else if (opts & DIFF_PICKAXE_KIND_S) {
 		if (o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE &&
 		    has_non_ascii(needle)) {
@@ -251,10 +260,14 @@ void diffcore_pickaxe(struct diff_options *o)
 			kwsincr(kws, needle, strlen(needle));
 			kwsprep(kws);
 		}
+		fn = has_changes;
+	} else if (opts & DIFF_PICKAXE_KIND_OBJFIND) {
+		fn = NULL;
+	} else {
+		BUG("unknown pickaxe_opts flag");
 	}
 
-	pickaxe(&diff_queued_diff, o, regexp, kws,
-		(opts & DIFF_PICKAXE_KIND_G) ? diff_grep : has_changes);
+	pickaxe(&diff_queued_diff, o, regexp, kws, fn);
 
 	if (regexp)
 		regfree(regexp);
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#83 lore raw

PATCH v2 12/22 pickaxe/style: consolidate declarations and assignments

In reply to Ævar Arnfjörð Bjarmason

Refactor contains() to do its assignments at the same time that it does its declarations.

This code could have been refactored in ef90ab66e8e (pickaxe: use textconv for -S counting, 2012-10-28) when a function call between the declarations and assignments was removed.

---
 diffcore-pickaxe.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
diffcore-pickaxe.c +3 -7
index a9c6d60df22..a278b9b71d9 100644
@@ -70,13 +70,9 @@ static int diff_grep(mmfile_t *one, mmfile_t *two,
 
 static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws)
 {
-	unsigned int cnt;
-	unsigned long sz;
-	const char *data;
-
-	sz = mf->size;
-	data = mf->ptr;
-	cnt = 0;
+	unsigned int cnt = 0;
+	unsigned long sz = mf->size;
+	const char *data = mf->ptr;
 
 	if (regexp) {
 		regmatch_t regmatch;
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#87 lore raw

PATCH v2 15/22 pickaxe: assert that we must have a needle under -G or -S

In reply to Ævar Arnfjörð Bjarmason

Assert early in diffcore_pickaxe() that we've got a needle to work with under -G and -S.

This code is redundant to the check -G and -S get from parse-options.c's get_arg(), which I'm adding a test for.

This check dates back to e1b161161d (diffcore-pickaxe: fix infinite loop on zero-length needle, 2007-01-25) when "git log -S" could send this code into an infinite loop.

It was then later refactored in 8fa4b09fb1 (pickaxe: hoist empty needle check, 2012-10-28) into its current form, but it seemingly wasn't noticed that in the meantime a move to the parse-options.c API in dea007fb4c (diff: parse separate options like -S foo, 2010-08-05) had made it redundant.

Let's retain some of the paranoia here with a BUG(), but there's no need to be checking this in the pickaxe_match() inner loop.

---
 diffcore-pickaxe.c     | 5 ++---
 t/t4209-log-pickaxe.sh | 6 ++++++
 2 files changed, 8 insertions(+), 3 deletions(-)
diffcore-pickaxe.c +2 -3
index cff46f9f8f7..dd1b5c72332 100644
@@ -132,9 +132,6 @@ static int pickaxe_match(struct diff_filepair *p, struct diff_options *o,
 			 oidset_contains(o->objfind, &p->two->oid));
 	}
 
-	if (!o->pickaxe[0])
-		return 0;
-
 	if (o->flags.allow_textconv) {
 		textconv_one = get_textconv(o->repo, p->one);
 		textconv_two = get_textconv(o->repo, p->two);
@@ -230,6 +227,8 @@ void diffcore_pickaxe(struct diff_options *o)
 	kwset_t kws = NULL;
 	pickaxe_fn fn;
 
+	if (opts & ~DIFF_PICKAXE_KIND_OBJFIND && !needle)
+		BUG("should have needle under -G or -S");
 	if (opts & (DIFF_PICKAXE_REGEX | DIFF_PICKAXE_KIND_G)) {
 		int cflags = REG_EXTENDED | REG_NEWLINE;
 		if (o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE)
t/t4209-log-pickaxe.sh +6 -0
index bcaca7e882c..4b65b89e7a5 100755
@@ -56,6 +56,12 @@ test_expect_success setup '
 '
 
 test_expect_success 'usage' '
+	test_expect_code 129 git log -S 2>err &&
+	test_i18ngrep "switch.*requires a value" err &&
+
+	test_expect_code 129 git log -G 2>err &&
+	test_i18ngrep "switch.*requires a value" err &&
+
 	test_expect_code 128 git log -Gregex -Sstring 2>err &&
 	test_i18ngrep "mutually exclusive" err &&
 
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#91 lore raw

PATCH v2 17/22 pickaxe: rename variables in has_changes() for brevity

In reply to Ævar Arnfjörð Bjarmason

Rename the {one,two}_contains variables to c{1,2}. This will make a follow-up change easier to read.

---
 diffcore-pickaxe.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
diffcore-pickaxe.c +3 -3
index 0bf50a2f595..66e34d254f1 100644
@@ -108,9 +108,9 @@ static int has_changes(mmfile_t *one, mmfile_t *two,
 		       struct diff_options *o,
 		       regex_t *regexp, kwset_t kws)
 {
-	unsigned int one_contains = one ? contains(one, regexp, kws) : 0;
-	unsigned int two_contains = two ? contains(two, regexp, kws) : 0;
-	return one_contains != two_contains;
+	unsigned int c1 = one ? contains(one, regexp, kws) : 0;
+	unsigned int c2 = two ? contains(two, regexp, kws) : 0;
+	return c1 != c2;
 }
 
 static int pickaxe_match(struct diff_filepair *p, struct diff_options *o,
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#84 lore raw

PATCH v2 13/22 perf: add performance test for pickaxe

In reply to Ævar Arnfjörð Bjarmason

Add a test for the -G and -S pickaxe options and related options.

This test supports being run with GIT_TEST_LONG=1 to adjust the limit on the number of commits from 1k to 10k. The 1k limit seems to hit a good spot on git.git

---
 t/perf/p4209-pickaxe.sh | 70 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 70 insertions(+)
 create mode 100755 t/perf/p4209-pickaxe.sh
t/perf/p4209-pickaxe.sh new file +70 -0
new file mode 100755
index 00000000000..f585a4465ae
@@ -0,0 +1,70 @@
+#!/bin/sh
+
+test_description="Test pickaxe performance"
+
+. ./perf-lib.sh
+
+test_perf_default_repo
+
+# Not --max-count, as that's the number of matching commit, so it's
+# unbounded. We want to limit our revision walk here.
+from_rev_desc=
+from_rev=
+max_count=1000
+if test_have_prereq EXPENSIVE
+then
+	max_count=10000
+fi
+from_rev=" $(git rev-list HEAD | head -n $max_count | tail -n 1).."
+from_rev_desc=" <limit-rev>.."
+
+for icase in \
+	'' \
+	'-i '
+do
+	# -S (no regex)
+	for pattern in \
+		'int main' \
+		'æ'
+	do
+		for opts in \
+			'-S'
+		do
+			test_perf "git log $icase$opts'$pattern'$from_rev_desc" "
+				git log --pretty=format:%H $icase$opts'$pattern'$from_rev
+			"
+		done
+	done
+
+	# -S (regex)
+	for pattern in  \
+		'(int|void|null)' \
+		'if *\([^ ]+ & ' \
+		'[àáâãäåæñøùúûüýþ]'
+	do
+		for opts in \
+			'--pickaxe-regex -S'
+		do
+			test_perf "git log $icase$opts'$pattern'$from_rev_desc" "
+				git log --pretty=format:%H $icase$opts'$pattern'$from_rev
+			"
+		done
+	done
+
+	# -G
+	for pattern in  \
+		'(int|void|null)' \
+		'if *\([^ ]+ & ' \
+		'[àáâãäåæñøùúûüýþ]'
+	do
+		for opts in \
+			'-G'
+		do
+			test_perf "git log $icase$opts'$pattern'$from_rev_desc" "
+				git log --pretty=format:%H $icase$opts'$pattern'$from_rev
+			"
+		done
+	done
+done
+
+test_done
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#89 lore raw

PATCH v2 16/22 pickaxe -S: support content with NULs under --pickaxe-regex

In reply to Ævar Arnfjörð Bjarmason

Fix a bug in the matching routine powering -S<rx> --pickaxe-regex so that we won't abort early on content that has NULs in it.

We've had a hard requirement on REG_STARTEND since 2f8952250a8 (regex:
add regexec_buf() that can work on a non NUL-terminated string, 2016-09-21), but this sanity check dates back to d01d8c67828 (Support for pickaxe matching regular expressions, 2006-03-29).

It wasn't needed anymore, and as the now-passing test shows, actively getting in our way.

---
 diffcore-pickaxe.c     | 4 ++--
 t/t4209-log-pickaxe.sh | 8 ++++++++
 2 files changed, 10 insertions(+), 2 deletions(-)
diffcore-pickaxe.c +2 -2
index dd1b5c72332..0bf50a2f595 100644
@@ -78,12 +78,12 @@ static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws)
 		regmatch_t regmatch;
 		int flags = 0;
 
-		while (sz && *data &&
+		while (sz &&
 		       !regexec_buf(regexp, data, sz, 1, &regmatch, flags)) {
 			flags |= REG_NOTBOL;
 			data += regmatch.rm_eo;
 			sz -= regmatch.rm_eo;
-			if (sz && *data && regmatch.rm_so == regmatch.rm_eo) {
+			if (sz && regmatch.rm_so == regmatch.rm_eo) {
 				data++;
 				sz--;
 			}
t/t4209-log-pickaxe.sh +8 -0
index 4b65b89e7a5..6ea1f02d142 100755
@@ -201,4 +201,12 @@ test_expect_success 'log -S looks into binary files' '
 	test_cmp log full-log
 '
 
+test_expect_success 'log -S --pickaxe-regex looks into binary files' '
+	git -C GS-bin-txt log --pickaxe-regex -Sa >log &&
+	test_cmp log full-log &&
+
+	git -C GS-bin-txt log --pickaxe-regex -S[a] >log &&
+	test_cmp log full-log
+'
+
 test_done
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#97 lore raw

PATCH v2 21/22 pickaxe -G: terminate early on matching lines

In reply to Ævar Arnfjörð Bjarmason

Solve a long-standing item for "git log -Grx" of us e.g. finding "+ str" in the diff context and noting that we had a "hit", but xdiff diligently continuing to generate and spew the rest of the diff at us.

The TODO item has been there since "git log -G" was implemented. See f506b8e8b5f (git log/diff: add -G<regexp> that greps in the patch text, 2010-08-23).

Our xdiff interface also had the limitation of not being able to abort early since the beginning, see d9ea73e0564 (combine-diff: refactor built-in xdiff interface., 2006-04-05). Although at that time "xdiff_emit_line_fn" was called "xdiff_emit_consume_fn", and "xdiff_emit_hunk_fn" didn't exist yet.

But now with the support added in the preceding ""xdiff-interface:
allow early return from xdiff_emit_{line,hunk}_fn" commit we can return early, and furthermore test the functionality of the new early-exit xdiff-interface by having a BUG() call here to die if it ever starts handing us needless work again.

---
 diffcore-pickaxe.c | 31 ++++++++++++++++++++-----------
 xdiff-interface.h  |  5 +++++
 2 files changed, 25 insertions(+), 11 deletions(-)
diffcore-pickaxe.c +20 -11
index 94601072bde..f11b38b7121 100644
@@ -27,13 +27,12 @@ static int diffgrep_consume(void *priv, char *line, unsigned long len)
 	if (line[0] != '+' && line[0] != '-')
 		return 0;
 	if (data->hit)
-		/*
-		 * NEEDSWORK: we should have a way to terminate the
-		 * caller early.
-		 */
-		return 0;
-	data->hit = !regexec_buf(data->regexp, line + 1, len - 1, 1,
-				 &regmatch, 0);
+		BUG("Already matched in diffgrep_consume! Broken xdiff_emit_line_fn?");
+	if (!regexec_buf(data->regexp, line + 1, len - 1, 1,
+			 &regmatch, 0)) {
+		data->hit = 1;
+		return -1;
+	}
 	return 0;
 }
 
@@ -45,6 +44,7 @@ static int diff_grep(mmfile_t *one, mmfile_t *two,
 	struct diffgrep_cb ecbdata;
 	xpparam_t xpp;
 	xdemitconf_t xecfg;
+	int ret;
 
 	if (!one)
 		return !regexec_buf(regexp, two->ptr, two->size,
@@ -63,10 +63,19 @@ static int diff_grep(mmfile_t *one, mmfile_t *two,
 	ecbdata.hit = 0;
 	xecfg.ctxlen = o->context;
 	xecfg.interhunkctxlen = o->interhunkcontext;
-	if (xdi_diff_outf(one, two, discard_hunk_line, diffgrep_consume,
-			  &ecbdata, &xpp, &xecfg))
-		return 0;
-	return ecbdata.hit;
+
+	/*
+	 * An xdiff error might be our "data->hit" from above. See the
+	 * comment for xdiff_emit_{line,hunk}_fn in xdiff-interface.h
+	 * for why.
+	 */
+	ret = xdi_diff_outf(one, two, discard_hunk_line, diffgrep_consume,
+			    &ecbdata, &xpp, &xecfg);
+	if (ecbdata.hit)
+		return 1;
+	if (ret)
+		return ret;
+	return 0;
 }
 
 static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws,
xdiff-interface.h +5 -0
index 1b27d6104ce..347d8a4425f 100644
@@ -25,6 +25,11 @@
  * granular return values, but for now use it carefully, or consider
  * e.g. using discard_hunk_line() if you say just don't care about
  * hunk headers.
+ *
+ * Note that just returning -1 will make your early return
+ * indistinguishable from an error internal to xdiff. See "diff_grep"
+ * in diffcore-pickaxe.c for a trick to work around this, i.e. using
+ * the "consume_callback_data" to note the desired early return.
  */
 typedef int (*xdiff_emit_line_fn)(void *, char *, unsigned long);
 typedef int (*xdiff_emit_hunk_fn)(void *data,
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#99 lore raw

PATCH v2 22/22 pickaxe -G: don't special-case create/delete

In reply to Ævar Arnfjörð Bjarmason

Instead of special-casing creations and deletions let's just generate a diff for them.

This logic of not running a diff under -G if we don't have both sides dates back to the original implementation of -S in 52e9578985f ([PATCH] Introducing software archaeologist's tool "pickaxe"., 2005-05-21).

In the case of -S we were not working with the xdiff interface and needed to do this, but when -G was implemented in f506b8e8b5f (git log/diff: add -G<regexp> that greps in the patch text, 2010-08-23) this logic was diligently copied over.

But as the performance test added earlier in this series shows, this does not make much of a difference. With:

time GIT_TEST_LONG= GIT_PERF_REPEAT_COUNT=10 GIT_PERF_MAKE_OPTS='-j8 CFLAGS=-O3' ./run origin/next HEAD~ HEAD -- p4209-pickaxe.sh

With the HEAD~ commit being the preceding "pickaxe -G: terminate early on matching lines" we get these results. Note that it's only the -G codepaths that are relevant to this change:

    Test                                                                      origin/next       HEAD~                   HEAD
    -----------------------------------------------------------------------------------------------------------------------------------------
    4209.1: git log -S'int main' <limit-rev>..                                0.35(0.32+0.03)   0.35(0.33+0.02) +0.0%   0.35(0.30+0.05) +0.0%
    4209.2: git log -S'æ' <limit-rev>..                                       0.46(0.42+0.04)   0.46(0.41+0.05) +0.0%   0.46(0.42+0.04) +0.0%
    4209.3: git log --pickaxe-regex -S'(int|void|null)' <limit-rev>..         0.65(0.62+0.02)   0.64(0.61+0.02) -1.5%   0.64(0.60+0.04) -1.5%
    4209.4: git log --pickaxe-regex -S'if *\([^ ]+ & ' <limit-rev>..          0.52(0.45+0.06)   0.52(0.50+0.01) +0.0%   0.54(0.47+0.04) +3.8%
    4209.5: git log --pickaxe-regex -S'[àáâãäåæñøùúûüýþ]' <limit-rev>..       0.39(0.34+0.05)   0.39(0.34+0.04) +0.0%   0.39(0.36+0.03) +0.0%
    4209.6: git log -G'(int|void|null)' <limit-rev>..                         0.60(0.55+0.04)   0.58(0.54+0.03) -3.3%   0.58(0.49+0.08) -3.3%
    4209.7: git log -G'if *\([^ ]+ & ' <limit-rev>..                          0.61(0.52+0.06)   0.59(0.53+0.05) -3.3%   0.59(0.54+0.05) -3.3%
    4209.8: git log -G'[àáâãäåæñøùúûüýþ]' <limit-rev>..                       0.61(0.51+0.07)   0.58(0.54+0.04) -4.9%   0.57(0.51+0.06) -6.6%
    4209.9: git log -i -S'int main' <limit-rev>..                             0.36(0.31+0.04)   0.36(0.34+0.02) +0.0%   0.35(0.32+0.03) -2.8%
    4209.10: git log -i -S'æ' <limit-rev>..                                   0.36(0.33+0.03)   0.39(0.34+0.01) +8.3%   0.36(0.32+0.03) +0.0%
    4209.11: git log -i --pickaxe-regex -S'(int|void|null)' <limit-rev>..     0.83(0.77+0.05)   0.82(0.77+0.05) -1.2%   0.80(0.75+0.04) -3.6%
    4209.12: git log -i --pickaxe-regex -S'if *\([^ ]+ & ' <limit-rev>..      0.67(0.61+0.03)   0.64(0.61+0.03) -4.5%   0.63(0.61+0.02) -6.0%
    4209.13: git log -i --pickaxe-regex -S'[àáâãäåæñøùúûüýþ]' <limit-rev>..   0.40(0.37+0.02)   0.40(0.37+0.03) +0.0%   0.40(0.36+0.04) +0.0%
    4209.14: git log -i -G'(int|void|null)' <limit-rev>..                     0.58(0.51+0.07)   0.59(0.52+0.06) +1.7%   0.58(0.52+0.05) +0.0%
    4209.15: git log -i -G'if *\([^ ]+ & ' <limit-rev>..                      0.60(0.54+0.05)   0.60(0.54+0.06) +0.0%   0.60(0.56+0.03) +0.0%
    4209.16: git log -i -G'[àáâãäåæñøùúûüýþ]' <limit-rev>..                   0.58(0.51+0.06)   0.57(0.52+0.05) -1.7%   0.60(0.48+0.09) +3.4%

This small simplification really doesn't buy us much now, but I've got plans to both convert the pickaxe code to using a PCREv2 backend[1] and to implement additional pickaxe modes to do custom searches through the diff[2]. Always having the diff available under -G is going to help to simplify both of those changes.

1. https://lore.kernel.org/git/[email protected]/
2. https://lore.kernel.org/git/[email protected]/

---
 diffcore-pickaxe.c | 12 +-----------
 1 file changed, 1 insertion(+), 11 deletions(-)
diffcore-pickaxe.c +1 -11
index f11b38b7121..94d3890e669 100644
@@ -40,19 +40,11 @@ static int diff_grep(mmfile_t *one, mmfile_t *two,
 		     struct diff_options *o,
 		     regex_t *regexp, kwset_t kws)
 {
-	regmatch_t regmatch;
 	struct diffgrep_cb ecbdata;
 	xpparam_t xpp;
 	xdemitconf_t xecfg;
 	int ret;
 
-	if (!one)
-		return !regexec_buf(regexp, two->ptr, two->size,
-				    1, &regmatch, 0);
-	if (!two)
-		return !regexec_buf(regexp, one->ptr, one->size,
-				    1, &regmatch, 0);
-
 	/*
 	 * We have both sides; need to run textual diff and see if
 	 * the pattern appears on added/deleted lines.
@@ -173,9 +165,7 @@ static int pickaxe_match(struct diff_filepair *p, struct diff_options *o,
 	mf1.size = fill_textconv(o->repo, textconv_one, p->one, &mf1.ptr);
 	mf2.size = fill_textconv(o->repo, textconv_two, p->two, &mf2.ptr);
 
-	ret = fn(DIFF_FILE_VALID(p->one) ? &mf1 : NULL,
-		 DIFF_FILE_VALID(p->two) ? &mf2 : NULL,
-		 o, regexp, kws);
+	ret = fn(&mf1, &mf2, o, regexp, kws);
 
 	if (textconv_one)
 		free(mf1.ptr);
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#92 lore raw

PATCH v2 18/22 pickaxe -S: slightly optimize contains()

In reply to Ævar Arnfjörð Bjarmason

When the "log -S<pat>" switch counts occurrences of <pat> on the pre-image and post-image of a change. As soon as we know we had e.g. 1 before and 2 now we can stop, we don't need to keep counting past 2.

---
 diffcore-pickaxe.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
diffcore-pickaxe.c +10 -3
index 66e34d254f1..76c178bae2b 100644
@@ -68,7 +68,8 @@ static int diff_grep(mmfile_t *one, mmfile_t *two,
 	return ecbdata.hit;
 }
 
-static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws)
+static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws,
+			     unsigned int limit)
 {
 	unsigned int cnt = 0;
 	unsigned long sz = mf->size;
@@ -88,6 +89,9 @@ static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws)
 				sz--;
 			}
 			cnt++;
+
+			if (limit && cnt == limit)
+				return cnt;
 		}
 
 	} else { /* Classic exact string match */
@@ -99,6 +103,9 @@ static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws)
 			sz -= offset + kwsm.size[0];
 			data += offset + kwsm.size[0];
 			cnt++;
+
+			if (limit && cnt == limit)
+				return cnt;
 		}
 	}
 	return cnt;
@@ -108,8 +115,8 @@ static int has_changes(mmfile_t *one, mmfile_t *two,
 		       struct diff_options *o,
 		       regex_t *regexp, kwset_t kws)
 {
-	unsigned int c1 = one ? contains(one, regexp, kws) : 0;
-	unsigned int c2 = two ? contains(two, regexp, kws) : 0;
+	unsigned int c1 = one ? contains(one, regexp, kws, 0) : 0;
+	unsigned int c2 = two ? contains(two, regexp, kws, c1 + 1) : 0;
 	return c1 != c2;
 }
 
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#96 lore raw

PATCH v2 20/22 xdiff-interface: support early exit in xdiff_outf()

In reply to Ævar Arnfjörð Bjarmason

Bridge the gap between the preceding "xdiff-interface: allow early return from xdiff_emit_{line,hunk}_fn" change and the public interface.

This change was split off from the preceding commit as it wasn't a purely mechanical addition of "return 0".

Here we want to be able to abort early, but do so in a way that doesn't skip the appropriate strbuf_reset() invocations.

The use of -1 as a return value is consistent with the rest of the xdiff codebase, where doing so signals an abort or error that'll propagate up the stack.

---
 xdiff-interface.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
xdiff-interface.c +12 -4
index ef557dc4e63..d066442470f 100644
@@ -39,7 +39,8 @@ static int consume_one(void *priv_, char *s, unsigned long size)
 		unsigned long this_size;
 		ep = memchr(s, '\n', size);
 		this_size = (ep == NULL) ? size : (ep - s + 1);
-		priv->line_fn(priv->consume_callback_data, s, this_size);
+		if (priv->line_fn(priv->consume_callback_data, s, this_size))
+			return -1;
 		size -= this_size;
 		s += this_size;
 	}
@@ -50,11 +51,14 @@ static int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
 {
 	struct xdiff_emit_state *priv = priv_;
 	int i;
+	int stop = 0;
 
 	if (!priv->line_fn)
 		return 0;
 
 	for (i = 0; i < nbuf; i++) {
+		if (stop)
+			return -1;
 		if (mb[i].ptr[mb[i].size-1] != '\n') {
 			/* Incomplete line */
 			strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
@@ -63,17 +67,21 @@ static int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
 
 		/* we have a complete line */
 		if (!priv->remainder.len) {
-			consume_one(priv, mb[i].ptr, mb[i].size);
+			stop = consume_one(priv, mb[i].ptr, mb[i].size);
 			continue;
 		}
 		strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
-		consume_one(priv, priv->remainder.buf, priv->remainder.len);
+		stop = consume_one(priv, priv->remainder.buf, priv->remainder.len);
 		strbuf_reset(&priv->remainder);
 	}
+	if (stop)
+		return -1;
 	if (priv->remainder.len) {
-		consume_one(priv, priv->remainder.buf, priv->remainder.len);
+		stop = consume_one(priv, priv->remainder.buf, priv->remainder.len);
 		strbuf_reset(&priv->remainder);
 	}
+	if (stop)
+		return -1;
 	return 0;
 }
 
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#94 lore raw

PATCH v2 19/22 xdiff-interface: allow early return from xdiff_emit_{line,hunk}_fn

In reply to Ævar Arnfjörð Bjarmason

Change the function prototype of xdiff_emit_{line,hunk}_fn to return an int instead of void. This will allow for returning early from hunk & diff consumers that want some of the data, but not all of it.

No behavior is being changed here, just replacing the equivalent of "return" with "return 0", nothing acts on the changed return values yet.

There was some work in this area of xdiff-interface.[ch] recently with 3b40a090fd4 (diff: avoid generating unused hunk header lines, 2018-11-02) and 7c61e25fbf1 (diff: use hunk callback for word-diff, 2018-11-02).

In combination those two changes allow us to not do any work on the hunks and diff at all, but didn't change the status quo with regards to consumers that e.g. want the diff lines, but might want to abort early.

Whereas soon we can abort e.g. on the first "-line" of a 1000 line diff if that's all we needed.

This interface is rather scary as noted in the comment to xdiff-interface.h being added here, but it will be useful for diffcore-pickaxe.c in a subsequent commit. A future change could e.g. add more exit codes, and hack xdl_emit_diff() and friends to ignore or skip things more selectively as a result.

---
 combine-diff.c     |  9 ++++++---
 diff.c             | 39 +++++++++++++++++++++++----------------
 diffcore-pickaxe.c |  7 ++++---
 range-diff.c       |  8 +++++---
 xdiff-interface.c  | 10 ++++++----
 xdiff-interface.h  | 31 +++++++++++++++++++++++--------
 6 files changed, 67 insertions(+), 37 deletions(-)
combine-diff.c +6 -3
index 9228aebc16b..6590c4b5fb7 100644
@@ -369,7 +369,7 @@ struct combine_diff_state {
 	struct sline *lost_bucket;
 };
 
-static void consume_hunk(void *state_,
+static int consume_hunk(void *state_,
 			 long ob, long on,
 			 long nb, long nn,
 			 const char *funcline, long funclen)
@@ -401,13 +401,15 @@ static void consume_hunk(void *state_,
 		state->sline[state->nb-1].p_lno =
 			xcalloc(state->num_parent, sizeof(unsigned long));
 	state->sline[state->nb-1].p_lno[state->n] = state->ob;
+
+	return 0;
 }
 
-static void consume_line(void *state_, char *line, unsigned long len)
+static int consume_line(void *state_, char *line, unsigned long len)
 {
 	struct combine_diff_state *state = state_;
 	if (!state->lost_bucket)
-		return; /* not in any hunk yet */
+		return 0; /* not in any hunk yet */
 	switch (line[0]) {
 	case '-':
 		append_lost(state->lost_bucket, state->n, line+1, len-1);
@@ -417,6 +419,7 @@ static void consume_line(void *state_, char *line, unsigned long len)
 		state->lno++;
 		break;
 	}
+	return 0;
 }
 
 static void combine_diff(struct repository *r,
diff.c +23 -16
index 63c49ecaef1..4d554281147 100644
@@ -1996,10 +1996,10 @@ static int color_words_output_graph_prefix(struct diff_words_data *diff_words)
 	}
 }
 
-static void fn_out_diff_words_aux(void *priv,
-				  long minus_first, long minus_len,
-				  long plus_first, long plus_len,
-				  const char *func, long funclen)
+static int fn_out_diff_words_aux(void *priv,
+				 long minus_first, long minus_len,
+				 long plus_first, long plus_len,
+				 const char *func, long funclen)
 {
 	struct diff_words_data *diff_words = priv;
 	struct diff_words_style *style = diff_words->style;
@@ -2047,6 +2047,8 @@ static void fn_out_diff_words_aux(void *priv,
 
 	diff_words->current_plus = plus_end;
 	diff_words->last_minus = minus_first;
+
+	return 0;
 }
 
 /* This function starts looking at *begin, and returns 0 iff a word was found. */
@@ -2338,7 +2340,7 @@ static void find_lno(const char *line, struct emit_callback *ecbdata)
 	ecbdata->lno_in_postimage = strtol(p + 1, NULL, 10);
 }
 
-static void fn_out_consume(void *priv, char *line, unsigned long len)
+static int fn_out_consume(void *priv, char *line, unsigned long len)
 {
 	struct emit_callback *ecbdata = priv;
 	struct diff_options *o = ecbdata->opt;
@@ -2374,7 +2376,7 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
 		len = sane_truncate_line(line, len);
 		find_lno(line, ecbdata);
 		emit_hunk_header(ecbdata, line, len);
-		return;
+		return 0;
 	}
 
 	if (ecbdata->diff_words) {
@@ -2384,11 +2386,11 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
 		if (line[0] == '-') {
 			diff_words_append(line, len,
 					  &ecbdata->diff_words->minus);
-			return;
+			return 0;
 		} else if (line[0] == '+') {
 			diff_words_append(line, len,
 					  &ecbdata->diff_words->plus);
-			return;
+			return 0;
 		} else if (starts_with(line, "\\ ")) {
 			/*
 			 * Eat the "no newline at eof" marker as if we
@@ -2397,11 +2399,11 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
 			 * defer processing. If this is the end of
 			 * preimage, more "+" lines may come after it.
 			 */
-			return;
+			return 0;
 		}
 		diff_words_flush(ecbdata);
 		emit_diff_symbol(o, s, line, len, 0);
-		return;
+		return 0;
 	}
 
 	switch (line[0]) {
@@ -2425,6 +2427,7 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
 				 line, len, 0);
 		break;
 	}
+	return 0;
 }
 
 static void pprint_rename(struct strbuf *name, const char *a, const char *b)
@@ -2524,7 +2527,7 @@ static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
 	return x;
 }
 
-static void diffstat_consume(void *priv, char *line, unsigned long len)
+static int diffstat_consume(void *priv, char *line, unsigned long len)
 {
 	struct diffstat_t *diffstat = priv;
 	struct diffstat_file *x = diffstat->files[diffstat->nr - 1];
@@ -2533,6 +2536,7 @@ static void diffstat_consume(void *priv, char *line, unsigned long len)
 		x->added++;
 	else if (line[0] == '-')
 		x->deleted++;
+	return 0;
 }
 
 const char mime_boundary_leader[] = "------------";
@@ -3201,16 +3205,17 @@ static int is_conflict_marker(const char *line, int marker_size, unsigned long l
 	return 1;
 }
 
-static void checkdiff_consume_hunk(void *priv,
+static int checkdiff_consume_hunk(void *priv,
 				   long ob, long on, long nb, long nn,
 				   const char *func, long funclen)
 
 {
 	struct checkdiff_t *data = priv;
 	data->lineno = nb - 1;
+	return 0;
 }
 
-static void checkdiff_consume(void *priv, char *line, unsigned long len)
+static int checkdiff_consume(void *priv, char *line, unsigned long len)
 {
 	struct checkdiff_t *data = priv;
 	int marker_size = data->conflict_marker_size;
@@ -3234,7 +3239,7 @@ static void checkdiff_consume(void *priv, char *line, unsigned long len)
 		}
 		bad = ws_check(line + 1, len - 1, data->ws_rule);
 		if (!bad)
-			return;
+			return 0;
 		data->status |= bad;
 		err = whitespace_error_string(bad);
 		fprintf(data->o->file, "%s%s:%d: %s.\n",
@@ -3246,6 +3251,7 @@ static void checkdiff_consume(void *priv, char *line, unsigned long len)
 	} else if (line[0] == ' ') {
 		data->lineno++;
 	}
+	return 0;
 }
 
 static unsigned char *deflate_it(char *data,
@@ -6104,17 +6110,18 @@ void flush_one_hunk(struct object_id *result, git_hash_ctx *ctx)
 	}
 }
 
-static void patch_id_consume(void *priv, char *line, unsigned long len)
+static int patch_id_consume(void *priv, char *line, unsigned long len)
 {
 	struct patch_id_t *data = priv;
 	int new_len;
 
 	if (len > 12 && starts_with(line, "\\ "))
-		return;
+		return 0;
 	new_len = remove_space(line, len);
 
 	the_hash_algo->update_fn(data->ctx, line, new_len);
 	data->patchlen += new_len;
+	return 0;
 }
 
 static void patch_id_add_string(git_hash_ctx *ctx, const char *str)
diffcore-pickaxe.c +4 -3
index 76c178bae2b..94601072bde 100644
@@ -19,21 +19,22 @@ struct diffgrep_cb {
 	int hit;
 };
 
-static void diffgrep_consume(void *priv, char *line, unsigned long len)
+static int diffgrep_consume(void *priv, char *line, unsigned long len)
 {
 	struct diffgrep_cb *data = priv;
 	regmatch_t regmatch;
 
 	if (line[0] != '+' && line[0] != '-')
-		return;
+		return 0;
 	if (data->hit)
 		/*
 		 * NEEDSWORK: we should have a way to terminate the
 		 * caller early.
 		 */
-		return;
+		return 0;
 	data->hit = !regexec_buf(data->regexp, line + 1, len - 1, 1,
 				 &regmatch, 0);
+	return 0;
 }
 
 static int diff_grep(mmfile_t *one, mmfile_t *two,
range-diff.c +5 -3
index a3cc7c94a3d..f51c6a67712 100644
@@ -274,15 +274,17 @@ static void find_exact_matches(struct string_list *a, struct string_list *b)
 	hashmap_clear(&map);
 }
 
-static void diffsize_consume(void *data, char *line, unsigned long len)
+static int diffsize_consume(void *data, char *line, unsigned long len)
 {
 	(*(int *)data)++;
+	return 0;
 }
 
-static void diffsize_hunk(void *data, long ob, long on, long nb, long nn,
-			  const char *funcline, long funclen)
+static int diffsize_hunk(void *data, long ob, long on, long nb, long nn,
+			 const char *funcline, long funclen)
 {
 	diffsize_consume(data, NULL, 0);
+	return 0;
 }
 
 static int diffsize(const char *a, const char *b)
xdiff-interface.c +6 -4
index 4d20069302b..ef557dc4e63 100644
@@ -31,7 +31,7 @@ static int xdiff_out_hunk(void *priv_,
 	return 0;
 }
 
-static void consume_one(void *priv_, char *s, unsigned long size)
+static int consume_one(void *priv_, char *s, unsigned long size)
 {
 	struct xdiff_emit_state *priv = priv_;
 	char *ep;
@@ -43,6 +43,7 @@ static void consume_one(void *priv_, char *s, unsigned long size)
 		size -= this_size;
 		s += this_size;
 	}
+	return 0;
 }
 
 static int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
@@ -115,10 +116,11 @@ int xdi_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t co
 	return xdl_diff(&a, &b, xpp, xecfg, xecb);
 }
 
-void discard_hunk_line(void *priv,
-		       long ob, long on, long nb, long nn,
-		       const char *func, long funclen)
+int discard_hunk_line(void *priv,
+		      long ob, long on, long nb, long nn,
+		      const char *func, long funclen)
 {
+	return 0;
 }
 
 int xdi_diff_outf(mmfile_t *mf1, mmfile_t *mf2,
xdiff-interface.h +23 -8
index 93df26900c2..1b27d6104ce 100644
@@ -11,11 +11,26 @@
  */
 #define MAX_XDIFF_SIZE (1024UL * 1024 * 1023)
 
-typedef void (*xdiff_emit_line_fn)(void *, char *, unsigned long);
-typedef void (*xdiff_emit_hunk_fn)(void *data,
-				   long old_begin, long old_nr,
-				   long new_begin, long new_nr,
-				   const char *func, long funclen);
+/*
+ * The xdiff_emit_{line,hunk}_fn consumers can return -1 to abort
+ * early, or 0 to continue processing. Note that doing so is an
+ * all-or-nothing affair, as returning -1 will return all the way to
+ * the top-level, e.g. the xdi_diff_outf() call to generate the diff.
+ *
+ * Thus returning -1 from a hunk header callback means you won't be
+ * getting any more hunks, or diffs, and likewise returning from a
+ * line callback means you won't be getting anymore lines.
+ *
+ * We may extend the interface in the future to understand other more
+ * granular return values, but for now use it carefully, or consider
+ * e.g. using discard_hunk_line() if you say just don't care about
+ * hunk headers.
+ */
+typedef int (*xdiff_emit_line_fn)(void *, char *, unsigned long);
+typedef int (*xdiff_emit_hunk_fn)(void *data,
+				  long old_begin, long old_nr,
+				  long new_begin, long new_nr,
+				  const char *func, long funclen);
 
 int xdi_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t const *xecfg, xdemitcb_t *ecb);
 int xdi_diff_outf(mmfile_t *mf1, mmfile_t *mf2,
@@ -36,9 +51,9 @@ extern int git_xmerge_style;
  * Can be used as a no-op hunk_fn for xdi_diff_outf(), since a NULL
  * one just sends the hunk line to the line_fn callback).
  */
-void discard_hunk_line(void *priv,
-		       long ob, long on, long nb, long nn,
-		       const char *func, long funclen);
+int discard_hunk_line(void *priv,
+		      long ob, long on, long nb, long nn,
+		      const char *func, long funclen);
 
 /*
  * Compare the strings l1 with l2 which are of size s1 and s2 respectively.
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7
#41 lore raw

Re: [PATCH v2 00/22] pickaxe: test and refactoring for follow-up changes

In reply to Ævar Arnfjörð Bjarmason
Quoted 8 lines · Ævar Arnfjörð Bjarmason

This is a smaller v2 of the series to remove the kwset backend and make pickaxe use PCRE v2[1].

That's not being done here yet. These are mostly small refactoring/test fixes. The most significant work is a new xdiff interface at the end of the series.

It's based on next where some preparatory work already landed[2].

Do you really mean <[email protected]>?

grep/pcre2: drop needless assignment + assert() on opt->pcre2 grep/pcre2: drop needless assignment to NULL grep/pcre2: correct reference to grep_init() in comment grep/pcre2: prepare to add debugging to pcre2_malloc() grep/pcre2: add GREP_PCRE2_DEBUG_MALLOC debug mode grep/pcre2: use compile-time PCREv2 version test grep/pcre2: use pcre2_maketables_free() function grep/pcre2: actually make pcre2 use custom allocator grep/pcre2: move back to thread-only PCREv2 structures grep/pcre2: move definitions of pcre2_{malloc,free}

I do not think we have that many patches whose title begin with grep/pcre2 in 'next'.

In any case, I'd rather not to see things done directly on 'next'; targetting a selected few topics merged on top of 'master' would not be bad, though.

Thanks.

#42 lore raw

Re: [PATCH v2 00/22] pickaxe: test and refactoring for follow-up changes

Quoted 11 lines · Junio C Hamano
Quoted 8 lines · Ævar Arnfjörð Bjarmason

This is a smaller v2 of the series to remove the kwset backend and make pickaxe use PCRE v2[1].

That's not being done here yet. These are mostly small refactoring/test fixes. The most significant work is a new xdiff interface at the end of the series.

It's based on next where some preparatory work already landed[2].

Quoted 8 lines

Do you really mean <[email protected]>?
...
I do not think we have that many patches whose title begin with grep/pcre2 in 'next'.

In any case, I'd rather not to see things done directly on 'next'; targetting a selected few topics merged on top of 'master' would not be bad, though.

It turns out that these patches textually depend on two topics in 'next', and both are to be merged to 'master' hopefully by the end of the week, so perhaps that is a good time to queue this topic to 'seen' on top of the then-current master.

Thanks.

#71 lore raw

Re: [PATCH v2 04/22] test-lib functions: add --printf option to test_commit

In reply to Ævar Arnfjörð Bjarmason

t/t1307-config-blob.sh test_expect_success 'parse errors in blobs are properly attributed' ' · line 68

Quoted 5 lines · Ævar Arnfjörð Bjarmason

test_expect_success 'can parse blob ending with CR' '

t/t1307-config-blob.sh +1 -3
-	printf "[some]key = value\\r" >config &&
-	git add config &&
-	git commit -m CR &&
+	test_commit --printf CR config "[some]key = value\\r" &&

OK, the first arg becomes the commit title, and the second one is used for the filename, and the next arg is the string given to printf, I guess.

t/t2030-unresolve-info.sh test_expect_success 'rerere and rerere forget (subdirectory)' ' · line 182

Quoted 5 lines

test_expect_success 'rerere forget (binary)' '

t/t2030-unresolve-info.sh +1 -2
 	git checkout -f side &&
-	printf "a\0c" >binary &&
-	git commit -a -m binary &&
+	test_commit binary binary "a\0c" &&

This lacks --printf. Are we breaking the test but "test-must-fail" is hiding the breakage here?

Quoted 3 lines
t/t2030-unresolve-info.sh +0 -0
 	test_must_fail git merge second &&
 	git rerere forget binary
 '
#68 lore raw

Re: [PATCH v2 02/22] test-lib-functions: document and test test_commit --no-tag

In reply to Ævar Arnfjörð Bjarmason

t/test-lib-functions.sh test_commit () { · line 248

Quoted 6 lines · Ævar Arnfjörð Bjarmason
t/test-lib-functions.sh +1 -1
@@ -242,7 +245,10 @@ test_commit () {
 	git ${indir:+ -C "$indir"} commit \
 	    ${author:+ --author "$author"} \
 	    $signoff -m "$1" &&
-	if test -z "$no_tag"
+	if test -n "$no_tag" -a $# -eq 4

Let's spell it

	test -n "$no_tag" && test $# = 4

to avoid clueless newbie from cargo-culting the use of '-o' and '-a' with test in an unsafe context, even though the way you used it here is perfectly safe.

Quoted 6 lines
t/test-lib-functions.sh +3 -0
+	then
+		BUG "expect no <tag> parameter with --no-tag"
+	elif test -z "$no_tag"
 	then
 		git ${indir:+ -C "$indir"} tag "${4:-$1}"
 	fi
#74 lore raw

Re: [PATCH v2 05/22] pickaxe tests: refactor to use test_commit --append --printf

In reply to Ævar Arnfjörð Bjarmason
Quoted 8 lines · Ævar Arnfjörð Bjarmason

Refactor existing tests added in e0e7cb8080c (log -G: ignore binary files, 2018-12-14) to use the --append option I added in 3373518cc8b (test-lib functions: add an --append option to test_commit, 2021-01-12) and the --printf option added in a preceding commit.

See also f5d79bf7dd6 (tests: refactor a few tests to use "test_commit --append", 2021-01-12) for prior similar refactoring.

This does not exactly look like a "refactoring". There are at least two differences and it is the courteous thing to do to readers to note them and explain why these differences do not impact the correctness of the tests, I would say.

* The original uses a dedicated branch, while the rewritten uses a dedicated repository for these tests. This does not impact the correctness as long as all the tests that originally used the branch are made to run in this new repository with "git -C".

* The youngest change removes the binary sample file, while the rewritten only truncates it. This does not impact the correctness, because a removed file and a file whose lines are all removed behave in the same way with respect to -G/-S. Both will reduce the number of occurrence of -S<needle>, and make "removal" (-) lines of -G<needle> appear in the patch.

t/t4209-log-pickaxe.sh test_expect_success 'log -S --no-textconv (missing textconv tool)' ' · line 111

Quoted 13 lines

test_expect_success 'setup log -[GS] binary & --text' '

t/t4209-log-pickaxe.sh +2 -10
-	git checkout --orphan GS-binary-and-text &&
-	git read-tree --empty &&
-	printf "a\na\0a\n" >data.bin &&
-	git add data.bin &&
-	git commit -m "create binary file" data.bin &&
-	printf "a\na\0a\n" >>data.bin &&
-	git commit -m "modify binary file" data.bin &&
-	git rm data.bin &&
-	git commit -m "delete binary file" data.bin &&
-	git log >full-log
+	test_create_repo GS-bin-txt &&
+	test_commit -C GS-bin-txt --append --printf A data.bin "a\na\0a\n" &&

The --append on the first one does not make any difference ;-)

t/t4209-log-pickaxe.sh test_expect_success 'log -S --no-textconv (missing textconv tool)' ' · line 113

Quoted 2 lines
t/t4209-log-pickaxe.sh +2 -0
+	test_commit -C GS-bin-txt --append --printf B data.bin "a\na\0a\n" &&
+	test_commit -C GS-bin-txt C data.bin "" &&

The original removes the file at the end, while this merely truncates it.

Quoted 2 lines
t/t4209-log-pickaxe.sh +1 -0
+	git -C GS-bin-txt log >full-log
 '
#78 lore raw

Re: [PATCH v2 08/22] pickaxe tests: test for -G, -S and --find-object incompatibility

In reply to Ævar Arnfjörð Bjarmason
Quoted 16 lines · Ævar Arnfjörð Bjarmason

Add a test for the options sanity check added in 5e505257f2 (diff:
properly error out when combining multiple pickaxe options, 2018-01-04).

---
 t/t4209-log-pickaxe.sh | 11 +++++++++++
 1 file changed, 11 insertions(+)
t/t4209-log-pickaxe.sh +0 -0
index c6b4751d5b6..5ad4fad964c 100755
@@ -55,6 +55,17 @@ test_expect_success setup '
 	git rev-parse --verify HEAD >expect_second
 '

t/t4209-log-pickaxe.sh test_expect_success setup ' · line 66

Quoted 10 lines
t/t4209-log-pickaxe.sh +9 -0
 
+test_expect_success 'usage' '
+	test_expect_code 128 git log -Gregex -Sstring 2>err &&
+	test_i18ngrep "mutually exclusive" err &&
+
+	test_expect_code 128 git log -Gregex --find-object=HEAD 2>err &&
+	test_i18ngrep "mutually exclusive" err &&
+
+	test_expect_code 128 git log -Gstring --find-object=HEAD 2>err &&
+	test_i18ngrep "mutually exclusive" err

Didn't you mean -Sstring for the third one? Otherwise we'd be testing -G/--find-object combination twice.

Quoted 5 lines
t/t4209-log-pickaxe.sh +2 -0
+'
+
 test_log	expect_initial	--grep initial
 test_log	expect_nomatch	--grep InItial
 test_log_icase	expect_initial	--grep InItial
#80 lore raw

Re: [PATCH v2 09/22] pickaxe: die when -G and --pickaxe-regex are combined

In reply to Ævar Arnfjörð Bjarmason
Quoted 9 lines · Ævar Arnfjörð Bjarmason

When the -G and --pickaxe-regex options are combined we simply ignore the --pickaxe-regex option. Let's die instead as suggested by our documentation, since -G is always a regex.

When --pickaxe-regex was added in d01d8c6782 (Support for pickaxe matching regular expressions, 2006-03-29) only the -S option existed. Then when -G was added in f506b8e8b5 (git log/diff: add -G<regexp> that greps in the patch text, 2010-08-23) neither the documentation for --pickaxe-regex was updater accordingly, nor was

s/updater/updated/;

Quoted 5 lines

something like this assertion added.

Since 5bc3f0b567 (diffcore-pickaxe doc: document -S and -G properly, 2013-05-31) we've claimed that --pickaxe-regex should only be used with -S, but have silently toileted combining it with -G, let's die

toilet? tolerate?

Quoted 1 line

instead.

Hmph. I've always hated that -G can only take regexp and users cannot ask for a fixed-string match. It may not be a bad idea to keep this as-is, as that would leave the door open for those who are motivated enough to later introduce --no-pickaxe-regex, so that -G<string> would naturally work with combination with it.

#86 lore raw

Re: [PATCH v2 14/22] pickaxe: refactor function selection in diffcore-pickaxe()

In reply to Ævar Arnfjörð Bjarmason

diffcore-pickaxe.c void diffcore_pickaxe(struct diff_options *o) · line 240

Quoted 10 lines · Ævar Arnfjörð Bjarmason
diffcore-pickaxe.c +3 -0
+	pickaxe_fn fn;
 
 	if (opts & (DIFF_PICKAXE_REGEX | DIFF_PICKAXE_KIND_G)) {
 		int cflags = REG_EXTENDED | REG_NEWLINE;
@@ -235,6 +236,14 @@ void diffcore_pickaxe(struct diff_options *o)
 			cflags |= REG_ICASE;
 		regcomp_or_die(&regex, needle, cflags);
 		regexp = &regex;
+
+		/* diff.c errors on -G and --pickaxe-regex for us */

I had to read this twice; I am guessing that the comment wants to say that this if/else if/else cascade is correct because KIND_G and PICKAXE_REGEX are mutually incompatible (ensured in diff.c). And I think that is true (but as I said, I am not sure if we want to cast in stone that kind-g and regex are mutually exclusive---rather, I'd want to see them eventually orthogonal).

Quoted 10 lines
diffcore-pickaxe.c +6 -0
+		if (opts & DIFF_PICKAXE_KIND_G)
+			fn = diff_grep;
+		else if (opts & DIFF_PICKAXE_REGEX)
+			fn = has_changes;
+		else
+			BUG("unreachable");
 	} else if (opts & DIFF_PICKAXE_KIND_S) {
 		if (o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE &&
 		    has_non_ascii(needle)) {

diffcore-pickaxe.c void diffcore_pickaxe(struct diff_options *o) · line 265

Quoted 6 lines
diffcore-pickaxe.c +3 -0
@@ -251,10 +260,14 @@ void diffcore_pickaxe(struct diff_options *o)
 			kwsincr(kws, needle, strlen(needle));
 			kwsprep(kws);
 		}
+		fn = has_changes;
+	} else if (opts & DIFF_PICKAXE_KIND_OBJFIND) {
+		fn = NULL;

This is the most valuable line in this patch ;-) It makes tons of sense.

Quoted 10 lines
diffcore-pickaxe.c +3 -2
+	} else {
+		BUG("unknown pickaxe_opts flag");
 	}
 
-	pickaxe(&diff_queued_diff, o, regexp, kws,
-		(opts & DIFF_PICKAXE_KIND_G) ? diff_grep : has_changes);
+	pickaxe(&diff_queued_diff, o, regexp, kws, fn);
 
 	if (regexp)
 		regfree(regexp);
#88 lore raw

Re: [PATCH v2 15/22] pickaxe: assert that we must have a needle under -G or -S

In reply to Ævar Arnfjörð Bjarmason

diffcore-pickaxe.c static int pickaxe_match(struct diff_filepair *p, struct diff_options *o, · line 136

Quoted 10 lines · Ævar Arnfjörð Bjarmason
diffcore-pickaxe.c +0 -2
index cff46f9f8f7..dd1b5c72332 100644
@@ -132,9 +132,6 @@ static int pickaxe_match(struct diff_filepair *p, struct diff_options *o,
 			 oidset_contains(o->objfind, &p->two->oid));
 	}
 
-	if (!o->pickaxe[0])
-		return 0;

So -S"" could pass o->pickaxe a non-NULL pointer, but the string pointed by that pointer could be 0-length. And that is not what we want to see happen.

diffcore-pickaxe.c void diffcore_pickaxe(struct diff_options *o) · line 231

Quoted 9 lines
diffcore-pickaxe.c +2 -0
 	if (o->flags.allow_textconv) {
 		textconv_one = get_textconv(o->repo, p->one);
 		textconv_two = get_textconv(o->repo, p->two);
@@ -230,6 +227,8 @@ void diffcore_pickaxe(struct diff_options *o)
 	kwset_t kws = NULL;
 	pickaxe_fn fn;
 
+	if (opts & ~DIFF_PICKAXE_KIND_OBJFIND && !needle)
+		BUG("should have needle under -G or -S");

Here, needle was picked up at the beginning of this function like so:

        void diffcore_pickaxe(struct diff_options *o)
        {
                const char *needle = o->pickaxe;

The two checks seem to be protecting from different things. Shouldn't the new BUG() condition be more like

	if ((opts & ~DIFF_PICKAXE_KIND_OBJFIND) && (!needle || !*needle))

instead?

Quoted 19 lines
diffcore-pickaxe.c +0 -0
 	if (opts & (DIFF_PICKAXE_REGEX | DIFF_PICKAXE_KIND_G)) {
 		int cflags = REG_EXTENDED | REG_NEWLINE;
 		if (o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE)
t/t4209-log-pickaxe.sh +6 -0
index bcaca7e882c..4b65b89e7a5 100755
@@ -56,6 +56,12 @@ test_expect_success setup '
 '
 
 test_expect_success 'usage' '
+	test_expect_code 129 git log -S 2>err &&
+	test_i18ngrep "switch.*requires a value" err &&
+
+	test_expect_code 129 git log -G 2>err &&
+	test_i18ngrep "switch.*requires a value" err &&
+
 	test_expect_code 128 git log -Gregex -Sstring 2>err &&
 	test_i18ngrep "mutually exclusive" err &&
#90 lore raw

Re: [PATCH v2 16/22] pickaxe -S: support content with NULs under --pickaxe-regex

In reply to Ævar Arnfjörð Bjarmason
Quoted 23 lines · Ævar Arnfjörð Bjarmason

Fix a bug in the matching routine powering -S<rx> --pickaxe-regex so that we won't abort early on content that has NULs in it.

We've had a hard requirement on REG_STARTEND since 2f8952250a8 (regex:
add regexec_buf() that can work on a non NUL-terminated string, 2016-09-21), but this sanity check dates back to d01d8c67828 (Support for pickaxe matching regular expressions, 2006-03-29).

It wasn't needed anymore, and as the now-passing test shows, actively getting in our way.

---
 diffcore-pickaxe.c     | 4 ++--
 t/t4209-log-pickaxe.sh | 8 ++++++++
 2 files changed, 10 insertions(+), 2 deletions(-)
diffcore-pickaxe.c +0 -0
index dd1b5c72332..0bf50a2f595 100644
@@ -78,12 +78,12 @@ static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws)
 		regmatch_t regmatch;

diffcore-pickaxe.c static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws) · line 86

Quoted 10 lines
diffcore-pickaxe.c +2 -2
 		int flags = 0;
 
-		while (sz && *data &&
+		while (sz &&
 		       !regexec_buf(regexp, data, sz, 1, &regmatch, flags)) {
 			flags |= REG_NOTBOL;
 			data += regmatch.rm_eo;
 			sz -= regmatch.rm_eo;
-			if (sz && *data && regmatch.rm_so == regmatch.rm_eo) {
+			if (sz && regmatch.rm_so == regmatch.rm_eo) {

OK. As we always require start-end support, we do not need to stop at NULs, and we shouldn't if we are dealing with a haystack with NUL in it. The needle may be behind that NUL.

Makes sense.

Quoted 8 lines
diffcore-pickaxe.c +0 -0
 				data++;
 				sz--;
 			}
t/t4209-log-pickaxe.sh +0 -0
index 4b65b89e7a5..6ea1f02d142 100755

t/t4209-log-pickaxe.sh test_expect_success 'log -S looks into binary files' ' · line 208

Quoted 8 lines
t/t4209-log-pickaxe.sh +5 -0
@@ -201,4 +201,12 @@ test_expect_success 'log -S looks into binary files' '
 	test_cmp log full-log
 '
 
+test_expect_success 'log -S --pickaxe-regex looks into binary files' '
+	git -C GS-bin-txt log --pickaxe-regex -Sa >log &&
+	test_cmp log full-log &&
+
+	git -C GS-bin-txt log --pickaxe-regex -S[a] >log &&

Please quote this so that readers do not have to look around to see if there _could_ have been a file with such a name that matches the [glob] to cause the needle not passed literally to us.

Quoted 4 lines
t/t4209-log-pickaxe.sh +3 -0
+	test_cmp log full-log
+'
+
 test_done
#93 lore raw

Re: [PATCH v2 18/22] pickaxe -S: slightly optimize contains()

In reply to Ævar Arnfjörð Bjarmason
Quoted 3 lines · Ævar Arnfjörð Bjarmason

When the "log -S<pat>" switch counts occurrences of <pat> on the pre-image and post-image of a change. As soon as we know we had e.g. 1 before and 2 now we can stop, we don't need to keep counting past 2.

Logical.

I do not know how much difference this would make in practice, though. The performance characteristics between "diff A B" and "diff B A" with the same pickaxe -Sneedle would be asymmetric with this optimization, which is a bit curious (but not incorrect).

I wonder if there is a good heuristics to decide which, between one and two, blob to start counting. Obviously, scanning the one that is likely to contain fewer occurrence of the needle, before we can employ this optimization to the other side, is what we want.

Quoted 50 lines
---
 diffcore-pickaxe.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
diffcore-pickaxe.c +10 -3
index 66e34d254f1..76c178bae2b 100644
@@ -68,7 +68,8 @@ static int diff_grep(mmfile_t *one, mmfile_t *two,
 	return ecbdata.hit;
 }
 
-static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws)
+static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws,
+			     unsigned int limit)
 {
 	unsigned int cnt = 0;
 	unsigned long sz = mf->size;
@@ -88,6 +89,9 @@ static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws)
 				sz--;
 			}
 			cnt++;
+
+			if (limit && cnt == limit)
+				return cnt;
 		}
 
 	} else { /* Classic exact string match */
@@ -99,6 +103,9 @@ static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws)
 			sz -= offset + kwsm.size[0];
 			data += offset + kwsm.size[0];
 			cnt++;
+
+			if (limit && cnt == limit)
+				return cnt;
 		}
 	}
 	return cnt;
@@ -108,8 +115,8 @@ static int has_changes(mmfile_t *one, mmfile_t *two,
 		       struct diff_options *o,
 		       regex_t *regexp, kwset_t kws)
 {
-	unsigned int c1 = one ? contains(one, regexp, kws) : 0;
-	unsigned int c2 = two ? contains(two, regexp, kws) : 0;
+	unsigned int c1 = one ? contains(one, regexp, kws, 0) : 0;
+	unsigned int c2 = two ? contains(two, regexp, kws, c1 + 1) : 0;
 	return c1 != c2;
 }
#95 lore raw

Re: [PATCH v2 19/22] xdiff-interface: allow early return from xdiff_emit_{line,hunk}_fn

In reply to Ævar Arnfjörð Bjarmason
Quoted 7 lines · Ævar Arnfjörð Bjarmason

Change the function prototype of xdiff_emit_{line,hunk}_fn to return an int instead of void. This will allow for returning early from hunk & diff consumers that want some of the data, but not all of it.

No behavior is being changed here, just replacing the equivalent of "return" with "return 0", nothing acts on the changed return values yet.

So, is this "allowing" early return yet? I am looking at the patch title and then reading the above paragraph and wondering.

Or is this step "preparation for allowing" and the real fun is left for later steps?

#98 lore raw

Re: [PATCH v2 21/22] pickaxe -G: terminate early on matching lines

In reply to Ævar Arnfjörð Bjarmason
Quoted 4 lines · Ævar Arnfjörð Bjarmason

Solve a long-standing item for "git log -Grx" of us e.g. finding "+ str" in the diff context and noting that we had a "hit", but xdiff diligently continuing to generate and spew the rest of the diff at us.

Nice. "git log -Gpattern" without "-p" has no reason to do the full diff once it finds out that pattern would have existed if "-p" were given in the output.

It actually is a bit of shame that "git log -Gpattern -p" still needs to run two diffs (instead of taking advantage of the diff that it needs to generate anyway (to show to the user of the "log") and pattern match in it).

diffcore-pickaxe.c static int diffgrep_consume(void *priv, char *line, unsigned long len) · line 30

Quoted 2 lines
diffcore-pickaxe.c +1 -0
 	if (data->hit)
+		BUG("Already matched in diffgrep_consume! Broken xdiff_emit_line_fn?");

Hmph, an obvious alternative would be to silently return -1 here, which probably would not hurt, either. I do not mind the check to be stricter, though.

Quoted 7 lines
diffcore-pickaxe.c +5 -0
+	if (!regexec_buf(data->regexp, line + 1, len - 1, 1,
+			 &regmatch, 0)) {
+		data->hit = 1;
+		return -1;
+	}
 	return 0;
 }
#100 lore raw

Re: [PATCH v2 22/22] pickaxe -G: don't special-case create/delete

In reply to Ævar Arnfjörð Bjarmason
Quoted 8 lines · Ævar Arnfjörð Bjarmason

Instead of special-casing creations and deletions let's just generate a diff for them.

This logic of not running a diff under -G if we don't have both sides dates back to the original implementation of -S in 52e9578985f ([PATCH] Introducing software archaeologist's tool "pickaxe"., 2005-05-21).

Quoted 4 lines

In the case of -S we were not working with the xdiff interface and needed to do this, but when -G was implemented in f506b8e8b5f (git log/diff: add -G<regexp> that greps in the patch text, 2010-08-23) this logic was diligently copied over.

Nicely analized. Yes, I agree that -G that special cases deletion and creation is just being silly, mimicking what -S did without thinking. I can imagine that running grep over diff output in normal case, and having to run grep over a single side in the edge cases, would require unnecessary code duplication.

#72 lore raw

Re: [PATCH v2 04/22] test-lib functions: add --printf option to test_commit

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

t/t1307-config-blob.sh test_expect_success 'parse errors in blobs are properly attributed' ' · line 68

Quoted 5 lines · Ævar Arnfjörð Bjarmason

test_expect_success 'can parse blob ending with CR' '

t/t1307-config-blob.sh +1 -3
-	printf "[some]key = value\\r" >config &&
-	git add config &&
-	git commit -m CR &&
+	test_commit --printf CR config "[some]key = value\\r" &&

OK, the first arg becomes the commit title, and the second one is used for the filename, and the next arg is the string given to printf, I guess.

Quoted 8 lines

t/t2030-unresolve-info.sh test_expect_success 'rerere and rerere forget (subdirectory)' ' · line 182

Quoted 5 lines

test_expect_success 'rerere forget (binary)' '

t/t2030-unresolve-info.sh +1 -2
 	git checkout -f side &&
-	printf "a\0c" >binary &&
-	git commit -a -m binary &&
+	test_commit binary binary "a\0c" &&

This lacks --printf. Are we breaking the test but "test-must-fail" is hiding the breakage here?

Yes, well spotted. FWIW in splitting this out into another series I fixed this bug in the re-roll:
https://lore.kernel.org/git/[email protected]/T/#ma9ef67d8198c203adc05aab44f87aa753a3df993

#44 lore raw

PATCH v3 1/22 grep/pcre2 tests: reword comments referring to kwset

In reply to Ævar Arnfjörð Bjarmason

The kwset optimization has not been used by grep since 48de2a768cf (grep: remove the kwset optimization, 2019-07-01).

---
 t/t7816-grep-binary-pattern.sh | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
t/t7816-grep-binary-pattern.sh +2 -2
index 60bab291e49..9d67a5fc4cf 100755
@@ -59,7 +59,7 @@ test_expect_success 'setup' "
 	git commit -m.
 "
 
-# Simple fixed-string matching that can use kwset (no -i && non-ASCII)
+# Simple fixed-string matching
 nul_match P P P '-F' 'yQf'
 nul_match P P P '-F' 'yQx'
 nul_match P P P '-Fi' 'YQf'
@@ -78,7 +78,7 @@ nul_match P P P '-Fi' '[Y]QF'
 nul_match P P P '-F' 'æQ[ð]'
 nul_match P P P '-F' '[æ]Qð'
 
-# The -F kwset codepath can't handle -i && non-ASCII...
+# Matching pattern and subject case with -i
 nul_match P 1 1 '-i' '[æ]Qð'
 
 # ...PCRE v2 only matches non-ASCII with -i casefolding under UTF-8
Signature · 1 line
2.31.1.639.g3d04783866f
#43 lore raw

v3 cover pickaxe: test and refactoring for future PCRE backend

In reply to Ævar Arnfjörð Bjarmason

This much-delayed re-roll of v2[2] is a test and refactoring change to diffcore-pickaxe.c to allow an eventual removal of the kwset.[ch] files and to add a PCRE backend.

This series is now based on my "test-lib.sh: new test_commit args, simplification & fixes" series. The trivial dependency between the two is using the new test_commit --printf option.

I'll not summarize the range-diff too much, but since v2 I addressed all outstanding feedback from Junio. There's 2x new patches at the end of the series to change the existing early return feature now using "discard_hunk_line" to use a flag.

In v2 I had what I realized was a WIP migration of that to using return values in the callback instead, but unlike the xdiff_emit_line_fn I don't think that makes sense in xdiff_emit_hunk_fn.

1. https://lore.kernel.org/git/[email protected]/
2. https://lore.kernel.org/git/[email protected]/

Ævar Arnfjörð Bjarmason (22):
grep/pcre2 tests: reword comments referring to kwset pickaxe tests: refactor to use test_commit --append --printf pickaxe tests: add test for diffgrep_consume() internals pickaxe tests: add test for "log -S" not being a regex pickaxe tests: test for -G, -S and --find-object incompatibility pickaxe tests: add missing test for --no-pickaxe-regex being an error
pickaxe: die when -G and --pickaxe-regex are combined
pickaxe: die when --find-object and --pickaxe-all are combined diff.h: move pickaxe fields together again
pickaxe/style: consolidate declarations and assignments
perf: add performance test for pickaxe
pickaxe: refactor function selection in diffcore-pickaxe()
pickaxe: assert that we must have a needle under -G or -S pickaxe -S: support content with NULs under --pickaxe-regex
pickaxe: rename variables in has_changes() for brevity pickaxe -S: slightly optimize contains()
xdiff-interface: prepare for allowing early return
xdiff-interface: allow early return from xdiff_emit_line_fn pickaxe -G: terminate early on matching lines
pickaxe -G: don't special-case create/delete
xdiff users: use designated initializers for out_line
xdiff-interface: replace discard_hunk_line() with a flag

 builtin/merge-tree.c           |   5 +-
 builtin/rerere.c               |   4 +-
 combine-diff.c                 |   5 +-
 diff.c                         |  39 +++++++----
 diff.h                         |   7 +-
 diffcore-pickaxe.c             | 106 ++++++++++++++++++------------
 range-diff.c                   |   3 +-
 t/perf/p4209-pickaxe.sh        |  70 ++++++++++++++++++++
 t/t4209-log-pickaxe.sh         | 114 ++++++++++++++++++++++++++++-----
 t/t7816-grep-binary-pattern.sh |   4 +-
 xdiff-interface.c              |  27 ++++----
 xdiff-interface.h              |  31 ++++++---
 xdiff/xdiff.h                  |   1 +
 xdiff/xemit.c                  |   3 +-
 14 files changed, 312 insertions(+), 107 deletions(-)
 create mode 100755 t/perf/p4209-pickaxe.sh

Range-diff against v2:

 1:  75bfc8eba13 =  1:  cfe934d6081 grep/pcre2 tests: reword comments referring to kwset
 2:  cddb1f75f6c <  -:  ----------- test-lib-functions: document and test test_commit --no-tag
 3:  44540f6039e <  -:  ----------- test-lib-functions: reword "test_commit --append" docs
 4:  0e1f133476f <  -:  ----------- test-lib functions: add --printf option to test_commit
 5:  2a814e8d53a !  2:  413a330d3d6 pickaxe tests: refactor to use test_commit --append --printf

@@ Metadata
## Commit message ##
pickaxe tests: refactor to use test_commit --append --printf

- Refactor existing tests added in e0e7cb8080c (log -G: ignore binary
- files, 2018-12-14) to use the --append option I added in
+ Refactor the existing tests added in e0e7cb8080c (log -G: ignore
+ binary files, 2018-12-14) to use the --append option I added in
3373518cc8b (test-lib functions: add an --append option to
- test_commit, 2021-01-12) and the --printf option added in a preceding
- commit.
+ test_commit, 2021-01-12) and the --printf option added as part of an
+ in-flight topic of mine this commit depends on.
+
+ While I'm at it change some of the setup of the test to use a more
+ sensible pattern, e.g. setting up a temporary repo instead of creating
+ an orphan branch.
+
+ Since the -G and -S options will behave the same way with truncated
+ and removed content also change the "git rm" to emptying data.bin,
+ that's just catering to how test_commit works. The resulting test is
+ shorter.

See also f5d79bf7dd6 (tests: refactor a few tests to use "test_commit --append", 2021-01-12) for prior similar refactoring.
@@ t/t4209-log-pickaxe.sh: test_expect_success 'log -S --no-textconv (missing textc
- git commit -m "delete binary file" data.bin &&
- git log >full-log
+ test_create_repo GS-bin-txt &&
-+ test_commit -C GS-bin-txt --append --printf A data.bin "a\na\0a\n" &&
++ test_commit -C GS-bin-txt --printf A data.bin "a\na\0a\n" &&
+ test_commit -C GS-bin-txt --append --printf B data.bin "a\na\0a\n" &&
+ test_commit -C GS-bin-txt C data.bin "" &&
+ git -C GS-bin-txt log >full-log
6: f49eb6c95e5 ! 3: ddd2224836b pickaxe tests: add test for diffgrep_consume() internals

    @@ t/t4209-log-pickaxe.sh: test_expect_success 'log -S --no-textconv (missing textc
     +
      test_expect_success 'setup log -[GS] binary & --text' '
      	test_create_repo GS-bin-txt &&
    - 	test_commit -C GS-bin-txt --append --printf A data.bin "a\na\0a\n" &&
    + 	test_commit -C GS-bin-txt --printf A data.bin "a\na\0a\n" &&
 7:  80c62fb0448 =  4:  ca6340c1fa7 pickaxe tests: add test for "log -S" not being a regex
 8:  6d329e0c3b1 !  5:  0c4657189a8 pickaxe tests: test for -G, -S and --find-object incompatibility
    @@ t/t4209-log-pickaxe.sh: test_expect_success setup '
      
     +test_expect_success 'usage' '
     +	test_expect_code 128 git log -Gregex -Sstring 2>err &&
    -+	test_i18ngrep "mutually exclusive" err &&
    ++	grep "mutually exclusive" err &&
     +
     +	test_expect_code 128 git log -Gregex --find-object=HEAD 2>err &&
    -+	test_i18ngrep "mutually exclusive" err &&
    ++	grep "mutually exclusive" err &&
     +
    -+	test_expect_code 128 git log -Gstring --find-object=HEAD 2>err &&
    -+	test_i18ngrep "mutually exclusive" err
    ++	test_expect_code 128 git log -Sstring --find-object=HEAD 2>err &&
    ++	grep "mutually exclusive" err
     +'
     +
      test_log	expect_initial	--grep initial
 -:  ----------- >  6:  1696076bb09 pickaxe tests: add missing test for --no-pickaxe-regex being an error
 9:  bd0c3b7e3b0 !  7:  83e7b4793b6 pickaxe: die when -G and --pickaxe-regex are combined

@@ Commit message
matching regular expressions, 2006-03-29) only the -S option existed. Then when -G was added in f506b8e8b5 (git log/diff: add -G<regexp> that greps in the patch text, 2010-08-23) neither the
- documentation for --pickaxe-regex was updater accordingly, nor was
+ documentation for --pickaxe-regex was updated accordingly, nor was
something like this assertion added.

Since 5bc3f0b567 (diffcore-pickaxe doc: document -S and -G properly, 2013-05-31) we've claimed that --pickaxe-regex should only be used
- with -S, but have silently toileted combining it with -G, let's die
+ with -S, but have silently tolerated combining it with -G, let's die
instead.

@@ diff.h: int git_config_rename(const char *var, const char *value);

## t/t4209-log-pickaxe.sh ##
@@ t/t4209-log-pickaxe.sh: test_expect_success 'usage' '
- test_expect_code 128 git log -Gregex -Sstring 2>err &&
- test_i18ngrep "mutually exclusive" err &&
+ grep "mutually exclusive" err
+ '

++test_expect_success 'usage: --pickaxe-regex' '
+ test_expect_code 128 git log -Gregex --pickaxe-regex 2>err &&
-+ test_i18ngrep "mutually exclusive" err &&
++ grep "mutually exclusive" err
++'
+
- test_expect_code 128 git log -Gregex --find-object=HEAD 2>err &&
- test_i18ngrep "mutually exclusive" err &&
-
+ test_expect_success 'usage: --no-pickaxe-regex' '
+ cat >expect <<-\EOF &&
+ fatal: unrecognized argument: --no-pickaxe-regex
10: 72075874f5c ! 8: 749c3ca3f98 pickaxe: die when --find-object and --pickaxe-all are combined

    @@ diff.h: int git_config_rename(const char *var, const char *value);
     
      ## t/t4209-log-pickaxe.sh ##
     @@ t/t4209-log-pickaxe.sh: test_expect_success 'usage' '
    - 	test_i18ngrep "mutually exclusive" err &&
    + 	grep "mutually exclusive" err &&
      
    - 	test_expect_code 128 git log -Gstring --find-object=HEAD 2>err &&
    -+	test_i18ngrep "mutually exclusive" err &&
    + 	test_expect_code 128 git log -Sstring --find-object=HEAD 2>err &&
    ++	grep "mutually exclusive" err &&
     +
     +	test_expect_code 128 git log --pickaxe-all --find-object=HEAD 2>err &&
    - 	test_i18ngrep "mutually exclusive" err
    + 	grep "mutually exclusive" err
      '
11:  f8116a2b814 =  9:  fe4e75c39d2 diff.h: move pickaxe fields together again
12:  4778357cfc7 = 10:  afe70b163a2 pickaxe/style: consolidate declarations and assignments
13:  7449a59b104 = 11:  97616d741c7 perf: add performance test for pickaxe
14:  160f7d8b0f2 ! 12:  c29deb428b1 pickaxe: refactor function selection in diffcore-pickaxe()
    @@ diffcore-pickaxe.c: void diffcore_pickaxe(struct diff_options *o)
      		regcomp_or_die(&regex, needle, cflags);
      		regexp = &regex;
     +
    -+		/* diff.c errors on -G and --pickaxe-regex for us */
     +		if (opts & DIFF_PICKAXE_KIND_G)
     +			fn = diff_grep;
     +		else if (opts & DIFF_PICKAXE_REGEX)
     +			fn = has_changes;
     +		else
    ++			/*
    ++			 * We don't need to check the combination of
    ++			 * -G and --pickaxe-regex, by the time we get
    ++			 * here diff.c has already died if they're
    ++			 * combined. See the usage tests in
    ++			 * t4209-log-pickaxe.sh.
    ++			 */
     +			BUG("unreachable");
      	} else if (opts & DIFF_PICKAXE_KIND_S) {
      		if (o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE &&
15:  05fc57e54e1 ! 13:  115a369d067 pickaxe: assert that we must have a needle under -G or -S
    @@ diffcore-pickaxe.c: void diffcore_pickaxe(struct diff_options *o)
      	kwset_t kws = NULL;
      	pickaxe_fn fn;
      
    -+	if (opts & ~DIFF_PICKAXE_KIND_OBJFIND && !needle)
    ++	if (opts & ~DIFF_PICKAXE_KIND_OBJFIND &&
    ++	    (!needle || !*needle))
     +		BUG("should have needle under -G or -S");
      	if (opts & (DIFF_PICKAXE_REGEX | DIFF_PICKAXE_KIND_G)) {
      		int cflags = REG_EXTENDED | REG_NEWLINE;
    @@ t/t4209-log-pickaxe.sh: test_expect_success setup '
     +	test_i18ngrep "switch.*requires a value" err &&
     +
      	test_expect_code 128 git log -Gregex -Sstring 2>err &&
    - 	test_i18ngrep "mutually exclusive" err &&
    + 	grep "mutually exclusive" err &&

16: 550620ec13b ! 14: a86032792b6 pickaxe -S: support content with NULs under --pickaxe-regex
@@ Commit message
for pickaxe matching regular expressions, 2006-03-29).

It wasn't needed anymore, and as the now-passing test shows, actively
- getting in our way.
+ getting in our way. Since we always require REG_STARTEND support we do
+ not need to stop at NULs. If we are dealing with a haystack with NUL
+ in it. The needle may be behind that NUL.

@@ t/t4209-log-pickaxe.sh: test_expect_success 'log -S looks into binary files' '
+ git -C GS-bin-txt log --pickaxe-regex -Sa >log &&
+ test_cmp log full-log &&
+
-+ git -C GS-bin-txt log --pickaxe-regex -S[a] >log &&
++ git -C GS-bin-txt log --pickaxe-regex -S"[a]" >log &&
+ test_cmp log full-log
+'
+

17:  985e077d561 = 15:  10f85edcff7 pickaxe: rename variables in has_changes() for brevity
18:  648e6e5f11b ! 16:  ed83c3add89 pickaxe -S: slightly optimize contains()

@@ Commit message
pre-image and post-image of a change. As soon as we know we had e.g. 1 before and 2 now we can stop, we don't need to keep counting past 2.

+ With this change a diff between A and B may have different performance
+ characteristics than between B and A. That's OK in this case, since
+ we'll emit the same output, and the effect is to make one of them
+ better.
+
+ I'm picking a check of "one" first on the assumption that it's a more
+ common case to have files grow over time than not.
+

## diffcore-pickaxe.c ##
19: b991660fbf5 ! 17: 62c306275c7 xdiff-interface: allow early return from xdiff_emit_{line,hunk}_fn
@@ Metadata
Author: Ævar Arnfjörð Bjarmason <[email protected]>

## Commit message ##
- xdiff-interface: allow early return from xdiff_emit_{line,hunk}_fn
+ xdiff-interface: prepare for allowing early return

- Change the function prototype of xdiff_emit_{line,hunk}_fn to return
- an int instead of void. This will allow for returning early from hunk
- & diff consumers that want some of the data, but not all of it.
+ Change the function prototype of xdiff_emit_line_fn to return an "int"
+ instead of "void". Change all of those functions to "return 0",
+ nothing checks those return values yet, and no behavior is being
+ changed.

- No behavior is being changed here, just replacing the equivalent of
- "return" with "return 0", nothing acts on the changed return values
- yet.
-
- There was some work in this area of xdiff-interface.[ch] recently with
- 3b40a090fd4 (diff: avoid generating unused hunk header lines,
- 2018-11-02) and 7c61e25fbf1 (diff: use hunk callback for word-diff,
- 2018-11-02).
-
- In combination those two changes allow us to not do any work on the
- hunks and diff at all, but didn't change the status quo with regards
- to consumers that e.g. want the diff lines, but might want to abort
- early.
-
- Whereas soon we can abort e.g. on the first "-line" of a 1000 line
- diff if that's all we needed.
-
- This interface is rather scary as noted in the comment to
- xdiff-interface.h being added here, but it will be useful for
- diffcore-pickaxe.c in a subsequent commit. A future change could
- e.g. add more exit codes, and hack xdl_emit_diff() and friends to
- ignore or skip things more selectively as a result.
+ In subsequent commits the interface will be changed to allow early
+ return via this new return value.

## combine-diff.c ##
-@@ combine-diff.c: struct combine_diff_state {
- struct sline *lost_bucket;
- };
-
--static void consume_hunk(void *state_, -+static int consume_hunk(void *state_,
- long ob, long on,
- long nb, long nn,
- const char *funcline, long funclen)
@@ combine-diff.c: static void consume_hunk(void *state_,
- state->sline[state->nb-1].p_lno =
- xcalloc(state->num_parent, sizeof(unsigned long));
state->sline[state->nb-1].p_lno[state->n] = state->ob;
-+
-+ return 0;
}

-static void consume_line(void *state_, char *line, unsigned long len)
@@ combine-diff.c: static void consume_line(void *state_, char *line, unsigned long
static void combine_diff(struct repository *r,

## diff.c ##
-@@ diff.c: static int color_words_output_graph_prefix(struct diff_words_data *diff_words)
- }
- }
-
--static void fn_out_diff_words_aux(void *priv,
-- long minus_first, long minus_len,
-- long plus_first, long plus_len,
-- const char *func, long funclen)
-+static int fn_out_diff_words_aux(void *priv,
-+ long minus_first, long minus_len,
-+ long plus_first, long plus_len,
-+ const char *func, long funclen)
- {
- struct diff_words_data *diff_words = priv;
- struct diff_words_style *style = diff_words->style;
-@@ diff.c: static void fn_out_diff_words_aux(void *priv,
-
- diff_words->current_plus = plus_end;
- diff_words->last_minus = minus_first;
-+
-+ return 0;
- }
-
- /* This function starts looking at *begin, and returns 0 iff a word was found. */ @@ diff.c: static void find_lno(const char *line, struct emit_callback *ecbdata) ecbdata->lno_in_postimage = strtol(p + 1, NULL, 10);
}
@@ diff.c: static void diffstat_consume(void *priv, char *line, unsigned long len)
}

const char mime_boundary_leader[] = "------------";
-@@ diff.c: static int is_conflict_marker(const char *line, int marker_size, unsigned long l
- return 1;
- }
-
--static void checkdiff_consume_hunk(void *priv,
-+static int checkdiff_consume_hunk(void *priv,
- long ob, long on, long nb, long nn,
- const char *func, long funclen)
-
- {
- struct checkdiff_t *data = priv;
+@@ diff.c: static void checkdiff_consume_hunk(void *priv,
data->lineno = nb - 1;
-+ return 0;
}

-static void checkdiff_consume(void *priv, char *line, unsigned long len)
@@ range-diff.c: static void find_exact_matches(struct string_list *a, struct strin
+ return 0;
}

--static void diffsize_hunk(void *data, long ob, long on, long nb, long nn,
-- const char *funcline, long funclen)
-+static int diffsize_hunk(void *data, long ob, long on, long nb, long nn,
-+ const char *funcline, long funclen)
- {
- diffsize_consume(data, NULL, 0);
-+ return 0;
- }
-
- static int diffsize(const char *a, const char *b)
+ static void diffsize_hunk(void *data, long ob, long on, long nb, long nn,

## xdiff-interface.c ##
@@ xdiff-interface.c: static int xdiff_out_hunk(void *priv_,
@@ xdiff-interface.c: static void consume_one(void *priv_, char *s, unsigned long s
}

static int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
-@@ xdiff-interface.c: int xdi_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t co
- return xdl_diff(&a, &b, xpp, xecfg, xecb);
- }
-
--void discard_hunk_line(void *priv,
-- long ob, long on, long nb, long nn,
-- const char *func, long funclen)
-+int discard_hunk_line(void *priv,
-+ long ob, long on, long nb, long nn,
-+ const char *func, long funclen)
- {
-+ return 0;
- }
-
- int xdi_diff_outf(mmfile_t *mf1, mmfile_t *mf2,

## xdiff-interface.h ##
@@
@@ xdiff-interface.h
#define MAX_XDIFF_SIZE (1024UL * 1024 * 1023)

-typedef void (*xdiff_emit_line_fn)(void *, char *, unsigned long);
--typedef void (*xdiff_emit_hunk_fn)(void *data,
-- long old_begin, long old_nr,
-- long new_begin, long new_nr,
-- const char *func, long funclen);
-+/*
-+ * The xdiff_emit_{line,hunk}_fn consumers can return -1 to abort
-+ * early, or 0 to continue processing. Note that doing so is an
-+ * all-or-nothing affair, as returning -1 will return all the way to
-+ * the top-level, e.g. the xdi_diff_outf() call to generate the diff.
-+ *
-+ * Thus returning -1 from a hunk header callback means you won't be
-+ * getting any more hunks, or diffs, and likewise returning from a
-+ * line callback means you won't be getting anymore lines.
-+ *
-+ * We may extend the interface in the future to understand other more
-+ * granular return values, but for now use it carefully, or consider
-+ * e.g. using discard_hunk_line() if you say just don't care about
-+ * hunk headers.
-+ */
+typedef int (*xdiff_emit_line_fn)(void *, char *, unsigned long);
-+typedef int (*xdiff_emit_hunk_fn)(void *data,
-+ long old_begin, long old_nr,
-+ long new_begin, long new_nr,
-+ const char *func, long funclen);
-
- int xdi_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t const *xecfg, xdemitcb_t *ecb);
- int xdi_diff_outf(mmfile_t *mf1, mmfile_t *mf2,
-@@ xdiff-interface.h: extern int git_xmerge_style;
- * Can be used as a no-op hunk_fn for xdi_diff_outf(), since a NULL
- * one just sends the hunk line to the line_fn callback).
- */
--void discard_hunk_line(void *priv,
-- long ob, long on, long nb, long nn,
-- const char *func, long funclen);
-+int discard_hunk_line(void *priv,
-+ long ob, long on, long nb, long nn,
-+ const char *func, long funclen);
-
- /*
- * Compare the strings l1 with l2 which are of size s1 and s2 respectively.
+ typedef void (*xdiff_emit_hunk_fn)(void *data,
+ long old_begin, long old_nr,
+ long new_begin, long new_nr,
20: 69b061832b3 ! 18: 76d667f152f xdiff-interface: support early exit in xdiff_outf()
@@ Metadata
Author: Ævar Arnfjörð Bjarmason <[email protected]>

## Commit message ##
- xdiff-interface: support early exit in xdiff_outf()
+ xdiff-interface: allow early return from xdiff_emit_line_fn

- Bridge the gap between the preceding "xdiff-interface: allow early
- return from xdiff_emit_{line,hunk}_fn" change and the public
- interface.
+ Finish the change started in the preceding commit and allow an early
+ return from "xdiff_emit_line_fn" callbacks, this will allows
+ diffcore-pickaxe.c to save itself redundant work.

- This change was split off from the preceding commit as it wasn't a
- purely mechanical addition of "return 0".
+ Our xdiff interface also had the limitation of not being able to abort
+ early since the beginning, see d9ea73e0564 (combine-diff: refactor
+ built-in xdiff interface., 2006-04-05). Although at that time
+ "xdiff_emit_line_fn" was called "xdiff_emit_consume_fn", and
+ "xdiff_emit_hunk_fn" didn't exist yet.

- Here we want to be able to abort early, but do so in a way that
- doesn't skip the appropriate strbuf_reset() invocations.
+ There was some work in this area of xdiff-interface.[ch] recently with
+ 3b40a090fd4 (diff: avoid generating unused hunk header lines,
+ 2018-11-02) and 7c61e25fbf1 (diff: use hunk callback for word-diff,
+ 2018-11-02).
+
+ In combination those two changes allow us to not do any work on the
+ hunks and diff at all, but didn't change the status quo with regards
+ to consumers that e.g. want the diff lines, but might want to abort
+ early.
+
+ Whereas now we can abort e.g. on the first "-line" of a 1000 line diff
+ if that's all we needed.
+
+ This interface is rather scary as noted in the comment to
+ xdiff-interface.h being added here, as noted there a future change
+ could add more exit codes, and hack xdl_emit_diff() and friends to
+ ignore or skip things more selectively as a result.
+
+ I did not see an inherent reason for why xdl_emit_{diffrec,record}()
+ could not be changed to ferry the "xdiff_emit_line_fn" error code
+ upwards instead of returning -1 on all "ret < 0".

- The use of -1 as a return value is consistent with the rest of the
- xdiff codebase, where doing so signals an abort or error that'll
- propagate up the stack.
+ But doing so would require corresponding changes in xdl_emit_diff(),
+ xdl_diff(). I didn't see any issue with narrowly doing that to
+ accomplish what I needed here, but it would leave xdiff's own return
+ values in an inconsistent state.
+
+ Instead I've left it at returning a more conventional (for git's own
+ codebase) 1 for an early return, and translating it (or rather, all
+ non-zero) to -1 for xdiff's consumption.
+
+ The reason for most of the "stop" complexity in xdiff_outf() is
+ because we want to be able to abort early, but do so in a way that
+ doesn't skip the appropriate strbuf_reset() invocations.

## xdiff-interface.c ##
@@ xdiff-interface.c: static int consume_one(void *priv_, char *s, unsigned long size)
+ char *ep;
+ while (size) {
unsigned long this_size;
++ int ret;
ep = memchr(s, '\n', size);
this_size = (ep == NULL) ? size : (ep - s + 1);
- priv->line_fn(priv->consume_callback_data, s, this_size);
-+ if (priv->line_fn(priv->consume_callback_data, s, this_size))
-+ return -1;
++ ret = priv->line_fn(priv->consume_callback_data, s, this_size);
++ if (ret)
++ return ret;
size -= this_size;
s += this_size;
}
@@ xdiff-interface.c: static int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)

for (i = 0; i < nbuf; i++) {
+ if (stop)
-+ return -1;
++ return 1;
if (mb[i].ptr[mb[i].size-1] != '\n') {
/* Incomplete line */ strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
@@ xdiff-interface.c: static int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
return 0;
}

+
+ ## xdiff-interface.h ##
+@@
+ */
+ #define MAX_XDIFF_SIZE (1024UL * 1024 * 1023)
+
++/**
++ * The xdiff_emit_line_fn function can return 1 to abort early, or 0 ++ * to continue processing. Note that doing so is an all-or-nothing ++ * affair, as returning 1 will return all the way to the top-level, ++ * e.g. the xdi_diff_outf() call to generate the diff.
++ *
++ * Thus returning 1 means you won't be getting any more diff lines. If ++ * you need something in-between those two options you'll to use ++ * xdl_emit_hunk_consume_func_t and implement your own version of ++ * xdl_emit_diff().
++ *
++ * We may extend the interface in the future to understand other more ++ * granular return values. While you should return 1 to exit early, ++ * doing so will currently make your early return indistinguishable ++ * from an error internal to xdiff, xdiff itself will see that ++ * non-zero return and translate it to -1.
++ */
+ typedef int (*xdiff_emit_line_fn)(void *, char *, unsigned long);
+ typedef void (*xdiff_emit_hunk_fn)(void *data,
+ long old_begin, long old_nr,
21: fc0aa61d093 ! 19: 53e9405f849 pickaxe -G: terminate early on matching lines
@@ Commit message

Solve a long-standing item for "git log -Grx" of us e.g. finding "+ str" in the diff context and noting that we had a "hit", but xdiff
- diligently continuing to generate and spew the rest of the diff at us.
+ diligently continuing to generate and spew the rest of the diff at
+ us. This makes use of a new "early return" xdiff interface added by
+ preceding commits.

- The TODO item has been there since "git log -G" was implemented. See
- f506b8e8b5f (git log/diff: add -G<regexp> that greps in the patch
- text, 2010-08-23).
+ The TODO item (or, the NEEDSWORK comment) has been there since "git
+ log -G" was implemented. See f506b8e8b5f (git log/diff: add -G<regexp>
+ that greps in the patch text, 2010-08-23).

- Our xdiff interface also had the limitation of not being able to abort
- early since the beginning, see d9ea73e0564 (combine-diff: refactor
- built-in xdiff interface., 2006-04-05). Although at that time
- "xdiff_emit_line_fn" was called "xdiff_emit_consume_fn", and
- "xdiff_emit_hunk_fn" didn't exist yet.
-
- But now with the support added in the preceding ""xdiff-interface:
- allow early return from xdiff_emit_{line,hunk}_fn" commit we can
- return early, and furthermore test the functionality of the new
- early-exit xdiff-interface by having a BUG() call here to die if it
- ever starts handing us needless work again.
+ But now with the support added in the preceding changes to the
+ xdiff-interface we can return early. Let's assert the behavior of that
+ new early-return xdiff-interface by having a BUG() call here to die if
+ it ever starts handing us needless work again.

@@ diffcore-pickaxe.c: static int diffgrep_consume(void *priv, char *line, unsigned
+ if (!regexec_buf(data->regexp, line + 1, len - 1, 1,
+ &regmatch, 0)) {
+ data->hit = 1;
-+ return -1;
++ return 1;
+ }
return 0;
}
@@ diffcore-pickaxe.c: static int diff_grep(mmfile_t *one, mmfile_t *two,
+
+ /*
+ * An xdiff error might be our "data->hit" from above. See the
-+ * comment for xdiff_emit_{line,hunk}_fn in xdiff-interface.h
-+ * for why.
++ * comment for xdiff_emit_line_fn in xdiff-interface.h
+ */
+ ret = xdi_diff_outf(one, two, discard_hunk_line, diffgrep_consume,
+ &ecbdata, &xpp, &xecfg);
@@ diffcore-pickaxe.c: static int diff_grep(mmfile_t *one, mmfile_t *two,

## xdiff-interface.h ##
@@
- * granular return values, but for now use it carefully, or consider
- * e.g. using discard_hunk_line() if you say just don't care about
- * hunk headers.
+ * doing so will currently make your early return indistinguishable
+ * from an error internal to xdiff, xdiff itself will see that
+ * non-zero return and translate it to -1.
+ *
-+ * Note that just returning -1 will make your early return -+ * indistinguishable from an error internal to xdiff. See "diff_grep" -+ * in diffcore-pickaxe.c for a trick to work around this, i.e. using -+ * the "consume_callback_data" to note the desired early return. ++ * See "diff_grep" in diffcore-pickaxe.c for a trick to work around ++ * this, i.e. using the "consume_callback_data" to note the desired ++ * early return.
*/
typedef int (*xdiff_emit_line_fn)(void *, char *, unsigned long);
- typedef int (*xdiff_emit_hunk_fn)(void *data,
+ typedef void (*xdiff_emit_hunk_fn)(void *data,

22:  c81b18ca4c7 = 20:  76de6ebc8b8 pickaxe -G: don't special-case create/delete
 -:  ----------- > 21:  9bb7ac910f3 xdiff users: use designated initializers for out_line
 -:  ----------- > 22:  1178956fb3d xdiff-interface: replace discard_hunk_line() with a flag
Signature · 1 line
2.31.1.639.g3d04783866f
#45 lore raw

PATCH v3 2/22 pickaxe tests: refactor to use test_commit --append --printf

Refactor the existing tests added in e0e7cb8080c (log -G: ignore binary files, 2018-12-14) to use the --append option I added in 3373518cc8b (test-lib functions: add an --append option to test_commit, 2021-01-12) and the --printf option added as part of an in-flight topic of mine this commit depends on.

While I'm at it change some of the setup of the test to use a more sensible pattern, e.g. setting up a temporary repo instead of creating an orphan branch.

Since the -G and -S options will behave the same way with truncated and removed content also change the "git rm" to emptying data.bin, that's just catering to how test_commit works. The resulting test is shorter.

See also f5d79bf7dd6 (tests: refactor a few tests to use "test_commit --append", 2021-01-12) for prior similar refactoring.

---
 t/t4209-log-pickaxe.sh | 30 ++++++++++++++----------------
 1 file changed, 14 insertions(+), 16 deletions(-)
t/t4209-log-pickaxe.sh +14 -16
index 5d06f5f45ea..ad45d8cfd0a 100755
@@ -107,37 +107,35 @@ test_expect_success 'log -S --no-textconv (missing textconv tool)' '
 '
 
 test_expect_success 'setup log -[GS] binary & --text' '
-	git checkout --orphan GS-binary-and-text &&
-	git read-tree --empty &&
-	printf "a\na\0a\n" >data.bin &&
-	git add data.bin &&
-	git commit -m "create binary file" data.bin &&
-	printf "a\na\0a\n" >>data.bin &&
-	git commit -m "modify binary file" data.bin &&
-	git rm data.bin &&
-	git commit -m "delete binary file" data.bin &&
-	git log >full-log
+	test_create_repo GS-bin-txt &&
+	test_commit -C GS-bin-txt --printf A data.bin "a\na\0a\n" &&
+	test_commit -C GS-bin-txt --append --printf B data.bin "a\na\0a\n" &&
+	test_commit -C GS-bin-txt C data.bin "" &&
+	git -C GS-bin-txt log >full-log
 '
 
 test_expect_success 'log -G ignores binary files' '
-	git log -Ga >log &&
+	git -C GS-bin-txt log -Ga >log &&
 	test_must_be_empty log
 '
 
 test_expect_success 'log -G looks into binary files with -a' '
-	git log -a -Ga >log &&
+	git -C GS-bin-txt log -a -Ga >log &&
 	test_cmp log full-log
 '
 
 test_expect_success 'log -G looks into binary files with textconv filter' '
-	test_when_finished "rm .gitattributes" &&
-	echo "* diff=bin" >.gitattributes &&
-	git -c diff.bin.textconv=cat log -Ga >log &&
+	test_when_finished "rm GS-bin-txt/.gitattributes" &&
+	(
+		cd GS-bin-txt &&
+		echo "* diff=bin" >.gitattributes &&
+		git -c diff.bin.textconv=cat log -Ga >../log
+	) &&
 	test_cmp log full-log
 '
 
 test_expect_success 'log -S looks into binary files' '
-	git log -Sa >log &&
+	git -C GS-bin-txt log -Sa >log &&
 	test_cmp log full-log
 '
 
Signature · 1 line
2.31.1.639.g3d04783866f
#46 lore raw

PATCH v3 3/22 pickaxe tests: add test for diffgrep_consume() internals

In reply to Ævar Arnfjörð Bjarmason

In diffgrep_consume() we generate a diff, and then advance past the "+" or "-" at the start of the line for matching. This has been done ever since the code was added in f506b8e8b5f (git log/diff: add -G<regexp> that greps in the patch text, 2010-08-23).

If we match "line" instead of "line + 1" no tests fail, i.e. we've got zero coverage for whether any of our searches match the beginning of the line or not. Let's add a test for this.

---
 t/t4209-log-pickaxe.sh | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
t/t4209-log-pickaxe.sh +15 -0
index ad45d8cfd0a..eacb9f0a1b5 100755
@@ -106,6 +106,21 @@ test_expect_success 'log -S --no-textconv (missing textconv tool)' '
 	rm .gitattributes
 '
 
+test_expect_success 'setup log -[GS] plain' '
+	test_create_repo GS-plain &&
+	test_commit -C GS-plain --append A data.txt "a" &&
+	test_commit -C GS-plain --append B data.txt "a a" &&
+	test_commit -C GS-plain C data.txt "" &&
+	git -C GS-plain log >full-log
+'
+
+test_expect_success 'log -G trims diff new/old [-+]' '
+	git -C GS-plain log -G"[+-]a" >log &&
+	test_must_be_empty log &&
+	git -C GS-plain log -G"^a" >log &&
+	test_cmp log full-log
+'
+
 test_expect_success 'setup log -[GS] binary & --text' '
 	test_create_repo GS-bin-txt &&
 	test_commit -C GS-bin-txt --printf A data.bin "a\na\0a\n" &&
Signature · 1 line
2.31.1.639.g3d04783866f
#47 lore raw

PATCH v3 4/22 pickaxe tests: add test for "log -S" not being a regex

In reply to Ævar Arnfjörð Bjarmason

No test in our test suite checked for "log -S<pat>" being a fixed string, as opposed to "log -S<pat> --pickaxe-regex". Let's test for it.

---
 t/t4209-log-pickaxe.sh | 30 +++++++++++++++++++++++++++---
 1 file changed, 27 insertions(+), 3 deletions(-)
t/t4209-log-pickaxe.sh +27 -3
index eacb9f0a1b5..9fa770b5fbd 100755
@@ -106,11 +106,18 @@ test_expect_success 'log -S --no-textconv (missing textconv tool)' '
 	rm .gitattributes
 '
 
-test_expect_success 'setup log -[GS] plain' '
+test_expect_success 'setup log -[GS] plain & regex' '
 	test_create_repo GS-plain &&
 	test_commit -C GS-plain --append A data.txt "a" &&
 	test_commit -C GS-plain --append B data.txt "a a" &&
-	test_commit -C GS-plain C data.txt "" &&
+	test_commit -C GS-plain --append C data.txt "b" &&
+	test_commit -C GS-plain --append D data.txt "[b]" &&
+	test_commit -C GS-plain E data.txt "" &&
+
+	# We also include E, the deletion commit
+	git -C GS-plain log --grep="[ABE]" >A-to-B-then-E-log &&
+	git -C GS-plain log --grep="[CDE]" >C-to-D-then-E-log &&
+	git -C GS-plain log --grep="[DE]" >D-then-E-log &&
 	git -C GS-plain log >full-log
 '
 
@@ -118,7 +125,24 @@ test_expect_success 'log -G trims diff new/old [-+]' '
 	git -C GS-plain log -G"[+-]a" >log &&
 	test_must_be_empty log &&
 	git -C GS-plain log -G"^a" >log &&
-	test_cmp log full-log
+	test_cmp log A-to-B-then-E-log
+'
+
+test_expect_success 'log -S<pat> is not a regex, but -S<pat> --pickaxe-regex is' '
+	git -C GS-plain log -S"a" >log &&
+	test_cmp log A-to-B-then-E-log &&
+
+	git -C GS-plain log -S"[a]" >log &&
+	test_must_be_empty log &&
+
+	git -C GS-plain log -S"[a]" --pickaxe-regex >log &&
+	test_cmp log A-to-B-then-E-log &&
+
+	git -C GS-plain log -S"[b]" >log &&
+	test_cmp log D-then-E-log &&
+
+	git -C GS-plain log -S"[b]" --pickaxe-regex >log &&
+	test_cmp log C-to-D-then-E-log
 '
 
 test_expect_success 'setup log -[GS] binary & --text' '
Signature · 1 line
2.31.1.639.g3d04783866f
#48 lore raw

PATCH v3 5/22 pickaxe tests: test for -G, -S and --find-object incompatibility

In reply to Ævar Arnfjörð Bjarmason

Add a test for the options sanity check added in 5e505257f2 (diff:
properly error out when combining multiple pickaxe options, 2018-01-04).

---
 t/t4209-log-pickaxe.sh | 11 +++++++++++
 1 file changed, 11 insertions(+)
t/t4209-log-pickaxe.sh +11 -0
index 9fa770b5fbd..21e22af1e7e 100755
@@ -55,6 +55,17 @@ test_expect_success setup '
 	git rev-parse --verify HEAD >expect_second
 '
 
+test_expect_success 'usage' '
+	test_expect_code 128 git log -Gregex -Sstring 2>err &&
+	grep "mutually exclusive" err &&
+
+	test_expect_code 128 git log -Gregex --find-object=HEAD 2>err &&
+	grep "mutually exclusive" err &&
+
+	test_expect_code 128 git log -Sstring --find-object=HEAD 2>err &&
+	grep "mutually exclusive" err
+'
+
 test_log	expect_initial	--grep initial
 test_log	expect_nomatch	--grep InItial
 test_log_icase	expect_initial	--grep InItial
Signature · 1 line
2.31.1.639.g3d04783866f
#49 lore raw

PATCH v3 6/22 pickaxe tests: add missing test for --no-pickaxe-regex being an error

In reply to Ævar Arnfjörð Bjarmason

Add a missing test for --no-pickaxe-regex. This has been an error ever since before the -S or -G options were added, or since 7ae0b0cb65f (git-log (internal): more options., 2006-03-01).

The reason for adding this test is that Junio suggested in [1] in response to a later test addition in this series that it might be good to support --no-pickaxe-regex in combination with -G. This would allow for fixed-string searching with -G, similr to grep's --fixed-strings mode.

I agree that that would make sense if anyone would like to implement it, but since it dies right now let's first add this test to assert the existing long-standing behavior. We can always add support for --[no-]pickaxe-regex in combination with -G at some later date.

1. http://lore.kernel.org/git/[email protected]

---
 t/t4209-log-pickaxe.sh | 12 ++++++++++++
 1 file changed, 12 insertions(+)
t/t4209-log-pickaxe.sh +12 -0
index 21e22af1e7e..532bb875f02 100755
@@ -66,6 +66,18 @@ test_expect_success 'usage' '
 	grep "mutually exclusive" err
 '
 
+test_expect_success 'usage: --no-pickaxe-regex' '
+	cat >expect <<-\EOF &&
+	fatal: unrecognized argument: --no-pickaxe-regex
+	EOF
+
+	test_expect_code 128 git log -Sstring --no-pickaxe-regex 2>actual &&
+	test_cmp expect actual &&
+
+	test_expect_code 128 git log -Gstring --no-pickaxe-regex 2>err &&
+	test_cmp expect actual
+'
+
 test_log	expect_initial	--grep initial
 test_log	expect_nomatch	--grep InItial
 test_log_icase	expect_initial	--grep InItial
Signature · 1 line
2.31.1.639.g3d04783866f
#50 lore raw

PATCH v3 7/22 pickaxe: die when -G and --pickaxe-regex are combined

In reply to Ævar Arnfjörð Bjarmason

When the -G and --pickaxe-regex options are combined we simply ignore the --pickaxe-regex option. Let's die instead as suggested by our documentation, since -G is always a regex.

When --pickaxe-regex was added in d01d8c6782 (Support for pickaxe matching regular expressions, 2006-03-29) only the -S option existed. Then when -G was added in f506b8e8b5 (git log/diff: add -G<regexp> that greps in the patch text, 2010-08-23) neither the documentation for --pickaxe-regex was updated accordingly, nor was something like this assertion added.

Since 5bc3f0b567 (diffcore-pickaxe doc: document -S and -G properly, 2013-05-31) we've claimed that --pickaxe-regex should only be used with -S, but have silently tolerated combining it with -G, let's die instead.

---
 diff.c                 | 3 +++
 diff.h                 | 2 ++
 t/t4209-log-pickaxe.sh | 5 +++++
 3 files changed, 10 insertions(+)
diff.c +3 -0
index 4acccd9d7ed..f9e86bca04e 100644
@@ -4628,6 +4628,9 @@ void diff_setup_done(struct diff_options *options)
 	if (HAS_MULTI_BITS(options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK))
 		die(_("-G, -S and --find-object are mutually exclusive"));
 
+	if (HAS_MULTI_BITS(options->pickaxe_opts & DIFF_PICKAXE_KINDS_G_REGEX_MASK))
+		die(_("-G and --pickaxe-regex are mutually exclusive, use --pickaxe-regex with -S"));
+
 	/*
 	 * Most of the time we can say "there are changes"
 	 * only by checking if there are changed paths, but
diff.h +2 -0
index c8f3faea8aa..5e110d349be 100644
@@ -556,6 +556,8 @@ int git_config_rename(const char *var, const char *value);
 #define DIFF_PICKAXE_KINDS_MASK (DIFF_PICKAXE_KIND_S | \
 				 DIFF_PICKAXE_KIND_G | \
 				 DIFF_PICKAXE_KIND_OBJFIND)
+#define DIFF_PICKAXE_KINDS_G_REGEX_MASK (DIFF_PICKAXE_KIND_G | \
+					 DIFF_PICKAXE_REGEX)
 
 #define DIFF_PICKAXE_IGNORE_CASE	32
 
t/t4209-log-pickaxe.sh +5 -0
index 532bb875f02..772c6c1a7c8 100755
@@ -66,6 +66,11 @@ test_expect_success 'usage' '
 	grep "mutually exclusive" err
 '
 
+test_expect_success 'usage: --pickaxe-regex' '
+	test_expect_code 128 git log -Gregex --pickaxe-regex 2>err &&
+	grep "mutually exclusive" err
+'
+
 test_expect_success 'usage: --no-pickaxe-regex' '
 	cat >expect <<-\EOF &&
 	fatal: unrecognized argument: --no-pickaxe-regex
Signature · 1 line
2.31.1.639.g3d04783866f
#51 lore raw

PATCH v3 8/22 pickaxe: die when --find-object and --pickaxe-all are combined

In reply to Ævar Arnfjörð Bjarmason

Neither the --pickaxe-all documentation nor --find-object's has ever suggested that you can combine the two. See f506b8e8b5 (git log/diff:
add -G<regexp> that greps in the patch text, 2010-08-23) and 15af58c1ad (diffcore: add a pickaxe option to find a specific blob, 2018-01-04).

But we've silently tolerated it, which makes the logic in diffcore_pickaxe() harder to reason about. Let's assert that we won't have the two combined.

---
 diff.c                 | 3 +++
 diff.h                 | 2 ++
 t/t4209-log-pickaxe.sh | 3 +++
 3 files changed, 8 insertions(+)
diff.c +3 -0
index f9e86bca04e..c1f47a7f013 100644
@@ -4631,6 +4631,9 @@ void diff_setup_done(struct diff_options *options)
 	if (HAS_MULTI_BITS(options->pickaxe_opts & DIFF_PICKAXE_KINDS_G_REGEX_MASK))
 		die(_("-G and --pickaxe-regex are mutually exclusive, use --pickaxe-regex with -S"));
 
+	if (HAS_MULTI_BITS(options->pickaxe_opts & DIFF_PICKAXE_KINDS_ALL_OBJFIND_MASK))
+		die(_("---pickaxe-all and --find-object are mutually exclusive, use --pickaxe-all with -G and -S"));
+
 	/*
 	 * Most of the time we can say "there are changes"
 	 * only by checking if there are changed paths, but
diff.h +2 -0
index 5e110d349be..82254396f95 100644
@@ -558,6 +558,8 @@ int git_config_rename(const char *var, const char *value);
 				 DIFF_PICKAXE_KIND_OBJFIND)
 #define DIFF_PICKAXE_KINDS_G_REGEX_MASK (DIFF_PICKAXE_KIND_G | \
 					 DIFF_PICKAXE_REGEX)
+#define DIFF_PICKAXE_KINDS_ALL_OBJFIND_MASK (DIFF_PICKAXE_ALL | \
+					     DIFF_PICKAXE_KIND_OBJFIND)
 
 #define DIFF_PICKAXE_IGNORE_CASE	32
 
t/t4209-log-pickaxe.sh +3 -0
index 772c6c1a7c8..16166ffd3e6 100755
@@ -63,6 +63,9 @@ test_expect_success 'usage' '
 	grep "mutually exclusive" err &&
 
 	test_expect_code 128 git log -Sstring --find-object=HEAD 2>err &&
+	grep "mutually exclusive" err &&
+
+	test_expect_code 128 git log --pickaxe-all --find-object=HEAD 2>err &&
 	grep "mutually exclusive" err
 '
 
Signature · 1 line
2.31.1.639.g3d04783866f
#52 lore raw

PATCH v3 9/22 diff.h: move pickaxe fields together again

In reply to Ævar Arnfjörð Bjarmason

Move the pickaxe and pickaxe_opts fields next to each other again. In a past life they'd been on adjacent lines, but when they got moved from a global variable to the diff_options struct in 6b5ee137e5 (Diff clean-up., 2005-09-21) they got split apart.

That split made sense at the time, the "char*" and "int" (flags) options were being grouped, but we've long since abandoned that pattern in the diff_options struct, and now it makes more sense to group these together again.

---
 diff.h | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
diff.h +1 -2
index 82254396f95..8ba85c5e605 100644
@@ -265,6 +265,7 @@ struct diff_options {
 	 * postimage of the diff_queue.
 	 */
 	const char *pickaxe;
+	unsigned pickaxe_opts;
 
 	/* -I<regex> */
 	regex_t **ignore_regex;
@@ -304,8 +305,6 @@ struct diff_options {
 	/* The output format used when `diff_flush()` is run. */
 	int output_format;
 
-	unsigned pickaxe_opts;
-
 	/* Affects the way detection logic for complete rewrites, renames and
 	 * copies.
 	 */
Signature · 1 line
2.31.1.639.g3d04783866f
#53 lore raw

PATCH v3 10/22 pickaxe/style: consolidate declarations and assignments

In reply to Ævar Arnfjörð Bjarmason

Refactor contains() to do its assignments at the same time that it does its declarations.

This code could have been refactored in ef90ab66e8e (pickaxe: use textconv for -S counting, 2012-10-28) when a function call between the declarations and assignments was removed.

---
 diffcore-pickaxe.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
diffcore-pickaxe.c +3 -7
index a9c6d60df22..a278b9b71d9 100644
@@ -70,13 +70,9 @@ static int diff_grep(mmfile_t *one, mmfile_t *two,
 
 static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws)
 {
-	unsigned int cnt;
-	unsigned long sz;
-	const char *data;
-
-	sz = mf->size;
-	data = mf->ptr;
-	cnt = 0;
+	unsigned int cnt = 0;
+	unsigned long sz = mf->size;
+	const char *data = mf->ptr;
 
 	if (regexp) {
 		regmatch_t regmatch;
Signature · 1 line
2.31.1.639.g3d04783866f
#54 lore raw

PATCH v3 11/22 perf: add performance test for pickaxe

In reply to Ævar Arnfjörð Bjarmason

Add a test for the -G and -S pickaxe options and related options.

This test supports being run with GIT_TEST_LONG=1 to adjust the limit on the number of commits from 1k to 10k. The 1k limit seems to hit a good spot on git.git

---
 t/perf/p4209-pickaxe.sh | 70 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 70 insertions(+)
 create mode 100755 t/perf/p4209-pickaxe.sh
t/perf/p4209-pickaxe.sh new file +70 -0
new file mode 100755
index 00000000000..f585a4465ae
@@ -0,0 +1,70 @@
+#!/bin/sh
+
+test_description="Test pickaxe performance"
+
+. ./perf-lib.sh
+
+test_perf_default_repo
+
+# Not --max-count, as that's the number of matching commit, so it's
+# unbounded. We want to limit our revision walk here.
+from_rev_desc=
+from_rev=
+max_count=1000
+if test_have_prereq EXPENSIVE
+then
+	max_count=10000
+fi
+from_rev=" $(git rev-list HEAD | head -n $max_count | tail -n 1).."
+from_rev_desc=" <limit-rev>.."
+
+for icase in \
+	'' \
+	'-i '
+do
+	# -S (no regex)
+	for pattern in \
+		'int main' \
+		'æ'
+	do
+		for opts in \
+			'-S'
+		do
+			test_perf "git log $icase$opts'$pattern'$from_rev_desc" "
+				git log --pretty=format:%H $icase$opts'$pattern'$from_rev
+			"
+		done
+	done
+
+	# -S (regex)
+	for pattern in  \
+		'(int|void|null)' \
+		'if *\([^ ]+ & ' \
+		'[àáâãäåæñøùúûüýþ]'
+	do
+		for opts in \
+			'--pickaxe-regex -S'
+		do
+			test_perf "git log $icase$opts'$pattern'$from_rev_desc" "
+				git log --pretty=format:%H $icase$opts'$pattern'$from_rev
+			"
+		done
+	done
+
+	# -G
+	for pattern in  \
+		'(int|void|null)' \
+		'if *\([^ ]+ & ' \
+		'[àáâãäåæñøùúûüýþ]'
+	do
+		for opts in \
+			'-G'
+		do
+			test_perf "git log $icase$opts'$pattern'$from_rev_desc" "
+				git log --pretty=format:%H $icase$opts'$pattern'$from_rev
+			"
+		done
+	done
+done
+
+test_done
Signature · 1 line
2.31.1.639.g3d04783866f
#55 lore raw

PATCH v3 12/22 pickaxe: refactor function selection in diffcore-pickaxe()

In reply to Ævar Arnfjörð Bjarmason

It's hard to read this codepath at a glance and reason about exactly what combination of -G and -S will compile either regexes or kwset, and whether we'll then dispatch to "diff_grep" or "has_changes".

Then in the "--find-object" case we aren't using the callback function, but were previously passing down "has_changes".

Refactor this code to exhaustively check "opts", it's now more obvious what callback function (or none) we want under what mode.

---
 diffcore-pickaxe.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
diffcore-pickaxe.c +21 -2
index a278b9b71d9..953b6ec1b4a 100644
@@ -228,6 +228,7 @@ void diffcore_pickaxe(struct diff_options *o)
 	int opts = o->pickaxe_opts;
 	regex_t regex, *regexp = NULL;
 	kwset_t kws = NULL;
+	pickaxe_fn fn;
 
 	if (opts & (DIFF_PICKAXE_REGEX | DIFF_PICKAXE_KIND_G)) {
 		int cflags = REG_EXTENDED | REG_NEWLINE;
@@ -235,6 +236,20 @@ void diffcore_pickaxe(struct diff_options *o)
 			cflags |= REG_ICASE;
 		regcomp_or_die(&regex, needle, cflags);
 		regexp = &regex;
+
+		if (opts & DIFF_PICKAXE_KIND_G)
+			fn = diff_grep;
+		else if (opts & DIFF_PICKAXE_REGEX)
+			fn = has_changes;
+		else
+			/*
+			 * We don't need to check the combination of
+			 * -G and --pickaxe-regex, by the time we get
+			 * here diff.c has already died if they're
+			 * combined. See the usage tests in
+			 * t4209-log-pickaxe.sh.
+			 */
+			BUG("unreachable");
 	} else if (opts & DIFF_PICKAXE_KIND_S) {
 		if (o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE &&
 		    has_non_ascii(needle)) {
@@ -251,10 +266,14 @@ void diffcore_pickaxe(struct diff_options *o)
 			kwsincr(kws, needle, strlen(needle));
 			kwsprep(kws);
 		}
+		fn = has_changes;
+	} else if (opts & DIFF_PICKAXE_KIND_OBJFIND) {
+		fn = NULL;
+	} else {
+		BUG("unknown pickaxe_opts flag");
 	}
 
-	pickaxe(&diff_queued_diff, o, regexp, kws,
-		(opts & DIFF_PICKAXE_KIND_G) ? diff_grep : has_changes);
+	pickaxe(&diff_queued_diff, o, regexp, kws, fn);
 
 	if (regexp)
 		regfree(regexp);
Signature · 1 line
2.31.1.639.g3d04783866f
#56 lore raw

PATCH v3 13/22 pickaxe: assert that we must have a needle under -G or -S

In reply to Ævar Arnfjörð Bjarmason

Assert early in diffcore_pickaxe() that we've got a needle to work with under -G and -S.

This code is redundant to the check -G and -S get from parse-options.c's get_arg(), which I'm adding a test for.

This check dates back to e1b161161d (diffcore-pickaxe: fix infinite loop on zero-length needle, 2007-01-25) when "git log -S" could send this code into an infinite loop.

It was then later refactored in 8fa4b09fb1 (pickaxe: hoist empty needle check, 2012-10-28) into its current form, but it seemingly wasn't noticed that in the meantime a move to the parse-options.c API in dea007fb4c (diff: parse separate options like -S foo, 2010-08-05) had made it redundant.

Let's retain some of the paranoia here with a BUG(), but there's no need to be checking this in the pickaxe_match() inner loop.

---
 diffcore-pickaxe.c     | 6 +++---
 t/t4209-log-pickaxe.sh | 6 ++++++
 2 files changed, 9 insertions(+), 3 deletions(-)
diffcore-pickaxe.c +3 -3
index 953b6ec1b4a..88b6ca840f6 100644
@@ -132,9 +132,6 @@ static int pickaxe_match(struct diff_filepair *p, struct diff_options *o,
 			 oidset_contains(o->objfind, &p->two->oid));
 	}
 
-	if (!o->pickaxe[0])
-		return 0;
-
 	if (o->flags.allow_textconv) {
 		textconv_one = get_textconv(o->repo, p->one);
 		textconv_two = get_textconv(o->repo, p->two);
@@ -230,6 +227,9 @@ void diffcore_pickaxe(struct diff_options *o)
 	kwset_t kws = NULL;
 	pickaxe_fn fn;
 
+	if (opts & ~DIFF_PICKAXE_KIND_OBJFIND &&
+	    (!needle || !*needle))
+		BUG("should have needle under -G or -S");
 	if (opts & (DIFF_PICKAXE_REGEX | DIFF_PICKAXE_KIND_G)) {
 		int cflags = REG_EXTENDED | REG_NEWLINE;
 		if (o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE)
t/t4209-log-pickaxe.sh +6 -0
index 16166ffd3e6..3f9aad0fdb0 100755
@@ -56,6 +56,12 @@ test_expect_success setup '
 '
 
 test_expect_success 'usage' '
+	test_expect_code 129 git log -S 2>err &&
+	test_i18ngrep "switch.*requires a value" err &&
+
+	test_expect_code 129 git log -G 2>err &&
+	test_i18ngrep "switch.*requires a value" err &&
+
 	test_expect_code 128 git log -Gregex -Sstring 2>err &&
 	grep "mutually exclusive" err &&
 
Signature · 1 line
2.31.1.639.g3d04783866f
#57 lore raw

PATCH v3 14/22 pickaxe -S: support content with NULs under --pickaxe-regex

In reply to Ævar Arnfjörð Bjarmason

Fix a bug in the matching routine powering -S<rx> --pickaxe-regex so that we won't abort early on content that has NULs in it.

We've had a hard requirement on REG_STARTEND since 2f8952250a8 (regex:
add regexec_buf() that can work on a non NUL-terminated string, 2016-09-21), but this sanity check dates back to d01d8c67828 (Support for pickaxe matching regular expressions, 2006-03-29).

It wasn't needed anymore, and as the now-passing test shows, actively getting in our way. Since we always require REG_STARTEND support we do not need to stop at NULs. If we are dealing with a haystack with NUL in it. The needle may be behind that NUL.

---
 diffcore-pickaxe.c     | 4 ++--
 t/t4209-log-pickaxe.sh | 8 ++++++++
 2 files changed, 10 insertions(+), 2 deletions(-)
diffcore-pickaxe.c +2 -2
index 88b6ca840f6..be0dd683b63 100644
@@ -78,12 +78,12 @@ static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws)
 		regmatch_t regmatch;
 		int flags = 0;
 
-		while (sz && *data &&
+		while (sz &&
 		       !regexec_buf(regexp, data, sz, 1, &regmatch, flags)) {
 			flags |= REG_NOTBOL;
 			data += regmatch.rm_eo;
 			sz -= regmatch.rm_eo;
-			if (sz && *data && regmatch.rm_so == regmatch.rm_eo) {
+			if (sz && regmatch.rm_so == regmatch.rm_eo) {
 				data++;
 				sz--;
 			}
t/t4209-log-pickaxe.sh +8 -0
index 3f9aad0fdb0..75795d0b492 100755
@@ -215,4 +215,12 @@ test_expect_success 'log -S looks into binary files' '
 	test_cmp log full-log
 '
 
+test_expect_success 'log -S --pickaxe-regex looks into binary files' '
+	git -C GS-bin-txt log --pickaxe-regex -Sa >log &&
+	test_cmp log full-log &&
+
+	git -C GS-bin-txt log --pickaxe-regex -S"[a]" >log &&
+	test_cmp log full-log
+'
+
 test_done
Signature · 1 line
2.31.1.639.g3d04783866f
#58 lore raw

PATCH v3 15/22 pickaxe: rename variables in has_changes() for brevity

In reply to Ævar Arnfjörð Bjarmason

Rename the {one,two}_contains variables to c{1,2}. This will make a follow-up change easier to read.

---
 diffcore-pickaxe.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
diffcore-pickaxe.c +3 -3
index be0dd683b63..23362a23597 100644
@@ -108,9 +108,9 @@ static int has_changes(mmfile_t *one, mmfile_t *two,
 		       struct diff_options *o,
 		       regex_t *regexp, kwset_t kws)
 {
-	unsigned int one_contains = one ? contains(one, regexp, kws) : 0;
-	unsigned int two_contains = two ? contains(two, regexp, kws) : 0;
-	return one_contains != two_contains;
+	unsigned int c1 = one ? contains(one, regexp, kws) : 0;
+	unsigned int c2 = two ? contains(two, regexp, kws) : 0;
+	return c1 != c2;
 }
 
 static int pickaxe_match(struct diff_filepair *p, struct diff_options *o,
Signature · 1 line
2.31.1.639.g3d04783866f
#59 lore raw

PATCH v3 16/22 pickaxe -S: slightly optimize contains()

In reply to Ævar Arnfjörð Bjarmason

When the "log -S<pat>" switch counts occurrences of <pat> on the pre-image and post-image of a change. As soon as we know we had e.g. 1 before and 2 now we can stop, we don't need to keep counting past 2.

With this change a diff between A and B may have different performance characteristics than between B and A. That's OK in this case, since we'll emit the same output, and the effect is to make one of them better.

I'm picking a check of "one" first on the assumption that it's a more common case to have files grow over time than not.

---
 diffcore-pickaxe.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
diffcore-pickaxe.c +10 -3
index 23362a23597..b7494fdf89c 100644
@@ -68,7 +68,8 @@ static int diff_grep(mmfile_t *one, mmfile_t *two,
 	return ecbdata.hit;
 }
 
-static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws)
+static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws,
+			     unsigned int limit)
 {
 	unsigned int cnt = 0;
 	unsigned long sz = mf->size;
@@ -88,6 +89,9 @@ static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws)
 				sz--;
 			}
 			cnt++;
+
+			if (limit && cnt == limit)
+				return cnt;
 		}
 
 	} else { /* Classic exact string match */
@@ -99,6 +103,9 @@ static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws)
 			sz -= offset + kwsm.size[0];
 			data += offset + kwsm.size[0];
 			cnt++;
+
+			if (limit && cnt == limit)
+				return cnt;
 		}
 	}
 	return cnt;
@@ -108,8 +115,8 @@ static int has_changes(mmfile_t *one, mmfile_t *two,
 		       struct diff_options *o,
 		       regex_t *regexp, kwset_t kws)
 {
-	unsigned int c1 = one ? contains(one, regexp, kws) : 0;
-	unsigned int c2 = two ? contains(two, regexp, kws) : 0;
+	unsigned int c1 = one ? contains(one, regexp, kws, 0) : 0;
+	unsigned int c2 = two ? contains(two, regexp, kws, c1 + 1) : 0;
 	return c1 != c2;
 }
 
Signature · 1 line
2.31.1.639.g3d04783866f
#60 lore raw

PATCH v3 17/22 xdiff-interface: prepare for allowing early return

In reply to Ævar Arnfjörð Bjarmason

Change the function prototype of xdiff_emit_line_fn to return an "int" instead of "void". Change all of those functions to "return 0", nothing checks those return values yet, and no behavior is being changed.

In subsequent commits the interface will be changed to allow early return via this new return value.

---
 combine-diff.c     |  5 +++--
 diff.c             | 26 +++++++++++++++-----------
 diffcore-pickaxe.c |  7 ++++---
 range-diff.c       |  3 ++-
 xdiff-interface.c  |  3 ++-
 xdiff-interface.h  |  2 +-
 6 files changed, 27 insertions(+), 19 deletions(-)
combine-diff.c +3 -2
index 06635f91bc2..a12d3bc0d9c 100644
@@ -403,11 +403,11 @@ static void consume_hunk(void *state_,
 	state->sline[state->nb-1].p_lno[state->n] = state->ob;
 }
 
-static void consume_line(void *state_, char *line, unsigned long len)
+static int consume_line(void *state_, char *line, unsigned long len)
 {
 	struct combine_diff_state *state = state_;
 	if (!state->lost_bucket)
-		return; /* not in any hunk yet */
+		return 0; /* not in any hunk yet */
 	switch (line[0]) {
 	case '-':
 		append_lost(state->lost_bucket, state->n, line+1, len-1);
@@ -417,6 +417,7 @@ static void consume_line(void *state_, char *line, unsigned long len)
 		state->lno++;
 		break;
 	}
+	return 0;
 }
 
 static void combine_diff(struct repository *r,
diff.c +15 -11
index c1f47a7f013..7a03c581c79 100644
@@ -2336,7 +2336,7 @@ static void find_lno(const char *line, struct emit_callback *ecbdata)
 	ecbdata->lno_in_postimage = strtol(p + 1, NULL, 10);
 }
 
-static void fn_out_consume(void *priv, char *line, unsigned long len)
+static int fn_out_consume(void *priv, char *line, unsigned long len)
 {
 	struct emit_callback *ecbdata = priv;
 	struct diff_options *o = ecbdata->opt;
@@ -2372,7 +2372,7 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
 		len = sane_truncate_line(line, len);
 		find_lno(line, ecbdata);
 		emit_hunk_header(ecbdata, line, len);
-		return;
+		return 0;
 	}
 
 	if (ecbdata->diff_words) {
@@ -2382,11 +2382,11 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
 		if (line[0] == '-') {
 			diff_words_append(line, len,
 					  &ecbdata->diff_words->minus);
-			return;
+			return 0;
 		} else if (line[0] == '+') {
 			diff_words_append(line, len,
 					  &ecbdata->diff_words->plus);
-			return;
+			return 0;
 		} else if (starts_with(line, "\\ ")) {
 			/*
 			 * Eat the "no newline at eof" marker as if we
@@ -2395,11 +2395,11 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
 			 * defer processing. If this is the end of
 			 * preimage, more "+" lines may come after it.
 			 */
-			return;
+			return 0;
 		}
 		diff_words_flush(ecbdata);
 		emit_diff_symbol(o, s, line, len, 0);
-		return;
+		return 0;
 	}
 
 	switch (line[0]) {
@@ -2423,6 +2423,7 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
 				 line, len, 0);
 		break;
 	}
+	return 0;
 }
 
 static void pprint_rename(struct strbuf *name, const char *a, const char *b)
@@ -2522,7 +2523,7 @@ static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
 	return x;
 }
 
-static void diffstat_consume(void *priv, char *line, unsigned long len)
+static int diffstat_consume(void *priv, char *line, unsigned long len)
 {
 	struct diffstat_t *diffstat = priv;
 	struct diffstat_file *x = diffstat->files[diffstat->nr - 1];
@@ -2531,6 +2532,7 @@ static void diffstat_consume(void *priv, char *line, unsigned long len)
 		x->added++;
 	else if (line[0] == '-')
 		x->deleted++;
+	return 0;
 }
 
 const char mime_boundary_leader[] = "------------";
@@ -3208,7 +3210,7 @@ static void checkdiff_consume_hunk(void *priv,
 	data->lineno = nb - 1;
 }
 
-static void checkdiff_consume(void *priv, char *line, unsigned long len)
+static int checkdiff_consume(void *priv, char *line, unsigned long len)
 {
 	struct checkdiff_t *data = priv;
 	int marker_size = data->conflict_marker_size;
@@ -3232,7 +3234,7 @@ static void checkdiff_consume(void *priv, char *line, unsigned long len)
 		}
 		bad = ws_check(line + 1, len - 1, data->ws_rule);
 		if (!bad)
-			return;
+			return 0;
 		data->status |= bad;
 		err = whitespace_error_string(bad);
 		fprintf(data->o->file, "%s%s:%d: %s.\n",
@@ -3244,6 +3246,7 @@ static void checkdiff_consume(void *priv, char *line, unsigned long len)
 	} else if (line[0] == ' ') {
 		data->lineno++;
 	}
+	return 0;
 }
 
 static unsigned char *deflate_it(char *data,
@@ -6121,17 +6124,18 @@ void flush_one_hunk(struct object_id *result, git_hash_ctx *ctx)
 	}
 }
 
-static void patch_id_consume(void *priv, char *line, unsigned long len)
+static int patch_id_consume(void *priv, char *line, unsigned long len)
 {
 	struct patch_id_t *data = priv;
 	int new_len;
 
 	if (len > 12 && starts_with(line, "\\ "))
-		return;
+		return 0;
 	new_len = remove_space(line, len);
 
 	the_hash_algo->update_fn(data->ctx, line, new_len);
 	data->patchlen += new_len;
+	return 0;
 }
 
 static void patch_id_add_string(git_hash_ctx *ctx, const char *str)
diffcore-pickaxe.c +4 -3
index b7494fdf89c..27aa20be350 100644
@@ -19,21 +19,22 @@ struct diffgrep_cb {
 	int hit;
 };
 
-static void diffgrep_consume(void *priv, char *line, unsigned long len)
+static int diffgrep_consume(void *priv, char *line, unsigned long len)
 {
 	struct diffgrep_cb *data = priv;
 	regmatch_t regmatch;
 
 	if (line[0] != '+' && line[0] != '-')
-		return;
+		return 0;
 	if (data->hit)
 		/*
 		 * NEEDSWORK: we should have a way to terminate the
 		 * caller early.
 		 */
-		return;
+		return 0;
 	data->hit = !regexec_buf(data->regexp, line + 1, len - 1, 1,
 				 &regmatch, 0);
+	return 0;
 }
 
 static int diff_grep(mmfile_t *one, mmfile_t *two,
range-diff.c +2 -1
index 116fb0735c6..83c90f946ea 100644
@@ -274,9 +274,10 @@ static void find_exact_matches(struct string_list *a, struct string_list *b)
 	hashmap_clear(&map);
 }
 
-static void diffsize_consume(void *data, char *line, unsigned long len)
+static int diffsize_consume(void *data, char *line, unsigned long len)
 {
 	(*(int *)data)++;
+	return 0;
 }
 
 static void diffsize_hunk(void *data, long ob, long on, long nb, long nn,
xdiff-interface.c +2 -1
index 4d20069302b..5d8c8c67dc2 100644
@@ -31,7 +31,7 @@ static int xdiff_out_hunk(void *priv_,
 	return 0;
 }
 
-static void consume_one(void *priv_, char *s, unsigned long size)
+static int consume_one(void *priv_, char *s, unsigned long size)
 {
 	struct xdiff_emit_state *priv = priv_;
 	char *ep;
@@ -43,6 +43,7 @@ static void consume_one(void *priv_, char *s, unsigned long size)
 		size -= this_size;
 		s += this_size;
 	}
+	return 0;
 }
 
 static int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
xdiff-interface.h +1 -1
index 93df26900c2..0198f9632f5 100644
@@ -11,7 +11,7 @@
  */
 #define MAX_XDIFF_SIZE (1024UL * 1024 * 1023)
 
-typedef void (*xdiff_emit_line_fn)(void *, char *, unsigned long);
+typedef int (*xdiff_emit_line_fn)(void *, char *, unsigned long);
 typedef void (*xdiff_emit_hunk_fn)(void *data,
 				   long old_begin, long old_nr,
 				   long new_begin, long new_nr,
Signature · 1 line
2.31.1.639.g3d04783866f
#61 lore raw

PATCH v3 18/22 xdiff-interface: allow early return from xdiff_emit_line_fn

In reply to Ævar Arnfjörð Bjarmason

Finish the change started in the preceding commit and allow an early return from "xdiff_emit_line_fn" callbacks, this will allows diffcore-pickaxe.c to save itself redundant work.

Our xdiff interface also had the limitation of not being able to abort early since the beginning, see d9ea73e0564 (combine-diff: refactor built-in xdiff interface., 2006-04-05). Although at that time "xdiff_emit_line_fn" was called "xdiff_emit_consume_fn", and "xdiff_emit_hunk_fn" didn't exist yet.

There was some work in this area of xdiff-interface.[ch] recently with 3b40a090fd4 (diff: avoid generating unused hunk header lines, 2018-11-02) and 7c61e25fbf1 (diff: use hunk callback for word-diff, 2018-11-02).

In combination those two changes allow us to not do any work on the hunks and diff at all, but didn't change the status quo with regards to consumers that e.g. want the diff lines, but might want to abort early.

Whereas now we can abort e.g. on the first "-line" of a 1000 line diff if that's all we needed.

This interface is rather scary as noted in the comment to xdiff-interface.h being added here, as noted there a future change could add more exit codes, and hack xdl_emit_diff() and friends to ignore or skip things more selectively as a result.

I did not see an inherent reason for why xdl_emit_{diffrec,record}() could not be changed to ferry the "xdiff_emit_line_fn" error code upwards instead of returning -1 on all "ret < 0".

But doing so would require corresponding changes in xdl_emit_diff(), xdl_diff(). I didn't see any issue with narrowly doing that to accomplish what I needed here, but it would leave xdiff's own return values in an inconsistent state.

Instead I've left it at returning a more conventional (for git's own codebase) 1 for an early return, and translating it (or rather, all non-zero) to -1 for xdiff's consumption.

The reason for most of the "stop" complexity in xdiff_outf() is because we want to be able to abort early, but do so in a way that doesn't skip the appropriate strbuf_reset() invocations.

---
 xdiff-interface.c | 18 ++++++++++++++----
 xdiff-interface.h | 17 +++++++++++++++++
 2 files changed, 31 insertions(+), 4 deletions(-)
xdiff-interface.c +14 -4
index 5d8c8c67dc2..50c0ef759dd 100644
@@ -37,9 +37,12 @@ static int consume_one(void *priv_, char *s, unsigned long size)
 	char *ep;
 	while (size) {
 		unsigned long this_size;
+		int ret;
 		ep = memchr(s, '\n', size);
 		this_size = (ep == NULL) ? size : (ep - s + 1);
-		priv->line_fn(priv->consume_callback_data, s, this_size);
+		ret = priv->line_fn(priv->consume_callback_data, s, this_size);
+		if (ret)
+			return ret;
 		size -= this_size;
 		s += this_size;
 	}
@@ -50,11 +53,14 @@ static int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
 {
 	struct xdiff_emit_state *priv = priv_;
 	int i;
+	int stop = 0;
 
 	if (!priv->line_fn)
 		return 0;
 
 	for (i = 0; i < nbuf; i++) {
+		if (stop)
+			return 1;
 		if (mb[i].ptr[mb[i].size-1] != '\n') {
 			/* Incomplete line */
 			strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
@@ -63,17 +69,21 @@ static int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
 
 		/* we have a complete line */
 		if (!priv->remainder.len) {
-			consume_one(priv, mb[i].ptr, mb[i].size);
+			stop = consume_one(priv, mb[i].ptr, mb[i].size);
 			continue;
 		}
 		strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
-		consume_one(priv, priv->remainder.buf, priv->remainder.len);
+		stop = consume_one(priv, priv->remainder.buf, priv->remainder.len);
 		strbuf_reset(&priv->remainder);
 	}
+	if (stop)
+		return -1;
 	if (priv->remainder.len) {
-		consume_one(priv, priv->remainder.buf, priv->remainder.len);
+		stop = consume_one(priv, priv->remainder.buf, priv->remainder.len);
 		strbuf_reset(&priv->remainder);
 	}
+	if (stop)
+		return -1;
 	return 0;
 }
 
xdiff-interface.h +17 -0
index 0198f9632f5..7d1724abb64 100644
@@ -11,6 +11,23 @@
  */
 #define MAX_XDIFF_SIZE (1024UL * 1024 * 1023)
 
+/**
+ * The `xdiff_emit_line_fn` function can return 1 to abort early, or 0
+ * to continue processing. Note that doing so is an all-or-nothing
+ * affair, as returning 1 will return all the way to the top-level,
+ * e.g. the xdi_diff_outf() call to generate the diff.
+ *
+ * Thus returning 1 means you won't be getting any more diff lines. If
+ * you need something in-between those two options you'll to use
+ * `xdl_emit_hunk_consume_func_t` and implement your own version of
+ * xdl_emit_diff().
+ *
+ * We may extend the interface in the future to understand other more
+ * granular return values. While you should return 1 to exit early,
+ * doing so will currently make your early return indistinguishable
+ * from an error internal to xdiff, xdiff itself will see that
+ * non-zero return and translate it to -1.
+ */
 typedef int (*xdiff_emit_line_fn)(void *, char *, unsigned long);
 typedef void (*xdiff_emit_hunk_fn)(void *data,
 				   long old_begin, long old_nr,
Signature · 1 line
2.31.1.639.g3d04783866f
#62 lore raw

PATCH v3 19/22 pickaxe -G: terminate early on matching lines

In reply to Ævar Arnfjörð Bjarmason

Solve a long-standing item for "git log -Grx" of us e.g. finding "+ str" in the diff context and noting that we had a "hit", but xdiff diligently continuing to generate and spew the rest of the diff at us. This makes use of a new "early return" xdiff interface added by preceding commits.

The TODO item (or, the NEEDSWORK comment) has been there since "git log -G" was implemented. See f506b8e8b5f (git log/diff: add -G<regexp> that greps in the patch text, 2010-08-23).

But now with the support added in the preceding changes to the xdiff-interface we can return early. Let's assert the behavior of that new early-return xdiff-interface by having a BUG() call here to die if it ever starts handing us needless work again.

---
 diffcore-pickaxe.c | 30 +++++++++++++++++++-----------
 xdiff-interface.h  |  4 ++++
 2 files changed, 23 insertions(+), 11 deletions(-)
diffcore-pickaxe.c +19 -11
index 27aa20be350..2147afef722 100644
@@ -27,13 +27,12 @@ static int diffgrep_consume(void *priv, char *line, unsigned long len)
 	if (line[0] != '+' && line[0] != '-')
 		return 0;
 	if (data->hit)
-		/*
-		 * NEEDSWORK: we should have a way to terminate the
-		 * caller early.
-		 */
-		return 0;
-	data->hit = !regexec_buf(data->regexp, line + 1, len - 1, 1,
-				 &regmatch, 0);
+		BUG("Already matched in diffgrep_consume! Broken xdiff_emit_line_fn?");
+	if (!regexec_buf(data->regexp, line + 1, len - 1, 1,
+			 &regmatch, 0)) {
+		data->hit = 1;
+		return 1;
+	}
 	return 0;
 }
 
@@ -45,6 +44,7 @@ static int diff_grep(mmfile_t *one, mmfile_t *two,
 	struct diffgrep_cb ecbdata;
 	xpparam_t xpp;
 	xdemitconf_t xecfg;
+	int ret;
 
 	if (!one)
 		return !regexec_buf(regexp, two->ptr, two->size,
@@ -63,10 +63,18 @@ static int diff_grep(mmfile_t *one, mmfile_t *two,
 	ecbdata.hit = 0;
 	xecfg.ctxlen = o->context;
 	xecfg.interhunkctxlen = o->interhunkcontext;
-	if (xdi_diff_outf(one, two, discard_hunk_line, diffgrep_consume,
-			  &ecbdata, &xpp, &xecfg))
-		return 0;
-	return ecbdata.hit;
+
+	/*
+	 * An xdiff error might be our "data->hit" from above. See the
+	 * comment for xdiff_emit_line_fn in xdiff-interface.h
+	 */
+	ret = xdi_diff_outf(one, two, discard_hunk_line, diffgrep_consume,
+			    &ecbdata, &xpp, &xecfg);
+	if (ecbdata.hit)
+		return 1;
+	if (ret)
+		return ret;
+	return 0;
 }
 
 static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws,
xdiff-interface.h +4 -0
index 7d1724abb64..3b6819586da 100644
@@ -27,6 +27,10 @@
  * doing so will currently make your early return indistinguishable
  * from an error internal to xdiff, xdiff itself will see that
  * non-zero return and translate it to -1.
+ *
+ * See "diff_grep" in diffcore-pickaxe.c for a trick to work around
+ * this, i.e. using the "consume_callback_data" to note the desired
+ * early return.
  */
 typedef int (*xdiff_emit_line_fn)(void *, char *, unsigned long);
 typedef void (*xdiff_emit_hunk_fn)(void *data,
Signature · 1 line
2.31.1.639.g3d04783866f
#63 lore raw

PATCH v3 20/22 pickaxe -G: don't special-case create/delete

In reply to Ævar Arnfjörð Bjarmason

Instead of special-casing creations and deletions let's just generate a diff for them.

This logic of not running a diff under -G if we don't have both sides dates back to the original implementation of -S in 52e9578985f ([PATCH] Introducing software archaeologist's tool "pickaxe"., 2005-05-21).

In the case of -S we were not working with the xdiff interface and needed to do this, but when -G was implemented in f506b8e8b5f (git log/diff: add -G<regexp> that greps in the patch text, 2010-08-23) this logic was diligently copied over.

But as the performance test added earlier in this series shows, this does not make much of a difference. With:

time GIT_TEST_LONG= GIT_PERF_REPEAT_COUNT=10 GIT_PERF_MAKE_OPTS='-j8 CFLAGS=-O3' ./run origin/next HEAD~ HEAD -- p4209-pickaxe.sh

With the HEAD~ commit being the preceding "pickaxe -G: terminate early on matching lines" we get these results. Note that it's only the -G codepaths that are relevant to this change:

    Test                                                                      origin/next       HEAD~                   HEAD
    -----------------------------------------------------------------------------------------------------------------------------------------
    4209.1: git log -S'int main' <limit-rev>..                                0.35(0.32+0.03)   0.35(0.33+0.02) +0.0%   0.35(0.30+0.05) +0.0%
    4209.2: git log -S'æ' <limit-rev>..                                       0.46(0.42+0.04)   0.46(0.41+0.05) +0.0%   0.46(0.42+0.04) +0.0%
    4209.3: git log --pickaxe-regex -S'(int|void|null)' <limit-rev>..         0.65(0.62+0.02)   0.64(0.61+0.02) -1.5%   0.64(0.60+0.04) -1.5%
    4209.4: git log --pickaxe-regex -S'if *\([^ ]+ & ' <limit-rev>..          0.52(0.45+0.06)   0.52(0.50+0.01) +0.0%   0.54(0.47+0.04) +3.8%
    4209.5: git log --pickaxe-regex -S'[àáâãäåæñøùúûüýþ]' <limit-rev>..       0.39(0.34+0.05)   0.39(0.34+0.04) +0.0%   0.39(0.36+0.03) +0.0%
    4209.6: git log -G'(int|void|null)' <limit-rev>..                         0.60(0.55+0.04)   0.58(0.54+0.03) -3.3%   0.58(0.49+0.08) -3.3%
    4209.7: git log -G'if *\([^ ]+ & ' <limit-rev>..                          0.61(0.52+0.06)   0.59(0.53+0.05) -3.3%   0.59(0.54+0.05) -3.3%
    4209.8: git log -G'[àáâãäåæñøùúûüýþ]' <limit-rev>..                       0.61(0.51+0.07)   0.58(0.54+0.04) -4.9%   0.57(0.51+0.06) -6.6%
    4209.9: git log -i -S'int main' <limit-rev>..                             0.36(0.31+0.04)   0.36(0.34+0.02) +0.0%   0.35(0.32+0.03) -2.8%
    4209.10: git log -i -S'æ' <limit-rev>..                                   0.36(0.33+0.03)   0.39(0.34+0.01) +8.3%   0.36(0.32+0.03) +0.0%
    4209.11: git log -i --pickaxe-regex -S'(int|void|null)' <limit-rev>..     0.83(0.77+0.05)   0.82(0.77+0.05) -1.2%   0.80(0.75+0.04) -3.6%
    4209.12: git log -i --pickaxe-regex -S'if *\([^ ]+ & ' <limit-rev>..      0.67(0.61+0.03)   0.64(0.61+0.03) -4.5%   0.63(0.61+0.02) -6.0%
    4209.13: git log -i --pickaxe-regex -S'[àáâãäåæñøùúûüýþ]' <limit-rev>..   0.40(0.37+0.02)   0.40(0.37+0.03) +0.0%   0.40(0.36+0.04) +0.0%
    4209.14: git log -i -G'(int|void|null)' <limit-rev>..                     0.58(0.51+0.07)   0.59(0.52+0.06) +1.7%   0.58(0.52+0.05) +0.0%
    4209.15: git log -i -G'if *\([^ ]+ & ' <limit-rev>..                      0.60(0.54+0.05)   0.60(0.54+0.06) +0.0%   0.60(0.56+0.03) +0.0%
    4209.16: git log -i -G'[àáâãäåæñøùúûüýþ]' <limit-rev>..                   0.58(0.51+0.06)   0.57(0.52+0.05) -1.7%   0.60(0.48+0.09) +3.4%

This small simplification really doesn't buy us much now, but I've got plans to both convert the pickaxe code to using a PCREv2 backend[1] and to implement additional pickaxe modes to do custom searches through the diff[2]. Always having the diff available under -G is going to help to simplify both of those changes.

1. https://lore.kernel.org/git/[email protected]/
2. https://lore.kernel.org/git/[email protected]/

---
 diffcore-pickaxe.c | 12 +-----------
 1 file changed, 1 insertion(+), 11 deletions(-)
diffcore-pickaxe.c +1 -11
index 2147afef722..96183f4cfab 100644
@@ -40,19 +40,11 @@ static int diff_grep(mmfile_t *one, mmfile_t *two,
 		     struct diff_options *o,
 		     regex_t *regexp, kwset_t kws)
 {
-	regmatch_t regmatch;
 	struct diffgrep_cb ecbdata;
 	xpparam_t xpp;
 	xdemitconf_t xecfg;
 	int ret;
 
-	if (!one)
-		return !regexec_buf(regexp, two->ptr, two->size,
-				    1, &regmatch, 0);
-	if (!two)
-		return !regexec_buf(regexp, one->ptr, one->size,
-				    1, &regmatch, 0);
-
 	/*
 	 * We have both sides; need to run textual diff and see if
 	 * the pattern appears on added/deleted lines.
@@ -172,9 +164,7 @@ static int pickaxe_match(struct diff_filepair *p, struct diff_options *o,
 	mf1.size = fill_textconv(o->repo, textconv_one, p->one, &mf1.ptr);
 	mf2.size = fill_textconv(o->repo, textconv_two, p->two, &mf2.ptr);
 
-	ret = fn(DIFF_FILE_VALID(p->one) ? &mf1 : NULL,
-		 DIFF_FILE_VALID(p->two) ? &mf2 : NULL,
-		 o, regexp, kws);
+	ret = fn(&mf1, &mf2, o, regexp, kws);
 
 	if (textconv_one)
 		free(mf1.ptr);
Signature · 1 line
2.31.1.639.g3d04783866f
#64 lore raw

PATCH v3 21/22 xdiff users: use designated initializers for out_line

In reply to Ævar Arnfjörð Bjarmason

Amend the code added in 611e42a5980 (xdiff: provide a separate emit callback for hunks, 2018-11-02) to be more readable by using designated initializers.

This changes "priv" in rerere.c to be initialized to NULL as we did in merge-tree.c. That's not needed as we'll only use it if the callback is defined, but being consistent here is better and less verbose.

---
 builtin/merge-tree.c | 5 +----
 builtin/rerere.c     | 4 +---
 2 files changed, 2 insertions(+), 7 deletions(-)
builtin/merge-tree.c +1 -4
index de8520778d2..5dc94d6f880 100644
@@ -107,15 +107,12 @@ static void show_diff(struct merge_list *entry)
 	mmfile_t src, dst;
 	xpparam_t xpp;
 	xdemitconf_t xecfg;
-	xdemitcb_t ecb;
+	xdemitcb_t ecb = { .out_line = show_outf };
 
 	memset(&xpp, 0, sizeof(xpp));
 	xpp.flags = 0;
 	memset(&xecfg, 0, sizeof(xecfg));
 	xecfg.ctxlen = 3;
-	ecb.out_hunk = NULL;
-	ecb.out_line = show_outf;
-	ecb.priv = NULL;
 
 	src.ptr = origin(entry, &size);
 	if (!src.ptr)
builtin/rerere.c +1 -3
index fd3be17b976..83d7a778e37 100644
@@ -28,7 +28,7 @@ static int diff_two(const char *file1, const char *label1,
 {
 	xpparam_t xpp;
 	xdemitconf_t xecfg;
-	xdemitcb_t ecb;
+	xdemitcb_t ecb = { .out_line = outf };
 	mmfile_t minus, plus;
 	int ret;
 
@@ -41,8 +41,6 @@ static int diff_two(const char *file1, const char *label1,
 	xpp.flags = 0;
 	memset(&xecfg, 0, sizeof(xecfg));
 	xecfg.ctxlen = 3;
-	ecb.out_hunk = NULL;
-	ecb.out_line = outf;
 	ret = xdi_diff(&minus, &plus, &xpp, &xecfg, &ecb);
 
 	free(minus.ptr);
Signature · 1 line
2.31.1.639.g3d04783866f
#65 lore raw

PATCH v3 22/22 xdiff-interface: replace discard_hunk_line() with a flag

In reply to Ævar Arnfjörð Bjarmason

Remove the dummy discard_hunk_line() function added in 3b40a090fd4 (diff: avoid generating unused hunk header lines, 2018-11-02) in favor of having a new XDL_EMIT_NO_HUNK_HDR flag, for use along with the two existing and similar XDL_EMIT_* flags.

Unlike the recently amended xdiff_emit_line_fn interface which'll be called in a loop in xdl_emit_diff(), the hunk header is only emitted once.

It makes more sense to pass this as a flag than provide a dummy callback because that function may be able to skip doing certain work if it knows the caller is doing nothing with the hunk header.

It would be possible to do so in the case of -U0 now, but the benefit of doing so is so small that I haven't bothered. But this leaves the door open to that, and more importantly makes the API use more intuitive.

The reason we're putting a flag in the gap between 1<<0 and 1<<2 is that the old 1<<1 flag was removed in 907681e940d (xdiff: drop XDL_EMIT_COMMON, 2016-02-23) without re-ordering the remaining flags.

---
 diff.c             | 7 ++++---
 diffcore-pickaxe.c | 3 ++-
 xdiff-interface.c  | 6 ------
 xdiff-interface.h  | 8 --------
 xdiff/xdiff.h      | 1 +
 xdiff/xemit.c      | 3 ++-
 6 files changed, 9 insertions(+), 19 deletions(-)
diff.c +4 -3
index 7a03c581c79..fe3abac79fe 100644
@@ -3725,7 +3725,8 @@ static void builtin_diffstat(const char *name_a, const char *name_b,
 		xpp.anchors_nr = o->anchors_nr;
 		xecfg.ctxlen = o->context;
 		xecfg.interhunkctxlen = o->interhunkcontext;
-		if (xdi_diff_outf(&mf1, &mf2, discard_hunk_line,
+		xecfg.flags = XDL_EMIT_NO_HUNK_HDR;
+		if (xdi_diff_outf(&mf1, &mf2, NULL,
 				  diffstat_consume, diffstat, &xpp, &xecfg))
 			die("unable to generate diffstat for %s", one->path);
 
@@ -6233,8 +6234,8 @@ static int diff_get_patch_id(struct diff_options *options, struct object_id *oid
 
 		xpp.flags = 0;
 		xecfg.ctxlen = 3;
-		xecfg.flags = 0;
-		if (xdi_diff_outf(&mf1, &mf2, discard_hunk_line,
+		xecfg.flags = XDL_EMIT_NO_HUNK_HDR;
+		if (xdi_diff_outf(&mf1, &mf2, NULL,
 				  patch_id_consume, &data, &xpp, &xecfg))
 			return error("unable to generate patch-id diff for %s",
 				     p->one->path);
diffcore-pickaxe.c +2 -1
index 96183f4cfab..c88e50c6329 100644
@@ -53,6 +53,7 @@ static int diff_grep(mmfile_t *one, mmfile_t *two,
 	memset(&xecfg, 0, sizeof(xecfg));
 	ecbdata.regexp = regexp;
 	ecbdata.hit = 0;
+	xecfg.flags = XDL_EMIT_NO_HUNK_HDR;
 	xecfg.ctxlen = o->context;
 	xecfg.interhunkctxlen = o->interhunkcontext;
 
@@ -60,7 +61,7 @@ static int diff_grep(mmfile_t *one, mmfile_t *two,
 	 * An xdiff error might be our "data->hit" from above. See the
 	 * comment for xdiff_emit_line_fn in xdiff-interface.h
 	 */
-	ret = xdi_diff_outf(one, two, discard_hunk_line, diffgrep_consume,
+	ret = xdi_diff_outf(one, two, NULL, diffgrep_consume,
 			    &ecbdata, &xpp, &xecfg);
 	if (ecbdata.hit)
 		return 1;
xdiff-interface.c +0 -6
index 50c0ef759dd..95f13a93ff9 100644
@@ -126,12 +126,6 @@ int xdi_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t co
 	return xdl_diff(&a, &b, xpp, xecfg, xecb);
 }
 
-void discard_hunk_line(void *priv,
-		       long ob, long on, long nb, long nn,
-		       const char *func, long funclen)
-{
-}
-
 int xdi_diff_outf(mmfile_t *mf1, mmfile_t *mf2,
 		  xdiff_emit_hunk_fn hunk_fn,
 		  xdiff_emit_line_fn line_fn,
xdiff-interface.h +0 -8
index 3b6819586da..4301a7eef27 100644
@@ -53,14 +53,6 @@ void xdiff_clear_find_func(xdemitconf_t *xecfg);
 int git_xmerge_config(const char *var, const char *value, void *cb);
 extern int git_xmerge_style;
 
-/*
- * Can be used as a no-op hunk_fn for xdi_diff_outf(), since a NULL
- * one just sends the hunk line to the line_fn callback).
- */
-void discard_hunk_line(void *priv,
-		       long ob, long on, long nb, long nn,
-		       const char *func, long funclen);
-
 /*
  * Compare the strings l1 with l2 which are of size s1 and s2 respectively.
  * Returns 1 if the strings are deemed equal, 0 otherwise.
xdiff/xdiff.h +1 -0
index 7a046051468..b29deca5de8 100644
@@ -50,6 +50,7 @@ extern "C" {
 
 /* xdemitconf_t.flags */
 #define XDL_EMIT_FUNCNAMES (1 << 0)
+#define XDL_EMIT_NO_HUNK_HDR (1 << 1)
 #define XDL_EMIT_FUNCCONTEXT (1 << 2)
 
 /* merge simplification levels */
xdiff/xemit.c +2 -1
index 9d7d6c50874..1cbf2b9829e 100644
@@ -278,7 +278,8 @@ int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
 				      s1 - 1, funclineprev);
 			funclineprev = s1 - 1;
 		}
-		if (xdl_emit_hunk_hdr(s1 + 1, e1 - s1, s2 + 1, e2 - s2,
+		if (!(xecfg->flags & XDL_EMIT_NO_HUNK_HDR) &&
+		    xdl_emit_hunk_hdr(s1 + 1, e1 - s1, s2 + 1, e2 - s2,
 				      func_line.buf, func_line.len, ecb) < 0)
 			return -1;
 
Signature · 1 line
2.31.1.639.g3d04783866f