7 static struct rev_info log_tree_opt;
9 static int diff_tree_commit_sha1(const unsigned char *sha1)
11 struct commit *commit = lookup_commit_reference(sha1);
14 return log_tree_commit(&log_tree_opt, commit);
17 /* Diff one or more commits. */
18 static int stdin_diff_commit(struct commit *commit, char *line, int len)
20 unsigned char sha1[20];
21 if (isspace(line[40]) && !get_sha1_hex(line+41, sha1)) {
22 /* Graft the fake parents locally to the commit */
24 struct commit_list **pptr, *parents;
26 /* Free the real parent list */
27 for (parents = commit->parents; parents; ) {
28 struct commit_list *tmp = parents->next;
32 commit->parents = NULL;
33 pptr = &(commit->parents);
34 while (line[pos] && !get_sha1_hex(line + pos, sha1)) {
35 struct commit *parent = lookup_commit(sha1);
37 pptr = &commit_list_insert(parent, pptr)->next;
42 return log_tree_commit(&log_tree_opt, commit);
46 static int stdin_diff_trees(struct tree *tree1, char *line, int len)
48 unsigned char sha1[20];
50 if (len != 82 || !isspace(line[40]) || get_sha1_hex(line + 41, sha1))
51 return error("Need exactly two trees, separated by a space");
52 tree2 = lookup_tree(sha1);
53 if (!tree2 || parse_tree(tree2))
55 printf("%s %s\n", sha1_to_hex(tree1->object.sha1),
56 sha1_to_hex(tree2->object.sha1));
57 diff_tree_sha1(tree1->object.sha1, tree2->object.sha1,
58 "", &log_tree_opt.diffopt);
59 log_tree_diff_flush(&log_tree_opt);
63 static int diff_tree_stdin(char *line)
65 int len = strlen(line);
66 unsigned char sha1[20];
69 if (!len || line[len-1] != '\n')
72 if (get_sha1_hex(line, sha1))
74 obj = lookup_object(sha1);
75 obj = obj ? obj : parse_object(sha1);
78 if (obj->type == OBJ_COMMIT)
79 return stdin_diff_commit((struct commit *)obj, line, len);
80 if (obj->type == OBJ_TREE)
81 return stdin_diff_trees((struct tree *)obj, line, len);
82 error("Object %s is a %s, not a commit or tree",
83 sha1_to_hex(sha1), typename(obj->type));
87 static const char diff_tree_usage[] =
88 "git diff-tree [--stdin] [-m] [-c] [--cc] [-s] [-v] [--pretty] [-t] [-r] [--root] "
89 "[<common diff options>] <tree-ish> [<tree-ish>] [<path>...]\n"
90 " -r diff recursively\n"
91 " --root include the initial commit as diff against /dev/null\n"
92 COMMON_DIFF_OPTIONS_HELP;
94 int cmd_diff_tree(int argc, const char **argv, const char *prefix)
98 struct object *tree1, *tree2;
99 static struct rev_info *opt = &log_tree_opt;
102 init_revisions(opt, prefix);
103 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
107 argc = setup_revisions(argc, argv, opt, NULL);
110 const char *arg = *++argv;
112 if (!strcmp(arg, "--stdin")) {
116 usage(diff_tree_usage);
119 if (!opt->diffopt.output_format)
120 opt->diffopt.output_format = DIFF_FORMAT_RAW;
123 * NOTE! We expect "a ^b" to be equal to "a..b", so we
124 * reverse the order of the objects if the second one
125 * is marked UNINTERESTING.
127 nr_sha1 = opt->pending.nr;
131 usage(diff_tree_usage);
134 tree1 = opt->pending.objects[0].item;
135 diff_tree_commit_sha1(tree1->sha1);
138 tree1 = opt->pending.objects[0].item;
139 tree2 = opt->pending.objects[1].item;
140 if (tree2->flags & UNINTERESTING) {
141 struct object *tmp = tree2;
145 diff_tree_sha1(tree1->sha1,
148 log_tree_diff_flush(opt);
153 if (opt->diffopt.detect_rename)
154 opt->diffopt.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
155 DIFF_SETUP_USE_CACHE);
156 while (fgets(line, sizeof(line), stdin)) {
157 unsigned char sha1[20];
159 if (get_sha1_hex(line, sha1)) {
164 diff_tree_stdin(line);
168 return diff_result_code(&opt->diffopt, 0);