|
| 1 | +// Copyright 2024 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package repo |
| 5 | + |
| 6 | +import ( |
| 7 | + "errors" |
| 8 | + "net/http" |
| 9 | + "net/url" |
| 10 | + |
| 11 | + "code.gitea.io/gitea/models/db" |
| 12 | + git_model "code.gitea.io/gitea/models/git" |
| 13 | + "code.gitea.io/gitea/models/organization" |
| 14 | + repo_model "code.gitea.io/gitea/models/repo" |
| 15 | + user_model "code.gitea.io/gitea/models/user" |
| 16 | + "code.gitea.io/gitea/modules/base" |
| 17 | + "code.gitea.io/gitea/modules/log" |
| 18 | + "code.gitea.io/gitea/modules/optional" |
| 19 | + "code.gitea.io/gitea/modules/setting" |
| 20 | + "code.gitea.io/gitea/modules/structs" |
| 21 | + "code.gitea.io/gitea/modules/web" |
| 22 | + "code.gitea.io/gitea/services/context" |
| 23 | + "code.gitea.io/gitea/services/forms" |
| 24 | + repo_service "code.gitea.io/gitea/services/repository" |
| 25 | +) |
| 26 | + |
| 27 | +const ( |
| 28 | + tplFork base.TplName = "repo/pulls/fork" |
| 29 | +) |
| 30 | + |
| 31 | +func getForkRepository(ctx *context.Context) *repo_model.Repository { |
| 32 | + forkRepo := ctx.Repo.Repository |
| 33 | + if ctx.Written() { |
| 34 | + return nil |
| 35 | + } |
| 36 | + |
| 37 | + if forkRepo.IsEmpty { |
| 38 | + log.Trace("Empty repository %-v", forkRepo) |
| 39 | + ctx.NotFound("getForkRepository", nil) |
| 40 | + return nil |
| 41 | + } |
| 42 | + |
| 43 | + if err := forkRepo.LoadOwner(ctx); err != nil { |
| 44 | + ctx.ServerError("LoadOwner", err) |
| 45 | + return nil |
| 46 | + } |
| 47 | + |
| 48 | + ctx.Data["repo_name"] = forkRepo.Name |
| 49 | + ctx.Data["description"] = forkRepo.Description |
| 50 | + ctx.Data["IsPrivate"] = forkRepo.IsPrivate || forkRepo.Owner.Visibility == structs.VisibleTypePrivate |
| 51 | + canForkToUser := forkRepo.OwnerID != ctx.Doer.ID && !repo_model.HasForkedRepo(ctx, ctx.Doer.ID, forkRepo.ID) |
| 52 | + |
| 53 | + ctx.Data["ForkRepo"] = forkRepo |
| 54 | + |
| 55 | + ownedOrgs, err := organization.GetOrgsCanCreateRepoByUserID(ctx, ctx.Doer.ID) |
| 56 | + if err != nil { |
| 57 | + ctx.ServerError("GetOrgsCanCreateRepoByUserID", err) |
| 58 | + return nil |
| 59 | + } |
| 60 | + var orgs []*organization.Organization |
| 61 | + for _, org := range ownedOrgs { |
| 62 | + if forkRepo.OwnerID != org.ID && !repo_model.HasForkedRepo(ctx, org.ID, forkRepo.ID) { |
| 63 | + orgs = append(orgs, org) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + traverseParentRepo := forkRepo |
| 68 | + for { |
| 69 | + if ctx.Doer.ID == traverseParentRepo.OwnerID { |
| 70 | + canForkToUser = false |
| 71 | + } else { |
| 72 | + for i, org := range orgs { |
| 73 | + if org.ID == traverseParentRepo.OwnerID { |
| 74 | + orgs = append(orgs[:i], orgs[i+1:]...) |
| 75 | + break |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + if !traverseParentRepo.IsFork { |
| 81 | + break |
| 82 | + } |
| 83 | + traverseParentRepo, err = repo_model.GetRepositoryByID(ctx, traverseParentRepo.ForkID) |
| 84 | + if err != nil { |
| 85 | + ctx.ServerError("GetRepositoryByID", err) |
| 86 | + return nil |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + ctx.Data["CanForkToUser"] = canForkToUser |
| 91 | + ctx.Data["Orgs"] = orgs |
| 92 | + |
| 93 | + if canForkToUser { |
| 94 | + ctx.Data["ContextUser"] = ctx.Doer |
| 95 | + } else if len(orgs) > 0 { |
| 96 | + ctx.Data["ContextUser"] = orgs[0] |
| 97 | + } else { |
| 98 | + ctx.Data["CanForkRepo"] = false |
| 99 | + ctx.Flash.Error(ctx.Tr("repo.fork_no_valid_owners"), true) |
| 100 | + return nil |
| 101 | + } |
| 102 | + |
| 103 | + branches, err := git_model.FindBranchNames(ctx, git_model.FindBranchOptions{ |
| 104 | + RepoID: ctx.Repo.Repository.ID, |
| 105 | + ListOptions: db.ListOptions{ |
| 106 | + ListAll: true, |
| 107 | + }, |
| 108 | + IsDeletedBranch: optional.Some(false), |
| 109 | + // Add it as the first option |
| 110 | + ExcludeBranchNames: []string{ctx.Repo.Repository.DefaultBranch}, |
| 111 | + }) |
| 112 | + if err != nil { |
| 113 | + ctx.ServerError("FindBranchNames", err) |
| 114 | + return nil |
| 115 | + } |
| 116 | + ctx.Data["Branches"] = append([]string{ctx.Repo.Repository.DefaultBranch}, branches...) |
| 117 | + |
| 118 | + return forkRepo |
| 119 | +} |
| 120 | + |
| 121 | +// Fork render repository fork page |
| 122 | +func Fork(ctx *context.Context) { |
| 123 | + ctx.Data["Title"] = ctx.Tr("new_fork") |
| 124 | + |
| 125 | + if ctx.Doer.CanForkRepo() { |
| 126 | + ctx.Data["CanForkRepo"] = true |
| 127 | + } else { |
| 128 | + maxCreationLimit := ctx.Doer.MaxCreationLimit() |
| 129 | + msg := ctx.TrN(maxCreationLimit, "repo.form.reach_limit_of_creation_1", "repo.form.reach_limit_of_creation_n", maxCreationLimit) |
| 130 | + ctx.Flash.Error(msg, true) |
| 131 | + } |
| 132 | + |
| 133 | + getForkRepository(ctx) |
| 134 | + if ctx.Written() { |
| 135 | + return |
| 136 | + } |
| 137 | + |
| 138 | + ctx.HTML(http.StatusOK, tplFork) |
| 139 | +} |
| 140 | + |
| 141 | +// ForkPost response for forking a repository |
| 142 | +func ForkPost(ctx *context.Context) { |
| 143 | + form := web.GetForm(ctx).(*forms.CreateRepoForm) |
| 144 | + ctx.Data["Title"] = ctx.Tr("new_fork") |
| 145 | + ctx.Data["CanForkRepo"] = true |
| 146 | + |
| 147 | + ctxUser := checkContextUser(ctx, form.UID) |
| 148 | + if ctx.Written() { |
| 149 | + return |
| 150 | + } |
| 151 | + |
| 152 | + forkRepo := getForkRepository(ctx) |
| 153 | + if ctx.Written() { |
| 154 | + return |
| 155 | + } |
| 156 | + |
| 157 | + ctx.Data["ContextUser"] = ctxUser |
| 158 | + |
| 159 | + if ctx.HasError() { |
| 160 | + ctx.HTML(http.StatusOK, tplFork) |
| 161 | + return |
| 162 | + } |
| 163 | + |
| 164 | + var err error |
| 165 | + traverseParentRepo := forkRepo |
| 166 | + for { |
| 167 | + if ctxUser.ID == traverseParentRepo.OwnerID { |
| 168 | + ctx.RenderWithErr(ctx.Tr("repo.settings.new_owner_has_same_repo"), tplFork, &form) |
| 169 | + return |
| 170 | + } |
| 171 | + repo := repo_model.GetForkedRepo(ctx, ctxUser.ID, traverseParentRepo.ID) |
| 172 | + if repo != nil { |
| 173 | + ctx.Redirect(ctxUser.HomeLink() + "/" + url.PathEscape(repo.Name)) |
| 174 | + return |
| 175 | + } |
| 176 | + if !traverseParentRepo.IsFork { |
| 177 | + break |
| 178 | + } |
| 179 | + traverseParentRepo, err = repo_model.GetRepositoryByID(ctx, traverseParentRepo.ForkID) |
| 180 | + if err != nil { |
| 181 | + ctx.ServerError("GetRepositoryByID", err) |
| 182 | + return |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + // Check if user is allowed to create repo's on the organization. |
| 187 | + if ctxUser.IsOrganization() { |
| 188 | + isAllowedToFork, err := organization.OrgFromUser(ctxUser).CanCreateOrgRepo(ctx, ctx.Doer.ID) |
| 189 | + if err != nil { |
| 190 | + ctx.ServerError("CanCreateOrgRepo", err) |
| 191 | + return |
| 192 | + } else if !isAllowedToFork { |
| 193 | + ctx.Error(http.StatusForbidden) |
| 194 | + return |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + repo, err := repo_service.ForkRepository(ctx, ctx.Doer, ctxUser, repo_service.ForkRepoOptions{ |
| 199 | + BaseRepo: forkRepo, |
| 200 | + Name: form.RepoName, |
| 201 | + Description: form.Description, |
| 202 | + SingleBranch: form.ForkSingleBranch, |
| 203 | + }) |
| 204 | + if err != nil { |
| 205 | + ctx.Data["Err_RepoName"] = true |
| 206 | + switch { |
| 207 | + case repo_model.IsErrReachLimitOfRepo(err): |
| 208 | + maxCreationLimit := ctxUser.MaxCreationLimit() |
| 209 | + msg := ctx.TrN(maxCreationLimit, "repo.form.reach_limit_of_creation_1", "repo.form.reach_limit_of_creation_n", maxCreationLimit) |
| 210 | + ctx.RenderWithErr(msg, tplFork, &form) |
| 211 | + case repo_model.IsErrRepoAlreadyExist(err): |
| 212 | + ctx.RenderWithErr(ctx.Tr("repo.settings.new_owner_has_same_repo"), tplFork, &form) |
| 213 | + case repo_model.IsErrRepoFilesAlreadyExist(err): |
| 214 | + switch { |
| 215 | + case ctx.IsUserSiteAdmin() || (setting.Repository.AllowAdoptionOfUnadoptedRepositories && setting.Repository.AllowDeleteOfUnadoptedRepositories): |
| 216 | + ctx.RenderWithErr(ctx.Tr("form.repository_files_already_exist.adopt_or_delete"), tplFork, form) |
| 217 | + case setting.Repository.AllowAdoptionOfUnadoptedRepositories: |
| 218 | + ctx.RenderWithErr(ctx.Tr("form.repository_files_already_exist.adopt"), tplFork, form) |
| 219 | + case setting.Repository.AllowDeleteOfUnadoptedRepositories: |
| 220 | + ctx.RenderWithErr(ctx.Tr("form.repository_files_already_exist.delete"), tplFork, form) |
| 221 | + default: |
| 222 | + ctx.RenderWithErr(ctx.Tr("form.repository_files_already_exist"), tplFork, form) |
| 223 | + } |
| 224 | + case db.IsErrNameReserved(err): |
| 225 | + ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(db.ErrNameReserved).Name), tplFork, &form) |
| 226 | + case db.IsErrNamePatternNotAllowed(err): |
| 227 | + ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(db.ErrNamePatternNotAllowed).Pattern), tplFork, &form) |
| 228 | + case errors.Is(err, user_model.ErrBlockedUser): |
| 229 | + ctx.RenderWithErr(ctx.Tr("repo.fork.blocked_user"), tplFork, form) |
| 230 | + default: |
| 231 | + ctx.ServerError("ForkPost", err) |
| 232 | + } |
| 233 | + return |
| 234 | + } |
| 235 | + |
| 236 | + log.Trace("Repository forked[%d]: %s/%s", forkRepo.ID, ctxUser.Name, repo.Name) |
| 237 | + ctx.Redirect(ctxUser.HomeLink() + "/" + url.PathEscape(repo.Name)) |
| 238 | +} |
0 commit comments