|
| 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