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.logview import LogView
44
class GAnnotateWindow(gtk.Window):
45
"""Annotate window."""
47
def __init__(self, all=False, plain=False):
51
gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
53
self.set_icon(self.render_icon(gtk.STOCK_FIND, gtk.ICON_SIZE_BUTTON))
54
self.annotate_colormap = AnnotateColorSaturation()
61
def annotate(self, tree, branch, file_id):
65
self.file_id = file_id
66
self.revision_id = getattr(tree, 'get_revision_id',
67
lambda: CURRENT_REVISION)()
69
# [revision id, line number, committer, revno, highlight color, line]
70
self.annomodel = gtk.ListStore(gobject.TYPE_STRING,
80
branch.repository.lock_read()
81
for line_no, (revision, revno, line)\
82
in enumerate(self._annotate(tree, file_id)):
83
if revision.revision_id == last_seen and not self.all:
84
revno = committer = ""
86
last_seen = revision.revision_id
87
committer = revision.committer
89
if revision.revision_id not in self.revisions:
90
self.revisions[revision.revision_id] = revision
92
self.annomodel.append([revision.revision_id,
99
self.annotations.append(revision)
103
self.annomodel.foreach(self._highlight_annotation, now)
105
branch.repository.unlock()
108
self.annoview.set_model(self.annomodel)
109
self.annoview.grab_focus()
111
def jump_to_line(self, lineno):
112
if lineno > len(self.annomodel) or lineno < 1:
114
# FIXME:should really deal with this in the gui. Perhaps a status
116
print("gannotate: Line number %d does't exist. Defaulting to "
122
self.annoview.set_cursor(row)
123
self.annoview.scroll_to_cell(row, use_align=True)
125
def _dotted_revnos(self, repository, revision_id):
126
"""Return a dict of revision_id -> dotted revno
128
:param repository: The repository to get the graph from
129
:param revision_id: The last revision for which this info is needed
131
graph = repository.get_revision_graph(revision_id)
133
for n, revision_id, d, revno, e in tsort.merge_sort(graph,
134
revision_id, generate_revno=True):
135
dotted[revision_id] = '.'.join(str(num) for num in revno)
138
def _annotate(self, tree, file_id):
139
current_revision = FakeRevision(CURRENT_REVISION)
140
current_revision.committer = self.branch.get_config().username()
141
current_revision.timestamp = time.time()
142
current_revision.message = '[Not yet committed]'
143
current_revision.parent_ids = tree.get_parent_ids()
144
current_revision.properties['branch-nick'] = self.branch.nick
145
current_revno = '%d?' % (self.branch.revno() + 1)
146
repository = self.branch.repository
147
if self.revision_id == CURRENT_REVISION:
148
revision_id = self.branch.last_revision()
150
revision_id = self.revision_id
151
dotted = self._dotted_revnos(repository, revision_id)
152
revision_cache = RevisionCache(repository, self.revisions)
153
for origin, text in tree.annotate_iter(file_id):
155
if rev_id == CURRENT_REVISION:
156
revision = current_revision
157
revno = current_revno
160
revision = revision_cache.get_revision(rev_id)
161
revno = dotted.get(rev_id, 'merge')
164
except NoSuchRevision:
165
revision = FakeRevision(rev_id)
168
yield revision, revno, text
170
def _highlight_annotation(self, model, path, iter, now):
171
revision_id, = model.get(iter, REVISION_ID_COL)
172
revision = self.revisions[revision_id]
173
model.set(iter, HIGHLIGHT_COLOR_COL,
174
self.annotate_colormap.get_color(revision, now))
176
def _selected_revision(self):
177
(path, col) = self.annoview.get_cursor()
180
return self.annomodel[path][REVISION_ID_COL]
182
def _activate_selected_revision(self, w):
183
rev_id = self._selected_revision()
186
selected = self.revisions[rev_id]
187
self.logview.set_revision(selected)
188
if (len(selected.parent_ids) != 0 and selected.parent_ids[0] not in
193
self.back_button.set_sensitive(enable_back)
196
self.logview = self._create_log_view()
197
self.annoview = self._create_annotate_view()
199
vbox = gtk.VBox(False)
202
sw = gtk.ScrolledWindow()
203
sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
204
sw.set_shadow_type(gtk.SHADOW_IN)
205
sw.add(self.annoview)
206
self.annoview.gwindow = self
213
hbox = gtk.HBox(False, 6)
214
self.back_button = self._create_back_button()
215
hbox.pack_start(self.back_button, expand=False, fill=True)
216
self.forward_button = self._create_forward_button()
217
hbox.pack_start(self.forward_button, expand=False, fill=True)
219
vbox.pack_start(hbox, expand=False, fill=True)
221
self.pane = pane = gtk.VPaned()
223
pane.add2(self.logview)
225
vbox.pack_start(pane, expand=True, fill=True)
227
self._search = SearchBox()
228
swbox.pack_start(self._search, expand=False, fill=True)
229
accels = gtk.AccelGroup()
230
accels.connect_group(gtk.keysyms.f, gtk.gdk.CONTROL_MASK,
232
self._search_by_text)
233
accels.connect_group(gtk.keysyms.g, gtk.gdk.CONTROL_MASK,
235
self._search_by_line)
236
self.add_accel_group(accels)
240
def _search_by_text(self, accel_group, window, key, modifiers):
241
self._search.show_for('text')
242
self._search.set_target(self.annoview, TEXT_LINE_COL)
244
def _search_by_line(self, accel_group, window, key, modifiers):
245
self._search.show_for('line')
246
self._search.set_target(self.annoview, LINE_NUM_COL)
248
def row_diff(self, tv, path, tvc):
250
revision = self.annotations[row]
251
repository = self.branch.repository
252
if revision.revision_id == CURRENT_REVISION:
254
tree2 = self.tree.basis_tree()
256
tree1 = repository.revision_tree(revision.revision_id)
257
if len(revision.parent_ids) > 0:
258
tree2 = repository.revision_tree(revision.parent_ids[0])
260
tree2 = repository.revision_tree(NULL_REVISION)
261
from bzrlib.plugins.gtk.diff import DiffWindow
262
window = DiffWindow()
263
window.set_diff("Diff for row %d" % (row+1), tree1, tree2)
264
window.set_file(tree1.id2path(self.file_id))
268
def _create_annotate_view(self):
270
tv.set_rules_hint(False)
271
tv.connect("cursor-changed", self._activate_selected_revision)
273
tv.connect("row-activated", self.row_diff)
275
cell = gtk.CellRendererText()
276
cell.set_property("xalign", 1.0)
277
cell.set_property("ypad", 0)
278
cell.set_property("family", "Monospace")
279
cell.set_property("cell-background-gdk",
280
tv.get_style().bg[gtk.STATE_NORMAL])
281
col = gtk.TreeViewColumn()
282
col.set_resizable(False)
283
col.pack_start(cell, expand=True)
284
col.add_attribute(cell, "text", LINE_NUM_COL)
285
tv.append_column(col)
287
cell = gtk.CellRendererText()
288
cell.set_property("ypad", 0)
289
cell.set_property("ellipsize", pango.ELLIPSIZE_END)
290
cell.set_property("cell-background-gdk",
291
self.get_style().bg[gtk.STATE_NORMAL])
292
col = gtk.TreeViewColumn("Committer")
293
col.set_resizable(True)
294
col.pack_start(cell, expand=True)
295
col.add_attribute(cell, "text", COMMITTER_COL)
296
tv.append_column(col)
298
cell = gtk.CellRendererText()
299
cell.set_property("xalign", 1.0)
300
cell.set_property("ypad", 0)
301
cell.set_property("cell-background-gdk",
302
self.get_style().bg[gtk.STATE_NORMAL])
303
col = gtk.TreeViewColumn("Revno")
304
col.set_resizable(False)
305
col.pack_start(cell, expand=True)
306
col.add_attribute(cell, "markup", REVNO_COL)
307
tv.append_column(col)
309
cell = gtk.CellRendererText()
310
cell.set_property("ypad", 0)
311
cell.set_property("family", "Monospace")
312
col = gtk.TreeViewColumn()
313
col.set_resizable(False)
314
col.pack_start(cell, expand=True)
315
# col.add_attribute(cell, "foreground", HIGHLIGHT_COLOR_COL)
316
col.add_attribute(cell, "background", HIGHLIGHT_COLOR_COL)
317
col.add_attribute(cell, "text", TEXT_LINE_COL)
318
tv.append_column(col)
320
# FIXME: Now that C-f is now used for search by text we
321
# may as well disable the auto search.
322
tv.set_search_column(LINE_NUM_COL)
326
def _create_log_view(self):
331
def _create_back_button(self):
332
button = gtk.Button()
333
button.set_use_stock(True)
334
button.set_label("gtk-go-back")
335
button.connect("clicked", lambda w: self.go_back())
336
button.set_relief(gtk.RELIEF_NONE)
340
def _create_forward_button(self):
341
button = gtk.Button()
342
button.set_use_stock(True)
343
button.set_label("gtk-go-forward")
344
button.connect("clicked", lambda w: self.go_forward())
345
button.set_relief(gtk.RELIEF_NONE)
347
button.set_sensitive(False)
351
last_tree = self.tree
352
rev_id = self._selected_revision()
353
parent_id = self.revisions[rev_id].parent_ids[0]
354
target_tree = self.branch.repository.revision_tree(parent_id)
355
if self._go(target_tree):
356
self.history.append(last_tree)
357
self.forward_button.set_sensitive(True)
359
self._no_back.add(parent_id)
360
self.back_button.set_sensitive(False)
362
def go_forward(self):
363
if len(self.history) == 0:
365
target_tree = self.history.pop()
366
if len(self.history) == 0:
367
self.forward_button.set_sensitive(False)
368
self._go(target_tree)
370
def _go(self, target_tree):
371
rev_id = self._selected_revision()
372
if self.file_id in target_tree:
373
offset = self.get_scroll_offset(target_tree)
374
(row,), col = self.annoview.get_cursor()
375
self.annotate(target_tree, self.branch, self.file_id)
379
self.annoview.set_cursor(new_row)
384
def get_scroll_offset(self, tree):
385
old = self.tree.get_file(self.file_id)
386
new = tree.get_file(self.file_id)
387
(row,), col = self.annoview.get_cursor()
388
matcher = patiencediff.PatienceSequenceMatcher(None, old.readlines(),
390
for i, j, n in matcher.get_matching_blocks():
399
For when a revision is referenced but not present.
402
def __init__(self, revision_id, committer='?', nick=None):
403
self.revision_id = revision_id
405
self.committer = committer
412
class RevisionCache(object):
413
"""A caching revision source"""
414
def __init__(self, real_source, seed_cache=None):
415
self.__real_source = real_source
416
if seed_cache is None:
419
self.__cache = dict(seed_cache)
421
def get_revision(self, revision_id):
422
if revision_id not in self.__cache:
423
revision = self.__real_source.get_revision(revision_id)
424
self.__cache[revision_id] = revision
425
return self.__cache[revision_id]
427
class SearchBox(gtk.HBox):
428
"""A button box for searching in text or lines of annotations"""
430
gtk.HBox.__init__(self, False, 6)
433
button = gtk.Button()
435
image.set_from_stock('gtk-stop', gtk.ICON_SIZE_BUTTON)
436
button.set_image(image)
437
button.set_relief(gtk.RELIEF_NONE)
438
button.connect("clicked", lambda w: self.hide_all())
439
self.pack_start(button, expand=False, fill=False)
444
self.pack_start(label, expand=False, fill=False)
448
entry.connect("activate", lambda w, d: self._do_search(d),
450
self.pack_start(entry, expand=False, fill=False)
452
# Next/previous buttons
453
button = gtk.Button('_Next')
455
image.set_from_stock('gtk-go-forward', gtk.ICON_SIZE_BUTTON)
456
button.set_image(image)
457
button.connect("clicked", lambda w, d: self._do_search(d),
459
self.pack_start(button, expand=False, fill=False)
461
button = gtk.Button('_Previous')
463
image.set_from_stock('gtk-go-back', gtk.ICON_SIZE_BUTTON)
464
button.set_image(image)
465
button.connect("clicked", lambda w, d: self._do_search(d),
467
self.pack_start(button, expand=False, fill=False)
470
check = gtk.CheckButton('Match case')
471
self._match_case = check
472
self.pack_start(check, expand=False, fill=False)
474
check = gtk.CheckButton('Regexp')
475
check.connect("toggled", lambda w: self._set_label())
477
self.pack_start(check, expand=False, fill=False)
481
# Note that we stay hidden (we do not call self.show_all())
484
def show_for(self, kind):
488
# Hide unrelated buttons
490
self._match_case.hide()
493
self._entry.grab_focus()
495
def _set_label(self):
496
if self._kind == 'line':
497
self._label.set_text('Find Line: ')
499
if self._regexp.get_active():
500
self._label.set_text('Find Regexp: ')
502
self._label.set_text('Find Text: ')
504
def set_target(self, view,column):
506
self._column = column
508
def _match(self, model, iterator, column):
509
matching_case = self._match_case.get_active()
510
string, = model.get(iterator, column)
511
key = self._entry.get_text()
512
if self._regexp.get_active():
514
match = re.compile(key).search(string, 1)
516
match = re.compile(key, re.I).search(string, 1)
518
if not matching_case:
519
string = string.lower()
521
match = string.find(key) != -1
525
def _iterate_rows_forward(self, model, start):
526
model_size = len(model)
528
while model_size != 0:
529
if current >= model_size: current = 0
530
yield model.get_iter_from_string('%d' % current)
531
if current == start: raise StopIteration
534
def _iterate_rows_backward(self, model, start):
535
model_size = len(model)
537
while model_size != 0:
538
if current < 0: current = model_size - 1
539
yield model.get_iter_from_string('%d' % current)
540
if current == start: raise StopIteration
543
def _do_search(self, direction):
544
if direction == 'forward':
545
iterate = self._iterate_rows_forward
547
iterate = self._iterate_rows_backward
549
model, sel = self._view.get_selection().get_selected()
553
path = model.get_string_from_iter(sel)
556
for row in iterate(model, start):
557
if self._match(model, row, self._column):
558
path = model.get_path(row)
559
self._view.set_cursor(path)
560
self._view.scroll_to_cell(path, use_align=True)