]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/diff-tree.c
builtin: stop using `the_index`
[thirdparty/git.git] / builtin / diff-tree.c
CommitLineData
bc5c5ec0 1#include "builtin.h"
b2141fc1 2#include "config.h"
3ebfd4aa 3#include "diff.h"
e3bc7a3b 4#include "commit.h"
f394e093 5#include "gettext.h"
41771fa4 6#include "hex.h"
5f1c3f07 7#include "log-tree.h"
08c46a49 8#include "read-cache-ll.h"
109cd76d 9#include "repository.h"
0fd2e215 10#include "revision.h"
d4a4f929 11#include "tree.h"
9174026c 12
cd2bdc53 13static struct rev_info log_tree_opt;
b11645be 14
315f49f2 15static int diff_tree_commit_oid(const struct object_id *oid)
45392a64 16{
2122f675 17 struct commit *commit = lookup_commit_reference(the_repository, oid);
45392a64
JH
18 if (!commit)
19 return -1;
5f1c3f07 20 return log_tree_commit(&log_tree_opt, commit);
45392a64
JH
21}
22
a57114c8 23/* Diff one or more commits. */
5f5e936d 24static int stdin_diff_commit(struct commit *commit, const char *p)
b11645be 25{
5f5e936d 26 struct object_id oid;
27 struct commit_list **pptr = NULL;
28
29 /* Graft the fake parents locally to the commit */
30 while (isspace(*p++) && !parse_oid_hex(p, &oid, &p)) {
c1f5eb49 31 struct commit *parent = lookup_commit(the_repository, &oid);
5f5e936d 32 if (!pptr) {
33 /* Free the real parent list */
34 free_commit_list(commit->parents);
35 commit->parents = NULL;
36 pptr = &(commit->parents);
37 }
38 if (parent) {
39 pptr = &commit_list_insert(parent, pptr)->next;
45392a64 40 }
b11645be 41 }
5f1c3f07 42 return log_tree_commit(&log_tree_opt, commit);
e0965d83
LT
43}
44
140b378d 45/* Diff two trees. */
5f5e936d 46static int stdin_diff_trees(struct tree *tree1, const char *p)
140b378d 47{
5f5e936d 48 struct object_id oid;
140b378d 49 struct tree *tree2;
5f5e936d 50 if (!isspace(*p++) || parse_oid_hex(p, &oid, &p) || *p)
140b378d 51 return error("Need exactly two trees, separated by a space");
f86bcc7b 52 tree2 = lookup_tree(the_repository, &oid);
140b378d
KW
53 if (!tree2 || parse_tree(tree2))
54 return -1;
f2fd0760 55 printf("%s %s\n", oid_to_hex(&tree1->object.oid),
56 oid_to_hex(&tree2->object.oid));
66f414f8
BW
57 diff_tree_oid(&tree1->object.oid, &tree2->object.oid,
58 "", &log_tree_opt.diffopt);
140b378d
KW
59 log_tree_diff_flush(&log_tree_opt);
60 return 0;
61}
62
a57114c8
KW
63static int diff_tree_stdin(char *line)
64{
65 int len = strlen(line);
5f5e936d 66 struct object_id oid;
140b378d 67 struct object *obj;
5f5e936d 68 const char *p;
a57114c8
KW
69
70 if (!len || line[len-1] != '\n')
71 return -1;
72 line[len-1] = 0;
5f5e936d 73 if (parse_oid_hex(line, &oid, &p))
a57114c8 74 return -1;
109cd76d 75 obj = parse_object(the_repository, &oid);
140b378d 76 if (!obj)
a57114c8 77 return -1;
140b378d 78 if (obj->type == OBJ_COMMIT)
5f5e936d 79 return stdin_diff_commit((struct commit *)obj, p);
140b378d 80 if (obj->type == OBJ_TREE)
5f5e936d 81 return stdin_diff_trees((struct tree *)obj, p);
140b378d 82 error("Object %s is a %s, not a commit or tree",
debca9d2 83 oid_to_hex(&oid), type_name(obj->type));
140b378d 84 return -1;
a57114c8
KW
85}
86
4d1f1190 87static const char diff_tree_usage[] =
320ee66d
ÆAB
88"git diff-tree [--stdin] [-m] [-s] [-v] [--no-commit-id] [--pretty]\n"
89" [-t] [-r] [-c | --cc] [--combined-all-paths] [--root] [--merge-base]\n"
90" [<common-diff-options>] <tree-ish> [<tree-ish>] [<path>...]\n"
acf7828e 91"\n"
50b8e355 92" -r diff recursively\n"
d76ce4f7
EN
93" -c show combined diff for merge commits\n"
94" --cc show combined diff for merge commits removing uninteresting hunks\n"
95" --combined-all-paths\n"
96" show name of file in all parents for combined diffs\n"
50b8e355 97" --root include the initial commit as diff against /dev/null\n"
dda2d79a 98COMMON_DIFF_OPTIONS_HELP;
a8db165e 99
cc88afad 100static void diff_tree_tweak_rev(struct rev_info *rev)
b4490059
JH
101{
102 if (!rev->diffopt.output_format) {
103 if (rev->dense_combined_merges)
104 rev->diffopt.output_format = DIFF_FORMAT_PATCH;
105 else
106 rev->diffopt.output_format = DIFF_FORMAT_RAW;
107 }
108}
109
a633fca0 110int cmd_diff_tree(int argc, const char **argv, const char *prefix)
73134b6d 111{
e0965d83 112 char line[1000];
cd2bdc53
LT
113 struct object *tree1, *tree2;
114 static struct rev_info *opt = &log_tree_opt;
b4490059 115 struct setup_revision_opt s_r_opt;
5778b22b 116 struct userformat_want w;
5f1c3f07 117 int read_stdin = 0;
3d09c228 118 int merge_base = 0;
73134b6d 119
5a88f97c
JH
120 if (argc == 2 && !strcmp(argv[1], "-h"))
121 usage(diff_tree_usage);
122
37590ce3 123 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
48c5fbfb
SL
124
125 prepare_repo_settings(the_repository);
126 the_repository->settings.command_requires_full_index = 0;
127
2abf3503 128 repo_init_revisions(the_repository, opt, prefix);
07047d68 129 if (repo_read_index(the_repository) < 0)
fd66bcc3 130 die(_("index file corrupt"));
8e8f9987 131 opt->abbrev = 0;
91539833 132 opt->diff = 1;
8b3dce56 133 opt->disable_stdin = 1;
b4490059
JH
134 memset(&s_r_opt, 0, sizeof(s_r_opt));
135 s_r_opt.tweak = diff_tree_tweak_rev;
90a78b83 136
5c327502 137 prefix = precompose_argv_prefix(argc, argv, prefix);
b4490059 138 argc = setup_revisions(argc, argv, opt, &s_r_opt);
6b5ee137 139
5778b22b
TB
140 memset(&w, 0, sizeof(w));
141 userformat_find_requirements(NULL, &w);
142
143 if (!opt->show_notes_given && w.notes)
144 opt->show_notes = 1;
145 if (opt->show_notes)
146 load_display_notes(&opt->notes_opt);
147
cd2bdc53
LT
148 while (--argc > 0) {
149 const char *arg = *++argv;
c5b42386 150
e0965d83
LT
151 if (!strcmp(arg, "--stdin")) {
152 read_stdin = 1;
153 continue;
154 }
3d09c228
DL
155 if (!strcmp(arg, "--merge-base")) {
156 merge_base = 1;
157 continue;
158 }
c5bac17a 159 usage(diff_tree_usage);
73134b6d
LT
160 }
161
3d09c228 162 if (read_stdin && merge_base)
43ea635c 163 die(_("options '%s' and '%s' cannot be used together"), "--stdin", "--merge-base");
3d09c228
DL
164 if (merge_base && opt->pending.nr != 2)
165 die(_("--merge-base only works with two commits"));
166
1eb4136a
JH
167 opt->diffopt.rotate_to_strict = 1;
168
cd2bdc53 169 /*
8ba74bfd
JH
170 * NOTE! We expect "a..b" to expand to "^a b" but it is
171 * perfectly valid for revision range parser to yield "b ^a",
172 * which means the same thing. If we get the latter, i.e. the
173 * second one is marked UNINTERESTING, we recover the original
174 * order the user gave, i.e. "a..b", by swapping the trees.
cd2bdc53 175 */
315f49f2 176 switch (opt->pending.nr) {
0a8365a1
LT
177 case 0:
178 if (!read_stdin)
179 usage(diff_tree_usage);
180 break;
181 case 1:
1f1e895f 182 tree1 = opt->pending.objects[0].item;
315f49f2 183 diff_tree_commit_oid(&tree1->oid);
0a8365a1
LT
184 break;
185 case 2:
1f1e895f
LT
186 tree1 = opt->pending.objects[0].item;
187 tree2 = opt->pending.objects[1].item;
3d09c228
DL
188 if (merge_base) {
189 struct object_id oid;
190
191 diff_get_merge_base(opt, &oid);
192 tree1 = lookup_object(the_repository, &oid);
193 } else if (tree2->flags & UNINTERESTING) {
35d803bc 194 SWAP(tree2, tree1);
1f1e895f 195 }
66f414f8 196 diff_tree_oid(&tree1->oid, &tree2->oid, "", &opt->diffopt);
5f1c3f07 197 log_tree_diff_flush(opt);
0a8365a1
LT
198 break;
199 }
200
62c64895 201 if (read_stdin) {
f31027c9
JH
202 int saved_nrl = 0;
203 int saved_dcctc = 0;
204
1eb4136a 205 opt->diffopt.rotate_to_strict = 0;
f8781bfd 206 opt->diffopt.no_free = 1;
ff7fe37b 207 if (opt->diffopt.detect_rename) {
f59aa5e0 208 if (the_repository->index->cache)
e1ff0a32 209 repo_read_index(the_repository);
ff7fe37b
NTND
210 opt->diffopt.setup |= DIFF_SETUP_USE_SIZE_CACHE;
211 }
62c64895 212 while (fgets(line, sizeof(line), stdin)) {
5f5e936d 213 struct object_id oid;
e0965d83 214
5f5e936d 215 if (get_oid_hex(line, &oid)) {
62c64895
WC
216 fputs(line, stdout);
217 fflush(stdout);
218 }
f31027c9 219 else {
62c64895 220 diff_tree_stdin(line);
f31027c9
JH
221 if (saved_nrl < opt->diffopt.needed_rename_limit)
222 saved_nrl = opt->diffopt.needed_rename_limit;
223 if (opt->diffopt.degraded_cc_to_c)
224 saved_dcctc = 1;
225 }
e0c97ca6 226 }
f31027c9
JH
227 opt->diffopt.degraded_cc_to_c = saved_dcctc;
228 opt->diffopt.needed_rename_limit = saved_nrl;
f8781bfd
JH
229 opt->diffopt.no_free = 0;
230 diff_free(&opt->diffopt);
e0c97ca6 231 }
da31b358 232
5cc6b2d7 233 return diff_result_code(&opt->diffopt);
9174026c 234}