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

branch.go 10 kB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
6 years ago
11 years ago
11 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
6 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
6 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
6 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
6 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
6 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
6 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. )
  18. const (
  19. tplBranch base.TplName = "repo/branch/list"
  20. )
  21. // Branch contains the branch information
  22. type Branch struct {
  23. Name string
  24. Commit *git.Commit
  25. IsProtected bool
  26. IsDeleted bool
  27. IsIncluded bool
  28. DeletedBranch *models.DeletedBranch
  29. CommitsAhead int
  30. CommitsBehind int
  31. LatestPullRequest *models.PullRequest
  32. MergeMovedOn bool
  33. }
  34. // Branches render repository branch page
  35. func Branches(ctx *context.Context) {
  36. ctx.Data["Title"] = "Branches"
  37. ctx.Data["IsRepoToolbarBranches"] = true
  38. ctx.Data["DefaultBranch"] = ctx.Repo.Repository.DefaultBranch
  39. ctx.Data["AllowsPulls"] = ctx.Repo.Repository.AllowsPulls()
  40. ctx.Data["IsWriter"] = ctx.Repo.CanWrite(models.UnitTypeCode)
  41. ctx.Data["IsMirror"] = ctx.Repo.Repository.IsMirror
  42. ctx.Data["PageIsViewCode"] = true
  43. ctx.Data["PageIsBranches"] = true
  44. ctx.Data["Branches"] = loadBranches(ctx)
  45. ctx.HTML(200, tplBranch)
  46. }
  47. // DeleteBranchPost responses for delete merged branch
  48. func DeleteBranchPost(ctx *context.Context) {
  49. defer redirect(ctx)
  50. branchName := ctx.Query("name")
  51. isProtected, err := ctx.Repo.Repository.IsProtectedBranch(branchName, ctx.User)
  52. if err != nil {
  53. log.Error("DeleteBranch: %v", err)
  54. ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", branchName))
  55. return
  56. }
  57. if isProtected {
  58. ctx.Flash.Error(ctx.Tr("repo.branch.protected_deletion_failed", branchName))
  59. return
  60. }
  61. if !ctx.Repo.GitRepo.IsBranchExist(branchName) || branchName == ctx.Repo.Repository.DefaultBranch {
  62. ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", branchName))
  63. return
  64. }
  65. if err := deleteBranch(ctx, branchName); err != nil {
  66. ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", branchName))
  67. return
  68. }
  69. ctx.Flash.Success(ctx.Tr("repo.branch.deletion_success", branchName))
  70. }
  71. // RestoreBranchPost responses for delete merged branch
  72. func RestoreBranchPost(ctx *context.Context) {
  73. defer redirect(ctx)
  74. branchID := ctx.QueryInt64("branch_id")
  75. branchName := ctx.Query("name")
  76. deletedBranch, err := ctx.Repo.Repository.GetDeletedBranchByID(branchID)
  77. if err != nil {
  78. log.Error("GetDeletedBranchByID: %v", err)
  79. ctx.Flash.Error(ctx.Tr("repo.branch.restore_failed", branchName))
  80. return
  81. }
  82. if err := ctx.Repo.GitRepo.CreateBranch(deletedBranch.Name, deletedBranch.Commit); err != nil {
  83. if strings.Contains(err.Error(), "already exists") {
  84. ctx.Flash.Error(ctx.Tr("repo.branch.already_exists", deletedBranch.Name))
  85. return
  86. }
  87. log.Error("CreateBranch: %v", err)
  88. ctx.Flash.Error(ctx.Tr("repo.branch.restore_failed", deletedBranch.Name))
  89. return
  90. }
  91. if err := ctx.Repo.Repository.RemoveDeletedBranch(deletedBranch.ID); err != nil {
  92. log.Error("RemoveDeletedBranch: %v", err)
  93. ctx.Flash.Error(ctx.Tr("repo.branch.restore_failed", deletedBranch.Name))
  94. return
  95. }
  96. // Don't return error below this
  97. if err := repofiles.PushUpdate(
  98. ctx.Repo.Repository,
  99. deletedBranch.Name,
  100. repofiles.PushUpdateOptions{
  101. RefFullName: git.BranchPrefix + deletedBranch.Name,
  102. OldCommitID: git.EmptySHA,
  103. NewCommitID: deletedBranch.Commit,
  104. PusherID: ctx.User.ID,
  105. PusherName: ctx.User.Name,
  106. RepoUserName: ctx.Repo.Owner.Name,
  107. RepoName: ctx.Repo.Repository.Name,
  108. }); err != nil {
  109. log.Error("Update: %v", err)
  110. }
  111. ctx.Flash.Success(ctx.Tr("repo.branch.restore_success", deletedBranch.Name))
  112. }
  113. func redirect(ctx *context.Context) {
  114. ctx.JSON(200, map[string]interface{}{
  115. "redirect": ctx.Repo.RepoLink + "/branches",
  116. })
  117. }
  118. func deleteBranch(ctx *context.Context, branchName string) error {
  119. commit, err := ctx.Repo.GitRepo.GetBranchCommit(branchName)
  120. if err != nil {
  121. log.Error("GetBranchCommit: %v", err)
  122. return err
  123. }
  124. if err := ctx.Repo.GitRepo.DeleteBranch(branchName, git.DeleteBranchOptions{
  125. Force: true,
  126. }); err != nil {
  127. log.Error("DeleteBranch: %v", err)
  128. return err
  129. }
  130. // Don't return error below this
  131. if err := repofiles.PushUpdate(
  132. ctx.Repo.Repository,
  133. branchName,
  134. repofiles.PushUpdateOptions{
  135. RefFullName: git.BranchPrefix + branchName,
  136. OldCommitID: commit.ID.String(),
  137. NewCommitID: git.EmptySHA,
  138. PusherID: ctx.User.ID,
  139. PusherName: ctx.User.Name,
  140. RepoUserName: ctx.Repo.Owner.Name,
  141. RepoName: ctx.Repo.Repository.Name,
  142. }); err != nil {
  143. log.Error("Update: %v", err)
  144. }
  145. if err := ctx.Repo.Repository.AddDeletedBranch(branchName, commit.ID.String(), ctx.User.ID); err != nil {
  146. log.Warn("AddDeletedBranch: %v", err)
  147. }
  148. return nil
  149. }
  150. func loadBranches(ctx *context.Context) []*Branch {
  151. rawBranches, err := repo_module.GetBranches(ctx.Repo.Repository)
  152. if err != nil {
  153. ctx.ServerError("GetBranches", err)
  154. return nil
  155. }
  156. protectedBranches, err := ctx.Repo.Repository.GetProtectedBranches()
  157. if err != nil {
  158. ctx.ServerError("GetProtectedBranches", err)
  159. return nil
  160. }
  161. repoIDToRepo := map[int64]*models.Repository{}
  162. repoIDToRepo[ctx.Repo.Repository.ID] = ctx.Repo.Repository
  163. repoIDToGitRepo := map[int64]*git.Repository{}
  164. repoIDToGitRepo[ctx.Repo.Repository.ID] = ctx.Repo.GitRepo
  165. branches := make([]*Branch, len(rawBranches))
  166. for i := range rawBranches {
  167. commit, err := rawBranches[i].GetCommit()
  168. if err != nil {
  169. ctx.ServerError("GetCommit", err)
  170. return nil
  171. }
  172. var isProtected bool
  173. branchName := rawBranches[i].Name
  174. for _, b := range protectedBranches {
  175. if b.BranchName == branchName {
  176. isProtected = true
  177. break
  178. }
  179. }
  180. divergence, divergenceError := repofiles.CountDivergingCommits(ctx.Repo.Repository, branchName)
  181. if divergenceError != nil {
  182. ctx.ServerError("CountDivergingCommits", divergenceError)
  183. return nil
  184. }
  185. pr, err := models.GetLatestPullRequestByHeadInfo(ctx.Repo.Repository.ID, branchName)
  186. if err != nil {
  187. ctx.ServerError("GetLatestPullRequestByHeadInfo", err)
  188. return nil
  189. }
  190. headCommit := commit.ID.String()
  191. mergeMovedOn := false
  192. if pr != nil {
  193. pr.HeadRepo = ctx.Repo.Repository
  194. if err := pr.LoadIssue(); err != nil {
  195. ctx.ServerError("pr.LoadIssue", err)
  196. return nil
  197. }
  198. if repo, ok := repoIDToRepo[pr.BaseRepoID]; ok {
  199. pr.BaseRepo = repo
  200. } else if err := pr.LoadBaseRepo(); err != nil {
  201. ctx.ServerError("pr.LoadBaseRepo", err)
  202. return nil
  203. } else {
  204. repoIDToRepo[pr.BaseRepoID] = pr.BaseRepo
  205. }
  206. pr.Issue.Repo = pr.BaseRepo
  207. if pr.HasMerged {
  208. baseGitRepo, ok := repoIDToGitRepo[pr.BaseRepoID]
  209. if !ok {
  210. baseGitRepo, err = git.OpenRepository(pr.BaseRepo.RepoPath())
  211. if err != nil {
  212. ctx.ServerError("OpenRepository", err)
  213. return nil
  214. }
  215. defer baseGitRepo.Close()
  216. repoIDToGitRepo[pr.BaseRepoID] = baseGitRepo
  217. }
  218. pullCommit, err := baseGitRepo.GetRefCommitID(pr.GetGitRefName())
  219. if err != nil && !git.IsErrNotExist(err) {
  220. ctx.ServerError("GetBranchCommitID", err)
  221. return nil
  222. }
  223. if err == nil && headCommit != pullCommit {
  224. // the head has moved on from the merge - we shouldn't delete
  225. mergeMovedOn = true
  226. }
  227. }
  228. }
  229. isIncluded := divergence.Ahead == 0 && ctx.Repo.Repository.DefaultBranch != branchName
  230. branches[i] = &Branch{
  231. Name: branchName,
  232. Commit: commit,
  233. IsProtected: isProtected,
  234. IsIncluded: isIncluded,
  235. CommitsAhead: divergence.Ahead,
  236. CommitsBehind: divergence.Behind,
  237. LatestPullRequest: pr,
  238. MergeMovedOn: mergeMovedOn,
  239. }
  240. }
  241. if ctx.Repo.CanWrite(models.UnitTypeCode) {
  242. deletedBranches, err := getDeletedBranches(ctx)
  243. if err != nil {
  244. ctx.ServerError("getDeletedBranches", err)
  245. return nil
  246. }
  247. branches = append(branches, deletedBranches...)
  248. }
  249. return branches
  250. }
  251. func getDeletedBranches(ctx *context.Context) ([]*Branch, error) {
  252. branches := []*Branch{}
  253. deletedBranches, err := ctx.Repo.Repository.GetDeletedBranches()
  254. if err != nil {
  255. return branches, err
  256. }
  257. for i := range deletedBranches {
  258. deletedBranches[i].LoadUser()
  259. branches = append(branches, &Branch{
  260. Name: deletedBranches[i].Name,
  261. IsDeleted: true,
  262. DeletedBranch: deletedBranches[i],
  263. })
  264. }
  265. return branches, nil
  266. }
  267. // CreateBranch creates new branch in repository
  268. func CreateBranch(ctx *context.Context, form auth.NewBranchForm) {
  269. if !ctx.Repo.CanCreateBranch() {
  270. ctx.NotFound("CreateBranch", nil)
  271. return
  272. }
  273. if ctx.HasError() {
  274. ctx.Flash.Error(ctx.GetErrMsg())
  275. ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL())
  276. return
  277. }
  278. var err error
  279. if ctx.Repo.IsViewBranch {
  280. err = repo_module.CreateNewBranch(ctx.User, ctx.Repo.Repository, ctx.Repo.BranchName, form.NewBranchName)
  281. } else {
  282. err = repo_module.CreateNewBranchFromCommit(ctx.User, ctx.Repo.Repository, ctx.Repo.BranchName, form.NewBranchName)
  283. }
  284. if err != nil {
  285. if models.IsErrTagAlreadyExists(err) {
  286. e := err.(models.ErrTagAlreadyExists)
  287. ctx.Flash.Error(ctx.Tr("repo.branch.tag_collision", e.TagName))
  288. ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL())
  289. return
  290. }
  291. if models.IsErrBranchAlreadyExists(err) {
  292. e := err.(models.ErrBranchAlreadyExists)
  293. ctx.Flash.Error(ctx.Tr("repo.branch.branch_already_exists", e.BranchName))
  294. ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL())
  295. return
  296. }
  297. if models.IsErrBranchNameConflict(err) {
  298. e := err.(models.ErrBranchNameConflict)
  299. ctx.Flash.Error(ctx.Tr("repo.branch.branch_name_conflict", form.NewBranchName, e.BranchName))
  300. ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL())
  301. return
  302. }
  303. ctx.ServerError("CreateNewBranch", err)
  304. return
  305. }
  306. ctx.Flash.Success(ctx.Tr("repo.branch.create_success", form.NewBranchName))
  307. ctx.Redirect(ctx.Repo.RepoLink + "/src/branch/" + util.PathEscapeSegments(form.NewBranchName))
  308. }