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 (
|
2019-04-26 17:30:30 +05:30
|
|
|
|
"bytes"
|
2022-01-20 04:56:57 +05:30
|
|
|
|
"context"
|
2019-04-17 21:36:35 +05:30
|
|
|
|
"fmt"
|
|
|
|
|
"path"
|
|
|
|
|
"strings"
|
2019-12-24 08:03:52 +05:30
|
|
|
|
"time"
|
2019-04-17 21:36:35 +05:30
|
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
2023-01-09 09:20:54 +05:30
|
|
|
|
"code.gitea.io/gitea/models/db"
|
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-08-15 17:37:28 +05:30
|
|
|
|
"code.gitea.io/gitea/modules/charset"
|
2019-04-17 21:36:35 +05:30
|
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
|
"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-10-25 02:42:43 +05:30
|
|
|
|
"code.gitea.io/gitea/modules/util"
|
2021-12-10 13:44:24 +05:30
|
|
|
|
asymkey_service "code.gitea.io/gitea/services/asymkey"
|
2019-08-15 17:37:28 +05:30
|
|
|
|
|
|
|
|
|
stdcharset "golang.org/x/net/html/charset"
|
|
|
|
|
"golang.org/x/text/transform"
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-17 21:36:35 +05:30
|
|
|
|
// UpdateRepoFileOptions holds the repository file update options
|
|
|
|
|
type UpdateRepoFileOptions struct {
|
|
|
|
|
LastCommitID string
|
|
|
|
|
OldBranch string
|
|
|
|
|
NewBranch string
|
|
|
|
|
TreePath string
|
|
|
|
|
FromTreePath string
|
|
|
|
|
Message string
|
|
|
|
|
Content string
|
|
|
|
|
SHA string
|
|
|
|
|
IsNewFile bool
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2021-12-10 06:57:50 +05:30
|
|
|
|
func detectEncodingAndBOM(entry *git.TreeEntry, repo *repo_model.Repository) (string, bool) {
|
2019-04-26 17:30:30 +05:30
|
|
|
|
reader, err := entry.Blob().DataAsync()
|
|
|
|
|
if err != nil {
|
|
|
|
|
// return default
|
|
|
|
|
return "UTF-8", false
|
|
|
|
|
}
|
|
|
|
|
defer reader.Close()
|
|
|
|
|
buf := make([]byte, 1024)
|
2021-10-25 02:42:43 +05:30
|
|
|
|
n, err := util.ReadAtMost(reader, buf)
|
2019-04-26 17:30:30 +05:30
|
|
|
|
if err != nil {
|
|
|
|
|
// return default
|
|
|
|
|
return "UTF-8", false
|
|
|
|
|
}
|
|
|
|
|
buf = buf[:n]
|
|
|
|
|
|
|
|
|
|
if setting.LFS.StartServer {
|
2021-04-09 03:55:57 +05:30
|
|
|
|
pointer, _ := lfs.ReadPointerFromBuffer(buf)
|
|
|
|
|
if pointer.IsValid() {
|
2023-01-09 09:20:54 +05:30
|
|
|
|
meta, err := git_model.GetLFSMetaObjectByOid(db.DefaultContext, repo.ID, pointer.Oid)
|
2022-06-12 21:21:54 +05:30
|
|
|
|
if err != nil && err != git_model.ErrLFSObjectNotExist {
|
2019-04-26 17:30:30 +05:30
|
|
|
|
// return default
|
|
|
|
|
return "UTF-8", false
|
|
|
|
|
}
|
2021-04-09 03:55:57 +05:30
|
|
|
|
if meta != nil {
|
|
|
|
|
dataRc, err := lfs.ReadMetaObject(pointer)
|
|
|
|
|
if err != nil {
|
|
|
|
|
// return default
|
|
|
|
|
return "UTF-8", false
|
|
|
|
|
}
|
|
|
|
|
defer dataRc.Close()
|
|
|
|
|
buf = make([]byte, 1024)
|
2021-10-25 02:42:43 +05:30
|
|
|
|
n, err = util.ReadAtMost(dataRc, buf)
|
2021-04-09 03:55:57 +05:30
|
|
|
|
if err != nil {
|
|
|
|
|
// return default
|
|
|
|
|
return "UTF-8", false
|
|
|
|
|
}
|
|
|
|
|
buf = buf[:n]
|
2019-04-26 17:30:30 +05:30
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-15 17:37:28 +05:30
|
|
|
|
encoding, err := charset.DetectEncoding(buf)
|
2019-04-26 17:30:30 +05:30
|
|
|
|
if err != nil {
|
|
|
|
|
// just default to utf-8 and no bom
|
|
|
|
|
return "UTF-8", false
|
|
|
|
|
}
|
|
|
|
|
if encoding == "UTF-8" {
|
2019-08-15 17:37:28 +05:30
|
|
|
|
return encoding, bytes.Equal(buf[0:3], charset.UTF8BOM)
|
2019-04-26 17:30:30 +05:30
|
|
|
|
}
|
2019-08-15 17:37:28 +05:30
|
|
|
|
charsetEncoding, _ := stdcharset.Lookup(encoding)
|
2019-04-26 17:30:30 +05:30
|
|
|
|
if charsetEncoding == nil {
|
|
|
|
|
return "UTF-8", false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result, n, err := transform.String(charsetEncoding.NewDecoder(), string(buf))
|
2019-06-13 01:11:28 +05:30
|
|
|
|
if err != nil {
|
|
|
|
|
// return default
|
|
|
|
|
return "UTF-8", false
|
|
|
|
|
}
|
2019-04-26 17:30:30 +05:30
|
|
|
|
|
|
|
|
|
if n > 2 {
|
2019-08-15 17:37:28 +05:30
|
|
|
|
return encoding, bytes.Equal([]byte(result)[0:3], charset.UTF8BOM)
|
2019-04-26 17:30:30 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return encoding, false
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-17 21:36:35 +05:30
|
|
|
|
// CreateOrUpdateRepoFile adds or updates a file in the given repository
|
2022-01-20 04:56:57 +05:30
|
|
|
|
func CreateOrUpdateRepoFile(ctx context.Context, repo *repo_model.Repository, doer *user_model.User, opts *UpdateRepoFileOptions) (*structs.FileResponse, error) {
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-20 04:56:57 +05:30
|
|
|
|
gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath())
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
|
return nil, models.ErrBranchAlreadyExists{
|
|
|
|
|
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
|
|
|
|
|
}
|
2022-01-20 04:56:57 +05:30
|
|
|
|
} else if err := VerifyBranchProtection(ctx, repo, doer, opts.OldBranch, opts.TreePath); err != nil {
|
2021-09-11 19:51:17 +05:30
|
|
|
|
return nil, err
|
2019-04-17 21:36:35 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If FromTreePath is not set, set it to the opts.TreePath
|
|
|
|
|
if opts.TreePath != "" && opts.FromTreePath == "" {
|
|
|
|
|
opts.FromTreePath = opts.TreePath
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check that the path given in opts.treePath is valid (not a git path)
|
|
|
|
|
treePath := CleanUploadFileName(opts.TreePath)
|
|
|
|
|
if treePath == "" {
|
|
|
|
|
return nil, models.ErrFilenameInvalid{
|
|
|
|
|
Path: opts.TreePath,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// If there is a fromTreePath (we are copying it), also clean it up
|
|
|
|
|
fromTreePath := CleanUploadFileName(opts.FromTreePath)
|
|
|
|
|
if fromTreePath == "" && opts.FromTreePath != "" {
|
|
|
|
|
return nil, models.ErrFilenameInvalid{
|
|
|
|
|
Path: opts.FromTreePath,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 {
|
2019-06-13 01:11:28 +05:30
|
|
|
|
log.Error("%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
|
2019-04-17 21:36:35 +05:30
|
|
|
|
if err := t.Clone(opts.OldBranch); err != nil {
|
2022-03-29 01:18:41 +05:30
|
|
|
|
if !git.IsErrBranchNotExist(err) || !repo.IsEmpty {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if err := t.Init(); err != nil {
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2019-04-26 17:30:30 +05:30
|
|
|
|
encoding := "UTF-8"
|
|
|
|
|
bom := false
|
2020-03-05 05:16:12 +05:30
|
|
|
|
executable := false
|
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 {
|
|
|
|
|
lastCommitID, err := t.gitRepo.ConvertToSHA1(opts.LastCommitID)
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !opts.IsNewFile {
|
|
|
|
|
fromEntry, err := commit.GetTreeEntryByPath(fromTreePath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if opts.SHA != "" {
|
|
|
|
|
// If a SHA was given and the SHA given doesn't match the SHA of the fromTreePath, throw error
|
|
|
|
|
if opts.SHA != fromEntry.ID.String() {
|
|
|
|
|
return nil, models.ErrSHADoesNotMatch{
|
|
|
|
|
Path: treePath,
|
|
|
|
|
GivenSHA: opts.SHA,
|
|
|
|
|
CurrentSHA: fromEntry.ID.String(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} 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(treePath, opts.LastCommitID); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
} else if changed {
|
|
|
|
|
return nil, models.ErrCommitIDDoesNotMatch{
|
|
|
|
|
GivenCommitID: opts.LastCommitID,
|
|
|
|
|
CurrentCommitID: opts.LastCommitID,
|
|
|
|
|
}
|
2019-04-17 21:36:35 +05:30
|
|
|
|
}
|
2022-03-29 01:18:41 +05:30
|
|
|
|
// The file wasn't modified, so we are good to delete it
|
2019-04-17 21:36:35 +05:30
|
|
|
|
}
|
2022-03-29 01:18:41 +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 nil, models.ErrSHAOrCommitIDNotProvided{}
|
2019-04-17 21:36:35 +05:30
|
|
|
|
}
|
2022-03-29 01:18:41 +05:30
|
|
|
|
encoding, bom = detectEncodingAndBOM(fromEntry, repo)
|
|
|
|
|
executable = fromEntry.IsExecutable()
|
2019-04-17 21:36:35 +05:30
|
|
|
|
}
|
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.
|
|
|
|
|
treePathParts := strings.Split(treePath, "/")
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
return nil, 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() {
|
|
|
|
|
return nil, models.ErrFilePathInvalid{
|
|
|
|
|
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() {
|
2019-04-17 21:36:35 +05:30
|
|
|
|
return nil, 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() {
|
|
|
|
|
return nil, models.ErrFilePathInvalid{
|
|
|
|
|
Message: fmt.Sprintf("a directory exists where you’re trying to create a file [path: %s]", subTreePath),
|
|
|
|
|
Path: subTreePath,
|
|
|
|
|
Name: part,
|
|
|
|
|
Type: git.EntryModeTree,
|
|
|
|
|
}
|
|
|
|
|
} else if fromTreePath != treePath || opts.IsNewFile {
|
|
|
|
|
// The entry shouldn't exist if we are creating new file or moving to a new path
|
|
|
|
|
return nil, models.ErrRepoFileAlreadyExists{
|
|
|
|
|
Path: 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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get the two paths (might be the same if not moving) from the index if they exist
|
|
|
|
|
filesInIndex, err := t.LsFiles(opts.TreePath, opts.FromTreePath)
|
|
|
|
|
if err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
|
return nil, 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
|
|
|
|
|
if opts.IsNewFile {
|
|
|
|
|
for _, file := range filesInIndex {
|
|
|
|
|
if file == opts.TreePath {
|
|
|
|
|
return nil, models.ErrRepoFileAlreadyExists{
|
|
|
|
|
Path: opts.TreePath,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove the old path from the tree
|
|
|
|
|
if fromTreePath != treePath && len(filesInIndex) > 0 {
|
|
|
|
|
for _, file := range filesInIndex {
|
|
|
|
|
if file == fromTreePath {
|
|
|
|
|
if err := t.RemoveFilesFromIndex(opts.FromTreePath); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
content := opts.Content
|
2019-04-26 17:30:30 +05:30
|
|
|
|
if bom {
|
2019-08-15 17:37:28 +05:30
|
|
|
|
content = string(charset.UTF8BOM) + content
|
2019-04-26 17:30:30 +05:30
|
|
|
|
}
|
|
|
|
|
if encoding != "UTF-8" {
|
2019-08-15 17:37:28 +05:30
|
|
|
|
charsetEncoding, _ := stdcharset.Lookup(encoding)
|
2019-04-26 17:30:30 +05:30
|
|
|
|
if charsetEncoding != nil {
|
2019-07-24 00:20:39 +05:30
|
|
|
|
result, _, err := transform.String(charsetEncoding.NewEncoder(), content)
|
2019-04-26 17:30:30 +05:30
|
|
|
|
if err != nil {
|
|
|
|
|
// Look if we can't encode back in to the original we should just stick with utf-8
|
|
|
|
|
log.Error("Error re-encoding %s (%s) as %s - will stay as UTF-8: %v", opts.TreePath, opts.FromTreePath, encoding, err)
|
|
|
|
|
result = content
|
|
|
|
|
}
|
|
|
|
|
content = result
|
|
|
|
|
} else {
|
|
|
|
|
log.Error("Unknown encoding: %s", encoding)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Reset the opts.Content to our adjusted content to ensure that LFS gets the correct content
|
|
|
|
|
opts.Content = content
|
2022-06-12 21:21:54 +05:30
|
|
|
|
var lfsMetaObject *git_model.LFSMetaObject
|
2019-04-17 21:36:35 +05:30
|
|
|
|
|
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
|
2021-03-01 17:44:17 +05:30
|
|
|
|
filename2attribute2info, err := t.gitRepo.CheckAttribute(git.CheckAttributeOpts{
|
2022-10-23 20:14:45 +05:30
|
|
|
|
Attributes: []git.CmdArg{"filter"},
|
2021-03-01 17:44:17 +05:30
|
|
|
|
Filenames: []string{treePath},
|
2022-01-18 13:14:30 +05:30
|
|
|
|
CachedOnly: true,
|
2021-03-01 17:44:17 +05:30
|
|
|
|
})
|
2019-04-17 21:36:35 +05:30
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-12 05:43:27 +05:30
|
|
|
|
if filename2attribute2info[treePath] != nil && filename2attribute2info[treePath]["filter"] == "lfs" {
|
|
|
|
|
// OK so we are supposed to LFS this data!
|
2021-04-09 03:55:57 +05:30
|
|
|
|
pointer, err := lfs.GeneratePointer(strings.NewReader(opts.Content))
|
2019-10-12 05:43:27 +05:30
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2022-06-12 21:21:54 +05:30
|
|
|
|
lfsMetaObject = &git_model.LFSMetaObject{Pointer: pointer, RepositoryID: repo.ID}
|
2021-04-09 03:55:57 +05:30
|
|
|
|
content = pointer.StringContent()
|
2019-10-12 05:43:27 +05:30
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 21:36:35 +05:30
|
|
|
|
// Add the object to the database
|
|
|
|
|
objectHash, err := t.HashObject(strings.NewReader(content))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add the object to the index
|
2020-03-05 05:16:12 +05:30
|
|
|
|
if executable {
|
|
|
|
|
if err := t.AddObjectToIndex("100755", objectHash, treePath); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if err := t.AddObjectToIndex("100644", objectHash, treePath); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2019-04-17 21:36:35 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Now write the tree
|
|
|
|
|
treeHash, err := t.WriteTree()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Now commit the tree
|
2019-12-24 08:03:52 +05:30
|
|
|
|
var commitHash string
|
|
|
|
|
if opts.Dates != nil {
|
2022-03-29 01:18:41 +05:30
|
|
|
|
commitHash, err = t.CommitTreeWithDate(opts.LastCommitID, author, committer, treeHash, message, opts.Signoff, opts.Dates.Author, opts.Dates.Committer)
|
2019-12-24 08:03:52 +05:30
|
|
|
|
} else {
|
2022-03-29 01:18:41 +05:30
|
|
|
|
commitHash, err = t.CommitTree(opts.LastCommitID, author, committer, treeHash, message, opts.Signoff)
|
2019-12-24 08:03:52 +05:30
|
|
|
|
}
|
2019-04-17 21:36:35 +05:30
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if lfsMetaObject != nil {
|
|
|
|
|
// We have an LFS object - create it
|
2023-01-09 09:20:54 +05:30
|
|
|
|
lfsMetaObject, err = git_model.NewLFSMetaObject(ctx, lfsMetaObject)
|
2019-04-17 21:36:35 +05:30
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2021-04-09 03:55:57 +05:30
|
|
|
|
contentStore := lfs.NewContentStore()
|
|
|
|
|
exist, err := contentStore.Exists(lfsMetaObject.Pointer)
|
2020-09-08 21:15:10 +05:30
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if !exist {
|
2021-04-09 03:55:57 +05:30
|
|
|
|
if err := contentStore.Put(lfsMetaObject.Pointer, strings.NewReader(opts.Content)); err != nil {
|
2023-01-09 09:20:54 +05:30
|
|
|
|
if _, err2 := git_model.RemoveLFSMetaObjectByOid(ctx, repo.ID, lfsMetaObject.Oid); err2 != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
|
return nil, fmt.Errorf("Error whilst removing failed inserted LFS object %s: %v (Prev Error: %w)", lfsMetaObject.Oid, err2, err)
|
2019-04-17 21:36:35 +05:30
|
|
|
|
}
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Then push this tree to NewBranch
|
|
|
|
|
if err := t.Push(doer, commitHash, opts.NewBranch); err != nil {
|
2020-03-28 09:43:18 +05:30
|
|
|
|
log.Error("%T %v", err, err)
|
2019-04-17 21:36:35 +05:30
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-29 01:18:41 +05:30
|
|
|
|
commit, err := t.GetCommit(commitHash)
|
2019-04-17 21:36:35 +05:30
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-20 04:56:57 +05:30
|
|
|
|
file, err := GetFileResponseFromCommit(ctx, repo, commit, opts.NewBranch, treePath)
|
2019-04-17 21:36:35 +05:30
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return file, nil
|
|
|
|
|
}
|
2021-09-11 19:51:17 +05:30
|
|
|
|
|
|
|
|
|
// VerifyBranchProtection verify the branch protection for modifying the given treePath on the given branch
|
2022-01-20 04:56:57 +05:30
|
|
|
|
func VerifyBranchProtection(ctx context.Context, repo *repo_model.Repository, doer *user_model.User, branchName, treePath 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
|
2021-09-11 19:51:17 +05:30
|
|
|
|
isUnprotectedFile := false
|
|
|
|
|
glob := protectedBranch.GetUnprotectedFilePatterns()
|
|
|
|
|
if len(glob) != 0 {
|
|
|
|
|
isUnprotectedFile = protectedBranch.IsUnprotectedFile(glob, treePath)
|
|
|
|
|
}
|
2023-01-16 13:30:22 +05:30
|
|
|
|
if !protectedBranch.CanUserPush(ctx, doer) && !isUnprotectedFile {
|
2021-09-11 19:51:17 +05:30
|
|
|
|
return models.ErrUserCannotCommit{
|
|
|
|
|
UserName: doer.LowerName,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
patterns := protectedBranch.GetProtectedFilePatterns()
|
|
|
|
|
for _, pat := range patterns {
|
|
|
|
|
if pat.Match(strings.ToLower(treePath)) {
|
|
|
|
|
return models.ErrFilePathProtected{
|
|
|
|
|
Path: treePath,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|