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 *repo_path = NULL;
593 size_t i;
595 *repop = NULL;
597 repo = calloc(1, sizeof(*repo));
598 if (repo == NULL) {
599 err = got_error_from_errno("calloc");
600 goto done;
603 for (i = 0; i < nitems(repo->privsep_children); i++) {
604 memset(&repo->privsep_children[i], 0,
605 sizeof(repo->privsep_children[0]));
606 repo->privsep_children[i].imsg_fd = -1;
609 err = got_object_cache_init(&repo->objcache,
610 GOT_OBJECT_CACHE_TYPE_OBJ);
611 if (err)
612 goto done;
613 err = got_object_cache_init(&repo->treecache,
614 GOT_OBJECT_CACHE_TYPE_TREE);
615 if (err)
616 goto done;
617 err = got_object_cache_init(&repo->commitcache,
618 GOT_OBJECT_CACHE_TYPE_COMMIT);
619 if (err)
620 goto done;
621 err = got_object_cache_init(&repo->tagcache,
622 GOT_OBJECT_CACHE_TYPE_TAG);
623 if (err)
624 goto done;
626 repo_path = realpath(path, NULL);
627 if (repo_path == NULL) {
628 err = got_error_from_errno2("realpath", path);
629 goto done;
632 for (;;) {
633 char *parent_path;
635 err = open_repo(repo, repo_path);
636 if (err == NULL)
637 break;
638 if (err->code != GOT_ERR_NOT_GIT_REPO)
639 goto done;
640 if (repo_path[0] == '/' && repo_path[1] == '\0') {
641 err = got_error(GOT_ERR_NOT_GIT_REPO);
642 goto done;
644 err = got_path_dirname(&parent_path, repo_path);
645 if (err)
646 goto done;
647 free(repo_path);
648 repo_path = parent_path;
651 err = read_gotconfig(repo);
652 if (err)
653 goto done;
655 err = read_gitconfig(repo, global_gitconfig_path);
656 if (err)
657 goto done;
658 if (repo->gitconfig_repository_format_version != 0)
659 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
660 for (i = 0; i < repo->nextensions; i++) {
661 char *ext = repo->extensions[i];
662 int j, supported = 0;
663 for (j = 0; j < nitems(repo_extensions); j++) {
664 if (strcmp(ext, repo_extensions[j]) == 0) {
665 supported = 1;
666 break;
669 if (!supported) {
670 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
671 goto done;
674 done:
675 if (err)
676 got_repo_close(repo);
677 else
678 *repop = repo;
679 free(repo_path);
680 return err;
683 const struct got_error *
684 got_repo_close(struct got_repository *repo)
686 const struct got_error *err = NULL, *child_err;
687 size_t i;
689 for (i = 0; i < nitems(repo->packidx_cache); i++) {
690 if (repo->packidx_cache[i] == NULL)
691 break;
692 got_packidx_close(repo->packidx_cache[i]);
695 for (i = 0; i < nitems(repo->packs); i++) {
696 if (repo->packs[i].path_packfile == NULL)
697 break;
698 got_pack_close(&repo->packs[i]);
701 free(repo->path);
702 free(repo->path_git_dir);
704 got_object_cache_close(&repo->objcache);
705 got_object_cache_close(&repo->treecache);
706 got_object_cache_close(&repo->commitcache);
707 got_object_cache_close(&repo->tagcache);
709 for (i = 0; i < nitems(repo->privsep_children); i++) {
710 if (repo->privsep_children[i].imsg_fd == -1)
711 continue;
712 imsg_clear(repo->privsep_children[i].ibuf);
713 free(repo->privsep_children[i].ibuf);
714 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
715 child_err = got_privsep_wait_for_child(
716 repo->privsep_children[i].pid);
717 if (child_err && err == NULL)
718 err = child_err;
719 if (close(repo->privsep_children[i].imsg_fd) != 0 &&
720 err == NULL)
721 err = got_error_from_errno("close");
724 if (repo->gotconfig)
725 got_gotconfig_free(repo->gotconfig);
726 free(repo->gitconfig_author_name);
727 free(repo->gitconfig_author_email);
728 for (i = 0; i < repo->ngitconfig_remotes; i++)
729 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
730 free(repo->gitconfig_remotes);
731 for (i = 0; i < repo->nextensions; i++)
732 free(repo->extensions[i]);
733 free(repo->extensions);
734 free(repo);
736 return err;
739 void
740 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
742 int i;
744 free(repo->name);
745 repo->name = NULL;
746 free(repo->url);
747 repo->url = NULL;
748 for (i = 0; i < repo->nbranches; i++)
749 free(repo->branches[i]);
750 free(repo->branches);
751 repo->branches = NULL;
752 repo->nbranches = 0;
755 const struct got_error *
756 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
757 const char *input_path)
759 const struct got_error *err = NULL;
760 const char *repo_abspath = NULL;
761 size_t repolen, len;
762 char *canonpath, *path = NULL;
764 *in_repo_path = NULL;
766 canonpath = strdup(input_path);
767 if (canonpath == NULL) {
768 err = got_error_from_errno("strdup");
769 goto done;
771 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
772 if (err)
773 goto done;
775 repo_abspath = got_repo_get_path(repo);
777 if (canonpath[0] == '\0') {
778 path = strdup(canonpath);
779 if (path == NULL) {
780 err = got_error_from_errno("strdup");
781 goto done;
783 } else {
784 path = realpath(canonpath, NULL);
785 if (path == NULL) {
786 if (errno != ENOENT) {
787 err = got_error_from_errno2("realpath",
788 canonpath);
789 goto done;
791 /*
792 * Path is not on disk.
793 * Assume it is already relative to repository root.
794 */
795 path = strdup(canonpath);
796 if (path == NULL) {
797 err = got_error_from_errno("strdup");
798 goto done;
802 repolen = strlen(repo_abspath);
803 len = strlen(path);
806 if (strcmp(path, repo_abspath) == 0) {
807 free(path);
808 path = strdup("");
809 if (path == NULL) {
810 err = got_error_from_errno("strdup");
811 goto done;
813 } else if (len > repolen &&
814 got_path_is_child(path, repo_abspath, repolen)) {
815 /* Matched an on-disk path inside repository. */
816 if (got_repo_is_bare(repo)) {
817 /*
818 * Matched an on-disk path inside repository
819 * database. Treat input as repository-relative.
820 */
821 free(path);
822 path = canonpath;
823 canonpath = NULL;
824 } else {
825 char *child;
826 /* Strip common prefix with repository path. */
827 err = got_path_skip_common_ancestor(&child,
828 repo_abspath, path);
829 if (err)
830 goto done;
831 free(path);
832 path = child;
834 } else {
835 /*
836 * Matched unrelated on-disk path.
837 * Treat input as repository-relative.
838 */
839 free(path);
840 path = canonpath;
841 canonpath = NULL;
845 /* Make in-repository path absolute */
846 if (path[0] != '/') {
847 char *abspath;
848 if (asprintf(&abspath, "/%s", path) == -1) {
849 err = got_error_from_errno("asprintf");
850 goto done;
852 free(path);
853 path = abspath;
856 done:
857 free(canonpath);
858 if (err)
859 free(path);
860 else
861 *in_repo_path = path;
862 return err;
865 static const struct got_error *
866 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
867 const char *path_packidx)
869 const struct got_error *err = NULL;
870 size_t i;
872 for (i = 0; i < nitems(repo->packidx_cache); i++) {
873 if (repo->packidx_cache[i] == NULL)
874 break;
875 if (strcmp(repo->packidx_cache[i]->path_packidx,
876 path_packidx) == 0) {
877 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
880 if (i == nitems(repo->packidx_cache)) {
881 err = got_packidx_close(repo->packidx_cache[i - 1]);
882 if (err)
883 return err;
886 /*
887 * Insert the new pack index at the front so it will
888 * be searched first in the future.
889 */
890 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
891 sizeof(repo->packidx_cache) -
892 sizeof(repo->packidx_cache[0]));
893 repo->packidx_cache[0] = packidx;
895 return NULL;
898 static int
899 is_packidx_filename(const char *name, size_t len)
901 if (len != GOT_PACKIDX_NAMELEN)
902 return 0;
904 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
905 return 0;
907 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
908 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
909 return 0;
911 return 1;
914 const struct got_error *
915 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
916 struct got_repository *repo, struct got_object_id *id)
918 const struct got_error *err;
919 char *path_packdir;
920 DIR *packdir;
921 struct dirent *dent;
922 char *path_packidx;
923 size_t i;
925 /* Search pack index cache. */
926 for (i = 0; i < nitems(repo->packidx_cache); i++) {
927 if (repo->packidx_cache[i] == NULL)
928 break;
929 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
930 if (*idx != -1) {
931 *packidx = repo->packidx_cache[i];
932 /*
933 * Move this cache entry to the front. Repeatedly
934 * searching a wrong pack index can be expensive.
935 */
936 if (i > 0) {
937 struct got_packidx *p;
938 p = repo->packidx_cache[0];
939 repo->packidx_cache[0] = *packidx;
940 repo->packidx_cache[i] = p;
942 return NULL;
945 /* No luck. Search the filesystem. */
947 path_packdir = got_repo_get_path_objects_pack(repo);
948 if (path_packdir == NULL)
949 return got_error_from_errno("got_repo_get_path_objects_pack");
951 packdir = opendir(path_packdir);
952 if (packdir == NULL) {
953 if (errno == ENOENT)
954 err = got_error_no_obj(id);
955 else
956 err = got_error_from_errno2("opendir", path_packdir);
957 goto done;
960 while ((dent = readdir(packdir)) != NULL) {
961 int is_cached = 0;
963 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
964 continue;
966 if (asprintf(&path_packidx, "%s/%s", path_packdir,
967 dent->d_name) == -1) {
968 err = got_error_from_errno("asprintf");
969 goto done;
972 for (i = 0; i < nitems(repo->packidx_cache); i++) {
973 if (repo->packidx_cache[i] == NULL)
974 break;
975 if (strcmp(repo->packidx_cache[i]->path_packidx,
976 path_packidx) == 0) {
977 is_cached = 1;
978 break;
981 if (is_cached) {
982 free(path_packidx);
983 continue; /* already searched */
986 err = got_packidx_open(packidx, path_packidx, 0);
987 if (err) {
988 free(path_packidx);
989 goto done;
992 err = cache_packidx(repo, *packidx, path_packidx);
993 free(path_packidx);
994 if (err)
995 goto done;
997 *idx = got_packidx_get_object_idx(*packidx, id);
998 if (*idx != -1) {
999 err = NULL; /* found the object */
1000 goto done;
1004 err = got_error_no_obj(id);
1005 done:
1006 free(path_packdir);
1007 if (packdir && closedir(packdir) != 0 && err == NULL)
1008 err = got_error_from_errno("closedir");
1009 return err;
1012 static const struct got_error *
1013 read_packfile_hdr(int fd, struct got_packidx *packidx)
1015 const struct got_error *err = NULL;
1016 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1017 struct got_packfile_hdr hdr;
1018 ssize_t n;
1020 n = read(fd, &hdr, sizeof(hdr));
1021 if (n < 0)
1022 return got_error_from_errno("read");
1023 if (n != sizeof(hdr))
1024 return got_error(GOT_ERR_BAD_PACKFILE);
1026 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1027 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1028 be32toh(hdr.nobjects) != totobj)
1029 err = got_error(GOT_ERR_BAD_PACKFILE);
1031 return err;
1034 static const struct got_error *
1035 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
1037 const struct got_error *err = NULL;
1039 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
1040 if (*fd == -1)
1041 return got_error_from_errno2("open", path_packfile);
1043 if (packidx) {
1044 err = read_packfile_hdr(*fd, packidx);
1045 if (err) {
1046 close(*fd);
1047 *fd = -1;
1051 return err;
1054 const struct got_error *
1055 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1056 const char *path_packfile, struct got_packidx *packidx)
1058 const struct got_error *err = NULL;
1059 struct got_pack *pack = NULL;
1060 struct stat sb;
1061 size_t i;
1063 if (packp)
1064 *packp = NULL;
1066 for (i = 0; i < nitems(repo->packs); i++) {
1067 pack = &repo->packs[i];
1068 if (pack->path_packfile == NULL)
1069 break;
1070 if (strcmp(pack->path_packfile, path_packfile) == 0)
1071 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1074 if (i == nitems(repo->packs) - 1) {
1075 err = got_pack_close(&repo->packs[i - 1]);
1076 if (err)
1077 return err;
1078 memmove(&repo->packs[1], &repo->packs[0],
1079 sizeof(repo->packs) - sizeof(repo->packs[0]));
1080 i = 0;
1083 pack = &repo->packs[i];
1085 pack->path_packfile = strdup(path_packfile);
1086 if (pack->path_packfile == NULL) {
1087 err = got_error_from_errno("strdup");
1088 goto done;
1091 err = open_packfile(&pack->fd, path_packfile, packidx);
1092 if (err)
1093 goto done;
1095 if (fstat(pack->fd, &sb) != 0) {
1096 err = got_error_from_errno("fstat");
1097 goto done;
1099 pack->filesize = sb.st_size;
1101 pack->privsep_child = NULL;
1103 #ifndef GOT_PACK_NO_MMAP
1104 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1105 pack->fd, 0);
1106 if (pack->map == MAP_FAILED) {
1107 if (errno != ENOMEM) {
1108 err = got_error_from_errno("mmap");
1109 goto done;
1111 pack->map = NULL; /* fall back to read(2) */
1113 #endif
1114 done:
1115 if (err) {
1116 if (pack) {
1117 free(pack->path_packfile);
1118 memset(pack, 0, sizeof(*pack));
1120 } else if (packp)
1121 *packp = pack;
1122 return err;
1125 struct got_pack *
1126 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1128 struct got_pack *pack = NULL;
1129 size_t i;
1131 for (i = 0; i < nitems(repo->packs); i++) {
1132 pack = &repo->packs[i];
1133 if (pack->path_packfile == NULL)
1134 break;
1135 if (strcmp(pack->path_packfile, path_packfile) == 0)
1136 return pack;
1139 return NULL;
1142 const struct got_error *
1143 got_repo_init(const char *repo_path)
1145 const struct got_error *err = NULL;
1146 const char *dirnames[] = {
1147 GOT_OBJECTS_DIR,
1148 GOT_OBJECTS_PACK_DIR,
1149 GOT_REFS_DIR,
1151 const char *description_str = "Unnamed repository; "
1152 "edit this file 'description' to name the repository.";
1153 const char *headref_str = "ref: refs/heads/main";
1154 const char *gitconfig_str = "[core]\n"
1155 "\trepositoryformatversion = 0\n"
1156 "\tfilemode = true\n"
1157 "\tbare = true\n";
1158 char *path;
1159 size_t i;
1161 if (!got_path_dir_is_empty(repo_path))
1162 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1164 for (i = 0; i < nitems(dirnames); i++) {
1165 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1166 return got_error_from_errno("asprintf");
1168 err = got_path_mkdir(path);
1169 free(path);
1170 if (err)
1171 return err;
1174 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1175 return got_error_from_errno("asprintf");
1176 err = got_path_create_file(path, description_str);
1177 free(path);
1178 if (err)
1179 return err;
1181 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1182 return got_error_from_errno("asprintf");
1183 err = got_path_create_file(path, headref_str);
1184 free(path);
1185 if (err)
1186 return err;
1188 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1189 return got_error_from_errno("asprintf");
1190 err = got_path_create_file(path, gitconfig_str);
1191 free(path);
1192 if (err)
1193 return err;
1195 return NULL;
1198 static const struct got_error *
1199 match_packed_object(struct got_object_id **unique_id,
1200 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1202 const struct got_error *err = NULL;
1203 char *path_packdir;
1204 DIR *packdir;
1205 struct dirent *dent;
1206 char *path_packidx;
1207 struct got_object_id_queue matched_ids;
1209 SIMPLEQ_INIT(&matched_ids);
1211 path_packdir = got_repo_get_path_objects_pack(repo);
1212 if (path_packdir == NULL)
1213 return got_error_from_errno("got_repo_get_path_objects_pack");
1215 packdir = opendir(path_packdir);
1216 if (packdir == NULL) {
1217 if (errno != ENOENT)
1218 err = got_error_from_errno2("opendir", path_packdir);
1219 goto done;
1222 while ((dent = readdir(packdir)) != NULL) {
1223 struct got_packidx *packidx;
1224 struct got_object_qid *qid;
1227 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1228 continue;
1230 if (asprintf(&path_packidx, "%s/%s", path_packdir,
1231 dent->d_name) == -1) {
1232 err = got_error_from_errno("asprintf");
1233 break;
1236 err = got_packidx_open(&packidx, path_packidx, 0);
1237 free(path_packidx);
1238 if (err)
1239 break;
1241 err = got_packidx_match_id_str_prefix(&matched_ids,
1242 packidx, id_str_prefix);
1243 if (err) {
1244 got_packidx_close(packidx);
1245 break;
1247 err = got_packidx_close(packidx);
1248 if (err)
1249 break;
1251 SIMPLEQ_FOREACH(qid, &matched_ids, entry) {
1252 if (obj_type != GOT_OBJ_TYPE_ANY) {
1253 int matched_type;
1254 err = got_object_get_type(&matched_type, repo,
1255 qid->id);
1256 if (err)
1257 goto done;
1258 if (matched_type != obj_type)
1259 continue;
1261 if (*unique_id == NULL) {
1262 *unique_id = got_object_id_dup(qid->id);
1263 if (*unique_id == NULL) {
1264 err = got_error_from_errno("malloc");
1265 goto done;
1267 } else {
1268 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1269 continue; /* packed multiple times */
1270 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1271 goto done;
1275 done:
1276 got_object_id_queue_free(&matched_ids);
1277 free(path_packdir);
1278 if (packdir && closedir(packdir) != 0 && err == NULL)
1279 err = got_error_from_errno("closedir");
1280 if (err) {
1281 free(*unique_id);
1282 *unique_id = NULL;
1284 return err;
1287 static const struct got_error *
1288 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1289 const char *object_dir, const char *id_str_prefix, int obj_type,
1290 struct got_repository *repo)
1292 const struct got_error *err = NULL;
1293 char *path;
1294 DIR *dir = NULL;
1295 struct dirent *dent;
1296 struct got_object_id id;
1298 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1299 err = got_error_from_errno("asprintf");
1300 goto done;
1303 dir = opendir(path);
1304 if (dir == NULL) {
1305 if (errno == ENOENT) {
1306 err = NULL;
1307 goto done;
1309 err = got_error_from_errno2("opendir", path);
1310 goto done;
1312 while ((dent = readdir(dir)) != NULL) {
1313 char *id_str;
1314 int cmp;
1316 if (strcmp(dent->d_name, ".") == 0 ||
1317 strcmp(dent->d_name, "..") == 0)
1318 continue;
1320 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1321 err = got_error_from_errno("asprintf");
1322 goto done;
1325 if (!got_parse_sha1_digest(id.sha1, id_str))
1326 continue;
1329 * Directory entries do not necessarily appear in
1330 * sorted order, so we must iterate over all of them.
1332 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1333 if (cmp != 0) {
1334 free(id_str);
1335 continue;
1338 if (*unique_id == NULL) {
1339 if (obj_type != GOT_OBJ_TYPE_ANY) {
1340 int matched_type;
1341 err = got_object_get_type(&matched_type, repo,
1342 &id);
1343 if (err)
1344 goto done;
1345 if (matched_type != obj_type)
1346 continue;
1348 *unique_id = got_object_id_dup(&id);
1349 if (*unique_id == NULL) {
1350 err = got_error_from_errno("got_object_id_dup");
1351 free(id_str);
1352 goto done;
1354 } else {
1355 if (got_object_id_cmp(*unique_id, &id) == 0)
1356 continue; /* both packed and loose */
1357 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1358 free(id_str);
1359 goto done;
1362 done:
1363 if (dir && closedir(dir) != 0 && err == NULL)
1364 err = got_error_from_errno("closedir");
1365 if (err) {
1366 free(*unique_id);
1367 *unique_id = NULL;
1369 free(path);
1370 return err;
1373 const struct got_error *
1374 got_repo_match_object_id_prefix(struct got_object_id **id,
1375 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1377 const struct got_error *err = NULL;
1378 char *path_objects = got_repo_get_path_objects(repo);
1379 char *object_dir = NULL;
1380 size_t len;
1381 int i;
1383 *id = NULL;
1385 for (i = 0; i < strlen(id_str_prefix); i++) {
1386 if (isxdigit((unsigned char)id_str_prefix[i]))
1387 continue;
1388 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1391 len = strlen(id_str_prefix);
1392 if (len >= 2) {
1393 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1394 if (err)
1395 goto done;
1396 object_dir = strndup(id_str_prefix, 2);
1397 if (object_dir == NULL) {
1398 err = got_error_from_errno("strdup");
1399 goto done;
1401 err = match_loose_object(id, path_objects, object_dir,
1402 id_str_prefix, obj_type, repo);
1403 } else if (len == 1) {
1404 int i;
1405 for (i = 0; i < 0xf; i++) {
1406 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1407 == -1) {
1408 err = got_error_from_errno("asprintf");
1409 goto done;
1411 err = match_packed_object(id, repo, object_dir,
1412 obj_type);
1413 if (err)
1414 goto done;
1415 err = match_loose_object(id, path_objects, object_dir,
1416 id_str_prefix, obj_type, repo);
1417 if (err)
1418 goto done;
1420 } else {
1421 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1422 goto done;
1424 done:
1425 free(object_dir);
1426 if (err) {
1427 free(*id);
1428 *id = NULL;
1429 } else if (*id == NULL)
1430 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1432 return err;
1435 const struct got_error *
1436 got_repo_match_object_id(struct got_object_id **id, char **label,
1437 const char *id_str, int obj_type, int resolve_tags,
1438 struct got_repository *repo)
1440 const struct got_error *err;
1441 struct got_tag_object *tag;
1442 struct got_reference *ref = NULL;
1444 *id = NULL;
1445 if (label)
1446 *label = NULL;
1448 if (resolve_tags) {
1449 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
1450 repo);
1451 if (err == NULL) {
1452 *id = got_object_id_dup(
1453 got_object_tag_get_object_id(tag));
1454 if (*id == NULL)
1455 err = got_error_from_errno("got_object_id_dup");
1456 else if (label && asprintf(label, "refs/tags/%s",
1457 got_object_tag_get_name(tag)) == -1) {
1458 err = got_error_from_errno("asprintf");
1459 free(*id);
1460 *id = NULL;
1462 got_object_tag_close(tag);
1463 return err;
1464 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1465 err->code != GOT_ERR_NO_OBJ)
1466 return err;
1469 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1470 if (err) {
1471 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1472 return err;
1473 err = got_ref_open(&ref, repo, id_str, 0);
1474 if (err != NULL)
1475 goto done;
1476 if (label) {
1477 *label = strdup(got_ref_get_name(ref));
1478 if (*label == NULL) {
1479 err = got_error_from_errno("strdup");
1480 goto done;
1483 err = got_ref_resolve(id, repo, ref);
1484 } else if (label) {
1485 err = got_object_id_str(label, *id);
1486 if (*label == NULL) {
1487 err = got_error_from_errno("strdup");
1488 goto done;
1491 done:
1492 if (ref)
1493 got_ref_close(ref);
1494 return err;
1497 const struct got_error *
1498 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1499 int obj_type, struct got_repository *repo)
1501 const struct got_error *err;
1502 struct got_reflist_head refs;
1503 struct got_reflist_entry *re;
1504 struct got_object_id *tag_id;
1505 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1507 SIMPLEQ_INIT(&refs);
1508 *tag = NULL;
1510 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_by_name, NULL);
1511 if (err)
1512 return err;
1514 SIMPLEQ_FOREACH(re, &refs, entry) {
1515 const char *refname;
1516 refname = got_ref_get_name(re->ref);
1517 if (got_ref_is_symbolic(re->ref))
1518 continue;
1519 if (!name_is_absolute)
1520 refname += strlen("refs/tags/");
1521 if (strcmp(refname, name) != 0)
1522 continue;
1523 err = got_ref_resolve(&tag_id, repo, re->ref);
1524 if (err)
1525 break;
1526 err = got_object_open_as_tag(tag, repo, tag_id);
1527 free(tag_id);
1528 if (err)
1529 break;
1530 if (obj_type == GOT_OBJ_TYPE_ANY ||
1531 got_object_tag_get_object_type(*tag) == obj_type)
1532 break;
1533 got_object_tag_close(*tag);
1534 *tag = NULL;
1537 got_ref_list_free(&refs);
1538 if (err == NULL && *tag == NULL)
1539 err = got_error_path(name, GOT_ERR_NO_OBJ);
1540 return err;
1543 static const struct got_error *
1544 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1545 const char *name, mode_t mode, struct got_object_id *blob_id)
1547 const struct got_error *err = NULL;
1549 *new_te = NULL;
1551 *new_te = calloc(1, sizeof(**new_te));
1552 if (*new_te == NULL)
1553 return got_error_from_errno("calloc");
1555 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1556 sizeof((*new_te)->name)) {
1557 err = got_error(GOT_ERR_NO_SPACE);
1558 goto done;
1561 if (S_ISLNK(mode)) {
1562 (*new_te)->mode = S_IFLNK;
1563 } else {
1564 (*new_te)->mode = S_IFREG;
1565 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1567 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1568 done:
1569 if (err && *new_te) {
1570 free(*new_te);
1571 *new_te = NULL;
1573 return err;
1576 static const struct got_error *
1577 import_file(struct got_tree_entry **new_te, struct dirent *de,
1578 const char *path, struct got_repository *repo)
1580 const struct got_error *err;
1581 struct got_object_id *blob_id = NULL;
1582 char *filepath;
1583 struct stat sb;
1585 if (asprintf(&filepath, "%s%s%s", path,
1586 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1587 return got_error_from_errno("asprintf");
1589 if (lstat(filepath, &sb) != 0) {
1590 err = got_error_from_errno2("lstat", path);
1591 goto done;
1594 err = got_object_blob_create(&blob_id, filepath, repo);
1595 if (err)
1596 goto done;
1598 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1599 blob_id);
1600 done:
1601 free(filepath);
1602 if (err)
1603 free(blob_id);
1604 return err;
1607 static const struct got_error *
1608 insert_tree_entry(struct got_tree_entry *new_te,
1609 struct got_pathlist_head *paths)
1611 const struct got_error *err = NULL;
1612 struct got_pathlist_entry *new_pe;
1614 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1615 if (err)
1616 return err;
1617 if (new_pe == NULL)
1618 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1619 return NULL;
1622 static const struct got_error *write_tree(struct got_object_id **,
1623 const char *, struct got_pathlist_head *, struct got_repository *,
1624 got_repo_import_cb progress_cb, void *progress_arg);
1626 static const struct got_error *
1627 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1628 const char *path, struct got_pathlist_head *ignores,
1629 struct got_repository *repo,
1630 got_repo_import_cb progress_cb, void *progress_arg)
1632 const struct got_error *err;
1633 struct got_object_id *id = NULL;
1634 char *subdirpath;
1636 if (asprintf(&subdirpath, "%s%s%s", path,
1637 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1638 return got_error_from_errno("asprintf");
1640 (*new_te) = calloc(1, sizeof(**new_te));
1641 if (*new_te == NULL)
1642 return got_error_from_errno("calloc");
1643 (*new_te)->mode = S_IFDIR;
1644 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1645 sizeof((*new_te)->name)) {
1646 err = got_error(GOT_ERR_NO_SPACE);
1647 goto done;
1649 err = write_tree(&id, subdirpath, ignores, repo,
1650 progress_cb, progress_arg);
1651 if (err)
1652 goto done;
1653 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1655 done:
1656 free(id);
1657 free(subdirpath);
1658 if (err) {
1659 free(*new_te);
1660 *new_te = NULL;
1662 return err;
1665 static const struct got_error *
1666 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1667 struct got_pathlist_head *ignores, struct got_repository *repo,
1668 got_repo_import_cb progress_cb, void *progress_arg)
1670 const struct got_error *err = NULL;
1671 DIR *dir;
1672 struct dirent *de;
1673 int nentries;
1674 struct got_tree_entry *new_te = NULL;
1675 struct got_pathlist_head paths;
1676 struct got_pathlist_entry *pe;
1678 *new_tree_id = NULL;
1680 TAILQ_INIT(&paths);
1682 dir = opendir(path_dir);
1683 if (dir == NULL) {
1684 err = got_error_from_errno2("opendir", path_dir);
1685 goto done;
1688 nentries = 0;
1689 while ((de = readdir(dir)) != NULL) {
1690 int ignore = 0;
1691 int type;
1693 if (strcmp(de->d_name, ".") == 0 ||
1694 strcmp(de->d_name, "..") == 0)
1695 continue;
1697 TAILQ_FOREACH(pe, ignores, entry) {
1698 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1699 ignore = 1;
1700 break;
1703 if (ignore)
1704 continue;
1706 err = got_path_dirent_type(&type, path_dir, de);
1707 if (err)
1708 goto done;
1710 if (type == DT_DIR) {
1711 err = import_subdir(&new_te, de, path_dir,
1712 ignores, repo, progress_cb, progress_arg);
1713 if (err) {
1714 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1715 goto done;
1716 err = NULL;
1717 continue;
1719 } else if (type == DT_REG || type == DT_LNK) {
1720 err = import_file(&new_te, de, path_dir, repo);
1721 if (err)
1722 goto done;
1723 } else
1724 continue;
1726 err = insert_tree_entry(new_te, &paths);
1727 if (err)
1728 goto done;
1729 nentries++;
1732 if (TAILQ_EMPTY(&paths)) {
1733 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
1734 "cannot create tree without any entries");
1735 goto done;
1738 TAILQ_FOREACH(pe, &paths, entry) {
1739 struct got_tree_entry *te = pe->data;
1740 char *path;
1741 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
1742 continue;
1743 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1744 err = got_error_from_errno("asprintf");
1745 goto done;
1747 err = (*progress_cb)(progress_arg, path);
1748 free(path);
1749 if (err)
1750 goto done;
1753 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1754 done:
1755 if (dir)
1756 closedir(dir);
1757 got_pathlist_free(&paths);
1758 return err;
1761 const struct got_error *
1762 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1763 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1764 struct got_repository *repo, got_repo_import_cb progress_cb,
1765 void *progress_arg)
1767 const struct got_error *err;
1768 struct got_object_id *new_tree_id;
1770 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1771 progress_cb, progress_arg);
1772 if (err)
1773 return err;
1775 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1776 author, time(NULL), author, time(NULL), logmsg, repo);
1777 free(new_tree_id);
1778 return err;