-
Notifications
You must be signed in to change notification settings - Fork 0
/
diffcore-pickaxe.c
139 lines (127 loc) · 3.2 KB
/
diffcore-pickaxe.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
* Copyright (C) 2005 Junio C Hamano
*/
#include "cache.h"
#include "diff.h"
#include "diffcore.h"
static unsigned int contains(struct diff_filespec *one,
const char *needle, unsigned long len,
regex_t *regexp)
{
unsigned int cnt;
unsigned long sz;
const char *data;
if (diff_populate_filespec(one, 0))
return 0;
if (!len)
return 0;
sz = one->size;
data = one->data;
cnt = 0;
if (regexp) {
regmatch_t regmatch;
int flags = 0;
assert(data[sz] == '\0');
while (*data && !regexec(regexp, data, 1, ®match, flags)) {
flags |= REG_NOTBOL;
data += regmatch.rm_eo;
if (*data && regmatch.rm_so == regmatch.rm_eo)
data++;
cnt++;
}
} else { /* Classic exact string match */
while (sz) {
const char *found = memmem(data, sz, needle, len);
if (!found)
break;
sz -= found - data + len;
data = found + len;
cnt++;
}
}
diff_free_filespec_data(one);
return cnt;
}
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(®ex, needle, REG_EXTENDED | REG_NEWLINE);
if (err) {
/* The POSIX.2 people are surely sick */
char errbuf[1024];
regerror(err, ®ex, errbuf, 1024);
regfree(®ex);
die("invalid pickaxe regex: %s", errbuf);
}
regexp = ®ex;
}
if (opts & DIFF_PICKAXE_ALL) {
/* Showing the whole changeset if needle exists */
for (i = has_changes = 0; !has_changes && i < q->nr; i++) {
struct diff_filepair *p = q->queue[i];
if (!DIFF_FILE_VALID(p->one)) {
if (!DIFF_FILE_VALID(p->two))
continue; /* ignore unmerged */
/* created */
if (contains(p->two, needle, len, regexp))
has_changes++;
}
else if (!DIFF_FILE_VALID(p->two)) {
if (contains(p->one, needle, len, regexp))
has_changes++;
}
else if (!diff_unmodified_pair(p) &&
contains(p->one, needle, len, regexp) !=
contains(p->two, needle, len, regexp))
has_changes++;
}
if (has_changes)
return; /* not munge the queue */
/* otherwise we will clear the whole queue
* by copying the empty outq at the end of this
* function, but first clear the current entries
* in the queue.
*/
for (i = 0; i < q->nr; i++)
diff_free_filepair(q->queue[i]);
}
else
/* Showing only the filepairs that has the needle */
for (i = 0; i < q->nr; i++) {
struct diff_filepair *p = q->queue[i];
has_changes = 0;
if (!DIFF_FILE_VALID(p->one)) {
if (!DIFF_FILE_VALID(p->two))
; /* ignore unmerged */
/* created */
else if (contains(p->two, needle, len, regexp))
has_changes = 1;
}
else if (!DIFF_FILE_VALID(p->two)) {
if (contains(p->one, needle, len, regexp))
has_changes = 1;
}
else if (!diff_unmodified_pair(p) &&
contains(p->one, needle, len, regexp) !=
contains(p->two, needle, len, regexp))
has_changes = 1;
if (has_changes)
diff_q(&outq, p);
else
diff_free_filepair(p);
}
if (opts & DIFF_PICKAXE_REGEX) {
regfree(®ex);
}
free(q->queue);
*q = outq;
return;
}