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