Browse Source

Saves children to avoid losing state on tree update

main
Eric Amodio 6 years ago
parent
commit
ae2c47b81b
2 changed files with 72 additions and 47 deletions
  1. +40
    -27
      src/views/nodes/branchNode.ts
  2. +32
    -20
      src/views/nodes/branchesNode.ts

+ 40
- 27
src/views/nodes/branchNode.ts View File

@ -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<ExplorerNode[]> {
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<TreeItem> {
@ -129,4 +138,8 @@ export class BranchNode extends ExplorerRefNode implements PageableExplorerNode
return item;
}
refresh() {
this._children = undefined;
}
}

+ 32
- 20
src/views/nodes/branchesNode.ts View File

@ -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<ExplorerNode[]> {
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<TreeItem> {
@ -60,4 +68,8 @@ export class BranchesNode extends ExplorerNode {
return item;
}
refresh() {
this._children = undefined;
}
}

Loading…
Cancel
Save