Skip to content

Commit 524a9fd

Browse files
mhaggergitster
authored andcommitted
refs_verify_refname_available(): use function in more places
Change `lock_raw_ref()` and `lock_ref_sha1_basic()` to use `refs_verify_refname_available()` instead of `verify_refname_available_dir()`. This means that those callsites now check for conflicts with all references rather than just packed refs, but the performance cost shouldn't be significant (and will be regained later). These were the last callers of `verify_refname_available_dir()`, so also delete that (very complicated) function. Signed-off-by: Michael Haggerty <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b05855b commit 524a9fd

File tree

1 file changed

+11
-160
lines changed

1 file changed

+11
-160
lines changed

refs/files-backend.c

Lines changed: 11 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -738,152 +738,6 @@ static struct ref_iterator *cache_ref_iterator_begin(struct ref_dir *dir)
738738
return ref_iterator;
739739
}
740740

741-
struct nonmatching_ref_data {
742-
const struct string_list *skip;
743-
const char *conflicting_refname;
744-
};
745-
746-
static int nonmatching_ref_fn(struct ref_entry *entry, void *vdata)
747-
{
748-
struct nonmatching_ref_data *data = vdata;
749-
750-
if (data->skip && string_list_has_string(data->skip, entry->name))
751-
return 0;
752-
753-
data->conflicting_refname = entry->name;
754-
return 1;
755-
}
756-
757-
/*
758-
* Return 0 if a reference named refname could be created without
759-
* conflicting with the name of an existing reference in dir.
760-
* See verify_refname_available for more information.
761-
*/
762-
static int verify_refname_available_dir(const char *refname,
763-
const struct string_list *extras,
764-
const struct string_list *skip,
765-
struct ref_dir *dir,
766-
struct strbuf *err)
767-
{
768-
const char *slash;
769-
const char *extra_refname;
770-
int pos;
771-
struct strbuf dirname = STRBUF_INIT;
772-
int ret = -1;
773-
774-
/*
775-
* For the sake of comments in this function, suppose that
776-
* refname is "refs/foo/bar".
777-
*/
778-
779-
assert(err);
780-
781-
strbuf_grow(&dirname, strlen(refname) + 1);
782-
for (slash = strchr(refname, '/'); slash; slash = strchr(slash + 1, '/')) {
783-
/* Expand dirname to the new prefix, not including the trailing slash: */
784-
strbuf_add(&dirname, refname + dirname.len, slash - refname - dirname.len);
785-
786-
/*
787-
* We are still at a leading dir of the refname (e.g.,
788-
* "refs/foo"; if there is a reference with that name,
789-
* it is a conflict, *unless* it is in skip.
790-
*/
791-
if (dir) {
792-
pos = search_ref_dir(dir, dirname.buf, dirname.len);
793-
if (pos >= 0 &&
794-
(!skip || !string_list_has_string(skip, dirname.buf))) {
795-
/*
796-
* We found a reference whose name is
797-
* a proper prefix of refname; e.g.,
798-
* "refs/foo", and is not in skip.
799-
*/
800-
strbuf_addf(err, "'%s' exists; cannot create '%s'",
801-
dirname.buf, refname);
802-
goto cleanup;
803-
}
804-
}
805-
806-
if (extras && string_list_has_string(extras, dirname.buf) &&
807-
(!skip || !string_list_has_string(skip, dirname.buf))) {
808-
strbuf_addf(err, "cannot process '%s' and '%s' at the same time",
809-
refname, dirname.buf);
810-
goto cleanup;
811-
}
812-
813-
/*
814-
* Otherwise, we can try to continue our search with
815-
* the next component. So try to look up the
816-
* directory, e.g., "refs/foo/". If we come up empty,
817-
* we know there is nothing under this whole prefix,
818-
* but even in that case we still have to continue the
819-
* search for conflicts with extras.
820-
*/
821-
strbuf_addch(&dirname, '/');
822-
if (dir) {
823-
pos = search_ref_dir(dir, dirname.buf, dirname.len);
824-
if (pos < 0) {
825-
/*
826-
* There was no directory "refs/foo/",
827-
* so there is nothing under this
828-
* whole prefix. So there is no need
829-
* to continue looking for conflicting
830-
* references. But we need to continue
831-
* looking for conflicting extras.
832-
*/
833-
dir = NULL;
834-
} else {
835-
dir = get_ref_dir(dir->entries[pos]);
836-
}
837-
}
838-
}
839-
840-
/*
841-
* We are at the leaf of our refname (e.g., "refs/foo/bar").
842-
* There is no point in searching for a reference with that
843-
* name, because a refname isn't considered to conflict with
844-
* itself. But we still need to check for references whose
845-
* names are in the "refs/foo/bar/" namespace, because they
846-
* *do* conflict.
847-
*/
848-
strbuf_addstr(&dirname, refname + dirname.len);
849-
strbuf_addch(&dirname, '/');
850-
851-
if (dir) {
852-
pos = search_ref_dir(dir, dirname.buf, dirname.len);
853-
854-
if (pos >= 0) {
855-
/*
856-
* We found a directory named "$refname/"
857-
* (e.g., "refs/foo/bar/"). It is a problem
858-
* iff it contains any ref that is not in
859-
* "skip".
860-
*/
861-
struct nonmatching_ref_data data;
862-
863-
data.skip = skip;
864-
data.conflicting_refname = NULL;
865-
dir = get_ref_dir(dir->entries[pos]);
866-
sort_ref_dir(dir);
867-
if (do_for_each_entry_in_dir(dir, 0, nonmatching_ref_fn, &data)) {
868-
strbuf_addf(err, "'%s' exists; cannot create '%s'",
869-
data.conflicting_refname, refname);
870-
goto cleanup;
871-
}
872-
}
873-
}
874-
875-
extra_refname = find_descendant_ref(dirname.buf, extras, skip);
876-
if (extra_refname)
877-
strbuf_addf(err, "cannot process '%s' and '%s' at the same time",
878-
refname, extra_refname);
879-
else
880-
ret = 0;
881-
882-
cleanup:
883-
strbuf_release(&dirname);
884-
return ret;
885-
}
886-
887741
struct packed_ref_cache {
888742
struct ref_entry *root;
889743

@@ -1562,7 +1416,7 @@ static void unlock_ref(struct ref_lock *lock)
15621416
*
15631417
* If the reference doesn't already exist, verify that refname doesn't
15641418
* have a D/F conflict with any existing references. extras and skip
1565-
* are passed to verify_refname_available_dir() for this check.
1419+
* are passed to refs_verify_refname_available() for this check.
15661420
*
15671421
* If mustexist is not set and the reference is not found or is
15681422
* broken, lock the reference anyway but clear sha1.
@@ -1577,7 +1431,7 @@ static void unlock_ref(struct ref_lock *lock)
15771431
*
15781432
* but it includes a lot more code to
15791433
* - Deal with possible races with other processes
1580-
* - Avoid calling verify_refname_available_dir() when it can be
1434+
* - Avoid calling refs_verify_refname_available() when it can be
15811435
* avoided, namely if we were successfully able to read the ref
15821436
* - Generate informative error messages in the case of failure
15831437
*/
@@ -1634,7 +1488,8 @@ static int lock_raw_ref(struct files_ref_store *refs,
16341488
} else {
16351489
/*
16361490
* The error message set by
1637-
* verify_refname_available_dir() is OK.
1491+
* refs_verify_refname_available() is
1492+
* OK.
16381493
*/
16391494
ret = TRANSACTION_NAME_CONFLICT;
16401495
}
@@ -1758,16 +1613,13 @@ static int lock_raw_ref(struct files_ref_store *refs,
17581613

17591614
/*
17601615
* If the ref did not exist and we are creating it,
1761-
* make sure there is no existing packed ref whose
1762-
* name begins with our refname, nor a packed ref
1763-
* whose name is a proper prefix of our refname.
1616+
* make sure there is no existing ref that conflicts
1617+
* with refname:
17641618
*/
1765-
if (verify_refname_available_dir(
1766-
refname, extras, skip,
1767-
get_packed_refs(refs),
1768-
err)) {
1619+
if (refs_verify_refname_available(
1620+
&refs->base, refname,
1621+
extras, skip, err))
17691622
goto error_return;
1770-
}
17711623
}
17721624

17731625
ret = 0;
@@ -2122,9 +1974,8 @@ static struct ref_lock *lock_ref_sha1_basic(struct files_ref_store *refs,
21221974
* our refname.
21231975
*/
21241976
if (is_null_oid(&lock->old_oid) &&
2125-
verify_refname_available_dir(refname, extras, skip,
2126-
get_packed_refs(refs),
2127-
err)) {
1977+
refs_verify_refname_available(&refs->base, refname,
1978+
extras, skip, err)) {
21281979
last_errno = ENOTDIR;
21291980
goto error_return;
21301981
}

0 commit comments

Comments
 (0)