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 goto done;
402 tried_root = 1;
404 path = dirname(path);
405 if (path == NULL) {
406 err = got_error_from_errno2("dirname", path);
407 goto done;
409 } while (path);
411 err = get_path_gitconfig(&gitconfig_path, repo);
412 if (err)
413 goto done;
415 #ifdef notyet
416 err = got_gitconfig_open(&repo->gitconfig, gitconfig_path);
417 if (err)
418 goto done;
419 #else
420 repo->gitconfig = NULL;
421 #endif
422 done:
423 if (err)
424 got_repo_close(repo);
425 else
426 *repop = repo;
427 free(abspath);
428 free(gitconfig_path);
429 return err;
432 const struct got_error *
433 got_repo_close(struct got_repository *repo)
435 const struct got_error *err = NULL, *child_err;
436 int i;
438 for (i = 0; i < nitems(repo->packidx_cache); i++) {
439 if (repo->packidx_cache[i] == NULL)
440 break;
441 got_packidx_close(repo->packidx_cache[i]);
444 for (i = 0; i < nitems(repo->packs); i++) {
445 if (repo->packs[i].path_packfile == NULL)
446 break;
447 got_pack_close(&repo->packs[i]);
450 free(repo->path);
451 free(repo->path_git_dir);
453 got_object_cache_close(&repo->objcache);
454 got_object_cache_close(&repo->treecache);
455 got_object_cache_close(&repo->commitcache);
456 got_object_cache_close(&repo->tagcache);
458 for (i = 0; i < nitems(repo->privsep_children); i++) {
459 if (repo->privsep_children[i].imsg_fd == -1)
460 continue;
461 imsg_clear(repo->privsep_children[i].ibuf);
462 free(repo->privsep_children[i].ibuf);
463 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
464 child_err = got_privsep_wait_for_child(
465 repo->privsep_children[i].pid);
466 if (child_err && err == NULL)
467 err = child_err;
468 if (close(repo->privsep_children[i].imsg_fd) != 0 &&
469 err == NULL)
470 err = got_error_from_errno("close");
472 if (repo->gitconfig)
473 got_gitconfig_close(repo->gitconfig);
474 free(repo);
476 return err;
479 const struct got_error *
480 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
481 const char *input_path, int check_disk)
483 const struct got_error *err = NULL;
484 const char *repo_abspath = NULL;
485 size_t repolen, cwdlen, len;
486 char *cwd, *canonpath, *path = NULL;
488 *in_repo_path = NULL;
490 cwd = getcwd(NULL, 0);
491 if (cwd == NULL)
492 return got_error_from_errno("getcwd");
494 canonpath = strdup(input_path);
495 if (canonpath == NULL) {
496 err = got_error_from_errno("strdup");
497 goto done;
499 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
500 if (err)
501 goto done;
503 repo_abspath = got_repo_get_path(repo);
505 if (!check_disk || canonpath[0] == '\0') {
506 path = strdup(canonpath);
507 if (path == NULL) {
508 err = got_error_from_errno("strdup");
509 goto done;
511 } else {
512 int is_repo_child = 0, is_cwd_child = 0;
514 path = realpath(canonpath, NULL);
515 if (path == NULL) {
516 if (errno != ENOENT) {
517 err = got_error_from_errno2("realpath",
518 canonpath);
519 goto done;
521 /*
522 * Path is not on disk.
523 * Assume it is already relative to repository root.
524 */
525 path = strdup(canonpath);
526 if (path == NULL) {
527 err = got_error_from_errno("strdup");
528 goto done;
532 repolen = strlen(repo_abspath);
533 cwdlen = strlen(cwd);
534 len = strlen(path);
536 if (len > repolen && strncmp(path, repo_abspath, repolen) == 0)
537 is_repo_child = 1;
538 if (len > cwdlen && strncmp(path, cwd, cwdlen) == 0)
539 is_cwd_child = 1;
541 if (strcmp(path, repo_abspath) == 0) {
542 free(path);
543 path = strdup("");
544 if (path == NULL) {
545 err = got_error_from_errno("strdup");
546 goto done;
548 } else if (is_repo_child && is_cwd_child) {
549 char *child;
550 /* Strip common prefix with repository path. */
551 err = got_path_skip_common_ancestor(&child,
552 repo_abspath, path);
553 if (err)
554 goto done;
555 free(path);
556 path = child;
557 } else if (is_repo_child) {
558 /* Matched an on-disk path inside repository. */
559 if (got_repo_is_bare(repo)) {
560 /*
561 * Matched an on-disk path inside repository
562 * database. Treat as repository-relative.
563 */
564 } else {
565 char *child;
566 /* Strip common prefix with repository path. */
567 err = got_path_skip_common_ancestor(&child,
568 repo_abspath, path);
569 if (err)
570 goto done;
571 free(path);
572 path = child;
574 } else if (is_cwd_child) {
575 char *child;
576 /* Strip common prefix with cwd. */
577 err = got_path_skip_common_ancestor(&child, cwd,
578 path);
579 if (err)
580 goto done;
581 free(path);
582 path = child;
583 } else {
584 /*
585 * Matched unrelated on-disk path.
586 * Treat it as repository-relative.
587 */
591 /* Make in-repository path absolute */
592 if (path[0] != '/') {
593 char *abspath;
594 if (asprintf(&abspath, "/%s", path) == -1) {
595 err = got_error_from_errno("asprintf");
596 goto done;
598 free(path);
599 path = abspath;
602 done:
603 free(cwd);
604 free(canonpath);
605 if (err)
606 free(path);
607 else
608 *in_repo_path = path;
609 return err;
612 const struct got_error *
613 got_repo_cache_packidx(struct got_repository *repo, struct got_packidx *packidx)
615 const struct got_error *err = NULL;
616 int i;
618 for (i = 0; i < nitems(repo->packidx_cache); i++) {
619 if (repo->packidx_cache[i] == NULL)
620 break;
622 if (i == nitems(repo->packidx_cache)) {
623 err = got_packidx_close(repo->packidx_cache[i - 1]);
624 if (err)
625 return err;
628 /*
629 * Insert the new pack index at the front so it will
630 * be searched first in the future.
631 */
632 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
633 sizeof(repo->packidx_cache) -
634 sizeof(repo->packidx_cache[0]));
635 repo->packidx_cache[0] = packidx;
637 return NULL;
640 static int
641 is_packidx_filename(const char *name, size_t len)
643 if (len != GOT_PACKIDX_NAMELEN)
644 return 0;
646 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
647 return 0;
649 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
650 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
651 return 0;
653 return 1;
656 const struct got_error *
657 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
658 struct got_repository *repo, struct got_object_id *id)
660 const struct got_error *err;
661 char *path_packdir;
662 DIR *packdir;
663 struct dirent *dent;
664 char *path_packidx;
665 int i;
667 /* Search pack index cache. */
668 for (i = 0; i < nitems(repo->packidx_cache); i++) {
669 if (repo->packidx_cache[i] == NULL)
670 break;
671 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
672 if (*idx != -1) {
673 *packidx = repo->packidx_cache[i];
674 return NULL;
677 /* No luck. Search the filesystem. */
679 path_packdir = got_repo_get_path_objects_pack(repo);
680 if (path_packdir == NULL)
681 return got_error_from_errno("got_repo_get_path_objects_pack");
683 packdir = opendir(path_packdir);
684 if (packdir == NULL) {
685 if (errno == ENOENT)
686 err = got_error_no_obj(id);
687 else
688 err = got_error_from_errno2("opendir", path_packdir);
689 goto done;
692 while ((dent = readdir(packdir)) != NULL) {
693 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
694 continue;
696 if (asprintf(&path_packidx, "%s/%s", path_packdir,
697 dent->d_name) == -1) {
698 err = got_error_from_errno("asprintf");
699 goto done;
702 err = got_packidx_open(packidx, path_packidx, 0);
703 free(path_packidx);
704 if (err)
705 goto done;
707 *idx = got_packidx_get_object_idx(*packidx, id);
708 if (*idx != -1) {
709 err = NULL; /* found the object */
710 err = got_repo_cache_packidx(repo, *packidx);
711 goto done;
714 err = got_packidx_close(*packidx);
715 *packidx = NULL;
716 if (err)
717 goto done;
720 err = got_error_no_obj(id);
721 done:
722 free(path_packdir);
723 if (packdir && closedir(packdir) != 0 && err == NULL)
724 err = got_error_from_errno("closedir");
725 return err;
728 static const struct got_error *
729 read_packfile_hdr(int fd, struct got_packidx *packidx)
731 const struct got_error *err = NULL;
732 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
733 struct got_packfile_hdr hdr;
734 ssize_t n;
736 n = read(fd, &hdr, sizeof(hdr));
737 if (n < 0)
738 return got_error_from_errno("read");
739 if (n != sizeof(hdr))
740 return got_error(GOT_ERR_BAD_PACKFILE);
742 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
743 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
744 betoh32(hdr.nobjects) != totobj)
745 err = got_error(GOT_ERR_BAD_PACKFILE);
747 return err;
750 static const struct got_error *
751 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
753 const struct got_error *err = NULL;
755 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
756 if (*fd == -1)
757 return got_error_from_errno2("open", path_packfile);
759 if (packidx) {
760 err = read_packfile_hdr(*fd, packidx);
761 if (err) {
762 close(*fd);
763 *fd = -1;
767 return err;
770 const struct got_error *
771 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
772 const char *path_packfile, struct got_packidx *packidx)
774 const struct got_error *err = NULL;
775 struct got_pack *pack = NULL;
776 struct stat sb;
777 int i;
779 if (packp)
780 *packp = NULL;
782 for (i = 0; i < nitems(repo->packs); i++) {
783 pack = &repo->packs[i];
784 if (pack->path_packfile == NULL)
785 break;
786 if (strcmp(pack->path_packfile, path_packfile) == 0)
787 return NULL;
790 if (i == nitems(repo->packs) - 1) {
791 err = got_pack_close(&repo->packs[i - 1]);
792 if (err)
793 return err;
794 memmove(&repo->packs[1], &repo->packs[0],
795 sizeof(repo->packs) - sizeof(repo->packs[0]));
796 i = 0;
799 pack = &repo->packs[i];
801 pack->path_packfile = strdup(path_packfile);
802 if (pack->path_packfile == NULL) {
803 err = got_error_from_errno("strdup");
804 goto done;
807 err = open_packfile(&pack->fd, path_packfile, packidx);
808 if (err)
809 goto done;
811 if (fstat(pack->fd, &sb) != 0) {
812 err = got_error_from_errno("fstat");
813 goto done;
815 pack->filesize = sb.st_size;
817 pack->privsep_child = NULL;
819 #ifndef GOT_PACK_NO_MMAP
820 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
821 pack->fd, 0);
822 if (pack->map == MAP_FAILED) {
823 if (errno != ENOMEM) {
824 err = got_error_from_errno("mmap");
825 goto done;
827 pack->map = NULL; /* fall back to read(2) */
829 #endif
830 done:
831 if (err) {
832 if (pack) {
833 free(pack->path_packfile);
834 memset(pack, 0, sizeof(*pack));
836 } else if (packp)
837 *packp = pack;
838 return err;
841 struct got_pack *
842 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
844 struct got_pack *pack = NULL;
845 int i;
847 for (i = 0; i < nitems(repo->packs); i++) {
848 pack = &repo->packs[i];
849 if (pack->path_packfile == NULL)
850 break;
851 if (strcmp(pack->path_packfile, path_packfile) == 0)
852 return pack;
855 return NULL;
858 const struct got_error *
859 got_repo_init(const char *repo_path)
861 const struct got_error *err = NULL;
862 const char *dirnames[] = {
863 GOT_OBJECTS_DIR,
864 GOT_OBJECTS_PACK_DIR,
865 GOT_REFS_DIR,
866 };
867 const char *description_str = "Unnamed repository; "
868 "edit this file 'description' to name the repository.";
869 const char *headref_str = "ref: refs/heads/master";
870 const char *gitconfig_str = "[core]\n"
871 "\trepositoryformatversion = 0\n"
872 "\tfilemode = true\n"
873 "\tbare = true\n";
874 char *path;
875 int i;
877 if (!got_path_dir_is_empty(repo_path))
878 return got_error(GOT_ERR_DIR_NOT_EMPTY);
880 for (i = 0; i < nitems(dirnames); i++) {
881 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
882 return got_error_from_errno("asprintf");
884 err = got_path_mkdir(path);
885 free(path);
886 if (err)
887 return err;
890 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
891 return got_error_from_errno("asprintf");
892 err = got_path_create_file(path, description_str);
893 free(path);
894 if (err)
895 return err;
897 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
898 return got_error_from_errno("asprintf");
899 err = got_path_create_file(path, headref_str);
900 free(path);
901 if (err)
902 return err;
904 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
905 return got_error_from_errno("asprintf");
906 err = got_path_create_file(path, gitconfig_str);
907 free(path);
908 if (err)
909 return err;
911 return NULL;
914 static const struct got_error *
915 match_packed_object(struct got_object_id **unique_id,
916 struct got_repository *repo, const char *id_str_prefix, int obj_type)
918 const struct got_error *err = NULL;
919 char *path_packdir;
920 DIR *packdir;
921 struct dirent *dent;
922 char *path_packidx;
923 struct got_object_id_queue matched_ids;
925 SIMPLEQ_INIT(&matched_ids);
927 path_packdir = got_repo_get_path_objects_pack(repo);
928 if (path_packdir == NULL)
929 return got_error_from_errno("got_repo_get_path_objects_pack");
931 packdir = opendir(path_packdir);
932 if (packdir == NULL) {
933 if (errno != ENOENT)
934 err = got_error_from_errno2("opendir", path_packdir);
935 goto done;
938 while ((dent = readdir(packdir)) != NULL) {
939 struct got_packidx *packidx;
940 struct got_object_qid *qid;
943 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
944 continue;
946 if (asprintf(&path_packidx, "%s/%s", path_packdir,
947 dent->d_name) == -1) {
948 err = got_error_from_errno("asprintf");
949 break;
952 err = got_packidx_open(&packidx, path_packidx, 0);
953 free(path_packidx);
954 if (err)
955 break;
957 err = got_packidx_match_id_str_prefix(&matched_ids,
958 packidx, id_str_prefix);
959 if (err) {
960 got_packidx_close(packidx);
961 break;
963 err = got_packidx_close(packidx);
964 if (err)
965 break;
967 SIMPLEQ_FOREACH(qid, &matched_ids, entry) {
968 if (obj_type != GOT_OBJ_TYPE_ANY) {
969 int matched_type;
970 err = got_object_get_type(&matched_type, repo,
971 qid->id);
972 if (err)
973 goto done;
974 if (matched_type != obj_type)
975 continue;
977 if (*unique_id == NULL) {
978 *unique_id = got_object_id_dup(qid->id);
979 if (*unique_id == NULL) {
980 err = got_error_from_errno("malloc");
981 goto done;
983 } else {
984 err = got_error(GOT_ERR_AMBIGUOUS_ID);
985 goto done;
989 done:
990 got_object_id_queue_free(&matched_ids);
991 free(path_packdir);
992 if (packdir && closedir(packdir) != 0 && err == NULL)
993 err = got_error_from_errno("closedir");
994 if (err) {
995 free(*unique_id);
996 *unique_id = NULL;
998 return err;
1001 static const struct got_error *
1002 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1003 const char *object_dir, const char *id_str_prefix, int obj_type,
1004 struct got_repository *repo)
1006 const struct got_error *err = NULL;
1007 char *path;
1008 DIR *dir = NULL;
1009 struct dirent *dent;
1010 struct got_object_id id;
1012 if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1013 err = got_error_from_errno("asprintf");
1014 goto done;
1017 dir = opendir(path);
1018 if (dir == NULL) {
1019 if (errno == ENOENT) {
1020 err = NULL;
1021 goto done;
1023 err = got_error_from_errno2("opendir", path);
1024 goto done;
1026 while ((dent = readdir(dir)) != NULL) {
1027 char *id_str;
1028 int cmp;
1030 if (strcmp(dent->d_name, ".") == 0 ||
1031 strcmp(dent->d_name, "..") == 0)
1032 continue;
1034 if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1035 err = got_error_from_errno("asprintf");
1036 goto done;
1039 if (!got_parse_sha1_digest(id.sha1, id_str))
1040 continue;
1043 * Directory entries do not necessarily appear in
1044 * sorted order, so we must iterate over all of them.
1046 cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1047 if (cmp != 0) {
1048 free(id_str);
1049 continue;
1052 if (*unique_id == NULL) {
1053 if (obj_type != GOT_OBJ_TYPE_ANY) {
1054 int matched_type;
1055 err = got_object_get_type(&matched_type, repo,
1056 &id);
1057 if (err)
1058 goto done;
1059 if (matched_type != obj_type)
1060 continue;
1062 *unique_id = got_object_id_dup(&id);
1063 if (*unique_id == NULL) {
1064 err = got_error_from_errno("got_object_id_dup");
1065 free(id_str);
1066 goto done;
1068 } else {
1069 err = got_error(GOT_ERR_AMBIGUOUS_ID);
1070 free(id_str);
1071 goto done;
1074 done:
1075 if (dir && closedir(dir) != 0 && err == NULL)
1076 err = got_error_from_errno("closedir");
1077 if (err) {
1078 free(*unique_id);
1079 *unique_id = NULL;
1081 free(path);
1082 return err;
1085 const struct got_error *
1086 got_repo_match_object_id_prefix(struct got_object_id **id,
1087 const char *id_str_prefix, int obj_type, struct got_repository *repo)
1089 const struct got_error *err = NULL;
1090 char *path_objects = got_repo_get_path_objects(repo);
1091 char *object_dir = NULL;
1092 size_t len;
1093 int i;
1095 *id = NULL;
1097 for (i = 0; i < strlen(id_str_prefix); i++) {
1098 if (isxdigit((unsigned char)id_str_prefix[i]))
1099 continue;
1100 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
1103 len = strlen(id_str_prefix);
1104 if (len >= 2) {
1105 err = match_packed_object(id, repo, id_str_prefix, obj_type);
1106 if (err)
1107 goto done;
1108 object_dir = strndup(id_str_prefix, 2);
1109 if (object_dir == NULL) {
1110 err = got_error_from_errno("strdup");
1111 goto done;
1113 err = match_loose_object(id, path_objects, object_dir,
1114 id_str_prefix, obj_type, repo);
1115 } else if (len == 1) {
1116 int i;
1117 for (i = 0; i < 0xf; i++) {
1118 if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1119 == -1) {
1120 err = got_error_from_errno("asprintf");
1121 goto done;
1123 err = match_packed_object(id, repo, object_dir,
1124 obj_type);
1125 if (err)
1126 goto done;
1127 err = match_loose_object(id, path_objects, object_dir,
1128 id_str_prefix, obj_type, repo);
1129 if (err)
1130 goto done;
1132 } else {
1133 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
1134 goto done;
1136 done:
1137 free(object_dir);
1138 if (err) {
1139 free(*id);
1140 *id = NULL;
1141 } else if (*id == NULL)
1142 err = got_error(GOT_ERR_NO_OBJ);
1144 return err;
1147 const struct got_error *
1148 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1149 int obj_type, struct got_repository *repo)
1151 const struct got_error *err;
1152 struct got_reflist_head refs;
1153 struct got_reflist_entry *re;
1154 struct got_object_id *tag_id;
1156 SIMPLEQ_INIT(&refs);
1157 *tag = NULL;
1159 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_by_name, NULL);
1160 if (err)
1161 return err;
1163 SIMPLEQ_FOREACH(re, &refs, entry) {
1164 const char *refname;
1165 refname = got_ref_get_name(re->ref);
1166 if (got_ref_is_symbolic(re->ref))
1167 continue;
1168 refname += strlen("refs/tags/");
1169 if (strcmp(refname, name) != 0)
1170 continue;
1171 err = got_ref_resolve(&tag_id, repo, re->ref);
1172 if (err)
1173 break;
1174 err = got_object_open_as_tag(tag, repo, tag_id);
1175 free(tag_id);
1176 if (err)
1177 break;
1178 if (obj_type == GOT_OBJ_TYPE_ANY ||
1179 got_object_tag_get_object_type(*tag) == obj_type)
1180 break;
1181 got_object_tag_close(*tag);
1182 *tag = NULL;
1185 got_ref_list_free(&refs);
1186 if (err == NULL && *tag == NULL)
1187 err = got_error(GOT_ERR_NO_OBJ);
1188 return err;
1191 static const struct got_error *
1192 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1193 const char *name, mode_t mode, struct got_object_id *blob_id)
1195 const struct got_error *err = NULL;
1197 *new_te = NULL;
1199 *new_te = calloc(1, sizeof(**new_te));
1200 if (*new_te == NULL)
1201 return got_error_from_errno("calloc");
1203 (*new_te)->name = strdup(name);
1204 if ((*new_te)->name == NULL) {
1205 err = got_error_from_errno("strdup");
1206 goto done;
1209 (*new_te)->mode = S_IFREG | (mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
1210 (*new_te)->id = blob_id;
1211 done:
1212 if (err && *new_te) {
1213 got_object_tree_entry_close(*new_te);
1214 *new_te = NULL;
1216 return err;
1219 static const struct got_error *
1220 import_file(struct got_tree_entry **new_te, struct dirent *de,
1221 const char *path, struct got_repository *repo)
1223 const struct got_error *err;
1224 struct got_object_id *blob_id = NULL;
1225 char *filepath;
1226 struct stat sb;
1228 if (asprintf(&filepath, "%s%s%s", path,
1229 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1230 return got_error_from_errno("asprintf");
1232 if (lstat(filepath, &sb) != 0) {
1233 err = got_error_from_errno2("lstat", path);
1234 goto done;
1237 err = got_object_blob_create(&blob_id, filepath, repo);
1238 if (err)
1239 goto done;
1241 err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1242 blob_id);
1243 done:
1244 free(filepath);
1245 if (err)
1246 free(blob_id);
1247 return err;
1250 static const struct got_error *
1251 insert_tree_entry(struct got_tree_entry *new_te,
1252 struct got_pathlist_head *paths)
1254 const struct got_error *err = NULL;
1255 struct got_pathlist_entry *new_pe;
1257 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1258 if (err)
1259 return err;
1260 if (new_pe == NULL)
1261 return got_error(GOT_ERR_TREE_DUP_ENTRY);
1262 return NULL;
1265 static const struct got_error *write_tree(struct got_object_id **,
1266 const char *, struct got_pathlist_head *, struct got_repository *,
1267 got_repo_import_cb progress_cb, void *progress_arg);
1269 static const struct got_error *
1270 import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1271 const char *path, struct got_pathlist_head *ignores,
1272 struct got_repository *repo,
1273 got_repo_import_cb progress_cb, void *progress_arg)
1275 const struct got_error *err;
1276 char *subdirpath;
1278 if (asprintf(&subdirpath, "%s%s%s", path,
1279 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1280 return got_error_from_errno("asprintf");
1282 (*new_te) = calloc(1, sizeof(**new_te));
1283 (*new_te)->mode = S_IFDIR;
1284 (*new_te)->name = strdup(de->d_name);
1285 if ((*new_te)->name == NULL) {
1286 err = got_error_from_errno("strdup");
1287 goto done;
1290 err = write_tree(&(*new_te)->id, subdirpath, ignores, repo,
1291 progress_cb, progress_arg);
1292 done:
1293 free(subdirpath);
1294 if (err) {
1295 got_object_tree_entry_close(*new_te);
1296 *new_te = NULL;
1298 return err;
1301 static const struct got_error *
1302 write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1303 struct got_pathlist_head *ignores, struct got_repository *repo,
1304 got_repo_import_cb progress_cb, void *progress_arg)
1306 const struct got_error *err = NULL;
1307 DIR *dir;
1308 struct dirent *de;
1309 struct got_tree_entries new_tree_entries;
1310 struct got_tree_entry *new_te = NULL;
1311 struct got_pathlist_head paths;
1312 struct got_pathlist_entry *pe;
1314 *new_tree_id = NULL;
1316 TAILQ_INIT(&paths);
1317 new_tree_entries.nentries = 0;
1318 SIMPLEQ_INIT(&new_tree_entries.head);
1320 dir = opendir(path_dir);
1321 if (dir == NULL) {
1322 err = got_error_from_errno2("opendir", path_dir);
1323 goto done;
1326 while ((de = readdir(dir)) != NULL) {
1327 int ignore = 0;
1329 if (strcmp(de->d_name, ".") == 0 ||
1330 strcmp(de->d_name, "..") == 0)
1331 continue;
1333 TAILQ_FOREACH(pe, ignores, entry) {
1334 if (fnmatch(pe->path, de->d_name, 0) == 0) {
1335 ignore = 1;
1336 break;
1339 if (ignore)
1340 continue;
1341 if (de->d_type == DT_DIR) {
1342 err = import_subdir(&new_te, de, path_dir,
1343 ignores, repo, progress_cb, progress_arg);
1344 if (err)
1345 goto done;
1346 } else if (de->d_type == DT_REG) {
1347 err = import_file(&new_te, de, path_dir, repo);
1348 if (err)
1349 goto done;
1350 } else
1351 continue;
1353 err = insert_tree_entry(new_te, &paths);
1354 if (err)
1355 goto done;
1358 TAILQ_FOREACH(pe, &paths, entry) {
1359 struct got_tree_entry *te = pe->data;
1360 char *path;
1361 new_tree_entries.nentries++;
1362 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
1363 if (!S_ISREG(te->mode))
1364 continue;
1365 if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1366 err = got_error_from_errno("asprintf");
1367 goto done;
1369 err = (*progress_cb)(progress_arg, path);
1370 free(path);
1371 if (err)
1372 goto done;
1375 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
1376 done:
1377 if (dir)
1378 closedir(dir);
1379 got_object_tree_entries_close(&new_tree_entries);
1380 got_pathlist_free(&paths);
1381 return err;
1384 const struct got_error *
1385 got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1386 const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1387 struct got_repository *repo, got_repo_import_cb progress_cb,
1388 void *progress_arg)
1390 const struct got_error *err;
1391 struct got_object_id *new_tree_id;
1393 err = write_tree(&new_tree_id, path_dir, ignores, repo,
1394 progress_cb, progress_arg);
1395 if (err)
1396 return err;
1398 err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1399 author, time(NULL), author, time(NULL), logmsg, repo);
1400 free(new_tree_id);
1401 return err;