mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-08 19:01:26 +01:00
7a7f56044a
* Don't automatically delete repository files if they are present Prior to this PR Gitea would delete any repository files if they are present during creation or migration. This can in certain circumstances lead to data-loss and is slightly unpleasant. This PR provides a mechanism for Gitea to adopt repositories on creation and otherwise requires an explicit flag for deletion. PushCreate is slightly different - the create will cause adoption if that is allowed otherwise it will delete the data if that is allowed. Signed-off-by: Andrew Thornton <art27@cantab.net> * Update swagger Signed-off-by: Andrew Thornton <art27@cantab.net> * Fix tests and migrate overwrite Signed-off-by: Andrew Thornton <art27@cantab.net> * as per @lunny Only offer to adopt or overwrite if the user can do that. Allow the site administrator to adopt or overwrite in all circumstances Signed-off-by: Andrew Thornton <art27@cantab.net> * Use setting.Repository.DefaultBranch for the default branch Signed-off-by: Andrew Thornton <art27@cantab.net> * Always set setting.Repository.DefaultBranch Signed-off-by: Andrew Thornton <art27@cantab.net> * update swagger Signed-off-by: Andrew Thornton <art27@cantab.net> * update templates Signed-off-by: Andrew Thornton <art27@cantab.net> * ensure repo closed Signed-off-by: Andrew Thornton <art27@cantab.net> * Rewrite of adoption as per @6543 and @lunny Signed-off-by: Andrew Thornton <art27@cantab.net> * Apply suggestions from code review * update swagger Signed-off-by: Andrew Thornton <art27@cantab.net> * missing not Signed-off-by: Andrew Thornton <art27@cantab.net> * add modals and flash reporting Signed-off-by: Andrew Thornton <art27@cantab.net> * Make the unadopted page searchable Signed-off-by: Andrew Thornton <art27@cantab.net> * Add API Signed-off-by: Andrew Thornton <art27@cantab.net> * Fix swagger Signed-off-by: Andrew Thornton <art27@cantab.net> * fix swagger Signed-off-by: Andrew Thornton <art27@cantab.net> * Handle empty and non-master branched repositories Signed-off-by: Andrew Thornton <art27@cantab.net> * placate lint Signed-off-by: Andrew Thornton <art27@cantab.net> * remove commented out code Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: techknowlogick <techknowlogick@gitea.io>
110 lines
3.2 KiB
Go
110 lines
3.2 KiB
Go
// 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 (
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"code.gitea.io/gitea/models"
|
|
"code.gitea.io/gitea/modules/git"
|
|
"code.gitea.io/gitea/modules/log"
|
|
"code.gitea.io/gitea/modules/structs"
|
|
)
|
|
|
|
// ForkRepository forks a repository
|
|
func ForkRepository(doer, owner *models.User, oldRepo *models.Repository, name, desc string) (_ *models.Repository, err error) {
|
|
forkedRepo, err := oldRepo.GetUserFork(owner.ID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if forkedRepo != nil {
|
|
return nil, models.ErrForkAlreadyExist{
|
|
Uname: owner.Name,
|
|
RepoName: oldRepo.FullName(),
|
|
ForkName: forkedRepo.FullName(),
|
|
}
|
|
}
|
|
|
|
repo := &models.Repository{
|
|
OwnerID: owner.ID,
|
|
Owner: owner,
|
|
OwnerName: owner.Name,
|
|
Name: name,
|
|
LowerName: strings.ToLower(name),
|
|
Description: desc,
|
|
DefaultBranch: oldRepo.DefaultBranch,
|
|
IsPrivate: oldRepo.IsPrivate || oldRepo.Owner.Visibility == structs.VisibleTypePrivate,
|
|
IsEmpty: oldRepo.IsEmpty,
|
|
IsFork: true,
|
|
ForkID: oldRepo.ID,
|
|
}
|
|
|
|
oldRepoPath := oldRepo.RepoPath()
|
|
|
|
err = models.WithTx(func(ctx models.DBContext) error {
|
|
if err = models.CreateRepository(ctx, doer, owner, repo, false); err != nil {
|
|
return err
|
|
}
|
|
|
|
rollbackRemoveFn := func() {
|
|
if repo.ID == 0 {
|
|
return
|
|
}
|
|
if errDelete := models.DeleteRepository(doer, owner.ID, repo.ID); errDelete != nil {
|
|
log.Error("Rollback deleteRepository: %v", errDelete)
|
|
}
|
|
}
|
|
|
|
if err = models.IncrementRepoForkNum(ctx, oldRepo.ID); err != nil {
|
|
rollbackRemoveFn()
|
|
return err
|
|
}
|
|
|
|
repoPath := models.RepoPath(owner.Name, repo.Name)
|
|
if stdout, err := git.NewCommand(
|
|
"clone", "--bare", oldRepoPath, repoPath).
|
|
SetDescription(fmt.Sprintf("ForkRepository(git clone): %s to %s", oldRepo.FullName(), repo.FullName())).
|
|
RunInDirTimeout(10*time.Minute, ""); err != nil {
|
|
log.Error("Fork Repository (git clone) Failed for %v (from %v):\nStdout: %s\nError: %v", repo, oldRepo, stdout, err)
|
|
rollbackRemoveFn()
|
|
return fmt.Errorf("git clone: %v", err)
|
|
}
|
|
|
|
if stdout, err := git.NewCommand("update-server-info").
|
|
SetDescription(fmt.Sprintf("ForkRepository(git update-server-info): %s", repo.FullName())).
|
|
RunInDir(repoPath); err != nil {
|
|
log.Error("Fork Repository (git update-server-info) failed for %v:\nStdout: %s\nError: %v", repo, stdout, err)
|
|
rollbackRemoveFn()
|
|
return fmt.Errorf("git update-server-info: %v", err)
|
|
}
|
|
|
|
if err = createDelegateHooks(repoPath); err != nil {
|
|
rollbackRemoveFn()
|
|
return fmt.Errorf("createDelegateHooks: %v", err)
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
ctx := models.DefaultDBContext()
|
|
if err = repo.UpdateSize(ctx); err != nil {
|
|
log.Error("Failed to update size for repository: %v", err)
|
|
}
|
|
if err := models.CopyLanguageStat(oldRepo, repo); err != nil {
|
|
log.Error("Copy language stat from oldRepo failed")
|
|
}
|
|
|
|
if err := models.CopyLFS(ctx, repo, oldRepo); err != nil {
|
|
if errDelete := models.DeleteRepository(doer, owner.ID, repo.ID); errDelete != nil {
|
|
log.Error("Rollback deleteRepository: %v", errDelete)
|
|
}
|
|
return nil, err
|
|
}
|
|
return repo, nil
|
|
}
|