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 <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sha1.h>
26 #include <zlib.h>
27 #include <ctype.h>
29 #include "got_error.h"
30 #include "got_object.h"
31 #include "got_commit_graph.h"
33 #include "got_lib_delta.h"
34 #include "got_lib_inflate.h"
35 #include "got_lib_object.h"
36 #include "got_lib_object_idset.h"
37 #include "got_lib_path.h"
39 struct got_commit_graph_node {
40 struct got_object_id id;
41 time_t timestamp;
43 /* Used during graph iteration. */
44 TAILQ_ENTRY(got_commit_graph_node) entry;
45 };
47 TAILQ_HEAD(got_commit_graph_iter_list, got_commit_graph_node);
49 struct got_commit_graph_branch_tip {
50 struct got_object_id *commit_id;
51 struct got_commit_object *commit;
52 struct got_commit_graph_node *new_node;
53 int changed;
54 int branch_done;
55 };
57 struct got_commit_graph {
58 /* The set of all commits we have traversed. */
59 struct got_object_idset *node_ids;
61 /* The commit at which traversal began (youngest commit in node_ids). */
62 struct got_commit_graph_node *head_node;
64 int flags;
65 #define GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL 0x01
67 /*
68 * A set of object IDs of known parent commits which we have not yet
69 * traversed. Each commit ID in this set represents a branch in commit
70 * history: Either the first-parent branch of the head node, or another
71 * branch corresponding to a traversed merge commit for which we have
72 * not traversed a branch point commit yet.
73 *
74 * Whenever we add a commit with a matching ID to the graph, we remove
75 * its corresponding element from this set, and add new elements for
76 * each of that commit's parent commits which were not traversed yet.
77 *
78 * When API users ask us to fetch more commits, we fetch commits from
79 * all currently open branches. This allows API users to process
80 * commits in linear order even though the history contains branches.
81 */
82 struct got_object_idset *open_branches;
84 /* Array of branch tips for fetch_commits_from_open_branches(). */
85 struct got_commit_graph_branch_tip *tips;
86 int ntips;
88 /* Path of tree entry of interest to the API user. */
89 char *path;
91 /* The next commit to return when the API user asks for one. */
92 struct got_commit_graph_node *iter_node;
94 /* The graph iteration list contains all nodes in sorted order. */
95 struct got_commit_graph_iter_list iter_list;
96 };
98 static struct got_commit_graph *
99 alloc_graph(const char *path)
101 struct got_commit_graph *graph;
103 graph = calloc(1, sizeof(*graph));
104 if (graph == NULL)
105 return NULL;
107 graph->path = strdup(path);
108 if (graph->path == NULL) {
109 free(graph);
110 return NULL;
113 graph->node_ids = got_object_idset_alloc();
114 if (graph->node_ids == NULL) {
115 free(graph->path);
116 free(graph);
117 return NULL;
120 graph->open_branches = got_object_idset_alloc();
121 if (graph->open_branches == NULL) {
122 got_object_idset_free(graph->node_ids);
123 free(graph->path);
124 free(graph);
125 return NULL;
128 TAILQ_INIT(&graph->iter_list);
129 return graph;
132 static const struct got_error *
133 detect_changed_path(int *changed, struct got_commit_object *commit,
134 struct got_object_id *commit_id, const char *path,
135 struct got_repository *repo)
137 const struct got_error *err = NULL;
138 struct got_commit_object *pcommit = NULL;
139 struct got_tree_object *tree = NULL, *ptree = NULL;
140 struct got_object_qid *pid;
142 if (got_path_is_root_dir(path)) {
143 *changed = 1;
144 return NULL;
147 *changed = 0;
149 pid = SIMPLEQ_FIRST(&commit->parent_ids);
150 if (pid == NULL) {
151 struct got_object_id *obj_id;
152 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
153 if (err) {
154 if (err->code == GOT_ERR_NO_TREE_ENTRY)
155 err = NULL;
156 } else
157 *changed = 1; /* The path was created in this commit. */
158 free(obj_id);
159 return err;
162 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
163 if (err)
164 return err;
166 err = got_object_open_as_commit(&pcommit, repo, pid->id);
167 if (err)
168 goto done;
170 err = got_object_open_as_tree(&ptree, repo, pcommit->tree_id);
171 if (err)
172 goto done;
174 err = got_object_tree_path_changed(changed, tree, ptree, path,
175 repo);
176 done:
177 if (tree)
178 got_object_tree_close(tree);
179 if (ptree)
180 got_object_tree_close(ptree);
181 if (pcommit)
182 got_object_commit_close(pcommit);
183 return err;
186 static void
187 add_node_to_iter_list(struct got_commit_graph *graph,
188 struct got_commit_graph_node *node,
189 struct got_commit_graph_node *child_node)
191 struct got_commit_graph_node *n, *next;
193 if (TAILQ_EMPTY(&graph->iter_list)) {
194 TAILQ_INSERT_HEAD(&graph->iter_list, node, entry);
195 graph->iter_node = node;
196 return;
199 n = graph->iter_node;
200 /* Ensure that an iteration in progress will see this new commit. */
201 while (n) {
202 next = TAILQ_NEXT(n, entry);
203 if (next && node->timestamp >= next->timestamp) {
204 TAILQ_INSERT_BEFORE(next, node, entry);
205 return;
207 n = next;
209 TAILQ_INSERT_TAIL(&graph->iter_list, node, entry);
212 static const struct got_error *
213 close_branch(struct got_commit_graph *graph, struct got_object_id *commit_id)
215 const struct got_error *err;
217 err = got_object_idset_remove(NULL, graph->open_branches, commit_id);
218 if (err && err->code != GOT_ERR_NO_OBJ)
219 return err;
220 return NULL;
223 static const struct got_error *
224 advance_branch(struct got_commit_graph *graph,
225 struct got_commit_graph_node *node,
226 struct got_object_id *commit_id, struct got_commit_object *commit,
227 struct got_repository *repo)
229 const struct got_error *err;
230 struct got_object_qid *qid;
232 err = close_branch(graph, commit_id);
233 if (err)
234 return err;
236 if (graph->flags & GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL) {
237 qid = SIMPLEQ_FIRST(&commit->parent_ids);
238 if (qid == NULL ||
239 got_object_idset_get(graph->open_branches, qid->id))
240 return NULL;
241 return got_object_idset_add(graph->open_branches,
242 qid->id, node);
245 /*
246 * If we are graphing commits for a specific path, skip branches
247 * which do not contribute any content to this path.
248 */
249 if (commit->nparents > 1 && !got_path_is_root_dir(graph->path)) {
250 struct got_object_id *merged_id, *prev_id = NULL;
251 int branches_differ = 0;
253 err = got_object_id_by_path(&merged_id, repo, commit_id,
254 graph->path);
255 if (err)
256 return err;
258 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
259 struct got_object_id *id;
261 if (got_object_idset_get(graph->node_ids, qid->id))
262 continue; /* parent already traversed */
263 if (got_object_idset_get(graph->open_branches, qid->id))
264 continue;
266 err = got_object_id_by_path(&id, repo, qid->id,
267 graph->path);
268 if (err) {
269 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
270 branches_differ = 1;
271 continue;
273 free(merged_id);
274 free(prev_id);
275 return err;
278 if (prev_id) {
279 if (!branches_differ &&
280 got_object_id_cmp(id, prev_id) != 0)
281 branches_differ = 1;
282 free(prev_id);
284 prev_id = id;
286 /*
287 * If a branch has created the merged content we can
288 * skip any other branches.
289 */
290 if (got_object_id_cmp(merged_id, id) == 0) {
291 err = got_object_idset_add(graph->open_branches,
292 qid->id, node);
293 free(merged_id);
294 free(id);
295 return err;
299 free(prev_id);
300 prev_id = NULL;
301 free(merged_id);
302 merged_id = NULL;
304 /*
305 * If the path's content is the same on all branches,
306 * follow the first parent only.
307 */
308 if (!branches_differ) {
309 qid = SIMPLEQ_FIRST(&commit->parent_ids);
310 if (qid == NULL)
311 return NULL;
312 if (got_object_idset_get(graph->node_ids, qid->id))
313 return NULL; /* parent already traversed */
314 if (got_object_idset_get(graph->open_branches, qid->id))
315 return NULL;
316 return got_object_idset_add(graph->open_branches,
317 qid->id, node);
321 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
322 if (got_object_idset_get(graph->node_ids, qid->id))
323 continue; /* parent already traversed */
324 if (got_object_idset_get(graph->open_branches, qid->id))
325 continue;
326 err = got_object_idset_add(graph->open_branches, qid->id, node);
327 if (err)
328 return err;
331 return NULL;
334 static const struct got_error *
335 add_node(struct got_commit_graph_node **new_node, int *changed,
336 int *branch_done, struct got_commit_graph *graph,
337 struct got_object_id *commit_id, struct got_commit_object *commit,
338 struct got_commit_graph_node *child_node, struct got_repository *repo)
340 const struct got_error *err = NULL;
341 struct got_commit_graph_node *node;
343 *new_node = NULL;
344 *changed = 0;
345 *branch_done = 0;
347 node = calloc(1, sizeof(*node));
348 if (node == NULL)
349 return got_error_from_errno();
351 memcpy(&node->id, commit_id, sizeof(node->id));
352 node->timestamp = commit->committer_time;
354 err = got_object_idset_add(graph->node_ids, &node->id, node);
355 if (err) {
356 free(node);
357 return err;
360 err = detect_changed_path(changed, commit, commit_id, graph->path,
361 repo);
362 if (err) {
363 if (err->code == GOT_ERR_NO_OBJ) {
364 /*
365 * History of the path stops here on the current
366 * branch. Keep going on other branches.
367 */
368 err = NULL;
369 *branch_done = 1;
370 } else {
371 got_object_idset_remove(NULL, graph->node_ids,
372 &node->id);
373 free(node);
374 return err;
378 if (*changed)
379 add_node_to_iter_list(graph, node, child_node);
380 *new_node = node;
381 return NULL;
384 const struct got_error *
385 got_commit_graph_open(struct got_commit_graph **graph,
386 struct got_object_id *commit_id, const char *path,
387 int first_parent_traversal, struct got_repository *repo)
389 const struct got_error *err = NULL;
390 struct got_commit_object *commit;
391 int changed, branch_done;
393 *graph = NULL;
395 err = got_object_open_as_commit(&commit, repo, commit_id);
396 if (err)
397 return err;
399 /* The path must exist in our initial commit. */
400 if (!got_path_is_root_dir(path)) {
401 struct got_object_id *obj_id;
402 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
403 if (err)
404 return err;
405 free(obj_id);
408 *graph = alloc_graph(path);
409 if (*graph == NULL) {
410 got_object_commit_close(commit);
411 return got_error_from_errno();
414 if (first_parent_traversal)
415 (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
417 err = add_node(&(*graph)->head_node, &changed, &branch_done, *graph,
418 commit_id, commit, NULL, repo);
419 if (err == NULL) {
420 err = advance_branch(*graph, (*graph)->head_node, commit_id,
421 commit, repo);
423 got_object_commit_close(commit);
424 if (err) {
425 got_commit_graph_close(*graph);
426 *graph = NULL;
427 return err;
430 return NULL;
433 struct add_branch_tip_arg {
434 struct got_commit_graph_branch_tip *tips;
435 int ntips;
436 struct got_repository *repo;
437 struct got_commit_graph *graph;
438 };
440 static const struct got_error *
441 add_branch_tip(struct got_object_id *commit_id, void *data, void *arg)
443 const struct got_error *err;
444 struct got_commit_graph_node *child_node = data;
445 struct add_branch_tip_arg *a = arg;
446 struct got_commit_graph_node *new_node;
447 struct got_commit_object *commit;
448 int changed, branch_done;
450 err = got_object_open_as_commit(&commit, a->repo, commit_id);
451 if (err)
452 return err;
454 err = add_node(&new_node, &changed, &branch_done, a->graph,
455 commit_id, commit, child_node, a->repo);
456 if (err)
457 return err;
459 a->tips[a->ntips].commit_id = new_node ? &new_node->id : NULL;
460 a->tips[a->ntips].commit = commit;
461 a->tips[a->ntips].new_node = new_node;
462 a->tips[a->ntips].changed = changed;
463 a->tips[a->ntips].branch_done = branch_done;
464 a->ntips++;
466 return NULL;
469 static const struct got_error *
470 fetch_commits_from_open_branches(int *nfetched,
471 struct got_object_id **changed_id, struct got_commit_graph *graph,
472 struct got_repository *repo)
474 const struct got_error *err;
475 struct add_branch_tip_arg arg;
476 int i, ntips;
478 *nfetched = 0;
479 *changed_id = NULL;
481 ntips = got_object_idset_num_elements(graph->open_branches);
482 if (ntips == 0)
483 return NULL;
485 /* (Re-)allocate branch tips array if necessary. */
486 if (graph->ntips < ntips) {
487 struct got_commit_graph_branch_tip *tips;
488 tips = recallocarray(graph->tips, graph->ntips, ntips,
489 sizeof(*tips));
490 if (tips == NULL)
491 return got_error_from_errno();
492 graph->tips = tips;
493 graph->ntips = ntips;
495 arg.tips = graph->tips;
496 arg.ntips = 0; /* add_branch_tip() will increment */
497 arg.repo = repo;
498 arg.graph = graph;
499 err = got_object_idset_for_each(graph->open_branches, add_branch_tip,
500 &arg);
501 if (err)
502 goto done;
504 for (i = 0; i < arg.ntips; i++) {
505 struct got_object_id *commit_id;
506 struct got_commit_object *commit;
507 struct got_commit_graph_node *new_node;
508 int branch_done, changed;
510 commit_id = arg.tips[i].commit_id;
511 commit = arg.tips[i].commit;
512 new_node = arg.tips[i].new_node;
513 branch_done = arg.tips[i].branch_done;
514 changed = arg.tips[i].changed;
516 if (branch_done)
517 err = close_branch(graph, commit_id);
518 else
519 err = advance_branch(graph, new_node, commit_id,
520 commit, repo);
521 if (err)
522 break;
523 if (changed && *changed_id == NULL)
524 *changed_id = commit_id;
526 done:
527 for (i = 0; i < arg.ntips; i++)
528 got_object_commit_close(arg.tips[i].commit);
529 (*nfetched) = arg.ntips;
530 return err;
533 const struct got_error *
534 got_commit_graph_fetch_commits(struct got_commit_graph *graph, int limit,
535 struct got_repository *repo)
537 const struct got_error *err;
538 int nfetched = 0, ncommits;
539 struct got_object_id *changed_id = NULL;
541 while (nfetched < limit) {
542 err = fetch_commits_from_open_branches(&ncommits,
543 &changed_id, graph, repo);
544 if (err)
545 return err;
546 if (ncommits == 0)
547 break;
548 if (changed_id)
549 nfetched += ncommits;
552 return NULL;
555 static const struct got_error *
556 free_node_iter(struct got_object_id *id, void *data, void *arg)
558 struct got_commit_graph_node *node = data;
559 free(node);
560 return NULL;
563 void
564 got_commit_graph_close(struct got_commit_graph *graph)
566 got_object_idset_free(graph->open_branches);
567 got_object_idset_for_each(graph->node_ids, free_node_iter, NULL);
568 got_object_idset_free(graph->node_ids);
569 free(graph->tips);
570 free(graph->path);
571 free(graph);
574 const struct got_error *
575 got_commit_graph_iter_start(struct got_commit_graph *graph,
576 struct got_object_id *id, struct got_repository *repo)
578 const struct got_error *err = NULL;
579 struct got_commit_graph_node *start_node;
580 struct got_commit_object *commit;
581 int changed;
583 start_node = got_object_idset_get(graph->node_ids, id);
584 if (start_node == NULL)
585 return got_error_no_obj(id);
587 err = got_object_open_as_commit(&commit, repo, &start_node->id);
588 if (err)
589 return err;
591 err = detect_changed_path(&changed, commit, &start_node->id,
592 graph->path, repo);
593 if (err) {
594 got_object_commit_close(commit);
595 return err;
598 if (!changed) {
599 /* Locate first commit which changed graph->path. */
600 struct got_object_id *changed_id = NULL;
601 while (changed_id == NULL) {
602 int ncommits;
603 err = fetch_commits_from_open_branches(&ncommits,
604 &changed_id, graph, repo);
605 if (err) {
606 got_object_commit_close(commit);
607 return err;
610 start_node = got_object_idset_get(graph->node_ids, changed_id);
612 got_object_commit_close(commit);
614 graph->iter_node = start_node;
615 return NULL;
618 const struct got_error *
619 got_commit_graph_iter_next(struct got_object_id **id,
620 struct got_commit_graph *graph)
622 *id = NULL;
624 if (graph->iter_node == NULL) {
625 /* We are done iterating, or iteration was not started. */
626 return got_error(GOT_ERR_ITER_COMPLETED);
629 if (graph->iter_node ==
630 TAILQ_LAST(&graph->iter_list, got_commit_graph_iter_list) &&
631 got_object_idset_num_elements(graph->open_branches) == 0) {
632 /* We are done iterating. */
633 *id = &graph->iter_node->id;
634 graph->iter_node = NULL;
635 return NULL;
638 if (TAILQ_NEXT(graph->iter_node, entry) == NULL)
639 return got_error(GOT_ERR_ITER_NEED_MORE);
641 *id = &graph->iter_node->id;
642 graph->iter_node = TAILQ_NEXT(graph->iter_node, entry);
643 return NULL;