@matheusbsbs / matheus-bagliano / commits / 22a3844eba
Fork 0

lock_ref_sha1(): check D/F conflict with packed ref when creating.

This makes the ref locking codepath to notice if an existing ref overlaps with the ref we are creating. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Sep 30, 2006 at 14:19 UTC 22a3844ebada55c207e2b85fb68509a7c9e2b5f0
1 file changed +24
refs.c
+24
@@ -588,6 +588,30 @@ static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char
588 orig_ref, strerror(errno));
589 goto error_return;
590 }
591 + if (is_null_sha1(lock->old_sha1)) {
592 + /* The ref did not exist and we are creating it.
593 + * Make sure there is no existing ref that is packed
594 + * whose name begins with our refname, nor a ref whose
595 + * name is a proper prefix of our refname.
596 + */
597 + int namlen = strlen(ref); /* e.g. 'foo/bar' */
598 + struct ref_list *list = get_packed_refs();
599 + while (list) {
600 + /* list->name could be 'foo' or 'foo/bar/baz' */
601 + int len = strlen(list->name);
602 + int cmplen = (namlen < len) ? namlen : len;
603 + const char *lead = (namlen < len) ? list->name : ref;
604 +
605 + if (!strncmp(ref, list->name, cmplen) &&
606 + lead[cmplen] == '/') {
607 + error("'%s' exists; cannot create '%s'",
608 + list->name, ref);
609 + goto error_return;
610 + }
611 + list = list->next;
612 + }
613 + }
614 +
615 lock->lk = xcalloc(1, sizeof(struct lock_file));
616
617 lock->ref_name = xstrdup(ref);