[PATCH 1 of 2 RFC] rollback: improve readability; clarify that the return value is an int

Greg Ward greg-hg at gerg.ca
Mon Sep 12 19:16:26 CDT 2011


# HG changeset patch
# User Greg Ward <greg-hg at gerg.ca>
# Date 1315790518 14400
# Node ID 2d12345fdecc19eff9d0498f9ee98b15669182c9
# Parent  3bccc15b201f449dd3223a227e1ac08723804448
rollback: improve readability; clarify that the return value is an int.

diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py
--- a/mercurial/localrepo.py
+++ b/mercurial/localrepo.py
@@ -760,50 +760,54 @@
             wlock = self.wlock()
             lock = self.lock()
             if os.path.exists(self.sjoin("undo")):
-                try:
-                    args = self.opener.read("undo.desc").splitlines()
-                    if len(args) >= 3 and self.ui.verbose:
-                        desc = _("repository tip rolled back to revision %s"
-                                 " (undo %s: %s)\n") % (
-                                 int(args[0]) - 1, args[1], args[2])
-                    elif len(args) >= 2:
-                        desc = _("repository tip rolled back to revision %s"
-                                 " (undo %s)\n") % (
-                                 int(args[0]) - 1, args[1])
-                except IOError:
-                    desc = _("rolling back unknown transaction\n")
-                self.ui.status(desc)
-                if dryrun:
-                    return
-                transaction.rollback(self.sopener, self.sjoin("undo"),
-                                     self.ui.warn)
-                util.rename(self.join("undo.dirstate"), self.join("dirstate"))
-                if os.path.exists(self.join('undo.bookmarks')):
-                    util.rename(self.join('undo.bookmarks'),
-                                self.join('bookmarks'))
-                try:
-                    branch = self.opener.read("undo.branch")
-                    self.dirstate.setbranch(branch)
-                except IOError:
-                    self.ui.warn(_("named branch could not be reset, "
-                                   "current branch is still: %s\n")
-                                 % self.dirstate.branch())
-                self.invalidate()
-                self.dirstate.invalidate()
-                self.destroyed()
-                parents = tuple([p.rev() for p in self.parents()])
-                if len(parents) > 1:
-                    self.ui.status(_("working directory now based on "
-                                     "revisions %d and %d\n") % parents)
-                else:
-                    self.ui.status(_("working directory now based on "
-                                     "revision %d\n") % parents)
+                return self._rollback(dryrun)
             else:
                 self.ui.warn(_("no rollback information available\n"))
                 return 1
         finally:
             release(lock, wlock)
 
+    def _rollback(self, dryrun):
+        try:
+            args = self.opener.read("undo.desc").splitlines()
+            if len(args) >= 3 and self.ui.verbose:
+                desc = _("repository tip rolled back to revision %s"
+                         " (undo %s: %s)\n") % (
+                         int(args[0]) - 1, args[1], args[2])
+            elif len(args) >= 2:
+                desc = _("repository tip rolled back to revision %s"
+                         " (undo %s)\n") % (
+                         int(args[0]) - 1, args[1])
+        except IOError:
+            desc = _("rolling back unknown transaction\n")
+        self.ui.status(desc)
+        if dryrun:
+            return 0
+        transaction.rollback(self.sopener, self.sjoin("undo"),
+                             self.ui.warn)
+        util.rename(self.join("undo.dirstate"), self.join("dirstate"))
+        if os.path.exists(self.join('undo.bookmarks')):
+            util.rename(self.join('undo.bookmarks'),
+                        self.join('bookmarks'))
+        try:
+            branch = self.opener.read("undo.branch")
+            self.dirstate.setbranch(branch)
+        except IOError:
+            self.ui.warn(_("named branch could not be reset, "
+                           "current branch is still: %s\n")
+                         % self.dirstate.branch())
+        self.invalidate()
+        self.dirstate.invalidate()
+        self.destroyed()
+        parents = tuple([p.rev() for p in self.parents()])
+        if len(parents) > 1:
+            self.ui.status(_("working directory now based on "
+                             "revisions %d and %d\n") % parents)
+        else:
+            self.ui.status(_("working directory now based on "
+                             "revision %d\n") % parents)
+        return 0
+
     def invalidatecaches(self):
         try:
             delattr(self, '_tagscache')


More information about the Mercurial-devel mailing list