Skip to content

Commit bb6dd0e

Browse files
committed
Merge branch 'js/builtin-rebase-perf-fix'
Code clean-up with correction to make the reimplemented "git rebase" a more faithful rewrite of the original, which also regains performance. * js/builtin-rebase-perf-fix: built-in rebase: reinstate `checkout -q` behavior where appropriate rebase: prepare reset_head() for more flags rebase: consolidate clean-up code before leaving reset_head()
2 parents 5d90463 + bac2a1e commit bb6dd0e

File tree

1 file changed

+46
-33
lines changed

1 file changed

+46
-33
lines changed

builtin/rebase.c

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -522,12 +522,17 @@ static int run_specific_rebase(struct rebase_options *opts)
522522

523523
#define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION"
524524

525+
#define RESET_HEAD_DETACH (1<<0)
526+
#define RESET_HEAD_HARD (1<<1)
527+
525528
static int reset_head(struct object_id *oid, const char *action,
526-
const char *switch_to_branch, int detach_head,
529+
const char *switch_to_branch, unsigned flags,
527530
const char *reflog_orig_head, const char *reflog_head)
528531
{
532+
unsigned detach_head = flags & RESET_HEAD_DETACH;
533+
unsigned reset_hard = flags & RESET_HEAD_HARD;
529534
struct object_id head_oid;
530-
struct tree_desc desc;
535+
struct tree_desc desc[2] = { { NULL }, { NULL } };
531536
struct lock_file lock = LOCK_INIT;
532537
struct unpack_trees_options unpack_tree_opts;
533538
struct tree *tree;
@@ -536,60 +541,62 @@ static int reset_head(struct object_id *oid, const char *action,
536541
size_t prefix_len;
537542
struct object_id *orig = NULL, oid_orig,
538543
*old_orig = NULL, oid_old_orig;
539-
int ret = 0;
544+
int ret = 0, nr = 0;
540545

541546
if (switch_to_branch && !starts_with(switch_to_branch, "refs/"))
542547
BUG("Not a fully qualified branch: '%s'", switch_to_branch);
543548

544-
if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0)
545-
return -1;
549+
if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0) {
550+
ret = -1;
551+
goto leave_reset_head;
552+
}
546553

547-
if (!oid) {
548-
if (get_oid("HEAD", &head_oid)) {
549-
rollback_lock_file(&lock);
550-
return error(_("could not determine HEAD revision"));
551-
}
552-
oid = &head_oid;
554+
if ((!oid || !reset_hard) && get_oid("HEAD", &head_oid)) {
555+
ret = error(_("could not determine HEAD revision"));
556+
goto leave_reset_head;
553557
}
554558

559+
if (!oid)
560+
oid = &head_oid;
561+
555562
memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts));
556563
setup_unpack_trees_porcelain(&unpack_tree_opts, action);
557564
unpack_tree_opts.head_idx = 1;
558565
unpack_tree_opts.src_index = the_repository->index;
559566
unpack_tree_opts.dst_index = the_repository->index;
560-
unpack_tree_opts.fn = oneway_merge;
567+
unpack_tree_opts.fn = reset_hard ? oneway_merge : twoway_merge;
561568
unpack_tree_opts.update = 1;
562569
unpack_tree_opts.merge = 1;
563570
if (!detach_head)
564571
unpack_tree_opts.reset = 1;
565572

566573
if (read_index_unmerged(the_repository->index) < 0) {
567-
rollback_lock_file(&lock);
568-
return error(_("could not read index"));
574+
ret = error(_("could not read index"));
575+
goto leave_reset_head;
569576
}
570577

571-
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;
578+
if (!reset_hard && !fill_tree_descriptor(&desc[nr++], &head_oid)) {
579+
ret = error(_("failed to find tree of %s"), oid_to_hex(oid));
580+
goto leave_reset_head;
576581
}
577582

578-
if (unpack_trees(1, &desc, &unpack_tree_opts)) {
579-
rollback_lock_file(&lock);
580-
free((void *)desc.buffer);
581-
return -1;
583+
if (!fill_tree_descriptor(&desc[nr++], oid)) {
584+
ret = error(_("failed to find tree of %s"), oid_to_hex(oid));
585+
goto leave_reset_head;
586+
}
587+
588+
if (unpack_trees(nr, desc, &unpack_tree_opts)) {
589+
ret = -1;
590+
goto leave_reset_head;
582591
}
583592

584593
tree = parse_tree_indirect(oid);
585594
prime_cache_tree(the_repository->index, tree);
586595

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

594601
reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
595602
strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : "rebase");
@@ -623,7 +630,11 @@ static int reset_head(struct object_id *oid, const char *action,
623630
UPDATE_REFS_MSG_ON_ERR);
624631
}
625632

633+
leave_reset_head:
626634
strbuf_release(&msg);
635+
rollback_lock_file(&lock);
636+
while (nr)
637+
free((void *)desc[--nr].buffer);
627638
return ret;
628639
}
629640

@@ -1007,7 +1018,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
10071018
rerere_clear(&merge_rr);
10081019
string_list_clear(&merge_rr, 1);
10091020

1010-
if (reset_head(NULL, "reset", NULL, 0, NULL, NULL) < 0)
1021+
if (reset_head(NULL, "reset", NULL, RESET_HEAD_HARD,
1022+
NULL, NULL) < 0)
10111023
die(_("could not discard worktree changes"));
10121024
if (read_basic_state(&options))
10131025
exit(1);
@@ -1023,7 +1035,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
10231035
if (read_basic_state(&options))
10241036
exit(1);
10251037
if (reset_head(&options.orig_head, "reset",
1026-
options.head_name, 0, NULL, NULL) < 0)
1038+
options.head_name, RESET_HEAD_HARD,
1039+
NULL, NULL) < 0)
10271040
die(_("could not move back to %s"),
10281041
oid_to_hex(&options.orig_head));
10291042
ret = finish_rebase(&options);
@@ -1387,7 +1400,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
13871400
write_file(autostash, "%s", oid_to_hex(&oid));
13881401
printf(_("Created autostash: %s\n"), buf.buf);
13891402
if (reset_head(&head->object.oid, "reset --hard",
1390-
NULL, 0, NULL, NULL) < 0)
1403+
NULL, RESET_HEAD_HARD, NULL, NULL) < 0)
13911404
die(_("could not reset --hard"));
13921405
printf(_("HEAD is now at %s"),
13931406
find_unique_abbrev(&head->object.oid,
@@ -1507,8 +1520,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
15071520
"it...\n"));
15081521

15091522
strbuf_addf(&msg, "rebase: checkout %s", options.onto_name);
1510-
if (reset_head(&options.onto->object.oid, "checkout", NULL, 1,
1511-
NULL, msg.buf))
1523+
if (reset_head(&options.onto->object.oid, "checkout", NULL,
1524+
RESET_HEAD_DETACH, NULL, msg.buf))
15121525
die(_("Could not detach HEAD"));
15131526
strbuf_release(&msg);
15141527

0 commit comments

Comments
 (0)