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,
70 const struct commit *commit)
72 static char this_header[16384];
75 int abbrev = diff_options.abbrev;
76 const char *msg = commit->buffer;
79 return sha1_to_hex(commit_sha1);
83 offset = sprintf(this_header, "%s%s ",
85 diff_unique_abbrev(commit_sha1, abbrev));
86 if (commit_sha1 != parent_sha1)
87 offset += sprintf(this_header + offset, "(from %s)\n",
89 ? diff_unique_abbrev(parent_sha1, abbrev)
92 offset += sprintf(this_header + offset, "(from parents)\n");
93 offset += pretty_print_commit(commit_format, commit, len,
95 sizeof(this_header) - offset, abbrev);
99 static int diff_tree_commit(const unsigned char *commit_sha1)
101 struct commit *commit;
102 struct commit_list *parents;
104 unsigned char sha1[20];
106 sprintf(name, "%s^0", sha1_to_hex(commit_sha1));
107 if (get_sha1(name, sha1))
110 commit = lookup_commit(sha1);
113 if (show_root_diff && !commit->parents) {
114 header = generate_header(sha1, NULL, commit);
115 diff_root_tree(commit_sha1, "");
118 /* More than one parent? */
119 if (commit->parents && commit->parents->next) {
122 else if (combine_merges) {
123 header = generate_header(sha1, sha1, commit);
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, parent->object.sha1, commit);
133 diff_tree_sha1_top(parent->object.sha1, commit_sha1, "");
134 if (!header && verbose_header) {
135 header_prefix = "\ndiff-tree ";
137 * Don't print multiple merge entries if we
138 * don't print the diffs.
145 static int diff_tree_stdin(char *line)
147 int len = strlen(line);
148 unsigned char commit[20], parent[20];
149 static char this_header[1000];
150 int abbrev = diff_options.abbrev;
152 if (!len || line[len-1] != '\n')
155 if (get_sha1_hex(line, commit))
157 if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
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, "");
167 return diff_tree_commit(commit);
170 static 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"
175 COMMON_DIFF_OPTIONS_HELP;
177 int main(int argc, const char **argv)
181 unsigned char sha1[2][20];
182 const char *prefix = setup_git_directory();
184 git_config(git_diff_config);
186 diff_setup(&diff_options);
199 if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
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;
216 if (!strcmp(arg, "--")) {
221 if (!strcmp(arg, "-r")) {
222 diff_options.recursive = 1;
225 if (!strcmp(arg, "-t")) {
226 diff_options.recursive = 1;
227 diff_options.tree_in_recursive = 1;
230 if (!strcmp(arg, "-m")) {
234 if (!strcmp(arg, "-c")) {
238 if (!strcmp(arg, "--cc")) {
239 dense_combined_merges = combine_merges = 1;
242 if (!strcmp(arg, "-v")) {
244 header_prefix = "diff-tree ";
247 if (!strncmp(arg, "--pretty", 8)) {
249 header_prefix = "diff-tree ";
250 commit_format = get_commit_format(arg+8);
253 if (!strcmp(arg, "--stdin")) {
257 if (!strcmp(arg, "--root")) {
261 if (!strcmp(arg, "--no-commit-id")) {
265 usage(diff_tree_usage);
267 if (diff_options.output_format == DIFF_FORMAT_PATCH)
268 diff_options.recursive = 1;
270 if (combine_merges) {
271 diff_options.output_format = DIFF_FORMAT_PATCH;
272 show_empty_combined = !ignore_merges;
276 diff_tree_setup_paths(get_pathspec(prefix, argv));
277 diff_setup_done(&diff_options);
282 usage(diff_tree_usage);
285 diff_tree_commit(sha1[0]);
288 diff_tree_sha1_top(sha1[0], sha1[1], "");
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);