83
73
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
74
for line_no, (revision, revno, line)\
89
in enumerate(self._annotate(tree, file_id)):
75
in enumerate(self._annotate(branch, file_id)):
90
76
if revision.revision_id == last_seen and not self.all:
77
revno = committer = ""
93
79
last_seen = revision.revision_id
94
author = ", ".join(revision.get_apparent_authors())
80
committer = revision.committer
96
82
if revision.revision_id not in self.revisions:
97
83
self.revisions[revision.revision_id] = revision
99
85
self.annomodel.append([revision.revision_id,
104
90
line.rstrip("\r\n")
106
self.annotations.append(revision)
108
93
if not self.plain:
110
self.annomodel.foreach(self._highlight_annotation, now)
94
self._set_oldest_newest()
95
# Recall that calling activate_default will emit "span-changed",
96
# so self._span_changed_cb will take care of initial highlighting
97
self.span_selector.activate_default()
112
99
branch.repository.unlock()
115
102
self.annoview.set_model(self.annomodel)
116
103
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
105
def jump_to_line(self, lineno):
122
106
if lineno > len(self.annomodel) or lineno < 1:
126
110
print("gannotate: Line number %d does't exist. Defaulting to "
127
111
"line 1." % lineno)
132
115
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._get_nick(local=True)
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):
117
def _annotate(self, branch, file_id):
118
rev_hist = branch.revision_history()
119
repository = branch.repository
120
rev_tree = repository.revision_tree(branch.last_revision())
121
rev_id = rev_tree.inventory[file_id].revision
122
weave = repository.weave_store.get_weave(file_id,
123
branch.get_transaction())
125
revision_cache = RevisionCache(repository)
126
for origin, text in weave.annotate_iter(rev_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)
129
revision = revision_cache.get_revision(rev_id)
130
if rev_id in rev_hist:
131
revno = branch.revision_id_to_revno(rev_id)
134
except NoSuchRevision:
135
revision = NoneRevision(rev_id)
165
138
yield revision, revno, text
140
def _set_oldest_newest(self):
141
rev_dates = map(lambda i: self.revisions[i].timestamp, self.revisions)
142
oldest = min(rev_dates)
143
newest = max(rev_dates)
145
span = self._span_from_seconds(time.time() - oldest)
146
self.span_selector.set_to_oldest_span(span)
148
span = self._span_from_seconds(newest - oldest)
149
self.span_selector.set_newest_to_oldest_span(span)
151
def _span_from_seconds(self, seconds):
152
return (seconds / (24 * 60 * 60))
154
def _span_changed_cb(self, w, span):
155
self.annotate_colormap.set_span(span)
157
self.annomodel.foreach(self._highlight_annotation, now)
167
159
def _highlight_annotation(self, model, path, iter, now):
168
160
revision_id, = model.get(iter, REVISION_ID_COL)
169
161
revision = self.revisions[revision_id]
170
162
model.set(iter, HIGHLIGHT_COLOR_COL,
171
163
self.annotate_colormap.get_color(revision, now))
173
def _selected_revision(self):
165
def _show_log(self, w):
174
166
(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)
167
rev_id = self.annomodel[path][REVISION_ID_COL]
168
self.logview.set_revision(self.revisions[rev_id])
192
170
def _create(self):
193
self.revisionview = self._create_log_view()
171
self.logview = self._create_log_view()
194
172
self.annoview = self._create_annotate_view()
173
self.span_selector = self._create_span_selector()
196
vbox = gtk.VBox(False)
175
vbox = gtk.VBox(False, 12)
176
vbox.set_border_width(12)
199
179
sw = gtk.ScrolledWindow()
200
180
sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
201
181
sw.set_shadow_type(gtk.SHADOW_IN)
202
182
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)
215
self.find_button = self._create_find_button()
216
hbox.pack_start(self.find_button, expand=False, fill=True)
217
self.goto_button = self._create_goto_button()
218
hbox.pack_start(self.goto_button, expand=False, fill=True)
220
vbox.pack_start(hbox, expand=False, fill=True)
222
185
self.pane = pane = gtk.VPaned()
224
pane.add2(self.revisionview)
187
pane.add2(self.logview)
226
189
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)
191
hbox = gtk.HBox(True, 6)
192
hbox.pack_start(self.span_selector, expand=False, fill=True)
193
hbox.pack_start(self._create_button_box(), expand=False, fill=True)
195
vbox.pack_start(hbox, expand=False, fill=True)
241
def _search_by_text(self, *ignored): # (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, *ignored): # accel_group, window, key, modifiers):
246
self._search.show_for('line')
247
self._search.set_target(self.annoview, LINE_NUM_COL)
249
def line_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 line %d" % (row+1), tree1, tree2)
265
window.set_file(tree1.id2path(self.file_id))
269
199
def _create_annotate_view(self):
270
200
tv = gtk.TreeView()
271
201
tv.set_rules_hint(False)
272
tv.connect("cursor-changed", self._activate_selected_revision)
202
tv.connect("cursor-changed", self._show_log)
274
tv.connect("row-activated", self.line_diff)
276
205
cell = gtk.CellRendererText()
277
206
cell.set_property("xalign", 1.0)
318
247
col.add_attribute(cell, "text", TEXT_LINE_COL)
319
248
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
250
tv.set_search_column(LINE_NUM_COL)
254
def _create_span_selector(self):
256
ss.connect("span-changed", self._span_changed_cb)
327
261
def _create_log_view(self):
328
lv = RevisionView(self._branch)
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)
351
def _create_find_button(self):
352
button = gtk.Button()
353
button.set_use_stock(True)
354
button.set_label("gtk-find")
355
button.set_tooltip_text("Search for text (Ctrl+F)")
356
button.connect("clicked", self._search_by_text)
357
button.set_relief(gtk.RELIEF_NONE)
359
button.set_sensitive(True)
362
def _create_goto_button(self):
363
button = gtk.Button()
364
button.set_label("Goto Line")
365
button.set_tooltip_text("Scroll to a line by entering its number (Ctrl+G)")
366
button.connect("clicked", self._search_by_line)
367
button.set_relief(gtk.RELIEF_NONE)
369
button.set_sensitive(True)
373
last_tree = self.tree
374
rev_id = self._selected_revision()
375
parent_id = self.revisions[rev_id].parent_ids[0]
376
target_tree = self.branch.repository.revision_tree(parent_id)
377
if self._go(target_tree):
378
self.history.append(last_tree)
379
self.forward_button.set_sensitive(True)
381
self._no_back.add(parent_id)
382
self.back_button.set_sensitive(False)
384
def go_forward(self):
385
if len(self.history) == 0:
387
target_tree = self.history.pop()
388
if len(self.history) == 0:
389
self.forward_button.set_sensitive(False)
390
self._go(target_tree)
392
def _go(self, target_tree):
393
rev_id = self._selected_revision()
394
if self.file_id in target_tree:
395
offset = self.get_scroll_offset(target_tree)
396
(row,), col = self.annoview.get_cursor()
397
self.annotate(target_tree, self.branch, self.file_id)
401
self.annoview.set_cursor(new_row)
406
def get_scroll_offset(self, tree):
407
old = self.tree.get_file(self.file_id)
408
new = tree.get_file(self.file_id)
409
(row,), col = self.annoview.get_cursor()
410
matcher = patiencediff.PatienceSequenceMatcher(None, old.readlines(),
412
for i, j, n in matcher.get_matching_blocks():
417
class FakeRevision(object):
267
def _create_button_box(self):
268
box = gtk.HButtonBox()
269
box.set_layout(gtk.BUTTONBOX_END)
272
button = gtk.Button()
273
button.set_use_stock(True)
274
button.set_label("gtk-close")
275
button.connect("clicked", lambda w: self.destroy())
278
box.pack_start(button, expand=False, fill=False)
418
284
""" A fake revision.
420
286
For when a revision is referenced but not present.
423
def __init__(self, revision_id, committer='?', nick=None):
289
def __init__(self, revision_id):
424
290
self.revision_id = revision_id
425
291
self.parent_ids = []
426
self.committer = committer
427
293
self.message = "?"
428
294
self.timestamp = 0.0
429
295
self.timezone = 0
432
def get_apparent_authors(self):
433
return [self.committer]
436
298
class RevisionCache(object):
437
299
"""A caching revision source"""
439
def __init__(self, real_source, seed_cache=None):
300
def __init__(self, real_source):
440
301
self.__real_source = real_source
441
if seed_cache is None:
444
self.__cache = dict(seed_cache)
446
304
def get_revision(self, revision_id):
447
305
if revision_id not in self.__cache:
448
306
revision = self.__real_source.get_revision(revision_id)
449
307
self.__cache[revision_id] = revision
450
308
return self.__cache[revision_id]
452
class SearchBox(gtk.HBox):
453
"""A button box for searching in text or lines of annotations"""
455
gtk.HBox.__init__(self, False, 6)
458
button = gtk.Button()
460
image.set_from_stock('gtk-stop', gtk.ICON_SIZE_BUTTON)
461
button.set_image(image)
462
button.set_relief(gtk.RELIEF_NONE)
463
button.connect("clicked", lambda w: self.hide_all())
464
self.pack_start(button, expand=False, fill=False)
469
self.pack_start(label, expand=False, fill=False)
473
entry.connect("activate", lambda w, d: self._do_search(d),
475
self.pack_start(entry, expand=False, fill=False)
477
# Next/previous buttons
478
button = gtk.Button('_Next')
480
image.set_from_stock('gtk-go-forward', gtk.ICON_SIZE_BUTTON)
481
button.set_image(image)
482
button.connect("clicked", lambda w, d: self._do_search(d),
484
self.pack_start(button, expand=False, fill=False)
486
button = gtk.Button('_Previous')
488
image.set_from_stock('gtk-go-back', gtk.ICON_SIZE_BUTTON)
489
button.set_image(image)
490
button.connect("clicked", lambda w, d: self._do_search(d),
492
self.pack_start(button, expand=False, fill=False)
495
check = gtk.CheckButton('Match case')
496
self._match_case = check
497
self.pack_start(check, expand=False, fill=False)
499
check = gtk.CheckButton('Regexp')
500
check.connect("toggled", lambda w: self._set_label())
502
self.pack_start(check, expand=False, fill=False)
506
# Note that we stay hidden (we do not call self.show_all())
509
def show_for(self, kind):
513
# Hide unrelated buttons
515
self._match_case.hide()
518
self._entry.grab_focus()
520
def _set_label(self):
521
if self._kind == 'line':
522
self._label.set_text('Find Line: ')
524
if self._regexp.get_active():
525
self._label.set_text('Find Regexp: ')
527
self._label.set_text('Find Text: ')
529
def set_target(self, view,column):
531
self._column = column
533
def _match(self, model, iterator, column):
534
matching_case = self._match_case.get_active()
535
string, = model.get(iterator, column)
536
key = self._entry.get_text()
537
if self._regexp.get_active():
539
match = re.compile(key).search(string, 1)
541
match = re.compile(key, re.I).search(string, 1)
543
if not matching_case:
544
string = string.lower()
546
match = string.find(key) != -1
550
def _iterate_rows_forward(self, model, start):
551
model_size = len(model)
553
while model_size != 0:
554
if current >= model_size: current = 0
555
yield model.get_iter_from_string('%d' % current)
556
if current == start: raise StopIteration
559
def _iterate_rows_backward(self, model, start):
560
model_size = len(model)
562
while model_size != 0:
563
if current < 0: current = model_size - 1
564
yield model.get_iter_from_string('%d' % current)
565
if current == start: raise StopIteration
568
def _do_search(self, direction):
569
if direction == 'forward':
570
iterate = self._iterate_rows_forward
572
iterate = self._iterate_rows_backward
574
model, sel = self._view.get_selection().get_selected()
578
path = model.get_string_from_iter(sel)
581
for row in iterate(model, start):
582
if self._match(model, row, self._column):
583
path = model.get_path(row)
584
self._view.set_cursor(path)
585
self._view.scroll_to_cell(path, use_align=True)