/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: Curtis Hovey
  • Date: 2011-07-31 15:52:43 UTC
  • mto: This revision was merged to the branch mainline in revision 741.
  • Revision ID: sinzui.is@verizon.net-20110731155243-ln8istmxbryhb4pz
Mechanical changes made by pygi.convert.sh.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
import re
18
18
 
19
 
from gi.repository import Gdk
 
19
try:
 
20
    import pygtk
 
21
    pygtk.require("2.0")
 
22
except:
 
23
    pass
 
24
 
20
25
from gi.repository import Gtk
21
26
from gi.repository import GObject
22
27
from gi.repository import Pango
23
28
 
24
 
try:
25
 
    from bzrlib import bencode
26
 
except ImportError:
27
 
    from bzrlib.util import bencode
28
 
 
29
29
from bzrlib import (
30
30
    errors,
 
31
    osutils,
31
32
    trace,
32
33
    )
 
34
try:
 
35
    from bzrlib import bencode
 
36
except ImportError:
 
37
    from bzrlib.util import bencode
 
38
 
33
39
from bzrlib.plugins.gtk.dialog import question_dialog
34
40
from bzrlib.plugins.gtk.errors import show_bzr_error
35
41
from bzrlib.plugins.gtk.i18n import _i18n
36
 
from bzrlib.plugins.gtk.commitmsgs import SavedCommitMessagesManager
37
42
 
38
43
try:
39
44
    import dbus
108
113
    """Implementation of Commit."""
109
114
 
110
115
    def __init__(self, wt, selected=None, parent=None):
111
 
        super(CommitDialog, self).__init__(
112
 
            title="Commit to %s" % wt.basedir, parent=parent, flags=0)
 
116
        GObject.GObject.__init__(self, title="Commit to %s" % wt.basedir,
 
117
                            parent=parent, flags=0,)
113
118
        self.connect('delete-event', self._on_delete_window)
114
119
        self._question_dialog = question_dialog
115
120
 
122
127
        self._enable_per_file_commits = True
123
128
        self._commit_all_changes = True
124
129
        self.committed_revision_id = None # Nothing has been committed yet
125
 
        self._saved_commit_messages_manager = SavedCommitMessagesManager(
126
 
            self._wt, self._wt.branch)
 
130
        self._saved_commit_messages_manager = SavedCommitMessagesManager(self._wt, self._wt.branch)
127
131
 
128
132
        self.setup_params()
129
133
        self.construct()
228
232
        # This sets the cursor, which causes the expander to close, which
229
233
        # causes the _file_message_text_view to never get realized. So we have
230
234
        # to give it a little kick, or it warns when we try to grab the focus
231
 
        self._treeview_files.set_cursor(initial_cursor, None, False)
 
235
        self._treeview_files.set_cursor(initial_cursor)
232
236
 
233
237
        def _realize_file_message_tree_view(*args):
234
238
            self._file_message_text_view.realize()
288
292
        self._construct_right_pane()
289
293
        self._construct_action_pane()
290
294
 
291
 
        self.get_content_area().pack_start(self._hpane, True, True, 0)
 
295
        self.vbox.pack_start(self._hpane, True, True, 0)
292
296
        self._hpane.show()
293
297
        self.set_focus(self._global_message_text_view)
294
298
 
314
318
 
315
319
    def _construct_accelerators(self):
316
320
        group = Gtk.AccelGroup()
317
 
        group.connect(Gdk.keyval_from_name('N'),
318
 
                      Gdk.ModifierType.CONTROL_MASK, 0, self._on_accel_next)
 
321
        group.connect_group(Gdk.keyval_from_name('N'),
 
322
                            Gdk.EventMask.CONTROL_MASK, 0, self._on_accel_next)
319
323
        self.add_accel_group(group)
320
324
 
321
325
        # ignore the escape key (avoid closing the window)
328
332
 
329
333
        self._check_local = Gtk.CheckButton(_i18n("_Only commit locally"),
330
334
                                            use_underline=True)
331
 
        self._left_pane_box.pack_end(self._check_local, False, False, 0)
 
335
        self._left_pane_box.pack_end(self._check_local, False, False)
332
336
        self._check_local.set_active(False)
333
337
 
334
338
        self._hpane.pack1(self._left_pane_box, resize=False, shrink=False)
356
360
        self._button_cancel = Gtk.Button(stock=Gtk.STOCK_CANCEL)
357
361
        self._button_cancel.connect('clicked', self._on_cancel_clicked)
358
362
        self._button_cancel.show()
359
 
        self.get_action_area().pack_end(
360
 
            self._button_cancel, True, True, 0)
 
363
        self.action_area.pack_end(self._button_cancel)
361
364
        self._button_commit = Gtk.Button(_i18n("Comm_it"), use_underline=True)
362
365
        self._button_commit.connect('clicked', self._on_commit_clicked)
363
366
        self._button_commit.set_can_default(True)
364
367
        self._button_commit.show()
