Skip to content

Commit

Permalink
#91 New setting to enable Git Graph to open to the repository contain…
Browse files Browse the repository at this point in the history
…ing the active Text Editor document.
  • Loading branch information
mhutchie committed Jun 2, 2019
1 parent cf37049 commit 7dbec5c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@
"default": 0,
"description": "Specifies the maximum depth of subfolders to search when discovering repositories in the workspace."
},
"git-graph.openDiffTabLocation":{
"git-graph.openDiffTabLocation": {
"type": "string",
"enum": [
"Active",
Expand All @@ -227,6 +227,11 @@
"default": "Active",
"description": "Specifies which pane the Visual Studio Code Diff is opened in."
},
"git-graph.openToTheRepoOfTheActiveTextEditorDocument": {
"type": "boolean",
"default": false,
"description": "Open Git Graph to the repository containing the active Text Editor document."
},
"git-graph.referenceLabelAlignment": {
"type": "string",
"enum": [
Expand Down
4 changes: 4 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ class Config {
return this.workspaceConfiguration.get('openDiffTabLocation', 'Active') === 'Active' ? vscode.ViewColumn.Active : vscode.ViewColumn.Beside;
}

public openToTheRepoOfTheActiveTextEditorDocument() {
return this.workspaceConfiguration.get('openToTheRepoOfTheActiveTextEditorDocument', false);
}

public refLabelAlignment(): RefLabelAlignment {
return this.workspaceConfiguration.get('referenceLabelAlignment', 'Normal');
}
Expand Down
26 changes: 18 additions & 8 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as vscode from 'vscode';
import { AvatarManager } from './avatarManager';
import { getConfig } from './config';
import { DataSource } from './dataSource';
import { DiffDocProvider } from './diffDocProvider';
import { ExtensionState } from './extensionState';
Expand All @@ -17,15 +18,24 @@ export function activate(context: vscode.ExtensionContext) {

context.subscriptions.push(
vscode.commands.registerCommand('git-graph.view', args => {
let loadRepo = typeof args === 'object' && args.rootUri ? getPathFromUri(args.rootUri) : null;
if (loadRepo !== null && !repoManager.isKnownRepo(loadRepo)) {
repoManager.registerRepo(loadRepo, true).then(valid => {
if (!valid) loadRepo = null;
GitGraphView.createOrShow(context.extensionPath, dataSource, extensionState, avatarManager, repoManager, loadRepo);
});
} else {
GitGraphView.createOrShow(context.extensionPath, dataSource, extensionState, avatarManager, repoManager, loadRepo);
let loadRepo: string | null = null;

if (typeof args === 'object' && args.rootUri) {
// If command is run from the SCP menu, load the specific repo
loadRepo = getPathFromUri(args.rootUri);
if (!repoManager.isKnownRepo(loadRepo)) {
repoManager.registerRepo(loadRepo, true).then(valid => {
if (!valid) loadRepo = null;
GitGraphView.createOrShow(context.extensionPath, dataSource, extensionState, avatarManager, repoManager, loadRepo);
});
return;
}
} else if (getConfig().openToTheRepoOfTheActiveTextEditorDocument() && vscode.window.activeTextEditor) {
// If the config setting is enabled, load the repo containing the active text editor document
loadRepo = repoManager.getRepoContainingFile(getPathFromUri(vscode.window.activeTextEditor.document.uri));
}

GitGraphView.createOrShow(context.extensionPath, dataSource, extensionState, avatarManager, repoManager, loadRepo);
}),
vscode.commands.registerCommand('git-graph.addGitRepository', () => {
vscode.window.showOpenDialog({ canSelectFiles: false, canSelectFolders: true, canSelectMany: false }).then(uris => {
Expand Down
7 changes: 7 additions & 0 deletions src/repoManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ export class RepoManager {
}
return repos;
}
public getRepoContainingFile(path: string) {
let repoPaths = Object.keys(this.repos), repo = null;
for (let i = 0; i < repoPaths.length; i++) {
if (path.startsWith(repoPaths[i] + '/') && (repo === null || repo.length < repoPaths[i].length)) repo = repoPaths[i];
}
return repo;
}
public isKnownRepo(repo: string) {
return typeof this.repos[repo] !== 'undefined';
}
Expand Down

0 comments on commit 7dbec5c

Please sign in to comment.