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
This commit is contained in:
Junio C Hamano 2018-01-23 13:16:37 -08:00
commit c0d75f0e2e
8 changed files with 154 additions and 39 deletions

View File

@ -508,6 +508,15 @@ occurrences of that string did not change).
See the 'pickaxe' entry in linkgit:gitdiffcore[7] for more See the 'pickaxe' entry in linkgit:gitdiffcore[7] for more
information. information.
--find-object=<object-id>::
Look for differences that change the number of occurrences of
the specified object. Similar to `-S`, just the argument is different
in that it doesn't search for a specific string but for a specific
object id.
+
The object can be a blob or a submodule commit. It implies the `-t` option in
`git-log` to also find trees.
--pickaxe-all:: --pickaxe-all::
When `-S` or `-G` finds a change, show all the changes in that When `-S` or `-G` finds a change, show all the changes in that
changeset, not just the files that contain the change changeset, not just the files that contain the change
@ -516,6 +525,7 @@ information.
--pickaxe-regex:: --pickaxe-regex::
Treat the <string> given to `-S` as an extended POSIX regular Treat the <string> given to `-S` as an extended POSIX regular
expression to match. expression to match.
endif::git-format-patch[] endif::git-format-patch[]
-O<orderfile>:: -O<orderfile>::

View File

