1010
1111
1212class Switch (GitSimBaseCommand ):
13- def __init__ (self , branch : str , c : bool ):
13+ def __init__ (self , branch : str , c : bool , detach : bool ):
1414 super ().__init__ ()
1515 self .branch = branch
1616 self .c = c
17+ self .detach = detach
1718
1819 if self .c :
1920 if self .branch in self .repo .heads :
@@ -23,6 +24,11 @@ def __init__(self, branch: str, c: bool):
2324 + "', it already exists"
2425 )
2526 sys .exit (1 )
27+ if detach :
28+ print (
29+ "git-sim error: can't use both '-c' and '--detach' flags"
30+ )
31+ sys .exit (1 )
2632 else :
2733 try :
2834 git .repo .fun .rev_parse (self .repo , self .branch )
@@ -34,33 +40,45 @@ def __init__(self, branch: str, c: bool):
3440 )
3541 sys .exit (1 )
3642
37- if self .branch == self .repo .active_branch .name :
43+ if not self . repo . head . is_detached and self .branch == self .repo .active_branch .name :
3844 print ("git-sim error: already on branch '" + self .branch + "'" )
3945 sys .exit (1 )
4046
47+ if not self .detach :
48+ if self .branch not in self .repo .heads :
49+ print (
50+ "git-sim error: include --detach to allow detached HEAD"
51+ )
52+ sys .exit (1 )
53+
4154 self .is_ancestor = False
4255 self .is_descendant = False
4356
4457 # branch being switched to is behind HEAD
45- if self .repo .active_branch .name in self .repo .git .branch (
46- "--contains" , self .branch
47- ):
58+ branch_names = self .repo .git .branch ("--contains" , self .branch )
59+ branch_names = branch_names .split ("\n " )
60+ for i , bn in enumerate (branch_names ):
61+ branch_names [i ] = bn .strip ("*" ).strip ()
62+ branch_hexshas = [self .repo .branches [branch ].commit .hexsha for branch in branch_names ]
63+ if self .repo .head .commit .hexsha in branch_hexshas :
4864 self .is_ancestor = True
65+
4966 # HEAD is behind branch being switched to
5067 elif self .branch in self .repo .git .branch (
51- "--contains" , self .repo .active_branch . name
68+ "--contains" , self .repo .head . commit . hexsha
5269 ):
5370 self .is_descendant = True
5471
5572 if self .branch in [branch .name for branch in self .repo .heads ]:
5673 self .selected_branches .append (self .branch )
5774
5875 try :
59- self .selected_branches .append (self .repo .active_branch .name )
76+ if not self .repo .head .is_detached :
77+ self .selected_branches .append (self .repo .active_branch .name )
6078 except TypeError :
6179 pass
6280
63- self .cmd += f"{ settings . INFO_STRING } { type (self ).__name__ .lower ()} { ' -c' if self .c else '' } { self .branch } "
81+ self .cmd += f"{ type (self ).__name__ .lower ()} { ' -c' if self .c else '' } { ' --detach' if self . detach else '' } { self .branch } "
6482
6583 def construct (self ):
6684 if not settings .stdout and not settings .output_only_path and not settings .quiet :
@@ -104,7 +122,8 @@ def construct(self):
104122 self .scale_frame ()
105123 if "HEAD" in self .drawnRefs :
106124 self .reset_head (reset_head_to )
107- self .reset_branch (head_commit .hexsha )
125+ if not self .repo .head .is_detached :
126+ self .reset_branch (head_commit .hexsha )
108127 else :
109128 self .draw_ref (branch_commit , self .topref )
110129 else :
0 commit comments