From de758bcda0e898d28b4c2539c7cf436bb2ff9116 Mon Sep 17 00:00:00 2001 From: Andrew Goode Date: Sun, 1 May 2016 19:53:04 -0400 Subject: [PATCH] feat: add --sign flag to sign git commit and tag (#29) * feat(cli): add tag signing option (--sign-tag and its alias -s) By default, a tag is not signed (git tag -a). The option `--sign-tag` enables tag signing (git tag -s). * feat: add --sign flag to sign git commit and tag --- index.js | 17 +++++++++++++++-- test.js | 14 ++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 01c62dbc5..8ab7c5a34 100755 --- a/index.js +++ b/index.js @@ -24,6 +24,13 @@ var argv = require('yargs') default: false, global: true }) + .option('sign', { + alias: 's', + describe: 'Should the git commit and tag be signed?', + type: 'boolean', + default: false, + global: true + }) .help() .alias('help', 'h') .example('$0', 'Update changelog and tag release') @@ -106,7 +113,7 @@ function commit (argv, newVersion, cb) { args.unshift('package.json') } checkpoint(msg, args) - exec('git add package.json ' + argv.infile + ';git commit package.json ' + argv.infile + ' -m "' + formatCommitMessage(argv.message, newVersion) + '"', function (err, stdout, stderr) { + exec('git add package.json ' + argv.infile + ';git commit ' + (argv.sign ? '-S ' : '') + 'package.json ' + argv.infile + ' -m "' + formatCommitMessage(argv.message, newVersion) + '"', function (err, stdout, stderr) { var errMessage = null if (err) errMessage = err.message if (stderr) errMessage = stderr @@ -123,8 +130,14 @@ function formatCommitMessage (msg, newVersion) { } function tag (newVersion, argv) { + var tagOption + if (argv.sign) { + tagOption = '-s ' + } else { + tagOption = '-a ' + } checkpoint('tagging release %s', [newVersion]) - exec('git tag -a v' + newVersion + ' -m "' + formatCommitMessage(argv.message, newVersion) + '"', function (err, stdout, stderr) { + exec('git tag ' + tagOption + 'v' + newVersion + ' -m "' + formatCommitMessage(argv.message, newVersion) + '"', function (err, stdout, stderr) { var errMessage = null if (err) errMessage = err.message if (stderr) errMessage = stderr diff --git a/test.js b/test.js index 672c197aa..a801441de 100644 --- a/test.js +++ b/test.js @@ -79,6 +79,20 @@ describe('cli', function () { }) }) + it('respects the --sign option', function () { + fs.writeFileSync('package.json', JSON.stringify({ + version: '1.0.0' + }), 'utf-8') + + commit('feat: first commit') + + // this should fail without a GPG key + var result = shell.exec(cliPath + ' --sign') + result.code.should.equal(1) + result.stdout.should.match(/gpg\: signing failed\: secret key not available/) + result.stdout.should.match(/error\: gpg failed to sign the data/) + }) + it('handles commit messages longer than 80 characters', function () { fs.writeFileSync('package.json', JSON.stringify({ version: '1.0.0'