Commits

Henrik Stuart committed e514db3

misc cleanup

  • Participants
  • Parent commits 74a5ef3

Comments (0)

Files changed (1)

         self.repo = repo
         self.filecache = {}
         try:
-            self.undolst = open(self._undoListPath()).read().splitlines()
-            data = open(self._undoIndexPath()).read()
-            self.currentidx = int(data)
-            if (self.currentidx + 1 < self._firstIndex()) or (self._lastIndex() < self.currentidx):
-                raise ValueError('currentIndex is outside first to last index')
+            with open(self._listpath()) as fh:
+                self.undolst = fh.read().splitlines()
+
+            indexpath = self._indexpath()
+
+            with open(indexpath) as fh:
+                data = fh.read()
+            self.index = int(data)
+            if self.index + 1 < self._firstindex() or self._lastindex() < self.index:
+                raise ValueError('invalid undo index found in %s' % (indexpath))
         except (IOError, ValueError):
             self.undolst = []
-            self.currentidx = -1
+            self.index = -1
 
-    def _firstIndex(self):
+    def _firstindex(self):
         return int(self.undolst[ 0].split()[0]) if self.undolst else -1
 
-    def _lastIndex(self):
+    def _lastindex(self):
         return int(self.undolst[-1].split()[0]) if self.undolst else -1
 
-    def _undoListPath(self):
-        return self.repo.join(os.path.join('multiundo','multiundo.lst'))
+    def _listpath(self):
+        return self.repo.join(os.path.join('multiundo', 'multiundo.lst'))
 
-    def _undoIndexPath(self):
-        return self.repo.join(os.path.join('multiundo','multiundo.idx'))
+    def _indexpath(self):
+        return self.repo.join(os.path.join('multiundo', 'multiundo.idx'))
 
     def _ithUndoRoot(self, idx):
         return self.repo.join(os.path.join('multiundo','undo', str(idx)))
         return self.repo.join(os.path.join('multiundo','redo', str(idx)))
     
     def _write_currentidx(self):
-        _ensureDirectoryExistance(os.path.dirname(self._undoIndexPath()))
-        open(self._undoIndexPath(), 'w').write(str(self.currentidx) + '\n')
+        _ensureDirectoryExistance(os.path.dirname(self._indexpath()))
+        open(self._indexpath(), 'w').write(str(self.index) + '\n')
 
     def _write_undoList(self):
         if not self.undolst:
-            _deletePath(self._undoListPath())
+            _deletePath(self._listpath())
         else:
             try:
-                path = self._undoListPath()
+                path = self._listpath()
                 _ensureDirectoryExistance(os.path.dirname(path))
                 f = open(path, 'w')
                 f.write(string.join(self.undolst,"\n"))
         
 
     def _delete_stale(self):
-        for i in xrange(self.currentidx + 1, self._lastIndex() + 1):
+        for i in xrange(self.index + 1, self._lastindex() + 1):
             _deletePath(self._ithUndoRoot(i))
         _deletePath(self.repo.join(os.path.join('multiundo','redo')))
-        self.undolst = self.undolst[:(self.currentidx + 1 - self._firstIndex())] if self.currentidx != -1 else []
+        self.undolst = self.undolst[:(self.index + 1 - self._firstindex())] if self.index != -1 else []
     
     def _pruneOldUndos(self):
         keepCount = int(self.repo.ui.config('multiundo', 'undocount', default=5))
-        keepCount = max(keepCount, self._lastIndex() - self.currentidx)
+        keepCount = max(keepCount, self._lastindex() - self.index)
         currentUndoCount = len(self.undolst)
         if currentUndoCount > keepCount:
-            newFirst = self._lastIndex() - keepCount
-            for i in xrange(self._firstIndex(), newFirst + 1):
+            newFirst = self._lastindex() - keepCount
+            for i in xrange(self._firstindex(), newFirst + 1):
                 _deletePath(self._ithRedoRoot(i))
                 _deletePath(self._ithUndoRoot(i))
             self.undolst = self.undolst[-keepCount:]
     
     def _addNewUndoState(self):
         global command
-        self.currentidx += 1
-        self.undolst.append(str(self.currentidx).rjust(3) + ' ' + command + '\n')
+        self.index += 1
+        self.undolst.append(str(self.index).rjust(3) + ' ' + command + '\n')
 
     def addfile(self, base, fname, hardlink=False):
         global written
             self._write_currentidx()
             self._write_undoList()
 
-        undoRoot = self._ithUndoRoot(self.currentidx)
+        undoRoot = self._ithUndoRoot(self.index)
         target = os.path.join(undoRoot, sourcebase)
         _ensureDirectoryExistance(os.path.dirname(target))
 
 
     def liststates(self, ui):
         ui.write('Available states:\n')
-        if self.currentidx == -1:
+        if self.index == -1:
             ui.write('  (current)\n')
         if self.undolst:
             for entry in self.undolst:
                 ui.write('  %s\n' % entry)
-                if self.currentidx == int(entry.split()[0]):
+                if self.index == int(entry.split()[0]):
                     ui.write('  (current)\n')
-        if self.currentidx > self._lastIndex():
+        if self.index > self._lastindex():
             ui.write('  (current)\n')
 
     def _undo_replay(self, ui):
-        undoroot = self._ithUndoRoot(self.currentidx)
-        redoRoot = self._ithRedoRoot(self.currentidx)
+        undoroot = self._ithUndoRoot(self.index)
+        redoRoot = self._ithRedoRoot(self.index)
         _deletePath(redoRoot)
         _copyTree(undoroot, self.repo.root, redoRoot)
         _copyTree(undoroot, undoroot, self.repo.root)
 
     def _redo_replay(self, ui):
-        redoRoot = self._ithRedoRoot(self.currentidx)
+        redoRoot = self._ithRedoRoot(self.index)
         _copyTree(redoRoot, redoRoot, self.repo.root)
 
     def undo(self, ui):
-        ui.write('Undoing state %d\n' % (self.currentidx,))
+        ui.write('Undoing state %d\n' % (self.index,))
         self._undo_replay(ui)
-        self.currentidx -= 1
+        self.index -= 1
         self._write_currentidx()
         ui.write('Done\n')
 
     def redo(self, ui):
-        self.currentidx += 1
-        ui.write('Redoing state %d\n' % (self.currentidx,))
+        self.index += 1
+        ui.write('Redoing state %d\n' % (self.index,))
         self._redo_replay(ui)
         self._write_currentidx()
         ui.write('Done\n')
     if opts.get('clear'):
         return _clear(ui, repo)
 
-    if undoer.currentidx < undoer._firstIndex() or undoer.currentidx == -1:
+    if undoer.index < undoer._firstindex() or undoer.index == -1:
         raise util.Abort('nothing to undo')
 
     u = undoer
     if opts.get('clear'):
         return _clear(ui, repo)
 
-    if undoer.currentidx >= undoer._lastIndex():
+    if undoer.index >= undoer._lastindex():
         raise util.Abort('nothing to redo')
 
     u = undoer