@@ -1290,11 +1290,44 @@ func IncrementRepoForkNum(ctx DBContext, repoID int64) error {
1290
1290
}
1291
1291
1292
1292
// TransferOwnership transfers all corresponding setting from old user to new one.
1293
- func TransferOwnership (doer * User , newOwnerName string , repo * Repository ) error {
1293
+ func TransferOwnership (doer * User , newOwnerName string , repo * Repository ) (err error ) {
1294
+ repoRenamed := false
1295
+ wikiRenamed := false
1296
+ oldOwnerName := doer .Name
1297
+
1298
+ defer func () {
1299
+ if ! repoRenamed && ! wikiRenamed {
1300
+ return
1301
+ }
1302
+
1303
+ recoverErr := recover ()
1304
+ if err == nil && recoverErr == nil {
1305
+ return
1306
+ }
1307
+
1308
+ if repoRenamed {
1309
+ if err := os .Rename (RepoPath (newOwnerName , repo .Name ), RepoPath (oldOwnerName , repo .Name )); err != nil {
1310
+ 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 )
1311
+ }
1312
+ }
1313
+
1314
+ if wikiRenamed {
1315
+ if err := os .Rename (WikiPath (newOwnerName , repo .Name ), WikiPath (oldOwnerName , repo .Name )); err != nil {
1316
+ 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 )
1317
+ }
1318
+ }
1319
+
1320
+ if recoverErr != nil {
1321
+ log .Error ("Panic within TransferOwnership: %v\n %s" , recoverErr , log .Stack (2 ))
1322
+ panic (recoverErr )
1323
+ }
1324
+ }()
1325
+
1294
1326
newOwner , err := GetUserByName (newOwnerName )
1295
1327
if err != nil {
1296
1328
return fmt .Errorf ("get new owner '%s': %v" , newOwnerName , err )
1297
1329
}
1330
+ newOwnerName = newOwner .Name // ensure capitalisation matches
1298
1331
1299
1332
// Check if new owner has repository with same name.
1300
1333
has , err := IsRepositoryExist (newOwner , repo .Name )
@@ -1311,6 +1344,7 @@ func TransferOwnership(doer *User, newOwnerName string, repo *Repository) error
1311
1344
}
1312
1345
1313
1346
oldOwner := repo .Owner
1347
+ oldOwnerName = oldOwner .Name
1314
1348
1315
1349
// Note: we have to set value here to make sure recalculate accesses is based on
1316
1350
// new owner.
@@ -1370,9 +1404,9 @@ func TransferOwnership(doer *User, newOwnerName string, repo *Repository) error
1370
1404
}
1371
1405
1372
1406
// Update repository count.
1373
- if _ , err = sess .Exec ("UPDATE `user` SET num_repos=num_repos+1 WHERE id=?" , newOwner .ID ); err != nil {
1407
+ if _ , err : = sess .Exec ("UPDATE `user` SET num_repos=num_repos+1 WHERE id=?" , newOwner .ID ); err != nil {
1374
1408
return fmt .Errorf ("increase new owner repository count: %v" , err )
1375
- } else if _ , err = sess .Exec ("UPDATE `user` SET num_repos=num_repos-1 WHERE id=?" , oldOwner .ID ); err != nil {
1409
+ } else if _ , err : = sess .Exec ("UPDATE `user` SET num_repos=num_repos-1 WHERE id=?" , oldOwner .ID ); err != nil {
1376
1410
return fmt .Errorf ("decrease old owner repository count: %v" , err )
1377
1411
}
1378
1412
@@ -1382,7 +1416,7 @@ func TransferOwnership(doer *User, newOwnerName string, repo *Repository) error
1382
1416
1383
1417
// Remove watch for organization.
1384
1418
if oldOwner .IsOrganization () {
1385
- if err = watchRepo (sess , oldOwner .ID , repo .ID , false ); err != nil {
1419
+ if err : = watchRepo (sess , oldOwner .ID , repo .ID , false ); err != nil {
1386
1420
return fmt .Errorf ("watchRepo [false]: %v" , err )
1387
1421
}
1388
1422
}
@@ -1394,16 +1428,18 @@ func TransferOwnership(doer *User, newOwnerName string, repo *Repository) error
1394
1428
return fmt .Errorf ("Failed to create dir %s: %v" , dir , err )
1395
1429
}
1396
1430
1397
- if err = os .Rename (RepoPath (oldOwner .Name , repo .Name ), RepoPath (newOwner .Name , repo .Name )); err != nil {
1431
+ if err : = os .Rename (RepoPath (oldOwner .Name , repo .Name ), RepoPath (newOwner .Name , repo .Name )); err != nil {
1398
1432
return fmt .Errorf ("rename repository directory: %v" , err )
1399
1433
}
1434
+ repoRenamed = true
1400
1435
1401
1436
// Rename remote wiki repository to new path and delete local copy.
1402
1437
wikiPath := WikiPath (oldOwner .Name , repo .Name )
1403
1438
if com .IsExist (wikiPath ) {
1404
- if err = os .Rename (wikiPath , WikiPath (newOwner .Name , repo .Name )); err != nil {
1439
+ if err : = os .Rename (wikiPath , WikiPath (newOwner .Name , repo .Name )); err != nil {
1405
1440
return fmt .Errorf ("rename repository wiki: %v" , err )
1406
1441
}
1442
+ wikiRenamed = true
1407
1443
}
1408
1444
1409
1445
// If there was previously a redirect at this location, remove it.
0 commit comments