Skip to content

Commit 65f099b

Browse files
pcloudsgitster
authored andcommitted
switch: no worktree status unless real branch switch happens
When we switch from one branch to another, it makes sense to show a summary of local changes since there could be conflicts, or some files left modified.... When switch is used solely for creating a new branch (and "switch" to the same commit) or detaching, we don't really need to show anything. "git checkout" does it anyway for historical reasons. But we can start with a clean slate with switch and don't have to. This essentially reverts fa655d8 (checkout: optimize "git checkout -b <new_branch>" - 2018-08-16) and make it default for switch, but also for -B and --detach. Users of big repos are encouraged to move to switch. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ccb111b commit 65f099b

File tree

3 files changed

+8
-148
lines changed

3 files changed

+8
-148
lines changed

Documentation/config/checkout.txt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,3 @@ will checkout the '<something>' branch on another remote,
1616
and by linkgit:git-worktree[1] when 'git worktree add' refers to a
1717
remote branch. This setting might be used for other checkout-like
1818
commands or functionality in the future.
19-
20-
checkout.optimizeNewBranch::
21-
Optimizes the performance of "git checkout -b <new_branch>" when
22-
using sparse-checkout. When set to true, git will not update the
23-
repo based on the current sparse-checkout settings. This means it
24-
will not update the skip-worktree bit in the index nor add/remove
25-
files in the working directory to reflect the current sparse checkout
26-
settings nor will it show the local changes.

builtin/checkout.c

Lines changed: 8 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
#include "unpack-trees.h"
2727
#include "xdiff-interface.h"
2828

