@@ -100,95 +100,76 @@ func ForkRepository(ctx context.Context, doer, owner *user_model.User, opts Fork
100100 IsFork : true ,
101101 ForkID : opts .BaseRepo .ID ,
102102 ObjectFormatName : opts .BaseRepo .ObjectFormatName ,
103+ Status : repo_model .RepositoryBeingMigrated ,
103104 }
104105
105- oldRepoPath := opts .BaseRepo .RepoPath ()
106-
107- needsRollback := false
108- rollbackFn := func () {
109- if ! needsRollback {
110- return
111- }
112-
113- if exists , _ := gitrepo .IsRepositoryExist (ctx , repo ); ! exists {
114- return
115- }
116-
117- // As the transaction will be failed and hence database changes will be destroyed we only need
118- // to delete the related repository on the filesystem
119- if errDelete := gitrepo .DeleteRepository (ctx , repo ); errDelete != nil {
120- log .Error ("Failed to remove fork repo" )
121- }
122- }
123-
124- needsRollbackInPanic := true
125- defer func () {
126- panicErr := recover ()
127- if panicErr == nil {
128- return
129- }
130-
131- if needsRollbackInPanic {
132- rollbackFn ()
133- }
134- panic (panicErr )
135- }()
136-
137- err = db .WithTx (ctx , func (txCtx context.Context ) error {
138- if err = CreateRepositoryByExample (txCtx , doer , owner , repo , false , true ); err != nil {
106+ // 1 - Create the repository in the database
107+ err = db .WithTx (ctx , func (ctx context.Context ) error {
108+ if err = CreateRepositoryInDB (ctx , doer , owner , repo , false , true ); err != nil {
139109 return err
140110 }
141-
142- if err = repo_model .IncrementRepoForkNum (txCtx , opts .BaseRepo .ID ); err != nil {
111+ if err = repo_model .IncrementRepoForkNum (ctx , opts .BaseRepo .ID ); err != nil {
143112 return err
144113 }
145114
146115 // copy lfs files failure should not be ignored
147- if err = git_model .CopyLFS (txCtx , repo , opts .BaseRepo ); err != nil {
148- return err
149- }
150-
151- needsRollback = true
116+ return git_model .CopyLFS (ctx , repo , opts .BaseRepo )
117+ })
118+ if err != nil {
119+ return nil , err
120+ }
152121
153- cloneCmd := git .NewCommand ("clone" , "--bare" )
154- if opts .SingleBranch != "" {
155- cloneCmd .AddArguments ("--single-branch" , "--branch" ).AddDynamicArguments (opts .SingleBranch )
156- }
157- if stdout , _ , err := cloneCmd .AddDynamicArguments (oldRepoPath , repo .RepoPath ()).
158- RunStdBytes (txCtx , & git.RunOpts {Timeout : 10 * time .Minute }); err != nil {
159- log .Error ("Fork Repository (git clone) Failed for %v (from %v):\n Stdout: %s\n Error: %v" , repo , opts .BaseRepo , stdout , err )
160- return fmt .Errorf ("git clone: %w" , err )
122+ // last - clean up if something goes wrong
123+ defer func () {
124+ if err != nil {
125+ if errDelete := gitrepo .DeleteRepository (ctx , repo ); errDelete != nil {
126+ log .Error ("Failed to remove fork repo" )
127+ }
161128 }
129+ }()
162130
163- if err := repo_module .CheckDaemonExportOK (txCtx , repo ); err != nil {
164- return fmt .Errorf ("checkDaemonExportOK: %w" , err )
165- }
131+ // 2 - Clone the repository
132+ cloneCmd := git .NewCommand ("clone" , "--bare" )
133+ if opts .SingleBranch != "" {
134+ cloneCmd .AddArguments ("--single-branch" , "--branch" ).AddDynamicArguments (opts .SingleBranch )
135+ }
136+ if stdout , _ , err := cloneCmd .AddDynamicArguments (opts .BaseRepo .RepoPath (), repo .RepoPath ()).
137+ RunStdBytes (ctx , & git.RunOpts {Timeout : 10 * time .Minute }); err != nil {
138+ log .Error ("Fork Repository (git clone) Failed for %v (from %v):\n Stdout: %s\n Error: %v" , repo , opts .BaseRepo , stdout , err )
139+ return nil , fmt .Errorf ("git clone: %w" , err )
140+ }
166141
167- if stdout , _ , err := git .NewCommand ("update-server-info" ).
168- RunStdString (txCtx , & git.RunOpts {Dir : repo .RepoPath ()}); err != nil {
169- log .Error ("Fork Repository (git update-server-info) failed for %v:\n Stdout: %s\n Error: %v" , repo , stdout , err )
170- return fmt .Errorf ("git update-server-info: %w" , err )
171- }
142+ // 3 - Update the repository
143+ if err := repo_module .CheckDaemonExportOK (ctx , repo ); err != nil {
144+ return nil , fmt .Errorf ("checkDaemonExportOK: %w" , err )
145+ }
172146
173- if err = gitrepo .CreateDelegateHooks (ctx , repo ); err != nil {
174- return fmt .Errorf ("createDelegateHooks: %w" , err )
175- }
147+ if stdout , _ , err := git .NewCommand ("update-server-info" ).
148+ RunStdString (ctx , & git.RunOpts {Dir : repo .RepoPath ()}); err != nil {
149+ log .Error ("Fork Repository (git update-server-info) failed for %v:\n Stdout: %s\n Error: %v" , repo , stdout , err )
150+ return nil , fmt .Errorf ("git update-server-info: %w" , err )
151+ }
176152
177- gitRepo , err := gitrepo .OpenRepository (txCtx , repo )
178- if err != nil {
179- return fmt .Errorf ("OpenRepository: %w" , err )
180- }
181- defer gitRepo .Close ()
153+ // 4 - Create hooks
154+ if err = gitrepo .CreateDelegateHooks (ctx , repo ); err != nil {
155+ return nil , fmt .Errorf ("createDelegateHooks: %w" , err )
156+ }
182157
183- _ , err = repo_module .SyncRepoBranchesWithRepo (txCtx , repo , gitRepo , doer .ID )
184- return err
185- })
186- needsRollbackInPanic = false
158+ // 5 - Sync the repository branches and tags
159+ gitRepo , err := gitrepo .OpenRepository (ctx , repo )
187160 if err != nil {
188- rollbackFn ()
189- return nil , err
161+ return nil , fmt .Errorf ("OpenRepository: %w" , err )
190162 }
163+ defer gitRepo .Close ()
191164
165+ if _ , err = repo_module .SyncRepoBranchesWithRepo (ctx , repo , gitRepo , doer .ID ); err != nil {
166+ return nil , fmt .Errorf ("SyncRepoBranchesWithRepo: %w" , err )
167+ }
168+ if err := repo_module .SyncReleasesWithTags (ctx , repo , gitRepo ); err != nil {
169+ return nil , fmt .Errorf ("Sync releases from git tags failed: %v" , err )
170+ }
171+
172+ // 6 - Update the repository
192173 // even if below operations failed, it could be ignored. And they will be retried
193174 if err := repo_module .UpdateRepoSize (ctx , repo ); err != nil {
194175 log .Error ("Failed to update size for repository: %v" , err )
@@ -200,14 +181,10 @@ func ForkRepository(ctx context.Context, doer, owner *user_model.User, opts Fork
200181 return nil , err
201182 }
202183
203- gitRepo , err := gitrepo .OpenRepository (ctx , repo )
204- if err != nil {
205- log .Error ("Open created git repository failed: %v" , err )
206- } else {
207- defer gitRepo .Close ()
208- if err := repo_module .SyncReleasesWithTags (ctx , repo , gitRepo ); err != nil {
209- log .Error ("Sync releases from git tags failed: %v" , err )
210- }
184+ // 7 - update repository status to be ready
185+ repo .Status = repo_model .RepositoryReady
186+ if err = repo_model .UpdateRepositoryCols (ctx , repo , "status" ); err != nil {
187+ return nil , fmt .Errorf ("UpdateRepositoryCols: %w" , err )
211188 }
212189
213190 notify_service .ForkRepository (ctx , doer , opts .BaseRepo , repo )
0 commit comments