| 15 | |
| 16 | |
| 17 | class Clone(GitSimBaseCommand): |
| 18 | # Override since 'clone' subcommand shouldn't require repo to exist |
| 19 | def init_repo(self): |
| 20 | pass |
| 21 | |
| 22 | def __init__(self, url: str, path: str): |
| 23 | super().__init__() |
| 24 | self.url = url |
| 25 | self.path = path |
| 26 | settings.max_branches_per_commit = 2 |
| 27 | self.cmd += f"{type(self).__name__.lower()} {self.url + ('' if self.path == '.' else ' ' + self.path)}" |
| 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 | self.show_intro() |
| 34 | |
| 35 | # Configure paths to make local clone to run networked commands in |
| 36 | repo_name = re.search(r"/([^/]+)/?$", self.url) |
| 37 | if repo_name: |
| 38 | repo_name = repo_name.group(1) |
| 39 | if repo_name.endswith(".git"): |
| 40 | repo_name = repo_name[:-4] |
| 41 | elif self.url == "." or self.url == "./" or self.url == ".\\": |
| 42 | repo_name = os.path.split(os.getcwd())[1] |
| 43 | else: |
| 44 | print( |
| 45 | f"git-sim error: Invalid repo URL, please confirm repo URL and try again" |
| 46 | ) |
| 47 | sys.exit(1) |
| 48 | |
| 49 | if self.url == os.path.join(self.path, repo_name): |
| 50 | print(f"git-sim error: Cannot clone into same path, please try again") |
| 51 | sys.exit(1) |
| 52 | new_dir = os.path.join(tempfile.gettempdir(), "git_sim", repo_name) |
| 53 | |
| 54 | # Create local clone of local repo |
| 55 | try: |
| 56 | self.repo = git.Repo.clone_from(self.url, new_dir, no_hardlinks=True) |
| 57 | except git.GitCommandError as e: |
| 58 | print( |
| 59 | f"git-sim error: Invalid repo URL, please confirm repo URL and try again" |
| 60 | ) |
| 61 | sys.exit(1) |
| 62 | |
| 63 | head_commit = self.get_commit() |
| 64 | self.parse_commits(head_commit) |
| 65 | self.recenter_frame() |
| 66 | self.scale_frame() |
| 67 | self.add_details(repo_name) |
| 68 | self.color_by() |
| 69 | self.show_command_as_title() |
| 70 | self.fadeout() |
| 71 | self.show_outro() |
| 72 | |
| 73 | # Unlink the program from the filesystem |
| 74 | self.repo.git.clear_cache() |