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 10KB

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