def __init__(self, all=False, plain=False, parent=None, branch=None): self.all = all self.plain = plain self._branch = branch Window.__init__(self, parent) self.set_icon(self.render_icon(gtk.STOCK_FIND, gtk.ICON_SIZE_BUTTON)) self.annotate_colormap = AnnotateColorSaturation() self._create() self.revisions = {} self.history = [] self._no_back = set()
class GAnnotateWindow(Window): """Annotate window.""" def __init__(self, all=False, plain=False, parent=None, branch=None): self.all = all self.plain = plain self._branch = branch Window.__init__(self, parent) self.set_icon(self.render_icon(gtk.STOCK_FIND, gtk.ICON_SIZE_BUTTON)) self.annotate_colormap = AnnotateColorSaturation() self._create() self.revisions = {} self.history = [] self._no_back = set() def annotate(self, tree, branch, file_id): self.annotations = [] self.branch = branch self.tree = tree self.file_id = file_id self.revisionview.set_file_id(file_id) self.revision_id = getattr(tree, 'get_revision_id', lambda: CURRENT_REVISION)() # [revision id, line number, author, revno, highlight color, line] self.annomodel = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_INT, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING) last_seen = None try: branch.lock_read() branch.repository.lock_read() self.dotted = {} revno_map = self.branch.get_revision_id_to_revno_map() for revision_id, revno in revno_map.iteritems(): self.dotted[revision_id] = '.'.join(str(num) for num in revno) for line_no, (revision, revno, line)\ in enumerate(self._annotate(tree, file_id)): if revision.revision_id == last_seen and not self.all: revno = author = "" else: last_seen = revision.revision_id author = ", ".join(revision.get_apparent_authors()) if revision.revision_id not in self.revisions: self.revisions[revision.revision_id] = revision self.annomodel.append([revision.revision_id, line_no + 1, author, revno, None, line.rstrip("\r\n") ]) self.annotations.append(revision) if not self.plain: now = time.time() self.annomodel.foreach(self._highlight_annotation, now) finally: branch.repository.unlock() branch.unlock() self.annoview.set_model(self.annomodel) self.annoview.grab_focus() my_revno = self.dotted.get(self.revision_id, 'current') title = '%s (%s) - gannotate' % (self.tree.id2path(file_id), my_revno) self.set_title(title) def jump_to_line(self, lineno): if lineno > len(self.annomodel) or lineno < 1: row = 0 # FIXME:should really deal with this in the gui. Perhaps a status # bar? print("gannotate: Line number %d does't exist. Defaulting to " "line 1." % lineno) return else: row = lineno - 1 self.annoview.set_cursor(row) self.annoview.scroll_to_cell(row, use_align=True) def _annotate(self, tree, file_id): current_revision = FakeRevision(CURRENT_REVISION) current_revision.committer = self.branch.get_config().username() current_revision.timestamp = time.time() current_revision.message = '[Not yet committed]' current_revision.parent_ids = tree.get_parent_ids() current_revision.properties['branch-nick'] = self.branch._get_nick(local=True) current_revno = '%d?' % (self.branch.revno() + 1) repository = self.branch.repository if self.revision_id == CURRENT_REVISION: revision_id = self.branch.last_revision() else: revision_id = self.revision_id revision_cache = RevisionCache(repository, self.revisions) for origin, text in tree.annotate_iter(file_id): rev_id = origin if rev_id == CURRENT_REVISION: revision = current_revision revno = current_revno else: try: revision = revision_cache.get_revision(rev_id) revno = self.dotted.get(rev_id, 'merge') if len(revno) > 15: revno = 'merge' except NoSuchRevision: revision = FakeRevision(rev_id) revno = "?" yield revision, revno, text def _highlight_annotation(self, model, path, iter, now): revision_id, = model.get(iter, REVISION_ID_COL) revision = self.revisions[revision_id] model.set(iter, HIGHLIGHT_COLOR_COL, self.annotate_colormap.get_color(revision, now)) def _selected_revision(self): (path, col) = self.annoview.get_cursor() if path is None: return None return self.annomodel[path][REVISION_ID_COL] def _activate_selected_revision(self, w): rev_id = self._selected_revision() if not rev_id or rev_id == NULL_REVISION: return selected = self.revisions[rev_id] self.revisionview.set_revision(selected) if (len(selected.parent_ids) != 0 and selected.parent_ids[0] not in self._no_back): enable_back = True else: enable_back = False self.back_button.set_sensitive(enable_back) def _create(self): self.revisionview = self._create_log_view() self.annoview = self._create_annotate_view() vbox = gtk.VBox(False) vbox.show() sw = gtk.ScrolledWindow() sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) sw.set_shadow_type(gtk.SHADOW_IN) sw.add(self.annoview) self.annoview.gwindow = self sw.show() swbox = gtk.VBox() swbox.pack_start(sw) swbox.show() hbox = gtk.HBox(False, 6) self.back_button = self._create_back_button() hbox.pack_start(self.back_button, expand=False, fill=True) self.forward_button = self._create_forward_button() hbox.pack_start(self.forward_button, expand=False, fill=True) self.find_button = self._create_find_button() hbox.pack_start(self.find_button, expand=False, fill=True) self.goto_button = self._create_goto_button() hbox.pack_start(self.goto_button, expand=False, fill=True) hbox.show() vbox.pack_start(hbox, expand=False, fill=True) self.pane = pane = gtk.VPaned() pane.add1(swbox) pane.add2(self.revisionview) pane.show() vbox.pack_start(pane, expand=True, fill=True) self._search = SearchBox() swbox.pack_start(self._search, expand=False, fill=True) accels = gtk.AccelGroup() accels.connect_group(gtk.keysyms.f, gtk.gdk.CONTROL_MASK, gtk.ACCEL_LOCKED, self._search_by_text) accels.connect_group(gtk.keysyms.g, gtk.gdk.CONTROL_MASK, gtk.ACCEL_LOCKED, self._search_by_line) self.add_accel_group(accels) self.add(vbox) def _search_by_text(self, *ignored): # (accel_group, window, key, modifiers): self._search.show_for('text') self._search.set_target(self.annoview, TEXT_LINE_COL) def _search_by_line(self, *ignored): # accel_group, window, key, modifiers): self._search.show_for('line') self._search.set_target(self.annoview, LINE_NUM_COL) def line_diff(self, tv, path, tvc): row = path[0] revision = self.annotations[row] repository = self.branch.repository if revision.revision_id == CURRENT_REVISION: tree1 = self.tree tree2 = self.tree.basis_tree() else: tree1 = repository.revision_tree(revision.revision_id) if len(revision.parent_ids) > 0: tree2 = repository.revision_tree(revision.parent_ids[0]) else: tree2 = repository.revision_tree(NULL_REVISION) from bzrlib.plugins.gtk.diff import DiffWindow window = DiffWindow(self) window.set_diff("Diff for line %d" % (row+1), tree1, tree2) window.set_file(tree1.id2path(self.file_id)) window.show() def _create_annotate_view(self): tv = gtk.TreeView() tv.set_rules_hint(False) tv.connect("cursor-changed", self._activate_selected_revision) tv.show() tv.connect("row-activated", self.line_diff) cell = gtk.CellRendererText() cell.set_property("xalign", 1.0) cell.set_property("ypad", 0) cell.set_property("family", "Monospace") cell.set_property("cell-background-gdk", tv.get_style().bg[gtk.STATE_NORMAL]) col = gtk.TreeViewColumn() col.set_resizable(False) col.pack_start(cell, expand=True) col.add_attribute(cell, "text", LINE_NUM_COL) tv.append_column(col) cell = gtk.CellRendererText() cell.set_property("ypad", 0) cell.set_property("ellipsize", pango.ELLIPSIZE_END) cell.set_property("cell-background-gdk", self.get_style().bg[gtk.STATE_NORMAL]) col = gtk.TreeViewColumn("Committer") col.set_resizable(True) col.pack_start(cell, expand=True) col.add_attribute(cell, "text", COMMITTER_COL) tv.append_column(col) cell = gtk.CellRendererText() cell.set_property("xalign", 1.0) cell.set_property("ypad", 0) cell.set_property("cell-background-gdk", self.get_style().bg[gtk.STATE_NORMAL]) col = gtk.TreeViewColumn("Revno") col.set_resizable(False) col.pack_start(cell, expand=True) col.add_attribute(cell, "markup", REVNO_COL) tv.append_column(col) cell = gtk.CellRendererText() cell.set_property("ypad", 0) cell.set_property("family", "Monospace") col = gtk.TreeViewColumn() col.set_resizable(False) col.pack_start(cell, expand=True) # col.add_attribute(cell, "foreground", HIGHLIGHT_COLOR_COL) col.add_attribute(cell, "background", HIGHLIGHT_COLOR_COL) col.add_attribute(cell, "text", TEXT_LINE_COL) tv.append_column(col) # FIXME: Now that C-f is now used for search by text we # may as well disable the auto search. tv.set_search_column(LINE_NUM_COL) return tv def _create_log_view(self): lv = RevisionView(self._branch) lv.show() return lv def _create_back_button(self): button = gtk.Button() button.set_use_stock(True) button.set_label("gtk-go-back") button.connect("clicked", lambda w: self.go_back()) button.set_relief(gtk.RELIEF_NONE) button.show() return button def _create_forward_button(self): button = gtk.Button() button.set_use_stock(True) button.set_label("gtk-go-forward") button.connect("clicked", lambda w: self.go_forward()) button.set_relief(gtk.RELIEF_NONE) button.show() button.set_sensitive(False) return button def _create_find_button(self): button = gtk.Button() button.set_use_stock(True) button.set_label("gtk-find") button.set_tooltip_text("Search for text (Ctrl+F)") button.connect("clicked", self._search_by_text) button.set_relief(gtk.RELIEF_NONE) button.show() button.set_sensitive(True) return button def _create_goto_button(self): button = gtk.Button() button.set_label("Goto Line") button.set_tooltip_text("Scroll to a line by entering its number (Ctrl+G)") button.connect("clicked", self._search_by_line) button.set_relief(gtk.RELIEF_NONE) button.show() button.set_sensitive(True) return button def go_back(self): last_tree = self.tree rev_id = self._selected_revision() parent_id = self.revisions[rev_id].parent_ids[0] target_tree = self.branch.repository.revision_tree(parent_id) if self._go(target_tree): self.history.append(last_tree) self.forward_button.set_sensitive(True) else: self._no_back.add(parent_id) self.back_button.set_sensitive(False) def go_forward(self): if len(self.history) == 0: return target_tree = self.history.pop() if len(self.history) == 0: self.forward_button.set_sensitive(False) self._go(target_tree) def _go(self, target_tree): rev_id = self._selected_revision() if self.file_id in target_tree: offset = self.get_scroll_offset(target_tree) (row,), col = self.annoview.get_cursor() self.annotate(target_tree, self.branch, self.file_id) new_row = row+offset if new_row < 0: new_row = 0 self.annoview.set_cursor(new_row) return True else: return False def get_scroll_offset(self, tree): old = self.tree.get_file(self.file_id) new = tree.get_file(self.file_id) (row,), col = self.annoview.get_cursor() matcher = patiencediff.PatienceSequenceMatcher(None, old.readlines(), new.readlines()) for i, j, n in matcher.get_matching_blocks(): if i + n >= row: return j - i