diff --git a/src/commands/diffWithBranch.ts b/src/commands/diffWithBranch.ts index b4c2f60..d61c197 100644 --- a/src/commands/diffWithBranch.ts +++ b/src/commands/diffWithBranch.ts @@ -1,7 +1,8 @@ 'use strict'; -import { commands, Range, TextDocumentShowOptions, TextEditor, Uri, window } from 'vscode'; +import { commands, TextDocumentShowOptions, TextEditor, Uri, window } from 'vscode'; import { ActiveEditorCommand, Commands, getCommandUri } from './common'; -import { BuiltInCommands, GlyphChars } from '../constants'; +import { GlyphChars } from '../constants'; +import { DiffWithCommandArgs } from './diffWith'; import { GitService, GitUri } from '../gitService'; import { Logger } from '../logger'; import { Messages } from '../messages'; @@ -43,20 +44,21 @@ export class DiffWithBranchCommand extends ActiveEditorCommand { if (branch === undefined) return undefined; try { - const compare = await this.git.getVersionedFile(gitUri.repoPath, gitUri.fsPath, branch); - - if (args.line !== undefined && args.line !== 0) { - if (args.showOptions === undefined) { - args.showOptions = {}; - } - args.showOptions.selection = new Range(args.line, 0, args.line, 0); - } - - await commands.executeCommand(BuiltInCommands.Diff, - Uri.file(compare), - gitUri.fileUri(), - `${path.basename(gitUri.fsPath)} (${branch}) ${GlyphChars.ArrowLeftRight} ${path.basename(gitUri.fsPath)}`, - args.showOptions); + const diffArgs: DiffWithCommandArgs = { + repoPath: gitUri.repoPath, + lhs: { + sha: pick.branch.remote ? `remotes/${branch}` : branch, + uri: gitUri as Uri, + title: `${path.basename(gitUri.fsPath)} (${branch})` + }, + rhs: { + sha: 'HEAD', + uri: gitUri as Uri + }, + line: args.line, + showOptions: args.showOptions + }; + await commands.executeCommand(Commands.DiffWith, diffArgs); } catch (ex) { Logger.error(ex, 'DiffWithBranchCommand', 'getVersionedFile'); diff --git a/src/commands/diffWithRevision.ts b/src/commands/diffWithRevision.ts index 4954a77..d04e459 100644 --- a/src/commands/diffWithRevision.ts +++ b/src/commands/diffWithRevision.ts @@ -1,12 +1,11 @@ 'use strict'; -import { commands, Range, TextDocumentShowOptions, TextEditor, Uri, window } from 'vscode'; +import { commands, TextDocumentShowOptions, TextEditor, Uri, window } from 'vscode'; import { ActiveEditorCommand, Commands, getCommandUri } from './common'; -import { BuiltInCommands, GlyphChars } from '../constants'; +import { DiffWithCommandArgs } from './diffWith'; import { GitService, GitUri } from '../gitService'; import { Logger } from '../logger'; import { Messages } from '../messages'; import { CommandQuickPickItem, FileHistoryQuickPick } from '../quickPicks'; -import * as path from 'path'; export interface DiffWithRevisionCommandArgs { line?: number; @@ -46,20 +45,20 @@ export class DiffWithRevisionCommand extends ActiveEditorCommand { if (pick instanceof CommandQuickPickItem) return pick.execute(); - const compare = await this.git.getVersionedFile(gitUri.repoPath, gitUri.fsPath, pick.commit.sha); - - if (args.line !== undefined && args.line !== 0) { - if (args.showOptions === undefined) { - args.showOptions = {}; - } - args.showOptions.selection = new Range(args.line, 0, args.line, 0); - } - - await commands.executeCommand(BuiltInCommands.Diff, - Uri.file(compare), - gitUri.fileUri(), - `${path.basename(gitUri.fsPath)} (${pick.commit.shortSha}) ${GlyphChars.ArrowLeftRight} ${path.basename(gitUri.fsPath)}`, - args.showOptions); + const diffArgs: DiffWithCommandArgs = { + repoPath: gitUri.repoPath, + lhs: { + sha: pick.commit.sha, + uri: gitUri as Uri + }, + rhs: { + sha: 'HEAD', + uri: gitUri as Uri + }, + line: args.line, + showOptions: args.showOptions + }; + await commands.executeCommand(Commands.DiffWith, diffArgs); } catch (ex) { Logger.error(ex, 'DiffWithRevisionCommand', 'getVersionedFile'); diff --git a/src/git/git.ts b/src/git/git.ts index e699ef9..495e581 100644 --- a/src/git/git.ts +++ b/src/git/git.ts @@ -99,7 +99,8 @@ export class Git { static async getVersionedFile(repoPath: string | undefined, fileName: string, branchOrSha: string) { const data = await Git.show(repoPath, fileName, branchOrSha, 'binary'); - const suffix = Git.isSha(branchOrSha) ? Git.shortenSha(branchOrSha) : branchOrSha; + // TODO: Sanitize the filename + const suffix = Git.isSha(branchOrSha) ? Git.shortenSha(branchOrSha) : branchOrSha.replace(/\\/g, '_').replace(/\//g, '_'); const ext = path.extname(fileName); return new Promise((resolve, reject) => { tmp.file({ prefix: `${path.basename(fileName, ext)}-${suffix}__`, postfix: ext },