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