Skip to content

Commit

Permalink
Fixes #695 - invalid url w/ remote in branch name
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed Apr 8, 2019
1 parent fb36df4 commit 6158ff4
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 34 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
64 changes: 39 additions & 25 deletions src/commands/openFileInRemote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 =
Expand All @@ -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:
Expand Down
17 changes: 8 additions & 9 deletions src/commands/openInRemote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
}
6 changes: 6 additions & 0 deletions src/system/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 6158ff4

Please sign in to comment.