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,
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 *gitconfig_author_name = NULL;
391 *gitconfig_author_email = NULL;
392 if (remotes)
393 *remotes = NULL;
394 if (nremotes)
395 *nremotes = 0;
396 if (gitconfig_owner)
397 *gitconfig_owner = NULL;
399 fd = open(gitconfig_path, O_RDONLY);
400 if (fd == -1) {
401 if (errno == ENOENT)
402 return NULL;
403 return got_error_from_errno2("open", gitconfig_path);
406 ibuf = calloc(1, sizeof(*ibuf));
407 if (ibuf == NULL) {
408 err = got_error_from_errno("calloc");
409 goto done;
412 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
413 err = got_error_from_errno("socketpair");
414 goto done;
417 pid = fork();
418 if (pid == -1) {
419 err = got_error_from_errno("fork");
420 goto done;
421 } else if (pid == 0) {
422 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
423 gitconfig_path);
424 /* not reached */
427 if (close(imsg_fds[1]) == -1) {
428 err = got_error_from_errno("close");
429 goto done;
431 imsg_fds[1] = -1;
432 imsg_init(ibuf, imsg_fds[0]);
434 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
435 if (err)
436 goto done;
437 fd = -1;
439 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
440 if (err)
441 goto done;
443 err = got_privsep_recv_gitconfig_int(
444 gitconfig_repository_format_version, ibuf);
445 if (err)
446 goto done;
448 err = got_privsep_send_gitconfig_author_name_req(ibuf);
449 if (err)
450 goto done;
452 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
453 if (err)
454 goto done;
456 err = got_privsep_send_gitconfig_author_email_req(ibuf);
457 if (err)
458 goto done;
460 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
461 if (err)
462 goto done;
464 if (remotes && nremotes) {
465 err = got_privsep_send_gitconfig_remotes_req(ibuf);
466 if (err)
467 goto done;
469 err = got_privsep_recv_gitconfig_remotes(remotes,
470 nremotes, ibuf);
471 if (err)
472 goto done;
475 if (gitconfig_owner) {
476 err = got_privsep_send_gitconfig_owner_req(ibuf);
477 if (err)
478 goto done;
479 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
480 if (err)
481 goto done;
484 imsg_clear(ibuf);
485 err = got_privsep_send_stop(imsg_fds[0]);
486 child_err = got_privsep_wait_for_child(pid);
487 if (child_err && err == NULL)
488 err = child_err;
489 done:
490 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
491 err = got_error_from_errno("close");
492 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
493 err = got_error_from_errno("close");
494 if (fd != -1 && close(fd) == -1 && err == NULL)
495 err = got_error_from_errno2("close", gitconfig_path);
496 free(ibuf);
497 return err;
500 static const struct got_error *
501 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
503 const struct got_error *err = NULL;
504 char *repo_gitconfig_path = NULL;
506 if (global_gitconfig_path) {
507 /* Read settings from ~/.gitconfig. */
508 int dummy_repo_version;
509 err = parse_gitconfig_file(&dummy_repo_version,
510 &repo->global_gitconfig_author_name,
511 &repo->global_gitconfig_author_email,
512 NULL, NULL, NULL, global_gitconfig_path);
513 if (err)
514 return err;
517 /* Read repository's .git/config file. */
518 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
519 if (repo_gitconfig_path == NULL)
520 return got_error_from_errno("got_repo_get_path_gitconfig");
522 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
523 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
524 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
525 &repo->gitconfig_owner, repo_gitconfig_path);
526 if (err)
527 goto done;
528 done:
529 free(repo_gitconfig_path);
530 return err;
533 static const struct got_error *
534 read_gotconfig(struct got_repository *repo)
536 const struct got_error *err = NULL;
537 char *gotconfig_path;
539 gotconfig_path = got_repo_get_path_gotconfig(repo);
540 if (gotconfig_path == NULL)
541 return got_error_from_errno("got_repo_get_path_gotconfig");
543 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
544 free(gotconfig_path);
545 return err;
548 const struct got_error *
549 got_repo_open(struct got_repository **repop, const char *path,
550 const char *global_gitconfig_path)
552 struct got_repository *repo = NULL;
553 const struct got_error *err = NULL;
554 char *abspath;
555 int i, tried_root = 0;
557 *repop = NULL;
559 if (got_path_is_absolute(path))
560 abspath = strdup(path);
561 else
562 abspath = got_path_get_absolute(path);
563 if (abspath == NULL)
564 return got_error_path(path, GOT_ERR_BAD_PATH);
566 repo = calloc(1, sizeof(*repo));
567 if (repo == NULL) {
568 err = got_error_from_errno("calloc");
569 goto done;
572 for (i = 0; i < nitems(repo->privsep_children); i++) {
573 memset(&repo->privsep_children[i], 0,
574 sizeof(repo->privsep_children[0]));
575 repo->privsep_children[i].imsg_fd = -1;
578 err = got_object_cache_init(&repo->objcache,
579 GOT_OBJECT_CACHE_TYPE_OBJ);
580 if (err)
581 goto done;
582 err = got_object_cache_init(&repo->treecache,
583 GOT_OBJECT_CACHE_TYPE_TREE);
584 if (err)
585 goto done;
586 err = got_object_cache_init(&repo->commitcache,
587 GOT_OBJECT_CACHE_TYPE_COMMIT);
588 if (err)
589 goto done;
590 err = got_object_cache_init(&repo->tagcache,
591 GOT_OBJECT_CACHE_TYPE_TAG);
592 if (err)
593 goto done;
595 path = realpath(abspath, NULL);
596 if (path == NULL) {
597 err = got_error_from_errno2("realpath", abspath);
598 goto done;
601 do {
602 err = open_repo(repo, path);
603 if (err == NULL)
604 break;
605 if (err->code != GOT_ERR_NOT_GIT_REPO)
606 break;
607 if (path[0] == '/' && path[1] == '\0') {
608 if (tried_root) {
609 err = got_error(GOT_ERR_NOT_GIT_REPO);
610 goto done;
612 tried_root = 1;
614 path = dirname(path);
615 if (path == NULL) {
616 err = got_error_from_errno2("dirname", path);
617 goto done;
619 } while (path);
621 err = read_gotconfig(repo);
622 if (err)
623 goto done;
625 err = read_gitconfig(repo, global_gitconfig_path);
626 if (err)
627 goto done;
628 if (repo->gitconfig_repository_format_version != 0)
629 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
630 done:
631 if (err)
632 got_repo_close(repo);
633 else
634 *repop = repo;
635 free(abspath);
636 return err;
639 const struct got_error *
640 got_repo_close(struct got_repository *repo)
642 const struct got_error *err = NULL, *child_err;
643 int i;
645 for (i = 0; i < nitems(repo->packidx_cache); i++) {
646 if (repo->packidx_cache[i] == NULL)
647 break;
648 got_packidx_close(repo->packidx_cache[i]);
651 for (i = 0; i < nitems(repo->packs); i++) {
652 if (repo->packs[i].path_packfile == NULL)
653 break;
654 got_pack_close(&repo->packs[i]);
657 free(repo->path);
658 free(repo->path_git_dir);
660 got_object_cache_close(&repo->objcache);
661 got_object_cache_close(&repo->treecache);
662 got_object_cache_close(&repo->commitcache);
663 got_object_cache_close(&repo->tagcache);
665 for (i = 0; i < nitems(repo->privsep_children); i++) {
666 if (repo->privsep_children[i].imsg_fd == -1)
667 continue;
668 imsg_clear(repo->privsep_children[i].ibuf);
669 free(repo->privsep_children[i].ibuf);
670 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
671 child_err = got_privsep_wait_for_child(
672 repo->privsep_children[i].pid);
673 if (child_err && err == NULL)
674 err = child_err;
675 if (close(repo->privsep_children[i].imsg_fd) != 0 &&
676 err == NULL)
677 err = got_error_from_errno("close");
680 if (repo->gotconfig)
681 got_gotconfig_free(repo->gotconfig);
682 free(repo->gitconfig_author_name);
683 free(repo->gitconfig_author_email);
684 for (i = 0; i < repo->ngitconfig_remotes; i++) {
685 free(repo->gitconfig_remotes[i].name);
686 free(repo->gitconfig_remotes[i].url);
688 free(repo->gitconfig_remotes);
689 free(repo);
691 return err;
694 const struct got_error *
695 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
696 const char *input_path, int check_disk)
698 const struct got_error *err = NULL;
699 const char *repo_abspath = NULL;
700 size_t repolen, len;
701 char *canonpath, *path = NULL;
703 *in_repo_path = NULL;
705 canonpath = strdup(input_path);
706 if (canonpath == NULL) {
707 err = got_error_from_errno("strdup");
708 goto done;
710 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
711 if (err)
712 goto done;
714 repo_abspath = got_repo_get_path(repo);
716 if (!check_disk || canonpath[0] == '\0') {
717 path = strdup(canonpath);
718 if (path == NULL) {
719 err = got_error_from_errno("strdup");
720 goto done;
722 } else {
723 path = realpath(canonpath, NULL);
724 if (path == NULL) {
725 if (errno != ENOENT) {
726 err = got_error_from_errno2("realpath",
727 canonpath);
728 goto done;
730 /*
731 * Path is not on disk.
732 * Assume it is already relative to repository root.
733 */
734 path = strdup(canonpath);
735 if (path == NULL) {
736 err = got_error_from_errno("strdup");
737 goto done;
741 repolen = strlen(repo_abspath);
742 len = strlen(path);
745 if (strcmp(path, repo_abspath) == 0) {
746 free(path);
747 path = strdup("");
748 if (path == NULL) {
749 err = got_error_from_errno("strdup");
750 goto done;
752 } else if (len > repolen &&
753 got_path_is_child(path, repo_abspath, repolen)) {
754 /* Matched an on-disk path inside repository. */
755 if (got_repo_is_bare(repo)) {
756 /*
757 * Matched an on-disk path inside repository
758 * database. Treat as repository-relative.
759 */
760 } else {
761 char *child;
762 /* Strip common prefix with repository path. */
763 err = got_path_skip_common_ancestor(&child,
764 repo_abspath, path);
765 if (err)
766 goto done;
767 free(path);
768 path = child;
770 } else {
771 /*
772 * Matched unrelated on-disk path.
773 * Treat it as repository-relative.
774 */
778 /* Make in-repository path absolute */
779 if (path[0] != '/') {
780 char *abspath;
781 if (asprintf(&abspath, "/%s", path) == -1) {
782 err = got_error_from_errno("asprintf");
783 goto done;
785 free(path);
786 path = abspath;
789 done:
790 free(canonpath);
791 if (err)
792 free(path);
793 else
794 *in_repo_path = path;
795 return err;
798 static const struct got_error *
799 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
800 const char *path_packidx)
802 const struct got_error *err = NULL;
803 int i;
805 for (i = 0; i < nitems(repo->packidx_cache); i++) {
806 if (repo->packidx_cache[i] == NULL)
807 break;
808 if (strcmp(repo->packidx_cache[i]->path_packidx,
809 path_packidx) == 0) {
810 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
813 if (i == nitems(repo->packidx_cache)) {
814 err = got_packidx_close(repo->packidx_cache[i - 1]);
815 if (err)
816 return err;
819 /*
820 * Insert the new pack index at the front so it will
821 * be searched first in the future.
822 */
823 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
824 sizeof(repo->packidx_cache) -
825 sizeof(repo->packidx_cache[0]));
826 repo->packidx_cache[0] = packidx;
828 return NULL;
831 static int
832 is_packidx_filename(const char *name, size_t len)
834 if (len != GOT_PACKIDX_NAMELEN)
835 return 0;
837 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
838 return 0;
840 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
841 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
842 return 0;
844 return 1;
847 const struct got_error *
848 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
849 struct got_repository *repo, struct got_object_id *id)
851 const struct got_error *err;
852 char *path_packdir;
853 DIR *packdir;
854 struct dirent *dent;
855 char *path_packidx;
856 int i;
858 /* Search pack index cache. */
859 for (i = 0; i < nitems(repo->packidx_cache); i++) {
860 if (repo->packidx_cache[i] == NULL)
861 break;
862 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
863 if (*idx != -1) {
864 *packidx = repo->packidx_cache[i];
865 /*
866 * Move this cache entry to the front. Repeatedly
867 * searching a wrong pack index can be expensive.
868 */
869 if (i > 0) {
870 struct got_packidx *p;
871 p = repo->packidx_cache[0];
872 repo->packidx_cache[0] = *packidx;
873 repo->packidx_cache[i] = p;
875 return NULL;
878 /* No luck. Search the filesystem. */
880 path_packdir = got_repo_get_path_objects_pack(repo);
881 if (path_packdir == NULL)
882 return got_error_from_errno("got_repo_get_path_objects_pack");
884 packdir = opendir(path_packdir);
885 if (packdir == NULL) {
886 if (errno == ENOENT)
887 err = got_error_no_obj(id);
888 else
889 err = got_error_from_errno2("opendir", path_packdir);
890 goto done;
893 while ((dent = readdir(packdir)) != NULL) {
894 int is_cached = 0;
896 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
897 continue;
899 if (asprintf(&path_packidx, "%s/%s", path_packdir,
900 dent->d_name) == -1) {
901 err = got_error_from_errno("asprintf");
902 goto done;
905 for (i = 0; i < nitems(repo->packidx_cache); i++) {
906 if (repo->packidx_cache[i] == NULL)
907 break;
908 if (strcmp(repo->packidx_cache[i]->path_packidx,
909 path_packidx) == 0) {
910 is_cached = 1;
911 break;
914 if (is_cached) {
915 free(path_packidx);
916 continue; /* already searched */
919 err = got_packidx_open(packidx, path_packidx, 0);
920 if (err) {
921 free(path_packidx);
922 goto done;
925 err = cache_packidx(repo, *packidx, path_packidx);
926 free(path_packidx);
927 if (err)
928 goto done;
930 *idx = got_packidx_get_object_idx(*packidx, id);
931 if (*idx != -1) {
932 err = NULL; /* found the object */
933 goto done;
937 err = got_error_no_obj(id);
938 done:
939 free(path_packdir);
940 if (packdir && closedir(packdir) != 0 && err == NULL)
941 err = got_error_from_errno("closedir");
942 return err;
945 static const struct got_error *
946 read_packfile_hdr(int fd, struct got_packidx *packidx)
948 const struct got_error *err = NULL;
949 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
950 struct got_packfile_hdr hdr;
951 ssize_t n;
953 n = read(fd, &hdr, sizeof(hdr));
954 if (n < 0)
955 return got_error_from_errno("read");
956 if (n != sizeof(hdr))
957 return got_error(GOT_ERR_BAD_PACKFILE);
959 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
960 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
961 be32toh(hdr.nobjects) != totobj)
962 err = got_error(GOT_ERR_BAD_PACKFILE);
964 return err;
967 static const struct got_error *
968 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
970 const struct got_error *err = NULL;
972 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
973 if (*fd == -1)
974 return got_error_from_errno2("open", path_packfile);
976 if (packidx) {
977 err = read_packfile_hdr(*fd, packidx);
978 if (err) {
979 close(*fd);
980 *fd = -1;
984 return err;
987 const struct got_error *
988 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
989 const char *path_packfile, struct got_packidx *packidx)
991 const struct got_error *err = NULL;
992 struct got_pack *pack = NULL;
993 struct stat sb;
994 int i;
996 if (packp)
997 *packp = NULL;
999 for (i = 0; i < nitems(repo->packs); i++) {
1000 pack = &repo->packs[i];
1001 if (pack->path_packfile == NULL)
1002 break;
1003 if (strcmp(pack->path_packfile, path_packfile) == 0)
1004 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1007 if (i == nitems(repo->packs) - 1) {
1008 err = got_pack_close(&repo->packs[i - 1]);
1009 if (err)
1010 return err;
1011 memmove(&repo->packs[1], &repo->packs[0],
1012 sizeof(repo->packs) - sizeof(repo->packs[0]));
1013 i = 0;
1016 pack = &repo->packs[i];
1018 pack->path_packfile = strdup(path_packfile);
1019 if (pack->path_packfile == NULL) {
1020 err = got_error_from_errno("strdup");
1021 goto done;
1024 err = open_packfile(&pack->fd, path_packfile, packidx);
1025 if (err)
1026 goto done;
1028 if (fstat(pack->fd, &sb) != 0) {
1029 err = got_error_from_errno("fstat");
1030 goto done;
1032 pack->filesize = sb.st_size;
1034 pack->privsep_child = NULL;
1036 #ifndef GOT_PACK_NO_MMAP
1037 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1038 pack->fd, 0);
1039 if (pack->map == MAP_FAILED) {
1040 if (errno != ENOMEM) {
1041 err = got_error_from_errno("mmap");
1042 goto done;
1044 pack->map = NULL; /* fall back to read(2) */
1046 #endif
1047 done:
1048 if (err) {
1049 if (pack) {
1050 free(pack->path_packfile);
1051 memset(pack, 0, sizeof(*pack));
1053 } else if (packp)
1054 *packp = pack;
1055 return err;
1058 struct got_pack *
1059 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1061 struct got_pack *pack = NULL;
1062 int i;
1064 for (i = 0; i < nitems(repo->packs); i++) {
1065 pack = &repo->packs[i];
1066 if (pack->path_packfile == NULL)
1067 break;
1068 if (strcmp(pack->path_packfile, path_packfile) == 0)
1069 return pack;
1072 return NULL;
1075 const struct got_error *
1076 got_repo_init(const char *repo_path)
1078 const struct got_error *err = NULL;
1079 const char *dirnames[] = {
1080 GOT_OBJECTS_DIR,
1081 GOT_OBJECTS_PACK_DIR,
1082 GOT_REFS_DIR,
1084 const char *description_str = "Unnamed repository; "
1085 "edit this file 'description' to name the repository.";
1086 const char *headref_str = "ref: refs/heads/main";
1087 const char *gitconfig_str = "[core]\n"
1088 "\trepositoryformatversion = 0\n"
1089 "\tfilemode = true\n"
1090 "\tbare = true\n";
1091 char *path;
1092 int i;
1094 if (!got_path_dir_is_empty(repo_path))
1095 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1097 for (i = 0; i < nitems(dirnames); i++) {
1098 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1099 return got_error_from_errno("asprintf");
1101 err = got_path_mkdir(path);
1102 free(path);
1103 if (err)
1104 return err;
1107 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1108 return got_error_from_errno("asprintf");
1109 err = got_path_create_file(path, description_str);
1110 free(path);
1111 if (err)
1112 return err;
1114 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1115 return got_error_from_errno("asprintf");
1116 err = got_path_create_file(path, headref_str);
1117 free(path);
1118 if (err)
1119 return err;
1121 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1122 return got_error_from_errno("asprintf");
1123 err = got_path_create_file(path, gitconfig_str);
1124 free(path);
1125 if (err)
1126 return err;
1128 return NULL;
1131 static const struct got_error *
1132 match_packed_object(struct got_object_id **unique_id,
1133 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1135 const struct got_error *err = NULL;
1136 char *path_packdir;
1137 DIR *packdir;
1138 struct dirent *dent;
1139 char *path_packidx;
1140 struct got_object_id_queue matched_ids;
1142 SIMPLEQ_INIT(&matched_ids);
1144 path_packdir = got_repo_get_path_objects_pack(repo);
1145 if (path_packdir == NULL)
1146 return got_error_from_errno("got_repo_get_path_objects_pack");
1148 packdir = opendir(path_packdir);
1149 if (packdir == NULL) {
1150 if (errno != ENOENT)
1151 err = got_error_from_errno2("opendir", path_packdir);
1152 goto done;
1155 while ((dent = readdir(packdir)) != NULL) {
1156 struct got_packidx *packidx;
1157 struct got_object_qid *qid;
1160 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1161 continue;
1163 if (asprintf(&path_packidx, "%s/%s", path_packdir,
1164 dent->d_name) == -1) {
1165 err = got_error_from_errno("asprintf");
1166 break;
1169 err = got_packidx_open(&packidx, path_packidx, 0);
1170 free(path_packidx);
1171 if (err)
1172 break;
1174 err = got_packidx_match_id_str_prefix(&matched_ids,
1175 packidx, id_str_prefix);
1176 if (err) {
1177 got_packidx_close(packidx);
1178 break;
1180 err = got_packidx_close(packidx);
1181 if (err)
1182 break;
1184 SIMPLEQ_FOREACH(qid, &matched_ids, entry) {
1185 if (obj_type != GOT_OBJ_TYPE_ANY) {
1186 int matched_type;
1187 err = got_object_get_type(&matched_type, repo,
1188 qid->id);
1189 if (err)
1190 goto done;
1191 if (matched_type != obj_type)
1192 continue;
1194 if (*unique_id == NULL) {
1195 *unique_id = got_object_id_dup(qid->id);
1196 if (*unique_id == NULL) {
1197 err = got_error_from_errno("malloc");
1198 goto done;
1200 } else {
1201 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1202 continue; /* packed multiple times */
1203 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1204 goto done;
1208 done:
1209 got_object_id_queue_free(&matched_ids);
1210 free(path_packdir);
1211 if (packdir && closedir(packdir) != 0 && err == NULL)
1212 err = got_error_from_errno("closedir");
1213 if (err) {
1214 free(*unique_id);
1215 *unique_id = NULL;
1217 return err;
1220 static const struct got_error *
1221 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1222 const char *object_dir, const char *id_str_prefix, int obj_type,
1223 struct got_repository *repo)
1225 const struct got_error *err = NULL;
1226 char *path;
1227 DIR *dir = NULL;
1228 struct dirent *dent;
1229 struct got_object_id id;
1231 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1232 err = got_error_from_errno("asprintf");
1233 goto done;
1236 dir = opendir(path);
1237 if (dir == NULL) {
1238 if (errno == ENOENT) {
1239 err = NULL;
1240 goto done;
1242 err = got_error_from_errno2("opendir", path);
1243 goto done;
1245 while ((dent = readdir(dir)) != NULL) {
1246 char *id_str;
1247 int cmp;
1249 if (strcmp(dent->d_name, ".") == 0 ||
1250 strcmp(dent->d_name, "..") == 0)
1251 continue;
1253 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1254 err = got_error_from_errno("asprintf");
1255 goto done;
1258 if (!got_parse_sha1_digest(id.sha1, id_str))
1259 continue;
1262 * Directory entries do not necessarily appear in
1263 * sorted order, so we must iterate over all of them.
1265 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1266 if (cmp != 0) {
1267 free(id_str);
1268 continue;
1271 if (*unique_id == NULL) {
1272 if (obj_type != GOT_OBJ_TYPE_ANY) {
1273 int matched_type;
1274 err = got_object_get_type(&matched_type, repo,
1275 &id);
1276 if (err)
1277 goto done;
1278 if (matched_type != obj_type)
1279 continue;
1281 *unique_id = got_object_id_dup(&id);
1282 if (*unique_id == NULL) {
1283 err = got_error_from_errno("got_object_id_dup");
1284 free(id_str);
1285 goto done;
1287 } else {
1288 if (got_object_id_cmp(*unique_id, &id) == 0)
1289 continue; /* both packed and loose */
1290 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1291 free(id_str);
1292 goto done;
1295 done:
1296 if (dir && closedir(dir) != 0 && err == NULL)
1297 err = got_error_from_errno("closedir");
1298 if (err) {
1299 free(*unique_id);
1300 *unique_id = NULL;
1302 free(path);
1303 return err;
1306 const struct got_error *
1307 got_repo_match_object_id_prefix(struct got_object_id **id,
1308 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1310 const struct got_error *err = NULL;
1311 char *path_objects = got_repo_get_path_objects(repo);
1312 char *object_dir = NULL;
1313 size_t len;
1314 int i;
1316 *id = NULL;
1318 for (i = 0; i < strlen(id_str_prefix); i++) {
1319 if (isxdigit((unsigned char)id_str_prefix[i]))
1320 continue;
1321 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1324 len = strlen(id_str_prefix);
1325 if (len >= 2) {
1326 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1327 if (err)
1328 goto done;
1329 object_dir = strndup(id_str_prefix, 2);
1330 if (object_dir == NULL) {
1331 err = got_error_from_errno("strdup");
1332 goto done;
1334 err = match_loose_object(id, path_objects, object_dir,
1335 id_str_prefix, obj_type, repo);
1336 } else if (len == 1) {
1337 int i;
1338 for (i = 0; i < 0xf; i++) {
1339 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1340 == -1) {
1341 err = got_error_from_errno("asprintf");
1342 goto done;
1344 err = match_packed_object(id, repo, object_dir,
1345 obj_type);
1346 if (err)
1347 goto done;
1348 err = match_loose_object(id, path_objects, object_dir,
1349 id_str_prefix, obj_type, repo);
1350 if (err)
1351 goto done;
1353 } else {
1354 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1355 goto done;
1357 done:
1358 free(object_dir);
1359 if (err) {
1360 free(*id);
1361 *id = NULL;
1362 } else if (*id == NULL)
1363 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1365 return err;
1368 const struct got_error *
1369 got_repo_match_object_id(struct got_object_id **id, char **label,
1370 const char *id_str, int obj_type, int resolve_tags,
1371 struct got_repository *repo)
1373 const struct got_error *err;
1374 struct got_tag_object *tag;
1375 struct got_reference *ref = NULL;
1377 *id = NULL;
1378 if (label)
1379 *label = NULL;
1381 if (resolve_tags) {
1382 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
1383 repo);
1384 if (err == NULL) {
1385 *id = got_object_id_dup(
1386 got_object_tag_get_object_id(tag));
1387 if (*id == NULL)
1388 err = got_error_from_errno("got_object_id_dup");
1389 else if (label && asprintf(label, "refs/tags/%s",
1390 got_object_tag_get_name(tag)) == -1) {
1391 err = got_error_from_errno("asprintf");
1392 free(*id);
1393 *id = NULL;
1395 got_object_tag_close(tag);
1396 return err;
1397 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1398 err->code != GOT_ERR_NO_OBJ)
1399 return err;
1402 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1403 if (err) {
1404 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1405 return err;
1406 err = got_ref_open(&ref, repo, id_str, 0);
1407 if (err != NULL)
1408 goto done;
1409 if (label) {
1410 *label = strdup(got_ref_get_name(ref));
1411 if (*label == NULL) {
1412 err = got_error_from_errno("strdup");
1413 goto done;
1416 err = got_ref_resolve(id, repo, ref);
1417 } else if (label) {
1418 err = got_object_id_str(label, *id);
1419 if (*label == NULL) {
1420 err = got_error_from_errno("strdup");
1421 goto done;
1424 done:
1425 if (ref)
1426 got_ref_close(ref);
1427 return err;
1430 const struct got_error *
1431 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1432 int obj_type, struct got_repository *repo)
1434 const struct got_error *err;
1435 struct got_reflist_head refs;
1436 struct got_reflist_entry *re;
1437 struct got_object_id *tag_id;
1439 SIMPLEQ_INIT(&refs);
1440 *tag = NULL;
1442 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_by_name, NULL);
1443 if (err)
1444 return err;
1446 SIMPLEQ_FOREACH(re, &refs, entry) {
1447 const char *refname;
1448 refname = got_ref_get_name(re->ref);
1449 if (got_ref_is_symbolic(re->ref))
1450 continue;
1451 refname += strlen("refs/tags/");
1452 if (strcmp(refname, name) != 0)
1453 continue;
1454 err = got_ref_resolve(&tag_id, repo, re->ref);
1455 if (err)
1456 break;
1457 err = got_object_open_as_tag(tag, repo, tag_id);
1458 free(tag_id);
1459 if (err)
1460 break;
1461 if (obj_type == GOT_OBJ_TYPE_ANY ||
1462 got_object_tag_get_object_type(*tag) == obj_type)
1463 break;
1464 got_object_tag_close(*tag);
1465 *tag = NULL;
1468 got_ref_list_free(&refs);
1469 if (err == NULL && *tag == NULL)
1470 err = got_error_path(name, GOT_ERR_NO_OBJ);
1471 return err;
1474 static const struct got_error *
1475 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1476 const char *name, mode_t mode, struct got_object_id *blob_id)
1478 const struct got_error *err = NULL;
1480 *new_te = NULL;
1482 *new_te = calloc(1, sizeof(**new_te));
1483 if (*new_te == NULL)
1484 return got_error_from_errno("calloc");
1486 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1487 sizeof((*new_te)->name)) {
1488 err = got_error(GOT_ERR_NO_SPACE);
1489 goto done;
1492 if (S_ISLNK(mode)) {
1493 (*new_te)->mode = S_IFLNK;
1494 } else {
1495 (*new_te)->mode = S_IFREG;
1496 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1498 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1499 done:
1500 if (err && *new_te) {
1501 free(*new_te);
1502 *new_te = NULL;
1504 return err;
1507 static const struct got_error *
1508 import_file(struct got_tree_entry **new_te, struct dirent *de,
1509 const char *path, struct got_repository *repo)
1511 const struct got_error *err;
1512 struct got_object_id *blob_id = NULL;
1513 char *filepath;
1514 struct stat sb;
1516 if (asprintf(&filepath, "%s%s%s", path,
1517 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1518 return got_error_from_errno("asprintf");
1520 if (lstat(filepath, &sb) != 0) {
1521 err = got_error_from_errno2("lstat", path);
1522 goto done;
1525 err = got_object_blob_create(&blob_id, filepath, repo);
1526 if (err)
1527 goto done;
1529 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1530 blob_id);
1531 done:
1532 free(filepath);
1533 if (err)
1534 free(blob_id);
1535 return err;
1538 static const struct got_error *
1539 insert_tree_entry(struct got_tree_entry *new_te,
1540 struct got_pathlist_head *paths)
1542 const struct got_error *err = NULL;
1543 struct got_pathlist_entry *new_pe;
1545 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1546 if (err)
1547 return err;
1548 if (new_pe == NULL)
1549 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1550 return NULL;
1553 static const struct got_error *write_tree(struct got_object_id **,
1554 const char *, struct got_pathlist_head *, struct got_repository *,
1555 got_repo_import_cb progress_cb, void *progress_arg);
1557 static const struct got_error *
1558 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1559 const char *path, struct got_pathlist_head *ignores,
1560 struct got_repository *repo,
1561 got_repo_import_cb progress_cb, void *progress_arg)
1563 const struct got_error *err;
1564 struct got_object_id *id = NULL;
1565 char *subdirpath;
1567 if (asprintf(&subdirpath, "%s%s%s", path,
1568 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1569 return got_error_from_errno("asprintf");
1571 (*new_te) = calloc(1, sizeof(**new_te));
1572 if (*new_te == NULL)
1573 return got_error_from_errno("calloc");
1574 (*new_te)->mode = S_IFDIR;
1575 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1576 sizeof((*new_te)->name)) {
1577 err = got_error(GOT_ERR_NO_SPACE);
1578 goto done;
1580 err = write_tree(&id, subdirpath, ignores, repo,
1581 progress_cb, progress_arg);
1582 if (err)
1583 goto done;
1584 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1586 done:
1587 free(id);
1588 free(subdirpath);
1589 if (err) {
1590 free(*new_te);
1591 *new_te = NULL;
1593 return err;
1596 static const struct got_error *
1597 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1598 struct got_pathlist_head *ignores, struct got_repository *repo,
1599 got_repo_import_cb progress_cb, void *progress_arg)
1601 const struct got_error *err = NULL;
1602 DIR *dir;
1603 struct dirent *de;
1604 int nentries;
1605 struct got_tree_entry *new_te = NULL;
1606 struct got_pathlist_head paths;
1607 struct got_pathlist_entry *pe;
1609 *new_tree_id = NULL;
1611 TAILQ_INIT(&paths);
1613 dir = opendir(path_dir);
1614 if (dir == NULL) {
1615 err = got_error_from_errno2("opendir", path_dir);
1616 goto done;
1619 nentries = 0;
1620 while ((de = readdir(dir)) != NULL) {
1621 int ignore = 0;
1622 int type;
1624 if (strcmp(de->d_name, ".") == 0 ||
1625 strcmp(de->d_name, "..") == 0)
1626 continue;
1628 TAILQ_FOREACH(pe, ignores, entry) {
1629 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1630 ignore = 1;
1631 break;
1634 if (ignore)
1635 continue;
1637 err = got_path_dirent_type(&type, path_dir, de);
1638 if (err)
1639 goto done;
1641 if (type == DT_DIR) {
1642 err = import_subdir(&new_te, de, path_dir,
1643 ignores, repo, progress_cb, progress_arg);
1644 if (err) {
1645 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1646 goto done;
1647 err = NULL;
1648 continue;
1650 } else if (type == DT_REG || type == DT_LNK) {
1651 err = import_file(&new_te, de, path_dir, repo);
1652 if (err)
1653 goto done;
1654 } else
1655 continue;
1657 err = insert_tree_entry(new_te, &paths);
1658 if (err)
1659 goto done;
1660 nentries++;
1663 if (TAILQ_EMPTY(&paths)) {
1664 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
1665 "cannot create tree without any entries");
1666 goto done;
1669 TAILQ_FOREACH(pe, &paths, entry) {
1670 struct got_tree_entry *te = pe->data;
1671 char *path;
1672 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
1673 continue;
1674 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1675 err = got_error_from_errno("asprintf");
1676 goto done;
1678 err = (*progress_cb)(progress_arg, path);
1679 free(path);
1680 if (err)
1681 goto done;
1684 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1685 done:
1686 if (dir)
1687 closedir(dir);
1688 got_pathlist_free(&paths);
1689 return err;
1692 const struct got_error *
1693 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1694 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1695 struct got_repository *repo, got_repo_import_cb progress_cb,
1696 void *progress_arg)
1698 const struct got_error *err;
1699 struct got_object_id *new_tree_id;
1701 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1702 progress_cb, progress_arg);
1703 if (err)
1704 return err;
1706 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1707 author, time(NULL), author, time(NULL), logmsg, repo);
1708 free(new_tree_id);
1709 return err;