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 while (*data
&& !regexec(regexp
, data
, 1, ®match
, flags
)) {
30 data
+= regmatch
.rm_so
;
35 } else { /* Classic exact string match */
37 const char *found
= memmem(data
, sz
, needle
, len
);
40 sz
-= found
- data
+ len
;
45 diff_free_filespec_data(one
);
49 void diffcore_pickaxe(const char *needle
, int opts
)
51 struct diff_queue_struct
*q
= &diff_queued_diff
;
52 unsigned long len
= strlen(needle
);
54 regex_t regex
, *regexp
= NULL
;
55 struct diff_queue_struct outq
;
57 outq
.nr
= outq
.alloc
= 0;
59 if (opts
& DIFF_PICKAXE_REGEX
) {
61 err
= regcomp(®ex
, needle
, REG_EXTENDED
| REG_NEWLINE
);
63 /* The POSIX.2 people are surely sick */
65 regerror(err
, ®ex
, errbuf
, 1024);
67 die("invalid pickaxe regex: %s", errbuf
);
72 if (opts
& DIFF_PICKAXE_ALL
) {
73 /* Showing the whole changeset if needle exists */
74 for (i
= has_changes
= 0; !has_changes
&& i
< q
->nr
; i
++) {
75 struct diff_filepair
*p
= q
->queue
[i
];
76 if (!DIFF_FILE_VALID(p
->one
)) {
77 if (!DIFF_FILE_VALID(p
->two
))
78 continue; /* ignore unmerged */
80 if (contains(p
->two
, needle
, len
, regexp
))
83 else if (!DIFF_FILE_VALID(p
->two
)) {
84 if (contains(p
->one
, needle
, len
, regexp
))
87 else if (!diff_unmodified_pair(p
) &&
88 contains(p
->one
, needle
, len
, regexp
) !=
89 contains(p
->two
, needle
, len
, regexp
))
93 return; /* not munge the queue */
95 /* otherwise we will clear the whole queue
96 * by copying the empty outq at the end of this
97 * function, but first clear the current entries
100 for (i
= 0; i
< q
->nr
; i
++)
101 diff_free_filepair(q
->queue
[i
]);
104 /* Showing only the filepairs that has the needle */
105 for (i
= 0; i
< q
->nr
; i
++) {
106 struct diff_filepair
*p
= q
->queue
[i
];
108 if (!DIFF_FILE_VALID(p
->one
)) {
109 if (!DIFF_FILE_VALID(p
->two
))
110 ; /* ignore unmerged */
112 else if (contains(p
->two
, needle
, len
, regexp
))
115 else if (!DIFF_FILE_VALID(p
->two
)) {
116 if (contains(p
->one
, needle
, len
, regexp
))
119 else if (!diff_unmodified_pair(p
) &&
120 contains(p
->one
, needle
, len
, regexp
) !=
121 contains(p
->two
, needle
, len
, regexp
))
127 diff_free_filepair(p
);
130 if (opts
& DIFF_PICKAXE_REGEX
) {