bug: remove use of the too recent %(refname:lstrip=-1) of git

Michael Muré created

fix #24

Change summary

bug/bug.go              | 20 ++++++++++++++++++--
repository/git.go       | 19 -------------------
repository/mock_repo.go | 16 ----------------
repository/repo.go      |  4 ----
4 files changed, 18 insertions(+), 41 deletions(-)

Detailed changes

bug/bug.go 🔗

@@ -58,7 +58,7 @@ func NewBug() *Bug {
 
 // FindLocalBug find an existing Bug matching a prefix
 func FindLocalBug(repo repository.Repo, prefix string) (*Bug, error) {
-	ids, err := repo.ListIds(bugsRefPattern)
+	ids, err := ListLocalIds(repo)
 
 	if err != nil {
 		return nil, err
@@ -255,7 +255,23 @@ func readAllBugs(repo repository.Repo, refPrefix string) <-chan StreamedBug {
 
 // ListLocalIds list all the available local bug ids
 func ListLocalIds(repo repository.Repo) ([]string, error) {
-	return repo.ListIds(bugsRefPattern)
+	refs, err := repo.ListRefs(bugsRefPattern)
+	if err != nil {
+		return nil, err
+	}
+
+	return refsToIds(refs), nil
+}
+
+func refsToIds(refs []string) []string {
+	ids := make([]string, len(refs))
+
+	for i, ref := range refs {
+		splitted := strings.Split(ref, "/")
+		ids[i] = splitted[len(splitted)-1]
+	}
+
+	return ids
 }
 
 // IsValid check if the Bug data is valid

repository/git.go 🔗

@@ -257,25 +257,6 @@ func (repo *GitRepo) ListRefs(refspec string) ([]string, error) {
 	return splitted, nil
 }
 
-// ListIds will return a list of Git ref matching the given refspec,
-// stripped to only the last part of the ref
-func (repo *GitRepo) ListIds(refspec string) ([]string, error) {
-	// the format option will strip the ref name to keep only the last part (ie, the bug id)
-	stdout, err := repo.runGitCommand("for-each-ref", "--format=%(refname:lstrip=-1)", refspec)
-
-	if err != nil {
-		return nil, err
-	}
-
-	splitted := strings.Split(stdout, "\n")
-
-	if len(splitted) == 1 && splitted[0] == "" {
-		return []string{}, nil
-	}
-
-	return splitted, nil
-}
-
 // RefExist will check if a reference exist in Git
 func (repo *GitRepo) RefExist(ref string) (bool, error) {
 	stdout, err := repo.runGitCommand("for-each-ref", ref)

repository/mock_repo.go 🔗

@@ -3,7 +3,6 @@ package repository
 import (
 	"crypto/sha1"
 	"fmt"
-	"strings"
 
 	"github.com/MichaelMure/git-bug/util"
 )
@@ -140,21 +139,6 @@ func (r *mockRepoForTest) ListRefs(refspec string) ([]string, error) {
 	return keys, nil
 }
 
-// ListIds will return a list of Git ref matching the given refspec,
-// stripped to only the last part of the ref
-func (r *mockRepoForTest) ListIds(refspec string) ([]string, error) {
-	keys := make([]string, len(r.refs))
-
-	i := 0
-	for k := range r.refs {
-		splitted := strings.Split(k, "/")
-		keys[i] = splitted[len(splitted)-1]
-		i++
-	}
-
-	return keys, nil
-}
-
 func (r *mockRepoForTest) ListCommits(ref string) ([]util.Hash, error) {
 	var hashes []util.Hash
 

repository/repo.go 🔗

@@ -49,10 +49,6 @@ type Repo interface {
 	// ListRefs will return a list of Git ref matching the given refspec
 	ListRefs(refspec string) ([]string, error)
 
-	// ListIds will return a list of Git ref matching the given refspec,
-	// stripped to only the last part of the ref
-	ListIds(refspec string) ([]string, error)
-
 	// RefExist will check if a reference exist in Git
 	RefExist(ref string) (bool, error)