blob: d0ef8397008824fb5139680856e3229ecf2c4eb1 [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;
René Scharfece163c72009-03-03 00:00:55 +010013 unsigned long sz;
Junio C Hamano52e95782005-05-21 02:40:01 -070014 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
René Scharfe50fd6992009-03-16 19:38:42 +010028 assert(data[sz] == '\0');
Petr Baudisd01d8c62006-03-29 02:16:33 +020029 while (*data && !regexec(regexp, data, 1, &regmatch, flags)) {
30 flags |= REG_NOTBOL;
René Scharfe50fd6992009-03-16 19:38:42 +010031 data += regmatch.rm_eo;
32 if (*data && regmatch.rm_so == regmatch.rm_eo)
33 data++;
Junio C Hamano2002eed2005-07-23 16:35:25 -070034 cnt++;
35 }
Petr Baudisd01d8c62006-03-29 02:16:33 +020036
37 } else { /* Classic exact string match */
René Scharfece163c72009-03-03 00:00:55 +010038 while (sz) {
39 const char *found = memmem(data, sz, needle, len);
40 if (!found)
41 break;
42 sz -= found - data + len;
43 data = found + len;
44 cnt++;
Petr Baudisd01d8c62006-03-29 02:16:33 +020045 }
Junio C Hamano2002eed2005-07-23 16:35:25 -070046 }
Junio C Hamanoa0cb9402007-05-07 01:24:27 -070047 diff_free_filespec_data(one);
Junio C Hamano2002eed2005-07-23 16:35:25 -070048 return cnt;
Junio C Hamano52e95782005-05-21 02:40:01 -070049}
50
Junio C Hamano367cec12005-05-27 15:55:28 -070051void diffcore_pickaxe(const char *needle, int opts)
Junio C Hamano52e95782005-05-21 02:40:01 -070052{
Junio C Hamano38c6f782005-05-21 19:40:36 -070053 struct diff_queue_struct *q = &diff_queued_diff;
Junio C Hamano52e95782005-05-21 02:40:01 -070054 unsigned long len = strlen(needle);
Junio C Hamano367cec12005-05-27 15:55:28 -070055 int i, has_changes;
Petr Baudisd01d8c62006-03-29 02:16:33 +020056 regex_t regex, *regexp = NULL;
Junio C Hamano52e95782005-05-21 02:40:01 -070057 struct diff_queue_struct outq;
58 outq.queue = NULL;
59 outq.nr = outq.alloc = 0;
60
Petr Baudisd01d8c62006-03-29 02:16:33 +020061 if (opts & DIFF_PICKAXE_REGEX) {
62 int err;
63 err = regcomp(&regex, needle, REG_EXTENDED | REG_NEWLINE);
64 if (err) {
65 /* The POSIX.2 people are surely sick */
66 char errbuf[1024];
67 regerror(err, &regex, errbuf, 1024);
68 regfree(&regex);
69 die("invalid pickaxe regex: %s", errbuf);
70 }
71 regexp = &regex;
72 }
73
Junio C Hamano367cec12005-05-27 15:55:28 -070074 if (opts & DIFF_PICKAXE_ALL) {
75 /* Showing the whole changeset if needle exists */
76 for (i = has_changes = 0; !has_changes && i < q->nr; i++) {
77 struct diff_filepair *p = q->queue[i];
78 if (!DIFF_FILE_VALID(p->one)) {
79 if (!DIFF_FILE_VALID(p->two))
80 continue; /* ignore unmerged */
81 /* created */
Petr Baudisd01d8c62006-03-29 02:16:33 +020082 if (contains(p->two, needle, len, regexp))
Junio C Hamano367cec12005-05-27 15:55:28 -070083 has_changes++;
84 }
85 else if (!DIFF_FILE_VALID(p->two)) {
Petr Baudisd01d8c62006-03-29 02:16:33 +020086 if (contains(p->one, needle, len, regexp))
Junio C Hamano367cec12005-05-27 15:55:28 -070087 has_changes++;
88 }
89 else if (!diff_unmodified_pair(p) &&
Petr Baudisd01d8c62006-03-29 02:16:33 +020090 contains(p->one, needle, len, regexp) !=
91 contains(p->two, needle, len, regexp))
Junio C Hamano367cec12005-05-27 15:55:28 -070092 has_changes++;
Junio C Hamano52e95782005-05-21 02:40:01 -070093 }
Junio C Hamano367cec12005-05-27 15:55:28 -070094 if (has_changes)
95 return; /* not munge the queue */
96
97 /* otherwise we will clear the whole queue
98 * by copying the empty outq at the end of this
99 * function, but first clear the current entries
100 * in the queue.
101 */
102 for (i = 0; i < q->nr; i++)
103 diff_free_filepair(q->queue[i]);
Junio C Hamano52e95782005-05-21 02:40:01 -0700104 }
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700105 else
Junio C Hamano367cec12005-05-27 15:55:28 -0700106 /* Showing only the filepairs that has the needle */
107 for (i = 0; i < q->nr; i++) {
108 struct diff_filepair *p = q->queue[i];
109 has_changes = 0;
110 if (!DIFF_FILE_VALID(p->one)) {
111 if (!DIFF_FILE_VALID(p->two))
112 ; /* ignore unmerged */
113 /* created */
Petr Baudisd01d8c62006-03-29 02:16:33 +0200114 else if (contains(p->two, needle, len, regexp))
Junio C Hamano367cec12005-05-27 15:55:28 -0700115 has_changes = 1;
116 }
117 else if (!DIFF_FILE_VALID(p->two)) {
Petr Baudisd01d8c62006-03-29 02:16:33 +0200118 if (contains(p->one, needle, len, regexp))
Junio C Hamano367cec12005-05-27 15:55:28 -0700119 has_changes = 1;
120 }
121 else if (!diff_unmodified_pair(p) &&
Petr Baudisd01d8c62006-03-29 02:16:33 +0200122 contains(p->one, needle, len, regexp) !=
123 contains(p->two, needle, len, regexp))
Junio C Hamano367cec12005-05-27 15:55:28 -0700124 has_changes = 1;
125
126 if (has_changes)
127 diff_q(&outq, p);
128 else
129 diff_free_filepair(p);
130 }
131
Petr Baudisd01d8c62006-03-29 02:16:33 +0200132 if (opts & DIFF_PICKAXE_REGEX) {
133 regfree(&regex);
134 }
135
Junio C Hamano52e95782005-05-21 02:40:01 -0700136 free(q->queue);
137 *q = outq;
138 return;
139}