Finally implement "git log --follow"
[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
304de2d2 9static char *malloc_base(const char *base, int baselen, const char *path, int pathlen)
ac1b3d12 10{
ac1b3d12
LT
11 char *newbase = xmalloc(baselen + pathlen + 2);
12 memcpy(newbase, base, baselen);
13 memcpy(newbase + baselen, path, pathlen);
14 memcpy(newbase + baselen + pathlen, "/", 2);
15 return newbase;
16}
17
cf995ede 18static void show_entry(struct diff_options *opt, const char *prefix, struct tree_desc *desc,
304de2d2 19 const char *base, int baselen);
ac1b3d12 20
304de2d2 21static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2, const char *base, int baselen, struct diff_options *opt)
ac1b3d12
LT
22{
23 unsigned mode1, mode2;
24 const char *path1, *path2;
25 const unsigned char *sha1, *sha2;
26 int cmp, pathlen1, pathlen2;
27
50f9a858
LT
28 sha1 = tree_entry_extract(t1, &path1, &mode1);
29 sha2 = tree_entry_extract(t2, &path2, &mode2);
ac1b3d12 30
304de2d2
LT
31 pathlen1 = tree_entry_len(path1, sha1);
32 pathlen2 = tree_entry_len(path2, sha2);
ac1b3d12
LT
33 cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
34 if (cmp < 0) {
304de2d2 35 show_entry(opt, "-", t1, base, baselen);
ac1b3d12
LT
36 return -1;
37 }
38 if (cmp > 0) {
304de2d2 39 show_entry(opt, "+", t2, base, baselen);
ac1b3d12
LT
40 return 1;
41 }
a89fccd2 42 if (!opt->find_copies_harder && !hashcmp(sha1, sha2) && mode1 == mode2)
ac1b3d12
LT
43 return 0;
44
45 /*
46 * If the filemode has changed to/from a directory from/to a regular
47 * file, we need to consider it a remove and an add.
48 */
49 if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
304de2d2
LT
50 show_entry(opt, "-", t1, base, baselen);
51 show_entry(opt, "+", t2, base, baselen);
ac1b3d12
LT
52 return 0;
53 }
54
55 if (opt->recursive && S_ISDIR(mode1)) {
56 int retval;
304de2d2 57 char *newbase = malloc_base(base, baselen, path1, pathlen1);
ac1b3d12
LT
58 if (opt->tree_in_recursive)
59 opt->change(opt, mode1, mode2,
60 sha1, sha2, base, path1);
61 retval = diff_tree_sha1(sha1, sha2, newbase, opt);
62 free(newbase);
63 return retval;
64 }
65
66 opt->change(opt, mode1, mode2, sha1, sha2, base, path1);
67 return 0;
68}
69
5d865017
LT
70/*
71 * Is a tree entry interesting given the pathspec we have?
72 *
73 * Return:
1d848f64
JH
74 * - 2 for "yes, and all subsequent entries will be"
75 * - 1 for yes
5d865017
LT
76 * - zero for no
77 * - negative for "no, and no subsequent entries will be either"
78 */
79static int tree_entry_interesting(struct tree_desc *desc, const char *base, int baselen, struct diff_options *opt)
ac1b3d12
LT
80{
81 const char *path;
304de2d2 82 const unsigned char *sha1;
ac1b3d12
LT
83 unsigned mode;
84 int i;
304de2d2 85 int pathlen;
7d2f667b 86 int never_interesting = -1;
ac1b3d12 87
a8baa7b9 88 if (!opt->nr_paths)
ac1b3d12
LT
89 return 1;
90
304de2d2 91 sha1 = tree_entry_extract(desc, &path, &mode);
ac1b3d12 92
304de2d2 93 pathlen = tree_entry_len(path, sha1);
ac1b3d12 94
7d2f667b 95 for (i = 0; i < opt->nr_paths; i++) {
a8baa7b9
JH
96 const char *match = opt->paths[i];
97 int matchlen = opt->pathlens[i];
ccc744ab 98 int m = -1; /* signals that we haven't called strncmp() */
ac1b3d12
LT
99
100 if (baselen >= matchlen) {
101 /* If it doesn't match, move along... */
102 if (strncmp(base, match, matchlen))
103 continue;
104
1d848f64
JH
105 /*
106 * The base is a subdirectory of a path which
107 * was specified, so all of them are interesting.
108 */
109 return 2;
ac1b3d12
LT
110 }
111
112 /* Does the base match? */
113 if (strncmp(base, match, baselen))
114 continue;
115
116 match += baselen;
117 matchlen -= baselen;
118
ccc744ab
JH
119 if (never_interesting) {
120 /*
121 * We have not seen any match that sorts later
122 * than the current path.
123 */
124
125 /*
126 * Does match sort strictly earlier than path
127 * with their common parts?
128 */
129 m = strncmp(match, path,
130 (matchlen < pathlen) ? matchlen : pathlen);
131 if (m < 0)
132 continue;
7d2f667b 133
ccc744ab
JH
134 /*
135 * If we come here even once, that means there is at
136 * least one pathspec that would sort equal to or
137 * later than the path we are currently looking at.
138 * In other words, if we have never reached this point
139 * after iterating all pathspecs, it means all
140 * pathspecs are either outside of base, or inside the
141 * base but sorts strictly earlier than the current
142 * one. In either case, they will never match the
143 * subsequent entries. In such a case, we initialized
144 * the variable to -1 and that is what will be
145 * returned, allowing the caller to terminate early.
146 */
147 never_interesting = 0;
148 }
7d2f667b 149
ac1b3d12
LT
150 if (pathlen > matchlen)
151 continue;
152
153 if (matchlen > pathlen) {
154 if (match[pathlen] != '/')
155 continue;
156 if (!S_ISDIR(mode))
157 continue;
158 }
159
ccc744ab
JH
160 if (m == -1)
161 /*
162 * we cheated and did not do strncmp(), so we do
163 * that here.
164 */
165 m = strncmp(match, path, pathlen);
166
7d2f667b
JH
167 /*
168 * If common part matched earlier then it is a hit,
169 * because we rejected the case where path is not a
170 * leading directory and is shorter than match.
171 */
172 if (!m)
173 return 1;
ac1b3d12 174 }
7d2f667b 175 return never_interesting; /* No matches */
ac1b3d12
LT
176}
177
178/* A whole sub-tree went away or appeared */
304de2d2 179static void show_tree(struct diff_options *opt, const char *prefix, struct tree_desc *desc, const char *base, int baselen)
ac1b3d12 180{
1d848f64 181 int all_interesting = 0;
ac1b3d12 182 while (desc->size) {
1d848f64
JH
183 int show;
184
185 if (all_interesting)
186 show = 1;
187 else {
188 show = tree_entry_interesting(desc, base, baselen,
189 opt);
190 if (show == 2)
191 all_interesting = 1;
192 }
5d865017
LT
193 if (show < 0)
194 break;
195 if (show)
304de2d2 196 show_entry(opt, prefix, desc, base, baselen);
ac1b3d12
LT
197 update_tree_entry(desc);
198 }
199}
200
201/* A file entry went away or appeared */
cf995ede 202static void show_entry(struct diff_options *opt, const char *prefix, struct tree_desc *desc,
304de2d2 203 const char *base, int baselen)
ac1b3d12
LT
204{
205 unsigned mode;
206 const char *path;
50f9a858 207 const unsigned char *sha1 = tree_entry_extract(desc, &path, &mode);
ac1b3d12
LT
208
209 if (opt->recursive && S_ISDIR(mode)) {
21666f1a 210 enum object_type type;
304de2d2
LT
211 int pathlen = tree_entry_len(path, sha1);
212 char *newbase = malloc_base(base, baselen, path, pathlen);
ac1b3d12
LT
213 struct tree_desc inner;
214 void *tree;
6fda5e51 215 unsigned long size;
ac1b3d12 216
6fda5e51 217 tree = read_sha1_file(sha1, &type, &size);
21666f1a 218 if (!tree || type != OBJ_TREE)
ac1b3d12
LT
219 die("corrupt tree sha %s", sha1_to_hex(sha1));
220
6fda5e51 221 init_tree_desc(&inner, tree, size);
304de2d2 222 show_tree(opt, prefix, &inner, newbase, baselen + 1 + pathlen);
ac1b3d12
LT
223
224 free(tree);
225 free(newbase);
cf995ede
DR
226 } else {
227 opt->add_remove(opt, prefix[0], mode, sha1, base, path);
ac1b3d12 228 }
ac1b3d12
LT
229}
230
5d865017
LT
231static void skip_uninteresting(struct tree_desc *t, const char *base, int baselen, struct diff_options *opt)
232{
1d848f64 233 int all_interesting = 0;
5d865017 234 while (t->size) {
1d848f64
JH
235 int show;
236
237 if (all_interesting)
238 show = 1;
239 else {
240 show = tree_entry_interesting(t, base, baselen, opt);
241 if (show == 2)
242 all_interesting = 1;
243 }
5d865017
LT
244 if (!show) {
245 update_tree_entry(t);
246 continue;
247 }
248 /* Skip it all? */
249 if (show < 0)
250 t->size = 0;
251 return;
252 }
253}
254
ac1b3d12
LT
255int diff_tree(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt)
256{
304de2d2
LT
257 int baselen = strlen(base);
258
5d865017 259 for (;;) {
822cac01
JH
260 if (opt->quiet && opt->has_changes)
261 break;
5d865017
LT
262 if (opt->nr_paths) {
263 skip_uninteresting(t1, base, baselen, opt);
264 skip_uninteresting(t2, base, baselen, opt);
ac1b3d12
LT
265 }
266 if (!t1->size) {
5d865017
LT
267 if (!t2->size)
268 break;
304de2d2 269 show_entry(opt, "+", t2, base, baselen);
ac1b3d12
LT
270 update_tree_entry(t2);
271 continue;
272 }
273 if (!t2->size) {
304de2d2 274 show_entry(opt, "-", t1, base, baselen);
ac1b3d12
LT
275 update_tree_entry(t1);
276 continue;
277 }
304de2d2 278 switch (compare_tree_entry(t1, t2, base, baselen, opt)) {
ac1b3d12
LT
279 case -1:
280 update_tree_entry(t1);
281 continue;
282 case 0:
283 update_tree_entry(t1);
284 /* Fallthrough */
285 case 1:
286 update_tree_entry(t2);
287 continue;
288 }
289 die("git-diff-tree: internal error");
290 }
291 return 0;
292}
293
750f7b66
LT
294/*
295 * Does it look like the resulting diff might be due to a rename?
296 * - single entry
297 * - not a valid previous file
298 */
299static inline int diff_might_be_rename(void)
300{
301 return diff_queued_diff.nr == 1 &&
302 !DIFF_FILE_VALID(diff_queued_diff.queue[0]->one);
303}
304
305static void try_to_follow_renames(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt)
306{
307 struct diff_options diff_opts;
308 const char *paths[2];
309 int i;
310
311 diff_setup(&diff_opts);
312 diff_opts.recursive = 1;
313 diff_opts.detect_rename = DIFF_DETECT_RENAME;
314 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
315 diff_opts.single_follow = opt->paths[0];
316 paths[0] = NULL;
317 diff_tree_setup_paths(paths, &diff_opts);
318 if (diff_setup_done(&diff_opts) < 0)
319 die("unable to set up diff options to follow renames");
320 diff_tree(t1, t2, base, &diff_opts);
321 diffcore_std(&diff_opts);
322
323 /* NOTE! Ignore the first diff! That was the old one! */
324 for (i = 1; i < diff_queued_diff.nr; i++) {
325 struct diff_filepair *p = diff_queued_diff.queue[i];
326
327 /*
328 * Found a source? Not only do we use that for the new
329 * diff_queued_diff, we also use that as the path in
330 * the future!
331 */
332 if ((p->status == 'R' || p->status == 'C') && !strcmp(p->two->path, opt->paths[0])) {
333 diff_queued_diff.queue[0] = p;
334 opt->paths[0] = xstrdup(p->one->path);
335 diff_tree_setup_paths(opt->paths, opt);
336 break;
337 }
338 }
339
340 /*
341 * Then, ignore any but the first entry! It might be the old one,
342 * or it might be the rename/copy we found
343 */
344 diff_queued_diff.nr = 1;
345}
346
ac1b3d12
LT
347int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base, struct diff_options *opt)
348{
349 void *tree1, *tree2;
350 struct tree_desc t1, t2;
6fda5e51 351 unsigned long size1, size2;
ac1b3d12
LT
352 int retval;
353
6fda5e51 354 tree1 = read_object_with_reference(old, tree_type, &size1, NULL);
ac1b3d12
LT
355 if (!tree1)
356 die("unable to read source tree (%s)", sha1_to_hex(old));
6fda5e51 357 tree2 = read_object_with_reference(new, tree_type, &size2, NULL);
ac1b3d12
LT
358 if (!tree2)
359 die("unable to read destination tree (%s)", sha1_to_hex(new));
6fda5e51
LT
360 init_tree_desc(&t1, tree1, size1);
361 init_tree_desc(&t2, tree2, size2);
ac1b3d12 362 retval = diff_tree(&t1, &t2, base, opt);
750f7b66
LT
363 if (opt->follow_renames && diff_might_be_rename()) {
364 init_tree_desc(&t1, tree1, size1);
365 init_tree_desc(&t2, tree2, size2);
366 try_to_follow_renames(&t1, &t2, base, opt);
367 }
ac1b3d12
LT
368 free(tree1);
369 free(tree2);
370 return retval;
371}
372
2b60356d
RS
373int diff_root_tree_sha1(const unsigned char *new, const char *base, struct diff_options *opt)
374{
375 int retval;
376 void *tree;
6fda5e51 377 unsigned long size;
2b60356d
RS
378 struct tree_desc empty, real;
379
6fda5e51 380 tree = read_object_with_reference(new, tree_type, &size, NULL);
2b60356d
RS
381 if (!tree)
382 die("unable to read root tree (%s)", sha1_to_hex(new));
6fda5e51 383 init_tree_desc(&real, tree, size);
2b60356d 384
6fda5e51 385 init_tree_desc(&empty, "", 0);
2b60356d
RS
386 retval = diff_tree(&empty, &real, base, opt);
387 free(tree);
388 return retval;
389}
390
ac1b3d12
LT
391static int count_paths(const char **paths)
392{
393 int i = 0;
394 while (*paths++)
395 i++;
396 return i;
397}
398
a8baa7b9 399void diff_tree_release_paths(struct diff_options *opt)
ac1b3d12 400{
a8baa7b9
JH
401 free(opt->pathlens);
402}
403
404void diff_tree_setup_paths(const char **p, struct diff_options *opt)
405{
406 opt->nr_paths = 0;
407 opt->pathlens = NULL;
408 opt->paths = NULL;
409
ac1b3d12
LT
410 if (p) {
411 int i;
412
a8baa7b9
JH
413 opt->paths = p;
414 opt->nr_paths = count_paths(p);
415 if (opt->nr_paths == 0) {
416 opt->pathlens = NULL;
7e4a2a84
JH
417 return;
418 }
a8baa7b9
JH
419 opt->pathlens = xmalloc(opt->nr_paths * sizeof(int));
420 for (i=0; i < opt->nr_paths; i++)
421 opt->pathlens[i] = strlen(p[i]);
ac1b3d12
LT
422 }
423}