From d1c69fbc0bf830ee57f67a2582d4844b78b846f1 Mon Sep 17 00:00:00 2001 From: Lars Erik Wik Date: Tue, 25 Oct 2022 16:38:00 +0200 Subject: [PATCH 1/4] Add utility function `ls_remote` Add utility function `ls_remote` which can be used to check which commit the HEAD of a given branch is pointing to from a given remote. Ticket: None Changelog: None Signed-off-by: Lars Erik Wik --- cfbs/git.py | 18 ++++++++++++++++++ tests/test_git.py | 9 +++++++++ 2 files changed, 27 insertions(+) create mode 100644 tests/test_git.py diff --git a/cfbs/git.py b/cfbs/git.py index eaa9ffbc..a2a0fe56 100644 --- a/cfbs/git.py +++ b/cfbs/git.py @@ -17,6 +17,24 @@ class CFBSGitError(Exception): pass +def ls_remote(remote, branch): + """Returns the hash of the commit that the current HEAD of a given branch + on a given remote is pointing to. + + :param remote: the remote to list + :param branch: the branch on the remote + """ + try: + return ( + check_output(["git", "ls-remote", remote, branch]) + .decode() + .strip() + .split()[0] + ) + except: + return None + + def is_git_repo(path=None): """Is the given path a Git repository?) diff --git a/tests/test_git.py b/tests/test_git.py new file mode 100644 index 00000000..4c416bdd --- /dev/null +++ b/tests/test_git.py @@ -0,0 +1,9 @@ +from cfbs.git import ls_remote +from cfbs.utils import is_a_commit_hash + + +def test_ls_remote(): + commit = ls_remote("https://github.com/cfengine/masterfiles.git", "master") + print(commit) + assert commit != None + assert is_a_commit_hash(commit) From 2c33147caa5084ba620b78fde992f8fd8cc66cd2 Mon Sep 17 00:00:00 2001 From: Lars Erik Wik Date: Tue, 25 Oct 2022 16:39:54 +0200 Subject: [PATCH 2/4] Add --masterfiles option for cfbs init You now can specify version of masterfiles in cfbs init using the --masterfiles option. The --masterfiles option can be set to `no`, resulting in no masterfiles added. It can be set to a version number matching regex `[0-9]+(\.[0-9]+){2}(\-[0-9]+)?` which adds a specific version of masterfiles. Else the argument is interpreted as a branch name, resulting in cfbs trying to add masterfiles using that branch. Ticket: CFE-4071 Changelog: Title Signed-off-by: Lars Erik Wik --- cfbs/args.py | 3 +++ cfbs/commands.py | 59 ++++++++++++++++++++++++++++++++++++------------ cfbs/main.py | 9 +++++++- 3 files changed, 56 insertions(+), 15 deletions(-) diff --git a/cfbs/args.py b/cfbs/args.py index 10fba19b..56a9c887 100644 --- a/cfbs/args.py +++ b/cfbs/args.py @@ -84,4 +84,7 @@ def _get_arg_parser(): help="Ignore versions.json. Necessary in case of a custom index or testing changes to the default index.", action="store_true", ) + parser.add_argument( + "--masterfiles", help="Add masterfiles on cfbs init choose between" + ) return parser diff --git a/cfbs/commands.py b/cfbs/commands.py index 15574628..72890dc0 100644 --- a/cfbs/commands.py +++ b/cfbs/commands.py @@ -44,6 +44,7 @@ git_set_config, git_init, CFBSGitError, + ls_remote, ) from cfbs.git_magic import Result, commit_after_command, git_commit_maybe_prompt from cfbs.prompts import YES_NO_CHOICES, prompt_user @@ -123,7 +124,7 @@ def pretty_command(filenames: list, check: bool, keep_order: bool) -> int: return 0 -def init_command(index=None, non_interactive=False) -> int: +def init_command(index=None, masterfiles=None, non_interactive=False) -> int: if is_cfbs_repo(): user_error("Already initialized - look at %s" % cfbs_filename()) @@ -228,22 +229,52 @@ def init_command(index=None, non_interactive=False) -> int: """ CFBSConfig.reload() - if prompt_user( - non_interactive, - "Do you wish to build on top of the default policy set, masterfiles? (Recommended)", - choices=YES_NO_CHOICES, - default="yes", - ) in ("yes", "y"): - to_add = "masterfiles" - else: - to_add = prompt_user( + branch = None + to_add = "" + if masterfiles is None: + if prompt_user( non_interactive, - "Specify policy set to use instead (empty to skip)", - default="", - ) + "Do you wish to build on top of the default policy set, masterfiles? (Recommended)", + choices=YES_NO_CHOICES, + default="yes", + ) in ("yes", "y"): + to_add = "masterfiles" + else: + to_add = prompt_user( + non_interactive, + "Specify policy set to use instead (empty to skip)", + default="", + ) + elif re.match(r"[0-9]+(\.[0-9]+){2}(\-[0-9]+)?", masterfiles): + log.debug("--masterfiles=%s appears to be a version number" % masterfiles) + to_add = "masterfiles@%s" % masterfiles + elif masterfiles != "no": + """This appears to be a branch. Thus we'll add masterfiles normally + and try to do the necessary modifications needed afterwards. I.e. + changing the 'repo' attribute to be 'url' and changing the commit to + be the current HEAD of the upstream branch.""" + + log.debug("--masterfiles=%s appears to be a branch" % masterfiles) + branch = masterfiles + to_add = "masterfiles" if to_add: - return add_command([to_add]) + ret = add_command([to_add]) + if ret != 0: + return ret + + if branch is not None: + config = CFBSConfig.get_instance() + module = config.get_module_from_build("masterfiles") + remote = module["repo"] + commit = ls_remote(remote, branch) + if commit is None: + user_error("Failed to add masterfiles from branch %s" % branch) + log.debug("Current commit for masterfiles branch %s is %s" % (branch, commit)) + module["url"] = remote + del module["repo"] + module["commit"] = commit + config.save() return 0 diff --git a/cfbs/main.py b/cfbs/main.py index 89a37b06..ee5761a7 100644 --- a/cfbs/main.py +++ b/cfbs/main.py @@ -45,6 +45,11 @@ def main() -> int: print("") user_error("No command given") + if args.masterfiles and args.command != "init": + user_error( + "The option --masterfiles is only for cfbs init, not cfbs %s" % args.command + ) + if args.non_interactive and args.command not in ( "init", "add", @@ -75,7 +80,9 @@ def main() -> int: if args.command == "init": return commands.init_command( - index=args.index, non_interactive=args.non_interactive + index=args.index, + masterfiles=args.masterfiles, + non_interactive=args.non_interactive, ) if args.command == "search": From ca8ab4fd70d2ec1a1a26c2260af1723c70248c74 Mon Sep 17 00:00:00 2001 From: Lars Erik Wik Date: Tue, 25 Oct 2022 17:12:50 +0200 Subject: [PATCH 3/4] Fix bug when building empty project Fix bug where exception is raised when building empty project. Ticket: None Changelog: None Signed-off-by: Lars Erik Wik --- cfbs/cfbs_config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cfbs/cfbs_config.py b/cfbs/cfbs_config.py index e0143c17..b68ff734 100644 --- a/cfbs/cfbs_config.py +++ b/cfbs/cfbs_config.py @@ -81,7 +81,7 @@ def save(self): f.write(data) def longest_module_name(self) -> int: - return max((len(m["name"]) for m in self["build"])) + return max((len(m["name"]) for m in self["build"])) if self["build"] else 0 def add_with_dependencies(self, module, remote_config=None, dependent=None): if type(module) is list: From 9ec38d6cc7c9f0527492b5395ffc8c7997ae0e45 Mon Sep 17 00:00:00 2001 From: Lars Erik Wik Date: Tue, 25 Oct 2022 17:14:37 +0200 Subject: [PATCH 4/4] Added acceptance test for --masterfiles option Added acceptance test for testing the --masterfiles option with cfbs init. Ticket: CFE-4071 Changelog: None Signed-off-by: Lars Erik Wik --- tests/shell/026_init_no_masterfiles.sh | 11 +++++++++++ tests/shell/027_init_masterfiles_version_master.sh | 12 ++++++++++++ tests/shell/028_init_masterfiles_version_3.18.2.sh | 12 ++++++++++++ tests/shell/029_init_masterfiles_version_3.18.1-1.sh | 12 ++++++++++++ tests/shell/all.sh | 4 ++++ 5 files changed, 51 insertions(+) create mode 100644 tests/shell/026_init_no_masterfiles.sh create mode 100644 tests/shell/027_init_masterfiles_version_master.sh create mode 100644 tests/shell/028_init_masterfiles_version_3.18.2.sh create mode 100644 tests/shell/029_init_masterfiles_version_3.18.1-1.sh diff --git a/tests/shell/026_init_no_masterfiles.sh b/tests/shell/026_init_no_masterfiles.sh new file mode 100644 index 00000000..ad92c5d8 --- /dev/null +++ b/tests/shell/026_init_no_masterfiles.sh @@ -0,0 +1,11 @@ +set -e +set -x +cd tests/ +mkdir -p ./tmp/ +cd ./tmp/ +touch cfbs.json && rm cfbs.json +rm -rf .git + +cfbs --non-interactive init --masterfiles=no +!( grep '"name": "masterfiles"' cfbs.json ) +cfbs build diff --git a/tests/shell/027_init_masterfiles_version_master.sh b/tests/shell/027_init_masterfiles_version_master.sh new file mode 100644 index 00000000..69106983 --- /dev/null +++ b/tests/shell/027_init_masterfiles_version_master.sh @@ -0,0 +1,12 @@ +set -e +set -x +cd tests/ +mkdir -p ./tmp/ +cd ./tmp/ +touch cfbs.json && rm cfbs.json +rm -rf .git + +cfbs --non-interactive init --masterfiles=master +grep '"name": "masterfiles"' cfbs.json +grep '"url": "https://github.com/cfengine/masterfiles"' cfbs.json +cfbs build diff --git a/tests/shell/028_init_masterfiles_version_3.18.2.sh b/tests/shell/028_init_masterfiles_version_3.18.2.sh new file mode 100644 index 00000000..f5c612dc --- /dev/null +++ b/tests/shell/028_init_masterfiles_version_3.18.2.sh @@ -0,0 +1,12 @@ +set -e +set -x +cd tests/ +mkdir -p ./tmp/ +cd ./tmp/ +touch cfbs.json && rm cfbs.json +rm -rf .git + +cfbs --non-interactive init --masterfiles=3.18.2 +grep '"name": "masterfiles"' cfbs.json +grep '"version": "3.18.2"' cfbs.json +cfbs build diff --git a/tests/shell/029_init_masterfiles_version_3.18.1-1.sh b/tests/shell/029_init_masterfiles_version_3.18.1-1.sh new file mode 100644 index 00000000..1eb4ec18 --- /dev/null +++ b/tests/shell/029_init_masterfiles_version_3.18.1-1.sh @@ -0,0 +1,12 @@ +set -e +set -x +cd tests/ +mkdir -p ./tmp/ +cd ./tmp/ +touch cfbs.json && rm cfbs.json +rm -rf .git + +cfbs --non-interactive init --masterfiles=3.18.1-1 +grep '"name": "masterfiles"' cfbs.json +grep '"version": "3.18.1-1"' cfbs.json +cfbs build diff --git a/tests/shell/all.sh b/tests/shell/all.sh index d6f9171c..648eb847 100644 --- a/tests/shell/all.sh +++ b/tests/shell/all.sh @@ -28,3 +28,7 @@ bash tests/shell/022_update_input_fail_variable.sh bash tests/shell/023_update_input_fail_number.sh bash tests/shell/024_update_input_fail_bundle.sh bash tests/shell/025_add_input_remove.sh +bash tests/shell/026_init_no_masterfiles.sh +bash tests/shell/027_init_masterfiles_version_master.sh +bash tests/shell/028_init_masterfiles_version_3.18.2.sh +bash tests/shell/029_init_masterfiles_version_3.18.1-1.sh