Blame


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