annotate src/hgext3rd/hggit_serve/_ssh.py @ 15:b65d5922b8ee

Fix formatting in _ssh.py.
author Paul Fisher <paul@pfish.zone>
date Fri, 20 Feb 2026 21:07:11 -0500
parents 00bdfac5416c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
1 from __future__ import annotations
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
2
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
3 import subprocess
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
4 import typing as t
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
5
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
6 from mercurial import error as hgerr
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
7 from mercurial import registrar
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
8
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
9 from . import _export as xp
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
10
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
11 if t.TYPE_CHECKING:
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
12 import mercurial.interfaces.repository as hgrepo
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
13 import mercurial.ui as hgui
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
14
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
15 cmdtable: dict[bytes, object] = {}
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
16 _command = registrar.command(cmdtable)
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
17
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
18
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
19 def _not_git() -> hgerr.StateError:
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
20 return hgerr.StateError(
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
21 b'Git is not enabled for this repository.',
15
b65d5922b8ee Fix formatting in _ssh.py.
Paul Fisher <paul@pfish.zone>
parents: 13
diff changeset
22 hint=(
b65d5922b8ee Fix formatting in _ssh.py.
Paul Fisher <paul@pfish.zone>
parents: 13
diff changeset
23 b'The server administrator should enable the ``hggit`` extension '
b65d5922b8ee Fix formatting in _ssh.py.
Paul Fisher <paul@pfish.zone>
parents: 13
diff changeset
24 b'and run ``hg git-export`` to enable Git access.'
b65d5922b8ee Fix formatting in _ssh.py.
Paul Fisher <paul@pfish.zone>
parents: 13
diff changeset
25 ),
13
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
26 )
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
27
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
28
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
29 def _maybe(flag: bytes, include: bool) -> tuple[bytes, ...]:
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
30 return (flag,) if include else ()
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
31
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
32
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
33 @_command(
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
34 b'git-upload-pack',
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
35 [
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
36 *(
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
37 (b'', opt, False, b'flag passed to git upload-pack')
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
38 for opt in (
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
39 b'strict',
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
40 b'no-strict',
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
41 b'stateless-rpc',
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
42 b'advertise-refs',
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
43 )
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
44 ),
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
45 (b'', b'timeout', -1, b'flag passed to git upload-pack'),
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
46 ],
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
47 helpcategory=b'import',
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
48 intents=(b'readonly',),
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
49 )
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
50 def _git_upload_pack(
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
51 ui: hgui.ui,
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
52 repo: hgrepo.IRepo,
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
53 *,
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
54 strict: bool,
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
55 no_strict: bool,
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
56 stateless_rpc: bool,
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
57 advertise_refs: bool,
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
58 timeout: int,
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
59 ) -> int:
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
60 """Server-side handler for ``git pull``/``git clone``."""
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
61 if not xp.is_gitty(repo):
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
62 raise _not_git()
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
63 timeout_flag = (
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
64 (b'--timeout=' + str(timeout).encode(),) if timeout != -1 else ()
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
65 )
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
66 upload_pack = ui.configlist(
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
67 b'hggit-serve', b'upload-pack', default=(b'git', b'upload-pack')
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
68 )
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
69 return subprocess.call(
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
70 (
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
71 *upload_pack,
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
72 *_maybe(b'--strict', strict),
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
73 *_maybe(b'--no-strict', no_strict),
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
74 *_maybe(b'--stateless-rpc', stateless_rpc),
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
75 *_maybe(b'--advertise_refs', advertise_refs),
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
76 *timeout_flag,
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
77 repo.githandler.gitdir,
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
78 ),
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
79 close_fds=True,
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
80 )
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
81
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
82
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
83 @_command(
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
84 b'git-receive-pack',
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
85 [
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
86 (b'', b'skip-connectivity-check', False, b'passed to git receive-pack'),
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
87 ],
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
88 helpcategory=b'import',
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
89 )
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
90 def _git_receive_pack(
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
91 ui: hgui.ui, repo: hgrepo.IRepo, *, skip_connectivity_check: bool
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
92 ) -> int:
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
93 """Server-side handler for ``git push``."""
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
94 if not xp.is_gitty(repo):
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
95 raise _not_git()
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
96 receive_pack = ui.configlist(
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
97 b'hggit-serve', b'receive-pack', default=(b'git', b'receive-pack')
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
98 )
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
99 try:
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
100 subprocess.check_call(
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
101 (
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
102 *receive_pack,
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
103 *_maybe(b'--skip-connectivity-check', skip_connectivity_check),
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
104 repo.githandler.gitdir,
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
105 ),
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
106 close_fds=True,
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
107 )
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
108 except subprocess.CalledProcessError as cpe:
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
109 return cpe.returncode
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
110 xp.import_all(repo, b'git-receive-pack')
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
111 return 0
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
112
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
113
00bdfac5416c Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
114 __all__ = ('cmdtable',)