hub/commands/remote.go

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

2013-06-13 18:11:05 +04:00
package commands
import (
"github.com/jingweno/gh/github"
2013-07-01 21:11:02 +04:00
"github.com/jingweno/gh/utils"
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-07-05 22:10:24 +04:00
if args.ParamsSize() >= 2 && (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-07-01 21:11:02 +04:00
owner, name, match := parseRepoNameOwner(ownerWithName)
2013-06-30 20:00:21 +04:00
if !match {
return
}
2013-07-13 20:59:32 +04:00
2013-07-10 01:36:02 +04:00
var err error
2013-07-01 21:11:02 +04:00
if name == "" {
2013-07-11 03:29:26 +04:00
name, err = utils.DirName()
2013-07-01 21:11:02 +04:00
utils.Check(err)
}
2013-07-13 20:59:32 +04:00
isPriavte := parseRemotePrivateFlag(args)
2013-07-01 21:11:02 +04:00
if owner == "origin" {
owner = github.CurrentConfig().FetchUser()
2013-07-13 20:59:32 +04:00
} else if args.ParamsSize() > 2 {
// `git remote add jingweno foo/bar`
args.RemoveParam(args.ParamsSize() - 1)
2013-07-01 21:11:02 +04:00
}
2013-06-13 18:11:05 +04:00
2013-07-01 21:11:02 +04:00
project := github.Project{Owner: owner, Name: name}
url := project.GitURL(name, owner, isPriavte)
2013-06-18 00:35:02 +04:00
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
}