1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
"""
Gitweb configuration: settings, config file loading, loadavg, features, snapshot formats.
Ported from gitweb/gitweb.perl (evaluate_gitweb_config, read_config_file, get_loadavg,
check_loadavg, known_snapshot_formats, feature_*, gitweb_get_feature, gitweb_check_feature,
filter_snapshot_fmts, filter_and_validate_refs, configure_gitweb_features, get_branch_refs).
"""
from __future__ import annotations
import os
import re
from pathlib import Path
from typing import Any
# Allowed actions (from %actions in gitweb.perl)
ACTIONS = {
"blame",
"blame_incremental",
"blame_data",
"blobdiff",
"blobpatch",
"blob",
"blob_plain",
"commitdiff",
"commit",
"config",
"heads",
"history",
"log",
"patch",
"patches",
"remotes",
"rss",
"atom",
"search",
"search_help",
"shortlog",
"summary",
"tag",
"tags",
"tree",
"snapshot",
"object",
"opml",
"project_list",
"project_index",
}
# Map file extension to highlight.js language (class name)
BLOB_LANG = {
"py": "python",
"js": "javascript",
"ts": "typescript",
"jsx": "javascript",
"tsx": "typescript",
"html": "html",
"htm": "html",
"css": "css",
"scss": "scss",
"json": "json",
"md": "markdown",
"sh": "bash",
"bash": "bash",
"yml": "yaml",
"yaml": "yaml",
"xml": "xml",
"go": "go",
"rs": "rust",
"java": "java",
"c": "c",
"h": "c",
"cpp": "cpp",
"cc": "cpp",
"cxx": "cpp",
"sql": "sql",
"r": "r",
"rb": "ruby",
"php": "php",
"swift": "swift",
"kt": "kotlin",
"vue": "xml",
"toml": "toml",
"ini": "ini",
"cfg": "ini",
"dockerfile": "dockerfile",
}
# Defaults (equivalent to @GITWEB_*@ in gitweb.perl)
PROJECTROOT = os.environ.get("GITWEB_PROJECTROOT", str(Path.home()))
PROJECT_MAXDEPTH = int(os.environ.get("GITWEB_PROJECT_MAXDEPTH", "1"))
PROJECTS_LIST = os.environ.get("GITWEB_LIST", PROJECTROOT)
SITE_NAME = os.environ.get("GITWEB_SITENAME", "") or "DistGit"
EXPORT_OK = os.environ.get("GITWEB_EXPORT_OK", "")
# When True, list all directories under project root without repo/export_ok checks (default on for now).
LIST_ALL = os.environ.get("GITWEB_LIST_ALL", "1").lower() in ("1", "true", "yes")
STRICT_EXPORT = os.environ.get("GITWEB_STRICT_EXPORT", "0").lower() in (
"1",
"true",
"yes",
)
GIT_BINDIR = os.environ.get("GIT_BINDIR", "")
GIT = (GIT_BINDIR + "/git") if GIT_BINDIR else "git"
MAXLOAD: float | None = None # 300 in perl; None = disabled
"""
The auth provider to use. "None" disables authentication entirely, bypassing distgit. USE WITH CAUTION.
RootProvider: Stub which provides admin login with a user/password. Parent class for other providers.
SSHProvider: TODO - Will authenticate using existing SSH keys. Easiest coming from raw git-daemon.
OAuth2Provider: TODO - Will authenticate against an OAuth2 server.
OIDCProvider: TODO - Will authenticate against an OIDC server.
MatrixProvider: TODO - Will authenticate against the Matrix network.
LDAPProvider: TODO - Will authenticate against a local LDAP server.
"""
DISTGIT_AUTH: str = os.environ.get("DISTGIT_AUTH", "None")
DISTGIT_ADMIN_USER: str = os.environ.get("DISTGIT_ADMIN_USER", None)
DISTGIT_ADMIN_PASSWORD: str = os.environ.get("DISTGIT_ADMIN_PASSWORD", None)
DISTGIT_SESSION_TIMEOUT: str = os.environ.get("DISTGIT_SESSION_TIMEOUT", 3600 * 24 * 7)
# Config file paths (can be overridden by env)
GITWEB_CONFIG = os.environ.get("GITWEB_CONFIG", "")
GITWEB_CONFIG_SYSTEM = os.environ.get("GITWEB_CONFIG_SYSTEM", "")
GITWEB_CONFIG_COMMON = os.environ.get("GITWEB_CONFIG_COMMON", "")
# Snapshot formats (from %known_snapshot_formats)
KNOWN_SNAPSHOT_FORMATS: dict[str, dict[str, Any]] = {
"tgz": {
"display": "tar.gz",
"type": "application/x-gzip",
"suffix": ".tar.gz",
"format": "tar",
"compressor": ["gzip", "-n"],
},
"tbz2": {
"display": "tar.bz2",
"type": "application/x-bzip2",
"suffix": ".tar.bz2",
"format": "tar",
"compressor": ["bzip2"],
},
"txz": {
"display": "tar.xz",
"type": "application/x-xz",
"suffix": ".tar.xz",
"format": "tar",
"compressor": ["xz"],
"disabled": True,
},
"zip": {
"display": "zip",
"type": "application/zip",
"suffix": ".zip",
"format": "zip",
},
}
KNOWN_SNAPSHOT_FORMAT_ALIASES: dict[str, str | None] = {
"gzip": "tgz",
"bzip2": "tbz2",
"xz": "txz",
"x-gzip": None,
"gz": None,
"x-bzip2": None,
"bz2": None,
"x-zip": None,
"": None,
}
# Feature defaults; override in config or per-repo
_feature_snapshot_default = ["tgz"]
_snapshot_fmts: list[str] = []
_extra_branch_refs: list[str] = []
def read_config_file(filename: str | None) -> bool:
"""Load and execute a Python config file. Returns True on success. Port of read_config_file."""
if not filename or not os.path.exists(filename):
return False
try:
with open(filename) as f:
code = compile(f.read(), filename, "exec")
glob = {
"PROJECTROOT": PROJECTROOT,
"PROJECTS_LIST": PROJECTS_LIST,
"SITE_NAME": SITE_NAME,
"EXPORT_OK": EXPORT_OK,
"LIST_ALL": LIST_ALL,
"STRICT_EXPORT": STRICT_EXPORT,
"GIT": GIT,
"MAXLOAD": MAXLOAD,
"KNOWN_SNAPSHOT_FORMATS": KNOWN_SNAPSHOT_FORMATS,
"os": os,
"Path": Path,
}
exec(code, glob)
for k in (
"PROJECTROOT",
"PROJECTS_LIST",
"SITE_NAME",
"EXPORT_OK",
"LIST_ALL",
"STRICT_EXPORT",
"GIT",
"MAXLOAD",
"KNOWN_SNAPSHOT_FORMATS",
):
if k in glob:
globals()[k] = glob[k]
return True
except Exception:
raise
def evaluate_gitweb_config() -> None:
"""Resolve config paths and load common + instance/system config. Port of evaluate_gitweb_config."""
global GITWEB_CONFIG, GITWEB_CONFIG_SYSTEM, GITWEB_CONFIG_COMMON
if not GITWEB_CONFIG:
GITWEB_CONFIG = os.environ.get("GITWEB_CONFIG", "")
if not GITWEB_CONFIG_SYSTEM:
GITWEB_CONFIG_SYSTEM = os.environ.get("GITWEB_CONFIG_SYSTEM", "")
if not GITWEB_CONFIG_COMMON:
GITWEB_CONFIG_COMMON = os.environ.get("GITWEB_CONFIG_COMMON", "")
if GITWEB_CONFIG == GITWEB_CONFIG_COMMON:
GITWEB_CONFIG = ""
if GITWEB_CONFIG_SYSTEM == GITWEB_CONFIG_COMMON:
GITWEB_CONFIG_SYSTEM = ""
if GITWEB_CONFIG_COMMON and os.path.exists(GITWEB_CONFIG_COMMON):
read_config_file(GITWEB_CONFIG_COMMON)
if GITWEB_CONFIG and os.path.exists(GITWEB_CONFIG):
read_config_file(GITWEB_CONFIG)
return
if GITWEB_CONFIG_SYSTEM and os.path.exists(GITWEB_CONFIG_SYSTEM):
read_config_file(GITWEB_CONFIG_SYSTEM)
def get_loadavg() -> float:
"""First element of load average, or 0 if unavailable. Port of get_loadavg."""
try:
return os.getloadavg()[0]
except (OSError, AttributeError):
pass
try:
with open("/proc/loadavg") as f:
return float(f.read().split()[0])
except (OSError, ValueError):
return 0.0
def check_loadavg() -> None:
"""Raise 503 if load exceeds maxload. Port of check_loadavg."""
if MAXLOAD is not None and get_loadavg() > MAXLOAD:
raise RuntimeError("503:The load average on the server is too high")
def gitweb_get_feature(
name: str,
git_dir: str | None = None,
get_project_config: Any = None,
) -> list[Any]:
"""Return feature value(s); project override when git_dir and get_project_config set. Port of gitweb_get_feature."""
if name == "snapshot":
defaults = _feature_snapshot_default
if git_dir and get_project_config:
val = get_project_config("snapshot") if callable(get_project_config) else None
if val:
defaults = (
[] if val.strip().lower() == "none" else [x.strip() for x in re.split(r"[\s,]+", val) if x.strip()]
)
return list(defaults)
if name == "avatar":
return ["gravatar"] # default
if name == "extra-branch-refs":
if git_dir and get_project_config and callable(get_project_config):
val = get_project_config("extrabranchrefs")
if val:
parts = [val] if isinstance(val, str) else (val if isinstance(val, list) else [])
return [x for part in parts for x in str(part).split()]
return []
return []
def gitweb_check_feature(name: str, git_dir: str | None = None, get_project_config: Any = None) -> bool | Any:
"""First value of gitweb_get_feature. Port of gitweb_check_feature."""
vals = gitweb_get_feature(name, git_dir, get_project_config)
return vals[0] if vals else False
def filter_snapshot_fmts(fmts: list[str]) -> list[str]:
"""Resolve aliases and drop unknown/disabled. Port of filter_snapshot_fmts."""
result = []
for f in fmts:
key = KNOWN_SNAPSHOT_FORMAT_ALIASES.get(f, f)
if key is None:
continue
if key not in KNOWN_SNAPSHOT_FORMATS:
continue
opt = KNOWN_SNAPSHOT_FORMATS[key]
if opt.get("disabled"):
continue
result.append(key)
return result
def filter_and_validate_refs(refs: list[str], is_valid_ref_format: Any) -> list[str]:
"""Validate ref names and unique sort; 'heads' omitted (added in get_branch_refs).
Port of filter_and_validate_refs.
"""
seen: set[str] = set()
for ref in refs:
if not is_valid_ref_format(ref):
raise ValueError(f"Invalid ref '{ref}' in 'extra-branch-refs' feature")
if ref != "heads":
seen.add(ref)
return sorted(seen)
def configure_gitweb_features(
get_project_config: Any = None,
git_dir: str | None = None,
is_valid_ref_format: Any = None,
) -> None:
"""Set snapshot_fmts and extra_branch_refs. Port of configure_gitweb_features."""
global _snapshot_fmts, _extra_branch_refs
_snapshot_fmts = filter_snapshot_fmts(gitweb_get_feature("snapshot", git_dir, get_project_config))
avatar = gitweb_get_feature("avatar", git_dir, get_project_config)
if avatar and avatar[0] not in ("gravatar", "picon"):
avatar = [""]
raw = gitweb_get_feature("extra-branch-refs", git_dir, get_project_config)
_extra_branch_refs = filter_and_validate_refs(raw, is_valid_ref_format) if is_valid_ref_format else []
def get_branch_refs() -> list[str]:
"""Return ['heads', ...extra_branch_refs]. Port of get_branch_refs."""
return ["heads"] + _extra_branch_refs
def get_snapshot_fmts() -> list[str]:
return _snapshot_fmts