Skip to content

Commit 2ff7dde

Browse files
committed
Moves local branch lookup into the sub-provider
1 parent 2cd22d3 commit 2ff7dde

File tree

6 files changed

+17
-23
lines changed

6 files changed

+17
-23
lines changed

src/commands/ghpr/openOrCreateWorktree.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { window } from 'vscode';
33
import { GlCommand } from '../../constants.commands';
44
import type { Container } from '../../container';
55
import { create as createWorktree, open as openWorktree } from '../../git/actions/worktree';
6-
import { getLocalBranchByUpstream } from '../../git/models/branch.utils';
76
import type { GitBranchReference } from '../../git/models/reference';
87
import { createReference, getReferenceFromBranch } from '../../git/models/reference.utils';
98
import type { GitRemote } from '../../git/models/remote';
@@ -113,7 +112,7 @@ export class OpenOrCreateWorktreeCommand extends GlCommandBase {
113112
let branchRef: GitBranchReference;
114113
let createBranch: string | undefined;
115114

116-
const localBranch = await getLocalBranchByUpstream(repo, remoteBranchName);
115+
const localBranch = await repo.git.branches().getLocalBranchByUpstream?.(remoteBranchName);
117116
if (localBranch != null) {
118117
branchRef = getReferenceFromBranch(localBranch);
119118
// TODO@eamodio check if we are behind and if so ask the user to fast-forward

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type {
99
PagingOptions,
1010
} from '../../../../git/gitProvider';
1111
import { GitBranch } from '../../../../git/models/branch';
12-
import { isDetachedHead } from '../../../../git/models/branch.utils';
12+
import { getLocalBranchByUpstream, isDetachedHead } from '../../../../git/models/branch.utils';
1313
import type { MergeConflict } from '../../../../git/models/mergeConflict';
1414
import { createRevisionRange } from '../../../../git/models/revision.utils';
1515
import { parseGitBranches } from '../../../../git/parsers/branchParser';
@@ -21,6 +21,7 @@ import { gate } from '../../../../system/decorators/gate';
2121
import { log } from '../../../../system/decorators/log';
2222
import { Logger } from '../../../../system/logger';
2323
import { getLogScope } from '../../../../system/logger.scope';
24+
import { PageableResult } from '../../../../system/paging';
2425
import { getSettledValue } from '../../../../system/promise';
2526
import { configuration } from '../../../../system/vscode/configuration';
2627
import type { Git } from '../git';
@@ -309,6 +310,14 @@ export class BranchesGitSubProvider implements GitBranchesSubProvider {
309310
await this.git.branch(repoPath, name, ref);
310311
}
311312

313+
@log()
314+
async getLocalBranchByUpstream(repoPath: string, remoteBranchName: string): Promise<GitBranch | undefined> {
315+
const branches = new PageableResult<GitBranch>(p =>
316+
this.getBranches(repoPath, p != null ? { paging: p } : undefined),
317+
);
318+
return getLocalBranchByUpstream(remoteBranchName, branches);
319+
}
320+
312321
@log()
313322
async getPotentialMergeOrRebaseConflict(
314323
repoPath: string,

src/git/gitProvider.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ export interface GitBranchesSubProvider {
398398
): Promise<string | undefined>;
399399

400400
createBranch?(repoPath: string, name: string, ref: string): Promise<void>;
401+
getLocalBranchByUpstream?(repoPath: string, remoteBranchName: string): Promise<GitBranch | undefined>;
401402
getPotentialMergeOrRebaseConflict?(
402403
repoPath: string,
403404
branch: string,

src/git/models/branch.utils.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@ import {
99
getIssueFromGitConfigEntityIdentifier,
1010
} from '../../plus/integrations/providers/utils';
1111
import { Logger } from '../../system/logger';
12-
import { PageableResult } from '../../system/paging';
12+
import type { PageableResult } from '../../system/paging';
1313
import type { MaybePausedResult } from '../../system/promise';
1414
import { getSettledValue, pauseOnCancelOrTimeout } from '../../system/promise';
1515
import type { GitBranch } from './branch';
1616
import type { Issue } from './issue';
1717
import type { PullRequest } from './pullRequest';
1818
import type { GitBranchReference, GitReference } from './reference';
19-
import type { Repository } from './repository';
2019
import { shortenRevision } from './revision.utils';
2120

2221
const detachedHEADRegex = /^(HEAD|\(.*\))$/;
@@ -100,9 +99,8 @@ export async function getDefaultBranchName(
10099
}
101100

102101
export async function getLocalBranchByUpstream(
103-
repo: Repository,
104102
remoteBranchName: string,
105-
branches?: PageableResult<GitBranch> | Map<unknown, GitBranch>,
103+
branches: PageableResult<GitBranch> | Map<unknown, GitBranch>,
106104
): Promise<GitBranch | undefined> {
107105
let qualifiedRemoteBranchName;
108106
if (remoteBranchName.startsWith('remotes/')) {
@@ -112,10 +110,6 @@ export async function getLocalBranchByUpstream(
112110
qualifiedRemoteBranchName = `remotes/${remoteBranchName}`;
113111
}
114112

115-
branches ??= new PageableResult<GitBranch>(p =>
116-
repo.git.branches().getBranches(p != null ? { paging: p } : undefined),
117-
);
118-
119113
function matches(branch: GitBranch): boolean {
120114
return (
121115
!branch.remote &&

src/plus/launchpad/launchpadProvider.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import { CancellationError } from '../../errors';
1414
import { openComparisonChanges } from '../../git/actions/commit';
1515
import type { Account } from '../../git/models/author';
1616
import type { GitBranch } from '../../git/models/branch';
17-
import { getLocalBranchByUpstream } from '../../git/models/branch.utils';
1817
import type { PullRequest, SearchedPullRequest } from '../../git/models/pullRequest';
1918
import {
2019
getComparisonRefsForPullRequest,
@@ -533,7 +532,7 @@ export class LaunchpadProvider implements Disposable {
533532
const [repo, remote] = match;
534533

535534
const remoteBranchName = `${remote.name}/${pr.refs?.head.branch ?? pr.headRef?.name}`;
536-
const matchingLocalBranch = await getLocalBranchByUpstream(repo, remoteBranchName);
535+
const matchingLocalBranch = await repo.git.branches().getLocalBranchByUpstream?.(remoteBranchName);
537536

538537
return { repo: repo, remote: remote, localBranch: matchingLocalBranch };
539538
}

src/webviews/plus/graph/graphWebview.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2754,11 +2754,7 @@ export class GraphWebviewProvider implements WebviewProvider<State, State, Graph
27542754
type: 'remote',
27552755
});
27562756

2757-
const localDefault = await getLocalBranchByUpstream(
2758-
this.repository!,
2759-
defaultBranchName,
2760-
graph.branches,
2761-
);
2757+
const localDefault = await getLocalBranchByUpstream(defaultBranchName, graph.branches);
27622758
if (localDefault != null) {
27632759
refs.push({
27642760
id: localDefault.id,
@@ -2899,11 +2895,7 @@ export class GraphWebviewProvider implements WebviewProvider<State, State, Graph
28992895
});
29002896
}
29012897

2902-
const localDefault = await getLocalBranchByUpstream(
2903-
this.repository!,
2904-
defaultBranchName,
2905-
graph.branches,
2906-
);
2898+
const localDefault = await getLocalBranchByUpstream(defaultBranchName, graph.branches);
29072899
if (localDefault != null) {
29082900
if (!refs.has(localDefault.id)) {
29092901
refs.set(localDefault.id, {

0 commit comments

Comments
 (0)