Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/server/api/git-diffs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface FileDiff {
status: "added" | "deleted" | "modified";
}

function parseGitDiff(stdout: string): FileDiff[] {
export function parseGitDiff(stdout: string): FileDiff[] {
const files: FileDiff[] = [];
// Split on diff --git headers
const blocks = stdout.split(/\ndiff --git /).filter(Boolean);
Expand All @@ -29,12 +29,12 @@ function parseGitDiff(stdout: string): FileDiff[] {
const patchContent = block;

// Count additions/deletions
const additions = (patchContent.match(/^\+/gm) || []).length;
const deletions = (patchContent.match(/^-/gm) || []).length;
const additions = (patchContent.match(/^\+(?!\+\+)/gm) || []).length;
const deletions = (patchContent.match(/^-(?!--)/gm) || []).length;

// Determine status
const isNew = /^new file mode/.test(patchContent);
const isDeleted = /^deleted file mode/.test(patchContent);
const isNew = /^new file mode/m.test(patchContent);
const isDeleted = /^deleted file mode/m.test(patchContent);
const status = isNew ? "added" : isDeleted ? "deleted" : "modified";

files.push({
Expand Down
60 changes: 60 additions & 0 deletions tests/git-diffs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { describe, expect, it } from "vitest";
import { parseGitDiff } from "../src/server/api/git-diffs.js";

describe("parseGitDiff", () => {
it("counts changed lines without counting diff file headers", () => {
const diff = [
"diff --git a/src/app.ts b/src/app.ts",
"index 1111111..2222222 100644",
"--- a/src/app.ts",
"+++ b/src/app.ts",
"@@ -1,3 +1,4 @@",
" const keep = true;",
"-const oldName = 'reasonix';",
"+const newName = 'carboncode';",
"+const enabled = true;",
" export { keep };",
"diff --git a/src/new.ts b/src/new.ts",
"new file mode 100644",
"index 0000000..3333333",
"--- /dev/null",
"+++ b/src/new.ts",
"@@ -0,0 +1,2 @@",
"+export const one = 1;",
"+export const two = 2;",
"diff --git a/src/old.ts b/src/old.ts",
"deleted file mode 100644",
"index 4444444..0000000",
"--- a/src/old.ts",
"+++ /dev/null",
"@@ -1,2 +0,0 @@",
"-export const gone = true;",
"-export const alsoGone = true;",
"",
].join("\n");

expect(parseGitDiff(diff)).toEqual([
{
file: "src/app.ts",
additions: 2,
deletions: 1,
patch: expect.any(String),
status: "modified",
},
{
file: "src/new.ts",
additions: 2,
deletions: 0,
patch: expect.any(String),
status: "added",
},
{
file: "src/old.ts",
additions: 0,
deletions: 2,
patch: expect.any(String),
status: "deleted",
},
]);
});
});
Loading