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.

utils.go 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package git
  4. import (
  5. "fmt"
  6. "io"
  7. "os"
  8. "strconv"
  9. "strings"
  10. "sync"
  11. )
  12. // ObjectCache provides thread-safe cache operations.
  13. type ObjectCache struct {
  14. lock sync.RWMutex
  15. cache map[string]any
  16. }
  17. func newObjectCache() *ObjectCache {
  18. return &ObjectCache{
  19. cache: make(map[string]any, 10),
  20. }
  21. }
  22. // Set add obj to cache
  23. func (oc *ObjectCache) Set(id string, obj any) {
  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) (any, 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. // ConcatenateError concatenats an error with stderr string
  60. func ConcatenateError(err error, stderr string) error {
  61. if len(stderr) == 0 {
  62. return err
  63. }
  64. return fmt.Errorf("%w - %s", err, stderr)
  65. }
  66. // ParseBool returns the boolean value represented by the string as per git's git_config_bool
  67. // true will be returned for the result if the string is empty, but valid will be false.
  68. // "true", "yes", "on" are all true, true
  69. // "false", "no", "off" are all false, true
  70. // 0 is false, true
  71. // Any other integer is true, true
  72. // Anything else will return false, false
  73. func ParseBool(value string) (result, valid bool) {
  74. // Empty strings are true but invalid
  75. if len(value) == 0 {
  76. return true, false
  77. }
  78. // These are the git expected true and false values
  79. if strings.EqualFold(value, "true") || strings.EqualFold(value, "yes") || strings.EqualFold(value, "on") {
  80. return true, true
  81. }
  82. if strings.EqualFold(value, "false") || strings.EqualFold(value, "no") || strings.EqualFold(value, "off") {
  83. return false, true
  84. }
  85. // Try a number
  86. intValue, err := strconv.ParseInt(value, 10, 32)
  87. if err != nil {
  88. return false, false
  89. }
  90. return intValue != 0, true
  91. }
  92. // LimitedReaderCloser is a limited reader closer
  93. type LimitedReaderCloser struct {
  94. R io.Reader
  95. C io.Closer
  96. N int64
  97. }
  98. // Read implements io.Reader
  99. func (l *LimitedReaderCloser) Read(p []byte) (n int, err error) {
  100. if l.N <= 0 {
  101. _ = l.C.Close()
  102. return 0, io.EOF
  103. }
  104. if int64(len(p)) > l.N {
  105. p = p[0:l.N]
  106. }
  107. n, err = l.R.Read(p)
  108. l.N -= int64(n)
  109. return n, err
  110. }
  111. // Close implements io.Closer
  112. func (l *LimitedReaderCloser) Close() error {
  113. return l.C.Close()
  114. }