portable: release 0.93
[got-portable.git] / lib / commit_graph.c
blob3567f7635ec5cb569326cc57275b021ce3dd16e7
1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/queue.h>
21 #include <limits.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <zlib.h>
26 #include <ctype.h>
28 #include "got_compat.h"
30 #include "got_error.h"
31 #include "got_object.h"
32 #include "got_cancel.h"
33 #include "got_commit_graph.h"
34 #include "got_path.h"
36 #include "got_lib_delta.h"
37 #include "got_lib_inflate.h"
38 #include "got_lib_object.h"
39 #include "got_lib_object_idset.h"
41 struct got_commit_graph_node {
42 struct got_object_id id;
44 /* Used only during iteration. */
45 time_t timestamp;
46 TAILQ_ENTRY(got_commit_graph_node) entry;
49 TAILQ_HEAD(got_commit_graph_iter_list, got_commit_graph_node);
51 struct got_commit_graph_branch_tip {
52 struct got_object_id *commit_id;
53 struct got_commit_object *commit;
54 struct got_commit_graph_node *new_node;
57 struct got_commit_graph {
58 /* The set of all commits we have traversed. */
59 struct got_object_idset *node_ids;
61 int flags;
62 #define GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL 0x01
65 * A set of object IDs of known parent commits which we have not yet
66 * traversed. Each commit ID in this set represents a branch in commit
67 * history: Either the first-parent branch of the head node, or another
68 * branch corresponding to a traversed merge commit for which we have
69 * not traversed a branch point commit yet.
71 * Whenever we add a commit with a matching ID to the graph, we remove
72 * its corresponding element from this set, and add new elements for
73 * each of that commit's parent commits which were not traversed yet.
75 * When API users ask us to fetch more commits, we fetch commits from
76 * all currently open branches. This allows API users to process
77 * commits in linear order even though the history contains branches.
79 struct got_object_idset *open_branches;
81 /* Array of branch tips for fetch_commits_from_open_branches(). */
82 struct got_commit_graph_branch_tip *tips;
83 int ntips;
85 /* Path of tree entry of interest to the API user. */
86 char *path;
89 * Nodes which will be passed to the API user next, sorted by
90 * commit timestmap.
92 struct got_commit_graph_iter_list iter_list;
95 static const struct got_error *
96 detect_changed_path(int *changed, struct got_commit_object *commit,
97 struct got_object_id *commit_id, const char *path,
98 struct got_repository *repo)
100 const struct got_error *err = NULL;
101 struct got_commit_object *pcommit = NULL;
102 struct got_tree_object *tree = NULL, *ptree = NULL;
103 struct got_object_qid *pid;
105 if (got_path_is_root_dir(path)) {
106 *changed = 1;
107 return NULL;
110 *changed = 0;
112 pid = STAILQ_FIRST(&commit->parent_ids);
113 if (pid == NULL) {
114 struct got_object_id *obj_id;
115 err = got_object_id_by_path(&obj_id, repo, commit, path);
116 if (err) {
117 if (err->code == GOT_ERR_NO_TREE_ENTRY)
118 err = NULL;
119 } else
120 *changed = 1; /* The path was created in this commit. */
121 free(obj_id);
122 return err;
125 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
126 if (err)
127 return err;
129 err = got_object_open_as_commit(&pcommit, repo, &pid->id);
130 if (err)
131 goto done;
133 err = got_object_open_as_tree(&ptree, repo, pcommit->tree_id);
134 if (err)
135 goto done;
137 err = got_object_tree_path_changed(changed, tree, ptree, path, repo);
138 done:
139 if (tree)
140 got_object_tree_close(tree);
141 if (ptree)
142 got_object_tree_close(ptree);
143 if (pcommit)
144 got_object_commit_close(pcommit);
145 return err;
148 static void
149 add_node_to_iter_list(struct got_commit_graph *graph,
150 struct got_commit_graph_node *node, time_t committer_time)
152 struct got_commit_graph_node *n, *next;
154 node->timestamp = committer_time;
156 n = TAILQ_FIRST(&graph->iter_list);
157 while (n) {
158 next = TAILQ_NEXT(n, entry);
159 if (next && node->timestamp >= next->timestamp) {
160 TAILQ_INSERT_BEFORE(next, node, entry);
161 return;
163 n = next;
165 TAILQ_INSERT_TAIL(&graph->iter_list, node, entry);
168 static const struct got_error *
169 add_node(struct got_commit_graph_node **new_node,
170 struct got_commit_graph *graph, struct got_object_id *commit_id,
171 struct got_repository *repo)
173 const struct got_error *err = NULL;
174 struct got_commit_graph_node *node;
176 *new_node = NULL;
178 node = calloc(1, sizeof(*node));
179 if (node == NULL)
180 return got_error_from_errno("calloc");
182 memcpy(&node->id, commit_id, sizeof(node->id));
183 err = got_object_idset_add(graph->node_ids, &node->id, NULL);
184 if (err)
185 free(node);
186 else
187 *new_node = node;
188 return err;
192 * Ask got-read-pack to traverse first-parent history until a commit is
193 * encountered which modified graph->path, or until the pack file runs
194 * out of relevant commits. This is faster than sending an individual
195 * request for each commit stored in the pack file.
197 static const struct got_error *
198 packed_first_parent_traversal(int *ncommits_traversed,
199 struct got_commit_graph *graph, struct got_object_id *commit_id,
200 struct got_repository *repo)
202 const struct got_error *err = NULL;
203 struct got_object_id_queue traversed_commits;
204 struct got_object_qid *qid;
206 STAILQ_INIT(&traversed_commits);
207 *ncommits_traversed = 0;
209 err = got_traverse_packed_commits(&traversed_commits,
210 commit_id, graph->path, repo);
211 if (err)
212 return err;
214 /* Add all traversed commits to the graph... */
215 STAILQ_FOREACH(qid, &traversed_commits, entry) {
216 if (got_object_idset_contains(graph->open_branches, &qid->id))
217 continue;
218 if (got_object_idset_contains(graph->node_ids, &qid->id))
219 continue;
221 (*ncommits_traversed)++;
223 /* ... except the last commit is the new branch tip. */
224 if (STAILQ_NEXT(qid, entry) == NULL) {
225 err = got_object_idset_add(graph->open_branches,
226 &qid->id, NULL);
227 break;
230 err = got_object_idset_add(graph->node_ids, &qid->id, NULL);
231 if (err)
232 break;
235 got_object_id_queue_free(&traversed_commits);
236 return err;
239 static const struct got_error *
240 close_branch(struct got_commit_graph *graph, struct got_object_id *commit_id)
242 const struct got_error *err;
244 err = got_object_idset_remove(NULL, graph->open_branches, commit_id);
245 if (err && err->code != GOT_ERR_NO_OBJ)
246 return err;
247 return NULL;
250 static const struct got_error *
251 advance_branch(struct got_commit_graph *graph, struct got_object_id *commit_id,
252 struct got_commit_object *commit, struct got_repository *repo)
254 const struct got_error *err;
255 struct got_object_qid *qid;
257 err = close_branch(graph, commit_id);
258 if (err)
259 return err;
261 if (graph->flags & GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL) {
262 qid = STAILQ_FIRST(&commit->parent_ids);
263 if (qid == NULL ||
264 got_object_idset_contains(graph->open_branches, &qid->id))
265 return NULL;
267 * The root directory always changes by definition, and when
268 * logging the root we want to traverse consecutive commits
269 * even if they point at the same tree.
270 * But if we are looking for a specific path then we can avoid
271 * fetching packed commits which did not modify the path and
272 * only fetch their IDs. This speeds up 'got blame'.
274 if (!got_path_is_root_dir(graph->path) &&
275 (commit->flags & GOT_COMMIT_FLAG_PACKED)) {
276 int ncommits = 0;
277 err = packed_first_parent_traversal(&ncommits,
278 graph, &qid->id, repo);
279 if (err || ncommits > 0)
280 return err;
282 return got_object_idset_add(graph->open_branches,
283 &qid->id, NULL);
287 * If we are graphing commits for a specific path, skip branches
288 * which do not contribute any content to this path.
290 if (commit->nparents > 1 && !got_path_is_root_dir(graph->path)) {
291 struct got_object_id *merged_id, *prev_id = NULL;
292 int branches_differ = 0;
294 err = got_object_id_by_path(&merged_id, repo, commit,
295 graph->path);
296 if (err)
297 return err;
299 STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
300 struct got_object_id *id = NULL;
301 struct got_commit_object *pcommit = NULL;
303 if (got_object_idset_contains(graph->open_branches,
304 &qid->id))
305 continue;
307 err = got_object_open_as_commit(&pcommit, repo,
308 &qid->id);
309 if (err) {
310 free(merged_id);
311 free(prev_id);
312 return err;
314 err = got_object_id_by_path(&id, repo, pcommit,
315 graph->path);
316 got_object_commit_close(pcommit);
317 pcommit = NULL;
318 if (err) {
319 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
320 branches_differ = 1;
321 continue;
323 free(merged_id);
324 free(prev_id);
325 return err;
328 if (prev_id) {
329 if (!branches_differ &&
330 got_object_id_cmp(id, prev_id) != 0)
331 branches_differ = 1;
332 free(prev_id);
334 prev_id = id;
337 * If a branch has created the merged content we can
338 * skip any other branches.
340 if (got_object_id_cmp(merged_id, id) == 0) {
341 err = got_object_idset_add(graph->open_branches,
342 &qid->id, NULL);
343 free(merged_id);
344 free(id);
345 return err;
349 free(prev_id);
350 prev_id = NULL;
351 free(merged_id);
352 merged_id = NULL;
355 * If the path's content is the same on all branches,
356 * follow the first parent only.
358 if (!branches_differ) {
359 qid = STAILQ_FIRST(&commit->parent_ids);
360 if (qid == NULL)
361 return NULL;
362 if (got_object_idset_contains(graph->open_branches,
363 &qid->id))
364 return NULL;
365 if (got_object_idset_contains(graph->node_ids,
366 &qid->id))
367 return NULL; /* parent already traversed */
368 return got_object_idset_add(graph->open_branches,
369 &qid->id, NULL);
373 STAILQ_FOREACH(qid, &commit->parent_ids, entry) {
374 if (got_object_idset_contains(graph->open_branches, &qid->id))
375 continue;
376 if (got_object_idset_contains(graph->node_ids, &qid->id))
377 continue; /* parent already traversed */
378 err = got_object_idset_add(graph->open_branches, &qid->id,
379 NULL);
380 if (err)
381 return err;
384 return NULL;
387 const struct got_error *
388 got_commit_graph_open(struct got_commit_graph **graph,
389 const char *path, int first_parent_traversal)
391 const struct got_error *err = NULL;
393 *graph = calloc(1, sizeof(**graph));
394 if (*graph == NULL)
395 return got_error_from_errno("calloc");
397 TAILQ_INIT(&(*graph)->iter_list);
399 (*graph)->path = strdup(path);
400 if ((*graph)->path == NULL) {
401 err = got_error_from_errno("strdup");
402 goto done;
405 (*graph)->node_ids = got_object_idset_alloc();
406 if ((*graph)->node_ids == NULL) {
407 err = got_error_from_errno("got_object_idset_alloc");
408 goto done;
411 (*graph)->open_branches = got_object_idset_alloc();
412 if ((*graph)->open_branches == NULL) {
413 err = got_error_from_errno("got_object_idset_alloc");
414 goto done;
417 if (first_parent_traversal)
418 (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
419 done:
420 if (err) {
421 got_commit_graph_close(*graph);
422 *graph = NULL;
424 return err;
427 struct add_branch_tip_arg {
428 struct got_commit_graph_branch_tip *tips;
429 int ntips;
430 struct got_repository *repo;
431 struct got_commit_graph *graph;
434 static const struct got_error *
435 add_branch_tip(struct got_object_id *commit_id, void *data, void *arg)
437 const struct got_error *err;
438 struct add_branch_tip_arg *a = arg;
439 struct got_commit_graph_node *new_node;
440 struct got_commit_object *commit;
442 err = got_object_open_as_commit(&commit, a->repo, commit_id);
443 if (err)
444 return err;
446 err = add_node(&new_node, a->graph, commit_id, a->repo);
447 if (err) {
448 got_object_commit_close(commit);
449 return err;
452 a->tips[a->ntips].commit_id = &new_node->id;
453 a->tips[a->ntips].commit = commit;
454 a->tips[a->ntips].new_node = new_node;
455 a->ntips++;
457 return NULL;
460 static const struct got_error *
461 fetch_commits_from_open_branches(struct got_commit_graph *graph,
462 struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
464 const struct got_error *err;
465 struct add_branch_tip_arg arg;
466 int i, ntips;
468 ntips = got_object_idset_num_elements(graph->open_branches);
469 if (ntips == 0)
470 return NULL;
472 /* (Re-)allocate branch tips array if necessary. */
473 if (graph->ntips < ntips) {
474 struct got_commit_graph_branch_tip *tips;
475 tips = recallocarray(graph->tips, graph->ntips, ntips,
476 sizeof(*tips));
477 if (tips == NULL)
478 return got_error_from_errno("recallocarray");
479 graph->tips = tips;
480 graph->ntips = ntips;
482 arg.tips = graph->tips;
483 arg.ntips = 0; /* add_branch_tip() will increment */
484 arg.repo = repo;
485 arg.graph = graph;
486 err = got_object_idset_for_each(graph->open_branches, add_branch_tip,
487 &arg);
488 if (err)
489 goto done;
491 for (i = 0; i < arg.ntips; i++) {
492 struct got_object_id *commit_id;
493 struct got_commit_object *commit;
494 struct got_commit_graph_node *new_node;
495 int changed;
497 if (cancel_cb) {
498 err = (*cancel_cb)(cancel_arg);
499 if (err)
500 break;
503 commit_id = arg.tips[i].commit_id;
504 commit = arg.tips[i].commit;
505 new_node = arg.tips[i].new_node;
507 err = detect_changed_path(&changed, commit, commit_id,
508 graph->path, repo);
509 if (err) {
510 if (err->code != GOT_ERR_NO_OBJ)
511 break;
513 * History of the path stops here on the current
514 * branch. Keep going on other branches.
516 err = close_branch(graph, commit_id);
517 if (err)
518 break;
519 continue;
521 if (changed) {
522 add_node_to_iter_list(graph, new_node,
523 got_object_commit_get_committer_time(commit));
524 arg.tips[i].new_node = NULL;
526 err = advance_branch(graph, commit_id, commit, repo);
527 if (err)
528 break;
530 done:
531 for (i = 0; i < arg.ntips; i++) {
532 got_object_commit_close(arg.tips[i].commit);
533 free(arg.tips[i].new_node);
535 return err;
538 void
539 got_commit_graph_close(struct got_commit_graph *graph)
541 struct got_commit_graph_node *node;
543 while ((node = TAILQ_FIRST(&graph->iter_list))) {
544 TAILQ_REMOVE(&graph->iter_list, node, entry);
545 free(node);
548 if (graph->open_branches)
549 got_object_idset_free(graph->open_branches);
550 if (graph->node_ids)
551 got_object_idset_free(graph->node_ids);
552 free(graph->tips);
553 free(graph->path);
554 free(graph);
557 const struct got_error *
558 got_commit_graph_iter_start(struct got_commit_graph *graph,
559 struct got_object_id *id, struct got_repository *repo,
560 got_cancel_cb cancel_cb, void *cancel_arg)
562 const struct got_error *err = NULL;
564 if (!TAILQ_EMPTY(&graph->iter_list))
565 return got_error(GOT_ERR_ITER_BUSY);
567 err = got_object_idset_add(graph->open_branches, id, NULL);
568 if (err)
569 return err;
571 /* Locate first commit which changed graph->path. */
572 while (TAILQ_EMPTY(&graph->iter_list) &&
573 got_object_idset_num_elements(graph->open_branches) > 0) {
574 err = fetch_commits_from_open_branches(graph, repo,
575 cancel_cb, cancel_arg);
576 if (err)
577 return err;
580 if (TAILQ_EMPTY(&graph->iter_list)) {
581 const char *path;
582 if (got_path_is_root_dir(graph->path))
583 return got_error_no_obj(id);
584 path = graph->path;
585 while (path[0] == '/')
586 path++;
587 return got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
590 return NULL;
593 const struct got_error *
594 got_commit_graph_iter_next(struct got_object_id *id,
595 struct got_commit_graph *graph, struct got_repository *repo,
596 got_cancel_cb cancel_cb, void *cancel_arg)
598 const struct got_error *err = NULL;
599 struct got_commit_graph_node *node;
601 node = TAILQ_FIRST(&graph->iter_list);
602 if (node == NULL) {
603 /* We are done iterating, or iteration was not started. */
604 return got_error(GOT_ERR_ITER_COMPLETED);
607 while (TAILQ_NEXT(node, entry) == NULL &&
608 got_object_idset_num_elements(graph->open_branches) > 0) {
609 err = fetch_commits_from_open_branches(graph, repo,
610 cancel_cb, cancel_arg);
611 if (err)
612 return err;
615 memcpy(id, &node->id, sizeof(*id));
617 TAILQ_REMOVE(&graph->iter_list, node, entry);
618 free(node);
619 return NULL;
622 static const struct got_error *
623 find_yca_add_id(struct got_object_id **yca_id, struct got_commit_graph *graph,
624 struct got_object_idset *commit_ids, struct got_repository *repo,
625 got_cancel_cb cancel_cb, void *cancel_arg)
627 const struct got_error *err = NULL;
628 struct got_object_id id;
630 err = got_commit_graph_iter_next(&id, graph, repo, cancel_cb,
631 cancel_arg);
632 if (err)
633 return err;
635 if (got_object_idset_contains(commit_ids, &id)) {
636 *yca_id = got_object_id_dup(&id);
637 if (*yca_id == NULL)
638 err = got_error_from_errno("got_object_id_dup");
639 return err;
642 return got_object_idset_add(commit_ids, &id, NULL);
646 * Sets *yca_id to the youngest common ancestor of commit_id and
647 * commit_id2. Returns got_error(GOT_ERR_ANCESTRY) if they have no
648 * common ancestors.
650 * If first_parent_traversal is nonzero, only linear history is considered.
652 const struct got_error *
653 got_commit_graph_find_youngest_common_ancestor(struct got_object_id **yca_id,
654 struct got_object_id *commit_id, struct got_object_id *commit_id2,
655 int first_parent_traversal,
656 struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
658 const struct got_error *err = NULL;
659 struct got_commit_graph *graph = NULL, *graph2 = NULL;
660 int completed = 0, completed2 = 0;
661 struct got_object_idset *commit_ids;
663 *yca_id = NULL;
665 commit_ids = got_object_idset_alloc();
666 if (commit_ids == NULL)
667 return got_error_from_errno("got_object_idset_alloc");
669 err = got_commit_graph_open(&graph, "/", first_parent_traversal);
670 if (err)
671 goto done;
673 err = got_commit_graph_open(&graph2, "/", first_parent_traversal);
674 if (err)
675 goto done;
677 err = got_commit_graph_iter_start(graph, commit_id, repo,
678 cancel_cb, cancel_arg);
679 if (err)
680 goto done;
682 err = got_commit_graph_iter_start(graph2, commit_id2, repo,
683 cancel_cb, cancel_arg);
684 if (err)
685 goto done;
687 for (;;) {
688 if (cancel_cb) {
689 err = (*cancel_cb)(cancel_arg);
690 if (err)
691 break;
694 if (!completed) {
695 err = find_yca_add_id(yca_id, graph, commit_ids, repo,
696 cancel_cb, cancel_arg);
697 if (err) {
698 if (err->code != GOT_ERR_ITER_COMPLETED)
699 break;
700 err = NULL;
701 completed = 1;
703 if (*yca_id)
704 break;
707 if (!completed2) {
708 err = find_yca_add_id(yca_id, graph2, commit_ids, repo,
709 cancel_cb, cancel_arg);
710 if (err) {
711 if (err->code != GOT_ERR_ITER_COMPLETED)
712 break;
713 err = NULL;
714 completed2 = 1;
716 if (*yca_id)
717 break;
720 if (completed && completed2) {
721 err = got_error(GOT_ERR_ANCESTRY);
722 break;
725 done:
726 got_object_idset_free(commit_ids);
727 if (graph)
728 got_commit_graph_close(graph);
729 if (graph2)
730 got_commit_graph_close(graph2);
731 return err;