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 err = got_error_from_errno2("opendir", path_packdir);
657 goto done;
660 while ((dent = readdir(packdir)) != NULL) {
661 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
662 continue;
664 if (asprintf(&path_packidx, "%s/%s", path_packdir,
665 dent->d_name) == -1) {
666 err = got_error_from_errno("asprintf");
667 goto done;
670 err = got_packidx_open(packidx, path_packidx, 0);
671 free(path_packidx);
672 if (err)
673 goto done;
675 *idx = got_packidx_get_object_idx(*packidx, id);
676 if (*idx != -1) {
677 err = NULL; /* found the object */
678 err = got_repo_cache_packidx(repo, *packidx);
679 goto done;
682 err = got_packidx_close(*packidx);
683 *packidx = NULL;
684 if (err)
685 goto done;
688 err = got_error_no_obj(id);
689 done:
690 free(path_packdir);
691 if (packdir && closedir(packdir) != 0 && err == NULL)
692 err = got_error_from_errno("closedir");
693 return err;
696 static const struct got_error *
697 read_packfile_hdr(int fd, struct got_packidx *packidx)
699 const struct got_error *err = NULL;
700 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
701 struct got_packfile_hdr hdr;
702 ssize_t n;
704 n = read(fd, &hdr, sizeof(hdr));
705 if (n < 0)
706 return got_error_from_errno("read");
707 if (n != sizeof(hdr))
708 return got_error(GOT_ERR_BAD_PACKFILE);
710 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
711 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
712 betoh32(hdr.nobjects) != totobj)
713 err = got_error(GOT_ERR_BAD_PACKFILE);
715 return err;
718 static const struct got_error *
719 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
721 const struct got_error *err = NULL;
723 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
724 if (*fd == -1)
725 return got_error_from_errno2("open", path_packfile);
727 if (packidx) {
728 err = read_packfile_hdr(*fd, packidx);
729 if (err) {
730 close(*fd);
731 *fd = -1;
735 return err;
738 const struct got_error *
739 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
740 const char *path_packfile, struct got_packidx *packidx)
742 const struct got_error *err = NULL;
743 struct got_pack *pack = NULL;
744 struct stat sb;
745 int i;
747 if (packp)
748 *packp = NULL;
750 for (i = 0; i < nitems(repo->packs); i++) {
751 pack = &repo->packs[i];
752 if (pack->path_packfile == NULL)
753 break;
754 if (strcmp(pack->path_packfile, path_packfile) == 0)
755 return NULL;
758 if (i == nitems(repo->packs) - 1) {
759 err = got_pack_close(&repo->packs[i - 1]);
760 if (err)
761 return err;
762 memmove(&repo->packs[1], &repo->packs[0],
763 sizeof(repo->packs) - sizeof(repo->packs[0]));
764 i = 0;
767 pack = &repo->packs[i];
769 pack->path_packfile = strdup(path_packfile);
770 if (pack->path_packfile == NULL) {
771 err = got_error_from_errno("strdup");
772 goto done;
775 err = open_packfile(&pack->fd, path_packfile, packidx);
776 if (err)
777 goto done;
779 if (fstat(pack->fd, &sb) != 0) {
780 err = got_error_from_errno("fstat");
781 goto done;
783 pack->filesize = sb.st_size;
785 pack->privsep_child = NULL;
787 #ifndef GOT_PACK_NO_MMAP
788 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
789 pack->fd, 0);
790 if (pack->map == MAP_FAILED) {
791 if (errno != ENOMEM) {
792 err = got_error_from_errno("mmap");
793 goto done;
795 pack->map = NULL; /* fall back to read(2) */
797 #endif
798 done:
799 if (err) {
800 if (pack) {
801 free(pack->path_packfile);
802 memset(pack, 0, sizeof(*pack));
804 } else if (packp)
805 *packp = pack;
806 return err;
809 struct got_pack *
810 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
812 struct got_pack *pack = NULL;
813 int i;
815 for (i = 0; i < nitems(repo->packs); i++) {
816 pack = &repo->packs[i];
817 if (pack->path_packfile == NULL)
818 break;
819 if (strcmp(pack->path_packfile, path_packfile) == 0)
820 return pack;
823 return NULL;
826 const struct got_error *
827 got_repo_init(const char *repo_path)
829 const struct got_error *err = NULL;
830 const char *dirnames[] = {
831 GOT_OBJECTS_DIR,
832 GOT_OBJECTS_PACK_DIR,
833 GOT_REFS_DIR,
834 };
835 const char *description_str = "Unnamed repository; "
836 "edit this file 'description' to name the repository.";
837 const char *headref_str = "ref: refs/heads/master";
838 const char *gitconfig_str = "[core]\n"
839 "\trepositoryformatversion = 0\n"
840 "\tfilemode = true\n"
841 "\tbare = true\n";
842 char *path;
843 int i;
845 if (!got_path_dir_is_empty(repo_path))
846 return got_error(GOT_ERR_DIR_NOT_EMPTY);
848 for (i = 0; i < nitems(dirnames); i++) {
849 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
850 return got_error_from_errno("asprintf");
852 err = got_path_mkdir(path);
853 free(path);
854 if (err)
855 return err;
858 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
859 return got_error_from_errno("asprintf");
860 err = got_path_create_file(path, description_str);
861 free(path);
862 if (err)
863 return err;
865 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
866 return got_error_from_errno("asprintf");
867 err = got_path_create_file(path, headref_str);
868 free(path);
869 if (err)
870 return err;
872 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
873 return got_error_from_errno("asprintf");
874 err = got_path_create_file(path, gitconfig_str);
875 free(path);
876 if (err)
877 return err;
879 return NULL;
882 static const struct got_error *
883 match_packed_object(struct got_object_id **unique_id,
884 struct got_repository *repo, const char *id_str_prefix, int obj_type)
886 const struct got_error *err = NULL;
887 char *path_packdir;
888 DIR *packdir;
889 struct dirent *dent;
890 char *path_packidx;
891 struct got_object_id_queue matched_ids;
893 SIMPLEQ_INIT(&matched_ids);
895 path_packdir = got_repo_get_path_objects_pack(repo);
896 if (path_packdir == NULL)
897 return got_error_from_errno("got_repo_get_path_objects_pack");
899 packdir = opendir(path_packdir);
900 if (packdir == NULL) {
901 err = got_error_from_errno2("opendir", path_packdir);
902 goto done;
905 while ((dent = readdir(packdir)) != NULL) {
906 struct got_packidx *packidx;
907 struct got_object_qid *qid;
910 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
911 continue;
913 if (asprintf(&path_packidx, "%s/%s", path_packdir,
914 dent->d_name) == -1) {
915 err = got_error_from_errno("asprintf");
916 break;
919 err = got_packidx_open(&packidx, path_packidx, 0);
920 free(path_packidx);
921 if (err)
922 break;
924 err = got_packidx_match_id_str_prefix(&matched_ids,
925 packidx, id_str_prefix);
926 if (err) {
927 got_packidx_close(packidx);
928 break;
930 err = got_packidx_close(packidx);
931 if (err)
932 break;
934 SIMPLEQ_FOREACH(qid, &matched_ids, entry) {
935 if (obj_type != GOT_OBJ_TYPE_ANY) {
936 int matched_type;
937 err = got_object_get_type(&matched_type, repo,
938 qid->id);
939 if (err)
940 goto done;
941 if (matched_type != obj_type)
942 continue;
944 if (*unique_id == NULL) {
945 *unique_id = got_object_id_dup(qid->id);
946 if (*unique_id == NULL) {
947 err = got_error_from_errno("malloc");
948 goto done;
950 } else {
951 err = got_error(GOT_ERR_AMBIGUOUS_ID);
952 goto done;
956 done:
957 got_object_id_queue_free(&matched_ids);
958 free(path_packdir);
959 if (packdir && closedir(packdir) != 0 && err == NULL)
960 err = got_error_from_errno("closedir");
961 if (err) {
962 free(*unique_id);
963 *unique_id = NULL;
965 return err;
968 static const struct got_error *
969 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
970 const char *object_dir, const char *id_str_prefix, int obj_type,
971 struct got_repository *repo)
973 const struct got_error *err = NULL;
974 char *path;
975 DIR *dir = NULL;
976 struct dirent *dent;
977 struct got_object_id id;
979 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
980 err = got_error_from_errno("asprintf");
981 goto done;
984 dir = opendir(path);
985 if (dir == NULL) {
986 if (errno == ENOENT) {
987 err = NULL;
988 goto done;
990 err = got_error_from_errno2("opendir", path);
991 goto done;
993 while ((dent = readdir(dir)) != NULL) {
994 char *id_str;
995 int cmp;
997 if (strcmp(dent->d_name, ".") == 0 ||
998 strcmp(dent->d_name, "..") == 0)
999 continue;
1001 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1002 err = got_error_from_errno("asprintf");
1003 goto done;
1006 if (!got_parse_sha1_digest(id.sha1, id_str))
1007 continue;
1010 * Directory entries do not necessarily appear in
1011 * sorted order, so we must iterate over all of them.
1013 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1014 if (cmp != 0) {
1015 free(id_str);
1016 continue;
1019 if (*unique_id == NULL) {
1020 if (obj_type != GOT_OBJ_TYPE_ANY) {
1021 int matched_type;
1022 err = got_object_get_type(&matched_type, repo,
1023 &id);
1024 if (err)
1025 goto done;
1026 if (matched_type != obj_type)
1027 continue;
1029 *unique_id = got_object_id_dup(&id);
1030 if (*unique_id == NULL) {
1031 err = got_error_from_errno("got_object_id_dup");
1032 free(id_str);
1033 goto done;
1035 } else {
1036 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1037 free(id_str);
1038 goto done;
1041 done:
1042 if (dir && closedir(dir) != 0 && err == NULL)
1043 err = got_error_from_errno("closedir");
1044 if (err) {
1045 free(*unique_id);
1046 *unique_id = NULL;
1048 free(path);
1049 return err;
1052 const struct got_error *
1053 got_repo_match_object_id_prefix(struct got_object_id **id,
1054 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1056 const struct got_error *err = NULL;
1057 char *path_objects = got_repo_get_path_objects(repo);
1058 char *object_dir = NULL;
1059 size_t len;
1060 int i;
1062 *id = NULL;
1064 for (i = 0; i < strlen(id_str_prefix); i++) {
1065 if (isxdigit((unsigned char)id_str_prefix[i]))
1066 continue;
1067 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
1070 len = strlen(id_str_prefix);
1071 if (len >= 2) {
1072 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1073 if (err)
1074 goto done;
1075 object_dir = strndup(id_str_prefix, 2);
1076 if (object_dir == NULL) {
1077 err = got_error_from_errno("strdup");
1078 goto done;
1080 err = match_loose_object(id, path_objects, object_dir,
1081 id_str_prefix, obj_type, repo);
1082 } else if (len == 1) {
1083 int i;
1084 for (i = 0; i < 0xf; i++) {
1085 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1086 == -1) {
1087 err = got_error_from_errno("asprintf");
1088 goto done;
1090 err = match_packed_object(id, repo, object_dir,
1091 obj_type);
1092 if (err)
1093 goto done;
1094 err = match_loose_object(id, path_objects, object_dir,
1095 id_str_prefix, obj_type, repo);
1096 if (err)
1097 goto done;
1099 } else {
1100 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
1101 goto done;
1103 done:
1104 free(object_dir);
1105 if (err) {
1106 free(*id);
1107 *id = NULL;
1108 } else if (*id == NULL)
1109 err = got_error(GOT_ERR_NO_OBJ);
1111 return err;
1114 static const struct got_error *
1115 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1116 const char *name, mode_t mode, struct got_object_id *blob_id)
1118 const struct got_error *err = NULL;
1120 *new_te = NULL;
1122 *new_te = calloc(1, sizeof(**new_te));
1123 if (*new_te == NULL)
1124 return got_error_from_errno("calloc");
1126 (*new_te)->name = strdup(name);
1127 if ((*new_te)->name == NULL) {
1128 err = got_error_from_errno("strdup");
1129 goto done;
1132 (*new_te)->mode = S_IFREG | (mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
1133 (*new_te)->id = blob_id;
1134 done:
1135 if (err && *new_te) {
1136 got_object_tree_entry_close(*new_te);
1137 *new_te = NULL;
1139 return err;
1142 static const struct got_error *
1143 import_file(struct got_tree_entry **new_te, struct dirent *de,
1144 const char *path, struct got_repository *repo)
1146 const struct got_error *err;
1147 struct got_object_id *blob_id = NULL;
1148 char *filepath;
1149 struct stat sb;
1151 if (asprintf(&filepath, "%s%s%s", path,
1152 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1153 return got_error_from_errno("asprintf");
1155 if (lstat(filepath, &sb) != 0) {
1156 err = got_error_from_errno2("lstat", path);
1157 goto done;
1160 err = got_object_blob_create(&blob_id, filepath, repo);
1161 if (err)
1162 goto done;
1164 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1165 blob_id);
1166 done:
1167 free(filepath);
1168 if (err)
1169 free(blob_id);
1170 return err;
1173 static const struct got_error *
1174 insert_tree_entry(struct got_tree_entry *new_te,
1175 struct got_pathlist_head *paths)
1177 const struct got_error *err = NULL;
1178 struct got_pathlist_entry *new_pe;
1180 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1181 if (err)
1182 return err;
1183 if (new_pe == NULL)
1184 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1185 return NULL;
1188 static const struct got_error *write_tree(struct got_object_id **,
1189 const char *, struct got_pathlist_head *, struct got_repository *,
1190 got_repo_import_cb progress_cb, void *progress_arg);
1192 static const struct got_error *
1193 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1194 const char *path, struct got_pathlist_head *ignores,
1195 struct got_repository *repo,
1196 got_repo_import_cb progress_cb, void *progress_arg)
1198 const struct got_error *err;
1199 char *subdirpath;
1201 if (asprintf(&subdirpath, "%s%s%s", path,
1202 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1203 return got_error_from_errno("asprintf");
1205 (*new_te) = calloc(1, sizeof(**new_te));
1206 (*new_te)->mode = S_IFDIR;
1207 (*new_te)->name = strdup(de->d_name);
1208 if ((*new_te)->name == NULL) {
1209 err = got_error_from_errno("strdup");
1210 goto done;
1213 err = write_tree(&(*new_te)->id, subdirpath, ignores, repo,
1214 progress_cb, progress_arg);
1215 done:
1216 free(subdirpath);
1217 if (err) {
1218 got_object_tree_entry_close(*new_te);
1219 *new_te = NULL;
1221 return err;
1224 static const struct got_error *
1225 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1226 struct got_pathlist_head *ignores, struct got_repository *repo,
1227 got_repo_import_cb progress_cb, void *progress_arg)
1229 const struct got_error *err = NULL;
1230 DIR *dir;
1231 struct dirent *de;
1232 struct got_tree_entries new_tree_entries;
1233 struct got_tree_entry *new_te = NULL;
1234 struct got_pathlist_head paths;
1235 struct got_pathlist_entry *pe;
1236 char *name;
1238 *new_tree_id = NULL;
1240 TAILQ_INIT(&paths);
1241 new_tree_entries.nentries = 0;
1242 SIMPLEQ_INIT(&new_tree_entries.head);
1244 dir = opendir(path_dir);
1245 if (dir == NULL) {
1246 err = got_error_from_errno2("opendir", path_dir);
1247 goto done;
1250 while ((de = readdir(dir)) != NULL) {
1251 int ignore = 0;
1253 if (strcmp(de->d_name, ".") == 0 ||
1254 strcmp(de->d_name, "..") == 0)
1255 continue;
1257 TAILQ_FOREACH(pe, ignores, entry) {
1258 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1259 ignore = 1;
1260 break;
1263 if (ignore)
1264 continue;
1265 if (de->d_type == DT_DIR) {
1266 err = import_subdir(&new_te, de, path_dir,
1267 ignores, repo, progress_cb, progress_arg);
1268 if (err)
1269 goto done;
1270 } else if (de->d_type == DT_REG) {
1271 err = import_file(&new_te, de, path_dir, repo);
1272 if (err)
1273 goto done;
1274 } else
1275 continue;
1277 err = insert_tree_entry(new_te, &paths);
1278 if (err)
1279 goto done;
1282 TAILQ_FOREACH(pe, &paths, entry) {
1283 struct got_tree_entry *te = pe->data;
1284 char *path;
1285 new_tree_entries.nentries++;
1286 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
1287 if (!S_ISREG(te->mode))
1288 continue;
1289 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1290 err = got_error_from_errno("asprintf");
1291 goto done;
1293 err = (*progress_cb)(progress_arg, path);
1294 free(path);
1295 if (err)
1296 goto done;
1299 name = basename(path_dir);
1300 if (name == NULL) {
1301 err = got_error_from_errno("basename");
1302 goto done;
1305 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
1306 done:
1307 if (dir)
1308 closedir(dir);
1309 got_object_tree_entries_close(&new_tree_entries);
1310 got_pathlist_free(&paths);
1311 return err;
1314 const struct got_error *
1315 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1316 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1317 struct got_repository *repo, got_repo_import_cb progress_cb,
1318 void *progress_arg)
1320 const struct got_error *err;
1321 struct got_object_id *new_tree_id;
1323 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1324 progress_cb, progress_arg);
1325 if (err)
1326 return err;
1328 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1329 author, time(NULL), author, time(NULL), logmsg, repo);
1330 free(new_tree_id);
1331 return err;