Skip to content

Commit

Permalink
Fixes #208 - UNC paths fail
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed Dec 30, 2017
1 parent 6ee848a commit 893a9af
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Changed
- Improves startup performance and reduces package size

### Fixed
- Fixes [#208](https://github.com/eamodio/vscode-gitlens/issues/208) - Gitlens doesn't work over UNC

## [7.1.0] - 2017-12-22
### Added
- Adds `Open Working File` command (`gitlens.openWorkingFile`) - opens the working file for the active file revision -- closes [#236](https://github.com/eamodio/vscode-gitlens/issues/236)
Expand Down
39 changes: 35 additions & 4 deletions src/git/gitUri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,20 @@ export interface IGitCommitInfo {
sha?: string;
}

// Taken from https://github.com/Microsoft/vscode/blob/master/src/vs/base/common/uri.ts#L331-L337
interface UriComponents {
scheme: string;
authority: string;
path: string;
query: string;
fragment: string;
}

interface UriEx {
new(): Uri;
new(scheme: string, authority: string, path: string, query: string, fragment: string): Uri;
// Use this ctor, because vscode doesn't validate it
new(components: UriComponents): Uri;
}

export class GitUri extends ((Uri as any) as UriEx) {
Expand All @@ -33,7 +44,9 @@ export class GitUri extends ((Uri as any) as UriEx) {

if (uri.scheme === DocumentSchemes.GitLensGit) {
const data: IUriRevisionData = JSON.parse(uri.query);
super(uri.scheme, uri.authority, path.resolve(data.repoPath, data.fileName), uri.query, uri.fragment);

const [authority, fsPath] = GitUri.ensureValidUNCPath(uri.authority, path.resolve(data.repoPath, data.fileName));
super({ scheme: uri.scheme, authority: authority, path: fsPath, query: uri.query, fragment: uri.fragment });

this.repoPath = data.repoPath;
if (GitService.isStagedUncommitted(data.sha) || !GitService.isUncommitted(data.sha)) {
Expand All @@ -44,20 +57,21 @@ export class GitUri extends ((Uri as any) as UriEx) {
}

if (commitOrRepoPath === undefined) {
super(uri.scheme, uri.authority, uri.path, uri.query, uri.fragment);
super(uri);

return;
}

if (typeof commitOrRepoPath === 'string') {
super(uri.scheme, uri.authority, uri.path, uri.query, uri.fragment);
super(uri);

this.repoPath = commitOrRepoPath;

return;
}

super(uri.scheme, uri.authority, path.resolve(commitOrRepoPath.repoPath, commitOrRepoPath.fileName || uri.fsPath), uri.query, uri.fragment);
const [authority, fsPath] = GitUri.ensureValidUNCPath(uri.authority, path.resolve(commitOrRepoPath.repoPath, commitOrRepoPath.fileName || uri.fsPath));
super({ scheme: uri.scheme, authority: authority, path: fsPath, query: uri.query, fragment: uri.fragment });

this.repoPath = commitOrRepoPath.repoPath;
if (GitService.isStagedUncommitted(commitOrRepoPath.sha) || !GitService.isUncommitted(commitOrRepoPath.sha)) {
Expand Down Expand Up @@ -96,6 +110,23 @@ export class GitUri extends ((Uri as any) as UriEx) {
return GitService.normalizePath(relativePath);
}

private static ensureValidUNCPath(authority: string, fsPath: string): [string, string] {
// Taken from https://github.com/Microsoft/vscode/blob/master/src/vs/base/common/uri.ts#L239-L251
// check for authority as used in UNC shares or use the path as given
if (fsPath[0] === '\\' && fsPath[1] === '\\') {
const index = fsPath.indexOf('\\', 2);
if (index === -1) {
authority = fsPath.substring(2);
fsPath = '\\';
} else {
authority = fsPath.substring(2, index);
fsPath = fsPath.substring(index) || '\\';
}
}

return [authority, fsPath];
}

static fromCommit(commit: GitCommit, previous: boolean = false) {
if (!previous) return new GitUri(commit.uri, commit);

Expand Down

0 comments on commit 893a9af

Please sign in to comment.