]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/diff-tree.c
t/README: A new section about test coverage
[thirdparty/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"
9174026c 6
cd2bdc53 7static struct rev_info log_tree_opt;
b11645be 8
45392a64
JH
9static int diff_tree_commit_sha1(const unsigned char *sha1)
10{
11 struct commit *commit = lookup_commit_reference(sha1);
12 if (!commit)
13 return -1;
5f1c3f07 14 return log_tree_commit(&log_tree_opt, commit);
45392a64
JH
15}
16
a57114c8
KW
17/* Diff one or more commits. */
18static int stdin_diff_commit(struct commit *commit, char *line, int len)
b11645be 19{
45392a64 20 unsigned char sha1[20];
45392a64
JH
21 if (isspace(line[40]) && !get_sha1_hex(line+41, sha1)) {
22 /* Graft the fake parents locally to the commit */
23 int pos = 41;
24 struct commit_list **pptr, *parents;
25
26 /* Free the real parent list */
27 for (parents = commit->parents; parents; ) {
28 struct commit_list *tmp = parents->next;
29 free(parents);
30 parents = tmp;
31 }
32 commit->parents = NULL;
33 pptr = &(commit->parents);
34 while (line[pos] && !get_sha1_hex(line + pos, sha1)) {
35 struct commit *parent = lookup_commit(sha1);
36 if (parent) {
37 pptr = &commit_list_insert(parent, pptr)->next;
38 }
39 pos += 41;
40 }
b11645be 41 }
5f1c3f07 42 return log_tree_commit(&log_tree_opt, commit);
e0965d83
LT
43}
44
140b378d
KW
45/* Diff two trees. */
46static int stdin_diff_trees(struct tree *tree1, char *line, int len)
47{
48 unsigned char sha1[20];
49 struct tree *tree2;
50 if (len != 82 || !isspace(line[40]) || get_sha1_hex(line + 41, sha1))
51 return error("Need exactly two trees, separated by a space");
52 tree2 = lookup_tree(sha1);
53 if (!tree2 || parse_tree(tree2))
54 return -1;
55 printf("%s %s\n", sha1_to_hex(tree1->object.sha1),
56 sha1_to_hex(tree2->object.sha1));
57 diff_tree_sha1(tree1->object.sha1, tree2->object.sha1,
58 "", &log_tree_opt.diffopt);
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);
66 unsigned char sha1[20];
140b378d 67 struct object *obj;
a57114c8
KW
68
69 if (!len || line[len-1] != '\n')
70 return -1;
71 line[len-1] = 0;
72 if (get_sha1_hex(line, sha1))
73 return -1;
628b06d7
JH
74 obj = lookup_unknown_object(sha1);
75 if (!obj || !obj->parsed)
76 obj = parse_object(sha1);
140b378d 77 if (!obj)
a57114c8 78 return -1;
140b378d
KW
79 if (obj->type == OBJ_COMMIT)
80 return stdin_diff_commit((struct commit *)obj, line, len);
81 if (obj->type == OBJ_TREE)
82 return stdin_diff_trees((struct tree *)obj, line, len);
83 error("Object %s is a %s, not a commit or tree",
84 sha1_to_hex(sha1), typename(obj->type));
85 return -1;
a57114c8
KW
86}
87
4d1f1190 88static const char diff_tree_usage[] =
1b1dd23f 89"git diff-tree [--stdin] [-m] [-c] [--cc] [-s] [-v] [--pretty] [-t] [-r] [--root] "
50b8e355
CS
90"[<common diff options>] <tree-ish> [<tree-ish>] [<path>...]\n"
91" -r diff recursively\n"
92" --root include the initial commit as diff against /dev/null\n"
dda2d79a 93COMMON_DIFF_OPTIONS_HELP;
a8db165e 94
b4490059
JH
95static void diff_tree_tweak_rev(struct rev_info *rev, struct setup_revision_opt *opt)
96{
97 if (!rev->diffopt.output_format) {
98 if (rev->dense_combined_merges)
99 rev->diffopt.output_format = DIFF_FORMAT_PATCH;
100 else
101 rev->diffopt.output_format = DIFF_FORMAT_RAW;
102 }
103}
104
a633fca0 105int cmd_diff_tree(int argc, const char **argv, const char *prefix)
73134b6d 106{
0a8365a1 107 int nr_sha1;
e0965d83 108 char line[1000];
cd2bdc53
LT
109 struct object *tree1, *tree2;
110 static struct rev_info *opt = &log_tree_opt;
b4490059 111 struct setup_revision_opt s_r_opt;
5f1c3f07 112 int read_stdin = 0;
73134b6d 113
a633fca0 114 init_revisions(opt, prefix);
ef90d6d4 115 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
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;
121 argc = setup_revisions(argc, argv, opt, &s_r_opt);
6b5ee137 122
cd2bdc53
LT
123 while (--argc > 0) {
124 const char *arg = *++argv;
c5b42386 125
e0965d83
LT
126 if (!strcmp(arg, "--stdin")) {
127 read_stdin = 1;
128 continue;
129 }
c5bac17a 130 usage(diff_tree_usage);
73134b6d
LT
131 }
132
cd2bdc53 133 /*
1f1e895f
LT
134 * NOTE! We expect "a ^b" to be equal to "a..b", so we
135 * reverse the order of the objects if the second one
136 * is marked UNINTERESTING.
cd2bdc53 137 */
1f1e895f 138 nr_sha1 = opt->pending.nr;
0a8365a1
LT
139 switch (nr_sha1) {
140 case 0:
141 if (!read_stdin)
142 usage(diff_tree_usage);
143 break;
144 case 1:
1f1e895f 145 tree1 = opt->pending.objects[0].item;
cd2bdc53 146 diff_tree_commit_sha1(tree1->sha1);
0a8365a1
LT
147 break;
148 case 2:
1f1e895f
LT
149 tree1 = opt->pending.objects[0].item;
150 tree2 = opt->pending.objects[1].item;
151 if (tree2->flags & UNINTERESTING) {
152 struct object *tmp = tree2;
153 tree2 = tree1;
154 tree1 = tmp;
155 }
cd2bdc53
LT
156 diff_tree_sha1(tree1->sha1,
157 tree2->sha1,
158 "", &opt->diffopt);
5f1c3f07 159 log_tree_diff_flush(opt);
0a8365a1
LT
160 break;
161 }
162
62c64895
WC
163 if (read_stdin) {
164 if (opt->diffopt.detect_rename)
165 opt->diffopt.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
166 DIFF_SETUP_USE_CACHE);
167 while (fgets(line, sizeof(line), stdin)) {
168 unsigned char sha1[20];
e0965d83 169
62c64895
WC
170 if (get_sha1_hex(line, sha1)) {
171 fputs(line, stdout);
172 fflush(stdout);
173 }
174 else
175 diff_tree_stdin(line);
e0c97ca6 176 }
e0c97ca6 177 }
da31b358
JH
178
179 return diff_result_code(&opt->diffopt, 0);
9174026c 180}