From 3c57b6fd55a1f07989598c209ece4cb8bd34ce2b Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Tue, 9 Mar 2021 02:05:31 -0500 Subject: [PATCH] Closes #1415: prioritize branch upstream for remote providers Also prioritizes "upstream" over "origin" --- src/git/gitService.ts | 81 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 52 insertions(+), 29 deletions(-) diff --git a/src/git/gitService.ts b/src/git/gitService.ts index 18c211b..d5e94d4 100644 --- a/src/git/gitService.ts +++ b/src/git/gitService.ts @@ -483,37 +483,42 @@ export class GitService implements Disposable { const hasRepository = repositoryTree.any(); await setEnabled(hasRepository); - let hasRemotes = false; - let hasRichRemotes = false; - let hasConnectedRemotes = false; - if (hasRepository) { - for (const repo of repositoryTree.values()) { - if (!hasConnectedRemotes) { - hasConnectedRemotes = await repo.hasRichRemote(true); - - if (hasConnectedRemotes) { - hasRichRemotes = true; - hasRemotes = true; + // Don't block for the remote context updates (because it can block other downstream requests during initialization) + async function updateRemoteContext() { + let hasRemotes = false; + let hasRichRemotes = false; + let hasConnectedRemotes = false; + if (hasRepository) { + for (const repo of repositoryTree.values()) { + if (!hasConnectedRemotes) { + hasConnectedRemotes = await repo.hasRichRemote(true); + + if (hasConnectedRemotes) { + hasRichRemotes = true; + hasRemotes = true; + } } - } - if (!hasRichRemotes) { - hasRichRemotes = await repo.hasRichRemote(); - } + if (!hasRichRemotes) { + hasRichRemotes = await repo.hasRichRemote(); + } - if (!hasRemotes) { - hasRemotes = await repo.hasRemotes(); - } + if (!hasRemotes) { + hasRemotes = await repo.hasRemotes(); + } - if (hasRemotes && hasRichRemotes && hasConnectedRemotes) break; + if (hasRemotes && hasRichRemotes && hasConnectedRemotes) break; + } } + + await Promise.all([ + setContext(ContextKeys.HasRemotes, hasRemotes), + setContext(ContextKeys.HasRichRemotes, hasRichRemotes), + setContext(ContextKeys.HasConnectedRemotes, hasConnectedRemotes), + ]); } - await Promise.all([ - setContext(ContextKeys.HasRemotes, hasRemotes), - setContext(ContextKeys.HasRichRemotes, hasRichRemotes), - setContext(ContextKeys.HasConnectedRemotes, hasConnectedRemotes), - ]); + void updateRemoteContext(); // If we have no repositories setup a watcher in case one is initialized if (!hasRepository) { @@ -3138,23 +3143,41 @@ export class GitService implements Disposable { : remotesOrRepoPath ).filter(r => r.provider != null); + if (remotes.length === 0) return undefined; + let remote; if (remotes.length === 1) { remote = remotes[0]; } else { - let originRemote; + const weightedRemotes = new Map([ + ['upstream', 15], + ['origin', 10], + ]); + + const branch = await this.getBranch(remotes[0].repoPath); + const branchRemote = branch?.getRemoteName(); + + if (branchRemote != null) { + weightedRemotes.set(branchRemote, 100); + } + + let bestRemote; + let weight = 0; for (const r of remotes) { if (r.default) { - remote = r; + bestRemote = r; break; } - if (r.name === 'origin') { - originRemote = r; + // Don't choose a remote unless its weighted above + const matchedWeight = weightedRemotes.get(r.name) ?? -1; + if (matchedWeight > weight) { + bestRemote = r; + weight = matchedWeight; } } - remote = remote ?? originRemote ?? null; + remote = bestRemote ?? null; } if (!remote?.provider?.hasApi()) {