2019-04-17 21:36:35 +05:30
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2019-04-17 21:36:35 +05:30
|
|
|
|
2021-11-24 13:26:24 +05:30
|
|
|
package files
|
2019-04-17 21:36:35 +05:30
|
|
|
|
|
|
|
import (
|
2022-01-20 04:56:57 +05:30
|
|
|
"context"
|
2019-06-30 02:21:10 +05:30
|
|
|
"fmt"
|
2019-04-17 21:36:35 +05:30
|
|
|
"net/url"
|
2019-06-30 02:21:10 +05:30
|
|
|
"path"
|
|
|
|
"strings"
|
2019-04-17 21:36:35 +05:30
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
2021-12-10 06:57:50 +05:30
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2019-04-17 21:36:35 +05:30
|
|
|
"code.gitea.io/gitea/modules/git"
|
Simplify how git repositories are opened (#28937)
## Purpose
This is a refactor toward building an abstraction over managing git
repositories.
Afterwards, it does not matter anymore if they are stored on the local
disk or somewhere remote.
## What this PR changes
We used `git.OpenRepository` everywhere previously.
Now, we should split them into two distinct functions:
Firstly, there are temporary repositories which do not change:
```go
git.OpenRepository(ctx, diskPath)
```
Gitea managed repositories having a record in the database in the
`repository` table are moved into the new package `gitrepo`:
```go
gitrepo.OpenRepository(ctx, repo_model.Repo)
```
Why is `repo_model.Repository` the second parameter instead of file
path?
Because then we can easily adapt our repository storage strategy.
The repositories can be stored locally, however, they could just as well
be stored on a remote server.
## Further changes in other PRs
- A Git Command wrapper on package `gitrepo` could be created. i.e.
`NewCommand(ctx, repo_model.Repository, commands...)`. `git.RunOpts{Dir:
repo.RepoPath()}`, the directory should be empty before invoking this
method and it can be filled in the function only. #28940
- Remove the `RepoPath()`/`WikiPath()` functions to reduce the
possibility of mistakes.
---------
Co-authored-by: delvh <dev.lh@web.de>
2024-01-28 01:39:51 +05:30
|
|
|
"code.gitea.io/gitea/modules/gitrepo"
|
2021-11-24 13:26:24 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-05-11 15:51:34 +05:30
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2023-02-12 07:01:14 +05:30
|
|
|
"code.gitea.io/gitea/modules/util"
|
2019-04-17 21:36:35 +05:30
|
|
|
)
|
|
|
|
|
2019-06-30 02:21:10 +05:30
|
|
|
// 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
|
2023-07-05 00:06:08 +05:30
|
|
|
func GetContentsOrList(ctx context.Context, repo *repo_model.Repository, treePath, ref string) (any, error) {
|
2019-10-19 21:08:49 +05:30
|
|
|
if repo.IsEmpty {
|
2023-07-05 00:06:08 +05:30
|
|
|
return make([]any, 0), nil
|
2019-10-19 21:08:49 +05:30
|
|
|
}
|
2019-04-17 21:36:35 +05:30
|
|
|
if ref == "" {
|
|
|
|
ref = repo.DefaultBranch
|
|
|
|
}
|
2019-06-30 02:21:10 +05:30
|
|
|
origRef := ref
|
2019-04-17 21:36:35 +05:30
|
|
|
|
|
|
|
// Check that the path given in opts.treePath is valid (not a git path)
|
2019-06-30 02:21:10 +05:30
|
|
|
cleanTreePath := CleanUploadFileName(treePath)
|
|
|
|
if cleanTreePath == "" && treePath != "" {
|
2019-04-17 21:36:35 +05:30
|
|
|
return nil, models.ErrFilenameInvalid{
|
|
|
|
Path: treePath,
|
|
|
|
}
|
|
|
|
}
|
2019-06-30 02:21:10 +05:30
|
|
|
treePath = cleanTreePath
|
2019-04-17 21:36:35 +05:30
|
|
|
|
Simplify how git repositories are opened (#28937)
## Purpose
This is a refactor toward building an abstraction over managing git
repositories.
Afterwards, it does not matter anymore if they are stored on the local
disk or somewhere remote.
## What this PR changes
We used `git.OpenRepository` everywhere previously.
Now, we should split them into two distinct functions:
Firstly, there are temporary repositories which do not change:
```go
git.OpenRepository(ctx, diskPath)
```
Gitea managed repositories having a record in the database in the
`repository` table are moved into the new package `gitrepo`:
```go
gitrepo.OpenRepository(ctx, repo_model.Repo)
```
Why is `repo_model.Repository` the second parameter instead of file
path?
Because then we can easily adapt our repository storage strategy.
The repositories can be stored locally, however, they could just as well
be stored on a remote server.
## Further changes in other PRs
- A Git Command wrapper on package `gitrepo` could be created. i.e.
`NewCommand(ctx, repo_model.Repository, commands...)`. `git.RunOpts{Dir:
repo.RepoPath()}`, the directory should be empty before invoking this
method and it can be filled in the function only. #28940
- Remove the `RepoPath()`/`WikiPath()` functions to reduce the
possibility of mistakes.
---------
Co-authored-by: delvh <dev.lh@web.de>
2024-01-28 01:39:51 +05:30
|
|
|
gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, repo)
|
2019-04-17 21:36:35 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-20 04:56:57 +05:30
|
|
|
defer closer.Close()
|
2019-04-17 21:36:35 +05:30
|
|
|
|
|
|
|
// 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 02:21:10 +05:30
|
|
|
if entry.Type() != "tree" {
|
2022-01-20 04:56:57 +05:30
|
|
|
return GetContents(ctx, repo, treePath, origRef, false)
|
2019-06-30 02:21:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// 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 04:56:57 +05:30
|
|
|
fileContentResponse, err := GetContents(ctx, repo, subTreePath, origRef, true)
|
2019-06-30 02:21:10 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fileList = append(fileList, fileContentResponse)
|
|
|
|
}
|
|
|
|
return fileList, nil
|
|
|
|
}
|
|
|
|
|
2022-07-22 00:48:41 +05:30
|
|
|
// 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 02:21:10 +05:30
|
|
|
// GetContents gets the meta data on a file's contents. Ref can be a branch, commit or tag
|
2022-01-20 04:56:57 +05:30
|
|
|
func GetContents(ctx context.Context, repo *repo_model.Repository, treePath, ref string, forList bool) (*api.ContentsResponse, error) {
|
2019-06-30 02:21:10 +05:30
|
|
|
if ref == "" {
|
|
|
|
ref = repo.DefaultBranch
|
2019-04-17 21:36:35 +05:30
|
|
|
}
|
2019-06-30 02:21:10 +05:30
|
|
|
origRef := ref
|
2019-04-17 21:36:35 +05:30
|
|
|
|
2019-06-30 02:21:10 +05:30
|
|
|
// 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
|
|
|
|
|
Simplify how git repositories are opened (#28937)
## Purpose
This is a refactor toward building an abstraction over managing git
repositories.
Afterwards, it does not matter anymore if they are stored on the local
disk or somewhere remote.
## What this PR changes
We used `git.OpenRepository` everywhere previously.
Now, we should split them into two distinct functions:
Firstly, there are temporary repositories which do not change:
```go
git.OpenRepository(ctx, diskPath)
```
Gitea managed repositories having a record in the database in the
`repository` table are moved into the new package `gitrepo`:
```go
gitrepo.OpenRepository(ctx, repo_model.Repo)
```
Why is `repo_model.Repository` the second parameter instead of file
path?
Because then we can easily adapt our repository storage strategy.
The repositories can be stored locally, however, they could just as well
be stored on a remote server.
## Further changes in other PRs
- A Git Command wrapper on package `gitrepo` could be created. i.e.
`NewCommand(ctx, repo_model.Repository, commands...)`. `git.RunOpts{Dir:
repo.RepoPath()}`, the directory should be empty before invoking this
method and it can be filled in the function only. #28940
- Remove the `RepoPath()`/`WikiPath()` functions to reduce the
possibility of mistakes.
---------
Co-authored-by: delvh <dev.lh@web.de>
2024-01-28 01:39:51 +05:30
|
|
|
gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, repo)
|
2019-06-30 02:21:10 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-20 04:56:57 +05:30
|
|
|
defer closer.Close()
|
2019-04-17 21:36:35 +05:30
|
|
|
|
2019-06-30 02:21:10 +05:30
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2023-02-12 07:01:14 +05:30
|
|
|
selfURL, err := url.Parse(repo.APIURL() + "/contents/" + util.PathEscapeSegments(treePath) + "?ref=" + url.QueryEscape(origRef))
|
2019-06-30 02:21:10 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
selfURLString := selfURL.String()
|
|
|
|
|
2022-07-30 13:39:04 +05:30
|
|
|
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 02:21:10 +05:30
|
|
|
// All content types have these fields in populated
|
|
|
|
contentsResponse := &api.ContentsResponse{
|
2022-07-30 13:39:04 +05:30
|
|
|
Name: entry.Name(),
|
|
|
|
Path: treePath,
|
|
|
|
SHA: entry.ID.String(),
|
|
|
|
LastCommitSHA: lastCommit.ID.String(),
|
|
|
|
Size: entry.Size(),
|
|
|
|
URL: &selfURLString,
|
2019-04-17 21:36:35 +05:30
|
|
|
Links: &api.FileLinksResponse{
|
2019-06-30 02:21:10 +05:30
|
|
|
Self: &selfURLString,
|
2019-04-17 21:36:35 +05:30
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-06-30 02:21:10 +05:30
|
|
|
// Now populate the rest of the ContentsResponse based on entry type
|
2020-04-24 21:50:22 +05:30
|
|
|
if entry.IsRegular() || entry.IsExecutable() {
|
2019-06-30 02:21:10 +05:30
|
|
|
contentsResponse.Type = string(ContentTypeRegular)
|
2022-04-21 20:47:57 +05:30
|
|
|
if blobResponse, err := GetBlobBySHA(ctx, repo, gitRepo, entry.ID.String()); err != nil {
|
2019-06-30 02:21:10 +05:30
|
|
|
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
|
2023-06-13 14:32:25 +05:30
|
|
|
targetFromContent, err := entry.Blob().GetBlobContent(1024)
|
2019-06-30 02:21:10 +05:30
|
|
|
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
|
|
|
|
}
|
2023-03-21 03:56:01 +05:30
|
|
|
if submodule != nil && submodule.URL != "" {
|
|
|
|
contentsResponse.SubmoduleGitURL = &submodule.URL
|
|
|
|
}
|
2019-06-30 02:21:10 +05:30
|
|
|
}
|
|
|
|
// Handle links
|
2023-11-28 17:16:34 +05:30
|
|
|
if entry.IsRegular() || entry.IsLink() || entry.IsExecutable() {
|
2023-02-12 07:01:14 +05:30
|
|
|
downloadURL, err := url.Parse(repo.HTMLURL() + "/raw/" + url.PathEscape(string(refType)) + "/" + util.PathEscapeSegments(ref) + "/" + util.PathEscapeSegments(treePath))
|
2019-06-30 02:21:10 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
downloadURLString := downloadURL.String()
|
|
|
|
contentsResponse.DownloadURL = &downloadURLString
|
|
|
|
}
|
|
|
|
if !entry.IsSubModule() {
|
2023-02-12 07:01:14 +05:30
|
|
|
htmlURL, err := url.Parse(repo.HTMLURL() + "/src/" + url.PathEscape(string(refType)) + "/" + util.PathEscapeSegments(ref) + "/" + util.PathEscapeSegments(treePath))
|
2019-06-30 02:21:10 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
htmlURLString := htmlURL.String()
|
|
|
|
contentsResponse.HTMLURL = &htmlURLString
|
|
|
|
contentsResponse.Links.HTMLURL = &htmlURLString
|
|
|
|
|
2023-02-12 07:01:14 +05:30
|
|
|
gitURL, err := url.Parse(repo.APIURL() + "/git/blobs/" + url.PathEscape(entry.ID.String()))
|
2019-06-30 02:21:10 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
gitURLString := gitURL.String()
|
|
|
|
contentsResponse.GitURL = &gitURLString
|
|
|
|
contentsResponse.Links.GitURL = &gitURLString
|
|
|
|
}
|
|
|
|
|
|
|
|
return contentsResponse, nil
|
2019-04-17 21:36:35 +05:30
|
|
|
}
|
2021-11-24 13:26:24 +05:30
|
|
|
|
|
|
|
// GetBlobBySHA get the GitBlobResponse of a repository using a sha hash.
|
2022-04-21 20:47:57 +05:30
|
|
|
func GetBlobBySHA(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, sha string) (*api.GitBlobResponse, error) {
|
2021-11-24 13:26:24 +05:30
|
|
|
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
|
|
|
|
}
|
2024-02-15 00:20:31 +05:30
|
|
|
|
|
|
|
// TryGetContentLanguage tries to get the (linguist) language of the file content
|
|
|
|
func TryGetContentLanguage(gitRepo *git.Repository, commitID, treePath string) (string, error) {
|
2024-03-24 17:14:30 +05:30
|
|
|
attribute, err := gitRepo.GitAttributeFirst(commitID, treePath, "linguist-language", "gitlab-language")
|
|
|
|
return attribute.Prefix(), err
|
2024-02-15 00:20:31 +05:30
|
|
|
}
|