Skip to content

Commit

Permalink
version: Add new sign-git-commit config (#12697)
Browse files Browse the repository at this point in the history
PR-URL: npm/npm#12697
Credit: @tribou
Reviewed-By: @zkat
  • Loading branch information
tribou authored and zkat committed Jul 10, 2018
1 parent 244b183 commit 7984206
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 10 deletions.
11 changes: 11 additions & 0 deletions doc/misc/npm-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,17 @@ will also prevent _writing_ `npm-shrinkwrap.json` if `save` is true.

This option is an alias for `--package-lock`.

### sign-git-commit

* Default: false
* Type: Boolean

If set to true, then the `npm version` command will commit the new package
version using `-S` to add a signature.

Note that git requires you to have set up GPG keys in your git configs
for this to work properly.

### sign-git-tag

* Default: false
Expand Down
2 changes: 2 additions & 0 deletions lib/config/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ Object.defineProperty(exports, 'defaults', {get: function () {
'send-metrics': false,
shell: osenv.shell(),
shrinkwrap: true,
'sign-git-commit': false,
'sign-git-tag': false,
'sso-poll-frequency': 500,
'sso-type': 'oauth',
Expand Down Expand Up @@ -352,6 +353,7 @@ exports.types = {
'send-metrics': Boolean,
shell: String,
shrinkwrap: Boolean,
'sign-git-commit': Boolean,
'sign-git-tag': Boolean,
'sso-poll-frequency': Number,
'sso-type': [null, 'oauth', 'saml'],
Expand Down
7 changes: 4 additions & 3 deletions lib/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,10 @@ function buildCommitArgs (args) {
function _commit (version, localData, cb) {
const options = { env: process.env }
const message = npm.config.get('message').replace(/%s/g, version)
const sign = npm.config.get('sign-git-tag')
const commitArgs = buildCommitArgs([ 'commit', '-m', message ])
const flagForTag = sign ? '-sm' : '-am'
const signTag = npm.config.get('sign-git-tag')
const signCommit = npm.config.get('sign-git-commit')
const commitArgs = buildCommitArgs([ 'commit', signCommit ? '-S -m' : '-m', message ])
const flagForTag = signTag ? '-sm' : '-am'

stagePackageFiles(localData, options).then(() => {
return git.exec(commitArgs, options)
Expand Down
2 changes: 1 addition & 1 deletion test/tap/tag-version-prefix.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var packagePath = path.resolve(pkg, 'package.json')

var json = { name: 'blah', version: '0.1.2' }

var configContents = 'sign-git-tag=false\nmessage=":bookmark: %s"\n'
var configContents = 'sign-git-commit=false\nsign-git-tag=false\nmessage=":bookmark: %s"\n'

test('npm version <semver> with message config', function (t) {
setup()
Expand Down
7 changes: 7 additions & 0 deletions test/tap/version-from-git.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ test('npm version from-git with a valid tag creates a new commit', function (t)

function runVersion (er) {
t.ifError(er, 'git tag ran without error')
npm.config.set('sign-git-commit', false)
npm.config.set('sign-git-tag', false)
npm.commands.version(['from-git'], checkVersion)
}
Expand Down Expand Up @@ -51,6 +52,7 @@ test('npm version from-git with a valid tag updates the package.json version', f

function runVersion (er) {
t.ifError(er, 'git tag ran without error')
npm.config.set('sign-git-commit', false)
npm.config.set('sign-git-tag', false)
npm.commands.version(['from-git'], checkManifest)
}
Expand All @@ -75,6 +77,7 @@ test('npm version from-git strips tag-version-prefix', function (t) {

function runVersion (er) {
t.ifError(er, 'git tag ran without error')
npm.config.set('sign-git-commit', false)
npm.config.set('sign-git-tag', false)
npm.config.set('tag-version-prefix', prefix)
npm.commands.version(['from-git'], checkVersion)
Expand Down Expand Up @@ -107,6 +110,7 @@ test('npm version from-git only strips tag-version-prefix if it is a prefix', fu

function runVersion (er) {
t.ifError(er, 'git tag ran without error')
npm.config.set('sign-git-commit', false)
npm.config.set('sign-git-tag', false)
npm.config.set('tag-version-prefix', prefix)
npm.commands.version(['from-git'], checkVersion)
Expand Down Expand Up @@ -137,6 +141,7 @@ test('npm version from-git with an existing version', function (t) {

function runVersion (er) {
t.ifError(er, 'git tag ran without error')
npm.config.set('sign-git-commit', false)
npm.config.set('sign-git-tag', false)
npm.commands.version(['from-git'], checkVersion)
}
Expand All @@ -154,6 +159,7 @@ test('npm version from-git with an invalid version tag', function (t) {

function runVersion (er) {
t.ifError(er, 'git tag ran without error')
npm.config.set('sign-git-commit', false)
npm.config.set('sign-git-tag', false)
npm.commands.version(['from-git'], checkVersion)
}
Expand All @@ -170,6 +176,7 @@ test('npm version from-git without any versions', function (t) {

function runVersion (er) {
t.ifError(er, 'created git repo without errors')
npm.config.set('sign-git-commit', false)
npm.config.set('sign-git-tag', false)
npm.commands.version(['from-git'], checkVersion)
}
Expand Down
1 change: 1 addition & 0 deletions test/tap/version-git-not-clean.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ test('npm version <semver> --force with working directory not clean', function (
common.npm(
[
'--force',
'--no-sign-git-commit',
'--no-sign-git-tag',
'--registry', common.registry,
'--prefix', pkg,
Expand Down
30 changes: 25 additions & 5 deletions test/tap/version-lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var npm = require('../../')
var pkg = path.resolve(__dirname, 'version-lifecycle')
var cache = path.resolve(pkg, 'cache')
var npmrc = path.resolve(pkg, './.npmrc')
var configContents = 'sign-git-tag=false\n'
var configContents = 'sign-git-commit=false\nsign-git-tag=false\n'

test('npm version <semver> with failing preversion lifecycle script', function (t) {
setup()
Expand All @@ -25,7 +25,12 @@ test('npm version <semver> with failing preversion lifecycle script', function (
}
}), 'utf8')
fs.writeFileSync(path.resolve(pkg, 'fail.js'), 'process.exit(50)', 'utf8')
npm.load({cache: cache, 'sign-git-tag': false, registry: common.registry}, function () {
npm.load({
cache: cache,
'sign-git-commit': false,
'sign-git-tag': false,
registry: common.registry
}, function () {
var version = require('../../lib/version')
version(['patch'], function (err) {
t.ok(err)
Expand All @@ -47,7 +52,12 @@ test('npm version <semver> with failing version lifecycle script', function (t)
}
}), 'utf8')
fs.writeFileSync(path.resolve(pkg, 'fail.js'), 'process.exit(50)', 'utf8')
npm.load({cache: cache, 'sign-git-tag': false, registry: common.registry}, function () {
npm.load({
cache: cache,
'sign-git-commit': false,
'sign-git-tag': false,
registry: common.registry
}, function () {
var version = require('../../lib/version')
version(['patch'], function (err) {
t.ok(err)
Expand All @@ -69,7 +79,12 @@ test('npm version <semver> with failing postversion lifecycle script', function
}
}), 'utf8')
fs.writeFileSync(path.resolve(pkg, 'fail.js'), 'process.exit(50)', 'utf8')
npm.load({cache: cache, 'sign-git-tag': false, registry: common.registry}, function () {
npm.load({
cache: cache,
'sign-git-commit': false,
'sign-git-tag': false,
registry: common.registry
}, function () {
var version = require('../../lib/version')
version(['patch'], function (err) {
t.ok(err)
Expand All @@ -95,7 +110,12 @@ test('npm version <semver> execution order', function (t) {
makeScript('preversion')
makeScript('version')
makeScript('postversion')
npm.load({cache: cache, 'sign-git-tag': false, registry: common.registry}, function () {
npm.load({
cache: cache,
'sign-git-commit': false,
'sign-git-tag': false,
registry: common.registry
}, function () {
common.makeGitRepo({path: pkg}, function (err, git) {
t.ifError(err, 'git bootstrap ran without error')

Expand Down
2 changes: 1 addition & 1 deletion test/tap/version-message-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var packagePath = path.resolve(pkg, 'package.json')

var json = { name: 'blah', version: '0.1.2' }

var configContents = 'sign-git-tag=false\nmessage=":bookmark: %s"\n'
var configContents = 'sign-git-commit=false\nsign-git-tag=false\nmessage=":bookmark: %s"\n'

test('npm version <semver> with message config', function (t) {
setup()
Expand Down
1 change: 1 addition & 0 deletions test/tap/version-sub-directory.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ test('npm version <semver> from a subdirectory', function (t) {
function version (er, stdout, stderr) {
t.ifError(er, 'git repo initialized without issue')
t.notOk(stderr, 'no error output')
npm.config.set('sign-git-commit', false)
npm.config.set('sign-git-tag', false)
npm.commands.version(['patch'], checkVersion)
}
Expand Down
2 changes: 2 additions & 0 deletions test/tap/version-update-shrinkwrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ test('npm version <semver> updates git works with no shrinkwrap', function (t) {
setup()
rimraf.sync(path.resolve(pkg, 'npm-shrinkwrap.json'))

npm.config.set('sign-git-commit', false)
npm.config.set('sign-git-tag', false)

common.makeGitRepo({
Expand Down Expand Up @@ -70,6 +71,7 @@ test('npm version <semver> updates git works with no shrinkwrap', function (t) {
test('npm version <semver> updates shrinkwrap and updates git', function (t) {
setup()

npm.config.set('sign-git-commit', false)
npm.config.set('sign-git-tag', false)

common.makeGitRepo({
Expand Down

0 comments on commit 7984206

Please # to comment.