Skip to content

Commit c0d75f0

Browse files
committed
Merge branch 'sb/diff-blobfind-pickaxe'
"diff" family of commands learned "--find-object=<object-id>" option to limit the findings to changes that involve the named object. * sb/diff-blobfind-pickaxe: diff: use HAS_MULTI_BITS instead of counting bits manually diff: properly error out when combining multiple pickaxe options diffcore: add a pickaxe option to find a specific blob diff: introduce DIFF_PICKAXE_KINDS_MASK diff: migrate diff_flags.pickaxe_ignore_case to a pickaxe_opts bit diff.h: make pickaxe_opts an unsigned bit field
2 parents addd37c + 4d8c51a commit c0d75f0

File tree

8 files changed

+155
-40
lines changed

8 files changed

+155
-40
lines changed

Documentation/diff-options.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,15 @@ occurrences of that string did not change).
508508
See the 'pickaxe' entry in linkgit:gitdiffcore[7] for more
509509
information.
510510

511+
--find-object=<object-id>::
512+
Look for differences that change the number of occurrences of
513+
the specified object. Similar to `-S`, just the argument is different
514+
in that it doesn't search for a specific string but for a specific
515+
object id.
516+
+
517+
The object can be a blob or a submodule commit. It implies the `-t` option in
518+
`git-log` to also find trees.
519+
511520
--pickaxe-all::
512521
When `-S` or `-G` finds a change, show all the changes in that
513522
changeset, not just the files that contain the change
@@ -516,6 +525,7 @@ information.
516525
--pickaxe-regex::
517526
Treat the <string> given to `-S` as an extended POSIX regular
518527
expression to match.
528+
519529
endif::git-format-patch[]
520530

521531
-O<orderfile>::

