2020-01-12 07:11:17 -05: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 19:45:12 -05:00
|
|
|
"code.gitea.io/gitea/modules/structs"
|
2021-09-14 11:16:40 -05:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2020-01-12 07:11:17 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// ForkRepository forks a repository
|
2021-08-28 03:37:14 -05:00
|
|
|
func ForkRepository(doer, owner *models.User, opts models.ForkRepoOptions) (_ *models.Repository, err error) {
|
|
|
|
forkedRepo, err := opts.BaseRepo.GetUserFork(owner.ID)
|
2020-01-12 07:11:17 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if forkedRepo != nil {
|
|
|
|
return nil, models.ErrForkAlreadyExist{
|
|
|
|
Uname: owner.Name,
|
2021-08-28 03:37:14 -05:00
|
|
|
RepoName: opts.BaseRepo.FullName(),
|
2020-01-12 07:11:17 -05:00
|
|
|
ForkName: forkedRepo.FullName(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
repo := &models.Repository{
|
|
|
|
OwnerID: owner.ID,
|
|
|
|
Owner: owner,
|
|
|
|
OwnerName: owner.Name,
|
2021-08-28 03:37:14 -05:00
|
|
|
Name: opts.Name,
|
|
|
|
LowerName: strings.ToLower(opts.Name),
|
|
|
|
Description: opts.Description,
|
|
|
|
DefaultBranch: opts.BaseRepo.DefaultBranch,
|
|
|
|
IsPrivate: opts.BaseRepo.IsPrivate || opts.BaseRepo.Owner.Visibility == structs.VisibleTypePrivate,
|
|
|
|
IsEmpty: opts.BaseRepo.IsEmpty,
|
2020-01-12 07:11:17 -05:00
|
|
|
IsFork: true,
|
2021-08-28 03:37:14 -05:00
|
|
|
ForkID: opts.BaseRepo.ID,
|
2020-01-12 07:11:17 -05:00
|
|
|
}
|
|
|
|
|
2021-08-28 03:37:14 -05:00
|
|
|
oldRepoPath := opts.BaseRepo.RepoPath()
|
2020-01-12 07:11:17 -05:00
|
|
|
|
2021-09-14 11:16:40 -05: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 07:11:17 -05:00
|
|
|
err = models.WithTx(func(ctx models.DBContext) error {
|
2020-09-24 23:09:23 -05:00
|
|
|
if err = models.CreateRepository(ctx, doer, owner, repo, false); err != nil {
|
2020-01-12 07:11:17 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-08-28 03:37:14 -05:00
|
|
|
if err = models.IncrementRepoForkNum(ctx, opts.BaseRepo.ID); err != nil {
|
2020-01-12 07:11:17 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-14 16:15:28 -05:00
|
|
|
// copy lfs files failure should not be ignored
|
2021-09-14 11:16:40 -05:00
|
|
|
if err = models.CopyLFS(ctx, repo, opts.BaseRepo); err != nil {
|
2021-04-14 16:15:28 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-14 11:16:40 -05:00
|
|
|
needsRollback = true
|
|
|
|
|
2020-01-12 07:11:17 -05:00
|
|
|
repoPath := models.RepoPath(owner.Name, repo.Name)
|
|
|
|
if stdout, err := git.NewCommand(
|
|
|
|
"clone", "--bare", oldRepoPath, repoPath).
|
2021-08-28 03:37:14 -05:00
|
|
|
SetDescription(fmt.Sprintf("ForkRepository(git clone): %s to %s", opts.BaseRepo.FullName(), repo.FullName())).
|
2020-01-12 07:11:17 -05:00
|
|
|
RunInDirTimeout(10*time.Minute, ""); err != nil {
|
2021-08-28 03:37:14 -05:00
|
|
|
log.Error("Fork Repository (git clone) Failed for %v (from %v):\nStdout: %s\nError: %v", repo, opts.BaseRepo, stdout, err)
|
2020-01-12 07:11:17 -05:00
|
|
|
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)
|
|
|
|
return fmt.Errorf("git update-server-info: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-01-20 15:01:19 -05:00
|
|
|
if err = createDelegateHooks(repoPath); err != nil {
|
2020-01-12 07:11:17 -05:00
|
|
|
return fmt.Errorf("createDelegateHooks: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2021-09-14 11:16:40 -05:00
|
|
|
needsRollbackInPanic = false
|
2020-01-12 07:11:17 -05:00
|
|
|
if err != nil {
|
2021-09-14 11:16:40 -05:00
|
|
|
rollbackFn()
|
2020-01-12 07:11:17 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-04-14 16:15:28 -05:00
|
|
|
// even if below operations failed, it could be ignored. And they will be retried
|
2020-01-12 07:11:17 -05:00
|
|
|
ctx := models.DefaultDBContext()
|
2021-09-14 11:16:40 -05:00
|
|
|
if err := repo.UpdateSize(ctx); err != nil {
|
2020-01-12 07:11:17 -05:00
|
|
|
log.Error("Failed to update size for repository: %v", err)
|
|
|
|
}
|
2021-08-28 03:37:14 -05:00
|
|
|
if err := models.CopyLanguageStat(opts.BaseRepo, repo); err != nil {
|
2020-04-08 07:13:04 -05:00
|
|
|
log.Error("Copy language stat from oldRepo failed")
|
|
|
|
}
|
2020-09-24 23:09:23 -05:00
|
|
|
|
|
|
|
return repo, nil
|
2020-01-12 07:11:17 -05:00
|
|
|
}
|