@@ -28,22 +28,25 @@ static const char * const checkout_usage[] = {
2828};
2929
3030struct checkout_opts {
31+ int patch_mode ;
3132 int quiet ;
3233 int merge ;
3334 int force ;
3435 int force_detach ;
3536 int writeout_stage ;
3637 int overwrite_ignore ;
3738
38- /* not set by parse_options */
39- int branch_exists ;
40-
4139 const char * new_branch ;
4240 const char * new_branch_force ;
4341 const char * new_orphan_branch ;
4442 int new_branch_log ;
4543 enum branch_track track ;
4644 struct diff_options diff_options ;
45+
46+ int branch_exists ;
47+ const char * prefix ;
48+ const char * * pathspec ;
49+ struct tree * source_tree ;
4750};
4851
4952static int post_checkout_hook (struct commit * old , struct commit * new ,
@@ -214,8 +217,7 @@ static int checkout_merged(int pos, struct checkout *state)
214217 return status ;
215218}
216219
217- static int checkout_paths (struct tree * source_tree , const char * * pathspec ,
218- const char * prefix , const struct checkout_opts * opts )
220+ static int checkout_paths (const struct checkout_opts * opts )
219221{
220222 int pos ;
221223 struct checkout state ;
@@ -230,34 +232,34 @@ static int checkout_paths(struct tree *source_tree, const char **pathspec,
230232 struct lock_file * lock_file = xcalloc (1 , sizeof (struct lock_file ));
231233
232234 newfd = hold_locked_index (lock_file , 1 );
233- if (read_cache_preload (pathspec ) < 0 )
235+ if (read_cache_preload (opts -> pathspec ) < 0 )
234236 return error (_ ("corrupt index file" ));
235237
236- if (source_tree )
237- read_tree_some (source_tree , pathspec );
238+ if (opts -> source_tree )
239+ read_tree_some (opts -> source_tree , opts -> pathspec );
238240
239- for (pos = 0 ; pathspec [pos ]; pos ++ )
241+ for (pos = 0 ; opts -> pathspec [pos ]; pos ++ )
240242 ;
241243 ps_matched = xcalloc (1 , pos );
242244
243245 for (pos = 0 ; pos < active_nr ; pos ++ ) {
244246 struct cache_entry * ce = active_cache [pos ];
245- if (source_tree && !(ce -> ce_flags & CE_UPDATE ))
247+ if (opts -> source_tree && !(ce -> ce_flags & CE_UPDATE ))
246248 continue ;
247- match_pathspec (pathspec , ce -> name , ce_namelen (ce ), 0 , ps_matched );
249+ match_pathspec (opts -> pathspec , ce -> name , ce_namelen (ce ), 0 , ps_matched );
248250 }
249251
250- if (report_path_error (ps_matched , pathspec , prefix ))
252+ if (report_path_error (ps_matched , opts -> pathspec , opts -> prefix ))
251253 return 1 ;
252254
253255 /* "checkout -m path" to recreate conflicted state */
254256 if (opts -> merge )
255- unmerge_cache (pathspec );
257+ unmerge_cache (opts -> pathspec );
256258
257259 /* Any unmerged paths? */
258260 for (pos = 0 ; pos < active_nr ; pos ++ ) {
259261 struct cache_entry * ce = active_cache [pos ];
260- if (match_pathspec (pathspec , ce -> name , ce_namelen (ce ), 0 , NULL )) {
262+ if (match_pathspec (opts -> pathspec , ce -> name , ce_namelen (ce ), 0 , NULL )) {
261263 if (!ce_stage (ce ))
262264 continue ;
263265 if (opts -> force ) {
@@ -282,9 +284,9 @@ static int checkout_paths(struct tree *source_tree, const char **pathspec,
282284 state .refresh_cache = 1 ;
283285 for (pos = 0 ; pos < active_nr ; pos ++ ) {
284286 struct cache_entry * ce = active_cache [pos ];
285- if (source_tree && !(ce -> ce_flags & CE_UPDATE ))
287+ if (opts -> source_tree && !(ce -> ce_flags & CE_UPDATE ))
286288 continue ;
287- if (match_pathspec (pathspec , ce -> name , ce_namelen (ce ), 0 , NULL )) {
289+ if (match_pathspec (opts -> pathspec , ce -> name , ce_namelen (ce ), 0 , NULL )) {
288290 if (!ce_stage (ce )) {
289291 errs |= checkout_entry (ce , & state , NULL );
290292 continue ;
@@ -706,7 +708,8 @@ static void orphaned_commit_warning(struct commit *old, struct commit *new)
706708 free (refs .objects );
707709}
708710
709- static int switch_branches (const struct checkout_opts * opts , struct branch_info * new )
711+ static int switch_branches (const struct checkout_opts * opts ,
712+ struct branch_info * new )
710713{
711714 int ret = 0 ;
712715 struct branch_info old ;
@@ -760,8 +763,7 @@ static int git_checkout_config(const char *var, const char *value, void *cb)
760763 return git_xmerge_config (var , value , NULL );
761764}
762765
763- static int interactive_checkout (const char * revision , const char * * pathspec ,
764- struct checkout_opts * opts )
766+ static int interactive_checkout (const char * revision , const char * * pathspec )
765767{
766768 return run_add_interactive (revision , "--patch=checkout" , pathspec );
767769}
@@ -931,11 +933,8 @@ static int switch_unborn_to_new_branch(const struct checkout_opts *opts)
931933int cmd_checkout (int argc , const char * * argv , const char * prefix )
932934{
933935 struct checkout_opts opts ;
934- unsigned char rev [20 ];
935936 struct branch_info new ;
936- struct tree * source_tree = NULL ;
937937 char * conflict_style = NULL ;
938- int patch_mode = 0 ;
939938 int dwim_new_local_branch = 1 ;
940939 struct option options [] = {
941940 OPT__QUIET (& opts .quiet , "suppress progress reporting" ),
@@ -957,7 +956,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
957956 OPT_BOOLEAN (0 , "overwrite-ignore" , & opts .overwrite_ignore , "update ignored files (default)" ),
958957 OPT_STRING (0 , "conflict" , & conflict_style , "style" ,
959958 "conflict style (merge or diff3)" ),
960- OPT_BOOLEAN ('p' , "patch" , & patch_mode , "select hunks interactively" ),
959+ OPT_BOOLEAN ('p' , "patch" , & opts . patch_mode , "select hunks interactively" ),
961960 { OPTION_BOOLEAN , 0 , "guess" , & dwim_new_local_branch , NULL ,
962961 "second guess 'git checkout no-such-branch'" ,
963962 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN },
@@ -967,6 +966,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
967966 memset (& opts , 0 , sizeof (opts ));
968967 memset (& new , 0 , sizeof (new ));
969968 opts .overwrite_ignore = 1 ;
969+ opts .prefix = prefix ;
970970
971971 gitmodules_config ();
972972 git_config (git_checkout_config , & opts );
@@ -984,7 +984,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
984984 if (opts .new_branch_force )
985985 opts .new_branch = opts .new_branch_force ;
986986
987- if (patch_mode && (opts .track > 0 || opts .new_branch
987+ if (opts . patch_mode && (opts .track > 0 || opts .new_branch
988988 || opts .new_branch_log || opts .merge || opts .force
989989 || opts .force_detach ))
990990 die (_ ("--patch is incompatible with all other options" ));
@@ -1039,13 +1039,15 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
10391039 * remote branches, erroring out for invalid or ambiguous cases.
10401040 */
10411041 if (argc ) {
1042+ unsigned char rev [20 ];
10421043 int dwim_ok =
1043- !patch_mode &&
1044+ !opts . patch_mode &&
10441045 dwim_new_local_branch &&
10451046 opts .track == BRANCH_TRACK_UNSPECIFIED &&
10461047 !opts .new_branch ;
10471048 int n = parse_branchname_arg (argc , argv , dwim_ok ,
1048- & new , & source_tree , rev , & opts .new_branch );
1049+ & new , & opts .source_tree ,
1050+ rev , & opts .new_branch );
10491051 argv += n ;
10501052 argc -= n ;
10511053 }
@@ -1054,13 +1056,13 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
10541056 opts .track = git_branch_track ;
10551057
10561058 if (argc ) {
1057- const char * * pathspec = get_pathspec (prefix , argv );
1059+ opts . pathspec = get_pathspec (prefix , argv );
10581060
1059- if (!pathspec )
1061+ if (!opts . pathspec )
10601062 die (_ ("invalid path specification" ));
10611063
1062- if (patch_mode )
1063- return interactive_checkout (new .name , pathspec , & opts );
1064+ if (opts . patch_mode )
1065+ return interactive_checkout (new .name , opts . pathspec );
10641066
10651067 /* Checkout paths */
10661068 if (opts .new_branch ) {
@@ -1077,11 +1079,11 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
10771079 if (1 < !!opts .writeout_stage + !!opts .force + !!opts .merge )
10781080 die (_ ("git checkout: --ours/--theirs, --force and --merge are incompatible when\nchecking out of the index." ));
10791081
1080- return checkout_paths (source_tree , pathspec , prefix , & opts );
1082+ return checkout_paths (& opts );
10811083 }
10821084
1083- if (patch_mode )
1084- return interactive_checkout (new .name , NULL , & opts );
1085+ if (opts . patch_mode )
1086+ return interactive_checkout (new .name , NULL );
10851087
10861088 if (opts .new_branch ) {
10871089 struct strbuf buf = STRBUF_INIT ;
0 commit comments