##// END OF EJS Templates
hooks: added changes to propagate commit metadata on pre-push....
marcink -
r170:88d2ba78 default
parent child Browse files
Show More
@@ -152,26 +152,42 b' def post_pull(ui, repo, **kwargs):'
152 return _call_hook('post_pull', _extras_from_ui(ui), HgMessageWriter(ui))
152 return _call_hook('post_pull', _extras_from_ui(ui), HgMessageWriter(ui))
153
153
154
154
155 def pre_push(ui, repo, **kwargs):
155 def pre_push(ui, repo, node=None, **kwargs):
156 return _call_hook('pre_push', _extras_from_ui(ui), HgMessageWriter(ui))
156 extras = _extras_from_ui(ui)
157
158 rev_data = []
159 if node and kwargs.get('hooktype') == 'pretxnchangegroup':
160 branches = collections.defaultdict(list)
161 for commit_id, branch in _rev_range_hash(repo, node, with_branch=True):
162 branches[branch].append(commit_id)
163
164 for branch, commits in branches.iteritems():
165 old_rev = kwargs.get('node_last') or commits[0]
166 rev_data.append({
167 'old_rev': old_rev,
168 'new_rev': commits[-1],
169 'ref': '',
170 'type': 'branch',
171 'name': branch,
172 })
173
174 extras['commit_ids'] = rev_data
175 return _call_hook('pre_push', extras, HgMessageWriter(ui))
157
176
158
177
159 # N.B.(skreft): the two functions below were taken and adapted from
178 def _rev_range_hash(repo, node, with_branch=False):
160 # rhodecode.lib.vcs.remote.handle_git_pre_receive
161 # They are required to compute the commit_ids
162 def _get_revs(repo, rev_opt):
163 revs = [rev for rev in mercurial.scmutil.revrange(repo, rev_opt)]
164 if len(revs) == 0:
165 return (mercurial.node.nullrev, mercurial.node.nullrev)
166
179
167 return max(revs), min(revs)
180 commits = []
168
181 for rev in xrange(repo[node], len(repo)):
182 ctx = repo[rev]
183 commit_id = mercurial.node.hex(ctx.node())
184 branch = ctx.branch()
185 if with_branch:
186 commits.append((commit_id, branch))
187 else:
188 commits.append(commit_id)
169
189
170 def _rev_range_hash(repo, node):
190 return commits
171 stop, start = _get_revs(repo, [node + ':'])
172 revs = [mercurial.node.hex(repo[r].node()) for r in xrange(start, stop + 1)]
173
174 return revs
175
191
176
192
177 def post_push(ui, repo, node, **kwargs):
193 def post_push(ui, repo, node, **kwargs):
@@ -257,7 +273,23 b' def git_post_pull(extras):'
257 return HookResponse(status, stdout.getvalue())
273 return HookResponse(status, stdout.getvalue())
258
274
259
275
260 def git_pre_receive(unused_repo_path, unused_revs, env):
276 def _parse_git_ref_lines(revision_lines):
277 rev_data = []
278 for revision_line in revision_lines or []:
279 old_rev, new_rev, ref = revision_line.strip().split(' ')
280 ref_data = ref.split('/', 2)
281 if ref_data[1] in ('tags', 'heads'):
282 rev_data.append({
283 'old_rev': old_rev,
284 'new_rev': new_rev,
285 'ref': ref,
286 'type': ref_data[1],
287 'name': ref_data[2],
288 })
289 return rev_data
290
291
292 def git_pre_receive(unused_repo_path, revision_lines, env):
261 """
293 """
262 Pre push hook.
294 Pre push hook.
263
295
@@ -268,8 +300,10 b' def git_pre_receive(unused_repo_path, un'
268 :rtype: int
300 :rtype: int
269 """
301 """
270 extras = json.loads(env['RC_SCM_DATA'])
302 extras = json.loads(env['RC_SCM_DATA'])
303 rev_data = _parse_git_ref_lines(revision_lines)
271 if 'push' not in extras['hooks']:
304 if 'push' not in extras['hooks']:
272 return 0
305 return 0
306 extras['commit_ids'] = rev_data
273 return _call_hook('pre_push', extras, GitMessageWriter())
307 return _call_hook('pre_push', extras, GitMessageWriter())
274
308
275
309
@@ -277,7 +311,7 b' def _run_command(arguments):'
277 """
311 """
278 Run the specified command and return the stdout.
312 Run the specified command and return the stdout.
279
313
280 :param arguments: sequence of program arugments (including the program name)
314 :param arguments: sequence of program arguments (including the program name)
281 :type arguments: list[str]
315 :type arguments: list[str]
282 """
316 """
283 # TODO(skreft): refactor this method and all the other similar ones.
317 # TODO(skreft): refactor this method and all the other similar ones.
@@ -308,18 +342,7 b' def git_post_receive(unused_repo_path, r'
308 if 'push' not in extras['hooks']:
342 if 'push' not in extras['hooks']:
309 return 0
343 return 0
310
344
311 rev_data = []
345 rev_data = _parse_git_ref_lines(revision_lines)
312 for revision_line in revision_lines:
313 old_rev, new_rev, ref = revision_line.strip().split(' ')
314 ref_data = ref.split('/', 2)
315 if ref_data[1] in ('tags', 'heads'):
316 rev_data.append({
317 'old_rev': old_rev,
318 'new_rev': new_rev,
319 'ref': ref,
320 'type': ref_data[1],
321 'name': ref_data[2],
322 })
323
346
324 git_revs = []
347 git_revs = []
325
348
@@ -339,7 +362,7 b' def git_post_receive(unused_repo_path, r'
339 except Exception:
362 except Exception:
340 cmd = ['git', 'symbolic-ref', 'HEAD',
363 cmd = ['git', 'symbolic-ref', 'HEAD',
341 'refs/heads/%s' % push_ref['name']]
364 'refs/heads/%s' % push_ref['name']]
342 print "Setting default branch to %s" % push_ref['name']
365 print("Setting default branch to %s" % push_ref['name'])
343 _run_command(cmd)
366 _run_command(cmd)
344
367
345 cmd = ['git', 'for-each-ref', '--format=%(refname)',
368 cmd = ['git', 'for-each-ref', '--format=%(refname)',
General Comments 0
You need to be logged in to leave comments. Login now