From 1f03eb9102f765c36cc201a499d815732e67dd39 Mon Sep 17 00:00:00 2001 From: Ondrej Nosek Date: Mon, 27 Mar 2023 23:34:12 +0200 Subject: [PATCH 12/12] pre-push hook script contains a user's config When the `clone` command is called with an argument -C|--config this argument is placed to the generated pre-push script. Fixes: #667 JIRA: RHELCMP-11394 Signed-off-by: Ondrej Nosek --- pyrpkg/__init__.py | 23 ++++++++++++------- pyrpkg/cli.py | 6 +++-- tests/commands/test_clone.py | 44 ++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 10 deletions(-) diff --git a/pyrpkg/__init__.py b/pyrpkg/__init__.py index 15203b7..9996402 100644 --- a/pyrpkg/__init__.py +++ b/pyrpkg/__init__.py @@ -1566,7 +1566,8 @@ class Commands(object): return def clone(self, repo, path=None, branch=None, bare_dir=None, - anon=False, target=None, depth=None, extra_args=None): + anon=False, target=None, depth=None, extra_args=None, + config_path=None): """Clone a repo, optionally check out a specific branch. :param str repo: the name of the repository to clone. @@ -1583,6 +1584,7 @@ class Commands(object): to the specified number of commits. :param list extra_args: additional arguments that are passed to the clone command. + :param str config_path: path to the global config file """ if not path: @@ -1638,7 +1640,7 @@ class Commands(object): if not bare_dir: self._add_git_excludes(os.path.join(path, git_dir)) - self._add_git_pre_push_hook(os.path.join(path, git_dir)) + self._add_git_pre_push_hook(os.path.join(path, git_dir), config_path) return @@ -1654,7 +1656,7 @@ class Commands(object): return repo def clone_with_dirs(self, repo, anon=False, target=None, depth=None, - extra_args=None): + extra_args=None, config_path=None): """Clone a repo old style with subdirs for each branch. :param str repo: name of the repository to clone. @@ -1666,6 +1668,7 @@ class Commands(object): to the specified number of commits. :param list extra_args: additional arguments that are passed to the clone command. + :param str config_path: path to the global config file """ self._push_url = None @@ -1724,7 +1727,7 @@ class Commands(object): # Add excludes self._add_git_excludes(branch_path) - self._add_git_pre_push_hook(branch_path) + self._add_git_pre_push_hook(branch_path, config_path) except (git.GitCommandError, OSError) as e: raise rpkgError('Could not locally clone %s from %s: %s' % (branch, repo_path, e)) @@ -1787,7 +1790,7 @@ class Commands(object): git_excludes.write() self.log.debug('Git-excludes patterns were added into %s' % git_excludes_path) - def _add_git_pre_push_hook(self, conf_dir): + def _add_git_pre_push_hook(self, repo_dir, config_path=None): """ Create pre-push hook script and write it in the location: /.git/hooks/pre-push @@ -1803,6 +1806,10 @@ class Commands(object): self.log.debug('Pre-push hook script was NOT added - missing ' 'the packaging tool like fedpkg, rhpkg, ...') return + + # in case the clone command run with 'x-pkg -C clone ' + config_arg = ' -C "{0}"'.format(os.path.realpath(config_path)) if config_path else "" + hook_content = textwrap.dedent(""" #!/bin/bash @@ -1818,7 +1825,7 @@ class Commands(object): do command -v {0} >/dev/null 2>&1 || {{ echo >&2 "Warning: '{0}' is missing, \\ pre-push check is omitted. See .git/hooks/pre-push"; exit 0; }} - {0} pre-push-check "$local_sha" + {0}{1} pre-push-check "$local_sha" ret_code=$? if [ $ret_code -ne 0 ] && [ $exit_code -eq 0 ]; then exit_code=$ret_code @@ -1826,8 +1833,8 @@ class Commands(object): done exit $exit_code - """).strip().format(tool_name) - git_pre_push_hook_path = os.path.join(conf_dir, '.git/hooks/pre-push') + """).strip().format(tool_name, config_arg) + git_pre_push_hook_path = os.path.join(repo_dir, '.git/hooks/pre-push') if not os.path.exists(os.path.dirname(git_pre_push_hook_path)): # prepare ".git/hooks" directory if it is missing os.makedirs(os.path.dirname(git_pre_push_hook_path)) diff --git a/pyrpkg/cli.py b/pyrpkg/cli.py index 1bcf6e4..3d8ce33 100644 --- a/pyrpkg/cli.py +++ b/pyrpkg/cli.py @@ -2182,14 +2182,16 @@ class cliClient(object): anon=self.args.anonymous, target=self.args.clone_target, depth=self.args.depth, - extra_args=self.extra_args) + extra_args=self.extra_args, + config_path=self.args.config) else: self.cmd.clone(self.args.repo[0], branch=self.args.branch, anon=self.args.anonymous, target=self.args.clone_target, depth=self.args.depth, - extra_args=self.extra_args) + extra_args=self.extra_args, + config_path=self.args.config) def commit(self): if self.args.with_changelog and not self.args.message: diff --git a/tests/commands/test_clone.py b/tests/commands/test_clone.py index f741864..6ef1300 100644 --- a/tests/commands/test_clone.py +++ b/tests/commands/test_clone.py @@ -95,6 +95,50 @@ class CommandCloneTestCase(CommandTestCase): shutil.rmtree(altpath) + def test_clone_anonymous_pre_push_hook(self): + self.make_new_git(self.module) + + altpath = tempfile.mkdtemp(prefix='rpkg-tests.') + + cmd = pyrpkg.Commands(self.path, self.lookaside, self.lookasidehash, + self.lookaside_cgi, self.gitbaseurl, + self.anongiturl, self.branchre, self.kojiprofile, + self.build_client, self.user, self.dist, + self.target, self.quiet) + cmd.clone(self.module, anon=True, config_path=None) + + moduledir = os.path.join(self.path, self.module) + self.assertTrue(os.path.isfile(os.path.join(moduledir, '.git/hooks/pre-push'))) + + with open(os.path.join(moduledir, '.git/hooks/pre-push')) as git_hook_script: + content = git_hook_script.read() + pattern = '__main__.py pre-push-check "$local_sha"' + self.assertIn(pattern, content) + + shutil.rmtree(altpath) + + def test_clone_anonymous_pre_push_hook_config(self): + self.make_new_git(self.module) + + altpath = tempfile.mkdtemp(prefix='rpkg-tests.') + + cmd = pyrpkg.Commands(self.path, self.lookaside, self.lookasidehash, + self.lookaside_cgi, self.gitbaseurl, + self.anongiturl, self.branchre, self.kojiprofile, + self.build_client, self.user, self.dist, + self.target, self.quiet) + cmd.clone(self.module, anon=True, config_path="/home/conf/rhpkg.conf") + + moduledir = os.path.join(self.path, self.module) + self.assertTrue(os.path.isfile(os.path.join(moduledir, '.git/hooks/pre-push'))) + + with open(os.path.join(moduledir, '.git/hooks/pre-push')) as git_hook_script: + content = git_hook_script.read() + pattern = '__main__.py -C "/home/conf/rhpkg.conf" pre-push-check "$local_sha"' + self.assertIn(pattern, content) + + shutil.rmtree(altpath) + def test_clone_anonymous_with_branch(self): self.make_new_git(self.module, branches=['rpkg-tests-1', 'rpkg-tests-2']) -- 2.39.2