Skip to content

Commit c4fde84

Browse files
committed
Use git plumbing for upload: #5621 repo_editor.go: UploadRepoFiles
1 parent 5394c64 commit c4fde84

File tree

1 file changed

+38
-43
lines changed

1 file changed

+38
-43
lines changed

models/repo_editor.go

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121

2222
"code.gitea.io/git"
2323

24-
"code.gitea.io/gitea/modules/log"
2524
"code.gitea.io/gitea/modules/process"
2625
"code.gitea.io/gitea/modules/setting"
2726
)
@@ -712,64 +711,59 @@ func (repo *Repository) UploadRepoFiles(doer *User, opts UploadRepoFileOptions)
712711
return fmt.Errorf("GetUploadsByUUIDs [uuids: %v]: %v", opts.Files, err)
713712
}
714713

715-
repoWorkingPool.CheckIn(com.ToStr(repo.ID))
716-
defer repoWorkingPool.CheckOut(com.ToStr(repo.ID))
717-
718-
if err = repo.DiscardLocalRepoBranchChanges(opts.OldBranch); err != nil {
719-
return fmt.Errorf("DiscardLocalRepoBranchChanges [branch: %s]: %v", opts.OldBranch, err)
720-
} else if err = repo.UpdateLocalCopyBranch(opts.OldBranch); err != nil {
721-
return fmt.Errorf("UpdateLocalCopyBranch [branch: %s]: %v", opts.OldBranch, err)
714+
timeStr := com.ToStr(time.Now().Nanosecond()) // SHOULD USE SOMETHING UNIQUE
715+
tmpBasePath := path.Join(LocalCopyPath(), "upload-"+timeStr+".git")
716+
if err := os.MkdirAll(path.Dir(tmpBasePath), os.ModePerm); err != nil {
717+
return fmt.Errorf("Failed to create dir %s: %v", tmpBasePath, err)
722718
}
723719

724-
if opts.OldBranch != opts.NewBranch {
725-
if err = repo.CheckoutNewBranch(opts.OldBranch, opts.NewBranch); err != nil {
726-
return fmt.Errorf("CheckoutNewBranch [old_branch: %s, new_branch: %s]: %v", opts.OldBranch, opts.NewBranch, err)
727-
}
728-
}
720+
defer os.RemoveAll(path.Dir(tmpBasePath))
729721

730-
localPath := repo.LocalCopyPath()
731-
dirPath := path.Join(localPath, opts.TreePath)
722+
// Do a bare shared clone into tmpBasePath and
723+
// make HEAD to point to the OldBranch tree
724+
if err := repo.bareClone(tmpBasePath, opts.OldBranch); err != nil {
725+
return fmt.Errorf("UpdateRepoFiles: %v", err)
726+
}
732727

733-
if err := os.MkdirAll(dirPath, os.ModePerm); err != nil {
734-
return fmt.Errorf("Failed to create dir %s: %v", dirPath, err)
728+
// Set the default index
729+
if err := repo.setDefaultIndex(tmpBasePath); err != nil {
730+
return fmt.Errorf("UpdateRepoFiles: %v", err)
735731
}
736732

737733
// Copy uploaded files into repository.
738734
for _, upload := range uploads {
739-
tmpPath := upload.LocalPath()
740-
targetPath := path.Join(dirPath, upload.Name)
741-
if !com.IsFile(tmpPath) {
742-
continue
735+
file, err := os.Open(upload.LocalPath())
736+
if err != nil {
737+
return err
743738
}
739+
defer file.Close()
744740

745-
if err = com.Copy(tmpPath, targetPath); err != nil {
746-
return fmt.Errorf("Copy: %v", err)
741+
objectHash, err := repo.hashObject(tmpBasePath, file)
742+
if err != nil {
743+
return err
747744
}
748-
}
749745

750-
if err = git.AddChanges(localPath, true); err != nil {
751-
return fmt.Errorf("git add --all: %v", err)
752-
} else if err = git.CommitChanges(localPath, git.CommitChangesOptions{
753-
Committer: doer.NewGitSig(),
754-
Message: opts.Message,
755-
}); err != nil {
756-
return fmt.Errorf("CommitChanges: %v", err)
757-
} else if err = git.Push(localPath, git.PushOptions{
758-
Remote: "origin",
759-
Branch: opts.NewBranch,
760-
}); err != nil {
761-
return fmt.Errorf("git push origin %s: %v", opts.NewBranch, err)
746+
// Add the object to the index
747+
if err := repo.addObjectToIndex(tmpBasePath, "100666", objectHash, path.Join(opts.TreePath, upload.Name)); err != nil {
748+
return err
749+
}
762750
}
763751

764-
gitRepo, err := git.OpenRepository(repo.RepoPath())
752+
// Now write the tree
753+
treeHash, err := repo.writeTree(tmpBasePath)
765754
if err != nil {
766-
log.Error(4, "OpenRepository: %v", err)
767-
return nil
755+
return err
768756
}
769-
commit, err := gitRepo.GetBranchCommit(opts.NewBranch)
757+
758+
// Now commit the tree
759+
commitHash, err := repo.commitTree(tmpBasePath, doer, treeHash, opts.Message)
770760
if err != nil {
771-
log.Error(4, "GetBranchCommit [branch: %s]: %v", opts.NewBranch, err)
772-
return nil
761+
return err
762+
}
763+
764+
// Then push this tree to NewBranch
765+
if err := repo.actuallyPush(tmpBasePath, doer, commitHash, opts.NewBranch); err != nil {
766+
return err
773767
}
774768

775769
// Simulate push event.
@@ -790,12 +784,13 @@ func (repo *Repository) UploadRepoFiles(doer *User, opts UploadRepoFileOptions)
790784
RepoName: repo.Name,
791785
RefFullName: git.BranchPrefix + opts.NewBranch,
792786
OldCommitID: oldCommitID,
793-
NewCommitID: commit.ID.String(),
787+
NewCommitID: commitHash,
794788
},
795789
)
796790
if err != nil {
797791
return fmt.Errorf("PushUpdate: %v", err)
798792
}
799793

794+
// FIXME: Should we UpdateRepoIndexer(repo) here?
800795
return DeleteUploads(uploads...)
801796
}

0 commit comments

Comments
 (0)