@@ -21,7 +21,6 @@ import (
21
21
22
22
"code.gitea.io/git"
23
23
24
- "code.gitea.io/gitea/modules/log"
25
24
"code.gitea.io/gitea/modules/process"
26
25
"code.gitea.io/gitea/modules/setting"
27
26
)
@@ -712,64 +711,59 @@ func (repo *Repository) UploadRepoFiles(doer *User, opts UploadRepoFileOptions)
712
711
return fmt .Errorf ("GetUploadsByUUIDs [uuids: %v]: %v" , opts .Files , err )
713
712
}
714
713
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 )
722
718
}
723
719
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 ))
729
721
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
+ }
732
727
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 )
735
731
}
736
732
737
733
// Copy uploaded files into repository.
738
734
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
743
738
}
739
+ defer file .Close ()
744
740
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
747
744
}
748
- }
749
745
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
+ }
762
750
}
763
751
764
- gitRepo , err := git .OpenRepository (repo .RepoPath ())
752
+ // Now write the tree
753
+ treeHash , err := repo .writeTree (tmpBasePath )
765
754
if err != nil {
766
- log .Error (4 , "OpenRepository: %v" , err )
767
- return nil
755
+ return err
768
756
}
769
- commit , err := gitRepo .GetBranchCommit (opts .NewBranch )
757
+
758
+ // Now commit the tree
759
+ commitHash , err := repo .commitTree (tmpBasePath , doer , treeHash , opts .Message )
770
760
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
773
767
}
774
768
775
769
// Simulate push event.
@@ -790,12 +784,13 @@ func (repo *Repository) UploadRepoFiles(doer *User, opts UploadRepoFileOptions)
790
784
RepoName : repo .Name ,
791
785
RefFullName : git .BranchPrefix + opts .NewBranch ,
792
786
OldCommitID : oldCommitID ,
793
- NewCommitID : commit . ID . String () ,
787
+ NewCommitID : commitHash ,
794
788
},
795
789
)
796
790
if err != nil {
797
791
return fmt .Errorf ("PushUpdate: %v" , err )
798
792
}
799
793
794
+ // FIXME: Should we UpdateRepoIndexer(repo) here?
800
795
return DeleteUploads (uploads ... )
801
796
}
0 commit comments