diffcore-pickaxe.c 3.23 KB
Newer Older
1 2 3 4 5 6 7
/*
 * Copyright (C) 2005 Junio C Hamano
 */
#include "cache.h"
#include "diff.h"
#include "diffcore.h"

8
static unsigned int contains(struct diff_filespec *one,
9 10
			     const char *needle, unsigned long len,
			     regex_t *regexp)
11
{
12
	unsigned int cnt;
13
	unsigned long sz;
14
	const char *data;
15
	if (diff_populate_filespec(one, 0))
16
		return 0;
17 18
	if (!len)
		return 0;
19

20 21
	sz = one->size;
	data = one->data;
22 23
	cnt = 0;

24 25 26 27
	if (regexp) {
		regmatch_t regmatch;
		int flags = 0;

28
		assert(data[sz] == '\0');
29 30
		while (*data && !regexec(regexp, data, 1, &regmatch, flags)) {
			flags |= REG_NOTBOL;
31 32 33
			data += regmatch.rm_eo;
			if (*data && regmatch.rm_so == regmatch.rm_eo)
				data++;
34 35
			cnt++;
		}
36 37

	} else { /* Classic exact string match */
38 39 40 41 42 43 44
		while (sz) {
			const char *found = memmem(data, sz, needle, len);
			if (!found)
				break;
			sz -= found - data + len;
			data = found + len;
			cnt++;
45
		}
46
	}
47
	diff_free_filespec_data(one);
48
	return cnt;
49 50
}

51
void diffcore_pickaxe(struct diff_options *o)
52
{
53 54
	const char *needle = o->pickaxe;
	int opts = o->pickaxe_opts;
55
	struct diff_queue_struct *q = &diff_queued_diff;
56
	unsigned long len = strlen(needle);
57
	int i, has_changes;
58
	regex_t regex, *regexp = NULL;
59
	struct diff_queue_struct outq;
Bo Yang's avatar
Bo Yang committed
60
	DIFF_QUEUE_CLEAR(&outq);
61

62 63 64 65 66 67 68 69 70 71 72 73 74
	if (opts & DIFF_PICKAXE_REGEX) {
		int err;
		err = regcomp(&regex, needle, REG_EXTENDED | REG_NEWLINE);
		if (err) {
			/* The POSIX.2 people are surely sick */
			char errbuf[1024];
			regerror(err, &regex, errbuf, 1024);
			regfree(&regex);
			die("invalid pickaxe regex: %s", errbuf);
		}
		regexp = &regex;
	}

75 76 77 78 79 80 81 82
	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 */
83
				if (contains(p->two, needle, len, regexp))
84 85 86
					has_changes++;
			}
			else if (!DIFF_FILE_VALID(p->two)) {
87
				if (contains(p->one, needle, len, regexp))
88 89 90
					has_changes++;
			}
			else if (!diff_unmodified_pair(p) &&
91 92
				 contains(p->one, needle, len, regexp) !=
				 contains(p->two, needle, len, regexp))
93
				has_changes++;
94
		}
95 96 97 98 99 100 101 102 103 104 105
		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]);
	}
Junio C Hamano's avatar
Junio C Hamano committed
106
	else
107 108 109 110 111 112 113 114
		/* 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 */
115
				else if (contains(p->two, needle, len, regexp))
116 117 118
					has_changes = 1;
			}
			else if (!DIFF_FILE_VALID(p->two)) {
119
				if (contains(p->one, needle, len, regexp))
120 121 122
					has_changes = 1;
			}
			else if (!diff_unmodified_pair(p) &&
123 124
				 contains(p->one, needle, len, regexp) !=
				 contains(p->two, needle, len, regexp))
125 126 127
				has_changes = 1;

			if (has_changes)
Junio C Hamano's avatar
Junio C Hamano committed
128
				diff_q(&outq, p);
129 130
			else
				diff_free_filepair(p);
131
		}
132

133 134 135 136
	if (opts & DIFF_PICKAXE_REGEX) {
		regfree(&regex);
	}

137 138 139 140
	free(q->queue);
	*q = outq;
	return;
}