/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz

« back to all changes in this revision

Viewing changes to commit.py

  • Committer: Vincent Ladeuil
  • Date: 2009-05-28 12:58:49 UTC
  • mto: This revision was merged to the branch mainline in revision 640.
  • Revision ID: v.ladeuil+lp@free.fr-20090528125849-9w4bhdp7cyzjokmh
Fix a leaking dialog windows appearing during the test suite.

* tests/test_commit.py:
(TestReusingSavedCommitMessages._get_commit_dialog): Ensure we
never prompt the user.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
 
17
import os.path
 
18
import re
 
19
 
17
20
try:
18
21
    import pygtk
19
22
    pygtk.require("2.0")
24
27
import gobject
25
28
import pango
26
29
 
27
 
import os.path
28
 
import re
29
 
 
30
30
from bzrlib import errors, osutils
31
31
from bzrlib.trace import mutter
32
32
from bzrlib.util import bencode
 
33
try:
 
34
    from bzrlib.uncommit import SavedCommitMessagesManager
 
35
except ImportError: # old bzrlib
 
36
    can_save_commit_messages = False
 
37
else:
 
38
    can_save_commit_messages = True
33
39
 
34
40
from bzrlib.plugins.gtk import _i18n
35
 
from dialog import error_dialog, question_dialog
36
 
from errors import show_bzr_error
 
41
from bzrlib.plugins.gtk.dialog import question_dialog
 
42
from bzrlib.plugins.gtk.errors import show_bzr_error
37
43
 
38
44
try:
39
45
    import dbus
97
103
    return pm
98
104
 
99
105
 
 
106
_newline_variants_re = re.compile(r'\r\n?')
 
107
def _sanitize_and_decode_message(utf8_message):
 
108
    """Turn a utf-8 message into a sanitized Unicode message."""
 
109
    fixed_newline = _newline_variants_re.sub('\n', utf8_message)
 
110
    return fixed_newline.decode('utf-8')
 
111
 
 
112
 
100
113
class CommitDialog(gtk.Dialog):
101
114
    """Implementation of Commit."""
102
115
 
103
116
    def __init__(self, wt, selected=None, parent=None):
104
 
        gtk.Dialog.__init__(self, title="Commit - Olive",
105
 
                                  parent=parent,
106
 
                                  flags=0,
107
 
                                  buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
 
117
        gtk.Dialog.__init__(self, title="Commit to %s" % wt.basedir,
 
118
                            parent=parent, flags=0,)
 
119
        self.connect('delete-event', self._on_delete_window)
108
120
        self._question_dialog = question_dialog
109
121
 
 
122
        self.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_NORMAL)
 
123
 
110
124
        self._wt = wt
111
125
        # TODO: Do something with this value, it is used by Olive
112
126
        #       It used to set all changes but this one to False
114
128
        self._enable_per_file_commits = True
115
129
        self._commit_all_changes = True
116
130
        self.committed_revision_id = None # Nothing has been committed yet
 
131
        if can_save_commit_messages:
 
132
            self._saved_commit_messages_manager = SavedCommitMessagesManager(self._wt, self._wt.branch)
117
133
 
118
134
        self.setup_params()
119
135
        self.construct()
188
204
        self._basis_tree.lock_read()
189
205
        try:
190
206
            from diff import iter_changes_to_status
 
207
            if can_save_commit_messages:
 
208
                saved_file_messages = self._saved_commit_messages_manager.get()[1]
 
209
            else:
 
210
                saved_file_messages = {}
191
211
            for (file_id, real_path, change_type, display_path
192
212
                ) in iter_changes_to_status(self._basis_tree, self._wt):
193
213
                if self._selected and real_path != self._selected:
194
214
                    enabled = False
195
215
                else:
196
216
                    enabled = True
 
217
                try:
 
218
                    default_message = saved_file_messages[file_id]
 
219
                except KeyError:
 
220
                    default_message = ''
197
221
                item_iter = store.append([
198
222
                    file_id,
199
223
                    real_path.encode('UTF-8'),
200
224
                    enabled,
201
225
                    display_path.encode('UTF-8'),
202
226
                    change_type,
203
 
                    '', # Initial comment
 
227
                    default_message, # Initial comment
204
228
                    ])
205
229
                if self._selected and enabled:
206
230
                    initial_cursor = store.get_path(item_iter)
338
362
        self._hpane.pack2(self._right_pane_table, resize=True, shrink=True)
339
363
 
340
364
    def _construct_action_pane(self):
 
365
        self._button_cancel = gtk.Button(stock=gtk.STOCK_CANCEL)
 
366
        self._button_cancel.connect('clicked', self._on_cancel_clicked)
 
367
        self._button_cancel.show()
 
368
        self.action_area.pack_end(self._button_cancel)
341
369
        self._button_commit = gtk.Button(_i18n("Comm_it"), use_underline=True)
342
370
        self._button_commit.connect('clicked', self._on_commit_clicked)
343
371
        self._button_commit.set_flags(gtk.CAN_DEFAULT)
