/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz
0.8.19 by Szilveszter Farkas (Phanatic)
2006-07-21 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
1
# Copyright (C) 2006 by Szilveszter Farkas (Phanatic) <szilveszter.farkas@gmail.com>
0.8.46 by Szilveszter Farkas (Phanatic)
Modified OliveDialog class interface; huge cleanups.
2
#
0.8.19 by Szilveszter Farkas (Phanatic)
2006-07-21 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
0.8.46 by Szilveszter Farkas (Phanatic)
Modified OliveDialog class interface; huge cleanups.
7
#
0.8.19 by Szilveszter Farkas (Phanatic)
2006-07-21 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
0.8.46 by Szilveszter Farkas (Phanatic)
Modified OliveDialog class interface; huge cleanups.
12
#
0.8.19 by Szilveszter Farkas (Phanatic)
2006-07-21 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
576.1.1 by Jasper Groenewegen
Add parent setting to dialogs and implement in gcommit
17
import os.path
18
import re
19
0.8.19 by Szilveszter Farkas (Phanatic)
2006-07-21 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
20
try:
21
    import pygtk
22
    pygtk.require("2.0")
23
except:
24
    pass
0.8.98 by Szilveszter Farkas (Phanatic)
Loads of fixes. Pyflakes cleanup.
25
0.13.11 by Jelmer Vernooij
Bunch of small fixes, cleanups and simplifications.
26
import gtk
27
import gobject
28
import pango
0.8.19 by Szilveszter Farkas (Phanatic)
2006-07-21 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
29
635.2.12 by Vincent Ladeuil
Implement commit message saving without modifying bzrlib.
30
from bzrlib import (
31
    branch,
32
    errors,
33
    osutils,
34
    trace,
35
    )
278.1.27 by John Arbash Meinel
Add the ability to commit just specific files.
36
from bzrlib.util import bencode
0.8.20 by Szilveszter Farkas (Phanatic)
2006-07-24 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
37
475.1.2 by Vincent Ladeuil
Fix bug #187283 fix replacing _() by _i18n().
38
from bzrlib.plugins.gtk import _i18n
576.1.1 by Jasper Groenewegen
Add parent setting to dialogs and implement in gcommit
39
from bzrlib.plugins.gtk.dialog import question_dialog
40
from bzrlib.plugins.gtk.errors import show_bzr_error
93.1.6 by Alexander Belchenko
detecting name of glade file doing in separate module (olive.gladefile)
41
158 by Jelmer Vernooij
If available, use NetworkManager to find out whether a commit should be local or not.
42
try:
43
    import dbus
44
    import dbus.glib
180 by Jelmer Vernooij
Don't obtain handle to network manager until it's actually needed.
45
    have_dbus = True
158 by Jelmer Vernooij
If available, use NetworkManager to find out whether a commit should be local or not.
46
except ImportError:
180 by Jelmer Vernooij
Don't obtain handle to network manager until it's actually needed.
47
    have_dbus = False
158 by Jelmer Vernooij
If available, use NetworkManager to find out whether a commit should be local or not.
48
278.1.4 by John Arbash Meinel
Just playing around.
49
278.1.5 by John Arbash Meinel
Starting to flesh out the dialog with actual windows.
50
def pending_revisions(wt):
51
    """Return a list of pending merges or None if there are none of them.
52
53
    Arguably this should be a core function, and
54
    ``bzrlib.status.show_pending_merges`` should be built on top of it.
55
56
    :return: [(rev, [children])]
57
    """
58
    parents = wt.get_parent_ids()
59
    if len(parents) < 2:
60
        return None
61
62
    # The basic pending merge algorithm uses the same algorithm as
63
    # bzrlib.status.show_pending_merges
64
    pending = parents[1:]
65
    branch = wt.branch
66
    last_revision = parents[0]
67
68
    if last_revision is not None:
69
        try:
70
            ignore = set(branch.repository.get_ancestry(last_revision,
71
                                                        topo_sorted=False))
72
        except errors.NoSuchRevision:
73
            # the last revision is a ghost : assume everything is new
74
            # except for it
75
            ignore = set([None, last_revision])
76
    else:
77
        ignore = set([None])
78
79
    pm = []
80
    for merge in pending:
81
        ignore.add(merge)
82
        try:
83
            rev = branch.repository.get_revision(merge)
84
            children = []
85
            pm.append((rev, children))
86
87
            # This does need to be topo sorted, so we search backwards
88
            inner_merges = branch.repository.get_ancestry(merge)
89
            assert inner_merges[0] is None
90
            inner_merges.pop(0)
91
            for mmerge in reversed(inner_merges):
92
                if mmerge in ignore:
93
                    continue
94
                rev = branch.repository.get_revision(mmerge)
95
                children.append(rev)
96
97
                ignore.add(mmerge)
98
        except errors.NoSuchRevision:
99
            print "DEBUG: NoSuchRevision:", merge
100
101
    return pm
102
103
622.1.1 by John Arbash Meinel
Ensure that per-file commit messages and global commit messages get sanitized.
104
_newline_variants_re = re.compile(r'\r\n?')
105
def _sanitize_and_decode_message(utf8_message):
106
    """Turn a utf-8 message into a sanitized Unicode message."""
107
    fixed_newline = _newline_variants_re.sub('\n', utf8_message)
108
    return fixed_newline.decode('utf-8')
109
110
135 by Jelmer Vernooij
Throw out the old CommitDialog code and use the new code instead, also for 'gcommit'.
111
class CommitDialog(gtk.Dialog):
278.1.5 by John Arbash Meinel
Starting to flesh out the dialog with actual windows.
112
    """Implementation of Commit."""
113
114
    def __init__(self, wt, selected=None, parent=None):
602 by Jelmer Vernooij
Show working tree in gcommit dialog window.
115
        gtk.Dialog.__init__(self, title="Commit to %s" % wt.basedir,
635.2.7 by Vincent Ladeuil
Land Anne Mohsen's patch for bug 215674.
116
                            parent=parent, flags=0,)
117
        self.connect('delete-event', self._on_delete_window)
606 by Vincent Ladeuil
Fix gtk dialogs popping up and asking for input during selftest.
118
        self._question_dialog = question_dialog
635.2.7 by Vincent Ladeuil
Land Anne Mohsen's patch for bug 215674.
119
625.7.1 by Szilveszter Farkas
Added a GTK type hint to allow the Commit window to maximize.
120
        self.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_NORMAL)
278.1.24 by John Arbash Meinel
Actually show the commit button.
121
278.1.16 by John Arbash Meinel
Implement the file changes list on top of _iter_changes rather than
122
        self._wt = wt
278.1.34 by John Arbash Meinel
Cleanup, we are still ignoring the 'selected' property, and the 'wtpath'
123
        # TODO: Do something with this value, it is used by Olive
124
        #       It used to set all changes but this one to False
278.1.16 by John Arbash Meinel
Implement the file changes list on top of _iter_changes rather than
125
        self._selected = selected
278.1.33 by John Arbash Meinel
Only enable the per-file dialog if 'per_file_commits' is enabled in the config.
126
        self._enable_per_file_commits = True
278.1.43 by John Arbash Meinel
Finish connecting the 'Commit all changes' radio buttons.
127
        self._commit_all_changes = True
278.1.23 by John Arbash Meinel
Beginning to support actual commit.
128
        self.committed_revision_id = None # Nothing has been committed yet
635.2.12 by Vincent Ladeuil
Implement commit message saving without modifying bzrlib.
129
        self._saved_commit_messages_manager = SavedCommitMessagesManager(self._wt, self._wt.branch)
278.1.5 by John Arbash Meinel
Starting to flesh out the dialog with actual windows.
130
278.1.16 by John Arbash Meinel
Implement the file changes list on top of _iter_changes rather than
131
        self.setup_params()
132
        self.construct()
133
        self.fill_in_data()
278.1.5 by John Arbash Meinel
Starting to flesh out the dialog with actual windows.
134
135
    def setup_params(self):
136
        """Setup the member variables for state."""
137
        self._basis_tree = self._wt.basis_tree()
278.1.12 by John Arbash Meinel
Delay computing the delta, and clean up some of the diff view names.
138
        self._delta = None
