@matheusbsbs / matheus-bagliano / commits / 8a470ebfa1
Fork 0

combine-diff: do not lose hunks with only deletion at end.

We used to lose hunks that appear at the end and have only deletion. This makes sure that the record beyond the end of file (which holds such deletions) is examined. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Apr 11, 2006 at 03:13 UTC 8a470ebfa1226724321b2df2c3e464c8e3600aa1
1 file changed +29 -11
combine-diff.c
+29 -11
@@ -243,7 +243,7 @@ static void combine_diff(const unsigned char *parent, mmfile_t *result_file,
243 * started by showing sline[lno] (possibly showing the lost
244 * lines attached to it first).
245 */
246 - for (lno = 0, p_lno = 1; lno < cnt; lno++) {
246 + for (lno = 0, p_lno = 1; lno <= cnt; lno++) {
247 struct lline *ll;
248 sline[lno].p_lno[n] = p_lno;
249
@@ -254,7 +254,7 @@ static void combine_diff(const unsigned char *parent, mmfile_t *result_file,
254 p_lno++; /* '-' means parent had it */
255 ll = ll->next;
256 }
257 - if (!(sline[lno].flag & nmask))
257 + if (lno < cnt && !(sline[lno].flag & nmask))
258 p_lno++; /* no '+' means parent had it */
259 }
260 sline[lno].p_lno[n] = p_lno; /* trailer */
@@ -509,15 +509,28 @@ static void dump_sline(struct sline *sline, unsigned long cnt, int num_parent)
509 int hunk_end;
510 while (lno < cnt && !(sline[lno].flag & mark))
511 lno++;
512 - if (cnt <= lno)
512 + if (cnt < lno)
513 break;
514 - for (hunk_end = lno + 1; hunk_end < cnt; hunk_end++)
515 - if (!(sline[hunk_end].flag & mark))
514 + else if (cnt == lno) {
515 + /* See if there is anything interesting */
516 + struct lline *ll;
517 + for (ll = sline[lno].lost_head; ll; ll = ll->next)
518 + if (ll->parent_map)
519 + break;
520 + if (!ll)
521 break;
522 + hunk_end = cnt + 1;
523 + }
524 + else {
525 + for (hunk_end = lno + 1; hunk_end < cnt; hunk_end++)
526 + if (!(sline[hunk_end].flag & mark))
527 + break;
528 + }
529 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
530 for (i = 0; i < num_parent; i++)
531 show_parent_lno(sline, lno, hunk_end, cnt, i);
520 - printf(" +%lu,%lu ", lno+1, hunk_end-lno);
532 + printf(" +%lu,%lu ", lno+1,
533 + (cnt == lno) ? 0 : hunk_end - lno);
534 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
535 putchar('\n');
536 while (lno < hunk_end) {
@@ -536,6 +549,8 @@ static void dump_sline(struct sline *sline, unsigned long cnt, int num_parent)
549 puts(ll->line);
550 ll = ll->next;
551 }
552 + if (cnt <= lno)
553 + break;
554 p_mask = 1;
555 for (j = 0; j < num_parent; j++) {
556 if (p_mask & sl->flag)
@@ -560,7 +575,7 @@ static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
575 imask = (1UL<<i);
576 jmask = (1UL<<j);
577
563 - for (lno = 0; lno < cnt; lno++) {
578 + for (lno = 0; lno <= cnt; lno++) {
579 struct lline *ll = sline->lost_head;
580 sline->p_lno[i] = sline->p_lno[j];
581 while (ll) {
@@ -632,10 +647,10 @@ static int show_patch_diff(struct combine_diff_path *elem, int num_parent,
647 if (result_size && result[result_size-1] != '\n')
648 cnt++; /* incomplete line */
649
635 - sline = xcalloc(cnt+1, sizeof(*sline));
650 + sline = xcalloc(cnt+2, sizeof(*sline));
651 ep = result;
652 sline[0].bol = result;
638 - for (lno = 0; lno <= cnt; lno++) {
653 + for (lno = 0; lno <= cnt + 1; lno++) {
654 sline[lno].lost_tail = &sline[lno].lost_head;
655 sline[lno].flag = 0;
656 }
@@ -653,8 +668,11 @@ static int show_patch_diff(struct combine_diff_path *elem, int num_parent,
668 result_file.ptr = result;
669 result_file.size = result_size;
670
656 - sline[0].p_lno = xcalloc((cnt+1) * num_parent, sizeof(unsigned long));
657 - for (lno = 0; lno < cnt; lno++)
671 + /* Even p_lno[cnt+1] is valid -- that is for the end line number
672 + * for deletion hunk at the end.
673 + */
674 + sline[0].p_lno = xcalloc((cnt+2) * num_parent, sizeof(unsigned long));
675 + for (lno = 0; lno <= cnt; lno++)
676 sline[lno+1].p_lno = sline[lno].p_lno + num_parent;
677
678 for (i = 0; i < num_parent; i++) {