Repository navigation
Expand file tree
/
Copy pathtest_protocol.py
More file actions
1101 lines (887 loc) · 40.5 KB
/
Copy pathtest_protocol.py
File metadata and controls
1101 lines (887 loc) · 40.5 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
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Characterization tests for mcp-server-git, driven through the protocol.
Each test runs the real `serve()` in-process (see `conftest.connect`) and
asserts on what a client receives: the wire JSON of the tool list and of each
tool result. They pin the server's *current* behavior on `mcp` 1.x, bugs
included, as the regression net for the SDK v2 port (#4851). A test that pins a
known bug says so and cites the issue (or says "no issue"); fixing the bug
means changing that test.
"""
import json
import re
from importlib.metadata import version
from pathlib import Path
from typing import Any
from unittest import mock
import git
import pytest
from mcp.shared.context import RequestContext
from mcp.types import ListRootsResult, Root
from pydantic import FileUrl
from conftest import connect, make_repo, text_result, wire
SNAPSHOTS = Path(__file__).parent / "snapshots"
def root_of(repo: git.Repo) -> Path:
assert repo.working_dir is not None
return Path(repo.working_dir)
async def call(
repository: Path | None, name: str, arguments: dict[str, Any]
) -> dict[str, Any]:
"""One tool call against a fresh server, as wire JSON."""
async with connect(repository) as session:
return wire(await session.call_tool(name, arguments))
def rejected(message: str) -> dict[str, Any]:
"""The error a flag-injection guard returns over the wire.
The guards raise `BadName(message)`, and BadName's `__str__` wraps its
argument as a ref name, so the client sees the guard's message nested
inside "Ref '...' did not resolve to an object". Characterized as is.
"""
return text_result(f"Ref '{message}' did not resolve to an object", is_error=True)
def staged_paths(repo: git.Repo) -> set[str]:
return set(repo.git.diff("--cached", "--name-only").splitlines())
# --------------------------------------------------------------------------
# Initialization and the tool list
# --------------------------------------------------------------------------
# KNOWN BUG #360: pins current (wrong) behavior; the fix changes this assertion.
async def test_initialize_reports_sdk_version_as_server_version(repo: git.Repo):
# Pins #360: serverInfo.version is the `mcp` SDK's version, not this
# package's, because serve() builds `Server("mcp-git")` without a version.
async with connect(root_of(repo), initialize=False) as session:
init = wire(await session.initialize())
assert init["serverInfo"] == {"name": "mcp-git", "version": version("mcp")}
assert version("mcp") != version("mcp-server-git")
assert init["capabilities"] == {
"experimental": {},
"tools": {"listChanged": False},
}
async def test_list_tools_matches_wire_snapshot():
expected = json.loads((SNAPSHOTS / "list_tools.json").read_text())
async with connect(None) as session:
tools = wire(await session.list_tools())
assert tools == expected
async def test_list_tools_names_in_order():
async with connect(None) as session:
tools = wire(await session.list_tools())["tools"]
assert [t["name"] for t in tools] == [
"git_status",
"git_diff_unstaged",
"git_diff_staged",
"git_diff",
"git_commit",
"git_add",
"git_reset",
"git_log",
"git_create_branch",
"git_checkout",
"git_show",
"git_branch",
]
# --------------------------------------------------------------------------
# Protocol-level errors common to every tool
# --------------------------------------------------------------------------
async def test_missing_required_argument_is_input_validation_error(repo: git.Repo):
assert await call(None, "git_status", {}) == text_result(
"Input validation error: 'repo_path' is a required property", is_error=True
)
async def test_unknown_tool_is_error(repo: git.Repo):
assert await call(
None, "git_nope", {"repo_path": str(root_of(repo))}
) == text_result("Unknown tool: git_nope", is_error=True)
# KNOWN BUG #4994: an unknown tool called without repo_path reports the KeyError repr "'repo_path'" instead of "Unknown tool"; the fix changes this assertion.
async def test_unknown_tool_without_repo_path_reports_the_key_error():
# An unlisted tool skips schema validation, so call_tool's
# `arguments["repo_path"]` raises KeyError and its repr is the message.
assert await call(None, "git_nope", {}) == text_result("'repo_path'", is_error=True)
async def test_nonexistent_repo_path_error_is_the_bare_path(tmp_path: Path):
# NoSuchPathError's message is just the path.
missing = tmp_path / "missing"
assert await call(None, "git_status", {"repo_path": str(missing)}) == (
text_result(str(missing), is_error=True)
)
async def test_non_repository_repo_path_error_is_the_bare_path(tmp_path: Path):
# InvalidGitRepositoryError's message is just the path.
plain = tmp_path / "plain"
plain.mkdir()
assert await call(None, "git_status", {"repo_path": str(plain)}) == (
text_result(str(plain), is_error=True)
)
async def test_repo_path_subdirectory_is_not_a_repository(repo: git.Repo):
# git.Repo() is opened without search_parent_directories, so a directory
# inside a working tree is rejected even though git itself accepts it.
sub = root_of(repo) / "sub"
sub.mkdir()
assert await call(None, "git_status", {"repo_path": str(sub)}) == text_result(
str(sub), is_error=True
)
# --------------------------------------------------------------------------
# git_status
# --------------------------------------------------------------------------
async def test_git_status_clean(repo: git.Repo):
assert await call(None, "git_status", {"repo_path": str(root_of(repo))}) == (
text_result(
"Repository status:\nOn branch main\nnothing to commit, working tree clean"
)
)
async def test_git_status_reports_untracked_and_modified(repo: git.Repo):
root = root_of(repo)
(root / "test.txt").write_text("changed\n")
(root / "new.txt").write_text("new\n")
result = await call(None, "git_status", {"repo_path": str(root)})
assert result == text_result(f"Repository status:\n{repo.git.status()}")
text = result["content"][0]["text"]
assert "modified: test.txt" in text
assert "new.txt" in text
# --------------------------------------------------------------------------
# git_diff_unstaged / git_diff_staged
# --------------------------------------------------------------------------
async def test_git_diff_unstaged(repo: git.Repo):
root = root_of(repo)
(root / "test.txt").write_text("line 1\nline 2\nCHANGED\nline 4\nline 5\n")
result = await call(None, "git_diff_unstaged", {"repo_path": str(root)})
assert result == text_result("Unstaged changes:\n" + repo.git.diff("--unified=3"))
assert "-line 3\n+CHANGED" in result["content"][0]["text"]
assert " line 1\n" in result["content"][0]["text"]
async def test_git_diff_unstaged_honors_context_lines(repo: git.Repo):
root = root_of(repo)
(root / "test.txt").write_text("line 1\nline 2\nCHANGED\nline 4\nline 5\n")
result = await call(
None, "git_diff_unstaged", {"repo_path": str(root), "context_lines": 0}
)
text = result["content"][0]["text"]
assert "@@ -3 +3 @@" in text
assert "\n line 1\n" not in text
async def test_git_diff_unstaged_empty(repo: git.Repo):
assert await call(
None, "git_diff_unstaged", {"repo_path": str(root_of(repo))}
) == text_result("Unstaged changes:\n")
async def test_git_diff_staged(repo: git.Repo):
root = root_of(repo)
(root / "test.txt").write_text("line 1\nline 2\nCHANGED\nline 4\nline 5\n")
repo.index.add(["test.txt"])
result = await call(None, "git_diff_staged", {"repo_path": str(root)})
assert result == text_result(
"Staged changes:\n" + repo.git.diff("--unified=3", "--cached")
)
assert "-line 3\n+CHANGED" in result["content"][0]["text"]
async def test_git_diff_staged_honors_context_lines(repo: git.Repo):
root = root_of(repo)
(root / "test.txt").write_text("line 1\nline 2\nCHANGED\nline 4\nline 5\n")
repo.index.add(["test.txt"])
result = await call(
None, "git_diff_staged", {"repo_path": str(root), "context_lines": 0}
)
text = result["content"][0]["text"]
assert "@@ -3 +3 @@" in text
assert "\n line 2\n" not in text
async def test_git_diff_staged_empty(repo: git.Repo):
assert await call(
None, "git_diff_staged", {"repo_path": str(root_of(repo))}
) == text_result("Staged changes:\n")
# --------------------------------------------------------------------------
# git_diff
# --------------------------------------------------------------------------
async def test_git_diff_against_branch(repo: git.Repo):
root = root_of(repo)
repo.git.checkout("-b", "feature")
(root / "test.txt").write_text("line 1\nline 2\nFEATURE\nline 4\nline 5\n")
repo.index.add(["test.txt"])
repo.index.commit("feature commit")
result = await call(None, "git_diff", {"repo_path": str(root), "target": "main"})
assert result == text_result(
"Diff with main:\n" + repo.git.diff("--unified=3", "main")
)
assert "+FEATURE" in result["content"][0]["text"]
async def test_git_diff_honors_context_lines(repo: git.Repo):
root = root_of(repo)
(root / "test.txt").write_text("line 1\nline 2\nCHANGED\nline 4\nline 5\n")
result = await call(
None,
"git_diff",
{"repo_path": str(root), "target": "HEAD", "context_lines": 0},
)
assert result == text_result(
"Diff with HEAD:\n" + repo.git.diff("--unified=0", "HEAD")
)
async def test_git_diff_unknown_target_is_error(repo: git.Repo):
assert await call(
None, "git_diff", {"repo_path": str(root_of(repo)), "target": "nope"}
) == text_result("Ref 'nope' did not resolve to an object", is_error=True)
# KNOWN BUG #4999: the flag-injection guard's message reaches the client wrapped as "Ref '...' did not resolve to an object"; the fix changes this assertion.
async def test_git_diff_rejects_flag_injection(repo: git.Repo):
assert await call(
None,
"git_diff",
{"repo_path": str(root_of(repo)), "target": "--output=/tmp/evil"},
) == rejected("Invalid target: '--output=/tmp/evil' - cannot start with '-'")
# --------------------------------------------------------------------------
# git_commit
# --------------------------------------------------------------------------
async def test_git_commit_records_staged_changes(repo: git.Repo):
root = root_of(repo)
(root / "new.txt").write_text("new\n")
repo.index.add(["new.txt"])
result = await call(
None, "git_commit", {"repo_path": str(root), "message": "add new"}
)
head = repo.head.commit
assert result == text_result(
f"Changes committed successfully with hash {head.hexsha}"
)
assert head.message == "add new"
assert "new.txt" in head.stats.files
# KNOWN BUG #4762: pins current (wrong) behavior; the fix changes this assertion.
async def test_git_commit_with_nothing_staged_creates_empty_commit(repo: git.Repo):
# Pins #4762: with nothing staged, git_commit still writes a commit (whose
# tree equals its parent's) and reports success. `git commit` would refuse
# with "no changes added to commit". Fixing #4762 changes this test.
root = root_of(repo)
(root / "test.txt").write_text("edited but not staged\n")
before = repo.head.commit
result = await call(
None, "git_commit", {"repo_path": str(root), "message": "claims a fix"}
)
after = repo.head.commit
assert result == text_result(
f"Changes committed successfully with hash {after.hexsha}"
)
assert after.parents == (before,)
assert after.tree.hexsha == before.tree.hexsha
assert repo.is_dirty()
# --------------------------------------------------------------------------
# git_add
# --------------------------------------------------------------------------
async def test_git_add_specific_files(repo: git.Repo):
root = root_of(repo)
(root / "a.txt").write_text("a\n")
(root / "b.txt").write_text("b\n")
result = await call(None, "git_add", {"repo_path": str(root), "files": ["a.txt"]})
assert result == text_result("Files staged successfully")
assert staged_paths(repo) == {"a.txt"}
async def test_git_add_dot_stages_everything(repo: git.Repo):
root = root_of(repo)
(root / "a.txt").write_text("a\n")
(root / "b.txt").write_text("b\n")
result = await call(None, "git_add", {"repo_path": str(root), "files": ["."]})
assert result == text_result("Files staged successfully")
assert staged_paths(repo) == {"a.txt", "b.txt"}
async def test_git_add_dot_never_stages_git_dir(repo: git.Repo):
# Regression guard for #628: `git add .` must not stage `.git` itself.
root = root_of(repo)
(root / "a.txt").write_text("a\n")
await call(None, "git_add", {"repo_path": str(root), "files": ["."]})
entries = [str(path) for path, _stage in repo.index.entries]
assert not any(p == ".git" or p.startswith(".git/") for p in entries)
assert "a.txt" in entries
async def test_git_add_absolute_path_inside_repository(repo: git.Repo):
root = root_of(repo)
(root / "a.txt").write_text("a\n")
result = await call(
None, "git_add", {"repo_path": str(root), "files": [str(root / "a.txt")]}
)
assert result == text_result("Files staged successfully")
assert staged_paths(repo) == {"a.txt"}
async def test_git_add_file_starting_with_dash_is_a_path(repo: git.Repo):
# `--` separates the pathspec, so a dash-named file is staged, not parsed
# as an option.
root = root_of(repo)
(root / "-n").write_text("dash\n")
result = await call(None, "git_add", {"repo_path": str(root), "files": ["-n"]})
assert result == text_result("Files staged successfully")
assert staged_paths(repo) == {"-n"}
# KNOWN BUG #4763: pins current (wrong) behavior; the fix changes this assertion.
async def test_git_add_empty_list_reports_success_but_stages_nothing(
repo: git.Repo,
):
# Pins #4763: `files: []` runs `git add --`, a no-op, and still reports
# success. Fixing #4763 changes this test.
root = root_of(repo)
(root / "test.txt").write_text("edited\n")
result = await call(None, "git_add", {"repo_path": str(root), "files": []})
assert result == text_result("Files staged successfully")
assert staged_paths(repo) == set()
# KNOWN BUG #4763: pins current (wrong) behavior; the fix changes this assertion.
async def test_git_add_dot_on_clean_tree_reports_success(repo: git.Repo):
# Pins #4763: nothing to stage, success reported anyway.
result = await call(
None, "git_add", {"repo_path": str(root_of(repo)), "files": ["."]}
)
assert result == text_result("Files staged successfully")
assert staged_paths(repo) == set()
async def test_git_add_nonexistent_file_is_git_error(repo: git.Repo):
result = await call(
None, "git_add", {"repo_path": str(root_of(repo)), "files": ["nope.txt"]}
)
assert result["isError"] is True
text = result["content"][0]["text"]
assert "cmdline: git add -- nope.txt" in text
assert "pathspec 'nope.txt' did not match any files" in text
async def test_git_add_rejects_relative_traversal(repo: git.Repo):
# CVE-2026-27735: a path escaping the working tree is refused before git
# runs.
root = root_of(repo)
(root.parent / "outside.txt").write_text("secret\n")
result = await call(
None, "git_add", {"repo_path": str(root), "files": ["../outside.txt"]}
)
assert result == text_result(
f"Path '../outside.txt' is outside the repository '{root.resolve()}'",
is_error=True,
)
assert staged_paths(repo) == set()
async def test_git_add_rejects_absolute_path_outside(repo: git.Repo):
root = root_of(repo)
outside = root.parent / "outside.txt"
outside.write_text("secret\n")
result = await call(
None, "git_add", {"repo_path": str(root), "files": [str(outside)]}
)
assert result == text_result(
f"Path '{outside}' is outside the repository '{root.resolve()}'",
is_error=True,
)
async def test_git_add_rejects_symlink_out_of_repository(repo: git.Repo):
root = root_of(repo)
outside = root.parent / "outside.txt"
outside.write_text("secret\n")
(root / "link.txt").symlink_to(outside)
result = await call(
None, "git_add", {"repo_path": str(root), "files": ["link.txt"]}
)
assert result == text_result(
f"Path 'link.txt' is outside the repository '{root.resolve()}'",
is_error=True,
)
async def test_git_add_unresolvable_path_is_invalid_path(repo: git.Repo):
# Path.resolve() raising (a symlink loop on some Pythons, an OS error)
# becomes "Invalid path". Simulated so the test does not depend on which
# Python versions raise for a loop.
root = root_of(repo)
real_resolve = Path.resolve
def resolve(self: Path, strict: bool = False) -> Path:
if self.name == "unresolvable":
raise OSError("simulated")
return real_resolve(self, strict)
with mock.patch.object(Path, "resolve", resolve):
result = await call(
None, "git_add", {"repo_path": str(root), "files": ["unresolvable"]}
)
assert result == text_result("Invalid path: 'unresolvable'", is_error=True)
# --------------------------------------------------------------------------
# git_reset
# --------------------------------------------------------------------------
async def test_git_reset_unstages_everything(repo: git.Repo):
root = root_of(repo)
(root / "a.txt").write_text("a\n")
(root / "test.txt").write_text("edited\n")
repo.index.add(["a.txt", "test.txt"])
assert staged_paths(repo) == {"a.txt", "test.txt"}
result = await call(None, "git_reset", {"repo_path": str(root)})
assert result == text_result("All staged changes reset")
assert staged_paths(repo) == set()
# The working tree is untouched.
assert (root / "test.txt").read_text() == "edited\n"
# --------------------------------------------------------------------------
# git_log
# --------------------------------------------------------------------------
def log_entry(commit: git.Commit) -> str:
return (
f"Commit: {commit.hexsha}\n"
f"Author: {commit.author}\n"
f"Date: {commit.authored_datetime}\n"
f"Message: {commit.message}\n"
)
def add_commits(repo: git.Repo, n: int) -> list[git.Commit]:
root = root_of(repo)
commits = []
for i in range(n):
(root / f"f{i}.txt").write_text(f"{i}\n")
repo.index.add([f"f{i}.txt"])
commits.append(repo.index.commit(f"commit {i}\n\nbody {i}"))
return commits
async def test_git_log_default(repo: git.Repo):
commits = add_commits(repo, 2)
initial = repo.head.commit.parents[0].parents[0]
result = await call(None, "git_log", {"repo_path": str(root_of(repo))})
assert result == text_result(
"Commit history:\n"
+ "\n".join(log_entry(c) for c in [commits[1], commits[0], initial])
)
text = result["content"][0]["text"]
assert "Author: Test User\n" in text
assert "Message: commit 1\n\nbody 1\n" in text
async def test_git_log_max_count(repo: git.Repo):
commits = add_commits(repo, 3)
result = await call(
None, "git_log", {"repo_path": str(root_of(repo)), "max_count": 1}
)
assert result == text_result("Commit history:\n" + log_entry(commits[2]))
async def test_git_log_timestamps_filter(repo: git.Repo):
root = str(root_of(repo))
future = await call(
None, "git_log", {"repo_path": root, "start_timestamp": "2099-01-01"}
)
assert future == text_result("Commit history:\n")
past = await call(
None, "git_log", {"repo_path": root, "end_timestamp": "2000-01-01"}
)
assert past == text_result("Commit history:\n")
both = await call(
None,
"git_log",
{
"repo_path": root,
"start_timestamp": "2000-01-01",
"end_timestamp": "2099-01-01",
},
)
assert both == text_result("Commit history:\n" + log_entry(repo.head.commit))
async def test_git_log_rejects_timestamp_flag_injection(repo: git.Repo):
root = str(root_of(repo))
assert await call(
None, "git_log", {"repo_path": root, "start_timestamp": "--all"}
) == text_result(
"Invalid start_timestamp: '--all' - cannot start with '-'", is_error=True
)
assert await call(
None, "git_log", {"repo_path": root, "end_timestamp": "--all"}
) == text_result(
"Invalid end_timestamp: '--all' - cannot start with '-'", is_error=True
)
# --------------------------------------------------------------------------
# git_create_branch
# --------------------------------------------------------------------------
async def test_git_create_branch_from_active_branch(repo: git.Repo):
root = root_of(repo)
result = await call(
None, "git_create_branch", {"repo_path": str(root), "branch_name": "feat"}
)
assert result == text_result("Created branch 'feat' from 'main'")
assert repo.heads["feat"].commit == repo.heads["main"].commit
# Creating a branch does not switch to it.
assert repo.active_branch.name == "main"
async def test_git_create_branch_from_base_branch(repo: git.Repo):
root = root_of(repo)
repo.git.checkout("-b", "base")
add_commits(repo, 1)
repo.git.checkout("main")
result = await call(
None,
"git_create_branch",
{"repo_path": str(root), "branch_name": "derived", "base_branch": "base"},
)
assert result == text_result("Created branch 'derived' from 'base'")
assert repo.heads["derived"].commit == repo.heads["base"].commit
async def test_git_create_branch_unknown_base_is_error(repo: git.Repo):
assert await call(
None,
"git_create_branch",
{"repo_path": str(root_of(repo)), "branch_name": "x", "base_branch": "nope"},
) == text_result("No item found with id nope", is_error=True)
async def test_git_create_branch_existing_name_at_same_commit_is_error(
repo: git.Repo,
):
# GitPython's create_head accepts an existing branch that already points
# at the base commit; the server rejects it rather than report a creation.
assert await call(
None,
"git_create_branch",
{"repo_path": str(root_of(repo)), "branch_name": "main"},
) == text_result(
"Cannot create branch 'main': refs/heads/main already exists", is_error=True
)
async def test_git_create_branch_name_matching_list_attribute(repo: git.Repo):
# The existence check compares head names, so a new branch named like an
# attribute of GitPython's IterableList ("append") is still created.
assert await call(
None,
"git_create_branch",
{"repo_path": str(root_of(repo)), "branch_name": "append"},
) == text_result("Created branch 'append' from 'main'")
assert repo.commit("refs/heads/append") == repo.heads["main"].commit
async def test_git_create_branch_existing_name_at_other_commit_is_error(
repo: git.Repo,
):
repo.git.branch("old")
add_commits(repo, 1)
result = await call(
None,
"git_create_branch",
{"repo_path": str(root_of(repo)), "branch_name": "old"},
)
assert result["isError"] is True
assert "refs/heads/old" in result["content"][0]["text"]
assert "already exist" in result["content"][0]["text"]
# KNOWN BUG #4999: the flag-injection guard's message reaches the client wrapped as "Ref '...' did not resolve to an object"; the fix changes this assertion.
async def test_git_create_branch_rejects_flag_injection(repo: git.Repo):
root = str(root_of(repo))
assert await call(
None, "git_create_branch", {"repo_path": root, "branch_name": "-f"}
) == rejected("Invalid branch name: '-f' - cannot start with '-'")
assert await call(
None,
"git_create_branch",
{"repo_path": root, "branch_name": "x", "base_branch": "--track"},
) == rejected("Invalid base branch: '--track' - cannot start with '-'")
# --------------------------------------------------------------------------
# git_checkout
# --------------------------------------------------------------------------
async def test_git_checkout_branch(repo: git.Repo):
repo.git.branch("feature")
result = await call(
None,
"git_checkout",
{"repo_path": str(root_of(repo)), "branch_name": "feature"},
)
assert result == text_result("Switched to branch 'feature'")
assert repo.active_branch.name == "feature"
# KNOWN BUG #4804: pins current (wrong) behavior; the fix changes this assertion.
@pytest.mark.parametrize("revision", ["sha", "tag", "HEAD~1", "refs/heads/feature"])
async def test_git_checkout_non_branch_detaches_head_but_claims_switch(
repo: git.Repo, revision: str
):
# Pins #4804: any revision rev_parse accepts is checked out, detaching
# HEAD, and the reply still says "Switched to branch". Fixing #4804 changes
# this test.
add_commits(repo, 1)
repo.git.branch("feature")
repo.create_tag("v1", ref="HEAD~1")
name = {
"sha": repo.head.commit.hexsha[:7],
"tag": "v1",
}.get(revision, revision)
result = await call(
None, "git_checkout", {"repo_path": str(root_of(repo)), "branch_name": name}
)
assert result == text_result(f"Switched to branch '{name}'")
assert repo.head.is_detached
async def test_git_checkout_unknown_branch_is_error(repo: git.Repo):
assert await call(
None,
"git_checkout",
{"repo_path": str(root_of(repo)), "branch_name": "nope"},
) == text_result("Ref 'nope' did not resolve to an object", is_error=True)
assert repo.active_branch.name == "main"
# KNOWN BUG #4999: the flag-injection guard's message reaches the client wrapped as "Ref '...' did not resolve to an object"; the fix changes this assertion.
async def test_git_checkout_rejects_flag_injection(repo: git.Repo):
assert await call(
None,
"git_checkout",
{"repo_path": str(root_of(repo)), "branch_name": "--orphan=evil"},
) == rejected("Invalid branch name: '--orphan=evil' - cannot start with '-'")
# --------------------------------------------------------------------------
# git_show
# --------------------------------------------------------------------------
def without_addresses(text: str) -> str:
"""Mask CPython object addresses (`object at 0x...`)."""
return re.sub(r" at 0x[0-9a-fA-F]+", " at 0x?", text)
def show_text(result: dict[str, Any]) -> str:
assert result["isError"] is False
return without_addresses(result["content"][0]["text"])
def show_header(commit: git.Commit) -> str:
# git_show formats its header with !r, so the reply carries Python reprs:
# a quoted sha, `<git.Actor ...>`, and a `datetime.datetime(...)` whose
# tzinfo repr includes a memory address. Compared with addresses masked.
return without_addresses(
f"Commit: {commit.hexsha!r}\n"
f"Author: {commit.author!r}\n"
f"Date: {commit.authored_datetime!r}\n"
f"Message: {commit.message!r}\n"
)
# KNOWN BUG #4998: git_show prints Python reprs (quoted sha, <git.Actor>, datetime with a memory address) instead of git's format; the fix changes this assertion.
async def test_git_show_commit_with_parent(repo: git.Repo):
root = root_of(repo)
(root / "test.txt").write_text("line 1\nline 2\nSHOWN\nline 4\nline 5\n")
repo.index.add(["test.txt"])
commit = repo.index.commit("show me")
result = await call(
None, "git_show", {"repo_path": str(root), "revision": commit.hexsha}
)
assert show_text(result) == (
show_header(commit)
+ "\n--- test.txt\n+++ test.txt\n"
+ "@@ -1,5 +1,5 @@\n line 1\n line 2\n-line 3\n+SHOWN\n line 4\n line 5\n"
)
text = show_text(result)
assert f"Commit: '{commit.hexsha}'\n" in text
assert 'Author: <git.Actor "Test User <test@example.com>">\n' in text
assert "Date: datetime.datetime(" in text
assert "Message: 'show me'\n" in text
# KNOWN BUG #4998: git_show prints Python reprs (with a memory address) and "--- None" where git prints /dev/null; the fix changes this assertion.
async def test_git_show_initial_commit_diffs_against_empty_tree(repo: git.Repo):
# With no parent the commit is diffed against NULL_TREE. An added file has
# no a_path, so the header prints Python's `None` where git prints
# `/dev/null`.
initial = repo.head.commit
result = await call(
None, "git_show", {"repo_path": str(root_of(repo)), "revision": "HEAD"}
)
assert show_text(result) == (
show_header(initial)
+ "\n--- None\n+++ test.txt\n"
+ "@@ -0,0 +1,5 @@\n+line 1\n+line 2\n+line 3\n+line 4\n+line 5\n"
)
# KNOWN BUG #4998: git_show prints Python reprs (quoted sha, <git.Actor>, datetime with a memory address) instead of git's format; the fix changes this assertion.
async def test_git_show_rename_only_commit_has_header_and_no_patch(repo: git.Repo):
# A pure rename yields a diff entry whose patch is empty bytes.
repo.index.move(["test.txt", "renamed.txt"])
commit = repo.index.commit("rename only")
result = await call(
None, "git_show", {"repo_path": str(root_of(repo)), "revision": "HEAD"}
)
assert show_text(result) == (
show_header(commit) + "\n--- test.txt\n+++ renamed.txt\n"
)
# KNOWN BUG #4998: git_show prints Python reprs (quoted sha, <git.Actor>, datetime with a memory address) instead of git's format; the fix changes this assertion.
async def test_git_show_binary_commit(repo: git.Repo):
root = root_of(repo)
(root / "blob.bin").write_bytes(bytes(range(256)))
repo.index.add(["blob.bin"])
commit = repo.index.commit("binary")
result = await call(None, "git_show", {"repo_path": str(root), "revision": "HEAD"})
assert show_text(result) == (
show_header(commit)
+ "\n--- blob.bin\n+++ blob.bin\n"
+ "Binary files /dev/null and b/blob.bin differ\n"
)
# KNOWN BUG #4997: git_show decodes the patch as strict UTF-8, so a Latin-1 file fails the whole call; the fix changes this assertion.
async def test_git_show_non_utf8_patch_is_decode_error(repo: git.Repo):
# Characterization: the patch bytes are decoded as strict UTF-8, so a
# Latin-1 text file makes the whole call fail.
root = root_of(repo)
(root / "latin.txt").write_bytes("caf\xe9\n".encode("latin-1"))
repo.index.add(["latin.txt"])
repo.index.commit("latin-1")
result = await call(None, "git_show", {"repo_path": str(root), "revision": "HEAD"})
assert result["isError"] is True
assert "codec can't decode byte 0xe9" in result["content"][0]["text"]
# KNOWN BUG #1682: pins current (wrong) behavior; the fix changes this assertion.
async def test_git_show_revision_path_syntax_is_error(repo: git.Repo):
# Pins #1682's current error: `<rev>:<path>` names a blob, not a commit,
# and repo.commit() appends `^0`, so the call fails with this message
# rather than crashing the server.
result = await call(
None,
"git_show",
{"repo_path": str(root_of(repo)), "revision": "HEAD:test.txt"},
)
assert result == text_result(
"\"Blob or Tree named 'test.txt^0' not found\"", is_error=True
)
async def test_git_show_unknown_revision_is_error(repo: git.Repo):
assert await call(
None, "git_show", {"repo_path": str(root_of(repo)), "revision": "nope"}
) == text_result("Ref 'nope' did not resolve to an object", is_error=True)
# KNOWN BUG #4999: the flag-injection guard's message reaches the client wrapped as "Ref '...' did not resolve to an object"; the fix changes this assertion.
async def test_git_show_rejects_flag_injection(repo: git.Repo):
assert await call(
None, "git_show", {"repo_path": str(root_of(repo)), "revision": "--format=x"}
) == rejected("Invalid revision: '--format=x' - cannot start with '-'")
# --------------------------------------------------------------------------
# git_branch
# --------------------------------------------------------------------------
async def test_git_branch_local(repo: git.Repo):
repo.git.branch("feature")
assert await call(
None, "git_branch", {"repo_path": str(root_of(repo)), "branch_type": "local"}
) == text_result(" feature\n* main")
async def test_git_branch_remote_and_all(tmp_path: Path, repo: git.Repo):
clone = repo.clone(str(tmp_path / "clone"))
try:
clone.git.branch("local-only")
clone_root = root_of(clone)
remote = await call(
None, "git_branch", {"repo_path": str(clone_root), "branch_type": "remote"}
)
assert remote == text_result(clone.git.branch("-r"))
assert "origin/main" in remote["content"][0]["text"]
assert "local-only" not in remote["content"][0]["text"]
everything = await call(
None, "git_branch", {"repo_path": str(clone_root), "branch_type": "all"}
)
text = everything["content"][0]["text"]
assert "local-only" in text
assert "remotes/origin/main" in text
finally:
clone.close()
async def test_git_branch_contains_and_not_contains(repo: git.Repo):
root = str(root_of(repo))
repo.git.checkout("-b", "feature")
commit = add_commits(repo, 1)[0]
repo.git.checkout("main")
contains = await call(
None,
"git_branch",
{"repo_path": root, "branch_type": "local", "contains": commit.hexsha},
)
assert contains == text_result(" feature")
not_contains = await call(
None,
"git_branch",
{"repo_path": root, "branch_type": "local", "not_contains": commit.hexsha},
)
assert not_contains == text_result("* main")
async def test_git_branch_unknown_type_is_error(repo: git.Repo):
assert await call(
None, "git_branch", {"repo_path": str(root_of(repo)), "branch_type": "bogus"}
) == text_result("Invalid branch type: bogus", is_error=True)
# KNOWN BUG #4999: the flag-injection guard's message reaches the client wrapped as "Ref '...' did not resolve to an object"; the fix changes this assertion.
async def test_git_branch_rejects_flag_injection(repo: git.Repo):
root = str(root_of(repo))
assert await call(
None,
"git_branch",
{"repo_path": root, "branch_type": "local", "contains": "--x"},
) == rejected("Invalid contains value: '--x' - cannot start with '-'")
assert await call(
None,
"git_branch",
{"repo_path": root, "branch_type": "local", "not_contains": "--x"},
) == rejected("Invalid not_contains value: '--x' - cannot start with '-'")
# --------------------------------------------------------------------------
# Repository restriction (--repository) and the #4550 concerns
# --------------------------------------------------------------------------
def outside_error(repo_path: Path | str, allowed: Path) -> dict[str, Any]:
return text_result(
f"Repository path '{repo_path}' is outside the allowed repository "
f"'{allowed}'",
is_error=True,
)
async def test_without_repository_any_repository_is_reachable(tmp_path: Path):
# Pins #4550: with no --repository, validate_repo_path is a no-op and every
# tool reaches any repository on the filesystem. Restricting this changes
# this test.
first = make_repo(tmp_path / "first")
second = make_repo(tmp_path / "second")
try:
async with connect(None) as session:
for r in (first, second):
result = wire(
await session.call_tool(
"git_status", {"repo_path": str(root_of(r))}
)
)
assert result["isError"] is False
finally:
first.close()
second.close()
async def test_restricted_server_serves_its_repository(repo: git.Repo):
root = root_of(repo)
assert (await call(root, "git_status", {"repo_path": str(root)}))[
"isError"
] is False
async def test_restricted_server_rejects_other_repository(
tmp_path: Path, repo: git.Repo
):
root = root_of(repo)
other = make_repo(tmp_path / "other")
try:
assert await call(
root, "git_status", {"repo_path": str(root_of(other))}
) == outside_error(root_of(other), root)
finally:
other.close()
async def test_restricted_server_rejects_sibling_with_shared_prefix(
tmp_path: Path, repo: git.Repo
):
# `/x/repo-evil` starts with the string `/x/repo` but is not inside it; the
# check compares path components, not string prefixes.
root = root_of(repo)
evil = make_repo(tmp_path / "repo-evil")
try:
assert await call(
root, "git_status", {"repo_path": str(root_of(evil))}
) == outside_error(root_of(evil), root)
finally:
evil.close()
async def test_restricted_server_rejects_dotdot_traversal(
tmp_path: Path, repo: git.Repo
):
root = root_of(repo)
other = make_repo(tmp_path / "other")
try:
sneaky = f"{root}/../other"
assert await call(root, "git_status", {"repo_path": sneaky}) == outside_error(
sneaky, root
)
finally:
other.close()