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/socket.h>
21 #include <sys/stat.h>
22 #include <sys/mman.h>
23 #include <sys/syslimits.h>
25 #include <ctype.h>
26 #include <fcntl.h>
27 #include <fnmatch.h>
28 #include <limits.h>
29 #include <dirent.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <sha1.h>
33 #include <string.h>
34 #include <zlib.h>
35 #include <errno.h>
36 #include <libgen.h>
37 #include <stdint.h>
38 #include <imsg.h>
39 #include <uuid.h>
41 #include "got_error.h"
42 #include "got_reference.h"
43 #include "got_repository.h"
44 #include "got_path.h"
45 #include "got_cancel.h"
46 #include "got_worktree.h"
47 #include "got_object.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_inflate.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_object_create.h"
54 #include "got_lib_pack.h"
55 #include "got_lib_privsep.h"
56 #include "got_lib_worktree.h"
57 #include "got_lib_sha1.h"
58 #include "got_lib_object_cache.h"
59 #include "got_lib_repository.h"
61 #ifndef nitems
62 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
63 #endif
65 #define GOT_GIT_DIR ".git"
67 /* Mandatory files and directories inside the git directory. */
68 #define GOT_OBJECTS_DIR "objects"
69 #define GOT_REFS_DIR "refs"
70 #define GOT_HEAD_FILE "HEAD"
71 #define GOT_GITCONFIG "config"
73 /* Other files and directories inside the git directory. */
74 #define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
75 #define GOT_ORIG_HEAD_FILE "ORIG_HEAD"
76 #define GOT_OBJECTS_PACK_DIR "objects/pack"
77 #define GOT_PACKED_REFS_FILE "packed-refs"
79 const char *
80 got_repo_get_path(struct got_repository *repo)
81 {
82 return repo->path;
83 }
85 const char *
86 got_repo_get_path_git_dir(struct got_repository *repo)
87 {
88 return repo->path_git_dir;
89 }
91 const char *
92 got_repo_get_gitconfig_author_name(struct got_repository *repo)
93 {
94 return repo->gitconfig_author_name;
95 }
97 const char *
98 got_repo_get_gitconfig_author_email(struct got_repository *repo)
99 {
100 return repo->gitconfig_author_email;
103 const char *
104 got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
106 return repo->global_gitconfig_author_name;
109 const char *
110 got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
112 return repo->global_gitconfig_author_email;
115 int
116 got_repo_is_bare(struct got_repository *repo)
118 return (strcmp(repo->path, repo->path_git_dir) == 0);
121 static char *
122 get_path_git_child(struct got_repository *repo, const char *basename)
124 char *path_child;
126 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
127 basename) == -1)
128 return NULL;
130 return path_child;
133 char *
134 got_repo_get_path_objects(struct got_repository *repo)
136 return get_path_git_child(repo, GOT_OBJECTS_DIR);
139 char *
140 got_repo_get_path_objects_pack(struct got_repository *repo)
142 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
145 char *
146 got_repo_get_path_refs(struct got_repository *repo)
148 return get_path_git_child(repo, GOT_REFS_DIR);
151 char *
152 got_repo_get_path_packed_refs(struct got_repository *repo)
154 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
157 static char *
158 get_path_head(struct got_repository *repo)
160 return get_path_git_child(repo, GOT_HEAD_FILE);
163 static const struct got_error *
164 get_path_gitconfig(char **p, struct got_repository *repo)
166 *p = get_path_git_child(repo, GOT_GITCONFIG);
167 if (*p == NULL)
168 return got_error_from_errno("asprintf");
169 return NULL;
172 static int
173 is_git_repo(struct got_repository *repo)
175 const char *path_git = got_repo_get_path_git_dir(repo);
176 char *path_objects = got_repo_get_path_objects(repo);
177 char *path_refs = got_repo_get_path_refs(repo);
178 char *path_head = get_path_head(repo);
179 int ret = 0;
180 struct stat sb;
181 struct got_reference *head_ref;
183 if (lstat(path_git, &sb) == -1)
184 goto done;
185 if (!S_ISDIR(sb.st_mode))
186 goto done;
188 if (lstat(path_objects, &sb) == -1)
189 goto done;
190 if (!S_ISDIR(sb.st_mode))
191 goto done;
193 if (lstat(path_refs, &sb) == -1)
194 goto done;
195 if (!S_ISDIR(sb.st_mode))
196 goto done;
198 if (lstat(path_head, &sb) == -1)
199 goto done;
200 if (!S_ISREG(sb.st_mode))
201 goto done;
203 /* Check if the HEAD reference can be opened. */
204 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
205 goto done;
206 got_ref_close(head_ref);
208 ret = 1;
209 done:
210 free(path_objects);
211 free(path_refs);
212 free(path_head);
213 return ret;
217 const struct got_error *
218 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
219 struct got_object *obj)
221 #ifndef GOT_NO_OBJ_CACHE
222 const struct got_error *err = NULL;
223 err = got_object_cache_add(&repo->objcache, id, obj);
224 if (err) {
225 if (err->code == GOT_ERR_OBJ_EXISTS ||
226 err->code == GOT_ERR_OBJ_TOO_LARGE)
227 err = NULL;
228 return err;
230 obj->refcnt++;
231 #endif
232 return NULL;
235 struct got_object *
236 got_repo_get_cached_object(struct got_repository *repo,
237 struct got_object_id *id)
239 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
242 const struct got_error *
243 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
244 struct got_tree_object *tree)
246 #ifndef GOT_NO_OBJ_CACHE
247 const struct got_error *err = NULL;
248 err = got_object_cache_add(&repo->treecache, id, tree);
249 if (err) {
250 if (err->code == GOT_ERR_OBJ_EXISTS ||
251 err->code == GOT_ERR_OBJ_TOO_LARGE)
252 err = NULL;
253 return err;
255 tree->refcnt++;
256 #endif
257 return NULL;
260 struct got_tree_object *
261 got_repo_get_cached_tree(struct got_repository *repo,
262 struct got_object_id *id)
264 return (struct got_tree_object *)got_object_cache_get(
265 &repo->treecache, id);
268 const struct got_error *
269 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
270 struct got_commit_object *commit)
272 #ifndef GOT_NO_OBJ_CACHE
273 const struct got_error *err = NULL;
274 err = got_object_cache_add(&repo->commitcache, id, commit);
275 if (err) {
276 if (err->code == GOT_ERR_OBJ_EXISTS ||
277 err->code == GOT_ERR_OBJ_TOO_LARGE)
278 err = NULL;
279 return err;
281 commit->refcnt++;
282 #endif
283 return NULL;
286 struct got_commit_object *
287 got_repo_get_cached_commit(struct got_repository *repo,
288 struct got_object_id *id)
290 return (struct got_commit_object *)got_object_cache_get(
291 &repo->commitcache, id);
294 const struct got_error *
295 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
296 struct got_tag_object *tag)
298 #ifndef GOT_NO_OBJ_CACHE
299 const struct got_error *err = NULL;
300 err = got_object_cache_add(&repo->tagcache, id, tag);
301 if (err) {
302 if (err->code == GOT_ERR_OBJ_EXISTS ||
303 err->code == GOT_ERR_OBJ_TOO_LARGE)
304 err = NULL;
305 return err;
307 tag->refcnt++;
308 #endif
309 return NULL;
312 struct got_tag_object *
313 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
315 return (struct got_tag_object *)got_object_cache_get(
316 &repo->tagcache, id);
319 const struct got_error *
320 open_repo(struct got_repository *repo, const char *path)
322 const struct got_error *err = NULL;
324 /* bare git repository? */
325 repo->path_git_dir = strdup(path);
326 if (repo->path_git_dir == NULL)
327 return got_error_from_errno("strdup");
328 if (is_git_repo(repo)) {
329 repo->path = strdup(repo->path_git_dir);
330 if (repo->path == NULL) {
331 err = got_error_from_errno("strdup");
332 goto done;
334 return NULL;
337 /* git repository with working tree? */
338 free(repo->path_git_dir);
339 repo->path_git_dir = NULL;
340 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
341 err = got_error_from_errno("asprintf");
342 goto done;
344 if (is_git_repo(repo)) {
345 repo->path = strdup(path);
346 if (repo->path == NULL) {
347 err = got_error_from_errno("strdup");
348 goto done;
350 return NULL;
353 err = got_error(GOT_ERR_NOT_GIT_REPO);
354 done:
355 if (err) {
356 free(repo->path);
357 repo->path = NULL;
358 free(repo->path_git_dir);
359 repo->path_git_dir = NULL;
361 return err;
364 static const struct got_error *
365 parse_gitconfig_file(int *gitconfig_repository_format_version,
366 char **gitconfig_author_name, char **gitconfig_author_email,
367 const char *gitconfig_path)
369 const struct got_error *err = NULL, *child_err = NULL;
370 int fd = -1;
371 int imsg_fds[2] = { -1, -1 };
372 pid_t pid;
373 struct imsgbuf *ibuf;
375 *gitconfig_repository_format_version = 0;
376 *gitconfig_author_name = NULL;
377 *gitconfig_author_email = NULL;
379 fd = open(gitconfig_path, O_RDONLY);
380 if (fd == -1) {
381 if (errno == ENOENT)
382 return NULL;
383 return got_error_from_errno2("open", gitconfig_path);
386 ibuf = calloc(1, sizeof(*ibuf));
387 if (ibuf == NULL) {
388 err = got_error_from_errno("calloc");
389 goto done;
392 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
393 err = got_error_from_errno("socketpair");
394 goto done;
397 pid = fork();
398 if (pid == -1) {
399 err = got_error_from_errno("fork");
400 goto done;
401 } else if (pid == 0) {
402 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
403 gitconfig_path);
404 /* not reached */
407 if (close(imsg_fds[1]) == -1) {
408 err = got_error_from_errno("close");
409 goto done;
411 imsg_fds[1] = -1;
412 imsg_init(ibuf, imsg_fds[0]);
414 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
415 if (err)
416 goto done;
417 fd = -1;
419 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
420 if (err)
421 goto done;
423 err = got_privsep_recv_gitconfig_int(
424 gitconfig_repository_format_version, ibuf);
425 if (err)
426 goto done;
428 err = got_privsep_send_gitconfig_author_name_req(ibuf);
429 if (err)
430 goto done;
432 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
433 if (err)
434 goto done;
436 err = got_privsep_send_gitconfig_author_email_req(ibuf);
437 if (err)
438 goto done;
440 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
441 if (err)
442 goto done;
444 imsg_clear(ibuf);
445 err = got_privsep_send_stop(imsg_fds[0]);
446 child_err = got_privsep_wait_for_child(pid);
447 if (child_err && err == NULL)
448 err = child_err;
449 done:
450 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
451 err = got_error_from_errno("close");
452 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
453 err = got_error_from_errno("close");
454 if (fd != -1 && close(fd) == -1 && err == NULL)
455 err = got_error_from_errno2("close", gitconfig_path);
456 free(ibuf);
457 return err;
460 static const struct got_error *
461 read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
463 const struct got_error *err = NULL;
464 char *repo_gitconfig_path = NULL;
466 if (global_gitconfig_path) {
467 /* Read settings from ~/.gitconfig. */
468 int dummy_repo_version;
469 err = parse_gitconfig_file(&dummy_repo_version,
470 &repo->global_gitconfig_author_name,
471 &repo->global_gitconfig_author_email,
472 global_gitconfig_path);
473 if (err)
474 return err;
477 /* Read repository's .git/config file. */
478 err = get_path_gitconfig(&repo_gitconfig_path, repo);
479 if (err)
480 return err;
482 err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
483 &repo->gitconfig_author_name, &repo->gitconfig_author_email,
484 repo_gitconfig_path);
485 if (err)
486 goto done;
487 done:
488 free(repo_gitconfig_path);
489 return err;
492 const struct got_error *
493 got_repo_open(struct got_repository **repop, const char *path,
494 const char *global_gitconfig_path)
496 struct got_repository *repo = NULL;
497 const struct got_error *err = NULL;
498 char *abspath;
499 int i, tried_root = 0;
501 *repop = NULL;
503 if (got_path_is_absolute(path))
504 abspath = strdup(path);
505 else
506 abspath = got_path_get_absolute(path);
507 if (abspath == NULL)
508 return got_error(GOT_ERR_BAD_PATH);
510 repo = calloc(1, sizeof(*repo));
511 if (repo == NULL) {
512 err = got_error_from_errno("calloc");
513 goto done;
516 for (i = 0; i < nitems(repo->privsep_children); i++) {
517 memset(&repo->privsep_children[i], 0,
518 sizeof(repo->privsep_children[0]));
519 repo->privsep_children[i].imsg_fd = -1;
522 err = got_object_cache_init(&repo->objcache,
523 GOT_OBJECT_CACHE_TYPE_OBJ);
524 if (err)
525 goto done;
526 err = got_object_cache_init(&repo->treecache,
527 GOT_OBJECT_CACHE_TYPE_TREE);
528 if (err)
529 goto done;
530 err = got_object_cache_init(&repo->commitcache,
531 GOT_OBJECT_CACHE_TYPE_COMMIT);
532 if (err)
533 goto done;
534 err = got_object_cache_init(&repo->tagcache,
535 GOT_OBJECT_CACHE_TYPE_TAG);
536 if (err)
537 goto done;
539 path = realpath(abspath, NULL);
540 if (path == NULL) {
541 err = got_error_from_errno2("realpath", abspath);
542 goto done;
545 do {
546 err = open_repo(repo, path);
547 if (err == NULL)
548 break;
549 if (err->code != GOT_ERR_NOT_GIT_REPO)
550 break;
551 if (path[0] == '/' && path[1] == '\0') {
552 if (tried_root) {
553 err = got_error(GOT_ERR_NOT_GIT_REPO);
554 goto done;
556 tried_root = 1;
558 path = dirname(path);
559 if (path == NULL) {
560 err = got_error_from_errno2("dirname", path);
561 goto done;
563 } while (path);
565 err = read_gitconfig(repo, global_gitconfig_path);
566 if (err)
567 goto done;
568 if (repo->gitconfig_repository_format_version != 0)
569 err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
570 done:
571 if (err)
572 got_repo_close(repo);
573 else
574 *repop = repo;
575 free(abspath);
576 return err;
579 const struct got_error *
580 got_repo_close(struct got_repository *repo)
582 const struct got_error *err = NULL, *child_err;
583 int i;
585 for (i = 0; i < nitems(repo->packidx_cache); i++) {
586 if (repo->packidx_cache[i] == NULL)
587 break;
588 got_packidx_close(repo->packidx_cache[i]);
591 for (i = 0; i < nitems(repo->packs); i++) {
592 if (repo->packs[i].path_packfile == NULL)
593 break;
594 got_pack_close(&repo->packs[i]);
597 free(repo->path);
598 free(repo->path_git_dir);
600 got_object_cache_close(&repo->objcache);
601 got_object_cache_close(&repo->treecache);
602 got_object_cache_close(&repo->commitcache);
603 got_object_cache_close(&repo->tagcache);
605 for (i = 0; i < nitems(repo->privsep_children); i++) {
606 if (repo->privsep_children[i].imsg_fd == -1)
607 continue;
608 imsg_clear(repo->privsep_children[i].ibuf);
609 free(repo->privsep_children[i].ibuf);
610 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
611 child_err = got_privsep_wait_for_child(
612 repo->privsep_children[i].pid);
613 if (child_err && err == NULL)
614 err = child_err;
615 if (close(repo->privsep_children[i].imsg_fd) != 0 &&
616 err == NULL)
617 err = got_error_from_errno("close");
620 free(repo->gitconfig_author_name);
621 free(repo->gitconfig_author_email);
622 free(repo);
624 return err;
627 const struct got_error *
628 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
629 const char *input_path, int check_disk)
631 const struct got_error *err = NULL;
632 const char *repo_abspath = NULL;
633 size_t repolen, cwdlen, len;
634 char *cwd, *canonpath, *path = NULL;
636 *in_repo_path = NULL;
638 cwd = getcwd(NULL, 0);
639 if (cwd == NULL)
640 return got_error_from_errno("getcwd");
642 canonpath = strdup(input_path);
643 if (canonpath == NULL) {
644 err = got_error_from_errno("strdup");
645 goto done;
647 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
648 if (err)
649 goto done;
651 repo_abspath = got_repo_get_path(repo);
653 if (!check_disk || canonpath[0] == '\0') {
654 path = strdup(canonpath);
655 if (path == NULL) {
656 err = got_error_from_errno("strdup");
657 goto done;
659 } else {
660 int is_repo_child = 0, is_cwd_child = 0;
662 path = realpath(canonpath, NULL);
663 if (path == NULL) {
664 if (errno != ENOENT) {
665 err = got_error_from_errno2("realpath",
666 canonpath);
667 goto done;
669 /*
670 * Path is not on disk.
671 * Assume it is already relative to repository root.
672 */
673 path = strdup(canonpath);
674 if (path == NULL) {
675 err = got_error_from_errno("strdup");
676 goto done;
680 repolen = strlen(repo_abspath);
681 cwdlen = strlen(cwd);
682 len = strlen(path);
684 if (len > repolen && strncmp(path, repo_abspath, repolen) == 0)
685 is_repo_child = 1;
686 if (len > cwdlen && strncmp(path, cwd, cwdlen) == 0)
687 is_cwd_child = 1;
689 if (strcmp(path, repo_abspath) == 0) {
690 free(path);
691 path = strdup("");
692 if (path == NULL) {
693 err = got_error_from_errno("strdup");
694 goto done;
696 } else if (is_repo_child && is_cwd_child) {
697 char *child;
698 /* Strip common prefix with repository path. */
699 err = got_path_skip_common_ancestor(&child,
700 repo_abspath, path);
701 if (err)
702 goto done;
703 free(path);
704 path = child;
705 } else if (is_repo_child) {
706 /* Matched an on-disk path inside repository. */
707 if (got_repo_is_bare(repo)) {
708 /*
709 * Matched an on-disk path inside repository
710 * database. Treat as repository-relative.
711 */
712 } else {
713 char *child;
714 /* Strip common prefix with repository path. */
715 err = got_path_skip_common_ancestor(&child,
716 repo_abspath, path);
717 if (err)
718 goto done;
719 free(path);
720 path = child;
722 } else if (is_cwd_child) {
723 char *child;
724 /* Strip common prefix with cwd. */
725 err = got_path_skip_common_ancestor(&child, cwd,
726 path);
727 if (err)
728 goto done;
729 free(path);
730 path = child;
731 } else {
732 /*
733 * Matched unrelated on-disk path.
734 * Treat it as repository-relative.
735 */
739 /* Make in-repository path absolute */
740 if (path[0] != '/') {
741 char *abspath;
742 if (asprintf(&abspath, "/%s", path) == -1) {
743 err = got_error_from_errno("asprintf");
744 goto done;
746 free(path);
747 path = abspath;
750 done:
751 free(cwd);
752 free(canonpath);
753 if (err)
754 free(path);
755 else
756 *in_repo_path = path;
757 return err;
760 const struct got_error *
761 got_repo_cache_packidx(struct got_repository *repo, struct got_packidx *packidx)
763 const struct got_error *err = NULL;
764 int i;
766 for (i = 0; i < nitems(repo->packidx_cache); i++) {
767 if (repo->packidx_cache[i] == NULL)
768 break;
770 if (i == nitems(repo->packidx_cache)) {
771 err = got_packidx_close(repo->packidx_cache[i - 1]);
772 if (err)
773 return err;
776 /*
777 * Insert the new pack index at the front so it will
778 * be searched first in the future.
779 */
780 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
781 sizeof(repo->packidx_cache) -
782 sizeof(repo->packidx_cache[0]));
783 repo->packidx_cache[0] = packidx;
785 return NULL;
788 static int
789 is_packidx_filename(const char *name, size_t len)
791 if (len != GOT_PACKIDX_NAMELEN)
792 return 0;
794 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
795 return 0;
797 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
798 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
799 return 0;
801 return 1;
804 const struct got_error *
805 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
806 struct got_repository *repo, struct got_object_id *id)
808 const struct got_error *err;
809 char *path_packdir;
810 DIR *packdir;
811 struct dirent *dent;
812 char *path_packidx;
813 int i;
815 /* Search pack index cache. */
816 for (i = 0; i < nitems(repo->packidx_cache); i++) {
817 if (repo->packidx_cache[i] == NULL)
818 break;
819 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
820 if (*idx != -1) {
821 *packidx = repo->packidx_cache[i];
822 return NULL;
825 /* No luck. Search the filesystem. */
827 path_packdir = got_repo_get_path_objects_pack(repo);
828 if (path_packdir == NULL)
829 return got_error_from_errno("got_repo_get_path_objects_pack");
831 packdir = opendir(path_packdir);
832 if (packdir == NULL) {
833 if (errno == ENOENT)
834 err = got_error_no_obj(id);
835 else
836 err = got_error_from_errno2("opendir", path_packdir);
837 goto done;
840 while ((dent = readdir(packdir)) != NULL) {
841 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
842 continue;
844 if (asprintf(&path_packidx, "%s/%s", path_packdir,
845 dent->d_name) == -1) {
846 err = got_error_from_errno("asprintf");
847 goto done;
850 err = got_packidx_open(packidx, path_packidx, 0);
851 free(path_packidx);
852 if (err)
853 goto done;
855 *idx = got_packidx_get_object_idx(*packidx, id);
856 if (*idx != -1) {
857 err = NULL; /* found the object */
858 err = got_repo_cache_packidx(repo, *packidx);
859 goto done;
862 err = got_packidx_close(*packidx);
863 *packidx = NULL;
864 if (err)
865 goto done;
868 err = got_error_no_obj(id);
869 done:
870 free(path_packdir);
871 if (packdir && closedir(packdir) != 0 && err == NULL)
872 err = got_error_from_errno("closedir");
873 return err;
876 static const struct got_error *
877 read_packfile_hdr(int fd, struct got_packidx *packidx)
879 const struct got_error *err = NULL;
880 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
881 struct got_packfile_hdr hdr;
882 ssize_t n;
884 n = read(fd, &hdr, sizeof(hdr));
885 if (n < 0)
886 return got_error_from_errno("read");
887 if (n != sizeof(hdr))
888 return got_error(GOT_ERR_BAD_PACKFILE);
890 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
891 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
892 betoh32(hdr.nobjects) != totobj)
893 err = got_error(GOT_ERR_BAD_PACKFILE);
895 return err;
898 static const struct got_error *
899 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
901 const struct got_error *err = NULL;
903 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
904 if (*fd == -1)
905 return got_error_from_errno2("open", path_packfile);
907 if (packidx) {
908 err = read_packfile_hdr(*fd, packidx);
909 if (err) {
910 close(*fd);
911 *fd = -1;
915 return err;
918 const struct got_error *
919 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
920 const char *path_packfile, struct got_packidx *packidx)
922 const struct got_error *err = NULL;
923 struct got_pack *pack = NULL;
924 struct stat sb;
925 int i;
927 if (packp)
928 *packp = NULL;
930 for (i = 0; i < nitems(repo->packs); i++) {
931 pack = &repo->packs[i];
932 if (pack->path_packfile == NULL)
933 break;
934 if (strcmp(pack->path_packfile, path_packfile) == 0)
935 return NULL;
938 if (i == nitems(repo->packs) - 1) {
939 err = got_pack_close(&repo->packs[i - 1]);
940 if (err)
941 return err;
942 memmove(&repo->packs[1], &repo->packs[0],
943 sizeof(repo->packs) - sizeof(repo->packs[0]));
944 i = 0;
947 pack = &repo->packs[i];
949 pack->path_packfile = strdup(path_packfile);
950 if (pack->path_packfile == NULL) {
951 err = got_error_from_errno("strdup");
952 goto done;
955 err = open_packfile(&pack->fd, path_packfile, packidx);
956 if (err)
957 goto done;
959 if (fstat(pack->fd, &sb) != 0) {
960 err = got_error_from_errno("fstat");
961 goto done;
963 pack->filesize = sb.st_size;
965 pack->privsep_child = NULL;
967 #ifndef GOT_PACK_NO_MMAP
968 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
969 pack->fd, 0);
970 if (pack->map == MAP_FAILED) {
971 if (errno != ENOMEM) {
972 err = got_error_from_errno("mmap");
973 goto done;
975 pack->map = NULL; /* fall back to read(2) */
977 #endif
978 done:
979 if (err) {
980 if (pack) {
981 free(pack->path_packfile);
982 memset(pack, 0, sizeof(*pack));
984 } else if (packp)
985 *packp = pack;
986 return err;
989 struct got_pack *
990 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
992 struct got_pack *pack = NULL;
993 int i;
995 for (i = 0; i < nitems(repo->packs); i++) {
996 pack = &repo->packs[i];
997 if (pack->path_packfile == NULL)
998 break;
999 if (strcmp(pack->path_packfile, path_packfile) == 0)
1000 return pack;
1003 return NULL;
1006 const struct got_error *
1007 got_repo_init(const char *repo_path)
1009 const struct got_error *err = NULL;
1010 const char *dirnames[] = {
1011 GOT_OBJECTS_DIR,
1012 GOT_OBJECTS_PACK_DIR,
1013 GOT_REFS_DIR,
1015 const char *description_str = "Unnamed repository; "
1016 "edit this file 'description' to name the repository.";
1017 const char *headref_str = "ref: refs/heads/master";
1018 const char *gitconfig_str = "[core]\n"
1019 "\trepositoryformatversion = 0\n"
1020 "\tfilemode = true\n"
1021 "\tbare = true\n";
1022 char *path;
1023 int i;
1025 if (!got_path_dir_is_empty(repo_path))
1026 return got_error(GOT_ERR_DIR_NOT_EMPTY);
1028 for (i = 0; i < nitems(dirnames); i++) {
1029 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1030 return got_error_from_errno("asprintf");
1032 err = got_path_mkdir(path);
1033 free(path);
1034 if (err)
1035 return err;
1038 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1039 return got_error_from_errno("asprintf");
1040 err = got_path_create_file(path, description_str);
1041 free(path);
1042 if (err)
1043 return err;
1045 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1046 return got_error_from_errno("asprintf");
1047 err = got_path_create_file(path, headref_str);
1048 free(path);
1049 if (err)
1050 return err;
1052 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1053 return got_error_from_errno("asprintf");
1054 err = got_path_create_file(path, gitconfig_str);
1055 free(path);
1056 if (err)
1057 return err;
1059 return NULL;
1062 static const struct got_error *
1063 match_packed_object(struct got_object_id **unique_id,
1064 struct got_repository *repo, const char *id_str_prefix, int obj_type)
1066 const struct got_error *err = NULL;
1067 char *path_packdir;
1068 DIR *packdir;
1069 struct dirent *dent;
1070 char *path_packidx;
1071 struct got_object_id_queue matched_ids;
1073 SIMPLEQ_INIT(&matched_ids);
1075 path_packdir = got_repo_get_path_objects_pack(repo);
1076 if (path_packdir == NULL)
1077 return got_error_from_errno("got_repo_get_path_objects_pack");
1079 packdir = opendir(path_packdir);
1080 if (packdir == NULL) {
1081 if (errno != ENOENT)
1082 err = got_error_from_errno2("opendir", path_packdir);
1083 goto done;
1086 while ((dent = readdir(packdir)) != NULL) {
1087 struct got_packidx *packidx;
1088 struct got_object_qid *qid;
1091 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1092 continue;
1094 if (asprintf(&path_packidx, "%s/%s", path_packdir,
1095 dent->d_name) == -1) {
1096 err = got_error_from_errno("asprintf");
1097 break;
1100 err = got_packidx_open(&packidx, path_packidx, 0);
1101 free(path_packidx);
1102 if (err)
1103 break;
1105 err = got_packidx_match_id_str_prefix(&matched_ids,
1106 packidx, id_str_prefix);
1107 if (err) {
1108 got_packidx_close(packidx);
1109 break;
1111 err = got_packidx_close(packidx);
1112 if (err)
1113 break;
1115 SIMPLEQ_FOREACH(qid, &matched_ids, entry) {
1116 if (obj_type != GOT_OBJ_TYPE_ANY) {
1117 int matched_type;
1118 err = got_object_get_type(&matched_type, repo,
1119 qid->id);
1120 if (err)
1121 goto done;
1122 if (matched_type != obj_type)
1123 continue;
1125 if (*unique_id == NULL) {
1126 *unique_id = got_object_id_dup(qid->id);
1127 if (*unique_id == NULL) {
1128 err = got_error_from_errno("malloc");
1129 goto done;
1131 } else {
1132 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1133 goto done;
1137 done:
1138 got_object_id_queue_free(&matched_ids);
1139 free(path_packdir);
1140 if (packdir && closedir(packdir) != 0 && err == NULL)
1141 err = got_error_from_errno("closedir");
1142 if (err) {
1143 free(*unique_id);
1144 *unique_id = NULL;
1146 return err;
1149 static const struct got_error *
1150 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1151 const char *object_dir, const char *id_str_prefix, int obj_type,
1152 struct got_repository *repo)
1154 const struct got_error *err = NULL;
1155 char *path;
1156 DIR *dir = NULL;
1157 struct dirent *dent;
1158 struct got_object_id id;
1160 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1161 err = got_error_from_errno("asprintf");
1162 goto done;
1165 dir = opendir(path);
1166 if (dir == NULL) {
1167 if (errno == ENOENT) {
1168 err = NULL;
1169 goto done;
1171 err = got_error_from_errno2("opendir", path);
1172 goto done;
1174 while ((dent = readdir(dir)) != NULL) {
1175 char *id_str;
1176 int cmp;
1178 if (strcmp(dent->d_name, ".") == 0 ||
1179 strcmp(dent->d_name, "..") == 0)
1180 continue;
1182 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1183 err = got_error_from_errno("asprintf");
1184 goto done;
1187 if (!got_parse_sha1_digest(id.sha1, id_str))
1188 continue;
1191 * Directory entries do not necessarily appear in
1192 * sorted order, so we must iterate over all of them.
1194 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1195 if (cmp != 0) {
1196 free(id_str);
1197 continue;
1200 if (*unique_id == NULL) {
1201 if (obj_type != GOT_OBJ_TYPE_ANY) {
1202 int matched_type;
1203 err = got_object_get_type(&matched_type, repo,
1204 &id);
1205 if (err)
1206 goto done;
1207 if (matched_type != obj_type)
1208 continue;
1210 *unique_id = got_object_id_dup(&id);
1211 if (*unique_id == NULL) {
1212 err = got_error_from_errno("got_object_id_dup");
1213 free(id_str);
1214 goto done;
1216 } else {
1217 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1218 free(id_str);
1219 goto done;
1222 done:
1223 if (dir && closedir(dir) != 0 && err == NULL)
1224 err = got_error_from_errno("closedir");
1225 if (err) {
1226 free(*unique_id);
1227 *unique_id = NULL;
1229 free(path);
1230 return err;
1233 const struct got_error *
1234 got_repo_match_object_id_prefix(struct got_object_id **id,
1235 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1237 const struct got_error *err = NULL;
1238 char *path_objects = got_repo_get_path_objects(repo);
1239 char *object_dir = NULL;
1240 size_t len;
1241 int i;
1243 *id = NULL;
1245 for (i = 0; i < strlen(id_str_prefix); i++) {
1246 if (isxdigit((unsigned char)id_str_prefix[i]))
1247 continue;
1248 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
1251 len = strlen(id_str_prefix);
1252 if (len >= 2) {
1253 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1254 if (err)
1255 goto done;
1256 object_dir = strndup(id_str_prefix, 2);
1257 if (object_dir == NULL) {
1258 err = got_error_from_errno("strdup");
1259 goto done;
1261 err = match_loose_object(id, path_objects, object_dir,
1262 id_str_prefix, obj_type, repo);
1263 } else if (len == 1) {
1264 int i;
1265 for (i = 0; i < 0xf; i++) {
1266 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1267 == -1) {
1268 err = got_error_from_errno("asprintf");
1269 goto done;
1271 err = match_packed_object(id, repo, object_dir,
1272 obj_type);
1273 if (err)
1274 goto done;
1275 err = match_loose_object(id, path_objects, object_dir,
1276 id_str_prefix, obj_type, repo);
1277 if (err)
1278 goto done;
1280 } else {
1281 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
1282 goto done;
1284 done:
1285 free(object_dir);
1286 if (err) {
1287 free(*id);
1288 *id = NULL;
1289 } else if (*id == NULL)
1290 err = got_error(GOT_ERR_NO_OBJ);
1292 return err;
1295 const struct got_error *
1296 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1297 int obj_type, struct got_repository *repo)
1299 const struct got_error *err;
1300 struct got_reflist_head refs;
1301 struct got_reflist_entry *re;
1302 struct got_object_id *tag_id;
1304 SIMPLEQ_INIT(&refs);
1305 *tag = NULL;
1307 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_by_name, NULL);
1308 if (err)
1309 return err;
1311 SIMPLEQ_FOREACH(re, &refs, entry) {
1312 const char *refname;
1313 refname = got_ref_get_name(re->ref);
1314 if (got_ref_is_symbolic(re->ref))
1315 continue;
1316 refname += strlen("refs/tags/");
1317 if (strcmp(refname, name) != 0)
1318 continue;
1319 err = got_ref_resolve(&tag_id, repo, re->ref);
1320 if (err)
1321 break;
1322 err = got_object_open_as_tag(tag, repo, tag_id);
1323 free(tag_id);
1324 if (err)
1325 break;
1326 if (obj_type == GOT_OBJ_TYPE_ANY ||
1327 got_object_tag_get_object_type(*tag) == obj_type)
1328 break;
1329 got_object_tag_close(*tag);
1330 *tag = NULL;
1333 got_ref_list_free(&refs);
1334 if (err == NULL && *tag == NULL)
1335 err = got_error(GOT_ERR_NO_OBJ);
1336 return err;
1339 static const struct got_error *
1340 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1341 const char *name, mode_t mode, struct got_object_id *blob_id)
1343 const struct got_error *err = NULL;
1345 *new_te = NULL;
1347 *new_te = calloc(1, sizeof(**new_te));
1348 if (*new_te == NULL)
1349 return got_error_from_errno("calloc");
1351 (*new_te)->name = strdup(name);
1352 if ((*new_te)->name == NULL) {
1353 err = got_error_from_errno("strdup");
1354 goto done;
1357 (*new_te)->mode = S_IFREG | (mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
1358 (*new_te)->id = blob_id;
1359 done:
1360 if (err && *new_te) {
1361 got_object_tree_entry_close(*new_te);
1362 *new_te = NULL;
1364 return err;
1367 static const struct got_error *
1368 import_file(struct got_tree_entry **new_te, struct dirent *de,
1369 const char *path, struct got_repository *repo)
1371 const struct got_error *err;
1372 struct got_object_id *blob_id = NULL;
1373 char *filepath;
1374 struct stat sb;
1376 if (asprintf(&filepath, "%s%s%s", path,
1377 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1378 return got_error_from_errno("asprintf");
1380 if (lstat(filepath, &sb) != 0) {
1381 err = got_error_from_errno2("lstat", path);
1382 goto done;
1385 err = got_object_blob_create(&blob_id, filepath, repo);
1386 if (err)
1387 goto done;
1389 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1390 blob_id);
1391 done:
1392 free(filepath);
1393 if (err)
1394 free(blob_id);
1395 return err;
1398 static const struct got_error *
1399 insert_tree_entry(struct got_tree_entry *new_te,
1400 struct got_pathlist_head *paths)
1402 const struct got_error *err = NULL;
1403 struct got_pathlist_entry *new_pe;
1405 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1406 if (err)
1407 return err;
1408 if (new_pe == NULL)
1409 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1410 return NULL;
1413 static const struct got_error *write_tree(struct got_object_id **,
1414 const char *, struct got_pathlist_head *, struct got_repository *,
1415 got_repo_import_cb progress_cb, void *progress_arg);
1417 static const struct got_error *
1418 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1419 const char *path, struct got_pathlist_head *ignores,
1420 struct got_repository *repo,
1421 got_repo_import_cb progress_cb, void *progress_arg)
1423 const struct got_error *err;
1424 char *subdirpath;
1426 if (asprintf(&subdirpath, "%s%s%s", path,
1427 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1428 return got_error_from_errno("asprintf");
1430 (*new_te) = calloc(1, sizeof(**new_te));
1431 (*new_te)->mode = S_IFDIR;
1432 (*new_te)->name = strdup(de->d_name);
1433 if ((*new_te)->name == NULL) {
1434 err = got_error_from_errno("strdup");
1435 goto done;
1438 err = write_tree(&(*new_te)->id, subdirpath, ignores, repo,
1439 progress_cb, progress_arg);
1440 done:
1441 free(subdirpath);
1442 if (err) {
1443 got_object_tree_entry_close(*new_te);
1444 *new_te = NULL;
1446 return err;
1449 static const struct got_error *
1450 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1451 struct got_pathlist_head *ignores, struct got_repository *repo,
1452 got_repo_import_cb progress_cb, void *progress_arg)
1454 const struct got_error *err = NULL;
1455 DIR *dir;
1456 struct dirent *de;
1457 struct got_tree_entries new_tree_entries;
1458 struct got_tree_entry *new_te = NULL;
1459 struct got_pathlist_head paths;
1460 struct got_pathlist_entry *pe;
1462 *new_tree_id = NULL;
1464 TAILQ_INIT(&paths);
1465 new_tree_entries.nentries = 0;
1466 SIMPLEQ_INIT(&new_tree_entries.head);
1468 dir = opendir(path_dir);
1469 if (dir == NULL) {
1470 err = got_error_from_errno2("opendir", path_dir);
1471 goto done;
1474 while ((de = readdir(dir)) != NULL) {
1475 int ignore = 0;
1477 if (strcmp(de->d_name, ".") == 0 ||
1478 strcmp(de->d_name, "..") == 0)
1479 continue;
1481 TAILQ_FOREACH(pe, ignores, entry) {
1482 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1483 ignore = 1;
1484 break;
1487 if (ignore)
1488 continue;
1489 if (de->d_type == DT_DIR) {
1490 err = import_subdir(&new_te, de, path_dir,
1491 ignores, repo, progress_cb, progress_arg);
1492 if (err)
1493 goto done;
1494 } else if (de->d_type == DT_REG) {
1495 err = import_file(&new_te, de, path_dir, repo);
1496 if (err)
1497 goto done;
1498 } else
1499 continue;
1501 err = insert_tree_entry(new_te, &paths);
1502 if (err)
1503 goto done;
1506 TAILQ_FOREACH(pe, &paths, entry) {
1507 struct got_tree_entry *te = pe->data;
1508 char *path;
1509 new_tree_entries.nentries++;
1510 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
1511 if (!S_ISREG(te->mode))
1512 continue;
1513 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1514 err = got_error_from_errno("asprintf");
1515 goto done;
1517 err = (*progress_cb)(progress_arg, path);
1518 free(path);
1519 if (err)
1520 goto done;
1523 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
1524 done:
1525 if (dir)
1526 closedir(dir);
1527 got_object_tree_entries_close(&new_tree_entries);
1528 got_pathlist_free(&paths);
1529 return err;
1532 const struct got_error *
1533 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1534 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1535 struct got_repository *repo, got_repo_import_cb progress_cb,
1536 void *progress_arg)
1538 const struct got_error *err;
1539 struct got_object_id *new_tree_id;
1541 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1542 progress_cb, progress_arg);
1543 if (err)
1544 return err;
1546 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1547 author, time(NULL), author, time(NULL), logmsg, repo);
1548 free(new_tree_id);
1549 return err;