Skip to content

Commit 7984206

Browse files
tribouzkat
authored andcommitted
version: Add new sign-git-commit config (#12697)
PR-URL: npm/npm#12697 Credit: @tribou Reviewed-By: @zkat
1 parent 244b183 commit 7984206

10 files changed

+55
-10
lines changed

doc/misc/npm-config.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,6 +1034,17 @@ will also prevent _writing_ `npm-shrinkwrap.json` if `save` is true.
10341034

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

1037+
### sign-git-commit
1038+
1039+
* Default: false
1040+
* Type: Boolean
1041+
1042+
If set to true, then the `npm version` command will commit the new package
1043+
version using `-S` to add a signature.
1044+
1045+
Note that git requires you to have set up GPG keys in your git configs
1046+
for this to work properly.
1047+
10371048
### sign-git-tag
10381049

10391050
* Default: false

lib/config/defaults.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ Object.defineProperty(exports, 'defaults', {get: function () {
220220
'send-metrics': false,
221221
shell: osenv.shell(),
222222
shrinkwrap: true,
223+
'sign-git-commit': false,
223224
'sign-git-tag': false,
224225
'sso-poll-frequency': 500,
225226
'sso-type': 'oauth',
@@ -352,6 +353,7 @@ exports.types = {
352353
'send-metrics': Boolean,
353354
shell: String,
354355
shrinkwrap: Boolean,
356+
'sign-git-commit': Boolean,
355357
'sign-git-tag': Boolean,
356358
'sso-poll-frequency': Number,
357359
'sso-type': [null, 'oauth', 'saml'],

lib/version.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,10 @@ function buildCommitArgs (args) {
294294
function _commit (version, localData, cb) {
295295
const options = { env: process.env }
296296
const message = npm.config.get('message').replace(/%s/g, version)
297-
const sign = npm.config.get('sign-git-tag')
298-
const commitArgs = buildCommitArgs([ 'commit', '-m', message ])
299-
const flagForTag = sign ? '-sm' : '-am'
297+
const signTag = npm.config.get('sign-git-tag')
298+
const signCommit = npm.config.get('sign-git-commit')
299+
const commitArgs = buildCommitArgs([ 'commit', signCommit ? '-S -m' : '-m', message ])
300+
const flagForTag = signTag ? '-sm' : '-am'
300301

301302
stagePackageFiles(localData, options).then(() => {
302303
return git.exec(commitArgs, options)

test/tap/tag-version-prefix.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var packagePath = path.resolve(pkg, 'package.json')
1616

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

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

2121
test('npm version <semver> with message config', function (t) {
2222
setup()

test/tap/version-from-git.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ test('npm version from-git with a valid tag creates a new commit', function (t)
2222

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

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

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

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

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

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

171177
function runVersion (er) {
172178
t.ifError(er, 'created git repo without errors')
179+
npm.config.set('sign-git-commit', false)
173180
npm.config.set('sign-git-tag', false)
174181
npm.commands.version(['from-git'], checkVersion)
175182
}

test/tap/version-git-not-clean.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ test('npm version <semver> --force with working directory not clean', function (
5959
common.npm(
6060
[
6161
'--force',
62+
'--no-sign-git-commit',
6263
'--no-sign-git-tag',
6364
'--registry', common.registry,
6465
'--prefix', pkg,

test/tap/version-lifecycle.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var npm = require('../../')
1111
var pkg = path.resolve(__dirname, 'version-lifecycle')
1212
var cache = path.resolve(pkg, 'cache')
1313
var npmrc = path.resolve(pkg, './.npmrc')
14-
var configContents = 'sign-git-tag=false\n'
14+
var configContents = 'sign-git-commit=false\nsign-git-tag=false\n'
1515

1616
test('npm version <semver> with failing preversion lifecycle script', function (t) {
1717
setup()
@@ -25,7 +25,12 @@ test('npm version <semver> with failing preversion lifecycle script', function (
2525
}
2626
}), 'utf8')
2727
fs.writeFileSync(path.resolve(pkg, 'fail.js'), 'process.exit(50)', 'utf8')
28-
npm.load({cache: cache, 'sign-git-tag': false, registry: common.registry}, function () {
28+
npm.load({
29+
cache: cache,
30+
'sign-git-commit': false,
31+
'sign-git-tag': false,
32+
registry: common.registry
33+
}, function () {
2934
var version = require('../../lib/version')
3035
version(['patch'], function (err) {
3136
t.ok(err)
@@ -47,7 +52,12 @@ test('npm version <semver> with failing version lifecycle script', function (t)
4752
}
4853
}), 'utf8')
4954
fs.writeFileSync(path.resolve(pkg, 'fail.js'), 'process.exit(50)', 'utf8')
50-
npm.load({cache: cache, 'sign-git-tag': false, registry: common.registry}, function () {
55+
npm.load({
56+
cache: cache,
57+
'sign-git-commit': false,
58+
'sign-git-tag': false,
59+
registry: common.registry
60+
}, function () {
5161
var version = require('../../lib/version')
5262
version(['patch'], function (err) {
5363
t.ok(err)
@@ -69,7 +79,12 @@ test('npm version <semver> with failing postversion lifecycle script', function
6979
}
7080
}), 'utf8')
7181
fs.writeFileSync(path.resolve(pkg, 'fail.js'), 'process.exit(50)', 'utf8')
72-
npm.load({cache: cache, 'sign-git-tag': false, registry: common.registry}, function () {
82+
npm.load({
83+
cache: cache,
84+
'sign-git-commit': false,
85+
'sign-git-tag': false,
86+
registry: common.registry
87+
}, function () {
7388
var version = require('../../lib/version')
7489
version(['patch'], function (err) {
7590
t.ok(err)
@@ -95,7 +110,12 @@ test('npm version <semver> execution order', function (t) {
95110
makeScript('preversion')
96111
makeScript('version')
97112
makeScript('postversion')
98-
npm.load({cache: cache, 'sign-git-tag': false, registry: common.registry}, function () {
113+
npm.load({
114+
cache: cache,
115+
'sign-git-commit': false,
116+
'sign-git-tag': false,
117+
registry: common.registry
118+
}, function () {
99119
common.makeGitRepo({path: pkg}, function (err, git) {
100120
t.ifError(err, 'git bootstrap ran without error')
101121

test/tap/version-message-config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var packagePath = path.resolve(pkg, 'package.json')
1616

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

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

2121
test('npm version <semver> with message config', function (t) {
2222
setup()

test/tap/version-sub-directory.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ test('npm version <semver> from a subdirectory', function (t) {
3232
function version (er, stdout, stderr) {
3333
t.ifError(er, 'git repo initialized without issue')
3434
t.notOk(stderr, 'no error output')
35+
npm.config.set('sign-git-commit', false)
3536
npm.config.set('sign-git-tag', false)
3637
npm.commands.version(['patch'], checkVersion)
3738
}

test/tap/version-update-shrinkwrap.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ test('npm version <semver> updates git works with no shrinkwrap', function (t) {
2828
setup()
2929
rimraf.sync(path.resolve(pkg, 'npm-shrinkwrap.json'))
3030

31+
npm.config.set('sign-git-commit', false)
3132
npm.config.set('sign-git-tag', false)
3233

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

74+
npm.config.set('sign-git-commit', false)
7375
npm.config.set('sign-git-tag', false)
7476

7577
common.makeGitRepo({

0 commit comments

Comments
 (0)