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"
59 #include "got_lib_gitconfig.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 int
92 got_repo_is_bare(struct got_repository *repo)
93 {
94 return (strcmp(repo->path, repo->path_git_dir) == 0);
95 }
97 static char *
98 get_path_git_child(struct got_repository *repo, const char *basename)
99 {
100 char *path_child;
102 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
103 basename) == -1)
104 return NULL;
106 return path_child;
109 char *
110 got_repo_get_path_objects(struct got_repository *repo)
112 return get_path_git_child(repo, GOT_OBJECTS_DIR);
115 char *
116 got_repo_get_path_objects_pack(struct got_repository *repo)
118 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
121 char *
122 got_repo_get_path_refs(struct got_repository *repo)
124 return get_path_git_child(repo, GOT_REFS_DIR);
127 char *
128 got_repo_get_path_packed_refs(struct got_repository *repo)
130 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
133 static char *
134 get_path_head(struct got_repository *repo)
136 return get_path_git_child(repo, GOT_HEAD_FILE);
139 static const struct got_error *
140 get_path_gitconfig(char **p, struct got_repository *repo)
142 *p = get_path_git_child(repo, GOT_GITCONFIG);
143 if (*p == NULL)
144 return got_error_from_errno("asprintf");
145 return NULL;
148 static int
149 is_git_repo(struct got_repository *repo)
151 const char *path_git = got_repo_get_path_git_dir(repo);
152 char *path_objects = got_repo_get_path_objects(repo);
153 char *path_refs = got_repo_get_path_refs(repo);
154 char *path_head = get_path_head(repo);
155 int ret = 0;
156 struct stat sb;
157 struct got_reference *head_ref;
159 if (lstat(path_git, &sb) == -1)
160 goto done;
161 if (!S_ISDIR(sb.st_mode))
162 goto done;
164 if (lstat(path_objects, &sb) == -1)
165 goto done;
166 if (!S_ISDIR(sb.st_mode))
167 goto done;
169 if (lstat(path_refs, &sb) == -1)
170 goto done;
171 if (!S_ISDIR(sb.st_mode))
172 goto done;
174 if (lstat(path_head, &sb) == -1)
175 goto done;
176 if (!S_ISREG(sb.st_mode))
177 goto done;
179 /* Check if the HEAD reference can be opened. */
180 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
181 goto done;
182 got_ref_close(head_ref);
184 ret = 1;
185 done:
186 free(path_objects);
187 free(path_refs);
188 free(path_head);
189 return ret;
193 const struct got_error *
194 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
195 struct got_object *obj)
197 #ifndef GOT_NO_OBJ_CACHE
198 const struct got_error *err = NULL;
199 err = got_object_cache_add(&repo->objcache, id, obj);
200 if (err) {
201 if (err->code == GOT_ERR_OBJ_EXISTS ||
202 err->code == GOT_ERR_OBJ_TOO_LARGE)
203 err = NULL;
204 return err;
206 obj->refcnt++;
207 #endif
208 return NULL;
211 struct got_object *
212 got_repo_get_cached_object(struct got_repository *repo,
213 struct got_object_id *id)
215 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
218 const struct got_error *
219 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
220 struct got_tree_object *tree)
222 #ifndef GOT_NO_OBJ_CACHE
223 const struct got_error *err = NULL;
224 err = got_object_cache_add(&repo->treecache, id, tree);
225 if (err) {
226 if (err->code == GOT_ERR_OBJ_EXISTS ||
227 err->code == GOT_ERR_OBJ_TOO_LARGE)
228 err = NULL;
229 return err;
231 tree->refcnt++;
232 #endif
233 return NULL;
236 struct got_tree_object *
237 got_repo_get_cached_tree(struct got_repository *repo,
238 struct got_object_id *id)
240 return (struct got_tree_object *)got_object_cache_get(
241 &repo->treecache, id);
244 const struct got_error *
245 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
246 struct got_commit_object *commit)
248 #ifndef GOT_NO_OBJ_CACHE
249 const struct got_error *err = NULL;
250 err = got_object_cache_add(&repo->commitcache, id, commit);
251 if (err) {
252 if (err->code == GOT_ERR_OBJ_EXISTS ||
253 err->code == GOT_ERR_OBJ_TOO_LARGE)
254 err = NULL;
255 return err;
257 commit->refcnt++;
258 #endif
259 return NULL;
262 struct got_commit_object *
263 got_repo_get_cached_commit(struct got_repository *repo,
264 struct got_object_id *id)
266 return (struct got_commit_object *)got_object_cache_get(
267 &repo->commitcache, id);
270 const struct got_error *
271 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
272 struct got_tag_object *tag)
274 #ifndef GOT_NO_OBJ_CACHE
275 const struct got_error *err = NULL;
276 err = got_object_cache_add(&repo->tagcache, id, tag);
277 if (err) {
278 if (err->code == GOT_ERR_OBJ_EXISTS ||
279 err->code == GOT_ERR_OBJ_TOO_LARGE)
280 err = NULL;
281 return err;
283 tag->refcnt++;
284 #endif
285 return NULL;
288 struct got_tag_object *
289 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
291 return (struct got_tag_object *)got_object_cache_get(
292 &repo->tagcache, id);
295 const struct got_error *
296 open_repo(struct got_repository *repo, const char *path)
298 const struct got_error *err = NULL;
300 /* bare git repository? */
301 repo->path_git_dir = strdup(path);
302 if (repo->path_git_dir == NULL)
303 return got_error_from_errno("strdup");
304 if (is_git_repo(repo)) {
305 repo->path = strdup(repo->path_git_dir);
306 if (repo->path == NULL) {
307 err = got_error_from_errno("strdup");
308 goto done;
310 return NULL;
313 /* git repository with working tree? */
314 free(repo->path_git_dir);
315 repo->path_git_dir = NULL;
316 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
317 err = got_error_from_errno("asprintf");
318 goto done;
320 if (is_git_repo(repo)) {
321 repo->path = strdup(path);
322 if (repo->path == NULL) {
323 err = got_error_from_errno("strdup");
324 goto done;
326 return NULL;
329 err = got_error(GOT_ERR_NOT_GIT_REPO);
330 done:
331 if (err) {
332 free(repo->path);
333 repo->path = NULL;
334 free(repo->path_git_dir);
335 repo->path_git_dir = NULL;
337 return err;
340 const struct got_error *
341 got_repo_open(struct got_repository **repop, const char *path)
343 struct got_repository *repo = NULL;
344 const struct got_error *err = NULL;
345 char *abspath, *gitconfig_path = NULL;
346 int i, tried_root = 0;
348 *repop = NULL;
350 if (got_path_is_absolute(path))
351 abspath = strdup(path);
352 else
353 abspath = got_path_get_absolute(path);
354 if (abspath == NULL)
355 return got_error(GOT_ERR_BAD_PATH);
357 repo = calloc(1, sizeof(*repo));
358 if (repo == NULL) {
359 err = got_error_from_errno("calloc");
360 goto done;
363 for (i = 0; i < nitems(repo->privsep_children); i++) {
364 memset(&repo->privsep_children[i], 0,
365 sizeof(repo->privsep_children[0]));
366 repo->privsep_children[i].imsg_fd = -1;
369 err = got_object_cache_init(&repo->objcache,
370 GOT_OBJECT_CACHE_TYPE_OBJ);
371 if (err)
372 goto done;
373 err = got_object_cache_init(&repo->treecache,
374 GOT_OBJECT_CACHE_TYPE_TREE);
375 if (err)
376 goto done;
377 err = got_object_cache_init(&repo->commitcache,
378 GOT_OBJECT_CACHE_TYPE_COMMIT);
379 if (err)
380 goto done;
381 err = got_object_cache_init(&repo->tagcache,
382 GOT_OBJECT_CACHE_TYPE_TAG);
383 if (err)
384 goto done;
386 path = realpath(abspath, NULL);
387 if (path == NULL) {
388 err = got_error_from_errno2("realpath", abspath);
389 goto done;
392 do {
393 err = open_repo(repo, path);
394 if (err == NULL)
395 break;
396 if (err->code != GOT_ERR_NOT_GIT_REPO)
397 break;
398 if (path[0] == '/' && path[1] == '\0') {
399 if (tried_root) {
400 err = got_error(GOT_ERR_NOT_GIT_REPO);
401 goto done;
403 tried_root = 1;
405 path = dirname(path);
406 if (path == NULL) {
407 err = got_error_from_errno2("dirname", path);
408 goto done;
410 } while (path);
412 err = get_path_gitconfig(&gitconfig_path, repo);
413 if (err)
414 goto done;
416 #ifdef notyet
417 err = got_gitconfig_open(&repo->gitconfig, gitconfig_path);
418 if (err)
419 goto done;
420 #else
421 repo->gitconfig = NULL;
422 #endif
423 done:
424 if (err)
425 got_repo_close(repo);
426 else
427 *repop = repo;
428 free(abspath);
429 free(gitconfig_path);
430 return err;
433 const struct got_error *
434 got_repo_close(struct got_repository *repo)
436 const struct got_error *err = NULL, *child_err;
437 int i;
439 for (i = 0; i < nitems(repo->packidx_cache); i++) {
440 if (repo->packidx_cache[i] == NULL)
441 break;
442 got_packidx_close(repo->packidx_cache[i]);
445 for (i = 0; i < nitems(repo->packs); i++) {
446 if (repo->packs[i].path_packfile == NULL)
447 break;
448 got_pack_close(&repo->packs[i]);
451 free(repo->path);
452 free(repo->path_git_dir);
454 got_object_cache_close(&repo->objcache);
455 got_object_cache_close(&repo->treecache);
456 got_object_cache_close(&repo->commitcache);
457 got_object_cache_close(&repo->tagcache);
459 for (i = 0; i < nitems(repo->privsep_children); i++) {
460 if (repo->privsep_children[i].imsg_fd == -1)
461 continue;
462 imsg_clear(repo->privsep_children[i].ibuf);
463 free(repo->privsep_children[i].ibuf);
464 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
465 child_err = got_privsep_wait_for_child(
466 repo->privsep_children[i].pid);
467 if (child_err && err == NULL)
468 err = child_err;
469 if (close(repo->privsep_children[i].imsg_fd) != 0 &&
470 err == NULL)
471 err = got_error_from_errno("close");
473 if (repo->gitconfig)
474 got_gitconfig_close(repo->gitconfig);
475 free(repo);
477 return err;
480 const struct got_error *
481 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
482 const char *input_path, int check_disk)
484 const struct got_error *err = NULL;
485 const char *repo_abspath = NULL;
486 size_t repolen, cwdlen, len;
487 char *cwd, *canonpath, *path = NULL;
489 *in_repo_path = NULL;
491 cwd = getcwd(NULL, 0);
492 if (cwd == NULL)
493 return got_error_from_errno("getcwd");
495 canonpath = strdup(input_path);
496 if (canonpath == NULL) {
497 err = got_error_from_errno("strdup");
498 goto done;
500 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
501 if (err)
502 goto done;
504 repo_abspath = got_repo_get_path(repo);
506 if (!check_disk || canonpath[0] == '\0') {
507 path = strdup(canonpath);
508 if (path == NULL) {
509 err = got_error_from_errno("strdup");
510 goto done;
512 } else {
513 int is_repo_child = 0, is_cwd_child = 0;
515 path = realpath(canonpath, NULL);
516 if (path == NULL) {
517 if (errno != ENOENT) {
518 err = got_error_from_errno2("realpath",
519 canonpath);
520 goto done;
522 /*
523 * Path is not on disk.
524 * Assume it is already relative to repository root.
525 */
526 path = strdup(canonpath);
527 if (path == NULL) {
528 err = got_error_from_errno("strdup");
529 goto done;
533 repolen = strlen(repo_abspath);
534 cwdlen = strlen(cwd);
535 len = strlen(path);
537 if (len > repolen && strncmp(path, repo_abspath, repolen) == 0)
538 is_repo_child = 1;
539 if (len > cwdlen && strncmp(path, cwd, cwdlen) == 0)
540 is_cwd_child = 1;
542 if (strcmp(path, repo_abspath) == 0) {
543 free(path);
544 path = strdup("");
545 if (path == NULL) {
546 err = got_error_from_errno("strdup");
547 goto done;
549 } else if (is_repo_child && is_cwd_child) {
550 char *child;
551 /* Strip common prefix with repository path. */
552 err = got_path_skip_common_ancestor(&child,
553 repo_abspath, path);
554 if (err)
555 goto done;
556 free(path);
557 path = child;
558 } else if (is_repo_child) {
559 /* Matched an on-disk path inside repository. */
560 if (got_repo_is_bare(repo)) {
561 /*
562 * Matched an on-disk path inside repository
563 * database. Treat as repository-relative.
564 */
565 } else {
566 char *child;
567 /* Strip common prefix with repository path. */
568 err = got_path_skip_common_ancestor(&child,
569 repo_abspath, path);
570 if (err)
571 goto done;
572 free(path);
573 path = child;
575 } else if (is_cwd_child) {
576 char *child;
577 /* Strip common prefix with cwd. */
578 err = got_path_skip_common_ancestor(&child, cwd,
579 path);
580 if (err)
581 goto done;
582 free(path);
583 path = child;
584 } else {
585 /*
586 * Matched unrelated on-disk path.
587 * Treat it as repository-relative.
588 */
592 /* Make in-repository path absolute */
593 if (path[0] != '/') {
594 char *abspath;
595 if (asprintf(&abspath, "/%s", path) == -1) {
596 err = got_error_from_errno("asprintf");
597 goto done;
599 free(path);
600 path = abspath;
603 done:
604 free(cwd);
605 free(canonpath);
606 if (err)
607 free(path);
608 else
609 *in_repo_path = path;
610 return err;
613 const struct got_error *
614 got_repo_cache_packidx(struct got_repository *repo, struct got_packidx *packidx)
616 const struct got_error *err = NULL;
617 int i;
619 for (i = 0; i < nitems(repo->packidx_cache); i++) {
620 if (repo->packidx_cache[i] == NULL)
621 break;
623 if (i == nitems(repo->packidx_cache)) {
624 err = got_packidx_close(repo->packidx_cache[i - 1]);
625 if (err)
626 return err;
629 /*
630 * Insert the new pack index at the front so it will
631 * be searched first in the future.
632 */
633 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
634 sizeof(repo->packidx_cache) -
635 sizeof(repo->packidx_cache[0]));
636 repo->packidx_cache[0] = packidx;
638 return NULL;
641 static int
642 is_packidx_filename(const char *name, size_t len)
644 if (len != GOT_PACKIDX_NAMELEN)
645 return 0;
647 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
648 return 0;
650 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
651 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
652 return 0;
654 return 1;
657 const struct got_error *
658 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
659 struct got_repository *repo, struct got_object_id *id)
661 const struct got_error *err;
662 char *path_packdir;
663 DIR *packdir;
664 struct dirent *dent;
665 char *path_packidx;
666 int i;
668 /* Search pack index cache. */
669 for (i = 0; i < nitems(repo->packidx_cache); i++) {
670 if (repo->packidx_cache[i] == NULL)
671 break;
672 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
673 if (*idx != -1) {
674 *packidx = repo->packidx_cache[i];
675 return NULL;
678 /* No luck. Search the filesystem. */
680 path_packdir = got_repo_get_path_objects_pack(repo);
681 if (path_packdir == NULL)
682 return got_error_from_errno("got_repo_get_path_objects_pack");
684 packdir = opendir(path_packdir);
685 if (packdir == NULL) {
686 if (errno == ENOENT)
687 err = got_error_no_obj(id);
688 else
689 err = got_error_from_errno2("opendir", path_packdir);
690 goto done;
693 while ((dent = readdir(packdir)) != NULL) {
694 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
695 continue;
697 if (asprintf(&path_packidx, "%s/%s", path_packdir,
698 dent->d_name) == -1) {
699 err = got_error_from_errno("asprintf");
700 goto done;
703 err = got_packidx_open(packidx, path_packidx, 0);
704 free(path_packidx);
705 if (err)
706 goto done;
708 *idx = got_packidx_get_object_idx(*packidx, id);
709 if (*idx != -1) {
710 err = NULL; /* found the object */
711 err = got_repo_cache_packidx(repo, *packidx);
712 goto done;
715 err = got_packidx_close(*packidx);
716 *packidx = NULL;
717 if (err)
718 goto done;
721 err = got_error_no_obj(id);
722 done:
723 free(path_packdir);
724 if (packdir && closedir(packdir) != 0 && err == NULL)
725 err = got_error_from_errno("closedir");
726 return err;
729 static const struct got_error *
730 read_packfile_hdr(int fd, struct got_packidx *packidx)
732 const struct got_error *err = NULL;
733 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
734 struct got_packfile_hdr hdr;
735 ssize_t n;
737 n = read(fd, &hdr, sizeof(hdr));
738 if (n < 0)
739 return got_error_from_errno("read");
740 if (n != sizeof(hdr))
741 return got_error(GOT_ERR_BAD_PACKFILE);
743 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
744 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
745 betoh32(hdr.nobjects) != totobj)
746 err = got_error(GOT_ERR_BAD_PACKFILE);
748 return err;
751 static const struct got_error *
752 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
754 const struct got_error *err = NULL;
756 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
757 if (*fd == -1)
758 return got_error_from_errno2("open", path_packfile);
760 if (packidx) {
761 err = read_packfile_hdr(*fd, packidx);
762 if (err) {
763 close(*fd);
764 *fd = -1;
768 return err;
771 const struct got_error *
772 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
773 const char *path_packfile, struct got_packidx *packidx)
775 const struct got_error *err = NULL;
776 struct got_pack *pack = NULL;
777 struct stat sb;
778 int i;
780 if (packp)
781 *packp = NULL;
783 for (i = 0; i < nitems(repo->packs); i++) {
784 pack = &repo->packs[i];
785 if (pack->path_packfile == NULL)
786 break;
787 if (strcmp(pack->path_packfile, path_packfile) == 0)
788 return NULL;
791 if (i == nitems(repo->packs) - 1) {
792 err = got_pack_close(&repo->packs[i - 1]);
793 if (err)
794 return err;
795 memmove(&repo->packs[1], &repo->packs[0],
796 sizeof(repo->packs) - sizeof(repo->packs[0]));
797 i = 0;
800 pack = &repo->packs[i];
802 pack->path_packfile = strdup(path_packfile);
803 if (pack->path_packfile == NULL) {
804 err = got_error_from_errno("strdup");
805 goto done;
808 err = open_packfile(&pack->fd, path_packfile, packidx);
809 if (err)
810 goto done;
812 if (fstat(pack->fd, &sb) != 0) {
813 err = got_error_from_errno("fstat");
814 goto done;
816 pack->filesize = sb.st_size;
818 pack->privsep_child = NULL;
820 #ifndef GOT_PACK_NO_MMAP
821 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
822 pack->fd, 0);
823 if (pack->map == MAP_FAILED) {
824 if (errno != ENOMEM) {
825 err = got_error_from_errno("mmap");
826 goto done;
828 pack->map = NULL; /* fall back to read(2) */
830 #endif
831 done:
832 if (err) {
833 if (pack) {
834 free(pack->path_packfile);
835 memset(pack, 0, sizeof(*pack));
837 } else if (packp)
838 *packp = pack;
839 return err;
842 struct got_pack *
843 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
845 struct got_pack *pack = NULL;
846 int i;
848 for (i = 0; i < nitems(repo->packs); i++) {
849 pack = &repo->packs[i];
850 if (pack->path_packfile == NULL)
851 break;
852 if (strcmp(pack->path_packfile, path_packfile) == 0)
853 return pack;
856 return NULL;
859 const struct got_error *
860 got_repo_init(const char *repo_path)
862 const struct got_error *err = NULL;
863 const char *dirnames[] = {
864 GOT_OBJECTS_DIR,
865 GOT_OBJECTS_PACK_DIR,
866 GOT_REFS_DIR,
867 };
868 const char *description_str = "Unnamed repository; "
869 "edit this file 'description' to name the repository.";
870 const char *headref_str = "ref: refs/heads/master";
871 const char *gitconfig_str = "[core]\n"
872 "\trepositoryformatversion = 0\n"
873 "\tfilemode = true\n"
874 "\tbare = true\n";
875 char *path;
876 int i;
878 if (!got_path_dir_is_empty(repo_path))
879 return got_error(GOT_ERR_DIR_NOT_EMPTY);
881 for (i = 0; i < nitems(dirnames); i++) {
882 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
883 return got_error_from_errno("asprintf");
885 err = got_path_mkdir(path);
886 free(path);
887 if (err)
888 return err;
891 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
892 return got_error_from_errno("asprintf");
893 err = got_path_create_file(path, description_str);
894 free(path);
895 if (err)
896 return err;
898 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
899 return got_error_from_errno("asprintf");
900 err = got_path_create_file(path, headref_str);
901 free(path);
902 if (err)
903 return err;
905 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
906 return got_error_from_errno("asprintf");
907 err = got_path_create_file(path, gitconfig_str);
908 free(path);
909 if (err)
910 return err;
912 return NULL;
915 static const struct got_error *
916 match_packed_object(struct got_object_id **unique_id,
917 struct got_repository *repo, const char *id_str_prefix, int obj_type)
919 const struct got_error *err = NULL;
920 char *path_packdir;
921 DIR *packdir;
922 struct dirent *dent;
923 char *path_packidx;
924 struct got_object_id_queue matched_ids;
926 SIMPLEQ_INIT(&matched_ids);
928 path_packdir = got_repo_get_path_objects_pack(repo);
929 if (path_packdir == NULL)
930 return got_error_from_errno("got_repo_get_path_objects_pack");
932 packdir = opendir(path_packdir);
933 if (packdir == NULL) {
934 if (errno != ENOENT)
935 err = got_error_from_errno2("opendir", path_packdir);
936 goto done;
939 while ((dent = readdir(packdir)) != NULL) {
940 struct got_packidx *packidx;
941 struct got_object_qid *qid;
944 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
945 continue;
947 if (asprintf(&path_packidx, "%s/%s", path_packdir,
948 dent->d_name) == -1) {
949 err = got_error_from_errno("asprintf");
950 break;
953 err = got_packidx_open(&packidx, path_packidx, 0);
954 free(path_packidx);
955 if (err)
956 break;
958 err = got_packidx_match_id_str_prefix(&matched_ids,
959 packidx, id_str_prefix);
960 if (err) {
961 got_packidx_close(packidx);
962 break;
964 err = got_packidx_close(packidx);
965 if (err)
966 break;
968 SIMPLEQ_FOREACH(qid, &matched_ids, entry) {
969 if (obj_type != GOT_OBJ_TYPE_ANY) {
970 int matched_type;
971 err = got_object_get_type(&matched_type, repo,
972 qid->id);
973 if (err)
974 goto done;
975 if (matched_type != obj_type)
976 continue;
978 if (*unique_id == NULL) {
979 *unique_id = got_object_id_dup(qid->id);
980 if (*unique_id == NULL) {
981 err = got_error_from_errno("malloc");
982 goto done;
984 } else {
985 err = got_error(GOT_ERR_AMBIGUOUS_ID);
986 goto done;
990 done:
991 got_object_id_queue_free(&matched_ids);
992 free(path_packdir);
993 if (packdir && closedir(packdir) != 0 && err == NULL)
994 err = got_error_from_errno("closedir");
995 if (err) {
996 free(*unique_id);
997 *unique_id = NULL;
999 return err;
1002 static const struct got_error *
1003 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1004 const char *object_dir, const char *id_str_prefix, int obj_type,
1005 struct got_repository *repo)
1007 const struct got_error *err = NULL;
1008 char *path;
1009 DIR *dir = NULL;
1010 struct dirent *dent;
1011 struct got_object_id id;
1013 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1014 err = got_error_from_errno("asprintf");
1015 goto done;
1018 dir = opendir(path);
1019 if (dir == NULL) {
1020 if (errno == ENOENT) {
1021 err = NULL;
1022 goto done;
1024 err = got_error_from_errno2("opendir", path);
1025 goto done;
1027 while ((dent = readdir(dir)) != NULL) {
1028 char *id_str;
1029 int cmp;
1031 if (strcmp(dent->d_name, ".") == 0 ||
1032 strcmp(dent->d_name, "..") == 0)
1033 continue;
1035 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1036 err = got_error_from_errno("asprintf");
1037 goto done;
1040 if (!got_parse_sha1_digest(id.sha1, id_str))
1041 continue;
1044 * Directory entries do not necessarily appear in
1045 * sorted order, so we must iterate over all of them.
1047 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1048 if (cmp != 0) {
1049 free(id_str);
1050 continue;
1053 if (*unique_id == NULL) {
1054 if (obj_type != GOT_OBJ_TYPE_ANY) {
1055 int matched_type;
1056 err = got_object_get_type(&matched_type, repo,
1057 &id);
1058 if (err)
1059 goto done;
1060 if (matched_type != obj_type)
1061 continue;
1063 *unique_id = got_object_id_dup(&id);
1064 if (*unique_id == NULL) {
1065 err = got_error_from_errno("got_object_id_dup");
1066 free(id_str);
1067 goto done;
1069 } else {
1070 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1071 free(id_str);
1072 goto done;
1075 done:
1076 if (dir && closedir(dir) != 0 && err == NULL)
1077 err = got_error_from_errno("closedir");
1078 if (err) {
1079 free(*unique_id);
1080 *unique_id = NULL;
1082 free(path);
1083 return err;
1086 const struct got_error *
1087 got_repo_match_object_id_prefix(struct got_object_id **id,
1088 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1090 const struct got_error *err = NULL;
1091 char *path_objects = got_repo_get_path_objects(repo);
1092 char *object_dir = NULL;
1093 size_t len;
1094 int i;
1096 *id = NULL;
1098 for (i = 0; i < strlen(id_str_prefix); i++) {
1099 if (isxdigit((unsigned char)id_str_prefix[i]))
1100 continue;
1101 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
1104 len = strlen(id_str_prefix);
1105 if (len >= 2) {
1106 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1107 if (err)
1108 goto done;
1109 object_dir = strndup(id_str_prefix, 2);
1110 if (object_dir == NULL) {
1111 err = got_error_from_errno("strdup");
1112 goto done;
1114 err = match_loose_object(id, path_objects, object_dir,
1115 id_str_prefix, obj_type, repo);
1116 } else if (len == 1) {
1117 int i;
1118 for (i = 0; i < 0xf; i++) {
1119 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1120 == -1) {
1121 err = got_error_from_errno("asprintf");
1122 goto done;
1124 err = match_packed_object(id, repo, object_dir,
1125 obj_type);
1126 if (err)
1127 goto done;
1128 err = match_loose_object(id, path_objects, object_dir,
1129 id_str_prefix, obj_type, repo);
1130 if (err)
1131 goto done;
1133 } else {
1134 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
1135 goto done;
1137 done:
1138 free(object_dir);
1139 if (err) {
1140 free(*id);
1141 *id = NULL;
1142 } else if (*id == NULL)
1143 err = got_error(GOT_ERR_NO_OBJ);
1145 return err;
1148 const struct got_error *
1149 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1150 int obj_type, struct got_repository *repo)
1152 const struct got_error *err;
1153 struct got_reflist_head refs;
1154 struct got_reflist_entry *re;
1155 struct got_object_id *tag_id;
1157 SIMPLEQ_INIT(&refs);
1158 *tag = NULL;
1160 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_by_name, NULL);
1161 if (err)
1162 return err;
1164 SIMPLEQ_FOREACH(re, &refs, entry) {
1165 const char *refname;
1166 refname = got_ref_get_name(re->ref);
1167 if (got_ref_is_symbolic(re->ref))
1168 continue;
1169 refname += strlen("refs/tags/");
1170 if (strcmp(refname, name) != 0)
1171 continue;
1172 err = got_ref_resolve(&tag_id, repo, re->ref);
1173 if (err)
1174 break;
1175 err = got_object_open_as_tag(tag, repo, tag_id);
1176 free(tag_id);
1177 if (err)
1178 break;
1179 if (obj_type == GOT_OBJ_TYPE_ANY ||
1180 got_object_tag_get_object_type(*tag) == obj_type)
1181 break;
1182 got_object_tag_close(*tag);
1183 *tag = NULL;
1186 got_ref_list_free(&refs);
1187 if (err == NULL && *tag == NULL)
1188 err = got_error(GOT_ERR_NO_OBJ);
1189 return err;
1192 static const struct got_error *
1193 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1194 const char *name, mode_t mode, struct got_object_id *blob_id)
1196 const struct got_error *err = NULL;
1198 *new_te = NULL;
1200 *new_te = calloc(1, sizeof(**new_te));
1201 if (*new_te == NULL)
1202 return got_error_from_errno("calloc");
1204 (*new_te)->name = strdup(name);
1205 if ((*new_te)->name == NULL) {
1206 err = got_error_from_errno("strdup");
1207 goto done;
1210 (*new_te)->mode = S_IFREG | (mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
1211 (*new_te)->id = blob_id;
1212 done:
1213 if (err && *new_te) {
1214 got_object_tree_entry_close(*new_te);
1215 *new_te = NULL;
1217 return err;
1220 static const struct got_error *
1221 import_file(struct got_tree_entry **new_te, struct dirent *de,
1222 const char *path, struct got_repository *repo)
1224 const struct got_error *err;
1225 struct got_object_id *blob_id = NULL;
1226 char *filepath;
1227 struct stat sb;
1229 if (asprintf(&filepath, "%s%s%s", path,
1230 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1231 return got_error_from_errno("asprintf");
1233 if (lstat(filepath, &sb) != 0) {
1234 err = got_error_from_errno2("lstat", path);
1235 goto done;
1238 err = got_object_blob_create(&blob_id, filepath, repo);
1239 if (err)
1240 goto done;
1242 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1243 blob_id);
1244 done:
1245 free(filepath);
1246 if (err)
1247 free(blob_id);
1248 return err;
1251 static const struct got_error *
1252 insert_tree_entry(struct got_tree_entry *new_te,
1253 struct got_pathlist_head *paths)
1255 const struct got_error *err = NULL;
1256 struct got_pathlist_entry *new_pe;
1258 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1259 if (err)
1260 return err;
1261 if (new_pe == NULL)
1262 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1263 return NULL;
1266 static const struct got_error *write_tree(struct got_object_id **,
1267 const char *, struct got_pathlist_head *, struct got_repository *,
1268 got_repo_import_cb progress_cb, void *progress_arg);
1270 static const struct got_error *
1271 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1272 const char *path, struct got_pathlist_head *ignores,
1273 struct got_repository *repo,
1274 got_repo_import_cb progress_cb, void *progress_arg)
1276 const struct got_error *err;
1277 char *subdirpath;
1279 if (asprintf(&subdirpath, "%s%s%s", path,
1280 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1281 return got_error_from_errno("asprintf");
1283 (*new_te) = calloc(1, sizeof(**new_te));
1284 (*new_te)->mode = S_IFDIR;
1285 (*new_te)->name = strdup(de->d_name);
1286 if ((*new_te)->name == NULL) {
1287 err = got_error_from_errno("strdup");
1288 goto done;
1291 err = write_tree(&(*new_te)->id, subdirpath, ignores, repo,
1292 progress_cb, progress_arg);
1293 done:
1294 free(subdirpath);
1295 if (err) {
1296 got_object_tree_entry_close(*new_te);
1297 *new_te = NULL;
1299 return err;
1302 static const struct got_error *
1303 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1304 struct got_pathlist_head *ignores, struct got_repository *repo,
1305 got_repo_import_cb progress_cb, void *progress_arg)
1307 const struct got_error *err = NULL;
1308 DIR *dir;
1309 struct dirent *de;
1310 struct got_tree_entries new_tree_entries;
1311 struct got_tree_entry *new_te = NULL;
1312 struct got_pathlist_head paths;
1313 struct got_pathlist_entry *pe;
1315 *new_tree_id = NULL;
1317 TAILQ_INIT(&paths);
1318 new_tree_entries.nentries = 0;
1319 SIMPLEQ_INIT(&new_tree_entries.head);
1321 dir = opendir(path_dir);
1322 if (dir == NULL) {
1323 err = got_error_from_errno2("opendir", path_dir);
1324 goto done;
1327 while ((de = readdir(dir)) != NULL) {
1328 int ignore = 0;
1330 if (strcmp(de->d_name, ".") == 0 ||
1331 strcmp(de->d_name, "..") == 0)
1332 continue;
1334 TAILQ_FOREACH(pe, ignores, entry) {
1335 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1336 ignore = 1;
1337 break;
1340 if (ignore)
1341 continue;
1342 if (de->d_type == DT_DIR) {
1343 err = import_subdir(&new_te, de, path_dir,
1344 ignores, repo, progress_cb, progress_arg);
1345 if (err)
1346 goto done;
1347 } else if (de->d_type == DT_REG) {
1348 err = import_file(&new_te, de, path_dir, repo);
1349 if (err)
1350 goto done;
1351 } else
1352 continue;
1354 err = insert_tree_entry(new_te, &paths);
1355 if (err)
1356 goto done;
1359 TAILQ_FOREACH(pe, &paths, entry) {
1360 struct got_tree_entry *te = pe->data;
1361 char *path;
1362 new_tree_entries.nentries++;
1363 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
1364 if (!S_ISREG(te->mode))
1365 continue;
1366 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1367 err = got_error_from_errno("asprintf");
1368 goto done;
1370 err = (*progress_cb)(progress_arg, path);
1371 free(path);
1372 if (err)
1373 goto done;
1376 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
1377 done:
1378 if (dir)
1379 closedir(dir);
1380 got_object_tree_entries_close(&new_tree_entries);
1381 got_pathlist_free(&paths);
1382 return err;
1385 const struct got_error *
1386 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1387 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1388 struct got_repository *repo, got_repo_import_cb progress_cb,
1389 void *progress_arg)
1391 const struct got_error *err;
1392 struct got_object_id *new_tree_id;
1394 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1395 progress_cb, progress_arg);
1396 if (err)
1397 return err;
1399 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1400 author, time(NULL), author, time(NULL), logmsg, repo);
1401 free(new_tree_id);
1402 return err;