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, *normpath = NULL;
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 normpath = got_path_normalize(abspath);
374 if (normpath == NULL) {
375 err = got_error(GOT_ERR_BAD_PATH);
376 goto done;
379 path = normpath;
380 do {
381 err = open_repo(repo, path);
382 if (err == NULL)
383 break;
384 if (err->code != GOT_ERR_NOT_GIT_REPO)
385 break;
386 if (path[0] == '/' && path[1] == '\0') {
387 if (tried_root) {
388 err = got_error(GOT_ERR_NOT_GIT_REPO);
389 break;
391 tried_root = 1;
393 path = dirname(path);
394 if (path == NULL)
395 err = got_error_from_errno2("dirname", path);
396 } while (path);
397 done:
398 if (err)
399 got_repo_close(repo);
400 else
401 *repop = repo;
402 free(abspath);
403 free(normpath);
404 return err;
407 const struct got_error *
408 got_repo_close(struct got_repository *repo)
410 const struct got_error *err = NULL, *child_err;
411 int i;
413 for (i = 0; i < nitems(repo->packidx_cache); i++) {
414 if (repo->packidx_cache[i] == NULL)
415 break;
416 got_packidx_close(repo->packidx_cache[i]);
419 for (i = 0; i < nitems(repo->packs); i++) {
420 if (repo->packs[i].path_packfile == NULL)
421 break;
422 got_pack_close(&repo->packs[i]);
425 free(repo->path);
426 free(repo->path_git_dir);
428 got_object_cache_close(&repo->objcache);
429 got_object_cache_close(&repo->treecache);
430 got_object_cache_close(&repo->commitcache);
431 got_object_cache_close(&repo->tagcache);
433 for (i = 0; i < nitems(repo->privsep_children); i++) {
434 if (repo->privsep_children[i].imsg_fd == -1)
435 continue;
436 imsg_clear(repo->privsep_children[i].ibuf);
437 free(repo->privsep_children[i].ibuf);
438 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
439 child_err = got_privsep_wait_for_child(
440 repo->privsep_children[i].pid);
441 if (child_err && err == NULL)
442 err = child_err;
443 if (close(repo->privsep_children[i].imsg_fd) != 0 &&
444 err == NULL)
445 err = got_error_from_errno("close");
447 free(repo);
449 return err;
452 const struct got_error *
453 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
454 const char *input_path, int check_disk)
456 const struct got_error *err = NULL;
457 const char *repo_abspath = NULL;
458 size_t repolen, cwdlen, len;
459 char *cwd, *canonpath, *path = NULL;
461 *in_repo_path = NULL;
463 cwd = getcwd(NULL, 0);
464 if (cwd == NULL)
465 return got_error_from_errno("getcwd");
467 canonpath = strdup(input_path);
468 if (canonpath == NULL) {
469 err = got_error_from_errno("strdup");
470 goto done;
472 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
473 if (err)
474 goto done;
476 repo_abspath = got_repo_get_path(repo);
478 if (!check_disk || canonpath[0] == '\0') {
479 path = strdup(canonpath);
480 if (path == NULL) {
481 err = got_error_from_errno("strdup");
482 goto done;
484 } else {
485 int is_repo_child = 0, is_cwd_child = 0;
487 path = realpath(canonpath, NULL);
488 if (path == NULL) {
489 if (errno != ENOENT) {
490 err = got_error_from_errno2("realpath",
491 canonpath);
492 goto done;
494 /*
495 * Path is not on disk.
496 * Assume it is already relative to repository root.
497 */
498 path = strdup(canonpath);
499 if (path == NULL) {
500 err = got_error_from_errno("strdup");
501 goto done;
505 repolen = strlen(repo_abspath);
506 cwdlen = strlen(cwd);
507 len = strlen(path);
509 if (len > repolen && strncmp(path, repo_abspath, repolen) == 0)
510 is_repo_child = 1;
511 if (len > cwdlen && strncmp(path, cwd, cwdlen) == 0)
512 is_cwd_child = 1;
514 if (strcmp(path, repo_abspath) == 0) {
515 free(path);
516 path = strdup("");
517 if (path == NULL) {
518 err = got_error_from_errno("strdup");
519 goto done;
521 } else if (is_repo_child && is_cwd_child) {
522 char *child;
523 /* Strip common prefix with repository path. */
524 err = got_path_skip_common_ancestor(&child,
525 repo_abspath, path);
526 if (err)
527 goto done;
528 free(path);
529 path = child;
530 } else if (is_repo_child) {
531 /* Matched an on-disk path inside repository. */
532 if (got_repo_is_bare(repo)) {
533 /*
534 * Matched an on-disk path inside repository
535 * database. Treat as repository-relative.
536 */
537 } else {
538 char *child;
539 /* Strip common prefix with repository path. */
540 err = got_path_skip_common_ancestor(&child,
541 repo_abspath, path);
542 if (err)
543 goto done;
544 free(path);
545 path = child;
547 } else if (is_cwd_child) {
548 char *child;
549 /* Strip common prefix with cwd. */
550 err = got_path_skip_common_ancestor(&child, cwd,
551 path);
552 if (err)
553 goto done;
554 free(path);
555 path = child;
556 } else {
557 /*
558 * Matched unrelated on-disk path.
559 * Treat it as repository-relative.
560 */
564 /* Make in-repository path absolute */
565 if (path[0] != '/') {
566 char *abspath;
567 if (asprintf(&abspath, "/%s", path) == -1) {
568 err = got_error_from_errno("asprintf");
569 goto done;
571 free(path);
572 path = abspath;
575 done:
576 free(cwd);
577 free(canonpath);
578 if (err)
579 free(path);
580 else
581 *in_repo_path = path;
582 return err;
585 const struct got_error *
586 got_repo_cache_packidx(struct got_repository *repo, struct got_packidx *packidx)
588 const struct got_error *err = NULL;
589 int i;
591 for (i = 0; i < nitems(repo->packidx_cache); i++) {
592 if (repo->packidx_cache[i] == NULL)
593 break;
595 if (i == nitems(repo->packidx_cache)) {
596 err = got_packidx_close(repo->packidx_cache[i - 1]);
597 if (err)
598 return err;
601 /*
602 * Insert the new pack index at the front so it will
603 * be searched first in the future.
604 */
605 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
606 sizeof(repo->packidx_cache) -
607 sizeof(repo->packidx_cache[0]));
608 repo->packidx_cache[0] = packidx;
610 return NULL;
613 static int
614 is_packidx_filename(const char *name, size_t len)
616 if (len != GOT_PACKIDX_NAMELEN)
617 return 0;
619 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
620 return 0;
622 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
623 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
624 return 0;
626 return 1;
629 const struct got_error *
630 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
631 struct got_repository *repo, struct got_object_id *id)
633 const struct got_error *err;
634 char *path_packdir;
635 DIR *packdir;
636 struct dirent *dent;
637 char *path_packidx;
638 int i;
640 /* Search pack index cache. */
641 for (i = 0; i < nitems(repo->packidx_cache); i++) {
642 if (repo->packidx_cache[i] == NULL)
643 break;
644 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
645 if (*idx != -1) {
646 *packidx = repo->packidx_cache[i];
647 return NULL;
650 /* No luck. Search the filesystem. */
652 path_packdir = got_repo_get_path_objects_pack(repo);
653 if (path_packdir == NULL)
654 return got_error_from_errno("got_repo_get_path_objects_pack");
656 packdir = opendir(path_packdir);
657 if (packdir == NULL) {
658 err = got_error_from_errno2("opendir", path_packdir);
659 goto done;
662 while ((dent = readdir(packdir)) != NULL) {
663 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
664 continue;
666 if (asprintf(&path_packidx, "%s/%s", path_packdir,
667 dent->d_name) == -1) {
668 err = got_error_from_errno("asprintf");
669 goto done;
672 err = got_packidx_open(packidx, path_packidx, 0);
673 free(path_packidx);
674 if (err)
675 goto done;
677 *idx = got_packidx_get_object_idx(*packidx, id);
678 if (*idx != -1) {
679 err = NULL; /* found the object */
680 err = got_repo_cache_packidx(repo, *packidx);
681 goto done;
684 err = got_packidx_close(*packidx);
685 *packidx = NULL;
686 if (err)
687 goto done;
690 err = got_error_no_obj(id);
691 done:
692 free(path_packdir);
693 if (packdir && closedir(packdir) != 0 && err == NULL)
694 err = got_error_from_errno("closedir");
695 return err;
698 static const struct got_error *
699 read_packfile_hdr(int fd, struct got_packidx *packidx)
701 const struct got_error *err = NULL;
702 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
703 struct got_packfile_hdr hdr;
704 ssize_t n;
706 n = read(fd, &hdr, sizeof(hdr));
707 if (n < 0)
708 return got_error_from_errno("read");
709 if (n != sizeof(hdr))
710 return got_error(GOT_ERR_BAD_PACKFILE);
712 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
713 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
714 betoh32(hdr.nobjects) != totobj)
715 err = got_error(GOT_ERR_BAD_PACKFILE);
717 return err;
720 static const struct got_error *
721 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
723 const struct got_error *err = NULL;
725 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
726 if (*fd == -1)
727 return got_error_from_errno2("open", path_packfile);
729 if (packidx) {
730 err = read_packfile_hdr(*fd, packidx);
731 if (err) {
732 close(*fd);
733 *fd = -1;
737 return err;
740 const struct got_error *
741 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
742 const char *path_packfile, struct got_packidx *packidx)
744 const struct got_error *err = NULL;
745 struct got_pack *pack = NULL;
746 struct stat sb;
747 int i;
749 if (packp)
750 *packp = NULL;
752 for (i = 0; i < nitems(repo->packs); i++) {
753 pack = &repo->packs[i];
754 if (pack->path_packfile == NULL)
755 break;
756 if (strcmp(pack->path_packfile, path_packfile) == 0)
757 return NULL;
760 if (i == nitems(repo->packs) - 1) {
761 err = got_pack_close(&repo->packs[i - 1]);
762 if (err)
763 return err;
764 memmove(&repo->packs[1], &repo->packs[0],
765 sizeof(repo->packs) - sizeof(repo->packs[0]));
766 i = 0;
769 pack = &repo->packs[i];
771 pack->path_packfile = strdup(path_packfile);
772 if (pack->path_packfile == NULL) {
773 err = got_error_from_errno("strdup");
774 goto done;
777 err = open_packfile(&pack->fd, path_packfile, packidx);
778 if (err)
779 goto done;
781 if (fstat(pack->fd, &sb) != 0) {
782 err = got_error_from_errno("fstat");
783 goto done;
785 pack->filesize = sb.st_size;
787 pack->privsep_child = NULL;
789 #ifndef GOT_PACK_NO_MMAP
790 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
791 pack->fd, 0);
792 if (pack->map == MAP_FAILED) {
793 if (errno != ENOMEM) {
794 err = got_error_from_errno("mmap");
795 goto done;
797 pack->map = NULL; /* fall back to read(2) */
799 #endif
800 done:
801 if (err) {
802 if (pack) {
803 free(pack->path_packfile);
804 memset(pack, 0, sizeof(*pack));
806 } else if (packp)
807 *packp = pack;
808 return err;
811 struct got_pack *
812 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
814 struct got_pack *pack = NULL;
815 int i;
817 for (i = 0; i < nitems(repo->packs); i++) {
818 pack = &repo->packs[i];
819 if (pack->path_packfile == NULL)
820 break;
821 if (strcmp(pack->path_packfile, path_packfile) == 0)
822 return pack;
825 return NULL;
828 const struct got_error *
829 got_repo_init(const char *repo_path)
831 const struct got_error *err = NULL;
832 const char *dirnames[] = {
833 GOT_OBJECTS_DIR,
834 GOT_OBJECTS_PACK_DIR,
835 GOT_REFS_DIR,
836 };
837 const char *description_str = "Unnamed repository; "
838 "edit this file 'description' to name the repository.";
839 const char *headref_str = "ref: refs/heads/master";
840 const char *gitconfig_str = "[core]\n"
841 "\trepositoryformatversion = 0\n"
842 "\tfilemode = true\n"
843 "\tbare = true\n";
844 char *path;
845 int i;
847 if (!got_path_dir_is_empty(repo_path))
848 return got_error(GOT_ERR_DIR_NOT_EMPTY);
850 for (i = 0; i < nitems(dirnames); i++) {
851 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
852 return got_error_from_errno("asprintf");
854 err = got_path_mkdir(path);
855 free(path);
856 if (err)
857 return err;
860 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
861 return got_error_from_errno("asprintf");
862 err = got_path_create_file(path, description_str);
863 free(path);
864 if (err)
865 return err;
867 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
868 return got_error_from_errno("asprintf");
869 err = got_path_create_file(path, headref_str);
870 free(path);
871 if (err)
872 return err;
874 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
875 return got_error_from_errno("asprintf");
876 err = got_path_create_file(path, gitconfig_str);
877 free(path);
878 if (err)
879 return err;
881 return NULL;
884 static const struct got_error *
885 match_packed_object(struct got_object_id **unique_id,
886 struct got_repository *repo, const char *id_str_prefix, int obj_type)
888 const struct got_error *err = NULL;
889 char *path_packdir;
890 DIR *packdir;
891 struct dirent *dent;
892 char *path_packidx;
893 struct got_object_id_queue matched_ids;
895 SIMPLEQ_INIT(&matched_ids);
897 path_packdir = got_repo_get_path_objects_pack(repo);
898 if (path_packdir == NULL)
899 return got_error_from_errno("got_repo_get_path_objects_pack");
901 packdir = opendir(path_packdir);
902 if (packdir == NULL) {
903 err = got_error_from_errno2("opendir", path_packdir);
904 goto done;
907 while ((dent = readdir(packdir)) != NULL) {
908 struct got_packidx *packidx;
909 struct got_object_qid *qid;
912 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
913 continue;
915 if (asprintf(&path_packidx, "%s/%s", path_packdir,
916 dent->d_name) == -1) {
917 err = got_error_from_errno("asprintf");
918 break;
921 err = got_packidx_open(&packidx, path_packidx, 0);
922 free(path_packidx);
923 if (err)
924 break;
926 err = got_packidx_match_id_str_prefix(&matched_ids,
927 packidx, id_str_prefix);
928 if (err) {
929 got_packidx_close(packidx);
930 break;
932 err = got_packidx_close(packidx);
933 if (err)
934 break;
936 SIMPLEQ_FOREACH(qid, &matched_ids, entry) {
937 if (obj_type != GOT_OBJ_TYPE_ANY) {
938 int matched_type;
939 err = got_object_get_type(&matched_type, repo,
940 qid->id);
941 if (err)
942 goto done;
943 if (matched_type != obj_type)
944 continue;
946 if (*unique_id == NULL) {
947 *unique_id = got_object_id_dup(qid->id);
948 if (*unique_id == NULL) {
949 err = got_error_from_errno("malloc");
950 goto done;
952 } else {
953 err = got_error(GOT_ERR_AMBIGUOUS_ID);
954 goto done;
958 done:
959 got_object_id_queue_free(&matched_ids);
960 free(path_packdir);
961 if (packdir && closedir(packdir) != 0 && err == NULL)
962 err = got_error_from_errno("closedir");
963 if (err) {
964 free(*unique_id);
965 *unique_id = NULL;
967 return err;
970 static const struct got_error *
971 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
972 const char *object_dir, const char *id_str_prefix, int obj_type,
973 struct got_repository *repo)
975 const struct got_error *err = NULL;
976 char *path;
977 DIR *dir = NULL;
978 struct dirent *dent;
979 struct got_object_id id;
981 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
982 err = got_error_from_errno("asprintf");
983 goto done;
986 dir = opendir(path);
987 if (dir == NULL) {
988 if (errno == ENOENT) {
989 err = NULL;
990 goto done;
992 err = got_error_from_errno2("opendir", path);
993 goto done;
995 while ((dent = readdir(dir)) != NULL) {
996 char *id_str;
997 int cmp;
999 if (strcmp(dent->d_name, ".") == 0 ||
1000 strcmp(dent->d_name, "..") == 0)
1001 continue;
1003 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1004 err = got_error_from_errno("asprintf");
1005 goto done;
1008 if (!got_parse_sha1_digest(id.sha1, id_str))
1009 continue;
1012 * Directory entries do not necessarily appear in
1013 * sorted order, so we must iterate over all of them.
1015 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1016 if (cmp != 0) {
1017 free(id_str);
1018 continue;
1021 if (*unique_id == NULL) {
1022 if (obj_type != GOT_OBJ_TYPE_ANY) {
1023 int matched_type;
1024 err = got_object_get_type(&matched_type, repo,
1025 &id);
1026 if (err)
1027 goto done;
1028 if (matched_type != obj_type)
1029 continue;
1031 *unique_id = got_object_id_dup(&id);
1032 if (*unique_id == NULL) {
1033 err = got_error_from_errno("got_object_id_dup");
1034 free(id_str);
1035 goto done;
1037 } else {
1038 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1039 free(id_str);
1040 goto done;
1043 done:
1044 if (dir && closedir(dir) != 0 && err == NULL)
1045 err = got_error_from_errno("closedir");
1046 if (err) {
1047 free(*unique_id);
1048 *unique_id = NULL;
1050 free(path);
1051 return err;
1054 const struct got_error *
1055 got_repo_match_object_id_prefix(struct got_object_id **id,
1056 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1058 const struct got_error *err = NULL;
1059 char *path_objects = got_repo_get_path_objects(repo);
1060 char *object_dir = NULL;
1061 size_t len;
1062 int i;
1064 *id = NULL;
1066 for (i = 0; i < strlen(id_str_prefix); i++) {
1067 if (isxdigit((unsigned char)id_str_prefix[i]))
1068 continue;
1069 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
1072 len = strlen(id_str_prefix);
1073 if (len >= 2) {
1074 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1075 if (err)
1076 goto done;
1077 object_dir = strndup(id_str_prefix, 2);
1078 if (object_dir == NULL) {
1079 err = got_error_from_errno("strdup");
1080 goto done;
1082 err = match_loose_object(id, path_objects, object_dir,
1083 id_str_prefix, obj_type, repo);
1084 } else if (len == 1) {
1085 int i;
1086 for (i = 0; i < 0xf; i++) {
1087 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1088 == -1) {
1089 err = got_error_from_errno("asprintf");
1090 goto done;
1092 err = match_packed_object(id, repo, object_dir,
1093 obj_type);
1094 if (err)
1095 goto done;
1096 err = match_loose_object(id, path_objects, object_dir,
1097 id_str_prefix, obj_type, repo);
1098 if (err)
1099 goto done;
1101 } else {
1102 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
1103 goto done;
1105 done:
1106 free(object_dir);
1107 if (err) {
1108 free(*id);
1109 *id = NULL;
1110 } else if (*id == NULL)
1111 err = got_error(GOT_ERR_NO_OBJ);
1113 return err;
1116 static const struct got_error *
1117 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1118 const char *name, mode_t mode, struct got_object_id *blob_id)
1120 const struct got_error *err = NULL;
1122 *new_te = NULL;
1124 *new_te = calloc(1, sizeof(**new_te));
1125 if (*new_te == NULL)
1126 return got_error_from_errno("calloc");
1128 (*new_te)->name = strdup(name);
1129 if ((*new_te)->name == NULL) {
1130 err = got_error_from_errno("strdup");
1131 goto done;
1134 (*new_te)->mode = S_IFREG | (mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
1135 (*new_te)->id = blob_id;
1136 done:
1137 if (err && *new_te) {
1138 got_object_tree_entry_close(*new_te);
1139 *new_te = NULL;
1141 return err;
1144 static const struct got_error *
1145 import_file(struct got_tree_entry **new_te, struct dirent *de,
1146 const char *path, struct got_repository *repo)
1148 const struct got_error *err;
1149 struct got_object_id *blob_id = NULL;
1150 char *filepath;
1151 struct stat sb;
1153 if (asprintf(&filepath, "%s%s%s", path,
1154 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1155 return got_error_from_errno("asprintf");
1157 if (lstat(filepath, &sb) != 0) {
1158 err = got_error_from_errno2("lstat", path);
1159 goto done;
1162 err = got_object_blob_create(&blob_id, filepath, repo);
1163 if (err)
1164 goto done;
1166 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1167 blob_id);
1168 done:
1169 free(filepath);
1170 if (err)
1171 free(blob_id);
1172 return err;
1175 static const struct got_error *
1176 insert_tree_entry(struct got_tree_entry *new_te,
1177 struct got_pathlist_head *paths)
1179 const struct got_error *err = NULL;
1180 struct got_pathlist_entry *new_pe;
1182 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1183 if (err)
1184 return err;
1185 if (new_pe == NULL)
1186 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1187 return NULL;
1190 static const struct got_error *write_tree(struct got_object_id **,
1191 const char *, struct got_pathlist_head *, struct got_repository *,
1192 got_repo_import_cb progress_cb, void *progress_arg);
1194 static const struct got_error *
1195 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1196 const char *path, struct got_pathlist_head *ignores,
1197 struct got_repository *repo,
1198 got_repo_import_cb progress_cb, void *progress_arg)
1200 const struct got_error *err;
1201 char *subdirpath;
1203 if (asprintf(&subdirpath, "%s%s%s", path,
1204 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1205 return got_error_from_errno("asprintf");
1207 (*new_te) = calloc(1, sizeof(**new_te));
1208 (*new_te)->mode = S_IFDIR;
1209 (*new_te)->name = strdup(de->d_name);
1210 if ((*new_te)->name == NULL) {
1211 err = got_error_from_errno("strdup");
1212 goto done;
1215 err = write_tree(&(*new_te)->id, subdirpath, ignores, repo,
1216 progress_cb, progress_arg);
1217 done:
1218 free(subdirpath);
1219 if (err) {
1220 got_object_tree_entry_close(*new_te);
1221 *new_te = NULL;
1223 return err;
1226 static const struct got_error *
1227 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1228 struct got_pathlist_head *ignores, struct got_repository *repo,
1229 got_repo_import_cb progress_cb, void *progress_arg)
1231 const struct got_error *err = NULL;
1232 DIR *dir;
1233 struct dirent *de;
1234 struct got_tree_entries new_tree_entries;
1235 struct got_tree_entry *new_te = NULL;
1236 struct got_pathlist_head paths;
1237 struct got_pathlist_entry *pe;
1238 char *name;
1240 *new_tree_id = NULL;
1242 TAILQ_INIT(&paths);
1243 new_tree_entries.nentries = 0;
1244 SIMPLEQ_INIT(&new_tree_entries.head);
1246 dir = opendir(path_dir);
1247 if (dir == NULL) {
1248 err = got_error_from_errno2("opendir", path_dir);
1249 goto done;
1252 while ((de = readdir(dir)) != NULL) {
1253 int ignore = 0;
1255 if (strcmp(de->d_name, ".") == 0 ||
1256 strcmp(de->d_name, "..") == 0)
1257 continue;
1259 TAILQ_FOREACH(pe, ignores, entry) {
1260 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1261 ignore = 1;
1262 break;
1265 if (ignore)
1266 continue;
1267 if (de->d_type == DT_DIR) {
1268 err = import_subdir(&new_te, de, path_dir,
1269 ignores, repo, progress_cb, progress_arg);
1270 if (err)
1271 goto done;
1272 } else if (de->d_type == DT_REG) {
1273 err = import_file(&new_te, de, path_dir, repo);
1274 if (err)
1275 goto done;
1276 } else
1277 continue;
1279 err = insert_tree_entry(new_te, &paths);
1280 if (err)
1281 goto done;
1284 TAILQ_FOREACH(pe, &paths, entry) {
1285 struct got_tree_entry *te = pe->data;
1286 char *path;
1287 new_tree_entries.nentries++;
1288 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
1289 if (!S_ISREG(te->mode))
1290 continue;
1291 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1292 err = got_error_from_errno("asprintf");
1293 goto done;
1295 err = (*progress_cb)(progress_arg, path);
1296 free(path);
1297 if (err)
1298 goto done;
1301 name = basename(path_dir);
1302 if (name == NULL) {
1303 err = got_error_from_errno("basename");
1304 goto done;
1307 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
1308 done:
1309 if (dir)
1310 closedir(dir);
1311 got_object_tree_entries_close(&new_tree_entries);
1312 got_pathlist_free(&paths);
1313 return err;
1316 const struct got_error *
1317 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1318 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1319 struct got_repository *repo, got_repo_import_cb progress_cb,
1320 void *progress_arg)
1322 const struct got_error *err;
1323 struct got_object_id *new_tree_id;
1325 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1326 progress_cb, progress_arg);
1327 if (err)
1328 return err;
1330 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1331 author, time(NULL), author, time(NULL), logmsg, repo);
1332 free(new_tree_id);
1333 return err;