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.

553 lines
14KB

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