Blame


1 86c3caaf 2018-03-09 stsp /*
2 86c3caaf 2018-03-09 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 86c3caaf 2018-03-09 stsp *
4 86c3caaf 2018-03-09 stsp * Permission to use, copy, modify, and distribute this software for any
5 86c3caaf 2018-03-09 stsp * purpose with or without fee is hereby granted, provided that the above
6 86c3caaf 2018-03-09 stsp * copyright notice and this permission notice appear in all copies.
7 86c3caaf 2018-03-09 stsp *
8 86c3caaf 2018-03-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 86c3caaf 2018-03-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 86c3caaf 2018-03-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 86c3caaf 2018-03-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 86c3caaf 2018-03-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 86c3caaf 2018-03-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 86c3caaf 2018-03-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 86c3caaf 2018-03-09 stsp */
16 86c3caaf 2018-03-09 stsp
17 86c3caaf 2018-03-09 stsp #include <sys/stat.h>
18 6d9d28c3 2018-03-11 stsp #include <sys/limits.h>
19 9d31a1d8 2018-03-11 stsp #include <sys/queue.h>
20 86c3caaf 2018-03-09 stsp
21 86c3caaf 2018-03-09 stsp #include <string.h>
22 86c3caaf 2018-03-09 stsp #include <stdio.h>
23 86c3caaf 2018-03-09 stsp #include <stdlib.h>
24 86c3caaf 2018-03-09 stsp #include <fcntl.h>
25 86c3caaf 2018-03-09 stsp #include <errno.h>
26 86c3caaf 2018-03-09 stsp #include <unistd.h>
27 9d31a1d8 2018-03-11 stsp #include <sha1.h>
28 9d31a1d8 2018-03-11 stsp #include <zlib.h>
29 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
30 86c3caaf 2018-03-09 stsp
31 86c3caaf 2018-03-09 stsp #include "got_error.h"
32 86c3caaf 2018-03-09 stsp #include "got_repository.h"
33 5261c201 2018-04-01 stsp #include "got_reference.h"
34 9d31a1d8 2018-03-11 stsp #include "got_object.h"
35 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
36 511a516b 2018-05-19 stsp #include "got_opentemp.h"
37 86c3caaf 2018-03-09 stsp
38 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
39 718b3ab0 2018-03-17 stsp #include "got_lib_path.h"
40 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
41 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
42 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
43 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
44 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
45 9d31a1d8 2018-03-11 stsp
46 9d31a1d8 2018-03-11 stsp #ifndef MIN
47 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
48 9d31a1d8 2018-03-11 stsp #endif
49 86c3caaf 2018-03-09 stsp
50 99724ed4 2018-03-10 stsp static const struct got_error *
51 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
52 99724ed4 2018-03-10 stsp {
53 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
54 99724ed4 2018-03-10 stsp char *path;
55 99724ed4 2018-03-10 stsp int fd = -1;
56 99724ed4 2018-03-10 stsp char buf[4];
57 99724ed4 2018-03-10 stsp ssize_t n;
58 99724ed4 2018-03-10 stsp
59 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
60 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
61 99724ed4 2018-03-10 stsp path = NULL;
62 99724ed4 2018-03-10 stsp goto done;
63 99724ed4 2018-03-10 stsp }
64 99724ed4 2018-03-10 stsp
65 73a5ef67 2018-03-11 stsp fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
66 99724ed4 2018-03-10 stsp GOT_DEFAULT_FILE_MODE);
67 99724ed4 2018-03-10 stsp if (fd == -1) {
68 99724ed4 2018-03-10 stsp err = got_error_from_errno();
69 99724ed4 2018-03-10 stsp goto done;
70 99724ed4 2018-03-10 stsp }
71 99724ed4 2018-03-10 stsp
72 99724ed4 2018-03-10 stsp /* The file should be empty. */
73 99724ed4 2018-03-10 stsp n = read(fd, buf, sizeof(buf));
74 99724ed4 2018-03-10 stsp if (n != 0) {
75 99724ed4 2018-03-10 stsp err = (n == -1 ? got_error_from_errno() :
76 99724ed4 2018-03-10 stsp got_error(GOT_ERR_WORKTREE_EXISTS));
77 99724ed4 2018-03-10 stsp goto done;
78 99724ed4 2018-03-10 stsp }
79 99724ed4 2018-03-10 stsp
80 99724ed4 2018-03-10 stsp if (content) {
81 99724ed4 2018-03-10 stsp int len = dprintf(fd, "%s\n", content);
82 99724ed4 2018-03-10 stsp if (len != strlen(content) + 1) {
83 99724ed4 2018-03-10 stsp err = got_error_from_errno();
84 99724ed4 2018-03-10 stsp goto done;
85 99724ed4 2018-03-10 stsp }
86 99724ed4 2018-03-10 stsp }
87 99724ed4 2018-03-10 stsp
88 99724ed4 2018-03-10 stsp done:
89 99724ed4 2018-03-10 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
90 99724ed4 2018-03-10 stsp err = got_error_from_errno();
91 99724ed4 2018-03-10 stsp free(path);
92 99724ed4 2018-03-10 stsp return err;
93 99724ed4 2018-03-10 stsp }
94 99724ed4 2018-03-10 stsp
95 09fe317a 2018-03-11 stsp static const struct got_error *
96 7ac97322 2018-03-11 stsp read_meta_file(char **content, const char *path_got, const char *name)
97 09fe317a 2018-03-11 stsp {
98 09fe317a 2018-03-11 stsp const struct got_error *err = NULL;
99 09fe317a 2018-03-11 stsp char *path;
100 09fe317a 2018-03-11 stsp int fd = -1;
101 09fe317a 2018-03-11 stsp ssize_t n;
102 09fe317a 2018-03-11 stsp struct stat sb;
103 09fe317a 2018-03-11 stsp
104 09fe317a 2018-03-11 stsp *content = NULL;
105 09fe317a 2018-03-11 stsp
106 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
107 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
108 09fe317a 2018-03-11 stsp path = NULL;
109 09fe317a 2018-03-11 stsp goto done;
110 09fe317a 2018-03-11 stsp }
111 09fe317a 2018-03-11 stsp
112 ef99fdb1 2018-03-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
113 09fe317a 2018-03-11 stsp if (fd == -1) {
114 ef99fdb1 2018-03-11 stsp err = got_error_from_errno();
115 ef99fdb1 2018-03-11 stsp goto done;
116 ef99fdb1 2018-03-11 stsp }
117 ef99fdb1 2018-03-11 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
118 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
119 73a5ef67 2018-03-11 stsp : got_error_from_errno());
120 09fe317a 2018-03-11 stsp goto done;
121 09fe317a 2018-03-11 stsp }
122 09fe317a 2018-03-11 stsp
123 09fe317a 2018-03-11 stsp stat(path, &sb);
124 09fe317a 2018-03-11 stsp *content = calloc(1, sb.st_size);
125 09fe317a 2018-03-11 stsp if (*content == NULL) {
126 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
127 09fe317a 2018-03-11 stsp goto done;
128 09fe317a 2018-03-11 stsp }
129 09fe317a 2018-03-11 stsp
130 09fe317a 2018-03-11 stsp n = read(fd, *content, sb.st_size);
131 09fe317a 2018-03-11 stsp if (n != sb.st_size) {
132 0605801d 2018-03-11 stsp err = (n == -1 ? got_error_from_errno() :
133 0605801d 2018-03-11 stsp got_error(GOT_ERR_WORKTREE_META));
134 09fe317a 2018-03-11 stsp goto done;
135 09fe317a 2018-03-11 stsp }
136 09fe317a 2018-03-11 stsp if ((*content)[sb.st_size - 1] != '\n') {
137 09fe317a 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
138 09fe317a 2018-03-11 stsp goto done;
139 09fe317a 2018-03-11 stsp }
140 09fe317a 2018-03-11 stsp (*content)[sb.st_size - 1] = '\0';
141 09fe317a 2018-03-11 stsp
142 09fe317a 2018-03-11 stsp done:
143 09fe317a 2018-03-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
144 09fe317a 2018-03-11 stsp err = got_error_from_errno();
145 09fe317a 2018-03-11 stsp free(path);
146 09fe317a 2018-03-11 stsp if (err) {
147 09fe317a 2018-03-11 stsp free(*content);
148 09fe317a 2018-03-11 stsp *content = NULL;
149 09fe317a 2018-03-11 stsp }
150 09fe317a 2018-03-11 stsp return err;
151 09fe317a 2018-03-11 stsp }
152 09fe317a 2018-03-11 stsp
153 86c3caaf 2018-03-09 stsp const struct got_error *
154 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
155 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
156 86c3caaf 2018-03-09 stsp {
157 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
158 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
159 65596e15 2018-12-24 stsp int obj_type;
160 7ac97322 2018-03-11 stsp char *path_got = NULL;
161 08d425ea 2018-12-24 stsp char *refstr = NULL;
162 cde76477 2018-03-11 stsp char *repo_path = NULL;
163 1451e70d 2018-03-10 stsp char *formatstr = NULL;
164 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
165 65596e15 2018-12-24 stsp char *basestr = NULL;
166 65596e15 2018-12-24 stsp
167 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
168 65596e15 2018-12-24 stsp if (err)
169 65596e15 2018-12-24 stsp return err;
170 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
171 65596e15 2018-12-24 stsp if (err)
172 65596e15 2018-12-24 stsp return err;
173 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
174 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
175 86c3caaf 2018-03-09 stsp
176 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
177 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
178 0a585a0d 2018-03-17 stsp return got_error_from_errno();
179 0bb8a95e 2018-03-12 stsp }
180 577ec78f 2018-03-11 stsp
181 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
182 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
183 86c3caaf 2018-03-09 stsp err = got_error_from_errno();
184 86c3caaf 2018-03-09 stsp goto done;
185 86c3caaf 2018-03-09 stsp }
186 86c3caaf 2018-03-09 stsp
187 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
188 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
189 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
190 86c3caaf 2018-03-09 stsp goto done;
191 86c3caaf 2018-03-09 stsp }
192 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
193 86c3caaf 2018-03-09 stsp err = got_error_from_errno();
194 86c3caaf 2018-03-09 stsp goto done;
195 86c3caaf 2018-03-09 stsp }
196 86c3caaf 2018-03-09 stsp
197 056e7441 2018-03-11 stsp /* Create an empty lock file. */
198 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
199 056e7441 2018-03-11 stsp if (err)
200 056e7441 2018-03-11 stsp goto done;
201 056e7441 2018-03-11 stsp
202 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
203 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
204 99724ed4 2018-03-10 stsp if (err)
205 86c3caaf 2018-03-09 stsp goto done;
206 86c3caaf 2018-03-09 stsp
207 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
208 08d425ea 2018-12-24 stsp refstr = got_ref_to_str(head_ref);
209 08d425ea 2018-12-24 stsp if (refstr == NULL) {
210 a1a7858a 2018-12-24 stsp err = got_error_from_errno();
211 a1a7858a 2018-12-24 stsp goto done;
212 a1a7858a 2018-12-24 stsp }
213 08d425ea 2018-12-24 stsp err = create_meta_file(path_got, GOT_WORKTREE_HEAD, refstr);
214 65596e15 2018-12-24 stsp if (err)
215 65596e15 2018-12-24 stsp goto done;
216 65596e15 2018-12-24 stsp
217 65596e15 2018-12-24 stsp /* Record our base commit. */
218 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
219 65596e15 2018-12-24 stsp if (err)
220 65596e15 2018-12-24 stsp goto done;
221 65596e15 2018-12-24 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE, basestr);
222 99724ed4 2018-03-10 stsp if (err)
223 86c3caaf 2018-03-09 stsp goto done;
224 86c3caaf 2018-03-09 stsp
225 1451e70d 2018-03-10 stsp /* Store path to repository. */
226 cde76477 2018-03-11 stsp repo_path = got_repo_get_path(repo);
227 cde76477 2018-03-11 stsp if (repo_path == NULL) {
228 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
229 86c3caaf 2018-03-09 stsp goto done;
230 86c3caaf 2018-03-09 stsp }
231 cde76477 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY, repo_path);
232 99724ed4 2018-03-10 stsp if (err)
233 86c3caaf 2018-03-09 stsp goto done;
234 86c3caaf 2018-03-09 stsp
235 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
236 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
237 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
238 577ec78f 2018-03-11 stsp if (err)
239 577ec78f 2018-03-11 stsp goto done;
240 577ec78f 2018-03-11 stsp
241 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
242 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
243 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
244 1451e70d 2018-03-10 stsp goto done;
245 1451e70d 2018-03-10 stsp }
246 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
247 99724ed4 2018-03-10 stsp if (err)
248 1451e70d 2018-03-10 stsp goto done;
249 1451e70d 2018-03-10 stsp
250 86c3caaf 2018-03-09 stsp done:
251 65596e15 2018-12-24 stsp free(commit_id);
252 7ac97322 2018-03-11 stsp free(path_got);
253 1451e70d 2018-03-10 stsp free(formatstr);
254 08d425ea 2018-12-24 stsp free(refstr);
255 cde76477 2018-03-11 stsp free(repo_path);
256 0bb8a95e 2018-03-12 stsp free(absprefix);
257 65596e15 2018-12-24 stsp free(basestr);
258 86c3caaf 2018-03-09 stsp return err;
259 86c3caaf 2018-03-09 stsp }
260 86c3caaf 2018-03-09 stsp
261 86c3caaf 2018-03-09 stsp const struct got_error *
262 86c3caaf 2018-03-09 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
263 86c3caaf 2018-03-09 stsp {
264 6d9d28c3 2018-03-11 stsp const struct got_error *err = NULL;
265 7ac97322 2018-03-11 stsp char *path_got;
266 6d9d28c3 2018-03-11 stsp char *formatstr = NULL;
267 056e7441 2018-03-11 stsp char *path_lock = NULL;
268 6d9d28c3 2018-03-11 stsp int version, fd = -1;
269 6d9d28c3 2018-03-11 stsp const char *errstr;
270 6d9d28c3 2018-03-11 stsp
271 6d9d28c3 2018-03-11 stsp *worktree = NULL;
272 6d9d28c3 2018-03-11 stsp
273 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
274 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
275 7ac97322 2018-03-11 stsp path_got = NULL;
276 6d9d28c3 2018-03-11 stsp goto done;
277 6d9d28c3 2018-03-11 stsp }
278 6d9d28c3 2018-03-11 stsp
279 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
280 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
281 056e7441 2018-03-11 stsp path_lock = NULL;
282 6d9d28c3 2018-03-11 stsp goto done;
283 6d9d28c3 2018-03-11 stsp }
284 6d9d28c3 2018-03-11 stsp
285 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
286 6d9d28c3 2018-03-11 stsp if (fd == -1) {
287 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
288 73a5ef67 2018-03-11 stsp : got_error_from_errno());
289 6d9d28c3 2018-03-11 stsp goto done;
290 6d9d28c3 2018-03-11 stsp }
291 6d9d28c3 2018-03-11 stsp
292 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
293 6d9d28c3 2018-03-11 stsp if (err)
294 6d9d28c3 2018-03-11 stsp goto done;
295 6d9d28c3 2018-03-11 stsp
296 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
297 6d9d28c3 2018-03-11 stsp if (errstr) {
298 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
299 6d9d28c3 2018-03-11 stsp goto done;
300 6d9d28c3 2018-03-11 stsp }
301 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
302 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
303 6d9d28c3 2018-03-11 stsp goto done;
304 6d9d28c3 2018-03-11 stsp }
305 6d9d28c3 2018-03-11 stsp
306 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
307 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
308 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
309 6d9d28c3 2018-03-11 stsp goto done;
310 6d9d28c3 2018-03-11 stsp }
311 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
312 6d9d28c3 2018-03-11 stsp
313 c88eb298 2018-03-11 stsp (*worktree)->root_path = strdup(path);
314 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
315 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
316 6d9d28c3 2018-03-11 stsp goto done;
317 6d9d28c3 2018-03-11 stsp }
318 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
319 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
320 6d9d28c3 2018-03-11 stsp if (err)
321 6d9d28c3 2018-03-11 stsp goto done;
322 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
323 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
324 93a30277 2018-12-24 stsp if (err)
325 93a30277 2018-12-24 stsp goto done;
326 93a30277 2018-12-24 stsp
327 93a30277 2018-12-24 stsp err = read_meta_file(&(*worktree)->base, path_got, GOT_WORKTREE_BASE);
328 f5baf295 2018-03-11 stsp if (err)
329 f5baf295 2018-03-11 stsp goto done;
330 f5baf295 2018-03-11 stsp
331 e8f36958 2018-03-11 stsp err = read_meta_file(&(*worktree)->head_ref, path_got,
332 e8f36958 2018-03-11 stsp GOT_WORKTREE_HEAD);
333 f5baf295 2018-03-11 stsp if (err)
334 6d9d28c3 2018-03-11 stsp goto done;
335 6d9d28c3 2018-03-11 stsp
336 6d9d28c3 2018-03-11 stsp done:
337 7ac97322 2018-03-11 stsp free(path_got);
338 056e7441 2018-03-11 stsp free(path_lock);
339 6d9d28c3 2018-03-11 stsp if (err) {
340 6d9d28c3 2018-03-11 stsp if (fd != -1)
341 6d9d28c3 2018-03-11 stsp close(fd);
342 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
343 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
344 6d9d28c3 2018-03-11 stsp *worktree = NULL;
345 6d9d28c3 2018-03-11 stsp } else
346 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
347 6d9d28c3 2018-03-11 stsp
348 6d9d28c3 2018-03-11 stsp return err;
349 86c3caaf 2018-03-09 stsp }
350 86c3caaf 2018-03-09 stsp
351 86c3caaf 2018-03-09 stsp void
352 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
353 86c3caaf 2018-03-09 stsp {
354 c88eb298 2018-03-11 stsp free(worktree->root_path);
355 cde76477 2018-03-11 stsp free(worktree->repo_path);
356 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
357 93a30277 2018-12-24 stsp free(worktree->base);
358 e8f36958 2018-03-11 stsp free(worktree->head_ref);
359 056e7441 2018-03-11 stsp if (worktree->lockfd != -1)
360 056e7441 2018-03-11 stsp close(worktree->lockfd);
361 6d9d28c3 2018-03-11 stsp free(worktree);
362 86c3caaf 2018-03-09 stsp }
363 86c3caaf 2018-03-09 stsp
364 86c3caaf 2018-03-09 stsp char *
365 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
366 86c3caaf 2018-03-09 stsp {
367 cde76477 2018-03-11 stsp return strdup(worktree->repo_path);
368 86c3caaf 2018-03-09 stsp }
369 86c3caaf 2018-03-09 stsp
370 35be1456 2018-03-11 stsp char *
371 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
372 86c3caaf 2018-03-09 stsp {
373 35be1456 2018-03-11 stsp return strdup(worktree->head_ref);
374 86c3caaf 2018-03-09 stsp }
375 86c3caaf 2018-03-09 stsp
376 9d31a1d8 2018-03-11 stsp static const struct got_error *
377 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
378 86c3caaf 2018-03-09 stsp {
379 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
380 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
381 9d31a1d8 2018-03-11 stsp : got_error_from_errno());
382 86c3caaf 2018-03-09 stsp return NULL;
383 86c3caaf 2018-03-09 stsp }
384 9d31a1d8 2018-03-11 stsp
385 9d31a1d8 2018-03-11 stsp static const char *
386 9d31a1d8 2018-03-11 stsp apply_path_prefix(struct got_worktree *worktree, const char *path)
387 9d31a1d8 2018-03-11 stsp {
388 9d31a1d8 2018-03-11 stsp const char *p = path;
389 9d31a1d8 2018-03-11 stsp p += strlen(worktree->path_prefix);
390 9d31a1d8 2018-03-11 stsp if (*p == '/')
391 9d31a1d8 2018-03-11 stsp p++;
392 9d31a1d8 2018-03-11 stsp return p;
393 9d31a1d8 2018-03-11 stsp }
394 9d31a1d8 2018-03-11 stsp
395 9d31a1d8 2018-03-11 stsp static const struct got_error *
396 9d31a1d8 2018-03-11 stsp add_file_on_disk(struct got_worktree *worktree, struct got_fileindex *fileindex,
397 9d31a1d8 2018-03-11 stsp const char *path, struct got_blob_object *blob, struct got_repository *repo)
398 9d31a1d8 2018-03-11 stsp {
399 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
400 c34b20a2 2018-03-12 stsp char *ondisk_path;
401 9d31a1d8 2018-03-11 stsp int fd;
402 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
403 9d31a1d8 2018-03-11 stsp struct got_fileindex_entry *entry;
404 9d31a1d8 2018-03-11 stsp
405 c34b20a2 2018-03-12 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
406 9d31a1d8 2018-03-11 stsp apply_path_prefix(worktree, path)) == -1)
407 0a585a0d 2018-03-17 stsp return got_error_from_errno();
408 9d31a1d8 2018-03-11 stsp
409 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
410 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
411 9d31a1d8 2018-03-11 stsp if (fd == -1) {
412 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
413 9d31a1d8 2018-03-11 stsp if (errno == EEXIST) {
414 9d31a1d8 2018-03-11 stsp struct stat sb;
415 c34b20a2 2018-03-12 stsp if (lstat(ondisk_path, &sb) == -1) {
416 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
417 9d31a1d8 2018-03-11 stsp } else if (!S_ISREG(sb.st_mode)) {
418 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
419 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
420 9d31a1d8 2018-03-11 stsp }
421 9d31a1d8 2018-03-11 stsp }
422 9d31a1d8 2018-03-11 stsp return err;
423 9d31a1d8 2018-03-11 stsp }
424 9d31a1d8 2018-03-11 stsp
425 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
426 9d31a1d8 2018-03-11 stsp do {
427 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
428 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
429 9d31a1d8 2018-03-11 stsp if (err)
430 9d31a1d8 2018-03-11 stsp break;
431 9d31a1d8 2018-03-11 stsp if (len > 0) {
432 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
433 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
434 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
435 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
436 b87c6f83 2018-12-24 stsp goto done;
437 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
438 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
439 b87c6f83 2018-12-24 stsp goto done;
440 9d31a1d8 2018-03-11 stsp }
441 61d6eaa3 2018-12-24 stsp hdrlen = 0;
442 9d31a1d8 2018-03-11 stsp }
443 9d31a1d8 2018-03-11 stsp } while (len != 0);
444 9d31a1d8 2018-03-11 stsp
445 9d31a1d8 2018-03-11 stsp fsync(fd);
446 9d31a1d8 2018-03-11 stsp
447 7426bbfd 2018-12-24 stsp err = got_fileindex_entry_alloc(&entry, ondisk_path,
448 c34b20a2 2018-03-12 stsp apply_path_prefix(worktree, path), blob->id.sha1);
449 9d31a1d8 2018-03-11 stsp if (err)
450 9d31a1d8 2018-03-11 stsp goto done;
451 9d31a1d8 2018-03-11 stsp
452 9d31a1d8 2018-03-11 stsp err = got_fileindex_entry_add(fileindex, entry);
453 9d31a1d8 2018-03-11 stsp if (err)
454 9d31a1d8 2018-03-11 stsp goto done;
455 9d31a1d8 2018-03-11 stsp done:
456 9d31a1d8 2018-03-11 stsp close(fd);
457 c34b20a2 2018-03-12 stsp free(ondisk_path);
458 9d31a1d8 2018-03-11 stsp return err;
459 9d31a1d8 2018-03-11 stsp }
460 9d31a1d8 2018-03-11 stsp
461 9d31a1d8 2018-03-11 stsp static const struct got_error *
462 9d31a1d8 2018-03-11 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
463 9d31a1d8 2018-03-11 stsp {
464 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
465 9d31a1d8 2018-03-11 stsp char *abspath;
466 9d31a1d8 2018-03-11 stsp
467 9d31a1d8 2018-03-11 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path,
468 9d31a1d8 2018-03-11 stsp apply_path_prefix(worktree, path)) == -1)
469 0a585a0d 2018-03-17 stsp return got_error_from_errno();
470 9d31a1d8 2018-03-11 stsp
471 9d31a1d8 2018-03-11 stsp /* XXX queue work rather than editing disk directly? */
472 9d31a1d8 2018-03-11 stsp if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
473 9d31a1d8 2018-03-11 stsp struct stat sb;
474 9d31a1d8 2018-03-11 stsp
475 9d31a1d8 2018-03-11 stsp if (errno != EEXIST) {
476 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
477 9d31a1d8 2018-03-11 stsp goto done;
478 9d31a1d8 2018-03-11 stsp }
479 9d31a1d8 2018-03-11 stsp
480 9d31a1d8 2018-03-11 stsp if (lstat(abspath, &sb) == -1) {
481 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
482 9d31a1d8 2018-03-11 stsp goto done;
483 9d31a1d8 2018-03-11 stsp }
484 9d31a1d8 2018-03-11 stsp
485 9d31a1d8 2018-03-11 stsp if (!S_ISDIR(sb.st_mode)) {
486 9d31a1d8 2018-03-11 stsp /* TODO directory is obstructed; do something */
487 9d31a1d8 2018-03-11 stsp return got_error(GOT_ERR_FILE_OBSTRUCTED);
488 9d31a1d8 2018-03-11 stsp }
489 9d31a1d8 2018-03-11 stsp }
490 9d31a1d8 2018-03-11 stsp
491 9d31a1d8 2018-03-11 stsp done:
492 9d31a1d8 2018-03-11 stsp free(abspath);
493 9d31a1d8 2018-03-11 stsp return err;
494 9d31a1d8 2018-03-11 stsp }
495 9d31a1d8 2018-03-11 stsp
496 9d31a1d8 2018-03-11 stsp static const struct got_error *
497 9d31a1d8 2018-03-11 stsp tree_checkout(struct got_worktree *, struct got_fileindex *,
498 92a684f4 2018-03-12 stsp struct got_tree_object *, const char *, struct got_repository *,
499 99437157 2018-11-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
500 99437157 2018-11-11 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg);
501 9d31a1d8 2018-03-11 stsp
502 9d31a1d8 2018-03-11 stsp static const struct got_error *
503 9d31a1d8 2018-03-11 stsp tree_checkout_entry(struct got_worktree *worktree,
504 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex, struct got_tree_entry *te,
505 92a684f4 2018-03-12 stsp const char *parent, struct got_repository *repo,
506 99437157 2018-11-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
507 99437157 2018-11-11 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
508 9d31a1d8 2018-03-11 stsp {
509 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
510 9d31a1d8 2018-03-11 stsp struct got_object *obj = NULL;
511 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
512 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
513 9d31a1d8 2018-03-11 stsp char *path = NULL;
514 f7b38925 2018-04-01 stsp char *progress_path = NULL;
515 9d31a1d8 2018-03-11 stsp size_t len;
516 9d31a1d8 2018-03-11 stsp
517 9d31a1d8 2018-03-11 stsp if (parent[0] == '/' && parent[1] == '\0')
518 9d31a1d8 2018-03-11 stsp parent = "";
519 9d31a1d8 2018-03-11 stsp if (asprintf(&path, "%s/%s", parent, te->name) == -1)
520 0a585a0d 2018-03-17 stsp return got_error_from_errno();
521 9d31a1d8 2018-03-11 stsp
522 9d31a1d8 2018-03-11 stsp /* Skip this entry if it is outside of our path prefix. */
523 9d31a1d8 2018-03-11 stsp len = MIN(strlen(worktree->path_prefix), strlen(path));
524 9d31a1d8 2018-03-11 stsp if (strncmp(path, worktree->path_prefix, len) != 0) {
525 9d31a1d8 2018-03-11 stsp free(path);
526 9d31a1d8 2018-03-11 stsp return NULL;
527 9d31a1d8 2018-03-11 stsp }
528 9d31a1d8 2018-03-11 stsp
529 9d31a1d8 2018-03-11 stsp err = got_object_open(&obj, repo, te->id);
530 9d31a1d8 2018-03-11 stsp if (err)
531 9d31a1d8 2018-03-11 stsp goto done;
532 9d31a1d8 2018-03-11 stsp
533 f7b38925 2018-04-01 stsp progress_path = path;
534 f7b38925 2018-04-01 stsp if (strncmp(progress_path, worktree->path_prefix, len) == 0)
535 f7b38925 2018-04-01 stsp progress_path += len;
536 f7b38925 2018-04-01 stsp (*progress_cb)(progress_arg, progress_path);
537 92a684f4 2018-03-12 stsp
538 15a94983 2018-12-23 stsp switch (obj->type) {
539 9d31a1d8 2018-03-11 stsp case GOT_OBJ_TYPE_BLOB:
540 9d31a1d8 2018-03-11 stsp if (strlen(worktree->path_prefix) >= strlen(path))
541 9d31a1d8 2018-03-11 stsp break;
542 9d31a1d8 2018-03-11 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
543 9d31a1d8 2018-03-11 stsp if (err)
544 9d31a1d8 2018-03-11 stsp goto done;
545 9d31a1d8 2018-03-11 stsp err = add_file_on_disk(worktree, fileindex, path, blob, repo);
546 9d31a1d8 2018-03-11 stsp break;
547 9d31a1d8 2018-03-11 stsp case GOT_OBJ_TYPE_TREE:
548 9d31a1d8 2018-03-11 stsp err = got_object_tree_open(&tree, repo, obj);
549 9d31a1d8 2018-03-11 stsp if (err)
550 9d31a1d8 2018-03-11 stsp goto done;
551 9d31a1d8 2018-03-11 stsp if (strlen(worktree->path_prefix) < strlen(path)) {
552 9d31a1d8 2018-03-11 stsp err = add_dir_on_disk(worktree, path);
553 9d31a1d8 2018-03-11 stsp if (err)
554 9d31a1d8 2018-03-11 stsp break;
555 9d31a1d8 2018-03-11 stsp }
556 99437157 2018-11-11 stsp /* XXX infinite recursion possible */
557 92a684f4 2018-03-12 stsp err = tree_checkout(worktree, fileindex, tree, path, repo,
558 99437157 2018-11-11 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
559 9d31a1d8 2018-03-11 stsp break;
560 9d31a1d8 2018-03-11 stsp default:
561 9d31a1d8 2018-03-11 stsp break;
562 9d31a1d8 2018-03-11 stsp }
563 9d31a1d8 2018-03-11 stsp
564 9d31a1d8 2018-03-11 stsp done:
565 9d31a1d8 2018-03-11 stsp if (blob)
566 9d31a1d8 2018-03-11 stsp got_object_blob_close(blob);
567 9d31a1d8 2018-03-11 stsp if (tree)
568 9d31a1d8 2018-03-11 stsp got_object_tree_close(tree);
569 9d31a1d8 2018-03-11 stsp if (obj)
570 9d31a1d8 2018-03-11 stsp got_object_close(obj);
571 9d31a1d8 2018-03-11 stsp free(path);
572 9d31a1d8 2018-03-11 stsp return err;
573 9d31a1d8 2018-03-11 stsp }
574 9d31a1d8 2018-03-11 stsp
575 9d31a1d8 2018-03-11 stsp static const struct got_error *
576 9d31a1d8 2018-03-11 stsp tree_checkout(struct got_worktree *worktree,
577 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex, struct got_tree_object *tree,
578 92a684f4 2018-03-12 stsp const char *path, struct got_repository *repo,
579 99437157 2018-11-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
580 99437157 2018-11-11 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
581 9d31a1d8 2018-03-11 stsp {
582 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
583 883f0469 2018-06-23 stsp const struct got_tree_entries *entries;
584 9d31a1d8 2018-03-11 stsp struct got_tree_entry *te;
585 9d31a1d8 2018-03-11 stsp size_t len;
586 9d31a1d8 2018-03-11 stsp
587 9d31a1d8 2018-03-11 stsp /* Skip this tree if it is outside of our path prefix. */
588 9d31a1d8 2018-03-11 stsp len = MIN(strlen(worktree->path_prefix), strlen(path));
589 9d31a1d8 2018-03-11 stsp if (strncmp(path, worktree->path_prefix, len) != 0)
590 9d31a1d8 2018-03-11 stsp return NULL;
591 9d31a1d8 2018-03-11 stsp
592 883f0469 2018-06-23 stsp entries = got_object_tree_get_entries(tree);
593 883f0469 2018-06-23 stsp SIMPLEQ_FOREACH(te, &entries->head, entry) {
594 99437157 2018-11-11 stsp if (cancel_cb) {
595 99437157 2018-11-11 stsp err = (*cancel_cb)(cancel_arg);
596 99437157 2018-11-11 stsp if (err)
597 99437157 2018-11-11 stsp break;
598 99437157 2018-11-11 stsp }
599 92a684f4 2018-03-12 stsp err = tree_checkout_entry(worktree, fileindex, te, path, repo,
600 99437157 2018-11-11 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
601 9d31a1d8 2018-03-11 stsp if (err)
602 9d31a1d8 2018-03-11 stsp break;
603 9d31a1d8 2018-03-11 stsp }
604 9d31a1d8 2018-03-11 stsp
605 9d31a1d8 2018-03-11 stsp return err;
606 9d31a1d8 2018-03-11 stsp }
607 9d31a1d8 2018-03-11 stsp
608 9d31a1d8 2018-03-11 stsp const struct got_error *
609 9d31a1d8 2018-03-11 stsp got_worktree_checkout_files(struct got_worktree *worktree,
610 93a30277 2018-12-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
611 93a30277 2018-12-24 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
612 9d31a1d8 2018-03-11 stsp {
613 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL, *unlockerr;
614 9d31a1d8 2018-03-11 stsp struct got_object_id *commit_id = NULL;
615 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
616 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
617 9d31a1d8 2018-03-11 stsp char *fileindex_path = NULL, *new_fileindex_path = NULL;
618 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
619 b8bdcc21 2018-12-24 stsp FILE *new_index = NULL;
620 9d31a1d8 2018-03-11 stsp
621 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
622 9d31a1d8 2018-03-11 stsp if (err)
623 9d31a1d8 2018-03-11 stsp return err;
624 9d31a1d8 2018-03-11 stsp
625 93a30277 2018-12-24 stsp err = got_object_resolve_id_str(&commit_id, repo, worktree->base);
626 93a30277 2018-12-24 stsp if (err)
627 93a30277 2018-12-24 stsp goto done;
628 93a30277 2018-12-24 stsp
629 7426bbfd 2018-12-24 stsp fileindex = got_fileindex_alloc();
630 9d31a1d8 2018-03-11 stsp if (fileindex == NULL) {
631 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
632 9d31a1d8 2018-03-11 stsp goto done;
633 9d31a1d8 2018-03-11 stsp }
634 9d31a1d8 2018-03-11 stsp
635 9d31a1d8 2018-03-11 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
636 9d31a1d8 2018-03-11 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
637 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
638 9d31a1d8 2018-03-11 stsp fileindex_path = NULL;
639 9d31a1d8 2018-03-11 stsp goto done;
640 9d31a1d8 2018-03-11 stsp }
641 9d31a1d8 2018-03-11 stsp
642 b8bdcc21 2018-12-24 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
643 b8bdcc21 2018-12-24 stsp fileindex_path);
644 51664889 2018-03-12 stsp if (err)
645 51664889 2018-03-12 stsp goto done;
646 51664889 2018-03-12 stsp
647 93a30277 2018-12-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
648 9d31a1d8 2018-03-11 stsp if (err)
649 9d31a1d8 2018-03-11 stsp goto done;
650 9d31a1d8 2018-03-11 stsp
651 93a30277 2018-12-24 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
652 9d31a1d8 2018-03-11 stsp if (err)
653 9d31a1d8 2018-03-11 stsp goto done;
654 9d31a1d8 2018-03-11 stsp
655 92a684f4 2018-03-12 stsp err = tree_checkout(worktree, fileindex, tree, "/", repo,
656 99437157 2018-11-11 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
657 9d31a1d8 2018-03-11 stsp if (err)
658 9d31a1d8 2018-03-11 stsp goto done;
659 9d31a1d8 2018-03-11 stsp
660 b8bdcc21 2018-12-24 stsp err = got_fileindex_write(fileindex, new_index);
661 9d31a1d8 2018-03-11 stsp if (err)
662 9d31a1d8 2018-03-11 stsp goto done;
663 9d31a1d8 2018-03-11 stsp
664 9d31a1d8 2018-03-11 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
665 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
666 9d31a1d8 2018-03-11 stsp goto done;
667 9d31a1d8 2018-03-11 stsp }
668 9d31a1d8 2018-03-11 stsp
669 9d31a1d8 2018-03-11 stsp free(new_fileindex_path);
670 9d31a1d8 2018-03-11 stsp new_fileindex_path = NULL;
671 9d31a1d8 2018-03-11 stsp
672 9d31a1d8 2018-03-11 stsp done:
673 edfa7d7f 2018-09-11 stsp if (tree)
674 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
675 9d31a1d8 2018-03-11 stsp if (commit)
676 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
677 9d31a1d8 2018-03-11 stsp free(commit_id);
678 9d31a1d8 2018-03-11 stsp if (new_fileindex_path)
679 9d31a1d8 2018-03-11 stsp unlink(new_fileindex_path);
680 b8bdcc21 2018-12-24 stsp if (new_index)
681 b8bdcc21 2018-12-24 stsp fclose(new_index);
682 9d31a1d8 2018-03-11 stsp free(new_fileindex_path);
683 9d31a1d8 2018-03-11 stsp free(fileindex_path);
684 7426bbfd 2018-12-24 stsp got_fileindex_free(fileindex);
685 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
686 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
687 9d31a1d8 2018-03-11 stsp err = unlockerr;
688 9d31a1d8 2018-03-11 stsp return err;
689 9d31a1d8 2018-03-11 stsp }