/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz

« back to all changes in this revision

Viewing changes to olive/commit.py

  • Committer: Jelmer Vernooij
  • Date: 2007-02-01 15:50:40 UTC
  • Revision ID: jelmer@samba.org-20070201155040-3hq4mfbxs99kzazy
add framework for tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
import gobject
26
26
import pango
27
27
 
 
28
import os.path
 
29
 
28
30
import bzrlib.errors as errors
29
31
from bzrlib import osutils
30
32
 
31
33
from dialog import error_dialog, question_dialog
 
34
from errors import show_bzr_error
32
35
from guifiles import GLADEFILENAME
33
36
 
34
 
 
35
 
class CommitDialog:
36
 
    """ Display Commit dialog and perform the needed actions. """
37
 
    def __init__(self, wt, wtpath, notbranch):
38
 
        """ Initialize the Commit dialog.
39
 
        :param  wt:         bzr working tree object
40
 
        :param  wtpath:     path to working tree root
41
 
        :param  notbranch:  flag that path is not a brach
42
 
        :type   notbranch:  bool
43
 
        """
44
 
        self.glade = gtk.glade.XML(GLADEFILENAME, 'window_commit', 'olive-gtk')
 
37
class CommitDialog(gtk.Dialog):
 
38
    """ New implementation of the Commit dialog. """
 
39
    def __init__(self, wt, wtpath, notbranch, selected=None, parent=None):
 
40
        """ Initialize the Commit Dialog. """
 
