hub/commands/apply.go

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

2013-07-11 22:23:59 +04:00
package commands
import (
2013-07-19 02:08:32 +04:00
"fmt"
"os"
"path/filepath"
"regexp"
2014-07-26 01:52:24 +04:00
"github.com/github/hub/github"
2013-07-11 22:23:59 +04:00
)
var cmdApply = &Command{
Run: apply,
GitExtension: true,
Usage: "apply GITHUB-URL",
Short: "Apply a patch to files and/or to the index",
Long: `Downloads the patch file for the pull request or commit at the URL and
applies that patch from disk with git am or git apply. Similar to
cherry-pick, but doesn't add new remotes. git am creates commits while
preserving authorship info while <code>apply</code> only applies the
patch to the working copy.
`,
}
2014-07-26 01:55:12 +04:00
var cmdAm = &Command{
Run: apply,
GitExtension: true,
Usage: "am GITHUB-URL",
Short: "Apply a patch to files and/or to the index",
Long: `Downloads the patch file for the pull request or commit at the URL and
applies that patch from disk with git am or git apply. Similar to
cherry-pick, but doesn't add new remotes. git am creates commits while
preserving authorship info while <code>apply</code> only applies the
patch to the working copy.
`,
}
func init() {
CmdRunner.Use(cmdApply)
2014-07-26 01:55:12 +04:00
CmdRunner.Use(cmdAm)
}
2013-07-19 02:08:32 +04:00
/*
$ gh 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
$ git apply --ignore-whitespace https://github.com/jingweno/gh/commit/fdb9921
> curl https://github.com/jingweno/gh/commit/fdb9921.patch -o /tmp/fdb9921.patch
> git apply --ignore-whitespace /tmp/fdb9921.patch
$ git apply https://gist.github.com/8da7fb575debd88c54cf
> curl https://gist.github.com/8da7fb575debd88c54cf.txt -o /tmp/gist-8da7fb575debd88c54cf.txt
> git apply /tmp/gist-8da7fb575debd88c54cf.txt
*/
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) {
2014-07-26 01:52:24 +04:00
urlRegexp := regexp.MustCompile("^https?://(gist\\.)github\\.com/")
2014-07-28 10:21:33 +04:00
pullRegexp := regexp.MustCompile("^(pull|commit)/([0-9a-f]+)")
2014-07-26 01:52:24 +04:00
for _, arg := range args.Params {
var (
url string
gist bool
)
projectURL, err := github.ParseURL(arg)
if err == nil {
2014-07-28 10:21:33 +04:00
match := pullRegexp.FindStringSubmatch(projectURL.ProjectPath())
2014-07-26 01:52:24 +04:00
if match != nil {
url = projectURL.Project.WebURL("", "", match[1]+"/"+match[2])
2013-07-19 02:08:32 +04:00
}
2014-07-26 01:52:24 +04:00
} else {
gist = urlRegexp.MatchString(arg)
2013-07-19 02:08:32 +04:00
if gist {
2014-07-26 01:52:24 +04:00
url = arg
2013-07-19 02:08:32 +04:00
}
2014-07-26 01:52:24 +04:00
}
2013-07-19 02:08:32 +04:00
2014-07-26 01:52:24 +04:00
if url == "" {
continue
}
2013-07-19 02:08:32 +04:00
2014-07-26 01:52:24 +04:00
var ext string
if gist {
ext = ".txt"
} else {
ext = ".patch"
}
2013-07-19 02:08:32 +04:00
2014-07-26 01:52:24 +04:00
idx := args.IndexOfParam(arg)
if filepath.Ext(url) != ext {
url += ext
}
2013-07-19 02:08:32 +04:00
2014-07-26 01:52:24 +04:00
var prefix string
if gist {
prefix = "gist-"
2013-07-19 02:08:32 +04:00
}
2014-07-26 01:52:24 +04:00
patchFile := filepath.Join(os.TempDir(), prefix+filepath.Base(url))
args.Before("curl", "-#LA", fmt.Sprintf("gh %s", Version), url, "-o", patchFile)
args.Params[idx] = patchFile
2013-07-19 02:08:32 +04:00
}
2013-07-11 22:23:59 +04:00
}