2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include <sys/types.h>
18 #include <sys/queue.h>
20 #include <sys/socket.h>
23 #include <sys/syslimits.h>
42 #include "got_error.h"
43 #include "got_reference.h"
44 #include "got_repository.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"
63 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
67 got_repo_get_path(struct got_repository *repo)
73 got_repo_get_path_git_dir(struct got_repository *repo)
75 return repo->path_git_dir;
79 got_repo_get_gitconfig_author_name(struct got_repository *repo)
81 return repo->gitconfig_author_name;
85 got_repo_get_gitconfig_author_email(struct got_repository *repo)
87 return repo->gitconfig_author_email;
91 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
93 return repo->global_gitconfig_author_name;
97 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
99 return repo->global_gitconfig_author_email;
103 got_repo_get_gitconfig_owner(struct got_repository *repo)
105 return repo->gitconfig_owner;
109 got_repo_is_bare(struct got_repository *repo)
111 return (strcmp(repo->path, repo->path_git_dir) == 0);
115 get_path_git_child(struct got_repository *repo, const char *basename)
119 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
127 got_repo_get_path_objects(struct got_repository *repo)
129 return get_path_git_child(repo, GOT_OBJECTS_DIR);
133 got_repo_get_path_objects_pack(struct got_repository *repo)
135 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
139 got_repo_get_path_refs(struct got_repository *repo)
141 return get_path_git_child(repo, GOT_REFS_DIR);
145 got_repo_get_path_packed_refs(struct got_repository *repo)
147 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
151 get_path_head(struct got_repository *repo)
153 return get_path_git_child(repo, GOT_HEAD_FILE);
157 got_repo_get_path_gitconfig(struct got_repository *repo)
159 return get_path_git_child(repo, GOT_GITCONFIG);
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;
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);
179 struct got_reference *head_ref;
181 if (lstat(path_git, &sb) == -1)
183 if (!S_ISDIR(sb.st_mode))
186 if (lstat(path_objects, &sb) == -1)
188 if (!S_ISDIR(sb.st_mode))
191 if (lstat(path_refs, &sb) == -1)
193 if (!S_ISDIR(sb.st_mode))
196 if (lstat(path_head, &sb) == -1)
198 if (!S_ISREG(sb.st_mode))
201 /* Check if the HEAD reference can be opened. */
202 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
204 got_ref_close(head_ref);
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);
223 if (err->code == GOT_ERR_OBJ_EXISTS ||
224 err->code == GOT_ERR_OBJ_TOO_LARGE)
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);
248 if (err->code == GOT_ERR_OBJ_EXISTS ||
249 err->code == GOT_ERR_OBJ_TOO_LARGE)
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);
274 if (err->code == GOT_ERR_OBJ_EXISTS ||
275 err->code == GOT_ERR_OBJ_TOO_LARGE)
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);
300 if (err->code == GOT_ERR_OBJ_EXISTS ||
301 err->code == GOT_ERR_OBJ_TOO_LARGE)
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");
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");
342 if (is_git_repo(repo)) {
343 repo->path = strdup(path);
344 if (repo->path == NULL) {
345 err = got_error_from_errno("strdup");
351 err = got_error(GOT_ERR_NOT_GIT_REPO);
356 free(repo->path_git_dir);
357 repo->path_git_dir = NULL;
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;
371 int imsg_fds[2] = { -1, -1 };
373 struct imsgbuf *ibuf;
375 *gitconfig_repository_format_version = 0;
376 *gitconfig_author_name = NULL;
377 *gitconfig_author_email = NULL;
379 fd = open(gitconfig_path, O_RDONLY);
383 return got_error_from_errno2("open", gitconfig_path);
386 ibuf = calloc(1, sizeof(*ibuf));
388 err = got_error_from_errno("calloc");
392 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
393 err = got_error_from_errno("socketpair");
399 err = got_error_from_errno("fork");
401 } else if (pid == 0) {
402 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
407 if (close(imsg_fds[1]) == -1) {
408 err = got_error_from_errno("close");
412 imsg_init(ibuf, imsg_fds[0]);
414 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
419 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
423 err = got_privsep_recv_gitconfig_int(
424 gitconfig_repository_format_version, ibuf);
428 err = got_privsep_send_gitconfig_author_name_req(ibuf);
432 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
436 err = got_privsep_send_gitconfig_author_email_req(ibuf);
440 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
444 if (remotes && nremotes) {
445 err = got_privsep_send_gitconfig_remotes_req(ibuf);
449 err = got_privsep_recv_gitconfig_remotes(remotes,
455 if (gitconfig_owner) {
456 err = got_privsep_send_gitconfig_owner_req(ibuf);
459 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
465 err = got_privsep_send_stop(imsg_fds[0]);
466 child_err = got_privsep_wait_for_child(pid);
467 if (child_err && err == NULL)
470 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
471 err = got_error_from_errno("close");
472 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
473 err = got_error_from_errno("close");
474 if (fd != -1 && close(fd) == -1 && err == NULL)
475 err = got_error_from_errno2("close", gitconfig_path);
480 static const struct got_error *
481 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
483 const struct got_error *err = NULL;
484 char *repo_gitconfig_path = NULL;
486 if (global_gitconfig_path) {
487 /* Read settings from ~/.gitconfig. */
488 int dummy_repo_version;
489 err = parse_gitconfig_file(&dummy_repo_version,
490 &repo->global_gitconfig_author_name,
491 &repo->global_gitconfig_author_email,
492 NULL, NULL, NULL, global_gitconfig_path);
497 /* Read repository's .git/config file. */
498 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
499 if (repo_gitconfig_path == NULL)
500 return got_error_from_errno("got_repo_get_path_gitconfig");
502 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
503 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
504 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
505 &repo->gitconfig_owner, repo_gitconfig_path);
509 free(repo_gitconfig_path);
513 const struct got_error *
514 got_repo_open(struct got_repository **repop, const char *path,
515 const char *global_gitconfig_path)
517 struct got_repository *repo = NULL;
518 const struct got_error *err = NULL;
520 int i, tried_root = 0;
524 if (got_path_is_absolute(path))
525 abspath = strdup(path);
527 abspath = got_path_get_absolute(path);
529 return got_error_path(path, GOT_ERR_BAD_PATH);
531 repo = calloc(1, sizeof(*repo));
533 err = got_error_from_errno("calloc");
537 for (i = 0; i < nitems(repo->privsep_children); i++) {
538 memset(&repo->privsep_children[i], 0,
539 sizeof(repo->privsep_children[0]));
540 repo->privsep_children[i].imsg_fd = -1;
543 err = got_object_cache_init(&repo->objcache,
544 GOT_OBJECT_CACHE_TYPE_OBJ);
547 err = got_object_cache_init(&repo->treecache,
548 GOT_OBJECT_CACHE_TYPE_TREE);
551 err = got_object_cache_init(&repo->commitcache,
552 GOT_OBJECT_CACHE_TYPE_COMMIT);
555 err = got_object_cache_init(&repo->tagcache,
556 GOT_OBJECT_CACHE_TYPE_TAG);
560 path = realpath(abspath, NULL);
562 err = got_error_from_errno2("realpath", abspath);
567 err = open_repo(repo, path);
570 if (err->code != GOT_ERR_NOT_GIT_REPO)
572 if (path[0] == '/' && path[1] == '\0') {
574 err = got_error(GOT_ERR_NOT_GIT_REPO);
579 path = dirname(path);
581 err = got_error_from_errno2("dirname", path);
586 err = read_gitconfig(repo, global_gitconfig_path);
589 if (repo->gitconfig_repository_format_version != 0)
590 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
593 got_repo_close(repo);
600 const struct got_error *
601 got_repo_close(struct got_repository *repo)
603 const struct got_error *err = NULL, *child_err;
606 for (i = 0; i < nitems(repo->packidx_cache); i++) {
607 if (repo->packidx_cache[i] == NULL)
609 got_packidx_close(repo->packidx_cache[i]);
612 for (i = 0; i < nitems(repo->packs); i++) {
613 if (repo->packs[i].path_packfile == NULL)
615 got_pack_close(&repo->packs[i]);
619 free(repo->path_git_dir);
621 got_object_cache_close(&repo->objcache);
622 got_object_cache_close(&repo->treecache);
623 got_object_cache_close(&repo->commitcache);
624 got_object_cache_close(&repo->tagcache);
626 for (i = 0; i < nitems(repo->privsep_children); i++) {
627 if (repo->privsep_children[i].imsg_fd == -1)
629 imsg_clear(repo->privsep_children[i].ibuf);
630 free(repo->privsep_children[i].ibuf);
631 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
632 child_err = got_privsep_wait_for_child(
633 repo->privsep_children[i].pid);
634 if (child_err && err == NULL)
636 if (close(repo->privsep_children[i].imsg_fd) != 0 &&
638 err = got_error_from_errno("close");
641 free(repo->gitconfig_author_name);
642 free(repo->gitconfig_author_email);
643 for (i = 0; i < repo->ngitconfig_remotes; i++) {
644 free(repo->gitconfig_remotes[i].name);
645 free(repo->gitconfig_remotes[i].url);
647 free(repo->gitconfig_remotes);
653 const struct got_error *
654 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
655 const char *input_path, int check_disk)
657 const struct got_error *err = NULL;
658 const char *repo_abspath = NULL;
660 char *canonpath, *path = NULL;
662 *in_repo_path = NULL;
664 canonpath = strdup(input_path);
665 if (canonpath == NULL) {
666 err = got_error_from_errno("strdup");
669 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
673 repo_abspath = got_repo_get_path(repo);
675 if (!check_disk || canonpath[0] == '\0') {
676 path = strdup(canonpath);
678 err = got_error_from_errno("strdup");
682 path = realpath(canonpath, NULL);
684 if (errno != ENOENT) {
685 err = got_error_from_errno2("realpath",
690 * Path is not on disk.
691 * Assume it is already relative to repository root.
693 path = strdup(canonpath);
695 err = got_error_from_errno("strdup");
700 repolen = strlen(repo_abspath);
704 if (strcmp(path, repo_abspath) == 0) {
708 err = got_error_from_errno("strdup");
711 } else if (len > repolen &&
712 got_path_is_child(path, repo_abspath, repolen)) {
713 /* Matched an on-disk path inside repository. */
714 if (got_repo_is_bare(repo)) {
716 * Matched an on-disk path inside repository
717 * database. Treat as repository-relative.
721 /* Strip common prefix with repository path. */
722 err = got_path_skip_common_ancestor(&child,
731 * Matched unrelated on-disk path.
732 * Treat it as repository-relative.
737 /* Make in-repository path absolute */
738 if (path[0] != '/') {
740 if (asprintf(&abspath, "/%s", path) == -1) {
741 err = got_error_from_errno("asprintf");
753 *in_repo_path = path;
757 static const struct got_error *
758 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
759 const char *path_packidx)
761 const struct got_error *err = NULL;
764 for (i = 0; i < nitems(repo->packidx_cache); i++) {
765 if (repo->packidx_cache[i] == NULL)
767 if (strcmp(repo->packidx_cache[i]->path_packidx,
768 path_packidx) == 0) {
769 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
772 if (i == nitems(repo->packidx_cache)) {
773 err = got_packidx_close(repo->packidx_cache[i - 1]);
779 * Insert the new pack index at the front so it will
780 * be searched first in the future.
782 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
783 sizeof(repo->packidx_cache) -
784 sizeof(repo->packidx_cache[0]));
785 repo->packidx_cache[0] = packidx;
791 is_packidx_filename(const char *name, size_t len)
793 if (len != GOT_PACKIDX_NAMELEN)
796 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
799 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
800 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
806 const struct got_error *
807 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
808 struct got_repository *repo, struct got_object_id *id)
810 const struct got_error *err;
817 /* Search pack index cache. */
818 for (i = 0; i < nitems(repo->packidx_cache); i++) {
819 if (repo->packidx_cache[i] == NULL)
821 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
823 *packidx = repo->packidx_cache[i];
825 * Move this cache entry to the front. Repeatedly
826 * searching a wrong pack index can be expensive.
829 struct got_packidx *p;
830 p = repo->packidx_cache[0];
831 repo->packidx_cache[0] = *packidx;
832 repo->packidx_cache[i] = p;
837 /* No luck. Search the filesystem. */
839 path_packdir = got_repo_get_path_objects_pack(repo);
840 if (path_packdir == NULL)
841 return got_error_from_errno("got_repo_get_path_objects_pack");
843 packdir = opendir(path_packdir);
844 if (packdir == NULL) {
846 err = got_error_no_obj(id);
848 err = got_error_from_errno2("opendir", path_packdir);
852 while ((dent = readdir(packdir)) != NULL) {
855 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
858 if (asprintf(&path_packidx, "%s/%s", path_packdir,
859 dent->d_name) == -1) {
860 err = got_error_from_errno("asprintf");
864 for (i = 0; i < nitems(repo->packidx_cache); i++) {
865 if (repo->packidx_cache[i] == NULL)
867 if (strcmp(repo->packidx_cache[i]->path_packidx,
868 path_packidx) == 0) {
875 continue; /* already searched */
878 err = got_packidx_open(packidx, path_packidx, 0);
884 err = cache_packidx(repo, *packidx, path_packidx);
889 *idx = got_packidx_get_object_idx(*packidx, id);
891 err = NULL; /* found the object */
896 err = got_error_no_obj(id);
899 if (packdir && closedir(packdir) != 0 && err == NULL)
900 err = got_error_from_errno("closedir");
904 static const struct got_error *
905 read_packfile_hdr(int fd, struct got_packidx *packidx)
907 const struct got_error *err = NULL;
908 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
909 struct got_packfile_hdr hdr;
912 n = read(fd, &hdr, sizeof(hdr));
914 return got_error_from_errno("read");
915 if (n != sizeof(hdr))
916 return got_error(GOT_ERR_BAD_PACKFILE);
918 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
919 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
920 betoh32(hdr.nobjects) != totobj)
921 err = got_error(GOT_ERR_BAD_PACKFILE);
926 static const struct got_error *
927 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
929 const struct got_error *err = NULL;
931 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
933 return got_error_from_errno2("open", path_packfile);
936 err = read_packfile_hdr(*fd, packidx);
946 const struct got_error *
947 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
948 const char *path_packfile, struct got_packidx *packidx)
950 const struct got_error *err = NULL;
951 struct got_pack *pack = NULL;
958 for (i = 0; i < nitems(repo->packs); i++) {
959 pack = &repo->packs[i];
960 if (pack->path_packfile == NULL)
962 if (strcmp(pack->path_packfile, path_packfile) == 0)
963 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
966 if (i == nitems(repo->packs) - 1) {
967 err = got_pack_close(&repo->packs[i - 1]);
970 memmove(&repo->packs[1], &repo->packs[0],
971 sizeof(repo->packs) - sizeof(repo->packs[0]));
975 pack = &repo->packs[i];
977 pack->path_packfile = strdup(path_packfile);
978 if (pack->path_packfile == NULL) {
979 err = got_error_from_errno("strdup");
983 err = open_packfile(&pack->fd, path_packfile, packidx);
987 if (fstat(pack->fd, &sb) != 0) {
988 err = got_error_from_errno("fstat");
991 pack->filesize = sb.st_size;
993 pack->privsep_child = NULL;
995 #ifndef GOT_PACK_NO_MMAP
996 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
998 if (pack->map == MAP_FAILED) {
999 if (errno != ENOMEM) {
1000 err = got_error_from_errno("mmap");
1003 pack->map = NULL; /* fall back to read(2) */
1009 free(pack->path_packfile);
1010 memset(pack, 0, sizeof(*pack));
1018 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1020 struct got_pack *pack = NULL;
1023 for (i = 0; i < nitems(repo->packs); i++) {
1024 pack = &repo->packs[i];
1025 if (pack->path_packfile == NULL)
1027 if (strcmp(pack->path_packfile, path_packfile) == 0)
1034 const struct got_error *
1035 got_repo_init(const char *repo_path)
1037 const struct got_error *err = NULL;
1038 const char *dirnames[] = {
1040 GOT_OBJECTS_PACK_DIR,
1043 const char *description_str = "Unnamed repository; "
1044 "edit this file 'description' to name the repository.";
1045 const char *headref_str = "ref: refs/heads/main";
1046 const char *gitconfig_str = "[core]\n"
1047 "\trepositoryformatversion = 0\n"
1048 "\tfilemode = true\n"
1053 if (!got_path_dir_is_empty(repo_path))
1054 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1056 for (i = 0; i < nitems(dirnames); i++) {
1057 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1058 return got_error_from_errno("asprintf");
1060 err = got_path_mkdir(path);
1066 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1067 return got_error_from_errno("asprintf");
1068 err = got_path_create_file(path, description_str);
1073 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1074 return got_error_from_errno("asprintf");
1075 err = got_path_create_file(path, headref_str);
1080 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1081 return got_error_from_errno("asprintf");
1082 err = got_path_create_file(path, gitconfig_str);
1090 static const struct got_error *
1091 match_packed_object(struct got_object_id **unique_id,
1092 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1094 const struct got_error *err = NULL;
1097 struct dirent *dent;
1099 struct got_object_id_queue matched_ids;
1101 SIMPLEQ_INIT(&matched_ids);
1103 path_packdir = got_repo_get_path_objects_pack(repo);
1104 if (path_packdir == NULL)
1105 return got_error_from_errno("got_repo_get_path_objects_pack");
1107 packdir = opendir(path_packdir);
1108 if (packdir == NULL) {
1109 if (errno != ENOENT)
1110 err = got_error_from_errno2("opendir", path_packdir);
1114 while ((dent = readdir(packdir)) != NULL) {
1115 struct got_packidx *packidx;
1116 struct got_object_qid *qid;
1119 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1122 if (asprintf(&path_packidx, "%s/%s", path_packdir,
1123 dent->d_name) == -1) {
1124 err = got_error_from_errno("asprintf");
1128 err = got_packidx_open(&packidx, path_packidx, 0);
1133 err = got_packidx_match_id_str_prefix(&matched_ids,
1134 packidx, id_str_prefix);
1136 got_packidx_close(packidx);
1139 err = got_packidx_close(packidx);
1143 SIMPLEQ_FOREACH(qid, &matched_ids, entry) {
1144 if (obj_type != GOT_OBJ_TYPE_ANY) {
1146 err = got_object_get_type(&matched_type, repo,
1150 if (matched_type != obj_type)
1153 if (*unique_id == NULL) {
1154 *unique_id = got_object_id_dup(qid->id);
1155 if (*unique_id == NULL) {
1156 err = got_error_from_errno("malloc");
1160 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1161 continue; /* packed multiple times */
1162 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1168 got_object_id_queue_free(&matched_ids);
1170 if (packdir && closedir(packdir) != 0 && err == NULL)
1171 err = got_error_from_errno("closedir");
1179 static const struct got_error *
1180 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1181 const char *object_dir, const char *id_str_prefix, int obj_type,
1182 struct got_repository *repo)
1184 const struct got_error *err = NULL;
1187 struct dirent *dent;
1188 struct got_object_id id;
1190 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1191 err = got_error_from_errno("asprintf");
1195 dir = opendir(path);
1197 if (errno == ENOENT) {
1201 err = got_error_from_errno2("opendir", path);
1204 while ((dent = readdir(dir)) != NULL) {
1208 if (strcmp(dent->d_name, ".") == 0 ||
1209 strcmp(dent->d_name, "..") == 0)
1212 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1213 err = got_error_from_errno("asprintf");
1217 if (!got_parse_sha1_digest(id.sha1, id_str))
1221 * Directory entries do not necessarily appear in
1222 * sorted order, so we must iterate over all of them.
1224 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1230 if (*unique_id == NULL) {
1231 if (obj_type != GOT_OBJ_TYPE_ANY) {
1233 err = got_object_get_type(&matched_type, repo,
1237 if (matched_type != obj_type)
1240 *unique_id = got_object_id_dup(&id);
1241 if (*unique_id == NULL) {
1242 err = got_error_from_errno("got_object_id_dup");
1247 if (got_object_id_cmp(*unique_id, &id) == 0)
1248 continue; /* both packed and loose */
1249 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1255 if (dir && closedir(dir) != 0 && err == NULL)
1256 err = got_error_from_errno("closedir");
1265 const struct got_error *
1266 got_repo_match_object_id_prefix(struct got_object_id **id,
1267 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1269 const struct got_error *err = NULL;
1270 char *path_objects = got_repo_get_path_objects(repo);
1271 char *object_dir = NULL;
1277 for (i = 0; i < strlen(id_str_prefix); i++) {
1278 if (isxdigit((unsigned char)id_str_prefix[i]))
1280 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1283 len = strlen(id_str_prefix);
1285 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1288 object_dir = strndup(id_str_prefix, 2);
1289 if (object_dir == NULL) {
1290 err = got_error_from_errno("strdup");
1293 err = match_loose_object(id, path_objects, object_dir,
1294 id_str_prefix, obj_type, repo);
1295 } else if (len == 1) {
1297 for (i = 0; i < 0xf; i++) {
1298 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1300 err = got_error_from_errno("asprintf");
1303 err = match_packed_object(id, repo, object_dir,
1307 err = match_loose_object(id, path_objects, object_dir,
1308 id_str_prefix, obj_type, repo);
1313 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1321 } else if (*id == NULL)
1322 err = got_error(GOT_ERR_NO_OBJ);
1327 const struct got_error *
1328 got_repo_match_object_id(struct got_object_id **id, char **label,
1329 const char *id_str, int obj_type, int resolve_tags,
1330 struct got_repository *repo)
1332 const struct got_error *err;
1333 struct got_tag_object *tag;
1334 struct got_reference *ref = NULL;
1341 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
1344 *id = got_object_id_dup(
1345 got_object_tag_get_object_id(tag));
1347 err = got_error_from_errno("got_object_id_dup");
1348 else if (label && asprintf(label, "refs/tags/%s",
1349 got_object_tag_get_name(tag)) == -1) {
1350 err = got_error_from_errno("asprintf");
1354 got_object_tag_close(tag);
1356 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1357 err->code != GOT_ERR_NO_OBJ)
1361 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1363 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1365 err = got_ref_open(&ref, repo, id_str, 0);
1369 *label = strdup(got_ref_get_name(ref));
1370 if (*label == NULL) {
1371 err = got_error_from_errno("strdup");
1375 err = got_ref_resolve(id, repo, ref);
1377 err = got_object_id_str(label, *id);
1378 if (*label == NULL) {
1379 err = got_error_from_errno("strdup");
1389 const struct got_error *
1390 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1391 int obj_type, struct got_repository *repo)
1393 const struct got_error *err;
1394 struct got_reflist_head refs;
1395 struct got_reflist_entry *re;
1396 struct got_object_id *tag_id;
1398 SIMPLEQ_INIT(&refs);
1401 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_by_name, NULL);
1405 SIMPLEQ_FOREACH(re, &refs, entry) {
1406 const char *refname;
1407 refname = got_ref_get_name(re->ref);
1408 if (got_ref_is_symbolic(re->ref))
1410 refname += strlen("refs/tags/");
1411 if (strcmp(refname, name) != 0)
1413 err = got_ref_resolve(&tag_id, repo, re->ref);
1416 err = got_object_open_as_tag(tag, repo, tag_id);
1420 if (obj_type == GOT_OBJ_TYPE_ANY ||
1421 got_object_tag_get_object_type(*tag) == obj_type)
1423 got_object_tag_close(*tag);
1427 got_ref_list_free(&refs);
1428 if (err == NULL && *tag == NULL)
1429 err = got_error(GOT_ERR_NO_OBJ);
1433 static const struct got_error *
1434 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1435 const char *name, mode_t mode, struct got_object_id *blob_id)
1437 const struct got_error *err = NULL;
1441 *new_te = calloc(1, sizeof(**new_te));
1442 if (*new_te == NULL)
1443 return got_error_from_errno("calloc");
1445 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1446 sizeof((*new_te)->name)) {
1447 err = got_error(GOT_ERR_NO_SPACE);
1451 (*new_te)->mode = S_IFREG | (mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
1452 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1454 if (err && *new_te) {
1461 static const struct got_error *
1462 import_file(struct got_tree_entry **new_te, struct dirent *de,
1463 const char *path, struct got_repository *repo)
1465 const struct got_error *err;
1466 struct got_object_id *blob_id = NULL;
1470 if (asprintf(&filepath, "%s%s%s", path,
1471 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1472 return got_error_from_errno("asprintf");
1474 if (lstat(filepath, &sb) != 0) {
1475 err = got_error_from_errno2("lstat", path);
1479 err = got_object_blob_create(&blob_id, filepath, repo);
1483 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1492 static const struct got_error *
1493 insert_tree_entry(struct got_tree_entry *new_te,
1494 struct got_pathlist_head *paths)
1496 const struct got_error *err = NULL;
1497 struct got_pathlist_entry *new_pe;
1499 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1503 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1507 static const struct got_error *write_tree(struct got_object_id **,
1508 const char *, struct got_pathlist_head *, struct got_repository *,
1509 got_repo_import_cb progress_cb, void *progress_arg);
1511 static const struct got_error *
1512 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1513 const char *path, struct got_pathlist_head *ignores,
1514 struct got_repository *repo,
1515 got_repo_import_cb progress_cb, void *progress_arg)
1517 const struct got_error *err;
1518 struct got_object_id *id = NULL;
1521 if (asprintf(&subdirpath, "%s%s%s", path,
1522 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1523 return got_error_from_errno("asprintf");
1525 (*new_te) = calloc(1, sizeof(**new_te));
1526 if (*new_te == NULL)
1527 return got_error_from_errno("calloc");
1528 (*new_te)->mode = S_IFDIR;
1529 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1530 sizeof((*new_te)->name)) {
1531 err = got_error(GOT_ERR_NO_SPACE);
1534 err = write_tree(&id, subdirpath, ignores, repo,
1535 progress_cb, progress_arg);
1538 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1550 static const struct got_error *
1551 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1552 struct got_pathlist_head *ignores, struct got_repository *repo,
1553 got_repo_import_cb progress_cb, void *progress_arg)
1555 const struct got_error *err = NULL;
1559 struct got_tree_entry *new_te = NULL;
1560 struct got_pathlist_head paths;
1561 struct got_pathlist_entry *pe;
1563 *new_tree_id = NULL;
1567 dir = opendir(path_dir);
1569 err = got_error_from_errno2("opendir", path_dir);
1574 while ((de = readdir(dir)) != NULL) {
1577 if (strcmp(de->d_name, ".") == 0 ||
1578 strcmp(de->d_name, "..") == 0)
1581 TAILQ_FOREACH(pe, ignores, entry) {
1582 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1589 if (de->d_type == DT_DIR) {
1590 err = import_subdir(&new_te, de, path_dir,
1591 ignores, repo, progress_cb, progress_arg);
1593 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1598 } else if (de->d_type == DT_REG) {
1599 err = import_file(&new_te, de, path_dir, repo);
1605 err = insert_tree_entry(new_te, &paths);
1611 if (TAILQ_EMPTY(&paths)) {
1612 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1616 TAILQ_FOREACH(pe, &paths, entry) {
1617 struct got_tree_entry *te = pe->data;
1619 if (!S_ISREG(te->mode))
1621 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1622 err = got_error_from_errno("asprintf");
1625 err = (*progress_cb)(progress_arg, path);
1631 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1635 got_pathlist_free(&paths);
1639 const struct got_error *
1640 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1641 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1642 struct got_repository *repo, got_repo_import_cb progress_cb,
1645 const struct got_error *err;
1646 struct got_object_id *new_tree_id;
1648 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1649 progress_cb, progress_arg);
1653 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1654 author, time(NULL), author, time(NULL), logmsg, repo);