Mercurial > hg-git-serve
annotate src/hgext3rd/hggit_serve/_ssh.py @ 13:00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
- Adds git-upload-pack and git-receive-pack as hg subcommands,
to be run on the server side by git push/pull.
- Starts on documentation.
- Cleans up a lot of stuff.
| author | Paul Fisher <paul@pfish.zone> |
|---|---|
| date | Thu, 19 Feb 2026 01:13:56 -0500 |
| parents | |
| children | b65d5922b8ee |
| 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.', |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
22 hint=b'The server administrator should enable the ``hggit`` extension ' |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
23 b'and run ``hg git-export`` to enable Git access.', |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
24 ) |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
25 |
|
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 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
|
28 return (flag,) if include else () |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
29 |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
30 |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
31 @_command( |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
32 b'git-upload-pack', |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
33 [ |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
34 *( |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
35 (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
|
36 for opt in ( |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
37 b'strict', |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
38 b'no-strict', |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
39 b'stateless-rpc', |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
40 b'advertise-refs', |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
41 ) |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
42 ), |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
43 (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
|
44 ], |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
45 helpcategory=b'import', |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
46 intents=(b'readonly',), |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
47 ) |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
48 def _git_upload_pack( |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
49 ui: hgui.ui, |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
50 repo: hgrepo.IRepo, |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
51 *, |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
52 strict: bool, |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
53 no_strict: bool, |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
54 stateless_rpc: bool, |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
55 advertise_refs: bool, |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
56 timeout: int, |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
57 ) -> int: |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
58 """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
|
59 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
|
60 raise _not_git() |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
61 timeout_flag = ( |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
62 (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
|
63 ) |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
64 upload_pack = ui.configlist( |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
65 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
|
66 ) |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
67 return subprocess.call( |
|
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 *upload_pack, |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
70 *_maybe(b'--strict', strict), |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
71 *_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
|
72 *_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
|
73 *_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
|
74 *timeout_flag, |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
75 repo.githandler.gitdir, |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
76 ), |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
77 close_fds=True, |
|
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 |
|
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 @_command( |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
82 b'git-receive-pack', |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
83 [ |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
84 (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
|
85 ], |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
86 helpcategory=b'import', |
|
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 def _git_receive_pack( |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
89 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
|
90 ) -> int: |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
91 """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
|
92 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
|
93 raise _not_git() |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
94 receive_pack = ui.configlist( |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
95 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
|
96 ) |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
97 try: |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
98 subprocess.check_call( |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
99 ( |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
100 *receive_pack, |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
101 *_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
|
102 repo.githandler.gitdir, |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
103 ), |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
104 close_fds=True, |
|
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 except subprocess.CalledProcessError as cpe: |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
107 return cpe.returncode |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
108 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
|
109 return 0 |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
110 |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
111 |
|
00bdfac5416c
Create Git SSH commands and add some documentation. Also cleanup.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
112 __all__ = ('cmdtable',) |
