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>
43 #include "got_error.h"
44 #include "got_reference.h"
45 #include "got_repository.h"
47 #include "got_cancel.h"
48 #include "got_worktree.h"
49 #include "got_object.h"
51 #include "got_lib_delta.h"
52 #include "got_lib_inflate.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_pack.h"
57 #include "got_lib_privsep.h"
58 #include "got_lib_worktree.h"
59 #include "got_lib_sha1.h"
60 #include "got_lib_object_cache.h"
61 #include "got_lib_repository.h"
62 #include "got_lib_gotconfig.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_path_gotconfig(struct got_repository *repo)
167 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
170 const struct got_gotconfig *
171 got_repo_get_gotconfig(struct got_repository *repo)
173 return repo->gotconfig;
177 got_repo_get_gitconfig_remotes(int *nremotes,
178 const struct got_remote_repo **remotes, struct got_repository *repo)
180 *nremotes = repo->ngitconfig_remotes;
181 *remotes = repo->gitconfig_remotes;
185 is_git_repo(struct got_repository *repo)
187 const char *path_git = got_repo_get_path_git_dir(repo);
188 char *path_objects = got_repo_get_path_objects(repo);
189 char *path_refs = got_repo_get_path_refs(repo);
190 char *path_head = get_path_head(repo);
193 struct got_reference *head_ref;
195 if (lstat(path_git, &sb) == -1)
197 if (!S_ISDIR(sb.st_mode))
200 if (lstat(path_objects, &sb) == -1)
202 if (!S_ISDIR(sb.st_mode))
205 if (lstat(path_refs, &sb) == -1)
207 if (!S_ISDIR(sb.st_mode))
210 if (lstat(path_head, &sb) == -1)
212 if (!S_ISREG(sb.st_mode))
215 /* Check if the HEAD reference can be opened. */
216 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
218 got_ref_close(head_ref);
229 const struct got_error *
230 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
231 struct got_object *obj)
233 #ifndef GOT_NO_OBJ_CACHE
234 const struct got_error *err = NULL;
235 err = got_object_cache_add(&repo->objcache, id, obj);
237 if (err->code == GOT_ERR_OBJ_EXISTS ||
238 err->code == GOT_ERR_OBJ_TOO_LARGE)
248 got_repo_get_cached_object(struct got_repository *repo,
249 struct got_object_id *id)
251 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
254 const struct got_error *
255 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
256 struct got_tree_object *tree)
258 #ifndef GOT_NO_OBJ_CACHE
259 const struct got_error *err = NULL;
260 err = got_object_cache_add(&repo->treecache, id, tree);
262 if (err->code == GOT_ERR_OBJ_EXISTS ||
263 err->code == GOT_ERR_OBJ_TOO_LARGE)
272 struct got_tree_object *
273 got_repo_get_cached_tree(struct got_repository *repo,
274 struct got_object_id *id)
276 return (struct got_tree_object *)got_object_cache_get(
277 &repo->treecache, id);
280 const struct got_error *
281 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
282 struct got_commit_object *commit)
284 #ifndef GOT_NO_OBJ_CACHE
285 const struct got_error *err = NULL;
286 err = got_object_cache_add(&repo->commitcache, id, commit);
288 if (err->code == GOT_ERR_OBJ_EXISTS ||
289 err->code == GOT_ERR_OBJ_TOO_LARGE)
298 struct got_commit_object *
299 got_repo_get_cached_commit(struct got_repository *repo,
300 struct got_object_id *id)
302 return (struct got_commit_object *)got_object_cache_get(
303 &repo->commitcache, id);
306 const struct got_error *
307 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
308 struct got_tag_object *tag)
310 #ifndef GOT_NO_OBJ_CACHE
311 const struct got_error *err = NULL;
312 err = got_object_cache_add(&repo->tagcache, id, tag);
314 if (err->code == GOT_ERR_OBJ_EXISTS ||
315 err->code == GOT_ERR_OBJ_TOO_LARGE)
324 struct got_tag_object *
325 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
327 return (struct got_tag_object *)got_object_cache_get(
328 &repo->tagcache, id);
331 const struct got_error *
332 open_repo(struct got_repository *repo, const char *path)
334 const struct got_error *err = NULL;
336 /* bare git repository? */
337 repo->path_git_dir = strdup(path);
338 if (repo->path_git_dir == NULL)
339 return got_error_from_errno("strdup");
340 if (is_git_repo(repo)) {
341 repo->path = strdup(repo->path_git_dir);
342 if (repo->path == NULL) {
343 err = got_error_from_errno("strdup");
349 /* git repository with working tree? */
350 free(repo->path_git_dir);
351 repo->path_git_dir = NULL;
352 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
353 err = got_error_from_errno("asprintf");
356 if (is_git_repo(repo)) {
357 repo->path = strdup(path);
358 if (repo->path == NULL) {
359 err = got_error_from_errno("strdup");
365 err = got_error(GOT_ERR_NOT_GIT_REPO);
370 free(repo->path_git_dir);
371 repo->path_git_dir = NULL;
376 static const struct got_error *
377 parse_gitconfig_file(int *gitconfig_repository_format_version,
378 char **gitconfig_author_name, char **gitconfig_author_email,
379 struct got_remote_repo **remotes, int *nremotes,
380 char **gitconfig_owner, char ***extensions, int *nextensions,
381 const char *gitconfig_path)
383 const struct got_error *err = NULL, *child_err = NULL;
385 int imsg_fds[2] = { -1, -1 };
387 struct imsgbuf *ibuf;
389 *gitconfig_repository_format_version = 0;
394 *gitconfig_author_name = NULL;
395 *gitconfig_author_email = NULL;
401 *gitconfig_owner = NULL;
403 fd = open(gitconfig_path, O_RDONLY);
407 return got_error_from_errno2("open", gitconfig_path);
410 ibuf = calloc(1, sizeof(*ibuf));
412 err = got_error_from_errno("calloc");
416 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
417 err = got_error_from_errno("socketpair");
423 err = got_error_from_errno("fork");
425 } else if (pid == 0) {
426 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
431 if (close(imsg_fds[1]) == -1) {
432 err = got_error_from_errno("close");
436 imsg_init(ibuf, imsg_fds[0]);
438 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
443 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
447 err = got_privsep_recv_gitconfig_int(
448 gitconfig_repository_format_version, ibuf);
452 if (extensions && nextensions) {
453 err = got_privsep_send_gitconfig_repository_extensions_req(
457 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
460 if (*nextensions > 0) {
462 *extensions = calloc(*nextensions, sizeof(char *));
463 if (*extensions == NULL) {
464 err = got_error_from_errno("calloc");
467 for (i = 0; i < *nextensions; i++) {
469 err = got_privsep_recv_gitconfig_str(&ext,
473 (*extensions)[i] = ext;
478 err = got_privsep_send_gitconfig_author_name_req(ibuf);
482 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
486 err = got_privsep_send_gitconfig_author_email_req(ibuf);
490 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
494 if (remotes && nremotes) {
495 err = got_privsep_send_gitconfig_remotes_req(ibuf);
499 err = got_privsep_recv_gitconfig_remotes(remotes,
505 if (gitconfig_owner) {
506 err = got_privsep_send_gitconfig_owner_req(ibuf);
509 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
515 err = got_privsep_send_stop(imsg_fds[0]);
516 child_err = got_privsep_wait_for_child(pid);
517 if (child_err && err == NULL)
520 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
521 err = got_error_from_errno("close");
522 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
523 err = got_error_from_errno("close");
524 if (fd != -1 && close(fd) == -1 && err == NULL)
525 err = got_error_from_errno2("close", gitconfig_path);
530 static const struct got_error *
531 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
533 const struct got_error *err = NULL;
534 char *repo_gitconfig_path = NULL;
536 if (global_gitconfig_path) {
537 /* Read settings from ~/.gitconfig. */
538 int dummy_repo_version;
539 err = parse_gitconfig_file(&dummy_repo_version,
540 &repo->global_gitconfig_author_name,
541 &repo->global_gitconfig_author_email,
542 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
547 /* Read repository's .git/config file. */
548 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
549 if (repo_gitconfig_path == NULL)
550 return got_error_from_errno("got_repo_get_path_gitconfig");
552 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
553 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
554 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
555 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
556 repo_gitconfig_path);
560 free(repo_gitconfig_path);
564 static const struct got_error *
565 read_gotconfig(struct got_repository *repo)
567 const struct got_error *err = NULL;
568 char *gotconfig_path;
570 gotconfig_path = got_repo_get_path_gotconfig(repo);
571 if (gotconfig_path == NULL)
572 return got_error_from_errno("got_repo_get_path_gotconfig");
574 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
575 free(gotconfig_path);
579 /* Supported repository format extensions. */
580 static const char *repo_extensions[] = {
581 "noop", /* Got supports repository format version 1. */
582 "preciousObjects", /* Got has no garbage collection yet. */
583 "worktreeConfig", /* Got does not care about Git work trees. */
586 const struct got_error *
587 got_repo_open(struct got_repository **repop, const char *path,
588 const char *global_gitconfig_path)
590 struct got_repository *repo = NULL;
591 const struct got_error *err = NULL;
592 char *abspath, *repo_path = NULL;
597 if (got_path_is_absolute(path))
598 abspath = strdup(path);
600 abspath = got_path_get_absolute(path);
602 return got_error_path(path, GOT_ERR_BAD_PATH);
604 repo = calloc(1, sizeof(*repo));
606 err = got_error_from_errno("calloc");
610 for (i = 0; i < nitems(repo->privsep_children); i++) {
611 memset(&repo->privsep_children[i], 0,
612 sizeof(repo->privsep_children[0]));
613 repo->privsep_children[i].imsg_fd = -1;
616 err = got_object_cache_init(&repo->objcache,
617 GOT_OBJECT_CACHE_TYPE_OBJ);
620 err = got_object_cache_init(&repo->treecache,
621 GOT_OBJECT_CACHE_TYPE_TREE);
624 err = got_object_cache_init(&repo->commitcache,
625 GOT_OBJECT_CACHE_TYPE_COMMIT);
628 err = got_object_cache_init(&repo->tagcache,
629 GOT_OBJECT_CACHE_TYPE_TAG);
633 repo_path = realpath(abspath, NULL);
634 if (repo_path == NULL) {
635 err = got_error_from_errno2("realpath", abspath);
642 err = open_repo(repo, repo_path);
645 if (err->code != GOT_ERR_NOT_GIT_REPO)
647 if (repo_path[0] == '/' && repo_path[1] == '\0') {
648 err = got_error(GOT_ERR_NOT_GIT_REPO);
651 err = got_path_dirname(&parent_path, repo_path);
655 repo_path = parent_path;
658 err = read_gotconfig(repo);
662 err = read_gitconfig(repo, global_gitconfig_path);
665 if (repo->gitconfig_repository_format_version != 0)
666 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
667 for (i = 0; i < repo->nextensions; i++) {
668 char *ext = repo->extensions[i];
669 int j, supported = 0;
670 for (j = 0; j < nitems(repo_extensions); j++) {
671 if (strcmp(ext, repo_extensions[j]) == 0) {
677 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
683 got_repo_close(repo);
691 const struct got_error *
692 got_repo_close(struct got_repository *repo)
694 const struct got_error *err = NULL, *child_err;
697 for (i = 0; i < nitems(repo->packidx_cache); i++) {
698 if (repo->packidx_cache[i] == NULL)
700 got_packidx_close(repo->packidx_cache[i]);
703 for (i = 0; i < nitems(repo->packs); i++) {
704 if (repo->packs[i].path_packfile == NULL)
706 got_pack_close(&repo->packs[i]);
710 free(repo->path_git_dir);
712 got_object_cache_close(&repo->objcache);
713 got_object_cache_close(&repo->treecache);
714 got_object_cache_close(&repo->commitcache);
715 got_object_cache_close(&repo->tagcache);
717 for (i = 0; i < nitems(repo->privsep_children); i++) {
718 if (repo->privsep_children[i].imsg_fd == -1)
720 imsg_clear(repo->privsep_children[i].ibuf);
721 free(repo->privsep_children[i].ibuf);
722 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
723 child_err = got_privsep_wait_for_child(
724 repo->privsep_children[i].pid);
725 if (child_err && err == NULL)
727 if (close(repo->privsep_children[i].imsg_fd) != 0 &&
729 err = got_error_from_errno("close");
733 got_gotconfig_free(repo->gotconfig);
734 free(repo->gitconfig_author_name);
735 free(repo->gitconfig_author_email);
736 for (i = 0; i < repo->ngitconfig_remotes; i++)
737 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
738 free(repo->gitconfig_remotes);
739 for (i = 0; i < repo->nextensions; i++)
740 free(repo->extensions[i]);
741 free(repo->extensions);
748 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
756 for (i = 0; i < repo->nbranches; i++)
757 free(repo->branches[i]);
758 free(repo->branches);
759 repo->branches = NULL;
763 const struct got_error *
764 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
765 const char *input_path, int check_disk)
767 const struct got_error *err = NULL;
768 const char *repo_abspath = NULL;
770 char *canonpath, *path = NULL;
772 *in_repo_path = NULL;
774 canonpath = strdup(input_path);
775 if (canonpath == NULL) {
776 err = got_error_from_errno("strdup");
779 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
783 repo_abspath = got_repo_get_path(repo);
785 if (!check_disk || canonpath[0] == '\0') {
786 path = strdup(canonpath);
788 err = got_error_from_errno("strdup");
792 path = realpath(canonpath, NULL);
794 if (errno != ENOENT) {
795 err = got_error_from_errno2("realpath",
800 * Path is not on disk.
801 * Assume it is already relative to repository root.
803 path = strdup(canonpath);
805 err = got_error_from_errno("strdup");
810 repolen = strlen(repo_abspath);
814 if (strcmp(path, repo_abspath) == 0) {
818 err = got_error_from_errno("strdup");
821 } else if (len > repolen &&
822 got_path_is_child(path, repo_abspath, repolen)) {
823 /* Matched an on-disk path inside repository. */
824 if (got_repo_is_bare(repo)) {
826 * Matched an on-disk path inside repository
827 * database. Treat as repository-relative.
831 /* Strip common prefix with repository path. */
832 err = got_path_skip_common_ancestor(&child,
841 * Matched unrelated on-disk path.
842 * Treat it as repository-relative.
847 /* Make in-repository path absolute */
848 if (path[0] != '/') {
850 if (asprintf(&abspath, "/%s", path) == -1) {
851 err = got_error_from_errno("asprintf");
863 *in_repo_path = path;
867 static const struct got_error *
868 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
869 const char *path_packidx)
871 const struct got_error *err = NULL;
874 for (i = 0; i < nitems(repo->packidx_cache); i++) {
875 if (repo->packidx_cache[i] == NULL)
877 if (strcmp(repo->packidx_cache[i]->path_packidx,
878 path_packidx) == 0) {
879 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
882 if (i == nitems(repo->packidx_cache)) {
883 err = got_packidx_close(repo->packidx_cache[i - 1]);
889 * Insert the new pack index at the front so it will
890 * be searched first in the future.
892 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
893 sizeof(repo->packidx_cache) -
894 sizeof(repo->packidx_cache[0]));
895 repo->packidx_cache[0] = packidx;
901 is_packidx_filename(const char *name, size_t len)
903 if (len != GOT_PACKIDX_NAMELEN)
906 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
909 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
910 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
916 const struct got_error *
917 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
918 struct got_repository *repo, struct got_object_id *id)
920 const struct got_error *err;
927 /* Search pack index cache. */
928 for (i = 0; i < nitems(repo->packidx_cache); i++) {
929 if (repo->packidx_cache[i] == NULL)
931 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
933 *packidx = repo->packidx_cache[i];
935 * Move this cache entry to the front. Repeatedly
936 * searching a wrong pack index can be expensive.
939 struct got_packidx *p;
940 p = repo->packidx_cache[0];
941 repo->packidx_cache[0] = *packidx;
942 repo->packidx_cache[i] = p;
947 /* No luck. Search the filesystem. */
949 path_packdir = got_repo_get_path_objects_pack(repo);
950 if (path_packdir == NULL)
951 return got_error_from_errno("got_repo_get_path_objects_pack");
953 packdir = opendir(path_packdir);
954 if (packdir == NULL) {
956 err = got_error_no_obj(id);
958 err = got_error_from_errno2("opendir", path_packdir);
962 while ((dent = readdir(packdir)) != NULL) {
965 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
968 if (asprintf(&path_packidx, "%s/%s", path_packdir,
969 dent->d_name) == -1) {
970 err = got_error_from_errno("asprintf");
974 for (i = 0; i < nitems(repo->packidx_cache); i++) {
975 if (repo->packidx_cache[i] == NULL)
977 if (strcmp(repo->packidx_cache[i]->path_packidx,
978 path_packidx) == 0) {
985 continue; /* already searched */
988 err = got_packidx_open(packidx, path_packidx, 0);
994 err = cache_packidx(repo, *packidx, path_packidx);
999 *idx = got_packidx_get_object_idx(*packidx, id);
1001 err = NULL; /* found the object */
1006 err = got_error_no_obj(id);
1009 if (packdir && closedir(packdir) != 0 && err == NULL)
1010 err = got_error_from_errno("closedir");
1014 static const struct got_error *
1015 read_packfile_hdr(int fd, struct got_packidx *packidx)
1017 const struct got_error *err = NULL;
1018 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1019 struct got_packfile_hdr hdr;
1022 n = read(fd, &hdr, sizeof(hdr));
1024 return got_error_from_errno("read");
1025 if (n != sizeof(hdr))
1026 return got_error(GOT_ERR_BAD_PACKFILE);
1028 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1029 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1030 be32toh(hdr.nobjects) != totobj)
1031 err = got_error(GOT_ERR_BAD_PACKFILE);
1036 static const struct got_error *
1037 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
1039 const struct got_error *err = NULL;
1041 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
1043 return got_error_from_errno2("open", path_packfile);
1046 err = read_packfile_hdr(*fd, packidx);
1056 const struct got_error *
1057 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1058 const char *path_packfile, struct got_packidx *packidx)
1060 const struct got_error *err = NULL;
1061 struct got_pack *pack = NULL;
1068 for (i = 0; i < nitems(repo->packs); i++) {
1069 pack = &repo->packs[i];
1070 if (pack->path_packfile == NULL)
1072 if (strcmp(pack->path_packfile, path_packfile) == 0)
1073 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1076 if (i == nitems(repo->packs) - 1) {
1077 err = got_pack_close(&repo->packs[i - 1]);
1080 memmove(&repo->packs[1], &repo->packs[0],
1081 sizeof(repo->packs) - sizeof(repo->packs[0]));
1085 pack = &repo->packs[i];
1087 pack->path_packfile = strdup(path_packfile);
1088 if (pack->path_packfile == NULL) {
1089 err = got_error_from_errno("strdup");
1093 err = open_packfile(&pack->fd, path_packfile, packidx);
1097 if (fstat(pack->fd, &sb) != 0) {
1098 err = got_error_from_errno("fstat");
1101 pack->filesize = sb.st_size;
1103 pack->privsep_child = NULL;
1105 #ifndef GOT_PACK_NO_MMAP
1106 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1108 if (pack->map == MAP_FAILED) {
1109 if (errno != ENOMEM) {
1110 err = got_error_from_errno("mmap");
1113 pack->map = NULL; /* fall back to read(2) */
1119 free(pack->path_packfile);
1120 memset(pack, 0, sizeof(*pack));
1128 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1130 struct got_pack *pack = NULL;
1133 for (i = 0; i < nitems(repo->packs); i++) {
1134 pack = &repo->packs[i];
1135 if (pack->path_packfile == NULL)
1137 if (strcmp(pack->path_packfile, path_packfile) == 0)
1144 const struct got_error *
1145 got_repo_init(const char *repo_path)
1147 const struct got_error *err = NULL;
1148 const char *dirnames[] = {
1150 GOT_OBJECTS_PACK_DIR,
1153 const char *description_str = "Unnamed repository; "
1154 "edit this file 'description' to name the repository.";
1155 const char *headref_str = "ref: refs/heads/main";
1156 const char *gitconfig_str = "[core]\n"
1157 "\trepositoryformatversion = 0\n"
1158 "\tfilemode = true\n"
1163 if (!got_path_dir_is_empty(repo_path))
1164 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1166 for (i = 0; i < nitems(dirnames); i++) {
1167 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1168 return got_error_from_errno("asprintf");
1170 err = got_path_mkdir(path);
1176 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1177 return got_error_from_errno("asprintf");
1178 err = got_path_create_file(path, description_str);
1183 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1184 return got_error_from_errno("asprintf");
1185 err = got_path_create_file(path, headref_str);
1190 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1191 return got_error_from_errno("asprintf");
1192 err = got_path_create_file(path, gitconfig_str);
1200 static const struct got_error *
1201 match_packed_object(struct got_object_id **unique_id,
1202 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1204 const struct got_error *err = NULL;
1207 struct dirent *dent;
1209 struct got_object_id_queue matched_ids;
1211 SIMPLEQ_INIT(&matched_ids);
1213 path_packdir = got_repo_get_path_objects_pack(repo);
1214 if (path_packdir == NULL)
1215 return got_error_from_errno("got_repo_get_path_objects_pack");
1217 packdir = opendir(path_packdir);
1218 if (packdir == NULL) {
1219 if (errno != ENOENT)
1220 err = got_error_from_errno2("opendir", path_packdir);
1224 while ((dent = readdir(packdir)) != NULL) {
1225 struct got_packidx *packidx;
1226 struct got_object_qid *qid;
1229 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1232 if (asprintf(&path_packidx, "%s/%s", path_packdir,
1233 dent->d_name) == -1) {
1234 err = got_error_from_errno("asprintf");
1238 err = got_packidx_open(&packidx, path_packidx, 0);
1243 err = got_packidx_match_id_str_prefix(&matched_ids,
1244 packidx, id_str_prefix);
1246 got_packidx_close(packidx);
1249 err = got_packidx_close(packidx);
1253 SIMPLEQ_FOREACH(qid, &matched_ids, entry) {
1254 if (obj_type != GOT_OBJ_TYPE_ANY) {
1256 err = got_object_get_type(&matched_type, repo,
1260 if (matched_type != obj_type)
1263 if (*unique_id == NULL) {
1264 *unique_id = got_object_id_dup(qid->id);
1265 if (*unique_id == NULL) {
1266 err = got_error_from_errno("malloc");
1270 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1271 continue; /* packed multiple times */
1272 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1278 got_object_id_queue_free(&matched_ids);
1280 if (packdir && closedir(packdir) != 0 && err == NULL)
1281 err = got_error_from_errno("closedir");
1289 static const struct got_error *
1290 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1291 const char *object_dir, const char *id_str_prefix, int obj_type,
1292 struct got_repository *repo)
1294 const struct got_error *err = NULL;
1297 struct dirent *dent;
1298 struct got_object_id id;
1300 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1301 err = got_error_from_errno("asprintf");
1305 dir = opendir(path);
1307 if (errno == ENOENT) {
1311 err = got_error_from_errno2("opendir", path);
1314 while ((dent = readdir(dir)) != NULL) {
1318 if (strcmp(dent->d_name, ".") == 0 ||
1319 strcmp(dent->d_name, "..") == 0)
1322 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1323 err = got_error_from_errno("asprintf");
1327 if (!got_parse_sha1_digest(id.sha1, id_str))
1331 * Directory entries do not necessarily appear in
1332 * sorted order, so we must iterate over all of them.
1334 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1340 if (*unique_id == NULL) {
1341 if (obj_type != GOT_OBJ_TYPE_ANY) {
1343 err = got_object_get_type(&matched_type, repo,
1347 if (matched_type != obj_type)
1350 *unique_id = got_object_id_dup(&id);
1351 if (*unique_id == NULL) {
1352 err = got_error_from_errno("got_object_id_dup");
1357 if (got_object_id_cmp(*unique_id, &id) == 0)
1358 continue; /* both packed and loose */
1359 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1365 if (dir && closedir(dir) != 0 && err == NULL)
1366 err = got_error_from_errno("closedir");
1375 const struct got_error *
1376 got_repo_match_object_id_prefix(struct got_object_id **id,
1377 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1379 const struct got_error *err = NULL;
1380 char *path_objects = got_repo_get_path_objects(repo);
1381 char *object_dir = NULL;
1387 for (i = 0; i < strlen(id_str_prefix); i++) {
1388 if (isxdigit((unsigned char)id_str_prefix[i]))
1390 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1393 len = strlen(id_str_prefix);
1395 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1398 object_dir = strndup(id_str_prefix, 2);
1399 if (object_dir == NULL) {
1400 err = got_error_from_errno("strdup");
1403 err = match_loose_object(id, path_objects, object_dir,
1404 id_str_prefix, obj_type, repo);
1405 } else if (len == 1) {
1407 for (i = 0; i < 0xf; i++) {
1408 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1410 err = got_error_from_errno("asprintf");
1413 err = match_packed_object(id, repo, object_dir,
1417 err = match_loose_object(id, path_objects, object_dir,
1418 id_str_prefix, obj_type, repo);
1423 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1431 } else if (*id == NULL)
1432 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1437 const struct got_error *
1438 got_repo_match_object_id(struct got_object_id **id, char **label,
1439 const char *id_str, int obj_type, int resolve_tags,
1440 struct got_repository *repo)
1442 const struct got_error *err;
1443 struct got_tag_object *tag;
1444 struct got_reference *ref = NULL;
1451 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
1454 *id = got_object_id_dup(
1455 got_object_tag_get_object_id(tag));
1457 err = got_error_from_errno("got_object_id_dup");
1458 else if (label && asprintf(label, "refs/tags/%s",
1459 got_object_tag_get_name(tag)) == -1) {
1460 err = got_error_from_errno("asprintf");
1464 got_object_tag_close(tag);
1466 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1467 err->code != GOT_ERR_NO_OBJ)
1471 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1473 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1475 err = got_ref_open(&ref, repo, id_str, 0);
1479 *label = strdup(got_ref_get_name(ref));
1480 if (*label == NULL) {
1481 err = got_error_from_errno("strdup");
1485 err = got_ref_resolve(id, repo, ref);
1487 err = got_object_id_str(label, *id);
1488 if (*label == NULL) {
1489 err = got_error_from_errno("strdup");
1499 const struct got_error *
1500 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1501 int obj_type, struct got_repository *repo)
1503 const struct got_error *err;
1504 struct got_reflist_head refs;
1505 struct got_reflist_entry *re;
1506 struct got_object_id *tag_id;
1508 SIMPLEQ_INIT(&refs);
1511 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_by_name, NULL);
1515 SIMPLEQ_FOREACH(re, &refs, entry) {
1516 const char *refname;
1517 refname = got_ref_get_name(re->ref);
1518 if (got_ref_is_symbolic(re->ref))
1520 refname += strlen("refs/tags/");
1521 if (strcmp(refname, name) != 0)
1523 err = got_ref_resolve(&tag_id, repo, re->ref);
1526 err = got_object_open_as_tag(tag, repo, tag_id);
1530 if (obj_type == GOT_OBJ_TYPE_ANY ||
1531 got_object_tag_get_object_type(*tag) == obj_type)
1533 got_object_tag_close(*tag);
1537 got_ref_list_free(&refs);
1538 if (err == NULL && *tag == NULL)
1539 err = got_error_path(name, GOT_ERR_NO_OBJ);
1543 static const struct got_error *
1544 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1545 const char *name, mode_t mode, struct got_object_id *blob_id)
1547 const struct got_error *err = NULL;
1551 *new_te = calloc(1, sizeof(**new_te));
1552 if (*new_te == NULL)
1553 return got_error_from_errno("calloc");
1555 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1556 sizeof((*new_te)->name)) {
1557 err = got_error(GOT_ERR_NO_SPACE);
1561 if (S_ISLNK(mode)) {
1562 (*new_te)->mode = S_IFLNK;
1564 (*new_te)->mode = S_IFREG;
1565 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1567 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1569 if (err && *new_te) {
1576 static const struct got_error *
1577 import_file(struct got_tree_entry **new_te, struct dirent *de,
1578 const char *path, struct got_repository *repo)
1580 const struct got_error *err;
1581 struct got_object_id *blob_id = NULL;
1585 if (asprintf(&filepath, "%s%s%s", path,
1586 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1587 return got_error_from_errno("asprintf");
1589 if (lstat(filepath, &sb) != 0) {
1590 err = got_error_from_errno2("lstat", path);
1594 err = got_object_blob_create(&blob_id, filepath, repo);
1598 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1607 static const struct got_error *
1608 insert_tree_entry(struct got_tree_entry *new_te,
1609 struct got_pathlist_head *paths)
1611 const struct got_error *err = NULL;
1612 struct got_pathlist_entry *new_pe;
1614 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1618 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1622 static const struct got_error *write_tree(struct got_object_id **,
1623 const char *, struct got_pathlist_head *, struct got_repository *,
1624 got_repo_import_cb progress_cb, void *progress_arg);
1626 static const struct got_error *
1627 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1628 const char *path, struct got_pathlist_head *ignores,
1629 struct got_repository *repo,
1630 got_repo_import_cb progress_cb, void *progress_arg)
1632 const struct got_error *err;
1633 struct got_object_id *id = NULL;
1636 if (asprintf(&subdirpath, "%s%s%s", path,
1637 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1638 return got_error_from_errno("asprintf");
1640 (*new_te) = calloc(1, sizeof(**new_te));
1641 if (*new_te == NULL)
1642 return got_error_from_errno("calloc");
1643 (*new_te)->mode = S_IFDIR;
1644 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1645 sizeof((*new_te)->name)) {
1646 err = got_error(GOT_ERR_NO_SPACE);
1649 err = write_tree(&id, subdirpath, ignores, repo,
1650 progress_cb, progress_arg);
1653 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1665 static const struct got_error *
1666 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1667 struct got_pathlist_head *ignores, struct got_repository *repo,
1668 got_repo_import_cb progress_cb, void *progress_arg)
1670 const struct got_error *err = NULL;
1674 struct got_tree_entry *new_te = NULL;
1675 struct got_pathlist_head paths;
1676 struct got_pathlist_entry *pe;
1678 *new_tree_id = NULL;
1682 dir = opendir(path_dir);
1684 err = got_error_from_errno2("opendir", path_dir);
1689 while ((de = readdir(dir)) != NULL) {
1693 if (strcmp(de->d_name, ".") == 0 ||
1694 strcmp(de->d_name, "..") == 0)
1697 TAILQ_FOREACH(pe, ignores, entry) {
1698 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1706 err = got_path_dirent_type(&type, path_dir, de);
1710 if (type == DT_DIR) {
1711 err = import_subdir(&new_te, de, path_dir,
1712 ignores, repo, progress_cb, progress_arg);
1714 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1719 } else if (type == DT_REG || type == DT_LNK) {
1720 err = import_file(&new_te, de, path_dir, repo);
1726 err = insert_tree_entry(new_te, &paths);
1732 if (TAILQ_EMPTY(&paths)) {
1733 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
1734 "cannot create tree without any entries");
1738 TAILQ_FOREACH(pe, &paths, entry) {
1739 struct got_tree_entry *te = pe->data;
1741 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
1743 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1744 err = got_error_from_errno("asprintf");
1747 err = (*progress_cb)(progress_arg, path);
1753 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1757 got_pathlist_free(&paths);
1761 const struct got_error *
1762 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1763 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1764 struct got_repository *repo, got_repo_import_cb progress_cb,
1767 const struct got_error *err;
1768 struct got_object_id *new_tree_id;
1770 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1771 progress_cb, progress_arg);
1775 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1776 author, time(NULL), author, time(NULL), logmsg, repo);