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]))
67 got_repo_get_path(struct got_repository *repo)
73 got_repo_get_path_git_dir(struct got_repository *repo)
75 return repo->path_git_dir;
79 got_repo_get_gitconfig_author_name(struct got_repository *repo)
81 return repo->gitconfig_author_name;
85 got_repo_get_gitconfig_author_email(struct got_repository *repo)
87 return repo->gitconfig_author_email;
91 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
93 return repo->global_gitconfig_author_name;
97 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
99 return repo->global_gitconfig_author_email;
103 got_repo_get_gitconfig_owner(struct got_repository *repo)
105 return repo->gitconfig_owner;
109 got_repo_is_bare(struct got_repository *repo)
111 return (strcmp(repo->path, repo->path_git_dir) == 0);
115 get_path_git_child(struct got_repository *repo, const char *basename)
119 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
127 got_repo_get_path_objects(struct got_repository *repo)
129 return get_path_git_child(repo, GOT_OBJECTS_DIR);
133 got_repo_get_path_objects_pack(struct got_repository *repo)
135 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
139 got_repo_get_path_refs(struct got_repository *repo)
141 return get_path_git_child(repo, GOT_REFS_DIR);
145 got_repo_get_path_packed_refs(struct got_repository *repo)
147 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
151 get_path_head(struct got_repository *repo)
153 return get_path_git_child(repo, GOT_HEAD_FILE);
157 got_repo_get_path_gitconfig(struct got_repository *repo)
159 return get_path_git_child(repo, GOT_GITCONFIG);
163 got_repo_get_gitconfig_remotes(int *nremotes, struct got_remote_repo **remotes,
164 struct got_repository *repo)
166 *nremotes = repo->ngitconfig_remotes;
167 *remotes = repo->gitconfig_remotes;
171 is_git_repo(struct got_repository *repo)
173 const char *path_git = got_repo_get_path_git_dir(repo);
174 char *path_objects = got_repo_get_path_objects(repo);
175 char *path_refs = got_repo_get_path_refs(repo);
176 char *path_head = get_path_head(repo);
179 struct got_reference *head_ref;
181 if (lstat(path_git, &sb) == -1)
183 if (!S_ISDIR(sb.st_mode))
186 if (lstat(path_objects, &sb) == -1)
188 if (!S_ISDIR(sb.st_mode))
191 if (lstat(path_refs, &sb) == -1)
193 if (!S_ISDIR(sb.st_mode))
196 if (lstat(path_head, &sb) == -1)
198 if (!S_ISREG(sb.st_mode))
201 /* Check if the HEAD reference can be opened. */
202 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
204 got_ref_close(head_ref);
215 const struct got_error *
216 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
217 struct got_object *obj)
219 #ifndef GOT_NO_OBJ_CACHE
220 const struct got_error *err = NULL;
221 err = got_object_cache_add(&repo->objcache, id, obj);
223 if (err->code == GOT_ERR_OBJ_EXISTS ||
224 err->code == GOT_ERR_OBJ_TOO_LARGE)
234 got_repo_get_cached_object(struct got_repository *repo,
235 struct got_object_id *id)
237 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
240 const struct got_error *
241 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
242 struct got_tree_object *tree)
244 #ifndef GOT_NO_OBJ_CACHE
245 const struct got_error *err = NULL;
246 err = got_object_cache_add(&repo->treecache, id, tree);
248 if (err->code == GOT_ERR_OBJ_EXISTS ||
249 err->code == GOT_ERR_OBJ_TOO_LARGE)
258 struct got_tree_object *
259 got_repo_get_cached_tree(struct got_repository *repo,
260 struct got_object_id *id)
262 return (struct got_tree_object *)got_object_cache_get(
263 &repo->treecache, id);
266 const struct got_error *
267 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
268 struct got_commit_object *commit)
270 #ifndef GOT_NO_OBJ_CACHE
271 const struct got_error *err = NULL;
272 err = got_object_cache_add(&repo->commitcache, id, commit);
274 if (err->code == GOT_ERR_OBJ_EXISTS ||
275 err->code == GOT_ERR_OBJ_TOO_LARGE)
284 struct got_commit_object *
285 got_repo_get_cached_commit(struct got_repository *repo,
286 struct got_object_id *id)
288 return (struct got_commit_object *)got_object_cache_get(
289 &repo->commitcache, id);
292 const struct got_error *
293 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
294 struct got_tag_object *tag)
296 #ifndef GOT_NO_OBJ_CACHE
297 const struct got_error *err = NULL;
298 err = got_object_cache_add(&repo->tagcache, id, tag);
300 if (err->code == GOT_ERR_OBJ_EXISTS ||
301 err->code == GOT_ERR_OBJ_TOO_LARGE)
310 struct got_tag_object *
311 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
313 return (struct got_tag_object *)got_object_cache_get(
314 &repo->tagcache, id);
317 const struct got_error *
318 open_repo(struct got_repository *repo, const char *path)
320 const struct got_error *err = NULL;
322 /* bare git repository? */
323 repo->path_git_dir = strdup(path);
324 if (repo->path_git_dir == NULL)
325 return got_error_from_errno("strdup");
326 if (is_git_repo(repo)) {
327 repo->path = strdup(repo->path_git_dir);
328 if (repo->path == NULL) {
329 err = got_error_from_errno("strdup");
335 /* git repository with working tree? */
336 free(repo->path_git_dir);
337 repo->path_git_dir = NULL;
338 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
339 err = got_error_from_errno("asprintf");
342 if (is_git_repo(repo)) {
343 repo->path = strdup(path);
344 if (repo->path == NULL) {
345 err = got_error_from_errno("strdup");
351 err = got_error(GOT_ERR_NOT_GIT_REPO);
356 free(repo->path_git_dir);
357 repo->path_git_dir = NULL;
362 static const struct got_error *
363 parse_gitconfig_file(int *gitconfig_repository_format_version,
364 char **gitconfig_author_name, char **gitconfig_author_email,
365 struct got_remote_repo **remotes, int *nremotes,
366 char **gitconfig_owner,
367 const char *gitconfig_path)
369 const struct got_error *err = NULL, *child_err = NULL;
371 int imsg_fds[2] = { -1, -1 };
373 struct imsgbuf *ibuf;
375 *gitconfig_repository_format_version = 0;
376 *gitconfig_author_name = NULL;
377 *gitconfig_author_email = NULL;
383 *gitconfig_owner = NULL;
385 fd = open(gitconfig_path, O_RDONLY);
389 return got_error_from_errno2("open", gitconfig_path);
392 ibuf = calloc(1, sizeof(*ibuf));
394 err = got_error_from_errno("calloc");
398 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
399 err = got_error_from_errno("socketpair");
405 err = got_error_from_errno("fork");
407 } else if (pid == 0) {
408 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
413 if (close(imsg_fds[1]) == -1) {
414 err = got_error_from_errno("close");
418 imsg_init(ibuf, imsg_fds[0]);
420 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
425 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
429 err = got_privsep_recv_gitconfig_int(
430 gitconfig_repository_format_version, ibuf);
434 err = got_privsep_send_gitconfig_author_name_req(ibuf);
438 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
442 err = got_privsep_send_gitconfig_author_email_req(ibuf);
446 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
450 if (remotes && nremotes) {
451 err = got_privsep_send_gitconfig_remotes_req(ibuf);
455 err = got_privsep_recv_gitconfig_remotes(remotes,
461 if (gitconfig_owner) {
462 err = got_privsep_send_gitconfig_owner_req(ibuf);
465 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
471 err = got_privsep_send_stop(imsg_fds[0]);
472 child_err = got_privsep_wait_for_child(pid);
473 if (child_err && err == NULL)
476 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
477 err = got_error_from_errno("close");
478 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
479 err = got_error_from_errno("close");
480 if (fd != -1 && close(fd) == -1 && err == NULL)
481 err = got_error_from_errno2("close", gitconfig_path);
486 static const struct got_error *
487 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
489 const struct got_error *err = NULL;
490 char *repo_gitconfig_path = NULL;
492 if (global_gitconfig_path) {
493 /* Read settings from ~/.gitconfig. */
494 int dummy_repo_version;
495 err = parse_gitconfig_file(&dummy_repo_version,
496 &repo->global_gitconfig_author_name,
497 &repo->global_gitconfig_author_email,
498 NULL, NULL, NULL, global_gitconfig_path);
503 /* Read repository's .git/config file. */
504 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
505 if (repo_gitconfig_path == NULL)
506 return got_error_from_errno("got_repo_get_path_gitconfig");
508 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
509 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
510 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
511 &repo->gitconfig_owner, repo_gitconfig_path);
515 free(repo_gitconfig_path);
519 const struct got_error *
520 got_repo_open(struct got_repository **repop, const char *path,
521 const char *global_gitconfig_path)
523 struct got_repository *repo = NULL;
524 const struct got_error *err = NULL;
526 int i, tried_root = 0;
530 if (got_path_is_absolute(path))
531 abspath = strdup(path);
533 abspath = got_path_get_absolute(path);
535 return got_error_path(path, GOT_ERR_BAD_PATH);
537 repo = calloc(1, sizeof(*repo));
539 err = got_error_from_errno("calloc");
543 for (i = 0; i < nitems(repo->privsep_children); i++) {
544 memset(&repo->privsep_children[i], 0,
545 sizeof(repo->privsep_children[0]));
546 repo->privsep_children[i].imsg_fd = -1;
549 err = got_object_cache_init(&repo->objcache,
550 GOT_OBJECT_CACHE_TYPE_OBJ);
553 err = got_object_cache_init(&repo->treecache,
554 GOT_OBJECT_CACHE_TYPE_TREE);
557 err = got_object_cache_init(&repo->commitcache,
558 GOT_OBJECT_CACHE_TYPE_COMMIT);
561 err = got_object_cache_init(&repo->tagcache,
562 GOT_OBJECT_CACHE_TYPE_TAG);
566 path = realpath(abspath, NULL);
568 err = got_error_from_errno2("realpath", abspath);
573 err = open_repo(repo, path);
576 if (err->code != GOT_ERR_NOT_GIT_REPO)
578 if (path[0] == '/' && path[1] == '\0') {
580 err = got_error(GOT_ERR_NOT_GIT_REPO);
585 path = dirname(path);
587 err = got_error_from_errno2("dirname", path);
592 err = read_gitconfig(repo, global_gitconfig_path);
595 if (repo->gitconfig_repository_format_version != 0)
596 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
599 got_repo_close(repo);
606 const struct got_error *
607 got_repo_close(struct got_repository *repo)
609 const struct got_error *err = NULL, *child_err;
612 for (i = 0; i < nitems(repo->packidx_cache); i++) {
613 if (repo->packidx_cache[i] == NULL)
615 got_packidx_close(repo->packidx_cache[i]);
618 for (i = 0; i < nitems(repo->packs); i++) {
619 if (repo->packs[i].path_packfile == NULL)
621 got_pack_close(&repo->packs[i]);
625 free(repo->path_git_dir);
627 got_object_cache_close(&repo->objcache);
628 got_object_cache_close(&repo->treecache);
629 got_object_cache_close(&repo->commitcache);
630 got_object_cache_close(&repo->tagcache);
632 for (i = 0; i < nitems(repo->privsep_children); i++) {
633 if (repo->privsep_children[i].imsg_fd == -1)
635 imsg_clear(repo->privsep_children[i].ibuf);
636 free(repo->privsep_children[i].ibuf);
637 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
638 child_err = got_privsep_wait_for_child(
639 repo->privsep_children[i].pid);
640 if (child_err && err == NULL)
642 if (close(repo->privsep_children[i].imsg_fd) != 0 &&
644 err = got_error_from_errno("close");
647 free(repo->gitconfig_author_name);
648 free(repo->gitconfig_author_email);
649 for (i = 0; i < repo->ngitconfig_remotes; i++) {
650 free(repo->gitconfig_remotes[i].name);
651 free(repo->gitconfig_remotes[i].url);
653 free(repo->gitconfig_remotes);
659 const struct got_error *
660 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
661 const char *input_path, int check_disk)
663 const struct got_error *err = NULL;
664 const char *repo_abspath = NULL;
666 char *canonpath, *path = NULL;
668 *in_repo_path = NULL;
670 canonpath = strdup(input_path);
671 if (canonpath == NULL) {
672 err = got_error_from_errno("strdup");
675 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
679 repo_abspath = got_repo_get_path(repo);
681 if (!check_disk || canonpath[0] == '\0') {
682 path = strdup(canonpath);
684 err = got_error_from_errno("strdup");
688 path = realpath(canonpath, NULL);
690 if (errno != ENOENT) {
691 err = got_error_from_errno2("realpath",
696 * Path is not on disk.
697 * Assume it is already relative to repository root.
699 path = strdup(canonpath);
701 err = got_error_from_errno("strdup");
706 repolen = strlen(repo_abspath);
710 if (strcmp(path, repo_abspath) == 0) {
714 err = got_error_from_errno("strdup");
717 } else if (len > repolen &&
718 got_path_is_child(path, repo_abspath, repolen)) {
719 /* Matched an on-disk path inside repository. */
720 if (got_repo_is_bare(repo)) {
722 * Matched an on-disk path inside repository
723 * database. Treat as repository-relative.
727 /* Strip common prefix with repository path. */
728 err = got_path_skip_common_ancestor(&child,
737 * Matched unrelated on-disk path.
738 * Treat it as repository-relative.
743 /* Make in-repository path absolute */
744 if (path[0] != '/') {
746 if (asprintf(&abspath, "/%s", path) == -1) {
747 err = got_error_from_errno("asprintf");
759 *in_repo_path = path;
763 static const struct got_error *
764 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
765 const char *path_packidx)
767 const struct got_error *err = NULL;
770 for (i = 0; i < nitems(repo->packidx_cache); i++) {
771 if (repo->packidx_cache[i] == NULL)
773 if (strcmp(repo->packidx_cache[i]->path_packidx,
774 path_packidx) == 0) {
775 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
778 if (i == nitems(repo->packidx_cache)) {
779 err = got_packidx_close(repo->packidx_cache[i - 1]);
785 * Insert the new pack index at the front so it will
786 * be searched first in the future.
788 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
789 sizeof(repo->packidx_cache) -
790 sizeof(repo->packidx_cache[0]));
791 repo->packidx_cache[0] = packidx;
797 is_packidx_filename(const char *name, size_t len)
799 if (len != GOT_PACKIDX_NAMELEN)
802 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
805 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
806 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
812 const struct got_error *
813 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
814 struct got_repository *repo, struct got_object_id *id)
816 const struct got_error *err;
823 /* Search pack index cache. */
824 for (i = 0; i < nitems(repo->packidx_cache); i++) {
825 if (repo->packidx_cache[i] == NULL)
827 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
829 *packidx = repo->packidx_cache[i];
831 * Move this cache entry to the front. Repeatedly
832 * searching a wrong pack index can be expensive.
835 struct got_packidx *p;
836 p = repo->packidx_cache[0];
837 repo->packidx_cache[0] = *packidx;
838 repo->packidx_cache[i] = p;
843 /* No luck. Search the filesystem. */
845 path_packdir = got_repo_get_path_objects_pack(repo);
846 if (path_packdir == NULL)
847 return got_error_from_errno("got_repo_get_path_objects_pack");
849 packdir = opendir(path_packdir);
850 if (packdir == NULL) {
852 err = got_error_no_obj(id);
854 err = got_error_from_errno2("opendir", path_packdir);
858 while ((dent = readdir(packdir)) != NULL) {
861 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
864 if (asprintf(&path_packidx, "%s/%s", path_packdir,
865 dent->d_name) == -1) {
866 err = got_error_from_errno("asprintf");
870 for (i = 0; i < nitems(repo->packidx_cache); i++) {
871 if (repo->packidx_cache[i] == NULL)
873 if (strcmp(repo->packidx_cache[i]->path_packidx,
874 path_packidx) == 0) {
881 continue; /* already searched */
884 err = got_packidx_open(packidx, path_packidx, 0);
890 err = cache_packidx(repo, *packidx, path_packidx);
895 *idx = got_packidx_get_object_idx(*packidx, id);
897 err = NULL; /* found the object */
902 err = got_error_no_obj(id);
905 if (packdir && closedir(packdir) != 0 && err == NULL)
906 err = got_error_from_errno("closedir");
910 static const struct got_error *
911 read_packfile_hdr(int fd, struct got_packidx *packidx)
913 const struct got_error *err = NULL;
914 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
915 struct got_packfile_hdr hdr;
918 n = read(fd, &hdr, sizeof(hdr));
920 return got_error_from_errno("read");
921 if (n != sizeof(hdr))
922 return got_error(GOT_ERR_BAD_PACKFILE);
924 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
925 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
926 betoh32(hdr.nobjects) != totobj)
927 err = got_error(GOT_ERR_BAD_PACKFILE);
932 static const struct got_error *
933 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
935 const struct got_error *err = NULL;
937 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
939 return got_error_from_errno2("open", path_packfile);
942 err = read_packfile_hdr(*fd, packidx);
952 const struct got_error *
953 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
954 const char *path_packfile, struct got_packidx *packidx)
956 const struct got_error *err = NULL;
957 struct got_pack *pack = NULL;
964 for (i = 0; i < nitems(repo->packs); i++) {
965 pack = &repo->packs[i];
966 if (pack->path_packfile == NULL)
968 if (strcmp(pack->path_packfile, path_packfile) == 0)
969 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
972 if (i == nitems(repo->packs) - 1) {
973 err = got_pack_close(&repo->packs[i - 1]);
976 memmove(&repo->packs[1], &repo->packs[0],
977 sizeof(repo->packs) - sizeof(repo->packs[0]));
981 pack = &repo->packs[i];
983 pack->path_packfile = strdup(path_packfile);
984 if (pack->path_packfile == NULL) {
985 err = got_error_from_errno("strdup");
989 err = open_packfile(&pack->fd, path_packfile, packidx);
993 if (fstat(pack->fd, &sb) != 0) {
994 err = got_error_from_errno("fstat");
997 pack->filesize = sb.st_size;
999 pack->privsep_child = NULL;
1001 #ifndef GOT_PACK_NO_MMAP
1002 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1004 if (pack->map == MAP_FAILED) {
1005 if (errno != ENOMEM) {
1006 err = got_error_from_errno("mmap");
1009 pack->map = NULL; /* fall back to read(2) */
1015 free(pack->path_packfile);
1016 memset(pack, 0, sizeof(*pack));
1024 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1026 struct got_pack *pack = NULL;
1029 for (i = 0; i < nitems(repo->packs); i++) {
1030 pack = &repo->packs[i];
1031 if (pack->path_packfile == NULL)
1033 if (strcmp(pack->path_packfile, path_packfile) == 0)
1040 const struct got_error *
1041 got_repo_init(const char *repo_path)
1043 const struct got_error *err = NULL;
1044 const char *dirnames[] = {
1046 GOT_OBJECTS_PACK_DIR,
1049 const char *description_str = "Unnamed repository; "
1050 "edit this file 'description' to name the repository.";
1051 const char *headref_str = "ref: refs/heads/main";
1052 const char *gitconfig_str = "[core]\n"
1053 "\trepositoryformatversion = 0\n"
1054 "\tfilemode = true\n"
1059 if (!got_path_dir_is_empty(repo_path))
1060 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1062 for (i = 0; i < nitems(dirnames); i++) {
1063 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1064 return got_error_from_errno("asprintf");
1066 err = got_path_mkdir(path);
1072 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1073 return got_error_from_errno("asprintf");
1074 err = got_path_create_file(path, description_str);
1079 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1080 return got_error_from_errno("asprintf");
1081 err = got_path_create_file(path, headref_str);
1086 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1087 return got_error_from_errno("asprintf");
1088 err = got_path_create_file(path, gitconfig_str);
1096 static const struct got_error *
1097 match_packed_object(struct got_object_id **unique_id,
1098 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1100 const struct got_error *err = NULL;
1103 struct dirent *dent;
1105 struct got_object_id_queue matched_ids;
1107 SIMPLEQ_INIT(&matched_ids);
1109 path_packdir = got_repo_get_path_objects_pack(repo);
1110 if (path_packdir == NULL)
1111 return got_error_from_errno("got_repo_get_path_objects_pack");
1113 packdir = opendir(path_packdir);
1114 if (packdir == NULL) {
1115 if (errno != ENOENT)
1116 err = got_error_from_errno2("opendir", path_packdir);
1120 while ((dent = readdir(packdir)) != NULL) {
1121 struct got_packidx *packidx;
1122 struct got_object_qid *qid;
1125 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1128 if (asprintf(&path_packidx, "%s/%s", path_packdir,
1129 dent->d_name) == -1) {
1130 err = got_error_from_errno("asprintf");
1134 err = got_packidx_open(&packidx, path_packidx, 0);
1139 err = got_packidx_match_id_str_prefix(&matched_ids,
1140 packidx, id_str_prefix);
1142 got_packidx_close(packidx);
1145 err = got_packidx_close(packidx);
1149 SIMPLEQ_FOREACH(qid, &matched_ids, entry) {
1150 if (obj_type != GOT_OBJ_TYPE_ANY) {
1152 err = got_object_get_type(&matched_type, repo,
1156 if (matched_type != obj_type)
1159 if (*unique_id == NULL) {
1160 *unique_id = got_object_id_dup(qid->id);
1161 if (*unique_id == NULL) {
1162 err = got_error_from_errno("malloc");
1166 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1167 continue; /* packed multiple times */
1168 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1174 got_object_id_queue_free(&matched_ids);
1176 if (packdir && closedir(packdir) != 0 && err == NULL)
1177 err = got_error_from_errno("closedir");
1185 static const struct got_error *
1186 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1187 const char *object_dir, const char *id_str_prefix, int obj_type,
1188 struct got_repository *repo)
1190 const struct got_error *err = NULL;
1193 struct dirent *dent;
1194 struct got_object_id id;
1196 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1197 err = got_error_from_errno("asprintf");
1201 dir = opendir(path);
1203 if (errno == ENOENT) {
1207 err = got_error_from_errno2("opendir", path);
1210 while ((dent = readdir(dir)) != NULL) {
1214 if (strcmp(dent->d_name, ".") == 0 ||
1215 strcmp(dent->d_name, "..") == 0)
1218 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1219 err = got_error_from_errno("asprintf");
1223 if (!got_parse_sha1_digest(id.sha1, id_str))
1227 * Directory entries do not necessarily appear in
1228 * sorted order, so we must iterate over all of them.
1230 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1236 if (*unique_id == NULL) {
1237 if (obj_type != GOT_OBJ_TYPE_ANY) {
1239 err = got_object_get_type(&matched_type, repo,
1243 if (matched_type != obj_type)
1246 *unique_id = got_object_id_dup(&id);
1247 if (*unique_id == NULL) {
1248 err = got_error_from_errno("got_object_id_dup");
1253 if (got_object_id_cmp(*unique_id, &id) == 0)
1254 continue; /* both packed and loose */
1255 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1261 if (dir && closedir(dir) != 0 && err == NULL)
1262 err = got_error_from_errno("closedir");
1271 const struct got_error *
1272 got_repo_match_object_id_prefix(struct got_object_id **id,
1273 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1275 const struct got_error *err = NULL;
1276 char *path_objects = got_repo_get_path_objects(repo);
1277 char *object_dir = NULL;
1283 for (i = 0; i < strlen(id_str_prefix); i++) {
1284 if (isxdigit((unsigned char)id_str_prefix[i]))
1286 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1289 len = strlen(id_str_prefix);
1291 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1294 object_dir = strndup(id_str_prefix, 2);
1295 if (object_dir == NULL) {
1296 err = got_error_from_errno("strdup");
1299 err = match_loose_object(id, path_objects, object_dir,
1300 id_str_prefix, obj_type, repo);
1301 } else if (len == 1) {
1303 for (i = 0; i < 0xf; i++) {
1304 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1306 err = got_error_from_errno("asprintf");
1309 err = match_packed_object(id, repo, object_dir,
1313 err = match_loose_object(id, path_objects, object_dir,
1314 id_str_prefix, obj_type, repo);
1319 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1327 } else if (*id == NULL)
1328 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1333 const struct got_error *
1334 got_repo_match_object_id(struct got_object_id **id, char **label,
1335 const char *id_str, int obj_type, int resolve_tags,
1336 struct got_repository *repo)
1338 const struct got_error *err;
1339 struct got_tag_object *tag;
1340 struct got_reference *ref = NULL;
1347 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
1350 *id = got_object_id_dup(
1351 got_object_tag_get_object_id(tag));
1353 err = got_error_from_errno("got_object_id_dup");
1354 else if (label && asprintf(label, "refs/tags/%s",
1355 got_object_tag_get_name(tag)) == -1) {
1356 err = got_error_from_errno("asprintf");
1360 got_object_tag_close(tag);
1362 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1363 err->code != GOT_ERR_NO_OBJ)
1367 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1369 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1371 err = got_ref_open(&ref, repo, id_str, 0);
1375 *label = strdup(got_ref_get_name(ref));
1376 if (*label == NULL) {
1377 err = got_error_from_errno("strdup");
1381 err = got_ref_resolve(id, repo, ref);
1383 err = got_object_id_str(label, *id);
1384 if (*label == NULL) {
1385 err = got_error_from_errno("strdup");
1395 const struct got_error *
1396 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1397 int obj_type, struct got_repository *repo)
1399 const struct got_error *err;
1400 struct got_reflist_head refs;
1401 struct got_reflist_entry *re;
1402 struct got_object_id *tag_id;
1404 SIMPLEQ_INIT(&refs);
1407 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_by_name, NULL);
1411 SIMPLEQ_FOREACH(re, &refs, entry) {
1412 const char *refname;
1413 refname = got_ref_get_name(re->ref);
1414 if (got_ref_is_symbolic(re->ref))
1416 refname += strlen("refs/tags/");
1417 if (strcmp(refname, name) != 0)
1419 err = got_ref_resolve(&tag_id, repo, re->ref);
1422 err = got_object_open_as_tag(tag, repo, tag_id);
1426 if (obj_type == GOT_OBJ_TYPE_ANY ||
1427 got_object_tag_get_object_type(*tag) == obj_type)
1429 got_object_tag_close(*tag);
1433 got_ref_list_free(&refs);
1434 if (err == NULL && *tag == NULL)
1435 err = got_error_path(name, GOT_ERR_NO_OBJ);
1439 static const struct got_error *
1440 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1441 const char *name, mode_t mode, struct got_object_id *blob_id)
1443 const struct got_error *err = NULL;
1447 *new_te = calloc(1, sizeof(**new_te));
1448 if (*new_te == NULL)
1449 return got_error_from_errno("calloc");
1451 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1452 sizeof((*new_te)->name)) {
1453 err = got_error(GOT_ERR_NO_SPACE);
1457 (*new_te)->mode = S_IFREG | (mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
1458 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1460 if (err && *new_te) {
1467 static const struct got_error *
1468 import_file(struct got_tree_entry **new_te, struct dirent *de,
1469 const char *path, struct got_repository *repo)
1471 const struct got_error *err;
1472 struct got_object_id *blob_id = NULL;
1476 if (asprintf(&filepath, "%s%s%s", path,
1477 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1478 return got_error_from_errno("asprintf");
1480 if (lstat(filepath, &sb) != 0) {
1481 err = got_error_from_errno2("lstat", path);
1485 err = got_object_blob_create(&blob_id, filepath, repo);
1489 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1498 static const struct got_error *
1499 insert_tree_entry(struct got_tree_entry *new_te,
1500 struct got_pathlist_head *paths)
1502 const struct got_error *err = NULL;
1503 struct got_pathlist_entry *new_pe;
1505 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1509 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1513 static const struct got_error *write_tree(struct got_object_id **,
1514 const char *, struct got_pathlist_head *, struct got_repository *,
1515 got_repo_import_cb progress_cb, void *progress_arg);
1517 static const struct got_error *
1518 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1519 const char *path, struct got_pathlist_head *ignores,
1520 struct got_repository *repo,
1521 got_repo_import_cb progress_cb, void *progress_arg)
1523 const struct got_error *err;
1524 struct got_object_id *id = NULL;
1527 if (asprintf(&subdirpath, "%s%s%s", path,
1528 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1529 return got_error_from_errno("asprintf");
1531 (*new_te) = calloc(1, sizeof(**new_te));
1532 if (*new_te == NULL)
1533 return got_error_from_errno("calloc");
1534 (*new_te)->mode = S_IFDIR;
1535 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1536 sizeof((*new_te)->name)) {
1537 err = got_error(GOT_ERR_NO_SPACE);
1540 err = write_tree(&id, subdirpath, ignores, repo,
1541 progress_cb, progress_arg);
1544 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1556 static const struct got_error *
1557 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1558 struct got_pathlist_head *ignores, struct got_repository *repo,
1559 got_repo_import_cb progress_cb, void *progress_arg)
1561 const struct got_error *err = NULL;
1565 struct got_tree_entry *new_te = NULL;
1566 struct got_pathlist_head paths;
1567 struct got_pathlist_entry *pe;
1569 *new_tree_id = NULL;
1573 dir = opendir(path_dir);
1575 err = got_error_from_errno2("opendir", path_dir);
1580 while ((de = readdir(dir)) != NULL) {
1584 if (strcmp(de->d_name, ".") == 0 ||
1585 strcmp(de->d_name, "..") == 0)
1588 TAILQ_FOREACH(pe, ignores, entry) {
1589 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1597 err = got_path_dirent_type(&type, path_dir, de);
1601 if (type == DT_DIR) {
1602 err = import_subdir(&new_te, de, path_dir,
1603 ignores, repo, progress_cb, progress_arg);
1605 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1610 } else if (type == DT_REG) {
1611 err = import_file(&new_te, de, path_dir, repo);
1617 err = insert_tree_entry(new_te, &paths);
1623 if (TAILQ_EMPTY(&paths)) {
1624 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1628 TAILQ_FOREACH(pe, &paths, entry) {
1629 struct got_tree_entry *te = pe->data;
1631 if (!S_ISREG(te->mode))
1633 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1634 err = got_error_from_errno("asprintf");
1637 err = (*progress_cb)(progress_arg, path);
1643 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1647 got_pathlist_free(&paths);
1651 const struct got_error *
1652 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1653 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1654 struct got_repository *repo, got_repo_import_cb progress_cb,
1657 const struct got_error *err;
1658 struct got_object_id *new_tree_id;
1660 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1661 progress_cb, progress_arg);
1665 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1666 author, time(NULL), author, time(NULL), logmsg, repo);