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/resource.h>
44 #include "got_error.h"
45 #include "got_reference.h"
46 #include "got_repository.h"
48 #include "got_cancel.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_sha1.h"
59 #include "got_lib_object_cache.h"
60 #include "got_lib_repository.h"
61 #include "got_lib_gotconfig.h"
64 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
68 got_repo_get_path(struct got_repository *repo)
74 got_repo_get_path_git_dir(struct got_repository *repo)
76 return repo->path_git_dir;
80 got_repo_get_fd(struct got_repository *repo)
82 return repo->gitdir_fd;
86 got_repo_get_gitconfig_author_name(struct got_repository *repo)
88 return repo->gitconfig_author_name;
92 got_repo_get_gitconfig_author_email(struct got_repository *repo)
94 return repo->gitconfig_author_email;
98 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
100 return repo->global_gitconfig_author_name;
104 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
106 return repo->global_gitconfig_author_email;
110 got_repo_get_gitconfig_owner(struct got_repository *repo)
112 return repo->gitconfig_owner;
116 got_repo_get_gitconfig_extensions(char ***extensions, int *nextensions,
117 struct got_repository *repo)
119 *extensions = repo->extensions;
120 *nextensions = repo->nextensions;
124 got_repo_is_bare(struct got_repository *repo)
126 return (strcmp(repo->path, repo->path_git_dir) == 0);
130 get_path_git_child(struct got_repository *repo, const char *basename)
134 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
142 got_repo_get_path_objects(struct got_repository *repo)
144 return get_path_git_child(repo, GOT_OBJECTS_DIR);
148 got_repo_get_path_objects_pack(struct got_repository *repo)
150 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
154 got_repo_get_path_refs(struct got_repository *repo)
156 return get_path_git_child(repo, GOT_REFS_DIR);
160 got_repo_get_path_packed_refs(struct got_repository *repo)
162 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
166 get_path_head(struct got_repository *repo)
168 return get_path_git_child(repo, GOT_HEAD_FILE);
172 got_repo_get_path_gitconfig(struct got_repository *repo)
174 return get_path_git_child(repo, GOT_GITCONFIG);
178 got_repo_get_path_gotconfig(struct got_repository *repo)
180 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
183 const struct got_gotconfig *
184 got_repo_get_gotconfig(struct got_repository *repo)
186 return repo->gotconfig;
190 got_repo_get_gitconfig_remotes(int *nremotes,
191 const struct got_remote_repo **remotes, struct got_repository *repo)
193 *nremotes = repo->ngitconfig_remotes;
194 *remotes = repo->gitconfig_remotes;
198 is_git_repo(struct got_repository *repo)
200 const char *path_git = got_repo_get_path_git_dir(repo);
201 char *path_objects = got_repo_get_path_objects(repo);
202 char *path_refs = got_repo_get_path_refs(repo);
203 char *path_head = get_path_head(repo);
206 struct got_reference *head_ref;
208 if (lstat(path_git, &sb) == -1)
210 if (!S_ISDIR(sb.st_mode))
213 if (lstat(path_objects, &sb) == -1)
215 if (!S_ISDIR(sb.st_mode))
218 if (lstat(path_refs, &sb) == -1)
220 if (!S_ISDIR(sb.st_mode))
223 if (lstat(path_head, &sb) == -1)
225 if (!S_ISREG(sb.st_mode))
228 /* Check if the HEAD reference can be opened. */
229 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
231 got_ref_close(head_ref);
242 const struct got_error *
243 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
244 struct got_object *obj)
246 #ifndef GOT_NO_OBJ_CACHE
247 const struct got_error *err = NULL;
248 err = got_object_cache_add(&repo->objcache, id, obj);
250 if (err->code == GOT_ERR_OBJ_EXISTS ||
251 err->code == GOT_ERR_OBJ_TOO_LARGE)
261 got_repo_get_cached_object(struct got_repository *repo,
262 struct got_object_id *id)
264 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
267 const struct got_error *
268 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
269 struct got_tree_object *tree)
271 #ifndef GOT_NO_OBJ_CACHE
272 const struct got_error *err = NULL;
273 err = got_object_cache_add(&repo->treecache, id, tree);
275 if (err->code == GOT_ERR_OBJ_EXISTS ||
276 err->code == GOT_ERR_OBJ_TOO_LARGE)
285 struct got_tree_object *
286 got_repo_get_cached_tree(struct got_repository *repo,
287 struct got_object_id *id)
289 return (struct got_tree_object *)got_object_cache_get(
290 &repo->treecache, id);
293 const struct got_error *
294 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
295 struct got_commit_object *commit)
297 #ifndef GOT_NO_OBJ_CACHE
298 const struct got_error *err = NULL;
299 err = got_object_cache_add(&repo->commitcache, id, commit);
301 if (err->code == GOT_ERR_OBJ_EXISTS ||
302 err->code == GOT_ERR_OBJ_TOO_LARGE)
311 struct got_commit_object *
312 got_repo_get_cached_commit(struct got_repository *repo,
313 struct got_object_id *id)
315 return (struct got_commit_object *)got_object_cache_get(
316 &repo->commitcache, id);
319 const struct got_error *
320 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
321 struct got_tag_object *tag)
323 #ifndef GOT_NO_OBJ_CACHE
324 const struct got_error *err = NULL;
325 err = got_object_cache_add(&repo->tagcache, id, tag);
327 if (err->code == GOT_ERR_OBJ_EXISTS ||
328 err->code == GOT_ERR_OBJ_TOO_LARGE)
337 struct got_tag_object *
338 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
340 return (struct got_tag_object *)got_object_cache_get(
341 &repo->tagcache, id);
344 static const struct got_error *
345 open_repo(struct got_repository *repo, const char *path)
347 const struct got_error *err = NULL;
349 repo->gitdir_fd = -1;
351 /* bare git repository? */
352 repo->path_git_dir = strdup(path);
353 if (repo->path_git_dir == NULL)
354 return got_error_from_errno("strdup");
355 if (is_git_repo(repo)) {
356 repo->path = strdup(repo->path_git_dir);
357 if (repo->path == NULL) {
358 err = got_error_from_errno("strdup");
361 repo->gitdir_fd = open(repo->path_git_dir, O_DIRECTORY);
362 if (repo->gitdir_fd == -1) {
363 err = got_error_from_errno2("open",
370 /* git repository with working tree? */
371 free(repo->path_git_dir);
372 repo->path_git_dir = NULL;
373 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
374 err = got_error_from_errno("asprintf");
377 if (is_git_repo(repo)) {
378 repo->path = strdup(path);
379 if (repo->path == NULL) {
380 err = got_error_from_errno("strdup");
383 repo->gitdir_fd = open(repo->path_git_dir, O_DIRECTORY);
384 if (repo->gitdir_fd == -1) {
385 err = got_error_from_errno2("open",
392 err = got_error(GOT_ERR_NOT_GIT_REPO);
397 free(repo->path_git_dir);
398 repo->path_git_dir = NULL;
399 if (repo->gitdir_fd != -1)
400 close(repo->gitdir_fd);
401 repo->gitdir_fd = -1;
407 static const struct got_error *
408 parse_gitconfig_file(int *gitconfig_repository_format_version,
409 char **gitconfig_author_name, char **gitconfig_author_email,
410 struct got_remote_repo **remotes, int *nremotes,
411 char **gitconfig_owner, char ***extensions, int *nextensions,
412 const char *gitconfig_path)
414 const struct got_error *err = NULL, *child_err = NULL;
416 int imsg_fds[2] = { -1, -1 };
418 struct imsgbuf *ibuf;
420 *gitconfig_repository_format_version = 0;
425 *gitconfig_author_name = NULL;
426 *gitconfig_author_email = NULL;
432 *gitconfig_owner = NULL;
434 fd = open(gitconfig_path, O_RDONLY);
438 return got_error_from_errno2("open", gitconfig_path);
441 ibuf = calloc(1, sizeof(*ibuf));
443 err = got_error_from_errno("calloc");
447 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
448 err = got_error_from_errno("socketpair");
454 err = got_error_from_errno("fork");
456 } else if (pid == 0) {
457 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
462 if (close(imsg_fds[1]) == -1) {
463 err = got_error_from_errno("close");
467 imsg_init(ibuf, imsg_fds[0]);
469 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
474 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
478 err = got_privsep_recv_gitconfig_int(
479 gitconfig_repository_format_version, ibuf);
483 if (extensions && nextensions) {
484 err = got_privsep_send_gitconfig_repository_extensions_req(
488 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
491 if (*nextensions > 0) {
493 *extensions = calloc(*nextensions, sizeof(char *));
494 if (*extensions == NULL) {
495 err = got_error_from_errno("calloc");
498 for (i = 0; i < *nextensions; i++) {
500 err = got_privsep_recv_gitconfig_str(&ext,
504 (*extensions)[i] = ext;
509 err = got_privsep_send_gitconfig_author_name_req(ibuf);
513 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
517 err = got_privsep_send_gitconfig_author_email_req(ibuf);
521 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
525 if (remotes && nremotes) {
526 err = got_privsep_send_gitconfig_remotes_req(ibuf);
530 err = got_privsep_recv_gitconfig_remotes(remotes,
536 if (gitconfig_owner) {
537 err = got_privsep_send_gitconfig_owner_req(ibuf);
540 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
546 err = got_privsep_send_stop(imsg_fds[0]);
547 child_err = got_privsep_wait_for_child(pid);
548 if (child_err && err == NULL)
551 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
552 err = got_error_from_errno("close");
553 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
554 err = got_error_from_errno("close");
555 if (fd != -1 && close(fd) == -1 && err == NULL)
556 err = got_error_from_errno2("close", gitconfig_path);
561 static const struct got_error *
562 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
564 const struct got_error *err = NULL;
565 char *repo_gitconfig_path = NULL;
567 if (global_gitconfig_path) {
568 /* Read settings from ~/.gitconfig. */
569 int dummy_repo_version;
570 err = parse_gitconfig_file(&dummy_repo_version,
571 &repo->global_gitconfig_author_name,
572 &repo->global_gitconfig_author_email,
573 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
578 /* Read repository's .git/config file. */
579 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
580 if (repo_gitconfig_path == NULL)
581 return got_error_from_errno("got_repo_get_path_gitconfig");
583 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
584 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
585 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
586 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
587 repo_gitconfig_path);
591 free(repo_gitconfig_path);
595 static const struct got_error *
596 read_gotconfig(struct got_repository *repo)
598 const struct got_error *err = NULL;
599 char *gotconfig_path;
601 gotconfig_path = got_repo_get_path_gotconfig(repo);
602 if (gotconfig_path == NULL)
603 return got_error_from_errno("got_repo_get_path_gotconfig");
605 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
606 free(gotconfig_path);
610 /* Supported repository format extensions. */
611 static const char *repo_extensions[] = {
612 "noop", /* Got supports repository format version 1. */
613 "preciousObjects", /* Supported by gotadmin cleanup. */
614 "worktreeConfig", /* Got does not care about Git work trees. */
617 const struct got_error *
618 got_repo_open(struct got_repository **repop, const char *path,
619 const char *global_gitconfig_path)
621 struct got_repository *repo = NULL;
622 const struct got_error *err = NULL;
623 char *repo_path = NULL;
629 if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
630 return got_error_from_errno("getrlimit");
632 repo = calloc(1, sizeof(*repo));
634 err = got_error_from_errno("calloc");
638 for (i = 0; i < nitems(repo->privsep_children); i++) {
639 memset(&repo->privsep_children[i], 0,
640 sizeof(repo->privsep_children[0]));
641 repo->privsep_children[i].imsg_fd = -1;
644 err = got_object_cache_init(&repo->objcache,
645 GOT_OBJECT_CACHE_TYPE_OBJ);
648 err = got_object_cache_init(&repo->treecache,
649 GOT_OBJECT_CACHE_TYPE_TREE);
652 err = got_object_cache_init(&repo->commitcache,
653 GOT_OBJECT_CACHE_TYPE_COMMIT);
656 err = got_object_cache_init(&repo->tagcache,
657 GOT_OBJECT_CACHE_TYPE_TAG);
661 repo->pack_cache_size = GOT_PACK_CACHE_SIZE;
662 if (repo->pack_cache_size > rl.rlim_cur / 8)
663 repo->pack_cache_size = rl.rlim_cur / 8;
665 repo_path = realpath(path, NULL);
666 if (repo_path == NULL) {
667 err = got_error_from_errno2("realpath", path);
674 err = open_repo(repo, repo_path);
677 if (err->code != GOT_ERR_NOT_GIT_REPO)
679 if (repo_path[0] == '/' && repo_path[1] == '\0') {
680 err = got_error(GOT_ERR_NOT_GIT_REPO);
683 err = got_path_dirname(&parent_path, repo_path);
687 repo_path = parent_path;
690 err = read_gotconfig(repo);
694 err = read_gitconfig(repo, global_gitconfig_path);
697 if (repo->gitconfig_repository_format_version != 0)
698 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
699 for (i = 0; i < repo->nextensions; i++) {
700 char *ext = repo->extensions[i];
701 int j, supported = 0;
702 for (j = 0; j < nitems(repo_extensions); j++) {
703 if (strcmp(ext, repo_extensions[j]) == 0) {
709 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
715 got_repo_close(repo);
722 const struct got_error *
723 got_repo_close(struct got_repository *repo)
725 const struct got_error *err = NULL, *child_err;
728 for (i = 0; i < repo->pack_cache_size; i++) {
729 if (repo->packidx_cache[i] == NULL)
731 got_packidx_close(repo->packidx_cache[i]);
734 for (i = 0; i < repo->pack_cache_size; i++) {
735 if (repo->packs[i].path_packfile == NULL)
737 got_pack_close(&repo->packs[i]);
741 free(repo->path_git_dir);
743 got_object_cache_close(&repo->objcache);
744 got_object_cache_close(&repo->treecache);
745 got_object_cache_close(&repo->commitcache);
746 got_object_cache_close(&repo->tagcache);
748 for (i = 0; i < nitems(repo->privsep_children); i++) {
749 if (repo->privsep_children[i].imsg_fd == -1)
751 imsg_clear(repo->privsep_children[i].ibuf);
752 free(repo->privsep_children[i].ibuf);
753 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
754 child_err = got_privsep_wait_for_child(
755 repo->privsep_children[i].pid);
756 if (child_err && err == NULL)
758 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
760 err = got_error_from_errno("close");
763 if (repo->gitdir_fd != -1 && close(repo->gitdir_fd) == -1 &&
765 err = got_error_from_errno("close");
768 got_gotconfig_free(repo->gotconfig);
769 free(repo->gitconfig_author_name);
770 free(repo->gitconfig_author_email);
771 for (i = 0; i < repo->ngitconfig_remotes; i++)
772 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
773 free(repo->gitconfig_remotes);
774 for (i = 0; i < repo->nextensions; i++)
775 free(repo->extensions[i]);
776 free(repo->extensions);
783 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
791 for (i = 0; i < repo->nbranches; i++)
792 free(repo->branches[i]);
793 free(repo->branches);
794 repo->branches = NULL;
798 const struct got_error *
799 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
800 const char *input_path)
802 const struct got_error *err = NULL;
803 const char *repo_abspath = NULL;
805 char *canonpath, *path = NULL;
807 *in_repo_path = NULL;
809 canonpath = strdup(input_path);
810 if (canonpath == NULL) {
811 err = got_error_from_errno("strdup");
814 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
818 repo_abspath = got_repo_get_path(repo);
820 if (canonpath[0] == '\0') {
821 path = strdup(canonpath);
823 err = got_error_from_errno("strdup");
827 path = realpath(canonpath, NULL);
829 if (errno != ENOENT) {
830 err = got_error_from_errno2("realpath",
835 * Path is not on disk.
836 * Assume it is already relative to repository root.
838 path = strdup(canonpath);
840 err = got_error_from_errno("strdup");
845 repolen = strlen(repo_abspath);
849 if (strcmp(path, repo_abspath) == 0) {
853 err = got_error_from_errno("strdup");
856 } else if (len > repolen &&
857 got_path_is_child(path, repo_abspath, repolen)) {
858 /* Matched an on-disk path inside repository. */
859 if (got_repo_is_bare(repo)) {
861 * Matched an on-disk path inside repository
862 * database. Treat input as repository-relative.
869 /* Strip common prefix with repository path. */
870 err = got_path_skip_common_ancestor(&child,
879 * Matched unrelated on-disk path.
880 * Treat input as repository-relative.
888 /* Make in-repository path absolute */
889 if (path[0] != '/') {
891 if (asprintf(&abspath, "/%s", path) == -1) {
892 err = got_error_from_errno("asprintf");
904 *in_repo_path = path;
908 static const struct got_error *
909 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
910 const char *path_packidx)
912 const struct got_error *err = NULL;
915 for (i = 0; i < repo->pack_cache_size; i++) {
916 if (repo->packidx_cache[i] == NULL)
918 if (strcmp(repo->packidx_cache[i]->path_packidx,
919 path_packidx) == 0) {
920 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
923 if (i == repo->pack_cache_size) {
924 err = got_packidx_close(repo->packidx_cache[i - 1]);
930 * Insert the new pack index at the front so it will
931 * be searched first in the future.
933 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
934 sizeof(repo->packidx_cache) -
935 sizeof(repo->packidx_cache[0]));
936 repo->packidx_cache[0] = packidx;
942 got_repo_is_packidx_filename(const char *name, size_t len)
944 if (len != GOT_PACKIDX_NAMELEN)
947 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
950 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
951 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
957 const struct got_error *
958 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
959 struct got_repository *repo, struct got_object_id *id)
961 const struct got_error *err;
968 /* Search pack index cache. */
969 for (i = 0; i < repo->pack_cache_size; i++) {
970 if (repo->packidx_cache[i] == NULL)
972 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
974 *packidx = repo->packidx_cache[i];
976 * Move this cache entry to the front. Repeatedly
977 * searching a wrong pack index can be expensive.
980 struct got_packidx *p;
981 p = repo->packidx_cache[0];
982 repo->packidx_cache[0] = *packidx;
983 repo->packidx_cache[i] = p;
988 /* No luck. Search the filesystem. */
990 packdir_fd = openat(got_repo_get_fd(repo),
991 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
992 if (packdir_fd == -1) {
994 err = got_error_no_obj(id);
996 err = got_error_from_errno_fmt("openat: %s/%s",
997 got_repo_get_path_git_dir(repo),
998 GOT_OBJECTS_PACK_DIR);
1002 packdir = fdopendir(packdir_fd);
1003 if (packdir == NULL) {
1004 err = got_error_from_errno("fdopendir");
1008 while ((dent = readdir(packdir)) != NULL) {
1011 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1014 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1015 dent->d_name) == -1) {
1016 err = got_error_from_errno("asprintf");
1020 for (i = 0; i < repo->pack_cache_size; i++) {
1021 if (repo->packidx_cache[i] == NULL)
1023 if (strcmp(repo->packidx_cache[i]->path_packidx,
1024 path_packidx) == 0) {
1031 continue; /* already searched */
1034 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1041 err = cache_packidx(repo, *packidx, path_packidx);
1046 *idx = got_packidx_get_object_idx(*packidx, id);
1048 err = NULL; /* found the object */
1053 err = got_error_no_obj(id);
1055 if (packdir && closedir(packdir) != 0 && err == NULL)
1056 err = got_error_from_errno("closedir");
1060 static const struct got_error *
1061 read_packfile_hdr(int fd, struct got_packidx *packidx)
1063 const struct got_error *err = NULL;
1064 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1065 struct got_packfile_hdr hdr;
1068 n = read(fd, &hdr, sizeof(hdr));
1070 return got_error_from_errno("read");
1071 if (n != sizeof(hdr))
1072 return got_error(GOT_ERR_BAD_PACKFILE);
1074 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1075 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1076 be32toh(hdr.nobjects) != totobj)
1077 err = got_error(GOT_ERR_BAD_PACKFILE);
1082 static const struct got_error *
1083 open_packfile(int *fd, struct got_repository *repo,
1084 const char *relpath, struct got_packidx *packidx)
1086 const struct got_error *err = NULL;
1088 *fd = openat(got_repo_get_fd(repo), relpath, O_RDONLY | O_NOFOLLOW);
1090 return got_error_from_errno_fmt("openat: %s/%s",
1091 got_repo_get_path_git_dir(repo), relpath);
1094 err = read_packfile_hdr(*fd, packidx);
1104 const struct got_error *
1105 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1106 const char *path_packfile, struct got_packidx *packidx)
1108 const struct got_error *err = NULL;
1109 struct got_pack *pack = NULL;
1116 for (i = 0; i < repo->pack_cache_size; i++) {
1117 pack = &repo->packs[i];
1118 if (pack->path_packfile == NULL)
1120 if (strcmp(pack->path_packfile, path_packfile) == 0)
1121 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1124 if (i == repo->pack_cache_size) {
1125 err = got_pack_close(&repo->packs[i - 1]);
1128 memmove(&repo->packs[1], &repo->packs[0],
1129 sizeof(repo->packs) - sizeof(repo->packs[0]));
1133 pack = &repo->packs[i];
1135 pack->path_packfile = strdup(path_packfile);
1136 if (pack->path_packfile == NULL) {
1137 err = got_error_from_errno("strdup");
1141 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1145 if (fstat(pack->fd, &sb) != 0) {
1146 err = got_error_from_errno("fstat");
1149 pack->filesize = sb.st_size;
1151 pack->privsep_child = NULL;
1153 #ifndef GOT_PACK_NO_MMAP
1154 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1156 if (pack->map == MAP_FAILED) {
1157 if (errno != ENOMEM) {
1158 err = got_error_from_errno("mmap");
1161 pack->map = NULL; /* fall back to read(2) */
1167 free(pack->path_packfile);
1168 memset(pack, 0, sizeof(*pack));
1176 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1178 struct got_pack *pack = NULL;
1181 for (i = 0; i < repo->pack_cache_size; i++) {
1182 pack = &repo->packs[i];
1183 if (pack->path_packfile == NULL)
1185 if (strcmp(pack->path_packfile, path_packfile) == 0)
1192 const struct got_error *
1193 got_repo_init(const char *repo_path)
1195 const struct got_error *err = NULL;
1196 const char *dirnames[] = {
1198 GOT_OBJECTS_PACK_DIR,
1201 const char *description_str = "Unnamed repository; "
1202 "edit this file 'description' to name the repository.";
1203 const char *headref_str = "ref: refs/heads/main";
1204 const char *gitconfig_str = "[core]\n"
1205 "\trepositoryformatversion = 0\n"
1206 "\tfilemode = true\n"
1211 if (!got_path_dir_is_empty(repo_path))
1212 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1214 for (i = 0; i < nitems(dirnames); i++) {
1215 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1216 return got_error_from_errno("asprintf");
1218 err = got_path_mkdir(path);
1224 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1225 return got_error_from_errno("asprintf");
1226 err = got_path_create_file(path, description_str);
1231 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1232 return got_error_from_errno("asprintf");
1233 err = got_path_create_file(path, headref_str);
1238 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1239 return got_error_from_errno("asprintf");
1240 err = got_path_create_file(path, gitconfig_str);
1248 static const struct got_error *
1249 match_packed_object(struct got_object_id **unique_id,
1250 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1252 const struct got_error *err = NULL;
1253 DIR *packdir = NULL;
1254 struct dirent *dent;
1256 struct got_object_id_queue matched_ids;
1259 STAILQ_INIT(&matched_ids);
1261 packdir_fd = openat(got_repo_get_fd(repo),
1262 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1263 if (packdir_fd == -1) {
1264 if (errno != ENOENT)
1265 err = got_error_from_errno2("openat", GOT_OBJECTS_PACK_DIR);
1269 packdir = fdopendir(packdir_fd);
1270 if (packdir == NULL) {
1271 err = got_error_from_errno("fdopendir");
1275 while ((dent = readdir(packdir)) != NULL) {
1276 struct got_packidx *packidx;
1277 struct got_object_qid *qid;
1279 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1282 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1283 dent->d_name) == -1) {
1284 err = got_error_from_errno("strdup");
1288 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1294 err = got_packidx_match_id_str_prefix(&matched_ids,
1295 packidx, id_str_prefix);
1297 got_packidx_close(packidx);
1300 err = got_packidx_close(packidx);
1304 STAILQ_FOREACH(qid, &matched_ids, entry) {
1305 if (obj_type != GOT_OBJ_TYPE_ANY) {
1307 err = got_object_get_type(&matched_type, repo,
1311 if (matched_type != obj_type)
1314 if (*unique_id == NULL) {
1315 *unique_id = got_object_id_dup(qid->id);
1316 if (*unique_id == NULL) {
1317 err = got_error_from_errno("malloc");
1321 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1322 continue; /* packed multiple times */
1323 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1329 got_object_id_queue_free(&matched_ids);
1330 if (packdir && closedir(packdir) != 0 && err == NULL)
1331 err = got_error_from_errno("closedir");
1339 static const struct got_error *
1340 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1341 const char *object_dir, const char *id_str_prefix, int obj_type,
1342 struct got_repository *repo)
1344 const struct got_error *err = NULL;
1347 struct dirent *dent;
1348 struct got_object_id id;
1350 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1351 err = got_error_from_errno("asprintf");
1355 dir = opendir(path);
1357 if (errno == ENOENT) {
1361 err = got_error_from_errno2("opendir", path);
1364 while ((dent = readdir(dir)) != NULL) {
1368 if (strcmp(dent->d_name, ".") == 0 ||
1369 strcmp(dent->d_name, "..") == 0)
1372 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1373 err = got_error_from_errno("asprintf");
1377 if (!got_parse_sha1_digest(id.sha1, id_str))
1381 * Directory entries do not necessarily appear in
1382 * sorted order, so we must iterate over all of them.
1384 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1390 if (*unique_id == NULL) {
1391 if (obj_type != GOT_OBJ_TYPE_ANY) {
1393 err = got_object_get_type(&matched_type, repo,
1397 if (matched_type != obj_type)
1400 *unique_id = got_object_id_dup(&id);
1401 if (*unique_id == NULL) {
1402 err = got_error_from_errno("got_object_id_dup");
1407 if (got_object_id_cmp(*unique_id, &id) == 0)
1408 continue; /* both packed and loose */
1409 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1415 if (dir && closedir(dir) != 0 && err == NULL)
1416 err = got_error_from_errno("closedir");
1425 const struct got_error *
1426 got_repo_match_object_id_prefix(struct got_object_id **id,
1427 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1429 const struct got_error *err = NULL;
1430 char *path_objects = got_repo_get_path_objects(repo);
1431 char *object_dir = NULL;
1437 for (i = 0; i < strlen(id_str_prefix); i++) {
1438 if (isxdigit((unsigned char)id_str_prefix[i]))
1440 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1443 len = strlen(id_str_prefix);
1445 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1448 object_dir = strndup(id_str_prefix, 2);
1449 if (object_dir == NULL) {
1450 err = got_error_from_errno("strdup");
1453 err = match_loose_object(id, path_objects, object_dir,
1454 id_str_prefix, obj_type, repo);
1455 } else if (len == 1) {
1457 for (i = 0; i < 0xf; i++) {
1458 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1460 err = got_error_from_errno("asprintf");
1463 err = match_packed_object(id, repo, object_dir,
1467 err = match_loose_object(id, path_objects, object_dir,
1468 id_str_prefix, obj_type, repo);
1473 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1481 } else if (*id == NULL)
1482 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1487 const struct got_error *
1488 got_repo_match_object_id(struct got_object_id **id, char **label,
1489 const char *id_str, int obj_type, struct got_reflist_head *refs,
1490 struct got_repository *repo)
1492 const struct got_error *err;
1493 struct got_tag_object *tag;
1494 struct got_reference *ref = NULL;
1501 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
1504 *id = got_object_id_dup(
1505 got_object_tag_get_object_id(tag));
1507 err = got_error_from_errno("got_object_id_dup");
1508 else if (label && asprintf(label, "refs/tags/%s",
1509 got_object_tag_get_name(tag)) == -1) {
1510 err = got_error_from_errno("asprintf");
1514 got_object_tag_close(tag);
1516 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1517 err->code != GOT_ERR_NO_OBJ)
1521 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1523 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1525 err = got_ref_open(&ref, repo, id_str, 0);
1529 *label = strdup(got_ref_get_name(ref));
1530 if (*label == NULL) {
1531 err = got_error_from_errno("strdup");
1535 err = got_ref_resolve(id, repo, ref);
1537 err = got_object_id_str(label, *id);
1538 if (*label == NULL) {
1539 err = got_error_from_errno("strdup");
1549 const struct got_error *
1550 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1551 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1553 const struct got_error *err = NULL;
1554 struct got_reflist_entry *re;
1555 struct got_object_id *tag_id;
1556 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1560 TAILQ_FOREACH(re, refs, entry) {
1561 const char *refname;
1562 refname = got_ref_get_name(re->ref);
1563 if (got_ref_is_symbolic(re->ref))
1565 if (strncmp(refname, "refs/tags/", 10) != 0)
1567 if (!name_is_absolute)
1568 refname += strlen("refs/tags/");
1569 if (strcmp(refname, name) != 0)
1571 err = got_ref_resolve(&tag_id, repo, re->ref);
1574 err = got_object_open_as_tag(tag, repo, tag_id);
1578 if (obj_type == GOT_OBJ_TYPE_ANY ||
1579 got_object_tag_get_object_type(*tag) == obj_type)
1581 got_object_tag_close(*tag);
1585 if (err == NULL && *tag == NULL)
1586 err = got_error_path(name, GOT_ERR_NO_OBJ);
1590 static const struct got_error *
1591 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1592 const char *name, mode_t mode, struct got_object_id *blob_id)
1594 const struct got_error *err = NULL;
1598 *new_te = calloc(1, sizeof(**new_te));
1599 if (*new_te == NULL)
1600 return got_error_from_errno("calloc");
1602 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1603 sizeof((*new_te)->name)) {
1604 err = got_error(GOT_ERR_NO_SPACE);
1608 if (S_ISLNK(mode)) {
1609 (*new_te)->mode = S_IFLNK;
1611 (*new_te)->mode = S_IFREG;
1612 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1614 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1616 if (err && *new_te) {
1623 static const struct got_error *
1624 import_file(struct got_tree_entry **new_te, struct dirent *de,
1625 const char *path, struct got_repository *repo)
1627 const struct got_error *err;
1628 struct got_object_id *blob_id = NULL;
1632 if (asprintf(&filepath, "%s%s%s", path,
1633 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1634 return got_error_from_errno("asprintf");
1636 if (lstat(filepath, &sb) != 0) {
1637 err = got_error_from_errno2("lstat", path);
1641 err = got_object_blob_create(&blob_id, filepath, repo);
1645 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1654 static const struct got_error *
1655 insert_tree_entry(struct got_tree_entry *new_te,
1656 struct got_pathlist_head *paths)
1658 const struct got_error *err = NULL;
1659 struct got_pathlist_entry *new_pe;
1661 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1665 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1669 static const struct got_error *write_tree(struct got_object_id **,
1670 const char *, struct got_pathlist_head *, struct got_repository *,
1671 got_repo_import_cb progress_cb, void *progress_arg);
1673 static const struct got_error *
1674 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1675 const char *path, struct got_pathlist_head *ignores,
1676 struct got_repository *repo,
1677 got_repo_import_cb progress_cb, void *progress_arg)
1679 const struct got_error *err;
1680 struct got_object_id *id = NULL;
1683 if (asprintf(&subdirpath, "%s%s%s", path,
1684 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1685 return got_error_from_errno("asprintf");
1687 (*new_te) = calloc(1, sizeof(**new_te));
1688 if (*new_te == NULL)
1689 return got_error_from_errno("calloc");
1690 (*new_te)->mode = S_IFDIR;
1691 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1692 sizeof((*new_te)->name)) {
1693 err = got_error(GOT_ERR_NO_SPACE);
1696 err = write_tree(&id, subdirpath, ignores, repo,
1697 progress_cb, progress_arg);
1700 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1712 static const struct got_error *
1713 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1714 struct got_pathlist_head *ignores, struct got_repository *repo,
1715 got_repo_import_cb progress_cb, void *progress_arg)
1717 const struct got_error *err = NULL;
1721 struct got_tree_entry *new_te = NULL;
1722 struct got_pathlist_head paths;
1723 struct got_pathlist_entry *pe;
1725 *new_tree_id = NULL;
1729 dir = opendir(path_dir);
1731 err = got_error_from_errno2("opendir", path_dir);
1736 while ((de = readdir(dir)) != NULL) {
1740 if (strcmp(de->d_name, ".") == 0 ||
1741 strcmp(de->d_name, "..") == 0)
1744 TAILQ_FOREACH(pe, ignores, entry) {
1745 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1753 err = got_path_dirent_type(&type, path_dir, de);
1757 if (type == DT_DIR) {
1758 err = import_subdir(&new_te, de, path_dir,
1759 ignores, repo, progress_cb, progress_arg);
1761 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1766 } else if (type == DT_REG || type == DT_LNK) {
1767 err = import_file(&new_te, de, path_dir, repo);
1773 err = insert_tree_entry(new_te, &paths);
1779 if (TAILQ_EMPTY(&paths)) {
1780 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
1781 "cannot create tree without any entries");
1785 TAILQ_FOREACH(pe, &paths, entry) {
1786 struct got_tree_entry *te = pe->data;
1788 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
1790 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1791 err = got_error_from_errno("asprintf");
1794 err = (*progress_cb)(progress_arg, path);
1800 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1804 got_pathlist_free(&paths);
1808 const struct got_error *
1809 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1810 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1811 struct got_repository *repo, got_repo_import_cb progress_cb,
1814 const struct got_error *err;
1815 struct got_object_id *new_tree_id;
1817 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1818 progress_cb, progress_arg);
1822 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1823 author, time(NULL), author, time(NULL), logmsg, repo);
1828 const struct got_error *
1829 got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size,
1830 struct got_repository *repo)
1832 const struct got_error *err = NULL;
1833 char *path_objects = NULL, *path = NULL;
1835 struct got_object_id id;
1841 path_objects = got_repo_get_path_objects(repo);
1842 if (path_objects == NULL)
1843 return got_error_from_errno("got_repo_get_path_objects");
1845 for (i = 0; i <= 0xff; i++) {
1846 struct dirent *dent;
1848 if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) {
1849 err = got_error_from_errno("asprintf");
1853 dir = opendir(path);
1855 if (errno == ENOENT) {
1859 err = got_error_from_errno2("opendir", path);
1863 while ((dent = readdir(dir)) != NULL) {
1868 if (strcmp(dent->d_name, ".") == 0 ||
1869 strcmp(dent->d_name, "..") == 0)
1872 if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) {
1873 err = got_error_from_errno("asprintf");
1877 if (!got_parse_sha1_digest(id.sha1, id_str)) {
1883 err = got_object_open_loose_fd(&fd, &id, repo);
1887 if (fstat(fd, &sb) == -1) {
1888 err = got_error_from_errno("fstat");
1893 (*ondisk_size) += sb.st_size;
1895 if (close(fd) == -1) {
1896 err = got_error_from_errno("close");
1901 if (closedir(dir) != 0) {
1902 err = got_error_from_errno("closedir");
1911 if (dir && closedir(dir) != 0 && err == NULL)
1912 err = got_error_from_errno("closedir");
1923 const struct got_error *
1924 got_repo_get_packfile_info(int *npackfiles, int *nobjects,
1925 off_t *total_packsize, struct got_repository *repo)
1927 const struct got_error *err = NULL;
1928 DIR *packdir = NULL;
1929 struct dirent *dent;
1930 struct got_packidx *packidx = NULL;
1932 char *path_packfile;
1938 *total_packsize = 0;
1940 packdir_fd = openat(got_repo_get_fd(repo),
1941 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1942 if (packdir_fd == -1) {
1943 return got_error_from_errno_fmt("openat: %s/%s",
1944 got_repo_get_path_git_dir(repo),
1945 GOT_OBJECTS_PACK_DIR);
1948 packdir = fdopendir(packdir_fd);
1949 if (packdir == NULL) {
1950 err = got_error_from_errno("fdopendir");
1954 while ((dent = readdir(packdir)) != NULL) {
1955 if (!got_repo_is_packidx_filename(dent->d_name, dent->d_namlen))
1958 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1959 dent->d_name) == -1) {
1960 err = got_error_from_errno("asprintf");
1964 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1970 if (fstat(packidx->fd, &sb) == -1)
1972 *total_packsize += sb.st_size;
1974 err = got_packidx_get_packfile_path(&path_packfile,
1975 packidx->path_packidx);
1979 if (fstatat(got_repo_get_fd(repo), path_packfile, &sb,
1981 free(path_packfile);
1984 free(path_packfile);
1985 *total_packsize += sb.st_size;
1987 *nobjects += be32toh(packidx->hdr.fanout_table[0xff]);
1991 got_packidx_close(packidx);
1996 got_packidx_close(packidx);
1997 if (packdir && closedir(packdir) != 0 && err == NULL)
1998 err = got_error_from_errno("closedir");
2002 *total_packsize = 0;