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