From b4c56217ee1f45168f10df968d2f547ac8cee307 Mon Sep 17 00:00:00 2001 From: Jingwen Owen Ou Date: Sun, 2 Jun 2013 17:04:25 -0700 Subject: [PATCH] Implement gh compare --- commands/browse.go | 8 +------ commands/commands.go | 1 + commands/compare.go | 52 ++++++++++++++++++++++++++++++++++++++++ commands/compare_test.go | 14 +++++++++++ commands/utils.go | 17 +++++++++++++ 5 files changed, 85 insertions(+), 7 deletions(-) create mode 100644 commands/compare.go create mode 100644 commands/compare_test.go create mode 100644 commands/utils.go diff --git a/commands/browse.go b/commands/browse.go index 4bcaa18e..a9cebf14 100644 --- a/commands/browse.go +++ b/commands/browse.go @@ -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) } diff --git a/commands/commands.go b/commands/commands.go index 4a9d9c61..463616cb 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -43,6 +43,7 @@ var All = []*Command{ cmdPullRequest, cmdCiStatus, cmdBrowse, + cmdCompare, cmdHelp, cmdVersion, } diff --git a/commands/compare.go b/commands/compare.go new file mode 100644 index 00000000..8a0879d9 --- /dev/null +++ b/commands/compare.go @@ -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") +} diff --git a/commands/compare_test.go b/commands/compare_test.go new file mode 100644 index 00000000..4e41b741 --- /dev/null +++ b/commands/compare_test.go @@ -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)) +} diff --git a/commands/utils.go b/commands/utils.go new file mode 100644 index 00000000..206d5e3f --- /dev/null +++ b/commands/utils.go @@ -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() +}