diff: pass the entire diff-options to diffcore_pickaxe()
[git/kirr.git] / diffcore-pickaxe.c
blob13f61fbb083747adf41be8e01951c55055ee20c2
1 /*
2 * Copyright (C) 2005 Junio C Hamano
3 */
4 #include "cache.h"
5 #include "diff.h"
6 #include "diffcore.h"
8 static unsigned int contains(struct diff_filespec *one,
9 const char *needle, unsigned long len,
10 regex_t *regexp)
12 unsigned int cnt;
13 unsigned long sz;
14 const char *data;
15 if (diff_populate_filespec(one, 0))
16 return 0;
17 if (!len)
18 return 0;
20 sz = one->size;
21 data = one->data;
22 cnt = 0;
24 if (regexp) {
25 regmatch_t regmatch;
26 int flags = 0;
28 assert(data[sz] == '\0');
29 while (*data && !regexec(regexp, data, 1, &regmatch, flags)) {
30 flags |= REG_NOTBOL;
31 data += regmatch.rm_eo;
32 if (*data && regmatch.rm_so == regmatch.rm_eo)
33 data++;
34 cnt++;
37 } else { /* Classic exact string match */
38 while (sz) {
39 const char *found = memmem(data, sz, needle, len);
40 if (!found)
41 break;
42 sz -= found - data + len;
43 data = found + len;
44 cnt++;
47 diff_free_filespec_data(one);
48 return cnt;
51 void diffcore_pickaxe(struct diff_options *o)
53 const char *needle = o->pickaxe;
54 int opts = o->pickaxe_opts;
55 struct diff_queue_struct *q = &diff_queued_diff;
56 unsigned long len = strlen(needle);
57 int i, has_changes;
58 regex_t regex, *regexp = NULL;
59 struct diff_queue_struct outq;
60 DIFF_QUEUE_CLEAR(&outq);
62 if (opts & DIFF_PICKAXE_REGEX) {
63 int err;
64 err = regcomp(&regex, needle, REG_EXTENDED | REG_NEWLINE);
65 if (err) {
66 /* The POSIX.2 people are surely sick */
67 char errbuf[1024];
68 regerror(err, &regex, errbuf, 1024);
69 regfree(&regex);
70 die("invalid pickaxe regex: %s", errbuf);
72 regexp = &regex;
75 if (opts & DIFF_PICKAXE_ALL) {
76 /* Showing the whole changeset if needle exists */
77 for (i = has_changes = 0; !has_changes && i < q->nr; i++) {
78 struct diff_filepair *p = q->queue[i];
79 if (!DIFF_FILE_VALID(p->one)) {
80 if (!DIFF_FILE_VALID(p->two))
81 continue; /* ignore unmerged */
82 /* created */
83 if (contains(p->two, needle, len, regexp))
84 has_changes++;
86 else if (!DIFF_FILE_VALID(p->two)) {
87 if (contains(p->one, needle, len, regexp))
88 has_changes++;
90 else if (!diff_unmodified_pair(p) &&
91 contains(p->one, needle, len, regexp) !=
92 contains(p->two, needle, len, regexp))
93 has_changes++;
95 if (has_changes)
96 return; /* not munge the queue */
98 /* otherwise we will clear the whole queue
99 * by copying the empty outq at the end of this
100 * function, but first clear the current entries
101 * in the queue.
103 for (i = 0; i < q->nr; i++)
104 diff_free_filepair(q->queue[i]);
106 else
107 /* Showing only the filepairs that has the needle */
108 for (i = 0; i < q->nr; i++) {
109 struct diff_filepair *p = q->queue[i];
110 has_changes = 0;
111 if (!DIFF_FILE_VALID(p->one)) {
112 if (!DIFF_FILE_VALID(p->two))
113 ; /* ignore unmerged */
114 /* created */
115 else if (contains(p->two, needle, len, regexp))
116 has_changes = 1;
118 else if (!DIFF_FILE_VALID(p->two)) {
119 if (contains(p->one, needle, len, regexp))
120 has_changes = 1;
122 else if (!diff_unmodified_pair(p) &&
123 contains(p->one, needle, len, regexp) !=
124 contains(p->two, needle, len, regexp))
125 has_changes = 1;
127 if (has_changes)
128 diff_q(&outq, p);
129 else
130 diff_free_filepair(p);
133 if (opts & DIFF_PICKAXE_REGEX) {
134 regfree(&regex);
137 free(q->queue);
138 *q = outq;
139 return;