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"
40 32777563 2018-11-07 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
43 372ccdbb 2018-06-10 stsp struct got_commit_graph_node {
44 372ccdbb 2018-06-10 stsp struct got_object_id id;
47 372ccdbb 2018-06-10 stsp * Each graph node corresponds to a commit object.
48 f4ceb45e 2018-07-23 stsp * Graph vertices are modelled with an adjacency list.
49 f4ceb45e 2018-07-23 stsp * Adjacencies of a graph node are parent (older) commits.
51 b43fbaa0 2018-06-11 stsp int nparents;
52 79f35eb3 2018-06-11 stsp struct got_object_id_queue parent_ids;
54 b43fbaa0 2018-06-11 stsp time_t commit_timestamp;
56 372ccdbb 2018-06-10 stsp /* Used during graph iteration. */
57 372ccdbb 2018-06-10 stsp TAILQ_ENTRY(got_commit_graph_node) entry;
60 9ba79e04 2018-06-11 stsp TAILQ_HEAD(got_commit_graph_iter_list, got_commit_graph_node);
62 b565f6f8 2018-07-23 stsp struct got_commit_graph_branch_tip {
63 32777563 2018-11-07 stsp struct got_object_id *commit_id;
64 32777563 2018-11-07 stsp struct got_commit_object *commit;
65 32777563 2018-11-07 stsp struct got_commit_graph_node *new_node;
66 32777563 2018-11-07 stsp int changed;
67 32777563 2018-11-07 stsp int branch_done;
70 372ccdbb 2018-06-10 stsp struct got_commit_graph {
71 372ccdbb 2018-06-10 stsp /* The set of all commits we have traversed. */
72 372ccdbb 2018-06-10 stsp struct got_object_idset *node_ids;
74 372ccdbb 2018-06-10 stsp /* The commit at which traversal began (youngest commit in node_ids). */
75 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *head_node;
78 0ed6ed4c 2018-06-13 stsp #define GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL 0x01
81 372ccdbb 2018-06-10 stsp * A set of object IDs of known parent commits which we have not yet
82 372ccdbb 2018-06-10 stsp * traversed. Each commit ID in this set represents a branch in commit
83 372ccdbb 2018-06-10 stsp * history: Either the first-parent branch of the head node, or another
84 372ccdbb 2018-06-10 stsp * branch corresponding to a traversed merge commit for which we have
85 372ccdbb 2018-06-10 stsp * not traversed a branch point commit yet.
87 372ccdbb 2018-06-10 stsp * Whenever we add a commit with a matching ID to the graph, we remove
88 372ccdbb 2018-06-10 stsp * its corresponding element from this set, and add new elements for
89 372ccdbb 2018-06-10 stsp * each of that commit's parent commits which were not traversed yet.
91 372ccdbb 2018-06-10 stsp * When API users ask us to fetch more commits, we fetch commits from
92 372ccdbb 2018-06-10 stsp * all currently open branches. This allows API users to process
93 372ccdbb 2018-06-10 stsp * commits in linear order even though the history contains branches.
95 372ccdbb 2018-06-10 stsp struct got_object_idset *open_branches;
97 32777563 2018-11-07 stsp /* Array of branch tips for fetch_commits_from_open_branches(). */
98 b565f6f8 2018-07-23 stsp struct got_commit_graph_branch_tip *tips;
99 b565f6f8 2018-07-23 stsp size_t ntips;
100 8d49203e 2018-09-20 stsp #define GOT_COMMIT_GRAPH_MIN_TIPS 10 /* minimum amount of tips to allocate */
102 31cedeaf 2018-09-15 stsp /* Path of tree entry of interest to the API user. */
103 31cedeaf 2018-09-15 stsp char *path;
105 372ccdbb 2018-06-10 stsp /* The next commit to return when the API user asks for one. */
106 372ccdbb 2018-06-10 stsp struct got_commit_graph_node *iter_node;
108 9ba79e04 2018-06-11 stsp /* The graph iteration list contains all nodes in sorted order. */
109 9ba79e04 2018-06-11 stsp struct got_commit_graph_iter_list iter_list;
112 372ccdbb 2018-06-10 stsp static struct got_commit_graph *
113 31cedeaf 2018-09-15 stsp alloc_graph(const char *path)
115 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
117 372ccdbb 2018-06-10 stsp graph = calloc(1, sizeof(*graph));
118 372ccdbb 2018-06-10 stsp if (graph == NULL)
119 372ccdbb 2018-06-10 stsp return NULL;
121 31cedeaf 2018-09-15 stsp graph->path = strdup(path);
122 31cedeaf 2018-09-15 stsp if (graph->path == NULL) {
123 31cedeaf 2018-09-15 stsp free(graph);
124 31cedeaf 2018-09-15 stsp return NULL;
127 60f2eee1 2018-07-08 stsp graph->node_ids = got_object_idset_alloc();
128 372ccdbb 2018-06-10 stsp if (graph->node_ids == NULL) {
129 31cedeaf 2018-09-15 stsp free(graph->path);
130 372ccdbb 2018-06-10 stsp free(graph);
131 372ccdbb 2018-06-10 stsp return NULL;
134 60f2eee1 2018-07-08 stsp graph->open_branches = got_object_idset_alloc();
135 372ccdbb 2018-06-10 stsp if (graph->open_branches == NULL) {
136 372ccdbb 2018-06-10 stsp got_object_idset_free(graph->node_ids);
137 31cedeaf 2018-09-15 stsp free(graph->path);
138 372ccdbb 2018-06-10 stsp free(graph);
139 372ccdbb 2018-06-10 stsp return NULL;
142 31920504 2018-06-11 stsp TAILQ_INIT(&graph->iter_list);
143 372ccdbb 2018-06-10 stsp return graph;
148 372ccdbb 2018-06-10 stsp is_head_node(struct got_commit_graph_node *node)
150 372ccdbb 2018-06-10 stsp return node->nchildren == 0;
154 372ccdbb 2018-06-10 stsp is_branch_point(struct got_commit_graph_node *node)
156 372ccdbb 2018-06-10 stsp return node->nchildren > 1;
160 372ccdbb 2018-06-10 stsp is_root_node(struct got_commit_graph_node *node)
162 b43fbaa0 2018-06-11 stsp return node->nparents == 0;
167 bee6b577 2018-09-19 stsp is_merge_point(struct got_commit_graph_node *node)
169 bee6b577 2018-09-19 stsp return node->nparents > 1;
172 31cedeaf 2018-09-15 stsp static const struct got_error *
173 41fa1437 2018-11-05 stsp detect_changed_path(int *changed, struct got_commit_object *commit,
174 31cedeaf 2018-09-15 stsp struct got_object_id *commit_id, const char *path,
175 31cedeaf 2018-09-15 stsp struct got_repository *repo)
177 31cedeaf 2018-09-15 stsp const struct got_error *err = NULL;
178 41fa1437 2018-11-05 stsp struct got_commit_object *pcommit = NULL;
179 31cedeaf 2018-09-15 stsp struct got_tree_object *tree = NULL, *ptree = NULL;
180 31cedeaf 2018-09-15 stsp struct got_object_qid *pid;
182 31cedeaf 2018-09-15 stsp if (got_path_is_root_dir(path)) {
183 31cedeaf 2018-09-15 stsp *changed = 1;
184 31cedeaf 2018-09-15 stsp return NULL;
187 31cedeaf 2018-09-15 stsp *changed = 0;
189 31cedeaf 2018-09-15 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
191 31cedeaf 2018-09-15 stsp return err;
193 31cedeaf 2018-09-15 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
194 31cedeaf 2018-09-15 stsp if (pid == NULL) {
195 31cedeaf 2018-09-15 stsp struct got_object_id *obj_id;
196 31cedeaf 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
198 fd1d2703 2018-11-04 stsp if (err->code == GOT_ERR_NO_OBJ)
199 fd1d2703 2018-11-04 stsp err = NULL;
201 fd1d2703 2018-11-04 stsp *changed = 1; /* The path was created in this commit. */
202 31cedeaf 2018-09-15 stsp free(obj_id);
204 41fa1437 2018-11-05 stsp err = got_object_open_as_commit(&pcommit, repo, pid->id);
208 fd1d2703 2018-11-04 stsp err = got_object_open_as_tree(&ptree, repo, pcommit->tree_id);
212 fd1d2703 2018-11-04 stsp err = got_object_tree_path_changed(changed, tree, ptree, path,
217 31cedeaf 2018-09-15 stsp got_object_tree_close(tree);
219 31cedeaf 2018-09-15 stsp got_object_tree_close(ptree);
220 31cedeaf 2018-09-15 stsp if (pcommit)
221 41fa1437 2018-11-05 stsp got_object_commit_close(pcommit);
222 31cedeaf 2018-09-15 stsp return err;
225 4626e416 2018-06-10 stsp static void
226 9ba79e04 2018-06-11 stsp add_node_to_iter_list(struct got_commit_graph *graph,
227 9ba79e04 2018-06-11 stsp struct got_commit_graph_node *node,
228 9ba79e04 2018-06-11 stsp struct got_commit_graph_node *child_node)
230 4bd3f2bb 2018-06-10 stsp struct got_commit_graph_node *n, *next;
232 31920504 2018-06-11 stsp if (TAILQ_EMPTY(&graph->iter_list)) {
233 bee6b577 2018-09-19 stsp TAILQ_INSERT_HEAD(&graph->iter_list, node, entry);
234 bee6b577 2018-09-19 stsp graph->iter_node = node;
238 bee6b577 2018-09-19 stsp n = graph->iter_node;
239 fe8df4c2 2018-06-16 stsp /* Ensure that an iteration in progress will see this new commit. */
240 bee6b577 2018-09-19 stsp while (n) {
241 bee6b577 2018-09-19 stsp next = TAILQ_NEXT(n, entry);
242 bee6b577 2018-09-19 stsp if (next && node->commit_timestamp >= next->commit_timestamp) {
243 bee6b577 2018-09-19 stsp TAILQ_INSERT_BEFORE(next, node, entry);
248 93e45b7c 2018-09-24 stsp TAILQ_INSERT_TAIL(&graph->iter_list, node, entry);
251 372ccdbb 2018-06-10 stsp static const struct got_error *
252 79f35eb3 2018-06-11 stsp add_vertex(struct got_object_id_queue *ids, struct got_object_id *id)
254 5df4932d 2018-11-05 stsp const struct got_error *err = NULL;
255 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
257 5df4932d 2018-11-05 stsp err = got_object_qid_alloc(&qid, id);
259 b43fbaa0 2018-06-11 stsp return err;
261 79f35eb3 2018-06-11 stsp SIMPLEQ_INSERT_TAIL(ids, qid, entry);
262 0ed6ed4c 2018-06-13 stsp return NULL;
265 0ed6ed4c 2018-06-13 stsp static const struct got_error *
266 31cedeaf 2018-09-15 stsp close_branch(struct got_commit_graph *graph, struct got_object_id *commit_id)
268 31cedeaf 2018-09-15 stsp const struct got_error *err;
270 31cedeaf 2018-09-15 stsp err = got_object_idset_remove(NULL, graph->open_branches, commit_id);
271 31cedeaf 2018-09-15 stsp if (err && err->code != GOT_ERR_NO_OBJ)
272 31cedeaf 2018-09-15 stsp return err;
273 31cedeaf 2018-09-15 stsp return NULL;
276 31cedeaf 2018-09-15 stsp static const struct got_error *
277 31cedeaf 2018-09-15 stsp advance_branch(struct got_commit_graph *graph,
278 0ed6ed4c 2018-06-13 stsp struct got_commit_graph_node *node,
279 41fa1437 2018-11-05 stsp struct got_object_id *commit_id, struct got_commit_object *commit,
280 bee6b577 2018-09-19 stsp struct got_repository *repo)
282 0ed6ed4c 2018-06-13 stsp const struct got_error *err;
283 0ed6ed4c 2018-06-13 stsp struct got_object_qid *qid;
285 31cedeaf 2018-09-15 stsp err = close_branch(graph, commit_id);
287 0ed6ed4c 2018-06-13 stsp return err;
289 0ed6ed4c 2018-06-13 stsp if (graph->flags & GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL) {
290 0ed6ed4c 2018-06-13 stsp qid = SIMPLEQ_FIRST(&commit->parent_ids);
291 0ed6ed4c 2018-06-13 stsp if (qid == NULL)
292 0ed6ed4c 2018-06-13 stsp return NULL;
293 b36429ab 2018-11-05 stsp err = got_object_idset_add(graph->open_branches, qid->id, node);
294 b36429ab 2018-11-05 stsp return err;
298 bee6b577 2018-09-19 stsp * If we are graphing commits for a specific path, skip branches
299 bee6b577 2018-09-19 stsp * which do not contribute any content to this path.
301 bee6b577 2018-09-19 stsp if (is_merge_point(node) && !got_path_is_root_dir(graph->path)) {
302 bee6b577 2018-09-19 stsp struct got_object_id *id, *merged_id, *prev_id = NULL;
303 bee6b577 2018-09-19 stsp int branches_differ = 0;
305 bee6b577 2018-09-19 stsp err = got_object_id_by_path(&merged_id, repo, commit_id,
306 bee6b577 2018-09-19 stsp graph->path);
308 bee6b577 2018-09-19 stsp return err;
310 bee6b577 2018-09-19 stsp SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
311 bee6b577 2018-09-19 stsp if (got_object_idset_get(graph->node_ids, qid->id))
312 bee6b577 2018-09-19 stsp continue; /* parent already traversed */
314 bee6b577 2018-09-19 stsp err = got_object_id_by_path(&id, repo, qid->id,
315 bee6b577 2018-09-19 stsp graph->path);
317 bee6b577 2018-09-19 stsp if (err->code == GOT_ERR_NO_OBJ) {
318 bee6b577 2018-09-19 stsp branches_differ = 1;
321 bee6b577 2018-09-19 stsp return err;
324 bee6b577 2018-09-19 stsp if (prev_id) {
325 bee6b577 2018-09-19 stsp if (!branches_differ &&
326 bee6b577 2018-09-19 stsp got_object_id_cmp(merged_id, prev_id) != 0)
327 bee6b577 2018-09-19 stsp branches_differ = 1;
329 bee6b577 2018-09-19 stsp prev_id = id;
332 bee6b577 2018-09-19 stsp * If a branch has created the merged content we can
333 bee6b577 2018-09-19 stsp * skip any other branches.
335 bee6b577 2018-09-19 stsp if (got_object_id_cmp(merged_id, id) == 0) {
336 b36429ab 2018-11-05 stsp err = got_object_idset_add(graph->open_branches,
337 b36429ab 2018-11-05 stsp qid->id, node);
338 b36429ab 2018-11-05 stsp return err;
343 bee6b577 2018-09-19 stsp * If the path's content is the same on all branches,
344 bee6b577 2018-09-19 stsp * follow the first parent only.
346 bee6b577 2018-09-19 stsp if (!branches_differ) {
347 bee6b577 2018-09-19 stsp qid = SIMPLEQ_FIRST(&commit->parent_ids);
348 bee6b577 2018-09-19 stsp if (qid == NULL)
349 bee6b577 2018-09-19 stsp return NULL;
350 bee6b577 2018-09-19 stsp if (got_object_idset_get(graph->node_ids, qid->id))
351 bee6b577 2018-09-19 stsp return NULL; /* parent already traversed */
352 b36429ab 2018-11-05 stsp if (got_object_idset_get(graph->open_branches, qid->id))
353 b36429ab 2018-11-05 stsp return NULL;
354 b36429ab 2018-11-05 stsp return got_object_idset_add(graph->open_branches,
355 b36429ab 2018-11-05 stsp qid->id, node);
359 0ed6ed4c 2018-06-13 stsp SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
360 0ed6ed4c 2018-06-13 stsp if (got_object_idset_get(graph->node_ids, qid->id))
361 0ed6ed4c 2018-06-13 stsp continue; /* parent already traversed */
362 b36429ab 2018-11-05 stsp if (got_object_idset_get(graph->open_branches, qid->id))
364 b36429ab 2018-11-05 stsp err = got_object_idset_add(graph->open_branches, qid->id, node);
366 0ed6ed4c 2018-06-13 stsp return err;
369 b43fbaa0 2018-06-11 stsp return NULL;
372 fb5b9d5a 2018-07-23 stsp static void
373 fb5b9d5a 2018-07-23 stsp free_node(struct got_commit_graph_node *node)
375 fb5b9d5a 2018-07-23 stsp while (!SIMPLEQ_EMPTY(&node->parent_ids)) {
376 fb5b9d5a 2018-07-23 stsp struct got_object_qid *pid = SIMPLEQ_FIRST(&node->parent_ids);
377 fb5b9d5a 2018-07-23 stsp SIMPLEQ_REMOVE_HEAD(&node->parent_ids, entry);
378 fa2f6902 2018-07-23 stsp got_object_qid_free(pid);
380 fb5b9d5a 2018-07-23 stsp free(node);
383 b43fbaa0 2018-06-11 stsp static const struct got_error *
384 649fc316 2018-11-05 stsp add_node(struct got_commit_graph_node **new_node, int *changed,
385 1bf85b22 2018-11-07 stsp int *branch_done, struct got_commit_graph *graph,
386 1bf85b22 2018-11-07 stsp struct got_object_id *commit_id, struct got_commit_object *commit,
387 1bf85b22 2018-11-07 stsp struct got_commit_graph_node *child_node, struct got_repository *repo)
389 372ccdbb 2018-06-10 stsp const struct got_error *err = NULL;
390 f4ceb45e 2018-07-23 stsp struct got_commit_graph_node *node;
391 fa2f6902 2018-07-23 stsp struct got_object_qid *pid;
393 372ccdbb 2018-06-10 stsp *new_node = NULL;
394 649fc316 2018-11-05 stsp *changed = 0;
395 1bf85b22 2018-11-07 stsp *branch_done = 0;
397 372ccdbb 2018-06-10 stsp node = calloc(1, sizeof(*node));
398 372ccdbb 2018-06-10 stsp if (node == NULL)
399 372ccdbb 2018-06-10 stsp return got_error_from_errno();
401 372ccdbb 2018-06-10 stsp memcpy(&node->id, commit_id, sizeof(node->id));
402 b43fbaa0 2018-06-11 stsp SIMPLEQ_INIT(&node->parent_ids);
403 fa2f6902 2018-07-23 stsp SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
404 fa2f6902 2018-07-23 stsp err = add_vertex(&node->parent_ids, pid->id);
406 fb5b9d5a 2018-07-23 stsp free_node(node);
407 b43fbaa0 2018-06-11 stsp return err;
409 b43fbaa0 2018-06-11 stsp node->nparents++;
411 bfd1f6da 2018-11-05 stsp node->commit_timestamp = commit->committer_time;
413 b36429ab 2018-11-05 stsp err = got_object_idset_add(graph->node_ids, &node->id, node);
415 fb5b9d5a 2018-07-23 stsp free_node(node);
416 372ccdbb 2018-06-10 stsp return err;
419 649fc316 2018-11-05 stsp err = detect_changed_path(changed, commit, commit_id, graph->path,
422 31cedeaf 2018-09-15 stsp if (err->code == GOT_ERR_NO_OBJ) {
424 31cedeaf 2018-09-15 stsp * History of the path stops here on the current
425 31cedeaf 2018-09-15 stsp * branch. Keep going on other branches.
427 31cedeaf 2018-09-15 stsp err = NULL;
428 1bf85b22 2018-11-07 stsp *branch_done = 1;
430 31cedeaf 2018-09-15 stsp free_node(node);
431 31cedeaf 2018-09-15 stsp return err;
435 649fc316 2018-11-05 stsp if (*changed)
436 bee6b577 2018-09-19 stsp add_node_to_iter_list(graph, node, child_node);
439 f4ceb45e 2018-07-23 stsp free_node(node);
441 f4ceb45e 2018-07-23 stsp *new_node = node;
443 c4d7a9c4 2018-06-11 stsp return err;
446 c4d7a9c4 2018-06-11 stsp const struct got_error *
447 c4d7a9c4 2018-06-11 stsp got_commit_graph_open(struct got_commit_graph **graph,
448 31cedeaf 2018-09-15 stsp struct got_object_id *commit_id, const char *path,
449 31cedeaf 2018-09-15 stsp int first_parent_traversal, struct got_repository *repo)
451 c4d7a9c4 2018-06-11 stsp const struct got_error *err = NULL;
452 41fa1437 2018-11-05 stsp struct got_commit_object *commit;
453 1bf85b22 2018-11-07 stsp int changed, branch_done;
455 c4d7a9c4 2018-06-11 stsp *graph = NULL;
457 41fa1437 2018-11-05 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
459 372ccdbb 2018-06-10 stsp return err;
461 31cedeaf 2018-09-15 stsp /* The path must exist in our initial commit. */
462 31cedeaf 2018-09-15 stsp if (!got_path_is_root_dir(path)) {
463 31cedeaf 2018-09-15 stsp struct got_object_id *obj_id;
464 31cedeaf 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
466 31cedeaf 2018-09-15 stsp return err;
467 31cedeaf 2018-09-15 stsp free(obj_id);
470 31cedeaf 2018-09-15 stsp *graph = alloc_graph(path);
471 372ccdbb 2018-06-10 stsp if (*graph == NULL) {
472 41fa1437 2018-11-05 stsp got_object_commit_close(commit);
473 372ccdbb 2018-06-10 stsp return got_error_from_errno();
476 0ed6ed4c 2018-06-13 stsp if (first_parent_traversal)
477 0ed6ed4c 2018-06-13 stsp (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
479 1bf85b22 2018-11-07 stsp err = add_node(&(*graph)->head_node, &changed, &branch_done, *graph,
480 1bf85b22 2018-11-07 stsp commit_id, commit, NULL, repo);
481 1bf85b22 2018-11-07 stsp if (err == NULL) {
482 1bf85b22 2018-11-07 stsp err = advance_branch(*graph, (*graph)->head_node, commit_id,
483 1bf85b22 2018-11-07 stsp commit, repo);
485 41fa1437 2018-11-05 stsp got_object_commit_close(commit);
487 372ccdbb 2018-06-10 stsp got_commit_graph_close(*graph);
488 372ccdbb 2018-06-10 stsp *graph = NULL;
489 372ccdbb 2018-06-10 stsp return err;
492 372ccdbb 2018-06-10 stsp return NULL;
495 32777563 2018-11-07 stsp struct add_branch_tip_arg {
496 b565f6f8 2018-07-23 stsp struct got_commit_graph_branch_tip *tips;
498 32777563 2018-11-07 stsp struct got_repository *repo;
499 32777563 2018-11-07 stsp struct got_commit_graph *graph;
502 cb103d04 2018-11-07 stsp static const struct got_error *
503 32777563 2018-11-07 stsp add_branch_tip(struct got_object_id *commit_id, void *data, void *arg)
505 32777563 2018-11-07 stsp const struct got_error *err;
506 32777563 2018-11-07 stsp struct got_commit_graph_node *child_node = data;
507 32777563 2018-11-07 stsp struct add_branch_tip_arg *a = arg;
508 32777563 2018-11-07 stsp struct got_commit_graph_node *new_node;
509 32777563 2018-11-07 stsp struct got_commit_object *commit;
510 32777563 2018-11-07 stsp int changed, branch_done;
512 32777563 2018-11-07 stsp err = got_object_open_as_commit(&commit, a->repo, commit_id);
514 32777563 2018-11-07 stsp return err;
516 32777563 2018-11-07 stsp err = add_node(&new_node, &changed, &branch_done, a->graph,
517 32777563 2018-11-07 stsp commit_id, commit, child_node, a->repo);
519 32777563 2018-11-07 stsp return err;
521 32777563 2018-11-07 stsp a->tips[a->ntips].commit_id = new_node ? &new_node->id : NULL;
522 32777563 2018-11-07 stsp a->tips[a->ntips].commit = commit;
523 32777563 2018-11-07 stsp a->tips[a->ntips].new_node = new_node;
524 32777563 2018-11-07 stsp a->tips[a->ntips].changed = changed;
525 32777563 2018-11-07 stsp a->tips[a->ntips].branch_done = branch_done;
526 b565f6f8 2018-07-23 stsp a->ntips++;
528 cb103d04 2018-11-07 stsp return NULL;
531 1142eae9 2018-06-11 stsp static const struct got_error *
532 32777563 2018-11-07 stsp fetch_commits_from_open_branches(int *nfetched,
533 31cedeaf 2018-09-15 stsp struct got_object_id **changed_id, struct got_commit_graph *graph,
534 644e44ed 2018-11-04 stsp struct got_repository *repo)
536 372ccdbb 2018-06-10 stsp const struct got_error *err;
537 32777563 2018-11-07 stsp struct add_branch_tip_arg arg;
538 32777563 2018-11-07 stsp int i, ntips;
540 32777563 2018-11-07 stsp *nfetched = 0;
541 649fc316 2018-11-05 stsp *changed_id = NULL;
543 32777563 2018-11-07 stsp ntips = got_object_idset_num_elements(graph->open_branches);
544 32777563 2018-11-07 stsp if (ntips == 0)
545 372ccdbb 2018-06-10 stsp return NULL;
547 32777563 2018-11-07 stsp /* (Re-)allocate branch tips array if necessary. */
548 32777563 2018-11-07 stsp if (graph->ntips < ntips) {
549 b565f6f8 2018-07-23 stsp struct got_commit_graph_branch_tip *tips;
550 32777563 2018-11-07 stsp tips = reallocarray(graph->tips,
551 32777563 2018-11-07 stsp MIN(ntips, GOT_COMMIT_GRAPH_MIN_TIPS), sizeof(*tips));
552 b565f6f8 2018-07-23 stsp if (tips == NULL)
553 b565f6f8 2018-07-23 stsp return got_error_from_errno();
554 b565f6f8 2018-07-23 stsp graph->tips = tips;
555 32777563 2018-11-07 stsp graph->ntips = ntips;
557 b565f6f8 2018-07-23 stsp arg.tips = graph->tips;
558 32777563 2018-11-07 stsp arg.ntips = 0; /* add_branch_tip() will increment */
559 32777563 2018-11-07 stsp arg.repo = repo;
560 32777563 2018-11-07 stsp arg.graph = graph;
561 32777563 2018-11-07 stsp err = got_object_idset_for_each(graph->open_branches, add_branch_tip,
566 b565f6f8 2018-07-23 stsp for (i = 0; i < arg.ntips; i++) {
567 372ccdbb 2018-06-10 stsp struct got_object_id *commit_id;
568 41fa1437 2018-11-05 stsp struct got_commit_object *commit;
569 32777563 2018-11-07 stsp struct got_commit_graph_node *new_node;
570 32777563 2018-11-07 stsp int branch_done, changed;
572 32777563 2018-11-07 stsp commit_id = arg.tips[i].commit_id;
573 32777563 2018-11-07 stsp commit = arg.tips[i].commit;
574 32777563 2018-11-07 stsp new_node = arg.tips[i].new_node;
575 32777563 2018-11-07 stsp branch_done = arg.tips[i].branch_done;
576 32777563 2018-11-07 stsp changed = arg.tips[i].changed;
578 1bf85b22 2018-11-07 stsp if (branch_done)
579 1bf85b22 2018-11-07 stsp err = close_branch(graph, commit_id);
581 1bf85b22 2018-11-07 stsp err = advance_branch(graph, new_node, commit_id,
582 1bf85b22 2018-11-07 stsp commit, repo);
585 32777563 2018-11-07 stsp if (changed && *changed_id == NULL)
586 32777563 2018-11-07 stsp *changed_id = commit_id;
589 32777563 2018-11-07 stsp for (i = 0; i < ntips; i++)
590 32777563 2018-11-07 stsp got_object_commit_close(arg.tips[i].commit);
591 32777563 2018-11-07 stsp (*nfetched) = arg.ntips;
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 644e44ed 2018-11-04 stsp err = fetch_commits_from_open_branches(&ncommits,
605 644e44ed 2018-11-04 stsp &changed_id, graph, repo);
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 cb103d04 2018-11-07 stsp static const struct got_error *
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);
622 cb103d04 2018-11-07 stsp return NULL;
626 372ccdbb 2018-06-10 stsp got_commit_graph_close(struct got_commit_graph *graph)
628 372ccdbb 2018-06-10 stsp got_object_idset_free(graph->open_branches);
629 fb5b9d5a 2018-07-23 stsp got_object_idset_for_each(graph->node_ids, free_node_iter, NULL);
630 372ccdbb 2018-06-10 stsp got_object_idset_free(graph->node_ids);
631 b565f6f8 2018-07-23 stsp free(graph->tips);
632 31cedeaf 2018-09-15 stsp free(graph->path);
633 372ccdbb 2018-06-10 stsp free(graph);
636 372ccdbb 2018-06-10 stsp const struct got_error *
637 372ccdbb 2018-06-10 stsp got_commit_graph_iter_start(struct got_commit_graph *graph,
638 31cedeaf 2018-09-15 stsp struct got_object_id *id, struct got_repository *repo)
640 31cedeaf 2018-09-15 stsp const struct got_error *err = NULL;
641 9ba79e04 2018-06-11 stsp struct got_commit_graph_node *start_node;
642 41fa1437 2018-11-05 stsp struct got_commit_object *commit;
643 31cedeaf 2018-09-15 stsp int changed;
645 372ccdbb 2018-06-10 stsp start_node = got_object_idset_get(graph->node_ids, id);
646 372ccdbb 2018-06-10 stsp if (start_node == NULL)
647 372ccdbb 2018-06-10 stsp return got_error(GOT_ERR_NO_OBJ);
649 41fa1437 2018-11-05 stsp err = got_object_open_as_commit(&commit, repo, &start_node->id);
651 31cedeaf 2018-09-15 stsp return err;
653 31cedeaf 2018-09-15 stsp err = detect_changed_path(&changed, commit, &start_node->id,
654 31cedeaf 2018-09-15 stsp graph->path, repo);
656 41fa1437 2018-11-05 stsp got_object_commit_close(commit);
657 31cedeaf 2018-09-15 stsp return err;
660 31cedeaf 2018-09-15 stsp if (!changed) {
661 31cedeaf 2018-09-15 stsp /* Locate first commit which changed graph->path. */
662 31cedeaf 2018-09-15 stsp struct got_object_id *changed_id = NULL;
663 31cedeaf 2018-09-15 stsp while (changed_id == NULL) {
664 31cedeaf 2018-09-15 stsp int ncommits;
665 644e44ed 2018-11-04 stsp err = fetch_commits_from_open_branches(&ncommits,
666 644e44ed 2018-11-04 stsp &changed_id, graph, repo);
668 41fa1437 2018-11-05 stsp got_object_commit_close(commit);
669 31cedeaf 2018-09-15 stsp return err;
672 31cedeaf 2018-09-15 stsp start_node = got_object_idset_get(graph->node_ids, changed_id);
674 41fa1437 2018-11-05 stsp got_object_commit_close(commit);
676 372ccdbb 2018-06-10 stsp graph->iter_node = start_node;
677 372ccdbb 2018-06-10 stsp return NULL;
680 372ccdbb 2018-06-10 stsp const struct got_error *
681 b43fbaa0 2018-06-11 stsp got_commit_graph_iter_next(struct got_object_id **id,
682 b43fbaa0 2018-06-11 stsp struct got_commit_graph *graph)
684 9ba79e04 2018-06-11 stsp *id = NULL;
686 372ccdbb 2018-06-10 stsp if (graph->iter_node == NULL) {
687 2c7f8870 2018-09-19 stsp /* We are done iterating, or iteration was not started. */
688 9ba79e04 2018-06-11 stsp return got_error(GOT_ERR_ITER_COMPLETED);
691 9ba79e04 2018-06-11 stsp if (graph->iter_node ==
692 9ba79e04 2018-06-11 stsp TAILQ_LAST(&graph->iter_list, got_commit_graph_iter_list) &&
693 9ba79e04 2018-06-11 stsp got_object_idset_num_elements(graph->open_branches) == 0) {
694 2c7f8870 2018-09-19 stsp /* We are done iterating. */
695 6bc66952 2018-09-19 stsp *id = &graph->iter_node->id;
696 9ba79e04 2018-06-11 stsp graph->iter_node = NULL;
697 6bc66952 2018-09-19 stsp return NULL;
700 9ba79e04 2018-06-11 stsp if (TAILQ_NEXT(graph->iter_node, entry) == NULL)
701 372ccdbb 2018-06-10 stsp return got_error(GOT_ERR_ITER_NEED_MORE);
703 6bc66952 2018-09-19 stsp *id = &graph->iter_node->id;
704 6bc66952 2018-09-19 stsp graph->iter_node = TAILQ_NEXT(graph->iter_node, entry);
705 372ccdbb 2018-06-10 stsp return NULL;