Skip to content

Commit

Permalink
revert change to fork
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny committed Dec 12, 2024
1 parent 743cebe commit 050fad9
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 23 deletions.
29 changes: 20 additions & 9 deletions models/repo/fork.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions models/repo/fork_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
12 changes: 7 additions & 5 deletions routers/common/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 1 addition & 5 deletions routers/web/repo/fork.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions services/repository/fork.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 050fad9

Please sign in to comment.