Skip to content

Commit 0039401

Browse files
authored
* refactor list files * check if the file is lfs
1 parent 002b35d commit 0039401

File tree

6 files changed

+366
-311
lines changed

6 files changed

+366
-311
lines changed

modules/structs/px_repo_file.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package structs
2+
3+
import "time"
4+
5+
// CommitContentsResponse contains information about a repo's entry's (dir, file, symlink, submodule) metadata and content and commit
6+
type CommitContentsResponse struct {
7+
URL *string `json:"url"`
8+
GitURL *string `json:"git_url"`
9+
HTMLURL *string `json:"html_url"`
10+
DownloadURL *string `json:"download_url"`
11+
12+
Name string `json:"name"`
13+
Path string `json:"path"`
14+
SHA string `json:"sha"`
15+
LastCommitSHA string `json:"last_commit_sha"`
16+
LastCommitMessage string `json:"last_commit_message"`
17+
LastCommitCreate time.Time `json:"last_commit_create"`
18+
19+
// `type` will be `file`, `dir`, `symlink`, or `submodule`
20+
Type string `json:"type"`
21+
Size int64 `json:"size"`
22+
IsLFS bool `json:"is_lfs"`
23+
24+
// `encoding` is populated when `type` is `file`, otherwise null
25+
Encoding *string `json:"encoding"`
26+
27+
// `content` is populated when `type` is `file`, otherwise null
28+
Content *string `json:"content"`
29+
30+
// `target` is populated when `type` is `symlink`, otherwise null
31+
Target *string `json:"target"`
32+
33+
// `submodule_git_url` is populated when `type` is `submodule`, otherwise null
34+
SubmoduleGitURL *string `json:"submodule_git_url"`
35+
Links *FileLinksResponse `json:"_links"`
36+
}

modules/structs/repo_file.go

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
package structs
66

