Skip to content

Commit fe106ab

Browse files
Update switch subcommand so it actually works properly
The git-sim switch command is updated to handle 3 scenarios: - branch being switched to is an ancestor of HEAD - branch being switched to is a descendant of HEAD - branch being switched to has diverged from HEAD Signed-off-by: Jacob Stopak <jacob@initialcommit.io>
1 parent 62095ba commit fe106ab

2 files changed

Lines changed: 72 additions & 56 deletions

File tree

git_sim/git_sim_base_command.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,28 @@ def reset_head(self, hexsha, shift=numpy.array([0.0, 0.0, 0.0])):
856856
0,
857857
)
858858
)
859+
860+
def reset_branch(self, hexsha, shift=numpy.array([0.0, 0.0, 0.0])):
861+
if settings.animate:
862+
self.play(
863+
self.drawnRefs[self.repo.active_branch.name].animate.move_to(
864+
(
865+
self.drawnCommits[hexsha].get_center()[0] + shift[0],
866+
self.drawnCommits[hexsha].get_center()[1] + 1.4 + shift[1],
867+
0,
868+
)
869+
),
870+
)
871+
else:
872+
self.drawnRefs[self.repo.active_branch.name].move_to(
873+
(
874+
self.drawnCommits[hexsha].get_center()[0] + shift[0],
875+
self.drawnCommits[hexsha].get_center()[1] + 1.4 + shift[1],
876+
0,
877+
)
878+
)
879+
880+
859881
def reset_head_branch_to_ref(self, ref, shift=numpy.array([0.0, 0.0, 0.0])):
860882
if settings.animate:
861883
self.play(self.drawnRefs["HEAD"].animate.next_to(ref, m.UP))

git_sim/switch.py

Lines changed: 50 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,24 @@ def __init__(self, branch: str):
2626
)
2727
sys.exit(1)
2828

29-
self.ff = False
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
46+
3047
if self.branch in [branch.name for branch in self.repo.heads]:
3148
self.selected_branches.append(self.branch)
3249

@@ -36,75 +53,52 @@ def __init__(self, branch: str):
3653
pass
3754

3855
def construct(self):
39-
if not settings.stdout:
56+
if not settings.stdout and not settings.output_only_path and not settings.quiet:
4057
print(
4158
f"{settings.INFO_STRING } {type(self).__name__.lower()} {self.branch}"
4259
)
4360

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-
5661
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])
62+
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'))
67+
68+
# branch is reached from HEAD, so draw everything
69+
if len(commits_in_range) <= self.n:
70+
self.parse_commits(head_commit)
71+
reset_head_to = branch_commit.hexsha
72+
self.recenter_frame()
73+
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
78+
else:
79+
self.parse_commits(branch_commit)
80+
self.draw_ref(branch_commit, self.topref)
8381

82+
elif self.is_descendant:
83+
self.parse_commits(branch_commit)
84+
reset_head_to = branch_commit.hexsha
8485
self.recenter_frame()
8586
self.scale_frame()
8687
if "HEAD" in self.drawnRefs:
87-
self.reset_head_branch(reset_head_to, shift=shift)
88+
self.reset_head(reset_head_to)
89+
self.reset_branch(head_commit.hexsha)
8890
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-
91+
self.draw_ref(branch_commit, self.topref)
9792
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])
93+
self.parse_commits(head_commit)
94+
self.parse_commits(branch_commit, shift=4 * m.DOWN)
95+
self.center_frame_on_commit(branch_commit)
10496
self.recenter_frame()
10597
self.scale_frame()
106-
self.reset_head(self.commits[0].hexsha)
98+
self.reset_head(branch_commit.hexsha)
99+
self.reset_branch(head_commit.hexsha)
107100

101+
self.color_by()
108102
self.fadeout()
109103
self.show_outro()
110104

0 commit comments

Comments
 (0)