2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
18 #include <sys/queue.h>
20 #include <sys/socket.h>
23 #include <sys/syslimits.h>
44 #include "got_error.h"
45 #include "got_reference.h"
46 #include "got_repository.h"
48 #include "got_cancel.h"
49 #include "got_worktree.h"
50 #include "got_object.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_inflate.h"
54 #include "got_lib_object.h"
55 #include "got_lib_object_parse.h"
56 #include "got_lib_object_create.h"
57 #include "got_lib_pack.h"
58 #include "got_lib_privsep.h"
59 #include "got_lib_worktree.h"
60 #include "got_lib_sha1.h"
61 #include "got_lib_object_cache.h"
62 #include "got_lib_repository.h"
65 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
69 got_repo_get_path(struct got_repository *repo)
75 got_repo_get_path_git_dir(struct got_repository *repo)
77 return repo->path_git_dir;
81 got_repo_get_gitconfig_author_name(struct got_repository *repo)
83 return repo->gitconfig_author_name;
87 got_repo_get_gitconfig_author_email(struct got_repository *repo)
89 return repo->gitconfig_author_email;
93 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
95 return repo->global_gitconfig_author_name;
99 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
101 return repo->global_gitconfig_author_email;
105 got_repo_get_gitconfig_owner(struct got_repository *repo)
107 return repo->gitconfig_owner;
111 got_repo_is_bare(struct got_repository *repo)
113 return (strcmp(repo->path, repo->path_git_dir) == 0);
117 get_path_git_child(struct got_repository *repo, const char *basename)
121 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
129 got_repo_get_path_objects(struct got_repository *repo)
131 return get_path_git_child(repo, GOT_OBJECTS_DIR);
135 got_repo_get_path_objects_pack(struct got_repository *repo)
137 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
141 got_repo_get_path_refs(struct got_repository *repo)
143 return get_path_git_child(repo, GOT_REFS_DIR);
147 got_repo_get_path_packed_refs(struct got_repository *repo)
149 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
153 get_path_head(struct got_repository *repo)
155 return get_path_git_child(repo, GOT_HEAD_FILE);
159 got_repo_get_path_gitconfig(struct got_repository *repo)
161 return get_path_git_child(repo, GOT_GITCONFIG);
165 got_repo_get_path_gotconfig(struct got_repository *repo)
167 return get_path_git_child(repo, GOT_GOTCONFIG);
171 got_repo_get_gitconfig_remotes(int *nremotes, struct got_remote_repo **remotes,
172 struct got_repository *repo)
174 *nremotes = repo->ngitconfig_remotes;
175 *remotes = repo->gitconfig_remotes;
179 got_repo_get_gotconfig_author(struct got_repository *repo)
181 return repo->gotconfig_author;
185 got_repo_get_gotconfig_remotes(int *nremotes, struct got_remote_repo **remotes,
186 struct got_repository *repo)
188 *nremotes = repo->ngotconfig_remotes;
189 *remotes = repo->gotconfig_remotes;
193 is_git_repo(struct got_repository *repo)
195 const char *path_git = got_repo_get_path_git_dir(repo);
196 char *path_objects = got_repo_get_path_objects(repo);
197 char *path_refs = got_repo_get_path_refs(repo);
198 char *path_head = get_path_head(repo);
201 struct got_reference *head_ref;
203 if (lstat(path_git, &sb) == -1)
205 if (!S_ISDIR(sb.st_mode))
208 if (lstat(path_objects, &sb) == -1)
210 if (!S_ISDIR(sb.st_mode))
213 if (lstat(path_refs, &sb) == -1)
215 if (!S_ISDIR(sb.st_mode))
218 if (lstat(path_head, &sb) == -1)
220 if (!S_ISREG(sb.st_mode))
223 /* Check if the HEAD reference can be opened. */
224 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
226 got_ref_close(head_ref);
237 const struct got_error *
238 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
239 struct got_object *obj)
241 #ifndef GOT_NO_OBJ_CACHE
242 const struct got_error *err = NULL;
243 err = got_object_cache_add(&repo->objcache, id, obj);
245 if (err->code == GOT_ERR_OBJ_EXISTS ||
246 err->code == GOT_ERR_OBJ_TOO_LARGE)
256 got_repo_get_cached_object(struct got_repository *repo,
257 struct got_object_id *id)
259 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
262 const struct got_error *
263 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
264 struct got_tree_object *tree)
266 #ifndef GOT_NO_OBJ_CACHE
267 const struct got_error *err = NULL;
268 err = got_object_cache_add(&repo->treecache, id, tree);
270 if (err->code == GOT_ERR_OBJ_EXISTS ||
271 err->code == GOT_ERR_OBJ_TOO_LARGE)
280 struct got_tree_object *
281 got_repo_get_cached_tree(struct got_repository *repo,
282 struct got_object_id *id)
284 return (struct got_tree_object *)got_object_cache_get(
285 &repo->treecache, id);
288 const struct got_error *
289 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
290 struct got_commit_object *commit)
292 #ifndef GOT_NO_OBJ_CACHE
293 const struct got_error *err = NULL;
294 err = got_object_cache_add(&repo->commitcache, id, commit);
296 if (err->code == GOT_ERR_OBJ_EXISTS ||
297 err->code == GOT_ERR_OBJ_TOO_LARGE)
306 struct got_commit_object *
307 got_repo_get_cached_commit(struct got_repository *repo,
308 struct got_object_id *id)
310 return (struct got_commit_object *)got_object_cache_get(
311 &repo->commitcache, id);
314 const struct got_error *
315 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
316 struct got_tag_object *tag)
318 #ifndef GOT_NO_OBJ_CACHE
319 const struct got_error *err = NULL;
320 err = got_object_cache_add(&repo->tagcache, id, tag);
322 if (err->code == GOT_ERR_OBJ_EXISTS ||
323 err->code == GOT_ERR_OBJ_TOO_LARGE)
332 struct got_tag_object *
333 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
335 return (struct got_tag_object *)got_object_cache_get(
336 &repo->tagcache, id);
339 const struct got_error *
340 open_repo(struct got_repository *repo, const char *path)
342 const struct got_error *err = NULL;
344 /* bare git repository? */
345 repo->path_git_dir = strdup(path);
346 if (repo->path_git_dir == NULL)
347 return got_error_from_errno("strdup");
348 if (is_git_repo(repo)) {
349 repo->path = strdup(repo->path_git_dir);
350 if (repo->path == NULL) {
351 err = got_error_from_errno("strdup");
357 /* git repository with working tree? */
358 free(repo->path_git_dir);
359 repo->path_git_dir = NULL;
360 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
361 err = got_error_from_errno("asprintf");
364 if (is_git_repo(repo)) {
365 repo->path = strdup(path);
366 if (repo->path == NULL) {
367 err = got_error_from_errno("strdup");
373 err = got_error(GOT_ERR_NOT_GIT_REPO);
378 free(repo->path_git_dir);
379 repo->path_git_dir = NULL;
384 static const struct got_error *
385 parse_gitconfig_file(int *gitconfig_repository_format_version,
386 char **gitconfig_author_name, char **gitconfig_author_email,
387 struct got_remote_repo **remotes, int *nremotes,
388 char **gitconfig_owner,
389 const char *gitconfig_path)
391 const struct got_error *err = NULL, *child_err = NULL;
393 int imsg_fds[2] = { -1, -1 };
395 struct imsgbuf *ibuf;
397 *gitconfig_repository_format_version = 0;
398 *gitconfig_author_name = NULL;
399 *gitconfig_author_email = NULL;
405 *gitconfig_owner = NULL;
407 fd = open(gitconfig_path, O_RDONLY);
411 return got_error_from_errno2("open", gitconfig_path);
414 ibuf = calloc(1, sizeof(*ibuf));
416 err = got_error_from_errno("calloc");
420 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
421 err = got_error_from_errno("socketpair");
427 err = got_error_from_errno("fork");
429 } else if (pid == 0) {
430 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
435 if (close(imsg_fds[1]) == -1) {
436 err = got_error_from_errno("close");
440 imsg_init(ibuf, imsg_fds[0]);
442 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
447 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
451 err = got_privsep_recv_gitconfig_int(
452 gitconfig_repository_format_version, ibuf);
456 err = got_privsep_send_gitconfig_author_name_req(ibuf);
460 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
464 err = got_privsep_send_gitconfig_author_email_req(ibuf);
468 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
472 if (remotes && nremotes) {
473 err = got_privsep_send_gitconfig_remotes_req(ibuf);
477 err = got_privsep_recv_gitconfig_remotes(remotes,
483 if (gitconfig_owner) {
484 err = got_privsep_send_gitconfig_owner_req(ibuf);
487 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
493 err = got_privsep_send_stop(imsg_fds[0]);
494 child_err = got_privsep_wait_for_child(pid);
495 if (child_err && err == NULL)
498 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
499 err = got_error_from_errno("close");
500 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
501 err = got_error_from_errno("close");
502 if (fd != -1 && close(fd) == -1 && err == NULL)
503 err = got_error_from_errno2("close", gitconfig_path);
508 static const struct got_error *
509 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
511 const struct got_error *err = NULL;
512 char *repo_gitconfig_path = NULL;
514 if (global_gitconfig_path) {
515 /* Read settings from ~/.gitconfig. */
516 int dummy_repo_version;
517 err = parse_gitconfig_file(&dummy_repo_version,
518 &repo->global_gitconfig_author_name,
519 &repo->global_gitconfig_author_email,
520 NULL, NULL, NULL, global_gitconfig_path);
525 /* Read repository's .git/config file. */
526 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
527 if (repo_gitconfig_path == NULL)
528 return got_error_from_errno("got_repo_get_path_gitconfig");
530 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
531 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
532 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
533 &repo->gitconfig_owner, repo_gitconfig_path);
537 free(repo_gitconfig_path);
541 static const struct got_error *
542 parse_gotconfig_file(char **author,
543 struct got_remote_repo **remotes, int *nremotes,
544 const char *gotconfig_path)
546 const struct got_error *err = NULL, *child_err = NULL;
548 int imsg_fds[2] = { -1, -1 };
550 struct imsgbuf *ibuf;
559 fd = open(gotconfig_path, O_RDONLY);
563 return got_error_from_errno2("open", gotconfig_path);
566 ibuf = calloc(1, sizeof(*ibuf));
568 err = got_error_from_errno("calloc");
572 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
573 err = got_error_from_errno("socketpair");
579 err = got_error_from_errno("fork");
581 } else if (pid == 0) {
582 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GOTCONFIG,
587 if (close(imsg_fds[1]) == -1) {
588 err = got_error_from_errno("close");
592 imsg_init(ibuf, imsg_fds[0]);
594 err = got_privsep_send_gotconfig_parse_req(ibuf, fd);
600 err = got_privsep_send_gotconfig_author_req(ibuf);
604 err = got_privsep_recv_gotconfig_str(author, ibuf);
609 if (remotes && nremotes) {
610 err = got_privsep_send_gotconfig_remotes_req(ibuf);
614 err = got_privsep_recv_gotconfig_remotes(remotes,
621 err = got_privsep_send_stop(imsg_fds[0]);
622 child_err = got_privsep_wait_for_child(pid);
623 if (child_err && err == NULL)
626 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
627 err = got_error_from_errno("close");
628 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
629 err = got_error_from_errno("close");
630 if (fd != -1 && close(fd) == -1 && err == NULL)
631 err = got_error_from_errno2("close", gotconfig_path);
642 static const struct got_error *
643 read_gotconfig(struct got_repository *repo)
645 const struct got_error *err = NULL;
646 char *gotconfig_path;
648 gotconfig_path = got_repo_get_path_gotconfig(repo);
649 if (gotconfig_path == NULL)
650 return got_error_from_errno("got_repo_get_path_gotconfig");
652 err = parse_gotconfig_file(&repo->gotconfig_author,
653 &repo->gotconfig_remotes, &repo->ngotconfig_remotes,
655 free(gotconfig_path);
659 const struct got_error *
660 got_repo_open(struct got_repository **repop, const char *path,
661 const char *global_gitconfig_path)
663 struct got_repository *repo = NULL;
664 const struct got_error *err = NULL;
666 int i, tried_root = 0;
670 if (got_path_is_absolute(path))
671 abspath = strdup(path);
673 abspath = got_path_get_absolute(path);
675 return got_error_path(path, GOT_ERR_BAD_PATH);
677 repo = calloc(1, sizeof(*repo));
679 err = got_error_from_errno("calloc");
683 for (i = 0; i < nitems(repo->privsep_children); i++) {
684 memset(&repo->privsep_children[i], 0,
685 sizeof(repo->privsep_children[0]));
686 repo->privsep_children[i].imsg_fd = -1;
689 err = got_object_cache_init(&repo->objcache,
690 GOT_OBJECT_CACHE_TYPE_OBJ);
693 err = got_object_cache_init(&repo->treecache,
694 GOT_OBJECT_CACHE_TYPE_TREE);
697 err = got_object_cache_init(&repo->commitcache,
698 GOT_OBJECT_CACHE_TYPE_COMMIT);
701 err = got_object_cache_init(&repo->tagcache,
702 GOT_OBJECT_CACHE_TYPE_TAG);
706 path = realpath(abspath, NULL);
708 err = got_error_from_errno2("realpath", abspath);
713 err = open_repo(repo, path);
716 if (err->code != GOT_ERR_NOT_GIT_REPO)
718 if (path[0] == '/' && path[1] == '\0') {
720 err = got_error(GOT_ERR_NOT_GIT_REPO);
725 path = dirname(path);
727 err = got_error_from_errno2("dirname", path);
732 err = read_gotconfig(repo);
736 err = read_gitconfig(repo, global_gitconfig_path);
739 if (repo->gitconfig_repository_format_version != 0)
740 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
743 got_repo_close(repo);
750 const struct got_error *
751 got_repo_close(struct got_repository *repo)
753 const struct got_error *err = NULL, *child_err;
756 for (i = 0; i < nitems(repo->packidx_cache); i++) {
757 if (repo->packidx_cache[i] == NULL)
759 got_packidx_close(repo->packidx_cache[i]);
762 for (i = 0; i < nitems(repo->packs); i++) {
763 if (repo->packs[i].path_packfile == NULL)
765 got_pack_close(&repo->packs[i]);
769 free(repo->path_git_dir);
771 got_object_cache_close(&repo->objcache);
772 got_object_cache_close(&repo->treecache);
773 got_object_cache_close(&repo->commitcache);
774 got_object_cache_close(&repo->tagcache);
776 for (i = 0; i < nitems(repo->privsep_children); i++) {
777 if (repo->privsep_children[i].imsg_fd == -1)
779 imsg_clear(repo->privsep_children[i].ibuf);
780 free(repo->privsep_children[i].ibuf);
781 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
782 child_err = got_privsep_wait_for_child(
783 repo->privsep_children[i].pid);
784 if (child_err && err == NULL)
786 if (close(repo->privsep_children[i].imsg_fd) != 0 &&
788 err = got_error_from_errno("close");
791 free(repo->gotconfig_author);
792 for (i = 0; i < repo->ngotconfig_remotes; i++) {
793 free(repo->gotconfig_remotes[i].name);
794 free(repo->gotconfig_remotes[i].url);
796 free(repo->gotconfig_remotes);
797 free(repo->gitconfig_author_name);
798 free(repo->gitconfig_author_email);
799 for (i = 0; i < repo->ngitconfig_remotes; i++) {
800 free(repo->gitconfig_remotes[i].name);
801 free(repo->gitconfig_remotes[i].url);
803 free(repo->gitconfig_remotes);
809 const struct got_error *
810 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
811 const char *input_path, int check_disk)
813 const struct got_error *err = NULL;
814 const char *repo_abspath = NULL;
816 char *canonpath, *path = NULL;
818 *in_repo_path = NULL;
820 canonpath = strdup(input_path);
821 if (canonpath == NULL) {
822 err = got_error_from_errno("strdup");
825 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
829 repo_abspath = got_repo_get_path(repo);
831 if (!check_disk || canonpath[0] == '\0') {
832 path = strdup(canonpath);
834 err = got_error_from_errno("strdup");
838 path = realpath(canonpath, NULL);
840 if (errno != ENOENT) {
841 err = got_error_from_errno2("realpath",
846 * Path is not on disk.
847 * Assume it is already relative to repository root.
849 path = strdup(canonpath);
851 err = got_error_from_errno("strdup");
856 repolen = strlen(repo_abspath);
860 if (strcmp(path, repo_abspath) == 0) {
864 err = got_error_from_errno("strdup");
867 } else if (len > repolen &&
868 got_path_is_child(path, repo_abspath, repolen)) {
869 /* Matched an on-disk path inside repository. */
870 if (got_repo_is_bare(repo)) {
872 * Matched an on-disk path inside repository
873 * database. Treat as repository-relative.
877 /* Strip common prefix with repository path. */
878 err = got_path_skip_common_ancestor(&child,
887 * Matched unrelated on-disk path.
888 * Treat it 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 < nitems(repo->packidx_cache); 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 == nitems(repo->packidx_cache)) {
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 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 < nitems(repo->packidx_cache); 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 path_packdir = got_repo_get_path_objects_pack(repo);
996 if (path_packdir == NULL)
997 return got_error_from_errno("got_repo_get_path_objects_pack");
999 packdir = opendir(path_packdir);
1000 if (packdir == NULL) {
1001 if (errno == ENOENT)
1002 err = got_error_no_obj(id);
1004 err = got_error_from_errno2("opendir", path_packdir);
1008 while ((dent = readdir(packdir)) != NULL) {
1011 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1014 if (asprintf(&path_packidx, "%s/%s", path_packdir,
1015 dent->d_name) == -1) {
1016 err = got_error_from_errno("asprintf");
1020 for (i = 0; i < nitems(repo->packidx_cache); 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, path_packidx, 0);
1040 err = cache_packidx(repo, *packidx, path_packidx);
1045 *idx = got_packidx_get_object_idx(*packidx, id);
1047 err = NULL; /* found the object */
1052 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, const char *path_packfile, struct got_packidx *packidx)
1085 const struct got_error *err = NULL;
1087 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
1089 return got_error_from_errno2("open", path_packfile);
1092 err = read_packfile_hdr(*fd, packidx);
1102 const struct got_error *
1103 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1104 const char *path_packfile, struct got_packidx *packidx)
1106 const struct got_error *err = NULL;
1107 struct got_pack *pack = NULL;
1114 for (i = 0; i < nitems(repo->packs); i++) {
1115 pack = &repo->packs[i];
1116 if (pack->path_packfile == NULL)
1118 if (strcmp(pack->path_packfile, path_packfile) == 0)
1119 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1122 if (i == nitems(repo->packs) - 1) {
1123 err = got_pack_close(&repo->packs[i - 1]);
1126 memmove(&repo->packs[1], &repo->packs[0],
1127 sizeof(repo->packs) - sizeof(repo->packs[0]));
1131 pack = &repo->packs[i];
1133 pack->path_packfile = strdup(path_packfile);
1134 if (pack->path_packfile == NULL) {
1135 err = got_error_from_errno("strdup");
1139 err = open_packfile(&pack->fd, path_packfile, packidx);
1143 if (fstat(pack->fd, &sb) != 0) {
1144 err = got_error_from_errno("fstat");
1147 pack->filesize = sb.st_size;
1149 pack->privsep_child = NULL;
1151 #ifndef GOT_PACK_NO_MMAP
1152 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1154 if (pack->map == MAP_FAILED) {
1155 if (errno != ENOMEM) {
1156 err = got_error_from_errno("mmap");
1159 pack->map = NULL; /* fall back to read(2) */
1165 free(pack->path_packfile);
1166 memset(pack, 0, sizeof(*pack));
1174 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1176 struct got_pack *pack = NULL;
1179 for (i = 0; i < nitems(repo->packs); i++) {
1180 pack = &repo->packs[i];
1181 if (pack->path_packfile == NULL)
1183 if (strcmp(pack->path_packfile, path_packfile) == 0)
1190 const struct got_error *
1191 got_repo_init(const char *repo_path)
1193 const struct got_error *err = NULL;
1194 const char *dirnames[] = {
1196 GOT_OBJECTS_PACK_DIR,
1199 const char *description_str = "Unnamed repository; "
1200 "edit this file 'description' to name the repository.";
1201 const char *headref_str = "ref: refs/heads/main";
1202 const char *gitconfig_str = "[core]\n"
1203 "\trepositoryformatversion = 0\n"
1204 "\tfilemode = true\n"
1209 if (!got_path_dir_is_empty(repo_path))
1210 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1212 for (i = 0; i < nitems(dirnames); i++) {
1213 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1214 return got_error_from_errno("asprintf");
1216 err = got_path_mkdir(path);
1222 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1223 return got_error_from_errno("asprintf");
1224 err = got_path_create_file(path, description_str);
1229 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1230 return got_error_from_errno("asprintf");
1231 err = got_path_create_file(path, headref_str);
1236 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1237 return got_error_from_errno("asprintf");
1238 err = got_path_create_file(path, gitconfig_str);
1246 static const struct got_error *
1247 match_packed_object(struct got_object_id **unique_id,
1248 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1250 const struct got_error *err = NULL;
1253 struct dirent *dent;
1255 struct got_object_id_queue matched_ids;
1257 SIMPLEQ_INIT(&matched_ids);
1259 path_packdir = got_repo_get_path_objects_pack(repo);
1260 if (path_packdir == NULL)
1261 return got_error_from_errno("got_repo_get_path_objects_pack");
1263 packdir = opendir(path_packdir);
1264 if (packdir == NULL) {
1265 if (errno != ENOENT)
1266 err = got_error_from_errno2("opendir", path_packdir);
1270 while ((dent = readdir(packdir)) != NULL) {
1271 struct got_packidx *packidx;
1272 struct got_object_qid *qid;
1275 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1278 if (asprintf(&path_packidx, "%s/%s", path_packdir,
1279 dent->d_name) == -1) {
1280 err = got_error_from_errno("asprintf");
1284 err = got_packidx_open(&packidx, path_packidx, 0);
1289 err = got_packidx_match_id_str_prefix(&matched_ids,
1290 packidx, id_str_prefix);
1292 got_packidx_close(packidx);
1295 err = got_packidx_close(packidx);
1299 SIMPLEQ_FOREACH(qid, &matched_ids, entry) {
1300 if (obj_type != GOT_OBJ_TYPE_ANY) {
1302 err = got_object_get_type(&matched_type, repo,
1306 if (matched_type != obj_type)
1309 if (*unique_id == NULL) {
1310 *unique_id = got_object_id_dup(qid->id);
1311 if (*unique_id == NULL) {
1312 err = got_error_from_errno("malloc");
1316 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1317 continue; /* packed multiple times */
1318 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1324 got_object_id_queue_free(&matched_ids);
1326 if (packdir && closedir(packdir) != 0 && err == NULL)
1327 err = got_error_from_errno("closedir");
1335 static const struct got_error *
1336 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1337 const char *object_dir, const char *id_str_prefix, int obj_type,
1338 struct got_repository *repo)
1340 const struct got_error *err = NULL;
1343 struct dirent *dent;
1344 struct got_object_id id;
1346 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1347 err = got_error_from_errno("asprintf");
1351 dir = opendir(path);
1353 if (errno == ENOENT) {
1357 err = got_error_from_errno2("opendir", path);
1360 while ((dent = readdir(dir)) != NULL) {
1364 if (strcmp(dent->d_name, ".") == 0 ||
1365 strcmp(dent->d_name, "..") == 0)
1368 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1369 err = got_error_from_errno("asprintf");
1373 if (!got_parse_sha1_digest(id.sha1, id_str))
1377 * Directory entries do not necessarily appear in
1378 * sorted order, so we must iterate over all of them.
1380 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1386 if (*unique_id == NULL) {
1387 if (obj_type != GOT_OBJ_TYPE_ANY) {
1389 err = got_object_get_type(&matched_type, repo,
1393 if (matched_type != obj_type)
1396 *unique_id = got_object_id_dup(&id);
1397 if (*unique_id == NULL) {
1398 err = got_error_from_errno("got_object_id_dup");
1403 if (got_object_id_cmp(*unique_id, &id) == 0)
1404 continue; /* both packed and loose */
1405 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1411 if (dir && closedir(dir) != 0 && err == NULL)
1412 err = got_error_from_errno("closedir");
1421 const struct got_error *
1422 got_repo_match_object_id_prefix(struct got_object_id **id,
1423 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1425 const struct got_error *err = NULL;
1426 char *path_objects = got_repo_get_path_objects(repo);
1427 char *object_dir = NULL;
1433 for (i = 0; i < strlen(id_str_prefix); i++) {
1434 if (isxdigit((unsigned char)id_str_prefix[i]))
1436 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1439 len = strlen(id_str_prefix);
1441 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1444 object_dir = strndup(id_str_prefix, 2);
1445 if (object_dir == NULL) {
1446 err = got_error_from_errno("strdup");
1449 err = match_loose_object(id, path_objects, object_dir,
1450 id_str_prefix, obj_type, repo);
1451 } else if (len == 1) {
1453 for (i = 0; i < 0xf; i++) {
1454 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1456 err = got_error_from_errno("asprintf");
1459 err = match_packed_object(id, repo, object_dir,
1463 err = match_loose_object(id, path_objects, object_dir,
1464 id_str_prefix, obj_type, repo);
1469 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1477 } else if (*id == NULL)
1478 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1483 const struct got_error *
1484 got_repo_match_object_id(struct got_object_id **id, char **label,
1485 const char *id_str, int obj_type, int resolve_tags,
1486 struct got_repository *repo)
1488 const struct got_error *err;
1489 struct got_tag_object *tag;
1490 struct got_reference *ref = NULL;
1497 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
1500 *id = got_object_id_dup(
1501 got_object_tag_get_object_id(tag));
1503 err = got_error_from_errno("got_object_id_dup");
1504 else if (label && asprintf(label, "refs/tags/%s",
1505 got_object_tag_get_name(tag)) == -1) {
1506 err = got_error_from_errno("asprintf");
1510 got_object_tag_close(tag);
1512 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1513 err->code != GOT_ERR_NO_OBJ)
1517 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1519 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1521 err = got_ref_open(&ref, repo, id_str, 0);
1525 *label = strdup(got_ref_get_name(ref));
1526 if (*label == NULL) {
1527 err = got_error_from_errno("strdup");
1531 err = got_ref_resolve(id, repo, ref);
1533 err = got_object_id_str(label, *id);
1534 if (*label == NULL) {
1535 err = got_error_from_errno("strdup");
1545 const struct got_error *
1546 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1547 int obj_type, struct got_repository *repo)
1549 const struct got_error *err;
1550 struct got_reflist_head refs;
1551 struct got_reflist_entry *re;
1552 struct got_object_id *tag_id;
1554 SIMPLEQ_INIT(&refs);
1557 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_by_name, NULL);
1561 SIMPLEQ_FOREACH(re, &refs, entry) {
1562 const char *refname;
1563 refname = got_ref_get_name(re->ref);
1564 if (got_ref_is_symbolic(re->ref))
1566 refname += strlen("refs/tags/");
1567 if (strcmp(refname, name) != 0)
1569 err = got_ref_resolve(&tag_id, repo, re->ref);
1572 err = got_object_open_as_tag(tag, repo, tag_id);
1576 if (obj_type == GOT_OBJ_TYPE_ANY ||
1577 got_object_tag_get_object_type(*tag) == obj_type)
1579 got_object_tag_close(*tag);
1583 got_ref_list_free(&refs);
1584 if (err == NULL && *tag == NULL)
1585 err = got_error_path(name, GOT_ERR_NO_OBJ);
1589 static const struct got_error *
1590 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1591 const char *name, mode_t mode, struct got_object_id *blob_id)
1593 const struct got_error *err = NULL;
1597 *new_te = calloc(1, sizeof(**new_te));
1598 if (*new_te == NULL)
1599 return got_error_from_errno("calloc");
1601 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1602 sizeof((*new_te)->name)) {
1603 err = got_error(GOT_ERR_NO_SPACE);
1607 if (S_ISLNK(mode)) {
1608 (*new_te)->mode = S_IFLNK;
1610 (*new_te)->mode = S_IFREG;
1611 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1613 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1615 if (err && *new_te) {
1622 static const struct got_error *
1623 import_file(struct got_tree_entry **new_te, struct dirent *de,
1624 const char *path, struct got_repository *repo)
1626 const struct got_error *err;
1627 struct got_object_id *blob_id = NULL;
1631 if (asprintf(&filepath, "%s%s%s", path,
1632 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1633 return got_error_from_errno("asprintf");
1635 if (lstat(filepath, &sb) != 0) {
1636 err = got_error_from_errno2("lstat", path);
1640 err = got_object_blob_create(&blob_id, filepath, repo);
1644 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1653 static const struct got_error *
1654 insert_tree_entry(struct got_tree_entry *new_te,
1655 struct got_pathlist_head *paths)
1657 const struct got_error *err = NULL;
1658 struct got_pathlist_entry *new_pe;
1660 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1664 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1668 static const struct got_error *write_tree(struct got_object_id **,
1669 const char *, struct got_pathlist_head *, struct got_repository *,
1670 got_repo_import_cb progress_cb, void *progress_arg);
1672 static const struct got_error *
1673 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1674 const char *path, struct got_pathlist_head *ignores,
1675 struct got_repository *repo,
1676 got_repo_import_cb progress_cb, void *progress_arg)
1678 const struct got_error *err;
1679 struct got_object_id *id = NULL;
1682 if (asprintf(&subdirpath, "%s%s%s", path,
1683 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1684 return got_error_from_errno("asprintf");
1686 (*new_te) = calloc(1, sizeof(**new_te));
1687 if (*new_te == NULL)
1688 return got_error_from_errno("calloc");
1689 (*new_te)->mode = S_IFDIR;
1690 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1691 sizeof((*new_te)->name)) {
1692 err = got_error(GOT_ERR_NO_SPACE);
1695 err = write_tree(&id, subdirpath, ignores, repo,
1696 progress_cb, progress_arg);
1699 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1711 static const struct got_error *
1712 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1713 struct got_pathlist_head *ignores, struct got_repository *repo,
1714 got_repo_import_cb progress_cb, void *progress_arg)
1716 const struct got_error *err = NULL;
1720 struct got_tree_entry *new_te = NULL;
1721 struct got_pathlist_head paths;
1722 struct got_pathlist_entry *pe;
1724 *new_tree_id = NULL;
1728 dir = opendir(path_dir);
1730 err = got_error_from_errno2("opendir", path_dir);
1735 while ((de = readdir(dir)) != NULL) {
1739 if (strcmp(de->d_name, ".") == 0 ||
1740 strcmp(de->d_name, "..") == 0)
1743 TAILQ_FOREACH(pe, ignores, entry) {
1744 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1752 err = got_path_dirent_type(&type, path_dir, de);
1756 if (type == DT_DIR) {
1757 err = import_subdir(&new_te, de, path_dir,
1758 ignores, repo, progress_cb, progress_arg);
1760 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1765 } else if (type == DT_REG || type == DT_LNK) {
1766 err = import_file(&new_te, de, path_dir, repo);
1772 err = insert_tree_entry(new_te, &paths);
1778 if (TAILQ_EMPTY(&paths)) {
1779 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
1780 "cannot create tree without any entries");
1784 TAILQ_FOREACH(pe, &paths, entry) {
1785 struct got_tree_entry *te = pe->data;
1787 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
1789 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1790 err = got_error_from_errno("asprintf");
1793 err = (*progress_cb)(progress_arg, path);
1799 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1803 got_pathlist_free(&paths);
1807 const struct got_error *
1808 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1809 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1810 struct got_repository *repo, got_repo_import_cb progress_cb,
1813 const struct got_error *err;
1814 struct got_object_id *new_tree_id;
1816 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1817 progress_cb, progress_arg);
1821 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1822 author, time(NULL), author, time(NULL), logmsg, repo);