2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
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.
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.
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>
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>
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"
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"
62 79b11c62 2018-03-09 stsp #ifndef nitems
63 79b11c62 2018-03-09 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
66 4027f31a 2017-11-04 stsp #define GOT_GIT_DIR ".git"
68 4027f31a 2017-11-04 stsp /* Mandatory files and directories inside the git directory. */
69 4df642d9 2017-11-05 stsp #define GOT_OBJECTS_DIR "objects"
70 4df642d9 2017-11-05 stsp #define GOT_REFS_DIR "refs"
71 4df642d9 2017-11-05 stsp #define GOT_HEAD_FILE "HEAD"
72 1d126e2d 2019-08-24 stsp #define GOT_GITCONFIG "config"
74 a1fd68d8 2018-01-12 stsp /* Other files and directories inside the git directory. */
75 4df642d9 2017-11-05 stsp #define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
76 4df642d9 2017-11-05 stsp #define GOT_ORIG_HEAD_FILE "ORIG_HEAD"
77 a1fd68d8 2018-01-12 stsp #define GOT_OBJECTS_PACK_DIR "objects/pack"
78 fb79db15 2019-02-01 stsp #define GOT_PACKED_REFS_FILE "packed-refs"
80 7839bc15 2019-01-06 stsp const char *
81 86c3caaf 2018-03-09 stsp got_repo_get_path(struct got_repository *repo)
83 7839bc15 2019-01-06 stsp return repo->path;
86 6e9da951 2019-01-06 stsp const char *
87 11995603 2017-11-05 stsp got_repo_get_path_git_dir(struct got_repository *repo)
89 6e9da951 2019-01-06 stsp return repo->path_git_dir;
92 aba9c984 2019-09-08 stsp const char *
93 aba9c984 2019-09-08 stsp got_repo_get_gitconfig_author_name(struct got_repository *repo)
95 aba9c984 2019-09-08 stsp return repo->gitconfig_author_name;
98 aba9c984 2019-09-08 stsp const char *
99 aba9c984 2019-09-08 stsp got_repo_get_gitconfig_author_email(struct got_repository *repo)
101 aba9c984 2019-09-08 stsp return repo->gitconfig_author_email;
104 c9956ddf 2019-09-08 stsp const char *
105 c9956ddf 2019-09-08 stsp got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
107 c9956ddf 2019-09-08 stsp return repo->global_gitconfig_author_name;
110 c9956ddf 2019-09-08 stsp const char *
111 c9956ddf 2019-09-08 stsp got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
113 c9956ddf 2019-09-08 stsp return repo->global_gitconfig_author_email;
117 04ca23f4 2018-07-16 stsp got_repo_is_bare(struct got_repository *repo)
119 04ca23f4 2018-07-16 stsp return (strcmp(repo->path, repo->path_git_dir) == 0);
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)
125 4027f31a 2017-11-04 stsp char *path_child;
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;
131 4027f31a 2017-11-04 stsp return path_child;
135 11995603 2017-11-05 stsp got_repo_get_path_objects(struct got_repository *repo)
137 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_OBJECTS_DIR);
141 a1fd68d8 2018-01-12 stsp got_repo_get_path_objects_pack(struct got_repository *repo)
143 a1fd68d8 2018-01-12 stsp return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
147 11995603 2017-11-05 stsp got_repo_get_path_refs(struct got_repository *repo)
149 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_REFS_DIR);
153 fb79db15 2019-02-01 stsp got_repo_get_path_packed_refs(struct got_repository *repo)
155 fb79db15 2019-02-01 stsp return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
158 4027f31a 2017-11-04 stsp static char *
159 4027f31a 2017-11-04 stsp get_path_head(struct got_repository *repo)
161 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_HEAD_FILE);
164 1d126e2d 2019-08-24 stsp static const struct got_error *
165 1d126e2d 2019-08-24 stsp get_path_gitconfig(char **p, struct got_repository *repo)
167 1d126e2d 2019-08-24 stsp *p = get_path_git_child(repo, GOT_GITCONFIG);
168 1d126e2d 2019-08-24 stsp if (*p == NULL)
169 1d126e2d 2019-08-24 stsp return got_error_from_errno("asprintf");
170 1d126e2d 2019-08-24 stsp return NULL;
174 cd95becd 2019-11-29 stsp got_repo_get_gitconfig_remotes(int *nremotes, struct got_remote_repo **remotes,
175 cd95becd 2019-11-29 stsp struct got_repository *repo)
177 cd95becd 2019-11-29 stsp *nremotes = repo->ngitconfig_remotes;
178 cd95becd 2019-11-29 stsp *remotes = repo->gitconfig_remotes;
182 4027f31a 2017-11-04 stsp is_git_repo(struct got_repository *repo)
184 6e9da951 2019-01-06 stsp const char *path_git = got_repo_get_path_git_dir(repo);
185 11995603 2017-11-05 stsp char *path_objects = got_repo_get_path_objects(repo);
186 11995603 2017-11-05 stsp char *path_refs = got_repo_get_path_refs(repo);
187 4027f31a 2017-11-04 stsp char *path_head = get_path_head(repo);
188 deeca238 2018-03-12 stsp int ret = 0;
189 deeca238 2018-03-12 stsp struct stat sb;
190 4847cca1 2018-03-12 stsp struct got_reference *head_ref;
192 deeca238 2018-03-12 stsp if (lstat(path_git, &sb) == -1)
194 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
197 deeca238 2018-03-12 stsp if (lstat(path_objects, &sb) == -1)
199 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
202 deeca238 2018-03-12 stsp if (lstat(path_refs, &sb) == -1)
204 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
207 deeca238 2018-03-12 stsp if (lstat(path_head, &sb) == -1)
209 deeca238 2018-03-12 stsp if (!S_ISREG(sb.st_mode))
212 4847cca1 2018-03-12 stsp /* Check if the HEAD reference can be opened. */
213 2f17228e 2019-05-12 stsp if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
215 4847cca1 2018-03-12 stsp got_ref_close(head_ref);
219 4027f31a 2017-11-04 stsp free(path_objects);
220 4027f31a 2017-11-04 stsp free(path_refs);
221 4027f31a 2017-11-04 stsp free(path_head);
222 4027f31a 2017-11-04 stsp return ret;
226 f6be5c30 2018-06-22 stsp const struct got_error *
227 f6be5c30 2018-06-22 stsp got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
228 f6be5c30 2018-06-22 stsp struct got_object *obj)
230 ccfe88e6 2018-07-12 stsp #ifndef GOT_NO_OBJ_CACHE
231 f6be5c30 2018-06-22 stsp const struct got_error *err = NULL;
232 6bef87be 2018-09-11 stsp err = got_object_cache_add(&repo->objcache, id, obj);
234 79c99a64 2019-05-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
235 79c99a64 2019-05-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
236 79c99a64 2019-05-23 stsp err = NULL;
237 f6be5c30 2018-06-22 stsp return err;
239 f6be5c30 2018-06-22 stsp obj->refcnt++;
241 f6be5c30 2018-06-22 stsp return NULL;
244 7bb0daa1 2018-06-21 stsp struct got_object *
245 7bb0daa1 2018-06-21 stsp got_repo_get_cached_object(struct got_repository *repo,
246 7bb0daa1 2018-06-21 stsp struct got_object_id *id)
248 6bef87be 2018-09-11 stsp return (struct got_object *)got_object_cache_get(&repo->objcache, id);
251 4027f31a 2017-11-04 stsp const struct got_error *
252 f6be5c30 2018-06-22 stsp got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
253 f6be5c30 2018-06-22 stsp struct got_tree_object *tree)
255 ccfe88e6 2018-07-12 stsp #ifndef GOT_NO_OBJ_CACHE
256 f6be5c30 2018-06-22 stsp const struct got_error *err = NULL;
257 6bef87be 2018-09-11 stsp err = got_object_cache_add(&repo->treecache, id, tree);
259 79c99a64 2019-05-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
260 79c99a64 2019-05-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
261 79c99a64 2019-05-23 stsp err = NULL;
262 f6be5c30 2018-06-22 stsp return err;
264 f6be5c30 2018-06-22 stsp tree->refcnt++;
266 f6be5c30 2018-06-22 stsp return NULL;
269 f6be5c30 2018-06-22 stsp struct got_tree_object *
270 f6be5c30 2018-06-22 stsp got_repo_get_cached_tree(struct got_repository *repo,
271 f6be5c30 2018-06-22 stsp struct got_object_id *id)
273 6bef87be 2018-09-11 stsp return (struct got_tree_object *)got_object_cache_get(
274 6bef87be 2018-09-11 stsp &repo->treecache, id);
277 1943de01 2018-06-22 stsp const struct got_error *
278 1943de01 2018-06-22 stsp got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
279 1943de01 2018-06-22 stsp struct got_commit_object *commit)
281 ccfe88e6 2018-07-12 stsp #ifndef GOT_NO_OBJ_CACHE
282 1943de01 2018-06-22 stsp const struct got_error *err = NULL;
283 6bef87be 2018-09-11 stsp err = got_object_cache_add(&repo->commitcache, id, commit);
285 79c99a64 2019-05-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
286 79c99a64 2019-05-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
287 79c99a64 2019-05-23 stsp err = NULL;
288 1943de01 2018-06-22 stsp return err;
290 1943de01 2018-06-22 stsp commit->refcnt++;
292 f6be5c30 2018-06-22 stsp return NULL;
295 1943de01 2018-06-22 stsp struct got_commit_object *
296 1943de01 2018-06-22 stsp got_repo_get_cached_commit(struct got_repository *repo,
297 1943de01 2018-06-22 stsp struct got_object_id *id)
299 6bef87be 2018-09-11 stsp return (struct got_commit_object *)got_object_cache_get(
300 6bef87be 2018-09-11 stsp &repo->commitcache, id);
303 f4a881ce 2018-11-17 stsp const struct got_error *
304 f4a881ce 2018-11-17 stsp got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
305 f4a881ce 2018-11-17 stsp struct got_tag_object *tag)
307 f4a881ce 2018-11-17 stsp #ifndef GOT_NO_OBJ_CACHE
308 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
309 f4a881ce 2018-11-17 stsp err = got_object_cache_add(&repo->tagcache, id, tag);
311 79c99a64 2019-05-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
312 79c99a64 2019-05-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
313 79c99a64 2019-05-23 stsp err = NULL;
314 f4a881ce 2018-11-17 stsp return err;
316 f4a881ce 2018-11-17 stsp tag->refcnt++;
318 f4a881ce 2018-11-17 stsp return NULL;
321 f4a881ce 2018-11-17 stsp struct got_tag_object *
322 f4a881ce 2018-11-17 stsp got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
324 f4a881ce 2018-11-17 stsp return (struct got_tag_object *)got_object_cache_get(
325 f4a881ce 2018-11-17 stsp &repo->tagcache, id);
328 f6be5c30 2018-06-22 stsp const struct got_error *
329 85f51bba 2018-07-16 stsp open_repo(struct got_repository *repo, const char *path)
331 85f51bba 2018-07-16 stsp const struct got_error *err = NULL;
333 85f51bba 2018-07-16 stsp /* bare git repository? */
334 85f51bba 2018-07-16 stsp repo->path_git_dir = strdup(path);
335 ee645855 2019-02-05 stsp if (repo->path_git_dir == NULL)
336 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
337 85f51bba 2018-07-16 stsp if (is_git_repo(repo)) {
338 85f51bba 2018-07-16 stsp repo->path = strdup(repo->path_git_dir);
339 85f51bba 2018-07-16 stsp if (repo->path == NULL) {
340 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
343 85f51bba 2018-07-16 stsp return NULL;
346 85f51bba 2018-07-16 stsp /* git repository with working tree? */
347 85f51bba 2018-07-16 stsp free(repo->path_git_dir);
348 6b68ccd6 2019-09-01 stsp repo->path_git_dir = NULL;
349 85f51bba 2018-07-16 stsp if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
350 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
353 85f51bba 2018-07-16 stsp if (is_git_repo(repo)) {
354 85f51bba 2018-07-16 stsp repo->path = strdup(path);
355 85f51bba 2018-07-16 stsp if (repo->path == NULL) {
356 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
359 85f51bba 2018-07-16 stsp return NULL;
362 ee645855 2019-02-05 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
365 ee645855 2019-02-05 stsp free(repo->path);
366 ee645855 2019-02-05 stsp repo->path = NULL;
367 85f51bba 2018-07-16 stsp free(repo->path_git_dir);
368 ee645855 2019-02-05 stsp repo->path_git_dir = NULL;
370 aba9c984 2019-09-08 stsp return err;
373 aba9c984 2019-09-08 stsp static const struct got_error *
374 c9956ddf 2019-09-08 stsp parse_gitconfig_file(int *gitconfig_repository_format_version,
375 c9956ddf 2019-09-08 stsp char **gitconfig_author_name, char **gitconfig_author_email,
376 cd95becd 2019-11-29 stsp struct got_remote_repo **remotes, int *nremotes,
377 c9956ddf 2019-09-08 stsp const char *gitconfig_path)
379 aba9c984 2019-09-08 stsp const struct got_error *err = NULL, *child_err = NULL;
380 aba9c984 2019-09-08 stsp int fd = -1;
381 aba9c984 2019-09-08 stsp int imsg_fds[2] = { -1, -1 };
383 aba9c984 2019-09-08 stsp struct imsgbuf *ibuf;
385 c9956ddf 2019-09-08 stsp *gitconfig_repository_format_version = 0;
386 c9956ddf 2019-09-08 stsp *gitconfig_author_name = NULL;
387 c9956ddf 2019-09-08 stsp *gitconfig_author_email = NULL;
389 aba9c984 2019-09-08 stsp fd = open(gitconfig_path, O_RDONLY);
390 aba9c984 2019-09-08 stsp if (fd == -1) {
391 c9956ddf 2019-09-08 stsp if (errno == ENOENT)
392 aba9c984 2019-09-08 stsp return NULL;
393 c9956ddf 2019-09-08 stsp return got_error_from_errno2("open", gitconfig_path);
396 aba9c984 2019-09-08 stsp ibuf = calloc(1, sizeof(*ibuf));
397 aba9c984 2019-09-08 stsp if (ibuf == NULL) {
398 aba9c984 2019-09-08 stsp err = got_error_from_errno("calloc");
402 aba9c984 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
403 aba9c984 2019-09-08 stsp err = got_error_from_errno("socketpair");
407 aba9c984 2019-09-08 stsp pid = fork();
408 aba9c984 2019-09-08 stsp if (pid == -1) {
409 aba9c984 2019-09-08 stsp err = got_error_from_errno("fork");
411 aba9c984 2019-09-08 stsp } else if (pid == 0) {
412 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
413 c9956ddf 2019-09-08 stsp gitconfig_path);
414 aba9c984 2019-09-08 stsp /* not reached */
417 aba9c984 2019-09-08 stsp if (close(imsg_fds[1]) == -1) {
418 aba9c984 2019-09-08 stsp err = got_error_from_errno("close");
421 aba9c984 2019-09-08 stsp imsg_fds[1] = -1;
422 aba9c984 2019-09-08 stsp imsg_init(ibuf, imsg_fds[0]);
424 aba9c984 2019-09-08 stsp err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
429 aba9c984 2019-09-08 stsp err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
433 aba9c984 2019-09-08 stsp err = got_privsep_recv_gitconfig_int(
434 c9956ddf 2019-09-08 stsp gitconfig_repository_format_version, ibuf);
438 aba9c984 2019-09-08 stsp err = got_privsep_send_gitconfig_author_name_req(ibuf);
442 c9956ddf 2019-09-08 stsp err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
446 aba9c984 2019-09-08 stsp err = got_privsep_send_gitconfig_author_email_req(ibuf);
450 c9956ddf 2019-09-08 stsp err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
454 cd95becd 2019-11-29 stsp if (remotes && nremotes) {
455 cd95becd 2019-11-29 stsp err = got_privsep_send_gitconfig_remotes_req(ibuf);
459 cd95becd 2019-11-29 stsp err = got_privsep_recv_gitconfig_remotes(remotes,
460 cd95becd 2019-11-29 stsp nremotes, ibuf);
465 aba9c984 2019-09-08 stsp imsg_clear(ibuf);
466 aba9c984 2019-09-08 stsp err = got_privsep_send_stop(imsg_fds[0]);
467 aba9c984 2019-09-08 stsp child_err = got_privsep_wait_for_child(pid);
468 aba9c984 2019-09-08 stsp if (child_err && err == NULL)
469 aba9c984 2019-09-08 stsp err = child_err;
471 aba9c984 2019-09-08 stsp if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
472 aba9c984 2019-09-08 stsp err = got_error_from_errno("close");
473 aba9c984 2019-09-08 stsp if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
474 aba9c984 2019-09-08 stsp err = got_error_from_errno("close");
475 aba9c984 2019-09-08 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
476 aba9c984 2019-09-08 stsp err = got_error_from_errno2("close", gitconfig_path);
477 aba9c984 2019-09-08 stsp free(ibuf);
478 c9956ddf 2019-09-08 stsp return err;
481 c9956ddf 2019-09-08 stsp static const struct got_error *
482 c9956ddf 2019-09-08 stsp read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
484 c9956ddf 2019-09-08 stsp const struct got_error *err = NULL;
485 c9956ddf 2019-09-08 stsp char *repo_gitconfig_path = NULL;
487 c9956ddf 2019-09-08 stsp if (global_gitconfig_path) {
488 c9956ddf 2019-09-08 stsp /* Read settings from ~/.gitconfig. */
489 c9956ddf 2019-09-08 stsp int dummy_repo_version;
490 c9956ddf 2019-09-08 stsp err = parse_gitconfig_file(&dummy_repo_version,
491 c9956ddf 2019-09-08 stsp &repo->global_gitconfig_author_name,
492 c9956ddf 2019-09-08 stsp &repo->global_gitconfig_author_email,
493 cd95becd 2019-11-29 stsp NULL, NULL, global_gitconfig_path);
495 c9956ddf 2019-09-08 stsp return err;
498 c9956ddf 2019-09-08 stsp /* Read repository's .git/config file. */
499 c9956ddf 2019-09-08 stsp err = get_path_gitconfig(&repo_gitconfig_path, repo);
501 c9956ddf 2019-09-08 stsp return err;
503 c9956ddf 2019-09-08 stsp err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
504 c9956ddf 2019-09-08 stsp &repo->gitconfig_author_name, &repo->gitconfig_author_email,
505 cd95becd 2019-11-29 stsp &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
506 c9956ddf 2019-09-08 stsp repo_gitconfig_path);
510 c9956ddf 2019-09-08 stsp free(repo_gitconfig_path);
511 85f51bba 2018-07-16 stsp return err;
514 85f51bba 2018-07-16 stsp const struct got_error *
515 c9956ddf 2019-09-08 stsp got_repo_open(struct got_repository **repop, const char *path,
516 c9956ddf 2019-09-08 stsp const char *global_gitconfig_path)
518 92af5469 2017-11-05 stsp struct got_repository *repo = NULL;
519 92af5469 2017-11-05 stsp const struct got_error *err = NULL;
520 aba9c984 2019-09-08 stsp char *abspath;
521 ad242220 2018-09-08 stsp int i, tried_root = 0;
523 85f51bba 2018-07-16 stsp *repop = NULL;
525 2393f13b 2018-03-09 stsp if (got_path_is_absolute(path))
526 2393f13b 2018-03-09 stsp abspath = strdup(path);
528 2393f13b 2018-03-09 stsp abspath = got_path_get_absolute(path);
529 92af5469 2017-11-05 stsp if (abspath == NULL)
530 92af5469 2017-11-05 stsp return got_error(GOT_ERR_BAD_PATH);
532 4027f31a 2017-11-04 stsp repo = calloc(1, sizeof(*repo));
533 92af5469 2017-11-05 stsp if (repo == NULL) {
534 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
538 ad242220 2018-09-08 stsp for (i = 0; i < nitems(repo->privsep_children); i++) {
539 3516b818 2018-09-08 stsp memset(&repo->privsep_children[i], 0,
540 3516b818 2018-09-08 stsp sizeof(repo->privsep_children[0]));
541 ad242220 2018-09-08 stsp repo->privsep_children[i].imsg_fd = -1;
544 6bef87be 2018-09-11 stsp err = got_object_cache_init(&repo->objcache,
545 6bef87be 2018-09-11 stsp GOT_OBJECT_CACHE_TYPE_OBJ);
548 6bef87be 2018-09-11 stsp err = got_object_cache_init(&repo->treecache,
549 6bef87be 2018-09-11 stsp GOT_OBJECT_CACHE_TYPE_TREE);
552 6bef87be 2018-09-11 stsp err = got_object_cache_init(&repo->commitcache,
553 6bef87be 2018-09-11 stsp GOT_OBJECT_CACHE_TYPE_COMMIT);
556 f4a881ce 2018-11-17 stsp err = got_object_cache_init(&repo->tagcache,
557 f4a881ce 2018-11-17 stsp GOT_OBJECT_CACHE_TYPE_TAG);
561 6876e203 2019-07-22 stsp path = realpath(abspath, NULL);
562 6876e203 2019-07-22 stsp if (path == NULL) {
563 62550b13 2019-07-23 stsp err = got_error_from_errno2("realpath", abspath);
568 85f51bba 2018-07-16 stsp err = open_repo(repo, path);
569 85f51bba 2018-07-16 stsp if (err == NULL)
571 85f51bba 2018-07-16 stsp if (err->code != GOT_ERR_NOT_GIT_REPO)
573 85f51bba 2018-07-16 stsp if (path[0] == '/' && path[1] == '\0') {
574 85f51bba 2018-07-16 stsp if (tried_root) {
575 85f51bba 2018-07-16 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
578 85f51bba 2018-07-16 stsp tried_root = 1;
580 85f51bba 2018-07-16 stsp path = dirname(path);
581 f2db9c47 2019-08-24 stsp if (path == NULL) {
582 638f9024 2019-05-13 stsp err = got_error_from_errno2("dirname", path);
585 85f51bba 2018-07-16 stsp } while (path);
587 c9956ddf 2019-09-08 stsp err = read_gitconfig(repo, global_gitconfig_path);
590 aba9c984 2019-09-08 stsp if (repo->gitconfig_repository_format_version != 0)
591 aba9c984 2019-09-08 stsp err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
594 5c2f5761 2018-09-19 stsp got_repo_close(repo);
596 85f51bba 2018-07-16 stsp *repop = repo;
597 92af5469 2017-11-05 stsp free(abspath);
598 92af5469 2017-11-05 stsp return err;
601 ad242220 2018-09-08 stsp const struct got_error *
602 4027f31a 2017-11-04 stsp got_repo_close(struct got_repository *repo)
604 ad242220 2018-09-08 stsp const struct got_error *err = NULL, *child_err;
607 65cf1e80 2018-03-16 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
608 65cf1e80 2018-03-16 stsp if (repo->packidx_cache[i] == NULL)
610 65cf1e80 2018-03-16 stsp got_packidx_close(repo->packidx_cache[i]);
613 7e656b93 2018-03-17 stsp for (i = 0; i < nitems(repo->packs); i++) {
614 7e656b93 2018-03-17 stsp if (repo->packs[i].path_packfile == NULL)
616 7e656b93 2018-03-17 stsp got_pack_close(&repo->packs[i]);
619 4027f31a 2017-11-04 stsp free(repo->path);
620 4986b9d5 2018-03-12 stsp free(repo->path_git_dir);
622 6bef87be 2018-09-11 stsp got_object_cache_close(&repo->objcache);
623 6bef87be 2018-09-11 stsp got_object_cache_close(&repo->treecache);
624 6bef87be 2018-09-11 stsp got_object_cache_close(&repo->commitcache);
625 f4a881ce 2018-11-17 stsp got_object_cache_close(&repo->tagcache);
627 ad242220 2018-09-08 stsp for (i = 0; i < nitems(repo->privsep_children); i++) {
628 ad242220 2018-09-08 stsp if (repo->privsep_children[i].imsg_fd == -1)
630 3516b818 2018-09-08 stsp imsg_clear(repo->privsep_children[i].ibuf);
631 3516b818 2018-09-08 stsp free(repo->privsep_children[i].ibuf);
632 ad242220 2018-09-08 stsp err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
633 876c234b 2018-09-10 stsp child_err = got_privsep_wait_for_child(
634 876c234b 2018-09-10 stsp repo->privsep_children[i].pid);
635 ad242220 2018-09-08 stsp if (child_err && err == NULL)
636 ad242220 2018-09-08 stsp err = child_err;
637 3a6ce05a 2019-02-11 stsp if (close(repo->privsep_children[i].imsg_fd) != 0 &&
638 3a6ce05a 2019-02-11 stsp err == NULL)
639 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
642 aba9c984 2019-09-08 stsp free(repo->gitconfig_author_name);
643 aba9c984 2019-09-08 stsp free(repo->gitconfig_author_email);
644 cd95becd 2019-11-29 stsp for (i = 0; i < repo->ngitconfig_remotes; i++) {
645 cd95becd 2019-11-29 stsp free(repo->gitconfig_remotes[i].name);
646 cd95becd 2019-11-29 stsp free(repo->gitconfig_remotes[i].url);
648 cd95becd 2019-11-29 stsp free(repo->gitconfig_remotes);
649 4027f31a 2017-11-04 stsp free(repo);
651 ad242220 2018-09-08 stsp return err;
654 04ca23f4 2018-07-16 stsp const struct got_error *
655 04ca23f4 2018-07-16 stsp got_repo_map_path(char **in_repo_path, struct got_repository *repo,
656 23721109 2018-10-22 stsp const char *input_path, int check_disk)
658 04ca23f4 2018-07-16 stsp const struct got_error *err = NULL;
659 7839bc15 2019-01-06 stsp const char *repo_abspath = NULL;
660 04ca23f4 2018-07-16 stsp size_t repolen, cwdlen, len;
661 7839bc15 2019-01-06 stsp char *cwd, *canonpath, *path = NULL;
663 04ca23f4 2018-07-16 stsp *in_repo_path = NULL;
665 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
666 04ca23f4 2018-07-16 stsp if (cwd == NULL)
667 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
669 04ca23f4 2018-07-16 stsp canonpath = strdup(input_path);
670 04ca23f4 2018-07-16 stsp if (canonpath == NULL) {
671 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
674 04ca23f4 2018-07-16 stsp err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
678 04ca23f4 2018-07-16 stsp repo_abspath = got_repo_get_path(repo);
680 2840f715 2019-07-11 stsp if (!check_disk || canonpath[0] == '\0') {
681 23721109 2018-10-22 stsp path = strdup(canonpath);
682 b70703ad 2019-03-18 stsp if (path == NULL) {
683 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
687 04ca23f4 2018-07-16 stsp int is_repo_child = 0, is_cwd_child = 0;
689 04ca23f4 2018-07-16 stsp path = realpath(canonpath, NULL);
690 04ca23f4 2018-07-16 stsp if (path == NULL) {
691 b70703ad 2019-03-18 stsp if (errno != ENOENT) {
692 638f9024 2019-05-13 stsp err = got_error_from_errno2("realpath",
697 b70703ad 2019-03-18 stsp * Path is not on disk.
698 b70703ad 2019-03-18 stsp * Assume it is already relative to repository root.
700 b70703ad 2019-03-18 stsp path = strdup(canonpath);
701 b70703ad 2019-03-18 stsp if (path == NULL) {
702 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
707 04ca23f4 2018-07-16 stsp repolen = strlen(repo_abspath);
708 04ca23f4 2018-07-16 stsp cwdlen = strlen(cwd);
709 04ca23f4 2018-07-16 stsp len = strlen(path);
711 04ca23f4 2018-07-16 stsp if (len > repolen && strncmp(path, repo_abspath, repolen) == 0)
712 04ca23f4 2018-07-16 stsp is_repo_child = 1;
713 04ca23f4 2018-07-16 stsp if (len > cwdlen && strncmp(path, cwd, cwdlen) == 0)
714 04ca23f4 2018-07-16 stsp is_cwd_child = 1;
716 04ca23f4 2018-07-16 stsp if (strcmp(path, repo_abspath) == 0) {
717 04ca23f4 2018-07-16 stsp free(path);
718 04ca23f4 2018-07-16 stsp path = strdup("");
719 04ca23f4 2018-07-16 stsp if (path == NULL) {
720 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
723 04ca23f4 2018-07-16 stsp } else if (is_repo_child && is_cwd_child) {
724 04ca23f4 2018-07-16 stsp char *child;
725 04ca23f4 2018-07-16 stsp /* Strip common prefix with repository path. */
726 04ca23f4 2018-07-16 stsp err = got_path_skip_common_ancestor(&child,
727 04ca23f4 2018-07-16 stsp repo_abspath, path);
730 04ca23f4 2018-07-16 stsp free(path);
731 04ca23f4 2018-07-16 stsp path = child;
732 04ca23f4 2018-07-16 stsp } else if (is_repo_child) {
733 04ca23f4 2018-07-16 stsp /* Matched an on-disk path inside repository. */
734 04ca23f4 2018-07-16 stsp if (got_repo_is_bare(repo)) {
736 04ca23f4 2018-07-16 stsp * Matched an on-disk path inside repository
737 04ca23f4 2018-07-16 stsp * database. Treat as repository-relative.
740 04ca23f4 2018-07-16 stsp char *child;
741 04ca23f4 2018-07-16 stsp /* Strip common prefix with repository path. */
742 04ca23f4 2018-07-16 stsp err = got_path_skip_common_ancestor(&child,
743 04ca23f4 2018-07-16 stsp repo_abspath, path);
746 04ca23f4 2018-07-16 stsp free(path);
747 04ca23f4 2018-07-16 stsp path = child;
749 04ca23f4 2018-07-16 stsp } else if (is_cwd_child) {
750 04ca23f4 2018-07-16 stsp char *child;
751 04ca23f4 2018-07-16 stsp /* Strip common prefix with cwd. */
752 04ca23f4 2018-07-16 stsp err = got_path_skip_common_ancestor(&child, cwd,
756 04ca23f4 2018-07-16 stsp free(path);
757 04ca23f4 2018-07-16 stsp path = child;
760 04ca23f4 2018-07-16 stsp * Matched unrelated on-disk path.
761 04ca23f4 2018-07-16 stsp * Treat it as repository-relative.
766 04ca23f4 2018-07-16 stsp /* Make in-repository path absolute */
767 04ca23f4 2018-07-16 stsp if (path[0] != '/') {
768 04ca23f4 2018-07-16 stsp char *abspath;
769 04ca23f4 2018-07-16 stsp if (asprintf(&abspath, "/%s", path) == -1) {
770 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
773 04ca23f4 2018-07-16 stsp free(path);
774 04ca23f4 2018-07-16 stsp path = abspath;
779 04ca23f4 2018-07-16 stsp free(canonpath);
781 04ca23f4 2018-07-16 stsp free(path);
783 04ca23f4 2018-07-16 stsp *in_repo_path = path;
784 1510f469 2018-09-09 stsp return err;
787 1510f469 2018-09-09 stsp const struct got_error *
788 1510f469 2018-09-09 stsp got_repo_cache_packidx(struct got_repository *repo, struct got_packidx *packidx)
790 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
793 1510f469 2018-09-09 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
794 1510f469 2018-09-09 stsp if (repo->packidx_cache[i] == NULL)
797 1510f469 2018-09-09 stsp if (i == nitems(repo->packidx_cache)) {
798 1510f469 2018-09-09 stsp err = got_packidx_close(repo->packidx_cache[i - 1]);
800 1510f469 2018-09-09 stsp return err;
804 15fe583f 2018-11-05 stsp * Insert the new pack index at the front so it will
805 15fe583f 2018-11-05 stsp * be searched first in the future.
807 15fe583f 2018-11-05 stsp memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
808 15fe583f 2018-11-05 stsp sizeof(repo->packidx_cache) -
809 15fe583f 2018-11-05 stsp sizeof(repo->packidx_cache[0]));
810 15fe583f 2018-11-05 stsp repo->packidx_cache[0] = packidx;
812 1510f469 2018-09-09 stsp return NULL;
816 1510f469 2018-09-09 stsp is_packidx_filename(const char *name, size_t len)
818 1510f469 2018-09-09 stsp if (len != GOT_PACKIDX_NAMELEN)
821 1510f469 2018-09-09 stsp if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
824 1510f469 2018-09-09 stsp if (strcmp(name + strlen(GOT_PACK_PREFIX) +
825 1510f469 2018-09-09 stsp SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
831 1510f469 2018-09-09 stsp const struct got_error *
832 1510f469 2018-09-09 stsp got_repo_search_packidx(struct got_packidx **packidx, int *idx,
833 1510f469 2018-09-09 stsp struct got_repository *repo, struct got_object_id *id)
835 1510f469 2018-09-09 stsp const struct got_error *err;
836 1510f469 2018-09-09 stsp char *path_packdir;
837 1510f469 2018-09-09 stsp DIR *packdir;
838 1510f469 2018-09-09 stsp struct dirent *dent;
839 1510f469 2018-09-09 stsp char *path_packidx;
842 1510f469 2018-09-09 stsp /* Search pack index cache. */
843 1510f469 2018-09-09 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
844 1510f469 2018-09-09 stsp if (repo->packidx_cache[i] == NULL)
846 1510f469 2018-09-09 stsp *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
847 1510f469 2018-09-09 stsp if (*idx != -1) {
848 1510f469 2018-09-09 stsp *packidx = repo->packidx_cache[i];
849 1510f469 2018-09-09 stsp return NULL;
852 1510f469 2018-09-09 stsp /* No luck. Search the filesystem. */
854 1510f469 2018-09-09 stsp path_packdir = got_repo_get_path_objects_pack(repo);
855 1510f469 2018-09-09 stsp if (path_packdir == NULL)
856 638f9024 2019-05-13 stsp return got_error_from_errno("got_repo_get_path_objects_pack");
858 1510f469 2018-09-09 stsp packdir = opendir(path_packdir);
859 1510f469 2018-09-09 stsp if (packdir == NULL) {
860 b90deaa1 2019-07-27 stsp if (errno == ENOENT)
861 b90deaa1 2019-07-27 stsp err = got_error_no_obj(id);
863 b90deaa1 2019-07-27 stsp err = got_error_from_errno2("opendir", path_packdir);
867 1510f469 2018-09-09 stsp while ((dent = readdir(packdir)) != NULL) {
868 1510f469 2018-09-09 stsp if (!is_packidx_filename(dent->d_name, dent->d_namlen))
871 1510f469 2018-09-09 stsp if (asprintf(&path_packidx, "%s/%s", path_packdir,
872 1510f469 2018-09-09 stsp dent->d_name) == -1) {
873 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
877 1510f469 2018-09-09 stsp err = got_packidx_open(packidx, path_packidx, 0);
878 1510f469 2018-09-09 stsp free(path_packidx);
882 1510f469 2018-09-09 stsp *idx = got_packidx_get_object_idx(*packidx, id);
883 1510f469 2018-09-09 stsp if (*idx != -1) {
884 1510f469 2018-09-09 stsp err = NULL; /* found the object */
885 1510f469 2018-09-09 stsp err = got_repo_cache_packidx(repo, *packidx);
889 1510f469 2018-09-09 stsp err = got_packidx_close(*packidx);
890 1510f469 2018-09-09 stsp *packidx = NULL;
895 91a3d81f 2018-11-11 stsp err = got_error_no_obj(id);
897 1510f469 2018-09-09 stsp free(path_packdir);
898 d69bcdf7 2019-06-28 stsp if (packdir && closedir(packdir) != 0 && err == NULL)
899 638f9024 2019-05-13 stsp err = got_error_from_errno("closedir");
900 04ca23f4 2018-07-16 stsp return err;
903 1510f469 2018-09-09 stsp static const struct got_error *
904 1510f469 2018-09-09 stsp read_packfile_hdr(int fd, struct got_packidx *packidx)
906 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
907 1510f469 2018-09-09 stsp uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
908 1510f469 2018-09-09 stsp struct got_packfile_hdr hdr;
911 1510f469 2018-09-09 stsp n = read(fd, &hdr, sizeof(hdr));
913 638f9024 2019-05-13 stsp return got_error_from_errno("read");
914 1510f469 2018-09-09 stsp if (n != sizeof(hdr))
915 1510f469 2018-09-09 stsp return got_error(GOT_ERR_BAD_PACKFILE);
917 1510f469 2018-09-09 stsp if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
918 1510f469 2018-09-09 stsp betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
919 1510f469 2018-09-09 stsp betoh32(hdr.nobjects) != totobj)
920 1510f469 2018-09-09 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
922 1510f469 2018-09-09 stsp return err;
925 1510f469 2018-09-09 stsp static const struct got_error *
926 1510f469 2018-09-09 stsp open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
928 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
930 a5b57ccf 2019-04-11 stsp *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
931 1510f469 2018-09-09 stsp if (*fd == -1)
932 638f9024 2019-05-13 stsp return got_error_from_errno2("open", path_packfile);
934 1510f469 2018-09-09 stsp if (packidx) {
935 1510f469 2018-09-09 stsp err = read_packfile_hdr(*fd, packidx);
937 1510f469 2018-09-09 stsp close(*fd);
942 1510f469 2018-09-09 stsp return err;
945 1510f469 2018-09-09 stsp const struct got_error *
946 1510f469 2018-09-09 stsp got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
947 1510f469 2018-09-09 stsp const char *path_packfile, struct got_packidx *packidx)
949 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
950 1510f469 2018-09-09 stsp struct got_pack *pack = NULL;
951 ff563a3d 2019-05-23 stsp struct stat sb;
955 1510f469 2018-09-09 stsp *packp = NULL;
957 1510f469 2018-09-09 stsp for (i = 0; i < nitems(repo->packs); i++) {
958 1510f469 2018-09-09 stsp pack = &repo->packs[i];
959 1510f469 2018-09-09 stsp if (pack->path_packfile == NULL)
961 1510f469 2018-09-09 stsp if (strcmp(pack->path_packfile, path_packfile) == 0)
962 1510f469 2018-09-09 stsp return NULL;
965 1510f469 2018-09-09 stsp if (i == nitems(repo->packs) - 1) {
966 1510f469 2018-09-09 stsp err = got_pack_close(&repo->packs[i - 1]);
968 1510f469 2018-09-09 stsp return err;
969 1510f469 2018-09-09 stsp memmove(&repo->packs[1], &repo->packs[0],
970 1510f469 2018-09-09 stsp sizeof(repo->packs) - sizeof(repo->packs[0]));
974 1510f469 2018-09-09 stsp pack = &repo->packs[i];
976 1510f469 2018-09-09 stsp pack->path_packfile = strdup(path_packfile);
977 1510f469 2018-09-09 stsp if (pack->path_packfile == NULL) {
978 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
982 1510f469 2018-09-09 stsp err = open_packfile(&pack->fd, path_packfile, packidx);
986 ff563a3d 2019-05-23 stsp if (fstat(pack->fd, &sb) != 0) {
987 ff563a3d 2019-05-23 stsp err = got_error_from_errno("fstat");
990 ff563a3d 2019-05-23 stsp pack->filesize = sb.st_size;
992 90636195 2018-09-11 stsp pack->privsep_child = NULL;
994 1510f469 2018-09-09 stsp #ifndef GOT_PACK_NO_MMAP
995 1510f469 2018-09-09 stsp pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
996 1510f469 2018-09-09 stsp pack->fd, 0);
997 3a11398b 2019-02-21 stsp if (pack->map == MAP_FAILED) {
998 3a11398b 2019-02-21 stsp if (errno != ENOMEM) {
999 638f9024 2019-05-13 stsp err = got_error_from_errno("mmap");
1000 3a11398b 2019-02-21 stsp goto done;
1002 1510f469 2018-09-09 stsp pack->map = NULL; /* fall back to read(2) */
1006 1510f469 2018-09-09 stsp if (err) {
1007 1510f469 2018-09-09 stsp if (pack) {
1008 1510f469 2018-09-09 stsp free(pack->path_packfile);
1009 1510f469 2018-09-09 stsp memset(pack, 0, sizeof(*pack));
1011 1510f469 2018-09-09 stsp } else if (packp)
1012 1510f469 2018-09-09 stsp *packp = pack;
1013 1510f469 2018-09-09 stsp return err;
1016 1510f469 2018-09-09 stsp struct got_pack *
1017 1510f469 2018-09-09 stsp got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1019 1510f469 2018-09-09 stsp struct got_pack *pack = NULL;
1022 1510f469 2018-09-09 stsp for (i = 0; i < nitems(repo->packs); i++) {
1023 1510f469 2018-09-09 stsp pack = &repo->packs[i];
1024 1510f469 2018-09-09 stsp if (pack->path_packfile == NULL)
1026 1510f469 2018-09-09 stsp if (strcmp(pack->path_packfile, path_packfile) == 0)
1027 1510f469 2018-09-09 stsp return pack;
1030 2c7829a4 2019-06-17 stsp return NULL;
1033 2c7829a4 2019-06-17 stsp const struct got_error *
1034 2c7829a4 2019-06-17 stsp got_repo_init(const char *repo_path)
1036 2c7829a4 2019-06-17 stsp const struct got_error *err = NULL;
1037 2c7829a4 2019-06-17 stsp const char *dirnames[] = {
1038 2c7829a4 2019-06-17 stsp GOT_OBJECTS_DIR,
1039 2c7829a4 2019-06-17 stsp GOT_OBJECTS_PACK_DIR,
1040 2c7829a4 2019-06-17 stsp GOT_REFS_DIR,
1042 2c7829a4 2019-06-17 stsp const char *description_str = "Unnamed repository; "
1043 2c7829a4 2019-06-17 stsp "edit this file 'description' to name the repository.";
1044 5d67f40d 2019-11-08 stsp const char *headref_str = "ref: refs/heads/main";
1045 2c7829a4 2019-06-17 stsp const char *gitconfig_str = "[core]\n"
1046 2c7829a4 2019-06-17 stsp "\trepositoryformatversion = 0\n"
1047 2c7829a4 2019-06-17 stsp "\tfilemode = true\n"
1048 2c7829a4 2019-06-17 stsp "\tbare = true\n";
1049 2c7829a4 2019-06-17 stsp char *path;
1052 2c7829a4 2019-06-17 stsp if (!got_path_dir_is_empty(repo_path))
1053 2c7829a4 2019-06-17 stsp return got_error(GOT_ERR_DIR_NOT_EMPTY);
1055 2c7829a4 2019-06-17 stsp for (i = 0; i < nitems(dirnames); i++) {
1056 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1057 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
1059 2c7829a4 2019-06-17 stsp err = got_path_mkdir(path);
1060 2c7829a4 2019-06-17 stsp free(path);
1062 2c7829a4 2019-06-17 stsp return err;
1065 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1066 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
1067 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, description_str);
1068 2c7829a4 2019-06-17 stsp free(path);
1070 2c7829a4 2019-06-17 stsp return err;
1072 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1073 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
1074 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, headref_str);
1075 2c7829a4 2019-06-17 stsp free(path);
1077 2c7829a4 2019-06-17 stsp return err;
1079 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1080 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
1081 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, gitconfig_str);
1082 2c7829a4 2019-06-17 stsp free(path);
1084 2c7829a4 2019-06-17 stsp return err;
1086 1510f469 2018-09-09 stsp return NULL;
1089 e09a504c 2019-06-28 stsp static const struct got_error *
1090 4277420a 2019-06-29 stsp match_packed_object(struct got_object_id **unique_id,
1091 dd88155e 2019-06-29 stsp struct got_repository *repo, const char *id_str_prefix, int obj_type)
1093 e09a504c 2019-06-28 stsp const struct got_error *err = NULL;
1094 e09a504c 2019-06-28 stsp char *path_packdir;
1095 e09a504c 2019-06-28 stsp DIR *packdir;
1096 e09a504c 2019-06-28 stsp struct dirent *dent;
1097 e09a504c 2019-06-28 stsp char *path_packidx;
1098 dd88155e 2019-06-29 stsp struct got_object_id_queue matched_ids;
1100 dd88155e 2019-06-29 stsp SIMPLEQ_INIT(&matched_ids);
1102 e09a504c 2019-06-28 stsp path_packdir = got_repo_get_path_objects_pack(repo);
1103 e09a504c 2019-06-28 stsp if (path_packdir == NULL)
1104 e09a504c 2019-06-28 stsp return got_error_from_errno("got_repo_get_path_objects_pack");
1106 e09a504c 2019-06-28 stsp packdir = opendir(path_packdir);
1107 e09a504c 2019-06-28 stsp if (packdir == NULL) {
1108 e4167f30 2019-07-27 stsp if (errno != ENOENT)
1109 e4167f30 2019-07-27 stsp err = got_error_from_errno2("opendir", path_packdir);
1110 e09a504c 2019-06-28 stsp goto done;
1113 e09a504c 2019-06-28 stsp while ((dent = readdir(packdir)) != NULL) {
1114 e09a504c 2019-06-28 stsp struct got_packidx *packidx;
1115 dd88155e 2019-06-29 stsp struct got_object_qid *qid;
1118 e09a504c 2019-06-28 stsp if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1121 e09a504c 2019-06-28 stsp if (asprintf(&path_packidx, "%s/%s", path_packdir,
1122 e09a504c 2019-06-28 stsp dent->d_name) == -1) {
1123 e09a504c 2019-06-28 stsp err = got_error_from_errno("asprintf");
1127 e09a504c 2019-06-28 stsp err = got_packidx_open(&packidx, path_packidx, 0);
1128 e09a504c 2019-06-28 stsp free(path_packidx);
1132 dd88155e 2019-06-29 stsp err = got_packidx_match_id_str_prefix(&matched_ids,
1133 4277420a 2019-06-29 stsp packidx, id_str_prefix);
1134 4277420a 2019-06-29 stsp if (err) {
1135 4277420a 2019-06-29 stsp got_packidx_close(packidx);
1138 e09a504c 2019-06-28 stsp err = got_packidx_close(packidx);
1142 dd88155e 2019-06-29 stsp SIMPLEQ_FOREACH(qid, &matched_ids, entry) {
1143 dd88155e 2019-06-29 stsp if (obj_type != GOT_OBJ_TYPE_ANY) {
1144 dd88155e 2019-06-29 stsp int matched_type;
1145 dd88155e 2019-06-29 stsp err = got_object_get_type(&matched_type, repo,
1148 dd88155e 2019-06-29 stsp goto done;
1149 dd88155e 2019-06-29 stsp if (matched_type != obj_type)
1152 4277420a 2019-06-29 stsp if (*unique_id == NULL) {
1153 dd88155e 2019-06-29 stsp *unique_id = got_object_id_dup(qid->id);
1154 dd88155e 2019-06-29 stsp if (*unique_id == NULL) {
1155 dd88155e 2019-06-29 stsp err = got_error_from_errno("malloc");
1156 dd88155e 2019-06-29 stsp goto done;
1159 e09a504c 2019-06-28 stsp err = got_error(GOT_ERR_AMBIGUOUS_ID);
1160 561c3678 2019-07-02 stsp goto done;
1165 dd88155e 2019-06-29 stsp got_object_id_queue_free(&matched_ids);
1166 e09a504c 2019-06-28 stsp free(path_packdir);
1167 e09a504c 2019-06-28 stsp if (packdir && closedir(packdir) != 0 && err == NULL)
1168 e09a504c 2019-06-28 stsp err = got_error_from_errno("closedir");
1169 e09a504c 2019-06-28 stsp if (err) {
1170 e09a504c 2019-06-28 stsp free(*unique_id);
1171 e09a504c 2019-06-28 stsp *unique_id = NULL;
1173 e09a504c 2019-06-28 stsp return err;
1176 e09a504c 2019-06-28 stsp static const struct got_error *
1177 4277420a 2019-06-29 stsp match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1178 dd88155e 2019-06-29 stsp const char *object_dir, const char *id_str_prefix, int obj_type,
1179 e09a504c 2019-06-28 stsp struct got_repository *repo)
1181 e09a504c 2019-06-28 stsp const struct got_error *err = NULL;
1182 e09a504c 2019-06-28 stsp char *path;
1183 e09a504c 2019-06-28 stsp DIR *dir = NULL;
1184 e09a504c 2019-06-28 stsp struct dirent *dent;
1185 e09a504c 2019-06-28 stsp struct got_object_id id;
1187 e09a504c 2019-06-28 stsp if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1188 e09a504c 2019-06-28 stsp err = got_error_from_errno("asprintf");
1189 e09a504c 2019-06-28 stsp goto done;
1192 e09a504c 2019-06-28 stsp dir = opendir(path);
1193 e09a504c 2019-06-28 stsp if (dir == NULL) {
1194 4277420a 2019-06-29 stsp if (errno == ENOENT) {
1195 4277420a 2019-06-29 stsp err = NULL;
1196 4277420a 2019-06-29 stsp goto done;
1198 e09a504c 2019-06-28 stsp err = got_error_from_errno2("opendir", path);
1199 e09a504c 2019-06-28 stsp goto done;
1201 e09a504c 2019-06-28 stsp while ((dent = readdir(dir)) != NULL) {
1202 e09a504c 2019-06-28 stsp char *id_str;
1205 e09a504c 2019-06-28 stsp if (strcmp(dent->d_name, ".") == 0 ||
1206 e09a504c 2019-06-28 stsp strcmp(dent->d_name, "..") == 0)
1209 e09a504c 2019-06-28 stsp if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1210 e09a504c 2019-06-28 stsp err = got_error_from_errno("asprintf");
1211 e09a504c 2019-06-28 stsp goto done;
1214 e09a504c 2019-06-28 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
1218 52d1d0d9 2019-07-07 stsp * Directory entries do not necessarily appear in
1219 52d1d0d9 2019-07-07 stsp * sorted order, so we must iterate over all of them.
1221 5903ff6e 2019-06-29 stsp cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1222 52d1d0d9 2019-07-07 stsp if (cmp != 0) {
1223 e09a504c 2019-06-28 stsp free(id_str);
1227 e09a504c 2019-06-28 stsp if (*unique_id == NULL) {
1228 dd88155e 2019-06-29 stsp if (obj_type != GOT_OBJ_TYPE_ANY) {
1229 dd88155e 2019-06-29 stsp int matched_type;
1230 dd88155e 2019-06-29 stsp err = got_object_get_type(&matched_type, repo,
1233 dd88155e 2019-06-29 stsp goto done;
1234 dd88155e 2019-06-29 stsp if (matched_type != obj_type)
1237 e09a504c 2019-06-28 stsp *unique_id = got_object_id_dup(&id);
1238 e09a504c 2019-06-28 stsp if (*unique_id == NULL) {
1239 e09a504c 2019-06-28 stsp err = got_error_from_errno("got_object_id_dup");
1240 e09a504c 2019-06-28 stsp free(id_str);
1241 e09a504c 2019-06-28 stsp goto done;
1244 e09a504c 2019-06-28 stsp err = got_error(GOT_ERR_AMBIGUOUS_ID);
1245 e09a504c 2019-06-28 stsp free(id_str);
1246 e09a504c 2019-06-28 stsp goto done;
1250 b2df341b 2019-06-29 stsp if (dir && closedir(dir) != 0 && err == NULL)
1251 b2df341b 2019-06-29 stsp err = got_error_from_errno("closedir");
1252 e09a504c 2019-06-28 stsp if (err) {
1253 e09a504c 2019-06-28 stsp free(*unique_id);
1254 e09a504c 2019-06-28 stsp *unique_id = NULL;
1256 e09a504c 2019-06-28 stsp free(path);
1257 e09a504c 2019-06-28 stsp return err;
1260 e09a504c 2019-06-28 stsp const struct got_error *
1261 4277420a 2019-06-29 stsp got_repo_match_object_id_prefix(struct got_object_id **id,
1262 dd88155e 2019-06-29 stsp const char *id_str_prefix, int obj_type, struct got_repository *repo)
1264 e09a504c 2019-06-28 stsp const struct got_error *err = NULL;
1265 e09a504c 2019-06-28 stsp char *path_objects = got_repo_get_path_objects(repo);
1266 e09a504c 2019-06-28 stsp char *object_dir = NULL;
1267 e09a504c 2019-06-28 stsp size_t len;
1270 4277420a 2019-06-29 stsp *id = NULL;
1272 4277420a 2019-06-29 stsp for (i = 0; i < strlen(id_str_prefix); i++) {
1273 4277420a 2019-06-29 stsp if (isxdigit((unsigned char)id_str_prefix[i]))
1275 6dd1ece6 2019-11-10 stsp return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1278 e09a504c 2019-06-28 stsp len = strlen(id_str_prefix);
1279 e09a504c 2019-06-28 stsp if (len >= 2) {
1280 dd88155e 2019-06-29 stsp err = match_packed_object(id, repo, id_str_prefix, obj_type);
1282 83c8b3b8 2019-06-29 stsp goto done;
1283 e09a504c 2019-06-28 stsp object_dir = strndup(id_str_prefix, 2);
1284 83c8b3b8 2019-06-29 stsp if (object_dir == NULL) {
1285 83c8b3b8 2019-06-29 stsp err = got_error_from_errno("strdup");
1286 83c8b3b8 2019-06-29 stsp goto done;
1288 4277420a 2019-06-29 stsp err = match_loose_object(id, path_objects, object_dir,
1289 dd88155e 2019-06-29 stsp id_str_prefix, obj_type, repo);
1290 e09a504c 2019-06-28 stsp } else if (len == 1) {
1292 e09a504c 2019-06-28 stsp for (i = 0; i < 0xf; i++) {
1293 e09a504c 2019-06-28 stsp if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1295 83c8b3b8 2019-06-29 stsp err = got_error_from_errno("asprintf");
1296 83c8b3b8 2019-06-29 stsp goto done;
1298 dd88155e 2019-06-29 stsp err = match_packed_object(id, repo, object_dir,
1299 dd88155e 2019-06-29 stsp obj_type);
1301 83c8b3b8 2019-06-29 stsp goto done;
1302 4277420a 2019-06-29 stsp err = match_loose_object(id, path_objects, object_dir,
1303 dd88155e 2019-06-29 stsp id_str_prefix, obj_type, repo);
1305 83c8b3b8 2019-06-29 stsp goto done;
1308 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1309 83c8b3b8 2019-06-29 stsp goto done;
1312 e09a504c 2019-06-28 stsp free(object_dir);
1313 4277420a 2019-06-29 stsp if (err) {
1314 4277420a 2019-06-29 stsp free(*id);
1315 4277420a 2019-06-29 stsp *id = NULL;
1316 4277420a 2019-06-29 stsp } else if (*id == NULL)
1317 4277420a 2019-06-29 stsp err = got_error(GOT_ERR_NO_OBJ);
1319 303e2782 2019-08-09 stsp return err;
1322 303e2782 2019-08-09 stsp const struct got_error *
1323 303e2782 2019-08-09 stsp got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1324 303e2782 2019-08-09 stsp int obj_type, struct got_repository *repo)
1326 303e2782 2019-08-09 stsp const struct got_error *err;
1327 303e2782 2019-08-09 stsp struct got_reflist_head refs;
1328 303e2782 2019-08-09 stsp struct got_reflist_entry *re;
1329 303e2782 2019-08-09 stsp struct got_object_id *tag_id;
1331 303e2782 2019-08-09 stsp SIMPLEQ_INIT(&refs);
1332 303e2782 2019-08-09 stsp *tag = NULL;
1334 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_by_name, NULL);
1336 303e2782 2019-08-09 stsp return err;
1338 303e2782 2019-08-09 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
1339 303e2782 2019-08-09 stsp const char *refname;
1340 303e2782 2019-08-09 stsp refname = got_ref_get_name(re->ref);
1341 29606af7 2019-08-23 stsp if (got_ref_is_symbolic(re->ref))
1343 29606af7 2019-08-23 stsp refname += strlen("refs/tags/");
1344 303e2782 2019-08-09 stsp if (strcmp(refname, name) != 0)
1346 303e2782 2019-08-09 stsp err = got_ref_resolve(&tag_id, repo, re->ref);
1349 303e2782 2019-08-09 stsp err = got_object_open_as_tag(tag, repo, tag_id);
1350 303e2782 2019-08-09 stsp free(tag_id);
1353 d24820bf 2019-08-11 stsp if (obj_type == GOT_OBJ_TYPE_ANY ||
1354 d24820bf 2019-08-11 stsp got_object_tag_get_object_type(*tag) == obj_type)
1356 303e2782 2019-08-09 stsp got_object_tag_close(*tag);
1357 303e2782 2019-08-09 stsp *tag = NULL;
1360 303e2782 2019-08-09 stsp got_ref_list_free(&refs);
1361 303e2782 2019-08-09 stsp if (err == NULL && *tag == NULL)
1362 303e2782 2019-08-09 stsp err = got_error(GOT_ERR_NO_OBJ);
1363 e09a504c 2019-06-28 stsp return err;
1366 3ce1b845 2019-07-15 stsp static const struct got_error *
1367 3ce1b845 2019-07-15 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1368 3ce1b845 2019-07-15 stsp const char *name, mode_t mode, struct got_object_id *blob_id)
1370 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
1372 3ce1b845 2019-07-15 stsp *new_te = NULL;
1374 3ce1b845 2019-07-15 stsp *new_te = calloc(1, sizeof(**new_te));
1375 3ce1b845 2019-07-15 stsp if (*new_te == NULL)
1376 3ce1b845 2019-07-15 stsp return got_error_from_errno("calloc");
1378 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1379 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
1380 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
1381 3ce1b845 2019-07-15 stsp goto done;
1384 3ce1b845 2019-07-15 stsp (*new_te)->mode = S_IFREG | (mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
1385 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1387 3ce1b845 2019-07-15 stsp if (err && *new_te) {
1388 56e0773d 2019-11-28 stsp free(*new_te);
1389 3ce1b845 2019-07-15 stsp *new_te = NULL;
1391 3ce1b845 2019-07-15 stsp return err;
1394 3ce1b845 2019-07-15 stsp static const struct got_error *
1395 3ce1b845 2019-07-15 stsp import_file(struct got_tree_entry **new_te, struct dirent *de,
1396 3ce1b845 2019-07-15 stsp const char *path, struct got_repository *repo)
1398 3ce1b845 2019-07-15 stsp const struct got_error *err;
1399 3ce1b845 2019-07-15 stsp struct got_object_id *blob_id = NULL;
1400 3ce1b845 2019-07-15 stsp char *filepath;
1401 3ce1b845 2019-07-15 stsp struct stat sb;
1403 3ce1b845 2019-07-15 stsp if (asprintf(&filepath, "%s%s%s", path,
1404 3ce1b845 2019-07-15 stsp path[0] == '\0' ? "" : "/", de->d_name) == -1)
1405 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
1407 3ce1b845 2019-07-15 stsp if (lstat(filepath, &sb) != 0) {
1408 3ce1b845 2019-07-15 stsp err = got_error_from_errno2("lstat", path);
1409 3ce1b845 2019-07-15 stsp goto done;
1412 3ce1b845 2019-07-15 stsp err = got_object_blob_create(&blob_id, filepath, repo);
1414 3ce1b845 2019-07-15 stsp goto done;
1416 3ce1b845 2019-07-15 stsp err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1419 3ce1b845 2019-07-15 stsp free(filepath);
1421 3ce1b845 2019-07-15 stsp free(blob_id);
1422 3ce1b845 2019-07-15 stsp return err;
1425 3ce1b845 2019-07-15 stsp static const struct got_error *
1426 3ce1b845 2019-07-15 stsp insert_tree_entry(struct got_tree_entry *new_te,
1427 3ce1b845 2019-07-15 stsp struct got_pathlist_head *paths)
1429 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
1430 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *new_pe;
1432 3ce1b845 2019-07-15 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1434 3ce1b845 2019-07-15 stsp return err;
1435 3ce1b845 2019-07-15 stsp if (new_pe == NULL)
1436 3ce1b845 2019-07-15 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
1437 3ce1b845 2019-07-15 stsp return NULL;
1440 3ce1b845 2019-07-15 stsp static const struct got_error *write_tree(struct got_object_id **,
1441 3ce1b845 2019-07-15 stsp const char *, struct got_pathlist_head *, struct got_repository *,
1442 3ce1b845 2019-07-15 stsp got_repo_import_cb progress_cb, void *progress_arg);
1444 3ce1b845 2019-07-15 stsp static const struct got_error *
1445 3ce1b845 2019-07-15 stsp import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1446 3ce1b845 2019-07-15 stsp const char *path, struct got_pathlist_head *ignores,
1447 3ce1b845 2019-07-15 stsp struct got_repository *repo,
1448 3ce1b845 2019-07-15 stsp got_repo_import_cb progress_cb, void *progress_arg)
1450 3ce1b845 2019-07-15 stsp const struct got_error *err;
1451 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
1452 3ce1b845 2019-07-15 stsp char *subdirpath;
1454 3ce1b845 2019-07-15 stsp if (asprintf(&subdirpath, "%s%s%s", path,
1455 3ce1b845 2019-07-15 stsp path[0] == '\0' ? "" : "/", de->d_name) == -1)
1456 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
1458 3ce1b845 2019-07-15 stsp (*new_te) = calloc(1, sizeof(**new_te));
1459 d6fca0ba 2019-09-15 hiltjo if (*new_te == NULL)
1460 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
1461 3ce1b845 2019-07-15 stsp (*new_te)->mode = S_IFDIR;
1462 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1463 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
1464 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
1465 3ce1b845 2019-07-15 stsp goto done;
1467 56e0773d 2019-11-28 stsp err = write_tree(&id, subdirpath, ignores, repo,
1468 3ce1b845 2019-07-15 stsp progress_cb, progress_arg);
1470 56e0773d 2019-11-28 stsp goto done;
1471 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1475 3ce1b845 2019-07-15 stsp free(subdirpath);
1476 3ce1b845 2019-07-15 stsp if (err) {
1477 56e0773d 2019-11-28 stsp free(*new_te);
1478 3ce1b845 2019-07-15 stsp *new_te = NULL;
1480 3ce1b845 2019-07-15 stsp return err;
1483 3ce1b845 2019-07-15 stsp static const struct got_error *
1484 3ce1b845 2019-07-15 stsp write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1485 3ce1b845 2019-07-15 stsp struct got_pathlist_head *ignores, struct got_repository *repo,
1486 3ce1b845 2019-07-15 stsp got_repo_import_cb progress_cb, void *progress_arg)
1488 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
1490 3ce1b845 2019-07-15 stsp struct dirent *de;
1491 56e0773d 2019-11-28 stsp int nentries;
1492 3ce1b845 2019-07-15 stsp struct got_tree_entry *new_te = NULL;
1493 3ce1b845 2019-07-15 stsp struct got_pathlist_head paths;
1494 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
1496 3ce1b845 2019-07-15 stsp *new_tree_id = NULL;
1498 3ce1b845 2019-07-15 stsp TAILQ_INIT(&paths);
1500 3ce1b845 2019-07-15 stsp dir = opendir(path_dir);
1501 3ce1b845 2019-07-15 stsp if (dir == NULL) {
1502 3ce1b845 2019-07-15 stsp err = got_error_from_errno2("opendir", path_dir);
1503 3ce1b845 2019-07-15 stsp goto done;
1506 56e0773d 2019-11-28 stsp nentries = 0;
1507 3ce1b845 2019-07-15 stsp while ((de = readdir(dir)) != NULL) {
1508 3ce1b845 2019-07-15 stsp int ignore = 0;
1510 3ce1b845 2019-07-15 stsp if (strcmp(de->d_name, ".") == 0 ||
1511 3ce1b845 2019-07-15 stsp strcmp(de->d_name, "..") == 0)
1514 3ce1b845 2019-07-15 stsp TAILQ_FOREACH(pe, ignores, entry) {
1515 3ce1b845 2019-07-15 stsp if (fnmatch(pe->path, de->d_name, 0) == 0) {
1516 3ce1b845 2019-07-15 stsp ignore = 1;
1520 3ce1b845 2019-07-15 stsp if (ignore)
1522 3ce1b845 2019-07-15 stsp if (de->d_type == DT_DIR) {
1523 3ce1b845 2019-07-15 stsp err = import_subdir(&new_te, de, path_dir,
1524 3ce1b845 2019-07-15 stsp ignores, repo, progress_cb, progress_arg);
1525 db1d3576 2019-10-04 stsp if (err) {
1526 db1d3576 2019-10-04 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY)
1527 db1d3576 2019-10-04 stsp goto done;
1528 db1d3576 2019-10-04 stsp err = NULL;
1531 3ce1b845 2019-07-15 stsp } else if (de->d_type == DT_REG) {
1532 3ce1b845 2019-07-15 stsp err = import_file(&new_te, de, path_dir, repo);
1534 3ce1b845 2019-07-15 stsp goto done;
1538 3ce1b845 2019-07-15 stsp err = insert_tree_entry(new_te, &paths);
1540 3ce1b845 2019-07-15 stsp goto done;
1541 56e0773d 2019-11-28 stsp nentries++;
1544 db1d3576 2019-10-04 stsp if (TAILQ_EMPTY(&paths)) {
1545 db1d3576 2019-10-04 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1546 db1d3576 2019-10-04 stsp goto done;
1549 3ce1b845 2019-07-15 stsp TAILQ_FOREACH(pe, &paths, entry) {
1550 3ce1b845 2019-07-15 stsp struct got_tree_entry *te = pe->data;
1551 3ce1b845 2019-07-15 stsp char *path;
1552 3ce1b845 2019-07-15 stsp if (!S_ISREG(te->mode))
1554 3ce1b845 2019-07-15 stsp if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1555 3ce1b845 2019-07-15 stsp err = got_error_from_errno("asprintf");
1556 3ce1b845 2019-07-15 stsp goto done;
1558 3ce1b845 2019-07-15 stsp err = (*progress_cb)(progress_arg, path);
1559 3ce1b845 2019-07-15 stsp free(path);
1561 3ce1b845 2019-07-15 stsp goto done;
1564 56e0773d 2019-11-28 stsp err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1567 3ce1b845 2019-07-15 stsp closedir(dir);
1568 3ce1b845 2019-07-15 stsp got_pathlist_free(&paths);
1569 3ce1b845 2019-07-15 stsp return err;
1572 3ce1b845 2019-07-15 stsp const struct got_error *
1573 3ce1b845 2019-07-15 stsp got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1574 3ce1b845 2019-07-15 stsp const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1575 3ce1b845 2019-07-15 stsp struct got_repository *repo, got_repo_import_cb progress_cb,
1576 3ce1b845 2019-07-15 stsp void *progress_arg)
1578 3ce1b845 2019-07-15 stsp const struct got_error *err;
1579 3ce1b845 2019-07-15 stsp struct got_object_id *new_tree_id;
1581 3ce1b845 2019-07-15 stsp err = write_tree(&new_tree_id, path_dir, ignores, repo,
1582 3ce1b845 2019-07-15 stsp progress_cb, progress_arg);
1584 3ce1b845 2019-07-15 stsp return err;
1586 3ce1b845 2019-07-15 stsp err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1587 3ce1b845 2019-07-15 stsp author, time(NULL), author, time(NULL), logmsg, repo);
1588 3ce1b845 2019-07-15 stsp free(new_tree_id);
1589 3ce1b845 2019-07-15 stsp return err;