git-visual-timeline 0.2.3__tar.gz → 0.2.5__tar.gz
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_visual_timeline-0.2.3/src/git_visual_timeline.egg-info → git_visual_timeline-0.2.5}/PKG-INFO +3 -3
- {git_visual_timeline-0.2.3 → git_visual_timeline-0.2.5}/pyproject.toml +3 -3
- {git_visual_timeline-0.2.3 → git_visual_timeline-0.2.5/src/git_visual_timeline.egg-info}/PKG-INFO +3 -3
- {git_visual_timeline-0.2.3 → git_visual_timeline-0.2.5}/src/git_visual_timeline.egg-info/SOURCES.txt +3 -0
- {git_visual_timeline-0.2.3 → git_visual_timeline-0.2.5}/src/git_visual_timeline.egg-info/requires.txt +2 -2
- git_visual_timeline-0.2.5/src/gvt/__init__.py +1 -0
- {git_visual_timeline-0.2.3 → git_visual_timeline-0.2.5}/src/gvt/app.py +18 -9
- {git_visual_timeline-0.2.3 → git_visual_timeline-0.2.5}/src/gvt/git/repo.py +66 -25
- git_visual_timeline-0.2.5/src/gvt/textsafe.py +33 -0
- {git_visual_timeline-0.2.3 → git_visual_timeline-0.2.5}/src/gvt/widgets/changed_files.py +4 -1
- {git_visual_timeline-0.2.3 → git_visual_timeline-0.2.5}/src/gvt/widgets/file_tree.py +7 -4
- {git_visual_timeline-0.2.3 → git_visual_timeline-0.2.5}/src/gvt/widgets/modals.py +3 -2
- {git_visual_timeline-0.2.3 → git_visual_timeline-0.2.5}/src/gvt/widgets/status_bar.py +3 -1
- {git_visual_timeline-0.2.3 → git_visual_timeline-0.2.5}/tests/test_app_actions.py +30 -0
- git_visual_timeline-0.2.5/tests/test_hostile_repo.py +305 -0
- git_visual_timeline-0.2.5/tests/test_textsafe.py +37 -0
- git_visual_timeline-0.2.3/src/gvt/__init__.py +0 -1
- {git_visual_timeline-0.2.3 → git_visual_timeline-0.2.5}/LICENSE +0 -0
- {git_visual_timeline-0.2.3 → git_visual_timeline-0.2.5}/README.md +0 -0
- {git_visual_timeline-0.2.3 → git_visual_timeline-0.2.5}/setup.cfg +0 -0
- {git_visual_timeline-0.2.3 → git_visual_timeline-0.2.5}/src/git_visual_timeline.egg-info/dependency_links.txt +0 -0
- {git_visual_timeline-0.2.3 → git_visual_timeline-0.2.5}/src/git_visual_timeline.egg-info/entry_points.txt +0 -0
- {git_visual_timeline-0.2.3 → git_visual_timeline-0.2.5}/src/git_visual_timeline.egg-info/top_level.txt +0 -0
- {git_visual_timeline-0.2.3 → git_visual_timeline-0.2.5}/src/gvt/cli.py +0 -0
- {git_visual_timeline-0.2.3 → git_visual_timeline-0.2.5}/src/gvt/git/__init__.py +0 -0
- {git_visual_timeline-0.2.3 → git_visual_timeline-0.2.5}/src/gvt/git/cache.py +0 -0
- {git_visual_timeline-0.2.3 → git_visual_timeline-0.2.5}/src/gvt/logging_setup.py +0 -0
- {git_visual_timeline-0.2.3 → git_visual_timeline-0.2.5}/src/gvt/styles/app.tcss +0 -0
- {git_visual_timeline-0.2.3 → git_visual_timeline-0.2.5}/src/gvt/widgets/__init__.py +0 -0
- {git_visual_timeline-0.2.3 → git_visual_timeline-0.2.5}/src/gvt/widgets/commit_bar.py +0 -0
- {git_visual_timeline-0.2.3 → git_visual_timeline-0.2.5}/src/gvt/widgets/commit_toast.py +0 -0
- {git_visual_timeline-0.2.3 → git_visual_timeline-0.2.5}/src/gvt/widgets/diff_view.py +0 -0
- {git_visual_timeline-0.2.3 → git_visual_timeline-0.2.5}/src/gvt/widgets/timeline.py +0 -0
- {git_visual_timeline-0.2.3 → git_visual_timeline-0.2.5}/tests/test_app_integration.py +0 -0
- {git_visual_timeline-0.2.3 → git_visual_timeline-0.2.5}/tests/test_cache.py +0 -0
- {git_visual_timeline-0.2.3 → git_visual_timeline-0.2.5}/tests/test_changed_files.py +0 -0
- {git_visual_timeline-0.2.3 → git_visual_timeline-0.2.5}/tests/test_diff_view_logic.py +0 -0
- {git_visual_timeline-0.2.3 → git_visual_timeline-0.2.5}/tests/test_git_repo.py +0 -0
- {git_visual_timeline-0.2.3 → git_visual_timeline-0.2.5}/tests/test_git_repo_extended.py +0 -0
- {git_visual_timeline-0.2.3 → git_visual_timeline-0.2.5}/tests/test_logging_setup.py +0 -0
- {git_visual_timeline-0.2.3 → git_visual_timeline-0.2.5}/tests/test_repo_resilience.py +0 -0
- {git_visual_timeline-0.2.3 → git_visual_timeline-0.2.5}/tests/test_timeline_logic.py +0 -0
- {git_visual_timeline-0.2.3 → git_visual_timeline-0.2.5}/tests/test_widgets.py +0 -0
{git_visual_timeline-0.2.3/src/git_visual_timeline.egg-info → git_visual_timeline-0.2.5}/PKG-INFO
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: git-visual-timeline
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.5
|
|
4
4
|
Summary: Git Visual Timeline — a keyboard-driven TUI for exploring file commit history
|
|
5
5
|
Author-email: Adam Reese <areese801@gmail.com>
|
|
6
6
|
License: MIT
|
|
@@ -22,8 +22,8 @@ Classifier: Topic :: Software Development :: Version Control :: Git
|
|
|
22
22
|
Requires-Python: >=3.10
|
|
23
23
|
Description-Content-Type: text/markdown
|
|
24
24
|
License-File: LICENSE
|
|
25
|
-
Requires-Dist: textual
|
|
26
|
-
Requires-Dist: gitpython>=3.1.
|
|
25
|
+
Requires-Dist: textual<9,>=8.0
|
|
26
|
+
Requires-Dist: gitpython>=3.1.41
|
|
27
27
|
Requires-Dist: pygments>=2.17.0
|
|
28
28
|
Requires-Dist: thefuzz[speedup]>=0.20.0
|
|
29
29
|
Provides-Extra: dev
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "git-visual-timeline"
|
|
7
|
-
version = "0.2.
|
|
7
|
+
version = "0.2.5"
|
|
8
8
|
description = "Git Visual Timeline — a keyboard-driven TUI for exploring file commit history"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
license = {text = "MIT"}
|
|
@@ -27,8 +27,8 @@ classifiers = [
|
|
|
27
27
|
]
|
|
28
28
|
keywords = ["git", "tui", "timeline", "diff", "blame", "textual"]
|
|
29
29
|
dependencies = [
|
|
30
|
-
"textual>=
|
|
31
|
-
"gitpython>=3.1.
|
|
30
|
+
"textual>=8.0,<9",
|
|
31
|
+
"gitpython>=3.1.41",
|
|
32
32
|
"pygments>=2.17.0",
|
|
33
33
|
"thefuzz[speedup]>=0.20.0",
|
|
34
34
|
]
|
{git_visual_timeline-0.2.3 → git_visual_timeline-0.2.5/src/git_visual_timeline.egg-info}/PKG-INFO
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: git-visual-timeline
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.5
|
|
4
4
|
Summary: Git Visual Timeline — a keyboard-driven TUI for exploring file commit history
|
|
5
5
|
Author-email: Adam Reese <areese801@gmail.com>
|
|
6
6
|
License: MIT
|
|
@@ -22,8 +22,8 @@ Classifier: Topic :: Software Development :: Version Control :: Git
|
|
|
22
22
|
Requires-Python: >=3.10
|
|
23
23
|
Description-Content-Type: text/markdown
|
|
24
24
|
License-File: LICENSE
|
|
25
|
-
Requires-Dist: textual
|
|
26
|
-
Requires-Dist: gitpython>=3.1.
|
|
25
|
+
Requires-Dist: textual<9,>=8.0
|
|
26
|
+
Requires-Dist: gitpython>=3.1.41
|
|
27
27
|
Requires-Dist: pygments>=2.17.0
|
|
28
28
|
Requires-Dist: thefuzz[speedup]>=0.20.0
|
|
29
29
|
Provides-Extra: dev
|
{git_visual_timeline-0.2.3 → git_visual_timeline-0.2.5}/src/git_visual_timeline.egg-info/SOURCES.txt
RENAMED
|
@@ -11,6 +11,7 @@ src/gvt/__init__.py
|
|
|
11
11
|
src/gvt/app.py
|
|
12
12
|
src/gvt/cli.py
|
|
13
13
|
src/gvt/logging_setup.py
|
|
14
|
+
src/gvt/textsafe.py
|
|
14
15
|
src/gvt/git/__init__.py
|
|
15
16
|
src/gvt/git/cache.py
|
|
16
17
|
src/gvt/git/repo.py
|
|
@@ -31,7 +32,9 @@ tests/test_changed_files.py
|
|
|
31
32
|
tests/test_diff_view_logic.py
|
|
32
33
|
tests/test_git_repo.py
|
|
33
34
|
tests/test_git_repo_extended.py
|
|
35
|
+
tests/test_hostile_repo.py
|
|
34
36
|
tests/test_logging_setup.py
|
|
35
37
|
tests/test_repo_resilience.py
|
|
38
|
+
tests/test_textsafe.py
|
|
36
39
|
tests/test_timeline_logic.py
|
|
37
40
|
tests/test_widgets.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.2.5"
|
|
@@ -4,6 +4,7 @@ from __future__ import annotations
|
|
|
4
4
|
|
|
5
5
|
import hashlib
|
|
6
6
|
import os
|
|
7
|
+
import shlex
|
|
7
8
|
import shutil
|
|
8
9
|
import subprocess
|
|
9
10
|
from datetime import datetime, timezone
|
|
@@ -16,11 +17,13 @@ from textual.app import App, ComposeResult
|
|
|
16
17
|
from textual.binding import Binding
|
|
17
18
|
from textual.containers import Container, Horizontal, Vertical
|
|
18
19
|
from textual.css.query import NoMatches
|
|
20
|
+
from textual.markup import escape
|
|
19
21
|
from textual.widget import Widget
|
|
20
22
|
|
|
21
23
|
from gvt.git.cache import DiffCache
|
|
22
24
|
from gvt.git.repo import CommitInfo, GitRepo
|
|
23
25
|
from gvt.logging_setup import get_logger
|
|
26
|
+
from gvt.textsafe import safe_text
|
|
24
27
|
|
|
25
28
|
log = get_logger(__name__)
|
|
26
29
|
from gvt.widgets.changed_files import ChangedFilesWidget, ChangedFileSelected, ChangedFileHighlighted
|
|
@@ -191,7 +194,7 @@ class GVTApp(App):
|
|
|
191
194
|
|
|
192
195
|
@on(ChangedFileHighlighted)
|
|
193
196
|
def on_changed_file_highlighted(self, event: ChangedFileHighlighted) -> None:
|
|
194
|
-
self.query_one("#changed-files-pane", Container).border_title = f"[5] {event.path}"
|
|
197
|
+
self.query_one("#changed-files-pane", Container).border_title = f"[5] {escape(safe_text(event.path))}"
|
|
195
198
|
|
|
196
199
|
def _load_untracked_file(self, file_path: str) -> None:
|
|
197
200
|
"""Show an untracked file as a read-only preview."""
|
|
@@ -231,7 +234,7 @@ class GVTApp(App):
|
|
|
231
234
|
_, branch = self.git_repo.get_branches()
|
|
232
235
|
status_bar.update_info(file_path=file_path, branch=branch)
|
|
233
236
|
self.query_one("#timeline-pane", Container).border_title = "[2] Timeline"
|
|
234
|
-
self.query_one("#diff-pane", Container).border_title = f"[4] Preview {file_path} (untracked)"
|
|
237
|
+
self.query_one("#diff-pane", Container).border_title = f"[4] Preview {escape(safe_text(file_path))} (untracked)"
|
|
235
238
|
self.query_one("#changed-files-pane", Container).border_title = "[5] Changed"
|
|
236
239
|
|
|
237
240
|
# Focus the diff pane so user can scroll the preview
|
|
@@ -314,10 +317,12 @@ class GVTApp(App):
|
|
|
314
317
|
title = "WIP"
|
|
315
318
|
else:
|
|
316
319
|
title = f"{commit.short_hash} {commit.date.strftime('%Y-%m-%d')} {commit.author} +{commit.additions} -{commit.deletions}"
|
|
320
|
+
# Commit text comes from the repo: show it literally, not as markup
|
|
317
321
|
self.notify(
|
|
318
322
|
commit.message.strip(),
|
|
319
323
|
title=title,
|
|
320
324
|
timeout=2,
|
|
325
|
+
markup=False,
|
|
321
326
|
)
|
|
322
327
|
|
|
323
328
|
def _update_from_timeline(self, timeline: TimelineWidget, file_path: str) -> None:
|
|
@@ -333,10 +338,10 @@ class GVTApp(App):
|
|
|
333
338
|
# Update dynamic pane titles
|
|
334
339
|
current_commit = self.current_commits[timeline.cursor]
|
|
335
340
|
if current_commit.is_wip:
|
|
336
|
-
self.query_one("#diff-pane", Container).border_title = f"[4] Diff {file_path} (WIP)"
|
|
341
|
+
self.query_one("#diff-pane", Container).border_title = f"[4] Diff {escape(safe_text(file_path))} (WIP)"
|
|
337
342
|
self.query_one("#changed-files-pane", Container).border_title = "[5] Uncommitted changes"
|
|
338
343
|
else:
|
|
339
|
-
self.query_one("#diff-pane", Container).border_title = f"[4] Diff {file_path}"
|
|
344
|
+
self.query_one("#diff-pane", Container).border_title = f"[4] Diff {escape(safe_text(file_path))}"
|
|
340
345
|
self.query_one("#changed-files-pane", Container).border_title = f"[5] Also changed on {current_commit.short_hash}"
|
|
341
346
|
|
|
342
347
|
self._load_diff(file_path, left_idx, right_idx)
|
|
@@ -515,7 +520,10 @@ class GVTApp(App):
|
|
|
515
520
|
def _tmux_select_pane(direction: str) -> None:
|
|
516
521
|
"""Hand off navigation to tmux if running inside tmux."""
|
|
517
522
|
if os.environ.get("TMUX"):
|
|
518
|
-
|
|
523
|
+
try:
|
|
524
|
+
subprocess.run(["tmux", "select-pane", f"-{direction}"], check=False)
|
|
525
|
+
except OSError as e:
|
|
526
|
+
log.warning("tmux pane handoff failed: %s", e)
|
|
519
527
|
|
|
520
528
|
def action_focus_left(self) -> None:
|
|
521
529
|
if self._current_pane_idx in self._LEFT_PANES:
|
|
@@ -709,7 +717,7 @@ class GVTApp(App):
|
|
|
709
717
|
bar = "█" * max(1, round(count / total * 20))
|
|
710
718
|
lines.append(f" {bar} {name} {count} commits ({pct:.0f}%)")
|
|
711
719
|
self.clear_notifications()
|
|
712
|
-
self.notify("\n".join(lines), title="Contributors", timeout=5)
|
|
720
|
+
self.notify("\n".join(lines), title="Contributors", timeout=5, markup=False)
|
|
713
721
|
|
|
714
722
|
def action_copy_short_hash(self) -> None:
|
|
715
723
|
if not self.current_commits:
|
|
@@ -749,11 +757,12 @@ class GVTApp(App):
|
|
|
749
757
|
result = None
|
|
750
758
|
with self.suspend():
|
|
751
759
|
try:
|
|
752
|
-
|
|
753
|
-
|
|
760
|
+
# $EDITOR may carry arguments, e.g. "code --wait"
|
|
761
|
+
result = subprocess.run(shlex.split(editor) + [file_path])
|
|
762
|
+
except (OSError, ValueError) as e:
|
|
754
763
|
log.warning("editor %s failed to launch: %s", editor, e)
|
|
755
764
|
if result is None:
|
|
756
|
-
self.notify(f"Could not launch editor: {editor}", timeout=3)
|
|
765
|
+
self.notify(f"Could not launch editor: {editor}", timeout=3, markup=False)
|
|
757
766
|
elif result.returncode != 0:
|
|
758
767
|
log.warning("editor %s exited with code %d", editor, result.returncode)
|
|
759
768
|
self.notify(f"Editor exited with code {result.returncode}", timeout=2)
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
5
|
import os
|
|
6
|
+
import re
|
|
6
7
|
from dataclasses import dataclass
|
|
7
8
|
from datetime import datetime, timezone
|
|
8
9
|
|
|
@@ -10,9 +11,31 @@ from git import Repo
|
|
|
10
11
|
from git.exc import GitCommandError
|
|
11
12
|
|
|
12
13
|
from gvt.logging_setup import get_logger
|
|
14
|
+
from gvt.textsafe import safe_text
|
|
13
15
|
|
|
14
16
|
log = get_logger(__name__)
|
|
15
17
|
|
|
18
|
+
# Commit records start with a NUL byte, which can't appear in commit metadata
|
|
19
|
+
LOG_FORMAT = "--format=%x00%H%n%at%n%an%n%s"
|
|
20
|
+
RECORD_SEPARATOR = "\x00"
|
|
21
|
+
|
|
22
|
+
# Never run diff programs or text converters configured by the repo
|
|
23
|
+
SAFE_DIFF = ("--no-ext-diff", "--no-textconv")
|
|
24
|
+
|
|
25
|
+
# Largest working-tree file shown as a preview
|
|
26
|
+
MAX_PREVIEW_BYTES = 2_000_000
|
|
27
|
+
|
|
28
|
+
# Commit timestamps are clamped to 1970..2100: a crafted date outside Python's
|
|
29
|
+
# datetime range would otherwise crash parsing
|
|
30
|
+
MAX_TIMESTAMP = 4_102_444_800 # 2100-01-01 UTC
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def commit_date(timestamp: int) -> datetime:
|
|
34
|
+
"""
|
|
35
|
+
UTC datetime for a git timestamp, clamped to 1970-01-01..2100-01-01.
|
|
36
|
+
"""
|
|
37
|
+
return datetime.fromtimestamp(max(0, min(timestamp, MAX_TIMESTAMP)), tz=timezone.utc)
|
|
38
|
+
|
|
16
39
|
|
|
17
40
|
@dataclass
|
|
18
41
|
class CommitInfo:
|
|
@@ -58,6 +81,13 @@ class GitRepo:
|
|
|
58
81
|
def __init__(self, path: str):
|
|
59
82
|
self.repo = Repo(path, search_parent_directories=True)
|
|
60
83
|
self.root = self.repo.working_tree_dir
|
|
84
|
+
# Paths are file names, not pathspecs: "app/[slug]/page.tsx" must not
|
|
85
|
+
# match "app/s/page.tsx". List non-ASCII paths as-is instead of
|
|
86
|
+
# C-quoting them, and never run a repo-configured fsmonitor hook.
|
|
87
|
+
self.repo.git.update_environment(GIT_LITERAL_PATHSPECS="1")
|
|
88
|
+
self.repo.git.set_persistent_git_options(
|
|
89
|
+
c=["core.quotePath=false", "core.fsmonitor=false"]
|
|
90
|
+
)
|
|
61
91
|
|
|
62
92
|
def _build_ref_map(self) -> dict[str, list[str]]:
|
|
63
93
|
"""Build a sha -> [ref names] map, tolerating individual bad refs."""
|
|
@@ -70,7 +100,8 @@ class GitRepo:
|
|
|
70
100
|
for ref in refs:
|
|
71
101
|
try:
|
|
72
102
|
sha = ref.commit.hexsha
|
|
73
|
-
|
|
103
|
+
# Ref names may hold bidi or C1 characters; they're only displayed
|
|
104
|
+
ref_map.setdefault(sha, []).append(safe_text(ref.name))
|
|
74
105
|
except (GitCommandError, AttributeError, ValueError) as e:
|
|
75
106
|
log.debug("skipping ref %s: %s", getattr(ref, "name", "?"), e)
|
|
76
107
|
return ref_map
|
|
@@ -87,7 +118,7 @@ class GitRepo:
|
|
|
87
118
|
|
|
88
119
|
# Use git log with --numstat for stats, avoiding gitpython's object parser
|
|
89
120
|
output = self.repo.git.log(
|
|
90
|
-
|
|
121
|
+
LOG_FORMAT,
|
|
91
122
|
"--numstat",
|
|
92
123
|
"--follow",
|
|
93
124
|
"--", rel_path,
|
|
@@ -97,7 +128,7 @@ class GitRepo:
|
|
|
97
128
|
return []
|
|
98
129
|
|
|
99
130
|
commits = []
|
|
100
|
-
chunks = output.split(
|
|
131
|
+
chunks = output.split(RECORD_SEPARATOR)
|
|
101
132
|
|
|
102
133
|
for chunk in chunks:
|
|
103
134
|
chunk = chunk.strip()
|
|
@@ -117,8 +148,8 @@ class GitRepo:
|
|
|
117
148
|
except (ValueError, IndexError):
|
|
118
149
|
continue
|
|
119
150
|
|
|
120
|
-
author = lines[2].strip()
|
|
121
|
-
subject = lines[3].strip()
|
|
151
|
+
author = safe_text(lines[2].strip())
|
|
152
|
+
subject = safe_text(lines[3].strip())
|
|
122
153
|
|
|
123
154
|
# Remaining lines are numstat (tab-separated: adds, dels, filename)
|
|
124
155
|
additions = 0
|
|
@@ -139,7 +170,7 @@ class GitRepo:
|
|
|
139
170
|
commits.append(
|
|
140
171
|
CommitInfo(
|
|
141
172
|
hexsha=hexsha,
|
|
142
|
-
date=
|
|
173
|
+
date=commit_date(timestamp),
|
|
143
174
|
author=author,
|
|
144
175
|
message=subject,
|
|
145
176
|
additions=additions,
|
|
@@ -162,7 +193,7 @@ class GitRepo:
|
|
|
162
193
|
output = self.repo.git.log(
|
|
163
194
|
f"--max-count={max_count}",
|
|
164
195
|
"--no-merges",
|
|
165
|
-
|
|
196
|
+
LOG_FORMAT,
|
|
166
197
|
"--shortstat",
|
|
167
198
|
)
|
|
168
199
|
|
|
@@ -170,7 +201,7 @@ class GitRepo:
|
|
|
170
201
|
return []
|
|
171
202
|
|
|
172
203
|
commits = []
|
|
173
|
-
chunks = output.split(
|
|
204
|
+
chunks = output.split(RECORD_SEPARATOR)
|
|
174
205
|
|
|
175
206
|
for chunk in chunks:
|
|
176
207
|
chunk = chunk.strip()
|
|
@@ -190,15 +221,14 @@ class GitRepo:
|
|
|
190
221
|
except (ValueError, IndexError):
|
|
191
222
|
continue
|
|
192
223
|
|
|
193
|
-
author = lines[2].strip()
|
|
194
|
-
subject = lines[3].strip()
|
|
224
|
+
author = safe_text(lines[2].strip())
|
|
225
|
+
subject = safe_text(lines[3].strip())
|
|
195
226
|
|
|
196
227
|
# Parse shortstat line (e.g. " 3 files changed, 10 insertions(+), 5 deletions(-)")
|
|
197
228
|
total_adds = 0
|
|
198
229
|
total_dels = 0
|
|
199
230
|
for line in lines[4:]:
|
|
200
231
|
if "insertion" in line or "deletion" in line:
|
|
201
|
-
import re
|
|
202
232
|
add_match = re.search(r"(\d+) insertion", line)
|
|
203
233
|
del_match = re.search(r"(\d+) deletion", line)
|
|
204
234
|
if add_match:
|
|
@@ -210,7 +240,7 @@ class GitRepo:
|
|
|
210
240
|
commits.append(
|
|
211
241
|
CommitInfo(
|
|
212
242
|
hexsha=hexsha,
|
|
213
|
-
date=
|
|
243
|
+
date=commit_date(timestamp),
|
|
214
244
|
author=author,
|
|
215
245
|
message=subject,
|
|
216
246
|
additions=total_adds,
|
|
@@ -227,26 +257,28 @@ class GitRepo:
|
|
|
227
257
|
rel_path = self._to_relative(path)
|
|
228
258
|
# Use git diff directly for context line control
|
|
229
259
|
diff_output = self.repo.git.diff(
|
|
230
|
-
f"-U{context_lines}", commit_a, commit_b, "--", rel_path
|
|
260
|
+
*SAFE_DIFF, f"-U{context_lines}", commit_a, commit_b, "--", rel_path
|
|
231
261
|
)
|
|
232
|
-
return diff_output
|
|
262
|
+
return safe_text(diff_output)
|
|
233
263
|
|
|
234
264
|
def has_uncommitted_changes(self, path: str) -> bool:
|
|
235
265
|
"""Check if a file has uncommitted changes (staged or unstaged)."""
|
|
236
266
|
rel_path = self._to_relative(path)
|
|
237
267
|
# Check both staged and unstaged
|
|
238
|
-
diff_output = self.repo.git.diff("HEAD", "--", rel_path)
|
|
268
|
+
diff_output = self.repo.git.diff(*SAFE_DIFF, "HEAD", "--", rel_path)
|
|
239
269
|
return bool(diff_output.strip())
|
|
240
270
|
|
|
241
271
|
def get_diff_to_working_tree(self, path: str, commit_sha: str, context_lines: int = 3) -> str:
|
|
242
272
|
"""Get diff from a commit to the current working tree for a file."""
|
|
243
273
|
rel_path = self._to_relative(path)
|
|
244
|
-
return
|
|
274
|
+
return safe_text(
|
|
275
|
+
self.repo.git.diff(*SAFE_DIFF, f"-U{context_lines}", commit_sha, "--", rel_path)
|
|
276
|
+
)
|
|
245
277
|
|
|
246
278
|
def get_working_tree_stats(self, path: str) -> tuple[int, int]:
|
|
247
279
|
"""Get +/- stats for uncommitted changes to a file."""
|
|
248
280
|
rel_path = self._to_relative(path)
|
|
249
|
-
output = self.repo.git.diff("--numstat", "HEAD", "--", rel_path)
|
|
281
|
+
output = self.repo.git.diff(*SAFE_DIFF, "--numstat", "HEAD", "--", rel_path)
|
|
250
282
|
if not output.strip():
|
|
251
283
|
return (0, 0)
|
|
252
284
|
parts = output.strip().split("\t")
|
|
@@ -262,7 +294,7 @@ class GitRepo:
|
|
|
262
294
|
"""
|
|
263
295
|
rel_path = self._to_relative(path)
|
|
264
296
|
try:
|
|
265
|
-
return self.repo.git.show(f"{commit_sha}:{rel_path}")
|
|
297
|
+
return safe_text(self.repo.git.show(f"{commit_sha}:{rel_path}"))
|
|
266
298
|
except GitCommandError as e:
|
|
267
299
|
log.warning("git show %s:%s failed: %s", commit_sha, rel_path, e)
|
|
268
300
|
return ""
|
|
@@ -275,7 +307,7 @@ class GitRepo:
|
|
|
275
307
|
rel_path = self._to_relative(path)
|
|
276
308
|
try:
|
|
277
309
|
output = self.repo.git.blame(
|
|
278
|
-
commit_sha, "--", rel_path,
|
|
310
|
+
"--no-textconv", commit_sha, "--", rel_path,
|
|
279
311
|
porcelain=True,
|
|
280
312
|
)
|
|
281
313
|
except GitCommandError as e:
|
|
@@ -296,11 +328,11 @@ class GitRepo:
|
|
|
296
328
|
dates.get(current_hash, ""),
|
|
297
329
|
))
|
|
298
330
|
elif raw_line.startswith("author "):
|
|
299
|
-
authors[current_hash] = raw_line[7:]
|
|
331
|
+
authors[current_hash] = safe_text(raw_line[7:])
|
|
300
332
|
elif raw_line.startswith("author-time "):
|
|
301
333
|
from datetime import datetime, timezone
|
|
302
334
|
ts = int(raw_line[12:])
|
|
303
|
-
dates[current_hash] =
|
|
335
|
+
dates[current_hash] = commit_date(ts).strftime("%Y-%m-%d")
|
|
304
336
|
elif not raw_line.startswith("author-") and not raw_line.startswith("committer") and not raw_line.startswith("summary ") and not raw_line.startswith("previous ") and not raw_line.startswith("filename ") and not raw_line.startswith("boundary"):
|
|
305
337
|
# Could be a commit line: hash orig_line final_line [num_lines]
|
|
306
338
|
parts = raw_line.split()
|
|
@@ -329,7 +361,7 @@ class GitRepo:
|
|
|
329
361
|
# Lines come in pairs: email, then name
|
|
330
362
|
for i in range(0, len(lines) - 1, 2):
|
|
331
363
|
email = lines[i].strip()
|
|
332
|
-
name = lines[i + 1].strip()
|
|
364
|
+
name = safe_text(lines[i + 1].strip())
|
|
333
365
|
if not email:
|
|
334
366
|
continue
|
|
335
367
|
if email in by_email:
|
|
@@ -353,7 +385,7 @@ class GitRepo:
|
|
|
353
385
|
"""
|
|
354
386
|
try:
|
|
355
387
|
output = self.repo.git.diff_tree(
|
|
356
|
-
"--numstat", "-r", "--root", "--no-commit-id", commit_sha
|
|
388
|
+
*SAFE_DIFF, "--numstat", "-r", "--root", "--no-commit-id", commit_sha
|
|
357
389
|
)
|
|
358
390
|
except (GitCommandError, ValueError) as e:
|
|
359
391
|
log.warning("diff-tree for %s failed: %s", commit_sha, e)
|
|
@@ -396,12 +428,21 @@ class GitRepo:
|
|
|
396
428
|
def get_file_content(self, path: str) -> str:
|
|
397
429
|
"""
|
|
398
430
|
Get the current working tree content of a file.
|
|
431
|
+
|
|
432
|
+
Only regular files inside the repo are read (a symlink pointing
|
|
433
|
+
elsewhere, a FIFO or a device returns ""), and only the first
|
|
434
|
+
MAX_PREVIEW_BYTES.
|
|
399
435
|
"""
|
|
400
436
|
rel_path = self._to_relative(path)
|
|
401
437
|
full_path = os.path.join(self.root, rel_path)
|
|
438
|
+
real_root = os.path.realpath(self.root)
|
|
439
|
+
real_path = os.path.realpath(full_path)
|
|
440
|
+
if os.path.commonpath([real_root, real_path]) != real_root or not os.path.isfile(real_path):
|
|
441
|
+
log.info("not previewing %s: outside the repo or not a regular file", rel_path)
|
|
442
|
+
return ""
|
|
402
443
|
try:
|
|
403
|
-
with open(
|
|
404
|
-
return f.read()
|
|
444
|
+
with open(real_path, "r", errors="replace") as f:
|
|
445
|
+
return safe_text(f.read(MAX_PREVIEW_BYTES))
|
|
405
446
|
except (OSError, UnicodeDecodeError):
|
|
406
447
|
return ""
|
|
407
448
|
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Make repository-controlled text safe to print in a terminal.
|
|
3
|
+
|
|
4
|
+
Commit messages, author names and file contents come from whoever made the
|
|
5
|
+
repo. Raw control characters in them would reach the terminal as escape
|
|
6
|
+
sequences, and bidi controls can reorder what is displayed, so anything shown
|
|
7
|
+
from a repo passes through safe_text() first.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import re
|
|
13
|
+
|
|
14
|
+
# C0 controls except tab and newline, DEL, C1 controls, and bidi
|
|
15
|
+
# embedding/override/isolate characters
|
|
16
|
+
_UNSAFE = re.compile(r"[\x00-\x08\x0b-\x1f\x7f-\x9f--]")
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _visible(match: re.Match[str]) -> str:
|
|
20
|
+
code = ord(match.group())
|
|
21
|
+
return f"\\x{code:02x}" if code < 0x100 else f"\\u{code:04x}"
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def safe_text(text: str) -> str:
|
|
25
|
+
"""
|
|
26
|
+
Replace control and bidi characters with visible escapes like "\\x1b".
|
|
27
|
+
|
|
28
|
+
Tabs and newlines are kept, and CRLF line endings become plain newlines.
|
|
29
|
+
"""
|
|
30
|
+
text = text.replace("\r\n", "\n")
|
|
31
|
+
if text.endswith("\r"):
|
|
32
|
+
text = text[:-1]
|
|
33
|
+
return _UNSAFE.sub(_visible, text)
|
|
@@ -7,6 +7,8 @@ from textual.message import Message
|
|
|
7
7
|
from textual.reactive import reactive
|
|
8
8
|
from textual.scroll_view import ScrollView
|
|
9
9
|
|
|
10
|
+
from gvt.textsafe import safe_text
|
|
11
|
+
|
|
10
12
|
COLOR_ACCENT = "#7aa2f7"
|
|
11
13
|
COLOR_TEXT = "#c0caf5"
|
|
12
14
|
COLOR_DIM = "#565f89"
|
|
@@ -70,7 +72,8 @@ class ChangedFilesWidget(ScrollView, can_focus=True):
|
|
|
70
72
|
self._lines.append(Text("No files changed", style=COLOR_DIM))
|
|
71
73
|
return
|
|
72
74
|
|
|
73
|
-
for idx, (
|
|
75
|
+
for idx, (raw_path, adds, dels) in enumerate(self.files):
|
|
76
|
+
file_path = safe_text(raw_path)
|
|
74
77
|
line = Text()
|
|
75
78
|
is_selected = idx == self.selected_idx
|
|
76
79
|
|
|
@@ -12,6 +12,8 @@ from textual.widget import Widget
|
|
|
12
12
|
from textual.widgets import Tree
|
|
13
13
|
from textual.widgets._tree import TreeNode
|
|
14
14
|
|
|
15
|
+
from gvt.textsafe import safe_text
|
|
16
|
+
|
|
15
17
|
COLOR_DIM = "#565f89"
|
|
16
18
|
COLOR_UNTRACKED = "#e0af68"
|
|
17
19
|
|
|
@@ -143,20 +145,21 @@ class FileTreeWidget(Widget, can_focus=True):
|
|
|
143
145
|
|
|
144
146
|
for subdir in subdirs:
|
|
145
147
|
# Display name is the last component
|
|
146
|
-
display_name = subdir.split("/")[-1]
|
|
148
|
+
display_name = safe_text(subdir.split("/")[-1])
|
|
149
|
+
# Text, not str: a str label would be parsed as markup
|
|
147
150
|
if is_untracked:
|
|
148
151
|
label = Text(display_name, style=COLOR_DIM)
|
|
149
152
|
else:
|
|
150
|
-
label = display_name
|
|
153
|
+
label = Text(display_name)
|
|
151
154
|
node = parent_node.add(label, data=subdir, expand=False)
|
|
152
155
|
node.allow_expand = True
|
|
153
156
|
|
|
154
157
|
for file_path in files:
|
|
155
|
-
display_name = file_path.split("/")[-1]
|
|
158
|
+
display_name = safe_text(file_path.split("/")[-1])
|
|
156
159
|
if is_untracked:
|
|
157
160
|
label = Text(f"? {display_name}", style=COLOR_DIM)
|
|
158
161
|
else:
|
|
159
|
-
label = display_name
|
|
162
|
+
label = Text(display_name)
|
|
160
163
|
parent_node.add_leaf(label, data=file_path)
|
|
161
164
|
|
|
162
165
|
@on(Tree.NodeExpanded)
|
|
@@ -13,6 +13,7 @@ from textual.widgets import Input, Label, Static
|
|
|
13
13
|
from rich.text import Text
|
|
14
14
|
|
|
15
15
|
from gvt.git.repo import CommitInfo
|
|
16
|
+
from gvt.textsafe import safe_text
|
|
16
17
|
|
|
17
18
|
try:
|
|
18
19
|
from thefuzz import fuzz
|
|
@@ -266,7 +267,7 @@ class FileSearchModal(ModalScreen):
|
|
|
266
267
|
else:
|
|
267
268
|
line.append(" ")
|
|
268
269
|
|
|
269
|
-
line.append(file_path, style=COLOR_TEXT)
|
|
270
|
+
line.append(safe_text(file_path), style=COLOR_TEXT)
|
|
270
271
|
|
|
271
272
|
label = Static(line)
|
|
272
273
|
if is_selected:
|
|
@@ -412,7 +413,7 @@ class CommitFilesModal(ModalScreen):
|
|
|
412
413
|
else:
|
|
413
414
|
line.append(" ")
|
|
414
415
|
|
|
415
|
-
line.append(file_path, style=COLOR_TEXT if not is_selected else f"bold {COLOR_TEXT}")
|
|
416
|
+
line.append(safe_text(file_path), style=COLOR_TEXT if not is_selected else f"bold {COLOR_TEXT}")
|
|
416
417
|
line.append(" ", style="")
|
|
417
418
|
line.append(f"+{adds}", style=COLOR_GREEN)
|
|
418
419
|
line.append(f" -{dels}", style=COLOR_RED)
|
|
@@ -6,6 +6,8 @@ from rich.text import Text
|
|
|
6
6
|
from textual.widget import Widget
|
|
7
7
|
from textual.strip import Strip
|
|
8
8
|
|
|
9
|
+
from gvt.textsafe import safe_text
|
|
10
|
+
|
|
9
11
|
COLOR_BAR_BG = "#7aa2f7"
|
|
10
12
|
COLOR_BAR_FG = "#1a1b26"
|
|
11
13
|
|
|
@@ -101,7 +103,7 @@ class GVTStatusBar(Widget):
|
|
|
101
103
|
text.append(" gvt", style=f"bold on {COLOR_BAR_BG} {COLOR_BAR_FG}")
|
|
102
104
|
|
|
103
105
|
if self.file_path:
|
|
104
|
-
text.append(f" {self.file_path}", style=f"on {COLOR_BAR_BG} {COLOR_BAR_FG}")
|
|
106
|
+
text.append(f" {safe_text(self.file_path)}", style=f"on {COLOR_BAR_BG} {COLOR_BAR_FG}")
|
|
105
107
|
|
|
106
108
|
if self.commit_position:
|
|
107
109
|
text.append(f" [{self.commit_position}]", style=f"on {COLOR_BAR_BG} {COLOR_BAR_FG}")
|
|
@@ -123,3 +123,33 @@ def test_open_in_editor_no_current_file(fixture_app, monkeypatch):
|
|
|
123
123
|
|
|
124
124
|
assert not runs
|
|
125
125
|
assert not called
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def test_open_in_editor_splits_editor_arguments(fixture_app, monkeypatch):
|
|
129
|
+
"""EDITOR="code --wait" runs `code --wait <file>`, not a program named "code --wait"."""
|
|
130
|
+
import contextlib
|
|
131
|
+
|
|
132
|
+
fixture_app.current_file = "hello.py"
|
|
133
|
+
monkeypatch.delenv("VISUAL", raising=False)
|
|
134
|
+
monkeypatch.setenv("EDITOR", "code --wait")
|
|
135
|
+
monkeypatch.setattr(fixture_app, "suspend", lambda: contextlib.nullcontext())
|
|
136
|
+
runs = []
|
|
137
|
+
monkeypatch.setattr(
|
|
138
|
+
"gvt.app.subprocess.run", lambda cmd, **k: runs.append(cmd) or subprocess.CompletedProcess(cmd, 0)
|
|
139
|
+
)
|
|
140
|
+
fixture_app.action_open_in_editor()
|
|
141
|
+
assert runs and runs[0][:2] == ["code", "--wait"]
|
|
142
|
+
assert runs[0][2].endswith("hello.py")
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def test_tmux_missing_from_path_is_ignored(monkeypatch):
|
|
146
|
+
"""With $TMUX set but no tmux binary, pane handoff does nothing instead of crashing."""
|
|
147
|
+
from gvt.app import GVTApp
|
|
148
|
+
|
|
149
|
+
monkeypatch.setenv("TMUX", "/tmp/tmux-1/default,1,0")
|
|
150
|
+
|
|
151
|
+
def missing(*a, **k):
|
|
152
|
+
raise FileNotFoundError("tmux")
|
|
153
|
+
|
|
154
|
+
monkeypatch.setattr("gvt.app.subprocess.run", missing)
|
|
155
|
+
GVTApp._tmux_select_pane("L")
|
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
"""
|
|
2
|
+
gvt run against a repo from an untrusted source: control characters in commit
|
|
3
|
+
messages and file contents, markup-like and pathspec-like file names, non-ASCII
|
|
4
|
+
names, and a hostile .git/config.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import os
|
|
8
|
+
import stat
|
|
9
|
+
|
|
10
|
+
import pytest
|
|
11
|
+
from git import Repo
|
|
12
|
+
|
|
13
|
+
from textual.content import Content
|
|
14
|
+
|
|
15
|
+
from gvt.app import GVTApp
|
|
16
|
+
from gvt.git.repo import GitRepo
|
|
17
|
+
from gvt.widgets.file_tree import FileTreeWidget
|
|
18
|
+
from gvt.widgets.timeline import TimelineWidget
|
|
19
|
+
|
|
20
|
+
ESC_TITLE = "\x1b]0;PWNED\x07"
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _write(repo_dir: str, path: str, text: str, mode: str = "w") -> None:
|
|
24
|
+
full = os.path.join(repo_dir, path)
|
|
25
|
+
os.makedirs(os.path.dirname(full) or repo_dir, exist_ok=True)
|
|
26
|
+
with open(full, mode, newline="") as f:
|
|
27
|
+
f.write(text)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@pytest.fixture
|
|
31
|
+
def hostile_repo(tmp_path):
|
|
32
|
+
"""
|
|
33
|
+
A repo whose names, messages and contents try to attack the terminal and gvt.
|
|
34
|
+
"""
|
|
35
|
+
repo_dir = str(tmp_path / "hostile")
|
|
36
|
+
os.makedirs(repo_dir)
|
|
37
|
+
repo = Repo.init(repo_dir)
|
|
38
|
+
with repo.config_writer() as cw:
|
|
39
|
+
cw.set_value("user", "name", f"Mal{ESC_TITLE}lory")
|
|
40
|
+
cw.set_value("user", "email", "m@example.com")
|
|
41
|
+
|
|
42
|
+
_write(repo_dir, "normal.py", "a\n")
|
|
43
|
+
_write(repo_dir, "[/]bad.py", "b\n")
|
|
44
|
+
_write(repo_dir, "[red]styled.py", "c\n")
|
|
45
|
+
_write(repo_dir, "café.py", "d\n")
|
|
46
|
+
_write(repo_dir, "app/[slug]/page.tsx", "slug v1\n")
|
|
47
|
+
_write(repo_dir, "app/s/page.tsx", "s v1\n")
|
|
48
|
+
_write(repo_dir, "content.txt", "plain\r\nwindows line\r\n")
|
|
49
|
+
repo.git.add("-A")
|
|
50
|
+
repo.git.commit("-m", "[/] unbalanced close tag")
|
|
51
|
+
|
|
52
|
+
_write(repo_dir, "normal.py", "a\na2\n")
|
|
53
|
+
_write(repo_dir, "content.txt", f"plain\r\nwindows line\r\n{ESC_TITLE} hidden\n")
|
|
54
|
+
repo.git.add("-A")
|
|
55
|
+
repo.git.commit("-m", f"escape {ESC_TITLE} and \x1b[2J clear")
|
|
56
|
+
|
|
57
|
+
_write(repo_dir, "app/s/page.tsx", "s v2\n")
|
|
58
|
+
repo.git.add("-A")
|
|
59
|
+
repo.git.commit("-m", "Only the s page")
|
|
60
|
+
|
|
61
|
+
_write(repo_dir, "normal.py", "a\na2\na3\n")
|
|
62
|
+
_write(repo_dir, "café.py", "d\nd2\n")
|
|
63
|
+
repo.git.add("-A")
|
|
64
|
+
repo.git.commit("-m", "Subject with ---GVT_START--- inside")
|
|
65
|
+
return repo_dir
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def _no_raw_controls(text: str) -> bool:
|
|
69
|
+
return "\x1b" not in text and "\x07" not in text
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
class TestGitBoundary:
|
|
73
|
+
def test_commit_messages_and_authors_are_safe(self, hostile_repo):
|
|
74
|
+
repo = GitRepo(hostile_repo)
|
|
75
|
+
commits = repo.get_file_commits("normal.py") + repo.get_all_commits()
|
|
76
|
+
assert commits
|
|
77
|
+
for c in commits:
|
|
78
|
+
assert _no_raw_controls(c.message), c.message
|
|
79
|
+
assert _no_raw_controls(c.author), c.author
|
|
80
|
+
assert any("\\x1b]0;PWNED\\x07" in c.message for c in commits)
|
|
81
|
+
|
|
82
|
+
def test_file_contents_and_diffs_are_safe(self, hostile_repo):
|
|
83
|
+
repo = GitRepo(hostile_repo)
|
|
84
|
+
commits = repo.get_file_commits("content.txt")
|
|
85
|
+
diff = repo.get_diff("content.txt", commits[0].hexsha, commits[-1].hexsha)
|
|
86
|
+
content = repo.get_file_at_commit("content.txt", commits[-1].hexsha)
|
|
87
|
+
assert "PWNED" in diff and _no_raw_controls(diff)
|
|
88
|
+
assert "PWNED" in content and _no_raw_controls(content)
|
|
89
|
+
assert "\r" not in content
|
|
90
|
+
assert _no_raw_controls(repo.get_file_content("content.txt"))
|
|
91
|
+
|
|
92
|
+
def test_blame_and_contributors_are_safe(self, hostile_repo):
|
|
93
|
+
repo = GitRepo(hostile_repo)
|
|
94
|
+
sha = repo.get_file_commits("normal.py")[-1].hexsha
|
|
95
|
+
assert all(_no_raw_controls(author) for _, author, _ in repo.get_blame("normal.py", sha))
|
|
96
|
+
assert all(_no_raw_controls(name) for name, _ in repo.get_file_contributors("normal.py"))
|
|
97
|
+
|
|
98
|
+
def test_paths_are_literal_not_pathspecs(self, hostile_repo):
|
|
99
|
+
repo = GitRepo(hostile_repo)
|
|
100
|
+
slug = repo.get_file_commits("app/[slug]/page.tsx")
|
|
101
|
+
assert [c.message for c in slug] == ["[/] unbalanced close tag"]
|
|
102
|
+
s_commits = repo.get_file_commits("app/s/page.tsx")
|
|
103
|
+
assert len(s_commits) == 2
|
|
104
|
+
diff = repo.get_diff("app/[slug]/page.tsx", s_commits[0].hexsha, s_commits[-1].hexsha)
|
|
105
|
+
assert "s v2" not in diff
|
|
106
|
+
|
|
107
|
+
def test_non_ascii_file_names(self, hostile_repo):
|
|
108
|
+
repo = GitRepo(hostile_repo)
|
|
109
|
+
assert "café.py" in repo.get_tracked_files()
|
|
110
|
+
assert len(repo.get_file_commits("café.py")) == 2
|
|
111
|
+
newest = repo.get_all_commits()[0]
|
|
112
|
+
assert ("café.py", 1, 0) in repo.get_commit_files(newest.hexsha)
|
|
113
|
+
|
|
114
|
+
def test_record_marker_in_subject_does_not_break_parsing(self, hostile_repo):
|
|
115
|
+
repo = GitRepo(hostile_repo)
|
|
116
|
+
newest = repo.get_all_commits()[0]
|
|
117
|
+
assert newest.message == "Subject with ---GVT_START--- inside"
|
|
118
|
+
assert (newest.additions, newest.deletions) == (2, 0)
|
|
119
|
+
normal = repo.get_file_commits("normal.py")
|
|
120
|
+
assert normal[-1].message == "Subject with ---GVT_START--- inside"
|
|
121
|
+
assert normal[-1].additions == 1
|
|
122
|
+
|
|
123
|
+
def test_control_characters_in_file_names_never_reach_output(self, tmp_path):
|
|
124
|
+
repo_dir = str(tmp_path / "ctlnames")
|
|
125
|
+
os.makedirs(repo_dir)
|
|
126
|
+
repo = Repo.init(repo_dir)
|
|
127
|
+
with repo.config_writer() as cw:
|
|
128
|
+
cw.set_value("user", "name", "T")
|
|
129
|
+
cw.set_value("user", "email", "t@example.com")
|
|
130
|
+
_write(repo_dir, f"evil{ESC_TITLE}.txt", "x\n")
|
|
131
|
+
repo.git.add("-A")
|
|
132
|
+
repo.git.commit("-m", "init")
|
|
133
|
+
gvt = GitRepo(repo_dir)
|
|
134
|
+
listed = gvt.get_tracked_files() + [f[0] for f in gvt.get_commit_files(gvt.get_all_commits()[0].hexsha)]
|
|
135
|
+
assert listed and all(_no_raw_controls(path) for path in listed)
|
|
136
|
+
|
|
137
|
+
def test_markup_named_paths_listed(self, hostile_repo):
|
|
138
|
+
files = GitRepo(hostile_repo).get_tracked_files()
|
|
139
|
+
assert "[/]bad.py" in files and "[red]styled.py" in files
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def _marker_script(tmp_path, name: str) -> tuple[str, str]:
|
|
143
|
+
marker = str(tmp_path / f"{name}.ran")
|
|
144
|
+
script = tmp_path / f"{name}.sh"
|
|
145
|
+
script.write_text(f"#!/bin/sh\ntouch {marker}\n")
|
|
146
|
+
script.chmod(script.stat().st_mode | stat.S_IEXEC)
|
|
147
|
+
return str(script), marker
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
class TestHostileGitConfig:
|
|
151
|
+
def test_external_diff_and_textconv_do_not_run(self, hostile_repo, tmp_path):
|
|
152
|
+
ext, ext_marker = _marker_script(tmp_path, "extdiff")
|
|
153
|
+
conv, conv_marker = _marker_script(tmp_path, "textconv")
|
|
154
|
+
_write(hostile_repo, ".gitattributes", "*.py diff=evil\n")
|
|
155
|
+
repo = Repo(hostile_repo)
|
|
156
|
+
with repo.config_writer() as cw:
|
|
157
|
+
cw.set_value("diff", "external", ext)
|
|
158
|
+
cw.set_value('diff "evil"', "textconv", conv)
|
|
159
|
+
gvt = GitRepo(hostile_repo)
|
|
160
|
+
commits = gvt.get_file_commits("normal.py")
|
|
161
|
+
assert "a2" in gvt.get_diff("normal.py", commits[0].hexsha, commits[-1].hexsha)
|
|
162
|
+
gvt.get_blame("normal.py", commits[-1].hexsha)
|
|
163
|
+
_write(hostile_repo, "normal.py", "changed\n")
|
|
164
|
+
gvt.has_uncommitted_changes("normal.py")
|
|
165
|
+
gvt.get_diff_to_working_tree("normal.py", commits[-1].hexsha)
|
|
166
|
+
assert not os.path.exists(ext_marker)
|
|
167
|
+
assert not os.path.exists(conv_marker)
|
|
168
|
+
|
|
169
|
+
def test_fsmonitor_does_not_run(self, hostile_repo, tmp_path):
|
|
170
|
+
hook, marker = _marker_script(tmp_path, "fsmonitor")
|
|
171
|
+
with Repo(hostile_repo).config_writer() as cw:
|
|
172
|
+
cw.set_value("core", "fsmonitor", hook)
|
|
173
|
+
gvt = GitRepo(hostile_repo)
|
|
174
|
+
gvt.get_untracked_files()
|
|
175
|
+
gvt.has_uncommitted_changes("normal.py")
|
|
176
|
+
gvt.get_commit_files(gvt.get_all_commits()[0].hexsha)
|
|
177
|
+
assert not os.path.exists(marker)
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
class TestApp:
|
|
181
|
+
SIZE = (120, 40)
|
|
182
|
+
|
|
183
|
+
@pytest.mark.parametrize("path", ["[/]bad.py", "[red]styled.py", "normal.py", "café.py"])
|
|
184
|
+
async def test_opening_hostile_files_does_not_crash(self, hostile_repo, path):
|
|
185
|
+
app = GVTApp(repo_path=hostile_repo, initial_file=path)
|
|
186
|
+
async with app.run_test(size=self.SIZE) as pilot:
|
|
187
|
+
await pilot.pause()
|
|
188
|
+
await app.workers.wait_for_complete()
|
|
189
|
+
await pilot.pause()
|
|
190
|
+
for key in ("h", "l", "B", "c"):
|
|
191
|
+
await pilot.press(key)
|
|
192
|
+
await pilot.pause()
|
|
193
|
+
assert app.is_running
|
|
194
|
+
assert app.current_file == path
|
|
195
|
+
assert app.query_one("#timeline-widget", TimelineWidget).commits
|
|
196
|
+
|
|
197
|
+
async def test_border_title_shows_path_literally(self, hostile_repo):
|
|
198
|
+
app = GVTApp(repo_path=hostile_repo, initial_file="[red]styled.py")
|
|
199
|
+
async with app.run_test(size=self.SIZE) as pilot:
|
|
200
|
+
await pilot.pause()
|
|
201
|
+
await app.workers.wait_for_complete()
|
|
202
|
+
await pilot.pause()
|
|
203
|
+
title = app.query_one("#diff-pane").border_title
|
|
204
|
+
# Rendered, the title must still show the brackets, not apply a style
|
|
205
|
+
assert "[red]styled.py" in Content.from_markup(str(title)).plain
|
|
206
|
+
|
|
207
|
+
async def test_tree_labels_show_names_literally(self, hostile_repo):
|
|
208
|
+
app = GVTApp(repo_path=hostile_repo)
|
|
209
|
+
async with app.run_test(size=self.SIZE) as pilot:
|
|
210
|
+
await pilot.pause()
|
|
211
|
+
tree = app.query_one(FileTreeWidget)._tree
|
|
212
|
+
labels = [str(child.label) for child in tree.root.children]
|
|
213
|
+
assert "[red]styled.py" in labels
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
# Characters git passes through raw with core.quotePath=false: C1 controls and
|
|
217
|
+
# bidi overrides (these can reorder or control what a terminal shows)
|
|
218
|
+
UNSAFE_DISPLAY = ("", "\x9b", "")
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def _display_safe(text: str) -> bool:
|
|
222
|
+
return not any(ch in text for ch in UNSAFE_DISPLAY) and _no_raw_controls(text)
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
@pytest.fixture
|
|
226
|
+
def unicode_tricks_repo(tmp_path):
|
|
227
|
+
repo_dir = str(tmp_path / "tricks")
|
|
228
|
+
os.makedirs(repo_dir)
|
|
229
|
+
repo = Repo.init(repo_dir)
|
|
230
|
+
with repo.config_writer() as cw:
|
|
231
|
+
cw.set_value("user", "name", "T")
|
|
232
|
+
cw.set_value("user", "email", "t@example.com")
|
|
233
|
+
_write(repo_dir, "biditxt.py", "x\n")
|
|
234
|
+
_write(repo_dir, "c1\x9bname.py", "y\n")
|
|
235
|
+
repo.git.add("-A")
|
|
236
|
+
repo.git.commit("-m", "init")
|
|
237
|
+
repo.git.tag("v1rtl")
|
|
238
|
+
return repo_dir
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
class TestDisplayedNames:
|
|
242
|
+
SIZE = (120, 40)
|
|
243
|
+
|
|
244
|
+
def test_ref_names_are_safe(self, unicode_tricks_repo):
|
|
245
|
+
commits = GitRepo(unicode_tricks_repo).get_all_commits()
|
|
246
|
+
assert "v1" in commits[0].refs
|
|
247
|
+
assert _display_safe(commits[0].refs)
|
|
248
|
+
|
|
249
|
+
async def test_file_names_with_c1_and_bidi_are_shown_escaped(self, unicode_tricks_repo):
|
|
250
|
+
from gvt.widgets.changed_files import ChangedFilesWidget
|
|
251
|
+
from gvt.widgets.status_bar import GVTStatusBar
|
|
252
|
+
|
|
253
|
+
path = "biditxt.py"
|
|
254
|
+
app = GVTApp(repo_path=unicode_tricks_repo, initial_file=path)
|
|
255
|
+
async with app.run_test(size=self.SIZE) as pilot:
|
|
256
|
+
await pilot.pause()
|
|
257
|
+
await app.workers.wait_for_complete()
|
|
258
|
+
await pilot.pause()
|
|
259
|
+
assert app.current_file == path # the real name still opens the file
|
|
260
|
+
|
|
261
|
+
title = Content.from_markup(str(app.query_one("#diff-pane").border_title)).plain
|
|
262
|
+
assert "bidi" in title and _display_safe(title)
|
|
263
|
+
|
|
264
|
+
tree = app.query_one(FileTreeWidget)._tree
|
|
265
|
+
labels = [str(child.label) for child in tree.root.children]
|
|
266
|
+
assert labels and all(_display_safe(label) for label in labels)
|
|
267
|
+
|
|
268
|
+
status = app.query_one(GVTStatusBar)._render_info_line().plain
|
|
269
|
+
assert "bidi" in status and _display_safe(status)
|
|
270
|
+
|
|
271
|
+
changed = app.query_one(ChangedFilesWidget)
|
|
272
|
+
rendered = " ".join(line.plain for line in changed._lines)
|
|
273
|
+
assert "bidi" in rendered and _display_safe(rendered)
|
|
274
|
+
|
|
275
|
+
async def test_commit_files_picker_shows_names_escaped(self, unicode_tricks_repo):
|
|
276
|
+
from gvt.widgets.modals import CommitFilesModal
|
|
277
|
+
|
|
278
|
+
app = GVTApp(repo_path=unicode_tricks_repo)
|
|
279
|
+
async with app.run_test(size=self.SIZE) as pilot:
|
|
280
|
+
await pilot.pause()
|
|
281
|
+
app._all_commits_cache = app.git_repo.get_all_commits()
|
|
282
|
+
await pilot.press("c")
|
|
283
|
+
await pilot.pause()
|
|
284
|
+
await pilot.press("enter")
|
|
285
|
+
await pilot.pause()
|
|
286
|
+
assert isinstance(app.screen, CommitFilesModal)
|
|
287
|
+
text = " ".join(str(s.render()) for s in app.screen.query("Static"))
|
|
288
|
+
assert "bidi" in text and _display_safe(text)
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
def test_far_future_commit_dates_do_not_crash(tmp_path):
|
|
292
|
+
repo_dir = str(tmp_path / "future")
|
|
293
|
+
os.makedirs(repo_dir)
|
|
294
|
+
repo = Repo.init(repo_dir)
|
|
295
|
+
with repo.config_writer() as cw:
|
|
296
|
+
cw.set_value("user", "name", "T")
|
|
297
|
+
cw.set_value("user", "email", "t@example.com")
|
|
298
|
+
_write(repo_dir, "a.txt", "x\n")
|
|
299
|
+
repo.git.add("a.txt")
|
|
300
|
+
stamp = "@300000000000 +0000" # year 11476
|
|
301
|
+
repo.git.commit("-m", "far", env={"GIT_AUTHOR_DATE": stamp, "GIT_COMMITTER_DATE": stamp})
|
|
302
|
+
gvt = GitRepo(repo_dir)
|
|
303
|
+
commits = gvt.get_file_commits("a.txt") + gvt.get_all_commits()
|
|
304
|
+
assert commits and all(c.date.year == 2100 for c in commits)
|
|
305
|
+
assert gvt.get_blame("a.txt", commits[0].hexsha)[0][2].startswith("2100")
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"""Tests for making repo-controlled text safe to show in a terminal."""
|
|
2
|
+
|
|
3
|
+
from gvt.textsafe import safe_text
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def test_plain_text_unchanged():
|
|
7
|
+
assert safe_text("Fix the parser (#12)\ttabs stay\nnewlines stay") == (
|
|
8
|
+
"Fix the parser (#12)\ttabs stay\nnewlines stay"
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def test_escape_sequences_become_visible():
|
|
13
|
+
assert safe_text("a\x1b]0;PWNED\x07b") == "a\\x1b]0;PWNED\\x07b"
|
|
14
|
+
assert safe_text("\x1b[2J") == "\\x1b[2J"
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def test_c1_and_del_controls_become_visible():
|
|
18
|
+
assert safe_text("x\x9by\x7fz") == "x\\x9by\\x7fz"
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def test_crlf_line_endings_are_normalized():
|
|
22
|
+
assert safe_text("one\r\ntwo\r\n") == "one\ntwo\n"
|
|
23
|
+
assert safe_text("trailing\r") == "trailing"
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def test_lone_carriage_return_is_visible():
|
|
27
|
+
assert safe_text("overwrite\rme") == "overwrite\\x0dme"
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def test_bidi_controls_become_visible():
|
|
31
|
+
# "Trojan Source": bidi overrides reorder what the terminal displays
|
|
32
|
+
assert safe_text("accesslevel") == "access\\u202elevel"
|
|
33
|
+
assert safe_text("x") == "\\u2066x\\u2069"
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def test_unicode_text_unchanged():
|
|
37
|
+
assert safe_text("café — naïve 日本") == "café — naïve 日本"
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
__version__ = "0.2.3"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|