Skip to content

Commit 0021510

Browse files
committed
Adds more exception protection
1 parent d0073e2 commit 0021510

File tree

2 files changed

+101
-68
lines changed

2 files changed

+101
-68
lines changed

src/git/gitContextTracker.ts

Lines changed: 74 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { TextDocumentComparer } from '../comparers';
55
import { configuration } from '../configuration';
66
import { CommandContext, isTextEditor, setCommandContext } from '../constants';
77
import { GitChangeEvent, GitChangeReason, GitService, GitUri, Repository, RepositoryChangeEvent } from '../gitService';
8-
// import { Logger } from '../logger';
8+
import { Logger } from '../logger';
99

1010
export 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
}

src/gitService.ts

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,8 +1041,14 @@ export class GitService extends Disposable {
10411041

10421042
providerMap = providerMap || RemoteProviderFactory.createMap(configuration.get<IRemotesConfig[] | null | undefined>(configuration.name('remotes').value, null));
10431043

1044-
const data = await Git.remote(repoPath);
1045-
return GitRemoteParser.parse(data, repoPath, RemoteProviderFactory.factory(providerMap));
1044+
try {
1045+
const data = await Git.remote(repoPath);
1046+
return GitRemoteParser.parse(data, repoPath, RemoteProviderFactory.factory(providerMap));
1047+
}
1048+
catch (ex) {
1049+
Logger.error(ex, 'GitService.getRemotesCore');
1050+
return [];
1051+
}
10461052
}
10471053

10481054
async getRepoPath(filePath: string): Promise<string | undefined>;
@@ -1081,8 +1087,14 @@ export class GitService extends Disposable {
10811087
return rp;
10821088
}
10831089

1084-
private getRepoPathCore(filePath: string, isDirectory: boolean): Promise<string | undefined> {
1085-
return Git.revparse_toplevel(isDirectory ? filePath : path.dirname(filePath));
1090+
private async getRepoPathCore(filePath: string, isDirectory: boolean): Promise<string | undefined> {
1091+
try {
1092+
return await Git.revparse_toplevel(isDirectory ? filePath : path.dirname(filePath));
1093+
}
1094+
catch (ex) {
1095+
Logger.error(ex, 'GitService.getRepoPathCore');
1096+
return undefined;
1097+
}
10861098
}
10871099

10881100
async getRepositories(): Promise<Iterable<Repository>> {
@@ -1250,12 +1262,18 @@ export class GitService extends Disposable {
12501262
}
12511263

12521264
private async isTrackedCore(repoPath: string, fileName: string, sha?: string) {
1253-
// Even if we have a sha, check first to see if the file exists (that way the cache will be better reused)
1254-
let tracked = !!await Git.ls_files(repoPath === undefined ? '' : repoPath, fileName);
1255-
if (!tracked && sha !== undefined) {
1256-
tracked = !!await Git.ls_files(repoPath === undefined ? '' : repoPath, fileName, sha);
1265+
try {
1266+
// Even if we have a sha, check first to see if the file exists (that way the cache will be better reused)
1267+
let tracked = !!await Git.ls_files(repoPath === undefined ? '' : repoPath, fileName);
1268+
if (!tracked && sha !== undefined) {
1269+
tracked = !!await Git.ls_files(repoPath === undefined ? '' : repoPath, fileName, sha);
1270+
}
1271+
return tracked;
1272+
}
1273+
catch (ex) {
1274+
Logger.error(ex, 'GitService.isTrackedCore');
1275+
return false;
12571276
}
1258-
return tracked;
12591277
}
12601278

12611279
async getDiffTool(repoPath?: string) {

0 commit comments

Comments
 (0)