2005-05-21 09:40:01 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2005 Junio C Hamano
|
|
|
|
*/
|
|
|
|
#include "cache.h"
|
|
|
|
#include "diff.h"
|
|
|
|
#include "diffcore.h"
|
|
|
|
|
2005-07-23 23:35:25 +00:00
|
|
|
static unsigned int contains(struct diff_filespec *one,
|
2006-03-29 00:16:33 +00:00
|
|
|
const char *needle, unsigned long len,
|
|
|
|
regex_t *regexp)
|
2005-05-21 09:40:01 +00:00
|
|
|
{
|
2005-07-23 23:35:25 +00:00
|
|
|
unsigned int cnt;
|
2009-03-02 23:00:55 +00:00
|
|
|
unsigned long sz;
|
2005-05-21 09:40:01 +00:00
|
|
|
const char *data;
|
2005-05-27 22:56:38 +00:00
|
|
|
if (diff_populate_filespec(one, 0))
|
2005-05-21 09:40:01 +00:00
|
|
|
return 0;
|
2007-01-26 04:48:58 +00:00
|
|
|
if (!len)
|
|
|
|
return 0;
|
2005-07-23 23:35:25 +00:00
|
|
|
|
2005-05-21 09:40:01 +00:00
|
|
|
sz = one->size;
|
|
|
|
data = one->data;
|
2005-07-23 23:35:25 +00:00
|
|
|
cnt = 0;
|
|
|
|
|
2006-03-29 00:16:33 +00:00
|
|
|
if (regexp) {
|
|
|
|
regmatch_t regmatch;
|
|
|
|
int flags = 0;
|
|
|
|
|
2009-03-16 18:38:42 +00:00
|
|
|
assert(data[sz] == '\0');
|
2006-03-29 00:16:33 +00:00
|
|
|
while (*data && !regexec(regexp, data, 1, ®match, flags)) {
|
|
|
|
flags |= REG_NOTBOL;
|
2009-03-16 18:38:42 +00:00
|
|
|
data += regmatch.rm_eo;
|
|
|
|
if (*data && regmatch.rm_so == regmatch.rm_eo)
|
|
|
|
data++;
|
2005-07-23 23:35:25 +00:00
|
|
|
cnt++;
|
|
|
|
}
|
2006-03-29 00:16:33 +00:00
|
|
|
|
|
|
|
} else { /* Classic exact string match */
|
2009-03-02 23:00:55 +00:00
|
|
|
while (sz) {
|
|
|
|
const char *found = memmem(data, sz, needle, len);
|
|
|
|
if (!found)
|
|
|
|
break;
|
|
|
|
sz -= found - data + len;
|
|
|
|
data = found + len;
|
|
|
|
cnt++;
|
2006-03-29 00:16:33 +00:00
|
|
|
}
|
2005-07-23 23:35:25 +00:00
|
|
|
}
|
2007-05-07 08:24:27 +00:00
|
|
|
diff_free_filespec_data(one);
|
2005-07-23 23:35:25 +00:00
|
|
|
return cnt;
|
2005-05-21 09:40:01 +00:00
|
|
|
}
|
|
|
|
|
2005-05-27 22:55:28 +00:00
|
|
|
void diffcore_pickaxe(const char *needle, int opts)
|
2005-05-21 09:40:01 +00:00
|
|
|
{
|
2005-05-22 02:40:36 +00:00
|
|
|
struct diff_queue_struct *q = &diff_queued_diff;
|
2005-05-21 09:40:01 +00:00
|
|
|
unsigned long len = strlen(needle);
|
2005-05-27 22:55:28 +00:00
|
|
|
int i, has_changes;
|
2006-03-29 00:16:33 +00:00
|
|
|
regex_t regex, *regexp = NULL;
|
2005-05-21 09:40:01 +00:00
|
|
|
struct diff_queue_struct outq;
|
2010-05-07 04:52:27 +00:00
|
|
|
DIFF_QUEUE_CLEAR(&outq);
|
2005-05-21 09:40:01 +00:00
|
|
|
|
2006-03-29 00:16:33 +00:00
|
|
|
if (opts & DIFF_PICKAXE_REGEX) {
|
|
|
|
int err;
|
|
|
|
err = regcomp(®ex, needle, REG_EXTENDED | REG_NEWLINE);
|
|
|
|
if (err) {
|
|
|
|
/* The POSIX.2 people are surely sick */
|
|
|
|
char errbuf[1024];
|
|
|
|
regerror(err, ®ex, errbuf, 1024);
|
|
|
|
regfree(®ex);
|
|
|
|
die("invalid pickaxe regex: %s", errbuf);
|
|
|
|
}
|
|
|
|
regexp = ®ex;
|
|
|
|
}
|
|
|
|
|
2005-05-27 22:55:28 +00:00
|
|
|
if (opts & DIFF_PICKAXE_ALL) {
|
|
|
|
/* Showing the whole changeset if needle exists */
|
|
|
|
for (i = has_changes = 0; !has_changes && i < q->nr; i++) {
|
|
|
|
struct diff_filepair *p = q->queue[i];
|
|
|
|
if (!DIFF_FILE_VALID(p->one)) {
|
|
|
|
if (!DIFF_FILE_VALID(p->two))
|
|
|
|
continue; /* ignore unmerged */
|
|
|
|
/* created */
|
2006-03-29 00:16:33 +00:00
|
|
|
if (contains(p->two, needle, len, regexp))
|
2005-05-27 22:55:28 +00:00
|
|
|
has_changes++;
|
|
|
|
}
|
|
|
|
else if (!DIFF_FILE_VALID(p->two)) {
|
2006-03-29 00:16:33 +00:00
|
|
|
if (contains(p->one, needle, len, regexp))
|
2005-05-27 22:55:28 +00:00
|
|
|
has_changes++;
|
|
|
|
}
|
|
|
|
else if (!diff_unmodified_pair(p) &&
|
2006-03-29 00:16:33 +00:00
|
|
|
contains(p->one, needle, len, regexp) !=
|
|
|
|
contains(p->two, needle, len, regexp))
|
2005-05-27 22:55:28 +00:00
|
|
|
has_changes++;
|
2005-05-21 09:40:01 +00:00
|
|
|
}
|
2005-05-27 22:55:28 +00:00
|
|
|
if (has_changes)
|
|
|
|
return; /* not munge the queue */
|
|
|
|
|
|
|
|
/* otherwise we will clear the whole queue
|
|
|
|
* by copying the empty outq at the end of this
|
|
|
|
* function, but first clear the current entries
|
|
|
|
* in the queue.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < q->nr; i++)
|
|
|
|
diff_free_filepair(q->queue[i]);
|
|
|
|
}
|
2007-06-07 07:04:01 +00:00
|
|
|
else
|
2005-05-27 22:55:28 +00:00
|
|
|
/* Showing only the filepairs that has the needle */
|
|
|
|
for (i = 0; i < q->nr; i++) {
|
|
|
|
struct diff_filepair *p = q->queue[i];
|
|
|
|
has_changes = 0;
|
|
|
|
if (!DIFF_FILE_VALID(p->one)) {
|
|
|
|
if (!DIFF_FILE_VALID(p->two))
|
|
|
|
; /* ignore unmerged */
|
|
|
|
/* created */
|
2006-03-29 00:16:33 +00:00
|
|
|
else if (contains(p->two, needle, len, regexp))
|
2005-05-27 22:55:28 +00:00
|
|
|
has_changes = 1;
|
|
|
|
}
|
|
|
|
else if (!DIFF_FILE_VALID(p->two)) {
|
2006-03-29 00:16:33 +00:00
|
|
|
if (contains(p->one, needle, len, regexp))
|
2005-05-27 22:55:28 +00:00
|
|
|
has_changes = 1;
|
|
|
|
}
|
|
|
|
else if (!diff_unmodified_pair(p) &&
|
2006-03-29 00:16:33 +00:00
|
|
|
contains(p->one, needle, len, regexp) !=
|
|
|
|
contains(p->two, needle, len, regexp))
|
2005-05-27 22:55:28 +00:00
|
|
|
has_changes = 1;
|
|
|
|
|
|
|
|
if (has_changes)
|
2005-05-22 17:04:37 +00:00
|
|
|
diff_q(&outq, p);
|
2005-05-27 22:55:28 +00:00
|
|
|
else
|
|
|
|
diff_free_filepair(p);
|
2005-05-21 09:40:01 +00:00
|
|
|
}
|
2005-05-27 22:55:28 +00:00
|
|
|
|
2006-03-29 00:16:33 +00:00
|
|
|
if (opts & DIFF_PICKAXE_REGEX) {
|
|
|
|
regfree(®ex);
|
|
|
|
}
|
|
|
|
|
2005-05-21 09:40:01 +00:00
|
|
|
free(q->queue);
|
|
|
|
*q = outq;
|
|
|
|
return;
|
|
|
|
}
|