merge_base.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab
  4. """
  5. Implementation of merge-base following the approach of git
  6. """
  7. # Copyright (c) 2020 Kevin B. Hendricks, Stratford Ontario Canada
  8. #
  9. # Available under the MIT License
  10. import sys
  11. from collections import deque
  12. def _find_lcas(lookup_parents, c1, c2s):
  13. cands = []
  14. cstates = {}
  15. # Flags to Record State
  16. _ANC_OF_1 = 1 # ancestor of commit 1
  17. _ANC_OF_2 = 2 # ancestor of commit 2
  18. _DNC = 4 # Do Not Consider
  19. _LCA = 8 # potential LCA
  20. def _has_candidates(wlst, cstates):
  21. for cmt in wlst:
  22. if cmt in cstates:
  23. if not (cstates[cmt] & _DNC):
  24. return True
  25. return False
  26. # initialize the working list
  27. wlst = deque()
  28. cstates[c1] = _ANC_OF_1
  29. wlst.append(c1)
  30. for c2 in c2s:
  31. cstates[c2] = _ANC_OF_2
  32. wlst.append(c2)
  33. # loop until no other LCA candidates are viable in working list
  34. # adding any parents to the list in a breadth first manner
  35. while _has_candidates(wlst, cstates):
  36. cmt = wlst.popleft()
  37. flags = cstates[cmt]
  38. if flags == (_ANC_OF_1 | _ANC_OF_2):
  39. # potential common ancestor
  40. if not (flags & _LCA):
  41. flags = flags | _LCA
  42. cstates[cmt] = flags
  43. cands.append(cmt)
  44. # mark any parents of this node _DNC as all parents
  45. # would be one level further removed common ancestors
  46. flags = flags | _DNC
  47. parents = lookup_parents(cmt)
  48. if parents:
  49. for pcmt in parents:
  50. if pcmt in cstates:
  51. cstates[pcmt] = cstates[pcmt] | flags
  52. else:
  53. cstates[pcmt] = flags
  54. wlst.append(pcmt)
  55. # walk final candidates removing any superceded by _DNC by later lower LCAs
  56. results = []
  57. for cmt in cands:
  58. if not (cstates[cmt] & _DNC):
  59. results.append(cmt)
  60. return results
  61. def find_merge_base(r, commit_ids):
  62. """ find lowest common ancestors of commit_ids[0] and *any* of commits_ids[1:]
  63. ARGS:
  64. r: Repo object
  65. commit_ids: list of commit ids
  66. Returns
  67. list of lowest common ancestor commit_ids
  68. """
  69. def lookup_parents(commit_id):
  70. return r.object_store[commit_id].parents
  71. if not commit_ids:
  72. return []
  73. c1 = commit_ids[0]
  74. if not len(commit_ids) > 1:
  75. return [c1]
  76. c2s = commit_ids[1:]
  77. if c1 in c2s:
  78. return [c1]
  79. return _find_lcas(lookup_parents, c1, c2s)
  80. def find_octopus_base(r, commit_ids):
  81. """ find lowest common ancestors of *all* provided commit_ids
  82. ARGS:
  83. r: Repo object
  84. commit_ids: list of commit ids
  85. Returns
  86. list of lowest common ancestor commit_ids
  87. """
  88. def lookup_parents(commit_id):
  89. return r.object_store[commit_id].parents
  90. if not commit_ids:
  91. return []
  92. if len(commit_ids) <= 2:
  93. return find_merge_base(r, commit_ids)
  94. lcas = [commit_ids[0]]
  95. others = commit_ids[1:]
  96. for cmt in others:
  97. next_lcas = []
  98. for ca in lcas:
  99. res = _find_lcas(lookup_parents, cmt, [ca])
  100. next_lcas.extend(res)
  101. lcas = next_lcas[:]
  102. return lcas
  103. def test():
  104. all_tests_passed = True
  105. parents = {}
  106. def lookup_parents(commit_id):
  107. return parents.get(commit_id, [])
  108. def run_test(dag, inputs, expected):
  109. nonlocal parents
  110. parents = dag
  111. c1 = inputs[0]
  112. c2s = inputs[1:]
  113. res = _find_lcas(lookup_parents, c1, c2s)
  114. return set(res) == expected
  115. # two lowest common ancestors
  116. test1 = {
  117. '5': ['1', '2'],
  118. '4': ['3', '1'],
  119. '3': ['2'],
  120. '2': ['0'],
  121. '1': [],
  122. '0': []
  123. }
  124. test_passed = run_test(test1, ['4', '5'], set(['1', '2']))
  125. print('Test 1: Multiple LCA ', test_passed)
  126. all_tests_passed = all_tests_passed and test_passed
  127. # no common ancestor
  128. test2 = {
  129. '4': ['2'],
  130. '3': ['1'],
  131. '2': [],
  132. '1': ['0'],
  133. '0': [],
  134. }
  135. test_passed = run_test(test2, ['4', '3'], set([]))
  136. print('Test 2: No Common Ancestor ', test_passed)
  137. all_tests_passed = all_tests_passed and test_passed
  138. # ancestor
  139. test3 = {
  140. 'G': ['D', 'F'],
  141. 'F': ['E'],
  142. 'D': ['C'],
  143. 'C': ['B'],
  144. 'E': ['B'],
  145. 'B': ['A'],
  146. 'A': []
  147. }
  148. test_passed = run_test(test3, ['D', 'C'], set(['C']))
  149. print('Test 3: Ancestor ', test_passed)
  150. all_tests_passed = all_tests_passed and test_passed
  151. # parent
  152. test4 = {
  153. 'G': ['D', 'F'],
  154. 'F': ['E'],
  155. 'D': ['C'],
  156. 'C': ['B'],
  157. 'E': ['B'],
  158. 'B': ['A'],
  159. 'A': []
  160. }
  161. test_passed = run_test(test4, ['G', 'D'], set(['D']))
  162. print('Test 4: Direct Parent ', test_passed)
  163. all_tests_passed = all_tests_passed and test_passed
  164. # Another cross over
  165. test5 = {
  166. 'G': ['D', 'F'],
  167. 'F': ['E', 'C'],
  168. 'D': ['C', 'E'],
  169. 'C': ['B'],
  170. 'E': ['B'],
  171. 'B': ['A'],
  172. 'A': []
  173. }
  174. test_passed = run_test(test5, ['D', 'F'], set(['E', 'C']))
  175. print('Test 5: Cross Over ', test_passed)
  176. all_tests_passed = all_tests_passed and test_passed
  177. # three way merge commit straight from git docs
  178. test6 = {
  179. 'C': ['C1'],
  180. 'C1': ['C2'],
  181. 'C2': ['C3'],
  182. 'C3': ['C4'],
  183. 'C4': ['2'],
  184. 'B': ['B1'],
  185. 'B1': ['B2'],
  186. 'B2': ['B3'],
  187. 'B3': ['1'],
  188. 'A': ['A1'],
  189. 'A1': ['A2'],
  190. 'A2': ['A3'],
  191. 'A3': ['1'],
  192. '1': ['2'],
  193. '2': [],
  194. }
  195. # assumes a theoretical merge M exists that merges B and C first
  196. # which actually means find the first LCA from either of B OR C with A
  197. test_passed = run_test(test6, ['A', 'B', 'C'], set(['1']))
  198. all_tests_passed = all_tests_passed and test_passed
  199. print('Test 6: LCA of 3 commits ', test_passed)
  200. # octopus algorithm test
  201. # test straight from git docs of A, B, and C
  202. # but this time use octopus to find lcas of A, B, and C simultaneously
  203. test7 = {
  204. 'C': ['C1'],
  205. 'C1': ['C2'],
  206. 'C2': ['C3'],
  207. 'C3': ['C4'],
  208. 'C4': ['2'],
  209. 'B': ['B1'],
  210. 'B1': ['B2'],
  211. 'B2': ['B3'],
  212. 'B3': ['1'],
  213. 'A': ['A1'],
  214. 'A1': ['A2'],
  215. 'A2': ['A3'],
  216. 'A3': ['1'],
  217. '1': ['2'],
  218. '2': [],
  219. }
  220. parents = test7
  221. lcas = ['A']
  222. others = ['B', 'C']
  223. for cmt in others:
  224. next_lcas = []
  225. for ca in lcas:
  226. res = _find_lcas(lookup_parents, cmt, [ca])
  227. next_lcas.extend(res)
  228. lcas = next_lcas[:]
  229. test_passed = set(lcas) == set(['2'])
  230. all_tests_passed = all_tests_passed and test_passed
  231. print('Test 7: Octopus LCA of 3 commits ', test_passed)
  232. if all_tests_passed:
  233. print('All Tests Succesfful')
  234. return 0
  235. if __name__ == '__main__':
  236. sys.exit(test())