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 combine_merges = 0;
10 static int dense_combined_merges = 0;
11 static int read_stdin = 0;
12 static int always_show_header = 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_type, &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);
96 if (always_show_header) {
103 static int diff_tree_commit(struct commit *commit)
105 struct commit_list *parents;
106 unsigned const char *sha1 = commit->object.sha1;
109 if (show_root_diff && !commit->parents) {
110 header = generate_header(sha1, NULL, commit);
111 diff_root_tree(sha1, "");
114 /* More than one parent? */
115 if (commit->parents && commit->parents->next) {
118 else if (combine_merges) {
119 header = generate_header(sha1, sha1, commit);
120 header = diff_tree_combined_merge(sha1, header,
121 dense_combined_merges,
123 if (!header && verbose_header)
124 header_prefix = "\ndiff-tree ";
129 for (parents = commit->parents; parents; parents = parents->next) {
130 struct commit *parent = parents->item;
131 header = generate_header(sha1, parent->object.sha1, commit);
132 diff_tree_sha1_top(parent->object.sha1, sha1, "");
133 if (!header && verbose_header) {
134 header_prefix = "\ndiff-tree ";
136 * Don't print multiple merge entries if we
137 * don't print the diffs.
144 static int diff_tree_commit_sha1(const unsigned char *sha1)
146 struct commit *commit = lookup_commit_reference(sha1);
149 return diff_tree_commit(commit);
152 static int diff_tree_stdin(char *line)
154 int len = strlen(line);
155 unsigned char sha1[20];
156 struct commit *commit;
158 if (!len || line[len-1] != '\n')
161 if (get_sha1_hex(line, sha1))
163 commit = lookup_commit(sha1);
164 if (!commit || parse_commit(commit))
166 if (isspace(line[40]) && !get_sha1_hex(line+41, sha1)) {
167 /* Graft the fake parents locally to the commit */
169 struct commit_list **pptr, *parents;
171 /* Free the real parent list */
172 for (parents = commit->parents; parents; ) {
173 struct commit_list *tmp = parents->next;
177 commit->parents = NULL;
178 pptr = &(commit->parents);
179 while (line[pos] && !get_sha1_hex(line + pos, sha1)) {
180 struct commit *parent = lookup_commit(sha1);
182 pptr = &commit_list_insert(parent, pptr)->next;
187 return diff_tree_commit(commit);
190 static const char diff_tree_usage[] =
191 "git-diff-tree [--stdin] [-m] [-c] [--cc] [-s] [-v] [--pretty] [-t] [-r] [--root] "
192 "[<common diff options>] <tree-ish> [<tree-ish>] [<path>...]\n"
193 " -r diff recursively\n"
194 " --root include the initial commit as diff against /dev/null\n"
195 COMMON_DIFF_OPTIONS_HELP;
197 int main(int argc, const char **argv)
201 unsigned char sha1[2][20];
202 const char *prefix = setup_git_directory();
204 git_config(git_diff_config);
206 diff_setup(&diff_options);
219 if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
226 diff_opt_cnt = diff_opt_parse(&diff_options, argv, argc);
227 if (diff_opt_cnt < 0)
228 usage(diff_tree_usage);
229 else if (diff_opt_cnt) {
230 argv += diff_opt_cnt - 1;
231 argc -= diff_opt_cnt - 1;
236 if (!strcmp(arg, "--")) {
241 if (!strcmp(arg, "-r")) {
242 diff_options.recursive = 1;
245 if (!strcmp(arg, "-t")) {
246 diff_options.recursive = 1;
247 diff_options.tree_in_recursive = 1;
250 if (!strcmp(arg, "-m")) {
254 if (!strcmp(arg, "-c")) {
258 if (!strcmp(arg, "--cc")) {
259 dense_combined_merges = combine_merges = 1;
262 if (!strcmp(arg, "-v")) {
264 header_prefix = "diff-tree ";
267 if (!strncmp(arg, "--pretty", 8)) {
269 header_prefix = "diff-tree ";
270 commit_format = get_commit_format(arg+8);
273 if (!strcmp(arg, "--stdin")) {
277 if (!strcmp(arg, "--root")) {
281 if (!strcmp(arg, "--no-commit-id")) {
285 if (!strcmp(arg, "--always")) {
286 always_show_header = 1;
289 usage(diff_tree_usage);
295 /* We can only do dense combined merges with diff output */
296 if (dense_combined_merges)
297 diff_options.output_format = DIFF_FORMAT_PATCH;
299 if (diff_options.output_format == DIFF_FORMAT_PATCH)
300 diff_options.recursive = 1;
302 diff_tree_setup_paths(get_pathspec(prefix, argv));
303 diff_setup_done(&diff_options);
308 usage(diff_tree_usage);
311 diff_tree_commit_sha1(sha1[0]);
314 diff_tree_sha1_top(sha1[0], sha1[1], "");
321 if (diff_options.detect_rename)
322 diff_options.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
323 DIFF_SETUP_USE_CACHE);
324 while (fgets(line, sizeof(line), stdin))
325 diff_tree_stdin(line);