hub/commands/merge.go

81 строка
2.0 KiB
Go
Исходник Обычный вид История

2013-07-02 20:59:02 +04:00
package commands
2013-07-02 22:28:50 +04:00
import (
"fmt"
2013-12-10 13:04:36 +04:00
"github.com/jingweno/gh/github"
2013-07-02 22:28:50 +04:00
"github.com/jingweno/gh/utils"
2013-12-10 13:04:36 +04:00
"regexp"
2013-07-02 22:28:50 +04:00
)
2013-07-02 20:59:02 +04:00
var cmdMerge = &Command{
Run: merge,
GitExtension: true,
Usage: "merge PULLREQ-URL",
Short: "Join two or more development histories (branches) together",
Long: `Merge the pull request with a commit message that includes the pull request
ID and title, similar to the GitHub Merge Button.
`,
}
2013-07-02 22:28:50 +04:00
/*
$ gh merge https://github.com/jingweno/gh/pull/73
> git fetch git://github.com/jingweno/gh.git +refs/heads/feature:refs/remotes/jingweno/feature
> git merge jingweno/feature --no-ff -m 'Merge pull request #73 from jingweno/feature...'
*/
2013-07-02 20:59:02 +04:00
func merge(command *Command, args *Args) {
2013-07-02 22:56:45 +04:00
if !args.IsParamsEmpty() {
2013-07-02 22:28:50 +04:00
err := transformMergeArgs(args)
2013-07-05 03:18:28 +04:00
utils.Check(err)
2013-07-02 22:28:50 +04:00
}
}
func transformMergeArgs(args *Args) error {
2013-12-10 13:04:36 +04:00
words := args.Words()
if len(words) == 0 {
return nil
}
mergeUrl := words[0]
url, err := github.ParseURL(mergeUrl)
if err != nil {
return nil
}
pullURLRegex := regexp.MustCompile("^pull/(\\d+)")
projectPath := url.ProjectPath()
if !pullURLRegex.MatchString(projectPath) {
return nil
}
id := pullURLRegex.FindStringSubmatch(projectPath)[1]
gh := github.NewClient(url.Project)
pullRequest, err := gh.PullRequest(id)
if err != nil {
return err
}
user, branch := parseUserBranchFromPR(pullRequest)
if pullRequest.Head.Repo.ID == 0 {
return fmt.Errorf("Error: %s's fork is not available anymore", user)
}
u := url.GitURL("", user, pullRequest.Head.Repo.Private)
mergeHead := fmt.Sprintf("%s/%s", user, branch)
ref := fmt.Sprintf("+refs/heads/%s:refs/remotes/%s", branch, mergeHead)
args.Before("git", "fetch", u, ref)
// Remove pull request URL
idx := args.IndexOfParam(mergeUrl)
args.RemoveParam(idx)
mergeMsg := fmt.Sprintf(`"Merge pull request #%v from %s\n\n%s"`, id, mergeHead, pullRequest.Title)
args.AppendParams(mergeHead, "-m", mergeMsg)
if args.IndexOfParam("--ff-only") == -1 {
i := args.IndexOfParam("-m")
args.InsertParam(i, "--no-ff")
2013-07-02 22:28:50 +04:00
}
return nil
}