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