]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/diff-tree.c
Merge branch 'tq/branch-style-fix'
[thirdparty/git.git] / builtin / diff-tree.c
CommitLineData
9174026c 1#include "cache.h"
b2141fc1 2#include "config.h"
3ebfd4aa 3#include "diff.h"
e3bc7a3b 4#include "commit.h"
5f1c3f07 5#include "log-tree.h"
e8cc9cd9 6#include "builtin.h"
302ad7a9 7#include "submodule.h"
109cd76d 8#include "repository.h"
9174026c 9
cd2bdc53 10static struct rev_info log_tree_opt;
b11645be 11
315f49f2 12static int diff_tree_commit_oid(const struct object_id *oid)
45392a64 13{
2122f675 14 struct commit *commit = lookup_commit_reference(the_repository, oid);
45392a64
JH
15 if (!commit)
16 return -1;
5f1c3f07 17 return log_tree_commit(&log_tree_opt, commit);
45392a64
JH
18}
19
a57114c8 20/* Diff one or more commits. */
5f5e936d 21static int stdin_diff_commit(struct commit *commit, const char *p)
b11645be 22{
5f5e936d 23 struct object_id oid;
24 struct commit_list **pptr = NULL;
25
26 /* Graft the fake parents locally to the commit */
27 while (isspace(*p++) && !parse_oid_hex(p, &oid, &p)) {
c1f5eb49 28 struct commit *parent = lookup_commit(the_repository, &oid);
5f5e936d 29 if (!pptr) {
30 /* Free the real parent list */
31 free_commit_list(commit->parents);
32 commit->parents = NULL;
33 pptr = &(commit->parents);
34 }
35 if (parent) {
36 pptr = &commit_list_insert(parent, pptr)->next;
45392a64 37 }
b11645be 38 }
5f1c3f07 39 return log_tree_commit(&log_tree_opt, commit);
e0965d83
LT
40}
41
140b378d 42/* Diff two trees. */
5f5e936d 43static int stdin_diff_trees(struct tree *tree1, const char *p)
140b378d 44{
5f5e936d 45 struct object_id oid;
140b378d 46 struct tree *tree2;
5f5e936d 47 if (!isspace(*p++) || parse_oid_hex(p, &oid, &p) || *p)
140b378d 48 return error("Need exactly two trees, separated by a space");
f86bcc7b 49 tree2 = lookup_tree(the_repository, &oid);
140b378d
KW
50 if (!tree2 || parse_tree(tree2))
51 return -1;
f2fd0760 52 printf("%s %s\n", oid_to_hex(&tree1->object.oid),
53 oid_to_hex(&tree2->object.oid));
66f414f8
BW
54 diff_tree_oid(&tree1->object.oid, &tree2->object.oid,
55 "", &log_tree_opt.diffopt);
140b378d
KW
56 log_tree_diff_flush(&log_tree_opt);
57 return 0;
58}
59
a57114c8
KW
60static int diff_tree_stdin(char *line)
61{
62 int len = strlen(line);
5f5e936d 63 struct object_id oid;
140b378d 64 struct object *obj;
5f5e936d 65 const char *p;
a57114c8
KW
66
67 if (!len || line[len-1] != '\n')
68 return -1;
69 line[len-1] = 0;
5f5e936d 70 if (parse_oid_hex(line, &oid, &p))
a57114c8 71 return -1;
109cd76d 72 obj = parse_object(the_repository, &oid);
140b378d 73 if (!obj)
a57114c8 74 return -1;
140b378d 75 if (obj->type == OBJ_COMMIT)
5f5e936d 76 return stdin_diff_commit((struct commit *)obj, p);
140b378d 77 if (obj->type == OBJ_TREE)
5f5e936d 78 return stdin_diff_trees((struct tree *)obj, p);
140b378d 79 error("Object %s is a %s, not a commit or tree",
debca9d2 80 oid_to_hex(&oid), type_name(obj->type));
140b378d 81 return -1;
a57114c8
KW
82}
83
4d1f1190 84static const char diff_tree_usage[] =
1b1dd23f 85"git diff-tree [--stdin] [-m] [-c] [--cc] [-s] [-v] [--pretty] [-t] [-r] [--root] "
9c9b4f2f 86"[<common-diff-options>] <tree-ish> [<tree-ish>] [<path>...]\n"
50b8e355
CS
87" -r diff recursively\n"
88" --root include the initial commit as diff against /dev/null\n"
dda2d79a 89COMMON_DIFF_OPTIONS_HELP;
a8db165e 90
b4490059
JH
91static void diff_tree_tweak_rev(struct rev_info *rev, struct setup_revision_opt *opt)
92{
93 if (!rev->diffopt.output_format) {
94 if (rev->dense_combined_merges)
95 rev->diffopt.output_format = DIFF_FORMAT_PATCH;
96 else
97 rev->diffopt.output_format = DIFF_FORMAT_RAW;
98 }
99}
100
a633fca0 101int cmd_diff_tree(int argc, const char **argv, const char *prefix)
73134b6d 102{
e0965d83 103 char line[1000];
cd2bdc53
LT
104 struct object *tree1, *tree2;
105 static struct rev_info *opt = &log_tree_opt;
b4490059 106 struct setup_revision_opt s_r_opt;
5f1c3f07 107 int read_stdin = 0;
73134b6d 108
5a88f97c
JH
109 if (argc == 2 && !strcmp(argv[1], "-h"))
110 usage(diff_tree_usage);
111
37590ce3 112 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
2abf3503 113 repo_init_revisions(the_repository, opt, prefix);
fd66bcc3
BW
114 if (read_cache() < 0)
115 die(_("index file corrupt"));
8e8f9987 116 opt->abbrev = 0;
91539833 117 opt->diff = 1;
8b3dce56 118 opt->disable_stdin = 1;
b4490059
JH
119 memset(&s_r_opt, 0, sizeof(s_r_opt));
120 s_r_opt.tweak = diff_tree_tweak_rev;
90a78b83
AR
121
122 precompose_argv(argc, argv);
b4490059 123 argc = setup_revisions(argc, argv, opt, &s_r_opt);
6b5ee137 124
cd2bdc53
LT
125 while (--argc > 0) {
126 const char *arg = *++argv;
c5b42386 127
e0965d83
LT
128 if (!strcmp(arg, "--stdin")) {
129 read_stdin = 1;
130 continue;
131 }
c5bac17a 132 usage(diff_tree_usage);
73134b6d
LT
133 }
134
cd2bdc53 135 /*
8ba74bfd
JH
136 * NOTE! We expect "a..b" to expand to "^a b" but it is
137 * perfectly valid for revision range parser to yield "b ^a",
138 * which means the same thing. If we get the latter, i.e. the
139 * second one is marked UNINTERESTING, we recover the original
140 * order the user gave, i.e. "a..b", by swapping the trees.
cd2bdc53 141 */
315f49f2 142 switch (opt->pending.nr) {
0a8365a1
LT
143 case 0:
144 if (!read_stdin)
145 usage(diff_tree_usage);
146 break;
147 case 1:
1f1e895f 148 tree1 = opt->pending.objects[0].item;
315f49f2 149 diff_tree_commit_oid(&tree1->oid);
0a8365a1
LT
150 break;
151 case 2:
1f1e895f
LT
152 tree1 = opt->pending.objects[0].item;
153 tree2 = opt->pending.objects[1].item;
154 if (tree2->flags & UNINTERESTING) {
35d803bc 155 SWAP(tree2, tree1);
1f1e895f 156 }
66f414f8 157 diff_tree_oid(&tree1->oid, &tree2->oid, "", &opt->diffopt);
5f1c3f07 158 log_tree_diff_flush(opt);
0a8365a1
LT
159 break;
160 }
161
62c64895 162 if (read_stdin) {
f31027c9
JH
163 int saved_nrl = 0;
164 int saved_dcctc = 0;
165
ff7fe37b
NTND
166 if (opt->diffopt.detect_rename) {
167 if (!the_index.cache)
168 read_index(&the_index);
169 opt->diffopt.setup |= DIFF_SETUP_USE_SIZE_CACHE;
170 }
62c64895 171 while (fgets(line, sizeof(line), stdin)) {
5f5e936d 172 struct object_id oid;
e0965d83 173
5f5e936d 174 if (get_oid_hex(line, &oid)) {
62c64895
WC
175 fputs(line, stdout);
176 fflush(stdout);
177 }
f31027c9 178 else {
62c64895 179 diff_tree_stdin(line);
f31027c9
JH
180 if (saved_nrl < opt->diffopt.needed_rename_limit)
181 saved_nrl = opt->diffopt.needed_rename_limit;
182 if (opt->diffopt.degraded_cc_to_c)
183 saved_dcctc = 1;
184 }
e0c97ca6 185 }
f31027c9
JH
186 opt->diffopt.degraded_cc_to_c = saved_dcctc;
187 opt->diffopt.needed_rename_limit = saved_nrl;
e0c97ca6 188 }
da31b358
JH
189
190 return diff_result_code(&opt->diffopt, 0);
9174026c 191}