Commits

Yuya Nishihara  committed c9932e7

rotate backup files

  • Participants
  • Parent commits 7f6d1cd

Comments (0)

Files changed (1)

File hgext/backupstore.py

 """
 
 import errno, shutil
-from mercurial import dispatch, extensions, scmutil
+from mercurial import dispatch, extensions, scmutil, util
 
 testedwith = ''  # TODO
 
+class storebackupopener(scmutil.opener):
+    def rename(self, srcpath, destpath):
+        util.rename(self.join(srcpath), self.join(destpath))
+
 class backupopener(scmutil.abstractopener):
+    lastkeep = 5  # make it configurable?
+
     def __init__(self, opener, backupbase):
         self._coreopener = opener
-        self._backupopener = scmutil.opener(backupbase, audit=False)
+        self._backupopener = storebackupopener(backupbase, audit=False)
 
     def _makebackup(self, path):
-        # TODO generation
+        self._rotatebackup(path)
         src, dest = None, None
         try:
             src = self._coreopener(path)
             if src:
                 src.close()
 
+    def _rotatebackup(self, path, n=0):
+        if (n >= self.lastkeep
+            or not self._backupopener.exists('%s.%d' % (path, n))):
+            return
+        self._rotatebackup(path, n + 1)
+        self._backupopener.rename('%s.%d' % (path, n), '%s.%d' % (path, n + 1))
+
     def __call__(self, path, mode='r', *args, **kwargs):
         if mode not in ('r', 'rb'):
             self._makebackup(path)