-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
893 lines (747 loc) · 31.2 KB
/
app.py
File metadata and controls
893 lines (747 loc) · 31.2 KB
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
#!/usr/bin/env python3
"""Commit Explorer — Interactive TUI for exploring git repository history."""
import asyncio
import os
import re
import shutil
import sys
import tempfile
from abc import ABC, abstractmethod
from datetime import datetime, timezone, timedelta
from typing import NamedTuple, Optional
from urllib.parse import quote
import webbrowser
from dotenv import load_dotenv
from rich.markup import escape
from rich.text import Text
from textual import on, work
from textual.app import App, ComposeResult
from textual.binding import Binding
from textual.containers import Horizontal, ScrollableContainer, Vertical
from textual.widget import Widget
from textual.widgets import (
Button,
Footer,
Header,
Input,
Label,
ListItem,
ListView,
LoadingIndicator,
Select,
Static,
)
load_dotenv(override=True)
# ── Types ─────────────────────────────────────────────────────────────────────
class CommitInfo(NamedTuple):
sha: str
short_sha: str
message: str
author: str
author_email: str
date: str # ISO format
parents: list[str]
class FileChange(NamedTuple):
filename: str
status: str # added, modified, removed, renamed, etc.
additions: int
deletions: int
class CommitDetail(NamedTuple):
info: CommitInfo
stats: dict[str, int]
files: list[FileChange]
refs: list[str] # issue refs
linked_prs: list[dict] # simplified PR info
class RepoInfo(NamedTuple):
description: str
created_at: str
default_branch: str
language: str
stars: int
forks: int
open_issues: int
branches: Optional[int]
total_commits: Optional[int]
# ── Providers (URL builders only) ─────────────────────────────────────────────
class GitProvider(ABC):
@property
@abstractmethod
def name(self) -> str:
pass
@abstractmethod
def clone_url(self, owner: str, repo: str) -> str:
"""Return the git clone URL."""
pass
@abstractmethod
def commit_url(self, owner: str, repo: str, sha: str) -> str:
"""Return a browser URL for the given commit."""
pass
class GitHubProvider(GitProvider):
@property
def name(self) -> str:
return "GitHub"
def clone_url(self, owner: str, repo: str) -> str:
token = os.getenv("GITHUB_TOKEN", "")
creds = f"{token}@" if token else ""
return f"https://{creds}github.com/{quote(owner, safe='')}/{quote(repo, safe='')}.git"
def commit_url(self, owner: str, repo: str, sha: str) -> str:
return f"https://github.com/{owner}/{repo}/commit/{sha}"
class GitLabProvider(GitProvider):
def __init__(self) -> None:
base = os.getenv("GITLAB_URL", "https://gitlab.com").rstrip("/")
if "/api/" in base:
base = base.split("/api/")[0]
self._host = base
@property
def name(self) -> str:
return "GitLab"
def clone_url(self, owner: str, repo: str) -> str:
token = os.getenv("GITLAB_TOKEN", "")
creds = f"oauth2:{token}@" if token else ""
host_no_scheme = re.sub(r'^https?://', '', self._host)
scheme = "https://" if self._host.startswith("https") else "http://"
return f"{scheme}{creds}{host_no_scheme}/{quote(owner, safe='')}/{quote(repo, safe='')}.git"
def commit_url(self, owner: str, repo: str, sha: str) -> str:
return f"{self._host}/{owner}/{repo}/-/commit/{sha}"
class AzureDevOpsProvider(GitProvider):
def __init__(self) -> None:
self._org = os.getenv("AZURE_DEVOPS_ORG", "")
@property
def name(self) -> str:
return "Azure DevOps"
def clone_url(self, owner: str, repo: str) -> str:
token = os.getenv("AZURE_DEVOPS_TOKEN", "")
creds = f":{token}@" if token else ""
return f"https://{creds}dev.azure.com/{self._org}/{quote(owner, safe='')}/{quote(repo, safe='')}/_git/{quote(repo, safe='')}"
def commit_url(self, owner: str, repo: str, sha: str) -> str:
return f"https://dev.azure.com/{self._org}/{owner}/_git/{repo}/commit/{sha}"
# ── Git Backend (Dulwich) ──────────────────────────────────────────────────────
class _GitBackend:
"""Bare-clone git backend using Dulwich. Stores the clone in a temp dir."""
_PER_PAGE = 30
def __init__(self) -> None:
self._tmpdir: Optional[str] = None
self._commits: list[CommitInfo] = []
self._graph_data: list[tuple[CommitInfo, list]] = []
self._shown: int = 0
@property
def all_commits(self) -> list[CommitInfo]:
return self._commits
@property
def graph_data(self) -> list[tuple[CommitInfo, list]]:
return self._graph_data
@property
def shown(self) -> int:
return self._shown
def has_more(self) -> bool:
return self._shown < len(self._graph_data)
def next_page(self) -> list[tuple[CommitInfo, list]]:
end = min(self._shown + self._PER_PAGE, len(self._graph_data))
page = self._graph_data[self._shown:end]
self._shown = end
return page
async def load(self, url: str, depth: Optional[int] = None) -> None:
self.cleanup()
self._tmpdir = tempfile.mkdtemp(prefix="cex-")
def _do_clone() -> None:
import io
import os
import urllib3
import ssl
from dulwich import porcelain
# Check if SSL verification should be disabled
disable_ssl_verify = os.getenv("GIT_SSL_NO_VERIFY", "").lower() in ("1", "true", "yes")
# Setup clone kwargs
clone_kwargs = {}
if disable_ssl_verify:
# Disable warnings about unverified requests
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# Create a custom pool manager that doesn't verify certificates
# and pass it through to the GitClient
pool_manager = urllib3.PoolManager(
cert_reqs=ssl.CERT_NONE,
assert_hostname=False
)
clone_kwargs['pool_manager'] = pool_manager
porcelain.clone(
url,
target=self._tmpdir,
depth=depth,
bare=True,
filter_spec="blob:none", # skip file contents — commits+trees only
errstream=io.BytesIO(),
**clone_kwargs
)
await asyncio.to_thread(_do_clone)
self._graph_data = await asyncio.to_thread(_build_graph_from_git, self._tmpdir)
self._commits = [c for c, _ in self._graph_data]
self._shown = 0
def _extract_commits(self) -> list[CommitInfo]:
from dulwich.repo import Repo
from dulwich.walk import ORDER_DATE
repo = Repo(self._tmpdir)
heads: list[bytes] = []
for ref, sha in repo.refs.as_dict().items():
if ref.startswith(b"refs/heads/") or ref.startswith(b"refs/remotes/"):
heads.append(sha)
if not heads:
try:
heads = [repo.head()]
except Exception:
pass
if not heads:
return []
commits: list[CommitInfo] = []
seen: set[str] = set()
for entry in repo.get_walker(include=list(set(heads)), order=ORDER_DATE):
c = entry.commit
sha = c.id.decode()
if sha in seen:
continue
seen.add(sha)
parents = [p.decode() for p in c.parents]
msg = c.message.decode("utf-8", errors="replace").strip().split("\n")[0]
author_raw = c.author.decode("utf-8", errors="replace")
m = re.match(r"^(.*?)\s*<(.*)>$", author_raw)
author = m.group(1).strip() if m else author_raw
email = m.group(2).strip() if m else ""
dt = datetime.fromtimestamp(
c.author_time,
tz=timezone(timedelta(seconds=c.author_timezone)),
)
commits.append(CommitInfo(
sha=sha, short_sha=sha[:7],
message=msg, author=author, author_email=email,
date=dt.isoformat(), parents=parents,
))
return commits
def get_detail(self, sha: str) -> "CommitDetail":
import difflib
from dulwich.repo import Repo
from dulwich.diff_tree import tree_changes, CHANGE_ADD, CHANGE_DELETE, CHANGE_RENAME
repo = Repo(self._tmpdir)
c = repo[sha.encode()]
parents = [p.decode() for p in c.parents]
msg_full = c.message.decode("utf-8", errors="replace").strip()
author_raw = c.author.decode("utf-8", errors="replace")
m = re.match(r"^(.*?)\s*<(.*)>$", author_raw)
author = m.group(1).strip() if m else author_raw
email = m.group(2).strip() if m else ""
dt = datetime.fromtimestamp(
c.author_time,
tz=timezone(timedelta(seconds=c.author_timezone)),
)
info = CommitInfo(
sha=sha, short_sha=sha[:7],
message=msg_full.split("\n")[0],
author=author, author_email=email,
date=dt.isoformat(), parents=parents,
)
parent_tree = None
if parents:
try:
parent_tree = repo[parents[0].encode()].tree
except Exception:
pass
files: list[FileChange] = []
total_add = total_del = 0
try:
for change in tree_changes(repo.object_store, parent_tree, c.tree):
if change.type == CHANGE_ADD:
status = "added"
filename = change.new.path.decode("utf-8", errors="replace")
elif change.type == CHANGE_DELETE:
status = "removed"
filename = change.old.path.decode("utf-8", errors="replace")
elif change.type == CHANGE_RENAME:
status = "renamed"
filename = change.new.path.decode("utf-8", errors="replace")
else:
status = "modified"
filename = (change.new.path or change.old.path).decode("utf-8", errors="replace")
add = del_ = 0
try:
old_data = repo.object_store[change.old.sha].data if change.old.sha else b""
new_data = repo.object_store[change.new.sha].data if change.new.sha else b""
for line in difflib.unified_diff(
old_data.splitlines(True), new_data.splitlines(True)
):
if line.startswith(b"+") and not line.startswith(b"+++"):
add += 1
elif line.startswith(b"-") and not line.startswith(b"---"):
del_ += 1
except Exception:
pass
total_add += add
total_del += del_
files.append(FileChange(filename=filename, status=status,
additions=add, deletions=del_))
except Exception:
pass
return CommitDetail(
info=info,
stats={"additions": total_add, "deletions": total_del, "total": len(files)},
files=files,
refs=[],
linked_prs=[],
)
def get_repo_info(self) -> "RepoInfo":
from dulwich.repo import Repo
r = Repo(self._tmpdir)
try:
default_branch = r.refs.get_symrefs().get(b"HEAD", b"refs/heads/main")
default_branch = default_branch.decode().removeprefix("refs/heads/")
except Exception:
default_branch = "main"
branch_count = sum(
1 for ref in r.refs.as_dict()
if ref.startswith(b"refs/heads/") or ref.startswith(b"refs/remotes/")
)
return RepoInfo(
description="",
created_at="",
default_branch=default_branch,
language="",
stars=0,
forks=0,
open_issues=0,
branches=branch_count,
total_commits=len(self._commits),
)
def cleanup(self) -> None:
if self._tmpdir:
shutil.rmtree(self._tmpdir, ignore_errors=True)
self._tmpdir = None
self._commits = []
self._graph_data = []
self._shown = 0
# ── Helpers ───────────────────────────────────────────────────────────────────
def fmt_date(iso: str) -> str:
try:
iso = iso.replace("Z", "+00:00")
dt = datetime.fromisoformat(iso)
return dt.strftime("%Y-%m-%d %H:%M")
except (ValueError, TypeError):
return iso[:16]
# ── Graph Builder ─────────────────────────────────────────────────────────────
def _build_graph_from_git(tmpdir: str) -> list[tuple[CommitInfo, list[Text]]]:
"""Run `git log --graph --color=always` on the cloned bare repo and parse
the ANSI-coloured output into (CommitInfo, graph_lines) pairs.
Each commit line is identified by a NUL-delimited marker injected via
--format so we can cleanly separate graph-prefix characters from commit
metadata without any regex fragility.
"""
import subprocess
# \x01 (SOH) marks commit lines; %x00 tells git to output NUL field separators.
# Neither appears in graph characters (*, |, \, /, space).
MARKER = "\x01"
fmt = f"{MARKER}%H%x00%s%x00%aN%x00%aE%x00%ad%x00%P"
proc = subprocess.run(
[
"git", "--git-dir", tmpdir,
"log", "--graph", "--color=always",
f"--format={fmt}",
"--date=short",
"--all",
],
capture_output=True,
encoding="utf-8",
errors="replace",
)
output: list[tuple[CommitInfo, list[Text]]] = []
current_commit: Optional[CommitInfo] = None
current_lines: list[Text] = []
for raw in proc.stdout.splitlines():
if MARKER in raw:
graph_part, data = raw.split(MARKER, 1)
fields = data.split("\x00")
sha = fields[0] if len(fields) > 0 else ""
subject = fields[1] if len(fields) > 1 else ""
author = fields[2] if len(fields) > 2 else ""
email = fields[3] if len(fields) > 3 else ""
date = fields[4] if len(fields) > 4 else ""
parents = fields[5].split() if len(fields) > 5 and fields[5] else []
if not sha:
continue
if current_commit is not None:
output.append((current_commit, current_lines))
current_commit = CommitInfo(
sha=sha, short_sha=sha[:7],
message=subject, author=author, author_email=email,
date=date, parents=parents,
)
current_lines = [Text.from_ansi(graph_part)]
else:
if current_commit is not None:
current_lines.append(Text.from_ansi(raw))
if current_commit is not None:
output.append((current_commit, current_lines))
return output
# ── UI Components ─────────────────────────────────────────────────────────────
class GraphSplitter(Widget):
"""Horizontal drag bar at the bottom of the left panel to resize the graph column."""
DEFAULT_CSS = """
GraphSplitter {
height: 1;
background: $primary-darken-2;
color: $text-muted;
content-align: center middle;
}
GraphSplitter:hover {
background: $accent;
color: $text;
}
"""
def __init__(self) -> None:
super().__init__()
self._dragging = False
self._drag_start_x = 0
self._drag_start_width = 8
def render(self) -> str:
w = getattr(self.app, "_graph_col_width", 8)
return f"◁ graph: {w} ▷"
def on_mouse_down(self, event) -> None:
self._dragging = True
self._drag_start_x = event.screen_x
self._drag_start_width = getattr(self.app, "_graph_col_width", 8)
self.capture_mouse()
event.stop()
def on_mouse_up(self, event) -> None:
if self._dragging:
self._dragging = False
self.release_mouse()
event.stop()
def on_mouse_move(self, event) -> None:
if self._dragging:
delta = event.screen_x - self._drag_start_x
new_width = max(4, min(60, self._drag_start_width + delta))
self.app._set_graph_col_width(new_width)
self.refresh()
event.stop()
class Splitter(Widget):
"""Draggable vertical splitter between two panels."""
DEFAULT_CSS = """
Splitter {
width: 1;
height: 100%;
background: $primary-darken-2;
}
Splitter:hover {
background: $accent;
}
"""
def __init__(self) -> None:
super().__init__()
self._dragging = False
def render(self) -> str:
return ""
def on_mouse_down(self, event) -> None:
self._dragging = True
self.capture_mouse()
event.stop()
def on_mouse_up(self, event) -> None:
if self._dragging:
self._dragging = False
self.release_mouse()
event.stop()
def on_mouse_move(self, event) -> None:
if self._dragging:
main = self.app.query_one("#main")
total = main.size.width
if total > 0:
rel_x = event.screen_x - main.region.x
pct = max(15, min(85, int((rel_x / total) * 100)))
self.app.query_one("#left").styles.width = f"{pct}%"
event.stop()
class CommitItem(ListItem):
def __init__(self, commit: CommitInfo, graph_lines: list[Text]) -> None:
super().__init__()
self.commit = commit
self.graph_lines = graph_lines
def compose(self) -> ComposeResult:
sha = self.commit.short_sha
msg_text = escape(self.commit.message.split("\n")[0].strip())
who = escape(self.commit.author)
date = fmt_date(self.commit.date)[:10]
# Build graph cell as a single Text by joining lines with newlines
graph_text = Text()
for i, line in enumerate(self.graph_lines):
if i:
graph_text.append("\n")
graph_text.append_text(line)
graph_height = len(self.graph_lines)
# Single-line info: message + sha + date on one row
info_cell = f"[bold]{msg_text}[/bold] [cyan]{sha}[/cyan] [dim]{date} {who}[/dim]"
# Pad to match graph height (connector lines have no info)
info_cell += "\n" * max(0, graph_height - 1)
with Horizontal(classes="commit-row"):
yield Label(graph_text, classes="graph-col")
yield Label(info_cell, classes="info-col", markup=True)
class CommitExplorer(App):
TITLE = "Commit Explorer"
CSS = """
#toolbar {
height: 3;
layout: horizontal;
padding: 0 1;
background: $panel;
}
#provider-select { width: 20; margin-right: 1; }
#repo-input { width: 1fr; margin-right: 1; }
#load-btn { width: 8; }
#main { height: 1fr; layout: horizontal; }
#left {
width: 50%;
}
#repo-info {
height: auto;
padding: 1 2;
background: $panel;
border-bottom: solid $primary-darken-2;
display: none;
}
#spinner { height: 3; display: none; }
#commits-list { height: 1fr; }
#more-btn { height: 3; dock: bottom; }
CommitItem { padding: 0 0; height: auto; }
CommitItem:hover { background: $boost; }
.commit-row { height: auto; }
.graph-col { width: auto; min-width: 2; padding: 0 1 0 0; }
.info-col { width: 1fr; padding: 0 1; }
#right { width: 1fr; }
#open-btn { margin: 1 2 0 2; width: auto; }
#right-scroll { height: 1fr; padding: 1 2; }
"""
BINDINGS = [
Binding("q", "quit", "Quit"),
Binding("r", "reload", "Reload"),
Binding("n", "more", "Next page"),
]
def __init__(self, initial_repo: str = "", depth: Optional[int] = None) -> None:
super().__init__()
self._initial_repo = initial_repo
self._owner = ""
self._repo = ""
self._current_sha: str = ""
self._graph_col_width: int = 20
self._depth = depth
self._backend = _GitBackend()
self.providers = {
"github": GitHubProvider(),
"gitlab": GitLabProvider(),
"azure": AzureDevOpsProvider(),
}
self.current_provider = self.providers["github"]
def compose(self) -> ComposeResult:
yield Header()
with Horizontal(id="toolbar"):
yield Select(
[(p.name, k) for k, p in self.providers.items()],
value="github",
id="provider-select",
allow_blank=False
)
yield Input(
placeholder="owner/repo (e.g. paperclipai/paperclip)",
id="repo-input",
)
yield Button("Load", id="load-btn", variant="primary")
with Horizontal(id="main"):
with Vertical(id="left"):
yield Static("", id="repo-info")
yield LoadingIndicator(id="spinner")
yield ListView(id="commits-list")
yield Button("Load more ↓", id="more-btn")
yield GraphSplitter()
yield Splitter()
with Vertical(id="right"):
yield Button("⎋ Open in browser", id="open-btn", variant="default", disabled=True)
with ScrollableContainer(id="right-scroll"):
yield Static("[dim]Select a commit to see details.[/dim]", id="detail")
yield Footer()
def _set_graph_col_width(self, width: int) -> None:
self._graph_col_width = width
def on_mount(self) -> None:
self.query_one("#spinner").display = False
if self._initial_repo:
self.query_one("#repo-input", Input).value = self._initial_repo
self._trigger_load()
@on(Select.Changed, "#provider-select")
def on_provider_changed(self, event: Select.Changed) -> None:
self.current_provider = self.providers[str(event.value)]
self.query_one("#commits-list", ListView).clear()
self.query_one("#repo-info", Static).display = False
@on(Input.Submitted, "#repo-input")
def on_input_submitted(self) -> None:
self._trigger_load()
@on(Button.Pressed, "#load-btn")
def on_load_pressed(self) -> None:
self._trigger_load()
@on(Button.Pressed, "#more-btn")
def on_more_pressed(self) -> None:
self._load_more()
@on(Button.Pressed, "#open-btn")
def on_open_pressed(self) -> None:
if self._current_sha and self._owner:
url = self.current_provider.commit_url(self._owner, self._repo, self._current_sha)
webbrowser.open(url)
@on(ListView.Selected, "#commits-list")
def on_commit_selected(self, event: ListView.Selected) -> None:
if isinstance(event.item, CommitItem):
self._fetch_detail(event.item.commit.sha)
def action_reload(self) -> None:
if self._owner:
self._fetch_commits(replace=True)
def action_more(self) -> None:
self._load_more()
_REPO_RE = re.compile(r"^[\w.\-]+/[\w.\-]+$")
def _trigger_load(self) -> None:
val = self.query_one("#repo-input", Input).value.strip()
if "/" not in val:
self.notify("Format: owner/repo (Azure: project/repo)", severity="warning")
return
parts = val.split("/", 1)
owner, repo = parts[0].strip(), parts[1].strip()
if not owner or not repo or not self._REPO_RE.match(f"{owner}/{repo}"):
self.notify("Invalid owner/repo — use alphanumeric, hyphens, dots only", severity="warning")
return
self._owner, self._repo = owner, repo
self._fetch_commits(replace=True)
def _load_more(self) -> None:
if self._owner and self._backend.has_more():
self._fetch_commits(replace=False)
@work
async def _fetch_commits(self, replace: bool) -> None:
spinner = self.query_one("#spinner")
spinner.display = True
more_btn = self.query_one("#more-btn", Button)
more_btn.display = False
try:
if replace:
url = self.current_provider.clone_url(self._owner, self._repo)
await self._backend.load(url, depth=self._depth)
# Show repo info from backend
info = self._backend.get_repo_info()
repo_widget = self.query_one("#repo-info", Static)
lines = [f"[bold]{escape(self._owner)}/[white]{escape(self._repo)}[/white][/bold]"]
meta = []
if info.default_branch:
meta.append(f"default: [magenta]{escape(info.default_branch)}[/magenta]")
if info.branches is not None:
meta.append(f"{info.branches:,} branches")
if info.total_commits is not None:
label = f"~{info.total_commits:,} commits"
if self._depth:
label += f" (depth {self._depth})"
meta.append(label)
if meta:
lines.append("[dim]" + " · ".join(meta) + "[/dim]")
repo_widget.update("\n".join(lines))
repo_widget.display = True
lv = self.query_one("#commits-list", ListView)
await lv.clear()
page = self._backend.next_page()
if not page:
self.notify("No commits found.", severity="information")
return
lv = self.query_one("#commits-list", ListView)
for commit, graph_lines in page:
await lv.append(CommitItem(commit, graph_lines))
more_btn.display = self._backend.has_more()
except Exception as e:
self.notify(f"Error: {e}", severity="error")
finally:
spinner.display = False
@work
async def _fetch_detail(self, sha: str) -> None:
self._current_sha = sha
self.query_one("#open-btn", Button).disabled = True
detail_widget = self.query_one("#detail", Static)
detail_widget.update("[dim]Loading details…[/dim]")
try:
d = await asyncio.to_thread(self._backend.get_detail, sha)
self.query_one("#open-btn", Button).disabled = False
lines = [
f"[bold yellow]SHA[/bold yellow] {d.info.sha}",
f"[bold yellow]Author[/bold yellow] {d.info.author} <{d.info.author_email}>",
f"[bold yellow]Date[/bold yellow] {fmt_date(d.info.date)}",
f"[bold yellow]Parents[/bold yellow] {', '.join(d.info.parents)}",
]
if d.stats:
lines.append(
f"[bold yellow]Stats[/bold yellow] "
f"[green]+{d.stats.get('additions', 0)}[/green] "
f"[red]-{d.stats.get('deletions', 0)}[/red] "
f"[dim]({d.stats.get('total', 0)} changes)[/dim]"
)
lines.extend([
"",
"[bold cyan]── Message ──────────────────────────────────────[/bold cyan]",
escape(d.info.message),
"",
])
if d.files:
lines.append(f"[bold cyan]── Files Changed ({len(d.files)}) ────────────────────────[/bold cyan]")
for f in d.files[:60]:
color = "white"
if f.status == "added": color = "green"
elif f.status == "removed": color = "red"
elif f.status == "modified": color = "yellow"
elif f.status == "renamed": color = "blue"
stats = ""
if f.additions or f.deletions:
stats = f"[dim](+{f.additions} -{f.deletions})[/dim]"
lines.append(f" [{color}]{f.status[0].upper()}[/{color}] {escape(f.filename)} {stats}")
if len(d.files) > 60:
lines.append(f" [dim]… and {len(d.files) - 60} more[/dim]")
lines.append("")
detail_widget.update("\n".join(lines))
except Exception as e:
detail_widget.update(f"[red]Error: {e}[/red]")
async def _export(owner: str, repo: str, provider_key: str, depth: Optional[int]) -> None:
"""Fetch commits via Dulwich and print the graph to stdout."""
from rich.console import Console
providers: dict[str, GitProvider] = {
"github": GitHubProvider(),
"gitlab": GitLabProvider(),
"azure": AzureDevOpsProvider(),
}
provider = providers.get(provider_key)
if provider is None:
print(f"Unknown provider '{provider_key}'. Choose from: {', '.join(providers)}", file=sys.stderr)
sys.exit(1)
backend = _GitBackend()
try:
console = Console(width=300, highlight=False)
url = provider.clone_url(owner, repo)
await backend.load(url, depth=depth)
for commit, lines in backend.graph_data:
date = commit.date[:10]
node = lines[0].copy()
node.append(f" {commit.short_sha} ", style="cyan")
node.append(commit.message.split("\n")[0].strip(), style="bold")
node.append(f" {commit.author}, {date}", style="dim")
console.print(node)
for cont in lines[1:]:
console.print(cont)
finally:
backend.cleanup()
def main() -> None:
"""Entry point for the commit-explorer command."""
import argparse
parser = argparse.ArgumentParser(prog="commit-explorer")
parser.add_argument("repo", nargs="?", default="", help="owner/repo")
parser.add_argument("--export", action="store_true", help="Print graph to stdout and exit")
parser.add_argument("--provider", default="github", choices=["github", "gitlab", "azure"])
parser.add_argument("--depth", type=int, default=None, metavar="N",
help="Limit fetch to N commits (default: fetch all)")
args = parser.parse_args()
if args.export:
if not args.repo or "/" not in args.repo:
parser.error("--export requires repo in owner/repo format")
owner, repo = args.repo.split("/", 1)
asyncio.run(_export(owner, repo, args.provider, args.depth))
else:
CommitExplorer(initial_repo=args.repo, depth=args.depth).run()
if __name__ == "__main__":
main()