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