@ -188,8 +188,8 @@ static void cmd_log_init_finish(int argc, const char **argv, const char *prefix,
if (rev->show_notes) if (rev->show_notes)
init_display_notes(&rev->notes_opt); init_display_notes(&rev->notes_opt);
if (rev->diffopt.pickaxe || rev->diffopt.filter || if ((rev->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
rev->diffopt.flags.follow_renames) rev->diffopt.filter || rev->diffopt.flags.follow_renames)
rev->always_show_header = 0; rev->always_show_header = 0;
if (source) if (source)

View File

@ -1438,7 +1438,7 @@ void diff_tree_combined(const struct object_id *oid,
opt->flags.follow_renames || opt->flags.follow_renames ||
opt->break_opt != -1 || opt->break_opt != -1 ||
opt->detect_rename || opt->detect_rename ||
opt->pickaxe || (opt->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
opt->filter; opt->filter;

43
diff.c
View File

@ -4086,6 +4086,7 @@ void diff_setup(struct diff_options *options)
options->interhunkcontext = diff_interhunk_context_default; options->interhunkcontext = diff_interhunk_context_default;
options->ws_error_highlight = ws_error_highlight_default; options->ws_error_highlight = ws_error_highlight_default;
options->flags.rename_empty = 1; options->flags.rename_empty = 1;
options->objfind = NULL;
/* pathchange left =NULL by default */ /* pathchange left =NULL by default */
options->change = diff_change; options->change = diff_change;
@ -4110,22 +4111,20 @@ void diff_setup(struct diff_options *options)
void diff_setup_done(struct diff_options *options) void diff_setup_done(struct diff_options *options)
{ {
int count = 0; unsigned check_mask = DIFF_FORMAT_NAME |
DIFF_FORMAT_NAME_STATUS |
DIFF_FORMAT_CHECKDIFF |
DIFF_FORMAT_NO_OUTPUT;
if (options->set_default) if (options->set_default)
options->set_default(options); options->set_default(options);
if (options->output_format & DIFF_FORMAT_NAME) if (HAS_MULTI_BITS(options->output_format & check_mask))
count++;
if (options->output_format & DIFF_FORMAT_NAME_STATUS)
count++;
if (options->output_format & DIFF_FORMAT_CHECKDIFF)
count++;
if (options->output_format & DIFF_FORMAT_NO_OUTPUT)
count++;
if (count > 1)
die(_("--name-only, --name-status, --check and -s are mutually exclusive")); die(_("--name-only, --name-status, --check and -s are mutually exclusive"));
if (HAS_MULTI_BITS(options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK))
die(_("-G, -S and --find-object are mutually exclusive"));
/* /*
* Most of the time we can say "there are changes" * Most of the time we can say "there are changes"
* only by checking if there are changed paths, but * only by checking if there are changed paths, but
@ -4175,7 +4174,7 @@ void diff_setup_done(struct diff_options *options)
/* /*
* Also pickaxe would not work very well if you do not say recursive * Also pickaxe would not work very well if you do not say recursive
*/ */
if (options->pickaxe) if (options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK)
options->flags.recursive = 1; options->flags.recursive = 1;
/* /*
* When patches are generated, submodules diffed against the work tree * 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
return 1; return 1;
} }
static int parse_objfind_opt(struct diff_options *opt, const char *arg)
{
struct object_id oid;
if (get_oid(arg, &oid))
return error("unable to resolve '%s'", arg);
if (!opt->objfind)
opt->objfind = xcalloc(1, sizeof(*opt->objfind));
opt->pickaxe_opts |= DIFF_PICKAXE_KIND_OBJFIND;
opt->flags.recursive = 1;
opt->flags.tree_in_recursive = 1;
oidset_insert(opt->objfind, &oid);
return 1;
}
int diff_opt_parse(struct diff_options *options, int diff_opt_parse(struct diff_options *options,
const char **av, int ac, const char *prefix) const char **av, int ac, const char *prefix)
{ {
@ -4736,7 +4752,8 @@ int diff_opt_parse(struct diff_options *options,
else if ((argcount = short_opt('O', av, &optarg))) { else if ((argcount = short_opt('O', av, &optarg))) {
options->orderfile = prefix_filename(prefix, optarg); options->orderfile = prefix_filename(prefix, optarg);
return argcount; return argcount;
} } else if (skip_prefix(arg, "--find-object=", &arg))
return parse_objfind_opt(options, arg);
else if ((argcount = parse_long_opt("diff-filter", av, &optarg))) { else if ((argcount = parse_long_opt("diff-filter", av, &optarg))) {
int offending = parse_diff_filter_opt(optarg, options); int offending = parse_diff_filter_opt(optarg, options);
if (offending) if (offending)
@ -5783,7 +5800,7 @@ void diffcore_std(struct diff_options *options)
if (options->break_opt != -1) if (options->break_opt != -1)
diffcore_merge_broken(); diffcore_merge_broken();
} }
if (options->pickaxe) if (options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK)
diffcore_pickaxe(options); diffcore_pickaxe(options);
if (options->orderfile) if (options->orderfile)
diffcore_order(options->orderfile); diffcore_order(options->orderfile);

13
diff.h
View File

@ -7,6 +7,7 @@
#include "tree-walk.h" #include "tree-walk.h"
#include "pathspec.h" #include "pathspec.h"
#include "object.h" #include "object.h"
#include "oidset.h"
struct rev_info; struct rev_info;
struct diff_options; struct diff_options;
@ -91,7 +92,6 @@ struct diff_flags {
unsigned override_submodule_config:1; unsigned override_submodule_config:1;
unsigned dirstat_by_line:1; unsigned dirstat_by_line:1;
unsigned funccontext:1; unsigned funccontext:1;
unsigned pickaxe_ignore_case:1;
unsigned default_follow_renames:1; unsigned default_follow_renames:1;
}; };
@ -146,7 +146,7 @@ struct diff_options {
int skip_stat_unmatch; int skip_stat_unmatch;
int line_termination; int line_termination;
int output_format; int output_format;
int pickaxe_opts; unsigned pickaxe_opts;
int rename_score; int rename_score;
int rename_limit; int rename_limit;
int needed_rename_limit; int needed_rename_limit;
@ -178,6 +178,8 @@ struct diff_options {
enum diff_words_type word_diff; enum diff_words_type word_diff;
enum diff_submodule_format submodule_format; enum diff_submodule_format submodule_format;
struct oidset *objfind;
/* this is set by diffcore for DIFF_FORMAT_PATCH */ /* this is set by diffcore for DIFF_FORMAT_PATCH */
int found_changes; int found_changes;
@ -330,6 +332,13 @@ extern void diff_setup_done(struct diff_options *);
#define DIFF_PICKAXE_KIND_S 4 /* traditional plumbing counter */ #define DIFF_PICKAXE_KIND_S 4 /* traditional plumbing counter */
#define DIFF_PICKAXE_KIND_G 8 /* grep in the patch */ #define DIFF_PICKAXE_KIND_G 8 /* grep in the patch */
#define DIFF_PICKAXE_KIND_OBJFIND 16 /* specific object IDs */
#define DIFF_PICKAXE_KINDS_MASK (DIFF_PICKAXE_KIND_S | \
DIFF_PICKAXE_KIND_G | \
DIFF_PICKAXE_KIND_OBJFIND)
#define DIFF_PICKAXE_IGNORE_CASE 32
extern void diffcore_std(struct diff_options *); extern void diffcore_std(struct diff_options *);
extern void diffcore_fix_diff_index(struct diff_options *); extern void diffcore_fix_diff_index(struct diff_options *);

View File

@ -124,13 +124,20 @@ static int pickaxe_match(struct diff_filepair *p, struct diff_options *o,
mmfile_t mf1, mf2; mmfile_t mf1, mf2;
int ret; int ret;
if (!o->pickaxe[0])
return 0;
/* ignore unmerged */ /* ignore unmerged */
if (!DIFF_FILE_VALID(p->one) && !DIFF_FILE_VALID(p->two)) if (!DIFF_FILE_VALID(p->one) && !DIFF_FILE_VALID(p->two))
return 0; return 0;
if (o->objfind) {
return (DIFF_FILE_VALID(p->one) &&
oidset_contains(o->objfind, &p->one->oid)) ||
(DIFF_FILE_VALID(p->two) &&
oidset_contains(o->objfind, &p->two->oid));
}
if (!o->pickaxe[0])
return 0;
if (o->flags.allow_textconv) { if (o->flags.allow_textconv) {
textconv_one = get_textconv(p->one); textconv_one = get_textconv(p->one);
textconv_two = get_textconv(p->two); textconv_two = get_textconv(p->two);
@ -222,33 +229,34 @@ void diffcore_pickaxe(struct diff_options *o)
if (opts & (DIFF_PICKAXE_REGEX | DIFF_PICKAXE_KIND_G)) { if (opts & (DIFF_PICKAXE_REGEX | DIFF_PICKAXE_KIND_G)) {
int cflags = REG_EXTENDED | REG_NEWLINE; int cflags = REG_EXTENDED | REG_NEWLINE;
if (o->flags.pickaxe_ignore_case) if (o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE)
cflags |= REG_ICASE; cflags |= REG_ICASE;
regcomp_or_die(&regex, needle, cflags); regcomp_or_die(&regex, needle, cflags);
regexp = &regex; regexp = &regex;
} else if (o->flags.pickaxe_ignore_case && } else if (opts & DIFF_PICKAXE_KIND_S) {
has_non_ascii(needle)) { if (o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE &&
struct strbuf sb = STRBUF_INIT; has_non_ascii(needle)) {
int cflags = REG_NEWLINE | REG_ICASE; struct strbuf sb = STRBUF_INIT;
int cflags = REG_NEWLINE | REG_ICASE;
basic_regex_quote_buf(&sb, needle); basic_regex_quote_buf(&sb, needle);
regcomp_or_die(&regex, sb.buf, cflags); regcomp_or_die(&regex, sb.buf, cflags);
strbuf_release(&sb); strbuf_release(&sb);
regexp = &regex; regexp = &regex;
} else { } else {
kws = kwsalloc(o->flags.pickaxe_ignore_case kws = kwsalloc(o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE
? tolower_trans_tbl : NULL); ? tolower_trans_tbl : NULL);
kwsincr(kws, needle, strlen(needle)); kwsincr(kws, needle, strlen(needle));
kwsprep(kws); kwsprep(kws);
}
} }
/* Might want to warn when both S and G are on; I don't care... */
pickaxe(&diff_queued_diff, o, regexp, kws, pickaxe(&diff_queued_diff, o, regexp, kws,
(opts & DIFF_PICKAXE_KIND_G) ? diff_grep : has_changes); (opts & DIFF_PICKAXE_KIND_G) ? diff_grep : has_changes);
if (regexp) if (regexp)
regfree(regexp); regfree(regexp);
else if (kws)
kwsfree(kws); kwsfree(kws);
return; return;
} }

View File

@ -2078,7 +2078,7 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE; revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
} else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) { } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
revs->grep_filter.ignore_case = 1; revs->grep_filter.ignore_case = 1;
revs->diffopt.flags.pickaxe_ignore_case = 1; revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
} else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) { } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED; revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
} else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) { } 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
revs->diff = 1; revs->diff = 1;
/* Pickaxe, diff-filter and rename following need diffs */ /* Pickaxe, diff-filter and rename following need diffs */
if (revs->diffopt.pickaxe || if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
revs->diffopt.filter || revs->diffopt.filter ||
revs->diffopt.flags.follow_renames) revs->diffopt.flags.follow_renames)
revs->diff = 1; revs->diff = 1;
if (revs->diffopt.objfind)
revs->simplify_history = 0;
if (revs->topo_order) if (revs->topo_order)
revs->limited = 1; revs->limited = 1;