builtin/log.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ static void cmd_log_init_finish(int argc, const char **argv, const char *prefix,
188188
if (rev->show_notes)
189189
init_display_notes(&rev->notes_opt);
190190

191-
if (rev->diffopt.pickaxe || rev->diffopt.filter ||
192-
rev->diffopt.flags.follow_renames)
191+
if ((rev->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
192+
rev->diffopt.filter || rev->diffopt.flags.follow_renames)
193193
rev->always_show_header = 0;
194194

195195
if (source)

combine-diff.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1438,7 +1438,7 @@ void diff_tree_combined(const struct object_id *oid,
14381438
opt->flags.follow_renames ||
14391439
opt->break_opt != -1 ||
14401440
opt->detect_rename ||
1441-
opt->pickaxe ||
1441+
(opt->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
14421442
opt->filter;
14431443

14441444

diff.c

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4086,6 +4086,7 @@ void diff_setup(struct diff_options *options)
40864086
options->interhunkcontext = diff_interhunk_context_default;
40874087
options->ws_error_highlight = ws_error_highlight_default;
40884088
options->flags.rename_empty = 1;
4089+
options->objfind = NULL;
40894090

40904091
/* pathchange left =NULL by default */
40914092
options->change = diff_change;
@@ -4110,22 +4111,20 @@ void diff_setup(struct diff_options *options)
41104111

41114112
void diff_setup_done(struct diff_options *options)
41124113
{
4113-
int count = 0;
4114+
unsigned check_mask = DIFF_FORMAT_NAME |
4115+
DIFF_FORMAT_NAME_STATUS |
4116+
DIFF_FORMAT_CHECKDIFF |
4117+
DIFF_FORMAT_NO_OUTPUT;
41144118

41154119
if (options->set_default)
41164120
options->set_default(options);
41174121

4118-
if (options->output_format & DIFF_FORMAT_NAME)
4119-
count++;
4120-
if (options->output_format & DIFF_FORMAT_NAME_STATUS)
4121-
count++;
4122-
if (options->output_format & DIFF_FORMAT_CHECKDIFF)
4123-
count++;
4124-
if (options->output_format & DIFF_FORMAT_NO_OUTPUT)
4125-
count++;
4126-
if (count > 1)
4122+
if (HAS_MULTI_BITS(options->output_format & check_mask))
41274123
die(_("--name-only, --name-status, --check and -s are mutually exclusive"));
41284124

4125+
if (HAS_MULTI_BITS(options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK))
4126+
die(_("-G, -S and --find-object are mutually exclusive"));
4127+
41294128
/*
41304129
* Most of the time we can say "there are changes"
41314130
* only by checking if there are changed paths, but
@@ -4175,7 +4174,7 @@ void diff_setup_done(struct diff_options *options)
41754174
/*
41764175
* Also pickaxe would not work very well if you do not say recursive
41774176
*/
4178-
if (options->pickaxe)
4177+
if (options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK)
41794178
options->flags.recursive = 1;
41804179
/*
41814180
* When patches are generated, submodules diffed against the work tree
@@ -4489,6 +4488,23 @@ static int parse_ws_error_highlight_opt(struct diff_options *opt, const char *ar
44894488
return 1;
44904489
}
44914490

4491+
static int parse_objfind_opt(struct diff_options *opt, const char *arg)
4492+
{
4493+
struct object_id oid;
4494+
4495+
if (get_oid(arg, &oid))
4496+
return error("unable to resolve '%s'", arg);
4497+
4498+
if (!opt->objfind)
4499+
opt->objfind = xcalloc(1, sizeof(*opt->objfind));
4500+
4501+
opt->pickaxe_opts |= DIFF_PICKAXE_KIND_OBJFIND;
4502+
opt->flags.recursive = 1;
4503+
opt->flags.tree_in_recursive = 1;
4504+
oidset_insert(opt->objfind, &oid);
4505+
return 1;
4506+
}
4507+
44924508
int diff_opt_parse(struct diff_options *options,
44934509
const char **av, int ac, const char *prefix)
44944510
{
@@ -4736,7 +4752,8 @@ int diff_opt_parse(struct diff_options *options,
47364752
else if ((argcount = short_opt('O', av, &optarg))) {
47374753
options->orderfile = prefix_filename(prefix, optarg);
47384754
return argcount;
4739-
}
4755+
} else if (skip_prefix(arg, "--find-object=", &arg))
4756+
return parse_objfind_opt(options, arg);
47404757
else if ((argcount = parse_long_opt("diff-filter", av, &optarg))) {
47414758
int offending = parse_diff_filter_opt(optarg, options);
47424759
if (offending)
@@ -5783,7 +5800,7 @@ void diffcore_std(struct diff_options *options)
57835800
if (options->break_opt != -1)
57845801
diffcore_merge_broken();
57855802
}
5786-
if (options->pickaxe)
5803+
if (options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK)
57875804
diffcore_pickaxe(options);
57885805
if (options->orderfile)
57895806
diffcore_order(options->orderfile);

diff.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "tree-walk.h"
88
#include "pathspec.h"
99
#include "object.h"
10+
#include "oidset.h"
1011

1112
struct rev_info;
1213
struct diff_options;
@@ -91,7 +92,6 @@ struct diff_flags {
9192
unsigned override_submodule_config:1;
9293
unsigned dirstat_by_line:1;
9394
unsigned funccontext:1;
94-
unsigned pickaxe_ignore_case:1;
9595
unsigned default_follow_renames:1;
9696
};
9797

@@ -146,7 +146,7 @@ struct diff_options {
146146
int skip_stat_unmatch;
147147
int line_termination;
148148
int output_format;
149-
int pickaxe_opts;
149+
unsigned pickaxe_opts;
150150
int rename_score;
151151
int rename_limit;
152152
int needed_rename_limit;
@@ -178,6 +178,8 @@ struct diff_options {
178178
enum diff_words_type word_diff;
179179
enum diff_submodule_format submodule_format;
180180

181+
struct oidset *objfind;
182+
181183
/* this is set by diffcore for DIFF_FORMAT_PATCH */
182184
int found_changes;
183185

@@ -330,6 +332,13 @@ extern void diff_setup_done(struct diff_options *);
330332

331333
#define DIFF_PICKAXE_KIND_S 4 /* traditional plumbing counter */
332334
#define DIFF_PICKAXE_KIND_G 8 /* grep in the patch */
335+
#define DIFF_PICKAXE_KIND_OBJFIND 16 /* specific object IDs */
336+
337+
#define DIFF_PICKAXE_KINDS_MASK (DIFF_PICKAXE_KIND_S | \
338+
DIFF_PICKAXE_KIND_G | \
339+
DIFF_PICKAXE_KIND_OBJFIND)
340+
341+
#define DIFF_PICKAXE_IGNORE_CASE 32
333342

334343
extern void diffcore_std(struct diff_options *);
335344
extern void diffcore_fix_diff_index(struct diff_options *);

diffcore-pickaxe.c

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,20 @@ static int pickaxe_match(struct diff_filepair *p, struct diff_options *o,
124124
mmfile_t mf1, mf2;
125125
int ret;
126126

127-
if (!o->pickaxe[0])
128-
return 0;
129-
130127
/* ignore unmerged */
131128
if (!DIFF_FILE_VALID(p->one) && !DIFF_FILE_VALID(p->two))
132129
return 0;
133130

131+
if (o->objfind) {
132+
return (DIFF_FILE_VALID(p->one) &&
133+
oidset_contains(o->objfind, &p->one->oid)) ||
134+
(DIFF_FILE_VALID(p->two) &&
135+
oidset_contains(o->objfind, &p->two->oid));
136+
}
137+
138+
if (!o->pickaxe[0])
139+
return 0;
140+
134141
if (o->flags.allow_textconv) {
135142
textconv_one = get_textconv(p->one);
136143
textconv_two = get_textconv(p->two);
@@ -222,33 +229,34 @@ void diffcore_pickaxe(struct diff_options *o)
222229

223230
if (opts & (DIFF_PICKAXE_REGEX | DIFF_PICKAXE_KIND_G)) {
224231
int cflags = REG_EXTENDED | REG_NEWLINE;
225-
if (o->flags.pickaxe_ignore_case)
232+
if (o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE)
226233
cflags |= REG_ICASE;
227234
regcomp_or_die(&regex, needle, cflags);
228235
regexp = &regex;
229-
} else if (o->flags.pickaxe_ignore_case &&
230-
has_non_ascii(needle)) {
231-
struct strbuf sb = STRBUF_INIT;
232-
int cflags = REG_NEWLINE | REG_ICASE;
233-
234-
basic_regex_quote_buf(&sb, needle);
235-
regcomp_or_die(&regex, sb.buf, cflags);
236-
strbuf_release(&sb);
237-
regexp = &regex;
238-
} else {
239-
kws = kwsalloc(o->flags.pickaxe_ignore_case
240-
? tolower_trans_tbl : NULL);
241-
kwsincr(kws, needle, strlen(needle));
242-
kwsprep(kws);
236+
} else if (opts & DIFF_PICKAXE_KIND_S) {
237+
if (o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE &&
238+
has_non_ascii(needle)) {
239+
struct strbuf sb = STRBUF_INIT;
240+
int cflags = REG_NEWLINE | REG_ICASE;
241+
242+
basic_regex_quote_buf(&sb, needle);
243+
regcomp_or_die(&regex, sb.buf, cflags);
244+
strbuf_release(&sb);
245+
regexp = &regex;
246+
} else {
247+
kws = kwsalloc(o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE
248+
? tolower_trans_tbl : NULL);
249+
kwsincr(kws, needle, strlen(needle));
250+
kwsprep(kws);
251+
}
243252
}
244253

245-
/* Might want to warn when both S and G are on; I don't care... */
246254
pickaxe(&diff_queued_diff, o, regexp, kws,
247255
(opts & DIFF_PICKAXE_KIND_G) ? diff_grep : has_changes);
248256

249257
if (regexp)
250258
regfree(regexp);
251-
else
259+
if (kws)
252260
kwsfree(kws);
253261
return;
254262
}

revision.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2078,7 +2078,7 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
20782078
revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
20792079
} else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
20802080
revs->grep_filter.ignore_case = 1;
2081-
revs->diffopt.flags.pickaxe_ignore_case = 1;
2081+
revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
20822082
} else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
20832083
revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
20842084
} else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
@@ -2409,11 +2409,14 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s
24092409
revs->diff = 1;
24102410

24112411
/* Pickaxe, diff-filter and rename following need diffs */
2412-
if (revs->diffopt.pickaxe ||
2412+
if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
24132413
revs->diffopt.filter ||
24142414
revs->diffopt.flags.follow_renames)
24152415
revs->diff = 1;
24162416

2417+
if (revs->diffopt.objfind)
2418+
revs->simplify_history = 0;
2419+
24172420
if (revs->topo_order)
24182421
revs->limited = 1;
24192422

t/t4064-diff-oidfind.sh

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/bin/sh
2+
3+
test_description='test finding specific blobs in the revision walking'
4+
. ./test-lib.sh
5+
6+
test_expect_success 'setup ' '
7+
git commit --allow-empty -m "empty initial commit" &&
8+
9+
echo "Hello, world!" >greeting &&
10+
git add greeting &&
11+
git commit -m "add the greeting blob" && # borrowed from Git from the Bottom Up
12+
git tag -m "the blob" greeting $(git rev-parse HEAD:greeting) &&
13+
14+
echo asdf >unrelated &&
15+
git add unrelated &&
16+
git commit -m "unrelated history" &&
17+
18+
git revert HEAD^ &&
19+
20+
git commit --allow-empty -m "another unrelated commit"
21+
'
22+
23+
test_expect_success 'find the greeting blob' '
24+
cat >expect <<-EOF &&
25+
Revert "add the greeting blob"
26+
add the greeting blob
27+
EOF
28+
29+
git log --format=%s --find-object=greeting^{blob} >actual &&
30+
31+
test_cmp expect actual
32+
'
33+
34+
test_expect_success 'setup a tree' '
35+
mkdir a &&
36+
echo asdf >a/file &&
37+
git add a/file &&
38+
git commit -m "add a file in a subdirectory"
39+
'
40+
41+
test_expect_success 'find a tree' '
42+
cat >expect <<-EOF &&
43+
add a file in a subdirectory
44+
EOF
45+
46+
git log --format=%s -t --find-object=HEAD:a >actual &&
47+
48+
test_cmp expect actual
49+
'
50+
51+
test_expect_success 'setup a submodule' '
52+
test_create_repo sub &&
53+
test_commit -C sub sub &&
54+
git submodule add ./sub sub &&
55+
git commit -a -m "add sub"
56+
'
57+
58+
test_expect_success 'find a submodule' '
59+
cat >expect <<-EOF &&
60+
add sub
61+
EOF
62+
63+
git log --format=%s --find-object=HEAD:sub >actual &&
64+
65+
test_cmp expect actual
66+
'
67+
68+
test_done

0 commit comments

Comments
 (0)