2 3b339b2f 2018-02-12 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 7b19e0f1 2017-11-05 stsp * Permission to use, copy, modify, and distribute this software for any
5 7b19e0f1 2017-11-05 stsp * purpose with or without fee is hereby granted, provided that the above
6 7b19e0f1 2017-11-05 stsp * copyright notice and this permission notice appear in all copies.
8 7b19e0f1 2017-11-05 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 7b19e0f1 2017-11-05 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 7b19e0f1 2017-11-05 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 7b19e0f1 2017-11-05 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 7b19e0f1 2017-11-05 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 7b19e0f1 2017-11-05 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 7b19e0f1 2017-11-05 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 79b11c62 2018-03-09 stsp #include <sys/queue.h>
18 deeca238 2018-03-12 stsp #include <sys/stat.h>
20 4027f31a 2017-11-04 stsp #include <limits.h>
21 4027f31a 2017-11-04 stsp #include <stdlib.h>
22 4027f31a 2017-11-04 stsp #include <stdio.h>
23 4027f31a 2017-11-04 stsp #include <sha1.h>
24 4027f31a 2017-11-04 stsp #include <string.h>
25 79b11c62 2018-03-09 stsp #include <zlib.h>
27 4027f31a 2017-11-04 stsp #include "got_error.h"
28 5261c201 2018-04-01 stsp #include "got_reference.h"
29 4027f31a 2017-11-04 stsp #include "got_repository.h"
31 718b3ab0 2018-03-17 stsp #include "got_lib_path.h"
32 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
33 718b3ab0 2018-03-17 stsp #include "got_lib_zbuf.h"
34 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
35 718b3ab0 2018-03-17 stsp #include "got_lib_pack.h"
36 718b3ab0 2018-03-17 stsp #include "got_lib_repository.h"
38 79b11c62 2018-03-09 stsp #ifndef nitems
39 79b11c62 2018-03-09 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
42 4027f31a 2017-11-04 stsp #define GOT_GIT_DIR ".git"
44 4027f31a 2017-11-04 stsp /* Mandatory files and directories inside the git directory. */
45 4df642d9 2017-11-05 stsp #define GOT_OBJECTS_DIR "objects"
46 4df642d9 2017-11-05 stsp #define GOT_REFS_DIR "refs"
47 4df642d9 2017-11-05 stsp #define GOT_HEAD_FILE "HEAD"
49 a1fd68d8 2018-01-12 stsp /* Other files and directories inside the git directory. */
50 4df642d9 2017-11-05 stsp #define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
51 4df642d9 2017-11-05 stsp #define GOT_ORIG_HEAD_FILE "ORIG_HEAD"
52 a1fd68d8 2018-01-12 stsp #define GOT_OBJECTS_PACK_DIR "objects/pack"
55 86c3caaf 2018-03-09 stsp got_repo_get_path(struct got_repository *repo)
57 86c3caaf 2018-03-09 stsp return strdup(repo->path);
61 11995603 2017-11-05 stsp got_repo_get_path_git_dir(struct got_repository *repo)
63 4986b9d5 2018-03-12 stsp return strdup(repo->path_git_dir);
66 4027f31a 2017-11-04 stsp static char *
67 4027f31a 2017-11-04 stsp get_path_git_child(struct got_repository *repo, const char *basename)
69 4027f31a 2017-11-04 stsp char *path_child;
71 4986b9d5 2018-03-12 stsp if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
72 4027f31a 2017-11-04 stsp basename) == -1)
73 4027f31a 2017-11-04 stsp return NULL;
75 4027f31a 2017-11-04 stsp return path_child;
79 11995603 2017-11-05 stsp got_repo_get_path_objects(struct got_repository *repo)
81 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_OBJECTS_DIR);
85 a1fd68d8 2018-01-12 stsp got_repo_get_path_objects_pack(struct got_repository *repo)
87 a1fd68d8 2018-01-12 stsp return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
91 11995603 2017-11-05 stsp got_repo_get_path_refs(struct got_repository *repo)
93 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_REFS_DIR);
96 4027f31a 2017-11-04 stsp static char *
97 4027f31a 2017-11-04 stsp get_path_head(struct got_repository *repo)
99 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_HEAD_FILE);
103 4027f31a 2017-11-04 stsp is_git_repo(struct got_repository *repo)
105 11995603 2017-11-05 stsp char *path_git = got_repo_get_path_git_dir(repo);
106 11995603 2017-11-05 stsp char *path_objects = got_repo_get_path_objects(repo);
107 11995603 2017-11-05 stsp char *path_refs = got_repo_get_path_refs(repo);
108 4027f31a 2017-11-04 stsp char *path_head = get_path_head(repo);
109 deeca238 2018-03-12 stsp int ret = 0;
110 deeca238 2018-03-12 stsp struct stat sb;
111 4847cca1 2018-03-12 stsp struct got_reference *head_ref;
113 deeca238 2018-03-12 stsp if (lstat(path_git, &sb) == -1)
115 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
118 deeca238 2018-03-12 stsp if (lstat(path_objects, &sb) == -1)
120 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
123 deeca238 2018-03-12 stsp if (lstat(path_refs, &sb) == -1)
125 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
128 deeca238 2018-03-12 stsp if (lstat(path_head, &sb) == -1)
130 deeca238 2018-03-12 stsp if (!S_ISREG(sb.st_mode))
133 4847cca1 2018-03-12 stsp /* Check if the HEAD reference can be opened. */
134 4847cca1 2018-03-12 stsp if (got_ref_open(&head_ref, repo, GOT_REF_HEAD) != NULL)
136 4847cca1 2018-03-12 stsp got_ref_close(head_ref);
140 4027f31a 2017-11-04 stsp free(path_git);
141 4027f31a 2017-11-04 stsp free(path_objects);
142 4027f31a 2017-11-04 stsp free(path_refs);
143 4027f31a 2017-11-04 stsp free(path_head);
144 4027f31a 2017-11-04 stsp return ret;
148 4027f31a 2017-11-04 stsp const struct got_error *
149 92af5469 2017-11-05 stsp got_repo_open(struct got_repository **ret, const char *path)
151 92af5469 2017-11-05 stsp struct got_repository *repo = NULL;
152 92af5469 2017-11-05 stsp const struct got_error *err = NULL;
153 2393f13b 2018-03-09 stsp char *abspath;
155 2393f13b 2018-03-09 stsp if (got_path_is_absolute(path))
156 2393f13b 2018-03-09 stsp abspath = strdup(path);
158 2393f13b 2018-03-09 stsp abspath = got_path_get_absolute(path);
159 92af5469 2017-11-05 stsp if (abspath == NULL)
160 92af5469 2017-11-05 stsp return got_error(GOT_ERR_BAD_PATH);
162 4027f31a 2017-11-04 stsp repo = calloc(1, sizeof(*repo));
163 92af5469 2017-11-05 stsp if (repo == NULL) {
164 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
168 4027f31a 2017-11-04 stsp repo->path = got_path_normalize(abspath);
169 92af5469 2017-11-05 stsp if (repo->path == NULL) {
170 92af5469 2017-11-05 stsp err = got_error(GOT_ERR_BAD_PATH);
174 4986b9d5 2018-03-12 stsp repo->path_git_dir = strdup(repo->path);
175 4986b9d5 2018-03-12 stsp if (repo->path_git_dir == NULL) {
176 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
179 4986b9d5 2018-03-12 stsp if (!is_git_repo(repo)) {
180 4986b9d5 2018-03-12 stsp free(repo->path_git_dir);
181 4986b9d5 2018-03-12 stsp if (asprintf(&repo->path_git_dir, "%s/%s", repo->path,
182 4986b9d5 2018-03-12 stsp GOT_GIT_DIR) == -1) {
183 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
186 4986b9d5 2018-03-12 stsp if (!is_git_repo(repo)) {
187 4986b9d5 2018-03-12 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
192 4027f31a 2017-11-04 stsp *ret = repo;
195 4986b9d5 2018-03-12 stsp got_repo_close(repo);
196 92af5469 2017-11-05 stsp free(abspath);
197 92af5469 2017-11-05 stsp return err;
201 4027f31a 2017-11-04 stsp got_repo_close(struct got_repository *repo)
205 65cf1e80 2018-03-16 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
206 65cf1e80 2018-03-16 stsp if (repo->packidx_cache[i] == NULL)
208 65cf1e80 2018-03-16 stsp got_packidx_close(repo->packidx_cache[i]);
211 7e656b93 2018-03-17 stsp for (i = 0; i < nitems(repo->packs); i++) {
212 7e656b93 2018-03-17 stsp if (repo->packs[i].path_packfile == NULL)
214 7e656b93 2018-03-17 stsp got_pack_close(&repo->packs[i]);
217 4027f31a 2017-11-04 stsp free(repo->path);
218 4986b9d5 2018-03-12 stsp free(repo->path_git_dir);
219 4027f31a 2017-11-04 stsp free(repo);