summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--ranger/ext/vcs/bzr.py178
-rw-r--r--ranger/ext/vcs/vcs.py42
2 files changed, 145 insertions, 75 deletions
diff --git a/ranger/ext/vcs/bzr.py b/ranger/ext/vcs/bzr.py
index a771dbb4..49ce8b34 100644
--- a/ranger/ext/vcs/bzr.py
+++ b/ranger/ext/vcs/bzr.py
@@ -10,106 +10,146 @@ from .vcs import Vcs, VcsError
 
 class Bzr(Vcs):
     """VCS implementation for GNU Bazaar"""
-    HEAD="last:1"
+    HEAD = 'last:1'
+
+    _status_translations = (
+        ('+ -R', 'K NM', 'staged'),
+        (' -', 'D', 'deleted'),
+        ('?', ' ', 'untracked'),
+    )
 
     # Generic
     #---------------------------
 
-    def _bzr(self, path, args, silent=True, catchout=False, retbytes=False):
-        return self._vcs(path, 'bzr', args, silent=silent, catchout=catchout, retbytes=retbytes)
-
-    def _has_head(self):
-        """Checks whether repo has head"""
-        rnum = self._bzr(self.path, ['revno'], catchout=True)
-        return rnum != '0'
-
-    def _sanitize_rev(self, rev):
-        if rev == None: return None
-        rev = rev.strip()
-        if len(rev) == 0: return None
+    def _bzr(self, args, path=None, catchout=True, retbytes=False):
+        """Run a bzr command"""
+        return self._vcs(path or self.path, 'bzr', args, catchout=catchout, retbytes=retbytes)
 
-        return rev
+    def _remote_url(self):
+        """Returns remote url"""
+        try:
+            return self._bzr(['config', 'parent_location']).rstrip() or None
+        except VcsError:
+            return None
 
     def _log(self, refspec=None, filelist=None):
-        """Gets a list of dicts containing revision info, for the revisions matching refspec"""
-        args = ['log', '-n0', '--show-ids']
-        if refspec: args = args + ["-r", refspec]
+        """Returns an array of dicts containing revision info for refspec"""
+        args = ['log', '--log-format', 'long', '--levels', '0', '--show-ids']
+        if refspec:
+            args += ['--revision', refspec]
+        if filelist:
+            args += ['--'] + filelist
 
-        if filelist: args = args + filelist
-
-        raw = self._bzr(self.path, args, catchout=True, silent=True)
-        L = re.findall('-+$(.*?)^-', raw + '\n---', re.MULTILINE | re.DOTALL)
+        try:
+            output = self._bzr(args)
+        except VcsError:
+            return None
+        entries = re.findall(r'-+\n(.+?)\n(?:-|\Z)', output, re.MULTILINE | re.DOTALL)
 
         log = []
-        for t in L:
-            t = t.strip()
-            if len(t) == 0: continue
-
-            dt = {}
-            m = re.search('^revno:\s*([0-9]+)\s*$', t, re.MULTILINE)
-            if m: dt['short'] = m.group(1).strip()
-            m = re.search('^revision-id:\s*(.+)\s*$', t, re.MULTILINE)
-            if m: dt['revid'] = m.group(1).strip()
-            m = re.search('^committer:\s*(.+)\s*$', t, re.MULTILINE)
-            if m: dt['author'] = m.group(1).strip()
-            m = re.search('^timestamp:\s*(.+)\s*$', t, re.MULTILINE)
-            if m: dt['date'] = datetime.strptime(m.group(1).strip(), '%a %Y-%m-%d %H:%M:%S %z')
-            m = re.search('^message:\s*^(.+)$', t, re.MULTILINE)
-            if m: dt['summary'] = m.group(1).strip()
-            log.append(dt)
+        for entry in entries:
+            new = {}
+            try:
+                new['short'] = re.search(r'^revno: ([0-9]+)', entry, re.MULTILINE).group(1)
+                new['revid'] = re.search(r'^revision-id: (.+)$', entry, re.MULTILINE).group(1)
+                new['author'] = re.search(r'^committer: (.+)$', entry, re.MULTILINE).group(1)
+                new['date'] = datetime.strptime(
+                    re.search(r'^timestamp: (.+)$', entry, re.MULTILINE).group(1),
+                    '%a %Y-%m-%d %H:%M:%S %z'
+                )
+                new['summary'] = re.search(r'^message:\n  (.+)$', entry, re.MULTILINE).group(1)
+            except AttributeError:
+                return None
+            log.append(new)
         return log
 
-    def _bzr_file_status(self, st):
-        st = st.strip()
-        if   st in "AM":     return 'staged'
-        elif st in "D":      return 'deleted'
-        elif st in "?":      return 'untracked'
-        else:                return 'unknown'
+    def _bzr_status_translate(self, code):
+        """Translate status code"""
+        for X, Y, status in self._status_translations:
+            if code[0] in X and code[1] in Y:
+                return status
+        return 'unknown'
 
     # Action Interface
     #---------------------------
 
     def action_add(self, filelist=None):
