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>
24 #include <ctype.h>
25 #include <endian.h>
26 #include <fcntl.h>
27 #include <fnmatch.h>
28 #include <limits.h>
29 #include <dirent.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <sha1.h>
33 #include <string.h>
34 #include <time.h>
35 #include <unistd.h>
36 #include <zlib.h>
37 #include <errno.h>
38 #include <libgen.h>
39 #include <stdint.h>
40 #include <imsg.h>
41 #include <uuid.h>
43 #include "got_error.h"
44 #include "got_reference.h"
45 #include "got_repository.h"
46 #include "got_path.h"
47 #include "got_cancel.h"
48 #include "got_worktree.h"
49 #include "got_object.h"
51 #include "got_lib_delta.h"
52 #include "got_lib_inflate.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_pack.h"
57 #include "got_lib_privsep.h"
58 #include "got_lib_worktree.h"
59 #include "got_lib_sha1.h"
60 #include "got_lib_object_cache.h"
61 #include "got_lib_repository.h"
62 #include "got_lib_gotconfig.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_FILENAME);
170 const struct got_gotconfig *
171 got_repo_get_gotconfig(struct got_repository *repo)
173 return repo->gotconfig;
176 void
177 got_repo_get_gitconfig_remotes(int *nremotes,
178 const struct got_remote_repo **remotes, struct got_repository *repo)
180 *nremotes = repo->ngitconfig_remotes;
181 *remotes = repo->gitconfig_remotes;
184 static int
185 is_git_repo(struct got_repository *repo)
187 const char *path_git = got_repo_get_path_git_dir(repo);
188 char *path_objects = got_repo_get_path_objects(repo);
189 char *path_refs = got_repo_get_path_refs(repo);
190 char *path_head = get_path_head(repo);
191 int ret = 0;
192 struct stat sb;
193 struct got_reference *head_ref;
195 if (lstat(path_git, &sb) == -1)
196 goto done;
197 if (!S_ISDIR(sb.st_mode))
198 goto done;
200 if (lstat(path_objects, &sb) == -1)
201 goto done;
202 if (!S_ISDIR(sb.st_mode))
203 goto done;
205 if (lstat(path_refs, &sb) == -1)
206 goto done;
207 if (!S_ISDIR(sb.st_mode))
208 goto done;
210 if (lstat(path_head, &sb) == -1)
211 goto done;
212 if (!S_ISREG(sb.st_mode))
213 goto done;
215 /* Check if the HEAD reference can be opened. */
216 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
217 goto done;
218 got_ref_close(head_ref);
220 ret = 1;
221 done:
222 free(path_objects);
223 free(path_refs);
224 free(path_head);
225 return ret;
229 const struct got_error *
230 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
231 struct got_object *obj)
233 #ifndef GOT_NO_OBJ_CACHE
234 const struct got_error *err = NULL;
235 err = got_object_cache_add(&repo->objcache, id, obj);
236 if (err) {
237 if (err->code == GOT_ERR_OBJ_EXISTS ||
238 err->code == GOT_ERR_OBJ_TOO_LARGE)
239 err = NULL;
240 return err;
242 obj->refcnt++;
243 #endif
244 return NULL;
247 struct got_object *
248 got_repo_get_cached_object(struct got_repository *repo,
249 struct got_object_id *id)
251 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
254 const struct got_error *
255 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
256 struct got_tree_object *tree)
258 #ifndef GOT_NO_OBJ_CACHE
259 const struct got_error *err = NULL;
260 err = got_object_cache_add(&repo->treecache, id, tree);
261 if (err) {
262 if (err->code == GOT_ERR_OBJ_EXISTS ||
263 err->code == GOT_ERR_OBJ_TOO_LARGE)
264 err = NULL;
265 return err;
267 tree->refcnt++;
268 #endif
269 return NULL;
272 struct got_tree_object *
273 got_repo_get_cached_tree(struct got_repository *repo,
274 struct got_object_id *id)
276 return (struct got_tree_object *)got_object_cache_get(
277 &repo->treecache, id);
280 const struct got_error *
281 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
282 struct got_commit_object *commit)
284 #ifndef GOT_NO_OBJ_CACHE
285 const struct got_error *err = NULL;
286 err = got_object_cache_add(&repo->commitcache, id, commit);
287 if (err) {
288 if (err->code == GOT_ERR_OBJ_EXISTS ||
289 err->code == GOT_ERR_OBJ_TOO_LARGE)
290 err = NULL;
291 return err;
293 commit->refcnt++;
294 #endif
295 return NULL;
298 struct got_commit_object *
299 got_repo_get_cached_commit(struct got_repository *repo,
300 struct got_object_id *id)
302 return (struct got_commit_object *)got_object_cache_get(
303 &repo->commitcache, id);
306 const struct got_error *
307 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
308 struct got_tag_object *tag)
310 #ifndef GOT_NO_OBJ_CACHE
311 const struct got_error *err = NULL;
312 err = got_object_cache_add(&repo->tagcache, id, tag);
313 if (err) {
314 if (err->code == GOT_ERR_OBJ_EXISTS ||
315 err->code == GOT_ERR_OBJ_TOO_LARGE)
316 err = NULL;
317 return err;
319 tag->refcnt++;
320 #endif
321 return NULL;
324 struct got_tag_object *
325 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
327 return (struct got_tag_object *)got_object_cache_get(
328 &repo->tagcache, id);
331 const struct got_error *
332 open_repo(struct got_repository *repo, const char *path)
334 const struct got_error *err = NULL;
336 /* bare git repository? */
337 repo->path_git_dir = strdup(path);
338 if (repo->path_git_dir == NULL)
339 return got_error_from_errno("strdup");
340 if (is_git_repo(repo)) {
341 repo->path = strdup(repo->path_git_dir);
342 if (repo->path == NULL) {
343 err = got_error_from_errno("strdup");
344 goto done;
346 return NULL;
349 /* git repository with working tree? */
350 free(repo->path_git_dir);
351 repo->path_git_dir = NULL;
352 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
353 err = got_error_from_errno("asprintf");
354 goto done;
356 if (is_git_repo(repo)) {
357 repo->path = strdup(path);
358 if (repo->path == NULL) {
359 err = got_error_from_errno("strdup");
360 goto done;
362 return NULL;
365 err = got_error(GOT_ERR_NOT_GIT_REPO);
366 done:
367 if (err) {
368 free(repo->path);
369 repo->path = NULL;
370 free(repo->path_git_dir);
371 repo->path_git_dir = NULL;
373 return err;
376 static const struct got_error *
377 parse_gitconfig_file(int *gitconfig_repository_format_version,
378 char **gitconfig_author_name, char **gitconfig_author_email,
379 struct got_remote_repo **remotes, int *nremotes,
380 char **gitconfig_owner, char ***extensions, int *nextensions,
381 const char *gitconfig_path)
383 const struct got_error *err = NULL, *child_err = NULL;
384 int fd = -1;
385 int imsg_fds[2] = { -1, -1 };
386 pid_t pid;
387 struct imsgbuf *ibuf;
389 *gitconfig_repository_format_version = 0;
390 if (extensions)
391 *extensions = NULL;
392 if (nextensions)
393 *nextensions = 0;
394 *gitconfig_author_name = NULL;
395 *gitconfig_author_email = NULL;
396 if (remotes)
397 *remotes = NULL;
398 if (nremotes)
399 *nremotes = 0;
400 if (gitconfig_owner)
401 *gitconfig_owner = NULL;
403 fd = open(gitconfig_path, O_RDONLY);
404 if (fd == -1) {
405 if (errno == ENOENT)
406 return NULL;
407 return got_error_from_errno2("open", gitconfig_path);
410 ibuf = calloc(1, sizeof(*ibuf));
411 if (ibuf == NULL) {
412 err = got_error_from_errno("calloc");
413 goto done;
416 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
417 err = got_error_from_errno("socketpair");
418 goto done;
421 pid = fork();
422 if (pid == -1) {
423 err = got_error_from_errno("fork");
424 goto done;
425 } else if (pid == 0) {
426 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
427 gitconfig_path);
428 /* not reached */
431 if (close(imsg_fds[1]) == -1) {
432 err = got_error_from_errno("close");
433 goto done;
435 imsg_fds[1] = -1;
436 imsg_init(ibuf, imsg_fds[0]);
438 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
439 if (err)
440 goto done;
441 fd = -1;
443 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
444 if (err)
445 goto done;
447 err = got_privsep_recv_gitconfig_int(
448 gitconfig_repository_format_version, ibuf);
449 if (err)
450 goto done;
452 if (extensions && nextensions) {
453 err = got_privsep_send_gitconfig_repository_extensions_req(
454 ibuf);
455 if (err)
456 goto done;
457 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
458 if (err)
459 goto done;
460 if (*nextensions > 0) {
461 int i;
462 *extensions = calloc(*nextensions, sizeof(char *));
463 if (*extensions == NULL) {
464 err = got_error_from_errno("calloc");
465 goto done;
467 for (i = 0; i < *nextensions; i++) {
468 char *ext;
469 err = got_privsep_recv_gitconfig_str(&ext,
470 ibuf);
471 if (err)
472 goto done;
473 (*extensions)[i] = ext;
478 err = got_privsep_send_gitconfig_author_name_req(ibuf);
479 if (err)
480 goto done;
482 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
483 if (err)
484 goto done;
486 err = got_privsep_send_gitconfig_author_email_req(ibuf);
487 if (err)
488 goto done;
490 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
491 if (err)
492 goto done;
494 if (remotes && nremotes) {
495 err = got_privsep_send_gitconfig_remotes_req(ibuf);
496 if (err)
497 goto done;
499 err = got_privsep_recv_gitconfig_remotes(remotes,
500 nremotes, ibuf);
501 if (err)
502 goto done;
505 if (gitconfig_owner) {
506 err = got_privsep_send_gitconfig_owner_req(ibuf);
507 if (err)
508 goto done;
509 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
510 if (err)
511 goto done;
514 imsg_clear(ibuf);
515 err = got_privsep_send_stop(imsg_fds[0]);
516 child_err = got_privsep_wait_for_child(pid);
517 if (child_err && err == NULL)
518 err = child_err;
519 done:
520 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
521 err = got_error_from_errno("close");
522 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
523 err = got_error_from_errno("close");
524 if (fd != -1 && close(fd) == -1 && err == NULL)
525 err = got_error_from_errno2("close", gitconfig_path);
526 free(ibuf);
527 return err;
530 static const struct got_error *
531 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
533 const struct got_error *err = NULL;
534 char *repo_gitconfig_path = NULL;
536 if (global_gitconfig_path) {
537 /* Read settings from ~/.gitconfig. */
538 int dummy_repo_version;
539 err = parse_gitconfig_file(&dummy_repo_version,
540 &repo->global_gitconfig_author_name,
541 &repo->global_gitconfig_author_email,
542 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
543 if (err)
544 return err;
547 /* Read repository's .git/config file. */
548 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
549 if (repo_gitconfig_path == NULL)
550 return got_error_from_errno("got_repo_get_path_gitconfig");
552 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
553 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
554 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
555 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
556 repo_gitconfig_path);
557 if (err)
558 goto done;
559 done:
560 free(repo_gitconfig_path);
561 return err;
564 static const struct got_error *
565 read_gotconfig(struct got_repository *repo)
567 const struct got_error *err = NULL;
568 char *gotconfig_path;
570 gotconfig_path = got_repo_get_path_gotconfig(repo);
571 if (gotconfig_path == NULL)
572 return got_error_from_errno("got_repo_get_path_gotconfig");
574 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
575 free(gotconfig_path);
576 return err;
579 /* Supported repository format extensions. */
580 static const char *repo_extensions[] = {
581 "noop", /* Got supports repository format version 1. */
582 "preciousObjects", /* Got has no garbage collection yet. */
583 "worktreeConfig", /* Got does not care about Git work trees. */
584 };
586 const struct got_error *
587 got_repo_open(struct got_repository **repop, const char *path,
588 const char *global_gitconfig_path)
590 struct got_repository *repo = NULL;
591 const struct got_error *err = NULL;
592 char *abspath, *repo_path = NULL;
593 size_t i;
595 *repop = NULL;
597 if (got_path_is_absolute(path))
598 abspath = strdup(path);
599 else
600 abspath = got_path_get_absolute(path);
601 if (abspath == NULL)
602 return got_error_path(path, GOT_ERR_BAD_PATH);
604 repo = calloc(1, sizeof(*repo));
605 if (repo == NULL) {
606 err = got_error_from_errno("calloc");
607 goto done;
610 for (i = 0; i < nitems(repo->privsep_children); i++) {
611 memset(&repo->privsep_children[i], 0,
612 sizeof(repo->privsep_children[0]));
613 repo->privsep_children[i].imsg_fd = -1;
616 err = got_object_cache_init(&repo->objcache,
617 GOT_OBJECT_CACHE_TYPE_OBJ);
618 if (err)
619 goto done;
620 err = got_object_cache_init(&repo->treecache,
621 GOT_OBJECT_CACHE_TYPE_TREE);
622 if (err)
623 goto done;
624 err = got_object_cache_init(&repo->commitcache,
625 GOT_OBJECT_CACHE_TYPE_COMMIT);
626 if (err)
627 goto done;
628 err = got_object_cache_init(&repo->tagcache,
629 GOT_OBJECT_CACHE_TYPE_TAG);
630 if (err)
631 goto done;
633 repo_path = realpath(abspath, NULL);
634 if (repo_path == NULL) {
635 err = got_error_from_errno2("realpath", abspath);
636 goto done;
639 for (;;) {
640 char *parent_path;
642 err = open_repo(repo, repo_path);
643 if (err == NULL)
644 break;
645 if (err->code != GOT_ERR_NOT_GIT_REPO)
646 goto done;
647 if (repo_path[0] == '/' && repo_path[1] == '\0') {
648 err = got_error(GOT_ERR_NOT_GIT_REPO);
649 goto done;
651 err = got_path_dirname(&parent_path, repo_path);
652 if (err)
653 goto done;
654 free(repo_path);
655 repo_path = parent_path;
658 err = read_gotconfig(repo);
659 if (err)
660 goto done;
662 err = read_gitconfig(repo, global_gitconfig_path);
663 if (err)
664 goto done;
665 if (repo->gitconfig_repository_format_version != 0)
666 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
667 for (i = 0; i < repo->nextensions; i++) {
668 char *ext = repo->extensions[i];
669 int j, supported = 0;
670 for (j = 0; j < nitems(repo_extensions); j++) {
671 if (strcmp(ext, repo_extensions[j]) == 0) {
672 supported = 1;
673 break;
676 if (!supported) {
677 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
678 goto done;
681 done:
682 if (err)
683 got_repo_close(repo);
684 else
685 *repop = repo;
686 free(abspath);
687 free(repo_path);
688 return err;
691 const struct got_error *
692 got_repo_close(struct got_repository *repo)
694 const struct got_error *err = NULL, *child_err;
695 size_t i;
697 for (i = 0; i < nitems(repo->packidx_cache); i++) {
698 if (repo->packidx_cache[i] == NULL)
699 break;
700 got_packidx_close(repo->packidx_cache[i]);
703 for (i = 0; i < nitems(repo->packs); i++) {
704 if (repo->packs[i].path_packfile == NULL)
705 break;
706 got_pack_close(&repo->packs[i]);
709 free(repo->path);
710 free(repo->path_git_dir);
712 got_object_cache_close(&repo->objcache);
713 got_object_cache_close(&repo->treecache);
714 got_object_cache_close(&repo->commitcache);
715 got_object_cache_close(&repo->tagcache);
717 for (i = 0; i < nitems(repo->privsep_children); i++) {
718 if (repo->privsep_children[i].imsg_fd == -1)
719 continue;
720 imsg_clear(repo->privsep_children[i].ibuf);
721 free(repo->privsep_children[i].ibuf);
722 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
723 child_err = got_privsep_wait_for_child(
724 repo->privsep_children[i].pid);
725 if (child_err && err == NULL)
726 err = child_err;
727 if (close(repo->privsep_children[i].imsg_fd) != 0 &&
728 err == NULL)
729 err = got_error_from_errno("close");
732 if (repo->gotconfig)
733 got_gotconfig_free(repo->gotconfig);
734 free(repo->gitconfig_author_name);
735 free(repo->gitconfig_author_email);
736 for (i = 0; i < repo->ngitconfig_remotes; i++)
737 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
738 free(repo->gitconfig_remotes);
739 for (i = 0; i < repo->nextensions; i++)
740 free(repo->extensions[i]);
741 free(repo->extensions);
742 free(repo);
744 return err;
747 void
748 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
750 int i;
752 free(repo->name);
753 repo->name = NULL;
754 free(repo->url);
755 repo->url = NULL;
756 for (i = 0; i < repo->nbranches; i++)
757 free(repo->branches[i]);
758 free(repo->branches);
759 repo->branches = NULL;
760 repo->nbranches = 0;
763 const struct got_error *
764 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
765 const char *input_path)
767 const struct got_error *err = NULL;
768 const char *repo_abspath = NULL;
769 size_t repolen, len;
770 char *canonpath, *path = NULL;
772 *in_repo_path = NULL;
774 canonpath = strdup(input_path);
775 if (canonpath == NULL) {
776 err = got_error_from_errno("strdup");
777 goto done;
779 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
780 if (err)
781 goto done;
783 repo_abspath = got_repo_get_path(repo);
785 if (canonpath[0] == '\0') {
786 path = strdup(canonpath);
787 if (path == NULL) {
788 err = got_error_from_errno("strdup");
789 goto done;
791 } else {
792 path = realpath(canonpath, NULL);
793 if (path == NULL) {
794 if (errno != ENOENT) {
795 err = got_error_from_errno2("realpath",
796 canonpath);
797 goto done;
799 /*
800 * Path is not on disk.
801 * Assume it is already relative to repository root.
802 */
803 path = strdup(canonpath);
804 if (path == NULL) {
805 err = got_error_from_errno("strdup");
806 goto done;
810 repolen = strlen(repo_abspath);
811 len = strlen(path);
814 if (strcmp(path, repo_abspath) == 0) {
815 free(path);
816 path = strdup("");
817 if (path == NULL) {
818 err = got_error_from_errno("strdup");
819 goto done;
821 } else if (len > repolen &&
822 got_path_is_child(path, repo_abspath, repolen)) {
823 /* Matched an on-disk path inside repository. */
824 if (got_repo_is_bare(repo)) {
825 /*
826 * Matched an on-disk path inside repository
827 * database. Treat input as repository-relative.
828 */
829 free(path);
830 path = canonpath;
831 canonpath = NULL;
832 } else {
833 char *child;
834 /* Strip common prefix with repository path. */
835 err = got_path_skip_common_ancestor(&child,
836 repo_abspath, path);
837 if (err)
838 goto done;
839 free(path);
840 path = child;
842 } else {
843 /*
844 * Matched unrelated on-disk path.
845 * Treat input as repository-relative.
846 */
847 free(path);
848 path = canonpath;
849 canonpath = NULL;
853 /* Make in-repository path absolute */
854 if (path[0] != '/') {
855 char *abspath;
856 if (asprintf(&abspath, "/%s", path) == -1) {
857 err = got_error_from_errno("asprintf");
858 goto done;
860 free(path);
861 path = abspath;
864 done:
865 free(canonpath);
866 if (err)
867 free(path);
868 else
869 *in_repo_path = path;
870 return err;
873 static const struct got_error *
874 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
875 const char *path_packidx)
877 const struct got_error *err = NULL;
878 size_t i;
880 for (i = 0; i < nitems(repo->packidx_cache); i++) {
881 if (repo->packidx_cache[i] == NULL)
882 break;
883 if (strcmp(repo->packidx_cache[i]->path_packidx,
884 path_packidx) == 0) {
885 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
888 if (i == nitems(repo->packidx_cache)) {
889 err = got_packidx_close(repo->packidx_cache[i - 1]);
890 if (err)
891 return err;
894 /*
895 * Insert the new pack index at the front so it will
896 * be searched first in the future.
897 */
898 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
899 sizeof(repo->packidx_cache) -
900 sizeof(repo->packidx_cache[0]));
901 repo->packidx_cache[0] = packidx;
903 return NULL;
906 static int
907 is_packidx_filename(const char *name, size_t len)
909 if (len != GOT_PACKIDX_NAMELEN)
910 return 0;
912 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
913 return 0;
915 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
916 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
917 return 0;
919 return 1;
922 const struct got_error *
923 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
924 struct got_repository *repo, struct got_object_id *id)
926 const struct got_error *err;
927 char *path_packdir;
928 DIR *packdir;
929 struct dirent *dent;
930 char *path_packidx;
931 size_t i;
933 /* Search pack index cache. */
934 for (i = 0; i < nitems(repo->packidx_cache); i++) {
935 if (repo->packidx_cache[i] == NULL)
936 break;
937 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
938 if (*idx != -1) {
939 *packidx = repo->packidx_cache[i];
940 /*
941 * Move this cache entry to the front. Repeatedly
942 * searching a wrong pack index can be expensive.
943 */
944 if (i > 0) {
945 struct got_packidx *p;
946 p = repo->packidx_cache[0];
947 repo->packidx_cache[0] = *packidx;
948 repo->packidx_cache[i] = p;
950 return NULL;
953 /* No luck. Search the filesystem. */
955 path_packdir = got_repo_get_path_objects_pack(repo);
956 if (path_packdir == NULL)
957 return got_error_from_errno("got_repo_get_path_objects_pack");
959 packdir = opendir(path_packdir);
960 if (packdir == NULL) {
961 if (errno == ENOENT)
962 err = got_error_no_obj(id);
963 else
964 err = got_error_from_errno2("opendir", path_packdir);
965 goto done;
968 while ((dent = readdir(packdir)) != NULL) {
969 int is_cached = 0;
971 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
972 continue;
974 if (asprintf(&path_packidx, "%s/%s", path_packdir,
975 dent->d_name) == -1) {
976 err = got_error_from_errno("asprintf");
977 goto done;
980 for (i = 0; i < nitems(repo->packidx_cache); i++) {
981 if (repo->packidx_cache[i] == NULL)
982 break;
983 if (strcmp(repo->packidx_cache[i]->path_packidx,
984 path_packidx) == 0) {
985 is_cached = 1;
986 break;
989 if (is_cached) {
990 free(path_packidx);
991 continue; /* already searched */
994 err = got_packidx_open(packidx, path_packidx, 0);
995 if (err) {
996 free(path_packidx);
997 goto done;
1000 err = cache_packidx(repo, *packidx, path_packidx);
1001 free(path_packidx);
1002 if (err)
1003 goto done;
1005 *idx = got_packidx_get_object_idx(*packidx, id);
1006 if (*idx != -1) {
1007 err = NULL; /* found the object */
1008 goto done;
1012 err = got_error_no_obj(id);
1013 done:
1014 free(path_packdir);
1015 if (packdir && closedir(packdir) != 0 && err == NULL)
1016 err = got_error_from_errno("closedir");
1017 return err;
1020 static const struct got_error *
1021 read_packfile_hdr(int fd, struct got_packidx *packidx)
1023 const struct got_error *err = NULL;
1024 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1025 struct got_packfile_hdr hdr;
1026 ssize_t n;
1028 n = read(fd, &hdr, sizeof(hdr));
1029 if (n < 0)
1030 return got_error_from_errno("read");
1031 if (n != sizeof(hdr))
1032 return got_error(GOT_ERR_BAD_PACKFILE);
1034 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1035 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1036 be32toh(hdr.nobjects) != totobj)
1037 err = got_error(GOT_ERR_BAD_PACKFILE);
1039 return err;
1042 static const struct got_error *
1043 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
1045 const struct got_error *err = NULL;
1047 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
1048 if (*fd == -1)
1049 return got_error_from_errno2("open", path_packfile);
1051 if (packidx) {
1052 err = read_packfile_hdr(*fd, packidx);
1053 if (err) {
1054 close(*fd);
1055 *fd = -1;
1059 return err;
1062 const struct got_error *
1063 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1064 const char *path_packfile, struct got_packidx *packidx)
1066 const struct got_error *err = NULL;
1067 struct got_pack *pack = NULL;
1068 struct stat sb;
1069 size_t i;
1071 if (packp)
1072 *packp = NULL;
1074 for (i = 0; i < nitems(repo->packs); i++) {
1075 pack = &repo->packs[i];
1076 if (pack->path_packfile == NULL)
1077 break;
1078 if (strcmp(pack->path_packfile, path_packfile) == 0)
1079 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1082 if (i == nitems(repo->packs) - 1) {
1083 err = got_pack_close(&repo->packs[i - 1]);
1084 if (err)
1085 return err;
1086 memmove(&repo->packs[1], &repo->packs[0],
1087 sizeof(repo->packs) - sizeof(repo->packs[0]));
1088 i = 0;
1091 pack = &repo->packs[i];
1093 pack->path_packfile = strdup(path_packfile);
1094 if (pack->path_packfile == NULL) {
1095 err = got_error_from_errno("strdup");
1096 goto done;
1099 err = open_packfile(&pack->fd, path_packfile, packidx);
1100 if (err)
1101 goto done;
1103 if (fstat(pack->fd, &sb) != 0) {
1104 err = got_error_from_errno("fstat");
1105 goto done;
1107 pack->filesize = sb.st_size;
1109 pack->privsep_child = NULL;
1111 #ifndef GOT_PACK_NO_MMAP
1112 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1113 pack->fd, 0);
1114 if (pack->map == MAP_FAILED) {
1115 if (errno != ENOMEM) {
1116 err = got_error_from_errno("mmap");
1117 goto done;
1119 pack->map = NULL; /* fall back to read(2) */
1121 #endif
1122 done:
1123 if (err) {
1124 if (pack) {
1125 free(pack->path_packfile);
1126 memset(pack, 0, sizeof(*pack));
1128 } else if (packp)
1129 *packp = pack;
1130 return err;
1133 struct got_pack *
1134 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1136 struct got_pack *pack = NULL;
1137 size_t i;
1139 for (i = 0; i < nitems(repo->packs); i++) {
1140 pack = &repo->packs[i];
1141 if (pack->path_packfile == NULL)
1142 break;
1143 if (strcmp(pack->path_packfile, path_packfile) == 0)
1144 return pack;
1147 return NULL;
1150 const struct got_error *
1151 got_repo_init(const char *repo_path)
1153 const struct got_error *err = NULL;
1154 const char *dirnames[] = {
1155 GOT_OBJECTS_DIR,
1156 GOT_OBJECTS_PACK_DIR,
1157 GOT_REFS_DIR,
1159 const char *description_str = "Unnamed repository; "
1160 "edit this file 'description' to name the repository.";
1161 const char *headref_str = "ref: refs/heads/main";
1162 const char *gitconfig_str = "[core]\n"
1163 "\trepositoryformatversion = 0\n"
1164 "\tfilemode = true\n"
1165 "\tbare = true\n";
1166 char *path;
1167 size_t i;
1169 if (!got_path_dir_is_empty(repo_path))
1170 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1172 for (i = 0; i < nitems(dirnames); i++) {
1173 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1174 return got_error_from_errno("asprintf");
1176 err = got_path_mkdir(path);
1177 free(path);
1178 if (err)
1179 return err;
1182 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1183 return got_error_from_errno("asprintf");
1184 err = got_path_create_file(path, description_str);
1185 free(path);
1186 if (err)
1187 return err;
1189 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1190 return got_error_from_errno("asprintf");
1191 err = got_path_create_file(path, headref_str);
1192 free(path);
1193 if (err)
1194 return err;
1196 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1197 return got_error_from_errno("asprintf");
1198 err = got_path_create_file(path, gitconfig_str);
1199 free(path);
1200 if (err)
1201 return err;
1203 return NULL;
1206 static const struct got_error *
1207 match_packed_object(struct got_object_id **unique_id,
1208 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1210 const struct got_error *err = NULL;
1211 char *path_packdir;
1212 DIR *packdir;
1213 struct dirent *dent;
1214 char *path_packidx;
1215 struct got_object_id_queue matched_ids;
1217 SIMPLEQ_INIT(&matched_ids);
1219 path_packdir = got_repo_get_path_objects_pack(repo);
1220 if (path_packdir == NULL)
1221 return got_error_from_errno("got_repo_get_path_objects_pack");
1223 packdir = opendir(path_packdir);
1224 if (packdir == NULL) {
1225 if (errno != ENOENT)
1226 err = got_error_from_errno2("opendir", path_packdir);
1227 goto done;
1230 while ((dent = readdir(packdir)) != NULL) {
1231 struct got_packidx *packidx;
1232 struct got_object_qid *qid;
1235 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1236 continue;
1238 if (asprintf(&path_packidx, "%s/%s", path_packdir,
1239 dent->d_name) == -1) {
1240 err = got_error_from_errno("asprintf");
1241 break;
1244 err = got_packidx_open(&packidx, path_packidx, 0);
1245 free(path_packidx);
1246 if (err)
1247 break;
1249 err = got_packidx_match_id_str_prefix(&matched_ids,
1250 packidx, id_str_prefix);
1251 if (err) {
1252 got_packidx_close(packidx);
1253 break;
1255 err = got_packidx_close(packidx);
1256 if (err)
1257 break;
1259 SIMPLEQ_FOREACH(qid, &matched_ids, entry) {
1260 if (obj_type != GOT_OBJ_TYPE_ANY) {
1261 int matched_type;
1262 err = got_object_get_type(&matched_type, repo,
1263 qid->id);
1264 if (err)
1265 goto done;
1266 if (matched_type != obj_type)
1267 continue;
1269 if (*unique_id == NULL) {
1270 *unique_id = got_object_id_dup(qid->id);
1271 if (*unique_id == NULL) {
1272 err = got_error_from_errno("malloc");
1273 goto done;
1275 } else {
1276 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1277 continue; /* packed multiple times */
1278 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1279 goto done;
1283 done:
1284 got_object_id_queue_free(&matched_ids);
1285 free(path_packdir);
1286 if (packdir && closedir(packdir) != 0 && err == NULL)
1287 err = got_error_from_errno("closedir");
1288 if (err) {
1289 free(*unique_id);
1290 *unique_id = NULL;
1292 return err;
1295 static const struct got_error *
1296 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1297 const char *object_dir, const char *id_str_prefix, int obj_type,
1298 struct got_repository *repo)
1300 const struct got_error *err = NULL;
1301 char *path;
1302 DIR *dir = NULL;
1303 struct dirent *dent;
1304 struct got_object_id id;
1306 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1307 err = got_error_from_errno("asprintf");
1308 goto done;
1311 dir = opendir(path);
1312 if (dir == NULL) {
1313 if (errno == ENOENT) {
1314 err = NULL;
1315 goto done;
1317 err = got_error_from_errno2("opendir", path);
1318 goto done;
1320 while ((dent = readdir(dir)) != NULL) {
1321 char *id_str;
1322 int cmp;
1324 if (strcmp(dent->d_name, ".") == 0 ||
1325 strcmp(dent->d_name, "..") == 0)
1326 continue;
1328 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1329 err = got_error_from_errno("asprintf");
1330 goto done;
1333 if (!got_parse_sha1_digest(id.sha1, id_str))
1334 continue;
1337 * Directory entries do not necessarily appear in
1338 * sorted order, so we must iterate over all of them.
1340 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1341 if (cmp != 0) {
1342 free(id_str);
1343 continue;
1346 if (*unique_id == NULL) {
1347 if (obj_type != GOT_OBJ_TYPE_ANY) {
1348 int matched_type;
1349 err = got_object_get_type(&matched_type, repo,
1350 &id);
1351 if (err)
1352 goto done;
1353 if (matched_type != obj_type)
1354 continue;
1356 *unique_id = got_object_id_dup(&id);
1357 if (*unique_id == NULL) {
1358 err = got_error_from_errno("got_object_id_dup");
1359 free(id_str);
1360 goto done;
1362 } else {
1363 if (got_object_id_cmp(*unique_id, &id) == 0)
1364 continue; /* both packed and loose */
1365 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1366 free(id_str);
1367 goto done;
1370 done:
1371 if (dir && closedir(dir) != 0 && err == NULL)
1372 err = got_error_from_errno("closedir");
1373 if (err) {
1374 free(*unique_id);
1375 *unique_id = NULL;
1377 free(path);
1378 return err;
1381 const struct got_error *
1382 got_repo_match_object_id_prefix(struct got_object_id **id,
1383 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1385 const struct got_error *err = NULL;
1386 char *path_objects = got_repo_get_path_objects(repo);
1387 char *object_dir = NULL;
1388 size_t len;
1389 int i;
1391 *id = NULL;
1393 for (i = 0; i < strlen(id_str_prefix); i++) {
1394 if (isxdigit((unsigned char)id_str_prefix[i]))
1395 continue;
1396 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1399 len = strlen(id_str_prefix);
1400 if (len >= 2) {
1401 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1402 if (err)
1403 goto done;
1404 object_dir = strndup(id_str_prefix, 2);
1405 if (object_dir == NULL) {
1406 err = got_error_from_errno("strdup");
1407 goto done;
1409 err = match_loose_object(id, path_objects, object_dir,
1410 id_str_prefix, obj_type, repo);
1411 } else if (len == 1) {
1412 int i;
1413 for (i = 0; i < 0xf; i++) {
1414 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1415 == -1) {
1416 err = got_error_from_errno("asprintf");
1417 goto done;
1419 err = match_packed_object(id, repo, object_dir,
1420 obj_type);
1421 if (err)
1422 goto done;
1423 err = match_loose_object(id, path_objects, object_dir,
1424 id_str_prefix, obj_type, repo);
1425 if (err)
1426 goto done;
1428 } else {
1429 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1430 goto done;
1432 done:
1433 free(object_dir);
1434 if (err) {
1435 free(*id);
1436 *id = NULL;
1437 } else if (*id == NULL)
1438 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1440 return err;
1443 const struct got_error *
1444 got_repo_match_object_id(struct got_object_id **id, char **label,
1445 const char *id_str, int obj_type, int resolve_tags,
1446 struct got_repository *repo)
1448 const struct got_error *err;
1449 struct got_tag_object *tag;
1450 struct got_reference *ref = NULL;
1452 *id = NULL;
1453 if (label)
1454 *label = NULL;
1456 if (resolve_tags) {
1457 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
1458 repo);
1459 if (err == NULL) {
1460 *id = got_object_id_dup(
1461 got_object_tag_get_object_id(tag));
1462 if (*id == NULL)
1463 err = got_error_from_errno("got_object_id_dup");
1464 else if (label && asprintf(label, "refs/tags/%s",
1465 got_object_tag_get_name(tag)) == -1) {
1466 err = got_error_from_errno("asprintf");
1467 free(*id);
1468 *id = NULL;
1470 got_object_tag_close(tag);
1471 return err;
1472 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1473 err->code != GOT_ERR_NO_OBJ)
1474 return err;
1477 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1478 if (err) {
1479 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1480 return err;
1481 err = got_ref_open(&ref, repo, id_str, 0);
1482 if (err != NULL)
1483 goto done;
1484 if (label) {
1485 *label = strdup(got_ref_get_name(ref));
1486 if (*label == NULL) {
1487 err = got_error_from_errno("strdup");
1488 goto done;
1491 err = got_ref_resolve(id, repo, ref);
1492 } else if (label) {
1493 err = got_object_id_str(label, *id);
1494 if (*label == NULL) {
1495 err = got_error_from_errno("strdup");
1496 goto done;
1499 done:
1500 if (ref)
1501 got_ref_close(ref);
1502 return err;
1505 const struct got_error *
1506 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1507 int obj_type, struct got_repository *repo)
1509 const struct got_error *err;
1510 struct got_reflist_head refs;
1511 struct got_reflist_entry *re;
1512 struct got_object_id *tag_id;
1514 SIMPLEQ_INIT(&refs);
1515 *tag = NULL;
1517 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_by_name, NULL);
1518 if (err)
1519 return err;
1521 SIMPLEQ_FOREACH(re, &refs, entry) {
1522 const char *refname;
1523 refname = got_ref_get_name(re->ref);
1524 if (got_ref_is_symbolic(re->ref))
1525 continue;
1526 refname += strlen("refs/tags/");
1527 if (strcmp(refname, name) != 0)
1528 continue;
1529 err = got_ref_resolve(&tag_id, repo, re->ref);
1530 if (err)
1531 break;
1532 err = got_object_open_as_tag(tag, repo, tag_id);
1533 free(tag_id);
1534 if (err)
1535 break;
1536 if (obj_type == GOT_OBJ_TYPE_ANY ||
1537 got_object_tag_get_object_type(*tag) == obj_type)
1538 break;
1539 got_object_tag_close(*tag);
1540 *tag = NULL;
1543 got_ref_list_free(&refs);
1544 if (err == NULL && *tag == NULL)
1545 err = got_error_path(name, GOT_ERR_NO_OBJ);
1546 return err;
1549 static const struct got_error *
1550 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1551 const char *name, mode_t mode, struct got_object_id *blob_id)
1553 const struct got_error *err = NULL;
1555 *new_te = NULL;
1557 *new_te = calloc(1, sizeof(**new_te));
1558 if (*new_te == NULL)
1559 return got_error_from_errno("calloc");
1561 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1562 sizeof((*new_te)->name)) {
1563 err = got_error(GOT_ERR_NO_SPACE);
1564 goto done;
1567 if (S_ISLNK(mode)) {
1568 (*new_te)->mode = S_IFLNK;
1569 } else {
1570 (*new_te)->mode = S_IFREG;
1571 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1573 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1574 done:
1575 if (err && *new_te) {
1576 free(*new_te);
1577 *new_te = NULL;
1579 return err;
1582 static const struct got_error *
1583 import_file(struct got_tree_entry **new_te, struct dirent *de,
1584 const char *path, struct got_repository *repo)
1586 const struct got_error *err;
1587 struct got_object_id *blob_id = NULL;
1588 char *filepath;
1589 struct stat sb;
1591 if (asprintf(&filepath, "%s%s%s", path,
1592 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1593 return got_error_from_errno("asprintf");
1595 if (lstat(filepath, &sb) != 0) {
1596 err = got_error_from_errno2("lstat", path);
1597 goto done;
1600 err = got_object_blob_create(&blob_id, filepath, repo);
1601 if (err)
1602 goto done;
1604 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1605 blob_id);
1606 done:
1607 free(filepath);
1608 if (err)
1609 free(blob_id);
1610 return err;
1613 static const struct got_error *
1614 insert_tree_entry(struct got_tree_entry *new_te,
1615 struct got_pathlist_head *paths)
1617 const struct got_error *err = NULL;
1618 struct got_pathlist_entry *new_pe;
1620 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1621 if (err)
1622 return err;
1623 if (new_pe == NULL)
1624 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1625 return NULL;
1628 static const struct got_error *write_tree(struct got_object_id **,
1629 const char *, struct got_pathlist_head *, struct got_repository *,
1630 got_repo_import_cb progress_cb, void *progress_arg);
1632 static const struct got_error *
1633 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1634 const char *path, struct got_pathlist_head *ignores,
1635 struct got_repository *repo,
1636 got_repo_import_cb progress_cb, void *progress_arg)
1638 const struct got_error *err;
1639 struct got_object_id *id = NULL;
1640 char *subdirpath;
1642 if (asprintf(&subdirpath, "%s%s%s", path,
1643 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1644 return got_error_from_errno("asprintf");
1646 (*new_te) = calloc(1, sizeof(**new_te));
1647 if (*new_te == NULL)
1648 return got_error_from_errno("calloc");
1649 (*new_te)->mode = S_IFDIR;
1650 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1651 sizeof((*new_te)->name)) {
1652 err = got_error(GOT_ERR_NO_SPACE);
1653 goto done;
1655 err = write_tree(&id, subdirpath, ignores, repo,
1656 progress_cb, progress_arg);
1657 if (err)
1658 goto done;
1659 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1661 done:
1662 free(id);
1663 free(subdirpath);
1664 if (err) {
1665 free(*new_te);
1666 *new_te = NULL;
1668 return err;
1671 static const struct got_error *
1672 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1673 struct got_pathlist_head *ignores, struct got_repository *repo,
1674 got_repo_import_cb progress_cb, void *progress_arg)
1676 const struct got_error *err = NULL;
1677 DIR *dir;
1678 struct dirent *de;
1679 int nentries;
1680 struct got_tree_entry *new_te = NULL;
1681 struct got_pathlist_head paths;
1682 struct got_pathlist_entry *pe;
1684 *new_tree_id = NULL;
1686 TAILQ_INIT(&paths);
1688 dir = opendir(path_dir);
1689 if (dir == NULL) {
1690 err = got_error_from_errno2("opendir", path_dir);
1691 goto done;
1694 nentries = 0;
1695 while ((de = readdir(dir)) != NULL) {
1696 int ignore = 0;
1697 int type;
1699 if (strcmp(de->d_name, ".") == 0 ||
1700 strcmp(de->d_name, "..") == 0)
1701 continue;
1703 TAILQ_FOREACH(pe, ignores, entry) {
1704 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1705 ignore = 1;
1706 break;
1709 if (ignore)
1710 continue;
1712 err = got_path_dirent_type(&type, path_dir, de);
1713 if (err)
1714 goto done;
1716 if (type == DT_DIR) {
1717 err = import_subdir(&new_te, de, path_dir,
1718 ignores, repo, progress_cb, progress_arg);
1719 if (err) {
1720 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1721 goto done;
1722 err = NULL;
1723 continue;
1725 } else if (type == DT_REG || type == DT_LNK) {
1726 err = import_file(&new_te, de, path_dir, repo);
1727 if (err)
1728 goto done;
1729 } else
1730 continue;
1732 err = insert_tree_entry(new_te, &paths);
1733 if (err)
1734 goto done;
1735 nentries++;
1738 if (TAILQ_EMPTY(&paths)) {
1739 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
1740 "cannot create tree without any entries");
1741 goto done;
1744 TAILQ_FOREACH(pe, &paths, entry) {
1745 struct got_tree_entry *te = pe->data;
1746 char *path;
1747 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
1748 continue;
1749 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1750 err = got_error_from_errno("asprintf");
1751 goto done;
1753 err = (*progress_cb)(progress_arg, path);
1754 free(path);
1755 if (err)
1756 goto done;
1759 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1760 done:
1761 if (dir)
1762 closedir(dir);
1763 got_pathlist_free(&paths);
1764 return err;
1767 const struct got_error *
1768 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1769 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1770 struct got_repository *repo, got_repo_import_cb progress_cb,
1771 void *progress_arg)
1773 const struct got_error *err;
1774 struct got_object_id *new_tree_id;
1776 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1777 progress_cb, progress_arg);
1778 if (err)
1779 return err;
1781 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1782 author, time(NULL), author, time(NULL), logmsg, repo);
1783 free(new_tree_id);
1784 return err;