Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 140 lines (127 sloc) 3.275 kb
52e9578 [PATCH] Introducing software archaeologist's tool "pickaxe".
Junio C Hamano authored
1 /*
2 * Copyright (C) 2005 Junio C Hamano
3 */
4 #include "cache.h"
5 #include "diff.h"
6 #include "diffcore.h"
7
2002eed [PATCH] diffcore-pickaxe: switch to "counting" behaviour.
Junio C Hamano authored
8 static unsigned int contains(struct diff_filespec *one,
d01d8c6 Support for pickaxe matching regular expressions
Petr Baudis authored
9 const char *needle, unsigned long len,
10 regex_t *regexp)
52e9578 [PATCH] Introducing software archaeologist's tool "pickaxe".
Junio C Hamano authored
11 {
2002eed [PATCH] diffcore-pickaxe: switch to "counting" behaviour.
Junio C Hamano authored
12 unsigned int cnt;
ce163c7 diffcore-pickaxe: use memmem()
René Scharfe authored
13 unsigned long sz;
52e9578 [PATCH] Introducing software archaeologist's tool "pickaxe".
Junio C Hamano authored
14 const char *data;
f0c6b2a [PATCH] Optimize diff-tree -[CM] --stdin
Junio C Hamano authored
15 if (diff_populate_filespec(one, 0))
52e9578 [PATCH] Introducing software archaeologist's tool "pickaxe".
Junio C Hamano authored
16 return 0;
e1b1611 @peff diffcore-pickaxe: fix infinite loop on zero-length needle
authored
17 if (!len)
18 return 0;
2002eed [PATCH] diffcore-pickaxe: switch to "counting" behaviour.
Junio C Hamano authored
19
52e9578 [PATCH] Introducing software archaeologist's tool "pickaxe".
Junio C Hamano authored
20 sz = one->size;
21 data = one->data;
2002eed [PATCH] diffcore-pickaxe: switch to "counting" behaviour.
Junio C Hamano authored
22 cnt = 0;
23
d01d8c6 Support for pickaxe matching regular expressions
Petr Baudis authored
24 if (regexp) {
25 regmatch_t regmatch;
26 int flags = 0;
27
50fd699 pickaxe: count regex matches only once
René Scharfe authored
28 assert(data[sz] == '\0');
d01d8c6 Support for pickaxe matching regular expressions
Petr Baudis authored
29 while (*data && !regexec(regexp, data, 1, &regmatch, flags)) {
30 flags |= REG_NOTBOL;
50fd699 pickaxe: count regex matches only once
René Scharfe authored
31 data += regmatch.rm_eo;
32 if (*data && regmatch.rm_so == regmatch.rm_eo)
33 data++;
2002eed [PATCH] diffcore-pickaxe: switch to "counting" behaviour.
Junio C Hamano authored
34 cnt++;
35 }
d01d8c6 Support for pickaxe matching regular expressions
Petr Baudis authored
36
37 } else { /* Classic exact string match */
ce163c7 diffcore-pickaxe: use memmem()
René Scharfe authored
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++;
d01d8c6 Support for pickaxe matching regular expressions
Petr Baudis authored
45 }
2002eed [PATCH] diffcore-pickaxe: switch to "counting" behaviour.
Junio C Hamano authored
46 }
a0cb940 diff -S: release the image after looking for needle in it
Junio C Hamano authored
47 diff_free_filespec_data(one);
2002eed [PATCH] diffcore-pickaxe: switch to "counting" behaviour.
Junio C Hamano authored
48 return cnt;
52e9578 [PATCH] Introducing software archaeologist's tool "pickaxe".
Junio C Hamano authored
49 }
50
367cec1 [PATCH] Add --pickaxe-all to diff-* brothers.
Junio C Hamano authored
51 void diffcore_pickaxe(const char *needle, int opts)
52e9578 [PATCH] Introducing software archaeologist's tool "pickaxe".
Junio C Hamano authored
52 {
38c6f78 [PATCH] Prepare diffcore interface for diff-tree header supression.
Junio C Hamano authored
53 struct diff_queue_struct *q = &diff_queued_diff;
52e9578 [PATCH] Introducing software archaeologist's tool "pickaxe".
Junio C Hamano authored
54 unsigned long len = strlen(needle);
367cec1 [PATCH] Add --pickaxe-all to diff-* brothers.
Junio C Hamano authored
55 int i, has_changes;
d01d8c6 Support for pickaxe matching regular expressions
Petr Baudis authored
56 regex_t regex, *regexp = NULL;
52e9578 [PATCH] Introducing software archaeologist's tool "pickaxe".
Junio C Hamano authored
57 struct diff_queue_struct outq;
58 outq.queue = NULL;
59 outq.nr = outq.alloc = 0;
60
d01d8c6 Support for pickaxe matching regular expressions
Petr Baudis authored
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
367cec1 [PATCH] Add --pickaxe-all to diff-* brothers.
Junio C Hamano authored
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 */
d01d8c6 Support for pickaxe matching regular expressions
Petr Baudis authored
82 if (contains(p->two, needle, len, regexp))
367cec1 [PATCH] Add --pickaxe-all to diff-* brothers.
Junio C Hamano authored
83 has_changes++;
84 }
85 else if (!DIFF_FILE_VALID(p->two)) {
d01d8c6 Support for pickaxe matching regular expressions
Petr Baudis authored
86 if (contains(p->one, needle, len, regexp))
367cec1 [PATCH] Add --pickaxe-all to diff-* brothers.
Junio C Hamano authored
87 has_changes++;
88 }
89 else if (!diff_unmodified_pair(p) &&
d01d8c6 Support for pickaxe matching regular expressions
Petr Baudis authored
90 contains(p->one, needle, len, regexp) !=
91 contains(p->two, needle, len, regexp))
367cec1 [PATCH] Add --pickaxe-all to diff-* brothers.
Junio C Hamano authored
92 has_changes++;
52e9578 [PATCH] Introducing software archaeologist's tool "pickaxe".
Junio C Hamano authored
93 }
367cec1 [PATCH] Add --pickaxe-all to diff-* brothers.
Junio C Hamano authored
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 }
a6080a0 @gitster War on whitespace
gitster authored
105 else
367cec1 [PATCH] Add --pickaxe-all to diff-* brothers.
Junio C Hamano authored
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 */
d01d8c6 Support for pickaxe matching regular expressions
Petr Baudis authored
114 else if (contains(p->two, needle, len, regexp))
367cec1 [PATCH] Add --pickaxe-all to diff-* brothers.
Junio C Hamano authored
115 has_changes = 1;
116 }
117 else if (!DIFF_FILE_VALID(p->two)) {
d01d8c6 Support for pickaxe matching regular expressions
Petr Baudis authored
118 if (contains(p->one, needle, len, regexp))
367cec1 [PATCH] Add --pickaxe-all to diff-* brothers.
Junio C Hamano authored
119 has_changes = 1;
120 }
121 else if (!diff_unmodified_pair(p) &&
d01d8c6 Support for pickaxe matching regular expressions
Petr Baudis authored
122 contains(p->one, needle, len, regexp) !=
123 contains(p->two, needle, len, regexp))
367cec1 [PATCH] Add --pickaxe-all to diff-* brothers.
Junio C Hamano authored
124 has_changes = 1;
125
126 if (has_changes)
6b14d7f [PATCH] Diffcore updates.
Junio C Hamano authored
127 diff_q(&outq, p);
367cec1 [PATCH] Add --pickaxe-all to diff-* brothers.
Junio C Hamano authored
128 else
129 diff_free_filepair(p);
52e9578 [PATCH] Introducing software archaeologist's tool "pickaxe".
Junio C Hamano authored
130 }
367cec1 [PATCH] Add --pickaxe-all to diff-* brothers.
Junio C Hamano authored
131
d01d8c6 Support for pickaxe matching regular expressions
Petr Baudis authored
132 if (opts & DIFF_PICKAXE_REGEX) {
133 regfree(&regex);
134 }
135
52e9578 [PATCH] Introducing software archaeologist's tool "pickaxe".
Junio C Hamano authored
136 free(q->queue);
137 *q = outq;
138 return;
139 }
Something went wrong with that request. Please try again.