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