Skip to content

Commit 4396437

Browse files
authored
Merge branch 'main' into lunny/remove_wiki_path_ref
2 parents b7c2f08 + 198f37e commit 4396437

File tree

17 files changed

+156
-203
lines changed

17 files changed

+156
-203
lines changed

modules/git/repo_commit_gogit.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,6 @@ func (repo *Repository) GetRefCommitID(name string) (string, error) {
3838
return ref.Hash().String(), nil
3939
}
4040

41-
// SetReference sets the commit ID string of given reference (e.g. branch or tag).
42-
func (repo *Repository) SetReference(name, commitID string) error {
43-
return repo.gogitRepo.Storer.SetReference(plumbing.NewReferenceFromStrings(name, commitID))
44-
}
45-
46-
// RemoveReference removes the given reference (e.g. branch or tag).
47-
func (repo *Repository) RemoveReference(name string) error {
48-
return repo.gogitRepo.Storer.RemoveReference(plumbing.ReferenceName(name))
49-
}
50-
5141
// ConvertToHash returns a Hash object from a potential ID string
5242
func (repo *Repository) ConvertToGitID(commitID string) (ObjectID, error) {
5343
objectFormat, err := repo.GetObjectFormat()

modules/git/repo_commit_nogogit.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,6 @@ func (repo *Repository) GetRefCommitID(name string) (string, error) {
5151
return string(shaBs), nil
5252
}
5353

54-
// SetReference sets the commit ID string of given reference (e.g. branch or tag).
55-
func (repo *Repository) SetReference(name, commitID string) error {
56-
_, _, err := gitcmd.NewCommand("update-ref").AddDynamicArguments(name, commitID).RunStdString(repo.Ctx, &gitcmd.RunOpts{Dir: repo.Path})
57-
return err
58-
}
59-
60-
// RemoveReference removes the given reference (e.g. branch or tag).
61-
func (repo *Repository) RemoveReference(name string) error {
62-
_, _, err := gitcmd.NewCommand("update-ref", "--no-deref", "-d").AddDynamicArguments(name).RunStdString(repo.Ctx, &gitcmd.RunOpts{Dir: repo.Path})
63-
return err
64-
}
65-
6654
// IsCommitExist returns true if given commit exists in current repository.
6755
func (repo *Repository) IsCommitExist(name string) bool {
6856
if err := ensureValidGitRepository(repo.Ctx, repo.Path); err != nil {

modules/git/repo_compare_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"path/filepath"
1010
"testing"
1111

12+
"code.gitea.io/gitea/modules/git/gitcmd"
13+
1214
"github.com/stretchr/testify/assert"
1315
)
1416

@@ -99,7 +101,9 @@ func TestReadWritePullHead(t *testing.T) {
99101

100102
// Write a fake sha1 with only 40 zeros
101103
newCommit := "feaf4ba6bc635fec442f46ddd4512416ec43c2c2"
102-
err = repo.SetReference(PullPrefix+"1/head", newCommit)
104+
_, _, err = gitcmd.NewCommand("update-ref").
105+
AddDynamicArguments(PullPrefix+"1/head", newCommit).
106+
RunStdString(t.Context(), &gitcmd.RunOpts{Dir: repo.Path})
103107
if err != nil {
104108
assert.NoError(t, err)
105109
return
@@ -116,7 +120,9 @@ func TestReadWritePullHead(t *testing.T) {
116120
assert.Equal(t, headContents, newCommit)
117121

118122
// Remove file after the test
119-
err = repo.RemoveReference(PullPrefix + "1/head")
123+
_, _, err = gitcmd.NewCommand("update-ref", "--no-deref", "-d").
124+
AddDynamicArguments(PullPrefix+"1/head").
125+
RunStdString(t.Context(), &gitcmd.RunOpts{Dir: repo.Path})
120126
assert.NoError(t, err)
121127
}
122128

modules/gitrepo/ref.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package gitrepo
5+
6+
import (
7+
"context"
8+
9+
"code.gitea.io/gitea/modules/git/gitcmd"
10+
)
11+
12+
func UpdateRef(ctx context.Context, repo Repository, refName, newCommitID string) error {
13+
_, _, err := gitcmd.NewCommand("update-ref").AddDynamicArguments(refName, newCommitID).RunStdString(ctx, &gitcmd.RunOpts{Dir: repoPath(repo)})
14+
return err
15+
}
16+
17+
func RemoveRef(ctx context.Context, repo Repository, refName string) error {
18+
_, _, err := gitcmd.NewCommand("update-ref", "--no-deref", "-d").
19+
AddDynamicArguments(refName).RunStdString(ctx, &gitcmd.RunOpts{Dir: repoPath(repo)})
20+
return err
21+
}

modules/setting/repository.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ var (
5454
AllowForkWithoutMaximumLimit bool
5555
AllowForkIntoSameOwner bool
5656

57+
// StreamArchives makes Gitea stream git archive files to the client directly instead of creating an archive first.
58+
// Ideally all users should use this streaming method. However, at the moment we don't know whether there are
59+
// any users who still need the old behavior, so we introduce this option, intentionally not documenting it.
60+
// After one or two releases, if no one complains, we will remove this option and always use streaming.
61+
StreamArchives bool
62+
5763
// Repository editor settings
5864
Editor struct {
5965
LineWrapExtensions []string
@@ -167,6 +173,7 @@ var (
167173
DisableStars: false,
168174
DefaultBranch: "main",
169175
AllowForkWithoutMaximumLimit: true,
176+
StreamArchives: true,
170177

171178
// Repository editor settings
172179
Editor: struct {

routers/api/v1/api.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,7 +1247,7 @@ func Routes() *web.Router {
12471247
}, reqToken())
12481248
m.Get("/raw/*", context.ReferencesGitRepo(), context.RepoRefForAPI, reqRepoReader(unit.TypeCode), repo.GetRawFile)
12491249
m.Get("/media/*", context.ReferencesGitRepo(), context.RepoRefForAPI, reqRepoReader(unit.TypeCode), repo.GetRawFileOrLFS)
1250-
m.Methods("HEAD,GET", "/archive/*", reqRepoReader(unit.TypeCode), repo.GetArchive)
1250+
m.Methods("HEAD,GET", "/archive/*", reqRepoReader(unit.TypeCode), context.ReferencesGitRepo(true), repo.GetArchive)
12511251
m.Combo("/forks").Get(repo.ListForks).
12521252
Post(reqToken(), reqRepoReader(unit.TypeCode), bind(api.CreateForkOption{}), repo.CreateFork)
12531253
m.Post("/merge-upstream", reqToken(), mustNotBeArchived, reqRepoWriter(unit.TypeCode), bind(api.MergeUpstreamRequest{}), repo.MergeUpstream)
@@ -1466,7 +1466,7 @@ func Routes() *web.Router {
14661466
m.Delete("", repo.DeleteAvatar)
14671467
}, reqAdmin(), reqToken())
14681468

1469-
m.Methods("HEAD,GET", "/{ball_type:tarball|zipball|bundle}/*", reqRepoReader(unit.TypeCode), repo.DownloadArchive)
1469+
m.Methods("HEAD,GET", "/{ball_type:tarball|zipball|bundle}/*", reqRepoReader(unit.TypeCode), context.ReferencesGitRepo(true), repo.DownloadArchive)
14701470
}, repoAssignment(), checkTokenPublicOnly())
14711471
}, tokenRequiresScopes(auth_model.AccessTokenScopeCategoryRepository))
14721472

routers/api/v1/repo/download.go

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,29 @@
44
package repo
55

66
import (
7+
"errors"
78
"net/http"
89

910
"code.gitea.io/gitea/modules/git"
10-
"code.gitea.io/gitea/modules/gitrepo"
1111
"code.gitea.io/gitea/services/context"
1212
archiver_service "code.gitea.io/gitea/services/repository/archiver"
1313
)
1414

15+
func serveRepoArchive(ctx *context.APIContext, reqFileName string) {
16+
aReq, err := archiver_service.NewRequest(ctx.Repo.Repository.ID, ctx.Repo.GitRepo, reqFileName)
17+
if err != nil {
18+
if errors.Is(err, archiver_service.ErrUnknownArchiveFormat{}) {
19+
ctx.APIError(http.StatusBadRequest, err)
20+
} else if errors.Is(err, archiver_service.RepoRefNotFoundError{}) {
21+
ctx.APIError(http.StatusNotFound, err)
22+
} else {
23+
ctx.APIErrorInternal(err)
24+
}
25+
return
26+
}
27+
archiver_service.ServeRepoArchive(ctx.Base, ctx.Repo.Repository, ctx.Repo.GitRepo, aReq)
28+
}
29+
1530
func DownloadArchive(ctx *context.APIContext) {
1631
var tp git.ArchiveType
1732
switch ballType := ctx.PathParam("ball_type"); ballType {
@@ -25,27 +40,5 @@ func DownloadArchive(ctx *context.APIContext) {
2540
ctx.APIError(http.StatusBadRequest, "Unknown archive type: "+ballType)
2641
return
2742
}
28-
29-
if ctx.Repo.GitRepo == nil {
30-
var err error
31-
ctx.Repo.GitRepo, err = gitrepo.RepositoryFromRequestContextOrOpen(ctx, ctx.Repo.Repository)
32-
if err != nil {
33-
ctx.APIErrorInternal(err)
34-
return
35-
}
36-
}
37-
38-
r, err := archiver_service.NewRequest(ctx.Repo.Repository.ID, ctx.Repo.GitRepo, ctx.PathParam("*")+"."+tp.String())
39-
if err != nil {
40-
ctx.APIErrorInternal(err)
41-
return
42-
}
43-
44-
archive, err := r.Await(ctx)
45-
if err != nil {
46-
ctx.APIErrorInternal(err)
47-
return
48-
}
49-
50-
download(ctx, r.GetArchiveName(), archive)
43+
serveRepoArchive(ctx, ctx.PathParam("*")+"."+tp.String())
5144
}

routers/api/v1/repo/file.go

Lines changed: 1 addition & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ import (
1515
"time"
1616

1717
git_model "code.gitea.io/gitea/models/git"
18-
repo_model "code.gitea.io/gitea/models/repo"
1918
"code.gitea.io/gitea/modules/git"
20-
"code.gitea.io/gitea/modules/gitrepo"
2119
"code.gitea.io/gitea/modules/httpcache"
2220
"code.gitea.io/gitea/modules/json"
2321
"code.gitea.io/gitea/modules/lfs"
@@ -31,7 +29,6 @@ import (
3129
"code.gitea.io/gitea/routers/common"
3230
"code.gitea.io/gitea/services/context"
3331
pull_service "code.gitea.io/gitea/services/pull"
34-
archiver_service "code.gitea.io/gitea/services/repository/archiver"
3532
files_service "code.gitea.io/gitea/services/repository/files"
3633
)
3734

@@ -282,74 +279,7 @@ func GetArchive(ctx *context.APIContext) {
282279
// "404":
283280
// "$ref": "#/responses/notFound"
284281

285-
if ctx.Repo.GitRepo == nil {
286-
var err error
287-
ctx.Repo.GitRepo, err = gitrepo.RepositoryFromRequestContextOrOpen(ctx, ctx.Repo.Repository)
288-
if err != nil {
289-
ctx.APIErrorInternal(err)
290-
return
291-
}
292-
}
293-
294-
archiveDownload(ctx)
295-
}
296-
297-
func archiveDownload(ctx *context.APIContext) {
298-
aReq, err := archiver_service.NewRequest(ctx.Repo.Repository.ID, ctx.Repo.GitRepo, ctx.PathParam("*"))
299-
if err != nil {
300-
if errors.Is(err, archiver_service.ErrUnknownArchiveFormat{}) {
301-
ctx.APIError(http.StatusBadRequest, err)
302-
} else if errors.Is(err, archiver_service.RepoRefNotFoundError{}) {
303-
ctx.APIError(http.StatusNotFound, err)
304-
} else {
305-
ctx.APIErrorInternal(err)
306-
}
307-
return
308-
}
309-
310-
archiver, err := aReq.Await(ctx)
311-
if err != nil {
312-
ctx.APIErrorInternal(err)
313-
return
314-
}
315-
316-
download(ctx, aReq.GetArchiveName(), archiver)
317-
}
318-
319-
func download(ctx *context.APIContext, archiveName string, archiver *repo_model.RepoArchiver) {
320-
downloadName := ctx.Repo.Repository.Name + "-" + archiveName
321-
322-
// Add nix format link header so tarballs lock correctly:
323-
// https://github.com/nixos/nix/blob/56763ff918eb308db23080e560ed2ea3e00c80a7/doc/manual/src/protocols/tarball-fetcher.md
324-
ctx.Resp.Header().Add("Link", fmt.Sprintf(`<%s/archive/%s.%s?rev=%s>; rel="immutable"`,
325-
ctx.Repo.Repository.APIURL(),
326-
archiver.CommitID,
327-
archiver.Type.String(),
328-
archiver.CommitID,
329-
))
330-
331-
rPath := archiver.RelativePath()
332-
if setting.RepoArchive.Storage.ServeDirect() {
333-
// If we have a signed url (S3, object storage), redirect to this directly.
334-
u, err := storage.RepoArchives.URL(rPath, downloadName, ctx.Req.Method, nil)
335-
if u != nil && err == nil {
336-
ctx.Redirect(u.String())
337-
return
338-
}
339-
}
340-
341-
// If we have matched and access to release or issue
342-
fr, err := storage.RepoArchives.Open(rPath)
343-
if err != nil {
344-
ctx.APIErrorInternal(err)
345-
return
346-
}
347-
defer fr.Close()
348-
349-
ctx.ServeContent(fr, &context.ServeHeaderOptions{
350-
Filename: downloadName,
351-
LastModified: archiver.CreatedUnix.AsLocalTime(),
352-
})
282+
serveRepoArchive(ctx, ctx.PathParam("*"))
353283
}
354284

355285
// GetEditorconfig get editor config of a repository

routers/api/v1/repo/issue.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -979,7 +979,7 @@ func DeleteIssue(ctx *context.APIContext) {
979979
return
980980
}
981981

982-
if err = issue_service.DeleteIssue(ctx, ctx.Doer, ctx.Repo.GitRepo, issue); err != nil {
982+
if err = issue_service.DeleteIssue(ctx, ctx.Doer, issue); err != nil {
983983
ctx.APIErrorInternal(err)
984984
return
985985
}

routers/web/repo/issue_list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ func BatchDeleteIssues(ctx *context.Context) {
398398
return
399399
}
400400
for _, issue := range issues {
401-
if err := issue_service.DeleteIssue(ctx, ctx.Doer, ctx.Repo.GitRepo, issue); err != nil {
401+
if err := issue_service.DeleteIssue(ctx, ctx.Doer, issue); err != nil {
402402
ctx.ServerError("DeleteIssue", err)
403403
return
404404
}

0 commit comments

Comments
 (0)