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 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
316 err = got_error_from_errno("asprintf");
319 if (is_git_repo(repo)) {
320 repo->path = strdup(path);
321 if (repo->path == NULL) {
322 err = got_error_from_errno("strdup");
328 err = got_error(GOT_ERR_NOT_GIT_REPO);
333 free(repo->path_git_dir);
334 repo->path_git_dir = NULL;
339 const struct got_error *
340 got_repo_open(struct got_repository **repop, const char *path)
342 struct got_repository *repo = NULL;
343 const struct got_error *err = NULL;
344 char *abspath, *gitconfig_path = NULL;
345 int i, tried_root = 0;
349 if (got_path_is_absolute(path))
350 abspath = strdup(path);
352 abspath = got_path_get_absolute(path);
354 return got_error(GOT_ERR_BAD_PATH);
356 repo = calloc(1, sizeof(*repo));
358 err = got_error_from_errno("calloc");
362 for (i = 0; i < nitems(repo->privsep_children); i++) {
363 memset(&repo->privsep_children[i], 0,
364 sizeof(repo->privsep_children[0]));
365 repo->privsep_children[i].imsg_fd = -1;
368 err = got_object_cache_init(&repo->objcache,
369 GOT_OBJECT_CACHE_TYPE_OBJ);
372 err = got_object_cache_init(&repo->treecache,
373 GOT_OBJECT_CACHE_TYPE_TREE);
376 err = got_object_cache_init(&repo->commitcache,
377 GOT_OBJECT_CACHE_TYPE_COMMIT);
380 err = got_object_cache_init(&repo->tagcache,
381 GOT_OBJECT_CACHE_TYPE_TAG);
385 path = realpath(abspath, NULL);
387 err = got_error_from_errno2("realpath", abspath);
392 err = open_repo(repo, path);
395 if (err->code != GOT_ERR_NOT_GIT_REPO)
397 if (path[0] == '/' && path[1] == '\0') {
399 err = got_error(GOT_ERR_NOT_GIT_REPO);
404 path = dirname(path);
406 err = got_error_from_errno2("dirname", path);
409 err = get_path_gitconfig(&gitconfig_path, repo);
414 err = got_gitconfig_open(&repo->gitconfig, gitconfig_path);
418 repo->gitconfig = NULL;
422 got_repo_close(repo);
426 free(gitconfig_path);
430 const struct got_error *
431 got_repo_close(struct got_repository *repo)
433 const struct got_error *err = NULL, *child_err;
436 for (i = 0; i < nitems(repo->packidx_cache); i++) {
437 if (repo->packidx_cache[i] == NULL)
439 got_packidx_close(repo->packidx_cache[i]);
442 for (i = 0; i < nitems(repo->packs); i++) {
443 if (repo->packs[i].path_packfile == NULL)
445 got_pack_close(&repo->packs[i]);
449 free(repo->path_git_dir);
451 got_object_cache_close(&repo->objcache);
452 got_object_cache_close(&repo->treecache);
453 got_object_cache_close(&repo->commitcache);
454 got_object_cache_close(&repo->tagcache);
456 for (i = 0; i < nitems(repo->privsep_children); i++) {
457 if (repo->privsep_children[i].imsg_fd == -1)
459 imsg_clear(repo->privsep_children[i].ibuf);
460 free(repo->privsep_children[i].ibuf);
461 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
462 child_err = got_privsep_wait_for_child(
463 repo->privsep_children[i].pid);
464 if (child_err && err == NULL)
466 if (close(repo->privsep_children[i].imsg_fd) != 0 &&
468 err = got_error_from_errno("close");
471 got_gitconfig_close(repo->gitconfig);
477 const struct got_error *
478 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
479 const char *input_path, int check_disk)
481 const struct got_error *err = NULL;
482 const char *repo_abspath = NULL;
483 size_t repolen, cwdlen, len;
484 char *cwd, *canonpath, *path = NULL;
486 *in_repo_path = NULL;
488 cwd = getcwd(NULL, 0);
490 return got_error_from_errno("getcwd");
492 canonpath = strdup(input_path);
493 if (canonpath == NULL) {
494 err = got_error_from_errno("strdup");
497 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
501 repo_abspath = got_repo_get_path(repo);
503 if (!check_disk || canonpath[0] == '\0') {
504 path = strdup(canonpath);
506 err = got_error_from_errno("strdup");
510 int is_repo_child = 0, is_cwd_child = 0;
512 path = realpath(canonpath, NULL);
514 if (errno != ENOENT) {
515 err = got_error_from_errno2("realpath",
520 * Path is not on disk.
521 * Assume it is already relative to repository root.
523 path = strdup(canonpath);
525 err = got_error_from_errno("strdup");
530 repolen = strlen(repo_abspath);
531 cwdlen = strlen(cwd);
534 if (len > repolen && strncmp(path, repo_abspath, repolen) == 0)
536 if (len > cwdlen && strncmp(path, cwd, cwdlen) == 0)
539 if (strcmp(path, repo_abspath) == 0) {
543 err = got_error_from_errno("strdup");
546 } else if (is_repo_child && is_cwd_child) {
548 /* Strip common prefix with repository path. */
549 err = got_path_skip_common_ancestor(&child,
555 } else if (is_repo_child) {
556 /* Matched an on-disk path inside repository. */
557 if (got_repo_is_bare(repo)) {
559 * Matched an on-disk path inside repository
560 * database. Treat as repository-relative.
564 /* Strip common prefix with repository path. */
565 err = got_path_skip_common_ancestor(&child,
572 } else if (is_cwd_child) {
574 /* Strip common prefix with cwd. */
575 err = got_path_skip_common_ancestor(&child, cwd,
583 * Matched unrelated on-disk path.
584 * Treat it as repository-relative.
589 /* Make in-repository path absolute */
590 if (path[0] != '/') {
592 if (asprintf(&abspath, "/%s", path) == -1) {
593 err = got_error_from_errno("asprintf");
606 *in_repo_path = path;
610 const struct got_error *
611 got_repo_cache_packidx(struct got_repository *repo, struct got_packidx *packidx)
613 const struct got_error *err = NULL;
616 for (i = 0; i < nitems(repo->packidx_cache); i++) {
617 if (repo->packidx_cache[i] == NULL)
620 if (i == nitems(repo->packidx_cache)) {
621 err = got_packidx_close(repo->packidx_cache[i - 1]);
627 * Insert the new pack index at the front so it will
628 * be searched first in the future.
630 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
631 sizeof(repo->packidx_cache) -
632 sizeof(repo->packidx_cache[0]));
633 repo->packidx_cache[0] = packidx;
639 is_packidx_filename(const char *name, size_t len)
641 if (len != GOT_PACKIDX_NAMELEN)
644 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
647 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
648 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
654 const struct got_error *
655 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
656 struct got_repository *repo, struct got_object_id *id)
658 const struct got_error *err;
665 /* Search pack index cache. */
666 for (i = 0; i < nitems(repo->packidx_cache); i++) {
667 if (repo->packidx_cache[i] == NULL)
669 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
671 *packidx = repo->packidx_cache[i];
675 /* No luck. Search the filesystem. */
677 path_packdir = got_repo_get_path_objects_pack(repo);
678 if (path_packdir == NULL)
679 return got_error_from_errno("got_repo_get_path_objects_pack");
681 packdir = opendir(path_packdir);
682 if (packdir == NULL) {
684 err = got_error_no_obj(id);
686 err = got_error_from_errno2("opendir", path_packdir);
690 while ((dent = readdir(packdir)) != NULL) {
691 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
694 if (asprintf(&path_packidx, "%s/%s", path_packdir,
695 dent->d_name) == -1) {
696 err = got_error_from_errno("asprintf");
700 err = got_packidx_open(packidx, path_packidx, 0);
705 *idx = got_packidx_get_object_idx(*packidx, id);
707 err = NULL; /* found the object */
708 err = got_repo_cache_packidx(repo, *packidx);
712 err = got_packidx_close(*packidx);
718 err = got_error_no_obj(id);
721 if (packdir && closedir(packdir) != 0 && err == NULL)
722 err = got_error_from_errno("closedir");
726 static const struct got_error *
727 read_packfile_hdr(int fd, struct got_packidx *packidx)
729 const struct got_error *err = NULL;
730 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
731 struct got_packfile_hdr hdr;
734 n = read(fd, &hdr, sizeof(hdr));
736 return got_error_from_errno("read");
737 if (n != sizeof(hdr))
738 return got_error(GOT_ERR_BAD_PACKFILE);
740 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
741 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
742 betoh32(hdr.nobjects) != totobj)
743 err = got_error(GOT_ERR_BAD_PACKFILE);
748 static const struct got_error *
749 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
751 const struct got_error *err = NULL;
753 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
755 return got_error_from_errno2("open", path_packfile);
758 err = read_packfile_hdr(*fd, packidx);
768 const struct got_error *
769 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
770 const char *path_packfile, struct got_packidx *packidx)
772 const struct got_error *err = NULL;
773 struct got_pack *pack = NULL;
780 for (i = 0; i < nitems(repo->packs); i++) {
781 pack = &repo->packs[i];
782 if (pack->path_packfile == NULL)
784 if (strcmp(pack->path_packfile, path_packfile) == 0)
788 if (i == nitems(repo->packs) - 1) {
789 err = got_pack_close(&repo->packs[i - 1]);
792 memmove(&repo->packs[1], &repo->packs[0],
793 sizeof(repo->packs) - sizeof(repo->packs[0]));
797 pack = &repo->packs[i];
799 pack->path_packfile = strdup(path_packfile);
800 if (pack->path_packfile == NULL) {
801 err = got_error_from_errno("strdup");
805 err = open_packfile(&pack->fd, path_packfile, packidx);
809 if (fstat(pack->fd, &sb) != 0) {
810 err = got_error_from_errno("fstat");
813 pack->filesize = sb.st_size;
815 pack->privsep_child = NULL;
817 #ifndef GOT_PACK_NO_MMAP
818 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
820 if (pack->map == MAP_FAILED) {
821 if (errno != ENOMEM) {
822 err = got_error_from_errno("mmap");
825 pack->map = NULL; /* fall back to read(2) */
831 free(pack->path_packfile);
832 memset(pack, 0, sizeof(*pack));
840 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
842 struct got_pack *pack = NULL;
845 for (i = 0; i < nitems(repo->packs); i++) {
846 pack = &repo->packs[i];
847 if (pack->path_packfile == NULL)
849 if (strcmp(pack->path_packfile, path_packfile) == 0)
856 const struct got_error *
857 got_repo_init(const char *repo_path)
859 const struct got_error *err = NULL;
860 const char *dirnames[] = {
862 GOT_OBJECTS_PACK_DIR,
865 const char *description_str = "Unnamed repository; "
866 "edit this file 'description' to name the repository.";
867 const char *headref_str = "ref: refs/heads/master";
868 const char *gitconfig_str = "[core]\n"
869 "\trepositoryformatversion = 0\n"
870 "\tfilemode = true\n"
875 if (!got_path_dir_is_empty(repo_path))
876 return got_error(GOT_ERR_DIR_NOT_EMPTY);
878 for (i = 0; i < nitems(dirnames); i++) {
879 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
880 return got_error_from_errno("asprintf");
882 err = got_path_mkdir(path);
888 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
889 return got_error_from_errno("asprintf");
890 err = got_path_create_file(path, description_str);
895 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
896 return got_error_from_errno("asprintf");
897 err = got_path_create_file(path, headref_str);
902 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
903 return got_error_from_errno("asprintf");
904 err = got_path_create_file(path, gitconfig_str);
912 static const struct got_error *
913 match_packed_object(struct got_object_id **unique_id,
914 struct got_repository *repo, const char *id_str_prefix, int obj_type)
916 const struct got_error *err = NULL;
921 struct got_object_id_queue matched_ids;
923 SIMPLEQ_INIT(&matched_ids);
925 path_packdir = got_repo_get_path_objects_pack(repo);
926 if (path_packdir == NULL)
927 return got_error_from_errno("got_repo_get_path_objects_pack");
929 packdir = opendir(path_packdir);
930 if (packdir == NULL) {
932 err = got_error_from_errno2("opendir", path_packdir);
936 while ((dent = readdir(packdir)) != NULL) {
937 struct got_packidx *packidx;
938 struct got_object_qid *qid;
941 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
944 if (asprintf(&path_packidx, "%s/%s", path_packdir,
945 dent->d_name) == -1) {
946 err = got_error_from_errno("asprintf");
950 err = got_packidx_open(&packidx, path_packidx, 0);
955 err = got_packidx_match_id_str_prefix(&matched_ids,
956 packidx, id_str_prefix);
958 got_packidx_close(packidx);
961 err = got_packidx_close(packidx);
965 SIMPLEQ_FOREACH(qid, &matched_ids, entry) {
966 if (obj_type != GOT_OBJ_TYPE_ANY) {
968 err = got_object_get_type(&matched_type, repo,
972 if (matched_type != obj_type)
975 if (*unique_id == NULL) {
976 *unique_id = got_object_id_dup(qid->id);
977 if (*unique_id == NULL) {
978 err = got_error_from_errno("malloc");
982 err = got_error(GOT_ERR_AMBIGUOUS_ID);
988 got_object_id_queue_free(&matched_ids);
990 if (packdir && closedir(packdir) != 0 && err == NULL)
991 err = got_error_from_errno("closedir");
999 static const struct got_error *
1000 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1001 const char *object_dir, const char *id_str_prefix, int obj_type,
1002 struct got_repository *repo)
1004 const struct got_error *err = NULL;
1007 struct dirent *dent;
1008 struct got_object_id id;
1010 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1011 err = got_error_from_errno("asprintf");
1015 dir = opendir(path);
1017 if (errno == ENOENT) {
1021 err = got_error_from_errno2("opendir", path);
1024 while ((dent = readdir(dir)) != NULL) {
1028 if (strcmp(dent->d_name, ".") == 0 ||
1029 strcmp(dent->d_name, "..") == 0)
1032 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1033 err = got_error_from_errno("asprintf");
1037 if (!got_parse_sha1_digest(id.sha1, id_str))
1041 * Directory entries do not necessarily appear in
1042 * sorted order, so we must iterate over all of them.
1044 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1050 if (*unique_id == NULL) {
1051 if (obj_type != GOT_OBJ_TYPE_ANY) {
1053 err = got_object_get_type(&matched_type, repo,
1057 if (matched_type != obj_type)
1060 *unique_id = got_object_id_dup(&id);
1061 if (*unique_id == NULL) {
1062 err = got_error_from_errno("got_object_id_dup");
1067 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1073 if (dir && closedir(dir) != 0 && err == NULL)
1074 err = got_error_from_errno("closedir");
1083 const struct got_error *
1084 got_repo_match_object_id_prefix(struct got_object_id **id,
1085 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1087 const struct got_error *err = NULL;
1088 char *path_objects = got_repo_get_path_objects(repo);
1089 char *object_dir = NULL;
1095 for (i = 0; i < strlen(id_str_prefix); i++) {
1096 if (isxdigit((unsigned char)id_str_prefix[i]))
1098 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
1101 len = strlen(id_str_prefix);
1103 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1106 object_dir = strndup(id_str_prefix, 2);
1107 if (object_dir == NULL) {
1108 err = got_error_from_errno("strdup");
1111 err = match_loose_object(id, path_objects, object_dir,
1112 id_str_prefix, obj_type, repo);
1113 } else if (len == 1) {
1115 for (i = 0; i < 0xf; i++) {
1116 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1118 err = got_error_from_errno("asprintf");
1121 err = match_packed_object(id, repo, object_dir,
1125 err = match_loose_object(id, path_objects, object_dir,
1126 id_str_prefix, obj_type, repo);
1131 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
1139 } else if (*id == NULL)
1140 err = got_error(GOT_ERR_NO_OBJ);
1145 const struct got_error *
1146 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1147 int obj_type, struct got_repository *repo)
1149 const struct got_error *err;
1150 struct got_reflist_head refs;
1151 struct got_reflist_entry *re;
1152 struct got_object_id *tag_id;
1154 SIMPLEQ_INIT(&refs);
1157 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_by_name, NULL);
1161 SIMPLEQ_FOREACH(re, &refs, entry) {
1162 const char *refname;
1163 refname = got_ref_get_name(re->ref);
1164 if (got_ref_is_symbolic(re->ref))
1166 refname += strlen("refs/tags/");
1167 if (strcmp(refname, name) != 0)
1169 err = got_ref_resolve(&tag_id, repo, re->ref);
1172 err = got_object_open_as_tag(tag, repo, tag_id);
1176 if (obj_type == GOT_OBJ_TYPE_ANY ||
1177 got_object_tag_get_object_type(*tag) == obj_type)
1179 got_object_tag_close(*tag);
1183 got_ref_list_free(&refs);
1184 if (err == NULL && *tag == NULL)
1185 err = got_error(GOT_ERR_NO_OBJ);
1189 static const struct got_error *
1190 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1191 const char *name, mode_t mode, struct got_object_id *blob_id)
1193 const struct got_error *err = NULL;
1197 *new_te = calloc(1, sizeof(**new_te));
1198 if (*new_te == NULL)
1199 return got_error_from_errno("calloc");
1201 (*new_te)->name = strdup(name);
1202 if ((*new_te)->name == NULL) {
1203 err = got_error_from_errno("strdup");
1207 (*new_te)->mode = S_IFREG | (mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
1208 (*new_te)->id = blob_id;
1210 if (err && *new_te) {
1211 got_object_tree_entry_close(*new_te);
1217 static const struct got_error *
1218 import_file(struct got_tree_entry **new_te, struct dirent *de,
1219 const char *path, struct got_repository *repo)
1221 const struct got_error *err;
1222 struct got_object_id *blob_id = NULL;
1226 if (asprintf(&filepath, "%s%s%s", path,
1227 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1228 return got_error_from_errno("asprintf");
1230 if (lstat(filepath, &sb) != 0) {
1231 err = got_error_from_errno2("lstat", path);
1235 err = got_object_blob_create(&blob_id, filepath, repo);
1239 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1248 static const struct got_error *
1249 insert_tree_entry(struct got_tree_entry *new_te,
1250 struct got_pathlist_head *paths)
1252 const struct got_error *err = NULL;
1253 struct got_pathlist_entry *new_pe;
1255 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1259 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1263 static const struct got_error *write_tree(struct got_object_id **,
1264 const char *, struct got_pathlist_head *, struct got_repository *,
1265 got_repo_import_cb progress_cb, void *progress_arg);
1267 static const struct got_error *
1268 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1269 const char *path, struct got_pathlist_head *ignores,
1270 struct got_repository *repo,
1271 got_repo_import_cb progress_cb, void *progress_arg)
1273 const struct got_error *err;
1276 if (asprintf(&subdirpath, "%s%s%s", path,
1277 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1278 return got_error_from_errno("asprintf");
1280 (*new_te) = calloc(1, sizeof(**new_te));
1281 (*new_te)->mode = S_IFDIR;
1282 (*new_te)->name = strdup(de->d_name);
1283 if ((*new_te)->name == NULL) {
1284 err = got_error_from_errno("strdup");
1288 err = write_tree(&(*new_te)->id, subdirpath, ignores, repo,
1289 progress_cb, progress_arg);
1293 got_object_tree_entry_close(*new_te);
1299 static const struct got_error *
1300 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1301 struct got_pathlist_head *ignores, struct got_repository *repo,
1302 got_repo_import_cb progress_cb, void *progress_arg)
1304 const struct got_error *err = NULL;
1307 struct got_tree_entries new_tree_entries;
1308 struct got_tree_entry *new_te = NULL;
1309 struct got_pathlist_head paths;
1310 struct got_pathlist_entry *pe;
1312 *new_tree_id = NULL;
1315 new_tree_entries.nentries = 0;
1316 SIMPLEQ_INIT(&new_tree_entries.head);
1318 dir = opendir(path_dir);
1320 err = got_error_from_errno2("opendir", path_dir);
1324 while ((de = readdir(dir)) != NULL) {
1327 if (strcmp(de->d_name, ".") == 0 ||
1328 strcmp(de->d_name, "..") == 0)
1331 TAILQ_FOREACH(pe, ignores, entry) {
1332 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1339 if (de->d_type == DT_DIR) {
1340 err = import_subdir(&new_te, de, path_dir,
1341 ignores, repo, progress_cb, progress_arg);
1344 } else if (de->d_type == DT_REG) {
1345 err = import_file(&new_te, de, path_dir, repo);
1351 err = insert_tree_entry(new_te, &paths);
1356 TAILQ_FOREACH(pe, &paths, entry) {
1357 struct got_tree_entry *te = pe->data;
1359 new_tree_entries.nentries++;
1360 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
1361 if (!S_ISREG(te->mode))
1363 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1364 err = got_error_from_errno("asprintf");
1367 err = (*progress_cb)(progress_arg, path);
1373 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
1377 got_object_tree_entries_close(&new_tree_entries);
1378 got_pathlist_free(&paths);
1382 const struct got_error *
1383 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1384 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1385 struct got_repository *repo, got_repo_import_cb progress_cb,
1388 const struct got_error *err;
1389 struct got_object_id *new_tree_id;
1391 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1392 progress_cb, progress_arg);
1396 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1397 author, time(NULL), author, time(NULL), logmsg, repo);