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_zbuf.h"
35 #include "got_lib_object.h"
36 #include "got_lib_object_idset.h"
38 struct got_commit_graph_node {
39 struct got_object_id id;
41 /*
42 * Each graph node corresponds to a commit object.
43 * Graph vertices are modelled with two separate adjacency lists:
44 * Adjacencies of a graph node are either parent (older) commits,
45 * and child (younger) commits.
46 */
47 struct got_commit_object *commit; /* contains list of parents */
48 int nchildren;
49 SIMPLEQ_HEAD(, got_parent_id) child_ids;
51 /* Used during graph iteration. */
52 TAILQ_ENTRY(got_commit_graph_node) entry;
53 };
55 struct got_commit_graph {
56 /* The set of all commits we have traversed. */
57 struct got_object_idset *node_ids;
59 /* The commit at which traversal began (youngest commit in node_ids). */
60 struct got_commit_graph_node *head_node;
62 /*
63 * A set of object IDs of known parent commits which we have not yet
64 * traversed. Each commit ID in this set represents a branch in commit
65 * history: Either the first-parent branch of the head node, or another
66 * branch corresponding to a traversed merge commit for which we have
67 * not traversed a branch point commit yet.
68 *
69 * Whenever we add a commit with a matching ID to the graph, we remove
70 * its corresponding element from this set, and add new elements for
71 * each of that commit's parent commits which were not traversed yet.
72 *
73 * When API users ask us to fetch more commits, we fetch commits from
74 * all currently open branches. This allows API users to process
75 * commits in linear order even though the history contains branches.
76 */
77 struct got_object_idset *open_branches;
79 /* The next commit to return when the API user asks for one. */
80 struct got_commit_graph_node *iter_node;
82 TAILQ_HEAD(, got_commit_graph_node) iter_candidates;
83 };
85 static struct got_commit_graph *
86 alloc_graph(void)
87 {
88 struct got_commit_graph *graph;
90 graph = calloc(1, sizeof(*graph));
91 if (graph == NULL)
92 return NULL;
94 graph->node_ids = got_object_idset_alloc();
95 if (graph->node_ids == NULL) {
96 free(graph);
97 return NULL;
98 }
100 graph->open_branches = got_object_idset_alloc();
101 if (graph->open_branches == NULL) {
102 got_object_idset_free(graph->node_ids);
103 free(graph);
104 return NULL;
107 TAILQ_INIT(&graph->iter_candidates);
108 return graph;
111 #if 0
112 static int
113 is_head_node(struct got_commit_graph_node *node)
115 return node->nchildren == 0;
118 static int
119 is_merge_point(struct got_commit_graph_node *node)
121 return node->commit->nparents > 1;
124 int
125 is_branch_point(struct got_commit_graph_node *node)
127 return node->nchildren > 1;
129 #endif
131 static int
132 is_root_node(struct got_commit_graph_node *node)
134 return node->commit->nparents == 0;
137 static const struct got_error *
138 parse_commit_time(int64_t *time, struct got_commit_object *commit)
140 const struct got_error *err = NULL;
141 const char *errstr;
142 char *committer, *space;
144 *time = 0;
146 committer = strdup(commit->committer);
147 if (committer == NULL)
148 return got_error_from_errno();
150 /* Strip off trailing timezone indicator. */
151 space = strrchr(committer, ' ');
152 if (space == NULL) {
153 err = got_error(GOT_ERR_BAD_OBJ_DATA);
154 goto done;
156 *space = '\0';
158 /* Timestamp is separated from committer name + email by space. */
159 space = strrchr(committer, ' ');
160 if (space == NULL) {
161 err = got_error(GOT_ERR_BAD_OBJ_DATA);
162 goto done;
165 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
166 if (errstr)
167 err = got_error(GOT_ERR_BAD_OBJ_DATA);
169 done:
170 free(committer);
171 return err;
174 static const struct got_error *
175 compare_commits(int *cmp, struct got_commit_object *c1,
176 struct got_commit_object *c2)
178 const struct got_error *err;
179 int64_t t1, t2;
181 err = parse_commit_time(&t1, c1);
182 if (err)
183 return err;
184 err = parse_commit_time(&t2, c2);
185 if (err)
186 return err;
188 if (t1 == t2)
189 *cmp = 0;
190 else if (t1 < t2)
191 *cmp = -1;
192 else
193 *cmp = 1;
195 return NULL;
198 static const struct got_error *
199 add_iteration_candidate(struct got_commit_graph *graph,
200 struct got_commit_graph_node *node)
202 struct got_commit_graph_node *n, *next;
204 if (TAILQ_EMPTY(&graph->iter_candidates)) {
205 TAILQ_INSERT_TAIL(&graph->iter_candidates, node, entry);
206 return NULL;
209 TAILQ_FOREACH(n, &graph->iter_candidates, entry) {
210 const struct got_error *err;
211 int cmp;
212 err = compare_commits(&cmp, node->commit, n->commit);
213 if (err)
214 return err;
215 if (cmp < 0) {
216 next = TAILQ_NEXT(n, entry);
217 if (next == NULL) {
218 TAILQ_INSERT_AFTER(&graph->iter_candidates, n,
219 node, entry);
220 break;
222 err = compare_commits(&cmp, node->commit, next->commit);
223 if (err)
224 return err;
225 if (cmp >= 0) {
226 TAILQ_INSERT_BEFORE(next, node, entry);
227 break;
229 } else {
230 TAILQ_INSERT_BEFORE(n, node, entry);
231 break;
235 return NULL;
238 static const struct got_error *
239 add_node(struct got_commit_graph_node **new_node,
240 struct got_commit_graph *graph, struct got_object_id *commit_id,
241 struct got_commit_object *commit, struct got_object_id *child_commit_id)
243 const struct got_error *err = NULL;
244 struct got_commit_graph_node *node, *existing_node;
246 *new_node = NULL;
248 node = calloc(1, sizeof(*node));
249 if (node == NULL)
250 return got_error_from_errno();
252 memcpy(&node->id, commit_id, sizeof(node->id));
253 node->commit = commit;
254 SIMPLEQ_INIT(&node->child_ids);
256 err = got_object_idset_add((void **)(&existing_node),
257 graph->node_ids, &node->id, node);
258 if (err == NULL) {
259 struct got_parent_id *pid;
261 err = add_iteration_candidate(graph, node);
262 if (err)
263 return err;
265 err = got_object_idset_remove(graph->open_branches, commit_id);
266 if (err && err->code != GOT_ERR_NO_OBJ)
267 return err;
268 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
269 if (got_object_idset_get(graph->node_ids, pid->id))
270 continue; /* parent already traversed */
271 err = got_object_idset_add(NULL, graph->open_branches,
272 pid->id, node);
273 if (err && err->code != GOT_ERR_OBJ_EXISTS)
274 return err;
276 *new_node = node;
277 } else if (err->code == GOT_ERR_OBJ_EXISTS) {
278 err = NULL;
279 free(node);
280 node = existing_node;
281 } else {
282 free(node);
283 return err;
286 if (child_commit_id) {
287 struct got_parent_id *child, *cid;
289 /* Prevent linking to self. */
290 if (got_object_id_cmp(commit_id, child_commit_id) == 0)
291 return got_error(GOT_ERR_BAD_OBJ_ID);
293 /* Prevent double-linking to the same child. */
294 SIMPLEQ_FOREACH(cid, &node->child_ids, entry) {
295 if (got_object_id_cmp(cid->id, child_commit_id) == 0)
296 return got_error(GOT_ERR_BAD_OBJ_ID);
299 child = calloc(1, sizeof(*child));
300 if (child == NULL)
301 return got_error_from_errno();
302 child->id = got_object_id_dup(child_commit_id);
303 if (child->id == NULL) {
304 err = got_error_from_errno();
305 free(child);
306 return err;
308 SIMPLEQ_INSERT_TAIL(&node->child_ids, child, entry);
309 node->nchildren++;
312 return err;
315 const struct got_error *
316 got_commit_graph_open(struct got_commit_graph **graph,
317 struct got_object_id *commit_id, struct got_repository *repo)
319 const struct got_error *err = NULL;
320 struct got_object *obj;
321 struct got_commit_object *commit;
323 *graph = NULL;
325 err = got_object_open(&obj, repo, commit_id);
326 if (err)
327 return err;
328 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
329 err = got_error(GOT_ERR_OBJ_TYPE);
330 got_object_close(obj);
331 return err;
334 err = got_object_commit_open(&commit, repo, obj);
335 got_object_close(obj);
336 if (err)
337 return err;
339 *graph = alloc_graph();
340 if (*graph == NULL) {
341 got_object_commit_close(commit);
342 return got_error_from_errno();
345 err = add_node(&(*graph)->head_node, *graph, commit_id, commit, NULL);
346 if (err) {
347 got_commit_graph_close(*graph);
348 *graph = NULL;
349 return err;
352 return NULL;
355 static const struct got_error *
356 open_commit(struct got_commit_object **commit, struct got_object_id *id,
357 struct got_repository *repo)
359 const struct got_error *err;
360 struct got_object *obj;
362 err = got_object_open(&obj, repo, id);
363 if (err)
364 return err;
365 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
366 err = got_error(GOT_ERR_OBJ_TYPE);
367 goto done;
370 err = got_object_commit_open(commit, repo, obj);
371 done:
372 got_object_close(obj);
373 return err;
376 struct got_commit_graph_branch {
377 struct got_object_id parent_id;
378 struct got_commit_graph_node *node;
379 };
381 struct gather_branches_arg {
382 struct got_commit_graph_branch *branches;
383 int nbranches;
384 };
386 static void
387 gather_branches(struct got_object_id *id, void *data, void *arg)
389 struct gather_branches_arg *a = arg;
390 memcpy(&a->branches[a->nbranches].parent_id, id, sizeof(*id));
391 a->branches[a->nbranches].node = data;
392 a->nbranches++;
395 const struct got_error *
396 fetch_commits_from_open_branches(int *ncommits,
397 struct got_commit_graph *graph, struct got_repository *repo)
399 const struct got_error *err;
400 struct got_commit_graph_branch *branches;
401 struct gather_branches_arg arg;
402 int i;
404 *ncommits = 0;
406 arg.nbranches = got_object_idset_num_elements(graph->open_branches);
407 if (arg.nbranches == 0)
408 return NULL;
410 /*
411 * Adding nodes to the graph might change the graph's open
412 * branches state. Create a local copy of the current state.
413 */
414 branches = calloc(arg.nbranches, sizeof(*branches));
415 if (branches == NULL)
416 return got_error_from_errno();
417 arg.nbranches = 0; /* reset; gather_branches() will increment */
418 arg.branches = branches;
419 got_object_idset_for_each(graph->open_branches, gather_branches, &arg);
421 for (i = 0; i < arg.nbranches; i++) {
422 struct got_object_id *commit_id;
423 struct got_commit_graph_node *child_node, *new_node;
424 struct got_commit_object *commit;
426 commit_id = &branches[i].parent_id;
427 child_node = branches[i].node;
429 err = open_commit(&commit, commit_id, repo);
430 if (err)
431 break;
433 err = add_node(&new_node, graph, commit_id, commit,
434 &child_node->id);
435 if (err) {
436 if (err->code != GOT_ERR_OBJ_EXISTS)
437 break;
438 err = NULL;
440 if (new_node)
441 (*ncommits)++;
442 else
443 got_object_commit_close(commit);
446 free(branches);
447 return err;
450 const struct got_error *
451 got_commit_graph_fetch_commits(int *nfetched, struct got_commit_graph *graph,
452 int limit, struct got_repository *repo)
454 const struct got_error *err;
455 int total = 0, ncommits;
457 *nfetched = 0;
459 while (total < limit) {
460 err = fetch_commits_from_open_branches(&ncommits, graph, repo);
461 if (err)
462 return err;
463 if (ncommits == 0)
464 break;
465 total += ncommits;
468 *nfetched = total;
469 return NULL;
472 static void
473 free_graph_node(struct got_object_id *id, void *data, void *arg)
475 struct got_commit_graph_node *node = data;
476 got_object_commit_close(node->commit);
477 while (!SIMPLEQ_EMPTY(&node->child_ids)) {
478 struct got_parent_id *child = SIMPLEQ_FIRST(&node->child_ids);
479 SIMPLEQ_REMOVE_HEAD(&node->child_ids, entry);
480 free(child);
482 free(node);
485 void
486 got_commit_graph_close(struct got_commit_graph *graph)
488 got_object_idset_free(graph->open_branches);
489 got_object_idset_for_each(graph->node_ids, free_graph_node, NULL);
490 got_object_idset_free(graph->node_ids);
491 free(graph);
494 const struct got_error *
495 got_commit_graph_iter_start(struct got_commit_graph *graph,
496 struct got_object_id *id)
498 struct got_commit_graph_node *start_node, *node;
499 struct got_parent_id *pid;
501 start_node = got_object_idset_get(graph->node_ids, id);
502 if (start_node == NULL)
503 return got_error(GOT_ERR_NO_OBJ);
505 graph->iter_node = start_node;
507 while (!TAILQ_EMPTY(&graph->iter_candidates)) {
508 node = TAILQ_FIRST(&graph->iter_candidates);
509 TAILQ_REMOVE(&graph->iter_candidates, node, entry);
512 /* Put all known parents of this commit on the candidate list. */
513 SIMPLEQ_FOREACH(pid, &start_node->commit->parent_ids, entry) {
514 node = got_object_idset_get(graph->node_ids, pid->id);
515 if (node) {
516 const struct got_error *err;
517 err = add_iteration_candidate(graph, node);
518 if (err)
519 return err;
523 return NULL;
526 const struct got_error *
527 got_commit_graph_iter_next(struct got_commit_object **commit,
528 struct got_object_id **id, struct got_commit_graph *graph)
530 struct got_commit_graph_node *node;
532 if (graph->iter_node == NULL) {
533 /* We are done interating, or iteration was not started. */
534 *commit = NULL;
535 *id = NULL;
536 return NULL;
539 if (TAILQ_EMPTY(&graph->iter_candidates)) {
540 if (is_root_node(graph->iter_node) &&
541 got_object_idset_num_elements(graph->open_branches) == 0) {
542 *commit = graph->iter_node->commit;
543 *id = &graph->iter_node->id;
544 /* We are done interating. */
545 graph->iter_node = NULL;
546 return NULL;
548 return got_error(GOT_ERR_ITER_NEED_MORE);
551 *commit = graph->iter_node->commit;
552 *id = &graph->iter_node->id;
553 node = TAILQ_FIRST(&graph->iter_candidates);
554 TAILQ_REMOVE(&graph->iter_candidates, node, entry);
555 graph->iter_node = node;
556 return NULL;
559 const struct got_commit_object *
560 got_commit_graph_get_commit(struct got_commit_graph *graph,
561 struct got_object_id *id)
563 struct got_commit_graph_node *node;
564 node = got_object_idset_get(graph->node_ids, id);
565 return node ? node->commit : NULL;