From af3feefa1d057713c2277cc1543d438e60c65306 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Tue, 24 Jan 2006 01:22:04 -0800 Subject: diff-tree -c: show a merge commit a bit more sensibly. A new option '-c' to diff-tree changes the way a merge commit is displayed when generating a patch output. It shows a "combined diff" (hence the option letter 'c'), which looks like this: $ git-diff-tree --pretty -c -p fec9ebf1 | head -n 18 diff-tree fec9ebf... (from parents) Merge: 0620db3... 8a263ae... Author: Junio C Hamano Date: Sun Jan 15 22:25:35 2006 -0800 Merge fixes up to GIT 1.1.3 diff --combined describe.c @@@ +98,7 @@@ return (a_date > b_date) ? -1 : (a_date == b_date) ? 0 : 1; } - static void describe(char *arg) - static void describe(struct commit *cmit, int last_one) ++ static void describe(char *arg, int last_one) { + unsigned char sha1[20]; + struct commit *cmit; There are a few things to note about this feature: - The '-c' option implies '-p'. It also implies '-m' halfway in the sense that "interesting" merges are shown, but not all merges. - When a blob matches one of the parents, we do not show a diff for that path at all. For a merge commit, this option shows paths with real file-level merge (aka "interesting things"). - As a concequence of the above, an "uninteresting" merge is not shown at all. You can use '-m' in addition to '-c' to show the commit log for such a merge, but there will be no combined diff output. - Unlike "gitk", the output is monochrome. A '-' character in the nth column means the line is from the nth parent and does not appear in the merge result (i.e. removed from that parent's version). A '+' character in the nth column means the line appears in the merge result, and the nth parent does not have that line (i.e. added by the merge itself or inherited from another parent). The above example output shows that the function signature was changed from either parents (hence two "-" lines and a "++" line), and "unsigned char sha1[20]", prefixed by a " +", was inherited from the first parent. The code as sent to the list was buggy in few corner cases, which I have fixed since then. It does not bother to keep track of and show the line numbers from parent commits, which it probably should. Signed-off-by: Junio C Hamano --- diff-tree.c | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) (limited to 'diff-tree.c') diff --git a/diff-tree.c b/diff-tree.c index efa2b9476e..0c689360ee 100644 --- a/diff-tree.c +++ b/diff-tree.c @@ -6,6 +6,8 @@ static int show_root_diff = 0; static int no_commit_id = 0; static int verbose_header = 0; static int ignore_merges = 1; +static int show_empty_combined = 0; +static int combine_merges = 0; static int read_stdin = 0; static const char *header = NULL; @@ -79,9 +81,13 @@ static const char *generate_header(const unsigned char *commit_sha1, offset = sprintf(this_header, "%s%s ", header_prefix, diff_unique_abbrev(commit_sha1, abbrev)); - offset += sprintf(this_header + offset, "(from %s)\n", - parent_sha1 ? - diff_unique_abbrev(parent_sha1, abbrev) : "root"); + if (commit_sha1 != parent_sha1) + offset += sprintf(this_header + offset, "(from %s)\n", + parent_sha1 + ? diff_unique_abbrev(parent_sha1, abbrev) + : "root"); + else + offset += sprintf(this_header + offset, "(from parents)\n"); offset += pretty_print_commit(commit_format, msg, len, this_header + offset, sizeof(this_header) - offset); @@ -108,8 +114,16 @@ static int diff_tree_commit(const unsigned char *commit_sha1) } /* More than one parent? */ - if (ignore_merges && commit->parents && commit->parents->next) - return 0; + if (commit->parents && commit->parents->next) { + if (ignore_merges) + return 0; + else if (combine_merges) { + header = generate_header(sha1, sha1, + commit->buffer); + return diff_tree_combined_merge(sha1, header, + show_empty_combined); + } + } for (parents = commit->parents; parents; parents = parents->next) { struct commit *parent = parents->item; @@ -154,7 +168,7 @@ static int diff_tree_stdin(char *line) } static const char diff_tree_usage[] = -"git-diff-tree [--stdin] [-m] [-s] [-v] [--pretty] [-t] [-r] [--root] " +"git-diff-tree [--stdin] [-m] [-c] [-s] [-v] [--pretty] [-t] [-r] [--root] " "[] [] [...]\n" " -r diff recursively\n" " --root include the initial commit as diff against /dev/null\n" @@ -217,6 +231,10 @@ int main(int argc, const char **argv) ignore_merges = 0; continue; } + if (!strcmp(arg, "-c")) { + combine_merges = 1; + continue; + } if (!strcmp(arg, "-v")) { verbose_header = 1; header_prefix = "diff-tree "; @@ -245,6 +263,12 @@ int main(int argc, const char **argv) if (diff_options.output_format == DIFF_FORMAT_PATCH) diff_options.recursive = 1; + if (combine_merges) { + diff_options.output_format = DIFF_FORMAT_PATCH; + show_empty_combined = !ignore_merges; + ignore_merges = 0; + } + diff_tree_setup_paths(get_pathspec(prefix, argv)); diff_setup_done(&diff_options); -- cgit v1.3 From d8f4790e6fe7a9d94468ea83d1370643604d43e5 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Tue, 24 Jan 2006 01:22:04 -0800 Subject: diff-tree --cc: denser combined diff output for a merge commit. Building on the previous '-c' (combined) option, '--cc' option squelches the output further by omitting hunks that consist of difference with solely one parent. Signed-off-by: Junio C Hamano --- combine-diff.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++----- diff-tree.c | 10 +++++++-- diff.h | 2 +- 3 files changed, 73 insertions(+), 8 deletions(-) (limited to 'diff-tree.c') diff --git a/combine-diff.c b/combine-diff.c index 669002349a..062ed8a7bf 100644 --- a/combine-diff.c +++ b/combine-diff.c @@ -278,7 +278,25 @@ static int interesting(struct sline *sline, unsigned long all_mask) return ((sline->flag & all_mask) != all_mask || sline->lost_head); } -static void make_hunks(struct sline *sline, unsigned long cnt, int num_parent) +static unsigned long line_diff_parents(struct sline *sline, unsigned long all_mask) +{ + /* + * Look at the line and see from which parents we have difference. + * Lower bits of sline->flag records if the parent had this line, + * so XOR with all_mask gives us on-bits for parents we have + * differences with. + */ + unsigned long parents = (sline->flag ^ all_mask); + if (sline->lost_head) { + struct lline *ll; + for (ll = sline->lost_head; ll; ll = ll->next) + parents |= ll->parent_map; + } + return parents & all_mask; +} + +static void make_hunks(struct sline *sline, unsigned long cnt, + int num_parent, int dense) { unsigned long all_mask = (1UL<parent_sha1[i], ourtmp, sline, cnt, i); - make_hunks(sline, cnt, num_parent); + make_hunks(sline, cnt, num_parent, dense); dump_sline(sline, cnt, num_parent); unlink(ourtmp); @@ -410,7 +468,8 @@ static void show_combined_diff(struct path_list *elem, int num_parent) } int diff_tree_combined_merge(const unsigned char *sha1, - const char *header, int show_empty_merge) + const char *header, + int show_empty_merge, int dense) { struct commit *commit = lookup_commit(sha1); struct diff_options diffopts; @@ -455,7 +514,7 @@ int diff_tree_combined_merge(const unsigned char *sha1, else printf("%s", p->path); putchar('\n'); - show_combined_diff(p, num_parent); + show_combined_diff(p, num_parent, dense); } } diff --git a/diff-tree.c b/diff-tree.c index 0c689360ee..99c580cf75 100644 --- a/diff-tree.c +++ b/diff-tree.c @@ -8,6 +8,7 @@ static int verbose_header = 0; static int ignore_merges = 1; static int show_empty_combined = 0; static int combine_merges = 0; +static int dense_combined_merges = 0; static int read_stdin = 0; static const char *header = NULL; @@ -121,7 +122,8 @@ static int diff_tree_commit(const unsigned char *commit_sha1) header = generate_header(sha1, sha1, commit->buffer); return diff_tree_combined_merge(sha1, header, - show_empty_combined); + show_empty_combined, + dense_combined_merges); } } @@ -168,7 +170,7 @@ static int diff_tree_stdin(char *line) } static const char diff_tree_usage[] = -"git-diff-tree [--stdin] [-m] [-c] [-s] [-v] [--pretty] [-t] [-r] [--root] " +"git-diff-tree [--stdin] [-m] [-c] [--cc] [-s] [-v] [--pretty] [-t] [-r] [--root] " "[] [] [...]\n" " -r diff recursively\n" " --root include the initial commit as diff against /dev/null\n" @@ -235,6 +237,10 @@ int main(int argc, const char **argv) combine_merges = 1; continue; } + if (!strcmp(arg, "--cc")) { + dense_combined_merges = combine_merges = 1; + continue; + } if (!strcmp(arg, "-v")) { verbose_header = 1; header_prefix = "diff-tree "; diff --git a/diff.h b/diff.h index 081234cb97..ab0d47b982 100644 --- a/diff.h +++ b/diff.h @@ -56,7 +56,7 @@ extern int diff_tree(struct tree_desc *t1, struct tree_desc *t2, extern int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base, struct diff_options *opt); -extern int diff_tree_combined_merge(const unsigned char *sha1, const char *, int); +extern int diff_tree_combined_merge(const unsigned char *sha1, const char *, int, int); extern void diff_addremove(struct diff_options *, int addremove, -- cgit v1.3 From b2d4c56f2f370481c80e478ac323ebb13eece807 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 25 Jan 2006 02:37:40 -0800 Subject: diff-tree: abbreviate merge parent object names with --abbrev --pretty. When --abbrev is in effect, abbreviate the merge parent names in prettyprinted output. Signed-off-by: Junio C Hamano --- commit.c | 19 ++++++++++++++----- commit.h | 2 +- diff-tree.c | 2 +- rev-list.c | 2 +- show-branch.c | 2 +- 5 files changed, 18 insertions(+), 9 deletions(-) (limited to 'diff-tree.c') diff --git a/commit.c b/commit.c index b8bf35e860..244104f930 100644 --- a/commit.c +++ b/commit.c @@ -426,9 +426,10 @@ static int is_empty_line(const char *line, int len) return !len; } -static int add_parent_info(enum cmit_fmt fmt, char *buf, const char *line, int parents) +static int add_parent_info(enum cmit_fmt fmt, char *buf, const char *line, int parents, int abbrev) { int offset = 0; + unsigned char sha1[20]; if (fmt == CMIT_FMT_ONELINE) return offset; @@ -437,17 +438,25 @@ static int add_parent_info(enum cmit_fmt fmt, char *buf, const char *line, int p break; case 2: /* Go back to the previous line: 40 characters of previous parent, and one '\n' */ - offset = sprintf(buf, "Merge: %.40s\n", line-41); + if (abbrev && !get_sha1_hex(line-41, sha1)) + offset = sprintf(buf, "Merge: %s\n", + find_unique_abbrev(sha1, abbrev)); + else + offset = sprintf(buf, "Merge: %.40s\n", line-41); /* Fallthrough */ default: /* Replace the previous '\n' with a space */ buf[offset-1] = ' '; - offset += sprintf(buf + offset, "%.40s\n", line+7); + if (abbrev && !get_sha1_hex(line+7, sha1)) + offset += sprintf(buf + offset, "%s\n", + find_unique_abbrev(sha1, abbrev)); + else + offset += sprintf(buf + offset, "%.40s\n", line+7); } return offset; } -unsigned long pretty_print_commit(enum cmit_fmt fmt, const char *msg, unsigned long len, char *buf, unsigned long space) +unsigned long pretty_print_commit(enum cmit_fmt fmt, const char *msg, unsigned long len, char *buf, unsigned long space, int abbrev) { int hdr = 1, body = 0; unsigned long offset = 0; @@ -488,7 +497,7 @@ unsigned long pretty_print_commit(enum cmit_fmt fmt, const char *msg, unsigned l if (!memcmp(line, "parent ", 7)) { if (linelen != 48) die("bad parent line in commit"); - offset += add_parent_info(fmt, buf + offset, line, ++parents); + offset += add_parent_info(fmt, buf + offset, line, ++parents, abbrev); } /* diff --git a/commit.h b/commit.h index 9c4a244bd9..a8c20964e0 100644 --- a/commit.h +++ b/commit.h @@ -48,7 +48,7 @@ enum cmit_fmt { }; extern enum cmit_fmt get_commit_format(const char *arg); -extern unsigned long pretty_print_commit(enum cmit_fmt fmt, const char *msg, unsigned long len, char *buf, unsigned long space); +extern unsigned long pretty_print_commit(enum cmit_fmt fmt, const char *msg, unsigned long len, char *buf, unsigned long space, int abbrev); /** Removes the first commit from a list sorted by date, and adds all * of its parents. diff --git a/diff-tree.c b/diff-tree.c index efa2b9476e..efa17d14a4 100644 --- a/diff-tree.c +++ b/diff-tree.c @@ -84,7 +84,7 @@ static const char *generate_header(const unsigned char *commit_sha1, diff_unique_abbrev(parent_sha1, abbrev) : "root"); offset += pretty_print_commit(commit_format, msg, len, this_header + offset, - sizeof(this_header) - offset); + sizeof(this_header) - offset, abbrev); return this_header; } diff --git a/rev-list.c b/rev-list.c index e00e6fc76d..5fad300635 100644 --- a/rev-list.c +++ b/rev-list.c @@ -81,7 +81,7 @@ static void show_commit(struct commit *commit) if (verbose_header) { static char pretty_header[16384]; - pretty_print_commit(commit_format, commit->buffer, ~0, pretty_header, sizeof(pretty_header)); + pretty_print_commit(commit_format, commit->buffer, ~0, pretty_header, sizeof(pretty_header), 0); printf("%s%c", pretty_header, hdr_termination); } fflush(stdout); diff --git a/show-branch.c b/show-branch.c index 7a0dcc649e..d06e577401 100644 --- a/show-branch.c +++ b/show-branch.c @@ -259,7 +259,7 @@ static void show_one_commit(struct commit *commit, int no_name) struct commit_name *name = commit->object.util; if (commit->object.parsed) pretty_print_commit(CMIT_FMT_ONELINE, commit->buffer, ~0, - pretty, sizeof(pretty)); + pretty, sizeof(pretty), 0); else strcpy(pretty, "(unavailable)"); if (!strncmp(pretty, "[PATCH] ", 8)) -- cgit v1.3 From 3815f423ae39bf774de3c268c6d3e3b72128a4e5 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Fri, 27 Jan 2006 01:54:59 -0800 Subject: pretty_print_commit(): pass commit object instead of commit->buffer. Signed-off-by: Junio C Hamano --- commit.c | 3 ++- commit.h | 2 +- diff-tree.c | 11 +++++------ rev-list.c | 2 +- show-branch.c | 2 +- 5 files changed, 10 insertions(+), 10 deletions(-) (limited to 'diff-tree.c') diff --git a/commit.c b/commit.c index 244104f930..e8f53e8966 100644 --- a/commit.c +++ b/commit.c @@ -456,12 +456,13 @@ static int add_parent_info(enum cmit_fmt fmt, char *buf, const char *line, int p return offset; } -unsigned long pretty_print_commit(enum cmit_fmt fmt, const char *msg, unsigned long len, char *buf, unsigned long space, int abbrev) +unsigned long pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit, unsigned long len, char *buf, unsigned long space, int abbrev) { int hdr = 1, body = 0; unsigned long offset = 0; int parents = 0; int indent = (fmt == CMIT_FMT_ONELINE) ? 0 : 4; + const char *msg = commit->buffer; for (;;) { const char *line = msg; diff --git a/commit.h b/commit.h index a8c20964e0..986b22de8a 100644 --- a/commit.h +++ b/commit.h @@ -48,7 +48,7 @@ enum cmit_fmt { }; extern enum cmit_fmt get_commit_format(const char *arg); -extern unsigned long pretty_print_commit(enum cmit_fmt fmt, const char *msg, unsigned long len, char *buf, unsigned long space, int abbrev); +extern unsigned long pretty_print_commit(enum cmit_fmt fmt, const struct commit *, unsigned long len, char *buf, unsigned long space, int abbrev); /** Removes the first commit from a list sorted by date, and adds all * of its parents. diff --git a/diff-tree.c b/diff-tree.c index efa17d14a4..44bc2381c9 100644 --- a/diff-tree.c +++ b/diff-tree.c @@ -64,12 +64,13 @@ static int diff_root_tree(const unsigned char *new, const char *base) static const char *generate_header(const unsigned char *commit_sha1, const unsigned char *parent_sha1, - const char *msg) + const struct commit *commit) { static char this_header[16384]; int offset; unsigned long len; int abbrev = diff_options.abbrev; + const char *msg = commit->buffer; if (!verbose_header) return sha1_to_hex(commit_sha1); @@ -82,7 +83,7 @@ static const char *generate_header(const unsigned char *commit_sha1, offset += sprintf(this_header + offset, "(from %s)\n", parent_sha1 ? diff_unique_abbrev(parent_sha1, abbrev) : "root"); - offset += pretty_print_commit(commit_format, msg, len, + offset += pretty_print_commit(commit_format, commit, len, this_header + offset, sizeof(this_header) - offset, abbrev); return this_header; @@ -103,7 +104,7 @@ static int diff_tree_commit(const unsigned char *commit_sha1) /* Root commit? */ if (show_root_diff && !commit->parents) { - header = generate_header(sha1, NULL, commit->buffer); + header = generate_header(sha1, NULL, commit); diff_root_tree(commit_sha1, ""); } @@ -113,9 +114,7 @@ static int diff_tree_commit(const unsigned char *commit_sha1) for (parents = commit->parents; parents; parents = parents->next) { struct commit *parent = parents->item; - header = generate_header(sha1, - parent->object.sha1, - commit->buffer); + header = generate_header(sha1, parent->object.sha1, commit); diff_tree_sha1_top(parent->object.sha1, commit_sha1, ""); if (!header && verbose_header) { header_prefix = "\ndiff-tree "; diff --git a/rev-list.c b/rev-list.c index 5fad300635..334713ab66 100644 --- a/rev-list.c +++ b/rev-list.c @@ -81,7 +81,7 @@ static void show_commit(struct commit *commit) if (verbose_header) { static char pretty_header[16384]; - pretty_print_commit(commit_format, commit->buffer, ~0, pretty_header, sizeof(pretty_header), 0); + pretty_print_commit(commit_format, commit, ~0, pretty_header, sizeof(pretty_header), 0); printf("%s%c", pretty_header, hdr_termination); } fflush(stdout); diff --git a/show-branch.c b/show-branch.c index d06e577401..ffe7456a6a 100644 --- a/show-branch.c +++ b/show-branch.c @@ -258,7 +258,7 @@ static void show_one_commit(struct commit *commit, int no_name) char pretty[256], *cp; struct commit_name *name = commit->object.util; if (commit->object.parsed) - pretty_print_commit(CMIT_FMT_ONELINE, commit->buffer, ~0, + pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0, pretty, sizeof(pretty), 0); else strcpy(pretty, "(unavailable)"); -- cgit v1.3