Skip to content

Commit c5c31d3

Browse files
committed
grep: move pattern-type bits support to top-level grep.[ch]
Switching between -E/-G/-P/-F correctly needs a lot more than just flipping opt->regflags bit these days, and we have a nice helper function buried in builtin/grep.c for the sole use of "git grep". Extract it so that "log --grep" family can also use it. Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 7687a05 commit c5c31d3

File tree

3 files changed

+45
-39
lines changed

3 files changed

+45
-39
lines changed

builtin/grep.c

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -261,38 +261,6 @@ static int wait_all(void)
261261
}
262262
#endif
263263

264-
static void grep_pattern_type_options(const int pattern_type, struct grep_opt *opt)
265-
{
266-
switch (pattern_type) {
267-
case GREP_PATTERN_TYPE_UNSPECIFIED:
268-
/* fall through */
269-
270-
case GREP_PATTERN_TYPE_BRE:
271-
opt->fixed = 0;
272-
opt->pcre = 0;
273-
opt->regflags &= ~REG_EXTENDED;
274-
break;
275-
276-
case GREP_PATTERN_TYPE_ERE:
277-
opt->fixed = 0;
278-
opt->pcre = 0;
279-
opt->regflags |= REG_EXTENDED;
280-
break;
281-
282-
case GREP_PATTERN_TYPE_FIXED:
283-
opt->fixed = 1;
284-
opt->pcre = 0;
285-
opt->regflags &= ~REG_EXTENDED;
286-
break;
287-
288-
case GREP_PATTERN_TYPE_PCRE:
289-
opt->fixed = 0;
290-
opt->pcre = 1;
291-
opt->regflags &= ~REG_EXTENDED;
292-
break;
293-
}
294-
}
295-
296264
static int grep_cmd_config(const char *var, const char *value, void *cb)
297265
{
298266
int st = grep_config(var, value, cb);
@@ -798,13 +766,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
798766
PARSE_OPT_KEEP_DASHDASH |
799767
PARSE_OPT_STOP_AT_NON_OPTION |
800768
PARSE_OPT_NO_INTERNAL_HELP);
801-
802-
if (pattern_type_arg != GREP_PATTERN_TYPE_UNSPECIFIED)
803-
grep_pattern_type_options(pattern_type_arg, &opt);
804-
else if (opt.pattern_type_option != GREP_PATTERN_TYPE_UNSPECIFIED)
805-
grep_pattern_type_options(opt.pattern_type_option, &opt);
806-
else if (opt.extended_regexp_option)
807-
grep_pattern_type_options(GREP_PATTERN_TYPE_ERE, &opt);
769+
grep_commit_pattern_type(pattern_type_arg, &opt);
808770

809771
if (use_index && !startup_info->have_repository)
810772
/* die the same way as if we did it at the beginning */

grep.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,48 @@ void grep_init(struct grep_opt *opt, const char *prefix)
137137
strcpy(opt->color_sep, def->color_sep);
138138
}
139139

140+
void grep_commit_pattern_type(enum grep_pattern_type pattern_type, struct grep_opt *opt)
141+
{
142+
if (pattern_type != GREP_PATTERN_TYPE_UNSPECIFIED)
143+
grep_set_pattern_type_option(pattern_type, opt);
144+
else if (opt->pattern_type_option != GREP_PATTERN_TYPE_UNSPECIFIED)
145+
grep_set_pattern_type_option(opt->pattern_type_option, opt);
146+
else if (opt->extended_regexp_option)
147+
grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE, opt);
148+
}
149+
150+
void grep_set_pattern_type_option(enum grep_pattern_type pattern_type, struct grep_opt *opt)
151+
{
152+
switch (pattern_type) {
153+
case GREP_PATTERN_TYPE_UNSPECIFIED:
154+
/* fall through */
155+
156+
case GREP_PATTERN_TYPE_BRE:
157+
opt->fixed = 0;
158+
opt->pcre = 0;
159+
opt->regflags &= ~REG_EXTENDED;
160+
break;
161+
162+
case GREP_PATTERN_TYPE_ERE:
163+
opt->fixed = 0;
164+
opt->pcre = 0;
165+
opt->regflags |= REG_EXTENDED;
166+
break;
167+
168+
case GREP_PATTERN_TYPE_FIXED:
169+
opt->fixed = 1;
170+
opt->pcre = 0;
171+
opt->regflags &= ~REG_EXTENDED;
172+
break;
173+
174+
case GREP_PATTERN_TYPE_PCRE:
175+
opt->fixed = 0;
176+
opt->pcre = 1;
177+
opt->regflags &= ~REG_EXTENDED;
178+
break;
179+
}
180+
}
181+
140182
static struct grep_pat *create_grep_pat(const char *pat, size_t patlen,
141183
const char *origin, int no,
142184
enum grep_pat_token t,

grep.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ struct grep_opt {
141141
extern void init_grep_defaults(void);
142142
extern int grep_config(const char *var, const char *value, void *);
143143
extern void grep_init(struct grep_opt *, const char *prefix);
144+
void grep_set_pattern_type_option(enum grep_pattern_type, struct grep_opt *opt);
145+
void grep_commit_pattern_type(enum grep_pattern_type, struct grep_opt *opt);
144146

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

0 commit comments

Comments
 (0)