From 811de87206806246a98033c60c5db2557d56da12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Zasso?= Date: Sun, 10 Oct 2021 11:38:29 +0200 Subject: [PATCH] feat: spawn the user's editor to edit commit messages --- lib/landing_session.js | 20 ++++++++++++++++++-- lib/utils.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/lib/landing_session.js b/lib/landing_session.js index d522cd76..79749f56 100644 --- a/lib/landing_session.js +++ b/lib/landing_session.js @@ -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'; @@ -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'); diff --git a/lib/utils.js b/lib/utils.js index 919c2095..532f7760 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,6 +1,7 @@ 'use strict'; const which = require('which'); +const { forceRunAsync } = require('./run'); exports.ascending = function(a, b) { if (a === b) return 0; @@ -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; +};