Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gracefully handle branch not having an upstream #321

Merged
merged 7 commits into from
Jan 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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