combine-diff: remove misguided --show-empty hack.
[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 combine_merges = 0;
10 static int dense_combined_merges = 0;
11 static int read_stdin = 0;
12 static int always_show_header = 0;
13
14 static const char *header = NULL;
15 static const char *header_prefix = "";
16 static enum cmit_fmt commit_format = CMIT_FMT_RAW;
17
18 static struct diff_options diff_options;
19
20 static int call_diff_flush(void)
21 {
22         diffcore_std(&diff_options);
23         if (diff_queue_is_empty()) {
24                 int saved_fmt = diff_options.output_format;
25                 diff_options.output_format = DIFF_FORMAT_NO_OUTPUT;
26                 diff_flush(&diff_options);
27                 diff_options.output_format = saved_fmt;
28                 return 0;
29         }
30         if (header) {
31                 if (!no_commit_id)
32                         printf("%s%c", header, diff_options.line_termination);
33                 header = NULL;
34         }
35         diff_flush(&diff_options);
36         return 1;
37 }
38
39 static int diff_tree_sha1_top(const unsigned char *old,
40                               const unsigned char *new, const char *base)
41 {
42         int ret;
43
44         ret = diff_tree_sha1(old, new, base, &diff_options);
45         call_diff_flush();
46         return ret;
47 }
48
49 static int diff_root_tree(const unsigned char *new, const char *base)
50 {
51         int retval;
52         void *tree;
53         struct tree_desc empty, real;
54
55         tree = read_object_with_reference(new, "tree", &real.size, NULL);
56         if (!tree)
57                 die("unable to read root tree (%s)", sha1_to_hex(new));
58         real.buf = tree;
59
60         empty.buf = "";
61         empty.size = 0;
62         retval = diff_tree(&empty, &real, base, &diff_options);
63         free(tree);
64         call_diff_flush();
65         return retval;
66 }
67
68 static const char *generate_header(const unsigned char *commit_sha1,
69                                    const unsigned char *parent_sha1,
70                                    const struct commit *commit)
71 {
72         static char this_header[16384];
73         int offset;
74         unsigned long len;
75         int abbrev = diff_options.abbrev;
76         const char *msg = commit->buffer;
77
78         if (!verbose_header)
79                 return sha1_to_hex(commit_sha1);
80
81         len = strlen(msg);
82
83         offset = sprintf(this_header, "%s%s ",
84                          header_prefix,
85                          diff_unique_abbrev(commit_sha1, abbrev));
86         if (commit_sha1 != parent_sha1)
87                 offset += sprintf(this_header + offset, "(from %s)\n",
88                                   parent_sha1
89                                   ? diff_unique_abbrev(parent_sha1, abbrev)
90                                   : "root");
91         else
92                 offset += sprintf(this_header + offset, "(from parents)\n");
93         offset += pretty_print_commit(commit_format, commit, len,
94                                       this_header + offset,
95                                       sizeof(this_header) - offset, abbrev);
96         if (always_show_header) {
97                 puts(this_header);
98                 return NULL;
99         }
100         return this_header;
101 }
102
103 static int diff_tree_commit(const unsigned char *commit_sha1)
104 {
105         struct commit *commit;
106         struct commit_list *parents;
107         char name[50];
108         unsigned char sha1[20];
109
110         sprintf(name, "%s^0", sha1_to_hex(commit_sha1));
111         if (get_sha1(name, sha1))
112                 return -1;
113         name[40] = 0;
114         commit = lookup_commit(sha1);
115         
116         /* Root commit? */
117         if (show_root_diff && !commit->parents) {
118                 header = generate_header(sha1, NULL, commit);
119                 diff_root_tree(commit_sha1, "");
120         }
121
122         /* More than one parent? */
123         if (commit->parents && commit->parents->next) {
124                 if (ignore_merges)
125                         return 0;
126                 else if (combine_merges) {
127                         header = generate_header(sha1, sha1, commit);
128                         return diff_tree_combined_merge(sha1, header,
129                                                         dense_combined_merges);
130                 }
131         }
132
133         for (parents = commit->parents; parents; parents = parents->next) {
134                 struct commit *parent = parents->item;
135                 header = generate_header(sha1, parent->object.sha1, commit);
136                 diff_tree_sha1_top(parent->object.sha1, commit_sha1, "");
137                 if (!header && verbose_header) {
138                         header_prefix = "\ndiff-tree ";
139                         /*
140                          * Don't print multiple merge entries if we
141                          * don't print the diffs.
142                          */
143                 }
144         }
145         return 0;
146 }
147
148 static int diff_tree_stdin(char *line)
149 {
150         int len = strlen(line);
151         unsigned char commit[20], parent[20];
152         static char this_header[1000];
153         int abbrev = diff_options.abbrev;
154
155         if (!len || line[len-1] != '\n')
156                 return -1;
157         line[len-1] = 0;
158         if (get_sha1_hex(line, commit))
159                 return -1;
160         if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
161                 line[40] = 0;
162                 line[81] = 0;
163                 sprintf(this_header, "%s (from %s)\n",
164                         diff_unique_abbrev(commit, abbrev),
165                         diff_unique_abbrev(parent, abbrev));
166                 header = this_header;
167                 return diff_tree_sha1_top(parent, commit, "");
168         }
169         line[40] = 0;
170         return diff_tree_commit(commit);
171 }
172
173 static const char diff_tree_usage[] =
174 "git-diff-tree [--stdin] [-m] [-c] [--cc] [-s] [-v] [--pretty] [-t] [-r] [--root] "
175 "[<common diff options>] <tree-ish> [<tree-ish>] [<path>...]\n"
176 "  -r            diff recursively\n"
177 "  --root        include the initial commit as diff against /dev/null\n"
178 COMMON_DIFF_OPTIONS_HELP;
179
180 int main(int argc, const char **argv)
181 {
182         int nr_sha1;
183         char line[1000];
184         unsigned char sha1[2][20];
185         const char *prefix = setup_git_directory();
186
187         git_config(git_diff_config);
188         nr_sha1 = 0;
189         diff_setup(&diff_options);
190
191         for (;;) {
192                 int diff_opt_cnt;
193                 const char *arg;
194
195                 argv++;
196                 argc--;
197                 arg = *argv;
198                 if (!arg)
199                         break;
200
201                 if (*arg != '-') {
202                         if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
203                                 nr_sha1++;
204                                 continue;
205                         }
206                         break;
207                 }
208
209                 diff_opt_cnt = diff_opt_parse(&diff_options, argv, argc);
210                 if (diff_opt_cnt < 0)
211                         usage(diff_tree_usage);
212                 else if (diff_opt_cnt) {
213                         argv += diff_opt_cnt - 1;
214                         argc -= diff_opt_cnt - 1;
215                         continue;
216                 }
217
218
219                 if (!strcmp(arg, "--")) {
220                         argv++;
221                         argc--;
222                         break;
223                 }
224                 if (!strcmp(arg, "-r")) {
225                         diff_options.recursive = 1;
226                         continue;
227                 }
228                 if (!strcmp(arg, "-t")) {
229                         diff_options.recursive = 1;
230                         diff_options.tree_in_recursive = 1;
231                         continue;
232                 }
233                 if (!strcmp(arg, "-m")) {
234                         ignore_merges = 0;
235                         continue;
236                 }
237                 if (!strcmp(arg, "-c")) {
238                         combine_merges = 1;
239                         continue;
240                 }
241                 if (!strcmp(arg, "--cc")) {
242                         dense_combined_merges = combine_merges = 1;
243                         continue;
244                 }
245                 if (!strcmp(arg, "-v")) {
246                         verbose_header = 1;
247                         header_prefix = "diff-tree ";
248                         continue;
249                 }
250                 if (!strncmp(arg, "--pretty", 8)) {
251                         verbose_header = 1;
252                         header_prefix = "diff-tree ";
253                         commit_format = get_commit_format(arg+8);
254                         continue;
255                 }
256                 if (!strcmp(arg, "--stdin")) {
257                         read_stdin = 1;
258                         continue;
259                 }
260                 if (!strcmp(arg, "--root")) {
261                         show_root_diff = 1;
262                         continue;
263                 }
264                 if (!strcmp(arg, "--no-commit-id")) {
265                         no_commit_id = 1;
266                         continue;
267                 }
268                 if (!strcmp(arg, "--always")) {
269                         always_show_header = 1;
270                         continue;
271                 }
272                 usage(diff_tree_usage);
273         }
274         if (diff_options.output_format == DIFF_FORMAT_PATCH)
275                 diff_options.recursive = 1;
276
277         if (combine_merges) {
278                 diff_options.output_format = DIFF_FORMAT_PATCH;
279                 ignore_merges = 0;
280         }
281
282         diff_tree_setup_paths(get_pathspec(prefix, argv));
283         diff_setup_done(&diff_options);
284
285         switch (nr_sha1) {
286         case 0:
287                 if (!read_stdin)
288                         usage(diff_tree_usage);
289                 break;
290         case 1:
291                 diff_tree_commit(sha1[0]);
292                 break;
293         case 2:
294                 diff_tree_sha1_top(sha1[0], sha1[1], "");
295                 break;
296         }
297
298         if (!read_stdin)
299                 return 0;
300
301         if (diff_options.detect_rename)
302                 diff_options.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
303                                        DIFF_SETUP_USE_CACHE);
304         while (fgets(line, sizeof(line), stdin))
305                 diff_tree_stdin(line);
306
307         return 0;
308 }