/* @flow */

const execa = require('execa');

const tasks = [
  {
    title: 'check current branch',
    skip: () => process.env.ANY_BRANCH === '1',
    task: () => execa.stdout('git', ['symbolic-ref', '--short', 'HEAD']).then(branch => {
      if (!branch === 'master') {
        throw {
            cmd: 'not on a `master` branch',
            stderr: 'use ANY_BRANCH=1 to publish anyway',
        }
      }
    }),
  },
  {
    title: 'check local working tree',
    task: () => execa.stdout('git', ['status', '--porcelain']).then(status => {
      if (status !== '') {
        throw {
          cmd: 'unclean working tree',
          stderr: 'commit or stash changes first',
        }
      }
    }),
  },
  {
    title: 'check git tag existence',
    task: () => execa('git', ['fetch'])
    .then(() => execa.stdout('git', ['rev-parse', '--quiet', '--verify', `refs/tags/v${process.env.NEXT_VERSION}`]))
    .then(
      output => {
        if (output) {
          throw {
            cmd: `git tag \`v${process.env.NEXT_VERSION}\` already exists.`,
            stderr: 'delete tag or try again with a new version'
          }
        }
      },
      err => {
        // Command fails with code 1 and no output if the tag does not exist,
        // even though `--quiet` is provided
        // https://github.com/sindresorhus/np/pull/73#discussion_r72385685
        if (err.stdout !== '' || err.stderr !== '') {
          throw err;
        }
      }
    ),
  },
  {
    title: 'check remote history',
    task: () => execa.stdout('git', ['rev-list', '--count', '--left-only', '@{u}...HEAD']).then(result => {
      if (result !== '0') {
        throw {
          cmd: 'remote history differs',
          stderr: 'please pull changes',
        }
      }
    }),
  },
];

module.exports = tasks;

@@ -2,16 +2,21 @@
 * @flow 
+weak 
 */%0A%0Acons
@@ -752,34 +752,35 @@
 nce',%0A    task: 
-()
+ctx
  =%3E execa('git',
@@ -882,32 +882,19 @@
 /v$%7B
-process.env.NEXT_VERSION
+ctx.version
 %7D%60%5D)
@@ -999,32 +999,19 @@
 %60v$%7B
-process.env.NEXT_VERSION
+ctx.version
 %7D%5C%60 

fix preflight git tag check