68
t/t4064-diff-oidfind.sh Executable file
View File

@ -0,0 +1,68 @@
#!/bin/sh
test_description='test finding specific blobs in the revision walking'
. ./test-lib.sh
test_expect_success 'setup ' '
git commit --allow-empty -m "empty initial commit" &&
echo "Hello, world!" >greeting &&
git add greeting &&
git commit -m "add the greeting blob" && # borrowed from Git from the Bottom Up
git tag -m "the blob" greeting $(git rev-parse HEAD:greeting) &&
echo asdf >unrelated &&
git add unrelated &&
git commit -m "unrelated history" &&
git revert HEAD^ &&
git commit --allow-empty -m "another unrelated commit"
'
test_expect_success 'find the greeting blob' '
cat >expect <<-EOF &&
Revert "add the greeting blob"
add the greeting blob
EOF
git log --format=%s --find-object=greeting^{blob} >actual &&
test_cmp expect actual
'
test_expect_success 'setup a tree' '
mkdir a &&
echo asdf >a/file &&
git add a/file &&
git commit -m "add a file in a subdirectory"
'
test_expect_success 'find a tree' '
cat >expect <<-EOF &&
add a file in a subdirectory
EOF
git log --format=%s -t --find-object=HEAD:a >actual &&
test_cmp expect actual
'
test_expect_success 'setup a submodule' '
test_create_repo sub &&
test_commit -C sub sub &&
git submodule add ./sub sub &&
git commit -a -m "add sub"
'
test_expect_success 'find a submodule' '
cat >expect <<-EOF &&
add sub
EOF
git log --format=%s --find-object=HEAD:sub >actual &&
test_cmp expect actual
'
test_done