| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {CommitPreview, Dag, WithPreviewType} from './previews'; |
| b69ab31 | | | 9 | import type {CommitInfo, Hash} from './types'; |
| b69ab31 | | | 10 | |
| b69ab31 | | | 11 | export type CommitTree = { |
| b69ab31 | | | 12 | info: CommitInfo; |
| b69ab31 | | | 13 | children: Array<CommitTree>; |
| b69ab31 | | | 14 | }; |
| b69ab31 | | | 15 | |
| b69ab31 | | | 16 | export type CommitTreeWithPreviews = { |
| b69ab31 | | | 17 | info: CommitInfo; |
| b69ab31 | | | 18 | children: Array<CommitTreeWithPreviews>; |
| b69ab31 | | | 19 | previewType?: CommitPreview; |
| b69ab31 | | | 20 | }; |
| b69ab31 | | | 21 | |
| b69ab31 | | | 22 | const byTimeDecreasing = (a: CommitInfo & WithPreviewType, b: CommitInfo & WithPreviewType) => { |
| b69ab31 | | | 23 | // Consider seqNumber (insertion order during preview calculation). |
| b69ab31 | | | 24 | if (a.seqNumber != null && b.seqNumber != null) { |
| b69ab31 | | | 25 | const seqDelta = a.seqNumber - b.seqNumber; |
| b69ab31 | | | 26 | if (seqDelta !== 0) { |
| b69ab31 | | | 27 | return seqDelta; |
| b69ab31 | | | 28 | } |
| b69ab31 | | | 29 | } |
| b69ab31 | | | 30 | // Sort by date. |
| b69ab31 | | | 31 | const timeDelta = b.date.getTime() - a.date.getTime(); |
| b69ab31 | | | 32 | if (timeDelta !== 0) { |
| b69ab31 | | | 33 | return timeDelta; |
| b69ab31 | | | 34 | } |
| b69ab31 | | | 35 | // Always break ties even if timestamp is the same. |
| b69ab31 | | | 36 | return a.hash < b.hash ? 1 : -1; |
| b69ab31 | | | 37 | }; |
| b69ab31 | | | 38 | |
| b69ab31 | | | 39 | /** |
| b69ab31 | | | 40 | * Given a list of commits from disk, produce a tree capturing the |
| b69ab31 | | | 41 | * parent/child structure of the commits. |
| b69ab31 | | | 42 | * - Public commits are always top level (on the main line) |
| b69ab31 | | | 43 | * - Public commits are sorted by date |
| b69ab31 | | | 44 | * - Draft commits are always offshoots of public commits (never on main line) |
| b69ab31 | | | 45 | * - Caveat: if there are no public commits found, use the parent of everything |
| b69ab31 | | | 46 | * as if it were a public commit |
| b69ab31 | | | 47 | * - If a public commit has no draft children, it is hidden |
| b69ab31 | | | 48 | * - ...unless it has a bookmark |
| b69ab31 | | | 49 | * - If a commit has multiple children, they are sorted by date |
| b69ab31 | | | 50 | */ |
| b69ab31 | | | 51 | export function getCommitTree( |
| b69ab31 | | | 52 | commits: Array<CommitInfo & WithPreviewType>, |
| b69ab31 | | | 53 | ): Array<CommitTreeWithPreviews> { |
| b69ab31 | | | 54 | const childNodesByParent = new Map<string, Set<CommitInfo>>(); |
| b69ab31 | | | 55 | commits.forEach(commit => { |
| b69ab31 | | | 56 | const [parent] = commit.parents; |
| b69ab31 | | | 57 | if (!parent) { |
| b69ab31 | | | 58 | return; |
| b69ab31 | | | 59 | } |
| b69ab31 | | | 60 | let set = childNodesByParent.get(parent); |
| b69ab31 | | | 61 | if (!set) { |
| b69ab31 | | | 62 | set = new Set(); |
| b69ab31 | | | 63 | childNodesByParent.set(parent, set); |
| b69ab31 | | | 64 | } |
| b69ab31 | | | 65 | set.add(commit); |
| b69ab31 | | | 66 | }); |
| b69ab31 | | | 67 | |
| b69ab31 | | | 68 | const makeTree = (revision: CommitInfo & WithPreviewType): CommitTreeWithPreviews => { |
| b69ab31 | | | 69 | const {hash, previewType} = revision; |
| b69ab31 | | | 70 | const childrenSet = childNodesByParent.get(hash) ?? []; |
| b69ab31 | | | 71 | |
| b69ab31 | | | 72 | const childrenInfos = [...childrenSet].sort(byTimeDecreasing); |
| b69ab31 | | | 73 | |
| b69ab31 | | | 74 | const children: Array<CommitTree> = |
| b69ab31 | | | 75 | childrenInfos == null |
| b69ab31 | | | 76 | ? [] |
| b69ab31 | | | 77 | : // only make branches off the main line for non-public revisions |
| b69ab31 | | | 78 | childrenInfos.filter(child => child.phase !== 'public').map(makeTree); |
| b69ab31 | | | 79 | |
| b69ab31 | | | 80 | return { |
| b69ab31 | | | 81 | info: revision, |
| b69ab31 | | | 82 | children, |
| b69ab31 | | | 83 | previewType, |
| b69ab31 | | | 84 | }; |
| b69ab31 | | | 85 | }; |
| b69ab31 | | | 86 | |
| b69ab31 | | | 87 | const initialCommits = commits.filter( |
| b69ab31 | | | 88 | commit => commit.phase === 'public' || commit.parents.length === 0, |
| b69ab31 | | | 89 | ); |
| b69ab31 | | | 90 | |
| b69ab31 | | | 91 | // build tree starting from public revisions |
| b69ab31 | | | 92 | return initialCommits.sort(byTimeDecreasing).map(makeTree); |
| b69ab31 | | | 93 | } |
| b69ab31 | | | 94 | |
| b69ab31 | | | 95 | export function* walkTreePostorder( |
| b69ab31 | | | 96 | commitTree: Array<CommitTreeWithPreviews>, |
| b69ab31 | | | 97 | ): IterableIterator<CommitTreeWithPreviews> { |
| b69ab31 | | | 98 | for (const node of commitTree) { |
| b69ab31 | | | 99 | if (node.children.length > 0) { |
| b69ab31 | | | 100 | yield* walkTreePostorder(node.children); |
| b69ab31 | | | 101 | } |
| b69ab31 | | | 102 | yield node; |
| b69ab31 | | | 103 | } |
| b69ab31 | | | 104 | } |
| b69ab31 | | | 105 | |
| b69ab31 | | | 106 | export function isDescendant(hash: string, commitTree: CommitTree): boolean { |
| b69ab31 | | | 107 | for (const commit of walkTreePostorder([commitTree])) { |
| b69ab31 | | | 108 | if (commit.info.hash === hash) { |
| b69ab31 | | | 109 | return true; |
| b69ab31 | | | 110 | } |
| b69ab31 | | | 111 | } |
| b69ab31 | | | 112 | return false; |
| b69ab31 | | | 113 | } |
| b69ab31 | | | 114 | |
| b69ab31 | | | 115 | /** Test if a tree is linear - no merge or folds. */ |
| b69ab31 | | | 116 | export function isTreeLinear(tree: CommitTreeWithPreviews): boolean { |
| b69ab31 | | | 117 | if (tree.children.length > 1 || tree.info.parents.length > 1) { |
| b69ab31 | | | 118 | return false; |
| b69ab31 | | | 119 | } |
| b69ab31 | | | 120 | return tree.children.every(t => isTreeLinear(t)); |
| b69ab31 | | | 121 | } |
| b69ab31 | | | 122 | |
| b69ab31 | | | 123 | /** Finds the public ancestor by walking up parents, either from a starting hash or from `.` if none provided. */ |
| b69ab31 | | | 124 | export function findPublicBaseAncestor(dag?: Dag, from?: Hash): CommitInfo | undefined { |
| b69ab31 | | | 125 | let commit = dag?.resolve(from ?? '.'); |
| b69ab31 | | | 126 | while (commit) { |
| b69ab31 | | | 127 | if (commit.phase === 'public') { |
| b69ab31 | | | 128 | return commit; |
| b69ab31 | | | 129 | } |
| b69ab31 | | | 130 | commit = dag?.get(commit.parents.at(0)); |
| b69ab31 | | | 131 | } |
| b69ab31 | | | 132 | return undefined; |
| b69ab31 | | | 133 | } |