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 8.3 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. DeletedBranch *models.DeletedBranch
  27. CommitsAhead int
  28. CommitsBehind int
  29. LatestPullRequest *models.PullRequest
  30. }
  31. // Branches render repository branch page
  32. func Branches(ctx *context.Context) {
  33. ctx.Data["Title"] = "Branches"
  34. ctx.Data["IsRepoToolbarBranches"] = true
  35. ctx.Data["DefaultBranch"] = ctx.Repo.Repository.DefaultBranch
  36. ctx.Data["AllowsPulls"] = ctx.Repo.Repository.AllowsPulls()
  37. ctx.Data["IsWriter"] = ctx.Repo.CanWrite(models.UnitTypeCode)
  38. ctx.Data["IsMirror"] = ctx.Repo.Repository.IsMirror
  39. ctx.Data["PageIsViewCode"] = true
  40. ctx.Data["PageIsBranches"] = true
  41. ctx.Data["Branches"] = loadBranches(ctx)
  42. ctx.HTML(200, tplBranch)
  43. }
  44. // DeleteBranchPost responses for delete merged branch
  45. func DeleteBranchPost(ctx *context.Context) {
  46. defer redirect(ctx)
  47. branchName := ctx.Query("name")
  48. isProtected, err := ctx.Repo.Repository.IsProtectedBranch(branchName, ctx.User)
  49. if err != nil {
  50. log.Error("DeleteBranch: %v", err)
  51. ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", branchName))
  52. return
  53. }
  54. if isProtected {
  55. ctx.Flash.Error(ctx.Tr("repo.branch.protected_deletion_failed", branchName))
  56. return
  57. }
  58. if !ctx.Repo.GitRepo.IsBranchExist(branchName) || branchName == ctx.Repo.Repository.DefaultBranch {
  59. ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", branchName))
  60. return
  61. }
  62. if err := deleteBranch(ctx, 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 := repofiles.PushUpdate(
  114. ctx.Repo.Repository,
  115. branchName,
  116. models.PushUpdateOptions{
  117. RefFullName: git.BranchPrefix + branchName,
  118. OldCommitID: commit.ID.String(),
  119. NewCommitID: git.EmptySHA,
  120. PusherID: ctx.User.ID,
  121. PusherName: ctx.User.Name,
  122. RepoUserName: ctx.Repo.Owner.Name,
  123. RepoName: ctx.Repo.Repository.Name,
  124. }); err != nil {
  125. log.Error("Update: %v", err)
  126. }
  127. if err := ctx.Repo.Repository.AddDeletedBranch(branchName, commit.ID.String(), ctx.User.ID); err != nil {
  128. log.Warn("AddDeletedBranch: %v", err)
  129. }
  130. return nil
  131. }
  132. func loadBranches(ctx *context.Context) []*Branch {
  133. rawBranches, err := ctx.Repo.Repository.GetBranches()
  134. if err != nil {
  135. ctx.ServerError("GetBranches", err)
  136. return nil
  137. }
  138. protectedBranches, err := ctx.Repo.Repository.GetProtectedBranches()
  139. if err != nil {
  140. ctx.ServerError("GetProtectedBranches", err)
  141. return nil
  142. }
  143. branches := make([]*Branch, len(rawBranches))
  144. for i := range rawBranches {
  145. commit, err := rawBranches[i].GetCommit()
  146. if err != nil {
  147. ctx.ServerError("GetCommit", err)
  148. return nil
  149. }
  150. var isProtected bool
  151. branchName := rawBranches[i].Name
  152. for _, b := range protectedBranches {
  153. if b.BranchName == branchName {
  154. isProtected = true
  155. break
  156. }
  157. }
  158. divergence, divergenceError := repofiles.CountDivergingCommits(ctx.Repo.Repository, branchName)
  159. if divergenceError != nil {
  160. ctx.ServerError("CountDivergingCommits", divergenceError)
  161. return nil
  162. }
  163. pr, err := models.GetLatestPullRequestByHeadInfo(ctx.Repo.Repository.ID, branchName)
  164. if err != nil {
  165. ctx.ServerError("GetLatestPullRequestByHeadInfo", err)
  166. return nil
  167. }
  168. if pr != nil {
  169. if err := pr.LoadIssue(); err != nil {
  170. ctx.ServerError("pr.LoadIssue", err)
  171. return nil
  172. }
  173. }
  174. branches[i] = &Branch{
  175. Name: branchName,
  176. Commit: commit,
  177. IsProtected: isProtected,
  178. CommitsAhead: divergence.Ahead,
  179. CommitsBehind: divergence.Behind,
  180. LatestPullRequest: pr,
  181. }
  182. }
  183. if ctx.Repo.CanWrite(models.UnitTypeCode) {
  184. deletedBranches, err := getDeletedBranches(ctx)
  185. if err != nil {
  186. ctx.ServerError("getDeletedBranches", err)
  187. return nil
  188. }
  189. branches = append(branches, deletedBranches...)
  190. }
  191. return branches
  192. }
  193. func getDeletedBranches(ctx *context.Context) ([]*Branch, error) {
  194. branches := []*Branch{}
  195. deletedBranches, err := ctx.Repo.Repository.GetDeletedBranches()
  196. if err != nil {
  197. return branches, err
  198. }
  199. for i := range deletedBranches {
  200. deletedBranches[i].LoadUser()
  201. branches = append(branches, &Branch{
  202. Name: deletedBranches[i].Name,
  203. IsDeleted: true,
  204. DeletedBranch: deletedBranches[i],
  205. })
  206. }
  207. return branches, nil
  208. }
  209. // CreateBranch creates new branch in repository
  210. func CreateBranch(ctx *context.Context, form auth.NewBranchForm) {
  211. if !ctx.Repo.CanCreateBranch() {
  212. ctx.NotFound("CreateBranch", nil)
  213. return
  214. }
  215. if ctx.HasError() {
  216. ctx.Flash.Error(ctx.GetErrMsg())
  217. ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL())
  218. return
  219. }
  220. var err error
  221. if ctx.Repo.IsViewBranch {
  222. err = ctx.Repo.Repository.CreateNewBranch(ctx.User, ctx.Repo.BranchName, form.NewBranchName)
  223. } else {
  224. err = ctx.Repo.Repository.CreateNewBranchFromCommit(ctx.User, ctx.Repo.BranchName, form.NewBranchName)
  225. }
  226. if err != nil {
  227. if models.IsErrTagAlreadyExists(err) {
  228. e := err.(models.ErrTagAlreadyExists)
  229. ctx.Flash.Error(ctx.Tr("repo.branch.tag_collision", e.TagName))
  230. ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL())
  231. return
  232. }
  233. if models.IsErrBranchAlreadyExists(err) {
  234. e := err.(models.ErrBranchAlreadyExists)
  235. ctx.Flash.Error(ctx.Tr("repo.branch.branch_already_exists", e.BranchName))
  236. ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL())
  237. return
  238. }
  239. if models.IsErrBranchNameConflict(err) {
  240. e := err.(models.ErrBranchNameConflict)
  241. ctx.Flash.Error(ctx.Tr("repo.branch.branch_name_conflict", form.NewBranchName, e.BranchName))
  242. ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL())
  243. return
  244. }
  245. ctx.ServerError("CreateNewBranch", err)
  246. return
  247. }
  248. ctx.Flash.Success(ctx.Tr("repo.branch.create_success", form.NewBranchName))
  249. ctx.Redirect(ctx.Repo.RepoLink + "/src/branch/" + util.PathEscapeSegments(form.NewBranchName))
  250. }