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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. "errors"
  8. "fmt"
  9. "net/http"
  10. "strings"
  11. "code.gitea.io/gitea/models"
  12. repo_model "code.gitea.io/gitea/models/repo"
  13. "code.gitea.io/gitea/models/unit"
  14. "code.gitea.io/gitea/modules/base"
  15. "code.gitea.io/gitea/modules/context"
  16. "code.gitea.io/gitea/modules/git"
  17. "code.gitea.io/gitea/modules/log"
  18. repo_module "code.gitea.io/gitea/modules/repository"
  19. "code.gitea.io/gitea/modules/setting"
  20. "code.gitea.io/gitea/modules/util"
  21. "code.gitea.io/gitea/modules/web"
  22. "code.gitea.io/gitea/routers/utils"
  23. "code.gitea.io/gitea/services/forms"
  24. release_service "code.gitea.io/gitea/services/release"
  25. repo_service "code.gitea.io/gitea/services/repository"
  26. files_service "code.gitea.io/gitea/services/repository/files"
  27. )
  28. const (
  29. tplBranch base.TplName = "repo/branch/list"
  30. )
  31. // Branch contains the branch information
  32. type Branch struct {
  33. Name string
  34. Commit *git.Commit
  35. IsProtected bool
  36. IsDeleted bool
  37. IsIncluded bool
  38. DeletedBranch *models.DeletedBranch
  39. CommitsAhead int
  40. CommitsBehind int
  41. LatestPullRequest *models.PullRequest
  42. MergeMovedOn bool
  43. }
  44. // Branches render repository branch page
  45. func Branches(ctx *context.Context) {
  46. ctx.Data["Title"] = "Branches"
  47. ctx.Data["IsRepoToolbarBranches"] = true
  48. ctx.Data["DefaultBranch"] = ctx.Repo.Repository.DefaultBranch
  49. ctx.Data["AllowsPulls"] = ctx.Repo.Repository.AllowsPulls()
  50. ctx.Data["IsWriter"] = ctx.Repo.CanWrite(unit.TypeCode)
  51. ctx.Data["IsMirror"] = ctx.Repo.Repository.IsMirror
  52. ctx.Data["CanPull"] = ctx.Repo.CanWrite(unit.TypeCode) ||
  53. (ctx.IsSigned && repo_model.HasForkedRepo(ctx.Doer.ID, ctx.Repo.Repository.ID))
  54. ctx.Data["PageIsViewCode"] = true
  55. ctx.Data["PageIsBranches"] = true
  56. page := ctx.FormInt("page")
  57. if page <= 1 {
  58. page = 1
  59. }
  60. limit := ctx.FormInt("limit")
  61. if limit <= 0 || limit > setting.Git.BranchesRangeSize {
  62. limit = setting.Git.BranchesRangeSize
  63. }
  64. skip := (page - 1) * limit
  65. log.Debug("Branches: skip: %d limit: %d", skip, limit)
  66. defaultBranchBranch, branches, branchesCount := loadBranches(ctx, skip, limit)
  67. if ctx.Written() {
  68. return
  69. }
  70. ctx.Data["Branches"] = branches
  71. ctx.Data["DefaultBranchBranch"] = defaultBranchBranch
  72. pager := context.NewPagination(int(branchesCount), setting.Git.BranchesRangeSize, page, 5)
  73. pager.SetDefaultParams(ctx)
  74. ctx.Data["Page"] = pager
  75. ctx.HTML(http.StatusOK, tplBranch)
  76. }
  77. // DeleteBranchPost responses for delete merged branch
  78. func DeleteBranchPost(ctx *context.Context) {
  79. defer redirect(ctx)
  80. branchName := ctx.FormString("name")
  81. if err := repo_service.DeleteBranch(ctx.Doer, ctx.Repo.Repository, ctx.Repo.GitRepo, branchName); err != nil {
  82. switch {
  83. case git.IsErrBranchNotExist(err):
  84. log.Debug("DeleteBranch: Can't delete non existing branch '%s'", branchName)
  85. ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", branchName))
  86. case errors.Is(err, repo_service.ErrBranchIsDefault):
  87. log.Debug("DeleteBranch: Can't delete default branch '%s'", branchName)
  88. ctx.Flash.Error(ctx.Tr("repo.branch.default_deletion_failed", branchName))
  89. case errors.Is(err, repo_service.ErrBranchIsProtected):
  90. log.Debug("DeleteBranch: Can't delete protected branch '%s'", branchName)
  91. ctx.Flash.Error(ctx.Tr("repo.branch.protected_deletion_failed", branchName))
  92. default:
  93. log.Error("DeleteBranch: %v", err)
  94. ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", branchName))
  95. }
  96. return
  97. }
  98. ctx.Flash.Success(ctx.Tr("repo.branch.deletion_success", branchName))
  99. }
  100. // RestoreBranchPost responses for delete merged branch
  101. func RestoreBranchPost(ctx *context.Context) {
  102. defer redirect(ctx)
  103. branchID := ctx.FormInt64("branch_id")
  104. branchName := ctx.FormString("name")
  105. deletedBranch, err := models.GetDeletedBranchByID(ctx.Repo.Repository.ID, branchID)
  106. if err != nil {
  107. log.Error("GetDeletedBranchByID: %v", err)
  108. ctx.Flash.Error(ctx.Tr("repo.branch.restore_failed", branchName))
  109. return
  110. }
  111. if err := git.Push(ctx, ctx.Repo.Repository.RepoPath(), git.PushOptions{
  112. Remote: ctx.Repo.Repository.RepoPath(),
  113. Branch: fmt.Sprintf("%s:%s%s", deletedBranch.Commit, git.BranchPrefix, deletedBranch.Name),
  114. Env: repo_module.PushingEnvironment(ctx.Doer, ctx.Repo.Repository),
  115. }); err != nil {
  116. if strings.Contains(err.Error(), "already exists") {
  117. log.Debug("RestoreBranch: Can't restore branch '%s', since one with same name already exist", deletedBranch.Name)
  118. ctx.Flash.Error(ctx.Tr("repo.branch.already_exists", deletedBranch.Name))
  119. return
  120. }
  121. log.Error("RestoreBranch: CreateBranch: %v", err)
  122. ctx.Flash.Error(ctx.Tr("repo.branch.restore_failed", deletedBranch.Name))
  123. return
  124. }
  125. // Don't return error below this
  126. if err := repo_service.PushUpdate(
  127. &repo_module.PushUpdateOptions{
  128. RefFullName: git.BranchPrefix + deletedBranch.Name,
  129. OldCommitID: git.EmptySHA,
  130. NewCommitID: deletedBranch.Commit,
  131. PusherID: ctx.Doer.ID,
  132. PusherName: ctx.Doer.Name,
  133. RepoUserName: ctx.Repo.Owner.Name,
  134. RepoName: ctx.Repo.Repository.Name,
  135. }); err != nil {
  136. log.Error("RestoreBranch: Update: %v", err)
  137. }
  138. ctx.Flash.Success(ctx.Tr("repo.branch.restore_success", deletedBranch.Name))
  139. }
  140. func redirect(ctx *context.Context) {
  141. ctx.JSON(http.StatusOK, map[string]interface{}{
  142. "redirect": ctx.Repo.RepoLink + "/branches",
  143. })
  144. }
  145. // loadBranches loads branches from the repository limited by page & pageSize.
  146. // NOTE: May write to context on error.
  147. func loadBranches(ctx *context.Context, skip, limit int) (*Branch, []*Branch, int) {
  148. defaultBranch, err := ctx.Repo.GitRepo.GetBranch(ctx.Repo.Repository.DefaultBranch)
  149. if err != nil {
  150. if !git.IsErrBranchNotExist(err) {
  151. log.Error("loadBranches: get default branch: %v", err)
  152. ctx.ServerError("GetDefaultBranch", err)
  153. return nil, nil, 0
  154. }
  155. log.Warn("loadBranches: missing default branch %s for %-v", ctx.Repo.Repository.DefaultBranch, ctx.Repo.Repository)
  156. }
  157. rawBranches, totalNumOfBranches, err := ctx.Repo.GitRepo.GetBranches(skip, limit)
  158. if err != nil {
  159. log.Error("GetBranches: %v", err)
  160. ctx.ServerError("GetBranches", err)
  161. return nil, nil, 0
  162. }
  163. protectedBranches, err := models.GetProtectedBranches(ctx.Repo.Repository.ID)
  164. if err != nil {
  165. ctx.ServerError("GetProtectedBranches", err)
  166. return nil, nil, 0
  167. }
  168. repoIDToRepo := map[int64]*repo_model.Repository{}
  169. repoIDToRepo[ctx.Repo.Repository.ID] = ctx.Repo.Repository
  170. repoIDToGitRepo := map[int64]*git.Repository{}
  171. repoIDToGitRepo[ctx.Repo.Repository.ID] = ctx.Repo.GitRepo
  172. var branches []*Branch
  173. for i := range rawBranches {
  174. if defaultBranch != nil && rawBranches[i].Name == defaultBranch.Name {
  175. // Skip default branch
  176. continue
  177. }
  178. branch := loadOneBranch(ctx, rawBranches[i], defaultBranch, protectedBranches, repoIDToRepo, repoIDToGitRepo)
  179. if branch == nil {
  180. return nil, nil, 0
  181. }
  182. branches = append(branches, branch)
  183. }
  184. var defaultBranchBranch *Branch
  185. if defaultBranch != nil {
  186. // Always add the default branch
  187. log.Debug("loadOneBranch: load default: '%s'", defaultBranch.Name)
  188. defaultBranchBranch = loadOneBranch(ctx, defaultBranch, defaultBranch, protectedBranches, repoIDToRepo, repoIDToGitRepo)
  189. branches = append(branches, defaultBranchBranch)
  190. }
  191. if ctx.Repo.CanWrite(unit.TypeCode) {
  192. deletedBranches, err := getDeletedBranches(ctx)
  193. if err != nil {
  194. ctx.ServerError("getDeletedBranches", err)
  195. return nil, nil, 0
  196. }
  197. branches = append(branches, deletedBranches...)
  198. }
  199. return defaultBranchBranch, branches, totalNumOfBranches
  200. }
  201. func loadOneBranch(ctx *context.Context, rawBranch, defaultBranch *git.Branch, protectedBranches []*models.ProtectedBranch,
  202. repoIDToRepo map[int64]*repo_model.Repository,
  203. repoIDToGitRepo map[int64]*git.Repository,
  204. ) *Branch {
  205. log.Trace("loadOneBranch: '%s'", rawBranch.Name)
  206. commit, err := rawBranch.GetCommit()
  207. if err != nil {
  208. ctx.ServerError("GetCommit", err)
  209. return nil
  210. }
  211. branchName := rawBranch.Name
  212. var isProtected bool
  213. for _, b := range protectedBranches {
  214. if b.BranchName == branchName {
  215. isProtected = true
  216. break
  217. }
  218. }
  219. divergence := &git.DivergeObject{
  220. Ahead: -1,
  221. Behind: -1,
  222. }
  223. if defaultBranch != nil {
  224. divergence, err = files_service.CountDivergingCommits(ctx, ctx.Repo.Repository, git.BranchPrefix+branchName)
  225. if err != nil {
  226. log.Error("CountDivergingCommits", err)
  227. }
  228. }
  229. pr, err := models.GetLatestPullRequestByHeadInfo(ctx.Repo.Repository.ID, branchName)
  230. if err != nil {
  231. ctx.ServerError("GetLatestPullRequestByHeadInfo", err)
  232. return nil
  233. }
  234. headCommit := commit.ID.String()
  235. mergeMovedOn := false
  236. if pr != nil {
  237. pr.HeadRepo = ctx.Repo.Repository
  238. if err := pr.LoadIssue(); err != nil {
  239. ctx.ServerError("pr.LoadIssue", err)
  240. return nil
  241. }
  242. if repo, ok := repoIDToRepo[pr.BaseRepoID]; ok {
  243. pr.BaseRepo = repo
  244. } else if err := pr.LoadBaseRepoCtx(ctx); err != nil {
  245. ctx.ServerError("pr.LoadBaseRepo", err)
  246. return nil
  247. } else {
  248. repoIDToRepo[pr.BaseRepoID] = pr.BaseRepo
  249. }
  250. pr.Issue.Repo = pr.BaseRepo
  251. if pr.HasMerged {
  252. baseGitRepo, ok := repoIDToGitRepo[pr.BaseRepoID]
  253. if !ok {
  254. baseGitRepo, err = git.OpenRepository(ctx, pr.BaseRepo.RepoPath())
  255. if err != nil {
  256. ctx.ServerError("OpenRepository", err)
  257. return nil
  258. }
  259. defer baseGitRepo.Close()
  260. repoIDToGitRepo[pr.BaseRepoID] = baseGitRepo
  261. }
  262. pullCommit, err := baseGitRepo.GetRefCommitID(pr.GetGitRefName())
  263. if err != nil && !git.IsErrNotExist(err) {
  264. ctx.ServerError("GetBranchCommitID", err)
  265. return nil
  266. }
  267. if err == nil && headCommit != pullCommit {
  268. // the head has moved on from the merge - we shouldn't delete
  269. mergeMovedOn = true
  270. }
  271. }
  272. }
  273. isIncluded := divergence.Ahead == 0 && ctx.Repo.Repository.DefaultBranch != branchName
  274. return &Branch{
  275. Name: branchName,
  276. Commit: commit,
  277. IsProtected: isProtected,
  278. IsIncluded: isIncluded,
  279. CommitsAhead: divergence.Ahead,
  280. CommitsBehind: divergence.Behind,
  281. LatestPullRequest: pr,
  282. MergeMovedOn: mergeMovedOn,
  283. }
  284. }
  285. func getDeletedBranches(ctx *context.Context) ([]*Branch, error) {
  286. branches := []*Branch{}
  287. deletedBranches, err := models.GetDeletedBranches(ctx.Repo.Repository.ID)
  288. if err != nil {
  289. return branches, err
  290. }
  291. for i := range deletedBranches {
  292. deletedBranches[i].LoadUser()
  293. branches = append(branches, &Branch{
  294. Name: deletedBranches[i].Name,
  295. IsDeleted: true,
  296. DeletedBranch: deletedBranches[i],
  297. })
  298. }
  299. return branches, nil
  300. }
  301. // CreateBranch creates new branch in repository
  302. func CreateBranch(ctx *context.Context) {
  303. form := web.GetForm(ctx).(*forms.NewBranchForm)
  304. if !ctx.Repo.CanCreateBranch() {
  305. ctx.NotFound("CreateBranch", nil)
  306. return
  307. }
  308. if ctx.HasError() {
  309. ctx.Flash.Error(ctx.GetErrMsg())
  310. ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL())
  311. return
  312. }
  313. var err error
  314. if form.CreateTag {
  315. target := ctx.Repo.CommitID
  316. if ctx.Repo.IsViewBranch {
  317. target = ctx.Repo.BranchName
  318. }
  319. err = release_service.CreateNewTag(ctx, ctx.Doer, ctx.Repo.Repository, target, form.NewBranchName, "")
  320. } else if ctx.Repo.IsViewBranch {
  321. err = repo_service.CreateNewBranch(ctx, ctx.Doer, ctx.Repo.Repository, ctx.Repo.BranchName, form.NewBranchName)
  322. } else {
  323. err = repo_service.CreateNewBranchFromCommit(ctx, ctx.Doer, ctx.Repo.Repository, ctx.Repo.CommitID, form.NewBranchName)
  324. }
  325. if err != nil {
  326. if models.IsErrTagAlreadyExists(err) {
  327. e := err.(models.ErrTagAlreadyExists)
  328. ctx.Flash.Error(ctx.Tr("repo.branch.tag_collision", e.TagName))
  329. ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL())
  330. return
  331. }
  332. if models.IsErrBranchAlreadyExists(err) || git.IsErrPushOutOfDate(err) {
  333. ctx.Flash.Error(ctx.Tr("repo.branch.branch_already_exists", form.NewBranchName))
  334. ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL())
  335. return
  336. }
  337. if models.IsErrBranchNameConflict(err) {
  338. e := err.(models.ErrBranchNameConflict)
  339. ctx.Flash.Error(ctx.Tr("repo.branch.branch_name_conflict", form.NewBranchName, e.BranchName))
  340. ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL())
  341. return
  342. }
  343. if git.IsErrPushRejected(err) {
  344. e := err.(*git.ErrPushRejected)
  345. if len(e.Message) == 0 {
  346. ctx.Flash.Error(ctx.Tr("repo.editor.push_rejected_no_message"))
  347. } else {
  348. flashError, err := ctx.RenderToString(tplAlertDetails, map[string]interface{}{
  349. "Message": ctx.Tr("repo.editor.push_rejected"),
  350. "Summary": ctx.Tr("repo.editor.push_rejected_summary"),
  351. "Details": utils.SanitizeFlashErrorString(e.Message),
  352. })
  353. if err != nil {
  354. ctx.ServerError("UpdatePullRequest.HTMLString", err)
  355. return
  356. }
  357. ctx.Flash.Error(flashError)
  358. }
  359. ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL())
  360. return
  361. }
  362. ctx.ServerError("CreateNewBranch", err)
  363. return
  364. }
  365. if form.CreateTag {
  366. ctx.Flash.Success(ctx.Tr("repo.tag.create_success", form.NewBranchName))
  367. ctx.Redirect(ctx.Repo.RepoLink + "/src/tag/" + util.PathEscapeSegments(form.NewBranchName))
  368. return
  369. }
  370. ctx.Flash.Success(ctx.Tr("repo.branch.create_success", form.NewBranchName))
  371. ctx.Redirect(ctx.Repo.RepoLink + "/src/branch/" + util.PathEscapeSegments(form.NewBranchName))
  372. }