1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
package git
import (
"bytes"
"strings"
"time"
"golang.org/x/crypto/openpgp"
"golang.org/x/crypto/openpgp/armor"
"golang.org/x/crypto/openpgp/errors"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/object"
"gopkg.in/src-d/go-git.v4/plumbing/storer"
"gopkg.in/src-d/go-git.v4/storage/memory"
. "gopkg.in/check.v1"
"gopkg.in/src-d/go-billy.v4/memfs"
"gopkg.in/src-d/go-billy.v4/util"
)
func (s *WorktreeSuite) TestCommitInvalidOptions(c *C) {
r, err := Init(memory.NewStorage(), memfs.New())
c.Assert(err, IsNil)
w, err := r.Worktree()
c.Assert(err, IsNil)
hash, err := w.Commit("", &CommitOptions{})
c.Assert(err, Equals, ErrMissingAuthor)
c.Assert(hash.IsZero(), Equals, true)
}
func (s *WorktreeSuite) TestCommitInitial(c *C) {
expected := plumbing.NewHash("98c4ac7c29c913f7461eae06e024dc18e80d23a4")
fs := memfs.New()
storage := memory.NewStorage()
r, err := Init(storage, fs)
c.Assert(err, IsNil)
w, err := r.Worktree()
c.Assert(err, IsNil)
util.WriteFile(fs, "foo", []byte("foo"), 0644)
_, err = w.Add("foo")
c.Assert(err, IsNil)
hash, err := w.Commit("foo\n", &CommitOptions{Author: defaultSignature()})
c.Assert(hash, Equals, expected)
c.Assert(err, IsNil)
assertStorageStatus(c, r, 1, 1, 1, expected)
}
func (s *WorktreeSuite) TestCommitParent(c *C) {
expected := plumbing.NewHash("ef3ca05477530b37f48564be33ddd48063fc7a22")
fs := memfs.New()
w := &Worktree{
r: s.Repository,
Filesystem: fs,
}
err := w.Checkout(&CheckoutOptions{})
c.Assert(err, IsNil)
util.WriteFile(fs, "foo", []byte("foo"), 0644)
_, err = w.Add("foo")
c.Assert(err, IsNil)
hash, err := w.Commit("foo\n", &CommitOptions{Author: defaultSignature()})
c.Assert(hash, Equals, expected)
c.Assert(err, IsNil)
assertStorageStatus(c, s.Repository, 13, 11, 10, expected)
}
func (s *WorktreeSuite) TestCommitAll(c *C) {
expected := plumbing.NewHash("aede6f8c9c1c7ec9ca8d287c64b8ed151276fa28")
fs := memfs.New()
w := &Worktree{
r: s.Repository,
Filesystem: fs,
}
err := w.Checkout(&CheckoutOptions{})
c.Assert(err, IsNil)
util.WriteFile(fs, "LICENSE", []byte("foo"), 0644)
util.WriteFile(fs, "foo", []byte("foo"), 0644)
hash, err := w.Commit("foo\n", &CommitOptions{
All: true,
Author: defaultSignature(),
})
c.Assert(hash, Equals, expected)
c.Assert(err, IsNil)
assertStorageStatus(c, s.Repository, 13, 11, 10, expected)
}
func (s *WorktreeSuite) TestRemoveAndCommitAll(c *C) {
expected := plumbing.NewHash("907cd576c6ced2ecd3dab34a72bf9cf65944b9a9")
fs := memfs.New()
w := &Worktree{
r: s.Repository,
Filesystem: fs,
}
err := w.Checkout(&CheckoutOptions{})
c.Assert(err, IsNil)
util.WriteFile(fs, "foo", []byte("foo"), 0644)
_, err = w.Add("foo")
c.Assert(err, IsNil)
_, errFirst := w.Commit("Add in Repo\n", &CommitOptions{
Author: defaultSignature(),
})
c.Assert(errFirst, IsNil)
errRemove := fs.Remove("foo")
c.Assert(errRemove, IsNil)
hash, errSecond := w.Commit("Remove foo\n", &CommitOptions{
All: true,
Author: defaultSignature(),
})
c.Assert(errSecond, IsNil)
c.Assert(hash, Equals, expected)
c.Assert(err, IsNil)
assertStorageStatus(c, s.Repository, 13, 11, 11, expected)
}
func (s *WorktreeSuite) TestCommitSign(c *C) {
fs := memfs.New()
storage := memory.NewStorage()
r, err := Init(storage, fs)
c.Assert(err, IsNil)
w, err := r.Worktree()
c.Assert(err, IsNil)
util.WriteFile(fs, "foo", []byte("foo"), 0644)
_, err = w.Add("foo")
c.Assert(err, IsNil)
key := commitSignKey(c, true)
hash, err := w.Commit("foo\n", &CommitOptions{Author: defaultSignature(), SignKey: key})
c.Assert(err, IsNil)
// Verify the commit.
pks := new(bytes.Buffer)
pkw, err := armor.Encode(pks, openpgp.PublicKeyType, nil)
c.Assert(err, IsNil)
err = key.Serialize(pkw)
c.Assert(err, IsNil)
err = pkw.Close()
c.Assert(err, IsNil)
expectedCommit, err := r.CommitObject(hash)
c.Assert(err, IsNil)
actual, err := expectedCommit.Verify(pks.String())
c.Assert(err, IsNil)
c.Assert(actual.PrimaryKey, DeepEquals, key.PrimaryKey)
}
func (s *WorktreeSuite) TestCommitSignBadKey(c *C) {
fs := memfs.New()
storage := memory.NewStorage()
r, err := Init(storage, fs)
c.Assert(err, IsNil)
w, err := r.Worktree()
c.Assert(err, IsNil)
util.WriteFile(fs, "foo", []byte("foo"), 0644)
_, err = w.Add("foo")
c.Assert(err, IsNil)
key := commitSignKey(c, false)
_, err = w.Commit("foo\n", &CommitOptions{Author: defaultSignature(), SignKey: key})
c.Assert(err, Equals, errors.InvalidArgumentError("signing key is encrypted"))
}
func assertStorageStatus(
c *C, r *Repository,
treesCount, blobCount, commitCount int, head plumbing.Hash,
) {
trees, err := r.Storer.IterEncodedObjects(plumbing.TreeObject)
c.Assert(err, IsNil)
blobs, err := r.Storer.IterEncodedObjects(plumbing.BlobObject)
c.Assert(err, IsNil)
commits, err := r.Storer.IterEncodedObjects(plumbing.CommitObject)
c.Assert(err, IsNil)
c.Assert(lenIterEncodedObjects(trees), Equals, treesCount)
c.Assert(lenIterEncodedObjects(blobs), Equals, blobCount)
c.Assert(lenIterEncodedObjects(commits), Equals, commitCount)
ref, err := r.Head()
c.Assert(err, IsNil)
c.Assert(ref.Hash(), Equals, head)
}
func lenIterEncodedObjects(iter storer.EncodedObjectIter) int {
count := 0
iter.ForEach(func(plumbing.EncodedObject) error {
count++
return nil
})
return count
}
func defaultSignature() *object.Signature {
when, _ := time.Parse(object.DateFormat, "Thu May 04 00:03:43 2017 +0200")
return &object.Signature{
Name: "foo",
Email: "foo@foo.foo",
When: when,
}
}
func commitSignKey(c *C, decrypt bool) *openpgp.Entity {
s := strings.NewReader(armoredKeyRing)
es, err := openpgp.ReadArmoredKeyRing(s)
c.Assert(err, IsNil)
c.Assert(es, HasLen, 1)
c.Assert(es[0].Identities, HasLen, 1)
_, ok := es[0].Identities["foo bar <foo@foo.foo>"]
c.Assert(ok, Equals, true)
key := es[0]
if decrypt {
err = key.PrivateKey.Decrypt([]byte(keyPassphrase))
c.Assert(err, IsNil)
}
return key
}
const armoredKeyRing = `
-----BEGIN PGP PRIVATE KEY BLOCK-----
lQdGBFt2OHgBEADQpRmFm9X9xBfUljVs1B24MXWRHcEP5tx2k6Cp90sSz/ZOJcxH
RjzYuXjpkE7g/PaZxAMVS1PptJip/w1/+5l2gZ7RmzU/e3hKe4vALHzKMVp8t7Ta
0e2K3STxapCr9FNITjQRGOhnFwqiYoPCf9u5Iy8uszDH7HHnBZx+Nvbl95dDvmMs
aFUKMeaoFD19i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