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 32777563 2018-11-07 stsp struct got_object_id *commit_id;
60 32777563 2018-11-07 stsp struct got_commit_object *commit;
61 32777563 2018-11-07 stsp struct got_commit_graph_node *new_node;
62 32777563 2018-11-07 stsp int changed;
63 32777563 2018-11-07 stsp int branch_done;
66 372ccdbb 2018-06-10 stsp struct got_commit_graph {
67 372ccdbb 2018-06-10 stsp /* The set of all commits we have traversed. */
68 372ccdbb 2018-06-10 stsp struct got_object_idset *node_ids;
70 372ccdbb 2018-06-10 stsp /* The commit at which traversal began (youngest commit in node_ids). */
71 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *head_node;
74 0ed6ed4c 2018-06-13 stsp #define GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL 0x01
77 372ccdbb 2018-06-10 stsp * A set of object IDs of known parent commits which we have not yet
78 372ccdbb 2018-06-10 stsp * traversed. Each commit ID in this set represents a branch in commit
79 372ccdbb 2018-06-10 stsp * history: Either the first-parent branch of the head node, or another
80 372ccdbb 2018-06-10 stsp * branch corresponding to a traversed merge commit for which we have
81 372ccdbb 2018-06-10 stsp * not traversed a branch point commit yet.
83 372ccdbb 2018-06-10 stsp * Whenever we add a commit with a matching ID to the graph, we remove
84 372ccdbb 2018-06-10 stsp * its corresponding element from this set, and add new elements for
85 372ccdbb 2018-06-10 stsp * each of that commit's parent commits which were not traversed yet.
87 372ccdbb 2018-06-10 stsp * When API users ask us to fetch more commits, we fetch commits from
88 372ccdbb 2018-06-10 stsp * all currently open branches. This allows API users to process
89 372ccdbb 2018-06-10 stsp * commits in linear order even though the history contains branches.
91 372ccdbb 2018-06-10 stsp struct got_object_idset *open_branches;
93 32777563 2018-11-07 stsp /* Array of branch tips for fetch_commits_from_open_branches(). */
94 b565f6f8 2018-07-23 stsp struct got_commit_graph_branch_tip *tips;
97 31cedeaf 2018-09-15 stsp /* Path of tree entry of interest to the API user. */
100 372ccdbb 2018-06-10 stsp /* The next commit to return when the API user asks for one. */
101 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *iter_node;
103 9ba79e04 2018-06-11 stsp /* The graph iteration list contains all nodes in sorted order. */
104 9ba79e04 2018-06-11 stsp struct got_commit_graph_iter_list iter_list;
107 372ccdbb 2018-06-10 stsp static struct got_commit_graph *
108 31cedeaf 2018-09-15 stsp alloc_graph(const char *path)
110 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
112 372ccdbb 2018-06-10 stsp graph = calloc(1, sizeof(*graph));
113 372ccdbb 2018-06-10 stsp if (graph == NULL)
114 372ccdbb 2018-06-10 stsp return NULL;
116 31cedeaf 2018-09-15 stsp graph->path = strdup(path);
117 31cedeaf 2018-09-15 stsp if (graph->path == NULL) {
118 31cedeaf 2018-09-15 stsp free(graph);
119 31cedeaf 2018-09-15 stsp return NULL;
122 60f2eee1 2018-07-08 stsp graph->node_ids = got_object_idset_alloc();
123 372ccdbb 2018-06-10 stsp if (graph->node_ids == NULL) {
124 31cedeaf 2018-09-15 stsp free(graph->path);
125 372ccdbb 2018-06-10 stsp free(graph);
126 372ccdbb 2018-06-10 stsp return NULL;
129 60f2eee1 2018-07-08 stsp graph->open_branches = got_object_idset_alloc();
130 372ccdbb 2018-06-10 stsp if (graph->open_branches == NULL) {
131 372ccdbb 2018-06-10 stsp got_object_idset_free(graph->node_ids);
132 31cedeaf 2018-09-15 stsp free(graph->path);
133 372ccdbb 2018-06-10 stsp free(graph);
134 372ccdbb 2018-06-10 stsp return NULL;
137 31920504 2018-06-11 stsp TAILQ_INIT(&graph->iter_list);
138 372ccdbb 2018-06-10 stsp return graph;
143 372ccdbb 2018-06-10 stsp is_head_node(struct got_commit_graph_node *node)
145 372ccdbb 2018-06-10 stsp return node->nchildren == 0;
149 372ccdbb 2018-06-10 stsp is_branch_point(struct got_commit_graph_node *node)
151 372ccdbb 2018-06-10 stsp return node->nchildren > 1;
155 372ccdbb 2018-06-10 stsp is_root_node(struct got_commit_graph_node *node)
157 b43fbaa0 2018-06-11 stsp return node->nparents == 0;
162 bee6b577 2018-09-19 stsp is_merge_point(struct got_commit_graph_node *node)
164 bee6b577 2018-09-19 stsp return node->nparents > 1;
167 31cedeaf 2018-09-15 stsp static const struct got_error *
168 41fa1437 2018-11-05 stsp detect_changed_path(int *changed, struct got_commit_object *commit,
169 31cedeaf 2018-09-15 stsp struct got_object_id *commit_id, const char *path,
170 31cedeaf 2018-09-15 stsp struct got_repository *repo)
172 31cedeaf 2018-09-15 stsp const struct got_error *err = NULL;
173 41fa1437 2018-11-05 stsp struct got_commit_object *pcommit = NULL;
174 31cedeaf 2018-09-15 stsp struct got_tree_object *tree = NULL, *ptree = NULL;
175 31cedeaf 2018-09-15 stsp struct got_object_qid *pid;
177 31cedeaf 2018-09-15 stsp if (got_path_is_root_dir(path)) {
178 31cedeaf 2018-09-15 stsp *changed = 1;
179 31cedeaf 2018-09-15 stsp return NULL;
182 31cedeaf 2018-09-15 stsp *changed = 0;
184 31cedeaf 2018-09-15 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
186 31cedeaf 2018-09-15 stsp return err;
188 31cedeaf 2018-09-15 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
189 31cedeaf 2018-09-15 stsp if (pid == NULL) {
190 31cedeaf 2018-09-15 stsp struct got_object_id *obj_id;
191 31cedeaf 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
193 d1451975 2018-11-11 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
194 fd1d2703 2018-11-04 stsp err = NULL;
196 fd1d2703 2018-11-04 stsp *changed = 1; /* The path was created in this commit. */
197 31cedeaf 2018-09-15 stsp free(obj_id);
199 41fa1437 2018-11-05 stsp err = got_object_open_as_commit(&pcommit, repo, pid->id);
203 fd1d2703 2018-11-04 stsp err = got_object_open_as_tree(&ptree, repo, pcommit->tree_id);
207 fd1d2703 2018-11-04 stsp err = got_object_tree_path_changed(changed, tree, ptree, path,
212 31cedeaf 2018-09-15 stsp got_object_tree_close(tree);
214 31cedeaf 2018-09-15 stsp got_object_tree_close(ptree);
215 31cedeaf 2018-09-15 stsp if (pcommit)
216 41fa1437 2018-11-05 stsp got_object_commit_close(pcommit);
217 31cedeaf 2018-09-15 stsp return err;
220 4626e416 2018-06-10 stsp static void
221 9ba79e04 2018-06-11 stsp add_node_to_iter_list(struct got_commit_graph *graph,
222 9ba79e04 2018-06-11 stsp struct got_commit_graph_node *node,
223 9ba79e04 2018-06-11 stsp struct got_commit_graph_node *child_node)
225 4bd3f2bb 2018-06-10 stsp struct got_commit_graph_node *n, *next;
227 31920504 2018-06-11 stsp if (TAILQ_EMPTY(&graph->iter_list)) {
228 bee6b577 2018-09-19 stsp TAILQ_INSERT_HEAD(&graph->iter_list, node, entry);
229 bee6b577 2018-09-19 stsp graph->iter_node = node;
233 bee6b577 2018-09-19 stsp n = graph->iter_node;
234 fe8df4c2 2018-06-16 stsp /* Ensure that an iteration in progress will see this new commit. */
235 bee6b577 2018-09-19 stsp while (n) {
236 bee6b577 2018-09-19 stsp next = TAILQ_NEXT(n, entry);
237 bee6b577 2018-09-19 stsp if (next && node->commit_timestamp >= next->commit_timestamp) {
238 bee6b577 2018-09-19 stsp TAILQ_INSERT_BEFORE(next, node, entry);
243 93e45b7c 2018-09-24 stsp TAILQ_INSERT_TAIL(&graph->iter_list, node, entry);
246 372ccdbb 2018-06-10 stsp static const struct got_error *
247 79f35eb3 2018-06-11 stsp add_vertex(struct got_object_id_queue *ids, struct got_object_id *id)
249 5df4932d 2018-11-05 stsp const struct got_error *err = NULL;
250 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
252 5df4932d 2018-11-05 stsp err = got_object_qid_alloc(&qid, id);
254 b43fbaa0 2018-06-11 stsp return err;
256 79f35eb3 2018-06-11 stsp SIMPLEQ_INSERT_TAIL(ids, qid, entry);
257 0ed6ed4c 2018-06-13 stsp return NULL;
260 0ed6ed4c 2018-06-13 stsp static const struct got_error *
261 31cedeaf 2018-09-15 stsp close_branch(struct got_commit_graph *graph, struct got_object_id *commit_id)
263 31cedeaf 2018-09-15 stsp const struct got_error *err;
265 31cedeaf 2018-09-15 stsp err = got_object_idset_remove(NULL, graph->open_branches, commit_id);
266 31cedeaf 2018-09-15 stsp if (err && err->code != GOT_ERR_NO_OBJ)
267 31cedeaf 2018-09-15 stsp return err;
268 31cedeaf 2018-09-15 stsp return NULL;
271 31cedeaf 2018-09-15 stsp static const struct got_error *
272 31cedeaf 2018-09-15 stsp advance_branch(struct got_commit_graph *graph,
273 0ed6ed4c 2018-06-13 stsp struct got_commit_graph_node *node,
274 41fa1437 2018-11-05 stsp struct got_object_id *commit_id, struct got_commit_object *commit,
275 bee6b577 2018-09-19 stsp struct got_repository *repo)
277 0ed6ed4c 2018-06-13 stsp const struct got_error *err;
278 0ed6ed4c 2018-06-13 stsp struct got_object_qid *qid;
280 31cedeaf 2018-09-15 stsp err = close_branch(graph, commit_id);
282 0ed6ed4c 2018-06-13 stsp return err;
284 0ed6ed4c 2018-06-13 stsp if (graph->flags & GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL) {
285 0ed6ed4c 2018-06-13 stsp qid = SIMPLEQ_FIRST(&commit->parent_ids);
286 0ed6ed4c 2018-06-13 stsp if (qid == NULL)
287 0ed6ed4c 2018-06-13 stsp return NULL;
288 b36429ab 2018-11-05 stsp err = got_object_idset_add(graph->open_branches, qid->id, node);
289 b36429ab 2018-11-05 stsp return err;
293 bee6b577 2018-09-19 stsp * If we are graphing commits for a specific path, skip branches
294 bee6b577 2018-09-19 stsp * which do not contribute any content to this path.
296 bee6b577 2018-09-19 stsp if (is_merge_point(node) && !got_path_is_root_dir(graph->path)) {
297 bee6b577 2018-09-19 stsp struct got_object_id *id, *merged_id, *prev_id = NULL;
298 bee6b577 2018-09-19 stsp int branches_differ = 0;
300 bee6b577 2018-09-19 stsp err = got_object_id_by_path(&merged_id, repo, commit_id,
301 bee6b577 2018-09-19 stsp graph->path);
303 bee6b577 2018-09-19 stsp return err;
305 bee6b577 2018-09-19 stsp SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
306 bee6b577 2018-09-19 stsp if (got_object_idset_get(graph->node_ids, qid->id))
307 bee6b577 2018-09-19 stsp continue; /* parent already traversed */
309 bee6b577 2018-09-19 stsp err = got_object_id_by_path(&id, repo, qid->id,
310 bee6b577 2018-09-19 stsp graph->path);
312 d1451975 2018-11-11 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY) {
313 bee6b577 2018-09-19 stsp branches_differ = 1;
316 bee6b577 2018-09-19 stsp return err;
319 bee6b577 2018-09-19 stsp if (prev_id) {
320 bee6b577 2018-09-19 stsp if (!branches_differ &&
321 bee6b577 2018-09-19 stsp got_object_id_cmp(merged_id, prev_id) != 0)
322 bee6b577 2018-09-19 stsp branches_differ = 1;
324 bee6b577 2018-09-19 stsp prev_id = id;
327 bee6b577 2018-09-19 stsp * If a branch has created the merged content we can
328 bee6b577 2018-09-19 stsp * skip any other branches.
330 bee6b577 2018-09-19 stsp if (got_object_id_cmp(merged_id, id) == 0) {
331 b36429ab 2018-11-05 stsp err = got_object_idset_add(graph->open_branches,
332 b36429ab 2018-11-05 stsp qid->id, node);
333 b36429ab 2018-11-05 stsp return err;
338 bee6b577 2018-09-19 stsp * If the path's content is the same on all branches,
339 bee6b577 2018-09-19 stsp * follow the first parent only.
341 bee6b577 2018-09-19 stsp if (!branches_differ) {
342 bee6b577 2018-09-19 stsp qid = SIMPLEQ_FIRST(&commit->parent_ids);
343 bee6b577 2018-09-19 stsp if (qid == NULL)
344 bee6b577 2018-09-19 stsp return NULL;
345 bee6b577 2018-09-19 stsp if (got_object_idset_get(graph->node_ids, qid->id))
346 bee6b577 2018-09-19 stsp return NULL; /* parent already traversed */
347 b36429ab 2018-11-05 stsp if (got_object_idset_get(graph->open_branches, qid->id))
348 b36429ab 2018-11-05 stsp return NULL;
349 b36429ab 2018-11-05 stsp return got_object_idset_add(graph->open_branches,
350 b36429ab 2018-11-05 stsp qid->id, node);
354 0ed6ed4c 2018-06-13 stsp SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
355 0ed6ed4c 2018-06-13 stsp if (got_object_idset_get(graph->node_ids, qid->id))
356 0ed6ed4c 2018-06-13 stsp continue; /* parent already traversed */
357 b36429ab 2018-11-05 stsp if (got_object_idset_get(graph->open_branches, qid->id))
359 b36429ab 2018-11-05 stsp err = got_object_idset_add(graph->open_branches, qid->id, node);
361 0ed6ed4c 2018-06-13 stsp return err;
364 b43fbaa0 2018-06-11 stsp return NULL;
367 fb5b9d5a 2018-07-23 stsp static void
368 fb5b9d5a 2018-07-23 stsp free_node(struct got_commit_graph_node *node)
370 fb5b9d5a 2018-07-23 stsp while (!SIMPLEQ_EMPTY(&node->parent_ids)) {
371 fb5b9d5a 2018-07-23 stsp struct got_object_qid *pid = SIMPLEQ_FIRST(&node->parent_ids);
372 fb5b9d5a 2018-07-23 stsp SIMPLEQ_REMOVE_HEAD(&node->parent_ids, entry);
373 fa2f6902 2018-07-23 stsp got_object_qid_free(pid);
375 fb5b9d5a 2018-07-23 stsp free(node);
378 b43fbaa0 2018-06-11 stsp static const struct got_error *
379 649fc316 2018-11-05 stsp add_node(struct got_commit_graph_node **new_node, int *changed,
380 1bf85b22 2018-11-07 stsp int *branch_done, struct got_commit_graph *graph,
381 1bf85b22 2018-11-07 stsp struct got_object_id *commit_id, struct got_commit_object *commit,
382 1bf85b22 2018-11-07 stsp struct got_commit_graph_node *child_node, struct got_repository *repo)
384 372ccdbb 2018-06-10 stsp const struct got_error *err = NULL;
385 f4ceb45e 2018-07-23 stsp struct got_commit_graph_node *node;
386 fa2f6902 2018-07-23 stsp struct got_object_qid *pid;
388 372ccdbb 2018-06-10 stsp *new_node = NULL;
389 649fc316 2018-11-05 stsp *changed = 0;
390 1bf85b22 2018-11-07 stsp *branch_done = 0;
392 372ccdbb 2018-06-10 stsp node = calloc(1, sizeof(*node));
393 372ccdbb 2018-06-10 stsp if (node == NULL)
394 372ccdbb 2018-06-10 stsp return got_error_from_errno();
396 372ccdbb 2018-06-10 stsp memcpy(&node->id, commit_id, sizeof(node->id));
397 b43fbaa0 2018-06-11 stsp SIMPLEQ_INIT(&node->parent_ids);
398 fa2f6902 2018-07-23 stsp SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
399 fa2f6902 2018-07-23 stsp err = add_vertex(&node->parent_ids, pid->id);
401 fb5b9d5a 2018-07-23 stsp free_node(node);
402 b43fbaa0 2018-06-11 stsp return err;
404 b43fbaa0 2018-06-11 stsp node->nparents++;
406 bfd1f6da 2018-11-05 stsp node->commit_timestamp = commit->committer_time;
408 b36429ab 2018-11-05 stsp err = got_object_idset_add(graph->node_ids, &node->id, node);
410 fb5b9d5a 2018-07-23 stsp free_node(node);
411 372ccdbb 2018-06-10 stsp return err;
414 649fc316 2018-11-05 stsp err = detect_changed_path(changed, commit, commit_id, graph->path,
417 31cedeaf 2018-09-15 stsp if (err->code == GOT_ERR_NO_OBJ) {
419 31cedeaf 2018-09-15 stsp * History of the path stops here on the current
420 31cedeaf 2018-09-15 stsp * branch. Keep going on other branches.
422 31cedeaf 2018-09-15 stsp err = NULL;
423 1bf85b22 2018-11-07 stsp *branch_done = 1;
425 31cedeaf 2018-09-15 stsp free_node(node);
426 31cedeaf 2018-09-15 stsp return err;
430 649fc316 2018-11-05 stsp if (*changed)
431 bee6b577 2018-09-19 stsp add_node_to_iter_list(graph, node, child_node);
434 f4ceb45e 2018-07-23 stsp free_node(node);
436 f4ceb45e 2018-07-23 stsp *new_node = node;
438 c4d7a9c4 2018-06-11 stsp return err;
441 c4d7a9c4 2018-06-11 stsp const struct got_error *
442 c4d7a9c4 2018-06-11 stsp got_commit_graph_open(struct got_commit_graph **graph,
443 31cedeaf 2018-09-15 stsp struct got_object_id *commit_id, const char *path,
444 31cedeaf 2018-09-15 stsp int first_parent_traversal, struct got_repository *repo)
446 c4d7a9c4 2018-06-11 stsp const struct got_error *err = NULL;
447 41fa1437 2018-11-05 stsp struct got_commit_object *commit;
448 1bf85b22 2018-11-07 stsp int changed, branch_done;
450 c4d7a9c4 2018-06-11 stsp *graph = NULL;
452 41fa1437 2018-11-05 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
454 372ccdbb 2018-06-10 stsp return err;
456 31cedeaf 2018-09-15 stsp /* The path must exist in our initial commit. */
457 31cedeaf 2018-09-15 stsp if (!got_path_is_root_dir(path)) {
458 31cedeaf 2018-09-15 stsp struct got_object_id *obj_id;
459 31cedeaf 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
461 31cedeaf 2018-09-15 stsp return err;
462 31cedeaf 2018-09-15 stsp free(obj_id);
465 31cedeaf 2018-09-15 stsp *graph = alloc_graph(path);
466 372ccdbb 2018-06-10 stsp if (*graph == NULL) {
467 41fa1437 2018-11-05 stsp got_object_commit_close(commit);
468 372ccdbb 2018-06-10 stsp return got_error_from_errno();
471 0ed6ed4c 2018-06-13 stsp if (first_parent_traversal)
472 0ed6ed4c 2018-06-13 stsp (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
474 1bf85b22 2018-11-07 stsp err = add_node(&(*graph)->head_node, &changed, &branch_done, *graph,
475 1bf85b22 2018-11-07 stsp commit_id, commit, NULL, repo);
476 1bf85b22 2018-11-07 stsp if (err == NULL) {
477 1bf85b22 2018-11-07 stsp err = advance_branch(*graph, (*graph)->head_node, commit_id,
478 1bf85b22 2018-11-07 stsp commit, repo);
480 41fa1437 2018-11-05 stsp got_object_commit_close(commit);
482 372ccdbb 2018-06-10 stsp got_commit_graph_close(*graph);
483 372ccdbb 2018-06-10 stsp *graph = NULL;
484 372ccdbb 2018-06-10 stsp return err;
487 372ccdbb 2018-06-10 stsp return NULL;
490 32777563 2018-11-07 stsp struct add_branch_tip_arg {
491 b565f6f8 2018-07-23 stsp struct got_commit_graph_branch_tip *tips;
493 32777563 2018-11-07 stsp struct got_repository *repo;
494 32777563 2018-11-07 stsp struct got_commit_graph *graph;
497 cb103d04 2018-11-07 stsp static const struct got_error *
498 32777563 2018-11-07 stsp add_branch_tip(struct got_object_id *commit_id, void *data, void *arg)
500 32777563 2018-11-07 stsp const struct got_error *err;
501 32777563 2018-11-07 stsp struct got_commit_graph_node *child_node = data;
502 32777563 2018-11-07 stsp struct add_branch_tip_arg *a = arg;
503 32777563 2018-11-07 stsp struct got_commit_graph_node *new_node;
504 32777563 2018-11-07 stsp struct got_commit_object *commit;
505 32777563 2018-11-07 stsp int changed, branch_done;
507 32777563 2018-11-07 stsp err = got_object_open_as_commit(&commit, a->repo, commit_id);
509 32777563 2018-11-07 stsp return err;
511 32777563 2018-11-07 stsp err = add_node(&new_node, &changed, &branch_done, a->graph,
512 32777563 2018-11-07 stsp commit_id, commit, child_node, a->repo);
514 32777563 2018-11-07 stsp return err;
516 32777563 2018-11-07 stsp a->tips[a->ntips].commit_id = new_node ? &new_node->id : NULL;
517 32777563 2018-11-07 stsp a->tips[a->ntips].commit = commit;
518 32777563 2018-11-07 stsp a->tips[a->ntips].new_node = new_node;
519 32777563 2018-11-07 stsp a->tips[a->ntips].changed = changed;
520 32777563 2018-11-07 stsp a->tips[a->ntips].branch_done = branch_done;
521 b565f6f8 2018-07-23 stsp a->ntips++;
523 cb103d04 2018-11-07 stsp return NULL;
526 1142eae9 2018-06-11 stsp static const struct got_error *
527 32777563 2018-11-07 stsp fetch_commits_from_open_branches(int *nfetched,
528 31cedeaf 2018-09-15 stsp struct got_object_id **changed_id, struct got_commit_graph *graph,
529 644e44ed 2018-11-04 stsp struct got_repository *repo)
531 372ccdbb 2018-06-10 stsp const struct got_error *err;
532 32777563 2018-11-07 stsp struct add_branch_tip_arg arg;
533 32777563 2018-11-07 stsp int i, ntips;
535 32777563 2018-11-07 stsp *nfetched = 0;
536 649fc316 2018-11-05 stsp *changed_id = NULL;
538 32777563 2018-11-07 stsp ntips = got_object_idset_num_elements(graph->open_branches);
539 32777563 2018-11-07 stsp if (ntips == 0)
540 372ccdbb 2018-06-10 stsp return NULL;
542 32777563 2018-11-07 stsp /* (Re-)allocate branch tips array if necessary. */
543 32777563 2018-11-07 stsp if (graph->ntips < ntips) {
544 b565f6f8 2018-07-23 stsp struct got_commit_graph_branch_tip *tips;
545 5e50c36a 2018-11-08 stsp tips = recallocarray(graph->tips, graph->ntips, ntips,
546 5e50c36a 2018-11-08 stsp sizeof(*tips));
547 b565f6f8 2018-07-23 stsp if (tips == NULL)
548 b565f6f8 2018-07-23 stsp return got_error_from_errno();
549 b565f6f8 2018-07-23 stsp graph->tips = tips;
550 32777563 2018-11-07 stsp graph->ntips = ntips;
552 b565f6f8 2018-07-23 stsp arg.tips = graph->tips;
553 32777563 2018-11-07 stsp arg.ntips = 0; /* add_branch_tip() will increment */
554 32777563 2018-11-07 stsp arg.repo = repo;
555 32777563 2018-11-07 stsp arg.graph = graph;
556 32777563 2018-11-07 stsp err = got_object_idset_for_each(graph->open_branches, add_branch_tip,
561 b565f6f8 2018-07-23 stsp for (i = 0; i < arg.ntips; i++) {
562 372ccdbb 2018-06-10 stsp struct got_object_id *commit_id;
563 41fa1437 2018-11-05 stsp struct got_commit_object *commit;
564 32777563 2018-11-07 stsp struct got_commit_graph_node *new_node;
565 32777563 2018-11-07 stsp int branch_done, changed;
567 32777563 2018-11-07 stsp commit_id = arg.tips[i].commit_id;
568 32777563 2018-11-07 stsp commit = arg.tips[i].commit;
569 32777563 2018-11-07 stsp new_node = arg.tips[i].new_node;
570 32777563 2018-11-07 stsp branch_done = arg.tips[i].branch_done;
571 32777563 2018-11-07 stsp changed = arg.tips[i].changed;
573 1bf85b22 2018-11-07 stsp if (branch_done)
574 1bf85b22 2018-11-07 stsp err = close_branch(graph, commit_id);
576 1bf85b22 2018-11-07 stsp err = advance_branch(graph, new_node, commit_id,
577 1bf85b22 2018-11-07 stsp commit, repo);
580 32777563 2018-11-07 stsp if (changed && *changed_id == NULL)
581 32777563 2018-11-07 stsp *changed_id = commit_id;
584 246e1c78 2018-11-08 stsp for (i = 0; i < arg.ntips; i++)
585 32777563 2018-11-07 stsp got_object_commit_close(arg.tips[i].commit);
586 32777563 2018-11-07 stsp (*nfetched) = arg.ntips;
587 372ccdbb 2018-06-10 stsp return err;
590 372ccdbb 2018-06-10 stsp const struct got_error *
591 31cedeaf 2018-09-15 stsp got_commit_graph_fetch_commits(struct got_commit_graph *graph, int limit,
592 31cedeaf 2018-09-15 stsp struct got_repository *repo)
594 372ccdbb 2018-06-10 stsp const struct got_error *err;
595 31cedeaf 2018-09-15 stsp int nfetched = 0, ncommits;
596 31cedeaf 2018-09-15 stsp struct got_object_id *changed_id = NULL;
598 31cedeaf 2018-09-15 stsp while (nfetched < limit) {
599 644e44ed 2018-11-04 stsp err = fetch_commits_from_open_branches(&ncommits,
600 644e44ed 2018-11-04 stsp &changed_id, graph, repo);
602 372ccdbb 2018-06-10 stsp return err;
603 372ccdbb 2018-06-10 stsp if (ncommits == 0)
605 31cedeaf 2018-09-15 stsp if (changed_id)
606 31cedeaf 2018-09-15 stsp nfetched += ncommits;
609 372ccdbb 2018-06-10 stsp return NULL;
612 cb103d04 2018-11-07 stsp static const struct got_error *
613 fb5b9d5a 2018-07-23 stsp free_node_iter(struct got_object_id *id, void *data, void *arg)
615 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *node = data;
616 fb5b9d5a 2018-07-23 stsp free_node(node);
617 cb103d04 2018-11-07 stsp return NULL;
621 372ccdbb 2018-06-10 stsp got_commit_graph_close(struct got_commit_graph *graph)
623 372ccdbb 2018-06-10 stsp got_object_idset_free(graph->open_branches);
624 fb5b9d5a 2018-07-23 stsp got_object_idset_for_each(graph->node_ids, free_node_iter, NULL);
625 372ccdbb 2018-06-10 stsp got_object_idset_free(graph->node_ids);
626 b565f6f8 2018-07-23 stsp free(graph->tips);
627 31cedeaf 2018-09-15 stsp free(graph->path);
628 372ccdbb 2018-06-10 stsp free(graph);
631 372ccdbb 2018-06-10 stsp const struct got_error *
632 372ccdbb 2018-06-10 stsp got_commit_graph_iter_start(struct got_commit_graph *graph,
633 31cedeaf 2018-09-15 stsp struct got_object_id *id, struct got_repository *repo)
635 31cedeaf 2018-09-15 stsp const struct got_error *err = NULL;
636 9ba79e04 2018-06-11 stsp struct got_commit_graph_node *start_node;
637 41fa1437 2018-11-05 stsp struct got_commit_object *commit;
638 31cedeaf 2018-09-15 stsp int changed;
640 372ccdbb 2018-06-10 stsp start_node = got_object_idset_get(graph->node_ids, id);
641 372ccdbb 2018-06-10 stsp if (start_node == NULL)
642 91a3d81f 2018-11-11 stsp return got_error_no_obj(id);
644 41fa1437 2018-11-05 stsp err = got_object_open_as_commit(&commit, repo, &start_node->id);
646 31cedeaf 2018-09-15 stsp return err;
648 31cedeaf 2018-09-15 stsp err = detect_changed_path(&changed, commit, &start_node->id,
649 31cedeaf 2018-09-15 stsp graph->path, repo);
651 41fa1437 2018-11-05 stsp got_object_commit_close(commit);
652 31cedeaf 2018-09-15 stsp return err;
655 31cedeaf 2018-09-15 stsp if (!changed) {
656 31cedeaf 2018-09-15 stsp /* Locate first commit which changed graph->path. */
657 31cedeaf 2018-09-15 stsp struct got_object_id *changed_id = NULL;
658 31cedeaf 2018-09-15 stsp while (changed_id == NULL) {
659 31cedeaf 2018-09-15 stsp int ncommits;
660 644e44ed 2018-11-04 stsp err = fetch_commits_from_open_branches(&ncommits,
661 644e44ed 2018-11-04 stsp &changed_id, graph, repo);
663 41fa1437 2018-11-05 stsp got_object_commit_close(commit);
664 31cedeaf 2018-09-15 stsp return err;
667 31cedeaf 2018-09-15 stsp start_node = got_object_idset_get(graph->node_ids, changed_id);
669 41fa1437 2018-11-05 stsp got_object_commit_close(commit);
671 372ccdbb 2018-06-10 stsp graph->iter_node = start_node;
672 372ccdbb 2018-06-10 stsp return NULL;
675 372ccdbb 2018-06-10 stsp const struct got_error *
676 b43fbaa0 2018-06-11 stsp got_commit_graph_iter_next(struct got_object_id **id,
677 b43fbaa0 2018-06-11 stsp struct got_commit_graph *graph)
679 9ba79e04 2018-06-11 stsp *id = NULL;
681 372ccdbb 2018-06-10 stsp if (graph->iter_node == NULL) {
682 2c7f8870 2018-09-19 stsp /* We are done iterating, or iteration was not started. */
683 9ba79e04 2018-06-11 stsp return got_error(GOT_ERR_ITER_COMPLETED);
686 9ba79e04 2018-06-11 stsp if (graph->iter_node ==
687 9ba79e04 2018-06-11 stsp TAILQ_LAST(&graph->iter_list, got_commit_graph_iter_list) &&
688 9ba79e04 2018-06-11 stsp got_object_idset_num_elements(graph->open_branches) == 0) {
689 2c7f8870 2018-09-19 stsp /* We are done iterating. */
690 6bc66952 2018-09-19 stsp *id = &graph->iter_node->id;
691 9ba79e04 2018-06-11 stsp graph->iter_node = NULL;
692 6bc66952 2018-09-19 stsp return NULL;
695 9ba79e04 2018-06-11 stsp if (TAILQ_NEXT(graph->iter_node, entry) == NULL)
696 372ccdbb 2018-06-10 stsp return got_error(GOT_ERR_ITER_NEED_MORE);
698 6bc66952 2018-09-19 stsp *id = &graph->iter_node->id;
699 6bc66952 2018-09-19 stsp graph->iter_node = TAILQ_NEXT(graph->iter_node, entry);
700 372ccdbb 2018-06-10 stsp return NULL;