THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

506 lines
13KB

  1. #include "cache.h"
  2. #include "range-diff.h"
  3. #include "string-list.h"
  4. #include "run-command.h"
  5. #include "argv-array.h"
  6. #include "hashmap.h"
  7. #include "xdiff-interface.h"
  8. #include "linear-assignment.h"
  9. #include "diffcore.h"
  10. #include "commit.h"
  11. #include "pretty.h"
  12. #include "userdiff.h"
  13. struct patch_util {
  14. /* For the search for an exact match */
  15. struct hashmap_entry e;
  16. const char *diff, *patch;
  17. int i, shown;
  18. int diffsize;
  19. size_t diff_offset;
  20. /* the index of the matching item in the other branch, or -1 */
  21. int matching;
  22. struct object_id oid;
  23. };
  24. static size_t find_end_of_line(char *buffer, unsigned long size)
  25. {
  26. char *eol = memchr(buffer, '\n', size);
  27. if (!eol)
  28. return size;
  29. *eol = '\0';
  30. return eol + 1 - buffer;
  31. }
  32. /*
  33. * Reads the patches into a string list, with the `util` field being populated
  34. * as struct object_id (will need to be free()d).
  35. */
  36. static int read_patches(const char *range, struct string_list *list)
  37. {
  38. struct child_process cp = CHILD_PROCESS_INIT;
  39. struct strbuf buf = STRBUF_INIT, contents = STRBUF_INIT;
  40. struct patch_util *util = NULL;
  41. int in_header = 1;
  42. char *line;
  43. int offset, len;
  44. size_t size;
  45. argv_array_pushl(&cp.args, "log", "--no-color", "-p", "--no-merges",
  46. "--reverse", "--date-order", "--decorate=no",
  47. /*
  48. * Choose indicators that are not used anywhere
  49. * else in diffs, but still look reasonable
  50. * (e.g. will not be confusing when debugging)
  51. */
  52. "--output-indicator-new=>",
  53. "--output-indicator-old=<",
  54. "--output-indicator-context=#",
  55. "--no-abbrev-commit", range,
  56. NULL);
  57. cp.out = -1;
  58. cp.no_stdin = 1;
  59. cp.git_cmd = 1;
  60. if (start_command(&cp))
  61. return error_errno(_("could not start `log`"));
  62. if (strbuf_read(&contents, cp.out, 0) < 0) {
  63. error_errno(_("could not read `log` output"));
  64. finish_command(&cp);
  65. return -1;
  66. }
  67. line = contents.buf;
  68. size = contents.len;
  69. for (offset = 0; size > 0; offset += len, size -= len, line += len) {
  70. const char *p;
  71. len = find_end_of_line(line, size);
  72. line[len - 1] = '\0';
  73. if (skip_prefix(line, "commit ", &p)) {
  74. if (util) {
  75. string_list_append(list, buf.buf)->util = util;
  76. strbuf_reset(&buf);
  77. }
  78. util = xcalloc(sizeof(*util), 1);
  79. if (get_oid(p, &util->oid)) {
  80. error(_("could not parse commit '%s'"), p);
  81. free(util);
  82. string_list_clear(list, 1);
  83. strbuf_release(&buf);
  84. strbuf_release(&contents);
  85. finish_command(&cp);
  86. return -1;
  87. }
  88. util->matching = -1;
  89. in_header = 1;
  90. continue;
  91. }
  92. if (starts_with(line, "diff --git")) {
  93. in_header = 0;
  94. strbuf_addch(&buf, '\n');
  95. if (!util->diff_offset)
  96. util->diff_offset = buf.len;
  97. strbuf_addch(&buf, ' ');
  98. strbuf_addstr(&buf, line);
  99. } else if (in_header) {
  100. if (starts_with(line, "Author: ")) {
  101. strbuf_addstr(&buf, line);
  102. strbuf_addstr(&buf, "\n\n");
  103. } else if (starts_with(line, " ")) {
  104. p = line + len - 2;
  105. while (isspace(*p) && p >= line)
  106. p--;
  107. strbuf_add(&buf, line, p - line + 1);
  108. strbuf_addch(&buf, '\n');
  109. }
  110. continue;
  111. } else if (skip_prefix(line, "@@ ", &p)) {
  112. p = strstr(p, "@@");
  113. strbuf_addstr(&buf, p ? p : "@@");
  114. } else if (!line[0] || starts_with(line, "index "))
  115. /*
  116. * A completely blank (not ' \n', which is context)
  117. * line is not valid in a diff. We skip it
  118. * silently, because this neatly handles the blank
  119. * separator line between commits in git-log
  120. * output.
  121. *
  122. * We also want to ignore the diff's `index` lines
  123. * because they contain exact blob hashes in which
  124. * we are not interested.
  125. */
  126. continue;
  127. else if (line[0] == '>') {
  128. strbuf_addch(&buf, '+');
  129. strbuf_addstr(&buf, line + 1);
  130. } else if (line[0] == '<') {
  131. strbuf_addch(&buf, '-');
  132. strbuf_addstr(&buf, line + 1);
  133. } else if (line[0] == '#') {
  134. strbuf_addch(&buf, ' ');
  135. strbuf_addstr(&buf, line + 1);
  136. } else {
  137. strbuf_addch(&buf, ' ');
  138. strbuf_addstr(&buf, line);
  139. }
  140. strbuf_addch(&buf, '\n');
  141. util->diffsize++;
  142. }
  143. strbuf_release(&contents);
  144. if (util)
  145. string_list_append(list, buf.buf)->util = util;
  146. strbuf_release(&buf);
  147. if (finish_command(&cp))
  148. return -1;
  149. return 0;
  150. }
  151. static int patch_util_cmp(const void *dummy, const struct patch_util *a,
  152. const struct patch_util *b, const char *keydata)
  153. {
  154. return strcmp(a->diff, keydata ? keydata : b->diff);
  155. }
  156. static void find_exact_matches(struct string_list *a, struct string_list *b)
  157. {
  158. struct hashmap map;
  159. int i;
  160. hashmap_init(&map, (hashmap_cmp_fn)patch_util_cmp, NULL, 0);
  161. /* First, add the patches of a to a hash map */
  162. for (i = 0; i < a->nr; i++) {
  163. struct patch_util *util = a->items[i].util;
  164. util->i = i;
  165. util->patch = a->items[i].string;
  166. util->diff = util->patch + util->diff_offset;
  167. hashmap_entry_init(util, strhash(util->diff));
  168. hashmap_add(&map, util);
  169. }
  170. /* Now try to find exact matches in b */
  171. for (i = 0; i < b->nr; i++) {
  172. struct patch_util *util = b->items[i].util, *other;
  173. util->i = i;
  174. util->patch = b->items[i].string;
  175. util->diff = util->patch + util->diff_offset;
  176. hashmap_entry_init(util, strhash(util->diff));
  177. other = hashmap_remove(&map, util, NULL);
  178. if (other) {
  179. if (other->matching >= 0)
  180. BUG("already assigned!");
  181. other->matching = i;
  182. util->matching = other->i;
  183. }
  184. }
  185. hashmap_free(&map, 0);
  186. }
  187. static void diffsize_consume(void *data, char *line, unsigned long len)
  188. {
  189. (*(int *)data)++;
  190. }
  191. static void diffsize_hunk(void *data, long ob, long on, long nb, long nn,
  192. const char *funcline, long funclen)
  193. {
  194. diffsize_consume(data, NULL, 0);
  195. }
  196. static int diffsize(const char *a, const char *b)
  197. {
  198. xpparam_t pp = { 0 };
  199. xdemitconf_t cfg = { 0 };
  200. mmfile_t mf1, mf2;
  201. int count = 0;
  202. mf1.ptr = (char *)a;
  203. mf1.size = strlen(a);
  204. mf2.ptr = (char *)b;
  205. mf2.size = strlen(b);
  206. cfg.ctxlen = 3;
  207. if (!xdi_diff_outf(&mf1, &mf2,
  208. diffsize_hunk, diffsize_consume, &count,
  209. &pp, &cfg))
  210. return count;
  211. error(_("failed to generate diff"));
  212. return COST_MAX;
  213. }
  214. static void get_correspondences(struct string_list *a, struct string_list *b,
  215. int creation_factor)
  216. {
  217. int n = a->nr + b->nr;
  218. int *cost, c, *a2b, *b2a;
  219. int i, j;
  220. ALLOC_ARRAY(cost, st_mult(n, n));
  221. ALLOC_ARRAY(a2b, n);
  222. ALLOC_ARRAY(b2a, n);
  223. for (i = 0; i < a->nr; i++) {
  224. struct patch_util *a_util = a->items[i].util;
  225. for (j = 0; j < b->nr; j++) {
  226. struct patch_util *b_util = b->items[j].util;
  227. if (a_util->matching == j)
  228. c = 0;
  229. else if (a_util->matching < 0 && b_util->matching < 0)
  230. c = diffsize(a_util->diff, b_util->diff);
  231. else
  232. c = COST_MAX;
  233. cost[i + n * j] = c;
  234. }
  235. c = a_util->matching < 0 ?
  236. a_util->diffsize * creation_factor / 100 : COST_MAX;
  237. for (j = b->nr; j < n; j++)
  238. cost[i + n * j] = c;
  239. }
  240. for (j = 0; j < b->nr; j++) {
  241. struct patch_util *util = b->items[j].util;
  242. c = util->matching < 0 ?
  243. util->diffsize * creation_factor / 100 : COST_MAX;
  244. for (i = a->nr; i < n; i++)
  245. cost[i + n * j] = c;
  246. }
  247. for (i = a->nr; i < n; i++)
  248. for (j = b->nr; j < n; j++)
  249. cost[i + n * j] = 0;
  250. compute_assignment(n, n, cost, a2b, b2a);
  251. for (i = 0; i < a->nr; i++)
  252. if (a2b[i] >= 0 && a2b[i] < b->nr) {
  253. struct patch_util *a_util = a->items[i].util;
  254. struct patch_util *b_util = b->items[a2b[i]].util;
  255. a_util->matching = a2b[i];
  256. b_util->matching = i;
  257. }
  258. free(cost);
  259. free(a2b);
  260. free(b2a);
  261. }
  262. static void output_pair_header(struct diff_options *diffopt,
  263. int patch_no_width,
  264. struct strbuf *buf,
  265. struct strbuf *dashes,
  266. struct patch_util *a_util,
  267. struct patch_util *b_util)
  268. {
  269. struct object_id *oid = a_util ? &a_util->oid : &b_util->oid;
  270. struct commit *commit;
  271. char status;
  272. const char *color_reset = diff_get_color_opt(diffopt, DIFF_RESET);
  273. const char *color_old = diff_get_color_opt(diffopt, DIFF_FILE_OLD);
  274. const char *color_new = diff_get_color_opt(diffopt, DIFF_FILE_NEW);
  275. const char *color_commit = diff_get_color_opt(diffopt, DIFF_COMMIT);
  276. const char *color;
  277. if (!dashes->len)
  278. strbuf_addchars(dashes, '-',
  279. strlen(find_unique_abbrev(oid,
  280. DEFAULT_ABBREV)));
  281. if (!b_util) {
  282. color = color_old;
  283. status = '<';
  284. } else if (!a_util) {
  285. color = color_new;
  286. status = '>';
  287. } else if (strcmp(a_util->patch, b_util->patch)) {
  288. color = color_commit;
  289. status = '!';
  290. } else {
  291. color = color_commit;
  292. status = '=';
  293. }
  294. strbuf_reset(buf);
  295. strbuf_addstr(buf, status == '!' ? color_old : color);
  296. if (!a_util)
  297. strbuf_addf(buf, "%*s: %s ", patch_no_width, "-", dashes->buf);
  298. else
  299. strbuf_addf(buf, "%*d: %s ", patch_no_width, a_util->i + 1,
  300. find_unique_abbrev(&a_util->oid, DEFAULT_ABBREV));
  301. if (status == '!')
  302. strbuf_addf(buf, "%s%s", color_reset, color);
  303. strbuf_addch(buf, status);
  304. if (status == '!')
  305. strbuf_addf(buf, "%s%s", color_reset, color_new);
  306. if (!b_util)
  307. strbuf_addf(buf, " %*s: %s", patch_no_width, "-", dashes->buf);
  308. else
  309. strbuf_addf(buf, " %*d: %s", patch_no_width, b_util->i + 1,
  310. find_unique_abbrev(&b_util->oid, DEFAULT_ABBREV));
  311. commit = lookup_commit_reference(the_repository, oid);
  312. if (commit) {
  313. if (status == '!')
  314. strbuf_addf(buf, "%s%s", color_reset, color);
  315. strbuf_addch(buf, ' ');
  316. pp_commit_easy(CMIT_FMT_ONELINE, commit, buf);
  317. }
  318. strbuf_addf(buf, "%s\n", color_reset);
  319. fwrite(buf->buf, buf->len, 1, diffopt->file);
  320. }
  321. static struct userdiff_driver no_func_name = {
  322. .funcname = { "$^", 0 }
  323. };
  324. static struct diff_filespec *get_filespec(const char *name, const char *p)
  325. {
  326. struct diff_filespec *spec = alloc_filespec(name);
  327. fill_filespec(spec, &null_oid, 0, 0100644);
  328. spec->data = (char *)p;
  329. spec->size = strlen(p);
  330. spec->should_munmap = 0;
  331. spec->is_stdin = 1;
  332. spec->driver = &no_func_name;
  333. return spec;
  334. }
  335. static void patch_diff(const char *a, const char *b,
  336. struct diff_options *diffopt)
  337. {
  338. diff_queue(&diff_queued_diff,
  339. get_filespec("a", a), get_filespec("b", b));
  340. diffcore_std(diffopt);
  341. diff_flush(diffopt);
  342. }
  343. static void output(struct string_list *a, struct string_list *b,
  344. struct diff_options *diffopt)
  345. {
  346. struct strbuf buf = STRBUF_INIT, dashes = STRBUF_INIT;
  347. int patch_no_width = decimal_width(1 + (a->nr > b->nr ? a->nr : b->nr));
  348. int i = 0, j = 0;
  349. /*
  350. * We assume the user is really more interested in the second argument
  351. * ("newer" version). To that end, we print the output in the order of
  352. * the RHS (the `b` parameter). To put the LHS (the `a` parameter)
  353. * commits that are no longer in the RHS into a good place, we place
  354. * them once we have shown all of their predecessors in the LHS.
  355. */
  356. while (i < a->nr || j < b->nr) {
  357. struct patch_util *a_util, *b_util;
  358. a_util = i < a->nr ? a->items[i].util : NULL;
  359. b_util = j < b->nr ? b->items[j].util : NULL;
  360. /* Skip all the already-shown commits from the LHS. */
  361. while (i < a->nr && a_util->shown)
  362. a_util = ++i < a->nr ? a->items[i].util : NULL;
  363. /* Show unmatched LHS commit whose predecessors were shown. */
  364. if (i < a->nr && a_util->matching < 0) {
  365. output_pair_header(diffopt, patch_no_width,
  366. &buf, &dashes, a_util, NULL);
  367. i++;
  368. continue;
  369. }
  370. /* Show unmatched RHS commits. */
  371. while (j < b->nr && b_util->matching < 0) {
  372. output_pair_header(diffopt, patch_no_width,
  373. &buf, &dashes, NULL, b_util);
  374. b_util = ++j < b->nr ? b->items[j].util : NULL;
  375. }
  376. /* Show matching LHS/RHS pair. */
  377. if (j < b->nr) {
  378. a_util = a->items[b_util->matching].util;
  379. output_pair_header(diffopt, patch_no_width,
  380. &buf, &dashes, a_util, b_util);
  381. if (!(diffopt->output_format & DIFF_FORMAT_NO_OUTPUT))
  382. patch_diff(a->items[b_util->matching].string,
  383. b->items[j].string, diffopt);
  384. a_util->shown = 1;
  385. j++;
  386. }
  387. }
  388. strbuf_release(&buf);
  389. strbuf_release(&dashes);
  390. }
  391. static struct strbuf *output_prefix_cb(struct diff_options *opt, void *data)
  392. {
  393. return data;
  394. }
  395. int show_range_diff(const char *range1, const char *range2,
  396. int creation_factor, int dual_color,
  397. struct diff_options *diffopt)
  398. {
  399. int res = 0;
  400. struct string_list branch1 = STRING_LIST_INIT_DUP;
  401. struct string_list branch2 = STRING_LIST_INIT_DUP;
  402. if (read_patches(range1, &branch1))
  403. res = error(_("could not parse log for '%s'"), range1);
  404. if (!res && read_patches(range2, &branch2))
  405. res = error(_("could not parse log for '%s'"), range2);
  406. if (!res) {
  407. struct diff_options opts;
  408. struct strbuf indent = STRBUF_INIT;
  409. if (diffopt)
  410. memcpy(&opts, diffopt, sizeof(opts));
  411. else
  412. diff_setup(&opts);
  413. if (!opts.output_format)
  414. opts.output_format = DIFF_FORMAT_PATCH;
  415. opts.flags.suppress_diff_headers = 1;
  416. opts.flags.dual_color_diffed_diffs = dual_color;
  417. opts.output_prefix = output_prefix_cb;
  418. strbuf_addstr(&indent, " ");
  419. opts.output_prefix_data = &indent;
  420. diff_setup_done(&opts);
  421. find_exact_matches(&branch1, &branch2);
  422. get_correspondences(&branch1, &branch2, creation_factor);
  423. output(&branch1, &branch2, &opts);
  424. strbuf_release(&indent);
  425. }
  426. string_list_clear(&branch1, 1);
  427. string_list_clear(&branch2, 1);
  428. return res;
  429. }