interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / Documentation / technical / paint-down-to-common.adoc

155 lines · 6342 bytes Raw Blame History
1Merge-Base Computation and paint_down_to_common()
2==================================================
3
4The function `paint_down_to_common()` in `commit-reach.c` computes merge
5bases by walking the commit graph backwards from two sets of tips and
6finding where their ancestry meets.
7
8Use cases
9---------
10
11Computing 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
23Both use cases share the same algorithm and implementation.
24
25Algorithm
26---------
27
28Given a commit `one` and a set of commits `twos[]`, the walk paints
29commits with two colors:
30
31 - PARENT1: reachable from `one`
32 - PARENT2: reachable from any commit in `twos[]`
33
34The walk uses a priority queue ordered by generation number
35(highest first), breaking ties by commit date. Each step dequeues
36the highest-priority commit and propagates its paint flags to its
37parents, enqueuing any parent that gained new flags. When a
38commit receives both PARENT1 and PARENT2, it is a merge-base
39candidate. A candidate gains the STALE flag so its ancestors
40propagate staleness -- any deeper common ancestor is necessarily
41redundant.
42
43[[generation-regions]]
44Topologically ordered and unordered generation regions
45------------------------------------------------------
46
47Commits fall into two regions based on whether their generation
48numbers 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
65In the ordered region, a child's generation is strictly greater
66than its parent's. Same-generation commits are necessarily
67independent, so the queue always processes children before
68their parents.
69
70In the unordered region, parent-child pairs can share the same
71generation number, so topological order is not guaranteed. The
72queue uses commit-date as a heuristic, which typically produces
73a reasonable traversal order but may process a parent before
74its child.
75
76Commits not in the commit-graph have generation INFINITY; v1
77commit-graphs saturate at V1_MAX. Both place commits in the
78unordered region. Any optimization that depends on generation
79ordering must account for this saturation boundary. The early
80exit 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
83commits are treated as unordered.
84
85With generation ordering, values in the unordered region exceed
86those in the ordered region. The walk may therefore transition
87from the unordered region into the ordered region, but never in
88the reverse direction. Without a commit-graph, every commit has INFINITY
89and the walk operates entirely in the unordered region.
90
91In the ordered region, paint on a dequeued commit is final --
92no future step can add flags to it. In the unordered region,
93a dequeued commit may later gain additional paint. Paint flags
94are only added, never removed, bounding the number of
95re-enqueues per commit.
96
97Termination
98-----------
99
100The walk tracks the number of commits of each type in the queue
101(PARENT1-only, PARENT2-only, pending merge-base). The main loop
102ends 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
114Stale entry condition
115~~~~~~~~~~~~~~~~~~~~~
116Once all queued entries are stale, no new merge-base candidates can
117be discovered -- that requires at least one non-stale commit from
118each side meeting. Continuing the walk could still invalidate
119existing candidates by proving one is an ancestor of another, but
120`remove_redundant()` handles that as a post-processing step, so it
121is safe to exit early.
122
123Side-exhaustion condition
124~~~~~~~~~~~~~~~~~~~~~~~~~
125A new merge-base requires commits from both sides to meet. When one
126side's exclusive counter reaches zero and there are no pending
127merge-base candidates, no future traversal step can produce a new
128candidate. This optimization only activates in the ordered region,
129where paint flags are final at visit time; in the unordered region,
130a side that appears exhausted could reappear through late paint
131propagation.
132
133Generation cutoff
134~~~~~~~~~~~~~~~~~
135Some callers (notably `remove_redundant()`) supply a `min_generation`
136threshold equal to the minimum generation of the input commits.
137These callers only need to determine reachability among the inputs,
138not find deep merge bases, so the walk can safely terminate when it
139dequeues a commit below this threshold.
140
141Single result
142~~~~~~~~~~~~~
143When only one merge base is needed and the walk is in the
144ordered region with generation ordering, the first candidate
145found is necessarily the highest-generation common ancestor.
146No remaining commit in the queue can be a descendant of this
147candidate (generation ordering guarantees children are visited
148first), so it cannot be redundant and the walk can stop
149immediately.
150
151Related documentation
152---------------------
153
154 - `Documentation/technical/commit-graph.adoc` -- generation numbers
155 and the reachability closure property.