Skip to content

Commit 811de87

Browse files
committed
feat: spawn the user's editor to edit commit messages
1 parent 25b452d commit 811de87

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

lib/landing_session.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ const {
1111
runAsync, runSync, forceRunAsync
1212
} = require('./run');
1313
const Session = require('./session');
14-
const { shortSha, isGhAvailable } = require('./utils');
14+
const {
15+
shortSha, isGhAvailable, getEditor
16+
} = require('./utils');
1517

1618
const isWindows = process.platform === 'win32';
1719

@@ -345,7 +347,21 @@ class LandingSession extends Session {
345347
return true;
346348
}
347349

348-
// TODO: fire the configured git editor on that file
350+
const editor = await getEditor({ git: true });
351+
if (editor) {
352+
try {
353+
await forceRunAsync(
354+
editor,
355+
[`"${messageFile}"`],
356+
{ ignoreFailure: false, spawnArgs: { shell: true } }
357+
);
358+
await runAsync('git', ['commit', '--amend', '-F', messageFile]);
359+
return true;
360+
} catch {
361+
cli.error('Failed to edit the message using the configured editor');
362+
}
363+
}
364+
349365
cli.log(`Please manually edit ${messageFile}, then run\n` +
350366
`\`git commit --amend -F ${messageFile}\` ` +
351367
'to finish amending the message');

lib/utils.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const which = require('which');
4+
const { forceRunAsync } = require('./run');
45

56
exports.ascending = function(a, b) {
67
if (a === b) return 0;
@@ -36,3 +37,32 @@ exports.isGhAvailable = function isGhAvailable() {
3637
}
3738
return isGhAvailableCache;
3839
};
40+
41+
/**
42+
* Returns the user's preferred text editor command.
43+
* @param {object} [options]
44+
* @param {boolean} [options.git] - Whether to try the GIT_EDITOR environment
45+
* variable or `git config`.
46+
* @returns {string|null}
47+
*/
48+
exports.getEditor = async function getEditor(options = {}) {
49+
const {
50+
git = false
51+
} = options;
52+
53+
if (git) {
54+
if (process.env.GIT_EDITOR) {
55+
return process.env.GIT_EDITOR;
56+
}
57+
const out = await forceRunAsync(
58+
'git',
59+
['config', 'core.editor'],
60+
{ captureStdout: 'lines' }
61+
);
62+
if (out && out[0]) {
63+
return out[0];
64+
}
65+
}
66+
67+
return process.env.VISUAL || process.env.EDITOR || null;
68+
};

0 commit comments

Comments
 (0)