diff --git a/commands/commands.go b/commands/commands.go index ae9b04c1..03fd07d1 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -60,6 +60,7 @@ var Branching = []*Command{ var Remote = []*Command{ cmdClone, cmdFetch, + cmdPush, cmdRemote, } diff --git a/commands/push.go b/commands/push.go new file mode 100644 index 00000000..ca7337ee --- /dev/null +++ b/commands/push.go @@ -0,0 +1,54 @@ +package commands + +import ( + "github.com/jingweno/gh/git" + "github.com/jingweno/gh/utils" + "strings" +) + +var cmdPush = &Command{ + Run: push, + GitExtension: true, + Usage: "push REMOTE-1,REMOTE-2,...,REMOTE-N [REF]", + Short: "Upload data, tags and branches to a remote repository", + Long: `Push REF to each of REMOTE-1 through REMOTE-N by executing +multiple git-push(1) commands.`, +} + +/* + $ gh push origin,staging,qa bert_timeout + > git push origin bert_timeout + > git push staging bert_timeout + > git push qa bert_timeout + + $ gh push origin + > git push origin HEAD +*/ +func push(command *Command, args *Args) { + if !args.IsParamsEmpty() || !strings.Contains(args.FirstParam(), ",") { + transformPushArgs(args) + } +} + +func transformPushArgs(args *Args) { + refs := []string{} + if args.ParamsSize() > 1 { + refs = args.Params[1:] + } + + remotes := strings.Split(args.FirstParam(), ",") + args.ReplaceParam(0, remotes[0]) + + if len(refs) == 0 { + head, err := git.Head() + utils.Check(err) + refs = []string{head.ShortName()} + args.AppendParams(refs...) + } + + for _, remote := range remotes[1:] { + afterCmd := []string{"git", "push", remote} + afterCmd = append(afterCmd, refs...) + args.After(afterCmd...) + } +} diff --git a/commands/push_test.go b/commands/push_test.go new file mode 100644 index 00000000..497c2bd8 --- /dev/null +++ b/commands/push_test.go @@ -0,0 +1,25 @@ +package commands + +import ( + "github.com/bmizerany/assert" + "regexp" + "testing" +) + +func TestTransformPushArgs(t *testing.T) { + args := NewArgs([]string{"push", "origin,staging,qa", "bert_timeout"}) + transformPushArgs(args) + cmds := args.Commands() + + assert.Equal(t, 3, len(cmds)) + assert.Equal(t, "git push origin bert_timeout", cmds[0].String()) + assert.Equal(t, "git push staging bert_timeout", cmds[1].String()) + + args = NewArgs([]string{"push", "origin"}) + transformPushArgs(args) + cmds = args.Commands() + + assert.Equal(t, 1, len(cmds)) + pushRegexp := regexp.MustCompile("git push origin .+") + assert.T(t, pushRegexp.MatchString(cmds[0].String())) +}