Skip to content

Commit a1232fa

Browse files
committed
feat: refresh command
1 parent 4d1b0e6 commit a1232fa

File tree

8 files changed

+121
-98
lines changed

8 files changed

+121
-98
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"publisher": "zhangmo8",
33
"name": "git-panel",
44
"displayName": "Git Panel",
5-
"version": "0.0.4-beta.1",
5+
"version": "0.0.4",
66
"private": true,
77
"packageManager": "pnpm@9.7.1",
88
"description": "",

src/commands/diff.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import * as vscode from 'vscode'
2+
import type { DiffProvider } from '@/views/diff'
3+
import { GitService } from '@/git'
4+
5+
export default function diffCommand(gitService: GitService, diffProvider: DiffProvider) {
6+
return vscode.commands.registerCommand('vscGitPanel.openDiff', async (fileInfo: { path: string, status: string }) => {
7+
const commit = diffProvider.getSelectedCommitHash()
8+
if (!commit) {
9+
return
10+
}
11+
12+
const workspaceRoot = vscode.workspace.workspaceFolders?.[0]?.uri
13+
if (!workspaceRoot) {
14+
return
15+
}
16+
17+
const uri = vscode.Uri.joinPath(workspaceRoot, fileInfo.path)
18+
const title = `${fileInfo.path} (${commit})`
19+
20+
// For modified files, show diff between current commit and its parent
21+
if (fileInfo.status === 'M') {
22+
try {
23+
const parentCommit = await gitService.getParentCommit(commit)
24+
if (parentCommit) {
25+
const leftUri = GitService.toGitUri(uri, parentCommit)
26+
const rightUri = GitService.toGitUri(uri, commit)
27+
await vscode.commands.executeCommand('vscode.diff', leftUri, rightUri, title)
28+
return
29+
}
30+
}
31+
catch (error) {
32+
vscode.window.showErrorMessage(`无法获取父提交: ${error}`)
33+
return
34+
}
35+
}
36+
37+
// For added files, show the entire file content
38+
if (fileInfo.status === 'A') {
39+
const gitUri = GitService.toGitUri(uri, commit)
40+
await vscode.commands.executeCommand('vscode.open', gitUri)
41+
}
42+
})
43+
}

src/commands/index.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type * as vscode from 'vscode'
2+
3+
import refreshCommand from './refresh'
4+
import diffCommand from './diff'
5+
6+
import type { GitService } from '@/git'
7+
import type { DiffProvider } from '@/views/diff'
8+
import type { GitPanelViewProvider } from '@/views/webview'
9+
10+
interface CommandProvider {
11+
gitService: GitService
12+
diffProvider: DiffProvider
13+
provider: GitPanelViewProvider
14+
}
15+
16+
export function initCommands(context: vscode.ExtensionContext, { gitService, diffProvider, provider }: CommandProvider) {
17+
context.subscriptions.push(
18+
refreshCommand(provider),
19+
diffCommand(gitService, diffProvider),
20+
)
21+
}

src/commands/refresh.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import * as vscode from 'vscode'
2+
import type { GitPanelViewProvider } from '@/views/webview'
3+
4+
export default function refreshCommand(provider: GitPanelViewProvider) {
5+
return vscode.commands.registerCommand('git-panel.history.refresh', () => {
6+
provider.refreshHistory(true)
7+
})
8+
}

src/git/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,15 @@ export class GitService {
7474
return null
7575
}
7676
}
77+
78+
static toGitUri(uri: vscode.Uri, ref: string): vscode.Uri {
79+
return uri.with({
80+
scheme: 'git',
81+
path: uri.path,
82+
query: JSON.stringify({
83+
path: uri.path,
84+
ref,
85+
}),
86+
})
87+
}
7788
}

src/index.ts

Lines changed: 3 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { GitService } from './git'
77
import { StorageService } from './storage'
88
import { logger } from './utils'
99

10+
import { initCommands } from './commands'
11+
1012
export function activate(context: vscode.ExtensionContext) {
1113
logger.info('Git Panel Activated')
1214

@@ -24,56 +26,5 @@ export function activate(context: vscode.ExtensionContext) {
2426
showCollapseAll: true,
2527
})
2628

