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 <sunshine@sunshineco.com> Signed-off-by: Stefan Beller <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Stefan Beller committed Jan 7, 2015 at 19:23 UTC 222368c6456211a3b2054ce4651cb58703886965
1 file changed +37 -14
builtin/receive-pack.c
+37 -14
@@ -66,6 +66,7 @@ static const char *NONCE_SLOP = "SLOP";
66 static const char *nonce_status;
67 static long nonce_stamp_slop;
68 static unsigned long nonce_stamp_slop_limit;
69 +static struct ref_transaction *transaction;
70
71 static enum deny_action parse_deny_action(const char *var, const char *value)
72 {
@@ -821,6 +822,7 @@ static const char *update(struct command *cmd, struct shallow_info *si)
822 }
823
824 if (is_null_sha1(new_sha1)) {
825 + struct strbuf err = STRBUF_INIT;
826 if (!parse_object(old_sha1)) {
827 old_sha1 = NULL;
828 if (ref_exists(name)) {
@@ -830,35 +832,36 @@ static const char *update(struct command *cmd, struct shallow_info *si)
832 cmd->did_not_exist = 1;
833 }
834 }
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);
842 return "failed to delete";
843 }
844 + strbuf_release(&err);
845 return NULL; /* good */
846 }
847 else {
848 struct strbuf err = STRBUF_INIT;
841 - struct ref_transaction *transaction;
842 -
849 if (shallow_update && si->shallow_ref[cmd->index] &&
850 update_shallow_ref(cmd, si))
851 return "shallow error";
852
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)) {
858 rp_error("%s", err.buf);
859 strbuf_release(&err);
860 +
861 return "failed to update ref";
862 }
859 -
860 - ref_transaction_free(transaction);
863 strbuf_release(&err);
864 +
865 return NULL; /* good */
866 }
867 }
@@ -1068,12 +1071,32 @@ static void execute_commands_non_atomic(struct command *commands,
1071 struct shallow_info *si)
1072 {
1073 struct command *cmd;
1074 + struct strbuf err = STRBUF_INIT;
1075 +
1076 for (cmd = commands; cmd; cmd = cmd->next) {
1077 if (!should_process_cmd(cmd))
1078 continue;
1079
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 +
1088 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);
1097 }
1098 +
1099 + strbuf_release(&err);
1100 }
1101
1102 static void execute_commands(struct command *commands,