git-instaweb: Add support for running gitweb via 'plackup'
[git/jnareb-git.git] / diffcore-pickaxe.c
blob929de15aa9228099b3a772be788d746e440a9e8c
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(const char *needle, int opts)
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 DIFF_QUEUE_CLEAR(&outq);
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);
70 regexp = &regex;
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 */
81 if (contains(p->two, needle, len, regexp))
82 has_changes++;
84 else if (!DIFF_FILE_VALID(p->two)) {
85 if (contains(p->one, needle, len, regexp))
86 has_changes++;
88 else if (!diff_unmodified_pair(p) &&
89 contains(p->one, needle, len, regexp) !=
90 contains(p->two, needle, len, regexp))
91 has_changes++;
93 if (has_changes)
94 return; /* not munge the queue */
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.
101 for (i = 0; i < q->nr; i++)
102 diff_free_filepair(q->queue[i]);
104 else
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 */
113 else if (contains(p->two, needle, len, regexp))
114 has_changes = 1;
116 else if (!DIFF_FILE_VALID(p->two)) {
117 if (contains(p->one, needle, len, regexp))
118 has_changes = 1;
120 else if (!diff_unmodified_pair(p) &&
121 contains(p->one, needle, len, regexp) !=
122 contains(p->two, needle, len, regexp))
123 has_changes = 1;
125 if (has_changes)
126 diff_q(&outq, p);
127 else
128 diff_free_filepair(p);
131 if (opts & DIFF_PICKAXE_REGEX) {
132 regfree(&regex);
135 free(q->queue);
136 *q = outq;
137 return;