[PATCH] rollback: refactor for readability; cosmetics

Greg Ward greg-hg at gerg.ca
Sun Sep 18 19:04:04 CDT 2011


# HG changeset patch
# User Greg Ward <greg at gerg.ca>
# Date 1316223486 14400
# Node ID 6348bd575fd4c439e487cf570cc2cba11de5f596
# Parent  9b41ccb2043e724690018f7cd25645dc5b980d51
rollback: refactor for readability; cosmetics.

- clarify how we parse undo.desc
- fix bad grammar in an error message
- factor out ui local
- rename some local variables
- standardize string quoting

diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py
--- a/mercurial/localrepo.py
+++ b/mercurial/localrepo.py
@@ -768,44 +768,49 @@
             release(lock, wlock)
 
     def _rollback(self, dryrun):
+        ui = self.ui
         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])
+            args = self.opener.read('undo.desc').splitlines()
+            (oldlen, desc, detail) = (int(args[0]), args[1], None)
+            if len(args) >= 3:
+                detail = args[2]
+            oldtip = oldlen - 1
+
+            if detail and ui.verbose:
+                msg = (_('repository tip rolled back to revision %s'
+                         ' (undo %s: %s)\n')
+                       % (oldtip, desc, detail))
+            else:
+                msg = (_('repository tip rolled back to revision %s'
+                         ' (undo %s)\n')
+                       % (oldtip, desc))
         except IOError:
-            desc = _("rolling back unknown transaction\n")
-        self.ui.status(desc)
+            msg = _('rolling back unknown transaction\n')
+        ui.status(msg)
         if dryrun:
             return 0
-        transaction.rollback(self.sopener, self.sjoin("undo"),
-                             self.ui.warn)
-        util.rename(self.join("undo.dirstate"), self.join("dirstate"))
+        transaction.rollback(self.sopener, self.sjoin('undo'), 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")
+            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())
+            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)
+            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)
+            ui.status(_('working directory now based on '
+                        'revision %d\n') % parents)
         return 0
 
     def invalidatecaches(self):
diff --git a/tests/test-rollback.t b/tests/test-rollback.t
--- a/tests/test-rollback.t
+++ b/tests/test-rollback.t
@@ -59,7 +59,7 @@
   $ rm .hg/undo.branch
   $ hg rollback
   repository tip rolled back to revision 0 (undo commit)
-  named branch could not be reset, current branch is still: test
+  named branch could not be reset: current branch is still 'test'
   working directory now based on revision 0
   $ hg branch
   test


More information about the Mercurial-devel mailing list