hub/commands/remote.go

109 строки
2.8 KiB
Go
Исходник Обычный вид История

2013-06-13 18:11:05 +04:00
package commands
import (
2013-12-16 19:54:46 +04:00
"fmt"
2013-06-13 18:11:05 +04:00
"github.com/jingweno/gh/github"
2013-07-01 21:11:02 +04:00
"github.com/jingweno/gh/utils"
2013-12-16 19:54:46 +04:00
"regexp"
2013-06-13 18:11:05 +04:00
)
2013-06-14 21:05:47 +04:00
var cmdRemote = &Command{
2013-06-18 00:56:57 +04:00
Run: remote,
GitExtension: true,
Usage: "remote [-p] OPTIONS USER[/REPOSITORY]",
Short: "View and manage a set of remote repositories",
Long: `Add remote "git://github.com/USER/REPOSITORY.git" as with
git-remote(1). When /REPOSITORY is omitted, the basename of the
current working directory is used. With -p, use private remote
"git@github.com:USER/REPOSITORY.git". If USER is "origin"
then uses your GitHub login.
`,
2013-06-13 18:11:05 +04:00
}
2013-06-30 20:00:21 +04:00
/*
2013-06-18 00:35:02 +04:00
$ gh remote add jingweno
> git remote add jingweno git://github.com/jingweno/THIS_REPO.git
2013-06-18 00:35:02 +04:00
$ gh remote add -p jingweno
> git remote add jingweno git@github.com:jingweno/THIS_REPO.git
2013-06-13 18:11:05 +04:00
2013-06-18 00:35:02 +04:00
$ gh remote add origin
2013-06-30 20:00:21 +04:00
> git remote add origin git://github.com/YOUR_LOGIN/THIS_REPO.git
*/
2013-06-26 19:48:34 +04:00
func remote(command *Command, args *Args) {
2013-12-16 19:54:46 +04:00
if !args.IsParamsEmpty() && (args.FirstParam() == "add" || args.FirstParam() == "set-url") {
2013-06-26 19:48:34 +04:00
transformRemoteArgs(args)
2013-06-13 18:11:05 +04:00
}
2013-06-18 00:35:02 +04:00
}
2013-06-26 19:48:34 +04:00
func transformRemoteArgs(args *Args) {
2013-07-02 22:56:45 +04:00
ownerWithName := args.LastParam()
2013-12-16 19:54:46 +04:00
owner, name := parseRepoNameOwner(ownerWithName)
if owner == "" {
2013-06-30 20:00:21 +04:00
return
}
2013-07-13 20:59:32 +04:00
2013-12-16 19:54:46 +04:00
localRepo := github.LocalRepo()
var repoName string
2013-07-01 21:11:02 +04:00
if name == "" {
2013-12-16 19:54:46 +04:00
project, err := localRepo.MainProject()
if err == nil {
repoName = project.Name
} else {
repoName, err = utils.DirName()
utils.Check(err)
}
name = repoName
2013-07-01 21:11:02 +04:00
}
2013-12-16 19:54:46 +04:00
words := args.Words()
2013-07-13 20:59:32 +04:00
isPriavte := parseRemotePrivateFlag(args)
2013-12-16 19:54:46 +04:00
if len(words) == 2 && words[1] == "origin" {
// gh add origin
credentials := github.CurrentConfigs().DefaultCredentials()
owner = credentials.User
name = repoName
} else if len(words) == 2 {
// gh remote add jingweno foo/bar
if idx := args.IndexOfParam(words[1]); idx != -1 {
args.ReplaceParam(idx, owner)
}
} else {
2013-07-13 20:59:32 +04:00
args.RemoveParam(args.ParamsSize() - 1)
2013-07-01 21:11:02 +04:00
}
2013-06-13 18:11:05 +04:00
2013-12-10 10:47:15 +04:00
project := github.NewProject(owner, name, "")
2013-12-16 19:54:46 +04:00
// for GitHub Enterprise
isPriavte = isPriavte || project.Host != github.GitHubHost
2013-07-01 21:11:02 +04:00
url := project.GitURL(name, owner, isPriavte)
2013-07-02 22:56:45 +04:00
args.AppendParams(url)
2013-06-18 00:35:02 +04:00
}
2013-06-26 19:48:34 +04:00
func parseRemotePrivateFlag(args *Args) bool {
2013-07-02 22:56:45 +04:00
if i := args.IndexOfParam("-p"); i != -1 {
args.RemoveParam(i)
2013-06-26 19:48:34 +04:00
return true
2013-06-18 00:35:02 +04:00
}
2013-06-26 19:48:34 +04:00
return false
2013-06-13 18:11:05 +04:00
}
2013-12-16 19:54:46 +04:00
func parseRepoNameOwner(nameWithOwner string) (owner, name string) {
ownerRe := fmt.Sprintf("^(%s)$", OwnerRe)
ownerRegexp := regexp.MustCompile(ownerRe)
if ownerRegexp.MatchString(nameWithOwner) {
owner = ownerRegexp.FindStringSubmatch(nameWithOwner)[1]
return
}
nameWithOwnerRe := fmt.Sprintf("^(%s)\\/(%s)$", OwnerRe, NameRe)
nameWithOwnerRegexp := regexp.MustCompile(nameWithOwnerRe)
if nameWithOwnerRegexp.MatchString(nameWithOwner) {
result := nameWithOwnerRegexp.FindStringSubmatch(nameWithOwner)
owner = result[1]
name = result[2]
}
return
}