mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
Fix git porcelain output parsing and deduplicate staged/unstaged file entries
This commit is contained in:
1 parent
82ccacb7be
commit
353b9d4495
4 files changed
+26
-21
No files matched your search
@@ -61,8 +61,8 @@ export class GitStatusService {
|
||||
}
|
||||
|
||||
private parsePorcelainOutput(output: string): GitFileStatus[] {
|
||||
const files: GitFileStatus[] = []
|
||||
const lines = output.trim().split('\n').filter(line => line.trim())
|
||||
const fileMap = new Map<string, GitFileStatus>()
|
||||
const lines = output.split('\n').filter(line => line.length > 0)
|
||||
|
||||
for (const line of lines) {
|
||||
if (line.length < 3) continue
|
||||
@@ -78,34 +78,41 @@ export class GitStatusService {
|
||||
filePath = filePath.substring(arrowIndex + 4)
|
||||
}
|
||||
|
||||
const existing = fileMap.get(filePath)
|
||||
|
||||
if (stagedStatus !== ' ' && stagedStatus !== '?') {
|
||||
files.push({
|
||||
const fileStatus: GitFileStatus = {
|
||||
path: filePath,
|
||||
status: this.parseStatusCode(stagedStatus),
|
||||
staged: true,
|
||||
...(oldPath && { oldPath })
|
||||
})
|
||||
}
|
||||
fileMap.set(filePath, fileStatus)
|
||||
continue
|
||||
}
|
||||
|
||||
if (unstagedStatus !== ' ' && unstagedStatus !== '?') {
|
||||
files.push({
|
||||
if (unstagedStatus !== ' ' && unstagedStatus !== '?' && !existing) {
|
||||
const fileStatus: GitFileStatus = {
|
||||
path: filePath,
|
||||
status: this.parseStatusCode(unstagedStatus),
|
||||
staged: false,
|
||||
...(oldPath && { oldPath })
|
||||
})
|
||||
}
|
||||
fileMap.set(filePath, fileStatus)
|
||||
continue
|
||||
}
|
||||
|
||||
if (stagedStatus === '?' || unstagedStatus === '?') {
|
||||
files.push({
|
||||
if ((stagedStatus === '?' || unstagedStatus === '?') && !existing) {
|
||||
const fileStatus: GitFileStatus = {
|
||||
path: filePath,
|
||||
status: 'untracked',
|
||||
staged: false
|
||||
})
|
||||
}
|
||||
fileMap.set(filePath, fileStatus)
|
||||
}
|
||||
}
|
||||
|
||||
return files
|
||||
return Array.from(fileMap.values())
|
||||
}
|
||||
|
||||
private parseStatusCode(code: string): 'modified' | 'added' | 'deleted' | 'renamed' | 'untracked' | 'copied' {
|
||||
|
||||
@@ -81,9 +81,8 @@ describe('GitStatusService', () => {
|
||||
|
||||
const result = await service.getStatus(1, database)
|
||||
|
||||
expect(result.files).toHaveLength(2)
|
||||
expect(result.files).toHaveLength(1)
|
||||
expect(result.files[0]).toEqual({ path: 'file.ts', status: 'modified', staged: true })
|
||||
expect(result.files[1]).toEqual({ path: 'file.ts', status: 'modified', staged: false })
|
||||
expect(result.hasChanges).toBe(true)
|
||||
})
|
||||
|
||||
@@ -199,9 +198,8 @@ describe('GitStatusService', () => {
|
||||
|
||||
const result = await service.getStatus(1, database)
|
||||
|
||||
expect(result.files).toHaveLength(5)
|
||||
expect(result.files).toHaveLength(4)
|
||||
expect(result.files).toContainEqual({ path: 'file1.ts', status: 'modified', staged: true })
|
||||
expect(result.files).toContainEqual({ path: 'file1.ts', status: 'modified', staged: false })
|
||||
expect(result.files).toContainEqual({ path: 'file2.ts', status: 'added', staged: true })
|
||||
expect(result.files).toContainEqual({ path: 'file3.ts', status: 'untracked', staged: false })
|
||||
expect(result.files).toContainEqual({ path: 'file4.ts', status: 'deleted', staged: true })
|
||||
|
||||
@@ -96,7 +96,7 @@ export function ChangesTab({ repoId, onFileSelect, selectedFile, isMobile }: Cha
|
||||
<>
|
||||
{stagedFiles.length > 0 && (
|
||||
<GitFlatFileList
|
||||
files={status.files}
|
||||
files={stagedFiles}
|
||||
staged={true}
|
||||
onSelect={onFileSelect}
|
||||
onUnstage={handleUnstage}
|
||||
@@ -106,7 +106,7 @@ export function ChangesTab({ repoId, onFileSelect, selectedFile, isMobile }: Cha
|
||||
|
||||
{unstagedFiles.length > 0 && (
|
||||
<GitFlatFileList
|
||||
files={status.files}
|
||||
files={unstagedFiles}
|
||||
staged={false}
|
||||
onSelect={onFileSelect}
|
||||
onStage={handleStage}
|
||||
@@ -179,7 +179,7 @@ export function ChangesTab({ repoId, onFileSelect, selectedFile, isMobile }: Cha
|
||||
<>
|
||||
{stagedFiles.length > 0 && (
|
||||
<GitFlatFileList
|
||||
files={status.files}
|
||||
files={stagedFiles}
|
||||
staged={true}
|
||||
onSelect={onFileSelect}
|
||||
onUnstage={handleUnstage}
|
||||
@@ -189,7 +189,7 @@ export function ChangesTab({ repoId, onFileSelect, selectedFile, isMobile }: Cha
|
||||
|
||||
{unstagedFiles.length > 0 && (
|
||||
<GitFlatFileList
|
||||
files={status.files}
|
||||
files={unstagedFiles}
|
||||
staged={false}
|
||||
onSelect={onFileSelect}
|
||||
onStage={handleStage}
|
||||
|
||||
@@ -33,8 +33,8 @@ export function GitFlatFileList({
|
||||
const [collapsedGroups, setCollapsedGroups] = useState<Set<GitFileStatusType>>(new Set())
|
||||
|
||||
const filteredFiles = useMemo(() => {
|
||||
return files.filter(f => f.staged === staged)
|
||||
}, [files, staged])
|
||||
return files
|
||||
}, [files])
|
||||
|
||||
const groupedFiles = useMemo(() => {
|
||||
const groups: GroupedFiles[] = []
|
||||
|
||||
Reference in new issue
Block a user