git-module

git clone git://git.lin.moe/fork/git-module.git

 1package git
 2
 3import (
 4	"os"
 5	"strings"
 6	"sync"
 7)
 8
 9// objectCache provides thread-safe cache operations. TODO(@unknwon): Use
10// sync.Map once requires Go 1.13.
11type objectCache struct {
12	lock  sync.RWMutex
13	cache map[string]interface{}
14}
15
16func newObjectCache() *objectCache {
17	return &objectCache{
18		cache: make(map[string]interface{}),
19	}
20}
21
22func (oc *objectCache) Set(id string, obj interface{}) {
23	oc.lock.Lock()
24	defer oc.lock.Unlock()
25
26	oc.cache[id] = obj
27}
28
29func (oc *objectCache) Get(id string) (interface{}, bool) {
30	oc.lock.RLock()
31	defer oc.lock.RUnlock()
32
33	obj, has := oc.cache[id]
34	return obj, has
35}
36
37// isDir returns true if given path is a directory, or returns false when it's a
38// file or does not exist.
39func isDir(dir string) bool {
40	f, e := os.Stat(dir)
41	if e != nil {
42		return false
43	}
44	return f.IsDir()
45}
46
47// isFile returns true if given path is a file, or returns false when it's a
48// directory or does not exist.
49func isFile(filePath string) bool {
50	f, e := os.Stat(filePath)
51	if e != nil {
52		return false
53	}
54	return !f.IsDir()
55}
56
57// isExist checks whether a file or directory exists. It returns false when the
58// file or directory does not exist.
59func isExist(path string) bool {
60	_, err := os.Stat(path)
61	return err == nil || os.IsExist(err)
62}
63
64// bytesToStrings splits given bytes into strings by line separator ("\n"). It
65// returns empty slice if the given bytes only contains line separators.
66func bytesToStrings(in []byte) []string {
67	s := strings.TrimRight(string(in), "\n")
68	if s == "" { // empty (not {""}, len=1)
69		return []string{}
70	}
71	return strings.Split(s, "\n")
72}