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