Skip to content

Commit 6629ea2

Browse files
rsahlberggitster
authored andcommitted
receive-pack.c: use a reference transaction for updating the refs
Wrap all the ref updates inside a transaction. In the new API there is no distinction between failure to lock and failure to write a ref. Both can be permanent (e.g., a ref "refs/heads/topic" is blocking creation of the lock file "refs/heads/topic/1.lock") or transient (e.g., file system full) and there's no clear difference in how the client should respond, so replace the two statuses "failed to lock" and "failed to write" with a single status "failed to update ref". In both cases a more detailed message is sent by sideband to diagnose the problem. Example, before: error: there are still refs under 'refs/heads/topic' remote: error: failed to lock refs/heads/topic To foo ! [remote rejected] HEAD -> topic (failed to lock) After: error: there are still refs under 'refs/heads/topic' remote: error: Cannot lock the ref 'refs/heads/topic'. To foo ! [remote rejected] HEAD -> topic (failed to update ref) Signed-off-by: Ronnie Sahlberg <sahlberg@google.com> Reviewed-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent b4d75ac commit 6629ea2

1 file changed

Lines changed: 16 additions & 9 deletions

File tree

builtin/receive-pack.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,6 @@ static const char *update(struct command *cmd, struct shallow_info *si)
475475
const char *namespaced_name;
476476
unsigned char *old_sha1 = cmd->old_sha1;
477477
unsigned char *new_sha1 = cmd->new_sha1;
478-
struct ref_lock *lock;
479478

480479
/* only refs/... are allowed */
481480
if (!starts_with(name, "refs/") || check_refname_format(name + 5, 0)) {
@@ -576,19 +575,27 @@ static const char *update(struct command *cmd, struct shallow_info *si)
576575
return NULL; /* good */
577576
}
578577
else {
578+
struct strbuf err = STRBUF_INIT;
579+
struct ref_transaction *transaction;
580+
579581
if (shallow_update && si->shallow_ref[cmd->index] &&
580582
update_shallow_ref(cmd, si))
581583
return "shallow error";
582584

583-
lock = lock_any_ref_for_update(namespaced_name, old_sha1,
584-
0, NULL);
585-
if (!lock) {
586-
rp_error("failed to lock %s", name);
587-
return "failed to lock";
588-
}
589-
if (write_ref_sha1(lock, new_sha1, "push")) {
590-
return "failed to write"; /* error() already called */
585+
transaction = ref_transaction_begin(&err);
586+
if (!transaction ||
587+
ref_transaction_update(transaction, namespaced_name,
588+
new_sha1, old_sha1, 0, 1, &err) ||
589+
ref_transaction_commit(transaction, "push", &err)) {
590+
ref_transaction_free(transaction);
591+
592+
rp_error("%s", err.buf);
593+
strbuf_release(&err);
594+
return "failed to update ref";
591595
}
596+
597+
ref_transaction_free(transaction);
598+
strbuf_release(&err);
592599
return NULL; /* good */
593600
}
594601
}

0 commit comments

Comments
 (0)