diff options
author | Ronnie Sahlberg <sahlberg@google.com> | 2015-11-10 12:42:32 +0100 |
---|---|---|
committer | Jeff King <peff@peff.net> | 2015-11-20 04:52:01 -0500 |
commit | d336123160a4d9d31b01ae096ed3cdf135f92216 (patch) | |
tree | 3dc060555b3d22441d6f1495f868dba28b7055cc /refs.c | |
parent | 7003b3ce21e0ddf9028e124fd9add5ac2319e112 (diff) | |
download | git-d336123160a4d9d31b01ae096ed3cdf135f92216.tar.gz |
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 <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>
Diffstat (limited to 'refs.c')
-rw-r--r-- | refs.c | 66 |
1 files changed, 40 insertions, 26 deletions
@@ -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 @@ -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, @@ -3120,6 +3108,40 @@ out: 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; @@ -3127,10 +3149,7 @@ static int rename_ref_available(const char *oldname, const char *newname) 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); @@ -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; @@ -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; } |