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.go +148
| @@ -6,14 +6,31 @@ package git | |||
| 6 | 6 | // that cannot serve stale bytes, and skipping the store means a tarball cannot | |
| 7 | 7 | // drift from the tag it claims to be. Measured at 54 ms tar / 221 ms tar.gz for | |
| 8 | 8 | // a real repository. | |
| 9 | // | ||
| 10 | // A bare repository has no filter.lfs.smudge configured — and should not, since | ||
| 11 | // that would make serving a tarball depend on the git-lfs binary and on the | ||
| 12 | // server fetching from itself. So `git archive` copies LFS pointer files | ||
| 13 | // verbatim. ArchiveResolvingLFS rewrites the tar stream and replaces each | ||
| 14 | // pointer with the bytes its object names, which is what a game project's source | ||
| 15 | // archive has to contain to be worth downloading. | ||
| 9 | 16 | ||
| 10 | 17 | import ( | |
| 18 | "archive/tar" | ||
| 11 | 19 | "bytes" | |
| 20 | "compress/gzip" | ||
| 21 | "errors" | ||
| 12 | 22 | "fmt" | |
| 13 | 23 | "io" | |
| 14 | 24 | "strings" | |
| 25 | |||
| 26 | "rodent.software/vole/core/lfs" | ||
| 15 | 27 | ) | |
| 16 | 28 | ||
| 29 | // LFSResolver returns the stored bytes for the pointer's object. The caller | ||
| 30 | // closes the reader. Returning an error aborts the archive: a tarball that | ||
| 31 | // silently kept a pointer would be the surprise this exists to remove. | ||
| 32 | type LFSResolver func(oid string, size int64) (io.ReadCloser, error) | ||
| 33 | |||
| 17 | 34 | // Archive writes `git archive --format=<format> <rev>` to w. prefix, when set, | |
| 18 | 35 | // is prepended to every path so extraction creates one directory instead of | |
| 19 | 36 | // scattering the tree into the current one. | |
| @@ -35,3 +52,134 @@ func (r *Repo) Archive(w io.Writer, rev, format, prefix string) error { | |||
| 35 | 52 | } | |
| 36 | 53 | return nil | |
| 37 | 54 | } | |
| 55 | |||
| 56 | // ArchiveResolvingLFS is Archive with LFS pointer files replaced by the objects | ||
| 57 | // they name. resolve is called only for a regular file whose committed bytes | ||
| 58 | // parse as a pointer; nil resolve is exactly Archive, which keeps callers that | ||
| 59 | // have no store working. | ||
| 60 | // | ||
| 61 | // Only tar and tar.gz are supported, because the rewrite needs an uncompressed | ||
| 62 | // stream to read and a compression step it controls. That is every format a | ||
| 63 | // release tarball uses. | ||
| 64 | func (r *Repo) ArchiveResolvingLFS(w io.Writer, rev, format, prefix string, resolve LFSResolver) error { | ||
| 65 | if resolve == nil { | ||
| 66 | return r.Archive(w, rev, format, prefix) | ||
| 67 | } | ||
| 68 | if format != "tar" && format != "tar.gz" { | ||
| 69 | return fmt.Errorf("archive %s: cannot resolve LFS objects in format %q", rev, format) | ||
| 70 | } | ||
| 71 | |||
| 72 | // Always ask git for plain tar: gzip is applied here so the pointer rewrite | ||
| 73 | // can see the entries. The gzip header carries no name or time, so the bytes | ||
| 74 | // stay a function of the tag. | ||
| 75 | args := []string{"archive", "--format=tar"} | ||
| 76 | if prefix != "" { | ||
| 77 | args = append(args, "--prefix="+prefix) | ||
| 78 | } | ||
| 79 | args = append(args, rev) | ||
| 80 | cmd := r.command(args...) | ||
| 81 | var errb bytes.Buffer | ||
| 82 | cmd.Stderr = &errb | ||
| 83 | stdout, err := cmd.StdoutPipe() | ||
| 84 | if err != nil { | ||
| 85 | return fmt.Errorf("archive %s: %w", rev, err) | ||
| 86 | } | ||
| 87 | if err := cmd.Start(); err != nil { | ||
| 88 | return fmt.Errorf("archive %s: %w", rev, err) | ||
| 89 | } | ||
| 90 | |||
| 91 | var dst io.Writer = w | ||
| 92 | var gz *gzip.Writer | ||
| 93 | if format == "tar.gz" { | ||
| 94 | gz = gzip.NewWriter(w) | ||
| 95 | dst = gz | ||
| 96 | } | ||
| 97 | tw := tar.NewWriter(dst) | ||
| 98 | rewriteErr := copyArchive(tw, tar.NewReader(stdout), resolve) | ||
| 99 | closeErr := tw.Close() | ||
| 100 | if gz != nil { | ||
| 101 | if err := gz.Close(); closeErr == nil { | ||
| 102 | closeErr = err | ||
| 103 | } | ||
| 104 | } | ||
| 105 | // A failed write means the archive is incomplete; stop git rather than let it | ||
| 106 | // keep producing bytes nobody will read. | ||
| 107 | if rewriteErr != nil || closeErr != nil { | ||
| 108 | _ = cmd.Process.Kill() | ||
| 109 | _ = cmd.Wait() | ||
| 110 | if rewriteErr != nil { | ||
| 111 | return rewriteErr | ||
| 112 | } | ||
| 113 | return closeErr | ||
| 114 | } | ||
| 115 | if err := cmd.Wait(); err != nil { | ||
| 116 | return fmt.Errorf("archive %s: %w: %s", rev, err, strings.TrimSpace(errb.String())) | ||
| 117 | } | ||
| 118 | return nil | ||
| 119 | } | ||
| 120 | |||
| 121 | // copyArchive replays every entry into tw, substituting a pointer's object for | ||
| 122 | // the pointer itself. The head of each regular file is buffered only up to the | ||
| 123 | // pointer cap; everything larger streams through, so a 2 GiB asset still costs | ||
| 124 | // one buffer. | ||
| 125 | func copyArchive(tw *tar.Writer, tr *tar.Reader, resolve LFSResolver) error { | ||
| 126 | for { | ||
| 127 | hdr, err := tr.Next() | ||
| 128 | if errors.Is(err, io.EOF) { | ||
| 129 | return nil | ||
| 130 | } | ||
| 131 | if err != nil { | ||
| 132 | return fmt.Errorf("read archive: %w", err) | ||
| 133 | } | ||
| 134 | // git's tar carries a global pax header with the commit id. It is not a | ||
| 135 | // file and rewriteArchive would reject it, so drop it; the tree is intact. | ||
| 136 | if hdr.Typeflag == tar.TypeXGlobalHeader { | ||
| 137 | continue | ||
| 138 | } | ||
| 139 | if hdr.Typeflag != tar.TypeReg && hdr.Typeflag != tar.TypeRegA { | ||
| 140 | if err := tw.WriteHeader(hdr); err != nil { | ||
| 141 | return fmt.Errorf("write archive entry %s: %w", hdr.Name, err) | ||
| 142 | } | ||
| 143 | continue | ||
| 144 | } | ||
| 145 | |||
| 146 | head := make([]byte, min(hdr.Size, lfs.MaxPointerBytes)) | ||
| 147 | if _, err := io.ReadFull(tr, head); err != nil { | ||
| 148 | return fmt.Errorf("read archive entry %s: %w", hdr.Name, err) | ||
| 149 | } | ||
| 150 | pointer, isPointer := lfs.ParsePointer(head) | ||
| 151 | // A pointer is small by definition. A larger file that merely begins with | ||
| 152 | // the version line is a real file and must not be replaced. | ||
| 153 | if !isPointer || hdr.Size > lfs.MaxPointerBytes { | ||
| 154 | if err := tw.WriteHeader(hdr); err != nil { | ||
| 155 | return fmt.Errorf("write archive entry %s: %w", hdr.Name, err) | ||
| 156 | } | ||
| 157 | if _, err := tw.Write(head); err != nil { | ||
| 158 | return fmt.Errorf("write archive entry %s: %w", hdr.Name, err) | ||
| 159 | } | ||
| 160 | if _, err := io.Copy(tw, tr); err != nil { | ||
| 161 | return fmt.Errorf("write archive entry %s: %w", hdr.Name, err) | ||
| 162 | } | ||
| 163 | continue | ||
| 164 | } | ||
| 165 | |||
| 166 | obj, err := resolve(pointer.OID, pointer.Size) | ||
| 167 | if err != nil { | ||
| 168 | return fmt.Errorf("archive entry %s: LFS object %s: %w", hdr.Name, pointer.OID, err) | ||
| 169 | } | ||
| 170 | hdr.Size = pointer.Size | ||
| 171 | writeErr := tw.WriteHeader(hdr) | ||
| 172 | if writeErr == nil { | ||
| 173 | written, copyErr := io.CopyN(tw, obj, pointer.Size) | ||
| 174 | if copyErr != nil { | ||
| 175 | writeErr = copyErr | ||
| 176 | } else if written != pointer.Size { | ||
| 177 | writeErr = io.ErrUnexpectedEOF | ||
| 178 | } | ||
| 179 | } | ||
| 180 | obj.Close() | ||
| 181 | if writeErr != nil { | ||
| 182 | return fmt.Errorf("write archive entry %s: %w", hdr.Name, writeErr) | ||
| 183 | } | ||
| 184 | } | ||
| 185 | } | ||