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, author, 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.properties.get('author',
90
if revision.revision_id not in self.revisions:
91
self.revisions[revision.revision_id] = revision
93
self.annomodel.append([revision.revision_id,
100
self.annotations.append(revision)
104
self.annomodel.foreach(self._highlight_annotation, now)
106
branch.repository.unlock()
109
self.annoview.set_model(self.annomodel)
110
self.annoview.grab_focus()
112
def jump_to_line(self, lineno):
113
if lineno > len(self.annomodel) or lineno < 1:
115
# FIXME:should really deal with this in the gui. Perhaps a status
117
print("gannotate: Line number %d does't exist. Defaulting to "
123
self.annoview.set_cursor(row)
124
self.annoview.scroll_to_cell(row, use_align=True)
126
def _dotted_revnos(self, repository, revision_id):
127
"""Return a dict of revision_id -> dotted revno
129
:param repository: The repository to get the graph from
130
:param revision_id: The last revision for which this info is needed
132
graph = repository.get_revision_graph(revision_id)
134
for n, revision_id, d, revno, e in tsort.merge_sort(graph,
135
revision_id, generate_revno=True):
136
dotted[revision_id] = '.'.join(str(num) for num in revno)
139
def _annotate(self, tree, file_id):
140
current_revision = FakeRevision(CURRENT_REVISION)
141
current_revision.committer = self.branch.get_config().username()
142
current_revision.timestamp = time.time()
143
current_revision.message = '[Not yet committed]'
144
current_revision.parent_ids = tree.get_parent_ids()
145
current_revision.properties['branch-nick'] = self.branch.nick
146
current_revno = '%d?' % (self.branch.revno() + 1)
147
repository = self.branch.repository
148
if self.revision_id == CURRENT_REVISION:
149
revision_id = self.branch.last_revision()
151
revision_id = self.revision_id
152
dotted = self._dotted_revnos(repository, revision_id)
153
revision_cache = RevisionCache(repository, self.revisions)
154
for origin, text in tree.annotate_iter(file_id):
156
if rev_id == CURRENT_REVISION:
157
revision = current_revision
158
revno = current_revno
161
revision = revision_cache.get_revision(rev_id)
162
revno = dotted.get(rev_id, 'merge')
165
except NoSuchRevision:
166
revision = FakeRevision(rev_id)
169
yield revision, revno, text
171
def _highlight_annotation(self, model, path, iter, now):
172
revision_id, = model.get(iter, REVISION_ID_COL)
173
revision = self.revisions[revision_id]
174
model.set(iter, HIGHLIGHT_COLOR_COL,
175
self.annotate_colormap.get_color(revision, now))
177
def _selected_revision(self):
178
(path, col) = self.annoview.get_cursor()
181
return self.annomodel[path][REVISION_ID_COL]
183
def _activate_selected_revision(self, w):
184
rev_id = self._selected_revision()
187
selected = self.revisions[rev_id]
188
self.logview.set_revision(selected)
189
if (len(selected.parent_ids) != 0 and selected.parent_ids[0] not in
194
self.back_button.set_sensitive(enable_back)
197
self.logview = self._create_log_view()
198
self.annoview = self._create_annotate_view()
200
vbox = gtk.VBox(False)
203
sw = gtk.ScrolledWindow()
204
sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
205
sw.set_shadow_type(gtk.SHADOW_IN)
206
sw.add(self.annoview)
207
self.annoview.gwindow = self
214
hbox = gtk.HBox(False, 6)
215
self.back_button = self._create_back_button()
216
hbox.pack_start(self.back_button, expand=False, fill=True)
217
self.forward_button = self._create_forward_button()
218
hbox.pack_start(self.forward_button, expand=False, fill=True)
220
vbox.pack_start(hbox, expand=False, fill=True)
222
self.pane = pane = gtk.VPaned()
224
pane.add2(self.logview)
226
vbox.pack_start(pane, expand=True, fill=True)
228
self._search = SearchBox()
229
swbox.pack_start(self._search, expand=False, fill=True)
230
accels = gtk.AccelGroup()
231
accels.connect_group(gtk.keysyms.f, gtk.gdk.CONTROL_MASK,
233
self._search_by_text)
234
accels.connect_group(gtk.keysyms.g, gtk.gdk.CONTROL_MASK,
236
self._search_by_line)
237
self.add_accel_group(accels)
241
def _search_by_text(self, accel_group, window, key, modifiers):
242
self._search.show_for('text')
243
self._search.set_target(self.annoview, TEXT_LINE_COL)
245
def _search_by_line(self, accel_group, window, key, modifiers):
246
self._search.show_for('line')
247
self._search.set_target(self.annoview, LINE_NUM_COL)
249
def row_diff(self, tv, path, tvc):
251
revision = self.annotations[row]
252
repository = self.branch.repository
253
if revision.revision_id == CURRENT_REVISION:
255
tree2 = self.tree.basis_tree()
257
tree1 = repository.revision_tree(revision.revision_id)
258
if len(revision.parent_ids) > 0:
259
tree2 = repository.revision_tree(revision.parent_ids[0])
261
tree2 = repository.revision_tree(NULL_REVISION)
262
from bzrlib.plugins.gtk.diff import DiffWindow
263
window = DiffWindow()
264
window.set_diff("Diff for row %d" % (row+1), tree1, tree2)
265
window.set_file(tree1.id2path(self.file_id))
269
def _create_annotate_view(self):
271
tv.set_rules_hint(False)
272
tv.connect("cursor-changed", self._activate_selected_revision)
274
tv.connect("row-activated", self.row_diff)
276
cell = gtk.CellRendererText()
277
cell.set_property("xalign", 1.0)
278
cell.set_property("ypad", 0)
279
cell.set_property("family", "Monospace")
280
cell.set_property("cell-background-gdk",
281
tv.get_style().bg[gtk.STATE_NORMAL])
282
col = gtk.TreeViewColumn()
283
col.set_resizable(False)
284
col.pack_start(cell, expand=True)
285
col.add_attribute(cell, "text", LINE_NUM_COL)
286
tv.append_column(col)
288
cell = gtk.CellRendererText()
289
cell.set_property("ypad", 0)
290
cell.set_property("ellipsize", pango.ELLIPSIZE_END)
291
cell.set_property("cell-background-gdk",
292
self.get_style().bg[gtk.STATE_NORMAL])
293
col = gtk.TreeViewColumn("Committer")
294
col.set_resizable(True)
295
col.pack_start(cell, expand=True)
296
col.add_attribute(cell, "text", COMMITTER_COL)
297
tv.append_column(col)
299
cell = gtk.CellRendererText()
300
cell.set_property("xalign", 1.0)
301
cell.set_property("ypad", 0)
302
cell.set_property("cell-background-gdk",
303
self.get_style().bg[gtk.STATE_NORMAL])
304
col = gtk.TreeViewColumn("Revno")
305
col.set_resizable(False)
306
col.pack_start(cell, expand=True)
307
col.add_attribute(cell, "markup", REVNO_COL)
308
tv.append_column(col)
310
cell = gtk.CellRendererText()
311
cell.set_property("ypad", 0)
312
cell.set_property("family", "Monospace")
313
col = gtk.TreeViewColumn()
314
col.set_resizable(False)
315
col.pack_start(cell, expand=True)
316
# col.add_attribute(cell, "foreground", HIGHLIGHT_COLOR_COL)
317
col.add_attribute(cell, "background", HIGHLIGHT_COLOR_COL)
318
col.add_attribute(cell, "text", TEXT_LINE_COL)
319
tv.append_column(col)
321
# FIXME: Now that C-f is now used for search by text we
322
# may as well disable the auto search.
323
tv.set_search_column(LINE_NUM_COL)
327
def _create_log_view(self):
332
def _create_back_button(self):
333
button = gtk.Button()
334
button.set_use_stock(True)
335
button.set_label("gtk-go-back")
336
button.connect("clicked", lambda w: self.go_back())
337
button.set_relief(gtk.RELIEF_NONE)
341
def _create_forward_button(self):
342
button = gtk.Button()
343
button.set_use_stock(True)
344
button.set_label("gtk-go-forward")
345
button.connect("clicked", lambda w: self.go_forward())
346
button.set_relief(gtk.RELIEF_NONE)
348
button.set_sensitive(False)
352
last_tree = self.tree
353
rev_id = self._selected_revision()
354
parent_id = self.revisions[rev_id].parent_ids[0]
355
target_tree = self.branch.repository.revision_tree(parent_id)
356
if self._go(target_tree):
357
self.history.append(last_tree)
358
self.forward_button.set_sensitive(True)
360
self._no_back.add(parent_id)
361
self.back_button.set_sensitive(False)
363
def go_forward(self):
364
if len(self.history) == 0:
366
target_tree = self.history.pop()
367
if len(self.history) == 0:
368
self.forward_button.set_sensitive(False)
369
self._go(target_tree)
371
def _go(self, target_tree):
372
rev_id = self._selected_revision()
373
if self.file_id in target_tree:
374
offset = self.get_scroll_offset(target_tree)
375
(row,), col = self.annoview.get_cursor()
376
self.annotate(target_tree, self.branch, self.file_id)
380
self.annoview.set_cursor(new_row)
385
def get_scroll_offset(self, tree):
386
old = self.tree.get_file(self.file_id)
387
new = tree.get_file(self.file_id)
388
(row,), col = self.annoview.get_cursor()
389
matcher = patiencediff.PatienceSequenceMatcher(None, old.readlines(),
391
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
413
class RevisionCache(object):
414
"""A caching revision source"""
415
def __init__(self, real_source, seed_cache=None):
416
self.__real_source = real_source
417
if seed_cache is None:
420
self.__cache = dict(seed_cache)
422
def get_revision(self, revision_id):
423
if revision_id not in self.__cache:
424
revision = self.__real_source.get_revision(revision_id)
425
self.__cache[revision_id] = revision
426
return self.__cache[revision_id]
428
class SearchBox(gtk.HBox):
429
"""A button box for searching in text or lines of annotations"""
431
gtk.HBox.__init__(self, False, 6)
434
button = gtk.Button()
436
image.set_from_stock('gtk-stop', gtk.ICON_SIZE_BUTTON)
437
button.set_image(image)
438
button.set_relief(gtk.RELIEF_NONE)
439
button.connect("clicked", lambda w: self.hide_all())
440
self.pack_start(button, expand=False, fill=False)
445
self.pack_start(label, expand=False, fill=False)
449
entry.connect("activate", lambda w, d: self._do_search(d),
451
self.pack_start(entry, expand=False, fill=False)
453
# Next/previous buttons
454
button = gtk.Button('_Next')
456
image.set_from_stock('gtk-go-forward', gtk.ICON_SIZE_BUTTON)
457
button.set_image(image)
458
button.connect("clicked", lambda w, d: self._do_search(d),
460
self.pack_start(button, expand=False, fill=False)
462
button = gtk.Button('_Previous')
464
image.set_from_stock('gtk-go-back', gtk.ICON_SIZE_BUTTON)
465
button.set_image(image)
466
button.connect("clicked", lambda w, d: self._do_search(d),
468
self.pack_start(button, expand=False, fill=False)
471
check = gtk.CheckButton('Match case')
472
self._match_case = check
473
self.pack_start(check, expand=False, fill=False)
475
check = gtk.CheckButton('Regexp')
476
check.connect("toggled", lambda w: self._set_label())
478
self.pack_start(check, expand=False, fill=False)
482
# Note that we stay hidden (we do not call self.show_all())
485
def show_for(self, kind):
489
# Hide unrelated buttons
491
self._match_case.hide()
494
self._entry.grab_focus()
496
def _set_label(self):
497
if self._kind == 'line':
498
self._label.set_text('Find Line: ')
500
if self._regexp.get_active():
501
self._label.set_text('Find Regexp: ')
503
self._label.set_text('Find Text: ')
505
def set_target(self, view,column):
507
self._column = column
509
def _match(self, model, iterator, column):
510
matching_case = self._match_case.get_active()
511
string, = model.get(iterator, column)
512
key = self._entry.get_text()
513
if self._regexp.get_active():
515
match = re.compile(key).search(string, 1)
517
match = re.compile(key, re.I).search(string, 1)
519
if not matching_case:
520
string = string.lower()
522
match = string.find(key) != -1
526
def _iterate_rows_forward(self, model, start):
527
model_size = len(model)
529
while model_size != 0:
530
if current >= model_size: current = 0
531
yield model.get_iter_from_string('%d' % current)
532
if current == start: raise StopIteration
535
def _iterate_rows_backward(self, model, start):
536
model_size = len(model)
538
while model_size != 0:
539
if current < 0: current = model_size - 1
540
yield model.get_iter_from_string('%d' % current)
541
if current == start: raise StopIteration
544
def _do_search(self, direction):
545
if direction == 'forward':
546
iterate = self._iterate_rows_forward
548
iterate = self._iterate_rows_backward
550
model, sel = self._view.get_selection().get_selected()
554
path = model.get_string_from_iter(sel)
557
for row in iterate(model, start):
558
if self._match(model, row, self._column):
559
path = model.get_path(row)
560
self._view.set_cursor(path)
561
self._view.scroll_to_cell(path, use_align=True)