2019-04-17 12:06:35 -04:00
|
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
|
// SPDX-License-Identifier: MIT
|
2019-04-17 12:06:35 -04:00
|
|
|
|
|
2021-11-24 02:56:24 -05:00
|
|
|
|
package files
|
2019-04-17 12:06:35 -04:00
|
|
|
|
|
|
|
|
|
import (
|
2019-04-26 08:00:30 -04:00
|
|
|
|
"bytes"
|
2022-01-19 18:26:57 -05:00
|
|
|
|
"context"
|
2019-04-17 12:06:35 -04:00
|
|
|
|
"fmt"
|
|
|
|
|
"path"
|
|
|
|
|
"strings"
|
2019-12-23 21:33:52 -05:00
|
|
|
|
"time"
|
2019-04-17 12:06:35 -04:00
|
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
2023-01-08 22:50:54 -05:00
|
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-12 11:51:54 -04:00
|
|
|
|
git_model "code.gitea.io/gitea/models/git"
|
2021-12-09 20:27:50 -05:00
|
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-24 04:49:20 -05:00
|
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2019-08-15 08:07:28 -04:00
|
|
|
|
"code.gitea.io/gitea/modules/charset"
|
2019-04-17 12:06:35 -04:00
|
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
|
"code.gitea.io/gitea/modules/lfs"
|
2019-04-26 08:00:30 -04:00
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2019-04-17 12:06:35 -04:00
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-05-11 06:21:34 -04:00
|
|
|
|
"code.gitea.io/gitea/modules/structs"
|
2021-10-24 17:12:43 -04:00
|
|
|
|
"code.gitea.io/gitea/modules/util"
|
2021-12-10 03:14:24 -05:00
|
|
|
|
asymkey_service "code.gitea.io/gitea/services/asymkey"
|
2019-08-15 08:07:28 -04:00
|
|
|
|
|
|
|
|
|
stdcharset "golang.org/x/net/html/charset"
|
|
|
|
|
"golang.org/x/text/transform"
|
2019-04-17 12:06:35 -04:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// IdentityOptions for a person's identity like an author or committer
|
|
|
|
|
type IdentityOptions struct {
|
|
|
|
|
Name string
|
|
|
|
|
Email string
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-23 21:33:52 -05:00
|
|
|
|
// CommitDateOptions store dates for GIT_AUTHOR_DATE and GIT_COMMITTER_DATE
|
|
|
|
|
type CommitDateOptions struct {
|
|
|
|
|
Author time.Time
|
|
|
|
|
Committer time.Time
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-29 05:41:35 -04:00
|
|
|
|
type ChangeRepoFile struct {
|
|
|
|
|
Operation string
|
2019-04-17 12:06:35 -04:00
|
|
|
|
TreePath string
|
|
|
|
|
FromTreePath string
|
|
|
|
|
Content string
|
|
|
|
|
SHA string
|
2023-05-29 05:41:35 -04:00
|
|
|
|
Options *RepoFileOptions
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// UpdateRepoFilesOptions holds the repository files update options
|
|
|
|
|
type ChangeRepoFilesOptions struct {
|
|
|
|
|
LastCommitID string
|
|
|
|
|
OldBranch string
|
|
|
|
|
NewBranch string
|
|
|
|
|
Message string
|
|
|
|
|
Files []*ChangeRepoFile
|
2019-04-17 12:06:35 -04:00
|
|
|
|
Author *IdentityOptions
|
|
|
|
|
Committer *IdentityOptions
|
2019-12-23 21:33:52 -05:00
|
|
|
|
Dates *CommitDateOptions
|
2021-01-29 03:57:45 -05:00
|
|
|
|
Signoff bool
|
2019-04-17 12:06:35 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-29 05:41:35 -04:00
|
|
|
|
type RepoFileOptions struct {
|
|
|
|
|
treePath string
|
|
|
|
|
fromTreePath string
|
|
|
|
|
encoding string
|
|
|
|
|
bom bool
|
|
|
|
|
executable bool
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-09 20:27:50 -05:00
|
|
|
|
func detectEncodingAndBOM(entry *git.TreeEntry, repo *repo_model.Repository) (string, bool) {
|
2019-04-26 08:00:30 -04:00
|
|
|
|
reader, err := entry.Blob().DataAsync()
|
|
|
|
|
if err != nil {
|
|
|
|
|
// return default
|
|
|
|
|
return "UTF-8", false
|
|
|
|
|
}
|
|
|
|
|
defer reader.Close()
|
|
|
|
|
buf := make([]byte, 1024)
|
2021-10-24 17:12:43 -04:00
|
|
|
|
n, err := util.ReadAtMost(reader, buf)
|
2019-04-26 08:00:30 -04:00
|
|
|
|
if err != nil {
|
|
|
|
|
// return default
|
|
|
|
|
return "UTF-8", false
|
|
|
|
|
}
|
|
|
|
|
buf = buf[:n]
|
|
|
|
|
|
|
|
|
|
if setting.LFS.StartServer {
|
2021-04-08 18:25:57 -04:00
|
|
|
|
pointer, _ := lfs.ReadPointerFromBuffer(buf)
|
|
|
|
|
if pointer.IsValid() {
|
2023-01-08 22:50:54 -05:00
|
|
|
|
meta, err := git_model.GetLFSMetaObjectByOid(db.DefaultContext, repo.ID, pointer.Oid)
|
2022-06-12 11:51:54 -04:00
|
|
|
|
if err != nil && err != git_model.ErrLFSObjectNotExist {
|
2019-04-26 08:00:30 -04:00
|
|
|
|
// return default
|
|
|
|
|
return "UTF-8", false
|
|
|
|
|
}
|
2021-04-08 18:25:57 -04:00
|
|
|
|
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-24 17:12:43 -04:00
|
|
|
|
n, err = util.ReadAtMost(dataRc, buf)
|
2021-04-08 18:25:57 -04:00
|
|
|
|
if err != nil {
|
|
|
|
|
// return default
|
|
|
|
|
return "UTF-8", false
|
|
|
|
|
}
|
|
|
|
|
buf = buf[:n]
|
2019-04-26 08:00:30 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-15 08:07:28 -04:00
|
|
|
|
encoding, err := charset.DetectEncoding(buf)
|
2019-04-26 08:00:30 -04:00
|
|
|
|
if err != nil {
|
|
|
|
|
// just default to utf-8 and no bom
|
|
|
|
|
return "UTF-8", false
|
|
|
|
|
}
|
|
|
|
|
if encoding == "UTF-8" {
|
2019-08-15 08:07:28 -04:00
|
|
|
|
return encoding, bytes.Equal(buf[0:3], charset.UTF8BOM)
|
2019-04-26 08:00:30 -04:00
|
|
|
|
}
|
2019-08-15 08:07:28 -04:00
|
|
|
|
charsetEncoding, _ := stdcharset.Lookup(encoding)
|
2019-04-26 08:00:30 -04:00
|
|
|
|
if charsetEncoding == nil {
|
|
|
|
|
return "UTF-8", false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result, n, err := transform.String(charsetEncoding.NewDecoder(), string(buf))
|
2019-06-12 15:41:28 -04:00
|
|
|
|
if err != nil {
|
|
|
|
|
// return default
|
|
|
|
|
return "UTF-8", false
|
|
|
|
|
}
|
2019-04-26 08:00:30 -04:00
|
|
|
|
|
|
|
|
|
if n > 2 {
|
2019-08-15 08:07:28 -04:00
|
|
|
|
return encoding, bytes.Equal([]byte(result)[0:3], charset.UTF8BOM)
|
2019-04-26 08:00:30 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return encoding, false
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-29 05:41:35 -04:00
|
|
|
|
// 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) {
|
2020-10-13 14:50:57 -04:00
|
|
|
|
// If no branch name is set, assume default branch
|
2019-04-17 12:06:35 -04:00
|
|
|
|
if opts.OldBranch == "" {
|
|
|
|
|
opts.OldBranch = repo.DefaultBranch
|
|
|
|
|
}
|
|
|
|
|
if opts.NewBranch == "" {
|
|
|
|
|
opts.NewBranch = opts.OldBranch
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-19 18:26:57 -05:00
|
|
|
|
gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer closer.Close()
|
|
|
|
|
|
2019-04-17 12:06:35 -04:00
|
|
|
|
// oldBranch must exist for this operation
|
2022-03-28 15:48:41 -04:00
|
|
|
|
if _, err := gitRepo.GetBranch(opts.OldBranch); err != nil && !repo.IsEmpty {
|
2019-04-17 12:06:35 -04:00
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-29 05:41:35 -04:00
|
|
|
|
treePaths := []string{}
|
|
|
|
|
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,
|
|
|
|
|
encoding: "UTF-8",
|
|
|
|
|
bom: false,
|
|
|
|
|
executable: false,
|
|
|
|
|
}
|
|
|
|
|
treePaths = append(treePaths, treePath)
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-17 12:06:35 -04:00
|
|
|
|
// 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-19 18:26:57 -05:00
|
|
|
|
existingBranch, err := gitRepo.GetBranch(opts.NewBranch)
|
2019-04-17 12:06:35 -04:00
|
|
|
|
if existingBranch != nil {
|
|
|
|
|
return nil, models.ErrBranchAlreadyExists{
|
|
|
|
|
BranchName: opts.NewBranch,
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-19 08:17:27 -04:00
|
|
|
|
if err != nil && !git.IsErrBranchNotExist(err) {
|
2019-04-17 12:06:35 -04:00
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2023-05-29 05:41:35 -04:00
|
|
|
|
} else if err := VerifyBranchProtection(ctx, repo, doer, opts.OldBranch, treePaths); err != nil {
|
2021-09-11 10:21:17 -04:00
|
|
|
|
return nil, err
|
2019-04-17 12:06:35 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
message := strings.TrimSpace(opts.Message)
|
|
|
|
|
|
2019-12-09 08:11:24 -05:00
|
|
|
|
author, committer := GetAuthorAndCommitterUsers(opts.Author, opts.Committer, doer)
|
2019-04-17 12:06:35 -04:00
|
|
|
|
|
2022-01-19 18:26:57 -05:00
|
|
|
|
t, err := NewTemporaryUploadRepository(ctx, repo)
|
2019-04-17 12:06:35 -04:00
|
|
|
|
if err != nil {
|
2019-06-12 15:41:28 -04:00
|
|
|
|
log.Error("%v", err)
|
2019-04-17 12:06:35 -04:00
|
|
|
|
}
|
2019-06-12 15:41:28 -04:00
|
|
|
|
defer t.Close()
|
2022-03-28 15:48:41 -04:00
|
|
|
|
hasOldBranch := true
|
2019-04-17 12:06:35 -04:00
|
|
|
|
if err := t.Clone(opts.OldBranch); err != nil {
|
2023-05-29 05:41:35 -04:00
|
|
|
|
for _, file := range opts.Files {
|
|
|
|
|
if file.Operation == "delete" {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-28 15:48:41 -04:00
|
|
|
|
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 12:06:35 -04:00
|
|
|
|
}
|
2022-03-28 15:48:41 -04:00
|
|
|
|
if hasOldBranch {
|
|
|
|
|
if err := t.SetDefaultIndex(); err != nil {
|
|
|
|
|
return nil, err
|
2019-08-05 16:39:39 -04:00
|
|
|
|
}
|
2019-04-17 12:06:35 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-29 05:41:35 -04:00
|
|
|
|
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 08:00:30 -04:00
|
|
|
|
|
2022-03-28 15:48:41 -04:00
|
|
|
|
if hasOldBranch {
|
|
|
|
|
// Get the commit of the original branch
|
|
|
|
|
commit, err := t.GetBranchCommit(opts.OldBranch)
|
2019-04-17 12:06:35 -04:00
|
|
|
|
if err != nil {
|
2022-03-28 15:48:41 -04:00
|
|
|
|
return nil, err // Couldn't get a commit for the branch
|
2019-04-17 12:06:35 -04:00
|
|
|
|
}
|
2022-03-28 15:48:41 -04:00
|
|
|
|
|
|
|
|
|
// 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-24 15:29:17 -04:00
|
|
|
|
return nil, fmt.Errorf("ConvertToSHA1: Invalid last commit ID: %w", err)
|
2019-04-17 12:06:35 -04:00
|
|
|
|
}
|
2022-03-28 15:48:41 -04:00
|
|
|
|
opts.LastCommitID = lastCommitID.String()
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-29 05:41:35 -04:00
|
|
|
|
for _, file := range opts.Files {
|
|
|
|
|
if err := handleCheckErrors(file, commit, opts, repo); err != nil {
|
2022-03-28 15:48:41 -04:00
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2023-05-29 05:41:35 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
return nil, fmt.Errorf("Invalid file operation: %s %s, supported operations are create, update, delete", file.Operation, file.Options.treePath)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
filesReponse, err := GetFilesResponseFromCommit(ctx, repo, commit, opts.NewBranch, treePaths)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if repo.IsEmpty {
|
|
|
|
|
_ = repo_model.UpdateRepositoryCols(ctx, &repo_model.Repository{ID: repo.ID, IsEmpty: false}, "is_empty")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return filesReponse, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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-28 15:48:41 -04:00
|
|
|
|
}
|
2023-05-29 05:41:35 -04:00
|
|
|
|
}
|
|
|
|
|
} 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 12:06:35 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-29 05:41:35 -04:00
|
|
|
|
// The file wasn't modified, so we are good to delete it
|
2019-04-17 12:06:35 -04:00
|
|
|
|
}
|
2023-05-29 05:41:35 -04:00
|
|
|
|
} 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 12:06:35 -04:00
|
|
|
|
}
|
2023-05-29 05:41:35 -04:00
|
|
|
|
file.Options.encoding, file.Options.bom = detectEncodingAndBOM(fromEntry, repo)
|
|
|
|
|
file.Options.executable = fromEntry.IsExecutable()
|
|
|
|
|
}
|
|
|
|
|
if file.Operation == "create" || file.Operation == "update" {
|
2022-03-28 15:48:41 -04:00
|
|
|
|
// 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 05:41:35 -04:00
|
|
|
|
treePathParts := strings.Split(file.Options.treePath, "/")
|
2022-03-28 15:48:41 -04:00
|
|
|
|
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 05:41:35 -04:00
|
|
|
|
return err
|
2019-04-17 12:06:35 -04:00
|
|
|
|
}
|
2022-03-28 15:48:41 -04:00
|
|
|
|
if index < len(treePathParts)-1 {
|
|
|
|
|
if !entry.IsDir() {
|
2023-05-29 05:41:35 -04:00
|
|
|
|
return models.ErrFilePathInvalid{
|
2022-03-28 15:48:41 -04:00
|
|
|
|
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 05:41:35 -04:00
|
|
|
|
return models.ErrFilePathInvalid{
|
2022-03-28 15:48:41 -04:00
|
|
|
|
Message: fmt.Sprintf("a symbolic link exists where you’re trying to create a subdirectory [path: %s]", subTreePath),
|
2019-04-17 12:06:35 -04:00
|
|
|
|
Path: subTreePath,
|
|
|
|
|
Name: part,
|
2022-03-28 15:48:41 -04:00
|
|
|
|
Type: git.EntryModeSymlink,
|
|
|
|
|
}
|
|
|
|
|
} else if entry.IsDir() {
|
2023-05-29 05:41:35 -04:00
|
|
|
|
return models.ErrFilePathInvalid{
|
2022-03-28 15:48:41 -04:00
|
|
|
|
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 05:41:35 -04:00
|
|
|
|
} else if file.Options.fromTreePath != file.Options.treePath || file.Operation == "create" {
|
2022-03-28 15:48:41 -04:00
|
|
|
|
// The entry shouldn't exist if we are creating new file or moving to a new path
|
2023-05-29 05:41:35 -04:00
|
|
|
|
return models.ErrRepoFileAlreadyExists{
|
|
|
|
|
Path: file.Options.treePath,
|
2019-04-17 12:06:35 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-28 15:48:41 -04:00
|
|
|
|
}
|
2019-04-17 12:06:35 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-29 05:41:35 -04:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// handle creating or updating a file for ChangeRepoFiles
|
|
|
|
|
func CreateOrUpdateFile(ctx context.Context, t *TemporaryUploadRepository, file *ChangeRepoFile, contentStore *lfs.ContentStore, repoID int64, hasOldBranch bool) error {
|
2019-04-17 12:06:35 -04:00
|
|
|
|
// Get the two paths (might be the same if not moving) from the index if they exist
|
2023-05-29 05:41:35 -04:00
|
|
|
|
filesInIndex, err := t.LsFiles(file.TreePath, file.FromTreePath)
|
2019-04-17 12:06:35 -04:00
|
|
|
|
if err != nil {
|
2023-05-29 05:41:35 -04:00
|
|
|
|
return fmt.Errorf("UpdateRepoFile: %w", err)
|
2019-04-17 12:06:35 -04:00
|
|
|
|
}
|
|
|
|
|
// If is a new file (not updating) then the given path shouldn't exist
|
2023-05-29 05:41:35 -04:00
|
|
|
|
if file.Operation == "create" {
|
|
|
|
|
for _, indexFile := range filesInIndex {
|
|
|
|
|
if indexFile == file.TreePath {
|
|
|
|
|
return models.ErrRepoFileAlreadyExists{
|
|
|
|
|
Path: file.TreePath,
|
2019-04-17 12:06:35 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove the old path from the tree
|
2023-05-29 05:41:35 -04:00
|
|
|
|
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 12:06:35 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-29 05:41:35 -04:00
|
|
|
|
content := file.Content
|
|
|
|
|
if file.Options.bom {
|
2019-08-15 08:07:28 -04:00
|
|
|
|
content = string(charset.UTF8BOM) + content
|
2019-04-26 08:00:30 -04:00
|
|
|
|
}
|
2023-05-29 05:41:35 -04:00
|
|
|
|
if file.Options.encoding != "UTF-8" {
|
|
|
|
|
charsetEncoding, _ := stdcharset.Lookup(file.Options.encoding)
|
2019-04-26 08:00:30 -04:00
|
|
|
|
if charsetEncoding != nil {
|
2019-07-23 14:50:39 -04:00
|
|
|
|
result, _, err := transform.String(charsetEncoding.NewEncoder(), content)
|
2019-04-26 08:00:30 -04:00
|
|
|
|
if err != nil {
|
|
|
|
|
// Look if we can't encode back in to the original we should just stick with utf-8
|
2023-05-29 05:41:35 -04:00
|
|
|
|
log.Error("Error re-encoding %s (%s) as %s - will stay as UTF-8: %v", file.TreePath, file.FromTreePath, file.Options.encoding, err)
|
2019-04-26 08:00:30 -04:00
|
|
|
|
result = content
|
|
|
|
|
}
|
|
|
|
|
content = result
|
|
|
|
|
} else {
|
2023-05-29 05:41:35 -04:00
|
|
|
|
log.Error("Unknown encoding: %s", file.Options.encoding)
|
2019-04-26 08:00:30 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Reset the opts.Content to our adjusted content to ensure that LFS gets the correct content
|
2023-05-29 05:41:35 -04:00
|
|
|
|
file.Content = content
|
2022-06-12 11:51:54 -04:00
|
|
|
|
var lfsMetaObject *git_model.LFSMetaObject
|
2019-04-17 12:06:35 -04:00
|
|
|
|
|
2022-03-28 15:48:41 -04:00
|
|
|
|
if setting.LFS.StartServer && hasOldBranch {
|
2019-10-11 20:13:27 -04:00
|
|
|
|
// Check there is no way this can return multiple infos
|
2021-03-01 07:14:17 -05:00
|
|
|
|
filename2attribute2info, err := t.gitRepo.CheckAttribute(git.CheckAttributeOpts{
|
Refactor git command package to improve security and maintainability (#22678)
This PR follows #21535 (and replace #22592)
## Review without space diff
https://github.com/go-gitea/gitea/pull/22678/files?diff=split&w=1
## Purpose of this PR
1. Make git module command completely safe (risky user inputs won't be
passed as argument option anymore)
2. Avoid low-level mistakes like
https://github.com/go-gitea/gitea/pull/22098#discussion_r1045234918
3. Remove deprecated and dirty `CmdArgCheck` function, hide the `CmdArg`
type
4. Simplify code when using git command
## The main idea of this PR
* Move the `git.CmdArg` to the `internal` package, then no other package
except `git` could use it. Then developers could never do
`AddArguments(git.CmdArg(userInput))` any more.
* Introduce `git.ToTrustedCmdArgs`, it's for user-provided and already
trusted arguments. It's only used in a few cases, for example: use git
arguments from config file, help unit test with some arguments.
* Introduce `AddOptionValues` and `AddOptionFormat`, they make code more
clear and simple:
* Before: `AddArguments("-m").AddDynamicArguments(message)`
* After: `AddOptionValues("-m", message)`
* -
* Before: `AddArguments(git.CmdArg(fmt.Sprintf("--author='%s <%s>'",
sig.Name, sig.Email)))`
* After: `AddOptionFormat("--author='%s <%s>'", sig.Name, sig.Email)`
## FAQ
### Why these changes were not done in #21535 ?
#21535 is mainly a search&replace, it did its best to not change too
much logic.
Making the framework better needs a lot of changes, so this separate PR
is needed as the second step.
### The naming of `AddOptionXxx`
According to git's manual, the `--xxx` part is called `option`.
### How can it guarantee that `internal.CmdArg` won't be not misused?
Go's specification guarantees that. Trying to access other package's
internal package causes compilation error.
And, `golangci-lint` also denies the git/internal package. Only the
`git/command.go` can use it carefully.
### There is still a `ToTrustedCmdArgs`, will it still allow developers
to make mistakes and pass untrusted arguments?
Generally speaking, no. Because when using `ToTrustedCmdArgs`, the code
will be very complex (see the changes for examples). Then developers and
reviewers can know that something might be unreasonable.
### Why there was a `CmdArgCheck` and why it's removed?
At the moment of #21535, to reduce unnecessary changes, `CmdArgCheck`
was introduced as a hacky patch. Now, almost all code could be written
as `cmd := NewCommand(); cmd.AddXxx(...)`, then there is no need for
`CmdArgCheck` anymore.
### Why many codes for `signArg == ""` is deleted?
Because in the old code, `signArg` could never be empty string, it's
either `-S[key-id]` or `--no-gpg-sign`. So the `signArg == ""` is just
dead code.
---------
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2023-02-03 21:30:43 -05:00
|
|
|
|
Attributes: []string{"filter"},
|
2023-05-29 05:41:35 -04:00
|
|
|
|
Filenames: []string{file.Options.treePath},
|
2022-01-18 02:44:30 -05:00
|
|
|
|
CachedOnly: true,
|
2021-03-01 07:14:17 -05:00
|
|
|
|
})
|
2019-04-17 12:06:35 -04:00
|
|
|
|
if err != nil {
|
2023-05-29 05:41:35 -04:00
|
|
|
|
return err
|
2019-04-17 12:06:35 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-29 05:41:35 -04:00
|
|
|
|
if filename2attribute2info[file.Options.treePath] != nil && filename2attribute2info[file.Options.treePath]["filter"] == "lfs" {
|
2019-10-11 20:13:27 -04:00
|
|
|
|
// OK so we are supposed to LFS this data!
|
2023-05-29 05:41:35 -04:00
|
|
|
|
pointer, err := lfs.GeneratePointer(strings.NewReader(file.Content))
|
2019-10-11 20:13:27 -04:00
|
|
|
|
if err != nil {
|
2023-05-29 05:41:35 -04:00
|
|
|
|
return err
|
2019-10-11 20:13:27 -04:00
|
|
|
|
}
|
2023-05-29 05:41:35 -04:00
|
|
|
|
lfsMetaObject = &git_model.LFSMetaObject{Pointer: pointer, RepositoryID: repoID}
|
2021-04-08 18:25:57 -04:00
|
|
|
|
content = pointer.StringContent()
|
2019-10-11 20:13:27 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-29 05:41:35 -04:00
|
|
|
|
|
2019-04-17 12:06:35 -04:00
|
|
|
|
// Add the object to the database
|
|
|
|
|
objectHash, err := t.HashObject(strings.NewReader(content))
|
|
|
|
|
if err != nil {
|
2023-05-29 05:41:35 -04:00
|
|
|
|
return err
|
2019-04-17 12:06:35 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add the object to the index
|
2023-05-29 05:41:35 -04:00
|
|
|
|
if file.Options.executable {
|
|
|
|
|
if err := t.AddObjectToIndex("100755", objectHash, file.Options.treePath); err != nil {
|
|
|
|
|
return err
|
2020-03-04 18:46:12 -05:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2023-05-29 05:41:35 -04:00
|
|
|
|
if err := t.AddObjectToIndex("100644", objectHash, file.Options.treePath); err != nil {
|
|
|
|
|
return err
|
2020-03-04 18:46:12 -05:00
|
|
|
|
}
|
2019-04-17 12:06:35 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if lfsMetaObject != nil {
|
|
|
|
|
// We have an LFS object - create it
|
2023-01-08 22:50:54 -05:00
|
|
|
|
lfsMetaObject, err = git_model.NewLFSMetaObject(ctx, lfsMetaObject)
|
2019-04-17 12:06:35 -04:00
|
|
|
|
if err != nil {
|
2023-05-29 05:41:35 -04:00
|
|
|
|
return err
|
2019-04-17 12:06:35 -04:00
|
|
|
|
}
|
2021-04-08 18:25:57 -04:00
|
|
|
|
exist, err := contentStore.Exists(lfsMetaObject.Pointer)
|
2020-09-08 11:45:10 -04:00
|
|
|
|
if err != nil {
|
2023-05-29 05:41:35 -04:00
|
|
|
|
return err
|
2020-09-08 11:45:10 -04:00
|
|
|
|
}
|
|
|
|
|
if !exist {
|
2023-05-29 05:41:35 -04:00
|
|
|
|
if err := contentStore.Put(lfsMetaObject.Pointer, strings.NewReader(file.Content)); err != nil {
|
|
|
|
|
if _, err2 := git_model.RemoveLFSMetaObjectByOid(ctx, repoID, lfsMetaObject.Oid); err2 != nil {
|
|
|
|
|
return fmt.Errorf("Error whilst removing failed inserted LFS object %s: %v (Prev Error: %w)", lfsMetaObject.Oid, err2, err)
|
2019-04-17 12:06:35 -04:00
|
|
|
|
}
|
2023-05-29 05:41:35 -04:00
|
|
|
|
return err
|
2019-04-17 12:06:35 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-29 05:41:35 -04:00
|
|
|
|
return nil
|
2019-04-17 12:06:35 -04:00
|
|
|
|
}
|
2021-09-11 10:21:17 -04:00
|
|
|
|
|
|
|
|
|
// VerifyBranchProtection verify the branch protection for modifying the given treePath on the given branch
|
2023-05-29 05:41:35 -04:00
|
|
|
|
func VerifyBranchProtection(ctx context.Context, repo *repo_model.Repository, doer *user_model.User, branchName string, treePaths []string) error {
|
2023-01-16 03:00:22 -05:00
|
|
|
|
protectedBranch, err := git_model.GetFirstMatchProtectedBranchRule(ctx, repo.ID, branchName)
|
2021-09-11 10:21:17 -04:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if protectedBranch != nil {
|
2023-01-16 03:00:22 -05:00
|
|
|
|
protectedBranch.Repo = repo
|
2023-05-29 05:41:35 -04:00
|
|
|
|
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 10:21:17 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if protectedBranch.RequireSignedCommits {
|
2022-01-19 18:26:57 -05:00
|
|
|
|
_, _, _, err := asymkey_service.SignCRUDAction(ctx, repo.RepoPath(), doer, repo.RepoPath(), branchName)
|
2021-09-11 10:21:17 -04:00
|
|
|
|
if err != nil {
|
2021-12-10 03:14:24 -05:00
|
|
|
|
if !asymkey_service.IsErrWontSign(err) {
|
2021-09-11 10:21:17 -04:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return models.ErrUserCannotCommit{
|
|
|
|
|
UserName: doer.LowerName,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|