diff --git a/lib/git-shell-out-strategy.js b/lib/git-shell-out-strategy.js index d8ad54d16a..df4b160685 100644 --- a/lib/git-shell-out-strategy.js +++ b/lib/git-shell-out-strategy.js @@ -506,6 +506,13 @@ export default class GitShellOutStrategy { msg = rawMessage; } + // if commit template is used, strip commented lines from commit + // to be consistent with command line git. + const template = await this.fetchCommitMessageTemplate(); + if (template) { + msg = msg.split('\n').filter(line => !line.startsWith('#')).join('\n'); + } + // Determine the cleanup mode. if (verbatim) { args.push('--cleanup=verbatim'); diff --git a/test/git-strategies.test.js b/test/git-strategies.test.js index 6dbd031fdf..f5e6dcf894 100644 --- a/test/git-strategies.test.js +++ b/test/git-strategies.test.js @@ -1036,6 +1036,24 @@ import * as reporterProxy from '../lib/reporter-proxy'; 'and things', ].join('\n')); }); + it('strips commented lines if commit template is used', async function() { + const workingDirPath = await cloneRepository('three-files'); + const git = createTestStrategy(workingDirPath); + const templateText = '# this line should be stripped'; + + const commitMsgTemplatePath = path.join(workingDirPath, '.gitmessage'); + await fs.writeFile(commitMsgTemplatePath, templateText, {encoding: 'utf8'}); + + await git.setConfig('commit.template', commitMsgTemplatePath); + await git.setConfig('commit.cleanup', 'default'); + const commitMessage = ['this line should not be stripped', '', 'neither should this one', templateText].join('\n'); + await git.commit(commitMessage, {allowEmpty: true, verbatim: true}); + + const lastCommit = await git.getHeadCommit(); + assert.strictEqual(lastCommit.messageSubject, 'this line should not be stripped'); + // message body should not contain the template text + assert.strictEqual(lastCommit.messageBody, 'neither should this one'); + }); }); describe('when amend option is true', function() { @@ -1168,6 +1186,7 @@ import * as reporterProxy from '../lib/reporter-proxy'; beforeEach(async function() { const workingDirPath = await cloneRepository('multiple-commits'); git = createTestStrategy(workingDirPath); + sinon.stub(git, 'fetchCommitMessageTemplate').returns(null); }); const operations = [