push.go 1.3 KB
Newer Older
T
Theodore Kokkoris 已提交
1 2 3
package commands

import (
T
Theodore Kokkoris 已提交
4
	"fmt"
T
Theodore Kokkoris 已提交
5 6 7 8 9 10 11
	"os"
	"strings"
	"github.com/jingweno/gh/utils"
)

var cmdPush = &Command{
	Run: push,
T
Theodore Kokkoris 已提交
12
	GitExtension: true,
T
Theodore Kokkoris 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
	Usage: "push REMOTE-1,REMOTE-2,...,REMOTE-N [REF]",
	Short: "Update remote refs along with associated objects",
	Long: `Push REF to each of REMOTE-1 through REMOTE-N by executing  mul-
tiple git push commands.`,
}

/**
 $ git push origin,staging,qa bert_timeout
 > git push origin bert_timeout
 > git push staging bert_timeout
 > git push qa bert_timeout
**/

func push (command *Command, args *Args) {
	if !args.IsParamsEmpty() {
28
		pushToEveryRemote(args)
T
Theodore Kokkoris 已提交
29 30 31
	}
}

32
func pushToEveryRemote (args *Args) {
33
	remotes, idx := getRemotes(args)
T
Theodore Kokkoris 已提交
34
	for _, i := range remotes {
35 36
		copyArgs := args
		copyArgs.ReplaceParam(idx, i)
T
Theodore Kokkoris 已提交
37 38 39 40 41 42
		if !args.Noop {
			err := copyArgs.ToCmd().Exec()
			utils.Check(err)
		} else {
			fmt.Printf("it would run `git push %s`\n", strings.Join(copyArgs.Params, " "))
		}
T
Theodore Kokkoris 已提交
43 44
	}

T
Theodore Kokkoris 已提交
45
	fixHelp(args)
T
Theodore Kokkoris 已提交
46 47
}

48 49 50 51 52 53 54
func getRemotes(args *Args) (remotes []string, idx int) {
	for a, i := range args.Params {
		if !strings.HasPrefix(i, "-") {
			remotes = strings.Split(i, ",")
			idx = a
			return
		}
T
Theodore Kokkoris 已提交
55
	}
56

T
Theodore Kokkoris 已提交
57 58
	return
}
T
Theodore Kokkoris 已提交
59 60 61 62

func fixHelp(args *Args) {
	for _, i := range args.Params {
		if i == "--help" && args.ParamsSize() == 1 {
63
			args.Params = []string{"--help"}
T
Theodore Kokkoris 已提交
64 65 66 67 68 69
			return
		}
	}

	os.Exit(0)
}