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 const char *
67 got_repo_get_path(struct got_repository *repo)
68 {
69 return repo->path;
70 }
72 const char *
73 got_repo_get_path_git_dir(struct got_repository *repo)
74 {
75 return repo->path_git_dir;
76 }
78 const char *
79 got_repo_get_gitconfig_author_name(struct got_repository *repo)
80 {
81 return repo->gitconfig_author_name;
82 }
84 const char *
85 got_repo_get_gitconfig_author_email(struct got_repository *repo)
86 {
87 return repo->gitconfig_author_email;
88 }
90 const char *
91 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
92 {
93 return repo->global_gitconfig_author_name;
94 }
96 const char *
97 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
98 {
99 return repo->global_gitconfig_author_email;
102 const char *
103 got_repo_get_gitconfig_owner(struct got_repository *repo)
105 return repo->gitconfig_owner;
108 int
109 got_repo_is_bare(struct got_repository *repo)
111 return (strcmp(repo->path, repo->path_git_dir) == 0);
114 static char *
115 get_path_git_child(struct got_repository *repo, const char *basename)
117 char *path_child;
119 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
120 basename) == -1)
121 return NULL;
123 return path_child;
126 char *
127 got_repo_get_path_objects(struct got_repository *repo)
129 return get_path_git_child(repo, GOT_OBJECTS_DIR);
132 char *
133 got_repo_get_path_objects_pack(struct got_repository *repo)
135 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
138 char *
139 got_repo_get_path_refs(struct got_repository *repo)
141 return get_path_git_child(repo, GOT_REFS_DIR);
144 char *
145 got_repo_get_path_packed_refs(struct got_repository *repo)
147 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
150 static char *
151 get_path_head(struct got_repository *repo)
153 return get_path_git_child(repo, GOT_HEAD_FILE);
156 char *
157 got_repo_get_path_gitconfig(struct got_repository *repo)
159 return get_path_git_child(repo, GOT_GITCONFIG);
162 void
163 got_repo_get_gitconfig_remotes(int *nremotes, struct got_remote_repo **remotes,
164 struct got_repository *repo)
166 *nremotes = repo->ngitconfig_remotes;
167 *remotes = repo->gitconfig_remotes;
170 static int
171 is_git_repo(struct got_repository *repo)
173 const char *path_git = got_repo_get_path_git_dir(repo);
174 char *path_objects = got_repo_get_path_objects(repo);
175 char *path_refs = got_repo_get_path_refs(repo);
176 char *path_head = get_path_head(repo);
177 int ret = 0;
178 struct stat sb;
179 struct got_reference *head_ref;
181 if (lstat(path_git, &sb) == -1)
182 goto done;
183 if (!S_ISDIR(sb.st_mode))
184 goto done;
186 if (lstat(path_objects, &sb) == -1)
187 goto done;
188 if (!S_ISDIR(sb.st_mode))
189 goto done;
191 if (lstat(path_refs, &sb) == -1)
192 goto done;
193 if (!S_ISDIR(sb.st_mode))
194 goto done;
196 if (lstat(path_head, &sb) == -1)
197 goto done;
198 if (!S_ISREG(sb.st_mode))
199 goto done;
201 /* Check if the HEAD reference can be opened. */
202 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
203 goto done;
204 got_ref_close(head_ref);
206 ret = 1;
207 done:
208 free(path_objects);
209 free(path_refs);
210 free(path_head);
211 return ret;
215 const struct got_error *
216 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
217 struct got_object *obj)
219 #ifndef GOT_NO_OBJ_CACHE
220 const struct got_error *err = NULL;
221 err = got_object_cache_add(&repo->objcache, id, obj);
222 if (err) {
223 if (err->code == GOT_ERR_OBJ_EXISTS ||
224 err->code == GOT_ERR_OBJ_TOO_LARGE)
225 err = NULL;
226 return err;
228 obj->refcnt++;
229 #endif
230 return NULL;
233 struct got_object *
234 got_repo_get_cached_object(struct got_repository *repo,
235 struct got_object_id *id)
237 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
240 const struct got_error *
241 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
242 struct got_tree_object *tree)
244 #ifndef GOT_NO_OBJ_CACHE
245 const struct got_error *err = NULL;
246 err = got_object_cache_add(&repo->treecache, id, tree);
247 if (err) {
248 if (err->code == GOT_ERR_OBJ_EXISTS ||
249 err->code == GOT_ERR_OBJ_TOO_LARGE)
250 err = NULL;
251 return err;
253 tree->refcnt++;
254 #endif
255 return NULL;
258 struct got_tree_object *
259 got_repo_get_cached_tree(struct got_repository *repo,
260 struct got_object_id *id)
262 return (struct got_tree_object *)got_object_cache_get(
263 &repo->treecache, id);
266 const struct got_error *
267 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
268 struct got_commit_object *commit)
270 #ifndef GOT_NO_OBJ_CACHE
271 const struct got_error *err = NULL;
272 err = got_object_cache_add(&repo->commitcache, id, commit);
273 if (err) {
274 if (err->code == GOT_ERR_OBJ_EXISTS ||
275 err->code == GOT_ERR_OBJ_TOO_LARGE)
276 err = NULL;
277 return err;
279 commit->refcnt++;
280 #endif
281 return NULL;
284 struct got_commit_object *
285 got_repo_get_cached_commit(struct got_repository *repo,
286 struct got_object_id *id)
288 return (struct got_commit_object *)got_object_cache_get(
289 &repo->commitcache, id);
292 const struct got_error *
293 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
294 struct got_tag_object *tag)
296 #ifndef GOT_NO_OBJ_CACHE
297 const struct got_error *err = NULL;
298 err = got_object_cache_add(&repo->tagcache, id, tag);
299 if (err) {
300 if (err->code == GOT_ERR_OBJ_EXISTS ||
301 err->code == GOT_ERR_OBJ_TOO_LARGE)
302 err = NULL;
303 return err;
305 tag->refcnt++;
306 #endif
307 return NULL;
310 struct got_tag_object *
311 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
313 return (struct got_tag_object *)got_object_cache_get(
314 &repo->tagcache, id);
317 const struct got_error *
318 open_repo(struct got_repository *repo, const char *path)
320 const struct got_error *err = NULL;
322 /* bare git repository? */
323 repo->path_git_dir = strdup(path);
324 if (repo->path_git_dir == NULL)
325 return got_error_from_errno("strdup");
326 if (is_git_repo(repo)) {
327 repo->path = strdup(repo->path_git_dir);
328 if (repo->path == NULL) {
329 err = got_error_from_errno("strdup");
330 goto done;
332 return NULL;
335 /* git repository with working tree? */
336 free(repo->path_git_dir);
337 repo->path_git_dir = NULL;
338 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
339 err = got_error_from_errno("asprintf");
340 goto done;
342 if (is_git_repo(repo)) {
343 repo->path = strdup(path);
344 if (repo->path == NULL) {
345 err = got_error_from_errno("strdup");
346 goto done;
348 return NULL;
351 err = got_error(GOT_ERR_NOT_GIT_REPO);
352 done:
353 if (err) {
354 free(repo->path);
355 repo->path = NULL;
356 free(repo->path_git_dir);
357 repo->path_git_dir = NULL;
359 return err;
362 static const struct got_error *
363 parse_gitconfig_file(int *gitconfig_repository_format_version,
364 char **gitconfig_author_name, char **gitconfig_author_email,
365 struct got_remote_repo **remotes, int *nremotes,
366 char **gitconfig_owner,
367 const char *gitconfig_path)
369 const struct got_error *err = NULL, *child_err = NULL;
370 int fd = -1;
371 int imsg_fds[2] = { -1, -1 };
372 pid_t pid;
373 struct imsgbuf *ibuf;
375 *gitconfig_repository_format_version = 0;
376 *gitconfig_author_name = NULL;
377 *gitconfig_author_email = NULL;
378 if (remotes)
379 *remotes = NULL;
380 if (nremotes)
381 *nremotes = 0;
382 if (gitconfig_owner)
383 *gitconfig_owner = NULL;
385 fd = open(gitconfig_path, O_RDONLY);
386 if (fd == -1) {
387 if (errno == ENOENT)
388 return NULL;
389 return got_error_from_errno2("open", gitconfig_path);
392 ibuf = calloc(1, sizeof(*ibuf));
393 if (ibuf == NULL) {
394 err = got_error_from_errno("calloc");
395 goto done;
398 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
399 err = got_error_from_errno("socketpair");
400 goto done;
403 pid = fork();
404 if (pid == -1) {
405 err = got_error_from_errno("fork");
406 goto done;
407 } else if (pid == 0) {
408 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
409 gitconfig_path);
410 /* not reached */
413 if (close(imsg_fds[1]) == -1) {
414 err = got_error_from_errno("close");
415 goto done;
417 imsg_fds[1] = -1;
418 imsg_init(ibuf, imsg_fds[0]);
420 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
421 if (err)
422 goto done;
423 fd = -1;
425 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
426 if (err)
427 goto done;
429 err = got_privsep_recv_gitconfig_int(
430 gitconfig_repository_format_version, ibuf);
431 if (err)
432 goto done;
434 err = got_privsep_send_gitconfig_author_name_req(ibuf);
435 if (err)
436 goto done;
438 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
439 if (err)
440 goto done;
442 err = got_privsep_send_gitconfig_author_email_req(ibuf);
443 if (err)
444 goto done;
446 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
447 if (err)
448 goto done;
450 if (remotes && nremotes) {
451 err = got_privsep_send_gitconfig_remotes_req(ibuf);
452 if (err)
453 goto done;
455 err = got_privsep_recv_gitconfig_remotes(remotes,
456 nremotes, ibuf);
457 if (err)
458 goto done;
461 if (gitconfig_owner) {
462 err = got_privsep_send_gitconfig_owner_req(ibuf);
463 if (err)
464 goto done;
465 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
466 if (err)
467 goto done;
470 imsg_clear(ibuf);
471 err = got_privsep_send_stop(imsg_fds[0]);
472 child_err = got_privsep_wait_for_child(pid);
473 if (child_err && err == NULL)
474 err = child_err;
475 done:
476 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
477 err = got_error_from_errno("close");
478 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
479 err = got_error_from_errno("close");
480 if (fd != -1 && close(fd) == -1 && err == NULL)
481 err = got_error_from_errno2("close", gitconfig_path);
482 free(ibuf);
483 return err;
486 static const struct got_error *
487 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
489 const struct got_error *err = NULL;
490 char *repo_gitconfig_path = NULL;
492 if (global_gitconfig_path) {
493 /* Read settings from ~/.gitconfig. */
494 int dummy_repo_version;
495 err = parse_gitconfig_file(&dummy_repo_version,
496 &repo->global_gitconfig_author_name,
497 &repo->global_gitconfig_author_email,
498 NULL, NULL, NULL, global_gitconfig_path);
499 if (err)
500 return err;
503 /* Read repository's .git/config file. */
504 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
505 if (repo_gitconfig_path == NULL)
506 return got_error_from_errno("got_repo_get_path_gitconfig");
508 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
509 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
510 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
511 &repo->gitconfig_owner, repo_gitconfig_path);
512 if (err)
513 goto done;
514 done:
515 free(repo_gitconfig_path);
516 return err;
519 const struct got_error *
520 got_repo_open(struct got_repository **repop, const char *path,
521 const char *global_gitconfig_path)
523 struct got_repository *repo = NULL;
524 const struct got_error *err = NULL;
525 char *abspath;
526 int i, tried_root = 0;
528 *repop = NULL;
530 if (got_path_is_absolute(path))
531 abspath = strdup(path);
532 else
533 abspath = got_path_get_absolute(path);
534 if (abspath == NULL)
535 return got_error_path(path, GOT_ERR_BAD_PATH);
537 repo = calloc(1, sizeof(*repo));
538 if (repo == NULL) {
539 err = got_error_from_errno("calloc");
540 goto done;
543 for (i = 0; i < nitems(repo->privsep_children); i++) {
544 memset(&repo->privsep_children[i], 0,
545 sizeof(repo->privsep_children[0]));
546 repo->privsep_children[i].imsg_fd = -1;
549 err = got_object_cache_init(&repo->objcache,
550 GOT_OBJECT_CACHE_TYPE_OBJ);
551 if (err)
552 goto done;
553 err = got_object_cache_init(&repo->treecache,
554 GOT_OBJECT_CACHE_TYPE_TREE);
555 if (err)
556 goto done;
557 err = got_object_cache_init(&repo->commitcache,
558 GOT_OBJECT_CACHE_TYPE_COMMIT);
559 if (err)
560 goto done;
561 err = got_object_cache_init(&repo->tagcache,
562 GOT_OBJECT_CACHE_TYPE_TAG);
563 if (err)
564 goto done;
566 path = realpath(abspath, NULL);
567 if (path == NULL) {
568 err = got_error_from_errno2("realpath", abspath);
569 goto done;
572 do {
573 err = open_repo(repo, path);
574 if (err == NULL)
575 break;
576 if (err->code != GOT_ERR_NOT_GIT_REPO)
577 break;
578 if (path[0] == '/' && path[1] == '\0') {
579 if (tried_root) {
580 err = got_error(GOT_ERR_NOT_GIT_REPO);
581 goto done;
583 tried_root = 1;
585 path = dirname(path);
586 if (path == NULL) {
587 err = got_error_from_errno2("dirname", path);
588 goto done;
590 } while (path);
592 err = read_gitconfig(repo, global_gitconfig_path);
593 if (err)
594 goto done;
595 if (repo->gitconfig_repository_format_version != 0)
596 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
597 done:
598 if (err)
599 got_repo_close(repo);
600 else
601 *repop = repo;
602 free(abspath);
603 return err;
606 const struct got_error *
607 got_repo_close(struct got_repository *repo)
609 const struct got_error *err = NULL, *child_err;
610 int i;
612 for (i = 0; i < nitems(repo->packidx_cache); i++) {
613 if (repo->packidx_cache[i] == NULL)
614 break;
615 got_packidx_close(repo->packidx_cache[i]);
618 for (i = 0; i < nitems(repo->packs); i++) {
619 if (repo->packs[i].path_packfile == NULL)
620 break;
621 got_pack_close(&repo->packs[i]);
624 free(repo->path);
625 free(repo->path_git_dir);
627 got_object_cache_close(&repo->objcache);
628 got_object_cache_close(&repo->treecache);
629 got_object_cache_close(&repo->commitcache);
630 got_object_cache_close(&repo->tagcache);
632 for (i = 0; i < nitems(repo->privsep_children); i++) {
633 if (repo->privsep_children[i].imsg_fd == -1)
634 continue;
635 imsg_clear(repo->privsep_children[i].ibuf);
636 free(repo->privsep_children[i].ibuf);
637 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
638 child_err = got_privsep_wait_for_child(
639 repo->privsep_children[i].pid);
640 if (child_err && err == NULL)
641 err = child_err;
642 if (close(repo->privsep_children[i].imsg_fd) != 0 &&
643 err == NULL)
644 err = got_error_from_errno("close");
647 free(repo->gitconfig_author_name);
648 free(repo->gitconfig_author_email);
649 for (i = 0; i < repo->ngitconfig_remotes; i++) {
650 free(repo->gitconfig_remotes[i].name);
651 free(repo->gitconfig_remotes[i].url);
653 free(repo->gitconfig_remotes);
654 free(repo);
656 return err;
659 const struct got_error *
660 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
661 const char *input_path, int check_disk)
663 const struct got_error *err = NULL;
664 const char *repo_abspath = NULL;
665 size_t repolen, len;
666 char *canonpath, *path = NULL;
668 *in_repo_path = NULL;
670 canonpath = strdup(input_path);
671 if (canonpath == NULL) {
672 err = got_error_from_errno("strdup");
673 goto done;
675 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
676 if (err)
677 goto done;
679 repo_abspath = got_repo_get_path(repo);
681 if (!check_disk || canonpath[0] == '\0') {
682 path = strdup(canonpath);
683 if (path == NULL) {
684 err = got_error_from_errno("strdup");
685 goto done;
687 } else {
688 path = realpath(canonpath, NULL);
689 if (path == NULL) {
690 if (errno != ENOENT) {
691 err = got_error_from_errno2("realpath",
692 canonpath);
693 goto done;
695 /*
696 * Path is not on disk.
697 * Assume it is already relative to repository root.
698 */
699 path = strdup(canonpath);
700 if (path == NULL) {
701 err = got_error_from_errno("strdup");
702 goto done;
706 repolen = strlen(repo_abspath);
707 len = strlen(path);
710 if (strcmp(path, repo_abspath) == 0) {
711 free(path);
712 path = strdup("");
713 if (path == NULL) {
714 err = got_error_from_errno("strdup");
715 goto done;
717 } else if (len > repolen &&
718 got_path_is_child(path, repo_abspath, repolen)) {
719 /* Matched an on-disk path inside repository. */
720 if (got_repo_is_bare(repo)) {
721 /*
722 * Matched an on-disk path inside repository
723 * database. Treat as repository-relative.
724 */
725 } else {
726 char *child;
727 /* Strip common prefix with repository path. */
728 err = got_path_skip_common_ancestor(&child,
729 repo_abspath, path);
730 if (err)
731 goto done;
732 free(path);
733 path = child;
735 } else {
736 /*
737 * Matched unrelated on-disk path.
738 * Treat it as repository-relative.
739 */
743 /* Make in-repository path absolute */
744 if (path[0] != '/') {
745 char *abspath;
746 if (asprintf(&abspath, "/%s", path) == -1) {
747 err = got_error_from_errno("asprintf");
748 goto done;
750 free(path);
751 path = abspath;
754 done:
755 free(canonpath);
756 if (err)
757 free(path);
758 else
759 *in_repo_path = path;
760 return err;
763 static const struct got_error *
764 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
765 const char *path_packidx)
767 const struct got_error *err = NULL;
768 int i;
770 for (i = 0; i < nitems(repo->packidx_cache); i++) {
771 if (repo->packidx_cache[i] == NULL)
772 break;
773 if (strcmp(repo->packidx_cache[i]->path_packidx,
774 path_packidx) == 0) {
775 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
778 if (i == nitems(repo->packidx_cache)) {
779 err = got_packidx_close(repo->packidx_cache[i - 1]);
780 if (err)
781 return err;
784 /*
785 * Insert the new pack index at the front so it will
786 * be searched first in the future.
787 */
788 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
789 sizeof(repo->packidx_cache) -
790 sizeof(repo->packidx_cache[0]));
791 repo->packidx_cache[0] = packidx;
793 return NULL;
796 static int
797 is_packidx_filename(const char *name, size_t len)
799 if (len != GOT_PACKIDX_NAMELEN)
800 return 0;
802 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
803 return 0;
805 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
806 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
807 return 0;
809 return 1;
812 const struct got_error *
813 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
814 struct got_repository *repo, struct got_object_id *id)
816 const struct got_error *err;
817 char *path_packdir;
818 DIR *packdir;
819 struct dirent *dent;
820 char *path_packidx;
821 int i;
823 /* Search pack index cache. */
824 for (i = 0; i < nitems(repo->packidx_cache); i++) {
825 if (repo->packidx_cache[i] == NULL)
826 break;
827 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
828 if (*idx != -1) {
829 *packidx = repo->packidx_cache[i];
830 /*
831 * Move this cache entry to the front. Repeatedly
832 * searching a wrong pack index can be expensive.
833 */
834 if (i > 0) {
835 struct got_packidx *p;
836 p = repo->packidx_cache[0];
837 repo->packidx_cache[0] = *packidx;
838 repo->packidx_cache[i] = p;
840 return NULL;
843 /* No luck. Search the filesystem. */
845 path_packdir = got_repo_get_path_objects_pack(repo);
846 if (path_packdir == NULL)
847 return got_error_from_errno("got_repo_get_path_objects_pack");
849 packdir = opendir(path_packdir);
850 if (packdir == NULL) {
851 if (errno == ENOENT)
852 err = got_error_no_obj(id);
853 else
854 err = got_error_from_errno2("opendir", path_packdir);
855 goto done;
858 while ((dent = readdir(packdir)) != NULL) {
859 int is_cached = 0;
861 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
862 continue;
864 if (asprintf(&path_packidx, "%s/%s", path_packdir,
865 dent->d_name) == -1) {
866 err = got_error_from_errno("asprintf");
867 goto done;
870 for (i = 0; i < nitems(repo->packidx_cache); i++) {
871 if (repo->packidx_cache[i] == NULL)
872 break;
873 if (strcmp(repo->packidx_cache[i]->path_packidx,
874 path_packidx) == 0) {
875 is_cached = 1;
876 break;
879 if (is_cached) {
880 free(path_packidx);
881 continue; /* already searched */
884 err = got_packidx_open(packidx, path_packidx, 0);
885 if (err) {
886 free(path_packidx);
887 goto done;
890 err = cache_packidx(repo, *packidx, path_packidx);
891 free(path_packidx);
892 if (err)
893 goto done;
895 *idx = got_packidx_get_object_idx(*packidx, id);
896 if (*idx != -1) {
897 err = NULL; /* found the object */
898 goto done;
902 err = got_error_no_obj(id);
903 done:
904 free(path_packdir);
905 if (packdir && closedir(packdir) != 0 && err == NULL)
906 err = got_error_from_errno("closedir");
907 return err;
910 static const struct got_error *
911 read_packfile_hdr(int fd, struct got_packidx *packidx)
913 const struct got_error *err = NULL;
914 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
915 struct got_packfile_hdr hdr;
916 ssize_t n;
918 n = read(fd, &hdr, sizeof(hdr));
919 if (n < 0)
920 return got_error_from_errno("read");
921 if (n != sizeof(hdr))
922 return got_error(GOT_ERR_BAD_PACKFILE);
924 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
925 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
926 betoh32(hdr.nobjects) != totobj)
927 err = got_error(GOT_ERR_BAD_PACKFILE);
929 return err;
932 static const struct got_error *
933 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
935 const struct got_error *err = NULL;
937 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
938 if (*fd == -1)
939 return got_error_from_errno2("open", path_packfile);
941 if (packidx) {
942 err = read_packfile_hdr(*fd, packidx);
943 if (err) {
944 close(*fd);
945 *fd = -1;
949 return err;
952 const struct got_error *
953 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
954 const char *path_packfile, struct got_packidx *packidx)
956 const struct got_error *err = NULL;
957 struct got_pack *pack = NULL;
958 struct stat sb;
959 int i;
961 if (packp)
962 *packp = NULL;
964 for (i = 0; i < nitems(repo->packs); i++) {
965 pack = &repo->packs[i];
966 if (pack->path_packfile == NULL)
967 break;
968 if (strcmp(pack->path_packfile, path_packfile) == 0)
969 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
972 if (i == nitems(repo->packs) - 1) {
973 err = got_pack_close(&repo->packs[i - 1]);
974 if (err)
975 return err;
976 memmove(&repo->packs[1], &repo->packs[0],
977 sizeof(repo->packs) - sizeof(repo->packs[0]));
978 i = 0;
981 pack = &repo->packs[i];
983 pack->path_packfile = strdup(path_packfile);
984 if (pack->path_packfile == NULL) {
985 err = got_error_from_errno("strdup");
986 goto done;
989 err = open_packfile(&pack->fd, path_packfile, packidx);
990 if (err)
991 goto done;
993 if (fstat(pack->fd, &sb) != 0) {
994 err = got_error_from_errno("fstat");
995 goto done;
997 pack->filesize = sb.st_size;
999 pack->privsep_child = NULL;
1001 #ifndef GOT_PACK_NO_MMAP
1002 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1003 pack->fd, 0);
1004 if (pack->map == MAP_FAILED) {
1005 if (errno != ENOMEM) {
1006 err = got_error_from_errno("mmap");
1007 goto done;
1009 pack->map = NULL; /* fall back to read(2) */
1011 #endif
1012 done:
1013 if (err) {
1014 if (pack) {
1015 free(pack->path_packfile);
1016 memset(pack, 0, sizeof(*pack));
1018 } else if (packp)
1019 *packp = pack;
1020 return err;
1023 struct got_pack *
1024 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1026 struct got_pack *pack = NULL;
1027 int i;
1029 for (i = 0; i < nitems(repo->packs); i++) {
1030 pack = &repo->packs[i];
1031 if (pack->path_packfile == NULL)
1032 break;
1033 if (strcmp(pack->path_packfile, path_packfile) == 0)
1034 return pack;
1037 return NULL;
1040 const struct got_error *
1041 got_repo_init(const char *repo_path)
1043 const struct got_error *err = NULL;
1044 const char *dirnames[] = {
1045 GOT_OBJECTS_DIR,
1046 GOT_OBJECTS_PACK_DIR,
1047 GOT_REFS_DIR,
1049 const char *description_str = "Unnamed repository; "
1050 "edit this file 'description' to name the repository.";
1051 const char *headref_str = "ref: refs/heads/main";
1052 const char *gitconfig_str = "[core]\n"
1053 "\trepositoryformatversion = 0\n"
1054 "\tfilemode = true\n"
1055 "\tbare = true\n";
1056 char *path;
1057 int i;
1059 if (!got_path_dir_is_empty(repo_path))
1060 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1062 for (i = 0; i < nitems(dirnames); i++) {
1063 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1064 return got_error_from_errno("asprintf");
1066 err = got_path_mkdir(path);
1067 free(path);
1068 if (err)
1069 return err;
1072 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1073 return got_error_from_errno("asprintf");
1074 err = got_path_create_file(path, description_str);
1075 free(path);
1076 if (err)
1077 return err;
1079 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1080 return got_error_from_errno("asprintf");
1081 err = got_path_create_file(path, headref_str);
1082 free(path);
1083 if (err)
1084 return err;
1086 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1087 return got_error_from_errno("asprintf");
1088 err = got_path_create_file(path, gitconfig_str);
1089 free(path);
1090 if (err)
1091 return err;
1093 return NULL;
1096 static const struct got_error *
1097 match_packed_object(struct got_object_id **unique_id,
1098 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1100 const struct got_error *err = NULL;
1101 char *path_packdir;
1102 DIR *packdir;
1103 struct dirent *dent;
1104 char *path_packidx;
1105 struct got_object_id_queue matched_ids;
1107 SIMPLEQ_INIT(&matched_ids);
1109 path_packdir = got_repo_get_path_objects_pack(repo);
1110 if (path_packdir == NULL)
1111 return got_error_from_errno("got_repo_get_path_objects_pack");
1113 packdir = opendir(path_packdir);
1114 if (packdir == NULL) {
1115 if (errno != ENOENT)
1116 err = got_error_from_errno2("opendir", path_packdir);
1117 goto done;
1120 while ((dent = readdir(packdir)) != NULL) {
1121 struct got_packidx *packidx;
1122 struct got_object_qid *qid;
1125 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1126 continue;
1128 if (asprintf(&path_packidx, "%s/%s", path_packdir,
1129 dent->d_name) == -1) {
1130 err = got_error_from_errno("asprintf");
1131 break;
1134 err = got_packidx_open(&packidx, path_packidx, 0);
1135 free(path_packidx);
1136 if (err)
1137 break;
1139 err = got_packidx_match_id_str_prefix(&matched_ids,
1140 packidx, id_str_prefix);
1141 if (err) {
1142 got_packidx_close(packidx);
1143 break;
1145 err = got_packidx_close(packidx);
1146 if (err)
1147 break;
1149 SIMPLEQ_FOREACH(qid, &matched_ids, entry) {
1150 if (obj_type != GOT_OBJ_TYPE_ANY) {
1151 int matched_type;
1152 err = got_object_get_type(&matched_type, repo,
1153 qid->id);
1154 if (err)
1155 goto done;
1156 if (matched_type != obj_type)
1157 continue;
1159 if (*unique_id == NULL) {
1160 *unique_id = got_object_id_dup(qid->id);
1161 if (*unique_id == NULL) {
1162 err = got_error_from_errno("malloc");
1163 goto done;
1165 } else {
1166 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1167 continue; /* packed multiple times */
1168 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1169 goto done;
1173 done:
1174 got_object_id_queue_free(&matched_ids);
1175 free(path_packdir);
1176 if (packdir && closedir(packdir) != 0 && err == NULL)
1177 err = got_error_from_errno("closedir");
1178 if (err) {
1179 free(*unique_id);
1180 *unique_id = NULL;
1182 return err;
1185 static const struct got_error *
1186 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1187 const char *object_dir, const char *id_str_prefix, int obj_type,
1188 struct got_repository *repo)
1190 const struct got_error *err = NULL;
1191 char *path;
1192 DIR *dir = NULL;
1193 struct dirent *dent;
1194 struct got_object_id id;
1196 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1197 err = got_error_from_errno("asprintf");
1198 goto done;
1201 dir = opendir(path);
1202 if (dir == NULL) {
1203 if (errno == ENOENT) {
1204 err = NULL;
1205 goto done;
1207 err = got_error_from_errno2("opendir", path);
1208 goto done;
1210 while ((dent = readdir(dir)) != NULL) {
1211 char *id_str;
1212 int cmp;
1214 if (strcmp(dent->d_name, ".") == 0 ||
1215 strcmp(dent->d_name, "..") == 0)
1216 continue;
1218 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1219 err = got_error_from_errno("asprintf");
1220 goto done;
1223 if (!got_parse_sha1_digest(id.sha1, id_str))
1224 continue;
1227 * Directory entries do not necessarily appear in
1228 * sorted order, so we must iterate over all of them.
1230 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1231 if (cmp != 0) {
1232 free(id_str);
1233 continue;
1236 if (*unique_id == NULL) {
1237 if (obj_type != GOT_OBJ_TYPE_ANY) {
1238 int matched_type;
1239 err = got_object_get_type(&matched_type, repo,
1240 &id);
1241 if (err)
1242 goto done;
1243 if (matched_type != obj_type)
1244 continue;
1246 *unique_id = got_object_id_dup(&id);
1247 if (*unique_id == NULL) {
1248 err = got_error_from_errno("got_object_id_dup");
1249 free(id_str);
1250 goto done;
1252 } else {
1253 if (got_object_id_cmp(*unique_id, &id) == 0)
1254 continue; /* both packed and loose */
1255 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1256 free(id_str);
1257 goto done;
1260 done:
1261 if (dir && closedir(dir) != 0 && err == NULL)
1262 err = got_error_from_errno("closedir");
1263 if (err) {
1264 free(*unique_id);
1265 *unique_id = NULL;
1267 free(path);
1268 return err;
1271 const struct got_error *
1272 got_repo_match_object_id_prefix(struct got_object_id **id,
1273 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1275 const struct got_error *err = NULL;
1276 char *path_objects = got_repo_get_path_objects(repo);
1277 char *object_dir = NULL;
1278 size_t len;
1279 int i;
1281 *id = NULL;
1283 for (i = 0; i < strlen(id_str_prefix); i++) {
1284 if (isxdigit((unsigned char)id_str_prefix[i]))
1285 continue;
1286 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1289 len = strlen(id_str_prefix);
1290 if (len >= 2) {
1291 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1292 if (err)
1293 goto done;
1294 object_dir = strndup(id_str_prefix, 2);
1295 if (object_dir == NULL) {
1296 err = got_error_from_errno("strdup");
1297 goto done;
1299 err = match_loose_object(id, path_objects, object_dir,
1300 id_str_prefix, obj_type, repo);
1301 } else if (len == 1) {
1302 int i;
1303 for (i = 0; i < 0xf; i++) {
1304 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1305 == -1) {
1306 err = got_error_from_errno("asprintf");
1307 goto done;
1309 err = match_packed_object(id, repo, object_dir,
1310 obj_type);
1311 if (err)
1312 goto done;
1313 err = match_loose_object(id, path_objects, object_dir,
1314 id_str_prefix, obj_type, repo);
1315 if (err)
1316 goto done;
1318 } else {
1319 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1320 goto done;
1322 done:
1323 free(object_dir);
1324 if (err) {
1325 free(*id);
1326 *id = NULL;
1327 } else if (*id == NULL)
1328 err = got_error(GOT_ERR_NO_OBJ);
1330 return err;
1333 const struct got_error *
1334 got_repo_match_object_id(struct got_object_id **id, char **label,
1335 const char *id_str, int obj_type, int resolve_tags,
1336 struct got_repository *repo)
1338 const struct got_error *err;
1339 struct got_tag_object *tag;
1340 struct got_reference *ref = NULL;
1342 *id = NULL;
1343 if (label)
1344 *label = NULL;
1346 if (resolve_tags) {
1347 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
1348 repo);
1349 if (err == NULL) {
1350 *id = got_object_id_dup(
1351 got_object_tag_get_object_id(tag));
1352 if (*id == NULL)
1353 err = got_error_from_errno("got_object_id_dup");
1354 else if (label && asprintf(label, "refs/tags/%s",
1355 got_object_tag_get_name(tag)) == -1) {
1356 err = got_error_from_errno("asprintf");
1357 free(*id);
1358 *id = NULL;
1360 got_object_tag_close(tag);
1361 return err;
1362 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1363 err->code != GOT_ERR_NO_OBJ)
1364 return err;
1367 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1368 if (err) {
1369 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1370 return err;
1371 err = got_ref_open(&ref, repo, id_str, 0);
1372 if (err != NULL)
1373 goto done;
1374 if (label) {
1375 *label = strdup(got_ref_get_name(ref));
1376 if (*label == NULL) {
1377 err = got_error_from_errno("strdup");
1378 goto done;
1381 err = got_ref_resolve(id, repo, ref);
1382 } else if (label) {
1383 err = got_object_id_str(label, *id);
1384 if (*label == NULL) {
1385 err = got_error_from_errno("strdup");
1386 goto done;
1389 done:
1390 if (ref)
1391 got_ref_close(ref);
1392 return err;
1395 const struct got_error *
1396 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1397 int obj_type, struct got_repository *repo)
1399 const struct got_error *err;
1400 struct got_reflist_head refs;
1401 struct got_reflist_entry *re;
1402 struct got_object_id *tag_id;
1404 SIMPLEQ_INIT(&refs);
1405 *tag = NULL;
1407 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_by_name, NULL);
1408 if (err)
1409 return err;
1411 SIMPLEQ_FOREACH(re, &refs, entry) {
1412 const char *refname;
1413 refname = got_ref_get_name(re->ref);
1414 if (got_ref_is_symbolic(re->ref))
1415 continue;
1416 refname += strlen("refs/tags/");
1417 if (strcmp(refname, name) != 0)
1418 continue;
1419 err = got_ref_resolve(&tag_id, repo, re->ref);
1420 if (err)
1421 break;
1422 err = got_object_open_as_tag(tag, repo, tag_id);
1423 free(tag_id);
1424 if (err)
1425 break;
1426 if (obj_type == GOT_OBJ_TYPE_ANY ||
1427 got_object_tag_get_object_type(*tag) == obj_type)
1428 break;
1429 got_object_tag_close(*tag);
1430 *tag = NULL;
1433 got_ref_list_free(&refs);
1434 if (err == NULL && *tag == NULL)
1435 err = got_error(GOT_ERR_NO_OBJ);
1436 return err;
1439 static const struct got_error *
1440 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1441 const char *name, mode_t mode, struct got_object_id *blob_id)
1443 const struct got_error *err = NULL;
1445 *new_te = NULL;
1447 *new_te = calloc(1, sizeof(**new_te));
1448 if (*new_te == NULL)
1449 return got_error_from_errno("calloc");
1451 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1452 sizeof((*new_te)->name)) {
1453 err = got_error(GOT_ERR_NO_SPACE);
1454 goto done;
1457 (*new_te)->mode = S_IFREG | (mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
1458 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1459 done:
1460 if (err && *new_te) {
1461 free(*new_te);
1462 *new_te = NULL;
1464 return err;
1467 static const struct got_error *
1468 import_file(struct got_tree_entry **new_te, struct dirent *de,
1469 const char *path, struct got_repository *repo)
1471 const struct got_error *err;
1472 struct got_object_id *blob_id = NULL;
1473 char *filepath;
1474 struct stat sb;
1476 if (asprintf(&filepath, "%s%s%s", path,
1477 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1478 return got_error_from_errno("asprintf");
1480 if (lstat(filepath, &sb) != 0) {
1481 err = got_error_from_errno2("lstat", path);
1482 goto done;
1485 err = got_object_blob_create(&blob_id, filepath, repo);
1486 if (err)
1487 goto done;
1489 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1490 blob_id);
1491 done:
1492 free(filepath);
1493 if (err)
1494 free(blob_id);
1495 return err;
1498 static const struct got_error *
1499 insert_tree_entry(struct got_tree_entry *new_te,
1500 struct got_pathlist_head *paths)
1502 const struct got_error *err = NULL;
1503 struct got_pathlist_entry *new_pe;
1505 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1506 if (err)
1507 return err;
1508 if (new_pe == NULL)
1509 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1510 return NULL;
1513 static const struct got_error *write_tree(struct got_object_id **,
1514 const char *, struct got_pathlist_head *, struct got_repository *,
1515 got_repo_import_cb progress_cb, void *progress_arg);
1517 static const struct got_error *
1518 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1519 const char *path, struct got_pathlist_head *ignores,
1520 struct got_repository *repo,
1521 got_repo_import_cb progress_cb, void *progress_arg)
1523 const struct got_error *err;
1524 struct got_object_id *id = NULL;
1525 char *subdirpath;
1527 if (asprintf(&subdirpath, "%s%s%s", path,
1528 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1529 return got_error_from_errno("asprintf");
1531 (*new_te) = calloc(1, sizeof(**new_te));
1532 if (*new_te == NULL)
1533 return got_error_from_errno("calloc");
1534 (*new_te)->mode = S_IFDIR;
1535 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1536 sizeof((*new_te)->name)) {
1537 err = got_error(GOT_ERR_NO_SPACE);
1538 goto done;
1540 err = write_tree(&id, subdirpath, ignores, repo,
1541 progress_cb, progress_arg);
1542 if (err)
1543 goto done;
1544 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1546 done:
1547 free(id);
1548 free(subdirpath);
1549 if (err) {
1550 free(*new_te);
1551 *new_te = NULL;
1553 return err;
1556 static const struct got_error *
1557 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1558 struct got_pathlist_head *ignores, struct got_repository *repo,
1559 got_repo_import_cb progress_cb, void *progress_arg)
1561 const struct got_error *err = NULL;
1562 DIR *dir;
1563 struct dirent *de;
1564 int nentries;
1565 struct got_tree_entry *new_te = NULL;
1566 struct got_pathlist_head paths;
1567 struct got_pathlist_entry *pe;
1569 *new_tree_id = NULL;
1571 TAILQ_INIT(&paths);
1573 dir = opendir(path_dir);
1574 if (dir == NULL) {
1575 err = got_error_from_errno2("opendir", path_dir);
1576 goto done;
1579 nentries = 0;
1580 while ((de = readdir(dir)) != NULL) {
1581 int ignore = 0;
1583 if (strcmp(de->d_name, ".") == 0 ||
1584 strcmp(de->d_name, "..") == 0)
1585 continue;
1587 TAILQ_FOREACH(pe, ignores, entry) {
1588 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1589 ignore = 1;
1590 break;
1593 if (ignore)
1594 continue;
1595 if (de->d_type == DT_DIR) {
1596 err = import_subdir(&new_te, de, path_dir,
1597 ignores, repo, progress_cb, progress_arg);
1598 if (err) {
1599 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1600 goto done;
1601 err = NULL;
1602 continue;
1604 } else if (de->d_type == DT_REG) {
1605 err = import_file(&new_te, de, path_dir, repo);
1606 if (err)
1607 goto done;
1608 } else
1609 continue;
1611 err = insert_tree_entry(new_te, &paths);
1612 if (err)
1613 goto done;
1614 nentries++;
1617 if (TAILQ_EMPTY(&paths)) {
1618 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1619 goto done;
1622 TAILQ_FOREACH(pe, &paths, entry) {
1623 struct got_tree_entry *te = pe->data;
1624 char *path;
1625 if (!S_ISREG(te->mode))
1626 continue;
1627 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1628 err = got_error_from_errno("asprintf");
1629 goto done;
1631 err = (*progress_cb)(progress_arg, path);
1632 free(path);
1633 if (err)
1634 goto done;
1637 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1638 done:
1639 if (dir)
1640 closedir(dir);
1641 got_pathlist_free(&paths);
1642 return err;
1645 const struct got_error *
1646 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1647 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1648 struct got_repository *repo, got_repo_import_cb progress_cb,
1649 void *progress_arg)
1651 const struct got_error *err;
1652 struct got_object_id *new_tree_id;
1654 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1655 progress_cb, progress_arg);
1656 if (err)
1657 return err;
1659 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1660 author, time(NULL), author, time(NULL), logmsg, repo);
1661 free(new_tree_id);
1662 return err;