Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package repo
  6. import (
  7. "strings"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/auth"
  10. "code.gitea.io/gitea/modules/base"
  11. "code.gitea.io/gitea/modules/context"
  12. "code.gitea.io/gitea/modules/git"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/util"
  15. )
  16. const (
  17. tplBranch base.TplName = "repo/branch/list"
  18. )
  19. // Branch contains the branch information
  20. type Branch struct {
  21. Name string
  22. Commit *git.Commit
  23. IsProtected bool
  24. IsDeleted bool
  25. DeletedBranch *models.DeletedBranch
  26. }
  27. // Branches render repository branch page
  28. func Branches(ctx *context.Context) {
  29. ctx.Data["Title"] = "Branches"
  30. ctx.Data["IsRepoToolbarBranches"] = true
  31. ctx.Data["DefaultBranch"] = ctx.Repo.Repository.DefaultBranch
  32. ctx.Data["IsWriter"] = ctx.Repo.CanWrite(models.UnitTypeCode)
  33. ctx.Data["IsMirror"] = ctx.Repo.Repository.IsMirror
  34. ctx.Data["PageIsViewCode"] = true
  35. ctx.Data["PageIsBranches"] = true
  36. ctx.Data["Branches"] = loadBranches(ctx)
  37. ctx.HTML(200, tplBranch)
  38. }
  39. // DeleteBranchPost responses for delete merged branch
  40. func DeleteBranchPost(ctx *context.Context) {
  41. defer redirect(ctx)
  42. branchName := ctx.Query("name")
  43. isProtected, err := ctx.Repo.Repository.IsProtectedBranch(branchName, ctx.User)
  44. if err != nil {
  45. log.Error("DeleteBranch: %v", err)
  46. ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", branchName))
  47. return
  48. }
  49. if isProtected {
  50. ctx.Flash.Error(ctx.Tr("repo.branch.protected_deletion_failed", branchName))
  51. return
  52. }
  53. if !ctx.Repo.GitRepo.IsBranchExist(branchName) || branchName == ctx.Repo.Repository.DefaultBranch {
  54. ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", branchName))
  55. return
  56. }
  57. if err := deleteBranch(ctx, branchName); err != nil {
  58. ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", branchName))
  59. return
  60. }
  61. // Delete branch in local copy if it exists
  62. if err := ctx.Repo.Repository.DeleteLocalBranch(branchName); err != nil {
  63. ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", branchName))
  64. return
  65. }
  66. ctx.Flash.Success(ctx.Tr("repo.branch.deletion_success", branchName))
  67. }
  68. // RestoreBranchPost responses for delete merged branch
  69. func RestoreBranchPost(ctx *context.Context) {
  70. defer redirect(ctx)
  71. branchID := ctx.QueryInt64("branch_id")
  72. branchName := ctx.Query("name")
  73. deletedBranch, err := ctx.Repo.Repository.GetDeletedBranchByID(branchID)
  74. if err != nil {
  75. log.Error("GetDeletedBranchByID: %v", err)
  76. ctx.Flash.Error(ctx.Tr("repo.branch.restore_failed", branchName))
  77. return
  78. }
  79. if err := ctx.Repo.GitRepo.CreateBranch(deletedBranch.Name, deletedBranch.Commit); err != nil {
  80. if strings.Contains(err.Error(), "already exists") {
  81. ctx.Flash.Error(ctx.Tr("repo.branch.already_exists", deletedBranch.Name))
  82. return
  83. }
  84. log.Error("CreateBranch: %v", err)
  85. ctx.Flash.Error(ctx.Tr("repo.branch.restore_failed", deletedBranch.Name))
  86. return
  87. }
  88. if err := ctx.Repo.Repository.RemoveDeletedBranch(deletedBranch.ID); err != nil {
  89. log.Error("RemoveDeletedBranch: %v", err)
  90. ctx.Flash.Error(ctx.Tr("repo.branch.restore_failed", deletedBranch.Name))
  91. return
  92. }
  93. ctx.Flash.Success(ctx.Tr("repo.branch.restore_success", deletedBranch.Name))
  94. }
  95. func redirect(ctx *context.Context) {
  96. ctx.JSON(200, map[string]interface{}{
  97. "redirect": ctx.Repo.RepoLink + "/branches",
  98. })
  99. }
  100. func deleteBranch(ctx *context.Context, branchName string) error {
  101. commit, err := ctx.Repo.GitRepo.GetBranchCommit(branchName)
  102. if err != nil {
  103. log.Error("GetBranchCommit: %v", err)
  104. return err
  105. }
  106. if err := ctx.Repo.GitRepo.DeleteBranch(branchName, git.DeleteBranchOptions{
  107. Force: true,
  108. }); err != nil {
  109. log.Error("DeleteBranch: %v", err)
  110. return err
  111. }
  112. // Don't return error below this
  113. if err := models.PushUpdate(branchName, models.PushUpdateOptions{
  114. RefFullName: git.BranchPrefix + branchName,
  115. OldCommitID: commit.ID.String(),
  116. NewCommitID: git.EmptySHA,
  117. PusherID: ctx.User.ID,
  118. PusherName: ctx.User.Name,
  119. RepoUserName: ctx.Repo.Owner.Name,
  120. RepoName: ctx.Repo.Repository.Name,
  121. }); err != nil {
  122. log.Error("Update: %v", err)
  123. }
  124. if err := ctx.Repo.Repository.AddDeletedBranch(branchName, commit.ID.String(), ctx.User.ID); err != nil {
  125. log.Warn("AddDeletedBranch: %v", err)
  126. }
  127. return nil
  128. }
  129. func loadBranches(ctx *context.Context) []*Branch {
  130. rawBranches, err := ctx.Repo.Repository.GetBranches()
  131. if err != nil {
  132. ctx.ServerError("GetBranches", err)
  133. return nil
  134. }
  135. branches := make([]*Branch, len(rawBranches))
  136. for i := range rawBranches {
  137. commit, err := rawBranches[i].GetCommit()
  138. if err != nil {
  139. ctx.ServerError("GetCommit", err)
  140. return nil
  141. }
  142. isProtected, err := ctx.Repo.Repository.IsProtectedBranch(rawBranches[i].Name, ctx.User)
  143. if err != nil {
  144. ctx.ServerError("IsProtectedBranch", err)
  145. return nil
  146. }
  147. branches[i] = &Branch{
  148. Name: rawBranches[i].Name,
  149. Commit: commit,
  150. IsProtected: isProtected,
  151. }
  152. }
  153. if ctx.Repo.CanWrite(models.UnitTypeCode) {
  154. deletedBranches, err := getDeletedBranches(ctx)
  155. if err != nil {
  156. ctx.ServerError("getDeletedBranches", err)
  157. return nil
  158. }
  159. branches = append(branches, deletedBranches...)
  160. }
  161. return branches
  162. }
  163. func getDeletedBranches(ctx *context.Context) ([]*Branch, error) {
  164. branches := []*Branch{}
  165. deletedBranches, err := ctx.Repo.Repository.GetDeletedBranches()
  166. if err != nil {
  167. return branches, err
  168. }
  169. for i := range deletedBranches {
  170. deletedBranches[i].LoadUser()
  171. branches = append(branches, &Branch{
  172. Name: deletedBranches[i].Name,
  173. IsDeleted: true,
  174. DeletedBranch: deletedBranches[i],
  175. })
  176. }
  177. return branches, nil
  178. }
  179. // CreateBranch creates new branch in repository
  180. func CreateBranch(ctx *context.Context, form auth.NewBranchForm) {
  181. if !ctx.Repo.CanCreateBranch() {
  182. ctx.NotFound("CreateBranch", nil)
  183. return
  184. }
  185. if ctx.HasError() {
  186. ctx.Flash.Error(ctx.GetErrMsg())
  187. ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL())
  188. return
  189. }
  190. var err error
  191. if ctx.Repo.IsViewBranch {
  192. err = ctx.Repo.Repository.CreateNewBranch(ctx.User, ctx.Repo.BranchName, form.NewBranchName)
  193. } else {
  194. err = ctx.Repo.Repository.CreateNewBranchFromCommit(ctx.User, ctx.Repo.BranchName, form.NewBranchName)
  195. }
  196. if err != nil {
  197. if models.IsErrTagAlreadyExists(err) {
  198. e := err.(models.ErrTagAlreadyExists)
  199. ctx.Flash.Error(ctx.Tr("repo.branch.tag_collision", e.TagName))
  200. ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL())
  201. return
  202. }
  203. if models.IsErrBranchAlreadyExists(err) {
  204. e := err.(models.ErrBranchAlreadyExists)
  205. ctx.Flash.Error(ctx.Tr("repo.branch.branch_already_exists", e.BranchName))
  206. ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL())
  207. return
  208. }
  209. if models.IsErrBranchNameConflict(err) {
  210. e := err.(models.ErrBranchNameConflict)
  211. ctx.Flash.Error(ctx.Tr("repo.branch.branch_name_conflict", form.NewBranchName, e.BranchName))
  212. ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL())
  213. return
  214. }
  215. ctx.ServerError("CreateNewBranch", err)
  216. return
  217. }
  218. ctx.Flash.Success(ctx.Tr("repo.branch.create_success", form.NewBranchName))
  219. ctx.Redirect(ctx.Repo.RepoLink + "/src/branch/" + util.PathEscapeSegments(form.NewBranchName))
  220. }