Skip to content

Commit 5c5046a

Browse files
authored
Merge pull request #285 from jakub-nt/CFE-4089
Code quality improvements in preparation for CFE-4089
2 parents 143950f + a718040 commit 5c5046a

File tree

5 files changed

+29
-23
lines changed

5 files changed

+29
-23
lines changed

cfbs/cfbs_config.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from cfbs.utils import (
2626
CFBSExitError,
2727
CFBSUserError,
28+
is_a_commit_hash,
2829
read_file,
2930
write_json,
3031
load_bundlenames,
@@ -149,16 +150,25 @@ def _add_using_url(
149150
checksum=None,
150151
explicit_build_steps=None,
151152
):
153+
"""The `url` argument can optionally also have the form `<url>@<commit>`."""
152154
url_commit = None
153155
if url.endswith(SUPPORTED_ARCHIVES):
154156
config_path, url_commit = fetch_archive(url, checksum)
155157
else:
156158
assert url.startswith(SUPPORTED_URI_SCHEMES)
157-
config_path, url_commit = clone_url_repo(url)
158159

159-
if "@" in url and (url.rindex("@") > url.rindex(".")):
160-
assert url.split("@")[-1] == url_commit
161-
url = url[0 : url.rindex("@")]
160+
commit = None
161+
if "@" in url and (url.rindex("@") > url.rindex(".")):
162+
# commit specified in the url
163+
url, commit = url.rsplit("@", 1)
164+
if "@" in url and (url.rindex("@") > url.rindex(".")):
165+
raise CFBSUserError(
166+
"Cannot specify more than one commit for one add URL"
167+
)
168+
if not is_a_commit_hash(commit):
169+
raise CFBSExitError("'%s' is not a commit reference" % commit)
170+
171+
config_path, url_commit = clone_url_repo(url, commit)
162172

163173
remote_config = CFBSJson(path=config_path, url=url, url_commit=url_commit)
164174

cfbs/commands.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,6 @@ def init_command(
262262
"""
263263
CFBSConfig.reload()
264264

265-
branch = None
266265
to_add = []
267266
if masterfiles is None:
268267
if prompt_user_yesno(
@@ -282,16 +281,8 @@ def init_command(
282281
log.debug("--masterfiles=%s appears to be a version number" % masterfiles)
283282
to_add = ["masterfiles@%s" % masterfiles]
284283
elif masterfiles != "no":
285-
"""This appears to be a branch. Thus we'll add masterfiles normally
286-
and try to do the necessary modifications needed afterwards. I.e.
287-
changing the 'repo' attribute to be 'url' and changing the commit to
288-
be the current HEAD of the upstream branch."""
289-
290284
log.debug("--masterfiles=%s appears to be a branch" % masterfiles)
291285
branch = masterfiles
292-
to_add = ["masterfiles"]
293-
294-
if branch is not None:
295286
remote = "https://github.com/cfengine/masterfiles"
296287
commit = ls_remote(remote, branch)
297288
if commit is None:

cfbs/git.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def git_exists():
3333
def ls_remote(remote, branch):
3434
"""Returns the hash of the commit that the current HEAD of a given branch
3535
on a given remote is pointing to.
36+
Returns `None` in case of error (e.g. the branch does not exist).
3637
3738
:param remote: the remote to list
3839
:param branch: the branch on the remote

cfbs/internal_file_management.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import os
1212
import re
1313
import shutil
14+
from typing import Optional
1415

1516
from cfbs.utils import (
1617
cfbs_dir,
@@ -148,22 +149,20 @@ def _get_git_repo_commit_sha(repo_path):
148149
return f.read().strip()
149150

150151

151-
def _clone_and_checkout(url, path, commit):
152+
def _clone_and_checkout(url, path, treeish):
152153
# NOTE: If any of these shell (git) commands fail, we will exit
153154
if not os.path.exists(os.path.join(path, ".git")):
154155
sh("git clone --no-checkout %s %s" % (url, path))
155-
sh("git checkout " + commit, directory=path)
156+
sh("git checkout " + treeish, directory=path)
156157

157158

158-
def clone_url_repo(repo_url: str):
159-
assert repo_url.startswith(SUPPORTED_URI_SCHEMES)
159+
def clone_url_repo(repo_url: str, commit: Optional[str] = None):
160+
"""Clones a Git repository at `repo_url` URL, optionally checking out the `commit` commit.
160161
161-
commit = None
162-
if "@" in repo_url and (repo_url.rindex("@") > repo_url.rindex(".")):
163-
# commit specified in the url
164-
repo_url, commit = repo_url.rsplit("@", 1)
165-
if not is_a_commit_hash(commit):
166-
raise CFBSExitError("'%s' is not a commit reference" % commit)
162+
Returns path to the `cfbs.json` located in the cloned Git repository, and the Git commit hash.
163+
"""
164+
assert repo_url.startswith(SUPPORTED_URI_SCHEMES)
165+
assert "@" not in repo_url or (repo_url.rindex("@") < repo_url.rindex("."))
167166

168167
downloads = os.path.join(cfbs_dir(), "downloads")
169168

cfbs/module.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from collections import OrderedDict
22

33
from cfbs.pretty import pretty
4+
from cfbs.utils import CFBSUserError
45

56

67
def is_module_added_manually(added_by: str):
@@ -24,6 +25,10 @@ def __init__(
2425
"""Initialize from argument with format `NAME[@VERSION]`"""
2526
assert isinstance(arg, str)
2627
if "@" in arg:
28+
if arg.count("@") > 1:
29+
raise CFBSUserError(
30+
"Cannot specify more than one version of the same module"
31+
)
2732
self.name, self.version = arg.split("@")
2833
else:
2934
self.name = arg

0 commit comments

Comments
 (0)