Skip to content

Commit 3249c12

Browse files
dschogitster
authored andcommitted
rebase: consolidate clean-up code before leaving reset_head()
The same clean-up code is repeated quite a few times; Let's DRY up the code some. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8858448 commit 3249c12

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

builtin/rebase.c

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -541,13 +541,15 @@ static int reset_head(struct object_id *oid, const char *action,
541541
if (switch_to_branch && !starts_with(switch_to_branch, "refs/"))
542542
BUG("Not a fully qualified branch: '%s'", switch_to_branch);
543543

544-
if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0)
545-
return -1;
544+
if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0) {
545+
ret = -1;
546+
goto leave_reset_head;
547+
}
546548

547549
if (!oid) {
548550
if (get_oid("HEAD", &head_oid)) {
549-
rollback_lock_file(&lock);
550-
return error(_("could not determine HEAD revision"));
551+
ret = error(_("could not determine HEAD revision"));
552+
goto leave_reset_head;
551553
}
552554
oid = &head_oid;
553555
}
@@ -564,32 +566,27 @@ static int reset_head(struct object_id *oid, const char *action,
564566
unpack_tree_opts.reset = 1;
565567

566568
if (read_index_unmerged(the_repository->index) < 0) {
567-
rollback_lock_file(&lock);
568-
return error(_("could not read index"));
569+
ret = error(_("could not read index"));
570+
goto leave_reset_head;
569571
}
570572

571573
if (!fill_tree_descriptor(&desc, oid)) {
572-
error(_("failed to find tree of %s"), oid_to_hex(oid));
573-
rollback_lock_file(&lock);
574-
free((void *)desc.buffer);
575-
return -1;
574+
ret = error(_("failed to find tree of %s"), oid_to_hex(oid));
575+
goto leave_reset_head;
576576
}
577577

578578
if (unpack_trees(1, &desc, &unpack_tree_opts)) {
579-
rollback_lock_file(&lock);
580-
free((void *)desc.buffer);
581-
return -1;
579+
ret = -1;
580+
goto leave_reset_head;
582581
}
583582

584583
tree = parse_tree_indirect(oid);
585584
prime_cache_tree(the_repository->index, tree);
586585

587-
if (write_locked_index(the_repository->index, &lock, COMMIT_LOCK) < 0)
586+
if (write_locked_index(the_repository->index, &lock, COMMIT_LOCK) < 0) {
588587
ret = error(_("could not write index"));
589-
free((void *)desc.buffer);
590-
591-
if (ret)
592-
return ret;
588+
goto leave_reset_head;
589+
}
593590

594591
reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
595592
strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : "rebase");
@@ -622,7 +619,10 @@ static int reset_head(struct object_id *oid, const char *action,
622619
UPDATE_REFS_MSG_ON_ERR);
623620
}
624621

622+
leave_reset_head:
625623
strbuf_release(&msg);
624+
rollback_lock_file(&lock);
625+
free((void *)desc.buffer);
626626
return ret;
627627
}
628628

0 commit comments

Comments
 (0)