Repository navigation
Expand file tree
/
Copy pathtest_server.py
More file actions
750 lines (533 loc) · 26.3 KB
/
Copy pathtest_server.py
File metadata and controls
750 lines (533 loc) · 26.3 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
import pytest
from pathlib import Path
import git
from git.exc import BadName
from mcp_server_git.server import (
git_checkout,
git_branch,
git_add,
git_status,
git_diff_unstaged,
git_diff_staged,
git_diff,
git_commit,
git_reset,
git_log,
git_create_branch,
git_show,
validate_repo_path,
serve,
)
import unittest.mock as mock
def test_git_checkout_existing_branch(test_repository):
test_repository.git.branch("test-branch")
result = git_checkout(test_repository, "test-branch")
assert "Switched to branch 'test-branch'" in result
assert test_repository.active_branch.name == "test-branch"
def test_git_checkout_nonexistent_branch(test_repository):
with pytest.raises(BadName):
git_checkout(test_repository, "nonexistent-branch")
def test_git_branch_local(test_repository):
test_repository.git.branch("new-branch-local")
result = git_branch(test_repository, "local")
assert "new-branch-local" in result
def test_git_branch_remote(test_repository):
result = git_branch(test_repository, "remote")
assert "" == result.strip() # Should be empty if no remote branches
def test_git_branch_all(test_repository):
test_repository.git.branch("new-branch-all")
result = git_branch(test_repository, "all")
assert "new-branch-all" in result
def test_git_branch_contains(test_repository):
# Get the default branch name (could be "main" or "master")
default_branch = test_repository.active_branch.name
# Create a new branch and commit to it
test_repository.git.checkout("-b", "feature-branch")
Path(test_repository.working_dir / Path("feature.txt")).write_text(
"feature content"
)
test_repository.index.add(["feature.txt"])
commit = test_repository.index.commit("feature commit")
test_repository.git.checkout(default_branch)
result = git_branch(test_repository, "local", contains=commit.hexsha)
assert "feature-branch" in result
assert default_branch not in result
def test_git_branch_not_contains(test_repository):
# Get the default branch name (could be "main" or "master")
default_branch = test_repository.active_branch.name
# Create a new branch and commit to it
test_repository.git.checkout("-b", "another-feature-branch")
Path(test_repository.working_dir / Path("another_feature.txt")).write_text(
"another feature content"
)
test_repository.index.add(["another_feature.txt"])
commit = test_repository.index.commit("another feature commit")
test_repository.git.checkout(default_branch)
result = git_branch(test_repository, "local", not_contains=commit.hexsha)
assert "another-feature-branch" not in result
assert default_branch in result
def test_git_add_all_files(test_repository):
file_path = Path(test_repository.working_dir) / "all_file.txt"
file_path.write_text("adding all")
result = git_add(test_repository, ["."])
staged_files = [item.a_path for item in test_repository.index.diff("HEAD")]
assert "all_file.txt" in staged_files
assert result == "Files staged successfully"
def test_git_add_specific_files(test_repository):
file1 = Path(test_repository.working_dir) / "file1.txt"
file2 = Path(test_repository.working_dir) / "file2.txt"
file1.write_text("file 1 content")
file2.write_text("file 2 content")
result = git_add(test_repository, ["file1.txt"])
staged_files = [item.a_path for item in test_repository.index.diff("HEAD")]
assert "file1.txt" in staged_files
assert "file2.txt" not in staged_files
assert result == "Files staged successfully"
def test_git_add_rejects_an_empty_file_list(test_repository):
# #4763: `git add --` with no pathspec is a no-op that exits 0.
with pytest.raises(ValueError, match="No files provided to stage"):
git_add(test_repository, [])
def test_git_add_clean_tree_reports_nothing_staged(test_repository):
result = git_add(test_repository, ["."])
assert result.startswith("No changes were staged")
assert not test_repository.index.diff(test_repository.head.commit)
def test_git_add_already_staged_file_reports_nothing_staged(test_repository):
# A change staged earlier must not make a no-op call read as a success.
Path(test_repository.working_dir, "staged.txt").write_text("staged")
test_repository.index.add(["staged.txt"])
assert git_add(test_repository, ["staged.txt"]).startswith("No changes were staged")
assert git_add(test_repository, ["."]).startswith("No changes were staged")
def test_git_add_reports_a_staged_deletion(test_repository):
Path(test_repository.working_dir, "test.txt").unlink()
assert git_add(test_repository, ["test.txt"]) == "Files staged successfully"
assert "test.txt" not in [path for path, _stage in test_repository.index.entries]
def test_git_add_rejects_path_traversal(test_repository):
# Security invariant (CVE-2026-27735): a relative path escaping the
# repository must never be staged. Accept rejection from either the
# defense-in-depth validation (ValueError) or the underlying git CLI
# (GitCommandError) so the test asserts the property, not the layer.
outside = Path(test_repository.working_dir).parent / "outside.txt"
outside.write_text("secret")
with pytest.raises((ValueError, git.GitCommandError)):
git_add(test_repository, ["../outside.txt"])
staged = [path for path, _stage in test_repository.index.entries]
assert "../outside.txt" not in staged
assert "outside.txt" not in staged
def test_git_add_rejects_absolute_path_outside(test_repository):
# An absolute path outside the repository must never be staged.
outside = Path(test_repository.working_dir).parent / "abs_outside.txt"
outside.write_text("secret")
with pytest.raises((ValueError, git.GitCommandError)):
git_add(test_repository, [str(outside)])
staged = [path for path, _stage in test_repository.index.entries]
assert "abs_outside.txt" not in staged
def test_git_status(test_repository):
result = git_status(test_repository)
assert result is not None
assert "On branch" in result or "branch" in result.lower()
def test_git_diff_unstaged(test_repository):
file_path = Path(test_repository.working_dir) / "test.txt"
file_path.write_text("modified content")
result = git_diff_unstaged(test_repository)
assert "test.txt" in result
assert "modified content" in result
def test_git_diff_unstaged_empty(test_repository):
result = git_diff_unstaged(test_repository)
assert result == ""
def test_git_diff_staged(test_repository):
file_path = Path(test_repository.working_dir) / "staged_file.txt"
file_path.write_text("staged content")
test_repository.index.add(["staged_file.txt"])
result = git_diff_staged(test_repository)
assert "staged_file.txt" in result
assert "staged content" in result
def test_git_diff_staged_empty(test_repository):
result = git_diff_staged(test_repository)
assert result == ""
def test_git_diff(test_repository):
# Get the default branch name (could be "main" or "master")
default_branch = test_repository.active_branch.name
test_repository.git.checkout("-b", "feature-diff")
file_path = Path(test_repository.working_dir) / "test.txt"
file_path.write_text("feature changes")
test_repository.index.add(["test.txt"])
test_repository.index.commit("feature commit")
result = git_diff(test_repository, default_branch)
assert "test.txt" in result
assert "feature changes" in result
def test_git_commit(test_repository):
file_path = Path(test_repository.working_dir) / "commit_test.txt"
file_path.write_text("content to commit")
test_repository.index.add(["commit_test.txt"])
result = git_commit(test_repository, "test commit message")
assert "Changes committed successfully with hash" in result
latest_commit = test_repository.head.commit
assert latest_commit.message.strip() == "test commit message"
def test_git_commit_refuses_when_nothing_is_staged(test_repository):
# #4762: repo.index.commit() writes a tree unconditionally, so each of
# these used to come back as a hash for an empty commit.
head_before = test_repository.head.commit.hexsha
working_file = Path(test_repository.working_dir) / "test.txt"
# A clean tree.
with pytest.raises(ValueError, match="No changes staged for commit"):
git_commit(test_repository, "nothing to record")
# An untracked file, which git_add was never called for.
Path(test_repository.working_dir, "untracked.txt").write_text("never added")
with pytest.raises(ValueError, match="No changes staged for commit"):
git_commit(test_repository, "nothing to record")
# A tracked file edited but not staged.
working_file.write_text("edited but never staged")
with pytest.raises(ValueError, match="No changes staged for commit"):
git_commit(test_repository, "nothing to record")
assert test_repository.head.commit.hexsha == head_before
assert working_file.read_text() == "edited but never staged"
def test_git_commit_records_a_staged_deletion(test_repository):
# A deletion leaves no file behind, so it must not read as an empty index.
test_repository.git.rm("test.txt")
result = git_commit(test_repository, "remove test.txt")
assert "Changes committed successfully with hash" in result
assert "test.txt" not in test_repository.head.commit.tree
def test_git_commit_allows_the_first_commit_on_an_unborn_branch(tmp_path: Path):
repo = git.Repo.init(tmp_path / "unborn")
Path(repo.working_dir, "first.txt").write_text("first")
repo.index.add(["first.txt"])
result = git_commit(repo, "initial commit")
assert "Changes committed successfully with hash" in result
assert repo.head.commit.message.strip() == "initial commit"
repo.close()
def test_git_commit_refuses_an_empty_unborn_branch(tmp_path: Path):
repo = git.Repo.init(tmp_path / "unborn")
with pytest.raises(ValueError, match="No changes staged for commit"):
git_commit(repo, "initial commit")
assert not repo.head.is_valid()
repo.close()
def test_git_commit_allows_an_empty_merge_commit(repo):
# git permits an empty commit while a merge is in progress, so a merge
# whose result matches HEAD must still be committable. --no-commit sets
# MERGE_HEAD deterministically, without provoking a conflict.
starting_branch = repo.active_branch.name
repo.git.checkout("-b", "side")
Path(repo.working_dir, "side.txt").write_text("side only")
repo.git.add("side.txt")
side_sha = repo.index.commit("side change").hexsha
repo.git.checkout(starting_branch)
head_before = repo.head.commit.hexsha
repo.git.merge("side", "--no-commit", "--no-ff")
assert (Path(repo.git_dir) / "MERGE_HEAD").exists()
# Roll the index back to HEAD's own content: the pending merge commit
# records no change at all, which git allows.
repo.git.rm("side.txt", "--cached")
Path(repo.working_dir, "side.txt").unlink()
assert not repo.index.diff(repo.head.commit)
result = git_commit(repo, "merge side")
assert "Changes committed successfully with hash" in result
assert [p.hexsha for p in repo.head.commit.parents] == [head_before, side_sha]
assert not (Path(repo.git_dir) / "MERGE_HEAD").exists()
def test_git_commit_concludes_a_merge_with_both_parents(repo):
# #5012: a merge commit records HEAD and MERGE_HEAD as parents and clears
# the merge state, as `git commit` does.
starting_branch = repo.active_branch.name
repo.git.checkout("-b", "side")
Path(repo.working_dir, "side.txt").write_text("side only")
repo.git.add("side.txt")
side_sha = repo.index.commit("side change").hexsha
repo.git.checkout(starting_branch)
head_before = repo.head.commit.hexsha
repo.git.merge("side", "--no-commit", "--no-ff")
result = git_commit(repo, "merge side")
commit = repo.head.commit
assert commit.hexsha in result
assert [p.hexsha for p in commit.parents] == [head_before, side_sha]
assert "side.txt" in commit.tree
git_dir = Path(repo.git_dir)
for name in ("MERGE_HEAD", "MERGE_MSG", "MERGE_MODE"):
assert not (git_dir / name).exists()
assert repo.active_branch.name == starting_branch
def test_git_reset(test_repository):
file_path = Path(test_repository.working_dir) / "reset_test.txt"
file_path.write_text("content to reset")
test_repository.index.add(["reset_test.txt"])
staged_before = [item.a_path for item in test_repository.index.diff("HEAD")]
assert "reset_test.txt" in staged_before
result = git_reset(test_repository)
assert result == "All staged changes reset"
staged_after = [item.a_path for item in test_repository.index.diff("HEAD")]
assert "reset_test.txt" not in staged_after
def test_git_log(test_repository):
for i in range(3):
file_path = Path(test_repository.working_dir) / f"log_test_{i}.txt"
file_path.write_text(f"content {i}")
test_repository.index.add([f"log_test_{i}.txt"])
test_repository.index.commit(f"commit {i}")
result = git_log(test_repository, max_count=2)
assert isinstance(result, list)
assert len(result) == 2
assert "Commit:" in result[0]
assert "Author:" in result[0]
assert "Date:" in result[0]
assert "Message:" in result[0]
def test_git_log_default(test_repository):
result = git_log(test_repository)
assert isinstance(result, list)
assert len(result) >= 1
assert "initial commit" in result[0]
def test_git_create_branch(test_repository):
result = git_create_branch(test_repository, "new-feature-branch")
assert "Created branch 'new-feature-branch'" in result
branches = [ref.name for ref in test_repository.references]
assert "new-feature-branch" in branches
def test_git_create_branch_from_base(test_repository):
test_repository.git.checkout("-b", "base-branch")
file_path = Path(test_repository.working_dir) / "base.txt"
file_path.write_text("base content")
test_repository.index.add(["base.txt"])
test_repository.index.commit("base commit")
result = git_create_branch(test_repository, "derived-branch", "base-branch")
assert "Created branch 'derived-branch' from 'base-branch'" in result
def test_git_show(test_repository):
file_path = Path(test_repository.working_dir) / "show_test.txt"
file_path.write_text("show content")
test_repository.index.add(["show_test.txt"])
test_repository.index.commit("show test commit")
commit_sha = test_repository.head.commit.hexsha
result = git_show(test_repository, commit_sha)
assert "Commit:" in result
assert "Author:" in result
assert "show test commit" in result
assert "show_test.txt" in result
def test_git_show_initial_commit(test_repository):
initial_commit = list(test_repository.iter_commits())[-1]
result = git_show(test_repository, initial_commit.hexsha)
assert "Commit:" in result
assert "initial commit" in result
assert "test.txt" in result
# Tests for validate_repo_path (repository scoping security fix)
def test_validate_repo_path_no_restriction():
"""When no repository restriction is configured, any path should be allowed."""
validate_repo_path(Path("/any/path"), None) # Should not raise
def test_validate_repo_path_exact_match(tmp_path: Path):
"""When repo_path exactly matches allowed_repository, validation should pass."""
allowed = tmp_path / "repo"
allowed.mkdir()
validate_repo_path(allowed, allowed) # Should not raise
def test_validate_repo_path_subdirectory(tmp_path: Path):
"""When repo_path is a subdirectory of allowed_repository, validation should pass."""
allowed = tmp_path / "repo"
allowed.mkdir()
subdir = allowed / "subdir"
subdir.mkdir()
validate_repo_path(subdir, allowed) # Should not raise
def test_validate_repo_path_outside_allowed(tmp_path: Path):
"""When repo_path is outside allowed_repository, validation should raise ValueError."""
allowed = tmp_path / "allowed_repo"
allowed.mkdir()
outside = tmp_path / "other_repo"
outside.mkdir()
with pytest.raises(ValueError) as exc_info:
validate_repo_path(outside, allowed)
assert "outside the allowed repository" in str(exc_info.value)
def test_validate_repo_path_traversal_attempt(tmp_path: Path):
"""Path traversal attempts (../) should be caught and rejected."""
allowed = tmp_path / "allowed_repo"
allowed.mkdir()
# Attempt to escape via ../
traversal_path = allowed / ".." / "other_repo"
with pytest.raises(ValueError) as exc_info:
validate_repo_path(traversal_path, allowed)
assert "outside the allowed repository" in str(exc_info.value)
def test_validate_repo_path_symlink_escape(tmp_path: Path):
"""Symlinks pointing outside allowed_repository should be rejected."""
allowed = tmp_path / "allowed_repo"
allowed.mkdir()
outside = tmp_path / "outside"
outside.mkdir()
# Create a symlink inside allowed that points outside
symlink = allowed / "escape_link"
symlink.symlink_to(outside)
with pytest.raises(ValueError) as exc_info:
validate_repo_path(symlink, allowed)
assert "outside the allowed repository" in str(exc_info.value)
# Tests for argument injection protection
def test_git_diff_rejects_flag_injection(test_repository):
"""git_diff should reject flags that could be used for argument injection."""
with pytest.raises(BadName):
git_diff(test_repository, "--output=/tmp/evil")
with pytest.raises(BadName):
git_diff(test_repository, "--help")
with pytest.raises(BadName):
git_diff(test_repository, "-p")
def test_git_checkout_rejects_flag_injection(test_repository):
"""git_checkout should reject flags that could be used for argument injection."""
with pytest.raises(BadName):
git_checkout(test_repository, "--help")
with pytest.raises(BadName):
git_checkout(test_repository, "--orphan=evil")
with pytest.raises(BadName):
git_checkout(test_repository, "-f")
def test_git_diff_allows_valid_refs(test_repository):
"""git_diff should work normally with valid git refs."""
# Get the default branch name
default_branch = test_repository.active_branch.name
# Create a branch with a commit for diffing
test_repository.git.checkout("-b", "valid-diff-branch")
file_path = Path(test_repository.working_dir) / "test.txt"
file_path.write_text("valid diff content")
test_repository.index.add(["test.txt"])
test_repository.index.commit("valid diff commit")
# Test with branch name
result = git_diff(test_repository, default_branch)
assert "test.txt" in result
# Test with HEAD~1
result = git_diff(test_repository, "HEAD~1")
assert "test.txt" in result
# Test with commit hash
commit_sha = test_repository.head.commit.hexsha
result = git_diff(test_repository, commit_sha)
assert result is not None
def test_git_checkout_allows_valid_branches(test_repository):
"""git_checkout should work normally with valid branch names."""
# Get the default branch name
default_branch = test_repository.active_branch.name
# Create a branch to checkout
test_repository.git.branch("valid-checkout-branch")
result = git_checkout(test_repository, "valid-checkout-branch")
assert "Switched to branch 'valid-checkout-branch'" in result
assert test_repository.active_branch.name == "valid-checkout-branch"
# Checkout back to default branch
result = git_checkout(test_repository, default_branch)
assert "Switched to branch" in result
assert test_repository.active_branch.name == default_branch
def test_git_diff_rejects_malicious_refs(test_repository):
"""git_diff should reject refs starting with '-' even if they exist.
This tests defense in depth against an attacker who creates malicious
refs via filesystem manipulation (e.g. using mcp-filesystem to write
to .git/refs/heads/--output=...).
"""
import os
# Manually create a malicious ref by writing directly to .git/refs
sha = test_repository.head.commit.hexsha
refs_dir = Path(test_repository.git_dir) / "refs" / "heads"
malicious_ref_path = refs_dir / "--output=evil.txt"
malicious_ref_path.write_text(sha)
# Even though the ref exists, it should be rejected
with pytest.raises(BadName):
git_diff(test_repository, "--output=evil.txt")
# Verify no file was created (the attack was blocked)
assert not os.path.exists("evil.txt")
# Cleanup
malicious_ref_path.unlink()
def test_git_checkout_rejects_malicious_refs(test_repository):
"""git_checkout should reject refs starting with '-' even if they exist."""
# Manually create a malicious ref
sha = test_repository.head.commit.hexsha
refs_dir = Path(test_repository.git_dir) / "refs" / "heads"
malicious_ref_path = refs_dir / "--orphan=evil"
malicious_ref_path.write_text(sha)
# Even though the ref exists, it should be rejected
with pytest.raises(BadName):
git_checkout(test_repository, "--orphan=evil")
# Cleanup
malicious_ref_path.unlink()
# Tests for argument injection protection in git_show, git_create_branch,
# git_log, and git_branch — matching the existing guards on git_diff and
# git_checkout.
def test_git_show_rejects_flag_injection(test_repository):
"""git_show should reject revisions starting with '-'."""
with pytest.raises(BadName):
git_show(test_repository, "--output=/tmp/evil")
with pytest.raises(BadName):
git_show(test_repository, "-p")
def test_git_show_rejects_malicious_refs(test_repository):
"""git_show should reject refs starting with '-' even if they exist."""
sha = test_repository.head.commit.hexsha
refs_dir = Path(test_repository.git_dir) / "refs" / "heads"
malicious_ref_path = refs_dir / "--format=evil"
malicious_ref_path.write_text(sha)
with pytest.raises(BadName):
git_show(test_repository, "--format=evil")
malicious_ref_path.unlink()
def test_git_create_branch_rejects_flag_injection(test_repository):
"""git_create_branch should reject branch names starting with '-'."""
with pytest.raises(BadName):
git_create_branch(test_repository, "--track=evil")
with pytest.raises(BadName):
git_create_branch(test_repository, "-f")
def test_git_create_branch_rejects_base_branch_flag_injection(test_repository):
"""git_create_branch should reject base branch names starting with '-'."""
with pytest.raises(BadName):
git_create_branch(test_repository, "new-branch", "--track=evil")
def test_git_log_rejects_timestamp_flag_injection(test_repository):
"""git_log should reject timestamps starting with '-'."""
with pytest.raises(ValueError):
git_log(test_repository, start_timestamp="--exec=evil")
with pytest.raises(ValueError):
git_log(test_repository, end_timestamp="--exec=evil")
def test_git_branch_rejects_contains_flag_injection(test_repository):
"""git_branch should reject contains/not_contains values starting with '-'."""
with pytest.raises(BadName):
git_branch(test_repository, "local", contains="--exec=evil")
with pytest.raises(BadName):
git_branch(test_repository, "local", not_contains="--exec=evil")
def test_git_log_formatting_no_repr(test_repository):
"""Test that git_log does not use !r formatting (no Python object repr or quotes)."""
file_path = Path(test_repository.working_dir) / "multiline.txt"
file_path.write_text("multiline test")
test_repository.index.add(["multiline.txt"])
test_repository.index.commit(
"Subject line\n\nDetailed body line 1\nDetailed body line 2"
)
result = git_log(test_repository, max_count=1)
entry = result[0]
# Verify no python repr quotes around commit hash or message
assert "Commit: '" not in entry
assert 'Commit: "' not in entry
assert "<git.Actor" not in entry
assert "Message: '" not in entry
assert 'Message: "' not in entry
# Verify full message with body is included
assert "Subject line\n\nDetailed body line 1\nDetailed body line 2" in entry
def test_git_log_filtered_unfiltered_parity(test_repository):
"""Test that filtered and unfiltered git_log produce identical schema and preserve commit body."""
file_path = Path(test_repository.working_dir) / "parity_test.txt"
file_path.write_text("parity test")
test_repository.index.add(["parity_test.txt"])
test_repository.index.commit(
"Parity subject\n\nParity body line 1\nParity body line 2"
)
unfiltered = git_log(test_repository, max_count=1)
filtered_since = git_log(test_repository, max_count=1, start_timestamp="yesterday")
filtered_until = git_log(test_repository, max_count=1, end_timestamp="2099-01-01")
# Output schemas and contents must be identical
assert unfiltered == filtered_since
assert unfiltered == filtered_until
# Multi-line commit message preserved in filtered results
assert (
"Parity subject\n\nParity body line 1\nParity body line 2" in filtered_since[0]
)
def test_git_log_date_filtering(test_repository):
"""Test date filtering logic in git_log."""
# Future start_timestamp should return no commits
future_result = git_log(test_repository, start_timestamp="2099-01-01")
assert future_result == []
# Past end_timestamp should return no commits
past_result = git_log(test_repository, end_timestamp="2000-01-01")
assert past_result == []
# Valid range with max_count
valid_result = git_log(test_repository, max_count=1, start_timestamp="2000-01-01")
assert len(valid_result) == 1
async def test_serve_run_does_not_raise_exceptions(tmp_path: Path):
"""Verify that serve() runs server.run without raise_exceptions=True."""
repo_path = tmp_path / "serve_test_repo"
git.Repo.init(repo_path).close()
with mock.patch("mcp_server_git.server.stdio_server") as mock_stdio:
mock_read = mock.AsyncMock()
mock_write = mock.AsyncMock()
mock_stdio.return_value.__aenter__.return_value = (mock_read, mock_write)
mock_stdio.return_value.__aexit__.return_value = None
with mock.patch(
"mcp_server_git.server.Server.run", new_callable=mock.AsyncMock
) as mock_run:
await serve(repo_path)
mock_run.assert_awaited_once()
_, kwargs = mock_run.call_args
assert kwargs.get("raise_exceptions") is not True