diff --git a/src/views/nodes/branchNode.ts b/src/views/nodes/branchNode.ts index 4d446a3..1ee86fc 100644 --- a/src/views/nodes/branchNode.ts +++ b/src/views/nodes/branchNode.ts @@ -14,6 +14,8 @@ export class BranchNode extends ExplorerRefNode implements PageableExplorerNode readonly supportsPaging: boolean = true; maxCount: number | undefined; + private _children: ExplorerNode[] | undefined; + constructor( public readonly branch: GitBranch, uri: GitUri, @@ -45,35 +47,42 @@ export class BranchNode extends ExplorerRefNode implements PageableExplorerNode } async getChildren(): Promise { - const log = await Container.git.getLog(this.uri.repoPath!, { - maxCount: this.maxCount || this.explorer.config.defaultItemLimit, - ref: this.ref - }); - if (log === undefined) return [new MessageNode('No commits yet')]; - - const branches = await Container.git.getBranches(this.uri.repoPath); - // Get the sha length, since `git branch` can return variable length shas - const shaLength = branches[0].sha!.length; - const branchesBySha = Arrays.groupByFilterMap( - branches, - b => b.sha!, - b => (b.name === this.branch.name ? undefined : b.name) - ); - - const getBranchTips = (sha: string) => { - const branches = branchesBySha.get(sha.substr(0, shaLength)); - if (branches === undefined || branches.length === 0) return undefined; - return branches.join(', '); - }; - - const children: (CommitNode | ShowMoreNode)[] = [ - ...Iterables.map(log.commits.values(), c => new CommitNode(c, this.explorer, this.branch, getBranchTips)) - ]; + if (this._children === undefined) { + const log = await Container.git.getLog(this.uri.repoPath!, { + maxCount: this.maxCount || this.explorer.config.defaultItemLimit, + ref: this.ref + }); + if (log === undefined) return [new MessageNode('No commits yet')]; + + const branches = await Container.git.getBranches(this.uri.repoPath); + // Get the sha length, since `git branch` can return variable length shas + const shaLength = branches[0].sha!.length; + const branchesBySha = Arrays.groupByFilterMap( + branches, + b => b.sha!, + b => (b.name === this.branch.name ? undefined : b.name) + ); + + const getBranchTips = (sha: string) => { + const branches = branchesBySha.get(sha.substr(0, shaLength)); + if (branches === undefined || branches.length === 0) return undefined; + return branches.join(', '); + }; + + const children: (CommitNode | ShowMoreNode)[] = [ + ...Iterables.map( + log.commits.values(), + c => new CommitNode(c, this.explorer, this.branch, getBranchTips) + ) + ]; + + if (log.truncated) { + children.push(new ShowMoreNode('Commits', this, this.explorer)); + } - if (log.truncated) { - children.push(new ShowMoreNode('Commits', this, this.explorer)); + this._children = children; } - return children; + return this._children; } async getTreeItem(): Promise { @@ -129,4 +138,8 @@ export class BranchNode extends ExplorerRefNode implements PageableExplorerNode return item; } + + refresh() { + this._children = undefined; + } } diff --git a/src/views/nodes/branchesNode.ts b/src/views/nodes/branchesNode.ts index 2737bf1..324afbc 100644 --- a/src/views/nodes/branchesNode.ts +++ b/src/views/nodes/branchesNode.ts @@ -10,6 +10,8 @@ import { BranchOrTagFolderNode } from './branchOrTagFolderNode'; import { ExplorerNode, ResourceType } from './explorerNode'; export class BranchesNode extends ExplorerNode { + private _children: ExplorerNode[] | undefined; + constructor( uri: GitUri, private readonly repo: Repository, @@ -23,26 +25,32 @@ export class BranchesNode extends ExplorerNode { } async getChildren(): Promise { - const branches = await this.repo.getBranches(); - if (branches === undefined) return []; - - branches.sort((a, b) => a.name.localeCompare(b.name)); - - // filter local branches - const branchNodes = [ - ...Iterables.filterMap(branches, b => (b.remote ? undefined : new BranchNode(b, this.uri, this.explorer))) - ]; - if (this.explorer.config.branches.layout === ExplorerBranchesLayout.List) return branchNodes; - - const hierarchy = Arrays.makeHierarchical( - branchNodes, - n => (n.branch.detached ? [n.branch.name] : n.branch.getName().split('/')), - (...paths: string[]) => paths.join('/'), - this.explorer.config.files.compact - ); - - const root = new BranchOrTagFolderNode('branch', this.repo.path, '', undefined, hierarchy, this.explorer); - return root.getChildren(); + if (this._children === undefined) { + const branches = await this.repo.getBranches(); + if (branches === undefined) return []; + + branches.sort((a, b) => a.name.localeCompare(b.name)); + + // filter local branches + const branchNodes = [ + ...Iterables.filterMap( + branches, + b => (b.remote ? undefined : new BranchNode(b, this.uri, this.explorer)) + ) + ]; + if (this.explorer.config.branches.layout === ExplorerBranchesLayout.List) return branchNodes; + + const hierarchy = Arrays.makeHierarchical( + branchNodes, + n => (n.branch.detached ? [n.branch.name] : n.branch.getName().split('/')), + (...paths: string[]) => paths.join('/'), + this.explorer.config.files.compact + ); + + const root = new BranchOrTagFolderNode('branch', this.repo.path, '', undefined, hierarchy, this.explorer); + this._children = await root.getChildren(); + } + return this._children; } async getTreeItem(): Promise { @@ -60,4 +68,8 @@ export class BranchesNode extends ExplorerNode { return item; } + + refresh() { + this._children = undefined; + } }