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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
# vim:fileencoding=utf-8:noet
from __future__ import (unicode_literals, division, absolute_import, print_function)
import os
import errno
from threading import Lock
from collections import defaultdict
from powerline.lib.watcher import create_tree_watcher
from powerline.lib.unicode import out_u
from powerline.lib.path import join
def generate_directories(path):
if os.path.isdir(path):
yield path
while True:
if os.path.ismount(path):
break
old_path = path
path = os.path.dirname(path)
if path == old_path or not path:
break
yield path
_file_watcher = None
def file_watcher(create_watcher):
global _file_watcher
if _file_watcher is None:
_file_watcher = create_watcher()
return _file_watcher
_branch_watcher = None
def branch_watcher(create_watcher):
global _branch_watcher
if _branch_watcher is None:
_branch_watcher = create_watcher()
return _branch_watcher
branch_name_cache = {}
branch_lock = Lock()
file_status_lock = Lock()
def get_branch_name(directory, config_file, get_func, create_watcher):
global branch_name_cache
with branch_lock:
# Check if the repo directory was moved/deleted
fw = branch_watcher(create_watcher)
is_watched = fw.is_watching(directory)
try:
changed = fw(directory)
except OSError as e:
if getattr(e, 'errno', None) != errno.ENOENT:
raise
changed = True
if changed:
branch_name_cache.pop(config_file, None)
# Remove the watches for this repo
if is_watched:
fw.unwatch(directory)
fw.unwatch(config_file)
else:
# Check if the config file has changed
try:
changed = fw(config_file)
except OSError as e:
if getattr(e, 'errno', None) != errno.ENOENT:
raise
# Config file does not exist (happens for mercurial)
if config_file not in branch_name_cache:
branch_name_cache[config_file] = out_u(get_func(directory, config_file))
if changed:
# Config file has changed or was not tracked
branch_name_cache[config_file] = out_u(get_func(directory, config_file))
return branch_name_cache[config_file]
class FileStatusCache(dict):
def __init__(self):
self.dirstate_map = defaultdict(set)
self.ignore_map = defaultdict(set)
self.keypath_ignore_map = {}
def update_maps(self, keypath, directory, dirstate_file, ignore_file_name, extra_ignore_files):
parent = keypath
ignore_files = set()
while parent != directory:
nparent = os.path.dirname(keypath)
if nparent == parent:
break
parent = nparent
ignore_files.add(join(parent, ignore_file_name))
for f in extra_ignore_files:
ignore_files.add(f)
self.keypath_ignore_map[keypath] = ignore_files
for ignf in ignore_files:
self.ignore_map[ignf].add(keypath)
self.dirstate_map[dirstate_file].add(keypath)
def invalidate(self, dirstate_file=None, ignore_file=None):
for keypath in self.dirstate_map[dirstate_file]:
self.pop(keypath, None)
for keypath in self.ignore_map[ignore_file]:
self.pop(keypath, None)
def ignore_files(self, keypath):
for ignf in self.keypath_ignore_map[keypath]:
yield ignf
file_status_cache = FileStatusCache()
def get_file_status(directory, dirstate_file, file_path, ignore_file_name, get_func, create_watcher, extra_ignore_files=()):
global file_status_cache
keypath = file_path if os.path.isabs(file_path) else join(directory, file_path)
file_status_cache.update_maps(keypath, directory, dirstate_file, ignore_file_name, extra_ignore_files)
with file_status_lock:
# Optimize case of keypath not being cached
if keypath not in file_status_cache:
file_status_cache[keypath] = ans = get_func(directory, file_path)
return ans
# Check if any relevant files have changed
file_changed = file_watcher(create_watcher)
changed = False
# Check if dirstate has changed
try:
changed = file_changed(dirstate_file)
except OSError as e:
if getattr(e, 'errno', None) != errno.ENOENT:
raise
# The .git index file does not exist for a new git repo
return get_func(directory, file_path)
if changed:
# Remove all cached values for files that depend on this
# dirstate_file
file_status_cache.invalidate(dirstate_file=dirstate_file)
else:
# Check if the file itself has changed
try:
changed ^= file_changed(keypath)
except OSError as e:
if getattr(e, 'errno', None) != errno.ENOENT:
raise
# Do not call get_func again for a non-existent file
if keypath not in file_status_cache:
file_status_cache[keypath] = get_func(directory, file_path)
return file_status_cache[keypath]
if changed:
file_status_cache.pop(keypath, None)
else:
# Check if one of the ignore files has changed
for ignf in file_status_cache.ignore_files(keypath):
try:
changed ^= file_changed(ignf)
except OSError as e:
if getattr(e, 'errno', None) != errno.ENOENT:
raise
if changed:
# Invalidate cache for all files that might be affected
# by this ignore file
file_status_cache.invalidate(ignore_file=ignf)
break
try:
return file_status_cache[keypath]
except KeyError:
file_status_cache[keypath] = ans = get_func(directory, file_path)
return ans
class TreeStatusCache(dict):
def __init__(self, pl):
self.tw = create_tree_watcher(pl)
self.pl = pl
def cache_and_get(self, key, status):
ans = self.get(key, self)
if ans is self:
ans = self[key] = status()
return ans
def __call__(self, repo):
key = repo.directory
try:
if self.tw(key, ignore_event=getattr(repo, 'ignore_event', None)):
self.pop(key, None)
except OSError as e:
self.pl.warn('Failed to check {0} for changes, with error: {1}', key, str(e))
return self.cache_and_get(key, repo.status)
_tree_status_cache = None
def tree_status(repo, pl):
global _tree_status_cache
if _tree_status_cache is None:
_tree_status_cache = TreeStatusCache(pl)
return _tree_status_cache(repo)
vcs_props = (
('git', '.git', os.path.exists),
('mercurial', '.hg', os.path.isdir),
('bzr', '.bzr', os.path.isdir),
)
vcs_props_bytes = [
(vcs, vcs_dir.encode('ascii'), check)
for vcs, vcs_dir, check in vcs_props
]
def guess(path, create_watcher):
for directory in generate_directories(path):
for vcs, vcs_dir, check in (vcs_props_bytes if isinstance(path, bytes) else vcs_props):
repo_dir = os.path.join(directory, vcs_dir)
if check(repo_dir):
if os.path.isdir(repo_dir) and not os.access(repo_dir, os.X_OK):
continue
try:
if vcs not in globals():
globals()[vcs] = getattr(__import__(str('powerline.lib.vcs'), fromlist=[str(vcs)]), str(vcs))
return globals()[vcs].Repository(directory, create_watcher)
except:
pass
return None
def get_fallback_create_watcher():
from powerline.lib.watcher import create_file_watcher
from powerline import get_fallback_logger
from functools import partial
return partial(create_file_watcher, get_fallback_logger(), 'auto')
def debug():
'''Test run guess(), repo.branch() and repo.status()
To use::
python -c 'from powerline.lib.vcs import debug; debug()' some_file_to_watch.
'''
import sys
dest = sys.argv[-1]
repo = guess(os.path.abspath(dest), get_fallback_create_watcher)
if repo is None:
print ('%s is not a recognized vcs repo' % dest)
raise SystemExit(1)
print ('Watching %s' % dest)
print ('Press Ctrl-C to exit.')
try:
while True:
if os.path.isdir(dest):
print ('Branch name: %s Status: %s' % (repo.branch(), repo.status()))
else:
print ('File status: %s' % repo.status(dest))
raw_input('Press Enter to check again: ')
except KeyboardInterrupt:
pass
except EOFError:
pass
|