diff --git a/src/commands.ts b/src/commands.ts index 1cb77d8..eb0834b 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -43,7 +43,7 @@ export class DiffWithPreviousCommand extends EditorCommand { super(Commands.DiffWithPrevious); } - execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri, sha?: string, shaUri?: Uri, compareWithSha?: string, compareWithUri?: Uri, line?: number) { + execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri, repoPath?: string, sha?: string, shaUri?: Uri, compareWithSha?: string, compareWithUri?: Uri, line?: number) { line = line || editor.selection.active.line; if (!sha) { return this.git.getBlameForLine(uri.fsPath, line) @@ -52,9 +52,9 @@ export class DiffWithPreviousCommand extends EditorCommand { if (!blame) return; if (UncommitedRegex.test(blame.commit.sha)) { - return commands.executeCommand(Commands.DiffWithWorking, uri, blame.commit.previousSha, blame.commit.previousUri, line); + return commands.executeCommand(Commands.DiffWithWorking, uri, blame.commit.repoPath, blame.commit.previousSha, blame.commit.previousUri, line); } - return commands.executeCommand(Commands.DiffWithPrevious, uri, blame.commit.sha, blame.commit.uri, blame.commit.previousSha, blame.commit.previousUri, line); + return commands.executeCommand(Commands.DiffWithPrevious, uri, blame.commit.repoPath, blame.commit.sha, blame.commit.uri, blame.commit.previousSha, blame.commit.previousUri, line); }); } @@ -62,7 +62,7 @@ export class DiffWithPreviousCommand extends EditorCommand { return window.showInformationMessage(`Commit ${sha} has no previous commit`); } - return Promise.all([this.git.getVersionedFile(shaUri.fsPath, sha), this.git.getVersionedFile(compareWithUri.fsPath, compareWithSha)]) + return Promise.all([this.git.getVersionedFile(shaUri.fsPath, repoPath, sha), this.git.getVersionedFile(compareWithUri.fsPath, repoPath, compareWithSha)]) .catch(ex => console.error('[GitLens.DiffWithPreviousCommand]', 'getVersionedFile', ex)) .then(values => commands.executeCommand(BuiltInCommands.Diff, Uri.file(values[1]), Uri.file(values[0]), `${path.basename(compareWithUri.fsPath)} (${compareWithSha}) ↔ ${path.basename(shaUri.fsPath)} (${sha})`) .then(() => commands.executeCommand(BuiltInCommands.RevealLine, {lineNumber: line, at: 'center'}))); @@ -74,7 +74,7 @@ export class DiffWithWorkingCommand extends EditorCommand { super(Commands.DiffWithWorking); } - execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri, sha?: string, shaUri?: Uri, line?: number) { + execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri, repoPath?: string, sha?: string, shaUri?: Uri, line?: number) { line = line || editor.selection.active.line; if (!sha) { return this.git.getBlameForLine(uri.fsPath, line) @@ -83,13 +83,13 @@ export class DiffWithWorkingCommand extends EditorCommand { if (!blame) return; if (UncommitedRegex.test(blame.commit.sha)) { - return commands.executeCommand(Commands.DiffWithWorking, uri, blame.commit.previousSha, blame.commit.previousUri, line); + return commands.executeCommand(Commands.DiffWithWorking, uri, blame.commit.repoPath, blame.commit.previousSha, blame.commit.previousUri, line); } - return commands.executeCommand(Commands.DiffWithWorking, uri, blame.commit.sha, blame.commit.uri, line) + return commands.executeCommand(Commands.DiffWithWorking, uri, blame.commit.repoPath, blame.commit.sha, blame.commit.uri, line) }); }; - return this.git.getVersionedFile(shaUri.fsPath, sha) + return this.git.getVersionedFile(shaUri.fsPath, repoPath, sha) .catch(ex => console.error('[GitLens.DiffWithWorkingCommand]', 'getVersionedFile', ex)) .then(compare => commands.executeCommand(BuiltInCommands.Diff, Uri.file(compare), uri, `${path.basename(shaUri.fsPath)} (${sha}) ↔ ${path.basename(uri.fsPath)} (index)`) .then(() => commands.executeCommand(BuiltInCommands.RevealLine, {lineNumber: line, at: 'center'}))); diff --git a/src/git/git.ts b/src/git/git.ts index 0429351..c063d06 100644 --- a/src/git/git.ts +++ b/src/git/git.ts @@ -37,20 +37,24 @@ export default class Git { return fileName.replace(/\\/g, '/'); } - static splitPath(fileName: string) { + static splitPath(fileName: string, repoPath?: string) { // if (!path.isAbsolute(fileName)) { // console.error('[GitLens]', `Git.splitPath(${fileName}) is not an absolute path!`); // debugger; // } - return [path.basename(fileName).replace(/\\/g, '/'), path.dirname(fileName).replace(/\\/g, '/')]; + if (repoPath) { + return [fileName.replace(`${repoPath}/`, ''), repoPath]; + } else { + return [path.basename(fileName).replace(/\\/g, '/'), path.dirname(fileName).replace(/\\/g, '/')]; + } } static repoPath(cwd: string) { return gitCommand(cwd, 'rev-parse', '--show-toplevel').then(data => data.replace(/\r?\n|\r/g, '').replace(/\\/g, '/')); } - static blame(format: GitBlameFormat, fileName: string, sha?: string) { - const [file, root] = Git.splitPath(Git.normalizePath(fileName)); + static blame(format: GitBlameFormat, fileName: string, repoPath?: string, sha?: string) { + const [file, root] = Git.splitPath(Git.normalizePath(fileName), repoPath); if (sha) { return gitCommand(root, 'blame', format, '--root', `${sha}^`, '--', file); @@ -58,9 +62,9 @@ export default class Git { return gitCommand(root, 'blame', format, '--root', '--', file); } - static getVersionedFile(fileName: string, sha: string) { + static getVersionedFile(fileName: string, repoPath: string, sha: string) { return new Promise((resolve, reject) => { - Git.getVersionedFileText(fileName, sha).then(data => { + Git.getVersionedFileText(fileName, repoPath, sha).then(data => { const ext = path.extname(fileName); tmp.file({ prefix: `${path.basename(fileName, ext)}-${sha}_`, postfix: ext }, (err, destination, fd, cleanupCallback) => { if (err) { @@ -81,8 +85,8 @@ export default class Git { }); } - static getVersionedFileText(fileName: string, sha: string) { - const [file, root] = Git.splitPath(Git.normalizePath(fileName)); + static getVersionedFileText(fileName: string, repoPath: string, sha: string) { + const [file, root] = Git.splitPath(Git.normalizePath(fileName), repoPath); sha = sha.replace('^', ''); return gitCommand(root, 'show', `${sha}:./${file}`); diff --git a/src/gitBlameCodeLensProvider.ts b/src/gitBlameCodeLensProvider.ts index 81eb77c..a70f589 100644 --- a/src/gitBlameCodeLensProvider.ts +++ b/src/gitBlameCodeLensProvider.ts @@ -68,7 +68,11 @@ export default class GitBlameCodeLensProvider implements CodeLensProvider { lens.command = { title: `Compare with Working Tree`, command: Commands.DiffWithWorking, - arguments: [Uri.file(lens.fileName), lens.commit.sha, lens.commit.uri, lens.range.start.line] + arguments: [ + Uri.file(lens.fileName), + lens.commit.sha, + lens.commit.uri, + lens.range.start.line] }; return Promise.resolve(lens); } @@ -77,7 +81,14 @@ export default class GitBlameCodeLensProvider implements CodeLensProvider { lens.command = { title: `Compare with Previous (${lens.commit.previousSha})`, command: Commands.DiffWithPrevious, - arguments: [Uri.file(lens.fileName), lens.commit.sha, lens.commit.uri, lens.commit.previousSha, lens.commit.previousUri, lens.range.start.line] + arguments: [ + Uri.file(lens.fileName), + lens.commit.repoPath, + lens.commit.sha, + lens.commit.uri, + lens.commit.previousSha, + lens.commit.previousUri, + lens.range.start.line] }; return Promise.resolve(lens); } diff --git a/src/gitBlameContentProvider.ts b/src/gitBlameContentProvider.ts index c4b093a..2d0fdcc 100644 --- a/src/gitBlameContentProvider.ts +++ b/src/gitBlameContentProvider.ts @@ -51,7 +51,7 @@ export default class GitBlameContentProvider implements TextDocumentContentProvi //const editor = this._findEditor(Uri.file(join(data.repoPath, data.file))); - return this.git.getVersionedFileText(data.originalFileName || data.fileName, data.sha).then(text => { + return this.git.getVersionedFileText(data.originalFileName || data.fileName, data.sha, data.repoPath).then(text => { this.update(uri); // TODO: This only works on the first load -- not after since it is cached diff --git a/src/gitCodeActionProvider.ts b/src/gitCodeActionProvider.ts index 6851380..08764e3 100644 --- a/src/gitCodeActionProvider.ts +++ b/src/gitCodeActionProvider.ts @@ -37,6 +37,7 @@ export default class GitCodeActionProvider implements CodeActionProvider { command: Commands.DiffWithPrevious, arguments: [ Uri.file(document.fileName), + blame.commit.repoPath, blame.commit.sha, blame.commit.uri, blame.commit.previousSha, blame.commit.previousUri, blame.line.line diff --git a/src/gitContentProvider.ts b/src/gitContentProvider.ts index dedbf0d..89a6e27 100644 --- a/src/gitContentProvider.ts +++ b/src/gitContentProvider.ts @@ -10,6 +10,6 @@ export default class GitContentProvider implements TextDocumentContentProvider { provideTextDocumentContent(uri: Uri): string | Thenable { const data = GitProvider.fromGitUri(uri); - return this.git.getVersionedFileText(data.originalFileName || data.fileName, data.sha); + return this.git.getVersionedFileText(data.originalFileName || data.fileName, data.repoPath, data.sha); } } \ No newline at end of file diff --git a/src/gitProvider.ts b/src/gitProvider.ts index 9d7fc49..fcb88ac 100644 --- a/src/gitProvider.ts +++ b/src/gitProvider.ts @@ -292,12 +292,12 @@ export default class GitProvider extends Disposable { }); } - getVersionedFile(fileName: string, sha: string) { - return Git.getVersionedFile(fileName, sha); + getVersionedFile(fileName: string, repoPath: string, sha: string) { + return Git.getVersionedFile(fileName, repoPath, sha); } - getVersionedFileText(fileName: string, sha: string) { - return Git.getVersionedFileText(fileName, sha); + getVersionedFileText(fileName: string, repoPath: string, sha: string) { + return Git.getVersionedFileText(fileName, repoPath, sha); } static fromBlameUri(uri: Uri): IGitBlameUriData { @@ -336,7 +336,7 @@ export default class GitProvider extends Disposable { private static _toGitUriData(commit: IGitCommit, index: number, originalFileName?: string): T { const fileName = Git.normalizePath(path.join(commit.repoPath, commit.fileName)); - const data = { fileName: fileName, sha: commit.sha, index: index } as T; + const data = { repoPath: commit.repoPath, fileName: fileName, sha: commit.sha, index: index } as T; if (originalFileName) { data.originalFileName = Git.normalizePath(path.join(commit.repoPath, originalFileName)); } @@ -351,12 +351,13 @@ export default class GitProvider extends Disposable { } export interface IGitUriData { - fileName: string, + repoPath: string; + fileName: string; originalFileName?: string; - sha: string, - index: number + sha: string; + index: number; } export interface IGitBlameUriData extends IGitUriData { - range: Range + range: Range; } \ No newline at end of file