graph.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #!/usr/bin/env python
  2. # vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab
  3. # Copyright (c) 2020 Kevin B. Hendricks, Stratford Ontario Canada
  4. #
  5. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  6. # General Public License as public by the Free Software Foundation; version 2.0
  7. # or (at your option) any later version. You can redistribute it and/or
  8. # modify it under the terms of either of these two licenses.
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. # You should have received a copy of the licenses; if not, see
  17. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  18. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  19. # License, Version 2.0.
  20. """Implementation of merge-base following the approach of git."""
  21. from heapq import heappop, heappush
  22. from .lru_cache import LRUCache
  23. # priority queue using builtin python minheap tools
  24. # why they do not have a builtin maxheap is simply ridiculous but
  25. # liveable with integer time stamps using negation
  26. class WorkList:
  27. def __init__(self):
  28. self.pq = []
  29. def add(self, item):
  30. dt, cmt = item
  31. heappush(self.pq, (-dt, cmt))
  32. def get(self):
  33. item = heappop(self.pq)
  34. if item:
  35. pr, cmt = item
  36. return -pr, cmt
  37. return None
  38. def iter(self):
  39. for pr, cmt in self.pq:
  40. yield (-pr, cmt)
  41. def _find_lcas(lookup_parents, c1, c2s, lookup_stamp, min_stamp=0):
  42. cands = []
  43. cstates = {}
  44. # Flags to Record State
  45. _ANC_OF_1 = 1 # ancestor of commit 1
  46. _ANC_OF_2 = 2 # ancestor of commit 2
  47. _DNC = 4 # Do Not Consider
  48. _LCA = 8 # potential LCA (Lowest Common Ancestor)
  49. def _has_candidates(wlst, cstates):
  50. for dt, cmt in wlst.iter():
  51. if cmt in cstates:
  52. if not ((cstates[cmt] & _DNC) == _DNC):
  53. return True
  54. return False
  55. # initialize the working list states with ancestry info
  56. # note possibility of c1 being one of c2s should be handled
  57. wlst = WorkList()
  58. cstates[c1] = _ANC_OF_1
  59. wlst.add((lookup_stamp(c1), c1))
  60. for c2 in c2s:
  61. cflags = cstates.get(c2, 0)
  62. cstates[c2] = cflags | _ANC_OF_2
  63. wlst.add((lookup_stamp(c2), c2))
  64. # loop while at least one working list commit is still viable (not marked as _DNC)
  65. # adding any parents to the list in a breadth first manner
  66. while _has_candidates(wlst, cstates):
  67. dt, cmt = wlst.get()
  68. # Look only at ANCESTRY and _DNC flags so that already
  69. # found _LCAs can still be marked _DNC by lower _LCAS
  70. cflags = cstates[cmt] & (_ANC_OF_1 | _ANC_OF_2 | _DNC)
  71. if cflags == (_ANC_OF_1 | _ANC_OF_2):
  72. # potential common ancestor if not already in candidates add it
  73. if not (cstates[cmt] & _LCA) == _LCA:
  74. cstates[cmt] = cstates[cmt] | _LCA
  75. cands.append((dt, cmt))
  76. # mark any parents of this node _DNC as all parents
  77. # would be one generation further removed common ancestors
  78. cflags = cflags | _DNC
  79. parents = lookup_parents(cmt)
  80. if parents:
  81. for pcmt in parents:
  82. pflags = cstates.get(pcmt, 0)
  83. # if this parent was already visited with no new ancestry/flag information
  84. # do not add it to the working list again
  85. if (pflags & cflags) == cflags:
  86. continue
  87. pdt = lookup_stamp(pcmt)
  88. if pdt < min_stamp:
  89. continue
  90. cstates[pcmt] = pflags | cflags
  91. wlst.add((pdt, pcmt))
  92. # walk final candidates removing any superseded by _DNC by later lower _LCAs
  93. # remove any duplicates and sort it so that earliest is first
  94. results = []
  95. for dt, cmt in cands:
  96. if not ((cstates[cmt] & _DNC) == _DNC) and (dt, cmt) not in results:
  97. results.append((dt, cmt))
  98. results.sort(key=lambda x: x[0])
  99. lcas = [cmt for dt, cmt in results]
  100. return lcas
  101. # actual git sorts these based on commit times
  102. def find_merge_base(repo, commit_ids):
  103. """Find lowest common ancestors of commit_ids[0] and *any* of commits_ids[1:].
  104. Args:
  105. repo: Repository object
  106. commit_ids: list of commit ids
  107. Returns:
  108. list of lowest common ancestor commit_ids
  109. """
  110. cmtcache = LRUCache(max_cache=128)
  111. parents_provider = repo.parents_provider()
  112. def lookup_stamp(cmtid):
  113. if cmtid not in cmtcache:
  114. cmtcache[cmtid] = repo.object_store[cmtid]
  115. return cmtcache[cmtid].commit_time
  116. def lookup_parents(cmtid):
  117. commit = None
  118. if cmtid in cmtcache:
  119. commit = cmtcache[cmtid]
  120. # must use parents provider to handle grafts and shallow
  121. return parents_provider.get_parents(cmtid, commit=commit)
  122. if not commit_ids:
  123. return []
  124. c1 = commit_ids[0]
  125. if not len(commit_ids) > 1:
  126. return [c1]
  127. c2s = commit_ids[1:]
  128. if c1 in c2s:
  129. return [c1]
  130. lcas = _find_lcas(lookup_parents, c1, c2s, lookup_stamp)
  131. return lcas
  132. def find_octopus_base(repo, commit_ids):
  133. """Find lowest common ancestors of *all* provided commit_ids.
  134. Args:
  135. repo: Repository
  136. commit_ids: list of commit ids
  137. Returns:
  138. list of lowest common ancestor commit_ids
  139. """
  140. cmtcache = LRUCache(max_cache=128)
  141. parents_provider = repo.parents_provider()
  142. def lookup_stamp(cmtid):
  143. if cmtid not in cmtcache:
  144. cmtcache[cmtid] = repo.object_store[cmtid]
  145. return cmtcache[cmtid].commit_time
  146. def lookup_parents(cmtid):
  147. commit = None
  148. if cmtid in cmtcache:
  149. commit = cmtcache[cmtid]
  150. # must use parents provider to handle grafts and shallow
  151. return parents_provider.get_parents(cmtid, commit=commit)
  152. if not commit_ids:
  153. return []
  154. if len(commit_ids) <= 2:
  155. return find_merge_base(repo, commit_ids)
  156. lcas = [commit_ids[0]]
  157. others = commit_ids[1:]
  158. for cmt in others:
  159. next_lcas = []
  160. for ca in lcas:
  161. res = _find_lcas(lookup_parents, cmt, [ca], lookup_stamp)
  162. next_lcas.extend(res)
  163. lcas = next_lcas[:]
  164. return lcas
  165. def can_fast_forward(repo, c1, c2):
  166. """Is it possible to fast-forward from c1 to c2?
  167. Args:
  168. repo: Repository to retrieve objects from
  169. c1: Commit id for first commit
  170. c2: Commit id for second commit
  171. """
  172. cmtcache = LRUCache(max_cache=128)
  173. parents_provider = repo.parents_provider()
  174. def lookup_stamp(cmtid):
  175. if cmtid not in cmtcache:
  176. cmtcache[cmtid] = repo.object_store[cmtid]
  177. return cmtcache[cmtid].commit_time
  178. def lookup_parents(cmtid):
  179. commit = None
  180. if cmtid in cmtcache:
  181. commit = cmtcache[cmtid]
  182. # must use parents provider to handle grafts and shallow
  183. return parents_provider.get_parents(cmtid, commit=commit)
  184. if c1 == c2:
  185. return True
  186. # Algorithm: Find the common ancestor
  187. min_stamp = lookup_stamp(c1)
  188. lcas = _find_lcas(lookup_parents, c1, [c2], lookup_stamp, min_stamp=min_stamp)
  189. return lcas == [c1]