278.1.5 by John Arbash Meinel
Starting to flesh out the dialog with actual windows.
139
        self._pending = pending_revisions(self._wt)
140
141
        self._is_checkout = (self._wt.branch.get_bound_location() is not None)
142
278.1.14 by John Arbash Meinel
Tests that we fill out the pending list correctly.
143
    def fill_in_data(self):
144
        # Now that we are built, handle changes to the view based on the state
145
        self._fill_in_pending()
278.1.20 by John Arbash Meinel
We always select the All Files record in the files view,
146
        self._fill_in_diff()
278.1.15 by John Arbash Meinel
Hook up the list of modified files.
147
        self._fill_in_files()
278.1.25 by John Arbash Meinel
Add the 'Only Commit Locally' checkbox, we may want to put it elsewhere, though.
148
        self._fill_in_checkout()
278.1.33 by John Arbash Meinel
Only enable the per-file dialog if 'per_file_commits' is enabled in the config.
149
        self._fill_in_per_file_info()
278.1.14 by John Arbash Meinel
Tests that we fill out the pending list correctly.
150
151
    def _fill_in_pending(self):
152
        if not self._pending:
153
            self._pending_box.hide()
154
            return
155
156
        # TODO: We'd really prefer this to be a nested list
157
        for rev, children in self._pending:
158
            rev_info = self._rev_to_pending_info(rev)
278.1.15 by John Arbash Meinel
Hook up the list of modified files.
159
            self._pending_store.append([
278.1.14 by John Arbash Meinel
Tests that we fill out the pending list correctly.
160
                rev_info['revision_id'],
161
                rev_info['date'],
162
                rev_info['committer'],
163
                rev_info['summary'],
164
                ])
165
            for child in children:
166
                rev_info = self._rev_to_pending_info(child)
278.1.15 by John Arbash Meinel
Hook up the list of modified files.
167
                self._pending_store.append([
278.1.14 by John Arbash Meinel
Tests that we fill out the pending list correctly.
168
                    rev_info['revision_id'],
169
                    rev_info['date'],
170
                    rev_info['committer'],
171
                    rev_info['summary'],
172
                    ])
173
        self._pending_box.show()
174
278.1.15 by John Arbash Meinel
Hook up the list of modified files.
175
    def _fill_in_files(self):
278.1.29 by John Arbash Meinel
Start testing with Unicode data.
176
        # We should really use add a progress bar of some kind.
278.1.15 by John Arbash Meinel
Hook up the list of modified files.
177
        # While we fill in the view, hide the store
178
        store = self._files_store
179
        self._treeview_files.set_model(None)
180
475.1.2 by Vincent Ladeuil
Fix bug #187283 fix replacing _() by _i18n().
181
        added = _i18n('added')
182
        removed = _i18n('removed')
183
        renamed = _i18n('renamed')
184
        renamed_and_modified = _i18n('renamed and modified')
185
        modified = _i18n('modified')
186
        kind_changed = _i18n('kind changed')
278.1.15 by John Arbash Meinel
Hook up the list of modified files.
187
278.1.16 by John Arbash Meinel
Implement the file changes list on top of _iter_changes rather than
188
        # The store holds:
278.1.21 by John Arbash Meinel
Start tracking the per-file commit messages.
189
        # [file_id, real path, checkbox, display path, changes type, message]
450 by Aaron Bentley
Update to use new Tree.iter_changes
190
        # iter_changes returns:
278.1.16 by John Arbash Meinel
Implement the file changes list on top of _iter_changes rather than
191
        # (file_id, (path_in_source, path_in_target),
192
        #  changed_content, versioned, parent, name, kind,
193
        #  executable)
194
278.1.35 by John Arbash Meinel
Make use of the 'selected' parameter to CommitDialog.
195
        all_enabled = (self._selected is None)
278.1.20 by John Arbash Meinel
We always select the All Files record in the files view,
196
        # The first entry is always the 'whole tree'
278.1.35 by John Arbash Meinel
Make use of the 'selected' parameter to CommitDialog.
197
        all_iter = store.append([None, None, all_enabled, 'All Files', '', ''])
198
        initial_cursor = store.get_path(all_iter)
278.1.16 by John Arbash Meinel
Implement the file changes list on top of _iter_changes rather than
199
        # should we pass specific_files?
200
        self._wt.lock_read()
201
        self._basis_tree.lock_read()
202
        try:
450 by Aaron Bentley
Update to use new Tree.iter_changes
203
            from diff import iter_changes_to_status
635.2.12 by Vincent Ladeuil
Implement commit message saving without modifying bzrlib.
204
            saved_file_messages = self._saved_commit_messages_manager.get()[1]
278.1.29 by John Arbash Meinel
Start testing with Unicode data.
205
            for (file_id, real_path, change_type, display_path
450 by Aaron Bentley
Update to use new Tree.iter_changes
206
                ) in iter_changes_to_status(self._basis_tree, self._wt):
278.1.35 by John Arbash Meinel
Make use of the 'selected' parameter to CommitDialog.
207
                if self._selected and real_path != self._selected:
208
                    enabled = False
209
                else:
210
                    enabled = True
635.2.7 by Vincent Ladeuil
Land Anne Mohsen's patch for bug 215674.
211
                try:
212
                    default_message = saved_file_messages[file_id]
213
                except KeyError:
214
                    default_message = ''
278.1.35 by John Arbash Meinel
Make use of the 'selected' parameter to CommitDialog.
215
                item_iter = store.append([
216
                    file_id,
217
                    real_path.encode('UTF-8'),
218
                    enabled,
219
                    display_path.encode('UTF-8'),
220
                    change_type,
635.2.7 by Vincent Ladeuil
Land Anne Mohsen's patch for bug 215674.
221
                    default_message, # Initial comment
278.1.35 by John Arbash Meinel
Make use of the 'selected' parameter to CommitDialog.
222
                    ])
223
                if self._selected and enabled:
224
                    initial_cursor = store.get_path(item_iter)
278.1.16 by John Arbash Meinel
Implement the file changes list on top of _iter_changes rather than
225
        finally:
226
            self._basis_tree.unlock()
227
            self._wt.unlock()
278.1.15 by John Arbash Meinel
Hook up the list of modified files.
228
229
        self._treeview_files.set_model(store)
278.1.21 by John Arbash Meinel
Start tracking the per-file commit messages.
230
        self._last_selected_file = None
278.1.32 by John Arbash Meinel
Add the Ctrl+n accelerator to jump through the commit messages.
231
        # This sets the cursor, which causes the expander to close, which
232
        # causes the _file_message_text_view to never get realized. So we have
233
        # to give it a little kick, or it warns when we try to grab the focus
278.1.35 by John Arbash Meinel
Make use of the 'selected' parameter to CommitDialog.
234
        self._treeview_files.set_cursor(initial_cursor)
278.1.15 by John Arbash Meinel
Hook up the list of modified files.
235
278.1.32 by John Arbash Meinel
Add the Ctrl+n accelerator to jump through the commit messages.
236
        def _realize_file_message_tree_view(*args):
237
            self._file_message_text_view.realize()
238
        self.connect_after('realize', _realize_file_message_tree_view)
239
278.1.17 by John Arbash Meinel
Add a * reference for why you can't change the commit selection.
240
    def _fill_in_diff(self):
241
        self._diff_view.set_trees(self._wt, self._basis_tree)
242
278.1.25 by John Arbash Meinel
Add the 'Only Commit Locally' checkbox, we may want to put it elsewhere, though.
243
    def _fill_in_checkout(self):
244
        if not self._is_checkout:
245
            self._check_local.hide()
246
            return
247
        if have_dbus:
248
            bus = dbus.SystemBus()
