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