Skip to content

Commit 1146f17

Browse files
mhaggergitster
authored andcommitted
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 <[email protected]>
1 parent 5baf37d commit 1146f17

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

refs.c

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -860,12 +860,12 @@ static int nonmatching_ref_fn(struct ref_entry *entry, void *vdata)
860860
/*
861861
* Return 0 if a reference named refname could be created without
862862
* 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.
869869
*
870870
* Two reference names conflict if one of them exactly matches the
871871
* 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)
877877
static int verify_refname_available(const char *refname,
878878
const struct string_list *extras,
879879
const struct string_list *skip,
880-
struct ref_dir *dir)
880+
struct ref_dir *dir,
881+
struct strbuf *err)
881882
{
882883
const char *slash;
883884
int pos;
@@ -889,6 +890,8 @@ static int verify_refname_available(const char *refname,
889890
* refname is "refs/foo/bar".
890891
*/
891892

893+
assert(err);
894+
892895
strbuf_grow(&dirname, strlen(refname) + 1);
893896
for (slash = strchr(refname, '/'); slash; slash = strchr(slash + 1, '/')) {
894897
/* Expand dirname to the new prefix, not including the trailing slash: */
@@ -908,16 +911,16 @@ static int verify_refname_available(const char *refname,
908911
* a proper prefix of refname; e.g.,
909912
* "refs/foo", and is not in skip.
910913
*/
911-
error("'%s' exists; cannot create '%s'",
912-
dirname.buf, refname);
914+
strbuf_addf(err, "'%s' exists; cannot create '%s'",
915+
dirname.buf, refname);
913916
goto cleanup;
914917
}
915918
}
916919

917920
if (extras && string_list_has_string(extras, dirname.buf) &&
918921
(!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);
921924
goto cleanup;
922925
}
923926

@@ -976,8 +979,8 @@ static int verify_refname_available(const char *refname,
976979
dir = get_ref_dir(dir->entries[pos]);
977980
sort_ref_dir(dir);
978981
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);
981984
goto cleanup;
982985
}
983986
}
@@ -1000,8 +1003,8 @@ static int verify_refname_available(const char *refname,
10001003
break;
10011004

10021005
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);
10051008
goto cleanup;
10061009
}
10071010
}
@@ -2340,6 +2343,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
23402343
int mustexist = (old_sha1 && !is_null_sha1(old_sha1));
23412344
int resolve_flags = 0;
23422345
int attempts_remaining = 3;
2346+
struct strbuf err = STRBUF_INIT;
23432347

23442348
lock = xcalloc(1, sizeof(struct ref_lock));
23452349
lock->lock_fd = -1;
@@ -2384,7 +2388,9 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
23842388
* our refname.
23852389
*/
23862390
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);
23882394
last_errno = ENOTDIR;
23892395
goto error_return;
23902396
}
@@ -2425,17 +2431,16 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
24252431
*/
24262432
goto retry;
24272433
else {
2428-
struct strbuf err = STRBUF_INIT;
24292434
unable_to_lock_message(ref_file, errno, &err);
24302435
error("%s", err.buf);
2431-
strbuf_release(&err);
24322436
goto error_return;
24332437
}
24342438
}
24352439
return old_sha1 ? verify_lock(lock, old_sha1, mustexist) : lock;
24362440

24372441
error_return:
24382442
unlock_ref(lock);
2443+
strbuf_release(&err);
24392444
errno = last_errno;
24402445
return NULL;
24412446
}
@@ -2822,14 +2827,19 @@ static int rename_tmp_log(const char *newrefname)
28222827
static int rename_ref_available(const char *oldname, const char *newname)
28232828
{
28242829
struct string_list skip = STRING_LIST_INIT_NODUP;
2830+
struct strbuf err = STRBUF_INIT;
28252831
int ret;
28262832

28272833
string_list_insert(&skip, oldname);
28282834
ret = !verify_refname_available(newname, NULL, &skip,
2829-
get_packed_refs(&ref_cache))
2835+
get_packed_refs(&ref_cache), &err)
28302836
&& !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+
28322841
string_list_clear(&skip, 0);
2842+
strbuf_release(&err);
28332843
return ret;
28342844
}
28352845

0 commit comments

Comments
 (0)