Skip to content

Commit 5b1a97e

Browse files
committed
Fix branch updating and caching
1 parent 357007a commit 5b1a97e

1 file changed

Lines changed: 22 additions & 3 deletions

File tree

src/tmt_web/utils/git_handler.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
It uses tmt's Git utilities for robust clone operations with retry logic.
77
"""
88

9+
import hashlib
910
import re
1011
from shutil import rmtree
1112

@@ -20,15 +21,22 @@
2021
ROOT_DIR = Path(__file__).resolve().parents[2]
2122

2223

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+
2331
def get_unique_clone_path(url: str) -> Path:
2432
"""
2533
Generate a unique path for cloning a repository.
2634
2735
:param url: Repository URL
2836
:return: Unique path for cloning
2937
"""
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)
3240
return ROOT_DIR / settings.CLONE_DIR_PATH / clone_dir_name
3341

3442

@@ -136,7 +144,18 @@ def _get_default_branch(common: Common, repo_path: Path, logger: Logger) -> str:
136144
def _fetch_remote(common: Common, repo_path: Path, logger: Logger) -> None:
137145
"""Fetch updates from the remote repository."""
138146
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+
)
140159
except RunError as err:
141160
logger.fail(f"Failed to fetch remote for repository '{repo_path}'")
142161
raise GeneralError(f"Failed to fetch remote for repository '{repo_path}'") from err

0 commit comments

Comments
 (0)