Adds diffWithRevision command

This commit is contained in:
Eric Amodio
2017-06-27 01:22:23 -04:00
parent 1751987868
commit 3bf5f23c66
8 changed files with 222 additions and 122 deletions

View File

@@ -103,6 +103,8 @@ GitLens provides an unobtrusive blame annotation at the end of the current line,
- Adds a `Compare Line Commit with Previous` command (`gitlens.diffLineWithPrevious`) with a shortcut of `shift+alt+,` to compare the active file/diff with the previous line commit revision - Adds a `Compare Line Commit with Previous` command (`gitlens.diffLineWithPrevious`) with a shortcut of `shift+alt+,` to compare the active file/diff with the previous line commit revision
- Adds a `Compare File with Revision...` command (`gitlens.diffWithRevision`) to compare the active file with the selected revision of the same file
- Adds a `Compare File with Working Tree` command (`gitlens.diffWithWorking`) with a shortcut of `shift+alt+w` to compare the most recent commit revision of the active file/diff with the working tree - Adds a `Compare File with Working Tree` command (`gitlens.diffWithWorking`) with a shortcut of `shift+alt+w` to compare the most recent commit revision of the active file/diff with the working tree
- Adds a `Compare Line Commit with Working Tree` command (`gitlens.diffLineWithWorking`) with a shortcut of `alt+w` to compare the commit revision of the active line with the working tree - Adds a `Compare Line Commit with Working Tree` command (`gitlens.diffLineWithWorking`) with a shortcut of `alt+w` to compare the commit revision of the active line with the working tree

View File

