1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
From 1f03eb9102f765c36cc201a499d815732e67dd39 Mon Sep 17 00:00:00 2001
From: Ondrej Nosek <onosek@redhat.com>
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 <config_file>
this argument is placed to the generated pre-push script.
Fixes: #667
JIRA: RHELCMP-11394
Signed-off-by: Ondrej Nosek <onosek@redhat.com>
---
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:
<repository_directory>/.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 <config_path> clone <repo_name>'
+ 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
|