hub/commands/fork.go

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

2013-06-11 04:10:40 +04:00
package commands
import (
2013-06-11 18:46:48 +04:00
"fmt"
2014-02-10 20:22:36 +04:00
"github.com/github/hub/github"
"github.com/github/hub/utils"
2013-12-11 22:54:24 +04:00
"os"
"reflect"
2013-06-11 04:10:40 +04:00
)
var cmdFork = &Command{
Run: fork,
Usage: "fork [--no-remote]",
Short: "Make a fork of a remote repository on GitHub and add as remote",
Long: `Forks the original project (referenced by "origin" remote) on GitHub and
adds a new remote for it under your username.
`,
}
var flagForkNoRemote bool
func init() {
cmdFork.Flag.BoolVar(&flagForkNoRemote, "no-remote", false, "")
CmdRunner.Use(cmdFork)
2013-06-11 04:10:40 +04:00
}
2013-07-06 00:45:22 +04:00
/*
$ gh fork
[ repo forked on GitHub ]
> git remote add -f YOUR_USER git@github.com:YOUR_USER/CURRENT_REPO.git
$ gh fork --no-remote
[ repo forked on GitHub ]
*/
2013-06-26 19:48:34 +04:00
func fork(cmd *Command, args *Args) {
2013-12-11 22:54:24 +04:00
localRepo := github.LocalRepo()
2013-06-11 04:10:40 +04:00
2013-12-11 22:54:24 +04:00
project, err := localRepo.MainProject()
utils.Check(err)
configs := github.CurrentConfigs()
credentials := configs.PromptFor(project.Host)
forkProject := github.NewProject(credentials.User, project.Name, project.Host)
2013-06-11 04:10:40 +04:00
2013-12-17 19:45:48 +04:00
client := github.NewClient(project.Host)
2013-12-11 22:54:24 +04:00
existingRepo, err := client.Repository(forkProject)
if err == nil {
var parentURL *github.URL
if parent := existingRepo.Parent; parent != nil {
parentURL, _ = github.ParseURL(parent.HTMLURL)
}
if parentURL == nil || !reflect.DeepEqual(parentURL.Project, project) {
err = fmt.Errorf("Error creating fork: %s already exists on %s",
forkProject, forkProject.Host)
utils.Check(err)
}
} else {
if !args.Noop {
_, err := client.ForkRepository(project)
utils.Check(err)
}
2013-06-11 18:46:48 +04:00
}
2013-12-11 22:54:24 +04:00
if flagForkNoRemote {
os.Exit(0)
} else {
u := forkProject.GitURL("", "", true)
args.Replace("git", "remote", "add", "-f", forkProject.Owner, u)
args.After("echo", fmt.Sprintf("new remote: %s", forkProject.Owner))
2013-07-06 00:45:22 +04:00
}
2013-06-11 04:10:40 +04:00
}