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 /*
89 * Nodes which will be passed to the API user next, sorted by
90 * commit timestmap.
91 */
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 n = TAILQ_FIRST(&graph->iter_list);
155 while (n) {
156 next = TAILQ_NEXT(n, entry);
157 if (next && node->timestamp >= next->timestamp) {
158 TAILQ_INSERT_BEFORE(next, node, entry);
159 return;
161 n = next;
163 TAILQ_INSERT_TAIL(&graph->iter_list, node, entry);
166 static const struct got_error *
167 close_branch(struct got_commit_graph *graph, struct got_object_id *commit_id)
169 const struct got_error *err;
171 err = got_object_idset_remove(NULL, graph->open_branches, commit_id);
172 if (err && err->code != GOT_ERR_NO_OBJ)
173 return err;
174 return NULL;
177 static const struct got_error *
178 advance_branch(struct got_commit_graph *graph, struct got_object_id *commit_id,
179 struct got_commit_object *commit, struct got_repository *repo)
181 const struct got_error *err;
182 struct got_object_qid *qid;
184 err = close_branch(graph, commit_id);
185 if (err)
186 return err;
188 if (graph->flags & GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL) {
189 qid = SIMPLEQ_FIRST(&commit->parent_ids);
190 if (qid == NULL ||
191 got_object_idset_contains(graph->open_branches, qid->id))
192 return NULL;
193 return got_object_idset_add(graph->open_branches,
194 qid->id, NULL);
197 /*
198 * If we are graphing commits for a specific path, skip branches
199 * which do not contribute any content to this path.
200 */
201 if (commit->nparents > 1 && !got_path_is_root_dir(graph->path)) {
202 struct got_object_id *merged_id, *prev_id = NULL;
203 int branches_differ = 0;
205 err = got_object_id_by_path(&merged_id, repo, commit_id,
206 graph->path);
207 if (err)
208 return err;
210 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
211 struct got_object_id *id;
213 if (got_object_idset_contains(graph->open_branches,
214 qid->id))
215 continue;
217 err = got_object_id_by_path(&id, repo, qid->id,
218 graph->path);
219 if (err) {
220 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
221 branches_differ = 1;
222 continue;
224 free(merged_id);
225 free(prev_id);
226 return err;
229 if (prev_id) {
230 if (!branches_differ &&
231 got_object_id_cmp(id, prev_id) != 0)
232 branches_differ = 1;
233 free(prev_id);
235 prev_id = id;
237 /*
238 * If a branch has created the merged content we can
239 * skip any other branches.
240 */
241 if (got_object_id_cmp(merged_id, id) == 0) {
242 err = got_object_idset_add(graph->open_branches,
243 qid->id, NULL);
244 free(merged_id);
245 free(id);
246 return err;
250 free(prev_id);
251 prev_id = NULL;
252 free(merged_id);
253 merged_id = NULL;
255 /*
256 * If the path's content is the same on all branches,
257 * follow the first parent only.
258 */
259 if (!branches_differ) {
260 qid = SIMPLEQ_FIRST(&commit->parent_ids);
261 if (qid == NULL)
262 return NULL;
263 if (got_object_idset_contains(graph->open_branches,
264 qid->id))
265 return NULL;
266 if (got_object_idset_contains(graph->node_ids,
267 qid->id))
268 return NULL; /* parent already traversed */
269 return got_object_idset_add(graph->open_branches,
270 qid->id, NULL);
274 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
275 if (got_object_idset_contains(graph->open_branches, qid->id))
276 continue;
277 if (got_object_idset_contains(graph->node_ids, qid->id))
278 continue; /* parent already traversed */
279 err = got_object_idset_add(graph->open_branches, qid->id, NULL);
280 if (err)
281 return err;
284 return NULL;
287 static const struct got_error *
288 add_node(struct got_commit_graph_node **new_node,
289 struct got_commit_graph *graph,
290 struct got_object_id *commit_id,
291 struct got_commit_object *commit,
292 struct got_repository *repo)
294 const struct got_error *err = NULL;
295 struct got_commit_graph_node *node;
297 *new_node = NULL;
299 node = calloc(1, sizeof(*node));
300 if (node == NULL)
301 return got_error_from_errno("calloc");
303 memcpy(&node->id, commit_id, sizeof(node->id));
304 node->timestamp = commit->committer_time;
306 err = got_object_idset_add(graph->node_ids, &node->id, NULL);
307 if (err)
308 free(node);
309 else
310 *new_node = node;
311 return err;
314 const struct got_error *
315 got_commit_graph_open(struct got_commit_graph **graph,
316 const char *path, int first_parent_traversal)
318 const struct got_error *err = NULL;
320 *graph = calloc(1, sizeof(**graph));
321 if (*graph == NULL)
322 return got_error_from_errno("calloc");
324 TAILQ_INIT(&(*graph)->iter_list);
326 (*graph)->path = strdup(path);
327 if ((*graph)->path == NULL) {
328 err = got_error_from_errno("strdup");
329 goto done;
332 (*graph)->node_ids = got_object_idset_alloc();
333 if ((*graph)->node_ids == NULL) {
334 err = got_error_from_errno("got_object_idset_alloc");
335 goto done;
338 (*graph)->open_branches = got_object_idset_alloc();
339 if ((*graph)->open_branches == NULL) {
340 err = got_error_from_errno("got_object_idset_alloc");
341 goto done;
344 if (first_parent_traversal)
345 (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
346 done:
347 if (err) {
348 got_commit_graph_close(*graph);
349 *graph = NULL;
351 return err;
354 struct add_branch_tip_arg {
355 struct got_commit_graph_branch_tip *tips;
356 int ntips;
357 struct got_repository *repo;
358 struct got_commit_graph *graph;
359 };
361 static const struct got_error *
362 add_branch_tip(struct got_object_id *commit_id, void *data, void *arg)
364 const struct got_error *err;
365 struct add_branch_tip_arg *a = arg;
366 struct got_commit_graph_node *new_node;
367 struct got_commit_object *commit;
369 err = got_object_open_as_commit(&commit, a->repo, commit_id);
370 if (err)
371 return err;
373 err = add_node(&new_node, a->graph, commit_id, commit, a->repo);
374 if (err)
375 return err;
377 a->tips[a->ntips].commit_id = new_node ? &new_node->id : NULL;
378 a->tips[a->ntips].commit = commit;
379 a->tips[a->ntips].new_node = new_node;
380 a->ntips++;
382 return NULL;
385 static const struct got_error *
386 fetch_commits_from_open_branches(struct got_commit_graph *graph,
387 struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
389 const struct got_error *err;
390 struct add_branch_tip_arg arg;
391 int i, ntips;
393 ntips = got_object_idset_num_elements(graph->open_branches);
394 if (ntips == 0)
395 return NULL;
397 /* (Re-)allocate branch tips array if necessary. */
398 if (graph->ntips < ntips) {
399 struct got_commit_graph_branch_tip *tips;
400 tips = recallocarray(graph->tips, graph->ntips, ntips,
401 sizeof(*tips));
402 if (tips == NULL)
403 return got_error_from_errno("recallocarray");
404 graph->tips = tips;
405 graph->ntips = ntips;
407 arg.tips = graph->tips;
408 arg.ntips = 0; /* add_branch_tip() will increment */
409 arg.repo = repo;
410 arg.graph = graph;
411 err = got_object_idset_for_each(graph->open_branches, add_branch_tip,
412 &arg);
413 if (err)
414 goto done;
416 for (i = 0; i < arg.ntips; i++) {
417 struct got_object_id *commit_id;
418 struct got_commit_object *commit;
419 struct got_commit_graph_node *new_node;
420 int changed;
422 if (cancel_cb) {
423 err = (*cancel_cb)(cancel_arg);
424 if (err)
425 break;
428 commit_id = arg.tips[i].commit_id;
429 commit = arg.tips[i].commit;
430 new_node = arg.tips[i].new_node;
432 err = detect_changed_path(&changed, commit, commit_id,
433 graph->path, repo);
434 if (err) {
435 if (err->code != GOT_ERR_NO_OBJ)
436 break;
437 /*
438 * History of the path stops here on the current
439 * branch. Keep going on other branches.
440 */
441 err = close_branch(graph, commit_id);
442 if (err)
443 break;
444 continue;
446 if (changed)
447 add_node_to_iter_list(graph, new_node);
448 err = advance_branch(graph, commit_id, commit, repo);
449 if (err)
450 break;
452 done:
453 for (i = 0; i < arg.ntips; i++)
454 got_object_commit_close(arg.tips[i].commit);
455 return err;
458 void
459 got_commit_graph_close(struct got_commit_graph *graph)
461 if (graph->open_branches)
462 got_object_idset_free(graph->open_branches);
463 if (graph->node_ids)
464 got_object_idset_free(graph->node_ids);
465 free(graph->tips);
466 free(graph->path);
467 free(graph);
470 const struct got_error *
471 got_commit_graph_iter_start(struct got_commit_graph *graph,
472 struct got_object_id *id, struct got_repository *repo,
473 got_cancel_cb cancel_cb, void *cancel_arg)
475 const struct got_error *err = NULL;
477 if (!TAILQ_EMPTY(&graph->iter_list))
478 return got_error(GOT_ERR_ITER_BUSY);
480 err = got_object_idset_add(graph->open_branches, id, NULL);
481 if (err)
482 return err;
484 /* Locate first commit which changed graph->path. */
485 while (TAILQ_EMPTY(&graph->iter_list) &&
486 got_object_idset_num_elements(graph->open_branches) > 0) {
487 err = fetch_commits_from_open_branches(graph, repo,
488 cancel_cb, cancel_arg);
489 if (err)
490 return err;
493 if (TAILQ_EMPTY(&graph->iter_list)) {
494 const char *path;
495 if (got_path_is_root_dir(graph->path))
496 return got_error_no_obj(id);
497 path = graph->path;
498 while (path[0] == '/')
499 path++;
500 return got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
503 return NULL;
506 const struct got_error *
507 got_commit_graph_iter_next(struct got_object_id **id,
508 struct got_commit_graph *graph, struct got_repository *repo,
509 got_cancel_cb cancel_cb, void *cancel_arg)
511 const struct got_error *err = NULL;
512 struct got_commit_graph_node *node;
514 *id = NULL;
516 node = TAILQ_FIRST(&graph->iter_list);
517 if (node == NULL) {
518 /* We are done iterating, or iteration was not started. */
519 return got_error(GOT_ERR_ITER_COMPLETED);
522 while (TAILQ_NEXT(node, entry) == NULL &&
523 got_object_idset_num_elements(graph->open_branches) > 0) {
524 err = fetch_commits_from_open_branches(graph, repo,
525 cancel_cb, cancel_arg);
526 if (err)
527 return err;
530 *id = &node->id;
531 TAILQ_REMOVE(&graph->iter_list, node, entry);
532 return NULL;
535 const struct got_error *
536 got_commit_graph_find_youngest_common_ancestor(struct got_object_id **yca_id,
537 struct got_object_id *commit_id, struct got_object_id *commit_id2,
538 struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
540 const struct got_error *err = NULL;
541 struct got_commit_graph *graph = NULL, *graph2 = NULL;
542 int completed = 0, completed2 = 0;
543 struct got_object_idset *commit_ids;
545 *yca_id = NULL;
547 commit_ids = got_object_idset_alloc();
548 if (commit_ids == NULL)
549 return got_error_from_errno("got_object_idset_alloc");
551 err = got_commit_graph_open(&graph, "/", 1);
552 if (err)
553 goto done;
555 err = got_commit_graph_open(&graph2, "/", 1);
556 if (err)
557 goto done;
559 err = got_commit_graph_iter_start(graph, commit_id, repo,
560 cancel_cb, cancel_arg);
561 if (err)
562 goto done;
564 err = got_commit_graph_iter_start(graph2, commit_id2, repo,
565 cancel_cb, cancel_arg);
566 if (err)
567 goto done;
569 for (;;) {
570 struct got_object_id *id = NULL, *id2 = NULL;
572 if (cancel_cb) {
573 err = (*cancel_cb)(cancel_arg);
574 if (err)
575 break;
578 if (!completed) {
579 err = got_commit_graph_iter_next(&id, graph, repo,
580 cancel_cb, cancel_arg);
581 if (err) {
582 if (err->code != GOT_ERR_ITER_COMPLETED)
583 break;
584 err = NULL;
585 completed = 1;
589 if (!completed2) {
590 err = got_commit_graph_iter_next(&id2, graph2, repo,
591 cancel_cb, cancel_arg);
592 if (err) {
593 if (err->code != GOT_ERR_ITER_COMPLETED)
594 break;
595 err = NULL;
596 completed2 = 1;
600 if (id) {
601 if (got_object_idset_contains(commit_ids, id)) {
602 *yca_id = got_object_id_dup(id);
603 if (*yca_id)
604 break;
605 err = got_error_from_errno("got_object_id_dup");
606 break;
609 err = got_object_idset_add(commit_ids, id, NULL);
610 if (err)
611 break;
613 if (id2) {
614 if (got_object_idset_contains(commit_ids, id2)) {
615 *yca_id = got_object_id_dup(id2);
616 if (*yca_id)
617 break;
618 err = got_error_from_errno("got_object_id_dup");
619 break;
622 err = got_object_idset_add(commit_ids, id2, NULL);
623 if (err)
624 break;
627 if (completed && completed2) {
628 err = got_error(GOT_ERR_ANCESTRY);
629 break;
633 done:
634 got_object_idset_free(commit_ids);
635 if (graph)
636 got_commit_graph_close(graph);
637 if (graph2)
638 got_commit_graph_close(graph2);
639 return err;