forked from charleso/git-cc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.py
96 lines (89 loc) · 2.74 KB
/
cache.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from os.path import join, exists
from common import *
FILE = '.gitcc'
def getCache():
if cfg.getCore('cache', True) == 'False':
return NoCache()
return Cache(GIT_DIR)
class Cache(object):
def __init__(self, dir):
self.map = {}
self.file = FILE
self.dir = dir
self.empty = Version('/main/0')
def start(self):
f = join(self.dir, self.file)
if exists(f):
self.load(f)
else:
self.initial()
def load(self, file):
f = open(file, 'r')
try:
self.read(f.read())
finally:
f.close()
def initial(self):
ls = ['ls', '-recurse', '-short']
ls.extend(cfg.getInclude())
self.read(cc_exec(ls))
def read(self, lines):
for line in lines.splitlines():
if line.find('@@') < 0:
continue
self.update(CCFile2(line))
def update(self, path):
isChild = self.map.get(path.file, self.empty).isChild(path.version)
if isChild:
self.map[path.file] = path.version
return isChild or path.version.endswith(cfg.getBranches()[0])
def remove(self, file):
if file in self.map:
del self.map[file]
def write(self):
lines = []
keys = self.map.keys()
keys = sorted(keys)
for file in keys:
lines.append(file + '@@' + self.map[file].full)
f = open(join(self.dir, self.file), 'w')
try:
f.write('\n'.join(lines))
f.write('\n')
finally:
f.close()
git_exec(['add', self.file])
def list(self):
values = []
for file, version in self.map.items():
values.append(CCFile(file, version.full))
return values
def contains(self, path):
return self.map.get(path.file, self.empty).full == path.version.full
class NoCache(object):
def start(self):
pass
def write(self):
pass
def update(self, path):
return True
def remove(self, file):
pass
class CCFile(object):
def __init__(self, file, version):
if file.startswith('./') or file.startswith('.\\'):
file = file[2:]
self.file = file
self.version = Version(version)
class CCFile2(CCFile):
def __init__(self, line):
[file, version] = line.rsplit('@@', 1)
super(CCFile2, self).__init__(file, version)
class Version(object):
def __init__(self, version):
self.full = version.replace('\\', '/')
self.version = '/'.join(self.full.split('/')[0:-1])
def isChild(self, version):
return version.version.startswith(self.version)
def endswith(self, version):
return self.version.endswith('/' + version)