29-
static int checkout_optimize_new_branch;
30-
3129
static const char * const checkout_usage[] = {
3230
N_("git checkout [<options>] <branch>"),
3331
N_("git checkout [<options>] [<branch>] -- <file>..."),
@@ -57,11 +55,7 @@ struct checkout_opts {
5755
int discard_changes;
5856
int accept_pathspec;
5957
int switch_branch_doing_nothing_is_ok;
60-
61-
/*
62-
* If new checkout options are added, skip_merge_working_tree
63-
* should be updated accordingly.
64-
*/
58+
int only_merge_on_switching_branches;
6559

6660
const char *new_branch;
6761
const char *new_branch_force;
@@ -565,112 +559,6 @@ static void setup_branch_path(struct branch_info *branch)
565559
branch->path = strbuf_detach(&buf, NULL);
566560
}
567561

568-
/*
569-
* Skip merging the trees, updating the index and working directory if and
570-
* only if we are creating a new branch via "git checkout -b <new_branch>."
571-
*/
572-
static int skip_merge_working_tree(const struct checkout_opts *opts,
573-
const struct branch_info *old_branch_info,
574-
const struct branch_info *new_branch_info)
575-
{
576-
/*
577-
* Do the merge if sparse checkout is on and the user has not opted in
578-
* to the optimized behavior
579-
*/
580-
if (core_apply_sparse_checkout && !checkout_optimize_new_branch)
581-
return 0;
582-
583-
/*
584-
* We must do the merge if we are actually moving to a new commit.
585-
*/
586-
if (!old_branch_info->commit || !new_branch_info->commit ||
587-
!oideq(&old_branch_info->commit->object.oid,
588-
&new_branch_info->commit->object.oid))
589-
return 0;
590-
591-
/*
592-
* opts->patch_mode cannot be used with switching branches so is
593-
* not tested here
594-
*/
595-
596-
/*
597-
* opts->quiet only impacts output so doesn't require a merge
598-
*/
599-
600-
/*
601-
* Honor the explicit request for a three-way merge or to throw away
602-
* local changes
603-
*/
604-
if (opts->merge || opts->force)
605-
return 0;
606-
607-
/*
608-
* --detach is documented as "updating the index and the files in the
609-
* working tree" but this optimization skips those steps so fall through
610-
* to the regular code path.
611-
*/
612-
if (opts->force_detach)
613-
return 0;
614-
615-
/*
616-
* opts->writeout_stage cannot be used with switching branches so is
617-
* not tested here
618-
*/
619-
620-
/*
621-
* Honor the explicit ignore requests
622-
*/
623-
if (!opts->overwrite_ignore || opts->ignore_skipworktree ||
624-
opts->ignore_other_worktrees)
625-
return 0;
626-
627-
/*
628-
* opts->show_progress only impacts output so doesn't require a merge
629-
*/
630-
631-
/*
632-
* opts->overlay_mode cannot be used with switching branches so is
633-
* not tested here
634-
*/
635-
636-
/*
637-
* If we aren't creating a new branch any changes or updates will
638-
* happen in the existing branch. Since that could only be updating
639-
* the index and working directory, we don't want to skip those steps
640-
* or we've defeated any purpose in running the command.
641-
*/
642-
if (!opts->new_branch)
643-
return 0;
644-
645-
/*
646-
* new_branch_force is defined to "create/reset and checkout a branch"
647-
* so needs to go through the merge to do the reset
648-
*/
649-
if (opts->new_branch_force)
650-
return 0;
651-
652-
/*
653-
* A new orphaned branch requrires the index and the working tree to be
654-
* adjusted to <start_point>
655-
*/
656-
if (opts->new_orphan_branch)
657-
return 0;
658-
659-
/*
660-
* Remaining variables are not checkout options but used to track state
661-
*/
662-
663-
/*
664-
* Do the merge if this is the initial checkout. We cannot use
665-
* is_cache_unborn() here because the index hasn't been loaded yet
666-
* so cache_nr and timestamp.sec are always zero.
667-
*/
668-
if (!file_exists(get_index_file()))
669-
return 0;
670-
671-
return 1;
672-
}
673-
674562
static int merge_working_tree(const struct checkout_opts *opts,
675563
struct branch_info *old_branch_info,
676564
struct branch_info *new_branch_info,
@@ -1028,6 +916,7 @@ static int switch_branches(const struct checkout_opts *opts,
1028916
void *path_to_free;
1029917
struct object_id rev;
1030918
int flag, writeout_error = 0;
919+
int do_merge = 1;
1031920

1032921
trace2_cmd_mode("branch");
1033922

@@ -1047,16 +936,12 @@ static int switch_branches(const struct checkout_opts *opts,
1047936
if (!new_branch_info->commit)
1048937
die(_("You are on a branch yet to be born"));
1049938
parse_commit_or_die(new_branch_info->commit);
939+
940+
if (opts->only_merge_on_switching_branches)
941+
do_merge = 0;
1050942
}
1051943

1052-
/* optimize the "checkout -b <new_branch> path */
1053-
if (skip_merge_working_tree(opts, &old_branch_info, new_branch_info)) {
1054-
if (!checkout_optimize_new_branch && !opts->quiet) {
1055-
if (read_cache_preload(NULL) < 0)
1056-
return error(_("index file corrupt"));
1057-
show_local_changes(&new_branch_info->commit->object, &opts->diff_options);
1058-
}
1059-
} else {
944+
if (do_merge) {
1060945
ret = merge_working_tree(opts, &old_branch_info, new_branch_info, &writeout_error);
1061946
if (ret) {
1062947
free(path_to_free);
@@ -1076,11 +961,6 @@ static int switch_branches(const struct checkout_opts *opts,
1076961

1077962
static int git_checkout_config(const char *var, const char *value, void *cb)
1078963
{
1079-
if (!strcmp(var, "checkout.optimizenewbranch")) {
1080-
checkout_optimize_new_branch = git_config_bool(var, value);
1081-
return 0;
1082-
}
1083-
1084964
if (!strcmp(var, "diff.ignoresubmodules")) {
1085965
struct checkout_opts *opts = cb;
1086966
handle_ignore_submodules_arg(&opts->diff_options, value);
@@ -1631,6 +1511,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
16311511
memset(&opts, 0, sizeof(opts));
16321512
opts.dwim_new_local_branch = 1;
16331513
opts.switch_branch_doing_nothing_is_ok = 1;
1514+
opts.only_merge_on_switching_branches = 0;
16341515
opts.accept_pathspec = 1;
16351516
opts.implicit_detach = 1;
16361517

@@ -1666,6 +1547,7 @@ int cmd_switch(int argc, const char **argv, const char *prefix)
16661547
opts.dwim_new_local_branch = 1;
16671548
opts.accept_pathspec = 0;
16681549
opts.switch_branch_doing_nothing_is_ok = 0;
1550+
opts.only_merge_on_switching_branches = 1;
16691551
opts.implicit_detach = 0;
16701552

16711553
options = parse_options_dup(switch_options);

t/t1090-sparse-checkout-scope.sh

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,6 @@ test_expect_success 'perform sparse checkout of master' '
3131
test_path_is_file c
3232
'
3333

34-
test_expect_success 'checkout -b checkout.optimizeNewBranch interaction' '
35-
cp .git/info/sparse-checkout .git/info/sparse-checkout.bak &&
36-
test_when_finished "
37-
mv -f .git/info/sparse-checkout.bak .git/info/sparse-checkout
38-
git checkout master
39-
" &&
40-
echo "/b" >>.git/info/sparse-checkout &&
41-
test "$(git ls-files -t b)" = "S b" &&
42-
git -c checkout.optimizeNewBranch=true checkout -b fast &&
43-
test "$(git ls-files -t b)" = "S b" &&
44-
git checkout -b slow &&
45-
test "$(git ls-files -t b)" = "H b"
46-
'
47-
4834
test_expect_success 'merge feature branch into sparse checkout of master' '
4935
git merge feature &&
5036
test_path_is_file a &&

0 commit comments

Comments
 (0)