@@ -5,7 +5,7 @@ import { TextDocumentComparer } from '../comparers';
55import { configuration } from '../configuration' ;
66import { CommandContext , isTextEditor , setCommandContext } from '../constants' ;
77import { GitChangeEvent , GitChangeReason , GitService , GitUri , Repository , RepositoryChangeEvent } from '../gitService' ;
8- // import { Logger } from '../logger';
8+ import { Logger } from '../logger' ;
99
1010export enum BlameabilityChangeReason {
1111 BlameFailed = 'blame-failed' ,
@@ -146,85 +146,100 @@ export class GitContextTracker extends Disposable {
146146 }
147147
148148 private async updateContext ( reason : BlameabilityChangeReason , editor : TextEditor | undefined , force : boolean = false ) {
149- let tracked : boolean ;
150- if ( force || this . _context . editor !== editor ) {
151- this . _context . editor = editor ;
152- this . _context . repo = undefined ;
153- if ( this . _context . repoDisposable !== undefined ) {
154- this . _context . repoDisposable . dispose ( ) ;
155- this . _context . repoDisposable = undefined ;
156- }
149+ try {
150+ let tracked : boolean ;
151+ if ( force || this . _context . editor !== editor ) {
152+ this . _context . editor = editor ;
153+ this . _context . repo = undefined ;
154+ if ( this . _context . repoDisposable !== undefined ) {
155+ this . _context . repoDisposable . dispose ( ) ;
156+ this . _context . repoDisposable = undefined ;
157+ }
157158
158- if ( editor !== undefined ) {
159- this . _context . uri = await GitUri . fromUri ( editor . document . uri , this . git ) ;
159+ if ( editor !== undefined ) {
160+ this . _context . uri = await GitUri . fromUri ( editor . document . uri , this . git ) ;
160161
161- const repo = await this . git . getRepository ( this . _context . uri ) ;
162- if ( repo !== undefined ) {
163- this . _context . repo = repo ;
164- this . _context . repoDisposable = repo . onDidChange ( this . onRepoChanged , this ) ;
165- }
162+ const repo = await this . git . getRepository ( this . _context . uri ) ;
163+ if ( repo !== undefined ) {
164+ this . _context . repo = repo ;
165+ this . _context . repoDisposable = repo . onDidChange ( this . onRepoChanged , this ) ;
166+ }
166167
167- this . _context . state . dirty = editor . document . isDirty ;
168- tracked = await this . git . isTracked ( this . _context . uri ) ;
168+ this . _context . state . dirty = editor . document . isDirty ;
169+ tracked = await this . git . isTracked ( this . _context . uri ) ;
170+ }
171+ else {
172+ this . _context . uri = undefined ;
173+ this . _context . state . dirty = false ;
174+ this . _context . state . blameable = false ;
175+ tracked = false ;
176+ }
169177 }
170178 else {
171- this . _context . uri = undefined ;
172- this . _context . state . dirty = false ;
173- this . _context . state . blameable = false ;
174- tracked = false ;
179+ // Since the tracked state could have changed, update it
180+ tracked = this . _context . uri !== undefined
181+ ? await this . git . isTracked ( this . _context . uri ! )
182+ : false ;
175183 }
176- }
177- else {
178- // Since the tracked state could have changed, update it
179- tracked = this . _context . uri !== undefined
180- ? await this . git . isTracked ( this . _context . uri ! )
181- : false ;
182- }
183184
184- if ( this . _context . state . tracked !== tracked ) {
185- this . _context . state . tracked = tracked ;
186- setCommandContext ( CommandContext . ActiveFileIsTracked , tracked ) ;
187- }
185+ if ( this . _context . state . tracked !== tracked ) {
186+ this . _context . state . tracked = tracked ;
187+ setCommandContext ( CommandContext . ActiveFileIsTracked , tracked ) ;
188+ }
188189
189- this . updateBlameability ( reason , undefined , force ) ;
190- this . updateRemotes ( ) ;
190+ this . updateBlameability ( reason , undefined , force ) ;
191+ this . updateRemotes ( ) ;
192+ }
193+ catch ( ex ) {
194+ Logger . error ( ex , 'GitContextTracker.updateContext' ) ;
195+ }
191196 }
192197
193198 private updateBlameability ( reason : BlameabilityChangeReason , blameable ?: boolean , force : boolean = false ) {
194- if ( blameable === undefined ) {
195- blameable = this . _context . state . tracked && ! this . _context . state . dirty ;
196- }
199+ try {
200+ if ( blameable === undefined ) {
201+ blameable = this . _context . state . tracked && ! this . _context . state . dirty ;
202+ }
197203
198- if ( ! force && this . _context . state . blameable === blameable ) return ;
204+ if ( ! force && this . _context . state . blameable === blameable ) return ;
199205
200- this . _context . state . blameable = blameable ;
206+ this . _context . state . blameable = blameable ;
201207
202- setCommandContext ( CommandContext . ActiveIsBlameable , blameable ) ;
203- this . _onDidChangeBlameability . fire ( {
204- blameable : blameable ! ,
205- editor : this . _context && this . _context . editor ,
206- reason : reason
207- } ) ;
208+ setCommandContext ( CommandContext . ActiveIsBlameable , blameable ) ;
209+ this . _onDidChangeBlameability . fire ( {
210+ blameable : blameable ! ,
211+ editor : this . _context && this . _context . editor ,
212+ reason : reason
213+ } ) ;
214+ }
215+ catch ( ex ) {
216+ Logger . error ( ex , 'GitContextTracker.updateBlameability' ) ;
217+ }
208218 }
209219
210220 private async updateRemotes ( ) {
211- let hasRemotes = false ;
212- if ( this . _context . repo !== undefined ) {
213- hasRemotes = await this . _context . repo . hasRemote ( ) ;
214- }
221+ try {
222+ let hasRemotes = false ;
223+ if ( this . _context . repo !== undefined ) {
224+ hasRemotes = await this . _context . repo . hasRemote ( ) ;
225+ }
215226
216- setCommandContext ( CommandContext . ActiveHasRemote , hasRemotes ) ;
227+ setCommandContext ( CommandContext . ActiveHasRemote , hasRemotes ) ;
217228
218- if ( ! hasRemotes ) {
219- const repositories = await this . git . getRepositories ( ) ;
220- for ( const repo of repositories ) {
221- if ( repo === this . _context . repo ) continue ;
229+ if ( ! hasRemotes ) {
230+ const repositories = await this . git . getRepositories ( ) ;
231+ for ( const repo of repositories ) {
232+ if ( repo === this . _context . repo ) continue ;
222233
223- hasRemotes = await repo . hasRemotes ( ) ;
224- if ( hasRemotes ) break ;
234+ hasRemotes = await repo . hasRemotes ( ) ;
235+ if ( hasRemotes ) break ;
236+ }
225237 }
226- }
227238
228- setCommandContext ( CommandContext . HasRemotes , hasRemotes ) ;
239+ setCommandContext ( CommandContext . HasRemotes , hasRemotes ) ;
240+ }
241+ catch ( ex ) {
242+ Logger . error ( ex , 'GitContextTracker.updateRemotes' ) ;
243+ }
229244 }
230245}
0 commit comments