2013-07-20 01:26:00 +04:00
|
|
|
package commands
|
|
|
|
|
2013-07-20 09:46:47 +04:00
|
|
|
import (
|
2016-09-08 16:26:30 +03:00
|
|
|
"fmt"
|
2014-04-02 00:40:02 +04:00
|
|
|
"regexp"
|
|
|
|
|
2014-02-10 20:22:36 +04:00
|
|
|
"github.com/github/hub/github"
|
|
|
|
"github.com/github/hub/utils"
|
2013-07-20 09:46:47 +04:00
|
|
|
)
|
|
|
|
|
2013-07-20 01:26:00 +04:00
|
|
|
var cmdCherryPick = &Command{
|
|
|
|
Run: cherryPick,
|
|
|
|
GitExtension: true,
|
2016-01-24 11:56:18 +03:00
|
|
|
Usage: `
|
|
|
|
cherry-pick <COMMIT-URL>
|
|
|
|
cherry-pick <USER>@<SHA>
|
|
|
|
`,
|
|
|
|
Long: `Cherry-pick a commit from a fork on GitHub.
|
|
|
|
|
2016-01-24 18:50:01 +03:00
|
|
|
## See also:
|
|
|
|
|
|
|
|
hub-am(1), hub(1), git-cherry-pick(1)
|
2013-07-20 01:26:00 +04:00
|
|
|
`,
|
|
|
|
}
|
|
|
|
|
2013-12-30 02:18:14 +04:00
|
|
|
func init() {
|
|
|
|
CmdRunner.Use(cmdCherryPick)
|
|
|
|
}
|
|
|
|
|
2013-07-20 01:26:00 +04:00
|
|
|
func cherryPick(command *Command, args *Args) {
|
2013-07-20 09:46:47 +04:00
|
|
|
if args.IndexOfParam("-m") == -1 && args.IndexOfParam("--mainline") == -1 {
|
|
|
|
transformCherryPickArgs(args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func transformCherryPickArgs(args *Args) {
|
2014-10-16 22:55:08 +04:00
|
|
|
if args.IsParamsEmpty() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-07-20 09:46:47 +04:00
|
|
|
ref := args.LastParam()
|
2016-09-08 19:23:55 +03:00
|
|
|
project, sha, isPrivate := parseCherryPickProjectAndSha(ref)
|
2013-07-20 09:46:47 +04:00
|
|
|
if project != nil {
|
|
|
|
args.ReplaceParam(args.IndexOfParam(ref), sha)
|
|
|
|
|
2016-09-08 19:23:55 +03:00
|
|
|
if remote := gitRemoteForProject(project); remote != nil {
|
2014-07-28 05:24:34 +04:00
|
|
|
args.Before("git", "fetch", remote.Name)
|
2013-07-20 09:46:47 +04:00
|
|
|
} else {
|
2016-09-12 04:32:09 +03:00
|
|
|
tmpName := "_hub-cherry-pick"
|
|
|
|
args.Before("git", "remote", "add", tmpName, project.GitURL("", "", isPrivate))
|
|
|
|
args.Before("git", "fetch", "-q", "--no-tags", tmpName)
|
|
|
|
args.Before("git", "remote", "rm", tmpName)
|
2013-07-20 09:46:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-08 19:23:55 +03:00
|
|
|
func parseCherryPickProjectAndSha(ref string) (project *github.Project, sha string, isPrivate bool) {
|
2016-09-08 16:26:30 +03:00
|
|
|
shaRe := "[a-f0-9]{7,40}"
|
|
|
|
|
|
|
|
var mainProject *github.Project
|
|
|
|
localRepo, mainProjectErr := github.LocalRepo()
|
|
|
|
if mainProjectErr == nil {
|
|
|
|
mainProject, mainProjectErr = localRepo.MainProject()
|
|
|
|
}
|
|
|
|
|
2013-07-20 09:46:47 +04:00
|
|
|
url, err := github.ParseURL(ref)
|
|
|
|
if err == nil {
|
|
|
|
projectPath := url.ProjectPath()
|
|
|
|
|
2016-09-08 16:26:30 +03:00
|
|
|
commitRegex := regexp.MustCompile(fmt.Sprintf("^commit/(%s)", shaRe))
|
|
|
|
if matches := commitRegex.FindStringSubmatch(projectPath); len(matches) > 0 {
|
|
|
|
sha = matches[1]
|
|
|
|
project = url.Project
|
2013-07-20 09:46:47 +04:00
|
|
|
return
|
|
|
|
}
|
2016-09-08 19:23:55 +03:00
|
|
|
|
|
|
|
pullRegex := regexp.MustCompile(fmt.Sprintf(`^pull/(\d+)/commits/(%s)`, shaRe))
|
|
|
|
if matches := pullRegex.FindStringSubmatch(projectPath); len(matches) > 0 {
|
|
|
|
pullId := matches[1]
|
|
|
|
sha = matches[2]
|
|
|
|
utils.Check(mainProjectErr)
|
|
|
|
api := github.NewClient(mainProject.Host)
|
|
|
|
pullRequest, err := api.PullRequest(url.Project, pullId)
|
|
|
|
utils.Check(err)
|
|
|
|
headRepo := pullRequest.Head.Repo
|
|
|
|
project = github.NewProject(headRepo.Owner.Login, headRepo.Name, mainProject.Host)
|
|
|
|
isPrivate = headRepo.Private
|
|
|
|
return
|
|
|
|
}
|
2013-07-20 09:46:47 +04:00
|
|
|
}
|
|
|
|
|
2016-09-08 16:26:30 +03:00
|
|
|
ownerWithShaRegexp := regexp.MustCompile(fmt.Sprintf("^(%s)@(%s)$", OwnerRe, shaRe))
|
|
|
|
if matches := ownerWithShaRegexp.FindStringSubmatch(ref); len(matches) > 0 {
|
|
|
|
utils.Check(mainProjectErr)
|
|
|
|
project = mainProject
|
2013-07-20 09:46:47 +04:00
|
|
|
project.Owner = matches[1]
|
2016-09-08 16:26:30 +03:00
|
|
|
sha = matches[2]
|
2013-07-20 09:46:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|