11import * as vscode from 'vscode'
2- import { GitService } from '../../git'
3- import { CommitNode } from './CommitNode'
2+ import { CommitNode } from './entity/CommitNode'
43import { FileTreeProvider } from './FileTreeProvider'
54import type { CommitDetails } from './types'
5+ import { StorageService } from '@/storage'
6+ import { GitService } from '@/git'
67
7- export class GitChangesProvider implements vscode . TreeDataProvider < CommitNode > {
8+ export class DiffProvider implements vscode . TreeDataProvider < CommitNode > {
89 private _onDidChangeTreeData : vscode . EventEmitter < CommitNode | undefined | null | void > = new vscode . EventEmitter < CommitNode | undefined | null | void > ( )
910 readonly onDidChangeTreeData : vscode . Event < CommitNode | undefined | null | void > = this . _onDidChangeTreeData . event
1011 private gitService : GitService
12+ private storageService : StorageService
1113 private fileTreeProvider : FileTreeProvider
1214 private selectedCommitHash ?: string
13- private static instance : GitChangesProvider
15+ private static instance : DiffProvider
1416
1517 private constructor ( ) {
1618 this . gitService = new GitService ( )
17- this . fileTreeProvider = new FileTreeProvider ( this . gitService )
19+ this . storageService = StorageService . getInstance ( )
20+ this . fileTreeProvider = new FileTreeProvider ( this . gitService , this . storageService )
1821 }
1922
20- static getInstance ( ) : GitChangesProvider {
21- if ( ! GitChangesProvider . instance ) {
22- GitChangesProvider . instance = new GitChangesProvider ( )
23+ static getInstance ( ) : DiffProvider {
24+ if ( ! DiffProvider . instance ) {
25+ DiffProvider . instance = new DiffProvider ( )
2326 }
24- return GitChangesProvider . instance
27+ return DiffProvider . instance
2528 }
2629
2730 refresh ( commitHash ?: string ) : void {
@@ -35,19 +38,33 @@ export class GitChangesProvider implements vscode.TreeDataProvider<CommitNode> {
3538
3639 private async getCommitByHash ( hash ?: string ) : Promise < CommitDetails | null > {
3740 try {
38- const history = await this . gitService . getHistory ( )
39- if ( history . all . length === 0 ) {
40- return null
41+ if ( ! hash ) {
42+ throw new Error ( 'Commit hash is required' )
4143 }
4244
43- const commit = history . all . find ( c => c . hash === hash ) || history . all [ 0 ]
44- return {
45- hash : commit . hash ,
46- authorName : commit . author_name ,
47- authorEmail : commit . author_email ,
48- date : commit . date ,
49- stats : commit . stats ,
45+ // First try to get from cache
46+ let commit = this . storageService . getCommit ( hash )
47+
48+ if ( ! commit ) {
49+ // Only fetch all commits if not found in cache
50+ const history = await this . gitService . getHistory ( )
51+ const historyCommit = history . all . find ( c => c . hash === hash )
52+ if ( ! historyCommit ) {
53+ return null
54+ }
55+
56+ commit = {
57+ hash : historyCommit . hash ,
58+ authorName : historyCommit . author_name ,
59+ authorEmail : historyCommit . author_email ,
60+ date : historyCommit . date ,
61+ message : historyCommit . message ,
62+ body : historyCommit . body ,
63+ stats : historyCommit . stats ,
64+ }
5065 }
66+
67+ return commit
5168 }
5269 catch ( error ) {
5370 console . error ( 'Error getting commit details:' , error )
@@ -56,17 +73,15 @@ export class GitChangesProvider implements vscode.TreeDataProvider<CommitNode> {
5673 }
5774
5875 async getChildren ( element ?: CommitNode ) : Promise < CommitNode [ ] > {
59- if ( ! element ) {
76+ if ( ! element && this . selectedCommitHash ) {
6077 const commitDetails = await this . getCommitByHash ( this . selectedCommitHash )
6178
6279 if ( ! commitDetails ) {
6380 return [ ]
6481 }
6582
66- // Refresh the file tree provider with current commit hash
6783 this . fileTreeProvider . refresh ( commitDetails . hash )
6884
69- // Get changed files from FileTreeProvider
7085 const changedFiles = await this . fileTreeProvider . getChildren ( )
7186
7287 return [
@@ -97,10 +112,12 @@ export class GitChangesProvider implements vscode.TreeDataProvider<CommitNode> {
97112 ) ,
98113 ]
99114 }
100- else if ( element . children ) {
101- return element . children as CommitNode [ ]
102- }
103115
104- return [ ]
116+ return element ?. children as CommitNode [ ] || [ ]
117+ }
118+
119+ // Getter for selectedCommitHash
120+ public getSelectedCommitHash ( ) : string | undefined {
121+ return this . selectedCommitHash
105122 }
106123}
0 commit comments