Skip to content

Commit d336123

Browse files
rsahlbergpeff
authored andcommitted
verify_refname_available(): new function
Add a new verify_refname_available() function, which checks whether the refname is available for use, taking all references (both packed and loose) into account. This function, unlike the old verify_refname_available(), has semantics independent of the choice of reference storage, and can therefore be implemented by alternative reference backends. Use the new function in a couple of places. Signed-off-by: Ronnie Sahlberg <[email protected]> Signed-off-by: David Turner <[email protected]> Signed-off-by: Junio C Hamano <[email protected]> Signed-off-by: Michael Haggerty <[email protected]> Signed-off-by: Jeff King <[email protected]>
1 parent 7003b3c commit d336123

File tree

1 file changed

+40
-26
lines changed

1 file changed

+40
-26
lines changed

refs.c

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ struct ref_dir {
279279
* presence of an empty subdirectory does not block the creation of a
280280
* similarly-named reference. (The fact that reference names with the
281281
* same leading components can conflict *with each other* is a
282-
* separate issue that is regulated by verify_refname_available_dir().)
282+
* separate issue that is regulated by verify_refname_available().)
283283
*
284284
* Please note that the name field contains the fully-qualified
285285
* reference (or subdirectory) name. Space could be saved by only
@@ -897,19 +897,7 @@ static int nonmatching_ref_fn(struct ref_entry *entry, void *vdata)
897897
/*
898898
* Return 0 if a reference named refname could be created without
899899
* conflicting with the name of an existing reference in dir.
900-
* Otherwise, return a negative value and write an explanation to err.
901-
* If extras is non-NULL, it is a list of additional refnames with
902-
* which refname is not allowed to conflict. If skip is non-NULL,
903-
* ignore potential conflicts with refs in skip (e.g., because they
904-
* are scheduled for deletion in the same operation). Behavior is
905-
* undefined if the same name is listed in both extras and skip.
906-
*
907-
* Two reference names conflict if one of them exactly matches the
908-
* leading components of the other; e.g., "refs/foo/bar" conflicts
909-
* with both "refs/foo" and with "refs/foo/bar/baz" but not with
910-
* "refs/foo/bar" or "refs/foo/barbados".
911-
*
912-
* extras and skip must be sorted.
900+
* See verify_refname_available for more information.
913901
*/
914902
static int verify_refname_available_dir(const char *refname,
915903
const struct string_list *extras,
@@ -3120,17 +3108,48 @@ static int rename_tmp_log(const char *newrefname)
31203108
return ret;
31213109
}
31223110

3111+
/*
3112+
* Return 0 if a reference named refname could be created without
3113+
* conflicting with the name of an existing reference. Otherwise,
3114+
* return a negative value and write an explanation to err. If extras
3115+
* is non-NULL, it is a list of additional refnames with which refname
3116+
* is not allowed to conflict. If skip is non-NULL, ignore potential
3117+
* conflicts with refs in skip (e.g., because they are scheduled for
3118+
* deletion in the same operation). Behavior is undefined if the same
3119+
* name is listed in both extras and skip.
3120+
*
3121+
* Two reference names conflict if one of them exactly matches the
3122+
* leading components of the other; e.g., "foo/bar" conflicts with
3123+
* both "foo" and with "foo/bar/baz" but not with "foo/bar" or
3124+
* "foo/barbados".
3125+
*
3126+
* extras and skip must be sorted.
3127+
*/
3128+
static int verify_refname_available(const char *newname,
3129+
struct string_list *extras,
3130+
struct string_list *skip,
3131+
struct strbuf *err)
3132+
{
3133+
struct ref_dir *packed_refs = get_packed_refs(&ref_cache);
3134+
struct ref_dir *loose_refs = get_loose_refs(&ref_cache);
3135+
3136+
if (verify_refname_available_dir(newname, extras, skip,
3137+
packed_refs, err) ||
3138+
verify_refname_available_dir(newname, extras, skip,
3139+
loose_refs, err))
3140+
return -1;
3141+
3142+
return 0;
3143+
}
3144+
31233145
static int rename_ref_available(const char *oldname, const char *newname)
31243146
{
31253147
struct string_list skip = STRING_LIST_INIT_NODUP;
31263148
struct strbuf err = STRBUF_INIT;
31273149
int ret;
31283150

31293151
string_list_insert(&skip, oldname);
3130-
ret = !verify_refname_available_dir(newname, NULL, &skip,
3131-
get_packed_refs(&ref_cache), &err)
3132-
&& !verify_refname_available_dir(newname, NULL, &skip,
3133-
get_loose_refs(&ref_cache), &err);
3152+
ret = !verify_refname_available(newname, NULL, &skip, &err);
31343153
if (!ret)
31353154
error("%s", err.buf);
31363155

@@ -4334,8 +4353,6 @@ static int ref_present(const char *refname,
43344353
int initial_ref_transaction_commit(struct ref_transaction *transaction,
43354354
struct strbuf *err)
43364355
{
4337-
struct ref_dir *loose_refs = get_loose_refs(&ref_cache);
4338-
struct ref_dir *packed_refs = get_packed_refs(&ref_cache);
43394356
int ret = 0, i;
43404357
int n = transaction->nr;
43414358
struct ref_update **updates = transaction->updates;
@@ -4376,12 +4393,9 @@ int initial_ref_transaction_commit(struct ref_transaction *transaction,
43764393
if ((update->flags & REF_HAVE_OLD) &&
43774394
!is_null_sha1(update->old_sha1))
43784395
die("BUG: initial ref transaction with old_sha1 set");
4379-
if (verify_refname_available_dir(update->refname,
4380-
&affected_refnames, NULL,
4381-
loose_refs, err) ||
4382-
verify_refname_available_dir(update->refname,
4383-
&affected_refnames, NULL,
4384-
packed_refs, err)) {
4396+
if (verify_refname_available(update->refname,
4397+
&affected_refnames, NULL,
4398+
err)) {
43854399
ret = TRANSACTION_NAME_CONFLICT;
43864400
goto cleanup;
43874401
}

0 commit comments

Comments
 (0)