diff options
author | René Scharfe <rene.scharfe@lsrfire.ath.cx> | 2009-07-02 00:07:24 +0200 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2009-07-01 19:16:50 -0700 |
commit | 60ecac98ed6f420c6bf823491074219087c749d4 (patch) | |
tree | 480471031679f5d7f88dea0c673412f1ce03890b /grep.c | |
parent | 2944e4e6145bdfcb1a8730d7da671786d72c86ed (diff) | |
download | git-60ecac98ed6f420c6bf823491074219087c749d4.tar.gz |
grep -p: support user defined regular expressions
Respect the userdiff attributes and config settings when looking for
lines with function definitions in git grep -p.
Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'grep.c')
-rw-r--r-- | grep.c | 29 |
1 files changed, 26 insertions, 3 deletions
@@ -1,5 +1,6 @@ #include "cache.h" #include "grep.h" +#include "userdiff.h" #include "xdiff-interface.h" void append_header_grep_pattern(struct grep_opt *opt, enum grep_header_field field, const char *pat) @@ -535,8 +536,15 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol, printf("%.*s\n", rest, bol); } -static int match_funcname(char *bol, char *eol) +static int match_funcname(struct grep_opt *opt, char *bol, char *eol) { + xdemitconf_t *xecfg = opt->priv; + if (xecfg && xecfg->find_func) { + char buf[1]; + return xecfg->find_func(bol, eol - bol, buf, 1, + xecfg->find_func_priv) >= 0; + } + if (bol == eol) return 0; if (isalpha(*bol) || *bol == '_' || *bol == '$') @@ -557,7 +565,7 @@ static void show_funcname_line(struct grep_opt *opt, const char *name, if (lno <= opt->last_shown) break; - if (match_funcname(bol, eol)) { + if (match_funcname(opt, bol, eol)) { show_line(opt, bol, eol, name, lno, '='); break; } @@ -582,7 +590,7 @@ static void show_pre_context(struct grep_opt *opt, const char *name, char *buf, while (bol > buf && bol[-1] != '\n') bol--; cur--; - if (funcname_needed && match_funcname(bol, eol)) { + if (funcname_needed && match_funcname(opt, bol, eol)) { funcname_lno = cur; funcname_needed = 0; } @@ -614,6 +622,7 @@ static int grep_buffer_1(struct grep_opt *opt, const char *name, int binary_match_only = 0; unsigned count = 0; enum grep_context ctx = GREP_CONTEXT_HEAD; + xdemitconf_t xecfg; opt->last_shown = 0; @@ -630,6 +639,17 @@ static int grep_buffer_1(struct grep_opt *opt, const char *name, } } + memset(&xecfg, 0, sizeof(xecfg)); + if (opt->funcname && !opt->unmatch_name_only && !opt->status_only && + !opt->name_only && !binary_match_only && !collect_hits) { + struct userdiff_driver *drv = userdiff_find_by_path(name); + if (drv && drv->funcname.pattern) { + const struct userdiff_funcname *pe = &drv->funcname; + xdiff_set_find_func(&xecfg, pe->pattern, pe->cflags); + opt->priv = &xecfg; + } + } + while (left) { char *eol, ch; int hit; @@ -711,6 +731,9 @@ static int grep_buffer_1(struct grep_opt *opt, const char *name, return 1; } + xdiff_clear_find_func(&xecfg); + opt->priv = NULL; + /* NEEDSWORK: * The real "grep -c foo *.c" gives many "bar.c:0" lines, * which feels mostly useless but sometimes useful. Maybe |