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>
26 85f51bba 2018-07-16 stsp #include <errno.h>
27 85f51bba 2018-07-16 stsp #include <libgen.h>
29 4027f31a 2017-11-04 stsp #include "got_error.h"
30 5261c201 2018-04-01 stsp #include "got_reference.h"
31 4027f31a 2017-11-04 stsp #include "got_repository.h"
32 442a3ddc 2018-04-23 stsp #include "got_worktree.h"
33 7bb0daa1 2018-06-21 stsp #include "got_object.h"
35 718b3ab0 2018-03-17 stsp #include "got_lib_path.h"
36 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
37 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
38 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
39 718b3ab0 2018-03-17 stsp #include "got_lib_pack.h"
40 718b3ab0 2018-03-17 stsp #include "got_lib_repository.h"
41 442a3ddc 2018-04-23 stsp #include "got_lib_worktree.h"
42 eb77ee11 2018-07-08 stsp #include "got_lib_object_idcache.h"
44 79b11c62 2018-03-09 stsp #ifndef nitems
45 79b11c62 2018-03-09 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
48 4027f31a 2017-11-04 stsp #define GOT_GIT_DIR ".git"
50 4027f31a 2017-11-04 stsp /* Mandatory files and directories inside the git directory. */
51 4df642d9 2017-11-05 stsp #define GOT_OBJECTS_DIR "objects"
52 4df642d9 2017-11-05 stsp #define GOT_REFS_DIR "refs"
53 4df642d9 2017-11-05 stsp #define GOT_HEAD_FILE "HEAD"
55 a1fd68d8 2018-01-12 stsp /* Other files and directories inside the git directory. */
56 4df642d9 2017-11-05 stsp #define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
57 4df642d9 2017-11-05 stsp #define GOT_ORIG_HEAD_FILE "ORIG_HEAD"
58 a1fd68d8 2018-01-12 stsp #define GOT_OBJECTS_PACK_DIR "objects/pack"
61 86c3caaf 2018-03-09 stsp got_repo_get_path(struct got_repository *repo)
63 86c3caaf 2018-03-09 stsp return strdup(repo->path);
67 11995603 2017-11-05 stsp got_repo_get_path_git_dir(struct got_repository *repo)
69 4986b9d5 2018-03-12 stsp return strdup(repo->path_git_dir);
73 04ca23f4 2018-07-16 stsp got_repo_is_bare(struct got_repository *repo)
75 04ca23f4 2018-07-16 stsp return (strcmp(repo->path, repo->path_git_dir) == 0);
78 4027f31a 2017-11-04 stsp static char *
79 4027f31a 2017-11-04 stsp get_path_git_child(struct got_repository *repo, const char *basename)
81 4027f31a 2017-11-04 stsp char *path_child;
83 4986b9d5 2018-03-12 stsp if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
84 4027f31a 2017-11-04 stsp basename) == -1)
85 4027f31a 2017-11-04 stsp return NULL;
87 4027f31a 2017-11-04 stsp return path_child;
91 11995603 2017-11-05 stsp got_repo_get_path_objects(struct got_repository *repo)
93 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_OBJECTS_DIR);
97 a1fd68d8 2018-01-12 stsp got_repo_get_path_objects_pack(struct got_repository *repo)
99 a1fd68d8 2018-01-12 stsp return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
103 11995603 2017-11-05 stsp got_repo_get_path_refs(struct got_repository *repo)
105 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_REFS_DIR);
108 4027f31a 2017-11-04 stsp static char *
109 4027f31a 2017-11-04 stsp get_path_head(struct got_repository *repo)
111 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_HEAD_FILE);
115 4027f31a 2017-11-04 stsp is_git_repo(struct got_repository *repo)
117 11995603 2017-11-05 stsp char *path_git = got_repo_get_path_git_dir(repo);
118 11995603 2017-11-05 stsp char *path_objects = got_repo_get_path_objects(repo);
119 11995603 2017-11-05 stsp char *path_refs = got_repo_get_path_refs(repo);
120 4027f31a 2017-11-04 stsp char *path_head = get_path_head(repo);
121 deeca238 2018-03-12 stsp int ret = 0;
122 deeca238 2018-03-12 stsp struct stat sb;
123 4847cca1 2018-03-12 stsp struct got_reference *head_ref;
125 deeca238 2018-03-12 stsp if (lstat(path_git, &sb) == -1)
127 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
130 deeca238 2018-03-12 stsp if (lstat(path_objects, &sb) == -1)
132 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
135 deeca238 2018-03-12 stsp if (lstat(path_refs, &sb) == -1)
137 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
140 deeca238 2018-03-12 stsp if (lstat(path_head, &sb) == -1)
142 deeca238 2018-03-12 stsp if (!S_ISREG(sb.st_mode))
145 4847cca1 2018-03-12 stsp /* Check if the HEAD reference can be opened. */
146 4847cca1 2018-03-12 stsp if (got_ref_open(&head_ref, repo, GOT_REF_HEAD) != NULL)
148 4847cca1 2018-03-12 stsp got_ref_close(head_ref);
152 4027f31a 2017-11-04 stsp free(path_git);
153 4027f31a 2017-11-04 stsp free(path_objects);
154 4027f31a 2017-11-04 stsp free(path_refs);
155 4027f31a 2017-11-04 stsp free(path_head);
156 4027f31a 2017-11-04 stsp return ret;
160 ccfe88e6 2018-07-12 stsp #ifndef GOT_NO_OBJ_CACHE
161 f6be5c30 2018-06-22 stsp static const struct got_error *
162 f6be5c30 2018-06-22 stsp cache_add(struct got_object_cache *cache, struct got_object_id *id, void *item)
164 50bc349d 2018-06-22 stsp const struct got_error *err = NULL;
165 54f20211 2018-06-22 stsp struct got_object_cache_entry *ce;
168 eb77ee11 2018-07-08 stsp nelem = got_object_idcache_num_elements(cache->idcache);
169 4307e577 2018-06-22 stsp if (nelem >= cache->size) {
170 eb77ee11 2018-07-08 stsp err = got_object_idcache_remove_least_used((void **)&ce,
171 eb77ee11 2018-07-08 stsp cache->idcache);
173 50bc349d 2018-06-22 stsp return err;
174 f6be5c30 2018-06-22 stsp switch (cache->type) {
175 f6be5c30 2018-06-22 stsp case GOT_OBJECT_CACHE_TYPE_OBJ:
176 f6be5c30 2018-06-22 stsp got_object_close(ce->data.obj);
178 f6be5c30 2018-06-22 stsp case GOT_OBJECT_CACHE_TYPE_TREE:
179 f6be5c30 2018-06-22 stsp got_object_tree_close(ce->data.tree);
181 1943de01 2018-06-22 stsp case GOT_OBJECT_CACHE_TYPE_COMMIT:
182 1943de01 2018-06-22 stsp got_object_commit_close(ce->data.commit);
188 7bb0daa1 2018-06-21 stsp ce = calloc(1, sizeof(*ce));
189 7bb0daa1 2018-06-21 stsp if (ce == NULL)
190 7bb0daa1 2018-06-21 stsp return got_error_from_errno();
191 7bb0daa1 2018-06-21 stsp memcpy(&ce->id, id, sizeof(ce->id));
192 f6be5c30 2018-06-22 stsp switch (cache->type) {
193 f6be5c30 2018-06-22 stsp case GOT_OBJECT_CACHE_TYPE_OBJ:
194 f6be5c30 2018-06-22 stsp ce->data.obj = (struct got_object *)item;
196 f6be5c30 2018-06-22 stsp case GOT_OBJECT_CACHE_TYPE_TREE:
197 f6be5c30 2018-06-22 stsp ce->data.tree = (struct got_tree_object *)item;
199 1943de01 2018-06-22 stsp case GOT_OBJECT_CACHE_TYPE_COMMIT:
200 1943de01 2018-06-22 stsp ce->data.commit = (struct got_commit_object *)item;
204 eb77ee11 2018-07-08 stsp err = got_object_idcache_add(cache->idcache, id, ce);
206 50bc349d 2018-06-22 stsp if (err->code == GOT_ERR_OBJ_EXISTS) {
208 50bc349d 2018-06-22 stsp err = NULL;
211 50bc349d 2018-06-22 stsp return err;
215 f6be5c30 2018-06-22 stsp const struct got_error *
216 f6be5c30 2018-06-22 stsp got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
217 f6be5c30 2018-06-22 stsp struct got_object *obj)
219 ccfe88e6 2018-07-12 stsp #ifndef GOT_NO_OBJ_CACHE
220 f6be5c30 2018-06-22 stsp const struct got_error *err = NULL;
221 f6be5c30 2018-06-22 stsp err = cache_add(&repo->objcache, id, obj);
223 f6be5c30 2018-06-22 stsp return err;
224 f6be5c30 2018-06-22 stsp obj->refcnt++;
226 f6be5c30 2018-06-22 stsp return NULL;
229 7bb0daa1 2018-06-21 stsp struct got_object *
230 7bb0daa1 2018-06-21 stsp got_repo_get_cached_object(struct got_repository *repo,
231 7bb0daa1 2018-06-21 stsp struct got_object_id *id)
233 54f20211 2018-06-22 stsp struct got_object_cache_entry *ce;
235 eb77ee11 2018-07-08 stsp ce = got_object_idcache_get(repo->objcache.idcache, id);
237 54f20211 2018-06-22 stsp repo->objcache.cache_hit++;
238 f6be5c30 2018-06-22 stsp return ce->data.obj;
241 54f20211 2018-06-22 stsp repo->objcache.cache_miss++;
242 7bb0daa1 2018-06-21 stsp return NULL;
245 4027f31a 2017-11-04 stsp const struct got_error *
246 f6be5c30 2018-06-22 stsp got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
247 f6be5c30 2018-06-22 stsp struct got_tree_object *tree)
249 ccfe88e6 2018-07-12 stsp #ifndef GOT_NO_OBJ_CACHE
250 f6be5c30 2018-06-22 stsp const struct got_error *err = NULL;
251 f6be5c30 2018-06-22 stsp err = cache_add(&repo->treecache, id, tree);
253 f6be5c30 2018-06-22 stsp return err;
254 f6be5c30 2018-06-22 stsp tree->refcnt++;
256 f6be5c30 2018-06-22 stsp return NULL;
259 f6be5c30 2018-06-22 stsp struct got_tree_object *
260 f6be5c30 2018-06-22 stsp got_repo_get_cached_tree(struct got_repository *repo,
261 f6be5c30 2018-06-22 stsp struct got_object_id *id)
263 f6be5c30 2018-06-22 stsp struct got_object_cache_entry *ce;
265 eb77ee11 2018-07-08 stsp ce = got_object_idcache_get(repo->treecache.idcache, id);
267 f6be5c30 2018-06-22 stsp repo->treecache.cache_hit++;
268 f6be5c30 2018-06-22 stsp return ce->data.tree;
271 f6be5c30 2018-06-22 stsp repo->treecache.cache_miss++;
272 1943de01 2018-06-22 stsp return NULL;
275 1943de01 2018-06-22 stsp const struct got_error *
276 1943de01 2018-06-22 stsp got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
277 1943de01 2018-06-22 stsp struct got_commit_object *commit)
279 ccfe88e6 2018-07-12 stsp #ifndef GOT_NO_OBJ_CACHE
280 1943de01 2018-06-22 stsp const struct got_error *err = NULL;
281 1943de01 2018-06-22 stsp err = cache_add(&repo->commitcache, id, commit);
283 1943de01 2018-06-22 stsp return err;
285 1943de01 2018-06-22 stsp commit->refcnt++;
287 f6be5c30 2018-06-22 stsp return NULL;
290 1943de01 2018-06-22 stsp struct got_commit_object *
291 1943de01 2018-06-22 stsp got_repo_get_cached_commit(struct got_repository *repo,
292 1943de01 2018-06-22 stsp struct got_object_id *id)
294 1943de01 2018-06-22 stsp struct got_object_cache_entry *ce;
296 eb77ee11 2018-07-08 stsp ce = got_object_idcache_get(repo->commitcache.idcache, id);
298 1943de01 2018-06-22 stsp repo->commitcache.cache_hit++;
299 1943de01 2018-06-22 stsp return ce->data.commit;
302 1943de01 2018-06-22 stsp repo->commitcache.cache_miss++;
303 1943de01 2018-06-22 stsp return NULL;
306 f6be5c30 2018-06-22 stsp const struct got_error *
307 85f51bba 2018-07-16 stsp open_repo(struct got_repository *repo, const char *path)
309 85f51bba 2018-07-16 stsp const struct got_error *err = NULL;
310 85f51bba 2018-07-16 stsp struct got_worktree *worktree = NULL;
312 85f51bba 2018-07-16 stsp /* bare git repository? */
313 85f51bba 2018-07-16 stsp repo->path_git_dir = strdup(path);
314 85f51bba 2018-07-16 stsp if (repo->path_git_dir == NULL) {
315 85f51bba 2018-07-16 stsp err = got_error_from_errno();
318 85f51bba 2018-07-16 stsp if (is_git_repo(repo)) {
319 85f51bba 2018-07-16 stsp repo->path = strdup(repo->path_git_dir);
320 85f51bba 2018-07-16 stsp if (repo->path == NULL) {
321 85f51bba 2018-07-16 stsp err = got_error_from_errno();
324 85f51bba 2018-07-16 stsp return NULL;
327 85f51bba 2018-07-16 stsp /* git repository with working tree? */
328 85f51bba 2018-07-16 stsp free(repo->path_git_dir);
329 85f51bba 2018-07-16 stsp if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
330 85f51bba 2018-07-16 stsp err = got_error_from_errno();
333 85f51bba 2018-07-16 stsp if (is_git_repo(repo)) {
334 85f51bba 2018-07-16 stsp repo->path = strdup(path);
335 85f51bba 2018-07-16 stsp if (repo->path == NULL) {
336 85f51bba 2018-07-16 stsp err = got_error_from_errno();
339 85f51bba 2018-07-16 stsp return NULL;
342 85f51bba 2018-07-16 stsp /* got work tree checked out from bare git repository? */
343 85f51bba 2018-07-16 stsp free(repo->path_git_dir);
344 85f51bba 2018-07-16 stsp repo->path_git_dir = NULL;
345 85f51bba 2018-07-16 stsp err = got_worktree_open(&worktree, path);
347 85f51bba 2018-07-16 stsp if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
348 85f51bba 2018-07-16 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
351 85f51bba 2018-07-16 stsp repo->path_git_dir = strdup(worktree->repo_path);
352 85f51bba 2018-07-16 stsp if (repo->path_git_dir == NULL) {
353 85f51bba 2018-07-16 stsp err = got_error_from_errno();
357 85f51bba 2018-07-16 stsp /* got work tree checked out from git repository with working tree? */
358 85f51bba 2018-07-16 stsp if (!is_git_repo(repo)) {
359 85f51bba 2018-07-16 stsp free(repo->path_git_dir);
360 85f51bba 2018-07-16 stsp if (asprintf(&repo->path_git_dir, "%s/%s", worktree->repo_path,
361 85f51bba 2018-07-16 stsp GOT_GIT_DIR) == -1) {
362 85f51bba 2018-07-16 stsp err = got_error_from_errno();
363 85f51bba 2018-07-16 stsp repo->path_git_dir = NULL;
366 85f51bba 2018-07-16 stsp if (!is_git_repo(repo)) {
367 85f51bba 2018-07-16 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
370 85f51bba 2018-07-16 stsp repo->path = strdup(worktree->repo_path);
371 85f51bba 2018-07-16 stsp if (repo->path == NULL) {
372 85f51bba 2018-07-16 stsp err = got_error_from_errno();
376 85f51bba 2018-07-16 stsp repo->path = strdup(repo->path_git_dir);
377 85f51bba 2018-07-16 stsp if (repo->path == NULL) {
378 85f51bba 2018-07-16 stsp err = got_error_from_errno();
383 85f51bba 2018-07-16 stsp if (worktree)
384 85f51bba 2018-07-16 stsp got_worktree_close(worktree);
385 85f51bba 2018-07-16 stsp return err;
388 85f51bba 2018-07-16 stsp const struct got_error *
389 85f51bba 2018-07-16 stsp got_repo_open(struct got_repository **repop, const char *path)
391 92af5469 2017-11-05 stsp struct got_repository *repo = NULL;
392 92af5469 2017-11-05 stsp const struct got_error *err = NULL;
393 85f51bba 2018-07-16 stsp char *abspath, *normpath = NULL;
394 85f51bba 2018-07-16 stsp int tried_root = 0;
396 85f51bba 2018-07-16 stsp *repop = NULL;
398 2393f13b 2018-03-09 stsp if (got_path_is_absolute(path))
399 2393f13b 2018-03-09 stsp abspath = strdup(path);
401 2393f13b 2018-03-09 stsp abspath = got_path_get_absolute(path);
402 92af5469 2017-11-05 stsp if (abspath == NULL)
403 92af5469 2017-11-05 stsp return got_error(GOT_ERR_BAD_PATH);
405 4027f31a 2017-11-04 stsp repo = calloc(1, sizeof(*repo));
406 92af5469 2017-11-05 stsp if (repo == NULL) {
407 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
411 f6be5c30 2018-06-22 stsp repo->objcache.type = GOT_OBJECT_CACHE_TYPE_OBJ;
412 4307e577 2018-06-22 stsp repo->objcache.size = GOT_OBJECT_CACHE_SIZE_OBJ;
413 eb77ee11 2018-07-08 stsp repo->objcache.idcache = got_object_idcache_alloc(repo->objcache.size);
414 eb77ee11 2018-07-08 stsp if (repo->objcache.idcache == NULL) {
415 f6be5c30 2018-06-22 stsp err = got_error_from_errno();
419 f6be5c30 2018-06-22 stsp repo->treecache.type = GOT_OBJECT_CACHE_TYPE_TREE;
420 4307e577 2018-06-22 stsp repo->treecache.size = GOT_OBJECT_CACHE_SIZE_TREE;
421 eb77ee11 2018-07-08 stsp repo->treecache.idcache =
422 eb77ee11 2018-07-08 stsp got_object_idcache_alloc(repo->treecache.size);
423 eb77ee11 2018-07-08 stsp if (repo->treecache.idcache == NULL) {
424 1943de01 2018-06-22 stsp err = got_error_from_errno();
428 1943de01 2018-06-22 stsp repo->commitcache.type = GOT_OBJECT_CACHE_TYPE_COMMIT;
429 4307e577 2018-06-22 stsp repo->commitcache.size = GOT_OBJECT_CACHE_SIZE_COMMIT;
430 eb77ee11 2018-07-08 stsp repo->commitcache.idcache =
431 eb77ee11 2018-07-08 stsp got_object_idcache_alloc(repo->commitcache.size);
432 eb77ee11 2018-07-08 stsp if (repo->commitcache.idcache == NULL) {
433 eb77ee11 2018-07-08 stsp err = got_error_from_errno();
437 85f51bba 2018-07-16 stsp normpath = got_path_normalize(abspath);
438 85f51bba 2018-07-16 stsp if (normpath == NULL) {
439 92af5469 2017-11-05 stsp err = got_error(GOT_ERR_BAD_PATH);
443 85f51bba 2018-07-16 stsp path = normpath;
445 85f51bba 2018-07-16 stsp err = open_repo(repo, path);
446 85f51bba 2018-07-16 stsp if (err == NULL)
448 85f51bba 2018-07-16 stsp if (err->code != GOT_ERR_NOT_GIT_REPO)
450 85f51bba 2018-07-16 stsp if (path[0] == '/' && path[1] == '\0') {
451 85f51bba 2018-07-16 stsp if (tried_root) {
452 85f51bba 2018-07-16 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
455 85f51bba 2018-07-16 stsp tried_root = 1;
457 85f51bba 2018-07-16 stsp path = dirname(path);
458 85f51bba 2018-07-16 stsp if (path == NULL)
459 85f51bba 2018-07-16 stsp err = got_error_from_errno();
460 85f51bba 2018-07-16 stsp } while (path);
463 4986b9d5 2018-03-12 stsp got_repo_close(repo);
465 85f51bba 2018-07-16 stsp *repop = repo;
466 92af5469 2017-11-05 stsp free(abspath);
467 85f51bba 2018-07-16 stsp free(normpath);
468 92af5469 2017-11-05 stsp return err;
472 1943de01 2018-06-22 stsp static void
473 1943de01 2018-06-22 stsp print_cache_stats(struct got_object_cache *cache, const char *name)
475 1943de01 2018-06-22 stsp fprintf(stderr, "%s cache: %d elements, %d hits, %d missed\n",
476 eb77ee11 2018-07-08 stsp name, got_object_idcache_num_elements(cache->idcache),
477 eb77ee11 2018-07-08 stsp cache->cache_hit, cache->cache_miss);
480 cd717821 2018-06-22 stsp void check_refcount(struct got_object_id *id, void *data, void *arg)
482 cd717821 2018-06-22 stsp struct got_object_cache *cache = arg;
483 cd717821 2018-06-22 stsp struct got_object_cache_entry *ce = data;
484 cd717821 2018-06-22 stsp struct got_object *obj;
485 cd717821 2018-06-22 stsp struct got_tree_object *tree;
486 cd717821 2018-06-22 stsp struct got_commit_object *commit;
487 cd717821 2018-06-22 stsp char *id_str;
489 cd717821 2018-06-22 stsp if (got_object_id_str(&id_str, id) != NULL)
492 cd717821 2018-06-22 stsp switch (cache->type) {
493 cd717821 2018-06-22 stsp case GOT_OBJECT_CACHE_TYPE_OBJ:
494 cd717821 2018-06-22 stsp obj = ce->data.obj;
495 cd717821 2018-06-22 stsp if (obj->refcnt == 1)
497 cd717821 2018-06-22 stsp fprintf(stderr, "object %s has %d unclaimed references\n",
498 cd717821 2018-06-22 stsp id_str, obj->refcnt - 1);
500 cd717821 2018-06-22 stsp case GOT_OBJECT_CACHE_TYPE_TREE:
501 cd717821 2018-06-22 stsp tree = ce->data.tree;
502 cd717821 2018-06-22 stsp if (tree->refcnt == 1)
504 cd717821 2018-06-22 stsp fprintf(stderr, "tree %s has %d unclaimed references\n",
505 cd717821 2018-06-22 stsp id_str, tree->refcnt - 1);
507 cd717821 2018-06-22 stsp case GOT_OBJECT_CACHE_TYPE_COMMIT:
508 cd717821 2018-06-22 stsp commit = ce->data.commit;
509 cd717821 2018-06-22 stsp if (commit->refcnt == 1)
511 cd717821 2018-06-22 stsp fprintf(stderr, "commit %s has %d unclaimed references\n",
512 cd717821 2018-06-22 stsp id_str, commit->refcnt);
515 cd717821 2018-06-22 stsp free(id_str);
520 4027f31a 2017-11-04 stsp got_repo_close(struct got_repository *repo)
524 65cf1e80 2018-03-16 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
525 65cf1e80 2018-03-16 stsp if (repo->packidx_cache[i] == NULL)
527 65cf1e80 2018-03-16 stsp got_packidx_close(repo->packidx_cache[i]);
530 7e656b93 2018-03-17 stsp for (i = 0; i < nitems(repo->packs); i++) {
531 7e656b93 2018-03-17 stsp if (repo->packs[i].path_packfile == NULL)
533 7e656b93 2018-03-17 stsp got_pack_close(&repo->packs[i]);
536 4027f31a 2017-11-04 stsp free(repo->path);
537 4986b9d5 2018-03-12 stsp free(repo->path_git_dir);
540 1943de01 2018-06-22 stsp print_cache_stats(&repo->objcache, "object");
541 1943de01 2018-06-22 stsp print_cache_stats(&repo->treecache, "tree");
542 1943de01 2018-06-22 stsp print_cache_stats(&repo->commitcache, "commit");
543 eb77ee11 2018-07-08 stsp got_object_idcache_for_each(repo->objcache.idcache, check_refcount,
544 cd717821 2018-06-22 stsp &repo->objcache);
545 eb77ee11 2018-07-08 stsp got_object_idcache_for_each(repo->treecache.idcache, check_refcount,
546 cd717821 2018-06-22 stsp &repo->treecache);
547 eb77ee11 2018-07-08 stsp got_object_idcache_for_each(repo->commitcache.idcache, check_refcount,
548 cd717821 2018-06-22 stsp &repo->commitcache);
551 eb77ee11 2018-07-08 stsp if (repo->objcache.idcache)
552 eb77ee11 2018-07-08 stsp got_object_idcache_free(repo->objcache.idcache);
553 eb77ee11 2018-07-08 stsp if (repo->treecache.idcache)
554 eb77ee11 2018-07-08 stsp got_object_idcache_free(repo->treecache.idcache);
555 eb77ee11 2018-07-08 stsp if (repo->commitcache.idcache)
556 eb77ee11 2018-07-08 stsp got_object_idcache_free(repo->commitcache.idcache);
557 4027f31a 2017-11-04 stsp free(repo);
560 04ca23f4 2018-07-16 stsp const struct got_error *
561 04ca23f4 2018-07-16 stsp got_repo_map_path(char **in_repo_path, struct got_repository *repo,
562 04ca23f4 2018-07-16 stsp const char *input_path)
564 04ca23f4 2018-07-16 stsp const struct got_error *err = NULL;
565 04ca23f4 2018-07-16 stsp char *repo_abspath = NULL, *cwd = NULL;
566 04ca23f4 2018-07-16 stsp struct stat sb;
567 04ca23f4 2018-07-16 stsp size_t repolen, cwdlen, len;
568 04ca23f4 2018-07-16 stsp char *canonpath, *path;
570 04ca23f4 2018-07-16 stsp *in_repo_path = NULL;
572 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
573 04ca23f4 2018-07-16 stsp if (cwd == NULL)
574 04ca23f4 2018-07-16 stsp return got_error_from_errno();
576 04ca23f4 2018-07-16 stsp canonpath = strdup(input_path);
577 04ca23f4 2018-07-16 stsp if (canonpath == NULL) {
578 04ca23f4 2018-07-16 stsp err = got_error_from_errno();
581 04ca23f4 2018-07-16 stsp err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
585 04ca23f4 2018-07-16 stsp repo_abspath = got_repo_get_path(repo);
586 04ca23f4 2018-07-16 stsp if (repo_abspath == NULL) {
587 04ca23f4 2018-07-16 stsp err = got_error_from_errno();
591 04ca23f4 2018-07-16 stsp /* TODO: Call "get in-repository path of work-tree node" API. */
593 04ca23f4 2018-07-16 stsp if (lstat(canonpath, &sb) != 0) {
594 04ca23f4 2018-07-16 stsp if (errno != ENOENT) {
595 04ca23f4 2018-07-16 stsp err = got_error_from_errno();
599 04ca23f4 2018-07-16 stsp * Path is not on disk.
600 04ca23f4 2018-07-16 stsp * Assume it is already relative to repository root.
602 04ca23f4 2018-07-16 stsp path = strdup(canonpath);
604 04ca23f4 2018-07-16 stsp int is_repo_child = 0, is_cwd_child = 0;
606 04ca23f4 2018-07-16 stsp path = realpath(canonpath, NULL);
607 04ca23f4 2018-07-16 stsp if (path == NULL) {
608 04ca23f4 2018-07-16 stsp err = got_error_from_errno();
612 04ca23f4 2018-07-16 stsp repolen = strlen(repo_abspath);
613 04ca23f4 2018-07-16 stsp cwdlen = strlen(cwd);
614 04ca23f4 2018-07-16 stsp len = strlen(path);
616 04ca23f4 2018-07-16 stsp if (len > repolen && strncmp(path, repo_abspath, repolen) == 0)
617 04ca23f4 2018-07-16 stsp is_repo_child = 1;
618 04ca23f4 2018-07-16 stsp if (len > cwdlen && strncmp(path, cwd, cwdlen) == 0)
619 04ca23f4 2018-07-16 stsp is_cwd_child = 1;
621 04ca23f4 2018-07-16 stsp if (strcmp(path, repo_abspath) == 0) {
622 04ca23f4 2018-07-16 stsp free(path);
623 04ca23f4 2018-07-16 stsp path = strdup("");
624 04ca23f4 2018-07-16 stsp if (path == NULL) {
625 04ca23f4 2018-07-16 stsp err = got_error_from_errno();
628 04ca23f4 2018-07-16 stsp } else if (is_repo_child && is_cwd_child) {
629 04ca23f4 2018-07-16 stsp char *child;
630 04ca23f4 2018-07-16 stsp /* TODO: Is path inside a got worktree? */
631 04ca23f4 2018-07-16 stsp /* Strip common prefix with repository path. */
632 04ca23f4 2018-07-16 stsp err = got_path_skip_common_ancestor(&child,
633 04ca23f4 2018-07-16 stsp repo_abspath, path);
636 04ca23f4 2018-07-16 stsp free(path);
637 04ca23f4 2018-07-16 stsp path = child;
638 04ca23f4 2018-07-16 stsp } else if (is_repo_child) {
639 04ca23f4 2018-07-16 stsp /* Matched an on-disk path inside repository. */
640 04ca23f4 2018-07-16 stsp if (got_repo_is_bare(repo)) {
642 04ca23f4 2018-07-16 stsp * Matched an on-disk path inside repository
643 04ca23f4 2018-07-16 stsp * database. Treat as repository-relative.
646 04ca23f4 2018-07-16 stsp char *child;
647 04ca23f4 2018-07-16 stsp /* Strip common prefix with repository path. */
648 04ca23f4 2018-07-16 stsp err = got_path_skip_common_ancestor(&child,
649 04ca23f4 2018-07-16 stsp repo_abspath, path);
652 04ca23f4 2018-07-16 stsp free(path);
653 04ca23f4 2018-07-16 stsp path = child;
655 04ca23f4 2018-07-16 stsp } else if (is_cwd_child) {
656 04ca23f4 2018-07-16 stsp char *child;
657 04ca23f4 2018-07-16 stsp /* TODO: Is path inside a got worktree? */
658 04ca23f4 2018-07-16 stsp /* Strip common prefix with cwd. */
659 04ca23f4 2018-07-16 stsp err = got_path_skip_common_ancestor(&child, cwd,
663 04ca23f4 2018-07-16 stsp free(path);
664 04ca23f4 2018-07-16 stsp path = child;
667 04ca23f4 2018-07-16 stsp * Matched unrelated on-disk path.
668 04ca23f4 2018-07-16 stsp * Treat it as repository-relative.
673 04ca23f4 2018-07-16 stsp /* Make in-repository path absolute */
674 04ca23f4 2018-07-16 stsp if (path[0] != '/') {
675 04ca23f4 2018-07-16 stsp char *abspath;
676 04ca23f4 2018-07-16 stsp if (asprintf(&abspath, "/%s", path) == -1) {
677 04ca23f4 2018-07-16 stsp err = got_error_from_errno();
680 04ca23f4 2018-07-16 stsp free(path);
681 04ca23f4 2018-07-16 stsp path = abspath;
685 04ca23f4 2018-07-16 stsp free(repo_abspath);
687 04ca23f4 2018-07-16 stsp free(canonpath);
689 04ca23f4 2018-07-16 stsp free(path);
691 04ca23f4 2018-07-16 stsp *in_repo_path = path;
692 04ca23f4 2018-07-16 stsp return err;