-        if filelist != None: self._bzr(self.path, ['add'] + filelist)
-        else:                self._bzr(self.path, ['add'])
+        args = ['add']
+        if filelist:
+            args += ['--'] + filelist
+        self._bzr(args, catchout=False)
 
     def action_reset(self, filelist=None):
-        if filelist != None: self._bzr(self.path, ['remove', '--keep', '--new'] + filelist)
-        else:                self._bzr(self.path, ['remove', '--keep', '--new'])
+        args = ['remove', '--keep', '--new']
+        if filelist:
+            args += ['--'] + filelist
+        self._bzr(args, catchout=False)
 
     # Data Interface
     #---------------------------
 
+    def data_status_root(self):
+        statuses = set()
+
+        # Paths with status
+        for line in self._bzr(['status', '--short', '--no-classify']).splitlines():
+            statuses.add(self._bzr_status_translate(line[:2]))
+
+        for status in self.DIRSTATUSES:
+            if status in statuses:
+                return status
+        return 'sync'
+
     def data_status_subpaths(self):
-        raw = self._bzr(self.path, ['status', '--short', '--no-classify'], catchout=True, retbytes=True)
-        L = re.findall('^(..)\s*(.*?)\s*$', raw.decode('utf-8'), re.MULTILINE)
-        ret = {}
-        for st, p in L:
-            sta = self._bzr_file_status(st)
-            ret[os.path.normpath(p.strip())] = sta
-        return ret
+        statuses = {}
+
+        # Ignored
+        for path in self._bzr(['ls', '--null', '--ignored']).split('\x00')[:-1]:
+            statuses[path] = 'ignored'
+
+        # Paths with status
+        for line in self._bzr(['status', '--short', '--no-classify']).splitlines():
+            statuses[os.path.normpath(line[4:])] = self._bzr_status_translate(line[:2])
+
+        return statuses
 
     def data_status_remote(self):
+        if not self._remote_url():
+            return 'none'
+
+        # XXX: Find a local solution
+        ahead = behind = False
         try:
-            remote = self._bzr(self.path, ['config', 'parent_location'], catchout=True)
+            self._bzr(['missing', '--mine-only'], catchout=False)
         except VcsError:
-            remote = ""
+            ahead = True
+        try:
+            self._bzr(['missing', '--theirs-only'], catchout=False)
+        except VcsError:
+            behind = True
 
-        return remote.strip() or None
+        if ahead:
+            return 'diverged' if behind else 'ahead'
+        else:
+            return 'behind' if behind else 'sync'
 
     def data_branch(self):
-        branch = self._bzr(self.path, ['nick'], catchout=True)
-        return branch or None
+        try:
+            return self._bzr(['nick']).rstrip() or None
+        except VcsError:
+            return None
 
     def data_info(self, rev=None):
-        if rev == None: rev = self.HEAD
-        rev = self._sanitize_rev(rev)
-        if rev == self.HEAD and not self._has_head(): return []
-
-        L = self._log(refspec=rev)
-        if len(L) == 0:
-            raise VcsError("Revision %s does not exist" % rev)
-        elif len(L) > 1:
-            raise VcsError("More than one instance of revision %s ?!?" % rev)
+        if rev is None:
+            rev = self.HEAD
+
+        log = self._log(refspec=rev)
+        if not log:
+            if rev == self.HEAD:
+                return None
+            else:
+                raise VcsError('Revision {0:s} does not exist'.format(rev))
+        elif len(log) == 1:
+            return log[0]
         else:
-            return L[0]
+            raise VcsError('More than one instance of revision {0:s} ?!?'.format(rev))
diff --git a/ranger/ext/vcs/vcs.py b/ranger/ext/vcs/vcs.py
index e2d86d4b..ae770699 100644
--- a/ranger/ext/vcs/vcs.py
+++ b/ranger/ext/vcs/vcs.py
@@ -6,6 +6,7 @@
 import os
 import subprocess
 import threading
+import time
 
 class VcsError(Exception):
     """VCS exception"""
@@ -35,10 +36,10 @@ class Vcs(object):
 
     # Backends
     REPOTYPES = {
-        'git': {'class': 'Git', 'setting': 'vcs_backend_git'},
-        'hg': {'class': 'Hg', 'setting': 'vcs_backend_hg'},
-        'bzr': {'class': 'Bzr', 'setting': 'vcs_backend_bzr'},
-        'svn': {'class': 'SVN', 'setting': 'vcs_backend_svn'},
+        'git': {'class': 'Git', 'setting': 'vcs_backend_git', 'lazy': False},
+        'hg': {'class': 'Hg', 'setting': 'vcs_backend_hg', 'lazy': True},
+        'bzr': {'class': 'Bzr', 'setting': 'vcs_backend_bzr', 'lazy': True},
+        'svn': {'class': 'SVN', 'setting': 'vcs_backend_svn', 'lazy': True},
     }
 
     # Possible directory statuses in order of importance with statuses that
