/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-08-29 14:12:30 UTC
  • mto: This revision was merged to the branch mainline in revision 741.
  • Revision ID: sinzui.is@verizon.net-20110829141230-6nv7h0oxjrojb1y1
Updated hacking doc.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
from gi.repository import Pango
23
23
 
24
24
from bzrlib import (
25
 
    bencode,
26
25
    errors,
27
26
    osutils,
28
27
    trace,
29
28
    )
 
29
try:
 
30
    from bzrlib import bencode
 
31
except ImportError:
 
32
    from bzrlib.util import bencode
 
33
 
30
34
from bzrlib.plugins.gtk.dialog import question_dialog
31
35
from bzrlib.plugins.gtk.errors import show_bzr_error
32
36
from bzrlib.plugins.gtk.i18n import _i18n
33
 
from bzrlib.plugins.gtk.commitmsgs import SavedCommitMessagesManager
34
37
 
35
38
try:
36
39
    import dbus
59
62
    last_revision = parents[0]
60
63
 
61
64
    if last_revision is not None:
62
 
        graph = branch.repository.get_graph()
63
 
        ignore = set([r for r,ps in graph.iter_ancestry([last_revision])])
 
65
        try:
 
66
            ignore = set(branch.repository.get_ancestry(last_revision,
 
67
                                                        topo_sorted=False))
 
68
        except errors.NoSuchRevision:
 
69
            # the last revision is a ghost : assume everything is new
 
70
            # except for it
 
71
            ignore = set([None, last_revision])
64
72
    else:
65
 
        ignore = set([])
 
73
        ignore = set([None])
66
74
 
67
75
    pm = []
68
76
    for merge in pending:
93
101
def _sanitize_and_decode_message(utf8_message):
94
102
    """Turn a utf-8 message into a sanitized Unicode message."""
95
103
    fixed_newline = _newline_variants_re.sub('\n', utf8_message)
96
 
    return osutils.safe_unicode(fixed_newline)
 
104
    return fixed_newline.decode('utf-8')
97
105
 
98
106
 
99
107
class CommitDialog(Gtk.Dialog):
100
108
    """Implementation of Commit."""
101
109
 
102
110
    def __init__(self, wt, selected=None, parent=None):
103
 
        super(CommitDialog, self).__init__(
104
 
            title="Commit to %s" % wt.basedir, parent=parent, flags=0)
 
111
        Gtk.Dialog.__init__(self, title="Commit to %s" % wt.basedir,
 
112
                            parent=parent, flags=0,)
105
113
        self.connect('delete-event', self._on_delete_window)
106
114
        self._question_dialog = question_dialog
107
115
 
114
122
        self._enable_per_file_commits = True
115
123
        self._commit_all_changes = True
116
124
        self.committed_revision_id = None # Nothing has been committed yet
117
 
        self._last_selected_file = None
118
125
        self._saved_commit_messages_manager = SavedCommitMessagesManager(
119
126
            self._wt, self._wt.branch)
120
127
 
126
133
        """Setup the member variables for state."""
127
134
        self._basis_tree = self._wt.basis_tree()
128
135
        self._delta = None
129
 
        self._wt.lock_read()
130
 
        try:
131
 
            self._pending = pending_revisions(self._wt)
132
 
        finally:
133
 
            self._wt.unlock()
 
136
        self._pending = pending_revisions(self._wt)
134
137
 
135
138
        self._is_checkout = (self._wt.branch.get_bound_location() is not None)
136
139
 
188
191
 
189
192
        all_enabled = (self._selected is None)
190
193
        # The first entry is always the 'whole tree'
191
 
        all_iter = store.append(["", "", all_enabled, 'All Files', '', ''])
 
194
        all_iter = store.append([None, None, all_enabled, 'All Files', '', ''])
192
195
        initial_cursor = store.get_path(all_iter)
193
196
        # should we pass specific_files?
194
197
        self._wt.lock_read()
445
448
                                     self._on_treeview_files_cursor_changed)
446
449
 
447
450
    def _toggle_commit(self, cell, path, model):
448
 
        if model[path][0] == "": # No file_id means 'All Files'
 
451
        if model[path][0] is None: # No file_id means 'All Files'
449
452
            new_val = not model[path][2]
450
453
            for node in model:
451
454
                node[2] = new_val
568
571
 
569
572
    def _on_treeview_files_cursor_changed(self, treeview):
570
573
        treeselection = treeview.get_selection()
571
 
        if treeselection is None:
572
 
            # The treeview was probably destroyed as the dialog closes.
573
 
            return
574
574
        (model, selection) = treeselection.get_selected()
575
575
 
576
576
        if selection is not None:
577
577
            path, display_path = model.get(selection, 1, 3)
578
578
            self._diff_label.set_text(_i18n('Diff for ') + display_path)
579
 
            if path == "":
 
579
            if path is None:
580
580
                self._diff_view.show_diff(None)
581
581
            else:
582
 
                self._diff_view.show_diff([osutils.safe_unicode(path)])
 
582
                self._diff_view.show_diff([path.decode('UTF-8')])
583
583
            self._update_per_file_info(selection)
584
584
 
585
585
    def _on_accel_next(self, accel_group, window, keyval, modifier):
597
597
            # selected. Either way, select All Files, and jump to the global
598
598
            # commit message.
599
599
            self._treeview_files.set_cursor(
600
 
                Gtk.TreePath(path=0), "", False)
 
600
                Gtk.TreePath(path=0), None, False)
601
601
            self._global_message_text_view.grab_focus()
602
602
        else:
603
603
            # Set the cursor to this entry, and jump to the per-file commit
622
622
        self._save_current_file_message()
623
623
        text_buffer = self._file_message_text_view.get_buffer()
