all files / heroku-git/commands/git/ remote.js

100% Statements 19/19
88.89% Branches 8/9
100% Functions 3/3
100% Lines 19/19
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47                                                       
'use strict'
 
let co = require('co')
let cli = require('heroku-cli-util')
let git = require('../../lib/git')
 
function includes (array, item) {
  return array.indexOf(item) !== -1
}
 
module.exports = {
  topic: 'git',
  command: 'remote',
  description: 'adds a git remote to an app repo',
  help: `extra arguments will be passed to git remote add
 
Examples:
 
  $ heroku git:remote -a example set git remote heroku to https://git.heroku.com/example.git`,
  needsAuth: true,
  variableArgs: true,
  flags: [
    {name: 'app', char: 'a', hasValue: true, description: 'the Heroku app to use'},
    {name: 'remote', char: 'r', hasValue: true, description: 'the git remote to create'},
    {name: 'ssh-git', description: 'use SSH git protocol'}
  ],
  run: cli.command(function (context, heroku) {
    return co(function * () {
      git = git(context)
      let appName = context.flags.app || context.args.shift()
      if (!appName) {
        throw new Error('Specify an app with --app')
      }
      let app = yield heroku.apps(appName).info()
      let remote = context.flags.remote || (yield git.remoteFromGitConfig()) || 'heroku'
      let remotes = yield git.exec(['remote'])
      let url = git.url(app.name, context.flags['ssh-git'])
      if (includes(remotes.split('\n'), remote)) {
        yield git.exec(['remote', 'set-url', remote, url].concat(context.args))
      } else {
        yield git.exec(['remote', 'add', remote, url].concat(context.args))
      }
      cli.log(`set git remote ${cli.color.cyan(remote)} to ${cli.color.cyan(url)}`)
    })
  })
}