2013-07-02 20:59:02 +04:00
|
|
|
package commands
|
|
|
|
|
2013-07-02 22:28:50 +04:00
|
|
|
import (
|
|
|
|
"fmt"
|
2014-04-01 04:09:13 +04:00
|
|
|
"regexp"
|
|
|
|
|
2014-02-10 20:22:36 +04:00
|
|
|
"github.com/github/hub/github"
|
|
|
|
"github.com/github/hub/utils"
|
2013-07-02 22:28:50 +04:00
|
|
|
)
|
2013-07-02 20:59:02 +04:00
|
|
|
|
|
|
|
var cmdMerge = &Command{
|
|
|
|
Run: merge,
|
|
|
|
GitExtension: true,
|
2016-01-24 11:56:18 +03:00
|
|
|
Usage: "merge <PULLREQ-URL>",
|
2018-09-03 15:26:13 +03:00
|
|
|
Long: `Merge a pull request locally with a message like the GitHub Merge Button.
|
|
|
|
|
|
|
|
This creates a local merge commit in the current branch, but does not actually
|
|
|
|
change the state of the pull request. However, the pull request will get
|
|
|
|
auto-closed and marked as "merged" as soon as the newly created merge commit is
|
|
|
|
pushed to the default branch of the remote repository.
|
2016-01-24 11:56:18 +03:00
|
|
|
|
|
|
|
## Examples:
|
|
|
|
$ hub merge https://github.com/jingweno/gh/pull/73
|
2016-09-14 03:49:04 +03:00
|
|
|
> git fetch origin refs/pull/73/head
|
|
|
|
> git merge FETCH_HEAD --no-ff -m "Merge pull request #73 from jingweno/feature..."
|
2016-01-24 18:50:01 +03:00
|
|
|
|
|
|
|
## See also:
|
|
|
|
|
|
|
|
hub-checkout(1), hub(1), git-merge(1)
|
2013-07-02 20:59:02 +04:00
|
|
|
`,
|
|
|
|
}
|
|
|
|
|
2013-12-30 02:18:14 +04:00
|
|
|
func init() {
|
|
|
|
CmdRunner.Use(cmdMerge)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2013-12-11 10:05:26 +04:00
|
|
|
mergeURL := words[0]
|
|
|
|
url, err := github.ParseURL(mergeURL)
|
2013-12-10 13:04:36 +04:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pullURLRegex := regexp.MustCompile("^pull/(\\d+)")
|
|
|
|
projectPath := url.ProjectPath()
|
|
|
|
if !pullURLRegex.MatchString(projectPath) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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-10 13:04:36 +04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-14 03:41:58 +03:00
|
|
|
repo, err := github.LocalRepo()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
remote, err := repo.RemoteForRepo(pullRequest.Base.Repo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-02-23 08:44:36 +03:00
|
|
|
branch := pullRequest.Head.Ref
|
|
|
|
headRepo := pullRequest.Head.Repo
|
|
|
|
if headRepo == nil {
|
|
|
|
return fmt.Errorf("Error: that fork is not available anymore")
|
2013-12-10 13:04:36 +04:00
|
|
|
}
|
|
|
|
|
2016-09-14 03:41:58 +03:00
|
|
|
args.Before("git", "fetch", remote.Name, fmt.Sprintf("refs/pull/%s/head", id))
|
2013-12-10 13:04:36 +04:00
|
|
|
|
|
|
|
// Remove pull request URL
|
2013-12-11 10:05:26 +04:00
|
|
|
idx := args.IndexOfParam(mergeURL)
|
2013-12-10 13:04:36 +04:00
|
|
|
args.RemoveParam(idx)
|
|
|
|
|
2016-09-14 03:41:58 +03:00
|
|
|
mergeMsg := fmt.Sprintf("Merge pull request #%s from %s/%s\n\n%s", id, headRepo.Owner.Login, branch, pullRequest.Title)
|
|
|
|
args.AppendParams("FETCH_HEAD", "-m", mergeMsg)
|
2013-12-10 13:04:36 +04:00
|
|
|
|
2015-08-12 13:34:15 +03:00
|
|
|
if args.IndexOfParam("--ff-only") == -1 && args.IndexOfParam("--squash") == -1 && args.IndexOfParam("--ff") == -1 {
|
2013-12-10 13:04:36 +04:00
|
|
|
i := args.IndexOfParam("-m")
|
|
|
|
args.InsertParam(i, "--no-ff")
|
2013-07-02 22:28:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|