@matheusbsbs / matheus-bagliano / commits / d01d8c6782
Fork 0

Support for pickaxe matching regular expressions

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>

Petr Baudis committed Mar 29, 2006 at 02:16 UTC d01d8c6782850c18d62676dae3c72ad73be1e52e
4 files changed +57 -16
Documentation/diff-options.txt
+4
@@ -69,6 +69,10 @@
69 changeset, not just the files that contain the change
70 in <string>.
71
72 +--pickaxe-regex::
73 + Make the <string> not a plain string but an extended POSIX
74 + regex to match.
75 +
76 -O<orderfile>::
77 Output the patch in the order specified in the
78 <orderfile>, which has one shell glob pattern per line.
diff.c
+2
@@ -883,6 +883,8 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
883 options->filter = arg + 14;
884 else if (!strcmp(arg, "--pickaxe-all"))
885 options->pickaxe_opts = DIFF_PICKAXE_ALL;
886 + else if (!strcmp(arg, "--pickaxe-regex"))
887 + options->pickaxe_opts = DIFF_PICKAXE_REGEX;
888 else if (!strncmp(arg, "-B", 2)) {
889 if ((options->break_opt =
890 diff_scoreopt_parse(arg)) == -1)
diff.h
+1
@@ -102,6 +102,7 @@ extern int diff_setup_done(struct diff_options *);
102 #define DIFF_DETECT_COPY 2
103
104 #define DIFF_PICKAXE_ALL 1
105 +#define DIFF_PICKAXE_REGEX 2
106
107 extern void diffcore_std(struct diff_options *);
108
diffcore-pickaxe.c
+50 -16
@@ -1,12 +1,15 @@
1 /*
2 * Copyright (C) 2005 Junio C Hamano
3 */
4 +#include <regex.h>
5 +
6 #include "cache.h"
7 #include "diff.h"
8 #include "diffcore.h"
9
10 static unsigned int contains(struct diff_filespec *one,
9 - const char *needle, unsigned long len)
11 + const char *needle, unsigned long len,
12 + regex_t *regexp)
13 {
14 unsigned int cnt;
15 unsigned long offset, sz;
@@ -18,15 +21,28 @@ static unsigned int contains(struct diff_filespec *one,
21 data = one->data;
22 cnt = 0;
23
21 - /* Yes, I've heard of strstr(), but the thing is *data may
22 - * not be NUL terminated. Sue me.
23 - */
24 - for (offset = 0; offset + len <= sz; offset++) {
25 - /* we count non-overlapping occurrences of needle */
26 - if (!memcmp(needle, data + offset, len)) {
27 - offset += len - 1;
24 + if (regexp) {
25 + regmatch_t regmatch;
26 + int flags = 0;
27 +
28 + while (*data && !regexec(regexp, data, 1, &regmatch, flags)) {
29 + flags |= REG_NOTBOL;
30 + data += regmatch.rm_so;
31 + if (*data) data++;
32 cnt++;
33 }
34 +
35 + } else { /* Classic exact string match */
36 + /* Yes, I've heard of strstr(), but the thing is *data may
37 + * not be NUL terminated. Sue me.
38 + */
39 + for (offset = 0; offset + len <= sz; offset++) {
40 + /* we count non-overlapping occurrences of needle */
41 + if (!memcmp(needle, data + offset, len)) {
42 + offset += len - 1;
43 + cnt++;
44 + }
45 + }
46 }
47 return cnt;
48 }
@@ -36,10 +52,24 @@ void diffcore_pickaxe(const char *needle, int opts)
52 struct diff_queue_struct *q = &diff_queued_diff;
53 unsigned long len = strlen(needle);
54 int i, has_changes;
55 + regex_t regex, *regexp = NULL;
56 struct diff_queue_struct outq;
57 outq.queue = NULL;
58 outq.nr = outq.alloc = 0;
59
60 + if (opts & DIFF_PICKAXE_REGEX) {
61 + int err;
62 + err = regcomp(&regex, needle, REG_EXTENDED | REG_NEWLINE);
63 + if (err) {
64 + /* The POSIX.2 people are surely sick */
65 + char errbuf[1024];
66 + regerror(err, &regex, errbuf, 1024);
67 + regfree(&regex);
68 + die("invalid pickaxe regex: %s", errbuf);
69 + }
70 + regexp = &regex;
71 + }
72 +
73 if (opts & DIFF_PICKAXE_ALL) {
74 /* Showing the whole changeset if needle exists */
75 for (i = has_changes = 0; !has_changes && i < q->nr; i++) {
@@ -48,16 +78,16 @@ void diffcore_pickaxe(const char *needle, int opts)
78 if (!DIFF_FILE_VALID(p->two))
79 continue; /* ignore unmerged */
80 /* created */
51 - if (contains(p->two, needle, len))
81 + if (contains(p->two, needle, len, regexp))
82 has_changes++;
83 }
84 else if (!DIFF_FILE_VALID(p->two)) {
55 - if (contains(p->one, needle, len))
85 + if (contains(p->one, needle, len, regexp))
86 has_changes++;
87 }
88 else if (!diff_unmodified_pair(p) &&
59 - contains(p->one, needle, len) !=
60 - contains(p->two, needle, len))
89 + contains(p->one, needle, len, regexp) !=
90 + contains(p->two, needle, len, regexp))
91 has_changes++;
92 }
93 if (has_changes)
@@ -80,16 +110,16 @@ void diffcore_pickaxe(const char *needle, int opts)
110 if (!DIFF_FILE_VALID(p->two))
111 ; /* ignore unmerged */
112 /* created */
83 - else if (contains(p->two, needle, len))
113 + else if (contains(p->two, needle, len, regexp))
114 has_changes = 1;
115 }
116 else if (!DIFF_FILE_VALID(p->two)) {
87 - if (contains(p->one, needle, len))
117 + if (contains(p->one, needle, len, regexp))
118 has_changes = 1;
119 }
120 else if (!diff_unmodified_pair(p) &&
91 - contains(p->one, needle, len) !=
92 - contains(p->two, needle, len))
121 + contains(p->one, needle, len, regexp) !=
122 + contains(p->two, needle, len, regexp))
123 has_changes = 1;
124
125 if (has_changes)
@@ -98,6 +128,10 @@ void diffcore_pickaxe(const char *needle, int opts)
128 diff_free_filepair(p);
129 }
130
131 + if (opts & DIFF_PICKAXE_REGEX) {
132 + regfree(&regex);
133 + }
134 +
135 free(q->queue);
136 *q = outq;
137 return;