Skip to content

Commit ebdf0b7

Browse files
committed
Added error handling to prevent checking out non-existent references
Signed-off-by: jakub-nt <175944085+jakub-nt@users.noreply.github.com>
1 parent b37ed44 commit ebdf0b7

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

cfbs/git.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,16 @@ def git_check_tracked_changes(scope=["all"]):
249249
raise CFBSGitError(
250250
"Failed to run 'git status -s -u' to check for changes."
251251
) from cpe
252+
253+
254+
def treeish_exists(treeish, repo_path):
255+
command = [
256+
"git",
257+
"rev-parse",
258+
"--verify",
259+
"--end-of-options",
260+
treeish + r"^{object}",
261+
]
262+
result = run(command, cwd=repo_path, stdout=DEVNULL, stderr=DEVNULL, check=False)
263+
264+
return result.returncode == 0

cfbs/internal_file_management.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import shutil
1414
from typing import Optional
1515

16-
from cfbs.git import ls_remote
16+
from cfbs.git import ls_remote, treeish_exists
1717
from cfbs.utils import (
1818
cfbs_dir,
1919
cp,
@@ -141,6 +141,9 @@ def _clone_and_checkout(url, path, treeish):
141141
# NOTE: If any of these shell (git) commands fail, we will exit
142142
if not os.path.exists(os.path.join(path, ".git")):
143143
sh("git clone --no-checkout %s %s" % (url, path))
144+
if not treeish_exists(treeish, path):
145+
raise CFBSExitError("%s not found in %s" % (treeish, url))
146+
144147
sh("git checkout " + treeish, directory=path)
145148

146149

0 commit comments

Comments
 (0)