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 <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 <zlib.h>
36 #include <errno.h>
37 #include <libgen.h>
38 #include <stdint.h>
39 #include <imsg.h>
40 #include <uuid.h>
42 #include "got_error.h"
43 #include "got_reference.h"
44 #include "got_repository.h"
45 #include "got_path.h"
46 #include "got_cancel.h"
47 #include "got_worktree.h"
48 #include "got_object.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_object.h"
53 #include "got_lib_object_parse.h"
54 #include "got_lib_object_create.h"
55 #include "got_lib_pack.h"
56 #include "got_lib_privsep.h"
57 #include "got_lib_worktree.h"
58 #include "got_lib_sha1.h"
59 #include "got_lib_object_cache.h"
60 #include "got_lib_repository.h"
62 #ifndef nitems
63 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
64 #endif
66 #define GOT_GIT_DIR ".git"
68 /* Mandatory files and directories inside the git directory. */
69 #define GOT_OBJECTS_DIR "objects"
70 #define GOT_REFS_DIR "refs"
71 #define GOT_HEAD_FILE "HEAD"
72 #define GOT_GITCONFIG "config"
74 /* Other files and directories inside the git directory. */
75 #define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
76 #define GOT_ORIG_HEAD_FILE "ORIG_HEAD"
77 #define GOT_OBJECTS_PACK_DIR "objects/pack"
78 #define GOT_PACKED_REFS_FILE "packed-refs"
80 const char *
81 got_repo_get_path(struct got_repository *repo)
82 {
83 return repo->path;
84 }
86 const char *
87 got_repo_get_path_git_dir(struct got_repository *repo)
88 {
89 return repo->path_git_dir;
90 }
92 const char *
93 got_repo_get_gitconfig_author_name(struct got_repository *repo)
94 {
95 return repo->gitconfig_author_name;
96 }
98 const char *
99 got_repo_get_gitconfig_author_email(struct got_repository *repo)
101 return repo->gitconfig_author_email;
104 const char *
105 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
107 return repo->global_gitconfig_author_name;
110 const char *
111 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
113 return repo->global_gitconfig_author_email;
116 const char *
117 got_repo_get_gitconfig_owner(struct got_repository *repo)
119 return repo->gitconfig_owner;
122 int
123 got_repo_is_bare(struct got_repository *repo)
125 return (strcmp(repo->path, repo->path_git_dir) == 0);
128 static char *
129 get_path_git_child(struct got_repository *repo, const char *basename)
131 char *path_child;
133 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
134 basename) == -1)
135 return NULL;
137 return path_child;
140 char *
141 got_repo_get_path_objects(struct got_repository *repo)
143 return get_path_git_child(repo, GOT_OBJECTS_DIR);
146 char *
147 got_repo_get_path_objects_pack(struct got_repository *repo)
149 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
152 char *
153 got_repo_get_path_refs(struct got_repository *repo)
155 return get_path_git_child(repo, GOT_REFS_DIR);
158 char *
159 got_repo_get_path_packed_refs(struct got_repository *repo)
161 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
164 static char *
165 get_path_head(struct got_repository *repo)
167 return get_path_git_child(repo, GOT_HEAD_FILE);
170 static const struct got_error *
171 get_path_gitconfig(char **p, struct got_repository *repo)
173 *p = get_path_git_child(repo, GOT_GITCONFIG);
174 if (*p == NULL)
175 return got_error_from_errno("asprintf");
176 return NULL;
179 void
180 got_repo_get_gitconfig_remotes(int *nremotes, struct got_remote_repo **remotes,
181 struct got_repository *repo)
183 *nremotes = repo->ngitconfig_remotes;
184 *remotes = repo->gitconfig_remotes;
187 static int
188 is_git_repo(struct got_repository *repo)
190 const char *path_git = got_repo_get_path_git_dir(repo);
191 char *path_objects = got_repo_get_path_objects(repo);
192 char *path_refs = got_repo_get_path_refs(repo);
193 char *path_head = get_path_head(repo);
194 int ret = 0;
195 struct stat sb;
196 struct got_reference *head_ref;
198 if (lstat(path_git, &sb) == -1)
199 goto done;
200 if (!S_ISDIR(sb.st_mode))
201 goto done;
203 if (lstat(path_objects, &sb) == -1)
204 goto done;
205 if (!S_ISDIR(sb.st_mode))
206 goto done;
208 if (lstat(path_refs, &sb) == -1)
209 goto done;
210 if (!S_ISDIR(sb.st_mode))
211 goto done;
213 if (lstat(path_head, &sb) == -1)
214 goto done;
215 if (!S_ISREG(sb.st_mode))
216 goto done;
218 /* Check if the HEAD reference can be opened. */
219 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
220 goto done;
221 got_ref_close(head_ref);
223 ret = 1;
224 done:
225 free(path_objects);
226 free(path_refs);
227 free(path_head);
228 return ret;
232 const struct got_error *
233 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
234 struct got_object *obj)
236 #ifndef GOT_NO_OBJ_CACHE
237 const struct got_error *err = NULL;
238 err = got_object_cache_add(&repo->objcache, id, obj);
239 if (err) {
240 if (err->code == GOT_ERR_OBJ_EXISTS ||
241 err->code == GOT_ERR_OBJ_TOO_LARGE)
242 err = NULL;
243 return err;
245 obj->refcnt++;
246 #endif
247 return NULL;
250 struct got_object *
251 got_repo_get_cached_object(struct got_repository *repo,
252 struct got_object_id *id)
254 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
257 const struct got_error *
258 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
259 struct got_tree_object *tree)
261 #ifndef GOT_NO_OBJ_CACHE
262 const struct got_error *err = NULL;
263 err = got_object_cache_add(&repo->treecache, id, tree);
264 if (err) {
265 if (err->code == GOT_ERR_OBJ_EXISTS ||
266 err->code == GOT_ERR_OBJ_TOO_LARGE)
267 err = NULL;
268 return err;
270 tree->refcnt++;
271 #endif
272 return NULL;
275 struct got_tree_object *
276 got_repo_get_cached_tree(struct got_repository *repo,
277 struct got_object_id *id)
279 return (struct got_tree_object *)got_object_cache_get(
280 &repo->treecache, id);
283 const struct got_error *
284 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
285 struct got_commit_object *commit)
287 #ifndef GOT_NO_OBJ_CACHE
288 const struct got_error *err = NULL;
289 err = got_object_cache_add(&repo->commitcache, id, commit);
290 if (err) {
291 if (err->code == GOT_ERR_OBJ_EXISTS ||
292 err->code == GOT_ERR_OBJ_TOO_LARGE)
293 err = NULL;
294 return err;
296 commit->refcnt++;
297 #endif
298 return NULL;
301 struct got_commit_object *
302 got_repo_get_cached_commit(struct got_repository *repo,
303 struct got_object_id *id)
305 return (struct got_commit_object *)got_object_cache_get(
306 &repo->commitcache, id);
309 const struct got_error *
310 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
311 struct got_tag_object *tag)
313 #ifndef GOT_NO_OBJ_CACHE
314 const struct got_error *err = NULL;
315 err = got_object_cache_add(&repo->tagcache, id, tag);
316 if (err) {
317 if (err->code == GOT_ERR_OBJ_EXISTS ||
318 err->code == GOT_ERR_OBJ_TOO_LARGE)
319 err = NULL;
320 return err;
322 tag->refcnt++;
323 #endif
324 return NULL;
327 struct got_tag_object *
328 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
330 return (struct got_tag_object *)got_object_cache_get(
331 &repo->tagcache, id);
334 const struct got_error *
335 open_repo(struct got_repository *repo, const char *path)
337 const struct got_error *err = NULL;
339 /* bare git repository? */
340 repo->path_git_dir = strdup(path);
341 if (repo->path_git_dir == NULL)
342 return got_error_from_errno("strdup");
343 if (is_git_repo(repo)) {
344 repo->path = strdup(repo->path_git_dir);
345 if (repo->path == NULL) {
346 err = got_error_from_errno("strdup");
347 goto done;
349 return NULL;
352 /* git repository with working tree? */
353 free(repo->path_git_dir);
354 repo->path_git_dir = NULL;
355 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
356 err = got_error_from_errno("asprintf");
357 goto done;
359 if (is_git_repo(repo)) {
360 repo->path = strdup(path);
361 if (repo->path == NULL) {
362 err = got_error_from_errno("strdup");
363 goto done;
365 return NULL;
368 err = got_error(GOT_ERR_NOT_GIT_REPO);
369 done:
370 if (err) {
371 free(repo->path);
372 repo->path = NULL;
373 free(repo->path_git_dir);
374 repo->path_git_dir = NULL;
376 return err;
379 static const struct got_error *
380 parse_gitconfig_file(int *gitconfig_repository_format_version,
381 char **gitconfig_author_name, char **gitconfig_author_email,
382 struct got_remote_repo **remotes, int *nremotes,
383 char **gitconfig_owner,
384 const char *gitconfig_path)
386 const struct got_error *err = NULL, *child_err = NULL;
387 int fd = -1;
388 int imsg_fds[2] = { -1, -1 };
389 pid_t pid;
390 struct imsgbuf *ibuf;
392 *gitconfig_repository_format_version = 0;
393 *gitconfig_author_name = NULL;
394 *gitconfig_author_email = NULL;
396 fd = open(gitconfig_path, O_RDONLY);
397 if (fd == -1) {
398 if (errno == ENOENT)
399 return NULL;
400 return got_error_from_errno2("open", gitconfig_path);
403 ibuf = calloc(1, sizeof(*ibuf));
404 if (ibuf == NULL) {
405 err = got_error_from_errno("calloc");
406 goto done;
409 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
410 err = got_error_from_errno("socketpair");
411 goto done;
414 pid = fork();
415 if (pid == -1) {
416 err = got_error_from_errno("fork");
417 goto done;
418 } else if (pid == 0) {
419 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
420 gitconfig_path);
421 /* not reached */
424 if (close(imsg_fds[1]) == -1) {
425 err = got_error_from_errno("close");
426 goto done;
428 imsg_fds[1] = -1;
429 imsg_init(ibuf, imsg_fds[0]);
431 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
432 if (err)
433 goto done;
434 fd = -1;
436 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
437 if (err)
438 goto done;
440 err = got_privsep_recv_gitconfig_int(
441 gitconfig_repository_format_version, ibuf);
442 if (err)
443 goto done;
445 err = got_privsep_send_gitconfig_author_name_req(ibuf);
446 if (err)
447 goto done;
449 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
450 if (err)
451 goto done;
453 err = got_privsep_send_gitconfig_author_email_req(ibuf);
454 if (err)
455 goto done;
457 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
458 if (err)
459 goto done;
461 if (remotes && nremotes) {
462 err = got_privsep_send_gitconfig_remotes_req(ibuf);
463 if (err)
464 goto done;
466 err = got_privsep_recv_gitconfig_remotes(remotes,
467 nremotes, ibuf);
468 if (err)
469 goto done;
472 if (gitconfig_owner) {
473 err = got_privsep_send_gitconfig_owner_req(ibuf);
474 if (err)
475 goto done;
476 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
477 if (err)
478 goto done;
481 imsg_clear(ibuf);
482 err = got_privsep_send_stop(imsg_fds[0]);
483 child_err = got_privsep_wait_for_child(pid);
484 if (child_err && err == NULL)
485 err = child_err;
486 done:
487 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
488 err = got_error_from_errno("close");
489 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
490 err = got_error_from_errno("close");
491 if (fd != -1 && close(fd) == -1 && err == NULL)
492 err = got_error_from_errno2("close", gitconfig_path);
493 free(ibuf);
494 return err;
497 static const struct got_error *
498 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
500 const struct got_error *err = NULL;
501 char *repo_gitconfig_path = NULL;
503 if (global_gitconfig_path) {
504 /* Read settings from ~/.gitconfig. */
505 int dummy_repo_version;
506 err = parse_gitconfig_file(&dummy_repo_version,
507 &repo->global_gitconfig_author_name,
508 &repo->global_gitconfig_author_email,
509 NULL, NULL, NULL, global_gitconfig_path);
510 if (err)
511 return err;
514 /* Read repository's .git/config file. */
515 err = get_path_gitconfig(&repo_gitconfig_path, repo);
516 if (err)
517 return err;
519 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
520 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
521 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
522 &repo->gitconfig_owner, repo_gitconfig_path);
523 if (err)
524 goto done;
525 done:
526 free(repo_gitconfig_path);
527 return err;
530 const struct got_error *
531 got_repo_open(struct got_repository **repop, const char *path,
532 const char *global_gitconfig_path)
534 struct got_repository *repo = NULL;
535 const struct got_error *err = NULL;
536 char *abspath;
537 int i, tried_root = 0;
539 *repop = NULL;
541 if (got_path_is_absolute(path))
542 abspath = strdup(path);
543 else
544 abspath = got_path_get_absolute(path);
545 if (abspath == NULL)
546 return got_error(GOT_ERR_BAD_PATH);
548 repo = calloc(1, sizeof(*repo));
549 if (repo == NULL) {
550 err = got_error_from_errno("calloc");
551 goto done;
554 for (i = 0; i < nitems(repo->privsep_children); i++) {
555 memset(&repo->privsep_children[i], 0,
556 sizeof(repo->privsep_children[0]));
557 repo->privsep_children[i].imsg_fd = -1;
560 err = got_object_cache_init(&repo->objcache,
561 GOT_OBJECT_CACHE_TYPE_OBJ);
562 if (err)
563 goto done;
564 err = got_object_cache_init(&repo->treecache,
565 GOT_OBJECT_CACHE_TYPE_TREE);
566 if (err)
567 goto done;
568 err = got_object_cache_init(&repo->commitcache,
569 GOT_OBJECT_CACHE_TYPE_COMMIT);
570 if (err)
571 goto done;
572 err = got_object_cache_init(&repo->tagcache,
573 GOT_OBJECT_CACHE_TYPE_TAG);
574 if (err)
575 goto done;
577 path = realpath(abspath, NULL);
578 if (path == NULL) {
579 err = got_error_from_errno2("realpath", abspath);
580 goto done;
583 do {
584 err = open_repo(repo, path);
585 if (err == NULL)
586 break;
587 if (err->code != GOT_ERR_NOT_GIT_REPO)
588 break;
589 if (path[0] == '/' && path[1] == '\0') {
590 if (tried_root) {
591 err = got_error(GOT_ERR_NOT_GIT_REPO);
592 goto done;
594 tried_root = 1;
596 path = dirname(path);
597 if (path == NULL) {
598 err = got_error_from_errno2("dirname", path);
599 goto done;
601 } while (path);
603 err = read_gitconfig(repo, global_gitconfig_path);
604 if (err)
605 goto done;
606 if (repo->gitconfig_repository_format_version != 0)
607 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
608 done:
609 if (err)
610 got_repo_close(repo);
611 else
612 *repop = repo;
613 free(abspath);
614 return err;
617 const struct got_error *
618 got_repo_close(struct got_repository *repo)
620 const struct got_error *err = NULL, *child_err;
621 int i;
623 for (i = 0; i < nitems(repo->packidx_cache); i++) {
624 if (repo->packidx_cache[i] == NULL)
625 break;
626 got_packidx_close(repo->packidx_cache[i]);
629 for (i = 0; i < nitems(repo->packs); i++) {
630 if (repo->packs[i].path_packfile == NULL)
631 break;
632 got_pack_close(&repo->packs[i]);
635 free(repo->path);
636 free(repo->path_git_dir);
638 got_object_cache_close(&repo->objcache);
639 got_object_cache_close(&repo->treecache);
640 got_object_cache_close(&repo->commitcache);
641 got_object_cache_close(&repo->tagcache);
643 for (i = 0; i < nitems(repo->privsep_children); i++) {
644 if (repo->privsep_children[i].imsg_fd == -1)
645 continue;
646 imsg_clear(repo->privsep_children[i].ibuf);
647 free(repo->privsep_children[i].ibuf);
648 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
649 child_err = got_privsep_wait_for_child(
650 repo->privsep_children[i].pid);
651 if (child_err && err == NULL)
652 err = child_err;
653 if (close(repo->privsep_children[i].imsg_fd) != 0 &&
654 err == NULL)
655 err = got_error_from_errno("close");
658 free(repo->gitconfig_author_name);
659 free(repo->gitconfig_author_email);
660 for (i = 0; i < repo->ngitconfig_remotes; i++) {
661 free(repo->gitconfig_remotes[i].name);
662 free(repo->gitconfig_remotes[i].url);
664 free(repo->gitconfig_remotes);
665 free(repo);
667 return err;
670 const struct got_error *
671 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
672 const char *input_path, int check_disk)
674 const struct got_error *err = NULL;
675 const char *repo_abspath = NULL;
676 size_t repolen, len;
677 char *canonpath, *path = NULL;
679 *in_repo_path = NULL;
681 canonpath = strdup(input_path);
682 if (canonpath == NULL) {
683 err = got_error_from_errno("strdup");
684 goto done;
686 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
687 if (err)
688 goto done;
690 repo_abspath = got_repo_get_path(repo);
692 if (!check_disk || canonpath[0] == '\0') {
693 path = strdup(canonpath);
694 if (path == NULL) {
695 err = got_error_from_errno("strdup");
696 goto done;
698 } else {
699 path = realpath(canonpath, NULL);
700 if (path == NULL) {
701 if (errno != ENOENT) {
702 err = got_error_from_errno2("realpath",
703 canonpath);
704 goto done;
706 /*
707 * Path is not on disk.
708 * Assume it is already relative to repository root.
709 */
710 path = strdup(canonpath);
711 if (path == NULL) {
712 err = got_error_from_errno("strdup");
713 goto done;
717 repolen = strlen(repo_abspath);
718 len = strlen(path);
721 if (strcmp(path, repo_abspath) == 0) {
722 free(path);
723 path = strdup("");
724 if (path == NULL) {
725 err = got_error_from_errno("strdup");
726 goto done;
728 } else if (len > repolen &&
729 got_path_is_child(path, repo_abspath, repolen)) {
730 /* Matched an on-disk path inside repository. */
731 if (got_repo_is_bare(repo)) {
732 /*
733 * Matched an on-disk path inside repository
734 * database. Treat as repository-relative.
735 */
736 } else {
737 char *child;
738 /* Strip common prefix with repository path. */
739 err = got_path_skip_common_ancestor(&child,
740 repo_abspath, path);
741 if (err)
742 goto done;
743 free(path);
744 path = child;
746 } else {
747 /*
748 * Matched unrelated on-disk path.
749 * Treat it as repository-relative.
750 */
754 /* Make in-repository path absolute */
755 if (path[0] != '/') {
756 char *abspath;
757 if (asprintf(&abspath, "/%s", path) == -1) {
758 err = got_error_from_errno("asprintf");
759 goto done;
761 free(path);
762 path = abspath;
765 done:
766 free(canonpath);
767 if (err)
768 free(path);
769 else
770 *in_repo_path = path;
771 return err;
774 static const struct got_error *
775 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
776 const char *path_packidx)
778 const struct got_error *err = NULL;
779 int i;
781 for (i = 0; i < nitems(repo->packidx_cache); i++) {
782 if (repo->packidx_cache[i] == NULL)
783 break;
784 if (strcmp(repo->packidx_cache[i]->path_packidx,
785 path_packidx) == 0) {
786 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
789 if (i == nitems(repo->packidx_cache)) {
790 err = got_packidx_close(repo->packidx_cache[i - 1]);
791 if (err)
792 return err;
795 /*
796 * Insert the new pack index at the front so it will
797 * be searched first in the future.
798 */
799 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
800 sizeof(repo->packidx_cache) -
801 sizeof(repo->packidx_cache[0]));
802 repo->packidx_cache[0] = packidx;
804 return NULL;
807 static int
808 is_packidx_filename(const char *name, size_t len)
810 if (len != GOT_PACKIDX_NAMELEN)
811 return 0;
813 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
814 return 0;
816 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
817 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
818 return 0;
820 return 1;
823 const struct got_error *
824 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
825 struct got_repository *repo, struct got_object_id *id)
827 const struct got_error *err;
828 char *path_packdir;
829 DIR *packdir;
830 struct dirent *dent;
831 char *path_packidx;
832 int i;
834 /* Search pack index cache. */
835 for (i = 0; i < nitems(repo->packidx_cache); i++) {
836 if (repo->packidx_cache[i] == NULL)
837 break;
838 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
839 if (*idx != -1) {
840 *packidx = repo->packidx_cache[i];
841 /*
842 * Move this cache entry to the front. Repeatedly
843 * searching a wrong pack index can be expensive.
844 */
845 if (i > 0) {
846 struct got_packidx *p;
847 p = repo->packidx_cache[0];
848 repo->packidx_cache[0] = *packidx;
849 repo->packidx_cache[i] = p;
851 return NULL;
854 /* No luck. Search the filesystem. */
856 path_packdir = got_repo_get_path_objects_pack(repo);
857 if (path_packdir == NULL)
858 return got_error_from_errno("got_repo_get_path_objects_pack");
860 packdir = opendir(path_packdir);
861 if (packdir == NULL) {
862 if (errno == ENOENT)
863 err = got_error_no_obj(id);
864 else
865 err = got_error_from_errno2("opendir", path_packdir);
866 goto done;
869 while ((dent = readdir(packdir)) != NULL) {
870 int is_cached = 0;
872 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
873 continue;
875 if (asprintf(&path_packidx, "%s/%s", path_packdir,
876 dent->d_name) == -1) {
877 err = got_error_from_errno("asprintf");
878 goto done;
881 for (i = 0; i < nitems(repo->packidx_cache); i++) {
882 if (repo->packidx_cache[i] == NULL)
883 break;
884 if (strcmp(repo->packidx_cache[i]->path_packidx,
885 path_packidx) == 0) {
886 is_cached = 1;
887 break;
890 if (is_cached) {
891 free(path_packidx);
892 continue; /* already searched */
895 err = got_packidx_open(packidx, path_packidx, 0);
896 if (err) {
897 free(path_packidx);
898 goto done;
901 err = cache_packidx(repo, *packidx, path_packidx);
902 free(path_packidx);
903 if (err)
904 goto done;
906 *idx = got_packidx_get_object_idx(*packidx, id);
907 if (*idx != -1) {
908 err = NULL; /* found the object */
909 goto done;
913 err = got_error_no_obj(id);
914 done:
915 free(path_packdir);
916 if (packdir && closedir(packdir) != 0 && err == NULL)
917 err = got_error_from_errno("closedir");
918 return err;
921 static const struct got_error *
922 read_packfile_hdr(int fd, struct got_packidx *packidx)
924 const struct got_error *err = NULL;
925 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
926 struct got_packfile_hdr hdr;
927 ssize_t n;
929 n = read(fd, &hdr, sizeof(hdr));
930 if (n < 0)
931 return got_error_from_errno("read");
932 if (n != sizeof(hdr))
933 return got_error(GOT_ERR_BAD_PACKFILE);
935 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
936 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
937 betoh32(hdr.nobjects) != totobj)
938 err = got_error(GOT_ERR_BAD_PACKFILE);
940 return err;
943 static const struct got_error *
944 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
946 const struct got_error *err = NULL;
948 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
949 if (*fd == -1)
950 return got_error_from_errno2("open", path_packfile);
952 if (packidx) {
953 err = read_packfile_hdr(*fd, packidx);
954 if (err) {
955 close(*fd);
956 *fd = -1;
960 return err;
963 const struct got_error *
964 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
965 const char *path_packfile, struct got_packidx *packidx)
967 const struct got_error *err = NULL;
968 struct got_pack *pack = NULL;
969 struct stat sb;
970 int i;
972 if (packp)
973 *packp = NULL;
975 for (i = 0; i < nitems(repo->packs); i++) {
976 pack = &repo->packs[i];
977 if (pack->path_packfile == NULL)
978 break;
979 if (strcmp(pack->path_packfile, path_packfile) == 0)
980 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
983 if (i == nitems(repo->packs) - 1) {
984 err = got_pack_close(&repo->packs[i - 1]);
985 if (err)
986 return err;
987 memmove(&repo->packs[1], &repo->packs[0],
988 sizeof(repo->packs) - sizeof(repo->packs[0]));
989 i = 0;
992 pack = &repo->packs[i];
994 pack->path_packfile = strdup(path_packfile);
995 if (pack->path_packfile == NULL) {
996 err = got_error_from_errno("strdup");
997 goto done;
1000 err = open_packfile(&pack->fd, path_packfile, packidx);
1001 if (err)
1002 goto done;
1004 if (fstat(pack->fd, &sb) != 0) {
1005 err = got_error_from_errno("fstat");
1006 goto done;
1008 pack->filesize = sb.st_size;
1010 pack->privsep_child = NULL;
1012 #ifndef GOT_PACK_NO_MMAP
1013 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1014 pack->fd, 0);
1015 if (pack->map == MAP_FAILED) {
1016 if (errno != ENOMEM) {
1017 err = got_error_from_errno("mmap");
1018 goto done;
1020 pack->map = NULL; /* fall back to read(2) */
1022 #endif
1023 done:
1024 if (err) {
1025 if (pack) {
1026 free(pack->path_packfile);
1027 memset(pack, 0, sizeof(*pack));
1029 } else if (packp)
1030 *packp = pack;
1031 return err;
1034 struct got_pack *
1035 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1037 struct got_pack *pack = NULL;
1038 int i;
1040 for (i = 0; i < nitems(repo->packs); i++) {
1041 pack = &repo->packs[i];
1042 if (pack->path_packfile == NULL)
1043 break;
1044 if (strcmp(pack->path_packfile, path_packfile) == 0)
1045 return pack;
1048 return NULL;
1051 const struct got_error *
1052 got_repo_init(const char *repo_path)
1054 const struct got_error *err = NULL;
1055 const char *dirnames[] = {
1056 GOT_OBJECTS_DIR,
1057 GOT_OBJECTS_PACK_DIR,
1058 GOT_REFS_DIR,
1060 const char *description_str = "Unnamed repository; "
1061 "edit this file 'description' to name the repository.";
1062 const char *headref_str = "ref: refs/heads/main";
1063 const char *gitconfig_str = "[core]\n"
1064 "\trepositoryformatversion = 0\n"
1065 "\tfilemode = true\n"
1066 "\tbare = true\n";
1067 char *path;
1068 int i;
1070 if (!got_path_dir_is_empty(repo_path))
1071 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1073 for (i = 0; i < nitems(dirnames); i++) {
1074 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1075 return got_error_from_errno("asprintf");
1077 err = got_path_mkdir(path);
1078 free(path);
1079 if (err)
1080 return err;
1083 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1084 return got_error_from_errno("asprintf");
1085 err = got_path_create_file(path, description_str);
1086 free(path);
1087 if (err)
1088 return err;
1090 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1091 return got_error_from_errno("asprintf");
1092 err = got_path_create_file(path, headref_str);
1093 free(path);
1094 if (err)
1095 return err;
1097 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1098 return got_error_from_errno("asprintf");
1099 err = got_path_create_file(path, gitconfig_str);
1100 free(path);
1101 if (err)
1102 return err;
1104 return NULL;
1107 static const struct got_error *
1108 match_packed_object(struct got_object_id **unique_id,
1109 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1111 const struct got_error *err = NULL;
1112 char *path_packdir;
1113 DIR *packdir;
1114 struct dirent *dent;
1115 char *path_packidx;
1116 struct got_object_id_queue matched_ids;
1118 SIMPLEQ_INIT(&matched_ids);
1120 path_packdir = got_repo_get_path_objects_pack(repo);
1121 if (path_packdir == NULL)
1122 return got_error_from_errno("got_repo_get_path_objects_pack");
1124 packdir = opendir(path_packdir);
1125 if (packdir == NULL) {
1126 if (errno != ENOENT)
1127 err = got_error_from_errno2("opendir", path_packdir);
1128 goto done;
1131 while ((dent = readdir(packdir)) != NULL) {
1132 struct got_packidx *packidx;
1133 struct got_object_qid *qid;
1136 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1137 continue;
1139 if (asprintf(&path_packidx, "%s/%s", path_packdir,
1140 dent->d_name) == -1) {
1141 err = got_error_from_errno("asprintf");
1142 break;
1145 err = got_packidx_open(&packidx, path_packidx, 0);
1146 free(path_packidx);
1147 if (err)
1148 break;
1150 err = got_packidx_match_id_str_prefix(&matched_ids,
1151 packidx, id_str_prefix);
1152 if (err) {
1153 got_packidx_close(packidx);
1154 break;
1156 err = got_packidx_close(packidx);
1157 if (err)
1158 break;
1160 SIMPLEQ_FOREACH(qid, &matched_ids, entry) {
1161 if (obj_type != GOT_OBJ_TYPE_ANY) {
1162 int matched_type;
1163 err = got_object_get_type(&matched_type, repo,
1164 qid->id);
1165 if (err)
1166 goto done;
1167 if (matched_type != obj_type)
1168 continue;
1170 if (*unique_id == NULL) {
1171 *unique_id = got_object_id_dup(qid->id);
1172 if (*unique_id == NULL) {
1173 err = got_error_from_errno("malloc");
1174 goto done;
1176 } else {
1177 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1178 continue; /* packed multiple times */
1179 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1180 goto done;
1184 done:
1185 got_object_id_queue_free(&matched_ids);
1186 free(path_packdir);
1187 if (packdir && closedir(packdir) != 0 && err == NULL)
1188 err = got_error_from_errno("closedir");
1189 if (err) {
1190 free(*unique_id);
1191 *unique_id = NULL;
1193 return err;
1196 static const struct got_error *
1197 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1198 const char *object_dir, const char *id_str_prefix, int obj_type,
1199 struct got_repository *repo)
1201 const struct got_error *err = NULL;
1202 char *path;
1203 DIR *dir = NULL;
1204 struct dirent *dent;
1205 struct got_object_id id;
1207 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1208 err = got_error_from_errno("asprintf");
1209 goto done;
1212 dir = opendir(path);
1213 if (dir == NULL) {
1214 if (errno == ENOENT) {
1215 err = NULL;
1216 goto done;
1218 err = got_error_from_errno2("opendir", path);
1219 goto done;
1221 while ((dent = readdir(dir)) != NULL) {
1222 char *id_str;
1223 int cmp;
1225 if (strcmp(dent->d_name, ".") == 0 ||
1226 strcmp(dent->d_name, "..") == 0)
1227 continue;
1229 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1230 err = got_error_from_errno("asprintf");
1231 goto done;
1234 if (!got_parse_sha1_digest(id.sha1, id_str))
1235 continue;
1238 * Directory entries do not necessarily appear in
1239 * sorted order, so we must iterate over all of them.
1241 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1242 if (cmp != 0) {
1243 free(id_str);
1244 continue;
1247 if (*unique_id == NULL) {
1248 if (obj_type != GOT_OBJ_TYPE_ANY) {
1249 int matched_type;
1250 err = got_object_get_type(&matched_type, repo,
1251 &id);
1252 if (err)
1253 goto done;
1254 if (matched_type != obj_type)
1255 continue;
1257 *unique_id = got_object_id_dup(&id);
1258 if (*unique_id == NULL) {
1259 err = got_error_from_errno("got_object_id_dup");
1260 free(id_str);
1261 goto done;
1263 } else {
1264 if (got_object_id_cmp(*unique_id, &id) == 0)
1265 continue; /* both packed and loose */
1266 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1267 free(id_str);
1268 goto done;
1271 done:
1272 if (dir && closedir(dir) != 0 && err == NULL)
1273 err = got_error_from_errno("closedir");
1274 if (err) {
1275 free(*unique_id);
1276 *unique_id = NULL;
1278 free(path);
1279 return err;
1282 const struct got_error *
1283 got_repo_match_object_id_prefix(struct got_object_id **id,
1284 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1286 const struct got_error *err = NULL;
1287 char *path_objects = got_repo_get_path_objects(repo);
1288 char *object_dir = NULL;
1289 size_t len;
1290 int i;
1292 *id = NULL;
1294 for (i = 0; i < strlen(id_str_prefix); i++) {
1295 if (isxdigit((unsigned char)id_str_prefix[i]))
1296 continue;
1297 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1300 len = strlen(id_str_prefix);
1301 if (len >= 2) {
1302 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1303 if (err)
1304 goto done;
1305 object_dir = strndup(id_str_prefix, 2);
1306 if (object_dir == NULL) {
1307 err = got_error_from_errno("strdup");
1308 goto done;
1310 err = match_loose_object(id, path_objects, object_dir,
1311 id_str_prefix, obj_type, repo);
1312 } else if (len == 1) {
1313 int i;
1314 for (i = 0; i < 0xf; i++) {
1315 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1316 == -1) {
1317 err = got_error_from_errno("asprintf");
1318 goto done;
1320 err = match_packed_object(id, repo, object_dir,
1321 obj_type);
1322 if (err)
1323 goto done;
1324 err = match_loose_object(id, path_objects, object_dir,
1325 id_str_prefix, obj_type, repo);
1326 if (err)
1327 goto done;
1329 } else {
1330 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1331 goto done;
1333 done:
1334 free(object_dir);
1335 if (err) {
1336 free(*id);
1337 *id = NULL;
1338 } else if (*id == NULL)
1339 err = got_error(GOT_ERR_NO_OBJ);
1341 return err;
1344 const struct got_error *
1345 got_repo_match_object_id(struct got_object_id **id, char **label,
1346 const char *id_str, int obj_type, int resolve_tags,
1347 struct got_repository *repo)
1349 const struct got_error *err;
1350 struct got_tag_object *tag;
1351 struct got_reference *ref = NULL;
1353 *id = NULL;
1354 if (label)
1355 *label = NULL;
1357 if (resolve_tags) {
1358 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
1359 repo);
1360 if (err == NULL) {
1361 *id = got_object_id_dup(
1362 got_object_tag_get_object_id(tag));
1363 if (*id == NULL)
1364 err = got_error_from_errno("got_object_id_dup");
1365 else if (label && asprintf(label, "refs/tags/%s",
1366 got_object_tag_get_name(tag)) == -1) {
1367 err = got_error_from_errno("asprintf");
1368 free(*id);
1369 *id = NULL;
1371 got_object_tag_close(tag);
1372 return err;
1373 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1374 err->code != GOT_ERR_NO_OBJ)
1375 return err;
1378 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1379 if (err) {
1380 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1381 return err;
1382 err = got_ref_open(&ref, repo, id_str, 0);
1383 if (err != NULL)
1384 goto done;
1385 if (label) {
1386 *label = strdup(got_ref_get_name(ref));
1387 if (*label == NULL) {
1388 err = got_error_from_errno("strdup");
1389 goto done;
1392 err = got_ref_resolve(id, repo, ref);
1393 } else if (label) {
1394 err = got_object_id_str(label, *id);
1395 if (*label == NULL) {
1396 err = got_error_from_errno("strdup");
1397 goto done;
1400 done:
1401 if (ref)
1402 got_ref_close(ref);
1403 return err;
1406 const struct got_error *
1407 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1408 int obj_type, struct got_repository *repo)
1410 const struct got_error *err;
1411 struct got_reflist_head refs;
1412 struct got_reflist_entry *re;
1413 struct got_object_id *tag_id;
1415 SIMPLEQ_INIT(&refs);
1416 *tag = NULL;
1418 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_by_name, NULL);
1419 if (err)
1420 return err;
1422 SIMPLEQ_FOREACH(re, &refs, entry) {
1423 const char *refname;
1424 refname = got_ref_get_name(re->ref);
1425 if (got_ref_is_symbolic(re->ref))
1426 continue;
1427 refname += strlen("refs/tags/");
1428 if (strcmp(refname, name) != 0)
1429 continue;
1430 err = got_ref_resolve(&tag_id, repo, re->ref);
1431 if (err)
1432 break;
1433 err = got_object_open_as_tag(tag, repo, tag_id);
1434 free(tag_id);
1435 if (err)
1436 break;
1437 if (obj_type == GOT_OBJ_TYPE_ANY ||
1438 got_object_tag_get_object_type(*tag) == obj_type)
1439 break;
1440 got_object_tag_close(*tag);
1441 *tag = NULL;
1444 got_ref_list_free(&refs);
1445 if (err == NULL && *tag == NULL)
1446 err = got_error(GOT_ERR_NO_OBJ);
1447 return err;
1450 static const struct got_error *
1451 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1452 const char *name, mode_t mode, struct got_object_id *blob_id)
1454 const struct got_error *err = NULL;
1456 *new_te = NULL;
1458 *new_te = calloc(1, sizeof(**new_te));
1459 if (*new_te == NULL)
1460 return got_error_from_errno("calloc");
1462 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1463 sizeof((*new_te)->name)) {
1464 err = got_error(GOT_ERR_NO_SPACE);
1465 goto done;
1468 (*new_te)->mode = S_IFREG | (mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
1469 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1470 done:
1471 if (err && *new_te) {
1472 free(*new_te);
1473 *new_te = NULL;
1475 return err;
1478 static const struct got_error *
1479 import_file(struct got_tree_entry **new_te, struct dirent *de,
1480 const char *path, struct got_repository *repo)
1482 const struct got_error *err;
1483 struct got_object_id *blob_id = NULL;
1484 char *filepath;
1485 struct stat sb;
1487 if (asprintf(&filepath, "%s%s%s", path,
1488 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1489 return got_error_from_errno("asprintf");
1491 if (lstat(filepath, &sb) != 0) {
1492 err = got_error_from_errno2("lstat", path);
1493 goto done;
1496 err = got_object_blob_create(&blob_id, filepath, repo);
1497 if (err)
1498 goto done;
1500 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1501 blob_id);
1502 done:
1503 free(filepath);
1504 if (err)
1505 free(blob_id);
1506 return err;
1509 static const struct got_error *
1510 insert_tree_entry(struct got_tree_entry *new_te,
1511 struct got_pathlist_head *paths)
1513 const struct got_error *err = NULL;
1514 struct got_pathlist_entry *new_pe;
1516 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1517 if (err)
1518 return err;
1519 if (new_pe == NULL)
1520 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1521 return NULL;
1524 static const struct got_error *write_tree(struct got_object_id **,
1525 const char *, struct got_pathlist_head *, struct got_repository *,
1526 got_repo_import_cb progress_cb, void *progress_arg);
1528 static const struct got_error *
1529 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1530 const char *path, struct got_pathlist_head *ignores,
1531 struct got_repository *repo,
1532 got_repo_import_cb progress_cb, void *progress_arg)
1534 const struct got_error *err;
1535 struct got_object_id *id = NULL;
1536 char *subdirpath;
1538 if (asprintf(&subdirpath, "%s%s%s", path,
1539 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1540 return got_error_from_errno("asprintf");
1542 (*new_te) = calloc(1, sizeof(**new_te));
1543 if (*new_te == NULL)
1544 return got_error_from_errno("calloc");
1545 (*new_te)->mode = S_IFDIR;
1546 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1547 sizeof((*new_te)->name)) {
1548 err = got_error(GOT_ERR_NO_SPACE);
1549 goto done;
1551 err = write_tree(&id, subdirpath, ignores, repo,
1552 progress_cb, progress_arg);
1553 if (err)
1554 goto done;
1555 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1557 done:
1558 free(id);
1559 free(subdirpath);
1560 if (err) {
1561 free(*new_te);
1562 *new_te = NULL;
1564 return err;
1567 static const struct got_error *
1568 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1569 struct got_pathlist_head *ignores, struct got_repository *repo,
1570 got_repo_import_cb progress_cb, void *progress_arg)
1572 const struct got_error *err = NULL;
1573 DIR *dir;
1574 struct dirent *de;
1575 int nentries;
1576 struct got_tree_entry *new_te = NULL;
1577 struct got_pathlist_head paths;
1578 struct got_pathlist_entry *pe;
1580 *new_tree_id = NULL;
1582 TAILQ_INIT(&paths);
1584 dir = opendir(path_dir);
1585 if (dir == NULL) {
1586 err = got_error_from_errno2("opendir", path_dir);
1587 goto done;
1590 nentries = 0;
1591 while ((de = readdir(dir)) != NULL) {
1592 int ignore = 0;
1594 if (strcmp(de->d_name, ".") == 0 ||
1595 strcmp(de->d_name, "..") == 0)
1596 continue;
1598 TAILQ_FOREACH(pe, ignores, entry) {
1599 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1600 ignore = 1;
1601 break;
1604 if (ignore)
1605 continue;
1606 if (de->d_type == DT_DIR) {
1607 err = import_subdir(&new_te, de, path_dir,
1608 ignores, repo, progress_cb, progress_arg);
1609 if (err) {
1610 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1611 goto done;
1612 err = NULL;
1613 continue;
1615 } else if (de->d_type == DT_REG) {
1616 err = import_file(&new_te, de, path_dir, repo);
1617 if (err)
1618 goto done;
1619 } else
1620 continue;
1622 err = insert_tree_entry(new_te, &paths);
1623 if (err)
1624 goto done;
1625 nentries++;
1628 if (TAILQ_EMPTY(&paths)) {
1629 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1630 goto done;
1633 TAILQ_FOREACH(pe, &paths, entry) {
1634 struct got_tree_entry *te = pe->data;
1635 char *path;
1636 if (!S_ISREG(te->mode))
1637 continue;
1638 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1639 err = got_error_from_errno("asprintf");
1640 goto done;
1642 err = (*progress_cb)(progress_arg, path);
1643 free(path);
1644 if (err)
1645 goto done;
1648 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1649 done:
1650 if (dir)
1651 closedir(dir);
1652 got_pathlist_free(&paths);
1653 return err;
1656 const struct got_error *
1657 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1658 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1659 struct got_repository *repo, got_repo_import_cb progress_cb,
1660 void *progress_arg)
1662 const struct got_error *err;
1663 struct got_object_id *new_tree_id;
1665 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1666 progress_cb, progress_arg);
1667 if (err)
1668 return err;
1670 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1671 author, time(NULL), author, time(NULL), logmsg, repo);
1672 free(new_tree_id);
1673 return err;