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 fe8df4c2 2018-06-16 stsp /* Ensure that an iteration in progress will see this new commit. */
156 bee6b577 2018-09-19 stsp while (n) {
157 bee6b577 2018-09-19 stsp next = TAILQ_NEXT(n, entry);
158 73026088 2018-11-18 stsp if (next && node->timestamp >= next->timestamp) {
159 bee6b577 2018-09-19 stsp TAILQ_INSERT_BEFORE(next, node, entry);
160 bee6b577 2018-09-19 stsp return;
161 fe8df4c2 2018-06-16 stsp }
162 bee6b577 2018-09-19 stsp n = next;
163 fe8df4c2 2018-06-16 stsp }
164 93e45b7c 2018-09-24 stsp TAILQ_INSERT_TAIL(&graph->iter_list, node, entry);
165 0ed6ed4c 2018-06-13 stsp }
166 0ed6ed4c 2018-06-13 stsp
167 0ed6ed4c 2018-06-13 stsp static const struct got_error *
168 31cedeaf 2018-09-15 stsp close_branch(struct got_commit_graph *graph, struct got_object_id *commit_id)
169 31cedeaf 2018-09-15 stsp {
170 31cedeaf 2018-09-15 stsp const struct got_error *err;
171 31cedeaf 2018-09-15 stsp
172 31cedeaf 2018-09-15 stsp err = got_object_idset_remove(NULL, graph->open_branches, commit_id);
173 31cedeaf 2018-09-15 stsp if (err && err->code != GOT_ERR_NO_OBJ)
174 31cedeaf 2018-09-15 stsp return err;
175 31cedeaf 2018-09-15 stsp return NULL;
176 31cedeaf 2018-09-15 stsp }
177 31cedeaf 2018-09-15 stsp
178 31cedeaf 2018-09-15 stsp static const struct got_error *
179 14159a7b 2020-01-04 stsp advance_branch(struct got_commit_graph *graph, struct got_object_id *commit_id,
180 14159a7b 2020-01-04 stsp struct got_commit_object *commit, struct got_repository *repo)
181 0ed6ed4c 2018-06-13 stsp {
182 0ed6ed4c 2018-06-13 stsp const struct got_error *err;
183 0ed6ed4c 2018-06-13 stsp struct got_object_qid *qid;
184 0ed6ed4c 2018-06-13 stsp
185 31cedeaf 2018-09-15 stsp err = close_branch(graph, commit_id);
186 31cedeaf 2018-09-15 stsp if (err)
187 0ed6ed4c 2018-06-13 stsp return err;
188 0ed6ed4c 2018-06-13 stsp
189 0ed6ed4c 2018-06-13 stsp if (graph->flags & GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL) {
190 0ed6ed4c 2018-06-13 stsp qid = SIMPLEQ_FIRST(&commit->parent_ids);
191 9b88e78c 2018-11-18 stsp if (qid == NULL ||
192 8e291695 2020-01-04 stsp got_object_idset_contains(graph->open_branches, qid->id))
193 0ed6ed4c 2018-06-13 stsp return NULL;
194 9b88e78c 2018-11-18 stsp return got_object_idset_add(graph->open_branches,
195 8e291695 2020-01-04 stsp qid->id, NULL);
196 bee6b577 2018-09-19 stsp }
197 bee6b577 2018-09-19 stsp
198 bee6b577 2018-09-19 stsp /*
199 bee6b577 2018-09-19 stsp * If we are graphing commits for a specific path, skip branches
200 bee6b577 2018-09-19 stsp * which do not contribute any content to this path.
201 bee6b577 2018-09-19 stsp */
202 7a62478b 2018-11-18 stsp if (commit->nparents > 1 && !got_path_is_root_dir(graph->path)) {
203 6dcdad08 2018-11-18 stsp struct got_object_id *merged_id, *prev_id = NULL;
204 bee6b577 2018-09-19 stsp int branches_differ = 0;
205 bee6b577 2018-09-19 stsp
206 bee6b577 2018-09-19 stsp err = got_object_id_by_path(&merged_id, repo, commit_id,
207 bee6b577 2018-09-19 stsp graph->path);
208 bee6b577 2018-09-19 stsp if (err)
209 bee6b577 2018-09-19 stsp return err;
210 bee6b577 2018-09-19 stsp
211 bee6b577 2018-09-19 stsp SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
212 6dcdad08 2018-11-18 stsp struct got_object_id *id;
213 6dcdad08 2018-11-18 stsp
214 8e291695 2020-01-04 stsp if (got_object_idset_contains(graph->open_branches,
215 8e291695 2020-01-04 stsp qid->id))
216 9b88e78c 2018-11-18 stsp continue;
217 bee6b577 2018-09-19 stsp
218 bee6b577 2018-09-19 stsp err = got_object_id_by_path(&id, repo, qid->id,
219 bee6b577 2018-09-19 stsp graph->path);
220 bee6b577 2018-09-19 stsp if (err) {
221 d1451975 2018-11-11 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY) {
222 bee6b577 2018-09-19 stsp branches_differ = 1;
223 bee6b577 2018-09-19 stsp continue;
224 bee6b577 2018-09-19 stsp }
225 6dcdad08 2018-11-18 stsp free(merged_id);
226 6dcdad08 2018-11-18 stsp free(prev_id);
227 bee6b577 2018-09-19 stsp return err;
228 bee6b577 2018-09-19 stsp }
229 bee6b577 2018-09-19 stsp
230 bee6b577 2018-09-19 stsp if (prev_id) {
231 bee6b577 2018-09-19 stsp if (!branches_differ &&
232 d2c2d781 2018-11-18 stsp got_object_id_cmp(id, prev_id) != 0)
233 bee6b577 2018-09-19 stsp branches_differ = 1;
234 6dcdad08 2018-11-18 stsp free(prev_id);
235 6dcdad08 2018-11-18 stsp }
236 6dcdad08 2018-11-18 stsp prev_id = id;
237 bee6b577 2018-09-19 stsp
238 bee6b577 2018-09-19 stsp /*
239 bee6b577 2018-09-19 stsp * If a branch has created the merged content we can
240 bee6b577 2018-09-19 stsp * skip any other branches.
241 bee6b577 2018-09-19 stsp */
242 bee6b577 2018-09-19 stsp if (got_object_id_cmp(merged_id, id) == 0) {
243 b36429ab 2018-11-05 stsp err = got_object_idset_add(graph->open_branches,
244 8e291695 2020-01-04 stsp qid->id, NULL);
245 6dcdad08 2018-11-18 stsp free(merged_id);
246 6dcdad08 2018-11-18 stsp free(id);
247 b36429ab 2018-11-05 stsp return err;
248 bee6b577 2018-09-19 stsp }
249 bee6b577 2018-09-19 stsp }
250 6dcdad08 2018-11-18 stsp
251 6dcdad08 2018-11-18 stsp free(prev_id);
252 6dcdad08 2018-11-18 stsp prev_id = NULL;
253 6dcdad08 2018-11-18 stsp free(merged_id);
254 6dcdad08 2018-11-18 stsp merged_id = NULL;
255 bee6b577 2018-09-19 stsp
256 bee6b577 2018-09-19 stsp /*
257 bee6b577 2018-09-19 stsp * If the path's content is the same on all branches,
258 bee6b577 2018-09-19 stsp * follow the first parent only.
259 bee6b577 2018-09-19 stsp */
260 bee6b577 2018-09-19 stsp if (!branches_differ) {
261 bee6b577 2018-09-19 stsp qid = SIMPLEQ_FIRST(&commit->parent_ids);
262 bee6b577 2018-09-19 stsp if (qid == NULL)
263 bee6b577 2018-09-19 stsp return NULL;
264 8e291695 2020-01-04 stsp if (got_object_idset_contains(graph->open_branches,
265 8e291695 2020-01-04 stsp qid->id))
266 b36429ab 2018-11-05 stsp return NULL;
267 8e291695 2020-01-04 stsp if (got_object_idset_contains(graph->node_ids,
268 8e291695 2020-01-04 stsp qid->id))
269 998ff57f 2018-11-18 stsp return NULL; /* parent already traversed */
270 b36429ab 2018-11-05 stsp return got_object_idset_add(graph->open_branches,
271 8e291695 2020-01-04 stsp qid->id, NULL);
272 bee6b577 2018-09-19 stsp }
273 0ed6ed4c 2018-06-13 stsp }
274 0ed6ed4c 2018-06-13 stsp
275 0ed6ed4c 2018-06-13 stsp SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
276 8e291695 2020-01-04 stsp if (got_object_idset_contains(graph->open_branches, qid->id))
277 b36429ab 2018-11-05 stsp continue;
278 8e291695 2020-01-04 stsp if (got_object_idset_contains(graph->node_ids, qid->id))
279 998ff57f 2018-11-18 stsp continue; /* parent already traversed */
280 8e291695 2020-01-04 stsp err = got_object_idset_add(graph->open_branches, qid->id, NULL);
281 b36429ab 2018-11-05 stsp if (err)
282 0ed6ed4c 2018-06-13 stsp return err;
283 0ed6ed4c 2018-06-13 stsp }
284 0ed6ed4c 2018-06-13 stsp
285 b43fbaa0 2018-06-11 stsp return NULL;
286 fb5b9d5a 2018-07-23 stsp }
287 fb5b9d5a 2018-07-23 stsp
288 b43fbaa0 2018-06-11 stsp static const struct got_error *
289 de56b2d7 2020-01-04 stsp add_node(struct got_commit_graph_node **new_node,
290 de56b2d7 2020-01-04 stsp struct got_commit_graph *graph,
291 de56b2d7 2020-01-04 stsp struct got_object_id *commit_id,
292 de56b2d7 2020-01-04 stsp struct got_commit_object *commit,
293 de56b2d7 2020-01-04 stsp struct got_repository *repo)
294 372ccdbb 2018-06-10 stsp {
295 372ccdbb 2018-06-10 stsp const struct got_error *err = NULL;
296 f4ceb45e 2018-07-23 stsp struct got_commit_graph_node *node;
297 372ccdbb 2018-06-10 stsp
298 372ccdbb 2018-06-10 stsp *new_node = NULL;
299 372ccdbb 2018-06-10 stsp
300 372ccdbb 2018-06-10 stsp node = calloc(1, sizeof(*node));
301 372ccdbb 2018-06-10 stsp if (node == NULL)
302 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
303 372ccdbb 2018-06-10 stsp
304 372ccdbb 2018-06-10 stsp memcpy(&node->id, commit_id, sizeof(node->id));
305 73026088 2018-11-18 stsp node->timestamp = commit->committer_time;
306 372ccdbb 2018-06-10 stsp
307 8e291695 2020-01-04 stsp err = got_object_idset_add(graph->node_ids, &node->id, NULL);
308 de56b2d7 2020-01-04 stsp if (err)
309 7a62478b 2018-11-18 stsp free(node);
310 de56b2d7 2020-01-04 stsp else
311 de56b2d7 2020-01-04 stsp *new_node = node;
312 de56b2d7 2020-01-04 stsp return err;
313 de56b2d7 2020-01-04 stsp }
314 372ccdbb 2018-06-10 stsp
315 de56b2d7 2020-01-04 stsp const struct got_error *
316 c4d7a9c4 2018-06-11 stsp got_commit_graph_open(struct got_commit_graph **graph,
317 3d509237 2020-01-04 stsp const char *path, int first_parent_traversal)
318 3d509237 2020-01-04 stsp {
319 22220781 2020-01-04 stsp const struct got_error *err = NULL;
320 3ddcebf3 2020-01-04 stsp
321 3ddcebf3 2020-01-04 stsp *graph = calloc(1, sizeof(**graph));
322 3d509237 2020-01-04 stsp if (*graph == NULL)
323 3ddcebf3 2020-01-04 stsp return got_error_from_errno("calloc");
324 88cdb9c6 2020-01-04 stsp
325 88cdb9c6 2020-01-04 stsp TAILQ_INIT(&(*graph)->iter_list);
326 3ddcebf3 2020-01-04 stsp
327 3ddcebf3 2020-01-04 stsp (*graph)->path = strdup(path);
328 3ddcebf3 2020-01-04 stsp if ((*graph)->path == NULL) {
329 3ddcebf3 2020-01-04 stsp err = got_error_from_errno("strdup");
330 22220781 2020-01-04 stsp goto done;
331 3ddcebf3 2020-01-04 stsp }
332 3ddcebf3 2020-01-04 stsp
333 3ddcebf3 2020-01-04 stsp (*graph)->node_ids = got_object_idset_alloc();
334 3ddcebf3 2020-01-04 stsp if ((*graph)->node_ids == NULL) {
335 3ddcebf3 2020-01-04 stsp err = got_error_from_errno("got_object_idset_alloc");
336 22220781 2020-01-04 stsp goto done;
337 3ddcebf3 2020-01-04 stsp }
338 372ccdbb 2018-06-10 stsp
339 3ddcebf3 2020-01-04 stsp (*graph)->open_branches = got_object_idset_alloc();
340 3ddcebf3 2020-01-04 stsp if ((*graph)->open_branches == NULL) {
341 3ddcebf3 2020-01-04 stsp err = got_error_from_errno("got_object_idset_alloc");
342 22220781 2020-01-04 stsp goto done;
343 3ddcebf3 2020-01-04 stsp }
344 3ddcebf3 2020-01-04 stsp
345 0ed6ed4c 2018-06-13 stsp if (first_parent_traversal)
346 0ed6ed4c 2018-06-13 stsp (*graph)->flags |= GOT_COMMIT_GRAPH_FIRST_PARENT_TRAVERSAL;
347 22220781 2020-01-04 stsp done:
348 22220781 2020-01-04 stsp if (err) {
349 22220781 2020-01-04 stsp got_commit_graph_close(*graph);
350 22220781 2020-01-04 stsp *graph = NULL;
351 22220781 2020-01-04 stsp }
352 22220781 2020-01-04 stsp return err;
353 372ccdbb 2018-06-10 stsp }
354 372ccdbb 2018-06-10 stsp
355 32777563 2018-11-07 stsp struct add_branch_tip_arg {
356 b565f6f8 2018-07-23 stsp struct got_commit_graph_branch_tip *tips;
357 b565f6f8 2018-07-23 stsp int ntips;
358 32777563 2018-11-07 stsp struct got_repository *repo;
359 32777563 2018-11-07 stsp struct got_commit_graph *graph;
360 372ccdbb 2018-06-10 stsp };
361 372ccdbb 2018-06-10 stsp
362 cb103d04 2018-11-07 stsp static const struct got_error *
363 32777563 2018-11-07 stsp add_branch_tip(struct got_object_id *commit_id, void *data, void *arg)
364 372ccdbb 2018-06-10 stsp {
365 32777563 2018-11-07 stsp const struct got_error *err;
366 32777563 2018-11-07 stsp struct add_branch_tip_arg *a = arg;
367 32777563 2018-11-07 stsp struct got_commit_graph_node *new_node;
368 32777563 2018-11-07 stsp struct got_commit_object *commit;
369 32777563 2018-11-07 stsp
370 32777563 2018-11-07 stsp err = got_object_open_as_commit(&commit, a->repo, commit_id);
371 32777563 2018-11-07 stsp if (err)
372 32777563 2018-11-07 stsp return err;
373 32777563 2018-11-07 stsp
374 de56b2d7 2020-01-04 stsp err = add_node(&new_node, a->graph, commit_id, commit, a->repo);
375 32777563 2018-11-07 stsp if (err)
376 32777563 2018-11-07 stsp return err;
377 32777563 2018-11-07 stsp
378 32777563 2018-11-07 stsp a->tips[a->ntips].commit_id = new_node ? &new_node->id : NULL;
379 32777563 2018-11-07 stsp a->tips[a->ntips].commit = commit;
380 32777563 2018-11-07 stsp a->tips[a->ntips].new_node = new_node;
381 b565f6f8 2018-07-23 stsp a->ntips++;
382 32777563 2018-11-07 stsp
383 cb103d04 2018-11-07 stsp return NULL;
384 372ccdbb 2018-06-10 stsp }
385 372ccdbb 2018-06-10 stsp
386 1142eae9 2018-06-11 stsp static const struct got_error *
387 57eecd46 2020-01-04 stsp fetch_commits_from_open_branches(struct got_commit_graph *graph,
388 6fb7cd11 2019-08-22 stsp struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
389 372ccdbb 2018-06-10 stsp {
390 372ccdbb 2018-06-10 stsp const struct got_error *err;
391 32777563 2018-11-07 stsp struct add_branch_tip_arg arg;
392 32777563 2018-11-07 stsp int i, ntips;
393 372ccdbb 2018-06-10 stsp
394 32777563 2018-11-07 stsp ntips = got_object_idset_num_elements(graph->open_branches);
395 32777563 2018-11-07 stsp if (ntips == 0)
396 372ccdbb 2018-06-10 stsp return NULL;
397 372ccdbb 2018-06-10 stsp
398 32777563 2018-11-07 stsp /* (Re-)allocate branch tips array if necessary. */
399 32777563 2018-11-07 stsp if (graph->ntips < ntips) {
400 b565f6f8 2018-07-23 stsp struct got_commit_graph_branch_tip *tips;
401 5e50c36a 2018-11-08 stsp tips = recallocarray(graph->tips, graph->ntips, ntips,
402 5e50c36a 2018-11-08 stsp sizeof(*tips));
403 b565f6f8 2018-07-23 stsp if (tips == NULL)
404 638f9024 2019-05-13 stsp return got_error_from_errno("recallocarray");
405 b565f6f8 2018-07-23 stsp graph->tips = tips;
406 32777563 2018-11-07 stsp graph->ntips = ntips;
407 b565f6f8 2018-07-23 stsp }
408 b565f6f8 2018-07-23 stsp arg.tips = graph->tips;
409 32777563 2018-11-07 stsp arg.ntips = 0; /* add_branch_tip() will increment */
410 32777563 2018-11-07 stsp arg.repo = repo;
411 32777563 2018-11-07 stsp arg.graph = graph;
412 32777563 2018-11-07 stsp err = got_object_idset_for_each(graph->open_branches, add_branch_tip,
413 32777563 2018-11-07 stsp &arg);
414 cb103d04 2018-11-07 stsp if (err)
415 32777563 2018-11-07 stsp goto done;
416 372ccdbb 2018-06-10 stsp
417 b565f6f8 2018-07-23 stsp for (i = 0; i < arg.ntips; i++) {
418 372ccdbb 2018-06-10 stsp struct got_object_id *commit_id;
419 41fa1437 2018-11-05 stsp struct got_commit_object *commit;
420 32777563 2018-11-07 stsp struct got_commit_graph_node *new_node;
421 13a851c1 2020-01-04 stsp int changed;
422 372ccdbb 2018-06-10 stsp
423 6fb7cd11 2019-08-22 stsp if (cancel_cb) {
424 6fb7cd11 2019-08-22 stsp err = (*cancel_cb)(cancel_arg);
425 6fb7cd11 2019-08-22 stsp if (err)
426 6fb7cd11 2019-08-22 stsp break;
427 6fb7cd11 2019-08-22 stsp }
428 6fb7cd11 2019-08-22 stsp
429 32777563 2018-11-07 stsp commit_id = arg.tips[i].commit_id;
430 32777563 2018-11-07 stsp commit = arg.tips[i].commit;
431 32777563 2018-11-07 stsp new_node = arg.tips[i].new_node;
432 de56b2d7 2020-01-04 stsp
433 13a851c1 2020-01-04 stsp err = detect_changed_path(&changed, commit, commit_id,
434 13a851c1 2020-01-04 stsp graph->path, repo);
435 13a851c1 2020-01-04 stsp if (err) {
436 13a851c1 2020-01-04 stsp if (err->code != GOT_ERR_NO_OBJ)
437 13a851c1 2020-01-04 stsp break;
438 13a851c1 2020-01-04 stsp /*
439 13a851c1 2020-01-04 stsp * History of the path stops here on the current
440 13a851c1 2020-01-04 stsp * branch. Keep going on other branches.
441 13a851c1 2020-01-04 stsp */
442 13a851c1 2020-01-04 stsp err = close_branch(graph, commit_id);
443 ec1904dc 2020-01-04 stsp if (err)
444 ec1904dc 2020-01-04 stsp break;
445 ec1904dc 2020-01-04 stsp continue;
446 13a851c1 2020-01-04 stsp }
447 57eecd46 2020-01-04 stsp if (changed)
448 de56b2d7 2020-01-04 stsp add_node_to_iter_list(graph, new_node);
449 14159a7b 2020-01-04 stsp err = advance_branch(graph, commit_id, commit, repo);
450 cb352812 2018-07-22 stsp if (err)
451 cb352812 2018-07-22 stsp break;
452 372ccdbb 2018-06-10 stsp }
453 32777563 2018-11-07 stsp done:
454 246e1c78 2018-11-08 stsp for (i = 0; i < arg.ntips; i++)
455 32777563 2018-11-07 stsp got_object_commit_close(arg.tips[i].commit);
456 372ccdbb 2018-06-10 stsp return err;
457 372ccdbb 2018-06-10 stsp }
458 372ccdbb 2018-06-10 stsp
459 372ccdbb 2018-06-10 stsp void
460 372ccdbb 2018-06-10 stsp got_commit_graph_close(struct got_commit_graph *graph)
461 372ccdbb 2018-06-10 stsp {
462 22220781 2020-01-04 stsp if (graph->open_branches)
463 22220781 2020-01-04 stsp got_object_idset_free(graph->open_branches);
464 22220781 2020-01-04 stsp if (graph->node_ids)
465 22220781 2020-01-04 stsp got_object_idset_free(graph->node_ids);
466 b565f6f8 2018-07-23 stsp free(graph->tips);
467 31cedeaf 2018-09-15 stsp free(graph->path);
468 372ccdbb 2018-06-10 stsp free(graph);
469 372ccdbb 2018-06-10 stsp }
470 372ccdbb 2018-06-10 stsp
471 372ccdbb 2018-06-10 stsp const struct got_error *
472 372ccdbb 2018-06-10 stsp got_commit_graph_iter_start(struct got_commit_graph *graph,
473 6fb7cd11 2019-08-22 stsp struct got_object_id *id, struct got_repository *repo,
474 6fb7cd11 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
475 372ccdbb 2018-06-10 stsp {
476 31cedeaf 2018-09-15 stsp const struct got_error *err = NULL;
477 372ccdbb 2018-06-10 stsp
478 3d509237 2020-01-04 stsp if (!TAILQ_EMPTY(&graph->iter_list))
479 3d509237 2020-01-04 stsp return got_error(GOT_ERR_ITER_BUSY);
480 31cedeaf 2018-09-15 stsp
481 3ff3126d 2020-01-04 stsp err = got_object_idset_add(graph->open_branches, id, NULL);
482 3d509237 2020-01-04 stsp if (err)
483 7e33c8c5 2020-01-04 stsp return err;
484 3d509237 2020-01-04 stsp
485 3ff3126d 2020-01-04 stsp /* Locate first commit which changed graph->path. */
486 94489f7d 2020-01-04 stsp while (TAILQ_EMPTY(&graph->iter_list) &&
487 3ff3126d 2020-01-04 stsp got_object_idset_num_elements(graph->open_branches) > 0) {
488 3ff3126d 2020-01-04 stsp err = fetch_commits_from_open_branches(graph, repo,
489 3ff3126d 2020-01-04 stsp cancel_cb, cancel_arg);
490 3ff3126d 2020-01-04 stsp if (err)
491 7e33c8c5 2020-01-04 stsp return err;
492 5175b31a 2020-01-04 stsp }
493 5175b31a 2020-01-04 stsp
494 94489f7d 2020-01-04 stsp if (TAILQ_EMPTY(&graph->iter_list)) {
495 5175b31a 2020-01-04 stsp const char *path;
496 5175b31a 2020-01-04 stsp if (got_path_is_root_dir(graph->path))
497 5175b31a 2020-01-04 stsp return got_error_no_obj(id);
498 5175b31a 2020-01-04 stsp path = graph->path;
499 5175b31a 2020-01-04 stsp while (path[0] == '/')
500 5175b31a 2020-01-04 stsp path++;
501 5175b31a 2020-01-04 stsp return got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
502 31cedeaf 2018-09-15 stsp }
503 7e33c8c5 2020-01-04 stsp
504 7e33c8c5 2020-01-04 stsp return NULL;
505 372ccdbb 2018-06-10 stsp }
506 372ccdbb 2018-06-10 stsp
507 372ccdbb 2018-06-10 stsp const struct got_error *
508 b43fbaa0 2018-06-11 stsp got_commit_graph_iter_next(struct got_object_id **id,
509 ee780d5c 2020-01-04 stsp struct got_commit_graph *graph, struct got_repository *repo,
510 ee780d5c 2020-01-04 stsp got_cancel_cb cancel_cb, void *cancel_arg)
511 372ccdbb 2018-06-10 stsp {
512 ee780d5c 2020-01-04 stsp const struct got_error *err = NULL;
513 94489f7d 2020-01-04 stsp struct got_commit_graph_node *node;
514 ee780d5c 2020-01-04 stsp
515 9ba79e04 2018-06-11 stsp *id = NULL;
516 372ccdbb 2018-06-10 stsp
517 94489f7d 2020-01-04 stsp if (TAILQ_EMPTY(&graph->iter_list)) {
518 2c7f8870 2018-09-19 stsp /* We are done iterating, or iteration was not started. */
519 9ba79e04 2018-06-11 stsp return got_error(GOT_ERR_ITER_COMPLETED);
520 9ba79e04 2018-06-11 stsp }
521 9ba79e04 2018-06-11 stsp
522 94489f7d 2020-01-04 stsp node = TAILQ_FIRST(&graph->iter_list);
523 94489f7d 2020-01-04 stsp while (TAILQ_NEXT(node, entry) == NULL &&
524 ee780d5c 2020-01-04 stsp got_object_idset_num_elements(graph->open_branches) > 0) {
525 57eecd46 2020-01-04 stsp err = fetch_commits_from_open_branches(graph, repo,
526 57eecd46 2020-01-04 stsp cancel_cb, cancel_arg);
527 ee780d5c 2020-01-04 stsp if (err)
528 ee780d5c 2020-01-04 stsp return err;
529 ee780d5c 2020-01-04 stsp }
530 372ccdbb 2018-06-10 stsp
531 94489f7d 2020-01-04 stsp *id = &node->id;
532 94489f7d 2020-01-04 stsp TAILQ_REMOVE(&graph->iter_list, node, entry);
533 372ccdbb 2018-06-10 stsp return NULL;
534 372ccdbb 2018-06-10 stsp }
535 a9833bc9 2019-05-13 stsp
536 a9833bc9 2019-05-13 stsp const struct got_error *
537 a9833bc9 2019-05-13 stsp got_commit_graph_find_youngest_common_ancestor(struct got_object_id **yca_id,
538 a9833bc9 2019-05-13 stsp struct got_object_id *commit_id, struct got_object_id *commit_id2,
539 6fb7cd11 2019-08-22 stsp struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
540 a9833bc9 2019-05-13 stsp {
541 a9833bc9 2019-05-13 stsp const struct got_error *err = NULL;
542 a9833bc9 2019-05-13 stsp struct got_commit_graph *graph = NULL, *graph2 = NULL;
543 a9833bc9 2019-05-13 stsp int completed = 0, completed2 = 0;
544 a9833bc9 2019-05-13 stsp struct got_object_idset *commit_ids;
545 a9833bc9 2019-05-13 stsp
546 a9833bc9 2019-05-13 stsp *yca_id = NULL;
547 a9833bc9 2019-05-13 stsp
548 a9833bc9 2019-05-13 stsp commit_ids = got_object_idset_alloc();
549 a9833bc9 2019-05-13 stsp if (commit_ids == NULL)
550 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_idset_alloc");
551 a9833bc9 2019-05-13 stsp
552 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
553 a9833bc9 2019-05-13 stsp if (err)
554 a9833bc9 2019-05-13 stsp goto done;
555 a9833bc9 2019-05-13 stsp
556 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph2, "/", 1);
557 a9833bc9 2019-05-13 stsp if (err)
558 a9833bc9 2019-05-13 stsp goto done;
559 a9833bc9 2019-05-13 stsp
560 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, commit_id, repo,
561 6fb7cd11 2019-08-22 stsp cancel_cb, cancel_arg);
562 a9833bc9 2019-05-13 stsp if (err)
563 a9833bc9 2019-05-13 stsp goto done;
564 a9833bc9 2019-05-13 stsp
565 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph2, commit_id2, repo,
566 6fb7cd11 2019-08-22 stsp cancel_cb, cancel_arg);
567 a9833bc9 2019-05-13 stsp if (err)
568 a9833bc9 2019-05-13 stsp goto done;
569 a9833bc9 2019-05-13 stsp
570 a9833bc9 2019-05-13 stsp for (;;) {
571 ee780d5c 2020-01-04 stsp struct got_object_id *id = NULL, *id2 = NULL;
572 a9833bc9 2019-05-13 stsp
573 6fb7cd11 2019-08-22 stsp if (cancel_cb) {
574 6fb7cd11 2019-08-22 stsp err = (*cancel_cb)(cancel_arg);
575 6fb7cd11 2019-08-22 stsp if (err)
576 6fb7cd11 2019-08-22 stsp break;
577 6fb7cd11 2019-08-22 stsp }
578 6fb7cd11 2019-08-22 stsp
579 a9833bc9 2019-05-13 stsp if (!completed) {
580 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
581 ee780d5c 2020-01-04 stsp cancel_cb, cancel_arg);
582 a9833bc9 2019-05-13 stsp if (err) {
583 ee780d5c 2020-01-04 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
584 a9833bc9 2019-05-13 stsp break;
585 ee780d5c 2020-01-04 stsp err = NULL;
586 ee780d5c 2020-01-04 stsp completed = 1;
587 a9833bc9 2019-05-13 stsp }
588 a9833bc9 2019-05-13 stsp }
589 a9833bc9 2019-05-13 stsp
590 a9833bc9 2019-05-13 stsp if (!completed2) {
591 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id2, graph2, repo,
592 ee780d5c 2020-01-04 stsp cancel_cb, cancel_arg);
593 a9833bc9 2019-05-13 stsp if (err) {
594 ee780d5c 2020-01-04 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
595 a9833bc9 2019-05-13 stsp break;
596 ee780d5c 2020-01-04 stsp err = NULL;
597 ee780d5c 2020-01-04 stsp completed2 = 1;
598 a9833bc9 2019-05-13 stsp }
599 a9833bc9 2019-05-13 stsp }
600 a9833bc9 2019-05-13 stsp
601 a9833bc9 2019-05-13 stsp if (id) {
602 8e291695 2020-01-04 stsp if (got_object_idset_contains(commit_ids, id)) {
603 a9833bc9 2019-05-13 stsp *yca_id = got_object_id_dup(id);
604 a9833bc9 2019-05-13 stsp if (*yca_id)
605 a9833bc9 2019-05-13 stsp break;
606 82751bc5 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
607 a9833bc9 2019-05-13 stsp break;
608 a9833bc9 2019-05-13 stsp
609 a9833bc9 2019-05-13 stsp }
610 8e291695 2020-01-04 stsp err = got_object_idset_add(commit_ids, id, NULL);
611 a9833bc9 2019-05-13 stsp if (err)
612 a9833bc9 2019-05-13 stsp break;
613 a9833bc9 2019-05-13 stsp }
614 a9833bc9 2019-05-13 stsp if (id2) {
615 8e291695 2020-01-04 stsp if (got_object_idset_contains(commit_ids, id2)) {
616 a9833bc9 2019-05-13 stsp *yca_id = got_object_id_dup(id2);
617 a9833bc9 2019-05-13 stsp if (*yca_id)
618 a9833bc9 2019-05-13 stsp break;
619 82751bc5 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
620 a9833bc9 2019-05-13 stsp break;
621 a9833bc9 2019-05-13 stsp
622 a9833bc9 2019-05-13 stsp }
623 8e291695 2020-01-04 stsp err = got_object_idset_add(commit_ids, id2, NULL);
624 a9833bc9 2019-05-13 stsp if (err)
625 a9833bc9 2019-05-13 stsp break;
626 a9833bc9 2019-05-13 stsp }
627 a9833bc9 2019-05-13 stsp
628 a9833bc9 2019-05-13 stsp if (completed && completed2) {
629 a9833bc9 2019-05-13 stsp err = got_error(GOT_ERR_ANCESTRY);
630 a9833bc9 2019-05-13 stsp break;
631 a9833bc9 2019-05-13 stsp }
632 a9833bc9 2019-05-13 stsp
633 a9833bc9 2019-05-13 stsp }
634 a9833bc9 2019-05-13 stsp done:
635 a9833bc9 2019-05-13 stsp got_object_idset_free(commit_ids);
636 a9833bc9 2019-05-13 stsp if (graph)
637 a9833bc9 2019-05-13 stsp got_commit_graph_close(graph);
638 a9833bc9 2019-05-13 stsp if (graph2)
639 a9833bc9 2019-05-13 stsp got_commit_graph_close(graph2);
640 a9833bc9 2019-05-13 stsp return err;
641 a9833bc9 2019-05-13 stsp }