.gitignore: "git-verify-commit" is a generated file
[git.git] / builtin / diff-tree.c
blobce0e019e0caa358e1f5e39e5ed51bec3945197e0
1 #include "cache.h"
2 #include "diff.h"
3 #include "commit.h"
4 #include "log-tree.h"
5 #include "builtin.h"
6 #include "submodule.h"
8 static struct rev_info log_tree_opt;
10 static int diff_tree_commit_sha1(const unsigned char *sha1)
12 struct commit *commit = lookup_commit_reference(sha1);
13 if (!commit)
14 return -1;
15 return log_tree_commit(&log_tree_opt, commit);
18 /* Diff one or more commits. */
19 static int stdin_diff_commit(struct commit *commit, char *line, int len)
21 unsigned char sha1[20];
22 if (isspace(line[40]) && !get_sha1_hex(line+41, sha1)) {
23 /* Graft the fake parents locally to the commit */
24 int pos = 41;
25 struct commit_list **pptr;
27 /* Free the real parent list */
28 free_commit_list(commit->parents);
29 commit->parents = NULL;
30 pptr = &(commit->parents);
31 while (line[pos] && !get_sha1_hex(line + pos, sha1)) {
32 struct commit *parent = lookup_commit(sha1);
33 if (parent) {
34 pptr = &commit_list_insert(parent, pptr)->next;
36 pos += 41;
39 return log_tree_commit(&log_tree_opt, commit);
42 /* Diff two trees. */
43 static int stdin_diff_trees(struct tree *tree1, char *line, int len)
45 unsigned char sha1[20];
46 struct tree *tree2;
47 if (len != 82 || !isspace(line[40]) || get_sha1_hex(line + 41, sha1))
48 return error("Need exactly two trees, separated by a space");
49 tree2 = lookup_tree(sha1);
50 if (!tree2 || parse_tree(tree2))
51 return -1;
52 printf("%s %s\n", sha1_to_hex(tree1->object.sha1),
53 sha1_to_hex(tree2->object.sha1));
54 diff_tree_sha1(tree1->object.sha1, tree2->object.sha1,
55 "", &log_tree_opt.diffopt);
56 log_tree_diff_flush(&log_tree_opt);
57 return 0;
60 static int diff_tree_stdin(char *line)
62 int len = strlen(line);
63 unsigned char sha1[20];
64 struct object *obj;
66 if (!len || line[len-1] != '\n')
67 return -1;
68 line[len-1] = 0;
69 if (get_sha1_hex(line, sha1))
70 return -1;
71 obj = lookup_unknown_object(sha1);
72 if (!obj || !obj->parsed)
73 obj = parse_object(sha1);
74 if (!obj)
75 return -1;
76 if (obj->type == OBJ_COMMIT)
77 return stdin_diff_commit((struct commit *)obj, line, len);
78 if (obj->type == OBJ_TREE)
79 return stdin_diff_trees((struct tree *)obj, line, len);
80 error("Object %s is a %s, not a commit or tree",
81 sha1_to_hex(sha1), typename(obj->type));
82 return -1;
85 static const char diff_tree_usage[] =
86 "git diff-tree [--stdin] [-m] [-c] [--cc] [-s] [-v] [--pretty] [-t] [-r] [--root] "
87 "[<common diff options>] <tree-ish> [<tree-ish>] [<path>...]\n"
88 " -r diff recursively\n"
89 " --root include the initial commit as diff against /dev/null\n"
90 COMMON_DIFF_OPTIONS_HELP;
92 static void diff_tree_tweak_rev(struct rev_info *rev, struct setup_revision_opt *opt)
94 if (!rev->diffopt.output_format) {
95 if (rev->dense_combined_merges)
96 rev->diffopt.output_format = DIFF_FORMAT_PATCH;
97 else
98 rev->diffopt.output_format = DIFF_FORMAT_RAW;
102 int cmd_diff_tree(int argc, const char **argv, const char *prefix)
104 int nr_sha1;
105 char line[1000];
106 struct object *tree1, *tree2;
107 static struct rev_info *opt = &log_tree_opt;
108 struct setup_revision_opt s_r_opt;
109 int read_stdin = 0;
111 init_revisions(opt, prefix);
112 gitmodules_config();
113 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
114 opt->abbrev = 0;
115 opt->diff = 1;
116 opt->disable_stdin = 1;
117 memset(&s_r_opt, 0, sizeof(s_r_opt));
118 s_r_opt.tweak = diff_tree_tweak_rev;
119 argc = setup_revisions(argc, argv, opt, &s_r_opt);
121 while (--argc > 0) {
122 const char *arg = *++argv;
124 if (!strcmp(arg, "--stdin")) {
125 read_stdin = 1;
126 continue;
128 usage(diff_tree_usage);
132 * NOTE! We expect "a ^b" to be equal to "a..b", so we
133 * reverse the order of the objects if the second one
134 * is marked UNINTERESTING.
136 nr_sha1 = opt->pending.nr;
137 switch (nr_sha1) {
138 case 0:
139 if (!read_stdin)
140 usage(diff_tree_usage);
141 break;
142 case 1:
143 tree1 = opt->pending.objects[0].item;
144 diff_tree_commit_sha1(tree1->sha1);
145 break;
146 case 2:
147 tree1 = opt->pending.objects[0].item;
148 tree2 = opt->pending.objects[1].item;
149 if (tree2->flags & UNINTERESTING) {
150 struct object *tmp = tree2;
151 tree2 = tree1;
152 tree1 = tmp;
154 diff_tree_sha1(tree1->sha1,
155 tree2->sha1,
156 "", &opt->diffopt);
157 log_tree_diff_flush(opt);
158 break;
161 if (read_stdin) {
162 int saved_nrl = 0;
163 int saved_dcctc = 0;
165 if (opt->diffopt.detect_rename)
166 opt->diffopt.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
167 DIFF_SETUP_USE_CACHE);
168 while (fgets(line, sizeof(line), stdin)) {
169 unsigned char sha1[20];
171 if (get_sha1_hex(line, sha1)) {
172 fputs(line, stdout);
173 fflush(stdout);
175 else {
176 diff_tree_stdin(line);
177 if (saved_nrl < opt->diffopt.needed_rename_limit)
178 saved_nrl = opt->diffopt.needed_rename_limit;
179 if (opt->diffopt.degraded_cc_to_c)
180 saved_dcctc = 1;
183 opt->diffopt.degraded_cc_to_c = saved_dcctc;
184 opt->diffopt.needed_rename_limit = saved_nrl;
187 return diff_result_code(&opt->diffopt, 0);