changeset 162:5dcbe4d9a30c

Implement recover and undo commands This adds an interface to transaction to rollback with a given journal file and commands to rollback an existing .hg/journal or .hg/undo.
author mpm@selenic.com
date Thu, 26 May 2005 09:04:54 -0800
parents 0b4c5cb953d9
children f38c90953c2c
files hg mercurial/hg.py mercurial/transaction.py
diffstat 3 files changed, 24 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/hg	Thu May 26 08:53:04 2005 -0800
+++ b/hg	Thu May 26 09:04:54 2005 -0800
@@ -423,6 +423,14 @@
             r = "?"
         print "%-30s %5d:%s" % (k, repo.changelog.rev(n), hg.hex(n))
 
+elif cmd == "recover":
+    ui.status("rolling back any existing journal")
+    repo.recover()
+
+elif cmd == "undo":
+    ui.status("rolling back previous transaction")
+    repo.recover("undo")
+
 elif cmd == "verify":
     filelinkrevs = {}
     filenodes = {}
--- a/mercurial/hg.py	Thu May 26 08:53:04 2005 -0800
+++ b/mercurial/hg.py	Thu May 26 09:04:54 2005 -0800
@@ -297,6 +297,11 @@
         return transaction(self.opener, self.join("journal"),
                            self.join("undo"))
 
+    def recover(self, f = "journal"):
+        self.lock()
+        if os.path.exists(self.join(f)):
+            return rollback(self.opener, self.join(f))
+
     def lock(self, wait = 1):
         try:
             return lock.lock(self.join("lock"), 0)
--- a/mercurial/transaction.py	Thu May 26 08:53:04 2005 -0800
+++ b/mercurial/transaction.py	Thu May 26 09:04:54 2005 -0800
@@ -15,16 +15,18 @@
 
 class transaction:
     def __init__(self, opener, journal, after = None):
+        self.journal = None
+
+        # abort here if the journal already exists
+        if os.path.exists(journal):
+            raise "journal already exists - run hg recover"
+
         self.opener = opener
         self.after = after
         self.entries = []
         self.map = {}
         self.journal = journal
 
-        # abort here if the journal already exists
-        if os.path.exists(self.journal):
-            raise "journal already exists!"
-
         self.file = open(self.journal, "w")
 
     def __del__(self):
@@ -63,9 +65,9 @@
 
         print "rollback completed"
         
-    def recover(self):
-        for l in open(self.journal).readlines():
-            f, o = l.split('\0')
-            self.opener(f, "a").truncate(int(o))
-        os.unlink(self.journal)
+def rollback(opener, file):
+    for l in open(file).readlines():
+        f, o = l.split('\0')
+        opener(f, "a").truncate(int(o))
+    os.unlink(file)