@@ -73,7 +74,7 @@ class Vcs(object):
                 self.rootvcs = self
                 self.__class__ = getattr(getattr(ranger.ext.vcs, self.repotype),
                                          self.REPOTYPES[self.repotype]['class'])
-                self.status_subpaths = {}
+                self.status_subpaths = None
 
                 if not os.access(self.repodir, os.R_OK):
                     directoryobject.vcsstatus = 'unknown'
@@ -88,6 +89,7 @@ class Vcs(object):
                 except VcsError:
                     return
 
+                self.timestamp = time.time()
                 self.track = True
             else:
                 self.rootvcs = directoryobject.fm.get_directory(self.root).vcs
@@ -218,6 +220,7 @@ class Vcs(object):
         except VcsError:
             self.update_tree(purge=True)
             return False
+        self.timestamp = time.time()
         return True
 
     def check(self):
@@ -230,8 +233,28 @@ class Vcs(object):
             return False
         return True
 
+    def check_outdated(self):
+        """Check if outdated"""
+        for wroot, wdirs, _ in os.walk(self.path):
+            wrootobj = self.obj.fm.get_directory(wroot)
+            wrootobj.load_if_outdated()
+            if wroot != self.path and wrootobj.vcs.is_root:
+                wdirs[:] = []
+                continue
+
+            if wrootobj.stat and self.timestamp < wrootobj.stat.st_mtime:
+                return True
+            if wrootobj.files_all:
+                for wfile in wrootobj.files_all:
+                    if wfile.stat and self.timestamp < wfile.stat.st_mtime:
+                        return True
+        return False
+
     def status_root(self):
         """Returns root status"""
+        if self.rootvcs.status_subpaths is None:
+            return 'unknown'
+
         statuses = set(status for path, status in self.status_subpaths.items())
         for status in self.DIRSTATUSES:
             if status in statuses:
@@ -244,6 +267,9 @@ class Vcs(object):
 
         path needs to be self.obj.path or subpath thereof
         """
+        if self.rootvcs.status_subpaths is None:
+            return 'unknown'
+
         if path == self.obj.path:
             relpath = os.path.relpath(self.path, self.root)
         else:
@@ -345,7 +371,11 @@ class VcsThread(threading.Thread):
                                  and (clmn.target.path == target.vcs.repodir or
                                       clmn.target.path.startswith(target.vcs.repodir + '/'))):
                             continue
-                        if target.vcs.update_root():
+                        lazy = target.vcs.REPOTYPES[target.vcs.repotype]['lazy']
+                        if (target.vcs.rootvcs.status_subpaths is None \
+                                or (lazy and target.vcs.check_outdated()) \
+                                or not lazy) \
+                                and target.vcs.update_root():
                             target.vcs.update_tree()
                             redraw = True
 
91641d0e8b95120bc6a66accd'>^
2bbac1c8 ^
6bad38c2 ^
2bbac1c8 ^


0f3c1e56 ^



2bbac1c8 ^

e6987387 ^
2bbac1c8 ^
6bad38c2 ^
2bbac1c8 ^


0f3c1e56 ^



2bbac1c8 ^
5eaf687d ^

279737ba ^
2bbac1c8 ^
bbb0fbed ^
2bbac1c8 ^
e4ac23e9 ^
c58aca56 ^
4b73f3d7 ^

74a88ad5 ^


2bbac1c8 ^
74a88ad5 ^
2bbac1c8 ^
4d190a9c ^

279737ba ^

4d190a9c ^
e4ac23e9 ^
4d190a9c ^
bbb0fbed ^
2bbac1c8 ^

0f3c1e56 ^
2bbac1c8 ^

291c6bc3 ^

c58aca56 ^
2bbac1c8 ^

6bad38c2 ^
0f3c1e56 ^
2bbac1c8 ^
6bad38c2 ^
2bbac1c8 ^
6d329343 ^
291c6bc3 ^
4b73f3d7 ^
291c6bc3 ^



4b73f3d7 ^
2bbac1c8 ^


c90c83f7 ^









0030ae58 ^
c90c83f7 ^
0030ae58 ^

c90c83f7 ^


291c6bc3 ^


c58aca56 ^
4b73f3d7 ^
291c6bc3 ^

4b73f3d7 ^
291c6bc3 ^




4b73f3d7 ^
291c6bc3 ^


4b73f3d7 ^
291c6bc3 ^

bf1cf044 ^










8c5866ff ^








bf1cf044 ^



















































8c5866ff ^








































bf1cf044 ^




















































8c5866ff ^
bf1cf044 ^

















8c5866ff ^














bf1cf044 ^










































0b4c4649 ^
bf1cf044 ^
























0b4c4649 ^
8c5866ff ^



































0b4c4649 ^












c58aca56 ^











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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536