From ee63802422af14e43eccce3c6dc4150a27ceb1a3 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Thu, 9 Feb 2006 10:30:28 -0800 Subject: [PATCH 1/5] diff-tree -c raw output NOTE! This makes "-c" be the default, which effectively means that merges are never ignored any more, and "-m" is a no-op. So it changes semantics. I would also like to make "--cc" the default if you do patches, but didn't actually do that. The raw output format is not wonderfully pretty, but it's distinguishable from a "normal patch" in that a normal patch with just one parent has just one colon at the beginning, while a multi-parent raw diff has colons for parents. So now, in the kernel, when you do git-diff-tree cce0cac125623f9b68f25dd1350f6d616220a8dd (to see the manual ARM merge that had a conflict in arch/arm/Kconfig), you get cce0cac125623f9b68f25dd1350f6d616220a8dd ::100644 100644 100644 4a63a8e2e45247a11c068c6ed66c6e7aba29ddd9 77eee38762d69d3de95ae45dd9278df9b8225e2c 2f61726d2f4b636f6e66696700dbf71a59dad287 arch/arm/Kconfig ie you see two colons (two parents), then three modes (parent modes followed by result mode), then three sha1s (parent sha1s followed by result sha1). Which is pretty close to the normal raw diff output. Cool/stupid exercise: $ git-whatchanged | grep '^::' | cut -f2- | sort | uniq -c | sort -n | less -S will show which files have needed the most file-level merge conflict resolution. Useful? Probably not. But kind of interesting. For the kernel, it's .... 10 arch/ia64/Kconfig 11 drivers/scsi/Kconfig 12 drivers/net/Makefile 17 include/linux/libata.h 18 include/linux/pci_ids.h 23 drivers/net/Kconfig 24 drivers/scsi/libata-scsi.c 28 drivers/scsi/libata-core.c 43 MAINTAINERS Signed-off-by: Junio C Hamano --- combine-diff.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++--- diff-tree.c | 18 +++++++++++------ diff.h | 6 +++--- 3 files changed, 67 insertions(+), 12 deletions(-) diff --git a/combine-diff.c b/combine-diff.c index 6a9f3683c5..15f369e8e6 100644 --- a/combine-diff.c +++ b/combine-diff.c @@ -776,8 +776,52 @@ int show_combined_diff(struct combine_diff_path *elem, int num_parent, return shown_header; } -int diff_tree_combined_merge(const unsigned char *sha1, - const char *header, int dense) +#define COLONS "::::::::::::::::::::::::::::::::" + +static void show_raw_diff(struct combine_diff_path *p, int num_parent, const char *header, struct diff_options *opt) +{ + int i, offset, mod_type = 'A'; + const char *prefix; + int line_termination, inter_name_termination; + + line_termination = opt->line_termination; + inter_name_termination = '\t'; + if (!line_termination) + inter_name_termination = 0; + + if (header) + puts(header); + offset = strlen(COLONS) - num_parent; + if (offset < 0) + offset = 0; + prefix = COLONS + offset; + + /* Show the modes */ + for (i = 0; i < num_parent; i++) { + int mode = p->parent[i].mode; + if (mode) + mod_type = 'M'; + printf("%s%06o", prefix, mode); + prefix = " "; + } + printf("%s%06o", prefix, p->mode); + if (!p->mode) + mod_type = 'D'; + + /* Show sha1's */ + for (i = 0; i < num_parent; i++) { + printf("%s%s", prefix, diff_unique_abbrev(p->parent[i].sha1, opt->abbrev)); + prefix = " "; + } + printf("%s%s", prefix, diff_unique_abbrev(p->sha1, opt->abbrev)); + + /* Modification type, terminations, filename */ + printf(" %c%c%s%c", mod_type, inter_name_termination, p->path, line_termination); +} + +const char *diff_tree_combined_merge(const unsigned char *sha1, + const char *header, int dense, + struct diff_options *opt) { struct commit *commit = lookup_commit(sha1); struct diff_options diffopts; @@ -815,6 +859,11 @@ int diff_tree_combined_merge(const unsigned char *sha1, for (p = paths; p; p = p->next) { if (!p->len) continue; + if (opt->output_format == DIFF_FORMAT_RAW) { + show_raw_diff(p, num_parent, header, opt); + header = NULL; + continue; + } if (show_combined_diff(p, num_parent, dense, header)) header = NULL; } @@ -826,5 +875,5 @@ int diff_tree_combined_merge(const unsigned char *sha1, paths = paths->next; free(tmp); } - return 0; + return header; } diff --git a/diff-tree.c b/diff-tree.c index 7148323348..df6fd97fca 100644 --- a/diff-tree.c +++ b/diff-tree.c @@ -6,7 +6,7 @@ static int show_root_diff = 0; static int no_commit_id = 0; static int verbose_header = 0; static int ignore_merges = 1; -static int combine_merges = 0; +static int combine_merges = 1; static int dense_combined_merges = 0; static int read_stdin = 0; static int always_show_header = 0; @@ -117,8 +117,12 @@ static int diff_tree_commit(struct commit *commit) return 0; else if (combine_merges) { header = generate_header(sha1, sha1, commit); - return diff_tree_combined_merge(sha1, header, - dense_combined_merges); + header = diff_tree_combined_merge(sha1, header, + dense_combined_merges, + &diff_options); + if (!header && verbose_header) + header_prefix = "\ndiff-tree "; + return 0; } } @@ -285,10 +289,12 @@ int main(int argc, const char **argv) usage(diff_tree_usage); } - if (combine_merges) { - diff_options.output_format = DIFF_FORMAT_PATCH; + if (combine_merges) ignore_merges = 0; - } + + /* We can only do dense combined merges with diff output */ + if (dense_combined_merges) + diff_options.output_format = DIFF_FORMAT_PATCH; if (diff_options.output_format == DIFF_FORMAT_PATCH) diff_options.recursive = 1; diff --git a/diff.h b/diff.h index 5c5e7fa91f..9088519af0 100644 --- a/diff.h +++ b/diff.h @@ -74,10 +74,10 @@ struct combine_diff_path { (sizeof(struct combine_diff_path) + \ sizeof(struct combine_diff_parent) * (n) + (l) + 1) -int show_combined_diff(struct combine_diff_path *elem, int num_parent, - int dense, const char *header); +extern int show_combined_diff(struct combine_diff_path *elem, int num_parent, + int dense, const char *header); -extern int diff_tree_combined_merge(const unsigned char *sha1, const char *, int); +extern const char *diff_tree_combined_merge(const unsigned char *sha1, const char *, int, struct diff_options *opt); extern void diff_addremove(struct diff_options *, int addremove, From 5b23683251801381a17b5c3e82940c268821681c Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Thu, 9 Feb 2006 14:35:19 -0800 Subject: [PATCH 2/5] combined-diff: use diffcore before intersecting paths. This is needed to make "diff-tree -c -M" to work semi-sensibly. Otherwise rename detection, pickaxe and friends would never be invoked. Signed-off-by: Junio C Hamano --- combine-diff.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/combine-diff.c b/combine-diff.c index 15f369e8e6..6d783054d5 100644 --- a/combine-diff.c +++ b/combine-diff.c @@ -829,7 +829,7 @@ const char *diff_tree_combined_merge(const unsigned char *sha1, struct combine_diff_path *p, *paths = NULL; int num_parent, i, num_paths; - diff_setup(&diffopts); + diffopts = *opt; diffopts.output_format = DIFF_FORMAT_NO_OUTPUT; diffopts.recursive = 1; @@ -846,6 +846,7 @@ const char *diff_tree_combined_merge(const unsigned char *sha1, struct commit *parent = parents->item; diff_tree_sha1(parent->object.sha1, commit->object.sha1, "", &diffopts); + diffcore_std(&diffopts); paths = intersect_paths(paths, i, num_parent); diff_flush(&diffopts); } From 0a798076b8d1a4a31bf2b24c564e2a99fd1c43a1 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Thu, 9 Feb 2006 15:23:06 -0800 Subject: [PATCH 3/5] combine-diff: move formatting logic to show_combined_diff() This way, diff-files can make use of it. Also implement the full suite of what diff_flush_raw() supports just for consistency. With this, 'diff-tree -c -r --name-status' would show what is expected. There is no way to get the historical output (useful for debugging and low-level Plumbing work) anymore, so tentatively it makes '-m' to mean "do not combine and show individual diffs with parents". diff-files matches diff-tree to produce raw output for -c. For textual combined diff, use -p -c. Signed-off-by: Junio C Hamano --- combine-diff.c | 85 ++++++++++++++++++++++++++++++++++---------------- diff-files.c | 6 ++-- diff-tree.c | 2 +- diff.h | 3 +- 4 files changed, 64 insertions(+), 32 deletions(-) diff --git a/combine-diff.c b/combine-diff.c index 6d783054d5..9aa099b061 100644 --- a/combine-diff.c +++ b/combine-diff.c @@ -618,8 +618,8 @@ static void reuse_combine_diff(struct sline *sline, unsigned long cnt, sline->p_lno[i] = sline->p_lno[j]; } -int show_combined_diff(struct combine_diff_path *elem, int num_parent, - int dense, const char *header) +static int show_patch_diff(struct combine_diff_path *elem, int num_parent, + int dense, const char *header) { unsigned long size, cnt, lno; char *result, *cp, *ep; @@ -791,32 +791,69 @@ static void show_raw_diff(struct combine_diff_path *p, int num_parent, const cha if (header) puts(header); - offset = strlen(COLONS) - num_parent; - if (offset < 0) - offset = 0; - prefix = COLONS + offset; - /* Show the modes */ for (i = 0; i < num_parent; i++) { - int mode = p->parent[i].mode; - if (mode) + if (p->parent[i].mode) mod_type = 'M'; - printf("%s%06o", prefix, mode); - prefix = " "; } - printf("%s%06o", prefix, p->mode); if (!p->mode) mod_type = 'D'; - /* Show sha1's */ - for (i = 0; i < num_parent; i++) { - printf("%s%s", prefix, diff_unique_abbrev(p->parent[i].sha1, opt->abbrev)); - prefix = " "; + if (opt->output_format == DIFF_FORMAT_RAW) { + offset = strlen(COLONS) - num_parent; + if (offset < 0) + offset = 0; + prefix = COLONS + offset; + + /* Show the modes */ + for (i = 0; i < num_parent; i++) { + printf("%s%06o", prefix, p->parent[i].mode); + prefix = " "; + } + printf("%s%06o", prefix, p->mode); + + /* Show sha1's */ + for (i = 0; i < num_parent; i++) + printf(" %s", diff_unique_abbrev(p->parent[i].sha1, + opt->abbrev)); + printf(" %s ", diff_unique_abbrev(p->sha1, opt->abbrev)); + } + + if (opt->output_format == DIFF_FORMAT_RAW || + opt->output_format == DIFF_FORMAT_NAME_STATUS) + printf("%c%c", mod_type, inter_name_termination); + + if (line_termination) { + if (quote_c_style(p->path, NULL, NULL, 0)) + quote_c_style(p->path, NULL, stdout, 0); + else + printf("%s", p->path); + putchar(line_termination); + } + else { + printf("%s%c", p->path, line_termination); } - printf("%s%s", prefix, diff_unique_abbrev(p->sha1, opt->abbrev)); +} + +int show_combined_diff(struct combine_diff_path *p, + int num_parent, + int dense, + const char *header, + struct diff_options *opt) +{ + if (!p->len) + return 0; + switch (opt->output_format) { + case DIFF_FORMAT_RAW: + case DIFF_FORMAT_NAME_STATUS: + case DIFF_FORMAT_NAME: + show_raw_diff(p, num_parent, header, opt); + return 1; - /* Modification type, terminations, filename */ - printf(" %c%c%s%c", mod_type, inter_name_termination, p->path, line_termination); + default: + case DIFF_FORMAT_PATCH: + return show_patch_diff(p, num_parent, dense, header); + } } const char *diff_tree_combined_merge(const unsigned char *sha1, @@ -858,14 +895,8 @@ const char *diff_tree_combined_merge(const unsigned char *sha1, } if (num_paths) { for (p = paths; p; p = p->next) { - if (!p->len) - continue; - if (opt->output_format == DIFF_FORMAT_RAW) { - show_raw_diff(p, num_parent, header, opt); - header = NULL; - continue; - } - if (show_combined_diff(p, num_parent, dense, header)) + if (show_combined_diff(p, num_parent, dense, + header, opt)) header = NULL; } } diff --git a/diff-files.c b/diff-files.c index d24d11c28d..7db5ce6407 100644 --- a/diff-files.c +++ b/diff-files.c @@ -88,9 +88,8 @@ int main(int argc, const char **argv) } argv++; argc--; } - if (combine_merges) { + if (dense_combined_merges) diff_options.output_format = DIFF_FORMAT_PATCH; - } /* Find the directory, and set up the pathspec */ pathspec = get_pathspec(prefix, argv + 1); @@ -166,7 +165,8 @@ int main(int argc, const char **argv) if (combine_merges && num_compare_stages == 2) { show_combined_diff(&combine.p, 2, dense_combined_merges, - NULL); + NULL, + &diff_options); free(combine.p.path); continue; } diff --git a/diff-tree.c b/diff-tree.c index df6fd97fca..b170b03fd3 100644 --- a/diff-tree.c +++ b/diff-tree.c @@ -248,7 +248,7 @@ int main(int argc, const char **argv) continue; } if (!strcmp(arg, "-m")) { - ignore_merges = 0; + combine_merges = ignore_merges = 0; continue; } if (!strcmp(arg, "-c")) { diff --git a/diff.h b/diff.h index 9088519af0..946a4067cc 100644 --- a/diff.h +++ b/diff.h @@ -75,7 +75,8 @@ struct combine_diff_path { sizeof(struct combine_diff_parent) * (n) + (l) + 1) extern int show_combined_diff(struct combine_diff_path *elem, int num_parent, - int dense, const char *header); + int dense, const char *header, + struct diff_options *); extern const char *diff_tree_combined_merge(const unsigned char *sha1, const char *, int, struct diff_options *opt); From 297a1aadbe22c978dca60d6512e5c851ebc6688c Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Fri, 10 Feb 2006 01:51:12 -0800 Subject: [PATCH 4/5] find_unique_abbrev() simplification. Earlier it did not grok the 0{40} SHA1 very well, but what it needed to do was to find the shortest 0{N} that is not used as a valid object name to be consistent with the way names of valid objects are abbreviated. This makes some users simpler. Signed-off-by: Junio C Hamano --- combine-diff.c | 15 +++------------ diff.c | 14 +++----------- sha1_name.c | 6 ++++-- 3 files changed, 10 insertions(+), 25 deletions(-) diff --git a/combine-diff.c b/combine-diff.c index 9aa099b061..8ba6949203 100644 --- a/combine-diff.c +++ b/combine-diff.c @@ -716,10 +716,7 @@ static int show_patch_diff(struct combine_diff_path *elem, int num_parent, if (show_hunks || mode_differs) { const char *abb; - char null_abb[DEFAULT_ABBREV + 1]; - memset(null_abb, '0', DEFAULT_ABBREV); - null_abb[DEFAULT_ABBREV] = 0; if (header) { shown_header++; puts(header); @@ -734,17 +731,11 @@ static int show_patch_diff(struct combine_diff_path *elem, int num_parent, for (i = 0; i < num_parent; i++) { if (elem->parent[i].mode != elem->mode) mode_differs = 1; - if (memcmp(elem->parent[i].sha1, null_sha1, 20)) - abb = find_unique_abbrev(elem->parent[i].sha1, - DEFAULT_ABBREV); - else - abb = null_abb; + abb = find_unique_abbrev(elem->parent[i].sha1, + DEFAULT_ABBREV); printf("%s%s", i ? "," : "", abb); } - if (memcmp(elem->sha1, null_sha1, 20)) - abb = find_unique_abbrev(elem->sha1, DEFAULT_ABBREV); - else - abb = null_abb; + abb = find_unique_abbrev(elem->sha1, DEFAULT_ABBREV); printf("..%s\n", abb); if (mode_differs) { diff --git a/diff.c b/diff.c index ec51e7dd18..890bdaa6b8 100644 --- a/diff.c +++ b/diff.c @@ -963,7 +963,7 @@ void diff_free_filepair(struct diff_filepair *p) } /* This is different from find_unique_abbrev() in that - * it needs to deal with 0{40} SHA1. + * it stuffs the result with dots for alignment. */ const char *diff_unique_abbrev(const unsigned char *sha1, int len) { @@ -973,16 +973,8 @@ const char *diff_unique_abbrev(const unsigned char *sha1, int len) return sha1_to_hex(sha1); abbrev = find_unique_abbrev(sha1, len); - if (!abbrev) { - if (!memcmp(sha1, null_sha1, 20)) { - char *buf = sha1_to_hex(null_sha1); - if (len < 37) - strcpy(buf + len, "..."); - return buf; - } - else - return sha1_to_hex(sha1); - } + if (!abbrev) + return sha1_to_hex(sha1); abblen = strlen(abbrev); if (abblen < 37) { static char hex[41]; diff --git a/sha1_name.c b/sha1_name.c index fa85d8a88d..d67de18ba5 100644 --- a/sha1_name.c +++ b/sha1_name.c @@ -186,16 +186,18 @@ static int get_short_sha1(const char *name, int len, unsigned char *sha1, const char *find_unique_abbrev(const unsigned char *sha1, int len) { - int status; + int status, is_null; static char hex[41]; + is_null = !memcmp(sha1, null_sha1, 20); memcpy(hex, sha1_to_hex(sha1), 40); if (len == 40) return hex; while (len < 40) { unsigned char sha1_ret[20]; status = get_short_sha1(hex, len, sha1_ret, 1); - if (!status) { + if (!status || + (is_null && status != SHORT_NAME_AMBIGUOUS)) { hex[len] = 0; return hex; } From d416df8869d803282fb254a18505eccceee9ba3e Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Fri, 10 Feb 2006 02:30:52 -0800 Subject: [PATCH 5/5] combine-diff: Record diff status a bit more faithfully This shows "new file mode XXXX" and "deleted file mode XXXX" lines like two-way diff-patch output does, by checking the status from each parent. The diff-raw output for combined diff is made a bit uglier by showing diff status letters with each parent. While most of the case you would see "MM" in the output, an Evil Merge that touches a path that was added by inheriting from one parent is possible and it would be shown like these: $ git-diff-tree --abbrev -c HEAD 2d7ca89675eb8888b0b88a91102f096d4471f09f ::000000 000000 100644 0000000... 0000000... 31dd686... AA b ::000000 100644 100644 0000000... 6c884ae... c6d4fa8... AM d ::100644 100644 100644 4f7cbe7... f8c295c... 19d5d80... RR e Signed-off-by: Junio C Hamano --- combine-diff.c | 32 +++++++++++++++++++++++++------- diff.h | 1 + 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/combine-diff.c b/combine-diff.c index 8ba6949203..a38f01b13c 100644 --- a/combine-diff.c +++ b/combine-diff.c @@ -39,6 +39,7 @@ static struct combine_diff_path *intersect_paths(struct combine_diff_path *curr, p->mode = q->queue[i]->two->mode; memcpy(p->parent[n].sha1, q->queue[i]->one->sha1, 20); p->parent[n].mode = q->queue[i]->one->mode; + p->parent[n].status = q->queue[i]->status; *tail = p; tail = &p->next; } @@ -62,6 +63,7 @@ static struct combine_diff_path *intersect_paths(struct combine_diff_path *curr, memcpy(p->parent[n].sha1, q->queue[i]->one->sha1, 20); p->parent[n].mode = q->queue[i]->one->mode; + p->parent[n].status = q->queue[i]->status; break; } } @@ -739,12 +741,25 @@ static int show_patch_diff(struct combine_diff_path *elem, int num_parent, printf("..%s\n", abb); if (mode_differs) { - printf("mode "); - for (i = 0; i < num_parent; i++) { - printf("%s%06o", i ? "," : "", - elem->parent[i].mode); + int added = !!elem->mode; + for (i = 0; added && i < num_parent; i++) + if (elem->parent[i].status != + DIFF_STATUS_ADDED) + added = 0; + if (added) + printf("new file mode %06o", elem->mode); + else { + if (!elem->mode) + printf("deleted file "); + printf("mode "); + for (i = 0; i < num_parent; i++) { + printf("%s%06o", i ? "," : "", + elem->parent[i].mode); + } + if (elem->mode) + printf("..%06o", elem->mode); } - printf("..%06o\n", elem->mode); + putchar('\n'); } dump_sline(sline, cnt, num_parent); } @@ -811,8 +826,11 @@ static void show_raw_diff(struct combine_diff_path *p, int num_parent, const cha } if (opt->output_format == DIFF_FORMAT_RAW || - opt->output_format == DIFF_FORMAT_NAME_STATUS) - printf("%c%c", mod_type, inter_name_termination); + opt->output_format == DIFF_FORMAT_NAME_STATUS) { + for (i = 0; i < num_parent; i++) + putchar(p->parent[i].status); + putchar(inter_name_termination); + } if (line_termination) { if (quote_c_style(p->path, NULL, NULL, 0)) diff --git a/diff.h b/diff.h index 946a4067cc..8fac465a9d 100644 --- a/diff.h +++ b/diff.h @@ -66,6 +66,7 @@ struct combine_diff_path { unsigned int mode; unsigned char sha1[20]; struct combine_diff_parent { + char status; unsigned int mode; unsigned char sha1[20]; } parent[FLEX_ARRAY];