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