1package repository
2
3import (
4 "crypto/sha1"
5 "fmt"
6 "github.com/MichaelMure/git-bug/util"
7 "github.com/pkg/errors"
8)
9
10// mockRepoForTest defines an instance of Repo that can be used for testing.
11type mockRepoForTest struct {
12 blobs map[util.Hash][]byte
13 trees map[util.Hash]string
14 commits map[util.Hash]commit
15 refs map[string]util.Hash
16}
17
18type commit struct {
19 treeHash util.Hash
20 parent util.Hash
21}
22
23func NewMockRepoForTest() Repo {
24 return &mockRepoForTest{
25 blobs: make(map[util.Hash][]byte),
26 trees: make(map[util.Hash]string),
27 commits: make(map[util.Hash]commit),
28 refs: make(map[string]util.Hash),
29 }
30}
31
32// GetPath returns the path to the repo.
33func (r *mockRepoForTest) GetPath() string {
34 return "~/mockRepo/"
35}
36
37func (r *mockRepoForTest) GetUserName() (string, error) {
38 return "René Descartes", nil
39}
40
41// GetUserEmail returns the email address that the user has used to configure git.
42func (r *mockRepoForTest) GetUserEmail() (string, error) {
43 return "user@example.com", nil
44}
45
46// GetCoreEditor returns the name of the editor that the user has used to configure git.
47func (r *mockRepoForTest) GetCoreEditor() (string, error) {
48 return "vi", nil
49}
50
51// PushRefs push git refs to a remote
52func (r *mockRepoForTest) PushRefs(remote string, refPattern string) error {
53 return nil
54}
55
56func (r *mockRepoForTest) FetchRefs(remote string, refPattern string, remoteRefPattern string) error {
57 return nil
58}
59
60func (r *mockRepoForTest) StoreData(data []byte) (util.Hash, error) {
61 rawHash := sha1.Sum(data)
62 hash := util.Hash(fmt.Sprintf("%x", rawHash))
63 r.blobs[hash] = data
64 return hash, nil
65}
66
67func (r *mockRepoForTest) ReadData(hash util.Hash) ([]byte, error) {
68 data, ok := r.blobs[hash]
69
70 if !ok {
71 return nil, errors.New("unknown hash")
72 }
73
74 return data, nil
75}
76
77func (r *mockRepoForTest) StoreTree(entries []TreeEntry) (util.Hash, error) {
78 buffer := prepareTreeEntries(entries)
79 rawHash := sha1.Sum(buffer.Bytes())
80 hash := util.Hash(fmt.Sprintf("%x", rawHash))
81 r.trees[hash] = buffer.String()
82
83 return hash, nil
84}
85
86func (r *mockRepoForTest) StoreCommit(treeHash util.Hash) (util.Hash, error) {
87 rawHash := sha1.Sum([]byte(treeHash))
88 hash := util.Hash(fmt.Sprintf("%x", rawHash))
89 r.commits[hash] = commit{
90 treeHash: treeHash,
91 }
92 return hash, nil
93}
94
95func (r *mockRepoForTest) StoreCommitWithParent(treeHash util.Hash, parent util.Hash) (util.Hash, error) {
96 rawHash := sha1.Sum([]byte(treeHash + parent))
97 hash := util.Hash(fmt.Sprintf("%x", rawHash))
98 r.commits[hash] = commit{
99 treeHash: treeHash,
100 parent: parent,
101 }
102 return hash, nil
103}
104
105func (r *mockRepoForTest) UpdateRef(ref string, hash util.Hash) error {
106 r.refs[ref] = hash
107 return nil
108}
109
110func (r *mockRepoForTest) RefExist(ref string) (bool, error) {
111 _, exist := r.refs[ref]
112 return exist, nil
113}
114
115func (r *mockRepoForTest) CopyRef(source string, dest string) error {
116 hash, exist := r.refs[source]
117
118 if !exist {
119 return errors.New("Unknown ref")
120 }
121
122 r.refs[dest] = hash
123 return nil
124}
125
126func (r *mockRepoForTest) ListRefs(refspec string) ([]string, error) {
127 keys := make([]string, len(r.refs))
128
129 i := 0
130 for k := range r.refs {
131 keys[i] = k
132 i++
133 }
134
135 return keys, nil
136}
137
138func (r *mockRepoForTest) ListCommits(ref string) ([]util.Hash, error) {
139 var hashes []util.Hash
140
141 hash := r.refs[ref]
142
143 for {
144 commit, ok := r.commits[hash]
145
146 if !ok {
147 break
148 }
149
150 hashes = append([]util.Hash{hash}, hashes...)
151 hash = commit.parent
152 }
153
154 return hashes, nil
155}
156
157func (r *mockRepoForTest) ListEntries(hash util.Hash) ([]TreeEntry, error) {
158 var data string
159
160 data, ok := r.trees[hash]
161
162 if !ok {
163 // Git will understand a commit hash to reach a tree
164 commit, ok := r.commits[hash]
165
166 if !ok {
167 return nil, errors.New("unknown hash")
168 }
169
170 data, ok = r.trees[commit.treeHash]
171
172 if !ok {
173 return nil, errors.New("unknown hash")
174 }
175 }
176
177 return readTreeEntries(data)
178}
179
180func (r *mockRepoForTest) FindCommonAncestor(hash1 util.Hash, hash2 util.Hash) (util.Hash, error) {
181 panic("implement me")
182}
183
184func (r *mockRepoForTest) GetTreeHash(commit util.Hash) (util.Hash, error) {
185 panic("implement me")
186}