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

     1  package commands
     2  
     3  import (
     4  	"github.com/jingweno/gh/github"
     5  	"github.com/jingweno/gh/utils"
     6  	"regexp"
     7  	"strings"
     8  )
     9  
    10  var cmdFetch = &Command{
    11  	Run:          fetch,
    12  	GitExtension: true,
    13  	Usage:        "fetch [USER...]",
    14  	Short:        "Download data, tags and branches from a remote repository",
    15  	Long: `Adds missing remote(s) with git remote add prior to fetching. New
    16  remotes are only added if they correspond to valid forks on GitHub.
    17  `,
    18  }
    19  
    20  func init() {
    21  	CmdRunner.Use(cmdFetch)
    22  }
    23  
    24  /*
    25    $ gh fetch jingweno
    26    > git remote add jingweno git://github.com/jingweno/REPO.git
    27    > git fetch jingweno
    28  
    29    $ git fetch jingweno,foo
    30    > git remote add jingweno ...
    31    > git remote add foo ...
    32    > git fetch --multiple jingweno foo
    33  
    34    $ git fetch --multiple jingweno foo
    35    > git remote add jingweno ...
    36    > git remote add foo ...
    37    > git fetch --multiple jingweno foo
    38  */
    39  func fetch(command *Command, args *Args) {
    40  	if !args.IsParamsEmpty() {
    41  		err := tranformFetchArgs(args)
    42  		utils.Check(err)
    43  	}
    44  }
    45  
    46  func tranformFetchArgs(args *Args) error {
    47  	names := parseRemoteNames(args)
    48  	localRepo := github.LocalRepo()
    49  
    50  	projects := make(map[*github.Project]bool)
    51  	ownerRegexp := regexp.MustCompile(OwnerRe)
    52  	for _, name := range names {
    53  		if ownerRegexp.MatchString(name) {
    54  			_, err := localRepo.RemoteByName(name)
    55  			if err != nil {
    56  				project := github.NewProject(name, "", "")
    57  				gh := github.NewClient(project.Host)
    58  				repo, err := gh.Repository(project)
    59  				if err != nil {
    60  					continue
    61  				}
    62  
    63  				projects[project] = repo.Private
    64  			}
    65  		}
    66  	}
    67  
    68  	for project, private := range projects {
    69  		args.Before("git", "remote", "add", project.Owner, project.GitURL("", "", private))
    70  	}
    71  
    72  	return nil
    73  }
    74  
    75  func parseRemoteNames(args *Args) (names []string) {
    76  	words := args.Words()
    77  	if i := args.IndexOfParam("--multiple"); i != -1 {
    78  		if args.ParamsSize() > 1 {
    79  			names = words
    80  		}
    81  	} else if len(words) > 0 {
    82  		remoteName := words[0]
    83  		remoteNameRegexp := regexp.MustCompile("^\\w+(,\\w+)$")
    84  		if remoteNameRegexp.MatchString(remoteName) {
    85  			i := args.IndexOfParam(remoteName)
    86  			args.RemoveParam(i)
    87  			names = strings.Split(remoteName, ",")
    88  			args.InsertParam(i, names...)
    89  			args.InsertParam(i, "--multiple")
    90  		} else {
    91  			names = append(names, remoteName)
    92  		}
    93  	}
    94  
    95  	return
    96  }