30
from bzrlib import errors, osutils
31
from bzrlib.trace import mutter
32
from bzrlib.util import bencode
34
from bzrlib.plugins.gtk import _i18n
35
from dialog import error_dialog, question_dialog
36
from errors import show_bzr_error
35
from bzrlib import bencode
37
from bzrlib.util import bencode
39
from bzrlib.plugins.gtk.dialog import question_dialog
40
from bzrlib.plugins.gtk.errors import show_bzr_error
41
from bzrlib.plugins.gtk.i18n import _i18n
105
_newline_variants_re = re.compile(r'\r\n?')
106
def _sanitize_and_decode_message(utf8_message):
107
"""Turn a utf-8 message into a sanitized Unicode message."""
108
fixed_newline = _newline_variants_re.sub('\n', utf8_message)
109
return fixed_newline.decode('utf-8')
100
112
class CommitDialog(gtk.Dialog):
101
113
"""Implementation of Commit."""
103
115
def __init__(self, wt, selected=None, parent=None):
104
gtk.Dialog.__init__(self, title="Commit - Olive",
107
buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
116
gtk.Dialog.__init__(self, title="Commit to %s" % wt.basedir,
117
parent=parent, flags=0,)
118
self.connect('delete-event', self._on_delete_window)
108
119
self._question_dialog = question_dialog
121
self.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_NORMAL)
111
124
# TODO: Do something with this value, it is used by Olive
112
125
# It used to set all changes but this one to False
188
202
self._basis_tree.lock_read()
190
204
from diff import iter_changes_to_status
205
saved_file_messages = self._saved_commit_messages_manager.get()[1]
191
206
for (file_id, real_path, change_type, display_path
192
207
) in iter_changes_to_status(self._basis_tree, self._wt):
193
208
if self._selected and real_path != self._selected:
213
default_message = saved_file_messages[file_id]
197
216
item_iter = store.append([
199
218
real_path.encode('UTF-8'),
201
220
display_path.encode('UTF-8'),
203
'', # Initial comment
222
default_message, # Initial comment
205
224
if self._selected and enabled:
206
225
initial_cursor = store.get_path(item_iter)
338
357
self._hpane.pack2(self._right_pane_table, resize=True, shrink=True)
340
359
def _construct_action_pane(self):
360
self._button_cancel = gtk.Button(stock=gtk.STOCK_CANCEL)
361
self._button_cancel.connect('clicked', self._on_cancel_clicked)
362
self._button_cancel.show()
363
self.action_area.pack_end(self._button_cancel)
341
364
self._button_commit = gtk.Button(_i18n("Comm_it"), use_underline=True)
342
365
self._button_commit.connect('clicked', self._on_commit_clicked)
343
366
self._button_commit.set_flags(gtk.CAN_DEFAULT)
630
654
if self._commit_all_changes or record[2]:# [2] checkbox
631
655
file_id = record[0] # [0] file_id
632
656
path = record[1] # [1] real path
633
file_message = record[5] # [5] commit message
658
file_message = _sanitize_and_decode_message(record[5])
634
659
files.append(path.decode('UTF-8'))
635
660
if self._enable_per_file_commits and file_message:
636
661
# All of this needs to be utf-8 information
662
file_message = file_message.encode('UTF-8')
637
663
file_info.append({'path':path, 'file_id':file_id,
638
664
'message':file_message})
639
665
file_info.sort(key=lambda x:(x['path'], x['file_id']))
672
def _on_cancel_clicked(self, button):
673
""" Cancel button clicked handler. """
677
def _on_delete_window(self, source, event):
678
""" Delete window handler. """
681
def _do_cancel(self):
682
"""If requested, saves commit messages when cancelling gcommit; they are re-used by a next gcommit"""
683
mgr = SavedCommitMessagesManager()
684
self._saved_commit_messages_manager = mgr
685
mgr.insert(self._get_global_commit_message(),
686
self._get_specific_files()[1])
687
if mgr.is_not_empty(): # maybe worth saving
688
response = self._question_dialog(
689
_i18n('Commit cancelled'),
690
_i18n('Do you want to save your commit messages ?'),
692
if response == gtk.RESPONSE_NO:
693
# save nothing and destroy old comments if any
694
mgr = SavedCommitMessagesManager()
695
mgr.save(self._wt, self._wt.branch)
696
self.response(gtk.RESPONSE_CANCEL) # close window
646
699
def _on_commit_clicked(self, button):
647
700
""" Commit button clicked handler. """
648
701
self._do_commit()
702
759
specific_files=specific_files,
703
760
revprops=revprops)
704
761
self.committed_revision_id = rev_id
762
# destroy old comments if any
763
SavedCommitMessagesManager().save(self._wt, self._wt.branch)
705
764
self.response(gtk.RESPONSE_OK)
707
766
def _get_global_commit_message(self):
708
767
buf = self._global_message_text_view.get_buffer()
709
768
start, end = buf.get_bounds()
710
return buf.get_text(start, end).decode('utf-8')
769
text = buf.get_text(start, end)
770
return _sanitize_and_decode_message(text)
712
772
def _set_global_commit_message(self, message):
713
773
"""Just a helper for the test suite."""
734
794
show_offset=False)
735
795
rev_dict['revision_id'] = rev.revision_id
799
class SavedCommitMessagesManager:
800
"""Save glogal and per-file commit messages.
802
Saves global commit message and utf-8 file_id->message dictionary
803
of per-file commit messages on disk. Re-reads them later for re-using.
806
def __init__(self, tree=None, branch=None):
807
"""If branch is None, builds empty messages, otherwise reads them
808
from branch's disk storage. 'tree' argument is for the future."""
810
self.global_message = u''
811
self.file_messages = {}
813
config = branch.get_config()
814
self.global_message = config.get_user_option(
815
'gtk_global_commit_message')
816
if self.global_message is None:
817
self.global_message = u''
818
file_messages = config.get_user_option('gtk_file_commit_messages')
819
if file_messages: # unicode and B-encoded:
820
self.file_messages = bencode.bdecode(
821
file_messages.encode('UTF-8'))
823
self.file_messages = {}
826
return self.global_message, self.file_messages
828
def is_not_empty(self):
829
return bool(self.global_message or self.file_messages)
831
def insert(self, global_message, file_info):
832
"""Formats per-file commit messages (list of dictionaries, one per file)
833
into one utf-8 file_id->message dictionary and merges this with
834
previously existing dictionary. Merges global commit message too."""
837
file_message = fi['message']
839
file_messages[fi['file_id']] = file_message # utf-8 strings
840
for k,v in file_messages.iteritems():
842
self.file_messages[k] = v + '\n******\n' + self.file_messages[k]
844
self.file_messages[k] = v
845
if self.global_message:
846
self.global_message = global_message + '\n******\n' \
847
+ self.global_message
849
self.global_message = global_message
851
def save(self, tree, branch):
852
# We store in branch's config, which can be a problem if two gcommit
853
# are done in two checkouts of one single branch (comments overwrite
854
# each other). Ideally should be in working tree. But uncommit does
855
# not always have a working tree, though it always has a branch.
856
# 'tree' argument is for the future
857
config = branch.get_config()
858
# should it be named "gtk_" or some more neutral name ("gui_" ?) to
859
# be compatible with qbzr in the future?
860
config.set_user_option('gtk_global_commit_message', self.global_message)
861
# bencode() does not know unicode objects but set_user_option()
863
config.set_user_option(
864
'gtk_file_commit_messages',
865
bencode.bencode(self.file_messages).decode('UTF-8'))
868
def save_commit_messages(local, master, old_revno, old_revid,
869
new_revno, new_revid):
873
mgr = SavedCommitMessagesManager(None, b)
874
revid_iterator = b.repository.iter_reverse_revision_history(old_revid)
875
cur_revno = old_revno
876
new_revision_id = old_revid
877
graph = b.repository.get_graph()
878
for rev_id in revid_iterator:
879
if cur_revno == new_revno:
882
rev = b.repository.get_revision(rev_id)
883
file_info = rev.properties.get('file-info', None)
884
if file_info is None:
887
file_info = bencode.bdecode(file_info.encode('UTF-8'))
888
global_message = osutils.safe_unicode(rev.message)
889
# Concatenate comment of the uncommitted revision
890
mgr.insert(global_message, file_info)
892
parents = graph.get_parent_map([rev_id]).get(rev_id, None)