PageRenderTime 148ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/tortoisehg/hgqt/graph.py

https://bitbucket.org/tortoisehg/hgtk/
Python | 362 lines | 333 code | 1 blank | 28 comment | 1 complexity | a918bd345093394f0889eb0ca4f9936d MD5 | raw file
Possible License(s): GPL-2.0
  1. # Copyright (c) 2003-2010 LOGILAB S.A. (Paris, FRANCE).
  2. # http://www.logilab.fr/ -- mailto:contact@logilab.fr
  3. #
  4. # This program is free software; you can redistribute it and/or modify it under
  5. # the terms of the GNU General Public License as published by the Free Software
  6. # Foundation; either version 2 of the License, or (at your option) any later
  7. # version.
  8. #
  9. # This program is distributed in the hope that it will be useful, but WITHOUT
  10. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License along with
  14. # this program; if not, write to the Free Software Foundation, Inc.,
  15. # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. """helper functions and classes to ease hg revision graph building
  17. Based on graphlog's algorithm, with insipration stolen from TortoiseHg
  18. revision grapher (now stolen back).
  19. """
  20. import time
  21. import os
  22. import itertools
  23. from mercurial import util, error
  24. from tortoisehg.util.hglib import tounicode
  25. def revision_grapher(repo, **opts):
  26. """incremental revision grapher
  27. This generator function walks through the revision history from
  28. revision start_rev to revision stop_rev (which must be less than
  29. or equal to start_rev) and for each revision emits tuples with the
  30. following elements:
  31. - current revision
  32. - column of the current node in the set of ongoing edges
  33. - color of the node (?)
  34. - lines; a list of (col, next_col, color) indicating the edges between
  35. the current row and the next row
  36. - parent revisions of current revision
  37. - author of the current revision
  38. If follow is True, only generated the subtree from the start_rev head.
  39. If branch is set, only generated the subtree for the given named branch.
  40. If allparents is set, include the branch heads for the selected named
  41. branch heads and all ancestors. If not set, include only the revisions
  42. on the selected named branch.
  43. """
  44. revset = opts.get('revset', None)
  45. if revset:
  46. revset = sorted([repo[r].rev() for r in revset])
  47. start_rev = max(revset)
  48. stop_rev = min(revset)
  49. branch = None
  50. follow = False
  51. hidden = lambda rev: rev not in revset
  52. else:
  53. start_rev = opts.get('start_rev', None)
  54. stop_rev = opts.get('stop_rev', 0)
  55. branch = opts.get('branch', None)
  56. follow = opts.get('follow', False)
  57. hidden = lambda rev: False
  58. assert start_rev is None or start_rev >= stop_rev
  59. curr_rev = start_rev
  60. revs = []
  61. rev_color = {}
  62. nextcolor = 0
  63. if opts.get('allparents') or not branch:
  64. def getparents(ctx):
  65. return [x.rev() for x in ctx.parents() if x]
  66. else:
  67. def getparents(ctx):
  68. return [x.rev() for x in ctx.parents() \
  69. if x and x.branch() == branch]
  70. while curr_rev is None or curr_rev >= stop_rev:
  71. if hidden(curr_rev):
  72. curr_rev -= 1
  73. continue
  74. # Compute revs and next_revs.
  75. ctx = repo[curr_rev]
  76. # Compute revs and next_revs.
  77. if curr_rev not in revs:
  78. if branch and ctx.branch() != branch:
  79. if curr_rev is None:
  80. curr_rev = len(repo)
  81. else:
  82. curr_rev -= 1
  83. yield None
  84. continue
  85. # New head.
  86. if start_rev and follow and curr_rev != start_rev:
  87. curr_rev -= 1
  88. continue
  89. revs.append(curr_rev)
  90. rev_color[curr_rev] = curcolor = nextcolor
  91. nextcolor += 1
  92. p_revs = getparents(ctx)
  93. while p_revs:
  94. rev0 = p_revs[0]
  95. if rev0 < stop_rev or rev0 in rev_color:
  96. break
  97. rev_color[rev0] = curcolor
  98. p_revs = getparents(repo[rev0])
  99. curcolor = rev_color[curr_rev]
  100. rev_index = revs.index(curr_rev)
  101. next_revs = revs[:]
  102. # Add parents to next_revs.
  103. parents = [p for p in getparents(ctx) if not hidden(p)]
  104. try:
  105. author = ctx.user()
  106. except error.Abort:
  107. author = ''
  108. parents_to_add = []
  109. if len(parents) > 1:
  110. preferred_color = None
  111. else:
  112. preferred_color = curcolor
  113. for parent in parents:
  114. if parent not in next_revs:
  115. parents_to_add.append(parent)
  116. if parent not in rev_color:
  117. if preferred_color:
  118. rev_color[parent] = preferred_color
  119. preferred_color = None
  120. else:
  121. rev_color[parent] = nextcolor
  122. nextcolor += 1
  123. preferred_color = None
  124. # parents_to_add.sort()
  125. next_revs[rev_index:rev_index + 1] = parents_to_add
  126. lines = []
  127. for i, rev in enumerate(revs):
  128. if rev in next_revs:
  129. color = rev_color[rev]
  130. lines.append( (i, next_revs.index(rev), color) )
  131. elif rev == curr_rev:
  132. for parent in parents:
  133. color = rev_color[parent]
  134. lines.append( (i, next_revs.index(parent), color) )
  135. yield (curr_rev, rev_index, curcolor, lines, parents, author)
  136. revs = next_revs
  137. if curr_rev is None:
  138. curr_rev = len(repo)
  139. else:
  140. curr_rev -= 1
  141. def filelog_grapher(repo, path):
  142. '''
  143. Graph the ancestry of a single file (log). Deletions show
  144. up as breaks in the graph.
  145. '''
  146. filerev = len(repo.file(path)) - 1
  147. fctx = repo.filectx(path, fileid=filerev)
  148. rev = fctx.rev()
  149. flog = fctx.filelog()
  150. heads = [repo.filectx(path, fileid=flog.rev(x)).rev() for x in flog.heads()]
  151. assert rev in heads
  152. heads.remove(rev)
  153. revs = []
  154. rev_color = {}
  155. nextcolor = 0
  156. _paths = {}
  157. while rev >= 0:
  158. # Compute revs and next_revs
  159. if rev not in revs:
  160. revs.append(rev)
  161. rev_color[rev] = nextcolor ; nextcolor += 1
  162. curcolor = rev_color[rev]
  163. index = revs.index(rev)
  164. next_revs = revs[:]
  165. # Add parents to next_revs
  166. fctx = repo.filectx(_paths.get(rev, path), changeid=rev)
  167. for pfctx in fctx.parents():
  168. _paths[pfctx.rev()] = pfctx.path()
  169. parents = [pfctx.rev() for pfctx in fctx.parents()]# if f.path() == path]
  170. parents_to_add = []
  171. for parent in parents:
  172. if parent not in next_revs:
  173. parents_to_add.append(parent)
  174. if len(parents) > 1:
  175. rev_color[parent] = nextcolor ; nextcolor += 1
  176. else:
  177. rev_color[parent] = curcolor
  178. parents_to_add.sort()
  179. next_revs[index:index + 1] = parents_to_add
  180. lines = []
  181. for i, nrev in enumerate(revs):
  182. if nrev in next_revs:
  183. color = rev_color[nrev]
  184. lines.append( (i, next_revs.index(nrev), color) )
  185. elif nrev == rev:
  186. for parent in parents:
  187. color = rev_color[parent]
  188. lines.append( (i, next_revs.index(parent), color) )
  189. pcrevs = [pfc.rev() for pfc in fctx.parents()]
  190. yield (fctx.rev(), index, curcolor, lines, pcrevs,
  191. _paths.get(fctx.rev(), path), fctx.user())
  192. revs = next_revs
  193. if revs:
  194. rev = max(revs)
  195. else:
  196. rev = -1
  197. if heads and rev <= heads[-1]:
  198. rev = heads.pop()
  199. def mq_patch_grapher(repo):
  200. """Graphs unapplied MQ patches"""
  201. for patchname in reversed(repo.thgmqunappliedpatches):
  202. yield (patchname, 0, "", [], [], "")
  203. class GraphNode(object):
  204. """
  205. Simple class to encapsulate e hg node in the revision graph. Does
  206. nothing but declaring attributes.
  207. """
  208. def __init__(self, rev, xposition, color, lines, parents, ncols=None,
  209. extra=None):
  210. self.rev = rev
  211. self.x = xposition
  212. self.color = color
  213. if ncols is None:
  214. ncols = len(lines)
  215. self.cols = ncols
  216. self.parents = parents
  217. self.bottomlines = lines
  218. self.toplines = []
  219. self.extra = extra
  220. class Graph(object):
  221. """
  222. Graph object to ease hg repo navigation. The Graph object
  223. instanciate a `revision_grapher` generator, and provide a `fill`
  224. method to build the graph progressively.
  225. """
  226. #@timeit
  227. def __init__(self, repo, grapher, include_mq=False):
  228. self.repo = repo
  229. self.maxlog = len(repo)
  230. if include_mq:
  231. patch_grapher = mq_patch_grapher(self.repo)
  232. self.grapher = itertools.chain(patch_grapher, grapher)
  233. else:
  234. self.grapher = grapher
  235. self.nodes = []
  236. self.nodesdict = {}
  237. self.max_cols = 0
  238. self.authors = set()
  239. def __getitem__(self, idx):
  240. if isinstance(idx, slice):
  241. # XXX TODO: ensure nodes are built
  242. return self.nodes.__getitem__(idx)
  243. if idx >= len(self.nodes):
  244. # build as many graph nodes as required to answer the
  245. # requested idx
  246. self.build_nodes(idx)
  247. if idx > len(self):
  248. return self.nodes[-1]
  249. return self.nodes[idx]
  250. def __len__(self):
  251. # len(graph) is the number of actually built graph nodes
  252. return len(self.nodes)
  253. def build_nodes(self, nnodes=None, rev=None):
  254. """
  255. Build up to `nnodes` more nodes in our graph, or build as many
  256. nodes required to reach `rev`.
  257. If both rev and nnodes are set, build as many nodes as
  258. required to reach rev plus nnodes more.
  259. """
  260. if self.grapher is None:
  261. return False
  262. usetimer = nnodes is None and rev is None
  263. if usetimer:
  264. if os.name == "nt":
  265. timer = time.clock
  266. else:
  267. timer = time.time
  268. startsec = timer()
  269. stopped = False
  270. mcol = set([self.max_cols])
  271. for vnext in self.grapher:
  272. if vnext is None:
  273. continue
  274. nrev, xpos, color, lines, parents, author = vnext[:6]
  275. self.authors.add(author)
  276. if not type(nrev) == str and nrev >= self.maxlog:
  277. continue
  278. gnode = GraphNode(nrev, xpos, color, lines, parents,
  279. extra=vnext[5:])
  280. if self.nodes:
  281. gnode.toplines = self.nodes[-1].bottomlines
  282. self.nodes.append(gnode)
  283. self.nodesdict[nrev] = gnode
  284. mcol = mcol.union(set([xpos]))
  285. mcol = mcol.union(set([max(x[:2]) for x in gnode.bottomlines]))
  286. if rev is not None and nrev <= rev:
  287. rev = None # we reached rev, switching to nnode counter
  288. if rev is None:
  289. if nnodes is not None:
  290. nnodes -= 1
  291. if not nnodes:
  292. break
  293. if usetimer:
  294. cursec = timer()
  295. if cursec < startsec or cursec > startsec + 0.1:
  296. break
  297. else:
  298. self.grapher = None
  299. stopped = True
  300. self.max_cols = max(mcol) + 1
  301. return not stopped
  302. def isfilled(self):
  303. return self.grapher is None
  304. def index(self, rev):
  305. if len(self) == 0: # graph is empty, let's build some nodes
  306. self.build_nodes(10)
  307. if rev is not None and rev < self.nodes[-1].rev:
  308. self.build_nodes(self.nodes[-1].rev - rev)
  309. if rev in self.nodesdict:
  310. return self.nodes.index(self.nodesdict[rev])
  311. return -1
  312. #
  313. # File graph method
  314. #
  315. def filename(self, rev):
  316. return self.nodesdict[rev].extra[0]