| 95 | |
| 96 | |
| 97 | class GitSourceManager: |
| 98 | def __init__(self, repo: Path) -> None: |
| 99 | self.repo: Path = repo |
| 100 | |
| 101 | # Will be set by derived classes but used here |
| 102 | self._pretty_name: str = '' |
| 103 | self._repo_url: str = '' |
| 104 | |
| 105 | def download(self, ref: str, shallow: bool = False) -> None: |
| 106 | if self.repo.exists(): |
| 107 | return |
| 108 | |
| 109 | tc_build.utils.print_header(f"Downloading {self._pretty_name}") |
| 110 | |
| 111 | git_clone: tc_build.utils.CmdList = ['git', 'clone'] |
| 112 | if shallow: |
| 113 | git_clone.append('--depth=1') |
| 114 | if ref != 'main': |
| 115 | git_clone.append(f"--branch={ref}") |
| 116 | git_clone += [self._repo_url, self.repo] |
| 117 | |
| 118 | subprocess.run(git_clone, check=True) |
| 119 | |
| 120 | self.git(['checkout', ref]) |
| 121 | |
| 122 | def git( |
| 123 | self, cmd: tc_build.utils.CmdList, capture_output: bool = False |
| 124 | ) -> subprocess.CompletedProcess: |
| 125 | return subprocess.run( |
| 126 | ['git', *cmd], capture_output=capture_output, check=True, cwd=self.repo, text=True |
| 127 | ) |
| 128 | |
| 129 | def git_capture(self, cmd: tc_build.utils.CmdList) -> str: |
| 130 | return self.git(cmd, capture_output=True).stdout.strip() |
| 131 | |
| 132 | def is_shallow(self) -> bool: |
| 133 | git_dir = self.git_capture(['rev-parse', '--git-dir']) |
| 134 | return Path(git_dir, 'shallow').exists() |
| 135 | |
| 136 | def ref_exists(self, ref: str) -> bool: |
| 137 | try: |
| 138 | self.git(['show-branch', ref]) |
| 139 | except subprocess.CalledProcessError: |
| 140 | return False |
| 141 | return True |
| 142 | |
| 143 | def update(self, ref: str) -> None: |
| 144 | tc_build.utils.print_header(f"Updating {self._pretty_name}") |
| 145 | |
| 146 | self.git(['fetch', 'origin']) |
| 147 | |
| 148 | if self.is_shallow() and not self.ref_exists(ref): |
| 149 | msg = f"Repo is shallow and supplied ref ('{ref}') does not exist!" |
| 150 | raise RuntimeError(msg) |
| 151 | |
| 152 | self.git(['checkout', ref]) |
| 153 | |
| 154 | local_ref = None |
nothing calls this directly
no outgoing calls
no test coverage detected