1 \section{Ranking phase}
3 We run the following algorithm:
5 \item Set $\allpatches = \{ \}$.
8 \item Clear out the graph $\hasdirdep$ so it has no edges.
9 \item Execute $\alg{Rank-Recurse}(\pc_0)$
10 \item Until $\allpatches$ remains unchanged.
14 $\alg{Rank-Recurse}(\pc)$ is:
17 \item If we have already done $\alg{Rank-Recurse}(\pc)$ in this
18 ranking iteration, do nothing. Otherwise:
20 \item Add $\pc$ to $\allpatches$ if it is not there already.
21 If it was added, recalculate $\allsrcs$ accordingly.
25 \set S \iassign h(\pcn)
27 \{ \baseof{E} \; | \; E \in \pendsof{\allsrcs}{\pcy} \}
30 and $W \iassign w(\pcn)$
32 \item While $\exists_{S \in \set S} S \ge W$,
33 update $W \assign S$ and $\set S \assign \set S \, \backslash \{ S \}$
35 (This will often remove $W$ from $\set S$. Afterwards, $\set S$
36 is a collection of heads to be merged into $W$.)
38 \item Choose an ordering of $\set S$, $S_i$ for $i=1 \ldots n$.
40 \item For each $S_i$ in turn, choose a corresponding $M_i$
42 M_i \le S_i \land \left[
43 M_i \le W \lor \bigexists_{j<i} M_i \le S_j
47 \item Set $\Gamma \iassign \depsreqof{W}$.
49 If there are multiple candidates we prefer $M_i \in \pcn$
52 \item For each $i \ldots 1..n$, update our putative direct
55 \Gamma \assign \setmergeof{
59 M_i \in \pcn : & \depsreqof{M_i} \\
60 M_i \not\in \pcn : & \{ \}
67 \item Finalise our putative direct dependencies
69 \Gamma \assign g(\pc, \Gamma)
72 \item For each direct dependency $\pd \in \Gamma$,
75 \item Add an edge $\pc \hasdirdep \pd$ to the digraph (adding nodes
77 If this results in a cycle, abort entirely (as the function $g$ is
78 inappropriate; a different $g$ could work).
79 \item Run $\alg{Rank-Recurse}(\pd)$.
84 \subsection{Results of the ranking phase}
86 By the end of the ranking phase, we have recorded the following
91 $ \allpatches, \hasdirdep $ and hence the completion of $\hasdirdep$
92 into the partial order $\hasdep$.
95 For each $\pc \in \allpatches$,
96 the base branch starting point commit $W^{\pcn} = W$.
100 the direct dependencies $\Gamma^{\pc} = \Gamma$.
104 the ordered set of base branch sources $\set S^{\pcn} = \set S,
106 and corresponding merge bases $M^{\pcn}_i = M_i$.
110 \subsection{Proof of termination}
112 $\alg{Rank-Recurse}(\pc)$ recurses but only downwards through the
113 finite graph $\hasdirdep$, so it must terminate.
115 The whole ranking algorithm iterates but each iteration involves
116 adding one or more patches to $\allpatches$. Since there are
117 finitely many patches and we never remove anything from $\allpatches$
118 this must complete eventually.