Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/Documentation/technical/paint-down-to-common.adoc # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / Documentation / technical / paint-down-to-common.adoc` 155 lines ยท 6342 bytes Raw Blame History 1 Merge-Base Computation and paint_down_to_common() 2 ================================================== 3 4 The function `paint_down_to_common()` in `commit-reach.c` computes merge 5 bases by walking the commit graph backwards from two sets of tips and 6 finding where their ancestry meets. 7 8 Use cases 9 --------- 10 11 Computing merge bases is used in two different ways: 12 13 1. *Finding all merge bases* (`merge-base --all`, `merge-tree`, 14 `merge`, `rebase`). A merge base is a common ancestor that is 15 not itself an ancestor of another common ancestor. 16 17 2. *Ancestry checks* (`in_merge_bases`, used by `merge-base 18 --is-ancestor`, `branch -d`, `fetch`). These ask: "is commit A 19 an ancestor of commit B?" If a common ancestor equals one of the 20 inputs, that input is necessarily the only merge base -- no other 21 common ancestor can be both as recent and not an ancestor of it. 22 23 Both use cases share the same algorithm and implementation. 24 25 Algorithm 26 --------- 27 28 Given a commit `one` and a set of commits `twos[]`, the walk paints 29 commits with two colors: 30 31 - PARENT1: reachable from `one` 32 - PARENT2: reachable from any commit in `twos[]` 33 34 The walk uses a priority queue ordered by generation number 35 (highest first), breaking ties by commit date. Each step dequeues 36 the highest-priority commit and propagates its paint flags to its 37 parents, enqueuing any parent that gained new flags. When a 38 commit receives both PARENT1 and PARENT2, it is a merge-base 39 candidate. A candidate gains the STALE flag so its ancestors 40 propagate staleness -- any deeper common ancestor is necessarily 41 redundant. 42 43 [[generation-regions]] 44 Topologically ordered and unordered generation regions 45 ------------------------------------------------------ 46 47 Commits fall into two regions based on whether their generation 48 numbers provide a topological ordering guarantee: 49 50 .... 51 +------------------------------------------+ 52 | Unordered region | 53 | generation = INFINITY or V1_MAX | 54 | queue order: heuristic (commit date) | 55 +------------------------------------------+ 56 | 57 v 58 +------------------------------------------+ 59 | Ordered region | 60 | generation = finite, unsaturated | 61 | queue order: topological | 62 +------------------------------------------+ 63 .... 64 65 In the ordered region, a child's generation is strictly greater 66 than its parent's. Same-generation commits are necessarily 67 independent, so the queue always processes children before 68 their parents. 69 70 In the unordered region, parent-child pairs can share the same 71 generation number, so topological order is not guaranteed. The 72 queue uses commit-date as a heuristic, which typically produces 73 a reasonable traversal order but may process a parent before 74 its child. 75 76 Commits not in the commit-graph have generation INFINITY; v1 77 commit-graphs saturate at V1_MAX. Both place commits in the 78 unordered region. Any optimization that depends on generation 79 ordering must account for this saturation boundary. The early 80 exit gates compare against a topological ceiling -- 81 `GENERATION_NUMBER_V1_MAX` for v1 graphs and 82 `GENERATION_NUMBER_INFINITY` for v2 graphs -- so that saturated 83 commits are treated as unordered. 84 85 With generation ordering, values in the unordered region exceed 86 those in the ordered region. The walk may therefore transition 87 from the unordered region into the ordered region, but never in 88 the reverse direction. Without a commit-graph, every commit has INFINITY 89 and the walk operates entirely in the unordered region. 90 91 In the ordered region, paint on a dequeued commit is final -- 92 no future step can add flags to it. In the unordered region, 93 a dequeued commit may later gain additional paint. Paint flags 94 are only added, never removed, bounding the number of 95 re-enqueues per commit. 96 97 Termination 98 ----------- 99 100 The walk tracks the number of commits of each type in the queue 101 (PARENT1-only, PARENT2-only, pending merge-base). The main loop 102 ends when one of the following conditions holds: 103 104 1. The queue is empty. 105 2. The queue contains only stale entries. 106 3. Generation cutoff: the dequeued commit's generation is below 107 a caller-supplied `min_generation` threshold. 108 4. Single result: the caller only needs one merge base, one has 109 been found, and the walk has entered the ordered region. 110 5. Side exhaustion: no pure PARENT1 or pure PARENT2 commits 111 remain in the queue, no pending merge-base candidates exist, 112 and the walk has entered the ordered region. 113 114 Stale entry condition 115 ~~~~~~~~~~~~~~~~~~~~~ 116 Once all queued entries are stale, no new merge-base candidates can 117 be discovered -- that requires at least one non-stale commit from 118 each side meeting. Continuing the walk could still invalidate 119 existing candidates by proving one is an ancestor of another, but 120 `remove_redundant()` handles that as a post-processing step, so it 121 is safe to exit early. 122 123 Side-exhaustion condition 124 ~~~~~~~~~~~~~~~~~~~~~~~~~ 125 A new merge-base requires commits from both sides to meet. When one 126 side's exclusive counter reaches zero and there are no pending 127 merge-base candidates, no future traversal step can produce a new 128 candidate. This optimization only activates in the ordered region, 129 where paint flags are final at visit time; in the unordered region, 130 a side that appears exhausted could reappear through late paint 131 propagation. 132 133 Generation cutoff 134 ~~~~~~~~~~~~~~~~~ 135 Some callers (notably `remove_redundant()`) supply a `min_generation` 136 threshold equal to the minimum generation of the input commits. 137 These callers only need to determine reachability among the inputs, 138 not find deep merge bases, so the walk can safely terminate when it 139 dequeues a commit below this threshold. 140 141 Single result 142 ~~~~~~~~~~~~~ 143 When only one merge base is needed and the walk is in the 144 ordered region with generation ordering, the first candidate 145 found is necessarily the highest-generation common ancestor. 146 No remaining commit in the queue can be a descendant of this 147 candidate (generation ordering guarantees children are visited 148 first), so it cannot be redundant and the walk can stop 149 immediately. 150 151 Related documentation 152 --------------------- 153 154 - `Documentation/technical/commit-graph.adoc` -- generation numbers 155 and the reachability closure property.