1
# Modified for use with Olive:
2
# Copyright (C) 2006 by Szilveszter Farkas (Phanatic) <szilveszter.farkas@gmail.com>
3
# Original copyright holder:
4
# Copyright (C) 2005 by Canonical Ltd. (Scott James Remnant <scott@ubuntu.com>)
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
This module contains the code to manage the diff window which shows
23
the changes made between two revisions on a branch.
26
__copyright__ = "Copyright © 2005 Canonical Ltd."
27
__author__ = "Scott James Remnant <scott@ubuntu.com>"
30
from cStringIO import StringIO
37
have_gtksourceview = True
39
have_gtksourceview = False
42
if (bzrlib.version_info[0] == 0) and (bzrlib.version_info[1] < 9):
43
# function deprecated after 0.9
44
from bzrlib.delta import compare_trees
46
from bzrlib.diff import show_diff_trees
47
from bzrlib.errors import NoSuchFile
50
class DiffWindow(gtk.Window):
53
This object represents and manages a single window containing the
54
differences between two revisions on a branch.
58
gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
59
self.set_border_width(0)
60
self.set_title("bzrk diff")
62
# Use two thirds of the screen by default
63
screen = self.get_screen()
64
monitor = screen.get_monitor_geometry(0)
65
width = int(monitor.width * 0.66)
66
height = int(monitor.height * 0.66)
67
self.set_default_size(width, height)
72
"""Construct the window contents."""
73
hbox = gtk.HBox(spacing=6)
74
hbox.set_border_width(0)
78
scrollwin = gtk.ScrolledWindow()
79
scrollwin.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
80
scrollwin.set_shadow_type(gtk.SHADOW_IN)
81
hbox.pack_start(scrollwin, expand=False, fill=True)
84
self.model = gtk.TreeStore(str, str)
85
self.treeview = gtk.TreeView(self.model)
86
self.treeview.set_headers_visible(False)
87
self.treeview.set_search_column(1)
88
self.treeview.connect("cursor-changed", self._treeview_cursor_cb)
89
scrollwin.add(self.treeview)
92
cell = gtk.CellRendererText()
93
cell.set_property("width-chars", 20)
94
column = gtk.TreeViewColumn()
95
column.pack_start(cell, expand=True)
96
column.add_attribute(cell, "text", 0)
97
self.treeview.append_column(column)
100
scrollwin = gtk.ScrolledWindow()
101
scrollwin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
102
scrollwin.set_shadow_type(gtk.SHADOW_IN)
103
hbox.pack_start(scrollwin, expand=True, fill=True)
106
if have_gtksourceview:
107
self.buffer = gtksourceview.SourceBuffer()
108
slm = gtksourceview.SourceLanguagesManager()
109
gsl = slm.get_language_from_mime_type("text/x-patch")
110
self.buffer.set_language(gsl)
111
self.buffer.set_highlight(True)
113
sourceview = gtksourceview.SourceView(self.buffer)
115
self.buffer = gtk.TextBuffer()
116
sourceview = gtk.TextView(self.buffer)
118
sourceview.set_editable(False)
119
sourceview.modify_font(pango.FontDescription("Monospace"))
120
scrollwin.add(sourceview)
123
def set_diff(self, description, rev_tree, parent_tree):
124
"""Set the differences showed by this window.
126
Compares the two trees and populates the window with the
129
self.rev_tree = rev_tree
130
self.parent_tree = parent_tree
134
if (bzrlib.version_info[0] == 0) and (bzrlib.version_info[1] < 9):
135
delta = compare_trees(self.parent_tree, self.rev_tree)
137
delta = self.rev_tree.changes_from(self.parent_tree)
139
self.model.append(None, [ _('Complete Diff'), "" ])
142
titer = self.model.append(None, [ _('Added'), None ])
143
for path, id, kind in delta.added:
144
self.model.append(titer, [ path, path ])
146
if len(delta.removed):
147
titer = self.model.append(None, [ _('Removed'), None ])
148
for path, id, kind in delta.removed:
149
self.model.append(titer, [ path, path ])
151
if len(delta.renamed):
152
titer = self.model.append(None, [ _('Renamed'), None ])
153
for oldpath, newpath, id, kind, text_modified, meta_modified \
155
self.model.append(titer, [ oldpath, newpath ])
157
if len(delta.modified):
158
titer = self.model.append(None, [ _('Modified'), None ])
159
for path, id, kind, text_modified, meta_modified in delta.modified:
160
self.model.append(titer, [ path, path ])
162
self.treeview.expand_all()
163
self.set_title(description + " - bzrk diff")
165
def set_file(self, file_path):
167
for data in self.model:
168
for child in data.iterchildren():
169
if child[0] == file_path or child[1] == file_path:
173
raise NoSuchFile(file_path)
174
self.treeview.set_cursor(tv_path)
175
self.treeview.scroll_to_cell(tv_path)
177
def _treeview_cursor_cb(self, *args):
178
"""Callback for when the treeview cursor changes."""
179
(path, col) = self.treeview.get_cursor()
180
specific_files = [ self.model[path][1] ]
181
if specific_files == [ None ]:
183
elif specific_files == [ "" ]:
187
show_diff_trees(self.parent_tree, self.rev_tree, s, specific_files)
188
self.buffer.set_text(s.getvalue())