24
21
have_gtksourceview = True
25
22
except ImportError:
26
23
have_gtksourceview = False
33
from bzrlib import osutils
34
from bzrlib.diff import show_diff_trees, internal_diff
27
from bzrlib.diff import show_diff_trees
35
28
from bzrlib.errors import NoSuchFile
36
from bzrlib.trace import warning
37
from bzrlib.plugins.gtk.window import Window
40
class DiffView(gtk.ScrolledWindow):
41
"""This is the soft and chewy filling for a DiffWindow."""
44
gtk.ScrolledWindow.__init__(self)
48
self.parent_tree = None
51
self.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
52
self.set_shadow_type(gtk.SHADOW_IN)
54
if have_gtksourceview:
55
self.buffer = gtksourceview.SourceBuffer()
56
slm = gtksourceview.SourceLanguagesManager()
57
gsl = slm.get_language_from_mime_type("text/x-patch")
59
self.apply_gedit_colors(gsl)
60
self.apply_colordiff_colors(gsl)
61
self.buffer.set_language(gsl)
62
self.buffer.set_highlight(True)
64
sourceview = gtksourceview.SourceView(self.buffer)
66
self.buffer = gtk.TextBuffer()
67
sourceview = gtk.TextView(self.buffer)
69
sourceview.set_editable(False)
70
sourceview.modify_font(pango.FontDescription("Monospace"))
75
def apply_gedit_colors(lang):
76
"""Set style for lang to that specified in gedit configuration.
78
This method needs the gconf module.
80
:param lang: a gtksourceview.SourceLanguage object.
82
GEDIT_SYNTAX_PATH = '/apps/gedit-2/preferences/syntax_highlighting'
83
GEDIT_LANG_PATH = GEDIT_SYNTAX_PATH + '/' + lang.get_id()
85
client = gconf.client_get_default()
86
client.add_dir(GEDIT_LANG_PATH, gconf.CLIENT_PRELOAD_NONE)
88
for tag in lang.get_tags():
90
gconf_key = GEDIT_LANG_PATH + '/' + tag_id
91
style_string = client.get_string(gconf_key)
93
if style_string is None:
96
# function to get a bool from a string that's either '0' or '1'
97
string_bool = lambda x: bool(int(x))
99
# style_string is a string like "2/#FFCCAA/#000000/0/1/0/0"
100
# values are: mask, fg, bg, italic, bold, underline, strike
101
# this packs them into (str_value, attr_name, conv_func) tuples
102
items = zip(style_string.split('/'), ['mask', 'foreground',
103
'background', 'italic', 'bold', 'underline', 'strikethrough' ],
104
[ int, gtk.gdk.color_parse, gtk.gdk.color_parse, string_bool,
105
string_bool, string_bool, string_bool ]
108
style = gtksourceview.SourceTagStyle()
110
# XXX The mask attribute controls whether the present values of
111
# foreground and background color should in fact be used. Ideally
112
# (and that's what gedit does), one could set all three attributes,
113
# and let the TagStyle object figure out which colors to use.
114
# However, in the GtkSourceview python bindings, the mask attribute
115
# is read-only, and it's derived instead from the colors being
116
# set or not. This means that we have to sometimes refrain from
117
# setting fg or bg colors, depending on the value of the mask.
118
# This code could go away if mask were writable.
119
mask = int(items[0][0])
120
if not (mask & 1): # GTK_SOURCE_TAG_STYLE_USE_BACKGROUND
122
if not (mask & 2): # GTK_SOURCE_TAG_STYLE_USE_FOREGROUND
124
items[0:1] = [] # skip the mask unconditionally
126
for value, attr, func in items:
130
warning('gconf key %s contains an invalid value: %s'
133
setattr(style, attr, value)
135
lang.set_tag_style(tag_id, style)
138
def apply_colordiff_colors(lang):
139
"""Set style colors for lang using the colordiff configuration file.
141
Both ~/.colordiffrc and ~/.colordiffrc.bzr-gtk are read.
143
:param lang: a "Diff" gtksourceview.SourceLanguage object.
147
for f in ('~/.colordiffrc', '~/.colordiffrc.bzr-gtk'):
148
f = os.path.expanduser(f)
149
if os.path.exists(f):
153
warning('could not open file %s: %s' % (f, str(e)))
155
colors.update(DiffView.parse_colordiffrc(f))
159
# ~/.colordiffrc does not exist
163
# map GtkSourceView tags to colordiff names
164
# since GSV is richer, accept new names for extra bits,
165
# defaulting to old names if they're not present
166
'Added@32@line': ['newtext'],
167
'Removed@32@line': ['oldtext'],
168
'Location': ['location', 'diffstuff'],
169
'Diff@32@file': ['file', 'diffstuff'],
170
'Special@32@case': ['specialcase', 'diffstuff'],
173
for tag in lang.get_tags():
174
tag_id = tag.get_id()
175
keys = mapping.get(tag_id, [])
179
color = colors.get(key, None)
180
if color is not None:
186
style = gtksourceview.SourceTagStyle()
188
style.foreground = gtk.gdk.color_parse(color)
190
warning('not a valid color: %s' % color)
192
lang.set_tag_style(tag_id, style)
195
def parse_colordiffrc(fileobj):
196
"""Parse fileobj as a colordiff configuration file.
198
:return: A dict with the key -> value pairs.
202
if re.match(r'^\s*#', line):
206
key, val = line.split('=', 1)
207
colors[key.strip()] = val.strip()
210
def set_trees(self, rev_tree, parent_tree):
211
self.rev_tree = rev_tree
212
self.parent_tree = parent_tree
213
# self._build_delta()
215
# def _build_delta(self):
216
# self.parent_tree.lock_read()
217
# self.rev_tree.lock_read()
219
# self.delta = _iter_changes_to_status(self.parent_tree, self.rev_tree)
220
# self.path_to_status = {}
221
# self.path_to_diff = {}
222
# source_inv = self.parent_tree.inventory
223
# target_inv = self.rev_tree.inventory
224
# for (file_id, real_path, change_type, display_path) in self.delta:
225
# self.path_to_status[real_path] = u'=== %s %s' % (change_type, display_path)
226
# if change_type in ('modified', 'renamed and modified'):
227
# source_ie = source_inv[file_id]
228
# target_ie = target_inv[file_id]
230
# source_ie.diff(internal_diff, *old path, *old_tree,
231
# *new_path, target_ie, self.rev_tree,
233
# self.path_to_diff[real_path] =
236
# self.rev_tree.unlock()
237
# self.parent_tree.unlock()
239
def show_diff(self, specific_files):
241
show_diff_trees(self.parent_tree, self.rev_tree, s, specific_files,
242
old_label='', new_label='',
243
# path_encoding=sys.getdefaultencoding()
244
# The default is utf-8, but we interpret the file
245
# contents as getdefaultencoding(), so we should
246
# probably try to make the paths in the same encoding.
248
# str.decode(encoding, 'replace') doesn't do anything. Because if a
249
# character is not valid in 'encoding' there is nothing to replace, the
250
# 'replace' is for 'str.encode()'
252
decoded = s.getvalue().decode(sys.getdefaultencoding())
253
except UnicodeDecodeError:
255
decoded = s.getvalue().decode('UTF-8')
256
except UnicodeDecodeError:
257
decoded = s.getvalue().decode('iso-8859-1')
258
# This always works, because every byte has a valid
259
# mapping from iso-8859-1 to Unicode
260
# TextBuffer must contain pure UTF-8 data
261
self.buffer.set_text(decoded.encode('UTF-8'))
264
class DiffWindow(Window):
31
class DiffWindow(gtk.Window):
267
34
This object represents and manages a single window containing the
268
35
differences between two revisions on a branch.
271
def __init__(self, parent=None):
272
Window.__init__(self, parent)
39
gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
273
40
self.set_border_width(0)
274
41
self.set_title("bzrk diff")
377
162
if specific_files == [ None ]:
379
164
elif specific_files == [ "" ]:
380
specific_files = None
382
self.diff_view.show_diff(specific_files)
385
def _iter_changes_to_status(source, target):
386
"""Determine the differences between trees.
388
This is a wrapper around _iter_changes which just yields more
389
understandable results.
391
:param source: The source tree (basis tree)
392
:param target: The target tree
393
:return: A list of (file_id, real_path, change_type, display_path)
398
renamed_and_modified = 'renamed and modified'
399
modified = 'modified'
400
kind_changed = 'kind changed'
402
# TODO: Handle metadata changes
409
for (file_id, paths, changed_content, versioned, parent_ids, names,
410
kinds, executables) in target._iter_changes(source):
412
# Skip the root entry if it isn't very interesting
413
if parent_ids == (None, None):
420
source_marker = osutils.kind_marker(kinds[0])
422
assert kinds[0] is not None
423
marker = osutils.kind_marker(kinds[0])
425
marker = osutils.kind_marker(kinds[1])
428
if real_path is None:
430
assert real_path is not None
431
display_path = real_path + marker
433
present_source = versioned[0] and kinds[0] is not None
434
present_target = versioned[1] and kinds[1] is not None
436
if present_source != present_target:
440
assert present_source
441
change_type = removed
442
elif names[0] != names[1] or parent_ids[0] != parent_ids[1]:
444
if changed_content or executables[0] != executables[1]:
446
change_type = renamed_and_modified
448
change_type = renamed
449
display_path = (paths[0] + source_marker
450
+ ' => ' + paths[1] + marker)
451
elif kinds[0] != kinds[1]:
452
change_type = kind_changed
453
display_path = (paths[0] + source_marker
454
+ ' => ' + paths[1] + marker)
455
elif changed_content is True or executables[0] != executables[1]:
456
change_type = modified
458
assert False, "How did we get here?"
460
status.append((file_id, real_path, change_type, display_path))
168
show_diff_trees(self.parent_tree, self.rev_tree, s, specific_files)
169
self.buffer.set_text(s.getvalue().decode(sys.getdefaultencoding(), 'replace'))