Merge pull request #249 from monkeydaichan/main

feat(git): add git_diff tool for branch comparison
This commit is contained in:
Justin Spahr-Summers
2024-12-09 13:43:59 +00:00
committed by GitHub
2 changed files with 32 additions and 5 deletions

View File

@@ -26,34 +26,41 @@ Please note that mcp-server-git is currently in early development. The functiona
- `repo_path` (string): Path to Git repository - `repo_path` (string): Path to Git repository
- Returns: Diff output of staged changes - Returns: Diff output of staged changes
4. `git_commit` 4. `git_diff`
- Shows differences between branches or commits
- Inputs:
- `repo_path` (string): Path to Git repository
- `target` (string): Target branch or commit to compare with
- Returns: Diff output comparing current state with target
5. `git_commit`
- Records changes to the repository - Records changes to the repository
- Inputs: - Inputs:
- `repo_path` (string): Path to Git repository - `repo_path` (string): Path to Git repository
- `message` (string): Commit message - `message` (string): Commit message
- Returns: Confirmation with new commit hash - Returns: Confirmation with new commit hash
5. `git_add` 6. `git_add`
- Adds file contents to the staging area - Adds file contents to the staging area
- Inputs: - Inputs:
- `repo_path` (string): Path to Git repository - `repo_path` (string): Path to Git repository
- `files` (string[]): Array of file paths to stage - `files` (string[]): Array of file paths to stage
- Returns: Confirmation of staged files - Returns: Confirmation of staged files
6. `git_reset` 7. `git_reset`
- Unstages all staged changes - Unstages all staged changes
- Input: - Input:
- `repo_path` (string): Path to Git repository - `repo_path` (string): Path to Git repository
- Returns: Confirmation of reset operation - Returns: Confirmation of reset operation
7. `git_log` 8. `git_log`
- Shows the commit logs - Shows the commit logs
- Inputs: - Inputs:
- `repo_path` (string): Path to Git repository - `repo_path` (string): Path to Git repository
- `max_count` (number, optional): Maximum number of commits to show (default: 10) - `max_count` (number, optional): Maximum number of commits to show (default: 10)
- Returns: Array of commit entries with hash, author, date, and message - Returns: Array of commit entries with hash, author, date, and message
8. `git_create_branch` 9. `git_create_branch`
- Creates a new branch - Creates a new branch
- Inputs: - Inputs:
- `repo_path` (string): Path to Git repository - `repo_path` (string): Path to Git repository

View File

@@ -24,6 +24,10 @@ class GitDiffUnstaged(BaseModel):
class GitDiffStaged(BaseModel): class GitDiffStaged(BaseModel):
repo_path: str repo_path: str
class GitDiff(BaseModel):
repo_path: str
target: str
class GitCommit(BaseModel): class GitCommit(BaseModel):
repo_path: str repo_path: str
message: str message: str
@@ -48,6 +52,7 @@ class GitTools(str, Enum):
STATUS = "git_status" STATUS = "git_status"
DIFF_UNSTAGED = "git_diff_unstaged" DIFF_UNSTAGED = "git_diff_unstaged"
DIFF_STAGED = "git_diff_staged" DIFF_STAGED = "git_diff_staged"
DIFF = "git_diff"
COMMIT = "git_commit" COMMIT = "git_commit"
ADD = "git_add" ADD = "git_add"
RESET = "git_reset" RESET = "git_reset"
@@ -63,6 +68,9 @@ def git_diff_unstaged(repo: git.Repo) -> str:
def git_diff_staged(repo: git.Repo) -> str: def git_diff_staged(repo: git.Repo) -> str:
return repo.git.diff("--cached") return repo.git.diff("--cached")
def git_diff(repo: git.Repo, target: str) -> str:
return repo.git.diff(target)
def git_commit(repo: git.Repo, message: str) -> str: def git_commit(repo: git.Repo, message: str) -> str:
commit = repo.index.commit(message) commit = repo.index.commit(message)
return f"Changes committed successfully with hash {commit.hexsha}" return f"Changes committed successfully with hash {commit.hexsha}"
@@ -127,6 +135,11 @@ async def serve(repository: Path | None) -> None:
description="Shows changes that are staged for commit", description="Shows changes that are staged for commit",
inputSchema=GitDiffStaged.schema(), inputSchema=GitDiffStaged.schema(),
), ),
Tool(
name=GitTools.DIFF,
description="Shows differences between branches or commits",
inputSchema=GitDiff.schema(),
),
Tool( Tool(
name=GitTools.COMMIT, name=GitTools.COMMIT,
description="Records changes to the repository", description="Records changes to the repository",
@@ -210,6 +223,13 @@ async def serve(repository: Path | None) -> None:
text=f"Staged changes:\n{diff}" text=f"Staged changes:\n{diff}"
)] )]
case GitTools.DIFF:
diff = git_diff(repo, arguments["target"])
return [TextContent(
type="text",
text=f"Diff with {arguments['target']}:\n{diff}"
)]
case GitTools.COMMIT: case GitTools.COMMIT:
result = git_commit(repo, arguments["message"]) result = git_commit(repo, arguments["message"])
return [TextContent( return [TextContent(