445 by Szilveszter Farkas (Phanatic)
Fix a traceback if NetworkManager is not available (#199513).
249
            try:
250
                proxy_obj = bus.get_object('org.freedesktop.NetworkManager',
251
                                           '/org/freedesktop/NetworkManager')
446 by Szilveszter Farkas (Phanatic)
Hot fix. Sorry dudes.
252
            except dbus.DBusException:
635.2.12 by Vincent Ladeuil
Implement commit message saving without modifying bzrlib.
253
                trace.mutter("networkmanager not available.")
445 by Szilveszter Farkas (Phanatic)
Fix a traceback if NetworkManager is not available (#199513).
254
                self._check_local.show()
255
                return
256
            
278.1.25 by John Arbash Meinel
Add the 'Only Commit Locally' checkbox, we may want to put it elsewhere, though.
257
            dbus_iface = dbus.Interface(proxy_obj,
258
                                        'org.freedesktop.NetworkManager')
259
            try:
260
                # 3 is the enum value for STATE_CONNECTED
261
                self._check_local.set_active(dbus_iface.state() != 3)
262
            except dbus.DBusException, e:
263
                # Silently drop errors. While DBus may be
264
                # available, NetworkManager doesn't necessarily have to be
635.2.12 by Vincent Ladeuil
Implement commit message saving without modifying bzrlib.
265
                trace.mutter("unable to get networkmanager state: %r" % e)
278.1.25 by John Arbash Meinel
Add the 'Only Commit Locally' checkbox, we may want to put it elsewhere, though.
266
        self._check_local.show()
267
278.1.33 by John Arbash Meinel
Only enable the per-file dialog if 'per_file_commits' is enabled in the config.
268
    def _fill_in_per_file_info(self):
269
        config = self._wt.branch.get_config()
270
        enable_per_file_commits = config.get_user_option('per_file_commits')
271
        if (enable_per_file_commits is None
272
            or enable_per_file_commits.lower()
273
                not in ('y', 'yes', 'on', 'enable', '1', 't', 'true')):
274
            self._enable_per_file_commits = False
275
        else:
276
            self._enable_per_file_commits = True
277
        if not self._enable_per_file_commits:
278
            self._file_message_expander.hide()
475.1.2 by Vincent Ladeuil
Fix bug #187283 fix replacing _() by _i18n().
279
            self._global_message_label.set_markup(_i18n('<b>Commit Message</b>'))
278.1.33 by John Arbash Meinel
Only enable the per-file dialog if 'per_file_commits' is enabled in the config.
280
278.1.12 by John Arbash Meinel
Delay computing the delta, and clean up some of the diff view names.
281
    def _compute_delta(self):
282
        self._delta = self._wt.changes_from(self._basis_tree)
283
278.1.5 by John Arbash Meinel
Starting to flesh out the dialog with actual windows.
284
    def construct(self):
285
        """Build up the dialog widgets."""
286
        # The primary pane which splits it into left and right (adjustable)
287
        # sections.
278.1.4 by John Arbash Meinel
Just playing around.
288
        self._hpane = gtk.HPaned()
278.1.5 by John Arbash Meinel
Starting to flesh out the dialog with actual windows.
289
290
        self._construct_left_pane()
291
        self._construct_right_pane()
278.1.23 by John Arbash Meinel
Beginning to support actual commit.
292
        self._construct_action_pane()
278.1.5 by John Arbash Meinel
Starting to flesh out the dialog with actual windows.
293
294
        self.vbox.pack_start(self._hpane)
295
        self._hpane.show()
296
        self.set_focus(self._global_message_text_view)
297
278.1.32 by John Arbash Meinel
Add the Ctrl+n accelerator to jump through the commit messages.
298
        self._construct_accelerators()
299
        self._set_sizes()
300
301
    def _set_sizes(self):
278.1.17 by John Arbash Meinel
Add a * reference for why you can't change the commit selection.
302
        # This seems like a reasonable default, we might like it to
303
        # be a bit wider, so that by default we can fit an 80-line diff in the
304
        # diff window.
305
        # Alternatively, we should be saving the last position/size rather than
306
        # setting it to a fixed value every time we start up.
307
        screen = self.get_screen()
308
        monitor = 0 # We would like it to be the monitor we are going to
309
                    # display on, but I don't know how to figure that out
310
                    # Only really useful for freaks like me that run dual
311
                    # monitor, with different sizes on the monitors
312
        monitor_rect = screen.get_monitor_geometry(monitor)
313
        width = int(monitor_rect.width * 0.66)
314
        height = int(monitor_rect.height * 0.66)
315
        self.set_default_size(width, height)
278.1.16 by John Arbash Meinel
Implement the file changes list on top of _iter_changes rather than
316
        self._hpane.set_position(300)
317
278.1.32 by John Arbash Meinel
Add the Ctrl+n accelerator to jump through the commit messages.
318
    def _construct_accelerators(self):
319
        group = gtk.AccelGroup()
320
        group.connect_group(gtk.gdk.keyval_from_name('N'),
321
                            gtk.gdk.CONTROL_MASK, 0, self._on_accel_next)
322
        self.add_accel_group(group)
323
500.2.1 by Scott Scriven
Made 'gcommit' ignore the escape key ('close' signal).
324
        # ignore the escape key (avoid closing the window)
500.2.2 by Scott Scriven
Simpler/cleaner way to ignore the 'close' signal.
325
        self.connect_object('close', self.emit_stop_by_name, 'close')
500.2.1 by Scott Scriven
Made 'gcommit' ignore the escape key ('close' signal).
326
278.1.5 by John Arbash Meinel
Starting to flesh out the dialog with actual windows.
327
    def _construct_left_pane(self):
278.1.11 by John Arbash Meinel
Worked out the rest of the spacing.
328
        self._left_pane_box = gtk.VBox(homogeneous=False, spacing=5)
278.1.5 by John Arbash Meinel
Starting to flesh out the dialog with actual windows.
329
        self._construct_file_list()
330
        self._construct_pending_list()
331
475.1.2 by Vincent Ladeuil
Fix bug #187283 fix replacing _() by _i18n().
332
        self._check_local = gtk.CheckButton(_i18n("_Only commit locally"),
278.1.25 by John Arbash Meinel
Add the 'Only Commit Locally' checkbox, we may want to put it elsewhere, though.
333
                                            use_underline=True)
334
        self._left_pane_box.pack_end(self._check_local, False, False)
335
        self._check_local.set_active(False)
336
278.1.15 by John Arbash Meinel
Hook up the list of modified files.
337
        self._hpane.pack1(self._left_pane_box, resize=False, shrink=False)
278.1.5 by John Arbash Meinel
Starting to flesh out the dialog with actual windows.
338
        self._left_pane_box.show()
339
340
    def _construct_right_pane(self):
341
        # TODO: I really want to make it so the diff view gets more space than
342
        # the global commit message, and the per-file commit message gets even
343
        # less. When I did it with wxGlade, I set it to 4 for diff, 2 for
344
        # commit, and 1 for file commit, and it looked good. But I don't seem
345
        # to have a way to do that with the gtk boxes... :( (Which is extra
346
        # weird since wx uses gtk on Linux...)
278.1.10 by John Arbash Meinel
To get the space weighting I wanted, I turned to a Table.
347
        self._right_pane_table = gtk.Table(rows=10, columns=1, homogeneous=False)
278.1.11 by John Arbash Meinel
Worked out the rest of the spacing.
348
        self._right_pane_table.set_row_spacings(5)
349
        self._right_pane_table.set_col_spacings(5)
278.1.10 by John Arbash Meinel
To get the space weighting I wanted, I turned to a Table.
350
        self._right_pane_table_row = 0
278.1.5 by John Arbash Meinel
Starting to flesh out the dialog with actual windows.
351
        self._construct_diff_view()
352
        self._construct_file_message()
353
        self._construct_global_message()
354
278.1.10 by John Arbash Meinel
To get the space weighting I wanted, I turned to a Table.
355
        self._right_pane_table.show()
356
        self._hpane.pack2(self._right_pane_table, resize=True, shrink=True)
357
278.1.23 by John Arbash Meinel
Beginning to support actual commit.
358
    def _construct_action_pane(self):
635.2.7 by Vincent Ladeuil
Land Anne Mohsen's patch for bug 215674.
359
        self._button_cancel = gtk.Button(stock=gtk.STOCK_CANCEL)
360
        self._button_cancel.connect('clicked', self._on_cancel_clicked)
361
        self._button_cancel.show()
362
        self.action_area.pack_end(self._button_cancel)
475.1.2 by Vincent Ladeuil
Fix bug #187283 fix replacing _() by _i18n().
363
        self._button_commit = gtk.Button(_i18n("Comm_it"), use_underline=True)
278.1.23 by John Arbash Meinel
Beginning to support actual commit.
364
        self._button_commit.connect('clicked', self._on_commit_clicked)
365
        self._button_commit.set_flags(gtk.CAN_DEFAULT)
278.1.24 by John Arbash Meinel
Actually show the commit button.
366
        self._button_commit.show()
278.1.23 by John Arbash Meinel
Beginning to support actual commit.
367
        self.action_area.pack_end(self._button_commit)
278.1.24 by John Arbash Meinel
Actually show the commit button.
368
        self._button_commit.grab_default()
278.1.23 by John Arbash Meinel
Beginning to support actual commit.
369
278.1.10 by John Arbash Meinel
To get the space weighting I wanted, I turned to a Table.
370
    def _add_to_right_table(self, widget, weight, expanding=False):
371
        """Add another widget to the table
372
373
        :param widget: The object to add
374
        :param weight: How many rows does this widget get to request
375
        :param expanding: Should expand|fill|shrink be set?
376
        """
377
        end_row = self._right_pane_table_row + weight
378
        options = 0
379
        expand_opts = gtk.EXPAND | gtk.FILL | gtk.SHRINK
380
        if expanding:
381
            options = expand_opts
382
        self._right_pane_table.attach(widget, 0, 1,
383
                                      self._right_pane_table_row, end_row,
384
                                      xoptions=expand_opts, yoptions=options)
385
        self._right_pane_table_row = end_row
278.1.5 by John Arbash Meinel
Starting to flesh out the dialog with actual windows.
386
387
    def _construct_file_list(self):
278.1.11 by John Arbash Meinel
Worked out the rest of the spacing.
388
        self._files_box = gtk.VBox(homogeneous=False, spacing=0)
475.1.2 by Vincent Ladeuil
Fix bug #187283 fix replacing _() by _i18n().
389
        file_label = gtk.Label(_i18n('Files'))
278.1.41 by John Arbash Meinel
For the moment, just hide the section headings Files and Diff for *.
390
        # file_label.show()
278.1.5 by John Arbash Meinel
Starting to flesh out the dialog with actual windows.
391
        self._files_box.pack_start(file_label, expand=False)
392
278.1.42 by John Arbash Meinel
start playing with using a radial box, rather than an entry in the list
393
        self._commit_all_files_radio = gtk.RadioButton(
475.1.2 by Vincent Ladeuil
Fix bug #187283 fix replacing _() by _i18n().
394
            None, _i18n("Commit all changes"))
278.1.42 by John Arbash Meinel
start playing with using a radial box, rather than an entry in the list
395
        self._files_box.pack_start(self._commit_all_files_radio, expand=False)
396
        self._commit_all_files_radio.show()
278.1.43 by John Arbash Meinel
Finish connecting the 'Commit all changes' radio buttons.
397
        self._commit_all_files_radio.connect('toggled',
398
            self._toggle_commit_selection)
278.1.42 by John Arbash Meinel
start playing with using a radial box, rather than an entry in the list
399
        self._commit_selected_radio = gtk.RadioButton(
475.1.2 by Vincent Ladeuil
Fix bug #187283 fix replacing _() by _i18n().
400
            self._commit_all_files_radio, _i18n("Only commit selected changes"))
278.1.42 by John Arbash Meinel
start playing with using a radial box, rather than an entry in the list
401
        self._files_box.pack_start(self._commit_selected_radio, expand=False)
402
        self._commit_selected_radio.show()
278.1.43 by John Arbash Meinel
Finish connecting the 'Commit all changes' radio buttons.
403
        self._commit_selected_radio.connect('toggled',
404
            self._toggle_commit_selection)
405
        if self._pending:
475.1.2 by Vincent Ladeuil
Fix bug #187283 fix replacing _() by _i18n().
406
            self._commit_all_files_radio.set_label(_i18n('Commit all changes*'))
278.1.43 by John Arbash Meinel
Finish connecting the 'Commit all changes' radio buttons.
407
            self._commit_all_files_radio.set_sensitive(False)
408
            self._commit_selected_radio.set_sensitive(False)
278.1.42 by John Arbash Meinel
start playing with using a radial box, rather than an entry in the list
409
278.1.5 by John Arbash Meinel
Starting to flesh out the dialog with actual windows.
410
        scroller = gtk.ScrolledWindow()
411
        scroller.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
126.1.1 by Szilveszter Farkas (Phanatic)
New Commit dialog implementation (no more Glade).
412
        self._treeview_files = gtk.TreeView()
278.1.5 by John Arbash Meinel
Starting to flesh out the dialog with actual windows.
413
        self._treeview_files.show()
414
        scroller.add(self._treeview_files)
278.1.32 by John Arbash Meinel
Add the Ctrl+n accelerator to jump through the commit messages.
415
        scroller.set_shadow_type(gtk.SHADOW_IN)
278.1.5 by John Arbash Meinel
Starting to flesh out the dialog with actual windows.
416
        scroller.show()
417
        self._files_box.pack_start(scroller,
418
                                   expand=True, fill=True)
419
        self._files_box.show()
420
        self._left_pane_box.pack_start(self._files_box)
421
278.1.31 by John Arbash Meinel
We can make bencode work again by a simple decode/encode step.
422
        # Keep note that all strings stored in a ListStore must be UTF-8
423
        # strings. GTK does not support directly setting and restoring Unicode
424
        # objects.
278.1.15 by John Arbash Meinel
Hook up the list of modified files.
425
        liststore = gtk.ListStore(
426
            gobject.TYPE_STRING,  # [0] file_id
427
            gobject.TYPE_STRING,  # [1] real path
428
            gobject.TYPE_BOOLEAN, # [2] checkbox
429
            gobject.TYPE_STRING,  # [3] display path
430
            gobject.TYPE_STRING,  # [4] changes type
278.1.21 by John Arbash Meinel
Start tracking the per-file commit messages.
431
            gobject.TYPE_STRING,  # [5] commit message
278.1.15 by John Arbash Meinel
Hook up the list of modified files.
432
            )
433
        self._files_store = liststore
434
        self._treeview_files.set_model(liststore)
435
        crt = gtk.CellRendererToggle()
278.1.39 by John Arbash Meinel
To disable a checkbox it is set_property('activatable', False),
436
        crt.set_property('activatable', not bool(self._pending))
278.1.15 by John Arbash Meinel
Hook up the list of modified files.
437
        crt.connect("toggled", self._toggle_commit, self._files_store)
278.1.17 by John Arbash Meinel
Add a * reference for why you can't change the commit selection.
438
        if self._pending:
475.1.2 by Vincent Ladeuil
Fix bug #187283 fix replacing _() by _i18n().
439
            name = _i18n('Commit*')
278.1.17 by John Arbash Meinel
Add a * reference for why you can't change the commit selection.
440
        else:
475.1.2 by Vincent Ladeuil
Fix bug #187283 fix replacing _() by _i18n().
441
            name = _i18n('Commit')
278.1.43 by John Arbash Meinel
Finish connecting the 'Commit all changes' radio buttons.
442
        commit_col = gtk.TreeViewColumn(name, crt, active=2)
443
        commit_col.set_visible(False)
444
        self._treeview_files.append_column(commit_col)
475.1.2 by Vincent Ladeuil
Fix bug #187283 fix replacing _() by _i18n().
445
        self._treeview_files.append_column(gtk.TreeViewColumn(_i18n('Path'),
278.1.15 by John Arbash Meinel
Hook up the list of modified files.
446
                                           gtk.CellRendererText(), text=3))
475.1.2 by Vincent Ladeuil
Fix bug #187283 fix replacing _() by _i18n().
447
        self._treeview_files.append_column(gtk.TreeViewColumn(_i18n('Type'),
278.1.15 by John Arbash Meinel
Hook up the list of modified files.
448
                                           gtk.CellRendererText(), text=4))
278.1.17 by John Arbash Meinel
Add a * reference for why you can't change the commit selection.
449
        self._treeview_files.connect('cursor-changed',
450
                                     self._on_treeview_files_cursor_changed)
278.1.15 by John Arbash Meinel
Hook up the list of modified files.
451
452
    def _toggle_commit(self, cell, path, model):
278.1.20 by John Arbash Meinel
We always select the All Files record in the files view,
453
        if model[path][0] is None: # No file_id means 'All Files'
454
            new_val = not model[path][2]
455
            for node in model:
456
                node[2] = new_val
457
        else:
458
            model[path][2] = not model[path][2]
278.1.15 by John Arbash Meinel
Hook up the list of modified files.
459
278.1.43 by John Arbash Meinel
Finish connecting the 'Commit all changes' radio buttons.
460
    def _toggle_commit_selection(self, button):
461
        all_files = self._commit_all_files_radio.get_active()
462
        if self._commit_all_changes != all_files:
463
            checked_col = self._treeview_files.get_column(0)
464
            self._commit_all_changes = all_files
465
            if all_files:
466
                checked_col.set_visible(False)
467
            else:
468
                checked_col.set_visible(True)
469
            renderer = checked_col.get_cell_renderers()[0]
470
            renderer.set_property('activatable', not all_files)
471
278.1.5 by John Arbash Meinel
Starting to flesh out the dialog with actual windows.
472
    def _construct_pending_list(self):
473
        # Pending information defaults to hidden, we put it all in 1 box, so
474
        # that we can show/hide all of them at once
475
        self._pending_box = gtk.VBox()
476
        self._pending_box.hide()
477
478
        pending_message = gtk.Label()
479
        pending_message.set_markup(
475.1.2 by Vincent Ladeuil
Fix bug #187283 fix replacing _() by _i18n().
480
            _i18n('<i>* Cannot select specific files when merging</i>'))
278.1.11 by John Arbash Meinel
Worked out the rest of the spacing.
481
        self._pending_box.pack_start(pending_message, expand=False, padding=5)
278.1.5 by John Arbash Meinel
Starting to flesh out the dialog with actual windows.
482
        pending_message.show()
483
475.1.2 by Vincent Ladeuil
Fix bug #187283 fix replacing _() by _i18n().
484
        pending_label = gtk.Label(_i18n('Pending Revisions'))
278.1.11 by John Arbash Meinel
Worked out the rest of the spacing.
485
        self._pending_box.pack_start(pending_label, expand=False, padding=0)
278.1.5 by John Arbash Meinel
Starting to flesh out the dialog with actual windows.
486
        pending_label.show()
487
488
        scroller = gtk.ScrolledWindow()
489
        scroller.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
490
        self._treeview_pending = gtk.TreeView()
491
        scroller.add(self._treeview_pending)
278.1.32 by John Arbash Meinel
Add the Ctrl+n accelerator to jump through the commit messages.
492
        scroller.set_shadow_type(gtk.SHADOW_IN)
278.1.5 by John Arbash Meinel
Starting to flesh out the dialog with actual windows.
493
        scroller.show()
494
        self._pending_box.pack_start(scroller,
278.1.11 by John Arbash Meinel
Worked out the rest of the spacing.
495
                                     expand=True, fill=True, padding=5)
278.1.5 by John Arbash Meinel
Starting to flesh out the dialog with actual windows.
496
        self._treeview_pending.show()
497
        self._left_pane_box.pack_start(self._pending_box)
498
278.1.15 by John Arbash Meinel
Hook up the list of modified files.
499
        liststore = gtk.ListStore(gobject.TYPE_STRING, # revision_id
500
                                  gobject.TYPE_STRING, # date
501
                                  gobject.TYPE_STRING, # committer
502
                                  gobject.TYPE_STRING, # summary
278.1.14 by John Arbash Meinel
Tests that we fill out the pending list correctly.
503
                                 )
278.1.15 by John Arbash Meinel
Hook up the list of modified files.
504
        self._pending_store = liststore
278.1.14 by John Arbash Meinel
Tests that we fill out the pending list correctly.
505
        self._treeview_pending.set_model(liststore)
475.1.2 by Vincent Ladeuil
Fix bug #187283 fix replacing _() by _i18n().
506
        self._treeview_pending.append_column(gtk.TreeViewColumn(_i18n('Date'),
278.1.14 by John Arbash Meinel
Tests that we fill out the pending list correctly.
507
                                             gtk.CellRendererText(), text=1))
475.1.2 by Vincent Ladeuil
Fix bug #187283 fix replacing _() by _i18n().
508
        self._treeview_pending.append_column(gtk.TreeViewColumn(_i18n('Committer'),
278.1.14 by John Arbash Meinel
Tests that we fill out the pending list correctly.
509
                                             gtk.CellRendererText(), text=2))
475.1.2 by Vincent Ladeuil
Fix bug #187283 fix replacing _() by _i18n().
510
        self._treeview_pending.append_column(gtk.TreeViewColumn(_i18n('Summary'),
278.1.14 by John Arbash Meinel
Tests that we fill out the pending list correctly.
511
                                             gtk.CellRendererText(), text=3))
512
278.1.5 by John Arbash Meinel
Starting to flesh out the dialog with actual windows.
513
    def _construct_diff_view(self):
278.1.12 by John Arbash Meinel
Delay computing the delta, and clean up some of the diff view names.
514
        from diff import DiffView
278.1.5 by John Arbash Meinel
Starting to flesh out the dialog with actual windows.
515
278.1.41 by John Arbash Meinel
For the moment, just hide the section headings Files and Diff for *.
516
        # TODO: jam 2007-10-30 The diff label is currently disabled. If we
517
        #       decide that we really don't ever want to display it, we should
518
        #       actually remove it, and other references to it, along with the
519
        #       tests that it is set properly.
475.1.2 by Vincent Ladeuil
Fix bug #187283 fix replacing _() by _i18n().
520
        self._diff_label = gtk.Label(_i18n('Diff for whole tree'))
278.1.11 by John Arbash Meinel
Worked out the rest of the spacing.
521
        self._diff_label.set_alignment(0, 0)
522
        self._right_pane_table.set_row_spacing(self._right_pane_table_row, 0)
278.1.10 by John Arbash Meinel
To get the space weighting I wanted, I turned to a Table.
523
        self._add_to_right_table(self._diff_label, 1, False)
278.1.41 by John Arbash Meinel
For the moment, just hide the section headings Files and Diff for *.
524
        # self._diff_label.show()
278.1.5 by John Arbash Meinel
Starting to flesh out the dialog with actual windows.
525
278.1.12 by John Arbash Meinel
Delay computing the delta, and clean up some of the diff view names.
526
        self._diff_view = DiffView()
278.1.10 by John Arbash Meinel
To get the space weighting I wanted, I turned to a Table.
527
        self._add_to_right_table(self._diff_view, 4, True)
278.1.5 by John Arbash Meinel
Starting to flesh out the dialog with actual windows.
528
        self._diff_view.show()
529
530
    def _construct_file_message(self):
278.1.9 by John Arbash Meinel
Move all text entry boxes into a ScrolledWindow, so that they don't change size constantly.
531
        scroller = gtk.ScrolledWindow()
532
        scroller.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
533
534
        self._file_message_text_view = gtk.TextView()
535
        scroller.add(self._file_message_text_view)
278.1.32 by John Arbash Meinel
Add the Ctrl+n accelerator to jump through the commit messages.
536
        scroller.set_shadow_type(gtk.SHADOW_IN)
278.1.9 by John Arbash Meinel
Move all text entry boxes into a ScrolledWindow, so that they don't change size constantly.
537
        scroller.show()
278.1.5 by John Arbash Meinel
Starting to flesh out the dialog with actual windows.
538
539
        self._file_message_text_view.modify_font(pango.FontDescription("Monospace"))
540
        self._file_message_text_view.set_wrap_mode(gtk.WRAP_WORD)
541
        self._file_message_text_view.set_accepts_tab(False)
542
        self._file_message_text_view.show()
543
475.1.2 by Vincent Ladeuil
Fix bug #187283 fix replacing _() by _i18n().
544
        self._file_message_expander = gtk.Expander(_i18n('File commit message'))
278.1.32 by John Arbash Meinel
Add the Ctrl+n accelerator to jump through the commit messages.
545
        self._file_message_expander.set_expanded(True)
546
        self._file_message_expander.add(scroller)
278.1.10 by John Arbash Meinel
To get the space weighting I wanted, I turned to a Table.
547
        self._add_to_right_table(self._file_message_expander, 1, False)
278.1.5 by John Arbash Meinel
Starting to flesh out the dialog with actual windows.
548
        self._file_message_expander.show()
549
550
    def _construct_global_message(self):
475.1.2 by Vincent Ladeuil
Fix bug #187283 fix replacing _() by _i18n().
551
        self._global_message_label = gtk.Label(_i18n('Global Commit Message'))
552
        self._global_message_label.set_markup(
553
            _i18n('<b>Global Commit Message</b>'))
278.1.11 by John Arbash Meinel
Worked out the rest of the spacing.
554
        self._global_message_label.set_alignment(0, 0)
555
        self._right_pane_table.set_row_spacing(self._right_pane_table_row, 0)
278.1.10 by John Arbash Meinel
To get the space weighting I wanted, I turned to a Table.
556
        self._add_to_right_table(self._global_message_label, 1, False)
278.1.11 by John Arbash Meinel
Worked out the rest of the spacing.
557
        # Can we remove the spacing between the label and the box?
278.1.5 by John Arbash Meinel
Starting to flesh out the dialog with actual windows.
558
        self._global_message_label.show()
559
278.1.9 by John Arbash Meinel
Move all text entry boxes into a ScrolledWindow, so that they don't change size constantly.
560
        scroller = gtk.ScrolledWindow()
561
        scroller.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
562
563
        self._global_message_text_view = gtk.TextView()
635.2.12 by Vincent Ladeuil
Implement commit message saving without modifying bzrlib.
564
        self._set_global_commit_message(self._saved_commit_messages_manager.get()[0])
278.1.5 by John Arbash Meinel
Starting to flesh out the dialog with actual windows.
565
        self._global_message_text_view.modify_font(pango.FontDescription("Monospace"))
278.1.9 by John Arbash Meinel
Move all text entry boxes into a ScrolledWindow, so that they don't change size constantly.
566
        scroller.add(self._global_message_text_view)
278.1.32 by John Arbash Meinel
Add the Ctrl+n accelerator to jump through the commit messages.
567
        scroller.set_shadow_type(gtk.SHADOW_IN)
278.1.9 by John Arbash Meinel
Move all text entry boxes into a ScrolledWindow, so that they don't change size constantly.
568
        scroller.show()
278.1.10 by John Arbash Meinel
To get the space weighting I wanted, I turned to a Table.
569
        self._add_to_right_table(scroller, 2, True)
278.1.5 by John Arbash Meinel
Starting to flesh out the dialog with actual windows.
570
        self._file_message_text_view.set_wrap_mode(gtk.WRAP_WORD)
571
        self._file_message_text_view.set_accepts_tab(False)
572
        self._global_message_text_view.show()
573
278.1.17 by John Arbash Meinel
Add a * reference for why you can't change the commit selection.
574
    def _on_treeview_files_cursor_changed(self, treeview):
278.1.18 by John Arbash Meinel
Start checking the diff view is correct.
575
        treeselection = treeview.get_selection()
278.1.21 by John Arbash Meinel
Start tracking the per-file commit messages.
576
        (model, selection) = treeselection.get_selected()
278.1.17 by John Arbash Meinel
Add a * reference for why you can't change the commit selection.
577
278.1.21 by John Arbash Meinel
Start tracking the per-file commit messages.
578
        if selection is not None:
579
            path, display_path = model.get(selection, 1, 3)
475.1.2 by Vincent Ladeuil
Fix bug #187283 fix replacing _() by _i18n().
580
            self._diff_label.set_text(_i18n('Diff for ') + display_path)
278.1.20 by John Arbash Meinel
We always select the All Files record in the files view,
581
            if path is None:
582
                self._diff_view.show_diff(None)
583
            else:
278.1.29 by John Arbash Meinel
Start testing with Unicode data.
584
                self._diff_view.show_diff([path.decode('UTF-8')])
278.1.21 by John Arbash Meinel
Start tracking the per-file commit messages.
585
            self._update_per_file_info(selection)
586
278.1.32 by John Arbash Meinel
Add the Ctrl+n accelerator to jump through the commit messages.
587
    def _on_accel_next(self, accel_group, window, keyval, modifier):
588
        # We don't really care about any of the parameters, because we know
589
        # where this message came from
590
        tree_selection = self._treeview_files.get_selection()
591
        (model, selection) = tree_selection.get_selected()
592
        if selection is None:
593
            next = None
594
        else:
595
            next = model.iter_next(selection)
596
597
        if next is None:
598
            # We have either made it to the end of the list, or nothing was
599
            # selected. Either way, select All Files, and jump to the global
600
            # commit message.
601
            self._treeview_files.set_cursor((0,))
602
            self._global_message_text_view.grab_focus()
603
        else:
604
            # Set the cursor to this entry, and jump to the per-file commit
605
            # message
606
            self._treeview_files.set_cursor(model.get_path(next))
607
            self._file_message_text_view.grab_focus()
608
278.1.21 by John Arbash Meinel
Start tracking the per-file commit messages.
609
    def _save_current_file_message(self):
610
        if self._last_selected_file is None:
611
            return # Nothing to save
612
        text_buffer = self._file_message_text_view.get_buffer()
613
        cur_text = text_buffer.get_text(text_buffer.get_start_iter(),
614
                                        text_buffer.get_end_iter())
615
        last_selected = self._files_store.get_iter(self._last_selected_file)
616
        self._files_store.set_value(last_selected, 5, cur_text)
617
618
    def _update_per_file_info(self, selection):
619
        # The node is changing, so cache the current message
278.1.33 by John Arbash Meinel
Only enable the per-file dialog if 'per_file_commits' is enabled in the config.
620
        if not self._enable_per_file_commits:
621
            return
622
278.1.21 by John Arbash Meinel
Start tracking the per-file commit messages.
623
        self._save_current_file_message()
624
        text_buffer = self._file_message_text_view.get_buffer()
625
        file_id, display_path, message = self._files_store.get(selection, 0, 3, 5)
626
        if file_id is None: # Whole tree
475.1.2 by Vincent Ladeuil
Fix bug #187283 fix replacing _() by _i18n().
627
            self._file_message_expander.set_label(_i18n('File commit message'))
278.1.21 by John Arbash Meinel
Start tracking the per-file commit messages.
628
            self._file_message_expander.set_expanded(False)
629
            self._file_message_expander.set_sensitive(False)
630
            text_buffer.set_text('')
631
            self._last_selected_file = None
632
        else:
475.1.2 by Vincent Ladeuil
Fix bug #187283 fix replacing _() by _i18n().
633
            self._file_message_expander.set_label(_i18n('Commit message for ')
278.1.21 by John Arbash Meinel
Start tracking the per-file commit messages.
634
                                                  + display_path)
635
            self._file_message_expander.set_expanded(True)
636
            self._file_message_expander.set_sensitive(True)
637
            text_buffer.set_text(message)
638
            self._last_selected_file = self._files_store.get_path(selection)
278.1.17 by John Arbash Meinel
Add a * reference for why you can't change the commit selection.
639
278.1.27 by John Arbash Meinel
Add the ability to commit just specific files.
640
    def _get_specific_files(self):
278.1.31 by John Arbash Meinel
We can make bencode work again by a simple decode/encode step.
641
        """Return the list of selected paths, and file info.
642
643
        :return: ([unicode paths], [{utf-8 file info}]
644
        """
278.1.27 by John Arbash Meinel
Add the ability to commit just specific files.
645
        self._save_current_file_message()
646
        files = []
647
        records = iter(self._files_store)
648
        rec = records.next() # Skip the All Files record
649
        assert rec[0] is None, "Are we skipping the wrong record?"
650
651
        file_info = []
652
        for record in records:
278.1.43 by John Arbash Meinel
Finish connecting the 'Commit all changes' radio buttons.
653
            if self._commit_all_changes or record[2]:# [2] checkbox
278.1.29 by John Arbash Meinel
Start testing with Unicode data.
654
                file_id = record[0] # [0] file_id
278.1.31 by John Arbash Meinel
We can make bencode work again by a simple decode/encode step.
655
                path = record[1]    # [1] real path
622.1.1 by John Arbash Meinel
Ensure that per-file commit messages and global commit messages get sanitized.
656
                # [5] commit message
657
                file_message = _sanitize_and_decode_message(record[5])
278.1.29 by John Arbash Meinel
Start testing with Unicode data.
658
                files.append(path.decode('UTF-8'))
278.1.33 by John Arbash Meinel
Only enable the per-file dialog if 'per_file_commits' is enabled in the config.
659
                if self._enable_per_file_commits and file_message:
278.1.29 by John Arbash Meinel
Start testing with Unicode data.
660
                    # All of this needs to be utf-8 information
622.1.1 by John Arbash Meinel
Ensure that per-file commit messages and global commit messages get sanitized.
661
                    file_message = file_message.encode('UTF-8')
278.1.27 by John Arbash Meinel
Add the ability to commit just specific files.
662
                    file_info.append({'path':path, 'file_id':file_id,
663
                                     'message':file_message})
664
        file_info.sort(key=lambda x:(x['path'], x['file_id']))
278.1.33 by John Arbash Meinel
Only enable the per-file dialog if 'per_file_commits' is enabled in the config.
665
        if self._enable_per_file_commits:
666
            return files, file_info
667
        else:
668
            return files, []
278.1.27 by John Arbash Meinel
Add the ability to commit just specific files.
669
278.1.23 by John Arbash Meinel
Beginning to support actual commit.
670
    @show_bzr_error
635.2.7 by Vincent Ladeuil
Land Anne Mohsen's patch for bug 215674.
671
    def _on_cancel_clicked(self, button):
672
        """ Cancel button clicked handler. """
673
        self._do_cancel()
674
675
    @show_bzr_error
676
    def _on_delete_window(self, source, event):
677
        """ Delete window handler. """
678
        self._do_cancel()
679
680
    def _do_cancel(self):
681
        """If requested, saves commit messages when cancelling gcommit; they are re-used by a next gcommit"""
635.2.12 by Vincent Ladeuil
Implement commit message saving without modifying bzrlib.
682
        mgr = SavedCommitMessagesManager()
683
        self._saved_commit_messages_manager = mgr
684
        mgr.insert(self._get_global_commit_message(),
685
                   self._get_specific_files()[1])
686
        if mgr.is_not_empty(): # maybe worth saving
687
            response = self._question_dialog(
688
                _i18n('Commit cancelled'),
689
                _i18n('Do you want to save your commit messages ?'),
690
                parent=self)
691
            if response == gtk.RESPONSE_NO:
692
                 # save nothing and destroy old comments if any
693
                mgr = SavedCommitMessagesManager()
694
        mgr.save(self._wt, self._wt.branch)
635.2.7 by Vincent Ladeuil
Land Anne Mohsen's patch for bug 215674.
695
        self.response(gtk.RESPONSE_CANCEL) # close window
696
697
    @show_bzr_error
278.1.23 by John Arbash Meinel
Beginning to support actual commit.
698
    def _on_commit_clicked(self, button):
699
        """ Commit button clicked handler. """
700
        self._do_commit()
701
702
    def _do_commit(self):
278.1.25 by John Arbash Meinel
Add the 'Only Commit Locally' checkbox, we may want to put it elsewhere, though.
703
        message = self._get_global_commit_message()
278.1.23 by John Arbash Meinel
Beginning to support actual commit.
704
705
        if message == '':
606 by Vincent Ladeuil
Fix gtk dialogs popping up and asking for input during selftest.
706
            response = self._question_dialog(
475.1.2 by Vincent Ladeuil
Fix bug #187283 fix replacing _() by _i18n().
707
                _i18n('Commit with an empty message?'),
576.1.1 by Jasper Groenewegen
Add parent setting to dialogs and implement in gcommit
708
                _i18n('You can describe your commit intent in the message.'),
709
                parent=self)
278.1.23 by John Arbash Meinel
Beginning to support actual commit.
710
            if response == gtk.RESPONSE_NO:
711
                # Kindly give focus to message area
712
                self._global_message_text_view.grab_focus()
713
                return
714
278.1.27 by John Arbash Meinel
Add the ability to commit just specific files.
715
        specific_files, file_info = self._get_specific_files()
278.1.28 by John Arbash Meinel
Ensure that we can set per-file messages even during a merge.
716
        if self._pending:
717
            specific_files = None
278.1.23 by John Arbash Meinel
Beginning to support actual commit.
718
278.1.25 by John Arbash Meinel
Add the 'Only Commit Locally' checkbox, we may want to put it elsewhere, though.
719
        local = self._check_local.get_active()
278.1.23 by John Arbash Meinel
Beginning to support actual commit.
720
278.1.26 by John Arbash Meinel
Handle pointless commits and trees with unknown files.
721
        # All we care about is if there is a single unknown, so if this loop is
722
        # entered, then there are unknown files.
723
        # TODO: jam 20071002 It seems like this should cancel the dialog
724
        #       entirely, since there isn't a way for them to add the unknown
725
        #       files at this point.
726
        for path in self._wt.unknowns():
606 by Vincent Ladeuil
Fix gtk dialogs popping up and asking for input during selftest.
727
            response = self._question_dialog(
475.1.2 by Vincent Ladeuil
Fix bug #187283 fix replacing _() by _i18n().
728
                _i18n("Commit with unknowns?"),
576.1.1 by Jasper Groenewegen
Add parent setting to dialogs and implement in gcommit
729
                _i18n("Unknown files exist in the working tree. Commit anyway?"),
730
                parent=self)
731
                # Doesn't set a parent for the dialog..
278.1.26 by John Arbash Meinel
Handle pointless commits and trees with unknown files.
732
            if response == gtk.RESPONSE_NO:
733
                return
734
            break
278.1.23 by John Arbash Meinel
Beginning to support actual commit.
735
278.1.25 by John Arbash Meinel
Add the 'Only Commit Locally' checkbox, we may want to put it elsewhere, though.
736
        rev_id = None
278.1.27 by John Arbash Meinel
Add the ability to commit just specific files.
737
        revprops = {}
738
        if file_info:
278.1.31 by John Arbash Meinel
We can make bencode work again by a simple decode/encode step.
739
            revprops['file-info'] = bencode.bencode(file_info).decode('UTF-8')
278.1.23 by John Arbash Meinel
Beginning to support actual commit.
740
        try:
741
            rev_id = self._wt.commit(message,
742
                       allow_pointless=False,
743
                       strict=False,
744
                       local=local,
278.1.27 by John Arbash Meinel
Add the ability to commit just specific files.
745
                       specific_files=specific_files,
746
                       revprops=revprops)
278.1.23 by John Arbash Meinel
Beginning to support actual commit.
747
        except errors.PointlessCommit:
606 by Vincent Ladeuil
Fix gtk dialogs popping up and asking for input during selftest.
748
            response = self._question_dialog(
475.1.2 by Vincent Ladeuil
Fix bug #187283 fix replacing _() by _i18n().
749
                _i18n('Commit with no changes?'),
750
                _i18n('There are no changes in the working tree.'
576.1.1 by Jasper Groenewegen
Add parent setting to dialogs and implement in gcommit
751
                      ' Do you want to commit anyway?'),
752
                parent=self)
278.1.23 by John Arbash Meinel
Beginning to support actual commit.
753
            if response == gtk.RESPONSE_YES:
754
                rev_id = self._wt.commit(message,
755
                               allow_pointless=True,
756
                               strict=False,
757
                               local=local,
278.1.27 by John Arbash Meinel
Add the ability to commit just specific files.
758
                               specific_files=specific_files,
759
                               revprops=revprops)
278.1.23 by John Arbash Meinel
Beginning to support actual commit.
760
        self.committed_revision_id = rev_id
635.2.7 by Vincent Ladeuil
Land Anne Mohsen's patch for bug 215674.
761
        # destroy old comments if any
635.2.12 by Vincent Ladeuil
Implement commit message saving without modifying bzrlib.
762
        SavedCommitMessagesManager().save(self._wt, self._wt.branch)
278.1.23 by John Arbash Meinel
Beginning to support actual commit.
763
        self.response(gtk.RESPONSE_OK)
764
278.1.25 by John Arbash Meinel
Add the 'Only Commit Locally' checkbox, we may want to put it elsewhere, though.
765
    def _get_global_commit_message(self):
766
        buf = self._global_message_text_view.get_buffer()
767
        start, end = buf.get_bounds()
622.1.1 by John Arbash Meinel
Ensure that per-file commit messages and global commit messages get sanitized.
768
        text = buf.get_text(start, end)
769
        return _sanitize_and_decode_message(text)
278.1.25 by John Arbash Meinel
Add the 'Only Commit Locally' checkbox, we may want to put it elsewhere, though.
770
771
    def _set_global_commit_message(self, message):
772
        """Just a helper for the test suite."""
278.1.29 by John Arbash Meinel
Start testing with Unicode data.
773
        if isinstance(message, unicode):
774
            message = message.encode('UTF-8')
278.1.25 by John Arbash Meinel
Add the 'Only Commit Locally' checkbox, we may want to put it elsewhere, though.
775
        self._global_message_text_view.get_buffer().set_text(message)
776
278.1.27 by John Arbash Meinel
Add the ability to commit just specific files.
777
    def _set_file_commit_message(self, message):
778
        """Helper for the test suite."""
278.1.29 by John Arbash Meinel
Start testing with Unicode data.
779
        if isinstance(message, unicode):
780
            message = message.encode('UTF-8')
278.1.27 by John Arbash Meinel
Add the ability to commit just specific files.
781
        self._file_message_text_view.get_buffer().set_text(message)
782
278.1.5 by John Arbash Meinel
Starting to flesh out the dialog with actual windows.
783
    @staticmethod
784
    def _rev_to_pending_info(rev):
785
        """Get the information from a pending merge."""
126.1.1 by Szilveszter Farkas (Phanatic)
New Commit dialog implementation (no more Glade).
786
        from bzrlib.osutils import format_date
278.1.5 by John Arbash Meinel
Starting to flesh out the dialog with actual windows.
787
        rev_dict = {}
788
        rev_dict['committer'] = re.sub('<.*@.*>', '', rev.committer).strip(' ')
789
        rev_dict['summary'] = rev.get_summary()
790
        rev_dict['date'] = format_date(rev.timestamp,
791
                                       rev.timezone or 0,
792
                                       'original', date_fmt="%Y-%m-%d",
793
                                       show_offset=False)
794
        rev_dict['revision_id'] = rev.revision_id
795
        return rev_dict
635.2.12 by Vincent Ladeuil
Implement commit message saving without modifying bzrlib.
796
797
798
class SavedCommitMessagesManager:
635.2.13 by Vincent Ladeuil
Add NEWS entry and do some cleanup.
799
    """Save glogal and per-file commit messages.
800
801
    Saves global commit message and utf-8 file_id->message dictionary
802
    of per-file commit messages on disk. Re-reads them later for re-using.
803
    """
804
635.2.12 by Vincent Ladeuil
Implement commit message saving without modifying bzrlib.
805
    def __init__(self, tree=None, branch=None):
806
        """If branch is None, builds empty messages, otherwise reads them
807
        from branch's disk storage. 'tree' argument is for the future."""
808
        if branch is None:
809
            self.global_message = u''
810
            self.file_messages = {}
811
        else:
812
            config = branch.get_config()._get_branch_data_config()
635.2.13 by Vincent Ladeuil
Add NEWS entry and do some cleanup.
813
            self.global_message = config.get_user_option(
814
                'gtk_global_commit_message')
635.2.12 by Vincent Ladeuil
Implement commit message saving without modifying bzrlib.
815
            if self.global_message is None:
816
                self.global_message = u''
817
            file_messages = config.get_user_option('gtk_file_commit_messages')
818
            if file_messages: # unicode and B-encoded:
635.2.13 by Vincent Ladeuil
Add NEWS entry and do some cleanup.
819
                self.file_messages = bencode.bdecode(
820
                    file_messages.encode('UTF-8'))
635.2.12 by Vincent Ladeuil
Implement commit message saving without modifying bzrlib.
821
            else:
822
                self.file_messages = {}
635.2.13 by Vincent Ladeuil
Add NEWS entry and do some cleanup.
823
635.2.12 by Vincent Ladeuil
Implement commit message saving without modifying bzrlib.
824
    def get(self):
825
        return self.global_message, self.file_messages
635.2.13 by Vincent Ladeuil
Add NEWS entry and do some cleanup.
826
635.2.12 by Vincent Ladeuil
Implement commit message saving without modifying bzrlib.
827
    def is_not_empty(self):
828
        return bool(self.global_message or self.file_messages)
635.2.13 by Vincent Ladeuil
Add NEWS entry and do some cleanup.
829
635.2.12 by Vincent Ladeuil
Implement commit message saving without modifying bzrlib.
830
    def insert(self, global_message, file_info):
831
        """Formats per-file commit messages (list of dictionaries, one per file)
832
        into one utf-8 file_id->message dictionary and merges this with
833
        previously existing dictionary. Merges global commit message too."""
834
        file_messages = {}
835
        for fi in file_info:
836
            file_message = fi['message']
837
            if file_message:
838
                file_messages[fi['file_id']] = file_message # utf-8 strings
839
        for k,v in file_messages.iteritems():
840
            try:
841
                self.file_messages[k] = v + '\n******\n' + self.file_messages[k]
842
            except KeyError:
843
                self.file_messages[k] = v
844
        if self.global_message:
635.2.13 by Vincent Ladeuil
Add NEWS entry and do some cleanup.
845
            self.global_message = global_message + '\n******\n' \
846
                + self.global_message
635.2.12 by Vincent Ladeuil
Implement commit message saving without modifying bzrlib.
847
        else:
848
            self.global_message = global_message
635.2.13 by Vincent Ladeuil
Add NEWS entry and do some cleanup.
849
635.2.12 by Vincent Ladeuil
Implement commit message saving without modifying bzrlib.
850
    def save(self, tree, branch):
851
        # We store in branch's config, which can be a problem if two gcommit
852
        # are done in two checkouts of one single branch (comments overwrite
853
        # each other). Ideally should be in working tree. But uncommit does
854
        # not always have a working tree, though it always has a branch.
855
        # 'tree' argument is for the future
856
        config = branch.get_config()
857
        # should it be named "gtk_" or some more neutral name ("gui_" ?) to
858
        # be compatible with qbzr in the future?
859
        config.set_user_option('gtk_global_commit_message', self.global_message)
635.2.13 by Vincent Ladeuil
Add NEWS entry and do some cleanup.
860
        # bencode() does not know unicode objects but set_user_option()
861
        # requires one:
862
        config.set_user_option(
863
            'gtk_file_commit_messages',
864
            bencode.bencode(self.file_messages).decode('UTF-8'))
635.2.12 by Vincent Ladeuil
Implement commit message saving without modifying bzrlib.
865
866
867
def save_commit_messages(local, master, old_revno, old_revid,
868
                         new_revno, new_revid):
869
    b = local
870
    if b is None:
871
        b = master
872
    mgr = SavedCommitMessagesManager(None, b)
873
    revid_iterator = b.repository.iter_reverse_revision_history(old_revid)
874
    cur_revno = old_revno
875
    new_revision_id = old_revid
876
    graph = b.repository.get_graph()
877
    for rev_id in revid_iterator:
878
        if cur_revno == new_revno:
879
            break
880
        cur_revno -= 1
881
        rev = b.repository.get_revision(rev_id)
882
        file_info = rev.properties.get('file-info', None)
883
        if file_info is None:
884
            file_info = {}
885
        else:
886
            file_info = bencode.bdecode(file_info.encode('UTF-8'))
887
        global_message = osutils.safe_unicode(rev.message)
888
        # Concatenate comment of the uncommitted revision
889
        mgr.insert(global_message, file_info)
890
891
        parents = graph.get_parent_map([rev_id]).get(rev_id, None)
892
        if not parents:
893
            continue
894
    mgr.save(None, b)