Decrement Fork Num when converting from Fork (#17035)

When converting repositories from forks to normal the root NumFork needs to be
decremented too.

Fix #17026

Signed-off-by: Andrew Thornton <art27@cantab.net>
This commit is contained in:
zeripath 2021-09-14 18:07:08 +01:00 committed by GitHub
parent 26ef180f46
commit 0a542dd59f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 4 deletions

View file

@ -1209,6 +1209,12 @@ func IncrementRepoForkNum(ctx DBContext, repoID int64) error {
return err return err
} }
// DecrementRepoForkNum decrement repository fork number
func DecrementRepoForkNum(ctx DBContext, repoID int64) error {
_, err := ctx.e.Exec("UPDATE `repository` SET num_forks=num_forks-1 WHERE id=?", repoID)
return err
}
// ChangeRepositoryName changes all corresponding setting from old repository name to new one. // ChangeRepositoryName changes all corresponding setting from old repository name to new one.
func ChangeRepositoryName(doer *User, repo *Repository, newRepoName string) (err error) { func ChangeRepositoryName(doer *User, repo *Repository, newRepoName string) (err error) {
oldRepoName := repo.Name oldRepoName := repo.Name

View file

@ -132,3 +132,34 @@ func ForkRepository(doer, owner *models.User, opts models.ForkRepoOptions) (_ *m
return repo, nil return repo, nil
} }
// ConvertForkToNormalRepository convert the provided repo from a forked repo to normal repo
func ConvertForkToNormalRepository(repo *models.Repository) error {
err := models.WithTx(func(ctx models.DBContext) error {
repo, err := models.GetRepositoryByIDCtx(ctx, repo.ID)
if err != nil {
return err
}
if !repo.IsFork {
return nil
}
if err := models.DecrementRepoForkNum(ctx, repo.ForkID); err != nil {
log.Error("Unable to decrement repo fork num for old root repo %d of repository %-v whilst converting from fork. Error: %v", repo.ForkID, repo, err)
return err
}
repo.IsFork = false
repo.ForkID = 0
if err := models.UpdateRepositoryCtx(ctx, repo, false); err != nil {
log.Error("Unable to update repository %-v whilst converting from fork. Error: %v", repo, err)
return err
}
return nil
})
return err
}

View file

@ -555,10 +555,8 @@ func SettingsPost(ctx *context.Context) {
return return
} }
repo.IsFork = false if err := repository.ConvertForkToNormalRepository(repo); err != nil {
repo.ForkID = 0 log.Error("Unable to convert repository %-v from fork. Error: %v", repo, err)
if err := models.UpdateRepository(repo, false); err != nil {
log.Error("Unable to update repository %-v whilst converting from fork", repo)
ctx.ServerError("Convert Fork", err) ctx.ServerError("Convert Fork", err)
return return
} }