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 <limits.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sha1.h>
27 #include <zlib.h>
28 #include <ctype.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;
43 time_t timestamp;
45 /* Used during graph iteration. */
46 TAILQ_ENTRY(got_commit_graph_node) entry;
47 };
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;
55 };
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
64 /*
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.
70 *
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.
74 *
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.
78 */
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;
88 /* The next commit to return when the API user asks for one. */
89 struct got_commit_graph_node *iter_node;
91 /* The graph iteration list contains all nodes in sorted order. */
92 struct got_commit_graph_iter_list iter_list;
93 };
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)
99 {
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 = SIMPLEQ_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_id, 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)
152 struct got_commit_graph_node *n, *next;
154 if (TAILQ_EMPTY(&graph->iter_list)) {
155 TAILQ_INSERT_HEAD(&graph->iter_list, node, entry);
156 graph->iter_node = node;
157 return;
160 n = graph->iter_node;
161 /* Ensure that an iteration in progress will see this new commit. */
162 while (n) {
163 next = TAILQ_NEXT(n, entry);
164 if (next && node->timestamp >= next->timestamp) {
165 TAILQ_INSERT_BEFORE(next, node, entry);
166 return;
168 n = next;
170 TAILQ_INSERT_TAIL(&graph->iter_list, node, entry);
173 static const struct got_error *
174 close_branch(struct got_commit_graph *graph, struct got_object_id *commit_id)
176 const struct got_error *err;
178 err = got_object_idset_remove(NULL, graph->open_branches, commit_id);
179 if (err && err->code != GOT_ERR_NO_OBJ)
180 return err;
181 return NULL;
184 static const struct got_error *
185 advance_branch(struct got_commit_graph *graph,
186 struct got_commit_graph_node *node,
187 struct got_object_id *commit_id, struct got_commit_object *commit,
188 struct got_repository *repo)
190 const struct got_error *err;
191 struct got_object_qid *qid;
193 err = close_branch(graph, commit_id);
194 if (err)
195 return err;
197 if (graph->flags & GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL) {
198 qid = SIMPLEQ_FIRST(&commit->parent_ids);
199 if (qid == NULL ||
200 got_object_idset_get(graph->open_branches, qid->id))
201 return NULL;
202 return got_object_idset_add(graph->open_branches,
203 qid->id, node);
206 /*
207 * If we are graphing commits for a specific path, skip branches
208 * which do not contribute any content to this path.
209 */
210 if (commit->nparents > 1 && !got_path_is_root_dir(graph->path)) {
211 struct got_object_id *merged_id, *prev_id = NULL;
212 int branches_differ = 0;
214 err = got_object_id_by_path(&merged_id, repo, commit_id,
215 graph->path);
216 if (err)
217 return err;
219 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
220 struct got_object_id *id;
222 if (got_object_idset_get(graph->open_branches, qid->id))
223 continue;
225 err = got_object_id_by_path(&id, repo, qid->id,
226 graph->path);
227 if (err) {
228 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
229 branches_differ = 1;
230 continue;
232 free(merged_id);
233 free(prev_id);
234 return err;
237 if (prev_id) {
238 if (!branches_differ &&
239 got_object_id_cmp(id, prev_id) != 0)
240 branches_differ = 1;
241 free(prev_id);
243 prev_id = id;
245 /*
246 * If a branch has created the merged content we can
247 * skip any other branches.
248 */
249 if (got_object_id_cmp(merged_id, id) == 0) {
250 err = got_object_idset_add(graph->open_branches,
251 qid->id, node);
252 free(merged_id);
253 free(id);
254 return err;
258 free(prev_id);
259 prev_id = NULL;
260 free(merged_id);
261 merged_id = NULL;
263 /*
264 * If the path's content is the same on all branches,
265 * follow the first parent only.
266 */
267 if (!branches_differ) {
268 qid = SIMPLEQ_FIRST(&commit->parent_ids);
269 if (qid == NULL)
270 return NULL;
271 if (got_object_idset_get(graph->open_branches, qid->id))
272 return NULL;
273 if (got_object_idset_get(graph->node_ids, qid->id))
274 return NULL; /* parent already traversed */
275 return got_object_idset_add(graph->open_branches,
276 qid->id, node);
280 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
281 if (got_object_idset_get(graph->open_branches, qid->id))
282 continue;
283 if (got_object_idset_get(graph->node_ids, qid->id))
284 continue; /* parent already traversed */
285 err = got_object_idset_add(graph->open_branches, qid->id, node);
286 if (err)
287 return err;
290 return NULL;
293 static const struct got_error *
294 add_node(struct got_commit_graph_node **new_node,
295 struct got_commit_graph *graph,
296 struct got_object_id *commit_id,
297 struct got_commit_object *commit,
298 struct got_repository *repo)
300 const struct got_error *err = NULL;
301 struct got_commit_graph_node *node;
303 *new_node = NULL;
305 node = calloc(1, sizeof(*node));
306 if (node == NULL)
307 return got_error_from_errno("calloc");
309 memcpy(&node->id, commit_id, sizeof(node->id));
310 node->timestamp = commit->committer_time;
312 err = got_object_idset_add(graph->node_ids, &node->id, node);
313 if (err)
314 free(node);
315 else
316 *new_node = node;
317 return err;
320 const struct got_error *
321 detect_change(int *changed, int *branch_done, const char *path,
322 struct got_commit_object *commit, struct got_object_id *commit_id,
323 struct got_repository *repo)
325 const struct got_error *err;
327 *changed = 0;
328 *branch_done = 0;
330 err = detect_changed_path(changed, commit, commit_id, path, repo);
331 if (err) {
332 if (err->code != GOT_ERR_NO_OBJ)
333 return err;
334 /*
335 * History of the path stops here on the current
336 * branch. Keep going on other branches.
337 */
338 err = NULL;
339 *branch_done = 1;
342 return NULL;
345 const struct got_error *
346 got_commit_graph_open(struct got_commit_graph **graph,
347 const char *path, int first_parent_traversal)
349 const struct got_error *err = NULL;
351 *graph = calloc(1, sizeof(**graph));
352 if (*graph == NULL)
353 return got_error_from_errno("calloc");
355 TAILQ_INIT(&(*graph)->iter_list);
357 (*graph)->path = strdup(path);
358 if ((*graph)->path == NULL) {
359 err = got_error_from_errno("strdup");
360 goto done;
363 (*graph)->node_ids = got_object_idset_alloc();
364 if ((*graph)->node_ids == NULL) {
365 err = got_error_from_errno("got_object_idset_alloc");
366 goto done;
369 (*graph)->open_branches = got_object_idset_alloc();
370 if ((*graph)->open_branches == NULL) {
371 err = got_error_from_errno("got_object_idset_alloc");
372 goto done;
375 if (first_parent_traversal)
376 (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
377 done:
378 if (err) {
379 got_commit_graph_close(*graph);
380 *graph = NULL;
382 return err;
385 struct add_branch_tip_arg {
386 struct got_commit_graph_branch_tip *tips;
387 int ntips;
388 struct got_repository *repo;
389 struct got_commit_graph *graph;
390 };
392 static const struct got_error *
393 add_branch_tip(struct got_object_id *commit_id, void *data, void *arg)
395 const struct got_error *err;
396 struct add_branch_tip_arg *a = arg;
397 struct got_commit_graph_node *new_node;
398 struct got_commit_object *commit;
400 err = got_object_open_as_commit(&commit, a->repo, commit_id);
401 if (err)
402 return err;
404 err = add_node(&new_node, a->graph, commit_id, commit, a->repo);
405 if (err)
406 return err;
408 a->tips[a->ntips].commit_id = new_node ? &new_node->id : NULL;
409 a->tips[a->ntips].commit = commit;
410 a->tips[a->ntips].new_node = new_node;
411 a->ntips++;
413 return NULL;
416 static const struct got_error *
417 fetch_commits_from_open_branches(int *nfetched,
418 struct got_object_id **changed_id, struct got_commit_graph *graph,
419 struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
421 const struct got_error *err;
422 struct add_branch_tip_arg arg;
423 int i, ntips;
425 *nfetched = 0;
426 if (changed_id)
427 *changed_id = NULL;
429 ntips = got_object_idset_num_elements(graph->open_branches);
430 if (ntips == 0)
431 return NULL;
433 /* (Re-)allocate branch tips array if necessary. */
434 if (graph->ntips < ntips) {
435 struct got_commit_graph_branch_tip *tips;
436 tips = recallocarray(graph->tips, graph->ntips, ntips,
437 sizeof(*tips));
438 if (tips == NULL)
439 return got_error_from_errno("recallocarray");
440 graph->tips = tips;
441 graph->ntips = ntips;
443 arg.tips = graph->tips;
444 arg.ntips = 0; /* add_branch_tip() will increment */
445 arg.repo = repo;
446 arg.graph = graph;
447 err = got_object_idset_for_each(graph->open_branches, add_branch_tip,
448 &arg);
449 if (err)
450 goto done;
452 for (i = 0; i < arg.ntips; i++) {
453 struct got_object_id *commit_id;
454 struct got_commit_object *commit;
455 struct got_commit_graph_node *new_node;
456 int branch_done, changed;
458 if (cancel_cb) {
459 err = (*cancel_cb)(cancel_arg);
460 if (err)
461 break;
464 commit_id = arg.tips[i].commit_id;
465 commit = arg.tips[i].commit;
466 new_node = arg.tips[i].new_node;
468 err = detect_change(&changed, &branch_done, graph->path,
469 commit, commit_id, repo);
470 if (err)
471 break;
472 if (changed) {
473 add_node_to_iter_list(graph, new_node);
474 if (changed_id && *changed_id == NULL)
475 *changed_id = commit_id;
477 if (branch_done)
478 err = close_branch(graph, commit_id);
479 else
480 err = advance_branch(graph, new_node, commit_id,
481 commit, repo);
482 if (err)
483 break;
485 done:
486 for (i = 0; i < arg.ntips; i++)
487 got_object_commit_close(arg.tips[i].commit);
488 (*nfetched) = arg.ntips;
489 return err;
492 static const struct got_error *
493 free_node_iter(struct got_object_id *id, void *data, void *arg)
495 struct got_commit_graph_node *node = data;
496 free(node);
497 return NULL;
500 void
501 got_commit_graph_close(struct got_commit_graph *graph)
503 if (graph->open_branches)
504 got_object_idset_free(graph->open_branches);
505 got_object_idset_for_each(graph->node_ids, free_node_iter, NULL);
506 if (graph->node_ids)
507 got_object_idset_free(graph->node_ids);
508 free(graph->tips);
509 free(graph->path);
510 free(graph);
513 const struct got_error *
514 got_commit_graph_iter_start(struct got_commit_graph *graph,
515 struct got_object_id *id, struct got_repository *repo,
516 got_cancel_cb cancel_cb, void *cancel_arg)
518 const struct got_error *err = NULL;
519 struct got_commit_graph_node *start_node;
520 struct got_commit_object *commit;
521 int changed, branch_done;
523 if (!TAILQ_EMPTY(&graph->iter_list))
524 return got_error(GOT_ERR_ITER_BUSY);
526 err = got_object_open_as_commit(&commit, repo, id);
527 if (err)
528 return err;
530 err = add_node(&start_node, graph, id, commit, repo);
531 if (err)
532 goto done;
534 err = detect_change(&changed, &branch_done, graph->path,
535 commit, id, repo);
536 if (err)
537 goto done;
538 if (changed)
539 add_node_to_iter_list(graph, start_node);
541 err = advance_branch(graph, start_node, id, commit, repo);
542 if (err)
543 goto done;
545 if (!changed) {
546 /* Locate first commit which changed graph->path. */
547 struct got_object_id *changed_id = NULL;
548 while (changed_id == NULL) {
549 int ncommits;
550 err = fetch_commits_from_open_branches(&ncommits,
551 &changed_id, graph, repo, cancel_cb, cancel_arg);
552 if (err) {
553 got_object_commit_close(commit);
554 return err;
557 start_node = got_object_idset_get(graph->node_ids, changed_id);
559 done:
560 got_object_commit_close(commit);
561 if (err == NULL)
562 graph->iter_node = start_node;
563 return err;
566 const struct got_error *
567 got_commit_graph_iter_next(struct got_object_id **id,
568 struct got_commit_graph *graph, struct got_repository *repo,
569 got_cancel_cb cancel_cb, void *cancel_arg)
571 const struct got_error *err = NULL;
573 *id = NULL;
575 if (graph->iter_node == NULL) {
576 /* We are done iterating, or iteration was not started. */
577 return got_error(GOT_ERR_ITER_COMPLETED);
580 if (graph->iter_node ==
581 TAILQ_LAST(&graph->iter_list, got_commit_graph_iter_list) &&
582 got_object_idset_num_elements(graph->open_branches) == 0) {
583 /* We are done iterating. */
584 *id = &graph->iter_node->id;
585 graph->iter_node = NULL;
586 return NULL;
589 while (TAILQ_NEXT(graph->iter_node, entry) == NULL &&
590 got_object_idset_num_elements(graph->open_branches) > 0) {
591 int ncommits;
592 struct got_object_id *changed_id;
593 err = fetch_commits_from_open_branches(&ncommits,
594 &changed_id, graph, repo, cancel_cb, cancel_arg);
595 if (err)
596 return err;
599 *id = &graph->iter_node->id;
600 graph->iter_node = TAILQ_NEXT(graph->iter_node, entry);
601 return NULL;
604 const struct got_error *
605 got_commit_graph_find_youngest_common_ancestor(struct got_object_id **yca_id,
606 struct got_object_id *commit_id, struct got_object_id *commit_id2,
607 struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
609 const struct got_error *err = NULL;
610 struct got_commit_graph *graph = NULL, *graph2 = NULL;
611 int completed = 0, completed2 = 0;
612 struct got_object_idset *commit_ids;
614 *yca_id = NULL;
616 commit_ids = got_object_idset_alloc();
617 if (commit_ids == NULL)
618 return got_error_from_errno("got_object_idset_alloc");
620 err = got_commit_graph_open(&graph, "/", 1);
621 if (err)
622 goto done;
624 err = got_commit_graph_open(&graph2, "/", 1);
625 if (err)
626 goto done;
628 err = got_commit_graph_iter_start(graph, commit_id, repo,
629 cancel_cb, cancel_arg);
630 if (err)
631 goto done;
633 err = got_commit_graph_iter_start(graph2, commit_id2, repo,
634 cancel_cb, cancel_arg);
635 if (err)
636 goto done;
638 for (;;) {
639 struct got_object_id *id = NULL, *id2 = NULL;
641 if (cancel_cb) {
642 err = (*cancel_cb)(cancel_arg);
643 if (err)
644 break;
647 if (!completed) {
648 err = got_commit_graph_iter_next(&id, graph, repo,
649 cancel_cb, cancel_arg);
650 if (err) {
651 if (err->code != GOT_ERR_ITER_COMPLETED)
652 break;
653 err = NULL;
654 completed = 1;
658 if (!completed2) {
659 err = got_commit_graph_iter_next(&id2, graph2, repo,
660 cancel_cb, cancel_arg);
661 if (err) {
662 if (err->code != GOT_ERR_ITER_COMPLETED)
663 break;
664 err = NULL;
665 completed2 = 1;
669 if (id) {
670 if (got_object_idset_get(commit_ids, id)) {
671 *yca_id = got_object_id_dup(id);
672 if (*yca_id)
673 break;
674 err = got_error_from_errno("got_object_id_dup");
675 break;
678 err = got_object_idset_add(commit_ids, id, id);
679 if (err)
680 break;
682 if (id2) {
683 if (got_object_idset_get(commit_ids, id2)) {
684 *yca_id = got_object_id_dup(id2);
685 if (*yca_id)
686 break;
687 err = got_error_from_errno("got_object_id_dup");
688 break;
691 err = got_object_idset_add(commit_ids, id2, id2);
692 if (err)
693 break;
696 if (completed && completed2) {
697 err = got_error(GOT_ERR_ANCESTRY);
698 break;
702 done:
703 got_object_idset_free(commit_ids);
704 if (graph)
705 got_commit_graph_close(graph);
706 if (graph2)
707 got_commit_graph_close(graph2);
708 return err;