get_diff_stats
Retrieve diff statistics—files changed, lines added/removed—for staged changes, a specific file, or between two commits.
Instructions
Get diff statistics (files changed, lines added/removed)
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| staged | No | Show stats for staged changes | |
| file | No | Specific file stats | |
| commit1 | No | First commit for comparison | |
| commit2 | No | Second commit for comparison | |
| cwd | No | Working directory |
Implementation Reference
- src/index.ts:446-447 (registration)Routes the 'get_diff_stats' tool call to GitService.getDiffStats() in the main server handler switch statement.
case 'get_diff_stats': return await this.gitService.getDiffStats(args); - src/toolDefinitions.ts:791-804 (schema)Defines the input schema for the 'get_diff_stats' tool, including staged, file, commit1, commit2, and cwd parameters.
{ name: 'get_diff_stats', description: 'Get diff statistics (files changed, lines added/removed)', inputSchema: { type: 'object', properties: { staged: { type: 'boolean', description: 'Show stats for staged changes' }, file: { type: 'string', description: 'Specific file stats' }, commit1: { type: 'string', description: 'First commit for comparison' }, commit2: { type: 'string', description: 'Second commit for comparison' }, cwd: { type: 'string', description: 'Working directory' } } } }, - src/services/GitService.ts:1214-1312 (handler)Main implementation of getDiffStats: builds git diff --stat command, parses the output (files changed, insertions, deletions), formats header based on context (staged/commit comparison), and returns result with metadata.
async getDiffStats(options: { staged?: boolean; file?: string; commit1?: string; commit2?: string; cwd?: string; } = {}): Promise<ToolResult> { try { const { staged = false, file, commit1, commit2, cwd } = options; const diffArgs = ['diff', '--stat']; if (staged) { diffArgs.push('--staged'); } if (commit1 && commit2) { diffArgs.push(`${commit1}..${commit2}`); } else if (commit1) { diffArgs.push(commit1); } if (file) { diffArgs.push('--', file); } const result = await this.gitCommand(diffArgs, cwd); if (result.isError) { return result; } // Parse the stats const statsText = result.content[0]?.text || ''; const lines = statsText.split('\n').filter(line => line.trim()); if (lines.length === 0) { const header = staged ? 'Staged Changes Statistics:\n\n' : (commit1 && commit2) ? `Commit Comparison Statistics (${commit1}..${commit2}):\n\n` : 'Diff Statistics:\n\n'; return { content: [{ type: 'text', text: header + 'No changes found', _meta: { filesChanged: 0, insertions: 0, deletions: 0 } }] }; } // Extract summary line (usually the last line) const summaryLine = lines[lines.length - 1]; const filesChanged = (summaryLine.match(/(\d+) files? changed/) || [])[1] || '0'; const insertions = (summaryLine.match(/(\d+) insertions?\(\+\)/) || [])[1] || '0'; const deletions = (summaryLine.match(/(\d+) deletions?\(\-\)/) || [])[1] || '0'; // Add appropriate header let header = ''; if (staged) { header = 'Staged Changes Statistics:\n\n'; } else if (commit1 && commit2) { header = `Commit Comparison Statistics (${commit1}..${commit2}):\n\n`; } else { header = 'Diff Statistics:\n\n'; } // Format the output with additional analysis let output = header; output += `Files changed: ${filesChanged}\n`; output += `Lines added: ${insertions}\n`; output += `Lines removed: ${deletions}\n\n`; output += `Detailed Breakdown:\n${statsText}`; return { content: [{ type: 'text', text: output, _meta: { filesChanged: parseInt(filesChanged), insertions: parseInt(insertions), deletions: parseInt(deletions), summary: summaryLine } }] }; } catch (error) { return { isError: true, content: [{ type: 'text', text: `Diff stats failed: ${error instanceof Error ? error.message : String(error)}` }] }; } }