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 int nparents;
48 struct got_object_id_queue parent_ids;
49 int nchildren;
50 struct got_object_id_queue child_ids;
52 time_t commit_timestamp;
54 /* Used during graph iteration. */
55 TAILQ_ENTRY(got_commit_graph_node) entry;
56 };
58 struct got_commit_graph {
59 /* The set of all commits we have traversed. */
60 struct got_object_idset *node_ids;
62 /* The commit at which traversal began (youngest commit in node_ids). */
63 struct got_commit_graph_node *head_node;
65 /*
66 * A set of object IDs of known parent commits which we have not yet
67 * traversed. Each commit ID in this set represents a branch in commit
68 * history: Either the first-parent branch of the head node, or another
69 * branch corresponding to a traversed merge commit for which we have
70 * not traversed a branch point commit yet.
71 *
72 * Whenever we add a commit with a matching ID to the graph, we remove
73 * its corresponding element from this set, and add new elements for
74 * each of that commit's parent commits which were not traversed yet.
75 *
76 * When API users ask us to fetch more commits, we fetch commits from
77 * all currently open branches. This allows API users to process
78 * commits in linear order even though the history contains branches.
79 */
80 struct got_object_idset *open_branches;
82 /* The next commit to return when the API user asks for one. */
83 struct got_commit_graph_node *iter_node;
85 TAILQ_HEAD(, got_commit_graph_node) iter_candidates;
86 };
88 static struct got_commit_graph *
89 alloc_graph(void)
90 {
91 struct got_commit_graph *graph;
93 graph = calloc(1, sizeof(*graph));
94 if (graph == NULL)
95 return NULL;
97 graph->node_ids = got_object_idset_alloc();
98 if (graph->node_ids == NULL) {
99 free(graph);
100 return NULL;
103 graph->open_branches = got_object_idset_alloc();
104 if (graph->open_branches == NULL) {
105 got_object_idset_free(graph->node_ids);
106 free(graph);
107 return NULL;
110 TAILQ_INIT(&graph->iter_candidates);
111 return graph;
114 #if 0
115 static int
116 is_head_node(struct got_commit_graph_node *node)
118 return node->nchildren == 0;
121 static int
122 is_merge_point(struct got_commit_graph_node *node)
124 return node->nparents > 1;
127 int
128 is_branch_point(struct got_commit_graph_node *node)
130 return node->nchildren > 1;
132 #endif
134 static int
135 is_root_node(struct got_commit_graph_node *node)
137 return node->nparents == 0;
140 static void
141 add_iteration_candidate(struct got_commit_graph *graph,
142 struct got_commit_graph_node *node)
144 struct got_commit_graph_node *n, *next;
146 if (TAILQ_EMPTY(&graph->iter_candidates)) {
147 TAILQ_INSERT_TAIL(&graph->iter_candidates, node, entry);
148 return;
151 TAILQ_FOREACH(n, &graph->iter_candidates, entry) {
152 if (node->commit_timestamp < n->commit_timestamp) {
153 next = TAILQ_NEXT(n, entry);
154 if (next == NULL) {
155 TAILQ_INSERT_AFTER(&graph->iter_candidates, n,
156 node, entry);
157 break;
159 if (node->commit_timestamp >= next->commit_timestamp) {
160 TAILQ_INSERT_BEFORE(next, node, entry);
161 break;
163 } else {
164 TAILQ_INSERT_BEFORE(n, node, entry);
165 break;
170 static const struct got_error *
171 add_vertex(struct got_object_id_queue *ids, struct got_object_id *id)
173 struct got_object_qid *qid;
175 qid = calloc(1, sizeof(*qid));
176 if (qid == NULL)
177 return got_error_from_errno();
179 qid->id = got_object_id_dup(id);
180 if (qid->id == NULL) {
181 const struct got_error *err = got_error_from_errno();
182 free(qid);
183 return err;
186 SIMPLEQ_INSERT_TAIL(ids, qid, entry);
187 return NULL;
190 static const struct got_error *
191 add_node(struct got_commit_graph_node **new_node,
192 struct got_commit_graph *graph, struct got_object_id *commit_id,
193 struct got_commit_object *commit, struct got_object_id *child_commit_id)
195 const struct got_error *err = NULL;
196 struct got_commit_graph_node *node, *existing_node;
197 struct got_object_qid *qid;
199 *new_node = NULL;
201 node = calloc(1, sizeof(*node));
202 if (node == NULL)
203 return got_error_from_errno();
205 memcpy(&node->id, commit_id, sizeof(node->id));
206 SIMPLEQ_INIT(&node->parent_ids);
207 SIMPLEQ_INIT(&node->child_ids);
208 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
209 err = add_vertex(&node->parent_ids, qid->id);
210 if (err)
211 return err;
212 node->nparents++;
214 node->commit_timestamp = commit->committer_time; /* XXX not UTC! */
216 err = got_object_idset_add((void **)(&existing_node),
217 graph->node_ids, &node->id, node);
218 if (err == NULL) {
219 struct got_object_qid *qid;
221 add_iteration_candidate(graph, node);
222 err = got_object_idset_remove(graph->open_branches, commit_id);
223 if (err && err->code != GOT_ERR_NO_OBJ)
224 return err;
225 SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
226 if (got_object_idset_get(graph->node_ids, qid->id))
227 continue; /* parent already traversed */
228 err = got_object_idset_add(NULL, graph->open_branches,
229 qid->id, node);
230 if (err && err->code != GOT_ERR_OBJ_EXISTS)
231 return err;
233 *new_node = node;
234 } else if (err->code == GOT_ERR_OBJ_EXISTS) {
235 err = NULL;
236 free(node);
237 node = existing_node;
238 } else {
239 free(node);
240 return err;
243 if (child_commit_id) {
244 struct got_object_qid *cid;
246 /* Prevent linking to self. */
247 if (got_object_id_cmp(commit_id, child_commit_id) == 0)
248 return got_error(GOT_ERR_BAD_OBJ_ID);
250 /* Prevent double-linking to the same child. */
251 SIMPLEQ_FOREACH(cid, &node->child_ids, entry) {
252 if (got_object_id_cmp(cid->id, child_commit_id) == 0)
253 return got_error(GOT_ERR_BAD_OBJ_ID);
256 err = add_vertex(&node->child_ids, child_commit_id);
257 if (err)
258 return err;
259 node->nchildren++;
263 return err;
266 const struct got_error *
267 got_commit_graph_open(struct got_commit_graph **graph,
268 struct got_object_id *commit_id, struct got_repository *repo)
270 const struct got_error *err = NULL;
271 struct got_object *obj;
272 struct got_commit_object *commit;
274 *graph = NULL;
276 err = got_object_open(&obj, repo, commit_id);
277 if (err)
278 return err;
279 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
280 err = got_error(GOT_ERR_OBJ_TYPE);
281 got_object_close(obj);
282 return err;
285 err = got_object_commit_open(&commit, repo, obj);
286 got_object_close(obj);
287 if (err)
288 return err;
290 *graph = alloc_graph();
291 if (*graph == NULL) {
292 got_object_commit_close(commit);
293 return got_error_from_errno();
296 err = add_node(&(*graph)->head_node, *graph, commit_id, commit, NULL);
297 got_object_commit_close(commit);
298 if (err) {
299 got_commit_graph_close(*graph);
300 *graph = NULL;
301 return err;
304 return NULL;
307 static const struct got_error *
308 open_commit(struct got_commit_object **commit, struct got_object_id *id,
309 struct got_repository *repo)
311 const struct got_error *err;
312 struct got_object *obj;
314 err = got_object_open(&obj, repo, id);
315 if (err)
316 return err;
317 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
318 err = got_error(GOT_ERR_OBJ_TYPE);
319 goto done;
322 err = got_object_commit_open(commit, repo, obj);
323 done:
324 got_object_close(obj);
325 return err;
328 struct got_commit_graph_branch {
329 struct got_object_id parent_id;
330 struct got_commit_graph_node *node;
331 };
333 struct gather_branches_arg {
334 struct got_commit_graph_branch *branches;
335 int nbranches;
336 };
338 static void
339 gather_branches(struct got_object_id *id, void *data, void *arg)
341 struct gather_branches_arg *a = arg;
342 memcpy(&a->branches[a->nbranches].parent_id, id, sizeof(*id));
343 a->branches[a->nbranches].node = data;
344 a->nbranches++;
347 const struct got_error *
348 fetch_commits_from_open_branches(int *ncommits,
349 struct got_commit_graph *graph, struct got_repository *repo)
351 const struct got_error *err;
352 struct got_commit_graph_branch *branches;
353 struct gather_branches_arg arg;
354 int i;
356 *ncommits = 0;
358 arg.nbranches = got_object_idset_num_elements(graph->open_branches);
359 if (arg.nbranches == 0)
360 return NULL;
362 /*
363 * Adding nodes to the graph might change the graph's open
364 * branches state. Create a local copy of the current state.
365 */
366 branches = calloc(arg.nbranches, sizeof(*branches));
367 if (branches == NULL)
368 return got_error_from_errno();
369 arg.nbranches = 0; /* reset; gather_branches() will increment */
370 arg.branches = branches;
371 got_object_idset_for_each(graph->open_branches, gather_branches, &arg);
373 for (i = 0; i < arg.nbranches; i++) {
374 struct got_object_id *commit_id;
375 struct got_commit_graph_node *child_node, *new_node;
376 struct got_commit_object *commit;
378 commit_id = &branches[i].parent_id;
379 child_node = branches[i].node;
381 err = open_commit(&commit, commit_id, repo);
382 if (err)
383 break;
385 err = add_node(&new_node, graph, commit_id, commit,
386 &child_node->id);
387 got_object_commit_close(commit);
388 if (err) {
389 if (err->code != GOT_ERR_OBJ_EXISTS)
390 break;
391 err = NULL;
393 if (new_node)
394 (*ncommits)++;
397 free(branches);
398 return err;
401 const struct got_error *
402 got_commit_graph_fetch_commits(int *nfetched, struct got_commit_graph *graph,
403 int limit, struct got_repository *repo)
405 const struct got_error *err;
406 int total = 0, ncommits;
408 *nfetched = 0;
410 while (total < limit) {
411 err = fetch_commits_from_open_branches(&ncommits, graph, repo);
412 if (err)
413 return err;
414 if (ncommits == 0)
415 break;
416 total += ncommits;
419 *nfetched = total;
420 return NULL;
423 static void
424 free_graph_node(struct got_object_id *id, void *data, void *arg)
426 struct got_commit_graph_node *node = data;
427 while (!SIMPLEQ_EMPTY(&node->child_ids)) {
428 struct got_object_qid *child = SIMPLEQ_FIRST(&node->child_ids);
429 SIMPLEQ_REMOVE_HEAD(&node->child_ids, entry);
430 free(child);
432 free(node);
435 void
436 got_commit_graph_close(struct got_commit_graph *graph)
438 got_object_idset_free(graph->open_branches);
439 got_object_idset_for_each(graph->node_ids, free_graph_node, NULL);
440 got_object_idset_free(graph->node_ids);
441 free(graph);
444 const struct got_error *
445 got_commit_graph_iter_start(struct got_commit_graph *graph,
446 struct got_object_id *id)
448 struct got_commit_graph_node *start_node, *node;
449 struct got_object_qid *qid;
451 start_node = got_object_idset_get(graph->node_ids, id);
452 if (start_node == NULL)
453 return got_error(GOT_ERR_NO_OBJ);
455 graph->iter_node = start_node;
457 while (!TAILQ_EMPTY(&graph->iter_candidates)) {
458 node = TAILQ_FIRST(&graph->iter_candidates);
459 TAILQ_REMOVE(&graph->iter_candidates, node, entry);
462 /* Put all known parents of this commit on the candidate list. */
463 SIMPLEQ_FOREACH(qid, &start_node->parent_ids, entry) {
464 node = got_object_idset_get(graph->node_ids, qid->id);
465 if (node)
466 add_iteration_candidate(graph, node);
469 return NULL;
472 const struct got_error *
473 got_commit_graph_iter_next(struct got_object_id **id,
474 struct got_commit_graph *graph)
476 struct got_commit_graph_node *node;
478 if (graph->iter_node == NULL) {
479 /* We are done interating, or iteration was not started. */
480 *id = NULL;
481 return NULL;
484 if (TAILQ_EMPTY(&graph->iter_candidates)) {
485 if (is_root_node(graph->iter_node) &&
486 got_object_idset_num_elements(graph->open_branches) == 0) {
487 *id = &graph->iter_node->id;
488 /* We are done interating. */
489 graph->iter_node = NULL;
490 return NULL;
492 return got_error(GOT_ERR_ITER_NEED_MORE);
495 *id = &graph->iter_node->id;
496 node = TAILQ_FIRST(&graph->iter_candidates);
497 TAILQ_REMOVE(&graph->iter_candidates, node, entry);
498 graph->iter_node = node;
499 return NULL;