Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/queue.h>
20 #include <sys/stdint.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sha1.h>
26 #include <zlib.h>
27 #include <ctype.h>
29 #include "got_error.h"
30 #include "got_object.h"
31 #include "got_commit_graph.h"
33 #include "got_lib_delta.h"
34 #include "got_lib_inflate.h"
35 #include "got_lib_object.h"
36 #include "got_lib_object_idset.h"
37 #include "got_lib_path.h"
39 struct got_commit_graph_node {
40 struct got_object_id id;
42 /*
43 * Each graph node corresponds to a commit object.
44 * Graph vertices are modelled with an adjacency list.
45 * Adjacencies of a graph node are parent (older) commits.
46 */
47 int nparents;
48 struct got_object_id_queue parent_ids;
50 time_t commit_timestamp;
52 /* Used during graph iteration. */
53 TAILQ_ENTRY(got_commit_graph_node) entry;
54 };
56 TAILQ_HEAD(got_commit_graph_iter_list, got_commit_graph_node);
58 struct got_commit_graph_branch_tip {
59 struct got_object_id id;
60 struct got_commit_graph_node *node;
61 };
63 struct got_commit_graph {
64 /* The set of all commits we have traversed. */
65 struct got_object_idset *node_ids;
67 /* The commit at which traversal began (youngest commit in node_ids). */
68 struct got_commit_graph_node *head_node;
70 int flags;
71 #define GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL 0x01
73 /*
74 * A set of object IDs of known parent commits which we have not yet
75 * traversed. Each commit ID in this set represents a branch in commit
76 * history: Either the first-parent branch of the head node, or another
77 * branch corresponding to a traversed merge commit for which we have
78 * not traversed a branch point commit yet.
79 *
80 * Whenever we add a commit with a matching ID to the graph, we remove
81 * its corresponding element from this set, and add new elements for
82 * each of that commit's parent commits which were not traversed yet.
83 *
84 * When API users ask us to fetch more commits, we fetch commits from
85 * all currently open branches. This allows API users to process
86 * commits in linear order even though the history contains branches.
87 */
88 struct got_object_idset *open_branches;
90 /* Copy of known branch tips for fetch_commits_from_open_branches(). */
91 struct got_commit_graph_branch_tip *tips;
92 size_t ntips;
93 #define GOT_COMMIT_GRAPH_MIN_TIPS 10 /* minimum amount of tips to allocate */
95 /* Path of tree entry of interest to the API user. */
96 char *path;
98 /* The next commit to return when the API user asks for one. */
99 struct got_commit_graph_node *iter_node;
101 /* The graph iteration list contains all nodes in sorted order. */
102 struct got_commit_graph_iter_list iter_list;
103 };
105 static struct got_commit_graph *
106 alloc_graph(const char *path)
108 struct got_commit_graph *graph;
110 graph = calloc(1, sizeof(*graph));
111 if (graph == NULL)
112 return NULL;
114 graph->path = strdup(path);
115 if (graph->path == NULL) {
116 free(graph);
117 return NULL;
120 graph->node_ids = got_object_idset_alloc();
121 if (graph->node_ids == NULL) {
122 free(graph->path);
123 free(graph);
124 return NULL;
127 graph->open_branches = got_object_idset_alloc();
128 if (graph->open_branches == NULL) {
129 got_object_idset_free(graph->node_ids);
130 free(graph->path);
131 free(graph);
132 return NULL;
135 TAILQ_INIT(&graph->iter_list);
136 return graph;
139 #if 0
140 static int
141 is_head_node(struct got_commit_graph_node *node)
143 return node->nchildren == 0;
146 int
147 is_branch_point(struct got_commit_graph_node *node)
149 return node->nchildren > 1;
152 static int
153 is_root_node(struct got_commit_graph_node *node)
155 return node->nparents == 0;
157 #endif
159 static int
160 is_merge_point(struct got_commit_graph_node *node)
162 return node->nparents > 1;
165 static const struct got_error *
166 detect_changed_path(int *changed, struct got_commit_object *commit,
167 struct got_object_id *commit_id, const char *path,
168 struct got_repository *repo)
170 const struct got_error *err = NULL;
171 struct got_commit_object *pcommit = NULL;
172 struct got_tree_object *tree = NULL, *ptree = NULL;
173 struct got_object_qid *pid;
175 if (got_path_is_root_dir(path)) {
176 *changed = 1;
177 return NULL;
180 *changed = 0;
182 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
183 if (err)
184 return err;
186 pid = SIMPLEQ_FIRST(&commit->parent_ids);
187 if (pid == NULL) {
188 struct got_object_id *obj_id;
189 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
190 if (err) {
191 if (err->code == GOT_ERR_NO_OBJ)
192 err = NULL;
193 } else
194 *changed = 1; /* The path was created in this commit. */
195 free(obj_id);
196 } else {
197 err = got_object_open_as_commit(&pcommit, repo, pid->id);
198 if (err)
199 goto done;
201 err = got_object_open_as_tree(&ptree, repo, pcommit->tree_id);
202 if (err)
203 goto done;
205 err = got_object_tree_path_changed(changed, tree, ptree, path,
206 repo);
208 done:
209 if (tree)
210 got_object_tree_close(tree);
211 if (ptree)
212 got_object_tree_close(ptree);
213 if (pcommit)
214 got_object_commit_close(pcommit);
215 return err;
218 static void
219 add_node_to_iter_list(struct got_commit_graph *graph,
220 struct got_commit_graph_node *node,
221 struct got_commit_graph_node *child_node)
223 struct got_commit_graph_node *n, *next;
225 if (TAILQ_EMPTY(&graph->iter_list)) {
226 TAILQ_INSERT_HEAD(&graph->iter_list, node, entry);
227 graph->iter_node = node;
228 return;
231 n = graph->iter_node;
232 /* Ensure that an iteration in progress will see this new commit. */
233 while (n) {
234 next = TAILQ_NEXT(n, entry);
235 if (next && node->commit_timestamp >= next->commit_timestamp) {
236 TAILQ_INSERT_BEFORE(next, node, entry);
237 return;
239 n = next;
241 TAILQ_INSERT_TAIL(&graph->iter_list, node, entry);
244 static const struct got_error *
245 add_vertex(struct got_object_id_queue *ids, struct got_object_id *id)
247 const struct got_error *err = NULL;
248 struct got_object_qid *qid;
250 err = got_object_qid_alloc(&qid, id);
251 if (err)
252 return err;
254 SIMPLEQ_INSERT_TAIL(ids, qid, entry);
255 return NULL;
258 static const struct got_error *
259 close_branch(struct got_commit_graph *graph, struct got_object_id *commit_id)
261 const struct got_error *err;
263 err = got_object_idset_remove(NULL, graph->open_branches, commit_id);
264 if (err && err->code != GOT_ERR_NO_OBJ)
265 return err;
266 return NULL;
269 static const struct got_error *
270 advance_branch(struct got_commit_graph *graph,
271 struct got_commit_graph_node *node,
272 struct got_object_id *commit_id, struct got_commit_object *commit,
273 struct got_repository *repo)
275 const struct got_error *err;
276 struct got_object_qid *qid;
278 err = close_branch(graph, commit_id);
279 if (err)
280 return err;
282 if (graph->flags & GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL) {
283 qid = SIMPLEQ_FIRST(&commit->parent_ids);
284 if (qid == NULL)
285 return NULL;
286 err = got_object_idset_add(graph->open_branches, qid->id, node);
287 return err;
290 /*
291 * If we are graphing commits for a specific path, skip branches
292 * which do not contribute any content to this path.
293 */
294 if (is_merge_point(node) && !got_path_is_root_dir(graph->path)) {
295 struct got_object_id *id, *merged_id, *prev_id = NULL;
296 int branches_differ = 0;
298 err = got_object_id_by_path(&merged_id, repo, commit_id,
299 graph->path);
300 if (err)
301 return err;
303 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
304 if (got_object_idset_get(graph->node_ids, qid->id))
305 continue; /* parent already traversed */
307 err = got_object_id_by_path(&id, repo, qid->id,
308 graph->path);
309 if (err) {
310 if (err->code == GOT_ERR_NO_OBJ) {
311 branches_differ = 1;
312 continue;
314 return err;
317 if (prev_id) {
318 if (!branches_differ &&
319 got_object_id_cmp(merged_id, prev_id) != 0)
320 branches_differ = 1;
321 } else
322 prev_id = id;
324 /*
325 * If a branch has created the merged content we can
326 * skip any other branches.
327 */
328 if (got_object_id_cmp(merged_id, id) == 0) {
329 err = got_object_idset_add(graph->open_branches,
330 qid->id, node);
331 return err;
335 /*
336 * If the path's content is the same on all branches,
337 * follow the first parent only.
338 */
339 if (!branches_differ) {
340 qid = SIMPLEQ_FIRST(&commit->parent_ids);
341 if (qid == NULL)
342 return NULL;
343 if (got_object_idset_get(graph->node_ids, qid->id))
344 return NULL; /* parent already traversed */
345 if (got_object_idset_get(graph->open_branches, qid->id))
346 return NULL;
347 return got_object_idset_add(graph->open_branches,
348 qid->id, node);
352 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
353 if (got_object_idset_get(graph->node_ids, qid->id))
354 continue; /* parent already traversed */
355 if (got_object_idset_get(graph->open_branches, qid->id))
356 continue;
357 err = got_object_idset_add(graph->open_branches, qid->id, node);
358 if (err)
359 return err;
362 return NULL;
365 static void
366 free_node(struct got_commit_graph_node *node)
368 while (!SIMPLEQ_EMPTY(&node->parent_ids)) {
369 struct got_object_qid *pid = SIMPLEQ_FIRST(&node->parent_ids);
370 SIMPLEQ_REMOVE_HEAD(&node->parent_ids, entry);
371 got_object_qid_free(pid);
373 free(node);
376 static const struct got_error *
377 add_node(struct got_commit_graph_node **new_node, int *changed,
378 struct got_commit_graph *graph, struct got_object_id *commit_id,
379 struct got_commit_object *commit, struct got_commit_graph_node *child_node,
380 struct got_repository *repo)
382 const struct got_error *err = NULL;
383 struct got_commit_graph_node *node;
384 struct got_object_qid *pid;
385 int branch_done = 0;
387 *new_node = NULL;
388 *changed = 0;
390 node = calloc(1, sizeof(*node));
391 if (node == NULL)
392 return got_error_from_errno();
394 memcpy(&node->id, commit_id, sizeof(node->id));
395 SIMPLEQ_INIT(&node->parent_ids);
396 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
397 err = add_vertex(&node->parent_ids, pid->id);
398 if (err) {
399 free_node(node);
400 return err;
402 node->nparents++;
404 node->commit_timestamp = commit->committer_time;
406 err = got_object_idset_add(graph->node_ids, &node->id, node);
407 if (err) {
408 free_node(node);
409 return err;
412 err = detect_changed_path(changed, commit, commit_id, graph->path,
413 repo);
414 if (err) {
415 if (err->code == GOT_ERR_NO_OBJ) {
416 /*
417 * History of the path stops here on the current
418 * branch. Keep going on other branches.
419 */
420 err = NULL;
421 branch_done = 1;
422 } else {
423 free_node(node);
424 return err;
428 if (*changed)
429 add_node_to_iter_list(graph, node, child_node);
431 if (branch_done)
432 err = close_branch(graph, commit_id);
433 else
434 err = advance_branch(graph, node, commit_id, commit, repo);
435 if (err)
436 free_node(node);
437 else
438 *new_node = node;
440 return err;
443 const struct got_error *
444 got_commit_graph_open(struct got_commit_graph **graph,
445 struct got_object_id *commit_id, const char *path,
446 int first_parent_traversal, struct got_repository *repo)
448 const struct got_error *err = NULL;
449 struct got_commit_object *commit;
450 int changed;
452 *graph = NULL;
454 err = got_object_open_as_commit(&commit, repo, commit_id);
455 if (err)
456 return err;
458 /* The path must exist in our initial commit. */
459 if (!got_path_is_root_dir(path)) {
460 struct got_object_id *obj_id;
461 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
462 if (err)
463 return err;
464 free(obj_id);
467 *graph = alloc_graph(path);
468 if (*graph == NULL) {
469 got_object_commit_close(commit);
470 return got_error_from_errno();
473 if (first_parent_traversal)
474 (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
476 err = add_node(&(*graph)->head_node, &changed, *graph, commit_id,
477 commit, NULL, repo);
478 got_object_commit_close(commit);
479 if (err) {
480 got_commit_graph_close(*graph);
481 *graph = NULL;
482 return err;
485 return NULL;
488 struct gather_branch_tips_arg {
489 struct got_commit_graph_branch_tip *tips;
490 int ntips;
491 };
493 static void
494 gather_branch_tips(struct got_object_id *id, void *data, void *arg)
496 struct gather_branch_tips_arg *a = arg;
497 memcpy(&a->tips[a->ntips].id, id, sizeof(*id));
498 a->tips[a->ntips].node = data;
499 a->ntips++;
502 static const struct got_error *
503 fetch_commits_from_open_branches(int *ncommits,
504 struct got_object_id **changed_id, struct got_commit_graph *graph,
505 struct got_repository *repo)
507 const struct got_error *err;
508 struct gather_branch_tips_arg arg;
509 int i;
511 *ncommits = 0;
512 *changed_id = NULL;
514 arg.ntips = got_object_idset_num_elements(graph->open_branches);
515 if (arg.ntips == 0)
516 return NULL;
518 /*
519 * Adding nodes to the graph might change the graph's open
520 * branches state. Create a local copy of the current state.
521 */
522 if (graph->ntips < arg.ntips) {
523 struct got_commit_graph_branch_tip *tips;
524 if (arg.ntips < GOT_COMMIT_GRAPH_MIN_TIPS)
525 arg.ntips = GOT_COMMIT_GRAPH_MIN_TIPS;
526 tips = reallocarray(graph->tips, arg.ntips, sizeof(*tips));
527 if (tips == NULL)
528 return got_error_from_errno();
529 graph->tips = tips;
530 graph->ntips = arg.ntips;
532 arg.ntips = 0; /* reset; gather_branch_tips() will increment */
533 arg.tips = graph->tips;
534 got_object_idset_for_each(graph->open_branches,
535 gather_branch_tips, &arg);
537 for (i = 0; i < arg.ntips; i++) {
538 struct got_object_id *commit_id;
539 struct got_commit_graph_node *child_node, *new_node;
540 struct got_commit_object *commit;
541 int changed;
543 commit_id = &graph->tips[i].id;
544 child_node = graph->tips[i].node;
546 err = got_object_open_as_commit(&commit, repo, commit_id);
547 if (err)
548 break;
550 err = add_node(&new_node, &changed, graph, commit_id, commit,
551 child_node, repo);
552 if (changed && *changed_id == NULL)
553 *changed_id = commit_id;
554 got_object_commit_close(commit);
555 if (err)
556 break;
557 if (new_node)
558 (*ncommits)++;
561 return err;
564 const struct got_error *
565 got_commit_graph_fetch_commits(struct got_commit_graph *graph, int limit,
566 struct got_repository *repo)
568 const struct got_error *err;
569 int nfetched = 0, ncommits;
570 struct got_object_id *changed_id = NULL;
572 while (nfetched < limit) {
573 err = fetch_commits_from_open_branches(&ncommits,
574 &changed_id, graph, repo);
575 if (err)
576 return err;
577 if (ncommits == 0)
578 break;
579 if (changed_id)
580 nfetched += ncommits;
583 return NULL;
586 static void
587 free_node_iter(struct got_object_id *id, void *data, void *arg)
589 struct got_commit_graph_node *node = data;
590 free_node(node);
593 void
594 got_commit_graph_close(struct got_commit_graph *graph)
596 got_object_idset_free(graph->open_branches);
597 got_object_idset_for_each(graph->node_ids, free_node_iter, NULL);
598 got_object_idset_free(graph->node_ids);
599 free(graph->tips);
600 free(graph->path);
601 free(graph);
604 const struct got_error *
605 got_commit_graph_iter_start(struct got_commit_graph *graph,
606 struct got_object_id *id, struct got_repository *repo)
608 const struct got_error *err = NULL;
609 struct got_commit_graph_node *start_node;
610 struct got_commit_object *commit;
611 int changed;
613 start_node = got_object_idset_get(graph->node_ids, id);
614 if (start_node == NULL)
615 return got_error(GOT_ERR_NO_OBJ);
617 err = got_object_open_as_commit(&commit, repo, &start_node->id);
618 if (err)
619 return err;
621 err = detect_changed_path(&changed, commit, &start_node->id,
622 graph->path, repo);
623 if (err) {
624 got_object_commit_close(commit);
625 return err;
628 if (!changed) {
629 /* Locate first commit which changed graph->path. */
630 struct got_object_id *changed_id = NULL;
631 while (changed_id == NULL) {
632 int ncommits;
633 err = fetch_commits_from_open_branches(&ncommits,
634 &changed_id, graph, repo);
635 if (err) {
636 got_object_commit_close(commit);
637 return err;
640 start_node = got_object_idset_get(graph->node_ids, changed_id);
642 got_object_commit_close(commit);
644 graph->iter_node = start_node;
645 return NULL;
648 const struct got_error *
649 got_commit_graph_iter_next(struct got_object_id **id,
650 struct got_commit_graph *graph)
652 *id = NULL;
654 if (graph->iter_node == NULL) {
655 /* We are done iterating, or iteration was not started. */
656 return got_error(GOT_ERR_ITER_COMPLETED);
659 if (graph->iter_node ==
660 TAILQ_LAST(&graph->iter_list, got_commit_graph_iter_list) &&
661 got_object_idset_num_elements(graph->open_branches) == 0) {
662 /* We are done iterating. */
663 *id = &graph->iter_node->id;
664 graph->iter_node = NULL;
665 return NULL;
668 if (TAILQ_NEXT(graph->iter_node, entry) == NULL)
669 return got_error(GOT_ERR_ITER_NEED_MORE);
671 *id = &graph->iter_node->id;
672 graph->iter_node = TAILQ_NEXT(graph->iter_node, entry);
673 return NULL;