summaryrefslogtreecommitdiff
path: root/commit-reach.c
diff options
context:
space:
mode:
Diffstat (limited to 'commit-reach.c')
-rw-r--r--commit-reach.c201
1 files changed, 167 insertions, 34 deletions
diff --git a/commit-reach.c b/commit-reach.c
index dabc2972e4..a339e41aa4 100644
--- a/commit-reach.c
+++ b/commit-reach.c
@@ -41,8 +41,7 @@ static int compare_commits_by_gen(const void *_a, const void *_b)
static int queue_has_nonstale(struct prio_queue *queue)
{
- int i;
- for (i = 0; i < queue->nr; i++) {
+ for (size_t i = 0; i < queue->nr; i++) {
struct commit *commit = queue->array[i].data;
if (!(commit->object.flags & STALE))
return 1;
@@ -212,12 +211,13 @@ int get_octopus_merge_bases(struct commit_list *in, struct commit_list **result)
}
static int remove_redundant_no_gen(struct repository *r,
- struct commit **array, int cnt)
+ struct commit **array,
+ size_t cnt, size_t *dedup_cnt)
{
struct commit **work;
unsigned char *redundant;
- int *filled_index;
- int i, j, filled;
+ size_t *filled_index;
+ size_t i, j, filled;
CALLOC_ARRAY(work, cnt);
redundant = xcalloc(cnt, 1);
@@ -267,20 +267,22 @@ static int remove_redundant_no_gen(struct repository *r,
for (i = filled = 0; i < cnt; i++)
if (!redundant[i])
array[filled++] = work[i];
+ *dedup_cnt = filled;
free(work);
free(redundant);
free(filled_index);
- return filled;
+ return 0;
}
static int remove_redundant_with_gen(struct repository *r,
- struct commit **array, int cnt)
+ struct commit **array, size_t cnt,
+ size_t *dedup_cnt)
{
- int i, count_non_stale = 0, count_still_independent = cnt;
+ size_t i, count_non_stale = 0, count_still_independent = cnt;
timestamp_t min_generation = GENERATION_NUMBER_INFINITY;
struct commit **walk_start, **sorted;
size_t walk_start_nr = 0, walk_start_alloc = cnt;
- int min_gen_pos = 0;
+ size_t min_gen_pos = 0;
/*
* Sort the input by generation number, ascending. This allows
@@ -326,12 +328,12 @@ static int remove_redundant_with_gen(struct repository *r,
* terminate early. Otherwise, we will do the same amount of work
* as before.
*/
- for (i = walk_start_nr - 1; i >= 0 && count_still_independent > 1; i--) {
+ for (i = walk_start_nr; i && count_still_independent > 1; i--) {
/* push the STALE bits up to min generation */
struct commit_list *stack = NULL;
- commit_list_insert(walk_start[i], &stack);
- walk_start[i]->object.flags |= STALE;
+ commit_list_insert(walk_start[i - 1], &stack);
+ walk_start[i - 1]->object.flags |= STALE;
while (stack) {
struct commit_list *parents;
@@ -388,10 +390,12 @@ static int remove_redundant_with_gen(struct repository *r,
clear_commit_marks_many(walk_start_nr, walk_start, STALE);
free(walk_start);
- return count_non_stale;
+ *dedup_cnt = count_non_stale;
+ return 0;
}
-static int remove_redundant(struct repository *r, struct commit **array, int cnt)
+static int remove_redundant(struct repository *r, struct commit **array,
+ size_t cnt, size_t *dedup_cnt)
{
/*
* Some commit in the array may be an ancestor of
@@ -401,31 +405,30 @@ static int remove_redundant(struct repository *r, struct commit **array, int cnt
* that number.
*/
if (generation_numbers_enabled(r)) {
- int i;
-
/*
* If we have a single commit with finite generation
* number, then the _with_gen algorithm is preferred.
*/
- for (i = 0; i < cnt; i++) {
+ for (size_t i = 0; i < cnt; i++) {
if (commit_graph_generation(array[i]) < GENERATION_NUMBER_INFINITY)
- return remove_redundant_with_gen(r, array, cnt);
+ return remove_redundant_with_gen(r, array, cnt, dedup_cnt);
}
}
- return remove_redundant_no_gen(r, array, cnt);
+ return remove_redundant_no_gen(r, array, cnt, dedup_cnt);
}
static int get_merge_bases_many_0(struct repository *r,
struct commit *one,
- int n,
+ size_t n,
struct commit **twos,
int cleanup,
struct commit_list **result)
{
struct commit_list *list;
struct commit **rslt;
- int cnt, i;
+ size_t cnt, i;
+ int ret;
if (merge_bases_many(r, one, n, twos, result) < 0)
return -1;
@@ -452,8 +455,8 @@ static int get_merge_bases_many_0(struct repository *r,
clear_commit_marks(one, all_flags);
clear_commit_marks_many(n, twos, all_flags);
- cnt = remove_redundant(r, rslt, cnt);
- if (cnt < 0) {
+ ret = remove_redundant(r, rslt, cnt, &cnt);
+ if (ret < 0) {
free(rslt);
return -1;
}
@@ -465,7 +468,7 @@ static int get_merge_bases_many_0(struct repository *r,
int repo_get_merge_bases_many(struct repository *r,
struct commit *one,
- int n,
+ size_t n,
struct commit **twos,
struct commit_list **result)
{
@@ -474,7 +477,7 @@ int repo_get_merge_bases_many(struct repository *r,
int repo_get_merge_bases_many_dirty(struct repository *r,
struct commit *one,
- int n,
+ size_t n,
struct commit **twos,
struct commit_list **result)
{
@@ -582,7 +585,8 @@ struct commit_list *reduce_heads(struct commit_list *heads)
struct commit_list *p;
struct commit_list *result = NULL, **tail = &result;
struct commit **array;
- int num_head, i;
+ size_t num_head, i;
+ int ret;
if (!heads)
return NULL;
@@ -603,11 +607,13 @@ struct commit_list *reduce_heads(struct commit_list *heads)
p->item->object.flags &= ~STALE;
}
}
- num_head = remove_redundant(the_repository, array, num_head);
- if (num_head < 0) {
+
+ ret = remove_redundant(the_repository, array, num_head, &num_head);
+ if (ret < 0) {
free(array);
return NULL;
}
+
for (i = 0; i < num_head; i++)
tail = &commit_list_insert(array[i], tail)->next;
free(array);
@@ -780,12 +786,12 @@ int commit_contains(struct ref_filter *filter, struct commit *commit,
int can_all_from_reach_with_flag(struct object_array *from,
unsigned int with_flag,
unsigned int assign_flag,
- time_t min_commit_date,
+ timestamp_t min_commit_date,
timestamp_t min_generation)
{
struct commit **list = NULL;
- int i;
- int nr_commits;
+ size_t i;
+ size_t nr_commits;
int result = 1;
ALLOC_ARRAY(list, from->nr);
@@ -883,9 +889,9 @@ int can_all_from_reach(struct commit_list *from, struct commit_list *to,
int cutoff_by_min_date)
{
struct object_array from_objs = OBJECT_ARRAY_INIT;
- time_t min_commit_date = cutoff_by_min_date ? from->item->date : 0;
struct commit_list *from_iter = from, *to_iter = to;
int result;
+ timestamp_t min_commit_date = cutoff_by_min_date ? from->item->date : 0;
timestamp_t min_generation = GENERATION_NUMBER_INFINITY;
while (from_iter) {
@@ -937,8 +943,8 @@ int can_all_from_reach(struct commit_list *from, struct commit_list *to,
return result;
}
-struct commit_list *get_reachable_subset(struct commit **from, int nr_from,
- struct commit **to, int nr_to,
+struct commit_list *get_reachable_subset(struct commit **from, size_t nr_from,
+ struct commit **to, size_t nr_to,
unsigned int reachable_flag)
{
struct commit **item;
@@ -1227,4 +1233,131 @@ void tips_reachable_from_bases(struct repository *r,
done:
free(commits);
repo_clear_commit_marks(r, SEEN);
+ free_commit_list(stack);
+}
+
+/*
+ * This slab initializes integers to zero, so use "-1" for "tip is best" and
+ * "i + 1" for "bases[i] is best".
+ */
+define_commit_slab(best_branch_base, int);
+static struct best_branch_base best_branch_base;
+#define get_best(c) (*best_branch_base_at(&best_branch_base, (c)))
+#define set_best(c,v) (*best_branch_base_at(&best_branch_base, (c)) = (v))
+
+int get_branch_base_for_tip(struct repository *r,
+ struct commit *tip,
+ struct commit **bases,
+ size_t bases_nr)
+{
+ int best_index = -1;
+ struct commit *branch_point = NULL;
+ struct prio_queue queue = { compare_commits_by_gen_then_commit_date };
+ int found_missing_gen = 0;
+
+ if (!bases_nr)
+ return -1;
+
+ repo_parse_commit(r, tip);
+ if (commit_graph_generation(tip) == GENERATION_NUMBER_INFINITY)
+ found_missing_gen = 1;
+
+ /* Check for missing generation numbers. */
+ for (size_t i = 0; i < bases_nr; i++) {
+ struct commit *c = bases[i];
+ repo_parse_commit(r, c);
+ if (commit_graph_generation(c) == GENERATION_NUMBER_INFINITY)
+ found_missing_gen = 1;
+ }
+
+ if (found_missing_gen) {
+ struct commit **commits;
+ size_t commits_nr = bases_nr + 1;
+
+ CALLOC_ARRAY(commits, commits_nr);
+ COPY_ARRAY(commits, bases, bases_nr);
+ commits[bases_nr] = tip;
+ ensure_generations_valid(r, commits, commits_nr);
+ free(commits);
+ }
+
+ /* Initialize queue and slab now that generations are guaranteed. */
+ init_best_branch_base(&best_branch_base);
+ set_best(tip, -1);
+ prio_queue_put(&queue, tip);
+
+ for (size_t i = 0; i < bases_nr; i++) {
+ struct commit *c = bases[i];
+ int best = get_best(c);
+
+ /* Has this already been marked as best by another commit? */
+ if (best) {
+ if (best == -1) {
+ /* We agree at this position. Stop now. */
+ best_index = i + 1;
+ goto cleanup;
+ }
+ continue;
+ }
+
+ set_best(c, i + 1);
+ prio_queue_put(&queue, c);
+ }
+
+ while (queue.nr) {
+ struct commit *c = prio_queue_get(&queue);
+ int best_for_c = get_best(c);
+ int best_for_p, positive;
+ struct commit *parent;
+
+ /* Have we reached a known branch point? It's optimal. */
+ if (c == branch_point)
+ break;
+
+ repo_parse_commit(r, c);
+ if (!c->parents)
+ continue;
+
+ parent = c->parents->item;
+ repo_parse_commit(r, parent);
+ best_for_p = get_best(parent);
+
+ if (!best_for_p) {
+ /* 'parent' is new, so pass along best_for_c. */
+ set_best(parent, best_for_c);
+ prio_queue_put(&queue, parent);
+ continue;
+ }
+
+ if (best_for_p > 0 && best_for_c > 0) {
+ /* Collision among bases. Minimize. */
+ if (best_for_c < best_for_p)
+ set_best(parent, best_for_c);
+ continue;
+ }
+
+ /*
+ * At this point, we have reached a commit that is reachable
+ * from the tip, either from 'c' or from an earlier commit to
+ * have 'parent' as its first parent.
+ *
+ * Update 'best_index' to match the minimum of all base indices
+ * to reach 'parent'.
+ */
+
+ /* Exactly one is positive due to initial conditions. */
+ positive = (best_for_c < 0) ? best_for_p : best_for_c;
+
+ if (best_index < 0 || positive < best_index)
+ best_index = positive;
+
+ /* No matter what, track that the parent is reachable from tip. */
+ set_best(parent, -1);
+ branch_point = parent;
+ }
+
+cleanup:
+ clear_best_branch_base(&best_branch_base);
+ clear_prio_queue(&queue);
+ return best_index > 0 ? best_index - 1 : -1;
}