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