2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
18 #include <sys/queue.h>
20 #include <sys/socket.h>
23 #include <sys/syslimits.h>
42 #include "got_error.h"
43 #include "got_reference.h"
44 #include "got_repository.h"
46 #include "got_cancel.h"
47 #include "got_worktree.h"
48 #include "got_object.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_object.h"
53 #include "got_lib_object_parse.h"
54 #include "got_lib_object_create.h"
55 #include "got_lib_pack.h"
56 #include "got_lib_privsep.h"
57 #include "got_lib_worktree.h"
58 #include "got_lib_sha1.h"
59 #include "got_lib_object_cache.h"
60 #include "got_lib_repository.h"
63 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
66 #define GOT_GIT_DIR ".git"
68 /* Mandatory files and directories inside the git directory. */
69 #define GOT_OBJECTS_DIR "objects"
70 #define GOT_REFS_DIR "refs"
71 #define GOT_HEAD_FILE "HEAD"
72 #define GOT_GITCONFIG "config"
74 /* Other files and directories inside the git directory. */
75 #define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
76 #define GOT_ORIG_HEAD_FILE "ORIG_HEAD"
77 #define GOT_OBJECTS_PACK_DIR "objects/pack"
78 #define GOT_PACKED_REFS_FILE "packed-refs"
81 got_repo_get_path(struct got_repository *repo)
87 got_repo_get_path_git_dir(struct got_repository *repo)
89 return repo->path_git_dir;
93 got_repo_get_gitconfig_author_name(struct got_repository *repo)
95 return repo->gitconfig_author_name;
99 got_repo_get_gitconfig_author_email(struct got_repository *repo)
101 return repo->gitconfig_author_email;
105 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
107 return repo->global_gitconfig_author_name;
111 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
113 return repo->global_gitconfig_author_email;
117 got_repo_get_gitconfig_owner(struct got_repository *repo)
119 return repo->gitconfig_owner;
123 got_repo_is_bare(struct got_repository *repo)
125 return (strcmp(repo->path, repo->path_git_dir) == 0);
129 get_path_git_child(struct got_repository *repo, const char *basename)
133 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
141 got_repo_get_path_objects(struct got_repository *repo)
143 return get_path_git_child(repo, GOT_OBJECTS_DIR);
147 got_repo_get_path_objects_pack(struct got_repository *repo)
149 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
153 got_repo_get_path_refs(struct got_repository *repo)
155 return get_path_git_child(repo, GOT_REFS_DIR);
159 got_repo_get_path_packed_refs(struct got_repository *repo)
161 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
165 get_path_head(struct got_repository *repo)
167 return get_path_git_child(repo, GOT_HEAD_FILE);
170 static const struct got_error *
171 get_path_gitconfig(char **p, struct got_repository *repo)
173 *p = get_path_git_child(repo, GOT_GITCONFIG);
175 return got_error_from_errno("asprintf");
180 got_repo_get_gitconfig_remotes(int *nremotes, struct got_remote_repo **remotes,
181 struct got_repository *repo)
183 *nremotes = repo->ngitconfig_remotes;
184 *remotes = repo->gitconfig_remotes;
188 is_git_repo(struct got_repository *repo)
190 const char *path_git = got_repo_get_path_git_dir(repo);
191 char *path_objects = got_repo_get_path_objects(repo);
192 char *path_refs = got_repo_get_path_refs(repo);
193 char *path_head = get_path_head(repo);
196 struct got_reference *head_ref;
198 if (lstat(path_git, &sb) == -1)
200 if (!S_ISDIR(sb.st_mode))
203 if (lstat(path_objects, &sb) == -1)
205 if (!S_ISDIR(sb.st_mode))
208 if (lstat(path_refs, &sb) == -1)
210 if (!S_ISDIR(sb.st_mode))
213 if (lstat(path_head, &sb) == -1)
215 if (!S_ISREG(sb.st_mode))
218 /* Check if the HEAD reference can be opened. */
219 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
221 got_ref_close(head_ref);
232 const struct got_error *
233 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
234 struct got_object *obj)
236 #ifndef GOT_NO_OBJ_CACHE
237 const struct got_error *err = NULL;
238 err = got_object_cache_add(&repo->objcache, id, obj);
240 if (err->code == GOT_ERR_OBJ_EXISTS ||
241 err->code == GOT_ERR_OBJ_TOO_LARGE)
251 got_repo_get_cached_object(struct got_repository *repo,
252 struct got_object_id *id)
254 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
257 const struct got_error *
258 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
259 struct got_tree_object *tree)
261 #ifndef GOT_NO_OBJ_CACHE
262 const struct got_error *err = NULL;
263 err = got_object_cache_add(&repo->treecache, id, tree);
265 if (err->code == GOT_ERR_OBJ_EXISTS ||
266 err->code == GOT_ERR_OBJ_TOO_LARGE)
275 struct got_tree_object *
276 got_repo_get_cached_tree(struct got_repository *repo,
277 struct got_object_id *id)
279 return (struct got_tree_object *)got_object_cache_get(
280 &repo->treecache, id);
283 const struct got_error *
284 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
285 struct got_commit_object *commit)
287 #ifndef GOT_NO_OBJ_CACHE
288 const struct got_error *err = NULL;
289 err = got_object_cache_add(&repo->commitcache, id, commit);
291 if (err->code == GOT_ERR_OBJ_EXISTS ||
292 err->code == GOT_ERR_OBJ_TOO_LARGE)
301 struct got_commit_object *
302 got_repo_get_cached_commit(struct got_repository *repo,
303 struct got_object_id *id)
305 return (struct got_commit_object *)got_object_cache_get(
306 &repo->commitcache, id);
309 const struct got_error *
310 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
311 struct got_tag_object *tag)
313 #ifndef GOT_NO_OBJ_CACHE
314 const struct got_error *err = NULL;
315 err = got_object_cache_add(&repo->tagcache, id, tag);
317 if (err->code == GOT_ERR_OBJ_EXISTS ||
318 err->code == GOT_ERR_OBJ_TOO_LARGE)
327 struct got_tag_object *
328 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
330 return (struct got_tag_object *)got_object_cache_get(
331 &repo->tagcache, id);
334 const struct got_error *
335 open_repo(struct got_repository *repo, const char *path)
337 const struct got_error *err = NULL;
339 /* bare git repository? */
340 repo->path_git_dir = strdup(path);
341 if (repo->path_git_dir == NULL)
342 return got_error_from_errno("strdup");
343 if (is_git_repo(repo)) {
344 repo->path = strdup(repo->path_git_dir);
345 if (repo->path == NULL) {
346 err = got_error_from_errno("strdup");
352 /* git repository with working tree? */
353 free(repo->path_git_dir);
354 repo->path_git_dir = NULL;
355 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
356 err = got_error_from_errno("asprintf");
359 if (is_git_repo(repo)) {
360 repo->path = strdup(path);
361 if (repo->path == NULL) {
362 err = got_error_from_errno("strdup");
368 err = got_error(GOT_ERR_NOT_GIT_REPO);
373 free(repo->path_git_dir);
374 repo->path_git_dir = NULL;
379 static const struct got_error *
380 parse_gitconfig_file(int *gitconfig_repository_format_version,
381 char **gitconfig_author_name, char **gitconfig_author_email,
382 struct got_remote_repo **remotes, int *nremotes,
383 char **gitconfig_owner,
384 const char *gitconfig_path)
386 const struct got_error *err = NULL, *child_err = NULL;
388 int imsg_fds[2] = { -1, -1 };
390 struct imsgbuf *ibuf;
392 *gitconfig_repository_format_version = 0;
393 *gitconfig_author_name = NULL;
394 *gitconfig_author_email = NULL;
396 fd = open(gitconfig_path, O_RDONLY);
400 return got_error_from_errno2("open", gitconfig_path);
403 ibuf = calloc(1, sizeof(*ibuf));
405 err = got_error_from_errno("calloc");
409 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
410 err = got_error_from_errno("socketpair");
416 err = got_error_from_errno("fork");
418 } else if (pid == 0) {
419 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
424 if (close(imsg_fds[1]) == -1) {
425 err = got_error_from_errno("close");
429 imsg_init(ibuf, imsg_fds[0]);
431 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
436 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
440 err = got_privsep_recv_gitconfig_int(
441 gitconfig_repository_format_version, ibuf);
445 err = got_privsep_send_gitconfig_author_name_req(ibuf);
449 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
453 err = got_privsep_send_gitconfig_author_email_req(ibuf);
457 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
461 if (remotes && nremotes) {
462 err = got_privsep_send_gitconfig_remotes_req(ibuf);
466 err = got_privsep_recv_gitconfig_remotes(remotes,
472 if (gitconfig_owner) {
473 err = got_privsep_send_gitconfig_owner_req(ibuf);
476 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
482 err = got_privsep_send_stop(imsg_fds[0]);
483 child_err = got_privsep_wait_for_child(pid);
484 if (child_err && err == NULL)
487 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
488 err = got_error_from_errno("close");
489 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
490 err = got_error_from_errno("close");
491 if (fd != -1 && close(fd) == -1 && err == NULL)
492 err = got_error_from_errno2("close", gitconfig_path);
497 static const struct got_error *
498 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
500 const struct got_error *err = NULL;
501 char *repo_gitconfig_path = NULL;
503 if (global_gitconfig_path) {
504 /* Read settings from ~/.gitconfig. */
505 int dummy_repo_version;
506 err = parse_gitconfig_file(&dummy_repo_version,
507 &repo->global_gitconfig_author_name,
508 &repo->global_gitconfig_author_email,
509 NULL, NULL, NULL, global_gitconfig_path);
514 /* Read repository's .git/config file. */
515 err = get_path_gitconfig(&repo_gitconfig_path, repo);
519 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
520 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
521 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
522 &repo->gitconfig_owner, repo_gitconfig_path);
526 free(repo_gitconfig_path);
530 const struct got_error *
531 got_repo_open(struct got_repository **repop, const char *path,
532 const char *global_gitconfig_path)
534 struct got_repository *repo = NULL;
535 const struct got_error *err = NULL;
537 int i, tried_root = 0;
541 if (got_path_is_absolute(path))
542 abspath = strdup(path);
544 abspath = got_path_get_absolute(path);
546 return got_error(GOT_ERR_BAD_PATH);
548 repo = calloc(1, sizeof(*repo));
550 err = got_error_from_errno("calloc");
554 for (i = 0; i < nitems(repo->privsep_children); i++) {
555 memset(&repo->privsep_children[i], 0,
556 sizeof(repo->privsep_children[0]));
557 repo->privsep_children[i].imsg_fd = -1;
560 err = got_object_cache_init(&repo->objcache,
561 GOT_OBJECT_CACHE_TYPE_OBJ);
564 err = got_object_cache_init(&repo->treecache,
565 GOT_OBJECT_CACHE_TYPE_TREE);
568 err = got_object_cache_init(&repo->commitcache,
569 GOT_OBJECT_CACHE_TYPE_COMMIT);
572 err = got_object_cache_init(&repo->tagcache,
573 GOT_OBJECT_CACHE_TYPE_TAG);
577 path = realpath(abspath, NULL);
579 err = got_error_from_errno2("realpath", abspath);
584 err = open_repo(repo, path);
587 if (err->code != GOT_ERR_NOT_GIT_REPO)
589 if (path[0] == '/' && path[1] == '\0') {
591 err = got_error(GOT_ERR_NOT_GIT_REPO);
596 path = dirname(path);
598 err = got_error_from_errno2("dirname", path);
603 err = read_gitconfig(repo, global_gitconfig_path);
606 if (repo->gitconfig_repository_format_version != 0)
607 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
610 got_repo_close(repo);
617 const struct got_error *
618 got_repo_close(struct got_repository *repo)
620 const struct got_error *err = NULL, *child_err;
623 for (i = 0; i < nitems(repo->packidx_cache); i++) {
624 if (repo->packidx_cache[i] == NULL)
626 got_packidx_close(repo->packidx_cache[i]);
629 for (i = 0; i < nitems(repo->packs); i++) {
630 if (repo->packs[i].path_packfile == NULL)
632 got_pack_close(&repo->packs[i]);
636 free(repo->path_git_dir);
638 got_object_cache_close(&repo->objcache);
639 got_object_cache_close(&repo->treecache);
640 got_object_cache_close(&repo->commitcache);
641 got_object_cache_close(&repo->tagcache);
643 for (i = 0; i < nitems(repo->privsep_children); i++) {
644 if (repo->privsep_children[i].imsg_fd == -1)
646 imsg_clear(repo->privsep_children[i].ibuf);
647 free(repo->privsep_children[i].ibuf);
648 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
649 child_err = got_privsep_wait_for_child(
650 repo->privsep_children[i].pid);
651 if (child_err && err == NULL)
653 if (close(repo->privsep_children[i].imsg_fd) != 0 &&
655 err = got_error_from_errno("close");
658 free(repo->gitconfig_author_name);
659 free(repo->gitconfig_author_email);
660 for (i = 0; i < repo->ngitconfig_remotes; i++) {
661 free(repo->gitconfig_remotes[i].name);
662 free(repo->gitconfig_remotes[i].url);
664 free(repo->gitconfig_remotes);
670 const struct got_error *
671 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
672 const char *input_path, int check_disk)
674 const struct got_error *err = NULL;
675 const char *repo_abspath = NULL;
677 char *canonpath, *path = NULL;
679 *in_repo_path = NULL;
681 canonpath = strdup(input_path);
682 if (canonpath == NULL) {
683 err = got_error_from_errno("strdup");
686 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
690 repo_abspath = got_repo_get_path(repo);
692 if (!check_disk || canonpath[0] == '\0') {
693 path = strdup(canonpath);
695 err = got_error_from_errno("strdup");
699 path = realpath(canonpath, NULL);
701 if (errno != ENOENT) {
702 err = got_error_from_errno2("realpath",
707 * Path is not on disk.
708 * Assume it is already relative to repository root.
710 path = strdup(canonpath);
712 err = got_error_from_errno("strdup");
717 repolen = strlen(repo_abspath);
721 if (strcmp(path, repo_abspath) == 0) {
725 err = got_error_from_errno("strdup");
728 } else if (len > repolen &&
729 got_path_is_child(path, repo_abspath, repolen)) {
730 /* Matched an on-disk path inside repository. */
731 if (got_repo_is_bare(repo)) {
733 * Matched an on-disk path inside repository
734 * database. Treat as repository-relative.
738 /* Strip common prefix with repository path. */
739 err = got_path_skip_common_ancestor(&child,
748 * Matched unrelated on-disk path.
749 * Treat it as repository-relative.
754 /* Make in-repository path absolute */
755 if (path[0] != '/') {
757 if (asprintf(&abspath, "/%s", path) == -1) {
758 err = got_error_from_errno("asprintf");
770 *in_repo_path = path;
774 static const struct got_error *
775 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
776 const char *path_packidx)
778 const struct got_error *err = NULL;
781 for (i = 0; i < nitems(repo->packidx_cache); i++) {
782 if (repo->packidx_cache[i] == NULL)
784 if (strcmp(repo->packidx_cache[i]->path_packidx,
785 path_packidx) == 0) {
786 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
789 if (i == nitems(repo->packidx_cache)) {
790 err = got_packidx_close(repo->packidx_cache[i - 1]);
796 * Insert the new pack index at the front so it will
797 * be searched first in the future.
799 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
800 sizeof(repo->packidx_cache) -
801 sizeof(repo->packidx_cache[0]));
802 repo->packidx_cache[0] = packidx;
808 is_packidx_filename(const char *name, size_t len)
810 if (len != GOT_PACKIDX_NAMELEN)
813 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
816 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
817 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
823 const struct got_error *
824 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
825 struct got_repository *repo, struct got_object_id *id)
827 const struct got_error *err;
834 /* Search pack index cache. */
835 for (i = 0; i < nitems(repo->packidx_cache); i++) {
836 if (repo->packidx_cache[i] == NULL)
838 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
840 *packidx = repo->packidx_cache[i];
842 * Move this cache entry to the front. Repeatedly
843 * searching a wrong pack index can be expensive.
846 struct got_packidx *p;
847 p = repo->packidx_cache[0];
848 repo->packidx_cache[0] = *packidx;
849 repo->packidx_cache[i] = p;
854 /* No luck. Search the filesystem. */
856 path_packdir = got_repo_get_path_objects_pack(repo);
857 if (path_packdir == NULL)
858 return got_error_from_errno("got_repo_get_path_objects_pack");
860 packdir = opendir(path_packdir);
861 if (packdir == NULL) {
863 err = got_error_no_obj(id);
865 err = got_error_from_errno2("opendir", path_packdir);
869 while ((dent = readdir(packdir)) != NULL) {
872 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
875 if (asprintf(&path_packidx, "%s/%s", path_packdir,
876 dent->d_name) == -1) {
877 err = got_error_from_errno("asprintf");
881 for (i = 0; i < nitems(repo->packidx_cache); i++) {
882 if (repo->packidx_cache[i] == NULL)
884 if (strcmp(repo->packidx_cache[i]->path_packidx,
885 path_packidx) == 0) {
892 continue; /* already searched */
895 err = got_packidx_open(packidx, path_packidx, 0);
901 err = cache_packidx(repo, *packidx, path_packidx);
906 *idx = got_packidx_get_object_idx(*packidx, id);
908 err = NULL; /* found the object */
913 err = got_error_no_obj(id);
916 if (packdir && closedir(packdir) != 0 && err == NULL)
917 err = got_error_from_errno("closedir");
921 static const struct got_error *
922 read_packfile_hdr(int fd, struct got_packidx *packidx)
924 const struct got_error *err = NULL;
925 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
926 struct got_packfile_hdr hdr;
929 n = read(fd, &hdr, sizeof(hdr));
931 return got_error_from_errno("read");
932 if (n != sizeof(hdr))
933 return got_error(GOT_ERR_BAD_PACKFILE);
935 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
936 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
937 betoh32(hdr.nobjects) != totobj)
938 err = got_error(GOT_ERR_BAD_PACKFILE);
943 static const struct got_error *
944 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
946 const struct got_error *err = NULL;
948 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
950 return got_error_from_errno2("open", path_packfile);
953 err = read_packfile_hdr(*fd, packidx);
963 const struct got_error *
964 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
965 const char *path_packfile, struct got_packidx *packidx)
967 const struct got_error *err = NULL;
968 struct got_pack *pack = NULL;
975 for (i = 0; i < nitems(repo->packs); i++) {
976 pack = &repo->packs[i];
977 if (pack->path_packfile == NULL)
979 if (strcmp(pack->path_packfile, path_packfile) == 0)
980 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
983 if (i == nitems(repo->packs) - 1) {
984 err = got_pack_close(&repo->packs[i - 1]);
987 memmove(&repo->packs[1], &repo->packs[0],
988 sizeof(repo->packs) - sizeof(repo->packs[0]));
992 pack = &repo->packs[i];
994 pack->path_packfile = strdup(path_packfile);
995 if (pack->path_packfile == NULL) {
996 err = got_error_from_errno("strdup");
1000 err = open_packfile(&pack->fd, path_packfile, packidx);
1004 if (fstat(pack->fd, &sb) != 0) {
1005 err = got_error_from_errno("fstat");
1008 pack->filesize = sb.st_size;
1010 pack->privsep_child = NULL;
1012 #ifndef GOT_PACK_NO_MMAP
1013 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1015 if (pack->map == MAP_FAILED) {
1016 if (errno != ENOMEM) {
1017 err = got_error_from_errno("mmap");
1020 pack->map = NULL; /* fall back to read(2) */
1026 free(pack->path_packfile);
1027 memset(pack, 0, sizeof(*pack));
1035 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1037 struct got_pack *pack = NULL;
1040 for (i = 0; i < nitems(repo->packs); i++) {
1041 pack = &repo->packs[i];
1042 if (pack->path_packfile == NULL)
1044 if (strcmp(pack->path_packfile, path_packfile) == 0)
1051 const struct got_error *
1052 got_repo_init(const char *repo_path)
1054 const struct got_error *err = NULL;
1055 const char *dirnames[] = {
1057 GOT_OBJECTS_PACK_DIR,
1060 const char *description_str = "Unnamed repository; "
1061 "edit this file 'description' to name the repository.";
1062 const char *headref_str = "ref: refs/heads/main";
1063 const char *gitconfig_str = "[core]\n"
1064 "\trepositoryformatversion = 0\n"
1065 "\tfilemode = true\n"
1070 if (!got_path_dir_is_empty(repo_path))
1071 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1073 for (i = 0; i < nitems(dirnames); i++) {
1074 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1075 return got_error_from_errno("asprintf");
1077 err = got_path_mkdir(path);
1083 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1084 return got_error_from_errno("asprintf");
1085 err = got_path_create_file(path, description_str);
1090 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1091 return got_error_from_errno("asprintf");
1092 err = got_path_create_file(path, headref_str);
1097 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1098 return got_error_from_errno("asprintf");
1099 err = got_path_create_file(path, gitconfig_str);
1107 static const struct got_error *
1108 match_packed_object(struct got_object_id **unique_id,
1109 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1111 const struct got_error *err = NULL;
1114 struct dirent *dent;
1116 struct got_object_id_queue matched_ids;
1118 SIMPLEQ_INIT(&matched_ids);
1120 path_packdir = got_repo_get_path_objects_pack(repo);
1121 if (path_packdir == NULL)
1122 return got_error_from_errno("got_repo_get_path_objects_pack");
1124 packdir = opendir(path_packdir);
1125 if (packdir == NULL) {
1126 if (errno != ENOENT)
1127 err = got_error_from_errno2("opendir", path_packdir);
1131 while ((dent = readdir(packdir)) != NULL) {
1132 struct got_packidx *packidx;
1133 struct got_object_qid *qid;
1136 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1139 if (asprintf(&path_packidx, "%s/%s", path_packdir,
1140 dent->d_name) == -1) {
1141 err = got_error_from_errno("asprintf");
1145 err = got_packidx_open(&packidx, path_packidx, 0);
1150 err = got_packidx_match_id_str_prefix(&matched_ids,
1151 packidx, id_str_prefix);
1153 got_packidx_close(packidx);
1156 err = got_packidx_close(packidx);
1160 SIMPLEQ_FOREACH(qid, &matched_ids, entry) {
1161 if (obj_type != GOT_OBJ_TYPE_ANY) {
1163 err = got_object_get_type(&matched_type, repo,
1167 if (matched_type != obj_type)
1170 if (*unique_id == NULL) {
1171 *unique_id = got_object_id_dup(qid->id);
1172 if (*unique_id == NULL) {
1173 err = got_error_from_errno("malloc");
1177 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1178 continue; /* packed multiple times */
1179 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1185 got_object_id_queue_free(&matched_ids);
1187 if (packdir && closedir(packdir) != 0 && err == NULL)
1188 err = got_error_from_errno("closedir");
1196 static const struct got_error *
1197 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1198 const char *object_dir, const char *id_str_prefix, int obj_type,
1199 struct got_repository *repo)
1201 const struct got_error *err = NULL;
1204 struct dirent *dent;
1205 struct got_object_id id;
1207 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1208 err = got_error_from_errno("asprintf");
1212 dir = opendir(path);
1214 if (errno == ENOENT) {
1218 err = got_error_from_errno2("opendir", path);
1221 while ((dent = readdir(dir)) != NULL) {
1225 if (strcmp(dent->d_name, ".") == 0 ||
1226 strcmp(dent->d_name, "..") == 0)
1229 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1230 err = got_error_from_errno("asprintf");
1234 if (!got_parse_sha1_digest(id.sha1, id_str))
1238 * Directory entries do not necessarily appear in
1239 * sorted order, so we must iterate over all of them.
1241 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1247 if (*unique_id == NULL) {
1248 if (obj_type != GOT_OBJ_TYPE_ANY) {
1250 err = got_object_get_type(&matched_type, repo,
1254 if (matched_type != obj_type)
1257 *unique_id = got_object_id_dup(&id);
1258 if (*unique_id == NULL) {
1259 err = got_error_from_errno("got_object_id_dup");
1264 if (got_object_id_cmp(*unique_id, &id) == 0)
1265 continue; /* both packed and loose */
1266 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1272 if (dir && closedir(dir) != 0 && err == NULL)
1273 err = got_error_from_errno("closedir");
1282 const struct got_error *
1283 got_repo_match_object_id_prefix(struct got_object_id **id,
1284 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1286 const struct got_error *err = NULL;
1287 char *path_objects = got_repo_get_path_objects(repo);
1288 char *object_dir = NULL;
1294 for (i = 0; i < strlen(id_str_prefix); i++) {
1295 if (isxdigit((unsigned char)id_str_prefix[i]))
1297 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1300 len = strlen(id_str_prefix);
1302 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1305 object_dir = strndup(id_str_prefix, 2);
1306 if (object_dir == NULL) {
1307 err = got_error_from_errno("strdup");
1310 err = match_loose_object(id, path_objects, object_dir,
1311 id_str_prefix, obj_type, repo);
1312 } else if (len == 1) {
1314 for (i = 0; i < 0xf; i++) {
1315 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1317 err = got_error_from_errno("asprintf");
1320 err = match_packed_object(id, repo, object_dir,
1324 err = match_loose_object(id, path_objects, object_dir,
1325 id_str_prefix, obj_type, repo);
1330 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1338 } else if (*id == NULL)
1339 err = got_error(GOT_ERR_NO_OBJ);
1344 const struct got_error *
1345 got_repo_match_object_id(struct got_object_id **id, char **label,
1346 const char *id_str, int obj_type, int resolve_tags,
1347 struct got_repository *repo)
1349 const struct got_error *err;
1350 struct got_tag_object *tag;
1351 struct got_reference *ref = NULL;
1358 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
1361 *id = got_object_id_dup(
1362 got_object_tag_get_object_id(tag));
1364 err = got_error_from_errno("got_object_id_dup");
1365 else if (label && asprintf(label, "refs/tags/%s",
1366 got_object_tag_get_name(tag)) == -1) {
1367 err = got_error_from_errno("asprintf");
1371 got_object_tag_close(tag);
1373 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1374 err->code != GOT_ERR_NO_OBJ)
1378 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1380 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1382 err = got_ref_open(&ref, repo, id_str, 0);
1386 *label = strdup(got_ref_get_name(ref));
1387 if (*label == NULL) {
1388 err = got_error_from_errno("strdup");
1392 err = got_ref_resolve(id, repo, ref);
1394 err = got_object_id_str(label, *id);
1395 if (*label == NULL) {
1396 err = got_error_from_errno("strdup");
1406 const struct got_error *
1407 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1408 int obj_type, struct got_repository *repo)
1410 const struct got_error *err;
1411 struct got_reflist_head refs;
1412 struct got_reflist_entry *re;
1413 struct got_object_id *tag_id;
1415 SIMPLEQ_INIT(&refs);
1418 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_by_name, NULL);
1422 SIMPLEQ_FOREACH(re, &refs, entry) {
1423 const char *refname;
1424 refname = got_ref_get_name(re->ref);
1425 if (got_ref_is_symbolic(re->ref))
1427 refname += strlen("refs/tags/");
1428 if (strcmp(refname, name) != 0)
1430 err = got_ref_resolve(&tag_id, repo, re->ref);
1433 err = got_object_open_as_tag(tag, repo, tag_id);
1437 if (obj_type == GOT_OBJ_TYPE_ANY ||
1438 got_object_tag_get_object_type(*tag) == obj_type)
1440 got_object_tag_close(*tag);
1444 got_ref_list_free(&refs);
1445 if (err == NULL && *tag == NULL)
1446 err = got_error(GOT_ERR_NO_OBJ);
1450 static const struct got_error *
1451 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1452 const char *name, mode_t mode, struct got_object_id *blob_id)
1454 const struct got_error *err = NULL;
1458 *new_te = calloc(1, sizeof(**new_te));
1459 if (*new_te == NULL)
1460 return got_error_from_errno("calloc");
1462 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1463 sizeof((*new_te)->name)) {
1464 err = got_error(GOT_ERR_NO_SPACE);
1468 (*new_te)->mode = S_IFREG | (mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
1469 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1471 if (err && *new_te) {
1478 static const struct got_error *
1479 import_file(struct got_tree_entry **new_te, struct dirent *de,
1480 const char *path, struct got_repository *repo)
1482 const struct got_error *err;
1483 struct got_object_id *blob_id = NULL;
1487 if (asprintf(&filepath, "%s%s%s", path,
1488 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1489 return got_error_from_errno("asprintf");
1491 if (lstat(filepath, &sb) != 0) {
1492 err = got_error_from_errno2("lstat", path);
1496 err = got_object_blob_create(&blob_id, filepath, repo);
1500 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1509 static const struct got_error *
1510 insert_tree_entry(struct got_tree_entry *new_te,
1511 struct got_pathlist_head *paths)
1513 const struct got_error *err = NULL;
1514 struct got_pathlist_entry *new_pe;
1516 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1520 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1524 static const struct got_error *write_tree(struct got_object_id **,
1525 const char *, struct got_pathlist_head *, struct got_repository *,
1526 got_repo_import_cb progress_cb, void *progress_arg);
1528 static const struct got_error *
1529 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1530 const char *path, struct got_pathlist_head *ignores,
1531 struct got_repository *repo,
1532 got_repo_import_cb progress_cb, void *progress_arg)
1534 const struct got_error *err;
1535 struct got_object_id *id = NULL;
1538 if (asprintf(&subdirpath, "%s%s%s", path,
1539 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1540 return got_error_from_errno("asprintf");
1542 (*new_te) = calloc(1, sizeof(**new_te));
1543 if (*new_te == NULL)
1544 return got_error_from_errno("calloc");
1545 (*new_te)->mode = S_IFDIR;
1546 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1547 sizeof((*new_te)->name)) {
1548 err = got_error(GOT_ERR_NO_SPACE);
1551 err = write_tree(&id, subdirpath, ignores, repo,
1552 progress_cb, progress_arg);
1555 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1567 static const struct got_error *
1568 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1569 struct got_pathlist_head *ignores, struct got_repository *repo,
1570 got_repo_import_cb progress_cb, void *progress_arg)
1572 const struct got_error *err = NULL;
1576 struct got_tree_entry *new_te = NULL;
1577 struct got_pathlist_head paths;
1578 struct got_pathlist_entry *pe;
1580 *new_tree_id = NULL;
1584 dir = opendir(path_dir);
1586 err = got_error_from_errno2("opendir", path_dir);
1591 while ((de = readdir(dir)) != NULL) {
1594 if (strcmp(de->d_name, ".") == 0 ||
1595 strcmp(de->d_name, "..") == 0)
1598 TAILQ_FOREACH(pe, ignores, entry) {
1599 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1606 if (de->d_type == DT_DIR) {
1607 err = import_subdir(&new_te, de, path_dir,
1608 ignores, repo, progress_cb, progress_arg);
1610 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1615 } else if (de->d_type == DT_REG) {
1616 err = import_file(&new_te, de, path_dir, repo);
1622 err = insert_tree_entry(new_te, &paths);
1628 if (TAILQ_EMPTY(&paths)) {
1629 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1633 TAILQ_FOREACH(pe, &paths, entry) {
1634 struct got_tree_entry *te = pe->data;
1636 if (!S_ISREG(te->mode))
1638 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1639 err = got_error_from_errno("asprintf");
1642 err = (*progress_cb)(progress_arg, path);
1648 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1652 got_pathlist_free(&paths);
1656 const struct got_error *
1657 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1658 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1659 struct got_repository *repo, got_repo_import_cb progress_cb,
1662 const struct got_error *err;
1663 struct got_object_id *new_tree_id;
1665 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1666 progress_cb, progress_arg);
1670 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1671 author, time(NULL), author, time(NULL), logmsg, repo);