You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

branch.go 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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/repofiles"
  15. "code.gitea.io/gitea/modules/util"
  16. )
  17. const (
  18. tplBranch base.TplName = "repo/branch/list"
  19. )
  20. // Branch contains the branch information
  21. type Branch struct {
  22. Name string
  23. Commit *git.Commit
  24. IsProtected bool
  25. IsDeleted bool
  26. IsIncluded bool
  27. DeletedBranch *models.DeletedBranch
  28. CommitsAhead int
  29. CommitsBehind int
  30. LatestPullRequest *models.PullRequest
  31. MergeMovedOn bool
  32. }
  33. // Branches render repository branch page
  34. func Branches(ctx *context.Context) {
  35. ctx.Data["Title"] = "Branches"
  36. ctx.Data["IsRepoToolbarBranches"] = true
  37. ctx.Data["DefaultBranch"] = ctx.Repo.Repository.DefaultBranch
  38. ctx.Data["AllowsPulls"] = ctx.Repo.Repository.AllowsPulls()
  39. ctx.Data["IsWriter"] = ctx.Repo.CanWrite(models.UnitTypeCode)
  40. ctx.Data["IsMirror"] = ctx.Repo.Repository.IsMirror
  41. ctx.Data["PageIsViewCode"] = true
  42. ctx.Data["PageIsBranches"] = true
  43. ctx.Data["Branches"] = loadBranches(ctx)
  44. ctx.HTML(200, tplBranch)
  45. }
  46. // DeleteBranchPost responses for delete merged branch
  47. func DeleteBranchPost(ctx *context.Context) {
  48. defer redirect(ctx)
  49. branchName := ctx.Query("name")
  50. isProtected, err := ctx.Repo.Repository.IsProtectedBranch(branchName, ctx.User)
  51. if err != nil {
  52. log.Error("DeleteBranch: %v", err)
  53. ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", branchName))
  54. return
  55. }
  56. if isProtected {
  57. ctx.Flash.Error(ctx.Tr("repo.branch.protected_deletion_failed", branchName))
  58. return
  59. }
  60. if !ctx.Repo.GitRepo.IsBranchExist(branchName) || branchName == ctx.Repo.Repository.DefaultBranch {
  61. ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", branchName))
  62. return
  63. }
  64. if err := deleteBranch(ctx, branchName); err != nil {
  65. ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", branchName))
  66. return
  67. }
  68. ctx.Flash.Success(ctx.Tr("repo.branch.deletion_success", branchName))
  69. }
  70. // RestoreBranchPost responses for delete merged branch
  71. func RestoreBranchPost(ctx *context.Context) {
  72. defer redirect(ctx)
  73. branchID := ctx.QueryInt64("branch_id")
  74. branchName := ctx.Query("name")
  75. deletedBranch, err := ctx.Repo.Repository.GetDeletedBranchByID(branchID)
  76. if err != nil {
  77. log.Error("GetDeletedBranchByID: %v", err)
  78. ctx.Flash.Error(ctx.Tr("repo.branch.restore_failed", branchName))
  79. return
  80. }
  81. if err := ctx.Repo.GitRepo.CreateBranch(deletedBranch.Name, deletedBranch.Commit); err != nil {
  82. if strings.Contains(err.Error(), "already exists") {
  83. ctx.Flash.Error(ctx.Tr("repo.branch.already_exists", deletedBranch.Name))
  84. return
  85. }
  86. log.Error("CreateBranch: %v", err)
  87. ctx.Flash.Error(ctx.Tr("repo.branch.restore_failed", deletedBranch.Name))
  88. return
  89. }
  90. if err := ctx.Repo.Repository.RemoveDeletedBranch(deletedBranch.ID); err != nil {
  91. log.Error("RemoveDeletedBranch: %v", err)
  92. ctx.Flash.Error(ctx.Tr("repo.branch.restore_failed", deletedBranch.Name))
  93. return
  94. }
  95. // Don't return error below this
  96. if err := repofiles.PushUpdate(
  97. ctx.Repo.Repository,
  98. deletedBranch.Name,
  99. repofiles.PushUpdateOptions{
  100. RefFullName: git.BranchPrefix + deletedBranch.Name,
  101. OldCommitID: git.EmptySHA,
  102. NewCommitID: deletedBranch.Commit,
  103. PusherID: ctx.User.ID,
  104. PusherName: ctx.User.Name,
  105. RepoUserName: ctx.Repo.Owner.Name,
  106. RepoName: ctx.Repo.Repository.Name,
  107. }); err != nil {
  108. log.Error("Update: %v", err)
  109. }
  110. ctx.Flash.Success(ctx.Tr("repo.branch.restore_success", deletedBranch.Name))
  111. }
  112. func redirect(ctx *context.Context) {
  113. ctx.JSON(200, map[string]interface{}{
  114. "redirect": ctx.Repo.RepoLink + "/branches",
  115. })
  116. }
  117. func deleteBranch(ctx *context.Context, branchName string) error {
  118. commit, err := ctx.Repo.GitRepo.GetBranchCommit(branchName)
  119. if err != nil {
  120. log.Error("GetBranchCommit: %v", err)
  121. return err
  122. }
  123. if err := ctx.Repo.GitRepo.DeleteBranch(branchName, git.DeleteBranchOptions{
  124. Force: true,
  125. }); err != nil {
  126. log.Error("DeleteBranch: %v", err)
  127. return err
  128. }
  129. // Don't return error below this
  130. if err := repofiles.PushUpdate(
  131. ctx.Repo.Repository,
  132. branchName,
  133. repofiles.PushUpdateOptions{
  134. RefFullName: git.BranchPrefix + branchName,
  135. OldCommitID: commit.ID.String(),
  136. NewCommitID: git.EmptySHA,
  137. PusherID: ctx.User.ID,
  138. PusherName: ctx.User.Name,
  139. RepoUserName: ctx.Repo.Owner.Name,
  140. RepoName: ctx.Repo.Repository.Name,
  141. }); err != nil {
  142. log.Error("Update: %v", err)
  143. }
  144. if err := ctx.Repo.Repository.AddDeletedBranch(branchName, commit.ID.String(), ctx.User.ID); err != nil {
  145. log.Warn("AddDeletedBranch: %v", err)
  146. }
  147. return nil
  148. }
  149. func loadBranches(ctx *context.Context) []*Branch {
  150. rawBranches, err := ctx.Repo.Repository.GetBranches()
  151. if err != nil {
  152. ctx.ServerError("GetBranches", err)
  153. return nil
  154. }
  155. protectedBranches, err := ctx.Repo.Repository.GetProtectedBranches()
  156. if err != nil {
  157. ctx.ServerError("GetProtectedBranches", err)
  158. return nil
  159. }
  160. repoIDToRepo := map[int64]*models.Repository{}
  161. repoIDToRepo[ctx.Repo.Repository.ID] = ctx.Repo.Repository
  162. repoIDToGitRepo := map[int64]*git.Repository{}
  163. repoIDToGitRepo[ctx.Repo.Repository.ID] = ctx.Repo.GitRepo
  164. branches := make([]*Branch, len(rawBranches))
  165. for i := range rawBranches {
  166. commit, err := rawBranches[i].GetCommit()
  167. if err != nil {
  168. ctx.ServerError("GetCommit", err)
  169. return nil
  170. }
  171. var isProtected bool
  172. branchName := rawBranches[i].Name
  173. for _, b := range protectedBranches {
  174. if b.BranchName == branchName {
  175. isProtected = true
  176. break
  177. }
  178. }
  179. divergence, divergenceError := repofiles.CountDivergingCommits(ctx.Repo.Repository, branchName)
  180. if divergenceError != nil {
  181. ctx.ServerError("CountDivergingCommits", divergenceError)
  182. return nil
  183. }
  184. pr, err := models.GetLatestPullRequestByHeadInfo(ctx.Repo.Repository.ID, branchName)
  185. if err != nil {
  186. ctx.ServerError("GetLatestPullRequestByHeadInfo", err)
  187. return nil
  188. }
  189. headCommit := commit.ID.String()
  190. mergeMovedOn := false
  191. if pr != nil {
  192. pr.HeadRepo = ctx.Repo.Repository
  193. if err := pr.LoadIssue(); err != nil {
  194. ctx.ServerError("pr.LoadIssue", err)
  195. return nil
  196. }
  197. if repo, ok := repoIDToRepo[pr.BaseRepoID]; ok {
  198. pr.BaseRepo = repo
  199. } else if err := pr.LoadBaseRepo(); err != nil {
  200. ctx.ServerError("pr.LoadBaseRepo", err)
  201. return nil
  202. } else {
  203. repoIDToRepo[pr.BaseRepoID] = pr.BaseRepo
  204. }
  205. pr.Issue.Repo = pr.BaseRepo
  206. if pr.HasMerged {
  207. baseGitRepo, ok := repoIDToGitRepo[pr.BaseRepoID]
  208. if !ok {
  209. baseGitRepo, err = git.OpenRepository(pr.BaseRepo.RepoPath())
  210. if err != nil {
  211. ctx.ServerError("OpenRepository", err)
  212. return nil
  213. }
  214. defer baseGitRepo.Close()
  215. repoIDToGitRepo[pr.BaseRepoID] = baseGitRepo
  216. }
  217. pullCommit, err := baseGitRepo.GetRefCommitID(pr.GetGitRefName())
  218. if err != nil && !git.IsErrNotExist(err) {
  219. ctx.ServerError("GetBranchCommitID", err)
  220. return nil
  221. }
  222. if err == nil && headCommit != pullCommit {
  223. // the head has moved on from the merge - we shouldn't delete
  224. mergeMovedOn = true
  225. }
  226. }
  227. }
  228. isIncluded := divergence.Ahead == 0 && ctx.Repo.Repository.DefaultBranch != branchName
  229. branches[i] = &Branch{
  230. Name: branchName,
  231. Commit: commit,
  232. IsProtected: isProtected,
  233. IsIncluded: isIncluded,
  234. CommitsAhead: divergence.Ahead,
  235. CommitsBehind: divergence.Behind,
  236. LatestPullRequest: pr,
  237. MergeMovedOn: mergeMovedOn,
  238. }
  239. }
  240. if ctx.Repo.CanWrite(models.UnitTypeCode) {
  241. deletedBranches, err := getDeletedBranches(ctx)
  242. if err != nil {
  243. ctx.ServerError("getDeletedBranches", err)
  244. return nil
  245. }
  246. branches = append(branches, deletedBranches...)
  247. }
  248. return branches
  249. }
  250. func getDeletedBranches(ctx *context.Context) ([]*Branch, error) {
  251. branches := []*Branch{}
  252. deletedBranches, err := ctx.Repo.Repository.GetDeletedBranches()
  253. if err != nil {
  254. return branches, err
  255. }
  256. for i := range deletedBranches {
  257. deletedBranches[i].LoadUser()
  258. branches = append(branches, &Branch{
  259. Name: deletedBranches[i].Name,
  260. IsDeleted: true,
  261. DeletedBranch: deletedBranches[i],
  262. })
  263. }
  264. return branches, nil
  265. }
  266. // CreateBranch creates new branch in repository
  267. func CreateBranch(ctx *context.Context, form auth.NewBranchForm) {
  268. if !ctx.Repo.CanCreateBranch() {
  269. ctx.NotFound("CreateBranch", nil)
  270. return
  271. }
  272. if ctx.HasError() {
  273. ctx.Flash.Error(ctx.GetErrMsg())
  274. ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL())
  275. return
  276. }
  277. var err error
  278. if ctx.Repo.IsViewBranch {
  279. err = ctx.Repo.Repository.CreateNewBranch(ctx.User, ctx.Repo.BranchName, form.NewBranchName)
  280. } else {
  281. err = ctx.Repo.Repository.CreateNewBranchFromCommit(ctx.User, ctx.Repo.BranchName, form.NewBranchName)
  282. }
  283. if err != nil {
  284. if models.IsErrTagAlreadyExists(err) {
  285. e := err.(models.ErrTagAlreadyExists)
  286. ctx.Flash.Error(ctx.Tr("repo.branch.tag_collision", e.TagName))
  287. ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL())
  288. return
  289. }
  290. if models.IsErrBranchAlreadyExists(err) {
  291. e := err.(models.ErrBranchAlreadyExists)
  292. ctx.Flash.Error(ctx.Tr("repo.branch.branch_already_exists", e.BranchName))
  293. ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL())
  294. return
  295. }
  296. if models.IsErrBranchNameConflict(err) {
  297. e := err.(models.ErrBranchNameConflict)
  298. ctx.Flash.Error(ctx.Tr("repo.branch.branch_name_conflict", form.NewBranchName, e.BranchName))
  299. ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL())
  300. return
  301. }
  302. ctx.ServerError("CreateNewBranch", err)
  303. return
  304. }
  305. ctx.Flash.Success(ctx.Tr("repo.branch.create_success", form.NewBranchName))
  306. ctx.Redirect(ctx.Repo.RepoLink + "/src/branch/" + util.PathEscapeSegments(form.NewBranchName))
  307. }