624
624
        file_id, display_path, message = self._files_store.get(selection, 0, 3, 5)
625
 
        if file_id == "": # Whole tree
 
625
        if file_id is None: # Whole tree
626
626
            self._file_message_expander.set_label(_i18n('File commit message'))
627
627
            self._file_message_expander.set_expanded(False)
628
628
            self._file_message_expander.set_sensitive(False)
645
645
        files = []
646
646
        records = iter(self._files_store)
647
647
        rec = records.next() # Skip the All Files record
648
 
        assert rec[0] == "", "Are we skipping the wrong record?"
 
648
        assert rec[0] is None, "Are we skipping the wrong record?"
649
649
 
650
650
        file_info = []
651
651
        for record in records:
652
652
            if self._commit_all_changes or record[2]:# [2] checkbox
653
 
                file_id = osutils.safe_utf8(record[0]) # [0] file_id
654
 
                path = osutils.safe_utf8(record[1])    # [1] real path
 
653
                file_id = record[0] # [0] file_id
 
654
                path = record[1]    # [1] real path
655
655
                # [5] commit message
656
656
                file_message = _sanitize_and_decode_message(record[5])
657
657
                files.append(path.decode('UTF-8'))
793
793
        rev_dict['revision_id'] = rev.revision_id
794
794
        return rev_dict
795
795
 
 
796
 
 
797
class SavedCommitMessagesManager:
 
798
    """Save glogal and per-file commit messages.
 
799
 
 
800
    Saves global commit message and utf-8 file_id->message dictionary
 
801
    of per-file commit messages on disk. Re-reads them later for re-using.
 
802
    """
 
803
 
 
804
    def __init__(self, tree=None, branch=None):
 
805
        """If branch is None, builds empty messages, otherwise reads them
 
806
        from branch's disk storage. 'tree' argument is for the future."""
 
807
        if branch is None:
 
808
            self.global_message = u''
 
809
            self.file_messages = {}
 
810
        else:
 
811
            config = branch.get_config()
 
812
            self.global_message = config.get_user_option(
 
813
                'gtk_global_commit_message')
 
814
            if self.global_message is None:
 
815
                self.global_message = u''
 
816
            file_messages = config.get_user_option('gtk_file_commit_messages')
 
817
            if file_messages: # unicode and B-encoded:
 
818
                self.file_messages = bencode.bdecode(
 
819
                    file_messages.encode('UTF-8'))
 
820
            else:
 
821
                self.file_messages = {}
 
822
 
 
823
    def get(self):
 
824
        return self.global_message, self.file_messages
 
825
 
 
826
    def is_not_empty(self):
 
827
        return bool(self.global_message or self.file_messages)
 
828
 
 
829
    def insert(self, global_message, file_info):
 
830
        """Formats per-file commit messages (list of dictionaries, one per file)
 
831
        into one utf-8 file_id->message dictionary and merges this with
 
832
        previously existing dictionary. Merges global commit message too."""
 
833
        file_messages = {}
 
834
        for fi in file_info:
 
835
            file_message = fi['message']
 
836
            if file_message:
 
837
                file_messages[fi['file_id']] = file_message # utf-8 strings
 
838
        for k,v in file_messages.iteritems():
 
839
            try:
 
840
                self.file_messages[k] = v + '\n******\n' + self.file_messages[k]
 
841
            except KeyError:
 
842
                self.file_messages[k] = v
 
843
        if self.global_message:
 
844
            self.global_message = global_message + '\n******\n' \
 
845
                + self.global_message
 
846
        else:
 
847
            self.global_message = global_message
 
848
 
 
849
    def save(self, tree, branch):
 
850
        # We store in branch's config, which can be a problem if two gcommit
 
851
        # are done in two checkouts of one single branch (comments overwrite
 
852
        # each other). Ideally should be in working tree. But uncommit does
 
853
        # not always have a working tree, though it always has a branch.
 
854
        # 'tree' argument is for the future
 
855
        config = branch.get_config()
 
856
        # should it be named "gtk_" or some more neutral name ("gui_" ?) to
 
857
        # be compatible with qbzr in the future?
 
858
        config.set_user_option('gtk_global_commit_message', self.global_message)
 
859
        # bencode() does not know unicode objects but set_user_option()
 
860
        # requires one:
 
861
        config.set_user_option(
 
862
            'gtk_file_commit_messages',
 
863
            bencode.bencode(self.file_messages).decode('UTF-8'))
 
864
 
 
865
 
 
866
def save_commit_messages(local, master, old_revno, old_revid,
 
867
                         new_revno, new_revid):
 
868
    b = local
 
869
    if b is None:
 
870
        b = master
 
871
    mgr = SavedCommitMessagesManager(None, b)
 
872
    revid_iterator = b.repository.iter_reverse_revision_history(old_revid)
 
873
    cur_revno = old_revno
 
874
    new_revision_id = old_revid
 
875
    graph = b.repository.get_graph()
 
876
    for rev_id in revid_iterator:
 
877
        if cur_revno == new_revno:
 
878
            break
 
879
        cur_revno -= 1
 
880
        rev = b.repository.get_revision(rev_id)
 
881
        file_info = rev.properties.get('file-info', None)
 
882
        if file_info is None:
 
883
            file_info = {}
 
884
        else:
 
885
            file_info = bencode.bdecode(file_info.encode('UTF-8'))
 
886
        global_message = osutils.safe_unicode(rev.message)
 
887
        # Concatenate comment of the uncommitted revision
 
888
        mgr.insert(global_message, file_info)
 
889
 
 
890
        parents = graph.get_parent_map([rev_id]).get(rev_id, None)
 
891
        if not parents:
 
892
            continue
 
893
    mgr.save(None, b)