Skip to content

Commit 39aa10a

Browse files
committed
fix: diff file
1 parent 53d54d3 commit 39aa10a

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

src/git/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ export class GitService {
6565

6666
async getParentCommit(commitHash: string): Promise<string | null> {
6767
try {
68-
const result = await this.git.raw(['rev-parse', `${commitHash}^`])
69-
return result.trim()
68+
const result = await this.git.raw(['rev-list', '--parents', '-n', '1', commitHash])
69+
const [, parentHash] = result.trim().split(' ')
70+
return parentHash || null
7071
}
7172
catch (error) {
7273
console.error('Failed to get parent commit:', error)

src/index.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,30 +42,37 @@ export function activate(context: vscode.ExtensionContext) {
4242

4343
// For modified files, show diff between current commit and its parent
4444
if (fileInfo.status === 'M') {
45-
const parentCommit = await gitService.getParentCommit(commit)
46-
if (parentCommit) {
47-
const leftUri = toGitUri(uri, parentCommit, fileInfo)
48-
const rightUri = toGitUri(uri, commit, fileInfo)
49-
await vscode.commands.executeCommand('vscode.diff', leftUri, rightUri, title)
45+
try {
46+
const parentCommit = await gitService.getParentCommit(commit)
47+
if (parentCommit) {
48+
const leftUri = toGitUri(uri, parentCommit)
49+
const rightUri = toGitUri(uri, commit)
50+
await vscode.commands.executeCommand('vscode.diff', leftUri, rightUri, title)
51+
return
52+
}
53+
}
54+
catch (error) {
55+
vscode.window.showErrorMessage(`无法获取父提交: ${error}`)
5056
return
5157
}
5258
}
5359

5460
// For added files, show the entire file content
5561
if (fileInfo.status === 'A') {
56-
const gitUri = toGitUri(uri, commit, fileInfo)
62+
const gitUri = toGitUri(uri, commit)
5763
await vscode.commands.executeCommand('vscode.open', gitUri)
5864
}
5965
}),
6066
)
6167
}
6268

6369
// Helper function to create Git URIs
64-
function toGitUri(uri: vscode.Uri, ref: string, fileInfo: { path: string }): vscode.Uri {
70+
function toGitUri(uri: vscode.Uri, ref: string): vscode.Uri {
6571
return uri.with({
6672
scheme: 'git',
73+
path: uri.path,
6774
query: JSON.stringify({
68-
path: fileInfo.path,
75+
path: uri.path,
6976
ref,
7077
}),
7178
})

0 commit comments

Comments
 (0)