2
# -*- coding: UTF-8 -*-
5
This module contains the code to manage the diff window which shows
6
the changes made between two revisions on a branch.
9
__copyright__ = "Copyright © 2005 Canonical Ltd."
10
__author__ = "Scott James Remnant <scott@ubuntu.com>"
13
from cStringIO import StringIO
20
have_gtksourceview = True
22
have_gtksourceview = False
24
from bzrlib.delta import compare_trees
25
from bzrlib.diff import show_diff_trees
26
from bzrlib.errors import NoSuchFile
29
class DiffWindow(gtk.Window):
32
This object represents and manages a single window containing the
33
differences between two revisions on a branch.
37
gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
38
self.set_border_width(0)
39
self.set_title("bzrk diff")
41
# Use two thirds of the screen by default
42
screen = self.get_screen()
43
monitor = screen.get_monitor_geometry(0)
44
width = int(monitor.width * 0.66)
45
height = int(monitor.height * 0.66)
46
self.set_default_size(width, height)
51
"""Construct the window contents."""
52
hbox = gtk.HBox(spacing=6)
53
hbox.set_border_width(0)
57
scrollwin = gtk.ScrolledWindow()
58
scrollwin.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
59
scrollwin.set_shadow_type(gtk.SHADOW_IN)
60
hbox.pack_start(scrollwin, expand=False, fill=True)
63
self.model = gtk.TreeStore(str, str)
64
self.treeview = gtk.TreeView(self.model)
65
self.treeview.set_headers_visible(False)
66
self.treeview.set_search_column(1)
67
self.treeview.connect("cursor-changed", self._treeview_cursor_cb)
68
scrollwin.add(self.treeview)
71
cell = gtk.CellRendererText()
72
cell.set_property("width-chars", 20)
73
column = gtk.TreeViewColumn()
74
column.pack_start(cell, expand=True)
75
column.add_attribute(cell, "text", 0)
76
self.treeview.append_column(column)
79
scrollwin = gtk.ScrolledWindow()
80
scrollwin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
81
scrollwin.set_shadow_type(gtk.SHADOW_IN)
82
hbox.pack_start(scrollwin, expand=True, fill=True)
85
if have_gtksourceview:
86
self.buffer = gtksourceview.SourceBuffer()
87
slm = gtksourceview.SourceLanguagesManager()
88
gsl = slm.get_language_from_mime_type("text/x-patch")
89
self.buffer.set_language(gsl)
90
self.buffer.set_highlight(True)
92
sourceview = gtksourceview.SourceView(self.buffer)
94
self.buffer = gtk.TextBuffer()
95
sourceview = gtk.TextView(self.buffer)
97
sourceview.set_editable(False)
98
sourceview.modify_font(pango.FontDescription("Monospace"))
99
scrollwin.add(sourceview)
102
def set_diff(self, description, rev_tree, parent_tree):
103
"""Set the differences showed by this window.
105
Compares the two trees and populates the window with the
108
self.rev_tree = rev_tree
109
self.parent_tree = parent_tree
112
delta = compare_trees(self.parent_tree, self.rev_tree)
114
self.model.append(None, [ "Complete Diff", "" ])
117
titer = self.model.append(None, [ "Added", None ])
118
for path, id, kind in delta.added:
119
self.model.append(titer, [ path, path ])
121
if len(delta.removed):
122
titer = self.model.append(None, [ "Removed", None ])
123
for path, id, kind in delta.removed:
124
self.model.append(titer, [ path, path ])
126
if len(delta.renamed):
127
titer = self.model.append(None, [ "Renamed", None ])
128
for oldpath, newpath, id, kind, text_modified, meta_modified \
130
self.model.append(titer, [ oldpath, newpath ])
132
if len(delta.modified):
133
titer = self.model.append(None, [ "Modified", None ])
134
for path, id, kind, text_modified, meta_modified in delta.modified:
135
self.model.append(titer, [ path, path ])
137
self.treeview.expand_all()
138
self.set_title(description + " - bzrk diff")
140
def set_file(self, file_path):
142
for data in self.model:
143
for child in data.iterchildren():
144
if child[0] == file_path or child[1] == file_path:
148
raise NoSuchFile(file_path)
149
self.treeview.set_cursor(tv_path)
150
self.treeview.scroll_to_cell(tv_path)
152
def _treeview_cursor_cb(self, *args):
153
"""Callback for when the treeview cursor changes."""
154
(path, col) = self.treeview.get_cursor()
155
specific_files = [ self.model[path][1] ]
156
if specific_files == [ None ]:
158
elif specific_files == [ "" ]:
162
show_diff_trees(self.parent_tree, self.rev_tree, s, specific_files)
163
self.buffer.set_text(s.getvalue())