hub/commands/checkout.go

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

2013-06-21 22:40:42 +04:00
package commands
2013-06-22 00:17:52 +04:00
import (
2013-06-22 04:01:00 +04:00
"fmt"
2014-04-01 00:01:05 +04:00
"regexp"
"github.com/github/hub/git"
2014-02-10 20:22:36 +04:00
"github.com/github/hub/github"
"github.com/github/hub/utils"
2013-06-22 00:17:52 +04:00
)
2013-06-21 22:40:42 +04:00
var cmdCheckout = &Command{
Run: checkout,
GitExtension: true,
Usage: "checkout <PULLREQ-URL> [<BRANCH>]",
Long: `Check out the head of a pull request as a local branch.
## Examples:
$ hub checkout https://github.com/jingweno/gh/pull/73
> git fetch origin pull/73/head:jingweno-feature
> git checkout jingweno-feature
2016-01-24 18:50:01 +03:00
## See also:
hub-merge(1), hub-am(1), hub(1), git-checkout(1)
`,
2013-06-21 22:40:42 +04:00
}
func init() {
CmdRunner.Use(cmdCheckout)
}
2013-06-26 19:48:34 +04:00
func checkout(command *Command, args *Args) {
2013-07-02 22:56:45 +04:00
if !args.IsParamsEmpty() {
2013-07-02 22:28:50 +04:00
err := transformCheckoutArgs(args)
2013-07-05 03:18:28 +04:00
utils.Check(err)
2013-06-22 00:17:52 +04:00
}
}
2013-06-26 19:48:34 +04:00
func transformCheckoutArgs(args *Args) error {
2013-12-11 10:05:26 +04:00
words := args.Words()
2014-04-01 00:01:05 +04:00
2013-12-11 10:05:26 +04:00
if len(words) == 0 {
return nil
}
2013-06-22 04:01:00 +04:00
2013-12-11 10:05:26 +04:00
checkoutURL := words[0]
var newBranchName string
if len(words) > 1 {
newBranchName = words[1]
}
2014-04-01 00:01:05 +04:00
url, err := github.ParseURL(checkoutURL)
if err != nil {
// not a valid GitHub URL
return nil
}
2013-06-22 05:02:29 +04:00
2013-12-11 10:05:26 +04:00
pullURLRegex := regexp.MustCompile("^pull/(\\d+)")
projectPath := url.ProjectPath()
if !pullURLRegex.MatchString(projectPath) {
2014-04-01 00:01:05 +04:00
// not a valid PR URL
2013-12-11 10:05:26 +04:00
return nil
}
2013-07-02 22:28:50 +04:00
err = sanitizeCheckoutFlags(args)
if err != nil {
return err
}
2013-12-11 10:05:26 +04:00
id := pullURLRegex.FindStringSubmatch(projectPath)[1]
2013-12-17 19:45:48 +04:00
gh := github.NewClient(url.Project.Host)
pullRequest, err := gh.PullRequest(url.Project, id)
2013-12-11 10:05:26 +04:00
if err != nil {
return err
}
2013-06-22 04:01:00 +04:00
2013-12-11 10:05:26 +04:00
if idx := args.IndexOfParam(newBranchName); idx >= 0 {
args.RemoveParam(idx)
}
2013-06-22 04:01:00 +04:00
repo, err := github.LocalRepo()
if err != nil {
return err
}
baseRemote, err := repo.RemoteForRepo(pullRequest.Base.Repo)
if err != nil {
return err
2013-06-22 04:01:00 +04:00
}
var headRemote *github.Remote
if pullRequest.IsSameRepo() {
headRemote = baseRemote
} else {
headRemote, _ = repo.RemoteForRepo(pullRequest.Head.Repo)
}
var newArgs []string
if headRemote != nil {
if newBranchName == "" {
newBranchName = pullRequest.Head.Ref
}
remoteBranch := fmt.Sprintf("%s/%s", headRemote.Name, pullRequest.Head.Ref)
refSpec := fmt.Sprintf("+refs/heads/%s:refs/remotes/%s", pullRequest.Head.Ref, remoteBranch)
if git.HasFile("refs", "heads", newBranchName) {
newArgs = append(newArgs, newBranchName)
args.After("git", "merge", "--ff-only", fmt.Sprintf("refs/remotes/%s", remoteBranch))
} else {
newArgs = append(newArgs, "-b", newBranchName, "--track", remoteBranch)
}
args.Before("git", "fetch", headRemote.Name, refSpec)
} else {
if newBranchName == "" {
if pullRequest.Head.Repo == nil {
newBranchName = fmt.Sprintf("pr-%s", id)
} else {
newBranchName = fmt.Sprintf("%s-%s", pullRequest.Head.Repo.Owner.Login, pullRequest.Head.Ref)
}
}
2016-10-03 20:37:36 +03:00
refSpec := fmt.Sprintf("refs/pull/%s/head:%s", id, newBranchName)
newArgs = append(newArgs, newBranchName)
args.Before("git", "fetch", baseRemote.Name, refSpec)
}
replaceCheckoutParam(args, checkoutURL, newArgs...)
2013-12-11 10:05:26 +04:00
return nil
2013-07-02 22:28:50 +04:00
}
func sanitizeCheckoutFlags(args *Args) error {
if i := args.IndexOfParam("-b"); i != -1 {
return fmt.Errorf("Unsupported flag -b when checking out pull request")
}
if i := args.IndexOfParam("--orphan"); i != -1 {
return fmt.Errorf("Unsupported flag --orphan when checking out pull request")
}
return nil
}
func replaceCheckoutParam(args *Args, checkoutURL string, replacement ...string) {
idx := args.IndexOfParam(checkoutURL)
args.RemoveParam(idx)
args.InsertParam(idx, replacement...)
}