| 1 | /* |
| 2 | * Copyright (C) 2005 Junio C Hamano |
| 3 | * Copyright (C) 2010 Google Inc. |
| 4 | */ |
| 5 | #include "cache.h" |
| 6 | #include "diff.h" |
| 7 | #include "diffcore.h" |
| 8 | #include "xdiff-interface.h" |
| 9 | #include "kwset.h" |
| 10 | |
| 11 | struct diffgrep_cb { |
| 12 | regex_t *regexp; |
| 13 | int hit; |
| 14 | }; |
| 15 | |
| 16 | static void diffgrep_consume(void *priv, char *line, unsigned long len) |
| 17 | { |
| 18 | struct diffgrep_cb *data = priv; |
| 19 | regmatch_t regmatch; |
| 20 | int hold; |
| 21 | |
| 22 | if (line[0] != '+' && line[0] != '-') |
| 23 | return; |
| 24 | if (data->hit) |
| 25 | /* |
| 26 | * NEEDSWORK: we should have a way to terminate the |
| 27 | * caller early. |
| 28 | */ |
| 29 | return; |
| 30 | /* Yuck -- line ought to be "const char *"! */ |
| 31 | hold = line[len]; |
| 32 | line[len] = '\0'; |
| 33 | data->hit = !regexec(data->regexp, line + 1, 1, ®match, 0); |
| 34 | line[len] = hold; |
| 35 | } |
| 36 | |
| 37 | static void fill_one(struct diff_filespec *one, |
| 38 | mmfile_t *mf, struct userdiff_driver **textconv) |
| 39 | { |
| 40 | if (DIFF_FILE_VALID(one)) { |
| 41 | *textconv = get_textconv(one); |
| 42 | mf->size = fill_textconv(*textconv, one, &mf->ptr); |
| 43 | } else { |
| 44 | memset(mf, 0, sizeof(*mf)); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | static int diff_grep(struct diff_filepair *p, regex_t *regexp, struct diff_options *o) |
| 49 | { |
| 50 | regmatch_t regmatch; |
| 51 | struct userdiff_driver *textconv_one = NULL; |
| 52 | struct userdiff_driver *textconv_two = NULL; |
| 53 | mmfile_t mf1, mf2; |
| 54 | int hit; |
| 55 | |
| 56 | if (diff_unmodified_pair(p)) |
| 57 | return 0; |
| 58 | |
| 59 | fill_one(p->one, &mf1, &textconv_one); |
| 60 | fill_one(p->two, &mf2, &textconv_two); |
| 61 | |
| 62 | if (!mf1.ptr) { |
| 63 | if (!mf2.ptr) |
| 64 | return 0; /* ignore unmerged */ |
| 65 | /* created "two" -- does it have what we are looking for? */ |
| 66 | hit = !regexec(regexp, p->two->data, 1, ®match, 0); |
| 67 | } else if (!mf2.ptr) { |
| 68 | /* removed "one" -- did it have what we are looking for? */ |
| 69 | hit = !regexec(regexp, p->one->data, 1, ®match, 0); |
| 70 | } else { |
| 71 | /* |
| 72 | * We have both sides; need to run textual diff and see if |
| 73 | * the pattern appears on added/deleted lines. |
| 74 | */ |
| 75 | struct diffgrep_cb ecbdata; |
| 76 | xpparam_t xpp; |
| 77 | xdemitconf_t xecfg; |
| 78 | |
| 79 | memset(&xpp, 0, sizeof(xpp)); |
| 80 | memset(&xecfg, 0, sizeof(xecfg)); |
| 81 | ecbdata.regexp = regexp; |
| 82 | ecbdata.hit = 0; |
| 83 | xecfg.ctxlen = o->context; |
| 84 | xecfg.interhunkctxlen = o->interhunkcontext; |
| 85 | xdi_diff_outf(&mf1, &mf2, diffgrep_consume, &ecbdata, |
| 86 | &xpp, &xecfg); |
| 87 | hit = ecbdata.hit; |
| 88 | } |
| 89 | if (textconv_one) |
| 90 | free(mf1.ptr); |
| 91 | if (textconv_two) |
| 92 | free(mf2.ptr); |
| 93 | return hit; |
| 94 | } |
| 95 | |
| 96 | static void diffcore_pickaxe_grep(struct diff_options *o) |
| 97 | { |
| 98 | struct diff_queue_struct *q = &diff_queued_diff; |
| 99 | int i, err; |
| 100 | regex_t regex; |
| 101 | struct diff_queue_struct outq; |
| 102 | outq.queue = NULL; |
| 103 | outq.nr = outq.alloc = 0; |
| 104 | |
| 105 | err = regcomp(®ex, o->pickaxe, REG_EXTENDED | REG_NEWLINE); |
| 106 | if (err) { |
| 107 | char errbuf[1024]; |
| 108 | regerror(err, ®ex, errbuf, 1024); |
| 109 | regfree(®ex); |
| 110 | die("invalid log-grep regex: %s", errbuf); |
| 111 | } |
| 112 | |
| 113 | if (o->pickaxe_opts & DIFF_PICKAXE_ALL) { |
| 114 | /* Showing the whole changeset if needle exists */ |
| 115 | for (i = 0; i < q->nr; i++) { |
| 116 | struct diff_filepair *p = q->queue[i]; |
| 117 | if (diff_grep(p, ®ex, o)) |
| 118 | goto out; /* do not munge the queue */ |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * Otherwise we will clear the whole queue by copying |
| 123 | * the empty outq at the end of this function, but |
| 124 | * first clear the current entries in the queue. |
| 125 | */ |
| 126 | for (i = 0; i < q->nr; i++) |
| 127 | diff_free_filepair(q->queue[i]); |
| 128 | } else { |
| 129 | /* Showing only the filepairs that has the needle */ |
| 130 | for (i = 0; i < q->nr; i++) { |
| 131 | struct diff_filepair *p = q->queue[i]; |
| 132 | if (diff_grep(p, ®ex, o)) |
| 133 | diff_q(&outq, p); |
| 134 | else |
| 135 | diff_free_filepair(p); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | free(q->queue); |
| 140 | *q = outq; |
| 141 | |
| 142 | out: |
| 143 | regfree(®ex); |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | static unsigned int contains(struct diff_filespec *one, |
| 148 | const char *needle, unsigned long len, |
| 149 | regex_t *regexp, kwset_t kws) |
| 150 | { |
| 151 | unsigned int cnt; |
| 152 | unsigned long sz; |
| 153 | const char *data; |
| 154 | if (!len) |
| 155 | return 0; |
| 156 | if (diff_populate_filespec(one, 0)) |
| 157 | return 0; |
| 158 | |
| 159 | sz = one->size; |
| 160 | data = one->data; |
| 161 | cnt = 0; |
| 162 | |
| 163 | if (regexp) { |
| 164 | regmatch_t regmatch; |
| 165 | int flags = 0; |
| 166 | |
| 167 | assert(data[sz] == '\0'); |
| 168 | while (*data && !regexec(regexp, data, 1, ®match, flags)) { |
| 169 | flags |= REG_NOTBOL; |
| 170 | data += regmatch.rm_eo; |
| 171 | if (*data && regmatch.rm_so == regmatch.rm_eo) |
| 172 | data++; |
| 173 | cnt++; |
| 174 | } |
| 175 | |
| 176 | } else { /* Classic exact string match */ |
| 177 | while (sz) { |
| 178 | size_t offset = kwsexec(kws, data, sz, NULL); |
| 179 | const char *found; |
| 180 | if (offset == -1) |
| 181 | break; |
| 182 | else |
| 183 | found = data + offset; |
| 184 | sz -= found - data + len; |
| 185 | data = found + len; |
| 186 | cnt++; |
| 187 | } |
| 188 | } |
| 189 | diff_free_filespec_data(one); |
| 190 | return cnt; |
| 191 | } |
| 192 | |
| 193 | static void diffcore_pickaxe_count(struct diff_options *o) |
| 194 | { |
| 195 | const char *needle = o->pickaxe; |
| 196 | int opts = o->pickaxe_opts; |
| 197 | struct diff_queue_struct *q = &diff_queued_diff; |
| 198 | unsigned long len = strlen(needle); |
| 199 | int i, has_changes; |
| 200 | regex_t regex, *regexp = NULL; |
| 201 | kwset_t kws = NULL; |
| 202 | struct diff_queue_struct outq; |
| 203 | DIFF_QUEUE_CLEAR(&outq); |
| 204 | |
| 205 | if (opts & DIFF_PICKAXE_REGEX) { |
| 206 | int err; |
| 207 | err = regcomp(®ex, needle, REG_EXTENDED | REG_NEWLINE); |
| 208 | if (err) { |
| 209 | /* The POSIX.2 people are surely sick */ |
| 210 | char errbuf[1024]; |
| 211 | regerror(err, ®ex, errbuf, 1024); |
| 212 | regfree(®ex); |
| 213 | die("invalid pickaxe regex: %s", errbuf); |
| 214 | } |
| 215 | regexp = ®ex; |
| 216 | } else { |
| 217 | kws = kwsalloc(NULL); |
| 218 | kwsincr(kws, needle, len); |
| 219 | kwsprep(kws); |
| 220 | } |
| 221 | |
| 222 | if (opts & DIFF_PICKAXE_ALL) { |
| 223 | /* Showing the whole changeset if needle exists */ |
| 224 | for (i = 0; i < q->nr; i++) { |
| 225 | struct diff_filepair *p = q->queue[i]; |
| 226 | if (!DIFF_FILE_VALID(p->one)) { |
| 227 | if (!DIFF_FILE_VALID(p->two)) |
| 228 | continue; /* ignore unmerged */ |
| 229 | /* created */ |
| 230 | if (contains(p->two, needle, len, regexp, kws)) |
| 231 | has_changes++; |
| 232 | } |
| 233 | else if (!DIFF_FILE_VALID(p->two)) { |
| 234 | if (contains(p->one, needle, len, regexp, kws)) |
| 235 | has_changes++; |
| 236 | } |
| 237 | else if (!diff_unmodified_pair(p) && |
| 238 | contains(p->one, needle, len, regexp, kws) != |
| 239 | contains(p->two, needle, len, regexp, kws)) |
| 240 | has_changes++; |
| 241 | if (has_changes) |
| 242 | goto out; /* do not munge the queue */ |
| 243 | } |
| 244 | |
| 245 | /* otherwise we will clear the whole queue |
| 246 | * by copying the empty outq at the end of this |
| 247 | * function, but first clear the current entries |
| 248 | * in the queue. |
| 249 | */ |
| 250 | for (i = 0; i < q->nr; i++) |
| 251 | diff_free_filepair(q->queue[i]); |
| 252 | } |
| 253 | else |
| 254 | /* Showing only the filepairs that has the needle */ |
| 255 | for (i = 0; i < q->nr; i++) { |
| 256 | struct diff_filepair *p = q->queue[i]; |
| 257 | has_changes = 0; |
| 258 | if (!DIFF_FILE_VALID(p->one)) { |
| 259 | if (!DIFF_FILE_VALID(p->two)) |
| 260 | ; /* ignore unmerged */ |
| 261 | /* created */ |
| 262 | else if (contains(p->two, needle, len, regexp, |
| 263 | kws)) |
| 264 | has_changes = 1; |
| 265 | } |
| 266 | else if (!DIFF_FILE_VALID(p->two)) { |
| 267 | if (contains(p->one, needle, len, regexp, kws)) |
| 268 | has_changes = 1; |
| 269 | } |
| 270 | else if (!diff_unmodified_pair(p) && |
| 271 | contains(p->one, needle, len, regexp, kws) != |
| 272 | contains(p->two, needle, len, regexp, kws)) |
| 273 | has_changes = 1; |
| 274 | |
| 275 | if (has_changes) |
| 276 | diff_q(&outq, p); |
| 277 | else |
| 278 | diff_free_filepair(p); |
| 279 | } |
| 280 | |
| 281 | free(q->queue); |
| 282 | *q = outq; |
| 283 | |
| 284 | out: |
| 285 | if (opts & DIFF_PICKAXE_REGEX) |
| 286 | regfree(®ex); |
| 287 | else |
| 288 | kwsfree(kws); |
| 289 | return; |
| 290 | } |
| 291 | |
| 292 | void diffcore_pickaxe(struct diff_options *o) |
| 293 | { |
| 294 | /* Might want to warn when both S and G are on; I don't care... */ |
| 295 | if (o->pickaxe_opts & DIFF_PICKAXE_KIND_G) |
| 296 | diffcore_pickaxe_grep(o); |
| 297 | else |
| 298 | diffcore_pickaxe_count(o); |
| 299 | } |