Skip to content

Commit

Permalink
Fixes issues with open changes with working on results file nodes
Browse files Browse the repository at this point in the history
Fixes issues with open revision on results file nodes
  • Loading branch information
eamodio committed Sep 14, 2018
1 parent f13d354 commit c439ea0
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 64 deletions.
18 changes: 14 additions & 4 deletions src/git/gitService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1124,11 +1124,19 @@ export class GitService implements Disposable {
async getLogCommitForFile(
repoPath: string | undefined,
fileName: string,
options: { ref?: string; firstIfNotFound?: boolean } = {}
options: { ref?: string; firstIfNotFound?: boolean; reverse?: boolean } = {}
): Promise<GitLogCommit | undefined> {
Logger.log(`getFileLogCommit('${repoPath}', '${fileName}', '${options.ref}', ${options.firstIfNotFound})`);
Logger.log(
`getFileLogCommit('${repoPath}', '${fileName}', '${options.ref}', ${options.firstIfNotFound}, ${
options.reverse
})`
);

const log = await this.getLogForFile(repoPath, fileName, { maxCount: 2, ref: options.ref });
const log = await this.getLogForFile(repoPath, fileName, {
maxCount: 2,
ref: options.ref,
reverse: options.reverse
});
if (log === undefined) return undefined;

const commit = options.ref && log.commits.get(options.ref);
Expand Down Expand Up @@ -1888,7 +1896,9 @@ export class GitService implements Disposable {
try {
const gitExtension = extensions.getExtension('vscode.git');
if (gitExtension !== undefined) {
const gitApi = ((gitExtension.isActive ? gitExtension.exports : await gitExtension.activate()) as GitExtension).getAPI(1);
const gitApi = ((gitExtension.isActive
? gitExtension.exports
: await gitExtension.activate()) as GitExtension).getAPI(1);
gitPath = gitApi.git.path;
}
}
Expand Down
49 changes: 35 additions & 14 deletions src/views/explorerCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ export class ExplorerCommands implements Disposable {
void commands.executeCommand(BuiltInCommands.FocusFilesExplorer);
}

private openChanges(node: CommitNode | StashNode) {
private openChanges(node: CommitFileNode | StashFileNode | StatusFileNode) {
const command = node.getCommand();
if (command === undefined || command.arguments === undefined) return;

Expand All @@ -229,35 +229,55 @@ export class ExplorerCommands implements Disposable {
return commands.executeCommand(command.command, uri, args);
}

private openChangesWithWorking(node: CommitNode | StashNode) {
private async openChangesWithWorking(node: CommitFileNode | StashFileNode | StatusFileNode) {
const args: DiffWithWorkingCommandArgs = {
commit: node.commit,
showOptions: {
preserveFocus: true,
preview: false
}
};
return commands.executeCommand(Commands.DiffWithWorking, node.commit.toGitUri(), args);

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

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

private openFile(node: CommitFileNode | StashFileNode | StatusFileCommitsNode | StatusFileNode) {
private openFile(node: CommitFileNode | StashFileNode | StatusFileNode) {
return openEditor(node.uri, { preserveFocus: true, preview: false });
}

private openFileRevision(
node: CommitFileNode | StashFileNode | StatusFileCommitsNode,
node: CommitFileNode | StashFileNode | StatusFileNode,
options: OpenFileRevisionCommandArgs = { showOptions: { preserveFocus: true, preview: false } }
) {
const uri =
options.uri ||
(node.commit.status === 'D'
? GitUri.toRevisionUri(node.commit.previousSha!, node.commit.previousUri.fsPath, node.commit.repoPath)
: GitUri.toRevisionUri(node.uri));
let uri = options.uri;
if (uri == null) {
if (node instanceof StatusFileNode) {
uri = GitUri.toRevisionUri(node.uri);
}
else {
uri =
node.commit.status === 'D'
? GitUri.toRevisionUri(
node.commit.previousSha!,
node.commit.previousUri.fsPath,
node.commit.repoPath
)
: GitUri.toRevisionUri(node.uri);
}
}

return openEditor(uri, options.showOptions || { preserveFocus: true, preview: false });
}

private async openChangedFileChanges(
node: CommitFileNode | StashFileNode | StatusFileCommitsNode,
node: CommitNode | StashNode,
options: TextDocumentShowOptions = { preserveFocus: false, preview: false }
) {
const repoPath = node.commit.repoPath;
Expand All @@ -268,7 +288,8 @@ export class ExplorerCommands implements Disposable {
repoPath,
{
uri: uri,
sha: node.commit.previousSha !== undefined ? node.commit.previousSha : GitService.deletedOrMissingSha
sha:
node.commit.previousSha !== undefined ? node.commit.previousSha : GitService.deletedOrMissingSha
},
{ uri: uri, sha: node.commit.sha },
options
Expand All @@ -277,7 +298,7 @@ export class ExplorerCommands implements Disposable {
}

private async openChangedFileChangesWithWorking(
node: CommitFileNode | StashFileNode | StatusFileCommitsNode,
node: CommitNode | StashNode,
options: TextDocumentShowOptions = { preserveFocus: false, preview: false }
) {
const repoPath = node.commit.repoPath;
Expand Down
2 changes: 1 addition & 1 deletion src/views/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export * from './nodes/stashNode';
export * from './nodes/statusFileCommitsNode';
export * from './nodes/statusFileNode';
export * from './nodes/statusFilesNode';
export * from './nodes/statusFilesResultsNode';
export * from './nodes/resultsFilesNode';
export * from './nodes/statusUpstreamNode';
export * from './nodes/tagsNode';
export * from './nodes/tagNode';
6 changes: 3 additions & 3 deletions src/views/nodes/commitNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Arrays, Iterables, Strings } from '../../system';
import { Explorer } from '../explorer';
import { CommitFileNode, CommitFileNodeDisplayAs } from './commitFileNode';
import { ExplorerNode, ExplorerRefNode, ResourceType } from './explorerNode';
import { FolderNode, IFileExplorerNode } from './folderNode';
import { FileExplorerNode, FolderNode } from './folderNode';

export class CommitNode extends ExplorerRefNode {
constructor(
Expand All @@ -29,7 +29,7 @@ export class CommitNode extends ExplorerRefNode {

async getChildren(): Promise<ExplorerNode[]> {
const commit = this.commit;
let children: IFileExplorerNode[] = [
let children: FileExplorerNode[] = [
...Iterables.map(
commit.fileStatuses,
s => new CommitFileNode(s, commit.toFileCommit(s), this, this.explorer, CommitFileNodeDisplayAs.File)
Expand All @@ -45,7 +45,7 @@ export class CommitNode extends ExplorerRefNode {
);

const root = new FolderNode(this.repoPath, '', undefined, hierarchy, this, this.explorer);
children = (await root.getChildren()) as IFileExplorerNode[];
children = (await root.getChildren()) as FileExplorerNode[];
}
else {
children.sort((a, b) => a.label!.localeCompare(b.label!));
Expand Down
12 changes: 6 additions & 6 deletions src/views/nodes/folderNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import { Arrays, Objects } from '../../system';
import { Explorer } from '../explorer';
import { ExplorerNode, ResourceType } from './explorerNode';

export interface IFileExplorerNode extends ExplorerNode {
export interface FileExplorerNode extends ExplorerNode {
folderName: string;
label?: string;
priority: boolean;
relativePath?: string;
root?: Arrays.IHierarchicalItem<IFileExplorerNode>;
root?: Arrays.IHierarchicalItem<FileExplorerNode>;
}

export class FolderNode extends ExplorerNode {
Expand All @@ -21,17 +21,17 @@ export class FolderNode extends ExplorerNode {
public readonly repoPath: string,
public readonly folderName: string,
public readonly relativePath: string | undefined,
public readonly root: Arrays.IHierarchicalItem<IFileExplorerNode>,
public readonly root: Arrays.IHierarchicalItem<FileExplorerNode>,
parent: ExplorerNode,
public readonly explorer: Explorer
) {
super(GitUri.fromRepoPath(repoPath), parent);
}

async getChildren(): Promise<(FolderNode | IFileExplorerNode)[]> {
async getChildren(): Promise<(FolderNode | FileExplorerNode)[]> {
if (this.root.descendants === undefined || this.root.children === undefined) return [];

let children: (FolderNode | IFileExplorerNode)[];
let children: (FolderNode | FileExplorerNode)[];

const nesting = FolderNode.getFileNesting(
this.explorer.config.files,
Expand Down Expand Up @@ -81,7 +81,7 @@ export class FolderNode extends ExplorerNode {
return this.folderName;
}

static getFileNesting<T extends IFileExplorerNode>(
static getFileNesting<T extends FileExplorerNode>(
config: ExplorersFilesConfig,
children: T[],
isRoot: boolean
Expand Down
4 changes: 2 additions & 2 deletions src/views/nodes/resultsComparisonNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Strings } from '../../system';
import { ResultsExplorer } from '../resultsExplorer';
import { ExplorerNode, NamedRef, ResourceType } from './explorerNode';
import { CommitsQueryResults, ResultsCommitsNode } from './resultsCommitsNode';
import { StatusFilesResultsNode } from './statusFilesResultsNode';
import { ResultsFilesNode } from './resultsFilesNode';

export class ResultsComparisonNode extends ExplorerNode {
constructor(
Expand Down Expand Up @@ -35,7 +35,7 @@ export class ResultsComparisonNode extends ExplorerNode {
async getChildren(): Promise<ExplorerNode[]> {
return [
new ResultsCommitsNode(this.uri.repoPath!, this.getCommitsQuery.bind(this), this, this.explorer),
new StatusFilesResultsNode(this.uri.repoPath!, this._ref1.ref, this._ref2.ref, this, this.explorer)
new ResultsFilesNode(this.uri.repoPath!, this._ref1.ref, this._ref2.ref, this, this.explorer)
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import { GitStatusFile, GitUri } from '../../git/gitService';
import { Arrays, Iterables, Strings } from '../../system';
import { Explorer } from '../explorer';
import { ExplorerNode, ResourceType } from './explorerNode';
import { FolderNode, IFileExplorerNode } from './folderNode';
import { FileExplorerNode, FolderNode } from './folderNode';
import { StatusFileNode } from './statusFileNode';

export class StatusFilesResultsNode extends ExplorerNode {
readonly supportsPaging: boolean = true;

private _cache: { label: string; diff: GitStatusFile[] | undefined } | undefined;
export interface FilesQueryResults {
label: string;
diff: GitStatusFile[] | undefined;
}

export class ResultsFilesNode extends ExplorerNode {
constructor(
public readonly repoPath: string,
private readonly _ref1: string,
Expand All @@ -26,10 +27,10 @@ export class StatusFilesResultsNode extends ExplorerNode {
}

async getChildren(): Promise<ExplorerNode[]> {
const diff = await this.getDiff();
const { diff } = await this.getFilesQueryResults();
if (diff === undefined) return [];

let children: IFileExplorerNode[] = [
let children: FileExplorerNode[] = [
...Iterables.map(
diff,
s => new StatusFileNode(this.repoPath, s, this._ref1, this._ref2, this, this.explorer)
Expand All @@ -45,7 +46,7 @@ export class StatusFilesResultsNode extends ExplorerNode {
);

const root = new FolderNode(this.repoPath, '', undefined, hierarchy, this, this.explorer);
children = (await root.getChildren()) as IFileExplorerNode[];
children = (await root.getChildren()) as FileExplorerNode[];
}
else {
children.sort((a, b) => (a.priority ? -1 : 1) - (b.priority ? -1 : 1) || a.label!.localeCompare(b.label!));
Expand All @@ -55,43 +56,35 @@ export class StatusFilesResultsNode extends ExplorerNode {
}

async getTreeItem(): Promise<TreeItem> {
const diff = await this.getDiff();
const { diff, label } = await this.getFilesQueryResults();

const item = new TreeItem(
await this.getLabel(),
label,
diff && diff.length > 0 ? TreeItemCollapsibleState.Expanded : TreeItemCollapsibleState.None
);
item.contextValue = ResourceType.ResultsFiles;
return item;
}

refresh() {
this._cache = undefined;
async refresh() {
this._filesQueryResults = this.getFilesQueryResultsCore();
}

private async ensureCache() {
if (this._cache === undefined) {
const diff = await Container.git.getDiffStatus(this.uri.repoPath!, this._ref1, this._ref2);

const count = diff !== undefined ? diff.length : 0;
const label = `${Strings.pluralize('file', count, { zero: 'No' })} changed`;
private _filesQueryResults: Promise<FilesQueryResults> | undefined;

this._cache = {
label: label,
diff: diff
};
private getFilesQueryResults() {
if (this._filesQueryResults === undefined) {
this._filesQueryResults = this.getFilesQueryResultsCore();
}

return this._cache;
}

private async getDiff() {
const cache = await this.ensureCache();
return cache.diff;
return this._filesQueryResults;
}

private async getLabel() {
const cache = await this.ensureCache();
return cache.label;
private async getFilesQueryResultsCore(): Promise<FilesQueryResults> {
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`,
diff: diff
};
}
}
6 changes: 3 additions & 3 deletions src/views/nodes/statusFilesNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
import { Arrays, Iterables, Objects, Strings } from '../../system';
import { RepositoriesExplorer } from '../repositoriesExplorer';
import { ExplorerNode, ResourceType } from './explorerNode';
import { FolderNode, IFileExplorerNode } from './folderNode';
import { FileExplorerNode, FolderNode } from './folderNode';
import { StatusFileCommitsNode } from './statusFileCommitsNode';

export class StatusFilesNode extends ExplorerNode {
Expand Down Expand Up @@ -82,7 +82,7 @@ export class StatusFilesNode extends ExplorerNode {

const groups = Arrays.groupBy(statuses, s => s.fileName);

let children: IFileExplorerNode[] = [
let children: FileExplorerNode[] = [
...Iterables.map(
Objects.values(groups),
statuses =>
Expand All @@ -105,7 +105,7 @@ export class StatusFilesNode extends ExplorerNode {
);

const root = new FolderNode(repoPath, '', undefined, hierarchy, this, this.explorer);
children = (await root.getChildren()) as IFileExplorerNode[];
children = (await root.getChildren()) as FileExplorerNode[];
}
else {
children.sort((a, b) => (a.priority ? -1 : 1) - (b.priority ? -1 : 1) || a.label!.localeCompare(b.label!));
Expand Down

0 comments on commit c439ea0

Please sign in to comment.