diff-tree -c: show a merge commit a bit more sensibly.
[git.git] / diff-tree.c
1 #include "cache.h"
2 #include "diff.h"
3 #include "commit.h"
4
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;
12
13 static const char *header = NULL;
14 static const char *header_prefix = "";
15 static enum cmit_fmt commit_format = CMIT_FMT_RAW;
16
17 static struct diff_options diff_options;
18
19 static int call_diff_flush(void)
20 {
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;
28         }
29         if (header) {
30                 if (!no_commit_id)
31                         printf("%s%c", header, diff_options.line_termination);
32                 header = NULL;
33         }
34         diff_flush(&diff_options);
35         return 1;
36 }
37
38 static int diff_tree_sha1_top(const unsigned char *old,
39                               const unsigned char *new, const char *base)
40 {
41         int ret;
42
43         ret = diff_tree_sha1(old, new, base, &diff_options);
44         call_diff_flush();
45         return ret;
46 }
47
48 static int diff_root_tree(const unsigned char *new, const char *base)
49 {
50         int retval;
51         void *tree;
52         struct tree_desc empty, real;
53
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;
58
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;
65 }
66
67 static const char *generate_header(const unsigned char *commit_sha1,
68                                    const unsigned char *parent_sha1,
69                                    const char *msg)
70 {
71         static char this_header[16384];
72         int offset;
73         unsigned long len;
74         int abbrev = diff_options.abbrev;
75
76         if (!verbose_header)
77                 return sha1_to_hex(commit_sha1);
78
79         len = strlen(msg);
80
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;
95 }
96
97 static int diff_tree_commit(const unsigned char *commit_sha1)
98 {
99         struct commit *commit;
100         struct commit_list *parents;
101         char name[50];
102         unsigned char sha1[20];
103
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);
109         
110         /* Root commit? */
111         if (show_root_diff && !commit->parents) {
112                 header = generate_header(sha1, NULL, commit->buffer);
113                 diff_root_tree(commit_sha1, "");
114         }
115
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);
125                 }
126         }
127
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 ";
136                         /*
137                          * Don't print multiple merge entries if we
138                          * don't print the diffs.
139                          */
140                 }
141         }
142         return 0;
143 }
144
145 static int diff_tree_stdin(char *line)
146 {
147         int len = strlen(line);
148         unsigned char commit[20], parent[20];
149         static char this_header[1000];
150         int abbrev = diff_options.abbrev;
151
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, "");
165         }
166         line[40] = 0;
167         return diff_tree_commit(commit);
168 }
169
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;
176
177 int main(int argc, const char **argv)
178 {
179         int nr_sha1;
180         char line[1000];
181         unsigned char sha1[2][20];
182         const char *prefix = setup_git_directory();
183
184         git_config(git_diff_config);
185         nr_sha1 = 0;
186         diff_setup(&diff_options);
187
188         for (;;) {
189                 int diff_opt_cnt;
190                 const char *arg;
191
192                 argv++;
193                 argc--;
194                 arg = *argv;
195                 if (!arg)
196                         break;
197
198                 if (*arg != '-') {
199                         if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
200                                 nr_sha1++;
201                                 continue;
202                         }
203                         break;
204                 }
205
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;
213                 }
214
215
216                 if (!strcmp(arg, "--")) {
217                         argv++;
218                         argc--;
219                         break;
220                 }
221                 if (!strcmp(arg, "-r")) {
222                         diff_options.recursive = 1;
223                         continue;
224                 }
225                 if (!strcmp(arg, "-t")) {
226                         diff_options.recursive = 1;
227                         diff_options.tree_in_recursive = 1;
228                         continue;
229                 }
230                 if (!strcmp(arg, "-m")) {
231                         ignore_merges = 0;
232                         continue;
233                 }
234                 if (!strcmp(arg, "-c")) {
235                         combine_merges = 1;
236                         continue;
237                 }
238                 if (!strcmp(arg, "-v")) {
239                         verbose_header = 1;
240                         header_prefix = "diff-tree ";
241                         continue;
242                 }
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;
248                 }
249                 if (!strcmp(arg, "--stdin")) {
250                         read_stdin = 1;
251                         continue;
252                 }
253                 if (!strcmp(arg, "--root")) {
254                         show_root_diff = 1;
255                         continue;
256                 }
257                 if (!strcmp(arg, "--no-commit-id")) {
258                         no_commit_id = 1;
259                         continue;
260                 }
261                 usage(diff_tree_usage);
262         }
263         if (diff_options.output_format == DIFF_FORMAT_PATCH)
264                 diff_options.recursive = 1;
265
266         if (combine_merges) {
267                 diff_options.output_format = DIFF_FORMAT_PATCH;
268                 show_empty_combined = !ignore_merges;
269                 ignore_merges = 0;
270         }
271
272         diff_tree_setup_paths(get_pathspec(prefix, argv));
273         diff_setup_done(&diff_options);
274
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;
286         }
287
288         if (!read_stdin)
289                 return 0;
290
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);
296
297         return 0;
298 }