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