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 /* Ensure that an iteration in progress will see this new commit. */
156 while (n) {
157 next = TAILQ_NEXT(n, entry);
158 if (next && node->timestamp >= next->timestamp) {
159 TAILQ_INSERT_BEFORE(next, node, entry);
160 return;
162 n = next;
164 TAILQ_INSERT_TAIL(&graph->iter_list, node, entry);
167 static const struct got_error *
168 close_branch(struct got_commit_graph *graph, struct got_object_id *commit_id)
170 const struct got_error *err;
172 err = got_object_idset_remove(NULL, graph->open_branches, commit_id);
173 if (err && err->code != GOT_ERR_NO_OBJ)
174 return err;
175 return NULL;
178 static const struct got_error *
179 advance_branch(struct got_commit_graph *graph, struct got_object_id *commit_id,
180 struct got_commit_object *commit, struct got_repository *repo)
182 const struct got_error *err;
183 struct got_object_qid *qid;
185 err = close_branch(graph, commit_id);
186 if (err)
187 return err;
189 if (graph->flags & GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL) {
190 qid = SIMPLEQ_FIRST(&commit->parent_ids);
191 if (qid == NULL ||
192 got_object_idset_contains(graph->open_branches, qid->id))
193 return NULL;
194 return got_object_idset_add(graph->open_branches,
195 qid->id, NULL);
198 /*
199 * If we are graphing commits for a specific path, skip branches
200 * which do not contribute any content to this path.
201 */
202 if (commit->nparents > 1 && !got_path_is_root_dir(graph->path)) {
203 struct got_object_id *merged_id, *prev_id = NULL;
204 int branches_differ = 0;
206 err = got_object_id_by_path(&merged_id, repo, commit_id,
207 graph->path);
208 if (err)
209 return err;
211 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
212 struct got_object_id *id;
214 if (got_object_idset_contains(graph->open_branches,
215 qid->id))
216 continue;
218 err = got_object_id_by_path(&id, repo, qid->id,
219 graph->path);
220 if (err) {
221 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
222 branches_differ = 1;
223 continue;
225 free(merged_id);
226 free(prev_id);
227 return err;
230 if (prev_id) {
231 if (!branches_differ &&
232 got_object_id_cmp(id, prev_id) != 0)
233 branches_differ = 1;
234 free(prev_id);
236 prev_id = id;
238 /*
239 * If a branch has created the merged content we can
240 * skip any other branches.
241 */
242 if (got_object_id_cmp(merged_id, id) == 0) {
243 err = got_object_idset_add(graph->open_branches,
244 qid->id, NULL);
245 free(merged_id);
246 free(id);
247 return err;
251 free(prev_id);
252 prev_id = NULL;
253 free(merged_id);
254 merged_id = NULL;
256 /*
257 * If the path's content is the same on all branches,
258 * follow the first parent only.
259 */
260 if (!branches_differ) {
261 qid = SIMPLEQ_FIRST(&commit->parent_ids);
262 if (qid == NULL)
263 return NULL;
264 if (got_object_idset_contains(graph->open_branches,
265 qid->id))
266 return NULL;
267 if (got_object_idset_contains(graph->node_ids,
268 qid->id))
269 return NULL; /* parent already traversed */
270 return got_object_idset_add(graph->open_branches,
271 qid->id, NULL);
275 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
276 if (got_object_idset_contains(graph->open_branches, qid->id))
277 continue;
278 if (got_object_idset_contains(graph->node_ids, qid->id))
279 continue; /* parent already traversed */
280 err = got_object_idset_add(graph->open_branches, qid->id, NULL);
281 if (err)
282 return err;
285 return NULL;
288 static const struct got_error *
289 add_node(struct got_commit_graph_node **new_node,
290 struct got_commit_graph *graph,
291 struct got_object_id *commit_id,
292 struct got_commit_object *commit,
293 struct got_repository *repo)
295 const struct got_error *err = NULL;
296 struct got_commit_graph_node *node;
298 *new_node = NULL;
300 node = calloc(1, sizeof(*node));
301 if (node == NULL)
302 return got_error_from_errno("calloc");
304 memcpy(&node->id, commit_id, sizeof(node->id));
305 node->timestamp = commit->committer_time;
307 err = got_object_idset_add(graph->node_ids, &node->id, NULL);
308 if (err)
309 free(node);
310 else
311 *new_node = node;
312 return err;
315 const struct got_error *
316 got_commit_graph_open(struct got_commit_graph **graph,
317 const char *path, int first_parent_traversal)
319 const struct got_error *err = NULL;
321 *graph = calloc(1, sizeof(**graph));
322 if (*graph == NULL)
323 return got_error_from_errno("calloc");
325 TAILQ_INIT(&(*graph)->iter_list);
327 (*graph)->path = strdup(path);
328 if ((*graph)->path == NULL) {
329 err = got_error_from_errno("strdup");
330 goto done;
333 (*graph)->node_ids = got_object_idset_alloc();
334 if ((*graph)->node_ids == NULL) {
335 err = got_error_from_errno("got_object_idset_alloc");
336 goto done;
339 (*graph)->open_branches = got_object_idset_alloc();
340 if ((*graph)->open_branches == NULL) {
341 err = got_error_from_errno("got_object_idset_alloc");
342 goto done;
345 if (first_parent_traversal)
346 (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
347 done:
348 if (err) {
349 got_commit_graph_close(*graph);
350 *graph = NULL;
352 return err;
355 struct add_branch_tip_arg {
356 struct got_commit_graph_branch_tip *tips;
357 int ntips;
358 struct got_repository *repo;
359 struct got_commit_graph *graph;
360 };
362 static const struct got_error *
363 add_branch_tip(struct got_object_id *commit_id, void *data, void *arg)
365 const struct got_error *err;
366 struct add_branch_tip_arg *a = arg;
367 struct got_commit_graph_node *new_node;
368 struct got_commit_object *commit;
370 err = got_object_open_as_commit(&commit, a->repo, commit_id);
371 if (err)
372 return err;
374 err = add_node(&new_node, a->graph, commit_id, commit, a->repo);
375 if (err)
376 return err;
378 a->tips[a->ntips].commit_id = new_node ? &new_node->id : NULL;
379 a->tips[a->ntips].commit = commit;
380 a->tips[a->ntips].new_node = new_node;
381 a->ntips++;
383 return NULL;
386 static const struct got_error *
387 fetch_commits_from_open_branches(struct got_commit_graph *graph,
388 struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
390 const struct got_error *err;
391 struct add_branch_tip_arg arg;
392 int i, ntips;
394 ntips = got_object_idset_num_elements(graph->open_branches);
395 if (ntips == 0)
396 return NULL;
398 /* (Re-)allocate branch tips array if necessary. */
399 if (graph->ntips < ntips) {
400 struct got_commit_graph_branch_tip *tips;
401 tips = recallocarray(graph->tips, graph->ntips, ntips,
402 sizeof(*tips));
403 if (tips == NULL)
404 return got_error_from_errno("recallocarray");
405 graph->tips = tips;
406 graph->ntips = ntips;
408 arg.tips = graph->tips;
409 arg.ntips = 0; /* add_branch_tip() will increment */
410 arg.repo = repo;
411 arg.graph = graph;
412 err = got_object_idset_for_each(graph->open_branches, add_branch_tip,
413 &arg);
414 if (err)
415 goto done;
417 for (i = 0; i < arg.ntips; i++) {
418 struct got_object_id *commit_id;
419 struct got_commit_object *commit;
420 struct got_commit_graph_node *new_node;
421 int changed;
423 if (cancel_cb) {
424 err = (*cancel_cb)(cancel_arg);
425 if (err)
426 break;
429 commit_id = arg.tips[i].commit_id;
430 commit = arg.tips[i].commit;
431 new_node = arg.tips[i].new_node;
433 err = detect_changed_path(&changed, commit, commit_id,
434 graph->path, repo);
435 if (err) {
436 if (err->code != GOT_ERR_NO_OBJ)
437 break;
438 /*
439 * History of the path stops here on the current
440 * branch. Keep going on other branches.
441 */
442 err = close_branch(graph, commit_id);
443 if (err)
444 break;
445 continue;
447 if (changed)
448 add_node_to_iter_list(graph, new_node);
449 err = advance_branch(graph, commit_id, commit, repo);
450 if (err)
451 break;
453 done:
454 for (i = 0; i < arg.ntips; i++)
455 got_object_commit_close(arg.tips[i].commit);
456 return err;
459 void
460 got_commit_graph_close(struct got_commit_graph *graph)
462 if (graph->open_branches)
463 got_object_idset_free(graph->open_branches);
464 if (graph->node_ids)
465 got_object_idset_free(graph->node_ids);
466 free(graph->tips);
467 free(graph->path);
468 free(graph);
471 const struct got_error *
472 got_commit_graph_iter_start(struct got_commit_graph *graph,
473 struct got_object_id *id, struct got_repository *repo,
474 got_cancel_cb cancel_cb, void *cancel_arg)
476 const struct got_error *err = NULL;
478 if (!TAILQ_EMPTY(&graph->iter_list))
479 return got_error(GOT_ERR_ITER_BUSY);
481 err = got_object_idset_add(graph->open_branches, id, NULL);
482 if (err)
483 return err;
485 /* Locate first commit which changed graph->path. */
486 while (TAILQ_EMPTY(&graph->iter_list) &&
487 got_object_idset_num_elements(graph->open_branches) > 0) {
488 err = fetch_commits_from_open_branches(graph, repo,
489 cancel_cb, cancel_arg);
490 if (err)
491 return err;
494 if (TAILQ_EMPTY(&graph->iter_list)) {
495 const char *path;
496 if (got_path_is_root_dir(graph->path))
497 return got_error_no_obj(id);
498 path = graph->path;
499 while (path[0] == '/')
500 path++;
501 return got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
504 return NULL;
507 const struct got_error *
508 got_commit_graph_iter_next(struct got_object_id **id,
509 struct got_commit_graph *graph, struct got_repository *repo,
510 got_cancel_cb cancel_cb, void *cancel_arg)
512 const struct got_error *err = NULL;
513 struct got_commit_graph_node *node;
515 *id = NULL;
517 if (TAILQ_EMPTY(&graph->iter_list)) {
518 /* We are done iterating, or iteration was not started. */
519 return got_error(GOT_ERR_ITER_COMPLETED);
522 node = TAILQ_FIRST(&graph->iter_list);
523 while (TAILQ_NEXT(node, entry) == NULL &&
524 got_object_idset_num_elements(graph->open_branches) > 0) {
525 err = fetch_commits_from_open_branches(graph, repo,
526 cancel_cb, cancel_arg);
527 if (err)
528 return err;
531 *id = &node->id;
532 TAILQ_REMOVE(&graph->iter_list, node, entry);
533 return NULL;
536 const struct got_error *
537 got_commit_graph_find_youngest_common_ancestor(struct got_object_id **yca_id,
538 struct got_object_id *commit_id, struct got_object_id *commit_id2,
539 struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
541 const struct got_error *err = NULL;
542 struct got_commit_graph *graph = NULL, *graph2 = NULL;
543 int completed = 0, completed2 = 0;
544 struct got_object_idset *commit_ids;
546 *yca_id = NULL;
548 commit_ids = got_object_idset_alloc();
549 if (commit_ids == NULL)
550 return got_error_from_errno("got_object_idset_alloc");
552 err = got_commit_graph_open(&graph, "/", 1);
553 if (err)
554 goto done;
556 err = got_commit_graph_open(&graph2, "/", 1);
557 if (err)
558 goto done;
560 err = got_commit_graph_iter_start(graph, commit_id, repo,
561 cancel_cb, cancel_arg);
562 if (err)
563 goto done;
565 err = got_commit_graph_iter_start(graph2, commit_id2, repo,
566 cancel_cb, cancel_arg);
567 if (err)
568 goto done;
570 for (;;) {
571 struct got_object_id *id = NULL, *id2 = NULL;
573 if (cancel_cb) {
574 err = (*cancel_cb)(cancel_arg);
575 if (err)
576 break;
579 if (!completed) {
580 err = got_commit_graph_iter_next(&id, graph, repo,
581 cancel_cb, cancel_arg);
582 if (err) {
583 if (err->code != GOT_ERR_ITER_COMPLETED)
584 break;
585 err = NULL;
586 completed = 1;
590 if (!completed2) {
591 err = got_commit_graph_iter_next(&id2, graph2, repo,
592 cancel_cb, cancel_arg);
593 if (err) {
594 if (err->code != GOT_ERR_ITER_COMPLETED)
595 break;
596 err = NULL;
597 completed2 = 1;
601 if (id) {
602 if (got_object_idset_contains(commit_ids, id)) {
603 *yca_id = got_object_id_dup(id);
604 if (*yca_id)
605 break;
606 err = got_error_from_errno("got_object_id_dup");
607 break;
610 err = got_object_idset_add(commit_ids, id, NULL);
611 if (err)
612 break;
614 if (id2) {
615 if (got_object_idset_contains(commit_ids, id2)) {
616 *yca_id = got_object_id_dup(id2);
617 if (*yca_id)
618 break;
619 err = got_error_from_errno("got_object_id_dup");
620 break;
623 err = got_object_idset_add(commit_ids, id2, NULL);
624 if (err)
625 break;
628 if (completed && completed2) {
629 err = got_error(GOT_ERR_ANCESTRY);
630 break;
634 done:
635 got_object_idset_free(commit_ids);
636 if (graph)
637 got_commit_graph_close(graph);
638 if (graph2)
639 got_commit_graph_close(graph2);
640 return err;