Skip to content

Commit 6d15b90

Browse files
committed
Fixes repo fallback on many commandsFixes #366 - deals null parameters from command callbacks
1 parent 1cc3d98 commit 6d15b90

37 files changed

+151
-116
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).
66

7+
## Unreleased
8+
### Fixed
9+
- Fixes many issues where commands wouldn't work if the active file wasn't part of an open repository — now GitLens will try to find the best repository otherwise it will open a repository quick pick menu if there is more than one
10+
- Fixes [#366](https://github.com/eamodio/vscode-gitlens/issues/366) - Running a GitLens command from a keybinding fails (more cases)
11+
712
## [8.3.1] - 2018-05-18
813
### Added
914
- Adds the ability to control where the *GitLens*, *GitLens History*, and *GitLens Results* explorers are shown 🎉 — closes [#213](https://github.com/eamodio/vscode-gitlens/issues/213), [#377](https://github.com/eamodio/vscode-gitlens/issues/377)

src/commands/clearFileAnnotations.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ export class ClearFileAnnotationsCommand extends EditorCommand {
1212
}
1313

1414
async execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri): Promise<any> {
15-
if (editor === undefined) return undefined;
15+
if (editor == null) return undefined;
1616

1717
// Handle the case where we are focused on a non-editor editor (output, debug console)
18-
if (uri !== undefined && !UriComparer.equals(uri, editor.document.uri)) {
18+
if (uri != null && !UriComparer.equals(uri, editor.document.uri)) {
1919
const e = window.visibleTextEditors.find(e => UriComparer.equals(uri, e.document.uri));
2020
if (e !== undefined) {
2121
editor = e;

src/commands/closeUnchangedFiles.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
'use strict';
22
import { commands, TextEditor, Uri, window } from 'vscode';
33
import { ActiveEditorTracker } from '../trackers/activeEditorTracker';
4-
import { ActiveEditorCommand, Commands, getCommandUri } from './common';
4+
import { ActiveEditorCommand, Commands, getCommandUri, getRepoPathOrActiveOrPrompt } from './common';
55
import { TextEditorComparer, UriComparer } from '../comparers';
6-
import { BuiltInCommands } from '../constants';
6+
import { BuiltInCommands, GlyphChars } from '../constants';
77
import { Container } from '../container';
88
import { Logger } from '../logger';
9-
import { Messages } from '../messages';
109

1110
export interface CloseUnchangedFilesCommandArgs {
1211
uris?: Uri[];
@@ -25,8 +24,8 @@ export class CloseUnchangedFilesCommand extends ActiveEditorCommand {
2524
if (args.uris === undefined) {
2625
args = { ...args };
2726

28-
const repoPath = await Container.git.getRepoPath(uri);
29-
if (!repoPath) return Messages.showNoRepositoryWarningMessage(`Unable to close unchanged files`);
27+
const repoPath = await getRepoPathOrActiveOrPrompt(uri, editor, `Close unchanged files in which repository${GlyphChars.Ellipsis}`);
28+
if (!repoPath) return undefined;
3029

3130
const status = await Container.git.getStatusForRepo(repoPath);
3231
if (status === undefined) return window.showWarningMessage(`Unable to close unchanged files`);
@@ -40,9 +39,9 @@ export class CloseUnchangedFilesCommand extends ActiveEditorCommand {
4039

4140
let count = 0;
4241
let previous = undefined;
43-
let editor = window.activeTextEditor;
42+
editor = window.activeTextEditor;
4443
while (true) {
45-
if (editor !== undefined) {
44+
if (editor != null) {
4645
if (TextEditorComparer.equals(previous, editor, { useId: true, usePosition: true })) {
4746
break;
4847
}
@@ -63,7 +62,7 @@ export class CloseUnchangedFilesCommand extends ActiveEditorCommand {
6362
previous = editor;
6463
editor = await editorTracker.awaitClose(500);
6564

66-
if (previous === undefined && editor === undefined) {
65+
if (previous === undefined && editor == null) {
6766
count++;
6867
// This is such a shitty hack, but I can't figure out any other reliable way to know that we've cycled through all the editors :(
6968
if (count >= 4) {

src/commands/common.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { GitBranch, GitCommit, GitRemote, GitUri } from '../gitService';
77
import { Logger } from '../logger';
88
// import { Telemetry } from '../telemetry';
99
import * as path from 'path';
10+
import { CommandQuickPickItem, RepositoriesQuickPick } from '../quickPicks/quickPicks';
1011

1112
export enum Commands {
1213
ClearFileAnnotations = 'gitlens.clearFileAnnotations',
@@ -77,6 +78,27 @@ export function getCommandUri(uri?: Uri, editor?: TextEditor): Uri | undefined {
7778
return document.uri;
7879
}
7980

81+
export async function getRepoPathOrActiveOrPrompt(uri: Uri | undefined, editor: TextEditor | undefined, placeholder: string, goBackCommand?: CommandQuickPickItem) {
82+
let repoPath = await Container.git.getRepoPathOrActive(uri, editor);
83+
if (!repoPath) {
84+
const pick = await RepositoriesQuickPick.show(placeholder, goBackCommand);
85+
if (pick instanceof CommandQuickPickItem) {
86+
await pick.execute();
87+
return undefined;
88+
}
89+
90+
if (pick === undefined) {
91+
if (goBackCommand !== undefined) {
92+
await goBackCommand.execute();
93+
}
94+
return undefined;
95+
}
96+
97+
repoPath = pick.repoPath;
98+
}
99+
return repoPath;
100+
}
101+
80102
export interface CommandContextParsingOptions {
81103
editor: boolean;
82104
uri: boolean;
@@ -138,7 +160,7 @@ function isScmResourceGroup(group: any): group is SourceControlResourceGroup {
138160
function isScmResourceState(state: any): state is SourceControlResourceState {
139161
if (state == null) return false;
140162

141-
return (state as SourceControlResourceState).resourceUri !== undefined;
163+
return (state as SourceControlResourceState).resourceUri != null;
142164
}
143165

144166
function isTextEditor(editor: any): editor is TextEditor {

src/commands/copyMessageToClipboard.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class CopyMessageToClipboardCommand extends ActiveEditorCommand {
3636
args = { ...args };
3737

3838
// If we don't have an editor then get the message of the last commit to the branch
39-
if (uri === undefined) {
39+
if (uri == null) {
4040
const repoPath = await Container.git.getActiveRepoPath(editor);
4141
if (!repoPath) return undefined;
4242

src/commands/copyShaToClipboard.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export class CopyShaToClipboardCommand extends ActiveEditorCommand {
3434
args = { ...args };
3535

3636
// If we don't have an editor then get the sha of the last commit to the branch
37-
if (uri === undefined) {
37+
if (uri == null) {
3838
const repoPath = await Container.git.getActiveRepoPath(editor);
3939
if (!repoPath) return undefined;
4040

src/commands/diffBranchWithBranch.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
'use strict';
22
import { CancellationTokenSource, TextEditor, Uri, window } from 'vscode';
3-
import { ActiveEditorCommand, CommandContext, Commands, getCommandUri } from './common';
3+
import { ActiveEditorCommand, CommandContext, Commands, getCommandUri, getRepoPathOrActiveOrPrompt } from './common';
44
import { GlyphChars } from '../constants';
55
import { Container } from '../container';
66
import { Logger } from '../logger';
7-
import { Messages } from '../messages';
87
import { BranchesAndTagsQuickPick, CommandQuickPickItem } from '../quickPicks/quickPicks';
98

109
export interface DiffBranchWithBranchCommandArgs {
@@ -40,8 +39,8 @@ export class DiffBranchWithBranchCommand extends ActiveEditorCommand {
4039
let progressCancellation: CancellationTokenSource | undefined;
4140

4241
try {
43-
const repoPath = await Container.git.getRepoPath(uri);
44-
if (!repoPath) return Messages.showNoRepositoryWarningMessage(`Unable to open branch compare`);
42+
const repoPath = await getRepoPathOrActiveOrPrompt(uri, editor, `Compare with branch or tag in which repository${GlyphChars.Ellipsis}`);
43+
if (!repoPath) return undefined;
4544

4645
if (!args.ref1) {
4746
let placeHolder;

src/commands/diffDirectory.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
'use strict';
22
import { CancellationTokenSource, commands, TextEditor, Uri, window } from 'vscode';
3-
import { ActiveEditorCommand, Commands, getCommandUri } from './common';
3+
import { ActiveEditorCommand, Commands, getCommandUri, getRepoPathOrActiveOrPrompt } from './common';
44
import { CommandContext, isCommandViewContextWithRef } from '../commands';
55
import { BuiltInCommands, GlyphChars } from '../constants';
66
import { Container } from '../container';
77
import { ComparisonResultsNode } from '../views/explorerNodes';
88
import { Logger } from '../logger';
9-
import { Messages } from '../messages';
109
import { BranchesAndTagsQuickPick, CommandQuickPickItem } from '../quickPicks/quickPicks';
1110

1211
export interface DiffDirectoryCommandArgs {
@@ -51,8 +50,8 @@ export class DiffDirectoryCommand extends ActiveEditorCommand {
5150
let progressCancellation: CancellationTokenSource | undefined;
5251

5352
try {
54-
const repoPath = await Container.git.getRepoPath(uri);
55-
if (!repoPath) return Messages.showNoRepositoryWarningMessage(`Unable to open directory compare`);
53+
const repoPath = await getRepoPathOrActiveOrPrompt(uri, editor, `Compare directory in which repository${GlyphChars.Ellipsis}`);
54+
if (!repoPath) return undefined;
5655

5756
if (!args.ref1) {
5857
args = { ...args };

src/commands/diffLineWithPrevious.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ export class DiffLineWithPreviousCommand extends ActiveEditorCommand {
2222

2323
async execute(editor?: TextEditor, uri?: Uri, args: DiffLineWithPreviousCommandArgs = {}): Promise<any> {
2424
uri = getCommandUri(uri, editor);
25-
if (uri === undefined) return undefined;
25+
if (uri == null) return undefined;
2626

2727
const gitUri = await GitUri.fromUri(uri);
2828

2929
args = { ...args };
3030
if (args.line === undefined) {
31-
args.line = editor === undefined ? 0 : editor.selection.active.line;
31+
args.line = editor == null ? 0 : editor.selection.active.line;
3232
}
3333

3434
if (args.commit === undefined || GitService.isUncommitted(args.commit.sha)) {

src/commands/diffLineWithWorking.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ export class DiffLineWithWorkingCommand extends ActiveEditorCommand {
2222

2323
async execute(editor?: TextEditor, uri?: Uri, args: DiffLineWithWorkingCommandArgs = {}): Promise<any> {
2424
uri = getCommandUri(uri, editor);
25-
if (uri === undefined) return undefined;
25+
if (uri == null) return undefined;
2626

2727
const gitUri = await GitUri.fromUri(uri);
2828

2929
args = { ...args };
3030
if (args.line === undefined) {
31-
args.line = editor === undefined ? 0 : editor.selection.active.line;
31+
args.line = editor == null ? 0 : editor.selection.active.line;
3232
}
3333

3434
if (args.commit === undefined || GitService.isUncommitted(args.commit.sha)) {

0 commit comments

Comments
 (0)