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

diff-tree: teach single-commit diff-tree to honour grafts

We used to read the commit objects by hand and ignored the grafts. Rewrite it using lookup_commit() API, to make it grafts-aware. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Nov 21, 2005 at 21:49 UTC a50b870aa18738ec10ed31fd4a32a284ee1cfe50
1 file changed +25 -29
diff-tree.c
+25 -29
@@ -69,52 +69,50 @@ static int diff_root_tree(const unsigned char *new, const char *base)
69 return retval;
70 }
71
72 -static const char *generate_header(const char *commit, const char *parent, const char *msg, unsigned long len)
72 +static const char *generate_header(const char *commit, const char *parent, const char *msg)
73 {
74 static char this_header[16384];
75 int offset;
76 + unsigned long len;
77
78 if (!verbose_header)
79 return commit;
80
81 + len = strlen(msg);
82 offset = sprintf(this_header, "%s%s (from %s)\n", header_prefix, commit, parent);
83 offset += pretty_print_commit(commit_format, msg, len, this_header + offset, sizeof(this_header) - offset);
84 return this_header;
85 }
86
85 -static int diff_tree_commit(const unsigned char *commit, const char *name)
87 +static int diff_tree_commit(const unsigned char *commit_sha1)
88 {
87 - unsigned long size, offset;
88 - char *buf = read_object_with_reference(commit, "commit", &size, NULL);
89 + struct commit *commit;
90 + struct commit_list *parents;
91 + char name[50];
92 + unsigned char sha1[20];
93
90 - if (!buf)
94 + sprintf(name, "%s^0", sha1_to_hex(commit_sha1));
95 + if (get_sha1(name, sha1))
96 return -1;
92 -
93 - if (!name) {
94 - static char commit_name[60];
95 - strcpy(commit_name, sha1_to_hex(commit));
96 - name = commit_name;
97 - }
98 -
97 + name[40] = 0;
98 + commit = lookup_commit(sha1);
99 +
100 /* Root commit? */
100 - if (show_root_diff && memcmp(buf + 46, "parent ", 7)) {
101 - header = generate_header(name, "root", buf, size);
102 - diff_root_tree(commit, "");
101 + if (show_root_diff && !commit->parents) {
102 + header = generate_header(name, "root", commit->buffer);
103 + diff_root_tree(commit_sha1, "");
104 }
105
106 /* More than one parent? */
106 - if (ignore_merges) {
107 - if (!memcmp(buf + 46 + 48, "parent ", 7))
107 + if (ignore_merges && commit->parents && commit->parents->next)
108 return 0;
109 - }
109
111 - offset = 46;
112 - while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) {
113 - unsigned char parent[20];
114 - if (get_sha1_hex(buf + offset + 7, parent))
115 - return -1;
116 - header = generate_header(name, sha1_to_hex(parent), buf, size);
117 - diff_tree_sha1_top(parent, commit, "");
110 + for (parents = commit->parents; parents; parents = parents->next) {
111 + struct commit *parent = parents->item;
112 + header = generate_header(name,
113 + sha1_to_hex(parent->object.sha1),
114 + commit->buffer);
115 + diff_tree_sha1_top(parent->object.sha1, commit_sha1, "");
116 if (!header && verbose_header) {
117 header_prefix = "\ndiff-tree ";
118 /*
@@ -122,9 +120,7 @@ static int diff_tree_commit(const unsigned char *commit, const char *name)
120 * don't print the diffs.
121 */
122 }
125 - offset += 48;
123 }
127 - free(buf);
124 return 0;
125 }
126
@@ -147,7 +143,7 @@ static int diff_tree_stdin(char *line)
143 return diff_tree_sha1_top(parent, commit, "");
144 }
145 line[40] = 0;
150 - return diff_tree_commit(commit, line);
146 + return diff_tree_commit(commit);
147 }
148
149 static const char diff_tree_usage[] =
@@ -250,7 +246,7 @@ int main(int argc, const char **argv)
246 usage(diff_tree_usage);
247 break;
248 case 1:
253 - diff_tree_commit(sha1[0], NULL);
249 + diff_tree_commit(sha1[0]);
250 break;
251 case 2:
252 diff_tree_sha1_top(sha1[0], sha1[1], "");