Skip to content
This repository was archived by the owner on Oct 19, 2026. It is now read-only.

Commit 4586c9e

Browse files
authored
fix: Simplify the parsedDiffs function by removing the complex logic for file format and path extraction. (#1746)
* fix: simplify the parsedDiffs function by removing unnecessary path extraction logic. - Remove the complex logic for extracting paths from diff text and directly use block.path. - Identify binary files by detecting "Binary files differ" in the diff text. * fix: simplify the parsedDiffs function by removing unnecessary path extraction logic. - Remove the complex logic for extracting paths from diff text and directly use block.path. - Identify binary files by detecting "Binary files differ" in the diff text.
1 parent 5b265f6 commit 4586c9e

4 files changed

Lines changed: 21 additions & 37 deletions

File tree

‎moon/apps/web/components/CodeView/BlobView/BlobEditor.tsx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { Dialog } from '@gitmono/ui/Dialog'
1212
import { useDiffPreview } from '@/hooks/useDiffPreview'
1313
import { useGetCurrentUser } from '@/hooks/useGetCurrentUser'
1414
import { useUpdateBlob } from '@/hooks/useUpdateBlob'
15-
import { getLangFromFileName } from '@/utils/getLanguageDetection'
15+
import { getLangFromFileNameToDiff } from '@/utils/getLanguageDetection'
1616

1717
interface BlobEditorProps {
1818
fileContent: string
@@ -64,7 +64,7 @@ export default function BlobEditor({ fileContent, filePath, fileName, onCancel }
6464
return dir ? `${dir}/${editedFileName}` : editedFileName
6565
}, [pathSegments, editedFileName])
6666

67-
const detectedLanguage = useMemo(() => getLangFromFileName(editedFileName), [editedFileName])
67+
const detectedLanguage = useMemo(() => getLangFromFileNameToDiff(editedFileName), [editedFileName])
6868

6969
const handlePreviewClick = useCallback(async () => {
7070
setViewMode('preview')
@@ -83,7 +83,7 @@ export default function BlobEditor({ fileContent, filePath, fileName, onCancel }
8383
setDiffResult(result)
8484

8585
if (result?.data?.data) {
86-
const diff = new DiffFile('', '', '', '', [result.data.data], detectedLanguage || 'plaintext')
86+
const diff = new DiffFile('', '', '', '', [result.data.data], detectedLanguage)
8787

8888
diff.init()
8989
diff.buildSplitDiffLines()

‎moon/apps/web/components/CodeView/BlobView/CodeContent.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,9 +486,10 @@ const CodeContent = ({
486486
return (
487487
<div
488488
key={`block-${blockIndex}`}
489+
data-index={blockIndex}
489490
className={`border-x border-gray-200 transition-colors duration-150 ${
490491
isFirstBlock ? 'border-t' : ''
491-
} ${isLastBlock ? 'rounded-b-lg border-b' : ''}`}
492+
} ${isLastBlock ? 'rounded-b-lg border-b' : 'border-b'}`}
492493
>
493494
<div className='flex min-w-0'>
494495
<div className='flex w-1 flex-shrink-0 items-center'>

‎moon/apps/web/components/DiffView/parsedDiffs.ts‎

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,47 +9,30 @@ export function parsedDiffs(diffText: DiffItem[]): { path: string; lang: string;
99
if (diffText.length < 1) return []
1010

1111
return diffText.map((block) => {
12-
let path = ''
12+
const isBinary = /Binary files.*differ/.test(block.data)
1313

14-
const diffGitMatch = block.data.match(/^diff --git a\/[^\s]+ b\/([^\s]+)/m)
14+
const lang = getLangFromFileNameToDiff(block.path)
1515

16-
if (diffGitMatch) {
17-
const originalPath = diffGitMatch[1]?.trim()
18-
const newPath = diffGitMatch[2]?.trim()
16+
let diff = block.data
1917

20-
if (newPath && newPath !== '/dev/null') {
21-
path = newPath
22-
} else {
23-
path = originalPath
24-
}
25-
}
18+
if (isBinary) {
19+
/* empty */
20+
} else {
21+
const isEmptyDiff = !block.data.includes('@@')
2622

27-
if (getLangFromFileNameToDiff(path) === 'binary') {
28-
return {
29-
path,
30-
lang: getLangFromFileNameToDiff(path),
31-
diff: block.data
23+
if (isEmptyDiff) {
24+
diff = 'EMPTY_DIFF_MARKER'
3225
}
3326
}
3427

35-
let diffWithHeader = block.data
36-
const headerRegex = /^(diff|index|---|\+\+\+|new file mode|@@)/
37-
const hunkContent = block.data.split('\n').filter((line) => !headerRegex.test(line.trim()))
38-
39-
const isEmptyHunk = hunkContent.every((line) => line.trim() === '')
40-
41-
if (!block.data.includes('@@') || isEmptyHunk) {
42-
diffWithHeader = 'EMPTY_DIFF_MARKER'
43-
}
44-
45-
if (!diffWithHeader.endsWith('\n')) {
46-
diffWithHeader += '\n'
28+
if (!diff.endsWith('\n')) {
29+
diff += '\n'
4730
}
4831

4932
return {
5033
path: block.path,
51-
lang: getLangFromFileNameToDiff(path),
52-
diff: diffWithHeader
34+
lang: isBinary ? 'binary' : lang,
35+
diff
5336
}
5437
})
5538
}

‎moon/apps/web/utils/getLanguageDetection.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ export function getLangFromFileName(fileName: string): string {
190190
}
191191

192192
export function getLangFromFileNameToDiff(fileName: string): string {
193-
if (!fileName) return 'binary'
193+
if (!fileName) return 'plaintext'
194194
const lowerFileName = fileName.toLowerCase()
195195
const baseName = lowerFileName.split('/').pop() || ''
196196

@@ -209,8 +209,8 @@ export function getLangFromFileNameToDiff(fileName: string): string {
209209
if (lastPart) {
210210
const extension = lastPart[0]
211211

212-
return LANGUAGE_MAP[extension] ?? 'binary'
212+
return LANGUAGE_MAP[extension] ?? 'plaintext'
213213
}
214214

215-
return 'binary'
215+
return 'plaintext'
216216
}

0 commit comments

Comments
 (0)