2 * Copyright (C) 2005 Junio C Hamano
10 static unsigned int contains(struct diff_filespec
*one
,
11 const char *needle
, unsigned long len
,
15 unsigned long offset
, sz
;
17 if (diff_populate_filespec(one
, 0))
28 while (*data
&& !regexec(regexp
, data
, 1, ®match
, flags
)) {
30 data
+= regmatch
.rm_so
;
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.
39 for (offset
= 0; offset
+ len
<= sz
; offset
++) {
40 /* we count non-overlapping occurrences of needle */
41 if (!memcmp(needle
, data
+ offset
, len
)) {
50 void diffcore_pickaxe(const char *needle
, int opts
)
52 struct diff_queue_struct
*q
= &diff_queued_diff
;
53 unsigned long len
= strlen(needle
);
55 regex_t regex
, *regexp
= NULL
;
56 struct diff_queue_struct outq
;
58 outq
.nr
= outq
.alloc
= 0;
60 if (opts
& DIFF_PICKAXE_REGEX
) {
62 err
= regcomp(®ex
, needle
, REG_EXTENDED
| REG_NEWLINE
);
64 /* The POSIX.2 people are surely sick */
66 regerror(err
, ®ex
, errbuf
, 1024);
68 die("invalid pickaxe regex: %s", errbuf
);
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 */
81 if (contains(p
->two
, needle
, len
, regexp
))
84 else if (!DIFF_FILE_VALID(p
->two
)) {
85 if (contains(p
->one
, needle
, len
, regexp
))
88 else if (!diff_unmodified_pair(p
) &&
89 contains(p
->one
, needle
, len
, regexp
) !=
90 contains(p
->two
, needle
, len
, regexp
))
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
101 for (i
= 0; i
< q
->nr
; i
++)
102 diff_free_filepair(q
->queue
[i
]);
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
];
109 if (!DIFF_FILE_VALID(p
->one
)) {
110 if (!DIFF_FILE_VALID(p
->two
))
111 ; /* ignore unmerged */
113 else if (contains(p
->two
, needle
, len
, regexp
))
116 else if (!DIFF_FILE_VALID(p
->two
)) {
117 if (contains(p
->one
, needle
, len
, regexp
))
120 else if (!diff_unmodified_pair(p
) &&
121 contains(p
->one
, needle
, len
, regexp
) !=
122 contains(p
->two
, needle
, len
, regexp
))
128 diff_free_filepair(p
);
131 if (opts
& DIFF_PICKAXE_REGEX
) {