gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repo
  4. import (
  5. "strconv"
  6. user_model "code.gitea.io/gitea/models/user"
  7. "code.gitea.io/gitea/modules/optional"
  8. "code.gitea.io/gitea/services/context"
  9. user_service "code.gitea.io/gitea/services/user"
  10. )
  11. // SetEditorconfigIfExists set editor config as render variable
  12. func SetEditorconfigIfExists(ctx *context.Context) {
  13. if ctx.Repo.Repository.IsEmpty {
  14. return
  15. }
  16. ec, _, err := ctx.Repo.GetEditorconfig()
  17. if err != nil {
  18. // it used to check `!git.IsErrNotExist(err)` and create a system notice, but it is quite annoying and useless
  19. // because network errors also happen frequently, so we just ignore it
  20. return
  21. }
  22. ctx.Data["Editorconfig"] = ec
  23. }
  24. // SetDiffViewStyle set diff style as render variable
  25. func SetDiffViewStyle(ctx *context.Context) {
  26. queryStyle := ctx.FormString("style")
  27. if !ctx.IsSigned {
  28. ctx.Data["IsSplitStyle"] = queryStyle == "split"
  29. return
  30. }
  31. var (
  32. userStyle = ctx.Doer.DiffViewStyle
  33. style string
  34. )
  35. if queryStyle == "unified" || queryStyle == "split" {
  36. style = queryStyle
  37. } else if userStyle == "unified" || userStyle == "split" {
  38. style = userStyle
  39. } else {
  40. style = "unified"
  41. }
  42. ctx.Data["IsSplitStyle"] = style == "split"
  43. opts := &user_service.UpdateOptions{
  44. DiffViewStyle: optional.Some(style),
  45. }
  46. if err := user_service.UpdateUser(ctx, ctx.Doer, opts); err != nil {
  47. ctx.ServerError("UpdateUser", err)
  48. }
  49. }
  50. // SetWhitespaceBehavior set whitespace behavior as render variable
  51. func SetWhitespaceBehavior(ctx *context.Context) {
  52. const defaultWhitespaceBehavior = "show-all"
  53. whitespaceBehavior := ctx.FormString("whitespace")
  54. switch whitespaceBehavior {
  55. case "", "ignore-all", "ignore-eol", "ignore-change":
  56. break
  57. default:
  58. whitespaceBehavior = defaultWhitespaceBehavior
  59. }
  60. if ctx.IsSigned {
  61. userWhitespaceBehavior, err := user_model.GetUserSetting(ctx, ctx.Doer.ID, user_model.SettingsKeyDiffWhitespaceBehavior, defaultWhitespaceBehavior)
  62. if err == nil {
  63. if whitespaceBehavior == "" {
  64. whitespaceBehavior = userWhitespaceBehavior
  65. } else if whitespaceBehavior != userWhitespaceBehavior {
  66. _ = user_model.SetUserSetting(ctx, ctx.Doer.ID, user_model.SettingsKeyDiffWhitespaceBehavior, whitespaceBehavior)
  67. }
  68. } // else: we can ignore the error safely
  69. }
  70. // these behaviors are for gitdiff.GetWhitespaceFlag
  71. if whitespaceBehavior == "" {
  72. ctx.Data["WhitespaceBehavior"] = defaultWhitespaceBehavior
  73. } else {
  74. ctx.Data["WhitespaceBehavior"] = whitespaceBehavior
  75. }
  76. }
  77. // SetShowOutdatedComments set the show outdated comments option as context variable
  78. func SetShowOutdatedComments(ctx *context.Context) {
  79. showOutdatedCommentsValue := ctx.FormString("show-outdated")
  80. if showOutdatedCommentsValue != "true" && showOutdatedCommentsValue != "false" {
  81. // invalid or no value for this form string -> use default or stored user setting
  82. showOutdatedCommentsValue = "true"
  83. if ctx.IsSigned {
  84. showOutdatedCommentsValue, _ = user_model.GetUserSetting(ctx, ctx.Doer.ID, user_model.SettingsKeyShowOutdatedComments, showOutdatedCommentsValue)
  85. }
  86. } else if ctx.IsSigned {
  87. // valid value -> update user setting if user is logged in
  88. _ = user_model.SetUserSetting(ctx, ctx.Doer.ID, user_model.SettingsKeyShowOutdatedComments, showOutdatedCommentsValue)
  89. }
  90. ctx.Data["ShowOutdatedComments"], _ = strconv.ParseBool(showOutdatedCommentsValue)
  91. }