Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/uio.h>
20 #include <sys/socket.h>
21 #include <sys/stat.h>
22 #include <sys/mman.h>
24 #include <ctype.h>
25 #include <endian.h>
26 #include <fcntl.h>
27 #include <fnmatch.h>
28 #include <limits.h>
29 #include <dirent.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <sha1.h>
33 #include <string.h>
34 #include <time.h>
35 #include <unistd.h>
36 #include <zlib.h>
37 #include <errno.h>
38 #include <libgen.h>
39 #include <stdint.h>
40 #include <imsg.h>
41 #include <uuid.h>
43 #include "got_error.h"
44 #include "got_reference.h"
45 #include "got_repository.h"
46 #include "got_path.h"
47 #include "got_cancel.h"
48 #include "got_worktree.h"
49 #include "got_object.h"
51 #include "got_lib_delta.h"
52 #include "got_lib_inflate.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_pack.h"
57 #include "got_lib_privsep.h"
58 #include "got_lib_worktree.h"
59 #include "got_lib_sha1.h"
60 #include "got_lib_object_cache.h"
61 #include "got_lib_repository.h"
62 #include "got_lib_gotconfig.h"
64 #ifndef nitems
65 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
66 #endif
68 const char *
69 got_repo_get_path(struct got_repository *repo)
70 {
71 return repo->path;
72 }
74 const char *
75 got_repo_get_path_git_dir(struct got_repository *repo)
76 {
77 return repo->path_git_dir;
78 }
80 int
81 got_repo_get_fd(struct got_repository *repo)
82 {
83 return repo->gitdir_fd;
84 }
86 const char *
87 got_repo_get_gitconfig_author_name(struct got_repository *repo)
88 {
89 return repo->gitconfig_author_name;
90 }
92 const char *
93 got_repo_get_gitconfig_author_email(struct got_repository *repo)
94 {
95 return repo->gitconfig_author_email;
96 }
98 const char *
99 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
101 return repo->global_gitconfig_author_name;
104 const char *
105 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
107 return repo->global_gitconfig_author_email;
110 const char *
111 got_repo_get_gitconfig_owner(struct got_repository *repo)
113 return repo->gitconfig_owner;
116 int
117 got_repo_is_bare(struct got_repository *repo)
119 return (strcmp(repo->path, repo->path_git_dir) == 0);
122 static char *
123 get_path_git_child(struct got_repository *repo, const char *basename)
125 char *path_child;
127 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
128 basename) == -1)
129 return NULL;
131 return path_child;
134 char *
135 got_repo_get_path_objects(struct got_repository *repo)
137 return get_path_git_child(repo, GOT_OBJECTS_DIR);
140 char *
141 got_repo_get_path_objects_pack(struct got_repository *repo)
143 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
146 char *
147 got_repo_get_path_refs(struct got_repository *repo)
149 return get_path_git_child(repo, GOT_REFS_DIR);
152 char *
153 got_repo_get_path_packed_refs(struct got_repository *repo)
155 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
158 static char *
159 get_path_head(struct got_repository *repo)
161 return get_path_git_child(repo, GOT_HEAD_FILE);
164 char *
165 got_repo_get_path_gitconfig(struct got_repository *repo)
167 return get_path_git_child(repo, GOT_GITCONFIG);
170 char *
171 got_repo_get_path_gotconfig(struct got_repository *repo)
173 return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME);
176 const struct got_gotconfig *
177 got_repo_get_gotconfig(struct got_repository *repo)
179 return repo->gotconfig;
182 void
183 got_repo_get_gitconfig_remotes(int *nremotes,
184 const struct got_remote_repo **remotes, struct got_repository *repo)
186 *nremotes = repo->ngitconfig_remotes;
187 *remotes = repo->gitconfig_remotes;
190 static int
191 is_git_repo(struct got_repository *repo)
193 const char *path_git = got_repo_get_path_git_dir(repo);
194 char *path_objects = got_repo_get_path_objects(repo);
195 char *path_refs = got_repo_get_path_refs(repo);
196 char *path_head = get_path_head(repo);
197 int ret = 0;
198 struct stat sb;
199 struct got_reference *head_ref;
201 if (lstat(path_git, &sb) == -1)
202 goto done;
203 if (!S_ISDIR(sb.st_mode))
204 goto done;
206 if (lstat(path_objects, &sb) == -1)
207 goto done;
208 if (!S_ISDIR(sb.st_mode))
209 goto done;
211 if (lstat(path_refs, &sb) == -1)
212 goto done;
213 if (!S_ISDIR(sb.st_mode))
214 goto done;
216 if (lstat(path_head, &sb) == -1)
217 goto done;
218 if (!S_ISREG(sb.st_mode))
219 goto done;
221 /* Check if the HEAD reference can be opened. */
222 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
223 goto done;
224 got_ref_close(head_ref);
226 ret = 1;
227 done:
228 free(path_objects);
229 free(path_refs);
230 free(path_head);
231 return ret;
235 const struct got_error *
236 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
237 struct got_object *obj)
239 #ifndef GOT_NO_OBJ_CACHE
240 const struct got_error *err = NULL;
241 err = got_object_cache_add(&repo->objcache, id, obj);
242 if (err) {
243 if (err->code == GOT_ERR_OBJ_EXISTS ||
244 err->code == GOT_ERR_OBJ_TOO_LARGE)
245 err = NULL;
246 return err;
248 obj->refcnt++;
249 #endif
250 return NULL;
253 struct got_object *
254 got_repo_get_cached_object(struct got_repository *repo,
255 struct got_object_id *id)
257 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
260 const struct got_error *
261 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
262 struct got_tree_object *tree)
264 #ifndef GOT_NO_OBJ_CACHE
265 const struct got_error *err = NULL;
266 err = got_object_cache_add(&repo->treecache, id, tree);
267 if (err) {
268 if (err->code == GOT_ERR_OBJ_EXISTS ||
269 err->code == GOT_ERR_OBJ_TOO_LARGE)
270 err = NULL;
271 return err;
273 tree->refcnt++;
274 #endif
275 return NULL;
278 struct got_tree_object *
279 got_repo_get_cached_tree(struct got_repository *repo,
280 struct got_object_id *id)
282 return (struct got_tree_object *)got_object_cache_get(
283 &repo->treecache, id);
286 const struct got_error *
287 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
288 struct got_commit_object *commit)
290 #ifndef GOT_NO_OBJ_CACHE
291 const struct got_error *err = NULL;
292 err = got_object_cache_add(&repo->commitcache, id, commit);
293 if (err) {
294 if (err->code == GOT_ERR_OBJ_EXISTS ||
295 err->code == GOT_ERR_OBJ_TOO_LARGE)
296 err = NULL;
297 return err;
299 commit->refcnt++;
300 #endif
301 return NULL;
304 struct got_commit_object *
305 got_repo_get_cached_commit(struct got_repository *repo,
306 struct got_object_id *id)
308 return (struct got_commit_object *)got_object_cache_get(
309 &repo->commitcache, id);
312 const struct got_error *
313 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
314 struct got_tag_object *tag)
316 #ifndef GOT_NO_OBJ_CACHE
317 const struct got_error *err = NULL;
318 err = got_object_cache_add(&repo->tagcache, id, tag);
319 if (err) {
320 if (err->code == GOT_ERR_OBJ_EXISTS ||
321 err->code == GOT_ERR_OBJ_TOO_LARGE)
322 err = NULL;
323 return err;
325 tag->refcnt++;
326 #endif
327 return NULL;
330 struct got_tag_object *
331 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
333 return (struct got_tag_object *)got_object_cache_get(
334 &repo->tagcache, id);
337 const struct got_error *
338 open_repo(struct got_repository *repo, const char *path)
340 const struct got_error *err = NULL;
342 repo->gitdir_fd = -1;
344 /* bare git repository? */
345 repo->path_git_dir = strdup(path);
346 if (repo->path_git_dir == NULL)
347 return got_error_from_errno("strdup");
348 if (is_git_repo(repo)) {
349 repo->path = strdup(repo->path_git_dir);
350 if (repo->path == NULL) {
351 err = got_error_from_errno("strdup");
352 goto done;
354 repo->gitdir_fd = open(repo->path_git_dir, O_DIRECTORY);
355 if (repo->gitdir_fd == -1) {
356 err = got_error_from_errno2("open",
357 repo->path_git_dir);
358 goto done;
360 return NULL;
363 /* git repository with working tree? */
364 free(repo->path_git_dir);
365 repo->path_git_dir = NULL;
366 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
367 err = got_error_from_errno("asprintf");
368 goto done;
370 if (is_git_repo(repo)) {
371 repo->path = strdup(path);
372 if (repo->path == NULL) {
373 err = got_error_from_errno("strdup");
374 goto done;
376 repo->gitdir_fd = open(repo->path_git_dir, O_DIRECTORY);
377 if (repo->gitdir_fd == -1) {
378 err = got_error_from_errno2("open",
379 repo->path_git_dir);
380 goto done;
382 return NULL;
385 err = got_error(GOT_ERR_NOT_GIT_REPO);
386 done:
387 if (err) {
388 free(repo->path);
389 repo->path = NULL;
390 free(repo->path_git_dir);
391 repo->path_git_dir = NULL;
392 if (repo->gitdir_fd != -1)
393 close(repo->gitdir_fd);
394 repo->gitdir_fd = -1;
397 return err;
400 static const struct got_error *
401 parse_gitconfig_file(int *gitconfig_repository_format_version,
402 char **gitconfig_author_name, char **gitconfig_author_email,
403 struct got_remote_repo **remotes, int *nremotes,
404 char **gitconfig_owner, char ***extensions, int *nextensions,
405 const char *gitconfig_path)
407 const struct got_error *err = NULL, *child_err = NULL;
408 int fd = -1;
409 int imsg_fds[2] = { -1, -1 };
410 pid_t pid;
411 struct imsgbuf *ibuf;
413 *gitconfig_repository_format_version = 0;
414 if (extensions)
415 *extensions = NULL;
416 if (nextensions)
417 *nextensions = 0;
418 *gitconfig_author_name = NULL;
419 *gitconfig_author_email = NULL;
420 if (remotes)
421 *remotes = NULL;
422 if (nremotes)
423 *nremotes = 0;
424 if (gitconfig_owner)
425 *gitconfig_owner = NULL;
427 fd = open(gitconfig_path, O_RDONLY);
428 if (fd == -1) {
429 if (errno == ENOENT)
430 return NULL;
431 return got_error_from_errno2("open", gitconfig_path);
434 ibuf = calloc(1, sizeof(*ibuf));
435 if (ibuf == NULL) {
436 err = got_error_from_errno("calloc");
437 goto done;
440 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
441 err = got_error_from_errno("socketpair");
442 goto done;
445 pid = fork();
446 if (pid == -1) {
447 err = got_error_from_errno("fork");
448 goto done;
449 } else if (pid == 0) {
450 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
451 gitconfig_path);
452 /* not reached */
455 if (close(imsg_fds[1]) == -1) {
456 err = got_error_from_errno("close");
457 goto done;
459 imsg_fds[1] = -1;
460 imsg_init(ibuf, imsg_fds[0]);
462 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
463 if (err)
464 goto done;
465 fd = -1;
467 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
468 if (err)
469 goto done;
471 err = got_privsep_recv_gitconfig_int(
472 gitconfig_repository_format_version, ibuf);
473 if (err)
474 goto done;
476 if (extensions && nextensions) {
477 err = got_privsep_send_gitconfig_repository_extensions_req(
478 ibuf);
479 if (err)
480 goto done;
481 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
482 if (err)
483 goto done;
484 if (*nextensions > 0) {
485 int i;
486 *extensions = calloc(*nextensions, sizeof(char *));
487 if (*extensions == NULL) {
488 err = got_error_from_errno("calloc");
489 goto done;
491 for (i = 0; i < *nextensions; i++) {
492 char *ext;
493 err = got_privsep_recv_gitconfig_str(&ext,
494 ibuf);
495 if (err)
496 goto done;
497 (*extensions)[i] = ext;
502 err = got_privsep_send_gitconfig_author_name_req(ibuf);
503 if (err)
504 goto done;
506 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
507 if (err)
508 goto done;
510 err = got_privsep_send_gitconfig_author_email_req(ibuf);
511 if (err)
512 goto done;
514 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
515 if (err)
516 goto done;
518 if (remotes && nremotes) {
519 err = got_privsep_send_gitconfig_remotes_req(ibuf);
520 if (err)
521 goto done;
523 err = got_privsep_recv_gitconfig_remotes(remotes,
524 nremotes, ibuf);
525 if (err)
526 goto done;
529 if (gitconfig_owner) {
530 err = got_privsep_send_gitconfig_owner_req(ibuf);
531 if (err)
532 goto done;
533 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
534 if (err)
535 goto done;
538 imsg_clear(ibuf);
539 err = got_privsep_send_stop(imsg_fds[0]);
540 child_err = got_privsep_wait_for_child(pid);
541 if (child_err && err == NULL)
542 err = child_err;
543 done:
544 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
545 err = got_error_from_errno("close");
546 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
547 err = got_error_from_errno("close");
548 if (fd != -1 && close(fd) == -1 && err == NULL)
549 err = got_error_from_errno2("close", gitconfig_path);
550 free(ibuf);
551 return err;
554 static const struct got_error *
555 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
557 const struct got_error *err = NULL;
558 char *repo_gitconfig_path = NULL;
560 if (global_gitconfig_path) {
561 /* Read settings from ~/.gitconfig. */
562 int dummy_repo_version;
563 err = parse_gitconfig_file(&dummy_repo_version,
564 &repo->global_gitconfig_author_name,
565 &repo->global_gitconfig_author_email,
566 NULL, NULL, NULL, NULL, NULL, global_gitconfig_path);
567 if (err)
568 return err;
571 /* Read repository's .git/config file. */
572 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
573 if (repo_gitconfig_path == NULL)
574 return got_error_from_errno("got_repo_get_path_gitconfig");
576 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
577 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
578 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
579 &repo->gitconfig_owner, &repo->extensions, &repo->nextensions,
580 repo_gitconfig_path);
581 if (err)
582 goto done;
583 done:
584 free(repo_gitconfig_path);
585 return err;
588 static const struct got_error *
589 read_gotconfig(struct got_repository *repo)
591 const struct got_error *err = NULL;
592 char *gotconfig_path;
594 gotconfig_path = got_repo_get_path_gotconfig(repo);
595 if (gotconfig_path == NULL)
596 return got_error_from_errno("got_repo_get_path_gotconfig");
598 err = got_gotconfig_read(&repo->gotconfig, gotconfig_path);
599 free(gotconfig_path);
600 return err;
603 /* Supported repository format extensions. */
604 static const char *repo_extensions[] = {
605 "noop", /* Got supports repository format version 1. */
606 "preciousObjects", /* Got has no garbage collection yet. */
607 "worktreeConfig", /* Got does not care about Git work trees. */
608 };
610 const struct got_error *
611 got_repo_open(struct got_repository **repop, const char *path,
612 const char *global_gitconfig_path)
614 struct got_repository *repo = NULL;
615 const struct got_error *err = NULL;
616 char *repo_path = NULL;
617 size_t i;
619 *repop = NULL;
621 repo = calloc(1, sizeof(*repo));
622 if (repo == NULL) {
623 err = got_error_from_errno("calloc");
624 goto done;
627 for (i = 0; i < nitems(repo->privsep_children); i++) {
628 memset(&repo->privsep_children[i], 0,
629 sizeof(repo->privsep_children[0]));
630 repo->privsep_children[i].imsg_fd = -1;
633 err = got_object_cache_init(&repo->objcache,
634 GOT_OBJECT_CACHE_TYPE_OBJ);
635 if (err)
636 goto done;
637 err = got_object_cache_init(&repo->treecache,
638 GOT_OBJECT_CACHE_TYPE_TREE);
639 if (err)
640 goto done;
641 err = got_object_cache_init(&repo->commitcache,
642 GOT_OBJECT_CACHE_TYPE_COMMIT);
643 if (err)
644 goto done;
645 err = got_object_cache_init(&repo->tagcache,
646 GOT_OBJECT_CACHE_TYPE_TAG);
647 if (err)
648 goto done;
650 repo_path = realpath(path, NULL);
651 if (repo_path == NULL) {
652 err = got_error_from_errno2("realpath", path);
653 goto done;
656 for (;;) {
657 char *parent_path;
659 err = open_repo(repo, repo_path);
660 if (err == NULL)
661 break;
662 if (err->code != GOT_ERR_NOT_GIT_REPO)
663 goto done;
664 if (repo_path[0] == '/' && repo_path[1] == '\0') {
665 err = got_error(GOT_ERR_NOT_GIT_REPO);
666 goto done;
668 err = got_path_dirname(&parent_path, repo_path);
669 if (err)
670 goto done;
671 free(repo_path);
672 repo_path = parent_path;
675 err = read_gotconfig(repo);
676 if (err)
677 goto done;
679 err = read_gitconfig(repo, global_gitconfig_path);
680 if (err)
681 goto done;
682 if (repo->gitconfig_repository_format_version != 0)
683 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
684 for (i = 0; i < repo->nextensions; i++) {
685 char *ext = repo->extensions[i];
686 int j, supported = 0;
687 for (j = 0; j < nitems(repo_extensions); j++) {
688 if (strcmp(ext, repo_extensions[j]) == 0) {
689 supported = 1;
690 break;
693 if (!supported) {
694 err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT);
695 goto done;
698 done:
699 if (err)
700 got_repo_close(repo);
701 else
702 *repop = repo;
703 free(repo_path);
704 return err;
707 const struct got_error *
708 got_repo_close(struct got_repository *repo)
710 const struct got_error *err = NULL, *child_err;
711 size_t i;
713 for (i = 0; i < nitems(repo->packidx_cache); i++) {
714 if (repo->packidx_cache[i] == NULL)
715 break;
716 got_packidx_close(repo->packidx_cache[i]);
719 for (i = 0; i < nitems(repo->packs); i++) {
720 if (repo->packs[i].path_packfile == NULL)
721 break;
722 got_pack_close(&repo->packs[i]);
725 free(repo->path);
726 free(repo->path_git_dir);
728 got_object_cache_close(&repo->objcache);
729 got_object_cache_close(&repo->treecache);
730 got_object_cache_close(&repo->commitcache);
731 got_object_cache_close(&repo->tagcache);
733 for (i = 0; i < nitems(repo->privsep_children); i++) {
734 if (repo->privsep_children[i].imsg_fd == -1)
735 continue;
736 imsg_clear(repo->privsep_children[i].ibuf);
737 free(repo->privsep_children[i].ibuf);
738 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
739 child_err = got_privsep_wait_for_child(
740 repo->privsep_children[i].pid);
741 if (child_err && err == NULL)
742 err = child_err;
743 if (close(repo->privsep_children[i].imsg_fd) == -1 &&
744 err == NULL)
745 err = got_error_from_errno("close");
748 if (repo->gotconfig)
749 got_gotconfig_free(repo->gotconfig);
750 free(repo->gitconfig_author_name);
751 free(repo->gitconfig_author_email);
752 for (i = 0; i < repo->ngitconfig_remotes; i++)
753 got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]);
754 free(repo->gitconfig_remotes);
755 for (i = 0; i < repo->nextensions; i++)
756 free(repo->extensions[i]);
757 free(repo->extensions);
758 free(repo);
760 return err;
763 void
764 got_repo_free_remote_repo_data(struct got_remote_repo *repo)
766 int i;
768 free(repo->name);
769 repo->name = NULL;
770 free(repo->url);
771 repo->url = NULL;
772 for (i = 0; i < repo->nbranches; i++)
773 free(repo->branches[i]);
774 free(repo->branches);
775 repo->branches = NULL;
776 repo->nbranches = 0;
779 const struct got_error *
780 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
781 const char *input_path)
783 const struct got_error *err = NULL;
784 const char *repo_abspath = NULL;
785 size_t repolen, len;
786 char *canonpath, *path = NULL;
788 *in_repo_path = NULL;
790 canonpath = strdup(input_path);
791 if (canonpath == NULL) {
792 err = got_error_from_errno("strdup");
793 goto done;
795 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
796 if (err)
797 goto done;
799 repo_abspath = got_repo_get_path(repo);
801 if (canonpath[0] == '\0') {
802 path = strdup(canonpath);
803 if (path == NULL) {
804 err = got_error_from_errno("strdup");
805 goto done;
807 } else {
808 path = realpath(canonpath, NULL);
809 if (path == NULL) {
810 if (errno != ENOENT) {
811 err = got_error_from_errno2("realpath",
812 canonpath);
813 goto done;
815 /*
816 * Path is not on disk.
817 * Assume it is already relative to repository root.
818 */
819 path = strdup(canonpath);
820 if (path == NULL) {
821 err = got_error_from_errno("strdup");
822 goto done;
826 repolen = strlen(repo_abspath);
827 len = strlen(path);
830 if (strcmp(path, repo_abspath) == 0) {
831 free(path);
832 path = strdup("");
833 if (path == NULL) {
834 err = got_error_from_errno("strdup");
835 goto done;
837 } else if (len > repolen &&
838 got_path_is_child(path, repo_abspath, repolen)) {
839 /* Matched an on-disk path inside repository. */
840 if (got_repo_is_bare(repo)) {
841 /*
842 * Matched an on-disk path inside repository
843 * database. Treat input as repository-relative.
844 */
845 free(path);
846 path = canonpath;
847 canonpath = NULL;
848 } else {
849 char *child;
850 /* Strip common prefix with repository path. */
851 err = got_path_skip_common_ancestor(&child,
852 repo_abspath, path);
853 if (err)
854 goto done;
855 free(path);
856 path = child;
858 } else {
859 /*
860 * Matched unrelated on-disk path.
861 * Treat input as repository-relative.
862 */
863 free(path);
864 path = canonpath;
865 canonpath = NULL;
869 /* Make in-repository path absolute */
870 if (path[0] != '/') {
871 char *abspath;
872 if (asprintf(&abspath, "/%s", path) == -1) {
873 err = got_error_from_errno("asprintf");
874 goto done;
876 free(path);
877 path = abspath;
880 done:
881 free(canonpath);
882 if (err)
883 free(path);
884 else
885 *in_repo_path = path;
886 return err;
889 static const struct got_error *
890 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
891 const char *path_packidx)
893 const struct got_error *err = NULL;
894 size_t i;
896 for (i = 0; i < nitems(repo->packidx_cache); i++) {
897 if (repo->packidx_cache[i] == NULL)
898 break;
899 if (strcmp(repo->packidx_cache[i]->path_packidx,
900 path_packidx) == 0) {
901 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
904 if (i == nitems(repo->packidx_cache)) {
905 err = got_packidx_close(repo->packidx_cache[i - 1]);
906 if (err)
907 return err;
910 /*
911 * Insert the new pack index at the front so it will
912 * be searched first in the future.
913 */
914 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
915 sizeof(repo->packidx_cache) -
916 sizeof(repo->packidx_cache[0]));
917 repo->packidx_cache[0] = packidx;
919 return NULL;
922 static int
923 is_packidx_filename(const char *name, size_t len)
925 if (len != GOT_PACKIDX_NAMELEN)
926 return 0;
928 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
929 return 0;
931 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
932 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
933 return 0;
935 return 1;
938 const struct got_error *
939 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
940 struct got_repository *repo, struct got_object_id *id)
942 const struct got_error *err;
943 DIR *packdir = NULL;
944 struct dirent *dent;
945 char *path_packidx;
946 size_t i;
947 int packdir_fd;
949 /* Search pack index cache. */
950 for (i = 0; i < nitems(repo->packidx_cache); i++) {
951 if (repo->packidx_cache[i] == NULL)
952 break;
953 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
954 if (*idx != -1) {
955 *packidx = repo->packidx_cache[i];
956 /*
957 * Move this cache entry to the front. Repeatedly
958 * searching a wrong pack index can be expensive.
959 */
960 if (i > 0) {
961 struct got_packidx *p;
962 p = repo->packidx_cache[0];
963 repo->packidx_cache[0] = *packidx;
964 repo->packidx_cache[i] = p;
966 return NULL;
969 /* No luck. Search the filesystem. */
971 packdir_fd = openat(got_repo_get_fd(repo),
972 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
973 if (packdir_fd == -1) {
974 if (errno == ENOENT)
975 err = got_error_no_obj(id);
976 else
977 err = got_error_from_errno_fmt("openat: %s/%s",
978 got_repo_get_path_git_dir(repo),
979 GOT_OBJECTS_PACK_DIR);
980 goto done;
983 packdir = fdopendir(packdir_fd);
984 if (packdir == NULL) {
985 err = got_error_from_errno("fdopendir");
986 goto done;
989 while ((dent = readdir(packdir)) != NULL) {
990 int is_cached = 0;
992 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
993 continue;
995 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
996 dent->d_name) == -1) {
997 err = got_error_from_errno("asprintf");
998 goto done;
1001 for (i = 0; i < nitems(repo->packidx_cache); i++) {
1002 if (repo->packidx_cache[i] == NULL)
1003 break;
1004 if (strcmp(repo->packidx_cache[i]->path_packidx,
1005 path_packidx) == 0) {
1006 is_cached = 1;
1007 break;
1010 if (is_cached) {
1011 free(path_packidx);
1012 continue; /* already searched */
1015 err = got_packidx_open(packidx, got_repo_get_fd(repo),
1016 path_packidx, 0);
1017 if (err) {
1018 free(path_packidx);
1019 goto done;
1022 err = cache_packidx(repo, *packidx, path_packidx);
1023 free(path_packidx);
1024 if (err)
1025 goto done;
1027 *idx = got_packidx_get_object_idx(*packidx, id);
1028 if (*idx != -1) {
1029 err = NULL; /* found the object */
1030 goto done;
1034 err = got_error_no_obj(id);
1035 done:
1036 if (packdir && closedir(packdir) != 0 && err == NULL)
1037 err = got_error_from_errno("closedir");
1038 return err;
1041 static const struct got_error *
1042 read_packfile_hdr(int fd, struct got_packidx *packidx)
1044 const struct got_error *err = NULL;
1045 uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
1046 struct got_packfile_hdr hdr;
1047 ssize_t n;
1049 n = read(fd, &hdr, sizeof(hdr));
1050 if (n < 0)
1051 return got_error_from_errno("read");
1052 if (n != sizeof(hdr))
1053 return got_error(GOT_ERR_BAD_PACKFILE);
1055 if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
1056 be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
1057 be32toh(hdr.nobjects) != totobj)
1058 err = got_error(GOT_ERR_BAD_PACKFILE);
1060 return err;
1063 static const struct got_error *
1064 open_packfile(int *fd, struct got_repository *repo,
1065 const char *relpath, struct got_packidx *packidx)
1067 const struct got_error *err = NULL;
1069 *fd = openat(got_repo_get_fd(repo), relpath, O_RDONLY | O_NOFOLLOW);
1070 if (*fd == -1)
1071 return got_error_from_errno_fmt("openat: %s/%s",
1072 got_repo_get_path_git_dir(repo), relpath);
1074 if (packidx) {
1075 err = read_packfile_hdr(*fd, packidx);
1076 if (err) {
1077 close(*fd);
1078 *fd = -1;
1082 return err;
1085 const struct got_error *
1086 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
1087 const char *path_packfile, struct got_packidx *packidx)
1089 const struct got_error *err = NULL;
1090 struct got_pack *pack = NULL;
1091 struct stat sb;
1092 size_t i;
1094 if (packp)
1095 *packp = NULL;
1097 for (i = 0; i < nitems(repo->packs); i++) {
1098 pack = &repo->packs[i];
1099 if (pack->path_packfile == NULL)
1100 break;
1101 if (strcmp(pack->path_packfile, path_packfile) == 0)
1102 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
1105 if (i == nitems(repo->packs) - 1) {
1106 err = got_pack_close(&repo->packs[i - 1]);
1107 if (err)
1108 return err;
1109 memmove(&repo->packs[1], &repo->packs[0],
1110 sizeof(repo->packs) - sizeof(repo->packs[0]));
1111 i = 0;
1114 pack = &repo->packs[i];
1116 pack->path_packfile = strdup(path_packfile);
1117 if (pack->path_packfile == NULL) {
1118 err = got_error_from_errno("strdup");
1119 goto done;
1122 err = open_packfile(&pack->fd, repo, path_packfile, packidx);
1123 if (err)
1124 goto done;
1126 if (fstat(pack->fd, &sb) != 0) {
1127 err = got_error_from_errno("fstat");
1128 goto done;
1130 pack->filesize = sb.st_size;
1132 pack->privsep_child = NULL;
1134 #ifndef GOT_PACK_NO_MMAP
1135 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1136 pack->fd, 0);
1137 if (pack->map == MAP_FAILED) {
1138 if (errno != ENOMEM) {
1139 err = got_error_from_errno("mmap");
1140 goto done;
1142 pack->map = NULL; /* fall back to read(2) */
1144 #endif
1145 done:
1146 if (err) {
1147 if (pack) {
1148 free(pack->path_packfile);
1149 memset(pack, 0, sizeof(*pack));
1151 } else if (packp)
1152 *packp = pack;
1153 return err;
1156 struct got_pack *
1157 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1159 struct got_pack *pack = NULL;
1160 size_t i;
1162 for (i = 0; i < nitems(repo->packs); i++) {
1163 pack = &repo->packs[i];
1164 if (pack->path_packfile == NULL)
1165 break;
1166 if (strcmp(pack->path_packfile, path_packfile) == 0)
1167 return pack;
1170 return NULL;
1173 const struct got_error *
1174 got_repo_init(const char *repo_path)
1176 const struct got_error *err = NULL;
1177 const char *dirnames[] = {
1178 GOT_OBJECTS_DIR,
1179 GOT_OBJECTS_PACK_DIR,
1180 GOT_REFS_DIR,
1182 const char *description_str = "Unnamed repository; "
1183 "edit this file 'description' to name the repository.";
1184 const char *headref_str = "ref: refs/heads/main";
1185 const char *gitconfig_str = "[core]\n"
1186 "\trepositoryformatversion = 0\n"
1187 "\tfilemode = true\n"
1188 "\tbare = true\n";
1189 char *path;
1190 size_t i;
1192 if (!got_path_dir_is_empty(repo_path))
1193 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1195 for (i = 0; i < nitems(dirnames); i++) {
1196 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1197 return got_error_from_errno("asprintf");
1199 err = got_path_mkdir(path);
1200 free(path);
1201 if (err)
1202 return err;
1205 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1206 return got_error_from_errno("asprintf");
1207 err = got_path_create_file(path, description_str);
1208 free(path);
1209 if (err)
1210 return err;
1212 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1213 return got_error_from_errno("asprintf");
1214 err = got_path_create_file(path, headref_str);
1215 free(path);
1216 if (err)
1217 return err;
1219 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1220 return got_error_from_errno("asprintf");
1221 err = got_path_create_file(path, gitconfig_str);
1222 free(path);
1223 if (err)
1224 return err;
1226 return NULL;
1229 static const struct got_error *
1230 match_packed_object(struct got_object_id **unique_id,
1231 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1233 const struct got_error *err = NULL;
1234 DIR *packdir = NULL;
1235 struct dirent *dent;
1236 char *path_packidx;
1237 struct got_object_id_queue matched_ids;
1238 int packdir_fd;
1240 SIMPLEQ_INIT(&matched_ids);
1242 packdir_fd = openat(got_repo_get_fd(repo),
1243 GOT_OBJECTS_PACK_DIR, O_DIRECTORY);
1244 if (packdir_fd == -1) {
1245 if (errno != ENOENT)
1246 err = got_error_from_errno2("openat", GOT_OBJECTS_PACK_DIR);
1247 goto done;
1250 packdir = fdopendir(packdir_fd);
1251 if (packdir == NULL) {
1252 err = got_error_from_errno("fdopendir");
1253 goto done;
1256 while ((dent = readdir(packdir)) != NULL) {
1257 struct got_packidx *packidx;
1258 struct got_object_qid *qid;
1260 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1261 continue;
1263 if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR,
1264 dent->d_name) == -1) {
1265 err = got_error_from_errno("strdup");
1266 break;
1269 err = got_packidx_open(&packidx, got_repo_get_fd(repo),
1270 path_packidx, 0);
1271 free(path_packidx);
1272 if (err)
1273 break;
1275 err = got_packidx_match_id_str_prefix(&matched_ids,
1276 packidx, id_str_prefix);
1277 if (err) {
1278 got_packidx_close(packidx);
1279 break;
1281 err = got_packidx_close(packidx);
1282 if (err)
1283 break;
1285 SIMPLEQ_FOREACH(qid, &matched_ids, entry) {
1286 if (obj_type != GOT_OBJ_TYPE_ANY) {
1287 int matched_type;
1288 err = got_object_get_type(&matched_type, repo,
1289 qid->id);
1290 if (err)
1291 goto done;
1292 if (matched_type != obj_type)
1293 continue;
1295 if (*unique_id == NULL) {
1296 *unique_id = got_object_id_dup(qid->id);
1297 if (*unique_id == NULL) {
1298 err = got_error_from_errno("malloc");
1299 goto done;
1301 } else {
1302 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1303 continue; /* packed multiple times */
1304 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1305 goto done;
1309 done:
1310 got_object_id_queue_free(&matched_ids);
1311 if (packdir && closedir(packdir) != 0 && err == NULL)
1312 err = got_error_from_errno("closedir");
1313 if (err) {
1314 free(*unique_id);
1315 *unique_id = NULL;
1317 return err;
1320 static const struct got_error *
1321 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1322 const char *object_dir, const char *id_str_prefix, int obj_type,
1323 struct got_repository *repo)
1325 const struct got_error *err = NULL;
1326 char *path;
1327 DIR *dir = NULL;
1328 struct dirent *dent;
1329 struct got_object_id id;
1331 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1332 err = got_error_from_errno("asprintf");
1333 goto done;
1336 dir = opendir(path);
1337 if (dir == NULL) {
1338 if (errno == ENOENT) {
1339 err = NULL;
1340 goto done;
1342 err = got_error_from_errno2("opendir", path);
1343 goto done;
1345 while ((dent = readdir(dir)) != NULL) {
1346 char *id_str;
1347 int cmp;
1349 if (strcmp(dent->d_name, ".") == 0 ||
1350 strcmp(dent->d_name, "..") == 0)
1351 continue;
1353 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1354 err = got_error_from_errno("asprintf");
1355 goto done;
1358 if (!got_parse_sha1_digest(id.sha1, id_str))
1359 continue;
1362 * Directory entries do not necessarily appear in
1363 * sorted order, so we must iterate over all of them.
1365 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1366 if (cmp != 0) {
1367 free(id_str);
1368 continue;
1371 if (*unique_id == NULL) {
1372 if (obj_type != GOT_OBJ_TYPE_ANY) {
1373 int matched_type;
1374 err = got_object_get_type(&matched_type, repo,
1375 &id);
1376 if (err)
1377 goto done;
1378 if (matched_type != obj_type)
1379 continue;
1381 *unique_id = got_object_id_dup(&id);
1382 if (*unique_id == NULL) {
1383 err = got_error_from_errno("got_object_id_dup");
1384 free(id_str);
1385 goto done;
1387 } else {
1388 if (got_object_id_cmp(*unique_id, &id) == 0)
1389 continue; /* both packed and loose */
1390 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1391 free(id_str);
1392 goto done;
1395 done:
1396 if (dir && closedir(dir) != 0 && err == NULL)
1397 err = got_error_from_errno("closedir");
1398 if (err) {
1399 free(*unique_id);
1400 *unique_id = NULL;
1402 free(path);
1403 return err;
1406 const struct got_error *
1407 got_repo_match_object_id_prefix(struct got_object_id **id,
1408 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1410 const struct got_error *err = NULL;
1411 char *path_objects = got_repo_get_path_objects(repo);
1412 char *object_dir = NULL;
1413 size_t len;
1414 int i;
1416 *id = NULL;
1418 for (i = 0; i < strlen(id_str_prefix); i++) {
1419 if (isxdigit((unsigned char)id_str_prefix[i]))
1420 continue;
1421 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1424 len = strlen(id_str_prefix);
1425 if (len >= 2) {
1426 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1427 if (err)
1428 goto done;
1429 object_dir = strndup(id_str_prefix, 2);
1430 if (object_dir == NULL) {
1431 err = got_error_from_errno("strdup");
1432 goto done;
1434 err = match_loose_object(id, path_objects, object_dir,
1435 id_str_prefix, obj_type, repo);
1436 } else if (len == 1) {
1437 int i;
1438 for (i = 0; i < 0xf; i++) {
1439 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1440 == -1) {
1441 err = got_error_from_errno("asprintf");
1442 goto done;
1444 err = match_packed_object(id, repo, object_dir,
1445 obj_type);
1446 if (err)
1447 goto done;
1448 err = match_loose_object(id, path_objects, object_dir,
1449 id_str_prefix, obj_type, repo);
1450 if (err)
1451 goto done;
1453 } else {
1454 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1455 goto done;
1457 done:
1458 free(object_dir);
1459 if (err) {
1460 free(*id);
1461 *id = NULL;
1462 } else if (*id == NULL)
1463 err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ);
1465 return err;
1468 const struct got_error *
1469 got_repo_match_object_id(struct got_object_id **id, char **label,
1470 const char *id_str, int obj_type, struct got_reflist_head *refs,
1471 struct got_repository *repo)
1473 const struct got_error *err;
1474 struct got_tag_object *tag;
1475 struct got_reference *ref = NULL;
1477 *id = NULL;
1478 if (label)
1479 *label = NULL;
1481 if (refs) {
1482 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
1483 refs, repo);
1484 if (err == NULL) {
1485 *id = got_object_id_dup(
1486 got_object_tag_get_object_id(tag));
1487 if (*id == NULL)
1488 err = got_error_from_errno("got_object_id_dup");
1489 else if (label && asprintf(label, "refs/tags/%s",
1490 got_object_tag_get_name(tag)) == -1) {
1491 err = got_error_from_errno("asprintf");
1492 free(*id);
1493 *id = NULL;
1495 got_object_tag_close(tag);
1496 return err;
1497 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1498 err->code != GOT_ERR_NO_OBJ)
1499 return err;
1502 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1503 if (err) {
1504 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1505 return err;
1506 err = got_ref_open(&ref, repo, id_str, 0);
1507 if (err != NULL)
1508 goto done;
1509 if (label) {
1510 *label = strdup(got_ref_get_name(ref));
1511 if (*label == NULL) {
1512 err = got_error_from_errno("strdup");
1513 goto done;
1516 err = got_ref_resolve(id, repo, ref);
1517 } else if (label) {
1518 err = got_object_id_str(label, *id);
1519 if (*label == NULL) {
1520 err = got_error_from_errno("strdup");
1521 goto done;
1524 done:
1525 if (ref)
1526 got_ref_close(ref);
1527 return err;
1530 const struct got_error *
1531 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1532 int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
1534 const struct got_error *err = NULL;
1535 struct got_reflist_entry *re;
1536 struct got_object_id *tag_id;
1537 int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
1539 *tag = NULL;
1541 TAILQ_FOREACH(re, refs, entry) {
1542 const char *refname;
1543 refname = got_ref_get_name(re->ref);
1544 if (got_ref_is_symbolic(re->ref))
1545 continue;
1546 if (strncmp(refname, "refs/tags/", 10) != 0)
1547 continue;
1548 if (!name_is_absolute)
1549 refname += strlen("refs/tags/");
1550 if (strcmp(refname, name) != 0)
1551 continue;
1552 err = got_ref_resolve(&tag_id, repo, re->ref);
1553 if (err)
1554 break;
1555 err = got_object_open_as_tag(tag, repo, tag_id);
1556 free(tag_id);
1557 if (err)
1558 break;
1559 if (obj_type == GOT_OBJ_TYPE_ANY ||
1560 got_object_tag_get_object_type(*tag) == obj_type)
1561 break;
1562 got_object_tag_close(*tag);
1563 *tag = NULL;
1566 if (err == NULL && *tag == NULL)
1567 err = got_error_path(name, GOT_ERR_NO_OBJ);
1568 return err;
1571 static const struct got_error *
1572 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1573 const char *name, mode_t mode, struct got_object_id *blob_id)
1575 const struct got_error *err = NULL;
1577 *new_te = NULL;
1579 *new_te = calloc(1, sizeof(**new_te));
1580 if (*new_te == NULL)
1581 return got_error_from_errno("calloc");
1583 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1584 sizeof((*new_te)->name)) {
1585 err = got_error(GOT_ERR_NO_SPACE);
1586 goto done;
1589 if (S_ISLNK(mode)) {
1590 (*new_te)->mode = S_IFLNK;
1591 } else {
1592 (*new_te)->mode = S_IFREG;
1593 (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
1595 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1596 done:
1597 if (err && *new_te) {
1598 free(*new_te);
1599 *new_te = NULL;
1601 return err;
1604 static const struct got_error *
1605 import_file(struct got_tree_entry **new_te, struct dirent *de,
1606 const char *path, struct got_repository *repo)
1608 const struct got_error *err;
1609 struct got_object_id *blob_id = NULL;
1610 char *filepath;
1611 struct stat sb;
1613 if (asprintf(&filepath, "%s%s%s", path,
1614 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1615 return got_error_from_errno("asprintf");
1617 if (lstat(filepath, &sb) != 0) {
1618 err = got_error_from_errno2("lstat", path);
1619 goto done;
1622 err = got_object_blob_create(&blob_id, filepath, repo);
1623 if (err)
1624 goto done;
1626 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1627 blob_id);
1628 done:
1629 free(filepath);
1630 if (err)
1631 free(blob_id);
1632 return err;
1635 static const struct got_error *
1636 insert_tree_entry(struct got_tree_entry *new_te,
1637 struct got_pathlist_head *paths)
1639 const struct got_error *err = NULL;
1640 struct got_pathlist_entry *new_pe;
1642 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1643 if (err)
1644 return err;
1645 if (new_pe == NULL)
1646 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1647 return NULL;
1650 static const struct got_error *write_tree(struct got_object_id **,
1651 const char *, struct got_pathlist_head *, struct got_repository *,
1652 got_repo_import_cb progress_cb, void *progress_arg);
1654 static const struct got_error *
1655 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1656 const char *path, struct got_pathlist_head *ignores,
1657 struct got_repository *repo,
1658 got_repo_import_cb progress_cb, void *progress_arg)
1660 const struct got_error *err;
1661 struct got_object_id *id = NULL;
1662 char *subdirpath;
1664 if (asprintf(&subdirpath, "%s%s%s", path,
1665 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1666 return got_error_from_errno("asprintf");
1668 (*new_te) = calloc(1, sizeof(**new_te));
1669 if (*new_te == NULL)
1670 return got_error_from_errno("calloc");
1671 (*new_te)->mode = S_IFDIR;
1672 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1673 sizeof((*new_te)->name)) {
1674 err = got_error(GOT_ERR_NO_SPACE);
1675 goto done;
1677 err = write_tree(&id, subdirpath, ignores, repo,
1678 progress_cb, progress_arg);
1679 if (err)
1680 goto done;
1681 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1683 done:
1684 free(id);
1685 free(subdirpath);
1686 if (err) {
1687 free(*new_te);
1688 *new_te = NULL;
1690 return err;
1693 static const struct got_error *
1694 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1695 struct got_pathlist_head *ignores, struct got_repository *repo,
1696 got_repo_import_cb progress_cb, void *progress_arg)
1698 const struct got_error *err = NULL;
1699 DIR *dir;
1700 struct dirent *de;
1701 int nentries;
1702 struct got_tree_entry *new_te = NULL;
1703 struct got_pathlist_head paths;
1704 struct got_pathlist_entry *pe;
1706 *new_tree_id = NULL;
1708 TAILQ_INIT(&paths);
1710 dir = opendir(path_dir);
1711 if (dir == NULL) {
1712 err = got_error_from_errno2("opendir", path_dir);
1713 goto done;
1716 nentries = 0;
1717 while ((de = readdir(dir)) != NULL) {
1718 int ignore = 0;
1719 int type;
1721 if (strcmp(de->d_name, ".") == 0 ||
1722 strcmp(de->d_name, "..") == 0)
1723 continue;
1725 TAILQ_FOREACH(pe, ignores, entry) {
1726 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1727 ignore = 1;
1728 break;
1731 if (ignore)
1732 continue;
1734 err = got_path_dirent_type(&type, path_dir, de);
1735 if (err)
1736 goto done;
1738 if (type == DT_DIR) {
1739 err = import_subdir(&new_te, de, path_dir,
1740 ignores, repo, progress_cb, progress_arg);
1741 if (err) {
1742 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1743 goto done;
1744 err = NULL;
1745 continue;
1747 } else if (type == DT_REG || type == DT_LNK) {
1748 err = import_file(&new_te, de, path_dir, repo);
1749 if (err)
1750 goto done;
1751 } else
1752 continue;
1754 err = insert_tree_entry(new_te, &paths);
1755 if (err)
1756 goto done;
1757 nentries++;
1760 if (TAILQ_EMPTY(&paths)) {
1761 err = got_error_msg(GOT_ERR_NO_TREE_ENTRY,
1762 "cannot create tree without any entries");
1763 goto done;
1766 TAILQ_FOREACH(pe, &paths, entry) {
1767 struct got_tree_entry *te = pe->data;
1768 char *path;
1769 if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
1770 continue;
1771 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1772 err = got_error_from_errno("asprintf");
1773 goto done;
1775 err = (*progress_cb)(progress_arg, path);
1776 free(path);
1777 if (err)
1778 goto done;
1781 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1782 done:
1783 if (dir)
1784 closedir(dir);
1785 got_pathlist_free(&paths);
1786 return err;
1789 const struct got_error *
1790 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1791 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1792 struct got_repository *repo, got_repo_import_cb progress_cb,
1793 void *progress_arg)
1795 const struct got_error *err;
1796 struct got_object_id *new_tree_id;
1798 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1799 progress_cb, progress_arg);
1800 if (err)
1801 return err;
1803 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1804 author, time(NULL), author, time(NULL), logmsg, repo);
1805 free(new_tree_id);
1806 return err;