Something went wrong. Try again.
fork of prime agent with changes i want but are not upstreamable probqbly (and some fixes)
Something went wrong. Try again.
5.4 kB · 119 lines
Python
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120from __future__ import annotations
import subprocessimport tempfileimport unittestfrom pathlib import Path
import git
class GitSurfaceTests(unittest.TestCase): def test_grep_streams_only_the_bounded_revision_matches(self) -> None: with tempfile.TemporaryDirectory(prefix="git-grep-test-") as temporary: root = Path(temporary) subprocess.run(["git", "init", "-q", str(root)], check=True) (root / "source.txt").write_text("needle one\nneedle two\n", encoding="utf-8") (root / "ignored.md").write_text("needle three\n", encoding="utf-8") subprocess.run(["git", "-C", str(root), "add", "source.txt", "ignored.md"], check=True) subprocess.run( [ "git", "-C", str(root), "-c", "user.name=test", "-c", "user.email=test@example.com", "commit", "-qm", "fixture", ], check=True, )
result = git.grep("needle", globs=("*.txt",), max_hits=1, path=str(root))
self.assertEqual(result, [("source.txt", 1, "needle one")])
def test_grep_rejects_unbounded_hit_limits(self) -> None: with self.assertRaisesRegex(ValueError, "max_hits must be between 1 and 1000"): git.grep("needle", max_hits=0)
def test_commit_list_renders_pretty_and_indexes_dicts(self) -> None: raw = [ { "hash": "1234567890abcdef", "short": "123456789", "author": "Alice", "email": "alice@example.com", "date": "2026-09-03T12:00:00Z", "refs": ["HEAD -> main", "origin/main"], "subject": "initial commit", } ] cl = git.CommitList(raw) self.assertEqual(len(cl), 1) self.assertEqual(cl[0]["hash"], "1234567890abcdef") self.assertEqual(cl[0]["subject"], "initial commit") self.assertIn("123456789 (HEAD -> main, origin/main) initial commit [Alice, 2026-09-03]", repr(cl)) self.assertIn("123456789", str(cl))
def test_run_rejects_subcommands_with_native_functions(self) -> None: for subcmd in ("diff", "log", "status", "add", "commit", "checkout", "switch", "reset", "restore"): with self.assertRaises(git.GitError) as ctx: git.run(subcmd) self.assertIn(f"git.run('{subcmd}', ...) is rejected", str(ctx.exception))
def test_run_allows_unmapped_subcommands(self) -> None: with tempfile.TemporaryDirectory(prefix="git-run-test-") as temporary: root = Path(temporary) subprocess.run(["git", "init", "-q", str(root)], check=True) res = git.run("rev-parse", "--is-inside-work-tree", path=str(root)) self.assertEqual(res.strip(), "true")
def test_diff_name_only_and_extra_flags(self) -> None: with tempfile.TemporaryDirectory(prefix="git-diff-test-") as temporary: root = Path(temporary) subprocess.run(["git", "init", "-q", str(root)], check=True) (root / "file.txt").write_text("hello", encoding="utf-8") subprocess.run(["git", "-C", str(root), "add", "file.txt"], check=True) subprocess.run( ["git", "-C", str(root), "-c", "user.name=test", "-c", "user.email=t@example.com", "commit", "-qm", "init"], check=True, ) (root / "file.txt").write_text("world", encoding="utf-8") res = git.diff("file.txt", path=str(root), name_only=True) self.assertIn("file.txt", res)
def test_log_with_format(self) -> None: with tempfile.TemporaryDirectory(prefix="git-log-test-") as temporary: root = Path(temporary) subprocess.run(["git", "init", "-q", str(root)], check=True) (root / "file.txt").write_text("hello", encoding="utf-8") subprocess.run(["git", "-C", str(root), "add", "file.txt"], check=True) subprocess.run( ["git", "-C", str(root), "-c", "user.name=test", "-c", "user.email=t@example.com", "commit", "-qm", "my_subject"], check=True, ) res = git.log(path=str(root), format="%s") self.assertEqual(res.strip(), "my_subject")
def test_awaitable_git_results(self) -> None: import asyncio with tempfile.TemporaryDirectory(prefix="git-await-test-") as temporary: root = Path(temporary) subprocess.run(["git", "init", "-q", str(root)], check=True) (root / "source.txt").write_text("hello\n", encoding="utf-8") subprocess.run(["git", "-C", str(root), "add", "source.txt"], check=True) subprocess.run( [ "git", "-C", str(root), "-c", "user.name=test", "-c", "user.email=test@example.com", "commit", "-qm", "fixture", ], check=True, ) async def _test(): st = await git.status(path=str(root)) lg = await git.log(path=str(root)) return st, lg status, log_entries = asyncio.run(_test()) self.assertIn("##", status) self.assertEqual(len(log_entries), 1)
if __name__ == "__main__": unittest.main()