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>

Ronnie Sahlberg committed Nov 10, 2015 at 12:42 UTC d336123160a4d9d31b01ae096ed3cdf135f92216
1 file changed +40 -26
refs.c
+40 -26
@@ -279,7 +279,7 @@ struct ref_dir {
279 * presence of an empty subdirectory does not block the creation of a
280 * similarly-named reference. (The fact that reference names with the
281 * 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().)
283 *
284 * Please note that the name field contains the fully-qualified
285 * 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)
897 /*
898 * Return 0 if a reference named refname could be created without
899 * 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.
901 */
902 static int verify_refname_available_dir(const char *refname,
903 const struct string_list *extras,
@@ -3120,6 +3108,40 @@ out:
3108 return ret;
3109 }
3110
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 +
3145 static int rename_ref_available(const char *oldname, const char *newname)
3146 {
3147 struct string_list skip = STRING_LIST_INIT_NODUP;
@@ -3127,10 +3149,7 @@ static int rename_ref_available(const char *oldname, const char *newname)
3149 int ret;
3150
3151 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);
3153 if (!ret)
3154 error("%s", err.buf);
3155
@@ -4334,8 +4353,6 @@ static int ref_present(const char *refname,
4353 int initial_ref_transaction_commit(struct ref_transaction *transaction,
4354 struct strbuf *err)
4355 {
4337 - struct ref_dir *loose_refs = get_loose_refs(&ref_cache);
4338 - struct ref_dir *packed_refs = get_packed_refs(&ref_cache);
4356 int ret = 0, i;
4357 int n = transaction->nr;
4358 struct ref_update **updates = transaction->updates;
@@ -4376,12 +4393,9 @@ int initial_ref_transaction_commit(struct ref_transaction *transaction,
4393 if ((update->flags & REF_HAVE_OLD) &&
4394 !is_null_sha1(update->old_sha1))
4395 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)) {
4399 ret = TRANSACTION_NAME_CONFLICT;
4400 goto cleanup;
4401 }