Skip to content

Commit

Permalink
git.enabled as a resource setting
Browse files Browse the repository at this point in the history
fixes #37492
  • Loading branch information
joaomoreno committed Nov 27, 2017
1 parent e69ca9f commit 9544e5f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 51 deletions.
1 change: 1 addition & 0 deletions extensions/git/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,7 @@
"properties": {
"git.enabled": {
"type": "boolean",
"scope": "resource",
"description": "%config.enabled%",
"default": true
},
Expand Down
8 changes: 0 additions & 8 deletions extensions/git/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ async function init(context: ExtensionContext, outputChannel: OutputChannel, dis
const telemetryReporter: TelemetryReporter = new TelemetryReporter(name, version, aiKey);
disposables.push(telemetryReporter);

const config = workspace.getConfiguration('git');
const enabled = config.get<boolean>('enabled') === true;
const pathHint = workspace.getConfiguration('git').get<string>('path');
const info = await findGit(pathHint, path => outputChannel.appendLine(localize('looking', "Looking for git in: {0}", path)));
const askpass = new Askpass();
Expand All @@ -37,12 +35,6 @@ async function init(context: ExtensionContext, outputChannel: OutputChannel, dis
model.onDidCloseRepository(onRepository, null, disposables);
onRepository();

if (!enabled) {
const commandCenter = new CommandCenter(git, model, outputChannel, telemetryReporter);
disposables.push(commandCenter);
return;
}

outputChannel.appendLine(localize('using git', "Using git {0} from {1}", info.version, info.path));

const onOutput = (str: string) => outputChannel.append(str);
Expand Down
74 changes: 31 additions & 43 deletions extensions/git/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

'use strict';

import { workspace, WorkspaceFoldersChangeEvent, Uri, window, Event, EventEmitter, QuickPickItem, Disposable, SourceControl, SourceControlResourceGroup, TextEditor, Memento } from 'vscode';
import { workspace, WorkspaceFoldersChangeEvent, Uri, window, Event, EventEmitter, QuickPickItem, Disposable, SourceControl, SourceControlResourceGroup, TextEditor, Memento, ConfigurationChangeEvent } from 'vscode';
import { Repository, RepositoryState } from './repository';
import { memoize, sequentialize, debounce } from './decorators';
import { dispose, anyEvent, filterEvent } from './util';
import { dispose, anyEvent, filterEvent, IDisposable } from './util';
import { Git, GitErrorCodes } from './git';
import * as path from 'path';
import * as fs from 'fs';
Expand Down Expand Up @@ -67,45 +67,17 @@ export class Model {

private possibleGitRepositoryPaths = new Set<string>();

private enabled = false;
private configurationChangeDisposable: Disposable;
private disposables: Disposable[] = [];

constructor(private git: Git, private globalState: Memento) {
const config = workspace.getConfiguration('git');
this.enabled = config.get<boolean>('enabled') === true;

this.configurationChangeDisposable = workspace.onDidChangeConfiguration(this.onDidChangeConfiguration, this);

if (this.enabled) {
this.enable();
}
}

private onDidChangeConfiguration(): void {
const config = workspace.getConfiguration('git');
const enabled = config.get<boolean>('enabled') === true;

if (enabled === this.enabled) {
return;
}

this.enabled = enabled;

if (enabled) {
this.enable();
} else {
this.disable();
}
}

private enable(): void {
workspace.onDidChangeWorkspaceFolders(this.onDidChangeWorkspaceFolders, this, this.disposables);
this.onDidChangeWorkspaceFolders({ added: workspace.workspaceFolders || [], removed: [] });

window.onDidChangeVisibleTextEditors(this.onDidChangeVisibleTextEditors, this, this.disposables);
this.onDidChangeVisibleTextEditors(window.visibleTextEditors);

workspace.onDidChangeConfiguration(this.onDidChangeConfiguration, this, this.disposables);

const fsWatcher = workspace.createFileSystemWatcher('**');
this.disposables.push(fsWatcher);

Expand All @@ -117,15 +89,6 @@ export class Model {
this.scanWorkspaceFolders();
}

private disable(): void {
const openRepositories = [...this.openRepositories];
openRepositories.forEach(r => r.dispose());
this.openRepositories = [];

this.possibleGitRepositoryPaths.clear();
this.disposables = dispose(this.disposables);
}

/**
* Scans the first level of each workspace folder, looking
* for git repositories.
Expand Down Expand Up @@ -175,6 +138,20 @@ export class Model {
openRepositoriesToDispose.forEach(r => r.dispose());
}

private onDidChangeConfiguration(): void {
const possibleRepositoryFolders = (workspace.workspaceFolders || [])
.filter(folder => workspace.getConfiguration('git', folder.uri).get<boolean>('enabled') === true)
.filter(folder => !this.getOpenRepository(folder.uri));

const openRepositoriesToDispose = this.openRepositories
.map(repository => ({ repository, root: Uri.file(repository.repository.root) }))
.filter(({ root }) => workspace.getConfiguration('git', root).get<boolean>('enabled') !== true)
.map(({ repository }) => repository);

possibleRepositoryFolders.forEach(p => this.tryOpenRepository(p.uri.fsPath));
openRepositoriesToDispose.forEach(r => r.dispose());
}

private onDidChangeVisibleTextEditors(editors: TextEditor[]): void {
editors.forEach(editor => {
const uri = editor.document.uri;
Expand All @@ -199,6 +176,13 @@ export class Model {
return;
}

const config = workspace.getConfiguration('git', Uri.file(path));
const enabled = config.get<boolean>('enabled') === true;

if (!enabled) {
return;
}

try {
const repositoryRoot = await this.git.getRepositoryRoot(path);

Expand Down Expand Up @@ -321,7 +305,11 @@ export class Model {
}

dispose(): void {
this.disable();
this.configurationChangeDisposable.dispose();
const openRepositories = [...this.openRepositories];
openRepositories.forEach(r => r.dispose());
this.openRepositories = [];

this.possibleGitRepositoryPaths.clear();
this.disposables = dispose(this.disposables);
}
}

0 comments on commit 9544e5f

Please sign in to comment.