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 63581804 2018-07-09 stsp #include "got_lib_inflate.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 eb77ee11 2018-07-08 stsp #include "got_lib_object_idcache.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 f6be5c30 2018-06-22 stsp static const struct got_error *
153 f6be5c30 2018-06-22 stsp cache_add(struct got_object_cache *cache, struct got_object_id *id, void *item)
154 7bb0daa1 2018-06-21 stsp {
155 50bc349d 2018-06-22 stsp const struct got_error *err = NULL;
156 54f20211 2018-06-22 stsp struct got_object_cache_entry *ce;
157 a9bf0c2c 2018-06-22 stsp int nelem;
158 7bb0daa1 2018-06-21 stsp
159 eb77ee11 2018-07-08 stsp nelem = got_object_idcache_num_elements(cache->idcache);
160 4307e577 2018-06-22 stsp if (nelem >= cache->size) {
161 eb77ee11 2018-07-08 stsp err = got_object_idcache_remove_least_used((void **)&ce,
162 eb77ee11 2018-07-08 stsp cache->idcache);
163 50bc349d 2018-06-22 stsp if (err)
164 50bc349d 2018-06-22 stsp return err;
165 f6be5c30 2018-06-22 stsp switch (cache->type) {
166 f6be5c30 2018-06-22 stsp case GOT_OBJECT_CACHE_TYPE_OBJ:
167 f6be5c30 2018-06-22 stsp got_object_close(ce->data.obj);
168 f6be5c30 2018-06-22 stsp break;
169 f6be5c30 2018-06-22 stsp case GOT_OBJECT_CACHE_TYPE_TREE:
170 f6be5c30 2018-06-22 stsp got_object_tree_close(ce->data.tree);
171 f6be5c30 2018-06-22 stsp break;
172 1943de01 2018-06-22 stsp case GOT_OBJECT_CACHE_TYPE_COMMIT:
173 1943de01 2018-06-22 stsp got_object_commit_close(ce->data.commit);
174 1943de01 2018-06-22 stsp break;
175 f6be5c30 2018-06-22 stsp }
176 7bb0daa1 2018-06-21 stsp free(ce);
177 7bb0daa1 2018-06-21 stsp }
178 7bb0daa1 2018-06-21 stsp
179 7bb0daa1 2018-06-21 stsp ce = calloc(1, sizeof(*ce));
180 7bb0daa1 2018-06-21 stsp if (ce == NULL)
181 7bb0daa1 2018-06-21 stsp return got_error_from_errno();
182 7bb0daa1 2018-06-21 stsp memcpy(&ce->id, id, sizeof(ce->id));
183 f6be5c30 2018-06-22 stsp switch (cache->type) {
184 f6be5c30 2018-06-22 stsp case GOT_OBJECT_CACHE_TYPE_OBJ:
185 f6be5c30 2018-06-22 stsp ce->data.obj = (struct got_object *)item;
186 f6be5c30 2018-06-22 stsp break;
187 f6be5c30 2018-06-22 stsp case GOT_OBJECT_CACHE_TYPE_TREE:
188 f6be5c30 2018-06-22 stsp ce->data.tree = (struct got_tree_object *)item;
189 f6be5c30 2018-06-22 stsp break;
190 1943de01 2018-06-22 stsp case GOT_OBJECT_CACHE_TYPE_COMMIT:
191 1943de01 2018-06-22 stsp ce->data.commit = (struct got_commit_object *)item;
192 1943de01 2018-06-22 stsp break;
193 f6be5c30 2018-06-22 stsp }
194 eb77ee11 2018-07-08 stsp err = got_object_idcache_add(cache->idcache, id, ce);
195 50bc349d 2018-06-22 stsp if (err) {
196 50bc349d 2018-06-22 stsp if (err->code == GOT_ERR_OBJ_EXISTS) {
197 50bc349d 2018-06-22 stsp free(ce);
198 50bc349d 2018-06-22 stsp err = NULL;
199 50bc349d 2018-06-22 stsp }
200 a9bf0c2c 2018-06-22 stsp }
201 50bc349d 2018-06-22 stsp
202 50bc349d 2018-06-22 stsp return err;
203 7bb0daa1 2018-06-21 stsp }
204 7bb0daa1 2018-06-21 stsp
205 f6be5c30 2018-06-22 stsp const struct got_error *
206 f6be5c30 2018-06-22 stsp got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
207 f6be5c30 2018-06-22 stsp struct got_object *obj)
208 f6be5c30 2018-06-22 stsp {
209 f6be5c30 2018-06-22 stsp const struct got_error *err = NULL;
210 f6be5c30 2018-06-22 stsp
211 f6be5c30 2018-06-22 stsp err = cache_add(&repo->objcache, id, obj);
212 f6be5c30 2018-06-22 stsp if (err)
213 f6be5c30 2018-06-22 stsp return err;
214 f6be5c30 2018-06-22 stsp
215 f6be5c30 2018-06-22 stsp obj->refcnt++;
216 f6be5c30 2018-06-22 stsp return NULL;
217 f6be5c30 2018-06-22 stsp }
218 f6be5c30 2018-06-22 stsp
219 7bb0daa1 2018-06-21 stsp struct got_object *
220 7bb0daa1 2018-06-21 stsp got_repo_get_cached_object(struct got_repository *repo,
221 7bb0daa1 2018-06-21 stsp struct got_object_id *id)
222 7bb0daa1 2018-06-21 stsp {
223 54f20211 2018-06-22 stsp struct got_object_cache_entry *ce;
224 54f20211 2018-06-22 stsp
225 eb77ee11 2018-07-08 stsp ce = got_object_idcache_get(repo->objcache.idcache, id);
226 50bc349d 2018-06-22 stsp if (ce) {
227 54f20211 2018-06-22 stsp repo->objcache.cache_hit++;
228 f6be5c30 2018-06-22 stsp return ce->data.obj;
229 7bb0daa1 2018-06-21 stsp }
230 f6be5c30 2018-06-22 stsp
231 54f20211 2018-06-22 stsp repo->objcache.cache_miss++;
232 7bb0daa1 2018-06-21 stsp return NULL;
233 7bb0daa1 2018-06-21 stsp }
234 7bb0daa1 2018-06-21 stsp
235 4027f31a 2017-11-04 stsp const struct got_error *
236 f6be5c30 2018-06-22 stsp got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
237 f6be5c30 2018-06-22 stsp struct got_tree_object *tree)
238 f6be5c30 2018-06-22 stsp {
239 f6be5c30 2018-06-22 stsp const struct got_error *err = NULL;
240 f6be5c30 2018-06-22 stsp
241 f6be5c30 2018-06-22 stsp err = cache_add(&repo->treecache, id, tree);
242 f6be5c30 2018-06-22 stsp if (err)
243 f6be5c30 2018-06-22 stsp return err;
244 f6be5c30 2018-06-22 stsp
245 f6be5c30 2018-06-22 stsp tree->refcnt++;
246 f6be5c30 2018-06-22 stsp return NULL;
247 f6be5c30 2018-06-22 stsp }
248 f6be5c30 2018-06-22 stsp
249 f6be5c30 2018-06-22 stsp struct got_tree_object *
250 f6be5c30 2018-06-22 stsp got_repo_get_cached_tree(struct got_repository *repo,
251 f6be5c30 2018-06-22 stsp struct got_object_id *id)
252 f6be5c30 2018-06-22 stsp {
253 f6be5c30 2018-06-22 stsp struct got_object_cache_entry *ce;
254 f6be5c30 2018-06-22 stsp
255 eb77ee11 2018-07-08 stsp ce = got_object_idcache_get(repo->treecache.idcache, id);
256 f6be5c30 2018-06-22 stsp if (ce) {
257 f6be5c30 2018-06-22 stsp repo->treecache.cache_hit++;
258 f6be5c30 2018-06-22 stsp return ce->data.tree;
259 f6be5c30 2018-06-22 stsp }
260 f6be5c30 2018-06-22 stsp
261 f6be5c30 2018-06-22 stsp repo->treecache.cache_miss++;
262 1943de01 2018-06-22 stsp return NULL;
263 1943de01 2018-06-22 stsp }
264 1943de01 2018-06-22 stsp
265 1943de01 2018-06-22 stsp const struct got_error *
266 1943de01 2018-06-22 stsp got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
267 1943de01 2018-06-22 stsp struct got_commit_object *commit)
268 1943de01 2018-06-22 stsp {
269 1943de01 2018-06-22 stsp const struct got_error *err = NULL;
270 1943de01 2018-06-22 stsp
271 1943de01 2018-06-22 stsp err = cache_add(&repo->commitcache, id, commit);
272 1943de01 2018-06-22 stsp if (err)
273 1943de01 2018-06-22 stsp return err;
274 1943de01 2018-06-22 stsp
275 1943de01 2018-06-22 stsp commit->refcnt++;
276 f6be5c30 2018-06-22 stsp return NULL;
277 f6be5c30 2018-06-22 stsp }
278 f6be5c30 2018-06-22 stsp
279 1943de01 2018-06-22 stsp struct got_commit_object *
280 1943de01 2018-06-22 stsp got_repo_get_cached_commit(struct got_repository *repo,
281 1943de01 2018-06-22 stsp struct got_object_id *id)
282 1943de01 2018-06-22 stsp {
283 1943de01 2018-06-22 stsp struct got_object_cache_entry *ce;
284 f6be5c30 2018-06-22 stsp
285 eb77ee11 2018-07-08 stsp ce = got_object_idcache_get(repo->commitcache.idcache, id);
286 1943de01 2018-06-22 stsp if (ce) {
287 1943de01 2018-06-22 stsp repo->commitcache.cache_hit++;
288 1943de01 2018-06-22 stsp return ce->data.commit;
289 1943de01 2018-06-22 stsp }
290 1943de01 2018-06-22 stsp
291 1943de01 2018-06-22 stsp repo->commitcache.cache_miss++;
292 1943de01 2018-06-22 stsp return NULL;
293 1943de01 2018-06-22 stsp }
294 1943de01 2018-06-22 stsp
295 f6be5c30 2018-06-22 stsp const struct got_error *
296 92af5469 2017-11-05 stsp got_repo_open(struct got_repository **ret, const char *path)
297 4027f31a 2017-11-04 stsp {
298 92af5469 2017-11-05 stsp struct got_repository *repo = NULL;
299 92af5469 2017-11-05 stsp const struct got_error *err = NULL;
300 2393f13b 2018-03-09 stsp char *abspath;
301 4027f31a 2017-11-04 stsp
302 2393f13b 2018-03-09 stsp if (got_path_is_absolute(path))
303 2393f13b 2018-03-09 stsp abspath = strdup(path);
304 2393f13b 2018-03-09 stsp else
305 2393f13b 2018-03-09 stsp abspath = got_path_get_absolute(path);
306 92af5469 2017-11-05 stsp if (abspath == NULL)
307 92af5469 2017-11-05 stsp return got_error(GOT_ERR_BAD_PATH);
308 4027f31a 2017-11-04 stsp
309 4027f31a 2017-11-04 stsp repo = calloc(1, sizeof(*repo));
310 92af5469 2017-11-05 stsp if (repo == NULL) {
311 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
312 92af5469 2017-11-05 stsp goto done;
313 92af5469 2017-11-05 stsp }
314 4027f31a 2017-11-04 stsp
315 f6be5c30 2018-06-22 stsp repo->objcache.type = GOT_OBJECT_CACHE_TYPE_OBJ;
316 4307e577 2018-06-22 stsp repo->objcache.size = GOT_OBJECT_CACHE_SIZE_OBJ;
317 eb77ee11 2018-07-08 stsp repo->objcache.idcache = got_object_idcache_alloc(repo->objcache.size);
318 eb77ee11 2018-07-08 stsp if (repo->objcache.idcache == NULL) {
319 f6be5c30 2018-06-22 stsp err = got_error_from_errno();
320 f6be5c30 2018-06-22 stsp goto done;
321 f6be5c30 2018-06-22 stsp }
322 eb77ee11 2018-07-08 stsp
323 f6be5c30 2018-06-22 stsp repo->treecache.type = GOT_OBJECT_CACHE_TYPE_TREE;
324 4307e577 2018-06-22 stsp repo->treecache.size = GOT_OBJECT_CACHE_SIZE_TREE;
325 eb77ee11 2018-07-08 stsp repo->treecache.idcache =
326 eb77ee11 2018-07-08 stsp got_object_idcache_alloc(repo->treecache.size);
327 eb77ee11 2018-07-08 stsp if (repo->treecache.idcache == NULL) {
328 1943de01 2018-06-22 stsp err = got_error_from_errno();
329 1943de01 2018-06-22 stsp goto done;
330 1943de01 2018-06-22 stsp }
331 eb77ee11 2018-07-08 stsp
332 1943de01 2018-06-22 stsp repo->commitcache.type = GOT_OBJECT_CACHE_TYPE_COMMIT;
333 4307e577 2018-06-22 stsp repo->commitcache.size = GOT_OBJECT_CACHE_SIZE_COMMIT;
334 eb77ee11 2018-07-08 stsp repo->commitcache.idcache =
335 eb77ee11 2018-07-08 stsp got_object_idcache_alloc(repo->commitcache.size);
336 eb77ee11 2018-07-08 stsp if (repo->commitcache.idcache == NULL) {
337 eb77ee11 2018-07-08 stsp err = got_error_from_errno();
338 eb77ee11 2018-07-08 stsp goto done;
339 eb77ee11 2018-07-08 stsp }
340 1943de01 2018-06-22 stsp
341 4027f31a 2017-11-04 stsp repo->path = got_path_normalize(abspath);
342 92af5469 2017-11-05 stsp if (repo->path == NULL) {
343 92af5469 2017-11-05 stsp err = got_error(GOT_ERR_BAD_PATH);
344 92af5469 2017-11-05 stsp goto done;
345 92af5469 2017-11-05 stsp }
346 4027f31a 2017-11-04 stsp
347 4986b9d5 2018-03-12 stsp repo->path_git_dir = strdup(repo->path);
348 4986b9d5 2018-03-12 stsp if (repo->path_git_dir == NULL) {
349 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
350 92af5469 2017-11-05 stsp goto done;
351 92af5469 2017-11-05 stsp }
352 4986b9d5 2018-03-12 stsp if (!is_git_repo(repo)) {
353 4986b9d5 2018-03-12 stsp free(repo->path_git_dir);
354 4986b9d5 2018-03-12 stsp if (asprintf(&repo->path_git_dir, "%s/%s", repo->path,
355 4986b9d5 2018-03-12 stsp GOT_GIT_DIR) == -1) {
356 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
357 4986b9d5 2018-03-12 stsp goto done;
358 4986b9d5 2018-03-12 stsp }
359 4986b9d5 2018-03-12 stsp if (!is_git_repo(repo)) {
360 442a3ddc 2018-04-23 stsp struct got_worktree *worktree;
361 442a3ddc 2018-04-23 stsp if (got_worktree_open(&worktree, repo->path) == NULL) {
362 442a3ddc 2018-04-23 stsp free(repo->path_git_dir);
363 442a3ddc 2018-04-23 stsp repo->path_git_dir =
364 442a3ddc 2018-04-23 stsp strdup(worktree->repo_path);
365 442a3ddc 2018-04-23 stsp if (repo->path_git_dir == NULL) {
366 442a3ddc 2018-04-23 stsp err = got_error_from_errno();
367 442a3ddc 2018-04-23 stsp goto done;
368 442a3ddc 2018-04-23 stsp }
369 442a3ddc 2018-04-23 stsp if (!is_git_repo(repo)) {
370 442a3ddc 2018-04-23 stsp free(repo->path_git_dir);
371 442a3ddc 2018-04-23 stsp if (asprintf(&repo->path_git_dir,
372 442a3ddc 2018-04-23 stsp "%s/%s", worktree->repo_path,
373 442a3ddc 2018-04-23 stsp GOT_GIT_DIR) == -1) {
374 442a3ddc 2018-04-23 stsp err = got_error_from_errno();
375 442a3ddc 2018-04-23 stsp goto done;
376 442a3ddc 2018-04-23 stsp }
377 442a3ddc 2018-04-23 stsp }
378 442a3ddc 2018-04-23 stsp got_worktree_close(worktree);
379 442a3ddc 2018-04-23 stsp }
380 442a3ddc 2018-04-23 stsp }
381 442a3ddc 2018-04-23 stsp if (!is_git_repo(repo)) {
382 4986b9d5 2018-03-12 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
383 4986b9d5 2018-03-12 stsp goto done;
384 4986b9d5 2018-03-12 stsp }
385 4986b9d5 2018-03-12 stsp }
386 4027f31a 2017-11-04 stsp
387 4027f31a 2017-11-04 stsp *ret = repo;
388 92af5469 2017-11-05 stsp done:
389 92af5469 2017-11-05 stsp if (err)
390 4986b9d5 2018-03-12 stsp got_repo_close(repo);
391 92af5469 2017-11-05 stsp free(abspath);
392 92af5469 2017-11-05 stsp return err;
393 4027f31a 2017-11-04 stsp }
394 4027f31a 2017-11-04 stsp
395 cd717821 2018-06-22 stsp #if 0
396 1943de01 2018-06-22 stsp static void
397 1943de01 2018-06-22 stsp print_cache_stats(struct got_object_cache *cache, const char *name)
398 1943de01 2018-06-22 stsp {
399 1943de01 2018-06-22 stsp fprintf(stderr, "%s cache: %d elements, %d hits, %d missed\n",
400 eb77ee11 2018-07-08 stsp name, got_object_idcache_num_elements(cache->idcache),
401 eb77ee11 2018-07-08 stsp cache->cache_hit, cache->cache_miss);
402 cd717821 2018-06-22 stsp }
403 cd717821 2018-06-22 stsp
404 cd717821 2018-06-22 stsp void check_refcount(struct got_object_id *id, void *data, void *arg)
405 cd717821 2018-06-22 stsp {
406 cd717821 2018-06-22 stsp struct got_object_cache *cache = arg;
407 cd717821 2018-06-22 stsp struct got_object_cache_entry *ce = data;
408 cd717821 2018-06-22 stsp struct got_object *obj;
409 cd717821 2018-06-22 stsp struct got_tree_object *tree;
410 cd717821 2018-06-22 stsp struct got_commit_object *commit;
411 cd717821 2018-06-22 stsp char *id_str;
412 cd717821 2018-06-22 stsp
413 cd717821 2018-06-22 stsp if (got_object_id_str(&id_str, id) != NULL)
414 cd717821 2018-06-22 stsp return;
415 cd717821 2018-06-22 stsp
416 cd717821 2018-06-22 stsp switch (cache->type) {
417 cd717821 2018-06-22 stsp case GOT_OBJECT_CACHE_TYPE_OBJ:
418 cd717821 2018-06-22 stsp obj = ce->data.obj;
419 cd717821 2018-06-22 stsp if (obj->refcnt == 1)
420 cd717821 2018-06-22 stsp break;
421 cd717821 2018-06-22 stsp fprintf(stderr, "object %s has %d unclaimed references\n",
422 cd717821 2018-06-22 stsp id_str, obj->refcnt - 1);
423 cd717821 2018-06-22 stsp break;
424 cd717821 2018-06-22 stsp case GOT_OBJECT_CACHE_TYPE_TREE:
425 cd717821 2018-06-22 stsp tree = ce->data.tree;
426 cd717821 2018-06-22 stsp if (tree->refcnt == 1)
427 cd717821 2018-06-22 stsp break;
428 cd717821 2018-06-22 stsp fprintf(stderr, "tree %s has %d unclaimed references\n",
429 cd717821 2018-06-22 stsp id_str, tree->refcnt - 1);
430 cd717821 2018-06-22 stsp break;
431 cd717821 2018-06-22 stsp case GOT_OBJECT_CACHE_TYPE_COMMIT:
432 cd717821 2018-06-22 stsp commit = ce->data.commit;
433 cd717821 2018-06-22 stsp if (commit->refcnt == 1)
434 cd717821 2018-06-22 stsp break;
435 cd717821 2018-06-22 stsp fprintf(stderr, "commit %s has %d unclaimed references\n",
436 cd717821 2018-06-22 stsp id_str, commit->refcnt);
437 cd717821 2018-06-22 stsp break;
438 cd717821 2018-06-22 stsp }
439 cd717821 2018-06-22 stsp free(id_str);
440 1943de01 2018-06-22 stsp }
441 cd717821 2018-06-22 stsp #endif
442 1943de01 2018-06-22 stsp
443 4027f31a 2017-11-04 stsp void
444 4027f31a 2017-11-04 stsp got_repo_close(struct got_repository *repo)
445 4027f31a 2017-11-04 stsp {
446 79b11c62 2018-03-09 stsp int i;
447 79b11c62 2018-03-09 stsp
448 65cf1e80 2018-03-16 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
449 65cf1e80 2018-03-16 stsp if (repo->packidx_cache[i] == NULL)
450 79b11c62 2018-03-09 stsp break;
451 65cf1e80 2018-03-16 stsp got_packidx_close(repo->packidx_cache[i]);
452 79b11c62 2018-03-09 stsp }
453 bd1223b9 2018-03-14 stsp
454 7e656b93 2018-03-17 stsp for (i = 0; i < nitems(repo->packs); i++) {
455 7e656b93 2018-03-17 stsp if (repo->packs[i].path_packfile == NULL)
456 7e656b93 2018-03-17 stsp break;
457 7e656b93 2018-03-17 stsp got_pack_close(&repo->packs[i]);
458 7e656b93 2018-03-17 stsp }
459 7e656b93 2018-03-17 stsp
460 4027f31a 2017-11-04 stsp free(repo->path);
461 4986b9d5 2018-03-12 stsp free(repo->path_git_dir);
462 cd717821 2018-06-22 stsp
463 cd717821 2018-06-22 stsp #if 0
464 1943de01 2018-06-22 stsp print_cache_stats(&repo->objcache, "object");
465 1943de01 2018-06-22 stsp print_cache_stats(&repo->treecache, "tree");
466 1943de01 2018-06-22 stsp print_cache_stats(&repo->commitcache, "commit");
467 eb77ee11 2018-07-08 stsp got_object_idcache_for_each(repo->objcache.idcache, check_refcount,
468 cd717821 2018-06-22 stsp &repo->objcache);
469 eb77ee11 2018-07-08 stsp got_object_idcache_for_each(repo->treecache.idcache, check_refcount,
470 cd717821 2018-06-22 stsp &repo->treecache);
471 eb77ee11 2018-07-08 stsp got_object_idcache_for_each(repo->commitcache.idcache, check_refcount,
472 cd717821 2018-06-22 stsp &repo->commitcache);
473 cd717821 2018-06-22 stsp #endif
474 cd717821 2018-06-22 stsp
475 eb77ee11 2018-07-08 stsp if (repo->objcache.idcache)
476 eb77ee11 2018-07-08 stsp got_object_idcache_free(repo->objcache.idcache);
477 eb77ee11 2018-07-08 stsp if (repo->treecache.idcache)
478 eb77ee11 2018-07-08 stsp got_object_idcache_free(repo->treecache.idcache);
479 eb77ee11 2018-07-08 stsp if (repo->commitcache.idcache)
480 eb77ee11 2018-07-08 stsp got_object_idcache_free(repo->commitcache.idcache);
481 4027f31a 2017-11-04 stsp free(repo);
482 4027f31a 2017-11-04 stsp }