Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/uio.h>
20 #include <sys/socket.h>
21 #include <sys/stat.h>
22 #include <sys/mman.h>
23 #include <sys/syslimits.h>
25 #include <ctype.h>
26 #include <endian.h>
27 #include <fcntl.h>
28 #include <fnmatch.h>
29 #include <limits.h>
30 #include <dirent.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <sha1.h>
34 #include <string.h>
35 #include <time.h>
36 #include <unistd.h>
37 #include <zlib.h>
38 #include <errno.h>
39 #include <libgen.h>
40 #include <stdint.h>
41 #include <imsg.h>
42 #include <uuid.h>
44 #include "got_error.h"
45 #include "got_reference.h"
46 #include "got_repository.h"
47 #include "got_path.h"
48 #include "got_cancel.h"
49 #include "got_worktree.h"
50 #include "got_object.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_inflate.h"
54 #include "got_lib_object.h"
55 #include "got_lib_object_parse.h"
56 #include "got_lib_object_create.h"
57 #include "got_lib_pack.h"
58 #include "got_lib_privsep.h"
59 #include "got_lib_worktree.h"
60 #include "got_lib_sha1.h"
61 #include "got_lib_object_cache.h"
62 #include "got_lib_repository.h"
64 #ifndef nitems
65 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
66 #endif
68 const char *
69 got_repo_get_path(struct got_repository *repo)
70 {
71 return repo->path;
72 }
74 const char *
75 got_repo_get_path_git_dir(struct got_repository *repo)
76 {
77 return repo->path_git_dir;
78 }
80 const char *
81 got_repo_get_gitconfig_author_name(struct got_repository *repo)
82 {
83 return repo->gitconfig_author_name;
84 }
86 const char *
87 got_repo_get_gitconfig_author_email(struct got_repository *repo)
88 {
89 return repo->gitconfig_author_email;
90 }
92 const char *
93 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
94 {
95 return repo->global_gitconfig_author_name;
96 }
98 const char *
99 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
101 return repo->global_gitconfig_author_email;
104 const char *
105 got_repo_get_gitconfig_owner(struct got_repository *repo)
107 return repo->gitconfig_owner;
110 int
111 got_repo_is_bare(struct got_repository *repo)
113 return (strcmp(repo->path, repo->path_git_dir) == 0);
116 static char *
117 get_path_git_child(struct got_repository *repo, const char *basename)
119 char *path_child;
121 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
122 basename) == -1)
123 return NULL;
125 return path_child;
128 char *
129 got_repo_get_path_objects(struct got_repository *repo)
131 return get_path_git_child(repo, GOT_OBJECTS_DIR);
134 char *
135 got_repo_get_path_objects_pack(struct got_repository *repo)
137 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
140 char *
141 got_repo_get_path_refs(struct got_repository *repo)
143 return get_path_git_child(repo, GOT_REFS_DIR);
146 char *
147 got_repo_get_path_packed_refs(struct got_repository *repo)
149 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
152 static char *
153 get_path_head(struct got_repository *repo)
155 return get_path_git_child(repo, GOT_HEAD_FILE);
158 char *
159 got_repo_get_path_gitconfig(struct got_repository *repo)
161 return get_path_git_child(repo, GOT_GITCONFIG);
164 char *
165 got_repo_get_path_gotconfig(struct got_repository *repo)
167 return get_path_git_child(repo, GOT_GOTCONFIG);
170 void
171 got_repo_get_gitconfig_remotes(int *nremotes, struct got_remote_repo **remotes,
172 struct got_repository *repo)
174 *nremotes = repo->ngitconfig_remotes;
175 *remotes = repo->gitconfig_remotes;
178 const char *
179 got_repo_get_gotconfig_author(struct got_repository *repo)
181 return repo->gotconfig_author;
184 void
185 got_repo_get_gotconfig_remotes(int *nremotes, struct got_remote_repo **remotes,
186 struct got_repository *repo)
188 *nremotes = repo->ngotconfig_remotes;
189 *remotes = repo->gotconfig_remotes;
192 static int
193 is_git_repo(struct got_repository *repo)
195 const char *path_git = got_repo_get_path_git_dir(repo);
196 char *path_objects = got_repo_get_path_objects(repo);
197 char *path_refs = got_repo_get_path_refs(repo);
198 char *path_head = get_path_head(repo);
199 int ret = 0;
200 struct stat sb;
201 struct got_reference *head_ref;
203 if (lstat(path_git, &sb) == -1)
204 goto done;
205 if (!S_ISDIR(sb.st_mode))
206 goto done;
208 if (lstat(path_objects, &sb) == -1)
209 goto done;
210 if (!S_ISDIR(sb.st_mode))
211 goto done;
213 if (lstat(path_refs, &sb) == -1)
214 goto done;
215 if (!S_ISDIR(sb.st_mode))
216 goto done;
218 if (lstat(path_head, &sb) == -1)
219 goto done;
220 if (!S_ISREG(sb.st_mode))
221 goto done;
223 /* Check if the HEAD reference can be opened. */
224 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
225 goto done;
226 got_ref_close(head_ref);
228 ret = 1;
229 done:
230 free(path_objects);
231 free(path_refs);
232 free(path_head);
233 return ret;
237 const struct got_error *
238 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
239 struct got_object *obj)
241 #ifndef GOT_NO_OBJ_CACHE
242 const struct got_error *err = NULL;
243 err = got_object_cache_add(&repo->objcache, id, obj);
244 if (err) {
245 if (err->code == GOT_ERR_OBJ_EXISTS ||
246 err->code == GOT_ERR_OBJ_TOO_LARGE)
247 err = NULL;
248 return err;
250 obj->refcnt++;
251 #endif
252 return NULL;
255 struct got_object *
256 got_repo_get_cached_object(struct got_repository *repo,
257 struct got_object_id *id)
259 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
262 const struct got_error *
263 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
264 struct got_tree_object *tree)
266 #ifndef GOT_NO_OBJ_CACHE
267 const struct got_error *err = NULL;
268 err = got_object_cache_add(&repo->treecache, id, tree);
269 if (err) {
270 if (err->code == GOT_ERR_OBJ_EXISTS ||
271 err->code == GOT_ERR_OBJ_TOO_LARGE)
272 err = NULL;
273 return err;
275 tree->refcnt++;
276 #endif
277 return NULL;
280 struct got_tree_object *
281 got_repo_get_cached_tree(struct got_repository *repo,
282 struct got_object_id *id)
284 return (struct got_tree_object *)got_object_cache_get(
285 &repo->treecache, id);
288 const struct got_error *
289 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
290 struct got_commit_object *commit)
292 #ifndef GOT_NO_OBJ_CACHE
293 const struct got_error *err = NULL;
294 err = got_object_cache_add(&repo->commitcache, id, commit);
295 if (err) {
296 if (err->code == GOT_ERR_OBJ_EXISTS ||
297 err->code == GOT_ERR_OBJ_TOO_LARGE)
298 err = NULL;
299 return err;
301 commit->refcnt++;
302 #endif
303 return NULL;
306 struct got_commit_object *
307 got_repo_get_cached_commit(struct got_repository *repo,
308 struct got_object_id *id)
310 return (struct got_commit_object *)got_object_cache_get(
311 &repo->commitcache, id);
314 const struct got_error *
315 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
316 struct got_tag_object *tag)
318 #ifndef GOT_NO_OBJ_CACHE
319 const struct got_error *err = NULL;
320 err = got_object_cache_add(&repo->tagcache, id, tag);
321 if (err) {
322 if (err->code == GOT_ERR_OBJ_EXISTS ||
323 err->code == GOT_ERR_OBJ_TOO_LARGE)
324 err = NULL;
325 return err;
327 tag->refcnt++;
328 #endif
329 return NULL;
332 struct got_tag_object *
333 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
335 return (struct got_tag_object *)got_object_cache_get(
336 &repo->tagcache, id);
339 const struct got_error *
340 open_repo(struct got_repository *repo, const char *path)
342 const struct got_error *err = NULL;
344 /* bare git repository? */
345 repo->path_git_dir = strdup(path);
346 if (repo->path_git_dir == NULL)
347 return got_error_from_errno("strdup");
348 if (is_git_repo(repo)) {
349 repo->path = strdup(repo->path_git_dir);
350 if (repo->path == NULL) {
351 err = got_error_from_errno("strdup");
352 goto done;
354 return NULL;
357 /* git repository with working tree? */
358 free(repo->path_git_dir);
359 repo->path_git_dir = NULL;
360 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
361 err = got_error_from_errno("asprintf");
362 goto done;
364 if (is_git_repo(repo)) {
365 repo->path = strdup(path);
366 if (repo->path == NULL) {
367 err = got_error_from_errno("strdup");
368 goto done;
370 return NULL;
373 err = got_error(GOT_ERR_NOT_GIT_REPO);
374 done:
375 if (err) {
376 free(repo->path);
377 repo->path = NULL;
378 free(repo->path_git_dir);
379 repo->path_git_dir = NULL;
381 return err;
384 static const struct got_error *
385 parse_gitconfig_file(int *gitconfig_repository_format_version,
386 char **gitconfig_author_name, char **gitconfig_author_email,
387 struct got_remote_repo **remotes, int *nremotes,
388 char **gitconfig_owner,
389 const char *gitconfig_path)
391 const struct got_error *err = NULL, *child_err = NULL;
392 int fd = -1;
393 int imsg_fds[2] = { -1, -1 };
394 pid_t pid;
395 struct imsgbuf *ibuf;
397 *gitconfig_repository_format_version = 0;
398 *gitconfig_author_name = NULL;
399 *gitconfig_author_email = NULL;
400 if (remotes)
401 *remotes = NULL;
402 if (nremotes)
403 *nremotes = 0;
404 if (gitconfig_owner)
405 *gitconfig_owner = NULL;
407 fd = open(gitconfig_path, O_RDONLY);
408 if (fd == -1) {
409 if (errno == ENOENT)
410 return NULL;
411 return got_error_from_errno2("open", gitconfig_path);
414 ibuf = calloc(1, sizeof(*ibuf));
415 if (ibuf == NULL) {
416 err = got_error_from_errno("calloc");
417 goto done;
420 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
421 err = got_error_from_errno("socketpair");
422 goto done;
425 pid = fork();
426 if (pid == -1) {
427 err = got_error_from_errno("fork");
428 goto done;
429 } else if (pid == 0) {
430 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
431 gitconfig_path);
432 /* not reached */
435 if (close(imsg_fds[1]) == -1) {
436 err = got_error_from_errno("close");
437 goto done;
439 imsg_fds[1] = -1;
440 imsg_init(ibuf, imsg_fds[0]);
442 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
443 if (err)
444 goto done;
445 fd = -1;
447 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
448 if (err)
449 goto done;
451 err = got_privsep_recv_gitconfig_int(
452 gitconfig_repository_format_version, ibuf);
453 if (err)
454 goto done;
456 err = got_privsep_send_gitconfig_author_name_req(ibuf);
457 if (err)
458 goto done;
460 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
461 if (err)
462 goto done;
464 err = got_privsep_send_gitconfig_author_email_req(ibuf);
465 if (err)
466 goto done;
468 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
469 if (err)
470 goto done;
472 if (remotes && nremotes) {
473 err = got_privsep_send_gitconfig_remotes_req(ibuf);
474 if (err)
475 goto done;
477 err = got_privsep_recv_gitconfig_remotes(remotes,
478 nremotes, ibuf);
479 if (err)
480 goto done;
483 if (gitconfig_owner) {
484 err = got_privsep_send_gitconfig_owner_req(ibuf);
485 if (err)
486 goto done;
487 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
488 if (err)
489 goto done;
492 imsg_clear(ibuf);
493 err = got_privsep_send_stop(imsg_fds[0]);
494 child_err = got_privsep_wait_for_child(pid);
495 if (child_err && err == NULL)
496 err = child_err;
497 done:
498 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
499 err = got_error_from_errno("close");
500 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
501 err = got_error_from_errno("close");
502 if (fd != -1 && close(fd) == -1 && err == NULL)
503 err = got_error_from_errno2("close", gitconfig_path);
504 free(ibuf);
505 return err;
508 static const struct got_error *
509 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
511 const struct got_error *err = NULL;
512 char *repo_gitconfig_path = NULL;
514 if (global_gitconfig_path) {
515 /* Read settings from ~/.gitconfig. */
516 int dummy_repo_version;
517 err = parse_gitconfig_file(&dummy_repo_version,
518 &repo->global_gitconfig_author_name,
519 &repo->global_gitconfig_author_email,
520 NULL, NULL, NULL, global_gitconfig_path);
521 if (err)
522 return err;
525 /* Read repository's .git/config file. */
526 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
527 if (repo_gitconfig_path == NULL)
528 return got_error_from_errno("got_repo_get_path_gitconfig");
530 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
531 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
532 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
533 &repo->gitconfig_owner, repo_gitconfig_path);
534 if (err)
535 goto done;
536 done:
537 free(repo_gitconfig_path);
538 return err;
541 static const struct got_error *
542 parse_gotconfig_file(char **author,
543 struct got_remote_repo **remotes, int *nremotes,
544 const char *gotconfig_path)
546 const struct got_error *err = NULL, *child_err = NULL;
547 int fd = -1;
548 int imsg_fds[2] = { -1, -1 };
549 pid_t pid;
550 struct imsgbuf *ibuf;
552 if (author)
553 *author = NULL;
554 if (remotes)
555 *remotes = NULL;
556 if (nremotes)
557 *nremotes = 0;
559 fd = open(gotconfig_path, O_RDONLY);
560 if (fd == -1) {
561 if (errno == ENOENT)
562 return NULL;
563 return got_error_from_errno2("open", gotconfig_path);
566 ibuf = calloc(1, sizeof(*ibuf));
567 if (ibuf == NULL) {
568 err = got_error_from_errno("calloc");
569 goto done;
572 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
573 err = got_error_from_errno("socketpair");
574 goto done;
577 pid = fork();
578 if (pid == -1) {
579 err = got_error_from_errno("fork");
580 goto done;
581 } else if (pid == 0) {
582 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GOTCONFIG,
583 gotconfig_path);
584 /* not reached */
587 if (close(imsg_fds[1]) == -1) {
588 err = got_error_from_errno("close");
589 goto done;
591 imsg_fds[1] = -1;
592 imsg_init(ibuf, imsg_fds[0]);
594 err = got_privsep_send_gotconfig_parse_req(ibuf, fd);
595 if (err)
596 goto done;
597 fd = -1;
599 if (author) {
600 err = got_privsep_send_gotconfig_author_req(ibuf);
601 if (err)
602 goto done;
604 err = got_privsep_recv_gotconfig_str(author, ibuf);
605 if (err)
606 goto done;
609 if (remotes && nremotes) {
610 err = got_privsep_send_gotconfig_remotes_req(ibuf);
611 if (err)
612 goto done;
614 err = got_privsep_recv_gotconfig_remotes(remotes,
615 nremotes, ibuf);
616 if (err)
617 goto done;
620 imsg_clear(ibuf);
621 err = got_privsep_send_stop(imsg_fds[0]);
622 child_err = got_privsep_wait_for_child(pid);
623 if (child_err && err == NULL)
624 err = child_err;
625 done:
626 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
627 err = got_error_from_errno("close");
628 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
629 err = got_error_from_errno("close");
630 if (fd != -1 && close(fd) == -1 && err == NULL)
631 err = got_error_from_errno2("close", gotconfig_path);
632 if (err) {
633 if (author) {
634 free(*author);
635 *author = NULL;
638 free(ibuf);
639 return err;
642 static const struct got_error *
643 read_gotconfig(struct got_repository *repo)
645 const struct got_error *err = NULL;
646 char *gotconfig_path;
648 gotconfig_path = got_repo_get_path_gotconfig(repo);
649 if (gotconfig_path == NULL)
650 return got_error_from_errno("got_repo_get_path_gotconfig");
652 err = parse_gotconfig_file(&repo->gotconfig_author,
653 &repo->gotconfig_remotes, &repo->ngotconfig_remotes,
654 gotconfig_path);
655 free(gotconfig_path);
656 return err;
659 const struct got_error *
660 got_repo_open(struct got_repository **repop, const char *path,
661 const char *global_gitconfig_path)
663 struct got_repository *repo = NULL;
664 const struct got_error *err = NULL;
665 char *abspath;
666 int i, tried_root = 0;
668 *repop = NULL;
670 if (got_path_is_absolute(path))
671 abspath = strdup(path);
672 else
673 abspath = got_path_get_absolute(path);
674 if (abspath == NULL)
675 return got_error_path(path, GOT_ERR_BAD_PATH);
677 repo = calloc(1, sizeof(*repo));
678 if (repo == NULL) {
679 err = got_error_from_errno("calloc");
680 goto done;
683 for (i = 0; i < nitems(repo->privsep_children); i++) {
684 memset(&repo->privsep_children[i], 0,
685 sizeof(repo->privsep_children[0]));
686 repo->privsep_children[i].imsg_fd = -1;
689 err = got_object_cache_init(&repo->objcache,
690 GOT_OBJECT_CACHE_TYPE_OBJ);
691 if (err)
692 goto done;
693 err = got_object_cache_init(&repo->treecache,
694 GOT_OBJECT_CACHE_TYPE_TREE);
695 if (err)
696 goto done;
697 err = got_object_cache_init(&repo->commitcache,
698 GOT_OBJECT_CACHE_TYPE_COMMIT);
699 if (err)
700 goto done;
701 err = got_object_cache_init(&repo->tagcache,
702 GOT_OBJECT_CACHE_TYPE_TAG);
703 if (err)
704 goto done;
706 path = realpath(abspath, NULL);
707 if (path == NULL) {
708 err = got_error_from_errno2("realpath", abspath);
709 goto done;
712 do {
713 err = open_repo(repo, path);
714 if (err == NULL)
715 break;
716 if (err->code != GOT_ERR_NOT_GIT_REPO)
717 break;
718 if (path[0] == '/' && path[1] == '\0') {
719 if (tried_root) {
720 err = got_error(GOT_ERR_NOT_GIT_REPO);
721 goto done;
723 tried_root = 1;
725 path = dirname(path);
726 if (path == NULL) {
727 err = got_error_from_errno2("dirname", path);
728 goto done;
730 } while (path);
732 err = read_gotconfig(repo);
733 if (err)
734 goto done;
736 err = read_gitconfig(repo, global_gitconfig_path);
737 if (err)
738 goto done;
739 if (repo->gitconfig_repository_format_version != 0)
740 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
741 done:
742 if (err)
743 got_repo_close(repo);
744 else
745 *repop = repo;
746 free(abspath);
747 return err;
750 const struct got_error *
751 got_repo_close(struct got_repository *repo)
753 const struct got_error *err = NULL, *child_err;
754 int i;
756 for (i = 0; i < nitems(repo->packidx_cache); i++) {
757 if (repo->packidx_cache[i] == NULL)
758 break;
759 got_packidx_close(repo->packidx_cache[i]);
762 for (i = 0; i < nitems(repo->packs); i++) {
763 if (repo->packs[i].path_packfile == NULL)
764 break;
765 got_pack_close(&repo->packs[i]);
768 free(repo->path);
769 free(repo->path_git_dir);
771 got_object_cache_close(&repo->objcache);
772 got_object_cache_close(&repo->treecache);
773 got_object_cache_close(&repo->commitcache);
774 got_object_cache_close(&repo->tagcache);
776 for (i = 0; i < nitems(repo->privsep_children); i++) {
777 if (repo->privsep_children[i].imsg_fd == -1)
778 continue;
779 imsg_clear(repo->privsep_children[i].ibuf);
780 free(repo->privsep_children[i].ibuf);
781 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
782 child_err = got_privsep_wait_for_child(
783 repo->privsep_children[i].pid);
784 if (child_err && err == NULL)
785 err = child_err;
786 if (close(repo->privsep_children[i].imsg_fd) != 0 &&
787 err == NULL)
788 err = got_error_from_errno("close");
791 free(repo->gotconfig_author);
792 for (i = 0; i < repo->ngotconfig_remotes; i++) {
793 free(repo->gotconfig_remotes[i].name);
794 free(repo->gotconfig_remotes[i].url);
796 free(repo->gotconfig_remotes);
797 free(repo->gitconfig_author_name);
798 free(repo->gitconfig_author_email);
799 for (i = 0; i < repo->ngitconfig_remotes; i++) {
800 free(repo->gitconfig_remotes[i].name);
801 free(repo->gitconfig_remotes[i].url);
803 free(repo->gitconfig_remotes);
804 free(repo);
806 return err;
809 const struct got_error *
810 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
811 const char *input_path, int check_disk)
813 const struct got_error *err = NULL;
814 const char *repo_abspath = NULL;
815 size_t repolen, len;
816 char *canonpath, *path = NULL;
818 *in_repo_path = NULL;
820 canonpath = strdup(input_path);
821 if (canonpath == NULL) {
822 err = got_error_from_errno("strdup");
823 goto done;
825 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
826 if (err)
827 goto done;
829 repo_abspath = got_repo_get_path(repo);
831 if (!check_disk || canonpath[0] == '\0') {
832 path = strdup(canonpath);
833 if (path == NULL) {
834 err = got_error_from_errno("strdup");
835 goto done;
837 } else {
838 path = realpath(canonpath, NULL);
839 if (path == NULL) {
840 if (errno != ENOENT) {
841 err = got_error_from_errno2("realpath",
842 canonpath);
843 goto done;
845 /*
846 * Path is not on disk.
847 * Assume it is already relative to repository root.
848 */
849 path = strdup(canonpath);
850 if (path == NULL) {
851 err = got_error_from_errno("strdup");
852 goto done;
856 repolen = strlen(repo_abspath);
857 len = strlen(path);
860 if (strcmp(path, repo_abspath) == 0) {
861 free(path);
862 path = strdup("");
863 if (path == NULL) {
864 err = got_error_from_errno("strdup");
865 goto done;
867 } else if (len > repolen &&
868 got_path_is_child(path, repo_abspath, repolen)) {
869 /* Matched an on-disk path inside repository. */
870 if (got_repo_is_bare(repo)) {
871 /*
872 * Matched an on-disk path inside repository
873 * database. Treat as repository-relative.
874 */
875 } else {
876 char *child;
877 /* Strip common prefix with repository path. */
878 err = got_path_skip_common_ancestor(&child,
879 repo_abspath, path);
880 if (err)
881 goto done;
882 free(path);
883 path = child;
885 } else {
886 /*
887 * Matched unrelated on-disk path.
888 * Treat it as repository-relative.
889 */
893 /* Make in-repository path absolute */
894 if (path[0] != '/') {
895 char *abspath;
896 if (asprintf(&abspath, "/%s", path) == -1) {
897 err = got_error_from_errno("asprintf");
898 goto done;
900 free(path);
901 path = abspath;
904 done:
905 free(canonpath);
906 if (err)
907 free(path);
908 else
909 *in_repo_path = path;
910 return err;
913 static const struct got_error *
914 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
915 const char *path_packidx)
917 const struct got_error *err = NULL;
918 int i;
920 for (i = 0; i < nitems(repo->packidx_cache); i++) {
921 if (repo->packidx_cache[i] == NULL)
922 break;
923 if (strcmp(repo->packidx_cache[i]->path_packidx,
924 path_packidx) == 0) {
925 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
928 if (i == nitems(repo->packidx_cache)) {
929 err = got_packidx_close(repo->packidx_cache[i - 1]);
930 if (err)
931 return err;
934 /*
935 * Insert the new pack index at the front so it will
936 * be searched first in the future.
937 */
938 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
939 sizeof(repo->packidx_cache) -
940 sizeof(repo->packidx_cache[0]));
941 repo->packidx_cache[0] = packidx;
943 return NULL;
946 static int
947 is_packidx_filename(const char *name, size_t len)
949 if (len != GOT_PACKIDX_NAMELEN)
950 return 0;
952 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
953 return 0;
955 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
956 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
957 return 0;
959 return 1;
962 const struct got_error *
963 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
964 struct got_repository *repo, struct got_object_id *id)
966 const struct got_error *err;
967 char *path_packdir;
968 DIR *packdir;
969 struct dirent *dent;
970 char *path_packidx;
971 int i;
973 /* Search pack index cache. */
974 for (i = 0; i < nitems(repo->packidx_cache); i++) {
975 if (repo->packidx_cache[i] == NULL)
976 break;
977 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
978 if (*idx != -1) {
979 *packidx = repo->packidx_cache[i];
980 /*
981 * Move this cache entry to the front. Repeatedly
982 * searching a wrong pack index can be expensive.
983 */
984 if (i > 0) {
985 struct got_packidx *p;
986 p = repo->packidx_cache[0];
987 repo->packidx_cache[0] = *packidx;
988 repo->packidx_cache[i] = p;
990 return NULL;
993 /* No luck. Search the filesystem. */
995 path_packdir = got_repo_get_path_objects_pack(repo);
996 if (path_packdir == NULL)
997 return got_error_from_errno("got_repo_get_path_objects_pack");
999 packdir = opendir(path_packdir);
1000 if (packdir == NULL) {
1001 if (errno == ENOENT)
1002 err = got_error_no_obj(id);
1003 else
1004 err = got_error_from_errno2("opendir", path_packdir);
1005 goto done;
1008 while ((dent = readdir(packdir)) != NULL) {
1009 int is_cached = 0;
1011 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1012 continue;
1014 if (asprintf(&path_packidx, "%s/%s", path_packdir,
1015 dent->d_name) == -1) {
1016 err = got_error_from_errno("asprintf");
1017 goto done;
1020 for (i = 0; i < nitems(repo->packidx_cache); i++) {
1021 if (repo->packidx_cache[i] == NULL)
1022 break;
1023 if (strcmp(repo->packidx_cache[i]->path_packidx,
1024 path_packidx) == 0) {
1025 is_cached = 1;
1026 break;
1029 if (is_cached) {
1030 free(path_packidx);
1031 continue; /* already searched */
1034 err = got_packidx_open(packidx, path_packidx, 0);
1035 if (err) {
1036 free(path_packidx);
1037 goto done;
1040 err = cache_packidx(repo, *packidx, path_packidx);
1041 free(path_packidx);
1042 if (err)
1043 goto done;
1045 *idx = got_packidx_get_object_idx(*packidx, id);
1046 if (*idx != -1) {
1047 err = NULL; /* found the object */
1048 goto done;
1052 err = got_error_no_obj(id);
1053 done:
1054 free(path_packdir);
1055 if (packdir && closedir(packdir) != 0 && err == NULL)
1056 err = got_error_from_errno("closedir");
1057 return err;
1060 static const struct got_error *
1061 read_packfile_hdr(int fd, struct got_packidx *packidx)
1063 const struct got_error *err = NULL;
1064 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1065 struct got_packfile_hdr hdr;
1066 ssize_t n;
1068 n = read(fd, &hdr, sizeof(hdr));
1069 if (n < 0)
1070 return got_error_from_errno("read");
1071 if (n != sizeof(hdr))
1072 return got_error(GOT_ERR_BAD_PACKFILE);
1074 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1075 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1076 be32toh(hdr.nobjects) != totobj)
1077 err = got_error(GOT_ERR_BAD_PACKFILE);
1079 return err;
1082 static const struct got_error *
1083 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
1085 const struct got_error *err = NULL;
1087 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
1088 if (*fd == -1)
1089 return got_error_from_errno2("open", path_packfile);
1091 if (packidx) {
1092 err = read_packfile_hdr(*fd, packidx);
1093 if (err) {
1094 close(*fd);
1095 *fd = -1;
1099 return err;
1102 const struct got_error *
1103 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1104 const char *path_packfile, struct got_packidx *packidx)
1106 const struct got_error *err = NULL;
1107 struct got_pack *pack = NULL;
1108 struct stat sb;
1109 int i;
1111 if (packp)
1112 *packp = NULL;
1114 for (i = 0; i < nitems(repo->packs); i++) {
1115 pack = &repo->packs[i];
1116 if (pack->path_packfile == NULL)
1117 break;
1118 if (strcmp(pack->path_packfile, path_packfile) == 0)
1119 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1122 if (i == nitems(repo->packs) - 1) {
1123 err = got_pack_close(&repo->packs[i - 1]);
1124 if (err)
1125 return err;
1126 memmove(&repo->packs[1], &repo->packs[0],
1127 sizeof(repo->packs) - sizeof(repo->packs[0]));
1128 i = 0;
1131 pack = &repo->packs[i];
1133 pack->path_packfile = strdup(path_packfile);
1134 if (pack->path_packfile == NULL) {
1135 err = got_error_from_errno("strdup");
1136 goto done;
1139 err = open_packfile(&pack->fd, path_packfile, packidx);
1140 if (err)
1141 goto done;
1143 if (fstat(pack->fd, &sb) != 0) {
1144 err = got_error_from_errno("fstat");
1145 goto done;
1147 pack->filesize = sb.st_size;
1149 pack->privsep_child = NULL;
1151 #ifndef GOT_PACK_NO_MMAP
1152 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1153 pack->fd, 0);
1154 if (pack->map == MAP_FAILED) {
1155 if (errno != ENOMEM) {
1156 err = got_error_from_errno("mmap");
1157 goto done;
1159 pack->map = NULL; /* fall back to read(2) */
1161 #endif
1162 done:
1163 if (err) {
1164 if (pack) {
1165 free(pack->path_packfile);
1166 memset(pack, 0, sizeof(*pack));
1168 } else if (packp)
1169 *packp = pack;
1170 return err;
1173 struct got_pack *
1174 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1176 struct got_pack *pack = NULL;
1177 int i;
1179 for (i = 0; i < nitems(repo->packs); i++) {
1180 pack = &repo->packs[i];
1181 if (pack->path_packfile == NULL)
1182 break;
1183 if (strcmp(pack->path_packfile, path_packfile) == 0)
1184 return pack;
1187 return NULL;
1190 const struct got_error *
1191 got_repo_init(const char *repo_path)
1193 const struct got_error *err = NULL;
1194 const char *dirnames[] = {
1195 GOT_OBJECTS_DIR,
1196 GOT_OBJECTS_PACK_DIR,
1197 GOT_REFS_DIR,
1199 const char *description_str = "Unnamed repository; "
1200 "edit this file 'description' to name the repository.";
1201 const char *headref_str = "ref: refs/heads/main";
1202 const char *gitconfig_str = "[core]\n"
1203 "\trepositoryformatversion = 0\n"
1204 "\tfilemode = true\n"
1205 "\tbare = true\n";
1206 char *path;
1207 int i;
1209 if (!got_path_dir_is_empty(repo_path))
1210 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1212 for (i = 0; i < nitems(dirnames); i++) {
1213 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1214 return got_error_from_errno("asprintf");
1216 err = got_path_mkdir(path);
1217 free(path);
1218 if (err)
1219 return err;
1222 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1223 return got_error_from_errno("asprintf");
1224 err = got_path_create_file(path, description_str);
1225 free(path);
1226 if (err)
1227 return err;
1229 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1230 return got_error_from_errno("asprintf");
1231 err = got_path_create_file(path, headref_str);
1232 free(path);
1233 if (err)
1234 return err;
1236 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1237 return got_error_from_errno("asprintf");
1238 err = got_path_create_file(path, gitconfig_str);
1239 free(path);
1240 if (err)
1241 return err;
1243 return NULL;
1246 static const struct got_error *
1247 match_packed_object(struct got_object_id **unique_id,
1248 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1250 const struct got_error *err = NULL;
1251 char *path_packdir;
1252 DIR *packdir;
1253 struct dirent *dent;
1254 char *path_packidx;
1255 struct got_object_id_queue matched_ids;
1257 SIMPLEQ_INIT(&matched_ids);
1259 path_packdir = got_repo_get_path_objects_pack(repo);
1260 if (path_packdir == NULL)
1261 return got_error_from_errno("got_repo_get_path_objects_pack");
1263 packdir = opendir(path_packdir);
1264 if (packdir == NULL) {
1265 if (errno != ENOENT)
1266 err = got_error_from_errno2("opendir", path_packdir);
1267 goto done;
1270 while ((dent = readdir(packdir)) != NULL) {
1271 struct got_packidx *packidx;
1272 struct got_object_qid *qid;
1275 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1276 continue;
1278 if (asprintf(&path_packidx, "%s/%s", path_packdir,
1279 dent->d_name) == -1) {
1280 err = got_error_from_errno("asprintf");
1281 break;
1284 err = got_packidx_open(&packidx, path_packidx, 0);
1285 free(path_packidx);
1286 if (err)
1287 break;
1289 err = got_packidx_match_id_str_prefix(&matched_ids,
1290 packidx, id_str_prefix);
1291 if (err) {
1292 got_packidx_close(packidx);
1293 break;
1295 err = got_packidx_close(packidx);
1296 if (err)
1297 break;
1299 SIMPLEQ_FOREACH(qid, &matched_ids, entry) {
1300 if (obj_type != GOT_OBJ_TYPE_ANY) {
1301 int matched_type;
1302 err = got_object_get_type(&matched_type, repo,
1303 qid->id);
1304 if (err)
1305 goto done;
1306 if (matched_type != obj_type)
1307 continue;
1309 if (*unique_id == NULL) {
1310 *unique_id = got_object_id_dup(qid->id);
1311 if (*unique_id == NULL) {
1312 err = got_error_from_errno("malloc");
1313 goto done;
1315 } else {
1316 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1317 continue; /* packed multiple times */
1318 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1319 goto done;
1323 done:
1324 got_object_id_queue_free(&matched_ids);
1325 free(path_packdir);
1326 if (packdir && closedir(packdir) != 0 && err == NULL)
1327 err = got_error_from_errno("closedir");
1328 if (err) {
1329 free(*unique_id);
1330 *unique_id = NULL;
1332 return err;
1335 static const struct got_error *
1336 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1337 const char *object_dir, const char *id_str_prefix, int obj_type,
1338 struct got_repository *repo)
1340 const struct got_error *err = NULL;
1341 char *path;
1342 DIR *dir = NULL;
1343 struct dirent *dent;
1344 struct got_object_id id;
1346 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1347 err = got_error_from_errno("asprintf");
1348 goto done;
1351 dir = opendir(path);
1352 if (dir == NULL) {
1353 if (errno == ENOENT) {
1354 err = NULL;
1355 goto done;
1357 err = got_error_from_errno2("opendir", path);
1358 goto done;
1360 while ((dent = readdir(dir)) != NULL) {
1361 char *id_str;
1362 int cmp;
1364 if (strcmp(dent->d_name, ".") == 0 ||
1365 strcmp(dent->d_name, "..") == 0)
1366 continue;
1368 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1369 err = got_error_from_errno("asprintf");
1370 goto done;
1373 if (!got_parse_sha1_digest(id.sha1, id_str))
1374 continue;
1377 * Directory entries do not necessarily appear in
1378 * sorted order, so we must iterate over all of them.
1380 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1381 if (cmp != 0) {
1382 free(id_str);
1383 continue;
1386 if (*unique_id == NULL) {
1387 if (obj_type != GOT_OBJ_TYPE_ANY) {
1388 int matched_type;
1389 err = got_object_get_type(&matched_type, repo,
1390 &id);
1391 if (err)
1392 goto done;
1393 if (matched_type != obj_type)
1394 continue;
1396 *unique_id = got_object_id_dup(&id);
1397 if (*unique_id == NULL) {
1398 err = got_error_from_errno("got_object_id_dup");
1399 free(id_str);
1400 goto done;
1402 } else {
1403 if (got_object_id_cmp(*unique_id, &id) == 0)
1404 continue; /* both packed and loose */
1405 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1406 free(id_str);
1407 goto done;
1410 done:
1411 if (dir && closedir(dir) != 0 && err == NULL)
1412 err = got_error_from_errno("closedir");
1413 if (err) {
1414 free(*unique_id);
1415 *unique_id = NULL;
1417 free(path);
1418 return err;
1421 const struct got_error *
1422 got_repo_match_object_id_prefix(struct got_object_id **id,
1423 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1425 const struct got_error *err = NULL;
1426 char *path_objects = got_repo_get_path_objects(repo);
1427 char *object_dir = NULL;
1428 size_t len;
1429 int i;
1431 *id = NULL;
1433 for (i = 0; i < strlen(id_str_prefix); i++) {
1434 if (isxdigit((unsigned char)id_str_prefix[i]))
1435 continue;
1436 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1439 len = strlen(id_str_prefix);
1440 if (len >= 2) {
1441 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1442 if (err)
1443 goto done;
1444 object_dir = strndup(id_str_prefix, 2);
1445 if (object_dir == NULL) {
1446 err = got_error_from_errno("strdup");
1447 goto done;
1449 err = match_loose_object(id, path_objects, object_dir,
1450 id_str_prefix, obj_type, repo);
1451 } else if (len == 1) {
1452 int i;
1453 for (i = 0; i < 0xf; i++) {
1454 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1455 == -1) {
1456 err = got_error_from_errno("asprintf");
1457 goto done;
1459 err = match_packed_object(id, repo, object_dir,
1460 obj_type);
1461 if (err)
1462 goto done;
1463 err = match_loose_object(id, path_objects, object_dir,
1464 id_str_prefix, obj_type, repo);
1465 if (err)
1466 goto done;
1468 } else {
1469 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1470 goto done;
1472 done:
1473 free(object_dir);
1474 if (err) {
1475 free(*id);
1476 *id = NULL;
1477 } else if (*id == NULL)
1478 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1480 return err;
1483 const struct got_error *
1484 got_repo_match_object_id(struct got_object_id **id, char **label,
1485 const char *id_str, int obj_type, int resolve_tags,
1486 struct got_repository *repo)
1488 const struct got_error *err;
1489 struct got_tag_object *tag;
1490 struct got_reference *ref = NULL;
1492 *id = NULL;
1493 if (label)
1494 *label = NULL;
1496 if (resolve_tags) {
1497 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
1498 repo);
1499 if (err == NULL) {
1500 *id = got_object_id_dup(
1501 got_object_tag_get_object_id(tag));
1502 if (*id == NULL)
1503 err = got_error_from_errno("got_object_id_dup");
1504 else if (label && asprintf(label, "refs/tags/%s",
1505 got_object_tag_get_name(tag)) == -1) {
1506 err = got_error_from_errno("asprintf");
1507 free(*id);
1508 *id = NULL;
1510 got_object_tag_close(tag);
1511 return err;
1512 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1513 err->code != GOT_ERR_NO_OBJ)
1514 return err;
1517 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1518 if (err) {
1519 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1520 return err;
1521 err = got_ref_open(&ref, repo, id_str, 0);
1522 if (err != NULL)
1523 goto done;
1524 if (label) {
1525 *label = strdup(got_ref_get_name(ref));
1526 if (*label == NULL) {
1527 err = got_error_from_errno("strdup");
1528 goto done;
1531 err = got_ref_resolve(id, repo, ref);
1532 } else if (label) {
1533 err = got_object_id_str(label, *id);
1534 if (*label == NULL) {
1535 err = got_error_from_errno("strdup");
1536 goto done;
1539 done:
1540 if (ref)
1541 got_ref_close(ref);
1542 return err;
1545 const struct got_error *
1546 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1547 int obj_type, struct got_repository *repo)
1549 const struct got_error *err;
1550 struct got_reflist_head refs;
1551 struct got_reflist_entry *re;
1552 struct got_object_id *tag_id;
1554 SIMPLEQ_INIT(&refs);
1555 *tag = NULL;
1557 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_by_name, NULL);
1558 if (err)
1559 return err;
1561 SIMPLEQ_FOREACH(re, &refs, entry) {
1562 const char *refname;
1563 refname = got_ref_get_name(re->ref);
1564 if (got_ref_is_symbolic(re->ref))
1565 continue;
1566 refname += strlen("refs/tags/");
1567 if (strcmp(refname, name) != 0)
1568 continue;
1569 err = got_ref_resolve(&tag_id, repo, re->ref);
1570 if (err)
1571 break;
1572 err = got_object_open_as_tag(tag, repo, tag_id);
1573 free(tag_id);
1574 if (err)
1575 break;
1576 if (obj_type == GOT_OBJ_TYPE_ANY ||
1577 got_object_tag_get_object_type(*tag) == obj_type)
1578 break;
1579 got_object_tag_close(*tag);
1580 *tag = NULL;
1583 got_ref_list_free(&refs);
1584 if (err == NULL && *tag == NULL)
1585 err = got_error_path(name, GOT_ERR_NO_OBJ);
1586 return err;
1589 static const struct got_error *
1590 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1591 const char *name, mode_t mode, struct got_object_id *blob_id)
1593 const struct got_error *err = NULL;
1595 *new_te = NULL;
1597 *new_te = calloc(1, sizeof(**new_te));
1598 if (*new_te == NULL)
1599 return got_error_from_errno("calloc");
1601 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1602 sizeof((*new_te)->name)) {
1603 err = got_error(GOT_ERR_NO_SPACE);
1604 goto done;
1607 if (S_ISLNK(mode)) {
1608 (*new_te)->mode = S_IFLNK;
1609 } else {
1610 (*new_te)->mode = S_IFREG;
1611 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1613 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1614 done:
1615 if (err && *new_te) {
1616 free(*new_te);
1617 *new_te = NULL;
1619 return err;
1622 static const struct got_error *
1623 import_file(struct got_tree_entry **new_te, struct dirent *de,
1624 const char *path, struct got_repository *repo)
1626 const struct got_error *err;
1627 struct got_object_id *blob_id = NULL;
1628 char *filepath;
1629 struct stat sb;
1631 if (asprintf(&filepath, "%s%s%s", path,
1632 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1633 return got_error_from_errno("asprintf");
1635 if (lstat(filepath, &sb) != 0) {
1636 err = got_error_from_errno2("lstat", path);
1637 goto done;
1640 err = got_object_blob_create(&blob_id, filepath, repo);
1641 if (err)
1642 goto done;
1644 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1645 blob_id);
1646 done:
1647 free(filepath);
1648 if (err)
1649 free(blob_id);
1650 return err;
1653 static const struct got_error *
1654 insert_tree_entry(struct got_tree_entry *new_te,
1655 struct got_pathlist_head *paths)
1657 const struct got_error *err = NULL;
1658 struct got_pathlist_entry *new_pe;
1660 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1661 if (err)
1662 return err;
1663 if (new_pe == NULL)
1664 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1665 return NULL;
1668 static const struct got_error *write_tree(struct got_object_id **,
1669 const char *, struct got_pathlist_head *, struct got_repository *,
1670 got_repo_import_cb progress_cb, void *progress_arg);
1672 static const struct got_error *
1673 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1674 const char *path, struct got_pathlist_head *ignores,
1675 struct got_repository *repo,
1676 got_repo_import_cb progress_cb, void *progress_arg)
1678 const struct got_error *err;
1679 struct got_object_id *id = NULL;
1680 char *subdirpath;
1682 if (asprintf(&subdirpath, "%s%s%s", path,
1683 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1684 return got_error_from_errno("asprintf");
1686 (*new_te) = calloc(1, sizeof(**new_te));
1687 if (*new_te == NULL)
1688 return got_error_from_errno("calloc");
1689 (*new_te)->mode = S_IFDIR;
1690 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1691 sizeof((*new_te)->name)) {
1692 err = got_error(GOT_ERR_NO_SPACE);
1693 goto done;
1695 err = write_tree(&id, subdirpath, ignores, repo,
1696 progress_cb, progress_arg);
1697 if (err)
1698 goto done;
1699 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1701 done:
1702 free(id);
1703 free(subdirpath);
1704 if (err) {
1705 free(*new_te);
1706 *new_te = NULL;
1708 return err;
1711 static const struct got_error *
1712 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1713 struct got_pathlist_head *ignores, struct got_repository *repo,
1714 got_repo_import_cb progress_cb, void *progress_arg)
1716 const struct got_error *err = NULL;
1717 DIR *dir;
1718 struct dirent *de;
1719 int nentries;
1720 struct got_tree_entry *new_te = NULL;
1721 struct got_pathlist_head paths;
1722 struct got_pathlist_entry *pe;
1724 *new_tree_id = NULL;
1726 TAILQ_INIT(&paths);
1728 dir = opendir(path_dir);
1729 if (dir == NULL) {
1730 err = got_error_from_errno2("opendir", path_dir);
1731 goto done;
1734 nentries = 0;
1735 while ((de = readdir(dir)) != NULL) {
1736 int ignore = 0;
1737 int type;
1739 if (strcmp(de->d_name, ".") == 0 ||
1740 strcmp(de->d_name, "..") == 0)
1741 continue;
1743 TAILQ_FOREACH(pe, ignores, entry) {
1744 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1745 ignore = 1;
1746 break;
1749 if (ignore)
1750 continue;
1752 err = got_path_dirent_type(&type, path_dir, de);
1753 if (err)
1754 goto done;
1756 if (type == DT_DIR) {
1757 err = import_subdir(&new_te, de, path_dir,
1758 ignores, repo, progress_cb, progress_arg);
1759 if (err) {
1760 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1761 goto done;
1762 err = NULL;
1763 continue;
1765 } else if (type == DT_REG || type == DT_LNK) {
1766 err = import_file(&new_te, de, path_dir, repo);
1767 if (err)
1768 goto done;
1769 } else
1770 continue;
1772 err = insert_tree_entry(new_te, &paths);
1773 if (err)
1774 goto done;
1775 nentries++;
1778 if (TAILQ_EMPTY(&paths)) {
1779 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
1780 "cannot create tree without any entries");
1781 goto done;
1784 TAILQ_FOREACH(pe, &paths, entry) {
1785 struct got_tree_entry *te = pe->data;
1786 char *path;
1787 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
1788 continue;
1789 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1790 err = got_error_from_errno("asprintf");
1791 goto done;
1793 err = (*progress_cb)(progress_arg, path);
1794 free(path);
1795 if (err)
1796 goto done;
1799 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1800 done:
1801 if (dir)
1802 closedir(dir);
1803 got_pathlist_free(&paths);
1804 return err;
1807 const struct got_error *
1808 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1809 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1810 struct got_repository *repo, got_repo_import_cb progress_cb,
1811 void *progress_arg)
1813 const struct got_error *err;
1814 struct got_object_id *new_tree_id;
1816 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1817 progress_cb, progress_arg);
1818 if (err)
1819 return err;
1821 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1822 author, time(NULL), author, time(NULL), logmsg, repo);
1823 free(new_tree_id);
1824 return err;