Skip to content

Commit

Permalink
verify_refname_available(): new function
Browse files Browse the repository at this point in the history
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 <sahlberg@google.com>
Signed-off-by: David Turner <dturner@twopensource.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Signed-off-by: Jeff King <peff@peff.net>
  • Loading branch information
Ronnie Sahlberg authored and Jeff King committed Nov 20, 2015
1 parent 7003b3c commit d336123
Showing 1 changed file with 40 additions and 26 deletions.
66 changes: 40 additions & 26 deletions refs.c
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ struct ref_dir {
* presence of an empty subdirectory does not block the creation of a
* similarly-named reference. (The fact that reference names with the
* same leading components can conflict *with each other* is a
* separate issue that is regulated by verify_refname_available_dir().)
* separate issue that is regulated by verify_refname_available().)
*
* Please note that the name field contains the fully-qualified
* reference (or subdirectory) name. Space could be saved by only
Expand Down Expand Up @@ -897,19 +897,7 @@ static int nonmatching_ref_fn(struct ref_entry *entry, void *vdata)
/*
* Return 0 if a reference named refname could be created without
* conflicting with the name of an existing reference in dir.
* Otherwise, return a negative value and write an explanation to err.
* If extras is non-NULL, it is a list of additional refnames with
* which refname is not allowed to conflict. If skip is non-NULL,
* ignore potential conflicts with refs in skip (e.g., because they
* are scheduled for deletion in the same operation). Behavior is
* undefined if the same name is listed in both extras and skip.
*
* Two reference names conflict if one of them exactly matches the
* leading components of the other; e.g., "refs/foo/bar" conflicts
* with both "refs/foo" and with "refs/foo/bar/baz" but not with
* "refs/foo/bar" or "refs/foo/barbados".
*
* extras and skip must be sorted.
* See verify_refname_available for more information.
*/
static int verify_refname_available_dir(const char *refname,
const struct string_list *extras,
Expand Down Expand Up @@ -3120,17 +3108,48 @@ static int rename_tmp_log(const char *newrefname)
return ret;
}

/*
* Return 0 if a reference named refname could be created without
* conflicting with the name of an existing reference. Otherwise,
* return a negative value and write an explanation to err. If extras
* is non-NULL, it is a list of additional refnames with which refname
* is not allowed to conflict. If skip is non-NULL, ignore potential
* conflicts with refs in skip (e.g., because they are scheduled for
* deletion in the same operation). Behavior is undefined if the same
* name is listed in both extras and skip.
*
* Two reference names conflict if one of them exactly matches the
* leading components of the other; e.g., "foo/bar" conflicts with
* both "foo" and with "foo/bar/baz" but not with "foo/bar" or
* "foo/barbados".
*
* extras and skip must be sorted.
*/
static int verify_refname_available(const char *newname,
struct string_list *extras,
struct string_list *skip,
struct strbuf *err)
{
struct ref_dir *packed_refs = get_packed_refs(&ref_cache);
struct ref_dir *loose_refs = get_loose_refs(&ref_cache);

if (verify_refname_available_dir(newname, extras, skip,
packed_refs, err) ||
verify_refname_available_dir(newname, extras, skip,
loose_refs, err))
return -1;

return 0;
}

static int rename_ref_available(const char *oldname, const char *newname)
{
struct string_list skip = STRING_LIST_INIT_NODUP;
struct strbuf err = STRBUF_INIT;
int ret;

string_list_insert(&skip, oldname);
ret = !verify_refname_available_dir(newname, NULL, &skip,
get_packed_refs(&ref_cache), &err)
&& !verify_refname_available_dir(newname, NULL, &skip,
get_loose_refs(&ref_cache), &err);
ret = !verify_refname_available(newname, NULL, &skip, &err);
if (!ret)
error("%s", err.buf);

Expand Down Expand Up @@ -4334,8 +4353,6 @@ static int ref_present(const char *refname,
int initial_ref_transaction_commit(struct ref_transaction *transaction,
struct strbuf *err)
{
struct ref_dir *loose_refs = get_loose_refs(&ref_cache);
struct ref_dir *packed_refs = get_packed_refs(&ref_cache);
int ret = 0, i;
int n = transaction->nr;
struct ref_update **updates = transaction->updates;
Expand Down Expand Up @@ -4376,12 +4393,9 @@ int initial_ref_transaction_commit(struct ref_transaction *transaction,
if ((update->flags & REF_HAVE_OLD) &&
!is_null_sha1(update->old_sha1))
die("BUG: initial ref transaction with old_sha1 set");
if (verify_refname_available_dir(update->refname,
&affected_refnames, NULL,
loose_refs, err) ||
verify_refname_available_dir(update->refname,
&affected_refnames, NULL,
packed_refs, err)) {
if (verify_refname_available(update->refname,
&affected_refnames, NULL,
err)) {
ret = TRANSACTION_NAME_CONFLICT;
goto cleanup;
}
Expand Down

0 comments on commit d336123

Please sign in to comment.