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>
19 #include <sys/socket.h>
22 #include <sys/resource.h>
40 #include "got_compat.h"
42 #include "got_error.h"
43 #include "got_reference.h"
44 #include "got_repository.h"
46 #include "got_cancel.h"
47 #include "got_object.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_inflate.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_object_create.h"
54 #include "got_lib_pack.h"
55 #include "got_lib_privsep.h"
56 #include "got_lib_sha1.h"
57 #include "got_lib_object_cache.h"
58 #include "got_lib_repository.h"
59 #include "got_lib_gotconfig.h"
62 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
66 got_repo_get_path(struct got_repository *repo)
72 got_repo_get_path_git_dir(struct got_repository *repo)
74 return repo->path_git_dir;
78 got_repo_get_fd(struct got_repository *repo)
80 return repo->gitdir_fd;
84 got_repo_get_gitconfig_author_name(struct got_repository *repo)
86 return repo->gitconfig_author_name;
90 got_repo_get_gitconfig_author_email(struct got_repository *repo)
92 return repo->gitconfig_author_email;
96 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
98 return repo->global_gitconfig_author_name;
102 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
104 return repo->global_gitconfig_author_email;
108 got_repo_get_gitconfig_owner(struct got_repository *repo)
110 return repo->gitconfig_owner;
114 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
115 struct got_repository *repo)
117 *extensions = repo->extensions;
118 *nextensions = repo->nextensions;
122 got_repo_is_bare(struct got_repository *repo)
124 return (strcmp(repo->path, repo->path_git_dir) == 0);
128 get_path_git_child(struct got_repository *repo, const char *basename)
132 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
140 got_repo_get_path_objects(struct got_repository *repo)
142 return get_path_git_child(repo, GOT_OBJECTS_DIR);
146 got_repo_get_path_objects_pack(struct got_repository *repo)
148 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
152 got_repo_get_path_refs(struct got_repository *repo)
154 return get_path_git_child(repo, GOT_REFS_DIR);
158 got_repo_get_path_packed_refs(struct got_repository *repo)
160 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
164 get_path_head(struct got_repository *repo)
166 return get_path_git_child(repo, GOT_HEAD_FILE);
170 got_repo_get_path_gitconfig(struct got_repository *repo)
172 return get_path_git_child(repo, GOT_GITCONFIG);
176 got_repo_get_path_gotconfig(struct got_repository *repo)
178 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
181 const struct got_gotconfig *
182 got_repo_get_gotconfig(struct got_repository *repo)
184 return repo->gotconfig;
188 got_repo_get_gitconfig_remotes(int *nremotes,
189 const struct got_remote_repo **remotes, struct got_repository *repo)
191 *nremotes = repo->ngitconfig_remotes;
192 *remotes = repo->gitconfig_remotes;
196 is_git_repo(struct got_repository *repo)
198 const char *path_git = got_repo_get_path_git_dir(repo);
199 char *path_objects = got_repo_get_path_objects(repo);
200 char *path_refs = got_repo_get_path_refs(repo);
201 char *path_head = get_path_head(repo);
204 struct got_reference *head_ref;
206 if (lstat(path_git, &sb) == -1)
208 if (!S_ISDIR(sb.st_mode))
211 if (lstat(path_objects, &sb) == -1)
213 if (!S_ISDIR(sb.st_mode))
216 if (lstat(path_refs, &sb) == -1)
218 if (!S_ISDIR(sb.st_mode))
221 if (lstat(path_head, &sb) == -1)
223 if (!S_ISREG(sb.st_mode))
226 /* Check if the HEAD reference can be opened. */
227 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
229 got_ref_close(head_ref);
240 const struct got_error *
241 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
242 struct got_object *obj)
244 #ifndef GOT_NO_OBJ_CACHE
245 const struct got_error *err = NULL;
246 err = got_object_cache_add(&repo->objcache, id, obj);
248 if (err->code == GOT_ERR_OBJ_EXISTS ||
249 err->code == GOT_ERR_OBJ_TOO_LARGE)
259 got_repo_get_cached_object(struct got_repository *repo,
260 struct got_object_id *id)
262 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
265 const struct got_error *
266 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
267 struct got_tree_object *tree)
269 #ifndef GOT_NO_OBJ_CACHE
270 const struct got_error *err = NULL;
271 err = got_object_cache_add(&repo->treecache, id, tree);
273 if (err->code == GOT_ERR_OBJ_EXISTS ||
274 err->code == GOT_ERR_OBJ_TOO_LARGE)
283 struct got_tree_object *
284 got_repo_get_cached_tree(struct got_repository *repo,
285 struct got_object_id *id)
287 return (struct got_tree_object *)got_object_cache_get(
288 &repo->treecache, id);
291 const struct got_error *
292 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
293 struct got_commit_object *commit)
295 #ifndef GOT_NO_OBJ_CACHE
296 const struct got_error *err = NULL;
297 err = got_object_cache_add(&repo->commitcache, id, commit);
299 if (err->code == GOT_ERR_OBJ_EXISTS ||
300 err->code == GOT_ERR_OBJ_TOO_LARGE)
309 struct got_commit_object *
310 got_repo_get_cached_commit(struct got_repository *repo,
311 struct got_object_id *id)
313 return (struct got_commit_object *)got_object_cache_get(
314 &repo->commitcache, id);
317 const struct got_error *
318 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
319 struct got_tag_object *tag)
321 #ifndef GOT_NO_OBJ_CACHE
322 const struct got_error *err = NULL;
323 err = got_object_cache_add(&repo->tagcache, id, tag);
325 if (err->code == GOT_ERR_OBJ_EXISTS ||
326 err->code == GOT_ERR_OBJ_TOO_LARGE)
335 struct got_tag_object *
336 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
338 return (struct got_tag_object *)got_object_cache_get(
339 &repo->tagcache, id);
342 static const struct got_error *
343 open_repo(struct got_repository *repo, const char *path)
345 const struct got_error *err = NULL;
347 repo->gitdir_fd = -1;
349 /* bare git repository? */
350 repo->path_git_dir = strdup(path);
351 if (repo->path_git_dir == NULL)
352 return got_error_from_errno("strdup");
353 if (is_git_repo(repo)) {
354 repo->path = strdup(repo->path_git_dir);
355 if (repo->path == NULL) {
356 err = got_error_from_errno("strdup");
359 repo->gitdir_fd = open(repo->path_git_dir, O_DIRECTORY);
360 if (repo->gitdir_fd == -1) {
361 err = got_error_from_errno2("open",
368 /* git repository with working tree? */
369 free(repo->path_git_dir);
370 repo->path_git_dir = NULL;
371 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
372 err = got_error_from_errno("asprintf");
375 if (is_git_repo(repo)) {
376 repo->path = strdup(path);
377 if (repo->path == NULL) {
378 err = got_error_from_errno("strdup");
381 repo->gitdir_fd = open(repo->path_git_dir, O_DIRECTORY);
382 if (repo->gitdir_fd == -1) {
383 err = got_error_from_errno2("open",
390 err = got_error(GOT_ERR_NOT_GIT_REPO);
395 free(repo->path_git_dir);
396 repo->path_git_dir = NULL;
397 if (repo->gitdir_fd != -1)
398 close(repo->gitdir_fd);
399 repo->gitdir_fd = -1;
405 static const struct got_error *
406 parse_gitconfig_file(int *gitconfig_repository_format_version,
407 char **gitconfig_author_name, char **gitconfig_author_email,
408 struct got_remote_repo **remotes, int *nremotes,
409 char **gitconfig_owner, char ***extensions, int *nextensions,
410 const char *gitconfig_path)
412 const struct got_error *err = NULL, *child_err = NULL;
414 int imsg_fds[2] = { -1, -1 };
416 struct imsgbuf *ibuf;
418 *gitconfig_repository_format_version = 0;
423 *gitconfig_author_name = NULL;
424 *gitconfig_author_email = NULL;
430 *gitconfig_owner = NULL;
432 fd = open(gitconfig_path, O_RDONLY);
436 return got_error_from_errno2("open", gitconfig_path);
439 ibuf = calloc(1, sizeof(*ibuf));
441 err = got_error_from_errno("calloc");
445 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
446 err = got_error_from_errno("socketpair");
452 err = got_error_from_errno("fork");
454 } else if (pid == 0) {
455 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
460 if (close(imsg_fds[1]) == -1) {
461 err = got_error_from_errno("close");
465 imsg_init(ibuf, imsg_fds[0]);
467 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
472 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
476 err = got_privsep_recv_gitconfig_int(
477 gitconfig_repository_format_version, ibuf);
481 if (extensions && nextensions) {
482 err = got_privsep_send_gitconfig_repository_extensions_req(
486 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
489 if (*nextensions > 0) {
491 *extensions = calloc(*nextensions, sizeof(char *));
492 if (*extensions == NULL) {
493 err = got_error_from_errno("calloc");
496 for (i = 0; i < *nextensions; i++) {
498 err = got_privsep_recv_gitconfig_str(&ext,
502 (*extensions)[i] = ext;
507 err = got_privsep_send_gitconfig_author_name_req(ibuf);
511 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
515 err = got_privsep_send_gitconfig_author_email_req(ibuf);
519 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
523 if (remotes && nremotes) {
524 err = got_privsep_send_gitconfig_remotes_req(ibuf);
528 err = got_privsep_recv_gitconfig_remotes(remotes,
534 if (gitconfig_owner) {
535 err = got_privsep_send_gitconfig_owner_req(ibuf);
538 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
544 err = got_privsep_send_stop(imsg_fds[0]);
545 child_err = got_privsep_wait_for_child(pid);
546 if (child_err && err == NULL)
549 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
550 err = got_error_from_errno("close");
551 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
552 err = got_error_from_errno("close");
553 if (fd != -1 && close(fd) == -1 && err == NULL)
554 err = got_error_from_errno2("close", gitconfig_path);
559 static const struct got_error *
560 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
562 const struct got_error *err = NULL;
563 char *repo_gitconfig_path = NULL;
565 if (global_gitconfig_path) {
566 /* Read settings from ~/.gitconfig. */
567 int dummy_repo_version;
568 err = parse_gitconfig_file(&dummy_repo_version,
569 &repo->global_gitconfig_author_name,
570 &repo->global_gitconfig_author_email,
571 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
576 /* Read repository's .git/config file. */
577 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
578 if (repo_gitconfig_path == NULL)
579 return got_error_from_errno("got_repo_get_path_gitconfig");
581 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
582 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
583 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
584 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
585 repo_gitconfig_path);
589 free(repo_gitconfig_path);
593 static const struct got_error *
594 read_gotconfig(struct got_repository *repo)
596 const struct got_error *err = NULL;
597 char *gotconfig_path;
599 gotconfig_path = got_repo_get_path_gotconfig(repo);
600 if (gotconfig_path == NULL)
601 return got_error_from_errno("got_repo_get_path_gotconfig");
603 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
604 free(gotconfig_path);
608 /* Supported repository format extensions. */
609 static const char *repo_extensions[] = {
610 "noop", /* Got supports repository format version 1. */
611 "preciousObjects", /* Supported by gotadmin cleanup. */
612 "worktreeConfig", /* Got does not care about Git work trees. */
615 const struct got_error *
616 got_repo_open(struct got_repository **repop, const char *path,
617 const char *global_gitconfig_path)
619 struct got_repository *repo = NULL;
620 const struct got_error *err = NULL;
621 char *repo_path = NULL;
627 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
628 return got_error_from_errno("getrlimit");
630 repo = calloc(1, sizeof(*repo));
632 err = got_error_from_errno("calloc");
636 for (i = 0; i < nitems(repo->privsep_children); i++) {
637 memset(&repo->privsep_children[i], 0,
638 sizeof(repo->privsep_children[0]));
639 repo->privsep_children[i].imsg_fd = -1;
642 err = got_object_cache_init(&repo->objcache,
643 GOT_OBJECT_CACHE_TYPE_OBJ);
646 err = got_object_cache_init(&repo->treecache,
647 GOT_OBJECT_CACHE_TYPE_TREE);
650 err = got_object_cache_init(&repo->commitcache,
651 GOT_OBJECT_CACHE_TYPE_COMMIT);
654 err = got_object_cache_init(&repo->tagcache,
655 GOT_OBJECT_CACHE_TYPE_TAG);
659 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
660 if (repo->pack_cache_size > rl.rlim_cur / 8)
661 repo->pack_cache_size = rl.rlim_cur / 8;
663 repo_path = realpath(path, NULL);
664 if (repo_path == NULL) {
665 err = got_error_from_errno2("realpath", path);
672 err = open_repo(repo, repo_path);
675 if (err->code != GOT_ERR_NOT_GIT_REPO)
677 if (repo_path[0] == '/' && repo_path[1] == '\0') {
678 err = got_error(GOT_ERR_NOT_GIT_REPO);
681 err = got_path_dirname(&parent_path, repo_path);
685 repo_path = parent_path;
688 err = read_gotconfig(repo);
692 err = read_gitconfig(repo, global_gitconfig_path);
695 if (repo->gitconfig_repository_format_version != 0)
696 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
697 for (i = 0; i < repo->nextensions; i++) {
698 char *ext = repo->extensions[i];
699 int j, supported = 0;
700 for (j = 0; j < nitems(repo_extensions); j++) {
701 if (strcmp(ext, repo_extensions[j]) == 0) {
707 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
713 got_repo_close(repo);
720 const struct got_error *
721 got_repo_close(struct got_repository *repo)
723 const struct got_error *err = NULL, *child_err;
726 for (i = 0; i < repo->pack_cache_size; i++) {
727 if (repo->packidx_cache[i] == NULL)
729 got_packidx_close(repo->packidx_cache[i]);
732 for (i = 0; i < repo->pack_cache_size; i++) {
733 if (repo->packs[i].path_packfile == NULL)
735 got_pack_close(&repo->packs[i]);
739 free(repo->path_git_dir);
741 got_object_cache_close(&repo->objcache);
742 got_object_cache_close(&repo->treecache);
743 got_object_cache_close(&repo->commitcache);
744 got_object_cache_close(&repo->tagcache);
746 for (i = 0; i < nitems(repo->privsep_children); i++) {
747 if (repo->privsep_children[i].imsg_fd == -1)
749 imsg_clear(repo->privsep_children[i].ibuf);
750 free(repo->privsep_children[i].ibuf);
751 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
752 child_err = got_privsep_wait_for_child(
753 repo->privsep_children[i].pid);
754 if (child_err && err == NULL)
756 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
758 err = got_error_from_errno("close");
761 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
763 err = got_error_from_errno("close");
766 got_gotconfig_free(repo->gotconfig);
767 free(repo->gitconfig_author_name);
768 free(repo->gitconfig_author_email);
769 for (i = 0; i < repo->ngitconfig_remotes; i++)
770 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
771 free(repo->gitconfig_remotes);
772 for (i = 0; i < repo->nextensions; i++)
773 free(repo->extensions[i]);
774 free(repo->extensions);
781 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
787 free(repo->fetch_url);
788 repo->fetch_url = NULL;
789 free(repo->send_url);
790 repo->send_url = NULL;
791 for (i = 0; i < repo->nfetch_branches; i++)
792 free(repo->fetch_branches[i]);
793 free(repo->fetch_branches);
794 repo->fetch_branches = NULL;
795 repo->nfetch_branches = 0;
796 for (i = 0; i < repo->nsend_branches; i++)
797 free(repo->send_branches[i]);
798 free(repo->send_branches);
799 repo->send_branches = NULL;
800 repo->nsend_branches = 0;
803 const struct got_error *
804 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
805 const char *input_path)
807 const struct got_error *err = NULL;
808 const char *repo_abspath = NULL;
810 char *canonpath, *path = NULL;
812 *in_repo_path = NULL;
814 canonpath = strdup(input_path);
815 if (canonpath == NULL) {
816 err = got_error_from_errno("strdup");
819 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
823 repo_abspath = got_repo_get_path(repo);
825 if (canonpath[0] == '\0') {
826 path = strdup(canonpath);
828 err = got_error_from_errno("strdup");
832 path = realpath(canonpath, NULL);
834 if (errno != ENOENT) {
835 err = got_error_from_errno2("realpath",
840 * Path is not on disk.
841 * Assume it is already relative to repository root.
843 path = strdup(canonpath);
845 err = got_error_from_errno("strdup");
850 repolen = strlen(repo_abspath);
854 if (strcmp(path, repo_abspath) == 0) {
858 err = got_error_from_errno("strdup");
861 } else if (len > repolen &&
862 got_path_is_child(path, repo_abspath, repolen)) {
863 /* Matched an on-disk path inside repository. */
864 if (got_repo_is_bare(repo)) {
866 * Matched an on-disk path inside repository
867 * database. Treat input as repository-relative.
874 /* Strip common prefix with repository path. */
875 err = got_path_skip_common_ancestor(&child,
884 * Matched unrelated on-disk path.
885 * Treat input as repository-relative.
893 /* Make in-repository path absolute */
894 if (path[0] != '/') {
896 if (asprintf(&abspath, "/%s", path) == -1) {
897 err = got_error_from_errno("asprintf");
909 *in_repo_path = path;
913 static const struct got_error *
914 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
915 const char *path_packidx)
917 const struct got_error *err = NULL;
920 for (i = 0; i < repo->pack_cache_size; i++) {
921 if (repo->packidx_cache[i] == NULL)
923 if (strcmp(repo->packidx_cache[i]->path_packidx,
924 path_packidx) == 0) {
925 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
928 if (i == repo->pack_cache_size) {
929 err = got_packidx_close(repo->packidx_cache[i - 1]);
935 * Insert the new pack index at the front so it will
936 * be searched first in the future.
938 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
939 sizeof(repo->packidx_cache) -
940 sizeof(repo->packidx_cache[0]));
941 repo->packidx_cache[0] = packidx;
947 got_repo_is_packidx_filename(const char *name, size_t len)
949 if (len != GOT_PACKIDX_NAMELEN)
952 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
955 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
956 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
962 const struct got_error *
963 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
964 struct got_repository *repo, struct got_object_id *id)
966 const struct got_error *err;
973 /* Search pack index cache. */
974 for (i = 0; i < repo->pack_cache_size; i++) {
975 if (repo->packidx_cache[i] == NULL)
977 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
979 *packidx = repo->packidx_cache[i];
981 * Move this cache entry to the front. Repeatedly
982 * searching a wrong pack index can be expensive.
985 struct got_packidx *p;
986 p = repo->packidx_cache[0];
987 repo->packidx_cache[0] = *packidx;
988 repo->packidx_cache[i] = p;
993 /* No luck. Search the filesystem. */
995 packdir_fd = openat(got_repo_get_fd(repo),
996 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
997 if (packdir_fd == -1) {
999 err = got_error_no_obj(id);
1001 err = got_error_from_errno_fmt("openat: %s/%s",
1002 got_repo_get_path_git_dir(repo),
1003 GOT_OBJECTS_PACK_DIR);
1007 packdir = fdopendir(packdir_fd);
1008 if (packdir == NULL) {
1009 err = got_error_from_errno("fdopendir");
1013 while ((dent = readdir(packdir)) != NULL) {
1016 if (!got_repo_is_packidx_filename(dent->d_name,
1017 strlen(dent->d_name)))
1020 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1021 dent->d_name) == -1) {
1022 err = got_error_from_errno("asprintf");
1026 for (i = 0; i < repo->pack_cache_size; i++) {
1027 if (repo->packidx_cache[i] == NULL)
1029 if (strcmp(repo->packidx_cache[i]->path_packidx,
1030 path_packidx) == 0) {
1037 continue; /* already searched */
1040 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1047 err = cache_packidx(repo, *packidx, path_packidx);
1052 *idx = got_packidx_get_object_idx(*packidx, id);
1054 err = NULL; /* found the object */
1059 err = got_error_no_obj(id);
1061 if (packdir && closedir(packdir) != 0 && err == NULL)
1062 err = got_error_from_errno("closedir");
1066 static const struct got_error *
1067 read_packfile_hdr(int fd, struct got_packidx *packidx)
1069 const struct got_error *err = NULL;
1070 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1071 struct got_packfile_hdr hdr;
1074 n = read(fd, &hdr, sizeof(hdr));
1076 return got_error_from_errno("read");
1077 if (n != sizeof(hdr))
1078 return got_error(GOT_ERR_BAD_PACKFILE);
1080 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1081 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1082 be32toh(hdr.nobjects) != totobj)
1083 err = got_error(GOT_ERR_BAD_PACKFILE);
1088 static const struct got_error *
1089 open_packfile(int *fd, struct got_repository *repo,
1090 const char *relpath, struct got_packidx *packidx)
1092 const struct got_error *err = NULL;
1094 *fd = openat(got_repo_get_fd(repo), relpath, O_RDONLY | O_NOFOLLOW);
1096 return got_error_from_errno_fmt("openat: %s/%s",
1097 got_repo_get_path_git_dir(repo), relpath);
1100 err = read_packfile_hdr(*fd, packidx);
1110 const struct got_error *
1111 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1112 const char *path_packfile, struct got_packidx *packidx)
1114 const struct got_error *err = NULL;
1115 struct got_pack *pack = NULL;
1122 for (i = 0; i < repo->pack_cache_size; i++) {
1123 pack = &repo->packs[i];
1124 if (pack->path_packfile == NULL)
1126 if (strcmp(pack->path_packfile, path_packfile) == 0)
1127 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1130 if (i == repo->pack_cache_size) {
1131 err = got_pack_close(&repo->packs[i - 1]);
1134 memmove(&repo->packs[1], &repo->packs[0],
1135 sizeof(repo->packs) - sizeof(repo->packs[0]));
1139 pack = &repo->packs[i];
1141 pack->path_packfile = strdup(path_packfile);
1142 if (pack->path_packfile == NULL) {
1143 err = got_error_from_errno("strdup");
1147 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1151 if (fstat(pack->fd, &sb) != 0) {
1152 err = got_error_from_errno("fstat");
1155 pack->filesize = sb.st_size;
1157 pack->privsep_child = NULL;
1159 #ifndef GOT_PACK_NO_MMAP
1160 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1162 if (pack->map == MAP_FAILED) {
1163 if (errno != ENOMEM) {
1164 err = got_error_from_errno("mmap");
1167 pack->map = NULL; /* fall back to read(2) */
1173 free(pack->path_packfile);
1174 memset(pack, 0, sizeof(*pack));
1182 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1184 struct got_pack *pack = NULL;
1187 for (i = 0; i < repo->pack_cache_size; i++) {
1188 pack = &repo->packs[i];
1189 if (pack->path_packfile == NULL)
1191 if (strcmp(pack->path_packfile, path_packfile) == 0)
1198 const struct got_error *
1199 got_repo_init(const char *repo_path)
1201 const struct got_error *err = NULL;
1202 const char *dirnames[] = {
1204 GOT_OBJECTS_PACK_DIR,
1207 const char *description_str = "Unnamed repository; "
1208 "edit this file 'description' to name the repository.";
1209 const char *headref_str = "ref: refs/heads/main";
1210 const char *gitconfig_str = "[core]\n"
1211 "\trepositoryformatversion = 0\n"
1212 "\tfilemode = true\n"
1217 if (!got_path_dir_is_empty(repo_path))
1218 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1220 for (i = 0; i < nitems(dirnames); i++) {
1221 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1222 return got_error_from_errno("asprintf");
1224 err = got_path_mkdir(path);
1230 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1231 return got_error_from_errno("asprintf");
1232 err = got_path_create_file(path, description_str);
1237 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1238 return got_error_from_errno("asprintf");
1239 err = got_path_create_file(path, headref_str);
1244 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1245 return got_error_from_errno("asprintf");
1246 err = got_path_create_file(path, gitconfig_str);
1254 static const struct got_error *
1255 match_packed_object(struct got_object_id **unique_id,
1256 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1258 const struct got_error *err = NULL;
1259 DIR *packdir = NULL;
1260 struct dirent *dent;
1262 struct got_object_id_queue matched_ids;
1265 STAILQ_INIT(&matched_ids);
1267 packdir_fd = openat(got_repo_get_fd(repo),
1268 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1269 if (packdir_fd == -1) {
1270 if (errno != ENOENT)
1271 err = got_error_from_errno2("openat", GOT_OBJECTS_PACK_DIR);
1275 packdir = fdopendir(packdir_fd);
1276 if (packdir == NULL) {
1277 err = got_error_from_errno("fdopendir");
1281 while ((dent = readdir(packdir)) != NULL) {
1282 struct got_packidx *packidx;
1283 struct got_object_qid *qid;
1285 if (!got_repo_is_packidx_filename(dent->d_name,
1286 strlen(dent->d_name)))
1289 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1290 dent->d_name) == -1) {
1291 err = got_error_from_errno("strdup");
1295 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1301 err = got_packidx_match_id_str_prefix(&matched_ids,
1302 packidx, id_str_prefix);
1304 got_packidx_close(packidx);
1307 err = got_packidx_close(packidx);
1311 STAILQ_FOREACH(qid, &matched_ids, entry) {
1312 if (obj_type != GOT_OBJ_TYPE_ANY) {
1314 err = got_object_get_type(&matched_type, repo,
1318 if (matched_type != obj_type)
1321 if (*unique_id == NULL) {
1322 *unique_id = got_object_id_dup(qid->id);
1323 if (*unique_id == NULL) {
1324 err = got_error_from_errno("malloc");
1328 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1329 continue; /* packed multiple times */
1330 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1336 got_object_id_queue_free(&matched_ids);
1337 if (packdir && closedir(packdir) != 0 && err == NULL)
1338 err = got_error_from_errno("closedir");
1346 static const struct got_error *
1347 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1348 const char *object_dir, const char *id_str_prefix, int obj_type,
1349 struct got_repository *repo)
1351 const struct got_error *err = NULL;
1354 struct dirent *dent;
1355 struct got_object_id id;
1357 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1358 err = got_error_from_errno("asprintf");
1362 dir = opendir(path);
1364 if (errno == ENOENT) {
1368 err = got_error_from_errno2("opendir", path);
1371 while ((dent = readdir(dir)) != NULL) {
1375 if (strcmp(dent->d_name, ".") == 0 ||
1376 strcmp(dent->d_name, "..") == 0)
1379 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1380 err = got_error_from_errno("asprintf");
1384 if (!got_parse_sha1_digest(id.sha1, id_str))
1388 * Directory entries do not necessarily appear in
1389 * sorted order, so we must iterate over all of them.
1391 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1397 if (*unique_id == NULL) {
1398 if (obj_type != GOT_OBJ_TYPE_ANY) {
1400 err = got_object_get_type(&matched_type, repo,
1404 if (matched_type != obj_type)
1407 *unique_id = got_object_id_dup(&id);
1408 if (*unique_id == NULL) {
1409 err = got_error_from_errno("got_object_id_dup");
1414 if (got_object_id_cmp(*unique_id, &id) == 0)
1415 continue; /* both packed and loose */
1416 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1422 if (dir && closedir(dir) != 0 && err == NULL)
1423 err = got_error_from_errno("closedir");
1432 const struct got_error *
1433 got_repo_match_object_id_prefix(struct got_object_id **id,
1434 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1436 const struct got_error *err = NULL;
1437 char *path_objects = got_repo_get_path_objects(repo);
1438 char *object_dir = NULL;
1444 for (i = 0; i < strlen(id_str_prefix); i++) {
1445 if (isxdigit((unsigned char)id_str_prefix[i]))
1447 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1450 len = strlen(id_str_prefix);
1452 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1455 object_dir = strndup(id_str_prefix, 2);
1456 if (object_dir == NULL) {
1457 err = got_error_from_errno("strdup");
1460 err = match_loose_object(id, path_objects, object_dir,
1461 id_str_prefix, obj_type, repo);
1462 } else if (len == 1) {
1464 for (i = 0; i < 0xf; i++) {
1465 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1467 err = got_error_from_errno("asprintf");
1470 err = match_packed_object(id, repo, object_dir,
1474 err = match_loose_object(id, path_objects, object_dir,
1475 id_str_prefix, obj_type, repo);
1480 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1488 } else if (*id == NULL)
1489 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1494 const struct got_error *
1495 got_repo_match_object_id(struct got_object_id **id, char **label,
1496 const char *id_str, int obj_type, struct got_reflist_head *refs,
1497 struct got_repository *repo)
1499 const struct got_error *err;
1500 struct got_tag_object *tag;
1501 struct got_reference *ref = NULL;
1508 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
1511 *id = got_object_id_dup(
1512 got_object_tag_get_object_id(tag));
1514 err = got_error_from_errno("got_object_id_dup");
1515 else if (label && asprintf(label, "refs/tags/%s",
1516 got_object_tag_get_name(tag)) == -1) {
1517 err = got_error_from_errno("asprintf");
1521 got_object_tag_close(tag);
1523 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1524 err->code != GOT_ERR_NO_OBJ)
1528 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1530 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1532 err = got_ref_open(&ref, repo, id_str, 0);
1536 *label = strdup(got_ref_get_name(ref));
1537 if (*label == NULL) {
1538 err = got_error_from_errno("strdup");
1542 err = got_ref_resolve(id, repo, ref);
1544 err = got_object_id_str(label, *id);
1545 if (*label == NULL) {
1546 err = got_error_from_errno("strdup");
1556 const struct got_error *
1557 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1558 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1560 const struct got_error *err = NULL;
1561 struct got_reflist_entry *re;
1562 struct got_object_id *tag_id;
1563 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1567 TAILQ_FOREACH(re, refs, entry) {
1568 const char *refname;
1569 refname = got_ref_get_name(re->ref);
1570 if (got_ref_is_symbolic(re->ref))
1572 if (strncmp(refname, "refs/tags/", 10) != 0)
1574 if (!name_is_absolute)
1575 refname += strlen("refs/tags/");
1576 if (strcmp(refname, name) != 0)
1578 err = got_ref_resolve(&tag_id, repo, re->ref);
1581 err = got_object_open_as_tag(tag, repo, tag_id);
1585 if (obj_type == GOT_OBJ_TYPE_ANY ||
1586 got_object_tag_get_object_type(*tag) == obj_type)
1588 got_object_tag_close(*tag);
1592 if (err == NULL && *tag == NULL)
1593 err = got_error_path(name, GOT_ERR_NO_OBJ);
1597 static const struct got_error *
1598 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1599 const char *name, mode_t mode, struct got_object_id *blob_id)
1601 const struct got_error *err = NULL;
1605 *new_te = calloc(1, sizeof(**new_te));
1606 if (*new_te == NULL)
1607 return got_error_from_errno("calloc");
1609 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1610 sizeof((*new_te)->name)) {
1611 err = got_error(GOT_ERR_NO_SPACE);
1615 if (S_ISLNK(mode)) {
1616 (*new_te)->mode = S_IFLNK;
1618 (*new_te)->mode = S_IFREG;
1619 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1621 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1623 if (err && *new_te) {
1630 static const struct got_error *
1631 import_file(struct got_tree_entry **new_te, struct dirent *de,
1632 const char *path, struct got_repository *repo)
1634 const struct got_error *err;
1635 struct got_object_id *blob_id = NULL;
1639 if (asprintf(&filepath, "%s%s%s", path,
1640 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1641 return got_error_from_errno("asprintf");
1643 if (lstat(filepath, &sb) != 0) {
1644 err = got_error_from_errno2("lstat", path);
1648 err = got_object_blob_create(&blob_id, filepath, repo);
1652 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1661 static const struct got_error *
1662 insert_tree_entry(struct got_tree_entry *new_te,
1663 struct got_pathlist_head *paths)
1665 const struct got_error *err = NULL;
1666 struct got_pathlist_entry *new_pe;
1668 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1672 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1676 static const struct got_error *write_tree(struct got_object_id **,
1677 const char *, struct got_pathlist_head *, struct got_repository *,
1678 got_repo_import_cb progress_cb, void *progress_arg);
1680 static const struct got_error *
1681 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1682 const char *path, struct got_pathlist_head *ignores,
1683 struct got_repository *repo,
1684 got_repo_import_cb progress_cb, void *progress_arg)
1686 const struct got_error *err;
1687 struct got_object_id *id = NULL;
1690 if (asprintf(&subdirpath, "%s%s%s", path,
1691 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1692 return got_error_from_errno("asprintf");
1694 (*new_te) = calloc(1, sizeof(**new_te));
1695 if (*new_te == NULL)
1696 return got_error_from_errno("calloc");
1697 (*new_te)->mode = S_IFDIR;
1698 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1699 sizeof((*new_te)->name)) {
1700 err = got_error(GOT_ERR_NO_SPACE);
1703 err = write_tree(&id, subdirpath, ignores, repo,
1704 progress_cb, progress_arg);
1707 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1719 static const struct got_error *
1720 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1721 struct got_pathlist_head *ignores, struct got_repository *repo,
1722 got_repo_import_cb progress_cb, void *progress_arg)
1724 const struct got_error *err = NULL;
1728 struct got_tree_entry *new_te = NULL;
1729 struct got_pathlist_head paths;
1730 struct got_pathlist_entry *pe;
1732 *new_tree_id = NULL;
1736 dir = opendir(path_dir);
1738 err = got_error_from_errno2("opendir", path_dir);
1743 while ((de = readdir(dir)) != NULL) {
1747 if (strcmp(de->d_name, ".") == 0 ||
1748 strcmp(de->d_name, "..") == 0)
1751 TAILQ_FOREACH(pe, ignores, entry) {
1752 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1760 err = got_path_dirent_type(&type, path_dir, de);
1764 if (type == DT_DIR) {
1765 err = import_subdir(&new_te, de, path_dir,
1766 ignores, repo, progress_cb, progress_arg);
1768 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1773 } else if (type == DT_REG || type == DT_LNK) {
1774 err = import_file(&new_te, de, path_dir, repo);
1780 err = insert_tree_entry(new_te, &paths);
1786 if (TAILQ_EMPTY(&paths)) {
1787 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
1788 "cannot create tree without any entries");
1792 TAILQ_FOREACH(pe, &paths, entry) {
1793 struct got_tree_entry *te = pe->data;
1795 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
1797 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1798 err = got_error_from_errno("asprintf");
1801 err = (*progress_cb)(progress_arg, path);
1807 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1811 got_pathlist_free(&paths);
1815 const struct got_error *
1816 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1817 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1818 struct got_repository *repo, got_repo_import_cb progress_cb,
1821 const struct got_error *err;
1822 struct got_object_id *new_tree_id;
1824 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1825 progress_cb, progress_arg);
1829 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1830 author, time(NULL), author, time(NULL), logmsg, repo);
1835 const struct got_error *
1836 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
1837 struct got_repository *repo)
1839 const struct got_error *err = NULL;
1840 char *path_objects = NULL, *path = NULL;
1842 struct got_object_id id;
1848 path_objects = got_repo_get_path_objects(repo);
1849 if (path_objects == NULL)
1850 return got_error_from_errno("got_repo_get_path_objects");
1852 for (i = 0; i <= 0xff; i++) {
1853 struct dirent *dent;
1855 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
1856 err = got_error_from_errno("asprintf");
1860 dir = opendir(path);
1862 if (errno == ENOENT) {
1866 err = got_error_from_errno2("opendir", path);
1870 while ((dent = readdir(dir)) != NULL) {
1875 if (strcmp(dent->d_name, ".") == 0 ||
1876 strcmp(dent->d_name, "..") == 0)
1879 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
1880 err = got_error_from_errno("asprintf");
1884 if (!got_parse_sha1_digest(id.sha1, id_str)) {
1890 err = got_object_open_loose_fd(&fd, &id, repo);
1894 if (fstat(fd, &sb) == -1) {
1895 err = got_error_from_errno("fstat");
1900 (*ondisk_size) += sb.st_size;
1902 if (close(fd) == -1) {
1903 err = got_error_from_errno("close");
1908 if (closedir(dir) != 0) {
1909 err = got_error_from_errno("closedir");
1918 if (dir && closedir(dir) != 0 && err == NULL)
1919 err = got_error_from_errno("closedir");
1930 const struct got_error *
1931 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
1932 off_t *total_packsize, struct got_repository *repo)
1934 const struct got_error *err = NULL;
1935 DIR *packdir = NULL;
1936 struct dirent *dent;
1937 struct got_packidx *packidx = NULL;
1939 char *path_packfile;
1945 *total_packsize = 0;
1947 packdir_fd = openat(got_repo_get_fd(repo),
1948 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1949 if (packdir_fd == -1) {
1950 return got_error_from_errno_fmt("openat: %s/%s",
1951 got_repo_get_path_git_dir(repo),
1952 GOT_OBJECTS_PACK_DIR);
1955 packdir = fdopendir(packdir_fd);
1956 if (packdir == NULL) {
1957 err = got_error_from_errno("fdopendir");
1961 while ((dent = readdir(packdir)) != NULL) {
1962 if (!got_repo_is_packidx_filename(dent->d_name,
1963 strlen(dent->d_name)))
1966 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1967 dent->d_name) == -1) {
1968 err = got_error_from_errno("asprintf");
1972 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1978 if (fstat(packidx->fd, &sb) == -1)
1980 *total_packsize += sb.st_size;
1982 err = got_packidx_get_packfile_path(&path_packfile,
1983 packidx->path_packidx);
1987 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
1989 free(path_packfile);
1992 free(path_packfile);
1993 *total_packsize += sb.st_size;
1995 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
1999 got_packidx_close(packidx);
2004 got_packidx_close(packidx);
2005 if (packdir && closedir(packdir) != 0 && err == NULL)
2006 err = got_error_from_errno("closedir");
2010 *total_packsize = 0;