2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
18 #include <sys/queue.h>
20 #include <sys/socket.h>
23 #include <sys/syslimits.h>
42 #include "got_error.h"
43 #include "got_reference.h"
44 #include "got_repository.h"
46 #include "got_cancel.h"
47 #include "got_worktree.h"
48 #include "got_object.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_object.h"
53 #include "got_lib_object_parse.h"
54 #include "got_lib_object_create.h"
55 #include "got_lib_pack.h"
56 #include "got_lib_privsep.h"
57 #include "got_lib_worktree.h"
58 #include "got_lib_sha1.h"
59 #include "got_lib_object_cache.h"
60 #include "got_lib_repository.h"
63 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
66 #define GOT_GIT_DIR ".git"
68 /* Mandatory files and directories inside the git directory. */
69 #define GOT_OBJECTS_DIR "objects"
70 #define GOT_REFS_DIR "refs"
71 #define GOT_HEAD_FILE "HEAD"
72 #define GOT_GITCONFIG "config"
74 /* Other files and directories inside the git directory. */
75 #define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
76 #define GOT_ORIG_HEAD_FILE "ORIG_HEAD"
77 #define GOT_OBJECTS_PACK_DIR "objects/pack"
78 #define GOT_PACKED_REFS_FILE "packed-refs"
81 got_repo_get_path(struct got_repository *repo)
87 got_repo_get_path_git_dir(struct got_repository *repo)
89 return repo->path_git_dir;
93 got_repo_get_gitconfig_author_name(struct got_repository *repo)
95 return repo->gitconfig_author_name;
99 got_repo_get_gitconfig_author_email(struct got_repository *repo)
101 return repo->gitconfig_author_email;
105 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
107 return repo->global_gitconfig_author_name;
111 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
113 return repo->global_gitconfig_author_email;
117 got_repo_is_bare(struct got_repository *repo)
119 return (strcmp(repo->path, repo->path_git_dir) == 0);
123 get_path_git_child(struct got_repository *repo, const char *basename)
127 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
135 got_repo_get_path_objects(struct got_repository *repo)
137 return get_path_git_child(repo, GOT_OBJECTS_DIR);
141 got_repo_get_path_objects_pack(struct got_repository *repo)
143 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
147 got_repo_get_path_refs(struct got_repository *repo)
149 return get_path_git_child(repo, GOT_REFS_DIR);
153 got_repo_get_path_packed_refs(struct got_repository *repo)
155 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
159 get_path_head(struct got_repository *repo)
161 return get_path_git_child(repo, GOT_HEAD_FILE);
164 static const struct got_error *
165 get_path_gitconfig(char **p, struct got_repository *repo)
167 *p = get_path_git_child(repo, GOT_GITCONFIG);
169 return got_error_from_errno("asprintf");
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 const char *gitconfig_path)
370 const struct got_error *err = NULL, *child_err = NULL;
372 int imsg_fds[2] = { -1, -1 };
374 struct imsgbuf *ibuf;
376 *gitconfig_repository_format_version = 0;
377 *gitconfig_author_name = NULL;
378 *gitconfig_author_email = NULL;
380 fd = open(gitconfig_path, O_RDONLY);
384 return got_error_from_errno2("open", gitconfig_path);
387 ibuf = calloc(1, sizeof(*ibuf));
389 err = got_error_from_errno("calloc");
393 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
394 err = got_error_from_errno("socketpair");
400 err = got_error_from_errno("fork");
402 } else if (pid == 0) {
403 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
408 if (close(imsg_fds[1]) == -1) {
409 err = got_error_from_errno("close");
413 imsg_init(ibuf, imsg_fds[0]);
415 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
420 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
424 err = got_privsep_recv_gitconfig_int(
425 gitconfig_repository_format_version, ibuf);
429 err = got_privsep_send_gitconfig_author_name_req(ibuf);
433 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
437 err = got_privsep_send_gitconfig_author_email_req(ibuf);
441 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
446 err = got_privsep_send_stop(imsg_fds[0]);
447 child_err = got_privsep_wait_for_child(pid);
448 if (child_err && err == NULL)
451 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
452 err = got_error_from_errno("close");
453 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
454 err = got_error_from_errno("close");
455 if (fd != -1 && close(fd) == -1 && err == NULL)
456 err = got_error_from_errno2("close", gitconfig_path);
461 static const struct got_error *
462 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
464 const struct got_error *err = NULL;
465 char *repo_gitconfig_path = NULL;
467 if (global_gitconfig_path) {
468 /* Read settings from ~/.gitconfig. */
469 int dummy_repo_version;
470 err = parse_gitconfig_file(&dummy_repo_version,
471 &repo->global_gitconfig_author_name,
472 &repo->global_gitconfig_author_email,
473 global_gitconfig_path);
478 /* Read repository's .git/config file. */
479 err = get_path_gitconfig(&repo_gitconfig_path, repo);
483 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
484 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
485 repo_gitconfig_path);
489 free(repo_gitconfig_path);
493 const struct got_error *
494 got_repo_open(struct got_repository **repop, const char *path,
495 const char *global_gitconfig_path)
497 struct got_repository *repo = NULL;
498 const struct got_error *err = NULL;
500 int i, tried_root = 0;
504 if (got_path_is_absolute(path))
505 abspath = strdup(path);
507 abspath = got_path_get_absolute(path);
509 return got_error(GOT_ERR_BAD_PATH);
511 repo = calloc(1, sizeof(*repo));
513 err = got_error_from_errno("calloc");
517 for (i = 0; i < nitems(repo->privsep_children); i++) {
518 memset(&repo->privsep_children[i], 0,
519 sizeof(repo->privsep_children[0]));
520 repo->privsep_children[i].imsg_fd = -1;
523 err = got_object_cache_init(&repo->objcache,
524 GOT_OBJECT_CACHE_TYPE_OBJ);
527 err = got_object_cache_init(&repo->treecache,
528 GOT_OBJECT_CACHE_TYPE_TREE);
531 err = got_object_cache_init(&repo->commitcache,
532 GOT_OBJECT_CACHE_TYPE_COMMIT);
535 err = got_object_cache_init(&repo->tagcache,
536 GOT_OBJECT_CACHE_TYPE_TAG);
540 path = realpath(abspath, NULL);
542 err = got_error_from_errno2("realpath", abspath);
547 err = open_repo(repo, path);
550 if (err->code != GOT_ERR_NOT_GIT_REPO)
552 if (path[0] == '/' && path[1] == '\0') {
554 err = got_error(GOT_ERR_NOT_GIT_REPO);
559 path = dirname(path);
561 err = got_error_from_errno2("dirname", path);
566 err = read_gitconfig(repo, global_gitconfig_path);
569 if (repo->gitconfig_repository_format_version != 0)
570 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
573 got_repo_close(repo);
580 const struct got_error *
581 got_repo_close(struct got_repository *repo)
583 const struct got_error *err = NULL, *child_err;
586 for (i = 0; i < nitems(repo->packidx_cache); i++) {
587 if (repo->packidx_cache[i] == NULL)
589 got_packidx_close(repo->packidx_cache[i]);
592 for (i = 0; i < nitems(repo->packs); i++) {
593 if (repo->packs[i].path_packfile == NULL)
595 got_pack_close(&repo->packs[i]);
599 free(repo->path_git_dir);
601 got_object_cache_close(&repo->objcache);
602 got_object_cache_close(&repo->treecache);
603 got_object_cache_close(&repo->commitcache);
604 got_object_cache_close(&repo->tagcache);
606 for (i = 0; i < nitems(repo->privsep_children); i++) {
607 if (repo->privsep_children[i].imsg_fd == -1)
609 imsg_clear(repo->privsep_children[i].ibuf);
610 free(repo->privsep_children[i].ibuf);
611 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
612 child_err = got_privsep_wait_for_child(
613 repo->privsep_children[i].pid);
614 if (child_err && err == NULL)
616 if (close(repo->privsep_children[i].imsg_fd) != 0 &&
618 err = got_error_from_errno("close");
621 free(repo->gitconfig_author_name);
622 free(repo->gitconfig_author_email);
628 const struct got_error *
629 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
630 const char *input_path, int check_disk)
632 const struct got_error *err = NULL;
633 const char *repo_abspath = NULL;
634 size_t repolen, cwdlen, len;
635 char *cwd, *canonpath, *path = NULL;
637 *in_repo_path = NULL;
639 cwd = getcwd(NULL, 0);
641 return got_error_from_errno("getcwd");
643 canonpath = strdup(input_path);
644 if (canonpath == NULL) {
645 err = got_error_from_errno("strdup");
648 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
652 repo_abspath = got_repo_get_path(repo);
654 if (!check_disk || canonpath[0] == '\0') {
655 path = strdup(canonpath);
657 err = got_error_from_errno("strdup");
661 int is_repo_child = 0, is_cwd_child = 0;
663 path = realpath(canonpath, NULL);
665 if (errno != ENOENT) {
666 err = got_error_from_errno2("realpath",
671 * Path is not on disk.
672 * Assume it is already relative to repository root.
674 path = strdup(canonpath);
676 err = got_error_from_errno("strdup");
681 repolen = strlen(repo_abspath);
682 cwdlen = strlen(cwd);
685 if (len > repolen && strncmp(path, repo_abspath, repolen) == 0)
687 if (len > cwdlen && strncmp(path, cwd, cwdlen) == 0)
690 if (strcmp(path, repo_abspath) == 0) {
694 err = got_error_from_errno("strdup");
697 } else if (is_repo_child && is_cwd_child) {
699 /* Strip common prefix with repository path. */
700 err = got_path_skip_common_ancestor(&child,
706 } else if (is_repo_child) {
707 /* Matched an on-disk path inside repository. */
708 if (got_repo_is_bare(repo)) {
710 * Matched an on-disk path inside repository
711 * database. Treat as repository-relative.
715 /* Strip common prefix with repository path. */
716 err = got_path_skip_common_ancestor(&child,
723 } else if (is_cwd_child) {
725 /* Strip common prefix with cwd. */
726 err = got_path_skip_common_ancestor(&child, cwd,
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");
757 *in_repo_path = path;
761 const struct got_error *
762 got_repo_cache_packidx(struct got_repository *repo, struct got_packidx *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)
771 if (i == nitems(repo->packidx_cache)) {
772 err = got_packidx_close(repo->packidx_cache[i - 1]);
778 * Insert the new pack index at the front so it will
779 * be searched first in the future.
781 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
782 sizeof(repo->packidx_cache) -
783 sizeof(repo->packidx_cache[0]));
784 repo->packidx_cache[0] = packidx;
790 is_packidx_filename(const char *name, size_t len)
792 if (len != GOT_PACKIDX_NAMELEN)
795 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
798 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
799 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
805 const struct got_error *
806 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
807 struct got_repository *repo, struct got_object_id *id)
809 const struct got_error *err;
816 /* Search pack index cache. */
817 for (i = 0; i < nitems(repo->packidx_cache); i++) {
818 if (repo->packidx_cache[i] == NULL)
820 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
822 *packidx = repo->packidx_cache[i];
826 /* No luck. Search the filesystem. */
828 path_packdir = got_repo_get_path_objects_pack(repo);
829 if (path_packdir == NULL)
830 return got_error_from_errno("got_repo_get_path_objects_pack");
832 packdir = opendir(path_packdir);
833 if (packdir == NULL) {
835 err = got_error_no_obj(id);
837 err = got_error_from_errno2("opendir", path_packdir);
841 while ((dent = readdir(packdir)) != NULL) {
842 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
845 if (asprintf(&path_packidx, "%s/%s", path_packdir,
846 dent->d_name) == -1) {
847 err = got_error_from_errno("asprintf");
851 err = got_packidx_open(packidx, path_packidx, 0);
856 *idx = got_packidx_get_object_idx(*packidx, id);
858 err = NULL; /* found the object */
859 err = got_repo_cache_packidx(repo, *packidx);
863 err = got_packidx_close(*packidx);
869 err = got_error_no_obj(id);
872 if (packdir && closedir(packdir) != 0 && err == NULL)
873 err = got_error_from_errno("closedir");
877 static const struct got_error *
878 read_packfile_hdr(int fd, struct got_packidx *packidx)
880 const struct got_error *err = NULL;
881 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
882 struct got_packfile_hdr hdr;
885 n = read(fd, &hdr, sizeof(hdr));
887 return got_error_from_errno("read");
888 if (n != sizeof(hdr))
889 return got_error(GOT_ERR_BAD_PACKFILE);
891 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
892 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
893 betoh32(hdr.nobjects) != totobj)
894 err = got_error(GOT_ERR_BAD_PACKFILE);
899 static const struct got_error *
900 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
902 const struct got_error *err = NULL;
904 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
906 return got_error_from_errno2("open", path_packfile);
909 err = read_packfile_hdr(*fd, packidx);
919 const struct got_error *
920 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
921 const char *path_packfile, struct got_packidx *packidx)
923 const struct got_error *err = NULL;
924 struct got_pack *pack = NULL;
931 for (i = 0; i < nitems(repo->packs); i++) {
932 pack = &repo->packs[i];
933 if (pack->path_packfile == NULL)
935 if (strcmp(pack->path_packfile, path_packfile) == 0)
939 if (i == nitems(repo->packs) - 1) {
940 err = got_pack_close(&repo->packs[i - 1]);
943 memmove(&repo->packs[1], &repo->packs[0],
944 sizeof(repo->packs) - sizeof(repo->packs[0]));
948 pack = &repo->packs[i];
950 pack->path_packfile = strdup(path_packfile);
951 if (pack->path_packfile == NULL) {
952 err = got_error_from_errno("strdup");
956 err = open_packfile(&pack->fd, path_packfile, packidx);
960 if (fstat(pack->fd, &sb) != 0) {
961 err = got_error_from_errno("fstat");
964 pack->filesize = sb.st_size;
966 pack->privsep_child = NULL;
968 #ifndef GOT_PACK_NO_MMAP
969 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
971 if (pack->map == MAP_FAILED) {
972 if (errno != ENOMEM) {
973 err = got_error_from_errno("mmap");
976 pack->map = NULL; /* fall back to read(2) */
982 free(pack->path_packfile);
983 memset(pack, 0, sizeof(*pack));
991 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
993 struct got_pack *pack = NULL;
996 for (i = 0; i < nitems(repo->packs); i++) {
997 pack = &repo->packs[i];
998 if (pack->path_packfile == NULL)
1000 if (strcmp(pack->path_packfile, path_packfile) == 0)
1007 const struct got_error *
1008 got_repo_init(const char *repo_path)
1010 const struct got_error *err = NULL;
1011 const char *dirnames[] = {
1013 GOT_OBJECTS_PACK_DIR,
1016 const char *description_str = "Unnamed repository; "
1017 "edit this file 'description' to name the repository.";
1018 const char *headref_str = "ref: refs/heads/main";
1019 const char *gitconfig_str = "[core]\n"
1020 "\trepositoryformatversion = 0\n"
1021 "\tfilemode = true\n"
1026 if (!got_path_dir_is_empty(repo_path))
1027 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1029 for (i = 0; i < nitems(dirnames); i++) {
1030 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1031 return got_error_from_errno("asprintf");
1033 err = got_path_mkdir(path);
1039 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1040 return got_error_from_errno("asprintf");
1041 err = got_path_create_file(path, description_str);
1046 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1047 return got_error_from_errno("asprintf");
1048 err = got_path_create_file(path, headref_str);
1053 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1054 return got_error_from_errno("asprintf");
1055 err = got_path_create_file(path, gitconfig_str);
1063 static const struct got_error *
1064 match_packed_object(struct got_object_id **unique_id,
1065 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1067 const struct got_error *err = NULL;
1070 struct dirent *dent;
1072 struct got_object_id_queue matched_ids;
1074 SIMPLEQ_INIT(&matched_ids);
1076 path_packdir = got_repo_get_path_objects_pack(repo);
1077 if (path_packdir == NULL)
1078 return got_error_from_errno("got_repo_get_path_objects_pack");
1080 packdir = opendir(path_packdir);
1081 if (packdir == NULL) {
1082 if (errno != ENOENT)
1083 err = got_error_from_errno2("opendir", path_packdir);
1087 while ((dent = readdir(packdir)) != NULL) {
1088 struct got_packidx *packidx;
1089 struct got_object_qid *qid;
1092 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1095 if (asprintf(&path_packidx, "%s/%s", path_packdir,
1096 dent->d_name) == -1) {
1097 err = got_error_from_errno("asprintf");
1101 err = got_packidx_open(&packidx, path_packidx, 0);
1106 err = got_packidx_match_id_str_prefix(&matched_ids,
1107 packidx, id_str_prefix);
1109 got_packidx_close(packidx);
1112 err = got_packidx_close(packidx);
1116 SIMPLEQ_FOREACH(qid, &matched_ids, entry) {
1117 if (obj_type != GOT_OBJ_TYPE_ANY) {
1119 err = got_object_get_type(&matched_type, repo,
1123 if (matched_type != obj_type)
1126 if (*unique_id == NULL) {
1127 *unique_id = got_object_id_dup(qid->id);
1128 if (*unique_id == NULL) {
1129 err = got_error_from_errno("malloc");
1133 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1139 got_object_id_queue_free(&matched_ids);
1141 if (packdir && closedir(packdir) != 0 && err == NULL)
1142 err = got_error_from_errno("closedir");
1150 static const struct got_error *
1151 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1152 const char *object_dir, const char *id_str_prefix, int obj_type,
1153 struct got_repository *repo)
1155 const struct got_error *err = NULL;
1158 struct dirent *dent;
1159 struct got_object_id id;
1161 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1162 err = got_error_from_errno("asprintf");
1166 dir = opendir(path);
1168 if (errno == ENOENT) {
1172 err = got_error_from_errno2("opendir", path);
1175 while ((dent = readdir(dir)) != NULL) {
1179 if (strcmp(dent->d_name, ".") == 0 ||
1180 strcmp(dent->d_name, "..") == 0)
1183 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1184 err = got_error_from_errno("asprintf");
1188 if (!got_parse_sha1_digest(id.sha1, id_str))
1192 * Directory entries do not necessarily appear in
1193 * sorted order, so we must iterate over all of them.
1195 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1201 if (*unique_id == NULL) {
1202 if (obj_type != GOT_OBJ_TYPE_ANY) {
1204 err = got_object_get_type(&matched_type, repo,
1208 if (matched_type != obj_type)
1211 *unique_id = got_object_id_dup(&id);
1212 if (*unique_id == NULL) {
1213 err = got_error_from_errno("got_object_id_dup");
1218 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1224 if (dir && closedir(dir) != 0 && err == NULL)
1225 err = got_error_from_errno("closedir");
1234 const struct got_error *
1235 got_repo_match_object_id_prefix(struct got_object_id **id,
1236 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1238 const struct got_error *err = NULL;
1239 char *path_objects = got_repo_get_path_objects(repo);
1240 char *object_dir = NULL;
1246 for (i = 0; i < strlen(id_str_prefix); i++) {
1247 if (isxdigit((unsigned char)id_str_prefix[i]))
1249 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1252 len = strlen(id_str_prefix);
1254 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1257 object_dir = strndup(id_str_prefix, 2);
1258 if (object_dir == NULL) {
1259 err = got_error_from_errno("strdup");
1262 err = match_loose_object(id, path_objects, object_dir,
1263 id_str_prefix, obj_type, repo);
1264 } else if (len == 1) {
1266 for (i = 0; i < 0xf; i++) {
1267 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1269 err = got_error_from_errno("asprintf");
1272 err = match_packed_object(id, repo, object_dir,
1276 err = match_loose_object(id, path_objects, object_dir,
1277 id_str_prefix, obj_type, repo);
1282 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1290 } else if (*id == NULL)
1291 err = got_error(GOT_ERR_NO_OBJ);
1296 const struct got_error *
1297 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1298 int obj_type, struct got_repository *repo)
1300 const struct got_error *err;
1301 struct got_reflist_head refs;
1302 struct got_reflist_entry *re;
1303 struct got_object_id *tag_id;
1305 SIMPLEQ_INIT(&refs);
1308 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_by_name, NULL);
1312 SIMPLEQ_FOREACH(re, &refs, entry) {
1313 const char *refname;
1314 refname = got_ref_get_name(re->ref);
1315 if (got_ref_is_symbolic(re->ref))
1317 refname += strlen("refs/tags/");
1318 if (strcmp(refname, name) != 0)
1320 err = got_ref_resolve(&tag_id, repo, re->ref);
1323 err = got_object_open_as_tag(tag, repo, tag_id);
1327 if (obj_type == GOT_OBJ_TYPE_ANY ||
1328 got_object_tag_get_object_type(*tag) == obj_type)
1330 got_object_tag_close(*tag);
1334 got_ref_list_free(&refs);
1335 if (err == NULL && *tag == NULL)
1336 err = got_error(GOT_ERR_NO_OBJ);
1340 static const struct got_error *
1341 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1342 const char *name, mode_t mode, struct got_object_id *blob_id)
1344 const struct got_error *err = NULL;
1348 *new_te = calloc(1, sizeof(**new_te));
1349 if (*new_te == NULL)
1350 return got_error_from_errno("calloc");
1352 (*new_te)->name = strdup(name);
1353 if ((*new_te)->name == NULL) {
1354 err = got_error_from_errno("strdup");
1358 (*new_te)->mode = S_IFREG | (mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
1359 (*new_te)->id = blob_id;
1361 if (err && *new_te) {
1362 got_object_tree_entry_close(*new_te);
1368 static const struct got_error *
1369 import_file(struct got_tree_entry **new_te, struct dirent *de,
1370 const char *path, struct got_repository *repo)
1372 const struct got_error *err;
1373 struct got_object_id *blob_id = NULL;
1377 if (asprintf(&filepath, "%s%s%s", path,
1378 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1379 return got_error_from_errno("asprintf");
1381 if (lstat(filepath, &sb) != 0) {
1382 err = got_error_from_errno2("lstat", path);
1386 err = got_object_blob_create(&blob_id, filepath, repo);
1390 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1399 static const struct got_error *
1400 insert_tree_entry(struct got_tree_entry *new_te,
1401 struct got_pathlist_head *paths)
1403 const struct got_error *err = NULL;
1404 struct got_pathlist_entry *new_pe;
1406 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1410 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1414 static const struct got_error *write_tree(struct got_object_id **,
1415 const char *, struct got_pathlist_head *, struct got_repository *,
1416 got_repo_import_cb progress_cb, void *progress_arg);
1418 static const struct got_error *
1419 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1420 const char *path, struct got_pathlist_head *ignores,
1421 struct got_repository *repo,
1422 got_repo_import_cb progress_cb, void *progress_arg)
1424 const struct got_error *err;
1427 if (asprintf(&subdirpath, "%s%s%s", path,
1428 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1429 return got_error_from_errno("asprintf");
1431 (*new_te) = calloc(1, sizeof(**new_te));
1432 if (*new_te == NULL)
1433 return got_error_from_errno("calloc");
1434 (*new_te)->mode = S_IFDIR;
1435 (*new_te)->name = strdup(de->d_name);
1436 if ((*new_te)->name == NULL) {
1437 err = got_error_from_errno("strdup");
1441 err = write_tree(&(*new_te)->id, subdirpath, ignores, repo,
1442 progress_cb, progress_arg);
1446 got_object_tree_entry_close(*new_te);
1452 static const struct got_error *
1453 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1454 struct got_pathlist_head *ignores, struct got_repository *repo,
1455 got_repo_import_cb progress_cb, void *progress_arg)
1457 const struct got_error *err = NULL;
1460 struct got_tree_entries new_tree_entries;
1461 struct got_tree_entry *new_te = NULL;
1462 struct got_pathlist_head paths;
1463 struct got_pathlist_entry *pe;
1465 *new_tree_id = NULL;
1468 new_tree_entries.nentries = 0;
1469 SIMPLEQ_INIT(&new_tree_entries.head);
1471 dir = opendir(path_dir);
1473 err = got_error_from_errno2("opendir", path_dir);
1477 while ((de = readdir(dir)) != NULL) {
1480 if (strcmp(de->d_name, ".") == 0 ||
1481 strcmp(de->d_name, "..") == 0)
1484 TAILQ_FOREACH(pe, ignores, entry) {
1485 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1492 if (de->d_type == DT_DIR) {
1493 err = import_subdir(&new_te, de, path_dir,
1494 ignores, repo, progress_cb, progress_arg);
1496 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1501 } else if (de->d_type == DT_REG) {
1502 err = import_file(&new_te, de, path_dir, repo);
1508 err = insert_tree_entry(new_te, &paths);
1513 if (TAILQ_EMPTY(&paths)) {
1514 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1518 TAILQ_FOREACH(pe, &paths, entry) {
1519 struct got_tree_entry *te = pe->data;
1521 new_tree_entries.nentries++;
1522 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
1523 if (!S_ISREG(te->mode))
1525 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1526 err = got_error_from_errno("asprintf");
1529 err = (*progress_cb)(progress_arg, path);
1535 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
1539 got_object_tree_entries_close(&new_tree_entries);
1540 got_pathlist_free(&paths);
1544 const struct got_error *
1545 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1546 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1547 struct got_repository *repo, got_repo_import_cb progress_cb,
1550 const struct got_error *err;
1551 struct got_object_id *new_tree_id;
1553 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1554 progress_cb, progress_arg);
1558 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1559 author, time(NULL), author, time(NULL), logmsg, repo);