7-
import "time"
8-
97
// FileOptions options for all file APIs
108
type FileOptions struct {
119
// message (optional) for the commit of this file. if not supplied, a default message will be used
@@ -141,32 +139,6 @@ type ContentsResponse struct {
141139
Links *FileLinksResponse `json:"_links"`
142140
}
143141

144-
// CommitContentsResponse contains information about a repo's entry's (dir, file, symlink, submodule) metadata and content and commit
145-
type CommitContentsResponse struct {
146-
Name string `json:"name"`
147-
Path string `json:"path"`
148-
SHA string `json:"sha"`
149-
LastCommitSHA string `json:"last_commit_sha"`
150-
LastCommitMessage string `json:"last_commit_message"`
151-
LastCommitCreate time.Time `json:"last_commit_create"`
152-
// `type` will be `file`, `dir`, `symlink`, or `submodule`
153-
Type string `json:"type"`
154-
Size int64 `json:"size"`
155-
// `encoding` is populated when `type` is `file`, otherwise null
156-
Encoding *string `json:"encoding"`
157-
// `content` is populated when `type` is `file`, otherwise null
158-
Content *string `json:"content"`
159-
// `target` is populated when `type` is `symlink`, otherwise null
160-
Target *string `json:"target"`
161-
URL *string `json:"url"`
162-
HTMLURL *string `json:"html_url"`
163-
GitURL *string `json:"git_url"`
164-
DownloadURL *string `json:"download_url"`
165-
// `submodule_git_url` is populated when `type` is `submodule`, otherwise null
166-
SubmoduleGitURL *string `json:"submodule_git_url"`
167-
Links *FileLinksResponse `json:"_links"`
168-
}
169-
170142
// FileCommitResponse contains information generated from a Git commit for a repo's file.
171143
type FileCommitResponse struct {
172144
CommitMeta

routers/api/v1/repo/file.go

Lines changed: 0 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -974,90 +974,3 @@ func GetContentsList(ctx *context.APIContext) {
974974
// same as GetContents(), this function is here because swagger fails if path is empty in GetContents() interface
975975
GetContents(ctx)
976976
}
977-
978-
// GetCommitsContents Get the metadata and commit and contents (if a file) of an entry in a repository, or a list of entries if a dir
979-
func GetCommitsContents(ctx *context.APIContext) {
980-
// swagger:operation GET /repos/{owner}/{repo}/commit_contents/commits repository repoGetContentsList
981-
// ---
982-
// summary: Gets the metadata of all the entries of the root dir
983-
// produces:
984-
// - application/json
985-
// parameters:
986-
// - name: owner
987-
// in: path
988-
// description: owner of the repo
989-
// type: string
990-
// required: true
991-
// - name: repo
992-
// in: path
993-
// description: name of the repo
994-
// type: string
995-
// required: true
996-
// - name: ref
997-
// in: query
998-
// description: "The name of the commit/branch/tag. Default the repository’s default branch (usually master)"
999-
// type: string
1000-
// required: false
1001-
// responses:
1002-
// "200":
1003-
// "$ref": "#/responses/ContentsListResponse"
1004-
// "404":
1005-
// "$ref": "#/responses/notFound"
1006-
1007-
// same as GetContents(), this function is here because swagger fails if path is empty in GetContents() interface
1008-
if !canReadFiles(ctx.Repo) {
1009-
ctx.Error(http.StatusInternalServerError, "GetContentsOrList", repo_model.ErrUserDoesNotHaveAccessToRepo{
1010-
UserID: ctx.Doer.ID,
1011-
RepoName: ctx.Repo.Repository.LowerName,
1012-
})
1013-
return
1014-
}
1015-
1016-
treePath := ctx.Params("*")
1017-
ref := ctx.FormTrim("ref")
1018-
1019-
if fileList, err := files_service.GetCommitContentsOrList(ctx, ctx.Repo.Repository, treePath, ref); err != nil {
1020-
if git.IsErrNotExist(err) {
1021-
ctx.NotFound("GetContentsOrList", err)
1022-
return
1023-
}
1024-
ctx.Error(http.StatusInternalServerError, "GetContentsOrList", err)
1025-
} else {
1026-
ctx.JSON(http.StatusOK, fileList)
1027-
}
1028-
1029-
}
1030-
1031-
// GetCommitsContentsList Get the metadata (include commit information) of all the entries of the root dir
1032-
func GetCommitsContentsList(ctx *context.APIContext) {
1033-
// swagger:operation GET /repos/{owner}/{repo}/commit_contents repository repoGetContentsList
1034-
// ---
1035-
// summary: Gets the metadata of all the entries of the root dir
1036-
// produces:
1037-
// - application/json
1038-
// parameters:
1039-
// - name: owner
1040-
// in: path
1041-
// description: owner of the repo
1042-
// type: string
1043-
// required: true
1044-
// - name: repo
1045-
// in: path
1046-
// description: name of the repo
1047-
// type: string
1048-
// required: true
1049-
// - name: ref
1050-
// in: query
1051-
// description: "The name of the commit/branch/tag. Default the repository’s default branch (usually master)"
1052-
// type: string
1053-
// required: false
1054-
// responses:
1055-
// "200":
1056-
// "$ref": "#/responses/ContentsListResponse"
1057-
// "404":
1058-
// "$ref": "#/responses/notFound"
1059-
1060-
// same as GetContents(), this function is here because swagger fails if path is empty in GetContents() interface
1061-
GetCommitsContents(ctx)
1062-
1063-
}

routers/api/v1/repo/px_file.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package repo
2+
3+
import (
4+
"net/http"
5+
6+
repo_model "code.gitea.io/gitea/models/repo"
7+
"code.gitea.io/gitea/modules/context"
8+
"code.gitea.io/gitea/modules/git"
9+
files_service "code.gitea.io/gitea/services/repository/files"
10+
)
11+
12+
// GetCommitsContents Get the metadata and commit and contents (if a file) of an entry in a repository, or a list of entries if a dir
13+
func GetCommitsContents(ctx *context.APIContext) {
14+
// swagger:operation GET /repos/{owner}/{repo}/commit_contents/commits repository repoGetContentsList
15+
// ---
16+
// summary: Gets the metadata of all the entries of the root dir
17+
// produces:
18+
// - application/json
19+
// parameters:
20+
// - name: owner
21+
// in: path
22+
// description: owner of the repo
23+
// type: string
24+
// required: true
25+
// - name: repo
26+
// in: path
27+
// description: name of the repo
28+
// type: string
29+
// required: true
30+
// - name: ref
31+
// in: query
32+
// description: "The name of the commit/branch/tag. Default the repository’s default branch (usually master)"
33+
// type: string
34+
// required: false
35+
// responses:
36+
// "200":
37+
// "$ref": "#/responses/ContentsListResponse"
38+
// "404":
39+
// "$ref": "#/responses/notFound"
40+
41+
// same as GetContents(), this function is here because swagger fails if path is empty in GetContents() interface
42+
if !canReadFiles(ctx.Repo) {
43+
ctx.Error(http.StatusInternalServerError, "GetContentsOrList", repo_model.ErrUserDoesNotHaveAccessToRepo{
44+
UserID: ctx.Doer.ID,
45+
RepoName: ctx.Repo.Repository.LowerName,
46+
})
47+
return
48+
}
49+
50+
treePath := ctx.Params("*")
51+
ref := ctx.FormTrim("ref")
52+
53+
if fileList, err := files_service.GetCommitContentsOrList(ctx, ctx.Repo.Repository, treePath, ref); err != nil {
54+
if git.IsErrNotExist(err) {
55+
ctx.NotFound("GetContentsOrList", err)
56+
return
57+
}
58+
ctx.Error(http.StatusInternalServerError, "GetContentsOrList", err)
59+
} else {
60+
ctx.JSON(http.StatusOK, fileList)
61+
}
62+
63+
}
64+
65+
// GetCommitsContentsList Get the metadata (include commit information) of all the entries of the root dir
66+
func GetCommitsContentsList(ctx *context.APIContext) {
67+
// swagger:operation GET /repos/{owner}/{repo}/commit_contents repository repoGetContentsList
68+
// ---
69+
// summary: Gets the metadata of all the entries of the root dir
70+
// produces:
71+
// - application/json
72+
// parameters:
73+
// - name: owner
74+
// in: path
75+
// description: owner of the repo
76+
// type: string
77+
// required: true
78+
// - name: repo
79+
// in: path
80+
// description: name of the repo
81+
// type: string
82+
// required: true
83+
// - name: ref
84+
// in: query
85+
// description: "The name of the commit/branch/tag. Default the repository’s default branch (usually master)"
86+
// type: string
87+
// required: false
88+
// responses:
89+
// "200":
90+
// "$ref": "#/responses/ContentsListResponse"
91+
// "404":
92+
// "$ref": "#/responses/notFound"
93+
94+
// same as GetContents(), this function is here because swagger fails if path is empty in GetContents() interface
95+
GetCommitsContents(ctx)
96+
97+
}

0 commit comments

Comments
 (0)