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