Skip to content

Commit

Permalink
feat: spawn the user's editor to edit commit messages
Browse files Browse the repository at this point in the history
  • Loading branch information
targos committed Nov 8, 2021
1 parent 25b452d commit 811de87
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
20 changes: 18 additions & 2 deletions lib/landing_session.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ const {
runAsync, runSync, forceRunAsync
} = require('./run');
const Session = require('./session');
const { shortSha, isGhAvailable } = require('./utils');
const {
shortSha, isGhAvailable, getEditor
} = require('./utils');

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

Expand Down Expand Up @@ -345,7 +347,21 @@ class LandingSession extends Session {
return true;
}

// TODO: fire the configured git editor on that file
const editor = await getEditor({ git: true });
if (editor) {
try {
await forceRunAsync(
editor,
[`"${messageFile}"`],
{ ignoreFailure: false, spawnArgs: { shell: true } }
);
await runAsync('git', ['commit', '--amend', '-F', messageFile]);
return true;
} catch {
cli.error('Failed to edit the message using the configured editor');
}
}

cli.log(`Please manually edit ${messageFile}, then run\n` +
`\`git commit --amend -F ${messageFile}\` ` +
'to finish amending the message');
Expand Down
30 changes: 30 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const which = require('which');
const { forceRunAsync } = require('./run');

exports.ascending = function(a, b) {
if (a === b) return 0;
Expand Down Expand Up @@ -36,3 +37,32 @@ exports.isGhAvailable = function isGhAvailable() {
}
return isGhAvailableCache;
};

/**
* Returns the user's preferred text editor command.
* @param {object} [options]
* @param {boolean} [options.git] - Whether to try the GIT_EDITOR environment
* variable or `git config`.
* @returns {string|null}
*/
exports.getEditor = async function getEditor(options = {}) {
const {
git = false
} = options;

if (git) {
if (process.env.GIT_EDITOR) {
return process.env.GIT_EDITOR;
}
const out = await forceRunAsync(
'git',
['config', 'core.editor'],
{ captureStdout: 'lines' }
);
if (out && out[0]) {
return out[0];
}
}

return process.env.VISUAL || process.env.EDITOR || null;
};

0 comments on commit 811de87

Please sign in to comment.