27-
// Register openDiff command
28-
context.subscriptions.push(
29-
vscode.commands.registerCommand('vscGitPanel.openDiff', async (fileInfo: { path: string, status: string }) => {
30-
const commit = diffProvider.getSelectedCommitHash()
31-
if (!commit) {
32-
return
33-
}
34-
35-
const workspaceRoot = vscode.workspace.workspaceFolders?.[0]?.uri
36-
if (!workspaceRoot) {
37-
return
38-
}
39-
40-
const uri = vscode.Uri.joinPath(workspaceRoot, fileInfo.path)
41-
const title = `${fileInfo.path} (${commit})`
42-
43-
// For modified files, show diff between current commit and its parent
44-
if (fileInfo.status === 'M') {
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}`)
56-
return
57-
}
58-
}
59-
60-
// For added files, show the entire file content
61-
if (fileInfo.status === 'A') {
62-
const gitUri = toGitUri(uri, commit)
63-
await vscode.commands.executeCommand('vscode.open', gitUri)
64-
}
65-
}),
66-
)
67-
}
68-
69-
// Helper function to create Git URIs
70-
function toGitUri(uri: vscode.Uri, ref: string): vscode.Uri {
71-
return uri.with({
72-
scheme: 'git',
73-
path: uri.path,
74-
query: JSON.stringify({
75-
path: uri.path,
76-
ref,
77-
}),
78-
})
29+
initCommands(context, { gitService, diffProvider, provider })
7930
}

src/views/history/App.vue

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ window.vscode = vscode
3131
// Handle messages from extension
3232
window.addEventListener('message', (event: { data: any }) => {
3333
const message = event.data
34+
3435
switch (message.command) {
3536
case CHANNEL.HISTORY:
3637
commits.value = message.commits as Commit[]
@@ -41,26 +42,6 @@ window.addEventListener('message', (event: { data: any }) => {
4142
}
4243
})
4344
44-
// Save state when it changes
45-
watch([commits, selectedHash, filter], () => {
46-
try {
47-
const state: State = {
48-
commits: commits.value || [],
49-
selectedHash: selectedHash.value || '',
50-
filter: filter.value || '',
51-
}
52-
vscode.postMessage({ command: 'setState', state })
53-
}
54-
catch (err) {
55-
console.error('Failed to save state:', err)
56-
}
57-
}, { deep: true })
58-
59-
// function refreshHistory() {
60-
// commits.value = []
61-
// vscode.postMessage({ command: 'getHistory', forceRefresh: true })
62-
// }
63-
6445
onMounted(() => {
6546
vscode.postMessage({ command: WEBVIEW_CHANNEL.GET_HISTORY })
6647
})

src/views/webview.ts

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,41 @@ export class GitPanelViewProvider implements vscode.WebviewViewProvider {
2626
this._commits = this.storageService.getCommits()
2727
}
2828

29+
public async refreshHistory(forceRefresh: boolean = false) {
30+
if (!this._view)
31+
return
32+
33+
try {
34+
if (this._commits.length === 0 || forceRefresh) {
35+
const history = await this.gitService.getHistory()
36+
37+
this._commits = history.all.map(commit => ({
38+
...commit,
39+
authorName: commit.author_name,
40+
authorEmail: commit.author_email,
41+
body: commit.body || '',
42+
}))
43+
44+
this.storageService.saveCommits(this._commits)
45+
}
46+
47+
this._view.webview.postMessage({
48+
command: CHANNEL.HISTORY,
49+
commits: this._commits,
50+
})
51+
}
52+
catch (error) {
53+
this._view.webview.postMessage({
54+
command: 'Failed to get git history',
55+
message: `${error}`,
56+
})
57+
}
58+
}
59+
2960
public resolveWebviewView(
3061
webviewView: vscode.WebviewView,
3162
) {
63+
this._view = webviewView
3264
webviewView.webview.options = {
3365
enableScripts: true,
3466
localResourceRoots: [
@@ -43,31 +75,7 @@ export class GitPanelViewProvider implements vscode.WebviewViewProvider {
4375
webviewView.webview.onDidReceiveMessage(async (message) => {
4476
switch (message.command) {
4577
case WEBVIEW_CHANNEL.GET_HISTORY:
46-
try {
47-
if (this._commits.length === 0 || message.forceRefresh) {
48-
const history = await this.gitService.getHistory()
49-
50-
this._commits = history.all.map(commit => ({
51-
...commit,
52-
authorName: commit.author_name,
53-
authorEmail: commit.author_email,
54-
body: commit.body || '',
55-
}))
56-
57-
this.storageService.saveCommits(this._commits)
58-
}
59-
60-
webviewView.webview.postMessage({
61-
command: CHANNEL.HISTORY,
62-
commits: this._commits,
63-
})
64-
}
65-
catch (error) {
66-
webviewView.webview.postMessage({
67-
command: 'Failed to get git history',
68-
message: `${error}`,
69-
})
70-
}
78+
await this.refreshHistory(message.forceRefresh)
7179
break
7280

7381
case WEBVIEW_CHANNEL.SHOW_COMMIT_DETAILS:

0 commit comments

Comments
 (0)