@@ -12,6 +12,13 @@ static const char * const worktree_usage[] = {
12
12
NULL
13
13
};
14
14
15
+ struct add_opts {
16
+ int force ;
17
+ int detach ;
18
+ const char * new_branch ;
19
+ int force_new_branch ;
20
+ };
21
+
15
22
static int show_only ;
16
23
static int verbose ;
17
24
static unsigned long expire ;
@@ -171,7 +178,8 @@ static const char *worktree_basename(const char *path, int *olen)
171
178
return name ;
172
179
}
173
180
174
- static int add_worktree (const char * path , const char * * child_argv )
181
+ static int add_worktree (const char * path , const char * * child_argv ,
182
+ const struct add_opts * opts )
175
183
{
176
184
struct strbuf sb_git = STRBUF_INIT , sb_repo = STRBUF_INIT ;
177
185
struct strbuf sb = STRBUF_INIT ;
@@ -272,50 +280,51 @@ static int add_worktree(const char *path, const char **child_argv)
272
280
273
281
static int add (int ac , const char * * av , const char * prefix )
274
282
{
275
- int force = 0 , detach = 0 , force_new_branch ;
276
- const char * new_branch = NULL , * new_branch_force = NULL ;
283
+ struct add_opts opts ;
284
+ const char * new_branch_force = NULL ;
277
285
const char * path , * branch ;
278
286
struct argv_array cmd = ARGV_ARRAY_INIT ;
279
287
struct option options [] = {
280
- OPT__FORCE (& force , N_ ("checkout <branch> even if already checked out in other worktree" )),
281
- OPT_STRING ('b' , NULL , & new_branch , N_ ("branch" ),
288
+ OPT__FORCE (& opts . force , N_ ("checkout <branch> even if already checked out in other worktree" )),
289
+ OPT_STRING ('b' , NULL , & opts . new_branch , N_ ("branch" ),
282
290
N_ ("create a new branch" )),
283
291
OPT_STRING ('B' , NULL , & new_branch_force , N_ ("branch" ),
284
292
N_ ("create or reset a branch" )),
285
- OPT_BOOL (0 , "detach" , & detach , N_ ("detach HEAD at named commit" )),
293
+ OPT_BOOL (0 , "detach" , & opts . detach , N_ ("detach HEAD at named commit" )),
286
294
OPT_END ()
287
295
};
288
296
297
+ memset (& opts , 0 , sizeof (opts ));
289
298
ac = parse_options (ac , av , prefix , options , worktree_usage , 0 );
290
- if (new_branch && new_branch_force )
299
+ if (opts . new_branch && new_branch_force )
291
300
die (_ ("-b and -B are mutually exclusive" ));
292
301
if (ac < 1 || ac > 2 )
293
302
usage_with_options (worktree_usage , options );
294
303
295
304
path = prefix ? prefix_filename (prefix , strlen (prefix ), av [0 ]) : av [0 ];
296
305
branch = ac < 2 ? "HEAD" : av [1 ];
297
306
298
- force_new_branch = !!new_branch_force ;
299
- if (force_new_branch )
300
- new_branch = new_branch_force ;
307
+ opts . force_new_branch = !!new_branch_force ;
308
+ if (opts . force_new_branch )
309
+ opts . new_branch = new_branch_force ;
301
310
302
- if (ac < 2 && !new_branch ) {
311
+ if (ac < 2 && !opts . new_branch ) {
303
312
int n ;
304
313
const char * s = worktree_basename (path , & n );
305
- new_branch = xstrndup (s , n );
314
+ opts . new_branch = xstrndup (s , n );
306
315
}
307
316
308
317
argv_array_push (& cmd , "checkout" );
309
- if (force )
318
+ if (opts . force )
310
319
argv_array_push (& cmd , "--ignore-other-worktrees" );
311
- if (new_branch )
312
- argv_array_pushl (& cmd , force_new_branch ? "-B" : "-b" ,
313
- new_branch , NULL );
314
- if (detach )
320
+ if (opts . new_branch )
321
+ argv_array_pushl (& cmd , opts . force_new_branch ? "-B" : "-b" ,
322
+ opts . new_branch , NULL );
323
+ if (opts . detach )
315
324
argv_array_push (& cmd , "--detach" );
316
325
argv_array_push (& cmd , branch );
317
326
318
- return add_worktree (path , cmd .argv );
327
+ return add_worktree (path , cmd .argv , & opts );
319
328
}
320
329
321
330
int cmd_worktree (int ac , const char * * av , const char * prefix )
0 commit comments