github.com/trevoraustin/hub@v2.2.0-preview1.0.20141105230840-96d8bfc654cc+incompatible/commands/push.go (about)

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