Show More
@@ -1,390 +1,390 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """ |
|
2 | """ | |
3 | rhodecode.lib.hooks |
|
3 | rhodecode.lib.hooks | |
4 | ~~~~~~~~~~~~~~~~~~~ |
|
4 | ~~~~~~~~~~~~~~~~~~~ | |
5 |
|
5 | |||
6 | Hooks runned by rhodecode |
|
6 | Hooks runned by rhodecode | |
7 |
|
7 | |||
8 | :created_on: Aug 6, 2010 |
|
8 | :created_on: Aug 6, 2010 | |
9 | :author: marcink |
|
9 | :author: marcink | |
10 | :copyright: (C) 2010-2012 Marcin Kuzminski <marcin@python-works.com> |
|
10 | :copyright: (C) 2010-2012 Marcin Kuzminski <marcin@python-works.com> | |
11 | :license: GPLv3, see COPYING for more details. |
|
11 | :license: GPLv3, see COPYING for more details. | |
12 | """ |
|
12 | """ | |
13 | # This program is free software: you can redistribute it and/or modify |
|
13 | # This program is free software: you can redistribute it and/or modify | |
14 | # it under the terms of the GNU General Public License as published by |
|
14 | # it under the terms of the GNU General Public License as published by | |
15 | # the Free Software Foundation, either version 3 of the License, or |
|
15 | # the Free Software Foundation, either version 3 of the License, or | |
16 | # (at your option) any later version. |
|
16 | # (at your option) any later version. | |
17 | # |
|
17 | # | |
18 | # This program is distributed in the hope that it will be useful, |
|
18 | # This program is distributed in the hope that it will be useful, | |
19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | # GNU General Public License for more details. |
|
21 | # GNU General Public License for more details. | |
22 | # |
|
22 | # | |
23 | # You should have received a copy of the GNU General Public License |
|
23 | # You should have received a copy of the GNU General Public License | |
24 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
24 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
25 | import os |
|
25 | import os | |
26 | import sys |
|
26 | import sys | |
27 | import time |
|
27 | import time | |
28 | import binascii |
|
28 | import binascii | |
29 | import traceback |
|
29 | import traceback | |
30 | from inspect import isfunction |
|
30 | from inspect import isfunction | |
31 |
|
31 | |||
32 | from mercurial.scmutil import revrange |
|
32 | from mercurial.scmutil import revrange | |
33 | from mercurial.node import nullrev |
|
33 | from mercurial.node import nullrev | |
34 |
|
34 | |||
35 | from rhodecode.lib import helpers as h |
|
35 | from rhodecode.lib import helpers as h | |
36 | from rhodecode.lib.utils import action_logger |
|
36 | from rhodecode.lib.utils import action_logger | |
37 | from rhodecode.lib.vcs.backends.base import EmptyChangeset |
|
37 | from rhodecode.lib.vcs.backends.base import EmptyChangeset | |
38 | from rhodecode.lib.compat import json |
|
38 | from rhodecode.lib.compat import json | |
39 | from rhodecode.lib.exceptions import HTTPLockedRC |
|
39 | from rhodecode.lib.exceptions import HTTPLockedRC | |
40 | from rhodecode.lib.utils2 import safe_str, _extract_extras |
|
40 | from rhodecode.lib.utils2 import safe_str, _extract_extras | |
41 | from rhodecode.model.db import Repository, User |
|
41 | from rhodecode.model.db import Repository, User | |
42 |
|
42 | |||
43 |
|
43 | |||
44 | def _get_scm_size(alias, root_path): |
|
44 | def _get_scm_size(alias, root_path): | |
45 |
|
45 | |||
46 | if not alias.startswith('.'): |
|
46 | if not alias.startswith('.'): | |
47 | alias += '.' |
|
47 | alias += '.' | |
48 |
|
48 | |||
49 | size_scm, size_root = 0, 0 |
|
49 | size_scm, size_root = 0, 0 | |
50 | for path, dirs, files in os.walk(safe_str(root_path)): |
|
50 | for path, dirs, files in os.walk(safe_str(root_path)): | |
51 | if path.find(alias) != -1: |
|
51 | if path.find(alias) != -1: | |
52 | for f in files: |
|
52 | for f in files: | |
53 | try: |
|
53 | try: | |
54 | size_scm += os.path.getsize(os.path.join(path, f)) |
|
54 | size_scm += os.path.getsize(os.path.join(path, f)) | |
55 | except OSError: |
|
55 | except OSError: | |
56 | pass |
|
56 | pass | |
57 | else: |
|
57 | else: | |
58 | for f in files: |
|
58 | for f in files: | |
59 | try: |
|
59 | try: | |
60 | size_root += os.path.getsize(os.path.join(path, f)) |
|
60 | size_root += os.path.getsize(os.path.join(path, f)) | |
61 | except OSError: |
|
61 | except OSError: | |
62 | pass |
|
62 | pass | |
63 |
|
63 | |||
64 | size_scm_f = h.format_byte_size(size_scm) |
|
64 | size_scm_f = h.format_byte_size(size_scm) | |
65 | size_root_f = h.format_byte_size(size_root) |
|
65 | size_root_f = h.format_byte_size(size_root) | |
66 | size_total_f = h.format_byte_size(size_root + size_scm) |
|
66 | size_total_f = h.format_byte_size(size_root + size_scm) | |
67 |
|
67 | |||
68 | return size_scm_f, size_root_f, size_total_f |
|
68 | return size_scm_f, size_root_f, size_total_f | |
69 |
|
69 | |||
70 |
|
70 | |||
71 | def repo_size(ui, repo, hooktype=None, **kwargs): |
|
71 | def repo_size(ui, repo, hooktype=None, **kwargs): | |
72 | """ |
|
72 | """ | |
73 | Presents size of repository after push |
|
73 | Presents size of repository after push | |
74 |
|
74 | |||
75 | :param ui: |
|
75 | :param ui: | |
76 | :param repo: |
|
76 | :param repo: | |
77 | :param hooktype: |
|
77 | :param hooktype: | |
78 | """ |
|
78 | """ | |
79 |
|
79 | |||
80 | size_hg_f, size_root_f, size_total_f = _get_scm_size('.hg', repo.root) |
|
80 | size_hg_f, size_root_f, size_total_f = _get_scm_size('.hg', repo.root) | |
81 |
|
81 | |||
82 | last_cs = repo[len(repo) - 1] |
|
82 | last_cs = repo[len(repo) - 1] | |
83 |
|
83 | |||
84 | msg = ('Repository size .hg:%s repo:%s total:%s\n' |
|
84 | msg = ('Repository size .hg:%s repo:%s total:%s\n' | |
85 | 'Last revision is now r%s:%s\n') % ( |
|
85 | 'Last revision is now r%s:%s\n') % ( | |
86 | size_hg_f, size_root_f, size_total_f, last_cs.rev(), last_cs.hex()[:12] |
|
86 | size_hg_f, size_root_f, size_total_f, last_cs.rev(), last_cs.hex()[:12] | |
87 | ) |
|
87 | ) | |
88 |
|
88 | |||
89 | sys.stdout.write(msg) |
|
89 | sys.stdout.write(msg) | |
90 |
|
90 | |||
91 |
|
91 | |||
92 | def pre_push(ui, repo, **kwargs): |
|
92 | def pre_push(ui, repo, **kwargs): | |
93 | # pre push function, currently used to ban pushing when |
|
93 | # pre push function, currently used to ban pushing when | |
94 | # repository is locked |
|
94 | # repository is locked | |
95 | ex = _extract_extras() |
|
95 | ex = _extract_extras() | |
96 |
|
96 | |||
97 | usr = User.get_by_username(ex.username) |
|
97 | usr = User.get_by_username(ex.username) | |
98 | if ex.locked_by[0] and usr.user_id != int(ex.locked_by[0]): |
|
98 | if ex.locked_by[0] and usr.user_id != int(ex.locked_by[0]): | |
99 | locked_by = User.get(ex.locked_by[0]).username |
|
99 | locked_by = User.get(ex.locked_by[0]).username | |
100 | # this exception is interpreted in git/hg middlewares and based |
|
100 | # this exception is interpreted in git/hg middlewares and based | |
101 | # on that proper return code is server to client |
|
101 | # on that proper return code is server to client | |
102 | _http_ret = HTTPLockedRC(ex.repository, locked_by) |
|
102 | _http_ret = HTTPLockedRC(ex.repository, locked_by) | |
103 | if str(_http_ret.code).startswith('2'): |
|
103 | if str(_http_ret.code).startswith('2'): | |
104 | #2xx Codes don't raise exceptions |
|
104 | #2xx Codes don't raise exceptions | |
105 | sys.stdout.write(_http_ret.title) |
|
105 | sys.stdout.write(_http_ret.title) | |
106 | else: |
|
106 | else: | |
107 | raise _http_ret |
|
107 | raise _http_ret | |
108 |
|
108 | |||
109 |
|
109 | |||
110 | def pre_pull(ui, repo, **kwargs): |
|
110 | def pre_pull(ui, repo, **kwargs): | |
111 | # pre push function, currently used to ban pushing when |
|
111 | # pre push function, currently used to ban pushing when | |
112 | # repository is locked |
|
112 | # repository is locked | |
113 | ex = _extract_extras() |
|
113 | ex = _extract_extras() | |
114 | if ex.locked_by[0]: |
|
114 | if ex.locked_by[0]: | |
115 | locked_by = User.get(ex.locked_by[0]).username |
|
115 | locked_by = User.get(ex.locked_by[0]).username | |
116 | # this exception is interpreted in git/hg middlewares and based |
|
116 | # this exception is interpreted in git/hg middlewares and based | |
117 | # on that proper return code is server to client |
|
117 | # on that proper return code is server to client | |
118 | _http_ret = HTTPLockedRC(ex.repository, locked_by) |
|
118 | _http_ret = HTTPLockedRC(ex.repository, locked_by) | |
119 | if str(_http_ret.code).startswith('2'): |
|
119 | if str(_http_ret.code).startswith('2'): | |
120 | #2xx Codes don't raise exceptions |
|
120 | #2xx Codes don't raise exceptions | |
121 | sys.stdout.write(_http_ret.title) |
|
121 | sys.stdout.write(_http_ret.title) | |
122 | else: |
|
122 | else: | |
123 | raise _http_ret |
|
123 | raise _http_ret | |
124 |
|
124 | |||
125 |
|
125 | |||
126 | def log_pull_action(ui, repo, **kwargs): |
|
126 | def log_pull_action(ui, repo, **kwargs): | |
127 | """ |
|
127 | """ | |
128 | Logs user last pull action |
|
128 | Logs user last pull action | |
129 |
|
129 | |||
130 | :param ui: |
|
130 | :param ui: | |
131 | :param repo: |
|
131 | :param repo: | |
132 | """ |
|
132 | """ | |
133 | ex = _extract_extras() |
|
133 | ex = _extract_extras() | |
134 |
|
134 | |||
135 | user = User.get_by_username(ex.username) |
|
135 | user = User.get_by_username(ex.username) | |
136 | action = 'pull' |
|
136 | action = 'pull' | |
137 | action_logger(user, action, ex.repository, ex.ip, commit=True) |
|
137 | action_logger(user, action, ex.repository, ex.ip, commit=True) | |
138 | # extension hook call |
|
138 | # extension hook call | |
139 | from rhodecode import EXTENSIONS |
|
139 | from rhodecode import EXTENSIONS | |
140 | callback = getattr(EXTENSIONS, 'PULL_HOOK', None) |
|
140 | callback = getattr(EXTENSIONS, 'PULL_HOOK', None) | |
141 | if isfunction(callback): |
|
141 | if isfunction(callback): | |
142 | kw = {} |
|
142 | kw = {} | |
143 | kw.update(ex) |
|
143 | kw.update(ex) | |
144 | callback(**kw) |
|
144 | callback(**kw) | |
145 |
|
145 | |||
146 | if ex.make_lock: |
|
146 | if ex.make_lock is not None and ex.make_lock: | |
147 | Repository.lock(Repository.get_by_repo_name(ex.repository), user.user_id) |
|
147 | Repository.lock(Repository.get_by_repo_name(ex.repository), user.user_id) | |
148 | #msg = 'Made lock on repo `%s`' % repository |
|
148 | #msg = 'Made lock on repo `%s`' % repository | |
149 | #sys.stdout.write(msg) |
|
149 | #sys.stdout.write(msg) | |
150 |
|
150 | |||
151 | if ex.locked_by[0]: |
|
151 | if ex.locked_by[0]: | |
152 | locked_by = User.get(ex.locked_by[0]).username |
|
152 | locked_by = User.get(ex.locked_by[0]).username | |
153 | _http_ret = HTTPLockedRC(ex.repository, locked_by) |
|
153 | _http_ret = HTTPLockedRC(ex.repository, locked_by) | |
154 | if str(_http_ret.code).startswith('2'): |
|
154 | if str(_http_ret.code).startswith('2'): | |
155 | #2xx Codes don't raise exceptions |
|
155 | #2xx Codes don't raise exceptions | |
156 | sys.stdout.write(_http_ret.title) |
|
156 | sys.stdout.write(_http_ret.title) | |
157 | return 0 |
|
157 | return 0 | |
158 |
|
158 | |||
159 |
|
159 | |||
160 | def log_push_action(ui, repo, **kwargs): |
|
160 | def log_push_action(ui, repo, **kwargs): | |
161 | """ |
|
161 | """ | |
162 | Maps user last push action to new changeset id, from mercurial |
|
162 | Maps user last push action to new changeset id, from mercurial | |
163 |
|
163 | |||
164 | :param ui: |
|
164 | :param ui: | |
165 | :param repo: repo object containing the `ui` object |
|
165 | :param repo: repo object containing the `ui` object | |
166 | """ |
|
166 | """ | |
167 |
|
167 | |||
168 | ex = _extract_extras() |
|
168 | ex = _extract_extras() | |
169 |
|
169 | |||
170 | action = ex.action + ':%s' |
|
170 | action = ex.action + ':%s' | |
171 |
|
171 | |||
172 | if ex.scm == 'hg': |
|
172 | if ex.scm == 'hg': | |
173 | node = kwargs['node'] |
|
173 | node = kwargs['node'] | |
174 |
|
174 | |||
175 | def get_revs(repo, rev_opt): |
|
175 | def get_revs(repo, rev_opt): | |
176 | if rev_opt: |
|
176 | if rev_opt: | |
177 | revs = revrange(repo, rev_opt) |
|
177 | revs = revrange(repo, rev_opt) | |
178 |
|
178 | |||
179 | if len(revs) == 0: |
|
179 | if len(revs) == 0: | |
180 | return (nullrev, nullrev) |
|
180 | return (nullrev, nullrev) | |
181 | return (max(revs), min(revs)) |
|
181 | return (max(revs), min(revs)) | |
182 | else: |
|
182 | else: | |
183 | return (len(repo) - 1, 0) |
|
183 | return (len(repo) - 1, 0) | |
184 |
|
184 | |||
185 | stop, start = get_revs(repo, [node + ':']) |
|
185 | stop, start = get_revs(repo, [node + ':']) | |
186 | h = binascii.hexlify |
|
186 | h = binascii.hexlify | |
187 | revs = [h(repo[r].node()) for r in xrange(start, stop + 1)] |
|
187 | revs = [h(repo[r].node()) for r in xrange(start, stop + 1)] | |
188 | elif ex.scm == 'git': |
|
188 | elif ex.scm == 'git': | |
189 | revs = kwargs.get('_git_revs', []) |
|
189 | revs = kwargs.get('_git_revs', []) | |
190 | if '_git_revs' in kwargs: |
|
190 | if '_git_revs' in kwargs: | |
191 | kwargs.pop('_git_revs') |
|
191 | kwargs.pop('_git_revs') | |
192 |
|
192 | |||
193 | action = action % ','.join(revs) |
|
193 | action = action % ','.join(revs) | |
194 |
|
194 | |||
195 | action_logger(ex.username, action, ex.repository, ex.ip, commit=True) |
|
195 | action_logger(ex.username, action, ex.repository, ex.ip, commit=True) | |
196 |
|
196 | |||
197 | # extension hook call |
|
197 | # extension hook call | |
198 | from rhodecode import EXTENSIONS |
|
198 | from rhodecode import EXTENSIONS | |
199 | callback = getattr(EXTENSIONS, 'PUSH_HOOK', None) |
|
199 | callback = getattr(EXTENSIONS, 'PUSH_HOOK', None) | |
200 | if isfunction(callback): |
|
200 | if isfunction(callback): | |
201 | kw = {'pushed_revs': revs} |
|
201 | kw = {'pushed_revs': revs} | |
202 | kw.update(ex) |
|
202 | kw.update(ex) | |
203 | callback(**kw) |
|
203 | callback(**kw) | |
204 |
|
204 | |||
205 | if not ex.make_lock: |
|
205 | if ex.make_lock is not None and not ex.make_lock: | |
206 | Repository.unlock(Repository.get_by_repo_name(ex.repository)) |
|
206 | Repository.unlock(Repository.get_by_repo_name(ex.repository)) | |
207 | msg = 'Released lock on repo `%s`\n' % ex.repository |
|
207 | msg = 'Released lock on repo `%s`\n' % ex.repository | |
208 | sys.stdout.write(msg) |
|
208 | sys.stdout.write(msg) | |
209 |
|
209 | |||
210 | if ex.locked_by[0]: |
|
210 | if ex.locked_by[0]: | |
211 | locked_by = User.get(ex.locked_by[0]).username |
|
211 | locked_by = User.get(ex.locked_by[0]).username | |
212 | _http_ret = HTTPLockedRC(ex.repository, locked_by) |
|
212 | _http_ret = HTTPLockedRC(ex.repository, locked_by) | |
213 | if str(_http_ret.code).startswith('2'): |
|
213 | if str(_http_ret.code).startswith('2'): | |
214 | #2xx Codes don't raise exceptions |
|
214 | #2xx Codes don't raise exceptions | |
215 | sys.stdout.write(_http_ret.title) |
|
215 | sys.stdout.write(_http_ret.title) | |
216 |
|
216 | |||
217 | return 0 |
|
217 | return 0 | |
218 |
|
218 | |||
219 |
|
219 | |||
220 | def log_create_repository(repository_dict, created_by, **kwargs): |
|
220 | def log_create_repository(repository_dict, created_by, **kwargs): | |
221 | """ |
|
221 | """ | |
222 | Post create repository Hook. This is a dummy function for admins to re-use |
|
222 | Post create repository Hook. This is a dummy function for admins to re-use | |
223 | if needed. It's taken from rhodecode-extensions module and executed |
|
223 | if needed. It's taken from rhodecode-extensions module and executed | |
224 | if present |
|
224 | if present | |
225 |
|
225 | |||
226 | :param repository: dict dump of repository object |
|
226 | :param repository: dict dump of repository object | |
227 | :param created_by: username who created repository |
|
227 | :param created_by: username who created repository | |
228 |
|
228 | |||
229 | available keys of repository_dict: |
|
229 | available keys of repository_dict: | |
230 |
|
230 | |||
231 | 'repo_type', |
|
231 | 'repo_type', | |
232 | 'description', |
|
232 | 'description', | |
233 | 'private', |
|
233 | 'private', | |
234 | 'created_on', |
|
234 | 'created_on', | |
235 | 'enable_downloads', |
|
235 | 'enable_downloads', | |
236 | 'repo_id', |
|
236 | 'repo_id', | |
237 | 'user_id', |
|
237 | 'user_id', | |
238 | 'enable_statistics', |
|
238 | 'enable_statistics', | |
239 | 'clone_uri', |
|
239 | 'clone_uri', | |
240 | 'fork_id', |
|
240 | 'fork_id', | |
241 | 'group_id', |
|
241 | 'group_id', | |
242 | 'repo_name' |
|
242 | 'repo_name' | |
243 |
|
243 | |||
244 | """ |
|
244 | """ | |
245 | from rhodecode import EXTENSIONS |
|
245 | from rhodecode import EXTENSIONS | |
246 | callback = getattr(EXTENSIONS, 'CREATE_REPO_HOOK', None) |
|
246 | callback = getattr(EXTENSIONS, 'CREATE_REPO_HOOK', None) | |
247 | if isfunction(callback): |
|
247 | if isfunction(callback): | |
248 | kw = {} |
|
248 | kw = {} | |
249 | kw.update(repository_dict) |
|
249 | kw.update(repository_dict) | |
250 | kw.update({'created_by': created_by}) |
|
250 | kw.update({'created_by': created_by}) | |
251 | kw.update(kwargs) |
|
251 | kw.update(kwargs) | |
252 | return callback(**kw) |
|
252 | return callback(**kw) | |
253 |
|
253 | |||
254 | return 0 |
|
254 | return 0 | |
255 |
|
255 | |||
256 |
|
256 | |||
257 | def log_delete_repository(repository_dict, deleted_by, **kwargs): |
|
257 | def log_delete_repository(repository_dict, deleted_by, **kwargs): | |
258 | """ |
|
258 | """ | |
259 | Post delete repository Hook. This is a dummy function for admins to re-use |
|
259 | Post delete repository Hook. This is a dummy function for admins to re-use | |
260 | if needed. It's taken from rhodecode-extensions module and executed |
|
260 | if needed. It's taken from rhodecode-extensions module and executed | |
261 | if present |
|
261 | if present | |
262 |
|
262 | |||
263 | :param repository: dict dump of repository object |
|
263 | :param repository: dict dump of repository object | |
264 | :param deleted_by: username who deleted the repository |
|
264 | :param deleted_by: username who deleted the repository | |
265 |
|
265 | |||
266 | available keys of repository_dict: |
|
266 | available keys of repository_dict: | |
267 |
|
267 | |||
268 | 'repo_type', |
|
268 | 'repo_type', | |
269 | 'description', |
|
269 | 'description', | |
270 | 'private', |
|
270 | 'private', | |
271 | 'created_on', |
|
271 | 'created_on', | |
272 | 'enable_downloads', |
|
272 | 'enable_downloads', | |
273 | 'repo_id', |
|
273 | 'repo_id', | |
274 | 'user_id', |
|
274 | 'user_id', | |
275 | 'enable_statistics', |
|
275 | 'enable_statistics', | |
276 | 'clone_uri', |
|
276 | 'clone_uri', | |
277 | 'fork_id', |
|
277 | 'fork_id', | |
278 | 'group_id', |
|
278 | 'group_id', | |
279 | 'repo_name' |
|
279 | 'repo_name' | |
280 |
|
280 | |||
281 | """ |
|
281 | """ | |
282 | from rhodecode import EXTENSIONS |
|
282 | from rhodecode import EXTENSIONS | |
283 | callback = getattr(EXTENSIONS, 'DELETE_REPO_HOOK', None) |
|
283 | callback = getattr(EXTENSIONS, 'DELETE_REPO_HOOK', None) | |
284 | if isfunction(callback): |
|
284 | if isfunction(callback): | |
285 | kw = {} |
|
285 | kw = {} | |
286 | kw.update(repository_dict) |
|
286 | kw.update(repository_dict) | |
287 | kw.update({'deleted_by': deleted_by, |
|
287 | kw.update({'deleted_by': deleted_by, | |
288 | 'deleted_on': time.time()}) |
|
288 | 'deleted_on': time.time()}) | |
289 | kw.update(kwargs) |
|
289 | kw.update(kwargs) | |
290 | return callback(**kw) |
|
290 | return callback(**kw) | |
291 |
|
291 | |||
292 | return 0 |
|
292 | return 0 | |
293 |
|
293 | |||
294 |
|
294 | |||
295 | handle_git_pre_receive = (lambda repo_path, revs, env: |
|
295 | handle_git_pre_receive = (lambda repo_path, revs, env: | |
296 | handle_git_receive(repo_path, revs, env, hook_type='pre')) |
|
296 | handle_git_receive(repo_path, revs, env, hook_type='pre')) | |
297 | handle_git_post_receive = (lambda repo_path, revs, env: |
|
297 | handle_git_post_receive = (lambda repo_path, revs, env: | |
298 | handle_git_receive(repo_path, revs, env, hook_type='post')) |
|
298 | handle_git_receive(repo_path, revs, env, hook_type='post')) | |
299 |
|
299 | |||
300 |
|
300 | |||
301 | def handle_git_receive(repo_path, revs, env, hook_type='post'): |
|
301 | def handle_git_receive(repo_path, revs, env, hook_type='post'): | |
302 | """ |
|
302 | """ | |
303 | A really hacky method that is runned by git post-receive hook and logs |
|
303 | A really hacky method that is runned by git post-receive hook and logs | |
304 | an push action together with pushed revisions. It's executed by subprocess |
|
304 | an push action together with pushed revisions. It's executed by subprocess | |
305 | thus needs all info to be able to create a on the fly pylons enviroment, |
|
305 | thus needs all info to be able to create a on the fly pylons enviroment, | |
306 | connect to database and run the logging code. Hacky as sh*t but works. |
|
306 | connect to database and run the logging code. Hacky as sh*t but works. | |
307 |
|
307 | |||
308 | :param repo_path: |
|
308 | :param repo_path: | |
309 | :type repo_path: |
|
309 | :type repo_path: | |
310 | :param revs: |
|
310 | :param revs: | |
311 | :type revs: |
|
311 | :type revs: | |
312 | :param env: |
|
312 | :param env: | |
313 | :type env: |
|
313 | :type env: | |
314 | """ |
|
314 | """ | |
315 | from paste.deploy import appconfig |
|
315 | from paste.deploy import appconfig | |
316 | from sqlalchemy import engine_from_config |
|
316 | from sqlalchemy import engine_from_config | |
317 | from rhodecode.config.environment import load_environment |
|
317 | from rhodecode.config.environment import load_environment | |
318 | from rhodecode.model import init_model |
|
318 | from rhodecode.model import init_model | |
319 | from rhodecode.model.db import RhodeCodeUi |
|
319 | from rhodecode.model.db import RhodeCodeUi | |
320 | from rhodecode.lib.utils import make_ui |
|
320 | from rhodecode.lib.utils import make_ui | |
321 | extras = _extract_extras(env) |
|
321 | extras = _extract_extras(env) | |
322 |
|
322 | |||
323 | path, ini_name = os.path.split(extras['config']) |
|
323 | path, ini_name = os.path.split(extras['config']) | |
324 | conf = appconfig('config:%s' % ini_name, relative_to=path) |
|
324 | conf = appconfig('config:%s' % ini_name, relative_to=path) | |
325 | load_environment(conf.global_conf, conf.local_conf) |
|
325 | load_environment(conf.global_conf, conf.local_conf) | |
326 |
|
326 | |||
327 | engine = engine_from_config(conf, 'sqlalchemy.db1.') |
|
327 | engine = engine_from_config(conf, 'sqlalchemy.db1.') | |
328 | init_model(engine) |
|
328 | init_model(engine) | |
329 |
|
329 | |||
330 | baseui = make_ui('db') |
|
330 | baseui = make_ui('db') | |
331 | # fix if it's not a bare repo |
|
331 | # fix if it's not a bare repo | |
332 | if repo_path.endswith(os.sep + '.git'): |
|
332 | if repo_path.endswith(os.sep + '.git'): | |
333 | repo_path = repo_path[:-5] |
|
333 | repo_path = repo_path[:-5] | |
334 |
|
334 | |||
335 | repo = Repository.get_by_full_path(repo_path) |
|
335 | repo = Repository.get_by_full_path(repo_path) | |
336 | if not repo: |
|
336 | if not repo: | |
337 | raise OSError('Repository %s not found in database' |
|
337 | raise OSError('Repository %s not found in database' | |
338 | % (safe_str(repo_path))) |
|
338 | % (safe_str(repo_path))) | |
339 |
|
339 | |||
340 | _hooks = dict(baseui.configitems('hooks')) or {} |
|
340 | _hooks = dict(baseui.configitems('hooks')) or {} | |
341 |
|
341 | |||
342 | if hook_type == 'pre': |
|
342 | if hook_type == 'pre': | |
343 | repo = repo.scm_instance |
|
343 | repo = repo.scm_instance | |
344 | else: |
|
344 | else: | |
345 | #post push shouldn't use the cached instance never |
|
345 | #post push shouldn't use the cached instance never | |
346 | repo = repo.scm_instance_no_cache() |
|
346 | repo = repo.scm_instance_no_cache() | |
347 |
|
347 | |||
348 | if hook_type == 'pre': |
|
348 | if hook_type == 'pre': | |
349 | pre_push(baseui, repo) |
|
349 | pre_push(baseui, repo) | |
350 |
|
350 | |||
351 | # if push hook is enabled via web interface |
|
351 | # if push hook is enabled via web interface | |
352 | elif hook_type == 'post' and _hooks.get(RhodeCodeUi.HOOK_PUSH): |
|
352 | elif hook_type == 'post' and _hooks.get(RhodeCodeUi.HOOK_PUSH): | |
353 |
|
353 | |||
354 | rev_data = [] |
|
354 | rev_data = [] | |
355 | for l in revs: |
|
355 | for l in revs: | |
356 | old_rev, new_rev, ref = l.split(' ') |
|
356 | old_rev, new_rev, ref = l.split(' ') | |
357 | _ref_data = ref.split('/') |
|
357 | _ref_data = ref.split('/') | |
358 | if _ref_data[1] in ['tags', 'heads']: |
|
358 | if _ref_data[1] in ['tags', 'heads']: | |
359 | rev_data.append({'old_rev': old_rev, |
|
359 | rev_data.append({'old_rev': old_rev, | |
360 | 'new_rev': new_rev, |
|
360 | 'new_rev': new_rev, | |
361 | 'ref': ref, |
|
361 | 'ref': ref, | |
362 | 'type': _ref_data[1], |
|
362 | 'type': _ref_data[1], | |
363 | 'name': _ref_data[2].strip()}) |
|
363 | 'name': _ref_data[2].strip()}) | |
364 |
|
364 | |||
365 | git_revs = [] |
|
365 | git_revs = [] | |
366 | for push_ref in rev_data: |
|
366 | for push_ref in rev_data: | |
367 | _type = push_ref['type'] |
|
367 | _type = push_ref['type'] | |
368 | if _type == 'heads': |
|
368 | if _type == 'heads': | |
369 | if push_ref['old_rev'] == EmptyChangeset().raw_id: |
|
369 | if push_ref['old_rev'] == EmptyChangeset().raw_id: | |
370 | cmd = "for-each-ref --format='%(refname)' 'refs/heads/*'" |
|
370 | cmd = "for-each-ref --format='%(refname)' 'refs/heads/*'" | |
371 | heads = repo.run_git_command(cmd)[0] |
|
371 | heads = repo.run_git_command(cmd)[0] | |
372 | heads = heads.replace(push_ref['ref'], '') |
|
372 | heads = heads.replace(push_ref['ref'], '') | |
373 | heads = ' '.join(map(lambda c: c.strip('\n').strip(), |
|
373 | heads = ' '.join(map(lambda c: c.strip('\n').strip(), | |
374 | heads.splitlines())) |
|
374 | heads.splitlines())) | |
375 | cmd = (('log %(new_rev)s' % push_ref) + |
|
375 | cmd = (('log %(new_rev)s' % push_ref) + | |
376 | ' --reverse --pretty=format:"%H" --not ' + heads) |
|
376 | ' --reverse --pretty=format:"%H" --not ' + heads) | |
377 | git_revs += repo.run_git_command(cmd)[0].splitlines() |
|
377 | git_revs += repo.run_git_command(cmd)[0].splitlines() | |
378 |
|
378 | |||
379 | elif push_ref['new_rev'] == EmptyChangeset().raw_id: |
|
379 | elif push_ref['new_rev'] == EmptyChangeset().raw_id: | |
380 | #delete branch case |
|
380 | #delete branch case | |
381 | git_revs += ['delete_branch=>%s' % push_ref['name']] |
|
381 | git_revs += ['delete_branch=>%s' % push_ref['name']] | |
382 | else: |
|
382 | else: | |
383 | cmd = (('log %(old_rev)s..%(new_rev)s' % push_ref) + |
|
383 | cmd = (('log %(old_rev)s..%(new_rev)s' % push_ref) + | |
384 | ' --reverse --pretty=format:"%H"') |
|
384 | ' --reverse --pretty=format:"%H"') | |
385 | git_revs += repo.run_git_command(cmd)[0].splitlines() |
|
385 | git_revs += repo.run_git_command(cmd)[0].splitlines() | |
386 |
|
386 | |||
387 | elif _type == 'tags': |
|
387 | elif _type == 'tags': | |
388 | git_revs += ['tag=>%s' % push_ref['name']] |
|
388 | git_revs += ['tag=>%s' % push_ref['name']] | |
389 |
|
389 | |||
390 | log_push_action(baseui, repo, _git_revs=git_revs) |
|
390 | log_push_action(baseui, repo, _git_revs=git_revs) |
General Comments 0
You need to be logged in to leave comments.
Login now