19
19
#include "ll-merge.h"
20
20
#include "resolve-undo.h"
21
21
#include "submodule.h"
22
- #include "argv-array.h"
23
- #include "sigchain.h"
24
22
25
23
static const char * const checkout_usage [] = {
26
24
N_ ("git checkout [options] <branch>" ),
@@ -51,8 +49,6 @@ struct checkout_opts {
51
49
struct pathspec pathspec ;
52
50
struct tree * source_tree ;
53
51
54
- const char * new_worktree ;
55
- const char * * saved_argv ;
56
52
int new_worktree_mode ;
57
53
};
58
54
@@ -255,9 +251,6 @@ static int checkout_paths(const struct checkout_opts *opts,
255
251
die (_ ("Cannot update paths and switch to branch '%s' at the same time." ),
256
252
opts -> new_branch );
257
253
258
- if (opts -> new_worktree )
259
- die (_ ("'%s' cannot be used with updating paths" ), "--to" );
260
-
261
254
if (opts -> patch_mode )
262
255
return run_add_interactive (revision , "--patch=checkout" ,
263
256
& opts -> pathspec );
@@ -825,142 +818,6 @@ static int switch_branches(const struct checkout_opts *opts,
825
818
return ret || writeout_error ;
826
819
}
827
820
828
- static char * junk_work_tree ;
829
- static char * junk_git_dir ;
830
- static int is_junk ;
831
- static pid_t junk_pid ;
832
-
833
- static void remove_junk (void )
834
- {
835
- struct strbuf sb = STRBUF_INIT ;
836
- if (!is_junk || getpid () != junk_pid )
837
- return ;
838
- if (junk_git_dir ) {
839
- strbuf_addstr (& sb , junk_git_dir );
840
- remove_dir_recursively (& sb , 0 );
841
- strbuf_reset (& sb );
842
- }
843
- if (junk_work_tree ) {
844
- strbuf_addstr (& sb , junk_work_tree );
845
- remove_dir_recursively (& sb , 0 );
846
- }
847
- strbuf_release (& sb );
848
- }
849
-
850
- static void remove_junk_on_signal (int signo )
851
- {
852
- remove_junk ();
853
- sigchain_pop (signo );
854
- raise (signo );
855
- }
856
-
857
- static int prepare_linked_checkout (const char * path , const char * * child_argv )
858
- {
859
- struct strbuf sb_git = STRBUF_INIT , sb_repo = STRBUF_INIT ;
860
- struct strbuf sb = STRBUF_INIT ;
861
- const char * name ;
862
- struct stat st ;
863
- struct child_process cp ;
864
- int counter = 0 , len , ret ;
865
- unsigned char rev [20 ];
866
-
867
- if (file_exists (path ) && !is_empty_dir (path ))
868
- die (_ ("'%s' already exists" ), path );
869
-
870
- len = strlen (path );
871
- while (len && is_dir_sep (path [len - 1 ]))
872
- len -- ;
873
-
874
- for (name = path + len - 1 ; name > path ; name -- )
875
- if (is_dir_sep (* name )) {
876
- name ++ ;
877
- break ;
878
- }
879
- strbuf_addstr (& sb_repo ,
880
- git_path ("worktrees/%.*s" , (int )(path + len - name ), name ));
881
- len = sb_repo .len ;
882
- if (safe_create_leading_directories_const (sb_repo .buf ))
883
- die_errno (_ ("could not create leading directories of '%s'" ),
884
- sb_repo .buf );
885
- while (!stat (sb_repo .buf , & st )) {
886
- counter ++ ;
887
- strbuf_setlen (& sb_repo , len );
888
- strbuf_addf (& sb_repo , "%d" , counter );
889
- }
890
- name = strrchr (sb_repo .buf , '/' ) + 1 ;
891
-
892
- junk_pid = getpid ();
893
- atexit (remove_junk );
894
- sigchain_push_common (remove_junk_on_signal );
895
-
896
- if (mkdir (sb_repo .buf , 0777 ))
897
- die_errno (_ ("could not create directory of '%s'" ), sb_repo .buf );
898
- junk_git_dir = xstrdup (sb_repo .buf );
899
- is_junk = 1 ;
900
-
901
- /*
902
- * lock the incomplete repo so prune won't delete it, unlock
903
- * after the preparation is over.
904
- */
905
- strbuf_addf (& sb , "%s/locked" , sb_repo .buf );
906
- write_file (sb .buf , 1 , "initializing\n" );
907
-
908
- strbuf_addf (& sb_git , "%s/.git" , path );
909
- if (safe_create_leading_directories_const (sb_git .buf ))
910
- die_errno (_ ("could not create leading directories of '%s'" ),
911
- sb_git .buf );
912
- junk_work_tree = xstrdup (path );
913
-
914
- strbuf_reset (& sb );
915
- strbuf_addf (& sb , "%s/gitdir" , sb_repo .buf );
916
- write_file (sb .buf , 1 , "%s\n" , real_path (sb_git .buf ));
917
- write_file (sb_git .buf , 1 , "gitdir: %s/worktrees/%s\n" ,
918
- real_path (get_git_common_dir ()), name );
919
- /*
920
- * This is to keep resolve_ref() happy. We need a valid HEAD
921
- * or is_git_directory() will reject the directory. Moreover, HEAD
922
- * in the new worktree must resolve to the same value as HEAD in
923
- * the current tree since the command invoked to populate the new
924
- * worktree will be handed the branch/ref specified by the user.
925
- * For instance, if the user asks for the new worktree to be based
926
- * at HEAD~5, then the resolved HEAD~5 in the new worktree must
927
- * match the resolved HEAD~5 in the current tree in order to match
928
- * the user's expectation.
929
- */
930
- if (!resolve_ref_unsafe ("HEAD" , 0 , rev , NULL ))
931
- die (_ ("unable to resolve HEAD" ));
932
- strbuf_reset (& sb );
933
- strbuf_addf (& sb , "%s/HEAD" , sb_repo .buf );
934
- write_file (sb .buf , 1 , "%s\n" , sha1_to_hex (rev ));
935
- strbuf_reset (& sb );
936
- strbuf_addf (& sb , "%s/commondir" , sb_repo .buf );
937
- write_file (sb .buf , 1 , "../..\n" );
938
-
939
- fprintf_ln (stderr , _ ("Enter %s (identifier %s)" ), path , name );
940
-
941
- setenv ("GIT_CHECKOUT_NEW_WORKTREE" , "1" , 1 );
942
- setenv (GIT_DIR_ENVIRONMENT , sb_git .buf , 1 );
943
- setenv (GIT_WORK_TREE_ENVIRONMENT , path , 1 );
944
- memset (& cp , 0 , sizeof (cp ));
945
- cp .git_cmd = 1 ;
946
- cp .argv = child_argv ;
947
- ret = run_command (& cp );
948
- if (!ret ) {
949
- is_junk = 0 ;
950
- free (junk_work_tree );
951
- free (junk_git_dir );
952
- junk_work_tree = NULL ;
953
- junk_git_dir = NULL ;
954
- }
955
- strbuf_reset (& sb );
956
- strbuf_addf (& sb , "%s/locked" , sb_repo .buf );
957
- unlink_or_warn (sb .buf );
958
- strbuf_release (& sb );
959
- strbuf_release (& sb_repo );
960
- strbuf_release (& sb_git );
961
- return ret ;
962
- }
963
-
964
821
static int git_checkout_config (const char * var , const char * value , void * cb )
965
822
{
966
823
if (!strcmp (var , "diff.ignoresubmodules" )) {
@@ -1299,13 +1156,6 @@ static int checkout_branch(struct checkout_opts *opts,
1299
1156
free (head_ref );
1300
1157
}
1301
1158
1302
- if (opts -> new_worktree ) {
1303
- if (!new -> commit )
1304
- die (_ ("no branch specified" ));
1305
- return prepare_linked_checkout (opts -> new_worktree ,
1306
- opts -> saved_argv );
1307
- }
1308
-
1309
1159
if (!new -> commit && opts -> new_branch ) {
1310
1160
unsigned char rev [20 ];
1311
1161
int flag ;
@@ -1348,8 +1198,6 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
1348
1198
N_ ("do not limit pathspecs to sparse entries only" )),
1349
1199
OPT_HIDDEN_BOOL (0 , "guess" , & dwim_new_local_branch ,
1350
1200
N_ ("second guess 'git checkout no-such-branch'" )),
1351
- OPT_FILENAME (0 , "to" , & opts .new_worktree ,
1352
- N_ ("check a branch out in a separate working directory" )),
1353
1201
OPT_BOOL (0 , "ignore-other-worktrees" , & opts .ignore_other_worktrees ,
1354
1202
N_ ("do not check if another worktree is holding the given ref" )),
1355
1203
OPT_END (),
@@ -1360,9 +1208,6 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
1360
1208
opts .overwrite_ignore = 1 ;
1361
1209
opts .prefix = prefix ;
1362
1210
1363
- opts .saved_argv = xmalloc (sizeof (const char * ) * (argc + 2 ));
1364
- memcpy (opts .saved_argv , argv , sizeof (const char * ) * (argc + 1 ));
1365
-
1366
1211
gitmodules_config ();
1367
1212
git_config (git_checkout_config , & opts );
1368
1213
@@ -1371,13 +1216,9 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
1371
1216
argc = parse_options (argc , argv , prefix , options , checkout_usage ,
1372
1217
PARSE_OPT_KEEP_DASHDASH );
1373
1218
1374
- /* recursive execution from checkout_new_worktree() */
1375
1219
opts .new_worktree_mode = getenv ("GIT_CHECKOUT_NEW_WORKTREE" ) != NULL ;
1376
- if (opts .new_worktree_mode )
1377
- opts .new_worktree = NULL ;
1378
1220
1379
- if (!opts .new_worktree )
1380
- setup_work_tree ();
1221
+ setup_work_tree ();
1381
1222
1382
1223
if (conflict_style ) {
1383
1224
opts .merge = 1 ; /* implied */
0 commit comments