-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
commitCommands.ts
217 lines (177 loc) · 8.03 KB
/
commitCommands.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import * as vscode from 'vscode';
import * as Constants from '../common/constants';
import { execPath } from 'process';
import { MagitRepository } from '../models/magitRepository';
import { gitRun } from '../utils/gitRawRunner';
import { MenuUtil, MenuState } from '../menu/menu';
import MagitUtils from '../utils/magitUtils';
import * as Diffing from './diffingCommands';
import { Section } from '../views/general/sectionHeader';
import * as path from 'path';
import FilePathUtils from '../utils/filePathUtils';
import * as fs from 'fs';
import ViewUtils from '../utils/viewUtils';
import GitTextUtils from '../utils/gitTextUtils';
import { magitConfig } from '../extension';
const commitMenu = {
title: 'Committing',
commands: [
{ label: 'c', description: 'Commit', action: commit },
{ label: 'a', description: 'Amend', action: (menuState: MenuState) => ammendCommit(menuState, ['--amend']) },
{ label: 'e', description: 'Extend', action: (menuState: MenuState) => ammendCommit(menuState, ['--amend', '--no-edit']) },
{ label: 'w', description: 'Reword', action: (menuState: MenuState) => rewordCommit(menuState, ['--amend', '--only']) },
{ label: 'f', description: 'Fixup', action: (menuState: MenuState) => fixup(menuState) },
{ label: 'F', description: 'Instant Fixup', action: (menuState: MenuState) => instantFixup(menuState) },
]
};
export async function magitCommit(repository: MagitRepository) {
const switches = [
{ key: '-a', name: '--all', description: 'Stage all modified and deleted files' },
{ key: '-e', name: '--allow-empty', description: 'Allow empty commit' },
{ key: '-s', name: '--signoff', description: 'Add Signed-off-by line' },
{ key: '-n', name: '--no-verify', description: 'Disable hooks' },
{ key: '-S', name: '--gpg-sign', description: 'GPG-sign commit' },
];
return MenuUtil.showMenu(commitMenu, { repository, switches });
}
export async function commit({ repository, switches }: MenuState, commitArgs: string[] = []) {
let stageAllSwitch = switches?.find(({ key }) => key === '-a');
if (repository.indexChanges.length === 0 && !stageAllSwitch?.activated && stageAllSwitch) {
if (await MagitUtils.confirmAction('Nothing staged. Stage and commit all unstaged changes?')) {
stageAllSwitch.activated = true;
}
}
const args = ['commit', ...MenuUtil.switchesToArgs(switches), ...commitArgs];
return runCommitLikeCommand(repository, args, { showStagedChanges: !stageAllSwitch?.activated });
}
export async function ammendCommit({ repository, switches }: MenuState, commitArgs: string[] = []) {
const args = ['commit', ...MenuUtil.switchesToArgs(switches), ...commitArgs];
return runCommitLikeCommand(repository, args);
}
export async function rewordCommit({ repository, switches }: MenuState, commitArgs: string[] = []) {
const args = ['commit', ...MenuUtil.switchesToArgs(switches), ...commitArgs];
return runCommitLikeCommand(repository, args, { showStagedChanges: false });
}
async function fixup({ repository, switches }: MenuState) {
const sha = await MagitUtils.chooseCommit(repository, 'Fixup commit');
if (sha) {
const args = ['commit', ...MenuUtil.switchesToArgs(switches), '--fixup', sha];
return await gitRun(repository.gitRepository, args);
} else {
throw new Error('No commit chosen to fixup');
}
}
async function instantFixup({ repository, switches = [] }: MenuState) {
const sha = await MagitUtils.chooseCommit(repository, 'Instantly Fixup commit');
if (sha) {
let shortHash = GitTextUtils.shortHash(sha);
await gitRun(repository.gitRepository, ['commit', '--no-gpg-sign', '--no-edit', ...MenuUtil.switchesToArgs(switches), `--fixup=${shortHash}`, '--']);
const args = ['rebase', '-i', '--autosquash', '--autostash', shortHash + '~'];
return await gitRun(repository.gitRepository, args, { env: { 'GIT_SEQUENCE_EDITOR': 'true' } });
} else {
throw new Error('No commit chosen to fixup');
}
}
interface CommitEditorOptions {
updatePostCommitTask?: boolean;
showStagedChanges?: boolean;
editor?: string;
propagateErrors?: boolean;
}
let codePath: string = findCodePath();
export function setCodePath(path?: string) {
if (path && path !== '') {
codePath = path;
} else {
codePath = findCodePath();
}
}
export async function runCommitLikeCommand(repository: MagitRepository, args: string[], { showStagedChanges, updatePostCommitTask, editor, propagateErrors }: CommitEditorOptions = { showStagedChanges: true }) {
let stagedEditorTask: Thenable<vscode.TextEditor> | undefined;
let instructionStatus;
let editorListener;
try {
instructionStatus = vscode.window.setStatusBarMessage(`Type C-c C-c to finish, or C-c C-k to cancel`);
if (showStagedChanges) {
stagedEditorTask = Diffing.showDiffSection(repository, Section.Staged, true);
}
const env: NodeJS.ProcessEnv = { 'GIT_EDITOR': `"${codePath}" --wait` };
if (editor) {
env[editor] = `"${codePath}" --wait`;
}
const commitSuccessMessageTask = gitRun(repository.gitRepository, args, { env });
editorListener = vscode.window.onDidChangeActiveTextEditor(editor => {
if (
editor &&
FilePathUtils.fileName(editor.document.uri) === 'COMMIT_EDITMSG' &&
editor.document.getText(new vscode.Range(0, 0, 0, 1)) === ''
) {
// Move the cursor to the beginning
const position = editor.selection.active;
const newPosition = position.with(0, 0);
const newSelection = new vscode.Selection(newPosition, newPosition);
editor.selection = newSelection;
}
});
if (updatePostCommitTask) {
await new Promise(r => setTimeout(r, 100));
MagitUtils.magitStatusAndUpdate(repository);
}
const commitSuccessMessage = await commitSuccessMessageTask;
vscode.window.setStatusBarMessage(`Git finished: ${commitSuccessMessage.stdout.replace(Constants.LineSplitterRegex, ' ')}`, Constants.StatusMessageDisplayTimeout);
} catch (e) {
vscode.window.setStatusBarMessage(`Commit canceled.`, Constants.StatusMessageDisplayTimeout);
if (propagateErrors || !GitTextUtils.formatError(e).includes('Aborting commit due to empty commit message.')) {
throw e;
}
} finally {
if (editorListener) {
editorListener.dispose();
}
if (instructionStatus) {
instructionStatus.dispose();
}
const stagedEditor = await stagedEditorTask;
if (stagedEditor) {
for (const visibleEditor of vscode.window.visibleTextEditors) {
if (visibleEditor.document.uri === stagedEditor.document.uri) {
// This is a bit of a hack. Too bad about editor.hide() and editor.show() being deprecated.
const stagedEditorViewColumn = ViewUtils.showDocumentColumn();
await vscode.window.showTextDocument(stagedEditor.document, { viewColumn: stagedEditorViewColumn, preview: false });
await vscode.commands.executeCommand('workbench.action.closeActiveEditor');
vscode.commands.executeCommand(`workbench.action.navigate${stagedEditorViewColumn === vscode.ViewColumn.One ? 'Right' : 'Left'}`);
}
}
}
}
}
function findCodePath(): string {
// Check if we are currently running a Code Insiders or Codium build
let isInsiders = vscode.env.appName.includes('Insider');
let isCodium = vscode.env.appRoot.includes('codium');
let isDarwin = process.platform === 'darwin';
let isWindows = process.platform === 'win32';
let isRemote = !!vscode.env.remoteName;
let codePath = 'code';
if (isCodium && !isDarwin) {
codePath = 'codium';
}
if (isInsiders && !isDarwin) {
// On Mac the binary for the Insiders build is still called `code`
codePath += '-insiders';
}
if (isWindows && isRemote) {
// On window remote server, 'code' alias doesn't exist
codePath += '.cmd';
}
// Find the code binary on different platforms.
if (isDarwin) {
codePath = execPath.split(/(?<=\.app)/)[0] + '/Contents/Resources/app/bin/' + codePath;
} else {
codePath = path.join(path.dirname(execPath), 'bin', codePath);
}
if (!fs.existsSync(codePath)) {
return 'code';
}
return codePath;
}