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):
52
Window.__init__(self, parent)
54
self.set_icon(self.render_icon(gtk.STOCK_FIND, gtk.ICON_SIZE_BUTTON))
55
self.annotate_colormap = AnnotateColorSaturation()
62
def annotate(self, tree, branch, file_id):
66
self.file_id = file_id
67
self.revisionview.set_file_id(file_id)
68
self.revision_id = getattr(tree, 'get_revision_id',
69
lambda: CURRENT_REVISION)()
71
# [revision id, line number, author, revno, highlight color, line]
72
self.annomodel = gtk.ListStore(gobject.TYPE_STRING,
82
branch.repository.lock_read()
83
for line_no, (revision, revno, line)\
84
in enumerate(self._annotate(tree, file_id)):
85
if revision.revision_id == last_seen and not self.all:
88
last_seen = revision.revision_id
89
author = revision.get_apparent_author()
91
if revision.revision_id not in self.revisions:
92
self.revisions[revision.revision_id] = revision
94
self.annomodel.append([revision.revision_id,
101
self.annotations.append(revision)
105
self.annomodel.foreach(self._highlight_annotation, now)
107
branch.repository.unlock()
110
self.annoview.set_model(self.annomodel)
111
self.annoview.grab_focus()
113
def jump_to_line(self, lineno):
114
if lineno > len(self.annomodel) or lineno < 1:
116
# FIXME:should really deal with this in the gui. Perhaps a status
118
print("gannotate: Line number %d does't exist. Defaulting to "
124
self.annoview.set_cursor(row)
125
self.annoview.scroll_to_cell(row, use_align=True)
127
def _dotted_revnos(self, repository, revision_id):
128
"""Return a dict of revision_id -> dotted revno
130
:param repository: The repository to get the graph from
131
:param revision_id: The last revision for which this info is needed
133
graph = repository.get_revision_graph(revision_id)
135
for n, revision_id, d, revno, e in tsort.merge_sort(graph,
136
revision_id, generate_revno=True):
137
dotted[revision_id] = '.'.join(str(num) for num in revno)
140
def _annotate(self, tree, file_id):
141
current_revision = FakeRevision(CURRENT_REVISION)
142
current_revision.committer = self.branch.get_config().username()
143
current_revision.timestamp = time.time()
144
current_revision.message = '[Not yet committed]'
145
current_revision.parent_ids = tree.get_parent_ids()
146
current_revision.properties['branch-nick'] = self.branch.nick
147
current_revno = '%d?' % (self.branch.revno() + 1)
148
repository = self.branch.repository
149
if self.revision_id == CURRENT_REVISION:
150
revision_id = self.branch.last_revision()
152
revision_id = self.revision_id
153
dotted = self._dotted_revnos(repository, revision_id)
154
revision_cache = RevisionCache(repository, self.revisions)
155
for origin, text in tree.annotate_iter(file_id):
157
if rev_id == CURRENT_REVISION:
158
revision = current_revision
159
revno = current_revno
162
revision = revision_cache.get_revision(rev_id)
163
revno = dotted.get(rev_id, 'merge')
166
except NoSuchRevision:
167
revision = FakeRevision(rev_id)
170
yield revision, revno, text
172
def _highlight_annotation(self, model, path, iter, now):
173
revision_id, = model.get(iter, REVISION_ID_COL)
174
revision = self.revisions[revision_id]
175
model.set(iter, HIGHLIGHT_COLOR_COL,
176
self.annotate_colormap.get_color(revision, now))
178
def _selected_revision(self):
179
(path, col) = self.annoview.get_cursor()
182
return self.annomodel[path][REVISION_ID_COL]
184
def _activate_selected_revision(self, w):
185
rev_id = self._selected_revision()
188
selected = self.revisions[rev_id]
189
self.revisionview.set_revision(selected)
190
if (len(selected.parent_ids) != 0 and selected.parent_ids[0] not in
195
self.back_button.set_sensitive(enable_back)
198
self.revisionview = self._create_log_view()
199
self.annoview = self._create_annotate_view()
201
vbox = gtk.VBox(False)
204
sw = gtk.ScrolledWindow()
205
sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
206
sw.set_shadow_type(gtk.SHADOW_IN)
207
sw.add(self.annoview)
208
self.annoview.gwindow = self
215
hbox = gtk.HBox(False, 6)
216
self.back_button = self._create_back_button()
217
hbox.pack_start(self.back_button, expand=False, fill=True)
218
self.forward_button = self._create_forward_button()
219
hbox.pack_start(self.forward_button, expand=False, fill=True)
221
vbox.pack_start(hbox, expand=False, fill=True)
223
self.pane = pane = gtk.VPaned()
225
pane.add2(self.revisionview)
227
vbox.pack_start(pane, expand=True, fill=True)
229
self._search = SearchBox()
230
swbox.pack_start(self._search, expand=False, fill=True)
231
accels = gtk.AccelGroup()
232
accels.connect_group(gtk.keysyms.f, gtk.gdk.CONTROL_MASK,
234
self._search_by_text)
235
accels.connect_group(gtk.keysyms.g, gtk.gdk.CONTROL_MASK,
237
self._search_by_line)
238
self.add_accel_group(accels)
242
def _search_by_text(self, accel_group, window, key, modifiers):
243
self._search.show_for('text')
244
self._search.set_target(self.annoview, TEXT_LINE_COL)
246
def _search_by_line(self, accel_group, window, key, modifiers):
247
self._search.show_for('line')
248
self._search.set_target(self.annoview, LINE_NUM_COL)
250
def row_diff(self, tv, path, tvc):
252
revision = self.annotations[row]
253
repository = self.branch.repository
254
if revision.revision_id == CURRENT_REVISION:
256
tree2 = self.tree.basis_tree()
258
tree1 = repository.revision_tree(revision.revision_id)
259
if len(revision.parent_ids) > 0:
260
tree2 = repository.revision_tree(revision.parent_ids[0])
262
tree2 = repository.revision_tree(NULL_REVISION)
263
from bzrlib.plugins.gtk.diff import DiffWindow
264
window = DiffWindow()
265
window.set_diff("Diff for row %d" % (row+1), tree1, tree2)
266
window.set_file(tree1.id2path(self.file_id))
270
def _create_annotate_view(self):
272
tv.set_rules_hint(False)
273
tv.connect("cursor-changed", self._activate_selected_revision)
275
tv.connect("row-activated", self.row_diff)
277
cell = gtk.CellRendererText()
278
cell.set_property("xalign", 1.0)
279
cell.set_property("ypad", 0)
280
cell.set_property("family", "Monospace")
281
cell.set_property("cell-background-gdk",
282
tv.get_style().bg[gtk.STATE_NORMAL])
283
col = gtk.TreeViewColumn()
284
col.set_resizable(False)
285
col.pack_start(cell, expand=True)
286
col.add_attribute(cell, "text", LINE_NUM_COL)
287
tv.append_column(col)
289
cell = gtk.CellRendererText()
290
cell.set_property("ypad", 0)
291
cell.set_property("ellipsize", pango.ELLIPSIZE_END)
292
cell.set_property("cell-background-gdk",
293
self.get_style().bg[gtk.STATE_NORMAL])
294
col = gtk.TreeViewColumn("Committer")
295
col.set_resizable(True)
296
col.pack_start(cell, expand=True)
297
col.add_attribute(cell, "text", COMMITTER_COL)
298
tv.append_column(col)
300
cell = gtk.CellRendererText()
301
cell.set_property("xalign", 1.0)
302
cell.set_property("ypad", 0)
303
cell.set_property("cell-background-gdk",
304
self.get_style().bg[gtk.STATE_NORMAL])
305
col = gtk.TreeViewColumn("Revno")
306
col.set_resizable(False)
307
col.pack_start(cell, expand=True)
308
col.add_attribute(cell, "markup", REVNO_COL)
309
tv.append_column(col)
311
cell = gtk.CellRendererText()
312
cell.set_property("ypad", 0)
313
cell.set_property("family", "Monospace")
314
col = gtk.TreeViewColumn()
315
col.set_resizable(False)
316
col.pack_start(cell, expand=True)
317
# col.add_attribute(cell, "foreground", HIGHLIGHT_COLOR_COL)
318
col.add_attribute(cell, "background", HIGHLIGHT_COLOR_COL)
319
col.add_attribute(cell, "text", TEXT_LINE_COL)
320
tv.append_column(col)
322
# FIXME: Now that C-f is now used for search by text we
323
# may as well disable the auto search.
324
tv.set_search_column(LINE_NUM_COL)
328
def _create_log_view(self):
333
def _create_back_button(self):
334
button = gtk.Button()
335
button.set_use_stock(True)
336
button.set_label("gtk-go-back")
337
button.connect("clicked", lambda w: self.go_back())
338
button.set_relief(gtk.RELIEF_NONE)
342
def _create_forward_button(self):
343
button = gtk.Button()
344
button.set_use_stock(True)
345
button.set_label("gtk-go-forward")
346
button.connect("clicked", lambda w: self.go_forward())
347
button.set_relief(gtk.RELIEF_NONE)
349
button.set_sensitive(False)
353
last_tree = self.tree
354
rev_id = self._selected_revision()
355
parent_id = self.revisions[rev_id].parent_ids[0]
356
target_tree = self.branch.repository.revision_tree(parent_id)
357
if self._go(target_tree):
358
self.history.append(last_tree)
359
self.forward_button.set_sensitive(True)
361
self._no_back.add(parent_id)
362
self.back_button.set_sensitive(False)
364
def go_forward(self):
365
if len(self.history) == 0:
367
target_tree = self.history.pop()
368
if len(self.history) == 0:
369
self.forward_button.set_sensitive(False)
370
self._go(target_tree)
372
def _go(self, target_tree):
373
rev_id = self._selected_revision()
374
if self.file_id in target_tree:
375
offset = self.get_scroll_offset(target_tree)
376
(row,), col = self.annoview.get_cursor()
377
self.annotate(target_tree, self.branch, self.file_id)
381
self.annoview.set_cursor(new_row)
386
def get_scroll_offset(self, tree):
387
old = self.tree.get_file(self.file_id)
388
new = tree.get_file(self.file_id)
389
(row,), col = self.annoview.get_cursor()
390
matcher = patiencediff.PatienceSequenceMatcher(None, old.readlines(),
392
for i, j, n in matcher.get_matching_blocks():
400
For when a revision is referenced but not present.
403
def __init__(self, revision_id, committer='?', nick=None):
404
self.revision_id = revision_id
406
self.committer = committer
412
def get_apparent_author(self):
413
return self.committer
416
class RevisionCache(object):
417
"""A caching revision source"""
418
def __init__(self, real_source, seed_cache=None):
419
self.__real_source = real_source
420
if seed_cache is None:
423
self.__cache = dict(seed_cache)
425
def get_revision(self, revision_id):
426
if revision_id not in self.__cache:
427
revision = self.__real_source.get_revision(revision_id)
428
self.__cache[revision_id] = revision
429
return self.__cache[revision_id]
431
class SearchBox(gtk.HBox):
432
"""A button box for searching in text or lines of annotations"""
434
gtk.HBox.__init__(self, False, 6)
437
button = gtk.Button()
439
image.set_from_stock('gtk-stop', gtk.ICON_SIZE_BUTTON)
440
button.set_image(image)
441
button.set_relief(gtk.RELIEF_NONE)
442
button.connect("clicked", lambda w: self.hide_all())
443
self.pack_start(button, expand=False, fill=False)
448
self.pack_start(label, expand=False, fill=False)
452
entry.connect("activate", lambda w, d: self._do_search(d),
454
self.pack_start(entry, expand=False, fill=False)
456
# Next/previous buttons
457
button = gtk.Button('_Next')
459
image.set_from_stock('gtk-go-forward', gtk.ICON_SIZE_BUTTON)
460
button.set_image(image)
461
button.connect("clicked", lambda w, d: self._do_search(d),
463
self.pack_start(button, expand=False, fill=False)
465
button = gtk.Button('_Previous')
467
image.set_from_stock('gtk-go-back', gtk.ICON_SIZE_BUTTON)
468
button.set_image(image)
469
button.connect("clicked", lambda w, d: self._do_search(d),
471
self.pack_start(button, expand=False, fill=False)
474
check = gtk.CheckButton('Match case')
475
self._match_case = check
476
self.pack_start(check, expand=False, fill=False)
478
check = gtk.CheckButton('Regexp')
479
check.connect("toggled", lambda w: self._set_label())
481
self.pack_start(check, expand=False, fill=False)
485
# Note that we stay hidden (we do not call self.show_all())
488
def show_for(self, kind):
492
# Hide unrelated buttons
494
self._match_case.hide()
497
self._entry.grab_focus()
499
def _set_label(self):
500
if self._kind == 'line':
501
self._label.set_text('Find Line: ')
503
if self._regexp.get_active():
504
self._label.set_text('Find Regexp: ')
506
self._label.set_text('Find Text: ')
508
def set_target(self, view,column):
510
self._column = column
512
def _match(self, model, iterator, column):
513
matching_case = self._match_case.get_active()
514
string, = model.get(iterator, column)
515
key = self._entry.get_text()
516
if self._regexp.get_active():
518
match = re.compile(key).search(string, 1)
520
match = re.compile(key, re.I).search(string, 1)
522
if not matching_case:
523
string = string.lower()
525
match = string.find(key) != -1
529
def _iterate_rows_forward(self, model, start):
530
model_size = len(model)
532
while model_size != 0:
533
if current >= model_size: current = 0
534
yield model.get_iter_from_string('%d' % current)
535
if current == start: raise StopIteration
538
def _iterate_rows_backward(self, model, start):
539
model_size = len(model)
541
while model_size != 0:
542
if current < 0: current = model_size - 1
543
yield model.get_iter_from_string('%d' % current)
544
if current == start: raise StopIteration
547
def _do_search(self, direction):
548
if direction == 'forward':
549
iterate = self._iterate_rows_forward
551
iterate = self._iterate_rows_backward
553
model, sel = self._view.get_selection().get_selected()
557
path = model.get_string_from_iter(sel)
560
for row in iterate(model, start):
561
if self._match(model, row, self._column):
562
path = model.get_path(row)
563
self._view.set_cursor(path)
564
self._view.scroll_to_cell(path, use_align=True)