@matheusbsbs / matheus-bagliano / commits / 21cd8d00b6
Fork 0

add_cache_entry(): removal of file foo does not conflict with foo/bar

Similarly, removal of file foo/bar does not conflict with a file foo. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Mar 30, 2007 at 01:55 UTC 21cd8d00b6908dd8e1d4a5d101c1ba49d3645053
1 file changed +31 -9
read-cache.c
+31 -9
@@ -487,6 +487,8 @@ static int has_file_name(const struct cache_entry *ce, int pos, int ok_to_replac
487 continue;
488 if (p->name[len] != '/')
489 continue;
490 + if (!ce_stage(p) && !p->ce_mode)
491 + continue;
492 retval = -1;
493 if (!ok_to_replace)
494 break;
@@ -519,26 +521,37 @@ static int has_dir_name(const struct cache_entry *ce, int pos, int ok_to_replace
521
522 pos = cache_name_pos(name, ntohs(create_ce_flags(len, stage)));
523 if (pos >= 0) {
522 - retval = -1;
523 - if (!ok_to_replace)
524 - break;
525 - remove_cache_entry_at(pos);
526 - continue;
524 + /*
525 + * Found one, but not so fast. This could
526 + * be a marker that says "I was here, but
527 + * I am being removed". Such an entry is
528 + * not a part of the resulting tree, and
529 + * it is Ok to have a directory at the same
530 + * path.
531 + */
532 + if (stage || active_cache[pos]->ce_mode) {
533 + retval = -1;
534 + if (!ok_to_replace)
535 + break;
536 + remove_cache_entry_at(pos);
537 + continue;
538 + }
539 }
540 + else
541 + pos = -pos-1;
542
543 /*
544 * Trivial optimization: if we find an entry that
545 * already matches the sub-directory, then we know
546 * we're ok, and we can exit.
547 */
534 - pos = -pos-1;
548 while (pos < active_nr) {
549 struct cache_entry *p = active_cache[pos];
550 if ((ce_namelen(p) <= len) ||
551 (p->name[len] != '/') ||
552 memcmp(p->name, name, len))
553 break; /* not our subdirectory */
541 - if (ce_stage(p) == stage)
554 + if (ce_stage(p) == stage && (stage || p->ce_mode))
555 /* p is at the same stage as our entry, and
556 * is a subdirectory of what we are looking
557 * at, so we cannot have conflicts at our
@@ -562,12 +575,21 @@ static int has_dir_name(const struct cache_entry *ce, int pos, int ok_to_replace
575 */
576 static int check_file_directory_conflict(const struct cache_entry *ce, int pos, int ok_to_replace)
577 {
578 + int retval;
579 +
580 + /*
581 + * When ce is an "I am going away" entry, we allow it to be added
582 + */
583 + if (!ce_stage(ce) && !ce->ce_mode)
584 + return 0;
585 +
586 /*
587 * We check if the path is a sub-path of a subsequent pathname
588 * first, since removing those will not change the position
568 - * in the array
589 + * in the array.
590 */
570 - int retval = has_file_name(ce, pos, ok_to_replace);
591 + retval = has_file_name(ce, pos, ok_to_replace);
592 +
593 /*
594 * Then check if the path might have a clashing sub-directory
595 * before it.