diffcore-pickaxe.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. #include "commit.h"
  11. #include "quote.h"
  12. typedef int (*pickaxe_fn)(mmfile_t *one, mmfile_t *two,
  13. struct diff_options *o,
  14. regex_t *regexp, kwset_t kws);
  15. struct diffgrep_cb {
  16. regex_t *regexp;
  17. int hit;
  18. };
  19. static void diffgrep_consume(void *priv, char *line, unsigned long len)
  20. {
  21. struct diffgrep_cb *data = priv;
  22. regmatch_t regmatch;
  23. if (line[0] != '+' && line[0] != '-')
  24. return;
  25. if (data->hit)
  26. /*
  27. * NEEDSWORK: we should have a way to terminate the
  28. * caller early.
  29. */
  30. return;
  31. data->hit = !regexec_buf(data->regexp, line + 1, len - 1, 1,
  32. &regmatch, 0);
  33. }
  34. static int diff_grep(mmfile_t *one, mmfile_t *two,
  35. struct diff_options *o,
  36. regex_t *regexp, kwset_t kws)
  37. {
  38. regmatch_t regmatch;
  39. struct diffgrep_cb ecbdata;
  40. xpparam_t xpp;
  41. xdemitconf_t xecfg;
  42. if (!one)
  43. return !regexec_buf(regexp, two->ptr, two->size,
  44. 1, &regmatch, 0);
  45. if (!two)
  46. return !regexec_buf(regexp, one->ptr, one->size,
  47. 1, &regmatch, 0);
  48. /*
  49. * We have both sides; need to run textual diff and see if
  50. * the pattern appears on added/deleted lines.
  51. */
  52. memset(&xpp, 0, sizeof(xpp));
  53. memset(&xecfg, 0, sizeof(xecfg));
  54. ecbdata.regexp = regexp;
  55. ecbdata.hit = 0;
  56. xecfg.ctxlen = o->context;
  57. xecfg.interhunkctxlen = o->interhunkcontext;
  58. if (xdi_diff_outf(one, two, discard_hunk_line, diffgrep_consume,
  59. &ecbdata, &xpp, &xecfg))
  60. return 0;
  61. return ecbdata.hit;
  62. }
  63. static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws)
  64. {
  65. unsigned int cnt;
  66. unsigned long sz;
  67. const char *data;
  68. sz = mf->size;
  69. data = mf->ptr;
  70. cnt = 0;
  71. if (regexp) {
  72. regmatch_t regmatch;
  73. int flags = 0;
  74. while (sz && *data &&
  75. !regexec_buf(regexp, data, sz, 1, &regmatch, flags)) {
  76. flags |= REG_NOTBOL;
  77. data += regmatch.rm_eo;
  78. sz -= regmatch.rm_eo;
  79. if (sz && *data && regmatch.rm_so == regmatch.rm_eo) {
  80. data++;
  81. sz--;
  82. }
  83. cnt++;
  84. }
  85. } else { /* Classic exact string match */
  86. while (sz) {
  87. struct kwsmatch kwsm;
  88. size_t offset = kwsexec(kws, data, sz, &kwsm);
  89. if (offset == -1)
  90. break;
  91. sz -= offset + kwsm.size[0];
  92. data += offset + kwsm.size[0];
  93. cnt++;
  94. }
  95. }
  96. return cnt;
  97. }
  98. static int has_changes(mmfile_t *one, mmfile_t *two,
  99. struct diff_options *o,
  100. regex_t *regexp, kwset_t kws)
  101. {
  102. unsigned int one_contains = one ? contains(one, regexp, kws) : 0;
  103. unsigned int two_contains = two ? contains(two, regexp, kws) : 0;
  104. return one_contains != two_contains;
  105. }
  106. static int pickaxe_match(struct diff_filepair *p, struct diff_options *o,
  107. regex_t *regexp, kwset_t kws, pickaxe_fn fn)
  108. {
  109. struct userdiff_driver *textconv_one = NULL;
  110. struct userdiff_driver *textconv_two = NULL;
  111. mmfile_t mf1, mf2;
  112. int ret;
  113. /* ignore unmerged */
  114. if (!DIFF_FILE_VALID(p->one) && !DIFF_FILE_VALID(p->two))
  115. return 0;
  116. if (o->objfind) {
  117. return (DIFF_FILE_VALID(p->one) &&
  118. oidset_contains(o->objfind, &p->one->oid)) ||
  119. (DIFF_FILE_VALID(p->two) &&
  120. oidset_contains(o->objfind, &p->two->oid));
  121. }
  122. if (!o->pickaxe[0])
  123. return 0;
  124. if (o->flags.allow_textconv) {
  125. textconv_one = get_textconv(o->repo, p->one);
  126. textconv_two = get_textconv(o->repo, p->two);
  127. }
  128. /*
  129. * If we have an unmodified pair, we know that the count will be the
  130. * same and don't even have to load the blobs. Unless textconv is in
  131. * play, _and_ we are using two different textconv filters (e.g.,
  132. * because a pair is an exact rename with different textconv attributes
  133. * for each side, which might generate different content).
  134. */
  135. if (textconv_one == textconv_two && diff_unmodified_pair(p))
  136. return 0;
  137. if ((o->pickaxe_opts & DIFF_PICKAXE_KIND_G) &&
  138. !o->flags.text &&
  139. ((!textconv_one && diff_filespec_is_binary(o->repo, p->one)) ||
  140. (!textconv_two && diff_filespec_is_binary(o->repo, p->two))))
  141. return 0;
  142. mf1.size = fill_textconv(o->repo, textconv_one, p->one, &mf1.ptr);
  143. mf2.size = fill_textconv(o->repo, textconv_two, p->two, &mf2.ptr);
  144. ret = fn(DIFF_FILE_VALID(p->one) ? &mf1 : NULL,
  145. DIFF_FILE_VALID(p->two) ? &mf2 : NULL,
  146. o, regexp, kws);
  147. if (textconv_one)
  148. free(mf1.ptr);
  149. if (textconv_two)
  150. free(mf2.ptr);
  151. diff_free_filespec_data(p->one);
  152. diff_free_filespec_data(p->two);
  153. return ret;
  154. }
  155. static void pickaxe(struct diff_queue_struct *q, struct diff_options *o,
  156. regex_t *regexp, kwset_t kws, pickaxe_fn fn)
  157. {
  158. int i;
  159. struct diff_queue_struct outq;
  160. DIFF_QUEUE_CLEAR(&outq);
  161. if (o->pickaxe_opts & DIFF_PICKAXE_ALL) {
  162. /* Showing the whole changeset if needle exists */
  163. for (i = 0; i < q->nr; i++) {
  164. struct diff_filepair *p = q->queue[i];
  165. if (pickaxe_match(p, o, regexp, kws, fn))
  166. return; /* do not munge the queue */
  167. }
  168. /*
  169. * Otherwise we will clear the whole queue by copying
  170. * the empty outq at the end of this function, but
  171. * first clear the current entries in the queue.
  172. */
  173. for (i = 0; i < q->nr; i++)
  174. diff_free_filepair(q->queue[i]);
  175. } else {
  176. /* Showing only the filepairs that has the needle */
  177. for (i = 0; i < q->nr; i++) {
  178. struct diff_filepair *p = q->queue[i];
  179. if (pickaxe_match(p, o, regexp, kws, fn))
  180. diff_q(&outq, p);
  181. else
  182. diff_free_filepair(p);
  183. }
  184. }
  185. free(q->queue);
  186. *q = outq;
  187. }
  188. static void regcomp_or_die(regex_t *regex, const char *needle, int cflags)
  189. {
  190. int err = regcomp(regex, needle, cflags);
  191. if (err) {
  192. /* The POSIX.2 people are surely sick */
  193. char errbuf[1024];
  194. regerror(err, regex, errbuf, 1024);
  195. die("invalid regex: %s", errbuf);
  196. }
  197. }
  198. void diffcore_pickaxe(struct diff_options *o)
  199. {
  200. const char *needle = o->pickaxe;
  201. int opts = o->pickaxe_opts;
  202. regex_t regex, *regexp = NULL;
  203. kwset_t kws = NULL;
  204. if (opts & (DIFF_PICKAXE_REGEX | DIFF_PICKAXE_KIND_G)) {
  205. int cflags = REG_EXTENDED | REG_NEWLINE;
  206. if (o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE)
  207. cflags |= REG_ICASE;
  208. regcomp_or_die(&regex, needle, cflags);
  209. regexp = &regex;
  210. } else if (opts & DIFF_PICKAXE_KIND_S) {
  211. if (o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE &&
  212. has_non_ascii(needle)) {
  213. struct strbuf sb = STRBUF_INIT;
  214. int cflags = REG_NEWLINE | REG_ICASE;
  215. basic_regex_quote_buf(&sb, needle);
  216. regcomp_or_die(&regex, sb.buf, cflags);
  217. strbuf_release(&sb);
  218. regexp = &regex;
  219. } else {
  220. kws = kwsalloc(o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE
  221. ? tolower_trans_tbl : NULL);
  222. kwsincr(kws, needle, strlen(needle));
  223. kwsprep(kws);
  224. }
  225. }
  226. pickaxe(&diff_queued_diff, o, regexp, kws,
  227. (opts & DIFF_PICKAXE_KIND_G) ? diff_grep : has_changes);
  228. if (regexp)
  229. regfree(regexp);
  230. if (kws)
  231. kwsfree(kws);
  232. return;
  233. }