Skip to content

Commit 34d8a53

Browse files
committed
feat: get git commits
1 parent 507bc20 commit 34d8a53

File tree

7 files changed

+341
-9
lines changed

7 files changed

+341
-9
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
"id": "git-panel.history",
5353
"name": "Git Panel",
5454
"type": "webview",
55-
"icon": "$(list)",
55+
"icon": "$(repo-forked)",
5656
"contextualTitle": "History"
5757
},
5858
{
@@ -134,6 +134,7 @@
134134
"release": "bumpp && nr publish"
135135
},
136136
"dependencies": {
137+
"simple-git": "^3.27.0",
137138
"vue": "^3.5.13"
138139
},
139140
"devDependencies": {

pnpm-lock.yaml

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/container/webview.ts

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
11
import path from 'node:path'
22
import type * as vscode from 'vscode'
33
import { Uri } from 'vscode'
4+
import { GitService } from '../services/git'
5+
import { StorageService } from '../services/storage'
46

57
export class GitPanelViewProvider implements vscode.WebviewViewProvider {
8+
private gitService: GitService
9+
private storageService: StorageService
610
public static readonly viewType = 'git-panel.history'
11+
private _view?: vscode.WebviewView
712

813
constructor(
9-
private readonly _extensionUri: vscode.Uri,
10-
) { }
14+
private readonly _extensionUri: Uri,
15+
context: vscode.ExtensionContext,
16+
) {
17+
this.gitService = new GitService()
18+
this.storageService = new StorageService(context)
19+
}
1120

1221
public resolveWebviewView(
1322
webviewView: vscode.WebviewView,
1423
) {
24+
this._view = webviewView
25+
1526
webviewView.webview.options = {
1627
enableScripts: true,
1728
localResourceRoots: [
@@ -20,6 +31,43 @@ export class GitPanelViewProvider implements vscode.WebviewViewProvider {
2031
}
2132

2233
webviewView.webview.html = this._getHtmlForWebview(webviewView.webview)
34+
35+
// Handle messages from webview
36+
webviewView.webview.onDidReceiveMessage(async (message) => {
37+
switch (message.command) {
38+
case 'getHistory':
39+
try {
40+
// Try to get commits from storage first
41+
let commits = this.storageService.getCommits()
42+
// If no stored commits or force refresh, get from git
43+
if (commits.length === 0 || message.forceRefresh) {
44+
const history = await this.gitService.getHistory()
45+
46+
console.log('history', history)
47+
48+
commits = history.all
49+
// Store the new commits
50+
this.storageService.saveCommits(commits)
51+
}
52+
53+
webviewView.webview.postMessage({
54+
command: 'history',
55+
data: commits,
56+
})
57+
}
58+
catch (error) {
59+
webviewView.webview.postMessage({
60+
command: 'Failed to get git history',
61+
message: `${error}`,
62+
})
63+
}
64+
break
65+
66+
case 'clearHistory':
67+
this.storageService.clearCommits()
68+
break
69+
}
70+
})
2371
}
2472

2573
private _getHtmlForWebview(webview: vscode.Webview) {

src/index.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import { GitPanelViewProvider } from './container/webview'
55

66
import { logger } from './utils'
77

8-
const { activate, deactivate } = defineExtension(async () => {
8+
export function activate(context: vscode.ExtensionContext) {
99
logger.info('Git Panel Activated')
10-
const provider = new GitPanelViewProvider(vscode.Uri.file(__dirname))
10+
const provider = new GitPanelViewProvider(vscode.Uri.file(__dirname), context)
1111
vscode.window.registerWebviewViewProvider(GitPanelViewProvider.viewType, provider)
12-
})
12+
}
1313

14-
export { activate, deactivate }
14+
export function deactivate() {
15+
// Clean up resources
16+
}

src/services/git.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import simpleGit from 'simple-git'
2+
import type { LogResult, SimpleGit } from 'simple-git'
3+
import * as vscode from 'vscode'
4+
5+
export class GitService {
6+
private git: SimpleGit
7+
private readonly rootRepoPath = vscode.workspace.workspaceFolders![0].uri.fsPath
8+
9+
constructor() {
10+
try {
11+
this.git = simpleGit(this.rootRepoPath, {
12+
binary: 'git',
13+
maxConcurrentProcesses: 10,
14+
})
15+
}
16+
catch (error) {
17+
throw new Error(`Fail to init git service: ${error}`)
18+
}
19+
}
20+
21+
async getHistory(): Promise<LogResult> {
22+
const COMMIT_FORMAT = '%H%n%D%n%aN%n%aE%n%at%n%ct%n%P%n%B'
23+
24+
try {
25+
return await this.git.log()
26+
}
27+
catch (error) {
28+
console.error('Error getting git history:', error)
29+
throw error
30+
}
31+
}
32+
}

src/services/storage.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import type * as vscode from 'vscode'
2+
3+
export class StorageService {
4+
private static readonly COMMITS_KEY = 'git-panel.commits'
5+
6+
constructor(private context: vscode.ExtensionContext) {}
7+
8+
saveCommits(commits: any[]) {
9+
this.context.globalState.update(StorageService.COMMITS_KEY, commits)
10+
}
11+
12+
getCommits(): any[] {
13+
return this.context.globalState.get<any[]>(StorageService.COMMITS_KEY) || []
14+
}
15+
16+
clearCommits() {
17+
this.context.globalState.update(StorageService.COMMITS_KEY, undefined)
18+
}
19+
}

0 commit comments

Comments
 (0)