Blame


1 7b19e0f1 2017-11-05 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 7b19e0f1 2017-11-05 stsp *
4 7b19e0f1 2017-11-05 stsp * Permission to use, copy, modify, and distribute this software for any
5 7b19e0f1 2017-11-05 stsp * purpose with or without fee is hereby granted, provided that the above
6 7b19e0f1 2017-11-05 stsp * copyright notice and this permission notice appear in all copies.
7 7b19e0f1 2017-11-05 stsp *
8 7b19e0f1 2017-11-05 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 7b19e0f1 2017-11-05 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 7b19e0f1 2017-11-05 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 7b19e0f1 2017-11-05 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 7b19e0f1 2017-11-05 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 7b19e0f1 2017-11-05 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 7b19e0f1 2017-11-05 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 7b19e0f1 2017-11-05 stsp */
16 7b19e0f1 2017-11-05 stsp
17 ad242220 2018-09-08 stsp #include <sys/types.h>
18 79b11c62 2018-03-09 stsp #include <sys/queue.h>
19 ad242220 2018-09-08 stsp #include <sys/uio.h>
20 aba9c984 2019-09-08 stsp #include <sys/socket.h>
21 deeca238 2018-03-12 stsp #include <sys/stat.h>
22 1510f469 2018-09-09 stsp #include <sys/mman.h>
23 79b11c62 2018-03-09 stsp
24 e09a504c 2019-06-28 stsp #include <ctype.h>
25 78fb0967 2020-09-09 naddy #include <endian.h>
26 1510f469 2018-09-09 stsp #include <fcntl.h>
27 3ce1b845 2019-07-15 stsp #include <fnmatch.h>
28 4027f31a 2017-11-04 stsp #include <limits.h>
29 1510f469 2018-09-09 stsp #include <dirent.h>
30 4027f31a 2017-11-04 stsp #include <stdlib.h>
31 4027f31a 2017-11-04 stsp #include <stdio.h>
32 4027f31a 2017-11-04 stsp #include <sha1.h>
33 4027f31a 2017-11-04 stsp #include <string.h>
34 303e14b5 2019-09-23 stsp #include <time.h>
35 81a12da5 2020-09-09 naddy #include <unistd.h>
36 79b11c62 2018-03-09 stsp #include <zlib.h>
37 85f51bba 2018-07-16 stsp #include <errno.h>
38 85f51bba 2018-07-16 stsp #include <libgen.h>
39 ad242220 2018-09-08 stsp #include <stdint.h>
40 ad242220 2018-09-08 stsp #include <imsg.h>
41 c442a90d 2019-03-10 stsp #include <uuid.h>
42 4027f31a 2017-11-04 stsp
43 4027f31a 2017-11-04 stsp #include "got_error.h"
44 5261c201 2018-04-01 stsp #include "got_reference.h"
45 4027f31a 2017-11-04 stsp #include "got_repository.h"
46 1dd54920 2019-05-11 stsp #include "got_path.h"
47 e6209546 2019-08-22 stsp #include "got_cancel.h"
48 442a3ddc 2018-04-23 stsp #include "got_worktree.h"
49 7bb0daa1 2018-06-21 stsp #include "got_object.h"
50 4027f31a 2017-11-04 stsp
51 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
52 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
53 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
54 3ce1b845 2019-07-15 stsp #include "got_lib_object_parse.h"
55 3ce1b845 2019-07-15 stsp #include "got_lib_object_create.h"
56 718b3ab0 2018-03-17 stsp #include "got_lib_pack.h"
57 876c234b 2018-09-10 stsp #include "got_lib_privsep.h"
58 442a3ddc 2018-04-23 stsp #include "got_lib_worktree.h"
59 e09a504c 2019-06-28 stsp #include "got_lib_sha1.h"
60 6bef87be 2018-09-11 stsp #include "got_lib_object_cache.h"
61 6bef87be 2018-09-11 stsp #include "got_lib_repository.h"
62 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
63 c3f94f68 2017-11-05 stsp
64 79b11c62 2018-03-09 stsp #ifndef nitems
65 79b11c62 2018-03-09 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
66 79b11c62 2018-03-09 stsp #endif
67 4df642d9 2017-11-05 stsp
68 7839bc15 2019-01-06 stsp const char *
69 86c3caaf 2018-03-09 stsp got_repo_get_path(struct got_repository *repo)
70 86c3caaf 2018-03-09 stsp {
71 7839bc15 2019-01-06 stsp return repo->path;
72 86c3caaf 2018-03-09 stsp }
73 86c3caaf 2018-03-09 stsp
74 6e9da951 2019-01-06 stsp const char *
75 11995603 2017-11-05 stsp got_repo_get_path_git_dir(struct got_repository *repo)
76 4027f31a 2017-11-04 stsp {
77 6e9da951 2019-01-06 stsp return repo->path_git_dir;
78 04ca23f4 2018-07-16 stsp }
79 04ca23f4 2018-07-16 stsp
80 aba9c984 2019-09-08 stsp const char *
81 aba9c984 2019-09-08 stsp got_repo_get_gitconfig_author_name(struct got_repository *repo)
82 aba9c984 2019-09-08 stsp {
83 aba9c984 2019-09-08 stsp return repo->gitconfig_author_name;
84 aba9c984 2019-09-08 stsp }
85 aba9c984 2019-09-08 stsp
86 aba9c984 2019-09-08 stsp const char *
87 aba9c984 2019-09-08 stsp got_repo_get_gitconfig_author_email(struct got_repository *repo)
88 aba9c984 2019-09-08 stsp {
89 aba9c984 2019-09-08 stsp return repo->gitconfig_author_email;
90 c9956ddf 2019-09-08 stsp }
91 c9956ddf 2019-09-08 stsp
92 c9956ddf 2019-09-08 stsp const char *
93 c9956ddf 2019-09-08 stsp got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
94 c9956ddf 2019-09-08 stsp {
95 c9956ddf 2019-09-08 stsp return repo->global_gitconfig_author_name;
96 c9956ddf 2019-09-08 stsp }
97 c9956ddf 2019-09-08 stsp
98 c9956ddf 2019-09-08 stsp const char *
99 c9956ddf 2019-09-08 stsp got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
100 c9956ddf 2019-09-08 stsp {
101 c9956ddf 2019-09-08 stsp return repo->global_gitconfig_author_email;
102 9a1cc63f 2020-02-03 stsp }
103 9a1cc63f 2020-02-03 stsp
104 9a1cc63f 2020-02-03 stsp const char *
105 9a1cc63f 2020-02-03 stsp got_repo_get_gitconfig_owner(struct got_repository *repo)
106 9a1cc63f 2020-02-03 stsp {
107 9a1cc63f 2020-02-03 stsp return repo->gitconfig_owner;
108 aba9c984 2019-09-08 stsp }
109 aba9c984 2019-09-08 stsp
110 04ca23f4 2018-07-16 stsp int
111 04ca23f4 2018-07-16 stsp got_repo_is_bare(struct got_repository *repo)
112 04ca23f4 2018-07-16 stsp {
113 04ca23f4 2018-07-16 stsp return (strcmp(repo->path, repo->path_git_dir) == 0);
114 4027f31a 2017-11-04 stsp }
115 4027f31a 2017-11-04 stsp
116 4027f31a 2017-11-04 stsp static char *
117 4027f31a 2017-11-04 stsp get_path_git_child(struct got_repository *repo, const char *basename)
118 4027f31a 2017-11-04 stsp {
119 4027f31a 2017-11-04 stsp char *path_child;
120 3168e5da 2020-09-10 stsp
121 4986b9d5 2018-03-12 stsp if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
122 4027f31a 2017-11-04 stsp basename) == -1)
123 4027f31a 2017-11-04 stsp return NULL;
124 4027f31a 2017-11-04 stsp
125 4027f31a 2017-11-04 stsp return path_child;
126 4027f31a 2017-11-04 stsp }
127 4027f31a 2017-11-04 stsp
128 11995603 2017-11-05 stsp char *
129 11995603 2017-11-05 stsp got_repo_get_path_objects(struct got_repository *repo)
130 4027f31a 2017-11-04 stsp {
131 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_OBJECTS_DIR);
132 4027f31a 2017-11-04 stsp }
133 4027f31a 2017-11-04 stsp
134 11995603 2017-11-05 stsp char *
135 a1fd68d8 2018-01-12 stsp got_repo_get_path_objects_pack(struct got_repository *repo)
136 a1fd68d8 2018-01-12 stsp {
137 a1fd68d8 2018-01-12 stsp return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
138 a1fd68d8 2018-01-12 stsp }
139 a1fd68d8 2018-01-12 stsp
140 a1fd68d8 2018-01-12 stsp char *
141 11995603 2017-11-05 stsp got_repo_get_path_refs(struct got_repository *repo)
142 4027f31a 2017-11-04 stsp {
143 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_REFS_DIR);
144 4027f31a 2017-11-04 stsp }
145 4027f31a 2017-11-04 stsp
146 fb79db15 2019-02-01 stsp char *
147 fb79db15 2019-02-01 stsp got_repo_get_path_packed_refs(struct got_repository *repo)
148 fb79db15 2019-02-01 stsp {
149 fb79db15 2019-02-01 stsp return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
150 fb79db15 2019-02-01 stsp }
151 fb79db15 2019-02-01 stsp
152 4027f31a 2017-11-04 stsp static char *
153 4027f31a 2017-11-04 stsp get_path_head(struct got_repository *repo)
154 4027f31a 2017-11-04 stsp {
155 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_HEAD_FILE);
156 1d126e2d 2019-08-24 stsp }
157 1d126e2d 2019-08-24 stsp
158 b46f3e71 2020-03-18 stsp char *
159 b46f3e71 2020-03-18 stsp got_repo_get_path_gitconfig(struct got_repository *repo)
160 1d126e2d 2019-08-24 stsp {
161 b46f3e71 2020-03-18 stsp return get_path_git_child(repo, GOT_GITCONFIG);
162 cd95becd 2019-11-29 stsp }
163 cd95becd 2019-11-29 stsp
164 257add31 2020-09-09 stsp char *
165 257add31 2020-09-09 stsp got_repo_get_path_gotconfig(struct got_repository *repo)
166 257add31 2020-09-09 stsp {
167 50b0790e 2020-09-11 stsp return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
168 257add31 2020-09-09 stsp }
169 257add31 2020-09-09 stsp
170 50b0790e 2020-09-11 stsp const struct got_gotconfig *
171 50b0790e 2020-09-11 stsp got_repo_get_gotconfig(struct got_repository *repo)
172 cd95becd 2019-11-29 stsp {
173 50b0790e 2020-09-11 stsp return repo->gotconfig;
174 4027f31a 2017-11-04 stsp }
175 4027f31a 2017-11-04 stsp
176 257add31 2020-09-09 stsp void
177 50b0790e 2020-09-11 stsp got_repo_get_gitconfig_remotes(int *nremotes,
178 50b0790e 2020-09-11 stsp const struct got_remote_repo **remotes, struct got_repository *repo)
179 257add31 2020-09-09 stsp {
180 50b0790e 2020-09-11 stsp *nremotes = repo->ngitconfig_remotes;
181 50b0790e 2020-09-11 stsp *remotes = repo->gitconfig_remotes;
182 257add31 2020-09-09 stsp }
183 257add31 2020-09-09 stsp
184 4027f31a 2017-11-04 stsp static int
185 4027f31a 2017-11-04 stsp is_git_repo(struct got_repository *repo)
186 4027f31a 2017-11-04 stsp {
187 6e9da951 2019-01-06 stsp const char *path_git = got_repo_get_path_git_dir(repo);
188 11995603 2017-11-05 stsp char *path_objects = got_repo_get_path_objects(repo);
189 11995603 2017-11-05 stsp char *path_refs = got_repo_get_path_refs(repo);
190 4027f31a 2017-11-04 stsp char *path_head = get_path_head(repo);
191 deeca238 2018-03-12 stsp int ret = 0;
192 deeca238 2018-03-12 stsp struct stat sb;
193 4847cca1 2018-03-12 stsp struct got_reference *head_ref;
194 4027f31a 2017-11-04 stsp
195 deeca238 2018-03-12 stsp if (lstat(path_git, &sb) == -1)
196 deeca238 2018-03-12 stsp goto done;
197 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
198 deeca238 2018-03-12 stsp goto done;
199 4027f31a 2017-11-04 stsp
200 deeca238 2018-03-12 stsp if (lstat(path_objects, &sb) == -1)
201 deeca238 2018-03-12 stsp goto done;
202 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
203 deeca238 2018-03-12 stsp goto done;
204 deeca238 2018-03-12 stsp
205 deeca238 2018-03-12 stsp if (lstat(path_refs, &sb) == -1)
206 deeca238 2018-03-12 stsp goto done;
207 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
208 deeca238 2018-03-12 stsp goto done;
209 deeca238 2018-03-12 stsp
210 deeca238 2018-03-12 stsp if (lstat(path_head, &sb) == -1)
211 deeca238 2018-03-12 stsp goto done;
212 deeca238 2018-03-12 stsp if (!S_ISREG(sb.st_mode))
213 deeca238 2018-03-12 stsp goto done;
214 4847cca1 2018-03-12 stsp
215 4847cca1 2018-03-12 stsp /* Check if the HEAD reference can be opened. */
216 2f17228e 2019-05-12 stsp if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
217 4847cca1 2018-03-12 stsp goto done;
218 4847cca1 2018-03-12 stsp got_ref_close(head_ref);
219 4847cca1 2018-03-12 stsp
220 deeca238 2018-03-12 stsp ret = 1;
221 deeca238 2018-03-12 stsp done:
222 4027f31a 2017-11-04 stsp free(path_objects);
223 4027f31a 2017-11-04 stsp free(path_refs);
224 4027f31a 2017-11-04 stsp free(path_head);
225 4027f31a 2017-11-04 stsp return ret;
226 4027f31a 2017-11-04 stsp
227 7bb0daa1 2018-06-21 stsp }
228 7bb0daa1 2018-06-21 stsp
229 f6be5c30 2018-06-22 stsp const struct got_error *
230 f6be5c30 2018-06-22 stsp got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
231 f6be5c30 2018-06-22 stsp struct got_object *obj)
232 f6be5c30 2018-06-22 stsp {
233 ccfe88e6 2018-07-12 stsp #ifndef GOT_NO_OBJ_CACHE
234 f6be5c30 2018-06-22 stsp const struct got_error *err = NULL;
235 6bef87be 2018-09-11 stsp err = got_object_cache_add(&repo->objcache, id, obj);
236 79c99a64 2019-05-23 stsp if (err) {
237 79c99a64 2019-05-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
238 79c99a64 2019-05-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
239 79c99a64 2019-05-23 stsp err = NULL;
240 f6be5c30 2018-06-22 stsp return err;
241 79c99a64 2019-05-23 stsp }
242 f6be5c30 2018-06-22 stsp obj->refcnt++;
243 ccfe88e6 2018-07-12 stsp #endif
244 f6be5c30 2018-06-22 stsp return NULL;
245 f6be5c30 2018-06-22 stsp }
246 f6be5c30 2018-06-22 stsp
247 7bb0daa1 2018-06-21 stsp struct got_object *
248 7bb0daa1 2018-06-21 stsp got_repo_get_cached_object(struct got_repository *repo,
249 7bb0daa1 2018-06-21 stsp struct got_object_id *id)
250 7bb0daa1 2018-06-21 stsp {
251 6bef87be 2018-09-11 stsp return (struct got_object *)got_object_cache_get(&repo->objcache, id);
252 7bb0daa1 2018-06-21 stsp }
253 7bb0daa1 2018-06-21 stsp
254 4027f31a 2017-11-04 stsp const struct got_error *
255 f6be5c30 2018-06-22 stsp got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
256 f6be5c30 2018-06-22 stsp struct got_tree_object *tree)
257 f6be5c30 2018-06-22 stsp {
258 ccfe88e6 2018-07-12 stsp #ifndef GOT_NO_OBJ_CACHE
259 f6be5c30 2018-06-22 stsp const struct got_error *err = NULL;
260 6bef87be 2018-09-11 stsp err = got_object_cache_add(&repo->treecache, id, tree);
261 79c99a64 2019-05-23 stsp if (err) {
262 79c99a64 2019-05-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
263 79c99a64 2019-05-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
264 79c99a64 2019-05-23 stsp err = NULL;
265 f6be5c30 2018-06-22 stsp return err;
266 79c99a64 2019-05-23 stsp }
267 f6be5c30 2018-06-22 stsp tree->refcnt++;
268 ccfe88e6 2018-07-12 stsp #endif
269 f6be5c30 2018-06-22 stsp return NULL;
270 f6be5c30 2018-06-22 stsp }
271 f6be5c30 2018-06-22 stsp
272 f6be5c30 2018-06-22 stsp struct got_tree_object *
273 f6be5c30 2018-06-22 stsp got_repo_get_cached_tree(struct got_repository *repo,
274 f6be5c30 2018-06-22 stsp struct got_object_id *id)
275 f6be5c30 2018-06-22 stsp {
276 6bef87be 2018-09-11 stsp return (struct got_tree_object *)got_object_cache_get(
277 6bef87be 2018-09-11 stsp &repo->treecache, id);
278 1943de01 2018-06-22 stsp }
279 1943de01 2018-06-22 stsp
280 1943de01 2018-06-22 stsp const struct got_error *
281 1943de01 2018-06-22 stsp got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
282 1943de01 2018-06-22 stsp struct got_commit_object *commit)
283 1943de01 2018-06-22 stsp {
284 ccfe88e6 2018-07-12 stsp #ifndef GOT_NO_OBJ_CACHE
285 1943de01 2018-06-22 stsp const struct got_error *err = NULL;
286 6bef87be 2018-09-11 stsp err = got_object_cache_add(&repo->commitcache, id, commit);
287 79c99a64 2019-05-23 stsp if (err) {
288 79c99a64 2019-05-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
289 79c99a64 2019-05-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
290 79c99a64 2019-05-23 stsp err = NULL;
291 1943de01 2018-06-22 stsp return err;
292 79c99a64 2019-05-23 stsp }
293 1943de01 2018-06-22 stsp commit->refcnt++;
294 ccfe88e6 2018-07-12 stsp #endif
295 f6be5c30 2018-06-22 stsp return NULL;
296 f6be5c30 2018-06-22 stsp }
297 f6be5c30 2018-06-22 stsp
298 1943de01 2018-06-22 stsp struct got_commit_object *
299 1943de01 2018-06-22 stsp got_repo_get_cached_commit(struct got_repository *repo,
300 1943de01 2018-06-22 stsp struct got_object_id *id)
301 1943de01 2018-06-22 stsp {
302 6bef87be 2018-09-11 stsp return (struct got_commit_object *)got_object_cache_get(
303 6bef87be 2018-09-11 stsp &repo->commitcache, id);
304 f4a881ce 2018-11-17 stsp }
305 f4a881ce 2018-11-17 stsp
306 f4a881ce 2018-11-17 stsp const struct got_error *
307 f4a881ce 2018-11-17 stsp got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
308 f4a881ce 2018-11-17 stsp struct got_tag_object *tag)
309 f4a881ce 2018-11-17 stsp {
310 f4a881ce 2018-11-17 stsp #ifndef GOT_NO_OBJ_CACHE
311 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
312 f4a881ce 2018-11-17 stsp err = got_object_cache_add(&repo->tagcache, id, tag);
313 79c99a64 2019-05-23 stsp if (err) {
314 79c99a64 2019-05-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
315 79c99a64 2019-05-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
316 79c99a64 2019-05-23 stsp err = NULL;
317 f4a881ce 2018-11-17 stsp return err;
318 79c99a64 2019-05-23 stsp }
319 f4a881ce 2018-11-17 stsp tag->refcnt++;
320 f4a881ce 2018-11-17 stsp #endif
321 f4a881ce 2018-11-17 stsp return NULL;
322 f4a881ce 2018-11-17 stsp }
323 f4a881ce 2018-11-17 stsp
324 f4a881ce 2018-11-17 stsp struct got_tag_object *
325 f4a881ce 2018-11-17 stsp got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
326 f4a881ce 2018-11-17 stsp {
327 f4a881ce 2018-11-17 stsp return (struct got_tag_object *)got_object_cache_get(
328 f4a881ce 2018-11-17 stsp &repo->tagcache, id);
329 1943de01 2018-06-22 stsp }
330 1943de01 2018-06-22 stsp
331 f6be5c30 2018-06-22 stsp const struct got_error *
332 85f51bba 2018-07-16 stsp open_repo(struct got_repository *repo, const char *path)
333 4027f31a 2017-11-04 stsp {
334 85f51bba 2018-07-16 stsp const struct got_error *err = NULL;
335 85f51bba 2018-07-16 stsp
336 85f51bba 2018-07-16 stsp /* bare git repository? */
337 85f51bba 2018-07-16 stsp repo->path_git_dir = strdup(path);
338 ee645855 2019-02-05 stsp if (repo->path_git_dir == NULL)
339 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
340 85f51bba 2018-07-16 stsp if (is_git_repo(repo)) {
341 85f51bba 2018-07-16 stsp repo->path = strdup(repo->path_git_dir);
342 85f51bba 2018-07-16 stsp if (repo->path == NULL) {
343 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
344 85f51bba 2018-07-16 stsp goto done;
345 85f51bba 2018-07-16 stsp }
346 85f51bba 2018-07-16 stsp return NULL;
347 85f51bba 2018-07-16 stsp }
348 85f51bba 2018-07-16 stsp
349 85f51bba 2018-07-16 stsp /* git repository with working tree? */
350 85f51bba 2018-07-16 stsp free(repo->path_git_dir);
351 6b68ccd6 2019-09-01 stsp repo->path_git_dir = NULL;
352 85f51bba 2018-07-16 stsp if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
353 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
354 85f51bba 2018-07-16 stsp goto done;
355 85f51bba 2018-07-16 stsp }
356 85f51bba 2018-07-16 stsp if (is_git_repo(repo)) {
357 85f51bba 2018-07-16 stsp repo->path = strdup(path);
358 85f51bba 2018-07-16 stsp if (repo->path == NULL) {
359 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
360 85f51bba 2018-07-16 stsp goto done;
361 85f51bba 2018-07-16 stsp }
362 85f51bba 2018-07-16 stsp return NULL;
363 85f51bba 2018-07-16 stsp }
364 85f51bba 2018-07-16 stsp
365 ee645855 2019-02-05 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
366 ee645855 2019-02-05 stsp done:
367 85f51bba 2018-07-16 stsp if (err) {
368 ee645855 2019-02-05 stsp free(repo->path);
369 ee645855 2019-02-05 stsp repo->path = NULL;
370 85f51bba 2018-07-16 stsp free(repo->path_git_dir);
371 ee645855 2019-02-05 stsp repo->path_git_dir = NULL;
372 aba9c984 2019-09-08 stsp }
373 aba9c984 2019-09-08 stsp return err;
374 aba9c984 2019-09-08 stsp }
375 aba9c984 2019-09-08 stsp
376 aba9c984 2019-09-08 stsp static const struct got_error *
377 c9956ddf 2019-09-08 stsp parse_gitconfig_file(int *gitconfig_repository_format_version,
378 c9956ddf 2019-09-08 stsp char **gitconfig_author_name, char **gitconfig_author_email,
379 cd95becd 2019-11-29 stsp struct got_remote_repo **remotes, int *nremotes,
380 20b7abb3 2020-10-22 stsp char **gitconfig_owner, char ***extensions, int *nextensions,
381 c9956ddf 2019-09-08 stsp const char *gitconfig_path)
382 aba9c984 2019-09-08 stsp {
383 aba9c984 2019-09-08 stsp const struct got_error *err = NULL, *child_err = NULL;
384 aba9c984 2019-09-08 stsp int fd = -1;
385 aba9c984 2019-09-08 stsp int imsg_fds[2] = { -1, -1 };
386 aba9c984 2019-09-08 stsp pid_t pid;
387 aba9c984 2019-09-08 stsp struct imsgbuf *ibuf;
388 aba9c984 2019-09-08 stsp
389 c9956ddf 2019-09-08 stsp *gitconfig_repository_format_version = 0;
390 20b7abb3 2020-10-22 stsp if (extensions)
391 20b7abb3 2020-10-22 stsp *extensions = NULL;
392 20b7abb3 2020-10-22 stsp if (nextensions)
393 20b7abb3 2020-10-22 stsp *nextensions = 0;
394 c9956ddf 2019-09-08 stsp *gitconfig_author_name = NULL;
395 c9956ddf 2019-09-08 stsp *gitconfig_author_email = NULL;
396 2fb669fb 2020-03-20 stsp if (remotes)
397 2fb669fb 2020-03-20 stsp *remotes = NULL;
398 2fb669fb 2020-03-20 stsp if (nremotes)
399 2fb669fb 2020-03-20 stsp *nremotes = 0;
400 2fb669fb 2020-03-20 stsp if (gitconfig_owner)
401 2fb669fb 2020-03-20 stsp *gitconfig_owner = NULL;
402 aba9c984 2019-09-08 stsp
403 aba9c984 2019-09-08 stsp fd = open(gitconfig_path, O_RDONLY);
404 aba9c984 2019-09-08 stsp if (fd == -1) {
405 c9956ddf 2019-09-08 stsp if (errno == ENOENT)
406 aba9c984 2019-09-08 stsp return NULL;
407 c9956ddf 2019-09-08 stsp return got_error_from_errno2("open", gitconfig_path);
408 aba9c984 2019-09-08 stsp }
409 aba9c984 2019-09-08 stsp
410 aba9c984 2019-09-08 stsp ibuf = calloc(1, sizeof(*ibuf));
411 aba9c984 2019-09-08 stsp if (ibuf == NULL) {
412 aba9c984 2019-09-08 stsp err = got_error_from_errno("calloc");
413 aba9c984 2019-09-08 stsp goto done;
414 aba9c984 2019-09-08 stsp }
415 aba9c984 2019-09-08 stsp
416 aba9c984 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
417 aba9c984 2019-09-08 stsp err = got_error_from_errno("socketpair");
418 aba9c984 2019-09-08 stsp goto done;
419 aba9c984 2019-09-08 stsp }
420 aba9c984 2019-09-08 stsp
421 aba9c984 2019-09-08 stsp pid = fork();
422 aba9c984 2019-09-08 stsp if (pid == -1) {
423 aba9c984 2019-09-08 stsp err = got_error_from_errno("fork");
424 aba9c984 2019-09-08 stsp goto done;
425 aba9c984 2019-09-08 stsp } else if (pid == 0) {
426 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
427 c9956ddf 2019-09-08 stsp gitconfig_path);
428 aba9c984 2019-09-08 stsp /* not reached */
429 aba9c984 2019-09-08 stsp }
430 aba9c984 2019-09-08 stsp
431 aba9c984 2019-09-08 stsp if (close(imsg_fds[1]) == -1) {
432 aba9c984 2019-09-08 stsp err = got_error_from_errno("close");
433 aba9c984 2019-09-08 stsp goto done;
434 85f51bba 2018-07-16 stsp }
435 aba9c984 2019-09-08 stsp imsg_fds[1] = -1;
436 aba9c984 2019-09-08 stsp imsg_init(ibuf, imsg_fds[0]);
437 aba9c984 2019-09-08 stsp
438 aba9c984 2019-09-08 stsp err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
439 aba9c984 2019-09-08 stsp if (err)
440 aba9c984 2019-09-08 stsp goto done;
441 aba9c984 2019-09-08 stsp fd = -1;
442 aba9c984 2019-09-08 stsp
443 aba9c984 2019-09-08 stsp err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
444 aba9c984 2019-09-08 stsp if (err)
445 aba9c984 2019-09-08 stsp goto done;
446 aba9c984 2019-09-08 stsp
447 aba9c984 2019-09-08 stsp err = got_privsep_recv_gitconfig_int(
448 c9956ddf 2019-09-08 stsp gitconfig_repository_format_version, ibuf);
449 aba9c984 2019-09-08 stsp if (err)
450 aba9c984 2019-09-08 stsp goto done;
451 aba9c984 2019-09-08 stsp
452 20b7abb3 2020-10-22 stsp if (extensions && nextensions) {
453 20b7abb3 2020-10-22 stsp err = got_privsep_send_gitconfig_repository_extensions_req(
454 20b7abb3 2020-10-22 stsp ibuf);
455 20b7abb3 2020-10-22 stsp if (err)
456 20b7abb3 2020-10-22 stsp goto done;
457 20b7abb3 2020-10-22 stsp err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
458 20b7abb3 2020-10-22 stsp if (err)
459 20b7abb3 2020-10-22 stsp goto done;
460 20b7abb3 2020-10-22 stsp if (*nextensions > 0) {
461 20b7abb3 2020-10-22 stsp int i;
462 20b7abb3 2020-10-22 stsp *extensions = calloc(*nextensions, sizeof(char *));
463 20b7abb3 2020-10-22 stsp if (*extensions == NULL) {
464 20b7abb3 2020-10-22 stsp err = got_error_from_errno("calloc");
465 20b7abb3 2020-10-22 stsp goto done;
466 20b7abb3 2020-10-22 stsp }
467 20b7abb3 2020-10-22 stsp for (i = 0; i < *nextensions; i++) {
468 20b7abb3 2020-10-22 stsp char *ext;
469 20b7abb3 2020-10-22 stsp err = got_privsep_recv_gitconfig_str(&ext,
470 20b7abb3 2020-10-22 stsp ibuf);
471 20b7abb3 2020-10-22 stsp if (err)
472 20b7abb3 2020-10-22 stsp goto done;
473 20b7abb3 2020-10-22 stsp (*extensions)[i] = ext;
474 20b7abb3 2020-10-22 stsp }
475 20b7abb3 2020-10-22 stsp }
476 20b7abb3 2020-10-22 stsp }
477 20b7abb3 2020-10-22 stsp
478 aba9c984 2019-09-08 stsp err = got_privsep_send_gitconfig_author_name_req(ibuf);
479 aba9c984 2019-09-08 stsp if (err)
480 aba9c984 2019-09-08 stsp goto done;
481 aba9c984 2019-09-08 stsp
482 c9956ddf 2019-09-08 stsp err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
483 aba9c984 2019-09-08 stsp if (err)
484 aba9c984 2019-09-08 stsp goto done;
485 aba9c984 2019-09-08 stsp
486 aba9c984 2019-09-08 stsp err = got_privsep_send_gitconfig_author_email_req(ibuf);
487 aba9c984 2019-09-08 stsp if (err)
488 aba9c984 2019-09-08 stsp goto done;
489 aba9c984 2019-09-08 stsp
490 c9956ddf 2019-09-08 stsp err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
491 aba9c984 2019-09-08 stsp if (err)
492 aba9c984 2019-09-08 stsp goto done;
493 cd95becd 2019-11-29 stsp
494 cd95becd 2019-11-29 stsp if (remotes && nremotes) {
495 cd95becd 2019-11-29 stsp err = got_privsep_send_gitconfig_remotes_req(ibuf);
496 cd95becd 2019-11-29 stsp if (err)
497 cd95becd 2019-11-29 stsp goto done;
498 cd95becd 2019-11-29 stsp
499 cd95becd 2019-11-29 stsp err = got_privsep_recv_gitconfig_remotes(remotes,
500 cd95becd 2019-11-29 stsp nremotes, ibuf);
501 9a1cc63f 2020-02-03 stsp if (err)
502 9a1cc63f 2020-02-03 stsp goto done;
503 9a1cc63f 2020-02-03 stsp }
504 9a1cc63f 2020-02-03 stsp
505 9a1cc63f 2020-02-03 stsp if (gitconfig_owner) {
506 9a1cc63f 2020-02-03 stsp err = got_privsep_send_gitconfig_owner_req(ibuf);
507 cd95becd 2019-11-29 stsp if (err)
508 cd95becd 2019-11-29 stsp goto done;
509 9a1cc63f 2020-02-03 stsp err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
510 9a1cc63f 2020-02-03 stsp if (err)
511 9a1cc63f 2020-02-03 stsp goto done;
512 cd95becd 2019-11-29 stsp }
513 aba9c984 2019-09-08 stsp
514 aba9c984 2019-09-08 stsp imsg_clear(ibuf);
515 aba9c984 2019-09-08 stsp err = got_privsep_send_stop(imsg_fds[0]);
516 aba9c984 2019-09-08 stsp child_err = got_privsep_wait_for_child(pid);
517 aba9c984 2019-09-08 stsp if (child_err && err == NULL)
518 aba9c984 2019-09-08 stsp err = child_err;
519 aba9c984 2019-09-08 stsp done:
520 aba9c984 2019-09-08 stsp if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
521 aba9c984 2019-09-08 stsp err = got_error_from_errno("close");
522 aba9c984 2019-09-08 stsp if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
523 aba9c984 2019-09-08 stsp err = got_error_from_errno("close");
524 aba9c984 2019-09-08 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
525 aba9c984 2019-09-08 stsp err = got_error_from_errno2("close", gitconfig_path);
526 aba9c984 2019-09-08 stsp free(ibuf);
527 c9956ddf 2019-09-08 stsp return err;
528 c9956ddf 2019-09-08 stsp }
529 c9956ddf 2019-09-08 stsp
530 c9956ddf 2019-09-08 stsp static const struct got_error *
531 c9956ddf 2019-09-08 stsp read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
532 c9956ddf 2019-09-08 stsp {
533 c9956ddf 2019-09-08 stsp const struct got_error *err = NULL;
534 c9956ddf 2019-09-08 stsp char *repo_gitconfig_path = NULL;
535 c9956ddf 2019-09-08 stsp
536 c9956ddf 2019-09-08 stsp if (global_gitconfig_path) {
537 c9956ddf 2019-09-08 stsp /* Read settings from ~/.gitconfig. */
538 c9956ddf 2019-09-08 stsp int dummy_repo_version;
539 c9956ddf 2019-09-08 stsp err = parse_gitconfig_file(&dummy_repo_version,
540 c9956ddf 2019-09-08 stsp &repo->global_gitconfig_author_name,
541 c9956ddf 2019-09-08 stsp &repo->global_gitconfig_author_email,
542 20b7abb3 2020-10-22 stsp NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
543 c9956ddf 2019-09-08 stsp if (err)
544 c9956ddf 2019-09-08 stsp return err;
545 c9956ddf 2019-09-08 stsp }
546 c9956ddf 2019-09-08 stsp
547 c9956ddf 2019-09-08 stsp /* Read repository's .git/config file. */
548 b46f3e71 2020-03-18 stsp repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
549 b46f3e71 2020-03-18 stsp if (repo_gitconfig_path == NULL)
550 b46f3e71 2020-03-18 stsp return got_error_from_errno("got_repo_get_path_gitconfig");
551 c9956ddf 2019-09-08 stsp
552 c9956ddf 2019-09-08 stsp err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
553 c9956ddf 2019-09-08 stsp &repo->gitconfig_author_name, &repo->gitconfig_author_email,
554 cd95becd 2019-11-29 stsp &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
555 20b7abb3 2020-10-22 stsp &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
556 20b7abb3 2020-10-22 stsp repo_gitconfig_path);
557 c9956ddf 2019-09-08 stsp if (err)
558 c9956ddf 2019-09-08 stsp goto done;
559 c9956ddf 2019-09-08 stsp done:
560 c9956ddf 2019-09-08 stsp free(repo_gitconfig_path);
561 257add31 2020-09-09 stsp return err;
562 257add31 2020-09-09 stsp }
563 257add31 2020-09-09 stsp
564 257add31 2020-09-09 stsp static const struct got_error *
565 257add31 2020-09-09 stsp read_gotconfig(struct got_repository *repo)
566 257add31 2020-09-09 stsp {
567 257add31 2020-09-09 stsp const struct got_error *err = NULL;
568 257add31 2020-09-09 stsp char *gotconfig_path;
569 257add31 2020-09-09 stsp
570 257add31 2020-09-09 stsp gotconfig_path = got_repo_get_path_gotconfig(repo);
571 257add31 2020-09-09 stsp if (gotconfig_path == NULL)
572 257add31 2020-09-09 stsp return got_error_from_errno("got_repo_get_path_gotconfig");
573 257add31 2020-09-09 stsp
574 50b0790e 2020-09-11 stsp err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
575 257add31 2020-09-09 stsp free(gotconfig_path);
576 85f51bba 2018-07-16 stsp return err;
577 85f51bba 2018-07-16 stsp }
578 85f51bba 2018-07-16 stsp
579 20b7abb3 2020-10-22 stsp /* Supported repository format extensions. */
580 20b7abb3 2020-10-22 stsp static const char *repo_extensions[] = {
581 20b7abb3 2020-10-22 stsp "noop", /* Got supports repository format version 1. */
582 20b7abb3 2020-10-22 stsp "preciousObjects", /* Got has no garbage collection yet. */
583 20b7abb3 2020-10-22 stsp "worktreeConfig", /* Got does not care about Git work trees. */
584 20b7abb3 2020-10-22 stsp };
585 20b7abb3 2020-10-22 stsp
586 85f51bba 2018-07-16 stsp const struct got_error *
587 c9956ddf 2019-09-08 stsp got_repo_open(struct got_repository **repop, const char *path,
588 c9956ddf 2019-09-08 stsp const char *global_gitconfig_path)
589 85f51bba 2018-07-16 stsp {
590 92af5469 2017-11-05 stsp struct got_repository *repo = NULL;
591 92af5469 2017-11-05 stsp const struct got_error *err = NULL;
592 aff6eea4 2020-10-20 stsp char *abspath, *repo_path = NULL;
593 0c93d204 2020-10-20 stsp int i;
594 4027f31a 2017-11-04 stsp
595 85f51bba 2018-07-16 stsp *repop = NULL;
596 85f51bba 2018-07-16 stsp
597 2393f13b 2018-03-09 stsp if (got_path_is_absolute(path))
598 2393f13b 2018-03-09 stsp abspath = strdup(path);
599 2393f13b 2018-03-09 stsp else
600 2393f13b 2018-03-09 stsp abspath = got_path_get_absolute(path);
601 92af5469 2017-11-05 stsp if (abspath == NULL)
602 63f810e6 2020-02-29 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
603 4027f31a 2017-11-04 stsp
604 4027f31a 2017-11-04 stsp repo = calloc(1, sizeof(*repo));
605 92af5469 2017-11-05 stsp if (repo == NULL) {
606 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
607 92af5469 2017-11-05 stsp goto done;
608 92af5469 2017-11-05 stsp }
609 4027f31a 2017-11-04 stsp
610 ad242220 2018-09-08 stsp for (i = 0; i < nitems(repo->privsep_children); i++) {
611 3516b818 2018-09-08 stsp memset(&repo->privsep_children[i], 0,
612 3516b818 2018-09-08 stsp sizeof(repo->privsep_children[0]));
613 ad242220 2018-09-08 stsp repo->privsep_children[i].imsg_fd = -1;
614 ad242220 2018-09-08 stsp }
615 ad242220 2018-09-08 stsp
616 6bef87be 2018-09-11 stsp err = got_object_cache_init(&repo->objcache,
617 6bef87be 2018-09-11 stsp GOT_OBJECT_CACHE_TYPE_OBJ);
618 6bef87be 2018-09-11 stsp if (err)
619 f6be5c30 2018-06-22 stsp goto done;
620 6bef87be 2018-09-11 stsp err = got_object_cache_init(&repo->treecache,
621 6bef87be 2018-09-11 stsp GOT_OBJECT_CACHE_TYPE_TREE);
622 6bef87be 2018-09-11 stsp if (err)
623 1943de01 2018-06-22 stsp goto done;
624 6bef87be 2018-09-11 stsp err = got_object_cache_init(&repo->commitcache,
625 6bef87be 2018-09-11 stsp GOT_OBJECT_CACHE_TYPE_COMMIT);
626 6bef87be 2018-09-11 stsp if (err)
627 eb77ee11 2018-07-08 stsp goto done;
628 f4a881ce 2018-11-17 stsp err = got_object_cache_init(&repo->tagcache,
629 f4a881ce 2018-11-17 stsp GOT_OBJECT_CACHE_TYPE_TAG);
630 f4a881ce 2018-11-17 stsp if (err)
631 f4a881ce 2018-11-17 stsp goto done;
632 1943de01 2018-06-22 stsp
633 aff6eea4 2020-10-20 stsp repo_path = realpath(abspath, NULL);
634 aff6eea4 2020-10-20 stsp if (repo_path == NULL) {
635 62550b13 2019-07-23 stsp err = got_error_from_errno2("realpath", abspath);
636 92af5469 2017-11-05 stsp goto done;
637 92af5469 2017-11-05 stsp }
638 4027f31a 2017-11-04 stsp
639 aff6eea4 2020-10-20 stsp for (;;) {
640 aff6eea4 2020-10-20 stsp char *parent_path;
641 aff6eea4 2020-10-20 stsp
642 aff6eea4 2020-10-20 stsp err = open_repo(repo, repo_path);
643 85f51bba 2018-07-16 stsp if (err == NULL)
644 85f51bba 2018-07-16 stsp break;
645 85f51bba 2018-07-16 stsp if (err->code != GOT_ERR_NOT_GIT_REPO)
646 9aceaadf 2020-10-20 stsp goto done;
647 aff6eea4 2020-10-20 stsp if (repo_path[0] == '/' && repo_path[1] == '\0') {
648 0c93d204 2020-10-20 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
649 0c93d204 2020-10-20 stsp goto done;
650 442a3ddc 2018-04-23 stsp }
651 aff6eea4 2020-10-20 stsp err = got_path_dirname(&parent_path, repo_path);
652 aff6eea4 2020-10-20 stsp if (err)
653 f2db9c47 2019-08-24 stsp goto done;
654 aff6eea4 2020-10-20 stsp free(repo_path);
655 aff6eea4 2020-10-20 stsp repo_path = parent_path;
656 aff6eea4 2020-10-20 stsp }
657 1d126e2d 2019-08-24 stsp
658 257add31 2020-09-09 stsp err = read_gotconfig(repo);
659 257add31 2020-09-09 stsp if (err)
660 257add31 2020-09-09 stsp goto done;
661 257add31 2020-09-09 stsp
662 c9956ddf 2019-09-08 stsp err = read_gitconfig(repo, global_gitconfig_path);
663 1d126e2d 2019-08-24 stsp if (err)
664 1d126e2d 2019-08-24 stsp goto done;
665 aba9c984 2019-09-08 stsp if (repo->gitconfig_repository_format_version != 0)
666 aba9c984 2019-09-08 stsp err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
667 20b7abb3 2020-10-22 stsp for (i = 0; i < repo->nextensions; i++) {
668 20b7abb3 2020-10-22 stsp char *ext = repo->extensions[i];
669 20b7abb3 2020-10-22 stsp int j, supported = 0;
670 20b7abb3 2020-10-22 stsp for (j = 0; j < nitems(repo_extensions); j++) {
671 20b7abb3 2020-10-22 stsp if (strcmp(ext, repo_extensions[j]) == 0) {
672 20b7abb3 2020-10-22 stsp supported = 1;
673 20b7abb3 2020-10-22 stsp break;
674 20b7abb3 2020-10-22 stsp }
675 20b7abb3 2020-10-22 stsp }
676 20b7abb3 2020-10-22 stsp if (!supported) {
677 20b7abb3 2020-10-22 stsp err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
678 20b7abb3 2020-10-22 stsp goto done;
679 20b7abb3 2020-10-22 stsp }
680 20b7abb3 2020-10-22 stsp }
681 92af5469 2017-11-05 stsp done:
682 92af5469 2017-11-05 stsp if (err)
683 5c2f5761 2018-09-19 stsp got_repo_close(repo);
684 85f51bba 2018-07-16 stsp else
685 85f51bba 2018-07-16 stsp *repop = repo;
686 92af5469 2017-11-05 stsp free(abspath);
687 aff6eea4 2020-10-20 stsp free(repo_path);
688 92af5469 2017-11-05 stsp return err;
689 4027f31a 2017-11-04 stsp }
690 4027f31a 2017-11-04 stsp
691 ad242220 2018-09-08 stsp const struct got_error *
692 4027f31a 2017-11-04 stsp got_repo_close(struct got_repository *repo)
693 4027f31a 2017-11-04 stsp {
694 ad242220 2018-09-08 stsp const struct got_error *err = NULL, *child_err;
695 79b11c62 2018-03-09 stsp int i;
696 79b11c62 2018-03-09 stsp
697 65cf1e80 2018-03-16 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
698 65cf1e80 2018-03-16 stsp if (repo->packidx_cache[i] == NULL)
699 79b11c62 2018-03-09 stsp break;
700 65cf1e80 2018-03-16 stsp got_packidx_close(repo->packidx_cache[i]);
701 79b11c62 2018-03-09 stsp }
702 bd1223b9 2018-03-14 stsp
703 7e656b93 2018-03-17 stsp for (i = 0; i < nitems(repo->packs); i++) {
704 7e656b93 2018-03-17 stsp if (repo->packs[i].path_packfile == NULL)
705 7e656b93 2018-03-17 stsp break;
706 7e656b93 2018-03-17 stsp got_pack_close(&repo->packs[i]);
707 7e656b93 2018-03-17 stsp }
708 7e656b93 2018-03-17 stsp
709 4027f31a 2017-11-04 stsp free(repo->path);
710 4986b9d5 2018-03-12 stsp free(repo->path_git_dir);
711 cd717821 2018-06-22 stsp
712 6bef87be 2018-09-11 stsp got_object_cache_close(&repo->objcache);
713 6bef87be 2018-09-11 stsp got_object_cache_close(&repo->treecache);
714 6bef87be 2018-09-11 stsp got_object_cache_close(&repo->commitcache);
715 f4a881ce 2018-11-17 stsp got_object_cache_close(&repo->tagcache);
716 ad242220 2018-09-08 stsp
717 ad242220 2018-09-08 stsp for (i = 0; i < nitems(repo->privsep_children); i++) {
718 ad242220 2018-09-08 stsp if (repo->privsep_children[i].imsg_fd == -1)
719 ad242220 2018-09-08 stsp continue;
720 3516b818 2018-09-08 stsp imsg_clear(repo->privsep_children[i].ibuf);
721 3516b818 2018-09-08 stsp free(repo->privsep_children[i].ibuf);
722 ad242220 2018-09-08 stsp err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
723 876c234b 2018-09-10 stsp child_err = got_privsep_wait_for_child(
724 876c234b 2018-09-10 stsp repo->privsep_children[i].pid);
725 ad242220 2018-09-08 stsp if (child_err && err == NULL)
726 ad242220 2018-09-08 stsp err = child_err;
727 3a6ce05a 2019-02-11 stsp if (close(repo->privsep_children[i].imsg_fd) != 0 &&
728 3a6ce05a 2019-02-11 stsp err == NULL)
729 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
730 ad242220 2018-09-08 stsp }
731 aba9c984 2019-09-08 stsp
732 50b0790e 2020-09-11 stsp if (repo->gotconfig)
733 50b0790e 2020-09-11 stsp got_gotconfig_free(repo->gotconfig);
734 aba9c984 2019-09-08 stsp free(repo->gitconfig_author_name);
735 aba9c984 2019-09-08 stsp free(repo->gitconfig_author_email);
736 b8adfa55 2020-09-25 stsp for (i = 0; i < repo->ngitconfig_remotes; i++)
737 b8adfa55 2020-09-25 stsp got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
738 cd95becd 2019-11-29 stsp free(repo->gitconfig_remotes);
739 20b7abb3 2020-10-22 stsp for (i = 0; i < repo->nextensions; i++)
740 20b7abb3 2020-10-22 stsp free(repo->extensions[i]);
741 20b7abb3 2020-10-22 stsp free(repo->extensions);
742 4027f31a 2017-11-04 stsp free(repo);
743 ad242220 2018-09-08 stsp
744 ad242220 2018-09-08 stsp return err;
745 b8adfa55 2020-09-25 stsp }
746 b8adfa55 2020-09-25 stsp
747 b8adfa55 2020-09-25 stsp void
748 b8adfa55 2020-09-25 stsp got_repo_free_remote_repo_data(struct got_remote_repo *repo)
749 b8adfa55 2020-09-25 stsp {
750 b8adfa55 2020-09-25 stsp int i;
751 b8adfa55 2020-09-25 stsp
752 b8adfa55 2020-09-25 stsp free(repo->name);
753 b8adfa55 2020-09-25 stsp repo->name = NULL;
754 b8adfa55 2020-09-25 stsp free(repo->url);
755 b8adfa55 2020-09-25 stsp repo->url = NULL;
756 b8adfa55 2020-09-25 stsp for (i = 0; i < repo->nbranches; i++)
757 b8adfa55 2020-09-25 stsp free(repo->branches[i]);
758 b8adfa55 2020-09-25 stsp free(repo->branches);
759 b8adfa55 2020-09-25 stsp repo->branches = NULL;
760 b8adfa55 2020-09-25 stsp repo->nbranches = 0;
761 4027f31a 2017-11-04 stsp }
762 04ca23f4 2018-07-16 stsp
763 04ca23f4 2018-07-16 stsp const struct got_error *
764 04ca23f4 2018-07-16 stsp got_repo_map_path(char **in_repo_path, struct got_repository *repo,
765 8fa913ec 2020-11-14 stsp const char *input_path)
766 04ca23f4 2018-07-16 stsp {
767 04ca23f4 2018-07-16 stsp const struct got_error *err = NULL;
768 7839bc15 2019-01-06 stsp const char *repo_abspath = NULL;
769 e83c0634 2020-01-27 stsp size_t repolen, len;
770 e83c0634 2020-01-27 stsp char *canonpath, *path = NULL;
771 04ca23f4 2018-07-16 stsp
772 04ca23f4 2018-07-16 stsp *in_repo_path = NULL;
773 04ca23f4 2018-07-16 stsp
774 04ca23f4 2018-07-16 stsp canonpath = strdup(input_path);
775 04ca23f4 2018-07-16 stsp if (canonpath == NULL) {
776 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
777 04ca23f4 2018-07-16 stsp goto done;
778 04ca23f4 2018-07-16 stsp }
779 04ca23f4 2018-07-16 stsp err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
780 04ca23f4 2018-07-16 stsp if (err)
781 04ca23f4 2018-07-16 stsp goto done;
782 04ca23f4 2018-07-16 stsp
783 04ca23f4 2018-07-16 stsp repo_abspath = got_repo_get_path(repo);
784 04ca23f4 2018-07-16 stsp
785 8fa913ec 2020-11-14 stsp if (canonpath[0] == '\0') {
786 23721109 2018-10-22 stsp path = strdup(canonpath);
787 b70703ad 2019-03-18 stsp if (path == NULL) {
788 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
789 04ca23f4 2018-07-16 stsp goto done;
790 04ca23f4 2018-07-16 stsp }
791 04ca23f4 2018-07-16 stsp } else {
792 04ca23f4 2018-07-16 stsp path = realpath(canonpath, NULL);
793 04ca23f4 2018-07-16 stsp if (path == NULL) {
794 b70703ad 2019-03-18 stsp if (errno != ENOENT) {
795 638f9024 2019-05-13 stsp err = got_error_from_errno2("realpath",
796 230a42bd 2019-05-11 jcs canonpath);
797 b70703ad 2019-03-18 stsp goto done;
798 b70703ad 2019-03-18 stsp }
799 b70703ad 2019-03-18 stsp /*
800 b70703ad 2019-03-18 stsp * Path is not on disk.
801 b70703ad 2019-03-18 stsp * Assume it is already relative to repository root.
802 b70703ad 2019-03-18 stsp */
803 b70703ad 2019-03-18 stsp path = strdup(canonpath);
804 b70703ad 2019-03-18 stsp if (path == NULL) {
805 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
806 b70703ad 2019-03-18 stsp goto done;
807 b70703ad 2019-03-18 stsp }
808 04ca23f4 2018-07-16 stsp }
809 04ca23f4 2018-07-16 stsp
810 04ca23f4 2018-07-16 stsp repolen = strlen(repo_abspath);
811 04ca23f4 2018-07-16 stsp len = strlen(path);
812 04ca23f4 2018-07-16 stsp
813 04ca23f4 2018-07-16 stsp
814 04ca23f4 2018-07-16 stsp if (strcmp(path, repo_abspath) == 0) {
815 04ca23f4 2018-07-16 stsp free(path);
816 04ca23f4 2018-07-16 stsp path = strdup("");
817 04ca23f4 2018-07-16 stsp if (path == NULL) {
818 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
819 04ca23f4 2018-07-16 stsp goto done;
820 04ca23f4 2018-07-16 stsp }
821 65aa7d1c 2020-01-28 stsp } else if (len > repolen &&
822 65aa7d1c 2020-01-28 stsp got_path_is_child(path, repo_abspath, repolen)) {
823 04ca23f4 2018-07-16 stsp /* Matched an on-disk path inside repository. */
824 04ca23f4 2018-07-16 stsp if (got_repo_is_bare(repo)) {
825 04ca23f4 2018-07-16 stsp /*
826 04ca23f4 2018-07-16 stsp * Matched an on-disk path inside repository
827 eef9542c 2020-10-22 stsp * database. Treat input as repository-relative.
828 04ca23f4 2018-07-16 stsp */
829 eef9542c 2020-10-22 stsp free(path);
830 eef9542c 2020-10-22 stsp path = canonpath;
831 eef9542c 2020-10-22 stsp canonpath = NULL;
832 04ca23f4 2018-07-16 stsp } else {
833 04ca23f4 2018-07-16 stsp char *child;
834 04ca23f4 2018-07-16 stsp /* Strip common prefix with repository path. */
835 04ca23f4 2018-07-16 stsp err = got_path_skip_common_ancestor(&child,
836 04ca23f4 2018-07-16 stsp repo_abspath, path);
837 04ca23f4 2018-07-16 stsp if (err)
838 04ca23f4 2018-07-16 stsp goto done;
839 04ca23f4 2018-07-16 stsp free(path);
840 04ca23f4 2018-07-16 stsp path = child;
841 04ca23f4 2018-07-16 stsp }
842 04ca23f4 2018-07-16 stsp } else {
843 04ca23f4 2018-07-16 stsp /*
844 04ca23f4 2018-07-16 stsp * Matched unrelated on-disk path.
845 eef9542c 2020-10-22 stsp * Treat input as repository-relative.
846 04ca23f4 2018-07-16 stsp */
847 eef9542c 2020-10-22 stsp free(path);
848 eef9542c 2020-10-22 stsp path = canonpath;
849 eef9542c 2020-10-22 stsp canonpath = NULL;
850 04ca23f4 2018-07-16 stsp }
851 04ca23f4 2018-07-16 stsp }
852 04ca23f4 2018-07-16 stsp
853 04ca23f4 2018-07-16 stsp /* Make in-repository path absolute */
854 04ca23f4 2018-07-16 stsp if (path[0] != '/') {
855 04ca23f4 2018-07-16 stsp char *abspath;
856 04ca23f4 2018-07-16 stsp if (asprintf(&abspath, "/%s", path) == -1) {
857 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
858 04ca23f4 2018-07-16 stsp goto done;
859 04ca23f4 2018-07-16 stsp }
860 04ca23f4 2018-07-16 stsp free(path);
861 04ca23f4 2018-07-16 stsp path = abspath;
862 04ca23f4 2018-07-16 stsp }
863 04ca23f4 2018-07-16 stsp
864 04ca23f4 2018-07-16 stsp done:
865 04ca23f4 2018-07-16 stsp free(canonpath);
866 04ca23f4 2018-07-16 stsp if (err)
867 04ca23f4 2018-07-16 stsp free(path);
868 04ca23f4 2018-07-16 stsp else
869 04ca23f4 2018-07-16 stsp *in_repo_path = path;
870 1510f469 2018-09-09 stsp return err;
871 1510f469 2018-09-09 stsp }
872 1510f469 2018-09-09 stsp
873 e1a68182 2020-01-07 stsp static const struct got_error *
874 e1a68182 2020-01-07 stsp cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
875 e1a68182 2020-01-07 stsp const char *path_packidx)
876 1510f469 2018-09-09 stsp {
877 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
878 1510f469 2018-09-09 stsp int i;
879 1510f469 2018-09-09 stsp
880 1510f469 2018-09-09 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
881 1510f469 2018-09-09 stsp if (repo->packidx_cache[i] == NULL)
882 1510f469 2018-09-09 stsp break;
883 e1a68182 2020-01-07 stsp if (strcmp(repo->packidx_cache[i]->path_packidx,
884 e1a68182 2020-01-07 stsp path_packidx) == 0) {
885 e1a68182 2020-01-07 stsp return got_error(GOT_ERR_CACHE_DUP_ENTRY);
886 e1a68182 2020-01-07 stsp }
887 1510f469 2018-09-09 stsp }
888 1510f469 2018-09-09 stsp if (i == nitems(repo->packidx_cache)) {
889 1510f469 2018-09-09 stsp err = got_packidx_close(repo->packidx_cache[i - 1]);
890 1510f469 2018-09-09 stsp if (err)
891 1510f469 2018-09-09 stsp return err;
892 1510f469 2018-09-09 stsp }
893 1510f469 2018-09-09 stsp
894 15fe583f 2018-11-05 stsp /*
895 15fe583f 2018-11-05 stsp * Insert the new pack index at the front so it will
896 15fe583f 2018-11-05 stsp * be searched first in the future.
897 15fe583f 2018-11-05 stsp */
898 15fe583f 2018-11-05 stsp memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
899 15fe583f 2018-11-05 stsp sizeof(repo->packidx_cache) -
900 15fe583f 2018-11-05 stsp sizeof(repo->packidx_cache[0]));
901 15fe583f 2018-11-05 stsp repo->packidx_cache[0] = packidx;
902 15fe583f 2018-11-05 stsp
903 1510f469 2018-09-09 stsp return NULL;
904 1510f469 2018-09-09 stsp }
905 1510f469 2018-09-09 stsp
906 1510f469 2018-09-09 stsp static int
907 1510f469 2018-09-09 stsp is_packidx_filename(const char *name, size_t len)
908 1510f469 2018-09-09 stsp {
909 1510f469 2018-09-09 stsp if (len != GOT_PACKIDX_NAMELEN)
910 1510f469 2018-09-09 stsp return 0;
911 1510f469 2018-09-09 stsp
912 1510f469 2018-09-09 stsp if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
913 1510f469 2018-09-09 stsp return 0;
914 1510f469 2018-09-09 stsp
915 1510f469 2018-09-09 stsp if (strcmp(name + strlen(GOT_PACK_PREFIX) +
916 1510f469 2018-09-09 stsp SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
917 1510f469 2018-09-09 stsp return 0;
918 1510f469 2018-09-09 stsp
919 1510f469 2018-09-09 stsp return 1;
920 1510f469 2018-09-09 stsp }
921 1510f469 2018-09-09 stsp
922 1510f469 2018-09-09 stsp const struct got_error *
923 1510f469 2018-09-09 stsp got_repo_search_packidx(struct got_packidx **packidx, int *idx,
924 1510f469 2018-09-09 stsp struct got_repository *repo, struct got_object_id *id)
925 1510f469 2018-09-09 stsp {
926 1510f469 2018-09-09 stsp const struct got_error *err;
927 1510f469 2018-09-09 stsp char *path_packdir;
928 1510f469 2018-09-09 stsp DIR *packdir;
929 1510f469 2018-09-09 stsp struct dirent *dent;
930 1510f469 2018-09-09 stsp char *path_packidx;
931 1510f469 2018-09-09 stsp int i;
932 1510f469 2018-09-09 stsp
933 1510f469 2018-09-09 stsp /* Search pack index cache. */
934 1510f469 2018-09-09 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
935 1510f469 2018-09-09 stsp if (repo->packidx_cache[i] == NULL)
936 1510f469 2018-09-09 stsp break;
937 1510f469 2018-09-09 stsp *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
938 1510f469 2018-09-09 stsp if (*idx != -1) {
939 1510f469 2018-09-09 stsp *packidx = repo->packidx_cache[i];
940 87c1ed2b 2020-01-07 stsp /*
941 87c1ed2b 2020-01-07 stsp * Move this cache entry to the front. Repeatedly
942 87c1ed2b 2020-01-07 stsp * searching a wrong pack index can be expensive.
943 87c1ed2b 2020-01-07 stsp */
944 87c1ed2b 2020-01-07 stsp if (i > 0) {
945 87c1ed2b 2020-01-07 stsp struct got_packidx *p;
946 87c1ed2b 2020-01-07 stsp p = repo->packidx_cache[0];
947 87c1ed2b 2020-01-07 stsp repo->packidx_cache[0] = *packidx;
948 87c1ed2b 2020-01-07 stsp repo->packidx_cache[i] = p;
949 87c1ed2b 2020-01-07 stsp }
950 1510f469 2018-09-09 stsp return NULL;
951 1510f469 2018-09-09 stsp }
952 1510f469 2018-09-09 stsp }
953 1510f469 2018-09-09 stsp /* No luck. Search the filesystem. */
954 1510f469 2018-09-09 stsp
955 1510f469 2018-09-09 stsp path_packdir = got_repo_get_path_objects_pack(repo);
956 1510f469 2018-09-09 stsp if (path_packdir == NULL)
957 638f9024 2019-05-13 stsp return got_error_from_errno("got_repo_get_path_objects_pack");
958 1510f469 2018-09-09 stsp
959 1510f469 2018-09-09 stsp packdir = opendir(path_packdir);
960 1510f469 2018-09-09 stsp if (packdir == NULL) {
961 b90deaa1 2019-07-27 stsp if (errno == ENOENT)
962 b90deaa1 2019-07-27 stsp err = got_error_no_obj(id);
963 b90deaa1 2019-07-27 stsp else
964 b90deaa1 2019-07-27 stsp err = got_error_from_errno2("opendir", path_packdir);
965 1510f469 2018-09-09 stsp goto done;
966 1510f469 2018-09-09 stsp }
967 1510f469 2018-09-09 stsp
968 1510f469 2018-09-09 stsp while ((dent = readdir(packdir)) != NULL) {
969 e1a68182 2020-01-07 stsp int is_cached = 0;
970 e1a68182 2020-01-07 stsp
971 1510f469 2018-09-09 stsp if (!is_packidx_filename(dent->d_name, dent->d_namlen))
972 1510f469 2018-09-09 stsp continue;
973 1510f469 2018-09-09 stsp
974 1510f469 2018-09-09 stsp if (asprintf(&path_packidx, "%s/%s", path_packdir,
975 1510f469 2018-09-09 stsp dent->d_name) == -1) {
976 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
977 1510f469 2018-09-09 stsp goto done;
978 e1a68182 2020-01-07 stsp }
979 e1a68182 2020-01-07 stsp
980 e1a68182 2020-01-07 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
981 e1a68182 2020-01-07 stsp if (repo->packidx_cache[i] == NULL)
982 e1a68182 2020-01-07 stsp break;
983 e1a68182 2020-01-07 stsp if (strcmp(repo->packidx_cache[i]->path_packidx,
984 e1a68182 2020-01-07 stsp path_packidx) == 0) {
985 e1a68182 2020-01-07 stsp is_cached = 1;
986 e1a68182 2020-01-07 stsp break;
987 e1a68182 2020-01-07 stsp }
988 1510f469 2018-09-09 stsp }
989 e1a68182 2020-01-07 stsp if (is_cached) {
990 e1a68182 2020-01-07 stsp free(path_packidx);
991 e1a68182 2020-01-07 stsp continue; /* already searched */
992 e1a68182 2020-01-07 stsp }
993 1510f469 2018-09-09 stsp
994 1510f469 2018-09-09 stsp err = got_packidx_open(packidx, path_packidx, 0);
995 e1a68182 2020-01-07 stsp if (err) {
996 e1a68182 2020-01-07 stsp free(path_packidx);
997 e1a68182 2020-01-07 stsp goto done;
998 e1a68182 2020-01-07 stsp }
999 e1a68182 2020-01-07 stsp
1000 e1a68182 2020-01-07 stsp err = cache_packidx(repo, *packidx, path_packidx);
1001 1510f469 2018-09-09 stsp free(path_packidx);
1002 1510f469 2018-09-09 stsp if (err)
1003 1510f469 2018-09-09 stsp goto done;
1004 1510f469 2018-09-09 stsp
1005 1510f469 2018-09-09 stsp *idx = got_packidx_get_object_idx(*packidx, id);
1006 1510f469 2018-09-09 stsp if (*idx != -1) {
1007 1510f469 2018-09-09 stsp err = NULL; /* found the object */
1008 1510f469 2018-09-09 stsp goto done;
1009 1510f469 2018-09-09 stsp }
1010 1510f469 2018-09-09 stsp }
1011 1510f469 2018-09-09 stsp
1012 91a3d81f 2018-11-11 stsp err = got_error_no_obj(id);
1013 1510f469 2018-09-09 stsp done:
1014 1510f469 2018-09-09 stsp free(path_packdir);
1015 d69bcdf7 2019-06-28 stsp if (packdir && closedir(packdir) != 0 && err == NULL)
1016 638f9024 2019-05-13 stsp err = got_error_from_errno("closedir");
1017 04ca23f4 2018-07-16 stsp return err;
1018 04ca23f4 2018-07-16 stsp }
1019 1510f469 2018-09-09 stsp
1020 1510f469 2018-09-09 stsp static const struct got_error *
1021 1510f469 2018-09-09 stsp read_packfile_hdr(int fd, struct got_packidx *packidx)
1022 1510f469 2018-09-09 stsp {
1023 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
1024 78fb0967 2020-09-09 naddy uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1025 1510f469 2018-09-09 stsp struct got_packfile_hdr hdr;
1026 1510f469 2018-09-09 stsp ssize_t n;
1027 1510f469 2018-09-09 stsp
1028 1510f469 2018-09-09 stsp n = read(fd, &hdr, sizeof(hdr));
1029 1510f469 2018-09-09 stsp if (n < 0)
1030 638f9024 2019-05-13 stsp return got_error_from_errno("read");
1031 1510f469 2018-09-09 stsp if (n != sizeof(hdr))
1032 1510f469 2018-09-09 stsp return got_error(GOT_ERR_BAD_PACKFILE);
1033 1510f469 2018-09-09 stsp
1034 78fb0967 2020-09-09 naddy if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1035 78fb0967 2020-09-09 naddy be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1036 78fb0967 2020-09-09 naddy be32toh(hdr.nobjects) != totobj)
1037 1510f469 2018-09-09 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
1038 1510f469 2018-09-09 stsp
1039 1510f469 2018-09-09 stsp return err;
1040 1510f469 2018-09-09 stsp }
1041 1510f469 2018-09-09 stsp
1042 1510f469 2018-09-09 stsp static const struct got_error *
1043 1510f469 2018-09-09 stsp open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
1044 1510f469 2018-09-09 stsp {
1045 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
1046 1510f469 2018-09-09 stsp
1047 a5b57ccf 2019-04-11 stsp *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
1048 1510f469 2018-09-09 stsp if (*fd == -1)
1049 638f9024 2019-05-13 stsp return got_error_from_errno2("open", path_packfile);
1050 1510f469 2018-09-09 stsp
1051 1510f469 2018-09-09 stsp if (packidx) {
1052 1510f469 2018-09-09 stsp err = read_packfile_hdr(*fd, packidx);
1053 1510f469 2018-09-09 stsp if (err) {
1054 1510f469 2018-09-09 stsp close(*fd);
1055 1510f469 2018-09-09 stsp *fd = -1;
1056 1510f469 2018-09-09 stsp }
1057 1510f469 2018-09-09 stsp }
1058 1510f469 2018-09-09 stsp
1059 1510f469 2018-09-09 stsp return err;
1060 1510f469 2018-09-09 stsp }
1061 1510f469 2018-09-09 stsp
1062 1510f469 2018-09-09 stsp const struct got_error *
1063 1510f469 2018-09-09 stsp got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1064 1510f469 2018-09-09 stsp const char *path_packfile, struct got_packidx *packidx)
1065 1510f469 2018-09-09 stsp {
1066 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
1067 1510f469 2018-09-09 stsp struct got_pack *pack = NULL;
1068 ff563a3d 2019-05-23 stsp struct stat sb;
1069 1510f469 2018-09-09 stsp int i;
1070 1510f469 2018-09-09 stsp
1071 1510f469 2018-09-09 stsp if (packp)
1072 1510f469 2018-09-09 stsp *packp = NULL;
1073 1510f469 2018-09-09 stsp
1074 1510f469 2018-09-09 stsp for (i = 0; i < nitems(repo->packs); i++) {
1075 1510f469 2018-09-09 stsp pack = &repo->packs[i];
1076 1510f469 2018-09-09 stsp if (pack->path_packfile == NULL)
1077 1510f469 2018-09-09 stsp break;
1078 1510f469 2018-09-09 stsp if (strcmp(pack->path_packfile, path_packfile) == 0)
1079 e1a68182 2020-01-07 stsp return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1080 1510f469 2018-09-09 stsp }
1081 1510f469 2018-09-09 stsp
1082 1510f469 2018-09-09 stsp if (i == nitems(repo->packs) - 1) {
1083 1510f469 2018-09-09 stsp err = got_pack_close(&repo->packs[i - 1]);
1084 1510f469 2018-09-09 stsp if (err)
1085 1510f469 2018-09-09 stsp return err;
1086 1510f469 2018-09-09 stsp memmove(&repo->packs[1], &repo->packs[0],
1087 1510f469 2018-09-09 stsp sizeof(repo->packs) - sizeof(repo->packs[0]));
1088 1510f469 2018-09-09 stsp i = 0;
1089 1510f469 2018-09-09 stsp }
1090 1510f469 2018-09-09 stsp
1091 1510f469 2018-09-09 stsp pack = &repo->packs[i];
1092 1510f469 2018-09-09 stsp
1093 1510f469 2018-09-09 stsp pack->path_packfile = strdup(path_packfile);
1094 1510f469 2018-09-09 stsp if (pack->path_packfile == NULL) {
1095 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1096 1510f469 2018-09-09 stsp goto done;
1097 1510f469 2018-09-09 stsp }
1098 1510f469 2018-09-09 stsp
1099 1510f469 2018-09-09 stsp err = open_packfile(&pack->fd, path_packfile, packidx);
1100 1510f469 2018-09-09 stsp if (err)
1101 1510f469 2018-09-09 stsp goto done;
1102 1510f469 2018-09-09 stsp
1103 ff563a3d 2019-05-23 stsp if (fstat(pack->fd, &sb) != 0) {
1104 ff563a3d 2019-05-23 stsp err = got_error_from_errno("fstat");
1105 1510f469 2018-09-09 stsp goto done;
1106 ff563a3d 2019-05-23 stsp }
1107 ff563a3d 2019-05-23 stsp pack->filesize = sb.st_size;
1108 90636195 2018-09-11 stsp
1109 90636195 2018-09-11 stsp pack->privsep_child = NULL;
1110 1510f469 2018-09-09 stsp
1111 1510f469 2018-09-09 stsp #ifndef GOT_PACK_NO_MMAP
1112 1510f469 2018-09-09 stsp pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1113 1510f469 2018-09-09 stsp pack->fd, 0);
1114 3a11398b 2019-02-21 stsp if (pack->map == MAP_FAILED) {
1115 3a11398b 2019-02-21 stsp if (errno != ENOMEM) {
1116 638f9024 2019-05-13 stsp err = got_error_from_errno("mmap");
1117 3a11398b 2019-02-21 stsp goto done;
1118 3a11398b 2019-02-21 stsp }
1119 1510f469 2018-09-09 stsp pack->map = NULL; /* fall back to read(2) */
1120 3a11398b 2019-02-21 stsp }
1121 1510f469 2018-09-09 stsp #endif
1122 1510f469 2018-09-09 stsp done:
1123 1510f469 2018-09-09 stsp if (err) {
1124 1510f469 2018-09-09 stsp if (pack) {
1125 1510f469 2018-09-09 stsp free(pack->path_packfile);
1126 1510f469 2018-09-09 stsp memset(pack, 0, sizeof(*pack));
1127 1510f469 2018-09-09 stsp }
1128 1510f469 2018-09-09 stsp } else if (packp)
1129 1510f469 2018-09-09 stsp *packp = pack;
1130 1510f469 2018-09-09 stsp return err;
1131 1510f469 2018-09-09 stsp }
1132 1510f469 2018-09-09 stsp
1133 1510f469 2018-09-09 stsp struct got_pack *
1134 1510f469 2018-09-09 stsp got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1135 1510f469 2018-09-09 stsp {
1136 1510f469 2018-09-09 stsp struct got_pack *pack = NULL;
1137 1510f469 2018-09-09 stsp int i;
1138 1510f469 2018-09-09 stsp
1139 1510f469 2018-09-09 stsp for (i = 0; i < nitems(repo->packs); i++) {
1140 1510f469 2018-09-09 stsp pack = &repo->packs[i];
1141 1510f469 2018-09-09 stsp if (pack->path_packfile == NULL)
1142 1510f469 2018-09-09 stsp break;
1143 1510f469 2018-09-09 stsp if (strcmp(pack->path_packfile, path_packfile) == 0)
1144 1510f469 2018-09-09 stsp return pack;
1145 2c7829a4 2019-06-17 stsp }
1146 2c7829a4 2019-06-17 stsp
1147 2c7829a4 2019-06-17 stsp return NULL;
1148 2c7829a4 2019-06-17 stsp }
1149 2c7829a4 2019-06-17 stsp
1150 2c7829a4 2019-06-17 stsp const struct got_error *
1151 2c7829a4 2019-06-17 stsp got_repo_init(const char *repo_path)
1152 2c7829a4 2019-06-17 stsp {
1153 2c7829a4 2019-06-17 stsp const struct got_error *err = NULL;
1154 2c7829a4 2019-06-17 stsp const char *dirnames[] = {
1155 2c7829a4 2019-06-17 stsp GOT_OBJECTS_DIR,
1156 2c7829a4 2019-06-17 stsp GOT_OBJECTS_PACK_DIR,
1157 2c7829a4 2019-06-17 stsp GOT_REFS_DIR,
1158 2c7829a4 2019-06-17 stsp };
1159 2c7829a4 2019-06-17 stsp const char *description_str = "Unnamed repository; "
1160 2c7829a4 2019-06-17 stsp "edit this file 'description' to name the repository.";
1161 5d67f40d 2019-11-08 stsp const char *headref_str = "ref: refs/heads/main";
1162 2c7829a4 2019-06-17 stsp const char *gitconfig_str = "[core]\n"
1163 2c7829a4 2019-06-17 stsp "\trepositoryformatversion = 0\n"
1164 2c7829a4 2019-06-17 stsp "\tfilemode = true\n"
1165 2c7829a4 2019-06-17 stsp "\tbare = true\n";
1166 2c7829a4 2019-06-17 stsp char *path;
1167 2c7829a4 2019-06-17 stsp int i;
1168 2c7829a4 2019-06-17 stsp
1169 2c7829a4 2019-06-17 stsp if (!got_path_dir_is_empty(repo_path))
1170 2c7829a4 2019-06-17 stsp return got_error(GOT_ERR_DIR_NOT_EMPTY);
1171 2c7829a4 2019-06-17 stsp
1172 2c7829a4 2019-06-17 stsp for (i = 0; i < nitems(dirnames); i++) {
1173 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1174 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
1175 2c7829a4 2019-06-17 stsp }
1176 2c7829a4 2019-06-17 stsp err = got_path_mkdir(path);
1177 2c7829a4 2019-06-17 stsp free(path);
1178 2c7829a4 2019-06-17 stsp if (err)
1179 2c7829a4 2019-06-17 stsp return err;
1180 1510f469 2018-09-09 stsp }
1181 1510f469 2018-09-09 stsp
1182 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1183 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
1184 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, description_str);
1185 2c7829a4 2019-06-17 stsp free(path);
1186 2c7829a4 2019-06-17 stsp if (err)
1187 2c7829a4 2019-06-17 stsp return err;
1188 2c7829a4 2019-06-17 stsp
1189 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1190 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
1191 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, headref_str);
1192 2c7829a4 2019-06-17 stsp free(path);
1193 2c7829a4 2019-06-17 stsp if (err)
1194 2c7829a4 2019-06-17 stsp return err;
1195 2c7829a4 2019-06-17 stsp
1196 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1197 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
1198 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, gitconfig_str);
1199 2c7829a4 2019-06-17 stsp free(path);
1200 2c7829a4 2019-06-17 stsp if (err)
1201 2c7829a4 2019-06-17 stsp return err;
1202 2c7829a4 2019-06-17 stsp
1203 1510f469 2018-09-09 stsp return NULL;
1204 e09a504c 2019-06-28 stsp }
1205 e09a504c 2019-06-28 stsp
1206 e09a504c 2019-06-28 stsp static const struct got_error *
1207 4277420a 2019-06-29 stsp match_packed_object(struct got_object_id **unique_id,
1208 dd88155e 2019-06-29 stsp struct got_repository *repo, const char *id_str_prefix, int obj_type)
1209 e09a504c 2019-06-28 stsp {
1210 e09a504c 2019-06-28 stsp const struct got_error *err = NULL;
1211 e09a504c 2019-06-28 stsp char *path_packdir;
1212 e09a504c 2019-06-28 stsp DIR *packdir;
1213 e09a504c 2019-06-28 stsp struct dirent *dent;
1214 e09a504c 2019-06-28 stsp char *path_packidx;
1215 dd88155e 2019-06-29 stsp struct got_object_id_queue matched_ids;
1216 e09a504c 2019-06-28 stsp
1217 dd88155e 2019-06-29 stsp SIMPLEQ_INIT(&matched_ids);
1218 dd88155e 2019-06-29 stsp
1219 e09a504c 2019-06-28 stsp path_packdir = got_repo_get_path_objects_pack(repo);
1220 e09a504c 2019-06-28 stsp if (path_packdir == NULL)
1221 e09a504c 2019-06-28 stsp return got_error_from_errno("got_repo_get_path_objects_pack");
1222 e09a504c 2019-06-28 stsp
1223 e09a504c 2019-06-28 stsp packdir = opendir(path_packdir);
1224 e09a504c 2019-06-28 stsp if (packdir == NULL) {
1225 e4167f30 2019-07-27 stsp if (errno != ENOENT)
1226 e4167f30 2019-07-27 stsp err = got_error_from_errno2("opendir", path_packdir);
1227 e09a504c 2019-06-28 stsp goto done;
1228 e09a504c 2019-06-28 stsp }
1229 e09a504c 2019-06-28 stsp
1230 e09a504c 2019-06-28 stsp while ((dent = readdir(packdir)) != NULL) {
1231 e09a504c 2019-06-28 stsp struct got_packidx *packidx;
1232 dd88155e 2019-06-29 stsp struct got_object_qid *qid;
1233 dd88155e 2019-06-29 stsp
1234 e09a504c 2019-06-28 stsp
1235 e09a504c 2019-06-28 stsp if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1236 e09a504c 2019-06-28 stsp continue;
1237 e09a504c 2019-06-28 stsp
1238 e09a504c 2019-06-28 stsp if (asprintf(&path_packidx, "%s/%s", path_packdir,
1239 e09a504c 2019-06-28 stsp dent->d_name) == -1) {
1240 e09a504c 2019-06-28 stsp err = got_error_from_errno("asprintf");
1241 4277420a 2019-06-29 stsp break;
1242 e09a504c 2019-06-28 stsp }
1243 e09a504c 2019-06-28 stsp
1244 e09a504c 2019-06-28 stsp err = got_packidx_open(&packidx, path_packidx, 0);
1245 e09a504c 2019-06-28 stsp free(path_packidx);
1246 e09a504c 2019-06-28 stsp if (err)
1247 4277420a 2019-06-29 stsp break;
1248 e09a504c 2019-06-28 stsp
1249 dd88155e 2019-06-29 stsp err = got_packidx_match_id_str_prefix(&matched_ids,
1250 4277420a 2019-06-29 stsp packidx, id_str_prefix);
1251 4277420a 2019-06-29 stsp if (err) {
1252 4277420a 2019-06-29 stsp got_packidx_close(packidx);
1253 4277420a 2019-06-29 stsp break;
1254 4277420a 2019-06-29 stsp }
1255 e09a504c 2019-06-28 stsp err = got_packidx_close(packidx);
1256 dd88155e 2019-06-29 stsp if (err)
1257 e09a504c 2019-06-28 stsp break;
1258 e09a504c 2019-06-28 stsp
1259 dd88155e 2019-06-29 stsp SIMPLEQ_FOREACH(qid, &matched_ids, entry) {
1260 dd88155e 2019-06-29 stsp if (obj_type != GOT_OBJ_TYPE_ANY) {
1261 dd88155e 2019-06-29 stsp int matched_type;
1262 dd88155e 2019-06-29 stsp err = got_object_get_type(&matched_type, repo,
1263 dd88155e 2019-06-29 stsp qid->id);
1264 dd88155e 2019-06-29 stsp if (err)
1265 dd88155e 2019-06-29 stsp goto done;
1266 dd88155e 2019-06-29 stsp if (matched_type != obj_type)
1267 dd88155e 2019-06-29 stsp continue;
1268 dd88155e 2019-06-29 stsp }
1269 4277420a 2019-06-29 stsp if (*unique_id == NULL) {
1270 dd88155e 2019-06-29 stsp *unique_id = got_object_id_dup(qid->id);
1271 dd88155e 2019-06-29 stsp if (*unique_id == NULL) {
1272 dd88155e 2019-06-29 stsp err = got_error_from_errno("malloc");
1273 dd88155e 2019-06-29 stsp goto done;
1274 dd88155e 2019-06-29 stsp }
1275 4277420a 2019-06-29 stsp } else {
1276 1accf02b 2020-01-05 stsp if (got_object_id_cmp(*unique_id, qid->id) == 0)
1277 1accf02b 2020-01-05 stsp continue; /* packed multiple times */
1278 e09a504c 2019-06-28 stsp err = got_error(GOT_ERR_AMBIGUOUS_ID);
1279 561c3678 2019-07-02 stsp goto done;
1280 e09a504c 2019-06-28 stsp }
1281 e09a504c 2019-06-28 stsp }
1282 e09a504c 2019-06-28 stsp }
1283 e09a504c 2019-06-28 stsp done:
1284 dd88155e 2019-06-29 stsp got_object_id_queue_free(&matched_ids);
1285 e09a504c 2019-06-28 stsp free(path_packdir);
1286 e09a504c 2019-06-28 stsp if (packdir && closedir(packdir) != 0 && err == NULL)
1287 e09a504c 2019-06-28 stsp err = got_error_from_errno("closedir");
1288 e09a504c 2019-06-28 stsp if (err) {
1289 e09a504c 2019-06-28 stsp free(*unique_id);
1290 e09a504c 2019-06-28 stsp *unique_id = NULL;
1291 e09a504c 2019-06-28 stsp }
1292 e09a504c 2019-06-28 stsp return err;
1293 e09a504c 2019-06-28 stsp }
1294 e09a504c 2019-06-28 stsp
1295 e09a504c 2019-06-28 stsp static const struct got_error *
1296 4277420a 2019-06-29 stsp match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1297 dd88155e 2019-06-29 stsp const char *object_dir, const char *id_str_prefix, int obj_type,
1298 e09a504c 2019-06-28 stsp struct got_repository *repo)
1299 e09a504c 2019-06-28 stsp {
1300 e09a504c 2019-06-28 stsp const struct got_error *err = NULL;
1301 e09a504c 2019-06-28 stsp char *path;
1302 e09a504c 2019-06-28 stsp DIR *dir = NULL;
1303 e09a504c 2019-06-28 stsp struct dirent *dent;
1304 e09a504c 2019-06-28 stsp struct got_object_id id;
1305 e09a504c 2019-06-28 stsp
1306 e09a504c 2019-06-28 stsp if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1307 e09a504c 2019-06-28 stsp err = got_error_from_errno("asprintf");
1308 e09a504c 2019-06-28 stsp goto done;
1309 e09a504c 2019-06-28 stsp }
1310 e09a504c 2019-06-28 stsp
1311 e09a504c 2019-06-28 stsp dir = opendir(path);
1312 e09a504c 2019-06-28 stsp if (dir == NULL) {
1313 4277420a 2019-06-29 stsp if (errno == ENOENT) {
1314 4277420a 2019-06-29 stsp err = NULL;
1315 4277420a 2019-06-29 stsp goto done;
1316 4277420a 2019-06-29 stsp }
1317 e09a504c 2019-06-28 stsp err = got_error_from_errno2("opendir", path);
1318 e09a504c 2019-06-28 stsp goto done;
1319 e09a504c 2019-06-28 stsp }
1320 e09a504c 2019-06-28 stsp while ((dent = readdir(dir)) != NULL) {
1321 e09a504c 2019-06-28 stsp char *id_str;
1322 5903ff6e 2019-06-29 stsp int cmp;
1323 5903ff6e 2019-06-29 stsp
1324 e09a504c 2019-06-28 stsp if (strcmp(dent->d_name, ".") == 0 ||
1325 e09a504c 2019-06-28 stsp strcmp(dent->d_name, "..") == 0)
1326 e09a504c 2019-06-28 stsp continue;
1327 e09a504c 2019-06-28 stsp
1328 e09a504c 2019-06-28 stsp if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1329 e09a504c 2019-06-28 stsp err = got_error_from_errno("asprintf");
1330 e09a504c 2019-06-28 stsp goto done;
1331 e09a504c 2019-06-28 stsp }
1332 e09a504c 2019-06-28 stsp
1333 e09a504c 2019-06-28 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
1334 e09a504c 2019-06-28 stsp continue;
1335 e09a504c 2019-06-28 stsp
1336 52d1d0d9 2019-07-07 stsp /*
1337 52d1d0d9 2019-07-07 stsp * Directory entries do not necessarily appear in
1338 52d1d0d9 2019-07-07 stsp * sorted order, so we must iterate over all of them.
1339 52d1d0d9 2019-07-07 stsp */
1340 5903ff6e 2019-06-29 stsp cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1341 52d1d0d9 2019-07-07 stsp if (cmp != 0) {
1342 e09a504c 2019-06-28 stsp free(id_str);
1343 e09a504c 2019-06-28 stsp continue;
1344 e09a504c 2019-06-28 stsp }
1345 e09a504c 2019-06-28 stsp
1346 e09a504c 2019-06-28 stsp if (*unique_id == NULL) {
1347 dd88155e 2019-06-29 stsp if (obj_type != GOT_OBJ_TYPE_ANY) {
1348 dd88155e 2019-06-29 stsp int matched_type;
1349 dd88155e 2019-06-29 stsp err = got_object_get_type(&matched_type, repo,
1350 dd88155e 2019-06-29 stsp &id);
1351 dd88155e 2019-06-29 stsp if (err)
1352 dd88155e 2019-06-29 stsp goto done;
1353 dd88155e 2019-06-29 stsp if (matched_type != obj_type)
1354 dd88155e 2019-06-29 stsp continue;
1355 dd88155e 2019-06-29 stsp }
1356 e09a504c 2019-06-28 stsp *unique_id = got_object_id_dup(&id);
1357 e09a504c 2019-06-28 stsp if (*unique_id == NULL) {
1358 e09a504c 2019-06-28 stsp err = got_error_from_errno("got_object_id_dup");
1359 e09a504c 2019-06-28 stsp free(id_str);
1360 e09a504c 2019-06-28 stsp goto done;
1361 e09a504c 2019-06-28 stsp }
1362 e09a504c 2019-06-28 stsp } else {
1363 1accf02b 2020-01-05 stsp if (got_object_id_cmp(*unique_id, &id) == 0)
1364 1accf02b 2020-01-05 stsp continue; /* both packed and loose */
1365 e09a504c 2019-06-28 stsp err = got_error(GOT_ERR_AMBIGUOUS_ID);
1366 e09a504c 2019-06-28 stsp free(id_str);
1367 e09a504c 2019-06-28 stsp goto done;
1368 e09a504c 2019-06-28 stsp }
1369 e09a504c 2019-06-28 stsp }
1370 e09a504c 2019-06-28 stsp done:
1371 b2df341b 2019-06-29 stsp if (dir && closedir(dir) != 0 && err == NULL)
1372 b2df341b 2019-06-29 stsp err = got_error_from_errno("closedir");
1373 e09a504c 2019-06-28 stsp if (err) {
1374 e09a504c 2019-06-28 stsp free(*unique_id);
1375 e09a504c 2019-06-28 stsp *unique_id = NULL;
1376 e09a504c 2019-06-28 stsp }
1377 e09a504c 2019-06-28 stsp free(path);
1378 e09a504c 2019-06-28 stsp return err;
1379 1510f469 2018-09-09 stsp }
1380 e09a504c 2019-06-28 stsp
1381 e09a504c 2019-06-28 stsp const struct got_error *
1382 4277420a 2019-06-29 stsp got_repo_match_object_id_prefix(struct got_object_id **id,
1383 dd88155e 2019-06-29 stsp const char *id_str_prefix, int obj_type, struct got_repository *repo)
1384 e09a504c 2019-06-28 stsp {
1385 e09a504c 2019-06-28 stsp const struct got_error *err = NULL;
1386 e09a504c 2019-06-28 stsp char *path_objects = got_repo_get_path_objects(repo);
1387 e09a504c 2019-06-28 stsp char *object_dir = NULL;
1388 e09a504c 2019-06-28 stsp size_t len;
1389 4277420a 2019-06-29 stsp int i;
1390 e09a504c 2019-06-28 stsp
1391 4277420a 2019-06-29 stsp *id = NULL;
1392 4277420a 2019-06-29 stsp
1393 4277420a 2019-06-29 stsp for (i = 0; i < strlen(id_str_prefix); i++) {
1394 4277420a 2019-06-29 stsp if (isxdigit((unsigned char)id_str_prefix[i]))
1395 4277420a 2019-06-29 stsp continue;
1396 6dd1ece6 2019-11-10 stsp return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1397 4277420a 2019-06-29 stsp }
1398 4277420a 2019-06-29 stsp
1399 e09a504c 2019-06-28 stsp len = strlen(id_str_prefix);
1400 e09a504c 2019-06-28 stsp if (len >= 2) {
1401 dd88155e 2019-06-29 stsp err = match_packed_object(id, repo, id_str_prefix, obj_type);
1402 4277420a 2019-06-29 stsp if (err)
1403 83c8b3b8 2019-06-29 stsp goto done;
1404 e09a504c 2019-06-28 stsp object_dir = strndup(id_str_prefix, 2);
1405 83c8b3b8 2019-06-29 stsp if (object_dir == NULL) {
1406 83c8b3b8 2019-06-29 stsp err = got_error_from_errno("strdup");
1407 83c8b3b8 2019-06-29 stsp goto done;
1408 83c8b3b8 2019-06-29 stsp }
1409 4277420a 2019-06-29 stsp err = match_loose_object(id, path_objects, object_dir,
1410 dd88155e 2019-06-29 stsp id_str_prefix, obj_type, repo);
1411 e09a504c 2019-06-28 stsp } else if (len == 1) {
1412 e09a504c 2019-06-28 stsp int i;
1413 e09a504c 2019-06-28 stsp for (i = 0; i < 0xf; i++) {
1414 e09a504c 2019-06-28 stsp if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1415 83c8b3b8 2019-06-29 stsp == -1) {
1416 83c8b3b8 2019-06-29 stsp err = got_error_from_errno("asprintf");
1417 83c8b3b8 2019-06-29 stsp goto done;
1418 83c8b3b8 2019-06-29 stsp }
1419 dd88155e 2019-06-29 stsp err = match_packed_object(id, repo, object_dir,
1420 dd88155e 2019-06-29 stsp obj_type);
1421 4277420a 2019-06-29 stsp if (err)
1422 83c8b3b8 2019-06-29 stsp goto done;
1423 4277420a 2019-06-29 stsp err = match_loose_object(id, path_objects, object_dir,
1424 dd88155e 2019-06-29 stsp id_str_prefix, obj_type, repo);
1425 e09a504c 2019-06-28 stsp if (err)
1426 83c8b3b8 2019-06-29 stsp goto done;
1427 e09a504c 2019-06-28 stsp }
1428 83c8b3b8 2019-06-29 stsp } else {
1429 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1430 83c8b3b8 2019-06-29 stsp goto done;
1431 83c8b3b8 2019-06-29 stsp }
1432 83c8b3b8 2019-06-29 stsp done:
1433 e09a504c 2019-06-28 stsp free(object_dir);
1434 4277420a 2019-06-29 stsp if (err) {
1435 4277420a 2019-06-29 stsp free(*id);
1436 4277420a 2019-06-29 stsp *id = NULL;
1437 4277420a 2019-06-29 stsp } else if (*id == NULL)
1438 86d8a25a 2020-04-19 stsp err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1439 303e2782 2019-08-09 stsp
1440 303e2782 2019-08-09 stsp return err;
1441 303e2782 2019-08-09 stsp }
1442 303e2782 2019-08-09 stsp
1443 303e2782 2019-08-09 stsp const struct got_error *
1444 71a27632 2020-01-15 stsp got_repo_match_object_id(struct got_object_id **id, char **label,
1445 71a27632 2020-01-15 stsp const char *id_str, int obj_type, int resolve_tags,
1446 71a27632 2020-01-15 stsp struct got_repository *repo)
1447 71a27632 2020-01-15 stsp {
1448 71a27632 2020-01-15 stsp const struct got_error *err;
1449 71a27632 2020-01-15 stsp struct got_tag_object *tag;
1450 71a27632 2020-01-15 stsp struct got_reference *ref = NULL;
1451 71a27632 2020-01-15 stsp
1452 71a27632 2020-01-15 stsp *id = NULL;
1453 71a27632 2020-01-15 stsp if (label)
1454 71a27632 2020-01-15 stsp *label = NULL;
1455 71a27632 2020-01-15 stsp
1456 71a27632 2020-01-15 stsp if (resolve_tags) {
1457 71a27632 2020-01-15 stsp err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
1458 71a27632 2020-01-15 stsp repo);
1459 71a27632 2020-01-15 stsp if (err == NULL) {
1460 71a27632 2020-01-15 stsp *id = got_object_id_dup(
1461 71a27632 2020-01-15 stsp got_object_tag_get_object_id(tag));
1462 71a27632 2020-01-15 stsp if (*id == NULL)
1463 71a27632 2020-01-15 stsp err = got_error_from_errno("got_object_id_dup");
1464 71a27632 2020-01-15 stsp else if (label && asprintf(label, "refs/tags/%s",
1465 71a27632 2020-01-15 stsp got_object_tag_get_name(tag)) == -1) {
1466 71a27632 2020-01-15 stsp err = got_error_from_errno("asprintf");
1467 71a27632 2020-01-15 stsp free(*id);
1468 71a27632 2020-01-15 stsp *id = NULL;
1469 71a27632 2020-01-15 stsp }
1470 71a27632 2020-01-15 stsp got_object_tag_close(tag);
1471 71a27632 2020-01-15 stsp return err;
1472 71a27632 2020-01-15 stsp } else if (err->code != GOT_ERR_OBJ_TYPE &&
1473 71a27632 2020-01-15 stsp err->code != GOT_ERR_NO_OBJ)
1474 71a27632 2020-01-15 stsp return err;
1475 71a27632 2020-01-15 stsp }
1476 71a27632 2020-01-15 stsp
1477 71a27632 2020-01-15 stsp err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1478 71a27632 2020-01-15 stsp if (err) {
1479 71a27632 2020-01-15 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1480 71a27632 2020-01-15 stsp return err;
1481 71a27632 2020-01-15 stsp err = got_ref_open(&ref, repo, id_str, 0);
1482 71a27632 2020-01-15 stsp if (err != NULL)
1483 71a27632 2020-01-15 stsp goto done;
1484 71a27632 2020-01-15 stsp if (label) {
1485 71a27632 2020-01-15 stsp *label = strdup(got_ref_get_name(ref));
1486 71a27632 2020-01-15 stsp if (*label == NULL) {
1487 71a27632 2020-01-15 stsp err = got_error_from_errno("strdup");
1488 71a27632 2020-01-15 stsp goto done;
1489 71a27632 2020-01-15 stsp }
1490 71a27632 2020-01-15 stsp }
1491 71a27632 2020-01-15 stsp err = got_ref_resolve(id, repo, ref);
1492 71a27632 2020-01-15 stsp } else if (label) {
1493 71a27632 2020-01-15 stsp err = got_object_id_str(label, *id);
1494 71a27632 2020-01-15 stsp if (*label == NULL) {
1495 71a27632 2020-01-15 stsp err = got_error_from_errno("strdup");
1496 71a27632 2020-01-15 stsp goto done;
1497 71a27632 2020-01-15 stsp }
1498 71a27632 2020-01-15 stsp }
1499 71a27632 2020-01-15 stsp done:
1500 71a27632 2020-01-15 stsp if (ref)
1501 71a27632 2020-01-15 stsp got_ref_close(ref);
1502 71a27632 2020-01-15 stsp return err;
1503 71a27632 2020-01-15 stsp }
1504 71a27632 2020-01-15 stsp
1505 71a27632 2020-01-15 stsp const struct got_error *
1506 303e2782 2019-08-09 stsp got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1507 303e2782 2019-08-09 stsp int obj_type, struct got_repository *repo)
1508 303e2782 2019-08-09 stsp {
1509 303e2782 2019-08-09 stsp const struct got_error *err;
1510 303e2782 2019-08-09 stsp struct got_reflist_head refs;
1511 303e2782 2019-08-09 stsp struct got_reflist_entry *re;
1512 303e2782 2019-08-09 stsp struct got_object_id *tag_id;
1513 303e2782 2019-08-09 stsp
1514 303e2782 2019-08-09 stsp SIMPLEQ_INIT(&refs);
1515 303e2782 2019-08-09 stsp *tag = NULL;
1516 303e2782 2019-08-09 stsp
1517 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_by_name, NULL);
1518 303e2782 2019-08-09 stsp if (err)
1519 303e2782 2019-08-09 stsp return err;
1520 303e2782 2019-08-09 stsp
1521 303e2782 2019-08-09 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
1522 303e2782 2019-08-09 stsp const char *refname;
1523 303e2782 2019-08-09 stsp refname = got_ref_get_name(re->ref);
1524 29606af7 2019-08-23 stsp if (got_ref_is_symbolic(re->ref))
1525 303e2782 2019-08-09 stsp continue;
1526 29606af7 2019-08-23 stsp refname += strlen("refs/tags/");
1527 303e2782 2019-08-09 stsp if (strcmp(refname, name) != 0)
1528 303e2782 2019-08-09 stsp continue;
1529 303e2782 2019-08-09 stsp err = got_ref_resolve(&tag_id, repo, re->ref);
1530 303e2782 2019-08-09 stsp if (err)
1531 303e2782 2019-08-09 stsp break;
1532 303e2782 2019-08-09 stsp err = got_object_open_as_tag(tag, repo, tag_id);
1533 303e2782 2019-08-09 stsp free(tag_id);
1534 303e2782 2019-08-09 stsp if (err)
1535 303e2782 2019-08-09 stsp break;
1536 d24820bf 2019-08-11 stsp if (obj_type == GOT_OBJ_TYPE_ANY ||
1537 d24820bf 2019-08-11 stsp got_object_tag_get_object_type(*tag) == obj_type)
1538 303e2782 2019-08-09 stsp break;
1539 303e2782 2019-08-09 stsp got_object_tag_close(*tag);
1540 303e2782 2019-08-09 stsp *tag = NULL;
1541 303e2782 2019-08-09 stsp }
1542 4277420a 2019-06-29 stsp
1543 303e2782 2019-08-09 stsp got_ref_list_free(&refs);
1544 303e2782 2019-08-09 stsp if (err == NULL && *tag == NULL)
1545 ded8fbb8 2020-04-19 stsp err = got_error_path(name, GOT_ERR_NO_OBJ);
1546 e09a504c 2019-06-28 stsp return err;
1547 e09a504c 2019-06-28 stsp }
1548 7a1d6b72 2020-01-15 stsp
1549 3ce1b845 2019-07-15 stsp static const struct got_error *
1550 3ce1b845 2019-07-15 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1551 3ce1b845 2019-07-15 stsp const char *name, mode_t mode, struct got_object_id *blob_id)
1552 3ce1b845 2019-07-15 stsp {
1553 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
1554 3ce1b845 2019-07-15 stsp
1555 3ce1b845 2019-07-15 stsp *new_te = NULL;
1556 3ce1b845 2019-07-15 stsp
1557 3ce1b845 2019-07-15 stsp *new_te = calloc(1, sizeof(**new_te));
1558 3ce1b845 2019-07-15 stsp if (*new_te == NULL)
1559 3ce1b845 2019-07-15 stsp return got_error_from_errno("calloc");
1560 3ce1b845 2019-07-15 stsp
1561 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1562 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
1563 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
1564 3ce1b845 2019-07-15 stsp goto done;
1565 3ce1b845 2019-07-15 stsp }
1566 3ce1b845 2019-07-15 stsp
1567 e8863bdc 2020-07-23 stsp if (S_ISLNK(mode)) {
1568 e8863bdc 2020-07-23 stsp (*new_te)->mode = S_IFLNK;
1569 e8863bdc 2020-07-23 stsp } else {
1570 e8863bdc 2020-07-23 stsp (*new_te)->mode = S_IFREG;
1571 e8863bdc 2020-07-23 stsp (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1572 e8863bdc 2020-07-23 stsp }
1573 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1574 3ce1b845 2019-07-15 stsp done:
1575 3ce1b845 2019-07-15 stsp if (err && *new_te) {
1576 56e0773d 2019-11-28 stsp free(*new_te);
1577 3ce1b845 2019-07-15 stsp *new_te = NULL;
1578 3ce1b845 2019-07-15 stsp }
1579 3ce1b845 2019-07-15 stsp return err;
1580 3ce1b845 2019-07-15 stsp }
1581 3ce1b845 2019-07-15 stsp
1582 3ce1b845 2019-07-15 stsp static const struct got_error *
1583 3ce1b845 2019-07-15 stsp import_file(struct got_tree_entry **new_te, struct dirent *de,
1584 3ce1b845 2019-07-15 stsp const char *path, struct got_repository *repo)
1585 3ce1b845 2019-07-15 stsp {
1586 3ce1b845 2019-07-15 stsp const struct got_error *err;
1587 3ce1b845 2019-07-15 stsp struct got_object_id *blob_id = NULL;
1588 3ce1b845 2019-07-15 stsp char *filepath;
1589 3ce1b845 2019-07-15 stsp struct stat sb;
1590 3ce1b845 2019-07-15 stsp
1591 3ce1b845 2019-07-15 stsp if (asprintf(&filepath, "%s%s%s", path,
1592 3ce1b845 2019-07-15 stsp path[0] == '\0' ? "" : "/", de->d_name) == -1)
1593 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
1594 3ce1b845 2019-07-15 stsp
1595 3ce1b845 2019-07-15 stsp if (lstat(filepath, &sb) != 0) {
1596 3ce1b845 2019-07-15 stsp err = got_error_from_errno2("lstat", path);
1597 3ce1b845 2019-07-15 stsp goto done;
1598 3ce1b845 2019-07-15 stsp }
1599 3ce1b845 2019-07-15 stsp
1600 3ce1b845 2019-07-15 stsp err = got_object_blob_create(&blob_id, filepath, repo);
1601 3ce1b845 2019-07-15 stsp if (err)
1602 3ce1b845 2019-07-15 stsp goto done;
1603 3ce1b845 2019-07-15 stsp
1604 3ce1b845 2019-07-15 stsp err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1605 3ce1b845 2019-07-15 stsp blob_id);
1606 3ce1b845 2019-07-15 stsp done:
1607 3ce1b845 2019-07-15 stsp free(filepath);
1608 3ce1b845 2019-07-15 stsp if (err)
1609 3ce1b845 2019-07-15 stsp free(blob_id);
1610 3ce1b845 2019-07-15 stsp return err;
1611 3ce1b845 2019-07-15 stsp }
1612 3ce1b845 2019-07-15 stsp
1613 3ce1b845 2019-07-15 stsp static const struct got_error *
1614 3ce1b845 2019-07-15 stsp insert_tree_entry(struct got_tree_entry *new_te,
1615 3ce1b845 2019-07-15 stsp struct got_pathlist_head *paths)
1616 3ce1b845 2019-07-15 stsp {
1617 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
1618 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *new_pe;
1619 3ce1b845 2019-07-15 stsp
1620 3ce1b845 2019-07-15 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1621 3ce1b845 2019-07-15 stsp if (err)
1622 3ce1b845 2019-07-15 stsp return err;
1623 3ce1b845 2019-07-15 stsp if (new_pe == NULL)
1624 3ce1b845 2019-07-15 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
1625 3ce1b845 2019-07-15 stsp return NULL;
1626 3ce1b845 2019-07-15 stsp }
1627 3ce1b845 2019-07-15 stsp
1628 3ce1b845 2019-07-15 stsp static const struct got_error *write_tree(struct got_object_id **,
1629 3ce1b845 2019-07-15 stsp const char *, struct got_pathlist_head *, struct got_repository *,
1630 3ce1b845 2019-07-15 stsp got_repo_import_cb progress_cb, void *progress_arg);
1631 3ce1b845 2019-07-15 stsp
1632 3ce1b845 2019-07-15 stsp static const struct got_error *
1633 3ce1b845 2019-07-15 stsp import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1634 3ce1b845 2019-07-15 stsp const char *path, struct got_pathlist_head *ignores,
1635 3ce1b845 2019-07-15 stsp struct got_repository *repo,
1636 3ce1b845 2019-07-15 stsp got_repo_import_cb progress_cb, void *progress_arg)
1637 3ce1b845 2019-07-15 stsp {
1638 3ce1b845 2019-07-15 stsp const struct got_error *err;
1639 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
1640 3ce1b845 2019-07-15 stsp char *subdirpath;
1641 3ce1b845 2019-07-15 stsp
1642 3ce1b845 2019-07-15 stsp if (asprintf(&subdirpath, "%s%s%s", path,
1643 3ce1b845 2019-07-15 stsp path[0] == '\0' ? "" : "/", de->d_name) == -1)
1644 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
1645 3ce1b845 2019-07-15 stsp
1646 3ce1b845 2019-07-15 stsp (*new_te) = calloc(1, sizeof(**new_te));
1647 d6fca0ba 2019-09-15 hiltjo if (*new_te == NULL)
1648 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
1649 3ce1b845 2019-07-15 stsp (*new_te)->mode = S_IFDIR;
1650 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1651 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
1652 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
1653 3ce1b845 2019-07-15 stsp goto done;
1654 3ce1b845 2019-07-15 stsp }
1655 56e0773d 2019-11-28 stsp err = write_tree(&id, subdirpath, ignores, repo,
1656 3ce1b845 2019-07-15 stsp progress_cb, progress_arg);
1657 56e0773d 2019-11-28 stsp if (err)
1658 56e0773d 2019-11-28 stsp goto done;
1659 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1660 56e0773d 2019-11-28 stsp
1661 3ce1b845 2019-07-15 stsp done:
1662 56e0773d 2019-11-28 stsp free(id);
1663 3ce1b845 2019-07-15 stsp free(subdirpath);
1664 3ce1b845 2019-07-15 stsp if (err) {
1665 56e0773d 2019-11-28 stsp free(*new_te);
1666 3ce1b845 2019-07-15 stsp *new_te = NULL;
1667 3ce1b845 2019-07-15 stsp }
1668 3ce1b845 2019-07-15 stsp return err;
1669 3ce1b845 2019-07-15 stsp }
1670 3ce1b845 2019-07-15 stsp
1671 3ce1b845 2019-07-15 stsp static const struct got_error *
1672 3ce1b845 2019-07-15 stsp write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1673 3ce1b845 2019-07-15 stsp struct got_pathlist_head *ignores, struct got_repository *repo,
1674 3ce1b845 2019-07-15 stsp got_repo_import_cb progress_cb, void *progress_arg)
1675 3ce1b845 2019-07-15 stsp {
1676 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
1677 3ce1b845 2019-07-15 stsp DIR *dir;
1678 3ce1b845 2019-07-15 stsp struct dirent *de;
1679 56e0773d 2019-11-28 stsp int nentries;
1680 3ce1b845 2019-07-15 stsp struct got_tree_entry *new_te = NULL;
1681 3ce1b845 2019-07-15 stsp struct got_pathlist_head paths;
1682 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
1683 3ce1b845 2019-07-15 stsp
1684 3ce1b845 2019-07-15 stsp *new_tree_id = NULL;
1685 3ce1b845 2019-07-15 stsp
1686 3ce1b845 2019-07-15 stsp TAILQ_INIT(&paths);
1687 3ce1b845 2019-07-15 stsp
1688 3ce1b845 2019-07-15 stsp dir = opendir(path_dir);
1689 3ce1b845 2019-07-15 stsp if (dir == NULL) {
1690 3ce1b845 2019-07-15 stsp err = got_error_from_errno2("opendir", path_dir);
1691 3ce1b845 2019-07-15 stsp goto done;
1692 3ce1b845 2019-07-15 stsp }
1693 3ce1b845 2019-07-15 stsp
1694 56e0773d 2019-11-28 stsp nentries = 0;
1695 3ce1b845 2019-07-15 stsp while ((de = readdir(dir)) != NULL) {
1696 3ce1b845 2019-07-15 stsp int ignore = 0;
1697 20ccae39 2020-07-21 stsp int type;
1698 3ce1b845 2019-07-15 stsp
1699 3ce1b845 2019-07-15 stsp if (strcmp(de->d_name, ".") == 0 ||
1700 3ce1b845 2019-07-15 stsp strcmp(de->d_name, "..") == 0)
1701 3ce1b845 2019-07-15 stsp continue;
1702 3ce1b845 2019-07-15 stsp
1703 3ce1b845 2019-07-15 stsp TAILQ_FOREACH(pe, ignores, entry) {
1704 3ce1b845 2019-07-15 stsp if (fnmatch(pe->path, de->d_name, 0) == 0) {
1705 3ce1b845 2019-07-15 stsp ignore = 1;
1706 3ce1b845 2019-07-15 stsp break;
1707 3ce1b845 2019-07-15 stsp }
1708 3ce1b845 2019-07-15 stsp }
1709 3ce1b845 2019-07-15 stsp if (ignore)
1710 3ce1b845 2019-07-15 stsp continue;
1711 20ccae39 2020-07-21 stsp
1712 20ccae39 2020-07-21 stsp err = got_path_dirent_type(&type, path_dir, de);
1713 20ccae39 2020-07-21 stsp if (err)
1714 20ccae39 2020-07-21 stsp goto done;
1715 20ccae39 2020-07-21 stsp
1716 20ccae39 2020-07-21 stsp if (type == DT_DIR) {
1717 3ce1b845 2019-07-15 stsp err = import_subdir(&new_te, de, path_dir,
1718 3ce1b845 2019-07-15 stsp ignores, repo, progress_cb, progress_arg);
1719 db1d3576 2019-10-04 stsp if (err) {
1720 db1d3576 2019-10-04 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY)
1721 db1d3576 2019-10-04 stsp goto done;
1722 db1d3576 2019-10-04 stsp err = NULL;
1723 db1d3576 2019-10-04 stsp continue;
1724 db1d3576 2019-10-04 stsp }
1725 e8863bdc 2020-07-23 stsp } else if (type == DT_REG || type == DT_LNK) {
1726 3ce1b845 2019-07-15 stsp err = import_file(&new_te, de, path_dir, repo);
1727 3ce1b845 2019-07-15 stsp if (err)
1728 3ce1b845 2019-07-15 stsp goto done;
1729 3ce1b845 2019-07-15 stsp } else
1730 3ce1b845 2019-07-15 stsp continue;
1731 3ce1b845 2019-07-15 stsp
1732 3ce1b845 2019-07-15 stsp err = insert_tree_entry(new_te, &paths);
1733 3ce1b845 2019-07-15 stsp if (err)
1734 3ce1b845 2019-07-15 stsp goto done;
1735 56e0773d 2019-11-28 stsp nentries++;
1736 3ce1b845 2019-07-15 stsp }
1737 3ce1b845 2019-07-15 stsp
1738 db1d3576 2019-10-04 stsp if (TAILQ_EMPTY(&paths)) {
1739 b66cd6f3 2020-07-31 stsp err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
1740 b66cd6f3 2020-07-31 stsp "cannot create tree without any entries");
1741 db1d3576 2019-10-04 stsp goto done;
1742 db1d3576 2019-10-04 stsp }
1743 db1d3576 2019-10-04 stsp
1744 3ce1b845 2019-07-15 stsp TAILQ_FOREACH(pe, &paths, entry) {
1745 3ce1b845 2019-07-15 stsp struct got_tree_entry *te = pe->data;
1746 3ce1b845 2019-07-15 stsp char *path;
1747 e8863bdc 2020-07-23 stsp if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
1748 3ce1b845 2019-07-15 stsp continue;
1749 3ce1b845 2019-07-15 stsp if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1750 3ce1b845 2019-07-15 stsp err = got_error_from_errno("asprintf");
1751 3ce1b845 2019-07-15 stsp goto done;
1752 3ce1b845 2019-07-15 stsp }
1753 3ce1b845 2019-07-15 stsp err = (*progress_cb)(progress_arg, path);
1754 3ce1b845 2019-07-15 stsp free(path);
1755 3ce1b845 2019-07-15 stsp if (err)
1756 3ce1b845 2019-07-15 stsp goto done;
1757 3ce1b845 2019-07-15 stsp }
1758 3ce1b845 2019-07-15 stsp
1759 56e0773d 2019-11-28 stsp err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1760 3ce1b845 2019-07-15 stsp done:
1761 3ce1b845 2019-07-15 stsp if (dir)
1762 3ce1b845 2019-07-15 stsp closedir(dir);
1763 3ce1b845 2019-07-15 stsp got_pathlist_free(&paths);
1764 3ce1b845 2019-07-15 stsp return err;
1765 3ce1b845 2019-07-15 stsp }
1766 3ce1b845 2019-07-15 stsp
1767 3ce1b845 2019-07-15 stsp const struct got_error *
1768 3ce1b845 2019-07-15 stsp got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1769 3ce1b845 2019-07-15 stsp const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1770 3ce1b845 2019-07-15 stsp struct got_repository *repo, got_repo_import_cb progress_cb,
1771 3ce1b845 2019-07-15 stsp void *progress_arg)
1772 3ce1b845 2019-07-15 stsp {
1773 3ce1b845 2019-07-15 stsp const struct got_error *err;
1774 3ce1b845 2019-07-15 stsp struct got_object_id *new_tree_id;
1775 3ce1b845 2019-07-15 stsp
1776 3ce1b845 2019-07-15 stsp err = write_tree(&new_tree_id, path_dir, ignores, repo,
1777 3ce1b845 2019-07-15 stsp progress_cb, progress_arg);
1778 3ce1b845 2019-07-15 stsp if (err)
1779 3ce1b845 2019-07-15 stsp return err;
1780 3ce1b845 2019-07-15 stsp
1781 3ce1b845 2019-07-15 stsp err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1782 3ce1b845 2019-07-15 stsp author, time(NULL), author, time(NULL), logmsg, repo);
1783 3ce1b845 2019-07-15 stsp free(new_tree_id);
1784 3ce1b845 2019-07-15 stsp return err;
1785 3ce1b845 2019-07-15 stsp }