Skip to content

Commit 7c978a0

Browse files
peffgitster
authored andcommitted
combine-diff: split header printing into its own function
This is a pretty big logical chunk, so it makes the function a bit more readable to have it split out. In addition, it will make it easier to add an alternate code path for binary diffs in a future patch. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 391b142 commit 7c978a0

File tree

1 file changed

+74
-61
lines changed

1 file changed

+74
-61
lines changed

combine-diff.c

Lines changed: 74 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,78 @@ static void dump_quoted_path(const char *head,
681681
puts(buf.buf);
682682
}
683683

684+
static void show_combined_header(struct combine_diff_path *elem,
685+
int num_parent,
686+
int dense,
687+
struct rev_info *rev,
688+
int mode_differs)
689+
{
690+
struct diff_options *opt = &rev->diffopt;
691+
int abbrev = DIFF_OPT_TST(opt, FULL_INDEX) ? 40 : DEFAULT_ABBREV;
692+
const char *a_prefix = opt->a_prefix ? opt->a_prefix : "a/";
693+
const char *b_prefix = opt->b_prefix ? opt->b_prefix : "b/";
694+
int use_color = DIFF_OPT_TST(opt, COLOR_DIFF);
695+
const char *c_meta = diff_get_color(use_color, DIFF_METAINFO);
696+
const char *c_reset = diff_get_color(use_color, DIFF_RESET);
697+
const char *abb;
698+
int added = 0;
699+
int deleted = 0;
700+
int i;
701+
702+
if (rev->loginfo && !rev->no_commit_id)
703+
show_log(rev);
704+
705+
dump_quoted_path(dense ? "diff --cc " : "diff --combined ",
706+
"", elem->path, c_meta, c_reset);
707+
printf("%sindex ", c_meta);
708+
for (i = 0; i < num_parent; i++) {
709+
abb = find_unique_abbrev(elem->parent[i].sha1,
710+
abbrev);
711+
printf("%s%s", i ? "," : "", abb);
712+
}
713+
abb = find_unique_abbrev(elem->sha1, abbrev);
714+
printf("..%s%s\n", abb, c_reset);
715+
716+
if (mode_differs) {
717+
deleted = !elem->mode;
718+
719+
/* We say it was added if nobody had it */
720+
added = !deleted;
721+
for (i = 0; added && i < num_parent; i++)
722+
if (elem->parent[i].status !=
723+
DIFF_STATUS_ADDED)
724+
added = 0;
725+
if (added)
726+
printf("%snew file mode %06o",
727+
c_meta, elem->mode);
728+
else {
729+
if (deleted)
730+
printf("%sdeleted file ", c_meta);
731+
printf("mode ");
732+
for (i = 0; i < num_parent; i++) {
733+
printf("%s%06o", i ? "," : "",
734+
elem->parent[i].mode);
735+
}
736+
if (elem->mode)
737+
printf("..%06o", elem->mode);
738+
}
739+
printf("%s\n", c_reset);
740+
}
741+
742+
if (added)
743+
dump_quoted_path("--- ", "", "/dev/null",
744+
c_meta, c_reset);
745+
else
746+
dump_quoted_path("--- ", a_prefix, elem->path,
747+
c_meta, c_reset);
748+
if (deleted)
749+
dump_quoted_path("+++ ", "", "/dev/null",
750+
c_meta, c_reset);
751+
else
752+
dump_quoted_path("+++ ", b_prefix, elem->path,
753+
c_meta, c_reset);
754+
}
755+
684756
static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
685757
int dense, struct rev_info *rev)
686758
{
@@ -692,13 +764,9 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
692764
int mode_differs = 0;
693765
int i, show_hunks;
694766
int working_tree_file = is_null_sha1(elem->sha1);
695-
int abbrev = DIFF_OPT_TST(opt, FULL_INDEX) ? 40 : DEFAULT_ABBREV;
696-
const char *a_prefix, *b_prefix;
697767
mmfile_t result_file;
698768

699769
context = opt->context;
700-
a_prefix = opt->a_prefix ? opt->a_prefix : "a/";
701-
b_prefix = opt->b_prefix ? opt->b_prefix : "b/";
702770

703771
/* Read the result of merge first */
704772
if (!working_tree_file)
@@ -832,63 +900,8 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
832900
show_hunks = make_hunks(sline, cnt, num_parent, dense);
833901

834902
if (show_hunks || mode_differs || working_tree_file) {
835-
const char *abb;
836-
int use_color = DIFF_OPT_TST(opt, COLOR_DIFF);
837-
const char *c_meta = diff_get_color(use_color, DIFF_METAINFO);
838-
const char *c_reset = diff_get_color(use_color, DIFF_RESET);
839-
int added = 0;
840-
int deleted = 0;
841-
842-
if (rev->loginfo && !rev->no_commit_id)
843-
show_log(rev);
844-
dump_quoted_path(dense ? "diff --cc " : "diff --combined ",
845-
"", elem->path, c_meta, c_reset);
846-
printf("%sindex ", c_meta);
847-
for (i = 0; i < num_parent; i++) {
848-
abb = find_unique_abbrev(elem->parent[i].sha1,
849-
abbrev);
850-
printf("%s%s", i ? "," : "", abb);
851-
}
852-
abb = find_unique_abbrev(elem->sha1, abbrev);
853-
printf("..%s%s\n", abb, c_reset);
854-
855-
if (mode_differs) {
856-
deleted = !elem->mode;
857-
858-
/* We say it was added if nobody had it */
859-
added = !deleted;
860-
for (i = 0; added && i < num_parent; i++)
861-
if (elem->parent[i].status !=
862-
DIFF_STATUS_ADDED)
863-
added = 0;
864-
if (added)
865-
printf("%snew file mode %06o",
866-
c_meta, elem->mode);
867-
else {
868-
if (deleted)
869-
printf("%sdeleted file ", c_meta);
870-
printf("mode ");
871-
for (i = 0; i < num_parent; i++) {
872-
printf("%s%06o", i ? "," : "",
873-
elem->parent[i].mode);
874-
}
875-
if (elem->mode)
876-
printf("..%06o", elem->mode);
877-
}
878-
printf("%s\n", c_reset);
879-
}
880-
if (added)
881-
dump_quoted_path("--- ", "", "/dev/null",
882-
c_meta, c_reset);
883-
else
884-
dump_quoted_path("--- ", a_prefix, elem->path,
885-
c_meta, c_reset);
886-
if (deleted)
887-
dump_quoted_path("+++ ", "", "/dev/null",
888-
c_meta, c_reset);
889-
else
890-
dump_quoted_path("+++ ", b_prefix, elem->path,
891-
c_meta, c_reset);
903+
show_combined_header(elem, num_parent, dense, rev,
904+
mode_differs);
892905
dump_sline(sline, cnt, num_parent,
893906
DIFF_OPT_TST(opt, COLOR_DIFF), result_deleted);
894907
}

0 commit comments

Comments
 (0)