Blame


1 7b19e0f1 2017-11-05 stsp /*
2 3b339b2f 2018-02-12 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 7b19e0f1 2017-11-05 stsp *
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.
7 7b19e0f1 2017-11-05 stsp *
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.
15 7b19e0f1 2017-11-05 stsp */
16 7b19e0f1 2017-11-05 stsp
17 79b11c62 2018-03-09 stsp #include <sys/queue.h>
18 deeca238 2018-03-12 stsp #include <sys/stat.h>
19 79b11c62 2018-03-09 stsp
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>
26 4027f31a 2017-11-04 stsp
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"
30 442a3ddc 2018-04-23 stsp #include "got_worktree.h"
31 7bb0daa1 2018-06-21 stsp #include "got_object.h"
32 4027f31a 2017-11-04 stsp
33 718b3ab0 2018-03-17 stsp #include "got_lib_path.h"
34 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
35 718b3ab0 2018-03-17 stsp #include "got_lib_zbuf.h"
36 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
37 718b3ab0 2018-03-17 stsp #include "got_lib_pack.h"
38 718b3ab0 2018-03-17 stsp #include "got_lib_repository.h"
39 442a3ddc 2018-04-23 stsp #include "got_lib_worktree.h"
40 50bc349d 2018-06-22 stsp #include "got_lib_object_idset.h"
41 c3f94f68 2017-11-05 stsp
42 79b11c62 2018-03-09 stsp #ifndef nitems
43 79b11c62 2018-03-09 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
44 79b11c62 2018-03-09 stsp #endif
45 3b339b2f 2018-02-12 stsp
46 4027f31a 2017-11-04 stsp #define GOT_GIT_DIR ".git"
47 4027f31a 2017-11-04 stsp
48 4027f31a 2017-11-04 stsp /* Mandatory files and directories inside the git directory. */
49 4df642d9 2017-11-05 stsp #define GOT_OBJECTS_DIR "objects"
50 4df642d9 2017-11-05 stsp #define GOT_REFS_DIR "refs"
51 4df642d9 2017-11-05 stsp #define GOT_HEAD_FILE "HEAD"
52 4027f31a 2017-11-04 stsp
53 a1fd68d8 2018-01-12 stsp /* Other files and directories inside the git directory. */
54 4df642d9 2017-11-05 stsp #define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
55 4df642d9 2017-11-05 stsp #define GOT_ORIG_HEAD_FILE "ORIG_HEAD"
56 a1fd68d8 2018-01-12 stsp #define GOT_OBJECTS_PACK_DIR "objects/pack"
57 4df642d9 2017-11-05 stsp
58 11995603 2017-11-05 stsp char *
59 86c3caaf 2018-03-09 stsp got_repo_get_path(struct got_repository *repo)
60 86c3caaf 2018-03-09 stsp {
61 86c3caaf 2018-03-09 stsp return strdup(repo->path);
62 86c3caaf 2018-03-09 stsp }
63 86c3caaf 2018-03-09 stsp
64 86c3caaf 2018-03-09 stsp char *
65 11995603 2017-11-05 stsp got_repo_get_path_git_dir(struct got_repository *repo)
66 4027f31a 2017-11-04 stsp {
67 4986b9d5 2018-03-12 stsp return strdup(repo->path_git_dir);
68 4027f31a 2017-11-04 stsp }
69 4027f31a 2017-11-04 stsp
70 4027f31a 2017-11-04 stsp static char *
71 4027f31a 2017-11-04 stsp get_path_git_child(struct got_repository *repo, const char *basename)
72 4027f31a 2017-11-04 stsp {
73 4027f31a 2017-11-04 stsp char *path_child;
74 4027f31a 2017-11-04 stsp
75 4986b9d5 2018-03-12 stsp if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
76 4027f31a 2017-11-04 stsp basename) == -1)
77 4027f31a 2017-11-04 stsp return NULL;
78 4027f31a 2017-11-04 stsp
79 4027f31a 2017-11-04 stsp return path_child;
80 4027f31a 2017-11-04 stsp }
81 4027f31a 2017-11-04 stsp
82 11995603 2017-11-05 stsp char *
83 11995603 2017-11-05 stsp got_repo_get_path_objects(struct got_repository *repo)
84 4027f31a 2017-11-04 stsp {
85 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_OBJECTS_DIR);
86 4027f31a 2017-11-04 stsp }
87 4027f31a 2017-11-04 stsp
88 11995603 2017-11-05 stsp char *
89 a1fd68d8 2018-01-12 stsp got_repo_get_path_objects_pack(struct got_repository *repo)
90 a1fd68d8 2018-01-12 stsp {
91 a1fd68d8 2018-01-12 stsp return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
92 a1fd68d8 2018-01-12 stsp }
93 a1fd68d8 2018-01-12 stsp
94 a1fd68d8 2018-01-12 stsp char *
95 11995603 2017-11-05 stsp got_repo_get_path_refs(struct got_repository *repo)
96 4027f31a 2017-11-04 stsp {
97 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_REFS_DIR);
98 4027f31a 2017-11-04 stsp }
99 4027f31a 2017-11-04 stsp
100 4027f31a 2017-11-04 stsp static char *
101 4027f31a 2017-11-04 stsp get_path_head(struct got_repository *repo)
102 4027f31a 2017-11-04 stsp {
103 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_HEAD_FILE);
104 4027f31a 2017-11-04 stsp }
105 4027f31a 2017-11-04 stsp
106 4027f31a 2017-11-04 stsp static int
107 4027f31a 2017-11-04 stsp is_git_repo(struct got_repository *repo)
108 4027f31a 2017-11-04 stsp {
109 11995603 2017-11-05 stsp char *path_git = got_repo_get_path_git_dir(repo);
110 11995603 2017-11-05 stsp char *path_objects = got_repo_get_path_objects(repo);
111 11995603 2017-11-05 stsp char *path_refs = got_repo_get_path_refs(repo);
112 4027f31a 2017-11-04 stsp char *path_head = get_path_head(repo);
113 deeca238 2018-03-12 stsp int ret = 0;
114 deeca238 2018-03-12 stsp struct stat sb;
115 4847cca1 2018-03-12 stsp struct got_reference *head_ref;
116 4027f31a 2017-11-04 stsp
117 deeca238 2018-03-12 stsp if (lstat(path_git, &sb) == -1)
118 deeca238 2018-03-12 stsp goto done;
119 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
120 deeca238 2018-03-12 stsp goto done;
121 4027f31a 2017-11-04 stsp
122 deeca238 2018-03-12 stsp if (lstat(path_objects, &sb) == -1)
123 deeca238 2018-03-12 stsp goto done;
124 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
125 deeca238 2018-03-12 stsp goto done;
126 deeca238 2018-03-12 stsp
127 deeca238 2018-03-12 stsp if (lstat(path_refs, &sb) == -1)
128 deeca238 2018-03-12 stsp goto done;
129 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
130 deeca238 2018-03-12 stsp goto done;
131 deeca238 2018-03-12 stsp
132 deeca238 2018-03-12 stsp if (lstat(path_head, &sb) == -1)
133 deeca238 2018-03-12 stsp goto done;
134 deeca238 2018-03-12 stsp if (!S_ISREG(sb.st_mode))
135 deeca238 2018-03-12 stsp goto done;
136 4847cca1 2018-03-12 stsp
137 4847cca1 2018-03-12 stsp /* Check if the HEAD reference can be opened. */
138 4847cca1 2018-03-12 stsp if (got_ref_open(&head_ref, repo, GOT_REF_HEAD) != NULL)
139 4847cca1 2018-03-12 stsp goto done;
140 4847cca1 2018-03-12 stsp got_ref_close(head_ref);
141 4847cca1 2018-03-12 stsp
142 deeca238 2018-03-12 stsp ret = 1;
143 deeca238 2018-03-12 stsp done:
144 4027f31a 2017-11-04 stsp free(path_git);
145 4027f31a 2017-11-04 stsp free(path_objects);
146 4027f31a 2017-11-04 stsp free(path_refs);
147 4027f31a 2017-11-04 stsp free(path_head);
148 4027f31a 2017-11-04 stsp return ret;
149 4027f31a 2017-11-04 stsp
150 4027f31a 2017-11-04 stsp }
151 4027f31a 2017-11-04 stsp
152 50bc349d 2018-06-22 stsp const struct got_error *
153 7bb0daa1 2018-06-21 stsp got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
154 7bb0daa1 2018-06-21 stsp struct got_object *obj)
155 7bb0daa1 2018-06-21 stsp {
156 50bc349d 2018-06-22 stsp const struct got_error *err = NULL;
157 54f20211 2018-06-22 stsp struct got_object_cache_entry *ce;
158 7bb0daa1 2018-06-21 stsp
159 54f20211 2018-06-22 stsp if (repo->objcache.ncached >= GOT_OBJECT_CACHE_SIZE) {
160 50bc349d 2018-06-22 stsp err = got_object_idset_remove_random((void **)&ce,
161 54f20211 2018-06-22 stsp repo->objcache.set);
162 50bc349d 2018-06-22 stsp if (err)
163 50bc349d 2018-06-22 stsp return err;
164 7bb0daa1 2018-06-21 stsp got_object_close(ce->obj);
165 7bb0daa1 2018-06-21 stsp free(ce);
166 54f20211 2018-06-22 stsp repo->objcache.ncached--;
167 7bb0daa1 2018-06-21 stsp }
168 7bb0daa1 2018-06-21 stsp
169 7bb0daa1 2018-06-21 stsp ce = calloc(1, sizeof(*ce));
170 7bb0daa1 2018-06-21 stsp if (ce == NULL)
171 7bb0daa1 2018-06-21 stsp return got_error_from_errno();
172 7bb0daa1 2018-06-21 stsp memcpy(&ce->id, id, sizeof(ce->id));
173 7bb0daa1 2018-06-21 stsp ce->obj = obj;
174 54f20211 2018-06-22 stsp err = got_object_idset_add(NULL, repo->objcache.set, id, ce);
175 50bc349d 2018-06-22 stsp if (err) {
176 50bc349d 2018-06-22 stsp if (err->code == GOT_ERR_OBJ_EXISTS) {
177 50bc349d 2018-06-22 stsp free(ce);
178 50bc349d 2018-06-22 stsp err = NULL;
179 50bc349d 2018-06-22 stsp }
180 50bc349d 2018-06-22 stsp } else {
181 50bc349d 2018-06-22 stsp obj->refcnt++;
182 54f20211 2018-06-22 stsp repo->objcache.ncached++;
183 50bc349d 2018-06-22 stsp }
184 50bc349d 2018-06-22 stsp
185 50bc349d 2018-06-22 stsp return err;
186 7bb0daa1 2018-06-21 stsp }
187 7bb0daa1 2018-06-21 stsp
188 7bb0daa1 2018-06-21 stsp struct got_object *
189 7bb0daa1 2018-06-21 stsp got_repo_get_cached_object(struct got_repository *repo,
190 7bb0daa1 2018-06-21 stsp struct got_object_id *id)
191 7bb0daa1 2018-06-21 stsp {
192 54f20211 2018-06-22 stsp struct got_object_cache_entry *ce;
193 54f20211 2018-06-22 stsp
194 54f20211 2018-06-22 stsp ce = got_object_idset_get(repo->objcache.set, id);
195 50bc349d 2018-06-22 stsp if (ce) {
196 54f20211 2018-06-22 stsp repo->objcache.cache_hit++;
197 7bb0daa1 2018-06-21 stsp return ce->obj;
198 7bb0daa1 2018-06-21 stsp }
199 54f20211 2018-06-22 stsp repo->objcache.cache_miss++;
200 7bb0daa1 2018-06-21 stsp return NULL;
201 7bb0daa1 2018-06-21 stsp }
202 7bb0daa1 2018-06-21 stsp
203 4027f31a 2017-11-04 stsp const struct got_error *
204 92af5469 2017-11-05 stsp got_repo_open(struct got_repository **ret, const char *path)
205 4027f31a 2017-11-04 stsp {
206 92af5469 2017-11-05 stsp struct got_repository *repo = NULL;
207 92af5469 2017-11-05 stsp const struct got_error *err = NULL;
208 2393f13b 2018-03-09 stsp char *abspath;
209 4027f31a 2017-11-04 stsp
210 2393f13b 2018-03-09 stsp if (got_path_is_absolute(path))
211 2393f13b 2018-03-09 stsp abspath = strdup(path);
212 2393f13b 2018-03-09 stsp else
213 2393f13b 2018-03-09 stsp abspath = got_path_get_absolute(path);
214 92af5469 2017-11-05 stsp if (abspath == NULL)
215 92af5469 2017-11-05 stsp return got_error(GOT_ERR_BAD_PATH);
216 4027f31a 2017-11-04 stsp
217 4027f31a 2017-11-04 stsp repo = calloc(1, sizeof(*repo));
218 92af5469 2017-11-05 stsp if (repo == NULL) {
219 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
220 92af5469 2017-11-05 stsp goto done;
221 92af5469 2017-11-05 stsp }
222 4027f31a 2017-11-04 stsp
223 54f20211 2018-06-22 stsp repo->objcache.set = got_object_idset_alloc();
224 54f20211 2018-06-22 stsp if (repo->objcache.set == NULL) {
225 50bc349d 2018-06-22 stsp err = got_error_from_errno();
226 50bc349d 2018-06-22 stsp goto done;
227 50bc349d 2018-06-22 stsp }
228 50bc349d 2018-06-22 stsp
229 4027f31a 2017-11-04 stsp repo->path = got_path_normalize(abspath);
230 92af5469 2017-11-05 stsp if (repo->path == NULL) {
231 92af5469 2017-11-05 stsp err = got_error(GOT_ERR_BAD_PATH);
232 92af5469 2017-11-05 stsp goto done;
233 92af5469 2017-11-05 stsp }
234 4027f31a 2017-11-04 stsp
235 4986b9d5 2018-03-12 stsp repo->path_git_dir = strdup(repo->path);
236 4986b9d5 2018-03-12 stsp if (repo->path_git_dir == NULL) {
237 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
238 92af5469 2017-11-05 stsp goto done;
239 92af5469 2017-11-05 stsp }
240 4986b9d5 2018-03-12 stsp if (!is_git_repo(repo)) {
241 4986b9d5 2018-03-12 stsp free(repo->path_git_dir);
242 4986b9d5 2018-03-12 stsp if (asprintf(&repo->path_git_dir, "%s/%s", repo->path,
243 4986b9d5 2018-03-12 stsp GOT_GIT_DIR) == -1) {
244 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
245 4986b9d5 2018-03-12 stsp goto done;
246 4986b9d5 2018-03-12 stsp }
247 4986b9d5 2018-03-12 stsp if (!is_git_repo(repo)) {
248 442a3ddc 2018-04-23 stsp struct got_worktree *worktree;
249 442a3ddc 2018-04-23 stsp if (got_worktree_open(&worktree, repo->path) == NULL) {
250 442a3ddc 2018-04-23 stsp free(repo->path_git_dir);
251 442a3ddc 2018-04-23 stsp repo->path_git_dir =
252 442a3ddc 2018-04-23 stsp strdup(worktree->repo_path);
253 442a3ddc 2018-04-23 stsp if (repo->path_git_dir == NULL) {
254 442a3ddc 2018-04-23 stsp err = got_error_from_errno();
255 442a3ddc 2018-04-23 stsp goto done;
256 442a3ddc 2018-04-23 stsp }
257 442a3ddc 2018-04-23 stsp if (!is_git_repo(repo)) {
258 442a3ddc 2018-04-23 stsp free(repo->path_git_dir);
259 442a3ddc 2018-04-23 stsp if (asprintf(&repo->path_git_dir,
260 442a3ddc 2018-04-23 stsp "%s/%s", worktree->repo_path,
261 442a3ddc 2018-04-23 stsp GOT_GIT_DIR) == -1) {
262 442a3ddc 2018-04-23 stsp err = got_error_from_errno();
263 442a3ddc 2018-04-23 stsp goto done;
264 442a3ddc 2018-04-23 stsp }
265 442a3ddc 2018-04-23 stsp }
266 442a3ddc 2018-04-23 stsp got_worktree_close(worktree);
267 442a3ddc 2018-04-23 stsp }
268 442a3ddc 2018-04-23 stsp }
269 442a3ddc 2018-04-23 stsp if (!is_git_repo(repo)) {
270 4986b9d5 2018-03-12 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
271 4986b9d5 2018-03-12 stsp goto done;
272 4986b9d5 2018-03-12 stsp }
273 4986b9d5 2018-03-12 stsp }
274 4027f31a 2017-11-04 stsp
275 4027f31a 2017-11-04 stsp *ret = repo;
276 92af5469 2017-11-05 stsp done:
277 92af5469 2017-11-05 stsp if (err)
278 4986b9d5 2018-03-12 stsp got_repo_close(repo);
279 92af5469 2017-11-05 stsp free(abspath);
280 92af5469 2017-11-05 stsp return err;
281 4027f31a 2017-11-04 stsp }
282 4027f31a 2017-11-04 stsp
283 4027f31a 2017-11-04 stsp void
284 4027f31a 2017-11-04 stsp got_repo_close(struct got_repository *repo)
285 4027f31a 2017-11-04 stsp {
286 79b11c62 2018-03-09 stsp int i;
287 79b11c62 2018-03-09 stsp
288 65cf1e80 2018-03-16 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
289 65cf1e80 2018-03-16 stsp if (repo->packidx_cache[i] == NULL)
290 79b11c62 2018-03-09 stsp break;
291 65cf1e80 2018-03-16 stsp got_packidx_close(repo->packidx_cache[i]);
292 79b11c62 2018-03-09 stsp }
293 bd1223b9 2018-03-14 stsp
294 7e656b93 2018-03-17 stsp for (i = 0; i < nitems(repo->packs); i++) {
295 7e656b93 2018-03-17 stsp if (repo->packs[i].path_packfile == NULL)
296 7e656b93 2018-03-17 stsp break;
297 7e656b93 2018-03-17 stsp got_pack_close(&repo->packs[i]);
298 7e656b93 2018-03-17 stsp }
299 7e656b93 2018-03-17 stsp
300 4027f31a 2017-11-04 stsp free(repo->path);
301 4986b9d5 2018-03-12 stsp free(repo->path_git_dir);
302 54f20211 2018-06-22 stsp if (repo->objcache.set)
303 54f20211 2018-06-22 stsp got_object_idset_free(repo->objcache.set);
304 4027f31a 2017-11-04 stsp free(repo);
305 4027f31a 2017-11-04 stsp }