tree-diff: don't access hash of NULL object_id pointer
[git.git] / tree-diff.c
blob7f7ddda4ea51e32f67ded46d73f5c0b1c8adb6d6
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 /*
10 * internal mode marker, saying a tree entry != entry of tp[imin]
11 * (see ll_diff_tree_paths for what it means there)
13 * we will update/use/emit entry for diff only with it unset.
15 #define S_IFXMIN_NEQ S_DIFFTREE_IFXMIN_NEQ
17 #define FAST_ARRAY_ALLOC(x, nr) do { \
18 if ((nr) <= 2) \
19 (x) = xalloca((nr) * sizeof(*(x))); \
20 else \
21 ALLOC_ARRAY((x), nr); \
22 } while(0)
23 #define FAST_ARRAY_FREE(x, nr) do { \
24 if ((nr) > 2) \
25 free((x)); \
26 } while(0)
28 static struct combine_diff_path *ll_diff_tree_paths(
29 struct combine_diff_path *p, const struct object_id *oid,
30 const struct object_id **parents_oid, int nparent,
31 struct strbuf *base, struct diff_options *opt);
32 static int ll_diff_tree_oid(const struct object_id *old_oid,
33 const struct object_id *new_oid,
34 struct strbuf *base, struct diff_options *opt);
37 * Compare two tree entries, taking into account only path/S_ISDIR(mode),
38 * but not their sha1's.
40 * NOTE files and directories *always* compare differently, even when having
41 * the same name - thanks to base_name_compare().
43 * NOTE empty (=invalid) descriptor(s) take part in comparison as +infty,
44 * so that they sort *after* valid tree entries.
46 * Due to this convention, if trees are scanned in sorted order, all
47 * non-empty descriptors will be processed first.
49 static int tree_entry_pathcmp(struct tree_desc *t1, struct tree_desc *t2)
51 struct name_entry *e1, *e2;
52 int cmp;
54 /* empty descriptors sort after valid tree entries */
55 if (!t1->size)
56 return t2->size ? 1 : 0;
57 else if (!t2->size)
58 return -1;
60 e1 = &t1->entry;
61 e2 = &t2->entry;
62 cmp = base_name_compare(e1->path, tree_entry_len(e1), e1->mode,
63 e2->path, tree_entry_len(e2), e2->mode);
64 return cmp;
69 * convert path -> opt->diff_*() callbacks
71 * emits diff to first parent only, and tells diff tree-walker that we are done
72 * with p and it can be freed.
74 static int emit_diff_first_parent_only(struct diff_options *opt, struct combine_diff_path *p)
76 struct combine_diff_parent *p0 = &p->parent[0];
77 if (p->mode && p0->mode) {
78 opt->change(opt, p0->mode, p->mode, &p0->oid, &p->oid,
79 1, 1, p->path, 0, 0);
81 else {
82 const struct object_id *oid;
83 unsigned int mode;
84 int addremove;
86 if (p->mode) {
87 addremove = '+';
88 oid = &p->oid;
89 mode = p->mode;
90 } else {
91 addremove = '-';
92 oid = &p0->oid;
93 mode = p0->mode;
96 opt->add_remove(opt, addremove, mode, oid, 1, p->path, 0);
99 return 0; /* we are done with p */
104 * Make a new combine_diff_path from path/mode/sha1
105 * and append it to paths list tail.
107 * Memory for created elements could be reused:
109 * - if last->next == NULL, the memory is allocated;
111 * - if last->next != NULL, it is assumed that p=last->next was returned
112 * earlier by this function, and p->next was *not* modified.
113 * The memory is then reused from p.
115 * so for clients,
117 * - if you do need to keep the element
119 * p = path_appendnew(p, ...);
120 * process(p);
121 * p->next = NULL;
123 * - if you don't need to keep the element after processing
125 * pprev = p;
126 * p = path_appendnew(p, ...);
127 * process(p);
128 * p = pprev;
129 * ; don't forget to free tail->next in the end
131 * p->parent[] remains uninitialized.
133 static struct combine_diff_path *path_appendnew(struct combine_diff_path *last,
134 int nparent, const struct strbuf *base, const char *path, int pathlen,
135 unsigned mode, const struct object_id *oid)
137 struct combine_diff_path *p;
138 size_t len = st_add(base->len, pathlen);
139 size_t alloclen = combine_diff_path_size(nparent, len);
141 /* if last->next is !NULL - it is a pre-allocated memory, we can reuse */
142 p = last->next;
143 if (p && (alloclen > (intptr_t)p->next)) {
144 free(p);
145 p = NULL;
148 if (!p) {
149 p = xmalloc(alloclen);
152 * until we go to it next round, .next holds how many bytes we
153 * allocated (for faster realloc - we don't need copying old data).
155 p->next = (struct combine_diff_path *)(intptr_t)alloclen;
158 last->next = p;
160 p->path = (char *)&(p->parent[nparent]);
161 memcpy(p->path, base->buf, base->len);
162 memcpy(p->path + base->len, path, pathlen);
163 p->path[len] = 0;
164 p->mode = mode;
165 oidcpy(&p->oid, oid ? oid : &null_oid);
167 return p;
171 * new path should be added to combine diff
173 * 3 cases on how/when it should be called and behaves:
175 * t, !tp -> path added, all parents lack it
176 * !t, tp -> path removed from all parents
177 * t, tp -> path modified/added
178 * (M for tp[i]=tp[imin], A otherwise)
180 static struct combine_diff_path *emit_path(struct combine_diff_path *p,
181 struct strbuf *base, struct diff_options *opt, int nparent,
182 struct tree_desc *t, struct tree_desc *tp,
183 int imin)
185 unsigned mode;
186 const char *path;
187 const struct object_id *oid;
188 int pathlen;
189 int old_baselen = base->len;
190 int i, isdir, recurse = 0, emitthis = 1;
192 /* at least something has to be valid */
193 assert(t || tp);
195 if (t) {
196 /* path present in resulting tree */
197 oid = tree_entry_extract(t, &path, &mode);
198 pathlen = tree_entry_len(&t->entry);
199 isdir = S_ISDIR(mode);
200 } else {
202 * a path was removed - take path from imin parent. Also take
203 * mode from that parent, to decide on recursion(1).
205 * 1) all modes for tp[i]=tp[imin] should be the same wrt
206 * S_ISDIR, thanks to base_name_compare().
208 tree_entry_extract(&tp[imin], &path, &mode);
209 pathlen = tree_entry_len(&tp[imin].entry);
211 isdir = S_ISDIR(mode);
212 oid = NULL;
213 mode = 0;
216 if (DIFF_OPT_TST(opt, RECURSIVE) && isdir) {
217 recurse = 1;
218 emitthis = DIFF_OPT_TST(opt, TREE_IN_RECURSIVE);
221 if (emitthis) {
222 int keep;
223 struct combine_diff_path *pprev = p;
224 p = path_appendnew(p, nparent, base, path, pathlen, mode, oid);
226 for (i = 0; i < nparent; ++i) {
228 * tp[i] is valid, if present and if tp[i]==tp[imin] -
229 * otherwise, we should ignore it.
231 int tpi_valid = tp && !(tp[i].entry.mode & S_IFXMIN_NEQ);
233 const struct object_id *oid_i;
234 unsigned mode_i;
236 p->parent[i].status =
237 !t ? DIFF_STATUS_DELETED :
238 tpi_valid ?
239 DIFF_STATUS_MODIFIED :
240 DIFF_STATUS_ADDED;
242 if (tpi_valid) {
243 oid_i = tp[i].entry.oid;
244 mode_i = tp[i].entry.mode;
246 else {
247 oid_i = &null_oid;
248 mode_i = 0;
251 p->parent[i].mode = mode_i;
252 oidcpy(&p->parent[i].oid, oid_i);
255 keep = 1;
256 if (opt->pathchange)
257 keep = opt->pathchange(opt, p);
260 * If a path was filtered or consumed - we don't need to add it
261 * to the list and can reuse its memory, leaving it as
262 * pre-allocated element on the tail.
264 * On the other hand, if path needs to be kept, we need to
265 * correct its .next to NULL, as it was pre-initialized to how
266 * much memory was allocated.
268 * see path_appendnew() for details.
270 if (!keep)
271 p = pprev;
272 else
273 p->next = NULL;
276 if (recurse) {
277 const struct object_id **parents_oid;
279 FAST_ARRAY_ALLOC(parents_oid, nparent);
280 for (i = 0; i < nparent; ++i) {
281 /* same rule as in emitthis */
282 int tpi_valid = tp && !(tp[i].entry.mode & S_IFXMIN_NEQ);
284 parents_oid[i] = tpi_valid ? tp[i].entry.oid : NULL;
287 strbuf_add(base, path, pathlen);
288 strbuf_addch(base, '/');
289 p = ll_diff_tree_paths(p, oid, parents_oid, nparent, base, opt);
290 FAST_ARRAY_FREE(parents_oid, nparent);
293 strbuf_setlen(base, old_baselen);
294 return p;
297 static void skip_uninteresting(struct tree_desc *t, struct strbuf *base,
298 struct diff_options *opt)
300 enum interesting match;
302 while (t->size) {
303 match = tree_entry_interesting(&t->entry, base, 0, &opt->pathspec);
304 if (match) {
305 if (match == all_entries_not_interesting)
306 t->size = 0;
307 break;
309 update_tree_entry(t);
315 * generate paths for combined diff D(sha1,parents_oid[])
317 * Resulting paths are appended to combine_diff_path linked list, and also, are
318 * emitted on the go via opt->pathchange() callback, so it is possible to
319 * process the result as batch or incrementally.
321 * The paths are generated scanning new tree and all parents trees
322 * simultaneously, similarly to what diff_tree() was doing for 2 trees.
323 * The theory behind such scan is as follows:
326 * D(T,P1...Pn) calculation scheme
327 * -------------------------------
329 * D(T,P1...Pn) = D(T,P1) ^ ... ^ D(T,Pn) (regarding resulting paths set)
331 * D(T,Pj) - diff between T..Pj
332 * D(T,P1...Pn) - combined diff from T to parents P1,...,Pn
335 * We start from all trees, which are sorted, and compare their entries in
336 * lock-step:
338 * T P1 Pn
339 * - - -
340 * |t| |p1| |pn|
341 * |-| |--| ... |--| imin = argmin(p1...pn)
342 * | | | | | |
343 * |-| |--| |--|
344 * |.| |. | |. |
345 * . . .
346 * . . .
348 * at any time there could be 3 cases:
350 * 1) t < p[imin];
351 * 2) t > p[imin];
352 * 3) t = p[imin].
354 * Schematic deduction of what every case means, and what to do, follows:
356 * 1) t < p[imin] -> ∀j t ∉ Pj -> "+t" ∈ D(T,Pj) -> D += "+t"; t↓
358 * 2) t > p[imin]
360 * 2.1) ∃j: pj > p[imin] -> "-p[imin]" ∉ D(T,Pj) -> D += ø; ∀ pi=p[imin] pi↓
361 * 2.2) ∀i pi = p[imin] -> pi ∉ T -> "-pi" ∈ D(T,Pi) -> D += "-p[imin]"; ∀i pi↓
363 * 3) t = p[imin]
365 * 3.1) ∃j: pj > p[imin] -> "+t" ∈ D(T,Pj) -> only pi=p[imin] remains to investigate
366 * 3.2) pi = p[imin] -> investigate δ(t,pi)
371 * 3.1+3.2) looking at δ(t,pi) ∀i: pi=p[imin] - if all != ø ->
373 * ⎧δ(t,pi) - if pi=p[imin]
374 * -> D += ⎨
375 * ⎩"+t" - if pi>p[imin]
378 * in any case t↓ ∀ pi=p[imin] pi↓
381 * ~~~~~~~~
383 * NOTE
385 * Usual diff D(A,B) is by definition the same as combined diff D(A,[B]),
386 * so this diff paths generator can, and is used, for plain diffs
387 * generation too.
389 * Please keep attention to the common D(A,[B]) case when working on the
390 * code, in order not to slow it down.
392 * NOTE
393 * nparent must be > 0.
397 /* ∀ pi=p[imin] pi↓ */
398 static inline void update_tp_entries(struct tree_desc *tp, int nparent)
400 int i;
401 for (i = 0; i < nparent; ++i)
402 if (!(tp[i].entry.mode & S_IFXMIN_NEQ))
403 update_tree_entry(&tp[i]);
406 static struct combine_diff_path *ll_diff_tree_paths(
407 struct combine_diff_path *p, const struct object_id *oid,
408 const struct object_id **parents_oid, int nparent,
409 struct strbuf *base, struct diff_options *opt)
411 struct tree_desc t, *tp;
412 void *ttree, **tptree;
413 int i;
415 FAST_ARRAY_ALLOC(tp, nparent);
416 FAST_ARRAY_ALLOC(tptree, nparent);
419 * load parents first, as they are probably already cached.
421 * ( log_tree_diff() parses commit->parent before calling here via
422 * diff_tree_oid(parent, commit) )
424 for (i = 0; i < nparent; ++i)
425 tptree[i] = fill_tree_descriptor(&tp[i],
426 parents_oid[i] ? parents_oid[i]->hash : NULL);
427 ttree = fill_tree_descriptor(&t, oid ? oid->hash : NULL);
429 /* Enable recursion indefinitely */
430 opt->pathspec.recursive = DIFF_OPT_TST(opt, RECURSIVE);
432 for (;;) {
433 int imin, cmp;
435 if (diff_can_quit_early(opt))
436 break;
438 if (opt->pathspec.nr) {
439 skip_uninteresting(&t, base, opt);
440 for (i = 0; i < nparent; i++)
441 skip_uninteresting(&tp[i], base, opt);
444 /* comparing is finished when all trees are done */
445 if (!t.size) {
446 int done = 1;
447 for (i = 0; i < nparent; ++i)
448 if (tp[i].size) {
449 done = 0;
450 break;
452 if (done)
453 break;
457 * lookup imin = argmin(p1...pn),
458 * mark entries whether they =p[imin] along the way
460 imin = 0;
461 tp[0].entry.mode &= ~S_IFXMIN_NEQ;
463 for (i = 1; i < nparent; ++i) {
464 cmp = tree_entry_pathcmp(&tp[i], &tp[imin]);
465 if (cmp < 0) {
466 imin = i;
467 tp[i].entry.mode &= ~S_IFXMIN_NEQ;
469 else if (cmp == 0) {
470 tp[i].entry.mode &= ~S_IFXMIN_NEQ;
472 else {
473 tp[i].entry.mode |= S_IFXMIN_NEQ;
477 /* fixup markings for entries before imin */
478 for (i = 0; i < imin; ++i)
479 tp[i].entry.mode |= S_IFXMIN_NEQ; /* pi > p[imin] */
483 /* compare t vs p[imin] */
484 cmp = tree_entry_pathcmp(&t, &tp[imin]);
486 /* t = p[imin] */
487 if (cmp == 0) {
488 /* are either pi > p[imin] or diff(t,pi) != ø ? */
489 if (!DIFF_OPT_TST(opt, FIND_COPIES_HARDER)) {
490 for (i = 0; i < nparent; ++i) {
491 /* p[i] > p[imin] */
492 if (tp[i].entry.mode & S_IFXMIN_NEQ)
493 continue;
495 /* diff(t,pi) != ø */
496 if (oidcmp(t.entry.oid, tp[i].entry.oid) ||
497 (t.entry.mode != tp[i].entry.mode))
498 continue;
500 goto skip_emit_t_tp;
504 /* D += {δ(t,pi) if pi=p[imin]; "+a" if pi > p[imin]} */
505 p = emit_path(p, base, opt, nparent,
506 &t, tp, imin);
508 skip_emit_t_tp:
509 /* t↓, ∀ pi=p[imin] pi↓ */
510 update_tree_entry(&t);
511 update_tp_entries(tp, nparent);
514 /* t < p[imin] */
515 else if (cmp < 0) {
516 /* D += "+t" */
517 p = emit_path(p, base, opt, nparent,
518 &t, /*tp=*/NULL, -1);
520 /* t↓ */
521 update_tree_entry(&t);
524 /* t > p[imin] */
525 else {
526 /* ∀i pi=p[imin] -> D += "-p[imin]" */
527 if (!DIFF_OPT_TST(opt, FIND_COPIES_HARDER)) {
528 for (i = 0; i < nparent; ++i)
529 if (tp[i].entry.mode & S_IFXMIN_NEQ)
530 goto skip_emit_tp;
533 p = emit_path(p, base, opt, nparent,
534 /*t=*/NULL, tp, imin);
536 skip_emit_tp:
537 /* ∀ pi=p[imin] pi↓ */
538 update_tp_entries(tp, nparent);
542 free(ttree);
543 for (i = nparent-1; i >= 0; i--)
544 free(tptree[i]);
545 FAST_ARRAY_FREE(tptree, nparent);
546 FAST_ARRAY_FREE(tp, nparent);
548 return p;
551 struct combine_diff_path *diff_tree_paths(
552 struct combine_diff_path *p, const struct object_id *oid,
553 const struct object_id **parents_oid, int nparent,
554 struct strbuf *base, struct diff_options *opt)
556 p = ll_diff_tree_paths(p, oid, parents_oid, nparent, base, opt);
559 * free pre-allocated last element, if any
560 * (see path_appendnew() for details about why)
562 if (p->next) {
563 free(p->next);
564 p->next = NULL;
567 return p;
571 * Does it look like the resulting diff might be due to a rename?
572 * - single entry
573 * - not a valid previous file
575 static inline int diff_might_be_rename(void)
577 return diff_queued_diff.nr == 1 &&
578 !DIFF_FILE_VALID(diff_queued_diff.queue[0]->one);
581 static void try_to_follow_renames(const struct object_id *old_oid,
582 const struct object_id *new_oid,
583 struct strbuf *base, struct diff_options *opt)
585 struct diff_options diff_opts;
586 struct diff_queue_struct *q = &diff_queued_diff;
587 struct diff_filepair *choice;
588 int i;
591 * follow-rename code is very specific, we need exactly one
592 * path. Magic that matches more than one path is not
593 * supported.
595 GUARD_PATHSPEC(&opt->pathspec, PATHSPEC_FROMTOP | PATHSPEC_LITERAL);
596 #if 0
598 * We should reject wildcards as well. Unfortunately we
599 * haven't got a reliable way to detect that 'foo\*bar' in
600 * fact has no wildcards. nowildcard_len is merely a hint for
601 * optimization. Let it slip for now until wildmatch is taught
602 * about dry-run mode and returns wildcard info.
604 if (opt->pathspec.has_wildcard)
605 die("BUG:%s:%d: wildcards are not supported",
606 __FILE__, __LINE__);
607 #endif
609 /* Remove the file creation entry from the diff queue, and remember it */
610 choice = q->queue[0];
611 q->nr = 0;
613 diff_setup(&diff_opts);
614 DIFF_OPT_SET(&diff_opts, RECURSIVE);
615 DIFF_OPT_SET(&diff_opts, FIND_COPIES_HARDER);
616 diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
617 diff_opts.single_follow = opt->pathspec.items[0].match;
618 diff_opts.break_opt = opt->break_opt;
619 diff_opts.rename_score = opt->rename_score;
620 diff_setup_done(&diff_opts);
621 ll_diff_tree_oid(old_oid, new_oid, base, &diff_opts);
622 diffcore_std(&diff_opts);
623 clear_pathspec(&diff_opts.pathspec);
625 /* Go through the new set of filepairing, and see if we find a more interesting one */
626 opt->found_follow = 0;
627 for (i = 0; i < q->nr; i++) {
628 struct diff_filepair *p = q->queue[i];
631 * Found a source? Not only do we use that for the new
632 * diff_queued_diff, we will also use that as the path in
633 * the future!
635 if ((p->status == 'R' || p->status == 'C') &&
636 !strcmp(p->two->path, opt->pathspec.items[0].match)) {
637 const char *path[2];
639 /* Switch the file-pairs around */
640 q->queue[i] = choice;
641 choice = p;
643 /* Update the path we use from now on.. */
644 path[0] = p->one->path;
645 path[1] = NULL;
646 clear_pathspec(&opt->pathspec);
647 parse_pathspec(&opt->pathspec,
648 PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
649 PATHSPEC_LITERAL_PATH, "", path);
652 * The caller expects us to return a set of vanilla
653 * filepairs to let a later call to diffcore_std()
654 * it makes to sort the renames out (among other
655 * things), but we already have found renames
656 * ourselves; signal diffcore_std() not to muck with
657 * rename information.
659 opt->found_follow = 1;
660 break;
665 * Then, discard all the non-relevant file pairs...
667 for (i = 0; i < q->nr; i++) {
668 struct diff_filepair *p = q->queue[i];
669 diff_free_filepair(p);
673 * .. and re-instate the one we want (which might be either the
674 * original one, or the rename/copy we found)
676 q->queue[0] = choice;
677 q->nr = 1;
680 static int ll_diff_tree_oid(const struct object_id *old_oid,
681 const struct object_id *new_oid,
682 struct strbuf *base, struct diff_options *opt)
684 struct combine_diff_path phead, *p;
685 pathchange_fn_t pathchange_old = opt->pathchange;
687 phead.next = NULL;
688 opt->pathchange = emit_diff_first_parent_only;
689 diff_tree_paths(&phead, new_oid, &old_oid, 1, base, opt);
691 for (p = phead.next; p;) {
692 struct combine_diff_path *pprev = p;
693 p = p->next;
694 free(pprev);
697 opt->pathchange = pathchange_old;
698 return 0;
701 int diff_tree_oid(const struct object_id *old_oid,
702 const struct object_id *new_oid,
703 const char *base_str, struct diff_options *opt)
705 struct strbuf base;
706 int retval;
708 strbuf_init(&base, PATH_MAX);
709 strbuf_addstr(&base, base_str);
711 retval = ll_diff_tree_oid(old_oid, new_oid, &base, opt);
712 if (!*base_str && DIFF_OPT_TST(opt, FOLLOW_RENAMES) && diff_might_be_rename())
713 try_to_follow_renames(old_oid, new_oid, &base, opt);
715 strbuf_release(&base);
717 return retval;
720 int diff_root_tree_oid(const struct object_id *new_oid, const char *base, struct diff_options *opt)
722 return diff_tree_oid(NULL, new_oid, base, opt);