Sfoglia il codice sorgente

Adds more exception protection

main
Eric Amodio 7 anni fa
parent
commit
0021510de7
2 ha cambiato i file con 101 aggiunte e 68 eliminazioni
  1. +74
    -59
      src/git/gitContextTracker.ts
  2. +27
    -9
      src/gitService.ts

+ 74
- 59
src/git/gitContextTracker.ts Vedi File

@ -5,7 +5,7 @@ import { TextDocumentComparer } from '../comparers';
import { configuration } from '../configuration';
import { CommandContext, isTextEditor, setCommandContext } from '../constants';
import { GitChangeEvent, GitChangeReason, GitService, GitUri, Repository, RepositoryChangeEvent } from '../gitService';
// import { Logger } from '../logger';
import { Logger } from '../logger';
export enum BlameabilityChangeReason {
BlameFailed = 'blame-failed',
@ -146,85 +146,100 @@ export class GitContextTracker extends Disposable {
}
private async updateContext(reason: BlameabilityChangeReason, editor: TextEditor | undefined, force: boolean = false) {
let tracked: boolean;
if (force || this._context.editor !== editor) {
this._context.editor = editor;
this._context.repo = undefined;
if (this._context.repoDisposable !== undefined) {
this._context.repoDisposable.dispose();
this._context.repoDisposable = undefined;
}
try {
let tracked: boolean;
if (force || this._context.editor !== editor) {
this._context.editor = editor;
this._context.repo = undefined;
if (this._context.repoDisposable !== undefined) {
this._context.repoDisposable.dispose();
this._context.repoDisposable = undefined;
}
if (editor !== undefined) {
this._context.uri = await GitUri.fromUri(editor.document.uri, this.git);
if (editor !== undefined) {
this._context.uri = await GitUri.fromUri(editor.document.uri, this.git);
const repo = await this.git.getRepository(this._context.uri);
if (repo !== undefined) {
this._context.repo = repo;
this._context.repoDisposable = repo.onDidChange(this.onRepoChanged, this);
}
const repo = await this.git.getRepository(this._context.uri);
if (repo !== undefined) {
this._context.repo = repo;
this._context.repoDisposable = repo.onDidChange(this.onRepoChanged, this);
}
this._context.state.dirty = editor.document.isDirty;
tracked = await this.git.isTracked(this._context.uri);
this._context.state.dirty = editor.document.isDirty;
tracked = await this.git.isTracked(this._context.uri);
}
else {
this._context.uri = undefined;
this._context.state.dirty = false;
this._context.state.blameable = false;
tracked = false;
}
}
else {
this._context.uri = undefined;
this._context.state.dirty = false;
this._context.state.blameable = false;
tracked = false;
// Since the tracked state could have changed, update it
tracked = this._context.uri !== undefined
? await this.git.isTracked(this._context.uri!)
: false;
}
}
else {
// Since the tracked state could have changed, update it
tracked = this._context.uri !== undefined
? await this.git.isTracked(this._context.uri!)
: false;
}
if (this._context.state.tracked !== tracked) {
this._context.state.tracked = tracked;
setCommandContext(CommandContext.ActiveFileIsTracked, tracked);
}
if (this._context.state.tracked !== tracked) {
this._context.state.tracked = tracked;
setCommandContext(CommandContext.ActiveFileIsTracked, tracked);
}
this.updateBlameability(reason, undefined, force);
this.updateRemotes();
this.updateBlameability(reason, undefined, force);
this.updateRemotes();
}
catch (ex) {
Logger.error(ex, 'GitContextTracker.updateContext');
}
}
private updateBlameability(reason: BlameabilityChangeReason, blameable?: boolean, force: boolean = false) {
if (blameable === undefined) {
blameable = this._context.state.tracked && !this._context.state.dirty;
}
try {
if (blameable === undefined) {
blameable = this._context.state.tracked && !this._context.state.dirty;
}
if (!force && this._context.state.blameable === blameable) return;
if (!force && this._context.state.blameable === blameable) return;
this._context.state.blameable = blameable;
this._context.state.blameable = blameable;
setCommandContext(CommandContext.ActiveIsBlameable, blameable);
this._onDidChangeBlameability.fire({
blameable: blameable!,
editor: this._context && this._context.editor,
reason: reason
});
setCommandContext(CommandContext.ActiveIsBlameable, blameable);
this._onDidChangeBlameability.fire({
blameable: blameable!,
editor: this._context && this._context.editor,
reason: reason
});
}
catch (ex) {
Logger.error(ex, 'GitContextTracker.updateBlameability');
}
}
private async updateRemotes() {
let hasRemotes = false;
if (this._context.repo !== undefined) {
hasRemotes = await this._context.repo.hasRemote();
}
try {
let hasRemotes = false;
if (this._context.repo !== undefined) {
hasRemotes = await this._context.repo.hasRemote();
}
setCommandContext(CommandContext.ActiveHasRemote, hasRemotes);
setCommandContext(CommandContext.ActiveHasRemote, hasRemotes);
if (!hasRemotes) {
const repositories = await this.git.getRepositories();
for (const repo of repositories) {
if (repo === this._context.repo) continue;
if (!hasRemotes) {
const repositories = await this.git.getRepositories();
for (const repo of repositories) {
if (repo === this._context.repo) continue;
hasRemotes = await repo.hasRemotes();
if (hasRemotes) break;
hasRemotes = await repo.hasRemotes();
if (hasRemotes) break;
}
}
}
setCommandContext(CommandContext.HasRemotes, hasRemotes);
setCommandContext(CommandContext.HasRemotes, hasRemotes);
}
catch (ex) {
Logger.error(ex, 'GitContextTracker.updateRemotes');
}
}
}

+ 27
- 9
src/gitService.ts Vedi File

@ -1041,8 +1041,14 @@ export class GitService extends Disposable {
providerMap = providerMap || RemoteProviderFactory.createMap(configuration.get<IRemotesConfig[] | null | undefined>(configuration.name('remotes').value, null));
const data = await Git.remote(repoPath);
return GitRemoteParser.parse(data, repoPath, RemoteProviderFactory.factory(providerMap));
try {
const data = await Git.remote(repoPath);
return GitRemoteParser.parse(data, repoPath, RemoteProviderFactory.factory(providerMap));
}
catch (ex) {
Logger.error(ex, 'GitService.getRemotesCore');
return [];
}
}
async getRepoPath(filePath: string): Promise<string | undefined>;
@ -1081,8 +1087,14 @@ export class GitService extends Disposable {
return rp;
}
private getRepoPathCore(filePath: string, isDirectory: boolean): Promise<string | undefined> {
return Git.revparse_toplevel(isDirectory ? filePath : path.dirname(filePath));
private async getRepoPathCore(filePath: string, isDirectory: boolean): Promise<string | undefined> {
try {
return await Git.revparse_toplevel(isDirectory ? filePath : path.dirname(filePath));
}
catch (ex) {
Logger.error(ex, 'GitService.getRepoPathCore');
return undefined;
}
}
async getRepositories(): Promise<Iterable<Repository>> {
@ -1250,12 +1262,18 @@ export class GitService extends Disposable {
}
private async isTrackedCore(repoPath: string, fileName: string, sha?: string) {
// Even if we have a sha, check first to see if the file exists (that way the cache will be better reused)
let tracked = !!await Git.ls_files(repoPath === undefined ? '' : repoPath, fileName);
if (!tracked && sha !== undefined) {
tracked = !!await Git.ls_files(repoPath === undefined ? '' : repoPath, fileName, sha);
try {
// Even if we have a sha, check first to see if the file exists (that way the cache will be better reused)
let tracked = !!await Git.ls_files(repoPath === undefined ? '' : repoPath, fileName);
if (!tracked && sha !== undefined) {
tracked = !!await Git.ls_files(repoPath === undefined ? '' : repoPath, fileName, sha);
}
return tracked;
}
catch (ex) {
Logger.error(ex, 'GitService.isTrackedCore');
return false;
}
return tracked;
}
async getDiffTool(repoPath?: string) {

Caricamento…
Annulla
Salva