Skip to content

Commit

Permalink
Closes #760 - adds open * commands to result files
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed Jul 19, 2019
1 parent 3d42a74 commit 3e98e24
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 70 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p

## [Unreleased]

### Added

- Adds _Open All Changes_, _Open All Changes with Working Tree_, _Open Files_, and _Open Revisions_ commands to files changed nodes in the views — closes [#760](https://github.com/eamodio/vscode-gitlens/issues/760)

### Fixed

- Fixes [#709](https://github.com/eamodio/vscode-gitlens/issues/709) - Checkout a remote branch as new local branch
Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4411,22 +4411,22 @@
},
{
"command": "gitlens.views.openChangedFileChanges",
"when": "viewItem =~ /gitlens:(commit|stash)\\b/",
"when": "viewItem =~ /gitlens:(commit|stash|results:files)\\b/",
"group": "2_gitlens@1"
},
{
"command": "gitlens.views.openChangedFileChangesWithWorking",
"when": "viewItem =~ /gitlens:(commit|stash)\\b/",
"when": "viewItem =~ /gitlens:(commit|stash|results:files)\\b/",
"group": "2_gitlens@2"
},
{
"command": "gitlens.views.openChangedFiles",
"when": "viewItem =~ /gitlens:(commit|stash)\\b/",
"when": "viewItem =~ /gitlens:(commit|stash|results:files)\\b/",
"group": "3_gitlens@1"
},
{
"command": "gitlens.views.openChangedFileRevisions",
"when": "viewItem =~ /gitlens:(commit|stash)\\b/",
"when": "viewItem =~ /gitlens:(commit|stash|results:files)\\b/",
"group": "3_gitlens@2"
},
{
Expand Down Expand Up @@ -4549,7 +4549,7 @@
},
{
"command": "gitlens.views.openFileRevision",
"when": "viewItem =~ /gitlens:file\\b(?=.*?\\b\\+committed\\b)/",
"when": "viewItem =~ /gitlens:file\\b((?=.*?\\b\\+committed\\b)|:results)/",
"group": "3_gitlens@2"
},
{
Expand Down Expand Up @@ -4580,12 +4580,12 @@
},
{
"command": "gitlens.views.applyChanges",
"when": "viewItem =~ /gitlens:file\\b((?=.*?\\b\\+committed\\b)|results\\b)/",
"when": "viewItem =~ /gitlens:file\\b((?=.*?\\b\\+committed\\b)|:results\\b)/",
"group": "5_gitlens_1@1"
},
{
"command": "gitlens.views.checkout",
"when": "viewItem =~ /gitlens:file\\b((?=.*?\\b\\+committed\\b)|results\\b)/",
"when": "viewItem =~ /gitlens:file\\b((?=.*?\\b\\+committed\\b)|:results\\b)/",
"group": "5_gitlens_1@2"
},
{
Expand Down Expand Up @@ -4660,7 +4660,7 @@
},
{
"command": "gitlens.views.exploreRepoRevision",
"when": "viewItem =~ /gitlens:(branch|commit|file\\b((?=.*?\\b\\+committed\\b)|results)|stash|tag)\\b/",
"when": "viewItem =~ /gitlens:(branch|commit|file\\b((?=.*?\\b\\+committed\\b)|:results)|stash|tag)\\b/",
"group": "7_gitlens_more@1"
},
{
Expand Down
2 changes: 1 addition & 1 deletion src/commands/diffWithWorking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class DiffWithWorkingCommand extends ActiveEditorCommand {
firstIfNotFound: true
});
if (args.commit === undefined) {
return Messages.showFileNotUnderSourceControlWarningMessage('Unable to open compare');
return window.showWarningMessage('Unable to open compare. File doesn\'t exist in the specified revision');
}
}
catch (ex) {
Expand Down
10 changes: 5 additions & 5 deletions src/views/nodes/resultsFilesNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export class ResultsFilesNode extends ViewNode<ViewWithFiles> {
view: ViewWithFiles,
parent: ViewNode,
public readonly repoPath: string,
private readonly _ref1: string,
private readonly _ref2: string
public readonly ref1: string,
public readonly ref2: string
) {
super(GitUri.fromRepoPath(repoPath), view, parent);
}
Expand All @@ -35,7 +35,7 @@ export class ResultsFilesNode extends ViewNode<ViewWithFiles> {
if (diff === undefined) return [];

let children: FileNode[] = [
...Iterables.map(diff, s => new ResultsFileNode(this.view, this, this.repoPath, s, this._ref1, this._ref2))
...Iterables.map(diff, s => new ResultsFileNode(this.view, this, this.repoPath, s, this.ref1, this.ref2))
];

if (this.view.config.files.layout !== ViewFilesLayout.List) {
Expand Down Expand Up @@ -97,7 +97,7 @@ export class ResultsFilesNode extends ViewNode<ViewWithFiles> {

private _filesQueryResults: Promise<FilesQueryResults> | undefined;

private getFilesQueryResults() {
getFilesQueryResults() {
if (this._filesQueryResults === undefined) {
this._filesQueryResults = this.getFilesQueryResultsCore();
}
Expand All @@ -106,7 +106,7 @@ export class ResultsFilesNode extends ViewNode<ViewWithFiles> {
}

private async getFilesQueryResultsCore(): Promise<FilesQueryResults> {
const diff = await Container.git.getDiffStatus(this.uri.repoPath!, this._ref1, this._ref2);
const diff = await Container.git.getDiffStatus(this.uri.repoPath!, this.ref1, this.ref2);

return {
label: `${Strings.pluralize('file', diff !== undefined ? diff.length : 0, { zero: 'No' })} changed`,
Expand Down
212 changes: 156 additions & 56 deletions src/views/viewCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import { BuiltInCommands, CommandContext, setCommandContext } from '../constants
import { Container } from '../container';
import { toGitLensFSUri } from '../git/fsProvider';
import { GitService, GitUri } from '../git/gitService';
import { Arrays } from '../system';
import {
BranchNode,
BranchTrackingStatusNode,
Expand All @@ -34,6 +33,7 @@ import {
RemoteNode,
RepositoryNode,
ResultsFileNode,
ResultsFilesNode,
StashFileNode,
StashNode,
StatusFileNode,
Expand Down Expand Up @@ -384,7 +384,7 @@ export class ViewCommands {
return commands.executeCommand(command.command, uri, args);
}

private async openChangesWithWorking(node: ViewRefFileNode | StatusFileNode) {
private openChangesWithWorking(node: ViewRefFileNode | StatusFileNode) {
if (!(node instanceof ViewRefFileNode) && !(node instanceof StatusFileNode)) return undefined;

const args: DiffWithWorkingCommandArgs = {
Expand All @@ -393,15 +393,6 @@ export class ViewCommands {
preview: false
}
};

if (node instanceof ResultsFileNode) {
args.commit = await Container.git.getCommitForFile(node.repoPath, node.uri.fsPath, {
ref: node.uri.sha,
firstIfNotFound: true,
reverse: true
});
}

return commands.executeCommand(Commands.DiffWithWorking, node.uri, args);
}

Expand Down Expand Up @@ -472,60 +463,142 @@ export class ViewCommands {
}

private async openChangedFileChanges(
node: CommitNode | StashNode,
node: CommitNode | StashNode | ResultsFilesNode,
options: TextDocumentShowOptions = { preserveFocus: false, preview: false }
) {
if (!(node instanceof CommitNode) && !(node instanceof StashNode)) return;

const repoPath = node.commit.repoPath;
const uris = node.commit.files.map(s => GitUri.fromFile(s, repoPath));

for (const uri of uris) {
await this.openDiffWith(
repoPath,
{
uri: uri,
sha:
node.commit.previousSha !== undefined ? node.commit.previousSha : GitService.deletedOrMissingSha
},
{ uri: uri, sha: node.commit.sha },
options
if (!(node instanceof CommitNode) && !(node instanceof StashNode) && !(node instanceof ResultsFilesNode)) {
return;
}

let repoPath: string;
let files;
let ref1: string;
let ref2: string;

if (node instanceof ResultsFilesNode) {
const { diff } = await node.getFilesQueryResults();
if (diff == null || diff.length === 0) return;

repoPath = node.repoPath;
files = diff;
ref1 = node.ref1;
ref2 = node.ref2;
}
else {
repoPath = node.commit.repoPath;
files = node.commit.files;
ref1 = node.commit.previousSha !== undefined ? node.commit.previousSha : GitService.deletedOrMissingSha;
ref2 = node.commit.sha;
}

if (files.length > 20) {
const result = await window.showWarningMessage(
`Are your sure you want to open all ${files.length} files?`,
{ title: 'Yes' },
{ title: 'No', isCloseAffordance: true }
);
if (result === undefined || result.title === 'No') return;
}

for (const file of files) {
if (file.status === 'A') continue;

const uri1 = GitUri.fromFile(file, repoPath);
const uri2 = file.status === 'R' ? GitUri.fromFile(file, repoPath, ref2, true) : uri1;

await this.openDiffWith(repoPath, { uri: uri1, sha: ref1 }, { uri: uri2, sha: ref2 }, options);
}
}

private async openChangedFileChangesWithWorking(
node: CommitNode | StashNode,
node: CommitNode | StashNode | ResultsFilesNode,
options: TextDocumentShowOptions = { preserveFocus: false, preview: false }
) {
if (!(node instanceof CommitNode) && !(node instanceof StashNode)) return;
if (!(node instanceof CommitNode) && !(node instanceof StashNode) && !(node instanceof ResultsFilesNode)) {
return;
}

const repoPath = node.commit.repoPath;
const uris = Arrays.filterMap(node.commit.files, f =>
f.status !== 'D' ? GitUri.fromFile(f, repoPath) : undefined
);
let repoPath: string;
let files;
let ref: string;

for (const uri of uris) {
const workingUri = await Container.git.getWorkingUri(repoPath, uri);
await this.openDiffWith(
repoPath,
{ uri: uri, sha: node.commit.sha },
{ uri: workingUri || uri, sha: '' },
options
if (node instanceof ResultsFilesNode) {
const { diff } = await node.getFilesQueryResults();
if (diff == null || diff.length === 0) return;

repoPath = node.repoPath;
files = diff;
ref = node.ref1 || node.ref2;
}
else {
repoPath = node.commit.repoPath;
files = node.commit.files;
ref = node.commit.sha;
}

if (files.length > 20) {
const result = await window.showWarningMessage(
`Are your sure you want to open all ${files.length} files?`,
{ title: 'Yes' },
{ title: 'No', isCloseAffordance: true }
);
if (result === undefined || result.title === 'No') return;
}

for (const file of files) {
if (file.status === 'A' || file.status === 'D') continue;

const args: DiffWithWorkingCommandArgs = {
showOptions: options
};

if (!(node instanceof ResultsFilesNode)) {
args.commit = node.commit.toFileCommit(file);
}

const uri = GitUri.fromFile(file, repoPath, ref);
await commands.executeCommand(Commands.DiffWithWorking, uri, args);
}
}

private async openChangedFiles(
node: CommitNode | StashNode,
node: CommitNode | StashNode | ResultsFilesNode,
options: TextDocumentShowOptions = { preserveFocus: false, preview: false }
) {
if (!(node instanceof CommitNode) && !(node instanceof StashNode)) return;
if (!(node instanceof CommitNode) && !(node instanceof StashNode) && !(node instanceof ResultsFilesNode)) {
return;
}

let repoPath: string;
let files;
let ref: string;

if (node instanceof ResultsFilesNode) {
const { diff } = await node.getFilesQueryResults();
if (diff == null || diff.length === 0) return;

repoPath = node.repoPath;
files = diff;
ref = node.ref1 || node.ref2;
}
else {
repoPath = node.commit.repoPath;
files = node.commit.files;
ref = node.commit.sha;
}

if (files.length > 20) {
const result = await window.showWarningMessage(
`Are your sure you want to open all ${files.length} files?`,
{ title: 'Yes' },
{ title: 'No', isCloseAffordance: true }
);
if (result === undefined || result.title === 'No') return;
}

const repoPath = node.commit.repoPath;
const uris = Arrays.filterMap(node.commit.files, f => GitUri.fromFile(f, repoPath, node.commit.sha));
for (const file of files) {
const uri = GitUri.fromFile(file, repoPath, ref);

for (const uri of uris) {
const args: OpenWorkingFileCommandArgs = {
uri: uri,
showOptions: options
Expand All @@ -535,19 +608,46 @@ export class ViewCommands {
}

private async openChangedFileRevisions(
node: CommitNode | StashNode,
node: CommitNode | StashNode | ResultsFilesNode,
options: TextDocumentShowOptions = { preserveFocus: false, preview: false }
) {
if (!(node instanceof CommitNode) && !(node instanceof StashNode)) return;

const uris = Arrays.filterMap(node.commit.files, f =>
GitUri.toRevisionUri(
f.status === 'D' ? node.commit.previousFileSha : node.commit.sha,
f,
node.commit.repoPath
)
);
for (const uri of uris) {
if (!(node instanceof CommitNode) && !(node instanceof StashNode) && !(node instanceof ResultsFilesNode)) {
return;
}

let repoPath: string;
let files;
let ref1: string;
let ref2: string;

if (node instanceof ResultsFilesNode) {
const { diff } = await node.getFilesQueryResults();
if (diff == null || diff.length === 0) return;

repoPath = node.repoPath;
files = diff;
ref1 = node.ref1;
ref2 = node.ref2;
}
else {
repoPath = node.commit.repoPath;
files = node.commit.files;
ref1 = node.commit.sha;
ref2 = node.commit.previousFileSha;
}

if (files.length > 20) {
const result = await window.showWarningMessage(
`Are your sure you want to open all ${files.length} files?`,
{ title: 'Yes' },
{ title: 'No', isCloseAffordance: true }
);
if (result === undefined || result.title === 'No') return;
}

for (const file of files) {
const uri = GitUri.toRevisionUri(file.status === 'D' ? ref2 : ref1, file, repoPath);

await openEditor(uri, options);
}
}
Expand Down

0 comments on commit 3e98e24

Please sign in to comment.