37
from bzrlib import bencode
39
from bzrlib.util import bencode
30
from bzrlib import errors, osutils
31
from bzrlib.trace import mutter
32
from bzrlib.util import bencode
41
34
from bzrlib.plugins.gtk import _i18n
42
from bzrlib.plugins.gtk.dialog import question_dialog
43
from bzrlib.plugins.gtk.errors import show_bzr_error
35
from dialog import error_dialog, question_dialog
36
from errors import show_bzr_error
107
_newline_variants_re = re.compile(r'\r\n?')
108
def _sanitize_and_decode_message(utf8_message):
109
"""Turn a utf-8 message into a sanitized Unicode message."""
110
fixed_newline = _newline_variants_re.sub('\n', utf8_message)
111
return fixed_newline.decode('utf-8')
114
100
class CommitDialog(gtk.Dialog):
115
101
"""Implementation of Commit."""
117
103
def __init__(self, wt, selected=None, parent=None):
118
gtk.Dialog.__init__(self, title="Commit to %s" % wt.basedir,
119
parent=parent, flags=0,)
120
self.connect('delete-event', self._on_delete_window)
104
gtk.Dialog.__init__(self, title="Commit - Olive",
107
buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
121
108
self._question_dialog = question_dialog
123
self.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_NORMAL)
126
111
# TODO: Do something with this value, it is used by Olive
127
112
# It used to set all changes but this one to False
204
188
self._basis_tree.lock_read()
206
190
from diff import iter_changes_to_status
207
saved_file_messages = self._saved_commit_messages_manager.get()[1]
208
191
for (file_id, real_path, change_type, display_path
209
192
) in iter_changes_to_status(self._basis_tree, self._wt):
210
193
if self._selected and real_path != self._selected:
215
default_message = saved_file_messages[file_id]
218
197
item_iter = store.append([
220
199
real_path.encode('UTF-8'),
222
201
display_path.encode('UTF-8'),
224
default_message, # Initial comment
203
'', # Initial comment
226
205
if self._selected and enabled:
227
206
initial_cursor = store.get_path(item_iter)
359
335
self._hpane.pack2(self._right_pane_table, resize=True, shrink=True)
361
337
def _construct_action_pane(self):
362
self._button_cancel = gtk.Button(stock=gtk.STOCK_CANCEL)
363
self._button_cancel.connect('clicked', self._on_cancel_clicked)
364
self._button_cancel.show()
365
self.action_area.pack_end(self._button_cancel)
366
338
self._button_commit = gtk.Button(_i18n("Comm_it"), use_underline=True)
367
339
self._button_commit.connect('clicked', self._on_commit_clicked)
368
340
self._button_commit.set_flags(gtk.CAN_DEFAULT)
656
627
if self._commit_all_changes or record[2]:# [2] checkbox
657
628
file_id = record[0] # [0] file_id
658
629
path = record[1] # [1] real path
660
file_message = _sanitize_and_decode_message(record[5])
630
file_message = record[5] # [5] commit message
661
631
files.append(path.decode('UTF-8'))
662
632
if self._enable_per_file_commits and file_message:
663
633
# All of this needs to be utf-8 information
664
file_message = file_message.encode('UTF-8')
665
634
file_info.append({'path':path, 'file_id':file_id,
666
635
'message':file_message})
667
636
file_info.sort(key=lambda x:(x['path'], x['file_id']))
674
def _on_cancel_clicked(self, button):
675
""" Cancel button clicked handler. """
679
def _on_delete_window(self, source, event):
680
""" Delete window handler. """
683
def _do_cancel(self):
684
"""If requested, saves commit messages when cancelling gcommit; they are re-used by a next gcommit"""
685
mgr = SavedCommitMessagesManager()
686
self._saved_commit_messages_manager = mgr
687
mgr.insert(self._get_global_commit_message(),
688
self._get_specific_files()[1])
689
if mgr.is_not_empty(): # maybe worth saving
690
response = self._question_dialog(
691
_i18n('Commit cancelled'),
692
_i18n('Do you want to save your commit messages ?'),
694
if response == gtk.RESPONSE_NO:
695
# save nothing and destroy old comments if any
696
mgr = SavedCommitMessagesManager()
697
mgr.save(self._wt, self._wt.branch)
698
self.response(gtk.RESPONSE_CANCEL) # close window
701
643
def _on_commit_clicked(self, button):
702
644
""" Commit button clicked handler. """
703
645
self._do_commit()
761
699
specific_files=specific_files,
762
700
revprops=revprops)
763
701
self.committed_revision_id = rev_id
764
# destroy old comments if any
765
SavedCommitMessagesManager().save(self._wt, self._wt.branch)
766
702
self.response(gtk.RESPONSE_OK)
768
704
def _get_global_commit_message(self):
769
705
buf = self._global_message_text_view.get_buffer()
770
706
start, end = buf.get_bounds()
771
text = buf.get_text(start, end)
772
return _sanitize_and_decode_message(text)
707
return buf.get_text(start, end).decode('utf-8')
774
709
def _set_global_commit_message(self, message):
775
710
"""Just a helper for the test suite."""
796
731
show_offset=False)
797
732
rev_dict['revision_id'] = rev.revision_id
801
class SavedCommitMessagesManager:
802
"""Save glogal and per-file commit messages.
804
Saves global commit message and utf-8 file_id->message dictionary
805
of per-file commit messages on disk. Re-reads them later for re-using.
808
def __init__(self, tree=None, branch=None):
809
"""If branch is None, builds empty messages, otherwise reads them
810
from branch's disk storage. 'tree' argument is for the future."""
812
self.global_message = u''
813
self.file_messages = {}
815
config = branch.get_config()._get_branch_data_config()
816
self.global_message = config.get_user_option(
817
'gtk_global_commit_message')
818
if self.global_message is None:
819
self.global_message = u''
820
file_messages = config.get_user_option('gtk_file_commit_messages')
821
if file_messages: # unicode and B-encoded:
822
self.file_messages = bencode.bdecode(
823
file_messages.encode('UTF-8'))
825
self.file_messages = {}
828
return self.global_message, self.file_messages
830
def is_not_empty(self):
831
return bool(self.global_message or self.file_messages)
833
def insert(self, global_message, file_info):
834
"""Formats per-file commit messages (list of dictionaries, one per file)
835
into one utf-8 file_id->message dictionary and merges this with
836
previously existing dictionary. Merges global commit message too."""
839
file_message = fi['message']
841
file_messages[fi['file_id']] = file_message # utf-8 strings
842
for k,v in file_messages.iteritems():
844
self.file_messages[k] = v + '\n******\n' + self.file_messages[k]
846
self.file_messages[k] = v
847
if self.global_message:
848
self.global_message = global_message + '\n******\n' \
849
+ self.global_message
851
self.global_message = global_message
853
def save(self, tree, branch):
854
# We store in branch's config, which can be a problem if two gcommit
855
# are done in two checkouts of one single branch (comments overwrite
856
# each other). Ideally should be in working tree. But uncommit does
857
# not always have a working tree, though it always has a branch.
858
# 'tree' argument is for the future
859
config = branch.get_config()
860
# should it be named "gtk_" or some more neutral name ("gui_" ?) to
861
# be compatible with qbzr in the future?
862
config.set_user_option('gtk_global_commit_message', self.global_message)
863
# bencode() does not know unicode objects but set_user_option()
865
config.set_user_option(
866
'gtk_file_commit_messages',
867
bencode.bencode(self.file_messages).decode('UTF-8'))
870
def save_commit_messages(local, master, old_revno, old_revid,
871
new_revno, new_revid):
875
mgr = SavedCommitMessagesManager(None, b)
876
revid_iterator = b.repository.iter_reverse_revision_history(old_revid)
877
cur_revno = old_revno
878
new_revision_id = old_revid
879
graph = b.repository.get_graph()
880
for rev_id in revid_iterator:
881
if cur_revno == new_revno:
884
rev = b.repository.get_revision(rev_id)
885
file_info = rev.properties.get('file-info', None)
886
if file_info is None:
889
file_info = bencode.bdecode(file_info.encode('UTF-8'))
890
global_message = osutils.safe_unicode(rev.message)
891
# Concatenate comment of the uncommitted revision
892
mgr.insert(global_message, file_info)
894
parents = graph.get_parent_map([rev_id]).get(rev_id, None)