Skip to content

Commit c45889f

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 929d044 commit c45889f

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

builtin/fetch.c

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ static int s_update_ref(const char *action,
589589
char *rla = getenv("GIT_REFLOG_ACTION");
590590
struct ref_transaction *transaction;
591591
struct strbuf err = STRBUF_INIT;
592-
int ret, df_conflict = 0;
592+
int ret;
593593

594594
if (dry_run)
595595
return 0;
@@ -598,30 +598,37 @@ static int s_update_ref(const char *action,
598598
msg = xstrfmt("%s: %s", rla, action);
599599

600600
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);
609609
if (ret) {
610-
df_conflict = (ret == TRANSACTION_NAME_CONFLICT);
611-
goto fail;
610+
ret = STORE_REF_ERROR_OTHER;
611+
goto out;
612612
}
613613

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:
614626
ref_transaction_free(transaction);
627+
if (ret)
628+
error("%s", err.buf);
615629
strbuf_release(&err);
616630
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;
625632
}
626633

627634
static int refcol_width = 10;

0 commit comments

Comments
 (0)