blob: 35c4da4948122a6caea3a1757484b487db16b0fd [file] [log] [blame]
Ramsay Jones0009d352018-10-17 23:13:26 +01001#ifndef COMMIT_REACH_H
2#define COMMIT_REACH_H
Derrick Stolee5227c382018-07-20 16:33:02 +00003
Ramsay Jones14067252018-10-27 02:53:57 +01004#include "commit.h"
Derrick Stolee920f93c2018-07-20 16:33:08 +00005#include "commit-slab.h"
6
Derrick Stolee5227c382018-07-20 16:33:02 +00007struct commit_list;
Derrick Stolee920f93c2018-07-20 16:33:08 +00008struct ref_filter;
Ramsay Jones14067252018-10-27 02:53:57 +01009struct object_id;
10struct object_array;
Derrick Stolee5227c382018-07-20 16:33:02 +000011
Stefan Beller21a96512018-11-13 16:12:55 -080012struct commit_list *repo_get_merge_bases(struct repository *r,
13 struct commit *rev1,
14 struct commit *rev2);
15struct commit_list *repo_get_merge_bases_many(struct repository *r,
16 struct commit *one, int n,
17 struct commit **twos);
Derrick Stolee5227c382018-07-20 16:33:02 +000018/* To be used only when object flags after this call no longer matter */
Stefan Beller21a96512018-11-13 16:12:55 -080019struct commit_list *repo_get_merge_bases_many_dirty(struct repository *r,
20 struct commit *one, int n,
21 struct commit **twos);
Stefan Beller21a96512018-11-13 16:12:55 -080022
Derrick Stolee5227c382018-07-20 16:33:02 +000023struct commit_list *get_octopus_merge_bases(struct commit_list *in);
24
Carlo Marcelo Arenas Belónc1ea6252020-06-23 11:42:22 -070025int repo_is_descendant_of(struct repository *r,
26 struct commit *commit,
27 struct commit_list *with_commit);
Stefan Beller4d5430f2018-11-13 16:12:56 -080028int repo_in_merge_bases(struct repository *r,
29 struct commit *commit,
30 struct commit *reference);
31int repo_in_merge_bases_many(struct repository *r,
32 struct commit *commit,
33 int nr_reference, struct commit **reference);
Derrick Stolee5227c382018-07-20 16:33:02 +000034
Derrick Stolee5227c382018-07-20 16:33:02 +000035/*
36 * Takes a list of commits and returns a new list where those
37 * have been removed that can be reached from other commits in
38 * the list. It is useful for, e.g., reducing the commits
39 * randomly thrown at the git-merge command and removing
40 * redundant commits that the user shouldn't have given to it.
41 *
42 * This function destroys the STALE bit of the commit objects'
43 * flags.
44 */
45struct commit_list *reduce_heads(struct commit_list *heads);
46
47/*
48 * Like `reduce_heads()`, except it replaces the list. Use this
49 * instead of `foo = reduce_heads(foo);` to avoid memory leaks.
50 */
51void reduce_heads_replace(struct commit_list **heads);
52
Derrick Stolee1d614d42018-07-20 16:33:06 +000053int ref_newer(const struct object_id *new_oid, const struct object_id *old_oid);
54
Derrick Stolee920f93c2018-07-20 16:33:08 +000055/*
56 * Unknown has to be "0" here, because that's the default value for
57 * contains_cache slab entries that have not yet been assigned.
58 */
59enum contains_result {
60 CONTAINS_UNKNOWN = 0,
61 CONTAINS_NO,
62 CONTAINS_YES
63};
64
65define_commit_slab(contains_cache, enum contains_result);
66
67int commit_contains(struct ref_filter *filter, struct commit *commit,
68 struct commit_list *list, struct contains_cache *cache);
69
Derrick Stoleeba3ca1e2018-07-20 16:33:13 +000070/*
71 * Determine if every commit in 'from' can reach at least one commit
72 * that is marked with 'with_flag'. As we traverse, use 'assign_flag'
73 * as a marker for commits that are already visited. Do not walk
Derrick Stolee4fbcca42018-07-20 16:33:28 +000074 * commits with date below 'min_commit_date' or generation below
75 * 'min_generation'.
Derrick Stoleeba3ca1e2018-07-20 16:33:13 +000076 */
77int can_all_from_reach_with_flag(struct object_array *from,
78 unsigned int with_flag,
79 unsigned int assign_flag,
Derrick Stolee4fbcca42018-07-20 16:33:28 +000080 time_t min_commit_date,
Abhishek Kumard7f92782021-01-16 18:11:13 +000081 timestamp_t min_generation);
Derrick Stolee1792bc12018-07-20 16:33:23 +000082int can_all_from_reach(struct commit_list *from, struct commit_list *to,
83 int commit_date_cutoff);
Derrick Stoleeba3ca1e2018-07-20 16:33:13 +000084
Derrick Stoleefcb2c072018-11-02 06:14:45 -070085
86/*
87 * Return a list of commits containing the commits in the 'to' array
88 * that are reachable from at least one commit in the 'from' array.
89 * Also add the given 'flag' to each of the commits in the returned list.
90 *
91 * This method uses the PARENT1 and PARENT2 flags during its operation,
92 * so be sure these flags are not set before calling the method.
93 */
94struct commit_list *get_reachable_subset(struct commit **from, int nr_from,
95 struct commit **to, int nr_to,
96 unsigned int reachable_flag);
97
Derrick Stoleefd67d142023-03-20 11:26:53 +000098struct ahead_behind_count {
99 /**
100 * As input, the *_index members indicate which positions in
101 * the 'tips' array correspond to the tip and base of this
102 * comparison.
103 */
104 size_t tip_index;
105 size_t base_index;
106
107 /**
108 * These values store the computed counts for each side of the
109 * symmetric difference:
110 *
111 * 'ahead' stores the number of commits reachable from the tip
112 * and not reachable from the base.
113 *
114 * 'behind' stores the number of commits reachable from the base
115 * and not reachable from the tip.
116 */
117 unsigned int ahead;
118 unsigned int behind;
119};
120
121/*
122 * Given an array of commits and an array of ahead_behind_count pairs,
123 * compute the ahead/behind counts for each pair.
124 */
125void ahead_behind(struct repository *r,
126 struct commit **commits, size_t commits_nr,
127 struct ahead_behind_count *counts, size_t counts_nr);
128
Derrick Stoleecbfe3602023-03-20 11:26:55 +0000129/*
130 * For all tip commits, add 'mark' to their flags if and only if they
131 * are reachable from one of the commits in 'bases'.
132 */
133void tips_reachable_from_bases(struct repository *r,
134 struct commit_list *bases,
135 struct commit **tips, size_t tips_nr,
136 int mark);
137
Derrick Stolee5227c382018-07-20 16:33:02 +0000138#endif