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 <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Johannes Schindelin committed
Nov 12, 2018 at 03:44 UTC
bac2a1e36ffd7ae8b62e1ba708b64979c0f10ee8
1 file changed
+25
-15
builtin/rebase.c
+25
-15
@@ -523,14 +523,16 @@ finished_rebase:
523
#define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION"
524
525
#define RESET_HEAD_DETACH (1<<0)
526
+#define RESET_HEAD_HARD (1<<1)
527
528
static int reset_head(struct object_id *oid, const char *action,
529
const char *switch_to_branch, unsigned flags,
530
const char *reflog_orig_head, const char *reflog_head)
531
{
532
unsigned detach_head = flags & RESET_HEAD_DETACH;
533
+ unsigned reset_hard = flags & RESET_HEAD_HARD;
534
struct object_id head_oid;
533
- struct tree_desc desc;
535
+ struct tree_desc desc[2] = { { NULL }, { NULL } };
536
struct lock_file lock = LOCK_INIT;
537
struct unpack_trees_options unpack_tree_opts;
538
struct tree *tree;
@@ -539,7 +541,7 @@ static int reset_head(struct object_id *oid, const char *action,
541
size_t prefix_len;
542
struct object_id *orig = NULL, oid_orig,
543
*old_orig = NULL, oid_old_orig;
542
- int ret = 0;
544
+ int ret = 0, nr = 0;
545
546
if (switch_to_branch && !starts_with(switch_to_branch, "refs/"))
547
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,
551
goto leave_reset_head;
552
}
553
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;
557
}
558
559
+ if (!oid)
560
+ oid = &head_oid;
561
+
562
memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts));
563
setup_unpack_trees_porcelain(&unpack_tree_opts, action);
564
unpack_tree_opts.head_idx = 1;
565
unpack_tree_opts.src_index = the_repository->index;
566
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;
568
unpack_tree_opts.update = 1;
569
unpack_tree_opts.merge = 1;
570
if (!detach_head)
@@ -573,12 +575,17 @@ static int reset_head(struct object_id *oid, const char *action,
575
goto leave_reset_head;
576
}
577
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)) {
584
ret = error(_("failed to find tree of %s"), oid_to_hex(oid));
585
goto leave_reset_head;
586
}
587
581
- if (unpack_trees(1, &desc, &unpack_tree_opts)) {
588
+ if (unpack_trees(nr, desc, &unpack_tree_opts)) {
589
ret = -1;
590
goto leave_reset_head;
591
}
@@ -625,7 +632,8 @@ static int reset_head(struct object_id *oid, const char *action,
632
leave_reset_head:
633
strbuf_release(&msg);
634
rollback_lock_file(&lock);
628
- free((void *)desc.buffer);
635
+ while (nr)
636
+ free((void *)desc[--nr].buffer);
637
return ret;
638
}
639
@@ -1003,7 +1011,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1011
rerere_clear(&merge_rr);
1012
string_list_clear(&merge_rr, 1);
1013
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)
1016
die(_("could not discard worktree changes"));
1017
if (read_basic_state(&options))
1018
exit(1);
@@ -1019,7 +1028,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1028
if (read_basic_state(&options))
1029
exit(1);
1030
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)
1033
die(_("could not move back to %s"),
1034
oid_to_hex(&options.orig_head));
1035
ret = finish_rebase(&options);
@@ -1383,7 +1393,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1393
write_file(autostash, "%s", oid_to_hex(&oid));
1394
printf(_("Created autostash: %s\n"), buf.buf);
1395
if (reset_head(&head->object.oid, "reset --hard",
1386
- NULL, 0, NULL, NULL) < 0)
1396
+ NULL, RESET_HEAD_HARD, NULL, NULL) < 0)
1397
die(_("could not reset --hard"));
1398
printf(_("HEAD is now at %s"),
1399
find_unique_abbrev(&head->object.oid,