1212
1313
1414class Switch (GitSimBaseCommand ):
15- def __init__ (self , branch : str ):
15+ def __init__ (self , branch : str , c : bool ):
1616 super ().__init__ ()
1717 self .branch = branch
18+ self .c = c
1819
19- try :
20- git .repo .fun .rev_parse (self .repo , self .branch )
21- except git .exc .BadName :
22- print (
23- "git-sim error: '"
24- + self .branch
25- + "' is not a valid Git ref or identifier."
26- )
27- sys .exit (1 )
28-
29- if self .branch == self .repo .active_branch .name :
30- print ("git-sim error: already on branch '" + self .branch + "'" )
31- sys .exit (1 )
32-
33- self .is_ancestor = False
34- self .is_descendant = False
35-
36- # branch being switched to is behind HEAD
37- if self .repo .active_branch .name in self .repo .git .branch (
38- "--contains" , self .branch
39- ):
40- self .is_ancestor = True
41- # HEAD is behind branch being switched to
42- elif self .branch in self .repo .git .branch (
43- "--contains" , self .repo .active_branch .name
44- ):
45- self .is_descendant = True
20+ if self .c :
21+ if self .branch in self .repo .heads :
22+ print ("git-sim error: can't create new branch '" + self .branch + "', it already exists" )
23+ sys .exit (1 )
24+ else :
25+ try :
26+ git .repo .fun .rev_parse (self .repo , self .branch )
27+ except git .exc .BadName :
28+ print (
29+ "git-sim error: '"
30+ + self .branch
31+ + "' is not a valid Git ref or identifier."
32+ )
33+ sys .exit (1 )
34+
35+ if self .branch == self .repo .active_branch .name :
36+ print ("git-sim error: already on branch '" + self .branch + "'" )
37+ sys .exit (1 )
38+
39+ self .is_ancestor = False
40+ self .is_descendant = False
41+
42+ # branch being switched to is behind HEAD
43+ if self .repo .active_branch .name in self .repo .git .branch (
44+ "--contains" , self .branch
45+ ):
46+ self .is_ancestor = True
47+ # HEAD is behind branch being switched to
48+ elif self .branch in self .repo .git .branch (
49+ "--contains" , self .repo .active_branch .name
50+ ):
51+ self .is_descendant = True
4652
4753 if self .branch in [branch .name for branch in self .repo .heads ]:
4854 self .selected_branches .append (self .branch )
@@ -60,43 +66,49 @@ def construct(self):
6066
6167 self .show_intro ()
6268 head_commit = self .get_commit ()
63- branch_commit = self .get_commit (self .branch )
64-
65- if self .is_ancestor :
66- commits_in_range = list (self .repo .iter_commits (self .branch + '..HEAD' ))
6769
68- # branch is reached from HEAD, so draw everything
69- if len (commits_in_range ) <= self .n :
70- self .parse_commits (head_commit )
70+ # using -c flag, create new branch label and exit
71+ if self .c :
72+ self .parse_commits (head_commit )
73+ self .draw_ref (head_commit , self .topref , text = self .branch , color = m .GREEN )
74+ else :
75+ branch_commit = self .get_commit (self .branch )
76+
77+ if self .is_ancestor :
78+ commits_in_range = list (self .repo .iter_commits (self .branch + '..HEAD' ))
79+
80+ # branch is reached from HEAD, so draw everything
81+ if len (commits_in_range ) <= self .n :
82+ self .parse_commits (head_commit )
83+ reset_head_to = branch_commit .hexsha
84+ self .recenter_frame ()
85+ self .scale_frame ()
86+ self .reset_head (reset_head_to )
87+ self .reset_branch (head_commit .hexsha )
88+
89+ # branch is not reached, so start from branch
90+ else :
91+ self .parse_commits (branch_commit )
92+ self .draw_ref (branch_commit , self .topref )
93+
94+ elif self .is_descendant :
95+ self .parse_commits (branch_commit )
7196 reset_head_to = branch_commit .hexsha
7297 self .recenter_frame ()
7398 self .scale_frame ()
74- self .reset_head (reset_head_to )
75- self .reset_branch (head_commit .hexsha )
76-
77- # branch is not reached, so start from branch
99+ if "HEAD" in self .drawnRefs :
100+ self .reset_head (reset_head_to )
101+ self .reset_branch (head_commit .hexsha )
102+ else :
103+ self .draw_ref (branch_commit , self .topref )
78104 else :
79- self .parse_commits (branch_commit )
80- self .draw_ref (branch_commit , self .topref )
81-
82- elif self .is_descendant :
83- self .parse_commits (branch_commit )
84- reset_head_to = branch_commit .hexsha
85- self .recenter_frame ()
86- self .scale_frame ()
87- if "HEAD" in self .drawnRefs :
88- self .reset_head (reset_head_to )
105+ self .parse_commits (head_commit )
106+ self .parse_commits (branch_commit , shift = 4 * m .DOWN )
107+ self .center_frame_on_commit (branch_commit )
108+ self .recenter_frame ()
109+ self .scale_frame ()
110+ self .reset_head (branch_commit .hexsha )
89111 self .reset_branch (head_commit .hexsha )
90- else :
91- self .draw_ref (branch_commit , self .topref )
92- else :
93- self .parse_commits (head_commit )
94- self .parse_commits (branch_commit , shift = 4 * m .DOWN )
95- self .center_frame_on_commit (branch_commit )
96- self .recenter_frame ()
97- self .scale_frame ()
98- self .reset_head (branch_commit .hexsha )
99- self .reset_branch (head_commit .hexsha )
100112
101113 self .color_by ()
102114 self .fadeout ()
@@ -108,6 +120,11 @@ def switch(
108120 ...,
109121 help = "The name of the branch to switch to" ,
110122 ),
123+ c : bool = typer .Option (
124+ False ,
125+ "-c" ,
126+ help = "Create the specified branch if it doesn't already exist" ,
127+ ),
111128):
112- scene = Switch (branch = branch )
129+ scene = Switch (branch = branch , c = c )
113130 handle_animations (scene = scene )
0 commit comments