Skip to content

Commit 6158ff4

Browse files
committed
Fixes #695 - invalid url w/ remote in branch name
1 parent fb36df4 commit 6158ff4

File tree

4 files changed

+54
-34
lines changed

4 files changed

+54
-34
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
3737

3838
### Fixed
3939

40+
- Fixes [#695](https://github.com/eamodio/vscode-gitlens/issues/695) - Invalid URL in Open File in Remote when selecting origin/.. as comparison branch
4041
- Fixes [#683](https://github.com/eamodio/vscode-gitlens/issues/683) - log.showSignature leads to stray files being displayed
4142
- Fixes [#691](https://github.com/eamodio/vscode-gitlens/issues/691) - Auto-expand tree view on Swap Comparison
4243
- Fixes the behavior of the _Open Line Changes with Previous Revision_ (`gitlens.diffLineWithPrevious`) command to follow the line history much better

src/commands/openFileInRemote.ts

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import {
1515
isCommandViewContextWithCommit
1616
} from './common';
1717
import { OpenInRemoteCommandArgs } from './openInRemote';
18+
import { Git } from '../git/git';
19+
import { Strings } from '../system';
1820

1921
export interface OpenFileInRemoteCommandArgs {
2022
branch?: string;
@@ -54,30 +56,6 @@ export class OpenFileInRemoteCommand extends ActiveEditorCommand {
5456
const gitUri = await GitUri.fromUri(uri);
5557
if (!gitUri.repoPath) return undefined;
5658

57-
if (args.branch === undefined && args.sha === undefined) {
58-
const branch = await Container.git.getBranch(gitUri.repoPath);
59-
if (branch === undefined || branch.tracking === undefined) {
60-
const pick = await new BranchesAndTagsQuickPick(gitUri.repoPath).show(
61-
args.clipboard
62-
? `Copy url for ${gitUri.getRelativePath()} to clipboard for which branch${GlyphChars.Ellipsis}`
63-
: `Open ${gitUri.getRelativePath()} on remote for which branch${GlyphChars.Ellipsis}`,
64-
{
65-
autoPick: true,
66-
filters: {
67-
branches: b => b.tracking !== undefined
68-
},
69-
include: 'branches'
70-
}
71-
);
72-
if (pick === undefined || pick instanceof CommandQuickPickItem) return undefined;
73-
74-
args.branch = pick.ref;
75-
}
76-
else {
77-
args.branch = branch.name;
78-
}
79-
}
80-
8159
try {
8260
const remotes = await Container.git.getRemotes(gitUri.repoPath);
8361
const range =
@@ -87,7 +65,43 @@ export class OpenFileInRemoteCommand extends ActiveEditorCommand {
8765
editor.selection.end.with({ line: editor.selection.end.line + 1 })
8866
)
8967
: undefined;
90-
const sha = args.sha || gitUri.sha;
68+
let sha = args.sha || gitUri.sha;
69+
70+
if (args.branch === undefined && sha !== undefined && !Git.isSha(sha) && remotes.length !== 0) {
71+
const [remotePart, branchPart] = Strings.splitSingle(sha, '/');
72+
if (branchPart !== undefined) {
73+
if (remotes.some(r => r.name === remotePart)) {
74+
args.branch = branchPart;
75+
sha = undefined;
76+
}
77+
}
78+
}
79+
80+
if (args.branch === undefined && args.sha === undefined) {
81+
const branch = await Container.git.getBranch(gitUri.repoPath);
82+
if (branch === undefined || branch.tracking === undefined) {
83+
const pick = await new BranchesAndTagsQuickPick(gitUri.repoPath).show(
84+
args.clipboard
85+
? `Copy url for ${gitUri.getRelativePath()} to clipboard for which branch${
86+
GlyphChars.Ellipsis
87+
}`
88+
: `Open ${gitUri.getRelativePath()} on remote for which branch${GlyphChars.Ellipsis}`,
89+
{
90+
autoPick: true,
91+
filters: {
92+
branches: b => b.tracking !== undefined
93+
},
94+
include: 'branches'
95+
}
96+
);
97+
if (pick === undefined || pick instanceof CommandQuickPickItem) return undefined;
98+
99+
args.branch = pick.ref;
100+
}
101+
else {
102+
args.branch = branch.name;
103+
}
104+
}
91105

92106
const commandArgs: OpenInRemoteCommandArgs = {
93107
resource:

src/commands/openInRemote.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,13 @@ export class OpenInRemoteCommand extends ActiveEditorCommand {
113113
if (args.remotes === undefined || args.resource === undefined || args.resource.type !== 'branch') return;
114114

115115
// Check to see if the remote is in the branch
116-
const index = args.resource.branch.indexOf('/');
117-
if (index >= 0) {
118-
const remoteName = args.resource.branch.substring(0, index);
119-
const remote = args.remotes.find(r => r.name === remoteName);
120-
if (remote !== undefined) {
121-
args.resource.branch = args.resource.branch.substring(index + 1);
122-
args.remotes = [remote];
123-
}
124-
}
116+
const [remotePart, branchPart] = Strings.splitSingle(args.resource.branch, '/');
117+
if (branchPart === undefined) return;
118+
119+
const remote = args.remotes.find(r => r.name === remotePart);
120+
if (remote === undefined) return;
121+
122+
args.resource.branch = branchPart;
123+
args.remotes = [remote];
125124
}
126125
}

src/system/string.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,12 @@ export namespace Strings {
173173
.digest(encoding);
174174
}
175175

176+
export function splitSingle(s: string, splitter: string) {
177+
const parts = s.split(splitter, 1);
178+
const first = parts[0];
179+
return first.length === s.length ? parts : [first, s.substr(first.length + 1)];
180+
}
181+
176182
export function truncate(s: string, truncateTo: number, ellipsis: string = '\u2026', width?: number) {
177183
if (!s) return s;
178184

0 commit comments

Comments
 (0)