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

repository.go 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package setting
  5. import (
  6. "path"
  7. "path/filepath"
  8. "strings"
  9. "code.gitea.io/gitea/modules/log"
  10. )
  11. // enumerates all the policy repository creating
  12. const (
  13. RepoCreatingLastUserVisibility = "last"
  14. RepoCreatingPrivate = "private"
  15. RepoCreatingPublic = "public"
  16. )
  17. // Repository settings
  18. var (
  19. Repository = struct {
  20. DetectedCharsetsOrder []string
  21. DetectedCharsetScore map[string]int `ini:"-"`
  22. AnsiCharset string
  23. ForcePrivate bool
  24. DefaultPrivate string
  25. DefaultPushCreatePrivate bool
  26. MaxCreationLimit int
  27. MirrorQueueLength int
  28. PullRequestQueueLength int
  29. PreferredLicenses []string
  30. DisableHTTPGit bool
  31. AccessControlAllowOrigin string
  32. UseCompatSSHURI bool
  33. DefaultCloseIssuesViaCommitsInAnyBranch bool
  34. EnablePushCreateUser bool
  35. EnablePushCreateOrg bool
  36. DisabledRepoUnits []string
  37. DefaultRepoUnits []string
  38. PrefixArchiveFiles bool
  39. DisableMirrors bool
  40. DisableMigrations bool
  41. DefaultBranch string
  42. AllowAdoptionOfUnadoptedRepositories bool
  43. AllowDeleteOfUnadoptedRepositories bool
  44. // Repository editor settings
  45. Editor struct {
  46. LineWrapExtensions []string
  47. PreviewableFileModes []string
  48. } `ini:"-"`
  49. // Repository upload settings
  50. Upload struct {
  51. Enabled bool
  52. TempPath string
  53. AllowedTypes string
  54. FileMaxSize int64
  55. MaxFiles int
  56. } `ini:"-"`
  57. // Repository local settings
  58. Local struct {
  59. LocalCopyPath string
  60. } `ini:"-"`
  61. // Pull request settings
  62. PullRequest struct {
  63. WorkInProgressPrefixes []string
  64. CloseKeywords []string
  65. ReopenKeywords []string
  66. DefaultMergeMessageCommitsLimit int
  67. DefaultMergeMessageSize int
  68. DefaultMergeMessageAllAuthors bool
  69. DefaultMergeMessageMaxApprovers int
  70. DefaultMergeMessageOfficialApproversOnly bool
  71. } `ini:"repository.pull-request"`
  72. // Issue Setting
  73. Issue struct {
  74. LockReasons []string
  75. } `ini:"repository.issue"`
  76. Release struct {
  77. AllowedTypes string
  78. } `ini:"repository.release"`
  79. Signing struct {
  80. SigningKey string
  81. SigningName string
  82. SigningEmail string
  83. InitialCommit []string
  84. CRUDActions []string `ini:"CRUD_ACTIONS"`
  85. Merges []string
  86. Wiki []string
  87. DefaultTrustModel string
  88. } `ini:"repository.signing"`
  89. }{
  90. DetectedCharsetsOrder: []string{
  91. "UTF-8",
  92. "UTF-16BE",
  93. "UTF-16LE",
  94. "UTF-32BE",
  95. "UTF-32LE",
  96. "ISO-8859-1",
  97. "windows-1252",
  98. "ISO-8859-2",
  99. "windows-1250",
  100. "ISO-8859-5",
  101. "ISO-8859-6",
  102. "ISO-8859-7",
  103. "windows-1253",
  104. "ISO-8859-8-I",
  105. "windows-1255",
  106. "ISO-8859-8",
  107. "windows-1251",
  108. "windows-1256",
  109. "KOI8-R",
  110. "ISO-8859-9",
  111. "windows-1254",
  112. "Shift_JIS",
  113. "GB18030",
  114. "EUC-JP",
  115. "EUC-KR",
  116. "Big5",
  117. "ISO-2022-JP",
  118. "ISO-2022-KR",
  119. "ISO-2022-CN",
  120. "IBM424_rtl",
  121. "IBM424_ltr",
  122. "IBM420_rtl",
  123. "IBM420_ltr",
  124. },
  125. DetectedCharsetScore: map[string]int{},
  126. AnsiCharset: "",
  127. ForcePrivate: false,
  128. DefaultPrivate: RepoCreatingLastUserVisibility,
  129. DefaultPushCreatePrivate: true,
  130. MaxCreationLimit: -1,
  131. MirrorQueueLength: 1000,
  132. PullRequestQueueLength: 1000,
  133. PreferredLicenses: []string{"Apache License 2.0", "MIT License"},
  134. DisableHTTPGit: false,
  135. AccessControlAllowOrigin: "",
  136. UseCompatSSHURI: false,
  137. DefaultCloseIssuesViaCommitsInAnyBranch: false,
  138. EnablePushCreateUser: false,
  139. EnablePushCreateOrg: false,
  140. DisabledRepoUnits: []string{},
  141. DefaultRepoUnits: []string{},
  142. PrefixArchiveFiles: true,
  143. DisableMirrors: false,
  144. DisableMigrations: false,
  145. DefaultBranch: "master",
  146. // Repository editor settings
  147. Editor: struct {
  148. LineWrapExtensions []string
  149. PreviewableFileModes []string
  150. }{
  151. LineWrapExtensions: strings.Split(".txt,.md,.markdown,.mdown,.mkd,", ","),
  152. PreviewableFileModes: []string{"markdown"},
  153. },
  154. // Repository upload settings
  155. Upload: struct {
  156. Enabled bool
  157. TempPath string
  158. AllowedTypes string
  159. FileMaxSize int64
  160. MaxFiles int
  161. }{
  162. Enabled: true,
  163. TempPath: "data/tmp/uploads",
  164. AllowedTypes: "",
  165. FileMaxSize: 3,
  166. MaxFiles: 5,
  167. },
  168. // Repository local settings
  169. Local: struct {
  170. LocalCopyPath string
  171. }{
  172. LocalCopyPath: "tmp/local-repo",
  173. },
  174. // Pull request settings
  175. PullRequest: struct {
  176. WorkInProgressPrefixes []string
  177. CloseKeywords []string
  178. ReopenKeywords []string
  179. DefaultMergeMessageCommitsLimit int
  180. DefaultMergeMessageSize int
  181. DefaultMergeMessageAllAuthors bool
  182. DefaultMergeMessageMaxApprovers int
  183. DefaultMergeMessageOfficialApproversOnly bool
  184. }{
  185. WorkInProgressPrefixes: []string{"WIP:", "[WIP]"},
  186. // Same as GitHub. See
  187. // https://help.github.com/articles/closing-issues-via-commit-messages
  188. CloseKeywords: strings.Split("close,closes,closed,fix,fixes,fixed,resolve,resolves,resolved", ","),
  189. ReopenKeywords: strings.Split("reopen,reopens,reopened", ","),
  190. DefaultMergeMessageCommitsLimit: 50,
  191. DefaultMergeMessageSize: 5 * 1024,
  192. DefaultMergeMessageAllAuthors: false,
  193. DefaultMergeMessageMaxApprovers: 10,
  194. DefaultMergeMessageOfficialApproversOnly: true,
  195. },
  196. // Issue settings
  197. Issue: struct {
  198. LockReasons []string
  199. }{
  200. LockReasons: strings.Split("Too heated,Off-topic,Spam,Resolved", ","),
  201. },
  202. Release: struct {
  203. AllowedTypes string
  204. }{
  205. AllowedTypes: "",
  206. },
  207. // Signing settings
  208. Signing: struct {
  209. SigningKey string
  210. SigningName string
  211. SigningEmail string
  212. InitialCommit []string
  213. CRUDActions []string `ini:"CRUD_ACTIONS"`
  214. Merges []string
  215. Wiki []string
  216. DefaultTrustModel string
  217. }{
  218. SigningKey: "default",
  219. SigningName: "",
  220. SigningEmail: "",
  221. InitialCommit: []string{"always"},
  222. CRUDActions: []string{"pubkey", "twofa", "parentsigned"},
  223. Merges: []string{"pubkey", "twofa", "basesigned", "commitssigned"},
  224. Wiki: []string{"never"},
  225. DefaultTrustModel: "collaborator",
  226. },
  227. }
  228. RepoRootPath string
  229. ScriptType = "bash"
  230. )
  231. func newRepository() {
  232. var err error
  233. // Determine and create root git repository path.
  234. sec := Cfg.Section("repository")
  235. Repository.DisableHTTPGit = sec.Key("DISABLE_HTTP_GIT").MustBool()
  236. Repository.UseCompatSSHURI = sec.Key("USE_COMPAT_SSH_URI").MustBool()
  237. Repository.MaxCreationLimit = sec.Key("MAX_CREATION_LIMIT").MustInt(-1)
  238. Repository.DefaultBranch = sec.Key("DEFAULT_BRANCH").MustString(Repository.DefaultBranch)
  239. RepoRootPath = sec.Key("ROOT").MustString(path.Join(AppDataPath, "gitea-repositories"))
  240. forcePathSeparator(RepoRootPath)
  241. if !filepath.IsAbs(RepoRootPath) {
  242. RepoRootPath = filepath.Join(AppWorkPath, RepoRootPath)
  243. } else {
  244. RepoRootPath = filepath.Clean(RepoRootPath)
  245. }
  246. defaultDetectedCharsetsOrder := make([]string, 0, len(Repository.DetectedCharsetsOrder))
  247. for _, charset := range Repository.DetectedCharsetsOrder {
  248. defaultDetectedCharsetsOrder = append(defaultDetectedCharsetsOrder, strings.ToLower(strings.TrimSpace(charset)))
  249. }
  250. ScriptType = sec.Key("SCRIPT_TYPE").MustString("bash")
  251. if err = Cfg.Section("repository").MapTo(&Repository); err != nil {
  252. log.Fatal("Failed to map Repository settings: %v", err)
  253. } else if err = Cfg.Section("repository.editor").MapTo(&Repository.Editor); err != nil {
  254. log.Fatal("Failed to map Repository.Editor settings: %v", err)
  255. } else if err = Cfg.Section("repository.upload").MapTo(&Repository.Upload); err != nil {
  256. log.Fatal("Failed to map Repository.Upload settings: %v", err)
  257. } else if err = Cfg.Section("repository.local").MapTo(&Repository.Local); err != nil {
  258. log.Fatal("Failed to map Repository.Local settings: %v", err)
  259. } else if err = Cfg.Section("repository.pull-request").MapTo(&Repository.PullRequest); err != nil {
  260. log.Fatal("Failed to map Repository.PullRequest settings: %v", err)
  261. }
  262. // Handle default trustmodel settings
  263. Repository.Signing.DefaultTrustModel = strings.ToLower(strings.TrimSpace(Repository.Signing.DefaultTrustModel))
  264. if Repository.Signing.DefaultTrustModel == "default" {
  265. Repository.Signing.DefaultTrustModel = "collaborator"
  266. }
  267. // Handle preferred charset orders
  268. preferred := make([]string, 0, len(Repository.DetectedCharsetsOrder))
  269. for _, charset := range Repository.DetectedCharsetsOrder {
  270. canonicalCharset := strings.ToLower(strings.TrimSpace(charset))
  271. preferred = append(preferred, canonicalCharset)
  272. // remove it from the defaults
  273. for i, charset := range defaultDetectedCharsetsOrder {
  274. if charset == canonicalCharset {
  275. defaultDetectedCharsetsOrder = append(defaultDetectedCharsetsOrder[:i], defaultDetectedCharsetsOrder[i+1:]...)
  276. break
  277. }
  278. }
  279. }
  280. i := 0
  281. for _, charset := range preferred {
  282. // Add the defaults
  283. if charset == "defaults" {
  284. for _, charset := range defaultDetectedCharsetsOrder {
  285. canonicalCharset := strings.ToLower(strings.TrimSpace(charset))
  286. if _, has := Repository.DetectedCharsetScore[canonicalCharset]; !has {
  287. Repository.DetectedCharsetScore[canonicalCharset] = i
  288. i++
  289. }
  290. }
  291. continue
  292. }
  293. if _, has := Repository.DetectedCharsetScore[charset]; !has {
  294. Repository.DetectedCharsetScore[charset] = i
  295. i++
  296. }
  297. }
  298. if !filepath.IsAbs(Repository.Upload.TempPath) {
  299. Repository.Upload.TempPath = path.Join(AppWorkPath, Repository.Upload.TempPath)
  300. }
  301. }