Переглянути джерело

Consolidates path resolving and Uri.file to GitUri

main
Eric Amodio 6 роки тому
джерело
коміт
d8d13c7301
13 змінених файлів з 71 додано та 42 видалено
  1. +6
    -6
      src/codelens/codeLensProvider.ts
  2. +1
    -1
      src/commands/diffWithBranch.ts
  3. +1
    -2
      src/commands/openWorkingFile.ts
  4. +6
    -8
      src/git/gitService.ts
  5. +42
    -11
      src/git/gitUri.ts
  6. +3
    -3
      src/git/models/commit.ts
  7. +2
    -1
      src/git/models/logCommit.ts
  8. +1
    -1
      src/git/models/repository.ts
  9. +2
    -2
      src/git/models/status.ts
  10. +3
    -3
      src/quickpicks/commitFileQuickPick.ts
  11. +1
    -1
      src/quickpicks/fileHistoryQuickPick.ts
  12. +1
    -1
      src/views/nodes/fileHistoryTrackerNode.ts
  13. +2
    -2
      src/views/nodes/statusFileNode.ts

+ 6
- 6
src/codelens/codeLensProvider.ts Переглянути файл

@ -580,7 +580,7 @@ export class GitCodeLensProvider implements CodeLensProvider {
title: title,
command: Commands.DiffWithPrevious,
arguments: [
Uri.file(lens.uri!.fsPath),
lens.uri!.toFileUri(),
{
commit: commit
} as DiffWithPreviousCommandArgs
@ -599,7 +599,7 @@ export class GitCodeLensProvider implements CodeLensProvider {
title: title,
command: commit !== undefined && commit.isUncommitted ? '' : CodeLensCommand.ShowQuickCommitDetails,
arguments: [
Uri.file(lens.uri!.fsPath),
lens.uri!.toFileUri(),
{
commit,
sha: commit === undefined ? undefined : commit.sha
@ -619,7 +619,7 @@ export class GitCodeLensProvider implements CodeLensProvider {
title: title,
command: commit !== undefined && commit.isUncommitted ? '' : CodeLensCommand.ShowQuickCommitFileDetails,
arguments: [
Uri.file(lens.uri!.fsPath),
lens.uri!.toFileUri(),
{
commit,
sha: commit === undefined ? undefined : commit.sha
@ -638,7 +638,7 @@ export class GitCodeLensProvider implements CodeLensProvider {
lens.command = {
title: title,
command: CodeLensCommand.ShowQuickCurrentBranchHistory,
arguments: [Uri.file(lens.uri!.fsPath)]
arguments: [lens.uri!.toFileUri()]
};
return lens;
}
@ -653,7 +653,7 @@ export class GitCodeLensProvider implements CodeLensProvider {
title: title,
command: CodeLensCommand.ShowQuickFileHistory,
arguments: [
Uri.file(lens.uri!.fsPath),
lens.uri!.toFileUri(),
{
range: lens.isFullRange ? undefined : lens.blameRange
} as ShowQuickFileHistoryCommandArgs
@ -670,7 +670,7 @@ export class GitCodeLensProvider implements CodeLensProvider {
lens.command = {
title: title,
command: Commands.ToggleFileBlame,
arguments: [Uri.file(lens.uri!.fsPath)]
arguments: [lens.uri!.toFileUri()]
};
return lens;
}

+ 1
- 1
src/commands/diffWithBranch.ts Переглянути файл

@ -58,7 +58,7 @@ export class DiffWithBranchCommand extends ActiveEditorCommand {
const fileName = Strings.normalizePath(paths.relative(gitUri.repoPath, gitUri.fsPath));
const rename = files.find(s => s.fileName === fileName);
if (rename !== undefined && rename.originalFileName !== undefined) {
renamedUri = Uri.file(paths.join(gitUri.repoPath, rename.originalFileName));
renamedUri = GitUri.resolveToUri(rename.originalFileName, gitUri.repoPath);
renamedTitle = `${paths.basename(rename.originalFileName)} (${ref})`;
}
}

+ 1
- 2
src/commands/openWorkingFile.ts Переглянути файл

@ -1,5 +1,4 @@
'use strict';
import * as paths from 'path';
import { Range, TextDocumentShowOptions, TextEditor, Uri, window } from 'vscode';
import { FileAnnotationType } from '../configuration';
import { Container } from '../container';
@ -44,7 +43,7 @@ export class OpenWorkingFileCommand extends ActiveEditorCommand {
);
}
args.uri = new GitUri(Uri.file(paths.resolve(repoPath || '', fileName)), repoPath);
args.uri = new GitUri(GitUri.resolveToUri(fileName, repoPath), repoPath);
}
}

+ 6
- 8
src/git/gitService.ts Переглянути файл

@ -1420,9 +1420,7 @@ export class GitService implements Disposable {
key += `:reverse`;
}
const doc = await Container.tracker.getOrAdd(
new GitUri(Uri.file(fileName), { repoPath: repoPath!, sha: options.ref })
);
const doc = await Container.tracker.getOrAdd(GitUri.fromFile(fileName, repoPath!, options.ref));
if (this.UseCaching && options.range === undefined) {
if (doc.state !== undefined) {
const cachedLog = doc.state.get<CachedLog>(key);
@ -1628,11 +1626,11 @@ export class GitService implements Disposable {
// If this new repo is inside one of our known roots and we we don't already know about, add it
const root = this._repositoryTree.findSubstr(rp);
let folder = root === undefined ? workspace.getWorkspaceFolder(Uri.file(rp)) : root.folder;
let folder = root === undefined ? workspace.getWorkspaceFolder(GitUri.file(rp)) : root.folder;
if (folder === undefined) {
const parts = rp.split('/');
folder = { uri: Uri.file(rp), name: parts[parts.length - 1], index: this._repositoryTree.count() };
folder = { uri: GitUri.file(rp), name: parts[parts.length - 1], index: this._repositoryTree.count() };
}
Logger.log(cc, `Repository found in '${rp}'`);
@ -1815,13 +1813,13 @@ export class GitService implements Disposable {
if (ref === GitService.deletedOrMissingSha) return undefined;
if (!ref || (Git.isUncommitted(ref) && !Git.isStagedUncommitted(ref))) {
if (await this.fileExists(repoPath!, fileName)) return Uri.file(fileName);
if (await this.fileExists(repoPath!, fileName)) return GitUri.file(fileName);
return undefined;
}
if (Git.isStagedUncommitted(ref)) {
const path = paths.resolve(repoPath || '', fileName);
const path = GitUri.resolve(fileName, repoPath);
return Uri.parse(
`git:${path}?${JSON.stringify({
path: path,
@ -2034,7 +2032,7 @@ export class GitService implements Disposable {
static getEncoding(repoPath: string, fileName: string): string;
static getEncoding(uri: Uri): string;
static getEncoding(repoPathOrUri: string | Uri, fileName?: string): string {
const uri = typeof repoPathOrUri === 'string' ? Uri.file(paths.join(repoPathOrUri, fileName!)) : repoPathOrUri;
const uri = typeof repoPathOrUri === 'string' ? GitUri.resolveToUri(fileName!, repoPathOrUri) : repoPathOrUri;
return Git.getEncoding(workspace.getConfiguration('files', uri).get<string>('encoding'));
}

+ 42
- 11
src/git/gitUri.ts Переглянути файл

@ -87,7 +87,7 @@ export class GitUri extends ((Uri as any) as UriEx) {
const [authority, fsPath] = GitUri.ensureValidUNCPath(
uri.authority,
paths.resolve(commitOrRepoPath.repoPath, commitOrRepoPath.fileName || uri.fsPath)
GitUri.resolve(commitOrRepoPath.fileName || uri.fsPath, commitOrRepoPath.repoPath)
);
let path;
@ -129,9 +129,9 @@ export class GitUri extends ((Uri as any) as UriEx) {
}
documentUri(options: { noSha?: boolean; useVersionedPath?: boolean } = {}) {
if (options.useVersionedPath && this.versionedPath !== undefined) return Uri.file(this.versionedPath);
if (options.useVersionedPath && this.versionedPath !== undefined) return GitUri.file(this.versionedPath);
return this.scheme === 'file' ? Uri.file(!options.noSha && this.sha ? this.path : this.fsPath) : this;
return this.scheme === 'file' ? GitUri.file(!options.noSha && this.sha ? this.path : this.fsPath) : this;
}
equals(uri: Uri | undefined) {
@ -166,6 +166,10 @@ export class GitUri extends ((Uri as any) as UriEx) {
return Strings.normalizePath(relativePath);
}
toFileUri() {
return GitUri.file(this.fsPath);
}
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
@ -187,6 +191,10 @@ export class GitUri extends ((Uri as any) as UriEx) {
return [authority, fsPath];
}
static file(path: string) {
return Uri.file(path);
}
static fromCommit(commit: GitCommit, previous: boolean = false) {
if (!previous) return new GitUri(commit.uri, commit);
@ -196,15 +204,22 @@ export class GitUri extends ((Uri as any) as UriEx) {
});
}
static fromFile(file: GitFile, repoPath: string, ref?: string, original: boolean = false): GitUri {
const uri = Uri.file(paths.resolve(repoPath, (original && file.originalFileName) || file.fileName));
static fromFile(fileName: string, repoPath: string, ref?: string): GitUri;
static fromFile(file: GitFile, repoPath: string, ref?: string, original?: boolean): GitUri;
static fromFile(fileOrName: GitFile | string, repoPath: string, ref?: string, original: boolean = false): GitUri {
const uri = GitUri.resolveToUri(
typeof fileOrName === 'string'
? fileOrName
: (original && fileOrName.originalFileName) || fileOrName.fileName,
repoPath
);
return ref === undefined ? new GitUri(uri, repoPath) : new GitUri(uri, { repoPath: repoPath, sha: ref });
}
static fromRepoPath(repoPath: string, ref?: string) {
return ref === undefined
? new GitUri(Uri.file(repoPath), repoPath)
: new GitUri(Uri.file(repoPath), { repoPath: repoPath, sha: ref });
? new GitUri(GitUri.file(repoPath), repoPath)
: new GitUri(GitUri.file(repoPath), { repoPath: repoPath, sha: ref });
}
static fromRevisionUri(uri: Uri): GitUri {
@ -299,13 +314,29 @@ export class GitUri extends ((Uri as any) as UriEx) {
return Strings.normalizePath(relativePath);
}
static resolve(fileName: string, repoPath?: string) {
const normalizedFileName = Strings.normalizePath(fileName);
if (repoPath === undefined) return normalizedFileName;
const normalizedRepoPath = Strings.normalizePath(repoPath);
if (normalizedFileName.startsWith(normalizedRepoPath)) return normalizedFileName;
return Strings.normalizePath(paths.join(normalizedRepoPath, normalizedFileName));
}
static resolveToUri(fileName: string, repoPath?: string) {
return GitUri.file(this.resolve(fileName, repoPath));
}
static toKey(fileName: string): string;
static toKey(uri: Uri): string;
static toKey(fileNameOrUri: string | Uri): string;
static toKey(fileNameOrUri: string | Uri): string {
return typeof fileNameOrUri === 'string'
? Uri.file(fileNameOrUri).toString(true)
: fileNameOrUri.toString(true);
return Strings.normalizePath(typeof fileNameOrUri === 'string' ? fileNameOrUri : fileNameOrUri.fsPath);
// return typeof fileNameOrUri === 'string'
// ? GitUri.file(fileNameOrUri).toString(true)
// : fileNameOrUri.toString(true);
}
static toRevisionUri(uri: GitUri): Uri;
@ -321,7 +352,7 @@ export class GitUri extends ((Uri as any) as UriEx) {
fileName = fileNameOrFile;
}
else {
fileName = paths.resolve(repoPath!, fileNameOrFile!.fileName);
fileName = GitUri.resolve(fileNameOrFile!.fileName, repoPath);
}
ref = uriOrRef;

+ 3
- 3
src/git/models/commit.ts Переглянути файл

@ -143,15 +143,15 @@ export abstract class GitCommit {
}
get previousUri(): Uri {
return this.previousFileName ? Uri.file(paths.resolve(this.repoPath, this.previousFileName)) : this.uri;
return this.previousFileName ? GitUri.resolveToUri(this.previousFileName, this.repoPath) : this.uri;
}
get uri(): Uri {
return Uri.file(paths.resolve(this.repoPath, this.fileName));
return GitUri.resolveToUri(this.fileName, this.repoPath);
}
get workingUri(): Uri {
return this.workingFileName ? Uri.file(paths.resolve(this.repoPath, this.workingFileName)) : this.uri;
return this.workingFileName ? GitUri.resolveToUri(this.workingFileName, this.repoPath) : this.uri;
}
private _dateFormatter?: Dates.IDateFormatter;

+ 2
- 1
src/git/models/logCommit.ts Переглянути файл

@ -3,6 +3,7 @@ import * as paths from 'path';
import { Uri } from 'vscode';
import { Strings } from '../../system';
import { Git } from '../git';
import { GitUri } from '../gitUri';
import { GitCommit, GitCommitType } from './commit';
import { GitFile, GitFileStatus } from './file';
@ -51,7 +52,7 @@ export class GitLogCommit extends GitCommit {
}
get nextUri(): Uri {
return this.nextFileName ? Uri.file(paths.resolve(this.repoPath, this.nextFileName)) : this.uri;
return this.nextFileName ? GitUri.resolveToUri(this.nextFileName, this.repoPath) : this.uri;
}
get previousFileSha(): string {

+ 1
- 1
src/git/models/repository.ts Переглянути файл

@ -217,7 +217,7 @@ export class Repository implements Disposable {
containsUri(uri: Uri) {
if (uri instanceof GitUri) {
uri = uri.repoPath !== undefined ? Uri.file(uri.repoPath) : uri.documentUri();
uri = uri.repoPath !== undefined ? GitUri.file(uri.repoPath) : uri.documentUri();
}
return this.folder === workspace.getWorkspaceFolder(uri);

+ 2
- 2
src/git/models/status.ts Переглянути файл

@ -1,8 +1,8 @@
'use strict';
import * as paths from 'path';
import { Uri } from 'vscode';
import { GlyphChars } from '../../constants';
import { Strings } from '../../system';
import { GitUri } from '../gitUri';
import { GitBranch } from './branch';
import { GitFile, GitFileStatus } from './file';
@ -153,7 +153,7 @@ export class GitStatusFile implements GitFile {
}
get uri(): Uri {
return Uri.file(paths.resolve(this.repoPath, this.fileName));
return GitUri.resolveToUri(this.fileName, this.repoPath);
}
getFormattedDirectory(includeOriginal: boolean = false): string {

+ 3
- 3
src/quickpicks/commitFileQuickPick.ts Переглянути файл

@ -54,7 +54,7 @@ export class ApplyCommitFileChangesCommandQuickPickItem extends CommandQuickPick
export class OpenCommitFileCommandQuickPickItem extends OpenFileCommandQuickPickItem {
constructor(commit: GitLogCommit, item?: QuickPickItem) {
const uri = Uri.file(paths.resolve(commit.repoPath, commit.fileName));
const uri = GitUri.resolveToUri(commit.fileName, commit.repoPath);
super(
uri,
item || {
@ -152,7 +152,7 @@ export class CommitFileQuickPick {
},
Commands.DiffWithWorking,
[
Uri.file(paths.resolve(commit.repoPath, commit.workingFileName)),
GitUri.resolveToUri(commit.workingFileName, commit.repoPath),
{
commit
} as DiffWithWorkingCommandArgs
@ -246,7 +246,7 @@ export class CommitFileQuickPick {
},
Commands.ShowQuickFileHistory,
[
Uri.file(paths.resolve(commit.repoPath, commit.workingFileName)),
GitUri.resolveToUri(commit.workingFileName, commit.repoPath),
{
fileLog,
goBackCommand: currentCommand

+ 1
- 1
src/quickpicks/fileHistoryQuickPick.ts Переглянути файл

@ -84,7 +84,7 @@ export class FileHistoryQuickPick {
},
Commands.ShowQuickFileHistory,
[
Uri.file(paths.resolve(log.repoPath, workingFileName)),
GitUri.resolveToUri(workingFileName, log.repoPath),
{
goBackCommand: new CommandQuickPickItem(
{

+ 1
- 1
src/views/nodes/fileHistoryTrackerNode.ts Переглянути файл

@ -118,7 +118,7 @@ export class FileHistoryTrackerNode extends SubscribeableViewNode
);
if (fileName !== undefined) {
uri = Uri.file(repoPath !== undefined ? paths.join(repoPath, fileName) : fileName);
uri = GitUri.resolveToUri(fileName, repoPath);
}
}

+ 2
- 2
src/views/nodes/statusFileNode.ts Переглянути файл

@ -77,7 +77,7 @@ export class StatusFileNode extends ViewNode {
}
// Use the file icon and decorations
item.resourceUri = Uri.file(paths.resolve(this.repoPath, this.file.fileName));
item.resourceUri = GitUri.resolveToUri(this.file.fileName, this.repoPath);
item.iconPath = ThemeIcon.File;
item.command = this.getCommand();
@ -96,7 +96,7 @@ export class StatusFileNode extends ViewNode {
}
// Use the file icon and decorations
item.resourceUri = Uri.file(paths.resolve(this.repoPath, this.file.fileName));
item.resourceUri = GitUri.resolveToUri(this.file.fileName, this.repoPath);
item.iconPath = ThemeIcon.File;
}
else {

Завантаження…
Відмінити
Зберегти