-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
diffWithPrevious.ts
143 lines (123 loc) · 3.87 KB
/
diffWithPrevious.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { TextDocumentShowOptions, TextEditor, Uri } from 'vscode';
import type { Container } from '../container';
import { GitUri } from '../git/gitUri';
import { GitCommit, GitRevision } from '../git/models';
import { Logger } from '../logger';
import { Messages } from '../messages';
import {
ActiveEditorCommand,
command,
CommandContext,
Commands,
executeCommand,
findOrOpenEditor,
getCommandUri,
} from './common';
import { DiffWithCommandArgs } from './diffWith';
export interface DiffWithPreviousCommandArgs {
commit?: GitCommit;
inDiffRightEditor?: boolean;
uri?: Uri;
line?: number;
showOptions?: TextDocumentShowOptions;
}
@command()
export class DiffWithPreviousCommand extends ActiveEditorCommand {
constructor(private readonly container: Container) {
super([Commands.DiffWithPrevious, Commands.DiffWithPreviousInDiffLeft, Commands.DiffWithPreviousInDiffRight]);
}
protected override preExecute(context: CommandContext, args?: DiffWithPreviousCommandArgs) {
if (context.command === Commands.DiffWithPreviousInDiffRight) {
args = { ...args, inDiffRightEditor: true };
}
return this.execute(context.editor, context.uri, args);
}
async execute(editor?: TextEditor, uri?: Uri, args?: DiffWithPreviousCommandArgs) {
args = { ...args };
if (args.uri == null) {
uri = getCommandUri(uri, editor);
if (uri == null) return;
} else {
uri = args.uri;
}
if (args.line == null) {
args.line = editor?.selection.active.line ?? 0;
}
let gitUri;
if (args.commit?.file != null) {
if (!args.commit.isUncommitted) {
void (await executeCommand<DiffWithCommandArgs>(Commands.DiffWith, {
repoPath: args.commit.repoPath,
lhs: {
sha: `${args.commit.sha}^`,
uri: args.commit.file.originalUri ?? args.commit.file.uri,
},
rhs: {
sha: args.commit.sha || '',
uri: args.commit.file.uri,
},
line: args.line,
showOptions: args.showOptions,
}));
return;
}
gitUri = args.commit?.getGitUri();
} else {
gitUri = await GitUri.fromUri(uri);
}
// If we are in the right diff editor, we can't really trust the line number
// if (args.inDiffRightEditor && args.line !== 0) {
// // TODO@eamodio figure out how to tell where the line moved in the previous commit (if at all)
// }
try {
const diffUris = await this.container.git.getPreviousComparisonUris(
gitUri.repoPath!,
gitUri,
gitUri.sha,
// If we are in the right-side of the diff editor, we need to skip back 1 more revision
args.inDiffRightEditor ? 1 : 0,
);
if (diffUris == null || diffUris.previous == null) {
if (diffUris == null) {
void Messages.showCommitHasNoPreviousCommitWarningMessage();
return;
}
// If we have no previous and the current is the working file, just open the working file
if (diffUris.current.sha == null) {
void (await findOrOpenEditor(diffUris.current, args.showOptions));
return;
}
if (!diffUris.current.isUncommittedStaged) {
void Messages.showCommitHasNoPreviousCommitWarningMessage();
return;
}
// If we have no previous and the current is staged, then diff staged with missing
diffUris.previous = GitUri.fromFile(
diffUris.current.fileName,
diffUris.current.repoPath!,
GitRevision.deletedOrMissing,
);
}
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: args.line,
showOptions: args.showOptions,
}));
} catch (ex) {
Logger.error(
ex,
'DiffWithPreviousCommand',
`getPreviousDiffUris(${gitUri.repoPath}, ${gitUri.fsPath}, ${gitUri.sha})`,
);
void Messages.showGenericErrorMessage('Unable to open compare');
}
}
}