Skip to content

Commit 3033dbf

Browse files
committed
Merge branch 'js/builtin-rebase-perf-fix'
2 parents a083b88 + 89fa653 commit 3033dbf

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
@@ -524,12 +524,17 @@ static int run_specific_rebase(struct rebase_options *opts)
524524

525525
#define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION"
526526

527+
#define RESET_HEAD_DETACH (1<<0)
528+
#define RESET_HEAD_HARD (1<<1)
529+
527530
static int reset_head(struct object_id *oid, const char *action,
528-
const char *switch_to_branch, int detach_head,
531+
const char *switch_to_branch, unsigned flags,
529532
const char *reflog_orig_head, const char *reflog_head)
530533
{
534+
unsigned detach_head = flags & RESET_HEAD_DETACH;
535+
unsigned reset_hard = flags & RESET_HEAD_HARD;
531536
struct object_id head_oid;
532-
struct tree_desc desc;
537+
struct tree_desc desc[2] = { { NULL }, { NULL } };
533538
struct lock_file lock = LOCK_INIT;
534539
struct unpack_trees_options unpack_tree_opts;
535540
struct tree *tree;
@@ -538,60 +543,62 @@ static int reset_head(struct object_id *oid, const char *action,
538543
size_t prefix_len;
539544
struct object_id *orig = NULL, oid_orig,
540545
*old_orig = NULL, oid_old_orig;
541-
int ret = 0;
546+
int ret = 0, nr = 0;
542547

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

546-
if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0)
547-
return -1;
551+
if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0) {
552+
ret = -1;
553+
goto leave_reset_head;
554+
}
548555

549-
if (!oid) {
550-
if (get_oid("HEAD", &head_oid)) {
551-
rollback_lock_file(&lock);
552-
return error(_("could not determine HEAD revision"));
553-
}
554-
oid = &head_oid;
556+
if ((!oid || !reset_hard) && get_oid("HEAD", &head_oid)) {
557+
ret = error(_("could not determine HEAD revision"));
558+
goto leave_reset_head;
555559
}
556560

561+
if (!oid)
562+
oid = &head_oid;
563+
557564
memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts));
558565
setup_unpack_trees_porcelain(&unpack_tree_opts, action);
559566
unpack_tree_opts.head_idx = 1;
560567
unpack_tree_opts.src_index = the_repository->index;
561568
unpack_tree_opts.dst_index = the_repository->index;
562-
unpack_tree_opts.fn = oneway_merge;
569+
unpack_tree_opts.fn = reset_hard ? oneway_merge : twoway_merge;
563570
unpack_tree_opts.update = 1;
564571
unpack_tree_opts.merge = 1;
565572
if (!detach_head)
566573
unpack_tree_opts.reset = 1;
567574

568575
if (read_index_unmerged(the_repository->index) < 0) {
569-
rollback_lock_file(&lock);
570-
return error(_("could not read index"));
576+
ret = error(_("could not read index"));
577+
goto leave_reset_head;
571578
}
572579

573-
if (!fill_tree_descriptor(&desc, oid)) {
574-
error(_("failed to find tree of %s"), oid_to_hex(oid));
575-
rollback_lock_file(&lock);
576-
free((void *)desc.buffer);
577-
return -1;
580+
if (!reset_hard && !fill_tree_descriptor(&desc[nr++], &head_oid)) {
581+
ret = error(_("failed to find tree of %s"), oid_to_hex(oid));
582+
goto leave_reset_head;
578583
}
579584

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

586595
tree = parse_tree_indirect(oid);
587596
prime_cache_tree(the_repository->index, tree);
588597

589-
if (write_locked_index(the_repository->index, &lock, COMMIT_LOCK) < 0)
598+
if (write_locked_index(the_repository->index, &lock, COMMIT_LOCK) < 0) {
590599
ret = error(_("could not write index"));
591-
free((void *)desc.buffer);
592-
593-
if (ret)
594-
return ret;
600+
goto leave_reset_head;
601+
}
595602

596603
reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
597604
strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : "rebase");
@@ -625,7 +632,11 @@ static int reset_head(struct object_id *oid, const char *action,
625632
UPDATE_REFS_MSG_ON_ERR);
626633
}
627634

635+
leave_reset_head:
628636
strbuf_release(&msg);
637+
rollback_lock_file(&lock);
638+
while (nr)
639+
free((void *)desc[--nr].buffer);
629640
return ret;
630641
}
631642

@@ -1003,7 +1014,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
10031014
rerere_clear(&merge_rr);
10041015
string_list_clear(&merge_rr, 1);
10051016

1006-
if (reset_head(NULL, "reset", NULL, 0, NULL, NULL) < 0)
1017+
if (reset_head(NULL, "reset", NULL, RESET_HEAD_HARD,
1018+
NULL, NULL) < 0)
10071019
die(_("could not discard worktree changes"));
10081020
remove_branch_state();
10091021
if (read_basic_state(&options))
@@ -1020,7 +1032,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
10201032
if (read_basic_state(&options))
10211033
exit(1);
10221034
if (reset_head(&options.orig_head, "reset",
1023-
options.head_name, 0, NULL, NULL) < 0)
1035+
options.head_name, RESET_HEAD_HARD,
1036+
NULL, NULL) < 0)
10241037
die(_("could not move back to %s"),
10251038
oid_to_hex(&options.orig_head));
10261039
remove_branch_state();
@@ -1385,7 +1398,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
13851398
write_file(autostash, "%s", oid_to_hex(&oid));
13861399
printf(_("Created autostash: %s\n"), buf.buf);
13871400
if (reset_head(&head->object.oid, "reset --hard",
1388-
NULL, 0, NULL, NULL) < 0)
1401+
NULL, RESET_HEAD_HARD, NULL, NULL) < 0)
13891402
die(_("could not reset --hard"));
13901403
printf(_("HEAD is now at %s"),
13911404
find_unique_abbrev(&head->object.oid,
@@ -1505,8 +1518,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
15051518
"it...\n"));
15061519

15071520
strbuf_addf(&msg, "rebase: checkout %s", options.onto_name);
1508-
if (reset_head(&options.onto->object.oid, "checkout", NULL, 1,
1509-
NULL, msg.buf))
1521+
if (reset_head(&options.onto->object.oid, "checkout", NULL,
1522+
RESET_HEAD_DETACH, NULL, msg.buf))
15101523
die(_("Could not detach HEAD"));
15111524
strbuf_release(&msg);
15121525

0 commit comments

Comments
 (0)