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 5package git 6 7import ( 8 "fmt" 9 "io" 10 "os" 11 "strconv" 12 "strings" 13 "sync" 14 15 "code.gitea.io/gitea/modules/util" 16) 17 18// ObjectCache provides thread-safe cache operations. 19type ObjectCache struct { 20 lock sync.RWMutex 21 cache map[string]interface{} 22} 23 24func newObjectCache() *ObjectCache { 25 return &ObjectCache{ 26 cache: make(map[string]interface{}, 10), 27 } 28} 29 30// Set add obj to cache 31func (oc *ObjectCache) Set(id string, obj interface{}) { 32 oc.lock.Lock() 33 defer oc.lock.Unlock() 34 35 oc.cache[id] = obj 36} 37 38// Get get cached obj by id 39func (oc *ObjectCache) Get(id string) (interface{}, bool) { 40 oc.lock.RLock() 41 defer oc.lock.RUnlock() 42 43 obj, has := oc.cache[id] 44 return obj, has 45} 46 47// isDir returns true if given path is a directory, 48// or returns false when it's a file or does not exist. 49func isDir(dir string) bool { 50 f, e := os.Stat(dir) 51 if e != nil { 52 return false 53 } 54 return f.IsDir() 55} 56 57// isFile returns true if given path is a file, 58// or returns false when it's a directory or does not exist. 59func isFile(filePath string) bool { 60 f, e := os.Stat(filePath) 61 if e != nil { 62 return false 63 } 64 return !f.IsDir() 65} 66 67// isExist checks whether a file or directory exists. 68// It returns false when the file or directory does not exist. 69func isExist(path string) bool { 70 _, err := os.Stat(path) 71 return err == nil || os.IsExist(err) 72} 73 74// ConcatenateError concatenats an error with stderr string 75func ConcatenateError(err error, stderr string) error { 76 if len(stderr) == 0 { 77 return err 78 } 79 return fmt.Errorf("%w - %s", err, stderr) 80} 81 82// RefEndName return the end name of a ref name 83func RefEndName(refStr string) string { 84 if strings.HasPrefix(refStr, BranchPrefix) { 85 return refStr[len(BranchPrefix):] 86 } 87 88 if strings.HasPrefix(refStr, TagPrefix) { 89 return refStr[len(TagPrefix):] 90 } 91 92 return refStr 93} 94 95// RefURL returns the absolute URL for a ref in a repository 96func RefURL(repoURL, ref string) string { 97 refName := util.PathEscapeSegments(RefEndName(ref)) 98 switch { 99 case strings.HasPrefix(ref, BranchPrefix): 100 return repoURL + "/src/branch/" + refName 101 case strings.HasPrefix(ref, TagPrefix): 102 return repoURL + "/src/tag/" + refName 103 default: 104 return repoURL + "/src/commit/" + refName 105 } 106} 107 108// SplitRefName splits a full refname to reftype and simple refname 109func SplitRefName(refStr string) (string, string) { 110 if strings.HasPrefix(refStr, BranchPrefix) { 111 return BranchPrefix, refStr[len(BranchPrefix):] 112 } 113 114 if strings.HasPrefix(refStr, TagPrefix) { 115 return TagPrefix, refStr[len(TagPrefix):] 116 } 117 118 return "", refStr 119} 120 121// ParseBool returns the boolean value represented by the string as per git's git_config_bool 122// true will be returned for the result if the string is empty, but valid will be false. 123// "true", "yes", "on" are all true, true 124// "false", "no", "off" are all false, true 125// 0 is false, true 126// Any other integer is true, true 127// Anything else will return false, false 128func ParseBool(value string) (result, valid bool) { 129 // Empty strings are true but invalid 130 if len(value) == 0 { 131 return true, false 132 } 133 // These are the git expected true and false values 134 if strings.EqualFold(value, "true") || strings.EqualFold(value, "yes") || strings.EqualFold(value, "on") { 135 return true, true 136 } 137 if strings.EqualFold(value, "false") || strings.EqualFold(value, "no") || strings.EqualFold(value, "off") { 138 return false, true 139 } 140 // Try a number 141 intValue, err := strconv.ParseInt(value, 10, 32) 142 if err != nil { 143 return false, false 144 } 145 return intValue != 0, true 146} 147 148// LimitedReaderCloser is a limited reader closer 149type LimitedReaderCloser struct { 150 R io.Reader 151 C io.Closer 152 N int64 153} 154 155// Read implements io.Reader 156func (l *LimitedReaderCloser) Read(p []byte) (n int, err error) { 157 if l.N <= 0 { 158 _ = l.C.Close() 159 return 0, io.EOF 160 } 161 if int64(len(p)) > l.N { 162 p = p[0:l.N] 163 } 164 n, err = l.R.Read(p) 165 l.N -= int64(n) 166 return 167} 168 169// Close implements io.Closer 170func (l *LimitedReaderCloser) Close() error { 171 return l.C.Close() 172} 173