Splitting a hunk that adds a line at the top fails in "add -p"
[git/mjg.git] / diffcore-pickaxe.c
blobd0ef8397008824fb5139680856e3229ecf2c4eb1
1 /*
2 * Copyright (C) 2005 Junio C Hamano
3 */
4 #include "cache.h"
5 #include "diff.h"
6 #include "diffcore.h"
8 static unsigned int contains(struct diff_filespec *one,
9 const char *needle, unsigned long len,
10 regex_t *regexp)
12 unsigned int cnt;
13 unsigned long sz;
14 const char *data;
15 if (diff_populate_filespec(one, 0))
16 return 0;
17 if (!len)
18 return 0;
20 sz = one->size;
21 data = one->data;
22 cnt = 0;
24 if (regexp) {
25 regmatch_t regmatch;
26 int flags = 0;
28 assert(data[sz] == '\0');
29 while (*data && !regexec(regexp, data, 1, &regmatch, flags)) {
30 flags |= REG_NOTBOL;
31 data += regmatch.rm_eo;
32 if (*data && regmatch.rm_so == regmatch.rm_eo)
33 data++;
34 cnt++;
37 } else { /* Classic exact string match */
38 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++;
47 diff_free_filespec_data(one);
48 return cnt;
51 void diffcore_pickaxe(const char *needle, int opts)
53 struct diff_queue_struct *q = &diff_queued_diff;
54 unsigned long len = strlen(needle);
55 int i, has_changes;
56 regex_t regex, *regexp = NULL;
57 struct diff_queue_struct outq;
58 outq.queue = NULL;
59 outq.nr = outq.alloc = 0;
61 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);
71 regexp = &regex;
74 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 */
82 if (contains(p->two, needle, len, regexp))
83 has_changes++;
85 else if (!DIFF_FILE_VALID(p->two)) {
86 if (contains(p->one, needle, len, regexp))
87 has_changes++;
89 else if (!diff_unmodified_pair(p) &&
90 contains(p->one, needle, len, regexp) !=
91 contains(p->two, needle, len, regexp))
92 has_changes++;
94 if (has_changes)
95 return; /* not munge the queue */
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.
102 for (i = 0; i < q->nr; i++)
103 diff_free_filepair(q->queue[i]);
105 else
106 /* 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 */
114 else if (contains(p->two, needle, len, regexp))
115 has_changes = 1;
117 else if (!DIFF_FILE_VALID(p->two)) {
118 if (contains(p->one, needle, len, regexp))
119 has_changes = 1;
121 else if (!diff_unmodified_pair(p) &&
122 contains(p->one, needle, len, regexp) !=
123 contains(p->two, needle, len, regexp))
124 has_changes = 1;
126 if (has_changes)
127 diff_q(&outq, p);
128 else
129 diff_free_filepair(p);
132 if (opts & DIFF_PICKAXE_REGEX) {
133 regfree(&regex);
136 free(q->queue);
137 *q = outq;
138 return;