diff --git a/index.js b/index.js index 803dcc36..ddd964f6 100644 --- a/index.js +++ b/index.js @@ -15,6 +15,7 @@ const pkgDir = require('pkg-dir'); const prerequisiteTasks = require('./lib/prerequisite'); const gitTasks = require('./lib/git'); const util = require('./lib/util'); +const {hasUpstream} = require('./lib/git-util'); const publish = require('./lib/publish'); const exec = (cmd, args) => { @@ -149,6 +150,7 @@ module.exports = async (input = 'patch', options) => { tasks.add({ title: 'Pushing tags', + skip: async () => !(await hasUpstream()), task: () => exec('git', ['push', '--follow-tags']) }); diff --git a/lib/git-util.js b/lib/git-util.js new file mode 100644 index 00000000..75abd829 --- /dev/null +++ b/lib/git-util.js @@ -0,0 +1,7 @@ +'use strict'; +const execa = require('execa'); + +exports.hasUpstream = async () => { + const {stdout} = await execa('git', ['status', '--short', '--branch', '--porcelain=2']); + return /^# branch\.upstream [\w\-/]+$/m.test(stdout); +}; diff --git a/lib/git.js b/lib/git.js index 36341ebf..ad15b2eb 100644 --- a/lib/git.js +++ b/lib/git.js @@ -25,8 +25,12 @@ module.exports = options => { { title: 'Check remote history', task: async () => { - const {stdout} = await execa('git', ['rev-list', '--count', '--left-only', '@{u}...HEAD']); - if (stdout !== '0') { + let stdout; + try { // Gracefully handle no remote set up. + stdout = await execa.stdout('git', ['rev-list', '--count', '--left-only', '@{u}...HEAD']); + } catch (_) {} + + if (stdout && stdout !== '0') { throw new Error('Remote history differs. Please pull changes.'); } }