diff options
author | Michael Muré <batolettre@gmail.com> | 2020-06-27 22:00:15 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2020-06-27 22:00:15 +0200 |
commit | 939bcd57b9446b9dce78d7a8c3b15fa13e6320f7 (patch) | |
tree | b6f73782f2f400a89fe0a777502a98fe505a080b /repository/mock_repo.go | |
parent | 1e3dd82fd619f7b43b3193016232ef69f9707f42 (diff) | |
download | git-bug-939bcd57b9446b9dce78d7a8c3b15fa13e6320f7.tar.gz |
repo: more tests
Diffstat (limited to 'repository/mock_repo.go')
-rw-r--r-- | repository/mock_repo.go | 48 |
1 files changed, 41 insertions, 7 deletions
diff --git a/repository/mock_repo.go b/repository/mock_repo.go index 04d9355e..cf572d87 100644 --- a/repository/mock_repo.go +++ b/repository/mock_repo.go @@ -3,6 +3,7 @@ package repository import ( "crypto/sha1" "fmt" + "strings" "github.com/MichaelMure/git-bug/util/git" "github.com/MichaelMure/git-bug/util/lamport" @@ -151,12 +152,12 @@ func (r *mockRepoForTest) CopyRef(source string, dest string) error { } func (r *mockRepoForTest) ListRefs(refspec string) ([]string, error) { - keys := make([]string, len(r.refs)) + var keys []string - i := 0 for k := range r.refs { - keys[i] = k - i++ + if strings.HasPrefix(k, refspec) { + keys = append(keys, k) + } } return keys, nil @@ -181,7 +182,7 @@ func (r *mockRepoForTest) ListCommits(ref string) ([]git.Hash, error) { return hashes, nil } -func (r *mockRepoForTest) ListEntries(hash git.Hash) ([]TreeEntry, error) { +func (r *mockRepoForTest) ReadTree(hash git.Hash) ([]TreeEntry, error) { var data string data, ok := r.trees[hash] @@ -205,11 +206,44 @@ func (r *mockRepoForTest) ListEntries(hash git.Hash) ([]TreeEntry, error) { } func (r *mockRepoForTest) FindCommonAncestor(hash1 git.Hash, hash2 git.Hash) (git.Hash, error) { - panic("implement me") + ancestor1 := []git.Hash{hash1} + + for hash1 != "" { + c, ok := r.commits[hash1] + if !ok { + return "", fmt.Errorf("unknown commit %v", hash1) + } + ancestor1 = append(ancestor1, c.parent) + hash1 = c.parent + } + + for { + for _, ancestor := range ancestor1 { + if ancestor == hash2 { + return ancestor, nil + } + } + + c, ok := r.commits[hash2] + if !ok { + return "", fmt.Errorf("unknown commit %v", hash1) + } + + if c.parent == "" { + return "", fmt.Errorf("no ancestor found") + } + + hash2 = c.parent + } } func (r *mockRepoForTest) GetTreeHash(commit git.Hash) (git.Hash, error) { - panic("implement me") + c, ok := r.commits[commit] + if !ok { + return "", fmt.Errorf("unknown commit") + } + + return c.treeHash, nil } func (r *mockRepoForTest) GetOrCreateClock(name string) (lamport.Clock, error) { |