2005-05-21 17:40:01 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2005 Junio C Hamano
|
|
|
|
*/
|
|
|
|
#include "cache.h"
|
|
|
|
#include "diff.h"
|
|
|
|
#include "diffcore.h"
|
|
|
|
|
2005-07-24 07:35:25 +08:00
|
|
|
static unsigned int contains(struct diff_filespec *one,
|
2006-03-29 08:16:33 +08:00
|
|
|
const char *needle, unsigned long len,
|
|
|
|
regex_t *regexp)
|
2005-05-21 17:40:01 +08:00
|
|
|
{
|
2005-07-24 07:35:25 +08:00
|
|
|
unsigned int cnt;
|
2009-03-03 07:00:55 +08:00
|
|
|
unsigned long sz;
|
2005-05-21 17:40:01 +08:00
|
|
|
const char *data;
|
2005-05-28 06:56:38 +08:00
|
|
|
if (diff_populate_filespec(one, 0))
|
2005-05-21 17:40:01 +08:00
|
|
|
return 0;
|
2007-01-26 12:48:58 +08:00
|
|
|
if (!len)
|
|
|
|
return 0;
|
2005-07-24 07:35:25 +08:00
|
|
|
|
2005-05-21 17:40:01 +08:00
|
|
|
sz = one->size;
|
|
|
|
data = one->data;
|
2005-07-24 07:35:25 +08:00
|
|
|
cnt = 0;
|
|
|
|
|
2006-03-29 08:16:33 +08:00
|
|
|
if (regexp) {
|
|
|
|
regmatch_t regmatch;
|
|
|
|
int flags = 0;
|
|
|
|
|
2009-03-17 02:38:42 +08:00
|
|
|
assert(data[sz] == '\0');
|
2006-03-29 08:16:33 +08:00
|
|
|
while (*data && !regexec(regexp, data, 1, ®match, flags)) {
|
|
|
|
flags |= REG_NOTBOL;
|
2009-03-17 02:38:42 +08:00
|
|
|
data += regmatch.rm_eo;
|
|
|
|
if (*data && regmatch.rm_so == regmatch.rm_eo)
|
|
|
|
data++;
|
2005-07-24 07:35:25 +08:00
|
|
|
cnt++;
|
|
|
|
}
|
2006-03-29 08:16:33 +08:00
|
|
|
|
|
|
|
} else { /* Classic exact string match */
|
2009-03-03 07:00:55 +08: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 08:16:33 +08:00
|
|
|
}
|
2005-07-24 07:35:25 +08:00
|
|
|
}
|
2007-05-07 16:24:27 +08:00
|
|
|
diff_free_filespec_data(one);
|
2005-07-24 07:35:25 +08:00
|
|
|
return cnt;
|
2005-05-21 17:40:01 +08:00
|
|
|
}
|
|
|
|
|
2005-05-28 06:55:28 +08:00
|
|
|
void diffcore_pickaxe(const char *needle, int opts)
|
2005-05-21 17:40:01 +08:00
|
|
|
{
|
2005-05-22 10:40:36 +08:00
|
|
|
struct diff_queue_struct *q = &diff_queued_diff;
|
2005-05-21 17:40:01 +08:00
|
|
|
unsigned long len = strlen(needle);
|
2005-05-28 06:55:28 +08:00
|
|
|
int i, has_changes;
|
2006-03-29 08:16:33 +08:00
|
|
|
regex_t regex, *regexp = NULL;
|
2005-05-21 17:40:01 +08:00
|
|
|
struct diff_queue_struct outq;
|
|
|
|
outq.queue = NULL;
|
|
|
|
outq.nr = outq.alloc = 0;
|
|
|
|
|
2006-03-29 08:16:33 +08: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-28 06:55:28 +08: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 08:16:33 +08:00
|
|
|
if (contains(p->two, needle, len, regexp))
|
2005-05-28 06:55:28 +08:00
|
|
|
has_changes++;
|
|
|
|
}
|
|
|
|
else if (!DIFF_FILE_VALID(p->two)) {
|
2006-03-29 08:16:33 +08:00
|
|
|
if (contains(p->one, needle, len, regexp))
|
2005-05-28 06:55:28 +08:00
|
|
|
has_changes++;
|
|
|
|
}
|
|
|
|
else if (!diff_unmodified_pair(p) &&
|
2006-03-29 08:16:33 +08:00
|
|
|
contains(p->one, needle, len, regexp) !=
|
|
|
|
contains(p->two, needle, len, regexp))
|
2005-05-28 06:55:28 +08:00
|
|
|
has_changes++;
|
2005-05-21 17:40:01 +08:00
|
|
|
}
|
2005-05-28 06:55:28 +08: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 15:04:01 +08:00
|
|
|
else
|
2005-05-28 06:55:28 +08: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 08:16:33 +08:00
|
|
|
else if (contains(p->two, needle, len, regexp))
|
2005-05-28 06:55:28 +08:00
|
|
|
has_changes = 1;
|
|
|
|
}
|
|
|
|
else if (!DIFF_FILE_VALID(p->two)) {
|
2006-03-29 08:16:33 +08:00
|
|
|
if (contains(p->one, needle, len, regexp))
|
2005-05-28 06:55:28 +08:00
|
|
|
has_changes = 1;
|
|
|
|
}
|
|
|
|
else if (!diff_unmodified_pair(p) &&
|
2006-03-29 08:16:33 +08:00
|
|
|
contains(p->one, needle, len, regexp) !=
|
|
|
|
contains(p->two, needle, len, regexp))
|
2005-05-28 06:55:28 +08:00
|
|
|
has_changes = 1;
|
|
|
|
|
|
|
|
if (has_changes)
|
2005-05-23 01:04:37 +08:00
|
|
|
diff_q(&outq, p);
|
2005-05-28 06:55:28 +08:00
|
|
|
else
|
|
|
|
diff_free_filepair(p);
|
2005-05-21 17:40:01 +08:00
|
|
|
}
|
2005-05-28 06:55:28 +08:00
|
|
|
|
2006-03-29 08:16:33 +08:00
|
|
|
if (opts & DIFF_PICKAXE_REGEX) {
|
|
|
|
regfree(®ex);
|
|
|
|
}
|
|
|
|
|
2005-05-21 17:40:01 +08:00
|
|
|
free(q->queue);
|
|
|
|
*q = outq;
|
|
|
|
return;
|
|
|
|
}
|