|
| 1 | +import * as vscode from 'vscode' |
| 2 | + |
| 3 | +export class GitChangeMonitor { |
| 4 | + private disposables: vscode.Disposable[] = [] |
| 5 | + private fileWatcher: vscode.FileSystemWatcher | undefined |
| 6 | + private retryCount = 0 |
| 7 | + |
| 8 | + constructor(private readonly onGitChange: () => void) { |
| 9 | + this.initialize() |
| 10 | + this.setupFileSystemWatcher() |
| 11 | + } |
| 12 | + |
| 13 | + private setupFileSystemWatcher() { |
| 14 | + try { |
| 15 | + this.fileWatcher = vscode.workspace.createFileSystemWatcher('**/.git/index') |
| 16 | + |
| 17 | + this.disposables.push( |
| 18 | + this.fileWatcher.onDidChange(() => { |
| 19 | + this.onGitChange() |
| 20 | + }), |
| 21 | + this.fileWatcher.onDidCreate(() => { |
| 22 | + this.onGitChange() |
| 23 | + }), |
| 24 | + this.fileWatcher.onDidDelete(() => { |
| 25 | + this.onGitChange() |
| 26 | + }), |
| 27 | + ) |
| 28 | + } |
| 29 | + catch (error) { |
| 30 | + console.error('Error setting up file watcher:', error) |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + async getGitExtension() { |
| 35 | + try { |
| 36 | + const extension = vscode.extensions.getExtension( |
| 37 | + 'vscode.git', |
| 38 | + ) |
| 39 | + |
| 40 | + if (extension !== undefined) { |
| 41 | + const gitExtension = extension.isActive |
| 42 | + ? extension.exports |
| 43 | + : await extension.activate() |
| 44 | + |
| 45 | + const api = gitExtension.getAPI(1) |
| 46 | + return api |
| 47 | + } |
| 48 | + } |
| 49 | + catch (err) { |
| 50 | + console.error('Error getting git extension:', err) |
| 51 | + throw new Error(`Git extension not found: ${err}`) |
| 52 | + } |
| 53 | + |
| 54 | + return undefined |
| 55 | + } |
| 56 | + |
| 57 | + private async initialize() { |
| 58 | + try { |
| 59 | + const git = await this.getGitExtension() |
| 60 | + |
| 61 | + if (!git && this.retryCount === 0) { |
| 62 | + console.error('Failed to get Git extension API, will retry after 5 seconds...') |
| 63 | + setTimeout(() => this.initialize(), 5000) |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + if (!git.repositories || git.repositories.length === 0) { |
| 68 | + this.disposables.push(git.onDidOpenRepository((repo: any) => { |
| 69 | + this.setupRepository(repo) |
| 70 | + })) |
| 71 | + } |
| 72 | + else { |
| 73 | + git.repositories.forEach((repo: any) => this.setupRepository(repo)) |
| 74 | + } |
| 75 | + } |
| 76 | + catch (error) { |
| 77 | + console.error('Error in initialize:', error) |
| 78 | + // 如果出错,5秒后重试 |
| 79 | + setTimeout(() => this.initialize(), 5000) |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private setupRepository(repo: any) { |
| 84 | + try { |
| 85 | + this.disposables.push(repo.state.onDidChange(() => { |
| 86 | + this.onGitChange() |
| 87 | + })) |
| 88 | + } |
| 89 | + catch (error) { |
| 90 | + console.error('Error setting up repository:', error) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + dispose() { |
| 95 | + this.disposables.forEach(d => d.dispose()) |
| 96 | + this.disposables = [] |
| 97 | + if (this.fileWatcher) { |
| 98 | + this.fileWatcher.dispose() |
| 99 | + this.fileWatcher = undefined |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments