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;
14 static const char *header = NULL;
15 static const char *header_prefix = "";
16 static enum cmit_fmt commit_format = CMIT_FMT_RAW;
18 static struct diff_options diff_options;
20 static int call_diff_flush(void)
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;
32 printf("%s%c", header, diff_options.line_termination);
35 diff_flush(&diff_options);
39 static int diff_tree_sha1_top(const unsigned char *old,
40 const unsigned char *new, const char *base)
44 ret = diff_tree_sha1(old, new, base, &diff_options);
49 static int diff_root_tree(const unsigned char *new, const char *base)
53 struct tree_desc empty, real;
55 tree = read_object_with_reference(new, "tree", &real.size, NULL);
57 die("unable to read root tree (%s)", sha1_to_hex(new));
62 retval = diff_tree(&empty, &real, base, &diff_options);
68 static const char *generate_header(const unsigned char *commit_sha1,
69 const unsigned char *parent_sha1,
72 static char this_header[16384];
75 int abbrev = diff_options.abbrev;
78 return sha1_to_hex(commit_sha1);
82 offset = sprintf(this_header, "%s%s ",
84 diff_unique_abbrev(commit_sha1, abbrev));
85 if (commit_sha1 != parent_sha1)
86 offset += sprintf(this_header + offset, "(from %s)\n",
88 ? diff_unique_abbrev(parent_sha1, abbrev)
91 offset += sprintf(this_header + offset, "(from parents)\n");
92 offset += pretty_print_commit(commit_format, msg, len,
94 sizeof(this_header) - offset);
98 static int diff_tree_commit(const unsigned char *commit_sha1)
100 struct commit *commit;
101 struct commit_list *parents;
103 unsigned char sha1[20];
105 sprintf(name, "%s^0", sha1_to_hex(commit_sha1));
106 if (get_sha1(name, sha1))
109 commit = lookup_commit(sha1);
112 if (show_root_diff && !commit->parents) {
113 header = generate_header(sha1, NULL, commit->buffer);
114 diff_root_tree(commit_sha1, "");
117 /* More than one parent? */
118 if (commit->parents && commit->parents->next) {
121 else if (combine_merges) {
122 header = generate_header(sha1, sha1,
124 return diff_tree_combined_merge(sha1, header,
126 dense_combined_merges);
130 for (parents = commit->parents; parents; parents = parents->next) {
131 struct commit *parent = parents->item;
132 header = generate_header(sha1,
135 diff_tree_sha1_top(parent->object.sha1, commit_sha1, "");
136 if (!header && verbose_header) {
137 header_prefix = "\ndiff-tree ";
139 * Don't print multiple merge entries if we
140 * don't print the diffs.
147 static int diff_tree_stdin(char *line)
149 int len = strlen(line);
150 unsigned char commit[20], parent[20];
151 static char this_header[1000];
152 int abbrev = diff_options.abbrev;
154 if (!len || line[len-1] != '\n')
157 if (get_sha1_hex(line, commit))
159 if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
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, "");
169 return diff_tree_commit(commit);
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;
179 int main(int argc, const char **argv)
183 unsigned char sha1[2][20];
184 const char *prefix = setup_git_directory();
186 git_config(git_diff_config);
188 diff_setup(&diff_options);
201 if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
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;
218 if (!strcmp(arg, "--")) {
223 if (!strcmp(arg, "-r")) {
224 diff_options.recursive = 1;
227 if (!strcmp(arg, "-t")) {
228 diff_options.recursive = 1;
229 diff_options.tree_in_recursive = 1;
232 if (!strcmp(arg, "-m")) {
236 if (!strcmp(arg, "-c")) {
240 if (!strcmp(arg, "--cc")) {
241 dense_combined_merges = combine_merges = 1;
244 if (!strcmp(arg, "-v")) {
246 header_prefix = "diff-tree ";
249 if (!strncmp(arg, "--pretty", 8)) {
251 header_prefix = "diff-tree ";
252 commit_format = get_commit_format(arg+8);
255 if (!strcmp(arg, "--stdin")) {
259 if (!strcmp(arg, "--root")) {
263 if (!strcmp(arg, "--no-commit-id")) {
267 usage(diff_tree_usage);
269 if (diff_options.output_format == DIFF_FORMAT_PATCH)
270 diff_options.recursive = 1;
272 if (combine_merges) {
273 diff_options.output_format = DIFF_FORMAT_PATCH;
274 show_empty_combined = !ignore_merges;
278 diff_tree_setup_paths(get_pathspec(prefix, argv));
279 diff_setup_done(&diff_options);
284 usage(diff_tree_usage);
287 diff_tree_commit(sha1[0]);
290 diff_tree_sha1_top(sha1[0], sha1[1], "");
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);