-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
diffLineWithPrevious.ts
70 lines (60 loc) · 1.96 KB
/
diffLineWithPrevious.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
import { TextDocumentShowOptions, TextEditor, Uri } from 'vscode';
import type { Container } from '../container';
import { GitUri } from '../git/gitUri';
import { GitCommit } from '../git/models';
import { Logger } from '../logger';
import { Messages } from '../messages';
import { ActiveEditorCommand, command, Commands, executeCommand, getCommandUri } from './common';
import { DiffWithCommandArgs } from './diffWith';
export interface DiffLineWithPreviousCommandArgs {
commit?: GitCommit;
line?: number;
showOptions?: TextDocumentShowOptions;
}
@command()
export class DiffLineWithPreviousCommand extends ActiveEditorCommand {
constructor(private readonly container: Container) {
super(Commands.DiffLineWithPrevious);
}
async execute(editor?: TextEditor, uri?: Uri, args?: DiffLineWithPreviousCommandArgs): Promise<any> {
uri = getCommandUri(uri, editor);
if (uri == null) return;
args = { ...args };
if (args.line == null) {
args.line = editor?.selection.active.line ?? 0;
}
const gitUri = args.commit?.getGitUri() ?? (await GitUri.fromUri(uri));
try {
const diffUris = await this.container.git.getPreviousComparisonUrisForLine(
gitUri.repoPath!,
gitUri,
args.line,
gitUri.sha,
);
if (diffUris == null || diffUris.previous == null) {
void Messages.showCommitHasNoPreviousCommitWarningMessage();
return;
}
void (await executeCommand<DiffWithCommandArgs>(Commands.DiffWith, {
repoPath: diffUris.current.repoPath,
lhs: {
sha: diffUris.previous.sha ?? '',
uri: diffUris.previous.documentUri(),
},
rhs: {
sha: diffUris.current.sha ?? '',
uri: diffUris.current.documentUri(),
},
line: diffUris.line,
showOptions: args.showOptions,
}));
} catch (ex) {
Logger.error(
ex,
'DiffLineWithPreviousCommand',
`getPreviousLineDiffUris(${gitUri.repoPath}, ${gitUri.fsPath}, ${gitUri.sha})`,
);
void Messages.showGenericErrorMessage('Unable to open compare');
}
}
}