Skip to content

Commit 4a32b2e

Browse files
mhaggergitster
authored andcommitted
lock_ref_sha1_basic(): report errors via a "struct strbuf *err"
For now, change the callers to spew the error to stderr like before. But soon we will change them to incorporate the reason for the failure into their own error messages. Signed-off-by: Michael Haggerty <[email protected]>
1 parent 1146f17 commit 4a32b2e

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

refs.c

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2333,7 +2333,8 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
23332333
const unsigned char *old_sha1,
23342334
const struct string_list *extras,
23352335
const struct string_list *skip,
2336-
unsigned int flags, int *type_p)
2336+
unsigned int flags, int *type_p,
2337+
struct strbuf *err)
23372338
{
23382339
char *ref_file;
23392340
const char *orig_refname = refname;
@@ -2343,7 +2344,8 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
23432344
int mustexist = (old_sha1 && !is_null_sha1(old_sha1));
23442345
int resolve_flags = 0;
23452346
int attempts_remaining = 3;
2346-
struct strbuf err = STRBUF_INIT;
2347+
2348+
assert(err);
23472349

23482350
lock = xcalloc(1, sizeof(struct ref_lock));
23492351
lock->lock_fd = -1;
@@ -2367,7 +2369,8 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
23672369
ref_file = git_path("%s", orig_refname);
23682370
if (remove_empty_directories(ref_file)) {
23692371
last_errno = errno;
2370-
error("there are still refs under '%s'", orig_refname);
2372+
strbuf_addf(err, "there are still refs under '%s'",
2373+
orig_refname);
23712374
goto error_return;
23722375
}
23732376
refname = resolve_ref_unsafe(orig_refname, resolve_flags,
@@ -2377,8 +2380,8 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
23772380
*type_p = type;
23782381
if (!refname) {
23792382
last_errno = errno;
2380-
error("unable to resolve reference %s: %s",
2381-
orig_refname, strerror(errno));
2383+
strbuf_addf(err, "unable to resolve reference %s: %s",
2384+
orig_refname, strerror(errno));
23822385
goto error_return;
23832386
}
23842387
/*
@@ -2389,8 +2392,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
23892392
*/
23902393
if (is_null_sha1(lock->old_sha1) &&
23912394
verify_refname_available(refname, extras, skip,
2392-
get_packed_refs(&ref_cache), &err)) {
2393-
error("%s", err.buf);
2395+
get_packed_refs(&ref_cache), err)) {
23942396
last_errno = ENOTDIR;
23952397
goto error_return;
23962398
}
@@ -2416,7 +2418,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
24162418
/* fall through */
24172419
default:
24182420
last_errno = errno;
2419-
error("unable to create directory for %s", ref_file);
2421+
strbuf_addf(err, "unable to create directory for %s", ref_file);
24202422
goto error_return;
24212423
}
24222424

@@ -2431,16 +2433,14 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
24312433
*/
24322434
goto retry;
24332435
else {
2434-
unable_to_lock_message(ref_file, errno, &err);
2435-
error("%s", err.buf);
2436+
unable_to_lock_message(ref_file, errno, err);
24362437
goto error_return;
24372438
}
24382439
}
24392440
return old_sha1 ? verify_lock(lock, old_sha1, mustexist) : lock;
24402441

24412442
error_return:
24422443
unlock_ref(lock);
2443-
strbuf_release(&err);
24442444
errno = last_errno;
24452445
return NULL;
24462446
}
@@ -2854,6 +2854,7 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
28542854
struct stat loginfo;
28552855
int log = !lstat(git_path("logs/%s", oldrefname), &loginfo);
28562856
const char *symref = NULL;
2857+
struct strbuf err = STRBUF_INIT;
28572858

28582859
if (log && S_ISLNK(loginfo.st_mode))
28592860
return error("reflog for %s is a symlink", oldrefname);
@@ -2896,8 +2897,10 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
28962897

28972898
logmoved = log;
28982899

2899-
lock = lock_ref_sha1_basic(newrefname, NULL, NULL, NULL, 0, NULL);
2900+
lock = lock_ref_sha1_basic(newrefname, NULL, NULL, NULL, 0, NULL, &err);
29002901
if (!lock) {
2902+
error("%s", err.buf);
2903+
strbuf_release(&err);
29012904
error("unable to lock %s for update", newrefname);
29022905
goto rollback;
29032906
}
@@ -2910,8 +2913,10 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
29102913
return 0;
29112914

29122915
rollback:
2913-
lock = lock_ref_sha1_basic(oldrefname, NULL, NULL, NULL, 0, NULL);
2916+
lock = lock_ref_sha1_basic(oldrefname, NULL, NULL, NULL, 0, NULL, &err);
29142917
if (!lock) {
2918+
error("%s", err.buf);
2919+
strbuf_release(&err);
29152920
error("unable to lock %s for rollback", oldrefname);
29162921
goto rollbacklog;
29172922
}
@@ -3824,11 +3829,14 @@ int ref_transaction_commit(struct ref_transaction *transaction,
38243829
update->old_sha1 : NULL),
38253830
&affected_refnames, NULL,
38263831
flags,
3827-
&update->type);
3832+
&update->type,
3833+
err);
38283834
if (!update->lock) {
38293835
ret = (errno == ENOTDIR)
38303836
? TRANSACTION_NAME_CONFLICT
38313837
: TRANSACTION_GENERIC_ERROR;
3838+
error("%s", err->buf);
3839+
strbuf_reset(err);
38323840
strbuf_addf(err, "Cannot lock the ref '%s'.",
38333841
update->refname);
38343842
goto cleanup;
@@ -4088,6 +4096,7 @@ int reflog_expire(const char *refname, const unsigned char *sha1,
40884096
char *log_file;
40894097
int status = 0;
40904098
int type;
4099+
struct strbuf err = STRBUF_INIT;
40914100

40924101
memset(&cb, 0, sizeof(cb));
40934102
cb.flags = flags;
@@ -4099,9 +4108,12 @@ int reflog_expire(const char *refname, const unsigned char *sha1,
40994108
* reference itself, plus we might need to update the
41004109
* reference if --updateref was specified:
41014110
*/
4102-
lock = lock_ref_sha1_basic(refname, sha1, NULL, NULL, 0, &type);
4103-
if (!lock)
4111+
lock = lock_ref_sha1_basic(refname, sha1, NULL, NULL, 0, &type, &err);
4112+
if (!lock) {
4113+
error("%s", err.buf);
4114+
strbuf_release(&err);
41044115
return error("cannot lock ref '%s'", refname);
4116+
}
41054117
if (!reflog_exists(refname)) {
41064118
unlock_ref(lock);
41074119
return 0;

0 commit comments

Comments
 (0)