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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. // RefURL returns the absolute URL for a ref in a repository
  76. func RefURL(repoURL, ref string) string {
  77. refName := RefEndName(ref)
  78. switch {
  79. case strings.HasPrefix(ref, BranchPrefix):
  80. return repoURL + "/src/branch/" + refName
  81. case strings.HasPrefix(ref, TagPrefix):
  82. return repoURL + "/src/tag/" + refName
  83. default:
  84. return repoURL + "/src/commit/" + refName
  85. }
  86. }
  87. // SplitRefName splits a full refname to reftype and simple refname
  88. func SplitRefName(refStr string) (string, string) {
  89. if strings.HasPrefix(refStr, BranchPrefix) {
  90. return BranchPrefix, refStr[len(BranchPrefix):]
  91. }
  92. if strings.HasPrefix(refStr, TagPrefix) {
  93. return TagPrefix, refStr[len(TagPrefix):]
  94. }
  95. return "", refStr
  96. }
  97. // ParseBool returns the boolean value represented by the string as per git's git_config_bool
  98. // true will be returned for the result if the string is empty, but valid will be false.
  99. // "true", "yes", "on" are all true, true
  100. // "false", "no", "off" are all false, true
  101. // 0 is false, true
  102. // Any other integer is true, true
  103. // Anything else will return false, false
  104. func ParseBool(value string) (result bool, valid bool) {
  105. // Empty strings are true but invalid
  106. if len(value) == 0 {
  107. return true, false
  108. }
  109. // These are the git expected true and false values
  110. if strings.EqualFold(value, "true") || strings.EqualFold(value, "yes") || strings.EqualFold(value, "on") {
  111. return true, true
  112. }
  113. if strings.EqualFold(value, "false") || strings.EqualFold(value, "no") || strings.EqualFold(value, "off") {
  114. return false, true
  115. }
  116. // Try a number
  117. intValue, err := strconv.ParseInt(value, 10, 32)
  118. if err != nil {
  119. return false, false
  120. }
  121. return intValue != 0, true
  122. }