Skip to content

Commit f574f35

Browse files
committed
commit-reach: move min_generation check into paint_queue_get()
Consolidate the min_generation termination condition into paint_queue_get(), alongside the existing stale-entry and side-exhaustion checks. Move last_gen into struct paint_state so that commit_graph_generation() is called exactly once per dequeued commit and the result is shared across all termination checks and the monotonicity BUG assertion. The loop body in paint_down_to_common() reads state.last_gen instead of recomputing the generation number. Signed-off-by: Kristofer Karlsson <krka@spotify.com>
1 parent f3572a8 commit f574f35

2 files changed

Lines changed: 27 additions & 13 deletions

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ ends when one of the following conditions holds:
9797
3. Side exhaustion: no pure PARENT1 or pure PARENT2 commits
9898
remain in the queue, no pending merge-base candidates exist,
9999
and the walk has entered the finite-generation region.
100+
4. Generation cutoff: the dequeued commit's generation is below
101+
a caller-supplied `min_generation` threshold.
100102
101103
Stale entry condition
102104
~~~~~~~~~~~~~~~~~~~~~
@@ -121,6 +123,13 @@ time and an exhausted side cannot reappear. In the INFINITY region,
121123
commit-date ordering can violate this guarantee, so the check is
122124
skipped.
123125
126+
Generation cutoff
127+
~~~~~~~~~~~~~~~~~
128+
Some callers (notably `remove_redundant()`) supply a `min_generation`
129+
threshold -- the minimum generation of the input commits. No merge
130+
base can have a generation below this threshold, so the walk
131+
terminates as soon as it dequeues such a commit.
132+
124133
Related documentation
125134
---------------------
126135

commit-reach.c

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ struct paint_state {
8989
int p1_count;
9090
int p2_count;
9191
int pending_merge_bases;
92+
timestamp_t min_generation;
93+
timestamp_t last_gen;
9294
};
9395

9496
static void paint_count_update(struct paint_state *state,
@@ -138,11 +140,23 @@ static void paint_queue_put(struct paint_state *state,
138140
static struct commit *paint_queue_get(struct paint_state *state)
139141
{
140142
struct commit *commit = prio_queue_get(&state->queue);
143+
timestamp_t generation;
141144

142145
if (!commit)
143146
return NULL;
144147

145148
commit->object.flags &= ~ENQUEUED;
149+
generation = commit_graph_generation(commit);
150+
151+
if (generation > state->last_gen)
152+
BUG("bad generation skip %"PRItime" > %"PRItime" at %s",
153+
generation, state->last_gen,
154+
oid_to_hex(&commit->object.oid));
155+
state->last_gen = generation;
156+
157+
/* generation cutoff */
158+
if (state->min_generation && generation < state->min_generation)
159+
return NULL;
146160

147161
if (!state->pending_merge_bases) {
148162
/* only stale entries remain */
@@ -151,7 +165,7 @@ static struct commit *paint_queue_get(struct paint_state *state)
151165

152166
/* one side is exhausted */
153167
if ((!state->p1_count || !state->p2_count) &&
154-
commit_graph_generation(commit) < GENERATION_NUMBER_INFINITY)
168+
generation < GENERATION_NUMBER_INFINITY)
155169
return NULL;
156170
}
157171

@@ -177,9 +191,10 @@ static int paint_down_to_common(struct repository *r,
177191
struct commit *commit;
178192
int i;
179193
int steps = 0;
180-
timestamp_t last_gen = GENERATION_NUMBER_INFINITY;
181194
struct commit_list **tail = result;
182195

196+
state.min_generation = min_generation;
197+
state.last_gen = GENERATION_NUMBER_INFINITY;
183198
if (!min_generation && !corrected_commit_dates_enabled(r))
184199
state.queue.compare = compare_commits_by_commit_date;
185200

@@ -196,18 +211,8 @@ static int paint_down_to_common(struct repository *r,
196211
while ((commit = paint_queue_get(&state))) {
197212
struct commit_list *parents;
198213
int flags;
199-
timestamp_t generation = commit_graph_generation(commit);
200214
steps++;
201215

202-
if (generation > last_gen)
203-
BUG("bad generation skip %"PRItime" > %"PRItime" at %s",
204-
generation, last_gen,
205-
oid_to_hex(&commit->object.oid));
206-
last_gen = generation;
207-
208-
if (generation < min_generation)
209-
break;
210-
211216
flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
212217
if (flags == (PARENT1 | PARENT2)) {
213218
if (!(commit->object.flags & RESULT)) {
@@ -219,7 +224,7 @@ static int paint_down_to_common(struct repository *r,
219224
* descendant of this one.
220225
*/
221226
if (!(mb_flags & MERGE_BASE_FIND_ALL) &&
222-
generation < GENERATION_NUMBER_INFINITY)
227+
state.last_gen < GENERATION_NUMBER_INFINITY)
223228
break;
224229
}
225230
/* Mark parents of a found merge stale */

0 commit comments

Comments
 (0)