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
25
from bzrlib import tsort
26
from bzrlib.errors import NoSuchRevision
27
from bzrlib.revision import NULL_REVISION, CURRENT_REVISION
29
from colormap import AnnotateColorMap, AnnotateColorSaturation
30
from logview import LogView
31
from spanselector import SpanSelector
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()
59
self.span_selector.hide()
61
def annotate(self, tree, branch, file_id):
66
self.file_id = file_id
67
self.revision_id = getattr(tree, 'get_revision_id',
68
lambda: CURRENT_REVISION)()
70
# [revision id, line number, committer, revno, highlight color, line]
71
self.annomodel = gtk.ListStore(gobject.TYPE_STRING,
81
branch.repository.lock_read()
82
for line_no, (revision, revno, line)\
83
in enumerate(self._annotate(tree, file_id)):
84
if revision.revision_id == last_seen and not self.all:
85
revno = committer = ""
87
last_seen = revision.revision_id
88
committer = revision.committer
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)
103
self._set_oldest_newest()
104
# Recall that calling activate_default will emit "span-changed",
105
# so self._span_changed_cb will take care of initial highlighting
106
self.span_selector.activate_default()
108
branch.repository.unlock()
111
self.annoview.set_model(self.annomodel)
112
self.annoview.grab_focus()
114
def jump_to_line(self, lineno):
115
if lineno > len(self.annomodel) or lineno < 1:
117
# FIXME:should really deal with this in the gui. Perhaps a status
119
print("gannotate: Line number %d does't exist. Defaulting to "
125
self.annoview.set_cursor(row)
126
self.annoview.scroll_to_cell(row, use_align=True)
128
def _dotted_revnos(self, repository, revision_id):
129
"""Return a dict of revision_id -> dotted revno
131
:param repository: The repository to get the graph from
132
:param revision_id: The last revision for which this info is needed
134
graph = repository.get_revision_graph(revision_id)
136
for n, revision_id, d, revno, e in tsort.merge_sort(graph,
137
revision_id, generate_revno=True):
138
dotted[revision_id] = '.'.join(str(num) for num in revno)
141
def _annotate(self, tree, file_id):
142
current_revision = FakeRevision(CURRENT_REVISION)
143
current_revision.committer = self.branch.get_config().username()
144
current_revision.timestamp = time.time()
145
current_revision.message = '[Not yet committed]'
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)
154
for origin, text in tree.annotate_iter(file_id):
157
revision = revision_cache.get_revision(rev_id)
158
revno = dotted.get(rev_id, 'merge')
161
except NoSuchRevision:
163
if rev_id == CURRENT_REVISION:
164
revision = current_revision
165
revno = current_revno
167
revision = FakeRevision(rev_id)
170
yield revision, revno, text
172
def _set_oldest_newest(self):
173
rev_dates = map(lambda i: self.revisions[i].timestamp, self.revisions)
174
if len(rev_dates) == 0:
176
oldest = min(rev_dates)
177
newest = max(rev_dates)
179
span = self._span_from_seconds(time.time() - oldest)
180
self.span_selector.set_to_oldest_span(span)
182
span = self._span_from_seconds(newest - oldest)
183
self.span_selector.set_newest_to_oldest_span(span)
185
def _span_from_seconds(self, seconds):
186
return (seconds / (24 * 60 * 60))
188
def _span_changed_cb(self, w, span):
189
self.annotate_colormap.set_span(span)
191
self.annomodel.foreach(self._highlight_annotation, now)
193
def _highlight_annotation(self, model, path, iter, now):
194
revision_id, = model.get(iter, REVISION_ID_COL)
195
revision = self.revisions[revision_id]
196
model.set(iter, HIGHLIGHT_COLOR_COL,
197
self.annotate_colormap.get_color(revision, now))
199
def _show_log(self, w):
200
(path, col) = self.annoview.get_cursor()
203
rev_id = self.annomodel[path][REVISION_ID_COL]
204
self.logview.set_revision(self.revisions[rev_id])
207
self.logview = self._create_log_view()
208
self.annoview = self._create_annotate_view()
209
self.span_selector = self._create_span_selector()
211
vbox = gtk.VBox(False, 12)
212
vbox.set_border_width(12)
215
sw = gtk.ScrolledWindow()
216
sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
217
sw.set_shadow_type(gtk.SHADOW_IN)
218
sw.add(self.annoview)
219
self.annoview.gwindow = self
222
self.pane = pane = gtk.VPaned()
224
pane.add2(self.logview)
226
vbox.pack_start(pane, expand=True, fill=True)
228
hbox = gtk.HBox(True, 6)
229
hbox.pack_start(self.span_selector, expand=False, fill=True)
230
hbox.pack_start(self._create_button_box(), expand=False, fill=True)
232
vbox.pack_start(hbox, expand=False, fill=True)
236
def row_diff(self, tv, path, tvc):
238
revision = self.annotations[row]
239
repository = self.branch.repository
240
if revision.revision_id == CURRENT_REVISION:
242
tree2 = self.tree.basis_tree()
244
tree1 = repository.revision_tree(revision.revision_id)
245
if len(revision.parent_ids) > 0:
246
tree2 = repository.revision_tree(revision.parent_ids[0])
248
tree2 = repository.revision_tree(NULL_REVISION)
249
from bzrlib.plugins.gtk.viz.diffwin import DiffWindow
250
window = DiffWindow()
251
window.set_diff("Diff for row %d" % (row+1), tree1, tree2)
252
window.set_file(tree1.id2path(self.file_id))
256
def _create_annotate_view(self):
258
tv.set_rules_hint(False)
259
tv.connect("cursor-changed", self._show_log)
261
tv.connect("row-activated", self.row_diff)
263
cell = gtk.CellRendererText()
264
cell.set_property("xalign", 1.0)
265
cell.set_property("ypad", 0)
266
cell.set_property("family", "Monospace")
267
cell.set_property("cell-background-gdk",
268
tv.get_style().bg[gtk.STATE_NORMAL])
269
col = gtk.TreeViewColumn()
270
col.set_resizable(False)
271
col.pack_start(cell, expand=True)
272
col.add_attribute(cell, "text", LINE_NUM_COL)
273
tv.append_column(col)
275
cell = gtk.CellRendererText()
276
cell.set_property("ypad", 0)
277
cell.set_property("ellipsize", pango.ELLIPSIZE_END)
278
cell.set_property("cell-background-gdk",
279
self.get_style().bg[gtk.STATE_NORMAL])
280
col = gtk.TreeViewColumn("Committer")
281
col.set_resizable(True)
282
col.pack_start(cell, expand=True)
283
col.add_attribute(cell, "text", COMMITTER_COL)
284
tv.append_column(col)
286
cell = gtk.CellRendererText()
287
cell.set_property("xalign", 1.0)
288
cell.set_property("ypad", 0)
289
cell.set_property("cell-background-gdk",
290
self.get_style().bg[gtk.STATE_NORMAL])
291
col = gtk.TreeViewColumn("Revno")
292
col.set_resizable(False)
293
col.pack_start(cell, expand=True)
294
col.add_attribute(cell, "markup", REVNO_COL)
295
tv.append_column(col)
297
cell = gtk.CellRendererText()
298
cell.set_property("ypad", 0)
299
cell.set_property("family", "Monospace")
300
col = gtk.TreeViewColumn()
301
col.set_resizable(False)
302
col.pack_start(cell, expand=True)
303
# col.add_attribute(cell, "foreground", HIGHLIGHT_COLOR_COL)
304
col.add_attribute(cell, "background", HIGHLIGHT_COLOR_COL)
305
col.add_attribute(cell, "text", TEXT_LINE_COL)
306
tv.append_column(col)
308
tv.set_search_column(LINE_NUM_COL)
312
def _create_span_selector(self):
314
ss.connect("span-changed", self._span_changed_cb)
319
def _create_log_view(self):
325
def _create_button_box(self):
326
box = gtk.HButtonBox()
327
box.set_layout(gtk.BUTTONBOX_END)
330
button = gtk.Button()
331
button.set_use_stock(True)
332
button.set_label("gtk-close")
333
button.connect("clicked", lambda w: self.destroy())
336
box.pack_start(button, expand=False, fill=False)
344
For when a revision is referenced but not present.
347
def __init__(self, revision_id, committer='?'):
348
self.revision_id = revision_id
350
self.committer = committer
356
class RevisionCache(object):
357
"""A caching revision source"""
358
def __init__(self, real_source):
359
self.__real_source = real_source
362
def get_revision(self, revision_id):
363
if revision_id not in self.__cache:
364
revision = self.__real_source.get_revision(revision_id)
365
self.__cache[revision_id] = revision
366
return self.__cache[revision_id]