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

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