/
branchNode.ts
216 lines (186 loc) · 7.5 KB
/
branchNode.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
'use strict';
import { TreeItem, TreeItemCollapsibleState } from 'vscode';
import { ViewBranchesLayout } from '../../configuration';
import { GlyphChars } from '../../constants';
import { Container } from '../../container';
import { GitBranch, GitRemoteType, GitUri } from '../../git/gitService';
import { debug, gate, Iterables, log } from '../../system';
import { RepositoriesView } from '../repositoriesView';
import { BranchTrackingStatusNode } from './branchTrackingStatusNode';
import { CommitNode } from './commitNode';
import { MessageNode, ShowMoreNode } from './common';
import { insertDateMarkers } from './helpers';
import { PageableViewNode, ResourceType, ViewNode, ViewRefNode } from './viewNode';
export class BranchNode extends ViewRefNode<RepositoriesView> implements PageableViewNode {
readonly supportsPaging = true;
readonly rememberLastMaxCount = true;
maxCount: number | undefined = this.view.getNodeLastMaxCount(this);
private _children: ViewNode[] | undefined;
constructor(
uri: GitUri,
view: RepositoriesView,
parent: ViewNode,
public readonly branch: GitBranch,
// Specifies that the node is shown as a root under the repository node
private readonly _root: boolean = false
) {
super(uri, view, parent);
}
get id(): string {
return `gitlens:repository(${this.branch.repoPath})${this._root ? ':root' : ''}:branch(${this.branch.name})${
this.branch.current ? '+current' : ''
}${this.branch.remote ? '+remote' : ''}${this.branch.starred ? '+starred' : ''}`;
}
get current(): boolean {
return this.branch.current;
}
get label(): string {
const branchName = this.branch.getName();
if (this.view.config.branches.layout === ViewBranchesLayout.List) return branchName;
return (this._root && this.current) || this.branch.detached || this.branch.starred
? branchName
: this.branch.getBasename();
}
get ref(): string {
return this.branch.ref;
}
get treeHierarchy(): string[] {
return this.branch.detached || this.branch.starred ? [this.branch.name] : this.branch.getName().split('/');
}
async getChildren(): Promise<ViewNode[]> {
if (this._children === undefined) {
const children = [];
if (!this._root && this.branch.tracking) {
const status = {
ref: this.branch.ref,
repoPath: this.branch.repoPath,
state: this.branch.state,
upstream: this.branch.tracking
};
if (this.branch.state.behind) {
children.push(new BranchTrackingStatusNode(this.view, this, this.branch, status, 'behind'));
}
if (this.branch.state.ahead) {
children.push(new BranchTrackingStatusNode(this.view, this, this.branch, status, 'ahead'));
}
}
const log = await Container.git.getLog(this.uri.repoPath!, {
maxCount: this.maxCount !== undefined ? this.maxCount : this.view.config.defaultItemLimit,
ref: this.ref
});
if (log === undefined) return [new MessageNode(this.view, this, 'No commits could be found.')];
const getBranchAndTagTips = await Container.git.getBranchesAndTagsTipsFn(
this.uri.repoPath,
this.branch.name
);
children.push(
...insertDateMarkers(
Iterables.map(
log.commits.values(),
c => new CommitNode(this.view, this, c, this.branch, getBranchAndTagTips)
),
this
)
);
if (log.truncated) {
children.push(
new ShowMoreNode(this.view, this, 'Commits', log.maxCount, children[children.length - 1])
);
}
this._children = children;
}
return this._children;
}
async getTreeItem(): Promise<TreeItem> {
const name = this.label;
let tooltip = `${this.branch.getName()}${this.current ? ' (current)' : ''}`;
let iconSuffix = '';
let description;
if (!this.branch.remote && this.branch.tracking !== undefined) {
if (this.view.config.showTrackingBranch) {
let arrows = GlyphChars.Dash;
const remote = await this.branch.getRemote();
if (remote !== undefined) {
let left;
let right;
for (const { type } of remote.types) {
if (type === GitRemoteType.Fetch) {
left = true;
if (right) break;
}
else if (type === GitRemoteType.Push) {
right = true;
if (left) break;
}
}
if (left && right) {
arrows = GlyphChars.ArrowsRightLeft;
}
else if (right) {
arrows = GlyphChars.ArrowRight;
}
else if (left) {
arrows = GlyphChars.ArrowLeft;
}
}
description = `${this.branch.getTrackingStatus({ suffix: `${GlyphChars.Space} ` })}${arrows}${
GlyphChars.Space
} ${this.branch.tracking}`;
}
tooltip += ` is tracking ${this.branch.tracking}\n${this.branch.getTrackingStatus({
empty: 'up-to-date',
expand: true,
separator: '\n'
})}`;
if (this.branch.state.ahead || this.branch.state.behind) {
if (this.branch.state.behind) {
iconSuffix = '-red';
}
if (this.branch.state.ahead) {
iconSuffix = this.branch.state.behind ? '-yellow' : '-green';
}
}
}
const item = new TreeItem(
// Hide the current branch checkmark when the node is displayed as a root under the repository node
`${!this._root && this.current ? `${GlyphChars.Check} ${GlyphChars.Space}` : ''}${name}`,
TreeItemCollapsibleState.Collapsed
);
item.contextValue = ResourceType.Branch;
if (this.current) {
item.contextValue += '+current';
}
if (this.branch.remote) {
item.contextValue += '+remote';
}
if (this.branch.starred) {
item.contextValue += '+starred';
}
if (this.branch.tracking) {
item.contextValue += '+tracking';
}
item.description = description;
item.iconPath = {
dark: Container.context.asAbsolutePath(`images/dark/icon-branch${iconSuffix}.svg`),
light: Container.context.asAbsolutePath(`images/light/icon-branch${iconSuffix}.svg`)
};
item.id = this.id;
item.tooltip = tooltip;
return item;
}
@log()
async star() {
await this.branch.star();
void this.parent!.triggerChange();
}
@log()
async unstar() {
await this.branch.unstar();
void this.parent!.triggerChange();
}
@gate()
@debug()
refresh() {
this._children = undefined;
}
}