github.com/trevoraustin/hub@v2.2.0-preview1.0.20141105230840-96d8bfc654cc+incompatible/commands/cherry_pick.go (about) 1 package commands 2 3 import ( 4 "regexp" 5 6 "github.com/github/hub/github" 7 "github.com/github/hub/utils" 8 ) 9 10 var cmdCherryPick = &Command{ 11 Run: cherryPick, 12 GitExtension: true, 13 Usage: "cherry-pick GITHUB-REF", 14 Short: "Apply the changes introduced by some existing commits", 15 Long: `Cherry-pick a commit from a fork using either full URL to the commit 16 or GitHub-flavored Markdown notation, which is user@sha. If the remote 17 doesn't yet exist, it will be added. A git-fetch(1) user is issued 18 prior to the cherry-pick attempt. 19 `, 20 } 21 22 func init() { 23 CmdRunner.Use(cmdCherryPick) 24 } 25 26 /* 27 $ gh cherry-pick https://github.com/jingweno/gh/commit/a319d88#comments 28 > git remote add -f jingweno git://github.com/jingweno/gh.git 29 > git cherry-pick a319d88 30 31 $ gh cherry-pick jingweno@a319d88 32 > git remote add -f jingweno git://github.com/jingweno/gh.git 33 > git cherry-pick a319d88 34 35 $ gh cherry-pick jingweno@SHA 36 > git fetch jingweno 37 > git cherry-pick SHA 38 */ 39 func cherryPick(command *Command, args *Args) { 40 if args.IndexOfParam("-m") == -1 && args.IndexOfParam("--mainline") == -1 { 41 transformCherryPickArgs(args) 42 } 43 } 44 45 func transformCherryPickArgs(args *Args) { 46 if args.IsParamsEmpty() { 47 return 48 } 49 50 ref := args.LastParam() 51 project, sha := parseCherryPickProjectAndSha(ref) 52 if project != nil { 53 args.ReplaceParam(args.IndexOfParam(ref), sha) 54 55 remote := gitRemoteForProject(project) 56 if remote != nil { 57 args.Before("git", "fetch", remote.Name) 58 } else { 59 args.Before("git", "remote", "add", "-f", project.Owner, project.GitURL("", "", false)) 60 } 61 } 62 } 63 64 func parseCherryPickProjectAndSha(ref string) (project *github.Project, sha string) { 65 url, err := github.ParseURL(ref) 66 if err == nil { 67 commitRegex := regexp.MustCompile("^commit\\/([a-f0-9]{7,40})") 68 projectPath := url.ProjectPath() 69 if commitRegex.MatchString(projectPath) { 70 sha = commitRegex.FindStringSubmatch(projectPath)[1] 71 project = url.Project 72 73 return 74 } 75 } 76 77 ownerWithShaRegexp := regexp.MustCompile("^([a-zA-Z0-9][a-zA-Z0-9-]*)@([a-f0-9]{7,40})$") 78 if ownerWithShaRegexp.MatchString(ref) { 79 matches := ownerWithShaRegexp.FindStringSubmatch(ref) 80 sha = matches[2] 81 localRepo, err := github.LocalRepo() 82 utils.Check(err) 83 84 project, err = localRepo.CurrentProject() 85 utils.Check(err) 86 project.Owner = matches[1] 87 } 88 89 return 90 }