Skip to content

Commit 49f40ef

Browse files
committed
Moves upstream state to upstream object
1 parent ca21dca commit 49f40ef

File tree

25 files changed

+184
-176
lines changed

25 files changed

+184
-176
lines changed

src/commands/git/pull.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,10 @@ export class PullGitCommand extends QuickCommand<State> {
183183
createFlagsQuickPickItem<Flags>(state.flags, [], {
184184
label: this.title,
185185
detail: `Will pull${
186-
branch.state.behind
187-
? ` ${pluralize('commit', branch.state.behind)} into ${getReferenceLabel(branch)}`
186+
branch.upstream.state.behind
187+
? ` ${pluralize('commit', branch.upstream.state.behind)} into ${getReferenceLabel(
188+
branch,
189+
)}`
188190
: ` into ${getReferenceLabel(branch)}`
189191
}`,
190192
}),
@@ -200,10 +202,9 @@ export class PullGitCommand extends QuickCommand<State> {
200202
lastFetchedOn = `${pad(GlyphChars.Dot, 2, 2)}Last fetched ${fromNow(new Date(lastFetched))}`;
201203
}
202204

203-
const pullDetails =
204-
status?.state.behind != null
205-
? ` ${pluralize('commit', status.state.behind)} into $(repo) ${repo.formattedName}`
206-
: ` into $(repo) ${repo.formattedName}`;
205+
const pullDetails = status?.upstream?.state.behind
206+
? ` ${pluralize('commit', status.upstream.state.behind)} into $(repo) ${repo.formattedName}`
207+
: ` into $(repo) ${repo.formattedName}`;
207208

208209
step = this.createConfirmStep(
209210
appendReposToTitle(`Confirm ${context.title}`, state, context, lastFetchedOn),

src/commands/git/push.ts

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ export class PushGitCommand extends QuickCommand<State> {
231231
{ placeholder: 'Cannot publish; No remotes found' },
232232
);
233233
}
234-
} else if (branch != null && branch?.state.behind > 0) {
234+
} else if (branch?.upstream?.state.behind) {
235235
step = this.createConfirmStep(
236236
appendReposToTitle(`Confirm ${context.title}`, state, context),
237237
[
@@ -254,11 +254,13 @@ export class PushGitCommand extends QuickCommand<State> {
254254
: useForceWithLease
255255
? ' (with lease)'
256256
: ''
257-
} ${branch?.state.ahead ? ` ${pluralize('commit', branch.state.ahead)}` : ''}${
258-
branch.getRemoteName() ? ` to ${branch.getRemoteName()}` : ''
259-
}${
260-
branch != null && branch.state.behind > 0
261-
? `, overwriting ${pluralize('commit', branch.state.behind)}${
257+
} ${
258+
branch?.upstream.state.ahead
259+
? ` ${pluralize('commit', branch.upstream.state.ahead)}`
260+
: ''
261+
}${branch.getRemoteName() ? ` to ${branch.getRemoteName()}` : ''}${
262+
branch != null && branch.upstream.state.behind > 0
263+
? `, overwriting ${pluralize('commit', branch.upstream.state.behind)}${
262264
branch?.getRemoteName() ? ` on ${branch.getRemoteName()}` : ''
263265
}`
264266
: ''
@@ -269,16 +271,20 @@ export class PushGitCommand extends QuickCommand<State> {
269271
label: `Cancel ${this.title}`,
270272
detail: `Cannot push; ${getReferenceLabel(
271273
branch,
272-
)} is behind ${branch.getRemoteName()} by ${pluralize('commit', branch.state.behind)}`,
274+
)} is behind ${branch.getRemoteName()} by ${pluralize(
275+
'commit',
276+
branch.upstream.state.behind,
277+
)}`,
273278
}),
274279
);
275-
} else if (branch != null && branch?.state.ahead > 0) {
280+
} else if (branch?.upstream?.state.ahead) {
276281
step = this.createConfirmStep(appendReposToTitle(`Confirm ${context.title}`, state, context), [
277282
createFlagsQuickPickItem<Flags>(state.flags, [branch.getRemoteName()!], {
278283
label: this.title,
279-
detail: `Will push ${pluralize('commit', branch.state.ahead)} from ${getReferenceLabel(
280-
branch,
281-
)} to ${branch.getRemoteName()}`,
284+
detail: `Will push ${pluralize(
285+
'commit',
286+
branch.upstream.state.ahead,
287+
)} from ${getReferenceLabel(branch)} to ${branch.getRemoteName()}`,
282288
}),
283289
]);
284290
} else {
@@ -304,7 +310,7 @@ export class PushGitCommand extends QuickCommand<State> {
304310
repoPath: repo.path,
305311
};
306312

307-
if (status?.state.ahead === 0) {
313+
if (status?.upstream?.state.ahead === 0) {
308314
if (!isBranchReference(state.reference) && status.upstream == null) {
309315
let pushDetails;
310316

@@ -376,22 +382,22 @@ export class PushGitCommand extends QuickCommand<State> {
376382
let pushDetails;
377383
if (state.reference != null) {
378384
pushDetails = `${
379-
status?.state.ahead
385+
status?.upstream?.state.ahead
380386
? ` commits up to and including ${getReferenceLabel(state.reference, {
381387
label: false,
382388
})}`
383389
: ''
384390
}${status?.upstream ? ` to ${getRemoteNameFromBranchName(status.upstream?.name)}` : ''}`;
385391
} else {
386-
pushDetails = `${status?.state.ahead ? ` ${pluralize('commit', status.state.ahead)}` : ''}${
387-
status?.upstream ? ` to ${getRemoteNameFromBranchName(status.upstream?.name)}` : ''
388-
}`;
392+
pushDetails = `${
393+
status?.upstream?.state.ahead ? ` ${pluralize('commit', status.upstream.state.ahead)}` : ''
394+
}${status?.upstream ? ` to ${getRemoteNameFromBranchName(status.upstream?.name)}` : ''}`;
389395
}
390396

391397
step = this.createConfirmStep(
392398
appendReposToTitle(`Confirm ${context.title}`, state, context, lastFetchedOn),
393399
[
394-
...(status?.state.behind
400+
...(status?.upstream?.state.behind
395401
? []
396402
: [
397403
createFlagsQuickPickItem<Flags>(state.flags, [], {
@@ -419,8 +425,8 @@ export class PushGitCommand extends QuickCommand<State> {
419425
? ' (with lease)'
420426
: ''
421427
} ${pushDetails}${
422-
status != null && status.state.behind > 0
423-
? `, overwriting ${pluralize('commit', status.state.behind)}${
428+
status?.upstream?.state.behind
429+
? `, overwriting ${pluralize('commit', status.upstream.state.behind)}${
424430
status?.upstream
425431
? ` on ${getRemoteNameFromBranchName(status.upstream?.name)}`
426432
: ''
@@ -429,12 +435,12 @@ export class PushGitCommand extends QuickCommand<State> {
429435
}`,
430436
}),
431437
],
432-
status?.state.behind
438+
status?.upstream?.state.behind
433439
? createDirectiveQuickPickItem(Directive.Cancel, true, {
434440
label: `Cancel ${this.title}`,
435441
detail: `Cannot push; ${getReferenceLabel(branch)} is behind${
436442
status?.upstream ? ` ${getRemoteNameFromBranchName(status.upstream?.name)}` : ''
437-
} by ${pluralize('commit', status.state.behind)}`,
443+
} by ${pluralize('commit', status.upstream.state.behind)}`,
438444
})
439445
: undefined,
440446
);

src/commands/quickCommand.steps.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2520,28 +2520,28 @@ function getShowRepositoryStatusStepItems<
25202520
}
25212521

25222522
if (context.status.upstream) {
2523-
if (context.status.state.ahead === 0 && context.status.state.behind === 0) {
2523+
if (context.status.upstream.state.ahead === 0 && context.status.upstream.state.behind === 0) {
25242524
items.push(
25252525
createDirectiveQuickPickItem(Directive.Noop, true, {
25262526
label: `$(git-branch) ${context.status.branch} is up to date with $(git-branch) ${context.status.upstream?.name}`,
25272527
detail: workingTreeStatus,
25282528
}),
25292529
);
2530-
} else if (context.status.state.ahead !== 0 && context.status.state.behind !== 0) {
2530+
} else if (context.status.upstream.state.ahead !== 0 && context.status.upstream.state.behind !== 0) {
25312531
items.push(
25322532
createDirectiveQuickPickItem(Directive.Noop, true, {
25332533
label: `$(git-branch) ${context.status.branch} has diverged from $(git-branch) ${context.status.upstream?.name}`,
25342534
detail: workingTreeStatus,
25352535
}),
25362536
);
2537-
} else if (context.status.state.ahead !== 0) {
2537+
} else if (context.status.upstream.state.ahead !== 0) {
25382538
items.push(
25392539
createDirectiveQuickPickItem(Directive.Noop, true, {
25402540
label: `$(git-branch) ${context.status.branch} is ahead of $(git-branch) ${context.status.upstream?.name}`,
25412541
detail: workingTreeStatus,
25422542
}),
25432543
);
2544-
} else if (context.status.state.behind !== 0) {
2544+
} else if (context.status.upstream.state.behind !== 0) {
25452545
items.push(
25462546
createDirectiveQuickPickItem(Directive.Noop, true, {
25472547
label: `$(git-branch) ${context.status.branch} is behind $(git-branch) ${context.status.upstream?.name}`,
@@ -2550,10 +2550,10 @@ function getShowRepositoryStatusStepItems<
25502550
);
25512551
}
25522552

2553-
if (context.status.state.behind !== 0) {
2553+
if (context.status.upstream.state.behind !== 0) {
25542554
items.push(
25552555
new GitWizardQuickPickItem(
2556-
`$(cloud-download) ${pluralize('commit', context.status.state.behind)} behind`,
2556+
`$(cloud-download) ${pluralize('commit', context.status.upstream.state.behind)} behind`,
25572557
{
25582558
command: 'log',
25592559
state: {
@@ -2568,18 +2568,21 @@ function getShowRepositoryStatusStepItems<
25682568
);
25692569
}
25702570

2571-
if (context.status.state.ahead !== 0) {
2571+
if (context.status.upstream.state.ahead !== 0) {
25722572
items.push(
2573-
new GitWizardQuickPickItem(`$(cloud-upload) ${pluralize('commit', context.status.state.ahead)} ahead`, {
2574-
command: 'log',
2575-
state: {
2576-
repo: state.repo,
2577-
reference: createReference(
2578-
createRevisionRange(context.status.upstream?.name, context.status.ref, '..'),
2579-
state.repo.path,
2580-
),
2573+
new GitWizardQuickPickItem(
2574+
`$(cloud-upload) ${pluralize('commit', context.status.upstream.state.ahead)} ahead`,
2575+
{
2576+
command: 'log',
2577+
state: {
2578+
repo: state.repo,
2579+
reference: createReference(
2580+
createRevisionRange(context.status.upstream?.name, context.status.ref, '..'),
2581+
state.repo.path,
2582+
),
2583+
},
25812584
},
2582-
}),
2585+
),
25832586
);
25842587
}
25852588
} else {

src/env/node/git/sub-providers/branches.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,7 @@ export class BranchesGitSubProvider implements GitBranchesSubProvider {
102102
true,
103103
committerDate != null ? new Date(Number(committerDate) * 1000) : undefined,
104104
data[1],
105-
upstream ? { name: upstream, missing: false } : undefined,
106-
undefined,
107-
undefined,
105+
upstream ? { name: upstream, missing: false, state: { ahead: 0, behind: 0 } } : undefined,
108106
undefined,
109107
rebaseStatus != null,
110108
);

src/env/node/git/sub-providers/status.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,6 @@ export class StatusGitSubProvider implements GitStatusSubProvider {
482482
pausedOpStatus.incoming.name,
483483
status.sha,
484484
status.files,
485-
status.state,
486485
status.upstream,
487486
true,
488487
);

src/git/gitProviderService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1553,7 +1553,7 @@ export class GitProviderService implements Disposable {
15531553

15541554
@log<GitProviderService['getBranchAheadRange']>({ args: { 0: b => b.name } })
15551555
async getBranchAheadRange(branch: GitBranch): Promise<string | undefined> {
1556-
if (branch.state.ahead > 0) {
1556+
if (branch.upstream?.state.ahead) {
15571557
return createRevisionRange(branch.upstream?.name, branch.ref, '..');
15581558
}
15591559

src/git/models/branch.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ export class GitBranch implements GitBranchReference {
2727
readonly refType = 'branch';
2828
readonly detached: boolean;
2929
readonly id: string;
30-
readonly upstream?: { name: string; missing: boolean };
31-
readonly state: GitTrackingState;
3230

3331
constructor(
3432
private readonly container: Container,
@@ -38,9 +36,7 @@ export class GitBranch implements GitBranchReference {
3836
public readonly current: boolean,
3937
public readonly date: Date | undefined,
4038
public readonly sha?: string,
41-
upstream?: { name: string; missing: boolean },
42-
ahead: number = 0,
43-
behind: number = 0,
39+
public readonly upstream?: GitTrackingUpstream,
4440
detached: boolean = false,
4541
public readonly rebasing: boolean = false,
4642
) {
@@ -51,11 +47,7 @@ export class GitBranch implements GitBranchReference {
5147
this.name = formatDetachedHeadName(this.sha!);
5248
}
5349

54-
this.upstream = upstream?.name == null || upstream.name.length === 0 ? undefined : upstream;
55-
this.state = {
56-
ahead: ahead,
57-
behind: behind,
58-
};
50+
this.upstream = upstream?.name ? upstream : undefined;
5951
}
6052

6153
toString(): string {
@@ -77,9 +69,9 @@ export class GitBranch implements GitBranchReference {
7769
if (this.upstream == null) return this.detached ? 'detached' : 'local';
7870

7971
if (this.upstream.missing) return 'missingUpstream';
80-
if (this.state.ahead && this.state.behind) return 'diverged';
81-
if (this.state.ahead) return 'ahead';
82-
if (this.state.behind) return 'behind';
72+
if (this.upstream.state.ahead && this.upstream.state.behind) return 'diverged';
73+
if (this.upstream.state.ahead) return 'ahead';
74+
if (this.upstream.state.behind) return 'behind';
8375
return 'upToDate';
8476
}
8577

@@ -166,7 +158,7 @@ export class GitBranch implements GitBranchReference {
166158
separator?: string;
167159
suffix?: string;
168160
}): string {
169-
return getUpstreamStatus(this.upstream, this.state, options);
161+
return getUpstreamStatus(this.upstream, options);
170162
}
171163

172164
get starred(): boolean {
@@ -184,8 +176,14 @@ export class GitBranch implements GitBranchReference {
184176
}
185177

186178
export interface GitTrackingState {
187-
ahead: number;
188-
behind: number;
179+
readonly ahead: number;
180+
readonly behind: number;
181+
}
182+
183+
export interface GitTrackingUpstream {
184+
readonly name: string;
185+
readonly missing: boolean;
186+
readonly state: GitTrackingState;
189187
}
190188

191189
export type GitBranchStatus =

src/git/models/status.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { memoize } from '../../system/decorators/-webview/memoize';
44
import { pluralize } from '../../system/string';
55
import { formatDetachedHeadName, getRemoteNameFromBranchName, isDetachedHead } from '../utils/branch.utils';
66
import { getUpstreamStatus } from '../utils/status.utils';
7-
import type { GitBranchStatus, GitTrackingState } from './branch';
7+
import type { GitBranchStatus, GitTrackingUpstream } from './branch';
88
import type { GitDiffFileStats } from './diff';
99
import { GitFileConflictStatus, GitFileIndexStatus, GitFileWorkingTreeStatus } from './fileStatus';
1010
import type { GitRemote } from './remote';
@@ -19,8 +19,7 @@ export class GitStatus {
1919
public readonly branch: string,
2020
public readonly sha: string,
2121
public readonly files: GitStatusFile[],
22-
public readonly state: GitTrackingState,
23-
public readonly upstream?: { name: string; missing: boolean },
22+
public readonly upstream?: GitTrackingUpstream,
2423
public readonly rebasing: boolean = false,
2524
) {
2625
this.detached = isDetachedHead(branch);
@@ -33,9 +32,9 @@ export class GitStatus {
3332
if (this.upstream == null) return this.detached ? 'detached' : 'local';
3433

3534
if (this.upstream.missing) return 'missingUpstream';
36-
if (this.state.ahead && this.state.behind) return 'diverged';
37-
if (this.state.ahead) return 'ahead';
38-
if (this.state.behind) return 'behind';
35+
if (this.upstream.state.ahead && this.upstream.state.behind) return 'diverged';
36+
if (this.upstream.state.ahead) return 'ahead';
37+
if (this.upstream.state.behind) return 'behind';
3938
return 'upToDate';
4039
}
4140

@@ -272,7 +271,7 @@ export class GitStatus {
272271
separator?: string;
273272
suffix?: string;
274273
}): string {
275-
return getUpstreamStatus(this.upstream, this.state, options);
274+
return getUpstreamStatus(this.upstream, options);
276275
}
277276
}
278277

src/git/parsers/branchParser.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,11 @@ export function parseGitBranches(container: Container, data: string, repoPath: s
6767
// Stops excessive memory usage -- https://bugs.chromium.org/p/v8/issues/detail?id=2869
6868
upstream == null || upstream.length === 0
6969
? undefined
70-
: { name: ` ${upstream}`.substring(1), missing: Boolean(missing) },
71-
Number(ahead) || 0,
72-
Number(behind) || 0,
70+
: {
71+
name: ` ${upstream}`.substring(1),
72+
missing: Boolean(missing),
73+
state: { ahead: Number(ahead) || 0, behind: Number(behind) || 0 },
74+
},
7375
),
7476
);
7577
} while (true);

src/git/parsers/statusParser.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ function parseStatusV1(container: Container, lines: string[], repoPath: string):
7979
branch ?? '',
8080
'',
8181
files,
82-
state,
83-
upstream ? { name: upstream, missing: missing } : undefined,
82+
upstream ? { name: upstream, missing: missing, state: state } : undefined,
8483
);
8584
}
8685

@@ -145,8 +144,7 @@ function parseStatusV2(container: Container, lines: string[], repoPath: string):
145144
branch ?? '',
146145
sha ?? '',
147146
files,
148-
state,
149-
upstream ? { name: upstream, missing: missing } : undefined,
147+
upstream ? { name: upstream, missing: missing, state: state } : undefined,
150148
);
151149
}
152150

0 commit comments

Comments
 (0)