use SWAP macro
[git/git.git] / builtin / diff-tree.c
CommitLineData
9174026c 1#include "cache.h"
3ebfd4aa 2#include "diff.h"
e3bc7a3b 3#include "commit.h"
5f1c3f07 4#include "log-tree.h"
e8cc9cd9 5#include "builtin.h"
302ad7a9 6#include "submodule.h"
9174026c 7
cd2bdc53 8static struct rev_info log_tree_opt;
b11645be 9
45392a64
JH
10static int diff_tree_commit_sha1(const unsigned char *sha1)
11{
12 struct commit *commit = lookup_commit_reference(sha1);
13 if (!commit)
14 return -1;
5f1c3f07 15 return log_tree_commit(&log_tree_opt, commit);
45392a64
JH
16}
17
a57114c8
KW
18/* Diff one or more commits. */
19static int stdin_diff_commit(struct commit *commit, char *line, int len)
b11645be 20{
45392a64 21 unsigned char sha1[20];
45392a64
JH
22 if (isspace(line[40]) && !get_sha1_hex(line+41, sha1)) {
23 /* Graft the fake parents locally to the commit */
24 int pos = 41;
4602f1a4 25 struct commit_list **pptr;
45392a64
JH
26
27 /* Free the real parent list */
4602f1a4 28 free_commit_list(commit->parents);
45392a64
JH
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;
35 }
36 pos += 41;
37 }
b11645be 38 }
5f1c3f07 39 return log_tree_commit(&log_tree_opt, commit);
e0965d83
LT
40}
41
140b378d
KW
42/* Diff two trees. */
43static int stdin_diff_trees(struct tree *tree1, char *line, int len)
44{
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;
f2fd0760 52 printf("%s %s\n", oid_to_hex(&tree1->object.oid),
53 oid_to_hex(&tree2->object.oid));
ed1c9977 54 diff_tree_sha1(tree1->object.oid.hash, tree2->object.oid.hash,
140b378d
KW
55 "", &log_tree_opt.diffopt);
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);
63 unsigned char sha1[20];
140b378d 64 struct object *obj;
a57114c8
KW
65
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;
fa960826 71 obj = parse_object(sha1);
140b378d 72 if (!obj)
a57114c8 73 return -1;
140b378d
KW
74 if (obj->type == OBJ_COMMIT)
75 return stdin_diff_commit((struct commit *)obj, line, len);
76 if (obj->type == OBJ_TREE)
77 return stdin_diff_trees((struct tree *)obj, line, len);
78 error("Object %s is a %s, not a commit or tree",
79 sha1_to_hex(sha1), typename(obj->type));
80 return -1;
a57114c8
KW
81}
82
4d1f1190 83static const char diff_tree_usage[] =
1b1dd23f 84"git diff-tree [--stdin] [-m] [-c] [--cc] [-s] [-v] [--pretty] [-t] [-r] [--root] "
9c9b4f2f 85"[<common-diff-options>] <tree-ish> [<tree-ish>] [<path>...]\n"
50b8e355
CS
86" -r diff recursively\n"
87" --root include the initial commit as diff against /dev/null\n"
dda2d79a 88COMMON_DIFF_OPTIONS_HELP;
a8db165e 89
b4490059
JH
90static void diff_tree_tweak_rev(struct rev_info *rev, struct setup_revision_opt *opt)
91{
92 if (!rev->diffopt.output_format) {
93 if (rev->dense_combined_merges)
94 rev->diffopt.output_format = DIFF_FORMAT_PATCH;
95 else
96 rev->diffopt.output_format = DIFF_FORMAT_RAW;
97 }
98}
99
a633fca0 100int cmd_diff_tree(int argc, const char **argv, const char *prefix)
73134b6d 101{
0a8365a1 102 int nr_sha1;
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
a633fca0 109 init_revisions(opt, prefix);
302ad7a9 110 gitmodules_config();
ef90d6d4 111 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
8e8f9987 112 opt->abbrev = 0;
91539833 113 opt->diff = 1;
8b3dce56 114 opt->disable_stdin = 1;
b4490059
JH
115 memset(&s_r_opt, 0, sizeof(s_r_opt));
116 s_r_opt.tweak = diff_tree_tweak_rev;
90a78b83
AR
117
118 precompose_argv(argc, argv);
b4490059 119 argc = setup_revisions(argc, argv, opt, &s_r_opt);
6b5ee137 120
cd2bdc53
LT
121 while (--argc > 0) {
122 const char *arg = *++argv;
c5b42386 123
e0965d83
LT
124 if (!strcmp(arg, "--stdin")) {
125 read_stdin = 1;
126 continue;
127 }
c5bac17a 128 usage(diff_tree_usage);
73134b6d
LT
129 }
130
cd2bdc53 131 /*
1f1e895f
LT
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.
cd2bdc53 135 */
1f1e895f 136 nr_sha1 = opt->pending.nr;
0a8365a1
LT
137 switch (nr_sha1) {
138 case 0:
139 if (!read_stdin)
140 usage(diff_tree_usage);
141 break;
142 case 1:
1f1e895f 143 tree1 = opt->pending.objects[0].item;
ed1c9977 144 diff_tree_commit_sha1(tree1->oid.hash);
0a8365a1
LT
145 break;
146 case 2:
1f1e895f
LT
147 tree1 = opt->pending.objects[0].item;
148 tree2 = opt->pending.objects[1].item;
149 if (tree2->flags & UNINTERESTING) {
35d803bc 150 SWAP(tree2, tree1);
1f1e895f 151 }
ed1c9977 152 diff_tree_sha1(tree1->oid.hash,
153 tree2->oid.hash,
cd2bdc53 154 "", &opt->diffopt);
5f1c3f07 155 log_tree_diff_flush(opt);
0a8365a1
LT
156 break;
157 }
158
62c64895 159 if (read_stdin) {
f31027c9
JH
160 int saved_nrl = 0;
161 int saved_dcctc = 0;
162
62c64895
WC
163 if (opt->diffopt.detect_rename)
164 opt->diffopt.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
165 DIFF_SETUP_USE_CACHE);
166 while (fgets(line, sizeof(line), stdin)) {
167 unsigned char sha1[20];
e0965d83 168
62c64895
WC
169 if (get_sha1_hex(line, sha1)) {
170 fputs(line, stdout);
171 fflush(stdout);
172 }
f31027c9 173 else {
62c64895 174 diff_tree_stdin(line);
f31027c9
JH
175 if (saved_nrl < opt->diffopt.needed_rename_limit)
176 saved_nrl = opt->diffopt.needed_rename_limit;
177 if (opt->diffopt.degraded_cc_to_c)
178 saved_dcctc = 1;
179 }
e0c97ca6 180 }
f31027c9
JH
181 opt->diffopt.degraded_cc_to_c = saved_dcctc;
182 opt->diffopt.needed_rename_limit = saved_nrl;
e0c97ca6 183 }
da31b358
JH
184
185 return diff_result_code(&opt->diffopt, 0);
9174026c 186}