Skip to content

Commit

Permalink
Merge branch 'ab/pickaxe-pcre2'
Browse files Browse the repository at this point in the history
Rewrite the backend for "diff -G/-S" to use pcre2 engine when
available.

* ab/pickaxe-pcre2: (22 commits)
  xdiff-interface: replace discard_hunk_line() with a flag
  xdiff users: use designated initializers for out_line
  pickaxe -G: don't special-case create/delete
  pickaxe -G: terminate early on matching lines
  xdiff-interface: allow early return from xdiff_emit_line_fn
  xdiff-interface: prepare for allowing early return
  pickaxe -S: slightly optimize contains()
  pickaxe: rename variables in has_changes() for brevity
  pickaxe -S: support content with NULs under --pickaxe-regex
  pickaxe: assert that we must have a needle under -G or -S
  pickaxe: refactor function selection in diffcore-pickaxe()
  perf: add performance test for pickaxe
  pickaxe/style: consolidate declarations and assignments
  diff.h: move pickaxe fields together again
  pickaxe: die when --find-object and --pickaxe-all are combined
  pickaxe: die when -G and --pickaxe-regex are combined
  pickaxe tests: add missing test for --no-pickaxe-regex being an error
  pickaxe tests: test for -G, -S and --find-object incompatibility
  pickaxe tests: add test for "log -S" not being a regex
  pickaxe tests: add test for diffgrep_consume() internals
  ...
  • Loading branch information
gitster committed Jul 13, 2021
2 parents c9780bb + 5d93460 commit 4da281e
Show file tree
Hide file tree
Showing 14 changed files with 312 additions and 107 deletions.
5 changes: 1 addition & 4 deletions builtin/merge-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions builtin/rerere.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions combine-diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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,
Expand Down
39 changes: 25 additions & 14 deletions diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -2340,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;
Expand Down Expand Up @@ -2376,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) {
Expand All @@ -2386,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
Expand All @@ -2399,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]) {
Expand All @@ -2427,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)
Expand Down Expand Up @@ -2526,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];
Expand All @@ -2535,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[] = "------------";
Expand Down Expand Up @@ -3212,7 +3214,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;
Expand All @@ -3236,7 +3238,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",
Expand All @@ -3248,6 +3250,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,
Expand Down Expand Up @@ -3726,7 +3729,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);

Expand Down Expand Up @@ -4632,6 +4636,12 @@ 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"));

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
Expand Down Expand Up @@ -6119,17 +6129,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)
Expand Down Expand Up @@ -6227,8 +6238,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);
Expand Down
7 changes: 5 additions & 2 deletions diff.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ struct diff_options {
* postimage of the diff_queue.
*/
const char *pickaxe;
unsigned pickaxe_opts;

/* -I<regex> */
regex_t **ignore_regex;
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -556,6 +555,10 @@ 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_KINDS_ALL_OBJFIND_MASK (DIFF_PICKAXE_ALL | \
DIFF_PICKAXE_KIND_OBJFIND)

#define DIFF_PICKAXE_IGNORE_CASE 32

Expand Down
Loading

0 comments on commit 4da281e

Please sign in to comment.