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);
156 static const struct got_error *
157 get_path_gitconfig(char **p, struct got_repository *repo)
159 *p = get_path_git_child(repo, GOT_GITCONFIG);
161 return got_error_from_errno("asprintf");
166 got_repo_get_gitconfig_remotes(int *nremotes, struct got_remote_repo **remotes,
167 struct got_repository *repo)
169 *nremotes = repo->ngitconfig_remotes;
170 *remotes = repo->gitconfig_remotes;
174 is_git_repo(struct got_repository *repo)
176 const char *path_git = got_repo_get_path_git_dir(repo);
177 char *path_objects = got_repo_get_path_objects(repo);
178 char *path_refs = got_repo_get_path_refs(repo);
179 char *path_head = get_path_head(repo);
182 struct got_reference *head_ref;
184 if (lstat(path_git, &sb) == -1)
186 if (!S_ISDIR(sb.st_mode))
189 if (lstat(path_objects, &sb) == -1)
191 if (!S_ISDIR(sb.st_mode))
194 if (lstat(path_refs, &sb) == -1)
196 if (!S_ISDIR(sb.st_mode))
199 if (lstat(path_head, &sb) == -1)
201 if (!S_ISREG(sb.st_mode))
204 /* Check if the HEAD reference can be opened. */
205 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
207 got_ref_close(head_ref);
218 const struct got_error *
219 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
220 struct got_object *obj)
222 #ifndef GOT_NO_OBJ_CACHE
223 const struct got_error *err = NULL;
224 err = got_object_cache_add(&repo->objcache, id, obj);
226 if (err->code == GOT_ERR_OBJ_EXISTS ||
227 err->code == GOT_ERR_OBJ_TOO_LARGE)
237 got_repo_get_cached_object(struct got_repository *repo,
238 struct got_object_id *id)
240 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
243 const struct got_error *
244 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
245 struct got_tree_object *tree)
247 #ifndef GOT_NO_OBJ_CACHE
248 const struct got_error *err = NULL;
249 err = got_object_cache_add(&repo->treecache, id, tree);
251 if (err->code == GOT_ERR_OBJ_EXISTS ||
252 err->code == GOT_ERR_OBJ_TOO_LARGE)
261 struct got_tree_object *
262 got_repo_get_cached_tree(struct got_repository *repo,
263 struct got_object_id *id)
265 return (struct got_tree_object *)got_object_cache_get(
266 &repo->treecache, id);
269 const struct got_error *
270 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
271 struct got_commit_object *commit)
273 #ifndef GOT_NO_OBJ_CACHE
274 const struct got_error *err = NULL;
275 err = got_object_cache_add(&repo->commitcache, id, commit);
277 if (err->code == GOT_ERR_OBJ_EXISTS ||
278 err->code == GOT_ERR_OBJ_TOO_LARGE)
287 struct got_commit_object *
288 got_repo_get_cached_commit(struct got_repository *repo,
289 struct got_object_id *id)
291 return (struct got_commit_object *)got_object_cache_get(
292 &repo->commitcache, id);
295 const struct got_error *
296 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
297 struct got_tag_object *tag)
299 #ifndef GOT_NO_OBJ_CACHE
300 const struct got_error *err = NULL;
301 err = got_object_cache_add(&repo->tagcache, id, tag);
303 if (err->code == GOT_ERR_OBJ_EXISTS ||
304 err->code == GOT_ERR_OBJ_TOO_LARGE)
313 struct got_tag_object *
314 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
316 return (struct got_tag_object *)got_object_cache_get(
317 &repo->tagcache, id);
320 const struct got_error *
321 open_repo(struct got_repository *repo, const char *path)
323 const struct got_error *err = NULL;
325 /* bare git repository? */
326 repo->path_git_dir = strdup(path);
327 if (repo->path_git_dir == NULL)
328 return got_error_from_errno("strdup");
329 if (is_git_repo(repo)) {
330 repo->path = strdup(repo->path_git_dir);
331 if (repo->path == NULL) {
332 err = got_error_from_errno("strdup");
338 /* git repository with working tree? */
339 free(repo->path_git_dir);
340 repo->path_git_dir = NULL;
341 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
342 err = got_error_from_errno("asprintf");
345 if (is_git_repo(repo)) {
346 repo->path = strdup(path);
347 if (repo->path == NULL) {
348 err = got_error_from_errno("strdup");
354 err = got_error(GOT_ERR_NOT_GIT_REPO);
359 free(repo->path_git_dir);
360 repo->path_git_dir = NULL;
365 static const struct got_error *
366 parse_gitconfig_file(int *gitconfig_repository_format_version,
367 char **gitconfig_author_name, char **gitconfig_author_email,
368 struct got_remote_repo **remotes, int *nremotes,
369 char **gitconfig_owner,
370 const char *gitconfig_path)
372 const struct got_error *err = NULL, *child_err = NULL;
374 int imsg_fds[2] = { -1, -1 };
376 struct imsgbuf *ibuf;
378 *gitconfig_repository_format_version = 0;
379 *gitconfig_author_name = NULL;
380 *gitconfig_author_email = NULL;
382 fd = open(gitconfig_path, O_RDONLY);
386 return got_error_from_errno2("open", gitconfig_path);
389 ibuf = calloc(1, sizeof(*ibuf));
391 err = got_error_from_errno("calloc");
395 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
396 err = got_error_from_errno("socketpair");
402 err = got_error_from_errno("fork");
404 } else if (pid == 0) {
405 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
410 if (close(imsg_fds[1]) == -1) {
411 err = got_error_from_errno("close");
415 imsg_init(ibuf, imsg_fds[0]);
417 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
422 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
426 err = got_privsep_recv_gitconfig_int(
427 gitconfig_repository_format_version, ibuf);
431 err = got_privsep_send_gitconfig_author_name_req(ibuf);
435 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
439 err = got_privsep_send_gitconfig_author_email_req(ibuf);
443 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
447 if (remotes && nremotes) {
448 err = got_privsep_send_gitconfig_remotes_req(ibuf);
452 err = got_privsep_recv_gitconfig_remotes(remotes,
458 if (gitconfig_owner) {
459 err = got_privsep_send_gitconfig_owner_req(ibuf);
462 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
468 err = got_privsep_send_stop(imsg_fds[0]);
469 child_err = got_privsep_wait_for_child(pid);
470 if (child_err && err == NULL)
473 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
474 err = got_error_from_errno("close");
475 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
476 err = got_error_from_errno("close");
477 if (fd != -1 && close(fd) == -1 && err == NULL)
478 err = got_error_from_errno2("close", gitconfig_path);
483 static const struct got_error *
484 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
486 const struct got_error *err = NULL;
487 char *repo_gitconfig_path = NULL;
489 if (global_gitconfig_path) {
490 /* Read settings from ~/.gitconfig. */
491 int dummy_repo_version;
492 err = parse_gitconfig_file(&dummy_repo_version,
493 &repo->global_gitconfig_author_name,
494 &repo->global_gitconfig_author_email,
495 NULL, NULL, NULL, global_gitconfig_path);
500 /* Read repository's .git/config file. */
501 err = get_path_gitconfig(&repo_gitconfig_path, repo);
505 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
506 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
507 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
508 &repo->gitconfig_owner, repo_gitconfig_path);
512 free(repo_gitconfig_path);
516 const struct got_error *
517 got_repo_open(struct got_repository **repop, const char *path,
518 const char *global_gitconfig_path)
520 struct got_repository *repo = NULL;
521 const struct got_error *err = NULL;
523 int i, tried_root = 0;
527 if (got_path_is_absolute(path))
528 abspath = strdup(path);
530 abspath = got_path_get_absolute(path);
532 return got_error_path(path, GOT_ERR_BAD_PATH);
534 repo = calloc(1, sizeof(*repo));
536 err = got_error_from_errno("calloc");
540 for (i = 0; i < nitems(repo->privsep_children); i++) {
541 memset(&repo->privsep_children[i], 0,
542 sizeof(repo->privsep_children[0]));
543 repo->privsep_children[i].imsg_fd = -1;
546 err = got_object_cache_init(&repo->objcache,
547 GOT_OBJECT_CACHE_TYPE_OBJ);
550 err = got_object_cache_init(&repo->treecache,
551 GOT_OBJECT_CACHE_TYPE_TREE);
554 err = got_object_cache_init(&repo->commitcache,
555 GOT_OBJECT_CACHE_TYPE_COMMIT);
558 err = got_object_cache_init(&repo->tagcache,
559 GOT_OBJECT_CACHE_TYPE_TAG);
563 path = realpath(abspath, NULL);
565 err = got_error_from_errno2("realpath", abspath);
570 err = open_repo(repo, path);
573 if (err->code != GOT_ERR_NOT_GIT_REPO)
575 if (path[0] == '/' && path[1] == '\0') {
577 err = got_error(GOT_ERR_NOT_GIT_REPO);
582 path = dirname(path);
584 err = got_error_from_errno2("dirname", path);
589 err = read_gitconfig(repo, global_gitconfig_path);
592 if (repo->gitconfig_repository_format_version != 0)
593 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
596 got_repo_close(repo);
603 const struct got_error *
604 got_repo_close(struct got_repository *repo)
606 const struct got_error *err = NULL, *child_err;
609 for (i = 0; i < nitems(repo->packidx_cache); i++) {
610 if (repo->packidx_cache[i] == NULL)
612 got_packidx_close(repo->packidx_cache[i]);
615 for (i = 0; i < nitems(repo->packs); i++) {
616 if (repo->packs[i].path_packfile == NULL)
618 got_pack_close(&repo->packs[i]);
622 free(repo->path_git_dir);
624 got_object_cache_close(&repo->objcache);
625 got_object_cache_close(&repo->treecache);
626 got_object_cache_close(&repo->commitcache);
627 got_object_cache_close(&repo->tagcache);
629 for (i = 0; i < nitems(repo->privsep_children); i++) {
630 if (repo->privsep_children[i].imsg_fd == -1)
632 imsg_clear(repo->privsep_children[i].ibuf);
633 free(repo->privsep_children[i].ibuf);
634 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
635 child_err = got_privsep_wait_for_child(
636 repo->privsep_children[i].pid);
637 if (child_err && err == NULL)
639 if (close(repo->privsep_children[i].imsg_fd) != 0 &&
641 err = got_error_from_errno("close");
644 free(repo->gitconfig_author_name);
645 free(repo->gitconfig_author_email);
646 for (i = 0; i < repo->ngitconfig_remotes; i++) {
647 free(repo->gitconfig_remotes[i].name);
648 free(repo->gitconfig_remotes[i].url);
650 free(repo->gitconfig_remotes);
656 const struct got_error *
657 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
658 const char *input_path, int check_disk)
660 const struct got_error *err = NULL;
661 const char *repo_abspath = NULL;
663 char *canonpath, *path = NULL;
665 *in_repo_path = NULL;
667 canonpath = strdup(input_path);
668 if (canonpath == NULL) {
669 err = got_error_from_errno("strdup");
672 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
676 repo_abspath = got_repo_get_path(repo);
678 if (!check_disk || canonpath[0] == '\0') {
679 path = strdup(canonpath);
681 err = got_error_from_errno("strdup");
685 path = realpath(canonpath, NULL);
687 if (errno != ENOENT) {
688 err = got_error_from_errno2("realpath",
693 * Path is not on disk.
694 * Assume it is already relative to repository root.
696 path = strdup(canonpath);
698 err = got_error_from_errno("strdup");
703 repolen = strlen(repo_abspath);
707 if (strcmp(path, repo_abspath) == 0) {
711 err = got_error_from_errno("strdup");
714 } else if (len > repolen &&
715 got_path_is_child(path, repo_abspath, repolen)) {
716 /* Matched an on-disk path inside repository. */
717 if (got_repo_is_bare(repo)) {
719 * Matched an on-disk path inside repository
720 * database. Treat as repository-relative.
724 /* Strip common prefix with repository path. */
725 err = got_path_skip_common_ancestor(&child,
734 * Matched unrelated on-disk path.
735 * Treat it as repository-relative.
740 /* Make in-repository path absolute */
741 if (path[0] != '/') {
743 if (asprintf(&abspath, "/%s", path) == -1) {
744 err = got_error_from_errno("asprintf");
756 *in_repo_path = path;
760 static const struct got_error *
761 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
762 const char *path_packidx)
764 const struct got_error *err = NULL;
767 for (i = 0; i < nitems(repo->packidx_cache); i++) {
768 if (repo->packidx_cache[i] == NULL)
770 if (strcmp(repo->packidx_cache[i]->path_packidx,
771 path_packidx) == 0) {
772 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
775 if (i == nitems(repo->packidx_cache)) {
776 err = got_packidx_close(repo->packidx_cache[i - 1]);
782 * Insert the new pack index at the front so it will
783 * be searched first in the future.
785 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
786 sizeof(repo->packidx_cache) -
787 sizeof(repo->packidx_cache[0]));
788 repo->packidx_cache[0] = packidx;
794 is_packidx_filename(const char *name, size_t len)
796 if (len != GOT_PACKIDX_NAMELEN)
799 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
802 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
803 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
809 const struct got_error *
810 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
811 struct got_repository *repo, struct got_object_id *id)
813 const struct got_error *err;
820 /* Search pack index cache. */
821 for (i = 0; i < nitems(repo->packidx_cache); i++) {
822 if (repo->packidx_cache[i] == NULL)
824 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
826 *packidx = repo->packidx_cache[i];
828 * Move this cache entry to the front. Repeatedly
829 * searching a wrong pack index can be expensive.
832 struct got_packidx *p;
833 p = repo->packidx_cache[0];
834 repo->packidx_cache[0] = *packidx;
835 repo->packidx_cache[i] = p;
840 /* No luck. Search the filesystem. */
842 path_packdir = got_repo_get_path_objects_pack(repo);
843 if (path_packdir == NULL)
844 return got_error_from_errno("got_repo_get_path_objects_pack");
846 packdir = opendir(path_packdir);
847 if (packdir == NULL) {
849 err = got_error_no_obj(id);
851 err = got_error_from_errno2("opendir", path_packdir);
855 while ((dent = readdir(packdir)) != NULL) {
858 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
861 if (asprintf(&path_packidx, "%s/%s", path_packdir,
862 dent->d_name) == -1) {
863 err = got_error_from_errno("asprintf");
867 for (i = 0; i < nitems(repo->packidx_cache); i++) {
868 if (repo->packidx_cache[i] == NULL)
870 if (strcmp(repo->packidx_cache[i]->path_packidx,
871 path_packidx) == 0) {
878 continue; /* already searched */
881 err = got_packidx_open(packidx, path_packidx, 0);
887 err = cache_packidx(repo, *packidx, path_packidx);
892 *idx = got_packidx_get_object_idx(*packidx, id);
894 err = NULL; /* found the object */
899 err = got_error_no_obj(id);
902 if (packdir && closedir(packdir) != 0 && err == NULL)
903 err = got_error_from_errno("closedir");
907 static const struct got_error *
908 read_packfile_hdr(int fd, struct got_packidx *packidx)
910 const struct got_error *err = NULL;
911 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
912 struct got_packfile_hdr hdr;
915 n = read(fd, &hdr, sizeof(hdr));
917 return got_error_from_errno("read");
918 if (n != sizeof(hdr))
919 return got_error(GOT_ERR_BAD_PACKFILE);
921 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
922 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
923 betoh32(hdr.nobjects) != totobj)
924 err = got_error(GOT_ERR_BAD_PACKFILE);
929 static const struct got_error *
930 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
932 const struct got_error *err = NULL;
934 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
936 return got_error_from_errno2("open", path_packfile);
939 err = read_packfile_hdr(*fd, packidx);
949 const struct got_error *
950 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
951 const char *path_packfile, struct got_packidx *packidx)
953 const struct got_error *err = NULL;
954 struct got_pack *pack = NULL;
961 for (i = 0; i < nitems(repo->packs); i++) {
962 pack = &repo->packs[i];
963 if (pack->path_packfile == NULL)
965 if (strcmp(pack->path_packfile, path_packfile) == 0)
966 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
969 if (i == nitems(repo->packs) - 1) {
970 err = got_pack_close(&repo->packs[i - 1]);
973 memmove(&repo->packs[1], &repo->packs[0],
974 sizeof(repo->packs) - sizeof(repo->packs[0]));
978 pack = &repo->packs[i];
980 pack->path_packfile = strdup(path_packfile);
981 if (pack->path_packfile == NULL) {
982 err = got_error_from_errno("strdup");
986 err = open_packfile(&pack->fd, path_packfile, packidx);
990 if (fstat(pack->fd, &sb) != 0) {
991 err = got_error_from_errno("fstat");
994 pack->filesize = sb.st_size;
996 pack->privsep_child = NULL;
998 #ifndef GOT_PACK_NO_MMAP
999 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1001 if (pack->map == MAP_FAILED) {
1002 if (errno != ENOMEM) {
1003 err = got_error_from_errno("mmap");
1006 pack->map = NULL; /* fall back to read(2) */
1012 free(pack->path_packfile);
1013 memset(pack, 0, sizeof(*pack));
1021 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1023 struct got_pack *pack = NULL;
1026 for (i = 0; i < nitems(repo->packs); i++) {
1027 pack = &repo->packs[i];
1028 if (pack->path_packfile == NULL)
1030 if (strcmp(pack->path_packfile, path_packfile) == 0)
1037 const struct got_error *
1038 got_repo_init(const char *repo_path)
1040 const struct got_error *err = NULL;
1041 const char *dirnames[] = {
1043 GOT_OBJECTS_PACK_DIR,
1046 const char *description_str = "Unnamed repository; "
1047 "edit this file 'description' to name the repository.";
1048 const char *headref_str = "ref: refs/heads/main";
1049 const char *gitconfig_str = "[core]\n"
1050 "\trepositoryformatversion = 0\n"
1051 "\tfilemode = true\n"
1056 if (!got_path_dir_is_empty(repo_path))
1057 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1059 for (i = 0; i < nitems(dirnames); i++) {
1060 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1061 return got_error_from_errno("asprintf");
1063 err = got_path_mkdir(path);
1069 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1070 return got_error_from_errno("asprintf");
1071 err = got_path_create_file(path, description_str);
1076 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1077 return got_error_from_errno("asprintf");
1078 err = got_path_create_file(path, headref_str);
1083 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1084 return got_error_from_errno("asprintf");
1085 err = got_path_create_file(path, gitconfig_str);
1093 static const struct got_error *
1094 match_packed_object(struct got_object_id **unique_id,
1095 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1097 const struct got_error *err = NULL;
1100 struct dirent *dent;
1102 struct got_object_id_queue matched_ids;
1104 SIMPLEQ_INIT(&matched_ids);
1106 path_packdir = got_repo_get_path_objects_pack(repo);
1107 if (path_packdir == NULL)
1108 return got_error_from_errno("got_repo_get_path_objects_pack");
1110 packdir = opendir(path_packdir);
1111 if (packdir == NULL) {
1112 if (errno != ENOENT)
1113 err = got_error_from_errno2("opendir", path_packdir);
1117 while ((dent = readdir(packdir)) != NULL) {
1118 struct got_packidx *packidx;
1119 struct got_object_qid *qid;
1122 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1125 if (asprintf(&path_packidx, "%s/%s", path_packdir,
1126 dent->d_name) == -1) {
1127 err = got_error_from_errno("asprintf");
1131 err = got_packidx_open(&packidx, path_packidx, 0);
1136 err = got_packidx_match_id_str_prefix(&matched_ids,
1137 packidx, id_str_prefix);
1139 got_packidx_close(packidx);
1142 err = got_packidx_close(packidx);
1146 SIMPLEQ_FOREACH(qid, &matched_ids, entry) {
1147 if (obj_type != GOT_OBJ_TYPE_ANY) {
1149 err = got_object_get_type(&matched_type, repo,
1153 if (matched_type != obj_type)
1156 if (*unique_id == NULL) {
1157 *unique_id = got_object_id_dup(qid->id);
1158 if (*unique_id == NULL) {
1159 err = got_error_from_errno("malloc");
1163 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1164 continue; /* packed multiple times */
1165 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1171 got_object_id_queue_free(&matched_ids);
1173 if (packdir && closedir(packdir) != 0 && err == NULL)
1174 err = got_error_from_errno("closedir");
1182 static const struct got_error *
1183 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1184 const char *object_dir, const char *id_str_prefix, int obj_type,
1185 struct got_repository *repo)
1187 const struct got_error *err = NULL;
1190 struct dirent *dent;
1191 struct got_object_id id;
1193 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1194 err = got_error_from_errno("asprintf");
1198 dir = opendir(path);
1200 if (errno == ENOENT) {
1204 err = got_error_from_errno2("opendir", path);
1207 while ((dent = readdir(dir)) != NULL) {
1211 if (strcmp(dent->d_name, ".") == 0 ||
1212 strcmp(dent->d_name, "..") == 0)
1215 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1216 err = got_error_from_errno("asprintf");
1220 if (!got_parse_sha1_digest(id.sha1, id_str))
1224 * Directory entries do not necessarily appear in
1225 * sorted order, so we must iterate over all of them.
1227 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1233 if (*unique_id == NULL) {
1234 if (obj_type != GOT_OBJ_TYPE_ANY) {
1236 err = got_object_get_type(&matched_type, repo,
1240 if (matched_type != obj_type)
1243 *unique_id = got_object_id_dup(&id);
1244 if (*unique_id == NULL) {
1245 err = got_error_from_errno("got_object_id_dup");
1250 if (got_object_id_cmp(*unique_id, &id) == 0)
1251 continue; /* both packed and loose */
1252 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1258 if (dir && closedir(dir) != 0 && err == NULL)
1259 err = got_error_from_errno("closedir");
1268 const struct got_error *
1269 got_repo_match_object_id_prefix(struct got_object_id **id,
1270 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1272 const struct got_error *err = NULL;
1273 char *path_objects = got_repo_get_path_objects(repo);
1274 char *object_dir = NULL;
1280 for (i = 0; i < strlen(id_str_prefix); i++) {
1281 if (isxdigit((unsigned char)id_str_prefix[i]))
1283 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1286 len = strlen(id_str_prefix);
1288 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1291 object_dir = strndup(id_str_prefix, 2);
1292 if (object_dir == NULL) {
1293 err = got_error_from_errno("strdup");
1296 err = match_loose_object(id, path_objects, object_dir,
1297 id_str_prefix, obj_type, repo);
1298 } else if (len == 1) {
1300 for (i = 0; i < 0xf; i++) {
1301 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1303 err = got_error_from_errno("asprintf");
1306 err = match_packed_object(id, repo, object_dir,
1310 err = match_loose_object(id, path_objects, object_dir,
1311 id_str_prefix, obj_type, repo);
1316 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1324 } else if (*id == NULL)
1325 err = got_error(GOT_ERR_NO_OBJ);
1330 const struct got_error *
1331 got_repo_match_object_id(struct got_object_id **id, char **label,
1332 const char *id_str, int obj_type, int resolve_tags,
1333 struct got_repository *repo)
1335 const struct got_error *err;
1336 struct got_tag_object *tag;
1337 struct got_reference *ref = NULL;
1344 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
1347 *id = got_object_id_dup(
1348 got_object_tag_get_object_id(tag));
1350 err = got_error_from_errno("got_object_id_dup");
1351 else if (label && asprintf(label, "refs/tags/%s",
1352 got_object_tag_get_name(tag)) == -1) {
1353 err = got_error_from_errno("asprintf");
1357 got_object_tag_close(tag);
1359 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1360 err->code != GOT_ERR_NO_OBJ)
1364 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1366 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1368 err = got_ref_open(&ref, repo, id_str, 0);
1372 *label = strdup(got_ref_get_name(ref));
1373 if (*label == NULL) {
1374 err = got_error_from_errno("strdup");
1378 err = got_ref_resolve(id, repo, ref);
1380 err = got_object_id_str(label, *id);
1381 if (*label == NULL) {
1382 err = got_error_from_errno("strdup");
1392 const struct got_error *
1393 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1394 int obj_type, struct got_repository *repo)
1396 const struct got_error *err;
1397 struct got_reflist_head refs;
1398 struct got_reflist_entry *re;
1399 struct got_object_id *tag_id;
1401 SIMPLEQ_INIT(&refs);
1404 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_by_name, NULL);
1408 SIMPLEQ_FOREACH(re, &refs, entry) {
1409 const char *refname;
1410 refname = got_ref_get_name(re->ref);
1411 if (got_ref_is_symbolic(re->ref))
1413 refname += strlen("refs/tags/");
1414 if (strcmp(refname, name) != 0)
1416 err = got_ref_resolve(&tag_id, repo, re->ref);
1419 err = got_object_open_as_tag(tag, repo, tag_id);
1423 if (obj_type == GOT_OBJ_TYPE_ANY ||
1424 got_object_tag_get_object_type(*tag) == obj_type)
1426 got_object_tag_close(*tag);
1430 got_ref_list_free(&refs);
1431 if (err == NULL && *tag == NULL)
1432 err = got_error(GOT_ERR_NO_OBJ);
1436 static const struct got_error *
1437 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1438 const char *name, mode_t mode, struct got_object_id *blob_id)
1440 const struct got_error *err = NULL;
1444 *new_te = calloc(1, sizeof(**new_te));
1445 if (*new_te == NULL)
1446 return got_error_from_errno("calloc");
1448 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1449 sizeof((*new_te)->name)) {
1450 err = got_error(GOT_ERR_NO_SPACE);
1454 (*new_te)->mode = S_IFREG | (mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
1455 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1457 if (err && *new_te) {
1464 static const struct got_error *
1465 import_file(struct got_tree_entry **new_te, struct dirent *de,
1466 const char *path, struct got_repository *repo)
1468 const struct got_error *err;
1469 struct got_object_id *blob_id = NULL;
1473 if (asprintf(&filepath, "%s%s%s", path,
1474 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1475 return got_error_from_errno("asprintf");
1477 if (lstat(filepath, &sb) != 0) {
1478 err = got_error_from_errno2("lstat", path);
1482 err = got_object_blob_create(&blob_id, filepath, repo);
1486 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1495 static const struct got_error *
1496 insert_tree_entry(struct got_tree_entry *new_te,
1497 struct got_pathlist_head *paths)
1499 const struct got_error *err = NULL;
1500 struct got_pathlist_entry *new_pe;
1502 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1506 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1510 static const struct got_error *write_tree(struct got_object_id **,
1511 const char *, struct got_pathlist_head *, struct got_repository *,
1512 got_repo_import_cb progress_cb, void *progress_arg);
1514 static const struct got_error *
1515 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1516 const char *path, struct got_pathlist_head *ignores,
1517 struct got_repository *repo,
1518 got_repo_import_cb progress_cb, void *progress_arg)
1520 const struct got_error *err;
1521 struct got_object_id *id = NULL;
1524 if (asprintf(&subdirpath, "%s%s%s", path,
1525 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1526 return got_error_from_errno("asprintf");
1528 (*new_te) = calloc(1, sizeof(**new_te));
1529 if (*new_te == NULL)
1530 return got_error_from_errno("calloc");
1531 (*new_te)->mode = S_IFDIR;
1532 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1533 sizeof((*new_te)->name)) {
1534 err = got_error(GOT_ERR_NO_SPACE);
1537 err = write_tree(&id, subdirpath, ignores, repo,
1538 progress_cb, progress_arg);
1541 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1553 static const struct got_error *
1554 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1555 struct got_pathlist_head *ignores, struct got_repository *repo,
1556 got_repo_import_cb progress_cb, void *progress_arg)
1558 const struct got_error *err = NULL;
1562 struct got_tree_entry *new_te = NULL;
1563 struct got_pathlist_head paths;
1564 struct got_pathlist_entry *pe;
1566 *new_tree_id = NULL;
1570 dir = opendir(path_dir);
1572 err = got_error_from_errno2("opendir", path_dir);
1577 while ((de = readdir(dir)) != NULL) {
1580 if (strcmp(de->d_name, ".") == 0 ||
1581 strcmp(de->d_name, "..") == 0)
1584 TAILQ_FOREACH(pe, ignores, entry) {
1585 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1592 if (de->d_type == DT_DIR) {
1593 err = import_subdir(&new_te, de, path_dir,
1594 ignores, repo, progress_cb, progress_arg);
1596 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1601 } else if (de->d_type == DT_REG) {
1602 err = import_file(&new_te, de, path_dir, repo);
1608 err = insert_tree_entry(new_te, &paths);
1614 if (TAILQ_EMPTY(&paths)) {
1615 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1619 TAILQ_FOREACH(pe, &paths, entry) {
1620 struct got_tree_entry *te = pe->data;
1622 if (!S_ISREG(te->mode))
1624 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1625 err = got_error_from_errno("asprintf");
1628 err = (*progress_cb)(progress_arg, path);
1634 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1638 got_pathlist_free(&paths);
1642 const struct got_error *
1643 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1644 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1645 struct got_repository *repo, got_repo_import_cb progress_cb,
1648 const struct got_error *err;
1649 struct got_object_id *new_tree_id;
1651 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1652 progress_cb, progress_arg);
1656 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1657 author, time(NULL), author, time(NULL), logmsg, repo);