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 372ccdbb 2018-06-10 stsp #include <stdio.h>
23 372ccdbb 2018-06-10 stsp #include <stdlib.h>
24 372ccdbb 2018-06-10 stsp #include <string.h>
25 372ccdbb 2018-06-10 stsp #include <sha1.h>
26 372ccdbb 2018-06-10 stsp #include <zlib.h>
27 372ccdbb 2018-06-10 stsp #include <ctype.h>
29 372ccdbb 2018-06-10 stsp #include "got_error.h"
30 372ccdbb 2018-06-10 stsp #include "got_object.h"
31 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
33 372ccdbb 2018-06-10 stsp #include "got_lib_delta.h"
34 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
35 372ccdbb 2018-06-10 stsp #include "got_lib_object.h"
36 372ccdbb 2018-06-10 stsp #include "got_lib_object_idset.h"
37 31cedeaf 2018-09-15 stsp #include "got_lib_path.h"
39 372ccdbb 2018-06-10 stsp struct got_commit_graph_node {
40 372ccdbb 2018-06-10 stsp struct got_object_id id;
43 372ccdbb 2018-06-10 stsp * Each graph node corresponds to a commit object.
44 f4ceb45e 2018-07-23 stsp * Graph vertices are modelled with an adjacency list.
45 f4ceb45e 2018-07-23 stsp * Adjacencies of a graph node are parent (older) commits.
47 b43fbaa0 2018-06-11 stsp int nparents;
48 79f35eb3 2018-06-11 stsp struct got_object_id_queue parent_ids;
50 b43fbaa0 2018-06-11 stsp time_t commit_timestamp;
52 372ccdbb 2018-06-10 stsp /* Used during graph iteration. */
53 372ccdbb 2018-06-10 stsp TAILQ_ENTRY(got_commit_graph_node) entry;
56 9ba79e04 2018-06-11 stsp TAILQ_HEAD(got_commit_graph_iter_list, got_commit_graph_node);
58 b565f6f8 2018-07-23 stsp struct got_commit_graph_branch_tip {
59 b565f6f8 2018-07-23 stsp struct got_object_id id;
60 b565f6f8 2018-07-23 stsp struct got_commit_graph_node *node;
63 372ccdbb 2018-06-10 stsp struct got_commit_graph {
64 372ccdbb 2018-06-10 stsp /* The set of all commits we have traversed. */
65 372ccdbb 2018-06-10 stsp struct got_object_idset *node_ids;
67 372ccdbb 2018-06-10 stsp /* The commit at which traversal began (youngest commit in node_ids). */
68 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *head_node;
71 0ed6ed4c 2018-06-13 stsp #define GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL 0x01
74 372ccdbb 2018-06-10 stsp * A set of object IDs of known parent commits which we have not yet
75 372ccdbb 2018-06-10 stsp * traversed. Each commit ID in this set represents a branch in commit
76 372ccdbb 2018-06-10 stsp * history: Either the first-parent branch of the head node, or another
77 372ccdbb 2018-06-10 stsp * branch corresponding to a traversed merge commit for which we have
78 372ccdbb 2018-06-10 stsp * not traversed a branch point commit yet.
80 372ccdbb 2018-06-10 stsp * Whenever we add a commit with a matching ID to the graph, we remove
81 372ccdbb 2018-06-10 stsp * its corresponding element from this set, and add new elements for
82 372ccdbb 2018-06-10 stsp * each of that commit's parent commits which were not traversed yet.
84 372ccdbb 2018-06-10 stsp * When API users ask us to fetch more commits, we fetch commits from
85 372ccdbb 2018-06-10 stsp * all currently open branches. This allows API users to process
86 372ccdbb 2018-06-10 stsp * commits in linear order even though the history contains branches.
88 372ccdbb 2018-06-10 stsp struct got_object_idset *open_branches;
90 b565f6f8 2018-07-23 stsp /* Copy of known branch tips for fetch_commits_from_open_branches(). */
91 b565f6f8 2018-07-23 stsp struct got_commit_graph_branch_tip *tips;
92 b565f6f8 2018-07-23 stsp size_t ntips;
93 b565f6f8 2018-07-23 stsp #define GOT_COMMIT_GRAPH_MIN_TIPS 100 /* minimum amount of tips to allocate */
95 31cedeaf 2018-09-15 stsp /* Path of tree entry of interest to the API user. */
98 372ccdbb 2018-06-10 stsp /* The next commit to return when the API user asks for one. */
99 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *iter_node;
101 9ba79e04 2018-06-11 stsp /* The graph iteration list contains all nodes in sorted order. */
102 9ba79e04 2018-06-11 stsp struct got_commit_graph_iter_list iter_list;
105 372ccdbb 2018-06-10 stsp static struct got_commit_graph *
106 31cedeaf 2018-09-15 stsp alloc_graph(const char *path)
108 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
110 372ccdbb 2018-06-10 stsp graph = calloc(1, sizeof(*graph));
111 372ccdbb 2018-06-10 stsp if (graph == NULL)
112 372ccdbb 2018-06-10 stsp return NULL;
114 31cedeaf 2018-09-15 stsp graph->path = strdup(path);
115 31cedeaf 2018-09-15 stsp if (graph->path == NULL) {
116 31cedeaf 2018-09-15 stsp free(graph);
117 31cedeaf 2018-09-15 stsp return NULL;
120 60f2eee1 2018-07-08 stsp graph->node_ids = got_object_idset_alloc();
121 372ccdbb 2018-06-10 stsp if (graph->node_ids == NULL) {
122 31cedeaf 2018-09-15 stsp free(graph->path);
123 372ccdbb 2018-06-10 stsp free(graph);
124 372ccdbb 2018-06-10 stsp return NULL;
127 60f2eee1 2018-07-08 stsp graph->open_branches = got_object_idset_alloc();
128 372ccdbb 2018-06-10 stsp if (graph->open_branches == NULL) {
129 372ccdbb 2018-06-10 stsp got_object_idset_free(graph->node_ids);
130 31cedeaf 2018-09-15 stsp free(graph->path);
131 372ccdbb 2018-06-10 stsp free(graph);
132 372ccdbb 2018-06-10 stsp return NULL;
135 31920504 2018-06-11 stsp TAILQ_INIT(&graph->iter_list);
136 372ccdbb 2018-06-10 stsp return graph;
141 372ccdbb 2018-06-10 stsp is_head_node(struct got_commit_graph_node *node)
143 372ccdbb 2018-06-10 stsp return node->nchildren == 0;
147 372ccdbb 2018-06-10 stsp is_branch_point(struct got_commit_graph_node *node)
149 372ccdbb 2018-06-10 stsp return node->nchildren > 1;
153 372ccdbb 2018-06-10 stsp is_root_node(struct got_commit_graph_node *node)
155 b43fbaa0 2018-06-11 stsp return node->nparents == 0;
160 bee6b577 2018-09-19 stsp is_merge_point(struct got_commit_graph_node *node)
162 bee6b577 2018-09-19 stsp return node->nparents > 1;
165 31cedeaf 2018-09-15 stsp static const struct got_error *
166 31cedeaf 2018-09-15 stsp detect_changed_path(int *changed, struct got_commit_object *commit,
167 31cedeaf 2018-09-15 stsp struct got_object_id *commit_id, const char *path,
168 31cedeaf 2018-09-15 stsp struct got_repository *repo)
170 31cedeaf 2018-09-15 stsp const struct got_error *err = NULL;
171 31cedeaf 2018-09-15 stsp struct got_commit_object *pcommit = NULL;
172 31cedeaf 2018-09-15 stsp struct got_tree_object *tree = NULL, *ptree = NULL;
173 31cedeaf 2018-09-15 stsp struct got_object_qid *pid;
175 31cedeaf 2018-09-15 stsp if (got_path_is_root_dir(path)) {
176 31cedeaf 2018-09-15 stsp *changed = 1;
177 31cedeaf 2018-09-15 stsp return NULL;
180 31cedeaf 2018-09-15 stsp *changed = 0;
182 31cedeaf 2018-09-15 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
184 31cedeaf 2018-09-15 stsp return err;
186 31cedeaf 2018-09-15 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
187 31cedeaf 2018-09-15 stsp if (pid == NULL) {
188 31cedeaf 2018-09-15 stsp struct got_object_id *obj_id;
189 31cedeaf 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
191 31cedeaf 2018-09-15 stsp err = NULL;
193 31cedeaf 2018-09-15 stsp *changed = 1;
194 31cedeaf 2018-09-15 stsp free(obj_id);
198 31cedeaf 2018-09-15 stsp err = got_object_open_as_commit(&pcommit, repo, pid->id);
202 31cedeaf 2018-09-15 stsp err = got_object_open_as_tree(&ptree, repo, pcommit->tree_id);
206 31cedeaf 2018-09-15 stsp err = got_object_tree_path_changed(changed, tree, ptree, path, repo);
209 31cedeaf 2018-09-15 stsp got_object_tree_close(tree);
211 31cedeaf 2018-09-15 stsp got_object_tree_close(ptree);
212 31cedeaf 2018-09-15 stsp if (pcommit)
213 31cedeaf 2018-09-15 stsp got_object_commit_close(pcommit);
214 31cedeaf 2018-09-15 stsp return err;
217 4626e416 2018-06-10 stsp static void
218 9ba79e04 2018-06-11 stsp add_node_to_iter_list(struct got_commit_graph *graph,
219 9ba79e04 2018-06-11 stsp struct got_commit_graph_node *node,
220 9ba79e04 2018-06-11 stsp struct got_commit_graph_node *child_node)
222 4bd3f2bb 2018-06-10 stsp struct got_commit_graph_node *n, *next;
224 31920504 2018-06-11 stsp if (TAILQ_EMPTY(&graph->iter_list)) {
225 bee6b577 2018-09-19 stsp TAILQ_INSERT_HEAD(&graph->iter_list, node, entry);
226 bee6b577 2018-09-19 stsp graph->iter_node = node;
230 bee6b577 2018-09-19 stsp n = graph->iter_node;
231 fe8df4c2 2018-06-16 stsp /* Ensure that an iteration in progress will see this new commit. */
232 bee6b577 2018-09-19 stsp while (n) {
233 bee6b577 2018-09-19 stsp next = TAILQ_NEXT(n, entry);
234 bee6b577 2018-09-19 stsp if (next && node->commit_timestamp >= next->commit_timestamp) {
235 bee6b577 2018-09-19 stsp TAILQ_INSERT_BEFORE(next, node, entry);
240 bee6b577 2018-09-19 stsp TAILQ_INSERT_AFTER(&graph->iter_list, graph->iter_node, node, entry);
243 372ccdbb 2018-06-10 stsp static const struct got_error *
244 79f35eb3 2018-06-11 stsp add_vertex(struct got_object_id_queue *ids, struct got_object_id *id)
246 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
248 79f35eb3 2018-06-11 stsp qid = calloc(1, sizeof(*qid));
249 79f35eb3 2018-06-11 stsp if (qid == NULL)
250 b43fbaa0 2018-06-11 stsp return got_error_from_errno();
252 79f35eb3 2018-06-11 stsp qid->id = got_object_id_dup(id);
253 79f35eb3 2018-06-11 stsp if (qid->id == NULL) {
254 b43fbaa0 2018-06-11 stsp const struct got_error *err = got_error_from_errno();
255 fa2f6902 2018-07-23 stsp got_object_qid_free(qid);
256 b43fbaa0 2018-06-11 stsp return err;
259 79f35eb3 2018-06-11 stsp SIMPLEQ_INSERT_TAIL(ids, qid, entry);
260 0ed6ed4c 2018-06-13 stsp return NULL;
263 0ed6ed4c 2018-06-13 stsp static const struct got_error *
264 31cedeaf 2018-09-15 stsp close_branch(struct got_commit_graph *graph, struct got_object_id *commit_id)
266 31cedeaf 2018-09-15 stsp const struct got_error *err;
268 31cedeaf 2018-09-15 stsp err = got_object_idset_remove(NULL, graph->open_branches, commit_id);
269 31cedeaf 2018-09-15 stsp if (err && err->code != GOT_ERR_NO_OBJ)
270 31cedeaf 2018-09-15 stsp return err;
271 31cedeaf 2018-09-15 stsp return NULL;
274 31cedeaf 2018-09-15 stsp static const struct got_error *
275 31cedeaf 2018-09-15 stsp advance_branch(struct got_commit_graph *graph,
276 0ed6ed4c 2018-06-13 stsp struct got_commit_graph_node *node,
277 bee6b577 2018-09-19 stsp struct got_object_id *commit_id, struct got_commit_object *commit,
278 bee6b577 2018-09-19 stsp struct got_repository *repo)
280 0ed6ed4c 2018-06-13 stsp const struct got_error *err;
281 0ed6ed4c 2018-06-13 stsp struct got_object_qid *qid;
283 31cedeaf 2018-09-15 stsp err = close_branch(graph, commit_id);
285 0ed6ed4c 2018-06-13 stsp return err;
287 0ed6ed4c 2018-06-13 stsp if (graph->flags & GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL) {
288 0ed6ed4c 2018-06-13 stsp qid = SIMPLEQ_FIRST(&commit->parent_ids);
289 0ed6ed4c 2018-06-13 stsp if (qid == NULL)
290 0ed6ed4c 2018-06-13 stsp return NULL;
291 0ed6ed4c 2018-06-13 stsp err = got_object_idset_add(NULL, graph->open_branches, qid->id,
293 0ed6ed4c 2018-06-13 stsp if (err && err->code != GOT_ERR_OBJ_EXISTS)
294 0ed6ed4c 2018-06-13 stsp return err;
295 0ed6ed4c 2018-06-13 stsp return NULL;
299 bee6b577 2018-09-19 stsp * If we are graphing commits for a specific path, skip branches
300 bee6b577 2018-09-19 stsp * which do not contribute any content to this path.
302 bee6b577 2018-09-19 stsp if (is_merge_point(node) && !got_path_is_root_dir(graph->path)) {
303 bee6b577 2018-09-19 stsp struct got_object_id *id, *merged_id, *prev_id = NULL;
304 bee6b577 2018-09-19 stsp int branches_differ = 0;
309 bee6b577 2018-09-19 stsp err = got_object_id_by_path(&merged_id, repo, commit_id,
310 bee6b577 2018-09-19 stsp graph->path);
312 bee6b577 2018-09-19 stsp return err;
314 bee6b577 2018-09-19 stsp SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
315 bee6b577 2018-09-19 stsp if (got_object_idset_get(graph->node_ids, qid->id))
316 bee6b577 2018-09-19 stsp continue; /* parent already traversed */
318 bee6b577 2018-09-19 stsp err = got_object_id_by_path(&id, repo, qid->id,
319 bee6b577 2018-09-19 stsp graph->path);
321 bee6b577 2018-09-19 stsp if (err->code == GOT_ERR_NO_OBJ) {
322 bee6b577 2018-09-19 stsp branches_differ = 1;
325 bee6b577 2018-09-19 stsp return err;
328 bee6b577 2018-09-19 stsp if (prev_id) {
329 bee6b577 2018-09-19 stsp if (!branches_differ &&
330 bee6b577 2018-09-19 stsp got_object_id_cmp(merged_id, prev_id) != 0)
331 bee6b577 2018-09-19 stsp branches_differ = 1;
333 bee6b577 2018-09-19 stsp prev_id = id;
336 bee6b577 2018-09-19 stsp * If a branch has created the merged content we can
337 bee6b577 2018-09-19 stsp * skip any other branches.
339 bee6b577 2018-09-19 stsp if (got_object_id_cmp(merged_id, id) == 0) {
340 bee6b577 2018-09-19 stsp err = got_object_idset_add(NULL,
341 bee6b577 2018-09-19 stsp graph->open_branches, qid->id, node);
342 bee6b577 2018-09-19 stsp if (err && err->code != GOT_ERR_OBJ_EXISTS)
343 bee6b577 2018-09-19 stsp return err;
344 bee6b577 2018-09-19 stsp return NULL;
349 bee6b577 2018-09-19 stsp * If the path's content is the same on all branches,
350 bee6b577 2018-09-19 stsp * follow the first parent only.
352 bee6b577 2018-09-19 stsp if (!branches_differ) {
353 bee6b577 2018-09-19 stsp qid = SIMPLEQ_FIRST(&commit->parent_ids);
354 bee6b577 2018-09-19 stsp if (qid == NULL)
355 bee6b577 2018-09-19 stsp return NULL;
356 bee6b577 2018-09-19 stsp if (got_object_idset_get(graph->node_ids, qid->id))
357 bee6b577 2018-09-19 stsp return NULL; /* parent already traversed */
358 bee6b577 2018-09-19 stsp err = got_object_idset_add(NULL,
359 bee6b577 2018-09-19 stsp graph->open_branches, qid->id, node);
360 bee6b577 2018-09-19 stsp if (err && err->code != GOT_ERR_OBJ_EXISTS)
361 bee6b577 2018-09-19 stsp return err;
362 bee6b577 2018-09-19 stsp return NULL;
366 0ed6ed4c 2018-06-13 stsp SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
367 0ed6ed4c 2018-06-13 stsp if (got_object_idset_get(graph->node_ids, qid->id))
368 0ed6ed4c 2018-06-13 stsp continue; /* parent already traversed */
369 0ed6ed4c 2018-06-13 stsp err = got_object_idset_add(NULL, graph->open_branches,
370 0ed6ed4c 2018-06-13 stsp qid->id, node);
371 0ed6ed4c 2018-06-13 stsp if (err && err->code != GOT_ERR_OBJ_EXISTS)
372 0ed6ed4c 2018-06-13 stsp return err;
375 b43fbaa0 2018-06-11 stsp return NULL;
378 fb5b9d5a 2018-07-23 stsp static void
379 fb5b9d5a 2018-07-23 stsp free_node(struct got_commit_graph_node *node)
381 fb5b9d5a 2018-07-23 stsp while (!SIMPLEQ_EMPTY(&node->parent_ids)) {
382 fb5b9d5a 2018-07-23 stsp struct got_object_qid *pid = SIMPLEQ_FIRST(&node->parent_ids);
383 fb5b9d5a 2018-07-23 stsp SIMPLEQ_REMOVE_HEAD(&node->parent_ids, entry);
384 fa2f6902 2018-07-23 stsp got_object_qid_free(pid);
386 fb5b9d5a 2018-07-23 stsp free(node);
389 b43fbaa0 2018-06-11 stsp static const struct got_error *
390 372ccdbb 2018-06-10 stsp add_node(struct got_commit_graph_node **new_node,
391 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph, struct got_object_id *commit_id,
392 31cedeaf 2018-09-15 stsp struct got_commit_object *commit, struct got_commit_graph_node *child_node,
393 31cedeaf 2018-09-15 stsp struct got_repository *repo)
395 372ccdbb 2018-06-10 stsp const struct got_error *err = NULL;
396 f4ceb45e 2018-07-23 stsp struct got_commit_graph_node *node;
397 fa2f6902 2018-07-23 stsp struct got_object_qid *pid;
398 31cedeaf 2018-09-15 stsp int changed = 0, branch_done = 0;
400 372ccdbb 2018-06-10 stsp *new_node = NULL;
402 372ccdbb 2018-06-10 stsp node = calloc(1, sizeof(*node));
403 372ccdbb 2018-06-10 stsp if (node == NULL)
404 372ccdbb 2018-06-10 stsp return got_error_from_errno();
406 372ccdbb 2018-06-10 stsp memcpy(&node->id, commit_id, sizeof(node->id));
407 b43fbaa0 2018-06-11 stsp SIMPLEQ_INIT(&node->parent_ids);
408 fa2f6902 2018-07-23 stsp SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
409 fa2f6902 2018-07-23 stsp err = add_vertex(&node->parent_ids, pid->id);
411 fb5b9d5a 2018-07-23 stsp free_node(node);
412 b43fbaa0 2018-06-11 stsp return err;
414 b43fbaa0 2018-06-11 stsp node->nparents++;
417 f4ceb45e 2018-07-23 stsp err = got_object_idset_add(NULL, graph->node_ids, &node->id, node);
419 f4ceb45e 2018-07-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS)
420 f4ceb45e 2018-07-23 stsp err = NULL;
421 fb5b9d5a 2018-07-23 stsp free_node(node);
422 372ccdbb 2018-06-10 stsp return err;
425 31cedeaf 2018-09-15 stsp err = detect_changed_path(&changed, commit, commit_id, graph->path,
428 31cedeaf 2018-09-15 stsp if (err->code == GOT_ERR_NO_OBJ) {
430 31cedeaf 2018-09-15 stsp * History of the path stops here on the current
431 31cedeaf 2018-09-15 stsp * branch. Keep going on other branches.
433 31cedeaf 2018-09-15 stsp err = NULL;
434 31cedeaf 2018-09-15 stsp branch_done = 1;
436 31cedeaf 2018-09-15 stsp free_node(node);
437 31cedeaf 2018-09-15 stsp return err;
441 bee6b577 2018-09-19 stsp node->commit_timestamp = mktime(&commit->tm_committer);
442 bee6b577 2018-09-19 stsp if (node->commit_timestamp == -1) {
443 bee6b577 2018-09-19 stsp free_node(node);
444 bee6b577 2018-09-19 stsp return got_error_from_errno();
447 bee6b577 2018-09-19 stsp if (changed && !is_merge_point(node))
448 bee6b577 2018-09-19 stsp add_node_to_iter_list(graph, node, child_node);
450 31cedeaf 2018-09-15 stsp if (branch_done)
451 31cedeaf 2018-09-15 stsp err = close_branch(graph, commit_id);
453 bee6b577 2018-09-19 stsp err = advance_branch(graph, node, commit_id, commit, repo);
455 f4ceb45e 2018-07-23 stsp free_node(node);
457 f4ceb45e 2018-07-23 stsp *new_node = node;
459 c4d7a9c4 2018-06-11 stsp return err;
462 c4d7a9c4 2018-06-11 stsp const struct got_error *
463 c4d7a9c4 2018-06-11 stsp got_commit_graph_open(struct got_commit_graph **graph,
464 31cedeaf 2018-09-15 stsp struct got_object_id *commit_id, const char *path,
465 31cedeaf 2018-09-15 stsp int first_parent_traversal, struct got_repository *repo)
467 c4d7a9c4 2018-06-11 stsp const struct got_error *err = NULL;
468 c4d7a9c4 2018-06-11 stsp struct got_commit_object *commit;
470 c4d7a9c4 2018-06-11 stsp *graph = NULL;
472 be6a1b5a 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
474 372ccdbb 2018-06-10 stsp return err;
476 31cedeaf 2018-09-15 stsp /* The path must exist in our initial commit. */
477 31cedeaf 2018-09-15 stsp if (!got_path_is_root_dir(path)) {
478 31cedeaf 2018-09-15 stsp struct got_object_id *obj_id;
479 31cedeaf 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
481 31cedeaf 2018-09-15 stsp return err;
482 31cedeaf 2018-09-15 stsp free(obj_id);
485 31cedeaf 2018-09-15 stsp *graph = alloc_graph(path);
486 372ccdbb 2018-06-10 stsp if (*graph == NULL) {
487 372ccdbb 2018-06-10 stsp got_object_commit_close(commit);
488 372ccdbb 2018-06-10 stsp return got_error_from_errno();
491 0ed6ed4c 2018-06-13 stsp if (first_parent_traversal)
492 0ed6ed4c 2018-06-13 stsp (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
494 31cedeaf 2018-09-15 stsp err = add_node(&(*graph)->head_node, *graph, commit_id, commit, NULL,
496 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
498 372ccdbb 2018-06-10 stsp got_commit_graph_close(*graph);
499 372ccdbb 2018-06-10 stsp *graph = NULL;
500 372ccdbb 2018-06-10 stsp return err;
503 372ccdbb 2018-06-10 stsp return NULL;
506 b565f6f8 2018-07-23 stsp struct gather_branch_tips_arg {
507 b565f6f8 2018-07-23 stsp struct got_commit_graph_branch_tip *tips;
511 372ccdbb 2018-06-10 stsp static void
512 b565f6f8 2018-07-23 stsp gather_branch_tips(struct got_object_id *id, void *data, void *arg)
514 b565f6f8 2018-07-23 stsp struct gather_branch_tips_arg *a = arg;
515 b565f6f8 2018-07-23 stsp memcpy(&a->tips[a->ntips].id, id, sizeof(*id));
516 b565f6f8 2018-07-23 stsp a->tips[a->ntips].node = data;
517 b565f6f8 2018-07-23 stsp a->ntips++;
520 1142eae9 2018-06-11 stsp static const struct got_error *
521 1142eae9 2018-06-11 stsp fetch_commits_from_open_branches(int *ncommits, int *wanted_id_added,
522 31cedeaf 2018-09-15 stsp struct got_object_id **changed_id, struct got_commit_graph *graph,
523 31cedeaf 2018-09-15 stsp struct got_repository *repo, struct got_object_id *wanted_id)
525 372ccdbb 2018-06-10 stsp const struct got_error *err;
526 b565f6f8 2018-07-23 stsp struct gather_branch_tips_arg arg;
529 372ccdbb 2018-06-10 stsp *ncommits = 0;
530 1142eae9 2018-06-11 stsp if (wanted_id_added)
531 1142eae9 2018-06-11 stsp *wanted_id_added = 0;
532 31cedeaf 2018-09-15 stsp if (changed_id)
533 31cedeaf 2018-09-15 stsp *changed_id = NULL;
535 b565f6f8 2018-07-23 stsp arg.ntips = got_object_idset_num_elements(graph->open_branches);
536 b565f6f8 2018-07-23 stsp if (arg.ntips == 0)
537 372ccdbb 2018-06-10 stsp return NULL;
540 372ccdbb 2018-06-10 stsp * Adding nodes to the graph might change the graph's open
541 372ccdbb 2018-06-10 stsp * branches state. Create a local copy of the current state.
543 b565f6f8 2018-07-23 stsp if (graph->ntips < arg.ntips) {
544 b565f6f8 2018-07-23 stsp struct got_commit_graph_branch_tip *tips;
545 b565f6f8 2018-07-23 stsp if (arg.ntips < GOT_COMMIT_GRAPH_MIN_TIPS)
546 b565f6f8 2018-07-23 stsp arg.ntips = GOT_COMMIT_GRAPH_MIN_TIPS;
547 b565f6f8 2018-07-23 stsp tips = reallocarray(graph->tips, arg.ntips, sizeof(*tips));
548 b565f6f8 2018-07-23 stsp if (tips == NULL)
549 b565f6f8 2018-07-23 stsp return got_error_from_errno();
550 b565f6f8 2018-07-23 stsp graph->tips = tips;
551 b565f6f8 2018-07-23 stsp graph->ntips = arg.ntips;
553 b565f6f8 2018-07-23 stsp arg.ntips = 0; /* reset; gather_branch_tips() will increment */
554 b565f6f8 2018-07-23 stsp arg.tips = graph->tips;
555 b565f6f8 2018-07-23 stsp got_object_idset_for_each(graph->open_branches,
556 b565f6f8 2018-07-23 stsp gather_branch_tips, &arg);
558 b565f6f8 2018-07-23 stsp for (i = 0; i < arg.ntips; i++) {
559 372ccdbb 2018-06-10 stsp struct got_object_id *commit_id;
560 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *child_node, *new_node;
561 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
562 31cedeaf 2018-09-15 stsp int changed;
564 b565f6f8 2018-07-23 stsp commit_id = &graph->tips[i].id;
565 b565f6f8 2018-07-23 stsp child_node = graph->tips[i].node;
567 be6a1b5a 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
571 31cedeaf 2018-09-15 stsp err = detect_changed_path(&changed, commit, commit_id,
572 31cedeaf 2018-09-15 stsp graph->path, repo);
574 31cedeaf 2018-09-15 stsp got_object_commit_close(commit);
575 31cedeaf 2018-09-15 stsp if (err->code != GOT_ERR_NO_OBJ)
577 31cedeaf 2018-09-15 stsp err = close_branch(graph, commit_id);
582 31cedeaf 2018-09-15 stsp if (changed && changed_id && *changed_id == NULL)
583 31cedeaf 2018-09-15 stsp *changed_id = commit_id;
584 31cedeaf 2018-09-15 stsp err = add_node(&new_node, graph, commit_id, commit, child_node,
586 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
589 372ccdbb 2018-06-10 stsp if (new_node)
590 372ccdbb 2018-06-10 stsp (*ncommits)++;
591 1142eae9 2018-06-11 stsp if (wanted_id && got_object_id_cmp(commit_id, wanted_id) == 0)
592 1142eae9 2018-06-11 stsp *wanted_id_added = 1;
595 372ccdbb 2018-06-10 stsp return err;
598 372ccdbb 2018-06-10 stsp const struct got_error *
599 31cedeaf 2018-09-15 stsp got_commit_graph_fetch_commits(struct got_commit_graph *graph, int limit,
600 31cedeaf 2018-09-15 stsp struct got_repository *repo)
602 372ccdbb 2018-06-10 stsp const struct got_error *err;
603 31cedeaf 2018-09-15 stsp int nfetched = 0, ncommits;
604 31cedeaf 2018-09-15 stsp struct got_object_id *changed_id = NULL;
606 31cedeaf 2018-09-15 stsp while (nfetched < limit) {
607 1142eae9 2018-06-11 stsp err = fetch_commits_from_open_branches(&ncommits, NULL,
608 31cedeaf 2018-09-15 stsp &changed_id, graph, repo, NULL);
610 372ccdbb 2018-06-10 stsp return err;
611 372ccdbb 2018-06-10 stsp if (ncommits == 0)
613 31cedeaf 2018-09-15 stsp if (changed_id)
614 31cedeaf 2018-09-15 stsp nfetched += ncommits;
617 372ccdbb 2018-06-10 stsp return NULL;
620 1142eae9 2018-06-11 stsp const struct got_error *
621 1142eae9 2018-06-11 stsp got_commit_graph_fetch_commits_up_to(int *nfetched,
622 1142eae9 2018-06-11 stsp struct got_commit_graph *graph, struct got_object_id *wanted_id,
623 1142eae9 2018-06-11 stsp struct got_repository *repo)
625 1142eae9 2018-06-11 stsp const struct got_error *err;
626 1142eae9 2018-06-11 stsp int ncommits, wanted_id_added = 0;
628 1142eae9 2018-06-11 stsp *nfetched = 0;
630 6e0c0f9a 2018-06-11 stsp if (got_object_idset_get(graph->node_ids, wanted_id) != NULL)
631 6e0c0f9a 2018-06-11 stsp return NULL;
633 1142eae9 2018-06-11 stsp while (!wanted_id_added) {
634 1142eae9 2018-06-11 stsp err = fetch_commits_from_open_branches(&ncommits,
635 31cedeaf 2018-09-15 stsp &wanted_id_added, NULL, graph, repo, wanted_id);
637 1142eae9 2018-06-11 stsp return err;
638 1142eae9 2018-06-11 stsp if (ncommits == 0)
639 1142eae9 2018-06-11 stsp return NULL;
640 1142eae9 2018-06-11 stsp *nfetched += ncommits;
643 1142eae9 2018-06-11 stsp return NULL;
646 372ccdbb 2018-06-10 stsp static void
647 fb5b9d5a 2018-07-23 stsp free_node_iter(struct got_object_id *id, void *data, void *arg)
649 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *node = data;
650 fb5b9d5a 2018-07-23 stsp free_node(node);
654 372ccdbb 2018-06-10 stsp got_commit_graph_close(struct got_commit_graph *graph)
656 372ccdbb 2018-06-10 stsp got_object_idset_free(graph->open_branches);
657 fb5b9d5a 2018-07-23 stsp got_object_idset_for_each(graph->node_ids, free_node_iter, NULL);
658 372ccdbb 2018-06-10 stsp got_object_idset_free(graph->node_ids);
659 b565f6f8 2018-07-23 stsp free(graph->tips);
660 31cedeaf 2018-09-15 stsp free(graph->path);
661 372ccdbb 2018-06-10 stsp free(graph);
664 372ccdbb 2018-06-10 stsp const struct got_error *
665 372ccdbb 2018-06-10 stsp got_commit_graph_iter_start(struct got_commit_graph *graph,
666 31cedeaf 2018-09-15 stsp struct got_object_id *id, struct got_repository *repo)
668 31cedeaf 2018-09-15 stsp const struct got_error *err = NULL;
669 9ba79e04 2018-06-11 stsp struct got_commit_graph_node *start_node;
670 31cedeaf 2018-09-15 stsp struct got_commit_object *commit;
671 31cedeaf 2018-09-15 stsp int changed;
673 372ccdbb 2018-06-10 stsp start_node = got_object_idset_get(graph->node_ids, id);
674 372ccdbb 2018-06-10 stsp if (start_node == NULL)
675 372ccdbb 2018-06-10 stsp return got_error(GOT_ERR_NO_OBJ);
678 31cedeaf 2018-09-15 stsp err = got_object_open_as_commit(&commit, repo, &start_node->id);
680 31cedeaf 2018-09-15 stsp return err;
682 31cedeaf 2018-09-15 stsp err = detect_changed_path(&changed, commit, &start_node->id,
683 31cedeaf 2018-09-15 stsp graph->path, repo);
685 31cedeaf 2018-09-15 stsp got_object_commit_close(commit);
686 31cedeaf 2018-09-15 stsp return err;
689 31cedeaf 2018-09-15 stsp if (!changed) {
690 31cedeaf 2018-09-15 stsp /* Locate first commit which changed graph->path. */
691 31cedeaf 2018-09-15 stsp struct got_object_id *changed_id = NULL;
692 31cedeaf 2018-09-15 stsp while (changed_id == NULL) {
693 31cedeaf 2018-09-15 stsp int ncommits;
694 31cedeaf 2018-09-15 stsp err = fetch_commits_from_open_branches(&ncommits, NULL,
695 31cedeaf 2018-09-15 stsp &changed_id, graph, repo, NULL);
697 31cedeaf 2018-09-15 stsp got_object_commit_close(commit);
698 31cedeaf 2018-09-15 stsp return err;
701 31cedeaf 2018-09-15 stsp start_node = got_object_idset_get(graph->node_ids, changed_id);
703 31cedeaf 2018-09-15 stsp got_object_commit_close(commit);
705 372ccdbb 2018-06-10 stsp graph->iter_node = start_node;
706 372ccdbb 2018-06-10 stsp return NULL;
709 372ccdbb 2018-06-10 stsp const struct got_error *
710 b43fbaa0 2018-06-11 stsp got_commit_graph_iter_next(struct got_object_id **id,
711 b43fbaa0 2018-06-11 stsp struct got_commit_graph *graph)
713 9ba79e04 2018-06-11 stsp *id = NULL;
715 372ccdbb 2018-06-10 stsp if (graph->iter_node == NULL) {
716 2c7f8870 2018-09-19 stsp /* We are done iterating, or iteration was not started. */
717 9ba79e04 2018-06-11 stsp return got_error(GOT_ERR_ITER_COMPLETED);
720 9ba79e04 2018-06-11 stsp if (graph->iter_node ==
721 9ba79e04 2018-06-11 stsp TAILQ_LAST(&graph->iter_list, got_commit_graph_iter_list) &&
722 9ba79e04 2018-06-11 stsp got_object_idset_num_elements(graph->open_branches) == 0) {
723 2c7f8870 2018-09-19 stsp /* We are done iterating. */
724 028d7a4e 2018-09-19 stsp if (graph->iter_node == TAILQ_FIRST(&graph->iter_list))
725 028d7a4e 2018-09-19 stsp *id = &graph->iter_node->id;
727 028d7a4e 2018-09-19 stsp *id = NULL;
728 9ba79e04 2018-06-11 stsp graph->iter_node = NULL;
729 028d7a4e 2018-09-19 stsp return *id ? NULL : got_error(GOT_ERR_ITER_COMPLETED);
732 9ba79e04 2018-06-11 stsp if (TAILQ_NEXT(graph->iter_node, entry) == NULL)
733 372ccdbb 2018-06-10 stsp return got_error(GOT_ERR_ITER_NEED_MORE);
735 bee6b577 2018-09-19 stsp if (graph->iter_node == TAILQ_FIRST(&graph->iter_list)) {
736 bee6b577 2018-09-19 stsp *id = &graph->iter_node->id;
737 bee6b577 2018-09-19 stsp graph->iter_node = TAILQ_NEXT(graph->iter_node, entry);
739 bee6b577 2018-09-19 stsp graph->iter_node = TAILQ_NEXT(graph->iter_node, entry);
740 bee6b577 2018-09-19 stsp *id = &graph->iter_node->id;
743 372ccdbb 2018-06-10 stsp return NULL;