Skip to content

Commit 28c6caf

Browse files
committed
Merge branch 'ab/grep-patterntype' into seen
Some code clean-up in the "git grep" machinery. Reroll exists. source: <[email protected]> * ab/grep-patterntype: grep API: call grep_config() after grep_init() grep.c: don't pass along NULL callback value built-ins: trust the "prefix" from run_builtin() fixup! grep tests: add missing "grep.patternType" config tests grep tests: add missing "grep.patternType" config tests log tests: check if grep_config() is called by "log"-like cmds grep.h: remove unused "regex_t regexp" from grep_opt
2 parents bb49920 + bc77ac5 commit 28c6caf

File tree

9 files changed

+123
-54
lines changed

9 files changed

+123
-54
lines changed

builtin/grep.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#include "object-store.h"
2727
#include "packfile.h"
2828

29+
static const char *grep_prefix;
30+
2931
static char const * const grep_usage[] = {
3032
N_("git grep [<options>] [-e] <pattern> [<rev>...] [[--] <path>...]"),
3133
NULL
@@ -284,7 +286,7 @@ static int wait_all(void)
284286
static int grep_cmd_config(const char *var, const char *value, void *cb)
285287
{
286288
int st = grep_config(var, value, cb);
287-
if (git_color_default_config(var, value, cb) < 0)
289+
if (git_color_default_config(var, value, NULL) < 0)
288290
st = -1;
289291

290292
if (!strcmp(var, "grep.threads")) {
@@ -315,11 +317,11 @@ static void grep_source_name(struct grep_opt *opt, const char *filename,
315317
strbuf_reset(out);
316318

317319
if (opt->null_following_name) {
318-
if (opt->relative && opt->prefix_length) {
320+
if (opt->relative && grep_prefix) {
319321
struct strbuf rel_buf = STRBUF_INIT;
320322
const char *rel_name =
321323
relative_path(filename + tree_name_len,
322-
opt->prefix, &rel_buf);
324+
grep_prefix, &rel_buf);
323325

324326
if (tree_name_len)
325327
strbuf_add(out, filename, tree_name_len);
@@ -332,8 +334,8 @@ static void grep_source_name(struct grep_opt *opt, const char *filename,
332334
return;
333335
}
334336

335-
if (opt->relative && opt->prefix_length)
336-
quote_path(filename + tree_name_len, opt->prefix, out, 0);
337+
if (opt->relative && grep_prefix)
338+
quote_path(filename + tree_name_len, grep_prefix, out, 0);
337339
else
338340
quote_c_style(filename + tree_name_len, out, NULL, 0);
339341

@@ -962,9 +964,10 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
962964
PARSE_OPT_NOCOMPLETE),
963965
OPT_END()
964966
};
967+
grep_prefix = prefix;
965968

966-
git_config(grep_cmd_config, NULL);
967-
grep_init(&opt, the_repository, prefix);
969+
grep_init(&opt, the_repository);
970+
git_config(grep_cmd_config, &opt);
968971

969972
/*
970973
* If there is no -- then the paths must exist in the working

builtin/log.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,8 +533,6 @@ static int git_log_config(const char *var, const char *value, void *cb)
533533
return 0;
534534
}
535535

536-
if (grep_config(var, value, cb) < 0)
537-
return -1;
538536
if (git_gpg_config(var, value, cb) < 0)
539537
return -1;
540538
return git_diff_ui_config(var, value, cb);
@@ -549,6 +547,8 @@ int cmd_whatchanged(int argc, const char **argv, const char *prefix)
549547
git_config(git_log_config, NULL);
550548

551549
repo_init_revisions(the_repository, &rev, prefix);
550+
git_config(grep_config, &rev.grep_filter);
551+
552552
rev.diff = 1;
553553
rev.simplify_history = 0;
554554
memset(&opt, 0, sizeof(opt));
@@ -663,6 +663,8 @@ int cmd_show(int argc, const char **argv, const char *prefix)
663663

664664
memset(&match_all, 0, sizeof(match_all));
665665
repo_init_revisions(the_repository, &rev, prefix);
666+
git_config(grep_config, &rev.grep_filter);
667+
666668
rev.diff = 1;
667669
rev.always_show_header = 1;
668670
rev.no_walk = 1;
@@ -746,6 +748,8 @@ int cmd_log_reflog(int argc, const char **argv, const char *prefix)
746748

747749
repo_init_revisions(the_repository, &rev, prefix);
748750
init_reflog_walk(&rev.reflog_info);
751+
git_config(grep_config, &rev.grep_filter);
752+
749753
rev.verbose_header = 1;
750754
memset(&opt, 0, sizeof(opt));
751755
opt.def = "HEAD";
@@ -779,6 +783,8 @@ int cmd_log(int argc, const char **argv, const char *prefix)
779783
git_config(git_log_config, NULL);
780784

781785
repo_init_revisions(the_repository, &rev, prefix);
786+
git_config(grep_config, &rev.grep_filter);
787+
782788
rev.always_show_header = 1;
783789
memset(&opt, 0, sizeof(opt));
784790
opt.def = "HEAD";
@@ -1861,10 +1867,13 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
18611867
extra_hdr.strdup_strings = 1;
18621868
extra_to.strdup_strings = 1;
18631869
extra_cc.strdup_strings = 1;
1870+
18641871
init_log_defaults();
18651872
init_display_notes(&notes_opt);
18661873
git_config(git_format_config, NULL);
18671874
repo_init_revisions(the_repository, &rev, prefix);
1875+
git_config(grep_config, &rev.grep_filter);
1876+
18681877
rev.show_notes = show_notes;
18691878
memcpy(&rev.notes_opt, &notes_opt, sizeof(notes_opt));
18701879
rev.commit_format = CMIT_FMT_EMAIL;

builtin/ls-tree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)
335335

336336
git_config(git_default_config, NULL);
337337
ls_tree_prefix = prefix;
338-
if (prefix && *prefix)
338+
if (prefix)
339339
chomp_prefix = strlen(prefix);
340340

341341
argc = parse_options(argc, argv, prefix, ls_tree_options,

git.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
436436
} else {
437437
prefix = NULL;
438438
}
439+
assert(!prefix || *prefix);
439440
precompose_argv_prefix(argc, argv, NULL);
440441
if (use_pager == -1 && run_setup &&
441442
!(p->option & DELAY_PAGER_CONFIG))

grep.c

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,6 @@ static void std_output(struct grep_opt *opt, const void *buf, size_t size)
1919
fwrite(buf, size, 1, stdout);
2020
}
2121

22-
static struct grep_opt grep_defaults = {
23-
.relative = 1,
24-
.pathname = 1,
25-
.max_depth = -1,
26-
.pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED,
27-
.colors = {
28-
[GREP_COLOR_CONTEXT] = "",
29-
[GREP_COLOR_FILENAME] = GIT_COLOR_MAGENTA,
30-
[GREP_COLOR_FUNCTION] = "",
31-
[GREP_COLOR_LINENO] = GIT_COLOR_GREEN,
32-
[GREP_COLOR_COLUMNNO] = GIT_COLOR_GREEN,
33-
[GREP_COLOR_MATCH_CONTEXT] = GIT_COLOR_BOLD_RED,
34-
[GREP_COLOR_MATCH_SELECTED] = GIT_COLOR_BOLD_RED,
35-
[GREP_COLOR_SELECTED] = "",
36-
[GREP_COLOR_SEP] = GIT_COLOR_CYAN,
37-
},
38-
.only_matching = 0,
39-
.color = -1,
40-
.output = std_output,
41-
};
42-
4322
static const char *color_grep_slots[] = {
4423
[GREP_COLOR_CONTEXT] = "context",
4524
[GREP_COLOR_FILENAME] = "filename",
@@ -75,20 +54,12 @@ define_list_config_array_extra(color_grep_slots, {"match"});
7554
*/
7655
int grep_config(const char *var, const char *value, void *cb)
7756
{
78-
struct grep_opt *opt = &grep_defaults;
57+
struct grep_opt *opt = cb;
7958
const char *slot;
8059

8160
if (userdiff_config(var, value) < 0)
8261
return -1;
8362

84-
/*
85-
* The instance of grep_opt that we set up here is copied by
86-
* grep_init() to be used by each individual invocation.
87-
* When populating a new field of this structure here, be
88-
* sure to think about ownership -- e.g., you might need to
89-
* override the shallow copy in grep_init() with a deep copy.
90-
*/
91-
9263
if (!strcmp(var, "grep.extendedregexp")) {
9364
opt->extended_regexp_option = git_config_bool(var, value);
9465
return 0;
@@ -134,18 +105,12 @@ int grep_config(const char *var, const char *value, void *cb)
134105
return 0;
135106
}
136107

137-
/*
138-
* Initialize one instance of grep_opt and copy the
139-
* default values from the template we read the configuration
140-
* information in an earlier call to git_config(grep_config).
141-
*/
142-
void grep_init(struct grep_opt *opt, struct repository *repo, const char *prefix)
108+
void grep_init(struct grep_opt *opt, struct repository *repo)
143109
{
144-
*opt = grep_defaults;
110+
struct grep_opt blank = GREP_OPT_INIT;
111+
memcpy(opt, &blank, sizeof(*opt));
145112

146113
opt->repo = repo;
147-
opt->prefix = prefix;
148-
opt->prefix_length = (prefix && *prefix) ? strlen(prefix) : 0;
149114
opt->pattern_tail = &opt->pattern_list;
150115
opt->header_tail = &opt->header_list;
151116
}

grep.h

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,6 @@ struct grep_opt {
134134
*/
135135
struct repository *repo;
136136

137-
const char *prefix;
138-
int prefix_length;
139-
regex_t regexp;
140137
int linenum;
141138
int columnnum;
142139
int invert;
@@ -182,8 +179,29 @@ struct grep_opt {
182179
void *output_priv;
183180
};
184181

182+
#define GREP_OPT_INIT { \
183+
.relative = 1, \
184+
.pathname = 1, \
185+
.max_depth = -1, \
186+
.pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED, \
187+
.colors = { \
188+
[GREP_COLOR_CONTEXT] = "", \
189+
[GREP_COLOR_FILENAME] = GIT_COLOR_MAGENTA, \
190+
[GREP_COLOR_FUNCTION] = "", \
191+
[GREP_COLOR_LINENO] = GIT_COLOR_GREEN, \
192+
[GREP_COLOR_COLUMNNO] = GIT_COLOR_GREEN, \
193+
[GREP_COLOR_MATCH_CONTEXT] = GIT_COLOR_BOLD_RED, \
194+
[GREP_COLOR_MATCH_SELECTED] = GIT_COLOR_BOLD_RED, \
195+
[GREP_COLOR_SELECTED] = "", \
196+
[GREP_COLOR_SEP] = GIT_COLOR_CYAN, \
197+
}, \
198+
.only_matching = 0, \
199+
.color = -1, \
200+
.output = std_output, \
201+
}
202+
185203
int grep_config(const char *var, const char *value, void *);
186-
void grep_init(struct grep_opt *, struct repository *repo, const char *prefix);
204+
void grep_init(struct grep_opt *, struct repository *repo);
187205
void grep_commit_pattern_type(enum grep_pattern_type, struct grep_opt *opt);
188206

189207
void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen, const char *origin, int no, enum grep_pat_token t);

revision.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1838,7 +1838,7 @@ void repo_init_revisions(struct repository *r,
18381838
revs->commit_format = CMIT_FMT_DEFAULT;
18391839
revs->expand_tabs_in_log_default = 8;
18401840

1841-
grep_init(&revs->grep_filter, revs->repo, prefix);
1841+
grep_init(&revs->grep_filter, revs->repo);
18421842
revs->grep_filter.status_only = 1;
18431843

18441844
repo_diff_setup(revs->repo, &revs->diffopt);

t/t4202-log.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,30 @@ test_expect_success !FAIL_PREREQS 'log with various grep.patternType configurati
449449
)
450450
'
451451

452+
for cmd in show whatchanged reflog format-patch
453+
do
454+
case "$cmd" in
455+
format-patch) myarg="HEAD~.." ;;
456+
*) myarg= ;;
457+
esac
458+
459+
test_expect_success "$cmd: understands grep.patternType, like 'log'" '
460+
git init "pattern-type-$cmd" &&
461+
(
462+
cd "pattern-type-$cmd" &&
463+
test_commit 1 file A &&
464+
test_commit "(1|2)" file B 2 &&
465+
466+
git -c grep.patternType=fixed $cmd --grep="..." $myarg >actual &&
467+
test_must_be_empty actual &&
468+
469+
git -c grep.patternType=basic $cmd --grep="..." $myarg >actual &&
470+
test_file_not_empty actual
471+
)
472+
'
473+
done
474+
test_done
475+
452476
test_expect_success 'log --author' '
453477
cat >expect <<-\EOF &&
454478
Author: <BOLD;RED>A U<RESET> Thor <[email protected]>

t/t7810-grep.sh

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,55 @@ do
451451
test_cmp expected actual
452452
'
453453

454+
test_expect_success "grep $L with grep.extendedRegexp is last-one-wins" '
455+
echo "${HC}ab:a+bc" >expected &&
456+
git \
457+
-c grep.extendedRegexp=true \
458+
-c grep.patternType=basic \
459+
-c grep.extendedRegexp=false \
460+
grep "a+b*c" $H ab >actual &&
461+
test_cmp expected actual
462+
'
463+
464+
test_expect_success "grep $L with grep.extendedRegexp is last-one-wins & defers to grep.patternType" '
465+
echo "${HC}ab:abc" >expected &&
466+
git \
467+
-c grep.extendedRegexp=true \
468+
-c grep.patternType=extended \
469+
-c grep.extendedRegexp=false \
470+
grep "a+b*c" $H ab >actual &&
471+
test_cmp expected actual
472+
'
473+
474+
test_expect_success "grep $L with grep.extendedRegexp and grep.patternType are both last-one-wins independently" '
475+
echo "${HC}ab:abc" >expected &&
476+
git \
477+
-c grep.patternType=fixed \
478+
-c grep.extendedRegexp=true \
479+
-c grep.patternType=default \
480+
grep "a+b*c" $H ab >actual &&
481+
test_cmp expected actual
482+
'
483+
484+
test_expect_success "grep $L with grep.patternType=extended and grep.patternType=default" '
485+
echo "${HC}ab:a+bc" >expected &&
486+
git \
487+
-c grep.patternType=extended \
488+
-c grep.patternType=default \
489+
grep "a+b*c" $H ab >actual &&
490+
test_cmp expected actual
491+
'
492+
493+
test_expect_success "grep $L with grep.patternType=[extended -> default -> fixed]" '
494+
echo "${HC}ab:a+b*c" >expected &&
495+
git \
496+
-c grep.patternType=extended \
497+
-c grep.patternType=default \
498+
-c grep.patternType=fixed \
499+
grep "a+b*c" $H ab >actual &&
500+
test_cmp expected actual
501+
'
502+
454503
test_expect_success "grep $L with grep.patternType=extended and grep.extendedRegexp=false" '
455504
echo "${HC}ab:abc" >expected &&
456505
git \

0 commit comments

Comments
 (0)