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