@@ -769,6 +769,11 @@
"title": "Compare Line Commit with Previous", "title": "Compare Line Commit with Previous",
"category": "GitLens" "category": "GitLens"
}, },
{
"command": "gitlens.diffWithRevision",
"title": "Compare File with Revision...",
"category": "GitLens"
},
{ {
"command": "gitlens.diffWithWorking", "command": "gitlens.diffWithWorking",
"title": "Compare File with Working Tree", "title": "Compare File with Working Tree",
@@ -950,6 +955,10 @@
"command": "gitlens.diffLineWithPrevious", "command": "gitlens.diffLineWithPrevious",
"when": "gitlens:isBlameable" "when": "gitlens:isBlameable"
}, },
{
"command": "gitlens.diffWithRevision",
"when": "gitlens:isTracked"
},
{ {
"command": "gitlens.diffWithWorking", "command": "gitlens.diffWithWorking",
"when": "gitlens:isTracked" "when": "gitlens:isTracked"

View File

@@ -10,6 +10,7 @@ export * from './commands/diffLineWithWorking';
export * from './commands/diffWithBranch'; export * from './commands/diffWithBranch';
export * from './commands/diffWithNext'; export * from './commands/diffWithNext';
export * from './commands/diffWithPrevious'; export * from './commands/diffWithPrevious';
export * from './commands/diffWithRevision';
export * from './commands/diffWithWorking'; export * from './commands/diffWithWorking';
export * from './commands/openChangedFiles'; export * from './commands/openChangedFiles';
export * from './commands/openBranchInRemote'; export * from './commands/openBranchInRemote';

View File

@@ -11,6 +11,7 @@ export type Commands = 'gitlens.closeUnchangedFiles' |
'gitlens.diffWithNext' | 'gitlens.diffWithNext' |
'gitlens.diffWithPrevious' | 'gitlens.diffWithPrevious' |
'gitlens.diffLineWithPrevious' | 'gitlens.diffLineWithPrevious' |
'gitlens.diffWithRevision' |
'gitlens.diffWithWorking' | 'gitlens.diffWithWorking' |
'gitlens.diffLineWithWorking' | 'gitlens.diffLineWithWorking' |
'gitlens.openChangedFiles' | 'gitlens.openChangedFiles' |
@@ -49,6 +50,7 @@ export const Commands = {
DiffWithNext: 'gitlens.diffWithNext' as Commands, DiffWithNext: 'gitlens.diffWithNext' as Commands,
DiffWithPrevious: 'gitlens.diffWithPrevious' as Commands, DiffWithPrevious: 'gitlens.diffWithPrevious' as Commands,
DiffLineWithPrevious: 'gitlens.diffLineWithPrevious' as Commands, DiffLineWithPrevious: 'gitlens.diffLineWithPrevious' as Commands,
DiffWithRevision: 'gitlens.diffWithRevision' as Commands,
DiffWithWorking: 'gitlens.diffWithWorking' as Commands, DiffWithWorking: 'gitlens.diffWithWorking' as Commands,
DiffLineWithWorking: 'gitlens.diffLineWithWorking' as Commands, DiffLineWithWorking: 'gitlens.diffLineWithWorking' as Commands,
OpenChangedFiles: 'gitlens.openChangedFiles' as Commands, OpenChangedFiles: 'gitlens.openChangedFiles' as Commands,

View File

@@ -0,0 +1,81 @@
'use strict';
import { commands, TextDocumentShowOptions, TextEditor, Uri, window } from 'vscode';
import { ActiveEditorCommand, CommandContext, Commands, getCommandUri } from './common';
import { BuiltInCommands, GlyphChars } from '../constants';
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;
maxCount?: number;
showOptions?: TextDocumentShowOptions;
}
export class DiffWithRevisionCommand extends ActiveEditorCommand {
constructor(private git: GitService) {
super(Commands.DiffWithRevision);
}
async run(context: CommandContext, args: DiffWithRevisionCommandArgs = {}): Promise<any> {
// Since we can change the args and they could be cached -- make a copy
switch (context.type) {
case 'uri':
return this.execute(context.editor, context.uri, { ...args });
case 'scm-states':
const resource = context.scmResourceStates[0];
return this.execute(undefined, resource.resourceUri, { ...args });
case 'scm-groups':
return undefined;
default:
return this.execute(context.editor, undefined, { ...args });
}
}
async execute(editor: TextEditor | undefined, uri?: Uri, args: DiffWithRevisionCommandArgs = {}): Promise<any> {
uri = getCommandUri(uri, editor);
if (uri === undefined) return undefined;
if (args.line === undefined) {
args.line = editor === undefined ? 0 : editor.selection.active.line;
}
const gitUri = await GitUri.fromUri(uri, this.git);
if (args.maxCount == null) {
args.maxCount = this.git.config.advanced.maxQuickHistory;
}
const progressCancellation = FileHistoryQuickPick.showProgress(gitUri);
try {
const log = await this.git.getLogForFile(gitUri.repoPath, gitUri.fsPath, gitUri.sha, { maxCount: args.maxCount });
if (log === undefined) return Messages.showFileNotUnderSourceControlWarningMessage('Unable to open history compare');
if (progressCancellation.token.isCancellationRequested) return undefined;
const pick = await FileHistoryQuickPick.show(this.git, log, gitUri, progressCancellation, { pickerOnly: true });
if (pick === undefined) return undefined;
if (pick instanceof CommandQuickPickItem) return pick.execute();
const compare = await this.git.getVersionedFile(gitUri.repoPath, gitUri.fsPath, pick.commit.sha);
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);
if (args.line === undefined || args.line === 0) return undefined;
// TODO: Figure out how to focus the left pane
return await commands.executeCommand(BuiltInCommands.RevealLine, { lineNumber: args.line, at: 'center' });
}
catch (ex) {
Logger.error(ex, 'DiffWithRevisionCommand', 'getVersionedFile');
return window.showErrorMessage(`Unable to open history compare. See output channel for more details`);
}
}
}

View File

@@ -59,7 +59,7 @@ export class ShowQuickFileHistoryCommand extends ActiveEditorCachedCommand {
if (progressCancellation.token.isCancellationRequested) return undefined; if (progressCancellation.token.isCancellationRequested) return undefined;
const pick = await FileHistoryQuickPick.show(this.git, args.log, gitUri, progressCancellation, args.goBackCommand, args.nextPageCommand); const pick = await FileHistoryQuickPick.show(this.git, args.log, gitUri, progressCancellation, { goBackCommand: args.goBackCommand, nextPageCommand: args.nextPageCommand });
if (pick === undefined) return undefined; if (pick === undefined) return undefined;
if (pick instanceof CommandQuickPickItem) return pick.execute(); if (pick instanceof CommandQuickPickItem) return pick.execute();

View File

@@ -5,7 +5,7 @@ import { AnnotationController } from './annotations/annotationController';
import { CloseUnchangedFilesCommand, OpenChangedFilesCommand } from './commands'; import { CloseUnchangedFilesCommand, OpenChangedFilesCommand } from './commands';
import { OpenBranchInRemoteCommand, OpenCommitInRemoteCommand, OpenFileInRemoteCommand, OpenInRemoteCommand, OpenRepoInRemoteCommand } from './commands'; import { OpenBranchInRemoteCommand, OpenCommitInRemoteCommand, OpenFileInRemoteCommand, OpenInRemoteCommand, OpenRepoInRemoteCommand } from './commands';
import { CopyMessageToClipboardCommand, CopyShaToClipboardCommand } from './commands'; import { CopyMessageToClipboardCommand, CopyShaToClipboardCommand } from './commands';
import { DiffDirectoryCommand, DiffLineWithPreviousCommand, DiffLineWithWorkingCommand, DiffWithBranchCommand, DiffWithNextCommand, DiffWithPreviousCommand, DiffWithWorkingCommand} from './commands'; import { DiffDirectoryCommand, DiffLineWithPreviousCommand, DiffLineWithWorkingCommand, DiffWithBranchCommand, DiffWithNextCommand, DiffWithPreviousCommand, DiffWithRevisionCommand, DiffWithWorkingCommand} from './commands';
import { ResetSuppressedWarningsCommand } from './commands'; import { ResetSuppressedWarningsCommand } from './commands';
import { ShowFileBlameCommand, ShowLineBlameCommand, ToggleFileBlameCommand, ToggleFileRecentChangesCommand, ToggleLineBlameCommand } from './commands'; import { ShowFileBlameCommand, ShowLineBlameCommand, ToggleFileBlameCommand, ToggleFileRecentChangesCommand, ToggleLineBlameCommand } from './commands';
import { ShowBlameHistoryCommand, ShowFileHistoryCommand } from './commands'; import { ShowBlameHistoryCommand, ShowFileHistoryCommand } from './commands';
@@ -97,6 +97,7 @@ export async function activate(context: ExtensionContext) {
context.subscriptions.push(new DiffWithBranchCommand(git)); context.subscriptions.push(new DiffWithBranchCommand(git));
context.subscriptions.push(new DiffWithNextCommand(git)); context.subscriptions.push(new DiffWithNextCommand(git));
context.subscriptions.push(new DiffWithPreviousCommand(git)); context.subscriptions.push(new DiffWithPreviousCommand(git));
context.subscriptions.push(new DiffWithRevisionCommand(git));
context.subscriptions.push(new DiffWithWorkingCommand(git)); context.subscriptions.push(new DiffWithWorkingCommand(git));
context.subscriptions.push(new OpenBranchInRemoteCommand(git)); context.subscriptions.push(new OpenBranchInRemoteCommand(git));
context.subscriptions.push(new OpenCommitInRemoteCommand(git)); context.subscriptions.push(new OpenCommitInRemoteCommand(git));

View File

@@ -20,7 +20,9 @@ export class FileHistoryQuickPick {
}); });
} }
static async show(git: GitService, log: GitLog, uri: GitUri, progressCancellation: CancellationTokenSource, goBackCommand?: CommandQuickPickItem, nextPageCommand?: CommandQuickPickItem): Promise<CommitQuickPickItem | CommandQuickPickItem | undefined> { static async show(git: GitService, log: GitLog, uri: GitUri, progressCancellation: CancellationTokenSource, options: { goBackCommand?: CommandQuickPickItem, nextPageCommand?: CommandQuickPickItem, pickerOnly?: boolean } = {}): Promise<CommitQuickPickItem | CommandQuickPickItem | undefined> {
options = { ...{ pickerOnly: false }, ...options };
const items = Array.from(Iterables.map(log.commits.values(), c => new CommitQuickPickItem(c))) as (CommitQuickPickItem | CommandQuickPickItem)[]; const items = Array.from(Iterables.map(log.commits.values(), c => new CommitQuickPickItem(c))) as (CommitQuickPickItem | CommandQuickPickItem)[];
let previousPageCommand: CommandQuickPickItem | undefined = undefined; let previousPageCommand: CommandQuickPickItem | undefined = undefined;
@@ -36,7 +38,7 @@ export class FileHistoryQuickPick {
Uri.file(uri.fsPath), Uri.file(uri.fsPath),
{ {
maxCount: 0, maxCount: 0,
goBackCommand goBackCommand: options.goBackCommand
} as ShowQuickFileHistoryCommandArgs } as ShowQuickFileHistoryCommandArgs
])); ]));
} }
@@ -59,7 +61,7 @@ export class FileHistoryQuickPick {
log: log, log: log,
maxCount: log.maxCount, maxCount: log.maxCount,
range: log.range, range: log.range,
goBackCommand goBackCommand: options.goBackCommand
} as ShowQuickFileHistoryCommandArgs } as ShowQuickFileHistoryCommandArgs
]) ])
} as ShowQuickFileHistoryCommandArgs } as ShowQuickFileHistoryCommandArgs
@@ -67,9 +69,9 @@ export class FileHistoryQuickPick {
} }
} }
if (nextPageCommand) { if (options.nextPageCommand) {
index++; index++;
items.splice(0, 0, nextPageCommand); items.splice(0, 0, options.nextPageCommand);
} }
if (log.truncated) { if (log.truncated) {
@@ -80,8 +82,8 @@ export class FileHistoryQuickPick {
uri, uri,
{ {
maxCount: log.maxCount, maxCount: log.maxCount,
goBackCommand, goBackCommand: options.goBackCommand,
nextPageCommand nextPageCommand: options.nextPageCommand
} as ShowQuickFileHistoryCommandArgs } as ShowQuickFileHistoryCommandArgs
]); ]);
@@ -94,7 +96,7 @@ export class FileHistoryQuickPick {
new GitUri(uri, last), new GitUri(uri, last),
{ {
maxCount: log.maxCount, maxCount: log.maxCount,
goBackCommand, goBackCommand: options.goBackCommand,
nextPageCommand: npc nextPageCommand: npc
} as ShowQuickFileHistoryCommandArgs } as ShowQuickFileHistoryCommandArgs
]); ]);
@@ -105,6 +107,7 @@ export class FileHistoryQuickPick {
} }
} }
if (!options.pickerOnly) {
const branch = await git.getBranch(uri.repoPath!); const branch = await git.getBranch(uri.repoPath!);
const currentCommand = new CommandQuickPickItem({ const currentCommand = new CommandQuickPickItem({
@@ -120,7 +123,7 @@ export class FileHistoryQuickPick {
]); ]);
// Only show the full repo option if we are the root // Only show the full repo option if we are the root
if (goBackCommand === undefined) { if (options.goBackCommand === undefined) {
items.splice(index++, 0, new CommandQuickPickItem({ items.splice(index++, 0, new CommandQuickPickItem({
label: `$(history) Show Branch History`, label: `$(history) Show Branch History`,
description: `${Strings.pad(GlyphChars.Dash, 2, 3)} shows ${GlyphChars.Space}$(git-branch) ${branch!.name} history` description: `${Strings.pad(GlyphChars.Dash, 2, 3)} shows ${GlyphChars.Space}$(git-branch) ${branch!.name} history`
@@ -143,16 +146,17 @@ export class FileHistoryQuickPick {
} as RemoteResource, currentCommand)); } as RemoteResource, currentCommand));
} }
if (goBackCommand) { if (options.goBackCommand) {
items.splice(0, 0, goBackCommand); items.splice(0, 0, options.goBackCommand);
}
} }
if (progressCancellation.token.isCancellationRequested) return undefined; if (progressCancellation.token.isCancellationRequested) return undefined;
const scope = await Keyboard.instance.beginScope({ const scope = await Keyboard.instance.beginScope({
left: goBackCommand, left: options.goBackCommand,
',': previousPageCommand, ',': previousPageCommand,
'.': nextPageCommand '.': options.nextPageCommand
}); });
const commit = Iterables.first(log.commits.values()); const commit = Iterables.first(log.commits.values());