hub/commands/apply.go

115 строки
2.6 KiB
Go
Исходник Обычный вид История

2013-07-11 22:23:59 +04:00
package commands
import (
"io"
"io/ioutil"
"os"
2013-07-19 02:08:32 +04:00
"regexp"
2014-07-26 01:52:24 +04:00
"github.com/github/hub/github"
"github.com/github/hub/utils"
2013-07-11 22:23:59 +04:00
)
var cmdApply = &Command{
Run: apply,
GitExtension: true,
Usage: "apply <GITHUB-URL>",
Long: `Download a patch from GitHub and apply it locally.
## Options:
<GITHUB-URL>
A URL to a pull request or commit on GitHub.
## Examples:
$ hub apply https://github.com/jingweno/gh/pull/55
> curl https://github.com/jingweno/gh/pull/55.patch -o /tmp/55.patch
> git apply /tmp/55.patch
2016-01-24 18:50:01 +03:00
## See also:
hub-am(1), hub(1), git-apply(1)
2013-07-11 22:23:59 +04:00
`,
}
2014-07-26 01:55:12 +04:00
var cmdAm = &Command{
Run: apply,
GitExtension: true,
Usage: "am [-3] <GITHUB-URL>",
Long: `Replicate commits from a GitHub pull request locally.
## Options:
-3
(Recommended) See git-am(1).
<GITHUB-URL>
A URL to a pull request or commit on GitHub.
## Examples:
$ hub am -3 https://github.com/jingweno/gh/pull/55
> curl https://github.com/jingweno/gh/pull/55.patch -o /tmp/55.patch
> git am -3 /tmp/55.patch
2016-01-24 18:50:01 +03:00
## See also:
hub-apply(1), hub-cherry-pick(1), hub(1), git-am(1)
2014-07-26 01:55:12 +04:00
`,
}
func init() {
CmdRunner.Use(cmdApply)
2014-07-26 01:55:12 +04:00
CmdRunner.Use(cmdAm)
}
2013-07-11 22:23:59 +04:00
func apply(command *Command, args *Args) {
2013-07-19 02:08:32 +04:00
if !args.IsParamsEmpty() {
transformApplyArgs(args)
}
}
func transformApplyArgs(args *Args) {
gistRegexp := regexp.MustCompile("^https?://gist\\.github\\.com/([\\w.-]+/)?([a-f0-9]+)")
commitRegexp := regexp.MustCompile("^(commit|pull/[0-9]+/commits)/([0-9a-f]+)")
pullRegexp := regexp.MustCompile("^pull/([0-9]+)")
2019-01-14 05:50:21 +03:00
for idx, arg := range args.Params {
2014-07-26 01:52:24 +04:00
var (
patch io.ReadCloser
apiError error
2014-07-26 01:52:24 +04:00
)
projectURL, err := github.ParseURL(arg)
if err == nil {
gh := github.NewClient(projectURL.Project.Host)
if match := commitRegexp.FindStringSubmatch(projectURL.ProjectPath()); match != nil {
patch, apiError = gh.CommitPatch(projectURL.Project, match[2])
} else if match := pullRegexp.FindStringSubmatch(projectURL.ProjectPath()); match != nil {
patch, apiError = gh.PullRequestPatch(projectURL.Project, match[1])
2013-07-19 02:08:32 +04:00
}
2014-07-26 01:52:24 +04:00
} else {
match := gistRegexp.FindStringSubmatch(arg)
if match != nil {
// TODO: support Enterprise gist
gh := github.NewClient(github.GitHubHost)
patch, apiError = gh.GistPatch(match[2])
2013-07-19 02:08:32 +04:00
}
2014-07-26 01:52:24 +04:00
}
2013-07-19 02:08:32 +04:00
utils.Check(apiError)
if patch == nil {
2014-07-26 01:52:24 +04:00
continue
}
2013-07-19 02:08:32 +04:00
tempDir := os.TempDir()
err = os.MkdirAll(tempDir, 0775)
utils.Check(err)
patchFile, err := ioutil.TempFile(tempDir, "hub")
utils.Check(err)
2013-07-19 02:08:32 +04:00
_, err = io.Copy(patchFile, patch)
utils.Check(err)
2014-07-26 01:52:24 +04:00
patchFile.Close()
patch.Close()
2014-07-26 01:52:24 +04:00
2019-01-14 05:50:21 +03:00
args.ReplaceParam(idx, patchFile.Name())
2013-07-19 02:08:32 +04:00
}
2013-07-11 22:23:59 +04:00
}