Skip to content

Commit

Permalink
Fixes #503 - Open Changes (w difftool) should only open single file
Browse files Browse the repository at this point in the history
Changes Open All Changes (w difftool) to open files, rather than dirdiff
Changes Open All Changes (w difftool) to prompt for repo when needed
  • Loading branch information
eamodio committed Sep 21, 2018
1 parent 530bba4 commit 065bd0f
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 62 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p

### Fixed

- Fixes [#503](https://github.com/eamodio/vscode-gitlens/issues/503) - Open Changes (with difftool) opens one difftool window per changed file
- Fixes [#405](https://github.com/eamodio/vscode-gitlens/issues/405) - Secondary, blank repository appears repeatedly in gitExplorer view
- Fixes [#430](https://github.com/eamodio/vscode-gitlens/issues/430) - File revisions can end up being parsed by language servers (causing errors and warnings, etc)
- Fixes [#388](https://github.com/eamodio/vscode-gitlens/issues/388) - Support the mailmap feature of git
Expand Down
7 changes: 1 addition & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2285,7 +2285,7 @@
},
{
"command": "gitlens.externalDiff",
"when": "gitlens:enabled"
"when": "gitlens:activeFileStatus =~ /tracked/"
},
{
"command": "gitlens.externalDiffAll",
Expand Down Expand Up @@ -2878,11 +2878,6 @@
"when": "gitlens:enabled",
"group": "2_gitlens@3"
},
{
"command": "gitlens.externalDiffAll",
"when": "gitlens:enabled",
"group": "2_gitlens@4"
},
{
"command": "gitlens.stashSave",
"when": "gitlens:enabled",
Expand Down
25 changes: 25 additions & 0 deletions src/commands/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,31 @@ export async function getRepoPathOrActiveOrPrompt(
return repoPath;
}

export async function getRepoPathOrPrompt(
uri: Uri | undefined,
placeholder: string,
goBackCommand?: CommandQuickPickItem
) {
let repoPath = await Container.git.getRepoPath(uri);
if (!repoPath) {
const pick = await RepositoriesQuickPick.show(placeholder, goBackCommand);
if (pick instanceof CommandQuickPickItem) {
await pick.execute();
return undefined;
}

if (pick === undefined) {
if (goBackCommand !== undefined) {
await goBackCommand.execute();
}
return undefined;
}

repoPath = pick.repoPath;
}
return repoPath;
}

export interface CommandContextParsingOptions {
editor: boolean;
uri: boolean;
Expand Down
6 changes: 0 additions & 6 deletions src/commands/diffDirectory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,13 @@ export class DiffDirectoryCommand extends ActiveEditorCommand {
constructor() {
super([
Commands.DiffDirectory,
Commands.ExternalDiffAll,
Commands.ExplorersOpenDirectoryDiff,
Commands.ExplorersOpenDirectoryDiffWithWorking
]);
}

protected async preExecute(context: CommandContext, args: DiffDirectoryCommandArgs = {}): Promise<any> {
switch (context.command) {
case Commands.ExternalDiffAll:
args.ref1 = 'HEAD';
args.ref2 = undefined;
break;

case Commands.ExplorersOpenDirectoryDiff:
if (context.type === 'view' && context.node instanceof ResultsComparisonNode) {
args.ref1 = context.node.ref1.ref;
Expand Down
127 changes: 80 additions & 47 deletions src/commands/externalDiff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Container } from '../container';
import { Logger } from '../logger';
import { Messages } from '../messages';
import { Arrays } from '../system';
import { Command, CommandContext, Commands, getRepoPathOrActiveOrPrompt } from './common';
import { Command, CommandContext, Commands, getRepoPathOrPrompt } from './common';

enum Status {
INDEX_MODIFIED,
Expand Down Expand Up @@ -52,32 +52,61 @@ export interface ExternalDiffCommandArgs {

export class ExternalDiffCommand extends Command {
constructor() {
super(Commands.ExternalDiff);
super([Commands.ExternalDiff, Commands.ExternalDiffAll]);
}

protected async preExecute(context: CommandContext, args: ExternalDiffCommandArgs = {}): Promise<any> {
if (context.type === 'scm-states') {
args = { ...args };
args.files = context.scmResourceStates.map(
r => new ExternalDiffFile(r.resourceUri, (r as Resource).resourceGroupType === ResourceGroupType.Index)
);

return this.execute(args);
if (args.files === undefined) {
if (context.type === 'scm-states') {
args = { ...args };
args.files = context.scmResourceStates.map(
r =>
new ExternalDiffFile(
r.resourceUri,
(r as Resource).resourceGroupType === ResourceGroupType.Index
)
);
}
else if (context.type === 'scm-groups') {
args = { ...args };
args.files = Arrays.filterMap(
context.scmResourceGroups[0].resourceStates,
r =>
this.isModified(r)
? new ExternalDiffFile(
r.resourceUri,
(r as Resource).resourceGroupType === ResourceGroupType.Index
)
: undefined
);
}
}
else if (context.type === 'scm-groups') {
args = { ...args };
args.files = Arrays.filterMap(
context.scmResourceGroups[0].resourceStates,
r =>
this.isModified(r)
? new ExternalDiffFile(
r.resourceUri,
(r as Resource).resourceGroupType === ResourceGroupType.Index
)
: undefined
);

return this.execute(args);

if (context.command === Commands.ExternalDiffAll) {
if (args.files === undefined) {
const repoPath = await getRepoPathOrPrompt(
undefined,
`Open changes from which repository${GlyphChars.Ellipsis}`
);
if (!repoPath) return undefined;

const status = await Container.git.getStatusForRepo(repoPath);
if (status === undefined) {
return window.showInformationMessage("The repository doesn't have any changes");
}

args.files = [];

for (const file of status.files) {
if (file.indexStatus === 'M') {
args.files.push(new ExternalDiffFile(file.uri, true));
}

if (file.workingTreeStatus === 'M') {
args.files.push(new ExternalDiffFile(file.uri, false));
}
}
}
}

return this.execute(args);
Expand All @@ -90,17 +119,38 @@ export class ExternalDiffCommand extends Command {

async execute(args: ExternalDiffCommandArgs = {}) {
try {
const repoPath = await getRepoPathOrActiveOrPrompt(
undefined,
undefined,
`Open changes from which repository${GlyphChars.Ellipsis}`
);
if (!repoPath) return undefined;
let repoPath;
if (args.files === undefined) {
const editor = window.activeTextEditor;
if (editor === undefined) return undefined;

repoPath = await Container.git.getRepoPathOrActive(undefined, editor);
if (!repoPath) return undefined;

const uri = editor.document.uri;
const status = await Container.git.getStatusForFile(repoPath, uri.fsPath);
if (status === undefined) {
return window.showInformationMessage("The current file doesn't have any changes");
}

args.files = [];
if (status.indexStatus === 'M') {
args.files.push(new ExternalDiffFile(status.uri, true));
}

if (status.workingTreeStatus === 'M') {
args.files.push(new ExternalDiffFile(status.uri, false));
}
}
else {
repoPath = await Container.git.getRepoPath(args.files[0].uri.fsPath);
if (!repoPath) return undefined;
}

const tool = await Container.git.getDiffTool(repoPath);
if (tool === undefined) {
const result = await window.showWarningMessage(
`Unable to open changes in diff tool because there is no Git diff tool configured`,
`Unable to open changes in diff tool. No Git diff tool is configured`,
'View Git Docs'
);
if (!result) return undefined;
Expand All @@ -111,23 +161,6 @@ export class ExternalDiffCommand extends Command {
);
}

if (args.files === undefined) {
const status = await Container.git.getStatusForRepo(repoPath);
if (status === undefined) return window.showWarningMessage(`Unable to open changes in diff tool`);

args.files = [];

for (const file of status.files) {
if (file.indexStatus === 'M') {
args.files.push(new ExternalDiffFile(file.uri, true));
}

if (file.workingTreeStatus === 'M') {
args.files.push(new ExternalDiffFile(file.uri, false));
}
}
}

for (const file of args.files) {
void Container.git.openDiffTool(repoPath, file.uri, file.staged, tool);
}
Expand Down
6 changes: 3 additions & 3 deletions src/git/gitService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1491,7 +1491,7 @@ export class GitService implements Disposable {
filePathOrUri: string | Uri | undefined,
options: { ref?: string } = {}
): Promise<string | undefined> {
if (filePathOrUri == null) return await this.getActiveRepoPath();
if (filePathOrUri == null) return this.getHighlanderRepoPath();
if (filePathOrUri instanceof GitUri) return filePathOrUri.repoPath;

// Don't save the tracking info to the cache, because we could be looking in the wrong place (e.g. looking in the root when the file is in a submodule)
Expand Down Expand Up @@ -1541,10 +1541,10 @@ export class GitService implements Disposable {
}

async getRepoPathOrActive(uri: Uri | undefined, editor: TextEditor | undefined) {
const repoPath = await Container.git.getRepoPath(uri);
const repoPath = await this.getRepoPath(uri);
if (repoPath) return repoPath;

return Container.git.getActiveRepoPath(editor);
return this.getActiveRepoPath(editor);
}

async getRepositories(predicate?: (repo: Repository) => boolean): Promise<Iterable<Repository>> {
Expand Down

0 comments on commit 065bd0f

Please sign in to comment.