diff --git a/CHANGELOG.md b/CHANGELOG.md index 3386b1d..6c1e06c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p ### Fixed +- Fixes [#695](https://github.com/eamodio/vscode-gitlens/issues/695) - Invalid URL in Open File in Remote when selecting origin/.. as comparison branch - Fixes [#683](https://github.com/eamodio/vscode-gitlens/issues/683) - log.showSignature leads to stray files being displayed - Fixes [#691](https://github.com/eamodio/vscode-gitlens/issues/691) - Auto-expand tree view on Swap Comparison - Fixes the behavior of the _Open Line Changes with Previous Revision_ (`gitlens.diffLineWithPrevious`) command to follow the line history much better diff --git a/src/commands/openFileInRemote.ts b/src/commands/openFileInRemote.ts index fc72c64..57342d7 100644 --- a/src/commands/openFileInRemote.ts +++ b/src/commands/openFileInRemote.ts @@ -15,6 +15,8 @@ import { isCommandViewContextWithCommit } from './common'; import { OpenInRemoteCommandArgs } from './openInRemote'; +import { Git } from '../git/git'; +import { Strings } from '../system'; export interface OpenFileInRemoteCommandArgs { branch?: string; @@ -54,30 +56,6 @@ export class OpenFileInRemoteCommand extends ActiveEditorCommand { const gitUri = await GitUri.fromUri(uri); if (!gitUri.repoPath) return undefined; - if (args.branch === undefined && args.sha === undefined) { - const branch = await Container.git.getBranch(gitUri.repoPath); - if (branch === undefined || branch.tracking === undefined) { - const pick = await new BranchesAndTagsQuickPick(gitUri.repoPath).show( - args.clipboard - ? `Copy url for ${gitUri.getRelativePath()} to clipboard for which branch${GlyphChars.Ellipsis}` - : `Open ${gitUri.getRelativePath()} on remote for which branch${GlyphChars.Ellipsis}`, - { - autoPick: true, - filters: { - branches: b => b.tracking !== undefined - }, - include: 'branches' - } - ); - if (pick === undefined || pick instanceof CommandQuickPickItem) return undefined; - - args.branch = pick.ref; - } - else { - args.branch = branch.name; - } - } - try { const remotes = await Container.git.getRemotes(gitUri.repoPath); const range = @@ -87,7 +65,43 @@ export class OpenFileInRemoteCommand extends ActiveEditorCommand { editor.selection.end.with({ line: editor.selection.end.line + 1 }) ) : undefined; - const sha = args.sha || gitUri.sha; + let sha = args.sha || gitUri.sha; + + if (args.branch === undefined && sha !== undefined && !Git.isSha(sha) && remotes.length !== 0) { + const [remotePart, branchPart] = Strings.splitSingle(sha, '/'); + if (branchPart !== undefined) { + if (remotes.some(r => r.name === remotePart)) { + args.branch = branchPart; + sha = undefined; + } + } + } + + if (args.branch === undefined && args.sha === undefined) { + const branch = await Container.git.getBranch(gitUri.repoPath); + if (branch === undefined || branch.tracking === undefined) { + const pick = await new BranchesAndTagsQuickPick(gitUri.repoPath).show( + args.clipboard + ? `Copy url for ${gitUri.getRelativePath()} to clipboard for which branch${ + GlyphChars.Ellipsis + }` + : `Open ${gitUri.getRelativePath()} on remote for which branch${GlyphChars.Ellipsis}`, + { + autoPick: true, + filters: { + branches: b => b.tracking !== undefined + }, + include: 'branches' + } + ); + if (pick === undefined || pick instanceof CommandQuickPickItem) return undefined; + + args.branch = pick.ref; + } + else { + args.branch = branch.name; + } + } const commandArgs: OpenInRemoteCommandArgs = { resource: diff --git a/src/commands/openInRemote.ts b/src/commands/openInRemote.ts index da9bcbf..98a038f 100644 --- a/src/commands/openInRemote.ts +++ b/src/commands/openInRemote.ts @@ -113,14 +113,13 @@ export class OpenInRemoteCommand extends ActiveEditorCommand { if (args.remotes === undefined || args.resource === undefined || args.resource.type !== 'branch') return; // Check to see if the remote is in the branch - const index = args.resource.branch.indexOf('/'); - if (index >= 0) { - const remoteName = args.resource.branch.substring(0, index); - const remote = args.remotes.find(r => r.name === remoteName); - if (remote !== undefined) { - args.resource.branch = args.resource.branch.substring(index + 1); - args.remotes = [remote]; - } - } + const [remotePart, branchPart] = Strings.splitSingle(args.resource.branch, '/'); + if (branchPart === undefined) return; + + const remote = args.remotes.find(r => r.name === remotePart); + if (remote === undefined) return; + + args.resource.branch = branchPart; + args.remotes = [remote]; } } diff --git a/src/system/string.ts b/src/system/string.ts index 7989a1e..f7dc093 100644 --- a/src/system/string.ts +++ b/src/system/string.ts @@ -173,6 +173,12 @@ export namespace Strings { .digest(encoding); } + export function splitSingle(s: string, splitter: string) { + const parts = s.split(splitter, 1); + const first = parts[0]; + return first.length === s.length ? parts : [first, s.substr(first.length + 1)]; + } + export function truncate(s: string, truncateTo: number, ellipsis: string = '\u2026', width?: number) { if (!s) return s;