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 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
316 err = got_error_from_errno("asprintf");
317 goto done;
319 if (is_git_repo(repo)) {
320 repo->path = strdup(path);
321 if (repo->path == NULL) {
322 err = got_error_from_errno("strdup");
323 goto done;
325 return NULL;
328 err = got_error(GOT_ERR_NOT_GIT_REPO);
329 done:
330 if (err) {
331 free(repo->path);
332 repo->path = NULL;
333 free(repo->path_git_dir);
334 repo->path_git_dir = NULL;
336 return err;
339 const struct got_error *
340 got_repo_open(struct got_repository **repop, const char *path)
342 struct got_repository *repo = NULL;
343 const struct got_error *err = NULL;
344 char *abspath, *gitconfig_path = NULL;
345 int i, tried_root = 0;
347 *repop = NULL;
349 if (got_path_is_absolute(path))
350 abspath = strdup(path);
351 else
352 abspath = got_path_get_absolute(path);
353 if (abspath == NULL)
354 return got_error(GOT_ERR_BAD_PATH);
356 repo = calloc(1, sizeof(*repo));
357 if (repo == NULL) {
358 err = got_error_from_errno("calloc");
359 goto done;
362 for (i = 0; i < nitems(repo->privsep_children); i++) {
363 memset(&repo->privsep_children[i], 0,
364 sizeof(repo->privsep_children[0]));
365 repo->privsep_children[i].imsg_fd = -1;
368 err = got_object_cache_init(&repo->objcache,
369 GOT_OBJECT_CACHE_TYPE_OBJ);
370 if (err)
371 goto done;
372 err = got_object_cache_init(&repo->treecache,
373 GOT_OBJECT_CACHE_TYPE_TREE);
374 if (err)
375 goto done;
376 err = got_object_cache_init(&repo->commitcache,
377 GOT_OBJECT_CACHE_TYPE_COMMIT);
378 if (err)
379 goto done;
380 err = got_object_cache_init(&repo->tagcache,
381 GOT_OBJECT_CACHE_TYPE_TAG);
382 if (err)
383 goto done;
385 path = realpath(abspath, NULL);
386 if (path == NULL) {
387 err = got_error_from_errno2("realpath", abspath);
388 goto done;
391 do {
392 err = open_repo(repo, path);
393 if (err == NULL)
394 break;
395 if (err->code != GOT_ERR_NOT_GIT_REPO)
396 break;
397 if (path[0] == '/' && path[1] == '\0') {
398 if (tried_root) {
399 err = got_error(GOT_ERR_NOT_GIT_REPO);
400 break;
402 tried_root = 1;
404 path = dirname(path);
405 if (path == NULL)
406 err = got_error_from_errno2("dirname", path);
407 } while (path);
409 err = get_path_gitconfig(&gitconfig_path, repo);
410 if (err)
411 goto done;
413 #ifdef notyet
414 err = got_gitconfig_open(&repo->gitconfig, gitconfig_path);
415 if (err)
416 goto done;
417 #else
418 repo->gitconfig = NULL;
419 #endif
420 done:
421 if (err)
422 got_repo_close(repo);
423 else
424 *repop = repo;
425 free(abspath);
426 free(gitconfig_path);
427 return err;
430 const struct got_error *
431 got_repo_close(struct got_repository *repo)
433 const struct got_error *err = NULL, *child_err;
434 int i;
436 for (i = 0; i < nitems(repo->packidx_cache); i++) {
437 if (repo->packidx_cache[i] == NULL)
438 break;
439 got_packidx_close(repo->packidx_cache[i]);
442 for (i = 0; i < nitems(repo->packs); i++) {
443 if (repo->packs[i].path_packfile == NULL)
444 break;
445 got_pack_close(&repo->packs[i]);
448 free(repo->path);
449 free(repo->path_git_dir);
451 got_object_cache_close(&repo->objcache);
452 got_object_cache_close(&repo->treecache);
453 got_object_cache_close(&repo->commitcache);
454 got_object_cache_close(&repo->tagcache);
456 for (i = 0; i < nitems(repo->privsep_children); i++) {
457 if (repo->privsep_children[i].imsg_fd == -1)
458 continue;
459 imsg_clear(repo->privsep_children[i].ibuf);
460 free(repo->privsep_children[i].ibuf);
461 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
462 child_err = got_privsep_wait_for_child(
463 repo->privsep_children[i].pid);
464 if (child_err && err == NULL)
465 err = child_err;
466 if (close(repo->privsep_children[i].imsg_fd) != 0 &&
467 err == NULL)
468 err = got_error_from_errno("close");
470 if (repo->gitconfig)
471 got_gitconfig_close(repo->gitconfig);
472 free(repo);
474 return err;
477 const struct got_error *
478 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
479 const char *input_path, int check_disk)
481 const struct got_error *err = NULL;
482 const char *repo_abspath = NULL;
483 size_t repolen, cwdlen, len;
484 char *cwd, *canonpath, *path = NULL;
486 *in_repo_path = NULL;
488 cwd = getcwd(NULL, 0);
489 if (cwd == NULL)
490 return got_error_from_errno("getcwd");
492 canonpath = strdup(input_path);
493 if (canonpath == NULL) {
494 err = got_error_from_errno("strdup");
495 goto done;
497 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
498 if (err)
499 goto done;
501 repo_abspath = got_repo_get_path(repo);
503 if (!check_disk || canonpath[0] == '\0') {
504 path = strdup(canonpath);
505 if (path == NULL) {
506 err = got_error_from_errno("strdup");
507 goto done;
509 } else {
510 int is_repo_child = 0, is_cwd_child = 0;
512 path = realpath(canonpath, NULL);
513 if (path == NULL) {
514 if (errno != ENOENT) {
515 err = got_error_from_errno2("realpath",
516 canonpath);
517 goto done;
519 /*
520 * Path is not on disk.
521 * Assume it is already relative to repository root.
522 */
523 path = strdup(canonpath);
524 if (path == NULL) {
525 err = got_error_from_errno("strdup");
526 goto done;
530 repolen = strlen(repo_abspath);
531 cwdlen = strlen(cwd);
532 len = strlen(path);
534 if (len > repolen && strncmp(path, repo_abspath, repolen) == 0)
535 is_repo_child = 1;
536 if (len > cwdlen && strncmp(path, cwd, cwdlen) == 0)
537 is_cwd_child = 1;
539 if (strcmp(path, repo_abspath) == 0) {
540 free(path);
541 path = strdup("");
542 if (path == NULL) {
543 err = got_error_from_errno("strdup");
544 goto done;
546 } else if (is_repo_child && is_cwd_child) {
547 char *child;
548 /* Strip common prefix with repository path. */
549 err = got_path_skip_common_ancestor(&child,
550 repo_abspath, path);
551 if (err)
552 goto done;
553 free(path);
554 path = child;
555 } else if (is_repo_child) {
556 /* Matched an on-disk path inside repository. */
557 if (got_repo_is_bare(repo)) {
558 /*
559 * Matched an on-disk path inside repository
560 * database. Treat as repository-relative.
561 */
562 } else {
563 char *child;
564 /* Strip common prefix with repository path. */
565 err = got_path_skip_common_ancestor(&child,
566 repo_abspath, path);
567 if (err)
568 goto done;
569 free(path);
570 path = child;
572 } else if (is_cwd_child) {
573 char *child;
574 /* Strip common prefix with cwd. */
575 err = got_path_skip_common_ancestor(&child, cwd,
576 path);
577 if (err)
578 goto done;
579 free(path);
580 path = child;
581 } else {
582 /*
583 * Matched unrelated on-disk path.
584 * Treat it as repository-relative.
585 */
589 /* Make in-repository path absolute */
590 if (path[0] != '/') {
591 char *abspath;
592 if (asprintf(&abspath, "/%s", path) == -1) {
593 err = got_error_from_errno("asprintf");
594 goto done;
596 free(path);
597 path = abspath;
600 done:
601 free(cwd);
602 free(canonpath);
603 if (err)
604 free(path);
605 else
606 *in_repo_path = path;
607 return err;
610 const struct got_error *
611 got_repo_cache_packidx(struct got_repository *repo, struct got_packidx *packidx)
613 const struct got_error *err = NULL;
614 int i;
616 for (i = 0; i < nitems(repo->packidx_cache); i++) {
617 if (repo->packidx_cache[i] == NULL)
618 break;
620 if (i == nitems(repo->packidx_cache)) {
621 err = got_packidx_close(repo->packidx_cache[i - 1]);
622 if (err)
623 return err;
626 /*
627 * Insert the new pack index at the front so it will
628 * be searched first in the future.
629 */
630 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
631 sizeof(repo->packidx_cache) -
632 sizeof(repo->packidx_cache[0]));
633 repo->packidx_cache[0] = packidx;
635 return NULL;
638 static int
639 is_packidx_filename(const char *name, size_t len)
641 if (len != GOT_PACKIDX_NAMELEN)
642 return 0;
644 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
645 return 0;
647 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
648 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
649 return 0;
651 return 1;
654 const struct got_error *
655 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
656 struct got_repository *repo, struct got_object_id *id)
658 const struct got_error *err;
659 char *path_packdir;
660 DIR *packdir;
661 struct dirent *dent;
662 char *path_packidx;
663 int i;
665 /* Search pack index cache. */
666 for (i = 0; i < nitems(repo->packidx_cache); i++) {
667 if (repo->packidx_cache[i] == NULL)
668 break;
669 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
670 if (*idx != -1) {
671 *packidx = repo->packidx_cache[i];
672 return NULL;
675 /* No luck. Search the filesystem. */
677 path_packdir = got_repo_get_path_objects_pack(repo);
678 if (path_packdir == NULL)
679 return got_error_from_errno("got_repo_get_path_objects_pack");
681 packdir = opendir(path_packdir);
682 if (packdir == NULL) {
683 if (errno == ENOENT)
684 err = got_error_no_obj(id);
685 else
686 err = got_error_from_errno2("opendir", path_packdir);
687 goto done;
690 while ((dent = readdir(packdir)) != NULL) {
691 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
692 continue;
694 if (asprintf(&path_packidx, "%s/%s", path_packdir,
695 dent->d_name) == -1) {
696 err = got_error_from_errno("asprintf");
697 goto done;
700 err = got_packidx_open(packidx, path_packidx, 0);
701 free(path_packidx);
702 if (err)
703 goto done;
705 *idx = got_packidx_get_object_idx(*packidx, id);
706 if (*idx != -1) {
707 err = NULL; /* found the object */
708 err = got_repo_cache_packidx(repo, *packidx);
709 goto done;
712 err = got_packidx_close(*packidx);
713 *packidx = NULL;
714 if (err)
715 goto done;
718 err = got_error_no_obj(id);
719 done:
720 free(path_packdir);
721 if (packdir && closedir(packdir) != 0 && err == NULL)
722 err = got_error_from_errno("closedir");
723 return err;
726 static const struct got_error *
727 read_packfile_hdr(int fd, struct got_packidx *packidx)
729 const struct got_error *err = NULL;
730 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
731 struct got_packfile_hdr hdr;
732 ssize_t n;
734 n = read(fd, &hdr, sizeof(hdr));
735 if (n < 0)
736 return got_error_from_errno("read");
737 if (n != sizeof(hdr))
738 return got_error(GOT_ERR_BAD_PACKFILE);
740 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
741 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
742 betoh32(hdr.nobjects) != totobj)
743 err = got_error(GOT_ERR_BAD_PACKFILE);
745 return err;
748 static const struct got_error *
749 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
751 const struct got_error *err = NULL;
753 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
754 if (*fd == -1)
755 return got_error_from_errno2("open", path_packfile);
757 if (packidx) {
758 err = read_packfile_hdr(*fd, packidx);
759 if (err) {
760 close(*fd);
761 *fd = -1;
765 return err;
768 const struct got_error *
769 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
770 const char *path_packfile, struct got_packidx *packidx)
772 const struct got_error *err = NULL;
773 struct got_pack *pack = NULL;
774 struct stat sb;
775 int i;
777 if (packp)
778 *packp = NULL;
780 for (i = 0; i < nitems(repo->packs); i++) {
781 pack = &repo->packs[i];
782 if (pack->path_packfile == NULL)
783 break;
784 if (strcmp(pack->path_packfile, path_packfile) == 0)
785 return NULL;
788 if (i == nitems(repo->packs) - 1) {
789 err = got_pack_close(&repo->packs[i - 1]);
790 if (err)
791 return err;
792 memmove(&repo->packs[1], &repo->packs[0],
793 sizeof(repo->packs) - sizeof(repo->packs[0]));
794 i = 0;
797 pack = &repo->packs[i];
799 pack->path_packfile = strdup(path_packfile);
800 if (pack->path_packfile == NULL) {
801 err = got_error_from_errno("strdup");
802 goto done;
805 err = open_packfile(&pack->fd, path_packfile, packidx);
806 if (err)
807 goto done;
809 if (fstat(pack->fd, &sb) != 0) {
810 err = got_error_from_errno("fstat");
811 goto done;
813 pack->filesize = sb.st_size;
815 pack->privsep_child = NULL;
817 #ifndef GOT_PACK_NO_MMAP
818 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
819 pack->fd, 0);
820 if (pack->map == MAP_FAILED) {
821 if (errno != ENOMEM) {
822 err = got_error_from_errno("mmap");
823 goto done;
825 pack->map = NULL; /* fall back to read(2) */
827 #endif
828 done:
829 if (err) {
830 if (pack) {
831 free(pack->path_packfile);
832 memset(pack, 0, sizeof(*pack));
834 } else if (packp)
835 *packp = pack;
836 return err;
839 struct got_pack *
840 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
842 struct got_pack *pack = NULL;
843 int i;
845 for (i = 0; i < nitems(repo->packs); i++) {
846 pack = &repo->packs[i];
847 if (pack->path_packfile == NULL)
848 break;
849 if (strcmp(pack->path_packfile, path_packfile) == 0)
850 return pack;
853 return NULL;
856 const struct got_error *
857 got_repo_init(const char *repo_path)
859 const struct got_error *err = NULL;
860 const char *dirnames[] = {
861 GOT_OBJECTS_DIR,
862 GOT_OBJECTS_PACK_DIR,
863 GOT_REFS_DIR,
864 };
865 const char *description_str = "Unnamed repository; "
866 "edit this file 'description' to name the repository.";
867 const char *headref_str = "ref: refs/heads/master";
868 const char *gitconfig_str = "[core]\n"
869 "\trepositoryformatversion = 0\n"
870 "\tfilemode = true\n"
871 "\tbare = true\n";
872 char *path;
873 int i;
875 if (!got_path_dir_is_empty(repo_path))
876 return got_error(GOT_ERR_DIR_NOT_EMPTY);
878 for (i = 0; i < nitems(dirnames); i++) {
879 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
880 return got_error_from_errno("asprintf");
882 err = got_path_mkdir(path);
883 free(path);
884 if (err)
885 return err;
888 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
889 return got_error_from_errno("asprintf");
890 err = got_path_create_file(path, description_str);
891 free(path);
892 if (err)
893 return err;
895 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
896 return got_error_from_errno("asprintf");
897 err = got_path_create_file(path, headref_str);
898 free(path);
899 if (err)
900 return err;
902 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
903 return got_error_from_errno("asprintf");
904 err = got_path_create_file(path, gitconfig_str);
905 free(path);
906 if (err)
907 return err;
909 return NULL;
912 static const struct got_error *
913 match_packed_object(struct got_object_id **unique_id,
914 struct got_repository *repo, const char *id_str_prefix, int obj_type)
916 const struct got_error *err = NULL;
917 char *path_packdir;
918 DIR *packdir;
919 struct dirent *dent;
920 char *path_packidx;
921 struct got_object_id_queue matched_ids;
923 SIMPLEQ_INIT(&matched_ids);
925 path_packdir = got_repo_get_path_objects_pack(repo);
926 if (path_packdir == NULL)
927 return got_error_from_errno("got_repo_get_path_objects_pack");
929 packdir = opendir(path_packdir);
930 if (packdir == NULL) {
931 if (errno != ENOENT)
932 err = got_error_from_errno2("opendir", path_packdir);
933 goto done;
936 while ((dent = readdir(packdir)) != NULL) {
937 struct got_packidx *packidx;
938 struct got_object_qid *qid;
941 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
942 continue;
944 if (asprintf(&path_packidx, "%s/%s", path_packdir,
945 dent->d_name) == -1) {
946 err = got_error_from_errno("asprintf");
947 break;
950 err = got_packidx_open(&packidx, path_packidx, 0);
951 free(path_packidx);
952 if (err)
953 break;
955 err = got_packidx_match_id_str_prefix(&matched_ids,
956 packidx, id_str_prefix);
957 if (err) {
958 got_packidx_close(packidx);
959 break;
961 err = got_packidx_close(packidx);
962 if (err)
963 break;
965 SIMPLEQ_FOREACH(qid, &matched_ids, entry) {
966 if (obj_type != GOT_OBJ_TYPE_ANY) {
967 int matched_type;
968 err = got_object_get_type(&matched_type, repo,
969 qid->id);
970 if (err)
971 goto done;
972 if (matched_type != obj_type)
973 continue;
975 if (*unique_id == NULL) {
976 *unique_id = got_object_id_dup(qid->id);
977 if (*unique_id == NULL) {
978 err = got_error_from_errno("malloc");
979 goto done;
981 } else {
982 err = got_error(GOT_ERR_AMBIGUOUS_ID);
983 goto done;
987 done:
988 got_object_id_queue_free(&matched_ids);
989 free(path_packdir);
990 if (packdir && closedir(packdir) != 0 && err == NULL)
991 err = got_error_from_errno("closedir");
992 if (err) {
993 free(*unique_id);
994 *unique_id = NULL;
996 return err;
999 static const struct got_error *
1000 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1001 const char *object_dir, const char *id_str_prefix, int obj_type,
1002 struct got_repository *repo)
1004 const struct got_error *err = NULL;
1005 char *path;
1006 DIR *dir = NULL;
1007 struct dirent *dent;
1008 struct got_object_id id;
1010 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1011 err = got_error_from_errno("asprintf");
1012 goto done;
1015 dir = opendir(path);
1016 if (dir == NULL) {
1017 if (errno == ENOENT) {
1018 err = NULL;
1019 goto done;
1021 err = got_error_from_errno2("opendir", path);
1022 goto done;
1024 while ((dent = readdir(dir)) != NULL) {
1025 char *id_str;
1026 int cmp;
1028 if (strcmp(dent->d_name, ".") == 0 ||
1029 strcmp(dent->d_name, "..") == 0)
1030 continue;
1032 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1033 err = got_error_from_errno("asprintf");
1034 goto done;
1037 if (!got_parse_sha1_digest(id.sha1, id_str))
1038 continue;
1041 * Directory entries do not necessarily appear in
1042 * sorted order, so we must iterate over all of them.
1044 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1045 if (cmp != 0) {
1046 free(id_str);
1047 continue;
1050 if (*unique_id == NULL) {
1051 if (obj_type != GOT_OBJ_TYPE_ANY) {
1052 int matched_type;
1053 err = got_object_get_type(&matched_type, repo,
1054 &id);
1055 if (err)
1056 goto done;
1057 if (matched_type != obj_type)
1058 continue;
1060 *unique_id = got_object_id_dup(&id);
1061 if (*unique_id == NULL) {
1062 err = got_error_from_errno("got_object_id_dup");
1063 free(id_str);
1064 goto done;
1066 } else {
1067 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1068 free(id_str);
1069 goto done;
1072 done:
1073 if (dir && closedir(dir) != 0 && err == NULL)
1074 err = got_error_from_errno("closedir");
1075 if (err) {
1076 free(*unique_id);
1077 *unique_id = NULL;
1079 free(path);
1080 return err;
1083 const struct got_error *
1084 got_repo_match_object_id_prefix(struct got_object_id **id,
1085 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1087 const struct got_error *err = NULL;
1088 char *path_objects = got_repo_get_path_objects(repo);
1089 char *object_dir = NULL;
1090 size_t len;
1091 int i;
1093 *id = NULL;
1095 for (i = 0; i < strlen(id_str_prefix); i++) {
1096 if (isxdigit((unsigned char)id_str_prefix[i]))
1097 continue;
1098 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
1101 len = strlen(id_str_prefix);
1102 if (len >= 2) {
1103 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1104 if (err)
1105 goto done;
1106 object_dir = strndup(id_str_prefix, 2);
1107 if (object_dir == NULL) {
1108 err = got_error_from_errno("strdup");
1109 goto done;
1111 err = match_loose_object(id, path_objects, object_dir,
1112 id_str_prefix, obj_type, repo);
1113 } else if (len == 1) {
1114 int i;
1115 for (i = 0; i < 0xf; i++) {
1116 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1117 == -1) {
1118 err = got_error_from_errno("asprintf");
1119 goto done;
1121 err = match_packed_object(id, repo, object_dir,
1122 obj_type);
1123 if (err)
1124 goto done;
1125 err = match_loose_object(id, path_objects, object_dir,
1126 id_str_prefix, obj_type, repo);
1127 if (err)
1128 goto done;
1130 } else {
1131 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
1132 goto done;
1134 done:
1135 free(object_dir);
1136 if (err) {
1137 free(*id);
1138 *id = NULL;
1139 } else if (*id == NULL)
1140 err = got_error(GOT_ERR_NO_OBJ);
1142 return err;
1145 const struct got_error *
1146 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1147 int obj_type, struct got_repository *repo)
1149 const struct got_error *err;
1150 struct got_reflist_head refs;
1151 struct got_reflist_entry *re;
1152 struct got_object_id *tag_id;
1154 SIMPLEQ_INIT(&refs);
1155 *tag = NULL;
1157 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_by_name, NULL);
1158 if (err)
1159 return err;
1161 SIMPLEQ_FOREACH(re, &refs, entry) {
1162 const char *refname;
1163 refname = got_ref_get_name(re->ref);
1164 if (got_ref_is_symbolic(re->ref))
1165 continue;
1166 refname += strlen("refs/tags/");
1167 if (strcmp(refname, name) != 0)
1168 continue;
1169 err = got_ref_resolve(&tag_id, repo, re->ref);
1170 if (err)
1171 break;
1172 err = got_object_open_as_tag(tag, repo, tag_id);
1173 free(tag_id);
1174 if (err)
1175 break;
1176 if (obj_type == GOT_OBJ_TYPE_ANY ||
1177 got_object_tag_get_object_type(*tag) == obj_type)
1178 break;
1179 got_object_tag_close(*tag);
1180 *tag = NULL;
1183 got_ref_list_free(&refs);
1184 if (err == NULL && *tag == NULL)
1185 err = got_error(GOT_ERR_NO_OBJ);
1186 return err;
1189 static const struct got_error *
1190 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1191 const char *name, mode_t mode, struct got_object_id *blob_id)
1193 const struct got_error *err = NULL;
1195 *new_te = NULL;
1197 *new_te = calloc(1, sizeof(**new_te));
1198 if (*new_te == NULL)
1199 return got_error_from_errno("calloc");
1201 (*new_te)->name = strdup(name);
1202 if ((*new_te)->name == NULL) {
1203 err = got_error_from_errno("strdup");
1204 goto done;
1207 (*new_te)->mode = S_IFREG | (mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
1208 (*new_te)->id = blob_id;
1209 done:
1210 if (err && *new_te) {
1211 got_object_tree_entry_close(*new_te);
1212 *new_te = NULL;
1214 return err;
1217 static const struct got_error *
1218 import_file(struct got_tree_entry **new_te, struct dirent *de,
1219 const char *path, struct got_repository *repo)
1221 const struct got_error *err;
1222 struct got_object_id *blob_id = NULL;
1223 char *filepath;
1224 struct stat sb;
1226 if (asprintf(&filepath, "%s%s%s", path,
1227 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1228 return got_error_from_errno("asprintf");
1230 if (lstat(filepath, &sb) != 0) {
1231 err = got_error_from_errno2("lstat", path);
1232 goto done;
1235 err = got_object_blob_create(&blob_id, filepath, repo);
1236 if (err)
1237 goto done;
1239 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1240 blob_id);
1241 done:
1242 free(filepath);
1243 if (err)
1244 free(blob_id);
1245 return err;
1248 static const struct got_error *
1249 insert_tree_entry(struct got_tree_entry *new_te,
1250 struct got_pathlist_head *paths)
1252 const struct got_error *err = NULL;
1253 struct got_pathlist_entry *new_pe;
1255 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1256 if (err)
1257 return err;
1258 if (new_pe == NULL)
1259 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1260 return NULL;
1263 static const struct got_error *write_tree(struct got_object_id **,
1264 const char *, struct got_pathlist_head *, struct got_repository *,
1265 got_repo_import_cb progress_cb, void *progress_arg);
1267 static const struct got_error *
1268 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1269 const char *path, struct got_pathlist_head *ignores,
1270 struct got_repository *repo,
1271 got_repo_import_cb progress_cb, void *progress_arg)
1273 const struct got_error *err;
1274 char *subdirpath;
1276 if (asprintf(&subdirpath, "%s%s%s", path,
1277 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1278 return got_error_from_errno("asprintf");
1280 (*new_te) = calloc(1, sizeof(**new_te));
1281 (*new_te)->mode = S_IFDIR;
1282 (*new_te)->name = strdup(de->d_name);
1283 if ((*new_te)->name == NULL) {
1284 err = got_error_from_errno("strdup");
1285 goto done;
1288 err = write_tree(&(*new_te)->id, subdirpath, ignores, repo,
1289 progress_cb, progress_arg);
1290 done:
1291 free(subdirpath);
1292 if (err) {
1293 got_object_tree_entry_close(*new_te);
1294 *new_te = NULL;
1296 return err;
1299 static const struct got_error *
1300 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1301 struct got_pathlist_head *ignores, struct got_repository *repo,
1302 got_repo_import_cb progress_cb, void *progress_arg)
1304 const struct got_error *err = NULL;
1305 DIR *dir;
1306 struct dirent *de;
1307 struct got_tree_entries new_tree_entries;
1308 struct got_tree_entry *new_te = NULL;
1309 struct got_pathlist_head paths;
1310 struct got_pathlist_entry *pe;
1312 *new_tree_id = NULL;
1314 TAILQ_INIT(&paths);
1315 new_tree_entries.nentries = 0;
1316 SIMPLEQ_INIT(&new_tree_entries.head);
1318 dir = opendir(path_dir);
1319 if (dir == NULL) {
1320 err = got_error_from_errno2("opendir", path_dir);
1321 goto done;
1324 while ((de = readdir(dir)) != NULL) {
1325 int ignore = 0;
1327 if (strcmp(de->d_name, ".") == 0 ||
1328 strcmp(de->d_name, "..") == 0)
1329 continue;
1331 TAILQ_FOREACH(pe, ignores, entry) {
1332 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1333 ignore = 1;
1334 break;
1337 if (ignore)
1338 continue;
1339 if (de->d_type == DT_DIR) {
1340 err = import_subdir(&new_te, de, path_dir,
1341 ignores, repo, progress_cb, progress_arg);
1342 if (err)
1343 goto done;
1344 } else if (de->d_type == DT_REG) {
1345 err = import_file(&new_te, de, path_dir, repo);
1346 if (err)
1347 goto done;
1348 } else
1349 continue;
1351 err = insert_tree_entry(new_te, &paths);
1352 if (err)
1353 goto done;
1356 TAILQ_FOREACH(pe, &paths, entry) {
1357 struct got_tree_entry *te = pe->data;
1358 char *path;
1359 new_tree_entries.nentries++;
1360 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
1361 if (!S_ISREG(te->mode))
1362 continue;
1363 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1364 err = got_error_from_errno("asprintf");
1365 goto done;
1367 err = (*progress_cb)(progress_arg, path);
1368 free(path);
1369 if (err)
1370 goto done;
1373 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
1374 done:
1375 if (dir)
1376 closedir(dir);
1377 got_object_tree_entries_close(&new_tree_entries);
1378 got_pathlist_free(&paths);
1379 return err;
1382 const struct got_error *
1383 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1384 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1385 struct got_repository *repo, got_repo_import_cb progress_cb,
1386 void *progress_arg)
1388 const struct got_error *err;
1389 struct got_object_id *new_tree_id;
1391 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1392 progress_cb, progress_arg);
1393 if (err)
1394 return err;
1396 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1397 author, time(NULL), author, time(NULL), logmsg, repo);
1398 free(new_tree_id);
1399 return err;