Skip to content

Commit 114d73f

Browse files
committed
temp
1 parent 6911a9a commit 114d73f

File tree

7 files changed

+41
-35
lines changed

7 files changed

+41
-35
lines changed

routers/api/v1/repo/file.go

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ import (
2525
"code.gitea.io/gitea/modules/setting"
2626
"code.gitea.io/gitea/modules/storage"
2727
api "code.gitea.io/gitea/modules/structs"
28+
"code.gitea.io/gitea/modules/util"
2829
"code.gitea.io/gitea/modules/web"
30+
"code.gitea.io/gitea/routers/api/v1/utils"
2931
"code.gitea.io/gitea/routers/common"
3032
"code.gitea.io/gitea/services/context"
3133
pull_service "code.gitea.io/gitea/services/pull"
@@ -894,6 +896,17 @@ func DeleteFile(ctx *context.APIContext) {
894896
}
895897
}
896898

899+
func resolveRefCommit(ctx *context.APIContext, ref string) *utils.RefCommit {
900+
ref = util.IfZero(ref, ctx.Repo.Repository.DefaultBranch)
901+
refCommit, err := utils.ResolveRefCommit(ctx, ref)
902+
if errors.Is(err, util.ErrNotExist) {
903+
ctx.APIErrorNotFound(err)
904+
} else if err != nil {
905+
ctx.APIErrorInternal(err)
906+
}
907+
return refCommit
908+
}
909+
897910
// GetContents Get the metadata and contents (if a file) of an entry in a repository, or a list of entries if a dir
898911
func GetContents(ctx *context.APIContext) {
899912
// swagger:operation GET /repos/{owner}/{repo}/contents/{filepath} repository repoGetContents
@@ -928,18 +941,13 @@ func GetContents(ctx *context.APIContext) {
928941
// "404":
929942
// "$ref": "#/responses/notFound"
930943

931-
if !canReadFiles(ctx.Repo) {
932-
ctx.APIErrorInternal(repo_model.ErrUserDoesNotHaveAccessToRepo{
933-
UserID: ctx.Doer.ID,
934-
RepoName: ctx.Repo.Repository.LowerName,
935-
})
944+
treePath := ctx.PathParam("*")
945+
refCommit := resolveRefCommit(ctx, ctx.FormTrim("ref"))
946+
if ctx.Written() {
936947
return
937948
}
938949

939-
treePath := ctx.PathParam("*")
940-
ref := ctx.FormTrim("ref")
941-
942-
if fileList, err := files_service.GetContentsOrList(ctx, ctx.Repo.Repository, treePath, ref); err != nil {
950+
if fileList, err := files_service.GetContentsOrList(ctx, ctx.Repo.Repository, refCommit, treePath); err != nil {
943951
if git.IsErrNotExist(err) {
944952
ctx.APIErrorNotFound("GetContentsOrList", err)
945953
return
@@ -1023,19 +1031,10 @@ func GetFiles(ctx *context.APIContext) {
10231031
// "$ref": "#/responses/notFound"
10241032

10251033
apiOpts := web.GetForm(ctx).(*api.GetFilesOptions)
1026-
1027-
ref := ctx.FormTrim("ref")
1028-
if ref == "" {
1029-
ref = ctx.Repo.Repository.DefaultBranch
1030-
}
1031-
1032-
if !ctx.Repo.GitRepo.IsReferenceExist(ref) {
1033-
ctx.APIErrorNotFound("GetFiles", "ref does not exist")
1034+
refCommit := resolveRefCommit(ctx, ctx.FormTrim("ref"))
1035+
if ctx.Written() {
10341036
return
10351037
}
1036-
1037-
files := apiOpts.Files
1038-
filesResponse := files_service.GetContentsListFromTrees(ctx, ctx.Repo.Repository, ref, files)
1039-
1040-
ctx.JSON(http.StatusOK, filesResponse)
1038+
filesResponse := files_service.GetContentsListFromTrees(ctx, ctx.Repo.Repository, refCommit, apiOpts.Files)
1039+
ctx.JSON(http.StatusOK, util.SliceNilAsEmpty(filesResponse))
10411040
}

routers/api/v1/repo/status.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
package repo
55

66
import (
7-
"code.gitea.io/gitea/modules/util"
87
"errors"
98
"fmt"
109
"net/http"
1110

1211
"code.gitea.io/gitea/models/db"
1312
git_model "code.gitea.io/gitea/models/git"
1413
api "code.gitea.io/gitea/modules/structs"
14+
"code.gitea.io/gitea/modules/util"
1515
"code.gitea.io/gitea/modules/web"
1616
"code.gitea.io/gitea/routers/api/v1/utils"
1717
"code.gitea.io/gitea/services/context"

routers/api/v1/utils/git.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
package utils
55

66
import (
7+
"errors"
8+
79
"code.gitea.io/gitea/modules/git"
810
"code.gitea.io/gitea/modules/gitrepo"
911
"code.gitea.io/gitea/services/context"
10-
"errors"
1112
)
1213

1314
type RefCommit struct {
@@ -38,6 +39,10 @@ func ResolveRefCommit(ctx *context.APIContext, inputRef string) (_ *RefCommit, e
3839
return &refCommit, nil
3940
}
4041

42+
func NewRefCommit(refName git.RefName, commit *git.Commit) *RefCommit {
43+
return &RefCommit{InputRef: refName.ShortName(), Ref: refName, Commit: commit, CommitID: commit.ID.String()}
44+
}
45+
4146
// GetGitRefs return git references based on filter
4247
func GetGitRefs(ctx *context.APIContext, filter string) ([]*git.Reference, string, error) {
4348
if ctx.Repo.GitRepo == nil {

services/context/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package context
66

77
import (
8-
"code.gitea.io/gitea/modules/git"
98
"errors"
109
"fmt"
1110
"net/http"
@@ -16,6 +15,7 @@ import (
1615
"code.gitea.io/gitea/models/unit"
1716
user_model "code.gitea.io/gitea/models/user"
1817
"code.gitea.io/gitea/modules/cache"
18+
"code.gitea.io/gitea/modules/git"
1919
"code.gitea.io/gitea/modules/gitrepo"
2020
"code.gitea.io/gitea/modules/httpcache"
2121
"code.gitea.io/gitea/modules/log"

services/repository/files/content.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@
44
package files
55

66
import (
7+
"context"
8+
"fmt"
9+
"net/url"
10+
"path"
11+
712
repo_model "code.gitea.io/gitea/models/repo"
813
"code.gitea.io/gitea/modules/git"
914
"code.gitea.io/gitea/modules/gitrepo"
1015
"code.gitea.io/gitea/modules/setting"
1116
api "code.gitea.io/gitea/modules/structs"
1217
"code.gitea.io/gitea/modules/util"
1318
"code.gitea.io/gitea/routers/api/v1/utils"
14-
"context"
15-
"fmt"
16-
"net/url"
17-
"path"
1819
)
1920

2021
// ContentType repo content type

services/repository/files/file.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package files
55

66
import (
7-
"code.gitea.io/gitea/routers/api/v1/utils"
87
"context"
98
"errors"
109
"fmt"
@@ -17,6 +16,7 @@ import (
1716
"code.gitea.io/gitea/modules/setting"
1817
api "code.gitea.io/gitea/modules/structs"
1918
"code.gitea.io/gitea/modules/util"
19+
"code.gitea.io/gitea/routers/api/v1/utils"
2020
)
2121

2222
func GetContentsListFromTrees(ctx context.Context, repo *repo_model.Repository, refCommit *utils.RefCommit, treeNames []string) []*api.ContentsResponse {
@@ -40,10 +40,10 @@ func GetContentsListFromTrees(ctx context.Context, repo *repo_model.Repository,
4040
return files
4141
}
4242

43-
func GetFilesResponseFromCommit(ctx context.Context, repo *repo_model.Repository, commit *git.Commit, branch string, treeNames []string) (*api.FilesResponse, error) {
44-
files := GetContentsListFromTrees(ctx, repo, branch, treeNames)
45-
fileCommitResponse, _ := GetFileCommitResponse(repo, commit) // ok if fails, then will be nil
46-
verification := GetPayloadCommitVerification(ctx, commit)
43+
func GetFilesResponseFromCommit(ctx context.Context, repo *repo_model.Repository, refCommit *utils.RefCommit, treeNames []string) (*api.FilesResponse, error) {
44+
files := GetContentsListFromTrees(ctx, repo, refCommit, treeNames)
45+
fileCommitResponse, _ := GetFileCommitResponse(repo, refCommit.Commit) // ok if fails, then will be nil
46+
verification := GetPayloadCommitVerification(ctx, refCommit.Commit)
4747
filesResponse := &api.FilesResponse{
4848
Files: files,
4949
Commit: fileCommitResponse,

services/repository/files/update.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"code.gitea.io/gitea/modules/setting"
2323
"code.gitea.io/gitea/modules/structs"
2424
"code.gitea.io/gitea/modules/util"
25+
"code.gitea.io/gitea/routers/api/v1/utils"
2526
asymkey_service "code.gitea.io/gitea/services/asymkey"
2627
pull_service "code.gitea.io/gitea/services/pull"
2728
)
@@ -297,7 +298,7 @@ func ChangeRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *use
297298
}
298299

299300
// FIXME: this call seems not right, why it needs to read the file content again
300-
filesResponse, err := GetFilesResponseFromCommit(ctx, repo, commit, opts.NewBranch, treePaths)
301+
filesResponse, err := GetFilesResponseFromCommit(ctx, repo, utils.NewRefCommit(git.RefNameFromCommit(commit.ID.String()), commit), treePaths)
301302
if err != nil {
302303
return nil, err
303304
}

0 commit comments

Comments
 (0)