From d4f1d5659b9d23ff0768ed584ae320cb657c6f2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Mur=C3=A9?= Date: Tue, 8 Sep 2020 14:31:40 +0200 Subject: [PATCH] repo: fix some go-git implementation --- identity/identity_actions.go | 3 ++- repository/git.go | 4 ++-- repository/gogit.go | 24 +++++++++++++++++++----- repository/mock_repo.go | 4 ++-- repository/repo.go | 2 +- 5 files changed, 26 insertions(+), 11 deletions(-) diff --git a/identity/identity_actions.go b/identity/identity_actions.go index aa6a2a91..e33b75f9 100644 --- a/identity/identity_actions.go +++ b/identity/identity_actions.go @@ -4,9 +4,10 @@ import ( "fmt" "strings" + "github.com/pkg/errors" + "github.com/MichaelMure/git-bug/entity" "github.com/MichaelMure/git-bug/repository" - "github.com/pkg/errors" ) // Fetch retrieve updates from a remote diff --git a/repository/git.go b/repository/git.go index 85107ba5..37b79556 100644 --- a/repository/git.go +++ b/repository/git.go @@ -302,8 +302,8 @@ func (repo *GitRepo) RemoveRef(ref string) error { } // ListRefs will return a list of Git ref matching the given refspec -func (repo *GitRepo) ListRefs(refspec string) ([]string, error) { - stdout, err := repo.runGitCommand("for-each-ref", "--format=%(refname)", refspec) +func (repo *GitRepo) ListRefs(refPrefix string) ([]string, error) { + stdout, err := repo.runGitCommand("for-each-ref", "--format=%(refname)", refPrefix) if err != nil { return nil, err diff --git a/repository/gogit.go b/repository/gogit.go index bd8ada33..78bae1f9 100644 --- a/repository/gogit.go +++ b/repository/gogit.go @@ -7,6 +7,7 @@ import ( "os" stdpath "path" "path/filepath" + "strings" "sync" "time" @@ -420,7 +421,7 @@ func (repo *GoGitRepo) RemoveRef(ref string) error { return repo.r.Storer.RemoveReference(plumbing.ReferenceName(ref)) } -func (repo *GoGitRepo) ListRefs(refspec string) ([]string, error) { +func (repo *GoGitRepo) ListRefs(refPrefix string) ([]string, error) { refIter, err := repo.r.References() if err != nil { return nil, err @@ -428,9 +429,16 @@ func (repo *GoGitRepo) ListRefs(refspec string) ([]string, error) { refs := make([]string, 0) - for ref, _ := refIter.Next(); ref != nil; { - refs = append(refs, ref.String()) // TODO: Use format to search + err = refIter.ForEach(func(ref *plumbing.Reference) error { + if strings.HasPrefix(ref.Name().String(), refPrefix) { + refs = append(refs, ref.Name().String()) + } + return nil + }) + if err != nil { + return nil, err } + return refs, nil } @@ -454,10 +462,16 @@ func (repo *GoGitRepo) ListCommits(ref string) ([]Hash, error) { return nil, err } - var commits []Hash // TODO: Implement refspec - for commit, _ := commitIter.Next(); commit != nil; { + var commits []Hash + + err = commitIter.ForEach(func(commit *object.Commit) error { commits = append(commits, Hash(commit.Hash.String())) + return nil + }) + if err != nil { + return nil, err } + return commits, nil } diff --git a/repository/mock_repo.go b/repository/mock_repo.go index b3b4cb41..4e2c89bc 100644 --- a/repository/mock_repo.go +++ b/repository/mock_repo.go @@ -164,11 +164,11 @@ func (r *mockRepoForTest) CopyRef(source string, dest string) error { return nil } -func (r *mockRepoForTest) ListRefs(refspec string) ([]string, error) { +func (r *mockRepoForTest) ListRefs(refPrefix string) ([]string, error) { var keys []string for k := range r.refs { - if strings.HasPrefix(k, refspec) { + if strings.HasPrefix(k, refPrefix) { keys = append(keys, k) } } diff --git a/repository/repo.go b/repository/repo.go index 806edace..3deb6f1b 100644 --- a/repository/repo.go +++ b/repository/repo.go @@ -91,7 +91,7 @@ type RepoData interface { RemoveRef(ref string) error // ListRefs will return a list of Git ref matching the given refspec - ListRefs(refspec string) ([]string, error) + ListRefs(refPrefix string) ([]string, error) // RefExist will check if a reference exist in Git RefExist(ref string) (bool, error)