批量同步Gerrit Change的先后逻辑算法

1． 初始集合 setA = {1,2,3,4}
2． 得出初始数组的依赖集合 setB = {2,3,4}
3． setA – setB，得出 {1}，入列表[1]
4． setA & setB = {2,3,4}，将其设为新的setA
5． 得到新的setB = {4}
6． setA – setB，得出 {2,3}，入列表得到[2,3,1] （注，新的需要放在头上，当然，放成[3,2,1]也可以，因为认为2与3是等价的）
7． setA & setB = {4}，将其设为新的setA
8． setB为空
9． setA – setB，得出 {4}，入列表得到[4,2,3,1]

1. 由于setB为空，跳出循环

    def resortGerritNumberForDependency(self, gerritInfo_arr):
#Sort the gerrit changes here to make sure the depeneded gerritchange is got at first.
#  Algorithm is as following:
#    1. Initial set of gerrit changes topSet, as setGerrit
#    2. Get its depends on gerrit changes, as setDependsGerrit, set(gerritB) = set(gerritB) and set(gerritA)
#    3. DiffGerrit = setGerrit - setDependsGerrits.
#    4. push DiffGerrit into stack
#    5. if setDependsGerrit & setGerrit is not empty, set it as topSet and go to step 1, else break
#    6. Pop gerrit stack. The sequence will be the one to get gerrit changes without dependency conflicts
newOrderGerrit = []
topSet = gerritInfo_arr
while True:
setGerrit = set(topSet)
setDependsGerrits = self.getDependsOnArray(list(setGerrit))
diffSet = setGerrit - setDependsGerrits
if not len(diffSet):
logging.error("There is recursive dependency!")
newOrderGerrit = []
break
else:
#the new found one will be at the head of list which iterated at first
newOrderGerrit = list(diffSet)+(newOrderGerrit)

topSet = setDependsGerrits & setGerrit
if not len(topSet):
#break only this is empty. Because diffSet make sure in each loop, the number will be decreased,
# there will not be any endless loop under this logic.
break

return newOrderGerrit

+ 订阅