1
# Copyright (C) 2006 by Szilveszter Farkas (Phanatic) <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.
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.
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
36
from bzrlib.util import bencode
38
from bzrlib.plugins.gtk import _i18n
39
from bzrlib.plugins.gtk.dialog import question_dialog
40
from bzrlib.plugins.gtk.errors import show_bzr_error
50
def pending_revisions(wt):
51
"""Return a list of pending merges or None if there are none of them.
53
Arguably this should be a core function, and
54
``bzrlib.status.show_pending_merges`` should be built on top of it.
56
:return: [(rev, [children])]
58
parents = wt.get_parent_ids()
62
# The basic pending merge algorithm uses the same algorithm as
63
# bzrlib.status.show_pending_merges
66
last_revision = parents[0]
68
if last_revision is not None:
70
ignore = set(branch.repository.get_ancestry(last_revision,
72
except errors.NoSuchRevision:
73
# the last revision is a ghost : assume everything is new
75
ignore = set([None, last_revision])
83
rev = branch.repository.get_revision(merge)
85
pm.append((rev, children))
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
91
for mmerge in reversed(inner_merges):
94
rev = branch.repository.get_revision(mmerge)
98
except errors.NoSuchRevision:
99
print "DEBUG: NoSuchRevision:", merge
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')
111
class CommitDialog(gtk.Dialog):
112
"""Implementation of Commit."""
114
def __init__(self, wt, selected=None, parent=None):
115
gtk.Dialog.__init__(self, title="Commit to %s" % wt.basedir,
116
parent=parent, flags=0,)
117
self.connect('delete-event', self._on_delete_window)
118
self._question_dialog = question_dialog
120
self.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_NORMAL)
123
# TODO: Do something with this value, it is used by Olive
124
# It used to set all changes but this one to False
125
self._selected = selected
126
self._enable_per_file_commits = True
127
self._commit_all_changes = True
128
self.committed_revision_id = None # Nothing has been committed yet
129
self._saved_commit_messages_manager = SavedCommitMessagesManager(self._wt, self._wt.branch)
135
def setup_params(self):
136
"""Setup the member variables for state."""
137
self._basis_tree = self._wt.basis_tree()
139
self._pending = pending_revisions(self._wt)
141
self._is_checkout = (self._wt.branch.get_bound_location() is not None)
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()
147
self._fill_in_files()
148
self._fill_in_checkout()
149
self._fill_in_per_file_info()
151
def _fill_in_pending(self):
152
if not self._pending:
153
self._pending_box.hide()
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)
159
self._pending_store.append([
160
rev_info['revision_id'],
162
rev_info['committer'],
165
for child in children:
166
rev_info = self._rev_to_pending_info(child)
167
self._pending_store.append([
168
rev_info['revision_id'],
170
rev_info['committer'],
173
self._pending_box.show()
175
def _fill_in_files(self):
176
# We should really use add a progress bar of some kind.
177
# While we fill in the view, hide the store
178
store = self._files_store
179
self._treeview_files.set_model(None)
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')
189
# [file_id, real path, checkbox, display path, changes type, message]
190
# iter_changes returns:
191
# (file_id, (path_in_source, path_in_target),
192
# changed_content, versioned, parent, name, kind,
195
all_enabled = (self._selected is None)
196
# The first entry is always the 'whole tree'
197
all_iter = store.append([None, None, all_enabled, 'All Files', '', ''])
198
initial_cursor = store.get_path(all_iter)
199
# should we pass specific_files?
201
self._basis_tree.lock_read()
203
from diff import iter_changes_to_status
204
saved_file_messages = self._saved_commit_messages_manager.get()[1]
205
for (file_id, real_path, change_type, display_path
206
) in iter_changes_to_status(self._basis_tree, self._wt):
207
if self._selected and real_path != self._selected:
212
default_message = saved_file_messages[file_id]
215
item_iter = store.append([
217
real_path.encode('UTF-8'),
219
display_path.encode('UTF-8'),
221
default_message, # Initial comment
223
if self._selected and enabled:
224
initial_cursor = store.get_path(item_iter)
226
self._basis_tree.unlock()
229
self._treeview_files.set_model(store)
230
self._last_selected_file = None
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
234
self._treeview_files.set_cursor(initial_cursor)
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)
240
def _fill_in_diff(self):
241
self._diff_view.set_trees(self._wt, self._basis_tree)
243
def _fill_in_checkout(self):
244
if not self._is_checkout:
245
self._check_local.hide()
248
bus = dbus.SystemBus()
250
proxy_obj = bus.get_object('org.freedesktop.NetworkManager',
251
'/org/freedesktop/NetworkManager')
252
except dbus.DBusException:
253
trace.mutter("networkmanager not available.")
254
self._check_local.show()
257
dbus_iface = dbus.Interface(proxy_obj,
258
'org.freedesktop.NetworkManager')
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
265
trace.mutter("unable to get networkmanager state: %r" % e)
266
self._check_local.show()
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
276
self._enable_per_file_commits = True
277
if not self._enable_per_file_commits:
278
self._file_message_expander.hide()
279
self._global_message_label.set_markup(_i18n('<b>Commit Message</b>'))
281
def _compute_delta(self):
282
self._delta = self._wt.changes_from(self._basis_tree)
285
"""Build up the dialog widgets."""
286
# The primary pane which splits it into left and right (adjustable)
288
self._hpane = gtk.HPaned()
290
self._construct_left_pane()
291
self._construct_right_pane()
292
self._construct_action_pane()
294
self.vbox.pack_start(self._hpane)
296
self.set_focus(self._global_message_text_view)
298
self._construct_accelerators()
301
def _set_sizes(self):
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
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)
316
self._hpane.set_position(300)
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)
324
# ignore the escape key (avoid closing the window)
325
self.connect_object('close', self.emit_stop_by_name, 'close')
327
def _construct_left_pane(self):
328
self._left_pane_box = gtk.VBox(homogeneous=False, spacing=5)
329
self._construct_file_list()
330
self._construct_pending_list()
332
self._check_local = gtk.CheckButton(_i18n("_Only commit locally"),
334
self._left_pane_box.pack_end(self._check_local, False, False)
335
self._check_local.set_active(False)
337
self._hpane.pack1(self._left_pane_box, resize=False, shrink=False)
338
self._left_pane_box.show()
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...)
347
self._right_pane_table = gtk.Table(rows=10, columns=1, homogeneous=False)
348
self._right_pane_table.set_row_spacings(5)
349
self._right_pane_table.set_col_spacings(5)
350
self._right_pane_table_row = 0
351
self._construct_diff_view()
352
self._construct_file_message()
353
self._construct_global_message()
355
self._right_pane_table.show()
356
self._hpane.pack2(self._right_pane_table, resize=True, shrink=True)
358
def _construct_action_pane(self):
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)
363
self._button_commit = gtk.Button(_i18n("Comm_it"), use_underline=True)
364
self._button_commit.connect('clicked', self._on_commit_clicked)
365
self._button_commit.set_flags(gtk.CAN_DEFAULT)
366
self._button_commit.show()
367
self.action_area.pack_end(self._button_commit)
368
self._button_commit.grab_default()
370
def _add_to_right_table(self, widget, weight, expanding=False):
371
"""Add another widget to the table
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?
377
end_row = self._right_pane_table_row + weight
379
expand_opts = gtk.EXPAND | gtk.FILL | gtk.SHRINK
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
387
def _construct_file_list(self):
388
self._files_box = gtk.VBox(homogeneous=False, spacing=0)
389
file_label = gtk.Label(_i18n('Files'))
391
self._files_box.pack_start(file_label, expand=False)
393
self._commit_all_files_radio = gtk.RadioButton(
394
None, _i18n("Commit all changes"))
395
self._files_box.pack_start(self._commit_all_files_radio, expand=False)
396
self._commit_all_files_radio.show()
397
self._commit_all_files_radio.connect('toggled',
398
self._toggle_commit_selection)
399
self._commit_selected_radio = gtk.RadioButton(
400
self._commit_all_files_radio, _i18n("Only commit selected changes"))
401
self._files_box.pack_start(self._commit_selected_radio, expand=False)
402
self._commit_selected_radio.show()
403
self._commit_selected_radio.connect('toggled',
404
self._toggle_commit_selection)
406
self._commit_all_files_radio.set_label(_i18n('Commit all changes*'))
407
self._commit_all_files_radio.set_sensitive(False)
408
self._commit_selected_radio.set_sensitive(False)
410
scroller = gtk.ScrolledWindow()
411
scroller.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
412
self._treeview_files = gtk.TreeView()
413
self._treeview_files.show()
414
scroller.add(self._treeview_files)
415
scroller.set_shadow_type(gtk.SHADOW_IN)
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)
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
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
431
gobject.TYPE_STRING, # [5] commit message
433
self._files_store = liststore
434
self._treeview_files.set_model(liststore)
435
crt = gtk.CellRendererToggle()
436
crt.set_property('activatable', not bool(self._pending))
437
crt.connect("toggled", self._toggle_commit, self._files_store)
439
name = _i18n('Commit*')
441
name = _i18n('Commit')
442
commit_col = gtk.TreeViewColumn(name, crt, active=2)
443
commit_col.set_visible(False)
444
self._treeview_files.append_column(commit_col)
445
self._treeview_files.append_column(gtk.TreeViewColumn(_i18n('Path'),
446
gtk.CellRendererText(), text=3))
447
self._treeview_files.append_column(gtk.TreeViewColumn(_i18n('Type'),
448
gtk.CellRendererText(), text=4))
449
self._treeview_files.connect('cursor-changed',
450
self._on_treeview_files_cursor_changed)
452
def _toggle_commit(self, cell, path, model):
453
if model[path][0] is None: # No file_id means 'All Files'
454
new_val = not model[path][2]
458
model[path][2] = not model[path][2]
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
466
checked_col.set_visible(False)
468
checked_col.set_visible(True)
469
renderer = checked_col.get_cell_renderers()[0]
470
renderer.set_property('activatable', not all_files)
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()
478
pending_message = gtk.Label()
479
pending_message.set_markup(
480
_i18n('<i>* Cannot select specific files when merging</i>'))
481
self._pending_box.pack_start(pending_message, expand=False, padding=5)
482
pending_message.show()
484
pending_label = gtk.Label(_i18n('Pending Revisions'))
485
self._pending_box.pack_start(pending_label, expand=False, padding=0)
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)
492
scroller.set_shadow_type(gtk.SHADOW_IN)
494
self._pending_box.pack_start(scroller,
495
expand=True, fill=True, padding=5)
496
self._treeview_pending.show()
497
self._left_pane_box.pack_start(self._pending_box)
499
liststore = gtk.ListStore(gobject.TYPE_STRING, # revision_id
500
gobject.TYPE_STRING, # date
501
gobject.TYPE_STRING, # committer
502
gobject.TYPE_STRING, # summary
504
self._pending_store = liststore
505
self._treeview_pending.set_model(liststore)
506
self._treeview_pending.append_column(gtk.TreeViewColumn(_i18n('Date'),
507
gtk.CellRendererText(), text=1))
508
self._treeview_pending.append_column(gtk.TreeViewColumn(_i18n('Committer'),
509
gtk.CellRendererText(), text=2))
510
self._treeview_pending.append_column(gtk.TreeViewColumn(_i18n('Summary'),
511
gtk.CellRendererText(), text=3))
513
def _construct_diff_view(self):
514
from diff import DiffView
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.
520
self._diff_label = gtk.Label(_i18n('Diff for whole tree'))
521
self._diff_label.set_alignment(0, 0)
522
self._right_pane_table.set_row_spacing(self._right_pane_table_row, 0)
523
self._add_to_right_table(self._diff_label, 1, False)
524
# self._diff_label.show()
526
self._diff_view = DiffView()
527
self._add_to_right_table(self._diff_view, 4, True)
528
self._diff_view.show()
530
def _construct_file_message(self):
531
scroller = gtk.ScrolledWindow()
532
scroller.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
534
self._file_message_text_view = gtk.TextView()
535
scroller.add(self._file_message_text_view)
536
scroller.set_shadow_type(gtk.SHADOW_IN)
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()
544
self._file_message_expander = gtk.Expander(_i18n('File commit message'))
545
self._file_message_expander.set_expanded(True)
546
self._file_message_expander.add(scroller)
547
self._add_to_right_table(self._file_message_expander, 1, False)
548
self._file_message_expander.show()
550
def _construct_global_message(self):
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>'))
554
self._global_message_label.set_alignment(0, 0)
555
self._right_pane_table.set_row_spacing(self._right_pane_table_row, 0)
556
self._add_to_right_table(self._global_message_label, 1, False)
557
# Can we remove the spacing between the label and the box?
558
self._global_message_label.show()
560
scroller = gtk.ScrolledWindow()
561
scroller.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
563
self._global_message_text_view = gtk.TextView()
564
self._set_global_commit_message(self._saved_commit_messages_manager.get()[0])
565
self._global_message_text_view.modify_font(pango.FontDescription("Monospace"))
566
scroller.add(self._global_message_text_view)
567
scroller.set_shadow_type(gtk.SHADOW_IN)
569
self._add_to_right_table(scroller, 2, True)
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()
574
def _on_treeview_files_cursor_changed(self, treeview):
575
treeselection = treeview.get_selection()
576
(model, selection) = treeselection.get_selected()
578
if selection is not None:
579
path, display_path = model.get(selection, 1, 3)
580
self._diff_label.set_text(_i18n('Diff for ') + display_path)
582
self._diff_view.show_diff(None)
584
self._diff_view.show_diff([path.decode('UTF-8')])
585
self._update_per_file_info(selection)
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:
595
next = model.iter_next(selection)
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
601
self._treeview_files.set_cursor((0,))
602
self._global_message_text_view.grab_focus()
604
# Set the cursor to this entry, and jump to the per-file commit
606
self._treeview_files.set_cursor(model.get_path(next))
607
self._file_message_text_view.grab_focus()
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)
618
def _update_per_file_info(self, selection):
619
# The node is changing, so cache the current message
620
if not self._enable_per_file_commits:
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
627
self._file_message_expander.set_label(_i18n('File commit message'))
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
633
self._file_message_expander.set_label(_i18n('Commit message for ')
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)
640
def _get_specific_files(self):
641
"""Return the list of selected paths, and file info.
643
:return: ([unicode paths], [{utf-8 file info}]
645
self._save_current_file_message()
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?"
652
for record in records:
653
if self._commit_all_changes or record[2]:# [2] checkbox
654
file_id = record[0] # [0] file_id
655
path = record[1] # [1] real path
657
file_message = _sanitize_and_decode_message(record[5])
658
files.append(path.decode('UTF-8'))
659
if self._enable_per_file_commits and file_message:
660
# All of this needs to be utf-8 information
661
file_message = file_message.encode('UTF-8')
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']))
665
if self._enable_per_file_commits:
666
return files, file_info
671
def _on_cancel_clicked(self, button):
672
""" Cancel button clicked handler. """
676
def _on_delete_window(self, source, event):
677
""" Delete window handler. """
680
def _do_cancel(self):
681
"""If requested, saves commit messages when cancelling gcommit; they are re-used by a next gcommit"""
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 ?'),
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)
695
self.response(gtk.RESPONSE_CANCEL) # close window
698
def _on_commit_clicked(self, button):
699
""" Commit button clicked handler. """
702
def _do_commit(self):
703
message = self._get_global_commit_message()
706
response = self._question_dialog(
707
_i18n('Commit with an empty message?'),
708
_i18n('You can describe your commit intent in the message.'),
710
if response == gtk.RESPONSE_NO:
711
# Kindly give focus to message area
712
self._global_message_text_view.grab_focus()
715
specific_files, file_info = self._get_specific_files()
717
specific_files = None
719
local = self._check_local.get_active()
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():
727
response = self._question_dialog(
728
_i18n("Commit with unknowns?"),
729
_i18n("Unknown files exist in the working tree. Commit anyway?"),
731
# Doesn't set a parent for the dialog..
732
if response == gtk.RESPONSE_NO:
739
revprops['file-info'] = bencode.bencode(file_info).decode('UTF-8')
741
rev_id = self._wt.commit(message,
742
allow_pointless=False,
745
specific_files=specific_files,
747
except errors.PointlessCommit:
748
response = self._question_dialog(
749
_i18n('Commit with no changes?'),
750
_i18n('There are no changes in the working tree.'
751
' Do you want to commit anyway?'),
753
if response == gtk.RESPONSE_YES:
754
rev_id = self._wt.commit(message,
755
allow_pointless=True,
758
specific_files=specific_files,
760
self.committed_revision_id = rev_id
761
# destroy old comments if any
762
SavedCommitMessagesManager().save(self._wt, self._wt.branch)
763
self.response(gtk.RESPONSE_OK)
765
def _get_global_commit_message(self):
766
buf = self._global_message_text_view.get_buffer()
767
start, end = buf.get_bounds()
768
text = buf.get_text(start, end)
769
return _sanitize_and_decode_message(text)
771
def _set_global_commit_message(self, message):
772
"""Just a helper for the test suite."""
773
if isinstance(message, unicode):
774
message = message.encode('UTF-8')
775
self._global_message_text_view.get_buffer().set_text(message)
777
def _set_file_commit_message(self, message):
778
"""Helper for the test suite."""
779
if isinstance(message, unicode):
780
message = message.encode('UTF-8')
781
self._file_message_text_view.get_buffer().set_text(message)
784
def _rev_to_pending_info(rev):
785
"""Get the information from a pending merge."""
786
from bzrlib.osutils import format_date
788
rev_dict['committer'] = re.sub('<.*@.*>', '', rev.committer).strip(' ')
789
rev_dict['summary'] = rev.get_summary()
790
rev_dict['date'] = format_date(rev.timestamp,
792
'original', date_fmt="%Y-%m-%d",
794
rev_dict['revision_id'] = rev.revision_id
798
class SavedCommitMessagesManager:
799
"""Save glogal and per-file commit messages.
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.
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."""
809
self.global_message = u''
810
self.file_messages = {}
812
config = branch.get_config()._get_branch_data_config()
813
self.global_message = config.get_user_option(
814
'gtk_global_commit_message')
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:
819
self.file_messages = bencode.bdecode(
820
file_messages.encode('UTF-8'))
822
self.file_messages = {}
825
return self.global_message, self.file_messages
827
def is_not_empty(self):
828
return bool(self.global_message or self.file_messages)
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."""
836
file_message = fi['message']
838
file_messages[fi['file_id']] = file_message # utf-8 strings
839
for k,v in file_messages.iteritems():
841
self.file_messages[k] = v + '\n******\n' + self.file_messages[k]
843
self.file_messages[k] = v
844
if self.global_message:
845
self.global_message = global_message + '\n******\n' \
846
+ self.global_message
848
self.global_message = global_message
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)
860
# bencode() does not know unicode objects but set_user_option()
862
config.set_user_option(
863
'gtk_file_commit_messages',
864
bencode.bencode(self.file_messages).decode('UTF-8'))
867
def save_commit_messages(local, master, old_revno, old_revid,
868
new_revno, new_revid):
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:
881
rev = b.repository.get_revision(rev_id)
882
file_info = rev.properties.get('file-info', None)
883
if file_info is None:
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)
891
parents = graph.get_parent_map([rev_id]).get(rev_id, None)