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 <fcntl.h>
25 #include <limits.h>
26 #include <dirent.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <sha1.h>
30 #include <string.h>
31 #include <zlib.h>
32 #include <errno.h>
33 #include <libgen.h>
34 #include <stdint.h>
35 #include <imsg.h>
36 #include <uuid.h>
38 #include "got_error.h"
39 #include "got_reference.h"
40 #include "got_repository.h"
41 #include "got_path.h"
42 #include "got_worktree.h"
43 #include "got_object.h"
45 #include "got_lib_delta.h"
46 #include "got_lib_inflate.h"
47 #include "got_lib_object.h"
48 #include "got_lib_pack.h"
49 #include "got_lib_privsep.h"
50 #include "got_lib_worktree.h"
51 #include "got_lib_object_cache.h"
52 #include "got_lib_repository.h"
54 #ifndef nitems
55 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
56 #endif
58 #define GOT_GIT_DIR ".git"
60 /* Mandatory files and directories inside the git directory. */
61 #define GOT_OBJECTS_DIR "objects"
62 #define GOT_REFS_DIR "refs"
63 #define GOT_HEAD_FILE "HEAD"
65 /* Other files and directories inside the git directory. */
66 #define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
67 #define GOT_ORIG_HEAD_FILE "ORIG_HEAD"
68 #define GOT_OBJECTS_PACK_DIR "objects/pack"
69 #define GOT_PACKED_REFS_FILE "packed-refs"
71 const char *
72 got_repo_get_path(struct got_repository *repo)
73 {
74 return repo->path;
75 }
77 const char *
78 got_repo_get_path_git_dir(struct got_repository *repo)
79 {
80 return repo->path_git_dir;
81 }
83 int
84 got_repo_is_bare(struct got_repository *repo)
85 {
86 return (strcmp(repo->path, repo->path_git_dir) == 0);
87 }
89 static char *
90 get_path_git_child(struct got_repository *repo, const char *basename)
91 {
92 char *path_child;
94 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
95 basename) == -1)
96 return NULL;
98 return path_child;
99 }
101 char *
102 got_repo_get_path_objects(struct got_repository *repo)
104 return get_path_git_child(repo, GOT_OBJECTS_DIR);
107 char *
108 got_repo_get_path_objects_pack(struct got_repository *repo)
110 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
113 char *
114 got_repo_get_path_refs(struct got_repository *repo)
116 return get_path_git_child(repo, GOT_REFS_DIR);
119 char *
120 got_repo_get_path_packed_refs(struct got_repository *repo)
122 return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
125 static char *
126 get_path_head(struct got_repository *repo)
128 return get_path_git_child(repo, GOT_HEAD_FILE);
131 static int
132 is_git_repo(struct got_repository *repo)
134 const char *path_git = got_repo_get_path_git_dir(repo);
135 char *path_objects = got_repo_get_path_objects(repo);
136 char *path_refs = got_repo_get_path_refs(repo);
137 char *path_head = get_path_head(repo);
138 int ret = 0;
139 struct stat sb;
140 struct got_reference *head_ref;
142 if (lstat(path_git, &sb) == -1)
143 goto done;
144 if (!S_ISDIR(sb.st_mode))
145 goto done;
147 if (lstat(path_objects, &sb) == -1)
148 goto done;
149 if (!S_ISDIR(sb.st_mode))
150 goto done;
152 if (lstat(path_refs, &sb) == -1)
153 goto done;
154 if (!S_ISDIR(sb.st_mode))
155 goto done;
157 if (lstat(path_head, &sb) == -1)
158 goto done;
159 if (!S_ISREG(sb.st_mode))
160 goto done;
162 /* Check if the HEAD reference can be opened. */
163 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
164 goto done;
165 got_ref_close(head_ref);
167 ret = 1;
168 done:
169 free(path_objects);
170 free(path_refs);
171 free(path_head);
172 return ret;
176 const struct got_error *
177 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
178 struct got_object *obj)
180 #ifndef GOT_NO_OBJ_CACHE
181 const struct got_error *err = NULL;
182 err = got_object_cache_add(&repo->objcache, id, obj);
183 if (err) {
184 if (err->code == GOT_ERR_OBJ_EXISTS ||
185 err->code == GOT_ERR_OBJ_TOO_LARGE)
186 err = NULL;
187 return err;
189 obj->refcnt++;
190 #endif
191 return NULL;
194 struct got_object *
195 got_repo_get_cached_object(struct got_repository *repo,
196 struct got_object_id *id)
198 return (struct got_object *)got_object_cache_get(&repo->objcache, id);
201 const struct got_error *
202 got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
203 struct got_tree_object *tree)
205 #ifndef GOT_NO_OBJ_CACHE
206 const struct got_error *err = NULL;
207 err = got_object_cache_add(&repo->treecache, id, tree);
208 if (err) {
209 if (err->code == GOT_ERR_OBJ_EXISTS ||
210 err->code == GOT_ERR_OBJ_TOO_LARGE)
211 err = NULL;
212 return err;
214 tree->refcnt++;
215 #endif
216 return NULL;
219 struct got_tree_object *
220 got_repo_get_cached_tree(struct got_repository *repo,
221 struct got_object_id *id)
223 return (struct got_tree_object *)got_object_cache_get(
224 &repo->treecache, id);
227 const struct got_error *
228 got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
229 struct got_commit_object *commit)
231 #ifndef GOT_NO_OBJ_CACHE
232 const struct got_error *err = NULL;
233 err = got_object_cache_add(&repo->commitcache, id, commit);
234 if (err) {
235 if (err->code == GOT_ERR_OBJ_EXISTS ||
236 err->code == GOT_ERR_OBJ_TOO_LARGE)
237 err = NULL;
238 return err;
240 commit->refcnt++;
241 #endif
242 return NULL;
245 struct got_commit_object *
246 got_repo_get_cached_commit(struct got_repository *repo,
247 struct got_object_id *id)
249 return (struct got_commit_object *)got_object_cache_get(
250 &repo->commitcache, id);
253 const struct got_error *
254 got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
255 struct got_tag_object *tag)
257 #ifndef GOT_NO_OBJ_CACHE
258 const struct got_error *err = NULL;
259 err = got_object_cache_add(&repo->tagcache, id, tag);
260 if (err) {
261 if (err->code == GOT_ERR_OBJ_EXISTS ||
262 err->code == GOT_ERR_OBJ_TOO_LARGE)
263 err = NULL;
264 return err;
266 tag->refcnt++;
267 #endif
268 return NULL;
271 struct got_tag_object *
272 got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
274 return (struct got_tag_object *)got_object_cache_get(
275 &repo->tagcache, id);
278 const struct got_error *
279 open_repo(struct got_repository *repo, const char *path)
281 const struct got_error *err = NULL;
283 /* bare git repository? */
284 repo->path_git_dir = strdup(path);
285 if (repo->path_git_dir == NULL)
286 return got_error_from_errno("strdup");
287 if (is_git_repo(repo)) {
288 repo->path = strdup(repo->path_git_dir);
289 if (repo->path == NULL) {
290 err = got_error_from_errno("strdup");
291 goto done;
293 return NULL;
296 /* git repository with working tree? */
297 free(repo->path_git_dir);
298 if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
299 err = got_error_from_errno("asprintf");
300 goto done;
302 if (is_git_repo(repo)) {
303 repo->path = strdup(path);
304 if (repo->path == NULL) {
305 err = got_error_from_errno("strdup");
306 goto done;
308 return NULL;
311 err = got_error(GOT_ERR_NOT_GIT_REPO);
312 done:
313 if (err) {
314 free(repo->path);
315 repo->path = NULL;
316 free(repo->path_git_dir);
317 repo->path_git_dir = NULL;
319 return err;
322 const struct got_error *
323 got_repo_open(struct got_repository **repop, const char *path)
325 struct got_repository *repo = NULL;
326 const struct got_error *err = NULL;
327 char *abspath, *normpath = NULL;
328 int i, tried_root = 0;
330 *repop = NULL;
332 if (got_path_is_absolute(path))
333 abspath = strdup(path);
334 else
335 abspath = got_path_get_absolute(path);
336 if (abspath == NULL)
337 return got_error(GOT_ERR_BAD_PATH);
339 repo = calloc(1, sizeof(*repo));
340 if (repo == NULL) {
341 err = got_error_from_errno("calloc");
342 goto done;
345 for (i = 0; i < nitems(repo->privsep_children); i++) {
346 memset(&repo->privsep_children[i], 0,
347 sizeof(repo->privsep_children[0]));
348 repo->privsep_children[i].imsg_fd = -1;
351 err = got_object_cache_init(&repo->objcache,
352 GOT_OBJECT_CACHE_TYPE_OBJ);
353 if (err)
354 goto done;
355 err = got_object_cache_init(&repo->treecache,
356 GOT_OBJECT_CACHE_TYPE_TREE);
357 if (err)
358 goto done;
359 err = got_object_cache_init(&repo->commitcache,
360 GOT_OBJECT_CACHE_TYPE_COMMIT);
361 if (err)
362 goto done;
363 err = got_object_cache_init(&repo->tagcache,
364 GOT_OBJECT_CACHE_TYPE_TAG);
365 if (err)
366 goto done;
368 normpath = got_path_normalize(abspath);
369 if (normpath == NULL) {
370 err = got_error(GOT_ERR_BAD_PATH);
371 goto done;
374 path = normpath;
375 do {
376 err = open_repo(repo, path);
377 if (err == NULL)
378 break;
379 if (err->code != GOT_ERR_NOT_GIT_REPO)
380 break;
381 if (path[0] == '/' && path[1] == '\0') {
382 if (tried_root) {
383 err = got_error(GOT_ERR_NOT_GIT_REPO);
384 break;
386 tried_root = 1;
388 path = dirname(path);
389 if (path == NULL)
390 err = got_error_from_errno2("dirname", path);
391 } while (path);
392 done:
393 if (err)
394 got_repo_close(repo);
395 else
396 *repop = repo;
397 free(abspath);
398 free(normpath);
399 return err;
402 const struct got_error *
403 got_repo_close(struct got_repository *repo)
405 const struct got_error *err = NULL, *child_err;
406 int i;
408 for (i = 0; i < nitems(repo->packidx_cache); i++) {
409 if (repo->packidx_cache[i] == NULL)
410 break;
411 got_packidx_close(repo->packidx_cache[i]);
414 for (i = 0; i < nitems(repo->packs); i++) {
415 if (repo->packs[i].path_packfile == NULL)
416 break;
417 got_pack_close(&repo->packs[i]);
420 free(repo->path);
421 free(repo->path_git_dir);
423 got_object_cache_close(&repo->objcache);
424 got_object_cache_close(&repo->treecache);
425 got_object_cache_close(&repo->commitcache);
426 got_object_cache_close(&repo->tagcache);
428 for (i = 0; i < nitems(repo->privsep_children); i++) {
429 if (repo->privsep_children[i].imsg_fd == -1)
430 continue;
431 imsg_clear(repo->privsep_children[i].ibuf);
432 free(repo->privsep_children[i].ibuf);
433 err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
434 child_err = got_privsep_wait_for_child(
435 repo->privsep_children[i].pid);
436 if (child_err && err == NULL)
437 err = child_err;
438 if (close(repo->privsep_children[i].imsg_fd) != 0 &&
439 err == NULL)
440 err = got_error_from_errno("close");
442 free(repo);
444 return err;
447 const struct got_error *
448 got_repo_map_path(char **in_repo_path, struct got_repository *repo,
449 const char *input_path, int check_disk)
451 const struct got_error *err = NULL;
452 const char *repo_abspath = NULL;
453 size_t repolen, cwdlen, len;
454 char *cwd, *canonpath, *path = NULL;
456 *in_repo_path = NULL;
458 cwd = getcwd(NULL, 0);
459 if (cwd == NULL)
460 return got_error_from_errno("getcwd");
462 canonpath = strdup(input_path);
463 if (canonpath == NULL) {
464 err = got_error_from_errno("strdup");
465 goto done;
467 err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
468 if (err)
469 goto done;
471 repo_abspath = got_repo_get_path(repo);
473 if (!check_disk) {
474 path = strdup(canonpath);
475 if (path == NULL) {
476 err = got_error_from_errno("strdup");
477 goto done;
479 } else {
480 int is_repo_child = 0, is_cwd_child = 0;
482 path = realpath(canonpath, NULL);
483 if (path == NULL) {
484 if (errno != ENOENT) {
485 err = got_error_from_errno2("realpath",
486 canonpath);
487 goto done;
489 /*
490 * Path is not on disk.
491 * Assume it is already relative to repository root.
492 */
493 path = strdup(canonpath);
494 if (path == NULL) {
495 err = got_error_from_errno("strdup");
496 goto done;
500 repolen = strlen(repo_abspath);
501 cwdlen = strlen(cwd);
502 len = strlen(path);
504 if (len > repolen && strncmp(path, repo_abspath, repolen) == 0)
505 is_repo_child = 1;
506 if (len > cwdlen && strncmp(path, cwd, cwdlen) == 0)
507 is_cwd_child = 1;
509 if (strcmp(path, repo_abspath) == 0) {
510 free(path);
511 path = strdup("");
512 if (path == NULL) {
513 err = got_error_from_errno("strdup");
514 goto done;
516 } else if (is_repo_child && is_cwd_child) {
517 char *child;
518 /* Strip common prefix with repository path. */
519 err = got_path_skip_common_ancestor(&child,
520 repo_abspath, path);
521 if (err)
522 goto done;
523 free(path);
524 path = child;
525 } else if (is_repo_child) {
526 /* Matched an on-disk path inside repository. */
527 if (got_repo_is_bare(repo)) {
528 /*
529 * Matched an on-disk path inside repository
530 * database. Treat as repository-relative.
531 */
532 } else {
533 char *child;
534 /* Strip common prefix with repository path. */
535 err = got_path_skip_common_ancestor(&child,
536 repo_abspath, path);
537 if (err)
538 goto done;
539 free(path);
540 path = child;
542 } else if (is_cwd_child) {
543 char *child;
544 /* Strip common prefix with cwd. */
545 err = got_path_skip_common_ancestor(&child, cwd,
546 path);
547 if (err)
548 goto done;
549 free(path);
550 path = child;
551 } else {
552 /*
553 * Matched unrelated on-disk path.
554 * Treat it as repository-relative.
555 */
559 /* Make in-repository path absolute */
560 if (path[0] != '/') {
561 char *abspath;
562 if (asprintf(&abspath, "/%s", path) == -1) {
563 err = got_error_from_errno("asprintf");
564 goto done;
566 free(path);
567 path = abspath;
570 done:
571 free(cwd);
572 free(canonpath);
573 if (err)
574 free(path);
575 else
576 *in_repo_path = path;
577 return err;
580 const struct got_error *
581 got_repo_cache_packidx(struct got_repository *repo, struct got_packidx *packidx)
583 const struct got_error *err = NULL;
584 int i;
586 for (i = 0; i < nitems(repo->packidx_cache); i++) {
587 if (repo->packidx_cache[i] == NULL)
588 break;
590 if (i == nitems(repo->packidx_cache)) {
591 err = got_packidx_close(repo->packidx_cache[i - 1]);
592 if (err)
593 return err;
596 /*
597 * Insert the new pack index at the front so it will
598 * be searched first in the future.
599 */
600 memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
601 sizeof(repo->packidx_cache) -
602 sizeof(repo->packidx_cache[0]));
603 repo->packidx_cache[0] = packidx;
605 return NULL;
608 static int
609 is_packidx_filename(const char *name, size_t len)
611 if (len != GOT_PACKIDX_NAMELEN)
612 return 0;
614 if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
615 return 0;
617 if (strcmp(name + strlen(GOT_PACK_PREFIX) +
618 SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
619 return 0;
621 return 1;
624 const struct got_error *
625 got_repo_search_packidx(struct got_packidx **packidx, int *idx,
626 struct got_repository *repo, struct got_object_id *id)
628 const struct got_error *err;
629 char *path_packdir;
630 DIR *packdir;
631 struct dirent *dent;
632 char *path_packidx;
633 int i;
635 /* Search pack index cache. */
636 for (i = 0; i < nitems(repo->packidx_cache); i++) {
637 if (repo->packidx_cache[i] == NULL)
638 break;
639 *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
640 if (*idx != -1) {
641 *packidx = repo->packidx_cache[i];
642 return NULL;
645 /* No luck. Search the filesystem. */
647 path_packdir = got_repo_get_path_objects_pack(repo);
648 if (path_packdir == NULL)
649 return got_error_from_errno("got_repo_get_path_objects_pack");
651 packdir = opendir(path_packdir);
652 if (packdir == NULL) {
653 err = got_error_from_errno2("opendir", path_packdir);
654 goto done;
657 while ((dent = readdir(packdir)) != NULL) {
658 if (!is_packidx_filename(dent->d_name, dent->d_namlen))
659 continue;
661 if (asprintf(&path_packidx, "%s/%s", path_packdir,
662 dent->d_name) == -1) {
663 err = got_error_from_errno("asprintf");
664 goto done;
667 err = got_packidx_open(packidx, path_packidx, 0);
668 free(path_packidx);
669 if (err)
670 goto done;
672 *idx = got_packidx_get_object_idx(*packidx, id);
673 if (*idx != -1) {
674 err = NULL; /* found the object */
675 err = got_repo_cache_packidx(repo, *packidx);
676 goto done;
679 err = got_packidx_close(*packidx);
680 *packidx = NULL;
681 if (err)
682 goto done;
685 err = got_error_no_obj(id);
686 done:
687 free(path_packdir);
688 if (packdir && closedir(packdir) != 0 && err == 0)
689 err = got_error_from_errno("closedir");
690 return err;
693 static const struct got_error *
694 read_packfile_hdr(int fd, struct got_packidx *packidx)
696 const struct got_error *err = NULL;
697 uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
698 struct got_packfile_hdr hdr;
699 ssize_t n;
701 n = read(fd, &hdr, sizeof(hdr));
702 if (n < 0)
703 return got_error_from_errno("read");
704 if (n != sizeof(hdr))
705 return got_error(GOT_ERR_BAD_PACKFILE);
707 if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
708 betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
709 betoh32(hdr.nobjects) != totobj)
710 err = got_error(GOT_ERR_BAD_PACKFILE);
712 return err;
715 static const struct got_error *
716 open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
718 const struct got_error *err = NULL;
720 *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
721 if (*fd == -1)
722 return got_error_from_errno2("open", path_packfile);
724 if (packidx) {
725 err = read_packfile_hdr(*fd, packidx);
726 if (err) {
727 close(*fd);
728 *fd = -1;
732 return err;
735 const struct got_error *
736 got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
737 const char *path_packfile, struct got_packidx *packidx)
739 const struct got_error *err = NULL;
740 struct got_pack *pack = NULL;
741 struct stat sb;
742 int i;
744 if (packp)
745 *packp = NULL;
747 for (i = 0; i < nitems(repo->packs); i++) {
748 pack = &repo->packs[i];
749 if (pack->path_packfile == NULL)
750 break;
751 if (strcmp(pack->path_packfile, path_packfile) == 0)
752 return NULL;
755 if (i == nitems(repo->packs) - 1) {
756 err = got_pack_close(&repo->packs[i - 1]);
757 if (err)
758 return err;
759 memmove(&repo->packs[1], &repo->packs[0],
760 sizeof(repo->packs) - sizeof(repo->packs[0]));
761 i = 0;
764 pack = &repo->packs[i];
766 pack->path_packfile = strdup(path_packfile);
767 if (pack->path_packfile == NULL) {
768 err = got_error_from_errno("strdup");
769 goto done;
772 err = open_packfile(&pack->fd, path_packfile, packidx);
773 if (err)
774 goto done;
776 if (fstat(pack->fd, &sb) != 0) {
777 err = got_error_from_errno("fstat");
778 goto done;
780 pack->filesize = sb.st_size;
782 pack->privsep_child = NULL;
784 #ifndef GOT_PACK_NO_MMAP
785 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
786 pack->fd, 0);
787 if (pack->map == MAP_FAILED) {
788 if (errno != ENOMEM) {
789 err = got_error_from_errno("mmap");
790 goto done;
792 pack->map = NULL; /* fall back to read(2) */
794 #endif
795 done:
796 if (err) {
797 if (pack) {
798 free(pack->path_packfile);
799 memset(pack, 0, sizeof(*pack));
801 } else if (packp)
802 *packp = pack;
803 return err;
806 struct got_pack *
807 got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
809 struct got_pack *pack = NULL;
810 int i;
812 for (i = 0; i < nitems(repo->packs); i++) {
813 pack = &repo->packs[i];
814 if (pack->path_packfile == NULL)
815 break;
816 if (strcmp(pack->path_packfile, path_packfile) == 0)
817 return pack;
820 return NULL;
823 const struct got_error *
824 got_repo_init(const char *repo_path)
826 const struct got_error *err = NULL;
827 const char *dirnames[] = {
828 GOT_OBJECTS_DIR,
829 GOT_OBJECTS_PACK_DIR,
830 GOT_REFS_DIR,
831 };
832 const char *description_str = "Unnamed repository; "
833 "edit this file 'description' to name the repository.";
834 const char *headref_str = "ref: refs/heads/master";
835 const char *gitconfig_str = "[core]\n"
836 "\trepositoryformatversion = 0\n"
837 "\tfilemode = true\n"
838 "\tbare = true\n";
839 char *path;
840 int i;
842 if (!got_path_dir_is_empty(repo_path))
843 return got_error(GOT_ERR_DIR_NOT_EMPTY);
845 for (i = 0; i < nitems(dirnames); i++) {
846 if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
847 return got_error_from_errno("asprintf");
849 err = got_path_mkdir(path);
850 free(path);
851 if (err)
852 return err;
855 if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
856 return got_error_from_errno("asprintf");
857 err = got_path_create_file(path, description_str);
858 free(path);
859 if (err)
860 return err;
862 if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
863 return got_error_from_errno("asprintf");
864 err = got_path_create_file(path, headref_str);
865 free(path);
866 if (err)
867 return err;
869 if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
870 return got_error_from_errno("asprintf");
871 err = got_path_create_file(path, gitconfig_str);
872 free(path);
873 if (err)
874 return err;
876 return NULL;