verify_refname_available(): report errors via a "struct strbuf *err"
It shouldn't be spewing errors directly to stderr. For now, change its callers to spew the errors to stderr. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Michael Haggerty committed
May 11, 2015 at 17:25 UTC
1146f17e2ce6054b502a46bafc102312c60a8380
1 file changed
+30
-20
refs.c
+30
-20
@@ -860,12 +860,12 @@ static int nonmatching_ref_fn(struct ref_entry *entry, void *vdata)
860
/*
861
* Return 0 if a reference named refname could be created without
862
* conflicting with the name of an existing reference in dir.
863
- * Otherwise, return a negative value. If extras is non-NULL, it is a
864
- * list of additional refnames with which refname is not allowed to
865
- * conflict. If skip is non-NULL, ignore potential conflicts with refs
866
- * in skip (e.g., because they are scheduled for deletion in the same
867
- * operation). Behavior is undefined if the same name is listed in
868
- * both extras and skip.
863
+ * Otherwise, return a negative value and write an explanation to err.
864
+ * If extras is non-NULL, it is a list of additional refnames with
865
+ * which refname is not allowed to conflict. If skip is non-NULL,
866
+ * ignore potential conflicts with refs in skip (e.g., because they
867
+ * are scheduled for deletion in the same operation). Behavior is
868
+ * undefined if the same name is listed in both extras and skip.
869
*
870
* Two reference names conflict if one of them exactly matches the
871
* leading components of the other; e.g., "refs/foo/bar" conflicts
@@ -877,7 +877,8 @@ static int nonmatching_ref_fn(struct ref_entry *entry, void *vdata)
877
static int verify_refname_available(const char *refname,
878
const struct string_list *extras,
879
const struct string_list *skip,
880
- struct ref_dir *dir)
880
+ struct ref_dir *dir,
881
+ struct strbuf *err)
882
{
883
const char *slash;
884
int pos;
@@ -889,6 +890,8 @@ static int verify_refname_available(const char *refname,
890
* refname is "refs/foo/bar".
891
*/
892
893
+ assert(err);
894
+
895
strbuf_grow(&dirname, strlen(refname) + 1);
896
for (slash = strchr(refname, '/'); slash; slash = strchr(slash + 1, '/')) {
897
/* Expand dirname to the new prefix, not including the trailing slash: */
@@ -908,16 +911,16 @@ static int verify_refname_available(const char *refname,
911
* a proper prefix of refname; e.g.,
912
* "refs/foo", and is not in skip.
913
*/
911
- error("'%s' exists; cannot create '%s'",
912
- dirname.buf, refname);
914
+ strbuf_addf(err, "'%s' exists; cannot create '%s'",
915
+ dirname.buf, refname);
916
goto cleanup;
917
}
918
}
919
920
if (extras && string_list_has_string(extras, dirname.buf) &&
921
(!skip || !string_list_has_string(skip, dirname.buf))) {
919
- error("cannot process '%s' and '%s' at the same time",
920
- refname, dirname.buf);
922
+ strbuf_addf(err, "cannot process '%s' and '%s' at the same time",
923
+ refname, dirname.buf);
924
goto cleanup;
925
}
926
@@ -976,8 +979,8 @@ static int verify_refname_available(const char *refname,
979
dir = get_ref_dir(dir->entries[pos]);
980
sort_ref_dir(dir);
981
if (do_for_each_entry_in_dir(dir, 0, nonmatching_ref_fn, &data)) {
979
- error("'%s' exists; cannot create '%s'",
980
- data.conflicting_refname, refname);
982
+ strbuf_addf(err, "'%s' exists; cannot create '%s'",
983
+ data.conflicting_refname, refname);
984
goto cleanup;
985
}
986
}
@@ -1000,8 +1003,8 @@ static int verify_refname_available(const char *refname,
1003
break;
1004
1005
if (!skip || !string_list_has_string(skip, extra_refname)) {
1003
- error("cannot process '%s' and '%s' at the same time",
1004
- refname, extra_refname);
1006
+ strbuf_addf(err, "cannot process '%s' and '%s' at the same time",
1007
+ refname, extra_refname);
1008
goto cleanup;
1009
}
1010
}
@@ -2340,6 +2343,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
2343
int mustexist = (old_sha1 && !is_null_sha1(old_sha1));
2344
int resolve_flags = 0;
2345
int attempts_remaining = 3;
2346
+ struct strbuf err = STRBUF_INIT;
2347
2348
lock = xcalloc(1, sizeof(struct ref_lock));
2349
lock->lock_fd = -1;
@@ -2384,7 +2388,9 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
2388
* our refname.
2389
*/
2390
if (is_null_sha1(lock->old_sha1) &&
2387
- verify_refname_available(refname, extras, skip, get_packed_refs(&ref_cache))) {
2391
+ verify_refname_available(refname, extras, skip,
2392
+ get_packed_refs(&ref_cache), &err)) {
2393
+ error("%s", err.buf);
2394
last_errno = ENOTDIR;
2395
goto error_return;
2396
}
@@ -2425,10 +2431,8 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
2431
*/
2432
goto retry;
2433
else {
2428
- struct strbuf err = STRBUF_INIT;
2434
unable_to_lock_message(ref_file, errno, &err);
2435
error("%s", err.buf);
2431
- strbuf_release(&err);
2436
goto error_return;
2437
}
2438
}
@@ -2436,6 +2440,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
2440
2441
error_return:
2442
unlock_ref(lock);
2443
+ strbuf_release(&err);
2444
errno = last_errno;
2445
return NULL;
2446
}
@@ -2822,14 +2827,19 @@ static int rename_tmp_log(const char *newrefname)
2827
static int rename_ref_available(const char *oldname, const char *newname)
2828
{
2829
struct string_list skip = STRING_LIST_INIT_NODUP;
2830
+ struct strbuf err = STRBUF_INIT;
2831
int ret;
2832
2833
string_list_insert(&skip, oldname);
2834
ret = !verify_refname_available(newname, NULL, &skip,
2829
- get_packed_refs(&ref_cache))
2835
+ get_packed_refs(&ref_cache), &err)
2836
&& !verify_refname_available(newname, NULL, &skip,
2831
- get_loose_refs(&ref_cache));
2837
+ get_loose_refs(&ref_cache), &err);
2838
+ if (!ret)
2839
+ error("%s", err.buf);
2840
+
2841
string_list_clear(&skip, 0);
2842
+ strbuf_release(&err);
2843
return ret;
2844
}
2845