Skip to content

Commit 15d81ee

Browse files
committed
Fall back to first remote for simple upstream in permalinks
Part of #3402
1 parent 2ac4d61 commit 15d81ee

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

src/issues/util.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { URL, URLSearchParams } from 'url';
77
import LRUCache from 'lru-cache';
88
import * as marked from 'marked';
99
import * as vscode from 'vscode';
10-
import { Commit, Ref, Remote, Repository } from '../api/api';
10+
import { Commit, Ref, Remote, Repository, UpstreamRef } from '../api/api';
1111
import { GitApiImpl } from '../api/api1';
1212
import { Protocol } from '../common/protocol';
1313
import { FolderRepositoryManager, PullRequestDefaults } from '../github/folderRepositoryManager';
@@ -405,17 +405,21 @@ async function getUpstream(repository: Repository, commit: Commit): Promise<Remo
405405
let bestRemote: Remote | undefined;
406406
for (let branchIndex = 0; branchIndex < branchNames.length && !bestRef; branchIndex++) {
407407
for (let remoteIndex = 0; remoteIndex < remoteNames.length && !bestRef; remoteIndex++) {
408-
const remotes = (
409-
await repository.getBranches({
410-
contains: commit.hash,
411-
remote: true,
412-
pattern: `remotes/${remoteNames[remoteIndex].name}/${branchNames[branchIndex]}`,
413-
count: 1,
414-
})
415-
).filter(value => value.remote && value.name);
416-
if (remotes && remotes.length > 0) {
417-
bestRef = remotes[0];
418-
bestRemote = remoteNames[remoteIndex].remote;
408+
try {
409+
const remotes = (
410+
await repository.getBranches({
411+
contains: commit.hash,
412+
remote: true,
413+
pattern: `remotes/${remoteNames[remoteIndex].name}/${branchNames[branchIndex]}`,
414+
count: 1,
415+
})
416+
).filter(value => value.remote && value.name);
417+
if (remotes && remotes.length > 0) {
418+
bestRef = remotes[0];
419+
bestRemote = remoteNames[remoteIndex].remote;
420+
}
421+
} catch (e) {
422+
// continue
419423
}
420424
}
421425
}
@@ -450,11 +454,11 @@ export interface PermalinkInfo {
450454
}
451455

452456
function getSimpleUpstream(repository: Repository) {
453-
if (repository.state.HEAD?.upstream) {
454-
for (const remote of repository.state.remotes) {
455-
if (repository.state.HEAD.upstream.remote === remote.name) {
456-
return remote;
457-
}
457+
const upstream: UpstreamRef | undefined = repository.state.HEAD?.upstream;
458+
for (const remote of repository.state.remotes) {
459+
// If we don't have an upstream, then just use the first remote.
460+
if (!upstream || (upstream.remote === remote.name)) {
461+
return remote;
458462
}
459463
}
460464
}

0 commit comments

Comments
 (0)