forked from gitkraken/vscode-gitlens
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiffWithRevision.ts
105 lines (97 loc) · 3.16 KB
/
diffWithRevision.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import type { TextDocumentShowOptions, TextEditor, Uri } from 'vscode';
import { Commands, GlyphChars, quickPickTitleMaxChars } from '../constants';
import type { Container } from '../container';
import { GitUri } from '../git/gitUri';
import { shortenRevision } from '../git/models/reference';
import { showGenericErrorMessage } from '../messages';
import { showCommitPicker } from '../quickpicks/commitPicker';
import { CommandQuickPickItem } from '../quickpicks/items/common';
import { command, executeCommand } from '../system/command';
import { Logger } from '../system/logger';
import { pad } from '../system/string';
import { ActiveEditorCommand, getCommandUri } from './base';
import type { DiffWithCommandArgs } from './diffWith';
import type { DiffWithRevisionFromCommandArgs } from './diffWithRevisionFrom';
export interface DiffWithRevisionCommandArgs {
line?: number;
showOptions?: TextDocumentShowOptions;
}
@command()
export class DiffWithRevisionCommand extends ActiveEditorCommand {
constructor(private readonly container: Container) {
super(Commands.DiffWithRevision);
}
async execute(editor?: TextEditor, uri?: Uri, args?: DiffWithRevisionCommandArgs): Promise<any> {
uri = getCommandUri(uri, editor);
if (uri == null) return;
const gitUri = await GitUri.fromUri(uri);
args = { ...args };
if (args.line == null) {
args.line = editor?.selection.active.line ?? 0;
}
try {
const log = this.container.git
.getLogForFile(gitUri.repoPath, gitUri.fsPath)
.then(
log =>
log ??
(gitUri.sha
? this.container.git.getLogForFile(gitUri.repoPath, gitUri.fsPath, { ref: gitUri.sha })
: undefined),
);
const title = `Open Changes with Revision${pad(GlyphChars.Dot, 2, 2)}`;
const pick = await showCommitPicker(
log,
`${title}${gitUri.getFormattedFileName({
suffix: gitUri.sha ? `:${shortenRevision(gitUri.sha)}` : undefined,
truncateTo: quickPickTitleMaxChars - title.length,
})}`,
'Choose a commit to compare with',
{
picked: gitUri.sha,
keys: ['right', 'alt+right', 'ctrl+right'],
onDidPressKey: async (key, item) => {
void (await executeCommand<DiffWithCommandArgs>(Commands.DiffWith, {
repoPath: gitUri.repoPath,
lhs: {
sha: item.item.ref,
uri: gitUri,
},
rhs: {
sha: '',
uri: gitUri,
},
line: args!.line,
showOptions: args!.showOptions,
}));
},
showOtherReferences: [
CommandQuickPickItem.fromCommand('Choose a Branch or Tag...', Commands.DiffWithRevisionFrom),
CommandQuickPickItem.fromCommand<DiffWithRevisionFromCommandArgs>(
'Choose a Stash...',
Commands.DiffWithRevisionFrom,
{ stash: true },
),
],
},
);
if (pick == null) return;
void (await executeCommand<DiffWithCommandArgs>(Commands.DiffWith, {
repoPath: gitUri.repoPath,
lhs: {
sha: pick.ref,
uri: gitUri,
},
rhs: {
sha: '',
uri: gitUri,
},
line: args.line,
showOptions: args.showOptions,
}));
} catch (ex) {
Logger.error(ex, 'DiffWithRevisionCommand');
void showGenericErrorMessage('Unable to open compare');
}
}
}