Skip to content

Commit 89fa653

Browse files
committed
built-in rebase: reinstate checkout -q behavior where appropriate
When we converted a `git checkout -q $onto^0` call to use `reset_head()`, we inadvertently incurred a change from a twoway_merge to a oneway_merge, as if we wanted a `git reset --hard` instead. This has performance ramifications under certain, though, as the oneway_merge needs to lstat() every single index entry whereas twoway_merge does not. So let's go back to the old behavior. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 7b3704b commit 89fa653

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

builtin/rebase.c

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -524,14 +524,16 @@ static int run_specific_rebase(struct rebase_options *opts)
524524
#define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION"
525525

526526
#define RESET_HEAD_DETACH (1<<0)
527+
#define RESET_HEAD_HARD (1<<1)
527528

528529
static int reset_head(struct object_id *oid, const char *action,
529530
const char *switch_to_branch, unsigned flags,
530531
const char *reflog_orig_head, const char *reflog_head)
531532
{
532533
unsigned detach_head = flags & RESET_HEAD_DETACH;
534+
unsigned reset_hard = flags & RESET_HEAD_HARD;
533535
struct object_id head_oid;
534-
struct tree_desc desc;
536+
struct tree_desc desc[2] = { { NULL }, { NULL } };
535537
struct lock_file lock = LOCK_INIT;
536538
struct unpack_trees_options unpack_tree_opts;
537539
struct tree *tree;
@@ -540,7 +542,7 @@ static int reset_head(struct object_id *oid, const char *action,
540542
size_t prefix_len;
541543
struct object_id *orig = NULL, oid_orig,
542544
*old_orig = NULL, oid_old_orig;
543-
int ret = 0;
545+
int ret = 0, nr = 0;
544546

545547
if (switch_to_branch && !starts_with(switch_to_branch, "refs/"))
546548
BUG("Not a fully qualified branch: '%s'", switch_to_branch);
@@ -550,20 +552,20 @@ static int reset_head(struct object_id *oid, const char *action,
550552
goto leave_reset_head;
551553
}
552554

553-
if (!oid) {
554-
if (get_oid("HEAD", &head_oid)) {
555-
ret = error(_("could not determine HEAD revision"));
556-
goto leave_reset_head;
557-
}
558-
oid = &head_oid;
555+
if ((!oid || !reset_hard) && get_oid("HEAD", &head_oid)) {
556+
ret = error(_("could not determine HEAD revision"));
557+
goto leave_reset_head;
559558
}
560559

560+
if (!oid)
561+
oid = &head_oid;
562+
561563
memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts));
562564
setup_unpack_trees_porcelain(&unpack_tree_opts, action);
563565
unpack_tree_opts.head_idx = 1;
564566
unpack_tree_opts.src_index = the_repository->index;
565567
unpack_tree_opts.dst_index = the_repository->index;
566-
unpack_tree_opts.fn = oneway_merge;
568+
unpack_tree_opts.fn = reset_hard ? oneway_merge : twoway_merge;
567569
unpack_tree_opts.update = 1;
568570
unpack_tree_opts.merge = 1;
569571
if (!detach_head)
@@ -574,12 +576,17 @@ static int reset_head(struct object_id *oid, const char *action,
574576
goto leave_reset_head;
575577
}
576578

577-
if (!fill_tree_descriptor(&desc, oid)) {
579+
if (!reset_hard && !fill_tree_descriptor(&desc[nr++], &head_oid)) {
580+
ret = error(_("failed to find tree of %s"), oid_to_hex(oid));
581+
goto leave_reset_head;
582+
}
583+
584+
if (!fill_tree_descriptor(&desc[nr++], oid)) {
578585
ret = error(_("failed to find tree of %s"), oid_to_hex(oid));
579586
goto leave_reset_head;
580587
}
581588

582-
if (unpack_trees(1, &desc, &unpack_tree_opts)) {
589+
if (unpack_trees(nr, desc, &unpack_tree_opts)) {
583590
ret = -1;
584591
goto leave_reset_head;
585592
}
@@ -627,7 +634,8 @@ static int reset_head(struct object_id *oid, const char *action,
627634
leave_reset_head:
628635
strbuf_release(&msg);
629636
rollback_lock_file(&lock);
630-
free((void *)desc.buffer);
637+
while (nr)
638+
free((void *)desc[--nr].buffer);
631639
return ret;
632640
}
633641

@@ -1005,7 +1013,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
10051013
rerere_clear(&merge_rr);
10061014
string_list_clear(&merge_rr, 1);
10071015

1008-
if (reset_head(NULL, "reset", NULL, 0, NULL, NULL) < 0)
1016+
if (reset_head(NULL, "reset", NULL, RESET_HEAD_HARD,
1017+
NULL, NULL) < 0)
10091018
die(_("could not discard worktree changes"));
10101019
if (read_basic_state(&options))
10111020
exit(1);
@@ -1021,7 +1030,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
10211030
if (read_basic_state(&options))
10221031
exit(1);
10231032
if (reset_head(&options.orig_head, "reset",
1024-
options.head_name, 0, NULL, NULL) < 0)
1033+
options.head_name, RESET_HEAD_HARD,
1034+
NULL, NULL) < 0)
10251035
die(_("could not move back to %s"),
10261036
oid_to_hex(&options.orig_head));
10271037
ret = finish_rebase(&options);
@@ -1385,7 +1395,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
13851395
write_file(autostash, "%s", oid_to_hex(&oid));
13861396
printf(_("Created autostash: %s\n"), buf.buf);
13871397
if (reset_head(&head->object.oid, "reset --hard",
1388-
NULL, 0, NULL, NULL) < 0)
1398+
NULL, RESET_HEAD_HARD, NULL, NULL) < 0)
13891399
die(_("could not reset --hard"));
13901400
printf(_("HEAD is now at %s"),
13911401
find_unique_abbrev(&head->object.oid,

0 commit comments

Comments
 (0)