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