get_commit_stats
Retrieve commit statistics for a git repository within a date range, optionally filtered by author. Analyze development activity and team contribution patterns.
Instructions
Get commit statistics for a repository
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| repo_path | Yes | Path to git repository | |
| since | Yes | Start date (YYYY-MM-DD) | |
| until | No | End date (YYYY-MM-DD), optional | |
| author | No | Filter by author email/name, optional |
Implementation Reference
- src/handlers.ts:3-40 (handler)The main handler function for the get_commit_stats tool. It runs a git log command with --shortstat, parses the output to count commits, additions, deletions, and files changed, and returns an object with those stats plus netChange.
export function handleGetCommitStats(args: any) { const { repo_path, since, until, author } = args; validateRepoPath(repo_path); validateDate(since, "since"); if (until) validateDate(until, "until"); let cmd = `git log --since="${since}"`; if (until) cmd += ` --until="${until} 23:59:59"`; if (author) cmd += ` --author="${author}"`; cmd += ` --pretty=format:"%H" --shortstat`; const output = runGitCommand(repo_path, cmd); const lines = output.trim().split("\n"); let commits = 0, additions = 0, deletions = 0, filesChanged = 0; for (const line of lines) { if (line.match(/^[0-9a-f]{40}$/)) { commits++; } else if (line.includes("changed")) { const addMatch = line.match(/(\d+) insertion/); const delMatch = line.match(/(\d+) deletion/); const fileMatch = line.match(/(\d+) file/); if (addMatch) additions += parseInt(addMatch[1]); if (delMatch) deletions += parseInt(delMatch[1]); if (fileMatch) filesChanged += parseInt(fileMatch[1]); } } return { commits, additions, deletions, filesChanged, netChange: additions - deletions, }; } - src/git-metrics.ts:96-108 (schema)Tool registration with its input schema definition. Defines the tool name 'get_commit_stats', description, and input schema with properties: repo_path (required), since (required), until (optional), author (optional).
name: "get_commit_stats", description: "Get commit statistics for a repository", inputSchema: { type: "object", properties: { repo_path: { type: "string", description: "Path to git repository" }, since: { type: "string", description: "Start date (YYYY-MM-DD)" }, until: { type: "string", description: "End date (YYYY-MM-DD), optional" }, author: { type: "string", description: "Filter by author email/name, optional" }, }, required: ["repo_path", "since"], }, }, - src/git-metrics.ts:255-256 (registration)The CallToolRequestSchema handler that dispatches the 'get_commit_stats' tool name to the handleGetCommitStats function.
} else if (request.params.name === "get_commit_stats") { result = handlers.handleGetCommitStats(args); - src/git-metrics.ts:40-56 (helper)The runGitCommand helper function that executes git commands. Used by the handler to run git log.
export function runGitCommand(repoPath: string, command: string): string { const fullPath = resolve(repoPath); if (!existsSync(fullPath)) { throw new Error(`Repository path does not exist: ${fullPath}`); } try { return execSync(command, { cwd: fullPath, encoding: "utf-8", timeout: GIT_TIMEOUT, maxBuffer: MAX_BUFFER }); } catch (error: any) { log('ERROR', 'Git command failed', { command, error: error.message }); throw new Error(`Git command failed: ${error.message}`); } } - src/git-metrics.ts:62-66 (helper)The validateDate helper function used by the handler to ensure dates are in YYYY-MM-DD format.
export function validateDate(date: string, fieldName: string): void { if (!/^\d{4}-\d{2}-\d{2}$/.test(date)) { throw new Error(`Invalid ${fieldName} format. Use YYYY-MM-DD (e.g., 2025-11-21)`); } }