Skip to content

Commit

Permalink
Merge pull request #4 from vvatanabe/patch-2
Browse files Browse the repository at this point in the history
change internal communication via http to use git ls-remote
  • Loading branch information
vvatanabe authored May 27, 2019
2 parents 97673c3 + 117591b commit b407746
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions repository.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"os/exec"
"sort"
"strings"

Expand Down Expand Up @@ -118,7 +119,7 @@ func (b *BacklogRepository) OpenPullRequestList() error {
}

func (b *BacklogRepository) OpenPullRequest() error {
id, err := b.FindPullRequestIDFromRemote(b.head.Name().Short())
id, err := b.FindPullRequestIDFromRemote(b.head.Name().String())
if err != nil {
return err
}
Expand All @@ -128,44 +129,49 @@ func (b *BacklogRepository) OpenPullRequest() error {
PullRequestURL(id))
}

const (
refPrefix = "refs/"
refPullRequestPrefix = refPrefix + "pull/"
refPullRequestSuffix = "/head"
)

func (b *BacklogRepository) FindPullRequestIDFromRemote(branchName string) (string, error) {
r, err := b.repo.Remote("origin")
if err != nil {
return "", err
}

rfs, err := r.List(&git.ListOptions{})
cmd := exec.Command("git", "ls-remote", "-q")
out, err := cmd.Output()
if err != nil {
return "", err
}

var target *plumbing.Reference
for _, rf := range rfs {
if rf.Name().Short() == branchName {
target = rf
break
}
rfs := make(map[string]string)
remotes := strings.Split(strings.TrimSuffix(string(out), "\n"), "\n")
for _, v := range remotes {
delimited := strings.Split(v, "\t")
hash := delimited[0]
ref := delimited[1]
rfs[ref] = hash
}
if target == nil {

targetHash, ok := rfs[branchName]
if !ok {
return "", errors.New("not found a current branch in remote")
}

var prIDs []string
for _, rf := range rfs {
sn := rf.Name().Short()
if !strings.HasPrefix(sn, "pull/") || !strings.HasSuffix(sn, "/head") {
for refName, hash := range rfs {
if !strings.HasPrefix(refName, refPullRequestPrefix) || !strings.HasSuffix(refName, refPullRequestSuffix) {
continue
}
if rf.Hash() != target.Hash() {
if hash != targetHash {
continue
}
prID := strings.TrimPrefix(sn, "pull/")
prID = strings.TrimSuffix(prID, "/head")
prID := strings.TrimPrefix(refName, refPullRequestPrefix)
prID = strings.TrimSuffix(prID, refPullRequestSuffix)
prIDs = append(prIDs, prID)
}

if len(prIDs) == 0 {
return "", errors.New("not found a pull request")
return "", errors.New("not found a pull request related to current branch")
}

sort.Sort(sort.Reverse(sort.StringSlice(prIDs)))
Expand Down

0 comments on commit b407746

Please sign in to comment.