Skip to content

Commit 7cd77ff

Browse files
Attempts to pre-open PR branch when using Switch or Open in Worktree (#3635)
1 parent dab4cb7 commit 7cd77ff

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed

src/git/models/pullRequest.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ export function getVirtualUriForPullRequest(pr: PullRequest): Uri | undefined {
309309
export async function getOrOpenPullRequestRepository(
310310
container: Container,
311311
pr: PullRequest,
312-
options?: { promptIfNeeded?: boolean },
312+
options?: { promptIfNeeded?: boolean; skipVirtual?: boolean },
313313
): Promise<Repository | undefined> {
314314
const identity = getRepositoryIdentityForPullRequest(pr);
315315
let repo = await container.repositoryIdentity.getRepository(identity, {
@@ -318,7 +318,7 @@ export async function getOrOpenPullRequestRepository(
318318
prompt: false,
319319
});
320320

321-
if (repo == null) {
321+
if (repo == null && !options?.skipVirtual) {
322322
const virtualUri = getVirtualUriForPullRequest(pr);
323323
if (virtualUri != null) {
324324
repo = await container.git.getOrOpenRepository(virtualUri, { closeOnOpen: true, detectNested: false });

src/plus/launchpad/launchpadProvider.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ import type { Account } from '../../git/models/author';
1616
import type { GitBranch } from '../../git/models/branch';
1717
import { getLocalBranchByUpstream } from '../../git/models/branch';
1818
import type { PullRequest, SearchedPullRequest } from '../../git/models/pullRequest';
19-
import { getComparisonRefsForPullRequest, getRepositoryIdentityForPullRequest } from '../../git/models/pullRequest';
19+
import {
20+
getComparisonRefsForPullRequest,
21+
getOrOpenPullRequestRepository,
22+
getRepositoryIdentityForPullRequest,
23+
} from '../../git/models/pullRequest';
2024
import type { GitRemote } from '../../git/models/remote';
2125
import type { Repository } from '../../git/models/repository';
2226
import type { CodeSuggestionCounts, Draft } from '../../gk/models/drafts';
@@ -488,8 +492,11 @@ export class LaunchpadProvider implements Disposable {
488492
);
489493
if (deepLinkUrl == null) return;
490494

495+
const prRepo = await getOrOpenPullRequestRepository(this.container, item.underlyingPullRequest, {
496+
skipVirtual: true,
497+
});
491498
this._codeSuggestions?.delete(item.uuid);
492-
await this.container.deepLinks.processDeepLinkUri(deepLinkUrl, false);
499+
await this.container.deepLinks.processDeepLinkUri(deepLinkUrl, false, prRepo);
493500
}
494501

495502
@log<LaunchpadProvider['openChanges']>({ args: { 0: i => `${i.id} (${i.provider.name} ${i.type})` } })

src/uris/deepLinks/deepLinkService.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { getBranchNameWithoutRemote } from '../../git/models/branch';
1010
import type { GitCommit } from '../../git/models/commit';
1111
import type { GitReference } from '../../git/models/reference';
1212
import { createReference, isSha } from '../../git/models/reference';
13-
import type { RepositoryChangeEvent } from '../../git/models/repository';
13+
import type { Repository, RepositoryChangeEvent } from '../../git/models/repository';
1414
import { RepositoryChange, RepositoryChangeComparisonMode } from '../../git/models/repository';
1515
import type { GitTag } from '../../git/models/tag';
1616
import { parseGitRemoteUrl } from '../../git/parsers/remoteParser';
@@ -100,7 +100,7 @@ export class DeepLinkService implements Disposable {
100100
};
101101
}
102102

103-
private setContextFromDeepLink(link: DeepLink, url: string) {
103+
private setContextFromDeepLink(link: DeepLink, url: string, repo?: Repository) {
104104
this._context = {
105105
...this._context,
106106
mainId: link.mainId,
@@ -115,9 +115,14 @@ export class DeepLinkService implements Disposable {
115115
action: link.action,
116116
params: link.params,
117117
};
118+
119+
if (repo != null) {
120+
this._context.repo = repo;
121+
this._context.repoPath = repo.path;
122+
}
118123
}
119124

120-
async processDeepLinkUri(uri: Uri, useProgress: boolean = true) {
125+
async processDeepLinkUri(uri: Uri, useProgress: boolean = true, repo?: Repository) {
121126
const link = parseDeepLinkUri(uri);
122127
if (link == null) return;
123128

@@ -150,7 +155,7 @@ export class DeepLinkService implements Disposable {
150155
return;
151156
}
152157

153-
this.setContextFromDeepLink(link, uri.toString());
158+
this.setContextFromDeepLink(link, uri.toString(), repo);
154159

155160
await this.processDeepLink(undefined, useProgress);
156161
}
@@ -499,6 +504,9 @@ export class DeepLinkService implements Disposable {
499504
return remoteName;
500505
}
501506

507+
// TODO @axosoft-ramint: Move all the logic for matching a repo, prompting to add repo, matching remote, etc. for a target (branch, PR, etc.)
508+
// to a separate service where it can be used outside of the context of deep linking. Then the deep link service should leverage it,
509+
// and we should stop using deep links to process things like Launchpad switch actions, Open in Worktree command, etc.
502510
private async processDeepLink(
503511
initialAction: DeepLinkServiceAction = DeepLinkServiceAction.DeepLinkEventFired,
504512
useProgress: boolean = true,
@@ -660,7 +668,7 @@ export class DeepLinkService implements Disposable {
660668
}
661669
case DeepLinkServiceState.RepoMatch:
662670
case DeepLinkServiceState.AddedRepoMatch: {
663-
if (repo != null && repoOpenUri != null && repoOpenLocation != null) {
671+
if (repo != null) {
664672
action = DeepLinkServiceAction.RepoMatched;
665673
break;
666674
}

src/views/viewCommands.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
ensurePullRequestRefs,
2828
getComparisonRefsForPullRequest,
2929
getOpenedPullRequestRepo,
30+
getOrOpenPullRequestRepository,
3031
getRepositoryIdentityForPullRequest,
3132
} from '../git/models/pullRequest';
3233
import { createReference, shortenRevision } from '../git/models/reference';
@@ -807,7 +808,11 @@ export class ViewCommands {
807808
repoIdentity.remote.url,
808809
DeepLinkActionType.SwitchToPullRequestWorktree,
809810
);
810-
return this.container.deepLinks.processDeepLinkUri(deepLink, false);
811+
812+
const prRepo = await getOrOpenPullRequestRepository(this.container, pr, {
813+
skipVirtual: true,
814+
});
815+
return this.container.deepLinks.processDeepLinkUri(deepLink, false, prRepo);
811816
}
812817
}
813818

0 commit comments

Comments
 (0)