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 static int diff_tree_stdin(char *line)
19 int len = strlen(line);
20 unsigned char sha1[20];
21 struct commit *commit;
23 if (!len || line[len-1] != '\n')
26 if (get_sha1_hex(line, sha1))
28 commit = lookup_commit(sha1);
29 if (!commit || parse_commit(commit))
31 if (isspace(line[40]) && !get_sha1_hex(line+41, sha1)) {
32 /* Graft the fake parents locally to the commit */
34 struct commit_list **pptr, *parents;
36 /* Free the real parent list */
37 for (parents = commit->parents; parents; ) {
38 struct commit_list *tmp = parents->next;
42 commit->parents = NULL;
43 pptr = &(commit->parents);
44 while (line[pos] && !get_sha1_hex(line + pos, sha1)) {
45 struct commit *parent = lookup_commit(sha1);
47 pptr = &commit_list_insert(parent, pptr)->next;
52 return log_tree_commit(&log_tree_opt, commit);
55 static const char diff_tree_usage[] =
56 "git-diff-tree [--stdin] [-m] [-c] [--cc] [-s] [-v] [--pretty] [-t] [-r] [--root] "
57 "[<common diff options>] <tree-ish> [<tree-ish>] [<path>...]\n"
58 " -r diff recursively\n"
59 " --root include the initial commit as diff against /dev/null\n"
60 COMMON_DIFF_OPTIONS_HELP;
62 int cmd_diff_tree(int argc, const char **argv, const char *prefix)
66 struct object *tree1, *tree2;
67 static struct rev_info *opt = &log_tree_opt;
70 init_revisions(opt, prefix);
71 git_config(git_default_config); /* no "diff" UI options */
75 argc = setup_revisions(argc, argv, opt, NULL);
78 const char *arg = *++argv;
80 if (!strcmp(arg, "--stdin")) {
84 usage(diff_tree_usage);
87 if (!opt->diffopt.output_format)
88 opt->diffopt.output_format = DIFF_FORMAT_RAW;
91 * NOTE! We expect "a ^b" to be equal to "a..b", so we
92 * reverse the order of the objects if the second one
93 * is marked UNINTERESTING.
95 nr_sha1 = opt->pending.nr;
99 usage(diff_tree_usage);
102 tree1 = opt->pending.objects[0].item;
103 diff_tree_commit_sha1(tree1->sha1);
106 tree1 = opt->pending.objects[0].item;
107 tree2 = opt->pending.objects[1].item;
108 if (tree2->flags & UNINTERESTING) {
109 struct object *tmp = tree2;
113 diff_tree_sha1(tree1->sha1,
116 log_tree_diff_flush(opt);
121 return opt->diffopt.exit_with_status ?
122 opt->diffopt.has_changes: 0;
124 if (opt->diffopt.detect_rename)
125 opt->diffopt.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
126 DIFF_SETUP_USE_CACHE);
127 while (fgets(line, sizeof(line), stdin)) {
128 unsigned char sha1[20];
130 if (get_sha1_hex(line, sha1)) {
135 diff_tree_stdin(line);
137 return opt->diffopt.exit_with_status ? opt->diffopt.has_changes: 0;