| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
from PyQt5 import QtCore, QtGui |
| 21 |
from time import (strftime, localtime) |
| 22 |
import time |
| 23 |
import re |
| 24 |
import fnmatch |
| 25 |
|
| 26 |
from breezy.revision import Revision |
| 27 |
|
| 28 |
from breezy.plugins.qbrz.lib.bugs import get_bug_id |
| 29 |
from breezy.plugins.qbrz.lib import loggraphviz |
| 30 |
from breezy.plugins.qbrz.lib.lazycachedrevloader import (load_revisions, |
| 31 |
cached_revisions) |
| 32 |
from breezy.plugins.qbrz.lib.revtreeview import RevIdRole as im_RevIdRole |
| 33 |
from breezy.plugins.qbrz.lib.i18n import gettext |
| 34 |
from breezy.plugins.qbrz.lib.util import ( |
| 35 |
extract_name, |
| 36 |
get_apparent_author, |
| 37 |
runs_in_loading_queue, |
| 38 |
) |
| 39 |
|
| 40 |
RevIdRole = im_RevIdRole |
| 41 |
GraphDataRole = QtCore.Qt.UserRole + 2 |
| 42 |
|
| 43 |
header_labels = (gettext("Rev"), |
| 44 |
gettext("Message"), |
| 45 |
gettext("Date"), |
| 46 |
gettext("Author"), ) |
| 47 |
|
| 48 |
COL_REV = 0 |
| 49 |
COL_MESSAGE = 1 |
| 50 |
COL_DATE = 2 |
| 51 |
COL_AUTHOR = 3 |
| 52 |
|
| 53 |
|
| 54 |
class WorkingTreeRevision(Revision): |
| 55 |
def __init__(self, revid, tree): |
| 56 |
super(WorkingTreeRevision, self).__init__(revid) |
| 57 |
|
| 58 |
self.parent_ids = tree.get_parent_ids() |
| 59 |
self.committer = tree.branch.get_config().username() |
| 60 |
self.message = "" |
| 61 |
self.timestamp = None |
| 62 |
self.tree = tree |
| 63 |
|
| 64 |
|
| 65 |
class GraphVizLoader(loggraphviz.GraphVizLoader): |
| 66 |
|
| 67 |
def __init__(self, branches, primary_bi, no_graph, processEvents, throbber): |
| 68 |
self.processEvents = processEvents |
| 69 |
self.throbber = throbber |
| 70 |
loggraphviz.GraphVizLoader.__init__(self, branches, primary_bi, no_graph) |
| 71 |
|
| 72 |
def update_ui(self): |
| 73 |
self.processEvents() |
| 74 |
|
| 75 |
def throbber_show(self): |
| 76 |
self.throbber.show() |
| 77 |
|
| 78 |
def throbber_hide(self): |
| 79 |
self.throbber.hide() |
| 80 |
|
| 81 |
def revisions_filter_changed(self): |
| 82 |
self.on_filter_changed() |
| 83 |
|
| 84 |
def load_revisions(self, revids): |
| 85 |
return load_revisions(revids, self.get_repo_revids) |
| 86 |
|
| 87 |
|
| 88 |
class PendingMergesGraphVizLoader( |
| 89 |
loggraphviz.PendingMergesGraphVizLoader, |
| 90 |
GraphVizLoader): |
| 91 |
pass |
| 92 |
|
| 93 |
|
| 94 |
class WithWorkingTreeGraphVizLoader(loggraphviz.WithWorkingTreeGraphVizLoader, GraphVizLoader): |
| 95 |
|
| 96 |
def load(self): |
| 97 |
super(WithWorkingTreeGraphVizLoader, self).load() |
| 98 |
for wt_revid, tree in self.working_trees.items(): |
| 99 |
|
| 100 |
cached_revisions[wt_revid] = WorkingTreeRevision(wt_revid, tree) |
| 101 |
|
| 102 |
|
| 103 |
class FileIdFilter(loggraphviz.FileIdFilter): |
| 104 |
@runs_in_loading_queue |
| 105 |
def load(self, revids=None): |
| 106 |
super(FileIdFilter, self).load(revids) |
| 107 |
|
| 108 |
|
| 109 |
class WorkingTreeHasChangeFilter(loggraphviz.WorkingTreeHasChangeFilter): |
| 110 |
@runs_in_loading_queue |
| 111 |
def load(self): |
| 112 |
super(WorkingTreeHasChangeFilter, self).load() |
| 113 |
|
| 114 |
|
| 115 |
class FilterScheduler(object): |
| 116 |
def __init__(self, filter_changed_callback): |
| 117 |
self.pending_revs = [] |
| 118 |
self.last_run_time = 0 |
| 119 |
self.last_call_time = 0 |
| 120 |
self.filter_changed_callback = filter_changed_callback |
| 121 |
|
| 122 |
def filter_changed(self, revs, last_call=True): |
| 123 |
if revs is None: |
| 124 |
self.pending_revs = None |
| 125 |
else: |
| 126 |
self.pending_revs.extend(revs) |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
if last_call or revs is None or time.process_time() - self.last_call_time > self.last_run_time * 10: |
| 134 |
start_time = time.process_time() |
| 135 |
self.filter_changed_callback(self.pending_revs, last_call) |
| 136 |
self.pending_revs = [] |
| 137 |
self.last_run_time = time.process_time() - start_time |
| 138 |
self.last_call_time = time.process_time() |
| 139 |
|
| 140 |
if last_call: |
| 141 |
self.last_run_time = 0 |
| 142 |
self.last_call_time = 0 |
| 143 |
|
| 144 |
|
| 145 |
class LogModel(QtCore.QAbstractTableModel): |
| 146 |
|
| 147 |
layoutAboutToBeChanged = QtCore.pyqtSignal() |
| 148 |
layoutChanged = QtCore.pyqtSignal() |
| 149 |
dataChanged = QtCore.pyqtSignal(QtCore.QModelIndex, QtCore.QModelIndex) |
| 150 |
|
| 151 |
def __init__(self, processEvents, throbber, parent=None): |
| 152 |
QtCore.QAbstractTableModel.__init__(self, parent) |
| 153 |
self.processEvents = processEvents |
| 154 |
self.throbber = throbber |
| 155 |
|
| 156 |
self.graph_viz = GraphVizLoader((), None, False, processEvents, throbber) |
| 157 |
self.state = loggraphviz.GraphVizFilterState( |
| 158 |
self.graph_viz, self.compute_lines) |
| 159 |
self.computed = loggraphviz.ComputedGraphViz(self.graph_viz) |
| 160 |
|
| 161 |
self.clicked_f_index = None |
| 162 |
self.last_rev_is_placeholder = False |
| 163 |
self.bugtext = gettext("bug #%s") |
| 164 |
|
| 165 |
def load(self, branches, primary_bi, file_ids, no_graph, graph_provider_type): |
| 166 |
self.throbber.show() |
| 167 |
self.processEvents() |
| 168 |
try: |
| 169 |
graph_viz = graph_provider_type(branches, primary_bi, no_graph, processEvents=self.processEvents, throbber=self.throbber) |
| 170 |
graph_viz.load() |
| 171 |
graph_viz.on_filter_changed = self.on_filter_changed |
| 172 |
|
| 173 |
state = loggraphviz.GraphVizFilterState(graph_viz, self.compute_lines) |
| 174 |
|
| 175 |
for (branch_id, value) in self.state.branch_line_state.items(): |
| 176 |
if branch_id in graph_viz.branch_lines: |
| 177 |
state.branch_line_state[branch_id] = value |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
scheduler = FilterScheduler(state.filter_changed) |
| 183 |
if file_ids: |
| 184 |
file_id_filter = FileIdFilter(graph_viz, scheduler.filter_changed, file_ids) |
| 185 |
state.filters.append(file_id_filter) |
| 186 |
else: |
| 187 |
file_id_filter = None |
| 188 |
|
| 189 |
if isinstance(graph_viz, WithWorkingTreeGraphVizLoader): |
| 190 |
working_tree_filter = WorkingTreeHasChangeFilter(graph_viz, scheduler.filter_changed, file_ids) |
| 191 |
state.filters.append(working_tree_filter) |
| 192 |
else: |
| 193 |
working_tree_filter = None |
| 194 |
|
| 195 |
prop_search_filter = PropertySearchFilter(graph_viz,scheduler.filter_changed) |
| 196 |
state.filters.append(prop_search_filter) |
| 197 |
|
| 198 |
self.layoutAboutToBeChanged.emit() |
| 199 |
self.graph_viz = graph_viz |
| 200 |
self.state = state |
| 201 |
self.file_ids = file_ids |
| 202 |
self.file_id_filter = file_id_filter |
| 203 |
self.working_tree_filter = working_tree_filter |
| 204 |
self.prop_search_filter = prop_search_filter |
| 205 |
self.computed = loggraphviz.ComputedGraphViz(graph_viz) |
| 206 |
self.layoutChanged.emit() |
| 207 |
|
| 208 |
self.compute_lines() |
| 209 |
|
| 210 |
if self.working_tree_filter: |
| 211 |
QtCore.QTimer.singleShot(1, self.working_tree_filter.load) |
| 212 |
if self.file_id_filter: |
| 213 |
QtCore.QTimer.singleShot(1, self.file_id_filter.load) |
| 214 |
finally: |
| 215 |
self.throbber.hide() |
| 216 |
|
| 217 |
def compute_lines(self): |
| 218 |
computed = self.graph_viz.compute_viz(self.state) |
| 219 |
if self.last_rev_is_placeholder: |
| 220 |
computed.filtered_revs[-1].col_index = None |
| 221 |
self.layoutAboutToBeChanged.emit() |
| 222 |
self.computed = computed |
| 223 |
self.layoutChanged.emit() |
| 224 |
|
| 225 |
def collapse_expand_rev(self, c_rev): |
| 226 |
self.clicked_f_index = c_rev.f_index |
| 227 |
clicked_row_index = self.createIndex(c_rev.f_index, COL_MESSAGE, QtCore.QModelIndex()) |
| 228 |
self.dataChanged.emit(clicked_row_index, clicked_row_index) |
| 229 |
self.graph_viz.update_ui() |
| 230 |
self.clicked_f_index = None |
| 231 |
self.state.collapse_expand_rev(c_rev) |
| 232 |
self.dataChanged.emit(clicked_row_index, clicked_row_index) |
| 233 |
|
| 234 |
def ensure_rev_visible(self, rev): |
| 235 |
self.state.ensure_rev_visible(rev) |
| 236 |
|
| 237 |
def columnCount(self, parent): |
| 238 |
if parent.isValid(): |
| 239 |
return 0 |
| 240 |
return len(header_labels) |
| 241 |
|
| 242 |
def rowCount(self, parent): |
| 243 |
if parent.isValid(): |
| 244 |
return 0 |
| 245 |
return len(self.computed.filtered_revs) |
| 246 |
|
| 247 |
def data(self, index, role): |
| 248 |
|
| 249 |
if not index.isValid(): |
| 250 |
return None |
| 251 |
|
| 252 |
def blank(): |
| 253 |
if role == QtCore.Qt.DisplayRole: |
| 254 |
return "" |
| 255 |
return None |
| 256 |
|
| 257 |
c_rev = self.computed.filtered_revs[index.row()] |
| 258 |
if c_rev is None: |
| 259 |
return blank() |
| 260 |
|
| 261 |
if c_rev.rev.revid in cached_revisions: |
| 262 |
revision = cached_revisions[c_rev.rev.revid] |
| 263 |
else: |
| 264 |
revision = None |
| 265 |
|
| 266 |
if role == GraphDataRole: |
| 267 |
prev_c_rev = None |
| 268 |
prev_c_rev_f_index = c_rev.f_index - 1 |
| 269 |
if prev_c_rev_f_index >= 0: |
| 270 |
prev_c_rev = self.computed.filtered_revs[prev_c_rev_f_index] |
| 271 |
|
| 272 |
tags = [] |
| 273 |
|
| 274 |
tags.extend([(label, QtGui.QColor(24, 80, 200), QtGui.QColor(QtCore.Qt.white)) |
| 275 |
for (branch_info, label) in c_rev.branch_labels if label]) |
| 276 |
|
| 277 |
if c_rev.rev.revid in self.graph_viz.tags: |
| 278 |
tags.extend([(tag, QtGui.QColor(80, 128, 32), QtGui.QColor(QtCore.Qt.white)) |
| 279 |
for tag in self.graph_viz.tags[c_rev.rev.revid]]) |
| 280 |
|
| 281 |
|
| 282 |
if revision: |
| 283 |
if hasattr(revision, '_qlog_bugs'): |
| 284 |
bugs = revision._qlog_bugs |
| 285 |
else: |
| 286 |
bugs = [] |
| 287 |
for bug in revision.properties.get('bugs', '').split('\n'): |
| 288 |
if bug: |
| 289 |
url = bug.split(' ', 1)[0] |
| 290 |
bug_id = get_bug_id(url) |
| 291 |
if bug_id: |
| 292 |
bugs.append(self.bugtext % bug_id) |
| 293 |
revision._qlog_bugs = bugs |
| 294 |
tags.extend([ (bug, QtGui.QColor(164, 0, 0), QtGui.QColor(QtCore.Qt.white)) for bug in bugs]) |
| 295 |
is_clicked = c_rev.f_index == self.clicked_f_index |
| 296 |
return c_rev, prev_c_rev, tags, is_clicked |
| 297 |
|
| 298 |
if role == QtCore.Qt.DisplayRole and index.column() == COL_REV: |
| 299 |
return c_rev.rev.revno_str |
| 300 |
|
| 301 |
if role == QtCore.Qt.ToolTipRole and index.column() == COL_MESSAGE: |
| 302 |
urls = [branch_info.branch.base for (branch_info, label) in c_rev.branch_labels if label] |
| 303 |
return '\n'.join(urls) |
| 304 |
|
| 305 |
if role == RevIdRole: |
| 306 |
return c_rev.rev.revid |
| 307 |
|
| 308 |
|
| 309 |
if revision is None: |
| 310 |
return blank() |
| 311 |
|
| 312 |
if role == QtCore.Qt.DisplayRole and index.column() == COL_DATE: |
| 313 |
return strftime("%Y-%m-%d %H:%M", localtime(revision.timestamp)) |
| 314 |
if role == QtCore.Qt.DisplayRole and index.column() == COL_AUTHOR: |
| 315 |
return extract_name(get_apparent_author(revision)) |
| 316 |
if role == QtCore.Qt.DisplayRole and index.column() == COL_MESSAGE: |
| 317 |
return revision.get_summary() |
| 318 |
|
| 319 |
return blank() |
| 320 |
|
| 321 |
def flags(self, index): |
| 322 |
if not index.isValid(): |
| 323 |
return QtCore.Qt.ItemIsEnabled |
| 324 |
|
| 325 |
return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable |
| 326 |
|
| 327 |
def headerData(self, section, orientation, role): |
| 328 |
if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole: |
| 329 |
return header_labels[section] |
| 330 |
return None |
| 331 |
|
| 332 |
def on_revisions_loaded(self, revisions, last_call): |
| 333 |
for revid in revisions.keys(): |
| 334 |
rev = self.graph_viz.revid_rev[revid] |
| 335 |
self.dataChanged.emit(self.index(rev.index, COL_MESSAGE, QtCore.QModelIndex()), |
| 336 |
self.index(rev.index, COL_AUTHOR, QtCore.QModelIndex())) |
| 337 |
|
| 338 |
def on_filter_changed(self): |
| 339 |
self.compute_lines() |
| 340 |
|
| 341 |
def get_repo(self): |
| 342 |
return self.graph_viz.get_repo_revids |
| 343 |
|
| 344 |
def index_from_revid(self, revid, column=0): |
| 345 |
try: |
| 346 |
rev = self.graph_viz.revid_rev[revid] |
| 347 |
except KeyError: |
| 348 |
return |
| 349 |
return self.index_from_rev(rev) |
| 350 |
|
| 351 |
def index_from_rev(self, rev, column=0): |
| 352 |
try: |
| 353 |
c_rev = self.computed.revisions[rev.index] |
| 354 |
except IndexError: |
| 355 |
return |
| 356 |
if c_rev is None: |
| 357 |
return |
| 358 |
return self.index_from_c_rev(c_rev, column) |
| 359 |
|
| 360 |
def index_from_c_rev(self, c_rev, column=0): |
| 361 |
return self.index(c_rev.f_index, column, QtCore.QModelIndex()) |
| 362 |
|
| 363 |
def c_rev_from_index(self, index): |
| 364 |
f_index = index.row() |
| 365 |
try: |
| 366 |
return self.computed.filtered_revs[f_index] |
| 367 |
except IndexError: |
| 368 |
return None |
| 369 |
|
| 370 |
|
| 371 |
class PropertySearchFilter (object): |
| 372 |
def __init__(self, graph_viz, filter_changed_callback): |
| 373 |
self.graph_viz = graph_viz |
| 374 |
self.filter_changed_callback = filter_changed_callback |
| 375 |
self.field = None |
| 376 |
self.filter_re = None |
| 377 |
self.cache = None |
| 378 |
self.index_matched_revids = None |
| 379 |
self.loading_revisions = False |
| 380 |
|
| 381 |
def set_search(self, s, field): |
| 382 |
"""Set search string for specified kind of data. |
| 383 |
@param s: string to search (interpreted based on field value) |
| 384 |
@param field: kind of data to search, based on some field |
| 385 |
of revision metadata. Possible values: |
| 386 |
- message |
| 387 |
- index (require bzr-search plugin) |
| 388 |
- author |
| 389 |
- tag |
| 390 |
- bug |
| 391 |
|
| 392 |
Value of `str` interpreted based on field value. For index it's used |
| 393 |
as input value for bzr-search engine. |
| 394 |
For message, author, tag and bug it's used as shell pattern |
| 395 |
(glob pattern) to search in corresponding metadata of revisions. |
| 396 |
""" |
| 397 |
self.field = field |
| 398 |
|
| 399 |
def revisions_loaded(revisions, last_call): |
| 400 |
revs = [self.graph_viz.revid_rev[revid] for revid in revisions.keys()] |
| 401 |
self.filter_changed_callback(revs, last_call) |
| 402 |
|
| 403 |
def before_batch_load(repo, revids): |
| 404 |
if self.filter_re is None: |
| 405 |
return True |
| 406 |
return False |
| 407 |
|
| 408 |
def wildcard2regex(wildcard): |
| 409 |
"""Translate shel pattern to regexp.""" |
| 410 |
return fnmatch.translate(wildcard + '*') |
| 411 |
|
| 412 |
if s is None or s == "": |
| 413 |
self.filter_re = None |
| 414 |
self.index_matched_revids = None |
| 415 |
self.filter_changed_callback(None, True) |
| 416 |
else: |
| 417 |
if self.field == "index": |
| 418 |
from breezy.plugins.search import index as search_index |
| 419 |
self.filter_re = None |
| 420 |
indexes = [bi.index for bi in self.graph_viz.branches if bi.index is not None] |
| 421 |
if not indexes: |
| 422 |
self.index_matched_revids = None |
| 423 |
else: |
| 424 |
s = s.strip() |
| 425 |
query = [(query_item,) for query_item in s.split(" ")] |
| 426 |
self.index_matched_revids = {} |
| 427 |
for index in indexes: |
| 428 |
for result in index.search(query): |
| 429 |
if isinstance(result, search_index.RevisionHit): |
| 430 |
self.index_matched_revids[result.revision_key[0]] = True |
| 431 |
if isinstance(result, search_index.FileTextHit): |
| 432 |
self.index_matched_revids[result.text_key[1]] = True |
| 433 |
if isinstance(result, search_index.PathHit): |
| 434 |
pass |
| 435 |
elif self.field == "tag": |
| 436 |
self.filter_re = None |
| 437 |
filter_re = re.compile(wildcard2regex(s), re.IGNORECASE) |
| 438 |
self.index_matched_revids = {} |
| 439 |
for revid in self.graph_viz.tags: |
| 440 |
for t in self.graph_viz.tags[revid]: |
| 441 |
if filter_re.search(t): |
| 442 |
self.index_matched_revids[revid] = True |
| 443 |
break |
| 444 |
else: |
| 445 |
self.filter_re = re.compile(wildcard2regex(s), re.IGNORECASE) |
| 446 |
self.index_matched_revids = None |
| 447 |
|
| 448 |
self.filter_changed_callback(None, True) |
| 449 |
|
| 450 |
if self.filter_re is not None and not self.loading_revisions: |
| 451 |
self.loading_revisions = True |
| 452 |
try: |
| 453 |
revids = [rev.revid for rev in self.graph_viz.revisions ] |
| 454 |
load_revisions(revids, self.graph_viz.get_repo_revids, |
| 455 |
time_before_first_ui_update = 0, |
| 456 |
local_batch_size = 100, remote_batch_size = 10, |
| 457 |
before_batch_load = before_batch_load, |
| 458 |
revisions_loaded = revisions_loaded) |
| 459 |
finally: |
| 460 |
self.loading_revisions = False |
| 461 |
|
| 462 |
def get_revision_visible(self, rev): |
| 463 |
|
| 464 |
if self.filter_re: |
| 465 |
revid = rev.revid |
| 466 |
if revid not in cached_revisions: |
| 467 |
return False |
| 468 |
revision = cached_revisions[revid] |
| 469 |
|
| 470 |
filtered_str = None |
| 471 |
if self.field == "message": |
| 472 |
filtered_str = revision.message |
| 473 |
elif self.field == "author": |
| 474 |
filtered_str = get_apparent_author(revision) |
| 475 |
elif self.field == "bug": |
| 476 |
rbugs = revision.properties.get('bugs', '') |
| 477 |
if rbugs: |
| 478 |
filtered_str = rbugs.replace('\n', ' ') |
| 479 |
else: |
| 480 |
return False |
| 481 |
|
| 482 |
if filtered_str is not None: |
| 483 |
if self.filter_re.search(filtered_str) is None: |
| 484 |
return False |
| 485 |
|
| 486 |
if self.index_matched_revids is not None: |
| 487 |
revid = rev.revid |
| 488 |
if revid not in self.index_matched_revids: |
| 489 |
return False |
| 490 |
|
| 491 |
return True |