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 ad242220 2018-09-08 stsp #include <sys/types.h>
18 79b11c62 2018-03-09 stsp #include <sys/queue.h>
19 ad242220 2018-09-08 stsp #include <sys/uio.h>
20 deeca238 2018-03-12 stsp #include <sys/stat.h>
21 ad242220 2018-09-08 stsp #include <sys/wait.h>
22 79b11c62 2018-03-09 stsp
23 4027f31a 2017-11-04 stsp #include <limits.h>
24 4027f31a 2017-11-04 stsp #include <stdlib.h>
25 4027f31a 2017-11-04 stsp #include <stdio.h>
26 4027f31a 2017-11-04 stsp #include <sha1.h>
27 4027f31a 2017-11-04 stsp #include <string.h>
28 79b11c62 2018-03-09 stsp #include <zlib.h>
29 85f51bba 2018-07-16 stsp #include <errno.h>
30 85f51bba 2018-07-16 stsp #include <libgen.h>
31 ad242220 2018-09-08 stsp #include <stdint.h>
32 ad242220 2018-09-08 stsp #include <imsg.h>
33 4027f31a 2017-11-04 stsp
34 4027f31a 2017-11-04 stsp #include "got_error.h"
35 5261c201 2018-04-01 stsp #include "got_reference.h"
36 4027f31a 2017-11-04 stsp #include "got_repository.h"
37 442a3ddc 2018-04-23 stsp #include "got_worktree.h"
38 7bb0daa1 2018-06-21 stsp #include "got_object.h"
39 4027f31a 2017-11-04 stsp
40 718b3ab0 2018-03-17 stsp #include "got_lib_path.h"
41 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
42 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
43 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
44 718b3ab0 2018-03-17 stsp #include "got_lib_pack.h"
45 718b3ab0 2018-03-17 stsp #include "got_lib_repository.h"
46 442a3ddc 2018-04-23 stsp #include "got_lib_worktree.h"
47 eb77ee11 2018-07-08 stsp #include "got_lib_object_idcache.h"
48 ad242220 2018-09-08 stsp #include "got_lib_privsep.h"
49 c3f94f68 2017-11-05 stsp
50 79b11c62 2018-03-09 stsp #ifndef nitems
51 79b11c62 2018-03-09 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
52 79b11c62 2018-03-09 stsp #endif
53 3b339b2f 2018-02-12 stsp
54 4027f31a 2017-11-04 stsp #define GOT_GIT_DIR ".git"
55 4027f31a 2017-11-04 stsp
56 4027f31a 2017-11-04 stsp /* Mandatory files and directories inside the git directory. */
57 4df642d9 2017-11-05 stsp #define GOT_OBJECTS_DIR "objects"
58 4df642d9 2017-11-05 stsp #define GOT_REFS_DIR "refs"
59 4df642d9 2017-11-05 stsp #define GOT_HEAD_FILE "HEAD"
60 4027f31a 2017-11-04 stsp
61 a1fd68d8 2018-01-12 stsp /* Other files and directories inside the git directory. */
62 4df642d9 2017-11-05 stsp #define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
63 4df642d9 2017-11-05 stsp #define GOT_ORIG_HEAD_FILE "ORIG_HEAD"
64 a1fd68d8 2018-01-12 stsp #define GOT_OBJECTS_PACK_DIR "objects/pack"
65 4df642d9 2017-11-05 stsp
66 11995603 2017-11-05 stsp char *
67 86c3caaf 2018-03-09 stsp got_repo_get_path(struct got_repository *repo)
68 86c3caaf 2018-03-09 stsp {
69 86c3caaf 2018-03-09 stsp return strdup(repo->path);
70 86c3caaf 2018-03-09 stsp }
71 86c3caaf 2018-03-09 stsp
72 86c3caaf 2018-03-09 stsp char *
73 11995603 2017-11-05 stsp got_repo_get_path_git_dir(struct got_repository *repo)
74 4027f31a 2017-11-04 stsp {
75 4986b9d5 2018-03-12 stsp return strdup(repo->path_git_dir);
76 04ca23f4 2018-07-16 stsp }
77 04ca23f4 2018-07-16 stsp
78 04ca23f4 2018-07-16 stsp int
79 04ca23f4 2018-07-16 stsp got_repo_is_bare(struct got_repository *repo)
80 04ca23f4 2018-07-16 stsp {
81 04ca23f4 2018-07-16 stsp return (strcmp(repo->path, repo->path_git_dir) == 0);
82 4027f31a 2017-11-04 stsp }
83 4027f31a 2017-11-04 stsp
84 4027f31a 2017-11-04 stsp static char *
85 4027f31a 2017-11-04 stsp get_path_git_child(struct got_repository *repo, const char *basename)
86 4027f31a 2017-11-04 stsp {
87 4027f31a 2017-11-04 stsp char *path_child;
88 4027f31a 2017-11-04 stsp
89 4986b9d5 2018-03-12 stsp if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
90 4027f31a 2017-11-04 stsp basename) == -1)
91 4027f31a 2017-11-04 stsp return NULL;
92 4027f31a 2017-11-04 stsp
93 4027f31a 2017-11-04 stsp return path_child;
94 4027f31a 2017-11-04 stsp }
95 4027f31a 2017-11-04 stsp
96 11995603 2017-11-05 stsp char *
97 11995603 2017-11-05 stsp got_repo_get_path_objects(struct got_repository *repo)
98 4027f31a 2017-11-04 stsp {
99 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_OBJECTS_DIR);
100 4027f31a 2017-11-04 stsp }
101 4027f31a 2017-11-04 stsp
102 11995603 2017-11-05 stsp char *
103 a1fd68d8 2018-01-12 stsp got_repo_get_path_objects_pack(struct got_repository *repo)
104 a1fd68d8 2018-01-12 stsp {
105 a1fd68d8 2018-01-12 stsp return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
106 a1fd68d8 2018-01-12 stsp }
107 a1fd68d8 2018-01-12 stsp
108 a1fd68d8 2018-01-12 stsp char *
109 11995603 2017-11-05 stsp got_repo_get_path_refs(struct got_repository *repo)
110 4027f31a 2017-11-04 stsp {
111 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_REFS_DIR);
112 4027f31a 2017-11-04 stsp }
113 4027f31a 2017-11-04 stsp
114 4027f31a 2017-11-04 stsp static char *
115 4027f31a 2017-11-04 stsp get_path_head(struct got_repository *repo)
116 4027f31a 2017-11-04 stsp {
117 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_HEAD_FILE);
118 4027f31a 2017-11-04 stsp }
119 4027f31a 2017-11-04 stsp
120 4027f31a 2017-11-04 stsp static int
121 4027f31a 2017-11-04 stsp is_git_repo(struct got_repository *repo)
122 4027f31a 2017-11-04 stsp {
123 11995603 2017-11-05 stsp char *path_git = got_repo_get_path_git_dir(repo);
124 11995603 2017-11-05 stsp char *path_objects = got_repo_get_path_objects(repo);
125 11995603 2017-11-05 stsp char *path_refs = got_repo_get_path_refs(repo);
126 4027f31a 2017-11-04 stsp char *path_head = get_path_head(repo);
127 deeca238 2018-03-12 stsp int ret = 0;
128 deeca238 2018-03-12 stsp struct stat sb;
129 4847cca1 2018-03-12 stsp struct got_reference *head_ref;
130 4027f31a 2017-11-04 stsp
131 deeca238 2018-03-12 stsp if (lstat(path_git, &sb) == -1)
132 deeca238 2018-03-12 stsp goto done;
133 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
134 deeca238 2018-03-12 stsp goto done;
135 4027f31a 2017-11-04 stsp
136 deeca238 2018-03-12 stsp if (lstat(path_objects, &sb) == -1)
137 deeca238 2018-03-12 stsp goto done;
138 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
139 deeca238 2018-03-12 stsp goto done;
140 deeca238 2018-03-12 stsp
141 deeca238 2018-03-12 stsp if (lstat(path_refs, &sb) == -1)
142 deeca238 2018-03-12 stsp goto done;
143 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
144 deeca238 2018-03-12 stsp goto done;
145 deeca238 2018-03-12 stsp
146 deeca238 2018-03-12 stsp if (lstat(path_head, &sb) == -1)
147 deeca238 2018-03-12 stsp goto done;
148 deeca238 2018-03-12 stsp if (!S_ISREG(sb.st_mode))
149 deeca238 2018-03-12 stsp goto done;
150 4847cca1 2018-03-12 stsp
151 4847cca1 2018-03-12 stsp /* Check if the HEAD reference can be opened. */
152 4847cca1 2018-03-12 stsp if (got_ref_open(&head_ref, repo, GOT_REF_HEAD) != NULL)
153 4847cca1 2018-03-12 stsp goto done;
154 4847cca1 2018-03-12 stsp got_ref_close(head_ref);
155 4847cca1 2018-03-12 stsp
156 deeca238 2018-03-12 stsp ret = 1;
157 deeca238 2018-03-12 stsp done:
158 4027f31a 2017-11-04 stsp free(path_git);
159 4027f31a 2017-11-04 stsp free(path_objects);
160 4027f31a 2017-11-04 stsp free(path_refs);
161 4027f31a 2017-11-04 stsp free(path_head);
162 4027f31a 2017-11-04 stsp return ret;
163 4027f31a 2017-11-04 stsp
164 4027f31a 2017-11-04 stsp }
165 4027f31a 2017-11-04 stsp
166 ccfe88e6 2018-07-12 stsp #ifndef GOT_NO_OBJ_CACHE
167 f6be5c30 2018-06-22 stsp static const struct got_error *
168 f6be5c30 2018-06-22 stsp cache_add(struct got_object_cache *cache, struct got_object_id *id, void *item)
169 7bb0daa1 2018-06-21 stsp {
170 50bc349d 2018-06-22 stsp const struct got_error *err = NULL;
171 54f20211 2018-06-22 stsp struct got_object_cache_entry *ce;
172 a9bf0c2c 2018-06-22 stsp int nelem;
173 7bb0daa1 2018-06-21 stsp
174 eb77ee11 2018-07-08 stsp nelem = got_object_idcache_num_elements(cache->idcache);
175 4307e577 2018-06-22 stsp if (nelem >= cache->size) {
176 eb77ee11 2018-07-08 stsp err = got_object_idcache_remove_least_used((void **)&ce,
177 eb77ee11 2018-07-08 stsp cache->idcache);
178 50bc349d 2018-06-22 stsp if (err)
179 50bc349d 2018-06-22 stsp return err;
180 f6be5c30 2018-06-22 stsp switch (cache->type) {
181 f6be5c30 2018-06-22 stsp case GOT_OBJECT_CACHE_TYPE_OBJ:
182 f6be5c30 2018-06-22 stsp got_object_close(ce->data.obj);
183 f6be5c30 2018-06-22 stsp break;
184 f6be5c30 2018-06-22 stsp case GOT_OBJECT_CACHE_TYPE_TREE:
185 f6be5c30 2018-06-22 stsp got_object_tree_close(ce->data.tree);
186 f6be5c30 2018-06-22 stsp break;
187 1943de01 2018-06-22 stsp case GOT_OBJECT_CACHE_TYPE_COMMIT:
188 1943de01 2018-06-22 stsp got_object_commit_close(ce->data.commit);
189 1943de01 2018-06-22 stsp break;
190 f6be5c30 2018-06-22 stsp }
191 7bb0daa1 2018-06-21 stsp free(ce);
192 7bb0daa1 2018-06-21 stsp }
193 7bb0daa1 2018-06-21 stsp
194 7bb0daa1 2018-06-21 stsp ce = calloc(1, sizeof(*ce));
195 7bb0daa1 2018-06-21 stsp if (ce == NULL)
196 7bb0daa1 2018-06-21 stsp return got_error_from_errno();
197 7bb0daa1 2018-06-21 stsp memcpy(&ce->id, id, sizeof(ce->id));
198 f6be5c30 2018-06-22 stsp switch (cache->type) {
199 f6be5c30 2018-06-22 stsp case GOT_OBJECT_CACHE_TYPE_OBJ:
200 f6be5c30 2018-06-22 stsp ce->data.obj = (struct got_object *)item;
201 f6be5c30 2018-06-22 stsp break;
202 f6be5c30 2018-06-22 stsp case GOT_OBJECT_CACHE_TYPE_TREE:
203 f6be5c30 2018-06-22 stsp ce->data.tree = (struct got_tree_object *)item;
204 f6be5c30 2018-06-22 stsp break;
205 1943de01 2018-06-22 stsp case GOT_OBJECT_CACHE_TYPE_COMMIT:
206 1943de01 2018-06-22 stsp ce->data.commit = (struct got_commit_object *)item;
207 1943de01 2018-06-22 stsp break;
208 f6be5c30 2018-06-22 stsp }
209 ccfe88e6 2018-07-12 stsp
210 eb77ee11 2018-07-08 stsp err = got_object_idcache_add(cache->idcache, id, ce);
211 50bc349d 2018-06-22 stsp if (err) {
212 50bc349d 2018-06-22 stsp if (err->code == GOT_ERR_OBJ_EXISTS) {
213 50bc349d 2018-06-22 stsp free(ce);
214 50bc349d 2018-06-22 stsp err = NULL;
215 50bc349d 2018-06-22 stsp }
216 a9bf0c2c 2018-06-22 stsp }
217 50bc349d 2018-06-22 stsp return err;
218 7bb0daa1 2018-06-21 stsp }
219 ccfe88e6 2018-07-12 stsp #endif
220 7bb0daa1 2018-06-21 stsp
221 f6be5c30 2018-06-22 stsp const struct got_error *
222 f6be5c30 2018-06-22 stsp got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
223 f6be5c30 2018-06-22 stsp struct got_object *obj)
224 f6be5c30 2018-06-22 stsp {
225 ccfe88e6 2018-07-12 stsp #ifndef GOT_NO_OBJ_CACHE
226 f6be5c30 2018-06-22 stsp const struct got_error *err = NULL;
227 f6be5c30 2018-06-22 stsp err = cache_add(&repo->objcache, id, obj);
228 f6be5c30 2018-06-22 stsp if (err)
229 f6be5c30 2018-06-22 stsp return err;
230 f6be5c30 2018-06-22 stsp obj->refcnt++;
231 ccfe88e6 2018-07-12 stsp #endif
232 f6be5c30 2018-06-22 stsp return NULL;
233 f6be5c30 2018-06-22 stsp }
234 f6be5c30 2018-06-22 stsp
235 7bb0daa1 2018-06-21 stsp struct got_object *
236 7bb0daa1 2018-06-21 stsp got_repo_get_cached_object(struct got_repository *repo,
237 7bb0daa1 2018-06-21 stsp struct got_object_id *id)
238 7bb0daa1 2018-06-21 stsp {
239 54f20211 2018-06-22 stsp struct got_object_cache_entry *ce;
240 54f20211 2018-06-22 stsp
241 eb77ee11 2018-07-08 stsp ce = got_object_idcache_get(repo->objcache.idcache, id);
242 50bc349d 2018-06-22 stsp if (ce) {
243 54f20211 2018-06-22 stsp repo->objcache.cache_hit++;
244 f6be5c30 2018-06-22 stsp return ce->data.obj;
245 7bb0daa1 2018-06-21 stsp }
246 f6be5c30 2018-06-22 stsp
247 54f20211 2018-06-22 stsp repo->objcache.cache_miss++;
248 7bb0daa1 2018-06-21 stsp return NULL;
249 7bb0daa1 2018-06-21 stsp }
250 7bb0daa1 2018-06-21 stsp
251 4027f31a 2017-11-04 stsp const struct got_error *
252 f6be5c30 2018-06-22 stsp got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
253 f6be5c30 2018-06-22 stsp struct got_tree_object *tree)
254 f6be5c30 2018-06-22 stsp {
255 ccfe88e6 2018-07-12 stsp #ifndef GOT_NO_OBJ_CACHE
256 f6be5c30 2018-06-22 stsp const struct got_error *err = NULL;
257 f6be5c30 2018-06-22 stsp err = cache_add(&repo->treecache, id, tree);
258 f6be5c30 2018-06-22 stsp if (err)
259 f6be5c30 2018-06-22 stsp return err;
260 f6be5c30 2018-06-22 stsp tree->refcnt++;
261 ccfe88e6 2018-07-12 stsp #endif
262 f6be5c30 2018-06-22 stsp return NULL;
263 f6be5c30 2018-06-22 stsp }
264 f6be5c30 2018-06-22 stsp
265 f6be5c30 2018-06-22 stsp struct got_tree_object *
266 f6be5c30 2018-06-22 stsp got_repo_get_cached_tree(struct got_repository *repo,
267 f6be5c30 2018-06-22 stsp struct got_object_id *id)
268 f6be5c30 2018-06-22 stsp {
269 f6be5c30 2018-06-22 stsp struct got_object_cache_entry *ce;
270 f6be5c30 2018-06-22 stsp
271 eb77ee11 2018-07-08 stsp ce = got_object_idcache_get(repo->treecache.idcache, id);
272 f6be5c30 2018-06-22 stsp if (ce) {
273 f6be5c30 2018-06-22 stsp repo->treecache.cache_hit++;
274 f6be5c30 2018-06-22 stsp return ce->data.tree;
275 f6be5c30 2018-06-22 stsp }
276 f6be5c30 2018-06-22 stsp
277 f6be5c30 2018-06-22 stsp repo->treecache.cache_miss++;
278 1943de01 2018-06-22 stsp return NULL;
279 1943de01 2018-06-22 stsp }
280 1943de01 2018-06-22 stsp
281 1943de01 2018-06-22 stsp const struct got_error *
282 1943de01 2018-06-22 stsp got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
283 1943de01 2018-06-22 stsp struct got_commit_object *commit)
284 1943de01 2018-06-22 stsp {
285 ccfe88e6 2018-07-12 stsp #ifndef GOT_NO_OBJ_CACHE
286 1943de01 2018-06-22 stsp const struct got_error *err = NULL;
287 1943de01 2018-06-22 stsp err = cache_add(&repo->commitcache, id, commit);
288 1943de01 2018-06-22 stsp if (err)
289 1943de01 2018-06-22 stsp return err;
290 1943de01 2018-06-22 stsp
291 1943de01 2018-06-22 stsp commit->refcnt++;
292 ccfe88e6 2018-07-12 stsp #endif
293 f6be5c30 2018-06-22 stsp return NULL;
294 f6be5c30 2018-06-22 stsp }
295 f6be5c30 2018-06-22 stsp
296 1943de01 2018-06-22 stsp struct got_commit_object *
297 1943de01 2018-06-22 stsp got_repo_get_cached_commit(struct got_repository *repo,
298 1943de01 2018-06-22 stsp struct got_object_id *id)
299 1943de01 2018-06-22 stsp {
300 1943de01 2018-06-22 stsp struct got_object_cache_entry *ce;
301 f6be5c30 2018-06-22 stsp
302 eb77ee11 2018-07-08 stsp ce = got_object_idcache_get(repo->commitcache.idcache, id);
303 1943de01 2018-06-22 stsp if (ce) {
304 1943de01 2018-06-22 stsp repo->commitcache.cache_hit++;
305 1943de01 2018-06-22 stsp return ce->data.commit;
306 1943de01 2018-06-22 stsp }
307 1943de01 2018-06-22 stsp
308 1943de01 2018-06-22 stsp repo->commitcache.cache_miss++;
309 1943de01 2018-06-22 stsp return NULL;
310 1943de01 2018-06-22 stsp }
311 1943de01 2018-06-22 stsp
312 f6be5c30 2018-06-22 stsp const struct got_error *
313 85f51bba 2018-07-16 stsp open_repo(struct got_repository *repo, const char *path)
314 4027f31a 2017-11-04 stsp {
315 85f51bba 2018-07-16 stsp const struct got_error *err = NULL;
316 85f51bba 2018-07-16 stsp struct got_worktree *worktree = NULL;
317 85f51bba 2018-07-16 stsp
318 85f51bba 2018-07-16 stsp /* bare git repository? */
319 85f51bba 2018-07-16 stsp repo->path_git_dir = strdup(path);
320 85f51bba 2018-07-16 stsp if (repo->path_git_dir == 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 if (is_git_repo(repo)) {
325 85f51bba 2018-07-16 stsp repo->path = strdup(repo->path_git_dir);
326 85f51bba 2018-07-16 stsp if (repo->path == NULL) {
327 85f51bba 2018-07-16 stsp err = got_error_from_errno();
328 85f51bba 2018-07-16 stsp goto done;
329 85f51bba 2018-07-16 stsp }
330 85f51bba 2018-07-16 stsp return NULL;
331 85f51bba 2018-07-16 stsp }
332 85f51bba 2018-07-16 stsp
333 85f51bba 2018-07-16 stsp /* git repository with working tree? */
334 85f51bba 2018-07-16 stsp free(repo->path_git_dir);
335 85f51bba 2018-07-16 stsp if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
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 if (is_git_repo(repo)) {
340 85f51bba 2018-07-16 stsp repo->path = strdup(path);
341 85f51bba 2018-07-16 stsp if (repo->path == NULL) {
342 85f51bba 2018-07-16 stsp err = got_error_from_errno();
343 85f51bba 2018-07-16 stsp goto done;
344 85f51bba 2018-07-16 stsp }
345 85f51bba 2018-07-16 stsp return NULL;
346 85f51bba 2018-07-16 stsp }
347 85f51bba 2018-07-16 stsp
348 85f51bba 2018-07-16 stsp /* got work tree checked out from bare git repository? */
349 85f51bba 2018-07-16 stsp free(repo->path_git_dir);
350 85f51bba 2018-07-16 stsp repo->path_git_dir = NULL;
351 85f51bba 2018-07-16 stsp err = got_worktree_open(&worktree, path);
352 85f51bba 2018-07-16 stsp if (err) {
353 85f51bba 2018-07-16 stsp if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
354 85f51bba 2018-07-16 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
355 85f51bba 2018-07-16 stsp goto done;
356 85f51bba 2018-07-16 stsp }
357 85f51bba 2018-07-16 stsp repo->path_git_dir = strdup(worktree->repo_path);
358 85f51bba 2018-07-16 stsp if (repo->path_git_dir == NULL) {
359 85f51bba 2018-07-16 stsp err = got_error_from_errno();
360 85f51bba 2018-07-16 stsp goto done;
361 85f51bba 2018-07-16 stsp }
362 85f51bba 2018-07-16 stsp
363 85f51bba 2018-07-16 stsp /* got work tree checked out from git repository with working tree? */
364 85f51bba 2018-07-16 stsp if (!is_git_repo(repo)) {
365 85f51bba 2018-07-16 stsp free(repo->path_git_dir);
366 85f51bba 2018-07-16 stsp if (asprintf(&repo->path_git_dir, "%s/%s", worktree->repo_path,
367 85f51bba 2018-07-16 stsp GOT_GIT_DIR) == -1) {
368 85f51bba 2018-07-16 stsp err = got_error_from_errno();
369 85f51bba 2018-07-16 stsp repo->path_git_dir = NULL;
370 85f51bba 2018-07-16 stsp goto done;
371 85f51bba 2018-07-16 stsp }
372 85f51bba 2018-07-16 stsp if (!is_git_repo(repo)) {
373 85f51bba 2018-07-16 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
374 85f51bba 2018-07-16 stsp goto done;
375 85f51bba 2018-07-16 stsp }
376 85f51bba 2018-07-16 stsp repo->path = strdup(worktree->repo_path);
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 } else {
382 85f51bba 2018-07-16 stsp repo->path = strdup(repo->path_git_dir);
383 85f51bba 2018-07-16 stsp if (repo->path == NULL) {
384 85f51bba 2018-07-16 stsp err = got_error_from_errno();
385 85f51bba 2018-07-16 stsp goto done;
386 85f51bba 2018-07-16 stsp }
387 85f51bba 2018-07-16 stsp }
388 85f51bba 2018-07-16 stsp done:
389 85f51bba 2018-07-16 stsp if (worktree)
390 85f51bba 2018-07-16 stsp got_worktree_close(worktree);
391 85f51bba 2018-07-16 stsp return err;
392 85f51bba 2018-07-16 stsp }
393 85f51bba 2018-07-16 stsp
394 85f51bba 2018-07-16 stsp const struct got_error *
395 85f51bba 2018-07-16 stsp got_repo_open(struct got_repository **repop, const char *path)
396 85f51bba 2018-07-16 stsp {
397 92af5469 2017-11-05 stsp struct got_repository *repo = NULL;
398 92af5469 2017-11-05 stsp const struct got_error *err = NULL;
399 85f51bba 2018-07-16 stsp char *abspath, *normpath = NULL;
400 ad242220 2018-09-08 stsp int i, tried_root = 0;
401 4027f31a 2017-11-04 stsp
402 85f51bba 2018-07-16 stsp *repop = NULL;
403 85f51bba 2018-07-16 stsp
404 2393f13b 2018-03-09 stsp if (got_path_is_absolute(path))
405 2393f13b 2018-03-09 stsp abspath = strdup(path);
406 2393f13b 2018-03-09 stsp else
407 2393f13b 2018-03-09 stsp abspath = got_path_get_absolute(path);
408 92af5469 2017-11-05 stsp if (abspath == NULL)
409 92af5469 2017-11-05 stsp return got_error(GOT_ERR_BAD_PATH);
410 4027f31a 2017-11-04 stsp
411 4027f31a 2017-11-04 stsp repo = calloc(1, sizeof(*repo));
412 92af5469 2017-11-05 stsp if (repo == NULL) {
413 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
414 92af5469 2017-11-05 stsp goto done;
415 92af5469 2017-11-05 stsp }
416 4027f31a 2017-11-04 stsp
417 ad242220 2018-09-08 stsp for (i = 0; i < nitems(repo->privsep_children); i++) {
418 3516b818 2018-09-08 stsp memset(&repo->privsep_children[i], 0,
419 3516b818 2018-09-08 stsp sizeof(repo->privsep_children[0]));
420 ad242220 2018-09-08 stsp repo->privsep_children[i].imsg_fd = -1;
421 ad242220 2018-09-08 stsp }
422 ad242220 2018-09-08 stsp
423 f6be5c30 2018-06-22 stsp repo->objcache.type = GOT_OBJECT_CACHE_TYPE_OBJ;
424 4307e577 2018-06-22 stsp repo->objcache.size = GOT_OBJECT_CACHE_SIZE_OBJ;
425 eb77ee11 2018-07-08 stsp repo->objcache.idcache = got_object_idcache_alloc(repo->objcache.size);
426 eb77ee11 2018-07-08 stsp if (repo->objcache.idcache == NULL) {
427 f6be5c30 2018-06-22 stsp err = got_error_from_errno();
428 f6be5c30 2018-06-22 stsp goto done;
429 f6be5c30 2018-06-22 stsp }
430 eb77ee11 2018-07-08 stsp
431 f6be5c30 2018-06-22 stsp repo->treecache.type = GOT_OBJECT_CACHE_TYPE_TREE;
432 4307e577 2018-06-22 stsp repo->treecache.size = GOT_OBJECT_CACHE_SIZE_TREE;
433 eb77ee11 2018-07-08 stsp repo->treecache.idcache =
434 eb77ee11 2018-07-08 stsp got_object_idcache_alloc(repo->treecache.size);
435 eb77ee11 2018-07-08 stsp if (repo->treecache.idcache == NULL) {
436 1943de01 2018-06-22 stsp err = got_error_from_errno();
437 1943de01 2018-06-22 stsp goto done;
438 1943de01 2018-06-22 stsp }
439 eb77ee11 2018-07-08 stsp
440 1943de01 2018-06-22 stsp repo->commitcache.type = GOT_OBJECT_CACHE_TYPE_COMMIT;
441 4307e577 2018-06-22 stsp repo->commitcache.size = GOT_OBJECT_CACHE_SIZE_COMMIT;
442 eb77ee11 2018-07-08 stsp repo->commitcache.idcache =
443 eb77ee11 2018-07-08 stsp got_object_idcache_alloc(repo->commitcache.size);
444 eb77ee11 2018-07-08 stsp if (repo->commitcache.idcache == NULL) {
445 eb77ee11 2018-07-08 stsp err = got_error_from_errno();
446 eb77ee11 2018-07-08 stsp goto done;
447 eb77ee11 2018-07-08 stsp }
448 1943de01 2018-06-22 stsp
449 85f51bba 2018-07-16 stsp normpath = got_path_normalize(abspath);
450 85f51bba 2018-07-16 stsp if (normpath == NULL) {
451 92af5469 2017-11-05 stsp err = got_error(GOT_ERR_BAD_PATH);
452 92af5469 2017-11-05 stsp goto done;
453 92af5469 2017-11-05 stsp }
454 4027f31a 2017-11-04 stsp
455 85f51bba 2018-07-16 stsp path = normpath;
456 85f51bba 2018-07-16 stsp do {
457 85f51bba 2018-07-16 stsp err = open_repo(repo, path);
458 85f51bba 2018-07-16 stsp if (err == NULL)
459 85f51bba 2018-07-16 stsp break;
460 85f51bba 2018-07-16 stsp if (err->code != GOT_ERR_NOT_GIT_REPO)
461 85f51bba 2018-07-16 stsp break;
462 85f51bba 2018-07-16 stsp if (path[0] == '/' && path[1] == '\0') {
463 85f51bba 2018-07-16 stsp if (tried_root) {
464 85f51bba 2018-07-16 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
465 85f51bba 2018-07-16 stsp break;
466 442a3ddc 2018-04-23 stsp }
467 85f51bba 2018-07-16 stsp tried_root = 1;
468 442a3ddc 2018-04-23 stsp }
469 85f51bba 2018-07-16 stsp path = dirname(path);
470 85f51bba 2018-07-16 stsp if (path == NULL)
471 85f51bba 2018-07-16 stsp err = got_error_from_errno();
472 85f51bba 2018-07-16 stsp } while (path);
473 92af5469 2017-11-05 stsp done:
474 92af5469 2017-11-05 stsp if (err)
475 ad242220 2018-09-08 stsp err = got_repo_close(repo);
476 85f51bba 2018-07-16 stsp else
477 85f51bba 2018-07-16 stsp *repop = repo;
478 92af5469 2017-11-05 stsp free(abspath);
479 85f51bba 2018-07-16 stsp free(normpath);
480 92af5469 2017-11-05 stsp return err;
481 4027f31a 2017-11-04 stsp }
482 4027f31a 2017-11-04 stsp
483 cd717821 2018-06-22 stsp #if 0
484 1943de01 2018-06-22 stsp static void
485 1943de01 2018-06-22 stsp print_cache_stats(struct got_object_cache *cache, const char *name)
486 1943de01 2018-06-22 stsp {
487 1943de01 2018-06-22 stsp fprintf(stderr, "%s cache: %d elements, %d hits, %d missed\n",
488 eb77ee11 2018-07-08 stsp name, got_object_idcache_num_elements(cache->idcache),
489 eb77ee11 2018-07-08 stsp cache->cache_hit, cache->cache_miss);
490 cd717821 2018-06-22 stsp }
491 cd717821 2018-06-22 stsp
492 cd717821 2018-06-22 stsp void check_refcount(struct got_object_id *id, void *data, void *arg)
493 cd717821 2018-06-22 stsp {
494 cd717821 2018-06-22 stsp struct got_object_cache *cache = arg;
495 cd717821 2018-06-22 stsp struct got_object_cache_entry *ce = data;
496 cd717821 2018-06-22 stsp struct got_object *obj;
497 cd717821 2018-06-22 stsp struct got_tree_object *tree;
498 cd717821 2018-06-22 stsp struct got_commit_object *commit;
499 cd717821 2018-06-22 stsp char *id_str;
500 cd717821 2018-06-22 stsp
501 cd717821 2018-06-22 stsp if (got_object_id_str(&id_str, id) != NULL)
502 cd717821 2018-06-22 stsp return;
503 cd717821 2018-06-22 stsp
504 cd717821 2018-06-22 stsp switch (cache->type) {
505 cd717821 2018-06-22 stsp case GOT_OBJECT_CACHE_TYPE_OBJ:
506 cd717821 2018-06-22 stsp obj = ce->data.obj;
507 cd717821 2018-06-22 stsp if (obj->refcnt == 1)
508 cd717821 2018-06-22 stsp break;
509 cd717821 2018-06-22 stsp fprintf(stderr, "object %s has %d unclaimed references\n",
510 cd717821 2018-06-22 stsp id_str, obj->refcnt - 1);
511 cd717821 2018-06-22 stsp break;
512 cd717821 2018-06-22 stsp case GOT_OBJECT_CACHE_TYPE_TREE:
513 cd717821 2018-06-22 stsp tree = ce->data.tree;
514 cd717821 2018-06-22 stsp if (tree->refcnt == 1)
515 cd717821 2018-06-22 stsp break;
516 cd717821 2018-06-22 stsp fprintf(stderr, "tree %s has %d unclaimed references\n",
517 cd717821 2018-06-22 stsp id_str, tree->refcnt - 1);
518 cd717821 2018-06-22 stsp break;
519 cd717821 2018-06-22 stsp case GOT_OBJECT_CACHE_TYPE_COMMIT:
520 cd717821 2018-06-22 stsp commit = ce->data.commit;
521 cd717821 2018-06-22 stsp if (commit->refcnt == 1)
522 cd717821 2018-06-22 stsp break;
523 cd717821 2018-06-22 stsp fprintf(stderr, "commit %s has %d unclaimed references\n",
524 cd717821 2018-06-22 stsp id_str, commit->refcnt);
525 cd717821 2018-06-22 stsp break;
526 cd717821 2018-06-22 stsp }
527 cd717821 2018-06-22 stsp free(id_str);
528 1943de01 2018-06-22 stsp }
529 cd717821 2018-06-22 stsp #endif
530 1943de01 2018-06-22 stsp
531 ad242220 2018-09-08 stsp static const struct got_error *
532 ad242220 2018-09-08 stsp wait_for_child(pid_t pid)
533 ad242220 2018-09-08 stsp {
534 ad242220 2018-09-08 stsp int child_status;
535 ad242220 2018-09-08 stsp
536 ad242220 2018-09-08 stsp waitpid(pid, &child_status, 0);
537 ad242220 2018-09-08 stsp
538 ad242220 2018-09-08 stsp if (!WIFEXITED(child_status))
539 ad242220 2018-09-08 stsp return got_error(GOT_ERR_PRIVSEP_DIED);
540 ad242220 2018-09-08 stsp
541 ad242220 2018-09-08 stsp if (WEXITSTATUS(child_status) != 0)
542 ad242220 2018-09-08 stsp return got_error(GOT_ERR_PRIVSEP_EXIT);
543 ad242220 2018-09-08 stsp
544 ad242220 2018-09-08 stsp return NULL;
545 ad242220 2018-09-08 stsp }
546 ad242220 2018-09-08 stsp
547 ad242220 2018-09-08 stsp const struct got_error *
548 4027f31a 2017-11-04 stsp got_repo_close(struct got_repository *repo)
549 4027f31a 2017-11-04 stsp {
550 ad242220 2018-09-08 stsp const struct got_error *err = NULL, *child_err;
551 79b11c62 2018-03-09 stsp int i;
552 79b11c62 2018-03-09 stsp
553 65cf1e80 2018-03-16 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
554 65cf1e80 2018-03-16 stsp if (repo->packidx_cache[i] == NULL)
555 79b11c62 2018-03-09 stsp break;
556 65cf1e80 2018-03-16 stsp got_packidx_close(repo->packidx_cache[i]);
557 79b11c62 2018-03-09 stsp }
558 bd1223b9 2018-03-14 stsp
559 7e656b93 2018-03-17 stsp for (i = 0; i < nitems(repo->packs); i++) {
560 7e656b93 2018-03-17 stsp if (repo->packs[i].path_packfile == NULL)
561 7e656b93 2018-03-17 stsp break;
562 7e656b93 2018-03-17 stsp got_pack_close(&repo->packs[i]);
563 7e656b93 2018-03-17 stsp }
564 7e656b93 2018-03-17 stsp
565 4027f31a 2017-11-04 stsp free(repo->path);
566 4986b9d5 2018-03-12 stsp free(repo->path_git_dir);
567 cd717821 2018-06-22 stsp
568 cd717821 2018-06-22 stsp #if 0
569 1943de01 2018-06-22 stsp print_cache_stats(&repo->objcache, "object");
570 1943de01 2018-06-22 stsp print_cache_stats(&repo->treecache, "tree");
571 1943de01 2018-06-22 stsp print_cache_stats(&repo->commitcache, "commit");
572 eb77ee11 2018-07-08 stsp got_object_idcache_for_each(repo->objcache.idcache, check_refcount,
573 cd717821 2018-06-22 stsp &repo->objcache);
574 eb77ee11 2018-07-08 stsp got_object_idcache_for_each(repo->treecache.idcache, check_refcount,
575 cd717821 2018-06-22 stsp &repo->treecache);
576 eb77ee11 2018-07-08 stsp got_object_idcache_for_each(repo->commitcache.idcache, check_refcount,
577 cd717821 2018-06-22 stsp &repo->commitcache);
578 cd717821 2018-06-22 stsp #endif
579 cd717821 2018-06-22 stsp
580 eb77ee11 2018-07-08 stsp if (repo->objcache.idcache)
581 eb77ee11 2018-07-08 stsp got_object_idcache_free(repo->objcache.idcache);
582 eb77ee11 2018-07-08 stsp if (repo->treecache.idcache)
583 eb77ee11 2018-07-08 stsp got_object_idcache_free(repo->treecache.idcache);
584 eb77ee11 2018-07-08 stsp if (repo->commitcache.idcache)
585 eb77ee11 2018-07-08 stsp got_object_idcache_free(repo->commitcache.idcache);
586 ad242220 2018-09-08 stsp
587 ad242220 2018-09-08 stsp for (i = 0; i < nitems(repo->privsep_children); i++) {
588 ad242220 2018-09-08 stsp if (repo->privsep_children[i].imsg_fd == -1)
589 ad242220 2018-09-08 stsp continue;
590 3516b818 2018-09-08 stsp imsg_clear(repo->privsep_children[i].ibuf);
591 3516b818 2018-09-08 stsp free(repo->privsep_children[i].ibuf);
592 ad242220 2018-09-08 stsp err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
593 ad242220 2018-09-08 stsp child_err = wait_for_child(repo->privsep_children[i].pid);
594 ad242220 2018-09-08 stsp if (child_err && err == NULL)
595 ad242220 2018-09-08 stsp err = child_err;
596 ad242220 2018-09-08 stsp close(repo->privsep_children[i].imsg_fd);
597 ad242220 2018-09-08 stsp }
598 4027f31a 2017-11-04 stsp free(repo);
599 ad242220 2018-09-08 stsp
600 ad242220 2018-09-08 stsp return err;
601 4027f31a 2017-11-04 stsp }
602 04ca23f4 2018-07-16 stsp
603 04ca23f4 2018-07-16 stsp const struct got_error *
604 04ca23f4 2018-07-16 stsp got_repo_map_path(char **in_repo_path, struct got_repository *repo,
605 04ca23f4 2018-07-16 stsp const char *input_path)
606 04ca23f4 2018-07-16 stsp {
607 04ca23f4 2018-07-16 stsp const struct got_error *err = NULL;
608 04ca23f4 2018-07-16 stsp char *repo_abspath = NULL, *cwd = NULL;
609 04ca23f4 2018-07-16 stsp struct stat sb;
610 04ca23f4 2018-07-16 stsp size_t repolen, cwdlen, len;
611 04ca23f4 2018-07-16 stsp char *canonpath, *path;
612 04ca23f4 2018-07-16 stsp
613 04ca23f4 2018-07-16 stsp *in_repo_path = NULL;
614 04ca23f4 2018-07-16 stsp
615 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
616 04ca23f4 2018-07-16 stsp if (cwd == NULL)
617 04ca23f4 2018-07-16 stsp return got_error_from_errno();
618 04ca23f4 2018-07-16 stsp
619 04ca23f4 2018-07-16 stsp canonpath = strdup(input_path);
620 04ca23f4 2018-07-16 stsp if (canonpath == NULL) {
621 04ca23f4 2018-07-16 stsp err = got_error_from_errno();
622 04ca23f4 2018-07-16 stsp goto done;
623 04ca23f4 2018-07-16 stsp }
624 04ca23f4 2018-07-16 stsp err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
625 04ca23f4 2018-07-16 stsp if (err)
626 04ca23f4 2018-07-16 stsp goto done;
627 04ca23f4 2018-07-16 stsp
628 04ca23f4 2018-07-16 stsp repo_abspath = got_repo_get_path(repo);
629 04ca23f4 2018-07-16 stsp if (repo_abspath == NULL) {
630 04ca23f4 2018-07-16 stsp err = got_error_from_errno();
631 04ca23f4 2018-07-16 stsp goto done;
632 04ca23f4 2018-07-16 stsp }
633 04ca23f4 2018-07-16 stsp
634 04ca23f4 2018-07-16 stsp /* TODO: Call "get in-repository path of work-tree node" API. */
635 04ca23f4 2018-07-16 stsp
636 04ca23f4 2018-07-16 stsp if (lstat(canonpath, &sb) != 0) {
637 04ca23f4 2018-07-16 stsp if (errno != ENOENT) {
638 04ca23f4 2018-07-16 stsp err = got_error_from_errno();
639 04ca23f4 2018-07-16 stsp goto done;
640 04ca23f4 2018-07-16 stsp }
641 04ca23f4 2018-07-16 stsp /*
642 04ca23f4 2018-07-16 stsp * Path is not on disk.
643 04ca23f4 2018-07-16 stsp * Assume it is already relative to repository root.
644 04ca23f4 2018-07-16 stsp */
645 04ca23f4 2018-07-16 stsp path = strdup(canonpath);
646 04ca23f4 2018-07-16 stsp } else {
647 04ca23f4 2018-07-16 stsp int is_repo_child = 0, is_cwd_child = 0;
648 04ca23f4 2018-07-16 stsp
649 04ca23f4 2018-07-16 stsp path = realpath(canonpath, NULL);
650 04ca23f4 2018-07-16 stsp if (path == NULL) {
651 04ca23f4 2018-07-16 stsp err = got_error_from_errno();
652 04ca23f4 2018-07-16 stsp goto done;
653 04ca23f4 2018-07-16 stsp }
654 04ca23f4 2018-07-16 stsp
655 04ca23f4 2018-07-16 stsp repolen = strlen(repo_abspath);
656 04ca23f4 2018-07-16 stsp cwdlen = strlen(cwd);
657 04ca23f4 2018-07-16 stsp len = strlen(path);
658 04ca23f4 2018-07-16 stsp
659 04ca23f4 2018-07-16 stsp if (len > repolen && strncmp(path, repo_abspath, repolen) == 0)
660 04ca23f4 2018-07-16 stsp is_repo_child = 1;
661 04ca23f4 2018-07-16 stsp if (len > cwdlen && strncmp(path, cwd, cwdlen) == 0)
662 04ca23f4 2018-07-16 stsp is_cwd_child = 1;
663 04ca23f4 2018-07-16 stsp
664 04ca23f4 2018-07-16 stsp if (strcmp(path, repo_abspath) == 0) {
665 04ca23f4 2018-07-16 stsp free(path);
666 04ca23f4 2018-07-16 stsp path = strdup("");
667 04ca23f4 2018-07-16 stsp if (path == NULL) {
668 04ca23f4 2018-07-16 stsp err = got_error_from_errno();
669 04ca23f4 2018-07-16 stsp goto done;
670 04ca23f4 2018-07-16 stsp }
671 04ca23f4 2018-07-16 stsp } else if (is_repo_child && is_cwd_child) {
672 04ca23f4 2018-07-16 stsp char *child;
673 04ca23f4 2018-07-16 stsp /* TODO: Is path inside a got worktree? */
674 04ca23f4 2018-07-16 stsp /* Strip common prefix with repository path. */
675 04ca23f4 2018-07-16 stsp err = got_path_skip_common_ancestor(&child,
676 04ca23f4 2018-07-16 stsp repo_abspath, path);
677 04ca23f4 2018-07-16 stsp if (err)
678 04ca23f4 2018-07-16 stsp goto done;
679 04ca23f4 2018-07-16 stsp free(path);
680 04ca23f4 2018-07-16 stsp path = child;
681 04ca23f4 2018-07-16 stsp } else if (is_repo_child) {
682 04ca23f4 2018-07-16 stsp /* Matched an on-disk path inside repository. */
683 04ca23f4 2018-07-16 stsp if (got_repo_is_bare(repo)) {
684 04ca23f4 2018-07-16 stsp /*
685 04ca23f4 2018-07-16 stsp * Matched an on-disk path inside repository
686 04ca23f4 2018-07-16 stsp * database. Treat as repository-relative.
687 04ca23f4 2018-07-16 stsp */
688 04ca23f4 2018-07-16 stsp } else {
689 04ca23f4 2018-07-16 stsp char *child;
690 04ca23f4 2018-07-16 stsp /* Strip common prefix with repository path. */
691 04ca23f4 2018-07-16 stsp err = got_path_skip_common_ancestor(&child,
692 04ca23f4 2018-07-16 stsp repo_abspath, path);
693 04ca23f4 2018-07-16 stsp if (err)
694 04ca23f4 2018-07-16 stsp goto done;
695 04ca23f4 2018-07-16 stsp free(path);
696 04ca23f4 2018-07-16 stsp path = child;
697 04ca23f4 2018-07-16 stsp }
698 04ca23f4 2018-07-16 stsp } else if (is_cwd_child) {
699 04ca23f4 2018-07-16 stsp char *child;
700 04ca23f4 2018-07-16 stsp /* TODO: Is path inside a got worktree? */
701 04ca23f4 2018-07-16 stsp /* Strip common prefix with cwd. */
702 04ca23f4 2018-07-16 stsp err = got_path_skip_common_ancestor(&child, cwd,
703 04ca23f4 2018-07-16 stsp path);
704 04ca23f4 2018-07-16 stsp if (err)
705 04ca23f4 2018-07-16 stsp goto done;
706 04ca23f4 2018-07-16 stsp free(path);
707 04ca23f4 2018-07-16 stsp path = child;
708 04ca23f4 2018-07-16 stsp } else {
709 04ca23f4 2018-07-16 stsp /*
710 04ca23f4 2018-07-16 stsp * Matched unrelated on-disk path.
711 04ca23f4 2018-07-16 stsp * Treat it as repository-relative.
712 04ca23f4 2018-07-16 stsp */
713 04ca23f4 2018-07-16 stsp }
714 04ca23f4 2018-07-16 stsp }
715 04ca23f4 2018-07-16 stsp
716 04ca23f4 2018-07-16 stsp /* Make in-repository path absolute */
717 04ca23f4 2018-07-16 stsp if (path[0] != '/') {
718 04ca23f4 2018-07-16 stsp char *abspath;
719 04ca23f4 2018-07-16 stsp if (asprintf(&abspath, "/%s", path) == -1) {
720 04ca23f4 2018-07-16 stsp err = got_error_from_errno();
721 04ca23f4 2018-07-16 stsp goto done;
722 04ca23f4 2018-07-16 stsp }
723 04ca23f4 2018-07-16 stsp free(path);
724 04ca23f4 2018-07-16 stsp path = abspath;
725 04ca23f4 2018-07-16 stsp }
726 04ca23f4 2018-07-16 stsp
727 04ca23f4 2018-07-16 stsp done:
728 04ca23f4 2018-07-16 stsp free(repo_abspath);
729 04ca23f4 2018-07-16 stsp free(cwd);
730 04ca23f4 2018-07-16 stsp free(canonpath);
731 04ca23f4 2018-07-16 stsp if (err)
732 04ca23f4 2018-07-16 stsp free(path);
733 04ca23f4 2018-07-16 stsp else
734 04ca23f4 2018-07-16 stsp *in_repo_path = path;
735 04ca23f4 2018-07-16 stsp return err;
736 04ca23f4 2018-07-16 stsp }