Skip to content

Commit 524f917

Browse files
fix: filter source control commits by branch
1 parent 71ba737 commit 524f917

6 files changed

Lines changed: 67 additions & 11 deletions

File tree

backend/src/routes/repo-git.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,8 @@ export function createRepoGitRoutes(database: Database, gitAuthService: GitAuthS
407407
}
408408

409409
const limit = parseInt(c.req.query('limit') || '10', 10)
410-
const commits = await git.getLog(id, database, limit)
410+
const branch = c.req.query('branch') || undefined
411+
const commits = await git.getLog(id, database, limit, branch)
411412

412413
return c.json({ commits })
413414
} catch (error: unknown) {

backend/src/services/git/GitService.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export class GitService {
8484
return this.getFileDiff(repoId, filePath, database, { includeStaged })
8585
}
8686

87-
async getLog(repoId: number, database: Database, limit: number = 10): Promise<GitCommit[]> {
87+
async getLog(repoId: number, database: Database, limit: number = 10, branch?: string): Promise<GitCommit[]> {
8888
try {
8989
const repo = getRepoById(database, repoId)
9090
if (!repo) {
@@ -97,7 +97,7 @@ export class GitService {
9797
'-C',
9898
repoPath,
9999
'log',
100-
`--all`,
100+
branch?.trim() || 'HEAD',
101101
`-n`,
102102
String(limit),
103103
'--format=%H|%an|%ae|%at|%s'

backend/test/services/git/GitService.test.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,16 @@ describe('GitService', () => {
436436
const result = await service.getLog(1, database, 10)
437437

438438
expect(getRepoByIdMock).toHaveBeenCalledWith(database, 1)
439+
expect(executeCommandMock).toHaveBeenNthCalledWith(1, [
440+
'git',
441+
'-C',
442+
'/path/to/repo',
443+
'log',
444+
'HEAD',
445+
'-n',
446+
'10',
447+
'--format=%H|%an|%ae|%at|%s'
448+
], { env: {} })
439449
expect(result).toHaveLength(2)
440450
expect(result[0]).toEqual({
441451
hash: 'abc123',
@@ -467,6 +477,50 @@ describe('GitService', () => {
467477

468478
expect(result).toEqual([])
469479
})
480+
481+
it('uses the requested branch when provided', async () => {
482+
const mockRepo = {
483+
id: 1,
484+
fullPath: '/path/to/repo',
485+
}
486+
getRepoByIdMock.mockReturnValue(mockRepo as any)
487+
executeCommandMock.mockResolvedValueOnce('').mockResolvedValueOnce('')
488+
489+
await service.getLog(1, database, 10, 'feature/test')
490+
491+
expect(executeCommandMock).toHaveBeenNthCalledWith(1, [
492+
'git',
493+
'-C',
494+
'/path/to/repo',
495+
'log',
496+
'feature/test',
497+
'-n',
498+
'10',
499+
'--format=%H|%an|%ae|%at|%s'
500+
], { env: {} })
501+
})
502+
503+
it('falls back to HEAD for blank branch values', async () => {
504+
const mockRepo = {
505+
id: 1,
506+
fullPath: '/path/to/repo',
507+
}
508+
getRepoByIdMock.mockReturnValue(mockRepo as any)
509+
executeCommandMock.mockResolvedValueOnce('').mockResolvedValueOnce('')
510+
511+
await service.getLog(1, database, 10, ' ')
512+
513+
expect(executeCommandMock).toHaveBeenNthCalledWith(1, [
514+
'git',
515+
'-C',
516+
'/path/to/repo',
517+
'log',
518+
'HEAD',
519+
'-n',
520+
'10',
521+
'--format=%H|%an|%ae|%at|%s'
522+
], { env: {} })
523+
})
470524
})
471525

472526
describe('getCommit', () => {

frontend/src/api/git.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ export async function fetchGitDiff(repoId: number, path: string): Promise<{ diff
3535
return { diff: data }
3636
}
3737

38-
export async function fetchGitLog(repoId: number, limit?: number): Promise<{ commits: GitCommit[] }> {
38+
export async function fetchGitLog(repoId: number, limit?: number, branch?: string): Promise<{ commits: GitCommit[] }> {
3939
return fetchWrapper(`${API_BASE_URL}/api/repos/${repoId}/git/log`, {
40-
params: { limit },
40+
params: { limit, branch },
4141
})
4242
}
4343

@@ -130,10 +130,10 @@ export function useCommitFileDiff(repoId: number | undefined, commitHash: string
130130
})
131131
}
132132

133-
export function useGitLog(repoId: number | undefined, limit?: number) {
133+
export function useGitLog(repoId: number | undefined, limit?: number, branch?: string) {
134134
return useQuery({
135-
queryKey: ['gitLog', repoId, limit],
136-
queryFn: () => repoId ? fetchGitLog(repoId, limit) : Promise.reject(new Error('No repo ID')),
135+
queryKey: ['gitLog', repoId, limit, branch],
136+
queryFn: () => repoId ? fetchGitLog(repoId, limit, branch) : Promise.reject(new Error('No repo ID')),
137137
enabled: !!repoId,
138138
})
139139
}

frontend/src/components/source-control/CommitsTab.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { GIT_UI_COLORS } from '@/lib/git-status-styles'
55

66
interface CommitsTabProps {
77
repoId: number
8+
branch: string
89
onSelectCommit?: (hash: string) => void
910
}
1011

@@ -28,8 +29,8 @@ function formatRelativeTime(timestamp: string): string {
2829
return `${diffMonths}mo ago`
2930
}
3031

31-
export function CommitsTab({ repoId, onSelectCommit }: CommitsTabProps) {
32-
const { data, isLoading, error } = useGitLog(repoId, 50)
32+
export function CommitsTab({ repoId, branch, onSelectCommit }: CommitsTabProps) {
33+
const { data, isLoading, error } = useGitLog(repoId, 50, branch)
3334

3435
if (isLoading) {
3536
return (

frontend/src/components/source-control/SourceControlPanel.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ export function SourceControlPanel({
230230
/>
231231
)}
232232
{activeTab === 'commits' && currentView === 'default' && (
233-
<CommitsTab repoId={repoId} onSelectCommit={handleSelectCommit} />
233+
<CommitsTab repoId={repoId} branch={displayBranch} onSelectCommit={handleSelectCommit} />
234234
)}
235235
{activeTab === 'branches' && currentView === 'default' && (
236236
<BranchesTab repoId={repoId} currentBranch={displayBranch} />

0 commit comments

Comments
 (0)