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>
22 #include <sys/syslimits.h>
40 #include "got_error.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
44 #include "got_worktree.h"
45 #include "got_object.h"
47 #include "got_lib_delta.h"
48 #include "got_lib_inflate.h"
49 #include "got_lib_object.h"
50 #include "got_lib_object_parse.h"
51 #include "got_lib_object_create.h"
52 #include "got_lib_pack.h"
53 #include "got_lib_privsep.h"
54 #include "got_lib_worktree.h"
55 #include "got_lib_sha1.h"
56 #include "got_lib_object_cache.h"
57 #include "got_lib_repository.h"
60 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
63 #define GOT_GIT_DIR ".git"
65 /* Mandatory files and directories inside the git directory. */
66 #define GOT_OBJECTS_DIR "objects"
67 #define GOT_REFS_DIR "refs"
68 #define GOT_HEAD_FILE "HEAD"
70 /* Other files and directories inside the git directory. */
71 #define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
72 #define GOT_ORIG_HEAD_FILE "ORIG_HEAD"
73 #define GOT_OBJECTS_PACK_DIR "objects/pack"
74 #define GOT_PACKED_REFS_FILE "packed-refs"
77 got_repo_get_path(struct got_repository *repo)
83 got_repo_get_path_git_dir(struct got_repository *repo)
85 return repo->path_git_dir;
89 got_repo_is_bare(struct got_repository *repo)
91 return (strcmp(repo->path, repo->path_git_dir) == 0);
95 get_path_git_child(struct got_repository *repo, const char *basename)
99 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
107 got_repo_get_path_objects(struct got_repository *repo)
109 return get_path_git_child(repo, GOT_OBJECTS_DIR);
113 got_repo_get_path_objects_pack(struct got_repository *repo)
115 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
119 got_repo_get_path_refs(struct got_repository *repo)
121 return get_path_git_child(repo, GOT_REFS_DIR);
125 got_repo_get_path_packed_refs(struct got_repository *repo)
127 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
131 get_path_head(struct got_repository *repo)
133 return get_path_git_child(repo, GOT_HEAD_FILE);
137 is_git_repo(struct got_repository *repo)
139 const char *path_git = got_repo_get_path_git_dir(repo);
140 char *path_objects = got_repo_get_path_objects(repo);
141 char *path_refs = got_repo_get_path_refs(repo);
142 char *path_head = get_path_head(repo);
145 struct got_reference *head_ref;
147 if (lstat(path_git, &sb) == -1)
149 if (!S_ISDIR(sb.st_mode))
152 if (lstat(path_objects, &sb) == -1)
154 if (!S_ISDIR(sb.st_mode))
157 if (lstat(path_refs, &sb) == -1)
159 if (!S_ISDIR(sb.st_mode))
162 if (lstat(path_head, &sb) == -1)
164 if (!S_ISREG(sb.st_mode))
167 /* Check if the HEAD reference can be opened. */
168 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
170 got_ref_close(head_ref);
181 const struct got_error *
182 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
183 struct got_object *obj)
185 #ifndef GOT_NO_OBJ_CACHE
186 const struct got_error *err = NULL;
187 err = got_object_cache_add(&repo->objcache, id, obj);
189 if (err->code == GOT_ERR_OBJ_EXISTS ||
190 err->code == GOT_ERR_OBJ_TOO_LARGE)
200 got_repo_get_cached_object(struct got_repository *repo,
201 struct got_object_id *id)
203 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
206 const struct got_error *
207 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
208 struct got_tree_object *tree)
210 #ifndef GOT_NO_OBJ_CACHE
211 const struct got_error *err = NULL;
212 err = got_object_cache_add(&repo->treecache, id, tree);
214 if (err->code == GOT_ERR_OBJ_EXISTS ||
215 err->code == GOT_ERR_OBJ_TOO_LARGE)
224 struct got_tree_object *
225 got_repo_get_cached_tree(struct got_repository *repo,
226 struct got_object_id *id)
228 return (struct got_tree_object *)got_object_cache_get(
229 &repo->treecache, id);
232 const struct got_error *
233 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
234 struct got_commit_object *commit)
236 #ifndef GOT_NO_OBJ_CACHE
237 const struct got_error *err = NULL;
238 err = got_object_cache_add(&repo->commitcache, id, commit);
240 if (err->code == GOT_ERR_OBJ_EXISTS ||
241 err->code == GOT_ERR_OBJ_TOO_LARGE)
250 struct got_commit_object *
251 got_repo_get_cached_commit(struct got_repository *repo,
252 struct got_object_id *id)
254 return (struct got_commit_object *)got_object_cache_get(
255 &repo->commitcache, id);
258 const struct got_error *
259 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
260 struct got_tag_object *tag)
262 #ifndef GOT_NO_OBJ_CACHE
263 const struct got_error *err = NULL;
264 err = got_object_cache_add(&repo->tagcache, id, tag);
266 if (err->code == GOT_ERR_OBJ_EXISTS ||
267 err->code == GOT_ERR_OBJ_TOO_LARGE)
276 struct got_tag_object *
277 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
279 return (struct got_tag_object *)got_object_cache_get(
280 &repo->tagcache, id);
283 const struct got_error *
284 open_repo(struct got_repository *repo, const char *path)
286 const struct got_error *err = NULL;
288 /* bare git repository? */
289 repo->path_git_dir = strdup(path);
290 if (repo->path_git_dir == NULL)
291 return got_error_from_errno("strdup");
292 if (is_git_repo(repo)) {
293 repo->path = strdup(repo->path_git_dir);
294 if (repo->path == NULL) {
295 err = got_error_from_errno("strdup");
301 /* git repository with working tree? */
302 free(repo->path_git_dir);
303 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
304 err = got_error_from_errno("asprintf");
307 if (is_git_repo(repo)) {
308 repo->path = strdup(path);
309 if (repo->path == NULL) {
310 err = got_error_from_errno("strdup");
316 err = got_error(GOT_ERR_NOT_GIT_REPO);
321 free(repo->path_git_dir);
322 repo->path_git_dir = NULL;
327 const struct got_error *
328 got_repo_open(struct got_repository **repop, const char *path)
330 struct got_repository *repo = NULL;
331 const struct got_error *err = NULL;
332 char *abspath, *normpath = NULL;
333 int i, tried_root = 0;
337 if (got_path_is_absolute(path))
338 abspath = strdup(path);
340 abspath = got_path_get_absolute(path);
342 return got_error(GOT_ERR_BAD_PATH);
344 repo = calloc(1, sizeof(*repo));
346 err = got_error_from_errno("calloc");
350 for (i = 0; i < nitems(repo->privsep_children); i++) {
351 memset(&repo->privsep_children[i], 0,
352 sizeof(repo->privsep_children[0]));
353 repo->privsep_children[i].imsg_fd = -1;
356 err = got_object_cache_init(&repo->objcache,
357 GOT_OBJECT_CACHE_TYPE_OBJ);
360 err = got_object_cache_init(&repo->treecache,
361 GOT_OBJECT_CACHE_TYPE_TREE);
364 err = got_object_cache_init(&repo->commitcache,
365 GOT_OBJECT_CACHE_TYPE_COMMIT);
368 err = got_object_cache_init(&repo->tagcache,
369 GOT_OBJECT_CACHE_TYPE_TAG);
373 normpath = got_path_normalize(abspath);
374 if (normpath == NULL) {
375 err = got_error(GOT_ERR_BAD_PATH);
381 err = open_repo(repo, path);
384 if (err->code != GOT_ERR_NOT_GIT_REPO)
386 if (path[0] == '/' && path[1] == '\0') {
388 err = got_error(GOT_ERR_NOT_GIT_REPO);
393 path = dirname(path);
395 err = got_error_from_errno2("dirname", path);
399 got_repo_close(repo);
407 const struct got_error *
408 got_repo_close(struct got_repository *repo)
410 const struct got_error *err = NULL, *child_err;
413 for (i = 0; i < nitems(repo->packidx_cache); i++) {
414 if (repo->packidx_cache[i] == NULL)
416 got_packidx_close(repo->packidx_cache[i]);
419 for (i = 0; i < nitems(repo->packs); i++) {
420 if (repo->packs[i].path_packfile == NULL)
422 got_pack_close(&repo->packs[i]);
426 free(repo->path_git_dir);
428 got_object_cache_close(&repo->objcache);
429 got_object_cache_close(&repo->treecache);
430 got_object_cache_close(&repo->commitcache);
431 got_object_cache_close(&repo->tagcache);
433 for (i = 0; i < nitems(repo->privsep_children); i++) {
434 if (repo->privsep_children[i].imsg_fd == -1)
436 imsg_clear(repo->privsep_children[i].ibuf);
437 free(repo->privsep_children[i].ibuf);
438 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
439 child_err = got_privsep_wait_for_child(
440 repo->privsep_children[i].pid);
441 if (child_err && err == NULL)
443 if (close(repo->privsep_children[i].imsg_fd) != 0 &&
445 err = got_error_from_errno("close");
452 const struct got_error *
453 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
454 const char *input_path, int check_disk)
456 const struct got_error *err = NULL;
457 const char *repo_abspath = NULL;
458 size_t repolen, cwdlen, len;
459 char *cwd, *canonpath, *path = NULL;
461 *in_repo_path = NULL;
463 cwd = getcwd(NULL, 0);
465 return got_error_from_errno("getcwd");
467 canonpath = strdup(input_path);
468 if (canonpath == NULL) {
469 err = got_error_from_errno("strdup");
472 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
476 repo_abspath = got_repo_get_path(repo);
478 if (!check_disk || canonpath[0] == '\0') {
479 path = strdup(canonpath);
481 err = got_error_from_errno("strdup");
485 int is_repo_child = 0, is_cwd_child = 0;
487 path = realpath(canonpath, NULL);
489 if (errno != ENOENT) {
490 err = got_error_from_errno2("realpath",
495 * Path is not on disk.
496 * Assume it is already relative to repository root.
498 path = strdup(canonpath);
500 err = got_error_from_errno("strdup");
505 repolen = strlen(repo_abspath);
506 cwdlen = strlen(cwd);
509 if (len > repolen && strncmp(path, repo_abspath, repolen) == 0)
511 if (len > cwdlen && strncmp(path, cwd, cwdlen) == 0)
514 if (strcmp(path, repo_abspath) == 0) {
518 err = got_error_from_errno("strdup");
521 } else if (is_repo_child && is_cwd_child) {
523 /* Strip common prefix with repository path. */
524 err = got_path_skip_common_ancestor(&child,
530 } else if (is_repo_child) {
531 /* Matched an on-disk path inside repository. */
532 if (got_repo_is_bare(repo)) {
534 * Matched an on-disk path inside repository
535 * database. Treat as repository-relative.
539 /* Strip common prefix with repository path. */
540 err = got_path_skip_common_ancestor(&child,
547 } else if (is_cwd_child) {
549 /* Strip common prefix with cwd. */
550 err = got_path_skip_common_ancestor(&child, cwd,
558 * Matched unrelated on-disk path.
559 * Treat it as repository-relative.
564 /* Make in-repository path absolute */
565 if (path[0] != '/') {
567 if (asprintf(&abspath, "/%s", path) == -1) {
568 err = got_error_from_errno("asprintf");
581 *in_repo_path = path;
585 const struct got_error *
586 got_repo_cache_packidx(struct got_repository *repo, struct got_packidx *packidx)
588 const struct got_error *err = NULL;
591 for (i = 0; i < nitems(repo->packidx_cache); i++) {
592 if (repo->packidx_cache[i] == NULL)
595 if (i == nitems(repo->packidx_cache)) {
596 err = got_packidx_close(repo->packidx_cache[i - 1]);
602 * Insert the new pack index at the front so it will
603 * be searched first in the future.
605 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
606 sizeof(repo->packidx_cache) -
607 sizeof(repo->packidx_cache[0]));
608 repo->packidx_cache[0] = packidx;
614 is_packidx_filename(const char *name, size_t len)
616 if (len != GOT_PACKIDX_NAMELEN)
619 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
622 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
623 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
629 const struct got_error *
630 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
631 struct got_repository *repo, struct got_object_id *id)
633 const struct got_error *err;
640 /* Search pack index cache. */
641 for (i = 0; i < nitems(repo->packidx_cache); i++) {
642 if (repo->packidx_cache[i] == NULL)
644 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
646 *packidx = repo->packidx_cache[i];
650 /* No luck. Search the filesystem. */
652 path_packdir = got_repo_get_path_objects_pack(repo);
653 if (path_packdir == NULL)
654 return got_error_from_errno("got_repo_get_path_objects_pack");
656 packdir = opendir(path_packdir);
657 if (packdir == NULL) {
658 err = got_error_from_errno2("opendir", path_packdir);
662 while ((dent = readdir(packdir)) != NULL) {
663 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
666 if (asprintf(&path_packidx, "%s/%s", path_packdir,
667 dent->d_name) == -1) {
668 err = got_error_from_errno("asprintf");
672 err = got_packidx_open(packidx, path_packidx, 0);
677 *idx = got_packidx_get_object_idx(*packidx, id);
679 err = NULL; /* found the object */
680 err = got_repo_cache_packidx(repo, *packidx);
684 err = got_packidx_close(*packidx);
690 err = got_error_no_obj(id);
693 if (packdir && closedir(packdir) != 0 && err == NULL)
694 err = got_error_from_errno("closedir");
698 static const struct got_error *
699 read_packfile_hdr(int fd, struct got_packidx *packidx)
701 const struct got_error *err = NULL;
702 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
703 struct got_packfile_hdr hdr;
706 n = read(fd, &hdr, sizeof(hdr));
708 return got_error_from_errno("read");
709 if (n != sizeof(hdr))
710 return got_error(GOT_ERR_BAD_PACKFILE);
712 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
713 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
714 betoh32(hdr.nobjects) != totobj)
715 err = got_error(GOT_ERR_BAD_PACKFILE);
720 static const struct got_error *
721 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
723 const struct got_error *err = NULL;
725 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
727 return got_error_from_errno2("open", path_packfile);
730 err = read_packfile_hdr(*fd, packidx);
740 const struct got_error *
741 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
742 const char *path_packfile, struct got_packidx *packidx)
744 const struct got_error *err = NULL;
745 struct got_pack *pack = NULL;
752 for (i = 0; i < nitems(repo->packs); i++) {
753 pack = &repo->packs[i];
754 if (pack->path_packfile == NULL)
756 if (strcmp(pack->path_packfile, path_packfile) == 0)
760 if (i == nitems(repo->packs) - 1) {
761 err = got_pack_close(&repo->packs[i - 1]);
764 memmove(&repo->packs[1], &repo->packs[0],
765 sizeof(repo->packs) - sizeof(repo->packs[0]));
769 pack = &repo->packs[i];
771 pack->path_packfile = strdup(path_packfile);
772 if (pack->path_packfile == NULL) {
773 err = got_error_from_errno("strdup");
777 err = open_packfile(&pack->fd, path_packfile, packidx);
781 if (fstat(pack->fd, &sb) != 0) {
782 err = got_error_from_errno("fstat");
785 pack->filesize = sb.st_size;
787 pack->privsep_child = NULL;
789 #ifndef GOT_PACK_NO_MMAP
790 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
792 if (pack->map == MAP_FAILED) {
793 if (errno != ENOMEM) {
794 err = got_error_from_errno("mmap");
797 pack->map = NULL; /* fall back to read(2) */
803 free(pack->path_packfile);
804 memset(pack, 0, sizeof(*pack));
812 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
814 struct got_pack *pack = NULL;
817 for (i = 0; i < nitems(repo->packs); i++) {
818 pack = &repo->packs[i];
819 if (pack->path_packfile == NULL)
821 if (strcmp(pack->path_packfile, path_packfile) == 0)
828 const struct got_error *
829 got_repo_init(const char *repo_path)
831 const struct got_error *err = NULL;
832 const char *dirnames[] = {
834 GOT_OBJECTS_PACK_DIR,
837 const char *description_str = "Unnamed repository; "
838 "edit this file 'description' to name the repository.";
839 const char *headref_str = "ref: refs/heads/master";
840 const char *gitconfig_str = "[core]\n"
841 "\trepositoryformatversion = 0\n"
842 "\tfilemode = true\n"
847 if (!got_path_dir_is_empty(repo_path))
848 return got_error(GOT_ERR_DIR_NOT_EMPTY);
850 for (i = 0; i < nitems(dirnames); i++) {
851 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
852 return got_error_from_errno("asprintf");
854 err = got_path_mkdir(path);
860 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
861 return got_error_from_errno("asprintf");
862 err = got_path_create_file(path, description_str);
867 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
868 return got_error_from_errno("asprintf");
869 err = got_path_create_file(path, headref_str);
874 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
875 return got_error_from_errno("asprintf");
876 err = got_path_create_file(path, gitconfig_str);
884 static const struct got_error *
885 match_packed_object(struct got_object_id **unique_id,
886 struct got_repository *repo, const char *id_str_prefix, int obj_type)
888 const struct got_error *err = NULL;
893 struct got_object_id_queue matched_ids;
895 SIMPLEQ_INIT(&matched_ids);
897 path_packdir = got_repo_get_path_objects_pack(repo);
898 if (path_packdir == NULL)
899 return got_error_from_errno("got_repo_get_path_objects_pack");
901 packdir = opendir(path_packdir);
902 if (packdir == NULL) {
903 err = got_error_from_errno2("opendir", path_packdir);
907 while ((dent = readdir(packdir)) != NULL) {
908 struct got_packidx *packidx;
909 struct got_object_qid *qid;
912 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
915 if (asprintf(&path_packidx, "%s/%s", path_packdir,
916 dent->d_name) == -1) {
917 err = got_error_from_errno("asprintf");
921 err = got_packidx_open(&packidx, path_packidx, 0);
926 err = got_packidx_match_id_str_prefix(&matched_ids,
927 packidx, id_str_prefix);
929 got_packidx_close(packidx);
932 err = got_packidx_close(packidx);
936 SIMPLEQ_FOREACH(qid, &matched_ids, entry) {
937 if (obj_type != GOT_OBJ_TYPE_ANY) {
939 err = got_object_get_type(&matched_type, repo,
943 if (matched_type != obj_type)
946 if (*unique_id == NULL) {
947 *unique_id = got_object_id_dup(qid->id);
948 if (*unique_id == NULL) {
949 err = got_error_from_errno("malloc");
953 err = got_error(GOT_ERR_AMBIGUOUS_ID);
959 got_object_id_queue_free(&matched_ids);
961 if (packdir && closedir(packdir) != 0 && err == NULL)
962 err = got_error_from_errno("closedir");
970 static const struct got_error *
971 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
972 const char *object_dir, const char *id_str_prefix, int obj_type,
973 struct got_repository *repo)
975 const struct got_error *err = NULL;
979 struct got_object_id id;
981 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
982 err = got_error_from_errno("asprintf");
988 if (errno == ENOENT) {
992 err = got_error_from_errno2("opendir", path);
995 while ((dent = readdir(dir)) != NULL) {
999 if (strcmp(dent->d_name, ".") == 0 ||
1000 strcmp(dent->d_name, "..") == 0)
1003 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1004 err = got_error_from_errno("asprintf");
1008 if (!got_parse_sha1_digest(id.sha1, id_str))
1012 * Directory entries do not necessarily appear in
1013 * sorted order, so we must iterate over all of them.
1015 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1021 if (*unique_id == NULL) {
1022 if (obj_type != GOT_OBJ_TYPE_ANY) {
1024 err = got_object_get_type(&matched_type, repo,
1028 if (matched_type != obj_type)
1031 *unique_id = got_object_id_dup(&id);
1032 if (*unique_id == NULL) {
1033 err = got_error_from_errno("got_object_id_dup");
1038 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1044 if (dir && closedir(dir) != 0 && err == NULL)
1045 err = got_error_from_errno("closedir");
1054 const struct got_error *
1055 got_repo_match_object_id_prefix(struct got_object_id **id,
1056 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1058 const struct got_error *err = NULL;
1059 char *path_objects = got_repo_get_path_objects(repo);
1060 char *object_dir = NULL;
1066 for (i = 0; i < strlen(id_str_prefix); i++) {
1067 if (isxdigit((unsigned char)id_str_prefix[i]))
1069 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
1072 len = strlen(id_str_prefix);
1074 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1077 object_dir = strndup(id_str_prefix, 2);
1078 if (object_dir == NULL) {
1079 err = got_error_from_errno("strdup");
1082 err = match_loose_object(id, path_objects, object_dir,
1083 id_str_prefix, obj_type, repo);
1084 } else if (len == 1) {
1086 for (i = 0; i < 0xf; i++) {
1087 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1089 err = got_error_from_errno("asprintf");
1092 err = match_packed_object(id, repo, object_dir,
1096 err = match_loose_object(id, path_objects, object_dir,
1097 id_str_prefix, obj_type, repo);
1102 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
1110 } else if (*id == NULL)
1111 err = got_error(GOT_ERR_NO_OBJ);
1116 static const struct got_error *
1117 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1118 const char *name, mode_t mode, struct got_object_id *blob_id)
1120 const struct got_error *err = NULL;
1124 *new_te = calloc(1, sizeof(**new_te));
1125 if (*new_te == NULL)
1126 return got_error_from_errno("calloc");
1128 (*new_te)->name = strdup(name);
1129 if ((*new_te)->name == NULL) {
1130 err = got_error_from_errno("strdup");
1134 (*new_te)->mode = S_IFREG | (mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
1135 (*new_te)->id = blob_id;
1137 if (err && *new_te) {
1138 got_object_tree_entry_close(*new_te);
1144 static const struct got_error *
1145 import_file(struct got_tree_entry **new_te, struct dirent *de,
1146 const char *path, struct got_repository *repo)
1148 const struct got_error *err;
1149 struct got_object_id *blob_id = NULL;
1153 if (asprintf(&filepath, "%s%s%s", path,
1154 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1155 return got_error_from_errno("asprintf");
1157 if (lstat(filepath, &sb) != 0) {
1158 err = got_error_from_errno2("lstat", path);
1162 err = got_object_blob_create(&blob_id, filepath, repo);
1166 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1175 static const struct got_error *
1176 insert_tree_entry(struct got_tree_entry *new_te,
1177 struct got_pathlist_head *paths)
1179 const struct got_error *err = NULL;
1180 struct got_pathlist_entry *new_pe;
1182 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1186 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1190 static const struct got_error *write_tree(struct got_object_id **,
1191 const char *, struct got_pathlist_head *, struct got_repository *,
1192 got_repo_import_cb progress_cb, void *progress_arg);
1194 static const struct got_error *
1195 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1196 const char *path, struct got_pathlist_head *ignores,
1197 struct got_repository *repo,
1198 got_repo_import_cb progress_cb, void *progress_arg)
1200 const struct got_error *err;
1203 if (asprintf(&subdirpath, "%s%s%s", path,
1204 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1205 return got_error_from_errno("asprintf");
1207 (*new_te) = calloc(1, sizeof(**new_te));
1208 (*new_te)->mode = S_IFDIR;
1209 (*new_te)->name = strdup(de->d_name);
1210 if ((*new_te)->name == NULL) {
1211 err = got_error_from_errno("strdup");
1215 err = write_tree(&(*new_te)->id, subdirpath, ignores, repo,
1216 progress_cb, progress_arg);
1220 got_object_tree_entry_close(*new_te);
1226 static const struct got_error *
1227 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1228 struct got_pathlist_head *ignores, struct got_repository *repo,
1229 got_repo_import_cb progress_cb, void *progress_arg)
1231 const struct got_error *err = NULL;
1234 struct got_tree_entries new_tree_entries;
1235 struct got_tree_entry *new_te = NULL;
1236 struct got_pathlist_head paths;
1237 struct got_pathlist_entry *pe;
1240 *new_tree_id = NULL;
1243 new_tree_entries.nentries = 0;
1244 SIMPLEQ_INIT(&new_tree_entries.head);
1246 dir = opendir(path_dir);
1248 err = got_error_from_errno2("opendir", path_dir);
1252 while ((de = readdir(dir)) != NULL) {
1255 if (strcmp(de->d_name, ".") == 0 ||
1256 strcmp(de->d_name, "..") == 0)
1259 TAILQ_FOREACH(pe, ignores, entry) {
1260 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1267 if (de->d_type == DT_DIR) {
1268 err = import_subdir(&new_te, de, path_dir,
1269 ignores, repo, progress_cb, progress_arg);
1272 } else if (de->d_type == DT_REG) {
1273 err = import_file(&new_te, de, path_dir, repo);
1279 err = insert_tree_entry(new_te, &paths);
1284 TAILQ_FOREACH(pe, &paths, entry) {
1285 struct got_tree_entry *te = pe->data;
1287 new_tree_entries.nentries++;
1288 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
1289 if (!S_ISREG(te->mode))
1291 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1292 err = got_error_from_errno("asprintf");
1295 err = (*progress_cb)(progress_arg, path);
1301 name = basename(path_dir);
1303 err = got_error_from_errno("basename");
1307 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
1311 got_object_tree_entries_close(&new_tree_entries);
1312 got_pathlist_free(&paths);
1316 const struct got_error *
1317 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1318 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1319 struct got_repository *repo, got_repo_import_cb progress_cb,
1322 const struct got_error *err;
1323 struct got_object_id *new_tree_id;
1325 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1326 progress_cb, progress_arg);
1330 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1331 author, time(NULL), author, time(NULL), logmsg, repo);