| 10 | |
| 11 | |
| 12 | class Checkout(GitSimBaseCommand): |
| 13 | def __init__(self, branch: str, b: bool): |
| 14 | super().__init__() |
| 15 | self.branch = branch |
| 16 | self.b = b |
| 17 | |
| 18 | if self.b: |
| 19 | if self.branch in self.repo.heads: |
| 20 | print( |
| 21 | "git-sim error: can't create new branch '" |
| 22 | + self.branch |
| 23 | + "', it already exists" |
| 24 | ) |
| 25 | sys.exit(1) |
| 26 | else: |
| 27 | try: |
| 28 | git.repo.fun.rev_parse(self.repo, self.branch) |
| 29 | except git.exc.BadName: |
| 30 | print( |
| 31 | "git-sim error: '" |
| 32 | + self.branch |
| 33 | + "' is not a valid Git ref or identifier." |
| 34 | ) |
| 35 | sys.exit(1) |
| 36 | |
| 37 | if self.branch == self.repo.active_branch.name: |
| 38 | print("git-sim error: already on branch '" + self.branch + "'") |
| 39 | sys.exit(1) |
| 40 | |
| 41 | self.is_ancestor = False |
| 42 | self.is_descendant = False |
| 43 | |
| 44 | # branch being checked out is behind HEAD |
| 45 | if self.repo.active_branch.name in self.repo.git.branch( |
| 46 | "--contains", self.branch |
| 47 | ): |
| 48 | self.is_ancestor = True |
| 49 | # HEAD is behind branch being checked out |
| 50 | elif self.branch in self.repo.git.branch( |
| 51 | "--contains", self.repo.active_branch.name |
| 52 | ): |
| 53 | self.is_descendant = True |
| 54 | |
| 55 | if self.branch in [branch.name for branch in self.repo.heads]: |
| 56 | self.selected_branches.append(self.branch) |
| 57 | |
| 58 | try: |
| 59 | self.selected_branches.append(self.repo.active_branch.name) |
| 60 | except TypeError: |
| 61 | pass |
| 62 | |
| 63 | self.cmd += ( |
| 64 | f"{type(self).__name__.lower()}{' -b' if self.b else ''} {self.branch}" |
| 65 | ) |
| 66 | |
| 67 | def construct(self): |
| 68 | if not settings.stdout and not settings.output_only_path and not settings.quiet: |
| 69 | print(f"{settings.INFO_STRING} {self.cmd}") |