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_cancel.h"
45 #include "got_worktree.h"
46 #include "got_object.h"
48 #include "got_lib_delta.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_object.h"
51 #include "got_lib_object_parse.h"
52 #include "got_lib_object_create.h"
53 #include "got_lib_pack.h"
54 #include "got_lib_privsep.h"
55 #include "got_lib_worktree.h"
56 #include "got_lib_sha1.h"
57 #include "got_lib_object_cache.h"
58 #include "got_lib_repository.h"
60 #ifndef nitems
61 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
62 #endif
64 #define GOT_GIT_DIR ".git"
66 /* Mandatory files and directories inside the git directory. */
67 #define GOT_OBJECTS_DIR "objects"
68 #define GOT_REFS_DIR "refs"
69 #define GOT_HEAD_FILE "HEAD"
71 /* Other files and directories inside the git directory. */
72 #define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
73 #define GOT_ORIG_HEAD_FILE "ORIG_HEAD"
74 #define GOT_OBJECTS_PACK_DIR "objects/pack"
75 #define GOT_PACKED_REFS_FILE "packed-refs"
77 const char *
78 got_repo_get_path(struct got_repository *repo)
79 {
80 return repo->path;
81 }
83 const char *
84 got_repo_get_path_git_dir(struct got_repository *repo)
85 {
86 return repo->path_git_dir;
87 }
89 int
90 got_repo_is_bare(struct got_repository *repo)
91 {
92 return (strcmp(repo->path, repo->path_git_dir) == 0);
93 }
95 static char *
96 get_path_git_child(struct got_repository *repo, const char *basename)
97 {
98 char *path_child;
100 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
101 basename) == -1)
102 return NULL;
104 return path_child;
107 char *
108 got_repo_get_path_objects(struct got_repository *repo)
110 return get_path_git_child(repo, GOT_OBJECTS_DIR);
113 char *
114 got_repo_get_path_objects_pack(struct got_repository *repo)
116 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
119 char *
120 got_repo_get_path_refs(struct got_repository *repo)
122 return get_path_git_child(repo, GOT_REFS_DIR);
125 char *
126 got_repo_get_path_packed_refs(struct got_repository *repo)
128 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
131 static char *
132 get_path_head(struct got_repository *repo)
134 return get_path_git_child(repo, GOT_HEAD_FILE);
137 static int
138 is_git_repo(struct got_repository *repo)
140 const char *path_git = got_repo_get_path_git_dir(repo);
141 char *path_objects = got_repo_get_path_objects(repo);
142 char *path_refs = got_repo_get_path_refs(repo);
143 char *path_head = get_path_head(repo);
144 int ret = 0;
145 struct stat sb;
146 struct got_reference *head_ref;
148 if (lstat(path_git, &sb) == -1)
149 goto done;
150 if (!S_ISDIR(sb.st_mode))
151 goto done;
153 if (lstat(path_objects, &sb) == -1)
154 goto done;
155 if (!S_ISDIR(sb.st_mode))
156 goto done;
158 if (lstat(path_refs, &sb) == -1)
159 goto done;
160 if (!S_ISDIR(sb.st_mode))
161 goto done;
163 if (lstat(path_head, &sb) == -1)
164 goto done;
165 if (!S_ISREG(sb.st_mode))
166 goto done;
168 /* Check if the HEAD reference can be opened. */
169 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
170 goto done;
171 got_ref_close(head_ref);
173 ret = 1;
174 done:
175 free(path_objects);
176 free(path_refs);
177 free(path_head);
178 return ret;
182 const struct got_error *
183 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
184 struct got_object *obj)
186 #ifndef GOT_NO_OBJ_CACHE
187 const struct got_error *err = NULL;
188 err = got_object_cache_add(&repo->objcache, id, obj);
189 if (err) {
190 if (err->code == GOT_ERR_OBJ_EXISTS ||
191 err->code == GOT_ERR_OBJ_TOO_LARGE)
192 err = NULL;
193 return err;
195 obj->refcnt++;
196 #endif
197 return NULL;
200 struct got_object *
201 got_repo_get_cached_object(struct got_repository *repo,
202 struct got_object_id *id)
204 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
207 const struct got_error *
208 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
209 struct got_tree_object *tree)
211 #ifndef GOT_NO_OBJ_CACHE
212 const struct got_error *err = NULL;
213 err = got_object_cache_add(&repo->treecache, id, tree);
214 if (err) {
215 if (err->code == GOT_ERR_OBJ_EXISTS ||
216 err->code == GOT_ERR_OBJ_TOO_LARGE)
217 err = NULL;
218 return err;
220 tree->refcnt++;
221 #endif
222 return NULL;
225 struct got_tree_object *
226 got_repo_get_cached_tree(struct got_repository *repo,
227 struct got_object_id *id)
229 return (struct got_tree_object *)got_object_cache_get(
230 &repo->treecache, id);
233 const struct got_error *
234 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
235 struct got_commit_object *commit)
237 #ifndef GOT_NO_OBJ_CACHE
238 const struct got_error *err = NULL;
239 err = got_object_cache_add(&repo->commitcache, id, commit);
240 if (err) {
241 if (err->code == GOT_ERR_OBJ_EXISTS ||
242 err->code == GOT_ERR_OBJ_TOO_LARGE)
243 err = NULL;
244 return err;
246 commit->refcnt++;
247 #endif
248 return NULL;
251 struct got_commit_object *
252 got_repo_get_cached_commit(struct got_repository *repo,
253 struct got_object_id *id)
255 return (struct got_commit_object *)got_object_cache_get(
256 &repo->commitcache, id);
259 const struct got_error *
260 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
261 struct got_tag_object *tag)
263 #ifndef GOT_NO_OBJ_CACHE
264 const struct got_error *err = NULL;
265 err = got_object_cache_add(&repo->tagcache, id, tag);
266 if (err) {
267 if (err->code == GOT_ERR_OBJ_EXISTS ||
268 err->code == GOT_ERR_OBJ_TOO_LARGE)
269 err = NULL;
270 return err;
272 tag->refcnt++;
273 #endif
274 return NULL;
277 struct got_tag_object *
278 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
280 return (struct got_tag_object *)got_object_cache_get(
281 &repo->tagcache, id);
284 const struct got_error *
285 open_repo(struct got_repository *repo, const char *path)
287 const struct got_error *err = NULL;
289 /* bare git repository? */
290 repo->path_git_dir = strdup(path);
291 if (repo->path_git_dir == NULL)
292 return got_error_from_errno("strdup");
293 if (is_git_repo(repo)) {
294 repo->path = strdup(repo->path_git_dir);
295 if (repo->path == NULL) {
296 err = got_error_from_errno("strdup");
297 goto done;
299 return NULL;
302 /* git repository with working tree? */
303 free(repo->path_git_dir);
304 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
305 err = got_error_from_errno("asprintf");
306 goto done;
308 if (is_git_repo(repo)) {
309 repo->path = strdup(path);
310 if (repo->path == NULL) {
311 err = got_error_from_errno("strdup");
312 goto done;
314 return NULL;
317 err = got_error(GOT_ERR_NOT_GIT_REPO);
318 done:
319 if (err) {
320 free(repo->path);
321 repo->path = NULL;
322 free(repo->path_git_dir);
323 repo->path_git_dir = NULL;
325 return err;
328 const struct got_error *
329 got_repo_open(struct got_repository **repop, const char *path)
331 struct got_repository *repo = NULL;
332 const struct got_error *err = NULL;
333 char *abspath;
334 int i, tried_root = 0;
336 *repop = NULL;
338 if (got_path_is_absolute(path))
339 abspath = strdup(path);
340 else
341 abspath = got_path_get_absolute(path);
342 if (abspath == NULL)
343 return got_error(GOT_ERR_BAD_PATH);
345 repo = calloc(1, sizeof(*repo));
346 if (repo == NULL) {
347 err = got_error_from_errno("calloc");
348 goto done;
351 for (i = 0; i < nitems(repo->privsep_children); i++) {
352 memset(&repo->privsep_children[i], 0,
353 sizeof(repo->privsep_children[0]));
354 repo->privsep_children[i].imsg_fd = -1;
357 err = got_object_cache_init(&repo->objcache,
358 GOT_OBJECT_CACHE_TYPE_OBJ);
359 if (err)
360 goto done;
361 err = got_object_cache_init(&repo->treecache,
362 GOT_OBJECT_CACHE_TYPE_TREE);
363 if (err)
364 goto done;
365 err = got_object_cache_init(&repo->commitcache,
366 GOT_OBJECT_CACHE_TYPE_COMMIT);
367 if (err)
368 goto done;
369 err = got_object_cache_init(&repo->tagcache,
370 GOT_OBJECT_CACHE_TYPE_TAG);
371 if (err)
372 goto done;
374 path = realpath(abspath, NULL);
375 if (path == NULL) {
376 err = got_error_from_errno2("realpath", abspath);
377 goto done;
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 return err;
406 const struct got_error *
407 got_repo_close(struct got_repository *repo)
409 const struct got_error *err = NULL, *child_err;
410 int i;
412 for (i = 0; i < nitems(repo->packidx_cache); i++) {
413 if (repo->packidx_cache[i] == NULL)
414 break;
415 got_packidx_close(repo->packidx_cache[i]);
418 for (i = 0; i < nitems(repo->packs); i++) {
419 if (repo->packs[i].path_packfile == NULL)
420 break;
421 got_pack_close(&repo->packs[i]);
424 free(repo->path);
425 free(repo->path_git_dir);
427 got_object_cache_close(&repo->objcache);
428 got_object_cache_close(&repo->treecache);
429 got_object_cache_close(&repo->commitcache);
430 got_object_cache_close(&repo->tagcache);
432 for (i = 0; i < nitems(repo->privsep_children); i++) {
433 if (repo->privsep_children[i].imsg_fd == -1)
434 continue;
435 imsg_clear(repo->privsep_children[i].ibuf);
436 free(repo->privsep_children[i].ibuf);
437 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
438 child_err = got_privsep_wait_for_child(
439 repo->privsep_children[i].pid);
440 if (child_err && err == NULL)
441 err = child_err;
442 if (close(repo->privsep_children[i].imsg_fd) != 0 &&
443 err == NULL)
444 err = got_error_from_errno("close");
446 free(repo);
448 return err;
451 const struct got_error *
452 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
453 const char *input_path, int check_disk)
455 const struct got_error *err = NULL;
456 const char *repo_abspath = NULL;
457 size_t repolen, cwdlen, len;
458 char *cwd, *canonpath, *path = NULL;
460 *in_repo_path = NULL;
462 cwd = getcwd(NULL, 0);
463 if (cwd == NULL)
464 return got_error_from_errno("getcwd");
466 canonpath = strdup(input_path);
467 if (canonpath == NULL) {
468 err = got_error_from_errno("strdup");
469 goto done;
471 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
472 if (err)
473 goto done;
475 repo_abspath = got_repo_get_path(repo);
477 if (!check_disk || canonpath[0] == '\0') {
478 path = strdup(canonpath);
479 if (path == NULL) {
480 err = got_error_from_errno("strdup");
481 goto done;
483 } else {
484 int is_repo_child = 0, is_cwd_child = 0;
486 path = realpath(canonpath, NULL);
487 if (path == NULL) {
488 if (errno != ENOENT) {
489 err = got_error_from_errno2("realpath",
490 canonpath);
491 goto done;
493 /*
494 * Path is not on disk.
495 * Assume it is already relative to repository root.
496 */
497 path = strdup(canonpath);
498 if (path == NULL) {
499 err = got_error_from_errno("strdup");
500 goto done;
504 repolen = strlen(repo_abspath);
505 cwdlen = strlen(cwd);
506 len = strlen(path);
508 if (len > repolen && strncmp(path, repo_abspath, repolen) == 0)
509 is_repo_child = 1;
510 if (len > cwdlen && strncmp(path, cwd, cwdlen) == 0)
511 is_cwd_child = 1;
513 if (strcmp(path, repo_abspath) == 0) {
514 free(path);
515 path = strdup("");
516 if (path == NULL) {
517 err = got_error_from_errno("strdup");
518 goto done;
520 } else if (is_repo_child && is_cwd_child) {
521 char *child;
522 /* Strip common prefix with repository path. */
523 err = got_path_skip_common_ancestor(&child,
524 repo_abspath, path);
525 if (err)
526 goto done;
527 free(path);
528 path = child;
529 } else if (is_repo_child) {
530 /* Matched an on-disk path inside repository. */
531 if (got_repo_is_bare(repo)) {
532 /*
533 * Matched an on-disk path inside repository
534 * database. Treat as repository-relative.
535 */
536 } else {
537 char *child;
538 /* Strip common prefix with repository path. */
539 err = got_path_skip_common_ancestor(&child,
540 repo_abspath, path);
541 if (err)
542 goto done;
543 free(path);
544 path = child;
546 } else if (is_cwd_child) {
547 char *child;
548 /* Strip common prefix with cwd. */
549 err = got_path_skip_common_ancestor(&child, cwd,
550 path);
551 if (err)
552 goto done;
553 free(path);
554 path = child;
555 } else {
556 /*
557 * Matched unrelated on-disk path.
558 * Treat it as repository-relative.
559 */
563 /* Make in-repository path absolute */
564 if (path[0] != '/') {
565 char *abspath;
566 if (asprintf(&abspath, "/%s", path) == -1) {
567 err = got_error_from_errno("asprintf");
568 goto done;
570 free(path);
571 path = abspath;
574 done:
575 free(cwd);
576 free(canonpath);
577 if (err)
578 free(path);
579 else
580 *in_repo_path = path;
581 return err;
584 const struct got_error *
585 got_repo_cache_packidx(struct got_repository *repo, struct got_packidx *packidx)
587 const struct got_error *err = NULL;
588 int i;
590 for (i = 0; i < nitems(repo->packidx_cache); i++) {
591 if (repo->packidx_cache[i] == NULL)
592 break;
594 if (i == nitems(repo->packidx_cache)) {
595 err = got_packidx_close(repo->packidx_cache[i - 1]);
596 if (err)
597 return err;
600 /*
601 * Insert the new pack index at the front so it will
602 * be searched first in the future.
603 */
604 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
605 sizeof(repo->packidx_cache) -
606 sizeof(repo->packidx_cache[0]));
607 repo->packidx_cache[0] = packidx;
609 return NULL;
612 static int
613 is_packidx_filename(const char *name, size_t len)
615 if (len != GOT_PACKIDX_NAMELEN)
616 return 0;
618 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
619 return 0;
621 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
622 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
623 return 0;
625 return 1;
628 const struct got_error *
629 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
630 struct got_repository *repo, struct got_object_id *id)
632 const struct got_error *err;
633 char *path_packdir;
634 DIR *packdir;
635 struct dirent *dent;
636 char *path_packidx;
637 int i;
639 /* Search pack index cache. */
640 for (i = 0; i < nitems(repo->packidx_cache); i++) {
641 if (repo->packidx_cache[i] == NULL)
642 break;
643 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
644 if (*idx != -1) {
645 *packidx = repo->packidx_cache[i];
646 return NULL;
649 /* No luck. Search the filesystem. */
651 path_packdir = got_repo_get_path_objects_pack(repo);
652 if (path_packdir == NULL)
653 return got_error_from_errno("got_repo_get_path_objects_pack");
655 packdir = opendir(path_packdir);
656 if (packdir == NULL) {
657 if (errno == ENOENT)
658 err = got_error_no_obj(id);
659 else
660 err = got_error_from_errno2("opendir", path_packdir);
661 goto done;
664 while ((dent = readdir(packdir)) != NULL) {
665 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
666 continue;
668 if (asprintf(&path_packidx, "%s/%s", path_packdir,
669 dent->d_name) == -1) {
670 err = got_error_from_errno("asprintf");
671 goto done;
674 err = got_packidx_open(packidx, path_packidx, 0);
675 free(path_packidx);
676 if (err)
677 goto done;
679 *idx = got_packidx_get_object_idx(*packidx, id);
680 if (*idx != -1) {
681 err = NULL; /* found the object */
682 err = got_repo_cache_packidx(repo, *packidx);
683 goto done;
686 err = got_packidx_close(*packidx);
687 *packidx = NULL;
688 if (err)
689 goto done;
692 err = got_error_no_obj(id);
693 done:
694 free(path_packdir);
695 if (packdir && closedir(packdir) != 0 && err == NULL)
696 err = got_error_from_errno("closedir");
697 return err;
700 static const struct got_error *
701 read_packfile_hdr(int fd, struct got_packidx *packidx)
703 const struct got_error *err = NULL;
704 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
705 struct got_packfile_hdr hdr;
706 ssize_t n;
708 n = read(fd, &hdr, sizeof(hdr));
709 if (n < 0)
710 return got_error_from_errno("read");
711 if (n != sizeof(hdr))
712 return got_error(GOT_ERR_BAD_PACKFILE);
714 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
715 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
716 betoh32(hdr.nobjects) != totobj)
717 err = got_error(GOT_ERR_BAD_PACKFILE);
719 return err;
722 static const struct got_error *
723 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
725 const struct got_error *err = NULL;
727 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
728 if (*fd == -1)
729 return got_error_from_errno2("open", path_packfile);
731 if (packidx) {
732 err = read_packfile_hdr(*fd, packidx);
733 if (err) {
734 close(*fd);
735 *fd = -1;
739 return err;
742 const struct got_error *
743 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
744 const char *path_packfile, struct got_packidx *packidx)
746 const struct got_error *err = NULL;
747 struct got_pack *pack = NULL;
748 struct stat sb;
749 int i;
751 if (packp)
752 *packp = NULL;
754 for (i = 0; i < nitems(repo->packs); i++) {
755 pack = &repo->packs[i];
756 if (pack->path_packfile == NULL)
757 break;
758 if (strcmp(pack->path_packfile, path_packfile) == 0)
759 return NULL;
762 if (i == nitems(repo->packs) - 1) {
763 err = got_pack_close(&repo->packs[i - 1]);
764 if (err)
765 return err;
766 memmove(&repo->packs[1], &repo->packs[0],
767 sizeof(repo->packs) - sizeof(repo->packs[0]));
768 i = 0;
771 pack = &repo->packs[i];
773 pack->path_packfile = strdup(path_packfile);
774 if (pack->path_packfile == NULL) {
775 err = got_error_from_errno("strdup");
776 goto done;
779 err = open_packfile(&pack->fd, path_packfile, packidx);
780 if (err)
781 goto done;
783 if (fstat(pack->fd, &sb) != 0) {
784 err = got_error_from_errno("fstat");
785 goto done;
787 pack->filesize = sb.st_size;
789 pack->privsep_child = NULL;
791 #ifndef GOT_PACK_NO_MMAP
792 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
793 pack->fd, 0);
794 if (pack->map == MAP_FAILED) {
795 if (errno != ENOMEM) {
796 err = got_error_from_errno("mmap");
797 goto done;
799 pack->map = NULL; /* fall back to read(2) */
801 #endif
802 done:
803 if (err) {
804 if (pack) {
805 free(pack->path_packfile);
806 memset(pack, 0, sizeof(*pack));
808 } else if (packp)
809 *packp = pack;
810 return err;
813 struct got_pack *
814 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
816 struct got_pack *pack = NULL;
817 int i;
819 for (i = 0; i < nitems(repo->packs); i++) {
820 pack = &repo->packs[i];
821 if (pack->path_packfile == NULL)
822 break;
823 if (strcmp(pack->path_packfile, path_packfile) == 0)
824 return pack;
827 return NULL;
830 const struct got_error *
831 got_repo_init(const char *repo_path)
833 const struct got_error *err = NULL;
834 const char *dirnames[] = {
835 GOT_OBJECTS_DIR,
836 GOT_OBJECTS_PACK_DIR,
837 GOT_REFS_DIR,
838 };
839 const char *description_str = "Unnamed repository; "
840 "edit this file 'description' to name the repository.";
841 const char *headref_str = "ref: refs/heads/master";
842 const char *gitconfig_str = "[core]\n"
843 "\trepositoryformatversion = 0\n"
844 "\tfilemode = true\n"
845 "\tbare = true\n";
846 char *path;
847 int i;
849 if (!got_path_dir_is_empty(repo_path))
850 return got_error(GOT_ERR_DIR_NOT_EMPTY);
852 for (i = 0; i < nitems(dirnames); i++) {
853 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
854 return got_error_from_errno("asprintf");
856 err = got_path_mkdir(path);
857 free(path);
858 if (err)
859 return err;
862 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
863 return got_error_from_errno("asprintf");
864 err = got_path_create_file(path, description_str);
865 free(path);
866 if (err)
867 return err;
869 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
870 return got_error_from_errno("asprintf");
871 err = got_path_create_file(path, headref_str);
872 free(path);
873 if (err)
874 return err;
876 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
877 return got_error_from_errno("asprintf");
878 err = got_path_create_file(path, gitconfig_str);
879 free(path);
880 if (err)
881 return err;
883 return NULL;
886 static const struct got_error *
887 match_packed_object(struct got_object_id **unique_id,
888 struct got_repository *repo, const char *id_str_prefix, int obj_type)
890 const struct got_error *err = NULL;
891 char *path_packdir;
892 DIR *packdir;
893 struct dirent *dent;
894 char *path_packidx;
895 struct got_object_id_queue matched_ids;
897 SIMPLEQ_INIT(&matched_ids);
899 path_packdir = got_repo_get_path_objects_pack(repo);
900 if (path_packdir == NULL)
901 return got_error_from_errno("got_repo_get_path_objects_pack");
903 packdir = opendir(path_packdir);
904 if (packdir == NULL) {
905 if (errno != ENOENT)
906 err = got_error_from_errno2("opendir", path_packdir);
907 goto done;
910 while ((dent = readdir(packdir)) != NULL) {
911 struct got_packidx *packidx;
912 struct got_object_qid *qid;
915 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
916 continue;
918 if (asprintf(&path_packidx, "%s/%s", path_packdir,
919 dent->d_name) == -1) {
920 err = got_error_from_errno("asprintf");
921 break;
924 err = got_packidx_open(&packidx, path_packidx, 0);
925 free(path_packidx);
926 if (err)
927 break;
929 err = got_packidx_match_id_str_prefix(&matched_ids,
930 packidx, id_str_prefix);
931 if (err) {
932 got_packidx_close(packidx);
933 break;
935 err = got_packidx_close(packidx);
936 if (err)
937 break;
939 SIMPLEQ_FOREACH(qid, &matched_ids, entry) {
940 if (obj_type != GOT_OBJ_TYPE_ANY) {
941 int matched_type;
942 err = got_object_get_type(&matched_type, repo,
943 qid->id);
944 if (err)
945 goto done;
946 if (matched_type != obj_type)
947 continue;
949 if (*unique_id == NULL) {
950 *unique_id = got_object_id_dup(qid->id);
951 if (*unique_id == NULL) {
952 err = got_error_from_errno("malloc");
953 goto done;
955 } else {
956 err = got_error(GOT_ERR_AMBIGUOUS_ID);
957 goto done;
961 done:
962 got_object_id_queue_free(&matched_ids);
963 free(path_packdir);
964 if (packdir && closedir(packdir) != 0 && err == NULL)
965 err = got_error_from_errno("closedir");
966 if (err) {
967 free(*unique_id);
968 *unique_id = NULL;
970 return err;
973 static const struct got_error *
974 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
975 const char *object_dir, const char *id_str_prefix, int obj_type,
976 struct got_repository *repo)
978 const struct got_error *err = NULL;
979 char *path;
980 DIR *dir = NULL;
981 struct dirent *dent;
982 struct got_object_id id;
984 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
985 err = got_error_from_errno("asprintf");
986 goto done;
989 dir = opendir(path);
990 if (dir == NULL) {
991 if (errno == ENOENT) {
992 err = NULL;
993 goto done;
995 err = got_error_from_errno2("opendir", path);
996 goto done;
998 while ((dent = readdir(dir)) != NULL) {
999 char *id_str;
1000 int cmp;
1002 if (strcmp(dent->d_name, ".") == 0 ||
1003 strcmp(dent->d_name, "..") == 0)
1004 continue;
1006 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1007 err = got_error_from_errno("asprintf");
1008 goto done;
1011 if (!got_parse_sha1_digest(id.sha1, id_str))
1012 continue;
1015 * Directory entries do not necessarily appear in
1016 * sorted order, so we must iterate over all of them.
1018 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1019 if (cmp != 0) {
1020 free(id_str);
1021 continue;
1024 if (*unique_id == NULL) {
1025 if (obj_type != GOT_OBJ_TYPE_ANY) {
1026 int matched_type;
1027 err = got_object_get_type(&matched_type, repo,
1028 &id);
1029 if (err)
1030 goto done;
1031 if (matched_type != obj_type)
1032 continue;
1034 *unique_id = got_object_id_dup(&id);
1035 if (*unique_id == NULL) {
1036 err = got_error_from_errno("got_object_id_dup");
1037 free(id_str);
1038 goto done;
1040 } else {
1041 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1042 free(id_str);
1043 goto done;
1046 done:
1047 if (dir && closedir(dir) != 0 && err == NULL)
1048 err = got_error_from_errno("closedir");
1049 if (err) {
1050 free(*unique_id);
1051 *unique_id = NULL;
1053 free(path);
1054 return err;
1057 const struct got_error *
1058 got_repo_match_object_id_prefix(struct got_object_id **id,
1059 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1061 const struct got_error *err = NULL;
1062 char *path_objects = got_repo_get_path_objects(repo);
1063 char *object_dir = NULL;
1064 size_t len;
1065 int i;
1067 *id = NULL;
1069 for (i = 0; i < strlen(id_str_prefix); i++) {
1070 if (isxdigit((unsigned char)id_str_prefix[i]))
1071 continue;
1072 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
1075 len = strlen(id_str_prefix);
1076 if (len >= 2) {
1077 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1078 if (err)
1079 goto done;
1080 object_dir = strndup(id_str_prefix, 2);
1081 if (object_dir == NULL) {
1082 err = got_error_from_errno("strdup");
1083 goto done;
1085 err = match_loose_object(id, path_objects, object_dir,
1086 id_str_prefix, obj_type, repo);
1087 } else if (len == 1) {
1088 int i;
1089 for (i = 0; i < 0xf; i++) {
1090 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1091 == -1) {
1092 err = got_error_from_errno("asprintf");
1093 goto done;
1095 err = match_packed_object(id, repo, object_dir,
1096 obj_type);
1097 if (err)
1098 goto done;
1099 err = match_loose_object(id, path_objects, object_dir,
1100 id_str_prefix, obj_type, repo);
1101 if (err)
1102 goto done;
1104 } else {
1105 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
1106 goto done;
1108 done:
1109 free(object_dir);
1110 if (err) {
1111 free(*id);
1112 *id = NULL;
1113 } else if (*id == NULL)
1114 err = got_error(GOT_ERR_NO_OBJ);
1116 return err;
1119 const struct got_error *
1120 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1121 int obj_type, struct got_repository *repo)
1123 const struct got_error *err;
1124 struct got_reflist_head refs;
1125 struct got_reflist_entry *re;
1126 struct got_object_id *tag_id;
1128 SIMPLEQ_INIT(&refs);
1129 *tag = NULL;
1131 err = got_ref_list(&refs, repo);
1132 if (err)
1133 return err;
1135 SIMPLEQ_FOREACH(re, &refs, entry) {
1136 const char *refname;
1137 refname = got_ref_get_name(re->ref);
1138 if (got_ref_is_symbolic(re->ref) ||
1139 strncmp("refs/tags/", refname, 10) != 0)
1140 continue;
1141 refname += 10;
1142 if (strcmp(refname, name) != 0)
1143 continue;
1144 err = got_ref_resolve(&tag_id, repo, re->ref);
1145 if (err)
1146 break;
1147 err = got_object_open_as_tag(tag, repo, tag_id);
1148 free(tag_id);
1149 if (err)
1150 break;
1151 if (obj_type == GOT_OBJ_TYPE_ANY ||
1152 got_object_tag_get_object_type(*tag) == obj_type)
1153 break;
1154 got_object_tag_close(*tag);
1155 *tag = NULL;
1158 got_ref_list_free(&refs);
1159 if (err == NULL && *tag == NULL)
1160 err = got_error(GOT_ERR_NO_OBJ);
1161 return err;
1164 static const struct got_error *
1165 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1166 const char *name, mode_t mode, struct got_object_id *blob_id)
1168 const struct got_error *err = NULL;
1170 *new_te = NULL;
1172 *new_te = calloc(1, sizeof(**new_te));
1173 if (*new_te == NULL)
1174 return got_error_from_errno("calloc");
1176 (*new_te)->name = strdup(name);
1177 if ((*new_te)->name == NULL) {
1178 err = got_error_from_errno("strdup");
1179 goto done;
1182 (*new_te)->mode = S_IFREG | (mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
1183 (*new_te)->id = blob_id;
1184 done:
1185 if (err && *new_te) {
1186 got_object_tree_entry_close(*new_te);
1187 *new_te = NULL;
1189 return err;
1192 static const struct got_error *
1193 import_file(struct got_tree_entry **new_te, struct dirent *de,
1194 const char *path, struct got_repository *repo)
1196 const struct got_error *err;
1197 struct got_object_id *blob_id = NULL;
1198 char *filepath;
1199 struct stat sb;
1201 if (asprintf(&filepath, "%s%s%s", path,
1202 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1203 return got_error_from_errno("asprintf");
1205 if (lstat(filepath, &sb) != 0) {
1206 err = got_error_from_errno2("lstat", path);
1207 goto done;
1210 err = got_object_blob_create(&blob_id, filepath, repo);
1211 if (err)
1212 goto done;
1214 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1215 blob_id);
1216 done:
1217 free(filepath);
1218 if (err)
1219 free(blob_id);
1220 return err;
1223 static const struct got_error *
1224 insert_tree_entry(struct got_tree_entry *new_te,
1225 struct got_pathlist_head *paths)
1227 const struct got_error *err = NULL;
1228 struct got_pathlist_entry *new_pe;
1230 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1231 if (err)
1232 return err;
1233 if (new_pe == NULL)
1234 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1235 return NULL;
1238 static const struct got_error *write_tree(struct got_object_id **,
1239 const char *, struct got_pathlist_head *, struct got_repository *,
1240 got_repo_import_cb progress_cb, void *progress_arg);
1242 static const struct got_error *
1243 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1244 const char *path, struct got_pathlist_head *ignores,
1245 struct got_repository *repo,
1246 got_repo_import_cb progress_cb, void *progress_arg)
1248 const struct got_error *err;
1249 char *subdirpath;
1251 if (asprintf(&subdirpath, "%s%s%s", path,
1252 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1253 return got_error_from_errno("asprintf");
1255 (*new_te) = calloc(1, sizeof(**new_te));
1256 (*new_te)->mode = S_IFDIR;
1257 (*new_te)->name = strdup(de->d_name);
1258 if ((*new_te)->name == NULL) {
1259 err = got_error_from_errno("strdup");
1260 goto done;
1263 err = write_tree(&(*new_te)->id, subdirpath, ignores, repo,
1264 progress_cb, progress_arg);
1265 done:
1266 free(subdirpath);
1267 if (err) {
1268 got_object_tree_entry_close(*new_te);
1269 *new_te = NULL;
1271 return err;
1274 static const struct got_error *
1275 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1276 struct got_pathlist_head *ignores, struct got_repository *repo,
1277 got_repo_import_cb progress_cb, void *progress_arg)
1279 const struct got_error *err = NULL;
1280 DIR *dir;
1281 struct dirent *de;
1282 struct got_tree_entries new_tree_entries;
1283 struct got_tree_entry *new_te = NULL;
1284 struct got_pathlist_head paths;
1285 struct got_pathlist_entry *pe;
1287 *new_tree_id = NULL;
1289 TAILQ_INIT(&paths);
1290 new_tree_entries.nentries = 0;
1291 SIMPLEQ_INIT(&new_tree_entries.head);
1293 dir = opendir(path_dir);
1294 if (dir == NULL) {
1295 err = got_error_from_errno2("opendir", path_dir);
1296 goto done;
1299 while ((de = readdir(dir)) != NULL) {
1300 int ignore = 0;
1302 if (strcmp(de->d_name, ".") == 0 ||
1303 strcmp(de->d_name, "..") == 0)
1304 continue;
1306 TAILQ_FOREACH(pe, ignores, entry) {
1307 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1308 ignore = 1;
1309 break;
1312 if (ignore)
1313 continue;
1314 if (de->d_type == DT_DIR) {
1315 err = import_subdir(&new_te, de, path_dir,
1316 ignores, repo, progress_cb, progress_arg);
1317 if (err)
1318 goto done;
1319 } else if (de->d_type == DT_REG) {
1320 err = import_file(&new_te, de, path_dir, repo);
1321 if (err)
1322 goto done;
1323 } else
1324 continue;
1326 err = insert_tree_entry(new_te, &paths);
1327 if (err)
1328 goto done;
1331 TAILQ_FOREACH(pe, &paths, entry) {
1332 struct got_tree_entry *te = pe->data;
1333 char *path;
1334 new_tree_entries.nentries++;
1335 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
1336 if (!S_ISREG(te->mode))
1337 continue;
1338 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1339 err = got_error_from_errno("asprintf");
1340 goto done;
1342 err = (*progress_cb)(progress_arg, path);
1343 free(path);
1344 if (err)
1345 goto done;
1348 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
1349 done:
1350 if (dir)
1351 closedir(dir);
1352 got_object_tree_entries_close(&new_tree_entries);
1353 got_pathlist_free(&paths);
1354 return err;
1357 const struct got_error *
1358 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1359 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1360 struct got_repository *repo, got_repo_import_cb progress_cb,
1361 void *progress_arg)
1363 const struct got_error *err;
1364 struct got_object_id *new_tree_id;
1366 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1367 progress_cb, progress_arg);
1368 if (err)
1369 return err;
1371 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1372 author, time(NULL), author, time(NULL), logmsg, repo);
1373 free(new_tree_id);
1374 return err;