1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
package dotgit
import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"testing"
"gopkg.in/src-d/go-git.v4/core"
"gopkg.in/src-d/go-git.v4/fixtures"
"gopkg.in/src-d/go-git.v4/utils/fs"
. "gopkg.in/check.v1"
)
func Test(t *testing.T) { TestingT(t) }
type SuiteDotGit struct {
fixtures.Suite
}
var _ = Suite(&SuiteDotGit{})
func (s *SuiteDotGit) TestRefsFromPackedRefs(c *C) {
fs := fixtures.Basic().ByTag(".git").One().DotGit()
dir := New(fs)
refs, err := dir.Refs()
c.Assert(err, IsNil)
ref := findReference(refs, "refs/remotes/origin/branch")
c.Assert(ref, NotNil)
c.Assert(ref.Hash().String(), Equals, "e8d3ffab552895c19b9fcf7aa264d277cde33881")
}
func (s *SuiteDotGit) TestRefsFromReferenceFile(c *C) {
fs := fixtures.Basic().ByTag(".git").One().DotGit()
dir := New(fs)
refs, err := dir.Refs()
c.Assert(err, IsNil)
ref := findReference(refs, "refs/remotes/origin/HEAD")
c.Assert(ref, NotNil)
c.Assert(ref.Type(), Equals, core.SymbolicReference)
c.Assert(string(ref.Target()), Equals, "refs/remotes/origin/master")
}
func (s *SuiteDotGit) TestRefsFromHEADFile(c *C) {
fs := fixtures.Basic().ByTag(".git").One().DotGit()
dir := New(fs)
refs, err := dir.Refs()
c.Assert(err, IsNil)
ref := findReference(refs, "HEAD")
c.Assert(ref, NotNil)
c.Assert(ref.Type(), Equals, core.SymbolicReference)
c.Assert(string(ref.Target()), Equals, "refs/heads/master")
}
func (s *SuiteDotGit) TestConfig(c *C) {
fs := fixtures.Basic().ByTag(".git").One().DotGit()
dir := New(fs)
file, err := dir.Config()
c.Assert(err, IsNil)
c.Assert(filepath.Base(file.Filename()), Equals, "config")
}
func findReference(refs []*core.Reference, name string) *core.Reference {
n := core.ReferenceName(name)
for _, ref := range refs {
if ref.Name() == n {
return ref
}
}
return nil
}
func (s *SuiteDotGit) TestObjectsPack(c *C) {
f := fixtures.Basic().ByTag(".git").One()
fs := f.DotGit()
dir := New(fs)
hashes, err := dir.ObjectPacks()
c.Assert(err, IsNil)
c.Assert(hashes, HasLen, 1)
c.Assert(hashes[0], Equals, f.PackfileHash)
}
func (s *SuiteDotGit) TestObjectPack(c *C) {
f := fixtures.Basic().ByTag(".git").One()
fs := f.DotGit()
dir := New(fs)
pack, err := dir.ObjectPack(f.PackfileHash)
c.Assert(err, IsNil)
c.Assert(filepath.Ext(pack.Filename()), Equals, ".pack")
}
func (s *SuiteDotGit) TestObjectPackIdx(c *C) {
f := fixtures.Basic().ByTag(".git").One()
fs := f.DotGit()
dir := New(fs)
idx, err := dir.ObjectPackIdx(f.PackfileHash)
c.Assert(err, IsNil)
c.Assert(filepath.Ext(idx.Filename()), Equals, ".idx")
}
func (s *SuiteDotGit) TestObjectPackNotFound(c *C) {
fs := fixtures.Basic().ByTag(".git").One().DotGit()
dir := New(fs)
pack, err := dir.ObjectPack(core.ZeroHash)
c.Assert(err, Equals, ErrPackfileNotFound)
c.Assert(pack, IsNil)
idx, err := dir.ObjectPackIdx(core.ZeroHash)
c.Assert(idx, IsNil)
}
func (s *SuiteDotGit) TestObjects(c *C) {
fs := fixtures.ByTag(".git").ByTag("unpacked").One().DotGit()
dir := New(fs)
hashes, err := dir.Objects()
c.Assert(err, IsNil)
c.Assert(hashes, HasLen, 187)
c.Assert(hashes[0].String(), Equals, "0097821d427a3c3385898eb13b50dcbc8702b8a3")
c.Assert(hashes[1].String(), Equals, "01d5fa556c33743006de7e76e67a2dfcd994ca04")
c.Assert(hashes[2].String(), Equals, "03db8e1fbe133a480f2867aac478fd866686d69e")
}
func (s *SuiteDotGit) TestObject(c *C) {
fs := fixtures.ByTag(".git").ByTag("unpacked").One().DotGit()
dir := New(fs)
hash := core.NewHash("03db8e1fbe133a480f2867aac478fd866686d69e")
file, err := dir.Object(hash)
c.Assert(err, IsNil)
c.Assert(strings.HasSuffix(
file.Filename(), "objects/03/db8e1fbe133a480f2867aac478fd866686d69e"),
Equals, true,
)
}
func (s *SuiteDotGit) TestObjectNotFound(c *C) {
fs := fixtures.ByTag(".git").ByTag("unpacked").One().DotGit()
dir := New(fs)
hash := core.NewHash("not-found-object")
file, err := dir.Object(hash)
c.Assert(err, NotNil)
c.Assert(file, IsNil)
}
func (s *SuiteDotGit) TestNewObjectPack(c *C) {
f := fixtures.Basic().One()
dir, err := ioutil.TempDir("", "example")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(dir)
fs := fs.NewOS(dir)
dot := New(fs)
w, err := dot.NewObjectPack()
c.Assert(err, IsNil)
_, err = io.Copy(w, f.Packfile())
c.Assert(err, IsNil)
c.Assert(w.Close(), IsNil)
stat, err := fs.Stat(fmt.Sprintf("objects/pack/pack-%s.pack", f.PackfileHash))
c.Assert(err, IsNil)
c.Assert(stat.Size(), Equals, int64(84794))
stat, err = fs.Stat(fmt.Sprintf("objects/pack/pack-%s.idx", f.PackfileHash))
c.Assert(err, IsNil)
c.Assert(stat.Size(), Equals, int64(1940))
}
|