mirror of
https://github.com/ckaczor/vscode-gitlens.git
synced 2026-01-25 18:02:01 -05:00
Adds diffWithRevision command
This commit is contained in:
@@ -11,6 +11,7 @@ export type Commands = 'gitlens.closeUnchangedFiles' |
|
||||
'gitlens.diffWithNext' |
|
||||
'gitlens.diffWithPrevious' |
|
||||
'gitlens.diffLineWithPrevious' |
|
||||
'gitlens.diffWithRevision' |
|
||||
'gitlens.diffWithWorking' |
|
||||
'gitlens.diffLineWithWorking' |
|
||||
'gitlens.openChangedFiles' |
|
||||
@@ -49,6 +50,7 @@ export const Commands = {
|
||||
DiffWithNext: 'gitlens.diffWithNext' as Commands,
|
||||
DiffWithPrevious: 'gitlens.diffWithPrevious' as Commands,
|
||||
DiffLineWithPrevious: 'gitlens.diffLineWithPrevious' as Commands,
|
||||
DiffWithRevision: 'gitlens.diffWithRevision' as Commands,
|
||||
DiffWithWorking: 'gitlens.diffWithWorking' as Commands,
|
||||
DiffLineWithWorking: 'gitlens.diffLineWithWorking' as Commands,
|
||||
OpenChangedFiles: 'gitlens.openChangedFiles' as Commands,
|
||||
|
||||
81
src/commands/diffWithRevision.ts
Normal file
81
src/commands/diffWithRevision.ts
Normal 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`);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,30 +1,30 @@
|
||||
'use strict';
|
||||
import { Strings } from '../system';
|
||||
import { commands, Range, TextEditor, Uri, window } from 'vscode';
|
||||
'use strict';
|
||||
import { Strings } from '../system';
|
||||
import { commands, Range, TextEditor, Uri, window } from 'vscode';
|
||||
import { ActiveEditorCachedCommand, CommandContext, Commands, getCommandUri } from './common';
|
||||
import { GlyphChars } from '../constants';
|
||||
import { GitLog, GitService, GitUri } from '../gitService';
|
||||
import { Logger } from '../logger';
|
||||
import { CommandQuickPickItem, FileHistoryQuickPick } from '../quickPicks';
|
||||
import { ShowQuickCommitFileDetailsCommandArgs } from './showQuickCommitFileDetails';
|
||||
import { Messages } from '../messages';
|
||||
import * as path from 'path';
|
||||
|
||||
export interface ShowQuickFileHistoryCommandArgs {
|
||||
log?: GitLog;
|
||||
maxCount?: number;
|
||||
range?: Range;
|
||||
|
||||
goBackCommand?: CommandQuickPickItem;
|
||||
nextPageCommand?: CommandQuickPickItem;
|
||||
}
|
||||
|
||||
export class ShowQuickFileHistoryCommand extends ActiveEditorCachedCommand {
|
||||
|
||||
constructor(private git: GitService) {
|
||||
super(Commands.ShowQuickFileHistory);
|
||||
}
|
||||
|
||||
import { GlyphChars } from '../constants';
|
||||
import { GitLog, GitService, GitUri } from '../gitService';
|
||||
import { Logger } from '../logger';
|
||||
import { CommandQuickPickItem, FileHistoryQuickPick } from '../quickPicks';
|
||||
import { ShowQuickCommitFileDetailsCommandArgs } from './showQuickCommitFileDetails';
|
||||
import { Messages } from '../messages';
|
||||
import * as path from 'path';
|
||||
|
||||
export interface ShowQuickFileHistoryCommandArgs {
|
||||
log?: GitLog;
|
||||
maxCount?: number;
|
||||
range?: Range;
|
||||
|
||||
goBackCommand?: CommandQuickPickItem;
|
||||
nextPageCommand?: CommandQuickPickItem;
|
||||
}
|
||||
|
||||
export class ShowQuickFileHistoryCommand extends ActiveEditorCachedCommand {
|
||||
|
||||
constructor(private git: GitService) {
|
||||
super(Commands.ShowQuickFileHistory);
|
||||
}
|
||||
|
||||
async run(context: CommandContext, args: ShowQuickFileHistoryCommandArgs = {}): Promise<any> {
|
||||
// Since we can change the args and they could be cached -- make a copy
|
||||
switch (context.type) {
|
||||
@@ -41,53 +41,53 @@ export class ShowQuickFileHistoryCommand extends ActiveEditorCachedCommand {
|
||||
}
|
||||
|
||||
async execute(editor: TextEditor | undefined, uri?: Uri, args: ShowQuickFileHistoryCommandArgs = {}) {
|
||||
uri = getCommandUri(uri, editor);
|
||||
if (uri === undefined) return commands.executeCommand(Commands.ShowQuickCurrentBranchHistory);
|
||||
|
||||
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 {
|
||||
if (args.log === undefined) {
|
||||
args.log = await this.git.getLogForFile(gitUri.repoPath, gitUri.fsPath, gitUri.sha, { maxCount: args.maxCount, range: args.range });
|
||||
if (args.log === undefined) return Messages.showFileNotUnderSourceControlWarningMessage('Unable to show file history');
|
||||
}
|
||||
|
||||
if (progressCancellation.token.isCancellationRequested) return undefined;
|
||||
|
||||
const pick = await FileHistoryQuickPick.show(this.git, args.log, gitUri, progressCancellation, args.goBackCommand, args.nextPageCommand);
|
||||
if (pick === undefined) return undefined;
|
||||
|
||||
if (pick instanceof CommandQuickPickItem) return pick.execute();
|
||||
|
||||
// Create a command to get back to where we are right now
|
||||
const currentCommand = new CommandQuickPickItem({
|
||||
label: `go back ${GlyphChars.ArrowBack}`,
|
||||
description: `${Strings.pad(GlyphChars.Dash, 2, 3)} to history of ${GlyphChars.Space}$(file-text) ${path.basename(pick.commit.fileName)}${gitUri.sha ? ` from ${GlyphChars.Space}$(git-commit) ${gitUri.shortSha}` : ''}`
|
||||
}, Commands.ShowQuickFileHistory, [
|
||||
uri,
|
||||
args
|
||||
]);
|
||||
|
||||
return commands.executeCommand(Commands.ShowQuickCommitFileDetails,
|
||||
new GitUri(pick.commit.uri, pick.commit),
|
||||
{
|
||||
commit: pick.commit,
|
||||
fileLog: args.log,
|
||||
sha: pick.commit.sha,
|
||||
goBackCommand: currentCommand
|
||||
} as ShowQuickCommitFileDetailsCommandArgs);
|
||||
}
|
||||
catch (ex) {
|
||||
Logger.error(ex, 'ShowQuickFileHistoryCommand');
|
||||
return window.showErrorMessage(`Unable to show file history. See output channel for more details`);
|
||||
}
|
||||
finally {
|
||||
progressCancellation.dispose();
|
||||
}
|
||||
}
|
||||
uri = getCommandUri(uri, editor);
|
||||
if (uri === undefined) return commands.executeCommand(Commands.ShowQuickCurrentBranchHistory);
|
||||
|
||||
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 {
|
||||
if (args.log === undefined) {
|
||||
args.log = await this.git.getLogForFile(gitUri.repoPath, gitUri.fsPath, gitUri.sha, { maxCount: args.maxCount, range: args.range });
|
||||
if (args.log === undefined) return Messages.showFileNotUnderSourceControlWarningMessage('Unable to show file history');
|
||||
}
|
||||
|
||||
if (progressCancellation.token.isCancellationRequested) return undefined;
|
||||
|
||||
const pick = await FileHistoryQuickPick.show(this.git, args.log, gitUri, progressCancellation, { goBackCommand: args.goBackCommand, nextPageCommand: args.nextPageCommand });
|
||||
if (pick === undefined) return undefined;
|
||||
|
||||
if (pick instanceof CommandQuickPickItem) return pick.execute();
|
||||
|
||||
// Create a command to get back to where we are right now
|
||||
const currentCommand = new CommandQuickPickItem({
|
||||
label: `go back ${GlyphChars.ArrowBack}`,
|
||||
description: `${Strings.pad(GlyphChars.Dash, 2, 3)} to history of ${GlyphChars.Space}$(file-text) ${path.basename(pick.commit.fileName)}${gitUri.sha ? ` from ${GlyphChars.Space}$(git-commit) ${gitUri.shortSha}` : ''}`
|
||||
}, Commands.ShowQuickFileHistory, [
|
||||
uri,
|
||||
args
|
||||
]);
|
||||
|
||||
return commands.executeCommand(Commands.ShowQuickCommitFileDetails,
|
||||
new GitUri(pick.commit.uri, pick.commit),
|
||||
{
|
||||
commit: pick.commit,
|
||||
fileLog: args.log,
|
||||
sha: pick.commit.sha,
|
||||
goBackCommand: currentCommand
|
||||
} as ShowQuickCommitFileDetailsCommandArgs);
|
||||
}
|
||||
catch (ex) {
|
||||
Logger.error(ex, 'ShowQuickFileHistoryCommand');
|
||||
return window.showErrorMessage(`Unable to show file history. See output channel for more details`);
|
||||
}
|
||||
finally {
|
||||
progressCancellation.dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user