]> git.ipfire.org Git - thirdparty/git.git/commitdiff
fetch: refactor `s_update_ref` to use common exit path
authorPatrick Steinhardt <ps@pks.im>
Tue, 12 Jan 2021 12:27:43 +0000 (13:27 +0100)
committerJunio C Hamano <gitster@pobox.com>
Tue, 12 Jan 2021 20:06:15 +0000 (12:06 -0800)
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>
builtin/fetch.c

index e317e828cda31f6a18c8b5ac2ffd4f78c4c47c0e..b24a9e09a46f075fad963f56ee0addce3c73b70e 100644 (file)
@@ -589,7 +589,7 @@ static int s_update_ref(const char *action,
        char *rla = getenv("GIT_REFLOG_ACTION");
        struct ref_transaction *transaction;
        struct strbuf err = STRBUF_INIT;
-       int ret, df_conflict = 0;
+       int ret;
 
        if (dry_run)
                return 0;
@@ -598,30 +598,37 @@ static int s_update_ref(const char *action,
        msg = xstrfmt("%s: %s", rla, action);
 
        transaction = ref_transaction_begin(&err);
-       if (!transaction ||
-           ref_transaction_update(transaction, ref->name,
-                                  &ref->new_oid,
-                                  check_old ? &ref->old_oid : NULL,
-                                  0, msg, &err))
-               goto fail;
-
-       ret = ref_transaction_commit(transaction, &err);
+       if (!transaction) {
+               ret = STORE_REF_ERROR_OTHER;
+               goto out;
+       }
+
+       ret = ref_transaction_update(transaction, ref->name, &ref->new_oid,
+                                    check_old ? &ref->old_oid : NULL,
+                                    0, msg, &err);
        if (ret) {
-               df_conflict = (ret == TRANSACTION_NAME_CONFLICT);
-               goto fail;
+               ret = STORE_REF_ERROR_OTHER;
+               goto out;
        }
 
+       switch (ref_transaction_commit(transaction, &err)) {
+       case 0:
+               break;
+       case TRANSACTION_NAME_CONFLICT:
+               ret = STORE_REF_ERROR_DF_CONFLICT;
+               goto out;
+       default:
+               ret = STORE_REF_ERROR_OTHER;
+               goto out;
+       }
+
+out:
        ref_transaction_free(transaction);
+       if (ret)
+               error("%s", err.buf);
        strbuf_release(&err);
        free(msg);
-       return 0;
-fail:
-       ref_transaction_free(transaction);
-       error("%s", err.buf);
-       strbuf_release(&err);
-       free(msg);
-       return df_conflict ? STORE_REF_ERROR_DF_CONFLICT
-                          : STORE_REF_ERROR_OTHER;
+       return ret;
 }
 
 static int refcol_width = 10;