fix(repo): avoid a repo-sized temp file for every bundle download (#38863)

Bundle downloads run `git bundle create` into a temp file under
`data/tmp/git-repo-content` and copy it to the response, so every
download puts a second, repo-sized copy on disk before the first byte is
sent. If the process dies mid-request that copy is stranded: the startup
sweep only drops files older than 3 days, and cannot remove a directory
that still holds a newer file.

Streaming `git bundle create -` to the response removes that copy.
`CreateArchive` above already uses the same gitcmd pattern, and the
bundle bytes are unchanged (existing integration assertions on the exact
length still pass).

One consequence: git can now fail after output starts, so a mid-stream
failure truncates the body instead of returning an error.

I did not act on the TODO. A temp ref only works under `refs/heads/*`;
with `refs/bundle/temp-*` the bundle carries no branch and clones empty,
so I noted that on the TODO.

Fixes https://github.com/go-gitea/gitea/issues/38447

---------

Co-authored-by: silverwind <me@silverwind.io>
This commit is contained in:
somaz
2026-08-12 01:33:15 +09:00
committed by GitHub
parent 938cde959e
commit 3580591e28
2 changed files with 35 additions and 18 deletions
+6 -18
View File
@@ -39,10 +39,11 @@ func CreateArchive(ctx context.Context, repo RepositoryFacade, repoName, format
// CreateBundle create bundle content to the target path
func CreateBundle(ctx context.Context, repo RepositoryFacade, commit string, out io.Writer) error {
// TODO: use the following steps instead of creating a temp file, also need to iterate and clean up outdated refs
// git update-ref refs/bundle/temp-{timestamp} {commit}
// git bundle create - refs/bundle/temp-{timestamp}
// git update-ref -d refs/bundle/temp-{timestamp}
// TODO: use the following steps instead of creating a temp repo, also need to iterate and clean up outdated refs
// the temp ref has to be under refs/heads/*, and a clone only checks out with a HEAD, which needs the temp repo
// git update-ref refs/heads/bundle-temp-{timestamp} {commit}
// git bundle create - refs/heads/bundle-temp-{timestamp}
// git update-ref -d refs/heads/bundle-temp-{timestamp}
tmpDir, cleanup, err := setting.AppDataTempDir("git-repo-content").MkdirTempRandom("gitea-bundle")
if err != nil {
return err
@@ -69,18 +70,5 @@ func CreateBundle(ctx context.Context, repo RepositoryFacade, commit string, out
return err
}
tmpFile := filepath.Join(tmpDir, "bundle")
_, _, err = gitTmpCmd().AddArguments("bundle", "create").AddDynamicArguments(tmpFile, "bundle", "HEAD").RunStdString(ctx)
if err != nil {
return err
}
fi, err := os.Open(tmpFile)
if err != nil {
return err
}
defer fi.Close()
_, err = io.Copy(out, fi)
return err
return gitTmpCmd().AddArguments("bundle", "create", "-", "bundle", "HEAD").WithStdoutCopy(out).RunWithStderr(ctx)
}