45
def _get_sorted_revisions(tip_revision, revision_ids, parent_map):
46
"""Get an iterator which will return the revisions in merge sorted order.
48
This will build up a list of all nodes, such that only nodes in the list
49
are referenced. It then uses MergeSorter to return them in 'merge-sorted'
52
:param revision_ids: A set of revision_ids
53
:param parent_map: The parent information for each node. Revisions which
54
are considered ghosts should not be present in the map.
55
:return: iterator from MergeSorter.iter_topo_order()
57
# MergeSorter requires that all nodes be present in the graph, so get rid
58
# of any references pointing outside of this graph.
60
for revision_id in revision_ids:
61
if revision_id not in parent_map: # ghost
62
parent_graph[revision_id] = []
64
# Only include parents which are in this sub-graph
65
parent_graph[revision_id] = [p for p in parent_map[revision_id]
67
sorter = tsort.MergeSorter(parent_graph, tip_revision)
68
return sorter.iter_topo_order()
45
71
def pending_revisions(wt):
46
72
"""Return a list of pending merges or None if there are none of them.
61
87
last_revision = parents[0]
63
if last_revision is not None:
65
ignore = set(branch.repository.get_ancestry(last_revision,
67
except errors.NoSuchRevision:
68
# the last revision is a ghost : assume everything is new
70
ignore = set([None, last_revision])
89
graph = branch.repository.get_graph()
90
other_revisions = [last_revision]
75
93
for merge in pending:
78
rev = branch.repository.get_revision(merge)
80
pm.append((rev, children))
82
# This does need to be topo sorted, so we search backwards
83
inner_merges = branch.repository.get_ancestry(merge)
84
assert inner_merges[0] is None
86
for mmerge in reversed(inner_merges):
89
rev = branch.repository.get_revision(mmerge)
93
except errors.NoSuchRevision:
94
print "DEBUG: NoSuchRevision:", merge
95
merge_rev = branch.repository.get_revision(merge)
96
except errors.NoSuchRevision:
97
# If we are missing a revision, just print out the revision id
98
trace.mutter("ghost: %r", merge)
99
other_revisions.append(merge)
102
# Find all of the revisions in the merge source, which are not in the
103
# last committed revision.
104
merge_extra = graph.find_unique_ancestors(merge, other_revisions)
105
other_revisions.append(merge)
106
merge_extra.discard(_mod_revision.NULL_REVISION)
108
# Get a handle to all of the revisions we will need
110
revisions = dict((rev.revision_id, rev) for rev in
111
branch.repository.get_revisions(merge_extra))
112
except errors.NoSuchRevision:
113
# One of the sub nodes is a ghost, check each one
115
for revision_id in merge_extra:
117
rev = branch.repository.get_revisions([revision_id])[0]
118
except errors.NoSuchRevision:
119
revisions[revision_id] = None
121
revisions[revision_id] = rev
123
# Display the revisions brought in by this merge.
124
rev_id_iterator = _get_sorted_revisions(merge, merge_extra,
125
branch.repository.get_parent_map(merge_extra))
126
# Skip the first node
127
num, first, depth, eom = rev_id_iterator.next()
129
raise AssertionError('Somehow we misunderstood how'
130
' iter_topo_order works %s != %s' % (first, merge))
132
for num, sub_merge, depth, eom in rev_id_iterator:
133
rev = revisions[sub_merge]
135
trace.warning("ghost: %r", sub_merge)
138
yield (merge_rev, children)
99
141
_newline_variants_re = re.compile(r'\r\n?')
100
142
def _sanitize_and_decode_message(utf8_message):
101
143
"""Turn a utf-8 message into a sanitized Unicode message."""
102
144
fixed_newline = _newline_variants_re.sub('\n', utf8_message)
103
return fixed_newline.decode('utf-8')
145
return osutils.safe_unicode(fixed_newline)
106
148
class CommitDialog(Gtk.Dialog):
107
149
"""Implementation of Commit."""
109
151
def __init__(self, wt, selected=None, parent=None):
110
GObject.GObject.__init__(self, title="Commit to %s" % wt.basedir,
111
parent=parent, flags=0,)
152
super(CommitDialog, self).__init__(
153
title="Commit to %s" % wt.basedir, parent=parent, flags=0)
112
154
self.connect('delete-event', self._on_delete_window)
113
155
self._question_dialog = question_dialog
280
333
"""Build up the dialog widgets."""
281
334
# The primary pane which splits it into left and right (adjustable)
283
self._hpane = Gtk.HPaned()
336
self._hpane = Gtk.Paned.new(Gtk.Orientation.HORIZONTAL)
285
338
self._construct_left_pane()
286
339
self._construct_right_pane()
287
340
self._construct_action_pane()
289
self.vbox.pack_start(self._hpane, True, True, 0)
342
self.get_content_area().pack_start(self._hpane, True, True, 0)
290
343
self._hpane.show()
291
344
self.set_focus(self._global_message_text_view)
354
407
self._button_cancel = Gtk.Button(stock=Gtk.STOCK_CANCEL)
355
408
self._button_cancel.connect('clicked', self._on_cancel_clicked)
356
409
self._button_cancel.show()
357
self.action_area.pack_end(self._button_cancel)
410
self.get_action_area().pack_end(
411
self._button_cancel, True, True, 0)
358
412
self._button_commit = Gtk.Button(_i18n("Comm_it"), use_underline=True)
359
413
self._button_commit.connect('clicked', self._on_commit_clicked)
360
414
self._button_commit.set_can_default(True)
361
415
self._button_commit.show()
362
self.action_area.pack_end(self._button_commit)
416
self.get_action_area().pack_end(
417
self._button_commit, True, True, 0)
363
418
self._button_commit.grab_default()
365
420
def _add_to_right_table(self, widget, weight, expanding=False):
385
440
# file_label.show()
386
441
self._files_box.pack_start(file_label, False, True, 0)
388
self._commit_all_files_radio = Gtk.RadioButton(
443
self._commit_all_files_radio = Gtk.RadioButton.new_with_label(
389
444
None, _i18n("Commit all changes"))
390
445
self._files_box.pack_start(self._commit_all_files_radio, False, True, 0)
391
446
self._commit_all_files_radio.show()
392
447
self._commit_all_files_radio.connect('toggled',
393
448
self._toggle_commit_selection)
394
self._commit_selected_radio = Gtk.RadioButton(
449
self._commit_selected_radio = Gtk.RadioButton.new_with_label_from_widget(
395
450
self._commit_all_files_radio, _i18n("Only commit selected changes"))
396
451
self._files_box.pack_start(self._commit_selected_radio, False, True, 0)
397
452
self._commit_selected_radio.show()
473
527
pending_message = Gtk.Label()
474
528
pending_message.set_markup(
475
529
_i18n('<i>* Cannot select specific files when merging</i>'))
476
self._pending_box.pack_start(pending_message, expand=False, padding=5)
530
self._pending_box.pack_start(pending_message, False, True, 5)
477
531
pending_message.show()
479
533
pending_label = Gtk.Label(label=_i18n('Pending Revisions'))
480
self._pending_box.pack_start(pending_label, expand=False, padding=0)
534
self._pending_box.pack_start(pending_label, False, True, 0)
481
535
pending_label.show()
483
537
scroller = Gtk.ScrolledWindow()
569
623
def _on_treeview_files_cursor_changed(self, treeview):
570
624
treeselection = treeview.get_selection()
625
if treeselection is None:
626
# The treeview was probably destroyed as the dialog closes.
571
628
(model, selection) = treeselection.get_selected()
573
630
if selection is not None:
574
631
path, display_path = model.get(selection, 1, 3)
575
632
self._diff_label.set_text(_i18n('Diff for ') + display_path)
577
634
self._diff_view.show_diff(None)
579
self._diff_view.show_diff([path.decode('UTF-8')])
636
self._diff_view.show_diff([osutils.safe_unicode(path)])
580
637
self._update_per_file_info(selection)
582
639
def _on_accel_next(self, accel_group, window, keyval, modifier):
593
650
# We have either made it to the end of the list, or nothing was
594
651
# selected. Either way, select All Files, and jump to the global
595
652
# commit message.
596
self._treeview_files.set_cursor((0,))
653
self._treeview_files.set_cursor(
654
Gtk.TreePath(path=0), "", False)
597
655
self._global_message_text_view.grab_focus()
599
657
# Set the cursor to this entry, and jump to the per-file commit
601
self._treeview_files.set_cursor(model.get_path(next))
659
self._treeview_files.set_cursor(model.get_path(next), None, False)
602
660
self._file_message_text_view.grab_focus()
604
662
def _save_current_file_message(self):
642
700
records = iter(self._files_store)
643
701
rec = records.next() # Skip the All Files record
644
assert rec[0] is None, "Are we skipping the wrong record?"
702
assert rec[0] == "", "Are we skipping the wrong record?"
647
705
for record in records:
648
706
if self._commit_all_changes or record[2]:# [2] checkbox
649
file_id = record[0] # [0] file_id
650
path = record[1] # [1] real path
707
file_id = osutils.safe_utf8(record[0]) # [0] file_id
708
path = osutils.safe_utf8(record[1]) # [1] real path
651
709
# [5] commit message
652
710
file_message = _sanitize_and_decode_message(record[5])
653
711
files.append(path.decode('UTF-8'))
789
847
rev_dict['revision_id'] = rev.revision_id
793
class SavedCommitMessagesManager:
794
"""Save glogal and per-file commit messages.
796
Saves global commit message and utf-8 file_id->message dictionary
797
of per-file commit messages on disk. Re-reads them later for re-using.
800
def __init__(self, tree=None, branch=None):
801
"""If branch is None, builds empty messages, otherwise reads them
802
from branch's disk storage. 'tree' argument is for the future."""
804
self.global_message = u''
805
self.file_messages = {}
807
config = branch.get_config()
808
self.global_message = config.get_user_option(
809
'gtk_global_commit_message')
810
if self.global_message is None:
811
self.global_message = u''
812
file_messages = config.get_user_option('gtk_file_commit_messages')
813
if file_messages: # unicode and B-encoded:
814
self.file_messages = bencode.bdecode(
815
file_messages.encode('UTF-8'))
817
self.file_messages = {}
820
return self.global_message, self.file_messages
822
def is_not_empty(self):
823
return bool(self.global_message or self.file_messages)
825
def insert(self, global_message, file_info):
826
"""Formats per-file commit messages (list of dictionaries, one per file)
827
into one utf-8 file_id->message dictionary and merges this with
828
previously existing dictionary. Merges global commit message too."""
831
file_message = fi['message']
833
file_messages[fi['file_id']] = file_message # utf-8 strings
834
for k,v in file_messages.iteritems():
836
self.file_messages[k] = v + '\n******\n' + self.file_messages[k]
838
self.file_messages[k] = v
839
if self.global_message:
840
self.global_message = global_message + '\n******\n' \
841
+ self.global_message
843
self.global_message = global_message
845
def save(self, tree, branch):
846
# We store in branch's config, which can be a problem if two gcommit
847
# are done in two checkouts of one single branch (comments overwrite
848
# each other). Ideally should be in working tree. But uncommit does
849
# not always have a working tree, though it always has a branch.
850
# 'tree' argument is for the future
851
config = branch.get_config()
852
# should it be named "gtk_" or some more neutral name ("gui_" ?) to
853
# be compatible with qbzr in the future?
854
config.set_user_option('gtk_global_commit_message', self.global_message)
855
# bencode() does not know unicode objects but set_user_option()
857
config.set_user_option(
858
'gtk_file_commit_messages',
859
bencode.bencode(self.file_messages).decode('UTF-8'))
862
def save_commit_messages(local, master, old_revno, old_revid,
863
new_revno, new_revid):
867
mgr = SavedCommitMessagesManager(None, b)
868
revid_iterator = b.repository.iter_reverse_revision_history(old_revid)
869
cur_revno = old_revno
870
new_revision_id = old_revid
871
graph = b.repository.get_graph()
872
for rev_id in revid_iterator:
873
if cur_revno == new_revno:
876
rev = b.repository.get_revision(rev_id)
877
file_info = rev.properties.get('file-info', None)
878
if file_info is None:
881
file_info = bencode.bdecode(file_info.encode('UTF-8'))
882
global_message = osutils.safe_unicode(rev.message)
883
# Concatenate comment of the uncommitted revision
884
mgr.insert(global_message, file_info)
886
parents = graph.get_parent_map([rev_id]).get(rev_id, None)