Skip to content

Commit

Permalink
Gracefully handle branch not having an upstream (#321)
Browse files Browse the repository at this point in the history
Fixes #90
  • Loading branch information
itaisteinherz authored and sindresorhus committed Jan 12, 2019
1 parent f041588 commit 535e190
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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'])
});

Expand Down
7 changes: 7 additions & 0 deletions lib/git-util.js
Original file line number Diff line number Diff line change
@@ -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);
};
8 changes: 6 additions & 2 deletions lib/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}
}
Expand Down

0 comments on commit 535e190

Please sign in to comment.