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
34
from guifiles import GLADEFILENAME
36
class CommitDialog(gtk.Dialog):
37
""" New implementation of the Commit dialog. """
38
def __init__(self, wt, wtpath, notbranch, selected=None, parent=None):
39
""" Initialize the Commit Dialog. """
40
gtk.Dialog.__init__(self, title="Commit - Olive",
43
buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
48
self.notbranch = notbranch
49
self.selected = selected
52
self.old_tree = self.wt.branch.repository.revision_tree(self.wt.branch.last_revision())
53
self.delta = self.wt.changes_from(self.old_tree)
56
self.pending = self._pending_merges(self.wt)
58
# Do some preliminary checks
59
self._is_checkout = False
60
self._is_pending = False
61
if self.wt is None and not self.notbranch:
62
error_dialog(_('Directory does not have a working tree'),
63
_('Operation aborted.'))
68
error_dialog(_('Directory is not a branch'),
69
_('You can perform this action only in a branch.'))
73
if self.wt.branch.get_bound_location() is not None:
74
# we have a checkout, so the local commit checkbox must appear
75
self._is_checkout = True
78
# There are pending merges, file selection not supported
79
self._is_pending = True
82
self._button_commit = gtk.Button(_("Comm_it"), use_underline=True)
84
self._check_local = gtk.CheckButton(_("_Local only commit (works in checkouts)"),
86
self._check_strict = gtk.CheckButton(_("_Strict commit (fails if unknown files are present)"),
88
self._expander_files = gtk.Expander(_("Please select the file(s) to commit"))
89
self._vpaned_main = gtk.VPaned()
90
self._scrolledwindow_files = gtk.ScrolledWindow()
91
self._scrolledwindow_message = gtk.ScrolledWindow()
92
self._treeview_files = gtk.TreeView()
93
self._vbox_message = gtk.VBox()
94
self._label_message = gtk.Label(_("Please specify a commit message:"))
95
self._textview_message = gtk.TextView()
98
self._expander_merges = gtk.Expander(_("Pending merges"))
99
self._vpaned_list = gtk.VPaned()
100
self._scrolledwindow_merges = gtk.ScrolledWindow()
101
self._treeview_merges = gtk.TreeView()
104
self._button_commit.connect('clicked', self._on_commit_clicked)
105
self._treeview_files.connect('row_activated', self._on_treeview_files_row_activated)
108
self._scrolledwindow_files.set_policy(gtk.POLICY_AUTOMATIC,
109
gtk.POLICY_AUTOMATIC)
110
self._scrolledwindow_message.set_policy(gtk.POLICY_AUTOMATIC,
111
gtk.POLICY_AUTOMATIC)
112
self._textview_message.modify_font(pango.FontDescription("Monospace"))
113
self.set_default_size(500, 500)
114
self._vpaned_main.set_position(200)
117
self._scrolledwindow_merges.set_policy(gtk.POLICY_AUTOMATIC,
118
gtk.POLICY_AUTOMATIC)
119
self._treeview_files.set_sensitive(False)
121
# Construct the dialog
122
self.action_area.pack_end(self._button_commit)
124
self._scrolledwindow_files.add(self._treeview_files)
125
self._scrolledwindow_message.add(self._textview_message)
127
self._expander_files.add(self._scrolledwindow_files)
129
self._vbox_message.pack_start(self._label_message, False, False)
130
self._vbox_message.pack_start(self._scrolledwindow_message, True, True)
133
self._expander_merges.add(self._scrolledwindow_merges)
134
self._scrolledwindow_merges.add(self._treeview_merges)
135
self._vpaned_list.add1(self._expander_files)
136
self._vpaned_list.add2(self._expander_merges)
137
self._vpaned_main.add1(self._vpaned_list)
139
self._vpaned_main.add1(self._expander_files)
141
self._vpaned_main.add2(self._vbox_message)
143
self.vbox.pack_start(self._vpaned_main, True, True)
144
if self._is_checkout:
145
self.vbox.pack_start(self._check_local, False, False)
146
self.vbox.pack_start(self._check_strict, False, False)
148
# Create the file list
149
self._create_file_view()
150
# Create the pending merges
151
self._create_pending_merges()
153
# Expand the corresponding expander
155
self._expander_merges.set_expanded(True)
157
self._expander_files.set_expanded(True)
162
def _on_treeview_files_row_activated(self, treeview, path, view_column):
163
# FIXME: the diff window freezes for some reason
164
treeselection = treeview.get_selection()
165
(model, iter) = treeselection.get_selected()
168
from olive import DiffWindow
170
_selected = model.get_value(iter, 1)
173
parent_tree = self.wt.branch.repository.revision_tree(self.wt.branch.last_revision())
174
diff.set_diff(self.wt.branch.nick, self.wt, parent_tree)
176
diff.set_file(_selected)
177
except errors.NoSuchFile:
182
def _on_commit_clicked(self, button):
183
""" Commit button clicked handler. """
184
textbuffer = self._textview_message.get_buffer()
185
start, end = textbuffer.get_bounds()
186
message = textbuffer.get_text(start, end).decode('utf-8')
189
specific_files = self._get_specific_files()
191
specific_files = None
194
response = question_dialog(_('Commit with an empty message?'),
195
_('You can describe your commit intent in the message.'))
196
if response == gtk.RESPONSE_NO:
197
# Kindly give focus to message area
198
self._textview_message.grab_focus()
201
if self._is_checkout:
202
local = self._check_local.get_active()
207
self.wt.commit(message,
208
allow_pointless=False,
209
strict=self._check_strict.get_active(),
211
specific_files=specific_files)
212
except errors.PointlessCommit:
213
response = question_dialog(_('Commit with no changes?'),
214
_('There are no changes in the working tree.'))
215
if response == gtk.RESPONSE_YES:
216
self.wt.commit(message,
217
allow_pointless=True,
218
strict=self._check_strict.get_active(),
220
specific_files=specific_files)
221
self.response(gtk.RESPONSE_OK)
223
def _pending_merges(self, wt):
224
""" Return a list of pending merges or None if there are none of them. """
225
parents = wt.get_parent_ids()
230
from bzrlib.osutils import format_date
232
pending = parents[1:]
234
last_revision = parents[0]
236
if last_revision is not None:
238
ignore = set(branch.repository.get_ancestry(last_revision))
239
except errors.NoSuchRevision:
240
# the last revision is a ghost : assume everything is new
242
ignore = set([None, last_revision])
247
for merge in pending:
250
m_revision = branch.repository.get_revision(merge)
253
rev['committer'] = re.sub('<.*@.*>', '', m_revision.committer).strip(' ')
254
rev['summary'] = m_revision.get_summary()
255
rev['date'] = format_date(m_revision.timestamp,
256
m_revision.timezone or 0,
257
'original', date_fmt="%Y-%m-%d",
262
inner_merges = branch.repository.get_ancestry(merge)
263
assert inner_merges[0] is None
265
inner_merges.reverse()
266
for mmerge in inner_merges:
269
mm_revision = branch.repository.get_revision(mmerge)
272
rev['committer'] = re.sub('<.*@.*>', '', mm_revision.committer).strip(' ')
273
rev['summary'] = mm_revision.get_summary()
274
rev['date'] = format_date(mm_revision.timestamp,
275
mm_revision.timezone or 0,
276
'original', date_fmt="%Y-%m-%d",
282
except errors.NoSuchRevision:
283
print "DEBUG: NoSuchRevision:", merge
287
def _create_file_view(self):
288
self._file_store = gtk.ListStore(gobject.TYPE_BOOLEAN, # [0] checkbox
289
gobject.TYPE_STRING, # [1] path to display
290
gobject.TYPE_STRING, # [2] changes type
291
gobject.TYPE_STRING) # [3] real path
292
self._treeview_files.set_model(self._file_store)
293
crt = gtk.CellRendererToggle()
294
crt.set_property("activatable", True)
295
crt.connect("toggled", self._toggle_commit, self._file_store)
296
self._treeview_files.append_column(gtk.TreeViewColumn(_('Commit'),
298
self._treeview_files.append_column(gtk.TreeViewColumn(_('Path'),
299
gtk.CellRendererText(), text=1))
300
self._treeview_files.append_column(gtk.TreeViewColumn(_('Type'),
301
gtk.CellRendererText(), text=2))
303
for path, id, kind in self.delta.added:
304
marker = osutils.kind_marker(kind)
305
if self.selected is not None:
306
if path == os.path.join(self.wtpath, self.selected):
307
self._file_store.append([ True, path+marker, _('added'), path ])
309
self._file_store.append([ False, path+marker, _('added'), path ])
311
self._file_store.append([ True, path+marker, _('added'), path ])
313
for path, id, kind in self.delta.removed:
314
marker = osutils.kind_marker(kind)
315
if self.selected is not None:
316
if path == os.path.join(self.wtpath, self.selected):
317
self._file_store.append([ True, path+marker, _('removed'), path ])
319
self._file_store.append([ False, path+marker, _('removed'), path ])
321
self._file_store.append([ True, path+marker, _('removed'), path ])
323
for oldpath, newpath, id, kind, text_modified, meta_modified in self.delta.renamed:
324
marker = osutils.kind_marker(kind)
325
if text_modified or meta_modified:
326
changes = _('renamed and modified')
328
changes = _('renamed')
329
if self.selected is not None:
330
if newpath == os.path.join(self.wtpath, self.selected):
331
self._file_store.append([ True,
332
oldpath+marker + ' => ' + newpath+marker,
337
self._file_store.append([ False,
338
oldpath+marker + ' => ' + newpath+marker,
343
self._file_store.append([ True,
344
oldpath+marker + ' => ' + newpath+marker,
349
for path, id, kind, text_modified, meta_modified in self.delta.modified:
350
marker = osutils.kind_marker(kind)
351
if self.selected is not None:
352
if path == os.path.join(self.wtpath, self.selected):
353
self._file_store.append([ True, path+marker, _('modified'), path ])
355
self._file_store.append([ False, path+marker, _('modified'), path ])
357
self._file_store.append([ True, path+marker, _('modified'), path ])
359
def _create_pending_merges(self):
363
liststore = gtk.ListStore(gobject.TYPE_STRING,
366
self._treeview_merges.set_model(liststore)
368
self._treeview_merges.append_column(gtk.TreeViewColumn(_('Date'),
369
gtk.CellRendererText(), text=0))
370
self._treeview_merges.append_column(gtk.TreeViewColumn(_('Committer'),
371
gtk.CellRendererText(), text=1))
372
self._treeview_merges.append_column(gtk.TreeViewColumn(_('Summary'),
373
gtk.CellRendererText(), text=2))
375
for item in self.pending:
376
liststore.append([ item['date'],
380
def _get_specific_files(self):
382
it = self._file_store.get_iter_first()
384
if self._file_store.get_value(it, 0):
385
# get real path from hidden column 3
386
ret.append(self._file_store.get_value(it, 3))
387
it = self._file_store.iter_next(it)
391
def _toggle_commit(self, cell, path, model):
392
model[path][0] = not model[path][0]