123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- package git
- import (
- "fmt"
- "os"
- "path/filepath"
- "strings"
- "sync"
- )
- type objectCache struct {
- lock sync.RWMutex
- cache map[string]interface{}
- }
- func newObjectCache() *objectCache {
- return &objectCache{
- cache: make(map[string]interface{}, 10),
- }
- }
- func (oc *objectCache) Set(id string, obj interface{}) {
- oc.lock.Lock()
- defer oc.lock.Unlock()
- oc.cache[id] = obj
- }
- func (oc *objectCache) Get(id string) (interface{}, bool) {
- oc.lock.RLock()
- defer oc.lock.RUnlock()
- obj, has := oc.cache[id]
- return obj, has
- }
- func isDir(dir string) bool {
- f, e := os.Stat(dir)
- if e != nil {
- return false
- }
- return f.IsDir()
- }
- func isFile(filePath string) bool {
- f, e := os.Stat(filePath)
- if e != nil {
- return false
- }
- return !f.IsDir()
- }
- func isExist(path string) bool {
- _, err := os.Stat(path)
- return err == nil || os.IsExist(err)
- }
- func concatenateError(err error, stderr string) error {
- if len(stderr) == 0 {
- return err
- }
- return fmt.Errorf("%v - %s", err, stderr)
- }
- func filepathFromSHA1(rootdir, sha1 string) string {
- return filepath.Join(rootdir, "objects", sha1[:2], sha1[2:])
- }
- func RefEndName(refStr string) string {
- if strings.HasPrefix(refStr, BRANCH_PREFIX) {
- return refStr[len(BRANCH_PREFIX):]
- }
- if strings.HasPrefix(refStr, TAG_PREFIX) {
- return refStr[len(TAG_PREFIX):]
- }
- return refStr
- }
|