blob: 286919e71428049eda1449ba71ac81667ebc3f7d [file] [log] [blame]
Junio C Hamano52e95782005-05-21 02:40:01 -07001/*
2 * Copyright (C) 2005 Junio C Hamano
3 */
4#include "cache.h"
5#include "diff.h"
6#include "diffcore.h"
Junio C Hamano52e95782005-05-21 02:40:01 -07007
Junio C Hamano2002eed2005-07-23 16:35:25 -07008static unsigned int contains(struct diff_filespec *one,
Petr Baudisd01d8c62006-03-29 02:16:33 +02009 const char *needle, unsigned long len,
10 regex_t *regexp)
Junio C Hamano52e95782005-05-21 02:40:01 -070011{
Junio C Hamano2002eed2005-07-23 16:35:25 -070012 unsigned int cnt;
Junio C Hamano52e95782005-05-21 02:40:01 -070013 unsigned long offset, sz;
14 const char *data;
Junio C Hamanof0c6b2a2005-05-27 15:56:38 -070015 if (diff_populate_filespec(one, 0))
Junio C Hamano52e95782005-05-21 02:40:01 -070016 return 0;
Jeff Kinge1b16112007-01-25 23:48:58 -050017 if (!len)
18 return 0;
Junio C Hamano2002eed2005-07-23 16:35:25 -070019
Junio C Hamano52e95782005-05-21 02:40:01 -070020 sz = one->size;
21 data = one->data;
Junio C Hamano2002eed2005-07-23 16:35:25 -070022 cnt = 0;
23
Petr Baudisd01d8c62006-03-29 02:16:33 +020024 if (regexp) {
25 regmatch_t regmatch;
26 int flags = 0;
27
28 while (*data && !regexec(regexp, data, 1, &regmatch, flags)) {
29 flags |= REG_NOTBOL;
30 data += regmatch.rm_so;
31 if (*data) data++;
Junio C Hamano2002eed2005-07-23 16:35:25 -070032 cnt++;
33 }
Petr Baudisd01d8c62006-03-29 02:16:33 +020034
35 } else { /* Classic exact string match */
36 /* Yes, I've heard of strstr(), but the thing is *data may
37 * not be NUL terminated. Sue me.
38 */
39 for (offset = 0; offset + len <= sz; offset++) {
40 /* we count non-overlapping occurrences of needle */
41 if (!memcmp(needle, data + offset, len)) {
42 offset += len - 1;
43 cnt++;
44 }
45 }
Junio C Hamano2002eed2005-07-23 16:35:25 -070046 }
47 return cnt;
Junio C Hamano52e95782005-05-21 02:40:01 -070048}
49
Junio C Hamano367cec12005-05-27 15:55:28 -070050void diffcore_pickaxe(const char *needle, int opts)
Junio C Hamano52e95782005-05-21 02:40:01 -070051{
Junio C Hamano38c6f782005-05-21 19:40:36 -070052 struct diff_queue_struct *q = &diff_queued_diff;
Junio C Hamano52e95782005-05-21 02:40:01 -070053 unsigned long len = strlen(needle);
Junio C Hamano367cec12005-05-27 15:55:28 -070054 int i, has_changes;
Petr Baudisd01d8c62006-03-29 02:16:33 +020055 regex_t regex, *regexp = NULL;
Junio C Hamano52e95782005-05-21 02:40:01 -070056 struct diff_queue_struct outq;
57 outq.queue = NULL;
58 outq.nr = outq.alloc = 0;
59
Petr Baudisd01d8c62006-03-29 02:16:33 +020060 if (opts & DIFF_PICKAXE_REGEX) {
61 int err;
62 err = regcomp(&regex, needle, REG_EXTENDED | REG_NEWLINE);
63 if (err) {
64 /* The POSIX.2 people are surely sick */
65 char errbuf[1024];
66 regerror(err, &regex, errbuf, 1024);
67 regfree(&regex);
68 die("invalid pickaxe regex: %s", errbuf);
69 }
70 regexp = &regex;
71 }
72
Junio C Hamano367cec12005-05-27 15:55:28 -070073 if (opts & DIFF_PICKAXE_ALL) {
74 /* Showing the whole changeset if needle exists */
75 for (i = has_changes = 0; !has_changes && i < q->nr; i++) {
76 struct diff_filepair *p = q->queue[i];
77 if (!DIFF_FILE_VALID(p->one)) {
78 if (!DIFF_FILE_VALID(p->two))
79 continue; /* ignore unmerged */
80 /* created */
Petr Baudisd01d8c62006-03-29 02:16:33 +020081 if (contains(p->two, needle, len, regexp))
Junio C Hamano367cec12005-05-27 15:55:28 -070082 has_changes++;
83 }
84 else if (!DIFF_FILE_VALID(p->two)) {
Petr Baudisd01d8c62006-03-29 02:16:33 +020085 if (contains(p->one, needle, len, regexp))
Junio C Hamano367cec12005-05-27 15:55:28 -070086 has_changes++;
87 }
88 else if (!diff_unmodified_pair(p) &&
Petr Baudisd01d8c62006-03-29 02:16:33 +020089 contains(p->one, needle, len, regexp) !=
90 contains(p->two, needle, len, regexp))
Junio C Hamano367cec12005-05-27 15:55:28 -070091 has_changes++;
Junio C Hamano52e95782005-05-21 02:40:01 -070092 }
Junio C Hamano367cec12005-05-27 15:55:28 -070093 if (has_changes)
94 return; /* not munge the queue */
95
96 /* otherwise we will clear the whole queue
97 * by copying the empty outq at the end of this
98 * function, but first clear the current entries
99 * in the queue.
100 */
101 for (i = 0; i < q->nr; i++)
102 diff_free_filepair(q->queue[i]);
Junio C Hamano52e95782005-05-21 02:40:01 -0700103 }
Junio C Hamano367cec12005-05-27 15:55:28 -0700104 else
105 /* Showing only the filepairs that has the needle */
106 for (i = 0; i < q->nr; i++) {
107 struct diff_filepair *p = q->queue[i];
108 has_changes = 0;
109 if (!DIFF_FILE_VALID(p->one)) {
110 if (!DIFF_FILE_VALID(p->two))
111 ; /* ignore unmerged */
112 /* created */
Petr Baudisd01d8c62006-03-29 02:16:33 +0200113 else if (contains(p->two, needle, len, regexp))
Junio C Hamano367cec12005-05-27 15:55:28 -0700114 has_changes = 1;
115 }
116 else if (!DIFF_FILE_VALID(p->two)) {
Petr Baudisd01d8c62006-03-29 02:16:33 +0200117 if (contains(p->one, needle, len, regexp))
Junio C Hamano367cec12005-05-27 15:55:28 -0700118 has_changes = 1;
119 }
120 else if (!diff_unmodified_pair(p) &&
Petr Baudisd01d8c62006-03-29 02:16:33 +0200121 contains(p->one, needle, len, regexp) !=
122 contains(p->two, needle, len, regexp))
Junio C Hamano367cec12005-05-27 15:55:28 -0700123 has_changes = 1;
124
125 if (has_changes)
126 diff_q(&outq, p);
127 else
128 diff_free_filepair(p);
129 }
130
Petr Baudisd01d8c62006-03-29 02:16:33 +0200131 if (opts & DIFF_PICKAXE_REGEX) {
132 regfree(&regex);
133 }
134
Junio C Hamano52e95782005-05-21 02:40:01 -0700135 free(q->queue);
136 *q = outq;
137 return;
138}