Blame


1 372ccdbb 2018-06-10 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 372ccdbb 2018-06-10 stsp *
4 372ccdbb 2018-06-10 stsp * Permission to use, copy, modify, and distribute this software for any
5 372ccdbb 2018-06-10 stsp * purpose with or without fee is hereby granted, provided that the above
6 372ccdbb 2018-06-10 stsp * copyright notice and this permission notice appear in all copies.
7 372ccdbb 2018-06-10 stsp *
8 372ccdbb 2018-06-10 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 372ccdbb 2018-06-10 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 372ccdbb 2018-06-10 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 372ccdbb 2018-06-10 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 372ccdbb 2018-06-10 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 372ccdbb 2018-06-10 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 372ccdbb 2018-06-10 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 372ccdbb 2018-06-10 stsp */
16 372ccdbb 2018-06-10 stsp
17 372ccdbb 2018-06-10 stsp #include <sys/types.h>
18 372ccdbb 2018-06-10 stsp #include <sys/stat.h>
19 372ccdbb 2018-06-10 stsp #include <sys/queue.h>
20 372ccdbb 2018-06-10 stsp #include <sys/stdint.h>
21 372ccdbb 2018-06-10 stsp
22 56e0773d 2019-11-28 stsp #include <limits.h>
23 372ccdbb 2018-06-10 stsp #include <stdio.h>
24 372ccdbb 2018-06-10 stsp #include <stdlib.h>
25 372ccdbb 2018-06-10 stsp #include <string.h>
26 372ccdbb 2018-06-10 stsp #include <sha1.h>
27 5822e79e 2023-02-23 op #include <sha2.h>
28 372ccdbb 2018-06-10 stsp #include <zlib.h>
29 372ccdbb 2018-06-10 stsp #include <ctype.h>
30 372ccdbb 2018-06-10 stsp
31 372ccdbb 2018-06-10 stsp #include "got_error.h"
32 372ccdbb 2018-06-10 stsp #include "got_object.h"
33 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
34 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
35 324d37e7 2019-05-11 stsp #include "got_path.h"
36 372ccdbb 2018-06-10 stsp
37 372ccdbb 2018-06-10 stsp #include "got_lib_delta.h"
38 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
39 372ccdbb 2018-06-10 stsp #include "got_lib_object.h"
40 372ccdbb 2018-06-10 stsp #include "got_lib_object_idset.h"
41 2afa256d 2024-03-27 stsp #include "got_lib_object_qid.h"
42 372ccdbb 2018-06-10 stsp
43 2afa256d 2024-03-27 stsp #ifndef nitems
44 2afa256d 2024-03-27 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
45 2afa256d 2024-03-27 stsp #endif
46 2afa256d 2024-03-27 stsp
47 372ccdbb 2018-06-10 stsp struct got_commit_graph_node {
48 372ccdbb 2018-06-10 stsp struct got_object_id id;
49 b43fbaa0 2018-06-11 stsp
50 2afa256d 2024-03-27 stsp /* Used for topological sorting. */
51 2afa256d 2024-03-27 stsp struct got_commit_graph_node *parents[2];
52 2afa256d 2024-03-27 stsp struct got_commit_graph_node **more_parents;
53 2afa256d 2024-03-27 stsp int nparents;
54 2afa256d 2024-03-27 stsp int indegree;
55 2afa256d 2024-03-27 stsp
56 ca6e02ac 2020-01-07 stsp /* Used only during iteration. */
57 ca6e02ac 2020-01-07 stsp time_t timestamp;
58 372ccdbb 2018-06-10 stsp TAILQ_ENTRY(got_commit_graph_node) entry;
59 372ccdbb 2018-06-10 stsp };
60 372ccdbb 2018-06-10 stsp
61 9ba79e04 2018-06-11 stsp TAILQ_HEAD(got_commit_graph_iter_list, got_commit_graph_node);
62 9ba79e04 2018-06-11 stsp
63 b565f6f8 2018-07-23 stsp struct got_commit_graph_branch_tip {
64 32777563 2018-11-07 stsp struct got_object_id *commit_id;
65 32777563 2018-11-07 stsp struct got_commit_object *commit;
66 32777563 2018-11-07 stsp struct got_commit_graph_node *new_node;
67 b565f6f8 2018-07-23 stsp };
68 b565f6f8 2018-07-23 stsp
69 372ccdbb 2018-06-10 stsp struct got_commit_graph {
70 372ccdbb 2018-06-10 stsp /* The set of all commits we have traversed. */
71 372ccdbb 2018-06-10 stsp struct got_object_idset *node_ids;
72 372ccdbb 2018-06-10 stsp
73 0ed6ed4c 2018-06-13 stsp int flags;
74 0ed6ed4c 2018-06-13 stsp #define GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL 0x01
75 2afa256d 2024-03-27 stsp #define GOT_COMMIT_GRAPH_TOPOSORT 0x02
76 0ed6ed4c 2018-06-13 stsp
77 372ccdbb 2018-06-10 stsp /*
78 372ccdbb 2018-06-10 stsp * A set of object IDs of known parent commits which we have not yet
79 372ccdbb 2018-06-10 stsp * traversed. Each commit ID in this set represents a branch in commit
80 372ccdbb 2018-06-10 stsp * history: Either the first-parent branch of the head node, or another
81 372ccdbb 2018-06-10 stsp * branch corresponding to a traversed merge commit for which we have
82 372ccdbb 2018-06-10 stsp * not traversed a branch point commit yet.
83 372ccdbb 2018-06-10 stsp *
84 372ccdbb 2018-06-10 stsp * Whenever we add a commit with a matching ID to the graph, we remove
85 372ccdbb 2018-06-10 stsp * its corresponding element from this set, and add new elements for
86 372ccdbb 2018-06-10 stsp * each of that commit's parent commits which were not traversed yet.
87 372ccdbb 2018-06-10 stsp *
88 372ccdbb 2018-06-10 stsp * When API users ask us to fetch more commits, we fetch commits from
89 372ccdbb 2018-06-10 stsp * all currently open branches. This allows API users to process
90 372ccdbb 2018-06-10 stsp * commits in linear order even though the history contains branches.
91 372ccdbb 2018-06-10 stsp */
92 372ccdbb 2018-06-10 stsp struct got_object_idset *open_branches;
93 b565f6f8 2018-07-23 stsp
94 32777563 2018-11-07 stsp /* Array of branch tips for fetch_commits_from_open_branches(). */
95 b565f6f8 2018-07-23 stsp struct got_commit_graph_branch_tip *tips;
96 5e50c36a 2018-11-08 stsp int ntips;
97 372ccdbb 2018-06-10 stsp
98 31cedeaf 2018-09-15 stsp /* Path of tree entry of interest to the API user. */
99 31cedeaf 2018-09-15 stsp char *path;
100 31cedeaf 2018-09-15 stsp
101 94489f7d 2020-01-04 stsp /*
102 94489f7d 2020-01-04 stsp * Nodes which will be passed to the API user next, sorted by
103 2afa256d 2024-03-27 stsp * commit timestamp. Sorted in topological order only if topological
104 2afa256d 2024-03-27 stsp * sorting was requested.
105 94489f7d 2020-01-04 stsp */
106 9ba79e04 2018-06-11 stsp struct got_commit_graph_iter_list iter_list;
107 372ccdbb 2018-06-10 stsp };
108 372ccdbb 2018-06-10 stsp
109 31cedeaf 2018-09-15 stsp static const struct got_error *
110 41fa1437 2018-11-05 stsp detect_changed_path(int *changed, struct got_commit_object *commit,
111 31cedeaf 2018-09-15 stsp struct got_object_id *commit_id, const char *path,
112 31cedeaf 2018-09-15 stsp struct got_repository *repo)
113 31cedeaf 2018-09-15 stsp {
114 31cedeaf 2018-09-15 stsp const struct got_error *err = NULL;
115 41fa1437 2018-11-05 stsp struct got_commit_object *pcommit = NULL;
116 31cedeaf 2018-09-15 stsp struct got_tree_object *tree = NULL, *ptree = NULL;
117 31cedeaf 2018-09-15 stsp struct got_object_qid *pid;
118 31cedeaf 2018-09-15 stsp
119 31cedeaf 2018-09-15 stsp if (got_path_is_root_dir(path)) {
120 31cedeaf 2018-09-15 stsp *changed = 1;
121 31cedeaf 2018-09-15 stsp return NULL;
122 31cedeaf 2018-09-15 stsp }
123 31cedeaf 2018-09-15 stsp
124 31cedeaf 2018-09-15 stsp *changed = 0;
125 31cedeaf 2018-09-15 stsp
126 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(&commit->parent_ids);
127 31cedeaf 2018-09-15 stsp if (pid == NULL) {
128 31cedeaf 2018-09-15 stsp struct got_object_id *obj_id;
129 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&obj_id, repo, commit, path);
130 fd1d2703 2018-11-04 stsp if (err) {
131 d1451975 2018-11-11 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
132 fd1d2703 2018-11-04 stsp err = NULL;
133 fd1d2703 2018-11-04 stsp } else
134 fd1d2703 2018-11-04 stsp *changed = 1; /* The path was created in this commit. */
135 31cedeaf 2018-09-15 stsp free(obj_id);
136 0e9101d5 2018-11-18 stsp return err;
137 0e9101d5 2018-11-18 stsp }
138 7310c1c3 2018-11-18 stsp
139 0e9101d5 2018-11-18 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
140 0e9101d5 2018-11-18 stsp if (err)
141 0e9101d5 2018-11-18 stsp return err;
142 372ccdbb 2018-06-10 stsp
143 d7b5a0e8 2022-04-20 stsp err = got_object_open_as_commit(&pcommit, repo, &pid->id);
144 0e9101d5 2018-11-18 stsp if (err)
145 0e9101d5 2018-11-18 stsp goto done;
146 0e9101d5 2018-11-18 stsp
147 0e9101d5 2018-11-18 stsp err = got_object_open_as_tree(&ptree, repo, pcommit->tree_id);
148 0e9101d5 2018-11-18 stsp if (err)
149 0e9101d5 2018-11-18 stsp goto done;
150 0e9101d5 2018-11-18 stsp
151 81a966c0 2018-11-18 stsp err = got_object_tree_path_changed(changed, tree, ptree, path, repo);
152 31cedeaf 2018-09-15 stsp done:
153 31cedeaf 2018-09-15 stsp if (tree)
154 31cedeaf 2018-09-15 stsp got_object_tree_close(tree);
155 31cedeaf 2018-09-15 stsp if (ptree)
156 31cedeaf 2018-09-15 stsp got_object_tree_close(ptree);
157 31cedeaf 2018-09-15 stsp if (pcommit)
158 41fa1437 2018-11-05 stsp got_object_commit_close(pcommit);
159 31cedeaf 2018-09-15 stsp return err;
160 31cedeaf 2018-09-15 stsp }
161 31cedeaf 2018-09-15 stsp
162 4626e416 2018-06-10 stsp static void
163 9ba79e04 2018-06-11 stsp add_node_to_iter_list(struct got_commit_graph *graph,
164 ca6e02ac 2020-01-07 stsp struct got_commit_graph_node *node, time_t committer_time)
165 372ccdbb 2018-06-10 stsp {
166 4bd3f2bb 2018-06-10 stsp struct got_commit_graph_node *n, *next;
167 1c7a5dcb 2018-09-15 stsp
168 ca6e02ac 2020-01-07 stsp node->timestamp = committer_time;
169 ca6e02ac 2020-01-07 stsp
170 94489f7d 2020-01-04 stsp n = TAILQ_FIRST(&graph->iter_list);
171 bee6b577 2018-09-19 stsp while (n) {
172 bee6b577 2018-09-19 stsp next = TAILQ_NEXT(n, entry);
173 73026088 2018-11-18 stsp if (next && node->timestamp >= next->timestamp) {
174 bee6b577 2018-09-19 stsp TAILQ_INSERT_BEFORE(next, node, entry);
175 bee6b577 2018-09-19 stsp return;
176 fe8df4c2 2018-06-16 stsp }
177 bee6b577 2018-09-19 stsp n = next;
178 fe8df4c2 2018-06-16 stsp }
179 93e45b7c 2018-09-24 stsp TAILQ_INSERT_TAIL(&graph->iter_list, node, entry);
180 0ed6ed4c 2018-06-13 stsp }
181 0ed6ed4c 2018-06-13 stsp
182 0ed6ed4c 2018-06-13 stsp static const struct got_error *
183 ca6e02ac 2020-01-07 stsp add_node(struct got_commit_graph_node **new_node,
184 ca6e02ac 2020-01-07 stsp struct got_commit_graph *graph, struct got_object_id *commit_id,
185 ca6e02ac 2020-01-07 stsp struct got_repository *repo)
186 ca6e02ac 2020-01-07 stsp {
187 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
188 ca6e02ac 2020-01-07 stsp struct got_commit_graph_node *node;
189 ca6e02ac 2020-01-07 stsp
190 ca6e02ac 2020-01-07 stsp *new_node = NULL;
191 ca6e02ac 2020-01-07 stsp
192 ca6e02ac 2020-01-07 stsp node = calloc(1, sizeof(*node));
193 ca6e02ac 2020-01-07 stsp if (node == NULL)
194 ca6e02ac 2020-01-07 stsp return got_error_from_errno("calloc");
195 ca6e02ac 2020-01-07 stsp
196 ca6e02ac 2020-01-07 stsp memcpy(&node->id, commit_id, sizeof(node->id));
197 2afa256d 2024-03-27 stsp node->nparents = -1;
198 2afa256d 2024-03-27 stsp err = got_object_idset_add(graph->node_ids, &node->id, node);
199 ca6e02ac 2020-01-07 stsp if (err)
200 ca6e02ac 2020-01-07 stsp free(node);
201 ca6e02ac 2020-01-07 stsp else
202 ca6e02ac 2020-01-07 stsp *new_node = node;
203 ca6e02ac 2020-01-07 stsp return err;
204 ca6e02ac 2020-01-07 stsp }
205 ca6e02ac 2020-01-07 stsp
206 ca6e02ac 2020-01-07 stsp /*
207 ca6e02ac 2020-01-07 stsp * Ask got-read-pack to traverse first-parent history until a commit is
208 ca6e02ac 2020-01-07 stsp * encountered which modified graph->path, or until the pack file runs
209 ca6e02ac 2020-01-07 stsp * out of relevant commits. This is faster than sending an individual
210 ca6e02ac 2020-01-07 stsp * request for each commit stored in the pack file.
211 ca6e02ac 2020-01-07 stsp */
212 ca6e02ac 2020-01-07 stsp static const struct got_error *
213 ca6e02ac 2020-01-07 stsp packed_first_parent_traversal(int *ncommits_traversed,
214 ca6e02ac 2020-01-07 stsp struct got_commit_graph *graph, struct got_object_id *commit_id,
215 ca6e02ac 2020-01-07 stsp struct got_repository *repo)
216 ca6e02ac 2020-01-07 stsp {
217 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
218 ca6e02ac 2020-01-07 stsp struct got_object_id_queue traversed_commits;
219 ca6e02ac 2020-01-07 stsp struct got_object_qid *qid;
220 ca6e02ac 2020-01-07 stsp
221 dbdddfee 2021-06-23 naddy STAILQ_INIT(&traversed_commits);
222 ca6e02ac 2020-01-07 stsp *ncommits_traversed = 0;
223 ca6e02ac 2020-01-07 stsp
224 ca6e02ac 2020-01-07 stsp err = got_traverse_packed_commits(&traversed_commits,
225 ca6e02ac 2020-01-07 stsp commit_id, graph->path, repo);
226 ca6e02ac 2020-01-07 stsp if (err)
227 ca6e02ac 2020-01-07 stsp return err;
228 ca6e02ac 2020-01-07 stsp
229 ca6e02ac 2020-01-07 stsp /* Add all traversed commits to the graph... */
230 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, &traversed_commits, entry) {
231 d7b5a0e8 2022-04-20 stsp if (got_object_idset_contains(graph->open_branches, &qid->id))
232 ca6e02ac 2020-01-07 stsp continue;
233 d7b5a0e8 2022-04-20 stsp if (got_object_idset_contains(graph->node_ids, &qid->id))
234 ca6e02ac 2020-01-07 stsp continue;
235 ca6e02ac 2020-01-07 stsp
236 ca6e02ac 2020-01-07 stsp (*ncommits_traversed)++;
237 ca6e02ac 2020-01-07 stsp
238 ca6e02ac 2020-01-07 stsp /* ... except the last commit is the new branch tip. */
239 dbdddfee 2021-06-23 naddy if (STAILQ_NEXT(qid, entry) == NULL) {
240 ca6e02ac 2020-01-07 stsp err = got_object_idset_add(graph->open_branches,
241 d7b5a0e8 2022-04-20 stsp &qid->id, NULL);
242 ca6e02ac 2020-01-07 stsp break;
243 ca6e02ac 2020-01-07 stsp }
244 ca6e02ac 2020-01-07 stsp
245 932b646a 2022-09-05 stsp err = got_object_idset_add(graph->node_ids, &qid->id, NULL);
246 ca6e02ac 2020-01-07 stsp if (err)
247 ca6e02ac 2020-01-07 stsp break;
248 ca6e02ac 2020-01-07 stsp }
249 ca6e02ac 2020-01-07 stsp
250 ca6e02ac 2020-01-07 stsp got_object_id_queue_free(&traversed_commits);
251 ca6e02ac 2020-01-07 stsp return err;
252 ca6e02ac 2020-01-07 stsp }
253 ca6e02ac 2020-01-07 stsp
254 ca6e02ac 2020-01-07 stsp static const struct got_error *
255 32c85d2c 2020-01-06 stsp close_branch(struct got_commit_graph *graph, struct got_object_id *commit_id)
256 32c85d2c 2020-01-06 stsp {
257 32c85d2c 2020-01-06 stsp const struct got_error *err;
258 32c85d2c 2020-01-06 stsp
259 32c85d2c 2020-01-06 stsp err = got_object_idset_remove(NULL, graph->open_branches, commit_id);
260 32c85d2c 2020-01-06 stsp if (err && err->code != GOT_ERR_NO_OBJ)
261 32c85d2c 2020-01-06 stsp return err;
262 32c85d2c 2020-01-06 stsp return NULL;
263 32c85d2c 2020-01-06 stsp }
264 32c85d2c 2020-01-06 stsp
265 32c85d2c 2020-01-06 stsp static const struct got_error *
266 14159a7b 2020-01-04 stsp advance_branch(struct got_commit_graph *graph, struct got_object_id *commit_id,
267 14159a7b 2020-01-04 stsp struct got_commit_object *commit, struct got_repository *repo)
268 0ed6ed4c 2018-06-13 stsp {
269 0ed6ed4c 2018-06-13 stsp const struct got_error *err;
270 0ed6ed4c 2018-06-13 stsp struct got_object_qid *qid;
271 c8255edc 2024-02-01 stsp struct got_object_id *merged_id = NULL;
272 0ed6ed4c 2018-06-13 stsp
273 32c85d2c 2020-01-06 stsp err = close_branch(graph, commit_id);
274 32c85d2c 2020-01-06 stsp if (err)
275 32c85d2c 2020-01-06 stsp return err;
276 32c85d2c 2020-01-06 stsp
277 0ed6ed4c 2018-06-13 stsp if (graph->flags & GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL) {
278 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&commit->parent_ids);
279 9b88e78c 2018-11-18 stsp if (qid == NULL ||
280 d7b5a0e8 2022-04-20 stsp got_object_idset_contains(graph->open_branches, &qid->id))
281 0ed6ed4c 2018-06-13 stsp return NULL;
282 ca6e02ac 2020-01-07 stsp /*
283 ca6e02ac 2020-01-07 stsp * The root directory always changes by definition, and when
284 ca6e02ac 2020-01-07 stsp * logging the root we want to traverse consecutive commits
285 ca6e02ac 2020-01-07 stsp * even if they point at the same tree.
286 ca6e02ac 2020-01-07 stsp * But if we are looking for a specific path then we can avoid
287 ca6e02ac 2020-01-07 stsp * fetching packed commits which did not modify the path and
288 ca6e02ac 2020-01-07 stsp * only fetch their IDs. This speeds up 'got blame'.
289 ca6e02ac 2020-01-07 stsp */
290 ca6e02ac 2020-01-07 stsp if (!got_path_is_root_dir(graph->path) &&
291 ca6e02ac 2020-01-07 stsp (commit->flags & GOT_COMMIT_FLAG_PACKED)) {
292 ca6e02ac 2020-01-07 stsp int ncommits = 0;
293 ca6e02ac 2020-01-07 stsp err = packed_first_parent_traversal(&ncommits,
294 d7b5a0e8 2022-04-20 stsp graph, &qid->id, repo);
295 ca6e02ac 2020-01-07 stsp if (err || ncommits > 0)
296 ca6e02ac 2020-01-07 stsp return err;
297 ca6e02ac 2020-01-07 stsp }
298 9b88e78c 2018-11-18 stsp return got_object_idset_add(graph->open_branches,
299 d7b5a0e8 2022-04-20 stsp &qid->id, NULL);
300 bee6b577 2018-09-19 stsp }
301 bee6b577 2018-09-19 stsp
302 bee6b577 2018-09-19 stsp /*
303 bee6b577 2018-09-19 stsp * If we are graphing commits for a specific path, skip branches
304 bee6b577 2018-09-19 stsp * which do not contribute any content to this path.
305 bee6b577 2018-09-19 stsp */
306 7a62478b 2018-11-18 stsp if (commit->nparents > 1 && !got_path_is_root_dir(graph->path)) {
307 c8255edc 2024-02-01 stsp err = got_object_id_by_path(&merged_id, repo, commit, graph->path);
308 c8255edc 2024-02-01 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
309 c8255edc 2024-02-01 stsp return err;
310 c8255edc 2024-02-01 stsp /* The requested path does not exist in this merge commit. */
311 c8255edc 2024-02-01 stsp }
312 c8255edc 2024-02-01 stsp if (commit->nparents > 1 && !got_path_is_root_dir(graph->path) &&
313 c8255edc 2024-02-01 stsp merged_id != NULL) {
314 c8255edc 2024-02-01 stsp struct got_object_id *prev_id = NULL;
315 bee6b577 2018-09-19 stsp int branches_differ = 0;
316 bee6b577 2018-09-19 stsp
317 bee6b577 2018-09-19 stsp
318 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
319 a44927cc 2022-04-07 stsp struct got_object_id *id = NULL;
320 a44927cc 2022-04-07 stsp struct got_commit_object *pcommit = NULL;
321 6dcdad08 2018-11-18 stsp
322 8e291695 2020-01-04 stsp if (got_object_idset_contains(graph->open_branches,
323 d7b5a0e8 2022-04-20 stsp &qid->id))
324 9b88e78c 2018-11-18 stsp continue;
325 bee6b577 2018-09-19 stsp
326 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&pcommit, repo,
327 d7b5a0e8 2022-04-20 stsp &qid->id);
328 a44927cc 2022-04-07 stsp if (err) {
329 a44927cc 2022-04-07 stsp free(merged_id);
330 a44927cc 2022-04-07 stsp free(prev_id);
331 a44927cc 2022-04-07 stsp return err;
332 a44927cc 2022-04-07 stsp }
333 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&id, repo, pcommit,
334 bee6b577 2018-09-19 stsp graph->path);
335 a44927cc 2022-04-07 stsp got_object_commit_close(pcommit);
336 a44927cc 2022-04-07 stsp pcommit = NULL;
337 bee6b577 2018-09-19 stsp if (err) {
338 d1451975 2018-11-11 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY) {
339 bee6b577 2018-09-19 stsp branches_differ = 1;
340 bee6b577 2018-09-19 stsp continue;
341 bee6b577 2018-09-19 stsp }
342 6dcdad08 2018-11-18 stsp free(merged_id);
343 6dcdad08 2018-11-18 stsp free(prev_id);
344 bee6b577 2018-09-19 stsp return err;
345 bee6b577 2018-09-19 stsp }
346 bee6b577 2018-09-19 stsp
347 bee6b577 2018-09-19 stsp if (prev_id) {
348 bee6b577 2018-09-19 stsp if (!branches_differ &&
349 d2c2d781 2018-11-18 stsp got_object_id_cmp(id, prev_id) != 0)
350 bee6b577 2018-09-19 stsp branches_differ = 1;
351 6dcdad08 2018-11-18 stsp free(prev_id);
352 6dcdad08 2018-11-18 stsp }
353 6dcdad08 2018-11-18 stsp prev_id = id;
354 bee6b577 2018-09-19 stsp
355 bee6b577 2018-09-19 stsp /*
356 bee6b577 2018-09-19 stsp * If a branch has created the merged content we can
357 bee6b577 2018-09-19 stsp * skip any other branches.
358 bee6b577 2018-09-19 stsp */
359 bee6b577 2018-09-19 stsp if (got_object_id_cmp(merged_id, id) == 0) {
360 b36429ab 2018-11-05 stsp err = got_object_idset_add(graph->open_branches,
361 d7b5a0e8 2022-04-20 stsp &qid->id, NULL);
362 6dcdad08 2018-11-18 stsp free(merged_id);
363 6dcdad08 2018-11-18 stsp free(id);
364 b36429ab 2018-11-05 stsp return err;
365 bee6b577 2018-09-19 stsp }
366 bee6b577 2018-09-19 stsp }
367 6dcdad08 2018-11-18 stsp
368 6dcdad08 2018-11-18 stsp free(prev_id);
369 6dcdad08 2018-11-18 stsp prev_id = NULL;
370 6dcdad08 2018-11-18 stsp free(merged_id);
371 6dcdad08 2018-11-18 stsp merged_id = NULL;
372 bee6b577 2018-09-19 stsp
373 bee6b577 2018-09-19 stsp /*
374 bee6b577 2018-09-19 stsp * If the path's content is the same on all branches,
375 bee6b577 2018-09-19 stsp * follow the first parent only.
376 bee6b577 2018-09-19 stsp */
377 bee6b577 2018-09-19 stsp if (!branches_differ) {
378 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&commit->parent_ids);
379 bee6b577 2018-09-19 stsp if (qid == NULL)
380 bee6b577 2018-09-19 stsp return NULL;
381 8e291695 2020-01-04 stsp if (got_object_idset_contains(graph->open_branches,
382 d7b5a0e8 2022-04-20 stsp &qid->id))
383 b36429ab 2018-11-05 stsp return NULL;
384 8e291695 2020-01-04 stsp if (got_object_idset_contains(graph->node_ids,
385 d7b5a0e8 2022-04-20 stsp &qid->id))
386 998ff57f 2018-11-18 stsp return NULL; /* parent already traversed */
387 b36429ab 2018-11-05 stsp return got_object_idset_add(graph->open_branches,
388 d7b5a0e8 2022-04-20 stsp &qid->id, NULL);
389 bee6b577 2018-09-19 stsp }
390 0ed6ed4c 2018-06-13 stsp }
391 0ed6ed4c 2018-06-13 stsp
392 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
393 d7b5a0e8 2022-04-20 stsp if (got_object_idset_contains(graph->open_branches, &qid->id))
394 b36429ab 2018-11-05 stsp continue;
395 d7b5a0e8 2022-04-20 stsp if (got_object_idset_contains(graph->node_ids, &qid->id))
396 998ff57f 2018-11-18 stsp continue; /* parent already traversed */
397 d7b5a0e8 2022-04-20 stsp err = got_object_idset_add(graph->open_branches, &qid->id,
398 d7b5a0e8 2022-04-20 stsp NULL);
399 b36429ab 2018-11-05 stsp if (err)
400 0ed6ed4c 2018-06-13 stsp return err;
401 0ed6ed4c 2018-06-13 stsp }
402 0ed6ed4c 2018-06-13 stsp
403 b43fbaa0 2018-06-11 stsp return NULL;
404 de56b2d7 2020-01-04 stsp }
405 372ccdbb 2018-06-10 stsp
406 de56b2d7 2020-01-04 stsp const struct got_error *
407 c4d7a9c4 2018-06-11 stsp got_commit_graph_open(struct got_commit_graph **graph,
408 3d509237 2020-01-04 stsp const char *path, int first_parent_traversal)
409 3d509237 2020-01-04 stsp {
410 22220781 2020-01-04 stsp const struct got_error *err = NULL;
411 3ddcebf3 2020-01-04 stsp
412 3ddcebf3 2020-01-04 stsp *graph = calloc(1, sizeof(**graph));
413 3d509237 2020-01-04 stsp if (*graph == NULL)
414 3ddcebf3 2020-01-04 stsp return got_error_from_errno("calloc");
415 88cdb9c6 2020-01-04 stsp
416 88cdb9c6 2020-01-04 stsp TAILQ_INIT(&(*graph)->iter_list);
417 3ddcebf3 2020-01-04 stsp
418 3ddcebf3 2020-01-04 stsp (*graph)->path = strdup(path);
419 3ddcebf3 2020-01-04 stsp if ((*graph)->path == NULL) {
420 3ddcebf3 2020-01-04 stsp err = got_error_from_errno("strdup");
421 22220781 2020-01-04 stsp goto done;
422 3ddcebf3 2020-01-04 stsp }
423 3ddcebf3 2020-01-04 stsp
424 3ddcebf3 2020-01-04 stsp (*graph)->node_ids = got_object_idset_alloc();
425 3ddcebf3 2020-01-04 stsp if ((*graph)->node_ids == NULL) {
426 3ddcebf3 2020-01-04 stsp err = got_error_from_errno("got_object_idset_alloc");
427 22220781 2020-01-04 stsp goto done;
428 3ddcebf3 2020-01-04 stsp }
429 372ccdbb 2018-06-10 stsp
430 3ddcebf3 2020-01-04 stsp (*graph)->open_branches = got_object_idset_alloc();
431 3ddcebf3 2020-01-04 stsp if ((*graph)->open_branches == NULL) {
432 3ddcebf3 2020-01-04 stsp err = got_error_from_errno("got_object_idset_alloc");
433 22220781 2020-01-04 stsp goto done;
434 3ddcebf3 2020-01-04 stsp }
435 3ddcebf3 2020-01-04 stsp
436 0ed6ed4c 2018-06-13 stsp if (first_parent_traversal)
437 0ed6ed4c 2018-06-13 stsp (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
438 22220781 2020-01-04 stsp done:
439 22220781 2020-01-04 stsp if (err) {
440 22220781 2020-01-04 stsp got_commit_graph_close(*graph);
441 22220781 2020-01-04 stsp *graph = NULL;
442 22220781 2020-01-04 stsp }
443 22220781 2020-01-04 stsp return err;
444 372ccdbb 2018-06-10 stsp }
445 372ccdbb 2018-06-10 stsp
446 32777563 2018-11-07 stsp struct add_branch_tip_arg {
447 b565f6f8 2018-07-23 stsp struct got_commit_graph_branch_tip *tips;
448 b565f6f8 2018-07-23 stsp int ntips;
449 32777563 2018-11-07 stsp struct got_repository *repo;
450 32777563 2018-11-07 stsp struct got_commit_graph *graph;
451 372ccdbb 2018-06-10 stsp };
452 372ccdbb 2018-06-10 stsp
453 cb103d04 2018-11-07 stsp static const struct got_error *
454 32777563 2018-11-07 stsp add_branch_tip(struct got_object_id *commit_id, void *data, void *arg)
455 372ccdbb 2018-06-10 stsp {
456 32777563 2018-11-07 stsp const struct got_error *err;
457 32777563 2018-11-07 stsp struct add_branch_tip_arg *a = arg;
458 32777563 2018-11-07 stsp struct got_commit_graph_node *new_node;
459 32777563 2018-11-07 stsp struct got_commit_object *commit;
460 32777563 2018-11-07 stsp
461 32777563 2018-11-07 stsp err = got_object_open_as_commit(&commit, a->repo, commit_id);
462 32777563 2018-11-07 stsp if (err)
463 32777563 2018-11-07 stsp return err;
464 32777563 2018-11-07 stsp
465 ca6e02ac 2020-01-07 stsp err = add_node(&new_node, a->graph, commit_id, a->repo);
466 c877c437 2022-09-04 op if (err) {
467 c877c437 2022-09-04 op got_object_commit_close(commit);
468 32777563 2018-11-07 stsp return err;
469 c877c437 2022-09-04 op }
470 32777563 2018-11-07 stsp
471 c877c437 2022-09-04 op a->tips[a->ntips].commit_id = &new_node->id;
472 32777563 2018-11-07 stsp a->tips[a->ntips].commit = commit;
473 32777563 2018-11-07 stsp a->tips[a->ntips].new_node = new_node;
474 b565f6f8 2018-07-23 stsp a->ntips++;
475 32777563 2018-11-07 stsp
476 cb103d04 2018-11-07 stsp return NULL;
477 372ccdbb 2018-06-10 stsp }
478 372ccdbb 2018-06-10 stsp
479 1142eae9 2018-06-11 stsp static const struct got_error *
480 57eecd46 2020-01-04 stsp fetch_commits_from_open_branches(struct got_commit_graph *graph,
481 6fb7cd11 2019-08-22 stsp struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
482 372ccdbb 2018-06-10 stsp {
483 372ccdbb 2018-06-10 stsp const struct got_error *err;
484 32777563 2018-11-07 stsp struct add_branch_tip_arg arg;
485 32777563 2018-11-07 stsp int i, ntips;
486 372ccdbb 2018-06-10 stsp
487 32777563 2018-11-07 stsp ntips = got_object_idset_num_elements(graph->open_branches);
488 32777563 2018-11-07 stsp if (ntips == 0)
489 372ccdbb 2018-06-10 stsp return NULL;
490 372ccdbb 2018-06-10 stsp
491 32777563 2018-11-07 stsp /* (Re-)allocate branch tips array if necessary. */
492 32777563 2018-11-07 stsp if (graph->ntips < ntips) {
493 b565f6f8 2018-07-23 stsp struct got_commit_graph_branch_tip *tips;
494 5e50c36a 2018-11-08 stsp tips = recallocarray(graph->tips, graph->ntips, ntips,
495 5e50c36a 2018-11-08 stsp sizeof(*tips));
496 b565f6f8 2018-07-23 stsp if (tips == NULL)
497 638f9024 2019-05-13 stsp return got_error_from_errno("recallocarray");
498 b565f6f8 2018-07-23 stsp graph->tips = tips;
499 32777563 2018-11-07 stsp graph->ntips = ntips;
500 b565f6f8 2018-07-23 stsp }
501 b565f6f8 2018-07-23 stsp arg.tips = graph->tips;
502 32777563 2018-11-07 stsp arg.ntips = 0; /* add_branch_tip() will increment */
503 32777563 2018-11-07 stsp arg.repo = repo;
504 32777563 2018-11-07 stsp arg.graph = graph;
505 32777563 2018-11-07 stsp err = got_object_idset_for_each(graph->open_branches, add_branch_tip,
506 32777563 2018-11-07 stsp &arg);
507 cb103d04 2018-11-07 stsp if (err)
508 32777563 2018-11-07 stsp goto done;
509 372ccdbb 2018-06-10 stsp
510 b565f6f8 2018-07-23 stsp for (i = 0; i < arg.ntips; i++) {
511 372ccdbb 2018-06-10 stsp struct got_object_id *commit_id;
512 41fa1437 2018-11-05 stsp struct got_commit_object *commit;
513 32777563 2018-11-07 stsp struct got_commit_graph_node *new_node;
514 13a851c1 2020-01-04 stsp int changed;
515 372ccdbb 2018-06-10 stsp
516 6fb7cd11 2019-08-22 stsp if (cancel_cb) {
517 6fb7cd11 2019-08-22 stsp err = (*cancel_cb)(cancel_arg);
518 6fb7cd11 2019-08-22 stsp if (err)
519 6fb7cd11 2019-08-22 stsp break;
520 6fb7cd11 2019-08-22 stsp }
521 6fb7cd11 2019-08-22 stsp
522 32777563 2018-11-07 stsp commit_id = arg.tips[i].commit_id;
523 32777563 2018-11-07 stsp commit = arg.tips[i].commit;
524 32777563 2018-11-07 stsp new_node = arg.tips[i].new_node;
525 de56b2d7 2020-01-04 stsp
526 13a851c1 2020-01-04 stsp err = detect_changed_path(&changed, commit, commit_id,
527 13a851c1 2020-01-04 stsp graph->path, repo);
528 13a851c1 2020-01-04 stsp if (err) {
529 13a851c1 2020-01-04 stsp if (err->code != GOT_ERR_NO_OBJ)
530 13a851c1 2020-01-04 stsp break;
531 13a851c1 2020-01-04 stsp /*
532 13a851c1 2020-01-04 stsp * History of the path stops here on the current
533 13a851c1 2020-01-04 stsp * branch. Keep going on other branches.
534 13a851c1 2020-01-04 stsp */
535 32c85d2c 2020-01-06 stsp err = close_branch(graph, commit_id);
536 32c85d2c 2020-01-06 stsp if (err)
537 32c85d2c 2020-01-06 stsp break;
538 ec1904dc 2020-01-04 stsp continue;
539 13a851c1 2020-01-04 stsp }
540 85a99d1e 2022-09-04 op if (changed) {
541 ca6e02ac 2020-01-07 stsp add_node_to_iter_list(graph, new_node,
542 ca6e02ac 2020-01-07 stsp got_object_commit_get_committer_time(commit));
543 85a99d1e 2022-09-04 op arg.tips[i].new_node = NULL;
544 85a99d1e 2022-09-04 op }
545 14159a7b 2020-01-04 stsp err = advance_branch(graph, commit_id, commit, repo);
546 cb352812 2018-07-22 stsp if (err)
547 cb352812 2018-07-22 stsp break;
548 372ccdbb 2018-06-10 stsp }
549 32777563 2018-11-07 stsp done:
550 85a99d1e 2022-09-04 op for (i = 0; i < arg.ntips; i++) {
551 32777563 2018-11-07 stsp got_object_commit_close(arg.tips[i].commit);
552 85a99d1e 2022-09-04 op free(arg.tips[i].new_node);
553 85a99d1e 2022-09-04 op }
554 372ccdbb 2018-06-10 stsp return err;
555 372ccdbb 2018-06-10 stsp }
556 372ccdbb 2018-06-10 stsp
557 372ccdbb 2018-06-10 stsp void
558 372ccdbb 2018-06-10 stsp got_commit_graph_close(struct got_commit_graph *graph)
559 372ccdbb 2018-06-10 stsp {
560 9aae7366 2022-09-04 op struct got_commit_graph_node *node;
561 9aae7366 2022-09-04 op
562 9aae7366 2022-09-04 op while ((node = TAILQ_FIRST(&graph->iter_list))) {
563 9aae7366 2022-09-04 op TAILQ_REMOVE(&graph->iter_list, node, entry);
564 2afa256d 2024-03-27 stsp free(node->more_parents);
565 9aae7366 2022-09-04 op free(node);
566 9aae7366 2022-09-04 op }
567 9aae7366 2022-09-04 op
568 22220781 2020-01-04 stsp if (graph->open_branches)
569 22220781 2020-01-04 stsp got_object_idset_free(graph->open_branches);
570 22220781 2020-01-04 stsp if (graph->node_ids)
571 22220781 2020-01-04 stsp got_object_idset_free(graph->node_ids);
572 b565f6f8 2018-07-23 stsp free(graph->tips);
573 31cedeaf 2018-09-15 stsp free(graph->path);
574 372ccdbb 2018-06-10 stsp free(graph);
575 372ccdbb 2018-06-10 stsp }
576 372ccdbb 2018-06-10 stsp
577 78e82c8a 2024-03-26 stsp static const struct got_error *
578 78e82c8a 2024-03-26 stsp remove_branch_tip(struct got_object_id *commit_id, void *data, void *arg)
579 78e82c8a 2024-03-26 stsp {
580 78e82c8a 2024-03-26 stsp struct got_object_idset *open_branches = arg;
581 78e82c8a 2024-03-26 stsp
582 78e82c8a 2024-03-26 stsp return got_object_idset_remove(NULL, open_branches, commit_id);
583 78e82c8a 2024-03-26 stsp }
584 78e82c8a 2024-03-26 stsp
585 372ccdbb 2018-06-10 stsp const struct got_error *
586 98297eed 2024-03-27 stsp got_commit_graph_bfsort(struct got_commit_graph *graph,
587 6fb7cd11 2019-08-22 stsp struct got_object_id *id, struct got_repository *repo,
588 6fb7cd11 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
589 372ccdbb 2018-06-10 stsp {
590 31cedeaf 2018-09-15 stsp const struct got_error *err = NULL;
591 78e82c8a 2024-03-26 stsp struct got_commit_graph_node *node;
592 2afa256d 2024-03-27 stsp
593 2afa256d 2024-03-27 stsp graph->flags &= ~GOT_COMMIT_GRAPH_TOPOSORT;
594 372ccdbb 2018-06-10 stsp
595 78e82c8a 2024-03-26 stsp /* Clear left-over state from previous iteration attempts. */
596 78e82c8a 2024-03-26 stsp while ((node = TAILQ_FIRST(&graph->iter_list)))
597 78e82c8a 2024-03-26 stsp TAILQ_REMOVE(&graph->iter_list, node, entry);
598 78e82c8a 2024-03-26 stsp err = got_object_idset_for_each(graph->open_branches,
599 78e82c8a 2024-03-26 stsp remove_branch_tip, graph->open_branches);
600 78e82c8a 2024-03-26 stsp if (err)
601 78e82c8a 2024-03-26 stsp return err;
602 31cedeaf 2018-09-15 stsp
603 3ff3126d 2020-01-04 stsp err = got_object_idset_add(graph->open_branches, id, NULL);
604 3d509237 2020-01-04 stsp if (err)
605 7e33c8c5 2020-01-04 stsp return err;
606 3d509237 2020-01-04 stsp
607 3ff3126d 2020-01-04 stsp /* Locate first commit which changed graph->path. */
608 94489f7d 2020-01-04 stsp while (TAILQ_EMPTY(&graph->iter_list) &&
609 3ff3126d 2020-01-04 stsp got_object_idset_num_elements(graph->open_branches) > 0) {
610 3ff3126d 2020-01-04 stsp err = fetch_commits_from_open_branches(graph, repo,
611 3ff3126d 2020-01-04 stsp cancel_cb, cancel_arg);
612 3ff3126d 2020-01-04 stsp if (err)
613 7e33c8c5 2020-01-04 stsp return err;
614 5175b31a 2020-01-04 stsp }
615 5175b31a 2020-01-04 stsp
616 94489f7d 2020-01-04 stsp if (TAILQ_EMPTY(&graph->iter_list)) {
617 5175b31a 2020-01-04 stsp const char *path;
618 5175b31a 2020-01-04 stsp if (got_path_is_root_dir(graph->path))
619 5175b31a 2020-01-04 stsp return got_error_no_obj(id);
620 5175b31a 2020-01-04 stsp path = graph->path;
621 5175b31a 2020-01-04 stsp while (path[0] == '/')
622 5175b31a 2020-01-04 stsp path++;
623 5175b31a 2020-01-04 stsp return got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
624 31cedeaf 2018-09-15 stsp }
625 7e33c8c5 2020-01-04 stsp
626 7e33c8c5 2020-01-04 stsp return NULL;
627 372ccdbb 2018-06-10 stsp }
628 372ccdbb 2018-06-10 stsp
629 372ccdbb 2018-06-10 stsp const struct got_error *
630 d9787ed8 2022-09-10 op got_commit_graph_iter_next(struct got_object_id *id,
631 ee780d5c 2020-01-04 stsp struct got_commit_graph *graph, struct got_repository *repo,
632 ee780d5c 2020-01-04 stsp got_cancel_cb cancel_cb, void *cancel_arg)
633 372ccdbb 2018-06-10 stsp {
634 ee780d5c 2020-01-04 stsp const struct got_error *err = NULL;
635 2afa256d 2024-03-27 stsp struct got_commit_graph_node *node, *pnode;
636 2afa256d 2024-03-27 stsp int i;
637 372ccdbb 2018-06-10 stsp
638 df8cd9c6 2020-01-05 stsp node = TAILQ_FIRST(&graph->iter_list);
639 df8cd9c6 2020-01-05 stsp if (node == NULL) {
640 2c7f8870 2018-09-19 stsp /* We are done iterating, or iteration was not started. */
641 9ba79e04 2018-06-11 stsp return got_error(GOT_ERR_ITER_COMPLETED);
642 9ba79e04 2018-06-11 stsp }
643 9ba79e04 2018-06-11 stsp
644 2afa256d 2024-03-27 stsp if (graph->flags & GOT_COMMIT_GRAPH_TOPOSORT) {
645 2afa256d 2024-03-27 stsp /* At least one node with in-degree zero must exist. */
646 2afa256d 2024-03-27 stsp while (node->indegree != 0)
647 2afa256d 2024-03-27 stsp node = TAILQ_NEXT(node, entry);
648 2afa256d 2024-03-27 stsp } else {
649 2afa256d 2024-03-27 stsp while (TAILQ_NEXT(node, entry) == NULL &&
650 2afa256d 2024-03-27 stsp got_object_idset_num_elements(graph->open_branches) > 0) {
651 2afa256d 2024-03-27 stsp err = fetch_commits_from_open_branches(graph, repo,
652 2afa256d 2024-03-27 stsp cancel_cb, cancel_arg);
653 2afa256d 2024-03-27 stsp if (err)
654 2afa256d 2024-03-27 stsp return err;
655 2afa256d 2024-03-27 stsp }
656 ee780d5c 2020-01-04 stsp }
657 372ccdbb 2018-06-10 stsp
658 d9787ed8 2022-09-10 op memcpy(id, &node->id, sizeof(*id));
659 e15c42de 2022-09-05 op
660 94489f7d 2020-01-04 stsp TAILQ_REMOVE(&graph->iter_list, node, entry);
661 2afa256d 2024-03-27 stsp if (graph->flags & GOT_COMMIT_GRAPH_TOPOSORT) {
662 2afa256d 2024-03-27 stsp /* When visiting a commit decrement in-degree of all parents. */
663 2afa256d 2024-03-27 stsp for (i = 0; i < node->nparents; i++) {
664 2afa256d 2024-03-27 stsp if (i < nitems(node->parents))
665 2afa256d 2024-03-27 stsp pnode = node->parents[i];
666 2afa256d 2024-03-27 stsp else
667 2afa256d 2024-03-27 stsp pnode = node->more_parents[i];
668 2afa256d 2024-03-27 stsp pnode->indegree--;
669 2afa256d 2024-03-27 stsp }
670 2afa256d 2024-03-27 stsp }
671 e15c42de 2022-09-05 op free(node);
672 372ccdbb 2018-06-10 stsp return NULL;
673 d9787ed8 2022-09-10 op }
674 d9787ed8 2022-09-10 op
675 d9787ed8 2022-09-10 op static const struct got_error *
676 d9787ed8 2022-09-10 op find_yca_add_id(struct got_object_id **yca_id, struct got_commit_graph *graph,
677 d9787ed8 2022-09-10 op struct got_object_idset *commit_ids, struct got_repository *repo,
678 d9787ed8 2022-09-10 op got_cancel_cb cancel_cb, void *cancel_arg)
679 d9787ed8 2022-09-10 op {
680 d9787ed8 2022-09-10 op const struct got_error *err = NULL;
681 d9787ed8 2022-09-10 op struct got_object_id id;
682 d9787ed8 2022-09-10 op
683 d9787ed8 2022-09-10 op err = got_commit_graph_iter_next(&id, graph, repo, cancel_cb,
684 d9787ed8 2022-09-10 op cancel_arg);
685 d9787ed8 2022-09-10 op if (err)
686 d9787ed8 2022-09-10 op return err;
687 d9787ed8 2022-09-10 op
688 d9787ed8 2022-09-10 op if (got_object_idset_contains(commit_ids, &id)) {
689 d9787ed8 2022-09-10 op *yca_id = got_object_id_dup(&id);
690 d9787ed8 2022-09-10 op if (*yca_id == NULL)
691 d9787ed8 2022-09-10 op err = got_error_from_errno("got_object_id_dup");
692 d9787ed8 2022-09-10 op return err;
693 d9787ed8 2022-09-10 op }
694 d9787ed8 2022-09-10 op
695 d9787ed8 2022-09-10 op return got_object_idset_add(commit_ids, &id, NULL);
696 372ccdbb 2018-06-10 stsp }
697 a9833bc9 2019-05-13 stsp
698 ad575c3a 2023-05-25 stsp /*
699 ad575c3a 2023-05-25 stsp * Sets *yca_id to the youngest common ancestor of commit_id and
700 ad575c3a 2023-05-25 stsp * commit_id2. Returns got_error(GOT_ERR_ANCESTRY) if they have no
701 ad575c3a 2023-05-25 stsp * common ancestors.
702 ad575c3a 2023-05-25 stsp *
703 ad575c3a 2023-05-25 stsp * If first_parent_traversal is nonzero, only linear history is considered.
704 e12cc036 2024-03-27 stsp * If toposort is set then sort commits in topological order before
705 e12cc036 2024-03-27 stsp * traversing them.
706 ad575c3a 2023-05-25 stsp */
707 a9833bc9 2019-05-13 stsp const struct got_error *
708 a9833bc9 2019-05-13 stsp got_commit_graph_find_youngest_common_ancestor(struct got_object_id **yca_id,
709 a9833bc9 2019-05-13 stsp struct got_object_id *commit_id, struct got_object_id *commit_id2,
710 e12cc036 2024-03-27 stsp int first_parent_traversal, int toposort, struct got_repository *repo,
711 e12cc036 2024-03-27 stsp got_cancel_cb cancel_cb, void *cancel_arg)
712 a9833bc9 2019-05-13 stsp {
713 a9833bc9 2019-05-13 stsp const struct got_error *err = NULL;
714 a9833bc9 2019-05-13 stsp struct got_commit_graph *graph = NULL, *graph2 = NULL;
715 a9833bc9 2019-05-13 stsp int completed = 0, completed2 = 0;
716 a9833bc9 2019-05-13 stsp struct got_object_idset *commit_ids;
717 a9833bc9 2019-05-13 stsp
718 a9833bc9 2019-05-13 stsp *yca_id = NULL;
719 a9833bc9 2019-05-13 stsp
720 a9833bc9 2019-05-13 stsp commit_ids = got_object_idset_alloc();
721 a9833bc9 2019-05-13 stsp if (commit_ids == NULL)
722 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_idset_alloc");
723 a9833bc9 2019-05-13 stsp
724 4e91ef15 2021-09-26 stsp err = got_commit_graph_open(&graph, "/", first_parent_traversal);
725 a9833bc9 2019-05-13 stsp if (err)
726 a9833bc9 2019-05-13 stsp goto done;
727 a9833bc9 2019-05-13 stsp
728 4e91ef15 2021-09-26 stsp err = got_commit_graph_open(&graph2, "/", first_parent_traversal);
729 a9833bc9 2019-05-13 stsp if (err)
730 a9833bc9 2019-05-13 stsp goto done;
731 a9833bc9 2019-05-13 stsp
732 e12cc036 2024-03-27 stsp if (toposort) {
733 e12cc036 2024-03-27 stsp err = got_commit_graph_toposort(graph, commit_id, repo,
734 e12cc036 2024-03-27 stsp cancel_cb, cancel_arg);
735 e12cc036 2024-03-27 stsp if (err)
736 e12cc036 2024-03-27 stsp goto done;
737 a9833bc9 2019-05-13 stsp
738 e12cc036 2024-03-27 stsp err = got_commit_graph_toposort(graph2, commit_id2, repo,
739 e12cc036 2024-03-27 stsp cancel_cb, cancel_arg);
740 e12cc036 2024-03-27 stsp if (err)
741 e12cc036 2024-03-27 stsp goto done;
742 e12cc036 2024-03-27 stsp } else {
743 98297eed 2024-03-27 stsp err = got_commit_graph_bfsort(graph, commit_id, repo,
744 e12cc036 2024-03-27 stsp cancel_cb, cancel_arg);
745 e12cc036 2024-03-27 stsp if (err)
746 e12cc036 2024-03-27 stsp goto done;
747 a9833bc9 2019-05-13 stsp
748 98297eed 2024-03-27 stsp err = got_commit_graph_bfsort(graph2, commit_id2, repo,
749 e12cc036 2024-03-27 stsp cancel_cb, cancel_arg);
750 e12cc036 2024-03-27 stsp if (err)
751 e12cc036 2024-03-27 stsp goto done;
752 e12cc036 2024-03-27 stsp }
753 e12cc036 2024-03-27 stsp
754 a9833bc9 2019-05-13 stsp for (;;) {
755 6fb7cd11 2019-08-22 stsp if (cancel_cb) {
756 6fb7cd11 2019-08-22 stsp err = (*cancel_cb)(cancel_arg);
757 6fb7cd11 2019-08-22 stsp if (err)
758 6fb7cd11 2019-08-22 stsp break;
759 6fb7cd11 2019-08-22 stsp }
760 6fb7cd11 2019-08-22 stsp
761 a9833bc9 2019-05-13 stsp if (!completed) {
762 d9787ed8 2022-09-10 op err = find_yca_add_id(yca_id, graph, commit_ids, repo,
763 ee780d5c 2020-01-04 stsp cancel_cb, cancel_arg);
764 a9833bc9 2019-05-13 stsp if (err) {
765 ee780d5c 2020-01-04 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
766 a9833bc9 2019-05-13 stsp break;
767 ee780d5c 2020-01-04 stsp err = NULL;
768 ee780d5c 2020-01-04 stsp completed = 1;
769 a9833bc9 2019-05-13 stsp }
770 d9787ed8 2022-09-10 op if (*yca_id)
771 d9787ed8 2022-09-10 op break;
772 a9833bc9 2019-05-13 stsp }
773 a9833bc9 2019-05-13 stsp
774 a9833bc9 2019-05-13 stsp if (!completed2) {
775 d9787ed8 2022-09-10 op err = find_yca_add_id(yca_id, graph2, commit_ids, repo,
776 ee780d5c 2020-01-04 stsp cancel_cb, cancel_arg);
777 a9833bc9 2019-05-13 stsp if (err) {
778 ee780d5c 2020-01-04 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
779 a9833bc9 2019-05-13 stsp break;
780 ee780d5c 2020-01-04 stsp err = NULL;
781 ee780d5c 2020-01-04 stsp completed2 = 1;
782 a9833bc9 2019-05-13 stsp }
783 d9787ed8 2022-09-10 op if (*yca_id)
784 a9833bc9 2019-05-13 stsp break;
785 a9833bc9 2019-05-13 stsp }
786 a9833bc9 2019-05-13 stsp
787 a9833bc9 2019-05-13 stsp if (completed && completed2) {
788 a9833bc9 2019-05-13 stsp err = got_error(GOT_ERR_ANCESTRY);
789 a9833bc9 2019-05-13 stsp break;
790 a9833bc9 2019-05-13 stsp }
791 a9833bc9 2019-05-13 stsp }
792 a9833bc9 2019-05-13 stsp done:
793 a9833bc9 2019-05-13 stsp got_object_idset_free(commit_ids);
794 a9833bc9 2019-05-13 stsp if (graph)
795 a9833bc9 2019-05-13 stsp got_commit_graph_close(graph);
796 a9833bc9 2019-05-13 stsp if (graph2)
797 a9833bc9 2019-05-13 stsp got_commit_graph_close(graph2);
798 2afa256d 2024-03-27 stsp return err;
799 2afa256d 2024-03-27 stsp }
800 2afa256d 2024-03-27 stsp
801 2afa256d 2024-03-27 stsp /*
802 2afa256d 2024-03-27 stsp * Sort the graph for traversal in topological order.
803 2afa256d 2024-03-27 stsp *
804 2afa256d 2024-03-27 stsp * This implementation is based on the description of topological sorting
805 2afa256d 2024-03-27 stsp * of git commits by Derrick Stolee at
806 2afa256d 2024-03-27 stsp * https://github.blog/2022-08-30-gits-database-internals-ii-commit-history-queries/#topological-sorting
807 2afa256d 2024-03-27 stsp * which reads as follows:
808 2afa256d 2024-03-27 stsp *
809 2afa256d 2024-03-27 stsp * The basic algorithm for topological sorting is Kahn’s algorithm which
810 2afa256d 2024-03-27 stsp * follows two big steps:
811 2afa256d 2024-03-27 stsp * 1. Walk all reachable commits, counting the number of times a commit appears
812 2afa256d 2024-03-27 stsp * as a parent of another commit. Call these numbers the in-degree of the
813 2afa256d 2024-03-27 stsp * commit, referencing the number of incoming edges.
814 2afa256d 2024-03-27 stsp * 2. Walk the reachable commits, but only visit a commit if its in-degree
815 2afa256d 2024-03-27 stsp * value is zero. When visiting a commit, decrement the in-degree value of
816 2afa256d 2024-03-27 stsp * each parent.
817 2afa256d 2024-03-27 stsp *
818 2afa256d 2024-03-27 stsp * This algorithm works because at least one of our starting points will
819 2afa256d 2024-03-27 stsp * have in-degree zero, and then decrementing the in-degree value is similar
820 2afa256d 2024-03-27 stsp * to deleting the commit from the graph, always having at least one commit
821 2afa256d 2024-03-27 stsp * with in-degree zero.
822 2afa256d 2024-03-27 stsp */
823 2afa256d 2024-03-27 stsp const struct got_error *
824 2afa256d 2024-03-27 stsp got_commit_graph_toposort(struct got_commit_graph *graph,
825 2afa256d 2024-03-27 stsp struct got_object_id *id, struct got_repository *repo,
826 2afa256d 2024-03-27 stsp got_cancel_cb cancel_cb, void *cancel_arg)
827 2afa256d 2024-03-27 stsp {
828 2afa256d 2024-03-27 stsp const struct got_error *err = NULL;
829 2afa256d 2024-03-27 stsp struct got_commit_graph_node *node = NULL, *pnode = NULL;
830 2afa256d 2024-03-27 stsp struct got_commit_object *commit = NULL;
831 2afa256d 2024-03-27 stsp struct got_object_id_queue commits;
832 2afa256d 2024-03-27 stsp const struct got_object_id_queue *parent_ids;
833 2afa256d 2024-03-27 stsp struct got_object_qid *qid = NULL, *pid;
834 2afa256d 2024-03-27 stsp int i;
835 2afa256d 2024-03-27 stsp
836 2afa256d 2024-03-27 stsp STAILQ_INIT(&commits);
837 2afa256d 2024-03-27 stsp
838 2afa256d 2024-03-27 stsp if (graph->flags & GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL)
839 98297eed 2024-03-27 stsp return got_commit_graph_bfsort(graph, id, repo,
840 2afa256d 2024-03-27 stsp cancel_cb, cancel_arg);
841 2afa256d 2024-03-27 stsp
842 2afa256d 2024-03-27 stsp /* Clear left-over state from previous iteration attempts. */
843 2afa256d 2024-03-27 stsp while ((node = TAILQ_FIRST(&graph->iter_list)))
844 2afa256d 2024-03-27 stsp TAILQ_REMOVE(&graph->iter_list, node, entry);
845 2afa256d 2024-03-27 stsp err = got_object_idset_for_each(graph->open_branches,
846 2afa256d 2024-03-27 stsp remove_branch_tip, graph->open_branches);
847 2afa256d 2024-03-27 stsp if (err)
848 2afa256d 2024-03-27 stsp return err;
849 2afa256d 2024-03-27 stsp
850 2afa256d 2024-03-27 stsp graph->flags |= GOT_COMMIT_GRAPH_TOPOSORT;
851 2afa256d 2024-03-27 stsp
852 2afa256d 2024-03-27 stsp /*
853 2afa256d 2024-03-27 stsp * Sorting the commit graph in topological order requires visiting
854 2afa256d 2024-03-27 stsp * every reachable commit. This is very expensive but there are
855 2afa256d 2024-03-27 stsp * ways to speed this up significantly in the future:
856 2afa256d 2024-03-27 stsp * 1) Run this loop in got-read-pack if possible.
857 2afa256d 2024-03-27 stsp * 2) Use Git's commit-graph file to compute the result incrementally.
858 2afa256d 2024-03-27 stsp * See the blog post linked above for details.
859 2afa256d 2024-03-27 stsp */
860 2afa256d 2024-03-27 stsp err = got_object_qid_alloc_partial(&qid);
861 2afa256d 2024-03-27 stsp if (err)
862 2afa256d 2024-03-27 stsp return err;
863 2afa256d 2024-03-27 stsp memcpy(&qid->id, id, sizeof(qid->id));
864 2afa256d 2024-03-27 stsp STAILQ_INSERT_TAIL(&commits, qid, entry);
865 2afa256d 2024-03-27 stsp while (!STAILQ_EMPTY(&commits)) {
866 2afa256d 2024-03-27 stsp if (cancel_cb) {
867 2afa256d 2024-03-27 stsp err = (*cancel_cb)(cancel_arg);
868 2afa256d 2024-03-27 stsp if (err)
869 2afa256d 2024-03-27 stsp break;
870 2afa256d 2024-03-27 stsp }
871 2afa256d 2024-03-27 stsp
872 2afa256d 2024-03-27 stsp qid = STAILQ_FIRST(&commits);
873 2afa256d 2024-03-27 stsp STAILQ_REMOVE_HEAD(&commits, entry);
874 2afa256d 2024-03-27 stsp err = got_object_open_as_commit(&commit, repo, &qid->id);
875 2afa256d 2024-03-27 stsp if (err)
876 2afa256d 2024-03-27 stsp break;
877 2afa256d 2024-03-27 stsp
878 2afa256d 2024-03-27 stsp node = got_object_idset_get(graph->node_ids, &qid->id);
879 2afa256d 2024-03-27 stsp if (node == NULL) {
880 2afa256d 2024-03-27 stsp err = add_node(&node, graph, id, repo);
881 2afa256d 2024-03-27 stsp if (err)
882 2afa256d 2024-03-27 stsp break;
883 2afa256d 2024-03-27 stsp TAILQ_INSERT_TAIL(&graph->iter_list, node, entry);
884 2afa256d 2024-03-27 stsp }
885 2afa256d 2024-03-27 stsp
886 2afa256d 2024-03-27 stsp got_object_qid_free(qid);
887 2afa256d 2024-03-27 stsp qid = NULL;
888 2afa256d 2024-03-27 stsp
889 2afa256d 2024-03-27 stsp if (node->timestamp != 0) /* already traversed once */
890 2afa256d 2024-03-27 stsp continue;
891 2afa256d 2024-03-27 stsp
892 2afa256d 2024-03-27 stsp if (node->nparents == -1) {
893 2afa256d 2024-03-27 stsp node->nparents = got_object_commit_get_nparents(commit);
894 2afa256d 2024-03-27 stsp if (node->nparents > nitems(node->parents)) {
895 2afa256d 2024-03-27 stsp node->more_parents = calloc(node->nparents,
896 2afa256d 2024-03-27 stsp sizeof(*node->more_parents));
897 2afa256d 2024-03-27 stsp if (node->more_parents == NULL) {
898 2afa256d 2024-03-27 stsp err = got_error_from_errno("calloc");
899 2afa256d 2024-03-27 stsp break;
900 2afa256d 2024-03-27 stsp }
901 2afa256d 2024-03-27 stsp }
902 2afa256d 2024-03-27 stsp
903 2afa256d 2024-03-27 stsp }
904 2afa256d 2024-03-27 stsp
905 2afa256d 2024-03-27 stsp node->timestamp = got_object_commit_get_committer_time(commit);
906 2afa256d 2024-03-27 stsp parent_ids = got_object_commit_get_parent_ids(commit);
907 2afa256d 2024-03-27 stsp i = 0;
908 2afa256d 2024-03-27 stsp STAILQ_FOREACH(pid, parent_ids, entry) {
909 2afa256d 2024-03-27 stsp if (cancel_cb) {
910 2afa256d 2024-03-27 stsp err = (*cancel_cb)(cancel_arg);
911 2afa256d 2024-03-27 stsp if (err)
912 2afa256d 2024-03-27 stsp goto done;
913 2afa256d 2024-03-27 stsp }
914 2afa256d 2024-03-27 stsp
915 2afa256d 2024-03-27 stsp /*
916 2afa256d 2024-03-27 stsp * Increment the in-degree counter every time a given
917 2afa256d 2024-03-27 stsp * commit appears as the parent of another commit.
918 2afa256d 2024-03-27 stsp */
919 2afa256d 2024-03-27 stsp pnode = got_object_idset_get(graph->node_ids, &pid->id);
920 2afa256d 2024-03-27 stsp if (pnode == NULL) {
921 2afa256d 2024-03-27 stsp err = add_node(&pnode, graph, &pid->id, repo);
922 2afa256d 2024-03-27 stsp if (err)
923 2afa256d 2024-03-27 stsp goto done;
924 2afa256d 2024-03-27 stsp TAILQ_INSERT_TAIL(&graph->iter_list, pnode,
925 2afa256d 2024-03-27 stsp entry);
926 2afa256d 2024-03-27 stsp }
927 2afa256d 2024-03-27 stsp pnode->indegree++;
928 2afa256d 2024-03-27 stsp
929 2afa256d 2024-03-27 stsp /*
930 2afa256d 2024-03-27 stsp * Cache parent pointers on the node to make future
931 2afa256d 2024-03-27 stsp * in-degree updates easier.
932 2afa256d 2024-03-27 stsp */
933 2afa256d 2024-03-27 stsp if (node->nparents <= nitems(node->parents)) {
934 2afa256d 2024-03-27 stsp node->parents[i] = pnode;
935 2afa256d 2024-03-27 stsp } else {
936 2afa256d 2024-03-27 stsp node->more_parents[i] = pnode;
937 2afa256d 2024-03-27 stsp if (i < nitems(node->parents))
938 2afa256d 2024-03-27 stsp node->parents[i] = pnode;
939 2afa256d 2024-03-27 stsp }
940 2afa256d 2024-03-27 stsp i++;
941 2afa256d 2024-03-27 stsp
942 2afa256d 2024-03-27 stsp /* Keep traversing through all parent commits. */
943 2afa256d 2024-03-27 stsp err = got_object_qid_alloc_partial(&qid);
944 2afa256d 2024-03-27 stsp if (err)
945 2afa256d 2024-03-27 stsp goto done;
946 2afa256d 2024-03-27 stsp memcpy(&qid->id, &pid->id, sizeof(qid->id));
947 2afa256d 2024-03-27 stsp STAILQ_INSERT_TAIL(&commits, qid, entry);
948 2afa256d 2024-03-27 stsp qid = NULL;
949 2afa256d 2024-03-27 stsp }
950 2afa256d 2024-03-27 stsp
951 2afa256d 2024-03-27 stsp got_object_commit_close(commit);
952 2afa256d 2024-03-27 stsp commit = NULL;
953 2afa256d 2024-03-27 stsp }
954 2afa256d 2024-03-27 stsp done:
955 2afa256d 2024-03-27 stsp if (commit)
956 2afa256d 2024-03-27 stsp got_object_commit_close(commit);
957 2afa256d 2024-03-27 stsp got_object_qid_free(qid);
958 2afa256d 2024-03-27 stsp got_object_id_queue_free(&commits);
959 a9833bc9 2019-05-13 stsp return err;
960 a9833bc9 2019-05-13 stsp }