зеркало из https://github.com/mislav/hub.git
Merge branch 'compare'
This commit is contained in:
Коммит
39d0c43257
|
@ -1,7 +1,6 @@
|
|||
package commands
|
||||
|
||||
import (
|
||||
"github.com/jingweno/gh/cmd"
|
||||
"github.com/jingweno/gh/github"
|
||||
"github.com/jingweno/gh/utils"
|
||||
)
|
||||
|
@ -27,9 +26,6 @@ func init() {
|
|||
}
|
||||
|
||||
func browse(command *Command, args []string) {
|
||||
launcher, err := utils.BrowserLauncher()
|
||||
utils.Check(err)
|
||||
|
||||
project := github.CurrentProject()
|
||||
if flagBrowseSubpage == "tree" || flagBrowseSubpage == "commits" {
|
||||
repo := project.LocalRepo()
|
||||
|
@ -37,8 +33,6 @@ func browse(command *Command, args []string) {
|
|||
}
|
||||
|
||||
url := project.WebUrl(flagBrowseRepo, flagBrowseUser, flagBrowseSubpage)
|
||||
launcher = append(launcher, url)
|
||||
c := cmd.NewWithArray(launcher)
|
||||
err = c.Exec()
|
||||
err := browserCommand(url)
|
||||
utils.Check(err)
|
||||
}
|
||||
|
|
|
@ -43,6 +43,7 @@ var All = []*Command{
|
|||
cmdPullRequest,
|
||||
cmdCiStatus,
|
||||
cmdBrowse,
|
||||
cmdCompare,
|
||||
cmdHelp,
|
||||
cmdVersion,
|
||||
}
|
||||
|
|
|
@ -0,0 +1,52 @@
|
|||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/jingweno/gh/github"
|
||||
"github.com/jingweno/gh/utils"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
var cmdCompare = &Command{
|
||||
Run: compare,
|
||||
Usage: "compare [-u USER] [START...] END",
|
||||
Short: "Open a compare page on GitHub",
|
||||
Long: `Open a GitHub compare view page in the system's default web browser.
|
||||
START to END are branch names, tag names, or commit SHA1s specifying
|
||||
the range of history to compare. If a range with two dots (a..b) is given,
|
||||
it will be transformed into one with three dots. If START is omitted,
|
||||
GitHub will compare against the base branch (the default is "master").
|
||||
`,
|
||||
}
|
||||
|
||||
var flagCompareUser string
|
||||
|
||||
func init() {
|
||||
cmdCompare.Flag.StringVar(&flagCompareUser, "u", "", "USER")
|
||||
}
|
||||
|
||||
func compare(command *Command, args []string) {
|
||||
project := github.CurrentProject()
|
||||
|
||||
var r string
|
||||
if len(args) == 0 {
|
||||
repo := project.LocalRepo()
|
||||
r = repo.Head
|
||||
} else {
|
||||
r = args[0]
|
||||
}
|
||||
|
||||
r = transformToTripleDots(r)
|
||||
subpage := utils.ConcatPaths("compare", r)
|
||||
url := project.WebUrl("", flagCompareUser, subpage)
|
||||
err := browserCommand(url)
|
||||
utils.Check(err)
|
||||
}
|
||||
|
||||
func transformToTripleDots(r string) string {
|
||||
ownerRe := "[a-zA-Z0-9][a-zA-Z0-9-]*"
|
||||
shaOrTag := fmt.Sprintf("((?:%s:)?\\w[\\w.-]+\\w)", ownerRe)
|
||||
shaOrTagRange := fmt.Sprintf("^%s\\.\\.%s$", shaOrTag, shaOrTag)
|
||||
shaOrTagRangeRegexp := regexp.MustCompile(shaOrTagRange)
|
||||
return shaOrTagRangeRegexp.ReplaceAllString(r, "$1...$2")
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package commands
|
||||
|
||||
import (
|
||||
"github.com/bmizerany/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTransformToTripleDots(t *testing.T) {
|
||||
s := "1.0..2.0"
|
||||
assert.Equal(t, "1.0...2.0", transformToTripleDots(s))
|
||||
|
||||
s = "1.0...2.0"
|
||||
assert.Equal(t, "1.0...2.0", transformToTripleDots(s))
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package commands
|
||||
|
||||
import (
|
||||
"github.com/jingweno/gh/cmd"
|
||||
"github.com/jingweno/gh/utils"
|
||||
)
|
||||
|
||||
func browserCommand(url string) error {
|
||||
launcher, err := utils.BrowserLauncher()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
launcher = append(launcher, url)
|
||||
c := cmd.NewWithArray(launcher)
|
||||
return c.Exec()
|
||||
}
|
Загрузка…
Ссылка в новой задаче