365
 
        self.get_action_area().pack_end(
366
 
            self._button_commit, True, True, 0)
 
368
        self.action_area.pack_end(self._button_commit)
367
369
        self._button_commit.grab_default()
368
370
 
369
371
    def _add_to_right_table(self, widget, weight, expanding=False):
389
391
        # file_label.show()
390
392
        self._files_box.pack_start(file_label, False, True, 0)
391
393
 
392
 
        self._commit_all_files_radio = Gtk.RadioButton.new_with_label(
 
394
        self._commit_all_files_radio = Gtk.RadioButton(
393
395
            None, _i18n("Commit all changes"))
394
396
        self._files_box.pack_start(self._commit_all_files_radio, False, True, 0)
395
397
        self._commit_all_files_radio.show()
396
398
        self._commit_all_files_radio.connect('toggled',
397
399
            self._toggle_commit_selection)
398
 
        self._commit_selected_radio = Gtk.RadioButton.new_with_label_from_widget(
 
400
        self._commit_selected_radio = Gtk.RadioButton(
399
401
            self._commit_all_files_radio, _i18n("Only commit selected changes"))
400
402
        self._files_box.pack_start(self._commit_selected_radio, False, True, 0)
401
403
        self._commit_selected_radio.show()
413
415
        scroller.add(self._treeview_files)
414
416
        scroller.set_shadow_type(Gtk.ShadowType.IN)
415
417
        scroller.show()
416
 
        self._files_box.pack_start(scroller, True, True, 0)
 
418
        self._files_box.pack_start(scroller,
 
419
                                   expand=True, fill=True)
417
420
        self._files_box.show()
418
421
        self._left_pane_box.pack_start(self._files_box, True, True, 0)
419
422
 
464
467
                checked_col.set_visible(False)
465
468
            else:
466
469
                checked_col.set_visible(True)
467
 
            renderer = checked_col.get_cells()[0]
 
470
            renderer = checked_col.get_cell_renderers()[0]
468
471
            renderer.set_property('activatable', not all_files)
469
472
 
470
473
    def _construct_pending_list(self):
476
479
        pending_message = Gtk.Label()
477
480
        pending_message.set_markup(
478
481
            _i18n('<i>* Cannot select specific files when merging</i>'))
479
 
        self._pending_box.pack_start(pending_message, False, True, 5)
 
482
        self._pending_box.pack_start(pending_message, expand=False, padding=5)
480
483
        pending_message.show()
481
484
 
482
485
        pending_label = Gtk.Label(label=_i18n('Pending Revisions'))
483
 
        self._pending_box.pack_start(pending_label, False, True, 0)
 
486
        self._pending_box.pack_start(pending_label, expand=False, padding=0)
484
487
        pending_label.show()
485
488
 
486
489
        scroller = Gtk.ScrolledWindow()
489
492
        scroller.add(self._treeview_pending)
490
493
        scroller.set_shadow_type(Gtk.ShadowType.IN)
491
494
        scroller.show()
492
 
        self._pending_box.pack_start(scroller, True, True, 5)
 
495
        self._pending_box.pack_start(scroller,
 
496
                                     expand=True, fill=True, padding=5)
493
497
        self._treeview_pending.show()
494
498
        self._left_pane_box.pack_start(self._pending_box, True, True, 0)
495
499
 
538
542
        self._file_message_text_view.set_accepts_tab(False)
539
543
        self._file_message_text_view.show()
540
544
 
541
 
        self._file_message_expander = Gtk.Expander(
542
 
            label=_i18n('File commit message'))
 
545
        self._file_message_expander = Gtk.Expander(_i18n('File commit message'))
543
546
        self._file_message_expander.set_expanded(True)
544
547
        self._file_message_expander.add(scroller)
545
548
        self._add_to_right_table(self._file_message_expander, 1, False)
596
599
            # We have either made it to the end of the list, or nothing was
597
600
            # selected. Either way, select All Files, and jump to the global
598
601
            # commit message.
599
 
            self._treeview_files.set_cursor(
600
 
                Gtk.TreePath(path=0), None, False)
 
602
            self._treeview_files.set_cursor((0,))
601
603
            self._global_message_text_view.grab_focus()
602
604
        else:
603
605
            # Set the cursor to this entry, and jump to the per-file commit
604
606
            # message
605
 
            self._treeview_files.set_cursor(model.get_path(next), None, False)
 
607
            self._treeview_files.set_cursor(model.get_path(next))
606
608
            self._file_message_text_view.grab_focus()
607
609
 
608
610
    def _save_current_file_message(self):
610
612
            return # Nothing to save
611
613
        text_buffer = self._file_message_text_view.get_buffer()
612
614
        cur_text = text_buffer.get_text(text_buffer.get_start_iter(),
613
 
                                        text_buffer.get_end_iter(), True)
 
615
                                        text_buffer.get_end_iter())
614
616
        last_selected = self._files_store.get_iter(self._last_selected_file)
615
617
        self._files_store.set_value(last_selected, 5, cur_text)
616
618
 
764
766
    def _get_global_commit_message(self):
765
767
        buf = self._global_message_text_view.get_buffer()
766
768
        start, end = buf.get_bounds()
767
 
        text = buf.get_text(start, end, True)
 
769
        text = buf.get_text(start, end)
768
770
        return _sanitize_and_decode_message(text)
769
771
 
770
772
    def _set_global_commit_message(self, message):
793
795
        rev_dict['revision_id'] = rev.revision_id
794
796
        return rev_dict
795
797
 
 
798
 
 
799
class SavedCommitMessagesManager:
 
800
    """Save glogal and per-file commit messages.
 
801
 
 
802
    Saves global commit message and utf-8 file_id->message dictionary
 
803
    of per-file commit messages on disk. Re-reads them later for re-using.
 
804
    """
 
805
 
 
806
    def __init__(self, tree=None, branch=None):
 
807
        """If branch is None, builds empty messages, otherwise reads them
 
808
        from branch's disk storage. 'tree' argument is for the future."""
 
809
        if branch is None:
 
810
            self.global_message = u''
 
811
            self.file_messages = {}
 
812
        else:
 
813
            config = branch.get_config()
 
814
            self.global_message = config.get_user_option(
 
815
                'gtk_global_commit_message')
 
816
            if self.global_message is None:
 
817
                self.global_message = u''
 
818
            file_messages = config.get_user_option('gtk_file_commit_messages')
 
819
            if file_messages: # unicode and B-encoded:
 
820
                self.file_messages = bencode.bdecode(
 
821
                    file_messages.encode('UTF-8'))
 
822
            else:
 
823
                self.file_messages = {}
 
824
 
 
825
    def get(self):
 
826
        return self.global_message, self.file_messages
 
827
 
 
828
    def is_not_empty(self):
 
829
        return bool(self.global_message or self.file_messages)
 
830
 
 
831
    def insert(self, global_message, file_info):
 
832
        """Formats per-file commit messages (list of dictionaries, one per file)
 
833
        into one utf-8 file_id->message dictionary and merges this with
 
834
        previously existing dictionary. Merges global commit message too."""
 
835
        file_messages = {}
 
836
        for fi in file_info:
 
837
            file_message = fi['message']
 
838
            if file_message:
 
839
                file_messages[fi['file_id']] = file_message # utf-8 strings
 
840
        for k,v in file_messages.iteritems():
 
841
            try:
 
842
                self.file_messages[k] = v + '\n******\n' + self.file_messages[k]
 
843
            except KeyError:
 
844
                self.file_messages[k] = v
 
845
        if self.global_message:
 
846
            self.global_message = global_message + '\n******\n' \
 
847
                + self.global_message
 
848
        else:
 
849
            self.global_message = global_message
 
850
 
 
851
    def save(self, tree, branch):
 
852
        # We store in branch's config, which can be a problem if two gcommit
 
853
        # are done in two checkouts of one single branch (comments overwrite
 
854
        # each other). Ideally should be in working tree. But uncommit does
 
855
        # not always have a working tree, though it always has a branch.
 
856
        # 'tree' argument is for the future
 
857
        config = branch.get_config()
 
858
        # should it be named "gtk_" or some more neutral name ("gui_" ?) to
 
859
        # be compatible with qbzr in the future?
 
860
        config.set_user_option('gtk_global_commit_message', self.global_message)
 
861
        # bencode() does not know unicode objects but set_user_option()
 
862
        # requires one:
 
863
        config.set_user_option(
 
864
            'gtk_file_commit_messages',
 
865
            bencode.bencode(self.file_messages).decode('UTF-8'))
 
866
 
 
867
 
 
868
def save_commit_messages(local, master, old_revno, old_revid,
 
869
                         new_revno, new_revid):
 
870
    b = local
 
871
    if b is None:
 
872
        b = master
 
873
    mgr = SavedCommitMessagesManager(None, b)
 
874
    revid_iterator = b.repository.iter_reverse_revision_history(old_revid)
 
875
    cur_revno = old_revno
 
876
    new_revision_id = old_revid
 
877
    graph = b.repository.get_graph()
 
878
    for rev_id in revid_iterator:
 
879
        if cur_revno == new_revno:
 
880
            break
 
881
        cur_revno -= 1
 
882
        rev = b.repository.get_revision(rev_id)
 
883
        file_info = rev.properties.get('file-info', None)
 
884
        if file_info is None:
 
885
            file_info = {}
 
886
        else:
 
887
            file_info = bencode.bdecode(file_info.encode('UTF-8'))
 
888
        global_message = osutils.safe_unicode(rev.message)
 
889
        # Concatenate comment of the uncommitted revision
 
890
        mgr.insert(global_message, file_info)
 
891
 
 
892
        parents = graph.get_parent_map([rev_id]).get(rev_id, None)
 
893
        if not parents:
 
894
            continue
 
895
    mgr.save(None, b)