Skip to content

Commit 3c7f230

Browse files
committed
feat: diff for delete & rename
1 parent 0be29aa commit 3c7f230

File tree

2 files changed

+55
-9
lines changed

2 files changed

+55
-9
lines changed

src/commands/diff.ts

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { executeCommand, useWorkspaceFolders } from 'reactive-vscode'
33

44
import { useGitService } from '@/git'
55
import { GIT_STATUS } from '@/constant'
6-
import { toGitUri } from '@/utils'
6+
import { getFileNameByPath, shortHash, toGitUri } from '@/utils'
77
import { useDiffTreeView } from '@/views/diff'
88

99
export default function diffCommand() {
@@ -23,11 +23,11 @@ export default function diffCommand() {
2323
}
2424

2525
const uri = Uri.joinPath(workspaceRoot, fileInfo.path)
26-
const title = `${fileInfo.path} (${fileInfo.commitHash})`
2726

2827
// For modified files, show diff between current commit and its parent
2928
if (fileInfo.status === GIT_STATUS.MODIFIED) {
3029
try {
30+
const title = `${getFileNameByPath(fileInfo.path)} (${shortHash(fileInfo.commitHash)})`
3131
const previousCommit = await getPreviousCommit(fileInfo.commitHash)
3232
if (previousCommit) {
3333
const leftUri = toGitUri(uri, previousCommit)
@@ -42,10 +42,48 @@ export default function diffCommand() {
4242
}
4343
}
4444

45-
// For added files, show the entire file content
45+
// For added files, show diff between empty untitled file and new file
4646
if (fileInfo.status === GIT_STATUS.ADDED) {
47+
const title = `${getFileNameByPath(fileInfo.path)} (Add to ${shortHash(fileInfo.commitHash)})`
48+
const emptyUri = Uri.parse(`untitled: ${fileInfo.path}`)
4749
const gitUri = toGitUri(uri, fileInfo.commitHash)
48-
await executeCommand('vscode.open', gitUri)
50+
await executeCommand('vscode.diff', emptyUri, gitUri, title)
51+
}
52+
53+
// For deleted files, show diff between original file and empty untitled file
54+
if (fileInfo.status === GIT_STATUS.DELETED) {
55+
try {
56+
const previousCommit = await getPreviousCommit(fileInfo.commitHash)
57+
58+
if (previousCommit) {
59+
const title = `${getFileNameByPath(fileInfo.path)} (Remove to ${shortHash(previousCommit)})`
60+
const gitUri = toGitUri(uri, previousCommit)
61+
const emptyUri = Uri.parse(`untitled: ${fileInfo.path}`)
62+
await executeCommand('vscode.diff', gitUri, emptyUri, title)
63+
}
64+
}
65+
catch (error) {
66+
window.showErrorMessage(`Fail to get parent commit: ${error}`)
67+
}
68+
}
69+
70+
// For renamed files, show diff between old file path and new file path
71+
if (fileInfo.status === GIT_STATUS.RENAMED) {
72+
try {
73+
const previousCommit = await getPreviousCommit(fileInfo.commitHash)
74+
if (previousCommit && fileInfo.oldPath) {
75+
// Create URI for old file path
76+
const oldUri = Uri.joinPath(workspaceRoot, fileInfo.oldPath)
77+
const leftUri = toGitUri(oldUri, previousCommit)
78+
const rightUri = toGitUri(uri, fileInfo.commitHash)
79+
80+
const renameTitle = `${getFileNameByPath(fileInfo.oldPath)}${getFileNameByPath(fileInfo.path)} (${shortHash(fileInfo.commitHash)})`
81+
await executeCommand('vscode.diff', leftUri, rightUri, renameTitle)
82+
}
83+
}
84+
catch (error) {
85+
window.showErrorMessage(`Fail to get parent commit for renamed file: ${error}`)
86+
}
4987
}
5088
}
5189
}

src/utils.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import type { Uri } from 'vscode'
12
import { useLogger } from 'reactive-vscode'
23
import { extensions } from 'vscode'
34

4-
import type { Uri } from 'vscode'
5-
65
import { displayName } from './generated/meta'
6+
import type { GIT_STATUS } from './constant'
77

88
const branchColorCache = new Map<string, string>()
99
let colorCounter = 0
@@ -43,15 +43,15 @@ export function toGitUri(uri: Uri, ref: string): Uri {
4343
})
4444
}
4545

46-
export function parseGitStatus(status: string): { type: string, similarity?: number } {
46+
export function parseGitStatus(status: string): { type: keyof typeof GIT_STATUS, similarity?: number } {
4747
const match = status.match(/^([A-Z])(\d+)?$/)
4848
if (match) {
4949
return {
50-
type: match[1],
50+
type: match[1] as keyof typeof GIT_STATUS,
5151
similarity: match[2] ? Number.parseInt(match[2], 10) : undefined,
5252
}
5353
}
54-
return { type: status }
54+
return { type: status as keyof typeof GIT_STATUS }
5555
}
5656

5757
/**
@@ -82,3 +82,11 @@ export function getBranchColor(branchName: string): string {
8282

8383
return color
8484
}
85+
86+
export function shortHash(hash: string): string {
87+
return hash.substring(0, 7)
88+
}
89+
90+
export function getFileNameByPath(path: string): string {
91+
return path.split('/').pop() || ''
92+
}

0 commit comments

Comments
 (0)