Skip to content

Commit 7d0ec44

Browse files
committed
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]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent cc0dbd5 commit 7d0ec44

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
@@ -542,13 +542,15 @@ static int reset_head(struct object_id *oid, const char *action,
542542
if (switch_to_branch && !starts_with(switch_to_branch, "refs/"))
543543
BUG("Not a fully qualified branch: '%s'", switch_to_branch);
544544

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

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

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

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

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

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

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

595592
reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
596593
strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : "rebase");
@@ -624,7 +621,10 @@ static int reset_head(struct object_id *oid, const char *action,
624621
UPDATE_REFS_MSG_ON_ERR);
625622
}
626623

624+
leave_reset_head:
627625
strbuf_release(&msg);
626+
rollback_lock_file(&lock);
627+
free((void *)desc.buffer);
628628
return ret;
629629
}
630630

0 commit comments

Comments
 (0)