Blame


1 372ccdbb 2018-06-10 stsp /*
2 372ccdbb 2018-06-10 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 372ccdbb 2018-06-10 stsp *
4 372ccdbb 2018-06-10 stsp * Permission to use, copy, modify, and distribute this software for any
5 372ccdbb 2018-06-10 stsp * purpose with or without fee is hereby granted, provided that the above
6 372ccdbb 2018-06-10 stsp * copyright notice and this permission notice appear in all copies.
7 372ccdbb 2018-06-10 stsp *
8 372ccdbb 2018-06-10 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 372ccdbb 2018-06-10 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 372ccdbb 2018-06-10 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 372ccdbb 2018-06-10 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 372ccdbb 2018-06-10 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 372ccdbb 2018-06-10 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 372ccdbb 2018-06-10 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 372ccdbb 2018-06-10 stsp */
16 372ccdbb 2018-06-10 stsp
17 372ccdbb 2018-06-10 stsp #include <sys/types.h>
18 372ccdbb 2018-06-10 stsp #include <sys/stat.h>
19 372ccdbb 2018-06-10 stsp #include <sys/queue.h>
20 372ccdbb 2018-06-10 stsp #include <sys/stdint.h>
21 372ccdbb 2018-06-10 stsp
22 56e0773d 2019-11-28 stsp #include <limits.h>
23 372ccdbb 2018-06-10 stsp #include <stdio.h>
24 372ccdbb 2018-06-10 stsp #include <stdlib.h>
25 372ccdbb 2018-06-10 stsp #include <string.h>
26 372ccdbb 2018-06-10 stsp #include <sha1.h>
27 372ccdbb 2018-06-10 stsp #include <zlib.h>
28 372ccdbb 2018-06-10 stsp #include <ctype.h>
29 372ccdbb 2018-06-10 stsp
30 372ccdbb 2018-06-10 stsp #include "got_error.h"
31 372ccdbb 2018-06-10 stsp #include "got_object.h"
32 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
33 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
34 324d37e7 2019-05-11 stsp #include "got_path.h"
35 372ccdbb 2018-06-10 stsp
36 372ccdbb 2018-06-10 stsp #include "got_lib_delta.h"
37 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
38 372ccdbb 2018-06-10 stsp #include "got_lib_object.h"
39 372ccdbb 2018-06-10 stsp #include "got_lib_object_idset.h"
40 372ccdbb 2018-06-10 stsp
41 372ccdbb 2018-06-10 stsp struct got_commit_graph_node {
42 372ccdbb 2018-06-10 stsp struct got_object_id id;
43 73026088 2018-11-18 stsp time_t timestamp;
44 b43fbaa0 2018-06-11 stsp
45 372ccdbb 2018-06-10 stsp /* Used during graph iteration. */
46 372ccdbb 2018-06-10 stsp TAILQ_ENTRY(got_commit_graph_node) entry;
47 372ccdbb 2018-06-10 stsp };
48 372ccdbb 2018-06-10 stsp
49 9ba79e04 2018-06-11 stsp TAILQ_HEAD(got_commit_graph_iter_list, got_commit_graph_node);
50 9ba79e04 2018-06-11 stsp
51 b565f6f8 2018-07-23 stsp struct got_commit_graph_branch_tip {
52 32777563 2018-11-07 stsp struct got_object_id *commit_id;
53 32777563 2018-11-07 stsp struct got_commit_object *commit;
54 32777563 2018-11-07 stsp struct got_commit_graph_node *new_node;
55 b565f6f8 2018-07-23 stsp };
56 b565f6f8 2018-07-23 stsp
57 372ccdbb 2018-06-10 stsp struct got_commit_graph {
58 372ccdbb 2018-06-10 stsp /* The set of all commits we have traversed. */
59 372ccdbb 2018-06-10 stsp struct got_object_idset *node_ids;
60 372ccdbb 2018-06-10 stsp
61 0ed6ed4c 2018-06-13 stsp int flags;
62 0ed6ed4c 2018-06-13 stsp #define GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL 0x01
63 0ed6ed4c 2018-06-13 stsp
64 372ccdbb 2018-06-10 stsp /*
65 372ccdbb 2018-06-10 stsp * A set of object IDs of known parent commits which we have not yet
66 372ccdbb 2018-06-10 stsp * traversed. Each commit ID in this set represents a branch in commit
67 372ccdbb 2018-06-10 stsp * history: Either the first-parent branch of the head node, or another
68 372ccdbb 2018-06-10 stsp * branch corresponding to a traversed merge commit for which we have
69 372ccdbb 2018-06-10 stsp * not traversed a branch point commit yet.
70 372ccdbb 2018-06-10 stsp *
71 372ccdbb 2018-06-10 stsp * Whenever we add a commit with a matching ID to the graph, we remove
72 372ccdbb 2018-06-10 stsp * its corresponding element from this set, and add new elements for
73 372ccdbb 2018-06-10 stsp * each of that commit's parent commits which were not traversed yet.
74 372ccdbb 2018-06-10 stsp *
75 372ccdbb 2018-06-10 stsp * When API users ask us to fetch more commits, we fetch commits from
76 372ccdbb 2018-06-10 stsp * all currently open branches. This allows API users to process
77 372ccdbb 2018-06-10 stsp * commits in linear order even though the history contains branches.
78 372ccdbb 2018-06-10 stsp */
79 372ccdbb 2018-06-10 stsp struct got_object_idset *open_branches;
80 b565f6f8 2018-07-23 stsp
81 32777563 2018-11-07 stsp /* Array of branch tips for fetch_commits_from_open_branches(). */
82 b565f6f8 2018-07-23 stsp struct got_commit_graph_branch_tip *tips;
83 5e50c36a 2018-11-08 stsp int ntips;
84 372ccdbb 2018-06-10 stsp
85 31cedeaf 2018-09-15 stsp /* Path of tree entry of interest to the API user. */
86 31cedeaf 2018-09-15 stsp char *path;
87 31cedeaf 2018-09-15 stsp
88 94489f7d 2020-01-04 stsp /*
89 94489f7d 2020-01-04 stsp * Nodes which will be passed to the API user next, sorted by
90 94489f7d 2020-01-04 stsp * commit timestmap.
91 94489f7d 2020-01-04 stsp */
92 9ba79e04 2018-06-11 stsp struct got_commit_graph_iter_list iter_list;
93 372ccdbb 2018-06-10 stsp };
94 372ccdbb 2018-06-10 stsp
95 31cedeaf 2018-09-15 stsp static const struct got_error *
96 41fa1437 2018-11-05 stsp detect_changed_path(int *changed, struct got_commit_object *commit,
97 31cedeaf 2018-09-15 stsp struct got_object_id *commit_id, const char *path,
98 31cedeaf 2018-09-15 stsp struct got_repository *repo)
99 31cedeaf 2018-09-15 stsp {
100 31cedeaf 2018-09-15 stsp const struct got_error *err = NULL;
101 41fa1437 2018-11-05 stsp struct got_commit_object *pcommit = NULL;
102 31cedeaf 2018-09-15 stsp struct got_tree_object *tree = NULL, *ptree = NULL;
103 31cedeaf 2018-09-15 stsp struct got_object_qid *pid;
104 31cedeaf 2018-09-15 stsp
105 31cedeaf 2018-09-15 stsp if (got_path_is_root_dir(path)) {
106 31cedeaf 2018-09-15 stsp *changed = 1;
107 31cedeaf 2018-09-15 stsp return NULL;
108 31cedeaf 2018-09-15 stsp }
109 31cedeaf 2018-09-15 stsp
110 31cedeaf 2018-09-15 stsp *changed = 0;
111 31cedeaf 2018-09-15 stsp
112 31cedeaf 2018-09-15 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
113 31cedeaf 2018-09-15 stsp if (pid == NULL) {
114 31cedeaf 2018-09-15 stsp struct got_object_id *obj_id;
115 31cedeaf 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
116 fd1d2703 2018-11-04 stsp if (err) {
117 d1451975 2018-11-11 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
118 fd1d2703 2018-11-04 stsp err = NULL;
119 fd1d2703 2018-11-04 stsp } else
120 fd1d2703 2018-11-04 stsp *changed = 1; /* The path was created in this commit. */
121 31cedeaf 2018-09-15 stsp free(obj_id);
122 0e9101d5 2018-11-18 stsp return err;
123 0e9101d5 2018-11-18 stsp }
124 7310c1c3 2018-11-18 stsp
125 0e9101d5 2018-11-18 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
126 0e9101d5 2018-11-18 stsp if (err)
127 0e9101d5 2018-11-18 stsp return err;
128 372ccdbb 2018-06-10 stsp
129 0e9101d5 2018-11-18 stsp err = got_object_open_as_commit(&pcommit, repo, pid->id);
130 0e9101d5 2018-11-18 stsp if (err)
131 0e9101d5 2018-11-18 stsp goto done;
132 0e9101d5 2018-11-18 stsp
133 0e9101d5 2018-11-18 stsp err = got_object_open_as_tree(&ptree, repo, pcommit->tree_id);
134 0e9101d5 2018-11-18 stsp if (err)
135 0e9101d5 2018-11-18 stsp goto done;
136 0e9101d5 2018-11-18 stsp
137 81a966c0 2018-11-18 stsp err = got_object_tree_path_changed(changed, tree, ptree, path, repo);
138 31cedeaf 2018-09-15 stsp done:
139 31cedeaf 2018-09-15 stsp if (tree)
140 31cedeaf 2018-09-15 stsp got_object_tree_close(tree);
141 31cedeaf 2018-09-15 stsp if (ptree)
142 31cedeaf 2018-09-15 stsp got_object_tree_close(ptree);
143 31cedeaf 2018-09-15 stsp if (pcommit)
144 41fa1437 2018-11-05 stsp got_object_commit_close(pcommit);
145 31cedeaf 2018-09-15 stsp return err;
146 31cedeaf 2018-09-15 stsp }
147 31cedeaf 2018-09-15 stsp
148 4626e416 2018-06-10 stsp static void
149 9ba79e04 2018-06-11 stsp add_node_to_iter_list(struct got_commit_graph *graph,
150 de56b2d7 2020-01-04 stsp struct got_commit_graph_node *node)
151 372ccdbb 2018-06-10 stsp {
152 4bd3f2bb 2018-06-10 stsp struct got_commit_graph_node *n, *next;
153 1c7a5dcb 2018-09-15 stsp
154 94489f7d 2020-01-04 stsp n = TAILQ_FIRST(&graph->iter_list);
155 bee6b577 2018-09-19 stsp while (n) {
156 bee6b577 2018-09-19 stsp next = TAILQ_NEXT(n, entry);
157 73026088 2018-11-18 stsp if (next && node->timestamp >= next->timestamp) {
158 bee6b577 2018-09-19 stsp TAILQ_INSERT_BEFORE(next, node, entry);
159 bee6b577 2018-09-19 stsp return;
160 fe8df4c2 2018-06-16 stsp }
161 bee6b577 2018-09-19 stsp n = next;
162 fe8df4c2 2018-06-16 stsp }
163 93e45b7c 2018-09-24 stsp TAILQ_INSERT_TAIL(&graph->iter_list, node, entry);
164 0ed6ed4c 2018-06-13 stsp }
165 0ed6ed4c 2018-06-13 stsp
166 0ed6ed4c 2018-06-13 stsp static const struct got_error *
167 14159a7b 2020-01-04 stsp advance_branch(struct got_commit_graph *graph, struct got_object_id *commit_id,
168 14159a7b 2020-01-04 stsp struct got_commit_object *commit, struct got_repository *repo)
169 0ed6ed4c 2018-06-13 stsp {
170 0ed6ed4c 2018-06-13 stsp const struct got_error *err;
171 0ed6ed4c 2018-06-13 stsp struct got_object_qid *qid;
172 0ed6ed4c 2018-06-13 stsp
173 0ed6ed4c 2018-06-13 stsp if (graph->flags & GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL) {
174 0ed6ed4c 2018-06-13 stsp qid = SIMPLEQ_FIRST(&commit->parent_ids);
175 9b88e78c 2018-11-18 stsp if (qid == NULL ||
176 8e291695 2020-01-04 stsp got_object_idset_contains(graph->open_branches, qid->id))
177 0ed6ed4c 2018-06-13 stsp return NULL;
178 9b88e78c 2018-11-18 stsp return got_object_idset_add(graph->open_branches,
179 8e291695 2020-01-04 stsp qid->id, NULL);
180 bee6b577 2018-09-19 stsp }
181 bee6b577 2018-09-19 stsp
182 bee6b577 2018-09-19 stsp /*
183 bee6b577 2018-09-19 stsp * If we are graphing commits for a specific path, skip branches
184 bee6b577 2018-09-19 stsp * which do not contribute any content to this path.
185 bee6b577 2018-09-19 stsp */
186 7a62478b 2018-11-18 stsp if (commit->nparents > 1 && !got_path_is_root_dir(graph->path)) {
187 6dcdad08 2018-11-18 stsp struct got_object_id *merged_id, *prev_id = NULL;
188 bee6b577 2018-09-19 stsp int branches_differ = 0;
189 bee6b577 2018-09-19 stsp
190 bee6b577 2018-09-19 stsp err = got_object_id_by_path(&merged_id, repo, commit_id,
191 bee6b577 2018-09-19 stsp graph->path);
192 bee6b577 2018-09-19 stsp if (err)
193 bee6b577 2018-09-19 stsp return err;
194 bee6b577 2018-09-19 stsp
195 bee6b577 2018-09-19 stsp SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
196 6dcdad08 2018-11-18 stsp struct got_object_id *id;
197 6dcdad08 2018-11-18 stsp
198 8e291695 2020-01-04 stsp if (got_object_idset_contains(graph->open_branches,
199 8e291695 2020-01-04 stsp qid->id))
200 9b88e78c 2018-11-18 stsp continue;
201 bee6b577 2018-09-19 stsp
202 bee6b577 2018-09-19 stsp err = got_object_id_by_path(&id, repo, qid->id,
203 bee6b577 2018-09-19 stsp graph->path);
204 bee6b577 2018-09-19 stsp if (err) {
205 d1451975 2018-11-11 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY) {
206 bee6b577 2018-09-19 stsp branches_differ = 1;
207 bee6b577 2018-09-19 stsp continue;
208 bee6b577 2018-09-19 stsp }
209 6dcdad08 2018-11-18 stsp free(merged_id);
210 6dcdad08 2018-11-18 stsp free(prev_id);
211 bee6b577 2018-09-19 stsp return err;
212 bee6b577 2018-09-19 stsp }
213 bee6b577 2018-09-19 stsp
214 bee6b577 2018-09-19 stsp if (prev_id) {
215 bee6b577 2018-09-19 stsp if (!branches_differ &&
216 d2c2d781 2018-11-18 stsp got_object_id_cmp(id, prev_id) != 0)
217 bee6b577 2018-09-19 stsp branches_differ = 1;
218 6dcdad08 2018-11-18 stsp free(prev_id);
219 6dcdad08 2018-11-18 stsp }
220 6dcdad08 2018-11-18 stsp prev_id = id;
221 bee6b577 2018-09-19 stsp
222 bee6b577 2018-09-19 stsp /*
223 bee6b577 2018-09-19 stsp * If a branch has created the merged content we can
224 bee6b577 2018-09-19 stsp * skip any other branches.
225 bee6b577 2018-09-19 stsp */
226 bee6b577 2018-09-19 stsp if (got_object_id_cmp(merged_id, id) == 0) {
227 b36429ab 2018-11-05 stsp err = got_object_idset_add(graph->open_branches,
228 8e291695 2020-01-04 stsp qid->id, NULL);
229 6dcdad08 2018-11-18 stsp free(merged_id);
230 6dcdad08 2018-11-18 stsp free(id);
231 b36429ab 2018-11-05 stsp return err;
232 bee6b577 2018-09-19 stsp }
233 bee6b577 2018-09-19 stsp }
234 6dcdad08 2018-11-18 stsp
235 6dcdad08 2018-11-18 stsp free(prev_id);
236 6dcdad08 2018-11-18 stsp prev_id = NULL;
237 6dcdad08 2018-11-18 stsp free(merged_id);
238 6dcdad08 2018-11-18 stsp merged_id = NULL;
239 bee6b577 2018-09-19 stsp
240 bee6b577 2018-09-19 stsp /*
241 bee6b577 2018-09-19 stsp * If the path's content is the same on all branches,
242 bee6b577 2018-09-19 stsp * follow the first parent only.
243 bee6b577 2018-09-19 stsp */
244 bee6b577 2018-09-19 stsp if (!branches_differ) {
245 bee6b577 2018-09-19 stsp qid = SIMPLEQ_FIRST(&commit->parent_ids);
246 bee6b577 2018-09-19 stsp if (qid == NULL)
247 bee6b577 2018-09-19 stsp return NULL;
248 8e291695 2020-01-04 stsp if (got_object_idset_contains(graph->open_branches,
249 8e291695 2020-01-04 stsp qid->id))
250 b36429ab 2018-11-05 stsp return NULL;
251 8e291695 2020-01-04 stsp if (got_object_idset_contains(graph->node_ids,
252 8e291695 2020-01-04 stsp qid->id))
253 998ff57f 2018-11-18 stsp return NULL; /* parent already traversed */
254 b36429ab 2018-11-05 stsp return got_object_idset_add(graph->open_branches,
255 8e291695 2020-01-04 stsp qid->id, NULL);
256 bee6b577 2018-09-19 stsp }
257 0ed6ed4c 2018-06-13 stsp }
258 0ed6ed4c 2018-06-13 stsp
259 0ed6ed4c 2018-06-13 stsp SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
260 8e291695 2020-01-04 stsp if (got_object_idset_contains(graph->open_branches, qid->id))
261 b36429ab 2018-11-05 stsp continue;
262 8e291695 2020-01-04 stsp if (got_object_idset_contains(graph->node_ids, qid->id))
263 998ff57f 2018-11-18 stsp continue; /* parent already traversed */
264 8e291695 2020-01-04 stsp err = got_object_idset_add(graph->open_branches, qid->id, NULL);
265 b36429ab 2018-11-05 stsp if (err)
266 0ed6ed4c 2018-06-13 stsp return err;
267 0ed6ed4c 2018-06-13 stsp }
268 0ed6ed4c 2018-06-13 stsp
269 b43fbaa0 2018-06-11 stsp return NULL;
270 fb5b9d5a 2018-07-23 stsp }
271 fb5b9d5a 2018-07-23 stsp
272 b43fbaa0 2018-06-11 stsp static const struct got_error *
273 de56b2d7 2020-01-04 stsp add_node(struct got_commit_graph_node **new_node,
274 de56b2d7 2020-01-04 stsp struct got_commit_graph *graph,
275 de56b2d7 2020-01-04 stsp struct got_object_id *commit_id,
276 de56b2d7 2020-01-04 stsp struct got_commit_object *commit,
277 de56b2d7 2020-01-04 stsp struct got_repository *repo)
278 372ccdbb 2018-06-10 stsp {
279 372ccdbb 2018-06-10 stsp const struct got_error *err = NULL;
280 f4ceb45e 2018-07-23 stsp struct got_commit_graph_node *node;
281 372ccdbb 2018-06-10 stsp
282 372ccdbb 2018-06-10 stsp *new_node = NULL;
283 372ccdbb 2018-06-10 stsp
284 372ccdbb 2018-06-10 stsp node = calloc(1, sizeof(*node));
285 372ccdbb 2018-06-10 stsp if (node == NULL)
286 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
287 372ccdbb 2018-06-10 stsp
288 372ccdbb 2018-06-10 stsp memcpy(&node->id, commit_id, sizeof(node->id));
289 73026088 2018-11-18 stsp node->timestamp = commit->committer_time;
290 372ccdbb 2018-06-10 stsp
291 8e291695 2020-01-04 stsp err = got_object_idset_add(graph->node_ids, &node->id, NULL);
292 de56b2d7 2020-01-04 stsp if (err)
293 7a62478b 2018-11-18 stsp free(node);
294 de56b2d7 2020-01-04 stsp else
295 de56b2d7 2020-01-04 stsp *new_node = node;
296 de56b2d7 2020-01-04 stsp return err;
297 de56b2d7 2020-01-04 stsp }
298 372ccdbb 2018-06-10 stsp
299 de56b2d7 2020-01-04 stsp const struct got_error *
300 c4d7a9c4 2018-06-11 stsp got_commit_graph_open(struct got_commit_graph **graph,
301 3d509237 2020-01-04 stsp const char *path, int first_parent_traversal)
302 3d509237 2020-01-04 stsp {
303 22220781 2020-01-04 stsp const struct got_error *err = NULL;
304 3ddcebf3 2020-01-04 stsp
305 3ddcebf3 2020-01-04 stsp *graph = calloc(1, sizeof(**graph));
306 3d509237 2020-01-04 stsp if (*graph == NULL)
307 3ddcebf3 2020-01-04 stsp return got_error_from_errno("calloc");
308 88cdb9c6 2020-01-04 stsp
309 88cdb9c6 2020-01-04 stsp TAILQ_INIT(&(*graph)->iter_list);
310 3ddcebf3 2020-01-04 stsp
311 3ddcebf3 2020-01-04 stsp (*graph)->path = strdup(path);
312 3ddcebf3 2020-01-04 stsp if ((*graph)->path == NULL) {
313 3ddcebf3 2020-01-04 stsp err = got_error_from_errno("strdup");
314 22220781 2020-01-04 stsp goto done;
315 3ddcebf3 2020-01-04 stsp }
316 3ddcebf3 2020-01-04 stsp
317 3ddcebf3 2020-01-04 stsp (*graph)->node_ids = got_object_idset_alloc();
318 3ddcebf3 2020-01-04 stsp if ((*graph)->node_ids == NULL) {
319 3ddcebf3 2020-01-04 stsp err = got_error_from_errno("got_object_idset_alloc");
320 22220781 2020-01-04 stsp goto done;
321 3ddcebf3 2020-01-04 stsp }
322 372ccdbb 2018-06-10 stsp
323 3ddcebf3 2020-01-04 stsp (*graph)->open_branches = got_object_idset_alloc();
324 3ddcebf3 2020-01-04 stsp if ((*graph)->open_branches == NULL) {
325 3ddcebf3 2020-01-04 stsp err = got_error_from_errno("got_object_idset_alloc");
326 22220781 2020-01-04 stsp goto done;
327 3ddcebf3 2020-01-04 stsp }
328 3ddcebf3 2020-01-04 stsp
329 0ed6ed4c 2018-06-13 stsp if (first_parent_traversal)
330 0ed6ed4c 2018-06-13 stsp (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
331 22220781 2020-01-04 stsp done:
332 22220781 2020-01-04 stsp if (err) {
333 22220781 2020-01-04 stsp got_commit_graph_close(*graph);
334 22220781 2020-01-04 stsp *graph = NULL;
335 22220781 2020-01-04 stsp }
336 22220781 2020-01-04 stsp return err;
337 372ccdbb 2018-06-10 stsp }
338 372ccdbb 2018-06-10 stsp
339 32777563 2018-11-07 stsp struct add_branch_tip_arg {
340 b565f6f8 2018-07-23 stsp struct got_commit_graph_branch_tip *tips;
341 b565f6f8 2018-07-23 stsp int ntips;
342 32777563 2018-11-07 stsp struct got_repository *repo;
343 32777563 2018-11-07 stsp struct got_commit_graph *graph;
344 372ccdbb 2018-06-10 stsp };
345 372ccdbb 2018-06-10 stsp
346 cb103d04 2018-11-07 stsp static const struct got_error *
347 32777563 2018-11-07 stsp add_branch_tip(struct got_object_id *commit_id, void *data, void *arg)
348 372ccdbb 2018-06-10 stsp {
349 32777563 2018-11-07 stsp const struct got_error *err;
350 32777563 2018-11-07 stsp struct add_branch_tip_arg *a = arg;
351 32777563 2018-11-07 stsp struct got_commit_graph_node *new_node;
352 32777563 2018-11-07 stsp struct got_commit_object *commit;
353 32777563 2018-11-07 stsp
354 32777563 2018-11-07 stsp err = got_object_open_as_commit(&commit, a->repo, commit_id);
355 32777563 2018-11-07 stsp if (err)
356 32777563 2018-11-07 stsp return err;
357 32777563 2018-11-07 stsp
358 de56b2d7 2020-01-04 stsp err = add_node(&new_node, a->graph, commit_id, commit, a->repo);
359 32777563 2018-11-07 stsp if (err)
360 32777563 2018-11-07 stsp return err;
361 32777563 2018-11-07 stsp
362 32777563 2018-11-07 stsp a->tips[a->ntips].commit_id = new_node ? &new_node->id : NULL;
363 32777563 2018-11-07 stsp a->tips[a->ntips].commit = commit;
364 32777563 2018-11-07 stsp a->tips[a->ntips].new_node = new_node;
365 b565f6f8 2018-07-23 stsp a->ntips++;
366 32777563 2018-11-07 stsp
367 cb103d04 2018-11-07 stsp return NULL;
368 372ccdbb 2018-06-10 stsp }
369 372ccdbb 2018-06-10 stsp
370 1142eae9 2018-06-11 stsp static const struct got_error *
371 57eecd46 2020-01-04 stsp fetch_commits_from_open_branches(struct got_commit_graph *graph,
372 6fb7cd11 2019-08-22 stsp struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
373 372ccdbb 2018-06-10 stsp {
374 372ccdbb 2018-06-10 stsp const struct got_error *err;
375 32777563 2018-11-07 stsp struct add_branch_tip_arg arg;
376 32777563 2018-11-07 stsp int i, ntips;
377 372ccdbb 2018-06-10 stsp
378 32777563 2018-11-07 stsp ntips = got_object_idset_num_elements(graph->open_branches);
379 32777563 2018-11-07 stsp if (ntips == 0)
380 372ccdbb 2018-06-10 stsp return NULL;
381 372ccdbb 2018-06-10 stsp
382 32777563 2018-11-07 stsp /* (Re-)allocate branch tips array if necessary. */
383 32777563 2018-11-07 stsp if (graph->ntips < ntips) {
384 b565f6f8 2018-07-23 stsp struct got_commit_graph_branch_tip *tips;
385 5e50c36a 2018-11-08 stsp tips = recallocarray(graph->tips, graph->ntips, ntips,
386 5e50c36a 2018-11-08 stsp sizeof(*tips));
387 b565f6f8 2018-07-23 stsp if (tips == NULL)
388 638f9024 2019-05-13 stsp return got_error_from_errno("recallocarray");
389 b565f6f8 2018-07-23 stsp graph->tips = tips;
390 32777563 2018-11-07 stsp graph->ntips = ntips;
391 b565f6f8 2018-07-23 stsp }
392 b565f6f8 2018-07-23 stsp arg.tips = graph->tips;
393 32777563 2018-11-07 stsp arg.ntips = 0; /* add_branch_tip() will increment */
394 32777563 2018-11-07 stsp arg.repo = repo;
395 32777563 2018-11-07 stsp arg.graph = graph;
396 32777563 2018-11-07 stsp err = got_object_idset_for_each(graph->open_branches, add_branch_tip,
397 32777563 2018-11-07 stsp &arg);
398 cb103d04 2018-11-07 stsp if (err)
399 32777563 2018-11-07 stsp goto done;
400 372ccdbb 2018-06-10 stsp
401 b565f6f8 2018-07-23 stsp for (i = 0; i < arg.ntips; i++) {
402 372ccdbb 2018-06-10 stsp struct got_object_id *commit_id;
403 41fa1437 2018-11-05 stsp struct got_commit_object *commit;
404 32777563 2018-11-07 stsp struct got_commit_graph_node *new_node;
405 13a851c1 2020-01-04 stsp int changed;
406 372ccdbb 2018-06-10 stsp
407 6fb7cd11 2019-08-22 stsp if (cancel_cb) {
408 6fb7cd11 2019-08-22 stsp err = (*cancel_cb)(cancel_arg);
409 6fb7cd11 2019-08-22 stsp if (err)
410 6fb7cd11 2019-08-22 stsp break;
411 6fb7cd11 2019-08-22 stsp }
412 6fb7cd11 2019-08-22 stsp
413 32777563 2018-11-07 stsp commit_id = arg.tips[i].commit_id;
414 32777563 2018-11-07 stsp commit = arg.tips[i].commit;
415 32777563 2018-11-07 stsp new_node = arg.tips[i].new_node;
416 de56b2d7 2020-01-04 stsp
417 e7a5b9e9 2020-01-05 stsp err = got_object_idset_remove(NULL, graph->open_branches,
418 e7a5b9e9 2020-01-05 stsp commit_id);
419 e7a5b9e9 2020-01-05 stsp if (err && err->code != GOT_ERR_NO_OBJ)
420 e7a5b9e9 2020-01-05 stsp break;
421 e7a5b9e9 2020-01-05 stsp
422 13a851c1 2020-01-04 stsp err = detect_changed_path(&changed, commit, commit_id,
423 13a851c1 2020-01-04 stsp graph->path, repo);
424 13a851c1 2020-01-04 stsp if (err) {
425 13a851c1 2020-01-04 stsp if (err->code != GOT_ERR_NO_OBJ)
426 13a851c1 2020-01-04 stsp break;
427 13a851c1 2020-01-04 stsp /*
428 13a851c1 2020-01-04 stsp * History of the path stops here on the current
429 13a851c1 2020-01-04 stsp * branch. Keep going on other branches.
430 13a851c1 2020-01-04 stsp */
431 ec1904dc 2020-01-04 stsp continue;
432 13a851c1 2020-01-04 stsp }
433 57eecd46 2020-01-04 stsp if (changed)
434 de56b2d7 2020-01-04 stsp add_node_to_iter_list(graph, new_node);
435 14159a7b 2020-01-04 stsp err = advance_branch(graph, commit_id, commit, repo);
436 cb352812 2018-07-22 stsp if (err)
437 cb352812 2018-07-22 stsp break;
438 372ccdbb 2018-06-10 stsp }
439 32777563 2018-11-07 stsp done:
440 246e1c78 2018-11-08 stsp for (i = 0; i < arg.ntips; i++)
441 32777563 2018-11-07 stsp got_object_commit_close(arg.tips[i].commit);
442 372ccdbb 2018-06-10 stsp return err;
443 372ccdbb 2018-06-10 stsp }
444 372ccdbb 2018-06-10 stsp
445 372ccdbb 2018-06-10 stsp void
446 372ccdbb 2018-06-10 stsp got_commit_graph_close(struct got_commit_graph *graph)
447 372ccdbb 2018-06-10 stsp {
448 22220781 2020-01-04 stsp if (graph->open_branches)
449 22220781 2020-01-04 stsp got_object_idset_free(graph->open_branches);
450 22220781 2020-01-04 stsp if (graph->node_ids)
451 22220781 2020-01-04 stsp got_object_idset_free(graph->node_ids);
452 b565f6f8 2018-07-23 stsp free(graph->tips);
453 31cedeaf 2018-09-15 stsp free(graph->path);
454 372ccdbb 2018-06-10 stsp free(graph);
455 372ccdbb 2018-06-10 stsp }
456 372ccdbb 2018-06-10 stsp
457 372ccdbb 2018-06-10 stsp const struct got_error *
458 372ccdbb 2018-06-10 stsp got_commit_graph_iter_start(struct got_commit_graph *graph,
459 6fb7cd11 2019-08-22 stsp struct got_object_id *id, struct got_repository *repo,
460 6fb7cd11 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
461 372ccdbb 2018-06-10 stsp {
462 31cedeaf 2018-09-15 stsp const struct got_error *err = NULL;
463 372ccdbb 2018-06-10 stsp
464 3d509237 2020-01-04 stsp if (!TAILQ_EMPTY(&graph->iter_list))
465 3d509237 2020-01-04 stsp return got_error(GOT_ERR_ITER_BUSY);
466 31cedeaf 2018-09-15 stsp
467 3ff3126d 2020-01-04 stsp err = got_object_idset_add(graph->open_branches, id, NULL);
468 3d509237 2020-01-04 stsp if (err)
469 7e33c8c5 2020-01-04 stsp return err;
470 3d509237 2020-01-04 stsp
471 3ff3126d 2020-01-04 stsp /* Locate first commit which changed graph->path. */
472 94489f7d 2020-01-04 stsp while (TAILQ_EMPTY(&graph->iter_list) &&
473 3ff3126d 2020-01-04 stsp got_object_idset_num_elements(graph->open_branches) > 0) {
474 3ff3126d 2020-01-04 stsp err = fetch_commits_from_open_branches(graph, repo,
475 3ff3126d 2020-01-04 stsp cancel_cb, cancel_arg);
476 3ff3126d 2020-01-04 stsp if (err)
477 7e33c8c5 2020-01-04 stsp return err;
478 5175b31a 2020-01-04 stsp }
479 5175b31a 2020-01-04 stsp
480 94489f7d 2020-01-04 stsp if (TAILQ_EMPTY(&graph->iter_list)) {
481 5175b31a 2020-01-04 stsp const char *path;
482 5175b31a 2020-01-04 stsp if (got_path_is_root_dir(graph->path))
483 5175b31a 2020-01-04 stsp return got_error_no_obj(id);
484 5175b31a 2020-01-04 stsp path = graph->path;
485 5175b31a 2020-01-04 stsp while (path[0] == '/')
486 5175b31a 2020-01-04 stsp path++;
487 5175b31a 2020-01-04 stsp return got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
488 31cedeaf 2018-09-15 stsp }
489 7e33c8c5 2020-01-04 stsp
490 7e33c8c5 2020-01-04 stsp return NULL;
491 372ccdbb 2018-06-10 stsp }
492 372ccdbb 2018-06-10 stsp
493 372ccdbb 2018-06-10 stsp const struct got_error *
494 b43fbaa0 2018-06-11 stsp got_commit_graph_iter_next(struct got_object_id **id,
495 ee780d5c 2020-01-04 stsp struct got_commit_graph *graph, struct got_repository *repo,
496 ee780d5c 2020-01-04 stsp got_cancel_cb cancel_cb, void *cancel_arg)
497 372ccdbb 2018-06-10 stsp {
498 ee780d5c 2020-01-04 stsp const struct got_error *err = NULL;
499 94489f7d 2020-01-04 stsp struct got_commit_graph_node *node;
500 ee780d5c 2020-01-04 stsp
501 9ba79e04 2018-06-11 stsp *id = NULL;
502 372ccdbb 2018-06-10 stsp
503 df8cd9c6 2020-01-05 stsp node = TAILQ_FIRST(&graph->iter_list);
504 df8cd9c6 2020-01-05 stsp if (node == NULL) {
505 2c7f8870 2018-09-19 stsp /* We are done iterating, or iteration was not started. */
506 9ba79e04 2018-06-11 stsp return got_error(GOT_ERR_ITER_COMPLETED);
507 9ba79e04 2018-06-11 stsp }
508 9ba79e04 2018-06-11 stsp
509 94489f7d 2020-01-04 stsp while (TAILQ_NEXT(node, entry) == NULL &&
510 ee780d5c 2020-01-04 stsp got_object_idset_num_elements(graph->open_branches) > 0) {
511 57eecd46 2020-01-04 stsp err = fetch_commits_from_open_branches(graph, repo,
512 57eecd46 2020-01-04 stsp cancel_cb, cancel_arg);
513 ee780d5c 2020-01-04 stsp if (err)
514 ee780d5c 2020-01-04 stsp return err;
515 ee780d5c 2020-01-04 stsp }
516 372ccdbb 2018-06-10 stsp
517 94489f7d 2020-01-04 stsp *id = &node->id;
518 94489f7d 2020-01-04 stsp TAILQ_REMOVE(&graph->iter_list, node, entry);
519 372ccdbb 2018-06-10 stsp return NULL;
520 372ccdbb 2018-06-10 stsp }
521 a9833bc9 2019-05-13 stsp
522 a9833bc9 2019-05-13 stsp const struct got_error *
523 a9833bc9 2019-05-13 stsp got_commit_graph_find_youngest_common_ancestor(struct got_object_id **yca_id,
524 a9833bc9 2019-05-13 stsp struct got_object_id *commit_id, struct got_object_id *commit_id2,
525 6fb7cd11 2019-08-22 stsp struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
526 a9833bc9 2019-05-13 stsp {
527 a9833bc9 2019-05-13 stsp const struct got_error *err = NULL;
528 a9833bc9 2019-05-13 stsp struct got_commit_graph *graph = NULL, *graph2 = NULL;
529 a9833bc9 2019-05-13 stsp int completed = 0, completed2 = 0;
530 a9833bc9 2019-05-13 stsp struct got_object_idset *commit_ids;
531 a9833bc9 2019-05-13 stsp
532 a9833bc9 2019-05-13 stsp *yca_id = NULL;
533 a9833bc9 2019-05-13 stsp
534 a9833bc9 2019-05-13 stsp commit_ids = got_object_idset_alloc();
535 a9833bc9 2019-05-13 stsp if (commit_ids == NULL)
536 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_idset_alloc");
537 a9833bc9 2019-05-13 stsp
538 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
539 a9833bc9 2019-05-13 stsp if (err)
540 a9833bc9 2019-05-13 stsp goto done;
541 a9833bc9 2019-05-13 stsp
542 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph2, "/", 1);
543 a9833bc9 2019-05-13 stsp if (err)
544 a9833bc9 2019-05-13 stsp goto done;
545 a9833bc9 2019-05-13 stsp
546 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, commit_id, repo,
547 6fb7cd11 2019-08-22 stsp cancel_cb, cancel_arg);
548 a9833bc9 2019-05-13 stsp if (err)
549 a9833bc9 2019-05-13 stsp goto done;
550 a9833bc9 2019-05-13 stsp
551 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph2, commit_id2, repo,
552 6fb7cd11 2019-08-22 stsp cancel_cb, cancel_arg);
553 a9833bc9 2019-05-13 stsp if (err)
554 a9833bc9 2019-05-13 stsp goto done;
555 a9833bc9 2019-05-13 stsp
556 a9833bc9 2019-05-13 stsp for (;;) {
557 ee780d5c 2020-01-04 stsp struct got_object_id *id = NULL, *id2 = NULL;
558 a9833bc9 2019-05-13 stsp
559 6fb7cd11 2019-08-22 stsp if (cancel_cb) {
560 6fb7cd11 2019-08-22 stsp err = (*cancel_cb)(cancel_arg);
561 6fb7cd11 2019-08-22 stsp if (err)
562 6fb7cd11 2019-08-22 stsp break;
563 6fb7cd11 2019-08-22 stsp }
564 6fb7cd11 2019-08-22 stsp
565 a9833bc9 2019-05-13 stsp if (!completed) {
566 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
567 ee780d5c 2020-01-04 stsp cancel_cb, cancel_arg);
568 a9833bc9 2019-05-13 stsp if (err) {
569 ee780d5c 2020-01-04 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
570 a9833bc9 2019-05-13 stsp break;
571 ee780d5c 2020-01-04 stsp err = NULL;
572 ee780d5c 2020-01-04 stsp completed = 1;
573 a9833bc9 2019-05-13 stsp }
574 a9833bc9 2019-05-13 stsp }
575 a9833bc9 2019-05-13 stsp
576 a9833bc9 2019-05-13 stsp if (!completed2) {
577 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id2, graph2, repo,
578 ee780d5c 2020-01-04 stsp cancel_cb, cancel_arg);
579 a9833bc9 2019-05-13 stsp if (err) {
580 ee780d5c 2020-01-04 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
581 a9833bc9 2019-05-13 stsp break;
582 ee780d5c 2020-01-04 stsp err = NULL;
583 ee780d5c 2020-01-04 stsp completed2 = 1;
584 a9833bc9 2019-05-13 stsp }
585 a9833bc9 2019-05-13 stsp }
586 a9833bc9 2019-05-13 stsp
587 a9833bc9 2019-05-13 stsp if (id) {
588 8e291695 2020-01-04 stsp if (got_object_idset_contains(commit_ids, id)) {
589 a9833bc9 2019-05-13 stsp *yca_id = got_object_id_dup(id);
590 a9833bc9 2019-05-13 stsp if (*yca_id)
591 a9833bc9 2019-05-13 stsp break;
592 82751bc5 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
593 a9833bc9 2019-05-13 stsp break;
594 a9833bc9 2019-05-13 stsp
595 a9833bc9 2019-05-13 stsp }
596 8e291695 2020-01-04 stsp err = got_object_idset_add(commit_ids, id, NULL);
597 a9833bc9 2019-05-13 stsp if (err)
598 a9833bc9 2019-05-13 stsp break;
599 a9833bc9 2019-05-13 stsp }
600 a9833bc9 2019-05-13 stsp if (id2) {
601 8e291695 2020-01-04 stsp if (got_object_idset_contains(commit_ids, id2)) {
602 a9833bc9 2019-05-13 stsp *yca_id = got_object_id_dup(id2);
603 a9833bc9 2019-05-13 stsp if (*yca_id)
604 a9833bc9 2019-05-13 stsp break;
605 82751bc5 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
606 a9833bc9 2019-05-13 stsp break;
607 a9833bc9 2019-05-13 stsp
608 a9833bc9 2019-05-13 stsp }
609 8e291695 2020-01-04 stsp err = got_object_idset_add(commit_ids, id2, NULL);
610 a9833bc9 2019-05-13 stsp if (err)
611 a9833bc9 2019-05-13 stsp break;
612 a9833bc9 2019-05-13 stsp }
613 a9833bc9 2019-05-13 stsp
614 a9833bc9 2019-05-13 stsp if (completed && completed2) {
615 a9833bc9 2019-05-13 stsp err = got_error(GOT_ERR_ANCESTRY);
616 a9833bc9 2019-05-13 stsp break;
617 a9833bc9 2019-05-13 stsp }
618 a9833bc9 2019-05-13 stsp
619 a9833bc9 2019-05-13 stsp }
620 a9833bc9 2019-05-13 stsp done:
621 a9833bc9 2019-05-13 stsp got_object_idset_free(commit_ids);
622 a9833bc9 2019-05-13 stsp if (graph)
623 a9833bc9 2019-05-13 stsp got_commit_graph_close(graph);
624 a9833bc9 2019-05-13 stsp if (graph2)
625 a9833bc9 2019-05-13 stsp got_commit_graph_close(graph2);
626 a9833bc9 2019-05-13 stsp return err;
627 a9833bc9 2019-05-13 stsp }