Repair rev trees that have cycles introduced by SG Issue #2847
(ctx context.Context)
| 220 | |
| 221 | // Repair rev trees that have cycles introduced by SG Issue #2847 |
| 222 | func (tree RevTree) RepairCycles(ctx context.Context) (err error) { |
| 223 | |
| 224 | // This function will be called back for every leaf node in tree |
| 225 | leafProcessor := func(leaf *RevInfo) { |
| 226 | |
| 227 | // Walk up the tree until we find a root, and append each node |
| 228 | node := leaf |
| 229 | if node.IsRoot() { |
| 230 | return |
| 231 | } |
| 232 | |
| 233 | for { |
| 234 | |
| 235 | if node.ParentGenGTENodeGen(ctx) { |
| 236 | base.InfofCtx(ctx, base.KeyCRUD, "Node %+v detected to have invalid parent rev (parent generation larger than node generation). Repairing by designating as a root node.", base.UD(node)) |
| 237 | node.Parent = "" |
| 238 | break |
| 239 | } |
| 240 | |
| 241 | node = tree[node.Parent] |
| 242 | |
| 243 | // Reached a root, we're done -- there's no need |
| 244 | // to call appendNodeToResult() on the root, since |
| 245 | // the child of the root will have already added a node |
| 246 | // pointing to the root. |
| 247 | if node.IsRoot() { |
| 248 | break |
| 249 | } |
| 250 | |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | // Iterate over leaves |
| 255 | tree.forEachLeaf(leafProcessor) |
| 256 | |
| 257 | return nil |
| 258 | } |
| 259 | |
| 260 | // Detect situations like: |
| 261 | // |