[PATCH] Tidy up some rev-list-related stuff
[git/spearce.git] / epoch.c
bloba4c82135b873c4e1153a930b692b79642cf96396
1 /*
2 * Copyright (c) 2005, Jon Seymour
4 * For more information about epoch theory on which this module is based,
5 * refer to http://blackcubes.dyndns.org/epoch/. That web page defines
6 * terms such as "epoch" and "minimal, non-linear epoch" and provides rationales
7 * for some of the algorithms used here.
9 */
10 #include <stdlib.h>
12 /* Provides arbitrary precision integers required to accurately represent
13 * fractional mass: */
14 #include <openssl/bn.h>
16 #include "cache.h"
17 #include "commit.h"
18 #include "epoch.h"
20 struct fraction {
21 BIGNUM numerator;
22 BIGNUM denominator;
25 #define HAS_EXACTLY_ONE_PARENT(n) ((n)->parents && !(n)->parents->next)
27 static BN_CTX *context = NULL;
28 static struct fraction *one = NULL;
29 static struct fraction *zero = NULL;
31 static BN_CTX *get_BN_CTX()
33 if (!context) {
34 context = BN_CTX_new();
36 return context;
39 static struct fraction *new_zero()
41 struct fraction *result = xmalloc(sizeof(*result));
42 BN_init(&result->numerator);
43 BN_init(&result->denominator);
44 BN_zero(&result->numerator);
45 BN_one(&result->denominator);
46 return result;
49 static void clear_fraction(struct fraction *fraction)
51 BN_clear(&fraction->numerator);
52 BN_clear(&fraction->denominator);
55 static struct fraction *divide(struct fraction *result, struct fraction *fraction, int divisor)
57 BIGNUM bn_divisor;
59 BN_init(&bn_divisor);
60 BN_set_word(&bn_divisor, divisor);
62 BN_copy(&result->numerator, &fraction->numerator);
63 BN_mul(&result->denominator, &fraction->denominator, &bn_divisor, get_BN_CTX());
65 BN_clear(&bn_divisor);
66 return result;
69 static struct fraction *init_fraction(struct fraction *fraction)
71 BN_init(&fraction->numerator);
72 BN_init(&fraction->denominator);
73 BN_zero(&fraction->numerator);
74 BN_one(&fraction->denominator);
75 return fraction;
78 static struct fraction *get_one()
80 if (!one) {
81 one = new_zero();
82 BN_one(&one->numerator);
84 return one;
87 static struct fraction *get_zero()
89 if (!zero) {
90 zero = new_zero();
92 return zero;
95 static struct fraction *copy(struct fraction *to, struct fraction *from)
97 BN_copy(&to->numerator, &from->numerator);
98 BN_copy(&to->denominator, &from->denominator);
99 return to;
102 static struct fraction *add(struct fraction *result, struct fraction *left, struct fraction *right)
104 BIGNUM a, b, gcd;
106 BN_init(&a);
107 BN_init(&b);
108 BN_init(&gcd);
110 BN_mul(&a, &left->numerator, &right->denominator, get_BN_CTX());
111 BN_mul(&b, &left->denominator, &right->numerator, get_BN_CTX());
112 BN_mul(&result->denominator, &left->denominator, &right->denominator, get_BN_CTX());
113 BN_add(&result->numerator, &a, &b);
115 BN_gcd(&gcd, &result->denominator, &result->numerator, get_BN_CTX());
116 BN_div(&result->denominator, NULL, &result->denominator, &gcd, get_BN_CTX());
117 BN_div(&result->numerator, NULL, &result->numerator, &gcd, get_BN_CTX());
119 BN_clear(&a);
120 BN_clear(&b);
121 BN_clear(&gcd);
123 return result;
126 static int compare(struct fraction *left, struct fraction *right)
128 BIGNUM a, b;
129 int result;
131 BN_init(&a);
132 BN_init(&b);
134 BN_mul(&a, &left->numerator, &right->denominator, get_BN_CTX());
135 BN_mul(&b, &left->denominator, &right->numerator, get_BN_CTX());
137 result = BN_cmp(&a, &b);
139 BN_clear(&a);
140 BN_clear(&b);
142 return result;
145 struct mass_counter {
146 struct fraction seen;
147 struct fraction pending;
150 static struct mass_counter *new_mass_counter(struct commit *commit, struct fraction *pending)
152 struct mass_counter *mass_counter = xmalloc(sizeof(*mass_counter));
153 memset(mass_counter, 0, sizeof(*mass_counter));
155 init_fraction(&mass_counter->seen);
156 init_fraction(&mass_counter->pending);
158 copy(&mass_counter->pending, pending);
159 copy(&mass_counter->seen, get_zero());
161 if (commit->object.util) {
162 die("multiple attempts to initialize mass counter for %s",
163 sha1_to_hex(commit->object.sha1));
166 commit->object.util = mass_counter;
168 return mass_counter;
171 static void free_mass_counter(struct mass_counter *counter)
173 clear_fraction(&counter->seen);
174 clear_fraction(&counter->pending);
175 free(counter);
179 * Finds the base commit of a list of commits.
181 * One property of the commit being searched for is that every commit reachable
182 * from the base commit is reachable from the commits in the starting list only
183 * via paths that include the base commit.
185 * This algorithm uses a conservation of mass approach to find the base commit.
187 * We start by injecting one unit of mass into the graph at each
188 * of the commits in the starting list. Injecting mass into a commit
189 * is achieved by adding to its pending mass counter and, if it is not already
190 * enqueued, enqueuing the commit in a list of pending commits, in latest
191 * commit date first order.
193 * The algorithm then preceeds to visit each commit in the pending queue.
194 * Upon each visit, the pending mass is added to the mass already seen for that
195 * commit and then divided into N equal portions, where N is the number of
196 * parents of the commit being visited. The divided portions are then injected
197 * into each of the parents.
199 * The algorithm continues until we discover a commit which has seen all the
200 * mass originally injected or until we run out of things to do.
202 * If we find a commit that has seen all the original mass, we have found
203 * the common base of all the commits in the starting list.
205 * The algorithm does _not_ depend on accurate timestamps for correct operation.
206 * However, reasonably sane (e.g. non-random) timestamps are required in order
207 * to prevent an exponential performance characteristic. The occasional
208 * timestamp inaccuracy will not dramatically affect performance but may
209 * result in more nodes being processed than strictly necessary.
211 * This procedure sets *boundary to the address of the base commit. It returns
212 * non-zero if, and only if, there was a problem parsing one of the
213 * commits discovered during the traversal.
215 static int find_base_for_list(struct commit_list *list, struct commit **boundary)
217 int ret = 0;
218 struct commit_list *cleaner = NULL;
219 struct commit_list *pending = NULL;
220 struct fraction injected;
221 init_fraction(&injected);
222 *boundary = NULL;
224 for (; list; list = list->next) {
225 struct commit *item = list->item;
227 if (item->object.util) {
228 die("%s:%d:%s: logic error: this should not have happened - commit %s",
229 __FILE__, __LINE__, __FUNCTION__,
230 sha1_to_hex(item->object.sha1));
233 new_mass_counter(list->item, get_one());
234 add(&injected, &injected, get_one());
236 commit_list_insert(list->item, &cleaner);
237 commit_list_insert(list->item, &pending);
240 while (!*boundary && pending && !ret) {
241 struct commit *latest = pop_commit(&pending);
242 struct mass_counter *latest_node = (struct mass_counter *) latest->object.util;
243 int num_parents;
245 if ((ret = parse_commit(latest)))
246 continue;
247 add(&latest_node->seen, &latest_node->seen, &latest_node->pending);
249 num_parents = count_parents(latest);
250 if (num_parents) {
251 struct fraction distribution;
252 struct commit_list *parents;
254 divide(init_fraction(&distribution), &latest_node->pending, num_parents);
256 for (parents = latest->parents; parents; parents = parents->next) {
257 struct commit *parent = parents->item;
258 struct mass_counter *parent_node = (struct mass_counter *) parent->object.util;
260 if (!parent_node) {
261 parent_node = new_mass_counter(parent, &distribution);
262 insert_by_date(&pending, parent);
263 commit_list_insert(parent, &cleaner);
264 } else {
265 if (!compare(&parent_node->pending, get_zero()))
266 insert_by_date(&pending, parent);
267 add(&parent_node->pending, &parent_node->pending, &distribution);
271 clear_fraction(&distribution);
274 if (!compare(&latest_node->seen, &injected))
275 *boundary = latest;
276 copy(&latest_node->pending, get_zero());
279 while (cleaner) {
280 struct commit *next = pop_commit(&cleaner);
281 free_mass_counter((struct mass_counter *) next->object.util);
282 next->object.util = NULL;
285 if (pending)
286 free_commit_list(pending);
288 clear_fraction(&injected);
289 return ret;
294 * Finds the base of an minimal, non-linear epoch, headed at head, by
295 * applying the find_base_for_list to a list consisting of the parents
297 static int find_base(struct commit *head, struct commit **boundary)
299 int ret = 0;
300 struct commit_list *pending = NULL;
301 struct commit_list *next;
303 for (next = head->parents; next; next = next->next) {
304 commit_list_insert(next->item, &pending);
306 ret = find_base_for_list(pending, boundary);
307 free_commit_list(pending);
309 return ret;
313 * This procedure traverses to the boundary of the first epoch in the epoch
314 * sequence of the epoch headed at head_of_epoch. This is either the end of
315 * the maximal linear epoch or the base of a minimal non-linear epoch.
317 * The queue of pending nodes is sorted in reverse date order and each node
318 * is currently in the queue at most once.
320 static int find_next_epoch_boundary(struct commit *head_of_epoch, struct commit **boundary)
322 int ret;
323 struct commit *item = head_of_epoch;
325 ret = parse_commit(item);
326 if (ret)
327 return ret;
329 if (HAS_EXACTLY_ONE_PARENT(item)) {
331 * We are at the start of a maximimal linear epoch.
332 * Traverse to the end.
334 while (HAS_EXACTLY_ONE_PARENT(item) && !ret) {
335 item = item->parents->item;
336 ret = parse_commit(item);
338 *boundary = item;
340 } else {
342 * Otherwise, we are at the start of a minimal, non-linear
343 * epoch - find the common base of all parents.
345 ret = find_base(item, boundary);
348 return ret;
352 * Returns non-zero if parent is known to be a parent of child.
354 static int is_parent_of(struct commit *parent, struct commit *child)
356 struct commit_list *parents;
357 for (parents = child->parents; parents; parents = parents->next) {
358 if (!memcmp(parent->object.sha1, parents->item->object.sha1,
359 sizeof(parents->item->object.sha1)))
360 return 1;
362 return 0;
366 * Pushes an item onto the merge order stack. If the top of the stack is
367 * marked as being a possible "break", we check to see whether it actually
368 * is a break.
370 static void push_onto_merge_order_stack(struct commit_list **stack, struct commit *item)
372 struct commit_list *top = *stack;
373 if (top && (top->item->object.flags & DISCONTINUITY)) {
374 if (is_parent_of(top->item, item)) {
375 top->item->object.flags &= ~DISCONTINUITY;
378 commit_list_insert(item, stack);
382 * Marks all interesting, visited commits reachable from this commit
383 * as uninteresting. We stop recursing when we reach the epoch boundary,
384 * an unvisited node or a node that has already been marking uninteresting.
386 * This doesn't actually mark all ancestors between the start node and the
387 * epoch boundary uninteresting, but does ensure that they will eventually
388 * be marked uninteresting when the main sort_first_epoch() traversal
389 * eventually reaches them.
391 static void mark_ancestors_uninteresting(struct commit *commit)
393 unsigned int flags = commit->object.flags;
394 int visited = flags & VISITED;
395 int boundary = flags & BOUNDARY;
396 int uninteresting = flags & UNINTERESTING;
397 struct commit_list *next;
399 commit->object.flags |= UNINTERESTING;
402 * We only need to recurse if
403 * we are not on the boundary and
404 * we have not already been marked uninteresting and
405 * we have already been visited.
407 * The main sort_first_epoch traverse will mark unreachable
408 * all uninteresting, unvisited parents as they are visited
409 * so there is no need to duplicate that traversal here.
411 * Similarly, if we are already marked uninteresting
412 * then either all ancestors have already been marked
413 * uninteresting or will be once the sort_first_epoch
414 * traverse reaches them.
417 if (uninteresting || boundary || !visited)
418 return;
420 for (next = commit->parents; next; next = next->next)
421 mark_ancestors_uninteresting(next->item);
425 * Sorts the nodes of the first epoch of the epoch sequence of the epoch headed at head
426 * into merge order.
428 static void sort_first_epoch(struct commit *head, struct commit_list **stack)
430 struct commit_list *parents;
431 struct commit_list *reversed_parents = NULL;
433 head->object.flags |= VISITED;
436 * parse_commit() builds the parent list in reverse order with respect
437 * to the order of the git-commit-tree arguments. So we need to reverse
438 * this list to output the oldest (or most "local") commits last.
440 for (parents = head->parents; parents; parents = parents->next)
441 commit_list_insert(parents->item, &reversed_parents);
444 * TODO: By sorting the parents in a different order, we can alter the
445 * merge order to show contemporaneous changes in parallel branches
446 * occurring after "local" changes. This is useful for a developer
447 * when a developer wants to see all changes that were incorporated
448 * into the same merge as her own changes occur after her own
449 * changes.
452 while (reversed_parents) {
453 struct commit *parent = pop_commit(&reversed_parents);
455 if (head->object.flags & UNINTERESTING) {
457 * Propagates the uninteresting bit to all parents.
458 * if we have already visited this parent, then
459 * the uninteresting bit will be propagated to each
460 * reachable commit that is still not marked
461 * uninteresting and won't otherwise be reached.
463 mark_ancestors_uninteresting(parent);
466 if (!(parent->object.flags & VISITED)) {
467 if (parent->object.flags & BOUNDARY) {
468 if (*stack) {
469 die("something else is on the stack - %s",
470 sha1_to_hex((*stack)->item->object.sha1));
472 push_onto_merge_order_stack(stack, parent);
473 parent->object.flags |= VISITED;
475 } else {
476 sort_first_epoch(parent, stack);
477 if (reversed_parents) {
479 * This indicates a possible
480 * discontinuity it may not be be
481 * actual discontinuity if the head
482 * of parent N happens to be the tail
483 * of parent N+1.
485 * The next push onto the stack will
486 * resolve the question.
488 (*stack)->item->object.flags |= DISCONTINUITY;
494 push_onto_merge_order_stack(stack, head);
498 * Emit the contents of the stack.
500 * The stack is freed and replaced by NULL.
502 * Sets the return value to STOP if no further output should be generated.
504 static int emit_stack(struct commit_list **stack, emitter_func emitter)
506 unsigned int seen = 0;
507 int action = CONTINUE;
509 while (*stack && (action != STOP)) {
510 struct commit *next = pop_commit(stack);
511 seen |= next->object.flags;
512 if (*stack)
513 action = (*emitter) (next);
516 if (*stack) {
517 free_commit_list(*stack);
518 *stack = NULL;
521 return (action == STOP || (seen & UNINTERESTING)) ? STOP : CONTINUE;
525 * Sorts an arbitrary epoch into merge order by sorting each epoch
526 * of its epoch sequence into order.
528 * Note: this algorithm currently leaves traces of its execution in the
529 * object flags of nodes it discovers. This should probably be fixed.
531 static int sort_in_merge_order(struct commit *head_of_epoch, emitter_func emitter)
533 struct commit *next = head_of_epoch;
534 int ret = 0;
535 int action = CONTINUE;
537 ret = parse_commit(head_of_epoch);
539 while (next && next->parents && !ret && (action != STOP)) {
540 struct commit *base = NULL;
542 ret = find_next_epoch_boundary(next, &base);
543 if (ret)
544 return ret;
545 next->object.flags |= BOUNDARY;
546 if (base)
547 base->object.flags |= BOUNDARY;
549 if (HAS_EXACTLY_ONE_PARENT(next)) {
550 while (HAS_EXACTLY_ONE_PARENT(next)
551 && (action != STOP)
552 && !ret) {
553 if (next->object.flags & UNINTERESTING) {
554 action = STOP;
555 } else {
556 action = (*emitter) (next);
558 if (action != STOP) {
559 next = next->parents->item;
560 ret = parse_commit(next);
564 } else {
565 struct commit_list *stack = NULL;
566 sort_first_epoch(next, &stack);
567 action = emit_stack(&stack, emitter);
568 next = base;
572 if (next && (action != STOP) && !ret) {
573 (*emitter) (next);
576 return ret;
580 * Sorts the nodes reachable from a starting list in merge order, we
581 * first find the base for the starting list and then sort all nodes
582 * in this subgraph using the sort_first_epoch algorithm. Once we have
583 * reached the base we can continue sorting using sort_in_merge_order.
585 int sort_list_in_merge_order(struct commit_list *list, emitter_func emitter)
587 struct commit_list *stack = NULL;
588 struct commit *base;
589 int ret = 0;
590 int action = CONTINUE;
591 struct commit_list *reversed = NULL;
593 for (; list; list = list->next) {
594 struct commit *next = list->item;
596 if (!(next->object.flags & UNINTERESTING)) {
597 if (next->object.flags & DUPCHECK) {
598 fprintf(stderr, "%s: duplicate commit %s ignored\n",
599 __FUNCTION__, sha1_to_hex(next->object.sha1));
600 } else {
601 next->object.flags |= DUPCHECK;
602 commit_list_insert(list->item, &reversed);
607 if (!reversed->next) {
609 * If there is only one element in the list, we can sort it
610 * using sort_in_merge_order.
612 base = reversed->item;
613 } else {
615 * Otherwise, we search for the base of the list.
617 ret = find_base_for_list(reversed, &base);
618 if (ret)
619 return ret;
620 if (base)
621 base->object.flags |= BOUNDARY;
623 while (reversed) {
624 sort_first_epoch(pop_commit(&reversed), &stack);
625 if (reversed) {
627 * If we have more commits to push, then the
628 * first push for the next parent may (or may
629 * not) represent a discontinuity with respect
630 * to the parent currently on the top of
631 * the stack.
633 * Mark it for checking here, and check it
634 * with the next push. See sort_first_epoch()
635 * for more details.
637 stack->item->object.flags |= DISCONTINUITY;
641 action = emit_stack(&stack, emitter);
644 if (base && (action != STOP)) {
645 ret = sort_in_merge_order(base, emitter);
648 return ret;