Skip to content

Commit 46e48c3

Browse files
author
Junio C Hamano
committed
Merge branch 'pb/regex' into next
* pb/regex: Support for pickaxe matching regular expressions
2 parents 00cbdec + d01d8c6 commit 46e48c3

File tree

4 files changed

+57
-16
lines changed

4 files changed

+57
-16
lines changed

Documentation/diff-options.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@
6969
changeset, not just the files that contain the change
7070
in <string>.
7171

72+
--pickaxe-regex::
73+
Make the <string> not a plain string but an extended POSIX
74+
regex to match.
75+
7276
-O<orderfile>::
7377
Output the patch in the order specified in the
7478
<orderfile>, which has one shell glob pattern per line.

diff.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,8 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
883883
options->filter = arg + 14;
884884
else if (!strcmp(arg, "--pickaxe-all"))
885885
options->pickaxe_opts = DIFF_PICKAXE_ALL;
886+
else if (!strcmp(arg, "--pickaxe-regex"))
887+
options->pickaxe_opts = DIFF_PICKAXE_REGEX;
886888
else if (!strncmp(arg, "-B", 2)) {
887889
if ((options->break_opt =
888890
diff_scoreopt_parse(arg)) == -1)

diff.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ extern int diff_setup_done(struct diff_options *);
102102
#define DIFF_DETECT_COPY 2
103103

104104
#define DIFF_PICKAXE_ALL 1
105+
#define DIFF_PICKAXE_REGEX 2
105106

106107
extern void diffcore_std(struct diff_options *);
107108

diffcore-pickaxe.c

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
/*
22
* Copyright (C) 2005 Junio C Hamano
33
*/
4+
#include <regex.h>
5+
46
#include "cache.h"
57
#include "diff.h"
68
#include "diffcore.h"
79

810
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)
1013
{
1114
unsigned int cnt;
1215
unsigned long offset, sz;
@@ -18,15 +21,28 @@ static unsigned int contains(struct diff_filespec *one,
1821
data = one->data;
1922
cnt = 0;
2023

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++;
2832
cnt++;
2933
}
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+
}
3046
}
3147
return cnt;
3248
}
@@ -36,10 +52,24 @@ void diffcore_pickaxe(const char *needle, int opts)
3652
struct diff_queue_struct *q = &diff_queued_diff;
3753
unsigned long len = strlen(needle);
3854
int i, has_changes;
55+
regex_t regex, *regexp = NULL;
3956
struct diff_queue_struct outq;
4057
outq.queue = NULL;
4158
outq.nr = outq.alloc = 0;
4259

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+
4373
if (opts & DIFF_PICKAXE_ALL) {
4474
/* Showing the whole changeset if needle exists */
4575
for (i = has_changes = 0; !has_changes && i < q->nr; i++) {
@@ -48,16 +78,16 @@ void diffcore_pickaxe(const char *needle, int opts)
4878
if (!DIFF_FILE_VALID(p->two))
4979
continue; /* ignore unmerged */
5080
/* created */
51-
if (contains(p->two, needle, len))
81+
if (contains(p->two, needle, len, regexp))
5282
has_changes++;
5383
}
5484
else if (!DIFF_FILE_VALID(p->two)) {
55-
if (contains(p->one, needle, len))
85+
if (contains(p->one, needle, len, regexp))
5686
has_changes++;
5787
}
5888
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))
6191
has_changes++;
6292
}
6393
if (has_changes)
@@ -80,16 +110,16 @@ void diffcore_pickaxe(const char *needle, int opts)
80110
if (!DIFF_FILE_VALID(p->two))
81111
; /* ignore unmerged */
82112
/* created */
83-
else if (contains(p->two, needle, len))
113+
else if (contains(p->two, needle, len, regexp))
84114
has_changes = 1;
85115
}
86116
else if (!DIFF_FILE_VALID(p->two)) {
87-
if (contains(p->one, needle, len))
117+
if (contains(p->one, needle, len, regexp))
88118
has_changes = 1;
89119
}
90120
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))
93123
has_changes = 1;
94124

95125
if (has_changes)
@@ -98,6 +128,10 @@ void diffcore_pickaxe(const char *needle, int opts)
98128
diff_free_filepair(p);
99129
}
100130

131+
if (opts & DIFF_PICKAXE_REGEX) {
132+
regfree(&regex);
133+
}
134+
101135
free(q->queue);
102136
*q = outq;
103137
return;

0 commit comments

Comments
 (0)