Skip to content

Commit c36b84e

Browse files
committed
Convert git.VerifyRef into a more versatile ShowRefs
This is so we can pass a list of full qualified refs and have them resolved for both existence and their commit hashes.
1 parent bc3f964 commit c36b84e

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

command/pr_checkout.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func prCheckout(cmd *cobra.Command, args []string) error {
6666
cmdQueue = append(cmdQueue, []string{"git", "fetch", headRemote.Name, refSpec})
6767

6868
// local branch already exists
69-
if git.VerifyRef("refs/heads/" + newBranchName) {
69+
if _, err := git.ShowRefs("refs/heads/" + newBranchName); err == nil {
7070
cmdQueue = append(cmdQueue, []string{"git", "checkout", newBranchName})
7171
cmdQueue = append(cmdQueue, []string{"git", "merge", "--ff-only", fmt.Sprintf("refs/remotes/%s", remoteBranch)})
7272
} else {

command/pr_checkout_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func TestPRCheckout_sameRepo(t *testing.T) {
4646
ranCommands := [][]string{}
4747
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
4848
switch strings.Join(cmd.Args, " ") {
49-
case "git show-ref --verify --quiet refs/heads/feature":
49+
case "git show-ref --verify -- refs/heads/feature":
5050
return &errorStub{"exit status: 1"}
5151
default:
5252
ranCommands = append(ranCommands, cmd.Args)
@@ -98,7 +98,7 @@ func TestPRCheckout_urlArg(t *testing.T) {
9898
ranCommands := [][]string{}
9999
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
100100
switch strings.Join(cmd.Args, " ") {
101-
case "git show-ref --verify --quiet refs/heads/feature":
101+
case "git show-ref --verify -- refs/heads/feature":
102102
return &errorStub{"exit status: 1"}
103103
default:
104104
ranCommands = append(ranCommands, cmd.Args)
@@ -147,7 +147,7 @@ func TestPRCheckout_urlArg_differentBase(t *testing.T) {
147147
ranCommands := [][]string{}
148148
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
149149
switch strings.Join(cmd.Args, " ") {
150-
case "git show-ref --verify --quiet refs/heads/feature":
150+
case "git show-ref --verify -- refs/heads/feature":
151151
return &errorStub{"exit status: 1"}
152152
default:
153153
ranCommands = append(ranCommands, cmd.Args)
@@ -210,7 +210,7 @@ func TestPRCheckout_branchArg(t *testing.T) {
210210
ranCommands := [][]string{}
211211
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
212212
switch strings.Join(cmd.Args, " ") {
213-
case "git show-ref --verify --quiet refs/heads/feature":
213+
case "git show-ref --verify -- refs/heads/feature":
214214
return &errorStub{"exit status: 1"}
215215
default:
216216
ranCommands = append(ranCommands, cmd.Args)
@@ -260,7 +260,7 @@ func TestPRCheckout_existingBranch(t *testing.T) {
260260
ranCommands := [][]string{}
261261
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
262262
switch strings.Join(cmd.Args, " ") {
263-
case "git show-ref --verify --quiet refs/heads/feature":
263+
case "git show-ref --verify -- refs/heads/feature":
264264
return &test.OutputStub{}
265265
default:
266266
ranCommands = append(ranCommands, cmd.Args)
@@ -313,7 +313,7 @@ func TestPRCheckout_differentRepo_remoteExists(t *testing.T) {
313313
ranCommands := [][]string{}
314314
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
315315
switch strings.Join(cmd.Args, " ") {
316-
case "git show-ref --verify --quiet refs/heads/feature":
316+
case "git show-ref --verify -- refs/heads/feature":
317317
return &errorStub{"exit status: 1"}
318318
default:
319319
ranCommands = append(ranCommands, cmd.Args)

git/git.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,31 @@ import (
1313
"github.com/cli/cli/utils"
1414
)
1515

16-
func VerifyRef(ref string) bool {
17-
showRef := exec.Command("git", "show-ref", "--verify", "--quiet", ref)
18-
err := utils.PrepareCmd(showRef).Run()
19-
return err == nil
16+
// Ref represents a git commit reference
17+
type Ref struct {
18+
Hash string
19+
Name string
20+
}
21+
22+
// ShowRefs resolves fully-qualified refs to commit hashes
23+
func ShowRefs(ref ...string) ([]Ref, error) {
24+
args := append([]string{"show-ref", "--verify", "--"}, ref...)
25+
showRef := exec.Command("git", args...)
26+
output, err := utils.PrepareCmd(showRef).Output()
27+
28+
var refs []Ref
29+
for _, line := range outputLines(output) {
30+
parts := strings.SplitN(line, " ", 2)
31+
if len(parts) < 2 {
32+
continue
33+
}
34+
refs = append(refs, Ref{
35+
Hash: parts[0],
36+
Name: parts[1],
37+
})
38+
}
39+
40+
return refs, err
2041
}
2142

2243
// CurrentBranch reads the checked-out branch for the git repository

0 commit comments

Comments
 (0)