2013-07-20 01:26:00 +04:00
|
|
|
package commands
|
|
|
|
|
2013-07-20 09:46:47 +04:00
|
|
|
import (
|
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,
|
|
|
|
Usage: "cherry-pick GITHUB-REF",
|
|
|
|
Short: "Apply the changes introduced by some existing commits",
|
|
|
|
Long: `Cherry-pick a commit from a fork using either full URL to the commit
|
|
|
|
or GitHub-flavored Markdown notation, which is user@sha. If the remote
|
|
|
|
doesn't yet exist, it will be added. A git-fetch(1) user is issued
|
|
|
|
prior to the cherry-pick attempt.
|
|
|
|
`,
|
|
|
|
}
|
|
|
|
|
2013-12-30 02:18:14 +04:00
|
|
|
func init() {
|
|
|
|
CmdRunner.Use(cmdCherryPick)
|
|
|
|
}
|
|
|
|
|
2013-07-20 09:46:47 +04:00
|
|
|
/*
|
|
|
|
$ gh cherry-pick https://github.com/jingweno/gh/commit/a319d88#comments
|
|
|
|
> git remote add -f jingweno git://github.com/jingweno/gh.git
|
|
|
|
> git cherry-pick a319d88
|
|
|
|
|
|
|
|
$ gh cherry-pick jingweno@a319d88
|
|
|
|
> git remote add -f jingweno git://github.com/jingweno/gh.git
|
|
|
|
> git cherry-pick a319d88
|
|
|
|
|
|
|
|
$ gh cherry-pick jingweno@SHA
|
|
|
|
> git fetch jingweno
|
|
|
|
> git cherry-pick SHA
|
|
|
|
*/
|
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) {
|
|
|
|
ref := args.LastParam()
|
|
|
|
project, sha := parseCherryPickProjectAndSha(ref)
|
|
|
|
if project != nil {
|
|
|
|
args.ReplaceParam(args.IndexOfParam(ref), sha)
|
|
|
|
|
2014-07-28 05:24:34 +04:00
|
|
|
remote := gitRemoteForProject(project)
|
|
|
|
if remote != nil {
|
|
|
|
args.Before("git", "fetch", remote.Name)
|
2013-07-20 09:46:47 +04:00
|
|
|
} else {
|
|
|
|
args.Before("git", "remote", "add", "-f", project.Owner, project.GitURL("", "", false))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseCherryPickProjectAndSha(ref string) (project *github.Project, sha string) {
|
|
|
|
url, err := github.ParseURL(ref)
|
|
|
|
if err == nil {
|
|
|
|
commitRegex := regexp.MustCompile("^commit\\/([a-f0-9]{7,40})")
|
|
|
|
projectPath := url.ProjectPath()
|
|
|
|
if commitRegex.MatchString(projectPath) {
|
|
|
|
sha = commitRegex.FindStringSubmatch(projectPath)[1]
|
2013-12-10 13:04:36 +04:00
|
|
|
project = url.Project
|
2013-07-20 09:46:47 +04:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-28 05:24:34 +04:00
|
|
|
ownerWithShaRegexp := regexp.MustCompile("^([a-zA-Z0-9][a-zA-Z0-9-]*)@([a-f0-9]{7,40})$")
|
2013-07-20 09:46:47 +04:00
|
|
|
if ownerWithShaRegexp.MatchString(ref) {
|
|
|
|
matches := ownerWithShaRegexp.FindStringSubmatch(ref)
|
|
|
|
sha = matches[2]
|
2014-04-02 00:40:02 +04:00
|
|
|
localRepo, err := github.LocalRepo()
|
|
|
|
utils.Check(err)
|
|
|
|
|
2014-07-28 05:24:34 +04:00
|
|
|
project, err = localRepo.CurrentProject()
|
2013-12-08 07:27:53 +04:00
|
|
|
utils.Check(err)
|
2013-07-20 09:46:47 +04:00
|
|
|
project.Owner = matches[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|