diff --git a/src/git/models/logCommit.ts b/src/git/models/logCommit.ts index b533b8e..6fc8e09 100644 --- a/src/git/models/logCommit.ts +++ b/src/git/models/logCommit.ts @@ -175,7 +175,7 @@ export class GitLogCommit extends GitCommit { toFileCommit(file: string | GitFile): GitLogCommit | undefined { const fileName = typeof file === 'string' ? GitUri.relativeTo(file, this.repoPath) : file.fileName; const foundFile = this.files.find(f => f.fileName === fileName); - if (foundFile === undefined) return undefined; + if (foundFile == null) return undefined; let sha; // If this is a stash commit with an untracked file diff --git a/src/git/models/stashCommit.ts b/src/git/models/stashCommit.ts index 9c808c2..4861065 100644 --- a/src/git/models/stashCommit.ts +++ b/src/git/models/stashCommit.ts @@ -1,9 +1,10 @@ 'use strict'; import { GitCommitType } from './commit'; +import { Container } from '../../container'; import { GitFile } from './file'; import { GitLogCommit } from './logCommit'; import { GitReference } from './models'; -import { memoize } from '../../system'; +import { gate, memoize } from '../../system'; const stashNumberRegex = /stash@{(\d+)}/; @@ -49,6 +50,24 @@ export class GitStashCommit extends GitLogCommit { return this.stashName; } + private _untrackedFilesChecked = false; + @gate() + async checkForUntrackedFiles() { + if (!this._untrackedFilesChecked) { + this._untrackedFilesChecked = true; + + // Check for any untracked files -- since git doesn't return them via `git stash list` :( + // See https://stackoverflow.com/questions/12681529/ + const commit = await Container.git.getCommit(this.repoPath, `${this.stashName}^3`); + if (commit != null && commit.files.length !== 0) { + // Since these files are untracked -- make them look that way + commit.files.forEach(s => (s.status = '?')); + + this.files.push(...commit.files); + } + } + } + with(changes: { type?: GitCommitType; sha?: string | null; diff --git a/src/views/nodes/stashNode.ts b/src/views/nodes/stashNode.ts index d3bc6fb..9f56703 100644 --- a/src/views/nodes/stashNode.ts +++ b/src/views/nodes/stashNode.ts @@ -1,15 +1,12 @@ 'use strict'; import * as paths from 'path'; import { TreeItem, TreeItemCollapsibleState } from 'vscode'; +import { ViewFilesLayout } from '../../config'; import { Container } from '../../container'; import { CommitFormatter, GitStashCommit, GitStashReference } from '../../git/git'; -import { Arrays, Iterables, Strings } from '../../system'; +import { ContextValues, FileNode, FolderNode, RepositoryNode, StashFileNode, ViewNode, ViewRefNode } from '../nodes'; +import { Arrays, Strings } from '../../system'; import { ViewsWithFiles } from '../viewBase'; -import { StashFileNode } from './stashFileNode'; -import { ContextValues, ViewNode, ViewRefNode } from './viewNode'; -import { RepositoryNode } from './repositoryNode'; -import { FileNode, FolderNode } from '../nodes'; -import { ViewFilesLayout } from '../../config'; export class StashNode extends ViewRefNode { static key = ':stash'; @@ -34,25 +31,12 @@ export class StashNode extends ViewRefNode { } async getChildren(): Promise { - let files = this.commit.files; - - // Check for any untracked files -- since git doesn't return them via `git stash list` :( - // See https://stackoverflow.com/questions/12681529/ - const log = await Container.git.getLog(this.commit.repoPath, { - limit: 1, - ref: `${this.commit.stashName}^3`, - }); - if (log != null) { - const commit = Iterables.first(log.commits.values()); - if (commit != null && commit.files.length !== 0) { - // Since these files are untracked -- make them look that way - commit.files.forEach(s => (s.status = '?')); + // Ensure we have checked for untracked files + await this.commit.checkForUntrackedFiles(); - files = { ...files, ...commit.files }; - } - } - - let children: FileNode[] = files.map(s => new StashFileNode(this.view, this, s, this.commit.toFileCommit(s)!)); + let children: FileNode[] = this.commit.files.map( + s => new StashFileNode(this.view, this, s, this.commit.toFileCommit(s)!), + ); if (this.view.config.files.layout !== ViewFilesLayout.List) { const hierarchy = Arrays.makeHierarchical(