@@ -195,7 +195,39 @@ func CreatePendingRepositoryTransfer(doer, newOwner *User, repoID int64, teams [
195
195
}
196
196
197
197
// TransferOwnership transfers all corresponding repository items from old user to new one.
198
- func TransferOwnership (doer * User , newOwnerName string , repo * Repository ) error {
198
+ func TransferOwnership (doer * User , newOwnerName string , repo * Repository ) (err error ) {
199
+ repoRenamed := false
200
+ wikiRenamed := false
201
+ oldOwnerName := doer .Name
202
+
203
+ defer func () {
204
+ if ! repoRenamed && ! wikiRenamed {
205
+ return
206
+ }
207
+
208
+ recoverErr := recover ()
209
+ if err == nil && recoverErr == nil {
210
+ return
211
+ }
212
+
213
+ if repoRenamed {
214
+ if err := os .Rename (RepoPath (newOwnerName , repo .Name ), RepoPath (oldOwnerName , repo .Name )); err != nil {
215
+ log .Critical ("Unable to move repository %s/%s directory from %s back to correct place %s: %v" , oldOwnerName , repo .Name , RepoPath (newOwnerName , repo .Name ), RepoPath (oldOwnerName , repo .Name ), err )
216
+ }
217
+ }
218
+
219
+ if wikiRenamed {
220
+ if err := os .Rename (WikiPath (newOwnerName , repo .Name ), WikiPath (oldOwnerName , repo .Name )); err != nil {
221
+ log .Critical ("Unable to move wiki for repository %s/%s directory from %s back to correct place %s: %v" , oldOwnerName , repo .Name , WikiPath (newOwnerName , repo .Name ), WikiPath (oldOwnerName , repo .Name ), err )
222
+ }
223
+ }
224
+
225
+ if recoverErr != nil {
226
+ log .Error ("Panic within TransferOwnership: %v\n %s" , recoverErr , log .Stack (2 ))
227
+ panic (recoverErr )
228
+ }
229
+ }()
230
+
199
231
sess := x .NewSession ()
200
232
defer sess .Close ()
201
233
if err := sess .Begin (); err != nil {
@@ -206,6 +238,7 @@ func TransferOwnership(doer *User, newOwnerName string, repo *Repository) error
206
238
if err != nil {
207
239
return fmt .Errorf ("get new owner '%s': %v" , newOwnerName , err )
208
240
}
241
+ newOwnerName = newOwner .Name // ensure capitalisation matches
209
242
210
243
// Check if new owner has repository with same name.
211
244
if has , err := isRepositoryExist (sess , newOwner , repo .Name ); err != nil {
@@ -215,6 +248,7 @@ func TransferOwnership(doer *User, newOwnerName string, repo *Repository) error
215
248
}
216
249
217
250
oldOwner := repo .Owner
251
+ oldOwnerName = oldOwner .Name
218
252
219
253
// Note: we have to set value here to make sure recalculate accesses is based on
220
254
// new owner.
@@ -301,6 +335,7 @@ func TransferOwnership(doer *User, newOwnerName string, repo *Repository) error
301
335
if err := os .Rename (RepoPath (oldOwner .Name , repo .Name ), RepoPath (newOwner .Name , repo .Name )); err != nil {
302
336
return fmt .Errorf ("rename repository directory: %v" , err )
303
337
}
338
+ repoRenamed = true
304
339
305
340
// Rename remote wiki repository to new path and delete local copy.
306
341
wikiPath := WikiPath (oldOwner .Name , repo .Name )
@@ -312,6 +347,7 @@ func TransferOwnership(doer *User, newOwnerName string, repo *Repository) error
312
347
if err := os .Rename (wikiPath , WikiPath (newOwner .Name , repo .Name )); err != nil {
313
348
return fmt .Errorf ("rename repository wiki: %v" , err )
314
349
}
350
+ wikiRenamed = true
315
351
}
316
352
317
353
if err := deleteRepositoryTransfer (sess , repo .ID ); err != nil {
0 commit comments