26
26
#include "unpack-trees.h"
27
27
#include "xdiff-interface.h"
28
28
29
- static int checkout_optimize_new_branch ;
30
-
31
29
static const char * const checkout_usage [] = {
32
30
N_ ("git checkout [<options>] <branch>" ),
33
31
N_ ("git checkout [<options>] [<branch>] -- <file>..." ),
@@ -57,11 +55,7 @@ struct checkout_opts {
57
55
int discard_changes ;
58
56
int accept_pathspec ;
59
57
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 ;
65
59
66
60
const char * new_branch ;
67
61
const char * new_branch_force ;
@@ -565,112 +559,6 @@ static void setup_branch_path(struct branch_info *branch)
565
559
branch -> path = strbuf_detach (& buf , NULL );
566
560
}
567
561
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
-
674
562
static int merge_working_tree (const struct checkout_opts * opts ,
675
563
struct branch_info * old_branch_info ,
676
564
struct branch_info * new_branch_info ,
@@ -1028,6 +916,7 @@ static int switch_branches(const struct checkout_opts *opts,
1028
916
void * path_to_free ;
1029
917
struct object_id rev ;
1030
918
int flag , writeout_error = 0 ;
919
+ int do_merge = 1 ;
1031
920
1032
921
trace2_cmd_mode ("branch" );
1033
922
@@ -1047,16 +936,12 @@ static int switch_branches(const struct checkout_opts *opts,
1047
936
if (!new_branch_info -> commit )
1048
937
die (_ ("You are on a branch yet to be born" ));
1049
938
parse_commit_or_die (new_branch_info -> commit );
939
+
940
+ if (opts -> only_merge_on_switching_branches )
941
+ do_merge = 0 ;
1050
942
}
1051
943
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 ) {
1060
945
ret = merge_working_tree (opts , & old_branch_info , new_branch_info , & writeout_error );
1061
946
if (ret ) {
1062
947
free (path_to_free );
@@ -1076,11 +961,6 @@ static int switch_branches(const struct checkout_opts *opts,
1076
961
1077
962
static int git_checkout_config (const char * var , const char * value , void * cb )
1078
963
{
1079
- if (!strcmp (var , "checkout.optimizenewbranch" )) {
1080
- checkout_optimize_new_branch = git_config_bool (var , value );
1081
- return 0 ;
1082
- }
1083
-
1084
964
if (!strcmp (var , "diff.ignoresubmodules" )) {
1085
965
struct checkout_opts * opts = cb ;
1086
966
handle_ignore_submodules_arg (& opts -> diff_options , value );
@@ -1631,6 +1511,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
1631
1511
memset (& opts , 0 , sizeof (opts ));
1632
1512
opts .dwim_new_local_branch = 1 ;
1633
1513
opts .switch_branch_doing_nothing_is_ok = 1 ;
1514
+ opts .only_merge_on_switching_branches = 0 ;
1634
1515
opts .accept_pathspec = 1 ;
1635
1516
opts .implicit_detach = 1 ;
1636
1517
@@ -1666,6 +1547,7 @@ int cmd_switch(int argc, const char **argv, const char *prefix)
1666
1547
opts .dwim_new_local_branch = 1 ;
1667
1548
opts .accept_pathspec = 0 ;
1668
1549
opts .switch_branch_doing_nothing_is_ok = 0 ;
1550
+ opts .only_merge_on_switching_branches = 1 ;
1669
1551
opts .implicit_detach = 0 ;
1670
1552
1671
1553
options = parse_options_dup (switch_options );
0 commit comments