Blob


1 /*
2 * Copyright (c) 2018, 2019 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/stat.h>
21 #include <sys/mman.h>
22 #include <sys/syslimits.h>
24 #include <ctype.h>
25 #include <fcntl.h>
26 #include <fnmatch.h>
27 #include <limits.h>
28 #include <dirent.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <sha1.h>
32 #include <string.h>
33 #include <zlib.h>
34 #include <errno.h>
35 #include <libgen.h>
36 #include <stdint.h>
37 #include <imsg.h>
38 #include <uuid.h>
40 #include "got_error.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
43 #include "got_path.h"
44 #include "got_worktree.h"
45 #include "got_object.h"
47 #include "got_lib_delta.h"
48 #include "got_lib_inflate.h"
49 #include "got_lib_object.h"
50 #include "got_lib_object_parse.h"
51 #include "got_lib_object_create.h"
52 #include "got_lib_pack.h"
53 #include "got_lib_privsep.h"
54 #include "got_lib_worktree.h"
55 #include "got_lib_sha1.h"
56 #include "got_lib_object_cache.h"
57 #include "got_lib_repository.h"
59 #ifndef nitems
60 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
61 #endif
63 #define GOT_GIT_DIR ".git"
65 /* Mandatory files and directories inside the git directory. */
66 #define GOT_OBJECTS_DIR "objects"
67 #define GOT_REFS_DIR "refs"
68 #define GOT_HEAD_FILE "HEAD"
70 /* Other files and directories inside the git directory. */
71 #define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
72 #define GOT_ORIG_HEAD_FILE "ORIG_HEAD"
73 #define GOT_OBJECTS_PACK_DIR "objects/pack"
74 #define GOT_PACKED_REFS_FILE "packed-refs"
76 const char *
77 got_repo_get_path(struct got_repository *repo)
78 {
79 return repo->path;
80 }
82 const char *
83 got_repo_get_path_git_dir(struct got_repository *repo)
84 {
85 return repo->path_git_dir;
86 }
88 int
89 got_repo_is_bare(struct got_repository *repo)
90 {
91 return (strcmp(repo->path, repo->path_git_dir) == 0);
92 }
94 static char *
95 get_path_git_child(struct got_repository *repo, const char *basename)
96 {
97 char *path_child;
99 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
100 basename) == -1)
101 return NULL;
103 return path_child;
106 char *
107 got_repo_get_path_objects(struct got_repository *repo)
109 return get_path_git_child(repo, GOT_OBJECTS_DIR);
112 char *
113 got_repo_get_path_objects_pack(struct got_repository *repo)
115 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
118 char *
119 got_repo_get_path_refs(struct got_repository *repo)
121 return get_path_git_child(repo, GOT_REFS_DIR);
124 char *
125 got_repo_get_path_packed_refs(struct got_repository *repo)
127 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
130 static char *
131 get_path_head(struct got_repository *repo)
133 return get_path_git_child(repo, GOT_HEAD_FILE);
136 static int
137 is_git_repo(struct got_repository *repo)
139 const char *path_git = got_repo_get_path_git_dir(repo);
140 char *path_objects = got_repo_get_path_objects(repo);
141 char *path_refs = got_repo_get_path_refs(repo);
142 char *path_head = get_path_head(repo);
143 int ret = 0;
144 struct stat sb;
145 struct got_reference *head_ref;
147 if (lstat(path_git, &sb) == -1)
148 goto done;
149 if (!S_ISDIR(sb.st_mode))
150 goto done;
152 if (lstat(path_objects, &sb) == -1)
153 goto done;
154 if (!S_ISDIR(sb.st_mode))
155 goto done;
157 if (lstat(path_refs, &sb) == -1)
158 goto done;
159 if (!S_ISDIR(sb.st_mode))
160 goto done;
162 if (lstat(path_head, &sb) == -1)
163 goto done;
164 if (!S_ISREG(sb.st_mode))
165 goto done;
167 /* Check if the HEAD reference can be opened. */
168 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
169 goto done;
170 got_ref_close(head_ref);
172 ret = 1;
173 done:
174 free(path_objects);
175 free(path_refs);
176 free(path_head);
177 return ret;
181 const struct got_error *
182 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
183 struct got_object *obj)
185 #ifndef GOT_NO_OBJ_CACHE
186 const struct got_error *err = NULL;
187 err = got_object_cache_add(&repo->objcache, id, obj);
188 if (err) {
189 if (err->code == GOT_ERR_OBJ_EXISTS ||
190 err->code == GOT_ERR_OBJ_TOO_LARGE)
191 err = NULL;
192 return err;
194 obj->refcnt++;
195 #endif
196 return NULL;
199 struct got_object *
200 got_repo_get_cached_object(struct got_repository *repo,
201 struct got_object_id *id)
203 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
206 const struct got_error *
207 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
208 struct got_tree_object *tree)
210 #ifndef GOT_NO_OBJ_CACHE
211 const struct got_error *err = NULL;
212 err = got_object_cache_add(&repo->treecache, id, tree);
213 if (err) {
214 if (err->code == GOT_ERR_OBJ_EXISTS ||
215 err->code == GOT_ERR_OBJ_TOO_LARGE)
216 err = NULL;
217 return err;
219 tree->refcnt++;
220 #endif
221 return NULL;
224 struct got_tree_object *
225 got_repo_get_cached_tree(struct got_repository *repo,
226 struct got_object_id *id)
228 return (struct got_tree_object *)got_object_cache_get(
229 &repo->treecache, id);
232 const struct got_error *
233 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
234 struct got_commit_object *commit)
236 #ifndef GOT_NO_OBJ_CACHE
237 const struct got_error *err = NULL;
238 err = got_object_cache_add(&repo->commitcache, id, commit);
239 if (err) {
240 if (err->code == GOT_ERR_OBJ_EXISTS ||
241 err->code == GOT_ERR_OBJ_TOO_LARGE)
242 err = NULL;
243 return err;
245 commit->refcnt++;
246 #endif
247 return NULL;
250 struct got_commit_object *
251 got_repo_get_cached_commit(struct got_repository *repo,
252 struct got_object_id *id)
254 return (struct got_commit_object *)got_object_cache_get(
255 &repo->commitcache, id);
258 const struct got_error *
259 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
260 struct got_tag_object *tag)
262 #ifndef GOT_NO_OBJ_CACHE
263 const struct got_error *err = NULL;
264 err = got_object_cache_add(&repo->tagcache, id, tag);
265 if (err) {
266 if (err->code == GOT_ERR_OBJ_EXISTS ||
267 err->code == GOT_ERR_OBJ_TOO_LARGE)
268 err = NULL;
269 return err;
271 tag->refcnt++;
272 #endif
273 return NULL;
276 struct got_tag_object *
277 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
279 return (struct got_tag_object *)got_object_cache_get(
280 &repo->tagcache, id);
283 const struct got_error *
284 open_repo(struct got_repository *repo, const char *path)
286 const struct got_error *err = NULL;
288 /* bare git repository? */
289 repo->path_git_dir = strdup(path);
290 if (repo->path_git_dir == NULL)
291 return got_error_from_errno("strdup");
292 if (is_git_repo(repo)) {
293 repo->path = strdup(repo->path_git_dir);
294 if (repo->path == NULL) {
295 err = got_error_from_errno("strdup");
296 goto done;
298 return NULL;
301 /* git repository with working tree? */
302 free(repo->path_git_dir);
303 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
304 err = got_error_from_errno("asprintf");
305 goto done;
307 if (is_git_repo(repo)) {
308 repo->path = strdup(path);
309 if (repo->path == NULL) {
310 err = got_error_from_errno("strdup");
311 goto done;
313 return NULL;
316 err = got_error(GOT_ERR_NOT_GIT_REPO);
317 done:
318 if (err) {
319 free(repo->path);
320 repo->path = NULL;
321 free(repo->path_git_dir);
322 repo->path_git_dir = NULL;
324 return err;
327 const struct got_error *
328 got_repo_open(struct got_repository **repop, const char *path)
330 struct got_repository *repo = NULL;
331 const struct got_error *err = NULL;
332 char *abspath;
333 int i, tried_root = 0;
335 *repop = NULL;
337 if (got_path_is_absolute(path))
338 abspath = strdup(path);
339 else
340 abspath = got_path_get_absolute(path);
341 if (abspath == NULL)
342 return got_error(GOT_ERR_BAD_PATH);
344 repo = calloc(1, sizeof(*repo));
345 if (repo == NULL) {
346 err = got_error_from_errno("calloc");
347 goto done;
350 for (i = 0; i < nitems(repo->privsep_children); i++) {
351 memset(&repo->privsep_children[i], 0,
352 sizeof(repo->privsep_children[0]));
353 repo->privsep_children[i].imsg_fd = -1;
356 err = got_object_cache_init(&repo->objcache,
357 GOT_OBJECT_CACHE_TYPE_OBJ);
358 if (err)
359 goto done;
360 err = got_object_cache_init(&repo->treecache,
361 GOT_OBJECT_CACHE_TYPE_TREE);
362 if (err)
363 goto done;
364 err = got_object_cache_init(&repo->commitcache,
365 GOT_OBJECT_CACHE_TYPE_COMMIT);
366 if (err)
367 goto done;
368 err = got_object_cache_init(&repo->tagcache,
369 GOT_OBJECT_CACHE_TYPE_TAG);
370 if (err)
371 goto done;
373 path = realpath(abspath, NULL);
374 if (path == NULL) {
375 err = got_error_from_errno2("realpath", abspath);
376 goto done;
379 do {
380 err = open_repo(repo, path);
381 if (err == NULL)
382 break;
383 if (err->code != GOT_ERR_NOT_GIT_REPO)
384 break;
385 if (path[0] == '/' && path[1] == '\0') {
386 if (tried_root) {
387 err = got_error(GOT_ERR_NOT_GIT_REPO);
388 break;
390 tried_root = 1;
392 path = dirname(path);
393 if (path == NULL)
394 err = got_error_from_errno2("dirname", path);
395 } while (path);
396 done:
397 if (err)
398 got_repo_close(repo);
399 else
400 *repop = repo;
401 free(abspath);
402 return err;
405 const struct got_error *
406 got_repo_close(struct got_repository *repo)
408 const struct got_error *err = NULL, *child_err;
409 int i;
411 for (i = 0; i < nitems(repo->packidx_cache); i++) {
412 if (repo->packidx_cache[i] == NULL)
413 break;
414 got_packidx_close(repo->packidx_cache[i]);
417 for (i = 0; i < nitems(repo->packs); i++) {
418 if (repo->packs[i].path_packfile == NULL)
419 break;
420 got_pack_close(&repo->packs[i]);
423 free(repo->path);
424 free(repo->path_git_dir);
426 got_object_cache_close(&repo->objcache);
427 got_object_cache_close(&repo->treecache);
428 got_object_cache_close(&repo->commitcache);
429 got_object_cache_close(&repo->tagcache);
431 for (i = 0; i < nitems(repo->privsep_children); i++) {
432 if (repo->privsep_children[i].imsg_fd == -1)
433 continue;
434 imsg_clear(repo->privsep_children[i].ibuf);
435 free(repo->privsep_children[i].ibuf);
436 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
437 child_err = got_privsep_wait_for_child(
438 repo->privsep_children[i].pid);
439 if (child_err && err == NULL)
440 err = child_err;
441 if (close(repo->privsep_children[i].imsg_fd) != 0 &&
442 err == NULL)
443 err = got_error_from_errno("close");
445 free(repo);
447 return err;
450 const struct got_error *
451 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
452 const char *input_path, int check_disk)
454 const struct got_error *err = NULL;
455 const char *repo_abspath = NULL;
456 size_t repolen, cwdlen, len;
457 char *cwd, *canonpath, *path = NULL;
459 *in_repo_path = NULL;
461 cwd = getcwd(NULL, 0);
462 if (cwd == NULL)
463 return got_error_from_errno("getcwd");
465 canonpath = strdup(input_path);
466 if (canonpath == NULL) {
467 err = got_error_from_errno("strdup");
468 goto done;
470 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
471 if (err)
472 goto done;
474 repo_abspath = got_repo_get_path(repo);
476 if (!check_disk || canonpath[0] == '\0') {
477 path = strdup(canonpath);
478 if (path == NULL) {
479 err = got_error_from_errno("strdup");
480 goto done;
482 } else {
483 int is_repo_child = 0, is_cwd_child = 0;
485 path = realpath(canonpath, NULL);
486 if (path == NULL) {
487 if (errno != ENOENT) {
488 err = got_error_from_errno2("realpath",
489 canonpath);
490 goto done;
492 /*
493 * Path is not on disk.
494 * Assume it is already relative to repository root.
495 */
496 path = strdup(canonpath);
497 if (path == NULL) {
498 err = got_error_from_errno("strdup");
499 goto done;
503 repolen = strlen(repo_abspath);
504 cwdlen = strlen(cwd);
505 len = strlen(path);
507 if (len > repolen && strncmp(path, repo_abspath, repolen) == 0)
508 is_repo_child = 1;
509 if (len > cwdlen && strncmp(path, cwd, cwdlen) == 0)
510 is_cwd_child = 1;
512 if (strcmp(path, repo_abspath) == 0) {
513 free(path);
514 path = strdup("");
515 if (path == NULL) {
516 err = got_error_from_errno("strdup");
517 goto done;
519 } else if (is_repo_child && is_cwd_child) {
520 char *child;
521 /* Strip common prefix with repository path. */
522 err = got_path_skip_common_ancestor(&child,
523 repo_abspath, path);
524 if (err)
525 goto done;
526 free(path);
527 path = child;
528 } else if (is_repo_child) {
529 /* Matched an on-disk path inside repository. */
530 if (got_repo_is_bare(repo)) {
531 /*
532 * Matched an on-disk path inside repository
533 * database. Treat as repository-relative.
534 */
535 } else {
536 char *child;
537 /* Strip common prefix with repository path. */
538 err = got_path_skip_common_ancestor(&child,
539 repo_abspath, path);
540 if (err)
541 goto done;
542 free(path);
543 path = child;
545 } else if (is_cwd_child) {
546 char *child;
547 /* Strip common prefix with cwd. */
548 err = got_path_skip_common_ancestor(&child, cwd,
549 path);
550 if (err)
551 goto done;
552 free(path);
553 path = child;
554 } else {
555 /*
556 * Matched unrelated on-disk path.
557 * Treat it as repository-relative.
558 */
562 /* Make in-repository path absolute */
563 if (path[0] != '/') {
564 char *abspath;
565 if (asprintf(&abspath, "/%s", path) == -1) {
566 err = got_error_from_errno("asprintf");
567 goto done;
569 free(path);
570 path = abspath;
573 done:
574 free(cwd);
575 free(canonpath);
576 if (err)
577 free(path);
578 else
579 *in_repo_path = path;
580 return err;
583 const struct got_error *
584 got_repo_cache_packidx(struct got_repository *repo, struct got_packidx *packidx)
586 const struct got_error *err = NULL;
587 int i;
589 for (i = 0; i < nitems(repo->packidx_cache); i++) {
590 if (repo->packidx_cache[i] == NULL)
591 break;
593 if (i == nitems(repo->packidx_cache)) {
594 err = got_packidx_close(repo->packidx_cache[i - 1]);
595 if (err)
596 return err;
599 /*
600 * Insert the new pack index at the front so it will
601 * be searched first in the future.
602 */
603 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
604 sizeof(repo->packidx_cache) -
605 sizeof(repo->packidx_cache[0]));
606 repo->packidx_cache[0] = packidx;
608 return NULL;
611 static int
612 is_packidx_filename(const char *name, size_t len)
614 if (len != GOT_PACKIDX_NAMELEN)
615 return 0;
617 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
618 return 0;
620 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
621 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
622 return 0;
624 return 1;
627 const struct got_error *
628 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
629 struct got_repository *repo, struct got_object_id *id)
631 const struct got_error *err;
632 char *path_packdir;
633 DIR *packdir;
634 struct dirent *dent;
635 char *path_packidx;
636 int i;
638 /* Search pack index cache. */
639 for (i = 0; i < nitems(repo->packidx_cache); i++) {
640 if (repo->packidx_cache[i] == NULL)
641 break;
642 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
643 if (*idx != -1) {
644 *packidx = repo->packidx_cache[i];
645 return NULL;
648 /* No luck. Search the filesystem. */
650 path_packdir = got_repo_get_path_objects_pack(repo);
651 if (path_packdir == NULL)
652 return got_error_from_errno("got_repo_get_path_objects_pack");
654 packdir = opendir(path_packdir);
655 if (packdir == NULL) {
656 if (errno == ENOENT)
657 err = got_error_no_obj(id);
658 else
659 err = got_error_from_errno2("opendir", path_packdir);
660 goto done;
663 while ((dent = readdir(packdir)) != NULL) {
664 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
665 continue;
667 if (asprintf(&path_packidx, "%s/%s", path_packdir,
668 dent->d_name) == -1) {
669 err = got_error_from_errno("asprintf");
670 goto done;
673 err = got_packidx_open(packidx, path_packidx, 0);
674 free(path_packidx);
675 if (err)
676 goto done;
678 *idx = got_packidx_get_object_idx(*packidx, id);
679 if (*idx != -1) {
680 err = NULL; /* found the object */
681 err = got_repo_cache_packidx(repo, *packidx);
682 goto done;
685 err = got_packidx_close(*packidx);
686 *packidx = NULL;
687 if (err)
688 goto done;
691 err = got_error_no_obj(id);
692 done:
693 free(path_packdir);
694 if (packdir && closedir(packdir) != 0 && err == NULL)
695 err = got_error_from_errno("closedir");
696 return err;
699 static const struct got_error *
700 read_packfile_hdr(int fd, struct got_packidx *packidx)
702 const struct got_error *err = NULL;
703 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
704 struct got_packfile_hdr hdr;
705 ssize_t n;
707 n = read(fd, &hdr, sizeof(hdr));
708 if (n < 0)
709 return got_error_from_errno("read");
710 if (n != sizeof(hdr))
711 return got_error(GOT_ERR_BAD_PACKFILE);
713 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
714 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
715 betoh32(hdr.nobjects) != totobj)
716 err = got_error(GOT_ERR_BAD_PACKFILE);
718 return err;
721 static const struct got_error *
722 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
724 const struct got_error *err = NULL;
726 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
727 if (*fd == -1)
728 return got_error_from_errno2("open", path_packfile);
730 if (packidx) {
731 err = read_packfile_hdr(*fd, packidx);
732 if (err) {
733 close(*fd);
734 *fd = -1;
738 return err;
741 const struct got_error *
742 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
743 const char *path_packfile, struct got_packidx *packidx)
745 const struct got_error *err = NULL;
746 struct got_pack *pack = NULL;
747 struct stat sb;
748 int i;
750 if (packp)
751 *packp = NULL;
753 for (i = 0; i < nitems(repo->packs); i++) {
754 pack = &repo->packs[i];
755 if (pack->path_packfile == NULL)
756 break;
757 if (strcmp(pack->path_packfile, path_packfile) == 0)
758 return NULL;
761 if (i == nitems(repo->packs) - 1) {
762 err = got_pack_close(&repo->packs[i - 1]);
763 if (err)
764 return err;
765 memmove(&repo->packs[1], &repo->packs[0],
766 sizeof(repo->packs) - sizeof(repo->packs[0]));
767 i = 0;
770 pack = &repo->packs[i];
772 pack->path_packfile = strdup(path_packfile);
773 if (pack->path_packfile == NULL) {
774 err = got_error_from_errno("strdup");
775 goto done;
778 err = open_packfile(&pack->fd, path_packfile, packidx);
779 if (err)
780 goto done;
782 if (fstat(pack->fd, &sb) != 0) {
783 err = got_error_from_errno("fstat");
784 goto done;
786 pack->filesize = sb.st_size;
788 pack->privsep_child = NULL;
790 #ifndef GOT_PACK_NO_MMAP
791 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
792 pack->fd, 0);
793 if (pack->map == MAP_FAILED) {
794 if (errno != ENOMEM) {
795 err = got_error_from_errno("mmap");
796 goto done;
798 pack->map = NULL; /* fall back to read(2) */
800 #endif
801 done:
802 if (err) {
803 if (pack) {
804 free(pack->path_packfile);
805 memset(pack, 0, sizeof(*pack));
807 } else if (packp)
808 *packp = pack;
809 return err;
812 struct got_pack *
813 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
815 struct got_pack *pack = NULL;
816 int i;
818 for (i = 0; i < nitems(repo->packs); i++) {
819 pack = &repo->packs[i];
820 if (pack->path_packfile == NULL)
821 break;
822 if (strcmp(pack->path_packfile, path_packfile) == 0)
823 return pack;
826 return NULL;
829 const struct got_error *
830 got_repo_init(const char *repo_path)
832 const struct got_error *err = NULL;
833 const char *dirnames[] = {
834 GOT_OBJECTS_DIR,
835 GOT_OBJECTS_PACK_DIR,
836 GOT_REFS_DIR,
837 };
838 const char *description_str = "Unnamed repository; "
839 "edit this file 'description' to name the repository.";
840 const char *headref_str = "ref: refs/heads/master";
841 const char *gitconfig_str = "[core]\n"
842 "\trepositoryformatversion = 0\n"
843 "\tfilemode = true\n"
844 "\tbare = true\n";
845 char *path;
846 int i;
848 if (!got_path_dir_is_empty(repo_path))
849 return got_error(GOT_ERR_DIR_NOT_EMPTY);
851 for (i = 0; i < nitems(dirnames); i++) {
852 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
853 return got_error_from_errno("asprintf");
855 err = got_path_mkdir(path);
856 free(path);
857 if (err)
858 return err;
861 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
862 return got_error_from_errno("asprintf");
863 err = got_path_create_file(path, description_str);
864 free(path);
865 if (err)
866 return err;
868 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
869 return got_error_from_errno("asprintf");
870 err = got_path_create_file(path, headref_str);
871 free(path);
872 if (err)
873 return err;
875 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
876 return got_error_from_errno("asprintf");
877 err = got_path_create_file(path, gitconfig_str);
878 free(path);
879 if (err)
880 return err;
882 return NULL;
885 static const struct got_error *
886 match_packed_object(struct got_object_id **unique_id,
887 struct got_repository *repo, const char *id_str_prefix, int obj_type)
889 const struct got_error *err = NULL;
890 char *path_packdir;
891 DIR *packdir;
892 struct dirent *dent;
893 char *path_packidx;
894 struct got_object_id_queue matched_ids;
896 SIMPLEQ_INIT(&matched_ids);
898 path_packdir = got_repo_get_path_objects_pack(repo);
899 if (path_packdir == NULL)
900 return got_error_from_errno("got_repo_get_path_objects_pack");
902 packdir = opendir(path_packdir);
903 if (packdir == NULL) {
904 if (errno != ENOENT)
905 err = got_error_from_errno2("opendir", path_packdir);
906 goto done;
909 while ((dent = readdir(packdir)) != NULL) {
910 struct got_packidx *packidx;
911 struct got_object_qid *qid;
914 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
915 continue;
917 if (asprintf(&path_packidx, "%s/%s", path_packdir,
918 dent->d_name) == -1) {
919 err = got_error_from_errno("asprintf");
920 break;
923 err = got_packidx_open(&packidx, path_packidx, 0);
924 free(path_packidx);
925 if (err)
926 break;
928 err = got_packidx_match_id_str_prefix(&matched_ids,
929 packidx, id_str_prefix);
930 if (err) {
931 got_packidx_close(packidx);
932 break;
934 err = got_packidx_close(packidx);
935 if (err)
936 break;
938 SIMPLEQ_FOREACH(qid, &matched_ids, entry) {
939 if (obj_type != GOT_OBJ_TYPE_ANY) {
940 int matched_type;
941 err = got_object_get_type(&matched_type, repo,
942 qid->id);
943 if (err)
944 goto done;
945 if (matched_type != obj_type)
946 continue;
948 if (*unique_id == NULL) {
949 *unique_id = got_object_id_dup(qid->id);
950 if (*unique_id == NULL) {
951 err = got_error_from_errno("malloc");
952 goto done;
954 } else {
955 err = got_error(GOT_ERR_AMBIGUOUS_ID);
956 goto done;
960 done:
961 got_object_id_queue_free(&matched_ids);
962 free(path_packdir);
963 if (packdir && closedir(packdir) != 0 && err == NULL)
964 err = got_error_from_errno("closedir");
965 if (err) {
966 free(*unique_id);
967 *unique_id = NULL;
969 return err;
972 static const struct got_error *
973 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
974 const char *object_dir, const char *id_str_prefix, int obj_type,
975 struct got_repository *repo)
977 const struct got_error *err = NULL;
978 char *path;
979 DIR *dir = NULL;
980 struct dirent *dent;
981 struct got_object_id id;
983 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
984 err = got_error_from_errno("asprintf");
985 goto done;
988 dir = opendir(path);
989 if (dir == NULL) {
990 if (errno == ENOENT) {
991 err = NULL;
992 goto done;
994 err = got_error_from_errno2("opendir", path);
995 goto done;
997 while ((dent = readdir(dir)) != NULL) {
998 char *id_str;
999 int cmp;
1001 if (strcmp(dent->d_name, ".") == 0 ||
1002 strcmp(dent->d_name, "..") == 0)
1003 continue;
1005 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1006 err = got_error_from_errno("asprintf");
1007 goto done;
1010 if (!got_parse_sha1_digest(id.sha1, id_str))
1011 continue;
1014 * Directory entries do not necessarily appear in
1015 * sorted order, so we must iterate over all of them.
1017 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1018 if (cmp != 0) {
1019 free(id_str);
1020 continue;
1023 if (*unique_id == NULL) {
1024 if (obj_type != GOT_OBJ_TYPE_ANY) {
1025 int matched_type;
1026 err = got_object_get_type(&matched_type, repo,
1027 &id);
1028 if (err)
1029 goto done;
1030 if (matched_type != obj_type)
1031 continue;
1033 *unique_id = got_object_id_dup(&id);
1034 if (*unique_id == NULL) {
1035 err = got_error_from_errno("got_object_id_dup");
1036 free(id_str);
1037 goto done;
1039 } else {
1040 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1041 free(id_str);
1042 goto done;
1045 done:
1046 if (dir && closedir(dir) != 0 && err == NULL)
1047 err = got_error_from_errno("closedir");
1048 if (err) {
1049 free(*unique_id);
1050 *unique_id = NULL;
1052 free(path);
1053 return err;
1056 const struct got_error *
1057 got_repo_match_object_id_prefix(struct got_object_id **id,
1058 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1060 const struct got_error *err = NULL;
1061 char *path_objects = got_repo_get_path_objects(repo);
1062 char *object_dir = NULL;
1063 size_t len;
1064 int i;
1066 *id = NULL;
1068 for (i = 0; i < strlen(id_str_prefix); i++) {
1069 if (isxdigit((unsigned char)id_str_prefix[i]))
1070 continue;
1071 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
1074 len = strlen(id_str_prefix);
1075 if (len >= 2) {
1076 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1077 if (err)
1078 goto done;
1079 object_dir = strndup(id_str_prefix, 2);
1080 if (object_dir == NULL) {
1081 err = got_error_from_errno("strdup");
1082 goto done;
1084 err = match_loose_object(id, path_objects, object_dir,
1085 id_str_prefix, obj_type, repo);
1086 } else if (len == 1) {
1087 int i;
1088 for (i = 0; i < 0xf; i++) {
1089 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1090 == -1) {
1091 err = got_error_from_errno("asprintf");
1092 goto done;
1094 err = match_packed_object(id, repo, object_dir,
1095 obj_type);
1096 if (err)
1097 goto done;
1098 err = match_loose_object(id, path_objects, object_dir,
1099 id_str_prefix, obj_type, repo);
1100 if (err)
1101 goto done;
1103 } else {
1104 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
1105 goto done;
1107 done:
1108 free(object_dir);
1109 if (err) {
1110 free(*id);
1111 *id = NULL;
1112 } else if (*id == NULL)
1113 err = got_error(GOT_ERR_NO_OBJ);
1115 return err;
1118 static const struct got_error *
1119 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1120 const char *name, mode_t mode, struct got_object_id *blob_id)
1122 const struct got_error *err = NULL;
1124 *new_te = NULL;
1126 *new_te = calloc(1, sizeof(**new_te));
1127 if (*new_te == NULL)
1128 return got_error_from_errno("calloc");
1130 (*new_te)->name = strdup(name);
1131 if ((*new_te)->name == NULL) {
1132 err = got_error_from_errno("strdup");
1133 goto done;
1136 (*new_te)->mode = S_IFREG | (mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
1137 (*new_te)->id = blob_id;
1138 done:
1139 if (err && *new_te) {
1140 got_object_tree_entry_close(*new_te);
1141 *new_te = NULL;
1143 return err;
1146 static const struct got_error *
1147 import_file(struct got_tree_entry **new_te, struct dirent *de,
1148 const char *path, struct got_repository *repo)
1150 const struct got_error *err;
1151 struct got_object_id *blob_id = NULL;
1152 char *filepath;
1153 struct stat sb;
1155 if (asprintf(&filepath, "%s%s%s", path,
1156 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1157 return got_error_from_errno("asprintf");
1159 if (lstat(filepath, &sb) != 0) {
1160 err = got_error_from_errno2("lstat", path);
1161 goto done;
1164 err = got_object_blob_create(&blob_id, filepath, repo);
1165 if (err)
1166 goto done;
1168 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1169 blob_id);
1170 done:
1171 free(filepath);
1172 if (err)
1173 free(blob_id);
1174 return err;
1177 static const struct got_error *
1178 insert_tree_entry(struct got_tree_entry *new_te,
1179 struct got_pathlist_head *paths)
1181 const struct got_error *err = NULL;
1182 struct got_pathlist_entry *new_pe;
1184 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1185 if (err)
1186 return err;
1187 if (new_pe == NULL)
1188 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1189 return NULL;
1192 static const struct got_error *write_tree(struct got_object_id **,
1193 const char *, struct got_pathlist_head *, struct got_repository *,
1194 got_repo_import_cb progress_cb, void *progress_arg);
1196 static const struct got_error *
1197 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1198 const char *path, struct got_pathlist_head *ignores,
1199 struct got_repository *repo,
1200 got_repo_import_cb progress_cb, void *progress_arg)
1202 const struct got_error *err;
1203 char *subdirpath;
1205 if (asprintf(&subdirpath, "%s%s%s", path,
1206 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1207 return got_error_from_errno("asprintf");
1209 (*new_te) = calloc(1, sizeof(**new_te));
1210 (*new_te)->mode = S_IFDIR;
1211 (*new_te)->name = strdup(de->d_name);
1212 if ((*new_te)->name == NULL) {
1213 err = got_error_from_errno("strdup");
1214 goto done;
1217 err = write_tree(&(*new_te)->id, subdirpath, ignores, repo,
1218 progress_cb, progress_arg);
1219 done:
1220 free(subdirpath);
1221 if (err) {
1222 got_object_tree_entry_close(*new_te);
1223 *new_te = NULL;
1225 return err;
1228 static const struct got_error *
1229 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1230 struct got_pathlist_head *ignores, struct got_repository *repo,
1231 got_repo_import_cb progress_cb, void *progress_arg)
1233 const struct got_error *err = NULL;
1234 DIR *dir;
1235 struct dirent *de;
1236 struct got_tree_entries new_tree_entries;
1237 struct got_tree_entry *new_te = NULL;
1238 struct got_pathlist_head paths;
1239 struct got_pathlist_entry *pe;
1241 *new_tree_id = NULL;
1243 TAILQ_INIT(&paths);
1244 new_tree_entries.nentries = 0;
1245 SIMPLEQ_INIT(&new_tree_entries.head);
1247 dir = opendir(path_dir);
1248 if (dir == NULL) {
1249 err = got_error_from_errno2("opendir", path_dir);
1250 goto done;
1253 while ((de = readdir(dir)) != NULL) {
1254 int ignore = 0;
1256 if (strcmp(de->d_name, ".") == 0 ||
1257 strcmp(de->d_name, "..") == 0)
1258 continue;
1260 TAILQ_FOREACH(pe, ignores, entry) {
1261 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1262 ignore = 1;
1263 break;
1266 if (ignore)
1267 continue;
1268 if (de->d_type == DT_DIR) {
1269 err = import_subdir(&new_te, de, path_dir,
1270 ignores, repo, progress_cb, progress_arg);
1271 if (err)
1272 goto done;
1273 } else if (de->d_type == DT_REG) {
1274 err = import_file(&new_te, de, path_dir, repo);
1275 if (err)
1276 goto done;
1277 } else
1278 continue;
1280 err = insert_tree_entry(new_te, &paths);
1281 if (err)
1282 goto done;
1285 TAILQ_FOREACH(pe, &paths, entry) {
1286 struct got_tree_entry *te = pe->data;
1287 char *path;
1288 new_tree_entries.nentries++;
1289 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
1290 if (!S_ISREG(te->mode))
1291 continue;
1292 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1293 err = got_error_from_errno("asprintf");
1294 goto done;
1296 err = (*progress_cb)(progress_arg, path);
1297 free(path);
1298 if (err)
1299 goto done;
1302 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
1303 done:
1304 if (dir)
1305 closedir(dir);
1306 got_object_tree_entries_close(&new_tree_entries);
1307 got_pathlist_free(&paths);
1308 return err;
1311 const struct got_error *
1312 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1313 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1314 struct got_repository *repo, got_repo_import_cb progress_cb,
1315 void *progress_arg)
1317 const struct got_error *err;
1318 struct got_object_id *new_tree_id;
1320 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1321 progress_cb, progress_arg);
1322 if (err)
1323 return err;
1325 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1326 author, time(NULL), author, time(NULL), logmsg, repo);
1327 free(new_tree_id);
1328 return err;