]> git.ipfire.org Git - thirdparty/git.git/blame - diffcore-pickaxe.c
git-instaweb: fix lighttpd configuration on cygwin
[thirdparty/git.git] / diffcore-pickaxe.c
CommitLineData
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 8static 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;
52e95789
JH
13 unsigned long offset, sz;
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, &regmatch, 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 */
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 }
2002eed6 46 }
a0cb9400 47 diff_free_filespec_data(one);
2002eed6 48 return cnt;
52e95789
JH
49}
50
367cec1c 51void diffcore_pickaxe(const char *needle, int opts)
52e95789 52{
38c6f780 53 struct diff_queue_struct *q = &diff_queued_diff;
52e95789 54 unsigned long len = strlen(needle);
367cec1c 55 int i, has_changes;
d01d8c67 56 regex_t regex, *regexp = NULL;
52e95789
JH
57 struct diff_queue_struct outq;
58 outq.queue = NULL;
59 outq.nr = outq.alloc = 0;
60
d01d8c67
PB
61 if (opts & DIFF_PICKAXE_REGEX) {
62 int err;
63 err = regcomp(&regex, needle, REG_EXTENDED | REG_NEWLINE);
64 if (err) {
65 /* The POSIX.2 people are surely sick */
66 char errbuf[1024];
67 regerror(err, &regex, errbuf, 1024);
68 regfree(&regex);
69 die("invalid pickaxe regex: %s", errbuf);
70 }
71 regexp = &regex;
72 }
73
367cec1c
JH
74 if (opts & DIFF_PICKAXE_ALL) {
75 /* Showing the whole changeset if needle exists */
76 for (i = has_changes = 0; !has_changes && i < q->nr; i++) {
77 struct diff_filepair *p = q->queue[i];
78 if (!DIFF_FILE_VALID(p->one)) {
79 if (!DIFF_FILE_VALID(p->two))
80 continue; /* ignore unmerged */
81 /* created */
d01d8c67 82 if (contains(p->two, needle, len, regexp))
367cec1c
JH
83 has_changes++;
84 }
85 else if (!DIFF_FILE_VALID(p->two)) {
d01d8c67 86 if (contains(p->one, needle, len, regexp))
367cec1c
JH
87 has_changes++;
88 }
89 else if (!diff_unmodified_pair(p) &&
d01d8c67
PB
90 contains(p->one, needle, len, regexp) !=
91 contains(p->two, needle, len, regexp))
367cec1c 92 has_changes++;
52e95789 93 }
367cec1c
JH
94 if (has_changes)
95 return; /* not munge the queue */
96
97 /* otherwise we will clear the whole queue
98 * by copying the empty outq at the end of this
99 * function, but first clear the current entries
100 * in the queue.
101 */
102 for (i = 0; i < q->nr; i++)
103 diff_free_filepair(q->queue[i]);
104 }
a6080a0a 105 else
367cec1c
JH
106 /* Showing only the filepairs that has the needle */
107 for (i = 0; i < q->nr; i++) {
108 struct diff_filepair *p = q->queue[i];
109 has_changes = 0;
110 if (!DIFF_FILE_VALID(p->one)) {
111 if (!DIFF_FILE_VALID(p->two))
112 ; /* ignore unmerged */
113 /* created */
d01d8c67 114 else if (contains(p->two, needle, len, regexp))
367cec1c
JH
115 has_changes = 1;
116 }
117 else if (!DIFF_FILE_VALID(p->two)) {
d01d8c67 118 if (contains(p->one, needle, len, regexp))
367cec1c
JH
119 has_changes = 1;
120 }
121 else if (!diff_unmodified_pair(p) &&
d01d8c67
PB
122 contains(p->one, needle, len, regexp) !=
123 contains(p->two, needle, len, regexp))
367cec1c
JH
124 has_changes = 1;
125
126 if (has_changes)
6b14d7fa 127 diff_q(&outq, p);
367cec1c
JH
128 else
129 diff_free_filepair(p);
52e95789 130 }
367cec1c 131
d01d8c67
PB
132 if (opts & DIFF_PICKAXE_REGEX) {
133 regfree(&regex);
134 }
135
52e95789
JH
136 free(q->queue);
137 *q = outq;
138 return;
139}