8 static struct rev_info log_tree_opt;
10 static int diff_tree_commit_oid(const struct object_id *oid)
12 struct commit *commit = lookup_commit_reference(oid);
15 return log_tree_commit(&log_tree_opt, commit);
18 /* Diff one or more commits. */
19 static int stdin_diff_commit(struct commit *commit, const char *p)
22 struct commit_list **pptr = NULL;
24 /* Graft the fake parents locally to the commit */
25 while (isspace(*p++) && !parse_oid_hex(p, &oid, &p)) {
26 struct commit *parent = lookup_commit(&oid);
28 /* Free the real parent list */
29 free_commit_list(commit->parents);
30 commit->parents = NULL;
31 pptr = &(commit->parents);
34 pptr = &commit_list_insert(parent, pptr)->next;
37 return log_tree_commit(&log_tree_opt, commit);
41 static int stdin_diff_trees(struct tree *tree1, const char *p)
45 if (!isspace(*p++) || parse_oid_hex(p, &oid, &p) || *p)
46 return error("Need exactly two trees, separated by a space");
47 tree2 = lookup_tree(&oid);
48 if (!tree2 || parse_tree(tree2))
50 printf("%s %s\n", oid_to_hex(&tree1->object.oid),
51 oid_to_hex(&tree2->object.oid));
52 diff_tree_oid(&tree1->object.oid, &tree2->object.oid,
53 "", &log_tree_opt.diffopt);
54 log_tree_diff_flush(&log_tree_opt);
58 static int diff_tree_stdin(char *line)
60 int len = strlen(line);
65 if (!len || line[len-1] != '\n')
68 if (parse_oid_hex(line, &oid, &p))
70 obj = parse_object(&oid);
73 if (obj->type == OBJ_COMMIT)
74 return stdin_diff_commit((struct commit *)obj, p);
75 if (obj->type == OBJ_TREE)
76 return stdin_diff_trees((struct tree *)obj, p);
77 error("Object %s is a %s, not a commit or tree",
78 oid_to_hex(&oid), typename(obj->type));
82 static const char diff_tree_usage[] =
83 "git diff-tree [--stdin] [-m] [-c] [--cc] [-s] [-v] [--pretty] [-t] [-r] [--root] "
84 "[<common-diff-options>] <tree-ish> [<tree-ish>] [<path>...]\n"
85 " -r diff recursively\n"
86 " --root include the initial commit as diff against /dev/null\n"
87 COMMON_DIFF_OPTIONS_HELP;
89 static void diff_tree_tweak_rev(struct rev_info *rev, struct setup_revision_opt *opt)
91 if (!rev->diffopt.output_format) {
92 if (rev->dense_combined_merges)
93 rev->diffopt.output_format = DIFF_FORMAT_PATCH;
95 rev->diffopt.output_format = DIFF_FORMAT_RAW;
99 int cmd_diff_tree(int argc, const char **argv, const char *prefix)
102 struct object *tree1, *tree2;
103 static struct rev_info *opt = &log_tree_opt;
104 struct setup_revision_opt s_r_opt;
107 if (argc == 2 && !strcmp(argv[1], "-h"))
108 usage(diff_tree_usage);
110 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
111 init_revisions(opt, prefix);
115 opt->disable_stdin = 1;
116 memset(&s_r_opt, 0, sizeof(s_r_opt));
117 s_r_opt.tweak = diff_tree_tweak_rev;
119 precompose_argv(argc, argv);
120 argc = setup_revisions(argc, argv, opt, &s_r_opt);
123 const char *arg = *++argv;
125 if (!strcmp(arg, "--stdin")) {
129 usage(diff_tree_usage);
133 * NOTE! We expect "a..b" to expand to "^a b" but it is
134 * perfectly valid for revision range parser to yield "b ^a",
135 * which means the same thing. If we get the latter, i.e. the
136 * second one is marked UNINTERESTING, we recover the original
137 * order the user gave, i.e. "a..b", by swapping the trees.
139 switch (opt->pending.nr) {
142 usage(diff_tree_usage);
145 tree1 = opt->pending.objects[0].item;
146 diff_tree_commit_oid(&tree1->oid);
149 tree1 = opt->pending.objects[0].item;
150 tree2 = opt->pending.objects[1].item;
151 if (tree2->flags & UNINTERESTING) {
154 diff_tree_oid(&tree1->oid, &tree2->oid, "", &opt->diffopt);
155 log_tree_diff_flush(opt);
163 if (opt->diffopt.detect_rename)
164 opt->diffopt.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
165 DIFF_SETUP_USE_CACHE);
166 while (fgets(line, sizeof(line), stdin)) {
167 struct object_id oid;
169 if (get_oid_hex(line, &oid)) {
174 diff_tree_stdin(line);
175 if (saved_nrl < opt->diffopt.needed_rename_limit)
176 saved_nrl = opt->diffopt.needed_rename_limit;
177 if (opt->diffopt.degraded_cc_to_c)
181 opt->diffopt.degraded_cc_to_c = saved_dcctc;
182 opt->diffopt.needed_rename_limit = saved_nrl;
185 return diff_result_code(&opt->diffopt, 0);