Blame


1 7b19e0f1 2017-11-05 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 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 aba9c984 2019-09-08 stsp #include <sys/socket.h>
21 deeca238 2018-03-12 stsp #include <sys/stat.h>
22 1510f469 2018-09-09 stsp #include <sys/mman.h>
23 876c234b 2018-09-10 stsp #include <sys/syslimits.h>
24 79b11c62 2018-03-09 stsp
25 e09a504c 2019-06-28 stsp #include <ctype.h>
26 1510f469 2018-09-09 stsp #include <fcntl.h>
27 3ce1b845 2019-07-15 stsp #include <fnmatch.h>
28 4027f31a 2017-11-04 stsp #include <limits.h>
29 1510f469 2018-09-09 stsp #include <dirent.h>
30 4027f31a 2017-11-04 stsp #include <stdlib.h>
31 4027f31a 2017-11-04 stsp #include <stdio.h>
32 4027f31a 2017-11-04 stsp #include <sha1.h>
33 4027f31a 2017-11-04 stsp #include <string.h>
34 303e14b5 2019-09-23 stsp #include <time.h>
35 79b11c62 2018-03-09 stsp #include <zlib.h>
36 85f51bba 2018-07-16 stsp #include <errno.h>
37 85f51bba 2018-07-16 stsp #include <libgen.h>
38 ad242220 2018-09-08 stsp #include <stdint.h>
39 ad242220 2018-09-08 stsp #include <imsg.h>
40 c442a90d 2019-03-10 stsp #include <uuid.h>
41 4027f31a 2017-11-04 stsp
42 4027f31a 2017-11-04 stsp #include "got_error.h"
43 5261c201 2018-04-01 stsp #include "got_reference.h"
44 4027f31a 2017-11-04 stsp #include "got_repository.h"
45 1dd54920 2019-05-11 stsp #include "got_path.h"
46 e6209546 2019-08-22 stsp #include "got_cancel.h"
47 442a3ddc 2018-04-23 stsp #include "got_worktree.h"
48 7bb0daa1 2018-06-21 stsp #include "got_object.h"
49 4027f31a 2017-11-04 stsp
50 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
51 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
52 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
53 3ce1b845 2019-07-15 stsp #include "got_lib_object_parse.h"
54 3ce1b845 2019-07-15 stsp #include "got_lib_object_create.h"
55 718b3ab0 2018-03-17 stsp #include "got_lib_pack.h"
56 876c234b 2018-09-10 stsp #include "got_lib_privsep.h"
57 442a3ddc 2018-04-23 stsp #include "got_lib_worktree.h"
58 e09a504c 2019-06-28 stsp #include "got_lib_sha1.h"
59 6bef87be 2018-09-11 stsp #include "got_lib_object_cache.h"
60 6bef87be 2018-09-11 stsp #include "got_lib_repository.h"
61 c3f94f68 2017-11-05 stsp
62 79b11c62 2018-03-09 stsp #ifndef nitems
63 79b11c62 2018-03-09 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
64 79b11c62 2018-03-09 stsp #endif
65 3b339b2f 2018-02-12 stsp
66 4027f31a 2017-11-04 stsp #define GOT_GIT_DIR ".git"
67 4027f31a 2017-11-04 stsp
68 4027f31a 2017-11-04 stsp /* Mandatory files and directories inside the git directory. */
69 4df642d9 2017-11-05 stsp #define GOT_OBJECTS_DIR "objects"
70 4df642d9 2017-11-05 stsp #define GOT_REFS_DIR "refs"
71 4df642d9 2017-11-05 stsp #define GOT_HEAD_FILE "HEAD"
72 1d126e2d 2019-08-24 stsp #define GOT_GITCONFIG "config"
73 4027f31a 2017-11-04 stsp
74 a1fd68d8 2018-01-12 stsp /* Other files and directories inside the git directory. */
75 4df642d9 2017-11-05 stsp #define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
76 4df642d9 2017-11-05 stsp #define GOT_ORIG_HEAD_FILE "ORIG_HEAD"
77 a1fd68d8 2018-01-12 stsp #define GOT_OBJECTS_PACK_DIR "objects/pack"
78 fb79db15 2019-02-01 stsp #define GOT_PACKED_REFS_FILE "packed-refs"
79 4df642d9 2017-11-05 stsp
80 7839bc15 2019-01-06 stsp const char *
81 86c3caaf 2018-03-09 stsp got_repo_get_path(struct got_repository *repo)
82 86c3caaf 2018-03-09 stsp {
83 7839bc15 2019-01-06 stsp return repo->path;
84 86c3caaf 2018-03-09 stsp }
85 86c3caaf 2018-03-09 stsp
86 6e9da951 2019-01-06 stsp const char *
87 11995603 2017-11-05 stsp got_repo_get_path_git_dir(struct got_repository *repo)
88 4027f31a 2017-11-04 stsp {
89 6e9da951 2019-01-06 stsp return repo->path_git_dir;
90 04ca23f4 2018-07-16 stsp }
91 04ca23f4 2018-07-16 stsp
92 aba9c984 2019-09-08 stsp const char *
93 aba9c984 2019-09-08 stsp got_repo_get_gitconfig_author_name(struct got_repository *repo)
94 aba9c984 2019-09-08 stsp {
95 aba9c984 2019-09-08 stsp return repo->gitconfig_author_name;
96 aba9c984 2019-09-08 stsp }
97 aba9c984 2019-09-08 stsp
98 aba9c984 2019-09-08 stsp const char *
99 aba9c984 2019-09-08 stsp got_repo_get_gitconfig_author_email(struct got_repository *repo)
100 aba9c984 2019-09-08 stsp {
101 aba9c984 2019-09-08 stsp return repo->gitconfig_author_email;
102 c9956ddf 2019-09-08 stsp }
103 c9956ddf 2019-09-08 stsp
104 c9956ddf 2019-09-08 stsp const char *
105 c9956ddf 2019-09-08 stsp got_repo_get_global_gitconfig_author_name(struct got_repository *repo)
106 c9956ddf 2019-09-08 stsp {
107 c9956ddf 2019-09-08 stsp return repo->global_gitconfig_author_name;
108 c9956ddf 2019-09-08 stsp }
109 c9956ddf 2019-09-08 stsp
110 c9956ddf 2019-09-08 stsp const char *
111 c9956ddf 2019-09-08 stsp got_repo_get_global_gitconfig_author_email(struct got_repository *repo)
112 c9956ddf 2019-09-08 stsp {
113 c9956ddf 2019-09-08 stsp return repo->global_gitconfig_author_email;
114 9a1cc63f 2020-02-03 stsp }
115 9a1cc63f 2020-02-03 stsp
116 9a1cc63f 2020-02-03 stsp const char *
117 9a1cc63f 2020-02-03 stsp got_repo_get_gitconfig_owner(struct got_repository *repo)
118 9a1cc63f 2020-02-03 stsp {
119 9a1cc63f 2020-02-03 stsp return repo->gitconfig_owner;
120 aba9c984 2019-09-08 stsp }
121 aba9c984 2019-09-08 stsp
122 04ca23f4 2018-07-16 stsp int
123 04ca23f4 2018-07-16 stsp got_repo_is_bare(struct got_repository *repo)
124 04ca23f4 2018-07-16 stsp {
125 04ca23f4 2018-07-16 stsp return (strcmp(repo->path, repo->path_git_dir) == 0);
126 4027f31a 2017-11-04 stsp }
127 4027f31a 2017-11-04 stsp
128 4027f31a 2017-11-04 stsp static char *
129 4027f31a 2017-11-04 stsp get_path_git_child(struct got_repository *repo, const char *basename)
130 4027f31a 2017-11-04 stsp {
131 4027f31a 2017-11-04 stsp char *path_child;
132 4027f31a 2017-11-04 stsp
133 4986b9d5 2018-03-12 stsp if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
134 4027f31a 2017-11-04 stsp basename) == -1)
135 4027f31a 2017-11-04 stsp return NULL;
136 4027f31a 2017-11-04 stsp
137 4027f31a 2017-11-04 stsp return path_child;
138 4027f31a 2017-11-04 stsp }
139 4027f31a 2017-11-04 stsp
140 11995603 2017-11-05 stsp char *
141 11995603 2017-11-05 stsp got_repo_get_path_objects(struct got_repository *repo)
142 4027f31a 2017-11-04 stsp {
143 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_OBJECTS_DIR);
144 4027f31a 2017-11-04 stsp }
145 4027f31a 2017-11-04 stsp
146 11995603 2017-11-05 stsp char *
147 a1fd68d8 2018-01-12 stsp got_repo_get_path_objects_pack(struct got_repository *repo)
148 a1fd68d8 2018-01-12 stsp {
149 a1fd68d8 2018-01-12 stsp return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
150 a1fd68d8 2018-01-12 stsp }
151 a1fd68d8 2018-01-12 stsp
152 a1fd68d8 2018-01-12 stsp char *
153 11995603 2017-11-05 stsp got_repo_get_path_refs(struct got_repository *repo)
154 4027f31a 2017-11-04 stsp {
155 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_REFS_DIR);
156 4027f31a 2017-11-04 stsp }
157 4027f31a 2017-11-04 stsp
158 fb79db15 2019-02-01 stsp char *
159 fb79db15 2019-02-01 stsp got_repo_get_path_packed_refs(struct got_repository *repo)
160 fb79db15 2019-02-01 stsp {
161 fb79db15 2019-02-01 stsp return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
162 fb79db15 2019-02-01 stsp }
163 fb79db15 2019-02-01 stsp
164 4027f31a 2017-11-04 stsp static char *
165 4027f31a 2017-11-04 stsp get_path_head(struct got_repository *repo)
166 4027f31a 2017-11-04 stsp {
167 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_HEAD_FILE);
168 1d126e2d 2019-08-24 stsp }
169 1d126e2d 2019-08-24 stsp
170 1d126e2d 2019-08-24 stsp static const struct got_error *
171 1d126e2d 2019-08-24 stsp get_path_gitconfig(char **p, struct got_repository *repo)
172 1d126e2d 2019-08-24 stsp {
173 1d126e2d 2019-08-24 stsp *p = get_path_git_child(repo, GOT_GITCONFIG);
174 1d126e2d 2019-08-24 stsp if (*p == NULL)
175 1d126e2d 2019-08-24 stsp return got_error_from_errno("asprintf");
176 1d126e2d 2019-08-24 stsp return NULL;
177 cd95becd 2019-11-29 stsp }
178 cd95becd 2019-11-29 stsp
179 cd95becd 2019-11-29 stsp void
180 cd95becd 2019-11-29 stsp got_repo_get_gitconfig_remotes(int *nremotes, struct got_remote_repo **remotes,
181 cd95becd 2019-11-29 stsp struct got_repository *repo)
182 cd95becd 2019-11-29 stsp {
183 cd95becd 2019-11-29 stsp *nremotes = repo->ngitconfig_remotes;
184 cd95becd 2019-11-29 stsp *remotes = repo->gitconfig_remotes;
185 4027f31a 2017-11-04 stsp }
186 4027f31a 2017-11-04 stsp
187 4027f31a 2017-11-04 stsp static int
188 4027f31a 2017-11-04 stsp is_git_repo(struct got_repository *repo)
189 4027f31a 2017-11-04 stsp {
190 6e9da951 2019-01-06 stsp const char *path_git = got_repo_get_path_git_dir(repo);
191 11995603 2017-11-05 stsp char *path_objects = got_repo_get_path_objects(repo);
192 11995603 2017-11-05 stsp char *path_refs = got_repo_get_path_refs(repo);
193 4027f31a 2017-11-04 stsp char *path_head = get_path_head(repo);
194 deeca238 2018-03-12 stsp int ret = 0;
195 deeca238 2018-03-12 stsp struct stat sb;
196 4847cca1 2018-03-12 stsp struct got_reference *head_ref;
197 4027f31a 2017-11-04 stsp
198 deeca238 2018-03-12 stsp if (lstat(path_git, &sb) == -1)
199 deeca238 2018-03-12 stsp goto done;
200 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
201 deeca238 2018-03-12 stsp goto done;
202 4027f31a 2017-11-04 stsp
203 deeca238 2018-03-12 stsp if (lstat(path_objects, &sb) == -1)
204 deeca238 2018-03-12 stsp goto done;
205 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
206 deeca238 2018-03-12 stsp goto done;
207 deeca238 2018-03-12 stsp
208 deeca238 2018-03-12 stsp if (lstat(path_refs, &sb) == -1)
209 deeca238 2018-03-12 stsp goto done;
210 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
211 deeca238 2018-03-12 stsp goto done;
212 deeca238 2018-03-12 stsp
213 deeca238 2018-03-12 stsp if (lstat(path_head, &sb) == -1)
214 deeca238 2018-03-12 stsp goto done;
215 deeca238 2018-03-12 stsp if (!S_ISREG(sb.st_mode))
216 deeca238 2018-03-12 stsp goto done;
217 4847cca1 2018-03-12 stsp
218 4847cca1 2018-03-12 stsp /* Check if the HEAD reference can be opened. */
219 2f17228e 2019-05-12 stsp if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
220 4847cca1 2018-03-12 stsp goto done;
221 4847cca1 2018-03-12 stsp got_ref_close(head_ref);
222 4847cca1 2018-03-12 stsp
223 deeca238 2018-03-12 stsp ret = 1;
224 deeca238 2018-03-12 stsp done:
225 4027f31a 2017-11-04 stsp free(path_objects);
226 4027f31a 2017-11-04 stsp free(path_refs);
227 4027f31a 2017-11-04 stsp free(path_head);
228 4027f31a 2017-11-04 stsp return ret;
229 4027f31a 2017-11-04 stsp
230 7bb0daa1 2018-06-21 stsp }
231 7bb0daa1 2018-06-21 stsp
232 f6be5c30 2018-06-22 stsp const struct got_error *
233 f6be5c30 2018-06-22 stsp got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
234 f6be5c30 2018-06-22 stsp struct got_object *obj)
235 f6be5c30 2018-06-22 stsp {
236 ccfe88e6 2018-07-12 stsp #ifndef GOT_NO_OBJ_CACHE
237 f6be5c30 2018-06-22 stsp const struct got_error *err = NULL;
238 6bef87be 2018-09-11 stsp err = got_object_cache_add(&repo->objcache, id, obj);
239 79c99a64 2019-05-23 stsp if (err) {
240 79c99a64 2019-05-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
241 79c99a64 2019-05-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
242 79c99a64 2019-05-23 stsp err = NULL;
243 f6be5c30 2018-06-22 stsp return err;
244 79c99a64 2019-05-23 stsp }
245 f6be5c30 2018-06-22 stsp obj->refcnt++;
246 ccfe88e6 2018-07-12 stsp #endif
247 f6be5c30 2018-06-22 stsp return NULL;
248 f6be5c30 2018-06-22 stsp }
249 f6be5c30 2018-06-22 stsp
250 7bb0daa1 2018-06-21 stsp struct got_object *
251 7bb0daa1 2018-06-21 stsp got_repo_get_cached_object(struct got_repository *repo,
252 7bb0daa1 2018-06-21 stsp struct got_object_id *id)
253 7bb0daa1 2018-06-21 stsp {
254 6bef87be 2018-09-11 stsp return (struct got_object *)got_object_cache_get(&repo->objcache, id);
255 7bb0daa1 2018-06-21 stsp }
256 7bb0daa1 2018-06-21 stsp
257 4027f31a 2017-11-04 stsp const struct got_error *
258 f6be5c30 2018-06-22 stsp got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
259 f6be5c30 2018-06-22 stsp struct got_tree_object *tree)
260 f6be5c30 2018-06-22 stsp {
261 ccfe88e6 2018-07-12 stsp #ifndef GOT_NO_OBJ_CACHE
262 f6be5c30 2018-06-22 stsp const struct got_error *err = NULL;
263 6bef87be 2018-09-11 stsp err = got_object_cache_add(&repo->treecache, id, tree);
264 79c99a64 2019-05-23 stsp if (err) {
265 79c99a64 2019-05-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
266 79c99a64 2019-05-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
267 79c99a64 2019-05-23 stsp err = NULL;
268 f6be5c30 2018-06-22 stsp return err;
269 79c99a64 2019-05-23 stsp }
270 f6be5c30 2018-06-22 stsp tree->refcnt++;
271 ccfe88e6 2018-07-12 stsp #endif
272 f6be5c30 2018-06-22 stsp return NULL;
273 f6be5c30 2018-06-22 stsp }
274 f6be5c30 2018-06-22 stsp
275 f6be5c30 2018-06-22 stsp struct got_tree_object *
276 f6be5c30 2018-06-22 stsp got_repo_get_cached_tree(struct got_repository *repo,
277 f6be5c30 2018-06-22 stsp struct got_object_id *id)
278 f6be5c30 2018-06-22 stsp {
279 6bef87be 2018-09-11 stsp return (struct got_tree_object *)got_object_cache_get(
280 6bef87be 2018-09-11 stsp &repo->treecache, id);
281 1943de01 2018-06-22 stsp }
282 1943de01 2018-06-22 stsp
283 1943de01 2018-06-22 stsp const struct got_error *
284 1943de01 2018-06-22 stsp got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
285 1943de01 2018-06-22 stsp struct got_commit_object *commit)
286 1943de01 2018-06-22 stsp {
287 ccfe88e6 2018-07-12 stsp #ifndef GOT_NO_OBJ_CACHE
288 1943de01 2018-06-22 stsp const struct got_error *err = NULL;
289 6bef87be 2018-09-11 stsp err = got_object_cache_add(&repo->commitcache, id, commit);
290 79c99a64 2019-05-23 stsp if (err) {
291 79c99a64 2019-05-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
292 79c99a64 2019-05-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
293 79c99a64 2019-05-23 stsp err = NULL;
294 1943de01 2018-06-22 stsp return err;
295 79c99a64 2019-05-23 stsp }
296 1943de01 2018-06-22 stsp commit->refcnt++;
297 ccfe88e6 2018-07-12 stsp #endif
298 f6be5c30 2018-06-22 stsp return NULL;
299 f6be5c30 2018-06-22 stsp }
300 f6be5c30 2018-06-22 stsp
301 1943de01 2018-06-22 stsp struct got_commit_object *
302 1943de01 2018-06-22 stsp got_repo_get_cached_commit(struct got_repository *repo,
303 1943de01 2018-06-22 stsp struct got_object_id *id)
304 1943de01 2018-06-22 stsp {
305 6bef87be 2018-09-11 stsp return (struct got_commit_object *)got_object_cache_get(
306 6bef87be 2018-09-11 stsp &repo->commitcache, id);
307 f4a881ce 2018-11-17 stsp }
308 f4a881ce 2018-11-17 stsp
309 f4a881ce 2018-11-17 stsp const struct got_error *
310 f4a881ce 2018-11-17 stsp got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
311 f4a881ce 2018-11-17 stsp struct got_tag_object *tag)
312 f4a881ce 2018-11-17 stsp {
313 f4a881ce 2018-11-17 stsp #ifndef GOT_NO_OBJ_CACHE
314 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
315 f4a881ce 2018-11-17 stsp err = got_object_cache_add(&repo->tagcache, id, tag);
316 79c99a64 2019-05-23 stsp if (err) {
317 79c99a64 2019-05-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
318 79c99a64 2019-05-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
319 79c99a64 2019-05-23 stsp err = NULL;
320 f4a881ce 2018-11-17 stsp return err;
321 79c99a64 2019-05-23 stsp }
322 f4a881ce 2018-11-17 stsp tag->refcnt++;
323 f4a881ce 2018-11-17 stsp #endif
324 f4a881ce 2018-11-17 stsp return NULL;
325 f4a881ce 2018-11-17 stsp }
326 f4a881ce 2018-11-17 stsp
327 f4a881ce 2018-11-17 stsp struct got_tag_object *
328 f4a881ce 2018-11-17 stsp got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
329 f4a881ce 2018-11-17 stsp {
330 f4a881ce 2018-11-17 stsp return (struct got_tag_object *)got_object_cache_get(
331 f4a881ce 2018-11-17 stsp &repo->tagcache, id);
332 1943de01 2018-06-22 stsp }
333 1943de01 2018-06-22 stsp
334 f6be5c30 2018-06-22 stsp const struct got_error *
335 85f51bba 2018-07-16 stsp open_repo(struct got_repository *repo, const char *path)
336 4027f31a 2017-11-04 stsp {
337 85f51bba 2018-07-16 stsp const struct got_error *err = NULL;
338 85f51bba 2018-07-16 stsp
339 85f51bba 2018-07-16 stsp /* bare git repository? */
340 85f51bba 2018-07-16 stsp repo->path_git_dir = strdup(path);
341 ee645855 2019-02-05 stsp if (repo->path_git_dir == NULL)
342 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
343 85f51bba 2018-07-16 stsp if (is_git_repo(repo)) {
344 85f51bba 2018-07-16 stsp repo->path = strdup(repo->path_git_dir);
345 85f51bba 2018-07-16 stsp if (repo->path == NULL) {
346 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
347 85f51bba 2018-07-16 stsp goto done;
348 85f51bba 2018-07-16 stsp }
349 85f51bba 2018-07-16 stsp return NULL;
350 85f51bba 2018-07-16 stsp }
351 85f51bba 2018-07-16 stsp
352 85f51bba 2018-07-16 stsp /* git repository with working tree? */
353 85f51bba 2018-07-16 stsp free(repo->path_git_dir);
354 6b68ccd6 2019-09-01 stsp repo->path_git_dir = NULL;
355 85f51bba 2018-07-16 stsp if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
356 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
357 85f51bba 2018-07-16 stsp goto done;
358 85f51bba 2018-07-16 stsp }
359 85f51bba 2018-07-16 stsp if (is_git_repo(repo)) {
360 85f51bba 2018-07-16 stsp repo->path = strdup(path);
361 85f51bba 2018-07-16 stsp if (repo->path == NULL) {
362 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
363 85f51bba 2018-07-16 stsp goto done;
364 85f51bba 2018-07-16 stsp }
365 85f51bba 2018-07-16 stsp return NULL;
366 85f51bba 2018-07-16 stsp }
367 85f51bba 2018-07-16 stsp
368 ee645855 2019-02-05 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
369 ee645855 2019-02-05 stsp done:
370 85f51bba 2018-07-16 stsp if (err) {
371 ee645855 2019-02-05 stsp free(repo->path);
372 ee645855 2019-02-05 stsp repo->path = NULL;
373 85f51bba 2018-07-16 stsp free(repo->path_git_dir);
374 ee645855 2019-02-05 stsp repo->path_git_dir = NULL;
375 aba9c984 2019-09-08 stsp }
376 aba9c984 2019-09-08 stsp return err;
377 aba9c984 2019-09-08 stsp }
378 aba9c984 2019-09-08 stsp
379 aba9c984 2019-09-08 stsp static const struct got_error *
380 c9956ddf 2019-09-08 stsp parse_gitconfig_file(int *gitconfig_repository_format_version,
381 c9956ddf 2019-09-08 stsp char **gitconfig_author_name, char **gitconfig_author_email,
382 cd95becd 2019-11-29 stsp struct got_remote_repo **remotes, int *nremotes,
383 9a1cc63f 2020-02-03 stsp char **gitconfig_owner,
384 c9956ddf 2019-09-08 stsp const char *gitconfig_path)
385 aba9c984 2019-09-08 stsp {
386 aba9c984 2019-09-08 stsp const struct got_error *err = NULL, *child_err = NULL;
387 aba9c984 2019-09-08 stsp int fd = -1;
388 aba9c984 2019-09-08 stsp int imsg_fds[2] = { -1, -1 };
389 aba9c984 2019-09-08 stsp pid_t pid;
390 aba9c984 2019-09-08 stsp struct imsgbuf *ibuf;
391 aba9c984 2019-09-08 stsp
392 c9956ddf 2019-09-08 stsp *gitconfig_repository_format_version = 0;
393 c9956ddf 2019-09-08 stsp *gitconfig_author_name = NULL;
394 c9956ddf 2019-09-08 stsp *gitconfig_author_email = NULL;
395 aba9c984 2019-09-08 stsp
396 aba9c984 2019-09-08 stsp fd = open(gitconfig_path, O_RDONLY);
397 aba9c984 2019-09-08 stsp if (fd == -1) {
398 c9956ddf 2019-09-08 stsp if (errno == ENOENT)
399 aba9c984 2019-09-08 stsp return NULL;
400 c9956ddf 2019-09-08 stsp return got_error_from_errno2("open", gitconfig_path);
401 aba9c984 2019-09-08 stsp }
402 aba9c984 2019-09-08 stsp
403 aba9c984 2019-09-08 stsp ibuf = calloc(1, sizeof(*ibuf));
404 aba9c984 2019-09-08 stsp if (ibuf == NULL) {
405 aba9c984 2019-09-08 stsp err = got_error_from_errno("calloc");
406 aba9c984 2019-09-08 stsp goto done;
407 aba9c984 2019-09-08 stsp }
408 aba9c984 2019-09-08 stsp
409 aba9c984 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
410 aba9c984 2019-09-08 stsp err = got_error_from_errno("socketpair");
411 aba9c984 2019-09-08 stsp goto done;
412 aba9c984 2019-09-08 stsp }
413 aba9c984 2019-09-08 stsp
414 aba9c984 2019-09-08 stsp pid = fork();
415 aba9c984 2019-09-08 stsp if (pid == -1) {
416 aba9c984 2019-09-08 stsp err = got_error_from_errno("fork");
417 aba9c984 2019-09-08 stsp goto done;
418 aba9c984 2019-09-08 stsp } else if (pid == 0) {
419 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
420 c9956ddf 2019-09-08 stsp gitconfig_path);
421 aba9c984 2019-09-08 stsp /* not reached */
422 aba9c984 2019-09-08 stsp }
423 aba9c984 2019-09-08 stsp
424 aba9c984 2019-09-08 stsp if (close(imsg_fds[1]) == -1) {
425 aba9c984 2019-09-08 stsp err = got_error_from_errno("close");
426 aba9c984 2019-09-08 stsp goto done;
427 85f51bba 2018-07-16 stsp }
428 aba9c984 2019-09-08 stsp imsg_fds[1] = -1;
429 aba9c984 2019-09-08 stsp imsg_init(ibuf, imsg_fds[0]);
430 aba9c984 2019-09-08 stsp
431 aba9c984 2019-09-08 stsp err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
432 aba9c984 2019-09-08 stsp if (err)
433 aba9c984 2019-09-08 stsp goto done;
434 aba9c984 2019-09-08 stsp fd = -1;
435 aba9c984 2019-09-08 stsp
436 aba9c984 2019-09-08 stsp err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
437 aba9c984 2019-09-08 stsp if (err)
438 aba9c984 2019-09-08 stsp goto done;
439 aba9c984 2019-09-08 stsp
440 aba9c984 2019-09-08 stsp err = got_privsep_recv_gitconfig_int(
441 c9956ddf 2019-09-08 stsp gitconfig_repository_format_version, ibuf);
442 aba9c984 2019-09-08 stsp if (err)
443 aba9c984 2019-09-08 stsp goto done;
444 aba9c984 2019-09-08 stsp
445 aba9c984 2019-09-08 stsp err = got_privsep_send_gitconfig_author_name_req(ibuf);
446 aba9c984 2019-09-08 stsp if (err)
447 aba9c984 2019-09-08 stsp goto done;
448 aba9c984 2019-09-08 stsp
449 c9956ddf 2019-09-08 stsp err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
450 aba9c984 2019-09-08 stsp if (err)
451 aba9c984 2019-09-08 stsp goto done;
452 aba9c984 2019-09-08 stsp
453 aba9c984 2019-09-08 stsp err = got_privsep_send_gitconfig_author_email_req(ibuf);
454 aba9c984 2019-09-08 stsp if (err)
455 aba9c984 2019-09-08 stsp goto done;
456 aba9c984 2019-09-08 stsp
457 c9956ddf 2019-09-08 stsp err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
458 aba9c984 2019-09-08 stsp if (err)
459 aba9c984 2019-09-08 stsp goto done;
460 cd95becd 2019-11-29 stsp
461 cd95becd 2019-11-29 stsp if (remotes && nremotes) {
462 cd95becd 2019-11-29 stsp err = got_privsep_send_gitconfig_remotes_req(ibuf);
463 cd95becd 2019-11-29 stsp if (err)
464 cd95becd 2019-11-29 stsp goto done;
465 cd95becd 2019-11-29 stsp
466 cd95becd 2019-11-29 stsp err = got_privsep_recv_gitconfig_remotes(remotes,
467 cd95becd 2019-11-29 stsp nremotes, ibuf);
468 9a1cc63f 2020-02-03 stsp if (err)
469 9a1cc63f 2020-02-03 stsp goto done;
470 9a1cc63f 2020-02-03 stsp }
471 9a1cc63f 2020-02-03 stsp
472 9a1cc63f 2020-02-03 stsp if (gitconfig_owner) {
473 9a1cc63f 2020-02-03 stsp err = got_privsep_send_gitconfig_owner_req(ibuf);
474 cd95becd 2019-11-29 stsp if (err)
475 cd95becd 2019-11-29 stsp goto done;
476 9a1cc63f 2020-02-03 stsp err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
477 9a1cc63f 2020-02-03 stsp if (err)
478 9a1cc63f 2020-02-03 stsp goto done;
479 cd95becd 2019-11-29 stsp }
480 aba9c984 2019-09-08 stsp
481 aba9c984 2019-09-08 stsp imsg_clear(ibuf);
482 aba9c984 2019-09-08 stsp err = got_privsep_send_stop(imsg_fds[0]);
483 aba9c984 2019-09-08 stsp child_err = got_privsep_wait_for_child(pid);
484 aba9c984 2019-09-08 stsp if (child_err && err == NULL)
485 aba9c984 2019-09-08 stsp err = child_err;
486 aba9c984 2019-09-08 stsp done:
487 aba9c984 2019-09-08 stsp if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
488 aba9c984 2019-09-08 stsp err = got_error_from_errno("close");
489 aba9c984 2019-09-08 stsp if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
490 aba9c984 2019-09-08 stsp err = got_error_from_errno("close");
491 aba9c984 2019-09-08 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
492 aba9c984 2019-09-08 stsp err = got_error_from_errno2("close", gitconfig_path);
493 aba9c984 2019-09-08 stsp free(ibuf);
494 c9956ddf 2019-09-08 stsp return err;
495 c9956ddf 2019-09-08 stsp }
496 c9956ddf 2019-09-08 stsp
497 c9956ddf 2019-09-08 stsp static const struct got_error *
498 c9956ddf 2019-09-08 stsp read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path)
499 c9956ddf 2019-09-08 stsp {
500 c9956ddf 2019-09-08 stsp const struct got_error *err = NULL;
501 c9956ddf 2019-09-08 stsp char *repo_gitconfig_path = NULL;
502 c9956ddf 2019-09-08 stsp
503 c9956ddf 2019-09-08 stsp if (global_gitconfig_path) {
504 c9956ddf 2019-09-08 stsp /* Read settings from ~/.gitconfig. */
505 c9956ddf 2019-09-08 stsp int dummy_repo_version;
506 c9956ddf 2019-09-08 stsp err = parse_gitconfig_file(&dummy_repo_version,
507 c9956ddf 2019-09-08 stsp &repo->global_gitconfig_author_name,
508 c9956ddf 2019-09-08 stsp &repo->global_gitconfig_author_email,
509 9a1cc63f 2020-02-03 stsp NULL, NULL, NULL, global_gitconfig_path);
510 c9956ddf 2019-09-08 stsp if (err)
511 c9956ddf 2019-09-08 stsp return err;
512 c9956ddf 2019-09-08 stsp }
513 c9956ddf 2019-09-08 stsp
514 c9956ddf 2019-09-08 stsp /* Read repository's .git/config file. */
515 c9956ddf 2019-09-08 stsp err = get_path_gitconfig(&repo_gitconfig_path, repo);
516 c9956ddf 2019-09-08 stsp if (err)
517 c9956ddf 2019-09-08 stsp return err;
518 c9956ddf 2019-09-08 stsp
519 c9956ddf 2019-09-08 stsp err = parse_gitconfig_file(&repo->gitconfig_repository_format_version,
520 c9956ddf 2019-09-08 stsp &repo->gitconfig_author_name, &repo->gitconfig_author_email,
521 cd95becd 2019-11-29 stsp &repo->gitconfig_remotes, &repo->ngitconfig_remotes,
522 9a1cc63f 2020-02-03 stsp &repo->gitconfig_owner, repo_gitconfig_path);
523 c9956ddf 2019-09-08 stsp if (err)
524 c9956ddf 2019-09-08 stsp goto done;
525 c9956ddf 2019-09-08 stsp done:
526 c9956ddf 2019-09-08 stsp free(repo_gitconfig_path);
527 85f51bba 2018-07-16 stsp return err;
528 85f51bba 2018-07-16 stsp }
529 85f51bba 2018-07-16 stsp
530 85f51bba 2018-07-16 stsp const struct got_error *
531 c9956ddf 2019-09-08 stsp got_repo_open(struct got_repository **repop, const char *path,
532 c9956ddf 2019-09-08 stsp const char *global_gitconfig_path)
533 85f51bba 2018-07-16 stsp {
534 92af5469 2017-11-05 stsp struct got_repository *repo = NULL;
535 92af5469 2017-11-05 stsp const struct got_error *err = NULL;
536 aba9c984 2019-09-08 stsp char *abspath;
537 ad242220 2018-09-08 stsp int i, tried_root = 0;
538 4027f31a 2017-11-04 stsp
539 85f51bba 2018-07-16 stsp *repop = NULL;
540 85f51bba 2018-07-16 stsp
541 2393f13b 2018-03-09 stsp if (got_path_is_absolute(path))
542 2393f13b 2018-03-09 stsp abspath = strdup(path);
543 2393f13b 2018-03-09 stsp else
544 2393f13b 2018-03-09 stsp abspath = got_path_get_absolute(path);
545 92af5469 2017-11-05 stsp if (abspath == NULL)
546 92af5469 2017-11-05 stsp return got_error(GOT_ERR_BAD_PATH);
547 4027f31a 2017-11-04 stsp
548 4027f31a 2017-11-04 stsp repo = calloc(1, sizeof(*repo));
549 92af5469 2017-11-05 stsp if (repo == NULL) {
550 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
551 92af5469 2017-11-05 stsp goto done;
552 92af5469 2017-11-05 stsp }
553 4027f31a 2017-11-04 stsp
554 ad242220 2018-09-08 stsp for (i = 0; i < nitems(repo->privsep_children); i++) {
555 3516b818 2018-09-08 stsp memset(&repo->privsep_children[i], 0,
556 3516b818 2018-09-08 stsp sizeof(repo->privsep_children[0]));
557 ad242220 2018-09-08 stsp repo->privsep_children[i].imsg_fd = -1;
558 ad242220 2018-09-08 stsp }
559 ad242220 2018-09-08 stsp
560 6bef87be 2018-09-11 stsp err = got_object_cache_init(&repo->objcache,
561 6bef87be 2018-09-11 stsp GOT_OBJECT_CACHE_TYPE_OBJ);
562 6bef87be 2018-09-11 stsp if (err)
563 f6be5c30 2018-06-22 stsp goto done;
564 6bef87be 2018-09-11 stsp err = got_object_cache_init(&repo->treecache,
565 6bef87be 2018-09-11 stsp GOT_OBJECT_CACHE_TYPE_TREE);
566 6bef87be 2018-09-11 stsp if (err)
567 1943de01 2018-06-22 stsp goto done;
568 6bef87be 2018-09-11 stsp err = got_object_cache_init(&repo->commitcache,
569 6bef87be 2018-09-11 stsp GOT_OBJECT_CACHE_TYPE_COMMIT);
570 6bef87be 2018-09-11 stsp if (err)
571 eb77ee11 2018-07-08 stsp goto done;
572 f4a881ce 2018-11-17 stsp err = got_object_cache_init(&repo->tagcache,
573 f4a881ce 2018-11-17 stsp GOT_OBJECT_CACHE_TYPE_TAG);
574 f4a881ce 2018-11-17 stsp if (err)
575 f4a881ce 2018-11-17 stsp goto done;
576 1943de01 2018-06-22 stsp
577 6876e203 2019-07-22 stsp path = realpath(abspath, NULL);
578 6876e203 2019-07-22 stsp if (path == NULL) {
579 62550b13 2019-07-23 stsp err = got_error_from_errno2("realpath", abspath);
580 92af5469 2017-11-05 stsp goto done;
581 92af5469 2017-11-05 stsp }
582 4027f31a 2017-11-04 stsp
583 85f51bba 2018-07-16 stsp do {
584 85f51bba 2018-07-16 stsp err = open_repo(repo, path);
585 85f51bba 2018-07-16 stsp if (err == NULL)
586 85f51bba 2018-07-16 stsp break;
587 85f51bba 2018-07-16 stsp if (err->code != GOT_ERR_NOT_GIT_REPO)
588 85f51bba 2018-07-16 stsp break;
589 85f51bba 2018-07-16 stsp if (path[0] == '/' && path[1] == '\0') {
590 85f51bba 2018-07-16 stsp if (tried_root) {
591 85f51bba 2018-07-16 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
592 f2db9c47 2019-08-24 stsp goto done;
593 442a3ddc 2018-04-23 stsp }
594 85f51bba 2018-07-16 stsp tried_root = 1;
595 442a3ddc 2018-04-23 stsp }
596 85f51bba 2018-07-16 stsp path = dirname(path);
597 f2db9c47 2019-08-24 stsp if (path == NULL) {
598 638f9024 2019-05-13 stsp err = got_error_from_errno2("dirname", path);
599 f2db9c47 2019-08-24 stsp goto done;
600 f2db9c47 2019-08-24 stsp }
601 85f51bba 2018-07-16 stsp } while (path);
602 1d126e2d 2019-08-24 stsp
603 c9956ddf 2019-09-08 stsp err = read_gitconfig(repo, global_gitconfig_path);
604 1d126e2d 2019-08-24 stsp if (err)
605 1d126e2d 2019-08-24 stsp goto done;
606 aba9c984 2019-09-08 stsp if (repo->gitconfig_repository_format_version != 0)
607 aba9c984 2019-09-08 stsp err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT);
608 92af5469 2017-11-05 stsp done:
609 92af5469 2017-11-05 stsp if (err)
610 5c2f5761 2018-09-19 stsp got_repo_close(repo);
611 85f51bba 2018-07-16 stsp else
612 85f51bba 2018-07-16 stsp *repop = repo;
613 92af5469 2017-11-05 stsp free(abspath);
614 92af5469 2017-11-05 stsp return err;
615 4027f31a 2017-11-04 stsp }
616 4027f31a 2017-11-04 stsp
617 ad242220 2018-09-08 stsp const struct got_error *
618 4027f31a 2017-11-04 stsp got_repo_close(struct got_repository *repo)
619 4027f31a 2017-11-04 stsp {
620 ad242220 2018-09-08 stsp const struct got_error *err = NULL, *child_err;
621 79b11c62 2018-03-09 stsp int i;
622 79b11c62 2018-03-09 stsp
623 65cf1e80 2018-03-16 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
624 65cf1e80 2018-03-16 stsp if (repo->packidx_cache[i] == NULL)
625 79b11c62 2018-03-09 stsp break;
626 65cf1e80 2018-03-16 stsp got_packidx_close(repo->packidx_cache[i]);
627 79b11c62 2018-03-09 stsp }
628 bd1223b9 2018-03-14 stsp
629 7e656b93 2018-03-17 stsp for (i = 0; i < nitems(repo->packs); i++) {
630 7e656b93 2018-03-17 stsp if (repo->packs[i].path_packfile == NULL)
631 7e656b93 2018-03-17 stsp break;
632 7e656b93 2018-03-17 stsp got_pack_close(&repo->packs[i]);
633 7e656b93 2018-03-17 stsp }
634 7e656b93 2018-03-17 stsp
635 4027f31a 2017-11-04 stsp free(repo->path);
636 4986b9d5 2018-03-12 stsp free(repo->path_git_dir);
637 cd717821 2018-06-22 stsp
638 6bef87be 2018-09-11 stsp got_object_cache_close(&repo->objcache);
639 6bef87be 2018-09-11 stsp got_object_cache_close(&repo->treecache);
640 6bef87be 2018-09-11 stsp got_object_cache_close(&repo->commitcache);
641 f4a881ce 2018-11-17 stsp got_object_cache_close(&repo->tagcache);
642 ad242220 2018-09-08 stsp
643 ad242220 2018-09-08 stsp for (i = 0; i < nitems(repo->privsep_children); i++) {
644 ad242220 2018-09-08 stsp if (repo->privsep_children[i].imsg_fd == -1)
645 ad242220 2018-09-08 stsp continue;
646 3516b818 2018-09-08 stsp imsg_clear(repo->privsep_children[i].ibuf);
647 3516b818 2018-09-08 stsp free(repo->privsep_children[i].ibuf);
648 ad242220 2018-09-08 stsp err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
649 876c234b 2018-09-10 stsp child_err = got_privsep_wait_for_child(
650 876c234b 2018-09-10 stsp repo->privsep_children[i].pid);
651 ad242220 2018-09-08 stsp if (child_err && err == NULL)
652 ad242220 2018-09-08 stsp err = child_err;
653 3a6ce05a 2019-02-11 stsp if (close(repo->privsep_children[i].imsg_fd) != 0 &&
654 3a6ce05a 2019-02-11 stsp err == NULL)
655 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
656 ad242220 2018-09-08 stsp }
657 aba9c984 2019-09-08 stsp
658 aba9c984 2019-09-08 stsp free(repo->gitconfig_author_name);
659 aba9c984 2019-09-08 stsp free(repo->gitconfig_author_email);
660 cd95becd 2019-11-29 stsp for (i = 0; i < repo->ngitconfig_remotes; i++) {
661 cd95becd 2019-11-29 stsp free(repo->gitconfig_remotes[i].name);
662 cd95becd 2019-11-29 stsp free(repo->gitconfig_remotes[i].url);
663 cd95becd 2019-11-29 stsp }
664 cd95becd 2019-11-29 stsp free(repo->gitconfig_remotes);
665 4027f31a 2017-11-04 stsp free(repo);
666 ad242220 2018-09-08 stsp
667 ad242220 2018-09-08 stsp return err;
668 4027f31a 2017-11-04 stsp }
669 04ca23f4 2018-07-16 stsp
670 04ca23f4 2018-07-16 stsp const struct got_error *
671 04ca23f4 2018-07-16 stsp got_repo_map_path(char **in_repo_path, struct got_repository *repo,
672 23721109 2018-10-22 stsp const char *input_path, int check_disk)
673 04ca23f4 2018-07-16 stsp {
674 04ca23f4 2018-07-16 stsp const struct got_error *err = NULL;
675 7839bc15 2019-01-06 stsp const char *repo_abspath = NULL;
676 e83c0634 2020-01-27 stsp size_t repolen, len;
677 e83c0634 2020-01-27 stsp char *canonpath, *path = NULL;
678 04ca23f4 2018-07-16 stsp
679 04ca23f4 2018-07-16 stsp *in_repo_path = NULL;
680 04ca23f4 2018-07-16 stsp
681 04ca23f4 2018-07-16 stsp canonpath = strdup(input_path);
682 04ca23f4 2018-07-16 stsp if (canonpath == NULL) {
683 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
684 04ca23f4 2018-07-16 stsp goto done;
685 04ca23f4 2018-07-16 stsp }
686 04ca23f4 2018-07-16 stsp err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
687 04ca23f4 2018-07-16 stsp if (err)
688 04ca23f4 2018-07-16 stsp goto done;
689 04ca23f4 2018-07-16 stsp
690 04ca23f4 2018-07-16 stsp repo_abspath = got_repo_get_path(repo);
691 04ca23f4 2018-07-16 stsp
692 2840f715 2019-07-11 stsp if (!check_disk || canonpath[0] == '\0') {
693 23721109 2018-10-22 stsp path = strdup(canonpath);
694 b70703ad 2019-03-18 stsp if (path == NULL) {
695 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
696 04ca23f4 2018-07-16 stsp goto done;
697 04ca23f4 2018-07-16 stsp }
698 04ca23f4 2018-07-16 stsp } else {
699 04ca23f4 2018-07-16 stsp path = realpath(canonpath, NULL);
700 04ca23f4 2018-07-16 stsp if (path == NULL) {
701 b70703ad 2019-03-18 stsp if (errno != ENOENT) {
702 638f9024 2019-05-13 stsp err = got_error_from_errno2("realpath",
703 230a42bd 2019-05-11 jcs canonpath);
704 b70703ad 2019-03-18 stsp goto done;
705 b70703ad 2019-03-18 stsp }
706 b70703ad 2019-03-18 stsp /*
707 b70703ad 2019-03-18 stsp * Path is not on disk.
708 b70703ad 2019-03-18 stsp * Assume it is already relative to repository root.
709 b70703ad 2019-03-18 stsp */
710 b70703ad 2019-03-18 stsp path = strdup(canonpath);
711 b70703ad 2019-03-18 stsp if (path == NULL) {
712 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
713 b70703ad 2019-03-18 stsp goto done;
714 b70703ad 2019-03-18 stsp }
715 04ca23f4 2018-07-16 stsp }
716 04ca23f4 2018-07-16 stsp
717 04ca23f4 2018-07-16 stsp repolen = strlen(repo_abspath);
718 04ca23f4 2018-07-16 stsp len = strlen(path);
719 04ca23f4 2018-07-16 stsp
720 04ca23f4 2018-07-16 stsp
721 04ca23f4 2018-07-16 stsp if (strcmp(path, repo_abspath) == 0) {
722 04ca23f4 2018-07-16 stsp free(path);
723 04ca23f4 2018-07-16 stsp path = strdup("");
724 04ca23f4 2018-07-16 stsp if (path == NULL) {
725 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
726 04ca23f4 2018-07-16 stsp goto done;
727 04ca23f4 2018-07-16 stsp }
728 65aa7d1c 2020-01-28 stsp } else if (len > repolen &&
729 65aa7d1c 2020-01-28 stsp got_path_is_child(path, repo_abspath, repolen)) {
730 04ca23f4 2018-07-16 stsp /* Matched an on-disk path inside repository. */
731 04ca23f4 2018-07-16 stsp if (got_repo_is_bare(repo)) {
732 04ca23f4 2018-07-16 stsp /*
733 04ca23f4 2018-07-16 stsp * Matched an on-disk path inside repository
734 04ca23f4 2018-07-16 stsp * database. Treat as repository-relative.
735 04ca23f4 2018-07-16 stsp */
736 04ca23f4 2018-07-16 stsp } else {
737 04ca23f4 2018-07-16 stsp char *child;
738 04ca23f4 2018-07-16 stsp /* Strip common prefix with repository path. */
739 04ca23f4 2018-07-16 stsp err = got_path_skip_common_ancestor(&child,
740 04ca23f4 2018-07-16 stsp repo_abspath, path);
741 04ca23f4 2018-07-16 stsp if (err)
742 04ca23f4 2018-07-16 stsp goto done;
743 04ca23f4 2018-07-16 stsp free(path);
744 04ca23f4 2018-07-16 stsp path = child;
745 04ca23f4 2018-07-16 stsp }
746 04ca23f4 2018-07-16 stsp } else {
747 04ca23f4 2018-07-16 stsp /*
748 04ca23f4 2018-07-16 stsp * Matched unrelated on-disk path.
749 04ca23f4 2018-07-16 stsp * Treat it as repository-relative.
750 04ca23f4 2018-07-16 stsp */
751 04ca23f4 2018-07-16 stsp }
752 04ca23f4 2018-07-16 stsp }
753 04ca23f4 2018-07-16 stsp
754 04ca23f4 2018-07-16 stsp /* Make in-repository path absolute */
755 04ca23f4 2018-07-16 stsp if (path[0] != '/') {
756 04ca23f4 2018-07-16 stsp char *abspath;
757 04ca23f4 2018-07-16 stsp if (asprintf(&abspath, "/%s", path) == -1) {
758 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
759 04ca23f4 2018-07-16 stsp goto done;
760 04ca23f4 2018-07-16 stsp }
761 04ca23f4 2018-07-16 stsp free(path);
762 04ca23f4 2018-07-16 stsp path = abspath;
763 04ca23f4 2018-07-16 stsp }
764 04ca23f4 2018-07-16 stsp
765 04ca23f4 2018-07-16 stsp done:
766 04ca23f4 2018-07-16 stsp free(canonpath);
767 04ca23f4 2018-07-16 stsp if (err)
768 04ca23f4 2018-07-16 stsp free(path);
769 04ca23f4 2018-07-16 stsp else
770 04ca23f4 2018-07-16 stsp *in_repo_path = path;
771 1510f469 2018-09-09 stsp return err;
772 1510f469 2018-09-09 stsp }
773 1510f469 2018-09-09 stsp
774 e1a68182 2020-01-07 stsp static const struct got_error *
775 e1a68182 2020-01-07 stsp cache_packidx(struct got_repository *repo, struct got_packidx *packidx,
776 e1a68182 2020-01-07 stsp const char *path_packidx)
777 1510f469 2018-09-09 stsp {
778 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
779 1510f469 2018-09-09 stsp int i;
780 1510f469 2018-09-09 stsp
781 1510f469 2018-09-09 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
782 1510f469 2018-09-09 stsp if (repo->packidx_cache[i] == NULL)
783 1510f469 2018-09-09 stsp break;
784 e1a68182 2020-01-07 stsp if (strcmp(repo->packidx_cache[i]->path_packidx,
785 e1a68182 2020-01-07 stsp path_packidx) == 0) {
786 e1a68182 2020-01-07 stsp return got_error(GOT_ERR_CACHE_DUP_ENTRY);
787 e1a68182 2020-01-07 stsp }
788 1510f469 2018-09-09 stsp }
789 1510f469 2018-09-09 stsp if (i == nitems(repo->packidx_cache)) {
790 1510f469 2018-09-09 stsp err = got_packidx_close(repo->packidx_cache[i - 1]);
791 1510f469 2018-09-09 stsp if (err)
792 1510f469 2018-09-09 stsp return err;
793 1510f469 2018-09-09 stsp }
794 1510f469 2018-09-09 stsp
795 15fe583f 2018-11-05 stsp /*
796 15fe583f 2018-11-05 stsp * Insert the new pack index at the front so it will
797 15fe583f 2018-11-05 stsp * be searched first in the future.
798 15fe583f 2018-11-05 stsp */
799 15fe583f 2018-11-05 stsp memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
800 15fe583f 2018-11-05 stsp sizeof(repo->packidx_cache) -
801 15fe583f 2018-11-05 stsp sizeof(repo->packidx_cache[0]));
802 15fe583f 2018-11-05 stsp repo->packidx_cache[0] = packidx;
803 15fe583f 2018-11-05 stsp
804 1510f469 2018-09-09 stsp return NULL;
805 1510f469 2018-09-09 stsp }
806 1510f469 2018-09-09 stsp
807 1510f469 2018-09-09 stsp static int
808 1510f469 2018-09-09 stsp is_packidx_filename(const char *name, size_t len)
809 1510f469 2018-09-09 stsp {
810 1510f469 2018-09-09 stsp if (len != GOT_PACKIDX_NAMELEN)
811 1510f469 2018-09-09 stsp return 0;
812 1510f469 2018-09-09 stsp
813 1510f469 2018-09-09 stsp if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
814 1510f469 2018-09-09 stsp return 0;
815 1510f469 2018-09-09 stsp
816 1510f469 2018-09-09 stsp if (strcmp(name + strlen(GOT_PACK_PREFIX) +
817 1510f469 2018-09-09 stsp SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
818 1510f469 2018-09-09 stsp return 0;
819 1510f469 2018-09-09 stsp
820 1510f469 2018-09-09 stsp return 1;
821 1510f469 2018-09-09 stsp }
822 1510f469 2018-09-09 stsp
823 1510f469 2018-09-09 stsp const struct got_error *
824 1510f469 2018-09-09 stsp got_repo_search_packidx(struct got_packidx **packidx, int *idx,
825 1510f469 2018-09-09 stsp struct got_repository *repo, struct got_object_id *id)
826 1510f469 2018-09-09 stsp {
827 1510f469 2018-09-09 stsp const struct got_error *err;
828 1510f469 2018-09-09 stsp char *path_packdir;
829 1510f469 2018-09-09 stsp DIR *packdir;
830 1510f469 2018-09-09 stsp struct dirent *dent;
831 1510f469 2018-09-09 stsp char *path_packidx;
832 1510f469 2018-09-09 stsp int i;
833 1510f469 2018-09-09 stsp
834 1510f469 2018-09-09 stsp /* Search pack index cache. */
835 1510f469 2018-09-09 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
836 1510f469 2018-09-09 stsp if (repo->packidx_cache[i] == NULL)
837 1510f469 2018-09-09 stsp break;
838 1510f469 2018-09-09 stsp *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
839 1510f469 2018-09-09 stsp if (*idx != -1) {
840 1510f469 2018-09-09 stsp *packidx = repo->packidx_cache[i];
841 87c1ed2b 2020-01-07 stsp /*
842 87c1ed2b 2020-01-07 stsp * Move this cache entry to the front. Repeatedly
843 87c1ed2b 2020-01-07 stsp * searching a wrong pack index can be expensive.
844 87c1ed2b 2020-01-07 stsp */
845 87c1ed2b 2020-01-07 stsp if (i > 0) {
846 87c1ed2b 2020-01-07 stsp struct got_packidx *p;
847 87c1ed2b 2020-01-07 stsp p = repo->packidx_cache[0];
848 87c1ed2b 2020-01-07 stsp repo->packidx_cache[0] = *packidx;
849 87c1ed2b 2020-01-07 stsp repo->packidx_cache[i] = p;
850 87c1ed2b 2020-01-07 stsp }
851 1510f469 2018-09-09 stsp return NULL;
852 1510f469 2018-09-09 stsp }
853 1510f469 2018-09-09 stsp }
854 1510f469 2018-09-09 stsp /* No luck. Search the filesystem. */
855 1510f469 2018-09-09 stsp
856 1510f469 2018-09-09 stsp path_packdir = got_repo_get_path_objects_pack(repo);
857 1510f469 2018-09-09 stsp if (path_packdir == NULL)
858 638f9024 2019-05-13 stsp return got_error_from_errno("got_repo_get_path_objects_pack");
859 1510f469 2018-09-09 stsp
860 1510f469 2018-09-09 stsp packdir = opendir(path_packdir);
861 1510f469 2018-09-09 stsp if (packdir == NULL) {
862 b90deaa1 2019-07-27 stsp if (errno == ENOENT)
863 b90deaa1 2019-07-27 stsp err = got_error_no_obj(id);
864 b90deaa1 2019-07-27 stsp else
865 b90deaa1 2019-07-27 stsp err = got_error_from_errno2("opendir", path_packdir);
866 1510f469 2018-09-09 stsp goto done;
867 1510f469 2018-09-09 stsp }
868 1510f469 2018-09-09 stsp
869 1510f469 2018-09-09 stsp while ((dent = readdir(packdir)) != NULL) {
870 e1a68182 2020-01-07 stsp int is_cached = 0;
871 e1a68182 2020-01-07 stsp
872 1510f469 2018-09-09 stsp if (!is_packidx_filename(dent->d_name, dent->d_namlen))
873 1510f469 2018-09-09 stsp continue;
874 1510f469 2018-09-09 stsp
875 1510f469 2018-09-09 stsp if (asprintf(&path_packidx, "%s/%s", path_packdir,
876 1510f469 2018-09-09 stsp dent->d_name) == -1) {
877 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
878 1510f469 2018-09-09 stsp goto done;
879 e1a68182 2020-01-07 stsp }
880 e1a68182 2020-01-07 stsp
881 e1a68182 2020-01-07 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
882 e1a68182 2020-01-07 stsp if (repo->packidx_cache[i] == NULL)
883 e1a68182 2020-01-07 stsp break;
884 e1a68182 2020-01-07 stsp if (strcmp(repo->packidx_cache[i]->path_packidx,
885 e1a68182 2020-01-07 stsp path_packidx) == 0) {
886 e1a68182 2020-01-07 stsp is_cached = 1;
887 e1a68182 2020-01-07 stsp break;
888 e1a68182 2020-01-07 stsp }
889 1510f469 2018-09-09 stsp }
890 e1a68182 2020-01-07 stsp if (is_cached) {
891 e1a68182 2020-01-07 stsp free(path_packidx);
892 e1a68182 2020-01-07 stsp continue; /* already searched */
893 e1a68182 2020-01-07 stsp }
894 1510f469 2018-09-09 stsp
895 1510f469 2018-09-09 stsp err = got_packidx_open(packidx, path_packidx, 0);
896 e1a68182 2020-01-07 stsp if (err) {
897 e1a68182 2020-01-07 stsp free(path_packidx);
898 e1a68182 2020-01-07 stsp goto done;
899 e1a68182 2020-01-07 stsp }
900 e1a68182 2020-01-07 stsp
901 e1a68182 2020-01-07 stsp err = cache_packidx(repo, *packidx, path_packidx);
902 1510f469 2018-09-09 stsp free(path_packidx);
903 1510f469 2018-09-09 stsp if (err)
904 1510f469 2018-09-09 stsp goto done;
905 1510f469 2018-09-09 stsp
906 1510f469 2018-09-09 stsp *idx = got_packidx_get_object_idx(*packidx, id);
907 1510f469 2018-09-09 stsp if (*idx != -1) {
908 1510f469 2018-09-09 stsp err = NULL; /* found the object */
909 1510f469 2018-09-09 stsp goto done;
910 1510f469 2018-09-09 stsp }
911 1510f469 2018-09-09 stsp }
912 1510f469 2018-09-09 stsp
913 91a3d81f 2018-11-11 stsp err = got_error_no_obj(id);
914 1510f469 2018-09-09 stsp done:
915 1510f469 2018-09-09 stsp free(path_packdir);
916 d69bcdf7 2019-06-28 stsp if (packdir && closedir(packdir) != 0 && err == NULL)
917 638f9024 2019-05-13 stsp err = got_error_from_errno("closedir");
918 04ca23f4 2018-07-16 stsp return err;
919 04ca23f4 2018-07-16 stsp }
920 1510f469 2018-09-09 stsp
921 1510f469 2018-09-09 stsp static const struct got_error *
922 1510f469 2018-09-09 stsp read_packfile_hdr(int fd, struct got_packidx *packidx)
923 1510f469 2018-09-09 stsp {
924 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
925 1510f469 2018-09-09 stsp uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
926 1510f469 2018-09-09 stsp struct got_packfile_hdr hdr;
927 1510f469 2018-09-09 stsp ssize_t n;
928 1510f469 2018-09-09 stsp
929 1510f469 2018-09-09 stsp n = read(fd, &hdr, sizeof(hdr));
930 1510f469 2018-09-09 stsp if (n < 0)
931 638f9024 2019-05-13 stsp return got_error_from_errno("read");
932 1510f469 2018-09-09 stsp if (n != sizeof(hdr))
933 1510f469 2018-09-09 stsp return got_error(GOT_ERR_BAD_PACKFILE);
934 1510f469 2018-09-09 stsp
935 1510f469 2018-09-09 stsp if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
936 1510f469 2018-09-09 stsp betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
937 1510f469 2018-09-09 stsp betoh32(hdr.nobjects) != totobj)
938 1510f469 2018-09-09 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
939 1510f469 2018-09-09 stsp
940 1510f469 2018-09-09 stsp return err;
941 1510f469 2018-09-09 stsp }
942 1510f469 2018-09-09 stsp
943 1510f469 2018-09-09 stsp static const struct got_error *
944 1510f469 2018-09-09 stsp open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
945 1510f469 2018-09-09 stsp {
946 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
947 1510f469 2018-09-09 stsp
948 a5b57ccf 2019-04-11 stsp *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
949 1510f469 2018-09-09 stsp if (*fd == -1)
950 638f9024 2019-05-13 stsp return got_error_from_errno2("open", path_packfile);
951 1510f469 2018-09-09 stsp
952 1510f469 2018-09-09 stsp if (packidx) {
953 1510f469 2018-09-09 stsp err = read_packfile_hdr(*fd, packidx);
954 1510f469 2018-09-09 stsp if (err) {
955 1510f469 2018-09-09 stsp close(*fd);
956 1510f469 2018-09-09 stsp *fd = -1;
957 1510f469 2018-09-09 stsp }
958 1510f469 2018-09-09 stsp }
959 1510f469 2018-09-09 stsp
960 1510f469 2018-09-09 stsp return err;
961 1510f469 2018-09-09 stsp }
962 1510f469 2018-09-09 stsp
963 1510f469 2018-09-09 stsp const struct got_error *
964 1510f469 2018-09-09 stsp got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
965 1510f469 2018-09-09 stsp const char *path_packfile, struct got_packidx *packidx)
966 1510f469 2018-09-09 stsp {
967 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
968 1510f469 2018-09-09 stsp struct got_pack *pack = NULL;
969 ff563a3d 2019-05-23 stsp struct stat sb;
970 1510f469 2018-09-09 stsp int i;
971 1510f469 2018-09-09 stsp
972 1510f469 2018-09-09 stsp if (packp)
973 1510f469 2018-09-09 stsp *packp = NULL;
974 1510f469 2018-09-09 stsp
975 1510f469 2018-09-09 stsp for (i = 0; i < nitems(repo->packs); i++) {
976 1510f469 2018-09-09 stsp pack = &repo->packs[i];
977 1510f469 2018-09-09 stsp if (pack->path_packfile == NULL)
978 1510f469 2018-09-09 stsp break;
979 1510f469 2018-09-09 stsp if (strcmp(pack->path_packfile, path_packfile) == 0)
980 e1a68182 2020-01-07 stsp return got_error(GOT_ERR_CACHE_DUP_ENTRY);
981 1510f469 2018-09-09 stsp }
982 1510f469 2018-09-09 stsp
983 1510f469 2018-09-09 stsp if (i == nitems(repo->packs) - 1) {
984 1510f469 2018-09-09 stsp err = got_pack_close(&repo->packs[i - 1]);
985 1510f469 2018-09-09 stsp if (err)
986 1510f469 2018-09-09 stsp return err;
987 1510f469 2018-09-09 stsp memmove(&repo->packs[1], &repo->packs[0],
988 1510f469 2018-09-09 stsp sizeof(repo->packs) - sizeof(repo->packs[0]));
989 1510f469 2018-09-09 stsp i = 0;
990 1510f469 2018-09-09 stsp }
991 1510f469 2018-09-09 stsp
992 1510f469 2018-09-09 stsp pack = &repo->packs[i];
993 1510f469 2018-09-09 stsp
994 1510f469 2018-09-09 stsp pack->path_packfile = strdup(path_packfile);
995 1510f469 2018-09-09 stsp if (pack->path_packfile == NULL) {
996 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
997 1510f469 2018-09-09 stsp goto done;
998 1510f469 2018-09-09 stsp }
999 1510f469 2018-09-09 stsp
1000 1510f469 2018-09-09 stsp err = open_packfile(&pack->fd, path_packfile, packidx);
1001 1510f469 2018-09-09 stsp if (err)
1002 1510f469 2018-09-09 stsp goto done;
1003 1510f469 2018-09-09 stsp
1004 ff563a3d 2019-05-23 stsp if (fstat(pack->fd, &sb) != 0) {
1005 ff563a3d 2019-05-23 stsp err = got_error_from_errno("fstat");
1006 1510f469 2018-09-09 stsp goto done;
1007 ff563a3d 2019-05-23 stsp }
1008 ff563a3d 2019-05-23 stsp pack->filesize = sb.st_size;
1009 90636195 2018-09-11 stsp
1010 90636195 2018-09-11 stsp pack->privsep_child = NULL;
1011 1510f469 2018-09-09 stsp
1012 1510f469 2018-09-09 stsp #ifndef GOT_PACK_NO_MMAP
1013 1510f469 2018-09-09 stsp pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1014 1510f469 2018-09-09 stsp pack->fd, 0);
1015 3a11398b 2019-02-21 stsp if (pack->map == MAP_FAILED) {
1016 3a11398b 2019-02-21 stsp if (errno != ENOMEM) {
1017 638f9024 2019-05-13 stsp err = got_error_from_errno("mmap");
1018 3a11398b 2019-02-21 stsp goto done;
1019 3a11398b 2019-02-21 stsp }
1020 1510f469 2018-09-09 stsp pack->map = NULL; /* fall back to read(2) */
1021 3a11398b 2019-02-21 stsp }
1022 1510f469 2018-09-09 stsp #endif
1023 1510f469 2018-09-09 stsp done:
1024 1510f469 2018-09-09 stsp if (err) {
1025 1510f469 2018-09-09 stsp if (pack) {
1026 1510f469 2018-09-09 stsp free(pack->path_packfile);
1027 1510f469 2018-09-09 stsp memset(pack, 0, sizeof(*pack));
1028 1510f469 2018-09-09 stsp }
1029 1510f469 2018-09-09 stsp } else if (packp)
1030 1510f469 2018-09-09 stsp *packp = pack;
1031 1510f469 2018-09-09 stsp return err;
1032 1510f469 2018-09-09 stsp }
1033 1510f469 2018-09-09 stsp
1034 1510f469 2018-09-09 stsp struct got_pack *
1035 1510f469 2018-09-09 stsp got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
1036 1510f469 2018-09-09 stsp {
1037 1510f469 2018-09-09 stsp struct got_pack *pack = NULL;
1038 1510f469 2018-09-09 stsp int i;
1039 1510f469 2018-09-09 stsp
1040 1510f469 2018-09-09 stsp for (i = 0; i < nitems(repo->packs); i++) {
1041 1510f469 2018-09-09 stsp pack = &repo->packs[i];
1042 1510f469 2018-09-09 stsp if (pack->path_packfile == NULL)
1043 1510f469 2018-09-09 stsp break;
1044 1510f469 2018-09-09 stsp if (strcmp(pack->path_packfile, path_packfile) == 0)
1045 1510f469 2018-09-09 stsp return pack;
1046 2c7829a4 2019-06-17 stsp }
1047 2c7829a4 2019-06-17 stsp
1048 2c7829a4 2019-06-17 stsp return NULL;
1049 2c7829a4 2019-06-17 stsp }
1050 2c7829a4 2019-06-17 stsp
1051 2c7829a4 2019-06-17 stsp const struct got_error *
1052 2c7829a4 2019-06-17 stsp got_repo_init(const char *repo_path)
1053 2c7829a4 2019-06-17 stsp {
1054 2c7829a4 2019-06-17 stsp const struct got_error *err = NULL;
1055 2c7829a4 2019-06-17 stsp const char *dirnames[] = {
1056 2c7829a4 2019-06-17 stsp GOT_OBJECTS_DIR,
1057 2c7829a4 2019-06-17 stsp GOT_OBJECTS_PACK_DIR,
1058 2c7829a4 2019-06-17 stsp GOT_REFS_DIR,
1059 2c7829a4 2019-06-17 stsp };
1060 2c7829a4 2019-06-17 stsp const char *description_str = "Unnamed repository; "
1061 2c7829a4 2019-06-17 stsp "edit this file 'description' to name the repository.";
1062 5d67f40d 2019-11-08 stsp const char *headref_str = "ref: refs/heads/main";
1063 2c7829a4 2019-06-17 stsp const char *gitconfig_str = "[core]\n"
1064 2c7829a4 2019-06-17 stsp "\trepositoryformatversion = 0\n"
1065 2c7829a4 2019-06-17 stsp "\tfilemode = true\n"
1066 2c7829a4 2019-06-17 stsp "\tbare = true\n";
1067 2c7829a4 2019-06-17 stsp char *path;
1068 2c7829a4 2019-06-17 stsp int i;
1069 2c7829a4 2019-06-17 stsp
1070 2c7829a4 2019-06-17 stsp if (!got_path_dir_is_empty(repo_path))
1071 2c7829a4 2019-06-17 stsp return got_error(GOT_ERR_DIR_NOT_EMPTY);
1072 2c7829a4 2019-06-17 stsp
1073 2c7829a4 2019-06-17 stsp for (i = 0; i < nitems(dirnames); i++) {
1074 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
1075 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
1076 2c7829a4 2019-06-17 stsp }
1077 2c7829a4 2019-06-17 stsp err = got_path_mkdir(path);
1078 2c7829a4 2019-06-17 stsp free(path);
1079 2c7829a4 2019-06-17 stsp if (err)
1080 2c7829a4 2019-06-17 stsp return err;
1081 1510f469 2018-09-09 stsp }
1082 1510f469 2018-09-09 stsp
1083 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
1084 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
1085 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, description_str);
1086 2c7829a4 2019-06-17 stsp free(path);
1087 2c7829a4 2019-06-17 stsp if (err)
1088 2c7829a4 2019-06-17 stsp return err;
1089 2c7829a4 2019-06-17 stsp
1090 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
1091 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
1092 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, headref_str);
1093 2c7829a4 2019-06-17 stsp free(path);
1094 2c7829a4 2019-06-17 stsp if (err)
1095 2c7829a4 2019-06-17 stsp return err;
1096 2c7829a4 2019-06-17 stsp
1097 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
1098 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
1099 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, gitconfig_str);
1100 2c7829a4 2019-06-17 stsp free(path);
1101 2c7829a4 2019-06-17 stsp if (err)
1102 2c7829a4 2019-06-17 stsp return err;
1103 2c7829a4 2019-06-17 stsp
1104 1510f469 2018-09-09 stsp return NULL;
1105 e09a504c 2019-06-28 stsp }
1106 e09a504c 2019-06-28 stsp
1107 e09a504c 2019-06-28 stsp static const struct got_error *
1108 4277420a 2019-06-29 stsp match_packed_object(struct got_object_id **unique_id,
1109 dd88155e 2019-06-29 stsp struct got_repository *repo, const char *id_str_prefix, int obj_type)
1110 e09a504c 2019-06-28 stsp {
1111 e09a504c 2019-06-28 stsp const struct got_error *err = NULL;
1112 e09a504c 2019-06-28 stsp char *path_packdir;
1113 e09a504c 2019-06-28 stsp DIR *packdir;
1114 e09a504c 2019-06-28 stsp struct dirent *dent;
1115 e09a504c 2019-06-28 stsp char *path_packidx;
1116 dd88155e 2019-06-29 stsp struct got_object_id_queue matched_ids;
1117 e09a504c 2019-06-28 stsp
1118 dd88155e 2019-06-29 stsp SIMPLEQ_INIT(&matched_ids);
1119 dd88155e 2019-06-29 stsp
1120 e09a504c 2019-06-28 stsp path_packdir = got_repo_get_path_objects_pack(repo);
1121 e09a504c 2019-06-28 stsp if (path_packdir == NULL)
1122 e09a504c 2019-06-28 stsp return got_error_from_errno("got_repo_get_path_objects_pack");
1123 e09a504c 2019-06-28 stsp
1124 e09a504c 2019-06-28 stsp packdir = opendir(path_packdir);
1125 e09a504c 2019-06-28 stsp if (packdir == NULL) {
1126 e4167f30 2019-07-27 stsp if (errno != ENOENT)
1127 e4167f30 2019-07-27 stsp err = got_error_from_errno2("opendir", path_packdir);
1128 e09a504c 2019-06-28 stsp goto done;
1129 e09a504c 2019-06-28 stsp }
1130 e09a504c 2019-06-28 stsp
1131 e09a504c 2019-06-28 stsp while ((dent = readdir(packdir)) != NULL) {
1132 e09a504c 2019-06-28 stsp struct got_packidx *packidx;
1133 dd88155e 2019-06-29 stsp struct got_object_qid *qid;
1134 dd88155e 2019-06-29 stsp
1135 e09a504c 2019-06-28 stsp
1136 e09a504c 2019-06-28 stsp if (!is_packidx_filename(dent->d_name, dent->d_namlen))
1137 e09a504c 2019-06-28 stsp continue;
1138 e09a504c 2019-06-28 stsp
1139 e09a504c 2019-06-28 stsp if (asprintf(&path_packidx, "%s/%s", path_packdir,
1140 e09a504c 2019-06-28 stsp dent->d_name) == -1) {
1141 e09a504c 2019-06-28 stsp err = got_error_from_errno("asprintf");
1142 4277420a 2019-06-29 stsp break;
1143 e09a504c 2019-06-28 stsp }
1144 e09a504c 2019-06-28 stsp
1145 e09a504c 2019-06-28 stsp err = got_packidx_open(&packidx, path_packidx, 0);
1146 e09a504c 2019-06-28 stsp free(path_packidx);
1147 e09a504c 2019-06-28 stsp if (err)
1148 4277420a 2019-06-29 stsp break;
1149 e09a504c 2019-06-28 stsp
1150 dd88155e 2019-06-29 stsp err = got_packidx_match_id_str_prefix(&matched_ids,
1151 4277420a 2019-06-29 stsp packidx, id_str_prefix);
1152 4277420a 2019-06-29 stsp if (err) {
1153 4277420a 2019-06-29 stsp got_packidx_close(packidx);
1154 4277420a 2019-06-29 stsp break;
1155 4277420a 2019-06-29 stsp }
1156 e09a504c 2019-06-28 stsp err = got_packidx_close(packidx);
1157 dd88155e 2019-06-29 stsp if (err)
1158 e09a504c 2019-06-28 stsp break;
1159 e09a504c 2019-06-28 stsp
1160 dd88155e 2019-06-29 stsp SIMPLEQ_FOREACH(qid, &matched_ids, entry) {
1161 dd88155e 2019-06-29 stsp if (obj_type != GOT_OBJ_TYPE_ANY) {
1162 dd88155e 2019-06-29 stsp int matched_type;
1163 dd88155e 2019-06-29 stsp err = got_object_get_type(&matched_type, repo,
1164 dd88155e 2019-06-29 stsp qid->id);
1165 dd88155e 2019-06-29 stsp if (err)
1166 dd88155e 2019-06-29 stsp goto done;
1167 dd88155e 2019-06-29 stsp if (matched_type != obj_type)
1168 dd88155e 2019-06-29 stsp continue;
1169 dd88155e 2019-06-29 stsp }
1170 4277420a 2019-06-29 stsp if (*unique_id == NULL) {
1171 dd88155e 2019-06-29 stsp *unique_id = got_object_id_dup(qid->id);
1172 dd88155e 2019-06-29 stsp if (*unique_id == NULL) {
1173 dd88155e 2019-06-29 stsp err = got_error_from_errno("malloc");
1174 dd88155e 2019-06-29 stsp goto done;
1175 dd88155e 2019-06-29 stsp }
1176 4277420a 2019-06-29 stsp } else {
1177 1accf02b 2020-01-05 stsp if (got_object_id_cmp(*unique_id, qid->id) == 0)
1178 1accf02b 2020-01-05 stsp continue; /* packed multiple times */
1179 e09a504c 2019-06-28 stsp err = got_error(GOT_ERR_AMBIGUOUS_ID);
1180 561c3678 2019-07-02 stsp goto done;
1181 e09a504c 2019-06-28 stsp }
1182 e09a504c 2019-06-28 stsp }
1183 e09a504c 2019-06-28 stsp }
1184 e09a504c 2019-06-28 stsp done:
1185 dd88155e 2019-06-29 stsp got_object_id_queue_free(&matched_ids);
1186 e09a504c 2019-06-28 stsp free(path_packdir);
1187 e09a504c 2019-06-28 stsp if (packdir && closedir(packdir) != 0 && err == NULL)
1188 e09a504c 2019-06-28 stsp err = got_error_from_errno("closedir");
1189 e09a504c 2019-06-28 stsp if (err) {
1190 e09a504c 2019-06-28 stsp free(*unique_id);
1191 e09a504c 2019-06-28 stsp *unique_id = NULL;
1192 e09a504c 2019-06-28 stsp }
1193 e09a504c 2019-06-28 stsp return err;
1194 e09a504c 2019-06-28 stsp }
1195 e09a504c 2019-06-28 stsp
1196 e09a504c 2019-06-28 stsp static const struct got_error *
1197 4277420a 2019-06-29 stsp match_loose_object(struct got_object_id **unique_id, const char *path_objects,
1198 dd88155e 2019-06-29 stsp const char *object_dir, const char *id_str_prefix, int obj_type,
1199 e09a504c 2019-06-28 stsp struct got_repository *repo)
1200 e09a504c 2019-06-28 stsp {
1201 e09a504c 2019-06-28 stsp const struct got_error *err = NULL;
1202 e09a504c 2019-06-28 stsp char *path;
1203 e09a504c 2019-06-28 stsp DIR *dir = NULL;
1204 e09a504c 2019-06-28 stsp struct dirent *dent;
1205 e09a504c 2019-06-28 stsp struct got_object_id id;
1206 e09a504c 2019-06-28 stsp
1207 e09a504c 2019-06-28 stsp if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
1208 e09a504c 2019-06-28 stsp err = got_error_from_errno("asprintf");
1209 e09a504c 2019-06-28 stsp goto done;
1210 e09a504c 2019-06-28 stsp }
1211 e09a504c 2019-06-28 stsp
1212 e09a504c 2019-06-28 stsp dir = opendir(path);
1213 e09a504c 2019-06-28 stsp if (dir == NULL) {
1214 4277420a 2019-06-29 stsp if (errno == ENOENT) {
1215 4277420a 2019-06-29 stsp err = NULL;
1216 4277420a 2019-06-29 stsp goto done;
1217 4277420a 2019-06-29 stsp }
1218 e09a504c 2019-06-28 stsp err = got_error_from_errno2("opendir", path);
1219 e09a504c 2019-06-28 stsp goto done;
1220 e09a504c 2019-06-28 stsp }
1221 e09a504c 2019-06-28 stsp while ((dent = readdir(dir)) != NULL) {
1222 e09a504c 2019-06-28 stsp char *id_str;
1223 5903ff6e 2019-06-29 stsp int cmp;
1224 5903ff6e 2019-06-29 stsp
1225 e09a504c 2019-06-28 stsp if (strcmp(dent->d_name, ".") == 0 ||
1226 e09a504c 2019-06-28 stsp strcmp(dent->d_name, "..") == 0)
1227 e09a504c 2019-06-28 stsp continue;
1228 e09a504c 2019-06-28 stsp
1229 e09a504c 2019-06-28 stsp if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1230 e09a504c 2019-06-28 stsp err = got_error_from_errno("asprintf");
1231 e09a504c 2019-06-28 stsp goto done;
1232 e09a504c 2019-06-28 stsp }
1233 e09a504c 2019-06-28 stsp
1234 e09a504c 2019-06-28 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
1235 e09a504c 2019-06-28 stsp continue;
1236 e09a504c 2019-06-28 stsp
1237 52d1d0d9 2019-07-07 stsp /*
1238 52d1d0d9 2019-07-07 stsp * Directory entries do not necessarily appear in
1239 52d1d0d9 2019-07-07 stsp * sorted order, so we must iterate over all of them.
1240 52d1d0d9 2019-07-07 stsp */
1241 5903ff6e 2019-06-29 stsp cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1242 52d1d0d9 2019-07-07 stsp if (cmp != 0) {
1243 e09a504c 2019-06-28 stsp free(id_str);
1244 e09a504c 2019-06-28 stsp continue;
1245 e09a504c 2019-06-28 stsp }
1246 e09a504c 2019-06-28 stsp
1247 e09a504c 2019-06-28 stsp if (*unique_id == NULL) {
1248 dd88155e 2019-06-29 stsp if (obj_type != GOT_OBJ_TYPE_ANY) {
1249 dd88155e 2019-06-29 stsp int matched_type;
1250 dd88155e 2019-06-29 stsp err = got_object_get_type(&matched_type, repo,
1251 dd88155e 2019-06-29 stsp &id);
1252 dd88155e 2019-06-29 stsp if (err)
1253 dd88155e 2019-06-29 stsp goto done;
1254 dd88155e 2019-06-29 stsp if (matched_type != obj_type)
1255 dd88155e 2019-06-29 stsp continue;
1256 dd88155e 2019-06-29 stsp }
1257 e09a504c 2019-06-28 stsp *unique_id = got_object_id_dup(&id);
1258 e09a504c 2019-06-28 stsp if (*unique_id == NULL) {
1259 e09a504c 2019-06-28 stsp err = got_error_from_errno("got_object_id_dup");
1260 e09a504c 2019-06-28 stsp free(id_str);
1261 e09a504c 2019-06-28 stsp goto done;
1262 e09a504c 2019-06-28 stsp }
1263 e09a504c 2019-06-28 stsp } else {
1264 1accf02b 2020-01-05 stsp if (got_object_id_cmp(*unique_id, &id) == 0)
1265 1accf02b 2020-01-05 stsp continue; /* both packed and loose */
1266 e09a504c 2019-06-28 stsp err = got_error(GOT_ERR_AMBIGUOUS_ID);
1267 e09a504c 2019-06-28 stsp free(id_str);
1268 e09a504c 2019-06-28 stsp goto done;
1269 e09a504c 2019-06-28 stsp }
1270 e09a504c 2019-06-28 stsp }
1271 e09a504c 2019-06-28 stsp done:
1272 b2df341b 2019-06-29 stsp if (dir && closedir(dir) != 0 && err == NULL)
1273 b2df341b 2019-06-29 stsp err = got_error_from_errno("closedir");
1274 e09a504c 2019-06-28 stsp if (err) {
1275 e09a504c 2019-06-28 stsp free(*unique_id);
1276 e09a504c 2019-06-28 stsp *unique_id = NULL;
1277 e09a504c 2019-06-28 stsp }
1278 e09a504c 2019-06-28 stsp free(path);
1279 e09a504c 2019-06-28 stsp return err;
1280 1510f469 2018-09-09 stsp }
1281 e09a504c 2019-06-28 stsp
1282 e09a504c 2019-06-28 stsp const struct got_error *
1283 4277420a 2019-06-29 stsp got_repo_match_object_id_prefix(struct got_object_id **id,
1284 dd88155e 2019-06-29 stsp const char *id_str_prefix, int obj_type, struct got_repository *repo)
1285 e09a504c 2019-06-28 stsp {
1286 e09a504c 2019-06-28 stsp const struct got_error *err = NULL;
1287 e09a504c 2019-06-28 stsp char *path_objects = got_repo_get_path_objects(repo);
1288 e09a504c 2019-06-28 stsp char *object_dir = NULL;
1289 e09a504c 2019-06-28 stsp size_t len;
1290 4277420a 2019-06-29 stsp int i;
1291 e09a504c 2019-06-28 stsp
1292 4277420a 2019-06-29 stsp *id = NULL;
1293 4277420a 2019-06-29 stsp
1294 4277420a 2019-06-29 stsp for (i = 0; i < strlen(id_str_prefix); i++) {
1295 4277420a 2019-06-29 stsp if (isxdigit((unsigned char)id_str_prefix[i]))
1296 4277420a 2019-06-29 stsp continue;
1297 6dd1ece6 2019-11-10 stsp return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1298 4277420a 2019-06-29 stsp }
1299 4277420a 2019-06-29 stsp
1300 e09a504c 2019-06-28 stsp len = strlen(id_str_prefix);
1301 e09a504c 2019-06-28 stsp if (len >= 2) {
1302 dd88155e 2019-06-29 stsp err = match_packed_object(id, repo, id_str_prefix, obj_type);
1303 4277420a 2019-06-29 stsp if (err)
1304 83c8b3b8 2019-06-29 stsp goto done;
1305 e09a504c 2019-06-28 stsp object_dir = strndup(id_str_prefix, 2);
1306 83c8b3b8 2019-06-29 stsp if (object_dir == NULL) {
1307 83c8b3b8 2019-06-29 stsp err = got_error_from_errno("strdup");
1308 83c8b3b8 2019-06-29 stsp goto done;
1309 83c8b3b8 2019-06-29 stsp }
1310 4277420a 2019-06-29 stsp err = match_loose_object(id, path_objects, object_dir,
1311 dd88155e 2019-06-29 stsp id_str_prefix, obj_type, repo);
1312 e09a504c 2019-06-28 stsp } else if (len == 1) {
1313 e09a504c 2019-06-28 stsp int i;
1314 e09a504c 2019-06-28 stsp for (i = 0; i < 0xf; i++) {
1315 e09a504c 2019-06-28 stsp if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1316 83c8b3b8 2019-06-29 stsp == -1) {
1317 83c8b3b8 2019-06-29 stsp err = got_error_from_errno("asprintf");
1318 83c8b3b8 2019-06-29 stsp goto done;
1319 83c8b3b8 2019-06-29 stsp }
1320 dd88155e 2019-06-29 stsp err = match_packed_object(id, repo, object_dir,
1321 dd88155e 2019-06-29 stsp obj_type);
1322 4277420a 2019-06-29 stsp if (err)
1323 83c8b3b8 2019-06-29 stsp goto done;
1324 4277420a 2019-06-29 stsp err = match_loose_object(id, path_objects, object_dir,
1325 dd88155e 2019-06-29 stsp id_str_prefix, obj_type, repo);
1326 e09a504c 2019-06-28 stsp if (err)
1327 83c8b3b8 2019-06-29 stsp goto done;
1328 e09a504c 2019-06-28 stsp }
1329 83c8b3b8 2019-06-29 stsp } else {
1330 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
1331 83c8b3b8 2019-06-29 stsp goto done;
1332 83c8b3b8 2019-06-29 stsp }
1333 83c8b3b8 2019-06-29 stsp done:
1334 e09a504c 2019-06-28 stsp free(object_dir);
1335 4277420a 2019-06-29 stsp if (err) {
1336 4277420a 2019-06-29 stsp free(*id);
1337 4277420a 2019-06-29 stsp *id = NULL;
1338 4277420a 2019-06-29 stsp } else if (*id == NULL)
1339 4277420a 2019-06-29 stsp err = got_error(GOT_ERR_NO_OBJ);
1340 303e2782 2019-08-09 stsp
1341 303e2782 2019-08-09 stsp return err;
1342 303e2782 2019-08-09 stsp }
1343 303e2782 2019-08-09 stsp
1344 303e2782 2019-08-09 stsp const struct got_error *
1345 71a27632 2020-01-15 stsp got_repo_match_object_id(struct got_object_id **id, char **label,
1346 71a27632 2020-01-15 stsp const char *id_str, int obj_type, int resolve_tags,
1347 71a27632 2020-01-15 stsp struct got_repository *repo)
1348 71a27632 2020-01-15 stsp {
1349 71a27632 2020-01-15 stsp const struct got_error *err;
1350 71a27632 2020-01-15 stsp struct got_tag_object *tag;
1351 71a27632 2020-01-15 stsp struct got_reference *ref = NULL;
1352 71a27632 2020-01-15 stsp
1353 71a27632 2020-01-15 stsp *id = NULL;
1354 71a27632 2020-01-15 stsp if (label)
1355 71a27632 2020-01-15 stsp *label = NULL;
1356 71a27632 2020-01-15 stsp
1357 71a27632 2020-01-15 stsp if (resolve_tags) {
1358 71a27632 2020-01-15 stsp err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
1359 71a27632 2020-01-15 stsp repo);
1360 71a27632 2020-01-15 stsp if (err == NULL) {
1361 71a27632 2020-01-15 stsp *id = got_object_id_dup(
1362 71a27632 2020-01-15 stsp got_object_tag_get_object_id(tag));
1363 71a27632 2020-01-15 stsp if (*id == NULL)
1364 71a27632 2020-01-15 stsp err = got_error_from_errno("got_object_id_dup");
1365 71a27632 2020-01-15 stsp else if (label && asprintf(label, "refs/tags/%s",
1366 71a27632 2020-01-15 stsp got_object_tag_get_name(tag)) == -1) {
1367 71a27632 2020-01-15 stsp err = got_error_from_errno("asprintf");
1368 71a27632 2020-01-15 stsp free(*id);
1369 71a27632 2020-01-15 stsp *id = NULL;
1370 71a27632 2020-01-15 stsp }
1371 71a27632 2020-01-15 stsp got_object_tag_close(tag);
1372 71a27632 2020-01-15 stsp return err;
1373 71a27632 2020-01-15 stsp } else if (err->code != GOT_ERR_OBJ_TYPE &&
1374 71a27632 2020-01-15 stsp err->code != GOT_ERR_NO_OBJ)
1375 71a27632 2020-01-15 stsp return err;
1376 71a27632 2020-01-15 stsp }
1377 71a27632 2020-01-15 stsp
1378 71a27632 2020-01-15 stsp err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1379 71a27632 2020-01-15 stsp if (err) {
1380 71a27632 2020-01-15 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1381 71a27632 2020-01-15 stsp return err;
1382 71a27632 2020-01-15 stsp err = got_ref_open(&ref, repo, id_str, 0);
1383 71a27632 2020-01-15 stsp if (err != NULL)
1384 71a27632 2020-01-15 stsp goto done;
1385 71a27632 2020-01-15 stsp if (label) {
1386 71a27632 2020-01-15 stsp *label = strdup(got_ref_get_name(ref));
1387 71a27632 2020-01-15 stsp if (*label == NULL) {
1388 71a27632 2020-01-15 stsp err = got_error_from_errno("strdup");
1389 71a27632 2020-01-15 stsp goto done;
1390 71a27632 2020-01-15 stsp }
1391 71a27632 2020-01-15 stsp }
1392 71a27632 2020-01-15 stsp err = got_ref_resolve(id, repo, ref);
1393 71a27632 2020-01-15 stsp } else if (label) {
1394 71a27632 2020-01-15 stsp err = got_object_id_str(label, *id);
1395 71a27632 2020-01-15 stsp if (*label == NULL) {
1396 71a27632 2020-01-15 stsp err = got_error_from_errno("strdup");
1397 71a27632 2020-01-15 stsp goto done;
1398 71a27632 2020-01-15 stsp }
1399 71a27632 2020-01-15 stsp }
1400 71a27632 2020-01-15 stsp done:
1401 71a27632 2020-01-15 stsp if (ref)
1402 71a27632 2020-01-15 stsp got_ref_close(ref);
1403 71a27632 2020-01-15 stsp return err;
1404 71a27632 2020-01-15 stsp }
1405 71a27632 2020-01-15 stsp
1406 71a27632 2020-01-15 stsp const struct got_error *
1407 303e2782 2019-08-09 stsp got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
1408 303e2782 2019-08-09 stsp int obj_type, struct got_repository *repo)
1409 303e2782 2019-08-09 stsp {
1410 303e2782 2019-08-09 stsp const struct got_error *err;
1411 303e2782 2019-08-09 stsp struct got_reflist_head refs;
1412 303e2782 2019-08-09 stsp struct got_reflist_entry *re;
1413 303e2782 2019-08-09 stsp struct got_object_id *tag_id;
1414 303e2782 2019-08-09 stsp
1415 303e2782 2019-08-09 stsp SIMPLEQ_INIT(&refs);
1416 303e2782 2019-08-09 stsp *tag = NULL;
1417 303e2782 2019-08-09 stsp
1418 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_by_name, NULL);
1419 303e2782 2019-08-09 stsp if (err)
1420 303e2782 2019-08-09 stsp return err;
1421 303e2782 2019-08-09 stsp
1422 303e2782 2019-08-09 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
1423 303e2782 2019-08-09 stsp const char *refname;
1424 303e2782 2019-08-09 stsp refname = got_ref_get_name(re->ref);
1425 29606af7 2019-08-23 stsp if (got_ref_is_symbolic(re->ref))
1426 303e2782 2019-08-09 stsp continue;
1427 29606af7 2019-08-23 stsp refname += strlen("refs/tags/");
1428 303e2782 2019-08-09 stsp if (strcmp(refname, name) != 0)
1429 303e2782 2019-08-09 stsp continue;
1430 303e2782 2019-08-09 stsp err = got_ref_resolve(&tag_id, repo, re->ref);
1431 303e2782 2019-08-09 stsp if (err)
1432 303e2782 2019-08-09 stsp break;
1433 303e2782 2019-08-09 stsp err = got_object_open_as_tag(tag, repo, tag_id);
1434 303e2782 2019-08-09 stsp free(tag_id);
1435 303e2782 2019-08-09 stsp if (err)
1436 303e2782 2019-08-09 stsp break;
1437 d24820bf 2019-08-11 stsp if (obj_type == GOT_OBJ_TYPE_ANY ||
1438 d24820bf 2019-08-11 stsp got_object_tag_get_object_type(*tag) == obj_type)
1439 303e2782 2019-08-09 stsp break;
1440 303e2782 2019-08-09 stsp got_object_tag_close(*tag);
1441 303e2782 2019-08-09 stsp *tag = NULL;
1442 303e2782 2019-08-09 stsp }
1443 4277420a 2019-06-29 stsp
1444 303e2782 2019-08-09 stsp got_ref_list_free(&refs);
1445 303e2782 2019-08-09 stsp if (err == NULL && *tag == NULL)
1446 303e2782 2019-08-09 stsp err = got_error(GOT_ERR_NO_OBJ);
1447 e09a504c 2019-06-28 stsp return err;
1448 e09a504c 2019-06-28 stsp }
1449 7a1d6b72 2020-01-15 stsp
1450 3ce1b845 2019-07-15 stsp static const struct got_error *
1451 3ce1b845 2019-07-15 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1452 3ce1b845 2019-07-15 stsp const char *name, mode_t mode, struct got_object_id *blob_id)
1453 3ce1b845 2019-07-15 stsp {
1454 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
1455 3ce1b845 2019-07-15 stsp
1456 3ce1b845 2019-07-15 stsp *new_te = NULL;
1457 3ce1b845 2019-07-15 stsp
1458 3ce1b845 2019-07-15 stsp *new_te = calloc(1, sizeof(**new_te));
1459 3ce1b845 2019-07-15 stsp if (*new_te == NULL)
1460 3ce1b845 2019-07-15 stsp return got_error_from_errno("calloc");
1461 3ce1b845 2019-07-15 stsp
1462 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >=
1463 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
1464 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
1465 3ce1b845 2019-07-15 stsp goto done;
1466 3ce1b845 2019-07-15 stsp }
1467 3ce1b845 2019-07-15 stsp
1468 3ce1b845 2019-07-15 stsp (*new_te)->mode = S_IFREG | (mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
1469 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
1470 3ce1b845 2019-07-15 stsp done:
1471 3ce1b845 2019-07-15 stsp if (err && *new_te) {
1472 56e0773d 2019-11-28 stsp free(*new_te);
1473 3ce1b845 2019-07-15 stsp *new_te = NULL;
1474 3ce1b845 2019-07-15 stsp }
1475 3ce1b845 2019-07-15 stsp return err;
1476 3ce1b845 2019-07-15 stsp }
1477 3ce1b845 2019-07-15 stsp
1478 3ce1b845 2019-07-15 stsp static const struct got_error *
1479 3ce1b845 2019-07-15 stsp import_file(struct got_tree_entry **new_te, struct dirent *de,
1480 3ce1b845 2019-07-15 stsp const char *path, struct got_repository *repo)
1481 3ce1b845 2019-07-15 stsp {
1482 3ce1b845 2019-07-15 stsp const struct got_error *err;
1483 3ce1b845 2019-07-15 stsp struct got_object_id *blob_id = NULL;
1484 3ce1b845 2019-07-15 stsp char *filepath;
1485 3ce1b845 2019-07-15 stsp struct stat sb;
1486 3ce1b845 2019-07-15 stsp
1487 3ce1b845 2019-07-15 stsp if (asprintf(&filepath, "%s%s%s", path,
1488 3ce1b845 2019-07-15 stsp path[0] == '\0' ? "" : "/", de->d_name) == -1)
1489 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
1490 3ce1b845 2019-07-15 stsp
1491 3ce1b845 2019-07-15 stsp if (lstat(filepath, &sb) != 0) {
1492 3ce1b845 2019-07-15 stsp err = got_error_from_errno2("lstat", path);
1493 3ce1b845 2019-07-15 stsp goto done;
1494 3ce1b845 2019-07-15 stsp }
1495 3ce1b845 2019-07-15 stsp
1496 3ce1b845 2019-07-15 stsp err = got_object_blob_create(&blob_id, filepath, repo);
1497 3ce1b845 2019-07-15 stsp if (err)
1498 3ce1b845 2019-07-15 stsp goto done;
1499 3ce1b845 2019-07-15 stsp
1500 3ce1b845 2019-07-15 stsp err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1501 3ce1b845 2019-07-15 stsp blob_id);
1502 3ce1b845 2019-07-15 stsp done:
1503 3ce1b845 2019-07-15 stsp free(filepath);
1504 3ce1b845 2019-07-15 stsp if (err)
1505 3ce1b845 2019-07-15 stsp free(blob_id);
1506 3ce1b845 2019-07-15 stsp return err;
1507 3ce1b845 2019-07-15 stsp }
1508 3ce1b845 2019-07-15 stsp
1509 3ce1b845 2019-07-15 stsp static const struct got_error *
1510 3ce1b845 2019-07-15 stsp insert_tree_entry(struct got_tree_entry *new_te,
1511 3ce1b845 2019-07-15 stsp struct got_pathlist_head *paths)
1512 3ce1b845 2019-07-15 stsp {
1513 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
1514 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *new_pe;
1515 3ce1b845 2019-07-15 stsp
1516 3ce1b845 2019-07-15 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1517 3ce1b845 2019-07-15 stsp if (err)
1518 3ce1b845 2019-07-15 stsp return err;
1519 3ce1b845 2019-07-15 stsp if (new_pe == NULL)
1520 3ce1b845 2019-07-15 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
1521 3ce1b845 2019-07-15 stsp return NULL;
1522 3ce1b845 2019-07-15 stsp }
1523 3ce1b845 2019-07-15 stsp
1524 3ce1b845 2019-07-15 stsp static const struct got_error *write_tree(struct got_object_id **,
1525 3ce1b845 2019-07-15 stsp const char *, struct got_pathlist_head *, struct got_repository *,
1526 3ce1b845 2019-07-15 stsp got_repo_import_cb progress_cb, void *progress_arg);
1527 3ce1b845 2019-07-15 stsp
1528 3ce1b845 2019-07-15 stsp static const struct got_error *
1529 3ce1b845 2019-07-15 stsp import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1530 3ce1b845 2019-07-15 stsp const char *path, struct got_pathlist_head *ignores,
1531 3ce1b845 2019-07-15 stsp struct got_repository *repo,
1532 3ce1b845 2019-07-15 stsp got_repo_import_cb progress_cb, void *progress_arg)
1533 3ce1b845 2019-07-15 stsp {
1534 3ce1b845 2019-07-15 stsp const struct got_error *err;
1535 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
1536 3ce1b845 2019-07-15 stsp char *subdirpath;
1537 3ce1b845 2019-07-15 stsp
1538 3ce1b845 2019-07-15 stsp if (asprintf(&subdirpath, "%s%s%s", path,
1539 3ce1b845 2019-07-15 stsp path[0] == '\0' ? "" : "/", de->d_name) == -1)
1540 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
1541 3ce1b845 2019-07-15 stsp
1542 3ce1b845 2019-07-15 stsp (*new_te) = calloc(1, sizeof(**new_te));
1543 d6fca0ba 2019-09-15 hiltjo if (*new_te == NULL)
1544 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
1545 3ce1b845 2019-07-15 stsp (*new_te)->mode = S_IFDIR;
1546 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >=
1547 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
1548 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
1549 3ce1b845 2019-07-15 stsp goto done;
1550 3ce1b845 2019-07-15 stsp }
1551 56e0773d 2019-11-28 stsp err = write_tree(&id, subdirpath, ignores, repo,
1552 3ce1b845 2019-07-15 stsp progress_cb, progress_arg);
1553 56e0773d 2019-11-28 stsp if (err)
1554 56e0773d 2019-11-28 stsp goto done;
1555 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, id, sizeof((*new_te)->id));
1556 56e0773d 2019-11-28 stsp
1557 3ce1b845 2019-07-15 stsp done:
1558 56e0773d 2019-11-28 stsp free(id);
1559 3ce1b845 2019-07-15 stsp free(subdirpath);
1560 3ce1b845 2019-07-15 stsp if (err) {
1561 56e0773d 2019-11-28 stsp free(*new_te);
1562 3ce1b845 2019-07-15 stsp *new_te = NULL;
1563 3ce1b845 2019-07-15 stsp }
1564 3ce1b845 2019-07-15 stsp return err;
1565 3ce1b845 2019-07-15 stsp }
1566 3ce1b845 2019-07-15 stsp
1567 3ce1b845 2019-07-15 stsp static const struct got_error *
1568 3ce1b845 2019-07-15 stsp write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1569 3ce1b845 2019-07-15 stsp struct got_pathlist_head *ignores, struct got_repository *repo,
1570 3ce1b845 2019-07-15 stsp got_repo_import_cb progress_cb, void *progress_arg)
1571 3ce1b845 2019-07-15 stsp {
1572 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
1573 3ce1b845 2019-07-15 stsp DIR *dir;
1574 3ce1b845 2019-07-15 stsp struct dirent *de;
1575 56e0773d 2019-11-28 stsp int nentries;
1576 3ce1b845 2019-07-15 stsp struct got_tree_entry *new_te = NULL;
1577 3ce1b845 2019-07-15 stsp struct got_pathlist_head paths;
1578 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
1579 3ce1b845 2019-07-15 stsp
1580 3ce1b845 2019-07-15 stsp *new_tree_id = NULL;
1581 3ce1b845 2019-07-15 stsp
1582 3ce1b845 2019-07-15 stsp TAILQ_INIT(&paths);
1583 3ce1b845 2019-07-15 stsp
1584 3ce1b845 2019-07-15 stsp dir = opendir(path_dir);
1585 3ce1b845 2019-07-15 stsp if (dir == NULL) {
1586 3ce1b845 2019-07-15 stsp err = got_error_from_errno2("opendir", path_dir);
1587 3ce1b845 2019-07-15 stsp goto done;
1588 3ce1b845 2019-07-15 stsp }
1589 3ce1b845 2019-07-15 stsp
1590 56e0773d 2019-11-28 stsp nentries = 0;
1591 3ce1b845 2019-07-15 stsp while ((de = readdir(dir)) != NULL) {
1592 3ce1b845 2019-07-15 stsp int ignore = 0;
1593 3ce1b845 2019-07-15 stsp
1594 3ce1b845 2019-07-15 stsp if (strcmp(de->d_name, ".") == 0 ||
1595 3ce1b845 2019-07-15 stsp strcmp(de->d_name, "..") == 0)
1596 3ce1b845 2019-07-15 stsp continue;
1597 3ce1b845 2019-07-15 stsp
1598 3ce1b845 2019-07-15 stsp TAILQ_FOREACH(pe, ignores, entry) {
1599 3ce1b845 2019-07-15 stsp if (fnmatch(pe->path, de->d_name, 0) == 0) {
1600 3ce1b845 2019-07-15 stsp ignore = 1;
1601 3ce1b845 2019-07-15 stsp break;
1602 3ce1b845 2019-07-15 stsp }
1603 3ce1b845 2019-07-15 stsp }
1604 3ce1b845 2019-07-15 stsp if (ignore)
1605 3ce1b845 2019-07-15 stsp continue;
1606 3ce1b845 2019-07-15 stsp if (de->d_type == DT_DIR) {
1607 3ce1b845 2019-07-15 stsp err = import_subdir(&new_te, de, path_dir,
1608 3ce1b845 2019-07-15 stsp ignores, repo, progress_cb, progress_arg);
1609 db1d3576 2019-10-04 stsp if (err) {
1610 db1d3576 2019-10-04 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY)
1611 db1d3576 2019-10-04 stsp goto done;
1612 db1d3576 2019-10-04 stsp err = NULL;
1613 db1d3576 2019-10-04 stsp continue;
1614 db1d3576 2019-10-04 stsp }
1615 3ce1b845 2019-07-15 stsp } else if (de->d_type == DT_REG) {
1616 3ce1b845 2019-07-15 stsp err = import_file(&new_te, de, path_dir, repo);
1617 3ce1b845 2019-07-15 stsp if (err)
1618 3ce1b845 2019-07-15 stsp goto done;
1619 3ce1b845 2019-07-15 stsp } else
1620 3ce1b845 2019-07-15 stsp continue;
1621 3ce1b845 2019-07-15 stsp
1622 3ce1b845 2019-07-15 stsp err = insert_tree_entry(new_te, &paths);
1623 3ce1b845 2019-07-15 stsp if (err)
1624 3ce1b845 2019-07-15 stsp goto done;
1625 56e0773d 2019-11-28 stsp nentries++;
1626 3ce1b845 2019-07-15 stsp }
1627 3ce1b845 2019-07-15 stsp
1628 db1d3576 2019-10-04 stsp if (TAILQ_EMPTY(&paths)) {
1629 db1d3576 2019-10-04 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1630 db1d3576 2019-10-04 stsp goto done;
1631 db1d3576 2019-10-04 stsp }
1632 db1d3576 2019-10-04 stsp
1633 3ce1b845 2019-07-15 stsp TAILQ_FOREACH(pe, &paths, entry) {
1634 3ce1b845 2019-07-15 stsp struct got_tree_entry *te = pe->data;
1635 3ce1b845 2019-07-15 stsp char *path;
1636 3ce1b845 2019-07-15 stsp if (!S_ISREG(te->mode))
1637 3ce1b845 2019-07-15 stsp continue;
1638 3ce1b845 2019-07-15 stsp if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1639 3ce1b845 2019-07-15 stsp err = got_error_from_errno("asprintf");
1640 3ce1b845 2019-07-15 stsp goto done;
1641 3ce1b845 2019-07-15 stsp }
1642 3ce1b845 2019-07-15 stsp err = (*progress_cb)(progress_arg, path);
1643 3ce1b845 2019-07-15 stsp free(path);
1644 3ce1b845 2019-07-15 stsp if (err)
1645 3ce1b845 2019-07-15 stsp goto done;
1646 3ce1b845 2019-07-15 stsp }
1647 3ce1b845 2019-07-15 stsp
1648 56e0773d 2019-11-28 stsp err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
1649 3ce1b845 2019-07-15 stsp done:
1650 3ce1b845 2019-07-15 stsp if (dir)
1651 3ce1b845 2019-07-15 stsp closedir(dir);
1652 3ce1b845 2019-07-15 stsp got_pathlist_free(&paths);
1653 3ce1b845 2019-07-15 stsp return err;
1654 3ce1b845 2019-07-15 stsp }
1655 3ce1b845 2019-07-15 stsp
1656 3ce1b845 2019-07-15 stsp const struct got_error *
1657 3ce1b845 2019-07-15 stsp got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1658 3ce1b845 2019-07-15 stsp const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1659 3ce1b845 2019-07-15 stsp struct got_repository *repo, got_repo_import_cb progress_cb,
1660 3ce1b845 2019-07-15 stsp void *progress_arg)
1661 3ce1b845 2019-07-15 stsp {
1662 3ce1b845 2019-07-15 stsp const struct got_error *err;
1663 3ce1b845 2019-07-15 stsp struct got_object_id *new_tree_id;
1664 3ce1b845 2019-07-15 stsp
1665 3ce1b845 2019-07-15 stsp err = write_tree(&new_tree_id, path_dir, ignores, repo,
1666 3ce1b845 2019-07-15 stsp progress_cb, progress_arg);
1667 3ce1b845 2019-07-15 stsp if (err)
1668 3ce1b845 2019-07-15 stsp return err;
1669 3ce1b845 2019-07-15 stsp
1670 3ce1b845 2019-07-15 stsp err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1671 3ce1b845 2019-07-15 stsp author, time(NULL), author, time(NULL), logmsg, repo);
1672 3ce1b845 2019-07-15 stsp free(new_tree_id);
1673 3ce1b845 2019-07-15 stsp return err;
1674 3ce1b845 2019-07-15 stsp }