1
# Copyright (C) 2006 by Szilveszter Farkas (Phanatic) <szilveszter.farkas@gmail.com>
3
# Some parts of the code:
4
# Copyright (C) 2005 by Canonical Ltd.
5
# Author: Scott James Remnant <scott@ubuntu.com>
7
# This program is free software; you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 2 of the License, or
10
# (at your option) any later version.
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
17
# You should have received a copy of the GNU General Public License
18
# along with this program; if not, write to the Free Software
19
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
from cStringIO import StringIO
40
have_gtksourceview = True
42
have_gtksourceview = False
46
if bzrlib.version_info[1] < 9:
47
# function deprecated after 0.9
48
from bzrlib.delta import compare_trees
50
from bzrlib.diff import show_diff_trees
51
import bzrlib.errors as errors
52
from bzrlib.workingtree import WorkingTree
54
from dialog import OliveDialog
57
""" Display Diff window and perform the needed actions. """
58
def __init__(self, gladefile, comm):
59
""" Initialize the Diff window. """
60
self.gladefile = gladefile
61
self.glade = gtk.glade.XML(self.gladefile, 'window_diff')
65
self.dialog = OliveDialog(self.gladefile)
67
# Check if current location is a branch
69
(self.wt, path) = WorkingTree.open_containing(self.comm.get_path())
70
branch = self.wt.branch
71
except errors.NotBranchError:
77
file_id = self.wt.path2id(path)
79
self.notbranch = False
84
# Set the old working tree
85
self.old_tree = self.wt.branch.repository.revision_tree(self.wt.branch.last_revision())
87
# Get the Diff window widget
88
self.window = self.glade.get_widget('window_diff')
90
# Dictionary for signal_autoconnect
91
dic = { "on_button_diff_close_clicked": self.close,
92
"on_treeview_diff_files_cursor_changed": self.cursor_changed }
94
# Connect the signals to the handlers
95
self.glade.signal_autoconnect(dic)
97
# Create the file list
98
self._create_file_view()
100
# Generate initial diff
104
""" Display the Diff window. """
106
self.dialog.error_dialog('Directory is not a branch.')
108
self.window.show_all()
110
def _create_file_view(self):
111
""" Create the list of files. """
112
self.model = gtk.TreeStore(str, str)
113
self.treeview = self.glade.get_widget('treeview_diff_files')
114
self.treeview.set_model(self.model)
116
cell = gtk.CellRendererText()
117
cell.set_property("width-chars", 20)
118
column = gtk.TreeViewColumn()
119
column.pack_start(cell, expand=True)
120
column.add_attribute(cell, "text", 0)
121
self.treeview.append_column(column)
123
if have_gtksourceview:
124
self.buffer = gtksourceview.SourceBuffer()
125
slm = gtksourceview.SourceLanguagesManager()
126
gsl = slm.get_language_from_mime_type("text/x-patch")
127
self.buffer.set_language(gsl)
128
self.buffer.set_highlight(True)
130
sourceview = gtksourceview.SourceView(self.buffer)
132
self.buffer = gtk.TextBuffer()
133
sourceview = gtk.TextView(self.buffer)
135
sourceview.set_editable(False)
136
sourceview.modify_font(pango.FontDescription("Monospace"))
137
scrollwin_diff = self.glade.get_widget('scrolledwindow_diff_diff')
138
scrollwin_diff.add(sourceview)
140
def _init_diff(self):
141
""" Generate initial diff. """
143
if bzrlib.version_info[1] < 9:
144
delta = compare_trees(self.old_tree, self.wt)
146
delta = self.wt.changes_from(self.old_tree)
148
self.model.append(None, [ "Complete Diff", "" ])
151
titer = self.model.append(None, [ "Added", None ])
152
for path, id, kind in delta.added:
153
self.model.append(titer, [ path, path ])
155
if len(delta.removed):
156
titer = self.model.append(None, [ "Removed", None ])
157
for path, id, kind in delta.removed:
158
self.model.append(titer, [ path, path ])
160
if len(delta.renamed):
161
titer = self.model.append(None, [ "Renamed", None ])
162
for oldpath, newpath, id, kind, text_modified, meta_modified \
164
self.model.append(titer, [ oldpath, newpath ])
166
if len(delta.modified):
167
titer = self.model.append(None, [ "Modified", None ])
168
for path, id, kind, text_modified, meta_modified in delta.modified:
169
self.model.append(titer, [ path, path ])
171
self.treeview.expand_all()
173
def cursor_changed(self, *args):
174
""" Callback when the TreeView cursor changes. """
175
(path, col) = self.treeview.get_cursor()
176
specific_files = [ self.model[path][1] ]
177
if specific_files == [ None ]:
179
elif specific_files == [ "" ]:
183
show_diff_trees(self.old_tree, self.wt, s, specific_files)
184
self.buffer.set_text(s.getvalue())
186
def close(self, widget=None):
187
self.window.destroy()