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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. "strings"
  9. "sync"
  10. )
  11. // ObjectCache provides thread-safe cache opeations.
  12. type ObjectCache struct {
  13. lock sync.RWMutex
  14. cache map[string]interface{}
  15. }
  16. func newObjectCache() *ObjectCache {
  17. return &ObjectCache{
  18. cache: make(map[string]interface{}, 10),
  19. }
  20. }
  21. // Set add obj to cache
  22. func (oc *ObjectCache) Set(id string, obj interface{}) {
  23. oc.lock.Lock()
  24. defer oc.lock.Unlock()
  25. oc.cache[id] = obj
  26. }
  27. // Get get cached obj by id
  28. func (oc *ObjectCache) Get(id string) (interface{}, bool) {
  29. oc.lock.RLock()
  30. defer oc.lock.RUnlock()
  31. obj, has := oc.cache[id]
  32. return obj, has
  33. }
  34. // isDir returns true if given path is a directory,
  35. // or returns false when it's a file or does not exist.
  36. func isDir(dir string) bool {
  37. f, e := os.Stat(dir)
  38. if e != nil {
  39. return false
  40. }
  41. return f.IsDir()
  42. }
  43. // isFile returns true if given path is a file,
  44. // or returns false when it's a directory or does not exist.
  45. func isFile(filePath string) bool {
  46. f, e := os.Stat(filePath)
  47. if e != nil {
  48. return false
  49. }
  50. return !f.IsDir()
  51. }
  52. // isExist checks whether a file or directory exists.
  53. // It returns false when the file or directory does not exist.
  54. func isExist(path string) bool {
  55. _, err := os.Stat(path)
  56. return err == nil || os.IsExist(err)
  57. }
  58. func concatenateError(err error, stderr string) error {
  59. if len(stderr) == 0 {
  60. return err
  61. }
  62. return fmt.Errorf("%v - %s", err, stderr)
  63. }
  64. // RefEndName return the end name of a ref name
  65. func RefEndName(refStr string) string {
  66. if strings.HasPrefix(refStr, BranchPrefix) {
  67. return refStr[len(BranchPrefix):]
  68. }
  69. if strings.HasPrefix(refStr, TagPrefix) {
  70. return refStr[len(TagPrefix):]
  71. }
  72. return refStr
  73. }