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"
63 #include "got_lib_gotconfig.h"
65 #ifndef nitems
66 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
67 #endif
69 const char *
70 got_repo_get_path(struct got_repository *repo)
71 {
72 return repo->path;
73 }
75 const char *
76 got_repo_get_path_git_dir(struct got_repository *repo)
77 {
78 return repo->path_git_dir;
79 }
81 const char *
82 got_repo_get_gitconfig_author_name(struct got_repository *repo)
83 {
84 return repo->gitconfig_author_name;
85 }
87 const char *
88 got_repo_get_gitconfig_author_email(struct got_repository *repo)
89 {
90 return repo->gitconfig_author_email;
91 }
93 const char *
94 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
95 {
96 return repo->global_gitconfig_author_name;
97 }
99 const char *
100 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
102 return repo->global_gitconfig_author_email;
105 const char *
106 got_repo_get_gitconfig_owner(struct got_repository *repo)
108 return repo->gitconfig_owner;
111 int
112 got_repo_is_bare(struct got_repository *repo)
114 return (strcmp(repo->path, repo->path_git_dir) == 0);
117 static char *
118 get_path_git_child(struct got_repository *repo, const char *basename)
120 char *path_child;
122 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
123 basename) == -1)
124 return NULL;
126 return path_child;
129 char *
130 got_repo_get_path_objects(struct got_repository *repo)
132 return get_path_git_child(repo, GOT_OBJECTS_DIR);
135 char *
136 got_repo_get_path_objects_pack(struct got_repository *repo)
138 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
141 char *
142 got_repo_get_path_refs(struct got_repository *repo)
144 return get_path_git_child(repo, GOT_REFS_DIR);
147 char *
148 got_repo_get_path_packed_refs(struct got_repository *repo)
150 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
153 static char *
154 get_path_head(struct got_repository *repo)
156 return get_path_git_child(repo, GOT_HEAD_FILE);
159 char *
160 got_repo_get_path_gitconfig(struct got_repository *repo)
162 return get_path_git_child(repo, GOT_GITCONFIG);
165 char *
166 got_repo_get_path_gotconfig(struct got_repository *repo)
168 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
171 const struct got_gotconfig *
172 got_repo_get_gotconfig(struct got_repository *repo)
174 return repo->gotconfig;
177 void
178 got_repo_get_gitconfig_remotes(int *nremotes,
179 const struct got_remote_repo **remotes, struct got_repository *repo)
181 *nremotes = repo->ngitconfig_remotes;
182 *remotes = repo->gitconfig_remotes;
185 static int
186 is_git_repo(struct got_repository *repo)
188 const char *path_git = got_repo_get_path_git_dir(repo);
189 char *path_objects = got_repo_get_path_objects(repo);
190 char *path_refs = got_repo_get_path_refs(repo);
191 char *path_head = get_path_head(repo);
192 int ret = 0;
193 struct stat sb;
194 struct got_reference *head_ref;
196 if (lstat(path_git, &sb) == -1)
197 goto done;
198 if (!S_ISDIR(sb.st_mode))
199 goto done;
201 if (lstat(path_objects, &sb) == -1)
202 goto done;
203 if (!S_ISDIR(sb.st_mode))
204 goto done;
206 if (lstat(path_refs, &sb) == -1)
207 goto done;
208 if (!S_ISDIR(sb.st_mode))
209 goto done;
211 if (lstat(path_head, &sb) == -1)
212 goto done;
213 if (!S_ISREG(sb.st_mode))
214 goto done;
216 /* Check if the HEAD reference can be opened. */
217 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
218 goto done;
219 got_ref_close(head_ref);
221 ret = 1;
222 done:
223 free(path_objects);
224 free(path_refs);
225 free(path_head);
226 return ret;
230 const struct got_error *
231 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
232 struct got_object *obj)
234 #ifndef GOT_NO_OBJ_CACHE
235 const struct got_error *err = NULL;
236 err = got_object_cache_add(&repo->objcache, id, obj);
237 if (err) {
238 if (err->code == GOT_ERR_OBJ_EXISTS ||
239 err->code == GOT_ERR_OBJ_TOO_LARGE)
240 err = NULL;
241 return err;
243 obj->refcnt++;
244 #endif
245 return NULL;
248 struct got_object *
249 got_repo_get_cached_object(struct got_repository *repo,
250 struct got_object_id *id)
252 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
255 const struct got_error *
256 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
257 struct got_tree_object *tree)
259 #ifndef GOT_NO_OBJ_CACHE
260 const struct got_error *err = NULL;
261 err = got_object_cache_add(&repo->treecache, id, tree);
262 if (err) {
263 if (err->code == GOT_ERR_OBJ_EXISTS ||
264 err->code == GOT_ERR_OBJ_TOO_LARGE)
265 err = NULL;
266 return err;
268 tree->refcnt++;
269 #endif
270 return NULL;
273 struct got_tree_object *
274 got_repo_get_cached_tree(struct got_repository *repo,
275 struct got_object_id *id)
277 return (struct got_tree_object *)got_object_cache_get(
278 &repo->treecache, id);
281 const struct got_error *
282 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
283 struct got_commit_object *commit)
285 #ifndef GOT_NO_OBJ_CACHE
286 const struct got_error *err = NULL;
287 err = got_object_cache_add(&repo->commitcache, id, commit);
288 if (err) {
289 if (err->code == GOT_ERR_OBJ_EXISTS ||
290 err->code == GOT_ERR_OBJ_TOO_LARGE)
291 err = NULL;
292 return err;
294 commit->refcnt++;
295 #endif
296 return NULL;
299 struct got_commit_object *
300 got_repo_get_cached_commit(struct got_repository *repo,
301 struct got_object_id *id)
303 return (struct got_commit_object *)got_object_cache_get(
304 &repo->commitcache, id);
307 const struct got_error *
308 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
309 struct got_tag_object *tag)
311 #ifndef GOT_NO_OBJ_CACHE
312 const struct got_error *err = NULL;
313 err = got_object_cache_add(&repo->tagcache, id, tag);
314 if (err) {
315 if (err->code == GOT_ERR_OBJ_EXISTS ||
316 err->code == GOT_ERR_OBJ_TOO_LARGE)
317 err = NULL;
318 return err;
320 tag->refcnt++;
321 #endif
322 return NULL;
325 struct got_tag_object *
326 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
328 return (struct got_tag_object *)got_object_cache_get(
329 &repo->tagcache, id);
332 const struct got_error *
333 open_repo(struct got_repository *repo, const char *path)
335 const struct got_error *err = NULL;
337 /* bare git repository? */
338 repo->path_git_dir = strdup(path);
339 if (repo->path_git_dir == NULL)
340 return got_error_from_errno("strdup");
341 if (is_git_repo(repo)) {
342 repo->path = strdup(repo->path_git_dir);
343 if (repo->path == NULL) {
344 err = got_error_from_errno("strdup");
345 goto done;
347 return NULL;
350 /* git repository with working tree? */
351 free(repo->path_git_dir);
352 repo->path_git_dir = NULL;
353 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
354 err = got_error_from_errno("asprintf");
355 goto done;
357 if (is_git_repo(repo)) {
358 repo->path = strdup(path);
359 if (repo->path == NULL) {
360 err = got_error_from_errno("strdup");
361 goto done;
363 return NULL;
366 err = got_error(GOT_ERR_NOT_GIT_REPO);
367 done:
368 if (err) {
369 free(repo->path);
370 repo->path = NULL;
371 free(repo->path_git_dir);
372 repo->path_git_dir = NULL;
374 return err;
377 static const struct got_error *
378 parse_gitconfig_file(int *gitconfig_repository_format_version,
379 char **gitconfig_author_name, char **gitconfig_author_email,
380 struct got_remote_repo **remotes, int *nremotes,
381 char **gitconfig_owner,
382 const char *gitconfig_path)
384 const struct got_error *err = NULL, *child_err = NULL;
385 int fd = -1;
386 int imsg_fds[2] = { -1, -1 };
387 pid_t pid;
388 struct imsgbuf *ibuf;
390 *gitconfig_repository_format_version = 0;
391 *gitconfig_author_name = NULL;
392 *gitconfig_author_email = NULL;
393 if (remotes)
394 *remotes = NULL;
395 if (nremotes)
396 *nremotes = 0;
397 if (gitconfig_owner)
398 *gitconfig_owner = NULL;
400 fd = open(gitconfig_path, O_RDONLY);
401 if (fd == -1) {
402 if (errno == ENOENT)
403 return NULL;
404 return got_error_from_errno2("open", gitconfig_path);
407 ibuf = calloc(1, sizeof(*ibuf));
408 if (ibuf == NULL) {
409 err = got_error_from_errno("calloc");
410 goto done;
413 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
414 err = got_error_from_errno("socketpair");
415 goto done;
418 pid = fork();
419 if (pid == -1) {
420 err = got_error_from_errno("fork");
421 goto done;
422 } else if (pid == 0) {
423 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
424 gitconfig_path);
425 /* not reached */
428 if (close(imsg_fds[1]) == -1) {
429 err = got_error_from_errno("close");
430 goto done;
432 imsg_fds[1] = -1;
433 imsg_init(ibuf, imsg_fds[0]);
435 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
436 if (err)
437 goto done;
438 fd = -1;
440 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
441 if (err)
442 goto done;
444 err = got_privsep_recv_gitconfig_int(
445 gitconfig_repository_format_version, ibuf);
446 if (err)
447 goto done;
449 err = got_privsep_send_gitconfig_author_name_req(ibuf);
450 if (err)
451 goto done;
453 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
454 if (err)
455 goto done;
457 err = got_privsep_send_gitconfig_author_email_req(ibuf);
458 if (err)
459 goto done;
461 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
462 if (err)
463 goto done;
465 if (remotes && nremotes) {
466 err = got_privsep_send_gitconfig_remotes_req(ibuf);
467 if (err)
468 goto done;
470 err = got_privsep_recv_gitconfig_remotes(remotes,
471 nremotes, ibuf);
472 if (err)
473 goto done;
476 if (gitconfig_owner) {
477 err = got_privsep_send_gitconfig_owner_req(ibuf);
478 if (err)
479 goto done;
480 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
481 if (err)
482 goto done;
485 imsg_clear(ibuf);
486 err = got_privsep_send_stop(imsg_fds[0]);
487 child_err = got_privsep_wait_for_child(pid);
488 if (child_err && err == NULL)
489 err = child_err;
490 done:
491 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
492 err = got_error_from_errno("close");
493 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
494 err = got_error_from_errno("close");
495 if (fd != -1 && close(fd) == -1 && err == NULL)
496 err = got_error_from_errno2("close", gitconfig_path);
497 free(ibuf);
498 return err;
501 static const struct got_error *
502 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
504 const struct got_error *err = NULL;
505 char *repo_gitconfig_path = NULL;
507 if (global_gitconfig_path) {
508 /* Read settings from ~/.gitconfig. */
509 int dummy_repo_version;
510 err = parse_gitconfig_file(&dummy_repo_version,
511 &repo->global_gitconfig_author_name,
512 &repo->global_gitconfig_author_email,
513 NULL, NULL, NULL, global_gitconfig_path);
514 if (err)
515 return err;
518 /* Read repository's .git/config file. */
519 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
520 if (repo_gitconfig_path == NULL)
521 return got_error_from_errno("got_repo_get_path_gitconfig");
523 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
524 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
525 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
526 &repo->gitconfig_owner, repo_gitconfig_path);
527 if (err)
528 goto done;
529 done:
530 free(repo_gitconfig_path);
531 return err;
534 static const struct got_error *
535 read_gotconfig(struct got_repository *repo)
537 const struct got_error *err = NULL;
538 char *gotconfig_path;
540 gotconfig_path = got_repo_get_path_gotconfig(repo);
541 if (gotconfig_path == NULL)
542 return got_error_from_errno("got_repo_get_path_gotconfig");
544 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
545 free(gotconfig_path);
546 return err;
549 const struct got_error *
550 got_repo_open(struct got_repository **repop, const char *path,
551 const char *global_gitconfig_path)
553 struct got_repository *repo = NULL;
554 const struct got_error *err = NULL;
555 char *abspath;
556 int i, tried_root = 0;
558 *repop = NULL;
560 if (got_path_is_absolute(path))
561 abspath = strdup(path);
562 else
563 abspath = got_path_get_absolute(path);
564 if (abspath == NULL)
565 return got_error_path(path, GOT_ERR_BAD_PATH);
567 repo = calloc(1, sizeof(*repo));
568 if (repo == NULL) {
569 err = got_error_from_errno("calloc");
570 goto done;
573 for (i = 0; i < nitems(repo->privsep_children); i++) {
574 memset(&repo->privsep_children[i], 0,
575 sizeof(repo->privsep_children[0]));
576 repo->privsep_children[i].imsg_fd = -1;
579 err = got_object_cache_init(&repo->objcache,
580 GOT_OBJECT_CACHE_TYPE_OBJ);
581 if (err)
582 goto done;
583 err = got_object_cache_init(&repo->treecache,
584 GOT_OBJECT_CACHE_TYPE_TREE);
585 if (err)
586 goto done;
587 err = got_object_cache_init(&repo->commitcache,
588 GOT_OBJECT_CACHE_TYPE_COMMIT);
589 if (err)
590 goto done;
591 err = got_object_cache_init(&repo->tagcache,
592 GOT_OBJECT_CACHE_TYPE_TAG);
593 if (err)
594 goto done;
596 path = realpath(abspath, NULL);
597 if (path == NULL) {
598 err = got_error_from_errno2("realpath", abspath);
599 goto done;
602 do {
603 err = open_repo(repo, path);
604 if (err == NULL)
605 break;
606 if (err->code != GOT_ERR_NOT_GIT_REPO)
607 break;
608 if (path[0] == '/' && path[1] == '\0') {
609 if (tried_root) {
610 err = got_error(GOT_ERR_NOT_GIT_REPO);
611 goto done;
613 tried_root = 1;
615 path = dirname(path);
616 if (path == NULL) {
617 err = got_error_from_errno2("dirname", path);
618 goto done;
620 } while (path);
622 err = read_gotconfig(repo);
623 if (err)
624 goto done;
626 err = read_gitconfig(repo, global_gitconfig_path);
627 if (err)
628 goto done;
629 if (repo->gitconfig_repository_format_version != 0)
630 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
631 done:
632 if (err)
633 got_repo_close(repo);
634 else
635 *repop = repo;
636 free(abspath);
637 return err;
640 const struct got_error *
641 got_repo_close(struct got_repository *repo)
643 const struct got_error *err = NULL, *child_err;
644 int i;
646 for (i = 0; i < nitems(repo->packidx_cache); i++) {
647 if (repo->packidx_cache[i] == NULL)
648 break;
649 got_packidx_close(repo->packidx_cache[i]);
652 for (i = 0; i < nitems(repo->packs); i++) {
653 if (repo->packs[i].path_packfile == NULL)
654 break;
655 got_pack_close(&repo->packs[i]);
658 free(repo->path);
659 free(repo->path_git_dir);
661 got_object_cache_close(&repo->objcache);
662 got_object_cache_close(&repo->treecache);
663 got_object_cache_close(&repo->commitcache);
664 got_object_cache_close(&repo->tagcache);
666 for (i = 0; i < nitems(repo->privsep_children); i++) {
667 if (repo->privsep_children[i].imsg_fd == -1)
668 continue;
669 imsg_clear(repo->privsep_children[i].ibuf);
670 free(repo->privsep_children[i].ibuf);
671 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
672 child_err = got_privsep_wait_for_child(
673 repo->privsep_children[i].pid);
674 if (child_err && err == NULL)
675 err = child_err;
676 if (close(repo->privsep_children[i].imsg_fd) != 0 &&
677 err == NULL)
678 err = got_error_from_errno("close");
681 if (repo->gotconfig)
682 got_gotconfig_free(repo->gotconfig);
683 free(repo->gitconfig_author_name);
684 free(repo->gitconfig_author_email);
685 for (i = 0; i < repo->ngitconfig_remotes; i++) {
686 free(repo->gitconfig_remotes[i].name);
687 free(repo->gitconfig_remotes[i].url);
689 free(repo->gitconfig_remotes);
690 free(repo);
692 return err;
695 const struct got_error *
696 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
697 const char *input_path, int check_disk)
699 const struct got_error *err = NULL;
700 const char *repo_abspath = NULL;
701 size_t repolen, len;
702 char *canonpath, *path = NULL;
704 *in_repo_path = NULL;
706 canonpath = strdup(input_path);
707 if (canonpath == NULL) {
708 err = got_error_from_errno("strdup");
709 goto done;
711 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
712 if (err)
713 goto done;
715 repo_abspath = got_repo_get_path(repo);
717 if (!check_disk || canonpath[0] == '\0') {
718 path = strdup(canonpath);
719 if (path == NULL) {
720 err = got_error_from_errno("strdup");
721 goto done;
723 } else {
724 path = realpath(canonpath, NULL);
725 if (path == NULL) {
726 if (errno != ENOENT) {
727 err = got_error_from_errno2("realpath",
728 canonpath);
729 goto done;
731 /*
732 * Path is not on disk.
733 * Assume it is already relative to repository root.
734 */
735 path = strdup(canonpath);
736 if (path == NULL) {
737 err = got_error_from_errno("strdup");
738 goto done;
742 repolen = strlen(repo_abspath);
743 len = strlen(path);
746 if (strcmp(path, repo_abspath) == 0) {
747 free(path);
748 path = strdup("");
749 if (path == NULL) {
750 err = got_error_from_errno("strdup");
751 goto done;
753 } else if (len > repolen &&
754 got_path_is_child(path, repo_abspath, repolen)) {
755 /* Matched an on-disk path inside repository. */
756 if (got_repo_is_bare(repo)) {
757 /*
758 * Matched an on-disk path inside repository
759 * database. Treat as repository-relative.
760 */
761 } else {
762 char *child;
763 /* Strip common prefix with repository path. */
764 err = got_path_skip_common_ancestor(&child,
765 repo_abspath, path);
766 if (err)
767 goto done;
768 free(path);
769 path = child;
771 } else {
772 /*
773 * Matched unrelated on-disk path.
774 * Treat it as repository-relative.
775 */
779 /* Make in-repository path absolute */
780 if (path[0] != '/') {
781 char *abspath;
782 if (asprintf(&abspath, "/%s", path) == -1) {
783 err = got_error_from_errno("asprintf");
784 goto done;
786 free(path);
787 path = abspath;
790 done:
791 free(canonpath);
792 if (err)
793 free(path);
794 else
795 *in_repo_path = path;
796 return err;
799 static const struct got_error *
800 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
801 const char *path_packidx)
803 const struct got_error *err = NULL;
804 int i;
806 for (i = 0; i < nitems(repo->packidx_cache); i++) {
807 if (repo->packidx_cache[i] == NULL)
808 break;
809 if (strcmp(repo->packidx_cache[i]->path_packidx,
810 path_packidx) == 0) {
811 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
814 if (i == nitems(repo->packidx_cache)) {
815 err = got_packidx_close(repo->packidx_cache[i - 1]);
816 if (err)
817 return err;
820 /*
821 * Insert the new pack index at the front so it will
822 * be searched first in the future.
823 */
824 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
825 sizeof(repo->packidx_cache) -
826 sizeof(repo->packidx_cache[0]));
827 repo->packidx_cache[0] = packidx;
829 return NULL;
832 static int
833 is_packidx_filename(const char *name, size_t len)
835 if (len != GOT_PACKIDX_NAMELEN)
836 return 0;
838 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
839 return 0;
841 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
842 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
843 return 0;
845 return 1;
848 const struct got_error *
849 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
850 struct got_repository *repo, struct got_object_id *id)
852 const struct got_error *err;
853 char *path_packdir;
854 DIR *packdir;
855 struct dirent *dent;
856 char *path_packidx;
857 int i;
859 /* Search pack index cache. */
860 for (i = 0; i < nitems(repo->packidx_cache); i++) {
861 if (repo->packidx_cache[i] == NULL)
862 break;
863 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
864 if (*idx != -1) {
865 *packidx = repo->packidx_cache[i];
866 /*
867 * Move this cache entry to the front. Repeatedly
868 * searching a wrong pack index can be expensive.
869 */
870 if (i > 0) {
871 struct got_packidx *p;
872 p = repo->packidx_cache[0];
873 repo->packidx_cache[0] = *packidx;
874 repo->packidx_cache[i] = p;
876 return NULL;
879 /* No luck. Search the filesystem. */
881 path_packdir = got_repo_get_path_objects_pack(repo);
882 if (path_packdir == NULL)
883 return got_error_from_errno("got_repo_get_path_objects_pack");
885 packdir = opendir(path_packdir);
886 if (packdir == NULL) {
887 if (errno == ENOENT)
888 err = got_error_no_obj(id);
889 else
890 err = got_error_from_errno2("opendir", path_packdir);
891 goto done;
894 while ((dent = readdir(packdir)) != NULL) {
895 int is_cached = 0;
897 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
898 continue;
900 if (asprintf(&path_packidx, "%s/%s", path_packdir,
901 dent->d_name) == -1) {
902 err = got_error_from_errno("asprintf");
903 goto done;
906 for (i = 0; i < nitems(repo->packidx_cache); i++) {
907 if (repo->packidx_cache[i] == NULL)
908 break;
909 if (strcmp(repo->packidx_cache[i]->path_packidx,
910 path_packidx) == 0) {
911 is_cached = 1;
912 break;
915 if (is_cached) {
916 free(path_packidx);
917 continue; /* already searched */
920 err = got_packidx_open(packidx, path_packidx, 0);
921 if (err) {
922 free(path_packidx);
923 goto done;
926 err = cache_packidx(repo, *packidx, path_packidx);
927 free(path_packidx);
928 if (err)
929 goto done;
931 *idx = got_packidx_get_object_idx(*packidx, id);
932 if (*idx != -1) {
933 err = NULL; /* found the object */
934 goto done;
938 err = got_error_no_obj(id);
939 done:
940 free(path_packdir);
941 if (packdir && closedir(packdir) != 0 && err == NULL)
942 err = got_error_from_errno("closedir");
943 return err;
946 static const struct got_error *
947 read_packfile_hdr(int fd, struct got_packidx *packidx)
949 const struct got_error *err = NULL;
950 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
951 struct got_packfile_hdr hdr;
952 ssize_t n;
954 n = read(fd, &hdr, sizeof(hdr));
955 if (n < 0)
956 return got_error_from_errno("read");
957 if (n != sizeof(hdr))
958 return got_error(GOT_ERR_BAD_PACKFILE);
960 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
961 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
962 be32toh(hdr.nobjects) != totobj)
963 err = got_error(GOT_ERR_BAD_PACKFILE);
965 return err;
968 static const struct got_error *
969 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
971 const struct got_error *err = NULL;
973 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
974 if (*fd == -1)
975 return got_error_from_errno2("open", path_packfile);
977 if (packidx) {
978 err = read_packfile_hdr(*fd, packidx);
979 if (err) {
980 close(*fd);
981 *fd = -1;
985 return err;
988 const struct got_error *
989 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
990 const char *path_packfile, struct got_packidx *packidx)
992 const struct got_error *err = NULL;
993 struct got_pack *pack = NULL;
994 struct stat sb;
995 int i;
997 if (packp)
998 *packp = NULL;
1000 for (i = 0; i < nitems(repo->packs); i++) {
1001 pack = &repo->packs[i];
1002 if (pack->path_packfile == NULL)
1003 break;
1004 if (strcmp(pack->path_packfile, path_packfile) == 0)
1005 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1008 if (i == nitems(repo->packs) - 1) {
1009 err = got_pack_close(&repo->packs[i - 1]);
1010 if (err)
1011 return err;
1012 memmove(&repo->packs[1], &repo->packs[0],
1013 sizeof(repo->packs) - sizeof(repo->packs[0]));
1014 i = 0;
1017 pack = &repo->packs[i];
1019 pack->path_packfile = strdup(path_packfile);
1020 if (pack->path_packfile == NULL) {
1021 err = got_error_from_errno("strdup");
1022 goto done;
1025 err = open_packfile(&pack->fd, path_packfile, packidx);
1026 if (err)
1027 goto done;
1029 if (fstat(pack->fd, &sb) != 0) {
1030 err = got_error_from_errno("fstat");
1031 goto done;
1033 pack->filesize = sb.st_size;
1035 pack->privsep_child = NULL;
1037 #ifndef GOT_PACK_NO_MMAP
1038 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1039 pack->fd, 0);
1040 if (pack->map == MAP_FAILED) {
1041 if (errno != ENOMEM) {
1042 err = got_error_from_errno("mmap");
1043 goto done;
1045 pack->map = NULL; /* fall back to read(2) */
1047 #endif
1048 done:
1049 if (err) {
1050 if (pack) {
1051 free(pack->path_packfile);
1052 memset(pack, 0, sizeof(*pack));
1054 } else if (packp)
1055 *packp = pack;
1056 return err;
1059 struct got_pack *
1060 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1062 struct got_pack *pack = NULL;
1063 int i;
1065 for (i = 0; i < nitems(repo->packs); i++) {
1066 pack = &repo->packs[i];
1067 if (pack->path_packfile == NULL)
1068 break;
1069 if (strcmp(pack->path_packfile, path_packfile) == 0)
1070 return pack;
1073 return NULL;
1076 const struct got_error *
1077 got_repo_init(const char *repo_path)
1079 const struct got_error *err = NULL;
1080 const char *dirnames[] = {
1081 GOT_OBJECTS_DIR,
1082 GOT_OBJECTS_PACK_DIR,
1083 GOT_REFS_DIR,
1085 const char *description_str = "Unnamed repository; "
1086 "edit this file 'description' to name the repository.";
1087 const char *headref_str = "ref: refs/heads/main";
1088 const char *gitconfig_str = "[core]\n"
1089 "\trepositoryformatversion = 0\n"
1090 "\tfilemode = true\n"
1091 "\tbare = true\n";
1092 char *path;
1093 int i;
1095 if (!got_path_dir_is_empty(repo_path))
1096 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1098 for (i = 0; i < nitems(dirnames); i++) {
1099 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1100 return got_error_from_errno("asprintf");
1102 err = got_path_mkdir(path);
1103 free(path);
1104 if (err)
1105 return err;
1108 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1109 return got_error_from_errno("asprintf");
1110 err = got_path_create_file(path, description_str);
1111 free(path);
1112 if (err)
1113 return err;
1115 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1116 return got_error_from_errno("asprintf");
1117 err = got_path_create_file(path, headref_str);
1118 free(path);
1119 if (err)
1120 return err;
1122 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1123 return got_error_from_errno("asprintf");
1124 err = got_path_create_file(path, gitconfig_str);
1125 free(path);
1126 if (err)
1127 return err;
1129 return NULL;
1132 static const struct got_error *
1133 match_packed_object(struct got_object_id **unique_id,
1134 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1136 const struct got_error *err = NULL;
1137 char *path_packdir;
1138 DIR *packdir;
1139 struct dirent *dent;
1140 char *path_packidx;
1141 struct got_object_id_queue matched_ids;
1143 SIMPLEQ_INIT(&matched_ids);
1145 path_packdir = got_repo_get_path_objects_pack(repo);
1146 if (path_packdir == NULL)
1147 return got_error_from_errno("got_repo_get_path_objects_pack");
1149 packdir = opendir(path_packdir);
1150 if (packdir == NULL) {
1151 if (errno != ENOENT)
1152 err = got_error_from_errno2("opendir", path_packdir);
1153 goto done;
1156 while ((dent = readdir(packdir)) != NULL) {
1157 struct got_packidx *packidx;
1158 struct got_object_qid *qid;
1161 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1162 continue;
1164 if (asprintf(&path_packidx, "%s/%s", path_packdir,
1165 dent->d_name) == -1) {
1166 err = got_error_from_errno("asprintf");
1167 break;
1170 err = got_packidx_open(&packidx, path_packidx, 0);
1171 free(path_packidx);
1172 if (err)
1173 break;
1175 err = got_packidx_match_id_str_prefix(&matched_ids,
1176 packidx, id_str_prefix);
1177 if (err) {
1178 got_packidx_close(packidx);
1179 break;
1181 err = got_packidx_close(packidx);
1182 if (err)
1183 break;
1185 SIMPLEQ_FOREACH(qid, &matched_ids, entry) {
1186 if (obj_type != GOT_OBJ_TYPE_ANY) {
1187 int matched_type;
1188 err = got_object_get_type(&matched_type, repo,
1189 qid->id);
1190 if (err)
1191 goto done;
1192 if (matched_type != obj_type)
1193 continue;
1195 if (*unique_id == NULL) {
1196 *unique_id = got_object_id_dup(qid->id);
1197 if (*unique_id == NULL) {
1198 err = got_error_from_errno("malloc");
1199 goto done;
1201 } else {
1202 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1203 continue; /* packed multiple times */
1204 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1205 goto done;
1209 done:
1210 got_object_id_queue_free(&matched_ids);
1211 free(path_packdir);
1212 if (packdir && closedir(packdir) != 0 && err == NULL)
1213 err = got_error_from_errno("closedir");
1214 if (err) {
1215 free(*unique_id);
1216 *unique_id = NULL;
1218 return err;
1221 static const struct got_error *
1222 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1223 const char *object_dir, const char *id_str_prefix, int obj_type,
1224 struct got_repository *repo)
1226 const struct got_error *err = NULL;
1227 char *path;
1228 DIR *dir = NULL;
1229 struct dirent *dent;
1230 struct got_object_id id;
1232 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1233 err = got_error_from_errno("asprintf");
1234 goto done;
1237 dir = opendir(path);
1238 if (dir == NULL) {
1239 if (errno == ENOENT) {
1240 err = NULL;
1241 goto done;
1243 err = got_error_from_errno2("opendir", path);
1244 goto done;
1246 while ((dent = readdir(dir)) != NULL) {
1247 char *id_str;
1248 int cmp;
1250 if (strcmp(dent->d_name, ".") == 0 ||
1251 strcmp(dent->d_name, "..") == 0)
1252 continue;
1254 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1255 err = got_error_from_errno("asprintf");
1256 goto done;
1259 if (!got_parse_sha1_digest(id.sha1, id_str))
1260 continue;
1263 * Directory entries do not necessarily appear in
1264 * sorted order, so we must iterate over all of them.
1266 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1267 if (cmp != 0) {
1268 free(id_str);
1269 continue;
1272 if (*unique_id == NULL) {
1273 if (obj_type != GOT_OBJ_TYPE_ANY) {
1274 int matched_type;
1275 err = got_object_get_type(&matched_type, repo,
1276 &id);
1277 if (err)
1278 goto done;
1279 if (matched_type != obj_type)
1280 continue;
1282 *unique_id = got_object_id_dup(&id);
1283 if (*unique_id == NULL) {
1284 err = got_error_from_errno("got_object_id_dup");
1285 free(id_str);
1286 goto done;
1288 } else {
1289 if (got_object_id_cmp(*unique_id, &id) == 0)
1290 continue; /* both packed and loose */
1291 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1292 free(id_str);
1293 goto done;
1296 done:
1297 if (dir && closedir(dir) != 0 && err == NULL)
1298 err = got_error_from_errno("closedir");
1299 if (err) {
1300 free(*unique_id);
1301 *unique_id = NULL;
1303 free(path);
1304 return err;
1307 const struct got_error *
1308 got_repo_match_object_id_prefix(struct got_object_id **id,
1309 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1311 const struct got_error *err = NULL;
1312 char *path_objects = got_repo_get_path_objects(repo);
1313 char *object_dir = NULL;
1314 size_t len;
1315 int i;
1317 *id = NULL;
1319 for (i = 0; i < strlen(id_str_prefix); i++) {
1320 if (isxdigit((unsigned char)id_str_prefix[i]))
1321 continue;
1322 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1325 len = strlen(id_str_prefix);
1326 if (len >= 2) {
1327 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1328 if (err)
1329 goto done;
1330 object_dir = strndup(id_str_prefix, 2);
1331 if (object_dir == NULL) {
1332 err = got_error_from_errno("strdup");
1333 goto done;
1335 err = match_loose_object(id, path_objects, object_dir,
1336 id_str_prefix, obj_type, repo);
1337 } else if (len == 1) {
1338 int i;
1339 for (i = 0; i < 0xf; i++) {
1340 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1341 == -1) {
1342 err = got_error_from_errno("asprintf");
1343 goto done;
1345 err = match_packed_object(id, repo, object_dir,
1346 obj_type);
1347 if (err)
1348 goto done;
1349 err = match_loose_object(id, path_objects, object_dir,
1350 id_str_prefix, obj_type, repo);
1351 if (err)
1352 goto done;
1354 } else {
1355 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1356 goto done;
1358 done:
1359 free(object_dir);
1360 if (err) {
1361 free(*id);
1362 *id = NULL;
1363 } else if (*id == NULL)
1364 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1366 return err;
1369 const struct got_error *
1370 got_repo_match_object_id(struct got_object_id **id, char **label,
1371 const char *id_str, int obj_type, int resolve_tags,
1372 struct got_repository *repo)
1374 const struct got_error *err;
1375 struct got_tag_object *tag;
1376 struct got_reference *ref = NULL;
1378 *id = NULL;
1379 if (label)
1380 *label = NULL;
1382 if (resolve_tags) {
1383 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
1384 repo);
1385 if (err == NULL) {
1386 *id = got_object_id_dup(
1387 got_object_tag_get_object_id(tag));
1388 if (*id == NULL)
1389 err = got_error_from_errno("got_object_id_dup");
1390 else if (label && asprintf(label, "refs/tags/%s",
1391 got_object_tag_get_name(tag)) == -1) {
1392 err = got_error_from_errno("asprintf");
1393 free(*id);
1394 *id = NULL;
1396 got_object_tag_close(tag);
1397 return err;
1398 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1399 err->code != GOT_ERR_NO_OBJ)
1400 return err;
1403 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1404 if (err) {
1405 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1406 return err;
1407 err = got_ref_open(&ref, repo, id_str, 0);
1408 if (err != NULL)
1409 goto done;
1410 if (label) {
1411 *label = strdup(got_ref_get_name(ref));
1412 if (*label == NULL) {
1413 err = got_error_from_errno("strdup");
1414 goto done;
1417 err = got_ref_resolve(id, repo, ref);
1418 } else if (label) {
1419 err = got_object_id_str(label, *id);
1420 if (*label == NULL) {
1421 err = got_error_from_errno("strdup");
1422 goto done;
1425 done:
1426 if (ref)
1427 got_ref_close(ref);
1428 return err;
1431 const struct got_error *
1432 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1433 int obj_type, struct got_repository *repo)
1435 const struct got_error *err;
1436 struct got_reflist_head refs;
1437 struct got_reflist_entry *re;
1438 struct got_object_id *tag_id;
1440 SIMPLEQ_INIT(&refs);
1441 *tag = NULL;
1443 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_by_name, NULL);
1444 if (err)
1445 return err;
1447 SIMPLEQ_FOREACH(re, &refs, entry) {
1448 const char *refname;
1449 refname = got_ref_get_name(re->ref);
1450 if (got_ref_is_symbolic(re->ref))
1451 continue;
1452 refname += strlen("refs/tags/");
1453 if (strcmp(refname, name) != 0)
1454 continue;
1455 err = got_ref_resolve(&tag_id, repo, re->ref);
1456 if (err)
1457 break;
1458 err = got_object_open_as_tag(tag, repo, tag_id);
1459 free(tag_id);
1460 if (err)
1461 break;
1462 if (obj_type == GOT_OBJ_TYPE_ANY ||
1463 got_object_tag_get_object_type(*tag) == obj_type)
1464 break;
1465 got_object_tag_close(*tag);
1466 *tag = NULL;
1469 got_ref_list_free(&refs);
1470 if (err == NULL && *tag == NULL)
1471 err = got_error_path(name, GOT_ERR_NO_OBJ);
1472 return err;
1475 static const struct got_error *
1476 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1477 const char *name, mode_t mode, struct got_object_id *blob_id)
1479 const struct got_error *err = NULL;
1481 *new_te = NULL;
1483 *new_te = calloc(1, sizeof(**new_te));
1484 if (*new_te == NULL)
1485 return got_error_from_errno("calloc");
1487 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1488 sizeof((*new_te)->name)) {
1489 err = got_error(GOT_ERR_NO_SPACE);
1490 goto done;
1493 if (S_ISLNK(mode)) {
1494 (*new_te)->mode = S_IFLNK;
1495 } else {
1496 (*new_te)->mode = S_IFREG;
1497 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1499 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1500 done:
1501 if (err && *new_te) {
1502 free(*new_te);
1503 *new_te = NULL;
1505 return err;
1508 static const struct got_error *
1509 import_file(struct got_tree_entry **new_te, struct dirent *de,
1510 const char *path, struct got_repository *repo)
1512 const struct got_error *err;
1513 struct got_object_id *blob_id = NULL;
1514 char *filepath;
1515 struct stat sb;
1517 if (asprintf(&filepath, "%s%s%s", path,
1518 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1519 return got_error_from_errno("asprintf");
1521 if (lstat(filepath, &sb) != 0) {
1522 err = got_error_from_errno2("lstat", path);
1523 goto done;
1526 err = got_object_blob_create(&blob_id, filepath, repo);
1527 if (err)
1528 goto done;
1530 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1531 blob_id);
1532 done:
1533 free(filepath);
1534 if (err)
1535 free(blob_id);
1536 return err;
1539 static const struct got_error *
1540 insert_tree_entry(struct got_tree_entry *new_te,
1541 struct got_pathlist_head *paths)
1543 const struct got_error *err = NULL;
1544 struct got_pathlist_entry *new_pe;
1546 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1547 if (err)
1548 return err;
1549 if (new_pe == NULL)
1550 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1551 return NULL;
1554 static const struct got_error *write_tree(struct got_object_id **,
1555 const char *, struct got_pathlist_head *, struct got_repository *,
1556 got_repo_import_cb progress_cb, void *progress_arg);
1558 static const struct got_error *
1559 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1560 const char *path, struct got_pathlist_head *ignores,
1561 struct got_repository *repo,
1562 got_repo_import_cb progress_cb, void *progress_arg)
1564 const struct got_error *err;
1565 struct got_object_id *id = NULL;
1566 char *subdirpath;
1568 if (asprintf(&subdirpath, "%s%s%s", path,
1569 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1570 return got_error_from_errno("asprintf");
1572 (*new_te) = calloc(1, sizeof(**new_te));
1573 if (*new_te == NULL)
1574 return got_error_from_errno("calloc");
1575 (*new_te)->mode = S_IFDIR;
1576 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1577 sizeof((*new_te)->name)) {
1578 err = got_error(GOT_ERR_NO_SPACE);
1579 goto done;
1581 err = write_tree(&id, subdirpath, ignores, repo,
1582 progress_cb, progress_arg);
1583 if (err)
1584 goto done;
1585 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1587 done:
1588 free(id);
1589 free(subdirpath);
1590 if (err) {
1591 free(*new_te);
1592 *new_te = NULL;
1594 return err;
1597 static const struct got_error *
1598 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1599 struct got_pathlist_head *ignores, struct got_repository *repo,
1600 got_repo_import_cb progress_cb, void *progress_arg)
1602 const struct got_error *err = NULL;
1603 DIR *dir;
1604 struct dirent *de;
1605 int nentries;
1606 struct got_tree_entry *new_te = NULL;
1607 struct got_pathlist_head paths;
1608 struct got_pathlist_entry *pe;
1610 *new_tree_id = NULL;
1612 TAILQ_INIT(&paths);
1614 dir = opendir(path_dir);
1615 if (dir == NULL) {
1616 err = got_error_from_errno2("opendir", path_dir);
1617 goto done;
1620 nentries = 0;
1621 while ((de = readdir(dir)) != NULL) {
1622 int ignore = 0;
1623 int type;
1625 if (strcmp(de->d_name, ".") == 0 ||
1626 strcmp(de->d_name, "..") == 0)
1627 continue;
1629 TAILQ_FOREACH(pe, ignores, entry) {
1630 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1631 ignore = 1;
1632 break;
1635 if (ignore)
1636 continue;
1638 err = got_path_dirent_type(&type, path_dir, de);
1639 if (err)
1640 goto done;
1642 if (type == DT_DIR) {
1643 err = import_subdir(&new_te, de, path_dir,
1644 ignores, repo, progress_cb, progress_arg);
1645 if (err) {
1646 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1647 goto done;
1648 err = NULL;
1649 continue;
1651 } else if (type == DT_REG || type == DT_LNK) {
1652 err = import_file(&new_te, de, path_dir, repo);
1653 if (err)
1654 goto done;
1655 } else
1656 continue;
1658 err = insert_tree_entry(new_te, &paths);
1659 if (err)
1660 goto done;
1661 nentries++;
1664 if (TAILQ_EMPTY(&paths)) {
1665 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
1666 "cannot create tree without any entries");
1667 goto done;
1670 TAILQ_FOREACH(pe, &paths, entry) {
1671 struct got_tree_entry *te = pe->data;
1672 char *path;
1673 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
1674 continue;
1675 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1676 err = got_error_from_errno("asprintf");
1677 goto done;
1679 err = (*progress_cb)(progress_arg, path);
1680 free(path);
1681 if (err)
1682 goto done;
1685 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1686 done:
1687 if (dir)
1688 closedir(dir);
1689 got_pathlist_free(&paths);
1690 return err;
1693 const struct got_error *
1694 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1695 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1696 struct got_repository *repo, got_repo_import_cb progress_cb,
1697 void *progress_arg)
1699 const struct got_error *err;
1700 struct got_object_id *new_tree_id;
1702 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1703 progress_cb, progress_arg);
1704 if (err)
1705 return err;
1707 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1708 author, time(NULL), author, time(NULL), logmsg, repo);
1709 free(new_tree_id);
1710 return err;