From 050fad97d6c5ddcfda478a732895175459fda5b3 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 12 Dec 2024 12:21:40 -0800 Subject: [PATCH] revert change to fork --- models/repo/fork.go | 29 ++++++++++++++++++++--------- models/repo/fork_test.go | 4 ++-- routers/common/compare.go | 12 +++++++----- routers/web/repo/fork.go | 6 +----- services/repository/fork.go | 4 ++-- 5 files changed, 32 insertions(+), 23 deletions(-) diff --git a/models/repo/fork.go b/models/repo/fork.go index b69c539ccc..1c75e86458 100644 --- a/models/repo/fork.go +++ b/models/repo/fork.go @@ -21,17 +21,15 @@ func GetRepositoriesByForkID(ctx context.Context, forkID int64) ([]*Repository, } // GetForkedRepo checks if given user has already forked a repository with given ID. -func GetForkedRepo(ctx context.Context, ownerID, repoID int64) (*Repository, error) { +func GetForkedRepo(ctx context.Context, ownerID, repoID int64) *Repository { repo := new(Repository) - has, err := db.GetEngine(ctx). + has, _ := db.GetEngine(ctx). Where("owner_id=? AND fork_id=?", ownerID, repoID). Get(repo) - if err != nil { - return nil, err - } else if !has { - return nil, ErrRepoNotExist{ID: repoID} + if has { + return repo } - return repo, nil + return nil } // HasForkedRepo checks if given user has already forked a repository with given ID. @@ -43,6 +41,19 @@ func HasForkedRepo(ctx context.Context, ownerID, repoID int64) bool { return has } +// GetUserFork return user forked repository from this repository, if not forked return nil +func GetUserFork(ctx context.Context, repoID, userID int64) (*Repository, error) { + var forkedRepo Repository + has, err := db.GetEngine(ctx).Where("fork_id = ?", repoID).And("owner_id = ?", userID).Get(&forkedRepo) + if err != nil { + return nil, err + } + if !has { + return nil, nil + } + return &forkedRepo, nil +} + // IncrementRepoForkNum increment repository fork number func IncrementRepoForkNum(ctx context.Context, repoID int64) error { _, err := db.GetEngine(ctx).Exec("UPDATE `repository` SET num_forks=num_forks+1 WHERE id=?", repoID) @@ -76,8 +87,8 @@ func GetForksByUserAndOrgs(ctx context.Context, user *user_model.User, repo *Rep if user == nil { return repoList, nil } - forkedRepo, err := GetForkedRepo(ctx, repo.ID, user.ID) - if err != nil && !IsErrRepoNotExist(err) { + forkedRepo, err := GetUserFork(ctx, repo.ID, user.ID) + if err != nil { return repoList, err } if forkedRepo != nil { diff --git a/models/repo/fork_test.go b/models/repo/fork_test.go index 7a15874329..e8dca204cc 100644 --- a/models/repo/fork_test.go +++ b/models/repo/fork_test.go @@ -20,14 +20,14 @@ func TestGetUserFork(t *testing.T) { repo, err := repo_model.GetRepositoryByID(db.DefaultContext, 10) assert.NoError(t, err) assert.NotNil(t, repo) - repo, err = repo_model.GetForkedRepo(db.DefaultContext, repo.ID, 13) + repo, err = repo_model.GetUserFork(db.DefaultContext, repo.ID, 13) assert.NoError(t, err) assert.NotNil(t, repo) repo, err = repo_model.GetRepositoryByID(db.DefaultContext, 9) assert.NoError(t, err) assert.NotNil(t, repo) - repo, err = repo_model.GetForkedRepo(db.DefaultContext, repo.ID, 13) + repo, err = repo_model.GetUserFork(db.DefaultContext, repo.ID, 13) assert.NoError(t, err) assert.Nil(t, repo) } diff --git a/routers/common/compare.go b/routers/common/compare.go index a3cd0887e1..2f5edf635d 100644 --- a/routers/common/compare.go +++ b/routers/common/compare.go @@ -119,6 +119,8 @@ func (ci *CompareInfo) Close() { } } +// detectFullRef detects a short name as a branch, tag or commit's full ref name and type. +// It's the same job as git.UnstableGuessRefByShortName but with a database read instead of git read. func detectFullRef(ctx context.Context, repoID int64, gitRepo *git.Repository, oriRef string) (git.RefName, bool, error) { b, err := git_model.GetBranch(ctx, repoID, oriRef) if err != nil && !git_model.IsErrBranchNotExist(err) { @@ -166,13 +168,13 @@ func findHeadRepoFromRootBase(ctx context.Context, baseRepo *repo_model.Reposito return nil, nil } // test if we are lucky - repo, err := repo_model.GetForkedRepo(ctx, headUserID, baseRepo.ID) - if err == nil { - return repo, nil - } - if !repo_model.IsErrRepoNotExist(err) { + repo, err := repo_model.GetUserFork(ctx, headUserID, baseRepo.ID) + if err != nil { return nil, err } + if repo != nil { + return repo, nil + } firstLevelForkedRepo, err := repo_model.GetRepositoriesByForkID(ctx, baseRepo.ID) if err != nil { diff --git a/routers/web/repo/fork.go b/routers/web/repo/fork.go index c0d823c079..27e42a8f98 100644 --- a/routers/web/repo/fork.go +++ b/routers/web/repo/fork.go @@ -166,11 +166,7 @@ func ForkPost(ctx *context.Context) { ctx.RenderWithErr(ctx.Tr("repo.settings.new_owner_has_same_repo"), tplFork, &form) return } - repo, err := repo_model.GetForkedRepo(ctx, ctxUser.ID, traverseParentRepo.ID) - if err != nil && !repo_model.IsErrRepoNotExist(err) { - ctx.ServerError("GetForkedRepo", err) - return - } + repo := repo_model.GetForkedRepo(ctx, ctxUser.ID, traverseParentRepo.ID) if repo != nil { ctx.Redirect(ctxUser.HomeLink() + "/" + url.PathEscape(repo.Name)) return diff --git a/services/repository/fork.go b/services/repository/fork.go index 26057266c5..bc4fdf8562 100644 --- a/services/repository/fork.go +++ b/services/repository/fork.go @@ -71,8 +71,8 @@ func ForkRepository(ctx context.Context, doer, owner *user_model.User, opts Fork } } - forkedRepo, err := repo_model.GetForkedRepo(ctx, opts.BaseRepo.ID, owner.ID) - if err != nil && !repo_model.IsErrRepoNotExist(err) { + forkedRepo, err := repo_model.GetUserFork(ctx, opts.BaseRepo.ID, owner.ID) + if err != nil { return nil, err } if forkedRepo != nil {