PageRenderTime 166ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/combine-diff.c

https://gitlab.com/nmusco/git
C | 1426 lines | 1097 code | 151 blank | 178 comment | 302 complexity | c5acb7d22dca00f13c06e3dfa6edc29f MD5 | raw file
  1. #include "cache.h"
  2. #include "commit.h"
  3. #include "blob.h"
  4. #include "diff.h"
  5. #include "diffcore.h"
  6. #include "quote.h"
  7. #include "xdiff-interface.h"
  8. #include "xdiff/xmacros.h"
  9. #include "log-tree.h"
  10. #include "refs.h"
  11. #include "userdiff.h"
  12. #include "sha1-array.h"
  13. #include "revision.h"
  14. static struct combine_diff_path *intersect_paths(struct combine_diff_path *curr, int n, int num_parent)
  15. {
  16. struct diff_queue_struct *q = &diff_queued_diff;
  17. struct combine_diff_path *p, **tail = &curr;
  18. int i, cmp;
  19. if (!n) {
  20. for (i = 0; i < q->nr; i++) {
  21. int len;
  22. const char *path;
  23. if (diff_unmodified_pair(q->queue[i]))
  24. continue;
  25. path = q->queue[i]->two->path;
  26. len = strlen(path);
  27. p = xmalloc(combine_diff_path_size(num_parent, len));
  28. p->path = (char *) &(p->parent[num_parent]);
  29. memcpy(p->path, path, len);
  30. p->path[len] = 0;
  31. p->next = NULL;
  32. memset(p->parent, 0,
  33. sizeof(p->parent[0]) * num_parent);
  34. hashcpy(p->sha1, q->queue[i]->two->sha1);
  35. p->mode = q->queue[i]->two->mode;
  36. hashcpy(p->parent[n].sha1, q->queue[i]->one->sha1);
  37. p->parent[n].mode = q->queue[i]->one->mode;
  38. p->parent[n].status = q->queue[i]->status;
  39. *tail = p;
  40. tail = &p->next;
  41. }
  42. return curr;
  43. }
  44. /*
  45. * paths in curr (linked list) and q->queue[] (array) are
  46. * both sorted in the tree order.
  47. */
  48. i = 0;
  49. while ((p = *tail) != NULL) {
  50. cmp = ((i >= q->nr)
  51. ? -1 : strcmp(p->path, q->queue[i]->two->path));
  52. if (cmp < 0) {
  53. /* p->path not in q->queue[]; drop it */
  54. *tail = p->next;
  55. free(p);
  56. continue;
  57. }
  58. if (cmp > 0) {
  59. /* q->queue[i] not in p->path; skip it */
  60. i++;
  61. continue;
  62. }
  63. hashcpy(p->parent[n].sha1, q->queue[i]->one->sha1);
  64. p->parent[n].mode = q->queue[i]->one->mode;
  65. p->parent[n].status = q->queue[i]->status;
  66. tail = &p->next;
  67. i++;
  68. }
  69. return curr;
  70. }
  71. /* Lines lost from parent */
  72. struct lline {
  73. struct lline *next, *prev;
  74. int len;
  75. unsigned long parent_map;
  76. char line[FLEX_ARRAY];
  77. };
  78. /* Lines lost from current parent (before coalescing) */
  79. struct plost {
  80. struct lline *lost_head, *lost_tail;
  81. int len;
  82. };
  83. /* Lines surviving in the merge result */
  84. struct sline {
  85. /* Accumulated and coalesced lost lines */
  86. struct lline *lost;
  87. int lenlost;
  88. struct plost plost;
  89. char *bol;
  90. int len;
  91. /* bit 0 up to (N-1) are on if the parent has this line (i.e.
  92. * we did not change it).
  93. * bit N is used for "interesting" lines, including context.
  94. * bit (N+1) is used for "do not show deletion before this".
  95. */
  96. unsigned long flag;
  97. unsigned long *p_lno;
  98. };
  99. static int match_string_spaces(const char *line1, int len1,
  100. const char *line2, int len2,
  101. long flags)
  102. {
  103. if (flags & XDF_WHITESPACE_FLAGS) {
  104. for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--);
  105. for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--);
  106. }
  107. if (!(flags & (XDF_IGNORE_WHITESPACE | XDF_IGNORE_WHITESPACE_CHANGE)))
  108. return (len1 == len2 && !memcmp(line1, line2, len1));
  109. while (len1 > 0 && len2 > 0) {
  110. len1--;
  111. len2--;
  112. if (XDL_ISSPACE(line1[len1]) || XDL_ISSPACE(line2[len2])) {
  113. if ((flags & XDF_IGNORE_WHITESPACE_CHANGE) &&
  114. (!XDL_ISSPACE(line1[len1]) || !XDL_ISSPACE(line2[len2])))
  115. return 0;
  116. for (; len1 > 0 && XDL_ISSPACE(line1[len1]); len1--);
  117. for (; len2 > 0 && XDL_ISSPACE(line2[len2]); len2--);
  118. }
  119. if (line1[len1] != line2[len2])
  120. return 0;
  121. }
  122. if (flags & XDF_IGNORE_WHITESPACE) {
  123. /* Consume remaining spaces */
  124. for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--);
  125. for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--);
  126. }
  127. /* We matched full line1 and line2 */
  128. if (!len1 && !len2)
  129. return 1;
  130. return 0;
  131. }
  132. enum coalesce_direction { MATCH, BASE, NEW };
  133. /* Coalesce new lines into base by finding LCS */
  134. static struct lline *coalesce_lines(struct lline *base, int *lenbase,
  135. struct lline *new, int lennew,
  136. unsigned long parent, long flags)
  137. {
  138. int **lcs;
  139. enum coalesce_direction **directions;
  140. struct lline *baseend, *newend = NULL;
  141. int i, j, origbaselen = *lenbase;
  142. if (new == NULL)
  143. return base;
  144. if (base == NULL) {
  145. *lenbase = lennew;
  146. return new;
  147. }
  148. /*
  149. * Coalesce new lines into base by finding the LCS
  150. * - Create the table to run dynamic programming
  151. * - Compute the LCS
  152. * - Then reverse read the direction structure:
  153. * - If we have MATCH, assign parent to base flag, and consume
  154. * both baseend and newend
  155. * - Else if we have BASE, consume baseend
  156. * - Else if we have NEW, insert newend lline into base and
  157. * consume newend
  158. */
  159. lcs = xcalloc(origbaselen + 1, sizeof(int*));
  160. directions = xcalloc(origbaselen + 1, sizeof(enum coalesce_direction*));
  161. for (i = 0; i < origbaselen + 1; i++) {
  162. lcs[i] = xcalloc(lennew + 1, sizeof(int));
  163. directions[i] = xcalloc(lennew + 1, sizeof(enum coalesce_direction));
  164. directions[i][0] = BASE;
  165. }
  166. for (j = 1; j < lennew + 1; j++)
  167. directions[0][j] = NEW;
  168. for (i = 1, baseend = base; i < origbaselen + 1; i++) {
  169. for (j = 1, newend = new; j < lennew + 1; j++) {
  170. if (match_string_spaces(baseend->line, baseend->len,
  171. newend->line, newend->len, flags)) {
  172. lcs[i][j] = lcs[i - 1][j - 1] + 1;
  173. directions[i][j] = MATCH;
  174. } else if (lcs[i][j - 1] >= lcs[i - 1][j]) {
  175. lcs[i][j] = lcs[i][j - 1];
  176. directions[i][j] = NEW;
  177. } else {
  178. lcs[i][j] = lcs[i - 1][j];
  179. directions[i][j] = BASE;
  180. }
  181. if (newend->next)
  182. newend = newend->next;
  183. }
  184. if (baseend->next)
  185. baseend = baseend->next;
  186. }
  187. for (i = 0; i < origbaselen + 1; i++)
  188. free(lcs[i]);
  189. free(lcs);
  190. /* At this point, baseend and newend point to the end of each lists */
  191. i--;
  192. j--;
  193. while (i != 0 || j != 0) {
  194. if (directions[i][j] == MATCH) {
  195. baseend->parent_map |= 1<<parent;
  196. baseend = baseend->prev;
  197. newend = newend->prev;
  198. i--;
  199. j--;
  200. } else if (directions[i][j] == NEW) {
  201. struct lline *lline;
  202. lline = newend;
  203. /* Remove lline from new list and update newend */
  204. if (lline->prev)
  205. lline->prev->next = lline->next;
  206. else
  207. new = lline->next;
  208. if (lline->next)
  209. lline->next->prev = lline->prev;
  210. newend = lline->prev;
  211. j--;
  212. /* Add lline to base list */
  213. if (baseend) {
  214. lline->next = baseend->next;
  215. lline->prev = baseend;
  216. if (lline->prev)
  217. lline->prev->next = lline;
  218. }
  219. else {
  220. lline->next = base;
  221. base = lline;
  222. }
  223. (*lenbase)++;
  224. if (lline->next)
  225. lline->next->prev = lline;
  226. } else {
  227. baseend = baseend->prev;
  228. i--;
  229. }
  230. }
  231. newend = new;
  232. while (newend) {
  233. struct lline *lline = newend;
  234. newend = newend->next;
  235. free(lline);
  236. }
  237. for (i = 0; i < origbaselen + 1; i++)
  238. free(directions[i]);
  239. free(directions);
  240. return base;
  241. }
  242. static char *grab_blob(const unsigned char *sha1, unsigned int mode,
  243. unsigned long *size, struct userdiff_driver *textconv,
  244. const char *path)
  245. {
  246. char *blob;
  247. enum object_type type;
  248. if (S_ISGITLINK(mode)) {
  249. blob = xmalloc(100);
  250. *size = snprintf(blob, 100,
  251. "Subproject commit %s\n", sha1_to_hex(sha1));
  252. } else if (is_null_sha1(sha1)) {
  253. /* deleted blob */
  254. *size = 0;
  255. return xcalloc(1, 1);
  256. } else if (textconv) {
  257. struct diff_filespec *df = alloc_filespec(path);
  258. fill_filespec(df, sha1, 1, mode);
  259. *size = fill_textconv(textconv, df, &blob);
  260. free_filespec(df);
  261. } else {
  262. blob = read_sha1_file(sha1, &type, size);
  263. if (type != OBJ_BLOB)
  264. die("object '%s' is not a blob!", sha1_to_hex(sha1));
  265. }
  266. return blob;
  267. }
  268. static void append_lost(struct sline *sline, int n, const char *line, int len)
  269. {
  270. struct lline *lline;
  271. unsigned long this_mask = (1UL<<n);
  272. if (line[len-1] == '\n')
  273. len--;
  274. lline = xmalloc(sizeof(*lline) + len + 1);
  275. lline->len = len;
  276. lline->next = NULL;
  277. lline->prev = sline->plost.lost_tail;
  278. if (lline->prev)
  279. lline->prev->next = lline;
  280. else
  281. sline->plost.lost_head = lline;
  282. sline->plost.lost_tail = lline;
  283. sline->plost.len++;
  284. lline->parent_map = this_mask;
  285. memcpy(lline->line, line, len);
  286. lline->line[len] = 0;
  287. }
  288. struct combine_diff_state {
  289. unsigned int lno;
  290. int ob, on, nb, nn;
  291. unsigned long nmask;
  292. int num_parent;
  293. int n;
  294. struct sline *sline;
  295. struct sline *lost_bucket;
  296. };
  297. static void consume_line(void *state_, char *line, unsigned long len)
  298. {
  299. struct combine_diff_state *state = state_;
  300. if (5 < len && !memcmp("@@ -", line, 4)) {
  301. if (parse_hunk_header(line, len,
  302. &state->ob, &state->on,
  303. &state->nb, &state->nn))
  304. return;
  305. state->lno = state->nb;
  306. if (state->nn == 0) {
  307. /* @@ -X,Y +N,0 @@ removed Y lines
  308. * that would have come *after* line N
  309. * in the result. Our lost buckets hang
  310. * to the line after the removed lines,
  311. *
  312. * Note that this is correct even when N == 0,
  313. * in which case the hunk removes the first
  314. * line in the file.
  315. */
  316. state->lost_bucket = &state->sline[state->nb];
  317. if (!state->nb)
  318. state->nb = 1;
  319. } else {
  320. state->lost_bucket = &state->sline[state->nb-1];
  321. }
  322. if (!state->sline[state->nb-1].p_lno)
  323. state->sline[state->nb-1].p_lno =
  324. xcalloc(state->num_parent,
  325. sizeof(unsigned long));
  326. state->sline[state->nb-1].p_lno[state->n] = state->ob;
  327. return;
  328. }
  329. if (!state->lost_bucket)
  330. return; /* not in any hunk yet */
  331. switch (line[0]) {
  332. case '-':
  333. append_lost(state->lost_bucket, state->n, line+1, len-1);
  334. break;
  335. case '+':
  336. state->sline[state->lno-1].flag |= state->nmask;
  337. state->lno++;
  338. break;
  339. }
  340. }
  341. static void combine_diff(const unsigned char *parent, unsigned int mode,
  342. mmfile_t *result_file,
  343. struct sline *sline, unsigned int cnt, int n,
  344. int num_parent, int result_deleted,
  345. struct userdiff_driver *textconv,
  346. const char *path, long flags)
  347. {
  348. unsigned int p_lno, lno;
  349. unsigned long nmask = (1UL << n);
  350. xpparam_t xpp;
  351. xdemitconf_t xecfg;
  352. mmfile_t parent_file;
  353. struct combine_diff_state state;
  354. unsigned long sz;
  355. if (result_deleted)
  356. return; /* result deleted */
  357. parent_file.ptr = grab_blob(parent, mode, &sz, textconv, path);
  358. parent_file.size = sz;
  359. memset(&xpp, 0, sizeof(xpp));
  360. xpp.flags = flags;
  361. memset(&xecfg, 0, sizeof(xecfg));
  362. memset(&state, 0, sizeof(state));
  363. state.nmask = nmask;
  364. state.sline = sline;
  365. state.lno = 1;
  366. state.num_parent = num_parent;
  367. state.n = n;
  368. xdi_diff_outf(&parent_file, result_file, consume_line, &state,
  369. &xpp, &xecfg);
  370. free(parent_file.ptr);
  371. /* Assign line numbers for this parent.
  372. *
  373. * sline[lno].p_lno[n] records the first line number
  374. * (counting from 1) for parent N if the final hunk display
  375. * started by showing sline[lno] (possibly showing the lost
  376. * lines attached to it first).
  377. */
  378. for (lno = 0, p_lno = 1; lno <= cnt; lno++) {
  379. struct lline *ll;
  380. sline[lno].p_lno[n] = p_lno;
  381. /* Coalesce new lines */
  382. if (sline[lno].plost.lost_head) {
  383. struct sline *sl = &sline[lno];
  384. sl->lost = coalesce_lines(sl->lost, &sl->lenlost,
  385. sl->plost.lost_head,
  386. sl->plost.len, n, flags);
  387. sl->plost.lost_head = sl->plost.lost_tail = NULL;
  388. sl->plost.len = 0;
  389. }
  390. /* How many lines would this sline advance the p_lno? */
  391. ll = sline[lno].lost;
  392. while (ll) {
  393. if (ll->parent_map & nmask)
  394. p_lno++; /* '-' means parent had it */
  395. ll = ll->next;
  396. }
  397. if (lno < cnt && !(sline[lno].flag & nmask))
  398. p_lno++; /* no '+' means parent had it */
  399. }
  400. sline[lno].p_lno[n] = p_lno; /* trailer */
  401. }
  402. static unsigned long context = 3;
  403. static char combine_marker = '@';
  404. static int interesting(struct sline *sline, unsigned long all_mask)
  405. {
  406. /* If some parents lost lines here, or if we have added to
  407. * some parent, it is interesting.
  408. */
  409. return ((sline->flag & all_mask) || sline->lost);
  410. }
  411. static unsigned long adjust_hunk_tail(struct sline *sline,
  412. unsigned long all_mask,
  413. unsigned long hunk_begin,
  414. unsigned long i)
  415. {
  416. /* i points at the first uninteresting line. If the last line
  417. * of the hunk was interesting only because it has some
  418. * deletion, then it is not all that interesting for the
  419. * purpose of giving trailing context lines. This is because
  420. * we output '-' line and then unmodified sline[i-1] itself in
  421. * that case which gives us one extra context line.
  422. */
  423. if ((hunk_begin + 1 <= i) && !(sline[i-1].flag & all_mask))
  424. i--;
  425. return i;
  426. }
  427. static unsigned long find_next(struct sline *sline,
  428. unsigned long mark,
  429. unsigned long i,
  430. unsigned long cnt,
  431. int look_for_uninteresting)
  432. {
  433. /* We have examined up to i-1 and are about to look at i.
  434. * Find next interesting or uninteresting line. Here,
  435. * "interesting" does not mean interesting(), but marked by
  436. * the give_context() function below (i.e. it includes context
  437. * lines that are not interesting to interesting() function
  438. * that are surrounded by interesting() ones.
  439. */
  440. while (i <= cnt)
  441. if (look_for_uninteresting
  442. ? !(sline[i].flag & mark)
  443. : (sline[i].flag & mark))
  444. return i;
  445. else
  446. i++;
  447. return i;
  448. }
  449. static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
  450. {
  451. unsigned long all_mask = (1UL<<num_parent) - 1;
  452. unsigned long mark = (1UL<<num_parent);
  453. unsigned long no_pre_delete = (2UL<<num_parent);
  454. unsigned long i;
  455. /* Two groups of interesting lines may have a short gap of
  456. * uninteresting lines. Connect such groups to give them a
  457. * bit of context.
  458. *
  459. * We first start from what the interesting() function says,
  460. * and mark them with "mark", and paint context lines with the
  461. * mark. So interesting() would still say false for such context
  462. * lines but they are treated as "interesting" in the end.
  463. */
  464. i = find_next(sline, mark, 0, cnt, 0);
  465. if (cnt < i)
  466. return 0;
  467. while (i <= cnt) {
  468. unsigned long j = (context < i) ? (i - context) : 0;
  469. unsigned long k;
  470. /* Paint a few lines before the first interesting line. */
  471. while (j < i) {
  472. if (!(sline[j].flag & mark))
  473. sline[j].flag |= no_pre_delete;
  474. sline[j++].flag |= mark;
  475. }
  476. again:
  477. /* we know up to i is to be included. where does the
  478. * next uninteresting one start?
  479. */
  480. j = find_next(sline, mark, i, cnt, 1);
  481. if (cnt < j)
  482. break; /* the rest are all interesting */
  483. /* lookahead context lines */
  484. k = find_next(sline, mark, j, cnt, 0);
  485. j = adjust_hunk_tail(sline, all_mask, i, j);
  486. if (k < j + context) {
  487. /* k is interesting and [j,k) are not, but
  488. * paint them interesting because the gap is small.
  489. */
  490. while (j < k)
  491. sline[j++].flag |= mark;
  492. i = k;
  493. goto again;
  494. }
  495. /* j is the first uninteresting line and there is
  496. * no overlap beyond it within context lines. Paint
  497. * the trailing edge a bit.
  498. */
  499. i = k;
  500. k = (j + context < cnt+1) ? j + context : cnt+1;
  501. while (j < k)
  502. sline[j++].flag |= mark;
  503. }
  504. return 1;
  505. }
  506. static int make_hunks(struct sline *sline, unsigned long cnt,
  507. int num_parent, int dense)
  508. {
  509. unsigned long all_mask = (1UL<<num_parent) - 1;
  510. unsigned long mark = (1UL<<num_parent);
  511. unsigned long i;
  512. int has_interesting = 0;
  513. for (i = 0; i <= cnt; i++) {
  514. if (interesting(&sline[i], all_mask))
  515. sline[i].flag |= mark;
  516. else
  517. sline[i].flag &= ~mark;
  518. }
  519. if (!dense)
  520. return give_context(sline, cnt, num_parent);
  521. /* Look at each hunk, and if we have changes from only one
  522. * parent, or the changes are the same from all but one
  523. * parent, mark that uninteresting.
  524. */
  525. i = 0;
  526. while (i <= cnt) {
  527. unsigned long j, hunk_begin, hunk_end;
  528. unsigned long same_diff;
  529. while (i <= cnt && !(sline[i].flag & mark))
  530. i++;
  531. if (cnt < i)
  532. break; /* No more interesting hunks */
  533. hunk_begin = i;
  534. for (j = i + 1; j <= cnt; j++) {
  535. if (!(sline[j].flag & mark)) {
  536. /* Look beyond the end to see if there
  537. * is an interesting line after this
  538. * hunk within context span.
  539. */
  540. unsigned long la; /* lookahead */
  541. int contin = 0;
  542. la = adjust_hunk_tail(sline, all_mask,
  543. hunk_begin, j);
  544. la = (la + context < cnt + 1) ?
  545. (la + context) : cnt + 1;
  546. while (la && j <= --la) {
  547. if (sline[la].flag & mark) {
  548. contin = 1;
  549. break;
  550. }
  551. }
  552. if (!contin)
  553. break;
  554. j = la;
  555. }
  556. }
  557. hunk_end = j;
  558. /* [i..hunk_end) are interesting. Now is it really
  559. * interesting? We check if there are only two versions
  560. * and the result matches one of them. That is, we look
  561. * at:
  562. * (+) line, which records lines added to which parents;
  563. * this line appears in the result.
  564. * (-) line, which records from what parents the line
  565. * was removed; this line does not appear in the result.
  566. * then check the set of parents the result has difference
  567. * from, from all lines. If there are lines that has
  568. * different set of parents that the result has differences
  569. * from, that means we have more than two versions.
  570. *
  571. * Even when we have only two versions, if the result does
  572. * not match any of the parents, the it should be considered
  573. * interesting. In such a case, we would have all '+' line.
  574. * After passing the above "two versions" test, that would
  575. * appear as "the same set of parents" to be "all parents".
  576. */
  577. same_diff = 0;
  578. has_interesting = 0;
  579. for (j = i; j < hunk_end && !has_interesting; j++) {
  580. unsigned long this_diff = sline[j].flag & all_mask;
  581. struct lline *ll = sline[j].lost;
  582. if (this_diff) {
  583. /* This has some changes. Is it the
  584. * same as others?
  585. */
  586. if (!same_diff)
  587. same_diff = this_diff;
  588. else if (same_diff != this_diff) {
  589. has_interesting = 1;
  590. break;
  591. }
  592. }
  593. while (ll && !has_interesting) {
  594. /* Lost this line from these parents;
  595. * who are they? Are they the same?
  596. */
  597. this_diff = ll->parent_map;
  598. if (!same_diff)
  599. same_diff = this_diff;
  600. else if (same_diff != this_diff) {
  601. has_interesting = 1;
  602. }
  603. ll = ll->next;
  604. }
  605. }
  606. if (!has_interesting && same_diff != all_mask) {
  607. /* This hunk is not that interesting after all */
  608. for (j = hunk_begin; j < hunk_end; j++)
  609. sline[j].flag &= ~mark;
  610. }
  611. i = hunk_end;
  612. }
  613. has_interesting = give_context(sline, cnt, num_parent);
  614. return has_interesting;
  615. }
  616. static void show_parent_lno(struct sline *sline, unsigned long l0, unsigned long l1, int n, unsigned long null_context)
  617. {
  618. l0 = sline[l0].p_lno[n];
  619. l1 = sline[l1].p_lno[n];
  620. printf(" -%lu,%lu", l0, l1-l0-null_context);
  621. }
  622. static int hunk_comment_line(const char *bol)
  623. {
  624. int ch;
  625. if (!bol)
  626. return 0;
  627. ch = *bol & 0xff;
  628. return (isalpha(ch) || ch == '_' || ch == '$');
  629. }
  630. static void show_line_to_eol(const char *line, int len, const char *reset)
  631. {
  632. int saw_cr_at_eol = 0;
  633. if (len < 0)
  634. len = strlen(line);
  635. saw_cr_at_eol = (len && line[len-1] == '\r');
  636. printf("%.*s%s%s\n", len - saw_cr_at_eol, line,
  637. reset,
  638. saw_cr_at_eol ? "\r" : "");
  639. }
  640. static void dump_sline(struct sline *sline, const char *line_prefix,
  641. unsigned long cnt, int num_parent,
  642. int use_color, int result_deleted)
  643. {
  644. unsigned long mark = (1UL<<num_parent);
  645. unsigned long no_pre_delete = (2UL<<num_parent);
  646. int i;
  647. unsigned long lno = 0;
  648. const char *c_frag = diff_get_color(use_color, DIFF_FRAGINFO);
  649. const char *c_func = diff_get_color(use_color, DIFF_FUNCINFO);
  650. const char *c_new = diff_get_color(use_color, DIFF_FILE_NEW);
  651. const char *c_old = diff_get_color(use_color, DIFF_FILE_OLD);
  652. const char *c_plain = diff_get_color(use_color, DIFF_PLAIN);
  653. const char *c_reset = diff_get_color(use_color, DIFF_RESET);
  654. if (result_deleted)
  655. return; /* result deleted */
  656. while (1) {
  657. unsigned long hunk_end;
  658. unsigned long rlines;
  659. const char *hunk_comment = NULL;
  660. unsigned long null_context = 0;
  661. while (lno <= cnt && !(sline[lno].flag & mark)) {
  662. if (hunk_comment_line(sline[lno].bol))
  663. hunk_comment = sline[lno].bol;
  664. lno++;
  665. }
  666. if (cnt < lno)
  667. break;
  668. else {
  669. for (hunk_end = lno + 1; hunk_end <= cnt; hunk_end++)
  670. if (!(sline[hunk_end].flag & mark))
  671. break;
  672. }
  673. rlines = hunk_end - lno;
  674. if (cnt < hunk_end)
  675. rlines--; /* pointing at the last delete hunk */
  676. if (!context) {
  677. /*
  678. * Even when running with --unified=0, all
  679. * lines in the hunk needs to be processed in
  680. * the loop below in order to show the
  681. * deletion recorded in lost_head. However,
  682. * we do not want to show the resulting line
  683. * with all blank context markers in such a
  684. * case. Compensate.
  685. */
  686. unsigned long j;
  687. for (j = lno; j < hunk_end; j++)
  688. if (!(sline[j].flag & (mark-1)))
  689. null_context++;
  690. rlines -= null_context;
  691. }
  692. printf("%s%s", line_prefix, c_frag);
  693. for (i = 0; i <= num_parent; i++) putchar(combine_marker);
  694. for (i = 0; i < num_parent; i++)
  695. show_parent_lno(sline, lno, hunk_end, i, null_context);
  696. printf(" +%lu,%lu ", lno+1, rlines);
  697. for (i = 0; i <= num_parent; i++) putchar(combine_marker);
  698. if (hunk_comment) {
  699. int comment_end = 0;
  700. for (i = 0; i < 40; i++) {
  701. int ch = hunk_comment[i] & 0xff;
  702. if (!ch || ch == '\n')
  703. break;
  704. if (!isspace(ch))
  705. comment_end = i;
  706. }
  707. if (comment_end)
  708. printf("%s%s %s%s", c_reset,
  709. c_plain, c_reset,
  710. c_func);
  711. for (i = 0; i < comment_end; i++)
  712. putchar(hunk_comment[i]);
  713. }
  714. printf("%s\n", c_reset);
  715. while (lno < hunk_end) {
  716. struct lline *ll;
  717. int j;
  718. unsigned long p_mask;
  719. struct sline *sl = &sline[lno++];
  720. ll = (sl->flag & no_pre_delete) ? NULL : sl->lost;
  721. while (ll) {
  722. printf("%s%s", line_prefix, c_old);
  723. for (j = 0; j < num_parent; j++) {
  724. if (ll->parent_map & (1UL<<j))
  725. putchar('-');
  726. else
  727. putchar(' ');
  728. }
  729. show_line_to_eol(ll->line, -1, c_reset);
  730. ll = ll->next;
  731. }
  732. if (cnt < lno)
  733. break;
  734. p_mask = 1;
  735. fputs(line_prefix, stdout);
  736. if (!(sl->flag & (mark-1))) {
  737. /*
  738. * This sline was here to hang the
  739. * lost lines in front of it.
  740. */
  741. if (!context)
  742. continue;
  743. fputs(c_plain, stdout);
  744. }
  745. else
  746. fputs(c_new, stdout);
  747. for (j = 0; j < num_parent; j++) {
  748. if (p_mask & sl->flag)
  749. putchar('+');
  750. else
  751. putchar(' ');
  752. p_mask <<= 1;
  753. }
  754. show_line_to_eol(sl->bol, sl->len, c_reset);
  755. }
  756. }
  757. }
  758. static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
  759. int i, int j)
  760. {
  761. /* We have already examined parent j and we know parent i
  762. * and parent j are the same, so reuse the combined result
  763. * of parent j for parent i.
  764. */
  765. unsigned long lno, imask, jmask;
  766. imask = (1UL<<i);
  767. jmask = (1UL<<j);
  768. for (lno = 0; lno <= cnt; lno++) {
  769. struct lline *ll = sline->lost;
  770. sline->p_lno[i] = sline->p_lno[j];
  771. while (ll) {
  772. if (ll->parent_map & jmask)
  773. ll->parent_map |= imask;
  774. ll = ll->next;
  775. }
  776. if (sline->flag & jmask)
  777. sline->flag |= imask;
  778. sline++;
  779. }
  780. /* the overall size of the file (sline[cnt]) */
  781. sline->p_lno[i] = sline->p_lno[j];
  782. }
  783. static void dump_quoted_path(const char *head,
  784. const char *prefix,
  785. const char *path,
  786. const char *line_prefix,
  787. const char *c_meta, const char *c_reset)
  788. {
  789. static struct strbuf buf = STRBUF_INIT;
  790. strbuf_reset(&buf);
  791. strbuf_addstr(&buf, line_prefix);
  792. strbuf_addstr(&buf, c_meta);
  793. strbuf_addstr(&buf, head);
  794. quote_two_c_style(&buf, prefix, path, 0);
  795. strbuf_addstr(&buf, c_reset);
  796. puts(buf.buf);
  797. }
  798. static void show_combined_header(struct combine_diff_path *elem,
  799. int num_parent,
  800. int dense,
  801. struct rev_info *rev,
  802. const char *line_prefix,
  803. int mode_differs,
  804. int show_file_header)
  805. {
  806. struct diff_options *opt = &rev->diffopt;
  807. int abbrev = DIFF_OPT_TST(opt, FULL_INDEX) ? 40 : DEFAULT_ABBREV;
  808. const char *a_prefix = opt->a_prefix ? opt->a_prefix : "a/";
  809. const char *b_prefix = opt->b_prefix ? opt->b_prefix : "b/";
  810. const char *c_meta = diff_get_color_opt(opt, DIFF_METAINFO);
  811. const char *c_reset = diff_get_color_opt(opt, DIFF_RESET);
  812. const char *abb;
  813. int added = 0;
  814. int deleted = 0;
  815. int i;
  816. if (rev->loginfo && !rev->no_commit_id)
  817. show_log(rev);
  818. dump_quoted_path(dense ? "diff --cc " : "diff --combined ",
  819. "", elem->path, line_prefix, c_meta, c_reset);
  820. printf("%s%sindex ", line_prefix, c_meta);
  821. for (i = 0; i < num_parent; i++) {
  822. abb = find_unique_abbrev(elem->parent[i].sha1,
  823. abbrev);
  824. printf("%s%s", i ? "," : "", abb);
  825. }
  826. abb = find_unique_abbrev(elem->sha1, abbrev);
  827. printf("..%s%s\n", abb, c_reset);
  828. if (mode_differs) {
  829. deleted = !elem->mode;
  830. /* We say it was added if nobody had it */
  831. added = !deleted;
  832. for (i = 0; added && i < num_parent; i++)
  833. if (elem->parent[i].status !=
  834. DIFF_STATUS_ADDED)
  835. added = 0;
  836. if (added)
  837. printf("%s%snew file mode %06o",
  838. line_prefix, c_meta, elem->mode);
  839. else {
  840. if (deleted)
  841. printf("%s%sdeleted file ",
  842. line_prefix, c_meta);
  843. printf("mode ");
  844. for (i = 0; i < num_parent; i++) {
  845. printf("%s%06o", i ? "," : "",
  846. elem->parent[i].mode);
  847. }
  848. if (elem->mode)
  849. printf("..%06o", elem->mode);
  850. }
  851. printf("%s\n", c_reset);
  852. }
  853. if (!show_file_header)
  854. return;
  855. if (added)
  856. dump_quoted_path("--- ", "", "/dev/null",
  857. line_prefix, c_meta, c_reset);
  858. else
  859. dump_quoted_path("--- ", a_prefix, elem->path,
  860. line_prefix, c_meta, c_reset);
  861. if (deleted)
  862. dump_quoted_path("+++ ", "", "/dev/null",
  863. line_prefix, c_meta, c_reset);
  864. else
  865. dump_quoted_path("+++ ", b_prefix, elem->path,
  866. line_prefix, c_meta, c_reset);
  867. }
  868. static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
  869. int dense, int working_tree_file,
  870. struct rev_info *rev)
  871. {
  872. struct diff_options *opt = &rev->diffopt;
  873. unsigned long result_size, cnt, lno;
  874. int result_deleted = 0;
  875. char *result, *cp;
  876. struct sline *sline; /* survived lines */
  877. int mode_differs = 0;
  878. int i, show_hunks;
  879. mmfile_t result_file;
  880. struct userdiff_driver *userdiff;
  881. struct userdiff_driver *textconv = NULL;
  882. int is_binary;
  883. const char *line_prefix = diff_line_prefix(opt);
  884. context = opt->context;
  885. userdiff = userdiff_find_by_path(elem->path);
  886. if (!userdiff)
  887. userdiff = userdiff_find_by_name("default");
  888. if (DIFF_OPT_TST(opt, ALLOW_TEXTCONV))
  889. textconv = userdiff_get_textconv(userdiff);
  890. /* Read the result of merge first */
  891. if (!working_tree_file)
  892. result = grab_blob(elem->sha1, elem->mode, &result_size,
  893. textconv, elem->path);
  894. else {
  895. /* Used by diff-tree to read from the working tree */
  896. struct stat st;
  897. int fd = -1;
  898. if (lstat(elem->path, &st) < 0)
  899. goto deleted_file;
  900. if (S_ISLNK(st.st_mode)) {
  901. struct strbuf buf = STRBUF_INIT;
  902. if (strbuf_readlink(&buf, elem->path, st.st_size) < 0) {
  903. error("readlink(%s): %s", elem->path,
  904. strerror(errno));
  905. return;
  906. }
  907. result_size = buf.len;
  908. result = strbuf_detach(&buf, NULL);
  909. elem->mode = canon_mode(st.st_mode);
  910. } else if (S_ISDIR(st.st_mode)) {
  911. unsigned char sha1[20];
  912. if (resolve_gitlink_ref(elem->path, "HEAD", sha1) < 0)
  913. result = grab_blob(elem->sha1, elem->mode,
  914. &result_size, NULL, NULL);
  915. else
  916. result = grab_blob(sha1, elem->mode,
  917. &result_size, NULL, NULL);
  918. } else if (textconv) {
  919. struct diff_filespec *df = alloc_filespec(elem->path);
  920. fill_filespec(df, null_sha1, 0, st.st_mode);
  921. result_size = fill_textconv(textconv, df, &result);
  922. free_filespec(df);
  923. } else if (0 <= (fd = open(elem->path, O_RDONLY))) {
  924. size_t len = xsize_t(st.st_size);
  925. ssize_t done;
  926. int is_file, i;
  927. elem->mode = canon_mode(st.st_mode);
  928. /* if symlinks don't work, assume symlink if all parents
  929. * are symlinks
  930. */
  931. is_file = has_symlinks;
  932. for (i = 0; !is_file && i < num_parent; i++)
  933. is_file = !S_ISLNK(elem->parent[i].mode);
  934. if (!is_file)
  935. elem->mode = canon_mode(S_IFLNK);
  936. result_size = len;
  937. result = xmalloc(len + 1);
  938. done = read_in_full(fd, result, len);
  939. if (done < 0)
  940. die_errno("read error '%s'", elem->path);
  941. else if (done < len)
  942. die("early EOF '%s'", elem->path);
  943. result[len] = 0;
  944. /* If not a fake symlink, apply filters, e.g. autocrlf */
  945. if (is_file) {
  946. struct strbuf buf = STRBUF_INIT;
  947. if (convert_to_git(elem->path, result, len, &buf, safe_crlf)) {
  948. free(result);
  949. result = strbuf_detach(&buf, &len);
  950. result_size = len;
  951. }
  952. }
  953. }
  954. else {
  955. deleted_file:
  956. result_deleted = 1;
  957. result_size = 0;
  958. elem->mode = 0;
  959. result = xcalloc(1, 1);
  960. }
  961. if (0 <= fd)
  962. close(fd);
  963. }
  964. for (i = 0; i < num_parent; i++) {
  965. if (elem->parent[i].mode != elem->mode) {
  966. mode_differs = 1;
  967. break;
  968. }
  969. }
  970. if (textconv)
  971. is_binary = 0;
  972. else if (userdiff->binary != -1)
  973. is_binary = userdiff->binary;
  974. else {
  975. is_binary = buffer_is_binary(result, result_size);
  976. for (i = 0; !is_binary && i < num_parent; i++) {
  977. char *buf;
  978. unsigned long size;
  979. buf = grab_blob(elem->parent[i].sha1,
  980. elem->parent[i].mode,
  981. &size, NULL, NULL);
  982. if (buffer_is_binary(buf, size))
  983. is_binary = 1;
  984. free(buf);
  985. }
  986. }
  987. if (is_binary) {
  988. show_combined_header(elem, num_parent, dense, rev,
  989. line_prefix, mode_differs, 0);
  990. printf("Binary files differ\n");
  991. free(result);
  992. return;
  993. }
  994. for (cnt = 0, cp = result; cp < result + result_size; cp++) {
  995. if (*cp == '\n')
  996. cnt++;
  997. }
  998. if (result_size && result[result_size-1] != '\n')
  999. cnt++; /* incomplete line */
  1000. sline = xcalloc(cnt+2, sizeof(*sline));
  1001. sline[0].bol = result;
  1002. for (lno = 0, cp = result; cp < result + result_size; cp++) {
  1003. if (*cp == '\n') {
  1004. sline[lno].len = cp - sline[lno].bol;
  1005. lno++;
  1006. if (lno < cnt)
  1007. sline[lno].bol = cp + 1;
  1008. }
  1009. }
  1010. if (result_size && result[result_size-1] != '\n')
  1011. sline[cnt-1].len = result_size - (sline[cnt-1].bol - result);
  1012. result_file.ptr = result;
  1013. result_file.size = result_size;
  1014. /* Even p_lno[cnt+1] is valid -- that is for the end line number
  1015. * for deletion hunk at the end.
  1016. */
  1017. sline[0].p_lno = xcalloc((cnt+2) * num_parent, sizeof(unsigned long));
  1018. for (lno = 0; lno <= cnt; lno++)
  1019. sline[lno+1].p_lno = sline[lno].p_lno + num_parent;
  1020. for (i = 0; i < num_parent; i++) {
  1021. int j;
  1022. for (j = 0; j < i; j++) {
  1023. if (!hashcmp(elem->parent[i].sha1,
  1024. elem->parent[j].sha1)) {
  1025. reuse_combine_diff(sline, cnt, i, j);
  1026. break;
  1027. }
  1028. }
  1029. if (i <= j)
  1030. combine_diff(elem->parent[i].sha1,
  1031. elem->parent[i].mode,
  1032. &result_file, sline,
  1033. cnt, i, num_parent, result_deleted,
  1034. textconv, elem->path, opt->xdl_opts);
  1035. }
  1036. show_hunks = make_hunks(sline, cnt, num_parent, dense);
  1037. if (show_hunks || mode_differs || working_tree_file) {
  1038. show_combined_header(elem, num_parent, dense, rev,
  1039. line_prefix, mode_differs, 1);
  1040. dump_sline(sline, line_prefix, cnt, num_parent,
  1041. opt->use_color, result_deleted);
  1042. }
  1043. free(result);
  1044. for (lno = 0; lno < cnt; lno++) {
  1045. if (sline[lno].lost) {
  1046. struct lline *ll = sline[lno].lost;
  1047. while (ll) {
  1048. struct lline *tmp = ll;
  1049. ll = ll->next;
  1050. free(tmp);
  1051. }
  1052. }
  1053. }
  1054. free(sline[0].p_lno);
  1055. free(sline);
  1056. }
  1057. static void show_raw_diff(struct combine_diff_path *p, int num_parent, struct rev_info *rev)
  1058. {
  1059. struct diff_options *opt = &rev->diffopt;
  1060. int line_termination, inter_name_termination, i;
  1061. const char *line_prefix = diff_line_prefix(opt);
  1062. line_termination = opt->line_termination;
  1063. inter_name_termination = '\t';
  1064. if (!line_termination)
  1065. inter_name_termination = 0;
  1066. if (rev->loginfo && !rev->no_commit_id)
  1067. show_log(rev);
  1068. if (opt->output_format & DIFF_FORMAT_RAW) {
  1069. printf("%s", line_prefix);
  1070. /* As many colons as there are parents */
  1071. for (i = 0; i < num_parent; i++)
  1072. putchar(':');
  1073. /* Show the modes */
  1074. for (i = 0; i < num_parent; i++)
  1075. printf("%06o ", p->parent[i].mode);
  1076. printf("%06o", p->mode);
  1077. /* Show sha1's */
  1078. for (i = 0; i < num_parent; i++)
  1079. printf(" %s", diff_unique_abbrev(p->parent[i].sha1,
  1080. opt->abbrev));
  1081. printf(" %s ", diff_unique_abbrev(p->sha1, opt->abbrev));
  1082. }
  1083. if (opt->output_format & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS)) {
  1084. for (i = 0; i < num_parent; i++)
  1085. putchar(p->parent[i].status);
  1086. putchar(inter_name_termination);
  1087. }
  1088. write_name_quoted(p->path, stdout, line_termination);
  1089. }
  1090. /*
  1091. * The result (p->elem) is from the working tree and their
  1092. * parents are typically from multiple stages during a merge
  1093. * (i.e. diff-files) or the state in HEAD and in the index
  1094. * (i.e. diff-index).
  1095. */
  1096. void show_combined_diff(struct combine_diff_path *p,
  1097. int num_parent,
  1098. int dense,
  1099. struct rev_info *rev)
  1100. {
  1101. struct diff_options *opt = &rev->diffopt;
  1102. if (opt->output_format & (DIFF_FORMAT_RAW |
  1103. DIFF_FORMAT_NAME |
  1104. DIFF_FORMAT_NAME_STATUS))
  1105. show_raw_diff(p, num_parent, rev);
  1106. else if (opt->output_format & DIFF_FORMAT_PATCH)
  1107. show_patch_diff(p, num_parent, dense, 1, rev);
  1108. }
  1109. static void free_combined_pair(struct diff_filepair *pair)
  1110. {
  1111. free(pair->two);
  1112. free(pair);
  1113. }
  1114. /*
  1115. * A combine_diff_path expresses N parents on the LHS against 1 merge
  1116. * result. Synthesize a diff_filepair that has N entries on the "one"
  1117. * side and 1 entry on the "two" side.
  1118. *
  1119. * In the future, we might want to add more data to combine_diff_path
  1120. * so that we can fill fields we are ignoring (most notably, size) here,
  1121. * but currently nobody uses it, so this should suffice for now.
  1122. */
  1123. static struct diff_filepair *combined_pair(struct combine_diff_path *p,
  1124. int num_parent)
  1125. {
  1126. int i;
  1127. struct diff_filepair *pair;
  1128. struct diff_filespec *pool;
  1129. pair = xmalloc(sizeof(*pair));
  1130. pool = xcalloc(num_parent + 1, sizeof(struct diff_filespec));
  1131. pair->one = pool + 1;
  1132. pair->two = pool;
  1133. for (i = 0; i < num_parent; i++) {
  1134. pair->one[i].path = p->path;
  1135. pair->one[i].mode = p->parent[i].mode;
  1136. hashcpy(pair->one[i].sha1, p->parent[i].sha1);
  1137. pair->one[i].sha1_valid = !is_null_sha1(p->parent[i].sha1);
  1138. pair->one[i].has_more_entries = 1;
  1139. }
  1140. pair->one[num_parent - 1].has_more_entries = 0;
  1141. pair->two->path = p->path;
  1142. pair->two->mode = p->mode;
  1143. hashcpy(pair->two->sha1, p->sha1);
  1144. pair->two->sha1_valid = !is_null_sha1(p->sha1);
  1145. return pair;
  1146. }
  1147. static void handle_combined_callback(struct diff_options *opt,
  1148. struct combine_diff_path *paths,
  1149. int num_parent,
  1150. int num_paths)
  1151. {
  1152. struct combine_diff_path *p;
  1153. struct diff_queue_struct q;
  1154. int i;
  1155. q.queue = xcalloc(num_paths, sizeof(struct diff_filepair *));
  1156. q.alloc = num_paths;
  1157. q.nr = num_paths;
  1158. for (i = 0, p = paths; p; p = p->next)
  1159. q.queue[i++] = combined_pair(p, num_parent);
  1160. opt->format_callback(&q, opt, opt->format_callback_data);
  1161. for (i = 0; i < num_paths; i++)
  1162. free_combined_pair(q.queue[i]);
  1163. free(q.queue);
  1164. }
  1165. static const char *path_path(void *obj)
  1166. {
  1167. struct combine_diff_path *path = (struct combine_diff_path *)obj;
  1168. return path->path;
  1169. }
  1170. void diff_tree_combined(const unsigned char *sha1,
  1171. const struct sha1_array *parents,
  1172. int dense,
  1173. struct rev_info *rev)
  1174. {
  1175. struct diff_options *opt = &rev->diffopt;
  1176. struct diff_options diffopts;
  1177. struct combine_diff_path *p, *paths = NULL;
  1178. int i, num_paths, needsep, show_log_first, num_parent = parents->nr;
  1179. diffopts = *opt;
  1180. copy_pathspec(&diffopts.pathspec, &opt->pathspec);
  1181. diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
  1182. DIFF_OPT_SET(&diffopts, RECURSIVE);
  1183. DIFF_OPT_CLR(&diffopts, ALLOW_EXTERNAL);
  1184. /* tell diff_tree to emit paths in sorted (=tree) order */
  1185. diffopts.orderfile = NULL;
  1186. show_log_first = !!rev->loginfo && !rev->no_commit_id;
  1187. needsep = 0;
  1188. /* find set of paths that everybody touches */
  1189. for (i = 0; i < num_parent; i++) {
  1190. /* show stat against the first parent even
  1191. * when doing combined diff.
  1192. */
  1193. int stat_opt = (opt->output_format &
  1194. (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT));
  1195. if (i == 0 && stat_opt)
  1196. diffopts.output_format = stat_opt;
  1197. else
  1198. diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
  1199. diff_tree_sha1(parents->sha1[i], sha1, "", &diffopts);
  1200. diffcore_std(&diffopts);
  1201. paths = intersect_paths(paths, i, num_parent);
  1202. if (show_log_first && i == 0) {
  1203. show_log(rev);
  1204. if (rev->verbose_header && opt->output_format)
  1205. printf("%s%c", diff_line_prefix(opt),
  1206. opt->line_termination);
  1207. }
  1208. /* if showing diff, show it in requested order */
  1209. if (diffopts.output_format != DIFF_FORMAT_NO_OUTPUT &&
  1210. opt->orderfile) {
  1211. diffcore_order(opt->orderfile);
  1212. }
  1213. diff_flush(&diffopts);
  1214. }
  1215. /* find out number of surviving paths */
  1216. for (num_paths = 0, p = paths; p; p = p->next)
  1217. num_paths++;
  1218. /* order paths according to diffcore_order */
  1219. if (opt->orderfile && num_paths) {
  1220. struct obj_order *o;
  1221. o = xmalloc(sizeof(*o) * num_paths);
  1222. for (i = 0, p = paths; p; p = p->next, i++)
  1223. o[i].obj = p;
  1224. order_objects(opt->orderfile, path_path, o, num_paths);
  1225. for (i = 0; i < num_paths - 1; i++) {
  1226. p = o[i].obj;
  1227. p->next = o[i+1].obj;
  1228. }
  1229. p = o[num_paths-1].obj;
  1230. p->next = NULL;
  1231. paths = o[0].obj;
  1232. free(o);
  1233. }
  1234. if (num_paths) {
  1235. if (opt->output_format & (DIFF_FORMAT_RAW |
  1236. DIFF_FORMAT_NAME |
  1237. DIFF_FORMAT_NAME_STATUS)) {
  1238. for (p = paths; p; p = p->next)
  1239. show_raw_diff(p, num_parent, rev);
  1240. needsep = 1;
  1241. }
  1242. else if (opt->output_format &
  1243. (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT))
  1244. needsep = 1;
  1245. else if (opt->output_format & DIFF_FORMAT_CALLBACK)
  1246. handle_combined_callback(opt, paths, num_parent, num_paths);
  1247. if (opt->output_format & DIFF_FORMAT_PATCH) {
  1248. if (needsep)
  1249. printf("%s%c", diff_line_prefix(opt),
  1250. opt->line_termination);
  1251. for (p = paths; p; p = p->next)
  1252. show_patch_diff(p, num_parent, dense,
  1253. 0, rev);
  1254. }
  1255. }
  1256. /* Clean things up */
  1257. while (paths) {
  1258. struct combine_diff_path *tmp = paths;
  1259. paths = paths->next;
  1260. free(tmp);
  1261. }
  1262. free_pathspec(&diffopts.pathspec);
  1263. }
  1264. void diff_tree_combined_merge(const struct commit *commit, int dense,
  1265. struct rev_info *rev)
  1266. {
  1267. struct commit_list *parent = get_saved_parents(rev, commit);
  1268. struct sha1_array parents = SHA1_ARRAY_INIT;
  1269. while (parent) {
  1270. sha1_array_append(&parents, parent->item->object.sha1);
  1271. parent = parent->next;
  1272. }
  1273. diff_tree_combined(commit->object.sha1, &parents, dense, rev);
  1274. sha1_array_clear(&parents);
  1275. }