|
|
@@ -756,3 +756,267 @@ func createCommitRepoActions(repo *models.Repository, gitRepo *git.Repository, o |
|
|
|
} |
|
|
|
return actions, nil |
|
|
|
} |
|
|
|
|
|
|
|
// RenameRepoFileOptions |
|
|
|
type RenameRepoFileOptions struct { |
|
|
|
LastCommitID string |
|
|
|
BranchName string |
|
|
|
TreePath string |
|
|
|
FromTreePath string |
|
|
|
Message string |
|
|
|
Author *IdentityOptions |
|
|
|
Committer *IdentityOptions |
|
|
|
} |
|
|
|
|
|
|
|
// RenameRepoFile rename file in the given repository |
|
|
|
func RenameRepoFile(repo *models.Repository, doer *models.User, opts *RenameRepoFileOptions) error { |
|
|
|
|
|
|
|
// Branch must exist for this operation |
|
|
|
if _, err := repo_module.GetBranch(repo, opts.BranchName); err != nil { |
|
|
|
return 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 |
|
|
|
protectedBranch, err := repo.GetBranchProtection(opts.BranchName) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
if protectedBranch != nil { |
|
|
|
if !protectedBranch.CanUserPush(doer.ID) { |
|
|
|
return models.ErrUserCannotCommit{ |
|
|
|
UserName: doer.LowerName, |
|
|
|
} |
|
|
|
} |
|
|
|
if protectedBranch.RequireSignedCommits { |
|
|
|
_, _, err := repo.SignCRUDAction(doer, repo.RepoPath(), opts.BranchName) |
|
|
|
if err != nil { |
|
|
|
if !models.IsErrWontSign(err) { |
|
|
|
return err |
|
|
|
} |
|
|
|
return models.ErrUserCannotCommit{ |
|
|
|
UserName: doer.LowerName, |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
patterns := protectedBranch.GetProtectedFilePatterns() |
|
|
|
for _, pat := range patterns { |
|
|
|
if pat.Match(strings.ToLower(opts.TreePath)) { |
|
|
|
return models.ErrFilePathProtected{ |
|
|
|
Path: opts.TreePath, |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Check that the path given in opts.treePath is valid (not a git path) |
|
|
|
treePath := CleanUploadFileName(opts.TreePath) |
|
|
|
if treePath == "" { |
|
|
|
return 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 models.ErrFilenameInvalid{ |
|
|
|
Path: opts.FromTreePath, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
message := strings.TrimSpace(opts.Message) |
|
|
|
|
|
|
|
author, committer := GetAuthorAndCommitterUsers(opts.Author, opts.Committer, doer) |
|
|
|
|
|
|
|
t, err := NewTemporaryUploadRepository(repo) |
|
|
|
if err != nil { |
|
|
|
log.Error("%v", err) |
|
|
|
} |
|
|
|
defer t.Close() |
|
|
|
if err := t.Clone(opts.BranchName); err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
if err := t.SetDefaultIndex(); err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
// Get the commit of the original branch |
|
|
|
commit, err := t.GetBranchCommit(opts.BranchName) |
|
|
|
if err != nil { |
|
|
|
return err // Couldn't get a commit for the branch |
|
|
|
} |
|
|
|
|
|
|
|
lastCommitID, err := t.gitRepo.ConvertToSHA1(opts.LastCommitID) |
|
|
|
if err != nil { |
|
|
|
return fmt.Errorf("DeleteRepoFile: Invalid last commit ID: %v", err) |
|
|
|
} |
|
|
|
opts.LastCommitID = lastCommitID.String() |
|
|
|
|
|
|
|
//encoding := "UTF-8" |
|
|
|
//bom := false |
|
|
|
//executable := false |
|
|
|
|
|
|
|
fromTreeEntry, err := commit.GetTreeEntryByPath(fromTreePath) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
if opts.LastCommitID == "" { |
|
|
|
// 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{} |
|
|
|
} |
|
|
|
//获取 |
|
|
|
if fromTreeEntry.IsDir() { |
|
|
|
tree, err := commit.Tree.SubTree(fromTreePath) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
if err := moveAndAddDir(fromTreePath, treePath, opts.LastCommitID, tree, commit, t); err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
} else { |
|
|
|
if err := moveAndAddFile(opts.FromTreePath, opts.TreePath, opts.LastCommitID, commit, t); err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Now write the tree |
|
|
|
treeHash, err := t.WriteTree() |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
// Now commit the tree |
|
|
|
commitHash, err := t.CommitTree(author, committer, treeHash, message) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
// Then push this tree to NewBranch |
|
|
|
if err := t.Push(doer, commitHash, opts.BranchName); err != nil { |
|
|
|
log.Error("%T %v", err, err) |
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
func moveAndAddDir(oldTreePath, newTreePath, lastCommitID string, currentTree *git.Tree, currentBranchCommit *git.Commit, t *TemporaryUploadRepository) error { |
|
|
|
entries, err := currentTree.ListEntries() |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
for _, v := range entries { |
|
|
|
if v.IsDir() { |
|
|
|
subTree, err := currentTree.SubTree(v.Name()) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
if err := moveAndAddDir(path.Join(oldTreePath, v.Name()), path.Join(newTreePath, v.Name()), lastCommitID, subTree, currentBranchCommit, t); err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
} else { |
|
|
|
if err := moveAndAddFile(path.Join(oldTreePath, v.Name()), path.Join(newTreePath, v.Name()), lastCommitID, currentBranchCommit, t); err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
func moveAndAddFile(oldTreePath, newTreePath, lastCommitID string, currentBranchCommit *git.Commit, t *TemporaryUploadRepository) error { |
|
|
|
fromEntry, err := currentBranchCommit.GetTreeEntryByPath(oldTreePath) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
// 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 currentBranchCommit.ID.String() != lastCommitID { |
|
|
|
if changed, err := currentBranchCommit.FileChangedSinceCommit(oldTreePath, lastCommitID); err != nil { |
|
|
|
return err |
|
|
|
} else if changed { |
|
|
|
return models.ErrCommitIDDoesNotMatch{ |
|
|
|
GivenCommitID: lastCommitID, |
|
|
|
CurrentCommitID: lastCommitID, |
|
|
|
} |
|
|
|
} |
|
|
|
// The file wasn't modified, so we are good to delete it |
|
|
|
} |
|
|
|
executable := fromEntry.IsExecutable() |
|
|
|
|
|
|
|
// 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(newTreePath, "/") |
|
|
|
subTreePath := "" |
|
|
|
for index, part := range treePathParts { |
|
|
|
subTreePath = path.Join(subTreePath, part) |
|
|
|
entry, err := currentBranchCommit.GetTreeEntryByPath(subTreePath) |
|
|
|
if err != nil { |
|
|
|
if git.IsErrNotExist(err) { |
|
|
|
// Means there is no item with that name, so we're good |
|
|
|
break |
|
|
|
} |
|
|
|
return err |
|
|
|
} |
|
|
|
if index < len(treePathParts)-1 { |
|
|
|
if !entry.IsDir() { |
|
|
|
return 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() { |
|
|
|
return models.ErrFilePathInvalid{ |
|
|
|
Message: fmt.Sprintf("a symbolic link exists where you’re trying to create a subdirectory [path: %s]", subTreePath), |
|
|
|
Path: subTreePath, |
|
|
|
Name: part, |
|
|
|
Type: git.EntryModeSymlink, |
|
|
|
} |
|
|
|
} else if entry.IsDir() { |
|
|
|
return 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, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// Get the two paths (might be the same if not moving) from the index if they exist |
|
|
|
filesInIndex, err := t.LsFiles(newTreePath, oldTreePath) |
|
|
|
if err != nil { |
|
|
|
return fmt.Errorf("UpdateRepoFile: %v", err) |
|
|
|
} |
|
|
|
|
|
|
|
// Remove the old path from the tree |
|
|
|
if len(filesInIndex) > 0 { |
|
|
|
for _, file := range filesInIndex { |
|
|
|
if file == oldTreePath { |
|
|
|
if err := t.RemoveFilesFromIndex(oldTreePath); err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//获取oldTreePath的objectHash |
|
|
|
objectHash := fmt.Sprint(fromEntry.Blob().ID) |
|
|
|
|
|
|
|
// Add the object to the index |
|
|
|
if executable { |
|
|
|
if err := t.AddObjectToIndex("100755", objectHash, newTreePath); err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
} else { |
|
|
|
if err := t.AddObjectToIndex("100644", objectHash, newTreePath); err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
return nil |
|
|
|
} |