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_cancel.h"
45 #include "got_worktree.h"
46 #include "got_object.h"
48 #include "got_lib_delta.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_object.h"
51 #include "got_lib_object_parse.h"
52 #include "got_lib_object_create.h"
53 #include "got_lib_pack.h"
54 #include "got_lib_privsep.h"
55 #include "got_lib_worktree.h"
56 #include "got_lib_sha1.h"
57 #include "got_lib_object_cache.h"
58 #include "got_lib_repository.h"
59 #include "got_lib_gitconfig.h"
62 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
65 #define GOT_GIT_DIR ".git"
67 /* Mandatory files and directories inside the git directory. */
68 #define GOT_OBJECTS_DIR "objects"
69 #define GOT_REFS_DIR "refs"
70 #define GOT_HEAD_FILE "HEAD"
71 #define GOT_GITCONFIG "config"
73 /* Other files and directories inside the git directory. */
74 #define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
75 #define GOT_ORIG_HEAD_FILE "ORIG_HEAD"
76 #define GOT_OBJECTS_PACK_DIR "objects/pack"
77 #define GOT_PACKED_REFS_FILE "packed-refs"
80 got_repo_get_path(struct got_repository *repo)
86 got_repo_get_path_git_dir(struct got_repository *repo)
88 return repo->path_git_dir;
92 got_repo_is_bare(struct got_repository *repo)
94 return (strcmp(repo->path, repo->path_git_dir) == 0);
98 get_path_git_child(struct got_repository *repo, const char *basename)
102 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
110 got_repo_get_path_objects(struct got_repository *repo)
112 return get_path_git_child(repo, GOT_OBJECTS_DIR);
116 got_repo_get_path_objects_pack(struct got_repository *repo)
118 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
122 got_repo_get_path_refs(struct got_repository *repo)
124 return get_path_git_child(repo, GOT_REFS_DIR);
128 got_repo_get_path_packed_refs(struct got_repository *repo)
130 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
134 get_path_head(struct got_repository *repo)
136 return get_path_git_child(repo, GOT_HEAD_FILE);
139 static const struct got_error *
140 get_path_gitconfig(char **p, struct got_repository *repo)
142 *p = get_path_git_child(repo, GOT_GITCONFIG);
144 return got_error_from_errno("asprintf");
149 is_git_repo(struct got_repository *repo)
151 const char *path_git = got_repo_get_path_git_dir(repo);
152 char *path_objects = got_repo_get_path_objects(repo);
153 char *path_refs = got_repo_get_path_refs(repo);
154 char *path_head = get_path_head(repo);
157 struct got_reference *head_ref;
159 if (lstat(path_git, &sb) == -1)
161 if (!S_ISDIR(sb.st_mode))
164 if (lstat(path_objects, &sb) == -1)
166 if (!S_ISDIR(sb.st_mode))
169 if (lstat(path_refs, &sb) == -1)
171 if (!S_ISDIR(sb.st_mode))
174 if (lstat(path_head, &sb) == -1)
176 if (!S_ISREG(sb.st_mode))
179 /* Check if the HEAD reference can be opened. */
180 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
182 got_ref_close(head_ref);
193 const struct got_error *
194 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
195 struct got_object *obj)
197 #ifndef GOT_NO_OBJ_CACHE
198 const struct got_error *err = NULL;
199 err = got_object_cache_add(&repo->objcache, id, obj);
201 if (err->code == GOT_ERR_OBJ_EXISTS ||
202 err->code == GOT_ERR_OBJ_TOO_LARGE)
212 got_repo_get_cached_object(struct got_repository *repo,
213 struct got_object_id *id)
215 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
218 const struct got_error *
219 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
220 struct got_tree_object *tree)
222 #ifndef GOT_NO_OBJ_CACHE
223 const struct got_error *err = NULL;
224 err = got_object_cache_add(&repo->treecache, id, tree);
226 if (err->code == GOT_ERR_OBJ_EXISTS ||
227 err->code == GOT_ERR_OBJ_TOO_LARGE)
236 struct got_tree_object *
237 got_repo_get_cached_tree(struct got_repository *repo,
238 struct got_object_id *id)
240 return (struct got_tree_object *)got_object_cache_get(
241 &repo->treecache, id);
244 const struct got_error *
245 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
246 struct got_commit_object *commit)
248 #ifndef GOT_NO_OBJ_CACHE
249 const struct got_error *err = NULL;
250 err = got_object_cache_add(&repo->commitcache, id, commit);
252 if (err->code == GOT_ERR_OBJ_EXISTS ||
253 err->code == GOT_ERR_OBJ_TOO_LARGE)
262 struct got_commit_object *
263 got_repo_get_cached_commit(struct got_repository *repo,
264 struct got_object_id *id)
266 return (struct got_commit_object *)got_object_cache_get(
267 &repo->commitcache, id);
270 const struct got_error *
271 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
272 struct got_tag_object *tag)
274 #ifndef GOT_NO_OBJ_CACHE
275 const struct got_error *err = NULL;
276 err = got_object_cache_add(&repo->tagcache, id, tag);
278 if (err->code == GOT_ERR_OBJ_EXISTS ||
279 err->code == GOT_ERR_OBJ_TOO_LARGE)
288 struct got_tag_object *
289 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
291 return (struct got_tag_object *)got_object_cache_get(
292 &repo->tagcache, id);
295 const struct got_error *
296 open_repo(struct got_repository *repo, const char *path)
298 const struct got_error *err = NULL;
300 /* bare git repository? */
301 repo->path_git_dir = strdup(path);
302 if (repo->path_git_dir == NULL)
303 return got_error_from_errno("strdup");
304 if (is_git_repo(repo)) {
305 repo->path = strdup(repo->path_git_dir);
306 if (repo->path == NULL) {
307 err = got_error_from_errno("strdup");
313 /* git repository with working tree? */
314 free(repo->path_git_dir);
315 repo->path_git_dir = NULL;
316 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
317 err = got_error_from_errno("asprintf");
320 if (is_git_repo(repo)) {
321 repo->path = strdup(path);
322 if (repo->path == NULL) {
323 err = got_error_from_errno("strdup");
329 err = got_error(GOT_ERR_NOT_GIT_REPO);
334 free(repo->path_git_dir);
335 repo->path_git_dir = NULL;
340 const struct got_error *
341 got_repo_open(struct got_repository **repop, const char *path)
343 struct got_repository *repo = NULL;
344 const struct got_error *err = NULL;
345 char *abspath, *gitconfig_path = NULL;
346 int i, tried_root = 0;
350 if (got_path_is_absolute(path))
351 abspath = strdup(path);
353 abspath = got_path_get_absolute(path);
355 return got_error(GOT_ERR_BAD_PATH);
357 repo = calloc(1, sizeof(*repo));
359 err = got_error_from_errno("calloc");
363 for (i = 0; i < nitems(repo->privsep_children); i++) {
364 memset(&repo->privsep_children[i], 0,
365 sizeof(repo->privsep_children[0]));
366 repo->privsep_children[i].imsg_fd = -1;
369 err = got_object_cache_init(&repo->objcache,
370 GOT_OBJECT_CACHE_TYPE_OBJ);
373 err = got_object_cache_init(&repo->treecache,
374 GOT_OBJECT_CACHE_TYPE_TREE);
377 err = got_object_cache_init(&repo->commitcache,
378 GOT_OBJECT_CACHE_TYPE_COMMIT);
381 err = got_object_cache_init(&repo->tagcache,
382 GOT_OBJECT_CACHE_TYPE_TAG);
386 path = realpath(abspath, NULL);
388 err = got_error_from_errno2("realpath", abspath);
393 err = open_repo(repo, path);
396 if (err->code != GOT_ERR_NOT_GIT_REPO)
398 if (path[0] == '/' && path[1] == '\0') {
400 err = got_error(GOT_ERR_NOT_GIT_REPO);
405 path = dirname(path);
407 err = got_error_from_errno2("dirname", path);
412 err = get_path_gitconfig(&gitconfig_path, repo);
417 err = got_gitconfig_open(&repo->gitconfig, gitconfig_path);
421 repo->gitconfig = NULL;
425 got_repo_close(repo);
429 free(gitconfig_path);
433 const struct got_error *
434 got_repo_close(struct got_repository *repo)
436 const struct got_error *err = NULL, *child_err;
439 for (i = 0; i < nitems(repo->packidx_cache); i++) {
440 if (repo->packidx_cache[i] == NULL)
442 got_packidx_close(repo->packidx_cache[i]);
445 for (i = 0; i < nitems(repo->packs); i++) {
446 if (repo->packs[i].path_packfile == NULL)
448 got_pack_close(&repo->packs[i]);
452 free(repo->path_git_dir);
454 got_object_cache_close(&repo->objcache);
455 got_object_cache_close(&repo->treecache);
456 got_object_cache_close(&repo->commitcache);
457 got_object_cache_close(&repo->tagcache);
459 for (i = 0; i < nitems(repo->privsep_children); i++) {
460 if (repo->privsep_children[i].imsg_fd == -1)
462 imsg_clear(repo->privsep_children[i].ibuf);
463 free(repo->privsep_children[i].ibuf);
464 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
465 child_err = got_privsep_wait_for_child(
466 repo->privsep_children[i].pid);
467 if (child_err && err == NULL)
469 if (close(repo->privsep_children[i].imsg_fd) != 0 &&
471 err = got_error_from_errno("close");
474 got_gitconfig_close(repo->gitconfig);
480 const struct got_error *
481 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
482 const char *input_path, int check_disk)
484 const struct got_error *err = NULL;
485 const char *repo_abspath = NULL;
486 size_t repolen, cwdlen, len;
487 char *cwd, *canonpath, *path = NULL;
489 *in_repo_path = NULL;
491 cwd = getcwd(NULL, 0);
493 return got_error_from_errno("getcwd");
495 canonpath = strdup(input_path);
496 if (canonpath == NULL) {
497 err = got_error_from_errno("strdup");
500 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
504 repo_abspath = got_repo_get_path(repo);
506 if (!check_disk || canonpath[0] == '\0') {
507 path = strdup(canonpath);
509 err = got_error_from_errno("strdup");
513 int is_repo_child = 0, is_cwd_child = 0;
515 path = realpath(canonpath, NULL);
517 if (errno != ENOENT) {
518 err = got_error_from_errno2("realpath",
523 * Path is not on disk.
524 * Assume it is already relative to repository root.
526 path = strdup(canonpath);
528 err = got_error_from_errno("strdup");
533 repolen = strlen(repo_abspath);
534 cwdlen = strlen(cwd);
537 if (len > repolen && strncmp(path, repo_abspath, repolen) == 0)
539 if (len > cwdlen && strncmp(path, cwd, cwdlen) == 0)
542 if (strcmp(path, repo_abspath) == 0) {
546 err = got_error_from_errno("strdup");
549 } else if (is_repo_child && is_cwd_child) {
551 /* Strip common prefix with repository path. */
552 err = got_path_skip_common_ancestor(&child,
558 } else if (is_repo_child) {
559 /* Matched an on-disk path inside repository. */
560 if (got_repo_is_bare(repo)) {
562 * Matched an on-disk path inside repository
563 * database. Treat as repository-relative.
567 /* Strip common prefix with repository path. */
568 err = got_path_skip_common_ancestor(&child,
575 } else if (is_cwd_child) {
577 /* Strip common prefix with cwd. */
578 err = got_path_skip_common_ancestor(&child, cwd,
586 * Matched unrelated on-disk path.
587 * Treat it as repository-relative.
592 /* Make in-repository path absolute */
593 if (path[0] != '/') {
595 if (asprintf(&abspath, "/%s", path) == -1) {
596 err = got_error_from_errno("asprintf");
609 *in_repo_path = path;
613 const struct got_error *
614 got_repo_cache_packidx(struct got_repository *repo, struct got_packidx *packidx)
616 const struct got_error *err = NULL;
619 for (i = 0; i < nitems(repo->packidx_cache); i++) {
620 if (repo->packidx_cache[i] == NULL)
623 if (i == nitems(repo->packidx_cache)) {
624 err = got_packidx_close(repo->packidx_cache[i - 1]);
630 * Insert the new pack index at the front so it will
631 * be searched first in the future.
633 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
634 sizeof(repo->packidx_cache) -
635 sizeof(repo->packidx_cache[0]));
636 repo->packidx_cache[0] = packidx;
642 is_packidx_filename(const char *name, size_t len)
644 if (len != GOT_PACKIDX_NAMELEN)
647 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
650 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
651 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
657 const struct got_error *
658 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
659 struct got_repository *repo, struct got_object_id *id)
661 const struct got_error *err;
668 /* Search pack index cache. */
669 for (i = 0; i < nitems(repo->packidx_cache); i++) {
670 if (repo->packidx_cache[i] == NULL)
672 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
674 *packidx = repo->packidx_cache[i];
678 /* No luck. Search the filesystem. */
680 path_packdir = got_repo_get_path_objects_pack(repo);
681 if (path_packdir == NULL)
682 return got_error_from_errno("got_repo_get_path_objects_pack");
684 packdir = opendir(path_packdir);
685 if (packdir == NULL) {
687 err = got_error_no_obj(id);
689 err = got_error_from_errno2("opendir", path_packdir);
693 while ((dent = readdir(packdir)) != NULL) {
694 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
697 if (asprintf(&path_packidx, "%s/%s", path_packdir,
698 dent->d_name) == -1) {
699 err = got_error_from_errno("asprintf");
703 err = got_packidx_open(packidx, path_packidx, 0);
708 *idx = got_packidx_get_object_idx(*packidx, id);
710 err = NULL; /* found the object */
711 err = got_repo_cache_packidx(repo, *packidx);
715 err = got_packidx_close(*packidx);
721 err = got_error_no_obj(id);
724 if (packdir && closedir(packdir) != 0 && err == NULL)
725 err = got_error_from_errno("closedir");
729 static const struct got_error *
730 read_packfile_hdr(int fd, struct got_packidx *packidx)
732 const struct got_error *err = NULL;
733 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
734 struct got_packfile_hdr hdr;
737 n = read(fd, &hdr, sizeof(hdr));
739 return got_error_from_errno("read");
740 if (n != sizeof(hdr))
741 return got_error(GOT_ERR_BAD_PACKFILE);
743 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
744 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
745 betoh32(hdr.nobjects) != totobj)
746 err = got_error(GOT_ERR_BAD_PACKFILE);
751 static const struct got_error *
752 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
754 const struct got_error *err = NULL;
756 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
758 return got_error_from_errno2("open", path_packfile);
761 err = read_packfile_hdr(*fd, packidx);
771 const struct got_error *
772 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
773 const char *path_packfile, struct got_packidx *packidx)
775 const struct got_error *err = NULL;
776 struct got_pack *pack = NULL;
783 for (i = 0; i < nitems(repo->packs); i++) {
784 pack = &repo->packs[i];
785 if (pack->path_packfile == NULL)
787 if (strcmp(pack->path_packfile, path_packfile) == 0)
791 if (i == nitems(repo->packs) - 1) {
792 err = got_pack_close(&repo->packs[i - 1]);
795 memmove(&repo->packs[1], &repo->packs[0],
796 sizeof(repo->packs) - sizeof(repo->packs[0]));
800 pack = &repo->packs[i];
802 pack->path_packfile = strdup(path_packfile);
803 if (pack->path_packfile == NULL) {
804 err = got_error_from_errno("strdup");
808 err = open_packfile(&pack->fd, path_packfile, packidx);
812 if (fstat(pack->fd, &sb) != 0) {
813 err = got_error_from_errno("fstat");
816 pack->filesize = sb.st_size;
818 pack->privsep_child = NULL;
820 #ifndef GOT_PACK_NO_MMAP
821 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
823 if (pack->map == MAP_FAILED) {
824 if (errno != ENOMEM) {
825 err = got_error_from_errno("mmap");
828 pack->map = NULL; /* fall back to read(2) */
834 free(pack->path_packfile);
835 memset(pack, 0, sizeof(*pack));
843 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
845 struct got_pack *pack = NULL;
848 for (i = 0; i < nitems(repo->packs); i++) {
849 pack = &repo->packs[i];
850 if (pack->path_packfile == NULL)
852 if (strcmp(pack->path_packfile, path_packfile) == 0)
859 const struct got_error *
860 got_repo_init(const char *repo_path)
862 const struct got_error *err = NULL;
863 const char *dirnames[] = {
865 GOT_OBJECTS_PACK_DIR,
868 const char *description_str = "Unnamed repository; "
869 "edit this file 'description' to name the repository.";
870 const char *headref_str = "ref: refs/heads/master";
871 const char *gitconfig_str = "[core]\n"
872 "\trepositoryformatversion = 0\n"
873 "\tfilemode = true\n"
878 if (!got_path_dir_is_empty(repo_path))
879 return got_error(GOT_ERR_DIR_NOT_EMPTY);
881 for (i = 0; i < nitems(dirnames); i++) {
882 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
883 return got_error_from_errno("asprintf");
885 err = got_path_mkdir(path);
891 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
892 return got_error_from_errno("asprintf");
893 err = got_path_create_file(path, description_str);
898 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
899 return got_error_from_errno("asprintf");
900 err = got_path_create_file(path, headref_str);
905 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
906 return got_error_from_errno("asprintf");
907 err = got_path_create_file(path, gitconfig_str);
915 static const struct got_error *
916 match_packed_object(struct got_object_id **unique_id,
917 struct got_repository *repo, const char *id_str_prefix, int obj_type)
919 const struct got_error *err = NULL;
924 struct got_object_id_queue matched_ids;
926 SIMPLEQ_INIT(&matched_ids);
928 path_packdir = got_repo_get_path_objects_pack(repo);
929 if (path_packdir == NULL)
930 return got_error_from_errno("got_repo_get_path_objects_pack");
932 packdir = opendir(path_packdir);
933 if (packdir == NULL) {
935 err = got_error_from_errno2("opendir", path_packdir);
939 while ((dent = readdir(packdir)) != NULL) {
940 struct got_packidx *packidx;
941 struct got_object_qid *qid;
944 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
947 if (asprintf(&path_packidx, "%s/%s", path_packdir,
948 dent->d_name) == -1) {
949 err = got_error_from_errno("asprintf");
953 err = got_packidx_open(&packidx, path_packidx, 0);
958 err = got_packidx_match_id_str_prefix(&matched_ids,
959 packidx, id_str_prefix);
961 got_packidx_close(packidx);
964 err = got_packidx_close(packidx);
968 SIMPLEQ_FOREACH(qid, &matched_ids, entry) {
969 if (obj_type != GOT_OBJ_TYPE_ANY) {
971 err = got_object_get_type(&matched_type, repo,
975 if (matched_type != obj_type)
978 if (*unique_id == NULL) {
979 *unique_id = got_object_id_dup(qid->id);
980 if (*unique_id == NULL) {
981 err = got_error_from_errno("malloc");
985 err = got_error(GOT_ERR_AMBIGUOUS_ID);
991 got_object_id_queue_free(&matched_ids);
993 if (packdir && closedir(packdir) != 0 && err == NULL)
994 err = got_error_from_errno("closedir");
1002 static const struct got_error *
1003 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1004 const char *object_dir, const char *id_str_prefix, int obj_type,
1005 struct got_repository *repo)
1007 const struct got_error *err = NULL;
1010 struct dirent *dent;
1011 struct got_object_id id;
1013 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1014 err = got_error_from_errno("asprintf");
1018 dir = opendir(path);
1020 if (errno == ENOENT) {
1024 err = got_error_from_errno2("opendir", path);
1027 while ((dent = readdir(dir)) != NULL) {
1031 if (strcmp(dent->d_name, ".") == 0 ||
1032 strcmp(dent->d_name, "..") == 0)
1035 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1036 err = got_error_from_errno("asprintf");
1040 if (!got_parse_sha1_digest(id.sha1, id_str))
1044 * Directory entries do not necessarily appear in
1045 * sorted order, so we must iterate over all of them.
1047 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1053 if (*unique_id == NULL) {
1054 if (obj_type != GOT_OBJ_TYPE_ANY) {
1056 err = got_object_get_type(&matched_type, repo,
1060 if (matched_type != obj_type)
1063 *unique_id = got_object_id_dup(&id);
1064 if (*unique_id == NULL) {
1065 err = got_error_from_errno("got_object_id_dup");
1070 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1076 if (dir && closedir(dir) != 0 && err == NULL)
1077 err = got_error_from_errno("closedir");
1086 const struct got_error *
1087 got_repo_match_object_id_prefix(struct got_object_id **id,
1088 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1090 const struct got_error *err = NULL;
1091 char *path_objects = got_repo_get_path_objects(repo);
1092 char *object_dir = NULL;
1098 for (i = 0; i < strlen(id_str_prefix); i++) {
1099 if (isxdigit((unsigned char)id_str_prefix[i]))
1101 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
1104 len = strlen(id_str_prefix);
1106 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1109 object_dir = strndup(id_str_prefix, 2);
1110 if (object_dir == NULL) {
1111 err = got_error_from_errno("strdup");
1114 err = match_loose_object(id, path_objects, object_dir,
1115 id_str_prefix, obj_type, repo);
1116 } else if (len == 1) {
1118 for (i = 0; i < 0xf; i++) {
1119 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1121 err = got_error_from_errno("asprintf");
1124 err = match_packed_object(id, repo, object_dir,
1128 err = match_loose_object(id, path_objects, object_dir,
1129 id_str_prefix, obj_type, repo);
1134 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
1142 } else if (*id == NULL)
1143 err = got_error(GOT_ERR_NO_OBJ);
1148 const struct got_error *
1149 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1150 int obj_type, struct got_repository *repo)
1152 const struct got_error *err;
1153 struct got_reflist_head refs;
1154 struct got_reflist_entry *re;
1155 struct got_object_id *tag_id;
1157 SIMPLEQ_INIT(&refs);
1160 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_by_name, NULL);
1164 SIMPLEQ_FOREACH(re, &refs, entry) {
1165 const char *refname;
1166 refname = got_ref_get_name(re->ref);
1167 if (got_ref_is_symbolic(re->ref))
1169 refname += strlen("refs/tags/");
1170 if (strcmp(refname, name) != 0)
1172 err = got_ref_resolve(&tag_id, repo, re->ref);
1175 err = got_object_open_as_tag(tag, repo, tag_id);
1179 if (obj_type == GOT_OBJ_TYPE_ANY ||
1180 got_object_tag_get_object_type(*tag) == obj_type)
1182 got_object_tag_close(*tag);
1186 got_ref_list_free(&refs);
1187 if (err == NULL && *tag == NULL)
1188 err = got_error(GOT_ERR_NO_OBJ);
1192 static const struct got_error *
1193 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1194 const char *name, mode_t mode, struct got_object_id *blob_id)
1196 const struct got_error *err = NULL;
1200 *new_te = calloc(1, sizeof(**new_te));
1201 if (*new_te == NULL)
1202 return got_error_from_errno("calloc");
1204 (*new_te)->name = strdup(name);
1205 if ((*new_te)->name == NULL) {
1206 err = got_error_from_errno("strdup");
1210 (*new_te)->mode = S_IFREG | (mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
1211 (*new_te)->id = blob_id;
1213 if (err && *new_te) {
1214 got_object_tree_entry_close(*new_te);
1220 static const struct got_error *
1221 import_file(struct got_tree_entry **new_te, struct dirent *de,
1222 const char *path, struct got_repository *repo)
1224 const struct got_error *err;
1225 struct got_object_id *blob_id = NULL;
1229 if (asprintf(&filepath, "%s%s%s", path,
1230 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1231 return got_error_from_errno("asprintf");
1233 if (lstat(filepath, &sb) != 0) {
1234 err = got_error_from_errno2("lstat", path);
1238 err = got_object_blob_create(&blob_id, filepath, repo);
1242 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1251 static const struct got_error *
1252 insert_tree_entry(struct got_tree_entry *new_te,
1253 struct got_pathlist_head *paths)
1255 const struct got_error *err = NULL;
1256 struct got_pathlist_entry *new_pe;
1258 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1262 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1266 static const struct got_error *write_tree(struct got_object_id **,
1267 const char *, struct got_pathlist_head *, struct got_repository *,
1268 got_repo_import_cb progress_cb, void *progress_arg);
1270 static const struct got_error *
1271 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1272 const char *path, struct got_pathlist_head *ignores,
1273 struct got_repository *repo,
1274 got_repo_import_cb progress_cb, void *progress_arg)
1276 const struct got_error *err;
1279 if (asprintf(&subdirpath, "%s%s%s", path,
1280 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1281 return got_error_from_errno("asprintf");
1283 (*new_te) = calloc(1, sizeof(**new_te));
1284 (*new_te)->mode = S_IFDIR;
1285 (*new_te)->name = strdup(de->d_name);
1286 if ((*new_te)->name == NULL) {
1287 err = got_error_from_errno("strdup");
1291 err = write_tree(&(*new_te)->id, subdirpath, ignores, repo,
1292 progress_cb, progress_arg);
1296 got_object_tree_entry_close(*new_te);
1302 static const struct got_error *
1303 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1304 struct got_pathlist_head *ignores, struct got_repository *repo,
1305 got_repo_import_cb progress_cb, void *progress_arg)
1307 const struct got_error *err = NULL;
1310 struct got_tree_entries new_tree_entries;
1311 struct got_tree_entry *new_te = NULL;
1312 struct got_pathlist_head paths;
1313 struct got_pathlist_entry *pe;
1315 *new_tree_id = NULL;
1318 new_tree_entries.nentries = 0;
1319 SIMPLEQ_INIT(&new_tree_entries.head);
1321 dir = opendir(path_dir);
1323 err = got_error_from_errno2("opendir", path_dir);
1327 while ((de = readdir(dir)) != NULL) {
1330 if (strcmp(de->d_name, ".") == 0 ||
1331 strcmp(de->d_name, "..") == 0)
1334 TAILQ_FOREACH(pe, ignores, entry) {
1335 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1342 if (de->d_type == DT_DIR) {
1343 err = import_subdir(&new_te, de, path_dir,
1344 ignores, repo, progress_cb, progress_arg);
1347 } else if (de->d_type == DT_REG) {
1348 err = import_file(&new_te, de, path_dir, repo);
1354 err = insert_tree_entry(new_te, &paths);
1359 TAILQ_FOREACH(pe, &paths, entry) {
1360 struct got_tree_entry *te = pe->data;
1362 new_tree_entries.nentries++;
1363 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
1364 if (!S_ISREG(te->mode))
1366 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1367 err = got_error_from_errno("asprintf");
1370 err = (*progress_cb)(progress_arg, path);
1376 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
1380 got_object_tree_entries_close(&new_tree_entries);
1381 got_pathlist_free(&paths);
1385 const struct got_error *
1386 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1387 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1388 struct got_repository *repo, got_repo_import_cb progress_cb,
1391 const struct got_error *err;
1392 struct got_object_id *new_tree_id;
1394 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1395 progress_cb, progress_arg);
1399 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1400 author, time(NULL), author, time(NULL), logmsg, repo);