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 static const struct got_error *
157 get_path_gitconfig(char **p, struct got_repository *repo)
159 *p = get_path_git_child(repo, GOT_GITCONFIG);
160 if (*p == NULL)
161 return got_error_from_errno("asprintf");
162 return NULL;
165 void
166 got_repo_get_gitconfig_remotes(int *nremotes, struct got_remote_repo **remotes,
167 struct got_repository *repo)
169 *nremotes = repo->ngitconfig_remotes;
170 *remotes = repo->gitconfig_remotes;
173 static int
174 is_git_repo(struct got_repository *repo)
176 const char *path_git = got_repo_get_path_git_dir(repo);
177 char *path_objects = got_repo_get_path_objects(repo);
178 char *path_refs = got_repo_get_path_refs(repo);
179 char *path_head = get_path_head(repo);
180 int ret = 0;
181 struct stat sb;
182 struct got_reference *head_ref;
184 if (lstat(path_git, &sb) == -1)
185 goto done;
186 if (!S_ISDIR(sb.st_mode))
187 goto done;
189 if (lstat(path_objects, &sb) == -1)
190 goto done;
191 if (!S_ISDIR(sb.st_mode))
192 goto done;
194 if (lstat(path_refs, &sb) == -1)
195 goto done;
196 if (!S_ISDIR(sb.st_mode))
197 goto done;
199 if (lstat(path_head, &sb) == -1)
200 goto done;
201 if (!S_ISREG(sb.st_mode))
202 goto done;
204 /* Check if the HEAD reference can be opened. */
205 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
206 goto done;
207 got_ref_close(head_ref);
209 ret = 1;
210 done:
211 free(path_objects);
212 free(path_refs);
213 free(path_head);
214 return ret;
218 const struct got_error *
219 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
220 struct got_object *obj)
222 #ifndef GOT_NO_OBJ_CACHE
223 const struct got_error *err = NULL;
224 err = got_object_cache_add(&repo->objcache, id, obj);
225 if (err) {
226 if (err->code == GOT_ERR_OBJ_EXISTS ||
227 err->code == GOT_ERR_OBJ_TOO_LARGE)
228 err = NULL;
229 return err;
231 obj->refcnt++;
232 #endif
233 return NULL;
236 struct got_object *
237 got_repo_get_cached_object(struct got_repository *repo,
238 struct got_object_id *id)
240 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
243 const struct got_error *
244 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
245 struct got_tree_object *tree)
247 #ifndef GOT_NO_OBJ_CACHE
248 const struct got_error *err = NULL;
249 err = got_object_cache_add(&repo->treecache, id, tree);
250 if (err) {
251 if (err->code == GOT_ERR_OBJ_EXISTS ||
252 err->code == GOT_ERR_OBJ_TOO_LARGE)
253 err = NULL;
254 return err;
256 tree->refcnt++;
257 #endif
258 return NULL;
261 struct got_tree_object *
262 got_repo_get_cached_tree(struct got_repository *repo,
263 struct got_object_id *id)
265 return (struct got_tree_object *)got_object_cache_get(
266 &repo->treecache, id);
269 const struct got_error *
270 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
271 struct got_commit_object *commit)
273 #ifndef GOT_NO_OBJ_CACHE
274 const struct got_error *err = NULL;
275 err = got_object_cache_add(&repo->commitcache, id, commit);
276 if (err) {
277 if (err->code == GOT_ERR_OBJ_EXISTS ||
278 err->code == GOT_ERR_OBJ_TOO_LARGE)
279 err = NULL;
280 return err;
282 commit->refcnt++;
283 #endif
284 return NULL;
287 struct got_commit_object *
288 got_repo_get_cached_commit(struct got_repository *repo,
289 struct got_object_id *id)
291 return (struct got_commit_object *)got_object_cache_get(
292 &repo->commitcache, id);
295 const struct got_error *
296 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
297 struct got_tag_object *tag)
299 #ifndef GOT_NO_OBJ_CACHE
300 const struct got_error *err = NULL;
301 err = got_object_cache_add(&repo->tagcache, id, tag);
302 if (err) {
303 if (err->code == GOT_ERR_OBJ_EXISTS ||
304 err->code == GOT_ERR_OBJ_TOO_LARGE)
305 err = NULL;
306 return err;
308 tag->refcnt++;
309 #endif
310 return NULL;
313 struct got_tag_object *
314 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
316 return (struct got_tag_object *)got_object_cache_get(
317 &repo->tagcache, id);
320 const struct got_error *
321 open_repo(struct got_repository *repo, const char *path)
323 const struct got_error *err = NULL;
325 /* bare git repository? */
326 repo->path_git_dir = strdup(path);
327 if (repo->path_git_dir == NULL)
328 return got_error_from_errno("strdup");
329 if (is_git_repo(repo)) {
330 repo->path = strdup(repo->path_git_dir);
331 if (repo->path == NULL) {
332 err = got_error_from_errno("strdup");
333 goto done;
335 return NULL;
338 /* git repository with working tree? */
339 free(repo->path_git_dir);
340 repo->path_git_dir = NULL;
341 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
342 err = got_error_from_errno("asprintf");
343 goto done;
345 if (is_git_repo(repo)) {
346 repo->path = strdup(path);
347 if (repo->path == NULL) {
348 err = got_error_from_errno("strdup");
349 goto done;
351 return NULL;
354 err = got_error(GOT_ERR_NOT_GIT_REPO);
355 done:
356 if (err) {
357 free(repo->path);
358 repo->path = NULL;
359 free(repo->path_git_dir);
360 repo->path_git_dir = NULL;
362 return err;
365 static const struct got_error *
366 parse_gitconfig_file(int *gitconfig_repository_format_version,
367 char **gitconfig_author_name, char **gitconfig_author_email,
368 struct got_remote_repo **remotes, int *nremotes,
369 char **gitconfig_owner,
370 const char *gitconfig_path)
372 const struct got_error *err = NULL, *child_err = NULL;
373 int fd = -1;
374 int imsg_fds[2] = { -1, -1 };
375 pid_t pid;
376 struct imsgbuf *ibuf;
378 *gitconfig_repository_format_version = 0;
379 *gitconfig_author_name = NULL;
380 *gitconfig_author_email = NULL;
382 fd = open(gitconfig_path, O_RDONLY);
383 if (fd == -1) {
384 if (errno == ENOENT)
385 return NULL;
386 return got_error_from_errno2("open", gitconfig_path);
389 ibuf = calloc(1, sizeof(*ibuf));
390 if (ibuf == NULL) {
391 err = got_error_from_errno("calloc");
392 goto done;
395 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
396 err = got_error_from_errno("socketpair");
397 goto done;
400 pid = fork();
401 if (pid == -1) {
402 err = got_error_from_errno("fork");
403 goto done;
404 } else if (pid == 0) {
405 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
406 gitconfig_path);
407 /* not reached */
410 if (close(imsg_fds[1]) == -1) {
411 err = got_error_from_errno("close");
412 goto done;
414 imsg_fds[1] = -1;
415 imsg_init(ibuf, imsg_fds[0]);
417 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
418 if (err)
419 goto done;
420 fd = -1;
422 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
423 if (err)
424 goto done;
426 err = got_privsep_recv_gitconfig_int(
427 gitconfig_repository_format_version, ibuf);
428 if (err)
429 goto done;
431 err = got_privsep_send_gitconfig_author_name_req(ibuf);
432 if (err)
433 goto done;
435 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
436 if (err)
437 goto done;
439 err = got_privsep_send_gitconfig_author_email_req(ibuf);
440 if (err)
441 goto done;
443 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
444 if (err)
445 goto done;
447 if (remotes && nremotes) {
448 err = got_privsep_send_gitconfig_remotes_req(ibuf);
449 if (err)
450 goto done;
452 err = got_privsep_recv_gitconfig_remotes(remotes,
453 nremotes, ibuf);
454 if (err)
455 goto done;
458 if (gitconfig_owner) {
459 err = got_privsep_send_gitconfig_owner_req(ibuf);
460 if (err)
461 goto done;
462 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
463 if (err)
464 goto done;
467 imsg_clear(ibuf);
468 err = got_privsep_send_stop(imsg_fds[0]);
469 child_err = got_privsep_wait_for_child(pid);
470 if (child_err && err == NULL)
471 err = child_err;
472 done:
473 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
474 err = got_error_from_errno("close");
475 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
476 err = got_error_from_errno("close");
477 if (fd != -1 && close(fd) == -1 && err == NULL)
478 err = got_error_from_errno2("close", gitconfig_path);
479 free(ibuf);
480 return err;
483 static const struct got_error *
484 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
486 const struct got_error *err = NULL;
487 char *repo_gitconfig_path = NULL;
489 if (global_gitconfig_path) {
490 /* Read settings from ~/.gitconfig. */
491 int dummy_repo_version;
492 err = parse_gitconfig_file(&dummy_repo_version,
493 &repo->global_gitconfig_author_name,
494 &repo->global_gitconfig_author_email,
495 NULL, NULL, NULL, global_gitconfig_path);
496 if (err)
497 return err;
500 /* Read repository's .git/config file. */
501 err = get_path_gitconfig(&repo_gitconfig_path, repo);
502 if (err)
503 return err;
505 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
506 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
507 &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
508 &repo->gitconfig_owner, repo_gitconfig_path);
509 if (err)
510 goto done;
511 done:
512 free(repo_gitconfig_path);
513 return err;
516 const struct got_error *
517 got_repo_open(struct got_repository **repop, const char *path,
518 const char *global_gitconfig_path)
520 struct got_repository *repo = NULL;
521 const struct got_error *err = NULL;
522 char *abspath;
523 int i, tried_root = 0;
525 *repop = NULL;
527 if (got_path_is_absolute(path))
528 abspath = strdup(path);
529 else
530 abspath = got_path_get_absolute(path);
531 if (abspath == NULL)
532 return got_error_path(path, GOT_ERR_BAD_PATH);
534 repo = calloc(1, sizeof(*repo));
535 if (repo == NULL) {
536 err = got_error_from_errno("calloc");
537 goto done;
540 for (i = 0; i < nitems(repo->privsep_children); i++) {
541 memset(&repo->privsep_children[i], 0,
542 sizeof(repo->privsep_children[0]));
543 repo->privsep_children[i].imsg_fd = -1;
546 err = got_object_cache_init(&repo->objcache,
547 GOT_OBJECT_CACHE_TYPE_OBJ);
548 if (err)
549 goto done;
550 err = got_object_cache_init(&repo->treecache,
551 GOT_OBJECT_CACHE_TYPE_TREE);
552 if (err)
553 goto done;
554 err = got_object_cache_init(&repo->commitcache,
555 GOT_OBJECT_CACHE_TYPE_COMMIT);
556 if (err)
557 goto done;
558 err = got_object_cache_init(&repo->tagcache,
559 GOT_OBJECT_CACHE_TYPE_TAG);
560 if (err)
561 goto done;
563 path = realpath(abspath, NULL);
564 if (path == NULL) {
565 err = got_error_from_errno2("realpath", abspath);
566 goto done;
569 do {
570 err = open_repo(repo, path);
571 if (err == NULL)
572 break;
573 if (err->code != GOT_ERR_NOT_GIT_REPO)
574 break;
575 if (path[0] == '/' && path[1] == '\0') {
576 if (tried_root) {
577 err = got_error(GOT_ERR_NOT_GIT_REPO);
578 goto done;
580 tried_root = 1;
582 path = dirname(path);
583 if (path == NULL) {
584 err = got_error_from_errno2("dirname", path);
585 goto done;
587 } while (path);
589 err = read_gitconfig(repo, global_gitconfig_path);
590 if (err)
591 goto done;
592 if (repo->gitconfig_repository_format_version != 0)
593 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
594 done:
595 if (err)
596 got_repo_close(repo);
597 else
598 *repop = repo;
599 free(abspath);
600 return err;
603 const struct got_error *
604 got_repo_close(struct got_repository *repo)
606 const struct got_error *err = NULL, *child_err;
607 int i;
609 for (i = 0; i < nitems(repo->packidx_cache); i++) {
610 if (repo->packidx_cache[i] == NULL)
611 break;
612 got_packidx_close(repo->packidx_cache[i]);
615 for (i = 0; i < nitems(repo->packs); i++) {
616 if (repo->packs[i].path_packfile == NULL)
617 break;
618 got_pack_close(&repo->packs[i]);
621 free(repo->path);
622 free(repo->path_git_dir);
624 got_object_cache_close(&repo->objcache);
625 got_object_cache_close(&repo->treecache);
626 got_object_cache_close(&repo->commitcache);
627 got_object_cache_close(&repo->tagcache);
629 for (i = 0; i < nitems(repo->privsep_children); i++) {
630 if (repo->privsep_children[i].imsg_fd == -1)
631 continue;
632 imsg_clear(repo->privsep_children[i].ibuf);
633 free(repo->privsep_children[i].ibuf);
634 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
635 child_err = got_privsep_wait_for_child(
636 repo->privsep_children[i].pid);
637 if (child_err && err == NULL)
638 err = child_err;
639 if (close(repo->privsep_children[i].imsg_fd) != 0 &&
640 err == NULL)
641 err = got_error_from_errno("close");
644 free(repo->gitconfig_author_name);
645 free(repo->gitconfig_author_email);
646 for (i = 0; i < repo->ngitconfig_remotes; i++) {
647 free(repo->gitconfig_remotes[i].name);
648 free(repo->gitconfig_remotes[i].url);
650 free(repo->gitconfig_remotes);
651 free(repo);
653 return err;
656 const struct got_error *
657 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
658 const char *input_path, int check_disk)
660 const struct got_error *err = NULL;
661 const char *repo_abspath = NULL;
662 size_t repolen, len;
663 char *canonpath, *path = NULL;
665 *in_repo_path = NULL;
667 canonpath = strdup(input_path);
668 if (canonpath == NULL) {
669 err = got_error_from_errno("strdup");
670 goto done;
672 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
673 if (err)
674 goto done;
676 repo_abspath = got_repo_get_path(repo);
678 if (!check_disk || canonpath[0] == '\0') {
679 path = strdup(canonpath);
680 if (path == NULL) {
681 err = got_error_from_errno("strdup");
682 goto done;
684 } else {
685 path = realpath(canonpath, NULL);
686 if (path == NULL) {
687 if (errno != ENOENT) {
688 err = got_error_from_errno2("realpath",
689 canonpath);
690 goto done;
692 /*
693 * Path is not on disk.
694 * Assume it is already relative to repository root.
695 */
696 path = strdup(canonpath);
697 if (path == NULL) {
698 err = got_error_from_errno("strdup");
699 goto done;
703 repolen = strlen(repo_abspath);
704 len = strlen(path);
707 if (strcmp(path, repo_abspath) == 0) {
708 free(path);
709 path = strdup("");
710 if (path == NULL) {
711 err = got_error_from_errno("strdup");
712 goto done;
714 } else if (len > repolen &&
715 got_path_is_child(path, repo_abspath, repolen)) {
716 /* Matched an on-disk path inside repository. */
717 if (got_repo_is_bare(repo)) {
718 /*
719 * Matched an on-disk path inside repository
720 * database. Treat as repository-relative.
721 */
722 } else {
723 char *child;
724 /* Strip common prefix with repository path. */
725 err = got_path_skip_common_ancestor(&child,
726 repo_abspath, path);
727 if (err)
728 goto done;
729 free(path);
730 path = child;
732 } else {
733 /*
734 * Matched unrelated on-disk path.
735 * Treat it as repository-relative.
736 */
740 /* Make in-repository path absolute */
741 if (path[0] != '/') {
742 char *abspath;
743 if (asprintf(&abspath, "/%s", path) == -1) {
744 err = got_error_from_errno("asprintf");
745 goto done;
747 free(path);
748 path = abspath;
751 done:
752 free(canonpath);
753 if (err)
754 free(path);
755 else
756 *in_repo_path = path;
757 return err;
760 static const struct got_error *
761 cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
762 const char *path_packidx)
764 const struct got_error *err = NULL;
765 int i;
767 for (i = 0; i < nitems(repo->packidx_cache); i++) {
768 if (repo->packidx_cache[i] == NULL)
769 break;
770 if (strcmp(repo->packidx_cache[i]->path_packidx,
771 path_packidx) == 0) {
772 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
775 if (i == nitems(repo->packidx_cache)) {
776 err = got_packidx_close(repo->packidx_cache[i - 1]);
777 if (err)
778 return err;
781 /*
782 * Insert the new pack index at the front so it will
783 * be searched first in the future.
784 */
785 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
786 sizeof(repo->packidx_cache) -
787 sizeof(repo->packidx_cache[0]));
788 repo->packidx_cache[0] = packidx;
790 return NULL;
793 static int
794 is_packidx_filename(const char *name, size_t len)
796 if (len != GOT_PACKIDX_NAMELEN)
797 return 0;
799 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
800 return 0;
802 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
803 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
804 return 0;
806 return 1;
809 const struct got_error *
810 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
811 struct got_repository *repo, struct got_object_id *id)
813 const struct got_error *err;
814 char *path_packdir;
815 DIR *packdir;
816 struct dirent *dent;
817 char *path_packidx;
818 int i;
820 /* Search pack index cache. */
821 for (i = 0; i < nitems(repo->packidx_cache); i++) {
822 if (repo->packidx_cache[i] == NULL)
823 break;
824 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
825 if (*idx != -1) {
826 *packidx = repo->packidx_cache[i];
827 /*
828 * Move this cache entry to the front. Repeatedly
829 * searching a wrong pack index can be expensive.
830 */
831 if (i > 0) {
832 struct got_packidx *p;
833 p = repo->packidx_cache[0];
834 repo->packidx_cache[0] = *packidx;
835 repo->packidx_cache[i] = p;
837 return NULL;
840 /* No luck. Search the filesystem. */
842 path_packdir = got_repo_get_path_objects_pack(repo);
843 if (path_packdir == NULL)
844 return got_error_from_errno("got_repo_get_path_objects_pack");
846 packdir = opendir(path_packdir);
847 if (packdir == NULL) {
848 if (errno == ENOENT)
849 err = got_error_no_obj(id);
850 else
851 err = got_error_from_errno2("opendir", path_packdir);
852 goto done;
855 while ((dent = readdir(packdir)) != NULL) {
856 int is_cached = 0;
858 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
859 continue;
861 if (asprintf(&path_packidx, "%s/%s", path_packdir,
862 dent->d_name) == -1) {
863 err = got_error_from_errno("asprintf");
864 goto done;
867 for (i = 0; i < nitems(repo->packidx_cache); i++) {
868 if (repo->packidx_cache[i] == NULL)
869 break;
870 if (strcmp(repo->packidx_cache[i]->path_packidx,
871 path_packidx) == 0) {
872 is_cached = 1;
873 break;
876 if (is_cached) {
877 free(path_packidx);
878 continue; /* already searched */
881 err = got_packidx_open(packidx, path_packidx, 0);
882 if (err) {
883 free(path_packidx);
884 goto done;
887 err = cache_packidx(repo, *packidx, path_packidx);
888 free(path_packidx);
889 if (err)
890 goto done;
892 *idx = got_packidx_get_object_idx(*packidx, id);
893 if (*idx != -1) {
894 err = NULL; /* found the object */
895 goto done;
899 err = got_error_no_obj(id);
900 done:
901 free(path_packdir);
902 if (packdir && closedir(packdir) != 0 && err == NULL)
903 err = got_error_from_errno("closedir");
904 return err;
907 static const struct got_error *
908 read_packfile_hdr(int fd, struct got_packidx *packidx)
910 const struct got_error *err = NULL;
911 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
912 struct got_packfile_hdr hdr;
913 ssize_t n;
915 n = read(fd, &hdr, sizeof(hdr));
916 if (n < 0)
917 return got_error_from_errno("read");
918 if (n != sizeof(hdr))
919 return got_error(GOT_ERR_BAD_PACKFILE);
921 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
922 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
923 betoh32(hdr.nobjects) != totobj)
924 err = got_error(GOT_ERR_BAD_PACKFILE);
926 return err;
929 static const struct got_error *
930 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
932 const struct got_error *err = NULL;
934 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
935 if (*fd == -1)
936 return got_error_from_errno2("open", path_packfile);
938 if (packidx) {
939 err = read_packfile_hdr(*fd, packidx);
940 if (err) {
941 close(*fd);
942 *fd = -1;
946 return err;
949 const struct got_error *
950 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
951 const char *path_packfile, struct got_packidx *packidx)
953 const struct got_error *err = NULL;
954 struct got_pack *pack = NULL;
955 struct stat sb;
956 int i;
958 if (packp)
959 *packp = NULL;
961 for (i = 0; i < nitems(repo->packs); i++) {
962 pack = &repo->packs[i];
963 if (pack->path_packfile == NULL)
964 break;
965 if (strcmp(pack->path_packfile, path_packfile) == 0)
966 return got_error(GOT_ERR_CACHE_DUP_ENTRY);
969 if (i == nitems(repo->packs) - 1) {
970 err = got_pack_close(&repo->packs[i - 1]);
971 if (err)
972 return err;
973 memmove(&repo->packs[1], &repo->packs[0],
974 sizeof(repo->packs) - sizeof(repo->packs[0]));
975 i = 0;
978 pack = &repo->packs[i];
980 pack->path_packfile = strdup(path_packfile);
981 if (pack->path_packfile == NULL) {
982 err = got_error_from_errno("strdup");
983 goto done;
986 err = open_packfile(&pack->fd, path_packfile, packidx);
987 if (err)
988 goto done;
990 if (fstat(pack->fd, &sb) != 0) {
991 err = got_error_from_errno("fstat");
992 goto done;
994 pack->filesize = sb.st_size;
996 pack->privsep_child = NULL;
998 #ifndef GOT_PACK_NO_MMAP
999 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1000 pack->fd, 0);
1001 if (pack->map == MAP_FAILED) {
1002 if (errno != ENOMEM) {
1003 err = got_error_from_errno("mmap");
1004 goto done;
1006 pack->map = NULL; /* fall back to read(2) */
1008 #endif
1009 done:
1010 if (err) {
1011 if (pack) {
1012 free(pack->path_packfile);
1013 memset(pack, 0, sizeof(*pack));
1015 } else if (packp)
1016 *packp = pack;
1017 return err;
1020 struct got_pack *
1021 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1023 struct got_pack *pack = NULL;
1024 int i;
1026 for (i = 0; i < nitems(repo->packs); i++) {
1027 pack = &repo->packs[i];
1028 if (pack->path_packfile == NULL)
1029 break;
1030 if (strcmp(pack->path_packfile, path_packfile) == 0)
1031 return pack;
1034 return NULL;
1037 const struct got_error *
1038 got_repo_init(const char *repo_path)
1040 const struct got_error *err = NULL;
1041 const char *dirnames[] = {
1042 GOT_OBJECTS_DIR,
1043 GOT_OBJECTS_PACK_DIR,
1044 GOT_REFS_DIR,
1046 const char *description_str = "Unnamed repository; "
1047 "edit this file 'description' to name the repository.";
1048 const char *headref_str = "ref: refs/heads/main";
1049 const char *gitconfig_str = "[core]\n"
1050 "\trepositoryformatversion = 0\n"
1051 "\tfilemode = true\n"
1052 "\tbare = true\n";
1053 char *path;
1054 int i;
1056 if (!got_path_dir_is_empty(repo_path))
1057 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1059 for (i = 0; i < nitems(dirnames); i++) {
1060 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1061 return got_error_from_errno("asprintf");
1063 err = got_path_mkdir(path);
1064 free(path);
1065 if (err)
1066 return err;
1069 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1070 return got_error_from_errno("asprintf");
1071 err = got_path_create_file(path, description_str);
1072 free(path);
1073 if (err)
1074 return err;
1076 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1077 return got_error_from_errno("asprintf");
1078 err = got_path_create_file(path, headref_str);
1079 free(path);
1080 if (err)
1081 return err;
1083 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1084 return got_error_from_errno("asprintf");
1085 err = got_path_create_file(path, gitconfig_str);
1086 free(path);
1087 if (err)
1088 return err;
1090 return NULL;
1093 static const struct got_error *
1094 match_packed_object(struct got_object_id **unique_id,
1095 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1097 const struct got_error *err = NULL;
1098 char *path_packdir;
1099 DIR *packdir;
1100 struct dirent *dent;
1101 char *path_packidx;
1102 struct got_object_id_queue matched_ids;
1104 SIMPLEQ_INIT(&matched_ids);
1106 path_packdir = got_repo_get_path_objects_pack(repo);
1107 if (path_packdir == NULL)
1108 return got_error_from_errno("got_repo_get_path_objects_pack");
1110 packdir = opendir(path_packdir);
1111 if (packdir == NULL) {
1112 if (errno != ENOENT)
1113 err = got_error_from_errno2("opendir", path_packdir);
1114 goto done;
1117 while ((dent = readdir(packdir)) != NULL) {
1118 struct got_packidx *packidx;
1119 struct got_object_qid *qid;
1122 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1123 continue;
1125 if (asprintf(&path_packidx, "%s/%s", path_packdir,
1126 dent->d_name) == -1) {
1127 err = got_error_from_errno("asprintf");
1128 break;
1131 err = got_packidx_open(&packidx, path_packidx, 0);
1132 free(path_packidx);
1133 if (err)
1134 break;
1136 err = got_packidx_match_id_str_prefix(&matched_ids,
1137 packidx, id_str_prefix);
1138 if (err) {
1139 got_packidx_close(packidx);
1140 break;
1142 err = got_packidx_close(packidx);
1143 if (err)
1144 break;
1146 SIMPLEQ_FOREACH(qid, &matched_ids, entry) {
1147 if (obj_type != GOT_OBJ_TYPE_ANY) {
1148 int matched_type;
1149 err = got_object_get_type(&matched_type, repo,
1150 qid->id);
1151 if (err)
1152 goto done;
1153 if (matched_type != obj_type)
1154 continue;
1156 if (*unique_id == NULL) {
1157 *unique_id = got_object_id_dup(qid->id);
1158 if (*unique_id == NULL) {
1159 err = got_error_from_errno("malloc");
1160 goto done;
1162 } else {
1163 if (got_object_id_cmp(*unique_id, qid->id) == 0)
1164 continue; /* packed multiple times */
1165 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1166 goto done;
1170 done:
1171 got_object_id_queue_free(&matched_ids);
1172 free(path_packdir);
1173 if (packdir && closedir(packdir) != 0 && err == NULL)
1174 err = got_error_from_errno("closedir");
1175 if (err) {
1176 free(*unique_id);
1177 *unique_id = NULL;
1179 return err;
1182 static const struct got_error *
1183 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1184 const char *object_dir, const char *id_str_prefix, int obj_type,
1185 struct got_repository *repo)
1187 const struct got_error *err = NULL;
1188 char *path;
1189 DIR *dir = NULL;
1190 struct dirent *dent;
1191 struct got_object_id id;
1193 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1194 err = got_error_from_errno("asprintf");
1195 goto done;
1198 dir = opendir(path);
1199 if (dir == NULL) {
1200 if (errno == ENOENT) {
1201 err = NULL;
1202 goto done;
1204 err = got_error_from_errno2("opendir", path);
1205 goto done;
1207 while ((dent = readdir(dir)) != NULL) {
1208 char *id_str;
1209 int cmp;
1211 if (strcmp(dent->d_name, ".") == 0 ||
1212 strcmp(dent->d_name, "..") == 0)
1213 continue;
1215 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1216 err = got_error_from_errno("asprintf");
1217 goto done;
1220 if (!got_parse_sha1_digest(id.sha1, id_str))
1221 continue;
1224 * Directory entries do not necessarily appear in
1225 * sorted order, so we must iterate over all of them.
1227 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1228 if (cmp != 0) {
1229 free(id_str);
1230 continue;
1233 if (*unique_id == NULL) {
1234 if (obj_type != GOT_OBJ_TYPE_ANY) {
1235 int matched_type;
1236 err = got_object_get_type(&matched_type, repo,
1237 &id);
1238 if (err)
1239 goto done;
1240 if (matched_type != obj_type)
1241 continue;
1243 *unique_id = got_object_id_dup(&id);
1244 if (*unique_id == NULL) {
1245 err = got_error_from_errno("got_object_id_dup");
1246 free(id_str);
1247 goto done;
1249 } else {
1250 if (got_object_id_cmp(*unique_id, &id) == 0)
1251 continue; /* both packed and loose */
1252 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1253 free(id_str);
1254 goto done;
1257 done:
1258 if (dir && closedir(dir) != 0 && err == NULL)
1259 err = got_error_from_errno("closedir");
1260 if (err) {
1261 free(*unique_id);
1262 *unique_id = NULL;
1264 free(path);
1265 return err;
1268 const struct got_error *
1269 got_repo_match_object_id_prefix(struct got_object_id **id,
1270 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1272 const struct got_error *err = NULL;
1273 char *path_objects = got_repo_get_path_objects(repo);
1274 char *object_dir = NULL;
1275 size_t len;
1276 int i;
1278 *id = NULL;
1280 for (i = 0; i < strlen(id_str_prefix); i++) {
1281 if (isxdigit((unsigned char)id_str_prefix[i]))
1282 continue;
1283 return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1286 len = strlen(id_str_prefix);
1287 if (len >= 2) {
1288 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1289 if (err)
1290 goto done;
1291 object_dir = strndup(id_str_prefix, 2);
1292 if (object_dir == NULL) {
1293 err = got_error_from_errno("strdup");
1294 goto done;
1296 err = match_loose_object(id, path_objects, object_dir,
1297 id_str_prefix, obj_type, repo);
1298 } else if (len == 1) {
1299 int i;
1300 for (i = 0; i < 0xf; i++) {
1301 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1302 == -1) {
1303 err = got_error_from_errno("asprintf");
1304 goto done;
1306 err = match_packed_object(id, repo, object_dir,
1307 obj_type);
1308 if (err)
1309 goto done;
1310 err = match_loose_object(id, path_objects, object_dir,
1311 id_str_prefix, obj_type, repo);
1312 if (err)
1313 goto done;
1315 } else {
1316 err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1317 goto done;
1319 done:
1320 free(object_dir);
1321 if (err) {
1322 free(*id);
1323 *id = NULL;
1324 } else if (*id == NULL)
1325 err = got_error(GOT_ERR_NO_OBJ);
1327 return err;
1330 const struct got_error *
1331 got_repo_match_object_id(struct got_object_id **id, char **label,
1332 const char *id_str, int obj_type, int resolve_tags,
1333 struct got_repository *repo)
1335 const struct got_error *err;
1336 struct got_tag_object *tag;
1337 struct got_reference *ref = NULL;
1339 *id = NULL;
1340 if (label)
1341 *label = NULL;
1343 if (resolve_tags) {
1344 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
1345 repo);
1346 if (err == NULL) {
1347 *id = got_object_id_dup(
1348 got_object_tag_get_object_id(tag));
1349 if (*id == NULL)
1350 err = got_error_from_errno("got_object_id_dup");
1351 else if (label && asprintf(label, "refs/tags/%s",
1352 got_object_tag_get_name(tag)) == -1) {
1353 err = got_error_from_errno("asprintf");
1354 free(*id);
1355 *id = NULL;
1357 got_object_tag_close(tag);
1358 return err;
1359 } else if (err->code != GOT_ERR_OBJ_TYPE &&
1360 err->code != GOT_ERR_NO_OBJ)
1361 return err;
1364 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1365 if (err) {
1366 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1367 return err;
1368 err = got_ref_open(&ref, repo, id_str, 0);
1369 if (err != NULL)
1370 goto done;
1371 if (label) {
1372 *label = strdup(got_ref_get_name(ref));
1373 if (*label == NULL) {
1374 err = got_error_from_errno("strdup");
1375 goto done;
1378 err = got_ref_resolve(id, repo, ref);
1379 } else if (label) {
1380 err = got_object_id_str(label, *id);
1381 if (*label == NULL) {
1382 err = got_error_from_errno("strdup");
1383 goto done;
1386 done:
1387 if (ref)
1388 got_ref_close(ref);
1389 return err;
1392 const struct got_error *
1393 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1394 int obj_type, struct got_repository *repo)
1396 const struct got_error *err;
1397 struct got_reflist_head refs;
1398 struct got_reflist_entry *re;
1399 struct got_object_id *tag_id;
1401 SIMPLEQ_INIT(&refs);
1402 *tag = NULL;
1404 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_by_name, NULL);
1405 if (err)
1406 return err;
1408 SIMPLEQ_FOREACH(re, &refs, entry) {
1409 const char *refname;
1410 refname = got_ref_get_name(re->ref);
1411 if (got_ref_is_symbolic(re->ref))
1412 continue;
1413 refname += strlen("refs/tags/");
1414 if (strcmp(refname, name) != 0)
1415 continue;
1416 err = got_ref_resolve(&tag_id, repo, re->ref);
1417 if (err)
1418 break;
1419 err = got_object_open_as_tag(tag, repo, tag_id);
1420 free(tag_id);
1421 if (err)
1422 break;
1423 if (obj_type == GOT_OBJ_TYPE_ANY ||
1424 got_object_tag_get_object_type(*tag) == obj_type)
1425 break;
1426 got_object_tag_close(*tag);
1427 *tag = NULL;
1430 got_ref_list_free(&refs);
1431 if (err == NULL && *tag == NULL)
1432 err = got_error(GOT_ERR_NO_OBJ);
1433 return err;
1436 static const struct got_error *
1437 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1438 const char *name, mode_t mode, struct got_object_id *blob_id)
1440 const struct got_error *err = NULL;
1442 *new_te = NULL;
1444 *new_te = calloc(1, sizeof(**new_te));
1445 if (*new_te == NULL)
1446 return got_error_from_errno("calloc");
1448 if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1449 sizeof((*new_te)->name)) {
1450 err = got_error(GOT_ERR_NO_SPACE);
1451 goto done;
1454 (*new_te)->mode = S_IFREG | (mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
1455 memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1456 done:
1457 if (err && *new_te) {
1458 free(*new_te);
1459 *new_te = NULL;
1461 return err;
1464 static const struct got_error *
1465 import_file(struct got_tree_entry **new_te, struct dirent *de,
1466 const char *path, struct got_repository *repo)
1468 const struct got_error *err;
1469 struct got_object_id *blob_id = NULL;
1470 char *filepath;
1471 struct stat sb;
1473 if (asprintf(&filepath, "%s%s%s", path,
1474 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1475 return got_error_from_errno("asprintf");
1477 if (lstat(filepath, &sb) != 0) {
1478 err = got_error_from_errno2("lstat", path);
1479 goto done;
1482 err = got_object_blob_create(&blob_id, filepath, repo);
1483 if (err)
1484 goto done;
1486 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1487 blob_id);
1488 done:
1489 free(filepath);
1490 if (err)
1491 free(blob_id);
1492 return err;
1495 static const struct got_error *
1496 insert_tree_entry(struct got_tree_entry *new_te,
1497 struct got_pathlist_head *paths)
1499 const struct got_error *err = NULL;
1500 struct got_pathlist_entry *new_pe;
1502 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1503 if (err)
1504 return err;
1505 if (new_pe == NULL)
1506 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1507 return NULL;
1510 static const struct got_error *write_tree(struct got_object_id **,
1511 const char *, struct got_pathlist_head *, struct got_repository *,
1512 got_repo_import_cb progress_cb, void *progress_arg);
1514 static const struct got_error *
1515 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1516 const char *path, struct got_pathlist_head *ignores,
1517 struct got_repository *repo,
1518 got_repo_import_cb progress_cb, void *progress_arg)
1520 const struct got_error *err;
1521 struct got_object_id *id = NULL;
1522 char *subdirpath;
1524 if (asprintf(&subdirpath, "%s%s%s", path,
1525 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1526 return got_error_from_errno("asprintf");
1528 (*new_te) = calloc(1, sizeof(**new_te));
1529 if (*new_te == NULL)
1530 return got_error_from_errno("calloc");
1531 (*new_te)->mode = S_IFDIR;
1532 if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1533 sizeof((*new_te)->name)) {
1534 err = got_error(GOT_ERR_NO_SPACE);
1535 goto done;
1537 err = write_tree(&id, subdirpath, ignores, repo,
1538 progress_cb, progress_arg);
1539 if (err)
1540 goto done;
1541 memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1543 done:
1544 free(id);
1545 free(subdirpath);
1546 if (err) {
1547 free(*new_te);
1548 *new_te = NULL;
1550 return err;
1553 static const struct got_error *
1554 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1555 struct got_pathlist_head *ignores, struct got_repository *repo,
1556 got_repo_import_cb progress_cb, void *progress_arg)
1558 const struct got_error *err = NULL;
1559 DIR *dir;
1560 struct dirent *de;
1561 int nentries;
1562 struct got_tree_entry *new_te = NULL;
1563 struct got_pathlist_head paths;
1564 struct got_pathlist_entry *pe;
1566 *new_tree_id = NULL;
1568 TAILQ_INIT(&paths);
1570 dir = opendir(path_dir);
1571 if (dir == NULL) {
1572 err = got_error_from_errno2("opendir", path_dir);
1573 goto done;
1576 nentries = 0;
1577 while ((de = readdir(dir)) != NULL) {
1578 int ignore = 0;
1580 if (strcmp(de->d_name, ".") == 0 ||
1581 strcmp(de->d_name, "..") == 0)
1582 continue;
1584 TAILQ_FOREACH(pe, ignores, entry) {
1585 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1586 ignore = 1;
1587 break;
1590 if (ignore)
1591 continue;
1592 if (de->d_type == DT_DIR) {
1593 err = import_subdir(&new_te, de, path_dir,
1594 ignores, repo, progress_cb, progress_arg);
1595 if (err) {
1596 if (err->code != GOT_ERR_NO_TREE_ENTRY)
1597 goto done;
1598 err = NULL;
1599 continue;
1601 } else if (de->d_type == DT_REG) {
1602 err = import_file(&new_te, de, path_dir, repo);
1603 if (err)
1604 goto done;
1605 } else
1606 continue;
1608 err = insert_tree_entry(new_te, &paths);
1609 if (err)
1610 goto done;
1611 nentries++;
1614 if (TAILQ_EMPTY(&paths)) {
1615 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1616 goto done;
1619 TAILQ_FOREACH(pe, &paths, entry) {
1620 struct got_tree_entry *te = pe->data;
1621 char *path;
1622 if (!S_ISREG(te->mode))
1623 continue;
1624 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1625 err = got_error_from_errno("asprintf");
1626 goto done;
1628 err = (*progress_cb)(progress_arg, path);
1629 free(path);
1630 if (err)
1631 goto done;
1634 err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1635 done:
1636 if (dir)
1637 closedir(dir);
1638 got_pathlist_free(&paths);
1639 return err;
1642 const struct got_error *
1643 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1644 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1645 struct got_repository *repo, got_repo_import_cb progress_cb,
1646 void *progress_arg)
1648 const struct got_error *err;
1649 struct got_object_id *new_tree_id;
1651 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1652 progress_cb, progress_arg);
1653 if (err)
1654 return err;
1656 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1657 author, time(NULL), author, time(NULL), logmsg, repo);
1658 free(new_tree_id);
1659 return err;