2019-04-18 00:06:35 +08:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-28 02:20:29 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-04-18 00:06:35 +08:00
|
|
|
|
2021-11-24 15:56:24 +08:00
|
|
|
package files
|
2019-04-18 00:06:35 +08:00
|
|
|
|
|
|
|
import (
|
2022-01-20 07:26:57 +08:00
|
|
|
"context"
|
2019-06-30 04:51:10 +08:00
|
|
|
"fmt"
|
2019-04-18 00:06:35 +08:00
|
|
|
"net/url"
|
2019-06-30 04:51:10 +08:00
|
|
|
"path"
|
|
|
|
"strings"
|
2019-04-18 00:06:35 +08:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
2021-12-10 09:27:50 +08:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2019-04-18 00:06:35 +08:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
2021-11-24 15:56:24 +08:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-05-11 18:21:34 +08:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2019-04-18 00:06:35 +08:00
|
|
|
)
|
|
|
|
|
2019-06-30 04:51:10 +08:00
|
|
|
// ContentType repo content type
|
|
|
|
type ContentType string
|
|
|
|
|
|
|
|
// The string representations of different content types
|
|
|
|
const (
|
|
|
|
// ContentTypeRegular regular content type (file)
|
|
|
|
ContentTypeRegular ContentType = "file"
|
|
|
|
// ContentTypeDir dir content type (dir)
|
|
|
|
ContentTypeDir ContentType = "dir"
|
|
|
|
// ContentLink link content type (symlink)
|
|
|
|
ContentTypeLink ContentType = "symlink"
|
|
|
|
// ContentTag submodule content type (submodule)
|
|
|
|
ContentTypeSubmodule ContentType = "submodule"
|
|
|
|
)
|
|
|
|
|
|
|
|
// String gets the string of ContentType
|
|
|
|
func (ct *ContentType) String() string {
|
|
|
|
return string(*ct)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetContentsOrList gets the meta data of a file's contents (*ContentsResponse) if treePath not a tree
|
|
|
|
// directory, otherwise a listing of file contents ([]*ContentsResponse). Ref can be a branch, commit or tag
|
2022-01-20 07:26:57 +08:00
|
|
|
func GetContentsOrList(ctx context.Context, repo *repo_model.Repository, treePath, ref string) (interface{}, error) {
|
2019-10-19 23:38:49 +08:00
|
|
|
if repo.IsEmpty {
|
|
|
|
return make([]interface{}, 0), nil
|
|
|
|
}
|
2019-04-18 00:06:35 +08:00
|
|
|
if ref == "" {
|
|
|
|
ref = repo.DefaultBranch
|
|
|
|
}
|
2019-06-30 04:51:10 +08:00
|
|
|
origRef := ref
|
2019-04-18 00:06:35 +08:00
|
|
|
|
|
|
|
// Check that the path given in opts.treePath is valid (not a git path)
|
2019-06-30 04:51:10 +08:00
|
|
|
cleanTreePath := CleanUploadFileName(treePath)
|
|
|
|
if cleanTreePath == "" && treePath != "" {
|
2019-04-18 00:06:35 +08:00
|
|
|
return nil, models.ErrFilenameInvalid{
|
|
|
|
Path: treePath,
|
|
|
|
}
|
|
|
|
}
|
2019-06-30 04:51:10 +08:00
|
|
|
treePath = cleanTreePath
|
2019-04-18 00:06:35 +08:00
|
|
|
|
2022-01-20 07:26:57 +08:00
|
|
|
gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath())
|
2019-04-18 00:06:35 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-20 07:26:57 +08:00
|
|
|
defer closer.Close()
|
2019-04-18 00:06:35 +08:00
|
|
|
|
|
|
|
// Get the commit object for the ref
|
|
|
|
commit, err := gitRepo.GetCommit(ref)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
entry, err := commit.GetTreeEntryByPath(treePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-06-30 04:51:10 +08:00
|
|
|
if entry.Type() != "tree" {
|
2022-01-20 07:26:57 +08:00
|
|
|
return GetContents(ctx, repo, treePath, origRef, false)
|
2019-06-30 04:51:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// We are in a directory, so we return a list of FileContentResponse objects
|
|
|
|
var fileList []*api.ContentsResponse
|
|
|
|
|
|
|
|
gitTree, err := commit.SubTree(treePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
entries, err := gitTree.ListEntries()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, e := range entries {
|
|
|
|
subTreePath := path.Join(treePath, e.Name())
|
2022-01-20 07:26:57 +08:00
|
|
|
fileContentResponse, err := GetContents(ctx, repo, subTreePath, origRef, true)
|
2019-06-30 04:51:10 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fileList = append(fileList, fileContentResponse)
|
|
|
|
}
|
|
|
|
return fileList, nil
|
|
|
|
}
|
|
|
|
|
2022-07-22 03:18:41 +08:00
|
|
|
// GetObjectTypeFromTreeEntry check what content is behind it
|
|
|
|
func GetObjectTypeFromTreeEntry(entry *git.TreeEntry) ContentType {
|
|
|
|
switch {
|
|
|
|
case entry.IsDir():
|
|
|
|
return ContentTypeDir
|
|
|
|
case entry.IsSubModule():
|
|
|
|
return ContentTypeSubmodule
|
|
|
|
case entry.IsExecutable(), entry.IsRegular():
|
|
|
|
return ContentTypeRegular
|
|
|
|
case entry.IsLink():
|
|
|
|
return ContentTypeLink
|
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-30 04:51:10 +08:00
|
|
|
// GetContents gets the meta data on a file's contents. Ref can be a branch, commit or tag
|
2022-01-20 07:26:57 +08:00
|
|
|
func GetContents(ctx context.Context, repo *repo_model.Repository, treePath, ref string, forList bool) (*api.ContentsResponse, error) {
|
2019-06-30 04:51:10 +08:00
|
|
|
if ref == "" {
|
|
|
|
ref = repo.DefaultBranch
|
2019-04-18 00:06:35 +08:00
|
|
|
}
|
2019-06-30 04:51:10 +08:00
|
|
|
origRef := ref
|
2019-04-18 00:06:35 +08:00
|
|
|
|
2019-06-30 04:51:10 +08:00
|
|
|
// Check that the path given in opts.treePath is valid (not a git path)
|
|
|
|
cleanTreePath := CleanUploadFileName(treePath)
|
|
|
|
if cleanTreePath == "" && treePath != "" {
|
|
|
|
return nil, models.ErrFilenameInvalid{
|
|
|
|
Path: treePath,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
treePath = cleanTreePath
|
|
|
|
|
2022-01-20 07:26:57 +08:00
|
|
|
gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath())
|
2019-06-30 04:51:10 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-20 07:26:57 +08:00
|
|
|
defer closer.Close()
|
2019-04-18 00:06:35 +08:00
|
|
|
|
2019-06-30 04:51:10 +08:00
|
|
|
// Get the commit object for the ref
|
|
|
|
commit, err := gitRepo.GetCommit(ref)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
commitID := commit.ID.String()
|
|
|
|
if len(ref) >= 4 && strings.HasPrefix(commitID, ref) {
|
|
|
|
ref = commit.ID.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
entry, err := commit.GetTreeEntryByPath(treePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
refType := gitRepo.GetRefType(ref)
|
|
|
|
if refType == "invalid" {
|
|
|
|
return nil, fmt.Errorf("no commit found for the ref [ref: %s]", ref)
|
|
|
|
}
|
|
|
|
|
|
|
|
selfURL, err := url.Parse(fmt.Sprintf("%s/contents/%s?ref=%s", repo.APIURL(), treePath, origRef))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
selfURLString := selfURL.String()
|
|
|
|
|
2022-07-30 16:09:04 +08:00
|
|
|
err = gitRepo.AddLastCommitCache(repo.GetCommitsCountCacheKey(ref, refType != git.ObjectCommit), repo.FullName(), commitID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
lastCommit, err := commit.GetCommitByPath(treePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-06-30 04:51:10 +08:00
|
|
|
// All content types have these fields in populated
|
|
|
|
contentsResponse := &api.ContentsResponse{
|
2022-07-30 16:09:04 +08:00
|
|
|
Name: entry.Name(),
|
|
|
|
Path: treePath,
|
|
|
|
SHA: entry.ID.String(),
|
|
|
|
LastCommitSHA: lastCommit.ID.String(),
|
|
|
|
Size: entry.Size(),
|
|
|
|
URL: &selfURLString,
|
2019-04-18 00:06:35 +08:00
|
|
|
Links: &api.FileLinksResponse{
|
2019-06-30 04:51:10 +08:00
|
|
|
Self: &selfURLString,
|
2019-04-18 00:06:35 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-06-30 04:51:10 +08:00
|
|
|
// Now populate the rest of the ContentsResponse based on entry type
|
2020-04-25 00:20:22 +08:00
|
|
|
if entry.IsRegular() || entry.IsExecutable() {
|
2019-06-30 04:51:10 +08:00
|
|
|
contentsResponse.Type = string(ContentTypeRegular)
|
2022-04-21 23:17:57 +08:00
|
|
|
if blobResponse, err := GetBlobBySHA(ctx, repo, gitRepo, entry.ID.String()); err != nil {
|
2019-06-30 04:51:10 +08:00
|
|
|
return nil, err
|
|
|
|
} else if !forList {
|
|
|
|
// We don't show the content if we are getting a list of FileContentResponses
|
|
|
|
contentsResponse.Encoding = &blobResponse.Encoding
|
|
|
|
contentsResponse.Content = &blobResponse.Content
|
|
|
|
}
|
|
|
|
} else if entry.IsDir() {
|
|
|
|
contentsResponse.Type = string(ContentTypeDir)
|
|
|
|
} else if entry.IsLink() {
|
|
|
|
contentsResponse.Type = string(ContentTypeLink)
|
|
|
|
// The target of a symlink file is the content of the file
|
|
|
|
targetFromContent, err := entry.Blob().GetBlobContent()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
contentsResponse.Target = &targetFromContent
|
|
|
|
} else if entry.IsSubModule() {
|
|
|
|
contentsResponse.Type = string(ContentTypeSubmodule)
|
|
|
|
submodule, err := commit.GetSubModule(treePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
contentsResponse.SubmoduleGitURL = &submodule.URL
|
|
|
|
}
|
|
|
|
// Handle links
|
|
|
|
if entry.IsRegular() || entry.IsLink() {
|
|
|
|
downloadURL, err := url.Parse(fmt.Sprintf("%s/raw/%s/%s/%s", repo.HTMLURL(), refType, ref, treePath))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
downloadURLString := downloadURL.String()
|
|
|
|
contentsResponse.DownloadURL = &downloadURLString
|
|
|
|
}
|
|
|
|
if !entry.IsSubModule() {
|
|
|
|
htmlURL, err := url.Parse(fmt.Sprintf("%s/src/%s/%s/%s", repo.HTMLURL(), refType, ref, treePath))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
htmlURLString := htmlURL.String()
|
|
|
|
contentsResponse.HTMLURL = &htmlURLString
|
|
|
|
contentsResponse.Links.HTMLURL = &htmlURLString
|
|
|
|
|
|
|
|
gitURL, err := url.Parse(fmt.Sprintf("%s/git/blobs/%s", repo.APIURL(), entry.ID.String()))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
gitURLString := gitURL.String()
|
|
|
|
contentsResponse.GitURL = &gitURLString
|
|
|
|
contentsResponse.Links.GitURL = &gitURLString
|
|
|
|
}
|
|
|
|
|
|
|
|
return contentsResponse, nil
|
2019-04-18 00:06:35 +08:00
|
|
|
}
|
2021-11-24 15:56:24 +08:00
|
|
|
|
|
|
|
// GetBlobBySHA get the GitBlobResponse of a repository using a sha hash.
|
2022-04-21 23:17:57 +08:00
|
|
|
func GetBlobBySHA(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, sha string) (*api.GitBlobResponse, error) {
|
2021-11-24 15:56:24 +08:00
|
|
|
gitBlob, err := gitRepo.GetBlob(sha)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
content := ""
|
|
|
|
if gitBlob.Size() <= setting.API.DefaultMaxBlobSize {
|
|
|
|
content, err = gitBlob.GetBlobContentBase64()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &api.GitBlobResponse{
|
|
|
|
SHA: gitBlob.ID.String(),
|
|
|
|
URL: repo.APIURL() + "/git/blobs/" + url.PathEscape(gitBlob.ID.String()),
|
|
|
|
Size: gitBlob.Size(),
|
|
|
|
Encoding: "base64",
|
|
|
|
Content: content,
|
|
|
|
}, nil
|
|
|
|
}
|