PageRenderTime 75ms CodeModel.GetById 39ms RepoModel.GetById 0ms app.codeStats 0ms

/mercurial/manifest.py

https://bitbucket.org/mirror/mercurial/
Python | 218 lines | 210 code | 2 blank | 6 comment | 3 complexity | 792f6e73a56d86d6fd2d3ca2b7537369 MD5 | raw file
Possible License(s): GPL-2.0
  1. # manifest.py - manifest revision class for mercurial
  2. #
  3. # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
  4. #
  5. # This software may be used and distributed according to the terms of the
  6. # GNU General Public License version 2 or any later version.
  7. from i18n import _
  8. import mdiff, parsers, error, revlog, util, dicthelpers
  9. import array, struct
  10. class manifestdict(dict):
  11. def __init__(self, mapping=None, flags=None):
  12. if mapping is None:
  13. mapping = {}
  14. if flags is None:
  15. flags = {}
  16. dict.__init__(self, mapping)
  17. self._flags = flags
  18. def flags(self, f):
  19. return self._flags.get(f, "")
  20. def withflags(self):
  21. return set(self._flags.keys())
  22. def set(self, f, flags):
  23. self._flags[f] = flags
  24. def copy(self):
  25. return manifestdict(self, dict.copy(self._flags))
  26. def flagsdiff(self, d2):
  27. return dicthelpers.diff(self._flags, d2._flags, "")
  28. class manifest(revlog.revlog):
  29. def __init__(self, opener):
  30. # we expect to deal with not more than four revs at a time,
  31. # during a commit --amend
  32. self._mancache = util.lrucachedict(4)
  33. revlog.revlog.__init__(self, opener, "00manifest.i")
  34. def parse(self, lines):
  35. mfdict = manifestdict()
  36. parsers.parse_manifest(mfdict, mfdict._flags, lines)
  37. return mfdict
  38. def readdelta(self, node):
  39. r = self.rev(node)
  40. return self.parse(mdiff.patchtext(self.revdiff(self.deltaparent(r), r)))
  41. def readfast(self, node):
  42. '''use the faster of readdelta or read'''
  43. r = self.rev(node)
  44. deltaparent = self.deltaparent(r)
  45. if deltaparent != revlog.nullrev and deltaparent in self.parentrevs(r):
  46. return self.readdelta(node)
  47. return self.read(node)
  48. def read(self, node):
  49. if node == revlog.nullid:
  50. return manifestdict() # don't upset local cache
  51. if node in self._mancache:
  52. return self._mancache[node][0]
  53. text = self.revision(node)
  54. arraytext = array.array('c', text)
  55. mapping = self.parse(text)
  56. self._mancache[node] = (mapping, arraytext)
  57. return mapping
  58. def _search(self, m, s, lo=0, hi=None):
  59. '''return a tuple (start, end) that says where to find s within m.
  60. If the string is found m[start:end] are the line containing
  61. that string. If start == end the string was not found and
  62. they indicate the proper sorted insertion point.
  63. m should be a buffer or a string
  64. s is a string'''
  65. def advance(i, c):
  66. while i < lenm and m[i] != c:
  67. i += 1
  68. return i
  69. if not s:
  70. return (lo, lo)
  71. lenm = len(m)
  72. if not hi:
  73. hi = lenm
  74. while lo < hi:
  75. mid = (lo + hi) // 2
  76. start = mid
  77. while start > 0 and m[start - 1] != '\n':
  78. start -= 1
  79. end = advance(start, '\0')
  80. if m[start:end] < s:
  81. # we know that after the null there are 40 bytes of sha1
  82. # this translates to the bisect lo = mid + 1
  83. lo = advance(end + 40, '\n') + 1
  84. else:
  85. # this translates to the bisect hi = mid
  86. hi = start
  87. end = advance(lo, '\0')
  88. found = m[lo:end]
  89. if s == found:
  90. # we know that after the null there are 40 bytes of sha1
  91. end = advance(end + 40, '\n')
  92. return (lo, end + 1)
  93. else:
  94. return (lo, lo)
  95. def find(self, node, f):
  96. '''look up entry for a single file efficiently.
  97. return (node, flags) pair if found, (None, None) if not.'''
  98. if node in self._mancache:
  99. mapping = self._mancache[node][0]
  100. return mapping.get(f), mapping.flags(f)
  101. text = self.revision(node)
  102. start, end = self._search(text, f)
  103. if start == end:
  104. return None, None
  105. l = text[start:end]
  106. f, n = l.split('\0')
  107. return revlog.bin(n[:40]), n[40:-1]
  108. def add(self, map, transaction, link, p1=None, p2=None,
  109. changed=None):
  110. # apply the changes collected during the bisect loop to our addlist
  111. # return a delta suitable for addrevision
  112. def addlistdelta(addlist, x):
  113. # for large addlist arrays, building a new array is cheaper
  114. # than repeatedly modifying the existing one
  115. currentposition = 0
  116. newaddlist = array.array('c')
  117. for start, end, content in x:
  118. newaddlist += addlist[currentposition:start]
  119. if content:
  120. newaddlist += array.array('c', content)
  121. currentposition = end
  122. newaddlist += addlist[currentposition:]
  123. deltatext = "".join(struct.pack(">lll", start, end, len(content))
  124. + content for start, end, content in x)
  125. return deltatext, newaddlist
  126. def checkforbidden(l):
  127. for f in l:
  128. if '\n' in f or '\r' in f:
  129. raise error.RevlogError(
  130. _("'\\n' and '\\r' disallowed in filenames: %r") % f)
  131. # if we're using the cache, make sure it is valid and
  132. # parented by the same node we're diffing against
  133. if not (changed and p1 and (p1 in self._mancache)):
  134. files = sorted(map)
  135. checkforbidden(files)
  136. # if this is changed to support newlines in filenames,
  137. # be sure to check the templates/ dir again (especially *-raw.tmpl)
  138. hex, flags = revlog.hex, map.flags
  139. text = ''.join("%s\0%s%s\n" % (f, hex(map[f]), flags(f))
  140. for f in files)
  141. arraytext = array.array('c', text)
  142. cachedelta = None
  143. else:
  144. added, removed = changed
  145. addlist = self._mancache[p1][1]
  146. checkforbidden(added)
  147. # combine the changed lists into one list for sorting
  148. work = [(x, False) for x in added]
  149. work.extend((x, True) for x in removed)
  150. # this could use heapq.merge() (from Python 2.6+) or equivalent
  151. # since the lists are already sorted
  152. work.sort()
  153. delta = []
  154. dstart = None
  155. dend = None
  156. dline = [""]
  157. start = 0
  158. # zero copy representation of addlist as a buffer
  159. addbuf = util.buffer(addlist)
  160. # start with a readonly loop that finds the offset of
  161. # each line and creates the deltas
  162. for f, todelete in work:
  163. # bs will either be the index of the item or the insert point
  164. start, end = self._search(addbuf, f, start)
  165. if not todelete:
  166. l = "%s\0%s%s\n" % (f, revlog.hex(map[f]), map.flags(f))
  167. else:
  168. if start == end:
  169. # item we want to delete was not found, error out
  170. raise AssertionError(
  171. _("failed to remove %s from manifest") % f)
  172. l = ""
  173. if dstart is not None and dstart <= start and dend >= start:
  174. if dend < end:
  175. dend = end
  176. if l:
  177. dline.append(l)
  178. else:
  179. if dstart is not None:
  180. delta.append([dstart, dend, "".join(dline)])
  181. dstart = start
  182. dend = end
  183. dline = [l]
  184. if dstart is not None:
  185. delta.append([dstart, dend, "".join(dline)])
  186. # apply the delta to the addlist, and get a delta for addrevision
  187. deltatext, addlist = addlistdelta(addlist, delta)
  188. cachedelta = (self.rev(p1), deltatext)
  189. arraytext = addlist
  190. text = util.buffer(arraytext)
  191. n = self.addrevision(text, transaction, link, p1, p2, cachedelta)
  192. self._mancache[n] = (map, arraytext)
  193. return n