Sign in

Search issue titles and bodies, change requests and comments. Private repositories you cannot read are not included.

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.

server/web/releases.go +39−15

@@ -9,6 +9,7 @@ import (
9 9 "bytes"
10 10 "fmt"
11 11 "html/template"
12 "io"
12 13 "mime"
13 14 "net/http"
14 15 "path"
@@ -170,11 +171,10 @@ func (w *Web) ReleaseView(rw http.ResponseWriter, r *http.Request) {
170 171 // The index holds the same set, read from the branch it was built from.
171 172 artifacts := w.releaseArtifacts(r, m, t.Name)
172 173
173 // git archive copies LFS pointer files verbatim, so a tarball of an LFS
174 // repository holds shopping lists where the reader expects bytes. Saying so is
175 // cheaper than resolving pointers, and a silent pointer is the kind of surprise
176 // that costs an afternoon.
177 lfsPointers := usesLFS(g, t.Commit)
174 // Archives now resolve pointers, so this is only what decides whether the
175 // page needs to say that the contents are real. A pattern in a subdirectory
176 // is not detected; the note is a reassurance, so omitting it cannot mislead.
177 usesLFS := usesLFS(g, t.Commit)
178 178
179 179 // Only an annotated tag has notes of its own; a lightweight tag's message is
180 180 // the commit's subject, which the reader can already see.
@@ -192,7 +192,7 @@ func (w *Web) ReleaseView(rw http.ResponseWriter, r *http.Request) {
192 192 Name string
193 193 Tag git.Tag
194 194 Notes template.HTML
195 LFSPointers bool
195 UsesLFS bool
196 196 Artifacts []metadata.Artifact
197 197 Channels []string
198 198 CanEdit bool
@@ -203,7 +203,7 @@ func (w *Web) ReleaseView(rw http.ResponseWriter, r *http.Request) {
203 203 Name: m.Name,
204 204 Tag: t,
205 205 Notes: notes,
206 LFSPointers: lfsPointers,
206 UsesLFS: usesLFS,
207 207 Artifacts: artifacts,
208 208 Channels: channelNames,
209 209 CanEdit: canManage && refusal == "",
@@ -315,10 +315,13 @@ func (w *Web) releaseArchive(rw http.ResponseWriter, r *http.Request, format str
315 315 if format == "tar.gz" {
316 316 contentType, ext = "application/gzip", ".tar.gz"
317 317 }
318 // The bytes are a pure function of the tag object and the format, so the tag
319 // object id is the validator: a re-tagged name changes the ETag, and nothing
320 // else can change the bytes.
321 etag := `"` + releaseSafe(t.OID) + "-" + strings.ReplaceAll(format, ".", "") + `"`
318 // The bytes are a function of the tag object, the format, and the archive
319 // implementation. The tag object id plus a version suffix is the validator: a
320 // re-tagged name changes the ETag, and so does a change in how the archive is
321 // built (resolving LFS pointers, for example), so a cached pointer tarball
322 // cannot be served after the fix.
323 const archiveVersion = "2"
324 etag := `"` + releaseSafe(t.OID) + "-" + strings.ReplaceAll(format, ".", "") + "-" + archiveVersion + `"`
322 325 rw.Header().Set("ETag", etag)
323 326 rw.Header().Set("Cache-Control", w.downloadCacheControl(m))
324 327 rw.Header().Set("Content-Type", contentType)
@@ -336,18 +339,39 @@ func (w *Web) releaseArchive(rw http.ResponseWriter, r *http.Request, format str
336 339 }
337 340
338 341 prefix := releaseSafe(m.Name) + "-" + releaseSafe(t.Name) + "/"
339 if err := g.Archive(rw, t.Commit, format, prefix); err != nil {
342 if err := g.ArchiveResolvingLFS(rw, t.Commit, format, prefix, w.lfsResolver(m.Name)); err != nil {
340 343 // Headers are already on the wire, so the reader gets a cut archive; the
341 344 // log is the only place left to say so.
342 345 w.Logger("archive %s %s: %v", m.Name, t.Name, err)
343 346 }
344 347 }
345 348
349 // lfsResolver turns a pointer's object id into the stored bytes. Nil when the
350 // instance has no store, which keeps Archive's old behaviour for embedders and
351 // tests that build a Web without one.
352 func (w *Web) lfsResolver(repo string) git.LFSResolver {
353 if w.LFS == nil {
354 return nil
355 }
356 return func(oid string, size int64) (io.ReadCloser, error) {
357 f, stored, err := w.LFS.Open(repo, oid)
358 if err != nil {
359 return nil, err
360 }
361 if stored != size {
362 f.Close()
363 return nil, fmt.Errorf("stored object is %d bytes, the pointer says %d", stored, size)
364 }
365 return f, nil
366 }
367 }
368
346 369 // usesLFS reports whether the tree tracks anything through Git LFS. It reads the
347 370 // root .gitattributes, which is where `git lfs track` writes its patterns; a
348 // pattern declared in a subdirectory is not detected, and the notice is worded so
349 // that the omission cannot make it false. Only the head of the file is read: a
350 // pattern lives on one line, and the file's length is not evidence of anything.
371 // pattern declared in a subdirectory is not detected, and the note it controls is
372 // a reassurance, so the omission cannot make it false. Only the head of the file
373 // is read: a pattern lives on one line, and the file's length is not evidence of
374 // anything.
351 375 func usesLFS(g *git.Repo, commit string) bool {
352 376 const maxGitattributesBytes = 64 << 10
353 377 data, _, err := g.BlobPrefixAt(commit, ".gitattributes", maxGitattributesBytes)