Code

Support for pickaxe matching regular expressions
authorPetr Baudis <pasky@suse.cz>
Wed, 29 Mar 2006 00:16:33 +0000 (02:16 +0200)
committerJunio C Hamano <junkio@cox.net>
Tue, 4 Apr 2006 20:44:15 +0000 (13:44 -0700)
git-diff-* --pickaxe-regex will change the -S pickaxe to match
POSIX extended regular expressions instead of fixed strings.

The regex.h library is a rather stupid interface and I like pcre too, but
with any luck it will be everywhere we will want to run Git on, it being
POSIX.2 and all. I'm not sure if we can expect platforms like AIX to
conform to POSIX.2 or if win32 has regex.h. We might add a flag to
Makefile if there is a portability trouble potential.

Signed-off-by: Petr Baudis <pasky@suse.cz>
Documentation/diff-options.txt
diff.c
diff.h
diffcore-pickaxe.c

index 2a0275eeda19b7af50e22f0709e6a31afee10b5e..ec6811c71885a9aa0a28626c46e0645cd79864ac 100644 (file)
        changeset, not just the files that contain the change
        in <string>.
 
+--pickaxe-regex::
+       Make the <string> not a plain string but an extended POSIX
+       regex to match.
+
 -O<orderfile>::
        Output the patch in the order specified in the
        <orderfile>, which has one shell glob pattern per line.
diff --git a/diff.c b/diff.c
index e496905bad9853f912e1bbcc081c544524f8cb77..2db2cc568dae9e94f56343cb1c900a2da195c9d0 100644 (file)
--- a/diff.c
+++ b/diff.c
@@ -883,6 +883,8 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
                options->filter = arg + 14;
        else if (!strcmp(arg, "--pickaxe-all"))
                options->pickaxe_opts = DIFF_PICKAXE_ALL;
+       else if (!strcmp(arg, "--pickaxe-regex"))
+               options->pickaxe_opts = DIFF_PICKAXE_REGEX;
        else if (!strncmp(arg, "-B", 2)) {
                if ((options->break_opt =
                     diff_scoreopt_parse(arg)) == -1)
diff --git a/diff.h b/diff.h
index a268d16ff712e29fbccab779824551757f99837d..0cebec113f69e05debfe3ab20327aa6c4d0a3020 100644 (file)
--- a/diff.h
+++ b/diff.h
@@ -102,6 +102,7 @@ extern int diff_setup_done(struct diff_options *);
 #define DIFF_DETECT_COPY       2
 
 #define DIFF_PICKAXE_ALL       1
+#define DIFF_PICKAXE_REGEX     2
 
 extern void diffcore_std(struct diff_options *);
 
index 50e46ab863f71574622df4790ea33a8e521f2471..d89f314f6d809ebfdfd95104fc653bbfe048b669 100644 (file)
@@ -1,12 +1,15 @@
 /*
  * Copyright (C) 2005 Junio C Hamano
  */
+#include <regex.h>
+
 #include "cache.h"
 #include "diff.h"
 #include "diffcore.h"
 
 static unsigned int contains(struct diff_filespec *one,
-                            const char *needle, unsigned long len)
+                            const char *needle, unsigned long len,
+                            regex_t *regexp)
 {
        unsigned int cnt;
        unsigned long offset, sz;
@@ -18,15 +21,28 @@ static unsigned int contains(struct diff_filespec *one,
        data = one->data;
        cnt = 0;
 
-       /* Yes, I've heard of strstr(), but the thing is *data may
-        * not be NUL terminated.  Sue me.
-        */
-       for (offset = 0; offset + len <= sz; offset++) {
-               /* we count non-overlapping occurrences of needle */
-               if (!memcmp(needle, data + offset, len)) {
-                       offset += len - 1;
+       if (regexp) {
+               regmatch_t regmatch;
+               int flags = 0;
+
+               while (*data && !regexec(regexp, data, 1, &regmatch, flags)) {
+                       flags |= REG_NOTBOL;
+                       data += regmatch.rm_so;
+                       if (*data) data++;
                        cnt++;
                }
+
+       } else { /* Classic exact string match */
+               /* Yes, I've heard of strstr(), but the thing is *data may
+                * not be NUL terminated.  Sue me.
+                */
+               for (offset = 0; offset + len <= sz; offset++) {
+                       /* we count non-overlapping occurrences of needle */
+                       if (!memcmp(needle, data + offset, len)) {
+                               offset += len - 1;
+                               cnt++;
+                       }
+               }
        }
        return cnt;
 }
@@ -36,10 +52,24 @@ void diffcore_pickaxe(const char *needle, int opts)
        struct diff_queue_struct *q = &diff_queued_diff;
        unsigned long len = strlen(needle);
        int i, has_changes;
+       regex_t regex, *regexp = NULL;
        struct diff_queue_struct outq;
        outq.queue = NULL;
        outq.nr = outq.alloc = 0;
 
+       if (opts & DIFF_PICKAXE_REGEX) {
+               int err;
+               err = regcomp(&regex, needle, REG_EXTENDED | REG_NEWLINE);
+               if (err) {
+                       /* The POSIX.2 people are surely sick */
+                       char errbuf[1024];
+                       regerror(err, &regex, errbuf, 1024);
+                       regfree(&regex);
+                       die("invalid pickaxe regex: %s", errbuf);
+               }
+               regexp = &regex;
+       }
+
        if (opts & DIFF_PICKAXE_ALL) {
                /* Showing the whole changeset if needle exists */
                for (i = has_changes = 0; !has_changes && i < q->nr; i++) {
@@ -48,16 +78,16 @@ void diffcore_pickaxe(const char *needle, int opts)
                                if (!DIFF_FILE_VALID(p->two))
                                        continue; /* ignore unmerged */
                                /* created */
-                               if (contains(p->two, needle, len))
+                               if (contains(p->two, needle, len, regexp))
                                        has_changes++;
                        }
                        else if (!DIFF_FILE_VALID(p->two)) {
-                               if (contains(p->one, needle, len))
+                               if (contains(p->one, needle, len, regexp))
                                        has_changes++;
                        }
                        else if (!diff_unmodified_pair(p) &&
-                                contains(p->one, needle, len) !=
-                                contains(p->two, needle, len))
+                                contains(p->one, needle, len, regexp) !=
+                                contains(p->two, needle, len, regexp))
                                has_changes++;
                }
                if (has_changes)
@@ -80,16 +110,16 @@ void diffcore_pickaxe(const char *needle, int opts)
                                if (!DIFF_FILE_VALID(p->two))
                                        ; /* ignore unmerged */
                                /* created */
-                               else if (contains(p->two, needle, len))
+                               else if (contains(p->two, needle, len, regexp))
                                        has_changes = 1;
                        }
                        else if (!DIFF_FILE_VALID(p->two)) {
-                               if (contains(p->one, needle, len))
+                               if (contains(p->one, needle, len, regexp))
                                        has_changes = 1;
                        }
                        else if (!diff_unmodified_pair(p) &&
-                                contains(p->one, needle, len) !=
-                                contains(p->two, needle, len))
+                                contains(p->one, needle, len, regexp) !=
+                                contains(p->two, needle, len, regexp))
                                has_changes = 1;
 
                        if (has_changes)
@@ -98,6 +128,10 @@ void diffcore_pickaxe(const char *needle, int opts)
                                diff_free_filepair(p);
                }
 
+       if (opts & DIFF_PICKAXE_REGEX) {
+               regfree(&regex);
+       }
+
        free(q->queue);
        *q = outq;
        return;