2019-11-15 13:36:11 +05:30
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
"code.gitea.io/gitea/modules/notification"
|
2020-01-15 08:44:50 +05:30
|
|
|
"code.gitea.io/gitea/modules/sync"
|
2019-12-06 09:30:50 +05:30
|
|
|
|
|
|
|
"github.com/unknwon/com"
|
2019-11-15 13:36:11 +05:30
|
|
|
)
|
|
|
|
|
2020-01-15 08:44:50 +05:30
|
|
|
// repoWorkingPool represents a working pool to order the parallel changes to the same repository
|
|
|
|
var repoWorkingPool = sync.NewExclusivePool()
|
|
|
|
|
2019-11-15 13:36:11 +05:30
|
|
|
// TransferOwnership transfers all corresponding setting from old user to new one.
|
|
|
|
func TransferOwnership(doer *models.User, newOwnerName string, repo *models.Repository) error {
|
|
|
|
if err := repo.GetOwner(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
oldOwner := repo.Owner
|
|
|
|
|
2020-01-15 08:44:50 +05:30
|
|
|
repoWorkingPool.CheckIn(com.ToStr(repo.ID))
|
2019-11-15 13:36:11 +05:30
|
|
|
if err := models.TransferOwnership(doer, newOwnerName, repo); err != nil {
|
2020-01-15 08:44:50 +05:30
|
|
|
repoWorkingPool.CheckOut(com.ToStr(repo.ID))
|
2019-11-15 13:36:11 +05:30
|
|
|
return err
|
|
|
|
}
|
2020-01-15 08:44:50 +05:30
|
|
|
repoWorkingPool.CheckOut(com.ToStr(repo.ID))
|
2019-11-15 13:36:11 +05:30
|
|
|
|
|
|
|
notification.NotifyTransferRepository(doer, repo, oldOwner.Name)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChangeRepositoryName changes all corresponding setting from old repository name to new one.
|
|
|
|
func ChangeRepositoryName(doer *models.User, repo *models.Repository, newRepoName string) error {
|
|
|
|
oldRepoName := repo.Name
|
|
|
|
|
2019-12-06 09:30:50 +05:30
|
|
|
// Change repository directory name. We must lock the local copy of the
|
|
|
|
// repo so that we can atomically rename the repo path and updates the
|
|
|
|
// local copy's origin accordingly.
|
2019-11-15 13:36:11 +05:30
|
|
|
|
2020-01-15 08:44:50 +05:30
|
|
|
repoWorkingPool.CheckIn(com.ToStr(repo.ID))
|
2019-12-06 09:30:50 +05:30
|
|
|
if err := models.ChangeRepositoryName(doer, repo, newRepoName); err != nil {
|
2020-01-15 08:44:50 +05:30
|
|
|
repoWorkingPool.CheckOut(com.ToStr(repo.ID))
|
2019-11-15 13:36:11 +05:30
|
|
|
return err
|
|
|
|
}
|
2020-01-15 08:44:50 +05:30
|
|
|
repoWorkingPool.CheckOut(com.ToStr(repo.ID))
|
2019-11-15 13:36:11 +05:30
|
|
|
|
|
|
|
notification.NotifyRenameRepository(doer, repo, oldRepoName)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|