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 85f51bba 2018-07-16 stsp #include <errno.h>
27 85f51bba 2018-07-16 stsp #include <libgen.h>
28 4027f31a 2017-11-04 stsp
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"
34 4027f31a 2017-11-04 stsp
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"
43 c3f94f68 2017-11-05 stsp
44 79b11c62 2018-03-09 stsp #ifndef nitems
45 79b11c62 2018-03-09 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
46 79b11c62 2018-03-09 stsp #endif
47 3b339b2f 2018-02-12 stsp
48 4027f31a 2017-11-04 stsp #define GOT_GIT_DIR ".git"
49 4027f31a 2017-11-04 stsp
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"
54 4027f31a 2017-11-04 stsp
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"
59 4df642d9 2017-11-05 stsp
60 11995603 2017-11-05 stsp char *
61 86c3caaf 2018-03-09 stsp got_repo_get_path(struct got_repository *repo)
62 86c3caaf 2018-03-09 stsp {
63 86c3caaf 2018-03-09 stsp return strdup(repo->path);
64 86c3caaf 2018-03-09 stsp }
65 86c3caaf 2018-03-09 stsp
66 86c3caaf 2018-03-09 stsp char *
67 11995603 2017-11-05 stsp got_repo_get_path_git_dir(struct got_repository *repo)
68 4027f31a 2017-11-04 stsp {
69 4986b9d5 2018-03-12 stsp return strdup(repo->path_git_dir);
70 04ca23f4 2018-07-16 stsp }
71 04ca23f4 2018-07-16 stsp
72 04ca23f4 2018-07-16 stsp int
73 04ca23f4 2018-07-16 stsp got_repo_is_bare(struct got_repository *repo)
74 04ca23f4 2018-07-16 stsp {
75 04ca23f4 2018-07-16 stsp return (strcmp(repo->path, repo->path_git_dir) == 0);
76 4027f31a 2017-11-04 stsp }
77 4027f31a 2017-11-04 stsp
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)
80 4027f31a 2017-11-04 stsp {
81 4027f31a 2017-11-04 stsp char *path_child;
82 4027f31a 2017-11-04 stsp
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;
86 4027f31a 2017-11-04 stsp
87 4027f31a 2017-11-04 stsp return path_child;
88 4027f31a 2017-11-04 stsp }
89 4027f31a 2017-11-04 stsp
90 11995603 2017-11-05 stsp char *
91 11995603 2017-11-05 stsp got_repo_get_path_objects(struct got_repository *repo)
92 4027f31a 2017-11-04 stsp {
93 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_OBJECTS_DIR);
94 4027f31a 2017-11-04 stsp }
95 4027f31a 2017-11-04 stsp
96 11995603 2017-11-05 stsp char *
97 a1fd68d8 2018-01-12 stsp got_repo_get_path_objects_pack(struct got_repository *repo)
98 a1fd68d8 2018-01-12 stsp {
99 a1fd68d8 2018-01-12 stsp return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
100 a1fd68d8 2018-01-12 stsp }
101 a1fd68d8 2018-01-12 stsp
102 a1fd68d8 2018-01-12 stsp char *
103 11995603 2017-11-05 stsp got_repo_get_path_refs(struct got_repository *repo)
104 4027f31a 2017-11-04 stsp {
105 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_REFS_DIR);
106 4027f31a 2017-11-04 stsp }
107 4027f31a 2017-11-04 stsp
108 4027f31a 2017-11-04 stsp static char *
109 4027f31a 2017-11-04 stsp get_path_head(struct got_repository *repo)
110 4027f31a 2017-11-04 stsp {
111 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_HEAD_FILE);
112 4027f31a 2017-11-04 stsp }
113 4027f31a 2017-11-04 stsp
114 4027f31a 2017-11-04 stsp static int
115 4027f31a 2017-11-04 stsp is_git_repo(struct got_repository *repo)
116 4027f31a 2017-11-04 stsp {
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;
124 4027f31a 2017-11-04 stsp
125 deeca238 2018-03-12 stsp if (lstat(path_git, &sb) == -1)
126 deeca238 2018-03-12 stsp goto done;
127 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
128 deeca238 2018-03-12 stsp goto done;
129 4027f31a 2017-11-04 stsp
130 deeca238 2018-03-12 stsp if (lstat(path_objects, &sb) == -1)
131 deeca238 2018-03-12 stsp goto done;
132 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
133 deeca238 2018-03-12 stsp goto done;
134 deeca238 2018-03-12 stsp
135 deeca238 2018-03-12 stsp if (lstat(path_refs, &sb) == -1)
136 deeca238 2018-03-12 stsp goto done;
137 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
138 deeca238 2018-03-12 stsp goto done;
139 deeca238 2018-03-12 stsp
140 deeca238 2018-03-12 stsp if (lstat(path_head, &sb) == -1)
141 deeca238 2018-03-12 stsp goto done;
142 deeca238 2018-03-12 stsp if (!S_ISREG(sb.st_mode))
143 deeca238 2018-03-12 stsp goto done;
144 4847cca1 2018-03-12 stsp
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)
147 4847cca1 2018-03-12 stsp goto done;
148 4847cca1 2018-03-12 stsp got_ref_close(head_ref);
149 4847cca1 2018-03-12 stsp
150 deeca238 2018-03-12 stsp ret = 1;
151 deeca238 2018-03-12 stsp done:
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;
157 4027f31a 2017-11-04 stsp
158 4027f31a 2017-11-04 stsp }
159 4027f31a 2017-11-04 stsp
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)
163 7bb0daa1 2018-06-21 stsp {
164 50bc349d 2018-06-22 stsp const struct got_error *err = NULL;
165 54f20211 2018-06-22 stsp struct got_object_cache_entry *ce;
166 a9bf0c2c 2018-06-22 stsp int nelem;
167 7bb0daa1 2018-06-21 stsp
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);
172 50bc349d 2018-06-22 stsp if (err)
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);
177 f6be5c30 2018-06-22 stsp break;
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);
180 f6be5c30 2018-06-22 stsp break;
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);
183 1943de01 2018-06-22 stsp break;
184 f6be5c30 2018-06-22 stsp }
185 7bb0daa1 2018-06-21 stsp free(ce);
186 7bb0daa1 2018-06-21 stsp }
187 7bb0daa1 2018-06-21 stsp
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;
195 f6be5c30 2018-06-22 stsp break;
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;
198 f6be5c30 2018-06-22 stsp break;
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;
201 1943de01 2018-06-22 stsp break;
202 f6be5c30 2018-06-22 stsp }
203 ccfe88e6 2018-07-12 stsp
204 eb77ee11 2018-07-08 stsp err = got_object_idcache_add(cache->idcache, id, ce);
205 50bc349d 2018-06-22 stsp if (err) {
206 50bc349d 2018-06-22 stsp if (err->code == GOT_ERR_OBJ_EXISTS) {
207 50bc349d 2018-06-22 stsp free(ce);
208 50bc349d 2018-06-22 stsp err = NULL;
209 50bc349d 2018-06-22 stsp }
210 a9bf0c2c 2018-06-22 stsp }
211 50bc349d 2018-06-22 stsp return err;
212 7bb0daa1 2018-06-21 stsp }
213 ccfe88e6 2018-07-12 stsp #endif
214 7bb0daa1 2018-06-21 stsp
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)
218 f6be5c30 2018-06-22 stsp {
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);
222 f6be5c30 2018-06-22 stsp if (err)
223 f6be5c30 2018-06-22 stsp return err;
224 f6be5c30 2018-06-22 stsp obj->refcnt++;
225 ccfe88e6 2018-07-12 stsp #endif
226 f6be5c30 2018-06-22 stsp return NULL;
227 f6be5c30 2018-06-22 stsp }
228 f6be5c30 2018-06-22 stsp
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)
232 7bb0daa1 2018-06-21 stsp {
233 54f20211 2018-06-22 stsp struct got_object_cache_entry *ce;
234 54f20211 2018-06-22 stsp
235 eb77ee11 2018-07-08 stsp ce = got_object_idcache_get(repo->objcache.idcache, id);
236 50bc349d 2018-06-22 stsp if (ce) {
237 54f20211 2018-06-22 stsp repo->objcache.cache_hit++;
238 f6be5c30 2018-06-22 stsp return ce->data.obj;
239 7bb0daa1 2018-06-21 stsp }
240 f6be5c30 2018-06-22 stsp
241 54f20211 2018-06-22 stsp repo->objcache.cache_miss++;
242 7bb0daa1 2018-06-21 stsp return NULL;
243 7bb0daa1 2018-06-21 stsp }
244 7bb0daa1 2018-06-21 stsp
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)
248 f6be5c30 2018-06-22 stsp {
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);
252 f6be5c30 2018-06-22 stsp if (err)
253 f6be5c30 2018-06-22 stsp return err;
254 f6be5c30 2018-06-22 stsp tree->refcnt++;
255 ccfe88e6 2018-07-12 stsp #endif
256 f6be5c30 2018-06-22 stsp return NULL;
257 f6be5c30 2018-06-22 stsp }
258 f6be5c30 2018-06-22 stsp
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)
262 f6be5c30 2018-06-22 stsp {
263 f6be5c30 2018-06-22 stsp struct got_object_cache_entry *ce;
264 f6be5c30 2018-06-22 stsp
265 eb77ee11 2018-07-08 stsp ce = got_object_idcache_get(repo->treecache.idcache, id);
266 f6be5c30 2018-06-22 stsp if (ce) {
267 f6be5c30 2018-06-22 stsp repo->treecache.cache_hit++;
268 f6be5c30 2018-06-22 stsp return ce->data.tree;
269 f6be5c30 2018-06-22 stsp }
270 f6be5c30 2018-06-22 stsp
271 f6be5c30 2018-06-22 stsp repo->treecache.cache_miss++;
272 1943de01 2018-06-22 stsp return NULL;
273 1943de01 2018-06-22 stsp }
274 1943de01 2018-06-22 stsp
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)
278 1943de01 2018-06-22 stsp {
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);
282 1943de01 2018-06-22 stsp if (err)
283 1943de01 2018-06-22 stsp return err;
284 1943de01 2018-06-22 stsp
285 1943de01 2018-06-22 stsp commit->refcnt++;
286 ccfe88e6 2018-07-12 stsp #endif
287 f6be5c30 2018-06-22 stsp return NULL;
288 f6be5c30 2018-06-22 stsp }
289 f6be5c30 2018-06-22 stsp
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)
293 1943de01 2018-06-22 stsp {
294 1943de01 2018-06-22 stsp struct got_object_cache_entry *ce;
295 f6be5c30 2018-06-22 stsp
296 eb77ee11 2018-07-08 stsp ce = got_object_idcache_get(repo->commitcache.idcache, id);
297 1943de01 2018-06-22 stsp if (ce) {
298 1943de01 2018-06-22 stsp repo->commitcache.cache_hit++;
299 1943de01 2018-06-22 stsp return ce->data.commit;
300 1943de01 2018-06-22 stsp }
301 1943de01 2018-06-22 stsp
302 1943de01 2018-06-22 stsp repo->commitcache.cache_miss++;
303 1943de01 2018-06-22 stsp return NULL;
304 1943de01 2018-06-22 stsp }
305 1943de01 2018-06-22 stsp
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)
308 4027f31a 2017-11-04 stsp {
309 85f51bba 2018-07-16 stsp const struct got_error *err = NULL;
310 85f51bba 2018-07-16 stsp struct got_worktree *worktree = NULL;
311 85f51bba 2018-07-16 stsp
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();
316 85f51bba 2018-07-16 stsp goto done;
317 85f51bba 2018-07-16 stsp }
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();
322 85f51bba 2018-07-16 stsp goto done;
323 85f51bba 2018-07-16 stsp }
324 85f51bba 2018-07-16 stsp return NULL;
325 85f51bba 2018-07-16 stsp }
326 85f51bba 2018-07-16 stsp
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();
331 85f51bba 2018-07-16 stsp goto done;
332 85f51bba 2018-07-16 stsp }
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();
337 85f51bba 2018-07-16 stsp goto done;
338 85f51bba 2018-07-16 stsp }
339 85f51bba 2018-07-16 stsp return NULL;
340 85f51bba 2018-07-16 stsp }
341 85f51bba 2018-07-16 stsp
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);
346 85f51bba 2018-07-16 stsp if (err) {
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);
349 85f51bba 2018-07-16 stsp goto done;
350 85f51bba 2018-07-16 stsp }
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();
354 85f51bba 2018-07-16 stsp goto done;
355 85f51bba 2018-07-16 stsp }
356 85f51bba 2018-07-16 stsp
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;
364 85f51bba 2018-07-16 stsp goto done;
365 85f51bba 2018-07-16 stsp }
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);
368 85f51bba 2018-07-16 stsp goto done;
369 85f51bba 2018-07-16 stsp }
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();
373 85f51bba 2018-07-16 stsp goto done;
374 85f51bba 2018-07-16 stsp }
375 85f51bba 2018-07-16 stsp } else {
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();
379 85f51bba 2018-07-16 stsp goto done;
380 85f51bba 2018-07-16 stsp }
381 85f51bba 2018-07-16 stsp }
382 85f51bba 2018-07-16 stsp done:
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;
386 85f51bba 2018-07-16 stsp }
387 85f51bba 2018-07-16 stsp
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)
390 85f51bba 2018-07-16 stsp {
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;
395 4027f31a 2017-11-04 stsp
396 85f51bba 2018-07-16 stsp *repop = NULL;
397 85f51bba 2018-07-16 stsp
398 2393f13b 2018-03-09 stsp if (got_path_is_absolute(path))
399 2393f13b 2018-03-09 stsp abspath = strdup(path);
400 2393f13b 2018-03-09 stsp else
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);
404 4027f31a 2017-11-04 stsp
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();
408 92af5469 2017-11-05 stsp goto done;
409 92af5469 2017-11-05 stsp }
410 4027f31a 2017-11-04 stsp
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();
416 f6be5c30 2018-06-22 stsp goto done;
417 f6be5c30 2018-06-22 stsp }
418 eb77ee11 2018-07-08 stsp
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();
425 1943de01 2018-06-22 stsp goto done;
426 1943de01 2018-06-22 stsp }
427 eb77ee11 2018-07-08 stsp
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();
434 eb77ee11 2018-07-08 stsp goto done;
435 eb77ee11 2018-07-08 stsp }
436 1943de01 2018-06-22 stsp
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);
440 92af5469 2017-11-05 stsp goto done;
441 92af5469 2017-11-05 stsp }
442 4027f31a 2017-11-04 stsp
443 85f51bba 2018-07-16 stsp path = normpath;
444 85f51bba 2018-07-16 stsp do {
445 85f51bba 2018-07-16 stsp err = open_repo(repo, path);
446 85f51bba 2018-07-16 stsp if (err == NULL)
447 85f51bba 2018-07-16 stsp break;
448 85f51bba 2018-07-16 stsp if (err->code != GOT_ERR_NOT_GIT_REPO)
449 85f51bba 2018-07-16 stsp break;
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);
453 85f51bba 2018-07-16 stsp break;
454 442a3ddc 2018-04-23 stsp }
455 85f51bba 2018-07-16 stsp tried_root = 1;
456 442a3ddc 2018-04-23 stsp }
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);
461 92af5469 2017-11-05 stsp done:
462 92af5469 2017-11-05 stsp if (err)
463 4986b9d5 2018-03-12 stsp got_repo_close(repo);
464 85f51bba 2018-07-16 stsp else
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;
469 4027f31a 2017-11-04 stsp }
470 4027f31a 2017-11-04 stsp
471 cd717821 2018-06-22 stsp #if 0
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)
474 1943de01 2018-06-22 stsp {
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);
478 cd717821 2018-06-22 stsp }
479 cd717821 2018-06-22 stsp
480 cd717821 2018-06-22 stsp void check_refcount(struct got_object_id *id, void *data, void *arg)
481 cd717821 2018-06-22 stsp {
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;
488 cd717821 2018-06-22 stsp
489 cd717821 2018-06-22 stsp if (got_object_id_str(&id_str, id) != NULL)
490 cd717821 2018-06-22 stsp return;
491 cd717821 2018-06-22 stsp
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)
496 cd717821 2018-06-22 stsp break;
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);
499 cd717821 2018-06-22 stsp break;
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)
503 cd717821 2018-06-22 stsp break;
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);
506 cd717821 2018-06-22 stsp break;
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)
510 cd717821 2018-06-22 stsp break;
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);
513 cd717821 2018-06-22 stsp break;
514 cd717821 2018-06-22 stsp }
515 cd717821 2018-06-22 stsp free(id_str);
516 1943de01 2018-06-22 stsp }
517 cd717821 2018-06-22 stsp #endif
518 1943de01 2018-06-22 stsp
519 4027f31a 2017-11-04 stsp void
520 4027f31a 2017-11-04 stsp got_repo_close(struct got_repository *repo)
521 4027f31a 2017-11-04 stsp {
522 79b11c62 2018-03-09 stsp int i;
523 79b11c62 2018-03-09 stsp
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)
526 79b11c62 2018-03-09 stsp break;
527 65cf1e80 2018-03-16 stsp got_packidx_close(repo->packidx_cache[i]);
528 79b11c62 2018-03-09 stsp }
529 bd1223b9 2018-03-14 stsp
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)
532 7e656b93 2018-03-17 stsp break;
533 7e656b93 2018-03-17 stsp got_pack_close(&repo->packs[i]);
534 7e656b93 2018-03-17 stsp }
535 7e656b93 2018-03-17 stsp
536 4027f31a 2017-11-04 stsp free(repo->path);
537 4986b9d5 2018-03-12 stsp free(repo->path_git_dir);
538 cd717821 2018-06-22 stsp
539 cd717821 2018-06-22 stsp #if 0
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);
549 cd717821 2018-06-22 stsp #endif
550 cd717821 2018-06-22 stsp
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);
558 4027f31a 2017-11-04 stsp }
559 04ca23f4 2018-07-16 stsp
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)
563 04ca23f4 2018-07-16 stsp {
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;
569 04ca23f4 2018-07-16 stsp
570 04ca23f4 2018-07-16 stsp *in_repo_path = NULL;
571 04ca23f4 2018-07-16 stsp
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();
575 04ca23f4 2018-07-16 stsp
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();
579 04ca23f4 2018-07-16 stsp goto done;
580 04ca23f4 2018-07-16 stsp }
581 04ca23f4 2018-07-16 stsp err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
582 04ca23f4 2018-07-16 stsp if (err)
583 04ca23f4 2018-07-16 stsp goto done;
584 04ca23f4 2018-07-16 stsp
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();
588 04ca23f4 2018-07-16 stsp goto done;
589 04ca23f4 2018-07-16 stsp }
590 04ca23f4 2018-07-16 stsp
591 04ca23f4 2018-07-16 stsp /* TODO: Call "get in-repository path of work-tree node" API. */
592 04ca23f4 2018-07-16 stsp
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();
596 04ca23f4 2018-07-16 stsp goto done;
597 04ca23f4 2018-07-16 stsp }
598 04ca23f4 2018-07-16 stsp /*
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.
601 04ca23f4 2018-07-16 stsp */
602 04ca23f4 2018-07-16 stsp path = strdup(canonpath);
603 04ca23f4 2018-07-16 stsp } else {
604 04ca23f4 2018-07-16 stsp int is_repo_child = 0, is_cwd_child = 0;
605 04ca23f4 2018-07-16 stsp
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();
609 04ca23f4 2018-07-16 stsp goto done;
610 04ca23f4 2018-07-16 stsp }
611 04ca23f4 2018-07-16 stsp
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);
615 04ca23f4 2018-07-16 stsp
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;
620 04ca23f4 2018-07-16 stsp
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();
626 04ca23f4 2018-07-16 stsp goto done;
627 04ca23f4 2018-07-16 stsp }
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);
634 04ca23f4 2018-07-16 stsp if (err)
635 04ca23f4 2018-07-16 stsp goto done;
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)) {
641 04ca23f4 2018-07-16 stsp /*
642 04ca23f4 2018-07-16 stsp * Matched an on-disk path inside repository
643 04ca23f4 2018-07-16 stsp * database. Treat as repository-relative.
644 04ca23f4 2018-07-16 stsp */
645 04ca23f4 2018-07-16 stsp } else {
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);
650 04ca23f4 2018-07-16 stsp if (err)
651 04ca23f4 2018-07-16 stsp goto done;
652 04ca23f4 2018-07-16 stsp free(path);
653 04ca23f4 2018-07-16 stsp path = child;
654 04ca23f4 2018-07-16 stsp }
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,
660 04ca23f4 2018-07-16 stsp path);
661 04ca23f4 2018-07-16 stsp if (err)
662 04ca23f4 2018-07-16 stsp goto done;
663 04ca23f4 2018-07-16 stsp free(path);
664 04ca23f4 2018-07-16 stsp path = child;
665 04ca23f4 2018-07-16 stsp } else {
666 04ca23f4 2018-07-16 stsp /*
667 04ca23f4 2018-07-16 stsp * Matched unrelated on-disk path.
668 04ca23f4 2018-07-16 stsp * Treat it as repository-relative.
669 04ca23f4 2018-07-16 stsp */
670 04ca23f4 2018-07-16 stsp }
671 04ca23f4 2018-07-16 stsp }
672 04ca23f4 2018-07-16 stsp
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();
678 04ca23f4 2018-07-16 stsp goto done;
679 04ca23f4 2018-07-16 stsp }
680 04ca23f4 2018-07-16 stsp free(path);
681 04ca23f4 2018-07-16 stsp path = abspath;
682 04ca23f4 2018-07-16 stsp }
683 04ca23f4 2018-07-16 stsp
684 04ca23f4 2018-07-16 stsp done:
685 04ca23f4 2018-07-16 stsp free(repo_abspath);
686 04ca23f4 2018-07-16 stsp free(cwd);
687 04ca23f4 2018-07-16 stsp free(canonpath);
688 04ca23f4 2018-07-16 stsp if (err)
689 04ca23f4 2018-07-16 stsp free(path);
690 04ca23f4 2018-07-16 stsp else
691 04ca23f4 2018-07-16 stsp *in_repo_path = path;
692 04ca23f4 2018-07-16 stsp return err;
693 04ca23f4 2018-07-16 stsp }