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>
44 #include "got_error.h"
45 #include "got_reference.h"
46 #include "got_repository.h"
48 #include "got_cancel.h"
49 #include "got_worktree.h"
50 #include "got_object.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_inflate.h"
54 #include "got_lib_object.h"
55 #include "got_lib_object_parse.h"
56 #include "got_lib_object_create.h"
57 #include "got_lib_pack.h"
58 #include "got_lib_privsep.h"
59 #include "got_lib_worktree.h"
60 #include "got_lib_sha1.h"
61 #include "got_lib_object_cache.h"
62 #include "got_lib_repository.h"
65 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
69 got_repo_get_path(struct got_repository *repo)
75 got_repo_get_path_git_dir(struct got_repository *repo)
77 return repo->path_git_dir;
81 got_repo_get_gitconfig_author_name(struct got_repository *repo)
83 return repo->gitconfig_author_name;
87 got_repo_get_gitconfig_author_email(struct got_repository *repo)
89 return repo->gitconfig_author_email;
93 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
95 return repo->global_gitconfig_author_name;
99 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
101 return repo->global_gitconfig_author_email;
105 got_repo_get_gitconfig_owner(struct got_repository *repo)
107 return repo->gitconfig_owner;
111 got_repo_is_bare(struct got_repository *repo)
113 return (strcmp(repo->path, repo->path_git_dir) == 0);
117 get_path_git_child(struct got_repository *repo, const char *basename)
121 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
129 got_repo_get_path_objects(struct got_repository *repo)
131 return get_path_git_child(repo, GOT_OBJECTS_DIR);
135 got_repo_get_path_objects_pack(struct got_repository *repo)
137 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
141 got_repo_get_path_refs(struct got_repository *repo)
143 return get_path_git_child(repo, GOT_REFS_DIR);
147 got_repo_get_path_packed_refs(struct got_repository *repo)
149 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
153 get_path_head(struct got_repository *repo)
155 return get_path_git_child(repo, GOT_HEAD_FILE);
159 got_repo_get_path_gitconfig(struct got_repository *repo)
161 return get_path_git_child(repo, GOT_GITCONFIG);
165 got_repo_get_gitconfig_remotes(int *nremotes, struct got_remote_repo **remotes,
166 struct got_repository *repo)
168 *nremotes = repo->ngitconfig_remotes;
169 *remotes = repo->gitconfig_remotes;
173 is_git_repo(struct got_repository *repo)
175 const char *path_git = got_repo_get_path_git_dir(repo);
176 char *path_objects = got_repo_get_path_objects(repo);
177 char *path_refs = got_repo_get_path_refs(repo);
178 char *path_head = get_path_head(repo);
181 struct got_reference *head_ref;
183 if (lstat(path_git, &sb) == -1)
185 if (!S_ISDIR(sb.st_mode))
188 if (lstat(path_objects, &sb) == -1)
190 if (!S_ISDIR(sb.st_mode))
193 if (lstat(path_refs, &sb) == -1)
195 if (!S_ISDIR(sb.st_mode))
198 if (lstat(path_head, &sb) == -1)
200 if (!S_ISREG(sb.st_mode))
203 /* Check if the HEAD reference can be opened. */
204 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
206 got_ref_close(head_ref);
217 const struct got_error *
218 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
219 struct got_object *obj)
221 #ifndef GOT_NO_OBJ_CACHE
222 const struct got_error *err = NULL;
223 err = got_object_cache_add(&repo->objcache, id, obj);
225 if (err->code == GOT_ERR_OBJ_EXISTS ||
226 err->code == GOT_ERR_OBJ_TOO_LARGE)
236 got_repo_get_cached_object(struct got_repository *repo,
237 struct got_object_id *id)
239 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
242 const struct got_error *
243 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
244 struct got_tree_object *tree)
246 #ifndef GOT_NO_OBJ_CACHE
247 const struct got_error *err = NULL;
248 err = got_object_cache_add(&repo->treecache, id, tree);
250 if (err->code == GOT_ERR_OBJ_EXISTS ||
251 err->code == GOT_ERR_OBJ_TOO_LARGE)
260 struct got_tree_object *
261 got_repo_get_cached_tree(struct got_repository *repo,
262 struct got_object_id *id)
264 return (struct got_tree_object *)got_object_cache_get(
265 &repo->treecache, id);
268 const struct got_error *
269 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
270 struct got_commit_object *commit)
272 #ifndef GOT_NO_OBJ_CACHE
273 const struct got_error *err = NULL;
274 err = got_object_cache_add(&repo->commitcache, id, commit);
276 if (err->code == GOT_ERR_OBJ_EXISTS ||
277 err->code == GOT_ERR_OBJ_TOO_LARGE)
286 struct got_commit_object *
287 got_repo_get_cached_commit(struct got_repository *repo,
288 struct got_object_id *id)
290 return (struct got_commit_object *)got_object_cache_get(
291 &repo->commitcache, id);
294 const struct got_error *
295 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
296 struct got_tag_object *tag)
298 #ifndef GOT_NO_OBJ_CACHE
299 const struct got_error *err = NULL;
300 err = got_object_cache_add(&repo->tagcache, id, tag);
302 if (err->code == GOT_ERR_OBJ_EXISTS ||
303 err->code == GOT_ERR_OBJ_TOO_LARGE)
312 struct got_tag_object *
313 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
315 return (struct got_tag_object *)got_object_cache_get(
316 &repo->tagcache, id);
319 const struct got_error *
320 open_repo(struct got_repository *repo, const char *path)
322 const struct got_error *err = NULL;
324 /* bare git repository? */
325 repo->path_git_dir = strdup(path);
326 if (repo->path_git_dir == NULL)
327 return got_error_from_errno("strdup");
328 if (is_git_repo(repo)) {
329 repo->path = strdup(repo->path_git_dir);
330 if (repo->path == NULL) {
331 err = got_error_from_errno("strdup");
337 /* git repository with working tree? */
338 free(repo->path_git_dir);
339 repo->path_git_dir = NULL;
340 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
341 err = got_error_from_errno("asprintf");
344 if (is_git_repo(repo)) {
345 repo->path = strdup(path);
346 if (repo->path == NULL) {
347 err = got_error_from_errno("strdup");
353 err = got_error(GOT_ERR_NOT_GIT_REPO);
358 free(repo->path_git_dir);
359 repo->path_git_dir = NULL;
364 static const struct got_error *
365 parse_gitconfig_file(int *gitconfig_repository_format_version,
366 char **gitconfig_author_name, char **gitconfig_author_email,
367 struct got_remote_repo **remotes, int *nremotes,
368 char **gitconfig_owner,
369 const char *gitconfig_path)
371 const struct got_error *err = NULL, *child_err = NULL;
373 int imsg_fds[2] = { -1, -1 };
375 struct imsgbuf *ibuf;
377 *gitconfig_repository_format_version = 0;
378 *gitconfig_author_name = NULL;
379 *gitconfig_author_email = NULL;
385 *gitconfig_owner = NULL;
387 fd = open(gitconfig_path, O_RDONLY);
391 return got_error_from_errno2("open", gitconfig_path);
394 ibuf = calloc(1, sizeof(*ibuf));
396 err = got_error_from_errno("calloc");
400 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
401 err = got_error_from_errno("socketpair");
407 err = got_error_from_errno("fork");
409 } else if (pid == 0) {
410 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
415 if (close(imsg_fds[1]) == -1) {
416 err = got_error_from_errno("close");
420 imsg_init(ibuf, imsg_fds[0]);
422 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
427 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
431 err = got_privsep_recv_gitconfig_int(
432 gitconfig_repository_format_version, ibuf);
436 err = got_privsep_send_gitconfig_author_name_req(ibuf);
440 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
444 err = got_privsep_send_gitconfig_author_email_req(ibuf);
448 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
452 if (remotes && nremotes) {
453 err = got_privsep_send_gitconfig_remotes_req(ibuf);
457 err = got_privsep_recv_gitconfig_remotes(remotes,
463 if (gitconfig_owner) {
464 err = got_privsep_send_gitconfig_owner_req(ibuf);
467 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
473 err = got_privsep_send_stop(imsg_fds[0]);
474 child_err = got_privsep_wait_for_child(pid);
475 if (child_err && err == NULL)
478 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
479 err = got_error_from_errno("close");
480 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
481 err = got_error_from_errno("close");
482 if (fd != -1 && close(fd) == -1 && err == NULL)
483 err = got_error_from_errno2("close", gitconfig_path);
488 static const struct got_error *
489 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
491 const struct got_error *err = NULL;
492 char *repo_gitconfig_path = NULL;
494 if (global_gitconfig_path) {
495 /* Read settings from ~/.gitconfig. */
496 int dummy_repo_version;
497 err = parse_gitconfig_file(&dummy_repo_version,
498 &repo->global_gitconfig_author_name,
499 &repo->global_gitconfig_author_email,
500 NULL, NULL, NULL, global_gitconfig_path);
505 /* Read repository's .git/config file. */
506 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
507 if (repo_gitconfig_path == NULL)
508 return got_error_from_errno("got_repo_get_path_gitconfig");
510 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
511 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
512 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
513 &repo->gitconfig_owner, repo_gitconfig_path);
517 free(repo_gitconfig_path);
521 const struct got_error *
522 got_repo_open(struct got_repository **repop, const char *path,
523 const char *global_gitconfig_path)
525 struct got_repository *repo = NULL;
526 const struct got_error *err = NULL;
528 int i, tried_root = 0;
532 if (got_path_is_absolute(path))
533 abspath = strdup(path);
535 abspath = got_path_get_absolute(path);
537 return got_error_path(path, GOT_ERR_BAD_PATH);
539 repo = calloc(1, sizeof(*repo));
541 err = got_error_from_errno("calloc");
545 for (i = 0; i < nitems(repo->privsep_children); i++) {
546 memset(&repo->privsep_children[i], 0,
547 sizeof(repo->privsep_children[0]));
548 repo->privsep_children[i].imsg_fd = -1;
551 err = got_object_cache_init(&repo->objcache,
552 GOT_OBJECT_CACHE_TYPE_OBJ);
555 err = got_object_cache_init(&repo->treecache,
556 GOT_OBJECT_CACHE_TYPE_TREE);
559 err = got_object_cache_init(&repo->commitcache,
560 GOT_OBJECT_CACHE_TYPE_COMMIT);
563 err = got_object_cache_init(&repo->tagcache,
564 GOT_OBJECT_CACHE_TYPE_TAG);
568 path = realpath(abspath, NULL);
570 err = got_error_from_errno2("realpath", abspath);
575 err = open_repo(repo, path);
578 if (err->code != GOT_ERR_NOT_GIT_REPO)
580 if (path[0] == '/' && path[1] == '\0') {
582 err = got_error(GOT_ERR_NOT_GIT_REPO);
587 path = dirname(path);
589 err = got_error_from_errno2("dirname", path);
594 err = read_gitconfig(repo, global_gitconfig_path);
597 if (repo->gitconfig_repository_format_version != 0)
598 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
601 got_repo_close(repo);
608 const struct got_error *
609 got_repo_close(struct got_repository *repo)
611 const struct got_error *err = NULL, *child_err;
614 for (i = 0; i < nitems(repo->packidx_cache); i++) {
615 if (repo->packidx_cache[i] == NULL)
617 got_packidx_close(repo->packidx_cache[i]);
620 for (i = 0; i < nitems(repo->packs); i++) {
621 if (repo->packs[i].path_packfile == NULL)
623 got_pack_close(&repo->packs[i]);
627 free(repo->path_git_dir);
629 got_object_cache_close(&repo->objcache);
630 got_object_cache_close(&repo->treecache);
631 got_object_cache_close(&repo->commitcache);
632 got_object_cache_close(&repo->tagcache);
634 for (i = 0; i < nitems(repo->privsep_children); i++) {
635 if (repo->privsep_children[i].imsg_fd == -1)
637 imsg_clear(repo->privsep_children[i].ibuf);
638 free(repo->privsep_children[i].ibuf);
639 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
640 child_err = got_privsep_wait_for_child(
641 repo->privsep_children[i].pid);
642 if (child_err && err == NULL)
644 if (close(repo->privsep_children[i].imsg_fd) != 0 &&
646 err = got_error_from_errno("close");
649 free(repo->gitconfig_author_name);
650 free(repo->gitconfig_author_email);
651 for (i = 0; i < repo->ngitconfig_remotes; i++) {
652 free(repo->gitconfig_remotes[i].name);
653 free(repo->gitconfig_remotes[i].url);
655 free(repo->gitconfig_remotes);
661 const struct got_error *
662 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
663 const char *input_path, int check_disk)
665 const struct got_error *err = NULL;
666 const char *repo_abspath = NULL;
668 char *canonpath, *path = NULL;
670 *in_repo_path = NULL;
672 canonpath = strdup(input_path);
673 if (canonpath == NULL) {
674 err = got_error_from_errno("strdup");
677 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
681 repo_abspath = got_repo_get_path(repo);
683 if (!check_disk || canonpath[0] == '\0') {
684 path = strdup(canonpath);
686 err = got_error_from_errno("strdup");
690 path = realpath(canonpath, NULL);
692 if (errno != ENOENT) {
693 err = got_error_from_errno2("realpath",
698 * Path is not on disk.
699 * Assume it is already relative to repository root.
701 path = strdup(canonpath);
703 err = got_error_from_errno("strdup");
708 repolen = strlen(repo_abspath);
712 if (strcmp(path, repo_abspath) == 0) {
716 err = got_error_from_errno("strdup");
719 } else if (len > repolen &&
720 got_path_is_child(path, repo_abspath, repolen)) {
721 /* Matched an on-disk path inside repository. */
722 if (got_repo_is_bare(repo)) {
724 * Matched an on-disk path inside repository
725 * database. Treat as repository-relative.
729 /* Strip common prefix with repository path. */
730 err = got_path_skip_common_ancestor(&child,
739 * Matched unrelated on-disk path.
740 * Treat it as repository-relative.
745 /* Make in-repository path absolute */
746 if (path[0] != '/') {
748 if (asprintf(&abspath, "/%s", path) == -1) {
749 err = got_error_from_errno("asprintf");
761 *in_repo_path = path;
765 static const struct got_error *
766 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
767 const char *path_packidx)
769 const struct got_error *err = NULL;
772 for (i = 0; i < nitems(repo->packidx_cache); i++) {
773 if (repo->packidx_cache[i] == NULL)
775 if (strcmp(repo->packidx_cache[i]->path_packidx,
776 path_packidx) == 0) {
777 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
780 if (i == nitems(repo->packidx_cache)) {
781 err = got_packidx_close(repo->packidx_cache[i - 1]);
787 * Insert the new pack index at the front so it will
788 * be searched first in the future.
790 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
791 sizeof(repo->packidx_cache) -
792 sizeof(repo->packidx_cache[0]));
793 repo->packidx_cache[0] = packidx;
799 is_packidx_filename(const char *name, size_t len)
801 if (len != GOT_PACKIDX_NAMELEN)
804 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
807 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
808 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
814 const struct got_error *
815 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
816 struct got_repository *repo, struct got_object_id *id)
818 const struct got_error *err;
825 /* Search pack index cache. */
826 for (i = 0; i < nitems(repo->packidx_cache); i++) {
827 if (repo->packidx_cache[i] == NULL)
829 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
831 *packidx = repo->packidx_cache[i];
833 * Move this cache entry to the front. Repeatedly
834 * searching a wrong pack index can be expensive.
837 struct got_packidx *p;
838 p = repo->packidx_cache[0];
839 repo->packidx_cache[0] = *packidx;
840 repo->packidx_cache[i] = p;
845 /* No luck. Search the filesystem. */
847 path_packdir = got_repo_get_path_objects_pack(repo);
848 if (path_packdir == NULL)
849 return got_error_from_errno("got_repo_get_path_objects_pack");
851 packdir = opendir(path_packdir);
852 if (packdir == NULL) {
854 err = got_error_no_obj(id);
856 err = got_error_from_errno2("opendir", path_packdir);
860 while ((dent = readdir(packdir)) != NULL) {
863 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
866 if (asprintf(&path_packidx, "%s/%s", path_packdir,
867 dent->d_name) == -1) {
868 err = got_error_from_errno("asprintf");
872 for (i = 0; i < nitems(repo->packidx_cache); i++) {
873 if (repo->packidx_cache[i] == NULL)
875 if (strcmp(repo->packidx_cache[i]->path_packidx,
876 path_packidx) == 0) {
883 continue; /* already searched */
886 err = got_packidx_open(packidx, path_packidx, 0);
892 err = cache_packidx(repo, *packidx, path_packidx);
897 *idx = got_packidx_get_object_idx(*packidx, id);
899 err = NULL; /* found the object */
904 err = got_error_no_obj(id);
907 if (packdir && closedir(packdir) != 0 && err == NULL)
908 err = got_error_from_errno("closedir");
912 static const struct got_error *
913 read_packfile_hdr(int fd, struct got_packidx *packidx)
915 const struct got_error *err = NULL;
916 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
917 struct got_packfile_hdr hdr;
920 n = read(fd, &hdr, sizeof(hdr));
922 return got_error_from_errno("read");
923 if (n != sizeof(hdr))
924 return got_error(GOT_ERR_BAD_PACKFILE);
926 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
927 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
928 be32toh(hdr.nobjects) != totobj)
929 err = got_error(GOT_ERR_BAD_PACKFILE);
934 static const struct got_error *
935 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
937 const struct got_error *err = NULL;
939 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
941 return got_error_from_errno2("open", path_packfile);
944 err = read_packfile_hdr(*fd, packidx);
954 const struct got_error *
955 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
956 const char *path_packfile, struct got_packidx *packidx)
958 const struct got_error *err = NULL;
959 struct got_pack *pack = NULL;
966 for (i = 0; i < nitems(repo->packs); i++) {
967 pack = &repo->packs[i];
968 if (pack->path_packfile == NULL)
970 if (strcmp(pack->path_packfile, path_packfile) == 0)
971 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
974 if (i == nitems(repo->packs) - 1) {
975 err = got_pack_close(&repo->packs[i - 1]);
978 memmove(&repo->packs[1], &repo->packs[0],
979 sizeof(repo->packs) - sizeof(repo->packs[0]));
983 pack = &repo->packs[i];
985 pack->path_packfile = strdup(path_packfile);
986 if (pack->path_packfile == NULL) {
987 err = got_error_from_errno("strdup");
991 err = open_packfile(&pack->fd, path_packfile, packidx);
995 if (fstat(pack->fd, &sb) != 0) {
996 err = got_error_from_errno("fstat");
999 pack->filesize = sb.st_size;
1001 pack->privsep_child = NULL;
1003 #ifndef GOT_PACK_NO_MMAP
1004 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1006 if (pack->map == MAP_FAILED) {
1007 if (errno != ENOMEM) {
1008 err = got_error_from_errno("mmap");
1011 pack->map = NULL; /* fall back to read(2) */
1017 free(pack->path_packfile);
1018 memset(pack, 0, sizeof(*pack));
1026 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1028 struct got_pack *pack = NULL;
1031 for (i = 0; i < nitems(repo->packs); i++) {
1032 pack = &repo->packs[i];
1033 if (pack->path_packfile == NULL)
1035 if (strcmp(pack->path_packfile, path_packfile) == 0)
1042 const struct got_error *
1043 got_repo_init(const char *repo_path)
1045 const struct got_error *err = NULL;
1046 const char *dirnames[] = {
1048 GOT_OBJECTS_PACK_DIR,
1051 const char *description_str = "Unnamed repository; "
1052 "edit this file 'description' to name the repository.";
1053 const char *headref_str = "ref: refs/heads/main";
1054 const char *gitconfig_str = "[core]\n"
1055 "\trepositoryformatversion = 0\n"
1056 "\tfilemode = true\n"
1061 if (!got_path_dir_is_empty(repo_path))
1062 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1064 for (i = 0; i < nitems(dirnames); i++) {
1065 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1066 return got_error_from_errno("asprintf");
1068 err = got_path_mkdir(path);
1074 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1075 return got_error_from_errno("asprintf");
1076 err = got_path_create_file(path, description_str);
1081 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1082 return got_error_from_errno("asprintf");
1083 err = got_path_create_file(path, headref_str);
1088 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1089 return got_error_from_errno("asprintf");
1090 err = got_path_create_file(path, gitconfig_str);
1098 static const struct got_error *
1099 match_packed_object(struct got_object_id **unique_id,
1100 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1102 const struct got_error *err = NULL;
1105 struct dirent *dent;
1107 struct got_object_id_queue matched_ids;
1109 SIMPLEQ_INIT(&matched_ids);
1111 path_packdir = got_repo_get_path_objects_pack(repo);
1112 if (path_packdir == NULL)
1113 return got_error_from_errno("got_repo_get_path_objects_pack");
1115 packdir = opendir(path_packdir);
1116 if (packdir == NULL) {
1117 if (errno != ENOENT)
1118 err = got_error_from_errno2("opendir", path_packdir);
1122 while ((dent = readdir(packdir)) != NULL) {
1123 struct got_packidx *packidx;
1124 struct got_object_qid *qid;
1127 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1130 if (asprintf(&path_packidx, "%s/%s", path_packdir,
1131 dent->d_name) == -1) {
1132 err = got_error_from_errno("asprintf");
1136 err = got_packidx_open(&packidx, path_packidx, 0);
1141 err = got_packidx_match_id_str_prefix(&matched_ids,
1142 packidx, id_str_prefix);
1144 got_packidx_close(packidx);
1147 err = got_packidx_close(packidx);
1151 SIMPLEQ_FOREACH(qid, &matched_ids, entry) {
1152 if (obj_type != GOT_OBJ_TYPE_ANY) {
1154 err = got_object_get_type(&matched_type, repo,
1158 if (matched_type != obj_type)
1161 if (*unique_id == NULL) {
1162 *unique_id = got_object_id_dup(qid->id);
1163 if (*unique_id == NULL) {
1164 err = got_error_from_errno("malloc");
1168 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1169 continue; /* packed multiple times */
1170 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1176 got_object_id_queue_free(&matched_ids);
1178 if (packdir && closedir(packdir) != 0 && err == NULL)
1179 err = got_error_from_errno("closedir");
1187 static const struct got_error *
1188 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1189 const char *object_dir, const char *id_str_prefix, int obj_type,
1190 struct got_repository *repo)
1192 const struct got_error *err = NULL;
1195 struct dirent *dent;
1196 struct got_object_id id;
1198 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1199 err = got_error_from_errno("asprintf");
1203 dir = opendir(path);
1205 if (errno == ENOENT) {
1209 err = got_error_from_errno2("opendir", path);
1212 while ((dent = readdir(dir)) != NULL) {
1216 if (strcmp(dent->d_name, ".") == 0 ||
1217 strcmp(dent->d_name, "..") == 0)
1220 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1221 err = got_error_from_errno("asprintf");
1225 if (!got_parse_sha1_digest(id.sha1, id_str))
1229 * Directory entries do not necessarily appear in
1230 * sorted order, so we must iterate over all of them.
1232 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1238 if (*unique_id == NULL) {
1239 if (obj_type != GOT_OBJ_TYPE_ANY) {
1241 err = got_object_get_type(&matched_type, repo,
1245 if (matched_type != obj_type)
1248 *unique_id = got_object_id_dup(&id);
1249 if (*unique_id == NULL) {
1250 err = got_error_from_errno("got_object_id_dup");
1255 if (got_object_id_cmp(*unique_id, &id) == 0)
1256 continue; /* both packed and loose */
1257 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1263 if (dir && closedir(dir) != 0 && err == NULL)
1264 err = got_error_from_errno("closedir");
1273 const struct got_error *
1274 got_repo_match_object_id_prefix(struct got_object_id **id,
1275 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1277 const struct got_error *err = NULL;
1278 char *path_objects = got_repo_get_path_objects(repo);
1279 char *object_dir = NULL;
1285 for (i = 0; i < strlen(id_str_prefix); i++) {
1286 if (isxdigit((unsigned char)id_str_prefix[i]))
1288 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1291 len = strlen(id_str_prefix);
1293 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1296 object_dir = strndup(id_str_prefix, 2);
1297 if (object_dir == NULL) {
1298 err = got_error_from_errno("strdup");
1301 err = match_loose_object(id, path_objects, object_dir,
1302 id_str_prefix, obj_type, repo);
1303 } else if (len == 1) {
1305 for (i = 0; i < 0xf; i++) {
1306 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1308 err = got_error_from_errno("asprintf");
1311 err = match_packed_object(id, repo, object_dir,
1315 err = match_loose_object(id, path_objects, object_dir,
1316 id_str_prefix, obj_type, repo);
1321 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1329 } else if (*id == NULL)
1330 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1335 const struct got_error *
1336 got_repo_match_object_id(struct got_object_id **id, char **label,
1337 const char *id_str, int obj_type, int resolve_tags,
1338 struct got_repository *repo)
1340 const struct got_error *err;
1341 struct got_tag_object *tag;
1342 struct got_reference *ref = NULL;
1349 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
1352 *id = got_object_id_dup(
1353 got_object_tag_get_object_id(tag));
1355 err = got_error_from_errno("got_object_id_dup");
1356 else if (label && asprintf(label, "refs/tags/%s",
1357 got_object_tag_get_name(tag)) == -1) {
1358 err = got_error_from_errno("asprintf");
1362 got_object_tag_close(tag);
1364 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1365 err->code != GOT_ERR_NO_OBJ)
1369 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1371 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1373 err = got_ref_open(&ref, repo, id_str, 0);
1377 *label = strdup(got_ref_get_name(ref));
1378 if (*label == NULL) {
1379 err = got_error_from_errno("strdup");
1383 err = got_ref_resolve(id, repo, ref);
1385 err = got_object_id_str(label, *id);
1386 if (*label == NULL) {
1387 err = got_error_from_errno("strdup");
1397 const struct got_error *
1398 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1399 int obj_type, struct got_repository *repo)
1401 const struct got_error *err;
1402 struct got_reflist_head refs;
1403 struct got_reflist_entry *re;
1404 struct got_object_id *tag_id;
1406 SIMPLEQ_INIT(&refs);
1409 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_by_name, NULL);
1413 SIMPLEQ_FOREACH(re, &refs, entry) {
1414 const char *refname;
1415 refname = got_ref_get_name(re->ref);
1416 if (got_ref_is_symbolic(re->ref))
1418 refname += strlen("refs/tags/");
1419 if (strcmp(refname, name) != 0)
1421 err = got_ref_resolve(&tag_id, repo, re->ref);
1424 err = got_object_open_as_tag(tag, repo, tag_id);
1428 if (obj_type == GOT_OBJ_TYPE_ANY ||
1429 got_object_tag_get_object_type(*tag) == obj_type)
1431 got_object_tag_close(*tag);
1435 got_ref_list_free(&refs);
1436 if (err == NULL && *tag == NULL)
1437 err = got_error_path(name, GOT_ERR_NO_OBJ);
1441 static const struct got_error *
1442 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1443 const char *name, mode_t mode, struct got_object_id *blob_id)
1445 const struct got_error *err = NULL;
1449 *new_te = calloc(1, sizeof(**new_te));
1450 if (*new_te == NULL)
1451 return got_error_from_errno("calloc");
1453 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1454 sizeof((*new_te)->name)) {
1455 err = got_error(GOT_ERR_NO_SPACE);
1459 if (S_ISLNK(mode)) {
1460 (*new_te)->mode = S_IFLNK;
1462 (*new_te)->mode = S_IFREG;
1463 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1465 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1467 if (err && *new_te) {
1474 static const struct got_error *
1475 import_file(struct got_tree_entry **new_te, struct dirent *de,
1476 const char *path, struct got_repository *repo)
1478 const struct got_error *err;
1479 struct got_object_id *blob_id = NULL;
1483 if (asprintf(&filepath, "%s%s%s", path,
1484 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1485 return got_error_from_errno("asprintf");
1487 if (lstat(filepath, &sb) != 0) {
1488 err = got_error_from_errno2("lstat", path);
1492 err = got_object_blob_create(&blob_id, filepath, repo);
1496 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1505 static const struct got_error *
1506 insert_tree_entry(struct got_tree_entry *new_te,
1507 struct got_pathlist_head *paths)
1509 const struct got_error *err = NULL;
1510 struct got_pathlist_entry *new_pe;
1512 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1516 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1520 static const struct got_error *write_tree(struct got_object_id **,
1521 const char *, struct got_pathlist_head *, struct got_repository *,
1522 got_repo_import_cb progress_cb, void *progress_arg);
1524 static const struct got_error *
1525 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1526 const char *path, struct got_pathlist_head *ignores,
1527 struct got_repository *repo,
1528 got_repo_import_cb progress_cb, void *progress_arg)
1530 const struct got_error *err;
1531 struct got_object_id *id = NULL;
1534 if (asprintf(&subdirpath, "%s%s%s", path,
1535 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1536 return got_error_from_errno("asprintf");
1538 (*new_te) = calloc(1, sizeof(**new_te));
1539 if (*new_te == NULL)
1540 return got_error_from_errno("calloc");
1541 (*new_te)->mode = S_IFDIR;
1542 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1543 sizeof((*new_te)->name)) {
1544 err = got_error(GOT_ERR_NO_SPACE);
1547 err = write_tree(&id, subdirpath, ignores, repo,
1548 progress_cb, progress_arg);
1551 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1563 static const struct got_error *
1564 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1565 struct got_pathlist_head *ignores, struct got_repository *repo,
1566 got_repo_import_cb progress_cb, void *progress_arg)
1568 const struct got_error *err = NULL;
1572 struct got_tree_entry *new_te = NULL;
1573 struct got_pathlist_head paths;
1574 struct got_pathlist_entry *pe;
1576 *new_tree_id = NULL;
1580 dir = opendir(path_dir);
1582 err = got_error_from_errno2("opendir", path_dir);
1587 while ((de = readdir(dir)) != NULL) {
1591 if (strcmp(de->d_name, ".") == 0 ||
1592 strcmp(de->d_name, "..") == 0)
1595 TAILQ_FOREACH(pe, ignores, entry) {
1596 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1604 err = got_path_dirent_type(&type, path_dir, de);
1608 if (type == DT_DIR) {
1609 err = import_subdir(&new_te, de, path_dir,
1610 ignores, repo, progress_cb, progress_arg);
1612 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1617 } else if (type == DT_REG || type == DT_LNK) {
1618 err = import_file(&new_te, de, path_dir, repo);
1624 err = insert_tree_entry(new_te, &paths);
1630 if (TAILQ_EMPTY(&paths)) {
1631 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
1632 "cannot create tree without any entries");
1636 TAILQ_FOREACH(pe, &paths, entry) {
1637 struct got_tree_entry *te = pe->data;
1639 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
1641 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1642 err = got_error_from_errno("asprintf");
1645 err = (*progress_cb)(progress_arg, path);
1651 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1655 got_pathlist_free(&paths);
1659 const struct got_error *
1660 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1661 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1662 struct got_repository *repo, got_repo_import_cb progress_cb,
1665 const struct got_error *err;
1666 struct got_object_id *new_tree_id;
1668 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1669 progress_cb, progress_arg);
1673 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1674 author, time(NULL), author, time(NULL), logmsg, repo);