diff: pass the entire diff-options to diffcore_pickaxe()
[git/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;
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
50fd6997 28 assert(data[sz] == '\0');
d01d8c67
PB
29 while (*data && !regexec(regexp, data, 1, &regmatch, flags)) {
30 flags |= REG_NOTBOL;
50fd6997
RS
31 data += regmatch.rm_eo;
32 if (*data && regmatch.rm_so == regmatch.rm_eo)
33 data++;
2002eed6
JH
34 cnt++;
35 }
d01d8c67
PB
36
37 } else { /* Classic exact string match */
ce163c79
RS
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++;
d01d8c67 45 }
2002eed6 46 }
a0cb9400 47 diff_free_filespec_data(one);
2002eed6 48 return cnt;
52e95789
JH
49}
50
382f013b 51void diffcore_pickaxe(struct diff_options *o)
52e95789 52{
382f013b
JH
53 const char *needle = o->pickaxe;
54 int opts = o->pickaxe_opts;
38c6f780 55 struct diff_queue_struct *q = &diff_queued_diff;
52e95789 56 unsigned long len = strlen(needle);
367cec1c 57 int i, has_changes;
d01d8c67 58 regex_t regex, *regexp = NULL;
52e95789 59 struct diff_queue_struct outq;
9ca5df90 60 DIFF_QUEUE_CLEAR(&outq);
52e95789 61
d01d8c67
PB
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);
71 }
72 regexp = &regex;
73 }
74
367cec1c
JH
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 */
d01d8c67 83 if (contains(p->two, needle, len, regexp))
367cec1c
JH
84 has_changes++;
85 }
86 else if (!DIFF_FILE_VALID(p->two)) {
d01d8c67 87 if (contains(p->one, needle, len, regexp))
367cec1c
JH
88 has_changes++;
89 }
90 else if (!diff_unmodified_pair(p) &&
d01d8c67
PB
91 contains(p->one, needle, len, regexp) !=
92 contains(p->two, needle, len, regexp))
367cec1c 93 has_changes++;
52e95789 94 }
367cec1c
JH
95 if (has_changes)
96 return; /* not munge the queue */
97
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.
102 */
103 for (i = 0; i < q->nr; i++)
104 diff_free_filepair(q->queue[i]);
105 }
a6080a0a 106 else
367cec1c
JH
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 */
d01d8c67 115 else if (contains(p->two, needle, len, regexp))
367cec1c
JH
116 has_changes = 1;
117 }
118 else if (!DIFF_FILE_VALID(p->two)) {
d01d8c67 119 if (contains(p->one, needle, len, regexp))
367cec1c
JH
120 has_changes = 1;
121 }
122 else if (!diff_unmodified_pair(p) &&
d01d8c67
PB
123 contains(p->one, needle, len, regexp) !=
124 contains(p->two, needle, len, regexp))
367cec1c
JH
125 has_changes = 1;
126
127 if (has_changes)
6b14d7fa 128 diff_q(&outq, p);
367cec1c
JH
129 else
130 diff_free_filepair(p);
52e95789 131 }
367cec1c 132
d01d8c67
PB
133 if (opts & DIFF_PICKAXE_REGEX) {
134 regfree(&regex);
135 }
136
52e95789
JH
137 free(q->queue);
138 *q = outq;
139 return;
140}