diff --git a/extensions/git/src/git.ts b/extensions/git/src/git.ts index b748675b56d..c1fc82cb489 100644 --- a/extensions/git/src/git.ts +++ b/extensions/git/src/git.ts @@ -751,6 +751,27 @@ export class Repository { return { mode, object, size: parseInt(size) }; } + async lstreeOutput(treeish: string, path: string): Promise { + if (!treeish) { // index + const { stdout } = await this.run(['ls-files', '--stage', '--', path]); + return stdout; + } + + const { stdout } = await this.run(['ls-tree', '-l', treeish, '--', path]); + return stdout; + } + + async relativePathToGitRelativePath(treeish: string, path: string): Promise { + let gitPath: string = path; + const pathPrefix = path.substring(0, path.lastIndexOf('/')); + const lstOutput = await this.lstreeOutput(treeish, pathPrefix + '/'); + const findResult = lstOutput.toUpperCase().indexOf(path.toUpperCase()); + if (findResult) { + gitPath = lstOutput.substr(findResult, path.length); + } + return gitPath; + } + async detectObjectType(object: string): Promise<{ mimetype: string, encoding?: string }> { const child = await this.stream(['show', object]); const buffer = await readBytes(child.stdout, 4100); diff --git a/extensions/git/src/repository.ts b/extensions/git/src/repository.ts index e1edda82e80..fa1daf8b36c 100644 --- a/extensions/git/src/repository.ts +++ b/extensions/git/src/repository.ts @@ -876,12 +876,17 @@ export class Repository implements Disposable { } async show(ref: string, filePath: string): Promise { - return this.run(Operation.Show, () => { - const relativePath = path.relative(this.repository.root, filePath).replace(/\\/g, '/'); + return this.run(Operation.Show, async () => { + let relativePath = path.relative(this.repository.root, filePath).replace(/\\/g, '/'); const configFiles = workspace.getConfiguration('files', Uri.file(filePath)); const defaultEncoding = configFiles.get('encoding'); const autoGuessEncoding = configFiles.get('autoGuessEncoding'); + if (ref === '') { + ref = 'HEAD'; + } + + relativePath = await this.repository.relativePathToGitRelativePath(ref, relativePath); return this.repository.bufferString(`${ref}:${relativePath}`, defaultEncoding, autoGuessEncoding); }); }