Merge branch 'jc/describe-always'
[git] / diffcore-pickaxe.c
1 /*
2  * Copyright (C) 2005 Junio C Hamano
3  */
4 #include "cache.h"
5 #include "diff.h"
6 #include "diffcore.h"
7
8 static unsigned int contains(struct diff_filespec *one,
9                              const char *needle, unsigned long len,
10                              regex_t *regexp)
11 {
12         unsigned int cnt;
13         unsigned long offset, sz;
14         const char *data;
15         if (diff_populate_filespec(one, 0))
16                 return 0;
17         if (!len)
18                 return 0;
19
20         sz = one->size;
21         data = one->data;
22         cnt = 0;
23
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++;
32                         cnt++;
33                 }
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                 }
46         }
47         diff_free_filespec_data(one);
48         return cnt;
49 }
50
51 void diffcore_pickaxe(const char *needle, int opts)
52 {
53         struct diff_queue_struct *q = &diff_queued_diff;
54         unsigned long len = strlen(needle);
55         int i, has_changes;
56         regex_t regex, *regexp = NULL;
57         struct diff_queue_struct outq;
58         outq.queue = NULL;
59         outq.nr = outq.alloc = 0;
60
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
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 */
82                                 if (contains(p->two, needle, len, regexp))
83                                         has_changes++;
84                         }
85                         else if (!DIFF_FILE_VALID(p->two)) {
86                                 if (contains(p->one, needle, len, regexp))
87                                         has_changes++;
88                         }
89                         else if (!diff_unmodified_pair(p) &&
90                                  contains(p->one, needle, len, regexp) !=
91                                  contains(p->two, needle, len, regexp))
92                                 has_changes++;
93                 }
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         }
105         else
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 */
114                                 else if (contains(p->two, needle, len, regexp))
115                                         has_changes = 1;
116                         }
117                         else if (!DIFF_FILE_VALID(p->two)) {
118                                 if (contains(p->one, needle, len, regexp))
119                                         has_changes = 1;
120                         }
121                         else if (!diff_unmodified_pair(p) &&
122                                  contains(p->one, needle, len, regexp) !=
123                                  contains(p->two, needle, len, regexp))
124                                 has_changes = 1;
125
126                         if (has_changes)
127                                 diff_q(&outq, p);
128                         else
129                                 diff_free_filepair(p);
130                 }
131
132         if (opts & DIFF_PICKAXE_REGEX) {
133                 regfree(&regex);
134         }
135
136         free(q->queue);
137         *q = outq;
138         return;
139 }