539
567
        scroller.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
540
568
 
541
569
        self._global_message_text_view = gtk.TextView()
 
570
        if can_save_commit_messages:
 
571
            self._set_global_commit_message(self._saved_commit_messages_manager.get()[0])
542
572
        self._global_message_text_view.modify_font(pango.FontDescription("Monospace"))
543
573
        scroller.add(self._global_message_text_view)
544
574
        scroller.set_shadow_type(gtk.SHADOW_IN)
630
660
            if self._commit_all_changes or record[2]:# [2] checkbox
631
661
                file_id = record[0] # [0] file_id
632
662
                path = record[1]    # [1] real path
633
 
                file_message = record[5] # [5] commit message
 
663
                # [5] commit message
 
664
                file_message = _sanitize_and_decode_message(record[5])
634
665
                files.append(path.decode('UTF-8'))
635
666
                if self._enable_per_file_commits and file_message:
636
667
                    # All of this needs to be utf-8 information
 
668
                    file_message = file_message.encode('UTF-8')
637
669
                    file_info.append({'path':path, 'file_id':file_id,
638
670
                                     'message':file_message})
639
671
        file_info.sort(key=lambda x:(x['path'], x['file_id']))
643
675
            return files, []
644
676
 
645
677
    @show_bzr_error
 
678
    def _on_cancel_clicked(self, button):
 
679
        """ Cancel button clicked handler. """
 
680
        self._do_cancel()
 
681
 
 
682
    @show_bzr_error
 
683
    def _on_delete_window(self, source, event):
 
684
        """ Delete window handler. """
 
685
        self._do_cancel()
 
686
 
 
687
    def _do_cancel(self):
 
688
        """If requested, saves commit messages when cancelling gcommit; they are re-used by a next gcommit"""
 
689
        if can_save_commit_messages:
 
690
            mgr = SavedCommitMessagesManager()
 
691
            self._saved_commit_messages_manager = mgr
 
692
            mgr.insert(self._get_global_commit_message(),
 
693
                       self._get_specific_files()[1])
 
694
            if mgr.is_not_empty(): # maybe worth saving
 
695
                response = self._question_dialog(
 
696
                    _i18n('Commit cancelled'),
 
697
                    _i18n('Do you want to save your commit messages ?'),
 
698
                    parent=self)
 
699
                if response == gtk.RESPONSE_NO:
 
700
                     # save nothing and destroy old comments if any
 
701
                    mgr = SavedCommitMessagesManager()
 
702
            mgr.save(self._wt, self._wt.branch)
 
703
        self.response(gtk.RESPONSE_CANCEL) # close window
 
704
 
 
705
    @show_bzr_error
646
706
    def _on_commit_clicked(self, button):
647
707
        """ Commit button clicked handler. """
648
708
        self._do_commit()
653
713
        if message == '':
654
714
            response = self._question_dialog(
655
715
                _i18n('Commit with an empty message?'),
656
 
                _i18n('You can describe your commit intent in the message.'))
 
716
                _i18n('You can describe your commit intent in the message.'),
 
717
                parent=self)
657
718
            if response == gtk.RESPONSE_NO:
658
719
                # Kindly give focus to message area
659
720
                self._global_message_text_view.grab_focus()
673
734
        for path in self._wt.unknowns():
674
735
            response = self._question_dialog(
675
736
                _i18n("Commit with unknowns?"),
676
 
                _i18n("Unknown files exist in the working tree. Commit anyway?"))
 
737
                _i18n("Unknown files exist in the working tree. Commit anyway?"),
 
738
                parent=self)
 
739
                # Doesn't set a parent for the dialog..
677
740
            if response == gtk.RESPONSE_NO:
678
741
                return
679
742
            break
693
756
            response = self._question_dialog(
694
757
                _i18n('Commit with no changes?'),
695
758
                _i18n('There are no changes in the working tree.'
696
 
                      ' Do you want to commit anyway?'))
 
759
                      ' Do you want to commit anyway?'),
 
760
                parent=self)
697
761
            if response == gtk.RESPONSE_YES:
698
762
                rev_id = self._wt.commit(message,
699
763
                               allow_pointless=True,
702
766
                               specific_files=specific_files,
703
767
                               revprops=revprops)
704
768
        self.committed_revision_id = rev_id
 
769
        # destroy old comments if any
 
770
        if can_save_commit_messages:
 
771
            SavedCommitMessagesManager().save(self._wt, self._wt.branch)
705
772
        self.response(gtk.RESPONSE_OK)
706
773
 
707
774
    def _get_global_commit_message(self):
708
775
        buf = self._global_message_text_view.get_buffer()
709
776
        start, end = buf.get_bounds()
710
 
        return buf.get_text(start, end).decode('utf-8')
 
777
        text = buf.get_text(start, end)
 
778
        return _sanitize_and_decode_message(text)
711
779
 
712
780
    def _set_global_commit_message(self, message):
713
781
        """Just a helper for the test suite."""