code.gitea.io/gitea@v1.21.7/routers/web/repo/branch.go (about) 1 // Copyright 2014 The Gogs Authors. All rights reserved. 2 // Copyright 2018 The Gitea Authors. All rights reserved. 3 // SPDX-License-Identifier: MIT 4 5 package repo 6 7 import ( 8 "errors" 9 "fmt" 10 "net/http" 11 "net/url" 12 "strings" 13 14 "code.gitea.io/gitea/models" 15 git_model "code.gitea.io/gitea/models/git" 16 repo_model "code.gitea.io/gitea/models/repo" 17 "code.gitea.io/gitea/models/unit" 18 "code.gitea.io/gitea/modules/base" 19 "code.gitea.io/gitea/modules/context" 20 "code.gitea.io/gitea/modules/git" 21 "code.gitea.io/gitea/modules/log" 22 repo_module "code.gitea.io/gitea/modules/repository" 23 "code.gitea.io/gitea/modules/setting" 24 "code.gitea.io/gitea/modules/util" 25 "code.gitea.io/gitea/modules/web" 26 "code.gitea.io/gitea/routers/utils" 27 "code.gitea.io/gitea/services/forms" 28 release_service "code.gitea.io/gitea/services/release" 29 repo_service "code.gitea.io/gitea/services/repository" 30 ) 31 32 const ( 33 tplBranch base.TplName = "repo/branch/list" 34 ) 35 36 // Branches render repository branch page 37 func Branches(ctx *context.Context) { 38 ctx.Data["Title"] = "Branches" 39 ctx.Data["IsRepoToolbarBranches"] = true 40 ctx.Data["AllowsPulls"] = ctx.Repo.Repository.AllowsPulls() 41 ctx.Data["IsWriter"] = ctx.Repo.CanWrite(unit.TypeCode) 42 ctx.Data["IsMirror"] = ctx.Repo.Repository.IsMirror 43 ctx.Data["CanPull"] = ctx.Repo.CanWrite(unit.TypeCode) || 44 (ctx.IsSigned && repo_model.HasForkedRepo(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID)) 45 ctx.Data["PageIsViewCode"] = true 46 ctx.Data["PageIsBranches"] = true 47 48 page := ctx.FormInt("page") 49 if page <= 1 { 50 page = 1 51 } 52 pageSize := setting.Git.BranchesRangeSize 53 54 kw := ctx.FormString("q") 55 56 defaultBranch, branches, branchesCount, err := repo_service.LoadBranches(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, util.OptionalBoolNone, kw, page, pageSize) 57 if err != nil { 58 ctx.ServerError("LoadBranches", err) 59 return 60 } 61 62 commitIDs := []string{defaultBranch.DBBranch.CommitID} 63 for _, branch := range branches { 64 commitIDs = append(commitIDs, branch.DBBranch.CommitID) 65 } 66 67 commitStatuses, err := git_model.GetLatestCommitStatusForRepoCommitIDs(ctx, ctx.Repo.Repository.ID, commitIDs) 68 if err != nil { 69 ctx.ServerError("LoadBranches", err) 70 return 71 } 72 73 commitStatus := make(map[string]*git_model.CommitStatus) 74 for commitID, cs := range commitStatuses { 75 commitStatus[commitID] = git_model.CalcCommitStatus(cs) 76 } 77 78 ctx.Data["Keyword"] = kw 79 ctx.Data["Branches"] = branches 80 ctx.Data["CommitStatus"] = commitStatus 81 ctx.Data["CommitStatuses"] = commitStatuses 82 ctx.Data["DefaultBranchBranch"] = defaultBranch 83 pager := context.NewPagination(int(branchesCount), pageSize, page, 5) 84 pager.SetDefaultParams(ctx) 85 ctx.Data["Page"] = pager 86 87 ctx.HTML(http.StatusOK, tplBranch) 88 } 89 90 // DeleteBranchPost responses for delete merged branch 91 func DeleteBranchPost(ctx *context.Context) { 92 defer redirect(ctx) 93 branchName := ctx.FormString("name") 94 95 if err := repo_service.DeleteBranch(ctx, ctx.Doer, ctx.Repo.Repository, ctx.Repo.GitRepo, branchName); err != nil { 96 switch { 97 case git.IsErrBranchNotExist(err): 98 log.Debug("DeleteBranch: Can't delete non existing branch '%s'", branchName) 99 ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", branchName)) 100 case errors.Is(err, repo_service.ErrBranchIsDefault): 101 log.Debug("DeleteBranch: Can't delete default branch '%s'", branchName) 102 ctx.Flash.Error(ctx.Tr("repo.branch.default_deletion_failed", branchName)) 103 case errors.Is(err, git_model.ErrBranchIsProtected): 104 log.Debug("DeleteBranch: Can't delete protected branch '%s'", branchName) 105 ctx.Flash.Error(ctx.Tr("repo.branch.protected_deletion_failed", branchName)) 106 default: 107 log.Error("DeleteBranch: %v", err) 108 ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", branchName)) 109 } 110 111 return 112 } 113 114 ctx.Flash.Success(ctx.Tr("repo.branch.deletion_success", branchName)) 115 } 116 117 // RestoreBranchPost responses for delete merged branch 118 func RestoreBranchPost(ctx *context.Context) { 119 defer redirect(ctx) 120 121 branchID := ctx.FormInt64("branch_id") 122 branchName := ctx.FormString("name") 123 124 deletedBranch, err := git_model.GetDeletedBranchByID(ctx, ctx.Repo.Repository.ID, branchID) 125 if err != nil { 126 log.Error("GetDeletedBranchByID: %v", err) 127 ctx.Flash.Error(ctx.Tr("repo.branch.restore_failed", branchName)) 128 return 129 } else if deletedBranch == nil { 130 log.Debug("RestoreBranch: Can't restore branch[%d] '%s', as it does not exist", branchID, branchName) 131 ctx.Flash.Error(ctx.Tr("repo.branch.restore_failed", branchName)) 132 return 133 } 134 135 if err := git.Push(ctx, ctx.Repo.Repository.RepoPath(), git.PushOptions{ 136 Remote: ctx.Repo.Repository.RepoPath(), 137 Branch: fmt.Sprintf("%s:%s%s", deletedBranch.CommitID, git.BranchPrefix, deletedBranch.Name), 138 Env: repo_module.PushingEnvironment(ctx.Doer, ctx.Repo.Repository), 139 }); err != nil { 140 if strings.Contains(err.Error(), "already exists") { 141 log.Debug("RestoreBranch: Can't restore branch '%s', since one with same name already exist", deletedBranch.Name) 142 ctx.Flash.Error(ctx.Tr("repo.branch.already_exists", deletedBranch.Name)) 143 return 144 } 145 log.Error("RestoreBranch: CreateBranch: %v", err) 146 ctx.Flash.Error(ctx.Tr("repo.branch.restore_failed", deletedBranch.Name)) 147 return 148 } 149 150 // Don't return error below this 151 if err := repo_service.PushUpdate( 152 &repo_module.PushUpdateOptions{ 153 RefFullName: git.RefNameFromBranch(deletedBranch.Name), 154 OldCommitID: git.EmptySHA, 155 NewCommitID: deletedBranch.CommitID, 156 PusherID: ctx.Doer.ID, 157 PusherName: ctx.Doer.Name, 158 RepoUserName: ctx.Repo.Owner.Name, 159 RepoName: ctx.Repo.Repository.Name, 160 }); err != nil { 161 log.Error("RestoreBranch: Update: %v", err) 162 } 163 164 ctx.Flash.Success(ctx.Tr("repo.branch.restore_success", deletedBranch.Name)) 165 } 166 167 func redirect(ctx *context.Context) { 168 ctx.JSONRedirect(ctx.Repo.RepoLink + "/branches?page=" + url.QueryEscape(ctx.FormString("page"))) 169 } 170 171 // CreateBranch creates new branch in repository 172 func CreateBranch(ctx *context.Context) { 173 form := web.GetForm(ctx).(*forms.NewBranchForm) 174 if !ctx.Repo.CanCreateBranch() { 175 ctx.NotFound("CreateBranch", nil) 176 return 177 } 178 179 if ctx.HasError() { 180 ctx.Flash.Error(ctx.GetErrMsg()) 181 ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL()) 182 return 183 } 184 185 var err error 186 187 if form.CreateTag { 188 target := ctx.Repo.CommitID 189 if ctx.Repo.IsViewBranch { 190 target = ctx.Repo.BranchName 191 } 192 err = release_service.CreateNewTag(ctx, ctx.Doer, ctx.Repo.Repository, target, form.NewBranchName, "") 193 } else if ctx.Repo.IsViewBranch { 194 err = repo_service.CreateNewBranch(ctx, ctx.Doer, ctx.Repo.Repository, ctx.Repo.GitRepo, ctx.Repo.BranchName, form.NewBranchName) 195 } else { 196 err = repo_service.CreateNewBranchFromCommit(ctx, ctx.Doer, ctx.Repo.Repository, ctx.Repo.GitRepo, ctx.Repo.CommitID, form.NewBranchName) 197 } 198 if err != nil { 199 if models.IsErrProtectedTagName(err) { 200 ctx.Flash.Error(ctx.Tr("repo.release.tag_name_protected")) 201 ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL()) 202 return 203 } 204 205 if models.IsErrTagAlreadyExists(err) { 206 e := err.(models.ErrTagAlreadyExists) 207 ctx.Flash.Error(ctx.Tr("repo.branch.tag_collision", e.TagName)) 208 ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL()) 209 return 210 } 211 if git_model.IsErrBranchAlreadyExists(err) || git.IsErrPushOutOfDate(err) { 212 ctx.Flash.Error(ctx.Tr("repo.branch.branch_already_exists", form.NewBranchName)) 213 ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL()) 214 return 215 } 216 if git_model.IsErrBranchNameConflict(err) { 217 e := err.(git_model.ErrBranchNameConflict) 218 ctx.Flash.Error(ctx.Tr("repo.branch.branch_name_conflict", form.NewBranchName, e.BranchName)) 219 ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL()) 220 return 221 } 222 if git.IsErrPushRejected(err) { 223 e := err.(*git.ErrPushRejected) 224 if len(e.Message) == 0 { 225 ctx.Flash.Error(ctx.Tr("repo.editor.push_rejected_no_message")) 226 } else { 227 flashError, err := ctx.RenderToString(tplAlertDetails, map[string]any{ 228 "Message": ctx.Tr("repo.editor.push_rejected"), 229 "Summary": ctx.Tr("repo.editor.push_rejected_summary"), 230 "Details": utils.SanitizeFlashErrorString(e.Message), 231 }) 232 if err != nil { 233 ctx.ServerError("UpdatePullRequest.HTMLString", err) 234 return 235 } 236 ctx.Flash.Error(flashError) 237 } 238 ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL()) 239 return 240 } 241 242 ctx.ServerError("CreateNewBranch", err) 243 return 244 } 245 246 if form.CreateTag { 247 ctx.Flash.Success(ctx.Tr("repo.tag.create_success", form.NewBranchName)) 248 ctx.Redirect(ctx.Repo.RepoLink + "/src/tag/" + util.PathEscapeSegments(form.NewBranchName)) 249 return 250 } 251 252 ctx.Flash.Success(ctx.Tr("repo.branch.create_success", form.NewBranchName)) 253 ctx.Redirect(ctx.Repo.RepoLink + "/src/branch/" + util.PathEscapeSegments(form.NewBranchName) + "/" + util.PathEscapeSegments(form.CurrentPath)) 254 }