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 int
117 got_repo_is_bare(struct got_repository *repo)
119 return (strcmp(repo->path, repo->path_git_dir) == 0);
122 static char *
123 get_path_git_child(struct got_repository *repo, const char *basename)
125 char *path_child;
127 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
128 basename) == -1)
129 return NULL;
131 return path_child;
134 char *
135 got_repo_get_path_objects(struct got_repository *repo)
137 return get_path_git_child(repo, GOT_OBJECTS_DIR);
140 char *
141 got_repo_get_path_objects_pack(struct got_repository *repo)
143 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
146 char *
147 got_repo_get_path_refs(struct got_repository *repo)
149 return get_path_git_child(repo, GOT_REFS_DIR);
152 char *
153 got_repo_get_path_packed_refs(struct got_repository *repo)
155 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
158 static char *
159 get_path_head(struct got_repository *repo)
161 return get_path_git_child(repo, GOT_HEAD_FILE);
164 static const struct got_error *
165 get_path_gitconfig(char **p, struct got_repository *repo)
167 *p = get_path_git_child(repo, GOT_GITCONFIG);
168 if (*p == NULL)
169 return got_error_from_errno("asprintf");
170 return NULL;
173 void
174 got_repo_get_gitconfig_remotes(int *nremotes, struct got_remote_repo **remotes,
175 struct got_repository *repo)
177 *nremotes = repo->ngitconfig_remotes;
178 *remotes = repo->gitconfig_remotes;
181 static int
182 is_git_repo(struct got_repository *repo)
184 const char *path_git = got_repo_get_path_git_dir(repo);
185 char *path_objects = got_repo_get_path_objects(repo);
186 char *path_refs = got_repo_get_path_refs(repo);
187 char *path_head = get_path_head(repo);
188 int ret = 0;
189 struct stat sb;
190 struct got_reference *head_ref;
192 if (lstat(path_git, &sb) == -1)
193 goto done;
194 if (!S_ISDIR(sb.st_mode))
195 goto done;
197 if (lstat(path_objects, &sb) == -1)
198 goto done;
199 if (!S_ISDIR(sb.st_mode))
200 goto done;
202 if (lstat(path_refs, &sb) == -1)
203 goto done;
204 if (!S_ISDIR(sb.st_mode))
205 goto done;
207 if (lstat(path_head, &sb) == -1)
208 goto done;
209 if (!S_ISREG(sb.st_mode))
210 goto done;
212 /* Check if the HEAD reference can be opened. */
213 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
214 goto done;
215 got_ref_close(head_ref);
217 ret = 1;
218 done:
219 free(path_objects);
220 free(path_refs);
221 free(path_head);
222 return ret;
226 const struct got_error *
227 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
228 struct got_object *obj)
230 #ifndef GOT_NO_OBJ_CACHE
231 const struct got_error *err = NULL;
232 err = got_object_cache_add(&repo->objcache, id, obj);
233 if (err) {
234 if (err->code == GOT_ERR_OBJ_EXISTS ||
235 err->code == GOT_ERR_OBJ_TOO_LARGE)
236 err = NULL;
237 return err;
239 obj->refcnt++;
240 #endif
241 return NULL;
244 struct got_object *
245 got_repo_get_cached_object(struct got_repository *repo,
246 struct got_object_id *id)
248 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
251 const struct got_error *
252 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
253 struct got_tree_object *tree)
255 #ifndef GOT_NO_OBJ_CACHE
256 const struct got_error *err = NULL;
257 err = got_object_cache_add(&repo->treecache, id, tree);
258 if (err) {
259 if (err->code == GOT_ERR_OBJ_EXISTS ||
260 err->code == GOT_ERR_OBJ_TOO_LARGE)
261 err = NULL;
262 return err;
264 tree->refcnt++;
265 #endif
266 return NULL;
269 struct got_tree_object *
270 got_repo_get_cached_tree(struct got_repository *repo,
271 struct got_object_id *id)
273 return (struct got_tree_object *)got_object_cache_get(
274 &repo->treecache, id);
277 const struct got_error *
278 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
279 struct got_commit_object *commit)
281 #ifndef GOT_NO_OBJ_CACHE
282 const struct got_error *err = NULL;
283 err = got_object_cache_add(&repo->commitcache, id, commit);
284 if (err) {
285 if (err->code == GOT_ERR_OBJ_EXISTS ||
286 err->code == GOT_ERR_OBJ_TOO_LARGE)
287 err = NULL;
288 return err;
290 commit->refcnt++;
291 #endif
292 return NULL;
295 struct got_commit_object *
296 got_repo_get_cached_commit(struct got_repository *repo,
297 struct got_object_id *id)
299 return (struct got_commit_object *)got_object_cache_get(
300 &repo->commitcache, id);
303 const struct got_error *
304 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
305 struct got_tag_object *tag)
307 #ifndef GOT_NO_OBJ_CACHE
308 const struct got_error *err = NULL;
309 err = got_object_cache_add(&repo->tagcache, id, tag);
310 if (err) {
311 if (err->code == GOT_ERR_OBJ_EXISTS ||
312 err->code == GOT_ERR_OBJ_TOO_LARGE)
313 err = NULL;
314 return err;
316 tag->refcnt++;
317 #endif
318 return NULL;
321 struct got_tag_object *
322 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
324 return (struct got_tag_object *)got_object_cache_get(
325 &repo->tagcache, id);
328 const struct got_error *
329 open_repo(struct got_repository *repo, const char *path)
331 const struct got_error *err = NULL;
333 /* bare git repository? */
334 repo->path_git_dir = strdup(path);
335 if (repo->path_git_dir == NULL)
336 return got_error_from_errno("strdup");
337 if (is_git_repo(repo)) {
338 repo->path = strdup(repo->path_git_dir);
339 if (repo->path == NULL) {
340 err = got_error_from_errno("strdup");
341 goto done;
343 return NULL;
346 /* git repository with working tree? */
347 free(repo->path_git_dir);
348 repo->path_git_dir = NULL;
349 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
350 err = got_error_from_errno("asprintf");
351 goto done;
353 if (is_git_repo(repo)) {
354 repo->path = strdup(path);
355 if (repo->path == NULL) {
356 err = got_error_from_errno("strdup");
357 goto done;
359 return NULL;
362 err = got_error(GOT_ERR_NOT_GIT_REPO);
363 done:
364 if (err) {
365 free(repo->path);
366 repo->path = NULL;
367 free(repo->path_git_dir);
368 repo->path_git_dir = NULL;
370 return err;
373 static const struct got_error *
374 parse_gitconfig_file(int *gitconfig_repository_format_version,
375 char **gitconfig_author_name, char **gitconfig_author_email,
376 struct got_remote_repo **remotes, int *nremotes,
377 const char *gitconfig_path)
379 const struct got_error *err = NULL, *child_err = NULL;
380 int fd = -1;
381 int imsg_fds[2] = { -1, -1 };
382 pid_t pid;
383 struct imsgbuf *ibuf;
385 *gitconfig_repository_format_version = 0;
386 *gitconfig_author_name = NULL;
387 *gitconfig_author_email = NULL;
389 fd = open(gitconfig_path, O_RDONLY);
390 if (fd == -1) {
391 if (errno == ENOENT)
392 return NULL;
393 return got_error_from_errno2("open", gitconfig_path);
396 ibuf = calloc(1, sizeof(*ibuf));
397 if (ibuf == NULL) {
398 err = got_error_from_errno("calloc");
399 goto done;
402 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
403 err = got_error_from_errno("socketpair");
404 goto done;
407 pid = fork();
408 if (pid == -1) {
409 err = got_error_from_errno("fork");
410 goto done;
411 } else if (pid == 0) {
412 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
413 gitconfig_path);
414 /* not reached */
417 if (close(imsg_fds[1]) == -1) {
418 err = got_error_from_errno("close");
419 goto done;
421 imsg_fds[1] = -1;
422 imsg_init(ibuf, imsg_fds[0]);
424 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
425 if (err)
426 goto done;
427 fd = -1;
429 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
430 if (err)
431 goto done;
433 err = got_privsep_recv_gitconfig_int(
434 gitconfig_repository_format_version, ibuf);
435 if (err)
436 goto done;
438 err = got_privsep_send_gitconfig_author_name_req(ibuf);
439 if (err)
440 goto done;
442 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
443 if (err)
444 goto done;
446 err = got_privsep_send_gitconfig_author_email_req(ibuf);
447 if (err)
448 goto done;
450 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
451 if (err)
452 goto done;
454 if (remotes && nremotes) {
455 err = got_privsep_send_gitconfig_remotes_req(ibuf);
456 if (err)
457 goto done;
459 err = got_privsep_recv_gitconfig_remotes(remotes,
460 nremotes, ibuf);
461 if (err)
462 goto done;
465 imsg_clear(ibuf);
466 err = got_privsep_send_stop(imsg_fds[0]);
467 child_err = got_privsep_wait_for_child(pid);
468 if (child_err && err == NULL)
469 err = child_err;
470 done:
471 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
472 err = got_error_from_errno("close");
473 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
474 err = got_error_from_errno("close");
475 if (fd != -1 && close(fd) == -1 && err == NULL)
476 err = got_error_from_errno2("close", gitconfig_path);
477 free(ibuf);
478 return err;
481 static const struct got_error *
482 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
484 const struct got_error *err = NULL;
485 char *repo_gitconfig_path = NULL;
487 if (global_gitconfig_path) {
488 /* Read settings from ~/.gitconfig. */
489 int dummy_repo_version;
490 err = parse_gitconfig_file(&dummy_repo_version,
491 &repo->global_gitconfig_author_name,
492 &repo->global_gitconfig_author_email,
493 NULL, NULL, global_gitconfig_path);
494 if (err)
495 return err;
498 /* Read repository's .git/config file. */
499 err = get_path_gitconfig(&repo_gitconfig_path, repo);
500 if (err)
501 return err;
503 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
504 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
505 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
506 repo_gitconfig_path);
507 if (err)
508 goto done;
509 done:
510 free(repo_gitconfig_path);
511 return err;
514 const struct got_error *
515 got_repo_open(struct got_repository **repop, const char *path,
516 const char *global_gitconfig_path)
518 struct got_repository *repo = NULL;
519 const struct got_error *err = NULL;
520 char *abspath;
521 int i, tried_root = 0;
523 *repop = NULL;
525 if (got_path_is_absolute(path))
526 abspath = strdup(path);
527 else
528 abspath = got_path_get_absolute(path);
529 if (abspath == NULL)
530 return got_error(GOT_ERR_BAD_PATH);
532 repo = calloc(1, sizeof(*repo));
533 if (repo == NULL) {
534 err = got_error_from_errno("calloc");
535 goto done;
538 for (i = 0; i < nitems(repo->privsep_children); i++) {
539 memset(&repo->privsep_children[i], 0,
540 sizeof(repo->privsep_children[0]));
541 repo->privsep_children[i].imsg_fd = -1;
544 err = got_object_cache_init(&repo->objcache,
545 GOT_OBJECT_CACHE_TYPE_OBJ);
546 if (err)
547 goto done;
548 err = got_object_cache_init(&repo->treecache,
549 GOT_OBJECT_CACHE_TYPE_TREE);
550 if (err)
551 goto done;
552 err = got_object_cache_init(&repo->commitcache,
553 GOT_OBJECT_CACHE_TYPE_COMMIT);
554 if (err)
555 goto done;
556 err = got_object_cache_init(&repo->tagcache,
557 GOT_OBJECT_CACHE_TYPE_TAG);
558 if (err)
559 goto done;
561 path = realpath(abspath, NULL);
562 if (path == NULL) {
563 err = got_error_from_errno2("realpath", abspath);
564 goto done;
567 do {
568 err = open_repo(repo, path);
569 if (err == NULL)
570 break;
571 if (err->code != GOT_ERR_NOT_GIT_REPO)
572 break;
573 if (path[0] == '/' && path[1] == '\0') {
574 if (tried_root) {
575 err = got_error(GOT_ERR_NOT_GIT_REPO);
576 goto done;
578 tried_root = 1;
580 path = dirname(path);
581 if (path == NULL) {
582 err = got_error_from_errno2("dirname", path);
583 goto done;
585 } while (path);
587 err = read_gitconfig(repo, global_gitconfig_path);
588 if (err)
589 goto done;
590 if (repo->gitconfig_repository_format_version != 0)
591 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
592 done:
593 if (err)
594 got_repo_close(repo);
595 else
596 *repop = repo;
597 free(abspath);
598 return err;
601 const struct got_error *
602 got_repo_close(struct got_repository *repo)
604 const struct got_error *err = NULL, *child_err;
605 int i;
607 for (i = 0; i < nitems(repo->packidx_cache); i++) {
608 if (repo->packidx_cache[i] == NULL)
609 break;
610 got_packidx_close(repo->packidx_cache[i]);
613 for (i = 0; i < nitems(repo->packs); i++) {
614 if (repo->packs[i].path_packfile == NULL)
615 break;
616 got_pack_close(&repo->packs[i]);
619 free(repo->path);
620 free(repo->path_git_dir);
622 got_object_cache_close(&repo->objcache);
623 got_object_cache_close(&repo->treecache);
624 got_object_cache_close(&repo->commitcache);
625 got_object_cache_close(&repo->tagcache);
627 for (i = 0; i < nitems(repo->privsep_children); i++) {
628 if (repo->privsep_children[i].imsg_fd == -1)
629 continue;
630 imsg_clear(repo->privsep_children[i].ibuf);
631 free(repo->privsep_children[i].ibuf);
632 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
633 child_err = got_privsep_wait_for_child(
634 repo->privsep_children[i].pid);
635 if (child_err && err == NULL)
636 err = child_err;
637 if (close(repo->privsep_children[i].imsg_fd) != 0 &&
638 err == NULL)
639 err = got_error_from_errno("close");
642 free(repo->gitconfig_author_name);
643 free(repo->gitconfig_author_email);
644 for (i = 0; i < repo->ngitconfig_remotes; i++) {
645 free(repo->gitconfig_remotes[i].name);
646 free(repo->gitconfig_remotes[i].url);
648 free(repo->gitconfig_remotes);
649 free(repo);
651 return err;
654 const struct got_error *
655 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
656 const char *input_path, int check_disk)
658 const struct got_error *err = NULL;
659 const char *repo_abspath = NULL;
660 size_t repolen, cwdlen, len;
661 char *cwd, *canonpath, *path = NULL;
663 *in_repo_path = NULL;
665 cwd = getcwd(NULL, 0);
666 if (cwd == NULL)
667 return got_error_from_errno("getcwd");
669 canonpath = strdup(input_path);
670 if (canonpath == NULL) {
671 err = got_error_from_errno("strdup");
672 goto done;
674 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
675 if (err)
676 goto done;
678 repo_abspath = got_repo_get_path(repo);
680 if (!check_disk || canonpath[0] == '\0') {
681 path = strdup(canonpath);
682 if (path == NULL) {
683 err = got_error_from_errno("strdup");
684 goto done;
686 } else {
687 int is_repo_child = 0, is_cwd_child = 0;
689 path = realpath(canonpath, NULL);
690 if (path == NULL) {
691 if (errno != ENOENT) {
692 err = got_error_from_errno2("realpath",
693 canonpath);
694 goto done;
696 /*
697 * Path is not on disk.
698 * Assume it is already relative to repository root.
699 */
700 path = strdup(canonpath);
701 if (path == NULL) {
702 err = got_error_from_errno("strdup");
703 goto done;
707 repolen = strlen(repo_abspath);
708 cwdlen = strlen(cwd);
709 len = strlen(path);
711 if (len > repolen && strncmp(path, repo_abspath, repolen) == 0)
712 is_repo_child = 1;
713 if (len > cwdlen && strncmp(path, cwd, cwdlen) == 0)
714 is_cwd_child = 1;
716 if (strcmp(path, repo_abspath) == 0) {
717 free(path);
718 path = strdup("");
719 if (path == NULL) {
720 err = got_error_from_errno("strdup");
721 goto done;
723 } else if (is_repo_child && is_cwd_child) {
724 char *child;
725 /* Strip common prefix with repository path. */
726 err = got_path_skip_common_ancestor(&child,
727 repo_abspath, path);
728 if (err)
729 goto done;
730 free(path);
731 path = child;
732 } else if (is_repo_child) {
733 /* Matched an on-disk path inside repository. */
734 if (got_repo_is_bare(repo)) {
735 /*
736 * Matched an on-disk path inside repository
737 * database. Treat as repository-relative.
738 */
739 } else {
740 char *child;
741 /* Strip common prefix with repository path. */
742 err = got_path_skip_common_ancestor(&child,
743 repo_abspath, path);
744 if (err)
745 goto done;
746 free(path);
747 path = child;
749 } else if (is_cwd_child) {
750 char *child;
751 /* Strip common prefix with cwd. */
752 err = got_path_skip_common_ancestor(&child, cwd,
753 path);
754 if (err)
755 goto done;
756 free(path);
757 path = child;
758 } else {
759 /*
760 * Matched unrelated on-disk path.
761 * Treat it as repository-relative.
762 */
766 /* Make in-repository path absolute */
767 if (path[0] != '/') {
768 char *abspath;
769 if (asprintf(&abspath, "/%s", path) == -1) {
770 err = got_error_from_errno("asprintf");
771 goto done;
773 free(path);
774 path = abspath;
777 done:
778 free(cwd);
779 free(canonpath);
780 if (err)
781 free(path);
782 else
783 *in_repo_path = path;
784 return err;
787 static const struct got_error *
788 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
789 const char *path_packidx)
791 const struct got_error *err = NULL;
792 int i;
794 for (i = 0; i < nitems(repo->packidx_cache); i++) {
795 if (repo->packidx_cache[i] == NULL)
796 break;
797 if (strcmp(repo->packidx_cache[i]->path_packidx,
798 path_packidx) == 0) {
799 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
802 if (i == nitems(repo->packidx_cache)) {
803 err = got_packidx_close(repo->packidx_cache[i - 1]);
804 if (err)
805 return err;
808 /*
809 * Insert the new pack index at the front so it will
810 * be searched first in the future.
811 */
812 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
813 sizeof(repo->packidx_cache) -
814 sizeof(repo->packidx_cache[0]));
815 repo->packidx_cache[0] = packidx;
817 return NULL;
820 static int
821 is_packidx_filename(const char *name, size_t len)
823 if (len != GOT_PACKIDX_NAMELEN)
824 return 0;
826 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
827 return 0;
829 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
830 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
831 return 0;
833 return 1;
836 const struct got_error *
837 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
838 struct got_repository *repo, struct got_object_id *id)
840 const struct got_error *err;
841 char *path_packdir;
842 DIR *packdir;
843 struct dirent *dent;
844 char *path_packidx;
845 int i;
847 /* Search pack index cache. */
848 for (i = 0; i < nitems(repo->packidx_cache); i++) {
849 if (repo->packidx_cache[i] == NULL)
850 break;
851 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
852 if (*idx != -1) {
853 *packidx = repo->packidx_cache[i];
854 /*
855 * Move this cache entry to the front. Repeatedly
856 * searching a wrong pack index can be expensive.
857 */
858 if (i > 0) {
859 struct got_packidx *p;
860 p = repo->packidx_cache[0];
861 repo->packidx_cache[0] = *packidx;
862 repo->packidx_cache[i] = p;
864 return NULL;
867 /* No luck. Search the filesystem. */
869 path_packdir = got_repo_get_path_objects_pack(repo);
870 if (path_packdir == NULL)
871 return got_error_from_errno("got_repo_get_path_objects_pack");
873 packdir = opendir(path_packdir);
874 if (packdir == NULL) {
875 if (errno == ENOENT)
876 err = got_error_no_obj(id);
877 else
878 err = got_error_from_errno2("opendir", path_packdir);
879 goto done;
882 while ((dent = readdir(packdir)) != NULL) {
883 int is_cached = 0;
885 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
886 continue;
888 if (asprintf(&path_packidx, "%s/%s", path_packdir,
889 dent->d_name) == -1) {
890 err = got_error_from_errno("asprintf");
891 goto done;
894 for (i = 0; i < nitems(repo->packidx_cache); i++) {
895 if (repo->packidx_cache[i] == NULL)
896 break;
897 if (strcmp(repo->packidx_cache[i]->path_packidx,
898 path_packidx) == 0) {
899 is_cached = 1;
900 break;
903 if (is_cached) {
904 free(path_packidx);
905 continue; /* already searched */
908 err = got_packidx_open(packidx, path_packidx, 0);
909 if (err) {
910 free(path_packidx);
911 goto done;
914 err = cache_packidx(repo, *packidx, path_packidx);
915 free(path_packidx);
916 if (err)
917 goto done;
919 *idx = got_packidx_get_object_idx(*packidx, id);
920 if (*idx != -1) {
921 err = NULL; /* found the object */
922 goto done;
926 err = got_error_no_obj(id);
927 done:
928 free(path_packdir);
929 if (packdir && closedir(packdir) != 0 && err == NULL)
930 err = got_error_from_errno("closedir");
931 return err;
934 static const struct got_error *
935 read_packfile_hdr(int fd, struct got_packidx *packidx)
937 const struct got_error *err = NULL;
938 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
939 struct got_packfile_hdr hdr;
940 ssize_t n;
942 n = read(fd, &hdr, sizeof(hdr));
943 if (n < 0)
944 return got_error_from_errno("read");
945 if (n != sizeof(hdr))
946 return got_error(GOT_ERR_BAD_PACKFILE);
948 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
949 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
950 betoh32(hdr.nobjects) != totobj)
951 err = got_error(GOT_ERR_BAD_PACKFILE);
953 return err;
956 static const struct got_error *
957 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
959 const struct got_error *err = NULL;
961 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
962 if (*fd == -1)
963 return got_error_from_errno2("open", path_packfile);
965 if (packidx) {
966 err = read_packfile_hdr(*fd, packidx);
967 if (err) {
968 close(*fd);
969 *fd = -1;
973 return err;
976 const struct got_error *
977 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
978 const char *path_packfile, struct got_packidx *packidx)
980 const struct got_error *err = NULL;
981 struct got_pack *pack = NULL;
982 struct stat sb;
983 int i;
985 if (packp)
986 *packp = NULL;
988 for (i = 0; i < nitems(repo->packs); i++) {
989 pack = &repo->packs[i];
990 if (pack->path_packfile == NULL)
991 break;
992 if (strcmp(pack->path_packfile, path_packfile) == 0)
993 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
996 if (i == nitems(repo->packs) - 1) {
997 err = got_pack_close(&repo->packs[i - 1]);
998 if (err)
999 return err;
1000 memmove(&repo->packs[1], &repo->packs[0],
1001 sizeof(repo->packs) - sizeof(repo->packs[0]));
1002 i = 0;
1005 pack = &repo->packs[i];
1007 pack->path_packfile = strdup(path_packfile);
1008 if (pack->path_packfile == NULL) {
1009 err = got_error_from_errno("strdup");
1010 goto done;
1013 err = open_packfile(&pack->fd, path_packfile, packidx);
1014 if (err)
1015 goto done;
1017 if (fstat(pack->fd, &sb) != 0) {
1018 err = got_error_from_errno("fstat");
1019 goto done;
1021 pack->filesize = sb.st_size;
1023 pack->privsep_child = NULL;
1025 #ifndef GOT_PACK_NO_MMAP
1026 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1027 pack->fd, 0);
1028 if (pack->map == MAP_FAILED) {
1029 if (errno != ENOMEM) {
1030 err = got_error_from_errno("mmap");
1031 goto done;
1033 pack->map = NULL; /* fall back to read(2) */
1035 #endif
1036 done:
1037 if (err) {
1038 if (pack) {
1039 free(pack->path_packfile);
1040 memset(pack, 0, sizeof(*pack));
1042 } else if (packp)
1043 *packp = pack;
1044 return err;
1047 struct got_pack *
1048 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1050 struct got_pack *pack = NULL;
1051 int i;
1053 for (i = 0; i < nitems(repo->packs); i++) {
1054 pack = &repo->packs[i];
1055 if (pack->path_packfile == NULL)
1056 break;
1057 if (strcmp(pack->path_packfile, path_packfile) == 0)
1058 return pack;
1061 return NULL;
1064 const struct got_error *
1065 got_repo_init(const char *repo_path)
1067 const struct got_error *err = NULL;
1068 const char *dirnames[] = {
1069 GOT_OBJECTS_DIR,
1070 GOT_OBJECTS_PACK_DIR,
1071 GOT_REFS_DIR,
1073 const char *description_str = "Unnamed repository; "
1074 "edit this file 'description' to name the repository.";
1075 const char *headref_str = "ref: refs/heads/main";
1076 const char *gitconfig_str = "[core]\n"
1077 "\trepositoryformatversion = 0\n"
1078 "\tfilemode = true\n"
1079 "\tbare = true\n";
1080 char *path;
1081 int i;
1083 if (!got_path_dir_is_empty(repo_path))
1084 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1086 for (i = 0; i < nitems(dirnames); i++) {
1087 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1088 return got_error_from_errno("asprintf");
1090 err = got_path_mkdir(path);
1091 free(path);
1092 if (err)
1093 return err;
1096 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1097 return got_error_from_errno("asprintf");
1098 err = got_path_create_file(path, description_str);
1099 free(path);
1100 if (err)
1101 return err;
1103 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1104 return got_error_from_errno("asprintf");
1105 err = got_path_create_file(path, headref_str);
1106 free(path);
1107 if (err)
1108 return err;
1110 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1111 return got_error_from_errno("asprintf");
1112 err = got_path_create_file(path, gitconfig_str);
1113 free(path);
1114 if (err)
1115 return err;
1117 return NULL;
1120 static const struct got_error *
1121 match_packed_object(struct got_object_id **unique_id,
1122 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1124 const struct got_error *err = NULL;
1125 char *path_packdir;
1126 DIR *packdir;
1127 struct dirent *dent;
1128 char *path_packidx;
1129 struct got_object_id_queue matched_ids;
1131 SIMPLEQ_INIT(&matched_ids);
1133 path_packdir = got_repo_get_path_objects_pack(repo);
1134 if (path_packdir == NULL)
1135 return got_error_from_errno("got_repo_get_path_objects_pack");
1137 packdir = opendir(path_packdir);
1138 if (packdir == NULL) {
1139 if (errno != ENOENT)
1140 err = got_error_from_errno2("opendir", path_packdir);
1141 goto done;
1144 while ((dent = readdir(packdir)) != NULL) {
1145 struct got_packidx *packidx;
1146 struct got_object_qid *qid;
1149 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1150 continue;
1152 if (asprintf(&path_packidx, "%s/%s", path_packdir,
1153 dent->d_name) == -1) {
1154 err = got_error_from_errno("asprintf");
1155 break;
1158 err = got_packidx_open(&packidx, path_packidx, 0);
1159 free(path_packidx);
1160 if (err)
1161 break;
1163 err = got_packidx_match_id_str_prefix(&matched_ids,
1164 packidx, id_str_prefix);
1165 if (err) {
1166 got_packidx_close(packidx);
1167 break;
1169 err = got_packidx_close(packidx);
1170 if (err)
1171 break;
1173 SIMPLEQ_FOREACH(qid, &matched_ids, entry) {
1174 if (obj_type != GOT_OBJ_TYPE_ANY) {
1175 int matched_type;
1176 err = got_object_get_type(&matched_type, repo,
1177 qid->id);
1178 if (err)
1179 goto done;
1180 if (matched_type != obj_type)
1181 continue;
1183 if (*unique_id == NULL) {
1184 *unique_id = got_object_id_dup(qid->id);
1185 if (*unique_id == NULL) {
1186 err = got_error_from_errno("malloc");
1187 goto done;
1189 } else {
1190 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1191 continue; /* packed multiple times */
1192 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1193 goto done;
1197 done:
1198 got_object_id_queue_free(&matched_ids);
1199 free(path_packdir);
1200 if (packdir && closedir(packdir) != 0 && err == NULL)
1201 err = got_error_from_errno("closedir");
1202 if (err) {
1203 free(*unique_id);
1204 *unique_id = NULL;
1206 return err;
1209 static const struct got_error *
1210 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1211 const char *object_dir, const char *id_str_prefix, int obj_type,
1212 struct got_repository *repo)
1214 const struct got_error *err = NULL;
1215 char *path;
1216 DIR *dir = NULL;
1217 struct dirent *dent;
1218 struct got_object_id id;
1220 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1221 err = got_error_from_errno("asprintf");
1222 goto done;
1225 dir = opendir(path);
1226 if (dir == NULL) {
1227 if (errno == ENOENT) {
1228 err = NULL;
1229 goto done;
1231 err = got_error_from_errno2("opendir", path);
1232 goto done;
1234 while ((dent = readdir(dir)) != NULL) {
1235 char *id_str;
1236 int cmp;
1238 if (strcmp(dent->d_name, ".") == 0 ||
1239 strcmp(dent->d_name, "..") == 0)
1240 continue;
1242 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1243 err = got_error_from_errno("asprintf");
1244 goto done;
1247 if (!got_parse_sha1_digest(id.sha1, id_str))
1248 continue;
1251 * Directory entries do not necessarily appear in
1252 * sorted order, so we must iterate over all of them.
1254 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1255 if (cmp != 0) {
1256 free(id_str);
1257 continue;
1260 if (*unique_id == NULL) {
1261 if (obj_type != GOT_OBJ_TYPE_ANY) {
1262 int matched_type;
1263 err = got_object_get_type(&matched_type, repo,
1264 &id);
1265 if (err)
1266 goto done;
1267 if (matched_type != obj_type)
1268 continue;
1270 *unique_id = got_object_id_dup(&id);
1271 if (*unique_id == NULL) {
1272 err = got_error_from_errno("got_object_id_dup");
1273 free(id_str);
1274 goto done;
1276 } else {
1277 if (got_object_id_cmp(*unique_id, &id) == 0)
1278 continue; /* both packed and loose */
1279 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1280 free(id_str);
1281 goto done;
1284 done:
1285 if (dir && closedir(dir) != 0 && err == NULL)
1286 err = got_error_from_errno("closedir");
1287 if (err) {
1288 free(*unique_id);
1289 *unique_id = NULL;
1291 free(path);
1292 return err;
1295 const struct got_error *
1296 got_repo_match_object_id_prefix(struct got_object_id **id,
1297 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1299 const struct got_error *err = NULL;
1300 char *path_objects = got_repo_get_path_objects(repo);
1301 char *object_dir = NULL;
1302 size_t len;
1303 int i;
1305 *id = NULL;
1307 for (i = 0; i < strlen(id_str_prefix); i++) {
1308 if (isxdigit((unsigned char)id_str_prefix[i]))
1309 continue;
1310 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1313 len = strlen(id_str_prefix);
1314 if (len >= 2) {
1315 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1316 if (err)
1317 goto done;
1318 object_dir = strndup(id_str_prefix, 2);
1319 if (object_dir == NULL) {
1320 err = got_error_from_errno("strdup");
1321 goto done;
1323 err = match_loose_object(id, path_objects, object_dir,
1324 id_str_prefix, obj_type, repo);
1325 } else if (len == 1) {
1326 int i;
1327 for (i = 0; i < 0xf; i++) {
1328 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1329 == -1) {
1330 err = got_error_from_errno("asprintf");
1331 goto done;
1333 err = match_packed_object(id, repo, object_dir,
1334 obj_type);
1335 if (err)
1336 goto done;
1337 err = match_loose_object(id, path_objects, object_dir,
1338 id_str_prefix, obj_type, repo);
1339 if (err)
1340 goto done;
1342 } else {
1343 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1344 goto done;
1346 done:
1347 free(object_dir);
1348 if (err) {
1349 free(*id);
1350 *id = NULL;
1351 } else if (*id == NULL)
1352 err = got_error(GOT_ERR_NO_OBJ);
1354 return err;
1357 const struct got_error *
1358 got_repo_match_object_id(struct got_object_id **id, char **label,
1359 const char *id_str, int obj_type, int resolve_tags,
1360 struct got_repository *repo)
1362 const struct got_error *err;
1363 struct got_tag_object *tag;
1364 struct got_reference *ref = NULL;
1366 *id = NULL;
1367 if (label)
1368 *label = NULL;
1370 if (resolve_tags) {
1371 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
1372 repo);
1373 if (err == NULL) {
1374 *id = got_object_id_dup(
1375 got_object_tag_get_object_id(tag));
1376 if (*id == NULL)
1377 err = got_error_from_errno("got_object_id_dup");
1378 else if (label && asprintf(label, "refs/tags/%s",
1379 got_object_tag_get_name(tag)) == -1) {
1380 err = got_error_from_errno("asprintf");
1381 free(*id);
1382 *id = NULL;
1384 got_object_tag_close(tag);
1385 return err;
1386 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1387 err->code != GOT_ERR_NO_OBJ)
1388 return err;
1391 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1392 if (err) {
1393 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1394 return err;
1395 err = got_ref_open(&ref, repo, id_str, 0);
1396 if (err != NULL)
1397 goto done;
1398 if (label) {
1399 *label = strdup(got_ref_get_name(ref));
1400 if (*label == NULL) {
1401 err = got_error_from_errno("strdup");
1402 goto done;
1405 err = got_ref_resolve(id, repo, ref);
1406 } else if (label) {
1407 err = got_object_id_str(label, *id);
1408 if (*label == NULL) {
1409 err = got_error_from_errno("strdup");
1410 goto done;
1413 done:
1414 if (ref)
1415 got_ref_close(ref);
1416 return err;
1419 const struct got_error *
1420 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1421 int obj_type, struct got_repository *repo)
1423 const struct got_error *err;
1424 struct got_reflist_head refs;
1425 struct got_reflist_entry *re;
1426 struct got_object_id *tag_id;
1428 SIMPLEQ_INIT(&refs);
1429 *tag = NULL;
1431 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_by_name, NULL);
1432 if (err)
1433 return err;
1435 SIMPLEQ_FOREACH(re, &refs, entry) {
1436 const char *refname;
1437 refname = got_ref_get_name(re->ref);
1438 if (got_ref_is_symbolic(re->ref))
1439 continue;
1440 refname += strlen("refs/tags/");
1441 if (strcmp(refname, name) != 0)
1442 continue;
1443 err = got_ref_resolve(&tag_id, repo, re->ref);
1444 if (err)
1445 break;
1446 err = got_object_open_as_tag(tag, repo, tag_id);
1447 free(tag_id);
1448 if (err)
1449 break;
1450 if (obj_type == GOT_OBJ_TYPE_ANY ||
1451 got_object_tag_get_object_type(*tag) == obj_type)
1452 break;
1453 got_object_tag_close(*tag);
1454 *tag = NULL;
1457 got_ref_list_free(&refs);
1458 if (err == NULL && *tag == NULL)
1459 err = got_error(GOT_ERR_NO_OBJ);
1460 return err;
1463 static const struct got_error *
1464 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1465 const char *name, mode_t mode, struct got_object_id *blob_id)
1467 const struct got_error *err = NULL;
1469 *new_te = NULL;
1471 *new_te = calloc(1, sizeof(**new_te));
1472 if (*new_te == NULL)
1473 return got_error_from_errno("calloc");
1475 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1476 sizeof((*new_te)->name)) {
1477 err = got_error(GOT_ERR_NO_SPACE);
1478 goto done;
1481 (*new_te)->mode = S_IFREG | (mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
1482 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1483 done:
1484 if (err && *new_te) {
1485 free(*new_te);
1486 *new_te = NULL;
1488 return err;
1491 static const struct got_error *
1492 import_file(struct got_tree_entry **new_te, struct dirent *de,
1493 const char *path, struct got_repository *repo)
1495 const struct got_error *err;
1496 struct got_object_id *blob_id = NULL;
1497 char *filepath;
1498 struct stat sb;
1500 if (asprintf(&filepath, "%s%s%s", path,
1501 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1502 return got_error_from_errno("asprintf");
1504 if (lstat(filepath, &sb) != 0) {
1505 err = got_error_from_errno2("lstat", path);
1506 goto done;
1509 err = got_object_blob_create(&blob_id, filepath, repo);
1510 if (err)
1511 goto done;
1513 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1514 blob_id);
1515 done:
1516 free(filepath);
1517 if (err)
1518 free(blob_id);
1519 return err;
1522 static const struct got_error *
1523 insert_tree_entry(struct got_tree_entry *new_te,
1524 struct got_pathlist_head *paths)
1526 const struct got_error *err = NULL;
1527 struct got_pathlist_entry *new_pe;
1529 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1530 if (err)
1531 return err;
1532 if (new_pe == NULL)
1533 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1534 return NULL;
1537 static const struct got_error *write_tree(struct got_object_id **,
1538 const char *, struct got_pathlist_head *, struct got_repository *,
1539 got_repo_import_cb progress_cb, void *progress_arg);
1541 static const struct got_error *
1542 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1543 const char *path, struct got_pathlist_head *ignores,
1544 struct got_repository *repo,
1545 got_repo_import_cb progress_cb, void *progress_arg)
1547 const struct got_error *err;
1548 struct got_object_id *id = NULL;
1549 char *subdirpath;
1551 if (asprintf(&subdirpath, "%s%s%s", path,
1552 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1553 return got_error_from_errno("asprintf");
1555 (*new_te) = calloc(1, sizeof(**new_te));
1556 if (*new_te == NULL)
1557 return got_error_from_errno("calloc");
1558 (*new_te)->mode = S_IFDIR;
1559 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1560 sizeof((*new_te)->name)) {
1561 err = got_error(GOT_ERR_NO_SPACE);
1562 goto done;
1564 err = write_tree(&id, subdirpath, ignores, repo,
1565 progress_cb, progress_arg);
1566 if (err)
1567 goto done;
1568 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1570 done:
1571 free(id);
1572 free(subdirpath);
1573 if (err) {
1574 free(*new_te);
1575 *new_te = NULL;
1577 return err;
1580 static const struct got_error *
1581 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1582 struct got_pathlist_head *ignores, struct got_repository *repo,
1583 got_repo_import_cb progress_cb, void *progress_arg)
1585 const struct got_error *err = NULL;
1586 DIR *dir;
1587 struct dirent *de;
1588 int nentries;
1589 struct got_tree_entry *new_te = NULL;
1590 struct got_pathlist_head paths;
1591 struct got_pathlist_entry *pe;
1593 *new_tree_id = NULL;
1595 TAILQ_INIT(&paths);
1597 dir = opendir(path_dir);
1598 if (dir == NULL) {
1599 err = got_error_from_errno2("opendir", path_dir);
1600 goto done;
1603 nentries = 0;
1604 while ((de = readdir(dir)) != NULL) {
1605 int ignore = 0;
1607 if (strcmp(de->d_name, ".") == 0 ||
1608 strcmp(de->d_name, "..") == 0)
1609 continue;
1611 TAILQ_FOREACH(pe, ignores, entry) {
1612 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1613 ignore = 1;
1614 break;
1617 if (ignore)
1618 continue;
1619 if (de->d_type == DT_DIR) {
1620 err = import_subdir(&new_te, de, path_dir,
1621 ignores, repo, progress_cb, progress_arg);
1622 if (err) {
1623 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1624 goto done;
1625 err = NULL;
1626 continue;
1628 } else if (de->d_type == DT_REG) {
1629 err = import_file(&new_te, de, path_dir, repo);
1630 if (err)
1631 goto done;
1632 } else
1633 continue;
1635 err = insert_tree_entry(new_te, &paths);
1636 if (err)
1637 goto done;
1638 nentries++;
1641 if (TAILQ_EMPTY(&paths)) {
1642 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1643 goto done;
1646 TAILQ_FOREACH(pe, &paths, entry) {
1647 struct got_tree_entry *te = pe->data;
1648 char *path;
1649 if (!S_ISREG(te->mode))
1650 continue;
1651 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1652 err = got_error_from_errno("asprintf");
1653 goto done;
1655 err = (*progress_cb)(progress_arg, path);
1656 free(path);
1657 if (err)
1658 goto done;
1661 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1662 done:
1663 if (dir)
1664 closedir(dir);
1665 got_pathlist_free(&paths);
1666 return err;
1669 const struct got_error *
1670 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1671 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1672 struct got_repository *repo, got_repo_import_cb progress_cb,
1673 void *progress_arg)
1675 const struct got_error *err;
1676 struct got_object_id *new_tree_id;
1678 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1679 progress_cb, progress_arg);
1680 if (err)
1681 return err;
1683 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1684 author, time(NULL), author, time(NULL), logmsg, repo);
1685 free(new_tree_id);
1686 return err;