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