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