tree-diff: show_tree() is not needed
[git/git.git] / tree-diff.c
CommitLineData
ac1b3d12
LT
1/*
2 * Helper functions for tree diff generation
3 */
4#include "cache.h"
5#include "diff.h"
750f7b66 6#include "diffcore.h"
8e440259 7#include "tree.h"
ac1b3d12 8
48932677
NTND
9static void show_entry(struct diff_options *opt, const char *prefix,
10 struct tree_desc *desc, struct strbuf *base);
ac1b3d12 11
48932677
NTND
12static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2,
13 struct strbuf *base, struct diff_options *opt)
ac1b3d12
LT
14{
15 unsigned mode1, mode2;
16 const char *path1, *path2;
17 const unsigned char *sha1, *sha2;
18 int cmp, pathlen1, pathlen2;
48932677 19 int old_baselen = base->len;
ac1b3d12 20
50f9a858
LT
21 sha1 = tree_entry_extract(t1, &path1, &mode1);
22 sha2 = tree_entry_extract(t2, &path2, &mode2);
ac1b3d12 23
0de16337
NTND
24 pathlen1 = tree_entry_len(&t1->entry);
25 pathlen2 = tree_entry_len(&t2->entry);
e197c2b6
KS
26
27 /*
28 * NOTE files and directories *always* compare differently,
29 * even when having the same name.
30 */
ac1b3d12
LT
31 cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
32 if (cmp < 0) {
48932677 33 show_entry(opt, "-", t1, base);
ac1b3d12
LT
34 return -1;
35 }
36 if (cmp > 0) {
48932677 37 show_entry(opt, "+", t2, base);
ac1b3d12
LT
38 return 1;
39 }
8f67f8ae 40 if (!DIFF_OPT_TST(opt, FIND_COPIES_HARDER) && !hashcmp(sha1, sha2) && mode1 == mode2)
ac1b3d12
LT
41 return 0;
42
48932677 43 strbuf_add(base, path1, pathlen1);
8f67f8ae 44 if (DIFF_OPT_TST(opt, RECURSIVE) && S_ISDIR(mode1)) {
fd55a19e 45 if (DIFF_OPT_TST(opt, TREE_IN_RECURSIVE)) {
ac1b3d12 46 opt->change(opt, mode1, mode2,
e5450100 47 sha1, sha2, 1, 1, base->buf, 0, 0);
fd55a19e 48 }
48932677 49 strbuf_addch(base, '/');
c0aa335c 50 diff_tree_sha1(sha1, sha2, base->buf, opt);
48932677 51 } else {
e5450100 52 opt->change(opt, mode1, mode2, sha1, sha2, 1, 1, base->buf, 0, 0);
ac1b3d12 53 }
48932677 54 strbuf_setlen(base, old_baselen);
ac1b3d12
LT
55 return 0;
56}
57
7e9003c1 58/* An entry went away or appeared */
48932677
NTND
59static void show_entry(struct diff_options *opt, const char *prefix,
60 struct tree_desc *desc, struct strbuf *base)
ac1b3d12
LT
61{
62 unsigned mode;
63 const char *path;
50f9a858 64 const unsigned char *sha1 = tree_entry_extract(desc, &path, &mode);
0de16337 65 int pathlen = tree_entry_len(&desc->entry);
48932677 66 int old_baselen = base->len;
ac1b3d12 67
48932677 68 strbuf_add(base, path, pathlen);
8f67f8ae 69 if (DIFF_OPT_TST(opt, RECURSIVE) && S_ISDIR(mode)) {
48932677 70 if (DIFF_OPT_TST(opt, TREE_IN_RECURSIVE))
e5450100 71 opt->add_remove(opt, *prefix, mode, sha1, 1, base->buf, 0);
df533f34 72
48932677 73 strbuf_addch(base, '/');
7e9003c1
KS
74 diff_tree_sha1(*prefix == '-' ? sha1 : NULL,
75 *prefix == '+' ? sha1 : NULL, base->buf, opt);
48932677 76 } else
e5450100 77 opt->add_remove(opt, prefix[0], mode, sha1, 1, base->buf, 0);
48932677
NTND
78
79 strbuf_setlen(base, old_baselen);
ac1b3d12
LT
80}
81
48932677 82static void skip_uninteresting(struct tree_desc *t, struct strbuf *base,
e9066121 83 struct diff_options *opt)
5d865017 84{
e9066121
KS
85 enum interesting match;
86
5d865017 87 while (t->size) {
e9066121
KS
88 match = tree_entry_interesting(&t->entry, base, 0, &opt->pathspec);
89 if (match) {
90 if (match == all_entries_not_interesting)
97d0b74a
NTND
91 t->size = 0;
92 break;
5d865017 93 }
97d0b74a 94 update_tree_entry(t);
5d865017
LT
95 }
96}
97
48932677
NTND
98int diff_tree(struct tree_desc *t1, struct tree_desc *t2,
99 const char *base_str, struct diff_options *opt)
ac1b3d12 100{
48932677
NTND
101 struct strbuf base;
102 int baselen = strlen(base_str);
304de2d2 103
bc96cc87
NTND
104 /* Enable recursion indefinitely */
105 opt->pathspec.recursive = DIFF_OPT_TST(opt, RECURSIVE);
bc96cc87 106
48932677
NTND
107 strbuf_init(&base, PATH_MAX);
108 strbuf_add(&base, base_str, baselen);
109
5d865017 110 for (;;) {
28b9264d 111 if (diff_can_quit_early(opt))
822cac01 112 break;
66f13625 113 if (opt->pathspec.nr) {
e9066121
KS
114 skip_uninteresting(t1, &base, opt);
115 skip_uninteresting(t2, &base, opt);
ac1b3d12
LT
116 }
117 if (!t1->size) {
5d865017
LT
118 if (!t2->size)
119 break;
48932677 120 show_entry(opt, "+", t2, &base);
ac1b3d12
LT
121 update_tree_entry(t2);
122 continue;
123 }
124 if (!t2->size) {
48932677 125 show_entry(opt, "-", t1, &base);
ac1b3d12
LT
126 update_tree_entry(t1);
127 continue;
128 }
48932677 129 switch (compare_tree_entry(t1, t2, &base, opt)) {
ac1b3d12
LT
130 case -1:
131 update_tree_entry(t1);
132 continue;
133 case 0:
134 update_tree_entry(t1);
135 /* Fallthrough */
136 case 1:
137 update_tree_entry(t2);
138 continue;
139 }
7e44c935 140 die("git diff-tree: internal error");
ac1b3d12 141 }
48932677
NTND
142
143 strbuf_release(&base);
ac1b3d12
LT
144 return 0;
145}
146
750f7b66
LT
147/*
148 * Does it look like the resulting diff might be due to a rename?
149 * - single entry
150 * - not a valid previous file
151 */
152static inline int diff_might_be_rename(void)
153{
154 return diff_queued_diff.nr == 1 &&
155 !DIFF_FILE_VALID(diff_queued_diff.queue[0]->one);
156}
157
158static void try_to_follow_renames(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt)
159{
160 struct diff_options diff_opts;
9f38e1ef
LT
161 struct diff_queue_struct *q = &diff_queued_diff;
162 struct diff_filepair *choice;
750f7b66
LT
163 int i;
164
8f4f8f45
NTND
165 /*
166 * follow-rename code is very specific, we need exactly one
167 * path. Magic that matches more than one path is not
168 * supported.
169 */
5c6933d2 170 GUARD_PATHSPEC(&opt->pathspec, PATHSPEC_FROMTOP | PATHSPEC_LITERAL);
8f4f8f45
NTND
171#if 0
172 /*
173 * We should reject wildcards as well. Unfortunately we
174 * haven't got a reliable way to detect that 'foo\*bar' in
175 * fact has no wildcards. nowildcard_len is merely a hint for
176 * optimization. Let it slip for now until wildmatch is taught
177 * about dry-run mode and returns wildcard info.
178 */
179 if (opt->pathspec.has_wildcard)
180 die("BUG:%s:%d: wildcards are not supported",
181 __FILE__, __LINE__);
182#endif
183
9f38e1ef
LT
184 /* Remove the file creation entry from the diff queue, and remember it */
185 choice = q->queue[0];
186 q->nr = 0;
187
750f7b66 188 diff_setup(&diff_opts);
8f67f8ae 189 DIFF_OPT_SET(&diff_opts, RECURSIVE);
0cdca133 190 DIFF_OPT_SET(&diff_opts, FIND_COPIES_HARDER);
750f7b66 191 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
61588ccf 192 diff_opts.single_follow = opt->pathspec.items[0].match;
6dd4b66f 193 diff_opts.break_opt = opt->break_opt;
dd98d88b 194 diff_opts.rename_score = opt->rename_score;
28452655 195 diff_setup_done(&diff_opts);
750f7b66
LT
196 diff_tree(t1, t2, base, &diff_opts);
197 diffcore_std(&diff_opts);
bd1928df 198 free_pathspec(&diff_opts.pathspec);
750f7b66 199
9f38e1ef 200 /* Go through the new set of filepairing, and see if we find a more interesting one */
44c48a90 201 opt->found_follow = 0;
9f38e1ef
LT
202 for (i = 0; i < q->nr; i++) {
203 struct diff_filepair *p = q->queue[i];
750f7b66
LT
204
205 /*
206 * Found a source? Not only do we use that for the new
9f38e1ef 207 * diff_queued_diff, we will also use that as the path in
750f7b66
LT
208 * the future!
209 */
66f13625 210 if ((p->status == 'R' || p->status == 'C') &&
61588ccf 211 !strcmp(p->two->path, opt->pathspec.items[0].match)) {
9a087274
NTND
212 const char *path[2];
213
9f38e1ef
LT
214 /* Switch the file-pairs around */
215 q->queue[i] = choice;
216 choice = p;
217
218 /* Update the path we use from now on.. */
9a087274
NTND
219 path[0] = p->one->path;
220 path[1] = NULL;
bd1928df 221 free_pathspec(&opt->pathspec);
4a2d5ae2
NTND
222 parse_pathspec(&opt->pathspec,
223 PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
224 PATHSPEC_LITERAL_PATH, "", path);
44c48a90
JH
225
226 /*
227 * The caller expects us to return a set of vanilla
228 * filepairs to let a later call to diffcore_std()
229 * it makes to sort the renames out (among other
230 * things), but we already have found renames
231 * ourselves; signal diffcore_std() not to muck with
232 * rename information.
233 */
234 opt->found_follow = 1;
750f7b66
LT
235 break;
236 }
237 }
238
239 /*
3ea3c215 240 * Then, discard all the non-relevant file pairs...
9f38e1ef
LT
241 */
242 for (i = 0; i < q->nr; i++) {
243 struct diff_filepair *p = q->queue[i];
244 diff_free_filepair(p);
245 }
246
247 /*
248 * .. and re-instate the one we want (which might be either the
249 * original one, or the rename/copy we found)
750f7b66 250 */
9f38e1ef
LT
251 q->queue[0] = choice;
252 q->nr = 1;
750f7b66
LT
253}
254
ac1b3d12
LT
255int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base, struct diff_options *opt)
256{
257 void *tree1, *tree2;
258 struct tree_desc t1, t2;
6fda5e51 259 unsigned long size1, size2;
ac1b3d12
LT
260 int retval;
261
79130328
KS
262 tree1 = fill_tree_descriptor(&t1, old);
263 tree2 = fill_tree_descriptor(&t2, new);
264 size1 = t1.size;
265 size2 = t2.size;
ac1b3d12 266 retval = diff_tree(&t1, &t2, base, opt);
39f75d26 267 if (!*base && DIFF_OPT_TST(opt, FOLLOW_RENAMES) && diff_might_be_rename()) {
750f7b66
LT
268 init_tree_desc(&t1, tree1, size1);
269 init_tree_desc(&t2, tree2, size2);
270 try_to_follow_renames(&t1, &t2, base, opt);
271 }
ac1b3d12
LT
272 free(tree1);
273 free(tree2);
274 return retval;
275}
276
2b60356d
RS
277int diff_root_tree_sha1(const unsigned char *new, const char *base, struct diff_options *opt)
278{
0b707c33 279 return diff_tree_sha1(NULL, new, base, opt);
2b60356d 280}