git-vidi 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- git_vidi-0.1.0.data/data/share/man/man1/git-vidi.1 +162 -0
- git_vidi-0.1.0.data/data/share/man/man1/git-view.1 +162 -0
- git_vidi-0.1.0.data/data/share/man/man1/vidi.1 +162 -0
- git_vidi-0.1.0.dist-info/METADATA +215 -0
- git_vidi-0.1.0.dist-info/RECORD +32 -0
- git_vidi-0.1.0.dist-info/WHEEL +5 -0
- git_vidi-0.1.0.dist-info/entry_points.txt +4 -0
- git_vidi-0.1.0.dist-info/licenses/LICENSE +674 -0
- git_vidi-0.1.0.dist-info/top_level.txt +1 -0
- vidi/__init__.py +5 -0
- vidi/__main__.py +7 -0
- vidi/app.py +267 -0
- vidi/cli.py +119 -0
- vidi/config.py +119 -0
- vidi/data.py +266 -0
- vidi/docs.py +109 -0
- vidi/formatting.py +83 -0
- vidi/models.py +101 -0
- vidi/render.py +399 -0
- vidi/repository.py +657 -0
- vidi/screens/__init__.py +17 -0
- vidi/screens/branch_picker.py +216 -0
- vidi/screens/commit_detail.py +1268 -0
- vidi/screens/commit_files.py +249 -0
- vidi/screens/file_view.py +279 -0
- vidi/screens/help.py +153 -0
- vidi/screens/language_settings.py +129 -0
- vidi/screens/main.py +1118 -0
- vidi/screens/shared.py +35 -0
- vidi/text_view.py +438 -0
- vidi/theme.py +211 -0
- vidi/utils.py +155 -0
vidi/repository.py
ADDED
|
@@ -0,0 +1,657 @@
|
|
|
1
|
+
"""Repository discovery and git-backed data access."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
import subprocess
|
|
7
|
+
import time
|
|
8
|
+
from functools import lru_cache
|
|
9
|
+
from typing import Iterator
|
|
10
|
+
|
|
11
|
+
from .data import parse_patch_sections
|
|
12
|
+
from .models import (
|
|
13
|
+
BranchData,
|
|
14
|
+
CommitDetail,
|
|
15
|
+
CommitFileChange,
|
|
16
|
+
CommitRecord,
|
|
17
|
+
LanguageStat,
|
|
18
|
+
RepoContext,
|
|
19
|
+
RepoScanSnapshot,
|
|
20
|
+
)
|
|
21
|
+
from .utils import (
|
|
22
|
+
detect_language,
|
|
23
|
+
parse_iso_datetime,
|
|
24
|
+
random_color,
|
|
25
|
+
replace_terminal_control,
|
|
26
|
+
unquote_git_path,
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
DEFAULT_IGNORED_DIRS = {
|
|
31
|
+
".git",
|
|
32
|
+
"node_modules",
|
|
33
|
+
".venv",
|
|
34
|
+
"venv",
|
|
35
|
+
"dist",
|
|
36
|
+
"build",
|
|
37
|
+
"target",
|
|
38
|
+
"coverage",
|
|
39
|
+
".next",
|
|
40
|
+
".nuxt",
|
|
41
|
+
"__pycache__",
|
|
42
|
+
".mypy_cache",
|
|
43
|
+
".pytest_cache",
|
|
44
|
+
".idea",
|
|
45
|
+
".vscode",
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
EXTENSION_TO_LANGUAGE = {
|
|
49
|
+
".py": "Python",
|
|
50
|
+
".js": "JavaScript",
|
|
51
|
+
".jsx": "JavaScript",
|
|
52
|
+
".mjs": "JavaScript",
|
|
53
|
+
".cjs": "JavaScript",
|
|
54
|
+
".ts": "TypeScript",
|
|
55
|
+
".tsx": "TypeScript",
|
|
56
|
+
".html": "HTML",
|
|
57
|
+
".htm": "HTML",
|
|
58
|
+
".css": "CSS",
|
|
59
|
+
".java": "Java",
|
|
60
|
+
".c": "C",
|
|
61
|
+
".h": "C/C++ Header",
|
|
62
|
+
".cpp": "C++",
|
|
63
|
+
".cc": "C++",
|
|
64
|
+
".cxx": "C++",
|
|
65
|
+
".hpp": "C++ Header",
|
|
66
|
+
".cs": "C#",
|
|
67
|
+
".rb": "Ruby",
|
|
68
|
+
".go": "Go",
|
|
69
|
+
".php": "PHP",
|
|
70
|
+
".swift": "Swift",
|
|
71
|
+
".kt": "Kotlin",
|
|
72
|
+
".kts": "Kotlin",
|
|
73
|
+
".rs": "Rust",
|
|
74
|
+
".lua": "Lua",
|
|
75
|
+
".sh": "Shell",
|
|
76
|
+
".bash": "Shell",
|
|
77
|
+
".zsh": "Shell",
|
|
78
|
+
".fish": "Shell",
|
|
79
|
+
".json": "JSON",
|
|
80
|
+
".jsonc": "JSON",
|
|
81
|
+
".yaml": "YAML",
|
|
82
|
+
".yml": "YAML",
|
|
83
|
+
".xml": "XML",
|
|
84
|
+
".toml": "TOML",
|
|
85
|
+
".md": "Markdown",
|
|
86
|
+
".sql": "SQL",
|
|
87
|
+
".r": "R",
|
|
88
|
+
".dart": "Dart",
|
|
89
|
+
".scala": "Scala",
|
|
90
|
+
".pl": "Perl",
|
|
91
|
+
".pm": "Perl",
|
|
92
|
+
".m": "Objective-C/MATLAB",
|
|
93
|
+
".mm": "Objective-C++",
|
|
94
|
+
".vb": "Visual Basic",
|
|
95
|
+
".ps1": "PowerShell",
|
|
96
|
+
".bat": "Batchfile",
|
|
97
|
+
".cmd": "Batchfile",
|
|
98
|
+
".dockerfile": "Dockerfile",
|
|
99
|
+
".vue": "Vue",
|
|
100
|
+
".svelte": "Svelte",
|
|
101
|
+
".astro": "Astro",
|
|
102
|
+
".scss": "SCSS",
|
|
103
|
+
".sass": "Sass",
|
|
104
|
+
".less": "Less",
|
|
105
|
+
".tf": "HCL",
|
|
106
|
+
".tfvars": "HCL",
|
|
107
|
+
".hcl": "HCL",
|
|
108
|
+
".ipynb": "Jupyter Notebook",
|
|
109
|
+
".zig": "Zig",
|
|
110
|
+
".ex": "Elixir",
|
|
111
|
+
".exs": "Elixir",
|
|
112
|
+
".erl": "Erlang",
|
|
113
|
+
".hrl": "Erlang",
|
|
114
|
+
".hs": "Haskell",
|
|
115
|
+
".ml": "OCaml",
|
|
116
|
+
".mli": "OCaml",
|
|
117
|
+
".jl": "Julia",
|
|
118
|
+
".nix": "Nix",
|
|
119
|
+
".proto": "Protocol Buffer",
|
|
120
|
+
".clj": "Clojure",
|
|
121
|
+
".cljs": "Clojure",
|
|
122
|
+
".cljc": "Clojure",
|
|
123
|
+
".groovy": "Groovy",
|
|
124
|
+
".mk": "Makefile",
|
|
125
|
+
".cmake": "CMake",
|
|
126
|
+
".mts": "TypeScript",
|
|
127
|
+
".cts": "TypeScript",
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
LANGUAGE_COLORS = {
|
|
131
|
+
"Python": "#3572A5",
|
|
132
|
+
"JavaScript": "#f1e05a",
|
|
133
|
+
"HTML": "#e34c26",
|
|
134
|
+
"CSS": "#563d7c",
|
|
135
|
+
"Java": "#b07219",
|
|
136
|
+
"C": "#555555",
|
|
137
|
+
"C++": "#f34b7d",
|
|
138
|
+
"C#": "#178600",
|
|
139
|
+
"Ruby": "#701516",
|
|
140
|
+
"Go": "#00ADD8",
|
|
141
|
+
"PHP": "#4F5D95",
|
|
142
|
+
"TypeScript": "#3178c6",
|
|
143
|
+
"Swift": "#ffac45",
|
|
144
|
+
"Kotlin": "#A97BFF",
|
|
145
|
+
"Rust": "#dea584",
|
|
146
|
+
"Lua": "#000080",
|
|
147
|
+
"Shell": "#89e051",
|
|
148
|
+
"JSON": "#292929",
|
|
149
|
+
"YAML": "#cb171e",
|
|
150
|
+
"XML": "#0060ac",
|
|
151
|
+
"Markdown": "#083fa1",
|
|
152
|
+
"SQL": "#e38c00",
|
|
153
|
+
"R": "#198CE7",
|
|
154
|
+
"Dart": "#00B4AB",
|
|
155
|
+
"Scala": "#c22d40",
|
|
156
|
+
"Perl": "#0298c3",
|
|
157
|
+
"PowerShell": "#012456",
|
|
158
|
+
"Batchfile": "#C1F12E",
|
|
159
|
+
"Dockerfile": "#384d54",
|
|
160
|
+
"Vue": "#41b883",
|
|
161
|
+
"Svelte": "#ff3e00",
|
|
162
|
+
"Astro": "#ff5a03",
|
|
163
|
+
"SCSS": "#c6538c",
|
|
164
|
+
"Sass": "#a53b70",
|
|
165
|
+
"Less": "#1d365d",
|
|
166
|
+
"HCL": "#844FBA",
|
|
167
|
+
"Jupyter Notebook": "#DA5B0B",
|
|
168
|
+
"Zig": "#ec915c",
|
|
169
|
+
"Elixir": "#6e4a7e",
|
|
170
|
+
"Erlang": "#B83998",
|
|
171
|
+
"Haskell": "#5e5086",
|
|
172
|
+
"OCaml": "#ef7a08",
|
|
173
|
+
"Julia": "#a270ba",
|
|
174
|
+
"Nix": "#7e7eff",
|
|
175
|
+
"Clojure": "#db5855",
|
|
176
|
+
"Groovy": "#4298b8",
|
|
177
|
+
"Makefile": "#427819",
|
|
178
|
+
"CMake": "#DA3434",
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def run_git(repo_dir: str, args: list[str], *, errors: str = "replace") -> str:
|
|
183
|
+
"""Run git and return its output with non-ASCII paths left unescaped.
|
|
184
|
+
|
|
185
|
+
Bytes that are not UTF-8 become U+FFFD unless `errors` is "strict".
|
|
186
|
+
"""
|
|
187
|
+
try:
|
|
188
|
+
output = subprocess.check_output(
|
|
189
|
+
[
|
|
190
|
+
"git",
|
|
191
|
+
"-c",
|
|
192
|
+
"core.fsmonitor=false",
|
|
193
|
+
"-c",
|
|
194
|
+
"core.quotePath=false",
|
|
195
|
+
"-c",
|
|
196
|
+
"log.showSignature=false",
|
|
197
|
+
*args,
|
|
198
|
+
],
|
|
199
|
+
cwd=repo_dir,
|
|
200
|
+
encoding="utf-8",
|
|
201
|
+
errors=errors,
|
|
202
|
+
stderr=subprocess.PIPE,
|
|
203
|
+
)
|
|
204
|
+
except subprocess.CalledProcessError as exc:
|
|
205
|
+
stderr = exc.stderr.strip() if exc.stderr else "unknown git error"
|
|
206
|
+
raise RuntimeError(f"git {' '.join(args)} failed: {stderr}") from exc
|
|
207
|
+
return replace_terminal_control(output)
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
def get_branch_heads(repo_dir: str) -> tuple[list[str], str, dict[str, str]]:
|
|
211
|
+
output = run_git(
|
|
212
|
+
repo_dir,
|
|
213
|
+
["for-each-ref", "--format=%(refname:short)\t%(objectname)", "refs/heads"],
|
|
214
|
+
)
|
|
215
|
+
branch_heads: dict[str, str] = {}
|
|
216
|
+
for raw_line in output.splitlines():
|
|
217
|
+
line = raw_line.strip()
|
|
218
|
+
if not line:
|
|
219
|
+
continue
|
|
220
|
+
name, _, object_name = line.partition("\t")
|
|
221
|
+
if name and object_name:
|
|
222
|
+
branch_heads[name] = object_name
|
|
223
|
+
try:
|
|
224
|
+
head = run_git(repo_dir, ["rev-parse", "--abbrev-ref", "HEAD"]).strip()
|
|
225
|
+
except RuntimeError:
|
|
226
|
+
# An unborn HEAD in a new repo or on an orphan branch.
|
|
227
|
+
head = ""
|
|
228
|
+
if head and head != "HEAD" and head not in branch_heads:
|
|
229
|
+
branch_heads[head] = run_git(repo_dir, ["rev-parse", head]).strip()
|
|
230
|
+
branches = sorted(branch_heads)
|
|
231
|
+
if not branches:
|
|
232
|
+
if not head:
|
|
233
|
+
raise RuntimeError(f"no commits yet: {repo_dir}")
|
|
234
|
+
raise RuntimeError("No branches found in repository.")
|
|
235
|
+
return branches, head if head else branches[0], branch_heads
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
def list_worktree_paths(repo_dir: str) -> list[str]:
|
|
239
|
+
"""Tracked and untracked files that git does not ignore, each listed once."""
|
|
240
|
+
output = run_git(
|
|
241
|
+
repo_dir,
|
|
242
|
+
["ls-files", "-z", "--cached", "--others", "--exclude-standard"],
|
|
243
|
+
)
|
|
244
|
+
# A file with a merge conflict is listed once per stage.
|
|
245
|
+
return list(dict.fromkeys(path for path in output.split("\0") if path))
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
def _in_ignored_dir(path: str, ignore_dirs: set[str]) -> bool:
|
|
249
|
+
return any(component in ignore_dirs for component in path.split("/"))
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
def _scanned_files(
|
|
253
|
+
repo_dir: str, exclude_languages: set[str], ignore_dirs: set[str]
|
|
254
|
+
) -> Iterator[tuple[str, str, str]]:
|
|
255
|
+
"""The relative path, full path, and language of each work-tree file the scan counts."""
|
|
256
|
+
for rel_path in list_worktree_paths(repo_dir):
|
|
257
|
+
if _in_ignored_dir(rel_path, ignore_dirs):
|
|
258
|
+
continue
|
|
259
|
+
full_path = os.path.join(repo_dir, rel_path)
|
|
260
|
+
if not os.path.isfile(full_path):
|
|
261
|
+
continue
|
|
262
|
+
language = detect_language(rel_path, EXTENSION_TO_LANGUAGE)
|
|
263
|
+
if language not in exclude_languages:
|
|
264
|
+
yield rel_path, full_path, language
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
def scan_languages(
|
|
268
|
+
repo_dir: str,
|
|
269
|
+
language_mode: str,
|
|
270
|
+
exclude_languages: set[str],
|
|
271
|
+
ignore_dirs: set[str],
|
|
272
|
+
) -> tuple[list[LanguageStat], dict[str, list[str]]]:
|
|
273
|
+
totals: dict[str, int] = {}
|
|
274
|
+
files_by_language: dict[str, list[str]] = {}
|
|
275
|
+
for rel_path, full_path, language in _scanned_files(repo_dir, exclude_languages, ignore_dirs):
|
|
276
|
+
files_by_language.setdefault(language, []).append(rel_path)
|
|
277
|
+
if language_mode == "files":
|
|
278
|
+
value = 1
|
|
279
|
+
else:
|
|
280
|
+
try:
|
|
281
|
+
value = os.path.getsize(full_path)
|
|
282
|
+
except OSError:
|
|
283
|
+
value = 0
|
|
284
|
+
totals[language] = totals.get(language, 0) + value
|
|
285
|
+
return _language_stats(totals, files_by_language)
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
def _language_stats(
|
|
289
|
+
totals: dict[str, int], files_by_language: dict[str, list[str]]
|
|
290
|
+
) -> tuple[list[LanguageStat], dict[str, list[str]]]:
|
|
291
|
+
"""Per-language shares of the totals, largest first, and the sorted file lists."""
|
|
292
|
+
total_value = sum(totals.values())
|
|
293
|
+
stats: list[LanguageStat] = []
|
|
294
|
+
for language, value in totals.items():
|
|
295
|
+
percentage = (value / total_value * 100.0) if total_value else 0.0
|
|
296
|
+
stats.append(
|
|
297
|
+
{
|
|
298
|
+
"language": language,
|
|
299
|
+
"value": value,
|
|
300
|
+
"percentage": percentage,
|
|
301
|
+
"count": len(files_by_language[language]),
|
|
302
|
+
"color": LANGUAGE_COLORS.get(language, random_color(language)),
|
|
303
|
+
}
|
|
304
|
+
)
|
|
305
|
+
stats.sort(key=lambda item: float(item["percentage"]), reverse=True)
|
|
306
|
+
for language in files_by_language:
|
|
307
|
+
files_by_language[language].sort()
|
|
308
|
+
return stats, files_by_language
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
def _repo_commit_count(repo_dir: str) -> int:
|
|
312
|
+
return int(run_git(repo_dir, ["rev-list", "--all", "--count"]).strip() or "0")
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
def _repo_context(
|
|
316
|
+
repo_dir: str,
|
|
317
|
+
language_stats: list[LanguageStat],
|
|
318
|
+
files_by_language: dict[str, list[str]],
|
|
319
|
+
) -> RepoContext:
|
|
320
|
+
branches, current_branch, branch_heads = get_branch_heads(repo_dir)
|
|
321
|
+
return RepoContext(
|
|
322
|
+
repo_dir=repo_dir,
|
|
323
|
+
project_name=os.path.basename(os.path.abspath(repo_dir)),
|
|
324
|
+
repo_commit_count=_repo_commit_count(repo_dir),
|
|
325
|
+
branches=branches,
|
|
326
|
+
branch_heads=tuple(sorted(branch_heads.items())),
|
|
327
|
+
current_branch=current_branch,
|
|
328
|
+
language_stats=language_stats,
|
|
329
|
+
files_by_language=files_by_language,
|
|
330
|
+
)
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
def _resolve_ignore_dirs(extra_ignored_dirs: set[str]) -> set[str]:
|
|
334
|
+
return DEFAULT_IGNORED_DIRS | set(extra_ignored_dirs)
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
def build_initial_repo_context(
|
|
338
|
+
repo_dir: str,
|
|
339
|
+
language_mode: str,
|
|
340
|
+
exclude_languages: set[str],
|
|
341
|
+
extra_ignored_dirs: set[str],
|
|
342
|
+
) -> tuple[RepoContext, "RepoService"]:
|
|
343
|
+
"""The branch and commit counts without the language scan or any branch history."""
|
|
344
|
+
return _repo_context(repo_dir, [], {}), RepoService(
|
|
345
|
+
repo_dir,
|
|
346
|
+
language_mode=language_mode,
|
|
347
|
+
exclude_languages=exclude_languages,
|
|
348
|
+
ignore_dirs=_resolve_ignore_dirs(extra_ignored_dirs),
|
|
349
|
+
extra_ignored_dirs=extra_ignored_dirs,
|
|
350
|
+
)
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
class RepoService:
|
|
354
|
+
"""Thin cached wrapper over git commands."""
|
|
355
|
+
|
|
356
|
+
def __init__(
|
|
357
|
+
self,
|
|
358
|
+
repo_dir: str,
|
|
359
|
+
*,
|
|
360
|
+
language_mode: str = "bytes",
|
|
361
|
+
exclude_languages: set[str] | None = None,
|
|
362
|
+
ignore_dirs: set[str] | None = None,
|
|
363
|
+
extra_ignored_dirs: set[str] | None = None,
|
|
364
|
+
):
|
|
365
|
+
self.repo_dir = repo_dir
|
|
366
|
+
self.language_mode = language_mode
|
|
367
|
+
self.exclude_languages = set(exclude_languages or set())
|
|
368
|
+
self.ignore_dirs = set(ignore_dirs or set())
|
|
369
|
+
# The user-supplied extras alone, without the defaults.
|
|
370
|
+
self.extra_ignored_dirs = set(extra_ignored_dirs or set())
|
|
371
|
+
self._cached_scanned_file_count: int | None = None
|
|
372
|
+
self._origin_head: str | None = None
|
|
373
|
+
# Per-instance caches. A class-level lru_cache would keep every service alive.
|
|
374
|
+
self.get_branch_data = lru_cache(maxsize=64)(self._branch_data)
|
|
375
|
+
self.get_commit_detail = lru_cache(maxsize=256)(self._commit_detail)
|
|
376
|
+
self.get_commit_language_stats = lru_cache(maxsize=128)(self._commit_language_stats)
|
|
377
|
+
self.get_commit_files = lru_cache(maxsize=128)(self._commit_files)
|
|
378
|
+
self.get_branch_divergence = lru_cache(maxsize=256)(self._branch_divergence)
|
|
379
|
+
|
|
380
|
+
def invalidate_cached_git_data(self) -> None:
|
|
381
|
+
self.get_branch_data.cache_clear()
|
|
382
|
+
self.get_commit_detail.cache_clear()
|
|
383
|
+
self.get_commit_language_stats.cache_clear()
|
|
384
|
+
self.get_commit_files.cache_clear()
|
|
385
|
+
self.get_branch_divergence.cache_clear()
|
|
386
|
+
self._origin_head = None
|
|
387
|
+
|
|
388
|
+
def _branch_divergence(self, branch: str, base: str) -> tuple[int, int]:
|
|
389
|
+
"""Commits `branch` is ahead of and behind `base`, both 0 when they match or
|
|
390
|
+
are unknown."""
|
|
391
|
+
if not base or branch == base:
|
|
392
|
+
return (0, 0)
|
|
393
|
+
try:
|
|
394
|
+
ahead = int(run_git(self.repo_dir, ["rev-list", "--count", "--end-of-options", f"{base}..{branch}"]).strip() or "0")
|
|
395
|
+
behind = int(run_git(self.repo_dir, ["rev-list", "--count", "--end-of-options", f"{branch}..{base}"]).strip() or "0")
|
|
396
|
+
except (RuntimeError, ValueError):
|
|
397
|
+
return (0, 0)
|
|
398
|
+
return (ahead, behind)
|
|
399
|
+
|
|
400
|
+
def trunk_divergence(self, branch: str, branches: list[str]) -> tuple[int, int]:
|
|
401
|
+
"""Commits `branch` is ahead of and behind the default branch."""
|
|
402
|
+
base = self.default_base_branch(branches)
|
|
403
|
+
return self.get_branch_divergence(branch, base) if base else (0, 0)
|
|
404
|
+
|
|
405
|
+
def default_base_branch(self, branches: list[str]) -> str | None:
|
|
406
|
+
"""The trunk to measure branches against, or None if there is none.
|
|
407
|
+
|
|
408
|
+
Prefers the local branch that origin/HEAD points at, then main, then master.
|
|
409
|
+
"""
|
|
410
|
+
if self._origin_head is None:
|
|
411
|
+
self._origin_head = self._read_origin_head()
|
|
412
|
+
for candidate in (self._origin_head, "main", "master"):
|
|
413
|
+
if candidate and candidate in branches:
|
|
414
|
+
return candidate
|
|
415
|
+
return None
|
|
416
|
+
|
|
417
|
+
def _read_origin_head(self) -> str:
|
|
418
|
+
"""The branch name origin/HEAD points at, or an empty string."""
|
|
419
|
+
try:
|
|
420
|
+
target = run_git(
|
|
421
|
+
self.repo_dir, ["symbolic-ref", "--quiet", "refs/remotes/origin/HEAD"]
|
|
422
|
+
).strip()
|
|
423
|
+
except RuntimeError:
|
|
424
|
+
return ""
|
|
425
|
+
prefix = "refs/remotes/origin/"
|
|
426
|
+
return target.removeprefix(prefix) if target.startswith(prefix) else ""
|
|
427
|
+
|
|
428
|
+
def update_language_settings(
|
|
429
|
+
self,
|
|
430
|
+
language_mode: str,
|
|
431
|
+
exclude_languages: set[str],
|
|
432
|
+
extra_ignored_dirs: set[str],
|
|
433
|
+
) -> None:
|
|
434
|
+
"""Re-point the language scan at new settings and drop the stale cache."""
|
|
435
|
+
self.language_mode = language_mode
|
|
436
|
+
self.exclude_languages = set(exclude_languages)
|
|
437
|
+
self.extra_ignored_dirs = set(extra_ignored_dirs)
|
|
438
|
+
self.ignore_dirs = _resolve_ignore_dirs(self.extra_ignored_dirs)
|
|
439
|
+
self.get_commit_language_stats.cache_clear()
|
|
440
|
+
self.get_commit_files.cache_clear()
|
|
441
|
+
# Recount the header file count with the new excludes.
|
|
442
|
+
self._cached_scanned_file_count = self._count_scanned_files()
|
|
443
|
+
|
|
444
|
+
def _commit_language_stats(
|
|
445
|
+
self, commit_hash: str
|
|
446
|
+
) -> tuple[list[LanguageStat], dict[str, list[str]]]:
|
|
447
|
+
"""Language breakdown of the whole tree at a commit from `git ls-tree`,
|
|
448
|
+
with the same excludes as the working-tree scan."""
|
|
449
|
+
output = run_git(self.repo_dir, ["ls-tree", "-r", "-l", "-z", commit_hash])
|
|
450
|
+
totals: dict[str, int] = {}
|
|
451
|
+
files_by_language: dict[str, list[str]] = {}
|
|
452
|
+
for record in output.split("\0"):
|
|
453
|
+
meta, _, path = record.partition("\t")
|
|
454
|
+
if not path:
|
|
455
|
+
continue
|
|
456
|
+
parts = meta.split()
|
|
457
|
+
if len(parts) < 4 or parts[1] != "blob":
|
|
458
|
+
continue
|
|
459
|
+
if _in_ignored_dir(path, self.ignore_dirs):
|
|
460
|
+
continue
|
|
461
|
+
language = detect_language(path, EXTENSION_TO_LANGUAGE)
|
|
462
|
+
if language in self.exclude_languages:
|
|
463
|
+
continue
|
|
464
|
+
files_by_language.setdefault(language, []).append(path)
|
|
465
|
+
if self.language_mode == "files":
|
|
466
|
+
value = 1
|
|
467
|
+
else:
|
|
468
|
+
try:
|
|
469
|
+
value = int(parts[3])
|
|
470
|
+
except ValueError:
|
|
471
|
+
value = 0
|
|
472
|
+
totals[language] = totals.get(language, 0) + value
|
|
473
|
+
return _language_stats(totals, files_by_language)
|
|
474
|
+
|
|
475
|
+
def _commit_files(self, commit_hash: str) -> list[str]:
|
|
476
|
+
"""Every file in the tree at a commit, sorted and without ignored directories."""
|
|
477
|
+
output = run_git(self.repo_dir, ["ls-tree", "-r", "--name-only", "-z", commit_hash])
|
|
478
|
+
paths = (path for path in output.split("\0") if path)
|
|
479
|
+
return sorted(path for path in paths if not _in_ignored_dir(path, self.ignore_dirs))
|
|
480
|
+
|
|
481
|
+
def get_cached_scanned_file_count(self) -> int | None:
|
|
482
|
+
return self._cached_scanned_file_count
|
|
483
|
+
|
|
484
|
+
def scanned_file_count(self, context: RepoContext) -> int:
|
|
485
|
+
"""Scanned file count, preferring the cached scan and falling back to
|
|
486
|
+
the per-language file totals from the context."""
|
|
487
|
+
return self.get_cached_scanned_file_count() or sum(
|
|
488
|
+
len(files) for files in context.files_by_language.values()
|
|
489
|
+
)
|
|
490
|
+
|
|
491
|
+
def get_file_at_commit(self, commit_hash: str, path: str) -> str | None:
|
|
492
|
+
"""The text of a file at a commit, or None if it is binary, missing, or
|
|
493
|
+
unreadable."""
|
|
494
|
+
try:
|
|
495
|
+
content = run_git(self.repo_dir, ["show", f"{commit_hash}:{path}"], errors="strict")
|
|
496
|
+
except (RuntimeError, UnicodeDecodeError):
|
|
497
|
+
return None
|
|
498
|
+
return None if "\x00" in content else content
|
|
499
|
+
|
|
500
|
+
def _count_scanned_files(self) -> int:
|
|
501
|
+
files = _scanned_files(self.repo_dir, self.exclude_languages, self.ignore_dirs)
|
|
502
|
+
return sum(1 for _ in files)
|
|
503
|
+
|
|
504
|
+
def scan_repo_state(self) -> tuple[RepoScanSnapshot, float]:
|
|
505
|
+
started = time.perf_counter()
|
|
506
|
+
branches, current_branch, branch_heads = get_branch_heads(self.repo_dir)
|
|
507
|
+
repo_commit_count = _repo_commit_count(self.repo_dir)
|
|
508
|
+
scanned_file_count = self._count_scanned_files()
|
|
509
|
+
self._cached_scanned_file_count = scanned_file_count
|
|
510
|
+
snapshot = RepoScanSnapshot(
|
|
511
|
+
branch_heads=tuple(sorted(branch_heads.items())),
|
|
512
|
+
current_branch=current_branch,
|
|
513
|
+
repo_commit_count=repo_commit_count,
|
|
514
|
+
scanned_file_count=scanned_file_count,
|
|
515
|
+
)
|
|
516
|
+
return snapshot, time.perf_counter() - started
|
|
517
|
+
|
|
518
|
+
def refresh_context(self) -> RepoContext:
|
|
519
|
+
language_stats, files_by_language = scan_languages(
|
|
520
|
+
self.repo_dir,
|
|
521
|
+
self.language_mode,
|
|
522
|
+
self.exclude_languages,
|
|
523
|
+
self.ignore_dirs,
|
|
524
|
+
)
|
|
525
|
+
scanned_file_count = sum(len(files) for files in files_by_language.values())
|
|
526
|
+
self._cached_scanned_file_count = scanned_file_count
|
|
527
|
+
return _repo_context(self.repo_dir, language_stats, files_by_language)
|
|
528
|
+
|
|
529
|
+
def _branch_data(self, branch: str) -> BranchData:
|
|
530
|
+
meta_out = run_git(
|
|
531
|
+
self.repo_dir,
|
|
532
|
+
[
|
|
533
|
+
"log",
|
|
534
|
+
"--date=iso-strict",
|
|
535
|
+
"--pretty=format:%H%x1f%ad%x1f%an%x1f%ae%x1f%s",
|
|
536
|
+
"--end-of-options",
|
|
537
|
+
branch,
|
|
538
|
+
],
|
|
539
|
+
)
|
|
540
|
+
# A merge lists its files against the first parent, like the commit detail.
|
|
541
|
+
# Older git ignores the config and repeats a merge once per parent. Only the first list is kept.
|
|
542
|
+
files_out = run_git(
|
|
543
|
+
self.repo_dir,
|
|
544
|
+
[
|
|
545
|
+
"-c",
|
|
546
|
+
"log.diffMerges=first-parent",
|
|
547
|
+
"log",
|
|
548
|
+
"-m",
|
|
549
|
+
"--find-renames",
|
|
550
|
+
"--pretty=format:%x01%H",
|
|
551
|
+
"--name-only",
|
|
552
|
+
"--end-of-options",
|
|
553
|
+
branch,
|
|
554
|
+
],
|
|
555
|
+
)
|
|
556
|
+
|
|
557
|
+
files_by_commit: dict[str, list[str]] = {}
|
|
558
|
+
current_hash = ""
|
|
559
|
+
for line in files_out.split("\n"):
|
|
560
|
+
if not line:
|
|
561
|
+
continue
|
|
562
|
+
# Git quotes paths with control characters. No path line starts with \x01.
|
|
563
|
+
if line.startswith("\x01"):
|
|
564
|
+
commit_hash = line[1:]
|
|
565
|
+
current_hash = "" if commit_hash in files_by_commit else commit_hash
|
|
566
|
+
files_by_commit.setdefault(commit_hash, [])
|
|
567
|
+
continue
|
|
568
|
+
if current_hash:
|
|
569
|
+
files_by_commit[current_hash].append(unquote_git_path(line))
|
|
570
|
+
|
|
571
|
+
commits: list[CommitRecord] = []
|
|
572
|
+
for line in meta_out.splitlines():
|
|
573
|
+
if not line.strip():
|
|
574
|
+
continue
|
|
575
|
+
parts = line.split("\x1f", 4)
|
|
576
|
+
if len(parts) != 5:
|
|
577
|
+
continue
|
|
578
|
+
commit_hash, authored_raw, author_name, author_email, message = parts
|
|
579
|
+
commits.append(
|
|
580
|
+
CommitRecord(
|
|
581
|
+
commit_hash=commit_hash,
|
|
582
|
+
authored_at=parse_iso_datetime(authored_raw),
|
|
583
|
+
message=message.strip(),
|
|
584
|
+
author_name=author_name.strip(),
|
|
585
|
+
author_email=author_email.strip(),
|
|
586
|
+
changed_files=tuple(files_by_commit.get(commit_hash, [])),
|
|
587
|
+
)
|
|
588
|
+
)
|
|
589
|
+
|
|
590
|
+
years = sorted({item.authored_date.year for item in commits})
|
|
591
|
+
return BranchData(branch=branch, commits=commits, years=years)
|
|
592
|
+
|
|
593
|
+
def _commit_detail(self, commit_hash: str) -> CommitDetail:
|
|
594
|
+
meta_out = run_git(
|
|
595
|
+
self.repo_dir,
|
|
596
|
+
[
|
|
597
|
+
"show",
|
|
598
|
+
"--no-patch",
|
|
599
|
+
"--date=iso-strict",
|
|
600
|
+
"--format=%H%n%P%n%an%n%ae%n%ad%n%s%n%b",
|
|
601
|
+
commit_hash,
|
|
602
|
+
],
|
|
603
|
+
)
|
|
604
|
+
meta_lines = meta_out.splitlines()
|
|
605
|
+
if len(meta_lines) < 6:
|
|
606
|
+
raise RuntimeError(f"Could not parse commit metadata for {commit_hash}")
|
|
607
|
+
full_hash, parents_raw, author_name, author_email, authored_raw, subject = meta_lines[:6]
|
|
608
|
+
body = "\n".join(meta_lines[6:]).strip()
|
|
609
|
+
parents = tuple(part for part in parents_raw.split() if part)
|
|
610
|
+
|
|
611
|
+
# A merge is compared with its first parent, like the commit table.
|
|
612
|
+
compared = (
|
|
613
|
+
["--end-of-options", parents[0], full_hash]
|
|
614
|
+
if parents
|
|
615
|
+
else ["--root", "--end-of-options", full_hash]
|
|
616
|
+
)
|
|
617
|
+
diff_tree = ["diff-tree", "-r", "--no-commit-id", "--find-renames", "--no-textconv"]
|
|
618
|
+
numstat_out = run_git(self.repo_dir, [*diff_tree, "--numstat", "-z", *compared])
|
|
619
|
+
patch_out = run_git(self.repo_dir, [*diff_tree, "--patch", *compared])
|
|
620
|
+
|
|
621
|
+
file_changes: list[CommitFileChange] = []
|
|
622
|
+
records = numstat_out.split("\0")
|
|
623
|
+
index = 0
|
|
624
|
+
while index < len(records):
|
|
625
|
+
parts = records[index].split("\t", 2)
|
|
626
|
+
index += 1
|
|
627
|
+
if len(parts) != 3:
|
|
628
|
+
continue
|
|
629
|
+
added_raw, deleted_raw, path = parts
|
|
630
|
+
if not path:
|
|
631
|
+
# A rename lists its old and new paths as the next two records.
|
|
632
|
+
path = records[index + 1] if index + 1 < len(records) else ""
|
|
633
|
+
index += 2
|
|
634
|
+
if not path:
|
|
635
|
+
continue
|
|
636
|
+
additions = None if added_raw == "-" else int(added_raw)
|
|
637
|
+
deletions = None if deleted_raw == "-" else int(deleted_raw)
|
|
638
|
+
file_changes.append(
|
|
639
|
+
CommitFileChange(
|
|
640
|
+
path=path,
|
|
641
|
+
additions=additions,
|
|
642
|
+
deletions=deletions,
|
|
643
|
+
)
|
|
644
|
+
)
|
|
645
|
+
|
|
646
|
+
return CommitDetail(
|
|
647
|
+
commit_hash=full_hash,
|
|
648
|
+
parents=parents,
|
|
649
|
+
author_name=author_name,
|
|
650
|
+
author_email=author_email,
|
|
651
|
+
authored_at=parse_iso_datetime(authored_raw),
|
|
652
|
+
subject=subject,
|
|
653
|
+
body=body,
|
|
654
|
+
file_changes=tuple(file_changes),
|
|
655
|
+
patch_sections=parse_patch_sections(patch_out),
|
|
656
|
+
patch=patch_out.rstrip(),
|
|
657
|
+
)
|
vidi/screens/__init__.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"""Screen exports."""
|
|
2
|
+
|
|
3
|
+
from .branch_picker import BranchPickerScreen
|
|
4
|
+
from .commit_detail import CommitDetailScreen
|
|
5
|
+
from .commit_files import CommitFilesScreen
|
|
6
|
+
from .file_view import FileViewScreen
|
|
7
|
+
from .help import HelpScreen
|
|
8
|
+
from .main import MainScreen
|
|
9
|
+
|
|
10
|
+
__all__ = [
|
|
11
|
+
"BranchPickerScreen",
|
|
12
|
+
"CommitDetailScreen",
|
|
13
|
+
"CommitFilesScreen",
|
|
14
|
+
"FileViewScreen",
|
|
15
|
+
"HelpScreen",
|
|
16
|
+
"MainScreen",
|
|
17
|
+
]
|