Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 02e89df

Browse files
committed
feat(git): add getFileCommitHistory method to retrieve commit history for specific files
- Implemented a new method in GitService to fetch the last N commit messages for a specified file, enhancing the ability to analyze commit patterns. - Added corresponding tool in GitToolsService to facilitate retrieval of commit history with error handling for improved user experience.
1 parent 16c0ede commit 02e89df

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/services/git-tools.service.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,34 @@ export class GitToolsService {
274274
}
275275
},
276276
}),
277+
278+
getFileCommitHistory: tool({
279+
description: 'Get the last N commit messages that affected a specific file to understand commit patterns and context.',
280+
inputSchema: z.object({
281+
filePath: z.string().describe('Path to the file to get commit history for (relative to git root)'),
282+
count: z.number().optional().describe('Number of recent commits to retrieve (default: 10)'),
283+
}),
284+
execute: async ({ filePath, count = 10 }) => {
285+
onToolCall(`Getting commit history for file: ${filePath}`);
286+
287+
try {
288+
const commits = await this.gitService.getFileCommitHistory(filePath, count);
289+
290+
return {
291+
success: true,
292+
filePath,
293+
commits,
294+
count: commits.length,
295+
};
296+
} catch (error) {
297+
return {
298+
success: false,
299+
error: `Error getting commit history for file: ${error instanceof Error ? error.message : 'Unknown error'}`,
300+
filePath,
301+
};
302+
}
303+
},
304+
}),
277305
} as const;
278306
}
279307
}

src/services/git.service.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,20 @@ export class GitService {
136136
}
137137
}
138138

139+
async getFileCommitHistory(filePath: string, count: number): Promise<Array<{ hash: string; message: string; author: string; date: string }>> {
140+
try {
141+
const log = await this.git.log({ maxCount: count, file: filePath });
142+
return log.all.map((commit) => ({
143+
hash: commit.hash.substring(0, 7),
144+
message: commit.message,
145+
author: commit.author_name || 'Unknown',
146+
date: commit.date,
147+
}));
148+
} catch {
149+
throw new KnownError(`Failed to get commit history for file: ${filePath}`);
150+
}
151+
}
152+
139153
async getStatus(): Promise<string> {
140154
try {
141155
const status = await this.git.status();

0 commit comments

Comments
 (0)