41
        gtk.Dialog.__init__(self, title="Commit - Olive",
 
42
                                  parent=parent,
 
43
                                  flags=0,
 
44
                                  buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
45
45
        
 
46
        # Get arguments
46
47
        self.wt = wt
47
48
        self.wtpath = wtpath
48
49
        self.notbranch = notbranch
49
 
 
50
 
        # Get some important widgets
51
 
        self.window = self.glade.get_widget('window_commit')
52
 
        self.checkbutton_local = self.glade.get_widget('checkbutton_commit_local')
53
 
        self.textview = self.glade.get_widget('textview_commit')
54
 
        self.file_expander = self.glade.get_widget('expander_commit_select')
55
 
        self.file_view = self.glade.get_widget('treeview_commit_select')
56
 
        self.pending_expander = self.glade.get_widget('expander_commit_pending')
57
 
        self.pending_label = self.glade.get_widget('label_commit_pending')
58
 
        self.pending_view = self.glade.get_widget('treeview_commit_pending')
59
 
 
60
 
        if wt is None or notbranch:
61
 
            return
 
50
        self.selected = selected
62
51
        
63
52
        # Set the delta
64
53
        self.old_tree = self.wt.branch.repository.revision_tree(self.wt.branch.last_revision())
67
56
        # Get pending merges
68
57
        self.pending = self._pending_merges(self.wt)
69
58
        
70
 
        # Dictionary for signal_autoconnect
71
 
        dic = { "on_button_commit_commit_clicked": self.commit,
72
 
                "on_button_commit_cancel_clicked": self.close }
73
 
 
74
 
        # Connect the signals to the handlers
75
 
        self.glade.signal_autoconnect(dic)
76
 
        
77
 
        # Create the file list
78
 
        self._create_file_view()
79
 
        # Create the pending merges
80
 
        self._create_pending_merges()
81
 
    
82
 
    def display(self):
83
 
        """ Display the Push dialog.
84
 
        @return:    True if dialog is shown.
85
 
        """
 
59
        # Do some preliminary checks
 
60
        self._is_checkout = False
 
61
        self._is_pending = False
86
62
        if self.wt is None and not self.notbranch:
87
63
            error_dialog(_('Directory does not have a working tree'),
88
64
                         _('Operation aborted.'))
89
65
            self.close()
90
 
            return False
 
66
            return
 
67
 
91
68
        if self.notbranch:
92
69
            error_dialog(_('Directory is not a branch'),
93
70
                         _('You can perform this action only in a branch.'))
94
71
            self.close()
95
 
            return False
 
72
            return
96
73
        else:
97
74
            if self.wt.branch.get_bound_location() is not None:
98
75
                # we have a checkout, so the local commit checkbox must appear
99
 
                self.checkbutton_local.show()
 
76
                self._is_checkout = True
100
77
            
101
78
            if self.pending:
102
79
                # There are pending merges, file selection not supported
103
 
                self.file_expander.set_expanded(False)
104
 
                self.file_view.set_sensitive(False)
105
 
            else:
106
 
                # No pending merges
107
 
                self.pending_expander.hide()
108
 
            
109
 
            self.textview.modify_font(pango.FontDescription("Monospace"))
110
 
            self.window.show()
111
 
            return True
112
 
    
113
 
    def _create_file_view(self):
114
 
        self.file_store = gtk.ListStore(gobject.TYPE_BOOLEAN,   # [0] checkbox
115
 
                                        gobject.TYPE_STRING,    # [1] path to display
116
 
                                        gobject.TYPE_STRING,    # [2] changes type
117
 
                                        gobject.TYPE_STRING)    # [3] real path
118
 
        self.file_view.set_model(self.file_store)
119
 
        crt = gtk.CellRendererToggle()
120
 
        crt.set_property("activatable", True)
121
 
        crt.connect("toggled", self._toggle_commit, self.file_store)
122
 
        self.file_view.append_column(gtk.TreeViewColumn(_('Commit'),
123
 
                                     crt, active=0))
124
 
        self.file_view.append_column(gtk.TreeViewColumn(_('Path'),
125
 
                                     gtk.CellRendererText(), text=1))
126
 
        self.file_view.append_column(gtk.TreeViewColumn(_('Type'),
127
 
                                     gtk.CellRendererText(), text=2))
128
 
 
129
 
        for path, id, kind in self.delta.added:
130
 
            marker = osutils.kind_marker(kind)
131
 
            self.file_store.append([ True, path+marker, _('added'), path ])
132
 
 
133
 
        for path, id, kind in self.delta.removed:
134
 
            marker = osutils.kind_marker(kind)
135
 
            self.file_store.append([ True, path+marker, _('removed'), path ])
136
 
 
137
 
        for oldpath, newpath, id, kind, text_modified, meta_modified in self.delta.renamed:
138
 
            marker = osutils.kind_marker(kind)
139
 
            if text_modified or meta_modified:
140
 
                changes = _('renamed and modified')
141
 
            else:
142
 
                changes = _('renamed')
143
 
            self.file_store.append([ True,
144
 
                                     oldpath+marker + '  =>  ' + newpath+marker,
145
 
                                     changes,
146
 
                                     newpath
147
 
                                   ])
148
 
 
149
 
        for path, id, kind, text_modified, meta_modified in self.delta.modified:
150
 
            marker = osutils.kind_marker(kind)
151
 
            self.file_store.append([ True, path+marker, _('modified'), path ])
152
 
    
153
 
    def _create_pending_merges(self):
 
80
                self._is_pending = True
 
81
        
 
82
        # Create the widgets
 
83
        self._button_commit = gtk.Button(_("Comm_it"), use_underline=True)
 
84
        if self._is_checkout:
 
85
            self._check_local = gtk.CheckButton(_("_Local only commit (works in checkouts)"),
 
86
                                                use_underline=True)
 
87
        self._check_strict = gtk.CheckButton(_("_Strict commit (fails if unknown files are present)"),
 
88
                                             use_underline=True)
 
89
        self._expander_files = gtk.Expander(_("Please select the 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(_("Please specify a commit message:"))
 
96
        self._textview_message = gtk.TextView()
 
97
        
 
98
        if self._is_pending:
 
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()
 
103
 
 
104
        # Set callbacks
 
105
        self._button_commit.connect('clicked', self._on_commit_clicked)
 
106
        self._treeview_files.connect('row_activated', self._on_treeview_files_row_activated)
 
107
        
 
108
        # Set properties
 
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
 
 
117
        if self._is_pending:
 
118
            self._scrolledwindow_merges.set_policy(gtk.POLICY_AUTOMATIC,
 
119
                                                   gtk.POLICY_AUTOMATIC)
 
120
            self._treeview_files.set_sensitive(False)
 
121
        
 
122
        # Construct the dialog
 
123
        self.action_area.pack_end(self._button_commit)
 
124
        
 
125
        self._scrolledwindow_files.add(self._treeview_files)
 
126
        self._scrolledwindow_message.add(self._textview_message)
 
127
        
 
128
        self._expander_files.add(self._scrolledwindow_files)
 
129
        
 
130
        self._vbox_message.pack_start(self._label_message, False, False)
 
131
        self._vbox_message.pack_start(self._scrolledwindow_message, True, True)
 
132
        
 
133
        if self._is_pending:        
 
134
            self._expander_merges.add(self._scrolledwindow_merges)
 
135
            self._scrolledwindow_merges.add(self._treeview_merges)
 
136
            self._vpaned_list.add1(self._expander_files)
 
137
            self._vpaned_list.add2(self._expander_merges)
 
138
            self._vpaned_main.add1(self._vpaned_list)
 
139
        else:
 
140
            self._vpaned_main.add1(self._expander_files)
 
141
 
 
142
        self._vpaned_main.add2(self._vbox_message)
 
143
        
 
144
        self.vbox.pack_start(self._vpaned_main, True, True)
 
145
        if self._is_checkout:
 
146
            self.vbox.pack_start(self._check_local, False, False)
 
147
        self.vbox.pack_start(self._check_strict, False, False)
 
148
        
 
149
        # Create the file list
 
150
        self._create_file_view()
 
151
        # Create the pending merges
 
152
        self._create_pending_merges()
 
153
        
 
154
        # Expand the corresponding expander
 
155
        if self._is_pending:
 
156
            self._expander_merges.set_expanded(True)
 
157
        else:
 
158
            self._expander_files.set_expanded(True)
 
159
        
 
160
        # Display dialog
 
161
        self.vbox.show_all()
 
162
    
 
163
    def _on_treeview_files_row_activated(self, treeview, path, view_column):
 
164
        # FIXME: the diff window freezes for some reason
 
165
        treeselection = treeview.get_selection()
 
166
        (model, iter) = treeselection.get_selected()
 
167
        
 
168
        if iter is not None:
 
169
            from olive import DiffWindow
 
170
            
 
171
            _selected = model.get_value(iter, 1)
 
172
            
 
173
            diff = DiffWindow()
 
174
            parent_tree = self.wt.branch.repository.revision_tree(self.wt.branch.last_revision())
 
175
            diff.set_diff(self.wt.branch.nick, self.wt, parent_tree)
 
176
            try:
 
177
                diff.set_file(_selected)
 
178
            except errors.NoSuchFile:
 
179
                pass
 
180
            diff.show()
 
181
    
 
182
    @show_bzr_error
 
183
    def _on_commit_clicked(self, button):
 
184
        """ Commit button clicked handler. """
 
185
        textbuffer = self._textview_message.get_buffer()
 
186
        start, end = textbuffer.get_bounds()
 
187
        message = textbuffer.get_text(start, end).decode('utf-8')
 
188
        
154
189
        if not self.pending:
155
 
            # hide unused pending merge part
156
 
            scrolled_window = self.glade.get_widget('scrolledwindow_commit_pending')
157
 
            parent = scrolled_window.get_parent()
158
 
            parent.remove(scrolled_window)
159
 
            parent = self.pending_label.get_parent()
160
 
            parent.remove(self.pending_label)
161
 
            return
162
 
        
163
 
        liststore = gtk.ListStore(gobject.TYPE_STRING,
164
 
                                  gobject.TYPE_STRING,
165
 
                                  gobject.TYPE_STRING)
166
 
        self.pending_view.set_model(liststore)
167
 
        
168
 
        self.pending_view.append_column(gtk.TreeViewColumn(_('Date'),
169
 
                                        gtk.CellRendererText(), text=0))
170
 
        self.pending_view.append_column(gtk.TreeViewColumn(_('Committer'),
171
 
                                        gtk.CellRendererText(), text=1))
172
 
        self.pending_view.append_column(gtk.TreeViewColumn(_('Summary'),
173
 
                                        gtk.CellRendererText(), text=2))
174
 
        
175
 
        for item in self.pending:
176
 
            liststore.append([ item['date'],
177
 
                               item['committer'],
178
 
                               item['summary'] ])
179
 
    
180
 
    def _get_specific_files(self):
181
 
        ret = []
182
 
        it = self.file_store.get_iter_first()
183
 
        while it:
184
 
            if self.file_store.get_value(it, 0):
185
 
                # get real path from hidden column 3
186
 
                ret.append(self.file_store.get_value(it, 3))
187
 
            it = self.file_store.iter_next(it)
188
 
 
189
 
        return ret
190
 
    
191
 
    def _toggle_commit(self, cell, path, model):
192
 
        model[path][0] = not model[path][0]
193
 
        return
194
 
    
 
190
            specific_files = self._get_specific_files()
 
191
        else:
 
192
            specific_files = None
 
193
 
 
194
        if message == '':
 
195
            response = question_dialog(_('Commit with an empty message?'),
 
196
                                       _('You can describe your commit intent in the message.'))
 
197
            if response == gtk.RESPONSE_NO:
 
198
                # Kindly give focus to message area
 
199
                self._textview_message.grab_focus()
 
200
                return
 
201
 
 
202
        if self._is_checkout:
 
203
            local = self._check_local.get_active()
 
204
        else:
 
205
            local = False
 
206
        
 
207
        try:
 
208
            self.wt.commit(message,
 
209
                       allow_pointless=False,
 
210
                       strict=self._check_strict.get_active(),
 
211
                       local=local,
 
212
                       specific_files=specific_files)
 
213
        except errors.PointlessCommit:
 
214
            response = question_dialog(_('Commit with no changes?'),
 
215
                                       _('There are no changes in the working tree.'))
 
216
            if response == gtk.RESPONSE_YES:
 
217
                self.wt.commit(message,
 
218
                               allow_pointless=True,
 
219
                               strict=self._check_strict.get_active(),
 
220
                               local=local,
 
221
                               specific_files=specific_files)
 
222
        self.response(gtk.RESPONSE_OK)
 
223
 
195
224
    def _pending_merges(self, wt):
196
225
        """ Return a list of pending merges or None if there are none of them. """
197
226
        parents = wt.get_parent_ids()
256
285
        
257
286
        return pm
258
287
 
259
 
    def commit(self, widget):
260
 
        textbuffer = self.textview.get_buffer()
261
 
        start, end = textbuffer.get_bounds()
262
 
        message = textbuffer.get_text(start, end).decode('utf-8')
263
 
        
264
 
        checkbutton_strict = self.glade.get_widget('checkbutton_commit_strict')
265
 
        checkbutton_force = self.glade.get_widget('checkbutton_commit_force')
266
 
        
 
288
    def _create_file_view(self):
 
289
        self._file_store = gtk.ListStore(gobject.TYPE_BOOLEAN,   # [0] checkbox
 
290
                                         gobject.TYPE_STRING,    # [1] path to display
 
291
                                         gobject.TYPE_STRING,    # [2] changes type
 
292
                                         gobject.TYPE_STRING)    # [3] real path
 
293
        self._treeview_files.set_model(self._file_store)
 
294
        crt = gtk.CellRendererToggle()
 
295
        crt.set_property("activatable", True)
 
296
        crt.connect("toggled", self._toggle_commit, self._file_store)
 
297
        self._treeview_files.append_column(gtk.TreeViewColumn(_('Commit'),
 
298
                                     crt, active=0))
 
299
        self._treeview_files.append_column(gtk.TreeViewColumn(_('Path'),
 
300
                                     gtk.CellRendererText(), text=1))
 
301
        self._treeview_files.append_column(gtk.TreeViewColumn(_('Type'),
 
302
                                     gtk.CellRendererText(), text=2))
 
303
 
 
304
        for path, id, kind in self.delta.added:
 
305
            marker = osutils.kind_marker(kind)
 
306
            if self.selected is not None:
 
307
                if path == os.path.join(self.wtpath, self.selected):
 
308
                    self._file_store.append([ True, path+marker, _('added'), path ])
 
309
                else:
 
310
                    self._file_store.append([ False, path+marker, _('added'), path ])
 
311
            else:
 
312
                self._file_store.append([ True, path+marker, _('added'), path ])
 
313
 
 
314
        for path, id, kind in self.delta.removed:
 
315
            marker = osutils.kind_marker(kind)
 
316
            if self.selected is not None:
 
317
                if path == os.path.join(self.wtpath, self.selected):
 
318
                    self._file_store.append([ True, path+marker, _('removed'), path ])
 
319
                else:
 
320
                    self._file_store.append([ False, path+marker, _('removed'), path ])
 
321
            else:
 
322
                self._file_store.append([ True, path+marker, _('removed'), path ])
 
323
 
 
324
        for oldpath, newpath, id, kind, text_modified, meta_modified in self.delta.renamed:
 
325
            marker = osutils.kind_marker(kind)
 
326
            if text_modified or meta_modified:
 
327
                changes = _('renamed and modified')
 
328
            else:
 
329
                changes = _('renamed')
 
330
            if self.selected is not None:
 
331
                if newpath == os.path.join(self.wtpath, self.selected):
 
332
                    self._file_store.append([ True,
 
333
                                              oldpath+marker + '  =>  ' + newpath+marker,
 
334
                                              changes,
 
335
                                              newpath
 
336
                                            ])
 
337
                else:
 
338
                    self._file_store.append([ False,
 
339
                                              oldpath+marker + '  =>  ' + newpath+marker,
 
340
                                              changes,
 
341
                                              newpath
 
342
                                            ])
 
343
            else:
 
344
                self._file_store.append([ True,
 
345
                                          oldpath+marker + '  =>  ' + newpath+marker,
 
346
                                          changes,
 
347
                                          newpath
 
348
                                        ])
 
349
 
 
350
        for path, id, kind, text_modified, meta_modified in self.delta.modified:
 
351
            marker = osutils.kind_marker(kind)
 
352
            if self.selected is not None:
 
353
                if path == os.path.join(self.wtpath, self.selected):
 
354
                    self._file_store.append([ True, path+marker, _('modified'), path ])
 
355
                else:
 
356
                    self._file_store.append([ False, path+marker, _('modified'), path ])
 
357
            else:
 
358
                self._file_store.append([ True, path+marker, _('modified'), path ])
 
359
    
 
360
    def _create_pending_merges(self):
267
361
        if not self.pending:
268
 
            specific_files = self._get_specific_files()
269
 
        else:
270
 
            specific_files = None
271
 
 
272
 
        if message == '':
273
 
            response = question_dialog('Commit with an empty message ?',
274
 
                                       'You can describe your commit intent'
275
 
                                       +' in the message')
276
 
            if response == gtk.RESPONSE_NO:
277
 
                # Kindly give focus to message area
278
 
                self.textview.grab_focus()
279
 
                return
280
 
 
281
 
        try:
282
 
            self.wt.commit(message,
283
 
                           allow_pointless=checkbutton_force.get_active(),
284
 
                           strict=checkbutton_strict.get_active(),
285
 
                           local=self.checkbutton_local.get_active(),
286
 
                           specific_files=specific_files)
287
 
        except errors.NotBranchError:
288
 
            error_dialog(_('Directory is not a branch'),
289
 
                         _('You can perform this action only in a branch.'))
290
 
            return
291
 
        except errors.LocalRequiresBoundBranch:
292
 
            error_dialog(_('Directory is not a checkout'),
293
 
                         _('You can perform local commit only on checkouts.'))
294
 
            return
295
 
        except errors.PointlessCommit:
296
 
            error_dialog(_('No changes to commit'),
297
 
                         _('Try force commit if you want to commit anyway.'))
298
 
            return
299
 
        except errors.ConflictsInTree:
300
 
            error_dialog(_('Conflicts in tree'),
301
 
                         _('You need to resolve the conflicts before committing.'))
302
 
            return
303
 
        except errors.StrictCommitFailed:
304
 
            error_dialog(_('Strict commit failed'),
305
 
                         _('There are unknown files in the working tree.\nPlease add or delete them.'))
306
 
            return
307
 
        except errors.BoundBranchOutOfDate, errmsg:
308
 
            error_dialog(_('Bound branch is out of date'),
309
 
                         _('%s') % errmsg)
310
 
            return
311
 
        except errors.BzrError, msg:
312
 
            error_dialog(_('Unknown bzr error'), str(msg))
313
 
            return
314
 
        except Exception, msg:
315
 
            error_dialog(_('Unknown error'), str(msg))
316
 
            return
317
 
 
318
 
        self.close()
319
 
 
320
 
    def close(self, widget=None):
321
 
        self.window.destroy()
 
362
            return
 
363
        
 
364
        liststore = gtk.ListStore(gobject.TYPE_STRING,
 
365
                                  gobject.TYPE_STRING,
 
366
                                  gobject.TYPE_STRING)
 
367
        self._treeview_merges.set_model(liststore)
 
368
        
 
369
        self._treeview_merges.append_column(gtk.TreeViewColumn(_('Date'),
 
370
                                            gtk.CellRendererText(), text=0))
 
371
        self._treeview_merges.append_column(gtk.TreeViewColumn(_('Committer'),
 
372
                                            gtk.CellRendererText(), text=1))
 
373
        self._treeview_merges.append_column(gtk.TreeViewColumn(_('Summary'),
 
374
                                            gtk.CellRendererText(), text=2))
 
375
        
 
376
        for item in self.pending:
 
377
            liststore.append([ item['date'],
 
378
                               item['committer'],
 
379
                               item['summary'] ])
 
380
    
 
381
    def _get_specific_files(self):
 
382
        ret = []
 
383
        it = self._file_store.get_iter_first()
 
384
        while it:
 
385
            if self._file_store.get_value(it, 0):
 
386
                # get real path from hidden column 3
 
387
                ret.append(self._file_store.get_value(it, 3))
 
388
            it = self._file_store.iter_next(it)
 
389
 
 
390
        return ret
 
391
    
 
392
    def _toggle_commit(self, cell, path, model):
 
393
        model[path][0] = not model[path][0]
 
394
        return