Skip to content

Commit bac2a1e

Browse files
dschogitster
authored andcommitted
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]>
1 parent 73d6d7b commit bac2a1e

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
@@ -523,14 +523,16 @@ static int run_specific_rebase(struct rebase_options *opts)
523523
#define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION"
524524

525525
#define RESET_HEAD_DETACH (1<<0)
526+
#define RESET_HEAD_HARD (1<<1)
526527

527528
static int reset_head(struct object_id *oid, const char *action,
528529
const char *switch_to_branch, unsigned flags,
529530
const char *reflog_orig_head, const char *reflog_head)
530531
{
531532
unsigned detach_head = flags & RESET_HEAD_DETACH;
533+
unsigned reset_hard = flags & RESET_HEAD_HARD;
532534
struct object_id head_oid;
533-
struct tree_desc desc;
535+
struct tree_desc desc[2] = { { NULL }, { NULL } };
534536
struct lock_file lock = LOCK_INIT;
535537
struct unpack_trees_options unpack_tree_opts;
536538
struct tree *tree;
@@ -539,7 +541,7 @@ static int reset_head(struct object_id *oid, const char *action,
539541
size_t prefix_len;
540542
struct object_id *orig = NULL, oid_orig,
541543
*old_orig = NULL, oid_old_orig;
542-
int ret = 0;
544+
int ret = 0, nr = 0;
543545

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

552-
if (!oid) {
553-
if (get_oid("HEAD", &head_oid)) {
554-
ret = error(_("could not determine HEAD revision"));
555-
goto leave_reset_head;
556-
}
557-
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;
558557
}
559558

559+
if (!oid)
560+
oid = &head_oid;
561+
560562
memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts));
561563
setup_unpack_trees_porcelain(&unpack_tree_opts, action);
562564
unpack_tree_opts.head_idx = 1;
563565
unpack_tree_opts.src_index = the_repository->index;
564566
unpack_tree_opts.dst_index = the_repository->index;
565-
unpack_tree_opts.fn = oneway_merge;
567+
unpack_tree_opts.fn = reset_hard ? oneway_merge : twoway_merge;
566568
unpack_tree_opts.update = 1;
567569
unpack_tree_opts.merge = 1;
568570
if (!detach_head)
@@ -573,12 +575,17 @@ static int reset_head(struct object_id *oid, const char *action,
573575
goto leave_reset_head;
574576
}
575577

576-
if (!fill_tree_descriptor(&desc, oid)) {
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;
581+
}
582+
583+
if (!fill_tree_descriptor(&desc[nr++], oid)) {
577584
ret = error(_("failed to find tree of %s"), oid_to_hex(oid));
578585
goto leave_reset_head;
579586
}
580587

581-
if (unpack_trees(1, &desc, &unpack_tree_opts)) {
588+
if (unpack_trees(nr, desc, &unpack_tree_opts)) {
582589
ret = -1;
583590
goto leave_reset_head;
584591
}
@@ -625,7 +632,8 @@ static int reset_head(struct object_id *oid, const char *action,
625632
leave_reset_head:
626633
strbuf_release(&msg);
627634
rollback_lock_file(&lock);
628-
free((void *)desc.buffer);
635+
while (nr)
636+
free((void *)desc[--nr].buffer);
629637
return ret;
630638
}
631639

@@ -1003,7 +1011,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
10031011
rerere_clear(&merge_rr);
10041012
string_list_clear(&merge_rr, 1);
10051013

1006-
if (reset_head(NULL, "reset", NULL, 0, NULL, NULL) < 0)
1014+
if (reset_head(NULL, "reset", NULL, RESET_HEAD_HARD,
1015+
NULL, NULL) < 0)
10071016
die(_("could not discard worktree changes"));
10081017
if (read_basic_state(&options))
10091018
exit(1);
@@ -1019,7 +1028,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
10191028
if (read_basic_state(&options))
10201029
exit(1);
10211030
if (reset_head(&options.orig_head, "reset",
1022-
options.head_name, 0, NULL, NULL) < 0)
1031+
options.head_name, RESET_HEAD_HARD,
1032+
NULL, NULL) < 0)
10231033
die(_("could not move back to %s"),
10241034
oid_to_hex(&options.orig_head));
10251035
ret = finish_rebase(&options);
@@ -1383,7 +1393,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
13831393
write_file(autostash, "%s", oid_to_hex(&oid));
13841394
printf(_("Created autostash: %s\n"), buf.buf);
13851395
if (reset_head(&head->object.oid, "reset --hard",
1386-
NULL, 0, NULL, NULL) < 0)
1396+
NULL, RESET_HEAD_HARD, NULL, NULL) < 0)
13871397
die(_("could not reset --hard"));
13881398
printf(_("HEAD is now at %s"),
13891399
find_unique_abbrev(&head->object.oid,

0 commit comments

Comments
 (0)