Commits

Pierre-Yves David committed 423c62a

add rollback support.

Comments (0)

Files changed (2)

 
 name are not fixed yet.
 '''
+import os
 from functools import partial
+
 from mercurial.i18n import _
 from mercurial import cmdutil
 from mercurial import scmutil
 from mercurial import extensions
 from mercurial import wireproto
 from mercurial import pushkey
+from mercurial.lock import release
 
 
 _NOSHARE=2
     opull = repo.pull
     opush = repo.push
     o_tag = repo._tag
+    orollback = repo.rollback
+    o_writejournal = repo._writejournal
     class statefulrepo(repo.__class__):
 
         def nodestate(self, node):
             except IOError:
                 pass
             return heads
-        def _readstatesheads(self):
+
+        def _readstatesheads(self, undo=False):
             statesheads = {}
             for state in STATES:
                 if state.trackheads:
-                    filename = 'states/%s-heads' % state.name
+                    filemask = 'states/%s-heads'
+                    filename = filemask % state.name
                     statesheads[state] = self._readheadsfile(filename)
             return statesheads
 
                 remote = map(node.bin, remote.listkeys('immutableheads'))
             return remote
 
+        ### Tag support
+
         def _tag(self, names, node, *args, **kwargs):
             tagnode = o_tag(names, node, *args, **kwargs)
             self.setstate(ST0, [node, tagnode])
             return tagnode
 
+        ### rollback support
+
+        def _writejournal(self, desc):
+            entries = list(o_writejournal(desc))
+            for state in STATES:
+                if state.trackheads:
+                    filename = 'states/%s-heads' % state.name
+                    filepath = self.join(filename)
+                    if  os.path.exists(filepath):
+                        journalname = 'states/journal.%s-heads' % state.name
+                        journalpath = self.join(journalname)
+                        util.copyfile(filepath, journalpath)
+                        entries.append(journalpath)
+            return tuple(entries)
+
+        def rollback(self, dryrun=False):
+            wlock = lock = None
+            try:
+                wlock = self.wlock()
+                lock = self.lock()
+                ret = orollback(dryrun)
+                if not (ret or dryrun): #rollback did not failed
+                    for state in STATES:
+                        if state.trackheads:
+                            src  = self.join('states/undo.%s-heads') % state.name
+                            dest = self.join('states/%s-heads') % state.name
+                            if os.path.exists(src):
+                                util.rename(src, dest)
+                            elif os.path.exists(dest): #unlink in any case
+                                os.unlink(dest)
+                    self.__dict__.pop('_statesheads', None)
+                return ret
+            finally:
+                release(lock, wlock)
+
     repo.__class__ = statefulrepo
 

tests/test-ready.t

   date:        Thu Jan 01 00:00:00 1970 +0000
   summary:     Added tag babar for changeset cdaaa31e4239
   
+Check rollback
 
+  $ hg merge
+  1 files updated, 0 files merged, 0 files removed, 0 files unresolved
+  (branch merge, don't forget to commit)
+  $ hg commit -m 'merge'
+  $ hg published tip
+  $ hg log -r 'publishedheads()'
+  changeset:   7:26631d82e09e
+  tag:         tip
+  parent:      6:bd66bf1525ee
+  parent:      5:cdaaa31e4239
+  user:        test
+  date:        Thu Jan 01 00:00:00 1970 +0000
+  summary:     merge
+  
+test rollback does not mess up the liquid changeset
+  $ mkcommit bibi
+  $ hg rollback
+  repository tip rolled back to revision 7 (undo commit)
+  working directory now based on revision 7
+  $ hg log -r 'publishedheads()'
+  changeset:   7:26631d82e09e
+  tag:         tip
+  parent:      6:bd66bf1525ee
+  parent:      5:cdaaa31e4239
+  user:        test
+  date:        Thu Jan 01 00:00:00 1970 +0000
+  summary:     merge
+  
+
+test rollback even when rollbacking frozen
+
+  $ mkcommit bubu
+  $ hg published tip
+  $ hg rollback
+  repository tip rolled back to revision 7 (undo commit)
+  working directory now based on revision 7
+  $ hg log -r 'publishedheads()'
+  changeset:   7:26631d82e09e
+  tag:         tip
+  parent:      6:bd66bf1525ee
+  parent:      5:cdaaa31e4239
+  user:        test
+  date:        Thu Jan 01 00:00:00 1970 +0000
+  summary:     merge
+  
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.