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[0] == 0) and (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
# Get some important widgets
68
self.window = self.glade.get_widget('window_diff')
69
self.treeview = self.glade.get_widget('treeview_diff_files')
71
# Check if current location is a branch
73
(self.wt, path) = WorkingTree.open_containing(self.comm.get_path())
74
branch = self.wt.branch
75
except errors.NotBranchError:
81
file_id = self.wt.path2id(path)
83
self.notbranch = False
88
# Set the old working tree
89
self.old_tree = self.wt.branch.repository.revision_tree(self.wt.branch.last_revision())
91
# Dictionary for signal_autoconnect
92
dic = { "on_button_diff_close_clicked": self.close,
93
"on_treeview_diff_files_cursor_changed": self.cursor_changed }
95
# Connect the signals to the handlers
96
self.glade.signal_autoconnect(dic)
98
# Create the file list
99
self._create_file_view()
101
# Generate initial diff
105
""" Display the Diff window. """
107
self.dialog.error_dialog('Directory is not a branch.')
110
self.window.show_all()
112
def _create_file_view(self):
113
""" Create the list of files. """
114
self.model = gtk.TreeStore(str, str)
115
self.treeview.set_model(self.model)
117
cell = gtk.CellRendererText()
118
cell.set_property("width-chars", 20)
119
column = gtk.TreeViewColumn()
120
column.pack_start(cell, expand=True)
121
column.add_attribute(cell, "text", 0)
122
self.treeview.append_column(column)
124
if have_gtksourceview:
125
self.buffer = gtksourceview.SourceBuffer()
126
slm = gtksourceview.SourceLanguagesManager()
127
gsl = slm.get_language_from_mime_type("text/x-patch")
128
self.buffer.set_language(gsl)
129
self.buffer.set_highlight(True)
131
sourceview = gtksourceview.SourceView(self.buffer)
133
self.buffer = gtk.TextBuffer()
134
sourceview = gtk.TextView(self.buffer)
136
sourceview.set_editable(False)
137
sourceview.modify_font(pango.FontDescription("Monospace"))
138
scrollwin_diff = self.glade.get_widget('scrolledwindow_diff_diff')
139
scrollwin_diff.add(sourceview)
141
def _init_diff(self):
142
""" Generate initial diff. """
144
if (bzrlib.version_info[0] == 0) and (bzrlib.version_info[1] < 9):
145
delta = compare_trees(self.old_tree, self.wt)
147
delta = self.wt.changes_from(self.old_tree)
149
self.model.append(None, [ "Complete Diff", "" ])
152
titer = self.model.append(None, [ "Added", None ])
153
for path, id, kind in delta.added:
154
self.model.append(titer, [ path, path ])
156
if len(delta.removed):
157
titer = self.model.append(None, [ "Removed", None ])
158
for path, id, kind in delta.removed:
159
self.model.append(titer, [ path, path ])
161
if len(delta.renamed):
162
titer = self.model.append(None, [ "Renamed", None ])
163
for oldpath, newpath, id, kind, text_modified, meta_modified \
165
self.model.append(titer, [ oldpath, newpath ])
167
if len(delta.modified):
168
titer = self.model.append(None, [ "Modified", None ])
169
for path, id, kind, text_modified, meta_modified in delta.modified:
170
self.model.append(titer, [ path, path ])
172
self.treeview.expand_all()
174
def cursor_changed(self, *args):
175
""" Callback when the TreeView cursor changes. """
176
(path, col) = self.treeview.get_cursor()
177
specific_files = [ self.model[path][1] ]
178
if specific_files == [ None ]:
180
elif specific_files == [ "" ]:
184
show_diff_trees(self.old_tree, self.wt, s, specific_files)
185
self.buffer.set_text(s.getvalue())
187
def close(self, widget=None):
188
self.window.destroy()