github.com/pengwynn/gh@v1.0.1-0.20140118055701-14327ca3942e/commands/push.go (about)

     1  package commands
     2  
     3  import (
     4  	"github.com/jingweno/gh/github"
     5  	"github.com/jingweno/gh/utils"
     6  	"strings"
     7  )
     8  
     9  var cmdPush = &Command{
    10  	Run:          push,
    11  	GitExtension: true,
    12  	Usage:        "push REMOTE-1,REMOTE-2,...,REMOTE-N [REF]",
    13  	Short:        "Upload data, tags and branches to a remote repository",
    14  	Long: `Push REF to each of REMOTE-1 through REMOTE-N by executing
    15  multiple git-push(1) commands.`,
    16  }
    17  
    18  func init() {
    19  	CmdRunner.Use(cmdPush)
    20  }
    21  
    22  /*
    23    $ gh push origin,staging,qa bert_timeout
    24    > git push origin bert_timeout
    25    > git push staging bert_timeout
    26    > git push qa bert_timeout
    27  
    28    $ gh push origin
    29    > git push origin HEAD
    30  */
    31  func push(command *Command, args *Args) {
    32  	if !args.IsParamsEmpty() && strings.Contains(args.FirstParam(), ",") {
    33  		transformPushArgs(args)
    34  	}
    35  }
    36  
    37  func transformPushArgs(args *Args) {
    38  	refs := []string{}
    39  	if args.ParamsSize() > 1 {
    40  		refs = args.Params[1:]
    41  	}
    42  
    43  	remotes := strings.Split(args.FirstParam(), ",")
    44  	args.ReplaceParam(0, remotes[0])
    45  
    46  	if len(refs) == 0 {
    47  		localRepo := github.LocalRepo()
    48  		head, err := localRepo.CurrentBranch()
    49  		utils.Check(err)
    50  		refs = []string{head.ShortName()}
    51  		args.AppendParams(refs...)
    52  	}
    53  
    54  	for _, remote := range remotes[1:] {
    55  		afterCmd := []string{"git", "push", remote}
    56  		afterCmd = append(afterCmd, refs...)
    57  		args.After(afterCmd...)
    58  	}
    59  }