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-04-17 21:36:35 +05:30
|
|
|
|
"fmt"
|
2023-07-18 23:44:47 +05:30
|
|
|
|
"io"
|
2019-04-17 21:36:35 +05:30
|
|
|
|
"path"
|
|
|
|
|
"strings"
|
2019-12-24 08:03:52 +05:30
|
|
|
|
"time"
|
2019-04-17 21:36:35 +05:30
|
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
2022-06-12 21:21:54 +05:30
|
|
|
|
git_model "code.gitea.io/gitea/models/git"
|
2021-12-10 06:57:50 +05:30
|
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-24 15:19:20 +05:30
|
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
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"
|
2019-04-17 21:36:35 +05:30
|
|
|
|
"code.gitea.io/gitea/modules/lfs"
|
2019-04-26 17:30:30 +05:30
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2019-04-17 21:36:35 +05:30
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-05-11 15:51:34 +05:30
|
|
|
|
"code.gitea.io/gitea/modules/structs"
|
2021-12-10 13:44:24 +05:30
|
|
|
|
asymkey_service "code.gitea.io/gitea/services/asymkey"
|
2019-04-17 21:36:35 +05:30
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// IdentityOptions for a person's identity like an author or committer
|
|
|
|
|
type IdentityOptions struct {
|
|
|
|
|
Name string
|
|
|
|
|
Email string
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-24 08:03:52 +05:30
|
|
|
|
// CommitDateOptions store dates for GIT_AUTHOR_DATE and GIT_COMMITTER_DATE
|
|
|
|
|
type CommitDateOptions struct {
|
|
|
|
|
Author time.Time
|
|
|
|
|
Committer time.Time
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-29 15:11:35 +05:30
|
|
|
|
type ChangeRepoFile struct {
|
2023-07-18 23:44:47 +05:30
|
|
|
|
Operation string
|
|
|
|
|
TreePath string
|
|
|
|
|
FromTreePath string
|
2024-02-19 20:20:03 +05:30
|
|
|
|
ContentReader io.ReadSeeker
|
2023-07-18 23:44:47 +05:30
|
|
|
|
SHA string
|
|
|
|
|
Options *RepoFileOptions
|
2023-05-29 15:11:35 +05:30
|
|
|
|
}
|
|
|
|
|
|
2023-05-31 14:37:51 +05:30
|
|
|
|
// ChangeRepoFilesOptions holds the repository files update options
|
2023-05-29 15:11:35 +05:30
|
|
|
|
type ChangeRepoFilesOptions struct {
|
|
|
|
|
LastCommitID string
|
|
|
|
|
OldBranch string
|
|
|
|
|
NewBranch string
|
|
|
|
|
Message string
|
|
|
|
|
Files []*ChangeRepoFile
|
2019-04-17 21:36:35 +05:30
|
|
|
|
Author *IdentityOptions
|
|
|
|
|
Committer *IdentityOptions
|
2019-12-24 08:03:52 +05:30
|
|
|
|
Dates *CommitDateOptions
|
2021-01-29 14:27:45 +05:30
|
|
|
|
Signoff bool
|
2019-04-17 21:36:35 +05:30
|
|
|
|
}
|
|
|
|
|
|
2023-05-29 15:11:35 +05:30
|
|
|
|
type RepoFileOptions struct {
|
|
|
|
|
treePath string
|
|
|
|
|
fromTreePath string
|
|
|
|
|
executable bool
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ChangeRepoFiles adds, updates or removes multiple files in the given repository
|
|
|
|
|
func ChangeRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *user_model.User, opts *ChangeRepoFilesOptions) (*structs.FilesResponse, error) {
|
2023-09-22 05:13:29 +05:30
|
|
|
|
err := repo.MustNotBeArchived()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-14 00:20:57 +05:30
|
|
|
|
// If no branch name is set, assume default branch
|
2019-04-17 21:36:35 +05:30
|
|
|
|
if opts.OldBranch == "" {
|
|
|
|
|
opts.OldBranch = repo.DefaultBranch
|
|
|
|
|
}
|
|
|
|
|
if opts.NewBranch == "" {
|
|
|
|
|
opts.NewBranch = opts.OldBranch
|
|
|
|
|
}
|
|
|
|
|
|
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)
|
2022-01-20 04:56:57 +05:30
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer closer.Close()
|
|
|
|
|
|
2019-04-17 21:36:35 +05:30
|
|
|
|
// oldBranch must exist for this operation
|
2022-03-29 01:18:41 +05:30
|
|
|
|
if _, err := gitRepo.GetBranch(opts.OldBranch); err != nil && !repo.IsEmpty {
|
2019-04-17 21:36:35 +05:30
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-31 14:37:51 +05:30
|
|
|
|
var treePaths []string
|
2023-05-29 15:11:35 +05:30
|
|
|
|
for _, file := range opts.Files {
|
|
|
|
|
// If FromTreePath is not set, set it to the opts.TreePath
|
|
|
|
|
if file.TreePath != "" && file.FromTreePath == "" {
|
|
|
|
|
file.FromTreePath = file.TreePath
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check that the path given in opts.treePath is valid (not a git path)
|
|
|
|
|
treePath := CleanUploadFileName(file.TreePath)
|
|
|
|
|
if treePath == "" {
|
|
|
|
|
return nil, models.ErrFilenameInvalid{
|
|
|
|
|
Path: file.TreePath,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// If there is a fromTreePath (we are copying it), also clean it up
|
|
|
|
|
fromTreePath := CleanUploadFileName(file.FromTreePath)
|
|
|
|
|
if fromTreePath == "" && file.FromTreePath != "" {
|
|
|
|
|
return nil, models.ErrFilenameInvalid{
|
|
|
|
|
Path: file.FromTreePath,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
file.Options = &RepoFileOptions{
|
|
|
|
|
treePath: treePath,
|
|
|
|
|
fromTreePath: fromTreePath,
|
|
|
|
|
executable: false,
|
|
|
|
|
}
|
|
|
|
|
treePaths = append(treePaths, treePath)
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-17 21:36:35 +05:30
|
|
|
|
// A NewBranch can be specified for the file to be created/updated in a new branch.
|
|
|
|
|
// Check to make sure the branch does not already exist, otherwise we can't proceed.
|
|
|
|
|
// If we aren't branching to a new branch, make sure user can commit to the given branch
|
|
|
|
|
if opts.NewBranch != opts.OldBranch {
|
2022-01-20 04:56:57 +05:30
|
|
|
|
existingBranch, err := gitRepo.GetBranch(opts.NewBranch)
|
2019-04-17 21:36:35 +05:30
|
|
|
|
if existingBranch != nil {
|
2023-06-29 15:33:20 +05:30
|
|
|
|
return nil, git_model.ErrBranchAlreadyExists{
|
2019-04-17 21:36:35 +05:30
|
|
|
|
BranchName: opts.NewBranch,
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-19 17:47:27 +05:30
|
|
|
|
if err != nil && !git.IsErrBranchNotExist(err) {
|
2019-04-17 21:36:35 +05:30
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2023-05-29 15:11:35 +05:30
|
|
|
|
} else if err := VerifyBranchProtection(ctx, repo, doer, opts.OldBranch, treePaths); err != nil {
|
2021-09-11 19:51:17 +05:30
|
|
|
|
return nil, err
|
2019-04-17 21:36:35 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
message := strings.TrimSpace(opts.Message)
|
|
|
|
|
|
2019-12-09 18:41:24 +05:30
|
|
|
|
author, committer := GetAuthorAndCommitterUsers(opts.Author, opts.Committer, doer)
|
2019-04-17 21:36:35 +05:30
|
|
|
|
|
2022-01-20 04:56:57 +05:30
|
|
|
|
t, err := NewTemporaryUploadRepository(ctx, repo)
|
2019-04-17 21:36:35 +05:30
|
|
|
|
if err != nil {
|
2024-04-07 16:47:06 +05:30
|
|
|
|
log.Error("NewTemporaryUploadRepository failed: %v", err)
|
2019-04-17 21:36:35 +05:30
|
|
|
|
}
|
2019-06-13 01:11:28 +05:30
|
|
|
|
defer t.Close()
|
2022-03-29 01:18:41 +05:30
|
|
|
|
hasOldBranch := true
|
2024-01-16 20:36:51 +05:30
|
|
|
|
if err := t.Clone(opts.OldBranch, true); err != nil {
|
2023-05-29 15:11:35 +05:30
|
|
|
|
for _, file := range opts.Files {
|
|
|
|
|
if file.Operation == "delete" {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-29 01:18:41 +05:30
|
|
|
|
if !git.IsErrBranchNotExist(err) || !repo.IsEmpty {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2023-12-17 17:26:08 +05:30
|
|
|
|
if err := t.Init(repo.ObjectFormatName); err != nil {
|
2022-03-29 01:18:41 +05:30
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
hasOldBranch = false
|
|
|
|
|
opts.LastCommitID = ""
|
2019-04-17 21:36:35 +05:30
|
|
|
|
}
|
2022-03-29 01:18:41 +05:30
|
|
|
|
if hasOldBranch {
|
|
|
|
|
if err := t.SetDefaultIndex(); err != nil {
|
|
|
|
|
return nil, err
|
2019-08-06 02:09:39 +05:30
|
|
|
|
}
|
2019-04-17 21:36:35 +05:30
|
|
|
|
}
|
|
|
|
|
|
2023-05-29 15:11:35 +05:30
|
|
|
|
for _, file := range opts.Files {
|
|
|
|
|
if file.Operation == "delete" {
|
|
|
|
|
// Get the files in the index
|
|
|
|
|
filesInIndex, err := t.LsFiles(file.TreePath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("DeleteRepoFile: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find the file we want to delete in the index
|
|
|
|
|
inFilelist := false
|
|
|
|
|
for _, indexFile := range filesInIndex {
|
|
|
|
|
if indexFile == file.TreePath {
|
|
|
|
|
inFilelist = true
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !inFilelist {
|
|
|
|
|
return nil, models.ErrRepoFileDoesNotExist{
|
|
|
|
|
Path: file.TreePath,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-26 17:30:30 +05:30
|
|
|
|
|
2022-03-29 01:18:41 +05:30
|
|
|
|
if hasOldBranch {
|
|
|
|
|
// Get the commit of the original branch
|
|
|
|
|
commit, err := t.GetBranchCommit(opts.OldBranch)
|
2019-04-17 21:36:35 +05:30
|
|
|
|
if err != nil {
|
2022-03-29 01:18:41 +05:30
|
|
|
|
return nil, err // Couldn't get a commit for the branch
|
2019-04-17 21:36:35 +05:30
|
|
|
|
}
|
2022-03-29 01:18:41 +05:30
|
|
|
|
|
|
|
|
|
// Assigned LastCommitID in opts if it hasn't been set
|
|
|
|
|
if opts.LastCommitID == "" {
|
|
|
|
|
opts.LastCommitID = commit.ID.String()
|
|
|
|
|
} else {
|
2023-12-14 02:32:00 +05:30
|
|
|
|
lastCommitID, err := t.gitRepo.ConvertToGitID(opts.LastCommitID)
|
2022-03-29 01:18:41 +05:30
|
|
|
|
if err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
|
return nil, fmt.Errorf("ConvertToSHA1: Invalid last commit ID: %w", err)
|
2019-04-17 21:36:35 +05:30
|
|
|
|
}
|
2022-03-29 01:18:41 +05:30
|
|
|
|
opts.LastCommitID = lastCommitID.String()
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-29 15:11:35 +05:30
|
|
|
|
for _, file := range opts.Files {
|
|
|
|
|
if err := handleCheckErrors(file, commit, opts, repo); err != nil {
|
2022-03-29 01:18:41 +05:30
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2023-05-29 15:11:35 +05:30
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
contentStore := lfs.NewContentStore()
|
|
|
|
|
for _, file := range opts.Files {
|
|
|
|
|
switch file.Operation {
|
|
|
|
|
case "create", "update":
|
|
|
|
|
if err := CreateOrUpdateFile(ctx, t, file, contentStore, repo.ID, hasOldBranch); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
case "delete":
|
|
|
|
|
// Remove the file from the index
|
|
|
|
|
if err := t.RemoveFilesFromIndex(file.TreePath); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
default:
|
2023-05-31 14:37:51 +05:30
|
|
|
|
return nil, fmt.Errorf("invalid file operation: %s %s, supported operations are create, update, delete", file.Operation, file.Options.treePath)
|
2023-05-29 15:11:35 +05:30
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Now write the tree
|
|
|
|
|
treeHash, err := t.WriteTree()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Now commit the tree
|
|
|
|
|
var commitHash string
|
|
|
|
|
if opts.Dates != nil {
|
|
|
|
|
commitHash, err = t.CommitTreeWithDate(opts.LastCommitID, author, committer, treeHash, message, opts.Signoff, opts.Dates.Author, opts.Dates.Committer)
|
|
|
|
|
} else {
|
|
|
|
|
commitHash, err = t.CommitTree(opts.LastCommitID, author, committer, treeHash, message, opts.Signoff)
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Then push this tree to NewBranch
|
|
|
|
|
if err := t.Push(doer, commitHash, opts.NewBranch); err != nil {
|
|
|
|
|
log.Error("%T %v", err, err)
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
commit, err := t.GetCommit(commitHash)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-31 14:37:51 +05:30
|
|
|
|
filesResponse, err := GetFilesResponseFromCommit(ctx, repo, commit, opts.NewBranch, treePaths)
|
2023-05-29 15:11:35 +05:30
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if repo.IsEmpty {
|
2023-08-17 10:13:39 +05:30
|
|
|
|
if isEmpty, err := gitRepo.IsEmpty(); err == nil && !isEmpty {
|
|
|
|
|
_ = repo_model.UpdateRepositoryCols(ctx, &repo_model.Repository{ID: repo.ID, IsEmpty: false, DefaultBranch: opts.NewBranch}, "is_empty", "default_branch")
|
|
|
|
|
}
|
2023-05-29 15:11:35 +05:30
|
|
|
|
}
|
|
|
|
|
|
2023-05-31 14:37:51 +05:30
|
|
|
|
return filesResponse, nil
|
2023-05-29 15:11:35 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// handles the check for various issues for ChangeRepoFiles
|
|
|
|
|
func handleCheckErrors(file *ChangeRepoFile, commit *git.Commit, opts *ChangeRepoFilesOptions, repo *repo_model.Repository) error {
|
|
|
|
|
if file.Operation == "update" || file.Operation == "delete" {
|
|
|
|
|
fromEntry, err := commit.GetTreeEntryByPath(file.Options.fromTreePath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if file.SHA != "" {
|
|
|
|
|
// If a SHA was given and the SHA given doesn't match the SHA of the fromTreePath, throw error
|
|
|
|
|
if file.SHA != fromEntry.ID.String() {
|
|
|
|
|
return models.ErrSHADoesNotMatch{
|
|
|
|
|
Path: file.Options.treePath,
|
|
|
|
|
GivenSHA: file.SHA,
|
|
|
|
|
CurrentSHA: fromEntry.ID.String(),
|
2022-03-29 01:18:41 +05:30
|
|
|
|
}
|
2023-05-29 15:11:35 +05:30
|
|
|
|
}
|
|
|
|
|
} else if opts.LastCommitID != "" {
|
|
|
|
|
// If a lastCommitID was given and it doesn't match the commitID of the head of the branch throw
|
|
|
|
|
// an error, but only if we aren't creating a new branch.
|
|
|
|
|
if commit.ID.String() != opts.LastCommitID && opts.OldBranch == opts.NewBranch {
|
|
|
|
|
if changed, err := commit.FileChangedSinceCommit(file.Options.treePath, opts.LastCommitID); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
} else if changed {
|
|
|
|
|
return models.ErrCommitIDDoesNotMatch{
|
|
|
|
|
GivenCommitID: opts.LastCommitID,
|
|
|
|
|
CurrentCommitID: opts.LastCommitID,
|
2019-04-17 21:36:35 +05:30
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-29 15:11:35 +05:30
|
|
|
|
// The file wasn't modified, so we are good to delete it
|
2019-04-17 21:36:35 +05:30
|
|
|
|
}
|
2023-05-29 15:11:35 +05:30
|
|
|
|
} else {
|
|
|
|
|
// When updating a file, a lastCommitID or SHA needs to be given to make sure other commits
|
|
|
|
|
// haven't been made. We throw an error if one wasn't provided.
|
|
|
|
|
return models.ErrSHAOrCommitIDNotProvided{}
|
2019-04-17 21:36:35 +05:30
|
|
|
|
}
|
2023-05-29 15:11:35 +05:30
|
|
|
|
file.Options.executable = fromEntry.IsExecutable()
|
|
|
|
|
}
|
|
|
|
|
if file.Operation == "create" || file.Operation == "update" {
|
2022-03-29 01:18:41 +05:30
|
|
|
|
// For the path where this file will be created/updated, we need to make
|
|
|
|
|
// sure no parts of the path are existing files or links except for the last
|
|
|
|
|
// item in the path which is the file name, and that shouldn't exist IF it is
|
|
|
|
|
// a new file OR is being moved to a new path.
|
2023-05-29 15:11:35 +05:30
|
|
|
|
treePathParts := strings.Split(file.Options.treePath, "/")
|
2022-03-29 01:18:41 +05:30
|
|
|
|
subTreePath := ""
|
|
|
|
|
for index, part := range treePathParts {
|
|
|
|
|
subTreePath = path.Join(subTreePath, part)
|
|
|
|
|
entry, err := commit.GetTreeEntryByPath(subTreePath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if git.IsErrNotExist(err) {
|
|
|
|
|
// Means there is no item with that name, so we're good
|
|
|
|
|
break
|
|
|
|
|
}
|
2023-05-29 15:11:35 +05:30
|
|
|
|
return err
|
2019-04-17 21:36:35 +05:30
|
|
|
|
}
|
2022-03-29 01:18:41 +05:30
|
|
|
|
if index < len(treePathParts)-1 {
|
|
|
|
|
if !entry.IsDir() {
|
2023-05-29 15:11:35 +05:30
|
|
|
|
return models.ErrFilePathInvalid{
|
2022-03-29 01:18:41 +05:30
|
|
|
|
Message: fmt.Sprintf("a file exists where you’re trying to create a subdirectory [path: %s]", subTreePath),
|
|
|
|
|
Path: subTreePath,
|
|
|
|
|
Name: part,
|
|
|
|
|
Type: git.EntryModeBlob,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if entry.IsLink() {
|
2023-05-29 15:11:35 +05:30
|
|
|
|
return models.ErrFilePathInvalid{
|
2022-03-29 01:18:41 +05:30
|
|
|
|
Message: fmt.Sprintf("a symbolic link exists where you’re trying to create a subdirectory [path: %s]", subTreePath),
|
2019-04-17 21:36:35 +05:30
|
|
|
|
Path: subTreePath,
|
|
|
|
|
Name: part,
|
2022-03-29 01:18:41 +05:30
|
|
|
|
Type: git.EntryModeSymlink,
|
|
|
|
|
}
|
|
|
|
|
} else if entry.IsDir() {
|
2023-05-29 15:11:35 +05:30
|
|
|
|
return models.ErrFilePathInvalid{
|
2022-03-29 01:18:41 +05:30
|
|
|
|
Message: fmt.Sprintf("a directory exists where you’re trying to create a file [path: %s]", subTreePath),
|
|
|
|
|
Path: subTreePath,
|
|
|
|
|
Name: part,
|
|
|
|
|
Type: git.EntryModeTree,
|
|
|
|
|
}
|
2023-05-29 15:11:35 +05:30
|
|
|
|
} else if file.Options.fromTreePath != file.Options.treePath || file.Operation == "create" {
|
2022-03-29 01:18:41 +05:30
|
|
|
|
// The entry shouldn't exist if we are creating new file or moving to a new path
|
2023-05-29 15:11:35 +05:30
|
|
|
|
return models.ErrRepoFileAlreadyExists{
|
|
|
|
|
Path: file.Options.treePath,
|
2019-04-17 21:36:35 +05:30
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-29 01:18:41 +05:30
|
|
|
|
}
|
2019-04-17 21:36:35 +05:30
|
|
|
|
}
|
|
|
|
|
|
2023-05-29 15:11:35 +05:30
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-31 14:37:51 +05:30
|
|
|
|
// CreateOrUpdateFile handles creating or updating a file for ChangeRepoFiles
|
2023-05-29 15:11:35 +05:30
|
|
|
|
func CreateOrUpdateFile(ctx context.Context, t *TemporaryUploadRepository, file *ChangeRepoFile, contentStore *lfs.ContentStore, repoID int64, hasOldBranch bool) error {
|
2019-04-17 21:36:35 +05:30
|
|
|
|
// Get the two paths (might be the same if not moving) from the index if they exist
|
2023-05-29 15:11:35 +05:30
|
|
|
|
filesInIndex, err := t.LsFiles(file.TreePath, file.FromTreePath)
|
2019-04-17 21:36:35 +05:30
|
|
|
|
if err != nil {
|
2023-05-29 15:11:35 +05:30
|
|
|
|
return fmt.Errorf("UpdateRepoFile: %w", err)
|
2019-04-17 21:36:35 +05:30
|
|
|
|
}
|
|
|
|
|
// If is a new file (not updating) then the given path shouldn't exist
|
2023-05-29 15:11:35 +05:30
|
|
|
|
if file.Operation == "create" {
|
|
|
|
|
for _, indexFile := range filesInIndex {
|
|
|
|
|
if indexFile == file.TreePath {
|
|
|
|
|
return models.ErrRepoFileAlreadyExists{
|
|
|
|
|
Path: file.TreePath,
|
2019-04-17 21:36:35 +05:30
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove the old path from the tree
|
2023-05-29 15:11:35 +05:30
|
|
|
|
if file.Options.fromTreePath != file.Options.treePath && len(filesInIndex) > 0 {
|
|
|
|
|
for _, indexFile := range filesInIndex {
|
|
|
|
|
if indexFile == file.Options.fromTreePath {
|
|
|
|
|
if err := t.RemoveFilesFromIndex(file.FromTreePath); err != nil {
|
|
|
|
|
return err
|
2019-04-17 21:36:35 +05:30
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-18 23:44:47 +05:30
|
|
|
|
treeObjectContentReader := file.ContentReader
|
2022-06-12 21:21:54 +05:30
|
|
|
|
var lfsMetaObject *git_model.LFSMetaObject
|
2022-03-29 01:18:41 +05:30
|
|
|
|
if setting.LFS.StartServer && hasOldBranch {
|
2019-10-12 05:43:27 +05:30
|
|
|
|
// Check there is no way this can return multiple infos
|
2024-03-24 17:14:30 +05:30
|
|
|
|
filterAttribute, err := t.gitRepo.GitAttributeFirst("", file.Options.treePath, "filter")
|
2019-04-17 21:36:35 +05:30
|
|
|
|
if err != nil {
|
2023-05-29 15:11:35 +05:30
|
|
|
|
return err
|
2019-04-17 21:36:35 +05:30
|
|
|
|
}
|
|
|
|
|
|
2024-03-24 17:14:30 +05:30
|
|
|
|
if filterAttribute == "lfs" {
|
2019-10-12 05:43:27 +05:30
|
|
|
|
// OK so we are supposed to LFS this data!
|
2023-07-18 23:44:47 +05:30
|
|
|
|
pointer, err := lfs.GeneratePointer(treeObjectContentReader)
|
2019-10-12 05:43:27 +05:30
|
|
|
|
if err != nil {
|
2023-05-29 15:11:35 +05:30
|
|
|
|
return err
|
2019-10-12 05:43:27 +05:30
|
|
|
|
}
|
2023-05-29 15:11:35 +05:30
|
|
|
|
lfsMetaObject = &git_model.LFSMetaObject{Pointer: pointer, RepositoryID: repoID}
|
2023-07-18 23:44:47 +05:30
|
|
|
|
treeObjectContentReader = strings.NewReader(pointer.StringContent())
|
2019-10-12 05:43:27 +05:30
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-29 15:11:35 +05:30
|
|
|
|
|
2019-04-17 21:36:35 +05:30
|
|
|
|
// Add the object to the database
|
2023-07-18 23:44:47 +05:30
|
|
|
|
objectHash, err := t.HashObject(treeObjectContentReader)
|
2019-04-17 21:36:35 +05:30
|
|
|
|
if err != nil {
|
2023-05-29 15:11:35 +05:30
|
|
|
|
return err
|
2019-04-17 21:36:35 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add the object to the index
|
2023-05-29 15:11:35 +05:30
|
|
|
|
if file.Options.executable {
|
|
|
|
|
if err := t.AddObjectToIndex("100755", objectHash, file.Options.treePath); err != nil {
|
|
|
|
|
return err
|
2020-03-05 05:16:12 +05:30
|
|
|
|
}
|
|
|
|
|
} else {
|
2023-05-29 15:11:35 +05:30
|
|
|
|
if err := t.AddObjectToIndex("100644", objectHash, file.Options.treePath); err != nil {
|
|
|
|
|
return err
|
2020-03-05 05:16:12 +05:30
|
|
|
|
}
|
2019-04-17 21:36:35 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if lfsMetaObject != nil {
|
|
|
|
|
// We have an LFS object - create it
|
2023-12-07 12:57:36 +05:30
|
|
|
|
lfsMetaObject, err = git_model.NewLFSMetaObject(ctx, lfsMetaObject.RepositoryID, lfsMetaObject.Pointer)
|
2019-04-17 21:36:35 +05:30
|
|
|
|
if err != nil {
|
2023-05-29 15:11:35 +05:30
|
|
|
|
return err
|
2019-04-17 21:36:35 +05:30
|
|
|
|
}
|
2021-04-09 03:55:57 +05:30
|
|
|
|
exist, err := contentStore.Exists(lfsMetaObject.Pointer)
|
2020-09-08 21:15:10 +05:30
|
|
|
|
if err != nil {
|
2023-05-29 15:11:35 +05:30
|
|
|
|
return err
|
2020-09-08 21:15:10 +05:30
|
|
|
|
}
|
|
|
|
|
if !exist {
|
2024-02-19 20:20:03 +05:30
|
|
|
|
_, err := file.ContentReader.Seek(0, io.SeekStart)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2023-07-18 23:44:47 +05:30
|
|
|
|
if err := contentStore.Put(lfsMetaObject.Pointer, file.ContentReader); err != nil {
|
2023-05-29 15:11:35 +05:30
|
|
|
|
if _, err2 := git_model.RemoveLFSMetaObjectByOid(ctx, repoID, lfsMetaObject.Oid); err2 != nil {
|
2023-05-31 14:37:51 +05:30
|
|
|
|
return fmt.Errorf("unable to remove failed inserted LFS object %s: %v (Prev Error: %w)", lfsMetaObject.Oid, err2, err)
|
2019-04-17 21:36:35 +05:30
|
|
|
|
}
|
2023-05-29 15:11:35 +05:30
|
|
|
|
return err
|
2019-04-17 21:36:35 +05:30
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-29 15:11:35 +05:30
|
|
|
|
return nil
|
2019-04-17 21:36:35 +05:30
|
|
|
|
}
|
2021-09-11 19:51:17 +05:30
|
|
|
|
|
|
|
|
|
// VerifyBranchProtection verify the branch protection for modifying the given treePath on the given branch
|
2023-05-29 15:11:35 +05:30
|
|
|
|
func VerifyBranchProtection(ctx context.Context, repo *repo_model.Repository, doer *user_model.User, branchName string, treePaths []string) error {
|
2023-01-16 13:30:22 +05:30
|
|
|
|
protectedBranch, err := git_model.GetFirstMatchProtectedBranchRule(ctx, repo.ID, branchName)
|
2021-09-11 19:51:17 +05:30
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if protectedBranch != nil {
|
2023-01-16 13:30:22 +05:30
|
|
|
|
protectedBranch.Repo = repo
|
2023-05-29 15:11:35 +05:30
|
|
|
|
globUnprotected := protectedBranch.GetUnprotectedFilePatterns()
|
|
|
|
|
globProtected := protectedBranch.GetProtectedFilePatterns()
|
|
|
|
|
canUserPush := protectedBranch.CanUserPush(ctx, doer)
|
|
|
|
|
for _, treePath := range treePaths {
|
|
|
|
|
isUnprotectedFile := false
|
|
|
|
|
if len(globUnprotected) != 0 {
|
|
|
|
|
isUnprotectedFile = protectedBranch.IsUnprotectedFile(globUnprotected, treePath)
|
|
|
|
|
}
|
|
|
|
|
if !canUserPush && !isUnprotectedFile {
|
|
|
|
|
return models.ErrUserCannotCommit{
|
|
|
|
|
UserName: doer.LowerName,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if protectedBranch.IsProtectedFile(globProtected, treePath) {
|
|
|
|
|
return models.ErrFilePathProtected{
|
|
|
|
|
Path: treePath,
|
|
|
|
|
}
|
2021-09-11 19:51:17 +05:30
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if protectedBranch.RequireSignedCommits {
|
2022-01-20 04:56:57 +05:30
|
|
|
|
_, _, _, err := asymkey_service.SignCRUDAction(ctx, repo.RepoPath(), doer, repo.RepoPath(), branchName)
|
2021-09-11 19:51:17 +05:30
|
|
|
|
if err != nil {
|
2021-12-10 13:44:24 +05:30
|
|
|
|
if !asymkey_service.IsErrWontSign(err) {
|
2021-09-11 19:51:17 +05:30
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return models.ErrUserCannotCommit{
|
|
|
|
|
UserName: doer.LowerName,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|