Skip to content

Commit

Permalink
Lfs (go-gitea#12)
Browse files Browse the repository at this point in the history
* refactor list files

* check if the file is lfs
  • Loading branch information
zengchen1024 authored Dec 27, 2023
1 parent 002b35d commit 0039401
Show file tree
Hide file tree
Showing 6 changed files with 366 additions and 311 deletions.
36 changes: 36 additions & 0 deletions modules/structs/px_repo_file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package structs

import "time"

// CommitContentsResponse contains information about a repo's entry's (dir, file, symlink, submodule) metadata and content and commit
type CommitContentsResponse struct {
URL *string `json:"url"`
GitURL *string `json:"git_url"`
HTMLURL *string `json:"html_url"`
DownloadURL *string `json:"download_url"`

Name string `json:"name"`
Path string `json:"path"`
SHA string `json:"sha"`
LastCommitSHA string `json:"last_commit_sha"`
LastCommitMessage string `json:"last_commit_message"`
LastCommitCreate time.Time `json:"last_commit_create"`

// `type` will be `file`, `dir`, `symlink`, or `submodule`
Type string `json:"type"`
Size int64 `json:"size"`
IsLFS bool `json:"is_lfs"`

// `encoding` is populated when `type` is `file`, otherwise null
Encoding *string `json:"encoding"`

// `content` is populated when `type` is `file`, otherwise null
Content *string `json:"content"`

// `target` is populated when `type` is `symlink`, otherwise null
Target *string `json:"target"`

// `submodule_git_url` is populated when `type` is `submodule`, otherwise null
SubmoduleGitURL *string `json:"submodule_git_url"`
Links *FileLinksResponse `json:"_links"`
}
28 changes: 0 additions & 28 deletions modules/structs/repo_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

package structs

import "time"

// FileOptions options for all file APIs
type FileOptions struct {
// message (optional) for the commit of this file. if not supplied, a default message will be used
Expand Down Expand Up @@ -141,32 +139,6 @@ type ContentsResponse struct {
Links *FileLinksResponse `json:"_links"`
}

// CommitContentsResponse contains information about a repo's entry's (dir, file, symlink, submodule) metadata and content and commit
type CommitContentsResponse struct {
Name string `json:"name"`
Path string `json:"path"`
SHA string `json:"sha"`
LastCommitSHA string `json:"last_commit_sha"`
LastCommitMessage string `json:"last_commit_message"`
LastCommitCreate time.Time `json:"last_commit_create"`
// `type` will be `file`, `dir`, `symlink`, or `submodule`
Type string `json:"type"`
Size int64 `json:"size"`
// `encoding` is populated when `type` is `file`, otherwise null
Encoding *string `json:"encoding"`
// `content` is populated when `type` is `file`, otherwise null
Content *string `json:"content"`
// `target` is populated when `type` is `symlink`, otherwise null
Target *string `json:"target"`
URL *string `json:"url"`
HTMLURL *string `json:"html_url"`
GitURL *string `json:"git_url"`
DownloadURL *string `json:"download_url"`
// `submodule_git_url` is populated when `type` is `submodule`, otherwise null
SubmoduleGitURL *string `json:"submodule_git_url"`
Links *FileLinksResponse `json:"_links"`
}

// FileCommitResponse contains information generated from a Git commit for a repo's file.
type FileCommitResponse struct {
CommitMeta
Expand Down
87 changes: 0 additions & 87 deletions routers/api/v1/repo/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -974,90 +974,3 @@ func GetContentsList(ctx *context.APIContext) {
// same as GetContents(), this function is here because swagger fails if path is empty in GetContents() interface
GetContents(ctx)
}

// 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
func GetCommitsContents(ctx *context.APIContext) {
// swagger:operation GET /repos/{owner}/{repo}/commit_contents/commits repository repoGetContentsList
// ---
// summary: Gets the metadata of all the entries of the root dir
// produces:
// - application/json
// parameters:
// - name: owner
// in: path
// description: owner of the repo
// type: string
// required: true
// - name: repo
// in: path
// description: name of the repo
// type: string
// required: true
// - name: ref
// in: query
// description: "The name of the commit/branch/tag. Default the repository’s default branch (usually master)"
// type: string
// required: false
// responses:
// "200":
// "$ref": "#/responses/ContentsListResponse"
// "404":
// "$ref": "#/responses/notFound"

// same as GetContents(), this function is here because swagger fails if path is empty in GetContents() interface
if !canReadFiles(ctx.Repo) {
ctx.Error(http.StatusInternalServerError, "GetContentsOrList", repo_model.ErrUserDoesNotHaveAccessToRepo{
UserID: ctx.Doer.ID,
RepoName: ctx.Repo.Repository.LowerName,
})
return
}

treePath := ctx.Params("*")
ref := ctx.FormTrim("ref")

if fileList, err := files_service.GetCommitContentsOrList(ctx, ctx.Repo.Repository, treePath, ref); err != nil {
if git.IsErrNotExist(err) {
ctx.NotFound("GetContentsOrList", err)
return
}
ctx.Error(http.StatusInternalServerError, "GetContentsOrList", err)
} else {
ctx.JSON(http.StatusOK, fileList)
}

}

// GetCommitsContentsList Get the metadata (include commit information) of all the entries of the root dir
func GetCommitsContentsList(ctx *context.APIContext) {
// swagger:operation GET /repos/{owner}/{repo}/commit_contents repository repoGetContentsList
// ---
// summary: Gets the metadata of all the entries of the root dir
// produces:
// - application/json
// parameters:
// - name: owner
// in: path
// description: owner of the repo
// type: string
// required: true
// - name: repo
// in: path
// description: name of the repo
// type: string
// required: true
// - name: ref
// in: query
// description: "The name of the commit/branch/tag. Default the repository’s default branch (usually master)"
// type: string
// required: false
// responses:
// "200":
// "$ref": "#/responses/ContentsListResponse"
// "404":
// "$ref": "#/responses/notFound"

// same as GetContents(), this function is here because swagger fails if path is empty in GetContents() interface
GetCommitsContents(ctx)

}
97 changes: 97 additions & 0 deletions routers/api/v1/repo/px_file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package repo

import (
"net/http"

repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/git"
files_service "code.gitea.io/gitea/services/repository/files"
)

// 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
func GetCommitsContents(ctx *context.APIContext) {
// swagger:operation GET /repos/{owner}/{repo}/commit_contents/commits repository repoGetContentsList
// ---
// summary: Gets the metadata of all the entries of the root dir
// produces:
// - application/json
// parameters:
// - name: owner
// in: path
// description: owner of the repo
// type: string
// required: true
// - name: repo
// in: path
// description: name of the repo
// type: string
// required: true
// - name: ref
// in: query
// description: "The name of the commit/branch/tag. Default the repository’s default branch (usually master)"
// type: string
// required: false
// responses:
// "200":
// "$ref": "#/responses/ContentsListResponse"
// "404":
// "$ref": "#/responses/notFound"

// same as GetContents(), this function is here because swagger fails if path is empty in GetContents() interface
if !canReadFiles(ctx.Repo) {
ctx.Error(http.StatusInternalServerError, "GetContentsOrList", repo_model.ErrUserDoesNotHaveAccessToRepo{
UserID: ctx.Doer.ID,
RepoName: ctx.Repo.Repository.LowerName,
})
return
}

treePath := ctx.Params("*")
ref := ctx.FormTrim("ref")

if fileList, err := files_service.GetCommitContentsOrList(ctx, ctx.Repo.Repository, treePath, ref); err != nil {
if git.IsErrNotExist(err) {
ctx.NotFound("GetContentsOrList", err)
return
}
ctx.Error(http.StatusInternalServerError, "GetContentsOrList", err)
} else {
ctx.JSON(http.StatusOK, fileList)
}

}

// GetCommitsContentsList Get the metadata (include commit information) of all the entries of the root dir
func GetCommitsContentsList(ctx *context.APIContext) {
// swagger:operation GET /repos/{owner}/{repo}/commit_contents repository repoGetContentsList
// ---
// summary: Gets the metadata of all the entries of the root dir
// produces:
// - application/json
// parameters:
// - name: owner
// in: path
// description: owner of the repo
// type: string
// required: true
// - name: repo
// in: path
// description: name of the repo
// type: string
// required: true
// - name: ref
// in: query
// description: "The name of the commit/branch/tag. Default the repository’s default branch (usually master)"
// type: string
// required: false
// responses:
// "200":
// "$ref": "#/responses/ContentsListResponse"
// "404":
// "$ref": "#/responses/notFound"

// same as GetContents(), this function is here because swagger fails if path is empty in GetContents() interface
GetCommitsContents(ctx)

}
Loading

0 comments on commit 0039401

Please sign in to comment.