pickaxe: hoist empty needle check
[gitweb.git] / diffcore-pickaxe.c
index 380a837b5b136ae88d374ae8a7326c87597107fc..61f628c28579a0350065ae0b7725624625e03ba5 100644 (file)
@@ -104,10 +104,10 @@ static int diff_grep(struct diff_filepair *p, struct diff_options *o,
                if (!mf2.ptr)
                        return 0; /* ignore unmerged */
                /* created "two" -- does it have what we are looking for? */
-               hit = !regexec(regexp, p->two->data, 1, &regmatch, 0);
+               hit = !regexec(regexp, mf2.ptr, 1, &regmatch, 0);
        } else if (!mf2.ptr) {
                /* removed "one" -- did it have what we are looking for? */
-               hit = !regexec(regexp, p->one->data, 1, &regmatch, 0);
+               hit = !regexec(regexp, mf1.ptr, 1, &regmatch, 0);
        } else {
                /*
                 * We have both sides; need to run textual diff and see if
@@ -138,8 +138,12 @@ static void diffcore_pickaxe_grep(struct diff_options *o)
 {
        int err;
        regex_t regex;
+       int cflags = REG_EXTENDED | REG_NEWLINE;
 
-       err = regcomp(&regex, o->pickaxe, REG_EXTENDED | REG_NEWLINE);
+       if (DIFF_OPT_TST(o, PICKAXE_IGNORE_CASE))
+               cflags |= REG_ICASE;
+
+       err = regcomp(&regex, o->pickaxe, cflags);
        if (err) {
                char errbuf[1024];
                regerror(err, &regex, errbuf, 1024);
@@ -159,8 +163,6 @@ static unsigned int contains(struct diff_filespec *one, struct diff_options *o,
        unsigned int cnt;
        unsigned long sz;
        const char *data;
-       if (!o->pickaxe[0])
-               return 0;
        if (diff_populate_filespec(one, 0))
                return 0;
 
@@ -202,6 +204,9 @@ static unsigned int contains(struct diff_filespec *one, struct diff_options *o,
 static int has_changes(struct diff_filepair *p, struct diff_options *o,
                       regex_t *regexp, kwset_t kws)
 {
+       if (!o->pickaxe[0])
+               return 0;
+
        if (!DIFF_FILE_VALID(p->one)) {
                if (!DIFF_FILE_VALID(p->two))
                        return 0; /* ignore unmerged */
@@ -237,7 +242,8 @@ static void diffcore_pickaxe_count(struct diff_options *o)
                }
                regexp = &regex;
        } else {
-               kws = kwsalloc(NULL);
+               kws = kwsalloc(DIFF_OPT_TST(o, PICKAXE_IGNORE_CASE)
+                              ? tolower_trans_tbl : NULL);
                kwsincr(kws, needle, len);
                kwsprep(kws);
        }