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