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