From 76afc3caa2cc9c6f3a1df5766f14836728a4cb4a Mon Sep 17 00:00:00 2001 From: davrot Date: Sun, 26 Jan 2025 19:00:15 +0000 Subject: [PATCH] =?UTF-8?q?Dateien=20nach=20=E2=80=9Eservices/repository/f?= =?UTF-8?q?iles=E2=80=9C=20hochladen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- services/repository/files/update.go | 112 +++++++++++++++++++++------- 1 file changed, 87 insertions(+), 25 deletions(-) diff --git a/services/repository/files/update.go b/services/repository/files/update.go index d6025b6ced..4fdb625cf6 100644 --- a/services/repository/files/update.go +++ b/services/repository/files/update.go @@ -10,6 +10,7 @@ import ( "path" "strings" "time" + "errors" "code.gitea.io/gitea/models" git_model "code.gitea.io/gitea/models/git" @@ -56,6 +57,7 @@ type ChangeRepoFilesOptions struct { Committer *IdentityOptions Dates *CommitDateOptions Signoff bool + IsDir bool `default:"false"` } type RepoFileOptions struct { @@ -91,33 +93,64 @@ func ChangeRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *use } var 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 + if opts.IsDir { + var newFiles []*ChangeRepoFile + for _, file := range opts.Files { + if file.Operation != "delete" { + return nil, errors.New("invalid operation: only delete is allowed for directory paths") } + // Clean up and validate paths + treePath := CleanUploadFileName(file.TreePath) + if treePath == "" { + return nil, models.ErrFilenameInvalid{ + Path: file.TreePath, + } + } + dirFiles, err := ListFilesInDirectory(ctx, gitRepo, treePath) + if err != nil { + return nil, err + } + for _, dirFile := range dirFiles { + treePaths = append(treePaths, dirFile) + newFiles = append(newFiles, &ChangeRepoFile{ + TreePath: dirFile, + Options: &RepoFileOptions{ + treePath: dirFile, + fromTreePath: "", + executable: false, + }, + }) + } + } + opts.Files = newFiles + } else { + 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, + } + } - // 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) + file.Options = &RepoFileOptions{ + treePath: treePath, + fromTreePath: fromTreePath, + executable: false, + } + treePaths = append(treePaths, treePath) + } } // A NewBranch can be specified for the file to be created/updated in a new branch. @@ -276,6 +309,35 @@ func ChangeRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *use return filesResponse, nil } + +// Helper function to recursively list files in a directory +func ListFilesInDirectory(ctx context.Context, gitRepo *git.Repository, dirPath string) ([]string, error) { + var files []string +// +// // Implement recursive directory traversal +// // This would use gitRepo methods to list files +// // For example: +// commit, err := gitRepo.GetBranchCommit(gitRepo.DefaultBranch) +// if err != nil { +// return nil, err +// } + +// tree, err := commit.SubTree(dirPath) +// if err != nil { +// return nil, err +// } +// +// err = tree.Walk(func(path string, entry *git.TreeEntry) error { +// if entry.Type == git.EntryBlob { +// files = append(files, filepath.Join(dirPath, path)) +// } +// return nil +// }) + +// return files, err + return files, nil +} + // handles the check for various issues for ChangeRepoFiles func handleCheckErrors(file *ChangeRepoFile, commit *git.Commit, opts *ChangeRepoFilesOptions) error { if file.Operation == "update" || file.Operation == "delete" {