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, struct got_object_id *commit_id,
186 struct got_commit_object *commit, struct got_repository *repo)
188 const struct got_error *err;
189 struct got_object_qid *qid;
191 err = close_branch(graph, commit_id);
192 if (err)
193 return err;
195 if (graph->flags & GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL) {
196 qid = SIMPLEQ_FIRST(&commit->parent_ids);
197 if (qid == NULL ||
198 got_object_idset_contains(graph->open_branches, qid->id))
199 return NULL;
200 return got_object_idset_add(graph->open_branches,
201 qid->id, NULL);
204 /*
205 * If we are graphing commits for a specific path, skip branches
206 * which do not contribute any content to this path.
207 */
208 if (commit->nparents > 1 && !got_path_is_root_dir(graph->path)) {
209 struct got_object_id *merged_id, *prev_id = NULL;
210 int branches_differ = 0;
212 err = got_object_id_by_path(&merged_id, repo, commit_id,
213 graph->path);
214 if (err)
215 return err;
217 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
218 struct got_object_id *id;
220 if (got_object_idset_contains(graph->open_branches,
221 qid->id))
222 continue;
224 err = got_object_id_by_path(&id, repo, qid->id,
225 graph->path);
226 if (err) {
227 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
228 branches_differ = 1;
229 continue;
231 free(merged_id);
232 free(prev_id);
233 return err;
236 if (prev_id) {
237 if (!branches_differ &&
238 got_object_id_cmp(id, prev_id) != 0)
239 branches_differ = 1;
240 free(prev_id);
242 prev_id = id;
244 /*
245 * If a branch has created the merged content we can
246 * skip any other branches.
247 */
248 if (got_object_id_cmp(merged_id, id) == 0) {
249 err = got_object_idset_add(graph->open_branches,
250 qid->id, NULL);
251 free(merged_id);
252 free(id);
253 return err;
257 free(prev_id);
258 prev_id = NULL;
259 free(merged_id);
260 merged_id = NULL;
262 /*
263 * If the path's content is the same on all branches,
264 * follow the first parent only.
265 */
266 if (!branches_differ) {
267 qid = SIMPLEQ_FIRST(&commit->parent_ids);
268 if (qid == NULL)
269 return NULL;
270 if (got_object_idset_contains(graph->open_branches,
271 qid->id))
272 return NULL;
273 if (got_object_idset_contains(graph->node_ids,
274 qid->id))
275 return NULL; /* parent already traversed */
276 return got_object_idset_add(graph->open_branches,
277 qid->id, NULL);
281 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
282 if (got_object_idset_contains(graph->open_branches, qid->id))
283 continue;
284 if (got_object_idset_contains(graph->node_ids, qid->id))
285 continue; /* parent already traversed */
286 err = got_object_idset_add(graph->open_branches, qid->id, NULL);
287 if (err)
288 return err;
291 return NULL;
294 static const struct got_error *
295 add_node(struct got_commit_graph_node **new_node,
296 struct got_commit_graph *graph,
297 struct got_object_id *commit_id,
298 struct got_commit_object *commit,
299 struct got_repository *repo)
301 const struct got_error *err = NULL;
302 struct got_commit_graph_node *node;
304 *new_node = NULL;
306 node = calloc(1, sizeof(*node));
307 if (node == NULL)
308 return got_error_from_errno("calloc");
310 memcpy(&node->id, commit_id, sizeof(node->id));
311 node->timestamp = commit->committer_time;
313 err = got_object_idset_add(graph->node_ids, &node->id, NULL);
314 if (err)
315 free(node);
316 else
317 *new_node = node;
318 return err;
321 const struct got_error *
322 got_commit_graph_open(struct got_commit_graph **graph,
323 const char *path, int first_parent_traversal)
325 const struct got_error *err = NULL;
327 *graph = calloc(1, sizeof(**graph));
328 if (*graph == NULL)
329 return got_error_from_errno("calloc");
331 TAILQ_INIT(&(*graph)->iter_list);
333 (*graph)->path = strdup(path);
334 if ((*graph)->path == NULL) {
335 err = got_error_from_errno("strdup");
336 goto done;
339 (*graph)->node_ids = got_object_idset_alloc();
340 if ((*graph)->node_ids == NULL) {
341 err = got_error_from_errno("got_object_idset_alloc");
342 goto done;
345 (*graph)->open_branches = got_object_idset_alloc();
346 if ((*graph)->open_branches == NULL) {
347 err = got_error_from_errno("got_object_idset_alloc");
348 goto done;
351 if (first_parent_traversal)
352 (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
353 done:
354 if (err) {
355 got_commit_graph_close(*graph);
356 *graph = NULL;
358 return err;
361 struct add_branch_tip_arg {
362 struct got_commit_graph_branch_tip *tips;
363 int ntips;
364 struct got_repository *repo;
365 struct got_commit_graph *graph;
366 };
368 static const struct got_error *
369 add_branch_tip(struct got_object_id *commit_id, void *data, void *arg)
371 const struct got_error *err;
372 struct add_branch_tip_arg *a = arg;
373 struct got_commit_graph_node *new_node;
374 struct got_commit_object *commit;
376 err = got_object_open_as_commit(&commit, a->repo, commit_id);
377 if (err)
378 return err;
380 err = add_node(&new_node, a->graph, commit_id, commit, a->repo);
381 if (err)
382 return err;
384 a->tips[a->ntips].commit_id = new_node ? &new_node->id : NULL;
385 a->tips[a->ntips].commit = commit;
386 a->tips[a->ntips].new_node = new_node;
387 a->ntips++;
389 return NULL;
392 static const struct got_error *
393 fetch_commits_from_open_branches(struct got_commit_graph *graph,
394 struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
396 const struct got_error *err;
397 struct add_branch_tip_arg arg;
398 int i, ntips;
400 ntips = got_object_idset_num_elements(graph->open_branches);
401 if (ntips == 0)
402 return NULL;
404 /* (Re-)allocate branch tips array if necessary. */
405 if (graph->ntips < ntips) {
406 struct got_commit_graph_branch_tip *tips;
407 tips = recallocarray(graph->tips, graph->ntips, ntips,
408 sizeof(*tips));
409 if (tips == NULL)
410 return got_error_from_errno("recallocarray");
411 graph->tips = tips;
412 graph->ntips = ntips;
414 arg.tips = graph->tips;
415 arg.ntips = 0; /* add_branch_tip() will increment */
416 arg.repo = repo;
417 arg.graph = graph;
418 err = got_object_idset_for_each(graph->open_branches, add_branch_tip,
419 &arg);
420 if (err)
421 goto done;
423 for (i = 0; i < arg.ntips; i++) {
424 struct got_object_id *commit_id;
425 struct got_commit_object *commit;
426 struct got_commit_graph_node *new_node;
427 int changed;
429 if (cancel_cb) {
430 err = (*cancel_cb)(cancel_arg);
431 if (err)
432 break;
435 commit_id = arg.tips[i].commit_id;
436 commit = arg.tips[i].commit;
437 new_node = arg.tips[i].new_node;
439 err = detect_changed_path(&changed, commit, commit_id,
440 graph->path, repo);
441 if (err) {
442 if (err->code != GOT_ERR_NO_OBJ)
443 break;
444 /*
445 * History of the path stops here on the current
446 * branch. Keep going on other branches.
447 */
448 err = close_branch(graph, commit_id);
449 if (err)
450 break;
451 continue;
453 if (changed)
454 add_node_to_iter_list(graph, new_node);
455 err = advance_branch(graph, commit_id, commit, repo);
456 if (err)
457 break;
459 done:
460 for (i = 0; i < arg.ntips; i++)
461 got_object_commit_close(arg.tips[i].commit);
462 return err;
465 void
466 got_commit_graph_close(struct got_commit_graph *graph)
468 if (graph->open_branches)
469 got_object_idset_free(graph->open_branches);
470 if (graph->node_ids)
471 got_object_idset_free(graph->node_ids);
472 free(graph->tips);
473 free(graph->path);
474 free(graph);
477 const struct got_error *
478 got_commit_graph_iter_start(struct got_commit_graph *graph,
479 struct got_object_id *id, struct got_repository *repo,
480 got_cancel_cb cancel_cb, void *cancel_arg)
482 const struct got_error *err = NULL;
484 if (!TAILQ_EMPTY(&graph->iter_list))
485 return got_error(GOT_ERR_ITER_BUSY);
487 err = got_object_idset_add(graph->open_branches, id, NULL);
488 if (err)
489 return err;
491 /* Locate first commit which changed graph->path. */
492 while (graph->iter_node == NULL &&
493 got_object_idset_num_elements(graph->open_branches) > 0) {
494 err = fetch_commits_from_open_branches(graph, repo,
495 cancel_cb, cancel_arg);
496 if (err)
497 return err;
500 return NULL;
503 const struct got_error *
504 got_commit_graph_iter_next(struct got_object_id **id,
505 struct got_commit_graph *graph, struct got_repository *repo,
506 got_cancel_cb cancel_cb, void *cancel_arg)
508 const struct got_error *err = NULL;
510 *id = NULL;
512 if (graph->iter_node == NULL) {
513 /* We are done iterating, or iteration was not started. */
514 return got_error(GOT_ERR_ITER_COMPLETED);
517 if (graph->iter_node ==
518 TAILQ_LAST(&graph->iter_list, got_commit_graph_iter_list) &&
519 got_object_idset_num_elements(graph->open_branches) == 0) {
520 /* We are done iterating. */
521 *id = &graph->iter_node->id;
522 graph->iter_node = NULL;
523 return NULL;
526 while (TAILQ_NEXT(graph->iter_node, entry) == NULL &&
527 got_object_idset_num_elements(graph->open_branches) > 0) {
528 err = fetch_commits_from_open_branches(graph, repo,
529 cancel_cb, cancel_arg);
530 if (err)
531 return err;
534 *id = &graph->iter_node->id;
535 graph->iter_node = TAILQ_NEXT(graph->iter_node, entry);
536 return NULL;
539 const struct got_error *
540 got_commit_graph_find_youngest_common_ancestor(struct got_object_id **yca_id,
541 struct got_object_id *commit_id, struct got_object_id *commit_id2,
542 struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
544 const struct got_error *err = NULL;
545 struct got_commit_graph *graph = NULL, *graph2 = NULL;
546 int completed = 0, completed2 = 0;
547 struct got_object_idset *commit_ids;
549 *yca_id = NULL;
551 commit_ids = got_object_idset_alloc();
552 if (commit_ids == NULL)
553 return got_error_from_errno("got_object_idset_alloc");
555 err = got_commit_graph_open(&graph, "/", 1);
556 if (err)
557 goto done;
559 err = got_commit_graph_open(&graph2, "/", 1);
560 if (err)
561 goto done;
563 err = got_commit_graph_iter_start(graph, commit_id, repo,
564 cancel_cb, cancel_arg);
565 if (err)
566 goto done;
568 err = got_commit_graph_iter_start(graph2, commit_id2, repo,
569 cancel_cb, cancel_arg);
570 if (err)
571 goto done;
573 for (;;) {
574 struct got_object_id *id = NULL, *id2 = NULL;
576 if (cancel_cb) {
577 err = (*cancel_cb)(cancel_arg);
578 if (err)
579 break;
582 if (!completed) {
583 err = got_commit_graph_iter_next(&id, graph, repo,
584 cancel_cb, cancel_arg);
585 if (err) {
586 if (err->code != GOT_ERR_ITER_COMPLETED)
587 break;
588 err = NULL;
589 completed = 1;
593 if (!completed2) {
594 err = got_commit_graph_iter_next(&id2, graph2, repo,
595 cancel_cb, cancel_arg);
596 if (err) {
597 if (err->code != GOT_ERR_ITER_COMPLETED)
598 break;
599 err = NULL;
600 completed2 = 1;
604 if (id) {
605 if (got_object_idset_contains(commit_ids, id)) {
606 *yca_id = got_object_id_dup(id);
607 if (*yca_id)
608 break;
609 err = got_error_from_errno("got_object_id_dup");
610 break;
613 err = got_object_idset_add(commit_ids, id, NULL);
614 if (err)
615 break;
617 if (id2) {
618 if (got_object_idset_contains(commit_ids, id2)) {
619 *yca_id = got_object_id_dup(id2);
620 if (*yca_id)
621 break;
622 err = got_error_from_errno("got_object_id_dup");
623 break;
626 err = got_object_idset_add(commit_ids, id2, NULL);
627 if (err)
628 break;
631 if (completed && completed2) {
632 err = got_error(GOT_ERR_ANCESTRY);
633 break;
637 done:
638 got_object_idset_free(commit_ids);
639 if (graph)
640 got_commit_graph_close(graph);
641 if (graph2)
642 got_commit_graph_close(graph2);
643 return err;