@matheusbsbs / matheus-bagliano / commits / d5f6a01af0
Fork 0

combine-diff: a few more finishing touches.

"new file" and "deleted file" were already reported in the original code, but the logic was not as transparent as it could have. This uses a few variables and more comments to clarify the flow. The rule is: (1) if a path exists in the merge result when no parent had it, we report "new" (otherwise it came from the parents, as opposed to have added by the evil merge). (2) if the path does not exist in the merge result, it is "deleted". Since we can say "new" and "deleted", there is no reason not to follow the /dev/null convention. This fixes it. Appending function name after @@@ ... @@@ is trivial, so implement it. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Oct 26, 2006 at 00:05 UTC d5f6a01af0658bc0ec5f068d81ba321be94526d5
2 files changed +46 -8
Documentation/diff-format.txt
+3 -3
@@ -212,9 +212,9 @@ copying detection) are designed to work with diff of two
212 --- a/file
213 +++ b/file
214 +
215 -Contrary to two-line header for traditional 'unified' diff
216 -format, and similar to filenames in ordinary "diff header",
217 -/dev/null is not used for creation or deletion.
215 +Similar to two-line header for traditional 'unified' diff
216 +format, `/dev/null` is used to signal created or deleted
217 +files.
218
219 4. Chunk header format is modified to prevent people from
220 accidentally feeding it to `patch -p1`. Combined diff format
combine-diff.c
+43 -5
@@ -489,6 +489,12 @@ static void show_parent_lno(struct sline *sline, unsigned long l0, unsigned long
489 printf(" -%lu,%lu", l0, l1-l0);
490 }
491
492 +static int hunk_comment_line(const char *bol)
493 +{
494 + int ch = *bol & 0xff;
495 + return (isalpha(ch) || ch == '_' || ch == '$');
496 +}
497 +
498 static void dump_sline(struct sline *sline, unsigned long cnt, int num_parent,
499 int use_color)
500 {
@@ -508,8 +514,13 @@ static void dump_sline(struct sline *sline, unsigned long cnt, int num_parent,
514 struct sline *sl = &sline[lno];
515 unsigned long hunk_end;
516 unsigned long rlines;
511 - while (lno <= cnt && !(sline[lno].flag & mark))
517 + const char *hunk_comment = NULL;
518 +
519 + while (lno <= cnt && !(sline[lno].flag & mark)) {
520 + if (hunk_comment_line(sline[lno].bol))
521 + hunk_comment = sline[lno].bol;
522 lno++;
523 + }
524 if (cnt < lno)
525 break;
526 else {
@@ -526,6 +537,22 @@ static void dump_sline(struct sline *sline, unsigned long cnt, int num_parent,
537 show_parent_lno(sline, lno, hunk_end, i);
538 printf(" +%lu,%lu ", lno+1, rlines);
539 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
540 +
541 + if (hunk_comment) {
542 + int comment_end = 0;
543 + for (i = 0; i < 40; i++) {
544 + int ch = hunk_comment[i] & 0xff;
545 + if (!ch || ch == '\n')
546 + break;
547 + if (!isspace(ch))
548 + comment_end = i;
549 + }
550 + if (comment_end)
551 + putchar(' ');
552 + for (i = 0; i < comment_end; i++)
553 + putchar(hunk_comment[i]);
554 + }
555 +
556 printf("%s\n", c_reset);
557 while (lno < hunk_end) {
558 struct lline *ll;
@@ -707,6 +734,8 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
734 int use_color = opt->color_diff;
735 const char *c_meta = diff_get_color(use_color, DIFF_METAINFO);
736 const char *c_reset = diff_get_color(use_color, DIFF_RESET);
737 + int added = 0;
738 + int deleted = 0;
739
740 if (rev->loginfo)
741 show_log(rev, opt->msg_sep);
@@ -722,7 +751,10 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
751 printf("..%s%s\n", abb, c_reset);
752
753 if (mode_differs) {
725 - int added = !!elem->mode;
754 + deleted = !elem->mode;
755 +
756 + /* We say it was added if nobody had it */
757 + added = !deleted;
758 for (i = 0; added && i < num_parent; i++)
759 if (elem->parent[i].status !=
760 DIFF_STATUS_ADDED)
@@ -731,7 +763,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
763 printf("%snew file mode %06o",
764 c_meta, elem->mode);
765 else {
734 - if (!elem->mode)
766 + if (deleted)
767 printf("%sdeleted file ", c_meta);
768 printf("mode ");
769 for (i = 0; i < num_parent; i++) {
@@ -743,8 +775,14 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
775 }
776 printf("%s\n", c_reset);
777 }
746 - dump_quoted_path("--- a/", elem->path, c_meta, c_reset);
747 - dump_quoted_path("+++ b/", elem->path, c_meta, c_reset);
778 + if (added)
779 + dump_quoted_path("--- /dev/", "null", c_meta, c_reset);
780 + else
781 + dump_quoted_path("--- a/", elem->path, c_meta, c_reset);
782 + if (deleted)
783 + dump_quoted_path("+++ /dev/", "null", c_meta, c_reset);
784 + else
785 + dump_quoted_path("+++ b/", elem->path, c_meta, c_reset);
786 dump_sline(sline, cnt, num_parent, opt->color_diff);
787 }
788 free(result);