Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/uio.h>
20 #include <sys/socket.h>
21 #include <sys/stat.h>
22 #include <sys/mman.h>
23 #include <sys/syslimits.h>
25 #include <ctype.h>
26 #include <endian.h>
27 #include <fcntl.h>
28 #include <fnmatch.h>
29 #include <limits.h>
30 #include <dirent.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <sha1.h>
34 #include <string.h>
35 #include <time.h>
36 #include <unistd.h>
37 #include <zlib.h>
38 #include <errno.h>
39 #include <libgen.h>
40 #include <stdint.h>
41 #include <imsg.h>
42 #include <uuid.h>
44 #include "got_error.h"
45 #include "got_reference.h"
46 #include "got_repository.h"
47 #include "got_path.h"
48 #include "got_cancel.h"
49 #include "got_worktree.h"
50 #include "got_object.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_inflate.h"
54 #include "got_lib_object.h"
55 #include "got_lib_object_parse.h"
56 #include "got_lib_object_create.h"
57 #include "got_lib_pack.h"
58 #include "got_lib_privsep.h"
59 #include "got_lib_worktree.h"
60 #include "got_lib_sha1.h"
61 #include "got_lib_object_cache.h"
62 #include "got_lib_repository.h"
64 #ifndef nitems
65 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
66 #endif
68 const char *
69 got_repo_get_path(struct got_repository *repo)
70 {
71 return repo->path;
72 }
74 const char *
75 got_repo_get_path_git_dir(struct got_repository *repo)
76 {
77 return repo->path_git_dir;
78 }
80 const char *
81 got_repo_get_gitconfig_author_name(struct got_repository *repo)
82 {
83 return repo->gitconfig_author_name;
84 }
86 const char *
87 got_repo_get_gitconfig_author_email(struct got_repository *repo)
88 {
89 return repo->gitconfig_author_email;
90 }
92 const char *
93 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
94 {
95 return repo->global_gitconfig_author_name;
96 }
98 const char *
99 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
101 return repo->global_gitconfig_author_email;
104 const char *
105 got_repo_get_gitconfig_owner(struct got_repository *repo)
107 return repo->gitconfig_owner;
110 int
111 got_repo_is_bare(struct got_repository *repo)
113 return (strcmp(repo->path, repo->path_git_dir) == 0);
116 static char *
117 get_path_git_child(struct got_repository *repo, const char *basename)
119 char *path_child;
121 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
122 basename) == -1)
123 return NULL;
125 return path_child;
128 char *
129 got_repo_get_path_objects(struct got_repository *repo)
131 return get_path_git_child(repo, GOT_OBJECTS_DIR);
134 char *
135 got_repo_get_path_objects_pack(struct got_repository *repo)
137 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
140 char *
141 got_repo_get_path_refs(struct got_repository *repo)
143 return get_path_git_child(repo, GOT_REFS_DIR);
146 char *
147 got_repo_get_path_packed_refs(struct got_repository *repo)
149 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
152 static char *
153 get_path_head(struct got_repository *repo)
155 return get_path_git_child(repo, GOT_HEAD_FILE);
158 char *
159 got_repo_get_path_gitconfig(struct got_repository *repo)
161 return get_path_git_child(repo, GOT_GITCONFIG);
164 void
165 got_repo_get_gitconfig_remotes(int *nremotes, struct got_remote_repo **remotes,
166 struct got_repository *repo)
168 *nremotes = repo->ngitconfig_remotes;
169 *remotes = repo->gitconfig_remotes;
172 static int
173 is_git_repo(struct got_repository *repo)
175 const char *path_git = got_repo_get_path_git_dir(repo);
176 char *path_objects = got_repo_get_path_objects(repo);
177 char *path_refs = got_repo_get_path_refs(repo);
178 char *path_head = get_path_head(repo);
179 int ret = 0;
180 struct stat sb;
181 struct got_reference *head_ref;
183 if (lstat(path_git, &sb) == -1)
184 goto done;
185 if (!S_ISDIR(sb.st_mode))
186 goto done;
188 if (lstat(path_objects, &sb) == -1)
189 goto done;
190 if (!S_ISDIR(sb.st_mode))
191 goto done;
193 if (lstat(path_refs, &sb) == -1)
194 goto done;
195 if (!S_ISDIR(sb.st_mode))
196 goto done;
198 if (lstat(path_head, &sb) == -1)
199 goto done;
200 if (!S_ISREG(sb.st_mode))
201 goto done;
203 /* Check if the HEAD reference can be opened. */
204 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
205 goto done;
206 got_ref_close(head_ref);
208 ret = 1;
209 done:
210 free(path_objects);
211 free(path_refs);
212 free(path_head);
213 return ret;
217 const struct got_error *
218 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
219 struct got_object *obj)
221 #ifndef GOT_NO_OBJ_CACHE
222 const struct got_error *err = NULL;
223 err = got_object_cache_add(&repo->objcache, id, obj);
224 if (err) {
225 if (err->code == GOT_ERR_OBJ_EXISTS ||
226 err->code == GOT_ERR_OBJ_TOO_LARGE)
227 err = NULL;
228 return err;
230 obj->refcnt++;
231 #endif
232 return NULL;
235 struct got_object *
236 got_repo_get_cached_object(struct got_repository *repo,
237 struct got_object_id *id)
239 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
242 const struct got_error *
243 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
244 struct got_tree_object *tree)
246 #ifndef GOT_NO_OBJ_CACHE
247 const struct got_error *err = NULL;
248 err = got_object_cache_add(&repo->treecache, id, tree);
249 if (err) {
250 if (err->code == GOT_ERR_OBJ_EXISTS ||
251 err->code == GOT_ERR_OBJ_TOO_LARGE)
252 err = NULL;
253 return err;
255 tree->refcnt++;
256 #endif
257 return NULL;
260 struct got_tree_object *
261 got_repo_get_cached_tree(struct got_repository *repo,
262 struct got_object_id *id)
264 return (struct got_tree_object *)got_object_cache_get(
265 &repo->treecache, id);
268 const struct got_error *
269 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
270 struct got_commit_object *commit)
272 #ifndef GOT_NO_OBJ_CACHE
273 const struct got_error *err = NULL;
274 err = got_object_cache_add(&repo->commitcache, id, commit);
275 if (err) {
276 if (err->code == GOT_ERR_OBJ_EXISTS ||
277 err->code == GOT_ERR_OBJ_TOO_LARGE)
278 err = NULL;
279 return err;
281 commit->refcnt++;
282 #endif
283 return NULL;
286 struct got_commit_object *
287 got_repo_get_cached_commit(struct got_repository *repo,
288 struct got_object_id *id)
290 return (struct got_commit_object *)got_object_cache_get(
291 &repo->commitcache, id);
294 const struct got_error *
295 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
296 struct got_tag_object *tag)
298 #ifndef GOT_NO_OBJ_CACHE
299 const struct got_error *err = NULL;
300 err = got_object_cache_add(&repo->tagcache, id, tag);
301 if (err) {
302 if (err->code == GOT_ERR_OBJ_EXISTS ||
303 err->code == GOT_ERR_OBJ_TOO_LARGE)
304 err = NULL;
305 return err;
307 tag->refcnt++;
308 #endif
309 return NULL;
312 struct got_tag_object *
313 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
315 return (struct got_tag_object *)got_object_cache_get(
316 &repo->tagcache, id);
319 const struct got_error *
320 open_repo(struct got_repository *repo, const char *path)
322 const struct got_error *err = NULL;
324 /* bare git repository? */
325 repo->path_git_dir = strdup(path);
326 if (repo->path_git_dir == NULL)
327 return got_error_from_errno("strdup");
328 if (is_git_repo(repo)) {
329 repo->path = strdup(repo->path_git_dir);
330 if (repo->path == NULL) {
331 err = got_error_from_errno("strdup");
332 goto done;
334 return NULL;
337 /* git repository with working tree? */
338 free(repo->path_git_dir);
339 repo->path_git_dir = NULL;
340 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
341 err = got_error_from_errno("asprintf");
342 goto done;
344 if (is_git_repo(repo)) {
345 repo->path = strdup(path);
346 if (repo->path == NULL) {
347 err = got_error_from_errno("strdup");
348 goto done;
350 return NULL;
353 err = got_error(GOT_ERR_NOT_GIT_REPO);
354 done:
355 if (err) {
356 free(repo->path);
357 repo->path = NULL;
358 free(repo->path_git_dir);
359 repo->path_git_dir = NULL;
361 return err;
364 static const struct got_error *
365 parse_gitconfig_file(int *gitconfig_repository_format_version,
366 char **gitconfig_author_name, char **gitconfig_author_email,
367 struct got_remote_repo **remotes, int *nremotes,
368 char **gitconfig_owner,
369 const char *gitconfig_path)
371 const struct got_error *err = NULL, *child_err = NULL;
372 int fd = -1;
373 int imsg_fds[2] = { -1, -1 };
374 pid_t pid;
375 struct imsgbuf *ibuf;
377 *gitconfig_repository_format_version = 0;
378 *gitconfig_author_name = NULL;
379 *gitconfig_author_email = NULL;
380 if (remotes)
381 *remotes = NULL;
382 if (nremotes)
383 *nremotes = 0;
384 if (gitconfig_owner)
385 *gitconfig_owner = NULL;
387 fd = open(gitconfig_path, O_RDONLY);
388 if (fd == -1) {
389 if (errno == ENOENT)
390 return NULL;
391 return got_error_from_errno2("open", gitconfig_path);
394 ibuf = calloc(1, sizeof(*ibuf));
395 if (ibuf == NULL) {
396 err = got_error_from_errno("calloc");
397 goto done;
400 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
401 err = got_error_from_errno("socketpair");
402 goto done;
405 pid = fork();
406 if (pid == -1) {
407 err = got_error_from_errno("fork");
408 goto done;
409 } else if (pid == 0) {
410 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
411 gitconfig_path);
412 /* not reached */
415 if (close(imsg_fds[1]) == -1) {
416 err = got_error_from_errno("close");
417 goto done;
419 imsg_fds[1] = -1;
420 imsg_init(ibuf, imsg_fds[0]);
422 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
423 if (err)
424 goto done;
425 fd = -1;
427 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
428 if (err)
429 goto done;
431 err = got_privsep_recv_gitconfig_int(
432 gitconfig_repository_format_version, ibuf);
433 if (err)
434 goto done;
436 err = got_privsep_send_gitconfig_author_name_req(ibuf);
437 if (err)
438 goto done;
440 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
441 if (err)
442 goto done;
444 err = got_privsep_send_gitconfig_author_email_req(ibuf);
445 if (err)
446 goto done;
448 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
449 if (err)
450 goto done;
452 if (remotes && nremotes) {
453 err = got_privsep_send_gitconfig_remotes_req(ibuf);
454 if (err)
455 goto done;
457 err = got_privsep_recv_gitconfig_remotes(remotes,
458 nremotes, ibuf);
459 if (err)
460 goto done;
463 if (gitconfig_owner) {
464 err = got_privsep_send_gitconfig_owner_req(ibuf);
465 if (err)
466 goto done;
467 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
468 if (err)
469 goto done;
472 imsg_clear(ibuf);
473 err = got_privsep_send_stop(imsg_fds[0]);
474 child_err = got_privsep_wait_for_child(pid);
475 if (child_err && err == NULL)
476 err = child_err;
477 done:
478 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
479 err = got_error_from_errno("close");
480 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
481 err = got_error_from_errno("close");
482 if (fd != -1 && close(fd) == -1 && err == NULL)
483 err = got_error_from_errno2("close", gitconfig_path);
484 free(ibuf);
485 return err;
488 static const struct got_error *
489 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
491 const struct got_error *err = NULL;
492 char *repo_gitconfig_path = NULL;
494 if (global_gitconfig_path) {
495 /* Read settings from ~/.gitconfig. */
496 int dummy_repo_version;
497 err = parse_gitconfig_file(&dummy_repo_version,
498 &repo->global_gitconfig_author_name,
499 &repo->global_gitconfig_author_email,
500 NULL, NULL, NULL, global_gitconfig_path);
501 if (err)
502 return err;
505 /* Read repository's .git/config file. */
506 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
507 if (repo_gitconfig_path == NULL)
508 return got_error_from_errno("got_repo_get_path_gitconfig");
510 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
511 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
512 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
513 &repo->gitconfig_owner, repo_gitconfig_path);
514 if (err)
515 goto done;
516 done:
517 free(repo_gitconfig_path);
518 return err;
521 const struct got_error *
522 got_repo_open(struct got_repository **repop, const char *path,
523 const char *global_gitconfig_path)
525 struct got_repository *repo = NULL;
526 const struct got_error *err = NULL;
527 char *abspath;
528 int i, tried_root = 0;
530 *repop = NULL;
532 if (got_path_is_absolute(path))
533 abspath = strdup(path);
534 else
535 abspath = got_path_get_absolute(path);
536 if (abspath == NULL)
537 return got_error_path(path, GOT_ERR_BAD_PATH);
539 repo = calloc(1, sizeof(*repo));
540 if (repo == NULL) {
541 err = got_error_from_errno("calloc");
542 goto done;
545 for (i = 0; i < nitems(repo->privsep_children); i++) {
546 memset(&repo->privsep_children[i], 0,
547 sizeof(repo->privsep_children[0]));
548 repo->privsep_children[i].imsg_fd = -1;
551 err = got_object_cache_init(&repo->objcache,
552 GOT_OBJECT_CACHE_TYPE_OBJ);
553 if (err)
554 goto done;
555 err = got_object_cache_init(&repo->treecache,
556 GOT_OBJECT_CACHE_TYPE_TREE);
557 if (err)
558 goto done;
559 err = got_object_cache_init(&repo->commitcache,
560 GOT_OBJECT_CACHE_TYPE_COMMIT);
561 if (err)
562 goto done;
563 err = got_object_cache_init(&repo->tagcache,
564 GOT_OBJECT_CACHE_TYPE_TAG);
565 if (err)
566 goto done;
568 path = realpath(abspath, NULL);
569 if (path == NULL) {
570 err = got_error_from_errno2("realpath", abspath);
571 goto done;
574 do {
575 err = open_repo(repo, path);
576 if (err == NULL)
577 break;
578 if (err->code != GOT_ERR_NOT_GIT_REPO)
579 break;
580 if (path[0] == '/' && path[1] == '\0') {
581 if (tried_root) {
582 err = got_error(GOT_ERR_NOT_GIT_REPO);
583 goto done;
585 tried_root = 1;
587 path = dirname(path);
588 if (path == NULL) {
589 err = got_error_from_errno2("dirname", path);
590 goto done;
592 } while (path);
594 err = read_gitconfig(repo, global_gitconfig_path);
595 if (err)
596 goto done;
597 if (repo->gitconfig_repository_format_version != 0)
598 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
599 done:
600 if (err)
601 got_repo_close(repo);
602 else
603 *repop = repo;
604 free(abspath);
605 return err;
608 const struct got_error *
609 got_repo_close(struct got_repository *repo)
611 const struct got_error *err = NULL, *child_err;
612 int i;
614 for (i = 0; i < nitems(repo->packidx_cache); i++) {
615 if (repo->packidx_cache[i] == NULL)
616 break;
617 got_packidx_close(repo->packidx_cache[i]);
620 for (i = 0; i < nitems(repo->packs); i++) {
621 if (repo->packs[i].path_packfile == NULL)
622 break;
623 got_pack_close(&repo->packs[i]);
626 free(repo->path);
627 free(repo->path_git_dir);
629 got_object_cache_close(&repo->objcache);
630 got_object_cache_close(&repo->treecache);
631 got_object_cache_close(&repo->commitcache);
632 got_object_cache_close(&repo->tagcache);
634 for (i = 0; i < nitems(repo->privsep_children); i++) {
635 if (repo->privsep_children[i].imsg_fd == -1)
636 continue;
637 imsg_clear(repo->privsep_children[i].ibuf);
638 free(repo->privsep_children[i].ibuf);
639 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
640 child_err = got_privsep_wait_for_child(
641 repo->privsep_children[i].pid);
642 if (child_err && err == NULL)
643 err = child_err;
644 if (close(repo->privsep_children[i].imsg_fd) != 0 &&
645 err == NULL)
646 err = got_error_from_errno("close");
649 free(repo->gitconfig_author_name);
650 free(repo->gitconfig_author_email);
651 for (i = 0; i < repo->ngitconfig_remotes; i++) {
652 free(repo->gitconfig_remotes[i].name);
653 free(repo->gitconfig_remotes[i].url);
655 free(repo->gitconfig_remotes);
656 free(repo);
658 return err;
661 const struct got_error *
662 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
663 const char *input_path, int check_disk)
665 const struct got_error *err = NULL;
666 const char *repo_abspath = NULL;
667 size_t repolen, len;
668 char *canonpath, *path = NULL;
670 *in_repo_path = NULL;
672 canonpath = strdup(input_path);
673 if (canonpath == NULL) {
674 err = got_error_from_errno("strdup");
675 goto done;
677 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
678 if (err)
679 goto done;
681 repo_abspath = got_repo_get_path(repo);
683 if (!check_disk || canonpath[0] == '\0') {
684 path = strdup(canonpath);
685 if (path == NULL) {
686 err = got_error_from_errno("strdup");
687 goto done;
689 } else {
690 path = realpath(canonpath, NULL);
691 if (path == NULL) {
692 if (errno != ENOENT) {
693 err = got_error_from_errno2("realpath",
694 canonpath);
695 goto done;
697 /*
698 * Path is not on disk.
699 * Assume it is already relative to repository root.
700 */
701 path = strdup(canonpath);
702 if (path == NULL) {
703 err = got_error_from_errno("strdup");
704 goto done;
708 repolen = strlen(repo_abspath);
709 len = strlen(path);
712 if (strcmp(path, repo_abspath) == 0) {
713 free(path);
714 path = strdup("");
715 if (path == NULL) {
716 err = got_error_from_errno("strdup");
717 goto done;
719 } else if (len > repolen &&
720 got_path_is_child(path, repo_abspath, repolen)) {
721 /* Matched an on-disk path inside repository. */
722 if (got_repo_is_bare(repo)) {
723 /*
724 * Matched an on-disk path inside repository
725 * database. Treat as repository-relative.
726 */
727 } else {
728 char *child;
729 /* Strip common prefix with repository path. */
730 err = got_path_skip_common_ancestor(&child,
731 repo_abspath, path);
732 if (err)
733 goto done;
734 free(path);
735 path = child;
737 } else {
738 /*
739 * Matched unrelated on-disk path.
740 * Treat it as repository-relative.
741 */
745 /* Make in-repository path absolute */
746 if (path[0] != '/') {
747 char *abspath;
748 if (asprintf(&abspath, "/%s", path) == -1) {
749 err = got_error_from_errno("asprintf");
750 goto done;
752 free(path);
753 path = abspath;
756 done:
757 free(canonpath);
758 if (err)
759 free(path);
760 else
761 *in_repo_path = path;
762 return err;
765 static const struct got_error *
766 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
767 const char *path_packidx)
769 const struct got_error *err = NULL;
770 int i;
772 for (i = 0; i < nitems(repo->packidx_cache); i++) {
773 if (repo->packidx_cache[i] == NULL)
774 break;
775 if (strcmp(repo->packidx_cache[i]->path_packidx,
776 path_packidx) == 0) {
777 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
780 if (i == nitems(repo->packidx_cache)) {
781 err = got_packidx_close(repo->packidx_cache[i - 1]);
782 if (err)
783 return err;
786 /*
787 * Insert the new pack index at the front so it will
788 * be searched first in the future.
789 */
790 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
791 sizeof(repo->packidx_cache) -
792 sizeof(repo->packidx_cache[0]));
793 repo->packidx_cache[0] = packidx;
795 return NULL;
798 static int
799 is_packidx_filename(const char *name, size_t len)
801 if (len != GOT_PACKIDX_NAMELEN)
802 return 0;
804 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
805 return 0;
807 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
808 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
809 return 0;
811 return 1;
814 const struct got_error *
815 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
816 struct got_repository *repo, struct got_object_id *id)
818 const struct got_error *err;
819 char *path_packdir;
820 DIR *packdir;
821 struct dirent *dent;
822 char *path_packidx;
823 int i;
825 /* Search pack index cache. */
826 for (i = 0; i < nitems(repo->packidx_cache); i++) {
827 if (repo->packidx_cache[i] == NULL)
828 break;
829 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
830 if (*idx != -1) {
831 *packidx = repo->packidx_cache[i];
832 /*
833 * Move this cache entry to the front. Repeatedly
834 * searching a wrong pack index can be expensive.
835 */
836 if (i > 0) {
837 struct got_packidx *p;
838 p = repo->packidx_cache[0];
839 repo->packidx_cache[0] = *packidx;
840 repo->packidx_cache[i] = p;
842 return NULL;
845 /* No luck. Search the filesystem. */
847 path_packdir = got_repo_get_path_objects_pack(repo);
848 if (path_packdir == NULL)
849 return got_error_from_errno("got_repo_get_path_objects_pack");
851 packdir = opendir(path_packdir);
852 if (packdir == NULL) {
853 if (errno == ENOENT)
854 err = got_error_no_obj(id);
855 else
856 err = got_error_from_errno2("opendir", path_packdir);
857 goto done;
860 while ((dent = readdir(packdir)) != NULL) {
861 int is_cached = 0;
863 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
864 continue;
866 if (asprintf(&path_packidx, "%s/%s", path_packdir,
867 dent->d_name) == -1) {
868 err = got_error_from_errno("asprintf");
869 goto done;
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 is_cached = 1;
878 break;
881 if (is_cached) {
882 free(path_packidx);
883 continue; /* already searched */
886 err = got_packidx_open(packidx, path_packidx, 0);
887 if (err) {
888 free(path_packidx);
889 goto done;
892 err = cache_packidx(repo, *packidx, path_packidx);
893 free(path_packidx);
894 if (err)
895 goto done;
897 *idx = got_packidx_get_object_idx(*packidx, id);
898 if (*idx != -1) {
899 err = NULL; /* found the object */
900 goto done;
904 err = got_error_no_obj(id);
905 done:
906 free(path_packdir);
907 if (packdir && closedir(packdir) != 0 && err == NULL)
908 err = got_error_from_errno("closedir");
909 return err;
912 static const struct got_error *
913 read_packfile_hdr(int fd, struct got_packidx *packidx)
915 const struct got_error *err = NULL;
916 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
917 struct got_packfile_hdr hdr;
918 ssize_t n;
920 n = read(fd, &hdr, sizeof(hdr));
921 if (n < 0)
922 return got_error_from_errno("read");
923 if (n != sizeof(hdr))
924 return got_error(GOT_ERR_BAD_PACKFILE);
926 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
927 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
928 be32toh(hdr.nobjects) != totobj)
929 err = got_error(GOT_ERR_BAD_PACKFILE);
931 return err;
934 static const struct got_error *
935 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
937 const struct got_error *err = NULL;
939 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
940 if (*fd == -1)
941 return got_error_from_errno2("open", path_packfile);
943 if (packidx) {
944 err = read_packfile_hdr(*fd, packidx);
945 if (err) {
946 close(*fd);
947 *fd = -1;
951 return err;
954 const struct got_error *
955 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
956 const char *path_packfile, struct got_packidx *packidx)
958 const struct got_error *err = NULL;
959 struct got_pack *pack = NULL;
960 struct stat sb;
961 int i;
963 if (packp)
964 *packp = NULL;
966 for (i = 0; i < nitems(repo->packs); i++) {
967 pack = &repo->packs[i];
968 if (pack->path_packfile == NULL)
969 break;
970 if (strcmp(pack->path_packfile, path_packfile) == 0)
971 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
974 if (i == nitems(repo->packs) - 1) {
975 err = got_pack_close(&repo->packs[i - 1]);
976 if (err)
977 return err;
978 memmove(&repo->packs[1], &repo->packs[0],
979 sizeof(repo->packs) - sizeof(repo->packs[0]));
980 i = 0;
983 pack = &repo->packs[i];
985 pack->path_packfile = strdup(path_packfile);
986 if (pack->path_packfile == NULL) {
987 err = got_error_from_errno("strdup");
988 goto done;
991 err = open_packfile(&pack->fd, path_packfile, packidx);
992 if (err)
993 goto done;
995 if (fstat(pack->fd, &sb) != 0) {
996 err = got_error_from_errno("fstat");
997 goto done;
999 pack->filesize = sb.st_size;
1001 pack->privsep_child = NULL;
1003 #ifndef GOT_PACK_NO_MMAP
1004 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1005 pack->fd, 0);
1006 if (pack->map == MAP_FAILED) {
1007 if (errno != ENOMEM) {
1008 err = got_error_from_errno("mmap");
1009 goto done;
1011 pack->map = NULL; /* fall back to read(2) */
1013 #endif
1014 done:
1015 if (err) {
1016 if (pack) {
1017 free(pack->path_packfile);
1018 memset(pack, 0, sizeof(*pack));
1020 } else if (packp)
1021 *packp = pack;
1022 return err;
1025 struct got_pack *
1026 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1028 struct got_pack *pack = NULL;
1029 int i;
1031 for (i = 0; i < nitems(repo->packs); i++) {
1032 pack = &repo->packs[i];
1033 if (pack->path_packfile == NULL)
1034 break;
1035 if (strcmp(pack->path_packfile, path_packfile) == 0)
1036 return pack;
1039 return NULL;
1042 const struct got_error *
1043 got_repo_init(const char *repo_path)
1045 const struct got_error *err = NULL;
1046 const char *dirnames[] = {
1047 GOT_OBJECTS_DIR,
1048 GOT_OBJECTS_PACK_DIR,
1049 GOT_REFS_DIR,
1051 const char *description_str = "Unnamed repository; "
1052 "edit this file 'description' to name the repository.";
1053 const char *headref_str = "ref: refs/heads/main";
1054 const char *gitconfig_str = "[core]\n"
1055 "\trepositoryformatversion = 0\n"
1056 "\tfilemode = true\n"
1057 "\tbare = true\n";
1058 char *path;
1059 int i;
1061 if (!got_path_dir_is_empty(repo_path))
1062 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1064 for (i = 0; i < nitems(dirnames); i++) {
1065 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1066 return got_error_from_errno("asprintf");
1068 err = got_path_mkdir(path);
1069 free(path);
1070 if (err)
1071 return err;
1074 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1075 return got_error_from_errno("asprintf");
1076 err = got_path_create_file(path, description_str);
1077 free(path);
1078 if (err)
1079 return err;
1081 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1082 return got_error_from_errno("asprintf");
1083 err = got_path_create_file(path, headref_str);
1084 free(path);
1085 if (err)
1086 return err;
1088 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1089 return got_error_from_errno("asprintf");
1090 err = got_path_create_file(path, gitconfig_str);
1091 free(path);
1092 if (err)
1093 return err;
1095 return NULL;
1098 static const struct got_error *
1099 match_packed_object(struct got_object_id **unique_id,
1100 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1102 const struct got_error *err = NULL;
1103 char *path_packdir;
1104 DIR *packdir;
1105 struct dirent *dent;
1106 char *path_packidx;
1107 struct got_object_id_queue matched_ids;
1109 SIMPLEQ_INIT(&matched_ids);
1111 path_packdir = got_repo_get_path_objects_pack(repo);
1112 if (path_packdir == NULL)
1113 return got_error_from_errno("got_repo_get_path_objects_pack");
1115 packdir = opendir(path_packdir);
1116 if (packdir == NULL) {
1117 if (errno != ENOENT)
1118 err = got_error_from_errno2("opendir", path_packdir);
1119 goto done;
1122 while ((dent = readdir(packdir)) != NULL) {
1123 struct got_packidx *packidx;
1124 struct got_object_qid *qid;
1127 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1128 continue;
1130 if (asprintf(&path_packidx, "%s/%s", path_packdir,
1131 dent->d_name) == -1) {
1132 err = got_error_from_errno("asprintf");
1133 break;
1136 err = got_packidx_open(&packidx, path_packidx, 0);
1137 free(path_packidx);
1138 if (err)
1139 break;
1141 err = got_packidx_match_id_str_prefix(&matched_ids,
1142 packidx, id_str_prefix);
1143 if (err) {
1144 got_packidx_close(packidx);
1145 break;
1147 err = got_packidx_close(packidx);
1148 if (err)
1149 break;
1151 SIMPLEQ_FOREACH(qid, &matched_ids, entry) {
1152 if (obj_type != GOT_OBJ_TYPE_ANY) {
1153 int matched_type;
1154 err = got_object_get_type(&matched_type, repo,
1155 qid->id);
1156 if (err)
1157 goto done;
1158 if (matched_type != obj_type)
1159 continue;
1161 if (*unique_id == NULL) {
1162 *unique_id = got_object_id_dup(qid->id);
1163 if (*unique_id == NULL) {
1164 err = got_error_from_errno("malloc");
1165 goto done;
1167 } else {
1168 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1169 continue; /* packed multiple times */
1170 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1171 goto done;
1175 done:
1176 got_object_id_queue_free(&matched_ids);
1177 free(path_packdir);
1178 if (packdir && closedir(packdir) != 0 && err == NULL)
1179 err = got_error_from_errno("closedir");
1180 if (err) {
1181 free(*unique_id);
1182 *unique_id = NULL;
1184 return err;
1187 static const struct got_error *
1188 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1189 const char *object_dir, const char *id_str_prefix, int obj_type,
1190 struct got_repository *repo)
1192 const struct got_error *err = NULL;
1193 char *path;
1194 DIR *dir = NULL;
1195 struct dirent *dent;
1196 struct got_object_id id;
1198 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1199 err = got_error_from_errno("asprintf");
1200 goto done;
1203 dir = opendir(path);
1204 if (dir == NULL) {
1205 if (errno == ENOENT) {
1206 err = NULL;
1207 goto done;
1209 err = got_error_from_errno2("opendir", path);
1210 goto done;
1212 while ((dent = readdir(dir)) != NULL) {
1213 char *id_str;
1214 int cmp;
1216 if (strcmp(dent->d_name, ".") == 0 ||
1217 strcmp(dent->d_name, "..") == 0)
1218 continue;
1220 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1221 err = got_error_from_errno("asprintf");
1222 goto done;
1225 if (!got_parse_sha1_digest(id.sha1, id_str))
1226 continue;
1229 * Directory entries do not necessarily appear in
1230 * sorted order, so we must iterate over all of them.
1232 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1233 if (cmp != 0) {
1234 free(id_str);
1235 continue;
1238 if (*unique_id == NULL) {
1239 if (obj_type != GOT_OBJ_TYPE_ANY) {
1240 int matched_type;
1241 err = got_object_get_type(&matched_type, repo,
1242 &id);
1243 if (err)
1244 goto done;
1245 if (matched_type != obj_type)
1246 continue;
1248 *unique_id = got_object_id_dup(&id);
1249 if (*unique_id == NULL) {
1250 err = got_error_from_errno("got_object_id_dup");
1251 free(id_str);
1252 goto done;
1254 } else {
1255 if (got_object_id_cmp(*unique_id, &id) == 0)
1256 continue; /* both packed and loose */
1257 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1258 free(id_str);
1259 goto done;
1262 done:
1263 if (dir && closedir(dir) != 0 && err == NULL)
1264 err = got_error_from_errno("closedir");
1265 if (err) {
1266 free(*unique_id);
1267 *unique_id = NULL;
1269 free(path);
1270 return err;
1273 const struct got_error *
1274 got_repo_match_object_id_prefix(struct got_object_id **id,
1275 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1277 const struct got_error *err = NULL;
1278 char *path_objects = got_repo_get_path_objects(repo);
1279 char *object_dir = NULL;
1280 size_t len;
1281 int i;
1283 *id = NULL;
1285 for (i = 0; i < strlen(id_str_prefix); i++) {
1286 if (isxdigit((unsigned char)id_str_prefix[i]))
1287 continue;
1288 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1291 len = strlen(id_str_prefix);
1292 if (len >= 2) {
1293 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1294 if (err)
1295 goto done;
1296 object_dir = strndup(id_str_prefix, 2);
1297 if (object_dir == NULL) {
1298 err = got_error_from_errno("strdup");
1299 goto done;
1301 err = match_loose_object(id, path_objects, object_dir,
1302 id_str_prefix, obj_type, repo);
1303 } else if (len == 1) {
1304 int i;
1305 for (i = 0; i < 0xf; i++) {
1306 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1307 == -1) {
1308 err = got_error_from_errno("asprintf");
1309 goto done;
1311 err = match_packed_object(id, repo, object_dir,
1312 obj_type);
1313 if (err)
1314 goto done;
1315 err = match_loose_object(id, path_objects, object_dir,
1316 id_str_prefix, obj_type, repo);
1317 if (err)
1318 goto done;
1320 } else {
1321 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1322 goto done;
1324 done:
1325 free(object_dir);
1326 if (err) {
1327 free(*id);
1328 *id = NULL;
1329 } else if (*id == NULL)
1330 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1332 return err;
1335 const struct got_error *
1336 got_repo_match_object_id(struct got_object_id **id, char **label,
1337 const char *id_str, int obj_type, int resolve_tags,
1338 struct got_repository *repo)
1340 const struct got_error *err;
1341 struct got_tag_object *tag;
1342 struct got_reference *ref = NULL;
1344 *id = NULL;
1345 if (label)
1346 *label = NULL;
1348 if (resolve_tags) {
1349 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
1350 repo);
1351 if (err == NULL) {
1352 *id = got_object_id_dup(
1353 got_object_tag_get_object_id(tag));
1354 if (*id == NULL)
1355 err = got_error_from_errno("got_object_id_dup");
1356 else if (label && asprintf(label, "refs/tags/%s",
1357 got_object_tag_get_name(tag)) == -1) {
1358 err = got_error_from_errno("asprintf");
1359 free(*id);
1360 *id = NULL;
1362 got_object_tag_close(tag);
1363 return err;
1364 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1365 err->code != GOT_ERR_NO_OBJ)
1366 return err;
1369 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1370 if (err) {
1371 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1372 return err;
1373 err = got_ref_open(&ref, repo, id_str, 0);
1374 if (err != NULL)
1375 goto done;
1376 if (label) {
1377 *label = strdup(got_ref_get_name(ref));
1378 if (*label == NULL) {
1379 err = got_error_from_errno("strdup");
1380 goto done;
1383 err = got_ref_resolve(id, repo, ref);
1384 } else if (label) {
1385 err = got_object_id_str(label, *id);
1386 if (*label == NULL) {
1387 err = got_error_from_errno("strdup");
1388 goto done;
1391 done:
1392 if (ref)
1393 got_ref_close(ref);
1394 return err;
1397 const struct got_error *
1398 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1399 int obj_type, struct got_repository *repo)
1401 const struct got_error *err;
1402 struct got_reflist_head refs;
1403 struct got_reflist_entry *re;
1404 struct got_object_id *tag_id;
1406 SIMPLEQ_INIT(&refs);
1407 *tag = NULL;
1409 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_by_name, NULL);
1410 if (err)
1411 return err;
1413 SIMPLEQ_FOREACH(re, &refs, entry) {
1414 const char *refname;
1415 refname = got_ref_get_name(re->ref);
1416 if (got_ref_is_symbolic(re->ref))
1417 continue;
1418 refname += strlen("refs/tags/");
1419 if (strcmp(refname, name) != 0)
1420 continue;
1421 err = got_ref_resolve(&tag_id, repo, re->ref);
1422 if (err)
1423 break;
1424 err = got_object_open_as_tag(tag, repo, tag_id);
1425 free(tag_id);
1426 if (err)
1427 break;
1428 if (obj_type == GOT_OBJ_TYPE_ANY ||
1429 got_object_tag_get_object_type(*tag) == obj_type)
1430 break;
1431 got_object_tag_close(*tag);
1432 *tag = NULL;
1435 got_ref_list_free(&refs);
1436 if (err == NULL && *tag == NULL)
1437 err = got_error_path(name, GOT_ERR_NO_OBJ);
1438 return err;
1441 static const struct got_error *
1442 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1443 const char *name, mode_t mode, struct got_object_id *blob_id)
1445 const struct got_error *err = NULL;
1447 *new_te = NULL;
1449 *new_te = calloc(1, sizeof(**new_te));
1450 if (*new_te == NULL)
1451 return got_error_from_errno("calloc");
1453 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1454 sizeof((*new_te)->name)) {
1455 err = got_error(GOT_ERR_NO_SPACE);
1456 goto done;
1459 if (S_ISLNK(mode)) {
1460 (*new_te)->mode = S_IFLNK;
1461 } else {
1462 (*new_te)->mode = S_IFREG;
1463 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1465 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1466 done:
1467 if (err && *new_te) {
1468 free(*new_te);
1469 *new_te = NULL;
1471 return err;
1474 static const struct got_error *
1475 import_file(struct got_tree_entry **new_te, struct dirent *de,
1476 const char *path, struct got_repository *repo)
1478 const struct got_error *err;
1479 struct got_object_id *blob_id = NULL;
1480 char *filepath;
1481 struct stat sb;
1483 if (asprintf(&filepath, "%s%s%s", path,
1484 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1485 return got_error_from_errno("asprintf");
1487 if (lstat(filepath, &sb) != 0) {
1488 err = got_error_from_errno2("lstat", path);
1489 goto done;
1492 err = got_object_blob_create(&blob_id, filepath, repo);
1493 if (err)
1494 goto done;
1496 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1497 blob_id);
1498 done:
1499 free(filepath);
1500 if (err)
1501 free(blob_id);
1502 return err;
1505 static const struct got_error *
1506 insert_tree_entry(struct got_tree_entry *new_te,
1507 struct got_pathlist_head *paths)
1509 const struct got_error *err = NULL;
1510 struct got_pathlist_entry *new_pe;
1512 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1513 if (err)
1514 return err;
1515 if (new_pe == NULL)
1516 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1517 return NULL;
1520 static const struct got_error *write_tree(struct got_object_id **,
1521 const char *, struct got_pathlist_head *, struct got_repository *,
1522 got_repo_import_cb progress_cb, void *progress_arg);
1524 static const struct got_error *
1525 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1526 const char *path, struct got_pathlist_head *ignores,
1527 struct got_repository *repo,
1528 got_repo_import_cb progress_cb, void *progress_arg)
1530 const struct got_error *err;
1531 struct got_object_id *id = NULL;
1532 char *subdirpath;
1534 if (asprintf(&subdirpath, "%s%s%s", path,
1535 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1536 return got_error_from_errno("asprintf");
1538 (*new_te) = calloc(1, sizeof(**new_te));
1539 if (*new_te == NULL)
1540 return got_error_from_errno("calloc");
1541 (*new_te)->mode = S_IFDIR;
1542 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1543 sizeof((*new_te)->name)) {
1544 err = got_error(GOT_ERR_NO_SPACE);
1545 goto done;
1547 err = write_tree(&id, subdirpath, ignores, repo,
1548 progress_cb, progress_arg);
1549 if (err)
1550 goto done;
1551 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1553 done:
1554 free(id);
1555 free(subdirpath);
1556 if (err) {
1557 free(*new_te);
1558 *new_te = NULL;
1560 return err;
1563 static const struct got_error *
1564 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1565 struct got_pathlist_head *ignores, struct got_repository *repo,
1566 got_repo_import_cb progress_cb, void *progress_arg)
1568 const struct got_error *err = NULL;
1569 DIR *dir;
1570 struct dirent *de;
1571 int nentries;
1572 struct got_tree_entry *new_te = NULL;
1573 struct got_pathlist_head paths;
1574 struct got_pathlist_entry *pe;
1576 *new_tree_id = NULL;
1578 TAILQ_INIT(&paths);
1580 dir = opendir(path_dir);
1581 if (dir == NULL) {
1582 err = got_error_from_errno2("opendir", path_dir);
1583 goto done;
1586 nentries = 0;
1587 while ((de = readdir(dir)) != NULL) {
1588 int ignore = 0;
1589 int type;
1591 if (strcmp(de->d_name, ".") == 0 ||
1592 strcmp(de->d_name, "..") == 0)
1593 continue;
1595 TAILQ_FOREACH(pe, ignores, entry) {
1596 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1597 ignore = 1;
1598 break;
1601 if (ignore)
1602 continue;
1604 err = got_path_dirent_type(&type, path_dir, de);
1605 if (err)
1606 goto done;
1608 if (type == DT_DIR) {
1609 err = import_subdir(&new_te, de, path_dir,
1610 ignores, repo, progress_cb, progress_arg);
1611 if (err) {
1612 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1613 goto done;
1614 err = NULL;
1615 continue;
1617 } else if (type == DT_REG || type == DT_LNK) {
1618 err = import_file(&new_te, de, path_dir, repo);
1619 if (err)
1620 goto done;
1621 } else
1622 continue;
1624 err = insert_tree_entry(new_te, &paths);
1625 if (err)
1626 goto done;
1627 nentries++;
1630 if (TAILQ_EMPTY(&paths)) {
1631 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
1632 "cannot create tree without any entries");
1633 goto done;
1636 TAILQ_FOREACH(pe, &paths, entry) {
1637 struct got_tree_entry *te = pe->data;
1638 char *path;
1639 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
1640 continue;
1641 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1642 err = got_error_from_errno("asprintf");
1643 goto done;
1645 err = (*progress_cb)(progress_arg, path);
1646 free(path);
1647 if (err)
1648 goto done;
1651 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1652 done:
1653 if (dir)
1654 closedir(dir);
1655 got_pathlist_free(&paths);
1656 return err;
1659 const struct got_error *
1660 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1661 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1662 struct got_repository *repo, got_repo_import_cb progress_cb,
1663 void *progress_arg)
1665 const struct got_error *err;
1666 struct got_object_id *new_tree_id;
1668 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1669 progress_cb, progress_arg);
1670 if (err)
1671 return err;
1673 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1674 author, time(NULL), author, time(NULL), logmsg, repo);
1675 free(new_tree_id);
1676 return err;