You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
176 lines
4.9 KiB
176 lines
4.9 KiB
5 years ago
|
// 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 (
|
||
3 years ago
|
"context"
|
||
5 years ago
|
"fmt"
|
||
|
"strings"
|
||
|
"time"
|
||
|
|
||
|
"code.gitea.io/gitea/models"
|
||
3 years ago
|
"code.gitea.io/gitea/models/db"
|
||
5 years ago
|
"code.gitea.io/gitea/modules/git"
|
||
|
"code.gitea.io/gitea/modules/log"
|
||
3 years ago
|
"code.gitea.io/gitea/modules/notification"
|
||
|
repo_module "code.gitea.io/gitea/modules/repository"
|
||
4 years ago
|
"code.gitea.io/gitea/modules/structs"
|
||
3 years ago
|
"code.gitea.io/gitea/modules/util"
|
||
5 years ago
|
)
|
||
|
|
||
|
// ForkRepository forks a repository
|
||
3 years ago
|
func ForkRepository(doer, owner *models.User, opts models.ForkRepoOptions) (_ *models.Repository, err error) {
|
||
|
forkedRepo, err := opts.BaseRepo.GetUserFork(owner.ID)
|
||
5 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
if forkedRepo != nil {
|
||
|
return nil, models.ErrForkAlreadyExist{
|
||
|
Uname: owner.Name,
|
||
3 years ago
|
RepoName: opts.BaseRepo.FullName(),
|
||
5 years ago
|
ForkName: forkedRepo.FullName(),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
repo := &models.Repository{
|
||
|
OwnerID: owner.ID,
|
||
|
Owner: owner,
|
||
|
OwnerName: owner.Name,
|
||
3 years ago
|
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,
|
||
5 years ago
|
IsFork: true,
|
||
3 years ago
|
ForkID: opts.BaseRepo.ID,
|
||
5 years ago
|
}
|
||
|
|
||
3 years ago
|
oldRepoPath := opts.BaseRepo.RepoPath()
|
||
5 years ago
|
|
||
3 years ago
|
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)
|
||
|
}()
|
||
|
|
||
3 years ago
|
err = db.WithTx(func(ctx context.Context) error {
|
||
4 years ago
|
if err = models.CreateRepository(ctx, doer, owner, repo, false); err != nil {
|
||
5 years ago
|
return err
|
||
|
}
|
||
|
|
||
3 years ago
|
if err = models.IncrementRepoForkNum(ctx, opts.BaseRepo.ID); err != nil {
|
||
5 years ago
|
return err
|
||
|
}
|
||
|
|
||
4 years ago
|
// copy lfs files failure should not be ignored
|
||
3 years ago
|
if err = models.CopyLFS(ctx, repo, opts.BaseRepo); err != nil {
|
||
4 years ago
|
return err
|
||
|
}
|
||
|
|
||
3 years ago
|
needsRollback = true
|
||
|
|
||
5 years ago
|
repoPath := models.RepoPath(owner.Name, repo.Name)
|
||
3 years ago
|
if stdout, err := git.NewCommandContext(ctx,
|
||
5 years ago
|
"clone", "--bare", oldRepoPath, repoPath).
|
||
3 years ago
|
SetDescription(fmt.Sprintf("ForkRepository(git clone): %s to %s", opts.BaseRepo.FullName(), repo.FullName())).
|
||
5 years ago
|
RunInDirTimeout(10*time.Minute, ""); err != nil {
|
||
3 years ago
|
log.Error("Fork Repository (git clone) Failed for %v (from %v):\nStdout: %s\nError: %v", repo, opts.BaseRepo, stdout, err)
|
||
5 years ago
|
return fmt.Errorf("git clone: %v", err)
|
||
|
}
|
||
|
|
||
3 years ago
|
if err := repo.CheckDaemonExportOK(ctx); err != nil {
|
||
|
return fmt.Errorf("checkDaemonExportOK: %v", err)
|
||
|
}
|
||
|
|
||
|
if stdout, err := git.NewCommandContext(ctx, "update-server-info").
|
||
5 years ago
|
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)
|
||
|
}
|
||
|
|
||
3 years ago
|
if err = repo_module.CreateDelegateHooks(repoPath); err != nil {
|
||
5 years ago
|
return fmt.Errorf("createDelegateHooks: %v", err)
|
||
|
}
|
||
|
return nil
|
||
|
})
|
||
3 years ago
|
needsRollbackInPanic = false
|
||
5 years ago
|
if err != nil {
|
||
3 years ago
|
rollbackFn()
|
||
5 years ago
|
return nil, err
|
||
|
}
|
||
|
|
||
4 years ago
|
// even if below operations failed, it could be ignored. And they will be retried
|
||
3 years ago
|
ctx := db.DefaultContext
|
||
3 years ago
|
if err := repo.UpdateSize(ctx); err != nil {
|
||
5 years ago
|
log.Error("Failed to update size for repository: %v", err)
|
||
|
}
|
||
3 years ago
|
if err := models.CopyLanguageStat(opts.BaseRepo, repo); err != nil {
|
||
5 years ago
|
log.Error("Copy language stat from oldRepo failed")
|
||
|
}
|
||
4 years ago
|
|
||
3 years ago
|
notification.NotifyForkRepository(doer, opts.BaseRepo, repo)
|
||
|
|
||
4 years ago
|
return repo, nil
|
||
5 years ago
|
}
|
||
3 years ago
|
|
||
|
// ConvertForkToNormalRepository convert the provided repo from a forked repo to normal repo
|
||
|
func ConvertForkToNormalRepository(repo *models.Repository) error {
|
||
3 years ago
|
err := db.WithTx(func(ctx context.Context) error {
|
||
3 years ago
|
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
|
||
|
}
|