8 #include "repository.h"
10 static struct rev_info log_tree_opt;
12 static int diff_tree_commit_oid(const struct object_id *oid)
14 struct commit *commit = lookup_commit_reference(the_repository, oid);
17 return log_tree_commit(&log_tree_opt, commit);
20 /* Diff one or more commits. */
21 static int stdin_diff_commit(struct commit *commit, const char *p)
24 struct commit_list **pptr = NULL;
26 /* Graft the fake parents locally to the commit */
27 while (isspace(*p++) && !parse_oid_hex(p, &oid, &p)) {
28 struct commit *parent = lookup_commit(the_repository, &oid);
30 /* Free the real parent list */
31 free_commit_list(commit->parents);
32 commit->parents = NULL;
33 pptr = &(commit->parents);
36 pptr = &commit_list_insert(parent, pptr)->next;
39 return log_tree_commit(&log_tree_opt, commit);
43 static int stdin_diff_trees(struct tree *tree1, const char *p)
47 if (!isspace(*p++) || parse_oid_hex(p, &oid, &p) || *p)
48 return error("Need exactly two trees, separated by a space");
49 tree2 = lookup_tree(the_repository, &oid);
50 if (!tree2 || parse_tree(tree2))
52 printf("%s %s\n", oid_to_hex(&tree1->object.oid),
53 oid_to_hex(&tree2->object.oid));
54 diff_tree_oid(&tree1->object.oid, &tree2->object.oid,
55 "", &log_tree_opt.diffopt);
56 log_tree_diff_flush(&log_tree_opt);
60 static int diff_tree_stdin(char *line)
62 int len = strlen(line);
67 if (!len || line[len-1] != '\n')
70 if (parse_oid_hex(line, &oid, &p))
72 obj = parse_object(the_repository, &oid);
75 if (obj->type == OBJ_COMMIT)
76 return stdin_diff_commit((struct commit *)obj, p);
77 if (obj->type == OBJ_TREE)
78 return stdin_diff_trees((struct tree *)obj, p);
79 error("Object %s is a %s, not a commit or tree",
80 oid_to_hex(&oid), type_name(obj->type));
84 static const char diff_tree_usage[] =
85 "git diff-tree [--stdin] [-m] [-c | --cc] [-s] [-v] [--pretty] [-t] [-r] [--root] "
86 "[<common-diff-options>] <tree-ish> [<tree-ish>] [<path>...]\n"
87 " -r diff recursively\n"
88 " -c show combined diff for merge commits\n"
89 " --cc show combined diff for merge commits removing uninteresting hunks\n"
90 " --combined-all-paths\n"
91 " show name of file in all parents for combined diffs\n"
92 " --root include the initial commit as diff against /dev/null\n"
93 COMMON_DIFF_OPTIONS_HELP;
95 static void diff_tree_tweak_rev(struct rev_info *rev, struct setup_revision_opt *opt)
97 if (!rev->diffopt.output_format) {
98 if (rev->dense_combined_merges)
99 rev->diffopt.output_format = DIFF_FORMAT_PATCH;
101 rev->diffopt.output_format = DIFF_FORMAT_RAW;
105 int cmd_diff_tree(int argc, const char **argv, const char *prefix)
108 struct object *tree1, *tree2;
109 static struct rev_info *opt = &log_tree_opt;
110 struct setup_revision_opt s_r_opt;
113 if (argc == 2 && !strcmp(argv[1], "-h"))
114 usage(diff_tree_usage);
116 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
117 repo_init_revisions(the_repository, opt, prefix);
118 if (read_cache() < 0)
119 die(_("index file corrupt"));
122 opt->disable_stdin = 1;
123 memset(&s_r_opt, 0, sizeof(s_r_opt));
124 s_r_opt.tweak = diff_tree_tweak_rev;
126 precompose_argv(argc, argv);
127 argc = setup_revisions(argc, argv, opt, &s_r_opt);
130 const char *arg = *++argv;
132 if (!strcmp(arg, "--stdin")) {
136 usage(diff_tree_usage);
140 * NOTE! We expect "a..b" to expand to "^a b" but it is
141 * perfectly valid for revision range parser to yield "b ^a",
142 * which means the same thing. If we get the latter, i.e. the
143 * second one is marked UNINTERESTING, we recover the original
144 * order the user gave, i.e. "a..b", by swapping the trees.
146 switch (opt->pending.nr) {
149 usage(diff_tree_usage);
152 tree1 = opt->pending.objects[0].item;
153 diff_tree_commit_oid(&tree1->oid);
156 tree1 = opt->pending.objects[0].item;
157 tree2 = opt->pending.objects[1].item;
158 if (tree2->flags & UNINTERESTING) {
161 diff_tree_oid(&tree1->oid, &tree2->oid, "", &opt->diffopt);
162 log_tree_diff_flush(opt);
170 if (opt->diffopt.detect_rename) {
171 if (!the_index.cache)
172 read_index(&the_index);
173 opt->diffopt.setup |= DIFF_SETUP_USE_SIZE_CACHE;
175 while (fgets(line, sizeof(line), stdin)) {
176 struct object_id oid;
178 if (get_oid_hex(line, &oid)) {
183 diff_tree_stdin(line);
184 if (saved_nrl < opt->diffopt.needed_rename_limit)
185 saved_nrl = opt->diffopt.needed_rename_limit;
186 if (opt->diffopt.degraded_cc_to_c)
190 opt->diffopt.degraded_cc_to_c = saved_dcctc;
191 opt->diffopt.needed_rename_limit = saved_nrl;
194 return diff_result_code(&opt->diffopt, 0);