compare.go 3.3 KB
Newer Older
J
Jingwen Owen Ou 已提交
1 2 3 4
package commands

import (
	"fmt"
D
Darin Minamoto 已提交
5
	"net/url"
6
	"regexp"
7
	"strings"
8

J
Jingwen Owen Ou 已提交
9 10
	"github.com/github/hub/github"
	"github.com/github/hub/utils"
J
Jingwen Owen Ou 已提交
11 12 13 14
)

var cmdCompare = &Command{
	Run:   compare,
15
	Usage: "compare [-u] [-b <BASE>] [<USER>] [[<START>...]<END>]",
16 17 18 19 20 21
	Long: `Open a GitHub compare page in a web browser.

## Options:
	-u
		Print the URL instead of opening it.

22 23 24
	-b <BASE>
		Base branch to compare.

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
	[<START>...]<END>
		Branch names, tag names, or commit SHAs specifying the range to compare.
		<END> defaults to the current branch name.

		If a range with two dots ('A..B') is given, it will be transformed into a
		range with three dots.

## Examples:
		$ hub compare refactor
		> open https://github.com/USER/REPO/compare/refactor

		$ hub compare v1.0..v1.1
		> open https://github.com/USER/REPO/compare/v1.0...v1.1

		$ hub compare -u jingweno feature
		> echo https://github.com/jingweno/REPO/compare/feature
M
Mislav Marohnić 已提交
41 42 43 44

## See also:

hub-browse(1), hub(1)
J
Jingwen Owen Ou 已提交
45 46 47
`,
}

J
Jingwen Owen Ou 已提交
48 49
var (
	flagCompareURLOnly bool
D
Darin Minamoto 已提交
50
	flagCompareBase    string
J
Jingwen Owen Ou 已提交
51
)
J
Jingwen Owen Ou 已提交
52 53

func init() {
54
	cmdCompare.Flag.BoolVarP(&flagCompareURLOnly, "url-only", "u", false, "URL only")
D
Darin Minamoto 已提交
55
	cmdCompare.Flag.StringVarP(&flagCompareBase, "base", "b", "", "BASE")
56 57

	CmdRunner.Use(cmdCompare)
J
Jingwen Owen Ou 已提交
58 59
}

J
Jingwen Owen Ou 已提交
60
func compare(command *Command, args *Args) {
61 62 63
	localRepo, err := github.LocalRepo()
	utils.Check(err)

J
Jingwen Owen Ou 已提交
64
	var (
J
Jingwen Owen Ou 已提交
65
		branch  *github.Branch
J
Jingwen Owen Ou 已提交
66 67 68 69
		project *github.Project
		r       string
	)

70
	branch, project, err = localRepo.RemoteBranchAndProject("", false)
J
Jingwen Owen Ou 已提交
71
	utils.Check(err)
J
Jingwen Owen Ou 已提交
72

73 74 75 76
	usageHelp := func() {
		utils.Check(fmt.Errorf("Usage: hub compare [-u] [-b <BASE>] [<USER>] [[<START>...]<END>]"))
	}

J
Jingwen Owen Ou 已提交
77
	if args.IsParamsEmpty() {
D
Darin Minamoto 已提交
78 79 80 81
		if branch == nil ||
			(branch.IsMaster() && flagCompareBase == "") ||
			(flagCompareBase == branch.ShortName()) {

82
			usageHelp()
D
Darin Minamoto 已提交
83 84 85 86 87
		} else {
			r = branch.ShortName()
			if flagCompareBase != "" {
				r = parseCompareRange(flagCompareBase + "..." + r)
			}
J
Jingwen Owen Ou 已提交
88
		}
J
Jingwen Owen Ou 已提交
89
	} else {
D
Darin Minamoto 已提交
90
		if flagCompareBase != "" {
91
			usageHelp()
J
Jingwen Owen Ou 已提交
92
		} else {
D
Darin Minamoto 已提交
93
			r = parseCompareRange(args.RemoveParam(args.ParamsSize() - 1))
94
			project, err = localRepo.CurrentProject()
D
Darin Minamoto 已提交
95 96 97
			if args.IsParamsEmpty() {
				utils.Check(err)
			} else {
98 99 100 101 102 103 104 105
				projectName := ""
				if err == nil {
					projectName = project.Name
				}
				project = github.NewProject(args.RemoveParam(args.ParamsSize()-1), projectName, "")
				if project.Name == "" {
					utils.Check(fmt.Errorf("error: missing project name (owner: %q)\n", project.Owner))
				}
D
Darin Minamoto 已提交
106
			}
J
Jingwen Owen Ou 已提交
107
		}
J
Jingwen Owen Ou 已提交
108 109
	}

110 111 112 113 114
	if project == nil {
		project, err = localRepo.CurrentProject()
		utils.Check(err)
	}

115
	subpage := utils.ConcatPaths("compare", rangeQueryEscape(r))
J
Jingwen Owen Ou 已提交
116
	url := project.WebURL("", "", subpage)
117

118 119
	args.NoForward()
	printBrowseOrCopy(args, url, !flagCompareURLOnly, false)
J
Jingwen Owen Ou 已提交
120 121
}

J
Jingwen Owen Ou 已提交
122 123
func parseCompareRange(r string) string {
	shaOrTag := fmt.Sprintf("((?:%s:)?\\w[\\w.-]+\\w)", OwnerRe)
J
Jingwen Owen Ou 已提交
124 125 126 127
	shaOrTagRange := fmt.Sprintf("^%s\\.\\.%s$", shaOrTag, shaOrTag)
	shaOrTagRangeRegexp := regexp.MustCompile(shaOrTagRange)
	return shaOrTagRangeRegexp.ReplaceAllString(r, "$1...$2")
}
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145

// characters we want to allow unencoded in compare views
var compareUnescaper = strings.NewReplacer(
	"%2F", "/",
	"%3A", ":",
	"%5E", "^",
	"%7E", "~",
	"%2A", "*",
	"%21", "!",
)

func rangeQueryEscape(r string) string {
	if strings.Contains(r, "..") {
		return r
	} else {
		return compareUnescaper.Replace(url.QueryEscape(r))
	}
}