@@ -29,8 +29,6 @@ struct add_opts {
29
29
int detach ;
30
30
int checkout ;
31
31
int keep_locked ;
32
- const char * new_branch ;
33
- int force_new_branch ;
34
32
};
35
33
36
34
static int show_only ;
@@ -298,8 +296,6 @@ static int add_worktree(const char *path, const char *refname,
298
296
strbuf_addf (& sb , "%s/commondir" , sb_repo .buf );
299
297
write_file (sb .buf , "../.." );
300
298
301
- fprintf_ln (stderr , _ ("Preparing %s (identifier %s)" ), path , name );
302
-
303
299
argv_array_pushf (& child_env , "%s=%s" , GIT_DIR_ENVIRONMENT , sb_git .buf );
304
300
argv_array_pushf (& child_env , "%s=%s" , GIT_WORK_TREE_ENVIRONMENT , path );
305
301
cp .git_cmd = 1 ;
@@ -366,18 +362,75 @@ static int add_worktree(const char *path, const char *refname,
366
362
return ret ;
367
363
}
368
364
365
+ static void print_preparing_worktree_line (int detach ,
366
+ const char * branch ,
367
+ const char * new_branch ,
368
+ int force_new_branch )
369
+ {
370
+ if (force_new_branch ) {
371
+ struct commit * commit = lookup_commit_reference_by_name (new_branch );
372
+ if (!commit )
373
+ printf_ln (_ ("Preparing worktree (new branch '%s')" ), new_branch );
374
+ else
375
+ printf_ln (_ ("Preparing worktree (resetting branch '%s'; was at %s)" ),
376
+ new_branch ,
377
+ find_unique_abbrev (& commit -> object .oid , DEFAULT_ABBREV ));
378
+ } else if (new_branch ) {
379
+ printf_ln (_ ("Preparing worktree (new branch '%s')" ), new_branch );
380
+ } else {
381
+ struct strbuf s = STRBUF_INIT ;
382
+ if (!detach && !strbuf_check_branch_ref (& s , branch ) &&
383
+ ref_exists (s .buf ))
384
+ printf_ln (_ ("Preparing worktree (checking out '%s')" ),
385
+ branch );
386
+ else {
387
+ struct commit * commit = lookup_commit_reference_by_name (branch );
388
+ if (!commit )
389
+ die (_ ("invalid reference: %s" ), branch );
390
+ printf_ln (_ ("Preparing worktree (detached HEAD %s)" ),
391
+ find_unique_abbrev (& commit -> object .oid , DEFAULT_ABBREV ));
392
+ }
393
+ strbuf_release (& s );
394
+ }
395
+ }
396
+
397
+ static const char * dwim_branch (const char * path , const char * * new_branch )
398
+ {
399
+ int n ;
400
+ const char * s = worktree_basename (path , & n );
401
+ const char * branchname = xstrndup (s , n );
402
+ struct strbuf ref = STRBUF_INIT ;
403
+
404
+ UNLEAK (branchname );
405
+ if (!strbuf_check_branch_ref (& ref , branchname ) &&
406
+ ref_exists (ref .buf )) {
407
+ strbuf_release (& ref );
408
+ return branchname ;
409
+ }
410
+
411
+ * new_branch = branchname ;
412
+ if (guess_remote ) {
413
+ struct object_id oid ;
414
+ const char * remote =
415
+ unique_tracking_name (* new_branch , & oid );
416
+ return remote ;
417
+ }
418
+ return NULL ;
419
+ }
420
+
369
421
static int add (int ac , const char * * av , const char * prefix )
370
422
{
371
423
struct add_opts opts ;
372
424
const char * new_branch_force = NULL ;
373
425
char * path ;
374
426
const char * branch ;
427
+ const char * new_branch = NULL ;
375
428
const char * opt_track = NULL ;
376
429
struct option options [] = {
377
430
OPT__FORCE (& opts .force ,
378
431
N_ ("checkout <branch> even if already checked out in other worktree" ),
379
432
PARSE_OPT_NOCOMPLETE ),
380
- OPT_STRING ('b' , NULL , & opts . new_branch , N_ ("branch" ),
433
+ OPT_STRING ('b' , NULL , & new_branch , N_ ("branch" ),
381
434
N_ ("create a new branch" )),
382
435
OPT_STRING ('B' , NULL , & new_branch_force , N_ ("branch" ),
383
436
N_ ("create or reset a branch" )),
@@ -395,7 +448,7 @@ static int add(int ac, const char **av, const char *prefix)
395
448
memset (& opts , 0 , sizeof (opts ));
396
449
opts .checkout = 1 ;
397
450
ac = parse_options (ac , av , prefix , options , worktree_usage , 0 );
398
- if (!!opts .detach + !!opts . new_branch + !!new_branch_force > 1 )
451
+ if (!!opts .detach + !!new_branch + !!new_branch_force > 1 )
399
452
die (_ ("-b, -B, and --detach are mutually exclusive" ));
400
453
if (ac < 1 || ac > 2 )
401
454
usage_with_options (worktree_usage , options );
@@ -406,33 +459,25 @@ static int add(int ac, const char **av, const char *prefix)
406
459
if (!strcmp (branch , "-" ))
407
460
branch = "@{-1}" ;
408
461
409
- opts .force_new_branch = !!new_branch_force ;
410
- if (opts .force_new_branch ) {
462
+ if (new_branch_force ) {
411
463
struct strbuf symref = STRBUF_INIT ;
412
464
413
- opts . new_branch = new_branch_force ;
465
+ new_branch = new_branch_force ;
414
466
415
467
if (!opts .force &&
416
- !strbuf_check_branch_ref (& symref , opts . new_branch ) &&
468
+ !strbuf_check_branch_ref (& symref , new_branch ) &&
417
469
ref_exists (symref .buf ))
418
470
die_if_checked_out (symref .buf , 0 );
419
471
strbuf_release (& symref );
420
472
}
421
473
422
- if (ac < 2 && !opts .new_branch && !opts .detach ) {
423
- int n ;
424
- const char * s = worktree_basename (path , & n );
425
- opts .new_branch = xstrndup (s , n );
426
- if (guess_remote ) {
427
- struct object_id oid ;
428
- const char * remote =
429
- unique_tracking_name (opts .new_branch , & oid );
430
- if (remote )
431
- branch = remote ;
432
- }
474
+ if (ac < 2 && !new_branch && !opts .detach ) {
475
+ const char * s = dwim_branch (path , & new_branch );
476
+ if (s )
477
+ branch = s ;
433
478
}
434
479
435
- if (ac == 2 && !opts . new_branch && !opts .detach ) {
480
+ if (ac == 2 && !new_branch && !opts .detach ) {
436
481
struct object_id oid ;
437
482
struct commit * commit ;
438
483
const char * remote ;
@@ -441,25 +486,27 @@ static int add(int ac, const char **av, const char *prefix)
441
486
if (!commit ) {
442
487
remote = unique_tracking_name (branch , & oid );
443
488
if (remote ) {
444
- opts . new_branch = branch ;
489
+ new_branch = branch ;
445
490
branch = remote ;
446
491
}
447
492
}
448
493
}
449
494
450
- if (opts .new_branch ) {
495
+ print_preparing_worktree_line (opts .detach , branch , new_branch , !!new_branch_force );
496
+
497
+ if (new_branch ) {
451
498
struct child_process cp = CHILD_PROCESS_INIT ;
452
499
cp .git_cmd = 1 ;
453
500
argv_array_push (& cp .args , "branch" );
454
- if (opts . force_new_branch )
501
+ if (new_branch_force )
455
502
argv_array_push (& cp .args , "--force" );
456
- argv_array_push (& cp .args , opts . new_branch );
503
+ argv_array_push (& cp .args , new_branch );
457
504
argv_array_push (& cp .args , branch );
458
505
if (opt_track )
459
506
argv_array_push (& cp .args , opt_track );
460
507
if (run_command (& cp ))
461
508
return -1 ;
462
- branch = opts . new_branch ;
509
+ branch = new_branch ;
463
510
} else if (opt_track ) {
464
511
die (_ ("--[no-]track can only be used if a new branch is created" ));
465
512
}
0 commit comments