2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
18 #include <sys/queue.h>
20 #include <sys/socket.h>
23 #include <sys/syslimits.h>
42 #include "got_error.h"
43 #include "got_reference.h"
44 #include "got_repository.h"
46 #include "got_cancel.h"
47 #include "got_worktree.h"
48 #include "got_object.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_object.h"
53 #include "got_lib_object_parse.h"
54 #include "got_lib_object_create.h"
55 #include "got_lib_pack.h"
56 #include "got_lib_privsep.h"
57 #include "got_lib_worktree.h"
58 #include "got_lib_sha1.h"
59 #include "got_lib_object_cache.h"
60 #include "got_lib_repository.h"
63 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
66 #define GOT_GIT_DIR ".git"
68 /* Mandatory files and directories inside the git directory. */
69 #define GOT_OBJECTS_DIR "objects"
70 #define GOT_REFS_DIR "refs"
71 #define GOT_HEAD_FILE "HEAD"
72 #define GOT_GITCONFIG "config"
74 /* Other files and directories inside the git directory. */
75 #define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
76 #define GOT_ORIG_HEAD_FILE "ORIG_HEAD"
77 #define GOT_OBJECTS_PACK_DIR "objects/pack"
78 #define GOT_PACKED_REFS_FILE "packed-refs"
81 got_repo_get_path(struct got_repository *repo)
87 got_repo_get_path_git_dir(struct got_repository *repo)
89 return repo->path_git_dir;
93 got_repo_get_gitconfig_author_name(struct got_repository *repo)
95 return repo->gitconfig_author_name;
99 got_repo_get_gitconfig_author_email(struct got_repository *repo)
101 return repo->gitconfig_author_email;
105 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
107 return repo->global_gitconfig_author_name;
111 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
113 return repo->global_gitconfig_author_email;
117 got_repo_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 got_repo_get_gitconfig_remotes(int *nremotes, struct got_remote_repo **remotes,
175 struct got_repository *repo)
177 *nremotes = repo->ngitconfig_remotes;
178 *remotes = repo->gitconfig_remotes;
182 is_git_repo(struct got_repository *repo)
184 const char *path_git = got_repo_get_path_git_dir(repo);
185 char *path_objects = got_repo_get_path_objects(repo);
186 char *path_refs = got_repo_get_path_refs(repo);
187 char *path_head = get_path_head(repo);
190 struct got_reference *head_ref;
192 if (lstat(path_git, &sb) == -1)
194 if (!S_ISDIR(sb.st_mode))
197 if (lstat(path_objects, &sb) == -1)
199 if (!S_ISDIR(sb.st_mode))
202 if (lstat(path_refs, &sb) == -1)
204 if (!S_ISDIR(sb.st_mode))
207 if (lstat(path_head, &sb) == -1)
209 if (!S_ISREG(sb.st_mode))
212 /* Check if the HEAD reference can be opened. */
213 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
215 got_ref_close(head_ref);
226 const struct got_error *
227 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
228 struct got_object *obj)
230 #ifndef GOT_NO_OBJ_CACHE
231 const struct got_error *err = NULL;
232 err = got_object_cache_add(&repo->objcache, id, obj);
234 if (err->code == GOT_ERR_OBJ_EXISTS ||
235 err->code == GOT_ERR_OBJ_TOO_LARGE)
245 got_repo_get_cached_object(struct got_repository *repo,
246 struct got_object_id *id)
248 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
251 const struct got_error *
252 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
253 struct got_tree_object *tree)
255 #ifndef GOT_NO_OBJ_CACHE
256 const struct got_error *err = NULL;
257 err = got_object_cache_add(&repo->treecache, id, tree);
259 if (err->code == GOT_ERR_OBJ_EXISTS ||
260 err->code == GOT_ERR_OBJ_TOO_LARGE)
269 struct got_tree_object *
270 got_repo_get_cached_tree(struct got_repository *repo,
271 struct got_object_id *id)
273 return (struct got_tree_object *)got_object_cache_get(
274 &repo->treecache, id);
277 const struct got_error *
278 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
279 struct got_commit_object *commit)
281 #ifndef GOT_NO_OBJ_CACHE
282 const struct got_error *err = NULL;
283 err = got_object_cache_add(&repo->commitcache, id, commit);
285 if (err->code == GOT_ERR_OBJ_EXISTS ||
286 err->code == GOT_ERR_OBJ_TOO_LARGE)
295 struct got_commit_object *
296 got_repo_get_cached_commit(struct got_repository *repo,
297 struct got_object_id *id)
299 return (struct got_commit_object *)got_object_cache_get(
300 &repo->commitcache, id);
303 const struct got_error *
304 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
305 struct got_tag_object *tag)
307 #ifndef GOT_NO_OBJ_CACHE
308 const struct got_error *err = NULL;
309 err = got_object_cache_add(&repo->tagcache, id, tag);
311 if (err->code == GOT_ERR_OBJ_EXISTS ||
312 err->code == GOT_ERR_OBJ_TOO_LARGE)
321 struct got_tag_object *
322 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
324 return (struct got_tag_object *)got_object_cache_get(
325 &repo->tagcache, id);
328 const struct got_error *
329 open_repo(struct got_repository *repo, const char *path)
331 const struct got_error *err = NULL;
333 /* bare git repository? */
334 repo->path_git_dir = strdup(path);
335 if (repo->path_git_dir == NULL)
336 return got_error_from_errno("strdup");
337 if (is_git_repo(repo)) {
338 repo->path = strdup(repo->path_git_dir);
339 if (repo->path == NULL) {
340 err = got_error_from_errno("strdup");
346 /* git repository with working tree? */
347 free(repo->path_git_dir);
348 repo->path_git_dir = NULL;
349 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
350 err = got_error_from_errno("asprintf");
353 if (is_git_repo(repo)) {
354 repo->path = strdup(path);
355 if (repo->path == NULL) {
356 err = got_error_from_errno("strdup");
362 err = got_error(GOT_ERR_NOT_GIT_REPO);
367 free(repo->path_git_dir);
368 repo->path_git_dir = NULL;
373 static const struct got_error *
374 parse_gitconfig_file(int *gitconfig_repository_format_version,
375 char **gitconfig_author_name, char **gitconfig_author_email,
376 struct got_remote_repo **remotes, int *nremotes,
377 const char *gitconfig_path)
379 const struct got_error *err = NULL, *child_err = NULL;
381 int imsg_fds[2] = { -1, -1 };
383 struct imsgbuf *ibuf;
385 *gitconfig_repository_format_version = 0;
386 *gitconfig_author_name = NULL;
387 *gitconfig_author_email = NULL;
389 fd = open(gitconfig_path, O_RDONLY);
393 return got_error_from_errno2("open", gitconfig_path);
396 ibuf = calloc(1, sizeof(*ibuf));
398 err = got_error_from_errno("calloc");
402 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
403 err = got_error_from_errno("socketpair");
409 err = got_error_from_errno("fork");
411 } else if (pid == 0) {
412 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
417 if (close(imsg_fds[1]) == -1) {
418 err = got_error_from_errno("close");
422 imsg_init(ibuf, imsg_fds[0]);
424 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
429 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
433 err = got_privsep_recv_gitconfig_int(
434 gitconfig_repository_format_version, ibuf);
438 err = got_privsep_send_gitconfig_author_name_req(ibuf);
442 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
446 err = got_privsep_send_gitconfig_author_email_req(ibuf);
450 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
454 if (remotes && nremotes) {
455 err = got_privsep_send_gitconfig_remotes_req(ibuf);
459 err = got_privsep_recv_gitconfig_remotes(remotes,
466 err = got_privsep_send_stop(imsg_fds[0]);
467 child_err = got_privsep_wait_for_child(pid);
468 if (child_err && err == NULL)
471 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
472 err = got_error_from_errno("close");
473 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
474 err = got_error_from_errno("close");
475 if (fd != -1 && close(fd) == -1 && err == NULL)
476 err = got_error_from_errno2("close", gitconfig_path);
481 static const struct got_error *
482 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
484 const struct got_error *err = NULL;
485 char *repo_gitconfig_path = NULL;
487 if (global_gitconfig_path) {
488 /* Read settings from ~/.gitconfig. */
489 int dummy_repo_version;
490 err = parse_gitconfig_file(&dummy_repo_version,
491 &repo->global_gitconfig_author_name,
492 &repo->global_gitconfig_author_email,
493 NULL, NULL, global_gitconfig_path);
498 /* Read repository's .git/config file. */
499 err = get_path_gitconfig(&repo_gitconfig_path, repo);
503 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
504 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
505 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
506 repo_gitconfig_path);
510 free(repo_gitconfig_path);
514 const struct got_error *
515 got_repo_open(struct got_repository **repop, const char *path,
516 const char *global_gitconfig_path)
518 struct got_repository *repo = NULL;
519 const struct got_error *err = NULL;
521 int i, tried_root = 0;
525 if (got_path_is_absolute(path))
526 abspath = strdup(path);
528 abspath = got_path_get_absolute(path);
530 return got_error(GOT_ERR_BAD_PATH);
532 repo = calloc(1, sizeof(*repo));
534 err = got_error_from_errno("calloc");
538 for (i = 0; i < nitems(repo->privsep_children); i++) {
539 memset(&repo->privsep_children[i], 0,
540 sizeof(repo->privsep_children[0]));
541 repo->privsep_children[i].imsg_fd = -1;
544 err = got_object_cache_init(&repo->objcache,
545 GOT_OBJECT_CACHE_TYPE_OBJ);
548 err = got_object_cache_init(&repo->treecache,
549 GOT_OBJECT_CACHE_TYPE_TREE);
552 err = got_object_cache_init(&repo->commitcache,
553 GOT_OBJECT_CACHE_TYPE_COMMIT);
556 err = got_object_cache_init(&repo->tagcache,
557 GOT_OBJECT_CACHE_TYPE_TAG);
561 path = realpath(abspath, NULL);
563 err = got_error_from_errno2("realpath", abspath);
568 err = open_repo(repo, path);
571 if (err->code != GOT_ERR_NOT_GIT_REPO)
573 if (path[0] == '/' && path[1] == '\0') {
575 err = got_error(GOT_ERR_NOT_GIT_REPO);
580 path = dirname(path);
582 err = got_error_from_errno2("dirname", path);
587 err = read_gitconfig(repo, global_gitconfig_path);
590 if (repo->gitconfig_repository_format_version != 0)
591 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
594 got_repo_close(repo);
601 const struct got_error *
602 got_repo_close(struct got_repository *repo)
604 const struct got_error *err = NULL, *child_err;
607 for (i = 0; i < nitems(repo->packidx_cache); i++) {
608 if (repo->packidx_cache[i] == NULL)
610 got_packidx_close(repo->packidx_cache[i]);
613 for (i = 0; i < nitems(repo->packs); i++) {
614 if (repo->packs[i].path_packfile == NULL)
616 got_pack_close(&repo->packs[i]);
620 free(repo->path_git_dir);
622 got_object_cache_close(&repo->objcache);
623 got_object_cache_close(&repo->treecache);
624 got_object_cache_close(&repo->commitcache);
625 got_object_cache_close(&repo->tagcache);
627 for (i = 0; i < nitems(repo->privsep_children); i++) {
628 if (repo->privsep_children[i].imsg_fd == -1)
630 imsg_clear(repo->privsep_children[i].ibuf);
631 free(repo->privsep_children[i].ibuf);
632 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
633 child_err = got_privsep_wait_for_child(
634 repo->privsep_children[i].pid);
635 if (child_err && err == NULL)
637 if (close(repo->privsep_children[i].imsg_fd) != 0 &&
639 err = got_error_from_errno("close");
642 free(repo->gitconfig_author_name);
643 free(repo->gitconfig_author_email);
644 for (i = 0; i < repo->ngitconfig_remotes; i++) {
645 free(repo->gitconfig_remotes[i].name);
646 free(repo->gitconfig_remotes[i].url);
648 free(repo->gitconfig_remotes);
654 const struct got_error *
655 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
656 const char *input_path, int check_disk)
658 const struct got_error *err = NULL;
659 const char *repo_abspath = NULL;
661 char *canonpath, *path = NULL;
663 *in_repo_path = NULL;
665 canonpath = strdup(input_path);
666 if (canonpath == NULL) {
667 err = got_error_from_errno("strdup");
670 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
674 repo_abspath = got_repo_get_path(repo);
676 if (!check_disk || canonpath[0] == '\0') {
677 path = strdup(canonpath);
679 err = got_error_from_errno("strdup");
683 path = realpath(canonpath, NULL);
685 if (errno != ENOENT) {
686 err = got_error_from_errno2("realpath",
691 * Path is not on disk.
692 * Assume it is already relative to repository root.
694 path = strdup(canonpath);
696 err = got_error_from_errno("strdup");
701 repolen = strlen(repo_abspath);
705 if (strcmp(path, repo_abspath) == 0) {
709 err = got_error_from_errno("strdup");
712 } else if (len > repolen &&
713 got_path_is_child(path, repo_abspath, repolen)) {
714 /* Matched an on-disk path inside repository. */
715 if (got_repo_is_bare(repo)) {
717 * Matched an on-disk path inside repository
718 * database. Treat as repository-relative.
722 /* Strip common prefix with repository path. */
723 err = got_path_skip_common_ancestor(&child,
732 * Matched unrelated on-disk path.
733 * Treat it as repository-relative.
738 /* Make in-repository path absolute */
739 if (path[0] != '/') {
741 if (asprintf(&abspath, "/%s", path) == -1) {
742 err = got_error_from_errno("asprintf");
754 *in_repo_path = path;
758 static const struct got_error *
759 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
760 const char *path_packidx)
762 const struct got_error *err = NULL;
765 for (i = 0; i < nitems(repo->packidx_cache); i++) {
766 if (repo->packidx_cache[i] == NULL)
768 if (strcmp(repo->packidx_cache[i]->path_packidx,
769 path_packidx) == 0) {
770 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
773 if (i == nitems(repo->packidx_cache)) {
774 err = got_packidx_close(repo->packidx_cache[i - 1]);
780 * Insert the new pack index at the front so it will
781 * be searched first in the future.
783 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
784 sizeof(repo->packidx_cache) -
785 sizeof(repo->packidx_cache[0]));
786 repo->packidx_cache[0] = packidx;
792 is_packidx_filename(const char *name, size_t len)
794 if (len != GOT_PACKIDX_NAMELEN)
797 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
800 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
801 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
807 const struct got_error *
808 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
809 struct got_repository *repo, struct got_object_id *id)
811 const struct got_error *err;
818 /* Search pack index cache. */
819 for (i = 0; i < nitems(repo->packidx_cache); i++) {
820 if (repo->packidx_cache[i] == NULL)
822 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
824 *packidx = repo->packidx_cache[i];
826 * Move this cache entry to the front. Repeatedly
827 * searching a wrong pack index can be expensive.
830 struct got_packidx *p;
831 p = repo->packidx_cache[0];
832 repo->packidx_cache[0] = *packidx;
833 repo->packidx_cache[i] = p;
838 /* No luck. Search the filesystem. */
840 path_packdir = got_repo_get_path_objects_pack(repo);
841 if (path_packdir == NULL)
842 return got_error_from_errno("got_repo_get_path_objects_pack");
844 packdir = opendir(path_packdir);
845 if (packdir == NULL) {
847 err = got_error_no_obj(id);
849 err = got_error_from_errno2("opendir", path_packdir);
853 while ((dent = readdir(packdir)) != NULL) {
856 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
859 if (asprintf(&path_packidx, "%s/%s", path_packdir,
860 dent->d_name) == -1) {
861 err = got_error_from_errno("asprintf");
865 for (i = 0; i < nitems(repo->packidx_cache); i++) {
866 if (repo->packidx_cache[i] == NULL)
868 if (strcmp(repo->packidx_cache[i]->path_packidx,
869 path_packidx) == 0) {
876 continue; /* already searched */
879 err = got_packidx_open(packidx, path_packidx, 0);
885 err = cache_packidx(repo, *packidx, path_packidx);
890 *idx = got_packidx_get_object_idx(*packidx, id);
892 err = NULL; /* found the object */
897 err = got_error_no_obj(id);
900 if (packdir && closedir(packdir) != 0 && err == NULL)
901 err = got_error_from_errno("closedir");
905 static const struct got_error *
906 read_packfile_hdr(int fd, struct got_packidx *packidx)
908 const struct got_error *err = NULL;
909 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
910 struct got_packfile_hdr hdr;
913 n = read(fd, &hdr, sizeof(hdr));
915 return got_error_from_errno("read");
916 if (n != sizeof(hdr))
917 return got_error(GOT_ERR_BAD_PACKFILE);
919 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
920 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
921 betoh32(hdr.nobjects) != totobj)
922 err = got_error(GOT_ERR_BAD_PACKFILE);
927 static const struct got_error *
928 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
930 const struct got_error *err = NULL;
932 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
934 return got_error_from_errno2("open", path_packfile);
937 err = read_packfile_hdr(*fd, packidx);
947 const struct got_error *
948 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
949 const char *path_packfile, struct got_packidx *packidx)
951 const struct got_error *err = NULL;
952 struct got_pack *pack = NULL;
959 for (i = 0; i < nitems(repo->packs); i++) {
960 pack = &repo->packs[i];
961 if (pack->path_packfile == NULL)
963 if (strcmp(pack->path_packfile, path_packfile) == 0)
964 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
967 if (i == nitems(repo->packs) - 1) {
968 err = got_pack_close(&repo->packs[i - 1]);
971 memmove(&repo->packs[1], &repo->packs[0],
972 sizeof(repo->packs) - sizeof(repo->packs[0]));
976 pack = &repo->packs[i];
978 pack->path_packfile = strdup(path_packfile);
979 if (pack->path_packfile == NULL) {
980 err = got_error_from_errno("strdup");
984 err = open_packfile(&pack->fd, path_packfile, packidx);
988 if (fstat(pack->fd, &sb) != 0) {
989 err = got_error_from_errno("fstat");
992 pack->filesize = sb.st_size;
994 pack->privsep_child = NULL;
996 #ifndef GOT_PACK_NO_MMAP
997 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
999 if (pack->map == MAP_FAILED) {
1000 if (errno != ENOMEM) {
1001 err = got_error_from_errno("mmap");
1004 pack->map = NULL; /* fall back to read(2) */
1010 free(pack->path_packfile);
1011 memset(pack, 0, sizeof(*pack));
1019 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1021 struct got_pack *pack = NULL;
1024 for (i = 0; i < nitems(repo->packs); i++) {
1025 pack = &repo->packs[i];
1026 if (pack->path_packfile == NULL)
1028 if (strcmp(pack->path_packfile, path_packfile) == 0)
1035 const struct got_error *
1036 got_repo_init(const char *repo_path)
1038 const struct got_error *err = NULL;
1039 const char *dirnames[] = {
1041 GOT_OBJECTS_PACK_DIR,
1044 const char *description_str = "Unnamed repository; "
1045 "edit this file 'description' to name the repository.";
1046 const char *headref_str = "ref: refs/heads/main";
1047 const char *gitconfig_str = "[core]\n"
1048 "\trepositoryformatversion = 0\n"
1049 "\tfilemode = true\n"
1054 if (!got_path_dir_is_empty(repo_path))
1055 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1057 for (i = 0; i < nitems(dirnames); i++) {
1058 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1059 return got_error_from_errno("asprintf");
1061 err = got_path_mkdir(path);
1067 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1068 return got_error_from_errno("asprintf");
1069 err = got_path_create_file(path, description_str);
1074 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1075 return got_error_from_errno("asprintf");
1076 err = got_path_create_file(path, headref_str);
1081 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1082 return got_error_from_errno("asprintf");
1083 err = got_path_create_file(path, gitconfig_str);
1091 static const struct got_error *
1092 match_packed_object(struct got_object_id **unique_id,
1093 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1095 const struct got_error *err = NULL;
1098 struct dirent *dent;
1100 struct got_object_id_queue matched_ids;
1102 SIMPLEQ_INIT(&matched_ids);
1104 path_packdir = got_repo_get_path_objects_pack(repo);
1105 if (path_packdir == NULL)
1106 return got_error_from_errno("got_repo_get_path_objects_pack");
1108 packdir = opendir(path_packdir);
1109 if (packdir == NULL) {
1110 if (errno != ENOENT)
1111 err = got_error_from_errno2("opendir", path_packdir);
1115 while ((dent = readdir(packdir)) != NULL) {
1116 struct got_packidx *packidx;
1117 struct got_object_qid *qid;
1120 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1123 if (asprintf(&path_packidx, "%s/%s", path_packdir,
1124 dent->d_name) == -1) {
1125 err = got_error_from_errno("asprintf");
1129 err = got_packidx_open(&packidx, path_packidx, 0);
1134 err = got_packidx_match_id_str_prefix(&matched_ids,
1135 packidx, id_str_prefix);
1137 got_packidx_close(packidx);
1140 err = got_packidx_close(packidx);
1144 SIMPLEQ_FOREACH(qid, &matched_ids, entry) {
1145 if (obj_type != GOT_OBJ_TYPE_ANY) {
1147 err = got_object_get_type(&matched_type, repo,
1151 if (matched_type != obj_type)
1154 if (*unique_id == NULL) {
1155 *unique_id = got_object_id_dup(qid->id);
1156 if (*unique_id == NULL) {
1157 err = got_error_from_errno("malloc");
1161 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1162 continue; /* packed multiple times */
1163 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1169 got_object_id_queue_free(&matched_ids);
1171 if (packdir && closedir(packdir) != 0 && err == NULL)
1172 err = got_error_from_errno("closedir");
1180 static const struct got_error *
1181 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1182 const char *object_dir, const char *id_str_prefix, int obj_type,
1183 struct got_repository *repo)
1185 const struct got_error *err = NULL;
1188 struct dirent *dent;
1189 struct got_object_id id;
1191 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1192 err = got_error_from_errno("asprintf");
1196 dir = opendir(path);
1198 if (errno == ENOENT) {
1202 err = got_error_from_errno2("opendir", path);
1205 while ((dent = readdir(dir)) != NULL) {
1209 if (strcmp(dent->d_name, ".") == 0 ||
1210 strcmp(dent->d_name, "..") == 0)
1213 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1214 err = got_error_from_errno("asprintf");
1218 if (!got_parse_sha1_digest(id.sha1, id_str))
1222 * Directory entries do not necessarily appear in
1223 * sorted order, so we must iterate over all of them.
1225 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1231 if (*unique_id == NULL) {
1232 if (obj_type != GOT_OBJ_TYPE_ANY) {
1234 err = got_object_get_type(&matched_type, repo,
1238 if (matched_type != obj_type)
1241 *unique_id = got_object_id_dup(&id);
1242 if (*unique_id == NULL) {
1243 err = got_error_from_errno("got_object_id_dup");
1248 if (got_object_id_cmp(*unique_id, &id) == 0)
1249 continue; /* both packed and loose */
1250 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1256 if (dir && closedir(dir) != 0 && err == NULL)
1257 err = got_error_from_errno("closedir");
1266 const struct got_error *
1267 got_repo_match_object_id_prefix(struct got_object_id **id,
1268 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1270 const struct got_error *err = NULL;
1271 char *path_objects = got_repo_get_path_objects(repo);
1272 char *object_dir = NULL;
1278 for (i = 0; i < strlen(id_str_prefix); i++) {
1279 if (isxdigit((unsigned char)id_str_prefix[i]))
1281 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1284 len = strlen(id_str_prefix);
1286 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1289 object_dir = strndup(id_str_prefix, 2);
1290 if (object_dir == NULL) {
1291 err = got_error_from_errno("strdup");
1294 err = match_loose_object(id, path_objects, object_dir,
1295 id_str_prefix, obj_type, repo);
1296 } else if (len == 1) {
1298 for (i = 0; i < 0xf; i++) {
1299 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1301 err = got_error_from_errno("asprintf");
1304 err = match_packed_object(id, repo, object_dir,
1308 err = match_loose_object(id, path_objects, object_dir,
1309 id_str_prefix, obj_type, repo);
1314 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1322 } else if (*id == NULL)
1323 err = got_error(GOT_ERR_NO_OBJ);
1328 const struct got_error *
1329 got_repo_match_object_id(struct got_object_id **id, char **label,
1330 const char *id_str, int obj_type, int resolve_tags,
1331 struct got_repository *repo)
1333 const struct got_error *err;
1334 struct got_tag_object *tag;
1335 struct got_reference *ref = NULL;
1342 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
1345 *id = got_object_id_dup(
1346 got_object_tag_get_object_id(tag));
1348 err = got_error_from_errno("got_object_id_dup");
1349 else if (label && asprintf(label, "refs/tags/%s",
1350 got_object_tag_get_name(tag)) == -1) {
1351 err = got_error_from_errno("asprintf");
1355 got_object_tag_close(tag);
1357 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1358 err->code != GOT_ERR_NO_OBJ)
1362 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1364 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1366 err = got_ref_open(&ref, repo, id_str, 0);
1370 *label = strdup(got_ref_get_name(ref));
1371 if (*label == NULL) {
1372 err = got_error_from_errno("strdup");
1376 err = got_ref_resolve(id, repo, ref);
1378 err = got_object_id_str(label, *id);
1379 if (*label == NULL) {
1380 err = got_error_from_errno("strdup");
1390 const struct got_error *
1391 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1392 int obj_type, struct got_repository *repo)
1394 const struct got_error *err;
1395 struct got_reflist_head refs;
1396 struct got_reflist_entry *re;
1397 struct got_object_id *tag_id;
1399 SIMPLEQ_INIT(&refs);
1402 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_by_name, NULL);
1406 SIMPLEQ_FOREACH(re, &refs, entry) {
1407 const char *refname;
1408 refname = got_ref_get_name(re->ref);
1409 if (got_ref_is_symbolic(re->ref))
1411 refname += strlen("refs/tags/");
1412 if (strcmp(refname, name) != 0)
1414 err = got_ref_resolve(&tag_id, repo, re->ref);
1417 err = got_object_open_as_tag(tag, repo, tag_id);
1421 if (obj_type == GOT_OBJ_TYPE_ANY ||
1422 got_object_tag_get_object_type(*tag) == obj_type)
1424 got_object_tag_close(*tag);
1428 got_ref_list_free(&refs);
1429 if (err == NULL && *tag == NULL)
1430 err = got_error(GOT_ERR_NO_OBJ);
1434 static const struct got_error *
1435 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1436 const char *name, mode_t mode, struct got_object_id *blob_id)
1438 const struct got_error *err = NULL;
1442 *new_te = calloc(1, sizeof(**new_te));
1443 if (*new_te == NULL)
1444 return got_error_from_errno("calloc");
1446 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1447 sizeof((*new_te)->name)) {
1448 err = got_error(GOT_ERR_NO_SPACE);
1452 (*new_te)->mode = S_IFREG | (mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
1453 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1455 if (err && *new_te) {
1462 static const struct got_error *
1463 import_file(struct got_tree_entry **new_te, struct dirent *de,
1464 const char *path, struct got_repository *repo)
1466 const struct got_error *err;
1467 struct got_object_id *blob_id = NULL;
1471 if (asprintf(&filepath, "%s%s%s", path,
1472 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1473 return got_error_from_errno("asprintf");
1475 if (lstat(filepath, &sb) != 0) {
1476 err = got_error_from_errno2("lstat", path);
1480 err = got_object_blob_create(&blob_id, filepath, repo);
1484 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1493 static const struct got_error *
1494 insert_tree_entry(struct got_tree_entry *new_te,
1495 struct got_pathlist_head *paths)
1497 const struct got_error *err = NULL;
1498 struct got_pathlist_entry *new_pe;
1500 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1504 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1508 static const struct got_error *write_tree(struct got_object_id **,
1509 const char *, struct got_pathlist_head *, struct got_repository *,
1510 got_repo_import_cb progress_cb, void *progress_arg);
1512 static const struct got_error *
1513 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1514 const char *path, struct got_pathlist_head *ignores,
1515 struct got_repository *repo,
1516 got_repo_import_cb progress_cb, void *progress_arg)
1518 const struct got_error *err;
1519 struct got_object_id *id = NULL;
1522 if (asprintf(&subdirpath, "%s%s%s", path,
1523 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1524 return got_error_from_errno("asprintf");
1526 (*new_te) = calloc(1, sizeof(**new_te));
1527 if (*new_te == NULL)
1528 return got_error_from_errno("calloc");
1529 (*new_te)->mode = S_IFDIR;
1530 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1531 sizeof((*new_te)->name)) {
1532 err = got_error(GOT_ERR_NO_SPACE);
1535 err = write_tree(&id, subdirpath, ignores, repo,
1536 progress_cb, progress_arg);
1539 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1551 static const struct got_error *
1552 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1553 struct got_pathlist_head *ignores, struct got_repository *repo,
1554 got_repo_import_cb progress_cb, void *progress_arg)
1556 const struct got_error *err = NULL;
1560 struct got_tree_entry *new_te = NULL;
1561 struct got_pathlist_head paths;
1562 struct got_pathlist_entry *pe;
1564 *new_tree_id = NULL;
1568 dir = opendir(path_dir);
1570 err = got_error_from_errno2("opendir", path_dir);
1575 while ((de = readdir(dir)) != NULL) {
1578 if (strcmp(de->d_name, ".") == 0 ||
1579 strcmp(de->d_name, "..") == 0)
1582 TAILQ_FOREACH(pe, ignores, entry) {
1583 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1590 if (de->d_type == DT_DIR) {
1591 err = import_subdir(&new_te, de, path_dir,
1592 ignores, repo, progress_cb, progress_arg);
1594 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1599 } else if (de->d_type == DT_REG) {
1600 err = import_file(&new_te, de, path_dir, repo);
1606 err = insert_tree_entry(new_te, &paths);
1612 if (TAILQ_EMPTY(&paths)) {
1613 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1617 TAILQ_FOREACH(pe, &paths, entry) {
1618 struct got_tree_entry *te = pe->data;
1620 if (!S_ISREG(te->mode))
1622 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1623 err = got_error_from_errno("asprintf");
1626 err = (*progress_cb)(progress_arg, path);
1632 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1636 got_pathlist_free(&paths);
1640 const struct got_error *
1641 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1642 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1643 struct got_repository *repo, got_repo_import_cb progress_cb,
1646 const struct got_error *err;
1647 struct got_object_id *new_tree_id;
1649 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1650 progress_cb, progress_arg);
1654 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1655 author, time(NULL), author, time(NULL), logmsg, repo);