fork of go-git with some jj specific features
1package git
2
3import (
4 "testing"
5
6 "gopkg.in/src-d/go-git.v4/plumbing"
7 "gopkg.in/src-d/go-git.v4/plumbing/cache"
8 "gopkg.in/src-d/go-git.v4/plumbing/format/packfile"
9 "gopkg.in/src-d/go-git.v4/plumbing/transport"
10 "gopkg.in/src-d/go-git.v4/storage/filesystem"
11 "gopkg.in/src-d/go-git.v4/storage/memory"
12
13 . "gopkg.in/check.v1"
14 "gopkg.in/src-d/go-billy.v4"
15 "gopkg.in/src-d/go-billy.v4/memfs"
16 "gopkg.in/src-d/go-billy.v4/util"
17 "gopkg.in/src-d/go-git-fixtures.v3"
18)
19
20func Test(t *testing.T) { TestingT(t) }
21
22type BaseSuite struct {
23 fixtures.Suite
24 Repository *Repository
25
26 backupProtocol transport.Transport
27 cache map[string]*Repository
28}
29
30func (s *BaseSuite) SetUpSuite(c *C) {
31 s.Suite.SetUpSuite(c)
32 s.buildBasicRepository(c)
33
34 s.cache = make(map[string]*Repository)
35}
36
37func (s *BaseSuite) TearDownSuite(c *C) {
38 s.Suite.TearDownSuite(c)
39}
40
41func (s *BaseSuite) buildBasicRepository(c *C) {
42 f := fixtures.Basic().One()
43 s.Repository = s.NewRepository(f)
44}
45
46// NewRepository returns a new repository using the .git folder, if the fixture
47// is tagged as worktree the filesystem from fixture is used, otherwise a new
48// memfs filesystem is used as worktree.
49func (s *BaseSuite) NewRepository(f *fixtures.Fixture) *Repository {
50 var worktree, dotgit billy.Filesystem
51 if f.Is("worktree") {
52 r, err := PlainOpen(f.Worktree().Root())
53 if err != nil {
54 panic(err)
55 }
56
57 return r
58 }
59
60 dotgit = f.DotGit()
61 worktree = memfs.New()
62
63 st := filesystem.NewStorage(dotgit, cache.NewObjectLRUDefault())
64
65 r, err := Open(st, worktree)
66 if err != nil {
67 panic(err)
68 }
69
70 return r
71}
72
73// NewRepositoryWithEmptyWorktree returns a new repository using the .git folder
74// from the fixture but without a empty memfs worktree, the index and the
75// modules are deleted from the .git folder.
76func (s *BaseSuite) NewRepositoryWithEmptyWorktree(f *fixtures.Fixture) *Repository {
77 dotgit := f.DotGit()
78 err := dotgit.Remove("index")
79 if err != nil {
80 panic(err)
81 }
82
83 err = util.RemoveAll(dotgit, "modules")
84 if err != nil {
85 panic(err)
86 }
87
88 worktree := memfs.New()
89
90 st := filesystem.NewStorage(dotgit, cache.NewObjectLRUDefault())
91
92 r, err := Open(st, worktree)
93 if err != nil {
94 panic(err)
95 }
96
97 return r
98
99}
100
101func (s *BaseSuite) NewRepositoryFromPackfile(f *fixtures.Fixture) *Repository {
102 h := f.PackfileHash.String()
103 if r, ok := s.cache[h]; ok {
104 return r
105 }
106
107 storer := memory.NewStorage()
108 p := f.Packfile()
109 defer p.Close()
110
111 if err := packfile.UpdateObjectStorage(storer, p); err != nil {
112 panic(err)
113 }
114
115 storer.SetReference(plumbing.NewHashReference(plumbing.HEAD, f.Head))
116
117 r, err := Open(storer, memfs.New())
118 if err != nil {
119 panic(err)
120 }
121
122 s.cache[h] = r
123 return r
124}
125
126func (s *BaseSuite) GetBasicLocalRepositoryURL() string {
127 fixture := fixtures.Basic().One()
128 return s.GetLocalRepositoryURL(fixture)
129}
130
131func (s *BaseSuite) GetLocalRepositoryURL(f *fixtures.Fixture) string {
132 return f.DotGit().Root()
133}
134
135type SuiteCommon struct{}
136
137var _ = Suite(&SuiteCommon{})
138
139var countLinesTests = [...]struct {
140 i string // the string we want to count lines from
141 e int // the expected number of lines in i
142}{
143 {"", 0},
144 {"a", 1},
145 {"a\n", 1},
146 {"a\nb", 2},
147 {"a\nb\n", 2},
148 {"a\nb\nc", 3},
149 {"a\nb\nc\n", 3},
150 {"a\n\n\nb\n", 4},
151 {"first line\n\tsecond line\nthird line\n", 3},
152}
153
154func (s *SuiteCommon) TestCountLines(c *C) {
155 for i, t := range countLinesTests {
156 o := countLines(t.i)
157 c.Assert(o, Equals, t.e, Commentf("subtest %d, input=%q", i, t.i))
158 }
159}
160
161func AssertReferences(c *C, r *Repository, expected map[string]string) {
162 for name, target := range expected {
163 expected := plumbing.NewReferenceFromStrings(name, target)
164
165 obtained, err := r.Reference(expected.Name(), true)
166 c.Assert(err, IsNil)
167
168 c.Assert(obtained, DeepEquals, expected)
169 }
170}