| 14 | |
| 15 | |
| 16 | class Fetch(GitSimBaseCommand): |
| 17 | def __init__(self, remote: str, branch: str): |
| 18 | super().__init__() |
| 19 | self.remote = remote |
| 20 | self.branch = branch |
| 21 | settings.max_branches_per_commit = 2 |
| 22 | |
| 23 | if self.remote and self.remote not in self.repo.remotes: |
| 24 | print("git-sim error: no remote with name '" + self.remote + "'") |
| 25 | sys.exit(1) |
| 26 | |
| 27 | self.cmd += f"{type(self).__name__.lower()} {self.remote if self.remote else ''} {self.branch if self.branch else ''}" |
| 28 | |
| 29 | def construct(self): |
| 30 | if not settings.stdout and not settings.output_only_path and not settings.quiet: |
| 31 | print(f"{settings.INFO_STRING} {self.cmd}") |
| 32 | |
| 33 | if not self.remote: |
| 34 | self.remote = "origin" |
| 35 | if not self.branch: |
| 36 | self.branch = self.repo.active_branch.name |
| 37 | |
| 38 | self.show_intro() |
| 39 | |
| 40 | git_root = self.repo.git.rev_parse("--show-toplevel") |
| 41 | repo_name = os.path.basename(self.repo.working_dir) |
| 42 | new_dir = os.path.join(tempfile.gettempdir(), "git_sim", repo_name) |
| 43 | |
| 44 | orig_remotes = self.repo.remotes |
| 45 | self.repo = git.Repo.clone_from(git_root, new_dir, no_hardlinks=True) |
| 46 | for r1 in orig_remotes: |
| 47 | for r2 in self.repo.remotes: |
| 48 | if r1.name == r2.name: |
| 49 | r2.set_url(r1.url) |
| 50 | |
| 51 | try: |
| 52 | self.repo.git.fetch(self.remote, self.branch) |
| 53 | except git.GitCommandError as e: |
| 54 | print(e) |
| 55 | sys.exit(1) |
| 56 | |
| 57 | # local branch doesn't exist |
| 58 | if self.branch not in self.repo.heads: |
| 59 | start_parse_from_remote = True |
| 60 | # fetched branch is ahead of local branch |
| 61 | elif (self.remote + "/" + self.branch) in self.repo.git.branch( |
| 62 | "-r", "--contains", self.branch |
| 63 | ): |
| 64 | start_parse_from_remote = True |
| 65 | # fetched branch is behind local branch |
| 66 | elif self.branch in self.repo.git.branch( |
| 67 | "--contains", (self.remote + "/" + self.branch) |
| 68 | ): |
| 69 | start_parse_from_remote = False |
| 70 | else: |
| 71 | start_parse_from_remote = True |
| 72 | |
| 73 | if start_parse_from_remote: |