1
# Copyright (C) 2005 Dan Loda <danloda@gmail.com>
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
from bzrlib import patiencediff, tsort
27
from bzrlib.errors import NoSuchRevision
28
from bzrlib.revision import NULL_REVISION, CURRENT_REVISION
30
from colormap import AnnotateColorMap, AnnotateColorSaturation
31
from bzrlib.plugins.gtk.revisionview import RevisionView
32
from bzrlib.plugins.gtk.window import Window
45
class GAnnotateWindow(Window):
46
"""Annotate window."""
48
def __init__(self, all=False, plain=False, parent=None, branch=None):
53
Window.__init__(self, parent)
55
self.set_icon(self.render_icon(gtk.STOCK_FIND, gtk.ICON_SIZE_BUTTON))
56
self.annotate_colormap = AnnotateColorSaturation()
63
def annotate(self, tree, branch, file_id):
67
self.file_id = file_id
68
self.revisionview.set_file_id(file_id)
69
self.revision_id = getattr(tree, 'get_revision_id',
70
lambda: CURRENT_REVISION)()
72
# [revision id, line number, author, revno, highlight color, line]
73
self.annomodel = gtk.ListStore(gobject.TYPE_STRING,
83
branch.repository.lock_read()
85
revno_map = self.branch.get_revision_id_to_revno_map()
86
for revision_id, revno in revno_map.iteritems():
87
self.dotted[revision_id] = '.'.join(str(num) for num in revno)
88
for line_no, (revision, revno, line)\
89
in enumerate(self._annotate(tree, file_id)):
90
if revision.revision_id == last_seen and not self.all:
93
last_seen = revision.revision_id
94
author = revision.get_apparent_author()
96
if revision.revision_id not in self.revisions:
97
self.revisions[revision.revision_id] = revision
99
self.annomodel.append([revision.revision_id,
106
self.annotations.append(revision)
110
self.annomodel.foreach(self._highlight_annotation, now)
112
branch.repository.unlock()
115
self.annoview.set_model(self.annomodel)
116
self.annoview.grab_focus()
117
my_revno = self.dotted.get(self.revision_id, 'current')
118
title = '%s (%s) - gannotate' % (self.tree.id2path(file_id), my_revno)
119
self.set_title(title)
121
def jump_to_line(self, lineno):
122
if lineno > len(self.annomodel) or lineno < 1:
124
# FIXME:should really deal with this in the gui. Perhaps a status
126
print("gannotate: Line number %d does't exist. Defaulting to "
132
self.annoview.set_cursor(row)
133
self.annoview.scroll_to_cell(row, use_align=True)
136
def _annotate(self, tree, file_id):
137
current_revision = FakeRevision(CURRENT_REVISION)
138
current_revision.committer = self.branch.get_config().username()
139
current_revision.timestamp = time.time()
140
current_revision.message = '[Not yet committed]'
141
current_revision.parent_ids = tree.get_parent_ids()
142
current_revision.properties['branch-nick'] = self.branch.nick
143
current_revno = '%d?' % (self.branch.revno() + 1)
144
repository = self.branch.repository
145
if self.revision_id == CURRENT_REVISION:
146
revision_id = self.branch.last_revision()
148
revision_id = self.revision_id
149
revision_cache = RevisionCache(repository, self.revisions)
150
for origin, text in tree.annotate_iter(file_id):
152
if rev_id == CURRENT_REVISION:
153
revision = current_revision
154
revno = current_revno
157
revision = revision_cache.get_revision(rev_id)
158
revno = self.dotted.get(rev_id, 'merge')
161
except NoSuchRevision:
162
revision = FakeRevision(rev_id)
165
yield revision, revno, text
167
def _highlight_annotation(self, model, path, iter, now):
168
revision_id, = model.get(iter, REVISION_ID_COL)
169
revision = self.revisions[revision_id]
170
model.set(iter, HIGHLIGHT_COLOR_COL,
171
self.annotate_colormap.get_color(revision, now))
173
def _selected_revision(self):
174
(path, col) = self.annoview.get_cursor()
177
return self.annomodel[path][REVISION_ID_COL]
179
def _activate_selected_revision(self, w):
180
rev_id = self._selected_revision()
181
if not rev_id or rev_id == NULL_REVISION:
183
selected = self.revisions[rev_id]
184
self.revisionview.set_revision(selected)
185
if (len(selected.parent_ids) != 0 and selected.parent_ids[0] not in
190
self.back_button.set_sensitive(enable_back)
193
self.revisionview = self._create_log_view()
194
self.annoview = self._create_annotate_view()
196
vbox = gtk.VBox(False)
199
sw = gtk.ScrolledWindow()
200
sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
201
sw.set_shadow_type(gtk.SHADOW_IN)
202
sw.add(self.annoview)
203
self.annoview.gwindow = self
210
hbox = gtk.HBox(False, 6)
211
self.back_button = self._create_back_button()
212
hbox.pack_start(self.back_button, expand=False, fill=True)
213
self.forward_button = self._create_forward_button()
214
hbox.pack_start(self.forward_button, expand=False, fill=True)
216
vbox.pack_start(hbox, expand=False, fill=True)
218
self.pane = pane = gtk.VPaned()
220
pane.add2(self.revisionview)
222
vbox.pack_start(pane, expand=True, fill=True)
224
self._search = SearchBox()
225
swbox.pack_start(self._search, expand=False, fill=True)
226
accels = gtk.AccelGroup()
227
accels.connect_group(gtk.keysyms.f, gtk.gdk.CONTROL_MASK,
229
self._search_by_text)
230
accels.connect_group(gtk.keysyms.g, gtk.gdk.CONTROL_MASK,
232
self._search_by_line)
233
self.add_accel_group(accels)
237
def _search_by_text(self, accel_group, window, key, modifiers):
238
self._search.show_for('text')
239
self._search.set_target(self.annoview, TEXT_LINE_COL)
241
def _search_by_line(self, accel_group, window, key, modifiers):
242
self._search.show_for('line')
243
self._search.set_target(self.annoview, LINE_NUM_COL)
245
def line_diff(self, tv, path, tvc):
247
revision = self.annotations[row]
248
repository = self.branch.repository
249
if revision.revision_id == CURRENT_REVISION:
251
tree2 = self.tree.basis_tree()
253
tree1 = repository.revision_tree(revision.revision_id)
254
if len(revision.parent_ids) > 0:
255
tree2 = repository.revision_tree(revision.parent_ids[0])
257
tree2 = repository.revision_tree(NULL_REVISION)
258
from bzrlib.plugins.gtk.diff import DiffWindow
259
window = DiffWindow()
260
window.set_diff("Diff for line %d" % (row+1), tree1, tree2)
261
window.set_file(tree1.id2path(self.file_id))
265
def _create_annotate_view(self):
267
tv.set_rules_hint(False)
268
tv.connect("cursor-changed", self._activate_selected_revision)
270
tv.connect("row-activated", self.line_diff)
272
cell = gtk.CellRendererText()
273
cell.set_property("xalign", 1.0)
274
cell.set_property("ypad", 0)
275
cell.set_property("family", "Monospace")
276
cell.set_property("cell-background-gdk",
277
tv.get_style().bg[gtk.STATE_NORMAL])
278
col = gtk.TreeViewColumn()
279
col.set_resizable(False)
280
col.pack_start(cell, expand=True)
281
col.add_attribute(cell, "text", LINE_NUM_COL)
282
tv.append_column(col)
284
cell = gtk.CellRendererText()
285
cell.set_property("ypad", 0)
286
cell.set_property("ellipsize", pango.ELLIPSIZE_END)
287
cell.set_property("cell-background-gdk",
288
self.get_style().bg[gtk.STATE_NORMAL])
289
col = gtk.TreeViewColumn("Committer")
290
col.set_resizable(True)
291
col.pack_start(cell, expand=True)
292
col.add_attribute(cell, "text", COMMITTER_COL)
293
tv.append_column(col)
295
cell = gtk.CellRendererText()
296
cell.set_property("xalign", 1.0)
297
cell.set_property("ypad", 0)
298
cell.set_property("cell-background-gdk",
299
self.get_style().bg[gtk.STATE_NORMAL])
300
col = gtk.TreeViewColumn("Revno")
301
col.set_resizable(False)
302
col.pack_start(cell, expand=True)
303
col.add_attribute(cell, "markup", REVNO_COL)
304
tv.append_column(col)
306
cell = gtk.CellRendererText()
307
cell.set_property("ypad", 0)
308
cell.set_property("family", "Monospace")
309
col = gtk.TreeViewColumn()
310
col.set_resizable(False)
311
col.pack_start(cell, expand=True)
312
# col.add_attribute(cell, "foreground", HIGHLIGHT_COLOR_COL)
313
col.add_attribute(cell, "background", HIGHLIGHT_COLOR_COL)
314
col.add_attribute(cell, "text", TEXT_LINE_COL)
315
tv.append_column(col)
317
# FIXME: Now that C-f is now used for search by text we
318
# may as well disable the auto search.
319
tv.set_search_column(LINE_NUM_COL)
323
def _create_log_view(self):
324
lv = RevisionView(self._branch)
328
def _create_back_button(self):
329
button = gtk.Button()
330
button.set_use_stock(True)
331
button.set_label("gtk-go-back")
332
button.connect("clicked", lambda w: self.go_back())
333
button.set_relief(gtk.RELIEF_NONE)
337
def _create_forward_button(self):
338
button = gtk.Button()
339
button.set_use_stock(True)
340
button.set_label("gtk-go-forward")
341
button.connect("clicked", lambda w: self.go_forward())
342
button.set_relief(gtk.RELIEF_NONE)
344
button.set_sensitive(False)
348
last_tree = self.tree
349
rev_id = self._selected_revision()
350
parent_id = self.revisions[rev_id].parent_ids[0]
351
target_tree = self.branch.repository.revision_tree(parent_id)
352
if self._go(target_tree):
353
self.history.append(last_tree)
354
self.forward_button.set_sensitive(True)
356
self._no_back.add(parent_id)
357
self.back_button.set_sensitive(False)
359
def go_forward(self):
360
if len(self.history) == 0:
362
target_tree = self.history.pop()
363
if len(self.history) == 0:
364
self.forward_button.set_sensitive(False)
365
self._go(target_tree)
367
def _go(self, target_tree):
368
rev_id = self._selected_revision()
369
if self.file_id in target_tree:
370
offset = self.get_scroll_offset(target_tree)
371
(row,), col = self.annoview.get_cursor()
372
self.annotate(target_tree, self.branch, self.file_id)
376
self.annoview.set_cursor(new_row)
381
def get_scroll_offset(self, tree):
382
old = self.tree.get_file(self.file_id)
383
new = tree.get_file(self.file_id)
384
(row,), col = self.annoview.get_cursor()
385
matcher = patiencediff.PatienceSequenceMatcher(None, old.readlines(),
387
for i, j, n in matcher.get_matching_blocks():
395
For when a revision is referenced but not present.
398
def __init__(self, revision_id, committer='?', nick=None):
399
self.revision_id = revision_id
401
self.committer = committer
407
def get_apparent_author(self):
408
return self.committer
411
class RevisionCache(object):
412
"""A caching revision source"""
413
def __init__(self, real_source, seed_cache=None):
414
self.__real_source = real_source
415
if seed_cache is None:
418
self.__cache = dict(seed_cache)
420
def get_revision(self, revision_id):
421
if revision_id not in self.__cache:
422
revision = self.__real_source.get_revision(revision_id)
423
self.__cache[revision_id] = revision
424
return self.__cache[revision_id]
426
class SearchBox(gtk.HBox):
427
"""A button box for searching in text or lines of annotations"""
429
gtk.HBox.__init__(self, False, 6)
432
button = gtk.Button()
434
image.set_from_stock('gtk-stop', gtk.ICON_SIZE_BUTTON)
435
button.set_image(image)
436
button.set_relief(gtk.RELIEF_NONE)
437
button.connect("clicked", lambda w: self.hide_all())
438
self.pack_start(button, expand=False, fill=False)
443
self.pack_start(label, expand=False, fill=False)
447
entry.connect("activate", lambda w, d: self._do_search(d),
449
self.pack_start(entry, expand=False, fill=False)
451
# Next/previous buttons
452
button = gtk.Button('_Next')
454
image.set_from_stock('gtk-go-forward', gtk.ICON_SIZE_BUTTON)
455
button.set_image(image)
456
button.connect("clicked", lambda w, d: self._do_search(d),
458
self.pack_start(button, expand=False, fill=False)
460
button = gtk.Button('_Previous')
462
image.set_from_stock('gtk-go-back', gtk.ICON_SIZE_BUTTON)
463
button.set_image(image)
464
button.connect("clicked", lambda w, d: self._do_search(d),
466
self.pack_start(button, expand=False, fill=False)
469
check = gtk.CheckButton('Match case')
470
self._match_case = check
471
self.pack_start(check, expand=False, fill=False)
473
check = gtk.CheckButton('Regexp')
474
check.connect("toggled", lambda w: self._set_label())
476
self.pack_start(check, expand=False, fill=False)
480
# Note that we stay hidden (we do not call self.show_all())
483
def show_for(self, kind):
487
# Hide unrelated buttons
489
self._match_case.hide()
492
self._entry.grab_focus()
494
def _set_label(self):
495
if self._kind == 'line':
496
self._label.set_text('Find Line: ')
498
if self._regexp.get_active():
499
self._label.set_text('Find Regexp: ')
501
self._label.set_text('Find Text: ')
503
def set_target(self, view,column):
505
self._column = column
507
def _match(self, model, iterator, column):
508
matching_case = self._match_case.get_active()
509
string, = model.get(iterator, column)
510
key = self._entry.get_text()
511
if self._regexp.get_active():
513
match = re.compile(key).search(string, 1)
515
match = re.compile(key, re.I).search(string, 1)
517
if not matching_case:
518
string = string.lower()
520
match = string.find(key) != -1
524
def _iterate_rows_forward(self, model, start):
525
model_size = len(model)
527
while model_size != 0:
528
if current >= model_size: current = 0
529
yield model.get_iter_from_string('%d' % current)
530
if current == start: raise StopIteration
533
def _iterate_rows_backward(self, model, start):
534
model_size = len(model)
536
while model_size != 0:
537
if current < 0: current = model_size - 1
538
yield model.get_iter_from_string('%d' % current)
539
if current == start: raise StopIteration
542
def _do_search(self, direction):
543
if direction == 'forward':
544
iterate = self._iterate_rows_forward
546
iterate = self._iterate_rows_backward
548
model, sel = self._view.get_selection().get_selected()
552
path = model.get_string_from_iter(sel)
555
for row in iterate(model, start):
556
if self._match(model, row, self._column):
557
path = model.get_path(row)
558
self._view.set_cursor(path)
559
self._view.scroll_to_cell(path, use_align=True)