push.go 1.2 KB
Newer Older
T
Theodore Kokkoris 已提交
1 2 3 4 5 6 7 8 9 10
package commands

import (
	"os"
	"strings"
	"github.com/jingweno/gh/utils"
)

var cmdPush = &Command{
	Run: push,
T
Theodore Kokkoris 已提交
11
	GitExtension: true,
T
Theodore Kokkoris 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
	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() {
27
		pushToEveryRemote(args)
T
Theodore Kokkoris 已提交
28 29 30
	}
}

31
func pushToEveryRemote (args *Args) {
32
	remotes, idx := getRemotes(args)
T
Theodore Kokkoris 已提交
33
	for _, i := range remotes {
34 35 36
		copyArgs := args
		copyArgs.ReplaceParam(idx, i)
		err := copyArgs.ToCmd().Exec()
T
Theodore Kokkoris 已提交
37 38 39
		utils.Check(err)
	}

T
Theodore Kokkoris 已提交
40
	fixHelp(args)
T
Theodore Kokkoris 已提交
41 42
}

43 44 45 46 47 48 49
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 已提交
50
	}
51

T
Theodore Kokkoris 已提交
52 53
	return
}
T
Theodore Kokkoris 已提交
54 55 56 57 58 59 60 61 62 63 64

func fixHelp(args *Args) {
	for _, i := range args.Params {
		if i == "--help" && args.ParamsSize() == 1 {
			args.Replace(args.Executable, args.Command, "--help")
			return
		}
	}

	os.Exit(0)
}