Debian Codegraph @

repository_test.go

raw
1 package git
2
3 import (
4 "bytes"
5 "context"
6 "errors"
7 "fmt"
8 "io"
9 "os"
10 "os/exec"
11 "os/user"
12 "path"
13 "path/filepath"
14 "regexp"
15 "strings"
16 "testing"
17 "time"
18
19 fixtures "github.com/go-git/go-git-fixtures/v4"
20
21 "github.com/ProtonMail/go-crypto/openpgp"
22 "github.com/ProtonMail/go-crypto/openpgp/armor"
23 openpgperr "github.com/ProtonMail/go-crypto/openpgp/errors"
24
25 "github.com/go-git/go-git/v5/config"
26 "github.com/go-git/go-git/v5/plumbing"
27 "github.com/go-git/go-git/v5/plumbing/cache"
28 "github.com/go-git/go-git/v5/plumbing/object"
29 "github.com/go-git/go-git/v5/plumbing/storer"
30 "github.com/go-git/go-git/v5/plumbing/transport"
31 "github.com/go-git/go-git/v5/storage"
32 "github.com/go-git/go-git/v5/storage/filesystem"
33 "github.com/go-git/go-git/v5/storage/memory"
34
35 "github.com/go-git/go-billy/v5"
36 "github.com/go-git/go-billy/v5/memfs"
37 "github.com/go-git/go-billy/v5/osfs"
38 "github.com/go-git/go-billy/v5/util"
39 . "gopkg.in/check.v1"
40 )
41
42 type RepositorySuite struct {
43 BaseSuite
44 }
45
46 var _ = Suite(&RepositorySuite{})
47
48 func (s *RepositorySuite) TestInit(c *C) {
49 r, err := Init(memory.NewStorage(), memfs.New())
50 c.Assert(err, IsNil)
51 c.Assert(r, NotNil)
52
53 cfg, err := r.Config()
54 c.Assert(err, IsNil)
55 c.Assert(cfg.Core.IsBare, Equals, false)
56
57 // check the HEAD to see what the default branch is
58 createCommit(c, r)
59 ref, err := r.Head()
60 c.Assert(err, IsNil)
61 c.Assert(ref.Name().String(), Equals, plumbing.Master.String())
62 }
63
64 func (s *RepositorySuite) TestInitWithOptions(c *C) {
65 r, err := InitWithOptions(memory.NewStorage(), memfs.New(), InitOptions{
66 DefaultBranch: "refs/heads/foo",
67 })
68 c.Assert(err, IsNil)
69 c.Assert(r, NotNil)
70 createCommit(c, r)
71
72 ref, err := r.Head()
73 c.Assert(err, IsNil)
74 c.Assert(ref.Name().String(), Equals, "refs/heads/foo")
75
76 }
77
78 func (s *RepositorySuite) TestInitWithInvalidDefaultBranch(c *C) {
79 _, err := InitWithOptions(memory.NewStorage(), memfs.New(), InitOptions{
80 DefaultBranch: "foo",
81 })
82 c.Assert(err, NotNil)
83 }
84
85 func createCommit(c *C, r *Repository) plumbing.Hash {
86 // Create a commit so there is a HEAD to check
87 wt, err := r.Worktree()
88 c.Assert(err, IsNil)
89
90 rm, err := wt.Filesystem.Create("foo.txt")
91 c.Assert(err, IsNil)
92
93 _, err = rm.Write([]byte("foo text"))
94 c.Assert(err, IsNil)
95
96 _, err = wt.Add("foo.txt")
97 c.Assert(err, IsNil)
98
99 author := object.Signature{
100 Name: "go-git",
101 Email: "[email protected]",
102 When: time.Now(),
103 }
104
105 h, err := wt.Commit("test commit message", &CommitOptions{
106 All: true,
107 Author: &author,
108 Committer: &author,
109 AllowEmptyCommits: true,
110 })
111 c.Assert(err, IsNil)
112 return h
113 }
114
115 func (s *RepositorySuite) TestInitNonStandardDotGit(c *C) {
116 dir := c.MkDir()
117
118 fs := osfs.New(dir)
119 dot, _ := fs.Chroot("storage")
120 st := filesystem.NewStorage(dot, cache.NewObjectLRUDefault())
121
122 wt, _ := fs.Chroot("worktree")
123 r, err := Init(st, wt)
124 c.Assert(err, IsNil)
125 c.Assert(r, NotNil)
126
127 f, err := fs.Open(fs.Join("worktree", ".git"))
128 c.Assert(err, IsNil)
129 defer func() { _ = f.Close() }()
130
131 all, err := io.ReadAll(f)
132 c.Assert(err, IsNil)
133 c.Assert(string(all), Equals, fmt.Sprintf("gitdir: %s\n", filepath.Join("..", "storage")))
134
135 cfg, err := r.Config()
136 c.Assert(err, IsNil)
137 c.Assert(cfg.Core.Worktree, Equals, filepath.Join("..", "worktree"))
138 }
139
140 func (s *RepositorySuite) TestInitStandardDotGit(c *C) {
141 dir := c.MkDir()
142
143 fs := osfs.New(dir)
144 dot, _ := fs.Chroot(".git")
145 st := filesystem.NewStorage(dot, cache.NewObjectLRUDefault())
146
147 r, err := Init(st, fs)
148 c.Assert(err, IsNil)
149 c.Assert(r, NotNil)
150
151 l, err := fs.ReadDir(".git")
152 c.Assert(err, IsNil)
153 c.Assert(len(l) > 0, Equals, true)
154
155 cfg, err := r.Config()
156 c.Assert(err, IsNil)
157 c.Assert(cfg.Core.Worktree, Equals, "")
158 }
159
160 func (s *RepositorySuite) TestInitBare(c *C) {
161 r, err := Init(memory.NewStorage(), nil)
162 c.Assert(err, IsNil)
163 c.Assert(r, NotNil)
164
165 cfg, err := r.Config()
166 c.Assert(err, IsNil)
167 c.Assert(cfg.Core.IsBare, Equals, true)
168
169 }
170
171 func (s *RepositorySuite) TestInitAlreadyExists(c *C) {
172 st := memory.NewStorage()
173
174 r, err := Init(st, nil)
175 c.Assert(err, IsNil)
176 c.Assert(r, NotNil)
177
178 r, err = Init(st, nil)
179 c.Assert(err, Equals, ErrRepositoryAlreadyExists)
180 c.Assert(r, IsNil)
181 }
182
183 func (s *RepositorySuite) TestOpen(c *C) {
184 st := memory.NewStorage()
185
186 r, err := Init(st, memfs.New())
187 c.Assert(err, IsNil)
188 c.Assert(r, NotNil)
189
190 r, err = Open(st, memfs.New())
191 c.Assert(err, IsNil)
192 c.Assert(r, NotNil)
193 }
194
195 func (s *RepositorySuite) TestOpenBare(c *C) {
196 st := memory.NewStorage()
197
198 r, err := Init(st, nil)
199 c.Assert(err, IsNil)
200 c.Assert(r, NotNil)
201
202 r, err = Open(st, nil)
203 c.Assert(err, IsNil)
204 c.Assert(r, NotNil)
205 }
206
207 func (s *RepositorySuite) TestOpenBareMissingWorktree(c *C) {
208 st := memory.NewStorage()
209
210 r, err := Init(st, memfs.New())
211 c.Assert(err, IsNil)
212 c.Assert(r, NotNil)
213
214 r, err = Open(st, nil)
215 c.Assert(err, IsNil)
216 c.Assert(r, NotNil)
217 }
218
219 func (s *RepositorySuite) TestOpenNotExists(c *C) {
220 r, err := Open(memory.NewStorage(), nil)
221 c.Assert(err, Equals, ErrRepositoryNotExists)
222 c.Assert(r, IsNil)
223 }
224
225 func (s *RepositorySuite) TestClone(c *C) {
226 r, err := Clone(memory.NewStorage(), nil, &CloneOptions{
227 URL: s.GetBasicLocalRepositoryURL(),
228 })
229
230 c.Assert(err, IsNil)
231
232 remotes, err := r.Remotes()
233 c.Assert(err, IsNil)
234 c.Assert(remotes, HasLen, 1)
235 }
236
237 func (s *RepositorySuite) TestCloneContext(c *C) {
238 ctx, cancel := context.WithCancel(context.Background())
239 cancel()
240
241 r, err := CloneContext(ctx, memory.NewStorage(), nil, &CloneOptions{
242 URL: s.GetBasicLocalRepositoryURL(),
243 })
244
245 c.Assert(r, NotNil)
246 c.Assert(err, Equals, context.Canceled)
247 }
248
249 func (s *RepositorySuite) TestCloneMirror(c *C) {
250 r, err := Clone(memory.NewStorage(), nil, &CloneOptions{
251 URL: fixtures.Basic().One().URL,
252 Mirror: true,
253 })
254
255 c.Assert(err, IsNil)
256
257 refs, err := r.References()
258 var count int
259 refs.ForEach(func(r *plumbing.Reference) error { c.Log(r); count++; return nil })
260 c.Assert(err, IsNil)
261 // 6 refs total from github.com/git-fixtures/basic.git:
262 // - HEAD
263 // - refs/heads/master
264 // - refs/heads/branch
265 // - refs/pull/1/head
266 // - refs/pull/2/head
267 // - refs/pull/2/merge
268 c.Assert(count, Equals, 6)
269
270 cfg, err := r.Config()
271 c.Assert(err, IsNil)
272
273 c.Assert(cfg.Core.IsBare, Equals, true)
274 c.Assert(cfg.Remotes[DefaultRemoteName].Validate(), IsNil)
275 c.Assert(cfg.Remotes[DefaultRemoteName].Mirror, Equals, true)
276 }
277
278 func (s *RepositorySuite) TestCloneWithTags(c *C) {
279 url := s.GetLocalRepositoryURL(
280 fixtures.ByURL("https://github.com/git-fixtures/tags.git").One(),
281 )
282
283 r, err := Clone(memory.NewStorage(), nil, &CloneOptions{URL: url, Tags: NoTags})
284 c.Assert(err, IsNil)
285
286 remotes, err := r.Remotes()
287 c.Assert(err, IsNil)
288 c.Assert(remotes, HasLen, 1)
289
290 i, err := r.References()
291 c.Assert(err, IsNil)
292
293 var count int
294 i.ForEach(func(r *plumbing.Reference) error { count++; return nil })
295
296 c.Assert(count, Equals, 3)
297 }
298
299 func (s *RepositorySuite) TestCloneSparse(c *C) {
300 fs := memfs.New()
301 r, err := Clone(memory.NewStorage(), fs, &CloneOptions{
302 URL: s.GetBasicLocalRepositoryURL(),
303 NoCheckout: true,
304 })
305 c.Assert(err, IsNil)
306
307 w, err := r.Worktree()
308 c.Assert(err, IsNil)
309
310 sparseCheckoutDirectories := []string{"go", "json", "php"}
311 c.Assert(w.Checkout(&CheckoutOptions{
312 Branch: "refs/heads/master",
313 SparseCheckoutDirectories: sparseCheckoutDirectories,
314 }), IsNil)
315
316 fis, err := fs.ReadDir(".")
317 c.Assert(err, IsNil)
318 for _, fi := range fis {
319 c.Assert(fi.IsDir(), Equals, true)
320 var oneOfSparseCheckoutDirs bool
321
322 for _, sparseCheckoutDirectory := range sparseCheckoutDirectories {
323 if strings.HasPrefix(fi.Name(), sparseCheckoutDirectory) {
324 oneOfSparseCheckoutDirs = true
325 }
326 }
327 c.Assert(oneOfSparseCheckoutDirs, Equals, true)
328 }
329 }
330
331 func (s *RepositorySuite) TestCreateRemoteAndRemote(c *C) {
332 r, _ := Init(memory.NewStorage(), nil)
333 remote, err := r.CreateRemote(&config.RemoteConfig{
334 Name: "foo",
335 URLs: []string{"http://foo/foo.git"},
336 })
337
338 c.Assert(err, IsNil)
339 c.Assert(remote.Config().Name, Equals, "foo")
340
341 alt, err := r.Remote("foo")
342 c.Assert(err, IsNil)
343 c.Assert(alt, Not(Equals), remote)
344 c.Assert(alt.Config().Name, Equals, "foo")
345 }
346
347 func (s *RepositorySuite) TestCreateRemoteInvalid(c *C) {
348 r, _ := Init(memory.NewStorage(), nil)
349 remote, err := r.CreateRemote(&config.RemoteConfig{})
350
351 c.Assert(err, Equals, config.ErrRemoteConfigEmptyName)
352 c.Assert(remote, IsNil)
353 }
354
355 func (s *RepositorySuite) TestCreateRemoteAnonymous(c *C) {
356 r, _ := Init(memory.NewStorage(), nil)
357 remote, err := r.CreateRemoteAnonymous(&config.RemoteConfig{
358 Name: "anonymous",
359 URLs: []string{"http://foo/foo.git"},
360 })
361
362 c.Assert(err, IsNil)
363 c.Assert(remote.Config().Name, Equals, "anonymous")
364 }
365
366 func (s *RepositorySuite) TestCreateRemoteAnonymousInvalidName(c *C) {
367 r, _ := Init(memory.NewStorage(), nil)
368 remote, err := r.CreateRemoteAnonymous(&config.RemoteConfig{
369 Name: "not_anonymous",
370 URLs: []string{"http://foo/foo.git"},
371 })
372
373 c.Assert(err, Equals, ErrAnonymousRemoteName)
374 c.Assert(remote, IsNil)
375 }
376
377 func (s *RepositorySuite) TestCreateRemoteAnonymousInvalid(c *C) {
378 r, _ := Init(memory.NewStorage(), nil)
379 remote, err := r.CreateRemoteAnonymous(&config.RemoteConfig{})
380
381 c.Assert(err, Equals, config.ErrRemoteConfigEmptyName)
382 c.Assert(remote, IsNil)
383 }
384
385 func (s *RepositorySuite) TestDeleteRemote(c *C) {
386 r, _ := Init(memory.NewStorage(), nil)
387 _, err := r.CreateRemote(&config.RemoteConfig{
388 Name: "foo",
389 URLs: []string{"http://foo/foo.git"},
390 })
391
392 c.Assert(err, IsNil)
393
394 err = r.DeleteRemote("foo")
395 c.Assert(err, IsNil)
396
397 alt, err := r.Remote("foo")
398 c.Assert(err, Equals, ErrRemoteNotFound)
399 c.Assert(alt, IsNil)
400 }
401
402 func (s *RepositorySuite) TestEmptyCreateBranch(c *C) {
403 r, _ := Init(memory.NewStorage(), nil)
404 err := r.CreateBranch(&config.Branch{})
405
406 c.Assert(err, NotNil)
407 }
408
409 func (s *RepositorySuite) TestInvalidCreateBranch(c *C) {
410 r, _ := Init(memory.NewStorage(), nil)
411 err := r.CreateBranch(&config.Branch{
412 Name: "-foo",
413 })
414
415 c.Assert(err, NotNil)
416 }
417
418 func (s *RepositorySuite) TestCreateBranchAndBranch(c *C) {
419 r, _ := Init(memory.NewStorage(), nil)
420 testBranch := &config.Branch{
421 Name: "foo",
422 Remote: "origin",
423 Merge: "refs/heads/foo",
424 }
425 err := r.CreateBranch(testBranch)
426
427 c.Assert(err, IsNil)
428 cfg, err := r.Config()
429 c.Assert(err, IsNil)
430 c.Assert(len(cfg.Branches), Equals, 1)
431 branch := cfg.Branches["foo"]
432 c.Assert(branch.Name, Equals, testBranch.Name)
433 c.Assert(branch.Remote, Equals, testBranch.Remote)
434 c.Assert(branch.Merge, Equals, testBranch.Merge)
435
436 branch, err = r.Branch("foo")
437 c.Assert(err, IsNil)
438 c.Assert(branch.Name, Equals, testBranch.Name)
439 c.Assert(branch.Remote, Equals, testBranch.Remote)
440 c.Assert(branch.Merge, Equals, testBranch.Merge)
441 }
442
443 func (s *RepositorySuite) TestMergeFF(c *C) {
444 r, err := Init(memory.NewStorage(), memfs.New())
445 c.Assert(err, IsNil)
446 c.Assert(r, NotNil)
447
448 createCommit(c, r)
449 createCommit(c, r)
450 createCommit(c, r)
451 lastCommit := createCommit(c, r)
452
453 wt, err := r.Worktree()
454 c.Assert(err, IsNil)
455
456 targetBranch := plumbing.NewBranchReferenceName("foo")
457 err = wt.Checkout(&CheckoutOptions{
458 Hash: lastCommit,
459 Create: true,
460 Branch: targetBranch,
461 })
462 c.Assert(err, IsNil)
463
464 createCommit(c, r)
465 fooHash := createCommit(c, r)
466
467 // Checkout the master branch so that we can try to merge foo into it.
468 err = wt.Checkout(&CheckoutOptions{
469 Branch: plumbing.Master,
470 })
471 c.Assert(err, IsNil)
472
473 head, err := r.Head()
474 c.Assert(err, IsNil)
475 c.Assert(head.Hash(), Equals, lastCommit)
476
477 targetRef := plumbing.NewHashReference(targetBranch, fooHash)
478 c.Assert(targetRef, NotNil)
479
480 err = r.Merge(*targetRef, MergeOptions{
481 Strategy: FastForwardMerge,
482 })
483 c.Assert(err, IsNil)
484
485 head, err = r.Head()
486 c.Assert(err, IsNil)
487 c.Assert(head.Hash(), Equals, fooHash)
488 }
489
490 func (s *RepositorySuite) TestMergeFF_Invalid(c *C) {
491 r, err := Init(memory.NewStorage(), memfs.New())
492 c.Assert(err, IsNil)
493 c.Assert(r, NotNil)
494
495 // Keep track of the first commit, which will be the
496 // reference to create the target branch so that we
497 // can simulate a non-ff merge.
498 firstCommit := createCommit(c, r)
499 createCommit(c, r)
500 createCommit(c, r)
501 lastCommit := createCommit(c, r)
502
503 wt, err := r.Worktree()
504 c.Assert(err, IsNil)
505
506 targetBranch := plumbing.NewBranchReferenceName("foo")
507 err = wt.Checkout(&CheckoutOptions{
508 Hash: firstCommit,
509 Create: true,
510 Branch: targetBranch,
511 })
512
513 c.Assert(err, IsNil)
514
515 createCommit(c, r)
516 h := createCommit(c, r)
517
518 // Checkout the master branch so that we can try to merge foo into it.
519 err = wt.Checkout(&CheckoutOptions{
520 Branch: plumbing.Master,
521 })
522 c.Assert(err, IsNil)
523
524 head, err := r.Head()
525 c.Assert(err, IsNil)
526 c.Assert(head.Hash(), Equals, lastCommit)
527
528 targetRef := plumbing.NewHashReference(targetBranch, h)
529 c.Assert(targetRef, NotNil)
530
531 err = r.Merge(*targetRef, MergeOptions{
532 Strategy: MergeStrategy(10),
533 })
534 c.Assert(err, Equals, ErrUnsupportedMergeStrategy)
535
536 // Failed merge operations must not change HEAD.
537 head, err = r.Head()
538 c.Assert(err, IsNil)
539 c.Assert(head.Hash(), Equals, lastCommit)
540
541 err = r.Merge(*targetRef, MergeOptions{})
542 c.Assert(err, Equals, ErrFastForwardMergeNotPossible)
543
544 head, err = r.Head()
545 c.Assert(err, IsNil)
546 c.Assert(head.Hash(), Equals, lastCommit)
547 }
548
549 func (s *RepositorySuite) TestCreateBranchUnmarshal(c *C) {
550 r, _ := Init(memory.NewStorage(), nil)
551
552 expected := []byte(`[core]
553 bare = true
554 [remote "foo"]
555 url = http://foo/foo.git
556 fetch = +refs/heads/*:refs/remotes/foo/*
557 [branch "foo"]
558 remote = origin
559 merge = refs/heads/foo
560 [branch "master"]
561 remote = origin
562 merge = refs/heads/master
563 `)
564
565 _, err := r.CreateRemote(&config.RemoteConfig{
566 Name: "foo",
567 URLs: []string{"http://foo/foo.git"},
568 })
569 c.Assert(err, IsNil)
570 testBranch1 := &config.Branch{
571 Name: "master",
572 Remote: "origin",
573 Merge: "refs/heads/master",
574 }
575 testBranch2 := &config.Branch{
576 Name: "foo",
577 Remote: "origin",
578 Merge: "refs/heads/foo",
579 }
580 err = r.CreateBranch(testBranch1)
581 c.Assert(err, IsNil)
582 err = r.CreateBranch(testBranch2)
583 c.Assert(err, IsNil)
584
585 cfg, err := r.Config()
586 c.Assert(err, IsNil)
587 marshaled, err := cfg.Marshal()
588 c.Assert(err, IsNil)
589 c.Assert(string(expected), Equals, string(marshaled))
590 }
591
592 func (s *RepositorySuite) TestBranchInvalid(c *C) {
593 r, _ := Init(memory.NewStorage(), nil)
594 branch, err := r.Branch("foo")
595
596 c.Assert(err, NotNil)
597 c.Assert(branch, IsNil)
598 }
599
600 func (s *RepositorySuite) TestCreateBranchInvalid(c *C) {
601 r, _ := Init(memory.NewStorage(), nil)
602 err := r.CreateBranch(&config.Branch{})
603
604 c.Assert(err, NotNil)
605
606 testBranch := &config.Branch{
607 Name: "foo",
608 Remote: "origin",
609 Merge: "refs/heads/foo",
610 }
611 err = r.CreateBranch(testBranch)
612 c.Assert(err, IsNil)
613 err = r.CreateBranch(testBranch)
614 c.Assert(err, NotNil)
615 }
616
617 func (s *RepositorySuite) TestDeleteBranch(c *C) {
618 r, _ := Init(memory.NewStorage(), nil)
619 testBranch := &config.Branch{
620 Name: "foo",
621 Remote: "origin",
622 Merge: "refs/heads/foo",
623 }
624 err := r.CreateBranch(testBranch)
625
626 c.Assert(err, IsNil)
627
628 err = r.DeleteBranch("foo")
629 c.Assert(err, IsNil)
630
631 b, err := r.Branch("foo")
632 c.Assert(err, Equals, ErrBranchNotFound)
633 c.Assert(b, IsNil)
634
635 err = r.DeleteBranch("foo")
636 c.Assert(err, Equals, ErrBranchNotFound)
637 }
638
639 func (s *RepositorySuite) TestPlainInit(c *C) {
640 dir := c.MkDir()
641
642 r, err := PlainInit(dir, true)
643 c.Assert(err, IsNil)
644 c.Assert(r, NotNil)
645
646 cfg, err := r.Config()
647 c.Assert(err, IsNil)
648 c.Assert(cfg.Core.IsBare, Equals, true)
649 }
650
651 func (s *RepositorySuite) TestPlainInitWithOptions(c *C) {
652 dir := c.MkDir()
653
654 r, err := PlainInitWithOptions(dir, &PlainInitOptions{
655 InitOptions: InitOptions{
656 DefaultBranch: "refs/heads/foo",
657 },
658 Bare: false,
659 })
660 c.Assert(err, IsNil)
661 c.Assert(r, NotNil)
662
663 cfg, err := r.Config()
664 c.Assert(err, IsNil)
665 c.Assert(cfg.Core.IsBare, Equals, false)
666
667 createCommit(c, r)
668
669 ref, err := r.Head()
670 c.Assert(err, IsNil)
671 c.Assert(ref.Name().String(), Equals, "refs/heads/foo")
672 }
673
674 func (s *RepositorySuite) TestPlainInitAlreadyExists(c *C) {
675 dir := c.MkDir()
676
677 r, err := PlainInit(dir, true)
678 c.Assert(err, IsNil)
679 c.Assert(r, NotNil)
680
681 r, err = PlainInit(dir, true)
682 c.Assert(err, Equals, ErrRepositoryAlreadyExists)
683 c.Assert(r, IsNil)
684 }
685
686 func (s *RepositorySuite) TestPlainOpen(c *C) {
687 dir := c.MkDir()
688
689 r, err := PlainInit(dir, false)
690 c.Assert(err, IsNil)
691 c.Assert(r, NotNil)
692
693 r, err = PlainOpen(dir)
694 c.Assert(err, IsNil)
695 c.Assert(r, NotNil)
696 }
697
698 func (s *RepositorySuite) TestPlainOpenTildePath(c *C) {
699 dir, clean := s.TemporalHomeDir()
700 defer clean()
701
702 r, err := PlainInit(dir, false)
703 c.Assert(err, IsNil)
704 c.Assert(r, NotNil)
705
706 currentUser, err := user.Current()
707 c.Assert(err, IsNil)
708 // remove domain for windows
709 username := currentUser.Username[strings.Index(currentUser.Username, "\\")+1:]
710
711 homes := []string{"~/", "~" + username + "/"}
712 for _, home := range homes {
713 path := strings.Replace(dir, strings.Split(dir, ".tmp")[0], home, 1)
714
715 r, err = PlainOpen(path)
716 c.Assert(err, IsNil)
717 c.Assert(r, NotNil)
718 }
719 }
720
721 func (s *RepositorySuite) TestPlainOpenBare(c *C) {
722 dir := c.MkDir()
723
724 r, err := PlainInit(dir, true)
725 c.Assert(err, IsNil)
726 c.Assert(r, NotNil)
727
728 r, err = PlainOpen(dir)
729 c.Assert(err, IsNil)
730 c.Assert(r, NotNil)
731 }
732
733 func (s *RepositorySuite) TestPlainOpenNotBare(c *C) {
734 dir := c.MkDir()
735
736 r, err := PlainInit(dir, false)
737 c.Assert(err, IsNil)
738 c.Assert(r, NotNil)
739
740 r, err = PlainOpen(filepath.Join(dir, ".git"))
741 c.Assert(err, IsNil)
742 c.Assert(r, NotNil)
743 }
744
745 func (s *RepositorySuite) testPlainOpenGitFile(c *C, f func(string, string) string) {
746 fs := s.TemporalFilesystem(c)
747
748 dir, err := util.TempDir(fs, "", "plain-open")
749 c.Assert(err, IsNil)
750
751 r, err := PlainInit(fs.Join(fs.Root(), dir), true)
752 c.Assert(err, IsNil)
753 c.Assert(r, NotNil)
754
755 altDir, err := util.TempDir(fs, "", "plain-open")
756 c.Assert(err, IsNil)
757
758 err = util.WriteFile(fs, fs.Join(altDir, ".git"),
759 []byte(f(fs.Join(fs.Root(), dir), fs.Join(fs.Root(), altDir))),
760 0644,
761 )
762
763 c.Assert(err, IsNil)
764
765 r, err = PlainOpen(fs.Join(fs.Root(), altDir))
766 c.Assert(err, IsNil)
767 c.Assert(r, NotNil)
768 }
769
770 func (s *RepositorySuite) TestPlainOpenBareAbsoluteGitDirFile(c *C) {
771 s.testPlainOpenGitFile(c, func(dir, altDir string) string {
772 return fmt.Sprintf("gitdir: %s\n", dir)
773 })
774 }
775
776 func (s *RepositorySuite) TestPlainOpenBareAbsoluteGitDirFileNoEOL(c *C) {
777 s.testPlainOpenGitFile(c, func(dir, altDir string) string {
778 return fmt.Sprintf("gitdir: %s", dir)
779 })
780 }
781
782 func (s *RepositorySuite) TestPlainOpenBareRelativeGitDirFile(c *C) {
783 s.testPlainOpenGitFile(c, func(dir, altDir string) string {
784 dir, err := filepath.Rel(altDir, dir)
785 c.Assert(err, IsNil)
786 return fmt.Sprintf("gitdir: %s\n", dir)
787 })
788 }
789
790 func (s *RepositorySuite) TestPlainOpenBareRelativeGitDirFileNoEOL(c *C) {
791 s.testPlainOpenGitFile(c, func(dir, altDir string) string {
792 dir, err := filepath.Rel(altDir, dir)
793 c.Assert(err, IsNil)
794 return fmt.Sprintf("gitdir: %s\n", dir)
795 })
796 }
797
798 func (s *RepositorySuite) TestPlainOpenBareRelativeGitDirFileTrailingGarbage(c *C) {
799 fs := s.TemporalFilesystem(c)
800
801 dir, err := util.TempDir(fs, "", "")
802 c.Assert(err, IsNil)
803
804 r, err := PlainInit(dir, true)
805 c.Assert(err, IsNil)
806 c.Assert(r, NotNil)
807
808 altDir, err := util.TempDir(fs, "", "")
809 c.Assert(err, IsNil)
810
811 err = util.WriteFile(fs, fs.Join(altDir, ".git"),
812 []byte(fmt.Sprintf("gitdir: %s\nTRAILING", fs.Join(fs.Root(), altDir))),
813 0644,
814 )
815 c.Assert(err, IsNil)
816
817 r, err = PlainOpen(altDir)
818 c.Assert(err, Equals, ErrRepositoryNotExists)
819 c.Assert(r, IsNil)
820 }
821
822 func (s *RepositorySuite) TestPlainOpenBareRelativeGitDirFileBadPrefix(c *C) {
823 fs := s.TemporalFilesystem(c)
824
825 dir, err := util.TempDir(fs, "", "")
826 c.Assert(err, IsNil)
827
828 r, err := PlainInit(fs.Join(fs.Root(), dir), true)
829 c.Assert(err, IsNil)
830 c.Assert(r, NotNil)
831
832 altDir, err := util.TempDir(fs, "", "")
833 c.Assert(err, IsNil)
834
835 err = util.WriteFile(fs, fs.Join(altDir, ".git"), []byte(
836 fmt.Sprintf("xgitdir: %s\n", fs.Join(fs.Root(), dir)),
837 ), 0644)
838
839 c.Assert(err, IsNil)
840
841 r, err = PlainOpen(fs.Join(fs.Root(), altDir))
842 c.Assert(err, ErrorMatches, ".*gitdir.*")
843 c.Assert(r, IsNil)
844 }
845
846 func (s *RepositorySuite) TestPlainOpenNotExists(c *C) {
847 r, err := PlainOpen("/not-exists/")
848 c.Assert(err, Equals, ErrRepositoryNotExists)
849 c.Assert(r, IsNil)
850 }
851
852 func (s *RepositorySuite) TestPlainOpenDetectDotGit(c *C) {
853 fs := s.TemporalFilesystem(c)
854
855 dir, err := util.TempDir(fs, "", "")
856 c.Assert(err, IsNil)
857
858 subdir := filepath.Join(dir, "a", "b")
859 err = fs.MkdirAll(subdir, 0755)
860 c.Assert(err, IsNil)
861
862 file := fs.Join(subdir, "file.txt")
863 f, err := fs.Create(file)
864 c.Assert(err, IsNil)
865 f.Close()
866
867 r, err := PlainInit(fs.Join(fs.Root(), dir), false)
868 c.Assert(err, IsNil)
869 c.Assert(r, NotNil)
870
871 opt := &PlainOpenOptions{DetectDotGit: true}
872 r, err = PlainOpenWithOptions(fs.Join(fs.Root(), subdir), opt)
873 c.Assert(err, IsNil)
874 c.Assert(r, NotNil)
875
876 r, err = PlainOpenWithOptions(fs.Join(fs.Root(), file), opt)
877 c.Assert(err, IsNil)
878 c.Assert(r, NotNil)
879
880 optnodetect := &PlainOpenOptions{DetectDotGit: false}
881 r, err = PlainOpenWithOptions(fs.Join(fs.Root(), file), optnodetect)
882 c.Assert(err, NotNil)
883 c.Assert(r, IsNil)
884 }
885
886 func (s *RepositorySuite) TestPlainOpenNotExistsDetectDotGit(c *C) {
887 dir := c.MkDir()
888
889 opt := &PlainOpenOptions{DetectDotGit: true}
890 r, err := PlainOpenWithOptions(dir, opt)
891 c.Assert(err, Equals, ErrRepositoryNotExists)
892 c.Assert(r, IsNil)
893 }
894
895 func (s *RepositorySuite) TestPlainClone(c *C) {
896 dir := c.MkDir()
897
898 r, err := PlainClone(dir, false, &CloneOptions{
899 URL: s.GetBasicLocalRepositoryURL(),
900 })
901
902 c.Assert(err, IsNil)
903
904 remotes, err := r.Remotes()
905 c.Assert(err, IsNil)
906 c.Assert(remotes, HasLen, 1)
907 cfg, err := r.Config()
908 c.Assert(err, IsNil)
909 c.Assert(cfg.Branches, HasLen, 1)
910 c.Assert(cfg.Branches["master"].Name, Equals, "master")
911 }
912
913 func (s *RepositorySuite) TestPlainCloneBareAndShared(c *C) {
914 dir := c.MkDir()
915
916 remote := s.GetBasicLocalRepositoryURL()
917
918 r, err := PlainClone(dir, true, &CloneOptions{
919 URL: remote,
920 Shared: true,
921 })
922 c.Assert(err, IsNil)
923
924 altpath := path.Join(dir, "objects", "info", "alternates")
925 _, err = os.Stat(altpath)
926 c.Assert(err, IsNil)
927
928 data, err := os.ReadFile(altpath)
929 c.Assert(err, IsNil)
930
931 line := path.Join(remote, GitDirName, "objects") + "\n"
932 c.Assert(string(data), Equals, line)
933
934 cfg, err := r.Config()
935 c.Assert(err, IsNil)
936 c.Assert(cfg.Branches, HasLen, 1)
937 c.Assert(cfg.Branches["master"].Name, Equals, "master")
938 }
939
940 func (s *RepositorySuite) TestPlainCloneShared(c *C) {
941 dir := c.MkDir()
942
943 remote := s.GetBasicLocalRepositoryURL()
944
945 r, err := PlainClone(dir, false, &CloneOptions{
946 URL: remote,
947 Shared: true,
948 })
949 c.Assert(err, IsNil)
950
951 altpath := path.Join(dir, GitDirName, "objects", "info", "alternates")
952 _, err = os.Stat(altpath)
953 c.Assert(err, IsNil)
954
955 data, err := os.ReadFile(altpath)
956 c.Assert(err, IsNil)
957
958 line := path.Join(remote, GitDirName, "objects") + "\n"
959 c.Assert(string(data), Equals, line)
960
961 cfg, err := r.Config()
962 c.Assert(err, IsNil)
963 c.Assert(cfg.Branches, HasLen, 1)
964 c.Assert(cfg.Branches["master"].Name, Equals, "master")
965 }
966
967 func (s *RepositorySuite) TestPlainCloneSharedHttpShouldReturnError(c *C) {
968 dir := c.MkDir()
969
970 remote := "http://somerepo"
971
972 _, err := PlainClone(dir, false, &CloneOptions{
973 URL: remote,
974 Shared: true,
975 })
976 c.Assert(err, Equals, ErrAlternatePathNotSupported)
977 }
978
979 func (s *RepositorySuite) TestPlainCloneSharedHttpsShouldReturnError(c *C) {
980 dir := c.MkDir()
981
982 remote := "https://somerepo"
983
984 _, err := PlainClone(dir, false, &CloneOptions{
985 URL: remote,
986 Shared: true,
987 })
988 c.Assert(err, Equals, ErrAlternatePathNotSupported)
989 }
990
991 func (s *RepositorySuite) TestPlainCloneSharedSSHShouldReturnError(c *C) {
992 dir := c.MkDir()
993
994 remote := "ssh://somerepo"
995
996 _, err := PlainClone(dir, false, &CloneOptions{
997 URL: remote,
998 Shared: true,
999 })
1000 c.Assert(err, Equals, ErrAlternatePathNotSupported)
1001 }
1002
1003 func (s *RepositorySuite) TestPlainCloneWithRemoteName(c *C) {
1004 dir := c.MkDir()
1005
1006 r, err := PlainClone(dir, false, &CloneOptions{
1007 URL: s.GetBasicLocalRepositoryURL(),
1008 RemoteName: "test",
1009 })
1010
1011 c.Assert(err, IsNil)
1012
1013 remote, err := r.Remote("test")
1014 c.Assert(err, IsNil)
1015 c.Assert(remote, NotNil)
1016 }
1017
1018 func (s *RepositorySuite) TestPlainCloneOverExistingGitDirectory(c *C) {
1019 dir := c.MkDir()
1020
1021 r, err := PlainInit(dir, false)
1022 c.Assert(r, NotNil)
1023 c.Assert(err, IsNil)
1024
1025 r, err = PlainClone(dir, false, &CloneOptions{
1026 URL: s.GetBasicLocalRepositoryURL(),
1027 })
1028 c.Assert(r, IsNil)
1029 c.Assert(err, Equals, ErrRepositoryAlreadyExists)
1030 }
1031
1032 func (s *RepositorySuite) TestPlainCloneContextCancel(c *C) {
1033 ctx, cancel := context.WithCancel(context.Background())
1034 cancel()
1035
1036 dir := c.MkDir()
1037
1038 r, err := PlainCloneContext(ctx, dir, false, &CloneOptions{
1039 URL: s.GetBasicLocalRepositoryURL(),
1040 })
1041
1042 c.Assert(r, NotNil)
1043 c.Assert(err, Equals, context.Canceled)
1044 }
1045
1046 func (s *RepositorySuite) TestPlainCloneContextNonExistentWithExistentDir(c *C) {
1047 ctx, cancel := context.WithCancel(context.Background())
1048 defer cancel()
1049
1050 fs := s.TemporalFilesystem(c)
1051
1052 dir, err := util.TempDir(fs, "", "")
1053 c.Assert(err, IsNil)
1054
1055 r, err := PlainCloneContext(ctx, dir, false, &CloneOptions{
1056 URL: "incorrectOnPurpose",
1057 })
1058 c.Assert(r, NotNil)
1059 c.Assert(err, Equals, transport.ErrRepositoryNotFound)
1060
1061 _, err = fs.Stat(dir)
1062 c.Assert(os.IsNotExist(err), Equals, false)
1063
1064 names, err := fs.ReadDir(dir)
1065 c.Assert(err, IsNil)
1066 c.Assert(names, HasLen, 0)
1067 }
1068
1069 func (s *RepositorySuite) TestPlainCloneContextNonExistentWithNonExistentDir(c *C) {
1070 ctx, cancel := context.WithCancel(context.Background())
1071 defer cancel()
1072
1073 fs := s.TemporalFilesystem(c)
1074
1075 tmpDir, err := util.TempDir(fs, "", "")
1076 c.Assert(err, IsNil)
1077
1078 repoDir := filepath.Join(tmpDir, "repoDir")
1079
1080 r, err := PlainCloneContext(ctx, repoDir, false, &CloneOptions{
1081 URL: "incorrectOnPurpose",
1082 })
1083 c.Assert(r, NotNil)
1084 c.Assert(err, Equals, transport.ErrRepositoryNotFound)
1085
1086 _, err = fs.Stat(repoDir)
1087 c.Assert(os.IsNotExist(err), Equals, true)
1088 }
1089
1090 func (s *RepositorySuite) TestPlainCloneContextNonExistentWithNotDir(c *C) {
1091 ctx, cancel := context.WithCancel(context.Background())
1092 cancel()
1093
1094 fs := s.TemporalFilesystem(c)
1095
1096 tmpDir, err := util.TempDir(fs, "", "")
1097 c.Assert(err, IsNil)
1098
1099 repoDir := fs.Join(tmpDir, "repoDir")
1100
1101 f, err := fs.Create(repoDir)
1102 c.Assert(err, IsNil)
1103 c.Assert(f.Close(), IsNil)
1104
1105 r, err := PlainCloneContext(ctx, fs.Join(fs.Root(), repoDir), false, &CloneOptions{
1106 URL: "incorrectOnPurpose",
1107 })
1108 c.Assert(r, IsNil)
1109 c.Assert(err, ErrorMatches, ".*not a directory.*")
1110
1111 fi, err := fs.Stat(repoDir)
1112 c.Assert(err, IsNil)
1113 c.Assert(fi.IsDir(), Equals, false)
1114 }
1115
1116 func (s *RepositorySuite) TestPlainCloneContextNonExistentWithNotEmptyDir(c *C) {
1117 ctx, cancel := context.WithCancel(context.Background())
1118 defer cancel()
1119
1120 fs := s.TemporalFilesystem(c)
1121
1122 tmpDir, err := util.TempDir(fs, "", "")
1123 c.Assert(err, IsNil)
1124
1125 repoDir := filepath.Join(tmpDir, "repoDir")
1126 err = fs.MkdirAll(repoDir, 0777)
1127 c.Assert(err, IsNil)
1128
1129 dummyFile := filepath.Join(repoDir, "dummyFile")
1130 err = util.WriteFile(fs, dummyFile, []byte("dummyContent"), 0644)
1131 c.Assert(err, IsNil)
1132
1133 r, err := PlainCloneContext(ctx, fs.Join(fs.Root(), repoDir), false, &CloneOptions{
1134 URL: "incorrectOnPurpose",
1135 })
1136 c.Assert(r, NotNil)
1137 c.Assert(err, Equals, transport.ErrRepositoryNotFound)
1138
1139 _, err = fs.Stat(dummyFile)
1140 c.Assert(err, IsNil)
1141
1142 }
1143
1144 func (s *RepositorySuite) TestPlainCloneContextNonExistingOverExistingGitDirectory(c *C) {
1145 ctx, cancel := context.WithCancel(context.Background())
1146 defer cancel()
1147
1148 dir := c.MkDir()
1149
1150 r, err := PlainInit(dir, false)
1151 c.Assert(r, NotNil)
1152 c.Assert(err, IsNil)
1153
1154 r, err = PlainCloneContext(ctx, dir, false, &CloneOptions{
1155 URL: "incorrectOnPurpose",
1156 })
1157 c.Assert(r, IsNil)
1158 c.Assert(err, Equals, ErrRepositoryAlreadyExists)
1159 }
1160
1161 func (s *RepositorySuite) TestPlainCloneWithRecurseSubmodules(c *C) {
1162 if testing.Short() {
1163 c.Skip("skipping test in short mode.")
1164 }
1165
1166 dir := c.MkDir()
1167
1168 path := fixtures.ByTag("submodule").One().Worktree().Root()
1169 r, err := PlainClone(dir, false, &CloneOptions{
1170 URL: path,
1171 RecurseSubmodules: DefaultSubmoduleRecursionDepth,
1172 })
1173
1174 c.Assert(err, IsNil)
1175
1176 cfg, err := r.Config()
1177 c.Assert(err, IsNil)
1178 c.Assert(cfg.Remotes, HasLen, 1)
1179 c.Assert(cfg.Branches, HasLen, 1)
1180 c.Assert(cfg.Submodules, HasLen, 2)
1181 }
1182
1183 func (s *RepositorySuite) TestPlainCloneWithShallowSubmodules(c *C) {
1184 if testing.Short() {
1185 c.Skip("skipping test in short mode.")
1186 }
1187
1188 dir := c.MkDir()
1189
1190 path := fixtures.ByTag("submodule").One().Worktree().Root()
1191 mainRepo, err := PlainClone(dir, false, &CloneOptions{
1192 URL: path,
1193 RecurseSubmodules: 1,
1194 ShallowSubmodules: true,
1195 })
1196 c.Assert(err, IsNil)
1197
1198 mainWorktree, err := mainRepo.Worktree()
1199 c.Assert(err, IsNil)
1200
1201 submodule, err := mainWorktree.Submodule("basic")
1202 c.Assert(err, IsNil)
1203
1204 subRepo, err := submodule.Repository()
1205 c.Assert(err, IsNil)
1206
1207 lr, err := subRepo.Log(&LogOptions{})
1208 c.Assert(err, IsNil)
1209
1210 commitCount := 0
1211 for _, err := lr.Next(); err == nil; _, err = lr.Next() {
1212 commitCount++
1213 }
1214 c.Assert(err, IsNil)
1215
1216 c.Assert(commitCount, Equals, 1)
1217 }
1218
1219 func (s *RepositorySuite) TestPlainCloneNoCheckout(c *C) {
1220 dir := c.MkDir()
1221
1222 path := fixtures.ByTag("submodule").One().Worktree().Root()
1223 r, err := PlainClone(dir, false, &CloneOptions{
1224 URL: path,
1225 NoCheckout: true,
1226 RecurseSubmodules: DefaultSubmoduleRecursionDepth,
1227 })
1228 c.Assert(err, IsNil)
1229
1230 h, err := r.Head()
1231 c.Assert(err, IsNil)
1232 c.Assert(h.Hash().String(), Equals, "b685400c1f9316f350965a5993d350bc746b0bf4")
1233
1234 fi, err := osfs.New(dir).ReadDir("")
1235 c.Assert(err, IsNil)
1236 c.Assert(fi, HasLen, 1) // .git
1237 }
1238
1239 func (s *RepositorySuite) TestFetch(c *C) {
1240 r, _ := Init(memory.NewStorage(), nil)
1241 _, err := r.CreateRemote(&config.RemoteConfig{
1242 Name: DefaultRemoteName,
1243 URLs: []string{s.GetBasicLocalRepositoryURL()},
1244 })
1245 c.Assert(err, IsNil)
1246 c.Assert(r.Fetch(&FetchOptions{}), IsNil)
1247
1248 remotes, err := r.Remotes()
1249 c.Assert(err, IsNil)
1250 c.Assert(remotes, HasLen, 1)
1251
1252 _, err = r.Head()
1253 c.Assert(err, Equals, plumbing.ErrReferenceNotFound)
1254
1255 branch, err := r.Reference("refs/remotes/origin/master", false)
1256 c.Assert(err, IsNil)
1257 c.Assert(branch, NotNil)
1258 c.Assert(branch.Type(), Equals, plumbing.HashReference)
1259 c.Assert(branch.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
1260 }
1261
1262 func (s *RepositorySuite) TestFetchContext(c *C) {
1263 r, _ := Init(memory.NewStorage(), nil)
1264 _, err := r.CreateRemote(&config.RemoteConfig{
1265 Name: DefaultRemoteName,
1266 URLs: []string{s.GetBasicLocalRepositoryURL()},
1267 })
1268 c.Assert(err, IsNil)
1269
1270 ctx, cancel := context.WithCancel(context.Background())
1271 cancel()
1272
1273 c.Assert(r.FetchContext(ctx, &FetchOptions{}), NotNil)
1274 }
1275
1276 func (s *RepositorySuite) TestCloneWithProgress(c *C) {
1277 fs := memfs.New()
1278
1279 buf := bytes.NewBuffer(nil)
1280 _, err := Clone(memory.NewStorage(), fs, &CloneOptions{
1281 URL: s.GetBasicLocalRepositoryURL(),
1282 Progress: buf,
1283 })
1284
1285 c.Assert(err, IsNil)
1286 c.Assert(buf.Len(), Not(Equals), 0)
1287 }
1288
1289 func (s *RepositorySuite) TestCloneDeep(c *C) {
1290 fs := memfs.New()
1291 r, _ := Init(memory.NewStorage(), fs)
1292
1293 head, err := r.Head()
1294 c.Assert(err, Equals, plumbing.ErrReferenceNotFound)
1295 c.Assert(head, IsNil)
1296
1297 err = r.clone(context.Background(), &CloneOptions{
1298 URL: s.GetBasicLocalRepositoryURL(),
1299 })
1300
1301 c.Assert(err, IsNil)
1302
1303 remotes, err := r.Remotes()
1304 c.Assert(err, IsNil)
1305 c.Assert(remotes, HasLen, 1)
1306
1307 head, err = r.Reference(plumbing.HEAD, false)
1308 c.Assert(err, IsNil)
1309 c.Assert(head, NotNil)
1310 c.Assert(head.Type(), Equals, plumbing.SymbolicReference)
1311 c.Assert(head.Target().String(), Equals, "refs/heads/master")
1312
1313 branch, err := r.Reference(head.Target(), false)
1314 c.Assert(err, IsNil)
1315 c.Assert(branch, NotNil)
1316 c.Assert(branch.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
1317
1318 branch, err = r.Reference("refs/remotes/origin/master", false)
1319 c.Assert(err, IsNil)
1320 c.Assert(branch, NotNil)
1321 c.Assert(branch.Type(), Equals, plumbing.HashReference)
1322 c.Assert(branch.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
1323
1324 fi, err := fs.ReadDir("")
1325 c.Assert(err, IsNil)
1326 c.Assert(fi, HasLen, 8)
1327 }
1328
1329 func (s *RepositorySuite) TestCloneConfig(c *C) {
1330 r, _ := Init(memory.NewStorage(), nil)
1331
1332 head, err := r.Head()
1333 c.Assert(err, Equals, plumbing.ErrReferenceNotFound)
1334 c.Assert(head, IsNil)
1335
1336 err = r.clone(context.Background(), &CloneOptions{
1337 URL: s.GetBasicLocalRepositoryURL(),
1338 })
1339
1340 c.Assert(err, IsNil)
1341
1342 cfg, err := r.Config()
1343 c.Assert(err, IsNil)
1344
1345 c.Assert(cfg.Core.IsBare, Equals, true)
1346 c.Assert(cfg.Remotes, HasLen, 1)
1347 c.Assert(cfg.Remotes["origin"].Name, Equals, "origin")
1348 c.Assert(cfg.Remotes["origin"].URLs, HasLen, 1)
1349 c.Assert(cfg.Branches, HasLen, 1)
1350 c.Assert(cfg.Branches["master"].Name, Equals, "master")
1351 }
1352
1353 func (s *RepositorySuite) TestCloneSingleBranchAndNonHEAD(c *C) {
1354 s.testCloneSingleBranchAndNonHEADReference(c, "refs/heads/branch")
1355 }
1356
1357 func (s *RepositorySuite) TestCloneSingleBranchAndNonHEADAndNonFull(c *C) {
1358 s.testCloneSingleBranchAndNonHEADReference(c, "branch")
1359 }
1360
1361 func (s *RepositorySuite) testCloneSingleBranchAndNonHEADReference(c *C, ref string) {
1362 r, _ := Init(memory.NewStorage(), nil)
1363
1364 head, err := r.Head()
1365 c.Assert(err, Equals, plumbing.ErrReferenceNotFound)
1366 c.Assert(head, IsNil)
1367
1368 err = r.clone(context.Background(), &CloneOptions{
1369 URL: s.GetBasicLocalRepositoryURL(),
1370 ReferenceName: plumbing.ReferenceName(ref),
1371 SingleBranch: true,
1372 })
1373
1374 c.Assert(err, IsNil)
1375
1376 remotes, err := r.Remotes()
1377 c.Assert(err, IsNil)
1378 c.Assert(remotes, HasLen, 1)
1379
1380 cfg, err := r.Config()
1381 c.Assert(err, IsNil)
1382 c.Assert(cfg.Branches, HasLen, 1)
1383 c.Assert(cfg.Branches["branch"].Name, Equals, "branch")
1384 c.Assert(cfg.Branches["branch"].Remote, Equals, "origin")
1385 c.Assert(cfg.Branches["branch"].Merge, Equals, plumbing.ReferenceName("refs/heads/branch"))
1386
1387 head, err = r.Reference(plumbing.HEAD, false)
1388 c.Assert(err, IsNil)
1389 c.Assert(head, NotNil)
1390 c.Assert(head.Type(), Equals, plumbing.SymbolicReference)
1391 c.Assert(head.Target().String(), Equals, "refs/heads/branch")
1392
1393 branch, err := r.Reference(head.Target(), false)
1394 c.Assert(err, IsNil)
1395 c.Assert(branch, NotNil)
1396 c.Assert(branch.Hash().String(), Equals, "e8d3ffab552895c19b9fcf7aa264d277cde33881")
1397
1398 branch, err = r.Reference("refs/remotes/origin/branch", false)
1399 c.Assert(err, IsNil)
1400 c.Assert(branch, NotNil)
1401 c.Assert(branch.Type(), Equals, plumbing.HashReference)
1402 c.Assert(branch.Hash().String(), Equals, "e8d3ffab552895c19b9fcf7aa264d277cde33881")
1403 }
1404
1405 func (s *RepositorySuite) TestCloneSingleBranchHEADMain(c *C) {
1406 r, _ := Init(memory.NewStorage(), nil)
1407
1408 head, err := r.Head()
1409 c.Assert(err, Equals, plumbing.ErrReferenceNotFound)
1410 c.Assert(head, IsNil)
1411
1412 err = r.clone(context.Background(), &CloneOptions{
1413 URL: s.GetLocalRepositoryURL(fixtures.ByTag("no-master-head").One()),
1414 SingleBranch: true,
1415 })
1416
1417 c.Assert(err, IsNil)
1418
1419 remotes, err := r.Remotes()
1420 c.Assert(err, IsNil)
1421 c.Assert(remotes, HasLen, 1)
1422
1423 cfg, err := r.Config()
1424 c.Assert(err, IsNil)
1425 c.Assert(cfg.Branches, HasLen, 1)
1426 c.Assert(cfg.Branches["main"].Name, Equals, "main")
1427 c.Assert(cfg.Branches["main"].Remote, Equals, "origin")
1428 c.Assert(cfg.Branches["main"].Merge, Equals, plumbing.ReferenceName("refs/heads/main"))
1429
1430 head, err = r.Reference(plumbing.HEAD, false)
1431 c.Assert(err, IsNil)
1432 c.Assert(head, NotNil)
1433 c.Assert(head.Type(), Equals, plumbing.SymbolicReference)
1434 c.Assert(head.Target().String(), Equals, "refs/heads/main")
1435
1436 branch, err := r.Reference(head.Target(), false)
1437 c.Assert(err, IsNil)
1438 c.Assert(branch, NotNil)
1439 c.Assert(branch.Hash().String(), Equals, "786dafbd351e587da1ae97e5fb9fbdf868b4a28f")
1440
1441 branch, err = r.Reference("refs/remotes/origin/HEAD", false)
1442 c.Assert(err, IsNil)
1443 c.Assert(branch, NotNil)
1444 c.Assert(branch.Type(), Equals, plumbing.HashReference)
1445 c.Assert(branch.Hash().String(), Equals, "786dafbd351e587da1ae97e5fb9fbdf868b4a28f")
1446 }
1447
1448 func (s *RepositorySuite) TestCloneSingleBranch(c *C) {
1449 r, _ := Init(memory.NewStorage(), nil)
1450
1451 head, err := r.Head()
1452 c.Assert(err, Equals, plumbing.ErrReferenceNotFound)
1453 c.Assert(head, IsNil)
1454
1455 err = r.clone(context.Background(), &CloneOptions{
1456 URL: s.GetBasicLocalRepositoryURL(),
1457 SingleBranch: true,
1458 })
1459
1460 c.Assert(err, IsNil)
1461
1462 remotes, err := r.Remotes()
1463 c.Assert(err, IsNil)
1464 c.Assert(remotes, HasLen, 1)
1465
1466 cfg, err := r.Config()
1467 c.Assert(err, IsNil)
1468 c.Assert(cfg.Branches, HasLen, 1)
1469 c.Assert(cfg.Branches["master"].Name, Equals, "master")
1470 c.Assert(cfg.Branches["master"].Remote, Equals, "origin")
1471 c.Assert(cfg.Branches["master"].Merge, Equals, plumbing.ReferenceName("refs/heads/master"))
1472
1473 head, err = r.Reference(plumbing.HEAD, false)
1474 c.Assert(err, IsNil)
1475 c.Assert(head, NotNil)
1476 c.Assert(head.Type(), Equals, plumbing.SymbolicReference)
1477 c.Assert(head.Target().String(), Equals, "refs/heads/master")
1478
1479 branch, err := r.Reference(head.Target(), false)
1480 c.Assert(err, IsNil)
1481 c.Assert(branch, NotNil)
1482 c.Assert(branch.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
1483 }
1484
1485 func (s *RepositorySuite) TestCloneSingleTag(c *C) {
1486 r, _ := Init(memory.NewStorage(), nil)
1487
1488 url := s.GetLocalRepositoryURL(
1489 fixtures.ByURL("https://github.com/git-fixtures/tags.git").One(),
1490 )
1491
1492 err := r.clone(context.Background(), &CloneOptions{
1493 URL: url,
1494 SingleBranch: true,
1495 ReferenceName: plumbing.ReferenceName("refs/tags/commit-tag"),
1496 })
1497 c.Assert(err, IsNil)
1498
1499 branch, err := r.Reference("refs/tags/commit-tag", false)
1500 c.Assert(err, IsNil)
1501 c.Assert(branch, NotNil)
1502
1503 conf, err := r.Config()
1504 c.Assert(err, IsNil)
1505 originRemote := conf.Remotes["origin"]
1506 c.Assert(originRemote, NotNil)
1507 c.Assert(originRemote.Fetch, HasLen, 1)
1508 c.Assert(originRemote.Fetch[0].String(), Equals, "+refs/tags/commit-tag:refs/tags/commit-tag")
1509 }
1510
1511 func (s *RepositorySuite) TestCloneDetachedHEAD(c *C) {
1512 r, _ := Init(memory.NewStorage(), nil)
1513 err := r.clone(context.Background(), &CloneOptions{
1514 URL: s.GetBasicLocalRepositoryURL(),
1515 ReferenceName: plumbing.ReferenceName("refs/tags/v1.0.0"),
1516 })
1517 c.Assert(err, IsNil)
1518
1519 cfg, err := r.Config()
1520 c.Assert(err, IsNil)
1521 c.Assert(cfg.Branches, HasLen, 0)
1522
1523 head, err := r.Reference(plumbing.HEAD, false)
1524 c.Assert(err, IsNil)
1525 c.Assert(head, NotNil)
1526 c.Assert(head.Type(), Equals, plumbing.HashReference)
1527 c.Assert(head.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
1528
1529 count := 0
1530 objects, err := r.Objects()
1531 c.Assert(err, IsNil)
1532 objects.ForEach(func(object.Object) error { count++; return nil })
1533 c.Assert(count, Equals, 28)
1534 }
1535
1536 func (s *RepositorySuite) TestCloneDetachedHEADAndSingle(c *C) {
1537 r, _ := Init(memory.NewStorage(), nil)
1538 err := r.clone(context.Background(), &CloneOptions{
1539 URL: s.GetBasicLocalRepositoryURL(),
1540 ReferenceName: plumbing.ReferenceName("refs/tags/v1.0.0"),
1541 SingleBranch: true,
1542 })
1543 c.Assert(err, IsNil)
1544
1545 cfg, err := r.Config()
1546 c.Assert(err, IsNil)
1547 c.Assert(cfg.Branches, HasLen, 0)
1548
1549 head, err := r.Reference(plumbing.HEAD, false)
1550 c.Assert(err, IsNil)
1551 c.Assert(head, NotNil)
1552 c.Assert(head.Type(), Equals, plumbing.HashReference)
1553 c.Assert(head.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
1554
1555 count := 0
1556 objects, err := r.Objects()
1557 c.Assert(err, IsNil)
1558 objects.ForEach(func(object.Object) error { count++; return nil })
1559 c.Assert(count, Equals, 28)
1560 }
1561
1562 func (s *RepositorySuite) TestCloneDetachedHEADAndShallow(c *C) {
1563 r, _ := Init(memory.NewStorage(), memfs.New())
1564 err := r.clone(context.Background(), &CloneOptions{
1565 URL: s.GetBasicLocalRepositoryURL(),
1566 ReferenceName: plumbing.ReferenceName("refs/tags/v1.0.0"),
1567 Depth: 1,
1568 })
1569
1570 c.Assert(err, IsNil)
1571
1572 cfg, err := r.Config()
1573 c.Assert(err, IsNil)
1574 c.Assert(cfg.Branches, HasLen, 0)
1575
1576 head, err := r.Reference(plumbing.HEAD, false)
1577 c.Assert(err, IsNil)
1578 c.Assert(head, NotNil)
1579 c.Assert(head.Type(), Equals, plumbing.HashReference)
1580 c.Assert(head.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
1581
1582 count := 0
1583 objects, err := r.Objects()
1584 c.Assert(err, IsNil)
1585 objects.ForEach(func(object.Object) error { count++; return nil })
1586 c.Assert(count, Equals, 15)
1587 }
1588
1589 func (s *RepositorySuite) TestCloneDetachedHEADAnnotatedTag(c *C) {
1590 r, _ := Init(memory.NewStorage(), nil)
1591 err := r.clone(context.Background(), &CloneOptions{
1592 URL: s.GetLocalRepositoryURL(fixtures.ByTag("tags").One()),
1593 ReferenceName: plumbing.ReferenceName("refs/tags/annotated-tag"),
1594 })
1595 c.Assert(err, IsNil)
1596
1597 cfg, err := r.Config()
1598 c.Assert(err, IsNil)
1599 c.Assert(cfg.Branches, HasLen, 0)
1600
1601 head, err := r.Reference(plumbing.HEAD, false)
1602 c.Assert(err, IsNil)
1603 c.Assert(head, NotNil)
1604 c.Assert(head.Type(), Equals, plumbing.HashReference)
1605 c.Assert(head.Hash().String(), Equals, "f7b877701fbf855b44c0a9e86f3fdce2c298b07f")
1606
1607 count := 0
1608 objects, err := r.Objects()
1609 c.Assert(err, IsNil)
1610 objects.ForEach(func(object.Object) error { count++; return nil })
1611 c.Assert(count, Equals, 7)
1612 }
1613
1614 func (s *RepositorySuite) TestPush(c *C) {
1615 url := c.MkDir()
1616
1617 server, err := PlainInit(url, true)
1618 c.Assert(err, IsNil)
1619
1620 _, err = s.Repository.CreateRemote(&config.RemoteConfig{
1621 Name: "test",
1622 URLs: []string{url},
1623 })
1624 c.Assert(err, IsNil)
1625
1626 err = s.Repository.Push(&PushOptions{
1627 RemoteName: "test",
1628 })
1629 c.Assert(err, IsNil)
1630
1631 AssertReferences(c, server, map[string]string{
1632 "refs/heads/master": "6ecf0ef2c2dffb796033e5a02219af86ec6584e5",
1633 "refs/heads/branch": "e8d3ffab552895c19b9fcf7aa264d277cde33881",
1634 })
1635
1636 AssertReferences(c, s.Repository, map[string]string{
1637 "refs/remotes/test/master": "6ecf0ef2c2dffb796033e5a02219af86ec6584e5",
1638 "refs/remotes/test/branch": "e8d3ffab552895c19b9fcf7aa264d277cde33881",
1639 })
1640 }
1641
1642 func (s *RepositorySuite) TestPushContext(c *C) {
1643 url := c.MkDir()
1644
1645 _, err := PlainInit(url, true)
1646 c.Assert(err, IsNil)
1647
1648 _, err = s.Repository.CreateRemote(&config.RemoteConfig{
1649 Name: "foo",
1650 URLs: []string{url},
1651 })
1652 c.Assert(err, IsNil)
1653
1654 ctx, cancel := context.WithCancel(context.Background())
1655 cancel()
1656
1657 err = s.Repository.PushContext(ctx, &PushOptions{
1658 RemoteName: "foo",
1659 })
1660 c.Assert(err, NotNil)
1661 }
1662
1663 // installPreReceiveHook installs a pre-receive hook in the .git
1664 // directory at path which prints message m before exiting
1665 // successfully.
1666 func installPreReceiveHook(c *C, fs billy.Filesystem, path, m string) {
1667 hooks := fs.Join(path, "hooks")
1668 err := fs.MkdirAll(hooks, 0777)
1669 c.Assert(err, IsNil)
1670
1671 err = util.WriteFile(fs, fs.Join(hooks, "pre-receive"), preReceiveHook(m), 0777)
1672 c.Assert(err, IsNil)
1673 }
1674
1675 func (s *RepositorySuite) TestPushWithProgress(c *C) {
1676 fs := s.TemporalFilesystem(c)
1677
1678 path, err := util.TempDir(fs, "", "")
1679 c.Assert(err, IsNil)
1680
1681 url := fs.Join(fs.Root(), path)
1682
1683 server, err := PlainInit(url, true)
1684 c.Assert(err, IsNil)
1685
1686 m := "Receiving..."
1687 installPreReceiveHook(c, fs, path, m)
1688
1689 _, err = s.Repository.CreateRemote(&config.RemoteConfig{
1690 Name: "bar",
1691 URLs: []string{url},
1692 })
1693 c.Assert(err, IsNil)
1694
1695 var p bytes.Buffer
1696 err = s.Repository.Push(&PushOptions{
1697 RemoteName: "bar",
1698 Progress: &p,
1699 })
1700 c.Assert(err, IsNil)
1701
1702 AssertReferences(c, server, map[string]string{
1703 "refs/heads/master": "6ecf0ef2c2dffb796033e5a02219af86ec6584e5",
1704 "refs/heads/branch": "e8d3ffab552895c19b9fcf7aa264d277cde33881",
1705 })
1706
1707 c.Assert((&p).Bytes(), DeepEquals, []byte(m))
1708 }
1709
1710 func (s *RepositorySuite) TestPushDepth(c *C) {
1711 url := c.MkDir()
1712
1713 server, err := PlainClone(url, true, &CloneOptions{
1714 URL: fixtures.Basic().One().DotGit().Root(),
1715 })
1716
1717 c.Assert(err, IsNil)
1718
1719 r, err := Clone(memory.NewStorage(), memfs.New(), &CloneOptions{
1720 URL: url,
1721 Depth: 1,
1722 })
1723 c.Assert(err, IsNil)
1724
1725 err = util.WriteFile(r.wt, "foo", nil, 0755)
1726 c.Assert(err, IsNil)
1727
1728 w, err := r.Worktree()
1729 c.Assert(err, IsNil)
1730
1731 _, err = w.Add("foo")
1732 c.Assert(err, IsNil)
1733
1734 hash, err := w.Commit("foo", &CommitOptions{
1735 Author: defaultSignature(),
1736 Committer: defaultSignature(),
1737 })
1738 c.Assert(err, IsNil)
1739
1740 err = r.Push(&PushOptions{})
1741 c.Assert(err, IsNil)
1742
1743 AssertReferences(c, server, map[string]string{
1744 "refs/heads/master": hash.String(),
1745 })
1746
1747 AssertReferences(c, r, map[string]string{
1748 "refs/remotes/origin/master": hash.String(),
1749 })
1750 }
1751
1752 func (s *RepositorySuite) TestPushNonExistentRemote(c *C) {
1753 srcFs := fixtures.Basic().One().DotGit()
1754 sto := filesystem.NewStorage(srcFs, cache.NewObjectLRUDefault())
1755
1756 r, err := Open(sto, srcFs)
1757 c.Assert(err, IsNil)
1758
1759 err = r.Push(&PushOptions{RemoteName: "myremote"})
1760 c.Assert(err, ErrorMatches, ".*remote not found.*")
1761 }
1762
1763 func (s *RepositorySuite) TestLog(c *C) {
1764 r, _ := Init(memory.NewStorage(), nil)
1765 err := r.clone(context.Background(), &CloneOptions{
1766 URL: s.GetBasicLocalRepositoryURL(),
1767 })
1768
1769 c.Assert(err, IsNil)
1770
1771 cIter, err := r.Log(&LogOptions{
1772 From: plumbing.NewHash("b8e471f58bcbca63b07bda20e428190409c2db47"),
1773 })
1774
1775 c.Assert(err, IsNil)
1776
1777 commitOrder := []plumbing.Hash{
1778 plumbing.NewHash("b8e471f58bcbca63b07bda20e428190409c2db47"),
1779 plumbing.NewHash("b029517f6300c2da0f4b651b8642506cd6aaf45d"),
1780 }
1781
1782 for _, o := range commitOrder {
1783 commit, err := cIter.Next()
1784 c.Assert(err, IsNil)
1785 c.Assert(commit.Hash, Equals, o)
1786 }
1787 _, err = cIter.Next()
1788 c.Assert(err, Equals, io.EOF)
1789 }
1790
1791 func (s *RepositorySuite) TestLogAll(c *C) {
1792 r, _ := Init(memory.NewStorage(), nil)
1793 err := r.clone(context.Background(), &CloneOptions{
1794 URL: s.GetBasicLocalRepositoryURL(),
1795 })
1796 c.Assert(err, IsNil)
1797
1798 rIter, err := r.Storer.IterReferences()
1799 c.Assert(err, IsNil)
1800
1801 refCount := 0
1802 err = rIter.ForEach(func(ref *plumbing.Reference) error {
1803 refCount++
1804 return nil
1805 })
1806 c.Assert(err, IsNil)
1807 c.Assert(refCount, Equals, 5)
1808
1809 cIter, err := r.Log(&LogOptions{
1810 All: true,
1811 })
1812 c.Assert(err, IsNil)
1813
1814 commitOrder := []plumbing.Hash{
1815 plumbing.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5"),
1816 plumbing.NewHash("e8d3ffab552895c19b9fcf7aa264d277cde33881"),
1817 plumbing.NewHash("918c48b83bd081e863dbe1b80f8998f058cd8294"),
1818 plumbing.NewHash("af2d6a6954d532f8ffb47615169c8fdf9d383a1a"),
1819 plumbing.NewHash("1669dce138d9b841a518c64b10914d88f5e488ea"),
1820 plumbing.NewHash("35e85108805c84807bc66a02d91535e1e24b38b9"),
1821 plumbing.NewHash("b029517f6300c2da0f4b651b8642506cd6aaf45d"),
1822 plumbing.NewHash("a5b8b09e2f8fcb0bb99d3ccb0958157b40890d69"),
1823 plumbing.NewHash("b8e471f58bcbca63b07bda20e428190409c2db47"),
1824 }
1825
1826 for _, o := range commitOrder {
1827 commit, err := cIter.Next()
1828 c.Assert(err, IsNil)
1829 c.Assert(commit.Hash, Equals, o)
1830 }
1831 _, err = cIter.Next()
1832 c.Assert(err, Equals, io.EOF)
1833 cIter.Close()
1834 }
1835
1836 func (s *RepositorySuite) TestLogAllMissingReferences(c *C) {
1837 r, _ := Init(memory.NewStorage(), nil)
1838 err := r.clone(context.Background(), &CloneOptions{
1839 URL: s.GetBasicLocalRepositoryURL(),
1840 })
1841 c.Assert(err, IsNil)
1842 err = r.Storer.RemoveReference(plumbing.HEAD)
1843 c.Assert(err, IsNil)
1844
1845 rIter, err := r.Storer.IterReferences()
1846 c.Assert(err, IsNil)
1847
1848 refCount := 0
1849 err = rIter.ForEach(func(ref *plumbing.Reference) error {
1850 refCount++
1851 return nil
1852 })
1853 c.Assert(err, IsNil)
1854 c.Assert(refCount, Equals, 4)
1855
1856 err = r.Storer.SetReference(plumbing.NewHashReference(plumbing.ReferenceName("DUMMY"), plumbing.NewHash("DUMMY")))
1857 c.Assert(err, IsNil)
1858
1859 rIter, err = r.Storer.IterReferences()
1860 c.Assert(err, IsNil)
1861
1862 refCount = 0
1863 err = rIter.ForEach(func(ref *plumbing.Reference) error {
1864 refCount++
1865 return nil
1866 })
1867 c.Assert(err, IsNil)
1868 c.Assert(refCount, Equals, 5)
1869
1870 cIter, err := r.Log(&LogOptions{
1871 All: true,
1872 })
1873 c.Assert(cIter, NotNil)
1874 c.Assert(err, IsNil)
1875
1876 cCount := 0
1877 cIter.ForEach(func(c *object.Commit) error {
1878 cCount++
1879 return nil
1880 })
1881 c.Assert(cCount, Equals, 9)
1882
1883 _, err = cIter.Next()
1884 c.Assert(err, Equals, io.EOF)
1885 cIter.Close()
1886 }
1887
1888 func (s *RepositorySuite) TestLogAllOrderByTime(c *C) {
1889 r, _ := Init(memory.NewStorage(), nil)
1890 err := r.clone(context.Background(), &CloneOptions{
1891 URL: s.GetBasicLocalRepositoryURL(),
1892 })
1893
1894 c.Assert(err, IsNil)
1895
1896 cIter, err := r.Log(&LogOptions{
1897 Order: LogOrderCommitterTime,
1898 All: true,
1899 })
1900 c.Assert(err, IsNil)
1901
1902 commitOrder := []plumbing.Hash{
1903 plumbing.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5"),
1904 plumbing.NewHash("e8d3ffab552895c19b9fcf7aa264d277cde33881"),
1905 plumbing.NewHash("918c48b83bd081e863dbe1b80f8998f058cd8294"),
1906 plumbing.NewHash("af2d6a6954d532f8ffb47615169c8fdf9d383a1a"),
1907 plumbing.NewHash("1669dce138d9b841a518c64b10914d88f5e488ea"),
1908 plumbing.NewHash("a5b8b09e2f8fcb0bb99d3ccb0958157b40890d69"),
1909 plumbing.NewHash("35e85108805c84807bc66a02d91535e1e24b38b9"),
1910 plumbing.NewHash("b8e471f58bcbca63b07bda20e428190409c2db47"),
1911 plumbing.NewHash("b029517f6300c2da0f4b651b8642506cd6aaf45d"),
1912 }
1913
1914 for _, o := range commitOrder {
1915 commit, err := cIter.Next()
1916 c.Assert(err, IsNil)
1917 c.Assert(commit.Hash, Equals, o)
1918 }
1919 _, err = cIter.Next()
1920 c.Assert(err, Equals, io.EOF)
1921 cIter.Close()
1922 }
1923
1924 func (s *RepositorySuite) TestLogHead(c *C) {
1925 r, _ := Init(memory.NewStorage(), nil)
1926 err := r.clone(context.Background(), &CloneOptions{
1927 URL: s.GetBasicLocalRepositoryURL(),
1928 })
1929
1930 c.Assert(err, IsNil)
1931
1932 cIter, err := r.Log(&LogOptions{})
1933
1934 c.Assert(err, IsNil)
1935
1936 commitOrder := []plumbing.Hash{
1937 plumbing.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5"),
1938 plumbing.NewHash("918c48b83bd081e863dbe1b80f8998f058cd8294"),
1939 plumbing.NewHash("af2d6a6954d532f8ffb47615169c8fdf9d383a1a"),
1940 plumbing.NewHash("1669dce138d9b841a518c64b10914d88f5e488ea"),
1941 plumbing.NewHash("35e85108805c84807bc66a02d91535e1e24b38b9"),
1942 plumbing.NewHash("b029517f6300c2da0f4b651b8642506cd6aaf45d"),
1943 plumbing.NewHash("a5b8b09e2f8fcb0bb99d3ccb0958157b40890d69"),
1944 plumbing.NewHash("b8e471f58bcbca63b07bda20e428190409c2db47"),
1945 }
1946
1947 for _, o := range commitOrder {
1948 commit, err := cIter.Next()
1949 c.Assert(err, IsNil)
1950 c.Assert(commit.Hash, Equals, o)
1951 }
1952 _, err = cIter.Next()
1953 c.Assert(err, Equals, io.EOF)
1954 }
1955
1956 func (s *RepositorySuite) TestLogError(c *C) {
1957 r, _ := Init(memory.NewStorage(), nil)
1958 err := r.clone(context.Background(), &CloneOptions{
1959 URL: s.GetBasicLocalRepositoryURL(),
1960 })
1961
1962 c.Assert(err, IsNil)
1963
1964 _, err = r.Log(&LogOptions{
1965 From: plumbing.NewHash("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
1966 })
1967 c.Assert(err, NotNil)
1968 }
1969
1970 func (s *RepositorySuite) TestLogFileNext(c *C) {
1971 r, _ := Init(memory.NewStorage(), nil)
1972 err := r.clone(context.Background(), &CloneOptions{
1973 URL: s.GetBasicLocalRepositoryURL(),
1974 })
1975
1976 c.Assert(err, IsNil)
1977
1978 fileName := "vendor/foo.go"
1979 cIter, err := r.Log(&LogOptions{FileName: &fileName})
1980
1981 c.Assert(err, IsNil)
1982
1983 commitOrder := []plumbing.Hash{
1984 plumbing.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5"),
1985 }
1986
1987 for _, o := range commitOrder {
1988 commit, err := cIter.Next()
1989 c.Assert(err, IsNil)
1990 c.Assert(commit.Hash, Equals, o)
1991 }
1992 _, err = cIter.Next()
1993 c.Assert(err, Equals, io.EOF)
1994 }
1995
1996 func (s *RepositorySuite) TestLogFileForEach(c *C) {
1997 r, _ := Init(memory.NewStorage(), nil)
1998 err := r.clone(context.Background(), &CloneOptions{
1999 URL: s.GetBasicLocalRepositoryURL(),
2000 })
2001
2002 c.Assert(err, IsNil)
2003
2004 fileName := "php/crappy.php"
2005 cIter, err := r.Log(&LogOptions{FileName: &fileName})
2006 c.Assert(err, IsNil)
2007 defer cIter.Close()
2008
2009 commitOrder := []plumbing.Hash{
2010 plumbing.NewHash("918c48b83bd081e863dbe1b80f8998f058cd8294"),
2011 }
2012
2013 expectedIndex := 0
2014 err = cIter.ForEach(func(commit *object.Commit) error {
2015 expectedCommitHash := commitOrder[expectedIndex]
2016 c.Assert(commit.Hash.String(), Equals, expectedCommitHash.String())
2017 expectedIndex++
2018 return nil
2019 })
2020 c.Assert(err, IsNil)
2021 c.Assert(expectedIndex, Equals, 1)
2022 }
2023
2024 func (s *RepositorySuite) TestLogNonHeadFile(c *C) {
2025 r, _ := Init(memory.NewStorage(), nil)
2026 err := r.clone(context.Background(), &CloneOptions{
2027 URL: s.GetBasicLocalRepositoryURL(),
2028 })
2029
2030 c.Assert(err, IsNil)
2031
2032 fileName := "README"
2033 cIter, err := r.Log(&LogOptions{FileName: &fileName})
2034 c.Assert(err, IsNil)
2035 defer cIter.Close()
2036
2037 _, err = cIter.Next()
2038 c.Assert(err, Equals, io.EOF)
2039 }
2040
2041 func (s *RepositorySuite) TestLogAllFileForEach(c *C) {
2042 r, _ := Init(memory.NewStorage(), nil)
2043 err := r.clone(context.Background(), &CloneOptions{
2044 URL: s.GetBasicLocalRepositoryURL(),
2045 })
2046
2047 c.Assert(err, IsNil)
2048
2049 fileName := "README"
2050 cIter, err := r.Log(&LogOptions{FileName: &fileName, All: true})
2051 c.Assert(err, IsNil)
2052 defer cIter.Close()
2053
2054 commitOrder := []plumbing.Hash{
2055 plumbing.NewHash("e8d3ffab552895c19b9fcf7aa264d277cde33881"),
2056 }
2057
2058 expectedIndex := 0
2059 err = cIter.ForEach(func(commit *object.Commit) error {
2060 expectedCommitHash := commitOrder[expectedIndex]
2061 c.Assert(commit.Hash.String(), Equals, expectedCommitHash.String())
2062 expectedIndex++
2063 return nil
2064 })
2065 c.Assert(err, IsNil)
2066 c.Assert(expectedIndex, Equals, 1)
2067 }
2068
2069 func (s *RepositorySuite) TestLogInvalidFile(c *C) {
2070 r, _ := Init(memory.NewStorage(), nil)
2071 err := r.clone(context.Background(), &CloneOptions{
2072 URL: s.GetBasicLocalRepositoryURL(),
2073 })
2074 c.Assert(err, IsNil)
2075
2076 // Throwing in a file that does not exist
2077 fileName := "vendor/foo12.go"
2078 cIter, err := r.Log(&LogOptions{FileName: &fileName})
2079 // Not raising an error since `git log -- vendor/foo12.go` responds silently
2080 c.Assert(err, IsNil)
2081 defer cIter.Close()
2082
2083 _, err = cIter.Next()
2084 c.Assert(err, Equals, io.EOF)
2085 }
2086
2087 func (s *RepositorySuite) TestLogFileInitialCommit(c *C) {
2088 r, _ := Init(memory.NewStorage(), nil)
2089 err := r.clone(context.Background(), &CloneOptions{
2090 URL: s.GetBasicLocalRepositoryURL(),
2091 })
2092 c.Assert(err, IsNil)
2093
2094 fileName := "LICENSE"
2095 cIter, err := r.Log(&LogOptions{
2096 Order: LogOrderCommitterTime,
2097 FileName: &fileName,
2098 })
2099 c.Assert(err, IsNil)
2100 defer cIter.Close()
2101
2102 commitOrder := []plumbing.Hash{
2103 plumbing.NewHash("b029517f6300c2da0f4b651b8642506cd6aaf45d"),
2104 }
2105
2106 expectedIndex := 0
2107 err = cIter.ForEach(func(commit *object.Commit) error {
2108 expectedCommitHash := commitOrder[expectedIndex]
2109 c.Assert(commit.Hash.String(), Equals, expectedCommitHash.String())
2110 expectedIndex++
2111 return nil
2112 })
2113 c.Assert(err, IsNil)
2114 c.Assert(expectedIndex, Equals, 1)
2115 }
2116
2117 func (s *RepositorySuite) TestLogFileWithOtherParamsFail(c *C) {
2118 r, _ := Init(memory.NewStorage(), nil)
2119 err := r.clone(context.Background(), &CloneOptions{
2120 URL: s.GetBasicLocalRepositoryURL(),
2121 })
2122 c.Assert(err, IsNil)
2123
2124 fileName := "vendor/foo.go"
2125 cIter, err := r.Log(&LogOptions{
2126 Order: LogOrderCommitterTime,
2127 FileName: &fileName,
2128 From: plumbing.NewHash("35e85108805c84807bc66a02d91535e1e24b38b9"),
2129 })
2130 c.Assert(err, IsNil)
2131 defer cIter.Close()
2132
2133 _, iterErr := cIter.Next()
2134 c.Assert(iterErr, Equals, io.EOF)
2135 }
2136
2137 func (s *RepositorySuite) TestLogFileWithOtherParamsPass(c *C) {
2138 r, _ := Init(memory.NewStorage(), nil)
2139 err := r.clone(context.Background(), &CloneOptions{
2140 URL: s.GetBasicLocalRepositoryURL(),
2141 })
2142 c.Assert(err, IsNil)
2143
2144 fileName := "LICENSE"
2145 cIter, err := r.Log(&LogOptions{
2146 Order: LogOrderCommitterTime,
2147 FileName: &fileName,
2148 From: plumbing.NewHash("35e85108805c84807bc66a02d91535e1e24b38b9"),
2149 })
2150 c.Assert(err, IsNil)
2151 commitVal, iterErr := cIter.Next()
2152 c.Assert(iterErr, Equals, nil)
2153 c.Assert(commitVal.Hash.String(), Equals, "b029517f6300c2da0f4b651b8642506cd6aaf45d")
2154
2155 _, iterErr = cIter.Next()
2156 c.Assert(iterErr, Equals, io.EOF)
2157 }
2158
2159 type mockErrCommitIter struct{}
2160
2161 func (m *mockErrCommitIter) Next() (*object.Commit, error) {
2162 return nil, errors.New("mock next error")
2163 }
2164 func (m *mockErrCommitIter) ForEach(func(*object.Commit) error) error {
2165 return errors.New("mock foreach error")
2166 }
2167
2168 func (m *mockErrCommitIter) Close() {}
2169
2170 func (s *RepositorySuite) TestLogFileWithError(c *C) {
2171 fileName := "README"
2172 cIter := object.NewCommitFileIterFromIter(fileName, &mockErrCommitIter{}, false)
2173 defer cIter.Close()
2174
2175 err := cIter.ForEach(func(commit *object.Commit) error {
2176 return nil
2177 })
2178 c.Assert(err, NotNil)
2179 }
2180
2181 func (s *RepositorySuite) TestLogPathWithError(c *C) {
2182 fileName := "README"
2183 pathIter := func(path string) bool {
2184 return path == fileName
2185 }
2186 cIter := object.NewCommitPathIterFromIter(pathIter, &mockErrCommitIter{}, false)
2187 defer cIter.Close()
2188
2189 err := cIter.ForEach(func(commit *object.Commit) error {
2190 return nil
2191 })
2192 c.Assert(err, NotNil)
2193 }
2194
2195 func (s *RepositorySuite) TestLogPathRegexpWithError(c *C) {
2196 pathRE := regexp.MustCompile("R.*E")
2197 pathIter := func(path string) bool {
2198 return pathRE.MatchString(path)
2199 }
2200 cIter := object.NewCommitPathIterFromIter(pathIter, &mockErrCommitIter{}, false)
2201 defer cIter.Close()
2202
2203 err := cIter.ForEach(func(commit *object.Commit) error {
2204 return nil
2205 })
2206 c.Assert(err, NotNil)
2207 }
2208
2209 func (s *RepositorySuite) TestLogPathFilterRegexp(c *C) {
2210 pathRE := regexp.MustCompile(`.*\.go`)
2211 pathIter := func(path string) bool {
2212 return pathRE.MatchString(path)
2213 }
2214
2215 r, _ := Init(memory.NewStorage(), nil)
2216 err := r.clone(context.Background(), &CloneOptions{
2217 URL: s.GetBasicLocalRepositoryURL(),
2218 })
2219 c.Assert(err, IsNil)
2220
2221 expectedCommitIDs := []string{
2222 "6ecf0ef2c2dffb796033e5a02219af86ec6584e5",
2223 "918c48b83bd081e863dbe1b80f8998f058cd8294",
2224 }
2225 commitIDs := []string{}
2226
2227 cIter, err := r.Log(&LogOptions{
2228 PathFilter: pathIter,
2229 From: plumbing.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5"),
2230 })
2231 c.Assert(err, IsNil)
2232 defer cIter.Close()
2233
2234 cIter.ForEach(func(commit *object.Commit) error {
2235 commitIDs = append(commitIDs, commit.ID().String())
2236 return nil
2237 })
2238 c.Assert(
2239 strings.Join(commitIDs, ", "),
2240 Equals,
2241 strings.Join(expectedCommitIDs, ", "),
2242 )
2243 }
2244
2245 func (s *RepositorySuite) TestLogLimitNext(c *C) {
2246 r, _ := Init(memory.NewStorage(), nil)
2247 err := r.clone(context.Background(), &CloneOptions{
2248 URL: s.GetBasicLocalRepositoryURL(),
2249 })
2250
2251 c.Assert(err, IsNil)
2252
2253 since := time.Date(2015, 4, 1, 0, 0, 0, 0, time.UTC)
2254 cIter, err := r.Log(&LogOptions{Since: &since})
2255
2256 c.Assert(err, IsNil)
2257
2258 commitOrder := []plumbing.Hash{
2259 plumbing.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5"),
2260 }
2261
2262 for _, o := range commitOrder {
2263 commit, err := cIter.Next()
2264 c.Assert(err, IsNil)
2265 c.Assert(commit.Hash, Equals, o)
2266 }
2267 _, err = cIter.Next()
2268 c.Assert(err, Equals, io.EOF)
2269 }
2270
2271 func (s *RepositorySuite) TestLogLimitForEach(c *C) {
2272 r, _ := Init(memory.NewStorage(), nil)
2273 err := r.clone(context.Background(), &CloneOptions{
2274 URL: s.GetBasicLocalRepositoryURL(),
2275 })
2276
2277 c.Assert(err, IsNil)
2278
2279 since := time.Date(2015, 3, 31, 11, 54, 0, 0, time.UTC)
2280 until := time.Date(2015, 4, 1, 0, 0, 0, 0, time.UTC)
2281 cIter, err := r.Log(&LogOptions{Since: &since, Until: &until})
2282 c.Assert(err, IsNil)
2283 defer cIter.Close()
2284
2285 commitOrder := []plumbing.Hash{
2286 plumbing.NewHash("918c48b83bd081e863dbe1b80f8998f058cd8294"),
2287 }
2288
2289 expectedIndex := 0
2290 err = cIter.ForEach(func(commit *object.Commit) error {
2291 expectedCommitHash := commitOrder[expectedIndex]
2292 c.Assert(commit.Hash.String(), Equals, expectedCommitHash.String())
2293 expectedIndex++
2294 return nil
2295 })
2296 c.Assert(err, IsNil)
2297 c.Assert(expectedIndex, Equals, 1)
2298 }
2299
2300 func (s *RepositorySuite) TestLogAllLimitForEach(c *C) {
2301 r, _ := Init(memory.NewStorage(), nil)
2302 err := r.clone(context.Background(), &CloneOptions{
2303 URL: s.GetBasicLocalRepositoryURL(),
2304 })
2305
2306 c.Assert(err, IsNil)
2307
2308 since := time.Date(2015, 3, 31, 11, 54, 0, 0, time.UTC)
2309 until := time.Date(2015, 4, 1, 0, 0, 0, 0, time.UTC)
2310 cIter, err := r.Log(&LogOptions{Since: &since, Until: &until, All: true})
2311 c.Assert(err, IsNil)
2312 defer cIter.Close()
2313
2314 commitOrder := []plumbing.Hash{
2315 plumbing.NewHash("e8d3ffab552895c19b9fcf7aa264d277cde33881"),
2316 plumbing.NewHash("918c48b83bd081e863dbe1b80f8998f058cd8294"),
2317 }
2318
2319 expectedIndex := 0
2320 err = cIter.ForEach(func(commit *object.Commit) error {
2321 expectedCommitHash := commitOrder[expectedIndex]
2322 c.Assert(commit.Hash.String(), Equals, expectedCommitHash.String())
2323 expectedIndex++
2324 return nil
2325 })
2326 c.Assert(err, IsNil)
2327 c.Assert(expectedIndex, Equals, 2)
2328 }
2329
2330 func (s *RepositorySuite) TestLogLimitWithOtherParamsFail(c *C) {
2331 r, _ := Init(memory.NewStorage(), nil)
2332 err := r.clone(context.Background(), &CloneOptions{
2333 URL: s.GetBasicLocalRepositoryURL(),
2334 })
2335 c.Assert(err, IsNil)
2336
2337 since := time.Date(2015, 3, 31, 11, 54, 0, 0, time.UTC)
2338 cIter, err := r.Log(&LogOptions{
2339 Order: LogOrderCommitterTime,
2340 Since: &since,
2341 From: plumbing.NewHash("35e85108805c84807bc66a02d91535e1e24b38b9"),
2342 })
2343 c.Assert(err, IsNil)
2344 defer cIter.Close()
2345
2346 _, iterErr := cIter.Next()
2347 c.Assert(iterErr, Equals, io.EOF)
2348 }
2349
2350 func (s *RepositorySuite) TestLogLimitWithOtherParamsPass(c *C) {
2351 r, _ := Init(memory.NewStorage(), nil)
2352 err := r.clone(context.Background(), &CloneOptions{
2353 URL: s.GetBasicLocalRepositoryURL(),
2354 })
2355 c.Assert(err, IsNil)
2356
2357 until := time.Date(2015, 3, 31, 11, 43, 0, 0, time.UTC)
2358 cIter, err := r.Log(&LogOptions{
2359 Order: LogOrderCommitterTime,
2360 Until: &until,
2361 From: plumbing.NewHash("35e85108805c84807bc66a02d91535e1e24b38b9"),
2362 })
2363 c.Assert(err, IsNil)
2364 defer cIter.Close()
2365
2366 commitVal, iterErr := cIter.Next()
2367 c.Assert(iterErr, Equals, nil)
2368 c.Assert(commitVal.Hash.String(), Equals, "b029517f6300c2da0f4b651b8642506cd6aaf45d")
2369
2370 _, iterErr = cIter.Next()
2371 c.Assert(iterErr, Equals, io.EOF)
2372 }
2373
2374 func (s *RepositorySuite) TestConfigScoped(c *C) {
2375 r, _ := Init(memory.NewStorage(), nil)
2376 err := r.clone(context.Background(), &CloneOptions{
2377 URL: s.GetBasicLocalRepositoryURL(),
2378 })
2379 c.Assert(err, IsNil)
2380
2381 cfg, err := r.ConfigScoped(config.LocalScope)
2382 c.Assert(err, IsNil)
2383 c.Assert(cfg.User.Email, Equals, "")
2384
2385 cfg, err = r.ConfigScoped(config.SystemScope)
2386 c.Assert(err, IsNil)
2387 c.Assert(cfg.User.Email, Not(Equals), "")
2388 }
2389
2390 func (s *RepositorySuite) TestCommit(c *C) {
2391 r, _ := Init(memory.NewStorage(), nil)
2392 err := r.clone(context.Background(), &CloneOptions{
2393 URL: s.GetBasicLocalRepositoryURL(),
2394 })
2395
2396 c.Assert(err, IsNil)
2397
2398 hash := plumbing.NewHash("b8e471f58bcbca63b07bda20e428190409c2db47")
2399 commit, err := r.CommitObject(hash)
2400 c.Assert(err, IsNil)
2401
2402 c.Assert(commit.Hash.IsZero(), Equals, false)
2403 c.Assert(commit.Hash, Equals, commit.ID())
2404 c.Assert(commit.Hash, Equals, hash)
2405 c.Assert(commit.Type(), Equals, plumbing.CommitObject)
2406
2407 tree, err := commit.Tree()
2408 c.Assert(err, IsNil)
2409 c.Assert(tree.Hash.IsZero(), Equals, false)
2410
2411 c.Assert(commit.Author.Email, Equals, "[email protected]")
2412 }
2413
2414 func (s *RepositorySuite) TestCommits(c *C) {
2415 r, _ := Init(memory.NewStorage(), nil)
2416 err := r.clone(context.Background(), &CloneOptions{URL: s.GetBasicLocalRepositoryURL()})
2417 c.Assert(err, IsNil)
2418
2419 count := 0
2420 commits, err := r.CommitObjects()
2421 c.Assert(err, IsNil)
2422 for {
2423 commit, err := commits.Next()
2424 if err != nil {
2425 break
2426 }
2427
2428 count++
2429 c.Assert(commit.Hash.IsZero(), Equals, false)
2430 c.Assert(commit.Hash, Equals, commit.ID())
2431 c.Assert(commit.Type(), Equals, plumbing.CommitObject)
2432 }
2433
2434 c.Assert(count, Equals, 9)
2435 }
2436
2437 func (s *RepositorySuite) TestBlob(c *C) {
2438 r, _ := Init(memory.NewStorage(), nil)
2439 err := r.clone(context.Background(), &CloneOptions{
2440 URL: s.GetBasicLocalRepositoryURL(),
2441 })
2442
2443 c.Assert(err, IsNil)
2444
2445 blob, err := r.BlobObject(plumbing.NewHash("b8e471f58bcbca63b07bda20e428190409c2db47"))
2446 c.Assert(err, NotNil)
2447 c.Assert(blob, IsNil)
2448
2449 blobHash := plumbing.NewHash("9a48f23120e880dfbe41f7c9b7b708e9ee62a492")
2450 blob, err = r.BlobObject(blobHash)
2451 c.Assert(err, IsNil)
2452
2453 c.Assert(blob.Hash.IsZero(), Equals, false)
2454 c.Assert(blob.Hash, Equals, blob.ID())
2455 c.Assert(blob.Hash, Equals, blobHash)
2456 c.Assert(blob.Type(), Equals, plumbing.BlobObject)
2457 }
2458
2459 func (s *RepositorySuite) TestBlobs(c *C) {
2460 r, _ := Init(memory.NewStorage(), nil)
2461 err := r.clone(context.Background(), &CloneOptions{URL: s.GetBasicLocalRepositoryURL()})
2462 c.Assert(err, IsNil)
2463
2464 count := 0
2465 blobs, err := r.BlobObjects()
2466 c.Assert(err, IsNil)
2467 for {
2468 blob, err := blobs.Next()
2469 if err != nil {
2470 break
2471 }
2472
2473 count++
2474 c.Assert(blob.Hash.IsZero(), Equals, false)
2475 c.Assert(blob.Hash, Equals, blob.ID())
2476 c.Assert(blob.Type(), Equals, plumbing.BlobObject)
2477 }
2478
2479 c.Assert(count, Equals, 10)
2480 }
2481
2482 func (s *RepositorySuite) TestTagObject(c *C) {
2483 url := s.GetLocalRepositoryURL(
2484 fixtures.ByURL("https://github.com/git-fixtures/tags.git").One(),
2485 )
2486
2487 r, _ := Init(memory.NewStorage(), nil)
2488 err := r.clone(context.Background(), &CloneOptions{URL: url})
2489 c.Assert(err, IsNil)
2490
2491 hash := plumbing.NewHash("ad7897c0fb8e7d9a9ba41fa66072cf06095a6cfc")
2492 tag, err := r.TagObject(hash)
2493 c.Assert(err, IsNil)
2494
2495 c.Assert(tag.Hash.IsZero(), Equals, false)
2496 c.Assert(tag.Hash, Equals, hash)
2497 c.Assert(tag.Type(), Equals, plumbing.TagObject)
2498 }
2499
2500 func (s *RepositorySuite) TestTags(c *C) {
2501 url := s.GetLocalRepositoryURL(
2502 fixtures.ByURL("https://github.com/git-fixtures/tags.git").One(),
2503 )
2504
2505 r, _ := Init(memory.NewStorage(), nil)
2506 err := r.clone(context.Background(), &CloneOptions{URL: url})
2507 c.Assert(err, IsNil)
2508
2509 count := 0
2510 tags, err := r.Tags()
2511 c.Assert(err, IsNil)
2512
2513 tags.ForEach(func(tag *plumbing.Reference) error {
2514 count++
2515 c.Assert(tag.Hash().IsZero(), Equals, false)
2516 c.Assert(tag.Name().IsTag(), Equals, true)
2517 return nil
2518 })
2519
2520 c.Assert(count, Equals, 5)
2521 }
2522
2523 func (s *RepositorySuite) TestCreateTagLightweight(c *C) {
2524 url := s.GetLocalRepositoryURL(
2525 fixtures.ByURL("https://github.com/git-fixtures/tags.git").One(),
2526 )
2527
2528 r, _ := Init(memory.NewStorage(), nil)
2529 err := r.clone(context.Background(), &CloneOptions{URL: url})
2530 c.Assert(err, IsNil)
2531
2532 expected, err := r.Head()
2533 c.Assert(err, IsNil)
2534
2535 ref, err := r.CreateTag("foobar", expected.Hash(), nil)
2536 c.Assert(err, IsNil)
2537 c.Assert(ref, NotNil)
2538
2539 actual, err := r.Tag("foobar")
2540 c.Assert(err, IsNil)
2541
2542 c.Assert(expected.Hash(), Equals, actual.Hash())
2543 }
2544
2545 func (s *RepositorySuite) TestCreateTagLightweightExists(c *C) {
2546 url := s.GetLocalRepositoryURL(
2547 fixtures.ByURL("https://github.com/git-fixtures/tags.git").One(),
2548 )
2549
2550 r, _ := Init(memory.NewStorage(), nil)
2551 err := r.clone(context.Background(), &CloneOptions{URL: url})
2552 c.Assert(err, IsNil)
2553
2554 expected, err := r.Head()
2555 c.Assert(err, IsNil)
2556
2557 ref, err := r.CreateTag("lightweight-tag", expected.Hash(), nil)
2558 c.Assert(ref, IsNil)
2559 c.Assert(err, Equals, ErrTagExists)
2560 }
2561
2562 func (s *RepositorySuite) TestCreateTagAnnotated(c *C) {
2563 url := s.GetLocalRepositoryURL(
2564 fixtures.ByURL("https://github.com/git-fixtures/tags.git").One(),
2565 )
2566
2567 r, _ := Init(memory.NewStorage(), nil)
2568 err := r.clone(context.Background(), &CloneOptions{URL: url})
2569 c.Assert(err, IsNil)
2570
2571 h, err := r.Head()
2572 c.Assert(err, IsNil)
2573
2574 expectedHash := h.Hash()
2575
2576 ref, err := r.CreateTag("foobar", expectedHash, &CreateTagOptions{
2577 Tagger: defaultSignature(),
2578 Message: "foo bar baz qux",
2579 })
2580 c.Assert(err, IsNil)
2581
2582 tag, err := r.Tag("foobar")
2583 c.Assert(err, IsNil)
2584
2585 obj, err := r.TagObject(tag.Hash())
2586 c.Assert(err, IsNil)
2587
2588 c.Assert(ref, DeepEquals, tag)
2589 c.Assert(obj.Hash, Equals, ref.Hash())
2590 c.Assert(obj.Type(), Equals, plumbing.TagObject)
2591 c.Assert(obj.Target, Equals, expectedHash)
2592 }
2593
2594 func (s *RepositorySuite) TestCreateTagAnnotatedBadOpts(c *C) {
2595 url := s.GetLocalRepositoryURL(
2596 fixtures.ByURL("https://github.com/git-fixtures/tags.git").One(),
2597 )
2598
2599 r, _ := Init(memory.NewStorage(), nil)
2600 err := r.clone(context.Background(), &CloneOptions{URL: url})
2601 c.Assert(err, IsNil)
2602
2603 h, err := r.Head()
2604 c.Assert(err, IsNil)
2605
2606 expectedHash := h.Hash()
2607
2608 ref, err := r.CreateTag("foobar", expectedHash, &CreateTagOptions{})
2609 c.Assert(ref, IsNil)
2610 c.Assert(err, Equals, ErrMissingMessage)
2611 }
2612
2613 func (s *RepositorySuite) TestCreateTagAnnotatedBadHash(c *C) {
2614 url := s.GetLocalRepositoryURL(
2615 fixtures.ByURL("https://github.com/git-fixtures/tags.git").One(),
2616 )
2617
2618 r, _ := Init(memory.NewStorage(), nil)
2619 err := r.clone(context.Background(), &CloneOptions{URL: url})
2620 c.Assert(err, IsNil)
2621
2622 ref, err := r.CreateTag("foobar", plumbing.ZeroHash, &CreateTagOptions{
2623 Tagger: defaultSignature(),
2624 Message: "foo bar baz qux",
2625 })
2626 c.Assert(ref, IsNil)
2627 c.Assert(err, Equals, plumbing.ErrObjectNotFound)
2628 }
2629
2630 func (s *RepositorySuite) TestCreateTagSigned(c *C) {
2631 url := s.GetLocalRepositoryURL(
2632 fixtures.ByURL("https://github.com/git-fixtures/tags.git").One(),
2633 )
2634
2635 r, _ := Init(memory.NewStorage(), nil)
2636 err := r.clone(context.Background(), &CloneOptions{URL: url})
2637 c.Assert(err, IsNil)
2638
2639 h, err := r.Head()
2640 c.Assert(err, IsNil)
2641
2642 key := commitSignKey(c, true)
2643 _, err = r.CreateTag("foobar", h.Hash(), &CreateTagOptions{
2644 Tagger: defaultSignature(),
2645 Message: "foo bar baz qux",
2646 SignKey: key,
2647 })
2648 c.Assert(err, IsNil)
2649
2650 tag, err := r.Tag("foobar")
2651 c.Assert(err, IsNil)
2652
2653 obj, err := r.TagObject(tag.Hash())
2654 c.Assert(err, IsNil)
2655
2656 // Verify the tag.
2657 pks := new(bytes.Buffer)
2658 pkw, err := armor.Encode(pks, openpgp.PublicKeyType, nil)
2659 c.Assert(err, IsNil)
2660
2661 err = key.Serialize(pkw)
2662 c.Assert(err, IsNil)
2663 err = pkw.Close()
2664 c.Assert(err, IsNil)
2665
2666 actual, err := obj.Verify(pks.String())
2667 c.Assert(err, IsNil)
2668 c.Assert(actual.PrimaryKey, DeepEquals, key.PrimaryKey)
2669 }
2670
2671 func (s *RepositorySuite) TestCreateTagSignedBadKey(c *C) {
2672 url := s.GetLocalRepositoryURL(
2673 fixtures.ByURL("https://github.com/git-fixtures/tags.git").One(),
2674 )
2675
2676 r, _ := Init(memory.NewStorage(), nil)
2677 err := r.clone(context.Background(), &CloneOptions{URL: url})
2678 c.Assert(err, IsNil)
2679
2680 h, err := r.Head()
2681 c.Assert(err, IsNil)
2682
2683 key := commitSignKey(c, false)
2684 _, err = r.CreateTag("foobar", h.Hash(), &CreateTagOptions{
2685 Tagger: defaultSignature(),
2686 Message: "foo bar baz qux",
2687 SignKey: key,
2688 })
2689 c.Assert(err, Equals, openpgperr.InvalidArgumentError("signing key is encrypted"))
2690 }
2691
2692 func (s *RepositorySuite) TestCreateTagCanonicalize(c *C) {
2693 url := s.GetLocalRepositoryURL(
2694 fixtures.ByURL("https://github.com/git-fixtures/tags.git").One(),
2695 )
2696
2697 r, _ := Init(memory.NewStorage(), nil)
2698 err := r.clone(context.Background(), &CloneOptions{URL: url})
2699 c.Assert(err, IsNil)
2700
2701 h, err := r.Head()
2702 c.Assert(err, IsNil)
2703
2704 key := commitSignKey(c, true)
2705 _, err = r.CreateTag("foobar", h.Hash(), &CreateTagOptions{
2706 Tagger: defaultSignature(),
2707 Message: "\n\nfoo bar baz qux\n\nsome message here",
2708 SignKey: key,
2709 })
2710 c.Assert(err, IsNil)
2711
2712 tag, err := r.Tag("foobar")
2713 c.Assert(err, IsNil)
2714
2715 obj, err := r.TagObject(tag.Hash())
2716 c.Assert(err, IsNil)
2717
2718 // Assert the new canonicalized message.
2719 c.Assert(obj.Message, Equals, "foo bar baz qux\n\nsome message here\n")
2720
2721 // Verify the tag.
2722 pks := new(bytes.Buffer)
2723 pkw, err := armor.Encode(pks, openpgp.PublicKeyType, nil)
2724 c.Assert(err, IsNil)
2725
2726 err = key.Serialize(pkw)
2727 c.Assert(err, IsNil)
2728 err = pkw.Close()
2729 c.Assert(err, IsNil)
2730
2731 actual, err := obj.Verify(pks.String())
2732 c.Assert(err, IsNil)
2733 c.Assert(actual.PrimaryKey, DeepEquals, key.PrimaryKey)
2734 }
2735
2736 func (s *RepositorySuite) TestTagLightweight(c *C) {
2737 url := s.GetLocalRepositoryURL(
2738 fixtures.ByURL("https://github.com/git-fixtures/tags.git").One(),
2739 )
2740
2741 r, _ := Init(memory.NewStorage(), nil)
2742 err := r.clone(context.Background(), &CloneOptions{URL: url})
2743 c.Assert(err, IsNil)
2744
2745 expected := plumbing.NewHash("f7b877701fbf855b44c0a9e86f3fdce2c298b07f")
2746
2747 tag, err := r.Tag("lightweight-tag")
2748 c.Assert(err, IsNil)
2749
2750 actual := tag.Hash()
2751 c.Assert(expected, Equals, actual)
2752 }
2753
2754 func (s *RepositorySuite) TestTagLightweightMissingTag(c *C) {
2755 url := s.GetLocalRepositoryURL(
2756 fixtures.ByURL("https://github.com/git-fixtures/tags.git").One(),
2757 )
2758
2759 r, _ := Init(memory.NewStorage(), nil)
2760 err := r.clone(context.Background(), &CloneOptions{URL: url})
2761 c.Assert(err, IsNil)
2762
2763 tag, err := r.Tag("lightweight-tag-tag")
2764 c.Assert(tag, IsNil)
2765 c.Assert(err, Equals, ErrTagNotFound)
2766 }
2767
2768 func (s *RepositorySuite) TestDeleteTag(c *C) {
2769 url := s.GetLocalRepositoryURL(
2770 fixtures.ByURL("https://github.com/git-fixtures/tags.git").One(),
2771 )
2772
2773 r, _ := Init(memory.NewStorage(), nil)
2774 err := r.clone(context.Background(), &CloneOptions{URL: url})
2775 c.Assert(err, IsNil)
2776
2777 err = r.DeleteTag("lightweight-tag")
2778 c.Assert(err, IsNil)
2779
2780 _, err = r.Tag("lightweight-tag")
2781 c.Assert(err, Equals, ErrTagNotFound)
2782 }
2783
2784 func (s *RepositorySuite) TestDeleteTagMissingTag(c *C) {
2785 url := s.GetLocalRepositoryURL(
2786 fixtures.ByURL("https://github.com/git-fixtures/tags.git").One(),
2787 )
2788
2789 r, _ := Init(memory.NewStorage(), nil)
2790 err := r.clone(context.Background(), &CloneOptions{URL: url})
2791 c.Assert(err, IsNil)
2792
2793 err = r.DeleteTag("lightweight-tag-tag")
2794 c.Assert(err, Equals, ErrTagNotFound)
2795 }
2796
2797 func (s *RepositorySuite) TestDeleteTagAnnotated(c *C) {
2798 url := s.GetLocalRepositoryURL(
2799 fixtures.ByURL("https://github.com/git-fixtures/tags.git").One(),
2800 )
2801
2802 fs := s.TemporalFilesystem(c)
2803
2804 fss := filesystem.NewStorage(fs, cache.NewObjectLRUDefault())
2805
2806 r, _ := Init(fss, nil)
2807 err := r.clone(context.Background(), &CloneOptions{URL: url})
2808 c.Assert(err, IsNil)
2809
2810 ref, err := r.Tag("annotated-tag")
2811 c.Assert(ref, NotNil)
2812 c.Assert(err, IsNil)
2813
2814 obj, err := r.TagObject(ref.Hash())
2815 c.Assert(obj, NotNil)
2816 c.Assert(err, IsNil)
2817
2818 err = r.DeleteTag("annotated-tag")
2819 c.Assert(err, IsNil)
2820
2821 _, err = r.Tag("annotated-tag")
2822 c.Assert(err, Equals, ErrTagNotFound)
2823
2824 // Run a prune (and repack, to ensure that we are GCing everything regardless
2825 // of the fixture in use) and try to get the tag object again.
2826 //
2827 // The repo needs to be re-opened after the repack.
2828 err = r.Prune(PruneOptions{Handler: r.DeleteObject})
2829 c.Assert(err, IsNil)
2830
2831 err = r.RepackObjects(&RepackConfig{})
2832 c.Assert(err, IsNil)
2833
2834 r, err = PlainOpen(fs.Root())
2835 c.Assert(r, NotNil)
2836 c.Assert(err, IsNil)
2837
2838 // Now check to see if the GC was effective in removing the tag object.
2839 obj, err = r.TagObject(ref.Hash())
2840 c.Assert(obj, IsNil)
2841 c.Assert(err, Equals, plumbing.ErrObjectNotFound)
2842 }
2843
2844 func (s *RepositorySuite) TestDeleteTagAnnotatedUnpacked(c *C) {
2845 url := s.GetLocalRepositoryURL(
2846 fixtures.ByURL("https://github.com/git-fixtures/tags.git").One(),
2847 )
2848
2849 fs := s.TemporalFilesystem(c)
2850
2851 fss := filesystem.NewStorage(fs, cache.NewObjectLRUDefault())
2852
2853 r, _ := Init(fss, nil)
2854 err := r.clone(context.Background(), &CloneOptions{URL: url})
2855 c.Assert(err, IsNil)
2856
2857 // Create a tag for the deletion test. This ensures that the ultimate loose
2858 // object will be unpacked (as we aren't doing anything that should pack it),
2859 // so that we can effectively test that a prune deletes it, without having to
2860 // resort to a repack.
2861 h, err := r.Head()
2862 c.Assert(err, IsNil)
2863
2864 expectedHash := h.Hash()
2865
2866 ref, err := r.CreateTag("foobar", expectedHash, &CreateTagOptions{
2867 Tagger: defaultSignature(),
2868 Message: "foo bar baz qux",
2869 })
2870 c.Assert(err, IsNil)
2871
2872 tag, err := r.Tag("foobar")
2873 c.Assert(err, IsNil)
2874
2875 obj, err := r.TagObject(tag.Hash())
2876 c.Assert(obj, NotNil)
2877 c.Assert(err, IsNil)
2878
2879 err = r.DeleteTag("foobar")
2880 c.Assert(err, IsNil)
2881
2882 _, err = r.Tag("foobar")
2883 c.Assert(err, Equals, ErrTagNotFound)
2884
2885 // As mentioned, only run a prune. We are not testing for packed objects
2886 // here.
2887 err = r.Prune(PruneOptions{Handler: r.DeleteObject})
2888 c.Assert(err, IsNil)
2889
2890 // Now check to see if the GC was effective in removing the tag object.
2891 obj, err = r.TagObject(ref.Hash())
2892 c.Assert(obj, IsNil)
2893 c.Assert(err, Equals, plumbing.ErrObjectNotFound)
2894 }
2895
2896 func (s *RepositorySuite) TestInvalidTagName(c *C) {
2897 r, err := Init(memory.NewStorage(), nil)
2898 c.Assert(err, IsNil)
2899 for i, name := range []string{
2900 "",
2901 "foo bar",
2902 "foo\tbar",
2903 "foo\nbar",
2904 } {
2905 _, err = r.CreateTag(name, plumbing.ZeroHash, nil)
2906 c.Assert(err, NotNil, Commentf("case %d %q", i, name))
2907 }
2908 }
2909
2910 func (s *RepositorySuite) TestBranches(c *C) {
2911 f := fixtures.ByURL("https://github.com/git-fixtures/root-references.git").One()
2912 sto := filesystem.NewStorage(f.DotGit(), cache.NewObjectLRUDefault())
2913 r, err := Open(sto, f.DotGit())
2914 c.Assert(err, IsNil)
2915
2916 count := 0
2917 branches, err := r.Branches()
2918 c.Assert(err, IsNil)
2919
2920 branches.ForEach(func(branch *plumbing.Reference) error {
2921 count++
2922 c.Assert(branch.Hash().IsZero(), Equals, false)
2923 c.Assert(branch.Name().IsBranch(), Equals, true)
2924 return nil
2925 })
2926
2927 c.Assert(count, Equals, 8)
2928 }
2929
2930 func (s *RepositorySuite) TestNotes(c *C) {
2931 // TODO add fixture with Notes
2932 url := s.GetLocalRepositoryURL(
2933 fixtures.ByURL("https://github.com/git-fixtures/tags.git").One(),
2934 )
2935
2936 r, _ := Init(memory.NewStorage(), nil)
2937 err := r.clone(context.Background(), &CloneOptions{URL: url})
2938 c.Assert(err, IsNil)
2939
2940 count := 0
2941 notes, err := r.Notes()
2942 c.Assert(err, IsNil)
2943
2944 notes.ForEach(func(note *plumbing.Reference) error {
2945 count++
2946 c.Assert(note.Hash().IsZero(), Equals, false)
2947 c.Assert(note.Name().IsNote(), Equals, true)
2948 return nil
2949 })
2950
2951 c.Assert(count, Equals, 0)
2952 }
2953
2954 func (s *RepositorySuite) TestTree(c *C) {
2955 r, _ := Init(memory.NewStorage(), nil)
2956 err := r.clone(context.Background(), &CloneOptions{
2957 URL: s.GetBasicLocalRepositoryURL(),
2958 })
2959 c.Assert(err, IsNil)
2960
2961 invalidHash := plumbing.NewHash("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
2962 tree, err := r.TreeObject(invalidHash)
2963 c.Assert(tree, IsNil)
2964 c.Assert(err, NotNil)
2965
2966 hash := plumbing.NewHash("dbd3641b371024f44d0e469a9c8f5457b0660de1")
2967 tree, err = r.TreeObject(hash)
2968 c.Assert(err, IsNil)
2969
2970 c.Assert(tree.Hash.IsZero(), Equals, false)
2971 c.Assert(tree.Hash, Equals, tree.ID())
2972 c.Assert(tree.Hash, Equals, hash)
2973 c.Assert(tree.Type(), Equals, plumbing.TreeObject)
2974 c.Assert(len(tree.Entries), Not(Equals), 0)
2975 }
2976
2977 func (s *RepositorySuite) TestTrees(c *C) {
2978 r, _ := Init(memory.NewStorage(), nil)
2979 err := r.clone(context.Background(), &CloneOptions{URL: s.GetBasicLocalRepositoryURL()})
2980 c.Assert(err, IsNil)
2981
2982 count := 0
2983 trees, err := r.TreeObjects()
2984 c.Assert(err, IsNil)
2985 for {
2986 tree, err := trees.Next()
2987 if err != nil {
2988 break
2989 }
2990
2991 count++
2992 c.Assert(tree.Hash.IsZero(), Equals, false)
2993 c.Assert(tree.Hash, Equals, tree.ID())
2994 c.Assert(tree.Type(), Equals, plumbing.TreeObject)
2995 c.Assert(len(tree.Entries), Not(Equals), 0)
2996 }
2997
2998 c.Assert(count, Equals, 12)
2999 }
3000
3001 func (s *RepositorySuite) TestTagObjects(c *C) {
3002 url := s.GetLocalRepositoryURL(
3003 fixtures.ByURL("https://github.com/git-fixtures/tags.git").One(),
3004 )
3005
3006 r, _ := Init(memory.NewStorage(), nil)
3007 err := r.clone(context.Background(), &CloneOptions{URL: url})
3008 c.Assert(err, IsNil)
3009
3010 count := 0
3011 tags, err := r.TagObjects()
3012 c.Assert(err, IsNil)
3013
3014 tags.ForEach(func(tag *object.Tag) error {
3015 count++
3016
3017 c.Assert(tag.Hash.IsZero(), Equals, false)
3018 c.Assert(tag.Type(), Equals, plumbing.TagObject)
3019 return nil
3020 })
3021
3022 refs, _ := r.References()
3023 refs.ForEach(func(ref *plumbing.Reference) error {
3024 return nil
3025 })
3026
3027 c.Assert(count, Equals, 4)
3028 }
3029
3030 func (s *RepositorySuite) TestCommitIterClosePanic(c *C) {
3031 r, _ := Init(memory.NewStorage(), nil)
3032 err := r.clone(context.Background(), &CloneOptions{URL: s.GetBasicLocalRepositoryURL()})
3033 c.Assert(err, IsNil)
3034
3035 commits, err := r.CommitObjects()
3036 c.Assert(err, IsNil)
3037 commits.Close()
3038 }
3039
3040 func (s *RepositorySuite) TestRef(c *C) {
3041 r, _ := Init(memory.NewStorage(), nil)
3042 err := r.clone(context.Background(), &CloneOptions{URL: s.GetBasicLocalRepositoryURL()})
3043 c.Assert(err, IsNil)
3044
3045 ref, err := r.Reference(plumbing.HEAD, false)
3046 c.Assert(err, IsNil)
3047 c.Assert(ref.Name(), Equals, plumbing.HEAD)
3048
3049 ref, err = r.Reference(plumbing.HEAD, true)
3050 c.Assert(err, IsNil)
3051 c.Assert(ref.Name(), Equals, plumbing.ReferenceName("refs/heads/master"))
3052 }
3053
3054 func (s *RepositorySuite) TestRefs(c *C) {
3055 r, _ := Init(memory.NewStorage(), nil)
3056 err := r.clone(context.Background(), &CloneOptions{URL: s.GetBasicLocalRepositoryURL()})
3057 c.Assert(err, IsNil)
3058
3059 c.Assert(err, IsNil)
3060
3061 iter, err := r.References()
3062 c.Assert(err, IsNil)
3063 c.Assert(iter, NotNil)
3064 }
3065
3066 func (s *RepositorySuite) TestObject(c *C) {
3067 r, _ := Init(memory.NewStorage(), nil)
3068 err := r.clone(context.Background(), &CloneOptions{URL: s.GetBasicLocalRepositoryURL()})
3069 c.Assert(err, IsNil)
3070
3071 hash := plumbing.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
3072 o, err := r.Object(plumbing.CommitObject, hash)
3073 c.Assert(err, IsNil)
3074
3075 c.Assert(o.ID().IsZero(), Equals, false)
3076 c.Assert(o.Type(), Equals, plumbing.CommitObject)
3077 }
3078
3079 func (s *RepositorySuite) TestObjects(c *C) {
3080 r, _ := Init(memory.NewStorage(), nil)
3081 err := r.clone(context.Background(), &CloneOptions{URL: s.GetBasicLocalRepositoryURL()})
3082 c.Assert(err, IsNil)
3083
3084 count := 0
3085 objects, err := r.Objects()
3086 c.Assert(err, IsNil)
3087 for {
3088 o, err := objects.Next()
3089 if err != nil {
3090 break
3091 }
3092
3093 count++
3094 c.Assert(o.ID().IsZero(), Equals, false)
3095 c.Assert(o.Type(), Not(Equals), plumbing.AnyObject)
3096 }
3097
3098 c.Assert(count, Equals, 31)
3099 }
3100
3101 func (s *RepositorySuite) TestObjectNotFound(c *C) {
3102 r, _ := Init(memory.NewStorage(), nil)
3103 err := r.clone(context.Background(), &CloneOptions{URL: s.GetBasicLocalRepositoryURL()})
3104 c.Assert(err, IsNil)
3105
3106 hash := plumbing.NewHash("0a3fb06ff80156fb153bcdcc58b5e16c2d27625c")
3107 tag, err := r.Object(plumbing.TagObject, hash)
3108 c.Assert(err, DeepEquals, plumbing.ErrObjectNotFound)
3109 c.Assert(tag, IsNil)
3110 }
3111
3112 func (s *RepositorySuite) TestWorktree(c *C) {
3113 def := memfs.New()
3114 r, _ := Init(memory.NewStorage(), def)
3115 w, err := r.Worktree()
3116 c.Assert(err, IsNil)
3117 // w.Filesystem is the worktreeFilesystem wrapper around def.
3118 wfs, ok := w.Filesystem.(*worktreeFilesystem)
3119 c.Assert(ok, Equals, true)
3120 c.Assert(wfs.Filesystem, Equals, def)
3121 }
3122
3123 func (s *RepositorySuite) TestWorktreeBare(c *C) {
3124 r, _ := Init(memory.NewStorage(), nil)
3125 w, err := r.Worktree()
3126 c.Assert(err, Equals, ErrIsBareRepository)
3127 c.Assert(w, IsNil)
3128 }
3129
3130 func (s *RepositorySuite) TestResolveRevision(c *C) {
3131 f := fixtures.ByURL("https://github.com/git-fixtures/basic.git").One()
3132 sto := filesystem.NewStorage(f.DotGit(), cache.NewObjectLRUDefault())
3133 r, err := Open(sto, f.DotGit())
3134 c.Assert(err, IsNil)
3135
3136 datas := map[string]string{
3137 "HEAD": "6ecf0ef2c2dffb796033e5a02219af86ec6584e5",
3138 "heads/master": "6ecf0ef2c2dffb796033e5a02219af86ec6584e5",
3139 "heads/master~1": "918c48b83bd081e863dbe1b80f8998f058cd8294",
3140 "refs/heads/master": "6ecf0ef2c2dffb796033e5a02219af86ec6584e5",
3141 "refs/heads/master~2^^~": "b029517f6300c2da0f4b651b8642506cd6aaf45d",
3142 "refs/tags/v1.0.0": "6ecf0ef2c2dffb796033e5a02219af86ec6584e5",
3143 "refs/remotes/origin/master": "6ecf0ef2c2dffb796033e5a02219af86ec6584e5",
3144 "refs/remotes/origin/HEAD": "6ecf0ef2c2dffb796033e5a02219af86ec6584e5",
3145 "HEAD~2^^~": "b029517f6300c2da0f4b651b8642506cd6aaf45d",
3146 "HEAD~3^2": "a5b8b09e2f8fcb0bb99d3ccb0958157b40890d69",
3147 "HEAD~3^2^0": "a5b8b09e2f8fcb0bb99d3ccb0958157b40890d69",
3148 "HEAD~2^{/binary file}": "35e85108805c84807bc66a02d91535e1e24b38b9",
3149 "HEAD~^{/!-some}": "1669dce138d9b841a518c64b10914d88f5e488ea",
3150 "master": "6ecf0ef2c2dffb796033e5a02219af86ec6584e5",
3151 "branch": "e8d3ffab552895c19b9fcf7aa264d277cde33881",
3152 "v1.0.0": "6ecf0ef2c2dffb796033e5a02219af86ec6584e5",
3153 "branch~1": "918c48b83bd081e863dbe1b80f8998f058cd8294",
3154 "v1.0.0~1": "918c48b83bd081e863dbe1b80f8998f058cd8294",
3155 "master~1": "918c48b83bd081e863dbe1b80f8998f058cd8294",
3156 "918c48b83bd081e863dbe1b80f8998f058cd8294": "918c48b83bd081e863dbe1b80f8998f058cd8294",
3157 "918c48b": "918c48b83bd081e863dbe1b80f8998f058cd8294", // odd number of hex digits
3158 }
3159
3160 for rev, hash := range datas {
3161 h, err := r.ResolveRevision(plumbing.Revision(rev))
3162
3163 c.Assert(err, IsNil, Commentf("while checking %s", rev))
3164 c.Check(h.String(), Equals, hash, Commentf("while checking %s", rev))
3165 }
3166 }
3167
3168 func (s *RepositorySuite) TestResolveRevisionAnnotated(c *C) {
3169 f := fixtures.ByURL("https://github.com/git-fixtures/tags.git").One()
3170 sto := filesystem.NewStorage(f.DotGit(), cache.NewObjectLRUDefault())
3171 r, err := Open(sto, f.DotGit())
3172 c.Assert(err, IsNil)
3173
3174 datas := map[string]string{
3175 "refs/tags/annotated-tag": "f7b877701fbf855b44c0a9e86f3fdce2c298b07f",
3176 "b742a2a9fa0afcfa9a6fad080980fbc26b007c69": "f7b877701fbf855b44c0a9e86f3fdce2c298b07f",
3177 }
3178
3179 for rev, hash := range datas {
3180 h, err := r.ResolveRevision(plumbing.Revision(rev))
3181
3182 c.Assert(err, IsNil, Commentf("while checking %s", rev))
3183 c.Check(h.String(), Equals, hash, Commentf("while checking %s", rev))
3184 }
3185 }
3186
3187 func (s *RepositorySuite) TestResolveRevisionWithErrors(c *C) {
3188 url := s.GetLocalRepositoryURL(
3189 fixtures.ByURL("https://github.com/git-fixtures/basic.git").One(),
3190 )
3191
3192 r, _ := Init(memory.NewStorage(), nil)
3193 err := r.clone(context.Background(), &CloneOptions{URL: url})
3194 c.Assert(err, IsNil)
3195
3196 headRef, err := r.Head()
3197 c.Assert(err, IsNil)
3198
3199 ref := plumbing.NewHashReference("refs/heads/918c48b83bd081e863dbe1b80f8998f058cd8294", headRef.Hash())
3200 err = r.Storer.SetReference(ref)
3201 c.Assert(err, IsNil)
3202
3203 datas := map[string]string{
3204 "efs/heads/master~": "reference not found",
3205 "HEAD^3": `Revision invalid : "3" found must be 0, 1 or 2 after "^"`,
3206 "HEAD^{/whatever}": `no commit message match regexp: "whatever"`,
3207 "4e1243bd22c66e76c2ba9eddc1f91394e57f9f83": "reference not found",
3208 }
3209
3210 for rev, rerr := range datas {
3211 _, err := r.ResolveRevision(plumbing.Revision(rev))
3212 c.Assert(err, NotNil)
3213 c.Assert(err.Error(), Equals, rerr)
3214 }
3215 }
3216
3217 func (s *RepositorySuite) testRepackObjects(
3218 c *C, deleteTime time.Time, expectedPacks int) {
3219 srcFs := fixtures.ByTag("unpacked").One().DotGit()
3220 var sto storage.Storer
3221 var err error
3222 sto = filesystem.NewStorage(srcFs, cache.NewObjectLRUDefault())
3223
3224 los := sto.(storer.LooseObjectStorer)
3225 c.Assert(los, NotNil)
3226
3227 numLooseStart := 0
3228 err = los.ForEachObjectHash(func(_ plumbing.Hash) error {
3229 numLooseStart++
3230 return nil
3231 })
3232 c.Assert(err, IsNil)
3233 c.Assert(numLooseStart > 0, Equals, true)
3234
3235 pos := sto.(storer.PackedObjectStorer)
3236 c.Assert(los, NotNil)
3237
3238 packs, err := pos.ObjectPacks()
3239 c.Assert(err, IsNil)
3240 numPacksStart := len(packs)
3241 c.Assert(numPacksStart > 1, Equals, true)
3242
3243 r, err := Open(sto, srcFs)
3244 c.Assert(err, IsNil)
3245 c.Assert(r, NotNil)
3246
3247 err = r.RepackObjects(&RepackConfig{
3248 OnlyDeletePacksOlderThan: deleteTime,
3249 })
3250 c.Assert(err, IsNil)
3251
3252 numLooseEnd := 0
3253 err = los.ForEachObjectHash(func(_ plumbing.Hash) error {
3254 numLooseEnd++
3255 return nil
3256 })
3257 c.Assert(err, IsNil)
3258 c.Assert(numLooseEnd, Equals, 0)
3259
3260 packs, err = pos.ObjectPacks()
3261 c.Assert(err, IsNil)
3262 numPacksEnd := len(packs)
3263 c.Assert(numPacksEnd, Equals, expectedPacks)
3264 }
3265
3266 func (s *RepositorySuite) TestRepackObjects(c *C) {
3267 if testing.Short() {
3268 c.Skip("skipping test in short mode.")
3269 }
3270
3271 s.testRepackObjects(c, time.Time{}, 1)
3272 }
3273
3274 func (s *RepositorySuite) TestRepackObjectsWithNoDelete(c *C) {
3275 if testing.Short() {
3276 c.Skip("skipping test in short mode.")
3277 }
3278
3279 s.testRepackObjects(c, time.Unix(0, 1), 3)
3280 }
3281
3282 func ExecuteOnPath(c *C, path string, cmds ...string) error {
3283 for _, cmd := range cmds {
3284 err := executeOnPath(path, cmd)
3285 c.Assert(err, IsNil)
3286 }
3287
3288 return nil
3289 }
3290
3291 func executeOnPath(path, cmd string) error {
3292 args := strings.Split(cmd, " ")
3293 c := exec.Command(args[0], args[1:]...)
3294 c.Dir = path
3295 c.Env = os.Environ()
3296
3297 buf := bytes.NewBuffer(nil)
3298 c.Stderr = buf
3299 c.Stdout = buf
3300
3301 return c.Run()
3302 }
3303
3304 func (s *RepositorySuite) TestBrokenMultipleShallowFetch(c *C) {
3305 r, _ := Init(memory.NewStorage(), nil)
3306 _, err := r.CreateRemote(&config.RemoteConfig{
3307 Name: DefaultRemoteName,
3308 URLs: []string{s.GetBasicLocalRepositoryURL()},
3309 })
3310 c.Assert(err, IsNil)
3311
3312 c.Assert(r.Fetch(&FetchOptions{
3313 Depth: 2,
3314 RefSpecs: []config.RefSpec{config.RefSpec("refs/heads/master:refs/heads/master")},
3315 }), IsNil)
3316
3317 shallows, err := r.Storer.Shallow()
3318 c.Assert(err, IsNil)
3319 c.Assert(len(shallows), Equals, 1)
3320
3321 ref, err := r.Reference("refs/heads/master", true)
3322 c.Assert(err, IsNil)
3323 cobj, err := r.CommitObject(ref.Hash())
3324 c.Assert(err, IsNil)
3325 c.Assert(cobj, NotNil)
3326 err = object.NewCommitPreorderIter(cobj, nil, nil).ForEach(func(c *object.Commit) error {
3327 for _, ph := range c.ParentHashes {
3328 for _, h := range shallows {
3329 if ph == h {
3330 return storer.ErrStop
3331 }
3332 }
3333 }
3334
3335 return nil
3336 })
3337 c.Assert(err, IsNil)
3338
3339 c.Assert(r.Fetch(&FetchOptions{
3340 Depth: 5,
3341 RefSpecs: []config.RefSpec{config.RefSpec("refs/heads/*:refs/heads/*")},
3342 }), IsNil)
3343
3344 shallows, err = r.Storer.Shallow()
3345 c.Assert(err, IsNil)
3346 c.Assert(len(shallows), Equals, 3)
3347
3348 ref, err = r.Reference("refs/heads/master", true)
3349 c.Assert(err, IsNil)
3350 cobj, err = r.CommitObject(ref.Hash())
3351 c.Assert(err, IsNil)
3352 c.Assert(cobj, NotNil)
3353 err = object.NewCommitPreorderIter(cobj, nil, nil).ForEach(func(c *object.Commit) error {
3354 for _, ph := range c.ParentHashes {
3355 for _, h := range shallows {
3356 if ph == h {
3357 return storer.ErrStop
3358 }
3359 }
3360 }
3361
3362 return nil
3363 })
3364 c.Assert(err, IsNil)
3365 }
3366
3367 func (s *RepositorySuite) TestDotGitToOSFilesystemsInvalidPath(c *C) {
3368 _, _, err := dotGitToOSFilesystems("\000", false)
3369 c.Assert(err, NotNil)
3370 }
3371
3372 func (s *RepositorySuite) TestIssue674(c *C) {
3373 r, _ := Init(memory.NewStorage(), nil)
3374 h, err := r.ResolveRevision(plumbing.Revision(""))
3375
3376 c.Assert(err, NotNil)
3377 c.Assert(h, NotNil)
3378 c.Check(h.IsZero(), Equals, true)
3379 }
3380
3381 func BenchmarkObjects(b *testing.B) {
3382 defer fixtures.Clean()
3383
3384 for _, f := range fixtures.ByTag("packfile") {
3385 if f.DotGitHash == "" {
3386 continue
3387 }
3388
3389 b.Run(f.URL, func(b *testing.B) {
3390 fs := f.DotGit()
3391 st := filesystem.NewStorage(fs, cache.NewObjectLRUDefault())
3392
3393 worktree, err := fs.Chroot(filepath.Dir(fs.Root()))
3394 if err != nil {
3395 b.Fatal(err)
3396 }
3397
3398 repo, err := Open(st, worktree)
3399 if err != nil {
3400 b.Fatal(err)
3401 }
3402
3403 for i := 0; i < b.N; i++ {
3404 iter, err := repo.Objects()
3405 if err != nil {
3406 b.Fatal(err)
3407 }
3408
3409 for {
3410 _, err := iter.Next()
3411 if err == io.EOF {
3412 break
3413 }
3414
3415 if err != nil {
3416 b.Fatal(err)
3417 }
3418 }
3419
3420 iter.Close()
3421 }
3422 })
3423 }
3424 }
3425
3426 func BenchmarkPlainClone(b *testing.B) {
3427 b.StopTimer()
3428 clone := func(b *testing.B) {
3429 _, err := PlainClone(b.TempDir(), true, &CloneOptions{
3430 URL: "https://github.com/go-git/go-git.git",
3431 Depth: 1,
3432 Tags: NoTags,
3433 SingleBranch: true,
3434 })
3435 if err != nil {
3436 b.Error(err)
3437 }
3438 }
3439
3440 // Warm-up as the initial clone could have a higher cost which
3441 // may skew results.
3442 clone(b)
3443
3444 b.StartTimer()
3445 for i := 0; i < b.N; i++ {
3446 clone(b)
3447 }
3448 }