Skip to content

Commit 359f7a2

Browse files
committed
fix: handle empty commits and invalid dates in velocity_trends
- Add check for empty output before parsing - Add date validation to prevent Invalid time value errors - Fix parseCommitData to handle date|hash format correctly - Handle missing date fields gracefully
1 parent a542440 commit 359f7a2

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

src/git-metrics.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,15 @@ function parseCommitData(output: string) {
3737
for (const line of lines) {
3838
if (line.includes("|")) {
3939
if (current) commits.push(current);
40-
const [hash, author, email, date, message] = line.split("|");
41-
current = { hash, author, email, date, message, files: [] };
40+
const parts = line.split("|");
41+
const date = parts[0];
42+
const hash = parts[1] || "";
43+
current = { hash, date, files: [] };
4244
} else if (line.match(/^\d+\s+\d+/) && current) {
43-
const [add, del, file] = line.split(/\s+/);
45+
const parts = line.split(/\s+/);
46+
const add = parts[0];
47+
const del = parts[1];
48+
const file = parts.slice(2).join(" ");
4449
current.files.push({ file, additions: parseInt(add) || 0, deletions: parseInt(del) || 0 });
4550
}
4651
}
@@ -427,12 +432,25 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
427432
const cmd = `git log --since="${since}" --pretty=format:"%ad|%H" --date=short --numstat`;
428433

429434
const output = runGitCommand(repo_path, cmd);
435+
if (!output.trim()) {
436+
return {
437+
content: [{
438+
type: "text",
439+
text: JSON.stringify({ interval, trends: [], message: "No commits found in this period" }, null, 2),
440+
}],
441+
};
442+
}
443+
430444
const commits = parseCommitData(output);
431445

432446
const periods: Record<string, { commits: number; additions: number; deletions: number }> = {};
433447

434448
for (const commit of commits) {
449+
if (!commit.date) continue;
450+
435451
const date = new Date(commit.date);
452+
if (isNaN(date.getTime())) continue;
453+
436454
let periodKey: string;
437455

438456
if (interval === "week") {

0 commit comments

Comments
 (0)