compare.go 2.4 KB
Newer Older
J
Jingwen Owen Ou 已提交
1 2 3 4 5 6 7 8 9 10 11
package commands

import (
	"fmt"
	"github.com/jingweno/gh/github"
	"github.com/jingweno/gh/utils"
	"regexp"
)

var cmdCompare = &Command{
	Run:   compare,
12
	Usage: "compare [-u] [USER] [<START>...]<END>",
J
Jingwen Owen Ou 已提交
13 14
	Short: "Open a compare page on GitHub",
	Long: `Open a GitHub compare view page in the system's default web browser.
15 16 17
<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,
J
Jingwen Owen Ou 已提交
18
GitHub will compare against the base branch (the default is "master").
19 20
If <END> is omitted, GitHub compare view is opened for the current branch.
With "-u", outputs the URL rather than opening the browser.
J
Jingwen Owen Ou 已提交
21 22 23
`,
}

J
Jingwen Owen Ou 已提交
24 25 26
var (
	flagCompareURLOnly bool
)
J
Jingwen Owen Ou 已提交
27 28

func init() {
J
Jingwen Owen Ou 已提交
29
	cmdCompare.Flag.BoolVar(&flagCompareURLOnly, "u", false, "URL only")
30 31

	CmdRunner.Use(cmdCompare)
J
Jingwen Owen Ou 已提交
32 33
}

34 35 36 37 38 39 40 41 42 43
/*
  $ gh compare refactor
  > open https://github.com/CURRENT_REPO/compare/refactor

  $ gh compare 1.0..1.1
  > open https://github.com/CURRENT_REPO/compare/1.0...1.1

  $ gh compare -u other-user patch
  > open https://github.com/other-user/REPO/compare/patch
*/
J
Jingwen Owen Ou 已提交
44
func compare(command *Command, args *Args) {
J
Jingwen Owen Ou 已提交
45 46
	localRepo := github.LocalRepo()
	var (
J
Jingwen Owen Ou 已提交
47
		branch  *github.Branch
J
Jingwen Owen Ou 已提交
48 49 50 51 52
		project *github.Project
		r       string
		err     error
	)

J
Jingwen Owen Ou 已提交
53 54
	branch, project, err = localRepo.RemoteBranchAndProject("")
	utils.Check(err)
J
Jingwen Owen Ou 已提交
55

J
Jingwen Owen Ou 已提交
56 57 58
	if args.IsParamsEmpty() {
		master := localRepo.MasterBranch()
		if master.ShortName() == branch.ShortName() {
59
			err = fmt.Errorf(command.FormattedUsage())
J
Jingwen Owen Ou 已提交
60 61
			utils.Check(err)
		} else {
J
Jingwen Owen Ou 已提交
62
			r = branch.ShortName()
J
Jingwen Owen Ou 已提交
63
		}
J
Jingwen Owen Ou 已提交
64
	} else {
J
Jingwen Owen Ou 已提交
65 66 67 68 69 70 71
		r = parseCompareRange(args.RemoveParam(args.ParamsSize() - 1))
		if args.IsParamsEmpty() {
			project, err = localRepo.CurrentProject()
			utils.Check(err)
		} else {
			project = github.NewProject(args.RemoveParam(args.ParamsSize()-1), "", "")
		}
J
Jingwen Owen Ou 已提交
72 73 74
	}

	subpage := utils.ConcatPaths("compare", r)
J
Jingwen Owen Ou 已提交
75
	url := project.WebURL("", "", subpage)
J
Jingwen Owen Ou 已提交
76
	launcher, err := utils.BrowserLauncher()
J
Jingwen Owen Ou 已提交
77
	utils.Check(err)
78

J
Jingwen Owen Ou 已提交
79 80 81 82 83 84
	if flagCompareURLOnly {
		args.Replace("echo", url)
	} else {
		args.Replace(launcher[0], "", launcher[1:]...)
		args.AppendParams(url)
	}
J
Jingwen Owen Ou 已提交
85 86
}

J
Jingwen Owen Ou 已提交
87 88
func parseCompareRange(r string) string {
	shaOrTag := fmt.Sprintf("((?:%s:)?\\w[\\w.-]+\\w)", OwnerRe)
J
Jingwen Owen Ou 已提交
89 90 91 92
	shaOrTagRange := fmt.Sprintf("^%s\\.\\.%s$", shaOrTag, shaOrTag)
	shaOrTagRangeRegexp := regexp.MustCompile(shaOrTagRange)
	return shaOrTagRangeRegexp.ReplaceAllString(r, "$1...$2")
}