1#include "cache.h"
2#include "diff.h"
3#include "commit.h"
4
5static int show_root_diff = 0;
6static int no_commit_id = 0;
7static int verbose_header = 0;
8static int ignore_merges = 1;
9static int show_empty_combined = 0;
10static int combine_merges = 0;
11static int dense_combined_merges = 0;
12static int read_stdin = 0;
13
14static const char *header = NULL;
15static const char *header_prefix = "";
16static enum cmit_fmt commit_format = CMIT_FMT_RAW;
17
18static struct diff_options diff_options;
19
20static 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
39static 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
49static 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
68static 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 return this_header;
97}
98
99static int diff_tree_commit(const unsigned char *commit_sha1)
100{
101 struct commit *commit;
102 struct commit_list *parents;
103 char name[50];
104 unsigned char sha1[20];
105
106 sprintf(name, "%s^0", sha1_to_hex(commit_sha1));
107 if (get_sha1(name, sha1))
108 return -1;
109 name[40] = 0;
110 commit = lookup_commit(sha1);
111
112 /* Root commit? */
113 if (show_root_diff && !commit->parents) {
114 header = generate_header(sha1, NULL, commit);
115 diff_root_tree(commit_sha1, "");
116 }
117
118 /* More than one parent? */
119 if (commit->parents && commit->parents->next) {
120 if (ignore_merges)
121 return 0;
122 else if (combine_merges) {
123 header = generate_header(sha1, sha1, commit);
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, parent->object.sha1, commit);
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
145static 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
170static const char diff_tree_usage[] =
171"git-diff-tree [--stdin] [-m] [-c] [--cc] [-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"
175COMMON_DIFF_OPTIONS_HELP;
176
177int 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, "--cc")) {
239 dense_combined_merges = combine_merges = 1;
240 continue;
241 }
242 if (!strcmp(arg, "-v")) {
243 verbose_header = 1;
244 header_prefix = "diff-tree ";
245 continue;
246 }
247 if (!strncmp(arg, "--pretty", 8)) {
248 verbose_header = 1;
249 header_prefix = "diff-tree ";
250 commit_format = get_commit_format(arg+8);
251 continue;
252 }
253 if (!strcmp(arg, "--stdin")) {
254 read_stdin = 1;
255 continue;
256 }
257 if (!strcmp(arg, "--root")) {
258 show_root_diff = 1;
259 continue;
260 }
261 if (!strcmp(arg, "--no-commit-id")) {
262 no_commit_id = 1;
263 continue;
264 }
265 usage(diff_tree_usage);
266 }
267 if (diff_options.output_format == DIFF_FORMAT_PATCH)
268 diff_options.recursive = 1;
269
270 if (combine_merges) {
271 diff_options.output_format = DIFF_FORMAT_PATCH;
272 show_empty_combined = !ignore_merges;
273 ignore_merges = 0;
274 }
275
276 diff_tree_setup_paths(get_pathspec(prefix, argv));
277 diff_setup_done(&diff_options);
278
279 switch (nr_sha1) {
280 case 0:
281 if (!read_stdin)
282 usage(diff_tree_usage);
283 break;
284 case 1:
285 diff_tree_commit(sha1[0]);
286 break;
287 case 2:
288 diff_tree_sha1_top(sha1[0], sha1[1], "");
289 break;
290 }
291
292 if (!read_stdin)
293 return 0;
294
295 if (diff_options.detect_rename)
296 diff_options.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
297 DIFF_SETUP_USE_CACHE);
298 while (fgets(line, sizeof(line), stdin))
299 diff_tree_stdin(line);
300
301 return 0;
302}