graph.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 collections import deque
  22. from typing import Deque
  23. def _find_lcas(lookup_parents, c1, c2s):
  24. cands = []
  25. cstates = {}
  26. # Flags to Record State
  27. _ANC_OF_1 = 1 # ancestor of commit 1
  28. _ANC_OF_2 = 2 # ancestor of commit 2
  29. _DNC = 4 # Do Not Consider
  30. _LCA = 8 # potential LCA
  31. def _has_candidates(wlst, cstates):
  32. for cmt in wlst:
  33. if cmt in cstates:
  34. if not (cstates[cmt] & _DNC):
  35. return True
  36. return False
  37. # initialize the working list
  38. wlst: Deque[int] = deque()
  39. cstates[c1] = _ANC_OF_1
  40. wlst.append(c1)
  41. for c2 in c2s:
  42. cstates[c2] = _ANC_OF_2
  43. wlst.append(c2)
  44. # loop until no other LCA candidates are viable in working list
  45. # adding any parents to the list in a breadth first manner
  46. while _has_candidates(wlst, cstates):
  47. cmt = wlst.popleft()
  48. flags = cstates[cmt]
  49. if flags == (_ANC_OF_1 | _ANC_OF_2):
  50. # potential common ancestor
  51. if not (flags & _LCA):
  52. flags = flags | _LCA
  53. cstates[cmt] = flags
  54. cands.append(cmt)
  55. # mark any parents of this node _DNC as all parents
  56. # would be one level further removed common ancestors
  57. flags = flags | _DNC
  58. parents = lookup_parents(cmt)
  59. if parents:
  60. for pcmt in parents:
  61. if pcmt in cstates:
  62. cstates[pcmt] = cstates[pcmt] | flags
  63. else:
  64. cstates[pcmt] = flags
  65. wlst.append(pcmt)
  66. # walk final candidates removing any superseded by _DNC by later lower LCAs
  67. results = []
  68. for cmt in cands:
  69. if not (cstates[cmt] & _DNC):
  70. results.append(cmt)
  71. return results
  72. def find_merge_base(repo, commit_ids):
  73. """Find lowest common ancestors of commit_ids[0] and *any* of commits_ids[1:].
  74. Args:
  75. repo: Repository object
  76. commit_ids: list of commit ids
  77. Returns:
  78. list of lowest common ancestor commit_ids
  79. """
  80. if not commit_ids:
  81. return []
  82. c1 = commit_ids[0]
  83. if not len(commit_ids) > 1:
  84. return [c1]
  85. c2s = commit_ids[1:]
  86. if c1 in c2s:
  87. return [c1]
  88. parents_provider = repo.parents_provider()
  89. return _find_lcas(parents_provider.get_parents, c1, c2s)
  90. def find_octopus_base(repo, commit_ids):
  91. """Find lowest common ancestors of *all* provided commit_ids.
  92. Args:
  93. repo: Repository
  94. commit_ids: list of commit ids
  95. Returns:
  96. list of lowest common ancestor commit_ids
  97. """
  98. if not commit_ids:
  99. return []
  100. if len(commit_ids) <= 2:
  101. return find_merge_base(repo, commit_ids)
  102. parents_provider = repo.parents_provider()
  103. lcas = [commit_ids[0]]
  104. others = commit_ids[1:]
  105. for cmt in others:
  106. next_lcas = []
  107. for ca in lcas:
  108. res = _find_lcas(parents_provider.get_parents, cmt, [ca])
  109. next_lcas.extend(res)
  110. lcas = next_lcas[:]
  111. return lcas
  112. def can_fast_forward(repo, c1, c2):
  113. """Is it possible to fast-forward from c1 to c2?
  114. Args:
  115. repo: Repository to retrieve objects from
  116. c1: Commit id for first commit
  117. c2: Commit id for second commit
  118. """
  119. if c1 == c2:
  120. return True
  121. # Algorithm: Find the common ancestor
  122. parents_provider = repo.parents_provider()
  123. lcas = _find_lcas(parents_provider.get_parents, c1, [c2])
  124. return lcas == [c1]