|
6 | 6 | It uses tmt's Git utilities for robust clone operations with retry logic. |
7 | 7 | """ |
8 | 8 |
|
| 9 | +import hashlib |
9 | 10 | import re |
10 | 11 | from shutil import rmtree |
11 | 12 |
|
|
20 | 21 | ROOT_DIR = Path(__file__).resolve().parents[2] |
21 | 22 |
|
22 | 23 |
|
| 24 | +def create_hash(text: str): |
| 25 | + """Create SHA-1 hash of the given text that is consistent across runs.""" |
| 26 | + hashed_url = hashlib.new("sha1", usedforsecurity=False) |
| 27 | + hashed_url.update(text.encode()) |
| 28 | + return hashed_url.hexdigest() |
| 29 | + |
| 30 | + |
23 | 31 | def get_unique_clone_path(url: str) -> Path: |
24 | 32 | """ |
25 | 33 | Generate a unique path for cloning a repository. |
26 | 34 |
|
27 | 35 | :param url: Repository URL |
28 | 36 | :return: Unique path for cloning |
29 | 37 | """ |
30 | | - url = url.rstrip("/") |
31 | | - clone_dir_name = str(abs(hash(url))) |
| 38 | + url = url.rstrip("/").removesuffix(".git") |
| 39 | + clone_dir_name = create_hash(url) |
32 | 40 | return ROOT_DIR / settings.CLONE_DIR_PATH / clone_dir_name |
33 | 41 |
|
34 | 42 |
|
@@ -136,7 +144,18 @@ def _get_default_branch(common: Common, repo_path: Path, logger: Logger) -> str: |
136 | 144 | def _fetch_remote(common: Common, repo_path: Path, logger: Logger) -> None: |
137 | 145 | """Fetch updates from the remote repository.""" |
138 | 146 | try: |
139 | | - common.run(Command("git", "fetch"), cwd=repo_path) |
| 147 | + common.run( |
| 148 | + Command( |
| 149 | + "git", |
| 150 | + "fetch", |
| 151 | + "origin", |
| 152 | + "--prune", |
| 153 | + "--prune-tags", |
| 154 | + "+refs/heads/*:refs/remotes/origin/*", |
| 155 | + "+refs/tags/*:refs/tags/*", |
| 156 | + ), |
| 157 | + cwd=repo_path, |
| 158 | + ) |
140 | 159 | except RunError as err: |
141 | 160 | logger.fail(f"Failed to fetch remote for repository '{repo_path}'") |
142 | 161 | raise GeneralError(f"Failed to fetch remote for repository '{repo_path}'") from err |
|
0 commit comments