2020-01-12 05:11:17 -07:00
|
|
|
// 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"
|
2020-06-06 18:45:12 -06:00
|
|
|
"code.gitea.io/gitea/modules/structs"
|
2021-09-14 23:42:09 -06:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2020-01-12 05:11:17 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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,
|
2020-06-06 18:45:12 -06:00
|
|
|
IsPrivate: oldRepo.IsPrivate || oldRepo.Owner.Visibility == structs.VisibleTypePrivate,
|
2020-01-12 05:11:17 -07:00
|
|
|
IsEmpty: oldRepo.IsEmpty,
|
|
|
|
IsFork: true,
|
|
|
|
ForkID: oldRepo.ID,
|
|
|
|
}
|
|
|
|
|
|
|
|
oldRepoPath := oldRepo.RepoPath()
|
|
|
|
|
2021-09-14 23:42:09 -06:00
|
|
|
needsRollback := false
|
|
|
|
rollbackFn := func() {
|
|
|
|
if !needsRollback {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
repoPath := models.RepoPath(owner.Name, repo.Name)
|
|
|
|
|
|
|
|
if exists, _ := util.IsExist(repoPath); !exists {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// As the transaction will be failed and hence database changes will be destroyed we only need
|
|
|
|
// to delete the related repository on the filesystem
|
|
|
|
if errDelete := util.RemoveAll(repoPath); errDelete != nil {
|
|
|
|
log.Error("Failed to remove fork repo")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
needsRollbackInPanic := true
|
|
|
|
defer func() {
|
|
|
|
panicErr := recover()
|
|
|
|
if panicErr == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if needsRollbackInPanic {
|
|
|
|
rollbackFn()
|
|
|
|
}
|
|
|
|
panic(panicErr)
|
|
|
|
}()
|
|
|
|
|
2020-01-12 05:11:17 -07:00
|
|
|
err = models.WithTx(func(ctx models.DBContext) error {
|
2020-09-24 22:09:23 -06:00
|
|
|
if err = models.CreateRepository(ctx, doer, owner, repo, false); err != nil {
|
2020-01-12 05:11:17 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = models.IncrementRepoForkNum(ctx, oldRepo.ID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-14 15:15:28 -06:00
|
|
|
// copy lfs files failure should not be ignored
|
2021-09-14 23:42:09 -06:00
|
|
|
if err = models.CopyLFS(ctx, repo, oldRepo); err != nil {
|
2021-04-14 15:15:28 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-14 23:42:09 -06:00
|
|
|
needsRollback = true
|
|
|
|
|
2020-01-12 05:11:17 -07:00
|
|
|
repoPath := models.RepoPath(owner.Name, repo.Name)
|
2021-10-14 10:07:53 -06:00
|
|
|
if stdout, err := git.NewCommand("clone", "--bare", oldRepoPath, repoPath).
|
2020-01-12 05:11:17 -07:00
|
|
|
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)
|
|
|
|
return fmt.Errorf("git clone: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-10-14 10:07:53 -06:00
|
|
|
if err := repo.CheckDaemonExportOKCtx(ctx); err != nil {
|
|
|
|
return fmt.Errorf("checkDaemonExportOK: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-01-12 05:11:17 -07:00
|
|
|
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)
|
|
|
|
return fmt.Errorf("git update-server-info: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-01-20 13:01:19 -07:00
|
|
|
if err = createDelegateHooks(repoPath); err != nil {
|
2020-01-12 05:11:17 -07:00
|
|
|
return fmt.Errorf("createDelegateHooks: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2021-09-14 23:42:09 -06:00
|
|
|
needsRollbackInPanic = false
|
2020-01-12 05:11:17 -07:00
|
|
|
if err != nil {
|
2021-09-14 23:42:09 -06:00
|
|
|
rollbackFn()
|
2020-01-12 05:11:17 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-04-14 15:15:28 -06:00
|
|
|
// even if below operations failed, it could be ignored. And they will be retried
|
2020-01-12 05:11:17 -07:00
|
|
|
ctx := models.DefaultDBContext()
|
2021-09-14 23:42:09 -06:00
|
|
|
if err := repo.UpdateSize(ctx); err != nil {
|
2020-01-12 05:11:17 -07:00
|
|
|
log.Error("Failed to update size for repository: %v", err)
|
|
|
|
}
|
2020-04-08 06:13:04 -06:00
|
|
|
if err := models.CopyLanguageStat(oldRepo, repo); err != nil {
|
|
|
|
log.Error("Copy language stat from oldRepo failed")
|
|
|
|
}
|
2020-09-24 22:09:23 -06:00
|
|
|
|
|
|
|
return repo, nil
|
2020-01-12 05:11:17 -07:00
|
|
|
}
|
2021-09-14 20:05:47 -06:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|