2017-02-10 15:56:13 +03:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-02-26 22:17:01 +03:00
|
|
|
"os"
|
2017-02-10 15:56:13 +03:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/github/hub/github"
|
|
|
|
"github.com/github/hub/utils"
|
|
|
|
)
|
|
|
|
|
2017-02-26 12:42:07 +03:00
|
|
|
var (
|
2017-02-26 22:25:46 +03:00
|
|
|
cmdPr = &Command{
|
|
|
|
Run: printHelp,
|
|
|
|
Usage: "pr checkout <PULLREQ-NUMBER> [<BRANCH>]",
|
|
|
|
Long: `Check out the head of a pull request as a local branch.
|
2017-02-10 15:56:13 +03:00
|
|
|
|
|
|
|
## Examples:
|
2017-02-26 22:17:01 +03:00
|
|
|
$ hub pr checkout 73
|
|
|
|
> git fetch origin pull/73/head:jingweno-feature
|
|
|
|
> git checkout jingweno-feature
|
2017-02-10 15:56:13 +03:00
|
|
|
|
|
|
|
## See also:
|
|
|
|
|
2017-02-26 12:13:22 +03:00
|
|
|
hub-merge(1), hub(1), hub-checkout(1)
|
2017-02-26 22:25:46 +03:00
|
|
|
`,
|
2017-02-26 12:42:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
cmdCheckoutPr = &Command{
|
|
|
|
Key: "checkout",
|
2017-02-26 15:42:35 +03:00
|
|
|
Run: checkoutPr,
|
2017-02-26 12:42:07 +03:00
|
|
|
}
|
|
|
|
)
|
2017-02-10 15:56:13 +03:00
|
|
|
|
|
|
|
func init() {
|
2017-02-26 15:42:35 +03:00
|
|
|
cmdPr.Use(cmdCheckoutPr)
|
2017-02-26 12:13:22 +03:00
|
|
|
CmdRunner.Use(cmdPr)
|
2017-02-10 15:56:13 +03:00
|
|
|
}
|
|
|
|
|
2017-02-26 22:17:01 +03:00
|
|
|
func printHelp(command *Command, args *Args) {
|
|
|
|
fmt.Print(command.HelpText())
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2017-04-07 11:17:28 +03:00
|
|
|
/**
|
|
|
|
* Add a log messsage to /tmp/hub.log
|
|
|
|
*
|
|
|
|
* FIXME: Should be removed before PRing this.
|
|
|
|
*/
|
|
|
|
func johan(message string) {
|
|
|
|
f, err := os.OpenFile("/tmp/hub.log", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
if _, err = f.WriteString(message + "\n"); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-26 15:42:35 +03:00
|
|
|
func checkoutPr(command *Command, args *Args) {
|
2017-04-07 11:17:28 +03:00
|
|
|
johan("")
|
|
|
|
|
2017-02-10 15:56:13 +03:00
|
|
|
if args.ParamsSize() < 1 || args.ParamsSize() > 2 {
|
|
|
|
utils.Check(fmt.Errorf("Error: Expected one or two arguments, got %d", args.ParamsSize()))
|
|
|
|
}
|
|
|
|
|
|
|
|
prNumberString := args.GetParam(0)
|
|
|
|
_, err := strconv.Atoi(prNumberString)
|
|
|
|
utils.Check(err)
|
|
|
|
|
2017-04-07 12:04:56 +03:00
|
|
|
johan("ormen")
|
|
|
|
|
2017-02-10 15:56:13 +03:00
|
|
|
// Figure out the PR URL
|
|
|
|
localRepo, err := github.LocalRepo()
|
2017-04-07 12:04:56 +03:00
|
|
|
johan("eva")
|
2017-02-10 15:56:13 +03:00
|
|
|
utils.Check(err)
|
2017-04-07 12:04:56 +03:00
|
|
|
johan("adam")
|
2017-02-10 15:56:13 +03:00
|
|
|
baseProject, err := localRepo.MainProject()
|
|
|
|
utils.Check(err)
|
|
|
|
host, err := github.CurrentConfig().PromptForHost(baseProject.Host)
|
|
|
|
utils.Check(err)
|
|
|
|
client := github.NewClientWithHost(host)
|
|
|
|
pr, err := client.PullRequest(baseProject, prNumberString)
|
|
|
|
utils.Check(err)
|
|
|
|
|
|
|
|
if args.ParamsSize() == 1 {
|
|
|
|
args.Replace(args.Executable, "checkout", pr.HtmlUrl)
|
|
|
|
} else {
|
|
|
|
args.Replace(args.Executable, "checkout", pr.HtmlUrl, args.GetParam(1))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call into the checkout code which already provides the functionality we're
|
|
|
|
// after
|
|
|
|
err = transformCheckoutArgs(args)
|
|
|
|
utils.Check(err)
|
|
|
|
}
|