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);
411 err = get_path_gitconfig(&gitconfig_path, repo);
416 err = got_gitconfig_open(&repo->gitconfig, gitconfig_path);
420 repo->gitconfig = NULL;
424 got_repo_close(repo);
428 free(gitconfig_path);
432 const struct got_error *
433 got_repo_close(struct got_repository *repo)
435 const struct got_error *err = NULL, *child_err;
438 for (i = 0; i < nitems(repo->packidx_cache); i++) {
439 if (repo->packidx_cache[i] == NULL)
441 got_packidx_close(repo->packidx_cache[i]);
444 for (i = 0; i < nitems(repo->packs); i++) {
445 if (repo->packs[i].path_packfile == NULL)
447 got_pack_close(&repo->packs[i]);
451 free(repo->path_git_dir);
453 got_object_cache_close(&repo->objcache);
454 got_object_cache_close(&repo->treecache);
455 got_object_cache_close(&repo->commitcache);
456 got_object_cache_close(&repo->tagcache);
458 for (i = 0; i < nitems(repo->privsep_children); i++) {
459 if (repo->privsep_children[i].imsg_fd == -1)
461 imsg_clear(repo->privsep_children[i].ibuf);
462 free(repo->privsep_children[i].ibuf);
463 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
464 child_err = got_privsep_wait_for_child(
465 repo->privsep_children[i].pid);
466 if (child_err && err == NULL)
468 if (close(repo->privsep_children[i].imsg_fd) != 0 &&
470 err = got_error_from_errno("close");
473 got_gitconfig_close(repo->gitconfig);
479 const struct got_error *
480 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
481 const char *input_path, int check_disk)
483 const struct got_error *err = NULL;
484 const char *repo_abspath = NULL;
485 size_t repolen, cwdlen, len;
486 char *cwd, *canonpath, *path = NULL;
488 *in_repo_path = NULL;
490 cwd = getcwd(NULL, 0);
492 return got_error_from_errno("getcwd");
494 canonpath = strdup(input_path);
495 if (canonpath == NULL) {
496 err = got_error_from_errno("strdup");
499 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
503 repo_abspath = got_repo_get_path(repo);
505 if (!check_disk || canonpath[0] == '\0') {
506 path = strdup(canonpath);
508 err = got_error_from_errno("strdup");
512 int is_repo_child = 0, is_cwd_child = 0;
514 path = realpath(canonpath, NULL);
516 if (errno != ENOENT) {
517 err = got_error_from_errno2("realpath",
522 * Path is not on disk.
523 * Assume it is already relative to repository root.
525 path = strdup(canonpath);
527 err = got_error_from_errno("strdup");
532 repolen = strlen(repo_abspath);
533 cwdlen = strlen(cwd);
536 if (len > repolen && strncmp(path, repo_abspath, repolen) == 0)
538 if (len > cwdlen && strncmp(path, cwd, cwdlen) == 0)
541 if (strcmp(path, repo_abspath) == 0) {
545 err = got_error_from_errno("strdup");
548 } else if (is_repo_child && is_cwd_child) {
550 /* Strip common prefix with repository path. */
551 err = got_path_skip_common_ancestor(&child,
557 } else if (is_repo_child) {
558 /* Matched an on-disk path inside repository. */
559 if (got_repo_is_bare(repo)) {
561 * Matched an on-disk path inside repository
562 * database. Treat as repository-relative.
566 /* Strip common prefix with repository path. */
567 err = got_path_skip_common_ancestor(&child,
574 } else if (is_cwd_child) {
576 /* Strip common prefix with cwd. */
577 err = got_path_skip_common_ancestor(&child, cwd,
585 * Matched unrelated on-disk path.
586 * Treat it as repository-relative.
591 /* Make in-repository path absolute */
592 if (path[0] != '/') {
594 if (asprintf(&abspath, "/%s", path) == -1) {
595 err = got_error_from_errno("asprintf");
608 *in_repo_path = path;
612 const struct got_error *
613 got_repo_cache_packidx(struct got_repository *repo, struct got_packidx *packidx)
615 const struct got_error *err = NULL;
618 for (i = 0; i < nitems(repo->packidx_cache); i++) {
619 if (repo->packidx_cache[i] == NULL)
622 if (i == nitems(repo->packidx_cache)) {
623 err = got_packidx_close(repo->packidx_cache[i - 1]);
629 * Insert the new pack index at the front so it will
630 * be searched first in the future.
632 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
633 sizeof(repo->packidx_cache) -
634 sizeof(repo->packidx_cache[0]));
635 repo->packidx_cache[0] = packidx;
641 is_packidx_filename(const char *name, size_t len)
643 if (len != GOT_PACKIDX_NAMELEN)
646 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
649 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
650 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
656 const struct got_error *
657 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
658 struct got_repository *repo, struct got_object_id *id)
660 const struct got_error *err;
667 /* Search pack index cache. */
668 for (i = 0; i < nitems(repo->packidx_cache); i++) {
669 if (repo->packidx_cache[i] == NULL)
671 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
673 *packidx = repo->packidx_cache[i];
677 /* No luck. Search the filesystem. */
679 path_packdir = got_repo_get_path_objects_pack(repo);
680 if (path_packdir == NULL)
681 return got_error_from_errno("got_repo_get_path_objects_pack");
683 packdir = opendir(path_packdir);
684 if (packdir == NULL) {
686 err = got_error_no_obj(id);
688 err = got_error_from_errno2("opendir", path_packdir);
692 while ((dent = readdir(packdir)) != NULL) {
693 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
696 if (asprintf(&path_packidx, "%s/%s", path_packdir,
697 dent->d_name) == -1) {
698 err = got_error_from_errno("asprintf");
702 err = got_packidx_open(packidx, path_packidx, 0);
707 *idx = got_packidx_get_object_idx(*packidx, id);
709 err = NULL; /* found the object */
710 err = got_repo_cache_packidx(repo, *packidx);
714 err = got_packidx_close(*packidx);
720 err = got_error_no_obj(id);
723 if (packdir && closedir(packdir) != 0 && err == NULL)
724 err = got_error_from_errno("closedir");
728 static const struct got_error *
729 read_packfile_hdr(int fd, struct got_packidx *packidx)
731 const struct got_error *err = NULL;
732 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
733 struct got_packfile_hdr hdr;
736 n = read(fd, &hdr, sizeof(hdr));
738 return got_error_from_errno("read");
739 if (n != sizeof(hdr))
740 return got_error(GOT_ERR_BAD_PACKFILE);
742 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
743 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
744 betoh32(hdr.nobjects) != totobj)
745 err = got_error(GOT_ERR_BAD_PACKFILE);
750 static const struct got_error *
751 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
753 const struct got_error *err = NULL;
755 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
757 return got_error_from_errno2("open", path_packfile);
760 err = read_packfile_hdr(*fd, packidx);
770 const struct got_error *
771 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
772 const char *path_packfile, struct got_packidx *packidx)
774 const struct got_error *err = NULL;
775 struct got_pack *pack = NULL;
782 for (i = 0; i < nitems(repo->packs); i++) {
783 pack = &repo->packs[i];
784 if (pack->path_packfile == NULL)
786 if (strcmp(pack->path_packfile, path_packfile) == 0)
790 if (i == nitems(repo->packs) - 1) {
791 err = got_pack_close(&repo->packs[i - 1]);
794 memmove(&repo->packs[1], &repo->packs[0],
795 sizeof(repo->packs) - sizeof(repo->packs[0]));
799 pack = &repo->packs[i];
801 pack->path_packfile = strdup(path_packfile);
802 if (pack->path_packfile == NULL) {
803 err = got_error_from_errno("strdup");
807 err = open_packfile(&pack->fd, path_packfile, packidx);
811 if (fstat(pack->fd, &sb) != 0) {
812 err = got_error_from_errno("fstat");
815 pack->filesize = sb.st_size;
817 pack->privsep_child = NULL;
819 #ifndef GOT_PACK_NO_MMAP
820 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
822 if (pack->map == MAP_FAILED) {
823 if (errno != ENOMEM) {
824 err = got_error_from_errno("mmap");
827 pack->map = NULL; /* fall back to read(2) */
833 free(pack->path_packfile);
834 memset(pack, 0, sizeof(*pack));
842 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
844 struct got_pack *pack = NULL;
847 for (i = 0; i < nitems(repo->packs); i++) {
848 pack = &repo->packs[i];
849 if (pack->path_packfile == NULL)
851 if (strcmp(pack->path_packfile, path_packfile) == 0)
858 const struct got_error *
859 got_repo_init(const char *repo_path)
861 const struct got_error *err = NULL;
862 const char *dirnames[] = {
864 GOT_OBJECTS_PACK_DIR,
867 const char *description_str = "Unnamed repository; "
868 "edit this file 'description' to name the repository.";
869 const char *headref_str = "ref: refs/heads/master";
870 const char *gitconfig_str = "[core]\n"
871 "\trepositoryformatversion = 0\n"
872 "\tfilemode = true\n"
877 if (!got_path_dir_is_empty(repo_path))
878 return got_error(GOT_ERR_DIR_NOT_EMPTY);
880 for (i = 0; i < nitems(dirnames); i++) {
881 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
882 return got_error_from_errno("asprintf");
884 err = got_path_mkdir(path);
890 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
891 return got_error_from_errno("asprintf");
892 err = got_path_create_file(path, description_str);
897 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
898 return got_error_from_errno("asprintf");
899 err = got_path_create_file(path, headref_str);
904 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
905 return got_error_from_errno("asprintf");
906 err = got_path_create_file(path, gitconfig_str);
914 static const struct got_error *
915 match_packed_object(struct got_object_id **unique_id,
916 struct got_repository *repo, const char *id_str_prefix, int obj_type)
918 const struct got_error *err = NULL;
923 struct got_object_id_queue matched_ids;
925 SIMPLEQ_INIT(&matched_ids);
927 path_packdir = got_repo_get_path_objects_pack(repo);
928 if (path_packdir == NULL)
929 return got_error_from_errno("got_repo_get_path_objects_pack");
931 packdir = opendir(path_packdir);
932 if (packdir == NULL) {
934 err = got_error_from_errno2("opendir", path_packdir);
938 while ((dent = readdir(packdir)) != NULL) {
939 struct got_packidx *packidx;
940 struct got_object_qid *qid;
943 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
946 if (asprintf(&path_packidx, "%s/%s", path_packdir,
947 dent->d_name) == -1) {
948 err = got_error_from_errno("asprintf");
952 err = got_packidx_open(&packidx, path_packidx, 0);
957 err = got_packidx_match_id_str_prefix(&matched_ids,
958 packidx, id_str_prefix);
960 got_packidx_close(packidx);
963 err = got_packidx_close(packidx);
967 SIMPLEQ_FOREACH(qid, &matched_ids, entry) {
968 if (obj_type != GOT_OBJ_TYPE_ANY) {
970 err = got_object_get_type(&matched_type, repo,
974 if (matched_type != obj_type)
977 if (*unique_id == NULL) {
978 *unique_id = got_object_id_dup(qid->id);
979 if (*unique_id == NULL) {
980 err = got_error_from_errno("malloc");
984 err = got_error(GOT_ERR_AMBIGUOUS_ID);
990 got_object_id_queue_free(&matched_ids);
992 if (packdir && closedir(packdir) != 0 && err == NULL)
993 err = got_error_from_errno("closedir");
1001 static const struct got_error *
1002 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1003 const char *object_dir, const char *id_str_prefix, int obj_type,
1004 struct got_repository *repo)
1006 const struct got_error *err = NULL;
1009 struct dirent *dent;
1010 struct got_object_id id;
1012 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1013 err = got_error_from_errno("asprintf");
1017 dir = opendir(path);
1019 if (errno == ENOENT) {
1023 err = got_error_from_errno2("opendir", path);
1026 while ((dent = readdir(dir)) != NULL) {
1030 if (strcmp(dent->d_name, ".") == 0 ||
1031 strcmp(dent->d_name, "..") == 0)
1034 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1035 err = got_error_from_errno("asprintf");
1039 if (!got_parse_sha1_digest(id.sha1, id_str))
1043 * Directory entries do not necessarily appear in
1044 * sorted order, so we must iterate over all of them.
1046 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1052 if (*unique_id == NULL) {
1053 if (obj_type != GOT_OBJ_TYPE_ANY) {
1055 err = got_object_get_type(&matched_type, repo,
1059 if (matched_type != obj_type)
1062 *unique_id = got_object_id_dup(&id);
1063 if (*unique_id == NULL) {
1064 err = got_error_from_errno("got_object_id_dup");
1069 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1075 if (dir && closedir(dir) != 0 && err == NULL)
1076 err = got_error_from_errno("closedir");
1085 const struct got_error *
1086 got_repo_match_object_id_prefix(struct got_object_id **id,
1087 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1089 const struct got_error *err = NULL;
1090 char *path_objects = got_repo_get_path_objects(repo);
1091 char *object_dir = NULL;
1097 for (i = 0; i < strlen(id_str_prefix); i++) {
1098 if (isxdigit((unsigned char)id_str_prefix[i]))
1100 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
1103 len = strlen(id_str_prefix);
1105 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1108 object_dir = strndup(id_str_prefix, 2);
1109 if (object_dir == NULL) {
1110 err = got_error_from_errno("strdup");
1113 err = match_loose_object(id, path_objects, object_dir,
1114 id_str_prefix, obj_type, repo);
1115 } else if (len == 1) {
1117 for (i = 0; i < 0xf; i++) {
1118 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1120 err = got_error_from_errno("asprintf");
1123 err = match_packed_object(id, repo, object_dir,
1127 err = match_loose_object(id, path_objects, object_dir,
1128 id_str_prefix, obj_type, repo);
1133 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
1141 } else if (*id == NULL)
1142 err = got_error(GOT_ERR_NO_OBJ);
1147 const struct got_error *
1148 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1149 int obj_type, struct got_repository *repo)
1151 const struct got_error *err;
1152 struct got_reflist_head refs;
1153 struct got_reflist_entry *re;
1154 struct got_object_id *tag_id;
1156 SIMPLEQ_INIT(&refs);
1159 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_by_name, NULL);
1163 SIMPLEQ_FOREACH(re, &refs, entry) {
1164 const char *refname;
1165 refname = got_ref_get_name(re->ref);
1166 if (got_ref_is_symbolic(re->ref))
1168 refname += strlen("refs/tags/");
1169 if (strcmp(refname, name) != 0)
1171 err = got_ref_resolve(&tag_id, repo, re->ref);
1174 err = got_object_open_as_tag(tag, repo, tag_id);
1178 if (obj_type == GOT_OBJ_TYPE_ANY ||
1179 got_object_tag_get_object_type(*tag) == obj_type)
1181 got_object_tag_close(*tag);
1185 got_ref_list_free(&refs);
1186 if (err == NULL && *tag == NULL)
1187 err = got_error(GOT_ERR_NO_OBJ);
1191 static const struct got_error *
1192 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1193 const char *name, mode_t mode, struct got_object_id *blob_id)
1195 const struct got_error *err = NULL;
1199 *new_te = calloc(1, sizeof(**new_te));
1200 if (*new_te == NULL)
1201 return got_error_from_errno("calloc");
1203 (*new_te)->name = strdup(name);
1204 if ((*new_te)->name == NULL) {
1205 err = got_error_from_errno("strdup");
1209 (*new_te)->mode = S_IFREG | (mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
1210 (*new_te)->id = blob_id;
1212 if (err && *new_te) {
1213 got_object_tree_entry_close(*new_te);
1219 static const struct got_error *
1220 import_file(struct got_tree_entry **new_te, struct dirent *de,
1221 const char *path, struct got_repository *repo)
1223 const struct got_error *err;
1224 struct got_object_id *blob_id = NULL;
1228 if (asprintf(&filepath, "%s%s%s", path,
1229 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1230 return got_error_from_errno("asprintf");
1232 if (lstat(filepath, &sb) != 0) {
1233 err = got_error_from_errno2("lstat", path);
1237 err = got_object_blob_create(&blob_id, filepath, repo);
1241 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1250 static const struct got_error *
1251 insert_tree_entry(struct got_tree_entry *new_te,
1252 struct got_pathlist_head *paths)
1254 const struct got_error *err = NULL;
1255 struct got_pathlist_entry *new_pe;
1257 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1261 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1265 static const struct got_error *write_tree(struct got_object_id **,
1266 const char *, struct got_pathlist_head *, struct got_repository *,
1267 got_repo_import_cb progress_cb, void *progress_arg);
1269 static const struct got_error *
1270 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1271 const char *path, struct got_pathlist_head *ignores,
1272 struct got_repository *repo,
1273 got_repo_import_cb progress_cb, void *progress_arg)
1275 const struct got_error *err;
1278 if (asprintf(&subdirpath, "%s%s%s", path,
1279 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1280 return got_error_from_errno("asprintf");
1282 (*new_te) = calloc(1, sizeof(**new_te));
1283 (*new_te)->mode = S_IFDIR;
1284 (*new_te)->name = strdup(de->d_name);
1285 if ((*new_te)->name == NULL) {
1286 err = got_error_from_errno("strdup");
1290 err = write_tree(&(*new_te)->id, subdirpath, ignores, repo,
1291 progress_cb, progress_arg);
1295 got_object_tree_entry_close(*new_te);
1301 static const struct got_error *
1302 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1303 struct got_pathlist_head *ignores, struct got_repository *repo,
1304 got_repo_import_cb progress_cb, void *progress_arg)
1306 const struct got_error *err = NULL;
1309 struct got_tree_entries new_tree_entries;
1310 struct got_tree_entry *new_te = NULL;
1311 struct got_pathlist_head paths;
1312 struct got_pathlist_entry *pe;
1314 *new_tree_id = NULL;
1317 new_tree_entries.nentries = 0;
1318 SIMPLEQ_INIT(&new_tree_entries.head);
1320 dir = opendir(path_dir);
1322 err = got_error_from_errno2("opendir", path_dir);
1326 while ((de = readdir(dir)) != NULL) {
1329 if (strcmp(de->d_name, ".") == 0 ||
1330 strcmp(de->d_name, "..") == 0)
1333 TAILQ_FOREACH(pe, ignores, entry) {
1334 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1341 if (de->d_type == DT_DIR) {
1342 err = import_subdir(&new_te, de, path_dir,
1343 ignores, repo, progress_cb, progress_arg);
1346 } else if (de->d_type == DT_REG) {
1347 err = import_file(&new_te, de, path_dir, repo);
1353 err = insert_tree_entry(new_te, &paths);
1358 TAILQ_FOREACH(pe, &paths, entry) {
1359 struct got_tree_entry *te = pe->data;
1361 new_tree_entries.nentries++;
1362 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
1363 if (!S_ISREG(te->mode))
1365 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1366 err = got_error_from_errno("asprintf");
1369 err = (*progress_cb)(progress_arg, path);
1375 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
1379 got_object_tree_entries_close(&new_tree_entries);
1380 got_pathlist_free(&paths);
1384 const struct got_error *
1385 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1386 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1387 struct got_repository *repo, got_repo_import_cb progress_cb,
1390 const struct got_error *err;
1391 struct got_object_id *new_tree_id;
1393 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1394 progress_cb, progress_arg);
1398 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1399 author, time(NULL), author, time(NULL), logmsg, repo);