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 advance_branch(struct got_commit_graph *graph, struct got_object_id *commit_id,
168 struct got_commit_object *commit, struct got_repository *repo)
170 const struct got_error *err;
171 struct got_object_qid *qid;
173 if (graph->flags & GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL) {
174 qid = SIMPLEQ_FIRST(&commit->parent_ids);
175 if (qid == NULL ||
176 got_object_idset_contains(graph->open_branches, qid->id))
177 return NULL;
178 return got_object_idset_add(graph->open_branches,
179 qid->id, NULL);
182 /*
183 * If we are graphing commits for a specific path, skip branches
184 * which do not contribute any content to this path.
185 */
186 if (commit->nparents > 1 && !got_path_is_root_dir(graph->path)) {
187 struct got_object_id *merged_id, *prev_id = NULL;
188 int branches_differ = 0;
190 err = got_object_id_by_path(&merged_id, repo, commit_id,
191 graph->path);
192 if (err)
193 return err;
195 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
196 struct got_object_id *id;
198 if (got_object_idset_contains(graph->open_branches,
199 qid->id))
200 continue;
202 err = got_object_id_by_path(&id, repo, qid->id,
203 graph->path);
204 if (err) {
205 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
206 branches_differ = 1;
207 continue;
209 free(merged_id);
210 free(prev_id);
211 return err;
214 if (prev_id) {
215 if (!branches_differ &&
216 got_object_id_cmp(id, prev_id) != 0)
217 branches_differ = 1;
218 free(prev_id);
220 prev_id = id;
222 /*
223 * If a branch has created the merged content we can
224 * skip any other branches.
225 */
226 if (got_object_id_cmp(merged_id, id) == 0) {
227 err = got_object_idset_add(graph->open_branches,
228 qid->id, NULL);
229 free(merged_id);
230 free(id);
231 return err;
235 free(prev_id);
236 prev_id = NULL;
237 free(merged_id);
238 merged_id = NULL;
240 /*
241 * If the path's content is the same on all branches,
242 * follow the first parent only.
243 */
244 if (!branches_differ) {
245 qid = SIMPLEQ_FIRST(&commit->parent_ids);
246 if (qid == NULL)
247 return NULL;
248 if (got_object_idset_contains(graph->open_branches,
249 qid->id))
250 return NULL;
251 if (got_object_idset_contains(graph->node_ids,
252 qid->id))
253 return NULL; /* parent already traversed */
254 return got_object_idset_add(graph->open_branches,
255 qid->id, NULL);
259 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
260 if (got_object_idset_contains(graph->open_branches, qid->id))
261 continue;
262 if (got_object_idset_contains(graph->node_ids, qid->id))
263 continue; /* parent already traversed */
264 err = got_object_idset_add(graph->open_branches, qid->id, NULL);
265 if (err)
266 return err;
269 return NULL;
272 static const struct got_error *
273 add_node(struct got_commit_graph_node **new_node,
274 struct got_commit_graph *graph,
275 struct got_object_id *commit_id,
276 struct got_commit_object *commit,
277 struct got_repository *repo)
279 const struct got_error *err = NULL;
280 struct got_commit_graph_node *node;
282 *new_node = NULL;
284 node = calloc(1, sizeof(*node));
285 if (node == NULL)
286 return got_error_from_errno("calloc");
288 memcpy(&node->id, commit_id, sizeof(node->id));
289 node->timestamp = commit->committer_time;
291 err = got_object_idset_add(graph->node_ids, &node->id, NULL);
292 if (err)
293 free(node);
294 else
295 *new_node = node;
296 return err;
299 const struct got_error *
300 got_commit_graph_open(struct got_commit_graph **graph,
301 const char *path, int first_parent_traversal)
303 const struct got_error *err = NULL;
305 *graph = calloc(1, sizeof(**graph));
306 if (*graph == NULL)
307 return got_error_from_errno("calloc");
309 TAILQ_INIT(&(*graph)->iter_list);
311 (*graph)->path = strdup(path);
312 if ((*graph)->path == NULL) {
313 err = got_error_from_errno("strdup");
314 goto done;
317 (*graph)->node_ids = got_object_idset_alloc();
318 if ((*graph)->node_ids == NULL) {
319 err = got_error_from_errno("got_object_idset_alloc");
320 goto done;
323 (*graph)->open_branches = got_object_idset_alloc();
324 if ((*graph)->open_branches == NULL) {
325 err = got_error_from_errno("got_object_idset_alloc");
326 goto done;
329 if (first_parent_traversal)
330 (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
331 done:
332 if (err) {
333 got_commit_graph_close(*graph);
334 *graph = NULL;
336 return err;
339 struct add_branch_tip_arg {
340 struct got_commit_graph_branch_tip *tips;
341 int ntips;
342 struct got_repository *repo;
343 struct got_commit_graph *graph;
344 };
346 static const struct got_error *
347 add_branch_tip(struct got_object_id *commit_id, void *data, void *arg)
349 const struct got_error *err;
350 struct add_branch_tip_arg *a = arg;
351 struct got_commit_graph_node *new_node;
352 struct got_commit_object *commit;
354 err = got_object_open_as_commit(&commit, a->repo, commit_id);
355 if (err)
356 return err;
358 err = add_node(&new_node, a->graph, commit_id, commit, a->repo);
359 if (err)
360 return err;
362 a->tips[a->ntips].commit_id = new_node ? &new_node->id : NULL;
363 a->tips[a->ntips].commit = commit;
364 a->tips[a->ntips].new_node = new_node;
365 a->ntips++;
367 return NULL;
370 static const struct got_error *
371 fetch_commits_from_open_branches(struct got_commit_graph *graph,
372 struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
374 const struct got_error *err;
375 struct add_branch_tip_arg arg;
376 int i, ntips;
378 ntips = got_object_idset_num_elements(graph->open_branches);
379 if (ntips == 0)
380 return NULL;
382 /* (Re-)allocate branch tips array if necessary. */
383 if (graph->ntips < ntips) {
384 struct got_commit_graph_branch_tip *tips;
385 tips = recallocarray(graph->tips, graph->ntips, ntips,
386 sizeof(*tips));
387 if (tips == NULL)
388 return got_error_from_errno("recallocarray");
389 graph->tips = tips;
390 graph->ntips = ntips;
392 arg.tips = graph->tips;
393 arg.ntips = 0; /* add_branch_tip() will increment */
394 arg.repo = repo;
395 arg.graph = graph;
396 err = got_object_idset_for_each(graph->open_branches, add_branch_tip,
397 &arg);
398 if (err)
399 goto done;
401 for (i = 0; i < arg.ntips; i++) {
402 struct got_object_id *commit_id;
403 struct got_commit_object *commit;
404 struct got_commit_graph_node *new_node;
405 int changed;
407 if (cancel_cb) {
408 err = (*cancel_cb)(cancel_arg);
409 if (err)
410 break;
413 commit_id = arg.tips[i].commit_id;
414 commit = arg.tips[i].commit;
415 new_node = arg.tips[i].new_node;
417 err = got_object_idset_remove(NULL, graph->open_branches,
418 commit_id);
419 if (err && err->code != GOT_ERR_NO_OBJ)
420 break;
422 err = detect_changed_path(&changed, commit, commit_id,
423 graph->path, repo);
424 if (err) {
425 if (err->code != GOT_ERR_NO_OBJ)
426 break;
427 /*
428 * History of the path stops here on the current
429 * branch. Keep going on other branches.
430 */
431 continue;
433 if (changed)
434 add_node_to_iter_list(graph, new_node);
435 err = advance_branch(graph, commit_id, commit, repo);
436 if (err)
437 break;
439 done:
440 for (i = 0; i < arg.ntips; i++)
441 got_object_commit_close(arg.tips[i].commit);
442 return err;
445 void
446 got_commit_graph_close(struct got_commit_graph *graph)
448 if (graph->open_branches)
449 got_object_idset_free(graph->open_branches);
450 if (graph->node_ids)
451 got_object_idset_free(graph->node_ids);
452 free(graph->tips);
453 free(graph->path);
454 free(graph);
457 const struct got_error *
458 got_commit_graph_iter_start(struct got_commit_graph *graph,
459 struct got_object_id *id, struct got_repository *repo,
460 got_cancel_cb cancel_cb, void *cancel_arg)
462 const struct got_error *err = NULL;
464 if (!TAILQ_EMPTY(&graph->iter_list))
465 return got_error(GOT_ERR_ITER_BUSY);
467 err = got_object_idset_add(graph->open_branches, id, NULL);
468 if (err)
469 return err;
471 /* Locate first commit which changed graph->path. */
472 while (TAILQ_EMPTY(&graph->iter_list) &&
473 got_object_idset_num_elements(graph->open_branches) > 0) {
474 err = fetch_commits_from_open_branches(graph, repo,
475 cancel_cb, cancel_arg);
476 if (err)
477 return err;
480 if (TAILQ_EMPTY(&graph->iter_list)) {
481 const char *path;
482 if (got_path_is_root_dir(graph->path))
483 return got_error_no_obj(id);
484 path = graph->path;
485 while (path[0] == '/')
486 path++;
487 return got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
490 return NULL;
493 const struct got_error *
494 got_commit_graph_iter_next(struct got_object_id **id,
495 struct got_commit_graph *graph, struct got_repository *repo,
496 got_cancel_cb cancel_cb, void *cancel_arg)
498 const struct got_error *err = NULL;
499 struct got_commit_graph_node *node;
501 *id = NULL;
503 node = TAILQ_FIRST(&graph->iter_list);
504 if (node == NULL) {
505 /* We are done iterating, or iteration was not started. */
506 return got_error(GOT_ERR_ITER_COMPLETED);
509 while (TAILQ_NEXT(node, entry) == NULL &&
510 got_object_idset_num_elements(graph->open_branches) > 0) {
511 err = fetch_commits_from_open_branches(graph, repo,
512 cancel_cb, cancel_arg);
513 if (err)
514 return err;
517 *id = &node->id;
518 TAILQ_REMOVE(&graph->iter_list, node, entry);
519 return NULL;
522 const struct got_error *
523 got_commit_graph_find_youngest_common_ancestor(struct got_object_id **yca_id,
524 struct got_object_id *commit_id, struct got_object_id *commit_id2,
525 struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
527 const struct got_error *err = NULL;
528 struct got_commit_graph *graph = NULL, *graph2 = NULL;
529 int completed = 0, completed2 = 0;
530 struct got_object_idset *commit_ids;
532 *yca_id = NULL;
534 commit_ids = got_object_idset_alloc();
535 if (commit_ids == NULL)
536 return got_error_from_errno("got_object_idset_alloc");
538 err = got_commit_graph_open(&graph, "/", 1);
539 if (err)
540 goto done;
542 err = got_commit_graph_open(&graph2, "/", 1);
543 if (err)
544 goto done;
546 err = got_commit_graph_iter_start(graph, commit_id, repo,
547 cancel_cb, cancel_arg);
548 if (err)
549 goto done;
551 err = got_commit_graph_iter_start(graph2, commit_id2, repo,
552 cancel_cb, cancel_arg);
553 if (err)
554 goto done;
556 for (;;) {
557 struct got_object_id *id = NULL, *id2 = NULL;
559 if (cancel_cb) {
560 err = (*cancel_cb)(cancel_arg);
561 if (err)
562 break;
565 if (!completed) {
566 err = got_commit_graph_iter_next(&id, graph, repo,
567 cancel_cb, cancel_arg);
568 if (err) {
569 if (err->code != GOT_ERR_ITER_COMPLETED)
570 break;
571 err = NULL;
572 completed = 1;
576 if (!completed2) {
577 err = got_commit_graph_iter_next(&id2, graph2, repo,
578 cancel_cb, cancel_arg);
579 if (err) {
580 if (err->code != GOT_ERR_ITER_COMPLETED)
581 break;
582 err = NULL;
583 completed2 = 1;
587 if (id) {
588 if (got_object_idset_contains(commit_ids, id)) {
589 *yca_id = got_object_id_dup(id);
590 if (*yca_id)
591 break;
592 err = got_error_from_errno("got_object_id_dup");
593 break;
596 err = got_object_idset_add(commit_ids, id, NULL);
597 if (err)
598 break;
600 if (id2) {
601 if (got_object_idset_contains(commit_ids, id2)) {
602 *yca_id = got_object_id_dup(id2);
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, id2, NULL);
610 if (err)
611 break;
614 if (completed && completed2) {
615 err = got_error(GOT_ERR_ANCESTRY);
616 break;
620 done:
621 got_object_idset_free(commit_ids);
622 if (graph)
623 got_commit_graph_close(graph);
624 if (graph2)
625 got_commit_graph_close(graph2);
626 return err;