本站源代码
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

117 行
2.8KB

  1. // Copyright 2015 The Gogs 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 git
  5. import (
  6. "fmt"
  7. "os"
  8. "strconv"
  9. "strings"
  10. "sync"
  11. )
  12. // ObjectCache provides thread-safe cache opeations.
  13. type ObjectCache struct {
  14. lock sync.RWMutex
  15. cache map[string]interface{}
  16. }
  17. func newObjectCache() *ObjectCache {
  18. return &ObjectCache{
  19. cache: make(map[string]interface{}, 10),
  20. }
  21. }
  22. // Set add obj to cache
  23. func (oc *ObjectCache) Set(id string, obj interface{}) {
  24. oc.lock.Lock()
  25. defer oc.lock.Unlock()
  26. oc.cache[id] = obj
  27. }
  28. // Get get cached obj by id
  29. func (oc *ObjectCache) Get(id string) (interface{}, bool) {
  30. oc.lock.RLock()
  31. defer oc.lock.RUnlock()
  32. obj, has := oc.cache[id]
  33. return obj, has
  34. }
  35. // isDir returns true if given path is a directory,
  36. // or returns false when it's a file or does not exist.
  37. func isDir(dir string) bool {
  38. f, e := os.Stat(dir)
  39. if e != nil {
  40. return false
  41. }
  42. return f.IsDir()
  43. }
  44. // isFile returns true if given path is a file,
  45. // or returns false when it's a directory or does not exist.
  46. func isFile(filePath string) bool {
  47. f, e := os.Stat(filePath)
  48. if e != nil {
  49. return false
  50. }
  51. return !f.IsDir()
  52. }
  53. // isExist checks whether a file or directory exists.
  54. // It returns false when the file or directory does not exist.
  55. func isExist(path string) bool {
  56. _, err := os.Stat(path)
  57. return err == nil || os.IsExist(err)
  58. }
  59. func concatenateError(err error, stderr string) error {
  60. if len(stderr) == 0 {
  61. return err
  62. }
  63. return fmt.Errorf("%v - %s", err, stderr)
  64. }
  65. // RefEndName return the end name of a ref name
  66. func RefEndName(refStr string) string {
  67. if strings.HasPrefix(refStr, BranchPrefix) {
  68. return refStr[len(BranchPrefix):]
  69. }
  70. if strings.HasPrefix(refStr, TagPrefix) {
  71. return refStr[len(TagPrefix):]
  72. }
  73. return refStr
  74. }
  75. // ParseBool returns the boolean value represented by the string as per git's git_config_bool
  76. // true will be returned for the result if the string is empty, but valid will be false.
  77. // "true", "yes", "on" are all true, true
  78. // "false", "no", "off" are all false, true
  79. // 0 is false, true
  80. // Any other integer is true, true
  81. // Anything else will return false, false
  82. func ParseBool(value string) (result bool, valid bool) {
  83. // Empty strings are true but invalid
  84. if len(value) == 0 {
  85. return true, false
  86. }
  87. // These are the git expected true and false values
  88. if strings.EqualFold(value, "true") || strings.EqualFold(value, "yes") || strings.EqualFold(value, "on") {
  89. return true, true
  90. }
  91. if strings.EqualFold(value, "false") || strings.EqualFold(value, "no") || strings.EqualFold(value, "off") {
  92. return false, true
  93. }
  94. // Try a number
  95. intValue, err := strconv.ParseInt(value, 10, 32)
  96. if err != nil {
  97. return false, false
  98. }
  99. return intValue != 0, true
  100. }
上海开阖软件有限公司 沪ICP备12045867号-1