| 1 |
package dotgit |
| 2 |
|
| 3 |
import ( |
| 4 |
"bufio" |
| •5 |
"bytes" |
| 6 |
"compress/zlib" |
| 7 |
"encoding/hex" |
| 8 |
"errors" |
| 9 |
"io" |
| 10 |
"os" |
| 11 |
"path/filepath" |
| 12 |
"regexp" |
| 13 |
"runtime" |
| 14 |
"strings" |
| 15 |
"testing" |
| 16 |
|
| 17 |
"github.com/go-git/go-billy/v5" |
| 18 |
"github.com/go-git/go-billy/v5/memfs" |
| 19 |
"github.com/go-git/go-billy/v5/osfs" |
| 20 |
"github.com/go-git/go-billy/v5/util" |
| 21 |
fixtures "github.com/go-git/go-git-fixtures/v4" |
| 22 |
"github.com/go-git/go-git/v5/plumbing" |
| 23 |
"github.com/go-git/go-git/v5/storage" |
| 24 |
"github.com/stretchr/testify/assert" |
| 25 |
"github.com/stretchr/testify/require" |
| 26 |
. "gopkg.in/check.v1" |
| 27 |
) |
| 28 |
|
| 29 |
func Test(t *testing.T) { TestingT(t) } |
| 30 |
|
| 31 |
type SuiteDotGit struct { |
| 32 |
fixtures.Suite |
| 33 |
} |
| 34 |
|
| 35 |
var _ = Suite(&SuiteDotGit{}) |
| 36 |
|
| 37 |
func (s *SuiteDotGit) TemporalFilesystem(c *C) (fs billy.Filesystem) { |
| 38 |
fs = osfs.New(c.MkDir()) |
| 39 |
path, err := util.TempDir(fs, "", "") |
| 40 |
if err != nil { |
| 41 |
panic(err) |
| 42 |
} |
| 43 |
|
| 44 |
fs, err = fs.Chroot(path) |
| 45 |
if err != nil { |
| 46 |
panic(err) |
| 47 |
} |
| 48 |
|
| 49 |
return fs |
| 50 |
} |
| 51 |
|
| 52 |
func (s *SuiteDotGit) TestModuleRejectsEscapingNames(c *C) { |
| 53 |
d := New(memfs.New()) |
| 54 |
// Only true path-traversal cases — names like "/etc" or |
| 55 |
// "modules/../escape" land inside modules/ once Join cleans |
| 56 |
// them, so the containment check correctly accepts those. |
| 57 |
bad := []string{ |
| 58 |
"..", |
| 59 |
"../x", |
| 60 |
"foo/../..", |
| 61 |
"a/b/c/../../../..", |
| 62 |
} |
| 63 |
for _, n := range bad { |
| 64 |
_, err := d.Module(n) |
| 65 |
c.Assert(errors.Is(err, ErrModuleNameEscape), Equals, true, |
| 66 |
Commentf("name %q", n)) |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
func (s *SuiteDotGit) TestModuleAcceptsBenignNames(c *C) { |
| 71 |
d := New(memfs.New()) |
| 72 |
for _, n := range []string{"foo", "lib/foo", "x.y"} { |
| 73 |
_, err := d.Module(n) |
| 74 |
c.Assert(err, IsNil, Commentf("name %q", n)) |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
func (s *SuiteDotGit) TestReferenceNameRejectsEscapingNames(c *C) { |
| 79 |
d := New(memfs.New()) |
| 80 |
c.Assert(d.Initialize(), IsNil) |
| 81 |
|
| 82 |
// A ".." component (or a control character) lets a reference name climb |
| 83 |
// out of its sub-tree into unrelated .git metadata such as config; a |
| 84 |
// malicious remote can advertise such a name and, after refspec mapping, |
| 85 |
// have it reach the storage layer. |
| 86 |
bad := []plumbing.ReferenceName{ |
| 87 |
"refs/heads/../../config", |
| 88 |
"refs/remotes/origin/../../../config", |
| 89 |
"refs/heads/..", |
| 90 |
"refs/heads/foo\x00bar", |
| 91 |
} |
| 92 |
for _, n := range bad { |
| 93 |
ref := plumbing.NewHashReference(n, plumbing.NewHash("e8d3ffab552895c19b9fcf7aa264d277cde33881")) |
| 94 |
c.Assert(errors.Is(d.SetRef(ref, nil), ErrReferenceNameEscape), Equals, true, Commentf("SetRef %q", n)) |
| 95 |
|
| 96 |
_, err := d.Ref(n) |
| 97 |
c.Assert(errors.Is(err, ErrReferenceNameEscape), Equals, true, Commentf("Ref %q", n)) |
| 98 |
|
| 99 |
c.Assert(errors.Is(d.RemoveRef(n), ErrReferenceNameEscape), Equals, true, Commentf("RemoveRef %q", n)) |
| 100 |
} |
| 101 |
|
| 102 |
// The rejected traversals must not have written .git/config. |
| 103 |
_, err := d.fs.Stat(configPath) |
| 104 |
c.Assert(err, NotNil, Commentf("traversal must not create .git/config")) |
| 105 |
} |
| 106 |
|
| 107 |
func (s *SuiteDotGit) TestReferenceNameRejectsWindowsDisguisedTraversal(c *C) { |
| 108 |
d := New(memfs.New()) |
| 109 |
c.Assert(d.Initialize(), IsNil) |
| 110 |
|
| 111 |
// On NTFS, ".." followed by trailing periods/spaces or an Alternate Data |
| 112 |
// Stream / $INDEX_ALLOCATION suffix is canonicalised back to "..", so a |
| 113 |
// literal `== ".."` check would miss these while the filesystem still |
| 114 |
// performs the parent-directory hop. These are pure-string checks, so the |
| 115 |
// containment must hold on every host, not only Windows. |
| 116 |
bad := []plumbing.ReferenceName{ |
| 117 |
"refs/heads/..::$INDEX_ALLOCATION", |
| 118 |
"refs/heads/..:$DATA", |
| 119 |
"refs/heads/.. /config", |
| 120 |
"refs/heads/.../config", |
| 121 |
} |
| 122 |
for _, n := range bad { |
| 123 |
ref := plumbing.NewHashReference(n, plumbing.NewHash("e8d3ffab552895c19b9fcf7aa264d277cde33881")) |
| 124 |
c.Assert(errors.Is(d.SetRef(ref, nil), ErrReferenceNameEscape), Equals, true, Commentf("SetRef %q", n)) |
| 125 |
|
| 126 |
_, err := d.Ref(n) |
| 127 |
c.Assert(errors.Is(err, ErrReferenceNameEscape), Equals, true, Commentf("Ref %q", n)) |
| 128 |
} |
| 129 |
|
| 130 |
_, err := d.fs.Stat(configPath) |
| 131 |
c.Assert(err, NotNil, Commentf("traversal must not create .git/config")) |
| 132 |
} |
| 133 |
|
| 134 |
func (s *SuiteDotGit) TestReferenceNameRejectsHFSDisguisedTraversal(c *C) { |
| 135 |
d := New(memfs.New()) |
| 136 |
c.Assert(d.Initialize(), IsNil) |
| 137 |
|
| 138 |
// HFS+ strips a set of ignorable Unicode code points during |
| 139 |
// normalisation, so ".<U+200C>." (zero-width non-joiner) collapses to |
| 140 |
// ".." on disk. As above, this is a string-level check that must hold |
| 141 |
// regardless of host OS. |
| 142 |
n := plumbing.ReferenceName("refs/heads/." + "\u200c" + "./config") |
| 143 |
ref := plumbing.NewHashReference(n, plumbing.NewHash("e8d3ffab552895c19b9fcf7aa264d277cde33881")) |
| 144 |
c.Assert(errors.Is(d.SetRef(ref, nil), ErrReferenceNameEscape), Equals, true, Commentf("SetRef %q", n)) |
| 145 |
|
| 146 |
_, err := d.Ref(n) |
| 147 |
c.Assert(errors.Is(err, ErrReferenceNameEscape), Equals, true, Commentf("Ref %q", n)) |
| 148 |
|
| 149 |
_, err = d.fs.Stat(configPath) |
| 150 |
c.Assert(err, NotNil, Commentf("traversal must not create .git/config")) |
| 151 |
} |
| 152 |
|
| 153 |
func (s *SuiteDotGit) TestReferenceNameRejectsAbsoluteAndDriveNames(c *C) { |
| 154 |
d := New(memfs.New()) |
| 155 |
c.Assert(d.Initialize(), IsNil) |
| 156 |
|
| 157 |
// A leading or trailing separator, or a drive-letter prefix, lets the |
| 158 |
// name collapse onto a top-level .git file (e.g. "/config" -> .git/config) |
| 159 |
// once joined. filepath.VolumeName alone would not catch "C:config" off |
| 160 |
// Windows, so these are rejected host-independently as validSubmoduleName |
| 161 |
// does. |
| 162 |
bad := []plumbing.ReferenceName{ |
| 163 |
"/config", |
| 164 |
"\\config", |
| 165 |
"C:config", |
| 166 |
"refs/heads/foo/", |
| 167 |
"", |
| 168 |
} |
| 169 |
for _, n := range bad { |
| 170 |
ref := plumbing.NewHashReference(n, plumbing.NewHash("e8d3ffab552895c19b9fcf7aa264d277cde33881")) |
| 171 |
c.Assert(errors.Is(d.SetRef(ref, nil), ErrReferenceNameEscape), Equals, true, Commentf("SetRef %q", n)) |
| 172 |
|
| 173 |
_, err := d.Ref(n) |
| 174 |
c.Assert(errors.Is(err, ErrReferenceNameEscape), Equals, true, Commentf("Ref %q", n)) |
| 175 |
c.Assert(errors.Is(d.RemoveRef(n), ErrReferenceNameEscape), Equals, true, Commentf("RemoveRef %q", n)) |
| 176 |
} |
| 177 |
|
| 178 |
_, err := d.fs.Stat(configPath) |
| 179 |
c.Assert(err, NotNil, Commentf("must not create .git/config")) |
| 180 |
} |
| 181 |
|
| 182 |
func (s *SuiteDotGit) TestReferenceNameRejectsTopLevelMetadata(c *C) { |
| 183 |
d := New(memfs.New()) |
| 184 |
c.Assert(d.Initialize(), IsNil) |
| 185 |
|
| 186 |
// A single-level name that is neither under refs/ nor a [A-Z_] pseudo-ref |
| 187 |
// would land on top-level .git metadata once joined; the IsSafe gate |
| 188 |
// rejects it, matching upstream refname_is_safe. |
| 189 |
bad := []plumbing.ReferenceName{ |
| 190 |
"config", "config.worktree", "index", "packed-refs", |
| 191 |
"shallow", "hooks", "objects", "bar", "HEAD2", "head", |
| 192 |
} |
| 193 |
for _, n := range bad { |
| 194 |
ref := plumbing.NewHashReference(n, plumbing.NewHash("e8d3ffab552895c19b9fcf7aa264d277cde33881")) |
| 195 |
c.Assert(errors.Is(d.SetRef(ref, nil), ErrReferenceNameEscape), Equals, true, Commentf("SetRef %q", n)) |
| 196 |
} |
| 197 |
|
| 198 |
_, err := d.fs.Stat(configPath) |
| 199 |
c.Assert(err, NotNil, Commentf("must not create .git/config")) |
| 200 |
} |
| 201 |
|
| 202 |
func (s *SuiteDotGit) TestReferenceNameAcceptsBenignNames(c *C) { |
| 203 |
d := New(memfs.New()) |
| 204 |
c.Assert(d.Initialize(), IsNil) |
| 205 |
for _, n := range []plumbing.ReferenceName{ |
| 206 |
"HEAD", "ORIG_HEAD", "FETCH_HEAD", "MERGE_HEAD", "CHERRY_PICK_HEAD", |
| 207 |
"refs/heads/main", "refs/heads/release-1.2", |
| 208 |
"refs/tags/v1.0.0", "refs/remotes/origin/HEAD", "refs/stash", |
| 209 |
} { |
| 210 |
ref := plumbing.NewHashReference(n, plumbing.NewHash("e8d3ffab552895c19b9fcf7aa264d277cde33881")) |
| 211 |
c.Assert(d.SetRef(ref, nil), IsNil, Commentf("SetRef %q", n)) |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
func (s *SuiteDotGit) TestInitialize(c *C) { |
| 216 |
fs := s.TemporalFilesystem(c) |
| 217 |
|
| 218 |
dir := New(fs) |
| 219 |
|
| 220 |
err := dir.Initialize() |
| 221 |
c.Assert(err, IsNil) |
| 222 |
|
| 223 |
_, err = fs.Stat(fs.Join("objects", "info")) |
| 224 |
c.Assert(err, IsNil) |
| 225 |
|
| 226 |
_, err = fs.Stat(fs.Join("objects", "pack")) |
| 227 |
c.Assert(err, IsNil) |
| 228 |
|
| 229 |
_, err = fs.Stat(fs.Join("refs", "heads")) |
| 230 |
c.Assert(err, IsNil) |
| 231 |
|
| 232 |
_, err = fs.Stat(fs.Join("refs", "tags")) |
| 233 |
c.Assert(err, IsNil) |
| 234 |
} |
| 235 |
|
| 236 |
func (s *SuiteDotGit) TestSetRefs(c *C) { |
| 237 |
fs := s.TemporalFilesystem(c) |
| 238 |
|
| 239 |
dir := New(fs) |
| 240 |
|
| 241 |
testSetRefs(c, dir) |
| 242 |
} |
| 243 |
|
| 244 |
func (s *SuiteDotGit) TestSetRefsNorwfs(c *C) { |
| 245 |
fs := s.TemporalFilesystem(c) |
| 246 |
|
| 247 |
dir := New(&norwfs{fs}) |
| 248 |
|
| 249 |
testSetRefs(c, dir) |
| 250 |
} |
| 251 |
|
| 252 |
func (s *SuiteDotGit) TestRefsHeadFirst(c *C) { |
| 253 |
fs := fixtures.Basic().ByTag(".git").One().DotGit() |
| 254 |
dir := New(fs) |
| 255 |
refs, err := dir.Refs() |
| 256 |
c.Assert(err, IsNil) |
| 257 |
c.Assert(len(refs), Not(Equals), 0) |
| 258 |
c.Assert(refs[0].Name().String(), Equals, "HEAD") |
| 259 |
} |
| 260 |
|
| 261 |
func testSetRefs(c *C, dir *DotGit) { |
| 262 |
firstFoo := plumbing.NewReferenceFromStrings( |
| 263 |
"refs/heads/foo", |
| 264 |
"e8d3ffab552895c19b9fcf7aa264d277cde33881", |
| 265 |
) |
| 266 |
err := dir.SetRef(firstFoo, nil) |
| 267 |
|
| 268 |
c.Assert(err, IsNil) |
| 269 |
|
| 270 |
err = dir.SetRef(plumbing.NewReferenceFromStrings( |
| 271 |
"refs/heads/symbolic", |
| 272 |
"ref: refs/heads/foo", |
| 273 |
), nil) |
| 274 |
|
| 275 |
c.Assert(err, IsNil) |
| 276 |
|
| 277 |
// A single-level, non-pseudo-ref name is refused: it is not under refs/ |
| 278 |
// and its spelling is not the [A-Z_] pseudo-ref form (upstream |
| 279 |
// refname_is_safe rejects it likewise). |
| 280 |
err = dir.SetRef(plumbing.NewReferenceFromStrings( |
| 281 |
"bar", |
| 282 |
"e8d3ffab552895c19b9fcf7aa264d277cde33881", |
| 283 |
), nil) |
| 284 |
c.Assert(errors.Is(err, ErrReferenceNameEscape), Equals, true) |
| 285 |
|
| 286 |
err = dir.SetRef(plumbing.NewReferenceFromStrings( |
| 287 |
"refs/heads/feature/baz", |
| 288 |
"e8d3ffab552895c19b9fcf7aa264d277cde33881", |
| 289 |
), nil) |
| 290 |
c.Assert(err, IsNil) |
| 291 |
|
| 292 |
refs, err := dir.Refs() |
| 293 |
c.Assert(err, IsNil) |
| 294 |
c.Assert(refs, HasLen, 3) |
| 295 |
|
| 296 |
ref := findReference(refs, "refs/heads/foo") |
| 297 |
c.Assert(ref, NotNil) |
| 298 |
c.Assert(ref.Hash().String(), Equals, "e8d3ffab552895c19b9fcf7aa264d277cde33881") |
| 299 |
|
| 300 |
ref = findReference(refs, "refs/heads/symbolic") |
| 301 |
c.Assert(ref, NotNil) |
| 302 |
c.Assert(ref.Target().String(), Equals, "refs/heads/foo") |
| 303 |
|
| 304 |
ref = findReference(refs, "bar") |
| 305 |
c.Assert(ref, IsNil) |
| 306 |
|
| 307 |
_, err = dir.readReferenceFile(".", "refs/heads/feature/baz") |
| 308 |
c.Assert(err, IsNil) |
| 309 |
|
| 310 |
_, err = dir.readReferenceFile(".", "refs/heads/feature") |
| 311 |
c.Assert(err, Equals, ErrIsDir) |
| 312 |
|
| 313 |
ref, err = dir.Ref("refs/heads/foo") |
| 314 |
c.Assert(err, IsNil) |
| 315 |
c.Assert(ref, NotNil) |
| 316 |
c.Assert(ref.Hash().String(), Equals, "e8d3ffab552895c19b9fcf7aa264d277cde33881") |
| 317 |
|
| 318 |
ref, err = dir.Ref("refs/heads/symbolic") |
| 319 |
c.Assert(err, IsNil) |
| 320 |
c.Assert(ref, NotNil) |
| 321 |
c.Assert(ref.Target().String(), Equals, "refs/heads/foo") |
| 322 |
|
| 323 |
// "bar" was refused at write time and is refused at read time too. |
| 324 |
_, err = dir.Ref("bar") |
| 325 |
c.Assert(errors.Is(err, ErrReferenceNameEscape), Equals, true) |
| 326 |
|
| 327 |
// Check that SetRef with a non-nil `old` works. |
| 328 |
err = dir.SetRef(plumbing.NewReferenceFromStrings( |
| 329 |
"refs/heads/foo", |
| 330 |
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5", |
| 331 |
), firstFoo) |
| 332 |
c.Assert(err, IsNil) |
| 333 |
|
| 334 |
// `firstFoo` is no longer the right `old` reference, so this |
| 335 |
// should fail. |
| 336 |
err = dir.SetRef(plumbing.NewReferenceFromStrings( |
| 337 |
"refs/heads/foo", |
| 338 |
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5", |
| 339 |
), firstFoo) |
| 340 |
c.Assert(err, NotNil) |
| 341 |
} |
| 342 |
|
| 343 |
func (s *SuiteDotGit) TestRefsFromPackedRefs(c *C) { |
| 344 |
fs := fixtures.Basic().ByTag(".git").One().DotGit() |
| 345 |
dir := New(fs) |
| 346 |
|
| 347 |
refs, err := dir.Refs() |
| 348 |
c.Assert(err, IsNil) |
| 349 |
|
| 350 |
ref := findReference(refs, "refs/remotes/origin/branch") |
| 351 |
c.Assert(ref, NotNil) |
| 352 |
c.Assert(ref.Hash().String(), Equals, "e8d3ffab552895c19b9fcf7aa264d277cde33881") |
| 353 |
} |
| 354 |
|
| 355 |
func (s *SuiteDotGit) TestRefsFromReferenceFile(c *C) { |
| 356 |
fs := fixtures.Basic().ByTag(".git").One().DotGit() |
| 357 |
dir := New(fs) |
| 358 |
|
| 359 |
refs, err := dir.Refs() |
| 360 |
c.Assert(err, IsNil) |
| 361 |
|
| 362 |
ref := findReference(refs, "refs/remotes/origin/HEAD") |
| 363 |
c.Assert(ref, NotNil) |
| 364 |
c.Assert(ref.Type(), Equals, plumbing.SymbolicReference) |
| 365 |
c.Assert(string(ref.Target()), Equals, "refs/remotes/origin/master") |
| 366 |
} |
| 367 |
|
| 368 |
func BenchmarkRefMultipleTimes(b *testing.B) { |
| 369 |
fs := fixtures.Basic().ByTag(".git").One().DotGit() |
| 370 |
refname := plumbing.ReferenceName("refs/remotes/origin/branch") |
| 371 |
|
| 372 |
dir := New(fs) |
| 373 |
_, err := dir.Ref(refname) |
| 374 |
if err != nil { |
| 375 |
b.Fatalf("unexpected error: %s", err) |
| 376 |
} |
| 377 |
|
| 378 |
for i := 0; i < b.N; i++ { |
| 379 |
_, err := dir.Ref(refname) |
| 380 |
if err != nil { |
| 381 |
b.Fatalf("unexpected error: %s", err) |
| 382 |
} |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
func (s *SuiteDotGit) TestRemoveRefFromReferenceFile(c *C) { |
| 387 |
fs := fixtures.Basic().ByTag(".git").One().DotGit() |
| 388 |
dir := New(fs) |
| 389 |
|
| 390 |
name := plumbing.ReferenceName("refs/remotes/origin/HEAD") |
| 391 |
err := dir.RemoveRef(name) |
| 392 |
c.Assert(err, IsNil) |
| 393 |
|
| 394 |
refs, err := dir.Refs() |
| 395 |
c.Assert(err, IsNil) |
| 396 |
|
| 397 |
ref := findReference(refs, string(name)) |
| 398 |
c.Assert(ref, IsNil) |
| 399 |
} |
| 400 |
|
| 401 |
func (s *SuiteDotGit) TestRemoveRefFromPackedRefs(c *C) { |
| 402 |
fs := fixtures.Basic().ByTag(".git").One().DotGit() |
| 403 |
dir := New(fs) |
| 404 |
|
| 405 |
name := plumbing.ReferenceName("refs/remotes/origin/master") |
| 406 |
err := dir.RemoveRef(name) |
| 407 |
c.Assert(err, IsNil) |
| 408 |
|
| 409 |
b, err := util.ReadFile(fs, packedRefsPath) |
| 410 |
c.Assert(err, IsNil) |
| 411 |
|
| 412 |
c.Assert(string(b), Equals, ""+ |
| 413 |
"# pack-refs with: peeled fully-peeled \n"+ |
| 414 |
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5 refs/heads/master\n"+ |
| 415 |
"e8d3ffab552895c19b9fcf7aa264d277cde33881 refs/remotes/origin/branch\n") |
| 416 |
} |
| 417 |
|
| 418 |
func (s *SuiteDotGit) TestRemoveRefFromReferenceFileAndPackedRefs(c *C) { |
| 419 |
fs := fixtures.Basic().ByTag(".git").One().DotGit() |
| 420 |
dir := New(fs) |
| 421 |
|
| 422 |
// Make a ref file for a ref that's already in `packed-refs`. |
| 423 |
err := dir.SetRef(plumbing.NewReferenceFromStrings( |
| 424 |
"refs/remotes/origin/branch", |
| 425 |
"e8d3ffab552895c19b9fcf7aa264d277cde33881", |
| 426 |
), nil) |
| 427 |
c.Assert(err, IsNil) |
| 428 |
|
| 429 |
// Make sure it only appears once in the refs list. |
| 430 |
refs, err := dir.Refs() |
| 431 |
c.Assert(err, IsNil) |
| 432 |
found := false |
| 433 |
for _, ref := range refs { |
| 434 |
if ref.Name() == "refs/remotes/origin/branch" { |
| 435 |
c.Assert(found, Equals, false) |
| 436 |
found = true |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
name := plumbing.ReferenceName("refs/remotes/origin/branch") |
| 441 |
err = dir.RemoveRef(name) |
| 442 |
c.Assert(err, IsNil) |
| 443 |
|
| 444 |
b, err := util.ReadFile(fs, packedRefsPath) |
| 445 |
c.Assert(err, IsNil) |
| 446 |
|
| 447 |
c.Assert(string(b), Equals, ""+ |
| 448 |
"# pack-refs with: peeled fully-peeled \n"+ |
| 449 |
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5 refs/heads/master\n"+ |
| 450 |
"6ecf0ef2c2dffb796033e5a02219af86ec6584e5 refs/remotes/origin/master\n") |
| 451 |
|
| 452 |
refs, err = dir.Refs() |
| 453 |
c.Assert(err, IsNil) |
| 454 |
|
| 455 |
ref := findReference(refs, string(name)) |
| 456 |
c.Assert(ref, IsNil) |
| 457 |
} |
| 458 |
|
| 459 |
func (s *SuiteDotGit) TestRemoveRefNonExistent(c *C) { |
| 460 |
fs := fixtures.Basic().ByTag(".git").One().DotGit() |
| 461 |
dir := New(fs) |
| 462 |
|
| 463 |
before, err := util.ReadFile(fs, packedRefsPath) |
| 464 |
c.Assert(err, IsNil) |
| 465 |
|
| 466 |
name := plumbing.ReferenceName("refs/heads/nonexistent") |
| 467 |
err = dir.RemoveRef(name) |
| 468 |
c.Assert(err, IsNil) |
| 469 |
|
| 470 |
after, err := util.ReadFile(fs, packedRefsPath) |
| 471 |
c.Assert(err, IsNil) |
| 472 |
|
| 473 |
c.Assert(string(before), Equals, string(after)) |
| 474 |
} |
| 475 |
|
| 476 |
func (s *SuiteDotGit) TestRemoveRefInvalidPackedRefs(c *C) { |
| 477 |
fs := fixtures.Basic().ByTag(".git").One().DotGit() |
| 478 |
dir := New(fs) |
| 479 |
|
| 480 |
brokenContent := "BROKEN STUFF REALLY BROKEN" |
| 481 |
|
| 482 |
err := util.WriteFile(fs, packedRefsPath, []byte(brokenContent), os.FileMode(0755)) |
| 483 |
c.Assert(err, IsNil) |
| 484 |
|
| 485 |
name := plumbing.ReferenceName("refs/heads/nonexistent") |
| 486 |
err = dir.RemoveRef(name) |
| 487 |
c.Assert(err, NotNil) |
| 488 |
|
| 489 |
after, err := util.ReadFile(fs, packedRefsPath) |
| 490 |
c.Assert(err, IsNil) |
| 491 |
|
| 492 |
c.Assert(brokenContent, Equals, string(after)) |
| 493 |
} |
| 494 |
|
| 495 |
func (s *SuiteDotGit) TestRemoveRefInvalidPackedRefs2(c *C) { |
| 496 |
fs := fixtures.Basic().ByTag(".git").One().DotGit() |
| 497 |
dir := New(fs) |
| 498 |
|
| 499 |
brokenContent := strings.Repeat("a", bufio.MaxScanTokenSize*2) |
| 500 |
|
| 501 |
err := util.WriteFile(fs, packedRefsPath, []byte(brokenContent), os.FileMode(0755)) |
| 502 |
c.Assert(err, IsNil) |
| 503 |
|
| 504 |
name := plumbing.ReferenceName("refs/heads/nonexistent") |
| 505 |
err = dir.RemoveRef(name) |
| 506 |
c.Assert(err, NotNil) |
| 507 |
|
| 508 |
after, err := util.ReadFile(fs, packedRefsPath) |
| 509 |
c.Assert(err, IsNil) |
| 510 |
|
| 511 |
c.Assert(brokenContent, Equals, string(after)) |
| 512 |
} |
| 513 |
|
| 514 |
func (s *SuiteDotGit) TestRefsFromHEADFile(c *C) { |
| 515 |
fs := fixtures.Basic().ByTag(".git").One().DotGit() |
| 516 |
dir := New(fs) |
| 517 |
|
| 518 |
refs, err := dir.Refs() |
| 519 |
c.Assert(err, IsNil) |
| 520 |
|
| 521 |
ref := findReference(refs, "HEAD") |
| 522 |
c.Assert(ref, NotNil) |
| 523 |
c.Assert(ref.Type(), Equals, plumbing.SymbolicReference) |
| 524 |
c.Assert(string(ref.Target()), Equals, "refs/heads/master") |
| 525 |
} |
| 526 |
|
| 527 |
func (s *SuiteDotGit) TestConfig(c *C) { |
| 528 |
fs := fixtures.Basic().ByTag(".git").One().DotGit() |
| 529 |
dir := New(fs) |
| 530 |
|
| 531 |
file, err := dir.Config() |
| 532 |
c.Assert(err, IsNil) |
| 533 |
c.Assert(filepath.Base(file.Name()), Equals, "config") |
| 534 |
} |
| 535 |
|
| 536 |
func (s *SuiteDotGit) TestConfigWriteAndConfig(c *C) { |
| 537 |
fs := s.TemporalFilesystem(c) |
| 538 |
|
| 539 |
dir := New(fs) |
| 540 |
|
| 541 |
f, err := dir.ConfigWriter() |
| 542 |
c.Assert(err, IsNil) |
| 543 |
|
| 544 |
_, err = f.Write([]byte("foo")) |
| 545 |
c.Assert(err, IsNil) |
| 546 |
|
| 547 |
f, err = dir.Config() |
| 548 |
c.Assert(err, IsNil) |
| 549 |
|
| 550 |
cnt, err := io.ReadAll(f) |
| 551 |
c.Assert(err, IsNil) |
| 552 |
|
| 553 |
c.Assert(string(cnt), Equals, "foo") |
| 554 |
} |
| 555 |
|
| 556 |
func (s *SuiteDotGit) TestIndex(c *C) { |
| 557 |
fs := fixtures.Basic().ByTag(".git").One().DotGit() |
| 558 |
dir := New(fs) |
| 559 |
|
| 560 |
idx, err := dir.Index() |
| 561 |
c.Assert(err, IsNil) |
| 562 |
c.Assert(idx, NotNil) |
| 563 |
} |
| 564 |
|
| 565 |
func (s *SuiteDotGit) TestIndexWriteAndIndex(c *C) { |
| 566 |
fs := s.TemporalFilesystem(c) |
| 567 |
|
| 568 |
dir := New(fs) |
| 569 |
|
| 570 |
f, err := dir.IndexWriter() |
| 571 |
c.Assert(err, IsNil) |
| 572 |
|
| 573 |
_, err = f.Write([]byte("foo")) |
| 574 |
c.Assert(err, IsNil) |
| 575 |
|
| 576 |
f, err = dir.Index() |
| 577 |
c.Assert(err, IsNil) |
| 578 |
|
| 579 |
cnt, err := io.ReadAll(f) |
| 580 |
c.Assert(err, IsNil) |
| 581 |
|
| 582 |
c.Assert(string(cnt), Equals, "foo") |
| 583 |
} |
| 584 |
|
| 585 |
func (s *SuiteDotGit) TestShallow(c *C) { |
| 586 |
fs := fixtures.Basic().ByTag(".git").One().DotGit() |
| 587 |
dir := New(fs) |
| 588 |
|
| 589 |
file, err := dir.Shallow() |
| 590 |
c.Assert(err, IsNil) |
| 591 |
c.Assert(file, IsNil) |
| 592 |
} |
| 593 |
|
| 594 |
func (s *SuiteDotGit) TestShallowWriteAndShallow(c *C) { |
| 595 |
fs := s.TemporalFilesystem(c) |
| 596 |
|
| 597 |
dir := New(fs) |
| 598 |
|
| 599 |
f, err := dir.ShallowWriter() |
| 600 |
c.Assert(err, IsNil) |
| 601 |
|
| 602 |
_, err = f.Write([]byte("foo")) |
| 603 |
c.Assert(err, IsNil) |
| 604 |
|
| 605 |
f, err = dir.Shallow() |
| 606 |
c.Assert(err, IsNil) |
| 607 |
|
| 608 |
cnt, err := io.ReadAll(f) |
| 609 |
c.Assert(err, IsNil) |
| 610 |
|
| 611 |
c.Assert(string(cnt), Equals, "foo") |
| 612 |
} |
| 613 |
|
| 614 |
func findReference(refs []*plumbing.Reference, name string) *plumbing.Reference { |
| 615 |
n := plumbing.ReferenceName(name) |
| 616 |
for _, ref := range refs { |
| 617 |
if ref.Name() == n { |
| 618 |
return ref |
| 619 |
} |
| 620 |
} |
| 621 |
|
| 622 |
return nil |
| 623 |
} |
| 624 |
|
| 625 |
func (s *SuiteDotGit) TestObjectPacks(c *C) { |
| 626 |
f := fixtures.Basic().ByTag(".git").One() |
| 627 |
fs := f.DotGit() |
| 628 |
dir := New(fs) |
| 629 |
|
| 630 |
testObjectPacks(c, fs, dir, f) |
| 631 |
} |
| 632 |
|
| 633 |
func (s *SuiteDotGit) TestObjectPacksExclusive(c *C) { |
| 634 |
f := fixtures.Basic().ByTag(".git").One() |
| 635 |
fs := f.DotGit() |
| 636 |
dir := NewWithOptions(fs, Options{ExclusiveAccess: true}) |
| 637 |
|
| 638 |
testObjectPacks(c, fs, dir, f) |
| 639 |
} |
| 640 |
|
| 641 |
func testObjectPacks(c *C, fs billy.Filesystem, dir *DotGit, f *fixtures.Fixture) { |
| 642 |
hashes, err := dir.ObjectPacks() |
| 643 |
c.Assert(err, IsNil) |
| 644 |
c.Assert(hashes, HasLen, 1) |
| 645 |
c.Assert(hashes[0], Equals, plumbing.NewHash(f.PackfileHash)) |
| 646 |
|
| 647 |
// Make sure that a random file in the pack directory doesn't |
| 648 |
// break everything. |
| 649 |
badFile, err := fs.Create("objects/pack/OOPS_THIS_IS_NOT_RIGHT.pack") |
| 650 |
c.Assert(err, IsNil) |
| 651 |
err = badFile.Close() |
| 652 |
c.Assert(err, IsNil) |
| 653 |
// temporary file generated by git gc |
| 654 |
tmpFile, err := fs.Create("objects/pack/.tmp-11111-pack-58rf8y4wm1b1k52bpe0kdlx6lpreg6ahso8n3ylc.pack") |
| 655 |
c.Assert(err, IsNil) |
| 656 |
err = tmpFile.Close() |
| 657 |
c.Assert(err, IsNil) |
| 658 |
|
| 659 |
hashes2, err := dir.ObjectPacks() |
| 660 |
c.Assert(err, IsNil) |
| 661 |
c.Assert(hashes2, HasLen, 1) |
| 662 |
c.Assert(hashes[0], Equals, hashes2[0]) |
| 663 |
} |
| 664 |
|
| 665 |
func (s *SuiteDotGit) TestObjectPack(c *C) { |
| 666 |
f := fixtures.Basic().ByTag(".git").One() |
| 667 |
fs := f.DotGit() |
| 668 |
dir := New(fs) |
| 669 |
|
| 670 |
pack, err := dir.ObjectPack(plumbing.NewHash(f.PackfileHash)) |
| 671 |
c.Assert(err, IsNil) |
| 672 |
c.Assert(filepath.Ext(pack.Name()), Equals, ".pack") |
| 673 |
} |
| 674 |
|
| 675 |
func (s *SuiteDotGit) TestObjectPackWithKeepDescriptors(c *C) { |
| 676 |
f := fixtures.Basic().ByTag(".git").One() |
| 677 |
fs := f.DotGit() |
| 678 |
dir := NewWithOptions(fs, Options{KeepDescriptors: true}) |
| 679 |
|
| 680 |
pack, err := dir.ObjectPack(plumbing.NewHash(f.PackfileHash)) |
| 681 |
c.Assert(err, IsNil) |
| 682 |
c.Assert(filepath.Ext(pack.Name()), Equals, ".pack") |
| 683 |
|
| 684 |
// Move to an specific offset |
| 685 |
pack.Seek(42, io.SeekStart) |
| 686 |
|
| 687 |
pack2, err := dir.ObjectPack(plumbing.NewHash(f.PackfileHash)) |
| 688 |
c.Assert(err, IsNil) |
| 689 |
|
| 690 |
// If the file is the same the offset should be the same |
| 691 |
offset, err := pack2.Seek(0, io.SeekCurrent) |
| 692 |
c.Assert(err, IsNil) |
| 693 |
c.Assert(offset, Equals, int64(42)) |
| 694 |
|
| 695 |
err = dir.Close() |
| 696 |
c.Assert(err, IsNil) |
| 697 |
|
| 698 |
pack2, err = dir.ObjectPack(plumbing.NewHash(f.PackfileHash)) |
| 699 |
c.Assert(err, IsNil) |
| 700 |
|
| 701 |
// If the file is opened again its offset should be 0 |
| 702 |
offset, err = pack2.Seek(0, io.SeekCurrent) |
| 703 |
c.Assert(err, IsNil) |
| 704 |
c.Assert(offset, Equals, int64(0)) |
| 705 |
|
| 706 |
err = pack2.Close() |
| 707 |
c.Assert(err, IsNil) |
| 708 |
|
| 709 |
err = dir.Close() |
| 710 |
c.Assert(err, NotNil) |
| 711 |
} |
| 712 |
|
| 713 |
func (s *SuiteDotGit) TestObjectPackIdx(c *C) { |
| 714 |
f := fixtures.Basic().ByTag(".git").One() |
| 715 |
fs := f.DotGit() |
| 716 |
dir := New(fs) |
| 717 |
|
| 718 |
idx, err := dir.ObjectPackIdx(plumbing.NewHash(f.PackfileHash)) |
| 719 |
c.Assert(err, IsNil) |
| 720 |
c.Assert(filepath.Ext(idx.Name()), Equals, ".idx") |
| 721 |
c.Assert(idx.Close(), IsNil) |
| 722 |
} |
| 723 |
|
| 724 |
func (s *SuiteDotGit) TestObjectPackNotFound(c *C) { |
| 725 |
fs := fixtures.Basic().ByTag(".git").One().DotGit() |
| 726 |
dir := New(fs) |
| 727 |
|
| 728 |
pack, err := dir.ObjectPack(plumbing.ZeroHash) |
| 729 |
c.Assert(err, Equals, ErrPackfileNotFound) |
| 730 |
c.Assert(pack, IsNil) |
| 731 |
|
| 732 |
idx, err := dir.ObjectPackIdx(plumbing.ZeroHash) |
| 733 |
c.Assert(err, Equals, ErrPackfileNotFound) |
| 734 |
c.Assert(idx, IsNil) |
| 735 |
} |
| 736 |
|
| •737 |
func looseObjectSize(t require.TestingT, content string) int64 { |
| 738 |
var buf bytes.Buffer |
| 739 |
w := zlib.NewWriter(&buf) |
| 740 |
_, err := w.Write([]byte(content)) |
| 741 |
require.NoError(t, err) |
| 742 |
require.NoError(t, w.Close()) |
| 743 |
|
| 744 |
return int64(buf.Len()) |
| 745 |
} |
| 746 |
|
| 747 |
func (s *SuiteDotGit) TestNewObject(c *C) { |
| 748 |
fs := s.TemporalFilesystem(c) |
| 749 |
|
| 750 |
dir := New(fs) |
| 751 |
w, err := dir.NewObject() |
| 752 |
c.Assert(err, IsNil) |
| 753 |
|
| 754 |
err = w.WriteHeader(plumbing.BlobObject, 14) |
| 755 |
c.Assert(err, IsNil) |
| 756 |
n, err := w.Write([]byte("this is a test")) |
| 757 |
c.Assert(err, IsNil) |
| 758 |
c.Assert(n, Equals, 14) |
| 759 |
|
| 760 |
c.Assert(w.Hash().String(), Equals, "a8a940627d132695a9769df883f85992f0ff4a43") |
| 761 |
|
| 762 |
err = w.Close() |
| 763 |
c.Assert(err, IsNil) |
| 764 |
|
| 765 |
i, err := fs.Stat("objects/a8/a940627d132695a9769df883f85992f0ff4a43") |
| 766 |
c.Assert(err, IsNil) |
| •767 |
c.Assert(i.Size(), Equals, looseObjectSize(c, "blob 14\x00this is a test")) |
| 768 |
} |
| 769 |
|
| 770 |
func (s *SuiteDotGit) TestObjects(c *C) { |
| 771 |
fs := fixtures.ByTag(".git").ByTag("unpacked").One().DotGit() |
| 772 |
dir := New(fs) |
| 773 |
|
| 774 |
testObjects(c, fs, dir) |
| 775 |
testObjectsWithPrefix(c, fs, dir) |
| 776 |
} |
| 777 |
|
| 778 |
func (s *SuiteDotGit) TestObjectsExclusive(c *C) { |
| 779 |
fs := fixtures.ByTag(".git").ByTag("unpacked").One().DotGit() |
| 780 |
dir := NewWithOptions(fs, Options{ExclusiveAccess: true}) |
| 781 |
|
| 782 |
testObjects(c, fs, dir) |
| 783 |
testObjectsWithPrefix(c, fs, dir) |
| 784 |
} |
| 785 |
|
| 786 |
func testObjects(c *C, _ billy.Filesystem, dir *DotGit) { |
| 787 |
hashes, err := dir.Objects() |
| 788 |
c.Assert(err, IsNil) |
| 789 |
c.Assert(hashes, HasLen, 187) |
| 790 |
c.Assert(hashes[0].String(), Equals, "0097821d427a3c3385898eb13b50dcbc8702b8a3") |
| 791 |
c.Assert(hashes[1].String(), Equals, "01d5fa556c33743006de7e76e67a2dfcd994ca04") |
| 792 |
c.Assert(hashes[2].String(), Equals, "03db8e1fbe133a480f2867aac478fd866686d69e") |
| 793 |
} |
| 794 |
|
| 795 |
func testObjectsWithPrefix(c *C, _ billy.Filesystem, dir *DotGit) { |
| 796 |
prefix, _ := hex.DecodeString("01d5") |
| 797 |
hashes, err := dir.ObjectsWithPrefix(prefix) |
| 798 |
c.Assert(err, IsNil) |
| 799 |
c.Assert(hashes, HasLen, 1) |
| 800 |
c.Assert(hashes[0].String(), Equals, "01d5fa556c33743006de7e76e67a2dfcd994ca04") |
| 801 |
|
| 802 |
// Empty prefix should yield all objects. |
| 803 |
// (subset of testObjects) |
| 804 |
hashes, err = dir.ObjectsWithPrefix(nil) |
| 805 |
c.Assert(err, IsNil) |
| 806 |
c.Assert(hashes, HasLen, 187) |
| 807 |
} |
| 808 |
|
| 809 |
func (s *SuiteDotGit) TestObjectsNoFolder(c *C) { |
| 810 |
fs := s.TemporalFilesystem(c) |
| 811 |
|
| 812 |
dir := New(fs) |
| 813 |
hash, err := dir.Objects() |
| 814 |
c.Assert(err, IsNil) |
| 815 |
c.Assert(hash, HasLen, 0) |
| 816 |
} |
| 817 |
|
| 818 |
func (s *SuiteDotGit) TestObject(c *C) { |
| 819 |
fs := fixtures.ByTag(".git").ByTag("unpacked").One().DotGit() |
| 820 |
dir := New(fs) |
| 821 |
|
| 822 |
hash := plumbing.NewHash("03db8e1fbe133a480f2867aac478fd866686d69e") |
| 823 |
file, err := dir.Object(hash) |
| 824 |
c.Assert(err, IsNil) |
| 825 |
c.Assert(strings.HasSuffix( |
| 826 |
file.Name(), fs.Join("objects", "03", "db8e1fbe133a480f2867aac478fd866686d69e")), |
| 827 |
Equals, true, |
| 828 |
) |
| 829 |
incomingHash := "9d25e0f9bde9f82882b49fe29117b9411cb157b7" // made up hash |
| 830 |
incomingDirPath := fs.Join("objects", "tmp_objdir-incoming-123456") |
| 831 |
incomingFilePath := fs.Join(incomingDirPath, incomingHash[0:2], incomingHash[2:40]) |
| 832 |
fs.MkdirAll(incomingDirPath, os.FileMode(0755)) |
| 833 |
fs.Create(incomingFilePath) |
| 834 |
|
| 835 |
_, err = dir.Object(plumbing.NewHash(incomingHash)) |
| 836 |
c.Assert(err, IsNil) |
| 837 |
} |
| 838 |
|
| 839 |
func (s *SuiteDotGit) TestPreGit235Object(c *C) { |
| 840 |
fs := fixtures.ByTag(".git").ByTag("unpacked").One().DotGit() |
| 841 |
dir := New(fs) |
| 842 |
|
| 843 |
hash := plumbing.NewHash("03db8e1fbe133a480f2867aac478fd866686d69e") |
| 844 |
file, err := dir.Object(hash) |
| 845 |
c.Assert(err, IsNil) |
| 846 |
c.Assert(strings.HasSuffix( |
| 847 |
file.Name(), fs.Join("objects", "03", "db8e1fbe133a480f2867aac478fd866686d69e")), |
| 848 |
Equals, true, |
| 849 |
) |
| 850 |
incomingHash := "9d25e0f9bde9f82882b49fe29117b9411cb157b7" // made up hash |
| 851 |
incomingDirPath := fs.Join("objects", "incoming-123456") |
| 852 |
incomingFilePath := fs.Join(incomingDirPath, incomingHash[0:2], incomingHash[2:40]) |
| 853 |
fs.MkdirAll(incomingDirPath, os.FileMode(0755)) |
| 854 |
fs.Create(incomingFilePath) |
| 855 |
|
| 856 |
_, err = dir.Object(plumbing.NewHash(incomingHash)) |
| 857 |
c.Assert(err, IsNil) |
| 858 |
} |
| 859 |
|
| 860 |
func (s *SuiteDotGit) TestObjectStat(c *C) { |
| 861 |
fs := fixtures.ByTag(".git").ByTag("unpacked").One().DotGit() |
| 862 |
dir := New(fs) |
| 863 |
|
| 864 |
hash := plumbing.NewHash("03db8e1fbe133a480f2867aac478fd866686d69e") |
| 865 |
_, err := dir.ObjectStat(hash) |
| 866 |
c.Assert(err, IsNil) |
| 867 |
incomingHash := "9d25e0f9bde9f82882b49fe29117b9411cb157b7" // made up hash |
| 868 |
incomingDirPath := fs.Join("objects", "tmp_objdir-incoming-123456") |
| 869 |
incomingFilePath := fs.Join(incomingDirPath, incomingHash[0:2], incomingHash[2:40]) |
| 870 |
fs.MkdirAll(incomingDirPath, os.FileMode(0755)) |
| 871 |
fs.Create(incomingFilePath) |
| 872 |
|
| 873 |
_, err = dir.ObjectStat(plumbing.NewHash(incomingHash)) |
| 874 |
c.Assert(err, IsNil) |
| 875 |
} |
| 876 |
|
| 877 |
func (s *SuiteDotGit) TestObjectDelete(c *C) { |
| 878 |
fs := fixtures.ByTag(".git").ByTag("unpacked").One().DotGit() |
| 879 |
dir := New(fs) |
| 880 |
|
| 881 |
hash := plumbing.NewHash("03db8e1fbe133a480f2867aac478fd866686d69e") |
| 882 |
err := dir.ObjectDelete(hash) |
| 883 |
c.Assert(err, IsNil) |
| 884 |
|
| 885 |
incomingHash := "9d25e0f9bde9f82882b49fe29117b9411cb157b7" // made up hash |
| 886 |
incomingDirPath := fs.Join("objects", "tmp_objdir-incoming-123456") |
| 887 |
incomingSubDirPath := fs.Join(incomingDirPath, incomingHash[0:2]) |
| 888 |
incomingFilePath := fs.Join(incomingSubDirPath, incomingHash[2:40]) |
| 889 |
|
| 890 |
err = fs.MkdirAll(incomingSubDirPath, os.FileMode(0755)) |
| 891 |
c.Assert(err, IsNil) |
| 892 |
|
| 893 |
f, err := fs.Create(incomingFilePath) |
| 894 |
c.Assert(err, IsNil) |
| 895 |
|
| 896 |
err = f.Close() |
| 897 |
c.Assert(err, IsNil) |
| 898 |
|
| 899 |
err = dir.ObjectDelete(plumbing.NewHash(incomingHash)) |
| 900 |
c.Assert(err, IsNil) |
| 901 |
} |
| 902 |
|
| 903 |
func (s *SuiteDotGit) TestObjectNotFound(c *C) { |
| 904 |
fs := fixtures.ByTag(".git").ByTag("unpacked").One().DotGit() |
| 905 |
dir := New(fs) |
| 906 |
|
| 907 |
hash := plumbing.NewHash("not-found-object") |
| 908 |
file, err := dir.Object(hash) |
| 909 |
c.Assert(err, NotNil) |
| 910 |
c.Assert(file, IsNil) |
| 911 |
} |
| 912 |
|
| 913 |
func (s *SuiteDotGit) TestSubmodules(c *C) { |
| 914 |
fs := fixtures.ByTag("submodule").One().DotGit() |
| 915 |
dir := New(fs) |
| 916 |
|
| 917 |
m, err := dir.Module("basic") |
| 918 |
c.Assert(err, IsNil) |
| 919 |
c.Assert(strings.HasSuffix(m.Root(), m.Join(".git", "modules", "basic")), Equals, true) |
| 920 |
} |
| 921 |
|
| 922 |
func (s *SuiteDotGit) TestPackRefs(c *C) { |
| 923 |
fs := s.TemporalFilesystem(c) |
| 924 |
|
| 925 |
dir := New(fs) |
| 926 |
|
| 927 |
err := dir.SetRef(plumbing.NewReferenceFromStrings( |
| 928 |
"refs/heads/foo", |
| 929 |
"e8d3ffab552895c19b9fcf7aa264d277cde33881", |
| 930 |
), nil) |
| 931 |
c.Assert(err, IsNil) |
| 932 |
err = dir.SetRef(plumbing.NewReferenceFromStrings( |
| 933 |
"refs/heads/bar", |
| 934 |
"a8d3ffab552895c19b9fcf7aa264d277cde33881", |
| 935 |
), nil) |
| 936 |
c.Assert(err, IsNil) |
| 937 |
|
| 938 |
refs, err := dir.Refs() |
| 939 |
c.Assert(err, IsNil) |
| 940 |
c.Assert(refs, HasLen, 2) |
| 941 |
looseCount, err := dir.CountLooseRefs() |
| 942 |
c.Assert(err, IsNil) |
| 943 |
c.Assert(looseCount, Equals, 2) |
| 944 |
|
| 945 |
err = dir.PackRefs() |
| 946 |
c.Assert(err, IsNil) |
| 947 |
|
| 948 |
// Make sure the refs are still there, but no longer loose. |
| 949 |
refs, err = dir.Refs() |
| 950 |
c.Assert(err, IsNil) |
| 951 |
c.Assert(refs, HasLen, 2) |
| 952 |
looseCount, err = dir.CountLooseRefs() |
| 953 |
c.Assert(err, IsNil) |
| 954 |
c.Assert(looseCount, Equals, 0) |
| 955 |
|
| 956 |
ref, err := dir.Ref("refs/heads/foo") |
| 957 |
c.Assert(err, IsNil) |
| 958 |
c.Assert(ref, NotNil) |
| 959 |
c.Assert(ref.Hash().String(), Equals, "e8d3ffab552895c19b9fcf7aa264d277cde33881") |
| 960 |
ref, err = dir.Ref("refs/heads/bar") |
| 961 |
c.Assert(err, IsNil) |
| 962 |
c.Assert(ref, NotNil) |
| 963 |
c.Assert(ref.Hash().String(), Equals, "a8d3ffab552895c19b9fcf7aa264d277cde33881") |
| 964 |
|
| 965 |
// Now update one of them, re-pack, and check again. |
| 966 |
err = dir.SetRef(plumbing.NewReferenceFromStrings( |
| 967 |
"refs/heads/foo", |
| 968 |
"b8d3ffab552895c19b9fcf7aa264d277cde33881", |
| 969 |
), nil) |
| 970 |
c.Assert(err, IsNil) |
| 971 |
looseCount, err = dir.CountLooseRefs() |
| 972 |
c.Assert(err, IsNil) |
| 973 |
c.Assert(looseCount, Equals, 1) |
| 974 |
err = dir.PackRefs() |
| 975 |
c.Assert(err, IsNil) |
| 976 |
|
| 977 |
// Make sure the refs are still there, but no longer loose. |
| 978 |
refs, err = dir.Refs() |
| 979 |
c.Assert(err, IsNil) |
| 980 |
c.Assert(refs, HasLen, 2) |
| 981 |
looseCount, err = dir.CountLooseRefs() |
| 982 |
c.Assert(err, IsNil) |
| 983 |
c.Assert(looseCount, Equals, 0) |
| 984 |
|
| 985 |
ref, err = dir.Ref("refs/heads/foo") |
| 986 |
c.Assert(err, IsNil) |
| 987 |
c.Assert(ref, NotNil) |
| 988 |
c.Assert(ref.Hash().String(), Equals, "b8d3ffab552895c19b9fcf7aa264d277cde33881") |
| 989 |
} |
| 990 |
|
| 991 |
func TestAlternatesDefault(t *testing.T) { |
| 992 |
// Create a new dotgit object. |
| 993 |
dotFS := osfs.New(t.TempDir()) |
| 994 |
|
| 995 |
testAlternates(t, dotFS, dotFS) |
| 996 |
} |
| 997 |
|
| 998 |
func TestAlternatesWithFS(t *testing.T) { |
| 999 |
// Create a new dotgit object with a specific FS for alternates. |
| 1000 |
altFS := osfs.New(t.TempDir()) |
| 1001 |
dotFS, _ := altFS.Chroot("repo2") |
| 1002 |
|
| 1003 |
testAlternates(t, dotFS, altFS) |
| 1004 |
} |
| 1005 |
|
| 1006 |
func TestAlternatesWithBoundOS(t *testing.T) { |
| 1007 |
// Create a new dotgit object with a specific FS for alternates. |
| 1008 |
altFS := osfs.New(t.TempDir(), osfs.WithBoundOS()) |
| 1009 |
dotFS, _ := altFS.Chroot("repo2") |
| 1010 |
|
| 1011 |
testAlternates(t, dotFS, altFS) |
| 1012 |
} |
| 1013 |
|
| 1014 |
func testAlternates(t *testing.T, dotFS, altFS billy.Filesystem) { |
| 1015 |
tests := []struct { |
| 1016 |
name string |
| 1017 |
in []string |
| 1018 |
inWindows []string |
| 1019 |
setup func() |
| 1020 |
wantErr bool |
| 1021 |
wantRoots []string |
| 1022 |
}{ |
| 1023 |
{ |
| 1024 |
name: "no alternates", |
| 1025 |
}, |
| 1026 |
{ |
| 1027 |
name: "abs path", |
| 1028 |
in: []string{filepath.Join(altFS.Root(), "./repo1/.git/objects")}, |
| 1029 |
inWindows: []string{filepath.Join(altFS.Root(), ".\\repo1\\.git\\objects")}, |
| 1030 |
setup: func() { |
| 1031 |
err := altFS.MkdirAll(filepath.Join("repo1", ".git", "objects"), 0o700) |
| 1032 |
assert.NoError(t, err) |
| 1033 |
}, |
| 1034 |
wantRoots: []string{filepath.Join("repo1", ".git")}, |
| 1035 |
}, |
| 1036 |
{ |
| 1037 |
name: "rel path", |
| 1038 |
in: []string{"../../../repo3//.git/objects"}, |
| 1039 |
inWindows: []string{"..\\..\\..\\repo3\\.git\\objects"}, |
| 1040 |
setup: func() { |
| 1041 |
err := altFS.MkdirAll(filepath.Join("repo3", ".git", "objects"), 0o700) |
| 1042 |
assert.NoError(t, err) |
| 1043 |
}, |
| 1044 |
wantRoots: []string{filepath.Join("repo3", ".git")}, |
| 1045 |
}, |
| 1046 |
{ |
| 1047 |
name: "invalid abs path", |
| 1048 |
in: []string{"/alt/target2"}, |
| 1049 |
inWindows: []string{"\\alt\\target2"}, |
| 1050 |
wantErr: true, |
| 1051 |
}, |
| 1052 |
{ |
| 1053 |
name: "invalid rel path", |
| 1054 |
in: []string{"../../../alt/target3"}, |
| 1055 |
inWindows: []string{"..\\..\\..\\alt\\target3"}, |
| 1056 |
wantErr: true, |
| 1057 |
}, |
| 1058 |
} |
| 1059 |
|
| 1060 |
for _, tc := range tests { |
| 1061 |
t.Run(tc.name, func(t *testing.T) { |
| 1062 |
dir := NewWithOptions(dotFS, Options{AlternatesFS: altFS}) |
| 1063 |
err := dir.Initialize() |
| 1064 |
assert.NoError(t, err) |
| 1065 |
|
| 1066 |
content := strings.Join(tc.in, "\n") |
| 1067 |
if runtime.GOOS == "windows" { |
| 1068 |
content = strings.Join(tc.inWindows, "\r\n") |
| 1069 |
} |
| 1070 |
|
| 1071 |
// Create alternates file. |
| 1072 |
altpath := dotFS.Join("objects", "info", "alternates") |
| 1073 |
f, err := dotFS.Create(altpath) |
| 1074 |
assert.NoError(t, err) |
| 1075 |
f.Write([]byte(content)) |
| 1076 |
f.Close() |
| 1077 |
|
| 1078 |
if tc.setup != nil { |
| 1079 |
tc.setup() |
| 1080 |
} |
| 1081 |
|
| 1082 |
dotgits, err := dir.Alternates() |
| 1083 |
if tc.wantErr { |
| 1084 |
assert.Error(t, err) |
| 1085 |
} else { |
| 1086 |
assert.NoError(t, err) |
| 1087 |
} |
| 1088 |
|
| 1089 |
for i, d := range dotgits { |
| 1090 |
assert.Regexp(t, "^"+regexp.QuoteMeta(altFS.Root()), d.fs.Root()) |
| 1091 |
assert.Regexp(t, regexp.QuoteMeta(tc.wantRoots[i])+"$", d.fs.Root()) |
| 1092 |
} |
| 1093 |
}) |
| 1094 |
} |
| 1095 |
} |
| 1096 |
|
| 1097 |
func TestAlternatesDupes(t *testing.T) { |
| 1098 |
dotFS := osfs.New(t.TempDir()) |
| 1099 |
dir := New(dotFS) |
| 1100 |
err := dir.Initialize() |
| 1101 |
assert.NoError(t, err) |
| 1102 |
|
| 1103 |
path := filepath.Join(dotFS.Root(), "target3") |
| 1104 |
dupes := []string{path, path, path, path, path} |
| 1105 |
|
| 1106 |
content := strings.Join(dupes, "\n") |
| 1107 |
if runtime.GOOS == "windows" { |
| 1108 |
content = strings.Join(dupes, "\r\n") |
| 1109 |
} |
| 1110 |
|
| 1111 |
err = dotFS.MkdirAll("target3", 0o700) |
| 1112 |
assert.NoError(t, err) |
| 1113 |
|
| 1114 |
// Create alternates file. |
| 1115 |
altpath := dotFS.Join("objects", "info", "alternates") |
| 1116 |
f, err := dotFS.Create(altpath) |
| 1117 |
assert.NoError(t, err) |
| 1118 |
f.Write([]byte(content)) |
| 1119 |
f.Close() |
| 1120 |
|
| 1121 |
dotgits, err := dir.Alternates() |
| 1122 |
assert.NoError(t, err) |
| 1123 |
assert.Len(t, dotgits, 1) |
| 1124 |
} |
| 1125 |
|
| 1126 |
type norwfs struct { |
| 1127 |
billy.Filesystem |
| 1128 |
} |
| 1129 |
|
| 1130 |
func (f *norwfs) Capabilities() billy.Capability { |
| 1131 |
return billy.Capabilities(f.Filesystem) &^ billy.ReadAndWriteCapability |
| 1132 |
} |
| 1133 |
|
| 1134 |
func (s *SuiteDotGit) TestIncBytes(c *C) { |
| 1135 |
tests := []struct { |
| 1136 |
in []byte |
| 1137 |
out []byte |
| 1138 |
overflow bool |
| 1139 |
}{ |
| 1140 |
{[]byte{0}, []byte{1}, false}, |
| 1141 |
{[]byte{0xff}, []byte{0}, true}, |
| 1142 |
{[]byte{7, 0xff}, []byte{8, 0}, false}, |
| 1143 |
{[]byte{0xff, 0xff}, []byte{0, 0}, true}, |
| 1144 |
} |
| 1145 |
for _, test := range tests { |
| 1146 |
out, overflow := incBytes(test.in) |
| 1147 |
c.Assert(out, DeepEquals, test.out) |
| 1148 |
c.Assert(overflow, Equals, test.overflow) |
| 1149 |
} |
| 1150 |
} |
| 1151 |
|
| 1152 |
// this filesystem wrapper returns os.ErrNotExist if the file matches |
| 1153 |
// the provided paths list |
| 1154 |
type notExistsFS struct { |
| 1155 |
billy.Filesystem |
| 1156 |
|
| 1157 |
paths []string |
| 1158 |
} |
| 1159 |
|
| 1160 |
func (f *notExistsFS) matches(path string) bool { |
| 1161 |
p := filepath.ToSlash(path) |
| 1162 |
for _, n := range f.paths { |
| 1163 |
if p == n { |
| 1164 |
return true |
| 1165 |
} |
| 1166 |
} |
| 1167 |
return false |
| 1168 |
} |
| 1169 |
|
| 1170 |
func (f *notExistsFS) Open(filename string) (billy.File, error) { |
| 1171 |
if f.matches(filename) { |
| 1172 |
return nil, os.ErrNotExist |
| 1173 |
} |
| 1174 |
|
| 1175 |
return f.Filesystem.Open(filename) |
| 1176 |
} |
| 1177 |
|
| 1178 |
func (f *notExistsFS) ReadDir(path string) ([]os.FileInfo, error) { |
| 1179 |
if f.matches(path) { |
| 1180 |
return nil, os.ErrNotExist |
| 1181 |
} |
| 1182 |
|
| 1183 |
return f.Filesystem.ReadDir(path) |
| 1184 |
} |
| 1185 |
|
| 1186 |
func (s *SuiteDotGit) TestDeletedRefs(c *C) { |
| 1187 |
fs := s.TemporalFilesystem(c) |
| 1188 |
|
| 1189 |
dir := New(¬ExistsFS{ |
| 1190 |
Filesystem: fs, |
| 1191 |
paths: []string{ |
| 1192 |
"refs/heads/bar", |
| 1193 |
"refs/heads/baz", |
| 1194 |
}, |
| 1195 |
}) |
| 1196 |
|
| 1197 |
err := dir.SetRef(plumbing.NewReferenceFromStrings( |
| 1198 |
"refs/heads/foo", |
| 1199 |
"e8d3ffab552895c19b9fcf7aa264d277cde33881", |
| 1200 |
), nil) |
| 1201 |
c.Assert(err, IsNil) |
| 1202 |
err = dir.SetRef(plumbing.NewReferenceFromStrings( |
| 1203 |
"refs/heads/bar", |
| 1204 |
"a8d3ffab552895c19b9fcf7aa264d277cde33881", |
| 1205 |
), nil) |
| 1206 |
c.Assert(err, IsNil) |
| 1207 |
err = dir.SetRef(plumbing.NewReferenceFromStrings( |
| 1208 |
"refs/heads/baz/baz", |
| 1209 |
"a8d3ffab552895c19b9fcf7aa264d277cde33881", |
| 1210 |
), nil) |
| 1211 |
c.Assert(err, IsNil) |
| 1212 |
|
| 1213 |
refs, err := dir.Refs() |
| 1214 |
c.Assert(err, IsNil) |
| 1215 |
c.Assert(refs, HasLen, 1) |
| 1216 |
c.Assert(refs[0].Name(), Equals, plumbing.ReferenceName("refs/heads/foo")) |
| 1217 |
} |
| 1218 |
|
| 1219 |
// Checks that setting a reference that has been packed and checking its old value is successful |
| 1220 |
func (s *SuiteDotGit) TestSetPackedRef(c *C) { |
| 1221 |
fs := s.TemporalFilesystem(c) |
| 1222 |
|
| 1223 |
dir := New(fs) |
| 1224 |
|
| 1225 |
err := dir.SetRef(plumbing.NewReferenceFromStrings( |
| 1226 |
"refs/heads/foo", |
| 1227 |
"e8d3ffab552895c19b9fcf7aa264d277cde33881", |
| 1228 |
), nil) |
| 1229 |
c.Assert(err, IsNil) |
| 1230 |
|
| 1231 |
refs, err := dir.Refs() |
| 1232 |
c.Assert(err, IsNil) |
| 1233 |
c.Assert(refs, HasLen, 1) |
| 1234 |
looseCount, err := dir.CountLooseRefs() |
| 1235 |
c.Assert(err, IsNil) |
| 1236 |
c.Assert(looseCount, Equals, 1) |
| 1237 |
|
| 1238 |
err = dir.PackRefs() |
| 1239 |
c.Assert(err, IsNil) |
| 1240 |
|
| 1241 |
// Make sure the refs are still there, but no longer loose. |
| 1242 |
refs, err = dir.Refs() |
| 1243 |
c.Assert(err, IsNil) |
| 1244 |
c.Assert(refs, HasLen, 1) |
| 1245 |
looseCount, err = dir.CountLooseRefs() |
| 1246 |
c.Assert(err, IsNil) |
| 1247 |
c.Assert(looseCount, Equals, 0) |
| 1248 |
|
| 1249 |
ref, err := dir.Ref("refs/heads/foo") |
| 1250 |
c.Assert(err, IsNil) |
| 1251 |
c.Assert(ref, NotNil) |
| 1252 |
c.Assert(ref.Hash().String(), Equals, "e8d3ffab552895c19b9fcf7aa264d277cde33881") |
| 1253 |
|
| 1254 |
// Attempt to update the reference using an invalid old reference value |
| 1255 |
err = dir.SetRef(plumbing.NewReferenceFromStrings( |
| 1256 |
"refs/heads/foo", |
| 1257 |
"b8d3ffab552895c19b9fcf7aa264d277cde33881", |
| 1258 |
), plumbing.NewReferenceFromStrings( |
| 1259 |
"refs/heads/foo", |
| 1260 |
"e8d3ffab552895c19b9fcf7aa264d277cde33882", |
| 1261 |
)) |
| 1262 |
c.Assert(err, Equals, storage.ErrReferenceHasChanged) |
| 1263 |
|
| 1264 |
// Now update the reference and it should pass |
| 1265 |
err = dir.SetRef(plumbing.NewReferenceFromStrings( |
| 1266 |
"refs/heads/foo", |
| 1267 |
"b8d3ffab552895c19b9fcf7aa264d277cde33881", |
| 1268 |
), plumbing.NewReferenceFromStrings( |
| 1269 |
"refs/heads/foo", |
| 1270 |
"e8d3ffab552895c19b9fcf7aa264d277cde33881", |
| 1271 |
)) |
| 1272 |
c.Assert(err, IsNil) |
| 1273 |
looseCount, err = dir.CountLooseRefs() |
| 1274 |
c.Assert(err, IsNil) |
| 1275 |
c.Assert(looseCount, Equals, 1) |
| 1276 |
} |
| 1277 |
|
| 1278 |
func TestIssue55(t *testing.T) { |
| 1279 |
t.Parallel() |
| 1280 |
|
| 1281 |
writeObject := func(fs billy.Filesystem) { |
| 1282 |
t.Helper() |
| 1283 |
|
| 1284 |
dir := New(fs) |
| 1285 |
err := dir.Initialize() |
| 1286 |
require.NoError(t, err) |
| 1287 |
|
| 1288 |
w, err := dir.NewObject() |
| 1289 |
require.NoError(t, err) |
| 1290 |
|
| 1291 |
err = w.WriteHeader(plumbing.BlobObject, 14) |
| 1292 |
require.NoError(t, err) |
| 1293 |
n, err := w.Write([]byte("this is a test")) |
| 1294 |
require.NoError(t, err) |
| 1295 |
assert.Equal(t, 14, n) |
| 1296 |
|
| 1297 |
assert.Equal(t, "a8a940627d132695a9769df883f85992f0ff4a43", w.Hash().String()) |
| 1298 |
|
| 1299 |
err = w.Close() |
| 1300 |
require.NoError(t, err) |
| 1301 |
} |
| 1302 |
|
| 1303 |
for _, tc := range []struct { |
| 1304 |
name string |
| 1305 |
fs billy.Filesystem |
| 1306 |
}{ |
| 1307 |
{"BoundOS", osfs.New(t.TempDir(), osfs.WithBoundOS())}, |
| 1308 |
{"ChrootOS", osfs.New(t.TempDir(), osfs.WithChrootOS())}, |
| 1309 |
} { |
| 1310 |
t.Run(tc.name, func(t *testing.T) { |
| 1311 |
t.Parallel() |
| 1312 |
path := filepath.Join("objects", "a8", "a940627d132695a9769df883f85992f0ff4a43") |
| 1313 |
|
| 1314 |
writeObject(tc.fs) |
| 1315 |
i, err := tc.fs.Stat(path) |
| 1316 |
require.NoError(t, err) |
| •1317 |
assert.Equal(t, looseObjectSize(t, "blob 14\x00this is a test"), i.Size()) |
| 1318 |
|
| 1319 |
ro, err := isReadOnly(tc.fs, path) |
| 1320 |
require.NoError(t, err) |
| 1321 |
assert.True(t, ro, "file %q is not read-only", path) |
| 1322 |
|
| 1323 |
// Recreate the same object. |
| 1324 |
writeObject(tc.fs) |
| 1325 |
i, err = tc.fs.Stat(path) |
| 1326 |
require.NoError(t, err) |
| •1327 |
assert.Equal(t, looseObjectSize(t, "blob 14\x00this is a test"), i.Size()) |
| 1328 |
|
| 1329 |
ro, err = isReadOnly(tc.fs, path) |
| 1330 |
require.NoError(t, err) |
| 1331 |
assert.True(t, ro, "file %q is not read-only", path) |
| 1332 |
}) |
| 1333 |
} |
| 1334 |
} |