1. TortoiseHg
  2. TortoiseHg
  3. thg

Commits

TK Soh  committed 4fc4728

commit: add tool button to undo last commit

  • Participants
  • Parent commits a1ec75c
  • Branches default

Comments (0)

Files changed (3)

File hggtk/commit.py

View file
  • Ignore whitespace
 
     ### Overrides of base class methods ###
 
+    def init(self):
+        GStatus.init(self)
+        self._last_commit_id = None
+
     def parse_opts(self):
         GStatus.parse_opts(self)
 
 
     def get_tbbuttons(self):
         tbbuttons = GStatus.get_tbbuttons(self)
+        tbbuttons.insert(2, gtk.SeparatorToolItem())
+        self._undo_button = self.make_toolbutton(gtk.STOCK_UNDO, '_Undo',
+            self._undo_clicked, tip='undo recent commit')
+        tbbuttons.insert(2, self._undo_button)
         tbbuttons.insert(2, self.make_toolbutton(gtk.STOCK_OK, '_Commit',
             self._commit_clicked, tip='commit'))
         return tbbuttons
     def reload_status(self):
         success = GStatus.reload_status(self)
         self._check_merge()
+        self._check_undo()
         return success
 
 
     ### End of overridable methods ###
 
+    def _check_undo(self):
+        can_undo = os.path.exists(self.repo.sjoin("undo")) and \
+                self._last_commit_id is not None
+        self._undo_button.set_sensitive(can_undo)
+
+
     def _check_merge(self):
         # disable the checkboxes on the filelist if repo in merging state
         merged = len(self.repo.workingctx().parents()) > 1
         return True
 
 
+    def _undo_clicked(self, toolbutton, data=None):
+        response = Confirm('Undo commit', [], self, 'Undo last commit').run() 
+        if response != gtk.RESPONSE_YES:
+            return
+            
+        tip = self._get_tip_rev(True)
+        if not tip == self._last_commit_id:
+            Prompt('Undo commit', 
+                    'Unable to undo!\n\n'
+                    'Tip revision differs from last commit.',
+                    self).run()
+            return
+            
+        try:
+            self.repo.rollback()
+            self._last_commit_id = None
+            self.reload_status()
+        except:
+            Prompt('Undo commit', 'Errors during rollback!',
+                    self).run()
+
+
     def _should_addremove(self, files):
         if self.test_opt('addremove'):
             return True
         self.text.set_buffer(gtk.TextBuffer())
         self._update_recent_messages(self.opts['message'])
         shell_notify([self.cwd] + files)
+        self._last_commit_id = self._get_tip_rev(True)
         self.reload_status()
 
+    def _get_tip_rev(self, refresh=False):
+        if refresh:
+            self.repo.invalidate()
+        cl = self.repo.changelog
+        tip = cl.node(nullrev + cl.count())
+        return hex(tip)
+
 def launch(root='', files=[], cwd='', main=True):
     u = ui.ui()
     u.updateopts(debug=False, traceback=False)

File hggtk/gdialog.py

View file
  • Ignore whitespace
         self.tmproot = None
         self.toolbuttons = {}
         self.settings = Settings(self.__class__.__name__)
+        self.init()
 
     ### Following methods are meant to be overridden by subclasses ###
 
+    def init(self):
+        pass
+
     def parse_opts(self):
         pass
 

File hggtk/status.py

View file
  • Ignore whitespace
 
     ### Following methods are meant to be overridden by subclasses ###
 
+    def init(self):
+        GDialog.init(self)
+        
     def auto_check(self):
         if self.test_opt('check'):
             for entry in self.model : entry[0] = True