2 * Copyright (C) 2005 Junio C Hamano
8 static unsigned int contains(struct diff_filespec *one,
9 const char *needle, unsigned long len,
15 if (diff_populate_filespec(one, 0))
28 assert(data[sz] == '\0');
29 while (*data && !regexec(regexp, data, 1, ®match, flags)) {
31 data += regmatch.rm_eo;
32 if (*data && regmatch.rm_so == regmatch.rm_eo)
37 } else { /* Classic exact string match */
39 const char *found = memmem(data, sz, needle, len);
42 sz -= found - data + len;
47 diff_free_filespec_data(one);
51 void diffcore_pickaxe(const char *needle, int opts)
53 struct diff_queue_struct *q = &diff_queued_diff;
54 unsigned long len = strlen(needle);
56 regex_t regex, *regexp = NULL;
57 struct diff_queue_struct outq;
59 outq.nr = outq.alloc = 0;
61 if (opts & DIFF_PICKAXE_REGEX) {
63 err = regcomp(®ex, needle, REG_EXTENDED | REG_NEWLINE);
65 /* The POSIX.2 people are surely sick */
67 regerror(err, ®ex, errbuf, 1024);
69 die("invalid pickaxe regex: %s", errbuf);
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 */
82 if (contains(p->two, needle, len, regexp))
85 else if (!DIFF_FILE_VALID(p->two)) {
86 if (contains(p->one, needle, len, regexp))
89 else if (!diff_unmodified_pair(p) &&
90 contains(p->one, needle, len, regexp) !=
91 contains(p->two, needle, len, regexp))
95 return; /* not munge the queue */
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
102 for (i = 0; i < q->nr; i++)
103 diff_free_filepair(q->queue[i]);
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];
110 if (!DIFF_FILE_VALID(p->one)) {
111 if (!DIFF_FILE_VALID(p->two))
112 ; /* ignore unmerged */
114 else if (contains(p->two, needle, len, regexp))
117 else if (!DIFF_FILE_VALID(p->two)) {
118 if (contains(p->one, needle, len, regexp))
121 else if (!diff_unmodified_pair(p) &&
122 contains(p->one, needle, len, regexp) !=
123 contains(p->two, needle, len, regexp))
129 diff_free_filepair(p);
132 if (opts & DIFF_PICKAXE_REGEX) {