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
29
import bzrlib.errors as errors
30
from bzrlib import osutils
32
from dialog import error_dialog, question_dialog
33
from errors import show_bzr_error
42
class CommitDialog(gtk.Dialog):
43
""" New implementation of the Commit dialog. """
44
def __init__(self, wt, wtpath, notbranch, selected=None, parent=None):
45
""" Initialize the Commit Dialog. """
46
gtk.Dialog.__init__(self, title="Commit - Olive",
49
buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
54
self.notbranch = notbranch
55
self.selected = selected
58
self.old_tree = self.wt.branch.repository.revision_tree(self.wt.branch.last_revision())
59
self.delta = self.wt.changes_from(self.old_tree)
62
self.pending = self._pending_merges(self.wt)
64
# Do some preliminary checks
65
self._is_checkout = False
66
self._is_pending = False
67
if self.wt is None and not self.notbranch:
68
error_dialog(_('Directory does not have a working tree'),
69
_('Operation aborted.'))
74
error_dialog(_('Directory is not a branch'),
75
_('You can perform this action only in a branch.'))
79
if self.wt.branch.get_bound_location() is not None:
80
# we have a checkout, so the local commit checkbox must appear
81
self._is_checkout = True
84
# There are pending merges, file selection not supported
85
self._is_pending = True
88
self._button_commit = gtk.Button(_("Comm_it"), use_underline=True)
89
self._expander_files = gtk.Expander(_("File(s) to commit"))
90
self._vpaned_main = gtk.VPaned()
91
self._scrolledwindow_files = gtk.ScrolledWindow()
92
self._scrolledwindow_message = gtk.ScrolledWindow()
93
self._treeview_files = gtk.TreeView()
94
self._vbox_message = gtk.VBox()
95
self._label_message = gtk.Label(_("Commit message:"))
96
self._textview_message = gtk.TextView()
99
self._expander_merges = gtk.Expander(_("Pending merges"))
100
self._vpaned_list = gtk.VPaned()
101
self._scrolledwindow_merges = gtk.ScrolledWindow()
102
self._treeview_merges = gtk.TreeView()
105
self._button_commit.connect('clicked', self._on_commit_clicked)
106
self._treeview_files.connect('row_activated', self._on_treeview_files_row_activated)
109
self._scrolledwindow_files.set_policy(gtk.POLICY_AUTOMATIC,
110
gtk.POLICY_AUTOMATIC)
111
self._scrolledwindow_message.set_policy(gtk.POLICY_AUTOMATIC,
112
gtk.POLICY_AUTOMATIC)
113
self._textview_message.modify_font(pango.FontDescription("Monospace"))
114
self.set_default_size(500, 500)
115
self._vpaned_main.set_position(200)
116
self._button_commit.set_flags(gtk.CAN_DEFAULT)
119
self._scrolledwindow_merges.set_policy(gtk.POLICY_AUTOMATIC,
120
gtk.POLICY_AUTOMATIC)
121
self._treeview_files.set_sensitive(False)
123
# Construct the dialog
124
self.action_area.pack_end(self._button_commit)
126
self._scrolledwindow_files.add(self._treeview_files)
127
self._scrolledwindow_message.add(self._textview_message)
129
self._expander_files.add(self._scrolledwindow_files)
131
self._vbox_message.pack_start(self._label_message, False, False)
132
self._vbox_message.pack_start(self._scrolledwindow_message, True, True)
135
self._expander_merges.add(self._scrolledwindow_merges)
136
self._scrolledwindow_merges.add(self._treeview_merges)
137
self._vpaned_list.add1(self._expander_files)
138
self._vpaned_list.add2(self._expander_merges)
139
self._vpaned_main.add1(self._vpaned_list)
141
self._vpaned_main.add1(self._expander_files)
143
self._vpaned_main.add2(self._vbox_message)
145
self.vbox.pack_start(self._vpaned_main, True, True)
146
if self._is_checkout:
147
self._check_local = gtk.CheckButton(_("_Only commit locally"),
149
self.vbox.pack_start(self._check_local, False, False)
151
bus = dbus.SystemBus()
152
proxy_obj = bus.get_object('org.freedesktop.NetworkManager',
153
'/org/freedesktop/NetworkManager')
154
dbus_iface = dbus.Interface(
155
proxy_obj, 'org.freedesktop.NetworkManager')
156
# 3 is the enum value for STATE_CONNECTED
157
self._check_local.set_active(dbus_iface.state() != 3)
159
# Create the file list
160
self._create_file_view()
161
# Create the pending merges
162
self._create_pending_merges()
164
# Expand the corresponding expander
166
self._expander_merges.set_expanded(True)
168
self._expander_files.set_expanded(True)
173
# Default to Commit button
174
self._button_commit.grab_default()
176
def _on_treeview_files_row_activated(self, treeview, path, view_column):
177
# FIXME: the diff window freezes for some reason
178
treeselection = treeview.get_selection()
179
(model, iter) = treeselection.get_selected()
182
from diff import DiffWindow
184
_selected = model.get_value(iter, 1)
187
diff.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_DIALOG)
189
parent_tree = self.wt.branch.repository.revision_tree(self.wt.branch.last_revision())
190
diff.set_diff(self.wt.branch.nick, self.wt, parent_tree)
192
diff.set_file(_selected)
193
except errors.NoSuchFile:
198
def _on_commit_clicked(self, button):
199
""" Commit button clicked handler. """
200
textbuffer = self._textview_message.get_buffer()
201
start, end = textbuffer.get_bounds()
202
message = textbuffer.get_text(start, end).decode('utf-8')
205
specific_files = self._get_specific_files()
207
specific_files = None
210
response = question_dialog(_('Commit with an empty message?'),
211
_('You can describe your commit intent in the message.'))
212
if response == gtk.RESPONSE_NO:
213
# Kindly give focus to message area
214
self._textview_message.grab_focus()
217
if self._is_checkout:
218
local = self._check_local.get_active()
222
if list(self.wt.unknowns()) != []:
223
response = question_dialog(_("Commit with unknowns?"),
224
_("Unknown files exist in the working tree. Commit anyway?"))
225
if response == gtk.RESPONSE_NO:
229
self.wt.commit(message,
230
allow_pointless=False,
233
specific_files=specific_files)
234
except errors.PointlessCommit:
235
response = question_dialog(_('Commit with no changes?'),
236
_('There are no changes in the working tree.'))
237
if response == gtk.RESPONSE_YES:
238
self.wt.commit(message,
239
allow_pointless=True,
242
specific_files=specific_files)
243
self.response(gtk.RESPONSE_OK)
245
def _pending_merges(self, wt):
246
""" Return a list of pending merges or None if there are none of them. """
247
parents = wt.get_parent_ids()
252
from bzrlib.osutils import format_date
254
pending = parents[1:]
256
last_revision = parents[0]
258
if last_revision is not None:
260
ignore = set(branch.repository.get_ancestry(last_revision))
261
except errors.NoSuchRevision:
262
# the last revision is a ghost : assume everything is new
264
ignore = set([None, last_revision])
269
for merge in pending:
272
m_revision = branch.repository.get_revision(merge)
275
rev['committer'] = re.sub('<.*@.*>', '', m_revision.committer).strip(' ')
276
rev['summary'] = m_revision.get_summary()
277
rev['date'] = format_date(m_revision.timestamp,
278
m_revision.timezone or 0,
279
'original', date_fmt="%Y-%m-%d",
284
inner_merges = branch.repository.get_ancestry(merge)
285
assert inner_merges[0] is None
287
inner_merges.reverse()
288
for mmerge in inner_merges:
291
mm_revision = branch.repository.get_revision(mmerge)
294
rev['committer'] = re.sub('<.*@.*>', '', mm_revision.committer).strip(' ')
295
rev['summary'] = mm_revision.get_summary()
296
rev['date'] = format_date(mm_revision.timestamp,
297
mm_revision.timezone or 0,
298
'original', date_fmt="%Y-%m-%d",
304
except errors.NoSuchRevision:
305
print "DEBUG: NoSuchRevision:", merge
309
def _create_file_view(self):
310
self._file_store = gtk.ListStore(gobject.TYPE_BOOLEAN, # [0] checkbox
311
gobject.TYPE_STRING, # [1] path to display
312
gobject.TYPE_STRING, # [2] changes type
313
gobject.TYPE_STRING) # [3] real path
314
self._treeview_files.set_model(self._file_store)
315
crt = gtk.CellRendererToggle()
316
crt.set_property("activatable", True)
317
crt.connect("toggled", self._toggle_commit, self._file_store)
318
self._treeview_files.append_column(gtk.TreeViewColumn(_('Commit'),
320
self._treeview_files.append_column(gtk.TreeViewColumn(_('Path'),
321
gtk.CellRendererText(), text=1))
322
self._treeview_files.append_column(gtk.TreeViewColumn(_('Type'),
323
gtk.CellRendererText(), text=2))
325
for path, id, kind in self.delta.added:
326
marker = osutils.kind_marker(kind)
327
if self.selected is not None:
328
if path == os.path.join(self.wtpath, self.selected):
329
self._file_store.append([ True, path+marker, _('added'), path ])
331
self._file_store.append([ False, path+marker, _('added'), path ])
333
self._file_store.append([ True, path+marker, _('added'), path ])
335
for path, id, kind in self.delta.removed:
336
marker = osutils.kind_marker(kind)
337
if self.selected is not None:
338
if path == os.path.join(self.wtpath, self.selected):
339
self._file_store.append([ True, path+marker, _('removed'), path ])
341
self._file_store.append([ False, path+marker, _('removed'), path ])
343
self._file_store.append([ True, path+marker, _('removed'), path ])
345
for oldpath, newpath, id, kind, text_modified, meta_modified in self.delta.renamed:
346
marker = osutils.kind_marker(kind)
347
if text_modified or meta_modified:
348
changes = _('renamed and modified')
350
changes = _('renamed')
351
if self.selected is not None:
352
if newpath == os.path.join(self.wtpath, self.selected):
353
self._file_store.append([ True,
354
oldpath+marker + ' => ' + newpath+marker,
359
self._file_store.append([ False,
360
oldpath+marker + ' => ' + newpath+marker,
365
self._file_store.append([ True,
366
oldpath+marker + ' => ' + newpath+marker,
371
for path, id, kind, text_modified, meta_modified in self.delta.modified:
372
marker = osutils.kind_marker(kind)
373
if self.selected is not None:
374
if path == os.path.join(self.wtpath, self.selected):
375
self._file_store.append([ True, path+marker, _('modified'), path ])
377
self._file_store.append([ False, path+marker, _('modified'), path ])
379
self._file_store.append([ True, path+marker, _('modified'), path ])
381
def _create_pending_merges(self):
385
liststore = gtk.ListStore(gobject.TYPE_STRING,
388
self._treeview_merges.set_model(liststore)
390
self._treeview_merges.append_column(gtk.TreeViewColumn(_('Date'),
391
gtk.CellRendererText(), text=0))
392
self._treeview_merges.append_column(gtk.TreeViewColumn(_('Committer'),
393
gtk.CellRendererText(), text=1))
394
self._treeview_merges.append_column(gtk.TreeViewColumn(_('Summary'),
395
gtk.CellRendererText(), text=2))
397
for item in self.pending:
398
liststore.append([ item['date'],
402
def _get_specific_files(self):
404
it = self._file_store.get_iter_first()
406
if self._file_store.get_value(it, 0):
407
# get real path from hidden column 3
408
ret.append(self._file_store.get_value(it, 3))
409
it = self._file_store.iter_next(it)
413
def _toggle_commit(self, cell, path, model):
414
model[path][0] = not model[path][0]