Skip to content

Commit daf9f97

Browse files
Add switch subcommand, no detached head supported
Signed-off-by: Jacob Stopak <jacob@initialcommit.io>
1 parent 08530c3 commit daf9f97

3 files changed

Lines changed: 141 additions & 0 deletions

File tree

git_sim/__main__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import git_sim.stash
2020
import git_sim.status
2121
import git_sim.tag
22+
import git_sim.switch
2223
from git_sim.settings import ImgFormat, VideoFormat, settings
2324
from manim import config, WHITE
2425

@@ -164,6 +165,7 @@ def main(
164165
app.command()(git_sim.stash.stash)
165166
app.command()(git_sim.status.status)
166167
app.command()(git_sim.tag.tag)
168+
app.command()(git_sim.switch.switch)
167169

168170

169171
if __name__ == "__main__":

git_sim/git_sim_base_command.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,26 @@ def reset_head_branch(self, hexsha, shift=numpy.array([0.0, 0.0, 0.0])):
788788
)
789789
)
790790

791+
def reset_head(self, hexsha, shift=numpy.array([0.0, 0.0, 0.0])):
792+
if settings.animate:
793+
self.play(
794+
self.drawnRefs["HEAD"].animate.move_to(
795+
(
796+
self.drawnCommits[hexsha].get_center()[0] + shift[0],
797+
self.drawnCommits[hexsha].get_center()[1] + 2.0 + shift[1],
798+
0,
799+
)
800+
),
801+
)
802+
else:
803+
self.drawnRefs["HEAD"].move_to(
804+
(
805+
self.drawnCommits[hexsha].get_center()[0] + shift[0],
806+
self.drawnCommits[hexsha].get_center()[1] + 2.0 + shift[1],
807+
0,
808+
)
809+
)
810+
791811
def translate_frame(self, shift):
792812
if settings.animate:
793813
self.play(self.camera.frame.animate.shift(shift))

git_sim/switch.py

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import sys
2+
from argparse import Namespace
3+
4+
import git
5+
import manim as m
6+
import numpy
7+
import typer
8+
9+
from git_sim.animations import handle_animations
10+
from git_sim.git_sim_base_command import GitSimBaseCommand
11+
from git_sim.settings import settings
12+
13+
14+
class Switch(GitSimBaseCommand):
15+
def __init__(self, branch: str):
16+
super().__init__()
17+
self.branch = branch
18+
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+
self.ff = False
30+
if self.branch in [branch.name for branch in self.repo.heads]:
31+
self.selected_branches.append(self.branch)
32+
33+
try:
34+
self.selected_branches.append(self.repo.active_branch.name)
35+
except TypeError:
36+
pass
37+
38+
def construct(self):
39+
if not settings.stdout:
40+
print(
41+
f"{settings.INFO_STRING } {type(self).__name__.lower()} {self.branch}"
42+
)
43+
44+
if self.repo.active_branch.name in self.repo.git.branch(
45+
"--contains", self.branch
46+
):
47+
print(
48+
"git-sim error: Branch '"
49+
+ self.branch
50+
+ "' is already included in the history of active branch '"
51+
+ self.repo.active_branch.name
52+
+ "'."
53+
)
54+
sys.exit(1)
55+
56+
self.show_intro()
57+
self.get_commits()
58+
self.orig_commits = self.commits
59+
self.get_commits(start=self.branch)
60+
61+
if not self.is_remote_tracking_branch(self.branch):
62+
if self.branch in self.repo.git.branch(
63+
"--contains", self.orig_commits[0].hexsha
64+
):
65+
self.ff = True
66+
else:
67+
if self.branch in self.repo.git.branch(
68+
"-r", "--contains", self.orig_commits[0].hexsha
69+
):
70+
self.ff = True
71+
72+
if self.ff:
73+
self.get_commits(start=self.branch)
74+
self.parse_commits(self.commits[0])
75+
reset_head_to = self.commits[0].hexsha
76+
shift = numpy.array([0.0, 0.6, 0.0])
77+
78+
if self.no_ff:
79+
self.center_frame_on_commit(self.commits[0])
80+
commitId = self.setup_and_draw_parent(self.commits[0], "Merge commit")
81+
reset_head_to = "abcdef"
82+
shift = numpy.array([0.0, 0.0, 0.0])
83+
84+
self.recenter_frame()
85+
self.scale_frame()
86+
if "HEAD" in self.drawnRefs:
87+
self.reset_head_branch(reset_head_to, shift=shift)
88+
else:
89+
self.draw_ref(self.commits[0], commitId if self.no_ff else self.topref)
90+
self.draw_ref(
91+
self.commits[0],
92+
self.drawnRefs["HEAD"],
93+
text=self.repo.active_branch.name,
94+
color=m.GREEN,
95+
)
96+
97+
else:
98+
self.get_commits()
99+
self.parse_commits(self.commits[0])
100+
self.i = 0
101+
self.get_commits(start=self.branch)
102+
self.parse_commits(self.commits[0], shift=4 * m.DOWN)
103+
self.center_frame_on_commit(self.orig_commits[0])
104+
self.recenter_frame()
105+
self.scale_frame()
106+
self.reset_head(self.commits[0].hexsha)
107+
108+
self.fadeout()
109+
self.show_outro()
110+
111+
112+
def switch(
113+
branch: str = typer.Argument(
114+
...,
115+
help="The name of the branch to switch to",
116+
),
117+
):
118+
scene = Switch(branch=branch)
119+
handle_animations(scene=scene)

0 commit comments

Comments
 (0)