Skip to content
Closed
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed
- Fixed `GET /api/commit` parsing for commit bodies containing control-character separators so author and parent fields are returned correctly. [#1081](https://github.com/sourcebot-dev/sourcebot/pull/1081)

## [4.16.5] - 2026-04-02

### Added
Expand Down
97 changes: 97 additions & 0 deletions packages/web/src/features/git/getCommitApi.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { describe, it, expect, vi, beforeEach, type Mock } from 'vitest';
import { getCommit } from './getCommitApi';

vi.mock('simple-git');
vi.mock('@sourcebot/shared', () => ({
getRepoPath: (repo: { id: number }) => ({
path: `/mock/cache/dir/${repo.id}`,
}),
createLogger: () => ({
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
}),
}));
vi.mock('@/middleware/sew', () => ({
sew: async <T>(fn: () => Promise<T> | T): Promise<T> => {
return await fn();
},
}));

const mockFindFirst = vi.fn();
vi.mock('@/middleware/withAuth', () => ({
withOptionalAuth: async <T>(fn: (args: { org: { id: number }; prisma: unknown }) => Promise<T>): Promise<T> => {
return await fn({
org: { id: 1 },
prisma: {
repo: {
findFirst: mockFindFirst,
},
},
});
},
}));
vi.mock('@/lib/serviceError', () => ({
notFound: (message: string) => ({
errorCode: 'NOT_FOUND',
message,
}),
invalidGitRef: (ref: string) => ({
errorCode: 'INVALID_GIT_REF',
message: `Invalid git reference: "${ref}". Git refs cannot start with '-'.`,
}),
unexpectedError: (message: string) => ({
errorCode: 'UNEXPECTED_ERROR',
message,
}),
}));

import { simpleGit } from 'simple-git';

describe('getCommit', () => {
const mockRaw = vi.fn();
const mockCwd = vi.fn();
const mockSimpleGit = simpleGit as unknown as Mock;

beforeEach(() => {
vi.clearAllMocks();
mockFindFirst.mockResolvedValue({ id: 123, name: 'github.com/test/repo' });
mockCwd.mockReturnValue({
raw: mockRaw,
});
mockSimpleGit.mockReturnValue({
cwd: mockCwd,
});
});

it('parses commit body correctly when it contains field separator', async () => {
const fieldSep = '\x1f';
mockRaw.mockResolvedValue([
'abc123',
'2026-04-02T00:00:00Z',
'subject line',
'HEAD -> main',
`body before separator ${fieldSep} body after separator`,
'Test User',
'test@example.com',
'p1 p2',
].join(fieldSep));

const result = await getCommit({
repo: 'github.com/test/repo',
ref: 'HEAD',
});

expect(result).toEqual({
hash: 'abc123',
date: '2026-04-02T00:00:00Z',
message: 'subject line',
refs: 'HEAD -> main',
body: `body before separator ${fieldSep} body after separator`,
authorName: 'Test User',
authorEmail: 'test@example.com',
parents: ['p1', 'p2'],
});
});
});
9 changes: 8 additions & 1 deletion packages/web/src/features/git/getCommitApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,14 @@ export const getCommit = async ({
return unexpectedError(`Failed to parse commit data for revision "${ref}".`);
}

const [hash, date, message, refs, body, authorName, authorEmail, parentStr] = fields;
const hash = fields[0];
const date = fields[1];
const message = fields[2];
const refs = fields[3];
const body = fields.slice(4, fields.length - 3).join(FIELD_SEP);
const authorName = fields[fields.length - 3];
const authorEmail = fields[fields.length - 2];
const parentStr = fields[fields.length - 1];
const parents = parentStr.trim() === '' ? [] : parentStr.trim().split(' ');

return {
Expand Down
Loading