Skip to content

Commit 03c1f14

Browse files
avargitster
authored andcommitted
pickaxe: refactor function selection in diffcore-pickaxe()
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. Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d90d441 commit 03c1f14

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

diffcore-pickaxe.c

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,28 @@ void diffcore_pickaxe(struct diff_options *o)
228228
int opts = o->pickaxe_opts;
229229
regex_t regex, *regexp = NULL;
230230
kwset_t kws = NULL;
231+
pickaxe_fn fn;
231232

232233
if (opts & (DIFF_PICKAXE_REGEX | DIFF_PICKAXE_KIND_G)) {
233234
int cflags = REG_EXTENDED | REG_NEWLINE;
234235
if (o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE)
235236
cflags |= REG_ICASE;
236237
regcomp_or_die(&regex, needle, cflags);
237238
regexp = &regex;
239+
240+
if (opts & DIFF_PICKAXE_KIND_G)
241+
fn = diff_grep;
242+
else if (opts & DIFF_PICKAXE_REGEX)
243+
fn = has_changes;
244+
else
245+
/*
246+
* We don't need to check the combination of
247+
* -G and --pickaxe-regex, by the time we get
248+
* here diff.c has already died if they're
249+
* combined. See the usage tests in
250+
* t4209-log-pickaxe.sh.
251+
*/
252+
BUG("unreachable");
238253
} else if (opts & DIFF_PICKAXE_KIND_S) {
239254
if (o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE &&
240255
has_non_ascii(needle)) {
@@ -251,10 +266,14 @@ void diffcore_pickaxe(struct diff_options *o)
251266
kwsincr(kws, needle, strlen(needle));
252267
kwsprep(kws);
253268
}
269+
fn = has_changes;
270+
} else if (opts & DIFF_PICKAXE_KIND_OBJFIND) {
271+
fn = NULL;
272+
} else {
273+
BUG("unknown pickaxe_opts flag");
254274
}
255275

256-
pickaxe(&diff_queued_diff, o, regexp, kws,
257-
(opts & DIFF_PICKAXE_KIND_G) ? diff_grep : has_changes);
276+
pickaxe(&diff_queued_diff, o, regexp, kws, fn);
258277

259278
if (regexp)
260279
regfree(regexp);

0 commit comments

Comments
 (0)