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