提交 237dc108 编写于 作者: J Jingwen Owen Ou

Merge branch 'fix_am' into merge_master

......@@ -5,6 +5,8 @@ import (
"os"
"path/filepath"
"regexp"
"github.com/github/hub/github"
)
var cmdApply = &Command{
......@@ -20,8 +22,22 @@ patch to the working copy.
`,
}
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)
CmdRunner.Use(cmdAm)
}
/*
......@@ -44,20 +60,28 @@ func apply(command *Command, args *Args) {
}
func transformApplyArgs(args *Args) {
urlRegexp := regexp.MustCompile("^https?://(gist\\.)?github\\.com/")
for _, url := range args.Params {
if urlRegexp.MatchString(url) {
idx := args.IndexOfParam(url)
gist := urlRegexp.FindStringSubmatch(url)[1] == "gist."
fragmentRegexp := regexp.MustCompile("#.+")
url = fragmentRegexp.ReplaceAllString(url, "")
pullRegexp := regexp.MustCompile("(/pull/\\d+)/\\w*$")
if !gist {
if pullRegexp.MatchString(url) {
pull := pullRegexp.FindStringSubmatch(url)[1]
url = pullRegexp.ReplaceAllString(url, pull)
urlRegexp := regexp.MustCompile("^https?://(gist\\.)github\\.com/")
for _, arg := range args.Params {
var (
url string
gist bool
)
projectURL, err := github.ParseURL(arg)
if err == nil {
pullRegexp := regexp.MustCompile("/(pull|commit)/([0-9a-f]+)")
match := pullRegexp.FindStringSubmatch(projectURL.Path)
if match != nil {
url = projectURL.Project.WebURL("", "", match[1]+"/"+match[2])
}
} else {
gist = urlRegexp.MatchString(arg)
if gist {
url = arg
}
}
if url == "" {
continue
}
var ext string
......@@ -67,6 +91,7 @@ func transformApplyArgs(args *Args) {
ext = ".patch"
}
idx := args.IndexOfParam(arg)
if filepath.Ext(url) != ext {
url += ext
}
......@@ -80,8 +105,5 @@ func transformApplyArgs(args *Args) {
args.Before("curl", "-#LA", fmt.Sprintf("gh %s", Version), url, "-o", patchFile)
args.Params[idx] = patchFile
break
}
}
}
#!/bin/bash
set -e
url="$3"
file="$5"
if [ "${url%.patch}" = "$url" ]; then
echo "invalid pull request URL: $url" >&2
exit 1
fi
cat > "$file" <<OUT
From 7eb75a26ee8e402aad79fcf36a4c1461e3ec2592 Mon Sep 17 00:00:00 2001
From: Mislav <mislav.marohnic@gmail.com>
Date: Tue, 24 Jun 2014 11:07:05 -0700
Subject: [PATCH] Create a README
---
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..ce01362
--- /dev/null
+++ b/README.md
+hello
--
1.9.3
OUT
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册