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 static int always_show_header = 0;
15 static const char *header = NULL;
16 static const char *header_prefix = "";
17 static enum cmit_fmt commit_format = CMIT_FMT_RAW;
19 static struct diff_options diff_options;
21 static int call_diff_flush(void)
23 diffcore_std(&diff_options);
24 if (diff_queue_is_empty()) {
25 int saved_fmt = diff_options.output_format;
26 diff_options.output_format = DIFF_FORMAT_NO_OUTPUT;
27 diff_flush(&diff_options);
28 diff_options.output_format = saved_fmt;
33 printf("%s%c", header, diff_options.line_termination);
36 diff_flush(&diff_options);
40 static int diff_tree_sha1_top(const unsigned char *old,
41 const unsigned char *new, const char *base)
45 ret = diff_tree_sha1(old, new, base, &diff_options);
50 static int diff_root_tree(const unsigned char *new, const char *base)
54 struct tree_desc empty, real;
56 tree = read_object_with_reference(new, "tree", &real.size, NULL);
58 die("unable to read root tree (%s)", sha1_to_hex(new));
63 retval = diff_tree(&empty, &real, base, &diff_options);
69 static const char *generate_header(const unsigned char *commit_sha1,
70 const unsigned char *parent_sha1,
71 const struct commit *commit)
73 static char this_header[16384];
76 int abbrev = diff_options.abbrev;
77 const char *msg = commit->buffer;
80 return sha1_to_hex(commit_sha1);
84 offset = sprintf(this_header, "%s%s ",
86 diff_unique_abbrev(commit_sha1, abbrev));
87 if (commit_sha1 != parent_sha1)
88 offset += sprintf(this_header + offset, "(from %s)\n",
90 ? diff_unique_abbrev(parent_sha1, abbrev)
93 offset += sprintf(this_header + offset, "(from parents)\n");
94 offset += pretty_print_commit(commit_format, commit, len,
96 sizeof(this_header) - offset, abbrev);
97 if (always_show_header) {
104 static int diff_tree_commit(const unsigned char *commit_sha1)
106 struct commit *commit;
107 struct commit_list *parents;
109 unsigned char sha1[20];
111 sprintf(name, "%s^0", sha1_to_hex(commit_sha1));
112 if (get_sha1(name, sha1))
115 commit = lookup_commit(sha1);
118 if (show_root_diff && !commit->parents) {
119 header = generate_header(sha1, NULL, commit);
120 diff_root_tree(commit_sha1, "");
123 /* More than one parent? */
124 if (commit->parents && commit->parents->next) {
127 else if (combine_merges) {
128 header = generate_header(sha1, sha1, commit);
129 return diff_tree_combined_merge(sha1, header,
131 dense_combined_merges);
135 for (parents = commit->parents; parents; parents = parents->next) {
136 struct commit *parent = parents->item;
137 header = generate_header(sha1, parent->object.sha1, commit);
138 diff_tree_sha1_top(parent->object.sha1, commit_sha1, "");
139 if (!header && verbose_header) {
140 header_prefix = "\ndiff-tree ";
142 * Don't print multiple merge entries if we
143 * don't print the diffs.
150 static int diff_tree_stdin(char *line)
152 int len = strlen(line);
153 unsigned char commit[20], parent[20];
154 static char this_header[1000];
155 int abbrev = diff_options.abbrev;
157 if (!len || line[len-1] != '\n')
160 if (get_sha1_hex(line, commit))
162 if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
165 sprintf(this_header, "%s (from %s)\n",
166 diff_unique_abbrev(commit, abbrev),
167 diff_unique_abbrev(parent, abbrev));
168 header = this_header;
169 return diff_tree_sha1_top(parent, commit, "");
172 return diff_tree_commit(commit);
175 static const char diff_tree_usage[] =
176 "git-diff-tree [--stdin] [-m] [-c] [--cc] [-s] [-v] [--pretty] [-t] [-r] [--root] "
177 "[<common diff options>] <tree-ish> [<tree-ish>] [<path>...]\n"
178 " -r diff recursively\n"
179 " --root include the initial commit as diff against /dev/null\n"
180 COMMON_DIFF_OPTIONS_HELP;
182 int main(int argc, const char **argv)
186 unsigned char sha1[2][20];
187 const char *prefix = setup_git_directory();
189 git_config(git_diff_config);
191 diff_setup(&diff_options);
204 if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
211 diff_opt_cnt = diff_opt_parse(&diff_options, argv, argc);
212 if (diff_opt_cnt < 0)
213 usage(diff_tree_usage);
214 else if (diff_opt_cnt) {
215 argv += diff_opt_cnt - 1;
216 argc -= diff_opt_cnt - 1;
221 if (!strcmp(arg, "--")) {
226 if (!strcmp(arg, "-r")) {
227 diff_options.recursive = 1;
230 if (!strcmp(arg, "-t")) {
231 diff_options.recursive = 1;
232 diff_options.tree_in_recursive = 1;
235 if (!strcmp(arg, "-m")) {
239 if (!strcmp(arg, "-c")) {
243 if (!strcmp(arg, "--cc")) {
244 dense_combined_merges = combine_merges = 1;
247 if (!strcmp(arg, "-v")) {
249 header_prefix = "diff-tree ";
252 if (!strncmp(arg, "--pretty", 8)) {
254 header_prefix = "diff-tree ";
255 commit_format = get_commit_format(arg+8);
258 if (!strcmp(arg, "--stdin")) {
262 if (!strcmp(arg, "--root")) {
266 if (!strcmp(arg, "--no-commit-id")) {
270 if (!strcmp(arg, "--always")) {
271 always_show_header = 1;
274 usage(diff_tree_usage);
276 if (diff_options.output_format == DIFF_FORMAT_PATCH)
277 diff_options.recursive = 1;
279 if (combine_merges) {
280 diff_options.output_format = DIFF_FORMAT_PATCH;
281 show_empty_combined = !ignore_merges;
285 diff_tree_setup_paths(get_pathspec(prefix, argv));
286 diff_setup_done(&diff_options);
291 usage(diff_tree_usage);
294 diff_tree_commit(sha1[0]);
297 diff_tree_sha1_top(sha1[0], sha1[1], "");
304 if (diff_options.detect_rename)
305 diff_options.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
306 DIFF_SETUP_USE_CACHE);
307 while (fgets(line, sizeof(line), stdin))
308 diff_tree_stdin(line);