@@ -3,7 +3,7 @@ import { executeCommand, useWorkspaceFolders } from 'reactive-vscode'
33
44import { useGitService } from '@/git'
55import { GIT_STATUS } from '@/constant'
6- import { toGitUri } from '@/utils'
6+ import { getFileNameByPath , shortHash , toGitUri } from '@/utils'
77import { useDiffTreeView } from '@/views/diff'
88
99export 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}
0 commit comments