2 * Copyright (C) 2005 Junio C Hamano
8 static unsigned int contains(struct diff_filespec
*one
,
9 const char *needle
, unsigned long len
,
13 unsigned long offset
, sz
;
15 if (diff_populate_filespec(one
, 0))
26 while (*data
&& !regexec(regexp
, data
, 1, ®match
, flags
)) {
28 data
+= regmatch
.rm_so
;
33 } else { /* Classic exact string match */
34 /* Yes, I've heard of strstr(), but the thing is *data may
35 * not be NUL terminated. Sue me.
37 for (offset
= 0; offset
+ len
<= sz
; offset
++) {
38 /* we count non-overlapping occurrences of needle */
39 if (!memcmp(needle
, data
+ offset
, len
)) {
48 void diffcore_pickaxe(const char *needle
, int opts
)
50 struct diff_queue_struct
*q
= &diff_queued_diff
;
51 unsigned long len
= strlen(needle
);
53 regex_t regex
, *regexp
= NULL
;
54 struct diff_queue_struct outq
;
56 outq
.nr
= outq
.alloc
= 0;
58 if (opts
& DIFF_PICKAXE_REGEX
) {
60 err
= regcomp(®ex
, needle
, REG_EXTENDED
| REG_NEWLINE
);
62 /* The POSIX.2 people are surely sick */
64 regerror(err
, ®ex
, errbuf
, 1024);
66 die("invalid pickaxe regex: %s", errbuf
);
71 if (opts
& DIFF_PICKAXE_ALL
) {
72 /* Showing the whole changeset if needle exists */
73 for (i
= has_changes
= 0; !has_changes
&& i
< q
->nr
; i
++) {
74 struct diff_filepair
*p
= q
->queue
[i
];
75 if (!DIFF_FILE_VALID(p
->one
)) {
76 if (!DIFF_FILE_VALID(p
->two
))
77 continue; /* ignore unmerged */
79 if (contains(p
->two
, needle
, len
, regexp
))
82 else if (!DIFF_FILE_VALID(p
->two
)) {
83 if (contains(p
->one
, needle
, len
, regexp
))
86 else if (!diff_unmodified_pair(p
) &&
87 contains(p
->one
, needle
, len
, regexp
) !=
88 contains(p
->two
, needle
, len
, regexp
))
92 return; /* not munge the queue */
94 /* otherwise we will clear the whole queue
95 * by copying the empty outq at the end of this
96 * function, but first clear the current entries
99 for (i
= 0; i
< q
->nr
; i
++)
100 diff_free_filepair(q
->queue
[i
]);
103 /* Showing only the filepairs that has the needle */
104 for (i
= 0; i
< q
->nr
; i
++) {
105 struct diff_filepair
*p
= q
->queue
[i
];
107 if (!DIFF_FILE_VALID(p
->one
)) {
108 if (!DIFF_FILE_VALID(p
->two
))
109 ; /* ignore unmerged */
111 else if (contains(p
->two
, needle
, len
, regexp
))
114 else if (!DIFF_FILE_VALID(p
->two
)) {
115 if (contains(p
->one
, needle
, len
, regexp
))
118 else if (!diff_unmodified_pair(p
) &&
119 contains(p
->one
, needle
, len
, regexp
) !=
120 contains(p
->two
, needle
, len
, regexp
))
126 diff_free_filepair(p
);
129 if (opts
& DIFF_PICKAXE_REGEX
) {