@@ -1476,7 +1476,8 @@ static void die_if_some_operation_in_progress(void)
1476
1476
}
1477
1477
1478
1478
static int checkout_branch (struct checkout_opts * opts ,
1479
- struct branch_info * new_branch_info )
1479
+ struct branch_info * new_branch_info ,
1480
+ char * check_branch_path )
1480
1481
{
1481
1482
if (opts -> pathspec .nr )
1482
1483
die (_ ("paths cannot be used with switching branches" ));
@@ -1535,13 +1536,13 @@ static int checkout_branch(struct checkout_opts *opts,
1535
1536
if (!opts -> can_switch_when_in_progress )
1536
1537
die_if_some_operation_in_progress ();
1537
1538
1538
- if (new_branch_info -> path && !opts -> force_detach && !opts -> new_branch &&
1539
- ! opts -> ignore_other_worktrees ) {
1539
+ if (!opts -> ignore_other_worktrees && !opts -> force_detach &&
1540
+ check_branch_path && ref_exists ( check_branch_path ) ) {
1540
1541
int flag ;
1541
1542
char * head_ref = resolve_refdup ("HEAD" , 0 , NULL , & flag );
1542
- if (head_ref &&
1543
- (!(flag & REF_ISSYMREF ) || strcmp (head_ref , new_branch_info -> path )))
1544
- die_if_checked_out (new_branch_info -> path , 1 );
1543
+ if (opts -> new_branch_force || ( head_ref &&
1544
+ (!(flag & REF_ISSYMREF ) || strcmp (head_ref , check_branch_path ) )))
1545
+ die_if_checked_out (check_branch_path , 1 );
1545
1546
free (head_ref );
1546
1547
}
1547
1548
@@ -1629,7 +1630,9 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
1629
1630
const char * const usagestr [],
1630
1631
struct branch_info * new_branch_info )
1631
1632
{
1633
+ int ret ;
1632
1634
int parseopt_flags = 0 ;
1635
+ char * check_branch_path = NULL ;
1633
1636
1634
1637
opts -> overwrite_ignore = 1 ;
1635
1638
opts -> prefix = prefix ;
@@ -1719,6 +1722,13 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
1719
1722
opts -> new_branch = argv0 + 1 ;
1720
1723
}
1721
1724
1725
+ if (opts -> new_branch && !opts -> ignore_other_worktrees ) {
1726
+ struct strbuf buf = STRBUF_INIT ;
1727
+
1728
+ strbuf_branchname (& buf , opts -> new_branch , INTERPRET_BRANCH_LOCAL );
1729
+ strbuf_splice (& buf , 0 , 0 , "refs/heads/" , 11 );
1730
+ check_branch_path = strbuf_detach (& buf , NULL );
1731
+ }
1722
1732
/*
1723
1733
* Extract branch name from command line arguments, so
1724
1734
* all that is left is pathspecs.
@@ -1743,6 +1753,9 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
1743
1753
new_branch_info , opts , & rev );
1744
1754
argv += n ;
1745
1755
argc -= n ;
1756
+
1757
+ if (!opts -> ignore_other_worktrees && !check_branch_path && new_branch_info -> path )
1758
+ check_branch_path = xstrdup (new_branch_info -> path );
1746
1759
} else if (!opts -> accept_ref && opts -> from_treeish ) {
1747
1760
struct object_id rev ;
1748
1761
@@ -1819,9 +1832,12 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
1819
1832
}
1820
1833
1821
1834
if (opts -> patch_mode || opts -> pathspec .nr )
1822
- return checkout_paths (opts , new_branch_info );
1835
+ ret = checkout_paths (opts , new_branch_info );
1823
1836
else
1824
- return checkout_branch (opts , new_branch_info );
1837
+ ret = checkout_branch (opts , new_branch_info , check_branch_path );
1838
+
1839
+ free (check_branch_path );
1840
+ return ret ;
1825
1841
}
1826
1842
1827
1843
int cmd_checkout (int argc , const char * * argv , const char * prefix )
0 commit comments