|
| 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 Checkout(GitSimBaseCommand): |
| 15 | + def __init__(self, branch: str, b: bool): |
| 16 | + super().__init__() |
| 17 | + self.branch = branch |
| 18 | + self.b = b |
| 19 | + |
| 20 | + if self.b: |
| 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 checked out 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 checked out |
| 48 | + elif self.branch in self.repo.git.branch( |
| 49 | + "--contains", self.repo.active_branch.name |
| 50 | + ): |
| 51 | + self.is_descendant = True |
| 52 | + |
| 53 | + if self.branch in [branch.name for branch in self.repo.heads]: |
| 54 | + self.selected_branches.append(self.branch) |
| 55 | + |
| 56 | + try: |
| 57 | + self.selected_branches.append(self.repo.active_branch.name) |
| 58 | + except TypeError: |
| 59 | + pass |
| 60 | + |
| 61 | + def construct(self): |
| 62 | + if not settings.stdout and not settings.output_only_path and not settings.quiet: |
| 63 | + print( |
| 64 | + f"{settings.INFO_STRING } {type(self).__name__.lower()}{' -b' if self.b else ''} {self.branch}" |
| 65 | + ) |
| 66 | + |
| 67 | + self.show_intro() |
| 68 | + head_commit = self.get_commit() |
| 69 | + |
| 70 | + # using -b flag, create new branch label and exit |
| 71 | + if self.b: |
| 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) |
| 96 | + reset_head_to = branch_commit.hexsha |
| 97 | + self.recenter_frame() |
| 98 | + self.scale_frame() |
| 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) |
| 104 | + else: |
| 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) |
| 111 | + self.reset_branch(head_commit.hexsha) |
| 112 | + |
| 113 | + self.color_by() |
| 114 | + self.fadeout() |
| 115 | + self.show_outro() |
| 116 | + |
| 117 | + |
| 118 | +def checkout( |
| 119 | + branch: str = typer.Argument( |
| 120 | + ..., |
| 121 | + help="The name of the branch to checkout", |
| 122 | + ), |
| 123 | + b: bool = typer.Option( |
| 124 | + False, |
| 125 | + "-b", |
| 126 | + help="Create the specified branch if it doesn't already exist", |
| 127 | + ), |
| 128 | +): |
| 129 | + scene = Checkout(branch=branch, b=b) |
| 130 | + handle_animations(scene=scene) |
0 commit comments