diff-tree -c: show a merge commit a bit more sensibly.
[git.git] / diff-tree.c
blob0c689360ee5bf40eda7d1241ebcffcfce0b8e0db
1 #include "cache.h"
2 #include "diff.h"
3 #include "commit.h"
5 static int show_root_diff = 0;
6 static int no_commit_id = 0;
7 static int verbose_header = 0;
8 static int ignore_merges = 1;
9 static int show_empty_combined = 0;
10 static int combine_merges = 0;
11 static int read_stdin = 0;
13 static const char *header = NULL;
14 static const char *header_prefix = "";
15 static enum cmit_fmt commit_format = CMIT_FMT_RAW;
17 static struct diff_options diff_options;
19 static int call_diff_flush(void)
21 diffcore_std(&diff_options);
22 if (diff_queue_is_empty()) {
23 int saved_fmt = diff_options.output_format;
24 diff_options.output_format = DIFF_FORMAT_NO_OUTPUT;
25 diff_flush(&diff_options);
26 diff_options.output_format = saved_fmt;
27 return 0;
29 if (header) {
30 if (!no_commit_id)
31 printf("%s%c", header, diff_options.line_termination);
32 header = NULL;
34 diff_flush(&diff_options);
35 return 1;
38 static int diff_tree_sha1_top(const unsigned char *old,
39 const unsigned char *new, const char *base)
41 int ret;
43 ret = diff_tree_sha1(old, new, base, &diff_options);
44 call_diff_flush();
45 return ret;
48 static int diff_root_tree(const unsigned char *new, const char *base)
50 int retval;
51 void *tree;
52 struct tree_desc empty, real;
54 tree = read_object_with_reference(new, "tree", &real.size, NULL);
55 if (!tree)
56 die("unable to read root tree (%s)", sha1_to_hex(new));
57 real.buf = tree;
59 empty.buf = "";
60 empty.size = 0;
61 retval = diff_tree(&empty, &real, base, &diff_options);
62 free(tree);
63 call_diff_flush();
64 return retval;
67 static const char *generate_header(const unsigned char *commit_sha1,
68 const unsigned char *parent_sha1,
69 const char *msg)
71 static char this_header[16384];
72 int offset;
73 unsigned long len;
74 int abbrev = diff_options.abbrev;
76 if (!verbose_header)
77 return sha1_to_hex(commit_sha1);
79 len = strlen(msg);
81 offset = sprintf(this_header, "%s%s ",
82 header_prefix,
83 diff_unique_abbrev(commit_sha1, abbrev));
84 if (commit_sha1 != parent_sha1)
85 offset += sprintf(this_header + offset, "(from %s)\n",
86 parent_sha1
87 ? diff_unique_abbrev(parent_sha1, abbrev)
88 : "root");
89 else
90 offset += sprintf(this_header + offset, "(from parents)\n");
91 offset += pretty_print_commit(commit_format, msg, len,
92 this_header + offset,
93 sizeof(this_header) - offset);
94 return this_header;
97 static int diff_tree_commit(const unsigned char *commit_sha1)
99 struct commit *commit;
100 struct commit_list *parents;
101 char name[50];
102 unsigned char sha1[20];
104 sprintf(name, "%s^0", sha1_to_hex(commit_sha1));
105 if (get_sha1(name, sha1))
106 return -1;
107 name[40] = 0;
108 commit = lookup_commit(sha1);
110 /* Root commit? */
111 if (show_root_diff && !commit->parents) {
112 header = generate_header(sha1, NULL, commit->buffer);
113 diff_root_tree(commit_sha1, "");
116 /* More than one parent? */
117 if (commit->parents && commit->parents->next) {
118 if (ignore_merges)
119 return 0;
120 else if (combine_merges) {
121 header = generate_header(sha1, sha1,
122 commit->buffer);
123 return diff_tree_combined_merge(sha1, header,
124 show_empty_combined);
128 for (parents = commit->parents; parents; parents = parents->next) {
129 struct commit *parent = parents->item;
130 header = generate_header(sha1,
131 parent->object.sha1,
132 commit->buffer);
133 diff_tree_sha1_top(parent->object.sha1, commit_sha1, "");
134 if (!header && verbose_header) {
135 header_prefix = "\ndiff-tree ";
137 * Don't print multiple merge entries if we
138 * don't print the diffs.
142 return 0;
145 static int diff_tree_stdin(char *line)
147 int len = strlen(line);
148 unsigned char commit[20], parent[20];
149 static char this_header[1000];
150 int abbrev = diff_options.abbrev;
152 if (!len || line[len-1] != '\n')
153 return -1;
154 line[len-1] = 0;
155 if (get_sha1_hex(line, commit))
156 return -1;
157 if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
158 line[40] = 0;
159 line[81] = 0;
160 sprintf(this_header, "%s (from %s)\n",
161 diff_unique_abbrev(commit, abbrev),
162 diff_unique_abbrev(parent, abbrev));
163 header = this_header;
164 return diff_tree_sha1_top(parent, commit, "");
166 line[40] = 0;
167 return diff_tree_commit(commit);
170 static const char diff_tree_usage[] =
171 "git-diff-tree [--stdin] [-m] [-c] [-s] [-v] [--pretty] [-t] [-r] [--root] "
172 "[<common diff options>] <tree-ish> [<tree-ish>] [<path>...]\n"
173 " -r diff recursively\n"
174 " --root include the initial commit as diff against /dev/null\n"
175 COMMON_DIFF_OPTIONS_HELP;
177 int main(int argc, const char **argv)
179 int nr_sha1;
180 char line[1000];
181 unsigned char sha1[2][20];
182 const char *prefix = setup_git_directory();
184 git_config(git_diff_config);
185 nr_sha1 = 0;
186 diff_setup(&diff_options);
188 for (;;) {
189 int diff_opt_cnt;
190 const char *arg;
192 argv++;
193 argc--;
194 arg = *argv;
195 if (!arg)
196 break;
198 if (*arg != '-') {
199 if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
200 nr_sha1++;
201 continue;
203 break;
206 diff_opt_cnt = diff_opt_parse(&diff_options, argv, argc);
207 if (diff_opt_cnt < 0)
208 usage(diff_tree_usage);
209 else if (diff_opt_cnt) {
210 argv += diff_opt_cnt - 1;
211 argc -= diff_opt_cnt - 1;
212 continue;
216 if (!strcmp(arg, "--")) {
217 argv++;
218 argc--;
219 break;
221 if (!strcmp(arg, "-r")) {
222 diff_options.recursive = 1;
223 continue;
225 if (!strcmp(arg, "-t")) {
226 diff_options.recursive = 1;
227 diff_options.tree_in_recursive = 1;
228 continue;
230 if (!strcmp(arg, "-m")) {
231 ignore_merges = 0;
232 continue;
234 if (!strcmp(arg, "-c")) {
235 combine_merges = 1;
236 continue;
238 if (!strcmp(arg, "-v")) {
239 verbose_header = 1;
240 header_prefix = "diff-tree ";
241 continue;
243 if (!strncmp(arg, "--pretty", 8)) {
244 verbose_header = 1;
245 header_prefix = "diff-tree ";
246 commit_format = get_commit_format(arg+8);
247 continue;
249 if (!strcmp(arg, "--stdin")) {
250 read_stdin = 1;
251 continue;
253 if (!strcmp(arg, "--root")) {
254 show_root_diff = 1;
255 continue;
257 if (!strcmp(arg, "--no-commit-id")) {
258 no_commit_id = 1;
259 continue;
261 usage(diff_tree_usage);
263 if (diff_options.output_format == DIFF_FORMAT_PATCH)
264 diff_options.recursive = 1;
266 if (combine_merges) {
267 diff_options.output_format = DIFF_FORMAT_PATCH;
268 show_empty_combined = !ignore_merges;
269 ignore_merges = 0;
272 diff_tree_setup_paths(get_pathspec(prefix, argv));
273 diff_setup_done(&diff_options);
275 switch (nr_sha1) {
276 case 0:
277 if (!read_stdin)
278 usage(diff_tree_usage);
279 break;
280 case 1:
281 diff_tree_commit(sha1[0]);
282 break;
283 case 2:
284 diff_tree_sha1_top(sha1[0], sha1[1], "");
285 break;
288 if (!read_stdin)
289 return 0;
291 if (diff_options.detect_rename)
292 diff_options.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
293 DIFF_SETUP_USE_CACHE);
294 while (fgets(line, sizeof(line), stdin))
295 diff_tree_stdin(line);
297 return 0;