@@ -28,22 +28,25 @@ static const char * const checkout_usage[] = {
28
28
};
29
29
30
30
struct checkout_opts {
31
+ int patch_mode ;
31
32
int quiet ;
32
33
int merge ;
33
34
int force ;
34
35
int force_detach ;
35
36
int writeout_stage ;
36
37
int overwrite_ignore ;
37
38
38
- /* not set by parse_options */
39
- int branch_exists ;
40
-
41
39
const char * new_branch ;
42
40
const char * new_branch_force ;
43
41
const char * new_orphan_branch ;
44
42
int new_branch_log ;
45
43
enum branch_track track ;
46
44
struct diff_options diff_options ;
45
+
46
+ int branch_exists ;
47
+ const char * prefix ;
48
+ const char * * pathspec ;
49
+ struct tree * source_tree ;
47
50
};
48
51
49
52
static int post_checkout_hook (struct commit * old , struct commit * new ,
@@ -214,8 +217,7 @@ static int checkout_merged(int pos, struct checkout *state)
214
217
return status ;
215
218
}
216
219
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 )
219
221
{
220
222
int pos ;
221
223
struct checkout state ;
@@ -230,34 +232,34 @@ static int checkout_paths(struct tree *source_tree, const char **pathspec,
230
232
struct lock_file * lock_file = xcalloc (1 , sizeof (struct lock_file ));
231
233
232
234
newfd = hold_locked_index (lock_file , 1 );
233
- if (read_cache_preload (pathspec ) < 0 )
235
+ if (read_cache_preload (opts -> pathspec ) < 0 )
234
236
return error (_ ("corrupt index file" ));
235
237
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 );
238
240
239
- for (pos = 0 ; pathspec [pos ]; pos ++ )
241
+ for (pos = 0 ; opts -> pathspec [pos ]; pos ++ )
240
242
;
241
243
ps_matched = xcalloc (1 , pos );
242
244
243
245
for (pos = 0 ; pos < active_nr ; pos ++ ) {
244
246
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 ))
246
248
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 );
248
250
}
249
251
250
- if (report_path_error (ps_matched , pathspec , prefix ))
252
+ if (report_path_error (ps_matched , opts -> pathspec , opts -> prefix ))
251
253
return 1 ;
252
254
253
255
/* "checkout -m path" to recreate conflicted state */
254
256
if (opts -> merge )
255
- unmerge_cache (pathspec );
257
+ unmerge_cache (opts -> pathspec );
256
258
257
259
/* Any unmerged paths? */
258
260
for (pos = 0 ; pos < active_nr ; pos ++ ) {
259
261
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 )) {
261
263
if (!ce_stage (ce ))
262
264
continue ;
263
265
if (opts -> force ) {
@@ -282,9 +284,9 @@ static int checkout_paths(struct tree *source_tree, const char **pathspec,
282
284
state .refresh_cache = 1 ;
283
285
for (pos = 0 ; pos < active_nr ; pos ++ ) {
284
286
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 ))
286
288
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 )) {
288
290
if (!ce_stage (ce )) {
289
291
errs |= checkout_entry (ce , & state , NULL );
290
292
continue ;
@@ -706,7 +708,8 @@ static void orphaned_commit_warning(struct commit *old, struct commit *new)
706
708
free (refs .objects );
707
709
}
708
710
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 )
710
713
{
711
714
int ret = 0 ;
712
715
struct branch_info old ;
@@ -760,8 +763,7 @@ static int git_checkout_config(const char *var, const char *value, void *cb)
760
763
return git_xmerge_config (var , value , NULL );
761
764
}
762
765
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 )
765
767
{
766
768
return run_add_interactive (revision , "--patch=checkout" , pathspec );
767
769
}
@@ -931,11 +933,8 @@ static int switch_unborn_to_new_branch(const struct checkout_opts *opts)
931
933
int cmd_checkout (int argc , const char * * argv , const char * prefix )
932
934
{
933
935
struct checkout_opts opts ;
934
- unsigned char rev [20 ];
935
936
struct branch_info new ;
936
- struct tree * source_tree = NULL ;
937
937
char * conflict_style = NULL ;
938
- int patch_mode = 0 ;
939
938
int dwim_new_local_branch = 1 ;
940
939
struct option options [] = {
941
940
OPT__QUIET (& opts .quiet , "suppress progress reporting" ),
@@ -957,7 +956,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
957
956
OPT_BOOLEAN (0 , "overwrite-ignore" , & opts .overwrite_ignore , "update ignored files (default)" ),
958
957
OPT_STRING (0 , "conflict" , & conflict_style , "style" ,
959
958
"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" ),
961
960
{ OPTION_BOOLEAN , 0 , "guess" , & dwim_new_local_branch , NULL ,
962
961
"second guess 'git checkout no-such-branch'" ,
963
962
PARSE_OPT_NOARG | PARSE_OPT_HIDDEN },
@@ -967,6 +966,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
967
966
memset (& opts , 0 , sizeof (opts ));
968
967
memset (& new , 0 , sizeof (new ));
969
968
opts .overwrite_ignore = 1 ;
969
+ opts .prefix = prefix ;
970
970
971
971
gitmodules_config ();
972
972
git_config (git_checkout_config , & opts );
@@ -984,7 +984,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
984
984
if (opts .new_branch_force )
985
985
opts .new_branch = opts .new_branch_force ;
986
986
987
- if (patch_mode && (opts .track > 0 || opts .new_branch
987
+ if (opts . patch_mode && (opts .track > 0 || opts .new_branch
988
988
|| opts .new_branch_log || opts .merge || opts .force
989
989
|| opts .force_detach ))
990
990
die (_ ("--patch is incompatible with all other options" ));
@@ -1039,13 +1039,15 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
1039
1039
* remote branches, erroring out for invalid or ambiguous cases.
1040
1040
*/
1041
1041
if (argc ) {
1042
+ unsigned char rev [20 ];
1042
1043
int dwim_ok =
1043
- !patch_mode &&
1044
+ !opts . patch_mode &&
1044
1045
dwim_new_local_branch &&
1045
1046
opts .track == BRANCH_TRACK_UNSPECIFIED &&
1046
1047
!opts .new_branch ;
1047
1048
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 );
1049
1051
argv += n ;
1050
1052
argc -= n ;
1051
1053
}
@@ -1054,13 +1056,13 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
1054
1056
opts .track = git_branch_track ;
1055
1057
1056
1058
if (argc ) {
1057
- const char * * pathspec = get_pathspec (prefix , argv );
1059
+ opts . pathspec = get_pathspec (prefix , argv );
1058
1060
1059
- if (!pathspec )
1061
+ if (!opts . pathspec )
1060
1062
die (_ ("invalid path specification" ));
1061
1063
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 );
1064
1066
1065
1067
/* Checkout paths */
1066
1068
if (opts .new_branch ) {
@@ -1077,11 +1079,11 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
1077
1079
if (1 < !!opts .writeout_stage + !!opts .force + !!opts .merge )
1078
1080
die (_ ("git checkout: --ours/--theirs, --force and --merge are incompatible when\nchecking out of the index." ));
1079
1081
1080
- return checkout_paths (source_tree , pathspec , prefix , & opts );
1082
+ return checkout_paths (& opts );
1081
1083
}
1082
1084
1083
- if (patch_mode )
1084
- return interactive_checkout (new .name , NULL , & opts );
1085
+ if (opts . patch_mode )
1086
+ return interactive_checkout (new .name , NULL );
1085
1087
1086
1088
if (opts .new_branch ) {
1087
1089
struct strbuf buf = STRBUF_INIT ;
0 commit comments