vole
Site
Clone
Read only, no credentials:
https://rodent.software/vole.git
Write over SSH with a registered key:
git clone ssh://git@rodent.software/vole.git
Self-hosted forge that lives in your Git repo.
Resolve LFS pointers in release source archives
dc98c26d4eb07402ddd65b6c05aa70429ce618b3 · Keenan Nemetz · 13d ago · parent 617cda58
Resolve LFS pointers in release source archives A release tarball is generated by git archive from the bare server repository, which has no filter.lfs.smudge. Pointer blobs were copied verbatim, so a source archive of a game project held 130-byte pointers where textures and levels should be. ArchiveResolvingLFS streams git's uncompressed tar through a rewrite: a small regular file that parses as a pointer is replaced by the object from the repository's LFS namespace, and tar.gz is compressed in process so every entry is visible. Non-pointer files stream through, including files larger than the pointer cap that begin with the same version line. A missing object fails the archive rather than shipping the pointer silently. The release page says the contents are included, and the archive ETag gains a version suffix so a cached pointer tarball is revalidated after the change.
core/git/archive_test.go +137
| @@ -0,0 +1,137 @@ | |||
| 1 | package git | ||
| 2 | |||
| 3 | // A release tarball of a game project must contain the assets, not the LFS | ||
| 4 | // pointers that stand in for them. These tests drive the tar rewrite directly, | ||
| 5 | // with a resolver that returns bytes from a map, so the archive path is | ||
| 6 | // exercised without a server or a store. | ||
| 7 | |||
| 8 | import ( | ||
| 9 | "archive/tar" | ||
| 10 | "bytes" | ||
| 11 | "compress/gzip" | ||
| 12 | "crypto/sha256" | ||
| 13 | "encoding/hex" | ||
| 14 | "errors" | ||
| 15 | "io" | ||
| 16 | "strings" | ||
| 17 | "testing" | ||
| 18 | |||
| 19 | "rodent.software/vole/core/lfs" | ||
| 20 | ) | ||
| 21 | |||
| 22 | func pointerFor(t *testing.T, data []byte) string { | ||
| 23 | t.Helper() | ||
| 24 | sum := sha256.Sum256(data) | ||
| 25 | return string(lfs.Pointer{OID: hex.EncodeToString(sum[:]), Size: int64(len(data))}.Encode()) | ||
| 26 | } | ||
| 27 | |||
| 28 | func readArchive(t *testing.T, body []byte, gzipped bool) map[string][]byte { | ||
| 29 | t.Helper() | ||
| 30 | var r io.Reader = bytes.NewReader(body) | ||
| 31 | if gzipped { | ||
| 32 | zr, err := gzip.NewReader(r) | ||
| 33 | if err != nil { | ||
| 34 | t.Fatalf("gzip: %v", err) | ||
| 35 | } | ||
| 36 | defer zr.Close() | ||
| 37 | r = zr | ||
| 38 | } | ||
| 39 | out := map[string][]byte{} | ||
| 40 | tr := tar.NewReader(r) | ||
| 41 | for { | ||
| 42 | hdr, err := tr.Next() | ||
| 43 | if errors.Is(err, io.EOF) { | ||
| 44 | break | ||
| 45 | } | ||
| 46 | if err != nil { | ||
| 47 | t.Fatalf("tar: %v", err) | ||
| 48 | } | ||
| 49 | if hdr.Typeflag != tar.TypeReg { | ||
| 50 | continue | ||
| 51 | } | ||
| 52 | data, err := io.ReadAll(tr) | ||
| 53 | if err != nil { | ||
| 54 | t.Fatalf("tar body %s: %v", hdr.Name, err) | ||
| 55 | } | ||
| 56 | out[hdr.Name] = data | ||
| 57 | } | ||
| 58 | return out | ||
| 59 | } | ||
| 60 | |||
| 61 | func archiveRepo(t *testing.T, files map[string][]byte) *Repo { | ||
| 62 | t.Helper() | ||
| 63 | return blobRepo(t, files) | ||
| 64 | } | ||
| 65 | |||
| 66 | func TestArchiveResolvingLFSReplacesPointers(t *testing.T) { | ||
| 67 | asset := []byte("real texture bytes\x00\x01\x02") | ||
| 68 | pointer := pointerFor(t, asset) | ||
| 69 | files := map[string][]byte{ | ||
| 70 | ".gitattributes": []byte("*.bin filter=lfs diff=lfs merge=lfs -text\n"), | ||
| 71 | "art/hero.bin": []byte(pointer), | ||
| 72 | "readme.md": []byte("hello\n"), | ||
| 73 | "empty.txt": {}, | ||
| 74 | // Larger than a pointer can be, and beginning with the same first line: | ||
| 75 | // this is a real file that must pass through untouched. | ||
| 76 | "big.bin": []byte(pointer + strings.Repeat("padding", 512)), | ||
| 77 | } | ||
| 78 | g := archiveRepo(t, files) | ||
| 79 | resolve := func(oid string, size int64) (io.ReadCloser, error) { | ||
| 80 | if size != int64(len(asset)) { | ||
| 81 | t.Fatalf("resolver asked for %d bytes, pointer says %d", len(asset), size) | ||
| 82 | } | ||
| 83 | return io.NopCloser(bytes.NewReader(asset)), nil | ||
| 84 | } | ||
| 85 | |||
| 86 | for _, tc := range []struct { | ||
| 87 | format string | ||
| 88 | gzipped bool | ||
| 89 | }{ | ||
| 90 | {"tar", false}, | ||
| 91 | {"tar.gz", true}, | ||
| 92 | } { | ||
| 93 | var buf bytes.Buffer | ||
| 94 | if err := g.ArchiveResolvingLFS(&buf, "refs/heads/main", tc.format, "proj-v1/", resolve); err != nil { | ||
| 95 | t.Fatalf("%s: %v", tc.format, err) | ||
| 96 | } | ||
| 97 | got := readArchive(t, buf.Bytes(), tc.gzipped) | ||
| 98 | if string(got["proj-v1/art/hero.bin"]) != string(asset) { | ||
| 99 | t.Fatalf("%s hero.bin = %q, want the stored asset", tc.format, got["proj-v1/art/hero.bin"]) | ||
| 100 | } | ||
| 101 | if string(got["proj-v1/readme.md"]) != "hello\n" { | ||
| 102 | t.Fatalf("%s readme = %q", tc.format, got["proj-v1/readme.md"]) | ||
| 103 | } | ||
| 104 | if string(got["proj-v1/big.bin"]) != string(files["big.bin"]) { | ||
| 105 | t.Fatalf("%s replaced a large file that only looks like a pointer", tc.format) | ||
| 106 | } | ||
| 107 | if !bytes.Equal(got["proj-v1/empty.txt"], nil) { | ||
| 108 | t.Fatalf("%s empty file gained content", tc.format) | ||
| 109 | } | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | func TestArchiveResolvingLFSWithoutAResolverKeepsPointers(t *testing.T) { | ||
| 114 | asset := []byte("asset") | ||
| 115 | pointer := pointerFor(t, asset) | ||
| 116 | g := archiveRepo(t, map[string][]byte{"art/hero.bin": []byte(pointer)}) | ||
| 117 | var buf bytes.Buffer | ||
| 118 | if err := g.ArchiveResolvingLFS(&buf, "refs/heads/main", "tar", "", nil); err != nil { | ||
| 119 | t.Fatal(err) | ||
| 120 | } | ||
| 121 | if got := readArchive(t, buf.Bytes(), false)["art/hero.bin"]; string(got) != pointer { | ||
| 122 | t.Fatalf("nil resolver changed the pointer to %q", got) | ||
| 123 | } | ||
| 124 | } | ||
| 125 | |||
| 126 | func TestArchiveResolvingLFSFailsWhenTheObjectIsMissing(t *testing.T) { | ||
| 127 | pointer := pointerFor(t, []byte("gone")) | ||
| 128 | g := archiveRepo(t, map[string][]byte{"art/hero.bin": []byte(pointer)}) | ||
| 129 | resolve := func(string, int64) (io.ReadCloser, error) { | ||
| 130 | return nil, lfs.ErrNotFound | ||
| 131 | } | ||
| 132 | var buf bytes.Buffer | ||
| 133 | err := g.ArchiveResolvingLFS(&buf, "refs/heads/main", "tar", "", resolve) | ||
| 134 | if !errors.Is(err, lfs.ErrNotFound) { | ||
| 135 | t.Fatalf("archive error = %v, want the missing object", err) | ||
| 136 | } | ||
| 137 | } | ||