[Pkg-bazaar-commits] ./bzr/unstable r778: - simple revert of text files

Martin Pool mbp at sourcefrog.net
Fri Apr 10 08:21:01 UTC 2009


------------------------------------------------------------
revno: 778
committer: Martin Pool <mbp at sourcefrog.net>
timestamp: Fri 2005-06-24 20:42:47 +1000
message:
  - simple revert of text files 
  
  - test cases for this
modified:
  bzrlib/branch.py
  bzrlib/commands.py
  bzrlib/selftest/whitebox.py
-------------- next part --------------
=== modified file 'bzrlib/branch.py'
--- a/bzrlib/branch.py	2005-06-24 09:18:32 +0000
+++ b/bzrlib/branch.py	2005-06-24 10:42:47 +0000
@@ -948,6 +948,43 @@
             self.unlock()
 
 
+    def revert(self, filenames, old_tree=None):
+        """Restore selected files to the versions from a previous tree.
+        """
+        from bzrlib.errors import NotVersionedError, BzrError
+        from bzrlib.atomicfile import AtomicFile
+        
+        inv = self.read_working_inventory()
+        if old_tree is None:
+            old_tree = self.basis_tree()
+        old_inv = old_tree.inventory
+
+        nids = []
+        for fn in filenames:
+            file_id = inv.path2id(fn)
+            if not file_id:
+                raise NotVersionedError("not a versioned file", fn)
+            nids.append((fn, file_id))
+            
+        # TODO: Rename back if it was previously at a different location
+
+        # TODO: If given a directory, restore the entire contents from
+        # the previous version.
+
+        # TODO: Make a backup to a temporary file.
+
+        # TODO: If the file previously didn't exist, delete it?
+        for fn, file_id in nids:
+            if not old_inv.has_id(file_id):
+                raise BzrError("file not present in old tree", fn, file_id)
+            f = AtomicFile(fn, 'wb')
+            try:
+                f.write(old_tree.get_file(file_id).read())
+                f.commit()
+            finally:
+                f.close()
+
+
 
 class ScratchBranch(Branch):
     """Special test class: a branch that cleans up after itself.

=== modified file 'bzrlib/commands.py'
--- a/bzrlib/commands.py	2005-06-24 04:48:14 +0000
+++ b/bzrlib/commands.py	2005-06-24 10:42:47 +0000
@@ -1233,6 +1233,20 @@
               check_clean=(not force))
 
 
+
+class cmd_simple_revert(Command):
+    """Restore selected files from a previous revision.
+    """
+    takes_args = ['file+']
+    def run(self, file_list):
+        if not file_list:
+            file_list = ['.']
+            
+        b = find_branch(file_list[0])
+
+        b.revert([b.relpath(f) for f in file_list])
+
+
 class cmd_revert(Command):
     """Reverse all changes since the last commit.
 

=== modified file 'bzrlib/selftest/whitebox.py'
--- a/bzrlib/selftest/whitebox.py	2005-06-24 09:09:02 +0000
+++ b/bzrlib/selftest/whitebox.py	2005-06-24 10:42:47 +0000
@@ -5,14 +5,40 @@
 
 from bzrlib.selftest import InTempDir, TestBase
 from bzrlib.branch import ScratchBranch, Branch
-from bzrlib.errors import NotBranchError
+from bzrlib.errors import NotBranchError, NotVersionedError
+
+
+class Revert(InTempDir):
+    """Test selected-file revert"""
+    def runTest(self):
+        b = Branch('.', init=True)
+
+        self.build_tree(['hello.txt'])
+        file('hello.txt', 'w').write('initial hello')
+
+        self.assertRaises(NotVersionedError,
+                          b.revert, ['hello.txt'])
+        
+        b.add(['hello.txt'])
+        b.commit('create initial hello.txt')
+
+        self.check_file_contents('hello.txt', 'initial hello')
+        file('hello.txt', 'w').write('new hello')
+        self.check_file_contents('hello.txt', 'new hello')
+
+        # revert file modified since last revision
+        b.revert(['hello.txt'])
+        self.check_file_contents('hello.txt', 'initial hello')
+
+        # reverting again causes no change
+        b.revert(['hello.txt'])
+        self.check_file_contents('hello.txt', 'initial hello')
+
 
 
 class RenameDirs(InTempDir):
     """Test renaming directories and the files within them."""
     def runTest(self):
-        from bzrlib.commit import commit
-
         b = Branch('.', init=True)
         self.build_tree(['dir/', 'dir/sub/', 'dir/sub/file'])
         b.add(['dir', 'dir/sub', 'dir/sub/file'])



More information about the Pkg-bazaar-commits mailing list