@@ -27,8 +27,6 @@ struct add_opts {
27
27
int detach ;
28
28
int checkout ;
29
29
int keep_locked ;
30
- const char * new_branch ;
31
- int force_new_branch ;
32
30
};
33
31
34
32
static int show_only ;
@@ -363,10 +361,11 @@ static int add(int ac, const char **av, const char *prefix)
363
361
const char * new_branch_force = NULL ;
364
362
char * path ;
365
363
const char * branch ;
364
+ const char * new_branch = NULL ;
366
365
const char * opt_track = NULL ;
367
366
struct option options [] = {
368
367
OPT__FORCE (& opts .force , N_ ("checkout <branch> even if already checked out in other worktree" )),
369
- OPT_STRING ('b' , NULL , & opts . new_branch , N_ ("branch" ),
368
+ OPT_STRING ('b' , NULL , & new_branch , N_ ("branch" ),
370
369
N_ ("create a new branch" )),
371
370
OPT_STRING ('B' , NULL , & new_branch_force , N_ ("branch" ),
372
371
N_ ("create or reset a branch" )),
@@ -384,7 +383,7 @@ static int add(int ac, const char **av, const char *prefix)
384
383
memset (& opts , 0 , sizeof (opts ));
385
384
opts .checkout = 1 ;
386
385
ac = parse_options (ac , av , prefix , options , worktree_usage , 0 );
387
- if (!!opts .detach + !!opts . new_branch + !!new_branch_force > 1 )
386
+ if (!!opts .detach + !!new_branch + !!new_branch_force > 1 )
388
387
die (_ ("-b, -B, and --detach are mutually exclusive" ));
389
388
if (ac < 1 || ac > 2 )
390
389
usage_with_options (worktree_usage , options );
@@ -395,33 +394,33 @@ static int add(int ac, const char **av, const char *prefix)
395
394
if (!strcmp (branch , "-" ))
396
395
branch = "@{-1}" ;
397
396
398
- opts .force_new_branch = !!new_branch_force ;
399
- if (opts .force_new_branch ) {
397
+ if (new_branch_force ) {
400
398
struct strbuf symref = STRBUF_INIT ;
401
399
402
- opts . new_branch = new_branch_force ;
400
+ new_branch = new_branch_force ;
403
401
404
402
if (!opts .force &&
405
- !strbuf_check_branch_ref (& symref , opts . new_branch ) &&
403
+ !strbuf_check_branch_ref (& symref , new_branch ) &&
406
404
ref_exists (symref .buf ))
407
405
die_if_checked_out (symref .buf , 0 );
408
406
strbuf_release (& symref );
409
407
}
410
408
411
- if (ac < 2 && !opts . new_branch && !opts .detach ) {
409
+ if (ac < 2 && !new_branch && !opts .detach ) {
412
410
int n ;
413
411
const char * s = worktree_basename (path , & n );
414
- opts .new_branch = xstrndup (s , n );
412
+ new_branch = xstrndup (s , n );
413
+ UNLEAK (new_branch );
415
414
if (guess_remote ) {
416
415
struct object_id oid ;
417
416
const char * remote =
418
- unique_tracking_name (opts . new_branch , & oid );
417
+ unique_tracking_name (new_branch , & oid );
419
418
if (remote )
420
419
branch = remote ;
421
420
}
422
421
}
423
422
424
- if (ac == 2 && !opts . new_branch && !opts .detach ) {
423
+ if (ac == 2 && !new_branch && !opts .detach ) {
425
424
struct object_id oid ;
426
425
struct commit * commit ;
427
426
const char * remote ;
@@ -430,25 +429,25 @@ static int add(int ac, const char **av, const char *prefix)
430
429
if (!commit ) {
431
430
remote = unique_tracking_name (branch , & oid );
432
431
if (remote ) {
433
- opts . new_branch = branch ;
432
+ new_branch = branch ;
434
433
branch = remote ;
435
434
}
436
435
}
437
436
}
438
437
439
- if (opts . new_branch ) {
438
+ if (new_branch ) {
440
439
struct child_process cp = CHILD_PROCESS_INIT ;
441
440
cp .git_cmd = 1 ;
442
441
argv_array_push (& cp .args , "branch" );
443
- if (opts . force_new_branch )
442
+ if (new_branch_force )
444
443
argv_array_push (& cp .args , "--force" );
445
- argv_array_push (& cp .args , opts . new_branch );
444
+ argv_array_push (& cp .args , new_branch );
446
445
argv_array_push (& cp .args , branch );
447
446
if (opt_track )
448
447
argv_array_push (& cp .args , opt_track );
449
448
if (run_command (& cp ))
450
449
return -1 ;
451
- branch = opts . new_branch ;
450
+ branch = new_branch ;
452
451
} else if (opt_track ) {
453
452
die (_ ("--[no-]track can only be used if a new branch is created" ));
454
453
}
0 commit comments