##// END OF EJS Templates
py3: fixed mercurial clone
super-admin -
r1052:d4272d07 python3
parent child Browse files
Show More
@@ -1,738 +1,738 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # RhodeCode VCSServer provides access to different vcs backends via network.
4 4 # Copyright (C) 2014-2020 RhodeCode GmbH
5 5 #
6 6 # This program is free software; you can redistribute it and/or modify
7 7 # it under the terms of the GNU General Public License as published by
8 8 # the Free Software Foundation; either version 3 of the License, or
9 9 # (at your option) any later version.
10 10 #
11 11 # This program is distributed in the hope that it will be useful,
12 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 14 # GNU General Public License for more details.
15 15 #
16 16 # You should have received a copy of the GNU General Public License
17 17 # along with this program; if not, write to the Free Software Foundation,
18 18 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 19
20 20 import io
21 21 import os
22 22 import sys
23 23 import logging
24 24 import collections
25 25 import importlib
26 26 import base64
27 27 import msgpack
28 28
29 29 from http.client import HTTPConnection
30 30
31 31
32 32 import mercurial.scmutil
33 33 import mercurial.node
34 34
35 35 from vcsserver.lib.rc_json import json
36 36 from vcsserver import exceptions, subprocessio, settings
37 37 from vcsserver.utils import safe_bytes
38 38
39 39 log = logging.getLogger(__name__)
40 40
41 41
42 42 class HooksHttpClient(object):
43 43 proto = 'msgpack.v1'
44 44 connection = None
45 45
46 46 def __init__(self, hooks_uri):
47 47 self.hooks_uri = hooks_uri
48 48
49 49 def __call__(self, method, extras):
50 50 connection = HTTPConnection(self.hooks_uri)
51 51 # binary msgpack body
52 52 headers, body = self._serialize(method, extras)
53 53 try:
54 54 connection.request('POST', '/', body, headers)
55 55 except Exception as error:
56 56 log.error('Hooks calling Connection failed on %s, org error: %s', connection.__dict__, error)
57 57 raise
58 58 response = connection.getresponse()
59 59 try:
60 60 return msgpack.load(response, raw=False)
61 61 except Exception:
62 62 response_data = response.read()
63 63 log.exception('Failed to decode hook response json data. '
64 64 'response_code:%s, raw_data:%s',
65 65 response.status, response_data)
66 66 raise
67 67
68 68 @classmethod
69 69 def _serialize(cls, hook_name, extras):
70 70 data = {
71 71 'method': hook_name,
72 72 'extras': extras
73 73 }
74 74 headers = {
75 75 'rc-hooks-protocol': cls.proto
76 76 }
77 77 return headers, msgpack.packb(data)
78 78
79 79
80 80 class HooksDummyClient(object):
81 81 def __init__(self, hooks_module):
82 82 self._hooks_module = importlib.import_module(hooks_module)
83 83
84 84 def __call__(self, hook_name, extras):
85 85 with self._hooks_module.Hooks() as hooks:
86 86 return getattr(hooks, hook_name)(extras)
87 87
88 88
89 89 class HooksShadowRepoClient(object):
90 90
91 91 def __call__(self, hook_name, extras):
92 92 return {'output': '', 'status': 0}
93 93
94 94
95 95 class RemoteMessageWriter(object):
96 96 """Writer base class."""
97 97 def write(self, message):
98 98 raise NotImplementedError()
99 99
100 100
101 101 class HgMessageWriter(RemoteMessageWriter):
102 102 """Writer that knows how to send messages to mercurial clients."""
103 103
104 104 def __init__(self, ui):
105 105 self.ui = ui
106 106
107 107 def write(self, message):
108 108 # TODO: Check why the quiet flag is set by default.
109 109 old = self.ui.quiet
110 110 self.ui.quiet = False
111 111 self.ui.status(message.encode('utf-8'))
112 112 self.ui.quiet = old
113 113
114 114
115 115 class GitMessageWriter(RemoteMessageWriter):
116 116 """Writer that knows how to send messages to git clients."""
117 117
118 118 def __init__(self, stdout=None):
119 119 self.stdout = stdout or sys.stdout
120 120
121 121 def write(self, message):
122 122 self.stdout.write(safe_bytes(message))
123 123
124 124
125 125 class SvnMessageWriter(RemoteMessageWriter):
126 126 """Writer that knows how to send messages to svn clients."""
127 127
128 128 def __init__(self, stderr=None):
129 129 # SVN needs data sent to stderr for back-to-client messaging
130 130 self.stderr = stderr or sys.stderr
131 131
132 132 def write(self, message):
133 133 self.stderr.write(message.encode('utf-8'))
134 134
135 135
136 136 def _handle_exception(result):
137 137 exception_class = result.get('exception')
138 138 exception_traceback = result.get('exception_traceback')
139 139
140 140 if exception_traceback:
141 141 log.error('Got traceback from remote call:%s', exception_traceback)
142 142
143 143 if exception_class == 'HTTPLockedRC':
144 144 raise exceptions.RepositoryLockedException()(*result['exception_args'])
145 145 elif exception_class == 'HTTPBranchProtected':
146 146 raise exceptions.RepositoryBranchProtectedException()(*result['exception_args'])
147 147 elif exception_class == 'RepositoryError':
148 148 raise exceptions.VcsException()(*result['exception_args'])
149 149 elif exception_class:
150 150 raise Exception('Got remote exception "%s" with args "%s"' %
151 151 (exception_class, result['exception_args']))
152 152
153 153
154 154 def _get_hooks_client(extras):
155 155 hooks_uri = extras.get('hooks_uri')
156 156 is_shadow_repo = extras.get('is_shadow_repo')
157 157 if hooks_uri:
158 158 return HooksHttpClient(extras['hooks_uri'])
159 159 elif is_shadow_repo:
160 160 return HooksShadowRepoClient()
161 161 else:
162 162 return HooksDummyClient(extras['hooks_module'])
163 163
164 164
165 165 def _call_hook(hook_name, extras, writer):
166 166 hooks_client = _get_hooks_client(extras)
167 167 log.debug('Hooks, using client:%s', hooks_client)
168 168 result = hooks_client(hook_name, extras)
169 169 log.debug('Hooks got result: %s', result)
170 170
171 171 _handle_exception(result)
172 172 writer.write(result['output'])
173 173
174 174 return result['status']
175 175
176 176
177 177 def _extras_from_ui(ui):
178 hook_data = ui.config('rhodecode', 'RC_SCM_DATA')
178 hook_data = ui.config(b'rhodecode', b'RC_SCM_DATA')
179 179 if not hook_data:
180 180 # maybe it's inside environ ?
181 181 env_hook_data = os.environ.get('RC_SCM_DATA')
182 182 if env_hook_data:
183 183 hook_data = env_hook_data
184 184
185 185 extras = {}
186 186 if hook_data:
187 187 extras = json.loads(hook_data)
188 188 return extras
189 189
190 190
191 191 def _rev_range_hash(repo, node, check_heads=False):
192 192 from vcsserver.hgcompat import get_ctx
193 193
194 194 commits = []
195 195 revs = []
196 196 start = get_ctx(repo, node).rev()
197 197 end = len(repo)
198 198 for rev in range(start, end):
199 199 revs.append(rev)
200 200 ctx = get_ctx(repo, rev)
201 201 commit_id = mercurial.node.hex(ctx.node())
202 202 branch = ctx.branch()
203 203 commits.append((commit_id, branch))
204 204
205 205 parent_heads = []
206 206 if check_heads:
207 207 parent_heads = _check_heads(repo, start, end, revs)
208 208 return commits, parent_heads
209 209
210 210
211 211 def _check_heads(repo, start, end, commits):
212 212 from vcsserver.hgcompat import get_ctx
213 213 changelog = repo.changelog
214 214 parents = set()
215 215
216 216 for new_rev in commits:
217 217 for p in changelog.parentrevs(new_rev):
218 218 if p == mercurial.node.nullrev:
219 219 continue
220 220 if p < start:
221 221 parents.add(p)
222 222
223 223 for p in parents:
224 224 branch = get_ctx(repo, p).branch()
225 225 # The heads descending from that parent, on the same branch
226 226 parent_heads = set([p])
227 227 reachable = set([p])
228 228 for x in range(p + 1, end):
229 229 if get_ctx(repo, x).branch() != branch:
230 230 continue
231 231 for pp in changelog.parentrevs(x):
232 232 if pp in reachable:
233 233 reachable.add(x)
234 234 parent_heads.discard(pp)
235 235 parent_heads.add(x)
236 236 # More than one head? Suggest merging
237 237 if len(parent_heads) > 1:
238 238 return list(parent_heads)
239 239
240 240 return []
241 241
242 242
243 243 def _get_git_env():
244 244 env = {}
245 245 for k, v in os.environ.items():
246 246 if k.startswith('GIT'):
247 247 env[k] = v
248 248
249 249 # serialized version
250 250 return [(k, v) for k, v in env.items()]
251 251
252 252
253 253 def _get_hg_env(old_rev, new_rev, txnid, repo_path):
254 254 env = {}
255 255 for k, v in os.environ.items():
256 256 if k.startswith('HG'):
257 257 env[k] = v
258 258
259 259 env['HG_NODE'] = old_rev
260 260 env['HG_NODE_LAST'] = new_rev
261 261 env['HG_TXNID'] = txnid
262 262 env['HG_PENDING'] = repo_path
263 263
264 264 return [(k, v) for k, v in env.items()]
265 265
266 266
267 267 def repo_size(ui, repo, **kwargs):
268 268 extras = _extras_from_ui(ui)
269 269 return _call_hook('repo_size', extras, HgMessageWriter(ui))
270 270
271 271
272 272 def pre_pull(ui, repo, **kwargs):
273 273 extras = _extras_from_ui(ui)
274 274 return _call_hook('pre_pull', extras, HgMessageWriter(ui))
275 275
276 276
277 277 def pre_pull_ssh(ui, repo, **kwargs):
278 278 extras = _extras_from_ui(ui)
279 279 if extras and extras.get('SSH'):
280 280 return pre_pull(ui, repo, **kwargs)
281 281 return 0
282 282
283 283
284 284 def post_pull(ui, repo, **kwargs):
285 285 extras = _extras_from_ui(ui)
286 286 return _call_hook('post_pull', extras, HgMessageWriter(ui))
287 287
288 288
289 289 def post_pull_ssh(ui, repo, **kwargs):
290 290 extras = _extras_from_ui(ui)
291 291 if extras and extras.get('SSH'):
292 292 return post_pull(ui, repo, **kwargs)
293 293 return 0
294 294
295 295
296 296 def pre_push(ui, repo, node=None, **kwargs):
297 297 """
298 298 Mercurial pre_push hook
299 299 """
300 300 extras = _extras_from_ui(ui)
301 301 detect_force_push = extras.get('detect_force_push')
302 302
303 303 rev_data = []
304 304 if node and kwargs.get('hooktype') == 'pretxnchangegroup':
305 305 branches = collections.defaultdict(list)
306 306 commits, _heads = _rev_range_hash(repo, node, check_heads=detect_force_push)
307 307 for commit_id, branch in commits:
308 308 branches[branch].append(commit_id)
309 309
310 310 for branch, commits in branches.items():
311 311 old_rev = kwargs.get('node_last') or commits[0]
312 312 rev_data.append({
313 313 'total_commits': len(commits),
314 314 'old_rev': old_rev,
315 315 'new_rev': commits[-1],
316 316 'ref': '',
317 317 'type': 'branch',
318 318 'name': branch,
319 319 })
320 320
321 321 for push_ref in rev_data:
322 322 push_ref['multiple_heads'] = _heads
323 323
324 324 repo_path = os.path.join(
325 325 extras.get('repo_store', ''), extras.get('repository', ''))
326 326 push_ref['hg_env'] = _get_hg_env(
327 327 old_rev=push_ref['old_rev'],
328 328 new_rev=push_ref['new_rev'], txnid=kwargs.get('txnid'),
329 329 repo_path=repo_path)
330 330
331 331 extras['hook_type'] = kwargs.get('hooktype', 'pre_push')
332 332 extras['commit_ids'] = rev_data
333 333
334 334 return _call_hook('pre_push', extras, HgMessageWriter(ui))
335 335
336 336
337 337 def pre_push_ssh(ui, repo, node=None, **kwargs):
338 338 extras = _extras_from_ui(ui)
339 339 if extras.get('SSH'):
340 340 return pre_push(ui, repo, node, **kwargs)
341 341
342 342 return 0
343 343
344 344
345 345 def pre_push_ssh_auth(ui, repo, node=None, **kwargs):
346 346 """
347 347 Mercurial pre_push hook for SSH
348 348 """
349 349 extras = _extras_from_ui(ui)
350 350 if extras.get('SSH'):
351 351 permission = extras['SSH_PERMISSIONS']
352 352
353 353 if 'repository.write' == permission or 'repository.admin' == permission:
354 354 return 0
355 355
356 356 # non-zero ret code
357 357 return 1
358 358
359 359 return 0
360 360
361 361
362 362 def post_push(ui, repo, node, **kwargs):
363 363 """
364 364 Mercurial post_push hook
365 365 """
366 366 extras = _extras_from_ui(ui)
367 367
368 368 commit_ids = []
369 369 branches = []
370 370 bookmarks = []
371 371 tags = []
372 372
373 373 commits, _heads = _rev_range_hash(repo, node)
374 374 for commit_id, branch in commits:
375 375 commit_ids.append(commit_id)
376 376 if branch not in branches:
377 377 branches.append(branch)
378 378
379 379 if hasattr(ui, '_rc_pushkey_branches'):
380 380 bookmarks = ui._rc_pushkey_branches
381 381
382 382 extras['hook_type'] = kwargs.get('hooktype', 'post_push')
383 383 extras['commit_ids'] = commit_ids
384 384 extras['new_refs'] = {
385 385 'branches': branches,
386 386 'bookmarks': bookmarks,
387 387 'tags': tags
388 388 }
389 389
390 390 return _call_hook('post_push', extras, HgMessageWriter(ui))
391 391
392 392
393 393 def post_push_ssh(ui, repo, node, **kwargs):
394 394 """
395 395 Mercurial post_push hook for SSH
396 396 """
397 397 if _extras_from_ui(ui).get('SSH'):
398 398 return post_push(ui, repo, node, **kwargs)
399 399 return 0
400 400
401 401
402 402 def key_push(ui, repo, **kwargs):
403 403 from vcsserver.hgcompat import get_ctx
404 404 if kwargs['new'] != '0' and kwargs['namespace'] == 'bookmarks':
405 405 # store new bookmarks in our UI object propagated later to post_push
406 406 ui._rc_pushkey_branches = get_ctx(repo, kwargs['key']).bookmarks()
407 407 return
408 408
409 409
410 410 # backward compat
411 411 log_pull_action = post_pull
412 412
413 413 # backward compat
414 414 log_push_action = post_push
415 415
416 416
417 417 def handle_git_pre_receive(unused_repo_path, unused_revs, unused_env):
418 418 """
419 419 Old hook name: keep here for backward compatibility.
420 420
421 421 This is only required when the installed git hooks are not upgraded.
422 422 """
423 423 pass
424 424
425 425
426 426 def handle_git_post_receive(unused_repo_path, unused_revs, unused_env):
427 427 """
428 428 Old hook name: keep here for backward compatibility.
429 429
430 430 This is only required when the installed git hooks are not upgraded.
431 431 """
432 432 pass
433 433
434 434
435 435 HookResponse = collections.namedtuple('HookResponse', ('status', 'output'))
436 436
437 437
438 438 def git_pre_pull(extras):
439 439 """
440 440 Pre pull hook.
441 441
442 442 :param extras: dictionary containing the keys defined in simplevcs
443 443 :type extras: dict
444 444
445 445 :return: status code of the hook. 0 for success.
446 446 :rtype: int
447 447 """
448 448
449 449 if 'pull' not in extras['hooks']:
450 450 return HookResponse(0, '')
451 451
452 452 stdout = io.BytesIO()
453 453 try:
454 454 status = _call_hook('pre_pull', extras, GitMessageWriter(stdout))
455 455
456 456 except Exception as error:
457 457 log.exception('Failed to call pre_pull hook')
458 458 status = 128
459 459 stdout.write(safe_bytes(f'ERROR: {error}\n'))
460 460
461 461 return HookResponse(status, stdout.getvalue())
462 462
463 463
464 464 def git_post_pull(extras):
465 465 """
466 466 Post pull hook.
467 467
468 468 :param extras: dictionary containing the keys defined in simplevcs
469 469 :type extras: dict
470 470
471 471 :return: status code of the hook. 0 for success.
472 472 :rtype: int
473 473 """
474 474 if 'pull' not in extras['hooks']:
475 475 return HookResponse(0, '')
476 476
477 477 stdout = io.BytesIO()
478 478 try:
479 479 status = _call_hook('post_pull', extras, GitMessageWriter(stdout))
480 480 except Exception as error:
481 481 status = 128
482 482 stdout.write(safe_bytes(f'ERROR: {error}\n'))
483 483
484 484 return HookResponse(status, stdout.getvalue())
485 485
486 486
487 487 def _parse_git_ref_lines(revision_lines):
488 488 rev_data = []
489 489 for revision_line in revision_lines or []:
490 490 old_rev, new_rev, ref = revision_line.strip().split(' ')
491 491 ref_data = ref.split('/', 2)
492 492 if ref_data[1] in ('tags', 'heads'):
493 493 rev_data.append({
494 494 # NOTE(marcink):
495 495 # we're unable to tell total_commits for git at this point
496 496 # but we set the variable for consistency with GIT
497 497 'total_commits': -1,
498 498 'old_rev': old_rev,
499 499 'new_rev': new_rev,
500 500 'ref': ref,
501 501 'type': ref_data[1],
502 502 'name': ref_data[2],
503 503 })
504 504 return rev_data
505 505
506 506
507 507 def git_pre_receive(unused_repo_path, revision_lines, env):
508 508 """
509 509 Pre push hook.
510 510
511 511 :param extras: dictionary containing the keys defined in simplevcs
512 512 :type extras: dict
513 513
514 514 :return: status code of the hook. 0 for success.
515 515 :rtype: int
516 516 """
517 517 extras = json.loads(env['RC_SCM_DATA'])
518 518 rev_data = _parse_git_ref_lines(revision_lines)
519 519 if 'push' not in extras['hooks']:
520 520 return 0
521 521 empty_commit_id = '0' * 40
522 522
523 523 detect_force_push = extras.get('detect_force_push')
524 524
525 525 for push_ref in rev_data:
526 526 # store our git-env which holds the temp store
527 527 push_ref['git_env'] = _get_git_env()
528 528 push_ref['pruned_sha'] = ''
529 529 if not detect_force_push:
530 530 # don't check for forced-push when we don't need to
531 531 continue
532 532
533 533 type_ = push_ref['type']
534 534 new_branch = push_ref['old_rev'] == empty_commit_id
535 535 delete_branch = push_ref['new_rev'] == empty_commit_id
536 536 if type_ == 'heads' and not (new_branch or delete_branch):
537 537 old_rev = push_ref['old_rev']
538 538 new_rev = push_ref['new_rev']
539 539 cmd = [settings.GIT_EXECUTABLE, 'rev-list', old_rev, '^{}'.format(new_rev)]
540 540 stdout, stderr = subprocessio.run_command(
541 541 cmd, env=os.environ.copy())
542 542 # means we're having some non-reachable objects, this forced push was used
543 543 if stdout:
544 544 push_ref['pruned_sha'] = stdout.splitlines()
545 545
546 546 extras['hook_type'] = 'pre_receive'
547 547 extras['commit_ids'] = rev_data
548 548 return _call_hook('pre_push', extras, GitMessageWriter())
549 549
550 550
551 551 def git_post_receive(unused_repo_path, revision_lines, env):
552 552 """
553 553 Post push hook.
554 554
555 555 :param extras: dictionary containing the keys defined in simplevcs
556 556 :type extras: dict
557 557
558 558 :return: status code of the hook. 0 for success.
559 559 :rtype: int
560 560 """
561 561 extras = json.loads(env['RC_SCM_DATA'])
562 562 if 'push' not in extras['hooks']:
563 563 return 0
564 564
565 565 rev_data = _parse_git_ref_lines(revision_lines)
566 566
567 567 git_revs = []
568 568
569 569 # N.B.(skreft): it is ok to just call git, as git before calling a
570 570 # subcommand sets the PATH environment variable so that it point to the
571 571 # correct version of the git executable.
572 572 empty_commit_id = '0' * 40
573 573 branches = []
574 574 tags = []
575 575 for push_ref in rev_data:
576 576 type_ = push_ref['type']
577 577
578 578 if type_ == 'heads':
579 579 if push_ref['old_rev'] == empty_commit_id:
580 580 # starting new branch case
581 581 if push_ref['name'] not in branches:
582 582 branches.append(push_ref['name'])
583 583
584 584 # Fix up head revision if needed
585 585 cmd = [settings.GIT_EXECUTABLE, 'show', 'HEAD']
586 586 try:
587 587 subprocessio.run_command(cmd, env=os.environ.copy())
588 588 except Exception:
589 589 cmd = [settings.GIT_EXECUTABLE, 'symbolic-ref', '"HEAD"',
590 590 '"refs/heads/%s"' % push_ref['name']]
591 591 print(("Setting default branch to %s" % push_ref['name']))
592 592 subprocessio.run_command(cmd, env=os.environ.copy())
593 593
594 594 cmd = [settings.GIT_EXECUTABLE, 'for-each-ref',
595 595 '--format=%(refname)', 'refs/heads/*']
596 596 stdout, stderr = subprocessio.run_command(
597 597 cmd, env=os.environ.copy())
598 598 heads = stdout
599 599 heads = heads.replace(push_ref['ref'], '')
600 600 heads = ' '.join(head for head
601 601 in heads.splitlines() if head) or '.'
602 602 cmd = [settings.GIT_EXECUTABLE, 'log', '--reverse',
603 603 '--pretty=format:%H', '--', push_ref['new_rev'],
604 604 '--not', heads]
605 605 stdout, stderr = subprocessio.run_command(
606 606 cmd, env=os.environ.copy())
607 607 git_revs.extend(stdout.splitlines())
608 608 elif push_ref['new_rev'] == empty_commit_id:
609 609 # delete branch case
610 610 git_revs.append('delete_branch=>%s' % push_ref['name'])
611 611 else:
612 612 if push_ref['name'] not in branches:
613 613 branches.append(push_ref['name'])
614 614
615 615 cmd = [settings.GIT_EXECUTABLE, 'log',
616 616 '{old_rev}..{new_rev}'.format(**push_ref),
617 617 '--reverse', '--pretty=format:%H']
618 618 stdout, stderr = subprocessio.run_command(
619 619 cmd, env=os.environ.copy())
620 620 git_revs.extend(stdout.splitlines())
621 621 elif type_ == 'tags':
622 622 if push_ref['name'] not in tags:
623 623 tags.append(push_ref['name'])
624 624 git_revs.append('tag=>%s' % push_ref['name'])
625 625
626 626 extras['hook_type'] = 'post_receive'
627 627 extras['commit_ids'] = git_revs
628 628 extras['new_refs'] = {
629 629 'branches': branches,
630 630 'bookmarks': [],
631 631 'tags': tags,
632 632 }
633 633
634 634 if 'repo_size' in extras['hooks']:
635 635 try:
636 636 _call_hook('repo_size', extras, GitMessageWriter())
637 637 except:
638 638 pass
639 639
640 640 return _call_hook('post_push', extras, GitMessageWriter())
641 641
642 642
643 643 def _get_extras_from_txn_id(path, txn_id):
644 644 extras = {}
645 645 try:
646 646 cmd = [settings.SVNLOOK_EXECUTABLE, 'pget',
647 647 '-t', txn_id,
648 648 '--revprop', path, 'rc-scm-extras']
649 649 stdout, stderr = subprocessio.run_command(
650 650 cmd, env=os.environ.copy())
651 651 extras = json.loads(base64.urlsafe_b64decode(stdout))
652 652 except Exception:
653 653 log.exception('Failed to extract extras info from txn_id')
654 654
655 655 return extras
656 656
657 657
658 658 def _get_extras_from_commit_id(commit_id, path):
659 659 extras = {}
660 660 try:
661 661 cmd = [settings.SVNLOOK_EXECUTABLE, 'pget',
662 662 '-r', commit_id,
663 663 '--revprop', path, 'rc-scm-extras']
664 664 stdout, stderr = subprocessio.run_command(
665 665 cmd, env=os.environ.copy())
666 666 extras = json.loads(base64.urlsafe_b64decode(stdout))
667 667 except Exception:
668 668 log.exception('Failed to extract extras info from commit_id')
669 669
670 670 return extras
671 671
672 672
673 673 def svn_pre_commit(repo_path, commit_data, env):
674 674 path, txn_id = commit_data
675 675 branches = []
676 676 tags = []
677 677
678 678 if env.get('RC_SCM_DATA'):
679 679 extras = json.loads(env['RC_SCM_DATA'])
680 680 else:
681 681 # fallback method to read from TXN-ID stored data
682 682 extras = _get_extras_from_txn_id(path, txn_id)
683 683 if not extras:
684 684 return 0
685 685
686 686 extras['hook_type'] = 'pre_commit'
687 687 extras['commit_ids'] = [txn_id]
688 688 extras['txn_id'] = txn_id
689 689 extras['new_refs'] = {
690 690 'total_commits': 1,
691 691 'branches': branches,
692 692 'bookmarks': [],
693 693 'tags': tags,
694 694 }
695 695
696 696 return _call_hook('pre_push', extras, SvnMessageWriter())
697 697
698 698
699 699 def svn_post_commit(repo_path, commit_data, env):
700 700 """
701 701 commit_data is path, rev, txn_id
702 702 """
703 703 if len(commit_data) == 3:
704 704 path, commit_id, txn_id = commit_data
705 705 elif len(commit_data) == 2:
706 706 log.error('Failed to extract txn_id from commit_data using legacy method. '
707 707 'Some functionality might be limited')
708 708 path, commit_id = commit_data
709 709 txn_id = None
710 710
711 711 branches = []
712 712 tags = []
713 713
714 714 if env.get('RC_SCM_DATA'):
715 715 extras = json.loads(env['RC_SCM_DATA'])
716 716 else:
717 717 # fallback method to read from TXN-ID stored data
718 718 extras = _get_extras_from_commit_id(commit_id, path)
719 719 if not extras:
720 720 return 0
721 721
722 722 extras['hook_type'] = 'post_commit'
723 723 extras['commit_ids'] = [commit_id]
724 724 extras['txn_id'] = txn_id
725 725 extras['new_refs'] = {
726 726 'branches': branches,
727 727 'bookmarks': [],
728 728 'tags': tags,
729 729 'total_commits': 1,
730 730 }
731 731
732 732 if 'repo_size' in extras['hooks']:
733 733 try:
734 734 _call_hook('repo_size', extras, SvnMessageWriter())
735 735 except Exception:
736 736 pass
737 737
738 738 return _call_hook('post_push', extras, SvnMessageWriter())
@@ -1,241 +1,242 b''
1 1 # RhodeCode VCSServer provides access to different vcs backends via network.
2 2 # Copyright (C) 2014-2020 RhodeCode GmbH
3 3 #
4 4 # This program is free software; you can redistribute it and/or modify
5 5 # it under the terms of the GNU General Public License as published by
6 6 # the Free Software Foundation; either version 3 of the License, or
7 7 # (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software Foundation,
16 16 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 17
18 18 import os
19 19 import logging
20 20 import itertools
21 21
22 22 import mercurial
23 23 import mercurial.error
24 24 import mercurial.wireprotoserver
25 25 import mercurial.hgweb.common
26 26 import mercurial.hgweb.hgweb_mod
27 27 import webob.exc
28 28
29 29 from vcsserver import pygrack, exceptions, settings, git_lfs
30 30 from vcsserver.utils import ascii_bytes, safe_bytes
31 31
32 32 log = logging.getLogger(__name__)
33 33
34 34
35 35 # propagated from mercurial documentation
36 36 HG_UI_SECTIONS = [
37 37 'alias', 'auth', 'decode/encode', 'defaults', 'diff', 'email', 'extensions',
38 38 'format', 'merge-patterns', 'merge-tools', 'hooks', 'http_proxy', 'smtp',
39 39 'patch', 'paths', 'profiling', 'server', 'trusted', 'ui', 'web',
40 40 ]
41 41
42 42
43 43 class HgWeb(mercurial.hgweb.hgweb_mod.hgweb):
44 44 """Extension of hgweb that simplifies some functions."""
45 45
46 46 def _get_view(self, repo):
47 47 """Views are not supported."""
48 48 return repo
49 49
50 50 def loadsubweb(self):
51 51 """The result is only used in the templater method which is not used."""
52 52 return None
53 53
54 54 def run(self):
55 55 """Unused function so raise an exception if accidentally called."""
56 56 raise NotImplementedError
57 57
58 58 def templater(self, req):
59 59 """Function used in an unreachable code path.
60 60
61 61 This code is unreachable because we guarantee that the HTTP request,
62 62 corresponds to a Mercurial command. See the is_hg method. So, we are
63 63 never going to get a user-visible url.
64 64 """
65 65 raise NotImplementedError
66 66
67 67 def archivelist(self, nodeid):
68 68 """Unused function so raise an exception if accidentally called."""
69 69 raise NotImplementedError
70 70
71 71 def __call__(self, environ, start_response):
72 72 """Run the WSGI application.
73 73
74 74 This may be called by multiple threads.
75 75 """
76 76 from mercurial.hgweb import request as requestmod
77 77 req = requestmod.parserequestfromenv(environ)
78 78 res = requestmod.wsgiresponse(req, start_response)
79 79 gen = self.run_wsgi(req, res)
80 80
81 81 first_chunk = None
82 82
83 83 try:
84 84 data = next(gen)
85 85
86 86 def first_chunk():
87 87 yield data
88 88 except StopIteration:
89 89 pass
90 90
91 91 if first_chunk:
92 92 return itertools.chain(first_chunk(), gen)
93 93 return gen
94 94
95 95 def _runwsgi(self, req, res, repo):
96 96
97 cmd = req.qsparams.get('cmd', '')
97 cmd = req.qsparams.get(b'cmd', '')
98 98 if not mercurial.wireprotoserver.iscmd(cmd):
99 99 # NOTE(marcink): for unsupported commands, we return bad request
100 100 # internally from HG
101 log.warning('cmd: `%s` is not supported by the mercurial wireprotocol v1', cmd)
101 102 from mercurial.hgweb.common import statusmessage
102 103 res.status = statusmessage(mercurial.hgweb.common.HTTP_BAD_REQUEST)
103 res.setbodybytes('')
104 res.setbodybytes(b'')
104 105 return res.sendresponse()
105 106
106 107 return super(HgWeb, self)._runwsgi(req, res, repo)
107 108
108 109
109 110 def make_hg_ui_from_config(repo_config):
110 111 baseui = mercurial.ui.ui()
111 112
112 113 # clean the baseui object
113 114 baseui._ocfg = mercurial.config.config()
114 115 baseui._ucfg = mercurial.config.config()
115 116 baseui._tcfg = mercurial.config.config()
116 117
117 118 for section, option, value in repo_config:
118 119 baseui.setconfig(
119 120 ascii_bytes(section, allow_bytes=True),
120 121 ascii_bytes(option, allow_bytes=True),
121 122 ascii_bytes(value, allow_bytes=True))
122 123
123 124 # make our hgweb quiet so it doesn't print output
124 125 baseui.setconfig(b'ui', b'quiet', b'true')
125 126
126 127 return baseui
127 128
128 129
129 130 def update_hg_ui_from_hgrc(baseui, repo_path):
130 131 path = os.path.join(repo_path, '.hg', 'hgrc')
131 132
132 133 if not os.path.isfile(path):
133 134 log.debug('hgrc file is not present at %s, skipping...', path)
134 135 return
135 136 log.debug('reading hgrc from %s', path)
136 137 cfg = mercurial.config.config()
137 138 cfg.read(ascii_bytes(path))
138 139 for section in HG_UI_SECTIONS:
139 140 for k, v in cfg.items(section):
140 141 log.debug('settings ui from file: [%s] %s=%s', section, k, v)
141 142 baseui.setconfig(
142 143 ascii_bytes(section, allow_bytes=True),
143 144 ascii_bytes(k, allow_bytes=True),
144 145 ascii_bytes(v, allow_bytes=True))
145 146
146 147
147 148 def create_hg_wsgi_app(repo_path, repo_name, config):
148 149 """
149 150 Prepares a WSGI application to handle Mercurial requests.
150 151
151 152 :param config: is a list of 3-item tuples representing a ConfigObject
152 153 (it is the serialized version of the config object).
153 154 """
154 155 log.debug("Creating Mercurial WSGI application")
155 156
156 157 baseui = make_hg_ui_from_config(config)
157 158 update_hg_ui_from_hgrc(baseui, repo_path)
158 159
159 160 try:
160 161 return HgWeb(safe_bytes(repo_path), name=safe_bytes(repo_name), baseui=baseui)
161 162 except mercurial.error.RequirementError as e:
162 163 raise exceptions.RequirementException(e)(e)
163 164
164 165
165 166 class GitHandler(object):
166 167 """
167 168 Handler for Git operations like push/pull etc
168 169 """
169 170 def __init__(self, repo_location, repo_name, git_path, update_server_info,
170 171 extras):
171 172 if not os.path.isdir(repo_location):
172 173 raise OSError(repo_location)
173 174 self.content_path = repo_location
174 175 self.repo_name = repo_name
175 176 self.repo_location = repo_location
176 177 self.extras = extras
177 178 self.git_path = git_path
178 179 self.update_server_info = update_server_info
179 180
180 181 def __call__(self, environ, start_response):
181 182 app = webob.exc.HTTPNotFound()
182 183 candidate_paths = (
183 184 self.content_path, os.path.join(self.content_path, '.git'))
184 185
185 186 for content_path in candidate_paths:
186 187 try:
187 188 app = pygrack.GitRepository(
188 189 self.repo_name, content_path, self.git_path,
189 190 self.update_server_info, self.extras)
190 191 break
191 192 except OSError:
192 193 continue
193 194
194 195 return app(environ, start_response)
195 196
196 197
197 198 def create_git_wsgi_app(repo_path, repo_name, config):
198 199 """
199 200 Creates a WSGI application to handle Git requests.
200 201
201 202 :param config: is a dictionary holding the extras.
202 203 """
203 204 git_path = settings.GIT_EXECUTABLE
204 205 update_server_info = config.pop('git_update_server_info')
205 206 app = GitHandler(
206 207 repo_path, repo_name, git_path, update_server_info, config)
207 208
208 209 return app
209 210
210 211
211 212 class GitLFSHandler(object):
212 213 """
213 214 Handler for Git LFS operations
214 215 """
215 216
216 217 def __init__(self, repo_location, repo_name, git_path, update_server_info,
217 218 extras):
218 219 if not os.path.isdir(repo_location):
219 220 raise OSError(repo_location)
220 221 self.content_path = repo_location
221 222 self.repo_name = repo_name
222 223 self.repo_location = repo_location
223 224 self.extras = extras
224 225 self.git_path = git_path
225 226 self.update_server_info = update_server_info
226 227
227 228 def get_app(self, git_lfs_enabled, git_lfs_store_path, git_lfs_http_scheme):
228 229 app = git_lfs.create_app(git_lfs_enabled, git_lfs_store_path, git_lfs_http_scheme)
229 230 return app
230 231
231 232
232 233 def create_git_lfs_wsgi_app(repo_path, repo_name, config):
233 234 git_path = settings.GIT_EXECUTABLE
234 235 update_server_info = config.pop(b'git_update_server_info')
235 236 git_lfs_enabled = config.pop(b'git_lfs_enabled')
236 237 git_lfs_store_path = config.pop(b'git_lfs_store_path')
237 238 git_lfs_http_scheme = config.pop(b'git_lfs_http_scheme', 'http')
238 239 app = GitLFSHandler(
239 240 repo_path, repo_name, git_path, update_server_info, config)
240 241
241 242 return app.get_app(git_lfs_enabled, git_lfs_store_path, git_lfs_http_scheme)
General Comments 0
You need to be logged in to leave comments. Login now