github.com/trevoraustin/hub@v2.2.0-preview1.0.20141105230840-96d8bfc654cc+incompatible/commands/remote.go (about) 1 package commands 2 3 import ( 4 "fmt" 5 "regexp" 6 7 "github.com/github/hub/github" 8 "github.com/github/hub/utils" 9 ) 10 11 var cmdRemote = &Command{ 12 Run: remote, 13 GitExtension: true, 14 Usage: "remote [-p] OPTIONS USER[/REPOSITORY]", 15 Short: "View and manage a set of remote repositories", 16 Long: `Add remote "git://github.com/USER/REPOSITORY.git" as with 17 git-remote(1). When /REPOSITORY is omitted, the basename of the 18 current working directory is used. With -p, use private remote 19 "git@github.com:USER/REPOSITORY.git". If USER is "origin" 20 then uses your GitHub login. 21 `, 22 } 23 24 func init() { 25 CmdRunner.Use(cmdRemote) 26 } 27 28 /* 29 $ gh remote add jingweno 30 > git remote add jingweno git://github.com/jingweno/THIS_REPO.git 31 32 $ gh remote add -p jingweno 33 > git remote add jingweno git@github.com:jingweno/THIS_REPO.git 34 35 $ gh remote add origin 36 > git remote add origin git://github.com/YOUR_LOGIN/THIS_REPO.git 37 */ 38 func remote(command *Command, args *Args) { 39 if !args.IsParamsEmpty() && (args.FirstParam() == "add" || args.FirstParam() == "set-url") { 40 transformRemoteArgs(args) 41 } 42 } 43 44 func transformRemoteArgs(args *Args) { 45 ownerWithName := args.LastParam() 46 owner, name := parseRepoNameOwner(ownerWithName) 47 if owner == "" { 48 return 49 } 50 51 localRepo, err := github.LocalRepo() 52 utils.Check(err) 53 54 var repoName, host string 55 if name == "" { 56 project, err := localRepo.MainProject() 57 if err == nil { 58 repoName = project.Name 59 host = project.Host 60 } else { 61 repoName, err = utils.DirName() 62 utils.Check(err) 63 } 64 65 name = repoName 66 } 67 68 words := args.Words() 69 isPrivate := parseRemotePrivateFlag(args) 70 if len(words) == 2 && words[1] == "origin" { 71 // Origin special case triggers default user/repo 72 host, err := github.CurrentConfig().DefaultHost() 73 if err != nil { 74 utils.Check(github.FormatError("adding remote", err)) 75 } 76 77 owner = host.User 78 name = repoName 79 } else if len(words) == 2 { 80 // gh remote add jingweno foo/bar 81 if idx := args.IndexOfParam(words[1]); idx != -1 { 82 args.ReplaceParam(idx, owner) 83 } 84 } else { 85 args.RemoveParam(args.ParamsSize() - 1) 86 } 87 88 project := github.NewProject(owner, name, host) 89 // for GitHub Enterprise 90 isPrivate = isPrivate || project.Host != github.GitHubHost 91 url := project.GitURL(name, owner, isPrivate) 92 args.AppendParams(url) 93 } 94 95 func parseRemotePrivateFlag(args *Args) bool { 96 if i := args.IndexOfParam("-p"); i != -1 { 97 args.RemoveParam(i) 98 return true 99 } 100 101 return false 102 } 103 104 func parseRepoNameOwner(nameWithOwner string) (owner, name string) { 105 ownerRe := fmt.Sprintf("^(%s)$", OwnerRe) 106 ownerRegexp := regexp.MustCompile(ownerRe) 107 if ownerRegexp.MatchString(nameWithOwner) { 108 owner = ownerRegexp.FindStringSubmatch(nameWithOwner)[1] 109 return 110 } 111 112 nameWithOwnerRe := fmt.Sprintf("^(%s)\\/(%s)$", OwnerRe, NameRe) 113 nameWithOwnerRegexp := regexp.MustCompile(nameWithOwnerRe) 114 if nameWithOwnerRegexp.MatchString(nameWithOwner) { 115 result := nameWithOwnerRegexp.FindStringSubmatch(nameWithOwner) 116 owner = result[1] 117 name = result[2] 118 } 119 120 return 121 }