@@ -512,9 +512,9 @@ static void print_current_branch_name(void)
512
512
die (_ ("HEAD (%s) points outside of refs/heads/" ), refname );
513
513
}
514
514
515
- static void reject_rebase_or_bisect_branch (const char * target )
515
+ static void reject_rebase_or_bisect_branch (struct worktree * * worktrees ,
516
+ const char * target )
516
517
{
517
- struct worktree * * worktrees = get_worktrees ();
518
518
int i ;
519
519
520
520
for (i = 0 ; worktrees [i ]; i ++ ) {
@@ -531,17 +531,50 @@ static void reject_rebase_or_bisect_branch(const char *target)
531
531
die (_ ("Branch %s is being bisected at %s" ),
532
532
target , wt -> path );
533
533
}
534
+ }
534
535
535
- free_worktrees (worktrees );
536
+ /*
537
+ * Update all per-worktree HEADs pointing at the old ref to point the new ref.
538
+ * This will be used when renaming a branch. Returns 0 if successful, non-zero
539
+ * otherwise.
540
+ */
541
+ static int replace_each_worktree_head_symref (struct worktree * * worktrees ,
542
+ const char * oldref , const char * newref ,
543
+ const char * logmsg )
544
+ {
545
+ int ret = 0 ;
546
+ int i ;
547
+
548
+ for (i = 0 ; worktrees [i ]; i ++ ) {
549
+ struct ref_store * refs ;
550
+
551
+ if (worktrees [i ]-> is_detached )
552
+ continue ;
553
+ if (!worktrees [i ]-> head_ref )
554
+ continue ;
555
+ if (strcmp (oldref , worktrees [i ]-> head_ref ))
556
+ continue ;
557
+
558
+ refs = get_worktree_ref_store (worktrees [i ]);
559
+ if (refs_create_symref (refs , "HEAD" , newref , logmsg ))
560
+ ret = error (_ ("HEAD of working tree %s is not updated" ),
561
+ worktrees [i ]-> path );
562
+ }
563
+
564
+ return ret ;
536
565
}
537
566
567
+ #define IS_HEAD 1
568
+ #define IS_ORPHAN 2
569
+
538
570
static void copy_or_rename_branch (const char * oldname , const char * newname , int copy , int force )
539
571
{
540
572
struct strbuf oldref = STRBUF_INIT , newref = STRBUF_INIT , logmsg = STRBUF_INIT ;
541
573
struct strbuf oldsection = STRBUF_INIT , newsection = STRBUF_INIT ;
542
574
const char * interpreted_oldname = NULL ;
543
575
const char * interpreted_newname = NULL ;
544
- int recovery = 0 ;
576
+ int recovery = 0 , oldref_usage = 0 ;
577
+ struct worktree * * worktrees = get_worktrees ();
545
578
546
579
if (strbuf_check_branch_ref (& oldref , oldname )) {
547
580
/*
@@ -554,8 +587,19 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
554
587
die (_ ("Invalid branch name: '%s'" ), oldname );
555
588
}
556
589
557
- if ((copy || strcmp (head , oldname )) && !ref_exists (oldref .buf )) {
558
- if (copy && !strcmp (head , oldname ))
590
+ for (int i = 0 ; worktrees [i ]; i ++ ) {
591
+ struct worktree * wt = worktrees [i ];
592
+
593
+ if (wt -> head_ref && !strcmp (oldref .buf , wt -> head_ref )) {
594
+ oldref_usage |= IS_HEAD ;
595
+ if (is_null_oid (& wt -> head_oid ))
596
+ oldref_usage |= IS_ORPHAN ;
597
+ break ;
598
+ }
599
+ }
600
+
601
+ if ((copy || !(oldref_usage & IS_HEAD )) && !ref_exists (oldref .buf )) {
602
+ if (oldref_usage & IS_HEAD )
559
603
die (_ ("No commit on branch '%s' yet." ), oldname );
560
604
else
561
605
die (_ ("No branch named '%s'." ), oldname );
@@ -570,7 +614,7 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
570
614
else
571
615
validate_new_branchname (newname , & newref , force );
572
616
573
- reject_rebase_or_bisect_branch (oldref .buf );
617
+ reject_rebase_or_bisect_branch (worktrees , oldref .buf );
574
618
575
619
if (!skip_prefix (oldref .buf , "refs/heads/" , & interpreted_oldname ) ||
576
620
!skip_prefix (newref .buf , "refs/heads/" , & interpreted_newname )) {
@@ -584,8 +628,7 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
584
628
strbuf_addf (& logmsg , "Branch: renamed %s to %s" ,
585
629
oldref .buf , newref .buf );
586
630
587
- if (!copy &&
588
- (!head || strcmp (oldname , head ) || !is_null_oid (& head_oid )) &&
631
+ if (!copy && !(oldref_usage & IS_ORPHAN ) &&
589
632
rename_ref (oldref .buf , newref .buf , logmsg .buf ))
590
633
die (_ ("Branch rename failed" ));
591
634
if (copy && copy_existing_ref (oldref .buf , newref .buf , logmsg .buf ))
@@ -600,8 +643,9 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
600
643
interpreted_oldname );
601
644
}
602
645
603
- if (!copy &&
604
- replace_each_worktree_head_symref (oldref .buf , newref .buf , logmsg .buf ))
646
+ if (!copy && (oldref_usage & IS_HEAD ) &&
647
+ replace_each_worktree_head_symref (worktrees , oldref .buf , newref .buf ,
648
+ logmsg .buf ))
605
649
die (_ ("Branch renamed to %s, but HEAD is not updated!" ), newname );
606
650
607
651
strbuf_release (& logmsg );
@@ -616,6 +660,7 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
616
660
strbuf_release (& newref );
617
661
strbuf_release (& oldsection );
618
662
strbuf_release (& newsection );
663
+ free_worktrees (worktrees );
619
664
}
620
665
621
666
static GIT_PATH_FUNC (edit_description , "EDIT_DESCRIPTION ")
@@ -834,7 +879,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
834
879
835
880
strbuf_addf (& branch_ref , "refs/heads/%s" , branch_name );
836
881
if (!ref_exists (branch_ref .buf ))
837
- error ((!argc || ! strcmp ( head , branch_name ))
882
+ error ((!argc || branch_checked_out ( branch_ref . buf ))
838
883
? _ ("No commit on branch '%s' yet." )
839
884
: _ ("No branch named '%s'." ),
840
885
branch_name );
@@ -879,7 +924,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
879
924
}
880
925
881
926
if (!ref_exists (branch -> refname )) {
882
- if (!argc || ! strcmp ( head , branch -> name ))
927
+ if (!argc || branch_checked_out ( branch -> refname ))
883
928
die (_ ("No commit on branch '%s' yet." ), branch -> name );
884
929
die (_ ("branch '%s' does not exist" ), branch -> name );
885
930
}
0 commit comments