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.

93 lines
2.0 KiB

  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. "path/filepath"
  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. func (oc *objectCache) Set(id string, obj interface{}) {
  23. oc.lock.Lock()
  24. defer oc.lock.Unlock()
  25. oc.cache[id] = obj
  26. }
  27. func (oc *objectCache) Get(id string) (interface{}, bool) {
  28. oc.lock.RLock()
  29. defer oc.lock.RUnlock()
  30. obj, has := oc.cache[id]
  31. return obj, has
  32. }
  33. // isDir returns true if given path is a directory,
  34. // or returns false when it's a file or does not exist.
  35. func isDir(dir string) bool {
  36. f, e := os.Stat(dir)
  37. if e != nil {
  38. return false
  39. }
  40. return f.IsDir()
  41. }
  42. // isFile returns true if given path is a file,
  43. // or returns false when it's a directory or does not exist.
  44. func isFile(filePath string) bool {
  45. f, e := os.Stat(filePath)
  46. if e != nil {
  47. return false
  48. }
  49. return !f.IsDir()
  50. }
  51. // isExist checks whether a file or directory exists.
  52. // It returns false when the file or directory does not exist.
  53. func isExist(path string) bool {
  54. _, err := os.Stat(path)
  55. return err == nil || os.IsExist(err)
  56. }
  57. func concatenateError(err error, stderr string) error {
  58. if len(stderr) == 0 {
  59. return err
  60. }
  61. return fmt.Errorf("%v - %s", err, stderr)
  62. }
  63. // If the object is stored in its own file (i.e not in a pack file),
  64. // this function returns the full path to the object file.
  65. // It does not test if the file exists.
  66. func filepathFromSHA1(rootdir, sha1 string) string {
  67. return filepath.Join(rootdir, "objects", sha1[:2], sha1[2:])
  68. }
  69. func RefEndName(refStr string) string {
  70. if strings.HasPrefix(refStr, BRANCH_PREFIX) {
  71. return refStr[len(BRANCH_PREFIX):]
  72. }
  73. if strings.HasPrefix(refStr, TAG_PREFIX) {
  74. return refStr[len(TAG_PREFIX):]
  75. }
  76. return refStr
  77. }