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