30
30
except ImportError:
33
from bzrlib import osutils
34
40
from bzrlib.diff import show_diff_trees, internal_diff
35
41
from bzrlib.errors import NoSuchFile
42
from bzrlib.patches import parse_patches
36
43
from bzrlib.trace import warning
44
from bzrlib.plugins.gtk import _i18n
37
45
from bzrlib.plugins.gtk.window import Window
40
class DiffView(gtk.ScrolledWindow):
41
"""This is the soft and chewy filling for a DiffWindow."""
46
from dialog import error_dialog, info_dialog, warning_dialog
49
class SelectCancelled(Exception):
54
class DiffFileView(gtk.ScrolledWindow):
55
"""Window for displaying diffs from a diff file"""
43
57
def __init__(self):
44
58
gtk.ScrolledWindow.__init__(self)
48
self.parent_tree = None
50
62
def construct(self):
51
63
self.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
237
249
# self.parent_tree.unlock()
239
251
def show_diff(self, specific_files):
253
if specific_files is None:
254
self.buffer.set_text(self._diffs[None])
256
for specific_file in specific_files:
257
sections.append(self._diffs[specific_file])
258
self.buffer.set_text(''.join(sections))
261
class DiffView(DiffFileView):
262
"""This is the soft and chewy filling for a DiffWindow."""
265
DiffFileView.__init__(self)
267
self.parent_tree = None
269
def show_diff(self, specific_files):
270
"""Show the diff for the specified files"""
241
272
show_diff_trees(self.parent_tree, self.rev_tree, s, specific_files,
242
273
old_label='', new_label='',
290
321
column.add_attribute(cell, "text", 0)
291
322
self.treeview.append_column(column)
324
def set_diff_text(self, lines):
325
"""Set the current diff from a list of lines
327
:param lines: The diff to show, in unified diff format
293
329
# The diffs of the selected file: a scrollable source or
331
self.diff_view = DiffFileView()
332
self.diff_view.show()
333
self.pack2(self.diff_view)
334
self.model.append(None, [ "Complete Diff", "" ])
335
self.diff_view._diffs[None] = ''.join(lines)
336
for patch in parse_patches(lines):
337
oldname = patch.oldname.split('\t')[0]
338
newname = patch.newname.split('\t')[0]
339
self.model.append(None, [oldname, newname])
340
self.diff_view._diffs[newname] = str(patch)
341
self.diff_view.show_diff(None)
343
def set_diff(self, rev_tree, parent_tree):
344
"""Set the differences showed by this window.
346
Compares the two trees and populates the window with the
295
349
self.diff_view = DiffView()
296
350
self.pack2(self.diff_view)
297
351
self.diff_view.show()
299
def set_diff(self, rev_tree, parent_tree):
300
"""Set the differences showed by this window.
302
Compares the two trees and populates the window with the
305
352
self.diff_view.set_trees(rev_tree, parent_tree)
306
353
self.rev_tree = rev_tree
307
354
self.parent_tree = parent_tree
382
430
def construct(self):
383
431
"""Construct the window contents."""
432
self.vbox = gtk.VBox()
435
hbox = self._get_button_bar()
437
self.vbox.pack_start(hbox, expand=False, fill=True)
384
438
self.diff = DiffWidget()
439
self.vbox.add(self.diff)
386
440
self.diff.show_all()
442
def _get_button_bar(self):
443
"""Return a button bar to use.
445
:return: None, meaning that no button bar will be used.
449
def set_diff_text(self, description, lines):
450
"""Set the diff from a text.
452
The diff must be in unified diff format, and will be parsed to
455
self.diff.set_diff_text(lines)
456
self.set_title(description + " - bzrk diff")
388
458
def set_diff(self, description, rev_tree, parent_tree):
389
459
"""Set the differences showed by this window.
398
468
self.diff.set_file(file_path)
401
def _iter_changes_to_status(source, target):
471
class MergeDirectiveWindow(DiffWindow):
473
def __init__(self, directive, path):
474
DiffWindow.__init__(self, None)
475
self._merge_target = None
476
self.directive = directive
479
def _get_button_bar(self):
480
"""The button bar has only the Merge button"""
481
merge_button = gtk.Button('Merge')
483
merge_button.set_relief(gtk.RELIEF_NONE)
484
merge_button.connect("clicked", self.perform_merge)
486
save_button = gtk.Button('Save')
488
save_button.set_relief(gtk.RELIEF_NONE)
489
save_button.connect("clicked", self.perform_save)
491
hbox = gtk.HButtonBox()
492
hbox.set_layout(gtk.BUTTONBOX_START)
493
hbox.pack_start(merge_button, expand=False, fill=True)
494
hbox.pack_start(save_button, expand=False, fill=True)
498
def perform_merge(self, window):
500
tree = self._get_merge_target()
501
except SelectCancelled:
506
merger, verified = _mod_merge.Merger.from_mergeable(tree,
507
self.directive, progress.DummyProgress())
508
merger.check_basis(True)
509
merger.merge_type = _mod_merge.Merge3Merger
510
conflict_count = merger.do_merge()
512
if conflict_count == 0:
513
# No conflicts found.
514
info_dialog(_i18n('Merge successful'),
515
_i18n('All changes applied successfully.'))
517
# There are conflicts to be resolved.
518
warning_dialog(_i18n('Conflicts encountered'),
519
_i18n('Please resolve the conflicts manually'
520
' before committing.'))
523
error_dialog('Error', str(e))
527
def _get_merge_target(self):
528
if self._merge_target is not None:
529
return self._merge_target
530
d = gtk.FileChooserDialog('Merge branch', self,
531
gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER,
532
buttons=(gtk.STOCK_OK, gtk.RESPONSE_OK,
534
gtk.RESPONSE_CANCEL,))
537
if result != gtk.RESPONSE_OK:
538
raise SelectCancelled()
539
uri = d.get_current_folder_uri()
542
return workingtree.WorkingTree.open(uri)
544
def perform_save(self, window):
545
d = gtk.FileChooserDialog('Save As', self,
546
gtk.FILE_CHOOSER_ACTION_SAVE,
547
buttons=(gtk.STOCK_OK, gtk.RESPONSE_OK,
549
gtk.RESPONSE_CANCEL,))
550
d.set_current_name(osutils.basename(self.path))
554
if result != gtk.RESPONSE_OK:
555
raise SelectCancelled()
559
except SelectCancelled:
561
source = open(self.path, 'rb')
563
target = open(urlutils.local_path_from_url(uri), 'wb')
565
target.write(source.read())
572
def iter_changes_to_status(source, target):
402
573
"""Determine the differences between trees.
404
This is a wrapper around _iter_changes which just yields more
575
This is a wrapper around iter_changes which just yields more
405
576
understandable results.
407
578
:param source: The source tree (basis tree)