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 8d49203e 2018-09-20 stsp #define GOT_COMMIT_GRAPH_MIN_TIPS 10 /* 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 93e45b7c 2018-09-24 stsp TAILQ_INSERT_TAIL(&graph->iter_list, 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;
306 bee6b577 2018-09-19 stsp err = got_object_id_by_path(&merged_id, repo, commit_id,
307 bee6b577 2018-09-19 stsp graph->path);
309 bee6b577 2018-09-19 stsp return err;
311 bee6b577 2018-09-19 stsp SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
312 bee6b577 2018-09-19 stsp if (got_object_idset_get(graph->node_ids, qid->id))
313 bee6b577 2018-09-19 stsp continue; /* parent already traversed */
315 bee6b577 2018-09-19 stsp err = got_object_id_by_path(&id, repo, qid->id,
316 bee6b577 2018-09-19 stsp graph->path);
318 bee6b577 2018-09-19 stsp if (err->code == GOT_ERR_NO_OBJ) {
319 bee6b577 2018-09-19 stsp branches_differ = 1;
322 bee6b577 2018-09-19 stsp return err;
325 bee6b577 2018-09-19 stsp if (prev_id) {
326 bee6b577 2018-09-19 stsp if (!branches_differ &&
327 bee6b577 2018-09-19 stsp got_object_id_cmp(merged_id, prev_id) != 0)
328 bee6b577 2018-09-19 stsp branches_differ = 1;
330 bee6b577 2018-09-19 stsp prev_id = id;
333 bee6b577 2018-09-19 stsp * If a branch has created the merged content we can
334 bee6b577 2018-09-19 stsp * skip any other branches.
336 bee6b577 2018-09-19 stsp if (got_object_id_cmp(merged_id, id) == 0) {
337 bee6b577 2018-09-19 stsp err = got_object_idset_add(NULL,
338 bee6b577 2018-09-19 stsp graph->open_branches, qid->id, node);
339 bee6b577 2018-09-19 stsp if (err && err->code != GOT_ERR_OBJ_EXISTS)
340 bee6b577 2018-09-19 stsp return err;
341 bee6b577 2018-09-19 stsp return NULL;
346 bee6b577 2018-09-19 stsp * If the path's content is the same on all branches,
347 bee6b577 2018-09-19 stsp * follow the first parent only.
349 bee6b577 2018-09-19 stsp if (!branches_differ) {
350 bee6b577 2018-09-19 stsp qid = SIMPLEQ_FIRST(&commit->parent_ids);
351 bee6b577 2018-09-19 stsp if (qid == NULL)
352 bee6b577 2018-09-19 stsp return NULL;
353 bee6b577 2018-09-19 stsp if (got_object_idset_get(graph->node_ids, qid->id))
354 bee6b577 2018-09-19 stsp return NULL; /* parent already traversed */
355 bee6b577 2018-09-19 stsp err = got_object_idset_add(NULL,
356 bee6b577 2018-09-19 stsp graph->open_branches, qid->id, node);
357 bee6b577 2018-09-19 stsp if (err && err->code != GOT_ERR_OBJ_EXISTS)
358 bee6b577 2018-09-19 stsp return err;
359 bee6b577 2018-09-19 stsp return NULL;
363 0ed6ed4c 2018-06-13 stsp SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
364 0ed6ed4c 2018-06-13 stsp if (got_object_idset_get(graph->node_ids, qid->id))
365 0ed6ed4c 2018-06-13 stsp continue; /* parent already traversed */
366 0ed6ed4c 2018-06-13 stsp err = got_object_idset_add(NULL, graph->open_branches,
367 0ed6ed4c 2018-06-13 stsp qid->id, node);
368 0ed6ed4c 2018-06-13 stsp if (err && err->code != GOT_ERR_OBJ_EXISTS)
369 0ed6ed4c 2018-06-13 stsp return err;
372 b43fbaa0 2018-06-11 stsp return NULL;
375 fb5b9d5a 2018-07-23 stsp static void
376 fb5b9d5a 2018-07-23 stsp free_node(struct got_commit_graph_node *node)
378 fb5b9d5a 2018-07-23 stsp while (!SIMPLEQ_EMPTY(&node->parent_ids)) {
379 fb5b9d5a 2018-07-23 stsp struct got_object_qid *pid = SIMPLEQ_FIRST(&node->parent_ids);
380 fb5b9d5a 2018-07-23 stsp SIMPLEQ_REMOVE_HEAD(&node->parent_ids, entry);
381 fa2f6902 2018-07-23 stsp got_object_qid_free(pid);
383 fb5b9d5a 2018-07-23 stsp free(node);
386 b43fbaa0 2018-06-11 stsp static const struct got_error *
387 372ccdbb 2018-06-10 stsp add_node(struct got_commit_graph_node **new_node,
388 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph, struct got_object_id *commit_id,
389 31cedeaf 2018-09-15 stsp struct got_commit_object *commit, struct got_commit_graph_node *child_node,
390 31cedeaf 2018-09-15 stsp struct got_repository *repo)
392 372ccdbb 2018-06-10 stsp const struct got_error *err = NULL;
393 f4ceb45e 2018-07-23 stsp struct got_commit_graph_node *node;
394 fa2f6902 2018-07-23 stsp struct got_object_qid *pid;
395 31cedeaf 2018-09-15 stsp int changed = 0, branch_done = 0;
397 372ccdbb 2018-06-10 stsp *new_node = NULL;
399 372ccdbb 2018-06-10 stsp node = calloc(1, sizeof(*node));
400 372ccdbb 2018-06-10 stsp if (node == NULL)
401 372ccdbb 2018-06-10 stsp return got_error_from_errno();
403 372ccdbb 2018-06-10 stsp memcpy(&node->id, commit_id, sizeof(node->id));
404 b43fbaa0 2018-06-11 stsp SIMPLEQ_INIT(&node->parent_ids);
405 fa2f6902 2018-07-23 stsp SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
406 fa2f6902 2018-07-23 stsp err = add_vertex(&node->parent_ids, pid->id);
408 fb5b9d5a 2018-07-23 stsp free_node(node);
409 b43fbaa0 2018-06-11 stsp return err;
411 b43fbaa0 2018-06-11 stsp node->nparents++;
414 f4ceb45e 2018-07-23 stsp err = got_object_idset_add(NULL, graph->node_ids, &node->id, node);
416 f4ceb45e 2018-07-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS)
417 f4ceb45e 2018-07-23 stsp err = NULL;
418 fb5b9d5a 2018-07-23 stsp free_node(node);
419 372ccdbb 2018-06-10 stsp return err;
422 31cedeaf 2018-09-15 stsp err = detect_changed_path(&changed, commit, commit_id, graph->path,
425 31cedeaf 2018-09-15 stsp if (err->code == GOT_ERR_NO_OBJ) {
427 31cedeaf 2018-09-15 stsp * History of the path stops here on the current
428 31cedeaf 2018-09-15 stsp * branch. Keep going on other branches.
430 31cedeaf 2018-09-15 stsp err = NULL;
431 31cedeaf 2018-09-15 stsp branch_done = 1;
433 31cedeaf 2018-09-15 stsp free_node(node);
434 31cedeaf 2018-09-15 stsp return err;
438 bee6b577 2018-09-19 stsp node->commit_timestamp = mktime(&commit->tm_committer);
439 bee6b577 2018-09-19 stsp if (node->commit_timestamp == -1) {
440 bee6b577 2018-09-19 stsp free_node(node);
441 bee6b577 2018-09-19 stsp return got_error_from_errno();
444 bee6b577 2018-09-19 stsp if (changed && !is_merge_point(node))
445 bee6b577 2018-09-19 stsp add_node_to_iter_list(graph, node, child_node);
447 31cedeaf 2018-09-15 stsp if (branch_done)
448 31cedeaf 2018-09-15 stsp err = close_branch(graph, commit_id);
450 bee6b577 2018-09-19 stsp err = advance_branch(graph, node, commit_id, commit, repo);
452 f4ceb45e 2018-07-23 stsp free_node(node);
454 f4ceb45e 2018-07-23 stsp *new_node = node;
456 c4d7a9c4 2018-06-11 stsp return err;
459 c4d7a9c4 2018-06-11 stsp const struct got_error *
460 c4d7a9c4 2018-06-11 stsp got_commit_graph_open(struct got_commit_graph **graph,
461 31cedeaf 2018-09-15 stsp struct got_object_id *commit_id, const char *path,
462 31cedeaf 2018-09-15 stsp int first_parent_traversal, struct got_repository *repo)
464 c4d7a9c4 2018-06-11 stsp const struct got_error *err = NULL;
465 c4d7a9c4 2018-06-11 stsp struct got_commit_object *commit;
467 c4d7a9c4 2018-06-11 stsp *graph = NULL;
469 be6a1b5a 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
471 372ccdbb 2018-06-10 stsp return err;
473 31cedeaf 2018-09-15 stsp /* The path must exist in our initial commit. */
474 31cedeaf 2018-09-15 stsp if (!got_path_is_root_dir(path)) {
475 31cedeaf 2018-09-15 stsp struct got_object_id *obj_id;
476 31cedeaf 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
478 31cedeaf 2018-09-15 stsp return err;
479 31cedeaf 2018-09-15 stsp free(obj_id);
482 31cedeaf 2018-09-15 stsp *graph = alloc_graph(path);
483 372ccdbb 2018-06-10 stsp if (*graph == NULL) {
484 372ccdbb 2018-06-10 stsp got_object_commit_close(commit);
485 372ccdbb 2018-06-10 stsp return got_error_from_errno();
488 0ed6ed4c 2018-06-13 stsp if (first_parent_traversal)
489 0ed6ed4c 2018-06-13 stsp (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
491 31cedeaf 2018-09-15 stsp err = add_node(&(*graph)->head_node, *graph, commit_id, commit, NULL,
493 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
495 372ccdbb 2018-06-10 stsp got_commit_graph_close(*graph);
496 372ccdbb 2018-06-10 stsp *graph = NULL;
497 372ccdbb 2018-06-10 stsp return err;
500 372ccdbb 2018-06-10 stsp return NULL;
503 b565f6f8 2018-07-23 stsp struct gather_branch_tips_arg {
504 b565f6f8 2018-07-23 stsp struct got_commit_graph_branch_tip *tips;
508 372ccdbb 2018-06-10 stsp static void
509 b565f6f8 2018-07-23 stsp gather_branch_tips(struct got_object_id *id, void *data, void *arg)
511 b565f6f8 2018-07-23 stsp struct gather_branch_tips_arg *a = arg;
512 b565f6f8 2018-07-23 stsp memcpy(&a->tips[a->ntips].id, id, sizeof(*id));
513 b565f6f8 2018-07-23 stsp a->tips[a->ntips].node = data;
514 b565f6f8 2018-07-23 stsp a->ntips++;
517 1142eae9 2018-06-11 stsp static const struct got_error *
518 1142eae9 2018-06-11 stsp fetch_commits_from_open_branches(int *ncommits, int *wanted_id_added,
519 31cedeaf 2018-09-15 stsp struct got_object_id **changed_id, struct got_commit_graph *graph,
520 31cedeaf 2018-09-15 stsp struct got_repository *repo, struct got_object_id *wanted_id)
522 372ccdbb 2018-06-10 stsp const struct got_error *err;
523 b565f6f8 2018-07-23 stsp struct gather_branch_tips_arg arg;
526 372ccdbb 2018-06-10 stsp *ncommits = 0;
527 1142eae9 2018-06-11 stsp if (wanted_id_added)
528 1142eae9 2018-06-11 stsp *wanted_id_added = 0;
529 31cedeaf 2018-09-15 stsp if (changed_id)
530 31cedeaf 2018-09-15 stsp *changed_id = NULL;
532 b565f6f8 2018-07-23 stsp arg.ntips = got_object_idset_num_elements(graph->open_branches);
533 b565f6f8 2018-07-23 stsp if (arg.ntips == 0)
534 372ccdbb 2018-06-10 stsp return NULL;
537 372ccdbb 2018-06-10 stsp * Adding nodes to the graph might change the graph's open
538 372ccdbb 2018-06-10 stsp * branches state. Create a local copy of the current state.
540 b565f6f8 2018-07-23 stsp if (graph->ntips < arg.ntips) {
541 b565f6f8 2018-07-23 stsp struct got_commit_graph_branch_tip *tips;
542 b565f6f8 2018-07-23 stsp if (arg.ntips < GOT_COMMIT_GRAPH_MIN_TIPS)
543 b565f6f8 2018-07-23 stsp arg.ntips = GOT_COMMIT_GRAPH_MIN_TIPS;
544 b565f6f8 2018-07-23 stsp tips = reallocarray(graph->tips, arg.ntips, sizeof(*tips));
545 b565f6f8 2018-07-23 stsp if (tips == NULL)
546 b565f6f8 2018-07-23 stsp return got_error_from_errno();
547 b565f6f8 2018-07-23 stsp graph->tips = tips;
548 b565f6f8 2018-07-23 stsp graph->ntips = arg.ntips;
550 b565f6f8 2018-07-23 stsp arg.ntips = 0; /* reset; gather_branch_tips() will increment */
551 b565f6f8 2018-07-23 stsp arg.tips = graph->tips;
552 b565f6f8 2018-07-23 stsp got_object_idset_for_each(graph->open_branches,
553 b565f6f8 2018-07-23 stsp gather_branch_tips, &arg);
555 b565f6f8 2018-07-23 stsp for (i = 0; i < arg.ntips; i++) {
556 372ccdbb 2018-06-10 stsp struct got_object_id *commit_id;
557 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *child_node, *new_node;
558 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
559 31cedeaf 2018-09-15 stsp int changed;
561 b565f6f8 2018-07-23 stsp commit_id = &graph->tips[i].id;
562 b565f6f8 2018-07-23 stsp child_node = graph->tips[i].node;
564 be6a1b5a 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
568 31cedeaf 2018-09-15 stsp err = detect_changed_path(&changed, commit, commit_id,
569 31cedeaf 2018-09-15 stsp graph->path, repo);
571 31cedeaf 2018-09-15 stsp got_object_commit_close(commit);
572 31cedeaf 2018-09-15 stsp if (err->code != GOT_ERR_NO_OBJ)
574 31cedeaf 2018-09-15 stsp err = close_branch(graph, commit_id);
579 31cedeaf 2018-09-15 stsp if (changed && changed_id && *changed_id == NULL)
580 31cedeaf 2018-09-15 stsp *changed_id = commit_id;
581 31cedeaf 2018-09-15 stsp err = add_node(&new_node, graph, commit_id, commit, child_node,
583 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
586 372ccdbb 2018-06-10 stsp if (new_node)
587 372ccdbb 2018-06-10 stsp (*ncommits)++;
588 1142eae9 2018-06-11 stsp if (wanted_id && got_object_id_cmp(commit_id, wanted_id) == 0)
589 1142eae9 2018-06-11 stsp *wanted_id_added = 1;
592 372ccdbb 2018-06-10 stsp return err;
595 372ccdbb 2018-06-10 stsp const struct got_error *
596 31cedeaf 2018-09-15 stsp got_commit_graph_fetch_commits(struct got_commit_graph *graph, int limit,
597 31cedeaf 2018-09-15 stsp struct got_repository *repo)
599 372ccdbb 2018-06-10 stsp const struct got_error *err;
600 31cedeaf 2018-09-15 stsp int nfetched = 0, ncommits;
601 31cedeaf 2018-09-15 stsp struct got_object_id *changed_id = NULL;
603 31cedeaf 2018-09-15 stsp while (nfetched < limit) {
604 1142eae9 2018-06-11 stsp err = fetch_commits_from_open_branches(&ncommits, NULL,
605 31cedeaf 2018-09-15 stsp &changed_id, graph, repo, NULL);
607 372ccdbb 2018-06-10 stsp return err;
608 372ccdbb 2018-06-10 stsp if (ncommits == 0)
610 31cedeaf 2018-09-15 stsp if (changed_id)
611 31cedeaf 2018-09-15 stsp nfetched += ncommits;
614 372ccdbb 2018-06-10 stsp return NULL;
617 372ccdbb 2018-06-10 stsp static void
618 fb5b9d5a 2018-07-23 stsp free_node_iter(struct got_object_id *id, void *data, void *arg)
620 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *node = data;
621 fb5b9d5a 2018-07-23 stsp free_node(node);
625 372ccdbb 2018-06-10 stsp got_commit_graph_close(struct got_commit_graph *graph)
627 372ccdbb 2018-06-10 stsp got_object_idset_free(graph->open_branches);
628 fb5b9d5a 2018-07-23 stsp got_object_idset_for_each(graph->node_ids, free_node_iter, NULL);
629 372ccdbb 2018-06-10 stsp got_object_idset_free(graph->node_ids);
630 b565f6f8 2018-07-23 stsp free(graph->tips);
631 31cedeaf 2018-09-15 stsp free(graph->path);
632 372ccdbb 2018-06-10 stsp free(graph);
635 372ccdbb 2018-06-10 stsp const struct got_error *
636 372ccdbb 2018-06-10 stsp got_commit_graph_iter_start(struct got_commit_graph *graph,
637 31cedeaf 2018-09-15 stsp struct got_object_id *id, struct got_repository *repo)
639 31cedeaf 2018-09-15 stsp const struct got_error *err = NULL;
640 9ba79e04 2018-06-11 stsp struct got_commit_graph_node *start_node;
641 31cedeaf 2018-09-15 stsp struct got_commit_object *commit;
642 31cedeaf 2018-09-15 stsp int changed;
644 372ccdbb 2018-06-10 stsp start_node = got_object_idset_get(graph->node_ids, id);
645 372ccdbb 2018-06-10 stsp if (start_node == NULL)
646 372ccdbb 2018-06-10 stsp return got_error(GOT_ERR_NO_OBJ);
648 31cedeaf 2018-09-15 stsp err = got_object_open_as_commit(&commit, repo, &start_node->id);
650 31cedeaf 2018-09-15 stsp return err;
652 31cedeaf 2018-09-15 stsp err = detect_changed_path(&changed, commit, &start_node->id,
653 31cedeaf 2018-09-15 stsp graph->path, repo);
655 31cedeaf 2018-09-15 stsp got_object_commit_close(commit);
656 31cedeaf 2018-09-15 stsp return err;
659 31cedeaf 2018-09-15 stsp if (!changed) {
660 31cedeaf 2018-09-15 stsp /* Locate first commit which changed graph->path. */
661 31cedeaf 2018-09-15 stsp struct got_object_id *changed_id = NULL;
662 31cedeaf 2018-09-15 stsp while (changed_id == NULL) {
663 31cedeaf 2018-09-15 stsp int ncommits;
664 31cedeaf 2018-09-15 stsp err = fetch_commits_from_open_branches(&ncommits, NULL,
665 31cedeaf 2018-09-15 stsp &changed_id, graph, repo, NULL);
667 31cedeaf 2018-09-15 stsp got_object_commit_close(commit);
668 31cedeaf 2018-09-15 stsp return err;
671 31cedeaf 2018-09-15 stsp start_node = got_object_idset_get(graph->node_ids, changed_id);
673 31cedeaf 2018-09-15 stsp got_object_commit_close(commit);
675 372ccdbb 2018-06-10 stsp graph->iter_node = start_node;
676 372ccdbb 2018-06-10 stsp return NULL;
679 372ccdbb 2018-06-10 stsp const struct got_error *
680 b43fbaa0 2018-06-11 stsp got_commit_graph_iter_next(struct got_object_id **id,
681 b43fbaa0 2018-06-11 stsp struct got_commit_graph *graph)
683 9ba79e04 2018-06-11 stsp *id = NULL;
685 372ccdbb 2018-06-10 stsp if (graph->iter_node == NULL) {
686 2c7f8870 2018-09-19 stsp /* We are done iterating, or iteration was not started. */
687 9ba79e04 2018-06-11 stsp return got_error(GOT_ERR_ITER_COMPLETED);
690 9ba79e04 2018-06-11 stsp if (graph->iter_node ==
691 9ba79e04 2018-06-11 stsp TAILQ_LAST(&graph->iter_list, got_commit_graph_iter_list) &&
692 9ba79e04 2018-06-11 stsp got_object_idset_num_elements(graph->open_branches) == 0) {
693 2c7f8870 2018-09-19 stsp /* We are done iterating. */
694 6bc66952 2018-09-19 stsp *id = &graph->iter_node->id;
695 9ba79e04 2018-06-11 stsp graph->iter_node = NULL;
696 6bc66952 2018-09-19 stsp return NULL;
699 9ba79e04 2018-06-11 stsp if (TAILQ_NEXT(graph->iter_node, entry) == NULL)
700 372ccdbb 2018-06-10 stsp return got_error(GOT_ERR_ITER_NEED_MORE);
702 6bc66952 2018-09-19 stsp *id = &graph->iter_node->id;
703 6bc66952 2018-09-19 stsp graph->iter_node = TAILQ_NEXT(graph->iter_node, entry);
704 372ccdbb 2018-06-10 stsp return NULL;