Skip to content

Commit d447b7c

Browse files
committed
Apply feedback
1 parent f5648ae commit d447b7c

File tree

3 files changed

+30
-76
lines changed

3 files changed

+30
-76
lines changed

routers/api/v1/repo/file.go

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -307,23 +307,23 @@ func archiveDownload(ctx *context.APIContext) {
307307
return
308308
}
309309

310+
// Add nix format link header so tarballs lock correctly:
311+
// https://github.com/nixos/nix/blob/56763ff918eb308db23080e560ed2ea3e00c80a7/doc/manual/src/protocols/tarball-fetcher.md
312+
ctx.Resp.Header().Add("Link", fmt.Sprintf(`<%s/archive/%s.%s?rev=%s>; rel="immutable"`,
313+
ctx.Repo.Repository.APIURL(),
314+
aReq.CommitID,
315+
aReq.Type.String(),
316+
aReq.CommitID,
317+
))
318+
310319
if setting.Repository.StreamArchives {
311320
downloadName := ctx.Repo.Repository.Name + "-" + aReq.GetArchiveName()
312321

313-
// Add nix format link header so tarballs lock correctly:
314-
// https://github.com/nixos/nix/blob/56763ff918eb308db23080e560ed2ea3e00c80a7/doc/manual/src/protocols/tarball-fetcher.md
315-
ctx.Resp.Header().Add("Link", fmt.Sprintf(`<%s/archive/%s.%s?rev=%s>; rel="immutable"`,
316-
ctx.Repo.Repository.APIURL(),
317-
aReq.CommitID,
318-
aReq.Type.String(),
319-
aReq.CommitID,
320-
))
321-
322322
ctx.SetServeHeaders(&context.ServeHeaderOptions{
323323
Filename: downloadName,
324324
})
325325

326-
if err := aReq.Stream(ctx, ctx.Resp); err != nil && !ctx.Written() {
326+
if err := aReq.Stream(ctx, ctx.Repo.GitRepo, ctx.Resp); err != nil && !ctx.Written() {
327327
ctx.APIErrorInternal(err)
328328
}
329329
return
@@ -341,15 +341,6 @@ func archiveDownload(ctx *context.APIContext) {
341341
func download(ctx *context.APIContext, archiveName string, archiver *repo_model.RepoArchiver) {
342342
downloadName := ctx.Repo.Repository.Name + "-" + archiveName
343343

344-
// Add nix format link header so tarballs lock correctly:
345-
// https://github.com/nixos/nix/blob/56763ff918eb308db23080e560ed2ea3e00c80a7/doc/manual/src/protocols/tarball-fetcher.md
346-
ctx.Resp.Header().Add("Link", fmt.Sprintf(`<%s/archive/%s.%s?rev=%s>; rel="immutable"`,
347-
ctx.Repo.Repository.APIURL(),
348-
archiver.CommitID,
349-
archiver.Type.String(),
350-
archiver.CommitID,
351-
))
352-
353344
rPath := archiver.RelativePath()
354345
if setting.RepoArchive.Storage.ServeDirect() {
355346
// If we have a signed url (S3, object storage), redirect to this directly.

routers/web/repo/repo.go

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -377,23 +377,23 @@ func Download(ctx *context.Context) {
377377
return
378378
}
379379

380+
// Add nix format link header so tarballs lock correctly:
381+
// https://github.com/nixos/nix/blob/56763ff918eb308db23080e560ed2ea3e00c80a7/doc/manual/src/protocols/tarball-fetcher.md
382+
ctx.Resp.Header().Add("Link", fmt.Sprintf(`<%s/archive/%s.%s?rev=%s>; rel="immutable"`,
383+
ctx.Repo.Repository.APIURL(),
384+
aReq.CommitID,
385+
aReq.Type.String(),
386+
aReq.CommitID,
387+
))
388+
380389
if setting.Repository.StreamArchives {
381390
downloadName := ctx.Repo.Repository.Name + "-" + aReq.GetArchiveName()
382391

383-
// Add nix format link header so tarballs lock correctly:
384-
// https://github.com/nixos/nix/blob/56763ff918eb308db23080e560ed2ea3e00c80a7/doc/manual/src/protocols/tarball-fetcher.md
385-
ctx.Resp.Header().Add("Link", fmt.Sprintf(`<%s/archive/%s.%s?rev=%s>; rel="immutable"`,
386-
ctx.Repo.Repository.APIURL(),
387-
aReq.CommitID,
388-
aReq.Type.String(),
389-
aReq.CommitID,
390-
))
391-
392392
ctx.SetServeHeaders(&context.ServeHeaderOptions{
393393
Filename: downloadName,
394394
})
395395

396-
if err := aReq.Stream(ctx, ctx.Resp); err != nil && !ctx.Written() {
396+
if err := aReq.Stream(ctx, ctx.Repo.GitRepo, ctx.Resp); err != nil && !ctx.Written() {
397397
ctx.ServerError("archiver.StreamArchive", err)
398398
}
399399
return
@@ -411,12 +411,6 @@ func Download(ctx *context.Context) {
411411
func download(ctx *context.Context, archiveName string, archiver *repo_model.RepoArchiver) {
412412
downloadName := ctx.Repo.Repository.Name + "-" + archiveName
413413

414-
// Add nix format link header so tarballs lock correctly:
415-
// https://github.com/nixos/nix/blob/56763ff918eb308db23080e560ed2ea3e00c80a7/doc/manual/src/protocols/tarball-fetcher.md
416-
ctx.Resp.Header().Add("Link", fmt.Sprintf(`<%s/archive/%s.tar.gz?rev=%s>; rel="immutable"`,
417-
ctx.Repo.Repository.APIURL(),
418-
archiver.CommitID, archiver.CommitID))
419-
420414
rPath := archiver.RelativePath()
421415
if setting.RepoArchive.Storage.ServeDirect() {
422416
// If we have a signed url (S3, object storage), redirect to this directly.

services/repository/archiver/archiver.go

Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -140,38 +140,21 @@ func (aReq *ArchiveRequest) Await(ctx context.Context) (*repo_model.RepoArchiver
140140

141141
// Stream satisfies the ArchiveRequest being passed in. Processing
142142
// will occur directly in this routine.
143-
func (aReq *ArchiveRequest) Stream(ctx context.Context, w io.Writer) error {
144-
ctx, _, finished := process.GetManager().AddContext(ctx, fmt.Sprintf("ArchiveRequest[%d]: %s", aReq.RepoID, aReq.GetArchiveName()))
145-
defer finished()
146-
147-
repo, err := repo_model.GetRepositoryByID(ctx, aReq.RepoID)
148-
if err != nil {
149-
return fmt.Errorf("archiver.LoadRepo failed: %w", err)
150-
}
151-
152-
gitRepo, err := gitrepo.OpenRepository(ctx, repo)
153-
if err != nil {
154-
return err
155-
}
156-
defer gitRepo.Close()
157-
143+
func (aReq *ArchiveRequest) Stream(ctx context.Context, gitRepo *git.Repository, w io.Writer) error {
158144
if aReq.Type == git.ArchiveBundle {
159-
err = gitRepo.CreateBundle(
145+
return gitRepo.CreateBundle(
160146
ctx,
161147
aReq.CommitID,
162148
w,
163149
)
164-
} else {
165-
err = gitRepo.CreateArchive(
166-
ctx,
167-
aReq.Type,
168-
w,
169-
setting.Repository.PrefixArchiveFiles,
170-
aReq.CommitID,
171-
)
172150
}
173-
174-
return err
151+
return gitRepo.CreateArchive(
152+
ctx,
153+
aReq.Type,
154+
w,
155+
setting.Repository.PrefixArchiveFiles,
156+
aReq.CommitID,
157+
)
175158
}
176159

177160
// doArchive satisfies the ArchiveRequest being passed in. Processing
@@ -247,21 +230,7 @@ func doArchive(ctx context.Context, r *ArchiveRequest) (*repo_model.RepoArchiver
247230
}
248231
}()
249232

250-
if archiver.Type == git.ArchiveBundle {
251-
err = gitRepo.CreateBundle(
252-
ctx,
253-
archiver.CommitID,
254-
w,
255-
)
256-
} else {
257-
err = gitRepo.CreateArchive(
258-
ctx,
259-
archiver.Type,
260-
w,
261-
setting.Repository.PrefixArchiveFiles,
262-
archiver.CommitID,
263-
)
264-
}
233+
err = r.Stream(ctx, gitRepo, w)
265234
_ = w.CloseWithError(err)
266235
done <- err
267236
}(done, w, archiver, gitRepo)

0 commit comments

Comments
 (0)