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 11KB

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