Merge branch 'maint'
[git/kirr.git] / tree-diff.c
blobcd659c6fe447b6fcdedee2843113feb358f23849
1 /*
2 * Helper functions for tree diff generation
3 */
4 #include "cache.h"
5 #include "diff.h"
6 #include "diffcore.h"
7 #include "tree.h"
9 static char *malloc_base(const char *base, int baselen, const char *path, int pathlen)
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;
18 static char *malloc_fullname(const char *base, int baselen, const char *path, int pathlen)
20 char *fullname = xmalloc(baselen + pathlen + 1);
21 memcpy(fullname, base, baselen);
22 memcpy(fullname + baselen, path, pathlen);
23 fullname[baselen + pathlen] = 0;
24 return fullname;
27 static void show_entry(struct diff_options *opt, const char *prefix, struct tree_desc *desc,
28 const char *base, int baselen);
30 static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2, const char *base, int baselen, struct diff_options *opt)
32 unsigned mode1, mode2;
33 const char *path1, *path2;
34 const unsigned char *sha1, *sha2;
35 int cmp, pathlen1, pathlen2;
36 char *fullname;
38 sha1 = tree_entry_extract(t1, &path1, &mode1);
39 sha2 = tree_entry_extract(t2, &path2, &mode2);
41 pathlen1 = tree_entry_len(path1, sha1);
42 pathlen2 = tree_entry_len(path2, sha2);
43 cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
44 if (cmp < 0) {
45 show_entry(opt, "-", t1, base, baselen);
46 return -1;
48 if (cmp > 0) {
49 show_entry(opt, "+", t2, base, baselen);
50 return 1;
52 if (!DIFF_OPT_TST(opt, FIND_COPIES_HARDER) && !hashcmp(sha1, sha2) && mode1 == mode2)
53 return 0;
56 * If the filemode has changed to/from a directory from/to a regular
57 * file, we need to consider it a remove and an add.
59 if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
60 show_entry(opt, "-", t1, base, baselen);
61 show_entry(opt, "+", t2, base, baselen);
62 return 0;
65 if (DIFF_OPT_TST(opt, RECURSIVE) && S_ISDIR(mode1)) {
66 int retval;
67 char *newbase = malloc_base(base, baselen, path1, pathlen1);
68 if (DIFF_OPT_TST(opt, TREE_IN_RECURSIVE)) {
69 newbase[baselen + pathlen1] = 0;
70 opt->change(opt, mode1, mode2,
71 sha1, sha2, newbase, 0, 0);
72 newbase[baselen + pathlen1] = '/';
74 retval = diff_tree_sha1(sha1, sha2, newbase, opt);
75 free(newbase);
76 return retval;
79 fullname = malloc_fullname(base, baselen, path1, pathlen1);
80 opt->change(opt, mode1, mode2, sha1, sha2, fullname, 0, 0);
81 free(fullname);
82 return 0;
86 * Is a tree entry interesting given the pathspec we have?
88 * Return:
89 * - 2 for "yes, and all subsequent entries will be"
90 * - 1 for yes
91 * - zero for no
92 * - negative for "no, and no subsequent entries will be either"
94 static int tree_entry_interesting(struct tree_desc *desc, const char *base, int baselen, struct diff_options *opt)
96 const char *path;
97 const unsigned char *sha1;
98 unsigned mode;
99 int i;
100 int pathlen;
101 int never_interesting = -1;
103 if (!opt->nr_paths)
104 return 1;
106 sha1 = tree_entry_extract(desc, &path, &mode);
108 pathlen = tree_entry_len(path, sha1);
110 for (i = 0; i < opt->nr_paths; i++) {
111 const char *match = opt->paths[i];
112 int matchlen = opt->pathlens[i];
113 int m = -1; /* signals that we haven't called strncmp() */
115 if (baselen >= matchlen) {
116 /* If it doesn't match, move along... */
117 if (strncmp(base, match, matchlen))
118 continue;
121 * If the base is a subdirectory of a path which
122 * was specified, all of them are interesting.
124 if (!matchlen ||
125 base[matchlen] == '/' ||
126 match[matchlen - 1] == '/')
127 return 2;
129 /* Just a random prefix match */
130 continue;
133 /* Does the base match? */
134 if (strncmp(base, match, baselen))
135 continue;
137 match += baselen;
138 matchlen -= baselen;
140 if (never_interesting) {
142 * We have not seen any match that sorts later
143 * than the current path.
147 * Does match sort strictly earlier than path
148 * with their common parts?
150 m = strncmp(match, path,
151 (matchlen < pathlen) ? matchlen : pathlen);
152 if (m < 0)
153 continue;
156 * If we come here even once, that means there is at
157 * least one pathspec that would sort equal to or
158 * later than the path we are currently looking at.
159 * In other words, if we have never reached this point
160 * after iterating all pathspecs, it means all
161 * pathspecs are either outside of base, or inside the
162 * base but sorts strictly earlier than the current
163 * one. In either case, they will never match the
164 * subsequent entries. In such a case, we initialized
165 * the variable to -1 and that is what will be
166 * returned, allowing the caller to terminate early.
168 never_interesting = 0;
171 if (pathlen > matchlen)
172 continue;
174 if (matchlen > pathlen) {
175 if (match[pathlen] != '/')
176 continue;
177 if (!S_ISDIR(mode))
178 continue;
181 if (m == -1)
183 * we cheated and did not do strncmp(), so we do
184 * that here.
186 m = strncmp(match, path, pathlen);
189 * If common part matched earlier then it is a hit,
190 * because we rejected the case where path is not a
191 * leading directory and is shorter than match.
193 if (!m)
194 return 1;
196 return never_interesting; /* No matches */
199 /* A whole sub-tree went away or appeared */
200 static void show_tree(struct diff_options *opt, const char *prefix, struct tree_desc *desc, const char *base, int baselen)
202 int all_interesting = 0;
203 while (desc->size) {
204 int show;
206 if (all_interesting)
207 show = 1;
208 else {
209 show = tree_entry_interesting(desc, base, baselen,
210 opt);
211 if (show == 2)
212 all_interesting = 1;
214 if (show < 0)
215 break;
216 if (show)
217 show_entry(opt, prefix, desc, base, baselen);
218 update_tree_entry(desc);
222 /* A file entry went away or appeared */
223 static void show_entry(struct diff_options *opt, const char *prefix, struct tree_desc *desc,
224 const char *base, int baselen)
226 unsigned mode;
227 const char *path;
228 const unsigned char *sha1 = tree_entry_extract(desc, &path, &mode);
229 int pathlen = tree_entry_len(path, sha1);
231 if (DIFF_OPT_TST(opt, RECURSIVE) && S_ISDIR(mode)) {
232 enum object_type type;
233 char *newbase = malloc_base(base, baselen, path, pathlen);
234 struct tree_desc inner;
235 void *tree;
236 unsigned long size;
238 tree = read_sha1_file(sha1, &type, &size);
239 if (!tree || type != OBJ_TREE)
240 die("corrupt tree sha %s", sha1_to_hex(sha1));
242 if (DIFF_OPT_TST(opt, TREE_IN_RECURSIVE)) {
243 newbase[baselen + pathlen] = 0;
244 opt->add_remove(opt, *prefix, mode, sha1, newbase, 0);
245 newbase[baselen + pathlen] = '/';
248 init_tree_desc(&inner, tree, size);
249 show_tree(opt, prefix, &inner, newbase, baselen + 1 + pathlen);
251 free(tree);
252 free(newbase);
253 } else {
254 char *fullname = malloc_fullname(base, baselen, path, pathlen);
255 opt->add_remove(opt, prefix[0], mode, sha1, fullname, 0);
256 free(fullname);
260 static void skip_uninteresting(struct tree_desc *t, const char *base, int baselen, struct diff_options *opt)
262 int all_interesting = 0;
263 while (t->size) {
264 int show;
266 if (all_interesting)
267 show = 1;
268 else {
269 show = tree_entry_interesting(t, base, baselen, opt);
270 if (show == 2)
271 all_interesting = 1;
273 if (!show) {
274 update_tree_entry(t);
275 continue;
277 /* Skip it all? */
278 if (show < 0)
279 t->size = 0;
280 return;
284 int diff_tree(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt)
286 int baselen = strlen(base);
288 for (;;) {
289 if (DIFF_OPT_TST(opt, QUICK) &&
290 DIFF_OPT_TST(opt, HAS_CHANGES))
291 break;
292 if (opt->nr_paths) {
293 skip_uninteresting(t1, base, baselen, opt);
294 skip_uninteresting(t2, base, baselen, opt);
296 if (!t1->size) {
297 if (!t2->size)
298 break;
299 show_entry(opt, "+", t2, base, baselen);
300 update_tree_entry(t2);
301 continue;
303 if (!t2->size) {
304 show_entry(opt, "-", t1, base, baselen);
305 update_tree_entry(t1);
306 continue;
308 switch (compare_tree_entry(t1, t2, base, baselen, opt)) {
309 case -1:
310 update_tree_entry(t1);
311 continue;
312 case 0:
313 update_tree_entry(t1);
314 /* Fallthrough */
315 case 1:
316 update_tree_entry(t2);
317 continue;
319 die("git diff-tree: internal error");
321 return 0;
325 * Does it look like the resulting diff might be due to a rename?
326 * - single entry
327 * - not a valid previous file
329 static inline int diff_might_be_rename(void)
331 return diff_queued_diff.nr == 1 &&
332 !DIFF_FILE_VALID(diff_queued_diff.queue[0]->one);
335 static void try_to_follow_renames(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt)
337 struct diff_options diff_opts;
338 struct diff_queue_struct *q = &diff_queued_diff;
339 struct diff_filepair *choice;
340 const char *paths[1];
341 int i;
343 /* Remove the file creation entry from the diff queue, and remember it */
344 choice = q->queue[0];
345 q->nr = 0;
347 diff_setup(&diff_opts);
348 DIFF_OPT_SET(&diff_opts, RECURSIVE);
349 DIFF_OPT_SET(&diff_opts, FIND_COPIES_HARDER);
350 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
351 diff_opts.single_follow = opt->paths[0];
352 diff_opts.break_opt = opt->break_opt;
353 paths[0] = NULL;
354 diff_tree_setup_paths(paths, &diff_opts);
355 if (diff_setup_done(&diff_opts) < 0)
356 die("unable to set up diff options to follow renames");
357 diff_tree(t1, t2, base, &diff_opts);
358 diffcore_std(&diff_opts);
359 diff_tree_release_paths(&diff_opts);
361 /* Go through the new set of filepairing, and see if we find a more interesting one */
362 opt->found_follow = 0;
363 for (i = 0; i < q->nr; i++) {
364 struct diff_filepair *p = q->queue[i];
367 * Found a source? Not only do we use that for the new
368 * diff_queued_diff, we will also use that as the path in
369 * the future!
371 if ((p->status == 'R' || p->status == 'C') && !strcmp(p->two->path, opt->paths[0])) {
372 /* Switch the file-pairs around */
373 q->queue[i] = choice;
374 choice = p;
376 /* Update the path we use from now on.. */
377 diff_tree_release_paths(opt);
378 opt->paths[0] = xstrdup(p->one->path);
379 diff_tree_setup_paths(opt->paths, opt);
382 * The caller expects us to return a set of vanilla
383 * filepairs to let a later call to diffcore_std()
384 * it makes to sort the renames out (among other
385 * things), but we already have found renames
386 * ourselves; signal diffcore_std() not to muck with
387 * rename information.
389 opt->found_follow = 1;
390 break;
395 * Then, discard all the non-relevant file pairs...
397 for (i = 0; i < q->nr; i++) {
398 struct diff_filepair *p = q->queue[i];
399 diff_free_filepair(p);
403 * .. and re-instate the one we want (which might be either the
404 * original one, or the rename/copy we found)
406 q->queue[0] = choice;
407 q->nr = 1;
410 int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base, struct diff_options *opt)
412 void *tree1, *tree2;
413 struct tree_desc t1, t2;
414 unsigned long size1, size2;
415 int retval;
417 tree1 = read_object_with_reference(old, tree_type, &size1, NULL);
418 if (!tree1)
419 die("unable to read source tree (%s)", sha1_to_hex(old));
420 tree2 = read_object_with_reference(new, tree_type, &size2, NULL);
421 if (!tree2)
422 die("unable to read destination tree (%s)", sha1_to_hex(new));
423 init_tree_desc(&t1, tree1, size1);
424 init_tree_desc(&t2, tree2, size2);
425 retval = diff_tree(&t1, &t2, base, opt);
426 if (!*base && DIFF_OPT_TST(opt, FOLLOW_RENAMES) && diff_might_be_rename()) {
427 init_tree_desc(&t1, tree1, size1);
428 init_tree_desc(&t2, tree2, size2);
429 try_to_follow_renames(&t1, &t2, base, opt);
431 free(tree1);
432 free(tree2);
433 return retval;
436 int diff_root_tree_sha1(const unsigned char *new, const char *base, struct diff_options *opt)
438 int retval;
439 void *tree;
440 unsigned long size;
441 struct tree_desc empty, real;
443 tree = read_object_with_reference(new, tree_type, &size, NULL);
444 if (!tree)
445 die("unable to read root tree (%s)", sha1_to_hex(new));
446 init_tree_desc(&real, tree, size);
448 init_tree_desc(&empty, "", 0);
449 retval = diff_tree(&empty, &real, base, opt);
450 free(tree);
451 return retval;
454 static int count_paths(const char **paths)
456 int i = 0;
457 while (*paths++)
458 i++;
459 return i;
462 void diff_tree_release_paths(struct diff_options *opt)
464 free(opt->pathlens);
467 void diff_tree_setup_paths(const char **p, struct diff_options *opt)
469 opt->nr_paths = 0;
470 opt->pathlens = NULL;
471 opt->paths = NULL;
473 if (p) {
474 int i;
476 opt->paths = p;
477 opt->nr_paths = count_paths(p);
478 if (opt->nr_paths == 0) {
479 opt->pathlens = NULL;
480 return;
482 opt->pathlens = xmalloc(opt->nr_paths * sizeof(int));
483 for (i=0; i < opt->nr_paths; i++)
484 opt->pathlens[i] = strlen(p[i]);