fetch: refactor `s_update_ref` to use common exit path

The cleanup code in `s_update_ref()` is currently duplicated for both succesful and erroneous exit paths. This commit refactors the function to have a shared exit path for both cases to remove the duplication. Suggested-by: Christian Couder <christian.couder@gmail.com> Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Jan 12, 2021 at 13:27 UTC c45889f10453864130ccbfa45815e2f4e384ab5e
1 file changed +26 -19
builtin/fetch.c
+26 -19
@@ -589,7 +589,7 @@ static int s_update_ref(const char *action,
589 char *rla = getenv("GIT_REFLOG_ACTION");
590 struct ref_transaction *transaction;
591 struct strbuf err = STRBUF_INIT;
592 - int ret, df_conflict = 0;
592 + int ret;
593
594 if (dry_run)
595 return 0;
@@ -598,30 +598,37 @@ static int s_update_ref(const char *action,
598 msg = xstrfmt("%s: %s", rla, action);
599
600 transaction = ref_transaction_begin(&err);
601 - if (!transaction ||
602 - ref_transaction_update(transaction, ref->name,
603 - &ref->new_oid,
604 - check_old ? &ref->old_oid : NULL,
605 - 0, msg, &err))
606 - goto fail;
607 -
608 - ret = ref_transaction_commit(transaction, &err);
601 + if (!transaction) {
602 + ret = STORE_REF_ERROR_OTHER;
603 + goto out;
604 + }
605 +
606 + ret = ref_transaction_update(transaction, ref->name, &ref->new_oid,
607 + check_old ? &ref->old_oid : NULL,
608 + 0, msg, &err);
609 if (ret) {
610 - df_conflict = (ret == TRANSACTION_NAME_CONFLICT);
611 - goto fail;
610 + ret = STORE_REF_ERROR_OTHER;
611 + goto out;
612 }
613
614 + switch (ref_transaction_commit(transaction, &err)) {
615 + case 0:
616 + break;
617 + case TRANSACTION_NAME_CONFLICT:
618 + ret = STORE_REF_ERROR_DF_CONFLICT;
619 + goto out;
620 + default:
621 + ret = STORE_REF_ERROR_OTHER;
622 + goto out;
623 + }
624 +
625 +out:
626 ref_transaction_free(transaction);
627 + if (ret)
628 + error("%s", err.buf);
629 strbuf_release(&err);
630 free(msg);
617 - return 0;
618 -fail:
619 - ref_transaction_free(transaction);
620 - error("%s", err.buf);
621 - strbuf_release(&err);
622 - free(msg);
623 - return df_conflict ? STORE_REF_ERROR_DF_CONFLICT
624 - : STORE_REF_ERROR_OTHER;
631 + return ret;
632 }
633
634 static int refcol_width = 10;