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>
23 #include <sys/syslimits.h>
25 #include <ctype.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 <zlib.h>
36 #include <errno.h>
37 #include <libgen.h>
38 #include <stdint.h>
39 #include <imsg.h>
40 #include <uuid.h>
42 #include "got_error.h"
43 #include "got_reference.h"
44 #include "got_repository.h"
45 #include "got_path.h"
46 #include "got_cancel.h"
47 #include "got_worktree.h"
48 #include "got_object.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_object.h"
53 #include "got_lib_object_parse.h"
54 #include "got_lib_object_create.h"
55 #include "got_lib_pack.h"
56 #include "got_lib_privsep.h"
57 #include "got_lib_worktree.h"
58 #include "got_lib_sha1.h"
59 #include "got_lib_object_cache.h"
60 #include "got_lib_repository.h"
62 #ifndef nitems
63 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
64 #endif
66 const char *
67 got_repo_get_path(struct got_repository *repo)
68 {
69 return repo->path;
70 }
72 const char *
73 got_repo_get_path_git_dir(struct got_repository *repo)
74 {
75 return repo->path_git_dir;
76 }
78 const char *
79 got_repo_get_gitconfig_author_name(struct got_repository *repo)
80 {
81 return repo->gitconfig_author_name;
82 }
84 const char *
85 got_repo_get_gitconfig_author_email(struct got_repository *repo)
86 {
87 return repo->gitconfig_author_email;
88 }
90 const char *
91 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
92 {
93 return repo->global_gitconfig_author_name;
94 }
96 const char *
97 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
98 {
99 return repo->global_gitconfig_author_email;
102 const char *
103 got_repo_get_gitconfig_owner(struct got_repository *repo)
105 return repo->gitconfig_owner;
108 int
109 got_repo_is_bare(struct got_repository *repo)
111 return (strcmp(repo->path, repo->path_git_dir) == 0);
114 static char *
115 get_path_git_child(struct got_repository *repo, const char *basename)
117 char *path_child;
119 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
120 basename) == -1)
121 return NULL;
123 return path_child;
126 char *
127 got_repo_get_path_objects(struct got_repository *repo)
129 return get_path_git_child(repo, GOT_OBJECTS_DIR);
132 char *
133 got_repo_get_path_objects_pack(struct got_repository *repo)
135 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
138 char *
139 got_repo_get_path_refs(struct got_repository *repo)
141 return get_path_git_child(repo, GOT_REFS_DIR);
144 char *
145 got_repo_get_path_packed_refs(struct got_repository *repo)
147 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
150 static char *
151 get_path_head(struct got_repository *repo)
153 return get_path_git_child(repo, GOT_HEAD_FILE);
156 char *
157 got_repo_get_path_gitconfig(struct got_repository *repo)
159 return get_path_git_child(repo, GOT_GITCONFIG);
162 void
163 got_repo_get_gitconfig_remotes(int *nremotes, struct got_remote_repo **remotes,
164 struct got_repository *repo)
166 *nremotes = repo->ngitconfig_remotes;
167 *remotes = repo->gitconfig_remotes;
170 static int
171 is_git_repo(struct got_repository *repo)
173 const char *path_git = got_repo_get_path_git_dir(repo);
174 char *path_objects = got_repo_get_path_objects(repo);
175 char *path_refs = got_repo_get_path_refs(repo);
176 char *path_head = get_path_head(repo);
177 int ret = 0;
178 struct stat sb;
179 struct got_reference *head_ref;
181 if (lstat(path_git, &sb) == -1)
182 goto done;
183 if (!S_ISDIR(sb.st_mode))
184 goto done;
186 if (lstat(path_objects, &sb) == -1)
187 goto done;
188 if (!S_ISDIR(sb.st_mode))
189 goto done;
191 if (lstat(path_refs, &sb) == -1)
192 goto done;
193 if (!S_ISDIR(sb.st_mode))
194 goto done;
196 if (lstat(path_head, &sb) == -1)
197 goto done;
198 if (!S_ISREG(sb.st_mode))
199 goto done;
201 /* Check if the HEAD reference can be opened. */
202 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
203 goto done;
204 got_ref_close(head_ref);
206 ret = 1;
207 done:
208 free(path_objects);
209 free(path_refs);
210 free(path_head);
211 return ret;
215 const struct got_error *
216 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
217 struct got_object *obj)
219 #ifndef GOT_NO_OBJ_CACHE
220 const struct got_error *err = NULL;
221 err = got_object_cache_add(&repo->objcache, id, obj);
222 if (err) {
223 if (err->code == GOT_ERR_OBJ_EXISTS ||
224 err->code == GOT_ERR_OBJ_TOO_LARGE)
225 err = NULL;
226 return err;
228 obj->refcnt++;
229 #endif
230 return NULL;
233 struct got_object *
234 got_repo_get_cached_object(struct got_repository *repo,
235 struct got_object_id *id)
237 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
240 const struct got_error *
241 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
242 struct got_tree_object *tree)
244 #ifndef GOT_NO_OBJ_CACHE
245 const struct got_error *err = NULL;
246 err = got_object_cache_add(&repo->treecache, id, tree);
247 if (err) {
248 if (err->code == GOT_ERR_OBJ_EXISTS ||
249 err->code == GOT_ERR_OBJ_TOO_LARGE)
250 err = NULL;
251 return err;
253 tree->refcnt++;
254 #endif
255 return NULL;
258 struct got_tree_object *
259 got_repo_get_cached_tree(struct got_repository *repo,
260 struct got_object_id *id)
262 return (struct got_tree_object *)got_object_cache_get(
263 &repo->treecache, id);
266 const struct got_error *
267 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
268 struct got_commit_object *commit)
270 #ifndef GOT_NO_OBJ_CACHE
271 const struct got_error *err = NULL;
272 err = got_object_cache_add(&repo->commitcache, id, commit);
273 if (err) {
274 if (err->code == GOT_ERR_OBJ_EXISTS ||
275 err->code == GOT_ERR_OBJ_TOO_LARGE)
276 err = NULL;
277 return err;
279 commit->refcnt++;
280 #endif
281 return NULL;
284 struct got_commit_object *
285 got_repo_get_cached_commit(struct got_repository *repo,
286 struct got_object_id *id)
288 return (struct got_commit_object *)got_object_cache_get(
289 &repo->commitcache, id);
292 const struct got_error *
293 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
294 struct got_tag_object *tag)
296 #ifndef GOT_NO_OBJ_CACHE
297 const struct got_error *err = NULL;
298 err = got_object_cache_add(&repo->tagcache, id, tag);
299 if (err) {
300 if (err->code == GOT_ERR_OBJ_EXISTS ||
301 err->code == GOT_ERR_OBJ_TOO_LARGE)
302 err = NULL;
303 return err;
305 tag->refcnt++;
306 #endif
307 return NULL;
310 struct got_tag_object *
311 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
313 return (struct got_tag_object *)got_object_cache_get(
314 &repo->tagcache, id);
317 const struct got_error *
318 open_repo(struct got_repository *repo, const char *path)
320 const struct got_error *err = NULL;
322 /* bare git repository? */
323 repo->path_git_dir = strdup(path);
324 if (repo->path_git_dir == NULL)
325 return got_error_from_errno("strdup");
326 if (is_git_repo(repo)) {
327 repo->path = strdup(repo->path_git_dir);
328 if (repo->path == NULL) {
329 err = got_error_from_errno("strdup");
330 goto done;
332 return NULL;
335 /* git repository with working tree? */
336 free(repo->path_git_dir);
337 repo->path_git_dir = NULL;
338 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
339 err = got_error_from_errno("asprintf");
340 goto done;
342 if (is_git_repo(repo)) {
343 repo->path = strdup(path);
344 if (repo->path == NULL) {
345 err = got_error_from_errno("strdup");
346 goto done;
348 return NULL;
351 err = got_error(GOT_ERR_NOT_GIT_REPO);
352 done:
353 if (err) {
354 free(repo->path);
355 repo->path = NULL;
356 free(repo->path_git_dir);
357 repo->path_git_dir = NULL;
359 return err;
362 static const struct got_error *
363 parse_gitconfig_file(int *gitconfig_repository_format_version,
364 char **gitconfig_author_name, char **gitconfig_author_email,
365 struct got_remote_repo **remotes, int *nremotes,
366 char **gitconfig_owner,
367 const char *gitconfig_path)
369 const struct got_error *err = NULL, *child_err = NULL;
370 int fd = -1;
371 int imsg_fds[2] = { -1, -1 };
372 pid_t pid;
373 struct imsgbuf *ibuf;
375 *gitconfig_repository_format_version = 0;
376 *gitconfig_author_name = NULL;
377 *gitconfig_author_email = NULL;
379 fd = open(gitconfig_path, O_RDONLY);
380 if (fd == -1) {
381 if (errno == ENOENT)
382 return NULL;
383 return got_error_from_errno2("open", gitconfig_path);
386 ibuf = calloc(1, sizeof(*ibuf));
387 if (ibuf == NULL) {
388 err = got_error_from_errno("calloc");
389 goto done;
392 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
393 err = got_error_from_errno("socketpair");
394 goto done;
397 pid = fork();
398 if (pid == -1) {
399 err = got_error_from_errno("fork");
400 goto done;
401 } else if (pid == 0) {
402 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
403 gitconfig_path);
404 /* not reached */
407 if (close(imsg_fds[1]) == -1) {
408 err = got_error_from_errno("close");
409 goto done;
411 imsg_fds[1] = -1;
412 imsg_init(ibuf, imsg_fds[0]);
414 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
415 if (err)
416 goto done;
417 fd = -1;
419 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
420 if (err)
421 goto done;
423 err = got_privsep_recv_gitconfig_int(
424 gitconfig_repository_format_version, ibuf);
425 if (err)
426 goto done;
428 err = got_privsep_send_gitconfig_author_name_req(ibuf);
429 if (err)
430 goto done;
432 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
433 if (err)
434 goto done;
436 err = got_privsep_send_gitconfig_author_email_req(ibuf);
437 if (err)
438 goto done;
440 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
441 if (err)
442 goto done;
444 if (remotes && nremotes) {
445 err = got_privsep_send_gitconfig_remotes_req(ibuf);
446 if (err)
447 goto done;
449 err = got_privsep_recv_gitconfig_remotes(remotes,
450 nremotes, ibuf);
451 if (err)
452 goto done;
455 if (gitconfig_owner) {
456 err = got_privsep_send_gitconfig_owner_req(ibuf);
457 if (err)
458 goto done;
459 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
460 if (err)
461 goto done;
464 imsg_clear(ibuf);
465 err = got_privsep_send_stop(imsg_fds[0]);
466 child_err = got_privsep_wait_for_child(pid);
467 if (child_err && err == NULL)
468 err = child_err;
469 done:
470 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
471 err = got_error_from_errno("close");
472 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
473 err = got_error_from_errno("close");
474 if (fd != -1 && close(fd) == -1 && err == NULL)
475 err = got_error_from_errno2("close", gitconfig_path);
476 free(ibuf);
477 return err;
480 static const struct got_error *
481 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
483 const struct got_error *err = NULL;
484 char *repo_gitconfig_path = NULL;
486 if (global_gitconfig_path) {
487 /* Read settings from ~/.gitconfig. */
488 int dummy_repo_version;
489 err = parse_gitconfig_file(&dummy_repo_version,
490 &repo->global_gitconfig_author_name,
491 &repo->global_gitconfig_author_email,
492 NULL, NULL, NULL, global_gitconfig_path);
493 if (err)
494 return err;
497 /* Read repository's .git/config file. */
498 repo_gitconfig_path = got_repo_get_path_gitconfig(repo);
499 if (repo_gitconfig_path == NULL)
500 return got_error_from_errno("got_repo_get_path_gitconfig");
502 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
503 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
504 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
505 &repo->gitconfig_owner, repo_gitconfig_path);
506 if (err)
507 goto done;
508 done:
509 free(repo_gitconfig_path);
510 return err;
513 const struct got_error *
514 got_repo_open(struct got_repository **repop, const char *path,
515 const char *global_gitconfig_path)
517 struct got_repository *repo = NULL;
518 const struct got_error *err = NULL;
519 char *abspath;
520 int i, tried_root = 0;
522 *repop = NULL;
524 if (got_path_is_absolute(path))
525 abspath = strdup(path);
526 else
527 abspath = got_path_get_absolute(path);
528 if (abspath == NULL)
529 return got_error_path(path, GOT_ERR_BAD_PATH);
531 repo = calloc(1, sizeof(*repo));
532 if (repo == NULL) {
533 err = got_error_from_errno("calloc");
534 goto done;
537 for (i = 0; i < nitems(repo->privsep_children); i++) {
538 memset(&repo->privsep_children[i], 0,
539 sizeof(repo->privsep_children[0]));
540 repo->privsep_children[i].imsg_fd = -1;
543 err = got_object_cache_init(&repo->objcache,
544 GOT_OBJECT_CACHE_TYPE_OBJ);
545 if (err)
546 goto done;
547 err = got_object_cache_init(&repo->treecache,
548 GOT_OBJECT_CACHE_TYPE_TREE);
549 if (err)
550 goto done;
551 err = got_object_cache_init(&repo->commitcache,
552 GOT_OBJECT_CACHE_TYPE_COMMIT);
553 if (err)
554 goto done;
555 err = got_object_cache_init(&repo->tagcache,
556 GOT_OBJECT_CACHE_TYPE_TAG);
557 if (err)
558 goto done;
560 path = realpath(abspath, NULL);
561 if (path == NULL) {
562 err = got_error_from_errno2("realpath", abspath);
563 goto done;
566 do {
567 err = open_repo(repo, path);
568 if (err == NULL)
569 break;
570 if (err->code != GOT_ERR_NOT_GIT_REPO)
571 break;
572 if (path[0] == '/' && path[1] == '\0') {
573 if (tried_root) {
574 err = got_error(GOT_ERR_NOT_GIT_REPO);
575 goto done;
577 tried_root = 1;
579 path = dirname(path);
580 if (path == NULL) {
581 err = got_error_from_errno2("dirname", path);
582 goto done;
584 } while (path);
586 err = read_gitconfig(repo, global_gitconfig_path);
587 if (err)
588 goto done;
589 if (repo->gitconfig_repository_format_version != 0)
590 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
591 done:
592 if (err)
593 got_repo_close(repo);
594 else
595 *repop = repo;
596 free(abspath);
597 return err;
600 const struct got_error *
601 got_repo_close(struct got_repository *repo)
603 const struct got_error *err = NULL, *child_err;
604 int i;
606 for (i = 0; i < nitems(repo->packidx_cache); i++) {
607 if (repo->packidx_cache[i] == NULL)
608 break;
609 got_packidx_close(repo->packidx_cache[i]);
612 for (i = 0; i < nitems(repo->packs); i++) {
613 if (repo->packs[i].path_packfile == NULL)
614 break;
615 got_pack_close(&repo->packs[i]);
618 free(repo->path);
619 free(repo->path_git_dir);
621 got_object_cache_close(&repo->objcache);
622 got_object_cache_close(&repo->treecache);
623 got_object_cache_close(&repo->commitcache);
624 got_object_cache_close(&repo->tagcache);
626 for (i = 0; i < nitems(repo->privsep_children); i++) {
627 if (repo->privsep_children[i].imsg_fd == -1)
628 continue;
629 imsg_clear(repo->privsep_children[i].ibuf);
630 free(repo->privsep_children[i].ibuf);
631 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
632 child_err = got_privsep_wait_for_child(
633 repo->privsep_children[i].pid);
634 if (child_err && err == NULL)
635 err = child_err;
636 if (close(repo->privsep_children[i].imsg_fd) != 0 &&
637 err == NULL)
638 err = got_error_from_errno("close");
641 free(repo->gitconfig_author_name);
642 free(repo->gitconfig_author_email);
643 for (i = 0; i < repo->ngitconfig_remotes; i++) {
644 free(repo->gitconfig_remotes[i].name);
645 free(repo->gitconfig_remotes[i].url);
647 free(repo->gitconfig_remotes);
648 free(repo);
650 return err;
653 const struct got_error *
654 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
655 const char *input_path, int check_disk)
657 const struct got_error *err = NULL;
658 const char *repo_abspath = NULL;
659 size_t repolen, len;
660 char *canonpath, *path = NULL;
662 *in_repo_path = NULL;
664 canonpath = strdup(input_path);
665 if (canonpath == NULL) {
666 err = got_error_from_errno("strdup");
667 goto done;
669 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
670 if (err)
671 goto done;
673 repo_abspath = got_repo_get_path(repo);
675 if (!check_disk || canonpath[0] == '\0') {
676 path = strdup(canonpath);
677 if (path == NULL) {
678 err = got_error_from_errno("strdup");
679 goto done;
681 } else {
682 path = realpath(canonpath, NULL);
683 if (path == NULL) {
684 if (errno != ENOENT) {
685 err = got_error_from_errno2("realpath",
686 canonpath);
687 goto done;
689 /*
690 * Path is not on disk.
691 * Assume it is already relative to repository root.
692 */
693 path = strdup(canonpath);
694 if (path == NULL) {
695 err = got_error_from_errno("strdup");
696 goto done;
700 repolen = strlen(repo_abspath);
701 len = strlen(path);
704 if (strcmp(path, repo_abspath) == 0) {
705 free(path);
706 path = strdup("");
707 if (path == NULL) {
708 err = got_error_from_errno("strdup");
709 goto done;
711 } else if (len > repolen &&
712 got_path_is_child(path, repo_abspath, repolen)) {
713 /* Matched an on-disk path inside repository. */
714 if (got_repo_is_bare(repo)) {
715 /*
716 * Matched an on-disk path inside repository
717 * database. Treat as repository-relative.
718 */
719 } else {
720 char *child;
721 /* Strip common prefix with repository path. */
722 err = got_path_skip_common_ancestor(&child,
723 repo_abspath, path);
724 if (err)
725 goto done;
726 free(path);
727 path = child;
729 } else {
730 /*
731 * Matched unrelated on-disk path.
732 * Treat it as repository-relative.
733 */
737 /* Make in-repository path absolute */
738 if (path[0] != '/') {
739 char *abspath;
740 if (asprintf(&abspath, "/%s", path) == -1) {
741 err = got_error_from_errno("asprintf");
742 goto done;
744 free(path);
745 path = abspath;
748 done:
749 free(canonpath);
750 if (err)
751 free(path);
752 else
753 *in_repo_path = path;
754 return err;
757 static const struct got_error *
758 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
759 const char *path_packidx)
761 const struct got_error *err = NULL;
762 int i;
764 for (i = 0; i < nitems(repo->packidx_cache); i++) {
765 if (repo->packidx_cache[i] == NULL)
766 break;
767 if (strcmp(repo->packidx_cache[i]->path_packidx,
768 path_packidx) == 0) {
769 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
772 if (i == nitems(repo->packidx_cache)) {
773 err = got_packidx_close(repo->packidx_cache[i - 1]);
774 if (err)
775 return err;
778 /*
779 * Insert the new pack index at the front so it will
780 * be searched first in the future.
781 */
782 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
783 sizeof(repo->packidx_cache) -
784 sizeof(repo->packidx_cache[0]));
785 repo->packidx_cache[0] = packidx;
787 return NULL;
790 static int
791 is_packidx_filename(const char *name, size_t len)
793 if (len != GOT_PACKIDX_NAMELEN)
794 return 0;
796 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
797 return 0;
799 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
800 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
801 return 0;
803 return 1;
806 const struct got_error *
807 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
808 struct got_repository *repo, struct got_object_id *id)
810 const struct got_error *err;
811 char *path_packdir;
812 DIR *packdir;
813 struct dirent *dent;
814 char *path_packidx;
815 int i;
817 /* Search pack index cache. */
818 for (i = 0; i < nitems(repo->packidx_cache); i++) {
819 if (repo->packidx_cache[i] == NULL)
820 break;
821 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
822 if (*idx != -1) {
823 *packidx = repo->packidx_cache[i];
824 /*
825 * Move this cache entry to the front. Repeatedly
826 * searching a wrong pack index can be expensive.
827 */
828 if (i > 0) {
829 struct got_packidx *p;
830 p = repo->packidx_cache[0];
831 repo->packidx_cache[0] = *packidx;
832 repo->packidx_cache[i] = p;
834 return NULL;
837 /* No luck. Search the filesystem. */
839 path_packdir = got_repo_get_path_objects_pack(repo);
840 if (path_packdir == NULL)
841 return got_error_from_errno("got_repo_get_path_objects_pack");
843 packdir = opendir(path_packdir);
844 if (packdir == NULL) {
845 if (errno == ENOENT)
846 err = got_error_no_obj(id);
847 else
848 err = got_error_from_errno2("opendir", path_packdir);
849 goto done;
852 while ((dent = readdir(packdir)) != NULL) {
853 int is_cached = 0;
855 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
856 continue;
858 if (asprintf(&path_packidx, "%s/%s", path_packdir,
859 dent->d_name) == -1) {
860 err = got_error_from_errno("asprintf");
861 goto done;
864 for (i = 0; i < nitems(repo->packidx_cache); i++) {
865 if (repo->packidx_cache[i] == NULL)
866 break;
867 if (strcmp(repo->packidx_cache[i]->path_packidx,
868 path_packidx) == 0) {
869 is_cached = 1;
870 break;
873 if (is_cached) {
874 free(path_packidx);
875 continue; /* already searched */
878 err = got_packidx_open(packidx, path_packidx, 0);
879 if (err) {
880 free(path_packidx);
881 goto done;
884 err = cache_packidx(repo, *packidx, path_packidx);
885 free(path_packidx);
886 if (err)
887 goto done;
889 *idx = got_packidx_get_object_idx(*packidx, id);
890 if (*idx != -1) {
891 err = NULL; /* found the object */
892 goto done;
896 err = got_error_no_obj(id);
897 done:
898 free(path_packdir);
899 if (packdir && closedir(packdir) != 0 && err == NULL)
900 err = got_error_from_errno("closedir");
901 return err;
904 static const struct got_error *
905 read_packfile_hdr(int fd, struct got_packidx *packidx)
907 const struct got_error *err = NULL;
908 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
909 struct got_packfile_hdr hdr;
910 ssize_t n;
912 n = read(fd, &hdr, sizeof(hdr));
913 if (n < 0)
914 return got_error_from_errno("read");
915 if (n != sizeof(hdr))
916 return got_error(GOT_ERR_BAD_PACKFILE);
918 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
919 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
920 betoh32(hdr.nobjects) != totobj)
921 err = got_error(GOT_ERR_BAD_PACKFILE);
923 return err;
926 static const struct got_error *
927 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
929 const struct got_error *err = NULL;
931 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
932 if (*fd == -1)
933 return got_error_from_errno2("open", path_packfile);
935 if (packidx) {
936 err = read_packfile_hdr(*fd, packidx);
937 if (err) {
938 close(*fd);
939 *fd = -1;
943 return err;
946 const struct got_error *
947 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
948 const char *path_packfile, struct got_packidx *packidx)
950 const struct got_error *err = NULL;
951 struct got_pack *pack = NULL;
952 struct stat sb;
953 int i;
955 if (packp)
956 *packp = NULL;
958 for (i = 0; i < nitems(repo->packs); i++) {
959 pack = &repo->packs[i];
960 if (pack->path_packfile == NULL)
961 break;
962 if (strcmp(pack->path_packfile, path_packfile) == 0)
963 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
966 if (i == nitems(repo->packs) - 1) {
967 err = got_pack_close(&repo->packs[i - 1]);
968 if (err)
969 return err;
970 memmove(&repo->packs[1], &repo->packs[0],
971 sizeof(repo->packs) - sizeof(repo->packs[0]));
972 i = 0;
975 pack = &repo->packs[i];
977 pack->path_packfile = strdup(path_packfile);
978 if (pack->path_packfile == NULL) {
979 err = got_error_from_errno("strdup");
980 goto done;
983 err = open_packfile(&pack->fd, path_packfile, packidx);
984 if (err)
985 goto done;
987 if (fstat(pack->fd, &sb) != 0) {
988 err = got_error_from_errno("fstat");
989 goto done;
991 pack->filesize = sb.st_size;
993 pack->privsep_child = NULL;
995 #ifndef GOT_PACK_NO_MMAP
996 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
997 pack->fd, 0);
998 if (pack->map == MAP_FAILED) {
999 if (errno != ENOMEM) {
1000 err = got_error_from_errno("mmap");
1001 goto done;
1003 pack->map = NULL; /* fall back to read(2) */
1005 #endif
1006 done:
1007 if (err) {
1008 if (pack) {
1009 free(pack->path_packfile);
1010 memset(pack, 0, sizeof(*pack));
1012 } else if (packp)
1013 *packp = pack;
1014 return err;
1017 struct got_pack *
1018 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1020 struct got_pack *pack = NULL;
1021 int i;
1023 for (i = 0; i < nitems(repo->packs); i++) {
1024 pack = &repo->packs[i];
1025 if (pack->path_packfile == NULL)
1026 break;
1027 if (strcmp(pack->path_packfile, path_packfile) == 0)
1028 return pack;
1031 return NULL;
1034 const struct got_error *
1035 got_repo_init(const char *repo_path)
1037 const struct got_error *err = NULL;
1038 const char *dirnames[] = {
1039 GOT_OBJECTS_DIR,
1040 GOT_OBJECTS_PACK_DIR,
1041 GOT_REFS_DIR,
1043 const char *description_str = "Unnamed repository; "
1044 "edit this file 'description' to name the repository.";
1045 const char *headref_str = "ref: refs/heads/main";
1046 const char *gitconfig_str = "[core]\n"
1047 "\trepositoryformatversion = 0\n"
1048 "\tfilemode = true\n"
1049 "\tbare = true\n";
1050 char *path;
1051 int i;
1053 if (!got_path_dir_is_empty(repo_path))
1054 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1056 for (i = 0; i < nitems(dirnames); i++) {
1057 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1058 return got_error_from_errno("asprintf");
1060 err = got_path_mkdir(path);
1061 free(path);
1062 if (err)
1063 return err;
1066 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1067 return got_error_from_errno("asprintf");
1068 err = got_path_create_file(path, description_str);
1069 free(path);
1070 if (err)
1071 return err;
1073 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1074 return got_error_from_errno("asprintf");
1075 err = got_path_create_file(path, headref_str);
1076 free(path);
1077 if (err)
1078 return err;
1080 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1081 return got_error_from_errno("asprintf");
1082 err = got_path_create_file(path, gitconfig_str);
1083 free(path);
1084 if (err)
1085 return err;
1087 return NULL;
1090 static const struct got_error *
1091 match_packed_object(struct got_object_id **unique_id,
1092 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1094 const struct got_error *err = NULL;
1095 char *path_packdir;
1096 DIR *packdir;
1097 struct dirent *dent;
1098 char *path_packidx;
1099 struct got_object_id_queue matched_ids;
1101 SIMPLEQ_INIT(&matched_ids);
1103 path_packdir = got_repo_get_path_objects_pack(repo);
1104 if (path_packdir == NULL)
1105 return got_error_from_errno("got_repo_get_path_objects_pack");
1107 packdir = opendir(path_packdir);
1108 if (packdir == NULL) {
1109 if (errno != ENOENT)
1110 err = got_error_from_errno2("opendir", path_packdir);
1111 goto done;
1114 while ((dent = readdir(packdir)) != NULL) {
1115 struct got_packidx *packidx;
1116 struct got_object_qid *qid;
1119 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1120 continue;
1122 if (asprintf(&path_packidx, "%s/%s", path_packdir,
1123 dent->d_name) == -1) {
1124 err = got_error_from_errno("asprintf");
1125 break;
1128 err = got_packidx_open(&packidx, path_packidx, 0);
1129 free(path_packidx);
1130 if (err)
1131 break;
1133 err = got_packidx_match_id_str_prefix(&matched_ids,
1134 packidx, id_str_prefix);
1135 if (err) {
1136 got_packidx_close(packidx);
1137 break;
1139 err = got_packidx_close(packidx);
1140 if (err)
1141 break;
1143 SIMPLEQ_FOREACH(qid, &matched_ids, entry) {
1144 if (obj_type != GOT_OBJ_TYPE_ANY) {
1145 int matched_type;
1146 err = got_object_get_type(&matched_type, repo,
1147 qid->id);
1148 if (err)
1149 goto done;
1150 if (matched_type != obj_type)
1151 continue;
1153 if (*unique_id == NULL) {
1154 *unique_id = got_object_id_dup(qid->id);
1155 if (*unique_id == NULL) {
1156 err = got_error_from_errno("malloc");
1157 goto done;
1159 } else {
1160 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1161 continue; /* packed multiple times */
1162 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1163 goto done;
1167 done:
1168 got_object_id_queue_free(&matched_ids);
1169 free(path_packdir);
1170 if (packdir && closedir(packdir) != 0 && err == NULL)
1171 err = got_error_from_errno("closedir");
1172 if (err) {
1173 free(*unique_id);
1174 *unique_id = NULL;
1176 return err;
1179 static const struct got_error *
1180 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1181 const char *object_dir, const char *id_str_prefix, int obj_type,
1182 struct got_repository *repo)
1184 const struct got_error *err = NULL;
1185 char *path;
1186 DIR *dir = NULL;
1187 struct dirent *dent;
1188 struct got_object_id id;
1190 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1191 err = got_error_from_errno("asprintf");
1192 goto done;
1195 dir = opendir(path);
1196 if (dir == NULL) {
1197 if (errno == ENOENT) {
1198 err = NULL;
1199 goto done;
1201 err = got_error_from_errno2("opendir", path);
1202 goto done;
1204 while ((dent = readdir(dir)) != NULL) {
1205 char *id_str;
1206 int cmp;
1208 if (strcmp(dent->d_name, ".") == 0 ||
1209 strcmp(dent->d_name, "..") == 0)
1210 continue;
1212 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1213 err = got_error_from_errno("asprintf");
1214 goto done;
1217 if (!got_parse_sha1_digest(id.sha1, id_str))
1218 continue;
1221 * Directory entries do not necessarily appear in
1222 * sorted order, so we must iterate over all of them.
1224 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1225 if (cmp != 0) {
1226 free(id_str);
1227 continue;
1230 if (*unique_id == NULL) {
1231 if (obj_type != GOT_OBJ_TYPE_ANY) {
1232 int matched_type;
1233 err = got_object_get_type(&matched_type, repo,
1234 &id);
1235 if (err)
1236 goto done;
1237 if (matched_type != obj_type)
1238 continue;
1240 *unique_id = got_object_id_dup(&id);
1241 if (*unique_id == NULL) {
1242 err = got_error_from_errno("got_object_id_dup");
1243 free(id_str);
1244 goto done;
1246 } else {
1247 if (got_object_id_cmp(*unique_id, &id) == 0)
1248 continue; /* both packed and loose */
1249 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1250 free(id_str);
1251 goto done;
1254 done:
1255 if (dir && closedir(dir) != 0 && err == NULL)
1256 err = got_error_from_errno("closedir");
1257 if (err) {
1258 free(*unique_id);
1259 *unique_id = NULL;
1261 free(path);
1262 return err;
1265 const struct got_error *
1266 got_repo_match_object_id_prefix(struct got_object_id **id,
1267 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1269 const struct got_error *err = NULL;
1270 char *path_objects = got_repo_get_path_objects(repo);
1271 char *object_dir = NULL;
1272 size_t len;
1273 int i;
1275 *id = NULL;
1277 for (i = 0; i < strlen(id_str_prefix); i++) {
1278 if (isxdigit((unsigned char)id_str_prefix[i]))
1279 continue;
1280 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1283 len = strlen(id_str_prefix);
1284 if (len >= 2) {
1285 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1286 if (err)
1287 goto done;
1288 object_dir = strndup(id_str_prefix, 2);
1289 if (object_dir == NULL) {
1290 err = got_error_from_errno("strdup");
1291 goto done;
1293 err = match_loose_object(id, path_objects, object_dir,
1294 id_str_prefix, obj_type, repo);
1295 } else if (len == 1) {
1296 int i;
1297 for (i = 0; i < 0xf; i++) {
1298 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1299 == -1) {
1300 err = got_error_from_errno("asprintf");
1301 goto done;
1303 err = match_packed_object(id, repo, object_dir,
1304 obj_type);
1305 if (err)
1306 goto done;
1307 err = match_loose_object(id, path_objects, object_dir,
1308 id_str_prefix, obj_type, repo);
1309 if (err)
1310 goto done;
1312 } else {
1313 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1314 goto done;
1316 done:
1317 free(object_dir);
1318 if (err) {
1319 free(*id);
1320 *id = NULL;
1321 } else if (*id == NULL)
1322 err = got_error(GOT_ERR_NO_OBJ);
1324 return err;
1327 const struct got_error *
1328 got_repo_match_object_id(struct got_object_id **id, char **label,
1329 const char *id_str, int obj_type, int resolve_tags,
1330 struct got_repository *repo)
1332 const struct got_error *err;
1333 struct got_tag_object *tag;
1334 struct got_reference *ref = NULL;
1336 *id = NULL;
1337 if (label)
1338 *label = NULL;
1340 if (resolve_tags) {
1341 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
1342 repo);
1343 if (err == NULL) {
1344 *id = got_object_id_dup(
1345 got_object_tag_get_object_id(tag));
1346 if (*id == NULL)
1347 err = got_error_from_errno("got_object_id_dup");
1348 else if (label && asprintf(label, "refs/tags/%s",
1349 got_object_tag_get_name(tag)) == -1) {
1350 err = got_error_from_errno("asprintf");
1351 free(*id);
1352 *id = NULL;
1354 got_object_tag_close(tag);
1355 return err;
1356 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1357 err->code != GOT_ERR_NO_OBJ)
1358 return err;
1361 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1362 if (err) {
1363 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1364 return err;
1365 err = got_ref_open(&ref, repo, id_str, 0);
1366 if (err != NULL)
1367 goto done;
1368 if (label) {
1369 *label = strdup(got_ref_get_name(ref));
1370 if (*label == NULL) {
1371 err = got_error_from_errno("strdup");
1372 goto done;
1375 err = got_ref_resolve(id, repo, ref);
1376 } else if (label) {
1377 err = got_object_id_str(label, *id);
1378 if (*label == NULL) {
1379 err = got_error_from_errno("strdup");
1380 goto done;
1383 done:
1384 if (ref)
1385 got_ref_close(ref);
1386 return err;
1389 const struct got_error *
1390 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1391 int obj_type, struct got_repository *repo)
1393 const struct got_error *err;
1394 struct got_reflist_head refs;
1395 struct got_reflist_entry *re;
1396 struct got_object_id *tag_id;
1398 SIMPLEQ_INIT(&refs);
1399 *tag = NULL;
1401 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_by_name, NULL);
1402 if (err)
1403 return err;
1405 SIMPLEQ_FOREACH(re, &refs, entry) {
1406 const char *refname;
1407 refname = got_ref_get_name(re->ref);
1408 if (got_ref_is_symbolic(re->ref))
1409 continue;
1410 refname += strlen("refs/tags/");
1411 if (strcmp(refname, name) != 0)
1412 continue;
1413 err = got_ref_resolve(&tag_id, repo, re->ref);
1414 if (err)
1415 break;
1416 err = got_object_open_as_tag(tag, repo, tag_id);
1417 free(tag_id);
1418 if (err)
1419 break;
1420 if (obj_type == GOT_OBJ_TYPE_ANY ||
1421 got_object_tag_get_object_type(*tag) == obj_type)
1422 break;
1423 got_object_tag_close(*tag);
1424 *tag = NULL;
1427 got_ref_list_free(&refs);
1428 if (err == NULL && *tag == NULL)
1429 err = got_error(GOT_ERR_NO_OBJ);
1430 return err;
1433 static const struct got_error *
1434 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1435 const char *name, mode_t mode, struct got_object_id *blob_id)
1437 const struct got_error *err = NULL;
1439 *new_te = NULL;
1441 *new_te = calloc(1, sizeof(**new_te));
1442 if (*new_te == NULL)
1443 return got_error_from_errno("calloc");
1445 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1446 sizeof((*new_te)->name)) {
1447 err = got_error(GOT_ERR_NO_SPACE);
1448 goto done;
1451 (*new_te)->mode = S_IFREG | (mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
1452 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1453 done:
1454 if (err && *new_te) {
1455 free(*new_te);
1456 *new_te = NULL;
1458 return err;
1461 static const struct got_error *
1462 import_file(struct got_tree_entry **new_te, struct dirent *de,
1463 const char *path, struct got_repository *repo)
1465 const struct got_error *err;
1466 struct got_object_id *blob_id = NULL;
1467 char *filepath;
1468 struct stat sb;
1470 if (asprintf(&filepath, "%s%s%s", path,
1471 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1472 return got_error_from_errno("asprintf");
1474 if (lstat(filepath, &sb) != 0) {
1475 err = got_error_from_errno2("lstat", path);
1476 goto done;
1479 err = got_object_blob_create(&blob_id, filepath, repo);
1480 if (err)
1481 goto done;
1483 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1484 blob_id);
1485 done:
1486 free(filepath);
1487 if (err)
1488 free(blob_id);
1489 return err;
1492 static const struct got_error *
1493 insert_tree_entry(struct got_tree_entry *new_te,
1494 struct got_pathlist_head *paths)
1496 const struct got_error *err = NULL;
1497 struct got_pathlist_entry *new_pe;
1499 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1500 if (err)
1501 return err;
1502 if (new_pe == NULL)
1503 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1504 return NULL;
1507 static const struct got_error *write_tree(struct got_object_id **,
1508 const char *, struct got_pathlist_head *, struct got_repository *,
1509 got_repo_import_cb progress_cb, void *progress_arg);
1511 static const struct got_error *
1512 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1513 const char *path, struct got_pathlist_head *ignores,
1514 struct got_repository *repo,
1515 got_repo_import_cb progress_cb, void *progress_arg)
1517 const struct got_error *err;
1518 struct got_object_id *id = NULL;
1519 char *subdirpath;
1521 if (asprintf(&subdirpath, "%s%s%s", path,
1522 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1523 return got_error_from_errno("asprintf");
1525 (*new_te) = calloc(1, sizeof(**new_te));
1526 if (*new_te == NULL)
1527 return got_error_from_errno("calloc");
1528 (*new_te)->mode = S_IFDIR;
1529 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1530 sizeof((*new_te)->name)) {
1531 err = got_error(GOT_ERR_NO_SPACE);
1532 goto done;
1534 err = write_tree(&id, subdirpath, ignores, repo,
1535 progress_cb, progress_arg);
1536 if (err)
1537 goto done;
1538 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1540 done:
1541 free(id);
1542 free(subdirpath);
1543 if (err) {
1544 free(*new_te);
1545 *new_te = NULL;
1547 return err;
1550 static const struct got_error *
1551 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1552 struct got_pathlist_head *ignores, struct got_repository *repo,
1553 got_repo_import_cb progress_cb, void *progress_arg)
1555 const struct got_error *err = NULL;
1556 DIR *dir;
1557 struct dirent *de;
1558 int nentries;
1559 struct got_tree_entry *new_te = NULL;
1560 struct got_pathlist_head paths;
1561 struct got_pathlist_entry *pe;
1563 *new_tree_id = NULL;
1565 TAILQ_INIT(&paths);
1567 dir = opendir(path_dir);
1568 if (dir == NULL) {
1569 err = got_error_from_errno2("opendir", path_dir);
1570 goto done;
1573 nentries = 0;
1574 while ((de = readdir(dir)) != NULL) {
1575 int ignore = 0;
1577 if (strcmp(de->d_name, ".") == 0 ||
1578 strcmp(de->d_name, "..") == 0)
1579 continue;
1581 TAILQ_FOREACH(pe, ignores, entry) {
1582 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1583 ignore = 1;
1584 break;
1587 if (ignore)
1588 continue;
1589 if (de->d_type == DT_DIR) {
1590 err = import_subdir(&new_te, de, path_dir,
1591 ignores, repo, progress_cb, progress_arg);
1592 if (err) {
1593 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1594 goto done;
1595 err = NULL;
1596 continue;
1598 } else if (de->d_type == DT_REG) {
1599 err = import_file(&new_te, de, path_dir, repo);
1600 if (err)
1601 goto done;
1602 } else
1603 continue;
1605 err = insert_tree_entry(new_te, &paths);
1606 if (err)
1607 goto done;
1608 nentries++;
1611 if (TAILQ_EMPTY(&paths)) {
1612 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1613 goto done;
1616 TAILQ_FOREACH(pe, &paths, entry) {
1617 struct got_tree_entry *te = pe->data;
1618 char *path;
1619 if (!S_ISREG(te->mode))
1620 continue;
1621 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1622 err = got_error_from_errno("asprintf");
1623 goto done;
1625 err = (*progress_cb)(progress_arg, path);
1626 free(path);
1627 if (err)
1628 goto done;
1631 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1632 done:
1633 if (dir)
1634 closedir(dir);
1635 got_pathlist_free(&paths);
1636 return err;
1639 const struct got_error *
1640 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1641 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1642 struct got_repository *repo, got_repo_import_cb progress_cb,
1643 void *progress_arg)
1645 const struct got_error *err;
1646 struct got_object_id *new_tree_id;
1648 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1649 progress_cb, progress_arg);
1650 if (err)
1651 return err;
1653 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1654 author, time(NULL), author, time(NULL), logmsg, repo);
1655 free(new_tree_id);
1656 return err;