|
5 | 5 | from mcp_server_git.server import ( |
6 | 6 | git_checkout, |
7 | 7 | git_branch, |
| 8 | + git_current_branch, |
| 9 | + git_default_branch, |
| 10 | + git_remote, |
8 | 11 | git_add, |
9 | 12 | git_status, |
10 | 13 | git_diff_unstaged, |
@@ -482,3 +485,151 @@ def test_git_branch_rejects_contains_flag_injection(test_repository): |
482 | 485 |
|
483 | 486 | with pytest.raises(BadName): |
484 | 487 | git_branch(test_repository, "local", not_contains="--exec=evil") |
| 488 | + |
| 489 | + |
| 490 | +def test_git_default_branch_rejects_remote_flag_injection(test_repository): |
| 491 | + """git_default_branch should reject remote names starting with '-'.""" |
| 492 | + with pytest.raises(ValueError): |
| 493 | + git_default_branch(test_repository, remote="--upload-pack=evil") |
| 494 | + |
| 495 | + |
| 496 | +# Tests for git_current_branch |
| 497 | + |
| 498 | +def test_git_current_branch(test_repository): |
| 499 | + result = git_current_branch(test_repository) |
| 500 | + assert result == test_repository.active_branch.name |
| 501 | + |
| 502 | +def test_git_current_branch_detached_head(test_repository): |
| 503 | + commit_sha = test_repository.head.commit.hexsha |
| 504 | + test_repository.git.checkout(commit_sha) |
| 505 | + result = git_current_branch(test_repository) |
| 506 | + assert "detached" in result.lower() |
| 507 | + assert commit_sha[:7] in result |
| 508 | + |
| 509 | + |
| 510 | +# Tests for git_default_branch |
| 511 | + |
| 512 | +def test_git_default_branch_fallback_local(test_repository): |
| 513 | + """Repo with no remote; falls back to detecting the local default branch name.""" |
| 514 | + default_branch = test_repository.active_branch.name |
| 515 | + result = git_default_branch(test_repository) |
| 516 | + assert result == f"origin/{default_branch}" |
| 517 | + |
| 518 | +def test_git_default_branch_with_remote(tmp_path): |
| 519 | + """Create a bare remote repo, add it as origin, verify ls-remote detection works.""" |
| 520 | + # Create a bare repo to act as the remote |
| 521 | + bare_path = tmp_path / "bare_remote.git" |
| 522 | + bare_repo = git.Repo.init(bare_path, bare=True) |
| 523 | + |
| 524 | + # Create a local repo and push to the bare remote |
| 525 | + local_path = tmp_path / "local_repo" |
| 526 | + local_repo = git.Repo.init(local_path) |
| 527 | + |
| 528 | + Path(local_path / "test.txt").write_text("test") |
| 529 | + local_repo.index.add(["test.txt"]) |
| 530 | + local_repo.index.commit("initial commit") |
| 531 | + |
| 532 | + local_repo.create_remote("origin", str(bare_path)) |
| 533 | + local_repo.git.push("--set-upstream", "origin", local_repo.active_branch.name) |
| 534 | + |
| 535 | + result = git_default_branch(local_repo) |
| 536 | + assert result == f"origin/{local_repo.active_branch.name}" |
| 537 | + |
| 538 | + shutil.rmtree(local_path) |
| 539 | + shutil.rmtree(bare_path) |
| 540 | + |
| 541 | +def test_git_default_branch_custom_remote(tmp_path): |
| 542 | + """Add a remote with a non-'origin' name, verify the remote parameter selects it.""" |
| 543 | + bare_path = tmp_path / "custom_remote.git" |
| 544 | + bare_repo = git.Repo.init(bare_path, bare=True) |
| 545 | + |
| 546 | + local_path = tmp_path / "local_repo" |
| 547 | + local_repo = git.Repo.init(local_path) |
| 548 | + |
| 549 | + Path(local_path / "test.txt").write_text("test") |
| 550 | + local_repo.index.add(["test.txt"]) |
| 551 | + local_repo.index.commit("initial commit") |
| 552 | + |
| 553 | + local_repo.create_remote("upstream", str(bare_path)) |
| 554 | + local_repo.git.push("--set-upstream", "upstream", local_repo.active_branch.name) |
| 555 | + |
| 556 | + result = git_default_branch(local_repo, remote="upstream") |
| 557 | + assert result == f"upstream/{local_repo.active_branch.name}" |
| 558 | + |
| 559 | + shutil.rmtree(local_path) |
| 560 | + shutil.rmtree(bare_path) |
| 561 | + |
| 562 | +def test_git_default_branch_undetectable(tmp_path): |
| 563 | + """Repo with no remotes and no main/master branch; should raise ValueError.""" |
| 564 | + repo_path = tmp_path / "no_default_repo" |
| 565 | + repo = git.Repo.init(repo_path) |
| 566 | + |
| 567 | + # Create a commit on a non-standard branch name |
| 568 | + repo.git.checkout("-b", "develop") |
| 569 | + Path(repo_path / "test.txt").write_text("test") |
| 570 | + repo.index.add(["test.txt"]) |
| 571 | + repo.index.commit("initial commit") |
| 572 | + |
| 573 | + with pytest.raises(ValueError, match="Could not determine the default branch"): |
| 574 | + git_default_branch(repo) |
| 575 | + |
| 576 | + shutil.rmtree(repo_path) |
| 577 | + |
| 578 | +def test_git_default_branch_revparse_fallback(tmp_path): |
| 579 | + """When ls-remote fails but local ref cache exists, rev-parse fallback should work.""" |
| 580 | + # Create a bare repo to act as the remote |
| 581 | + bare_path = tmp_path / "bare_remote.git" |
| 582 | + git.Repo.init(bare_path, bare=True) |
| 583 | + |
| 584 | + # Create a local repo and push to the bare remote |
| 585 | + local_path = tmp_path / "local_repo" |
| 586 | + local_repo = git.Repo.init(local_path) |
| 587 | + |
| 588 | + Path(local_path / "test.txt").write_text("test") |
| 589 | + local_repo.index.add(["test.txt"]) |
| 590 | + local_repo.index.commit("initial commit") |
| 591 | + |
| 592 | + active_branch = local_repo.active_branch.name |
| 593 | + local_repo.create_remote("origin", str(bare_path)) |
| 594 | + local_repo.git.push("--set-upstream", "origin", active_branch) |
| 595 | + |
| 596 | + # Populate local ref cache for origin/HEAD |
| 597 | + local_repo.git.remote("set-head", "origin", "--auto") |
| 598 | + |
| 599 | + # Replace remote URL with an invalid path so ls-remote will fail |
| 600 | + local_repo.git.remote("set-url", "origin", "/nonexistent/path") |
| 601 | + |
| 602 | + result = git_default_branch(local_repo) |
| 603 | + assert result == f"origin/{active_branch}" |
| 604 | + |
| 605 | + shutil.rmtree(local_path) |
| 606 | + shutil.rmtree(bare_path) |
| 607 | + |
| 608 | + |
| 609 | +# Tests for git_remote |
| 610 | + |
| 611 | +def test_git_remote_no_remotes(test_repository): |
| 612 | + """Repo with no remotes; verify empty output.""" |
| 613 | + result = git_remote(test_repository) |
| 614 | + assert result == "" |
| 615 | + |
| 616 | +def test_git_remote_with_remote(tmp_path): |
| 617 | + """Repo with a remote configured; verify remote name and URL appear in output.""" |
| 618 | + bare_path = tmp_path / "bare_remote.git" |
| 619 | + git.Repo.init(bare_path, bare=True) |
| 620 | + |
| 621 | + local_path = tmp_path / "local_repo" |
| 622 | + local_repo = git.Repo.init(local_path) |
| 623 | + |
| 624 | + Path(local_path / "test.txt").write_text("test") |
| 625 | + local_repo.index.add(["test.txt"]) |
| 626 | + local_repo.index.commit("initial commit") |
| 627 | + |
| 628 | + local_repo.create_remote("origin", str(bare_path)) |
| 629 | + |
| 630 | + result = git_remote(local_repo) |
| 631 | + assert "origin" in result |
| 632 | + assert str(bare_path) in result |
| 633 | + |
| 634 | + shutil.rmtree(local_path) |
| 635 | + shutil.rmtree(bare_path) |
0 commit comments