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 <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Johannes Schindelin committed
Nov 12, 2018 at 03:44 UTC
3249c1251ed30c314f0dd152023e7d52d3beef29
1 file changed
+18
-18
builtin/rebase.c
+18
-18
@@ -541,13 +541,15 @@ static int reset_head(struct object_id *oid, const char *action,
541
if (switch_to_branch && !starts_with(switch_to_branch, "refs/"))
542
BUG("Not a fully qualified branch: '%s'", switch_to_branch);
543
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
+ }
548
549
if (!oid) {
550
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;
553
}
554
oid = &head_oid;
555
}
@@ -564,32 +566,27 @@ static int reset_head(struct object_id *oid, const char *action,
566
unpack_tree_opts.reset = 1;
567
568
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;
571
}
572
573
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;
576
}
577
578
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;
581
}
582
583
tree = parse_tree_indirect(oid);
584
prime_cache_tree(the_repository->index, tree);
585
587
- if (write_locked_index(the_repository->index, &lock, COMMIT_LOCK) < 0)
586
+ if (write_locked_index(the_repository->index, &lock, COMMIT_LOCK) < 0) {
587
ret = error(_("could not write index"));
589
- free((void *)desc.buffer);
590
-
591
- if (ret)
592
- return ret;
588
+ goto leave_reset_head;
589
+ }
590
591
reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
592
strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : "rebase");
@@ -622,7 +619,10 @@ static int reset_head(struct object_id *oid, const char *action,
619
UPDATE_REFS_MSG_ON_ERR);
620
}
621
622
+leave_reset_head:
623
strbuf_release(&msg);
624
+ rollback_lock_file(&lock);
625
+ free((void *)desc.buffer);
626
return ret;
627
}
628