2 372ccdbb 2018-06-10 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
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.
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.
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>
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 372ccdbb 2018-06-10 stsp #include <zlib.h>
28 372ccdbb 2018-06-10 stsp #include <ctype.h>
30 372ccdbb 2018-06-10 stsp #include "got_error.h"
31 372ccdbb 2018-06-10 stsp #include "got_object.h"
32 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
33 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
34 324d37e7 2019-05-11 stsp #include "got_path.h"
36 372ccdbb 2018-06-10 stsp #include "got_lib_delta.h"
37 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
38 372ccdbb 2018-06-10 stsp #include "got_lib_object.h"
39 372ccdbb 2018-06-10 stsp #include "got_lib_object_idset.h"
41 372ccdbb 2018-06-10 stsp struct got_commit_graph_node {
42 372ccdbb 2018-06-10 stsp struct got_object_id id;
43 73026088 2018-11-18 stsp time_t timestamp;
45 372ccdbb 2018-06-10 stsp /* Used during graph iteration. */
46 372ccdbb 2018-06-10 stsp TAILQ_ENTRY(got_commit_graph_node) entry;
49 9ba79e04 2018-06-11 stsp TAILQ_HEAD(got_commit_graph_iter_list, got_commit_graph_node);
51 b565f6f8 2018-07-23 stsp struct got_commit_graph_branch_tip {
52 32777563 2018-11-07 stsp struct got_object_id *commit_id;
53 32777563 2018-11-07 stsp struct got_commit_object *commit;
54 32777563 2018-11-07 stsp struct got_commit_graph_node *new_node;
57 372ccdbb 2018-06-10 stsp struct got_commit_graph {
58 372ccdbb 2018-06-10 stsp /* The set of all commits we have traversed. */
59 372ccdbb 2018-06-10 stsp struct got_object_idset *node_ids;
62 0ed6ed4c 2018-06-13 stsp #define GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL 0x01
65 372ccdbb 2018-06-10 stsp * A set of object IDs of known parent commits which we have not yet
66 372ccdbb 2018-06-10 stsp * traversed. Each commit ID in this set represents a branch in commit
67 372ccdbb 2018-06-10 stsp * history: Either the first-parent branch of the head node, or another
68 372ccdbb 2018-06-10 stsp * branch corresponding to a traversed merge commit for which we have
69 372ccdbb 2018-06-10 stsp * not traversed a branch point commit yet.
71 372ccdbb 2018-06-10 stsp * Whenever we add a commit with a matching ID to the graph, we remove
72 372ccdbb 2018-06-10 stsp * its corresponding element from this set, and add new elements for
73 372ccdbb 2018-06-10 stsp * each of that commit's parent commits which were not traversed yet.
75 372ccdbb 2018-06-10 stsp * When API users ask us to fetch more commits, we fetch commits from
76 372ccdbb 2018-06-10 stsp * all currently open branches. This allows API users to process
77 372ccdbb 2018-06-10 stsp * commits in linear order even though the history contains branches.
79 372ccdbb 2018-06-10 stsp struct got_object_idset *open_branches;
81 32777563 2018-11-07 stsp /* Array of branch tips for fetch_commits_from_open_branches(). */
82 b565f6f8 2018-07-23 stsp struct got_commit_graph_branch_tip *tips;
85 31cedeaf 2018-09-15 stsp /* Path of tree entry of interest to the API user. */
88 372ccdbb 2018-06-10 stsp /* The next commit to return when the API user asks for one. */
89 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *iter_node;
91 9ba79e04 2018-06-11 stsp /* The graph iteration list contains all nodes in sorted order. */
92 9ba79e04 2018-06-11 stsp struct got_commit_graph_iter_list iter_list;
95 31cedeaf 2018-09-15 stsp static const struct got_error *
96 41fa1437 2018-11-05 stsp detect_changed_path(int *changed, struct got_commit_object *commit,
97 31cedeaf 2018-09-15 stsp struct got_object_id *commit_id, const char *path,
98 31cedeaf 2018-09-15 stsp struct got_repository *repo)
100 31cedeaf 2018-09-15 stsp const struct got_error *err = NULL;
101 41fa1437 2018-11-05 stsp struct got_commit_object *pcommit = NULL;
102 31cedeaf 2018-09-15 stsp struct got_tree_object *tree = NULL, *ptree = NULL;
103 31cedeaf 2018-09-15 stsp struct got_object_qid *pid;
105 31cedeaf 2018-09-15 stsp if (got_path_is_root_dir(path)) {
106 31cedeaf 2018-09-15 stsp *changed = 1;
107 31cedeaf 2018-09-15 stsp return NULL;
110 31cedeaf 2018-09-15 stsp *changed = 0;
112 31cedeaf 2018-09-15 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
113 31cedeaf 2018-09-15 stsp if (pid == NULL) {
114 31cedeaf 2018-09-15 stsp struct got_object_id *obj_id;
115 31cedeaf 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
117 d1451975 2018-11-11 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
118 fd1d2703 2018-11-04 stsp err = NULL;
120 fd1d2703 2018-11-04 stsp *changed = 1; /* The path was created in this commit. */
121 31cedeaf 2018-09-15 stsp free(obj_id);
122 0e9101d5 2018-11-18 stsp return err;
125 0e9101d5 2018-11-18 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
127 0e9101d5 2018-11-18 stsp return err;
129 0e9101d5 2018-11-18 stsp err = got_object_open_as_commit(&pcommit, repo, pid->id);
133 0e9101d5 2018-11-18 stsp err = got_object_open_as_tree(&ptree, repo, pcommit->tree_id);
137 81a966c0 2018-11-18 stsp err = got_object_tree_path_changed(changed, tree, ptree, path, repo);
140 31cedeaf 2018-09-15 stsp got_object_tree_close(tree);
142 31cedeaf 2018-09-15 stsp got_object_tree_close(ptree);
143 31cedeaf 2018-09-15 stsp if (pcommit)
144 41fa1437 2018-11-05 stsp got_object_commit_close(pcommit);
145 31cedeaf 2018-09-15 stsp return err;
148 4626e416 2018-06-10 stsp static void
149 9ba79e04 2018-06-11 stsp add_node_to_iter_list(struct got_commit_graph *graph,
150 de56b2d7 2020-01-04 stsp struct got_commit_graph_node *node)
152 4bd3f2bb 2018-06-10 stsp struct got_commit_graph_node *n, *next;
154 31920504 2018-06-11 stsp if (TAILQ_EMPTY(&graph->iter_list)) {
155 bee6b577 2018-09-19 stsp TAILQ_INSERT_HEAD(&graph->iter_list, node, entry);
156 bee6b577 2018-09-19 stsp graph->iter_node = node;
160 bee6b577 2018-09-19 stsp n = graph->iter_node;
161 fe8df4c2 2018-06-16 stsp /* Ensure that an iteration in progress will see this new commit. */
162 bee6b577 2018-09-19 stsp while (n) {
163 bee6b577 2018-09-19 stsp next = TAILQ_NEXT(n, entry);
164 73026088 2018-11-18 stsp if (next && node->timestamp >= next->timestamp) {
165 bee6b577 2018-09-19 stsp TAILQ_INSERT_BEFORE(next, node, entry);
170 93e45b7c 2018-09-24 stsp TAILQ_INSERT_TAIL(&graph->iter_list, node, entry);
173 0ed6ed4c 2018-06-13 stsp static const struct got_error *
174 31cedeaf 2018-09-15 stsp close_branch(struct got_commit_graph *graph, struct got_object_id *commit_id)
176 31cedeaf 2018-09-15 stsp const struct got_error *err;
178 31cedeaf 2018-09-15 stsp err = got_object_idset_remove(NULL, graph->open_branches, commit_id);
179 31cedeaf 2018-09-15 stsp if (err && err->code != GOT_ERR_NO_OBJ)
180 31cedeaf 2018-09-15 stsp return err;
181 31cedeaf 2018-09-15 stsp return NULL;
184 31cedeaf 2018-09-15 stsp static const struct got_error *
185 31cedeaf 2018-09-15 stsp advance_branch(struct got_commit_graph *graph,
186 0ed6ed4c 2018-06-13 stsp struct got_commit_graph_node *node,
187 41fa1437 2018-11-05 stsp struct got_object_id *commit_id, struct got_commit_object *commit,
188 bee6b577 2018-09-19 stsp struct got_repository *repo)
190 0ed6ed4c 2018-06-13 stsp const struct got_error *err;
191 0ed6ed4c 2018-06-13 stsp struct got_object_qid *qid;
193 31cedeaf 2018-09-15 stsp err = close_branch(graph, commit_id);
195 0ed6ed4c 2018-06-13 stsp return err;
197 0ed6ed4c 2018-06-13 stsp if (graph->flags & GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL) {
198 0ed6ed4c 2018-06-13 stsp qid = SIMPLEQ_FIRST(&commit->parent_ids);
199 9b88e78c 2018-11-18 stsp if (qid == NULL ||
200 9b88e78c 2018-11-18 stsp got_object_idset_get(graph->open_branches, qid->id))
201 0ed6ed4c 2018-06-13 stsp return NULL;
202 9b88e78c 2018-11-18 stsp return got_object_idset_add(graph->open_branches,
203 9b88e78c 2018-11-18 stsp qid->id, node);
207 bee6b577 2018-09-19 stsp * If we are graphing commits for a specific path, skip branches
208 bee6b577 2018-09-19 stsp * which do not contribute any content to this path.
210 7a62478b 2018-11-18 stsp if (commit->nparents > 1 && !got_path_is_root_dir(graph->path)) {
211 6dcdad08 2018-11-18 stsp struct got_object_id *merged_id, *prev_id = NULL;
212 bee6b577 2018-09-19 stsp int branches_differ = 0;
214 bee6b577 2018-09-19 stsp err = got_object_id_by_path(&merged_id, repo, commit_id,
215 bee6b577 2018-09-19 stsp graph->path);
217 bee6b577 2018-09-19 stsp return err;
219 bee6b577 2018-09-19 stsp SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
220 6dcdad08 2018-11-18 stsp struct got_object_id *id;
222 9b88e78c 2018-11-18 stsp if (got_object_idset_get(graph->open_branches, qid->id))
225 bee6b577 2018-09-19 stsp err = got_object_id_by_path(&id, repo, qid->id,
226 bee6b577 2018-09-19 stsp graph->path);
228 d1451975 2018-11-11 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY) {
229 bee6b577 2018-09-19 stsp branches_differ = 1;
232 6dcdad08 2018-11-18 stsp free(merged_id);
233 6dcdad08 2018-11-18 stsp free(prev_id);
234 bee6b577 2018-09-19 stsp return err;
237 bee6b577 2018-09-19 stsp if (prev_id) {
238 bee6b577 2018-09-19 stsp if (!branches_differ &&
239 d2c2d781 2018-11-18 stsp got_object_id_cmp(id, prev_id) != 0)
240 bee6b577 2018-09-19 stsp branches_differ = 1;
241 6dcdad08 2018-11-18 stsp free(prev_id);
243 6dcdad08 2018-11-18 stsp prev_id = id;
246 bee6b577 2018-09-19 stsp * If a branch has created the merged content we can
247 bee6b577 2018-09-19 stsp * skip any other branches.
249 bee6b577 2018-09-19 stsp if (got_object_id_cmp(merged_id, id) == 0) {
250 b36429ab 2018-11-05 stsp err = got_object_idset_add(graph->open_branches,
251 b36429ab 2018-11-05 stsp qid->id, node);
252 6dcdad08 2018-11-18 stsp free(merged_id);
254 b36429ab 2018-11-05 stsp return err;
258 6dcdad08 2018-11-18 stsp free(prev_id);
259 6dcdad08 2018-11-18 stsp prev_id = NULL;
260 6dcdad08 2018-11-18 stsp free(merged_id);
261 6dcdad08 2018-11-18 stsp merged_id = NULL;
264 bee6b577 2018-09-19 stsp * If the path's content is the same on all branches,
265 bee6b577 2018-09-19 stsp * follow the first parent only.
267 bee6b577 2018-09-19 stsp if (!branches_differ) {
268 bee6b577 2018-09-19 stsp qid = SIMPLEQ_FIRST(&commit->parent_ids);
269 bee6b577 2018-09-19 stsp if (qid == NULL)
270 bee6b577 2018-09-19 stsp return NULL;
271 b36429ab 2018-11-05 stsp if (got_object_idset_get(graph->open_branches, qid->id))
272 b36429ab 2018-11-05 stsp return NULL;
273 998ff57f 2018-11-18 stsp if (got_object_idset_get(graph->node_ids, qid->id))
274 998ff57f 2018-11-18 stsp return NULL; /* parent already traversed */
275 b36429ab 2018-11-05 stsp return got_object_idset_add(graph->open_branches,
276 b36429ab 2018-11-05 stsp qid->id, node);
280 0ed6ed4c 2018-06-13 stsp SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
281 b36429ab 2018-11-05 stsp if (got_object_idset_get(graph->open_branches, qid->id))
283 998ff57f 2018-11-18 stsp if (got_object_idset_get(graph->node_ids, qid->id))
284 998ff57f 2018-11-18 stsp continue; /* parent already traversed */
285 b36429ab 2018-11-05 stsp err = got_object_idset_add(graph->open_branches, qid->id, node);
287 0ed6ed4c 2018-06-13 stsp return err;
290 b43fbaa0 2018-06-11 stsp return NULL;
293 b43fbaa0 2018-06-11 stsp static const struct got_error *
294 de56b2d7 2020-01-04 stsp add_node(struct got_commit_graph_node **new_node,
295 de56b2d7 2020-01-04 stsp struct got_commit_graph *graph,
296 de56b2d7 2020-01-04 stsp struct got_object_id *commit_id,
297 de56b2d7 2020-01-04 stsp struct got_commit_object *commit,
298 de56b2d7 2020-01-04 stsp struct got_repository *repo)
300 372ccdbb 2018-06-10 stsp const struct got_error *err = NULL;
301 f4ceb45e 2018-07-23 stsp struct got_commit_graph_node *node;
303 372ccdbb 2018-06-10 stsp *new_node = NULL;
305 372ccdbb 2018-06-10 stsp node = calloc(1, sizeof(*node));
306 372ccdbb 2018-06-10 stsp if (node == NULL)
307 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
309 372ccdbb 2018-06-10 stsp memcpy(&node->id, commit_id, sizeof(node->id));
310 73026088 2018-11-18 stsp node->timestamp = commit->committer_time;
312 b36429ab 2018-11-05 stsp err = got_object_idset_add(graph->node_ids, &node->id, node);
314 7a62478b 2018-11-18 stsp free(node);
316 de56b2d7 2020-01-04 stsp *new_node = node;
317 de56b2d7 2020-01-04 stsp return err;
320 de56b2d7 2020-01-04 stsp const struct got_error *
321 c4d7a9c4 2018-06-11 stsp got_commit_graph_open(struct got_commit_graph **graph,
322 3d509237 2020-01-04 stsp const char *path, int first_parent_traversal)
324 22220781 2020-01-04 stsp const struct got_error *err = NULL;
326 3ddcebf3 2020-01-04 stsp *graph = calloc(1, sizeof(**graph));
327 3d509237 2020-01-04 stsp if (*graph == NULL)
328 3ddcebf3 2020-01-04 stsp return got_error_from_errno("calloc");
330 88cdb9c6 2020-01-04 stsp TAILQ_INIT(&(*graph)->iter_list);
332 3ddcebf3 2020-01-04 stsp (*graph)->path = strdup(path);
333 3ddcebf3 2020-01-04 stsp if ((*graph)->path == NULL) {
334 3ddcebf3 2020-01-04 stsp err = got_error_from_errno("strdup");
338 3ddcebf3 2020-01-04 stsp (*graph)->node_ids = got_object_idset_alloc();
339 3ddcebf3 2020-01-04 stsp if ((*graph)->node_ids == NULL) {
340 3ddcebf3 2020-01-04 stsp err = got_error_from_errno("got_object_idset_alloc");
344 3ddcebf3 2020-01-04 stsp (*graph)->open_branches = got_object_idset_alloc();
345 3ddcebf3 2020-01-04 stsp if ((*graph)->open_branches == NULL) {
346 3ddcebf3 2020-01-04 stsp err = got_error_from_errno("got_object_idset_alloc");
350 0ed6ed4c 2018-06-13 stsp if (first_parent_traversal)
351 0ed6ed4c 2018-06-13 stsp (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
354 22220781 2020-01-04 stsp got_commit_graph_close(*graph);
355 22220781 2020-01-04 stsp *graph = NULL;
357 22220781 2020-01-04 stsp return err;
360 32777563 2018-11-07 stsp struct add_branch_tip_arg {
361 b565f6f8 2018-07-23 stsp struct got_commit_graph_branch_tip *tips;
363 32777563 2018-11-07 stsp struct got_repository *repo;
364 32777563 2018-11-07 stsp struct got_commit_graph *graph;
367 cb103d04 2018-11-07 stsp static const struct got_error *
368 32777563 2018-11-07 stsp add_branch_tip(struct got_object_id *commit_id, void *data, void *arg)
370 32777563 2018-11-07 stsp const struct got_error *err;
371 32777563 2018-11-07 stsp struct add_branch_tip_arg *a = arg;
372 32777563 2018-11-07 stsp struct got_commit_graph_node *new_node;
373 32777563 2018-11-07 stsp struct got_commit_object *commit;
375 32777563 2018-11-07 stsp err = got_object_open_as_commit(&commit, a->repo, commit_id);
377 32777563 2018-11-07 stsp return err;
379 de56b2d7 2020-01-04 stsp err = add_node(&new_node, a->graph, commit_id, commit, a->repo);
381 32777563 2018-11-07 stsp return err;
383 32777563 2018-11-07 stsp a->tips[a->ntips].commit_id = new_node ? &new_node->id : NULL;
384 32777563 2018-11-07 stsp a->tips[a->ntips].commit = commit;
385 32777563 2018-11-07 stsp a->tips[a->ntips].new_node = new_node;
386 b565f6f8 2018-07-23 stsp a->ntips++;
388 cb103d04 2018-11-07 stsp return NULL;
391 1142eae9 2018-06-11 stsp static const struct got_error *
392 32777563 2018-11-07 stsp fetch_commits_from_open_branches(int *nfetched,
393 31cedeaf 2018-09-15 stsp struct got_object_id **changed_id, struct got_commit_graph *graph,
394 6fb7cd11 2019-08-22 stsp struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
396 372ccdbb 2018-06-10 stsp const struct got_error *err;
397 32777563 2018-11-07 stsp struct add_branch_tip_arg arg;
398 32777563 2018-11-07 stsp int i, ntips;
400 32777563 2018-11-07 stsp *nfetched = 0;
401 818c7501 2019-07-11 stsp if (changed_id)
402 818c7501 2019-07-11 stsp *changed_id = NULL;
404 32777563 2018-11-07 stsp ntips = got_object_idset_num_elements(graph->open_branches);
405 32777563 2018-11-07 stsp if (ntips == 0)
406 372ccdbb 2018-06-10 stsp return NULL;
408 32777563 2018-11-07 stsp /* (Re-)allocate branch tips array if necessary. */
409 32777563 2018-11-07 stsp if (graph->ntips < ntips) {
410 b565f6f8 2018-07-23 stsp struct got_commit_graph_branch_tip *tips;
411 5e50c36a 2018-11-08 stsp tips = recallocarray(graph->tips, graph->ntips, ntips,
412 5e50c36a 2018-11-08 stsp sizeof(*tips));
413 b565f6f8 2018-07-23 stsp if (tips == NULL)
414 638f9024 2019-05-13 stsp return got_error_from_errno("recallocarray");
415 b565f6f8 2018-07-23 stsp graph->tips = tips;
416 32777563 2018-11-07 stsp graph->ntips = ntips;
418 b565f6f8 2018-07-23 stsp arg.tips = graph->tips;
419 32777563 2018-11-07 stsp arg.ntips = 0; /* add_branch_tip() will increment */
420 32777563 2018-11-07 stsp arg.repo = repo;
421 32777563 2018-11-07 stsp arg.graph = graph;
422 32777563 2018-11-07 stsp err = got_object_idset_for_each(graph->open_branches, add_branch_tip,
427 b565f6f8 2018-07-23 stsp for (i = 0; i < arg.ntips; i++) {
428 372ccdbb 2018-06-10 stsp struct got_object_id *commit_id;
429 41fa1437 2018-11-05 stsp struct got_commit_object *commit;
430 32777563 2018-11-07 stsp struct got_commit_graph_node *new_node;
431 13a851c1 2020-01-04 stsp int changed;
433 6fb7cd11 2019-08-22 stsp if (cancel_cb) {
434 6fb7cd11 2019-08-22 stsp err = (*cancel_cb)(cancel_arg);
439 32777563 2018-11-07 stsp commit_id = arg.tips[i].commit_id;
440 32777563 2018-11-07 stsp commit = arg.tips[i].commit;
441 32777563 2018-11-07 stsp new_node = arg.tips[i].new_node;
443 13a851c1 2020-01-04 stsp err = detect_changed_path(&changed, commit, commit_id,
444 13a851c1 2020-01-04 stsp graph->path, repo);
446 13a851c1 2020-01-04 stsp if (err->code != GOT_ERR_NO_OBJ)
449 13a851c1 2020-01-04 stsp * History of the path stops here on the current
450 13a851c1 2020-01-04 stsp * branch. Keep going on other branches.
452 13a851c1 2020-01-04 stsp err = close_branch(graph, commit_id);
457 de56b2d7 2020-01-04 stsp if (changed) {
458 de56b2d7 2020-01-04 stsp add_node_to_iter_list(graph, new_node);
459 de56b2d7 2020-01-04 stsp if (changed_id && *changed_id == NULL)
460 de56b2d7 2020-01-04 stsp *changed_id = commit_id;
462 13a851c1 2020-01-04 stsp err = advance_branch(graph, new_node, commit_id,
463 13a851c1 2020-01-04 stsp commit, repo);
468 246e1c78 2018-11-08 stsp for (i = 0; i < arg.ntips; i++)
469 32777563 2018-11-07 stsp got_object_commit_close(arg.tips[i].commit);
470 32777563 2018-11-07 stsp (*nfetched) = arg.ntips;
471 372ccdbb 2018-06-10 stsp return err;
474 cb103d04 2018-11-07 stsp static const struct got_error *
475 fb5b9d5a 2018-07-23 stsp free_node_iter(struct got_object_id *id, void *data, void *arg)
477 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *node = data;
478 7a62478b 2018-11-18 stsp free(node);
479 cb103d04 2018-11-07 stsp return NULL;
483 372ccdbb 2018-06-10 stsp got_commit_graph_close(struct got_commit_graph *graph)
485 22220781 2020-01-04 stsp if (graph->open_branches)
486 22220781 2020-01-04 stsp got_object_idset_free(graph->open_branches);
487 fb5b9d5a 2018-07-23 stsp got_object_idset_for_each(graph->node_ids, free_node_iter, NULL);
488 22220781 2020-01-04 stsp if (graph->node_ids)
489 22220781 2020-01-04 stsp got_object_idset_free(graph->node_ids);
490 b565f6f8 2018-07-23 stsp free(graph->tips);
491 31cedeaf 2018-09-15 stsp free(graph->path);
492 372ccdbb 2018-06-10 stsp free(graph);
495 372ccdbb 2018-06-10 stsp const struct got_error *
496 372ccdbb 2018-06-10 stsp got_commit_graph_iter_start(struct got_commit_graph *graph,
497 6fb7cd11 2019-08-22 stsp struct got_object_id *id, struct got_repository *repo,
498 6fb7cd11 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
500 31cedeaf 2018-09-15 stsp const struct got_error *err = NULL;
501 9ba79e04 2018-06-11 stsp struct got_commit_graph_node *start_node;
502 41fa1437 2018-11-05 stsp struct got_commit_object *commit;
503 13a851c1 2020-01-04 stsp int changed;
505 3d509237 2020-01-04 stsp if (!TAILQ_EMPTY(&graph->iter_list))
506 3d509237 2020-01-04 stsp return got_error(GOT_ERR_ITER_BUSY);
508 3d509237 2020-01-04 stsp err = got_object_open_as_commit(&commit, repo, id);
510 31cedeaf 2018-09-15 stsp return err;
512 de56b2d7 2020-01-04 stsp err = add_node(&start_node, graph, id, commit, repo);
516 13a851c1 2020-01-04 stsp err = detect_changed_path(&changed, commit, id, graph->path, repo);
519 de56b2d7 2020-01-04 stsp if (changed)
520 de56b2d7 2020-01-04 stsp add_node_to_iter_list(graph, start_node);
522 3d509237 2020-01-04 stsp err = advance_branch(graph, start_node, id, commit, repo);
526 31cedeaf 2018-09-15 stsp if (!changed) {
527 31cedeaf 2018-09-15 stsp /* Locate first commit which changed graph->path. */
528 31cedeaf 2018-09-15 stsp struct got_object_id *changed_id = NULL;
529 31cedeaf 2018-09-15 stsp while (changed_id == NULL) {
530 31cedeaf 2018-09-15 stsp int ncommits;
531 644e44ed 2018-11-04 stsp err = fetch_commits_from_open_branches(&ncommits,
532 6fb7cd11 2019-08-22 stsp &changed_id, graph, repo, cancel_cb, cancel_arg);
534 41fa1437 2018-11-05 stsp got_object_commit_close(commit);
535 31cedeaf 2018-09-15 stsp return err;
538 31cedeaf 2018-09-15 stsp start_node = got_object_idset_get(graph->node_ids, changed_id);
541 41fa1437 2018-11-05 stsp got_object_commit_close(commit);
542 3d509237 2020-01-04 stsp if (err == NULL)
543 3d509237 2020-01-04 stsp graph->iter_node = start_node;
544 3d509237 2020-01-04 stsp return err;
547 372ccdbb 2018-06-10 stsp const struct got_error *
548 b43fbaa0 2018-06-11 stsp got_commit_graph_iter_next(struct got_object_id **id,
549 ee780d5c 2020-01-04 stsp struct got_commit_graph *graph, struct got_repository *repo,
550 ee780d5c 2020-01-04 stsp got_cancel_cb cancel_cb, void *cancel_arg)
552 ee780d5c 2020-01-04 stsp const struct got_error *err = NULL;
554 9ba79e04 2018-06-11 stsp *id = NULL;
556 372ccdbb 2018-06-10 stsp if (graph->iter_node == NULL) {
557 2c7f8870 2018-09-19 stsp /* We are done iterating, or iteration was not started. */
558 9ba79e04 2018-06-11 stsp return got_error(GOT_ERR_ITER_COMPLETED);
561 9ba79e04 2018-06-11 stsp if (graph->iter_node ==
562 9ba79e04 2018-06-11 stsp TAILQ_LAST(&graph->iter_list, got_commit_graph_iter_list) &&
563 9ba79e04 2018-06-11 stsp got_object_idset_num_elements(graph->open_branches) == 0) {
564 2c7f8870 2018-09-19 stsp /* We are done iterating. */
565 6bc66952 2018-09-19 stsp *id = &graph->iter_node->id;
566 9ba79e04 2018-06-11 stsp graph->iter_node = NULL;
567 6bc66952 2018-09-19 stsp return NULL;
570 ee780d5c 2020-01-04 stsp while (TAILQ_NEXT(graph->iter_node, entry) == NULL &&
571 ee780d5c 2020-01-04 stsp got_object_idset_num_elements(graph->open_branches) > 0) {
572 ee780d5c 2020-01-04 stsp int ncommits;
573 ee780d5c 2020-01-04 stsp struct got_object_id *changed_id;
574 ee780d5c 2020-01-04 stsp err = fetch_commits_from_open_branches(&ncommits,
575 ee780d5c 2020-01-04 stsp &changed_id, graph, repo, cancel_cb, cancel_arg);
577 ee780d5c 2020-01-04 stsp return err;
580 6bc66952 2018-09-19 stsp *id = &graph->iter_node->id;
581 6bc66952 2018-09-19 stsp graph->iter_node = TAILQ_NEXT(graph->iter_node, entry);
582 372ccdbb 2018-06-10 stsp return NULL;
585 a9833bc9 2019-05-13 stsp const struct got_error *
586 a9833bc9 2019-05-13 stsp got_commit_graph_find_youngest_common_ancestor(struct got_object_id **yca_id,
587 a9833bc9 2019-05-13 stsp struct got_object_id *commit_id, struct got_object_id *commit_id2,
588 6fb7cd11 2019-08-22 stsp struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
590 a9833bc9 2019-05-13 stsp const struct got_error *err = NULL;
591 a9833bc9 2019-05-13 stsp struct got_commit_graph *graph = NULL, *graph2 = NULL;
592 a9833bc9 2019-05-13 stsp int completed = 0, completed2 = 0;
593 a9833bc9 2019-05-13 stsp struct got_object_idset *commit_ids;
595 a9833bc9 2019-05-13 stsp *yca_id = NULL;
597 a9833bc9 2019-05-13 stsp commit_ids = got_object_idset_alloc();
598 a9833bc9 2019-05-13 stsp if (commit_ids == NULL)
599 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_idset_alloc");
601 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
605 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph2, "/", 1);
609 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, commit_id, repo,
610 6fb7cd11 2019-08-22 stsp cancel_cb, cancel_arg);
614 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph2, commit_id2, repo,
615 6fb7cd11 2019-08-22 stsp cancel_cb, cancel_arg);
620 ee780d5c 2020-01-04 stsp struct got_object_id *id = NULL, *id2 = NULL;
622 6fb7cd11 2019-08-22 stsp if (cancel_cb) {
623 6fb7cd11 2019-08-22 stsp err = (*cancel_cb)(cancel_arg);
628 a9833bc9 2019-05-13 stsp if (!completed) {
629 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
630 ee780d5c 2020-01-04 stsp cancel_cb, cancel_arg);
632 ee780d5c 2020-01-04 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
634 ee780d5c 2020-01-04 stsp err = NULL;
635 ee780d5c 2020-01-04 stsp completed = 1;
639 a9833bc9 2019-05-13 stsp if (!completed2) {
640 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id2, graph2, repo,
641 ee780d5c 2020-01-04 stsp cancel_cb, cancel_arg);
643 ee780d5c 2020-01-04 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
645 ee780d5c 2020-01-04 stsp err = NULL;
646 ee780d5c 2020-01-04 stsp completed2 = 1;
651 a9833bc9 2019-05-13 stsp if (got_object_idset_get(commit_ids, id)) {
652 a9833bc9 2019-05-13 stsp *yca_id = got_object_id_dup(id);
653 a9833bc9 2019-05-13 stsp if (*yca_id)
655 82751bc5 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
659 a9833bc9 2019-05-13 stsp err = got_object_idset_add(commit_ids, id, id);
664 a9833bc9 2019-05-13 stsp if (got_object_idset_get(commit_ids, id2)) {
665 a9833bc9 2019-05-13 stsp *yca_id = got_object_id_dup(id2);
666 a9833bc9 2019-05-13 stsp if (*yca_id)
668 82751bc5 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
672 a9833bc9 2019-05-13 stsp err = got_object_idset_add(commit_ids, id2, id2);
677 a9833bc9 2019-05-13 stsp if (completed && completed2) {
678 a9833bc9 2019-05-13 stsp err = got_error(GOT_ERR_ANCESTRY);
684 a9833bc9 2019-05-13 stsp got_object_idset_free(commit_ids);
686 a9833bc9 2019-05-13 stsp got_commit_graph_close(graph);
687 a9833bc9 2019-05-13 stsp if (graph2)
688 a9833bc9 2019-05-13 stsp got_commit_graph_close(graph2);
689 a9833bc9 2019-05-13 stsp return err;