summaryrefslogtreecommitdiff
path: root/grep.c
diff options
context:
space:
mode:
Diffstat (limited to 'grep.c')
-rw-r--r--grep.c441
1 files changed, 331 insertions, 110 deletions
diff --git a/grep.c b/grep.c
index 0dbdc1d007..3d7cd0e96f 100644
--- a/grep.c
+++ b/grep.c
@@ -1,4 +1,5 @@
#include "cache.h"
+#include "config.h"
#include "grep.h"
#include "userdiff.h"
#include "xdiff-interface.h"
@@ -12,6 +13,11 @@ static int grep_source_is_binary(struct grep_source *gs);
static struct grep_opt grep_defaults;
+static void std_output(struct grep_opt *opt, const void *buf, size_t size)
+{
+ fwrite(buf, size, 1, stdout);
+}
+
/*
* Initialize the grep_defaults template with hardcoded defaults.
* We could let the compiler do this, but without C99 initializers
@@ -29,10 +35,8 @@ void init_grep_defaults(void)
memset(opt, 0, sizeof(*opt));
opt->relative = 1;
opt->pathname = 1;
- opt->regflags = REG_NEWLINE;
opt->max_depth = -1;
opt->pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED;
- opt->extended_regexp_option = 0;
color_set(opt->color_context, "");
color_set(opt->color_filename, "");
color_set(opt->color_function, "");
@@ -42,6 +46,7 @@ void init_grep_defaults(void)
color_set(opt->color_selected, "");
color_set(opt->color_sep, GIT_COLOR_CYAN);
opt->color = -1;
+ opt->output = std_output;
}
static int parse_pattern_type_arg(const char *opt, const char *arg)
@@ -72,10 +77,7 @@ int grep_config(const char *var, const char *value, void *cb)
return -1;
if (!strcmp(var, "grep.extendedregexp")) {
- if (git_config_bool(var, value))
- opt->extended_regexp_option = 1;
- else
- opt->extended_regexp_option = 0;
+ opt->extended_regexp_option = git_config_bool(var, value);
return 0;
}
@@ -150,8 +152,8 @@ void grep_init(struct grep_opt *opt, const char *prefix)
opt->linenum = def->linenum;
opt->max_depth = def->max_depth;
opt->pathname = def->pathname;
- opt->regflags = def->regflags;
opt->relative = def->relative;
+ opt->output = def->output;
color_set(opt->color_context, def->color_context);
color_set(opt->color_filename, def->color_filename);
@@ -165,32 +167,51 @@ void grep_init(struct grep_opt *opt, const char *prefix)
static void grep_set_pattern_type_option(enum grep_pattern_type pattern_type, struct grep_opt *opt)
{
+ /*
+ * When committing to the pattern type by setting the relevant
+ * fields in grep_opt it's generally not necessary to zero out
+ * the fields we're not choosing, since they won't have been
+ * set by anything. The extended_regexp_option field is the
+ * only exception to this.
+ *
+ * This is because in the process of parsing grep.patternType
+ * & grep.extendedRegexp we set opt->pattern_type_option and
+ * opt->extended_regexp_option, respectively. We then
+ * internally use opt->extended_regexp_option to see if we're
+ * compiling an ERE. It must be unset if that's not actually
+ * the case.
+ */
+ if (pattern_type != GREP_PATTERN_TYPE_ERE &&
+ opt->extended_regexp_option)
+ opt->extended_regexp_option = 0;
+
switch (pattern_type) {
case GREP_PATTERN_TYPE_UNSPECIFIED:
/* fall through */
case GREP_PATTERN_TYPE_BRE:
- opt->fixed = 0;
- opt->pcre = 0;
- opt->regflags &= ~REG_EXTENDED;
break;
case GREP_PATTERN_TYPE_ERE:
- opt->fixed = 0;
- opt->pcre = 0;
- opt->regflags |= REG_EXTENDED;
+ opt->extended_regexp_option = 1;
break;
case GREP_PATTERN_TYPE_FIXED:
opt->fixed = 1;
- opt->pcre = 0;
- opt->regflags &= ~REG_EXTENDED;
break;
case GREP_PATTERN_TYPE_PCRE:
- opt->fixed = 0;
- opt->pcre = 1;
- opt->regflags &= ~REG_EXTENDED;
+#ifdef USE_LIBPCRE2
+ opt->pcre2 = 1;
+#else
+ /*
+ * It's important that pcre1 always be assigned to
+ * even when there's no USE_LIBPCRE* defined. We still
+ * call the PCRE stub function, it just dies with
+ * "cannot use Perl-compatible regexes[...]".
+ */
+ opt->pcre1 = 1;
+#endif
break;
}
}
@@ -202,6 +223,11 @@ void grep_commit_pattern_type(enum grep_pattern_type pattern_type, struct grep_o
else if (opt->pattern_type_option != GREP_PATTERN_TYPE_UNSPECIFIED)
grep_set_pattern_type_option(opt->pattern_type_option, opt);
else if (opt->extended_regexp_option)
+ /*
+ * This branch *must* happen after setting from the
+ * opt->pattern_type_option above, we don't want
+ * grep.extendedRegexp to override grep.patternType!
+ */
grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE, opt);
}
@@ -317,8 +343,32 @@ static NORETURN void compile_regexp_failed(const struct grep_pat *p,
die("%s'%s': %s", where, p->pattern, error);
}
-#ifdef USE_LIBPCRE
-static void compile_pcre_regexp(struct grep_pat *p, const struct grep_opt *opt)
+static int is_fixed(const char *s, size_t len)
+{
+ size_t i;
+
+ for (i = 0; i < len; i++) {
+ if (is_regex_special(s[i]))
+ return 0;
+ }
+
+ return 1;
+}
+
+static int has_null(const char *s, size_t len)
+{
+ /*
+ * regcomp cannot accept patterns with NULs so when using it
+ * we consider any pattern containing a NUL fixed.
+ */
+ if (memchr(s, 0, len))
+ return 1;
+
+ return 0;
+}
+
+#ifdef USE_LIBPCRE1
+static void compile_pcre1_regexp(struct grep_pat *p, const struct grep_opt *opt)
{
const char *error;
int erroffset;
@@ -326,23 +376,36 @@ static void compile_pcre_regexp(struct grep_pat *p, const struct grep_opt *opt)
if (opt->ignore_case) {
if (has_non_ascii(p->pattern))
- p->pcre_tables = pcre_maketables();
+ p->pcre1_tables = pcre_maketables();
options |= PCRE_CASELESS;
}
if (is_utf8_locale() && has_non_ascii(p->pattern))
options |= PCRE_UTF8;
- p->pcre_regexp = pcre_compile(p->pattern, options, &error, &erroffset,
- p->pcre_tables);
- if (!p->pcre_regexp)
+ p->pcre1_regexp = pcre_compile(p->pattern, options, &error, &erroffset,
+ p->pcre1_tables);
+ if (!p->pcre1_regexp)
compile_regexp_failed(p, error);
- p->pcre_extra_info = pcre_study(p->pcre_regexp, 0, &error);
- if (!p->pcre_extra_info && error)
+ p->pcre1_extra_info = pcre_study(p->pcre1_regexp, GIT_PCRE_STUDY_JIT_COMPILE, &error);
+ if (!p->pcre1_extra_info && error)
die("%s", error);
+
+#ifdef GIT_PCRE1_USE_JIT
+ pcre_config(PCRE_CONFIG_JIT, &p->pcre1_jit_on);
+ if (p->pcre1_jit_on == 1) {
+ p->pcre1_jit_stack = pcre_jit_stack_alloc(1, 1024 * 1024);
+ if (!p->pcre1_jit_stack)
+ die("Couldn't allocate PCRE JIT stack");
+ pcre_assign_jit_stack(p->pcre1_extra_info, NULL, p->pcre1_jit_stack);
+ } else if (p->pcre1_jit_on != 0) {
+ die("BUG: The pcre1_jit_on variable should be 0 or 1, not %d",
+ p->pcre1_jit_on);
+ }
+#endif
}
-static int pcrematch(struct grep_pat *p, const char *line, const char *eol,
+static int pcre1match(struct grep_pat *p, const char *line, const char *eol,
regmatch_t *match, int eflags)
{
int ovector[30], ret, flags = 0;
@@ -350,8 +413,19 @@ static int pcrematch(struct grep_pat *p, const char *line, const char *eol,
if (eflags & REG_NOTBOL)
flags |= PCRE_NOTBOL;
- ret = pcre_exec(p->pcre_regexp, p->pcre_extra_info, line, eol - line,
- 0, flags, ovector, ARRAY_SIZE(ovector));
+#ifdef GIT_PCRE1_USE_JIT
+ if (p->pcre1_jit_on) {
+ ret = pcre_jit_exec(p->pcre1_regexp, p->pcre1_extra_info, line,
+ eol - line, 0, flags, ovector,
+ ARRAY_SIZE(ovector), p->pcre1_jit_stack);
+ } else
+#endif
+ {
+ ret = pcre_exec(p->pcre1_regexp, p->pcre1_extra_info, line,
+ eol - line, 0, flags, ovector,
+ ARRAY_SIZE(ovector));
+ }
+
if (ret < 0 && ret != PCRE_ERROR_NOMATCH)
die("pcre_exec failed with error code %d", ret);
if (ret > 0) {
@@ -363,55 +437,191 @@ static int pcrematch(struct grep_pat *p, const char *line, const char *eol,
return ret;
}
-static void free_pcre_regexp(struct grep_pat *p)
+static void free_pcre1_regexp(struct grep_pat *p)
{
- pcre_free(p->pcre_regexp);
- pcre_free(p->pcre_extra_info);
- pcre_free((void *)p->pcre_tables);
+ pcre_free(p->pcre1_regexp);
+#ifdef GIT_PCRE1_USE_JIT
+ if (p->pcre1_jit_on) {
+ pcre_free_study(p->pcre1_extra_info);
+ pcre_jit_stack_free(p->pcre1_jit_stack);
+ } else
+#endif
+ {
+ pcre_free(p->pcre1_extra_info);
+ }
+ pcre_free((void *)p->pcre1_tables);
}
-#else /* !USE_LIBPCRE */
-static void compile_pcre_regexp(struct grep_pat *p, const struct grep_opt *opt)
+#else /* !USE_LIBPCRE1 */
+static void compile_pcre1_regexp(struct grep_pat *p, const struct grep_opt *opt)
{
die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE");
}
-static int pcrematch(struct grep_pat *p, const char *line, const char *eol,
+static int pcre1match(struct grep_pat *p, const char *line, const char *eol,
regmatch_t *match, int eflags)
{
return 1;
}
-static void free_pcre_regexp(struct grep_pat *p)
+static void free_pcre1_regexp(struct grep_pat *p)
{
}
-#endif /* !USE_LIBPCRE */
+#endif /* !USE_LIBPCRE1 */
-static int is_fixed(const char *s, size_t len)
+#ifdef USE_LIBPCRE2
+static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt)
{
- size_t i;
+ int error;
+ PCRE2_UCHAR errbuf[256];
+ PCRE2_SIZE erroffset;
+ int options = PCRE2_MULTILINE;
+ const uint8_t *character_tables = NULL;
+ int jitret;
+ int patinforet;
+ size_t jitsizearg;
- /* regcomp cannot accept patterns with NULs so we
- * consider any pattern containing a NUL fixed.
- */
- if (memchr(s, 0, len))
- return 1;
+ assert(opt->pcre2);
- for (i = 0; i < len; i++) {
- if (is_regex_special(s[i]))
- return 0;
+ p->pcre2_compile_context = NULL;
+
+ if (opt->ignore_case) {
+ if (has_non_ascii(p->pattern)) {
+ character_tables = pcre2_maketables(NULL);
+ p->pcre2_compile_context = pcre2_compile_context_create(NULL);
+ pcre2_set_character_tables(p->pcre2_compile_context, character_tables);
+ }
+ options |= PCRE2_CASELESS;
+ }
+ if (is_utf8_locale() && has_non_ascii(p->pattern))
+ options |= PCRE2_UTF;
+
+ p->pcre2_pattern = pcre2_compile((PCRE2_SPTR)p->pattern,
+ p->patternlen, options, &error, &erroffset,
+ p->pcre2_compile_context);
+
+ if (p->pcre2_pattern) {
+ p->pcre2_match_data = pcre2_match_data_create_from_pattern(p->pcre2_pattern, NULL);
+ if (!p->pcre2_match_data)
+ die("Couldn't allocate PCRE2 match data");
+ } else {
+ pcre2_get_error_message(error, errbuf, sizeof(errbuf));
+ compile_regexp_failed(p, (const char *)&errbuf);
}
+ pcre2_config(PCRE2_CONFIG_JIT, &p->pcre2_jit_on);
+ if (p->pcre2_jit_on == 1) {
+ jitret = pcre2_jit_compile(p->pcre2_pattern, PCRE2_JIT_COMPLETE);
+ if (jitret)
+ die("Couldn't JIT the PCRE2 pattern '%s', got '%d'\n", p->pattern, jitret);
+
+ /*
+ * The pcre2_config(PCRE2_CONFIG_JIT, ...) call just
+ * tells us whether the library itself supports JIT,
+ * but to see whether we're going to be actually using
+ * JIT we need to extract PCRE2_INFO_JITSIZE from the
+ * pattern *after* we do pcre2_jit_compile() above.
+ *
+ * This is because if the pattern contains the
+ * (*NO_JIT) verb (see pcre2syntax(3))
+ * pcre2_jit_compile() will exit early with 0. If we
+ * then proceed to call pcre2_jit_match() further down
+ * the line instead of pcre2_match() we'll either
+ * segfault (pre PCRE 10.31) or run into a fatal error
+ * (post PCRE2 10.31)
+ */
+ patinforet = pcre2_pattern_info(p->pcre2_pattern, PCRE2_INFO_JITSIZE, &jitsizearg);
+ if (patinforet)
+ BUG("pcre2_pattern_info() failed: %d", patinforet);
+ if (jitsizearg == 0) {
+ p->pcre2_jit_on = 0;
+ return;
+ }
+
+ p->pcre2_jit_stack = pcre2_jit_stack_create(1, 1024 * 1024, NULL);
+ if (!p->pcre2_jit_stack)
+ die("Couldn't allocate PCRE2 JIT stack");
+ p->pcre2_match_context = pcre2_match_context_create(NULL);
+ if (!p->pcre2_match_context)
+ die("Couldn't allocate PCRE2 match context");
+ pcre2_jit_stack_assign(p->pcre2_match_context, NULL, p->pcre2_jit_stack);
+ } else if (p->pcre2_jit_on != 0) {
+ die("BUG: The pcre2_jit_on variable should be 0 or 1, not %d",
+ p->pcre1_jit_on);
+ }
+}
+
+static int pcre2match(struct grep_pat *p, const char *line, const char *eol,
+ regmatch_t *match, int eflags)
+{
+ int ret, flags = 0;
+ PCRE2_SIZE *ovector;
+ PCRE2_UCHAR errbuf[256];
+
+ if (eflags & REG_NOTBOL)
+ flags |= PCRE2_NOTBOL;
+
+ if (p->pcre2_jit_on)
+ ret = pcre2_jit_match(p->pcre2_pattern, (unsigned char *)line,
+ eol - line, 0, flags, p->pcre2_match_data,
+ NULL);
+ else
+ ret = pcre2_match(p->pcre2_pattern, (unsigned char *)line,
+ eol - line, 0, flags, p->pcre2_match_data,
+ NULL);
+
+ if (ret < 0 && ret != PCRE2_ERROR_NOMATCH) {
+ pcre2_get_error_message(ret, errbuf, sizeof(errbuf));
+ die("%s failed with error code %d: %s",
+ (p->pcre2_jit_on ? "pcre2_jit_match" : "pcre2_match"), ret,
+ errbuf);
+ }
+ if (ret > 0) {
+ ovector = pcre2_get_ovector_pointer(p->pcre2_match_data);
+ ret = 0;
+ match->rm_so = (int)ovector[0];
+ match->rm_eo = (int)ovector[1];
+ }
+
+ return ret;
+}
+
+static void free_pcre2_pattern(struct grep_pat *p)
+{
+ pcre2_compile_context_free(p->pcre2_compile_context);
+ pcre2_code_free(p->pcre2_pattern);
+ pcre2_match_data_free(p->pcre2_match_data);
+ pcre2_jit_stack_free(p->pcre2_jit_stack);
+ pcre2_match_context_free(p->pcre2_match_context);
+}
+#else /* !USE_LIBPCRE2 */
+static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt)
+{
+ /*
+ * Unreachable until USE_LIBPCRE2 becomes synonymous with
+ * USE_LIBPCRE. See the sibling comment in
+ * grep_set_pattern_type_option().
+ */
+ die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE");
+}
+
+static int pcre2match(struct grep_pat *p, const char *line, const char *eol,
+ regmatch_t *match, int eflags)
+{
return 1;
}
+static void free_pcre2_pattern(struct grep_pat *p)
+{
+}
+#endif /* !USE_LIBPCRE2 */
+
static void compile_fixed_regexp(struct grep_pat *p, struct grep_opt *opt)
{
struct strbuf sb = STRBUF_INIT;
int err;
- int regflags;
+ int regflags = 0;
basic_regex_quote_buf(&sb, p->pattern);
- regflags = opt->regflags & ~REG_EXTENDED;
if (opt->ignore_case)
regflags |= REG_ICASE;
err = regcomp(&p->regexp, sb.buf, regflags);
@@ -428,12 +638,12 @@ static void compile_fixed_regexp(struct grep_pat *p, struct grep_opt *opt)
static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
{
- int icase, ascii_only;
+ int ascii_only;
int err;
+ int regflags = REG_NEWLINE;
p->word_regexp = opt->word_regexp;
p->ignore_case = opt->ignore_case;
- icase = opt->regflags & REG_ICASE || p->ignore_case;
ascii_only = !has_non_ascii(p->pattern);
/*
@@ -448,13 +658,13 @@ static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
* simple string match using kws. p->fixed tells us if we
* want to use kws.
*/
- if (opt->fixed || is_fixed(p->pattern, p->patternlen))
- p->fixed = !icase || ascii_only;
- else
- p->fixed = 0;
+ if (opt->fixed ||
+ has_null(p->pattern, p->patternlen) ||
+ is_fixed(p->pattern, p->patternlen))
+ p->fixed = !p->ignore_case || ascii_only;
if (p->fixed) {
- p->kws = kwsalloc(icase ? tolower_trans_tbl : NULL);
+ p->kws = kwsalloc(p->ignore_case ? tolower_trans_tbl : NULL);
kwsincr(p->kws, p->pattern, p->patternlen);
kwsprep(p->kws);
return;
@@ -468,12 +678,21 @@ static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
return;
}
- if (opt->pcre) {
- compile_pcre_regexp(p, opt);
+ if (opt->pcre2) {
+ compile_pcre2_pattern(p, opt);
+ return;
+ }
+
+ if (opt->pcre1) {
+ compile_pcre1_regexp(p, opt);
return;
}
- err = regcomp(&p->regexp, p->pattern, opt->regflags);
+ if (p->ignore_case)
+ regflags |= REG_ICASE;
+ if (opt->extended_regexp_option)
+ regflags |= REG_EXTENDED;
+ err = regcomp(&p->regexp, p->pattern, regflags);
if (err) {
char errbuf[1024];
regerror(err, &p->regexp, errbuf, 1024);
@@ -825,8 +1044,10 @@ void free_grep_patterns(struct grep_opt *opt)
case GREP_PATTERN_BODY:
if (p->kws)
kwsfree(p->kws);
- else if (p->pcre_regexp)
- free_pcre_regexp(p);
+ else if (p->pcre1_regexp)
+ free_pcre1_regexp(p);
+ else if (p->pcre2_pattern)
+ free_pcre2_pattern(p);
else
regfree(&p->regexp);
free(p->pattern);
@@ -905,8 +1126,10 @@ static int patmatch(struct grep_pat *p, char *line, char *eol,
if (p->fixed)
hit = !fixmatch(p, line, eol, match);
- else if (p->pcre_regexp)
- hit = !pcrematch(p, line, eol, match, eflags);
+ else if (p->pcre1_regexp)
+ hit = !pcre1match(p, line, eol, match, eflags);
+ else if (p->pcre2_pattern)
+ hit = !pcre2match(p, line, eol, match, eflags);
else
hit = !regexec_buf(&p->regexp, line, eol - line, 1, match,
eflags);
@@ -1164,7 +1387,7 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
}
if (opt->linenum) {
char buf[32];
- snprintf(buf, sizeof(buf), "%d", lno);
+ xsnprintf(buf, sizeof(buf), "%d", lno);
output_color(opt, buf, strlen(buf), opt->color_lineno);
output_sep(opt, sign);
}
@@ -1279,31 +1502,52 @@ static void show_funcname_line(struct grep_opt *opt, struct grep_source *gs,
}
}
+static int is_empty_line(const char *bol, const char *eol);
+
static void show_pre_context(struct grep_opt *opt, struct grep_source *gs,
char *bol, char *end, unsigned lno)
{
- unsigned cur = lno, from = 1, funcname_lno = 0;
- int funcname_needed = !!opt->funcname;
-
- if (opt->funcbody && !match_funcname(opt, gs, bol, end))
- funcname_needed = 2;
+ unsigned cur = lno, from = 1, funcname_lno = 0, orig_from;
+ int funcname_needed = !!opt->funcname, comment_needed = 0;
if (opt->pre_context < lno)
from = lno - opt->pre_context;
if (from <= opt->last_shown)
from = opt->last_shown + 1;
+ orig_from = from;
+ if (opt->funcbody) {
+ if (match_funcname(opt, gs, bol, end))
+ comment_needed = 1;
+ else
+ funcname_needed = 1;
+ from = opt->last_shown + 1;
+ }
/* Rewind. */
- while (bol > gs->buf &&
- cur > (funcname_needed == 2 ? opt->last_shown + 1 : from)) {
+ while (bol > gs->buf && cur > from) {
+ char *next_bol = bol;
char *eol = --bol;
while (bol > gs->buf && bol[-1] != '\n')
bol--;
cur--;
+ if (comment_needed && (is_empty_line(bol, eol) ||
+ match_funcname(opt, gs, bol, eol))) {
+ comment_needed = 0;
+ from = orig_from;
+ if (cur < from) {
+ cur++;
+ bol = next_bol;
+ break;
+ }
+ }
if (funcname_needed && match_funcname(opt, gs, bol, eol)) {
funcname_lno = cur;
funcname_needed = 0;
+ if (opt->funcbody)
+ comment_needed = 1;
+ else
+ from = orig_from;
}
}
@@ -1379,11 +1623,6 @@ static int look_ahead(struct grep_opt *opt,
return 0;
}
-static void std_output(struct grep_opt *opt, const void *buf, size_t size)
-{
- fwrite(buf, size, 1, stdout);
-}
-
static int fill_textconv_grep(struct userdiff_driver *driver,
struct grep_source *gs)
{
@@ -1401,11 +1640,11 @@ static int fill_textconv_grep(struct userdiff_driver *driver,
*/
df = alloc_filespec(gs->path);
switch (gs->type) {
- case GREP_SOURCE_SHA1:
+ case GREP_SOURCE_OID:
fill_filespec(df, gs->identifier, 1, 0100644);
break;
case GREP_SOURCE_FILE:
- fill_filespec(df, null_sha1, 0, 0100644);
+ fill_filespec(df, &null_oid, 0, 0100644);
break;
default:
die("BUG: attempt to textconv something without a path?");
@@ -1629,7 +1868,7 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
return 0;
if (opt->status_only)
- return 0;
+ return opt->unmatch_name_only;
if (opt->unmatch_name_only) {
/* We did not see any hit, so we want to show this */
show_name(opt, gs->name);
@@ -1651,7 +1890,7 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
opt->color_filename);
output_sep(opt, ':');
}
- snprintf(buf, sizeof(buf), "%u\n", count);
+ xsnprintf(buf, sizeof(buf), "%u\n", count);
opt->output(opt, buf, strlen(buf));
return 1;
}
@@ -1735,19 +1974,8 @@ void grep_source_init(struct grep_source *gs, enum grep_source_type type,
case GREP_SOURCE_FILE:
gs->identifier = xstrdup(identifier);
break;
- case GREP_SOURCE_SUBMODULE:
- if (!identifier) {
- gs->identifier = NULL;
- break;
- }
- /*
- * FALL THROUGH
- * If the identifier is non-NULL (in the submodule case) it
- * will be a SHA1 that needs to be copied.
- */
- case GREP_SOURCE_SHA1:
- gs->identifier = xmalloc(20);
- hashcpy(gs->identifier, identifier);
+ case GREP_SOURCE_OID:
+ gs->identifier = oiddup(identifier);
break;
case GREP_SOURCE_BUF:
gs->identifier = NULL;
@@ -1757,12 +1985,9 @@ void grep_source_init(struct grep_source *gs, enum grep_source_type type,
void grep_source_clear(struct grep_source *gs)
{
- free(gs->name);
- gs->name = NULL;
- free(gs->path);
- gs->path = NULL;
- free(gs->identifier);
- gs->identifier = NULL;
+ FREE_AND_NULL(gs->name);
+ FREE_AND_NULL(gs->path);
+ FREE_AND_NULL(gs->identifier);
grep_source_clear_data(gs);
}
@@ -1770,10 +1995,8 @@ void grep_source_clear_data(struct grep_source *gs)
{
switch (gs->type) {
case GREP_SOURCE_FILE:
- case GREP_SOURCE_SHA1:
- case GREP_SOURCE_SUBMODULE:
- free(gs->buf);
- gs->buf = NULL;
+ case GREP_SOURCE_OID:
+ FREE_AND_NULL(gs->buf);
gs->size = 0;
break;
case GREP_SOURCE_BUF:
@@ -1782,7 +2005,7 @@ void grep_source_clear_data(struct grep_source *gs)
}
}
-static int grep_source_load_sha1(struct grep_source *gs)
+static int grep_source_load_oid(struct grep_source *gs)
{
enum object_type type;
@@ -1793,7 +2016,7 @@ static int grep_source_load_sha1(struct grep_source *gs)
if (!gs->buf)
return error(_("'%s': unable to read %s"),
gs->name,
- sha1_to_hex(gs->identifier));
+ oid_to_hex(gs->identifier));
return 0;
}
@@ -1839,12 +2062,10 @@ static int grep_source_load(struct grep_source *gs)
switch (gs->type) {
case GREP_SOURCE_FILE:
return grep_source_load_file(gs);
- case GREP_SOURCE_SHA1:
- return grep_source_load_sha1(gs);
+ case GREP_SOURCE_OID:
+ return grep_source_load_oid(gs);
case GREP_SOURCE_BUF:
return gs->buf ? 0 : -1;
- case GREP_SOURCE_SUBMODULE:
- break;
}
die("BUG: invalid grep_source type to load");
}