Code

Merge branch 'jk/credentials'
[git.git] / grep.c
diff --git a/grep.c b/grep.c
index 26e8d8ec4cbec60cd5f4e2bef0d99607c7118fb7..486230b511952dcf975199c82a5265d1906999cd 100644 (file)
--- a/grep.c
+++ b/grep.c
@@ -137,16 +137,50 @@ static void free_pcre_regexp(struct grep_pat *p)
 }
 #endif /* !USE_LIBPCRE */
 
+static int is_fixed(const char *s, size_t len)
+{
+       size_t i;
+
+       /* regcomp cannot accept patterns with NULs so we
+        * consider any pattern containing a NUL fixed.
+        */
+       if (memchr(s, 0, len))
+               return 1;
+
+       for (i = 0; i < len; i++) {
+               if (is_regex_special(s[i]))
+                       return 0;
+       }
+
+       return 1;
+}
+
 static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
 {
        int err;
 
        p->word_regexp = opt->word_regexp;
        p->ignore_case = opt->ignore_case;
-       p->fixed = opt->fixed;
 
-       if (p->fixed)
+       if (opt->fixed || is_fixed(p->pattern, p->patternlen))
+               p->fixed = 1;
+       else
+               p->fixed = 0;
+
+       if (p->fixed) {
+               if (opt->regflags & REG_ICASE || p->ignore_case) {
+                       static char trans[256];
+                       int i;
+                       for (i = 0; i < 256; i++)
+                               trans[i] = tolower(i);
+                       p->kws = kwsalloc(trans);
+               } else {
+                       p->kws = kwsalloc(NULL);
+               }
+               kwsincr(p->kws, p->pattern, p->patternlen);
+               kwsprep(p->kws);
                return;
+       }
 
        if (opt->pcre) {
                compile_pcre_regexp(p, opt);
@@ -395,7 +429,9 @@ void free_grep_patterns(struct grep_opt *opt)
                case GREP_PATTERN: /* atom */
                case GREP_PATTERN_HEAD:
                case GREP_PATTERN_BODY:
-                       if (p->pcre_regexp)
+                       if (p->kws)
+                               kwsfree(p->kws);
+                       else if (p->pcre_regexp)
                                free_pcre_regexp(p);
                        else
                                regfree(&p->regexp);
@@ -430,7 +466,7 @@ static int word_char(char ch)
 static void output_color(struct grep_opt *opt, const void *data, size_t size,
                         const char *color)
 {
-       if (opt->color && color && color[0]) {
+       if (want_color(opt->color) && color && color[0]) {
                opt->output(opt, color, strlen(color));
                opt->output(opt, data, size);
                opt->output(opt, GIT_COLOR_RESET, strlen(GIT_COLOR_RESET));
@@ -455,26 +491,14 @@ static void show_name(struct grep_opt *opt, const char *name)
 static int fixmatch(struct grep_pat *p, char *line, char *eol,
                    regmatch_t *match)
 {
-       char *hit;
-
-       if (p->ignore_case) {
-               char *s = line;
-               do {
-                       hit = strcasestr(s, p->pattern);
-                       if (hit)
-                               break;
-                       s += strlen(s) + 1;
-               } while (s < eol);
-       } else
-               hit = memmem(line, eol - line, p->pattern, p->patternlen);
-
-       if (!hit) {
+       struct kwsmatch kwsm;
+       size_t offset = kwsexec(p->kws, line, eol - line, &kwsm);
+       if (offset == -1) {
                match->rm_so = match->rm_eo = -1;
                return REG_NOMATCH;
-       }
-       else {
-               match->rm_so = hit - line;
-               match->rm_eo = match->rm_so + p->patternlen;
+       } else {
+               match->rm_so = offset;
+               match->rm_eo = match->rm_so + kwsm.size[0];
                return 0;
        }
 }
@@ -782,10 +806,46 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
        opt->output(opt, "\n", 1);
 }
 
-static int match_funcname(struct grep_opt *opt, char *bol, char *eol)
+#ifndef NO_PTHREADS
+/*
+ * This lock protects access to the gitattributes machinery, which is
+ * not thread-safe.
+ */
+pthread_mutex_t grep_attr_mutex;
+
+static inline void grep_attr_lock(struct grep_opt *opt)
+{
+       if (opt->use_threads)
+               pthread_mutex_lock(&grep_attr_mutex);
+}
+
+static inline void grep_attr_unlock(struct grep_opt *opt)
+{
+       if (opt->use_threads)
+               pthread_mutex_unlock(&grep_attr_mutex);
+}
+#else
+#define grep_attr_lock(opt)
+#define grep_attr_unlock(opt)
+#endif
+
+static int match_funcname(struct grep_opt *opt, const char *name, char *bol, char *eol)
 {
        xdemitconf_t *xecfg = opt->priv;
-       if (xecfg && xecfg->find_func) {
+       if (xecfg && !xecfg->find_func) {
+               struct userdiff_driver *drv;
+               grep_attr_lock(opt);
+               drv = userdiff_find_by_path(name);
+               grep_attr_unlock(opt);
+               if (drv && drv->funcname.pattern) {
+                       const struct userdiff_funcname *pe = &drv->funcname;
+                       xdiff_set_find_func(xecfg, pe->pattern, pe->cflags);
+               } else {
+                       xecfg = opt->priv = NULL;
+               }
+       }
+
+       if (xecfg) {
                char buf[1];
                return xecfg->find_func(bol, eol - bol, buf, 1,
                                        xecfg->find_func_priv) >= 0;
@@ -811,7 +871,7 @@ static void show_funcname_line(struct grep_opt *opt, const char *name,
                if (lno <= opt->last_shown)
                        break;
 
-               if (match_funcname(opt, bol, eol)) {
+               if (match_funcname(opt, name, bol, eol)) {
                        show_line(opt, bol, eol, name, lno, '=');
                        break;
                }
@@ -824,7 +884,7 @@ static void show_pre_context(struct grep_opt *opt, const char *name, char *buf,
        unsigned cur = lno, from = 1, funcname_lno = 0;
        int funcname_needed = !!opt->funcname;
 
-       if (opt->funcbody && !match_funcname(opt, bol, end))
+       if (opt->funcbody && !match_funcname(opt, name, bol, end))
                funcname_needed = 2;
 
        if (opt->pre_context < lno)
@@ -840,7 +900,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(opt, bol, eol)) {
+               if (funcname_needed && match_funcname(opt, name, bol, eol)) {
                        funcname_lno = cur;
                        funcname_needed = 0;
                }
@@ -918,19 +978,6 @@ static int look_ahead(struct grep_opt *opt,
        return 0;
 }
 
-int grep_threads_ok(const struct grep_opt *opt)
-{
-       /* If this condition is true, then we may use the attribute
-        * machinery in grep_buffer_1. The attribute code is not
-        * thread safe, so we disable the use of threads.
-        */
-       if (opt->funcname && !opt->unmatch_name_only && !opt->status_only &&
-           !opt->name_only)
-               return 0;
-
-       return 1;
-}
-
 static void std_output(struct grep_opt *opt, const void *buf, size_t size)
 {
        fwrite(buf, size, 1, stdout);
@@ -984,15 +1031,8 @@ 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;
-               }
-       }
+       opt->priv = &xecfg;
+
        try_lookahead = should_lookahead(opt);
 
        while (left) {
@@ -1068,7 +1108,7 @@ static int grep_buffer_1(struct grep_opt *opt, const char *name,
                                show_function = 1;
                        goto next_line;
                }
-               if (show_function && match_funcname(opt, bol, eol))
+               if (show_function && match_funcname(opt, name, bol, eol))
                        show_function = 0;
                if (show_function ||
                    (last_hit && lno <= last_hit + opt->post_context)) {