Skip to content

Commit 222368c

Browse files
stefanbellergitster
authored andcommitted
receive-pack.c: move transaction handling in a central place
This moves all code related to transactions into the execute_commands_non_atomic function. This includes beginning and committing the transaction as well as dealing with the errors which may occur during the begin and commit phase of a transaction. No functional changes intended. Helped-by: Eric Sunshine <[email protected]> Signed-off-by: Stefan Beller <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a1a2614 commit 222368c

File tree

1 file changed

+37
-14
lines changed

1 file changed

+37
-14
lines changed

builtin/receive-pack.c

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ static const char *NONCE_SLOP = "SLOP";
6666
static const char *nonce_status;
6767
static long nonce_stamp_slop;
6868
static unsigned long nonce_stamp_slop_limit;
69+
static struct ref_transaction *transaction;
6970

7071
static enum deny_action parse_deny_action(const char *var, const char *value)
7172
{
@@ -821,6 +822,7 @@ static const char *update(struct command *cmd, struct shallow_info *si)
821822
}
822823

823824
if (is_null_sha1(new_sha1)) {
825+
struct strbuf err = STRBUF_INIT;
824826
if (!parse_object(old_sha1)) {
825827
old_sha1 = NULL;
826828
if (ref_exists(name)) {
@@ -830,35 +832,36 @@ static const char *update(struct command *cmd, struct shallow_info *si)
830832
cmd->did_not_exist = 1;
831833
}
832834
}
833-
if (delete_ref(namespaced_name, old_sha1, 0)) {
834-
rp_error("failed to delete %s", name);
835+
if (ref_transaction_delete(transaction,
836+
namespaced_name,
837+
old_sha1,
838+
0, old_sha1 != NULL,
839+
"push", &err)) {
840+
rp_error("%s", err.buf);
841+
strbuf_release(&err);
835842
return "failed to delete";
836843
}
844+
strbuf_release(&err);
837845
return NULL; /* good */
838846
}
839847
else {
840848
struct strbuf err = STRBUF_INIT;
841-
struct ref_transaction *transaction;
842-
843849
if (shallow_update && si->shallow_ref[cmd->index] &&
844850
update_shallow_ref(cmd, si))
845851
return "shallow error";
846852

847-
transaction = ref_transaction_begin(&err);
848-
if (!transaction ||
849-
ref_transaction_update(transaction, namespaced_name,
850-
new_sha1, old_sha1, 0, 1, "push",
851-
&err) ||
852-
ref_transaction_commit(transaction, &err)) {
853-
ref_transaction_free(transaction);
854-
853+
if (ref_transaction_update(transaction,
854+
namespaced_name,
855+
new_sha1, old_sha1,
856+
0, 1, "push",
857+
&err)) {
855858
rp_error("%s", err.buf);
856859
strbuf_release(&err);
860+
857861
return "failed to update ref";
858862
}
859-
860-
ref_transaction_free(transaction);
861863
strbuf_release(&err);
864+
862865
return NULL; /* good */
863866
}
864867
}
@@ -1068,12 +1071,32 @@ static void execute_commands_non_atomic(struct command *commands,
10681071
struct shallow_info *si)
10691072
{
10701073
struct command *cmd;
1074+
struct strbuf err = STRBUF_INIT;
1075+
10711076
for (cmd = commands; cmd; cmd = cmd->next) {
10721077
if (!should_process_cmd(cmd))
10731078
continue;
10741079

1080+
transaction = ref_transaction_begin(&err);
1081+
if (!transaction) {
1082+
rp_error("%s", err.buf);
1083+
strbuf_reset(&err);
1084+
cmd->error_string = "transaction failed to start";
1085+
continue;
1086+
}
1087+
10751088
cmd->error_string = update(cmd, si);
1089+
1090+
if (!cmd->error_string
1091+
&& ref_transaction_commit(transaction, &err)) {
1092+
rp_error("%s", err.buf);
1093+
strbuf_reset(&err);
1094+
cmd->error_string = "failed to update ref";
1095+
}
1096+
ref_transaction_free(transaction);
10761097
}
1098+
1099+
strbuf_release(&err);
10771100
}
10781101

10791102
static void execute_commands(struct command *commands,

0 commit comments

Comments
 (0)