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
38
have_gtksourceview = True
40
have_gtksourceview = False
43
if (bzrlib.version_info[0] == 0) and (bzrlib.version_info[1] < 9):
44
# function deprecated after 0.9
45
from bzrlib.delta import compare_trees
47
from bzrlib.diff import show_diff_trees
48
from bzrlib.errors import NoSuchFile
51
class DiffWindow(gtk.Window):
54
This object represents and manages a single window containing the
55
differences between two revisions on a branch.
59
gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
60
self.set_border_width(0)
61
self.set_title("bzrk diff")
63
# Use two thirds of the screen by default
64
screen = self.get_screen()
65
monitor = screen.get_monitor_geometry(0)
66
width = int(monitor.width * 0.66)
67
height = int(monitor.height * 0.66)
68
self.set_default_size(width, height)
73
"""Construct the window contents."""
74
# The window consists of a pane containing: the
75
# hierarchical list of files on the left, and the diff
76
# for the currently selected file on the right.
81
# The file hierarchy: a scrollable treeview
82
scrollwin = gtk.ScrolledWindow()
83
scrollwin.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
84
scrollwin.set_shadow_type(gtk.SHADOW_IN)
88
self.model = gtk.TreeStore(str, str)
89
self.treeview = gtk.TreeView(self.model)
90
self.treeview.set_headers_visible(False)
91
self.treeview.set_search_column(1)
92
self.treeview.connect("cursor-changed", self._treeview_cursor_cb)
93
scrollwin.add(self.treeview)
96
cell = gtk.CellRendererText()
97
cell.set_property("width-chars", 20)
98
column = gtk.TreeViewColumn()
99
column.pack_start(cell, expand=True)
100
column.add_attribute(cell, "text", 0)
101
self.treeview.append_column(column)
103
# The diffs of the selected file: a scrollable source or
105
scrollwin = gtk.ScrolledWindow()
106
scrollwin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
107
scrollwin.set_shadow_type(gtk.SHADOW_IN)
108
pane.pack2(scrollwin)
111
if have_gtksourceview:
112
self.buffer = gtksourceview.SourceBuffer()
113
slm = gtksourceview.SourceLanguagesManager()
114
gsl = slm.get_language_from_mime_type("text/x-patch")
115
self.buffer.set_language(gsl)
116
self.buffer.set_highlight(True)
118
sourceview = gtksourceview.SourceView(self.buffer)
120
self.buffer = gtk.TextBuffer()
121
sourceview = gtk.TextView(self.buffer)
123
sourceview.set_editable(False)
124
sourceview.modify_font(pango.FontDescription("Monospace"))
125
scrollwin.add(sourceview)
128
def set_diff(self, description, rev_tree, parent_tree):
129
"""Set the differences showed by this window.
131
Compares the two trees and populates the window with the
134
self.rev_tree = rev_tree
135
self.parent_tree = parent_tree
139
if (bzrlib.version_info[0] == 0) and (bzrlib.version_info[1] < 9):
140
delta = compare_trees(self.parent_tree, self.rev_tree)
142
delta = self.rev_tree.changes_from(self.parent_tree)
144
self.model.append(None, [ _('Complete Diff'), "" ])
147
titer = self.model.append(None, [ _('Added'), None ])
148
for path, id, kind in delta.added:
149
self.model.append(titer, [ path, path ])
151
if len(delta.removed):
152
titer = self.model.append(None, [ _('Removed'), None ])
153
for path, id, kind in delta.removed:
154
self.model.append(titer, [ path, path ])
156
if len(delta.renamed):
157
titer = self.model.append(None, [ _('Renamed'), None ])
158
for oldpath, newpath, id, kind, text_modified, meta_modified \
160
self.model.append(titer, [ oldpath, newpath ])
162
if len(delta.modified):
163
titer = self.model.append(None, [ _('Modified'), None ])
164
for path, id, kind, text_modified, meta_modified in delta.modified:
165
self.model.append(titer, [ path, path ])
167
self.treeview.expand_all()
168
self.set_title(description + " - bzrk diff")
170
def set_file(self, file_path):
172
for data in self.model:
173
for child in data.iterchildren():
174
if child[0] == file_path or child[1] == file_path:
178
raise NoSuchFile(file_path)
179
self.treeview.set_cursor(tv_path)
180
self.treeview.scroll_to_cell(tv_path)
182
def _treeview_cursor_cb(self, *args):
183
"""Callback for when the treeview cursor changes."""
184
(path, col) = self.treeview.get_cursor()
185
specific_files = [ self.model[path][1] ]
186
if specific_files == [ None ]:
188
elif specific_files == [ "" ]:
192
show_diff_trees(self.parent_tree, self.rev_tree, s, specific_files)
193
self.buffer.set_text(s.getvalue().decode(sys.getdefaultencoding(), 'replace'))