| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
import re |
| 21 |
|
| 22 |
from PyQt5 import QtCore, QtGui, QtWidgets |
| 23 |
|
| 24 |
from breezy.revision import CURRENT_REVISION |
| 25 |
from breezy import ( |
| 26 |
config, |
| 27 |
errors, |
| 28 |
lazy_regex, |
| 29 |
log, |
| 30 |
gpg, |
| 31 |
) |
| 32 |
|
| 33 |
from breezy.plugins.qbrz.lib.i18n import gettext, ngettext |
| 34 |
|
| 35 |
from breezy.plugins.qbrz.lib.lazycachedrevloader import ( |
| 36 |
load_revisions, cached_revisions) |
| 37 |
from breezy.plugins.qbrz.lib.util import ( |
| 38 |
runs_in_loading_queue, |
| 39 |
format_timestamp, |
| 40 |
get_message, |
| 41 |
get_summary, |
| 42 |
open_browser, |
| 43 |
) |
| 44 |
|
| 45 |
from breezy import foreign |
| 46 |
from breezy.plugins.qbrz.lib.uifactory import ui_current_widget |
| 47 |
from breezy.plugins.qbrz.lib import logmodel |
| 48 |
|
| 49 |
_email_re = lazy_regex.lazy_compile(r'([a-z0-9_\-.+]+@[a-z0-9_\-.+]+)', re.IGNORECASE) |
| 50 |
_link1_re = lazy_regex.lazy_compile(r'([\s>])(https?)://([^\s<>{}()]+[^\s.,<>{}()])', re.IGNORECASE) |
| 51 |
_link2_re = lazy_regex.lazy_compile(r'(\s)www\.([a-z0-9\-]+)\.([a-z0-9\-.\~]+)((?:/[^ <>{}()\n\r]*[^., <>{}()\n\r]?)?)', re.IGNORECASE) |
| 52 |
_tag_re = lazy_regex.lazy_compile(r'[, ]') |
| 53 |
_start_of_line_whitespace_re = lazy_regex.lazy_compile(r'(?m)^ +') |
| 54 |
|
| 55 |
|
| 56 |
def _dummy_gpg_verify(): |
| 57 |
return False |
| 58 |
|
| 59 |
gpg_verify_available_func = getattr(gpg.GPGStrategy, "verify_signatures_available", _dummy_gpg_verify) |
| 60 |
|
| 61 |
|
| 62 |
def htmlencode(s): |
| 63 |
"""Convert single line to html snippet suitable to show in Qt widgets.""" |
| 64 |
return (s.replace("&", "&") |
| 65 |
.replace("<", "<") |
| 66 |
.replace(">", ">") |
| 67 |
.replace("\"", """) |
| 68 |
) |
| 69 |
|
| 70 |
def htmlize(text): |
| 71 |
"""Convert multiline text (of commit messages) to valid html snippet |
| 72 |
suitable to show in revision message browser widget. |
| 73 |
""" |
| 74 |
text = htmlencode(text) |
| 75 |
text = _start_of_line_whitespace_re.sub(lambda m: " " * len(m.group()), text) |
| 76 |
text = text.replace("\n", '<br />') |
| 77 |
text = _email_re.sub('<a href="mailto:\\1">\\1</a>', text) |
| 78 |
text = _link1_re.sub('\\1<a href="\\2://\\3">\\2://\\3</a>', text) |
| 79 |
text = _link2_re.sub('\\1<a href="http://www.\\2.\\3\\4">www.\\2.\\3\\4</a>', text) |
| 80 |
return text |
| 81 |
|
| 82 |
def quote_tag(tag): |
| 83 |
if _tag_re.search(tag): |
| 84 |
return '"%s"' % tag |
| 85 |
return tag |
| 86 |
|
| 87 |
|
| 88 |
def format_signature_validity(rev_id, repository, gpg_strategy): |
| 89 |
"""get the signature validity |
| 90 |
|
| 91 |
:param rev_id: revision id to validate |
| 92 |
:param repository: branch of revision |
| 93 |
:param gpg_strategy: GPG strategy |
| 94 |
:return: human readable string to print to log |
| 95 |
""" |
| 96 |
from breezy import gpg |
| 97 |
|
| 98 |
result = repository.verify_revision_signature(rev_id, gpg_strategy) |
| 99 |
if result[0] == gpg.SIGNATURE_VALID: |
| 100 |
return "valid signature from {0}".format(result[1]) |
| 101 |
if result[0] == gpg.SIGNATURE_KEY_MISSING: |
| 102 |
return "unknown key {0}".format(result[1]) |
| 103 |
if result[0] == gpg.SIGNATURE_NOT_VALID: |
| 104 |
return "invalid signature!" |
| 105 |
if result[0] == gpg.SIGNATURE_NOT_SIGNED: |
| 106 |
return "no signature" |
| 107 |
|
| 108 |
|
| 109 |
class RevisionMessageBrowser(QtWidgets.QTextBrowser): |
| 110 |
"""Widget to display revision metadata and messages.""" |
| 111 |
|
| 112 |
def __init__(self, parent=None): |
| 113 |
super(RevisionMessageBrowser, self).__init__(parent) |
| 114 |
|
| 115 |
|
| 116 |
conf = config.GlobalStack() |
| 117 |
self._gpg_strategy = gpg.GPGStrategy(conf) |
| 118 |
|
| 119 |
boxsize = self.fontMetrics().ascent() |
| 120 |
center = boxsize * 0.5 |
| 121 |
dotsize = 0.7 |
| 122 |
dot_rect = QtCore.QRectF(center - (boxsize * dotsize * 0.5 ), |
| 123 |
center - (boxsize * dotsize * 0.5 ), |
| 124 |
boxsize * dotsize, |
| 125 |
boxsize * dotsize) |
| 126 |
self.imagesize = boxsize |
| 127 |
self.images = [] |
| 128 |
for color in range(7): |
| 129 |
image = QtGui.QImage(boxsize, boxsize, QtGui.QImage.Format_ARGB32) |
| 130 |
image.fill(0) |
| 131 |
painter = QtGui.QPainter(image) |
| 132 |
painter.setRenderHint(QtGui.QPainter.Antialiasing) |
| 133 |
pen = QtGui.QPen() |
| 134 |
pen.setWidth(1) |
| 135 |
pen.setColor(self.get_act_color(color,False)) |
| 136 |
painter.setPen(pen) |
| 137 |
painter.setBrush(QtGui.QBrush(self.get_act_color(color,True))) |
| 138 |
painter.drawEllipse(dot_rect) |
| 139 |
painter.end() |
| 140 |
self.document().addResource(QtGui.QTextDocument.ImageResource, QtCore.QUrl("dot%d" % color), image) |
| 141 |
self.images.append(image) |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
self.props_back_color_str = ("#%02X%02X%02X" % self.palette().window().color().getRgb()[:3]) |
| 146 |
|
| 147 |
def get_act_color(self, color, back): |
| 148 |
qcolor = QtGui.QColor() |
| 149 |
if color == 0: |
| 150 |
if back: |
| 151 |
qcolor.setHsvF(0,0,0.8) |
| 152 |
else: |
| 153 |
qcolor.setHsvF(0,0,0) |
| 154 |
else: |
| 155 |
h = float(color % 6) / 6 |
| 156 |
if back: |
| 157 |
qcolor.setHsvF(h,0.4,1) |
| 158 |
else: |
| 159 |
qcolor.setHsvF(h,1,0.7) |
| 160 |
|
| 161 |
return qcolor |
| 162 |
|
| 163 |
def set_display_revids(self, revids, repo): |
| 164 |
self._display_revids = revids |
| 165 |
self._all_loaded_revs = {} |
| 166 |
|
| 167 |
revids_to_load = set(revids) |
| 168 |
for revid in revids: |
| 169 |
revids_to_load.update(set(self.get_parents(revid))) |
| 170 |
revids_to_load.update(set(self.get_children(revid))) |
| 171 |
|
| 172 |
load_revisions(list(revids_to_load), repo, revisions_loaded=self.revisions_loaded, pass_prev_loaded_rev=True) |
| 173 |
|
| 174 |
def revisions_loaded(self, revs_loaded, last_call): |
| 175 |
self._all_loaded_revs.update(revs_loaded) |
| 176 |
|
| 177 |
rev_html = [] |
| 178 |
min_merge_depth = min([self.get_merge_depth(revid) for revid in self._display_revids]) |
| 179 |
for revid in self._display_revids: |
| 180 |
props = [] |
| 181 |
message = "" |
| 182 |
props.append((gettext("Revision:"), "%s revid:%s" % (self.get_revno(revid), revid.decode('ascii')))) |
| 183 |
|
| 184 |
parents = self.get_parents(revid) |
| 185 |
children = self.get_children(revid) |
| 186 |
|
| 187 |
def short_text(summary, length): |
| 188 |
if len(summary) > length: |
| 189 |
return summary[:length - 1] + "\u2026" |
| 190 |
else: |
| 191 |
return summary |
| 192 |
|
| 193 |
def revision_list_html(revids): |
| 194 |
revs = [] |
| 195 |
for revid in revids: |
| 196 |
revno = self.get_revno(revid) |
| 197 |
color = self.get_color(revid) |
| 198 |
if color is not None: |
| 199 |
color = '<img src="dot%d" width="%d" height="%d">' % (color % 6, self.imagesize, self.imagesize) |
| 200 |
else: |
| 201 |
color = "" |
| 202 |
if revid in self._all_loaded_revs: |
| 203 |
summary = get_summary(self._all_loaded_revs[revid]) |
| 204 |
revs.append('<a href="qlog-revid:%s" title="%s">%s%s: %s</a>' % |
| 205 |
(revid.decode('ascii'), htmlencode(summary), color, revno, |
| 206 |
htmlencode((short_text(summary, 60))))) |
| 207 |
else: |
| 208 |
revs.append('<a href="qlog-revid:%s">%s%srevid: %s</a>' % (revid.decode('ascii'), color, revno, revid)) |
| 209 |
return '<br>'.join(revs) |
| 210 |
|
| 211 |
if parents: |
| 212 |
props.append((gettext("Parents:"), revision_list_html(parents))) |
| 213 |
if children: |
| 214 |
props.append((gettext("Children:"), revision_list_html(children))) |
| 215 |
|
| 216 |
if gpg_verify_available_func(): |
| 217 |
try: |
| 218 |
signature_result_text = format_signature_validity(revid, cached_revisions[revid].repository, self._gpg_strategy) |
| 219 |
props.append((gettext("Signature:"), signature_result_text)) |
| 220 |
except KeyError: |
| 221 |
|
| 222 |
pass |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
if not revid == CURRENT_REVISION: |
| 229 |
if revid in self._all_loaded_revs: |
| 230 |
rev = self._all_loaded_revs[revid] |
| 231 |
props.extend(self.loaded_revision_props(rev)) |
| 232 |
message = htmlize(get_message(rev)) |
| 233 |
|
| 234 |
search_replace = self.get_search_replace(revid) |
| 235 |
if search_replace: |
| 236 |
for search, replace in search_replace: |
| 237 |
message = re.sub(search, replace, message) |
| 238 |
else: |
| 239 |
message = gettext("Uncommited Working Tree Changes") |
| 240 |
|
| 241 |
margin_left = (self.get_merge_depth(revid)-min_merge_depth)*20 |
| 242 |
text = [] |
| 243 |
text.append('<table style="background:%s; margin-left:%dpx;">' % (self.props_back_color_str, margin_left)) |
| 244 |
for prop in props: |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
text.append(('<tr>' |
| 249 |
'<td style="padding-left:2px; ' |
| 250 |
'font-weight:bold; ' |
| 251 |
'white-space: pre;"' |
| 252 |
'align="right">%s</td>' |
| 253 |
'<td width="100%%">%s</td>' |
| 254 |
'</tr>') % prop) |
| 255 |
text.append('</table>') |
| 256 |
|
| 257 |
text.append('<div style="margin-top:0.5em; margin-left:%spx;">%s</div>' % (margin_left + 2 , message)) |
| 258 |
rev_html.append("".join(text)) |
| 259 |
|
| 260 |
self.setHtml("<br>".join(rev_html)) |
| 261 |
|
| 262 |
|
| 263 |
for color, image in enumerate(self.images): |
| 264 |
self.document().addResource(QtGui.QTextDocument.ImageResource, QtCore.QUrl("dot%d" % color), image) |
| 265 |
|
| 266 |
def loaded_revision_props(self, rev): |
| 267 |
props = [] |
| 268 |
if rev.timestamp is not None: |
| 269 |
props.append((gettext("Date:"), format_timestamp(rev.timestamp))) |
| 270 |
if rev.committer: |
| 271 |
props.append((gettext("Committer:"), htmlize(rev.committer))) |
| 272 |
|
| 273 |
authors = rev.properties.get('authors') |
| 274 |
if not authors: |
| 275 |
authors = rev.properties.get('author') |
| 276 |
if authors: |
| 277 |
props.append((gettext("Author:"), htmlize(authors))) |
| 278 |
|
| 279 |
branch_nick = rev.properties.get('branch-nick') |
| 280 |
if branch_nick: |
| 281 |
props.append((gettext("Branch:"), htmlize(branch_nick))) |
| 282 |
|
| 283 |
tags = self.get_tags(rev.revision_id) |
| 284 |
if tags: |
| 285 |
tags = list(map(quote_tag, tags)) |
| 286 |
props.append((gettext("Tags:"), htmlencode(", ".join(tags)))) |
| 287 |
|
| 288 |
bugs = [] |
| 289 |
for bug in rev.properties.get('bugs', '').split('\n'): |
| 290 |
if bug: |
| 291 |
try: |
| 292 |
url, status = bug.split(' ', 1) |
| 293 |
bugs.append('<a href="%(url)s">%(url)s</a> %(status)s' % (dict(url=url, status=gettext(status)))) |
| 294 |
except ValueError: |
| 295 |
bugs.append(bug) |
| 296 |
if bugs: |
| 297 |
props.append((ngettext("Bug:", "Bugs:", len(bugs)), ", ".join(bugs))) |
| 298 |
|
| 299 |
foreign_attribs = None |
| 300 |
if isinstance(rev, foreign.ForeignRevision): |
| 301 |
foreign_attribs = rev.mapping.vcs.show_foreign_revid(rev.foreign_revid) |
| 302 |
elif b":" in rev.revision_id: |
| 303 |
try: |
| 304 |
foreign_revid, mapping = foreign.foreign_vcs_registry.parse_revision_id(rev.revision_id) |
| 305 |
foreign_attribs = mapping.vcs.show_foreign_revid(foreign_revid) |
| 306 |
except errors.InvalidRevisionId: |
| 307 |
pass |
| 308 |
|
| 309 |
if foreign_attribs: |
| 310 |
keys = list(foreign_attribs.keys()) |
| 311 |
keys.sort() |
| 312 |
for key in keys: |
| 313 |
props.append((key + ":", foreign_attribs[key])) |
| 314 |
return props |
| 315 |
|
| 316 |
def get_parents(self, revid): |
| 317 |
|
| 318 |
return [] |
| 319 |
|
| 320 |
def get_children(self, revid): |
| 321 |
|
| 322 |
return [] |
| 323 |
|
| 324 |
def get_revno(self, revid): |
| 325 |
|
| 326 |
return "" |
| 327 |
|
| 328 |
def get_search_replace(self, revid): |
| 329 |
|
| 330 |
return None |
| 331 |
|
| 332 |
def get_merge_depth(self, revid): |
| 333 |
|
| 334 |
return 0 |
| 335 |
|
| 336 |
def get_color(self, revid): |
| 337 |
|
| 338 |
return None |
| 339 |
|
| 340 |
def get_tags(self, revid): |
| 341 |
return None |
| 342 |
|
| 343 |
def setSource(self, uri): |
| 344 |
pass |
| 345 |
|
| 346 |
|
| 347 |
class LogListRevisionMessageBrowser(RevisionMessageBrowser): |
| 348 |
"""RevisionMessageBrowser customized to work with LogList""" |
| 349 |
|
| 350 |
def __init__(self, log_list, parent=None): |
| 351 |
super(LogListRevisionMessageBrowser, self).__init__(parent) |
| 352 |
self.log_list = log_list |
| 353 |
|
| 354 |
self.log_list.selectionModel().selectionChanged[QtCore.QItemSelection, QtCore.QItemSelection].connect(self.update_selection) |
| 355 |
self.anchorClicked[QtCore.QUrl].connect(self.link_clicked) |
| 356 |
self.throbber = parent.throbber |
| 357 |
|
| 358 |
@runs_in_loading_queue |
| 359 |
@ui_current_widget |
| 360 |
def update_selection(self, selected, deselected): |
| 361 |
indexes = self.log_list.get_selection_indexes() |
| 362 |
if not indexes: |
| 363 |
self.setHtml("") |
| 364 |
else: |
| 365 |
revids = [index.data(logmodel.RevIdRole) for index in indexes] |
| 366 |
self.set_display_revids( |
| 367 |
revids, self.log_list.log_model.graph_viz.get_repo_revids) |
| 368 |
|
| 369 |
def link_clicked(self, url): |
| 370 |
scheme = str(url.scheme()) |
| 371 |
if scheme == 'qlog-revid': |
| 372 |
revision_id = str(url.path()).encode('ascii') |
| 373 |
self.log_list.select_revid(revision_id) |
| 374 |
else: |
| 375 |
open_browser(str(url.toEncoded())) |
| 376 |
|
| 377 |
def get_parents(self, revid): |
| 378 |
return self.log_list.log_model.graph_viz.known_graph.get_parent_keys(revid) |
| 379 |
|
| 380 |
def get_children(self, revid): |
| 381 |
return [child for child in |
| 382 |
self.log_list.log_model.graph_viz.known_graph.get_child_keys(revid) |
| 383 |
if not child == "top:"] |
| 384 |
|
| 385 |
def get_revno(self, revid): |
| 386 |
return self.log_list.log_model.graph_viz.revid_rev[revid].revno_str |
| 387 |
|
| 388 |
def get_merge_depth(self, revid): |
| 389 |
return self.log_list.log_model.graph_viz.revid_rev[revid].merge_depth |
| 390 |
|
| 391 |
def get_color(self, revid): |
| 392 |
return self.log_list.log_model.graph_viz.revid_rev[revid].color |
| 393 |
|
| 394 |
def get_tags(self, revid): |
| 395 |
return self.log_list.log_model.graph_viz.tags.get(revid) |