File: | got/../lib/worktree.c |
Warning: | line 6792, column 18 Null pointer passed as 1st argument to string comparison function |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* | ||
2 | * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org> | ||
3 | * | ||
4 | * Permission to use, copy, modify, and distribute this software for any | ||
5 | * purpose with or without fee is hereby granted, provided that the above | ||
6 | * copyright notice and this permission notice appear in all copies. | ||
7 | * | ||
8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
15 | */ | ||
16 | |||
17 | #include <sys/stat.h> | ||
18 | #include <sys/queue.h> | ||
19 | #include <sys/tree.h> | ||
20 | |||
21 | #include <dirent.h> | ||
22 | #include <limits.h> | ||
23 | #include <stddef.h> | ||
24 | #include <string.h> | ||
25 | #include <stdio.h> | ||
26 | #include <stdlib.h> | ||
27 | #include <time.h> | ||
28 | #include <fcntl.h> | ||
29 | #include <errno(*__errno()).h> | ||
30 | #include <unistd.h> | ||
31 | #include <sha1.h> | ||
32 | #include <zlib.h> | ||
33 | #include <fnmatch.h> | ||
34 | #include <libgen.h> | ||
35 | #include <uuid.h> | ||
36 | #include <util.h> | ||
37 | |||
38 | #include "got_error.h" | ||
39 | #include "got_repository.h" | ||
40 | #include "got_reference.h" | ||
41 | #include "got_object.h" | ||
42 | #include "got_path.h" | ||
43 | #include "got_cancel.h" | ||
44 | #include "got_worktree.h" | ||
45 | #include "got_opentemp.h" | ||
46 | #include "got_diff.h" | ||
47 | |||
48 | #include "got_lib_worktree.h" | ||
49 | #include "got_lib_sha1.h" | ||
50 | #include "got_lib_fileindex.h" | ||
51 | #include "got_lib_inflate.h" | ||
52 | #include "got_lib_delta.h" | ||
53 | #include "got_lib_object.h" | ||
54 | #include "got_lib_object_parse.h" | ||
55 | #include "got_lib_object_create.h" | ||
56 | #include "got_lib_object_idset.h" | ||
57 | #include "got_lib_diff.h" | ||
58 | #include "got_lib_gotconfig.h" | ||
59 | |||
60 | #ifndef MIN | ||
61 | #define MIN(_a,_b)((_a) < (_b) ? (_a) : (_b)) ((_a) < (_b) ? (_a) : (_b)) | ||
62 | #endif | ||
63 | |||
64 | #define GOT_MERGE_LABEL_MERGED"merged change" "merged change" | ||
65 | #define GOT_MERGE_LABEL_BASE"3-way merge base" "3-way merge base" | ||
66 | |||
67 | static const struct got_error * | ||
68 | create_meta_file(const char *path_got, const char *name, const char *content) | ||
69 | { | ||
70 | const struct got_error *err = NULL((void*)0); | ||
71 | char *path; | ||
72 | |||
73 | if (asprintf(&path, "%s/%s", path_got, name) == -1) | ||
74 | return got_error_from_errno("asprintf"); | ||
75 | |||
76 | err = got_path_create_file(path, content); | ||
77 | free(path); | ||
78 | return err; | ||
79 | } | ||
80 | |||
81 | static const struct got_error * | ||
82 | update_meta_file(const char *path_got, const char *name, const char *content) | ||
83 | { | ||
84 | const struct got_error *err = NULL((void*)0); | ||
85 | FILE *tmpfile = NULL((void*)0); | ||
86 | char *tmppath = NULL((void*)0); | ||
87 | char *path = NULL((void*)0); | ||
88 | |||
89 | if (asprintf(&path, "%s/%s", path_got, name) == -1) { | ||
90 | err = got_error_from_errno("asprintf"); | ||
91 | path = NULL((void*)0); | ||
92 | goto done; | ||
93 | } | ||
94 | |||
95 | err = got_opentemp_named(&tmppath, &tmpfile, path); | ||
96 | if (err) | ||
97 | goto done; | ||
98 | |||
99 | if (content) { | ||
100 | int len = fprintf(tmpfile, "%s\n", content); | ||
101 | if (len != strlen(content) + 1) { | ||
102 | err = got_error_from_errno2("fprintf", tmppath); | ||
103 | goto done; | ||
104 | } | ||
105 | } | ||
106 | |||
107 | if (rename(tmppath, path) != 0) { | ||
108 | err = got_error_from_errno3("rename", tmppath, path); | ||
109 | unlink(tmppath); | ||
110 | goto done; | ||
111 | } | ||
112 | |||
113 | done: | ||
114 | if (fclose(tmpfile) == EOF(-1) && err == NULL((void*)0)) | ||
115 | err = got_error_from_errno2("fclose", tmppath); | ||
116 | free(tmppath); | ||
117 | return err; | ||
118 | } | ||
119 | |||
120 | static const struct got_error * | ||
121 | read_meta_file(char **content, const char *path_got, const char *name) | ||
122 | { | ||
123 | const struct got_error *err = NULL((void*)0); | ||
124 | char *path; | ||
125 | int fd = -1; | ||
126 | ssize_t n; | ||
127 | struct stat sb; | ||
128 | |||
129 | *content = NULL((void*)0); | ||
130 | |||
131 | if (asprintf(&path, "%s/%s", path_got, name) == -1) { | ||
132 | err = got_error_from_errno("asprintf"); | ||
133 | path = NULL((void*)0); | ||
134 | goto done; | ||
135 | } | ||
136 | |||
137 | fd = open(path, O_RDONLY0x0000 | O_NOFOLLOW0x0100); | ||
138 | if (fd == -1) { | ||
139 | if (errno(*__errno()) == ENOENT2) | ||
140 | err = got_error_path(path, GOT_ERR_WORKTREE_META27); | ||
141 | else | ||
142 | err = got_error_from_errno2("open", path); | ||
143 | goto done; | ||
144 | } | ||
145 | if (flock(fd, LOCK_SH0x01 | LOCK_NB0x04) == -1) { | ||
146 | err = (errno(*__errno()) == EWOULDBLOCK35 ? got_error(GOT_ERR_WORKTREE_BUSY29) | ||
147 | : got_error_from_errno2("flock", path)); | ||
148 | goto done; | ||
149 | } | ||
150 | |||
151 | if (fstat(fd, &sb) != 0) { | ||
152 | err = got_error_from_errno2("fstat", path); | ||
153 | goto done; | ||
154 | } | ||
155 | *content = calloc(1, sb.st_size); | ||
156 | if (*content == NULL((void*)0)) { | ||
157 | err = got_error_from_errno("calloc"); | ||
158 | goto done; | ||
159 | } | ||
160 | |||
161 | n = read(fd, *content, sb.st_size); | ||
162 | if (n != sb.st_size) { | ||
163 | err = (n == -1 ? got_error_from_errno2("read", path) : | ||
164 | got_error_path(path, GOT_ERR_WORKTREE_META27)); | ||
165 | goto done; | ||
166 | } | ||
167 | if ((*content)[sb.st_size - 1] != '\n') { | ||
168 | err = got_error_path(path, GOT_ERR_WORKTREE_META27); | ||
169 | goto done; | ||
170 | } | ||
171 | (*content)[sb.st_size - 1] = '\0'; | ||
172 | |||
173 | done: | ||
174 | if (fd != -1 && close(fd) == -1 && err == NULL((void*)0)) | ||
175 | err = got_error_from_errno2("close", path_got); | ||
176 | free(path); | ||
177 | if (err) { | ||
178 | free(*content); | ||
179 | *content = NULL((void*)0); | ||
180 | } | ||
181 | return err; | ||
182 | } | ||
183 | |||
184 | static const struct got_error * | ||
185 | write_head_ref(const char *path_got, struct got_reference *head_ref) | ||
186 | { | ||
187 | const struct got_error *err = NULL((void*)0); | ||
188 | char *refstr = NULL((void*)0); | ||
189 | |||
190 | if (got_ref_is_symbolic(head_ref)) { | ||
191 | refstr = got_ref_to_str(head_ref); | ||
192 | if (refstr == NULL((void*)0)) | ||
193 | return got_error_from_errno("got_ref_to_str"); | ||
194 | } else { | ||
195 | refstr = strdup(got_ref_get_name(head_ref)); | ||
196 | if (refstr == NULL((void*)0)) | ||
197 | return got_error_from_errno("strdup"); | ||
198 | } | ||
199 | err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF"head-ref", refstr); | ||
200 | free(refstr); | ||
201 | return err; | ||
202 | } | ||
203 | |||
204 | const struct got_error * | ||
205 | got_worktree_init(const char *path, struct got_reference *head_ref, | ||
206 | const char *prefix, struct got_repository *repo) | ||
207 | { | ||
208 | const struct got_error *err = NULL((void*)0); | ||
209 | struct got_object_id *commit_id = NULL((void*)0); | ||
210 | uuid_t uuid; | ||
211 | uint32_t uuid_status; | ||
212 | int obj_type; | ||
213 | char *path_got = NULL((void*)0); | ||
214 | char *formatstr = NULL((void*)0); | ||
215 | char *absprefix = NULL((void*)0); | ||
216 | char *basestr = NULL((void*)0); | ||
217 | char *uuidstr = NULL((void*)0); | ||
218 | |||
219 | if (strcmp(path, got_repo_get_path(repo)) == 0) { | ||
220 | err = got_error(GOT_ERR_WORKTREE_REPO66); | ||
221 | goto done; | ||
222 | } | ||
223 | |||
224 | err = got_ref_resolve(&commit_id, repo, head_ref); | ||
225 | if (err) | ||
226 | return err; | ||
227 | err = got_object_get_type(&obj_type, repo, commit_id); | ||
228 | if (err) | ||
229 | return err; | ||
230 | if (obj_type != GOT_OBJ_TYPE_COMMIT1) | ||
231 | return got_error(GOT_ERR_OBJ_TYPE11); | ||
232 | |||
233 | if (!got_path_is_absolute(prefix)) { | ||
234 | if (asprintf(&absprefix, "/%s", prefix) == -1) | ||
235 | return got_error_from_errno("asprintf"); | ||
236 | } | ||
237 | |||
238 | /* Create top-level directory (may already exist). */ | ||
239 | if (mkdir(path, GOT_DEFAULT_DIR_MODE(0040000 | 0000700 | 0000040|0000010 | 0000004|0000001)) == -1 && errno(*__errno()) != EEXIST17) { | ||
240 | err = got_error_from_errno2("mkdir", path); | ||
241 | goto done; | ||
242 | } | ||
243 | |||
244 | /* Create .got directory (may already exist). */ | ||
245 | if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR".got") == -1) { | ||
246 | err = got_error_from_errno("asprintf"); | ||
247 | goto done; | ||
248 | } | ||
249 | if (mkdir(path_got, GOT_DEFAULT_DIR_MODE(0040000 | 0000700 | 0000040|0000010 | 0000004|0000001)) == -1 && errno(*__errno()) != EEXIST17) { | ||
250 | err = got_error_from_errno2("mkdir", path_got); | ||
251 | goto done; | ||
252 | } | ||
253 | |||
254 | /* Create an empty lock file. */ | ||
255 | err = create_meta_file(path_got, GOT_WORKTREE_LOCK"lock", NULL((void*)0)); | ||
256 | if (err) | ||
257 | goto done; | ||
258 | |||
259 | /* Create an empty file index. */ | ||
260 | err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX"file-index", NULL((void*)0)); | ||
261 | if (err) | ||
262 | goto done; | ||
263 | |||
264 | /* Write the HEAD reference. */ | ||
265 | err = write_head_ref(path_got, head_ref); | ||
266 | if (err) | ||
267 | goto done; | ||
268 | |||
269 | /* Record our base commit. */ | ||
270 | err = got_object_id_str(&basestr, commit_id); | ||
271 | if (err) | ||
272 | goto done; | ||
273 | err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT"base-commit", basestr); | ||
274 | if (err) | ||
275 | goto done; | ||
276 | |||
277 | /* Store path to repository. */ | ||
278 | err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY"repository", | ||
279 | got_repo_get_path(repo)); | ||
280 | if (err) | ||
281 | goto done; | ||
282 | |||
283 | /* Store in-repository path prefix. */ | ||
284 | err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX"path-prefix", | ||
285 | absprefix ? absprefix : prefix); | ||
286 | if (err) | ||
287 | goto done; | ||
288 | |||
289 | /* Generate UUID. */ | ||
290 | uuid_create(&uuid, &uuid_status); | ||
291 | if (uuid_status != uuid_s_ok0) { | ||
292 | err = got_error_uuid(uuid_status, "uuid_create"); | ||
293 | goto done; | ||
294 | } | ||
295 | uuid_to_string(&uuid, &uuidstr, &uuid_status); | ||
296 | if (uuid_status != uuid_s_ok0) { | ||
297 | err = got_error_uuid(uuid_status, "uuid_to_string"); | ||
298 | goto done; | ||
299 | } | ||
300 | err = create_meta_file(path_got, GOT_WORKTREE_UUID"uuid", uuidstr); | ||
301 | if (err) | ||
302 | goto done; | ||
303 | |||
304 | /* Stamp work tree with format file. */ | ||
305 | if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION1) == -1) { | ||
306 | err = got_error_from_errno("asprintf"); | ||
307 | goto done; | ||
308 | } | ||
309 | err = create_meta_file(path_got, GOT_WORKTREE_FORMAT"format", formatstr); | ||
310 | if (err) | ||
311 | goto done; | ||
312 | |||
313 | done: | ||
314 | free(commit_id); | ||
315 | free(path_got); | ||
316 | free(formatstr); | ||
317 | free(absprefix); | ||
318 | free(basestr); | ||
319 | free(uuidstr); | ||
320 | return err; | ||
321 | } | ||
322 | |||
323 | static const struct got_error * | ||
324 | open_worktree(struct got_worktree **worktree, const char *path) | ||
325 | { | ||
326 | const struct got_error *err = NULL((void*)0); | ||
327 | char *path_got; | ||
328 | char *formatstr = NULL((void*)0); | ||
329 | char *uuidstr = NULL((void*)0); | ||
330 | char *path_lock = NULL((void*)0); | ||
331 | char *base_commit_id_str = NULL((void*)0); | ||
332 | int version, fd = -1; | ||
333 | const char *errstr; | ||
334 | struct got_repository *repo = NULL((void*)0); | ||
335 | uint32_t uuid_status; | ||
336 | |||
337 | *worktree = NULL((void*)0); | ||
338 | |||
339 | if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR".got") == -1) { | ||
340 | err = got_error_from_errno("asprintf"); | ||
341 | path_got = NULL((void*)0); | ||
342 | goto done; | ||
343 | } | ||
344 | |||
345 | if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK"lock") == -1) { | ||
346 | err = got_error_from_errno("asprintf"); | ||
347 | path_lock = NULL((void*)0); | ||
348 | goto done; | ||
349 | } | ||
350 | |||
351 | fd = open(path_lock, O_RDWR0x0002 | O_EXLOCK0x0020 | O_NONBLOCK0x0004); | ||
352 | if (fd == -1) { | ||
353 | err = (errno(*__errno()) == EWOULDBLOCK35 ? got_error(GOT_ERR_WORKTREE_BUSY29) | ||
354 | : got_error_from_errno2("open", path_lock)); | ||
355 | goto done; | ||
356 | } | ||
357 | |||
358 | err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT"format"); | ||
359 | if (err) | ||
360 | goto done; | ||
361 | |||
362 | version = strtonum(formatstr, 1, INT_MAX2147483647, &errstr); | ||
363 | if (errstr) { | ||
364 | err = got_error_msg(GOT_ERR_WORKTREE_META27, | ||
365 | "could not parse work tree format version number"); | ||
366 | goto done; | ||
367 | } | ||
368 | if (version != GOT_WORKTREE_FORMAT_VERSION1) { | ||
369 | err = got_error(GOT_ERR_WORKTREE_VERS28); | ||
370 | goto done; | ||
371 | } | ||
372 | |||
373 | *worktree = calloc(1, sizeof(**worktree)); | ||
374 | if (*worktree == NULL((void*)0)) { | ||
375 | err = got_error_from_errno("calloc"); | ||
376 | goto done; | ||
377 | } | ||
378 | (*worktree)->lockfd = -1; | ||
379 | |||
380 | (*worktree)->root_path = realpath(path, NULL((void*)0)); | ||
381 | if ((*worktree)->root_path == NULL((void*)0)) { | ||
382 | err = got_error_from_errno2("realpath", path); | ||
383 | goto done; | ||
384 | } | ||
385 | err = read_meta_file(&(*worktree)->repo_path, path_got, | ||
386 | GOT_WORKTREE_REPOSITORY"repository"); | ||
387 | if (err) | ||
388 | goto done; | ||
389 | |||
390 | err = read_meta_file(&(*worktree)->path_prefix, path_got, | ||
391 | GOT_WORKTREE_PATH_PREFIX"path-prefix"); | ||
392 | if (err) | ||
393 | goto done; | ||
394 | |||
395 | err = read_meta_file(&base_commit_id_str, path_got, | ||
396 | GOT_WORKTREE_BASE_COMMIT"base-commit"); | ||
397 | if (err) | ||
398 | goto done; | ||
399 | |||
400 | err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID"uuid"); | ||
401 | if (err) | ||
402 | goto done; | ||
403 | uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status); | ||
404 | if (uuid_status != uuid_s_ok0) { | ||
405 | err = got_error_uuid(uuid_status, "uuid_from_string"); | ||
406 | goto done; | ||
407 | } | ||
408 | |||
409 | err = got_repo_open(&repo, (*worktree)->repo_path, NULL((void*)0)); | ||
410 | if (err) | ||
411 | goto done; | ||
412 | |||
413 | err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo, | ||
414 | base_commit_id_str); | ||
415 | if (err) | ||
416 | goto done; | ||
417 | |||
418 | err = read_meta_file(&(*worktree)->head_ref_name, path_got, | ||
419 | GOT_WORKTREE_HEAD_REF"head-ref"); | ||
420 | if (err) | ||
421 | goto done; | ||
422 | |||
423 | if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s", | ||
424 | (*worktree)->root_path, | ||
425 | GOT_WORKTREE_GOT_DIR".got", GOT_GOTCONFIG_FILENAME"got.conf") == -1) { | ||
426 | err = got_error_from_errno("asprintf"); | ||
427 | goto done; | ||
428 | } | ||
429 | |||
430 | err = got_gotconfig_read(&(*worktree)->gotconfig, | ||
431 | (*worktree)->gotconfig_path); | ||
432 | |||
433 | (*worktree)->root_fd = open((*worktree)->root_path, O_DIRECTORY0x20000); | ||
434 | if ((*worktree)->root_fd == -1) { | ||
435 | err = got_error_from_errno2("open", (*worktree)->root_path); | ||
436 | goto done; | ||
437 | } | ||
438 | done: | ||
439 | if (repo) | ||
440 | got_repo_close(repo); | ||
441 | free(path_got); | ||
442 | free(path_lock); | ||
443 | free(base_commit_id_str); | ||
444 | free(uuidstr); | ||
445 | free(formatstr); | ||
446 | if (err) { | ||
447 | if (fd != -1) | ||
448 | close(fd); | ||
449 | if (*worktree != NULL((void*)0)) | ||
450 | got_worktree_close(*worktree); | ||
451 | *worktree = NULL((void*)0); | ||
452 | } else | ||
453 | (*worktree)->lockfd = fd; | ||
454 | |||
455 | return err; | ||
456 | } | ||
457 | |||
458 | const struct got_error * | ||
459 | got_worktree_open(struct got_worktree **worktree, const char *path) | ||
460 | { | ||
461 | const struct got_error *err = NULL((void*)0); | ||
462 | char *worktree_path; | ||
463 | |||
464 | worktree_path = strdup(path); | ||
465 | if (worktree_path == NULL((void*)0)) | ||
466 | return got_error_from_errno("strdup"); | ||
467 | |||
468 | for (;;) { | ||
469 | char *parent_path; | ||
470 | |||
471 | err = open_worktree(worktree, worktree_path); | ||
472 | if (err && !(err->code == GOT_ERR_ERRNO1 && errno(*__errno()) == ENOENT2)) { | ||
473 | free(worktree_path); | ||
474 | return err; | ||
475 | } | ||
476 | if (*worktree) { | ||
477 | free(worktree_path); | ||
478 | return NULL((void*)0); | ||
479 | } | ||
480 | if (worktree_path[0] == '/' && worktree_path[1] == '\0') | ||
481 | break; | ||
482 | err = got_path_dirname(&parent_path, worktree_path); | ||
483 | if (err) { | ||
484 | if (err->code != GOT_ERR_BAD_PATH4) { | ||
485 | free(worktree_path); | ||
486 | return err; | ||
487 | } | ||
488 | break; | ||
489 | } | ||
490 | free(worktree_path); | ||
491 | worktree_path = parent_path; | ||
492 | } | ||
493 | |||
494 | free(worktree_path); | ||
495 | return got_error(GOT_ERR_NOT_WORKTREE60); | ||
496 | } | ||
497 | |||
498 | const struct got_error * | ||
499 | got_worktree_close(struct got_worktree *worktree) | ||
500 | { | ||
501 | const struct got_error *err = NULL((void*)0); | ||
502 | |||
503 | if (worktree->lockfd != -1) { | ||
504 | if (close(worktree->lockfd) == -1) | ||
505 | err = got_error_from_errno2("close", | ||
506 | got_worktree_get_root_path(worktree)); | ||
507 | } | ||
508 | if (close(worktree->root_fd) == -1 && err == NULL((void*)0)) | ||
509 | err = got_error_from_errno2("close", | ||
510 | got_worktree_get_root_path(worktree)); | ||
511 | free(worktree->repo_path); | ||
512 | free(worktree->path_prefix); | ||
513 | free(worktree->base_commit_id); | ||
514 | free(worktree->head_ref_name); | ||
515 | free(worktree->root_path); | ||
516 | free(worktree->gotconfig_path); | ||
517 | got_gotconfig_free(worktree->gotconfig); | ||
518 | free(worktree); | ||
519 | return err; | ||
520 | } | ||
521 | |||
522 | const char * | ||
523 | got_worktree_get_root_path(struct got_worktree *worktree) | ||
524 | { | ||
525 | return worktree->root_path; | ||
526 | } | ||
527 | |||
528 | const char * | ||
529 | got_worktree_get_repo_path(struct got_worktree *worktree) | ||
530 | { | ||
531 | return worktree->repo_path; | ||
532 | } | ||
533 | const char * | ||
534 | got_worktree_get_path_prefix(struct got_worktree *worktree) | ||
535 | { | ||
536 | return worktree->path_prefix; | ||
537 | } | ||
538 | |||
539 | const struct got_error * | ||
540 | got_worktree_match_path_prefix(int *match, struct got_worktree *worktree, | ||
541 | const char *path_prefix) | ||
542 | { | ||
543 | char *absprefix = NULL((void*)0); | ||
544 | |||
545 | if (!got_path_is_absolute(path_prefix)) { | ||
546 | if (asprintf(&absprefix, "/%s", path_prefix) == -1) | ||
547 | return got_error_from_errno("asprintf"); | ||
548 | } | ||
549 | *match = (strcmp(absprefix ? absprefix : path_prefix, | ||
550 | worktree->path_prefix) == 0); | ||
551 | free(absprefix); | ||
552 | return NULL((void*)0); | ||
553 | } | ||
554 | |||
555 | const char * | ||
556 | got_worktree_get_head_ref_name(struct got_worktree *worktree) | ||
557 | { | ||
558 | return worktree->head_ref_name; | ||
559 | } | ||
560 | |||
561 | const struct got_error * | ||
562 | got_worktree_set_head_ref(struct got_worktree *worktree, | ||
563 | struct got_reference *head_ref) | ||
564 | { | ||
565 | const struct got_error *err = NULL((void*)0); | ||
566 | char *path_got = NULL((void*)0), *head_ref_name = NULL((void*)0); | ||
567 | |||
568 | if (asprintf(&path_got, "%s/%s", worktree->root_path, | ||
569 | GOT_WORKTREE_GOT_DIR".got") == -1) { | ||
570 | err = got_error_from_errno("asprintf"); | ||
571 | path_got = NULL((void*)0); | ||
572 | goto done; | ||
573 | } | ||
574 | |||
575 | head_ref_name = strdup(got_ref_get_name(head_ref)); | ||
576 | if (head_ref_name == NULL((void*)0)) { | ||
577 | err = got_error_from_errno("strdup"); | ||
578 | goto done; | ||
579 | } | ||
580 | |||
581 | err = write_head_ref(path_got, head_ref); | ||
582 | if (err) | ||
583 | goto done; | ||
584 | |||
585 | free(worktree->head_ref_name); | ||
586 | worktree->head_ref_name = head_ref_name; | ||
587 | done: | ||
588 | free(path_got); | ||
589 | if (err) | ||
590 | free(head_ref_name); | ||
591 | return err; | ||
592 | } | ||
593 | |||
594 | struct got_object_id * | ||
595 | got_worktree_get_base_commit_id(struct got_worktree *worktree) | ||
596 | { | ||
597 | return worktree->base_commit_id; | ||
598 | } | ||
599 | |||
600 | const struct got_error * | ||
601 | got_worktree_set_base_commit_id(struct got_worktree *worktree, | ||
602 | struct got_repository *repo, struct got_object_id *commit_id) | ||
603 | { | ||
604 | const struct got_error *err; | ||
605 | struct got_object *obj = NULL((void*)0); | ||
606 | char *id_str = NULL((void*)0); | ||
607 | char *path_got = NULL((void*)0); | ||
608 | |||
609 | if (asprintf(&path_got, "%s/%s", worktree->root_path, | ||
610 | GOT_WORKTREE_GOT_DIR".got") == -1) { | ||
611 | err = got_error_from_errno("asprintf"); | ||
612 | path_got = NULL((void*)0); | ||
613 | goto done; | ||
614 | } | ||
615 | |||
616 | err = got_object_open(&obj, repo, commit_id); | ||
617 | if (err) | ||
618 | return err; | ||
619 | |||
620 | if (obj->type != GOT_OBJ_TYPE_COMMIT1) { | ||
621 | err = got_error(GOT_ERR_OBJ_TYPE11); | ||
622 | goto done; | ||
623 | } | ||
624 | |||
625 | /* Record our base commit. */ | ||
626 | err = got_object_id_str(&id_str, commit_id); | ||
627 | if (err) | ||
628 | goto done; | ||
629 | err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT"base-commit", id_str); | ||
630 | if (err) | ||
631 | goto done; | ||
632 | |||
633 | free(worktree->base_commit_id); | ||
634 | worktree->base_commit_id = got_object_id_dup(commit_id); | ||
635 | if (worktree->base_commit_id == NULL((void*)0)) { | ||
636 | err = got_error_from_errno("got_object_id_dup"); | ||
637 | goto done; | ||
638 | } | ||
639 | done: | ||
640 | if (obj) | ||
641 | got_object_close(obj); | ||
642 | free(id_str); | ||
643 | free(path_got); | ||
644 | return err; | ||
645 | } | ||
646 | |||
647 | const struct got_gotconfig * | ||
648 | got_worktree_get_gotconfig(struct got_worktree *worktree) | ||
649 | { | ||
650 | return worktree->gotconfig; | ||
651 | } | ||
652 | |||
653 | static const struct got_error * | ||
654 | lock_worktree(struct got_worktree *worktree, int operation) | ||
655 | { | ||
656 | if (flock(worktree->lockfd, operation | LOCK_NB0x04) == -1) | ||
657 | return (errno(*__errno()) == EWOULDBLOCK35 ? got_error(GOT_ERR_WORKTREE_BUSY29) | ||
658 | : got_error_from_errno2("flock", | ||
659 | got_worktree_get_root_path(worktree))); | ||
660 | return NULL((void*)0); | ||
661 | } | ||
662 | |||
663 | static const struct got_error * | ||
664 | add_dir_on_disk(struct got_worktree *worktree, const char *path) | ||
665 | { | ||
666 | const struct got_error *err = NULL((void*)0); | ||
667 | char *abspath; | ||
668 | |||
669 | if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1) | ||
670 | return got_error_from_errno("asprintf"); | ||
671 | |||
672 | err = got_path_mkdir(abspath); | ||
673 | if (err && err->code == GOT_ERR_ERRNO1 && errno(*__errno()) == EEXIST17) { | ||
674 | struct stat sb; | ||
675 | err = NULL((void*)0); | ||
676 | if (lstat(abspath, &sb) == -1) { | ||
677 | err = got_error_from_errno2("lstat", abspath); | ||
678 | } else if (!S_ISDIR(sb.st_mode)((sb.st_mode & 0170000) == 0040000)) { | ||
679 | /* TODO directory is obstructed; do something */ | ||
680 | err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED31); | ||
681 | } | ||
682 | } | ||
683 | free(abspath); | ||
684 | return err; | ||
685 | } | ||
686 | |||
687 | static const struct got_error * | ||
688 | check_file_contents_equal(int *same, FILE *f1, FILE *f2) | ||
689 | { | ||
690 | const struct got_error *err = NULL((void*)0); | ||
691 | uint8_t fbuf1[8192]; | ||
692 | uint8_t fbuf2[8192]; | ||
693 | size_t flen1 = 0, flen2 = 0; | ||
694 | |||
695 | *same = 1; | ||
696 | |||
697 | for (;;) { | ||
698 | flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1); | ||
699 | if (flen1 == 0 && ferror(f1)(!__isthreaded ? (((f1)->_flags & 0x0040) != 0) : (ferror )(f1))) { | ||
700 | err = got_error_from_errno("fread"); | ||
701 | break; | ||
702 | } | ||
703 | flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2); | ||
704 | if (flen2 == 0 && ferror(f2)(!__isthreaded ? (((f2)->_flags & 0x0040) != 0) : (ferror )(f2))) { | ||
705 | err = got_error_from_errno("fread"); | ||
706 | break; | ||
707 | } | ||
708 | if (flen1 == 0) { | ||
709 | if (flen2 != 0) | ||
710 | *same = 0; | ||
711 | break; | ||
712 | } else if (flen2 == 0) { | ||
713 | if (flen1 != 0) | ||
714 | *same = 0; | ||
715 | break; | ||
716 | } else if (flen1 == flen2) { | ||
717 | if (memcmp(fbuf1, fbuf2, flen2) != 0) { | ||
718 | *same = 0; | ||
719 | break; | ||
720 | } | ||
721 | } else { | ||
722 | *same = 0; | ||
723 | break; | ||
724 | } | ||
725 | } | ||
726 | |||
727 | return err; | ||
728 | } | ||
729 | |||
730 | static const struct got_error * | ||
731 | check_files_equal(int *same, const char *f1_path, const char *f2_path) | ||
732 | { | ||
733 | const struct got_error *err = NULL((void*)0); | ||
734 | struct stat sb; | ||
735 | size_t size1, size2; | ||
736 | FILE *f1 = NULL((void*)0), *f2 = NULL((void*)0); | ||
737 | |||
738 | *same = 1; | ||
739 | |||
740 | if (lstat(f1_path, &sb) != 0) { | ||
741 | err = got_error_from_errno2("lstat", f1_path); | ||
742 | goto done; | ||
743 | } | ||
744 | size1 = sb.st_size; | ||
745 | |||
746 | if (lstat(f2_path, &sb) != 0) { | ||
747 | err = got_error_from_errno2("lstat", f2_path); | ||
748 | goto done; | ||
749 | } | ||
750 | size2 = sb.st_size; | ||
751 | |||
752 | if (size1 != size2) { | ||
753 | *same = 0; | ||
754 | return NULL((void*)0); | ||
755 | } | ||
756 | |||
757 | f1 = fopen(f1_path, "r"); | ||
758 | if (f1 == NULL((void*)0)) | ||
759 | return got_error_from_errno2("fopen", f1_path); | ||
760 | |||
761 | f2 = fopen(f2_path, "r"); | ||
762 | if (f2 == NULL((void*)0)) { | ||
763 | err = got_error_from_errno2("fopen", f2_path); | ||
764 | goto done; | ||
765 | } | ||
766 | |||
767 | err = check_file_contents_equal(same, f1, f2); | ||
768 | done: | ||
769 | if (f1 && fclose(f1) == EOF(-1) && err == NULL((void*)0)) | ||
770 | err = got_error_from_errno("fclose"); | ||
771 | if (f2 && fclose(f2) == EOF(-1) && err == NULL((void*)0)) | ||
772 | err = got_error_from_errno("fclose"); | ||
773 | |||
774 | return err; | ||
775 | } | ||
776 | |||
777 | /* | ||
778 | * Perform a 3-way merge where blob_orig acts as the common ancestor, | ||
779 | * the file at deriv_path acts as the first derived version, and the | ||
780 | * file on disk acts as the second derived version. | ||
781 | */ | ||
782 | static const struct got_error * | ||
783 | merge_file(int *local_changes_subsumed, struct got_worktree *worktree, | ||
784 | struct got_blob_object *blob_orig, const char *ondisk_path, | ||
785 | const char *path, uint16_t st_mode, const char *deriv_path, | ||
786 | const char *label_orig, const char *label_deriv, | ||
787 | struct got_repository *repo, | ||
788 | got_worktree_checkout_cb progress_cb, void *progress_arg) | ||
789 | { | ||
790 | const struct got_error *err = NULL((void*)0); | ||
791 | int merged_fd = -1; | ||
792 | FILE *f_orig = NULL((void*)0); | ||
793 | char *blob_orig_path = NULL((void*)0); | ||
794 | char *merged_path = NULL((void*)0), *base_path = NULL((void*)0); | ||
795 | int overlapcnt = 0; | ||
796 | char *parent = NULL((void*)0); | ||
797 | char *symlink_path = NULL((void*)0); | ||
798 | FILE *symlinkf = NULL((void*)0); | ||
799 | |||
800 | *local_changes_subsumed = 0; | ||
801 | |||
802 | err = got_path_dirname(&parent, ondisk_path); | ||
803 | if (err) | ||
804 | return err; | ||
805 | |||
806 | if (asprintf(&base_path, "%s/got-merged", parent) == -1) { | ||
807 | err = got_error_from_errno("asprintf"); | ||
808 | goto done; | ||
809 | } | ||
810 | |||
811 | err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path); | ||
812 | if (err) | ||
813 | goto done; | ||
814 | |||
815 | free(base_path); | ||
816 | if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) { | ||
817 | err = got_error_from_errno("asprintf"); | ||
818 | base_path = NULL((void*)0); | ||
819 | goto done; | ||
820 | } | ||
821 | |||
822 | err = got_opentemp_named(&blob_orig_path, &f_orig, base_path); | ||
823 | if (err) | ||
824 | goto done; | ||
825 | if (blob_orig) { | ||
826 | err = got_object_blob_dump_to_file(NULL((void*)0), NULL((void*)0), NULL((void*)0), f_orig, | ||
827 | blob_orig); | ||
828 | if (err) | ||
829 | goto done; | ||
830 | } else { | ||
831 | /* | ||
832 | * If the file has no blob, this is an "add vs add" conflict, | ||
833 | * and we simply use an empty ancestor file to make both files | ||
834 | * appear in the merged result in their entirety. | ||
835 | */ | ||
836 | } | ||
837 | |||
838 | /* | ||
839 | * In order the run a 3-way merge with a symlink we copy the symlink's | ||
840 | * target path into a temporary file and use that file with diff3. | ||
841 | */ | ||
842 | if (S_ISLNK(st_mode)((st_mode & 0170000) == 0120000)) { | ||
843 | char target_path[PATH_MAX1024]; | ||
844 | ssize_t target_len; | ||
845 | size_t n; | ||
846 | |||
847 | free(base_path); | ||
848 | if (asprintf(&base_path, "%s/got-symlink-merge", | ||
849 | parent) == -1) { | ||
850 | err = got_error_from_errno("asprintf"); | ||
851 | base_path = NULL((void*)0); | ||
852 | goto done; | ||
853 | } | ||
854 | err = got_opentemp_named(&symlink_path, &symlinkf, base_path); | ||
855 | if (err) | ||
856 | goto done; | ||
857 | target_len = readlink(ondisk_path, target_path, | ||
858 | sizeof(target_path)); | ||
859 | if (target_len == -1) { | ||
860 | err = got_error_from_errno2("readlink", ondisk_path); | ||
861 | goto done; | ||
862 | } | ||
863 | n = fwrite(target_path, 1, target_len, symlinkf); | ||
864 | if (n != target_len) { | ||
865 | err = got_ferror(symlinkf, GOT_ERR_IO6); | ||
866 | goto done; | ||
867 | } | ||
868 | if (fflush(symlinkf) == EOF(-1)) { | ||
869 | err = got_error_from_errno2("fflush", symlink_path); | ||
870 | goto done; | ||
871 | } | ||
872 | } | ||
873 | |||
874 | err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path, | ||
875 | blob_orig_path, symlink_path ? symlink_path : ondisk_path, | ||
876 | label_deriv, label_orig, NULL((void*)0)); | ||
877 | if (err) | ||
878 | goto done; | ||
879 | |||
880 | err = (*progress_cb)(progress_arg, | ||
881 | overlapcnt > 0 ? GOT_STATUS_CONFLICT'C' : GOT_STATUS_MERGE'G', path); | ||
882 | if (err) | ||
883 | goto done; | ||
884 | |||
885 | if (fsync(merged_fd) != 0) { | ||
886 | err = got_error_from_errno("fsync"); | ||
887 | goto done; | ||
888 | } | ||
889 | |||
890 | /* Check if a clean merge has subsumed all local changes. */ | ||
891 | if (overlapcnt == 0) { | ||
892 | err = check_files_equal(local_changes_subsumed, deriv_path, | ||
893 | merged_path); | ||
894 | if (err) | ||
895 | goto done; | ||
896 | } | ||
897 | |||
898 | if (fchmod(merged_fd, st_mode) != 0) { | ||
899 | err = got_error_from_errno2("fchmod", merged_path); | ||
900 | goto done; | ||
901 | } | ||
902 | |||
903 | if (rename(merged_path, ondisk_path) != 0) { | ||
904 | err = got_error_from_errno3("rename", merged_path, | ||
905 | ondisk_path); | ||
906 | goto done; | ||
907 | } | ||
908 | done: | ||
909 | if (err) { | ||
910 | if (merged_path) | ||
911 | unlink(merged_path); | ||
912 | } | ||
913 | if (symlink_path) { | ||
914 | if (unlink(symlink_path) == -1 && err == NULL((void*)0)) | ||
915 | err = got_error_from_errno2("unlink", symlink_path); | ||
916 | } | ||
917 | if (symlinkf && fclose(symlinkf) == EOF(-1) && err == NULL((void*)0)) | ||
918 | err = got_error_from_errno2("fclose", symlink_path); | ||
919 | free(symlink_path); | ||
920 | if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL((void*)0)) | ||
921 | err = got_error_from_errno("close"); | ||
922 | if (f_orig && fclose(f_orig) == EOF(-1) && err == NULL((void*)0)) | ||
923 | err = got_error_from_errno("fclose"); | ||
924 | free(merged_path); | ||
925 | free(base_path); | ||
926 | if (blob_orig_path) { | ||
927 | unlink(blob_orig_path); | ||
928 | free(blob_orig_path); | ||
929 | } | ||
930 | free(parent); | ||
931 | return err; | ||
932 | } | ||
933 | |||
934 | static const struct got_error * | ||
935 | update_symlink(const char *ondisk_path, const char *target_path, | ||
936 | size_t target_len) | ||
937 | { | ||
938 | /* This is not atomic but matches what 'ln -sf' does. */ | ||
939 | if (unlink(ondisk_path) == -1) | ||
940 | return got_error_from_errno2("unlink", ondisk_path); | ||
941 | if (symlink(target_path, ondisk_path) == -1) | ||
942 | return got_error_from_errno3("symlink", target_path, | ||
943 | ondisk_path); | ||
944 | return NULL((void*)0); | ||
945 | } | ||
946 | |||
947 | /* | ||
948 | * Overwrite a symlink (or a regular file in case there was a "bad" symlink) | ||
949 | * in the work tree with a file that contains conflict markers and the | ||
950 | * conflicting target paths of the original version, a "derived version" | ||
951 | * of a symlink from an incoming change, and a local version of the symlink. | ||
952 | * | ||
953 | * The original versions's target path can be NULL if it is not available, | ||
954 | * such as if both derived versions added a new symlink at the same path. | ||
955 | * | ||
956 | * The incoming derived symlink target is NULL in case the incoming change | ||
957 | * has deleted this symlink. | ||
958 | */ | ||
959 | static const struct got_error * | ||
960 | install_symlink_conflict(const char *deriv_target, | ||
961 | struct got_object_id *deriv_base_commit_id, const char *orig_target, | ||
962 | const char *label_orig, const char *local_target, const char *ondisk_path) | ||
963 | { | ||
964 | const struct got_error *err; | ||
965 | char *id_str = NULL((void*)0), *label_deriv = NULL((void*)0), *path = NULL((void*)0); | ||
966 | FILE *f = NULL((void*)0); | ||
967 | |||
968 | err = got_object_id_str(&id_str, deriv_base_commit_id); | ||
969 | if (err) | ||
970 | return got_error_from_errno("asprintf"); | ||
971 | |||
972 | if (asprintf(&label_deriv, "%s: commit %s", | ||
973 | GOT_MERGE_LABEL_MERGED"merged change", id_str) == -1) { | ||
974 | err = got_error_from_errno("asprintf"); | ||
975 | goto done; | ||
976 | } | ||
977 | |||
978 | err = got_opentemp_named(&path, &f, "got-symlink-conflict"); | ||
979 | if (err) | ||
980 | goto done; | ||
981 | |||
982 | if (fchmod(fileno(f)(!__isthreaded ? ((f)->_file) : (fileno)(f)), GOT_DEFAULT_FILE_MODE(0100000 | 0000400|0000200 | 0000040 | 0000004)) == -1) { | ||
983 | err = got_error_from_errno2("fchmod", path); | ||
984 | goto done; | ||
985 | } | ||
986 | |||
987 | if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n", | ||
988 | GOT_DIFF_CONFLICT_MARKER_BEGIN"<<<<<<<", label_deriv, | ||
989 | deriv_target ? deriv_target : "(symlink was deleted)", | ||
990 | orig_target ? label_orig : "", | ||
991 | orig_target ? "\n" : "", | ||
992 | orig_target ? orig_target : "", | ||
993 | orig_target ? "\n" : "", | ||
994 | GOT_DIFF_CONFLICT_MARKER_SEP"=======", | ||
995 | local_target, GOT_DIFF_CONFLICT_MARKER_END">>>>>>>") < 0) { | ||
996 | err = got_error_from_errno2("fprintf", path); | ||
997 | goto done; | ||
998 | } | ||
999 | |||
1000 | if (unlink(ondisk_path) == -1) { | ||
1001 | err = got_error_from_errno2("unlink", ondisk_path); | ||
1002 | goto done; | ||
1003 | } | ||
1004 | if (rename(path, ondisk_path) == -1) { | ||
1005 | err = got_error_from_errno3("rename", path, ondisk_path); | ||
1006 | goto done; | ||
1007 | } | ||
1008 | done: | ||
1009 | if (f != NULL((void*)0) && fclose(f) == EOF(-1) && err == NULL((void*)0)) | ||
1010 | err = got_error_from_errno2("fclose", path); | ||
1011 | free(path); | ||
1012 | free(id_str); | ||
1013 | free(label_deriv); | ||
1014 | return err; | ||
1015 | } | ||
1016 | |||
1017 | /* forward declaration */ | ||
1018 | static const struct got_error * | ||
1019 | merge_blob(int *, struct got_worktree *, struct got_blob_object *, | ||
1020 | const char *, const char *, uint16_t, const char *, | ||
1021 | struct got_blob_object *, struct got_object_id *, | ||
1022 | struct got_repository *, got_worktree_checkout_cb, void *); | ||
1023 | |||
1024 | /* | ||
1025 | * Merge a symlink into the work tree, where blob_orig acts as the common | ||
1026 | * ancestor, deriv_target is the link target of the first derived version, | ||
1027 | * and the symlink on disk acts as the second derived version. | ||
1028 | * Assume that contents of both blobs represent symlinks. | ||
1029 | */ | ||
1030 | static const struct got_error * | ||
1031 | merge_symlink(struct got_worktree *worktree, | ||
1032 | struct got_blob_object *blob_orig, const char *ondisk_path, | ||
1033 | const char *path, const char *label_orig, const char *deriv_target, | ||
1034 | struct got_object_id *deriv_base_commit_id, struct got_repository *repo, | ||
1035 | got_worktree_checkout_cb progress_cb, void *progress_arg) | ||
1036 | { | ||
1037 | const struct got_error *err = NULL((void*)0); | ||
1038 | char *ancestor_target = NULL((void*)0); | ||
1039 | struct stat sb; | ||
1040 | ssize_t ondisk_len, deriv_len; | ||
1041 | char ondisk_target[PATH_MAX1024]; | ||
1042 | int have_local_change = 0; | ||
1043 | int have_incoming_change = 0; | ||
1044 | |||
1045 | if (lstat(ondisk_path, &sb) == -1) | ||
1046 | return got_error_from_errno2("lstat", ondisk_path); | ||
1047 | |||
1048 | ondisk_len = readlink(ondisk_path, ondisk_target, | ||
1049 | sizeof(ondisk_target)); | ||
1050 | if (ondisk_len == -1) { | ||
1051 | err = got_error_from_errno2("readlink", | ||
1052 | ondisk_path); | ||
1053 | goto done; | ||
1054 | } | ||
1055 | ondisk_target[ondisk_len] = '\0'; | ||
1056 | |||
1057 | if (blob_orig) { | ||
1058 | err = got_object_blob_read_to_str(&ancestor_target, blob_orig); | ||
1059 | if (err) | ||
1060 | goto done; | ||
1061 | } | ||
1062 | |||
1063 | if (ancestor_target == NULL((void*)0) || | ||
1064 | (ondisk_len != strlen(ancestor_target) || | ||
1065 | memcmp(ondisk_target, ancestor_target, ondisk_len) != 0)) | ||
1066 | have_local_change = 1; | ||
1067 | |||
1068 | deriv_len = strlen(deriv_target); | ||
1069 | if (ancestor_target == NULL((void*)0) || | ||
1070 | (deriv_len != strlen(ancestor_target) || | ||
1071 | memcmp(deriv_target, ancestor_target, deriv_len) != 0)) | ||
1072 | have_incoming_change = 1; | ||
1073 | |||
1074 | if (!have_local_change && !have_incoming_change) { | ||
1075 | if (ancestor_target) { | ||
1076 | /* Both sides made the same change. */ | ||
1077 | err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE'G', | ||
1078 | path); | ||
1079 | } else if (deriv_len == ondisk_len && | ||
1080 | memcmp(ondisk_target, deriv_target, deriv_len) == 0) { | ||
1081 | /* Both sides added the same symlink. */ | ||
1082 | err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE'G', | ||
1083 | path); | ||
1084 | } else { | ||
1085 | /* Both sides added symlinks which don't match. */ | ||
1086 | err = install_symlink_conflict(deriv_target, | ||
1087 | deriv_base_commit_id, ancestor_target, | ||
1088 | label_orig, ondisk_target, ondisk_path); | ||
1089 | if (err) | ||
1090 | goto done; | ||
1091 | err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT'C', | ||
1092 | path); | ||
1093 | } | ||
1094 | } else if (!have_local_change && have_incoming_change) { | ||
1095 | /* Apply the incoming change. */ | ||
1096 | err = update_symlink(ondisk_path, deriv_target, | ||
1097 | strlen(deriv_target)); | ||
1098 | if (err) | ||
1099 | goto done; | ||
1100 | err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE'G', path); | ||
1101 | } else if (have_local_change && have_incoming_change) { | ||
1102 | if (deriv_len == ondisk_len && | ||
1103 | memcmp(deriv_target, ondisk_target, deriv_len) == 0) { | ||
1104 | /* Both sides made the same change. */ | ||
1105 | err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE'G', | ||
1106 | path); | ||
1107 | } else { | ||
1108 | err = install_symlink_conflict(deriv_target, | ||
1109 | deriv_base_commit_id, ancestor_target, label_orig, | ||
1110 | ondisk_target, ondisk_path); | ||
1111 | if (err) | ||
1112 | goto done; | ||
1113 | err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT'C', | ||
1114 | path); | ||
1115 | } | ||
1116 | } | ||
1117 | |||
1118 | done: | ||
1119 | free(ancestor_target); | ||
1120 | return err; | ||
1121 | } | ||
1122 | |||
1123 | /* | ||
1124 | * Perform a 3-way merge where blob_orig acts as the common ancestor, | ||
1125 | * blob_deriv acts as the first derived version, and the file on disk | ||
1126 | * acts as the second derived version. | ||
1127 | */ | ||
1128 | static const struct got_error * | ||
1129 | merge_blob(int *local_changes_subsumed, struct got_worktree *worktree, | ||
1130 | struct got_blob_object *blob_orig, const char *ondisk_path, | ||
1131 | const char *path, uint16_t st_mode, const char *label_orig, | ||
1132 | struct got_blob_object *blob_deriv, | ||
1133 | struct got_object_id *deriv_base_commit_id, struct got_repository *repo, | ||
1134 | got_worktree_checkout_cb progress_cb, void *progress_arg) | ||
1135 | { | ||
1136 | const struct got_error *err = NULL((void*)0); | ||
1137 | FILE *f_deriv = NULL((void*)0); | ||
1138 | char *blob_deriv_path = NULL((void*)0), *base_path = NULL((void*)0), *id_str = NULL((void*)0); | ||
1139 | char *label_deriv = NULL((void*)0), *parent = NULL((void*)0); | ||
1140 | |||
1141 | *local_changes_subsumed = 0; | ||
1142 | |||
1143 | err = got_path_dirname(&parent, ondisk_path); | ||
1144 | if (err) | ||
1145 | return err; | ||
1146 | |||
1147 | free(base_path); | ||
1148 | if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) { | ||
1149 | err = got_error_from_errno("asprintf"); | ||
1150 | base_path = NULL((void*)0); | ||
1151 | goto done; | ||
1152 | } | ||
1153 | |||
1154 | err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path); | ||
1155 | if (err) | ||
1156 | goto done; | ||
1157 | err = got_object_blob_dump_to_file(NULL((void*)0), NULL((void*)0), NULL((void*)0), f_deriv, | ||
1158 | blob_deriv); | ||
1159 | if (err) | ||
1160 | goto done; | ||
1161 | |||
1162 | err = got_object_id_str(&id_str, deriv_base_commit_id); | ||
1163 | if (err) | ||
1164 | goto done; | ||
1165 | if (asprintf(&label_deriv, "%s: commit %s", | ||
1166 | GOT_MERGE_LABEL_MERGED"merged change", id_str) == -1) { | ||
1167 | err = got_error_from_errno("asprintf"); | ||
1168 | goto done; | ||
1169 | } | ||
1170 | |||
1171 | err = merge_file(local_changes_subsumed, worktree, blob_orig, | ||
1172 | ondisk_path, path, st_mode, blob_deriv_path, label_orig, | ||
1173 | label_deriv, repo, progress_cb, progress_arg); | ||
1174 | done: | ||
1175 | if (f_deriv && fclose(f_deriv) == EOF(-1) && err == NULL((void*)0)) | ||
1176 | err = got_error_from_errno("fclose"); | ||
1177 | free(base_path); | ||
1178 | if (blob_deriv_path) { | ||
1179 | unlink(blob_deriv_path); | ||
1180 | free(blob_deriv_path); | ||
1181 | } | ||
1182 | free(id_str); | ||
1183 | free(label_deriv); | ||
1184 | free(parent); | ||
1185 | return err; | ||
1186 | } | ||
1187 | |||
1188 | static const struct got_error * | ||
1189 | create_fileindex_entry(struct got_fileindex_entry **new_iep, | ||
1190 | struct got_fileindex *fileindex, struct got_object_id *base_commit_id, | ||
1191 | int wt_fd, const char *path, struct got_object_id *blob_id) | ||
1192 | { | ||
1193 | const struct got_error *err = NULL((void*)0); | ||
1194 | struct got_fileindex_entry *new_ie; | ||
1195 | |||
1196 | *new_iep = NULL((void*)0); | ||
1197 | |||
1198 | err = got_fileindex_entry_alloc(&new_ie, path); | ||
1199 | if (err) | ||
1200 | return err; | ||
1201 | |||
1202 | err = got_fileindex_entry_update(new_ie, wt_fd, path, | ||
1203 | blob_id->sha1, base_commit_id->sha1, 1); | ||
1204 | if (err) | ||
1205 | goto done; | ||
1206 | |||
1207 | err = got_fileindex_entry_add(fileindex, new_ie); | ||
1208 | done: | ||
1209 | if (err) | ||
1210 | got_fileindex_entry_free(new_ie); | ||
1211 | else | ||
1212 | *new_iep = new_ie; | ||
1213 | return err; | ||
1214 | } | ||
1215 | |||
1216 | static mode_t | ||
1217 | get_ondisk_perms(int executable, mode_t st_mode) | ||
1218 | { | ||
1219 | mode_t xbits = S_IXUSR0000100; | ||
1220 | |||
1221 | if (executable) { | ||
1222 | /* Map read bits to execute bits. */ | ||
1223 | if (st_mode & S_IRGRP0000040) | ||
1224 | xbits |= S_IXGRP0000010; | ||
1225 | if (st_mode & S_IROTH0000004) | ||
1226 | xbits |= S_IXOTH0000001; | ||
1227 | return st_mode | xbits; | ||
1228 | } | ||
1229 | |||
1230 | return (st_mode & ~(S_IXUSR0000100 | S_IXGRP0000010 | S_IXOTH0000001)); | ||
1231 | } | ||
1232 | |||
1233 | /* forward declaration */ | ||
1234 | static const struct got_error * | ||
1235 | install_blob(struct got_worktree *worktree, const char *ondisk_path, | ||
1236 | const char *path, mode_t te_mode, mode_t st_mode, | ||
1237 | struct got_blob_object *blob, int restoring_missing_file, | ||
1238 | int reverting_versioned_file, int installing_bad_symlink, | ||
1239 | int path_is_unversioned, struct got_repository *repo, | ||
1240 | got_worktree_checkout_cb progress_cb, void *progress_arg); | ||
1241 | |||
1242 | /* | ||
1243 | * This function assumes that the provided symlink target points at a | ||
1244 | * safe location in the work tree! | ||
1245 | */ | ||
1246 | static const struct got_error * | ||
1247 | replace_existing_symlink(int *did_something, const char *ondisk_path, | ||
1248 | const char *target_path, size_t target_len) | ||
1249 | { | ||
1250 | const struct got_error *err = NULL((void*)0); | ||
1251 | ssize_t elen; | ||
1252 | char etarget[PATH_MAX1024]; | ||
1253 | int fd; | ||
1254 | |||
1255 | *did_something = 0; | ||
1256 | |||
1257 | /* | ||
1258 | * "Bad" symlinks (those pointing outside the work tree or into the | ||
1259 | * .got directory) are installed in the work tree as a regular file | ||
1260 | * which contains the bad symlink target path. | ||
1261 | * The new symlink target has already been checked for safety by our | ||
1262 | * caller. If we can successfully open a regular file then we simply | ||
1263 | * replace this file with a symlink below. | ||
1264 | */ | ||
1265 | fd = open(ondisk_path, O_RDWR0x0002 | O_EXCL0x0800 | O_NOFOLLOW0x0100); | ||
1266 | if (fd == -1) { | ||
1267 | if (errno(*__errno()) != ELOOP62) | ||
1268 | return got_error_from_errno2("open", ondisk_path); | ||
1269 | |||
1270 | /* We are updating an existing on-disk symlink. */ | ||
1271 | elen = readlink(ondisk_path, etarget, sizeof(etarget)); | ||
1272 | if (elen == -1) | ||
1273 | return got_error_from_errno2("readlink", ondisk_path); | ||
1274 | |||
1275 | if (elen == target_len && | ||
1276 | memcmp(etarget, target_path, target_len) == 0) | ||
1277 | return NULL((void*)0); /* nothing to do */ | ||
1278 | } | ||
1279 | |||
1280 | *did_something = 1; | ||
1281 | err = update_symlink(ondisk_path, target_path, target_len); | ||
1282 | if (fd != -1 && close(fd) == -1 && err == NULL((void*)0)) | ||
1283 | err = got_error_from_errno2("close", ondisk_path); | ||
1284 | return err; | ||
1285 | } | ||
1286 | |||
1287 | static const struct got_error * | ||
1288 | is_bad_symlink_target(int *is_bad_symlink, const char *target_path, | ||
1289 | size_t target_len, const char *ondisk_path, const char *wtroot_path) | ||
1290 | { | ||
1291 | const struct got_error *err = NULL((void*)0); | ||
1292 | char canonpath[PATH_MAX1024]; | ||
1293 | char *path_got = NULL((void*)0); | ||
1294 | |||
1295 | *is_bad_symlink = 0; | ||
1296 | |||
1297 | if (target_len >= sizeof(canonpath)) { | ||
1298 | *is_bad_symlink = 1; | ||
1299 | return NULL((void*)0); | ||
1300 | } | ||
1301 | |||
1302 | /* | ||
1303 | * We do not use realpath(3) to resolve the symlink's target | ||
1304 | * path because we don't want to resolve symlinks recursively. | ||
1305 | * Instead we make the path absolute and then canonicalize it. | ||
1306 | * Relative symlink target lookup should begin at the directory | ||
1307 | * in which the blob object is being installed. | ||
1308 | */ | ||
1309 | if (!got_path_is_absolute(target_path)) { | ||
1310 | char *abspath, *parent; | ||
1311 | err = got_path_dirname(&parent, ondisk_path); | ||
1312 | if (err) | ||
1313 | return err; | ||
1314 | if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) { | ||
1315 | free(parent); | ||
1316 | return got_error_from_errno("asprintf"); | ||
1317 | } | ||
1318 | free(parent); | ||
1319 | if (strlen(abspath) >= sizeof(canonpath)) { | ||
1320 | err = got_error_path(abspath, GOT_ERR_BAD_PATH4); | ||
1321 | free(abspath); | ||
1322 | return err; | ||
1323 | } | ||
1324 | err = got_canonpath(abspath, canonpath, sizeof(canonpath)); | ||
1325 | free(abspath); | ||
1326 | if (err) | ||
1327 | return err; | ||
1328 | } else { | ||
1329 | err = got_canonpath(target_path, canonpath, sizeof(canonpath)); | ||
1330 | if (err) | ||
1331 | return err; | ||
1332 | } | ||
1333 | |||
1334 | /* Only allow symlinks pointing at paths within the work tree. */ | ||
1335 | if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) { | ||
1336 | *is_bad_symlink = 1; | ||
1337 | return NULL((void*)0); | ||
1338 | } | ||
1339 | |||
1340 | /* Do not allow symlinks pointing into the .got directory. */ | ||
1341 | if (asprintf(&path_got, "%s/%s", wtroot_path, | ||
1342 | GOT_WORKTREE_GOT_DIR".got") == -1) | ||
1343 | return got_error_from_errno("asprintf"); | ||
1344 | if (got_path_is_child(canonpath, path_got, strlen(path_got))) | ||
1345 | *is_bad_symlink = 1; | ||
1346 | |||
1347 | free(path_got); | ||
1348 | return NULL((void*)0); | ||
1349 | } | ||
1350 | |||
1351 | static const struct got_error * | ||
1352 | install_symlink(int *is_bad_symlink, struct got_worktree *worktree, | ||
1353 | const char *ondisk_path, const char *path, struct got_blob_object *blob, | ||
1354 | int restoring_missing_file, int reverting_versioned_file, | ||
1355 | int path_is_unversioned, struct got_repository *repo, | ||
1356 | got_worktree_checkout_cb progress_cb, void *progress_arg) | ||
1357 | { | ||
1358 | const struct got_error *err = NULL((void*)0); | ||
1359 | char target_path[PATH_MAX1024]; | ||
1360 | size_t len, target_len = 0; | ||
1361 | char *path_got = NULL((void*)0); | ||
1362 | const uint8_t *buf = got_object_blob_get_read_buf(blob); | ||
1363 | size_t hdrlen = got_object_blob_get_hdrlen(blob); | ||
1364 | |||
1365 | *is_bad_symlink = 0; | ||
1366 | |||
1367 | /* | ||
1368 | * Blob object content specifies the target path of the link. | ||
1369 | * If a symbolic link cannot be installed we instead create | ||
1370 | * a regular file which contains the link target path stored | ||
1371 | * in the blob object. | ||
1372 | */ | ||
1373 | do { | ||
1374 | err = got_object_blob_read_block(&len, blob); | ||
1375 | if (len + target_len >= sizeof(target_path)) { | ||
1376 | /* Path too long; install as a regular file. */ | ||
1377 | *is_bad_symlink = 1; | ||
1378 | got_object_blob_rewind(blob); | ||
1379 | return install_blob(worktree, ondisk_path, path, | ||
1380 | GOT_DEFAULT_FILE_MODE(0100000 | 0000400|0000200 | 0000040 | 0000004), GOT_DEFAULT_FILE_MODE(0100000 | 0000400|0000200 | 0000040 | 0000004), blob, | ||
1381 | restoring_missing_file, reverting_versioned_file, | ||
1382 | 1, path_is_unversioned, repo, progress_cb, | ||
1383 | progress_arg); | ||
1384 | } | ||
1385 | if (len > 0) { | ||
1386 | /* Skip blob object header first time around. */ | ||
1387 | memcpy(target_path + target_len, buf + hdrlen, | ||
1388 | len - hdrlen); | ||
1389 | target_len += len - hdrlen; | ||
1390 | hdrlen = 0; | ||
1391 | } | ||
1392 | } while (len != 0); | ||
1393 | target_path[target_len] = '\0'; | ||
1394 | |||
1395 | err = is_bad_symlink_target(is_bad_symlink, target_path, target_len, | ||
1396 | ondisk_path, worktree->root_path); | ||
1397 | if (err) | ||
1398 | return err; | ||
1399 | |||
1400 | if (*is_bad_symlink) { | ||
1401 | /* install as a regular file */ | ||
1402 | got_object_blob_rewind(blob); | ||
1403 | err = install_blob(worktree, ondisk_path, path, | ||
1404 | GOT_DEFAULT_FILE_MODE(0100000 | 0000400|0000200 | 0000040 | 0000004), GOT_DEFAULT_FILE_MODE(0100000 | 0000400|0000200 | 0000040 | 0000004), blob, | ||
1405 | restoring_missing_file, reverting_versioned_file, 1, | ||
1406 | path_is_unversioned, repo, progress_cb, progress_arg); | ||
1407 | goto done; | ||
1408 | } | ||
1409 | |||
1410 | if (symlink(target_path, ondisk_path) == -1) { | ||
1411 | if (errno(*__errno()) == EEXIST17) { | ||
1412 | int symlink_replaced; | ||
1413 | if (path_is_unversioned) { | ||
1414 | err = (*progress_cb)(progress_arg, | ||
1415 | GOT_STATUS_UNVERSIONED'?', path); | ||
1416 | goto done; | ||
1417 | } | ||
1418 | err = replace_existing_symlink(&symlink_replaced, | ||
1419 | ondisk_path, target_path, target_len); | ||
1420 | if (err) | ||
1421 | goto done; | ||
1422 | if (progress_cb) { | ||
1423 | if (symlink_replaced) { | ||
1424 | err = (*progress_cb)(progress_arg, | ||
1425 | reverting_versioned_file ? | ||
1426 | GOT_STATUS_REVERT'R' : | ||
1427 | GOT_STATUS_UPDATE'U', path); | ||
1428 | } else { | ||
1429 | err = (*progress_cb)(progress_arg, | ||
1430 | GOT_STATUS_EXISTS'E', path); | ||
1431 | } | ||
1432 | } | ||
1433 | goto done; /* Nothing else to do. */ | ||
1434 | } | ||
1435 | |||
1436 | if (errno(*__errno()) == ENOENT2) { | ||
1437 | char *parent; | ||
1438 | err = got_path_dirname(&parent, ondisk_path); | ||
1439 | if (err) | ||
1440 | goto done; | ||
1441 | err = add_dir_on_disk(worktree, parent); | ||
1442 | free(parent); | ||
1443 | if (err) | ||
1444 | goto done; | ||
1445 | /* | ||
1446 | * Retry, and fall through to error handling | ||
1447 | * below if this second attempt fails. | ||
1448 | */ | ||
1449 | if (symlink(target_path, ondisk_path) != -1) { | ||
1450 | err = NULL((void*)0); /* success */ | ||
1451 | goto done; | ||
1452 | } | ||
1453 | } | ||
1454 | |||
1455 | /* Handle errors from first or second creation attempt. */ | ||
1456 | if (errno(*__errno()) == ENAMETOOLONG63) { | ||
1457 | /* bad target path; install as a regular file */ | ||
1458 | *is_bad_symlink = 1; | ||
1459 | got_object_blob_rewind(blob); | ||
1460 | err = install_blob(worktree, ondisk_path, path, | ||
1461 | GOT_DEFAULT_FILE_MODE(0100000 | 0000400|0000200 | 0000040 | 0000004), GOT_DEFAULT_FILE_MODE(0100000 | 0000400|0000200 | 0000040 | 0000004), blob, | ||
1462 | restoring_missing_file, reverting_versioned_file, 1, | ||
1463 | path_is_unversioned, repo, | ||
1464 | progress_cb, progress_arg); | ||
1465 | } else if (errno(*__errno()) == ENOTDIR20) { | ||
1466 | err = got_error_path(ondisk_path, | ||
1467 | GOT_ERR_FILE_OBSTRUCTED31); | ||
1468 | } else { | ||
1469 | err = got_error_from_errno3("symlink", | ||
1470 | target_path, ondisk_path); | ||
1471 | } | ||
1472 | } else if (progress_cb) | ||
1473 | err = (*progress_cb)(progress_arg, reverting_versioned_file ? | ||
1474 | GOT_STATUS_REVERT'R' : GOT_STATUS_ADD'A', path); | ||
1475 | done: | ||
1476 | free(path_got); | ||
1477 | return err; | ||
1478 | } | ||
1479 | |||
1480 | static const struct got_error * | ||
1481 | install_blob(struct got_worktree *worktree, const char *ondisk_path, | ||
1482 | const char *path, mode_t te_mode, mode_t st_mode, | ||
1483 | struct got_blob_object *blob, int restoring_missing_file, | ||
1484 | int reverting_versioned_file, int installing_bad_symlink, | ||
1485 | int path_is_unversioned, struct got_repository *repo, | ||
1486 | got_worktree_checkout_cb progress_cb, void *progress_arg) | ||
1487 | { | ||
1488 | const struct got_error *err = NULL((void*)0); | ||
1489 | int fd = -1; | ||
1490 | size_t len, hdrlen; | ||
1491 | int update = 0; | ||
1492 | char *tmppath = NULL((void*)0); | ||
1493 | |||
1494 | fd = open(ondisk_path, O_RDWR0x0002 | O_CREAT0x0200 | O_EXCL0x0800 | O_NOFOLLOW0x0100, | ||
1495 | GOT_DEFAULT_FILE_MODE(0100000 | 0000400|0000200 | 0000040 | 0000004)); | ||
1496 | if (fd == -1) { | ||
1497 | if (errno(*__errno()) == ENOENT2) { | ||
1498 | char *parent; | ||
1499 | err = got_path_dirname(&parent, path); | ||
1500 | if (err) | ||
1501 | return err; | ||
1502 | err = add_dir_on_disk(worktree, parent); | ||
1503 | free(parent); | ||
1504 | if (err) | ||
1505 | return err; | ||
1506 | fd = open(ondisk_path, | ||
1507 | O_RDWR0x0002 | O_CREAT0x0200 | O_EXCL0x0800 | O_NOFOLLOW0x0100, | ||
1508 | GOT_DEFAULT_FILE_MODE(0100000 | 0000400|0000200 | 0000040 | 0000004)); | ||
1509 | if (fd == -1) | ||
1510 | return got_error_from_errno2("open", | ||
1511 | ondisk_path); | ||
1512 | } else if (errno(*__errno()) == EEXIST17) { | ||
1513 | if (path_is_unversioned) { | ||
1514 | err = (*progress_cb)(progress_arg, | ||
1515 | GOT_STATUS_UNVERSIONED'?', path); | ||
1516 | goto done; | ||
1517 | } | ||
1518 | if (!(S_ISLNK(st_mode)((st_mode & 0170000) == 0120000) && S_ISREG(te_mode)((te_mode & 0170000) == 0100000)) && | ||
1519 | !S_ISREG(st_mode)((st_mode & 0170000) == 0100000) && !installing_bad_symlink) { | ||
1520 | /* TODO file is obstructed; do something */ | ||
1521 | err = got_error_path(ondisk_path, | ||
1522 | GOT_ERR_FILE_OBSTRUCTED31); | ||
1523 | goto done; | ||
1524 | } else { | ||
1525 | err = got_opentemp_named_fd(&tmppath, &fd, | ||
1526 | ondisk_path); | ||
1527 | if (err) | ||
1528 | goto done; | ||
1529 | update = 1; | ||
1530 | } | ||
1531 | } else | ||
1532 | return got_error_from_errno2("open", ondisk_path); | ||
1533 | } | ||
1534 | |||
1535 | if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR0000100, st_mode)) == -1) { | ||
1536 | err = got_error_from_errno2("fchmod", | ||
1537 | update ? tmppath : ondisk_path); | ||
1538 | goto done; | ||
1539 | } | ||
1540 | |||
1541 | if (progress_cb) { | ||
1542 | if (restoring_missing_file) | ||
1543 | err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING'!', | ||
1544 | path); | ||
1545 | else if (reverting_versioned_file) | ||
1546 | err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT'R', | ||
1547 | path); | ||
1548 | else | ||
1549 | err = (*progress_cb)(progress_arg, | ||
1550 | update ? GOT_STATUS_UPDATE'U' : GOT_STATUS_ADD'A', path); | ||
1551 | if (err) | ||
1552 | goto done; | ||
1553 | } | ||
1554 | |||
1555 | hdrlen = got_object_blob_get_hdrlen(blob); | ||
1556 | do { | ||
1557 | const uint8_t *buf = got_object_blob_get_read_buf(blob); | ||
1558 | err = got_object_blob_read_block(&len, blob); | ||
1559 | if (err) | ||
1560 | break; | ||
1561 | if (len > 0) { | ||
1562 | /* Skip blob object header first time around. */ | ||
1563 | ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen); | ||
1564 | if (outlen == -1) { | ||
1565 | err = got_error_from_errno("write"); | ||
1566 | goto done; | ||
1567 | } else if (outlen != len - hdrlen) { | ||
1568 | err = got_error(GOT_ERR_IO6); | ||
1569 | goto done; | ||
1570 | } | ||
1571 | hdrlen = 0; | ||
1572 | } | ||
1573 | } while (len != 0); | ||
1574 | |||
1575 | if (fsync(fd) != 0) { | ||
1576 | err = got_error_from_errno("fsync"); | ||
1577 | goto done; | ||
1578 | } | ||
1579 | |||
1580 | if (update) { | ||
1581 | if (S_ISLNK(st_mode)((st_mode & 0170000) == 0120000) && unlink(ondisk_path) == -1) { | ||
1582 | err = got_error_from_errno2("unlink", ondisk_path); | ||
1583 | goto done; | ||
1584 | } | ||
1585 | if (rename(tmppath, ondisk_path) != 0) { | ||
1586 | err = got_error_from_errno3("rename", tmppath, | ||
1587 | ondisk_path); | ||
1588 | goto done; | ||
1589 | } | ||
1590 | free(tmppath); | ||
1591 | tmppath = NULL((void*)0); | ||
1592 | } | ||
1593 | |||
1594 | done: | ||
1595 | if (fd != -1 && close(fd) == -1 && err == NULL((void*)0)) | ||
1596 | err = got_error_from_errno("close"); | ||
1597 | if (tmppath != NULL((void*)0) && unlink(tmppath) == -1 && err == NULL((void*)0)) | ||
1598 | err = got_error_from_errno2("unlink", tmppath); | ||
1599 | free(tmppath); | ||
1600 | return err; | ||
1601 | } | ||
1602 | |||
1603 | /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */ | ||
1604 | static const struct got_error * | ||
1605 | get_modified_file_content_status(unsigned char *status, FILE *f) | ||
1606 | { | ||
1607 | const struct got_error *err = NULL((void*)0); | ||
1608 | const char *markers[3] = { | ||
1609 | GOT_DIFF_CONFLICT_MARKER_BEGIN"<<<<<<<", | ||
1610 | GOT_DIFF_CONFLICT_MARKER_SEP"=======", | ||
1611 | GOT_DIFF_CONFLICT_MARKER_END">>>>>>>" | ||
1612 | }; | ||
1613 | int i = 0; | ||
1614 | char *line = NULL((void*)0); | ||
1615 | size_t linesize = 0; | ||
1616 | ssize_t linelen; | ||
1617 | |||
1618 | while (*status == GOT_STATUS_MODIFY'M') { | ||
1619 | linelen = getline(&line, &linesize, f); | ||
1620 | if (linelen == -1) { | ||
1621 | if (feof(f)(!__isthreaded ? (((f)->_flags & 0x0020) != 0) : (feof )(f))) | ||
1622 | break; | ||
1623 | err = got_ferror(f, GOT_ERR_IO6); | ||
1624 | break; | ||
1625 | } | ||
1626 | |||
1627 | if (strncmp(line, markers[i], strlen(markers[i])) == 0) { | ||
1628 | if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END">>>>>>>") | ||
1629 | == 0) | ||
1630 | *status = GOT_STATUS_CONFLICT'C'; | ||
1631 | else | ||
1632 | i++; | ||
1633 | } | ||
1634 | } | ||
1635 | free(line); | ||
1636 | |||
1637 | return err; | ||
1638 | } | ||
1639 | |||
1640 | static int | ||
1641 | xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode) | ||
1642 | { | ||
1643 | mode_t ie_mode = got_fileindex_perms_to_st(ie); | ||
1644 | return ((ie_mode & S_IXUSR0000100) != (st_mode & S_IXUSR0000100)); | ||
1645 | } | ||
1646 | |||
1647 | static int | ||
1648 | stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb) | ||
1649 | { | ||
1650 | return !(ie->ctime_sec == sb->st_ctim.tv_sec && | ||
1651 | ie->ctime_nsec == sb->st_ctim.tv_nsec && | ||
1652 | ie->mtime_sec == sb->st_mtim.tv_sec && | ||
1653 | ie->mtime_nsec == sb->st_mtim.tv_nsec && | ||
1654 | ie->size == (sb->st_size & 0xffffffff) && | ||
1655 | !xbit_differs(ie, sb->st_mode)); | ||
1656 | } | ||
1657 | |||
1658 | static unsigned char | ||
1659 | get_staged_status(struct got_fileindex_entry *ie) | ||
1660 | { | ||
1661 | switch (got_fileindex_entry_stage_get(ie)) { | ||
1662 | case GOT_FILEIDX_STAGE_ADD2: | ||
1663 | return GOT_STATUS_ADD'A'; | ||
1664 | case GOT_FILEIDX_STAGE_DELETE3: | ||
1665 | return GOT_STATUS_DELETE'D'; | ||
1666 | case GOT_FILEIDX_STAGE_MODIFY1: | ||
1667 | return GOT_STATUS_MODIFY'M'; | ||
1668 | default: | ||
1669 | return GOT_STATUS_NO_CHANGE' '; | ||
1670 | } | ||
1671 | } | ||
1672 | |||
1673 | static const struct got_error * | ||
1674 | get_symlink_modification_status(unsigned char *status, | ||
1675 | struct got_fileindex_entry *ie, const char *abspath, | ||
1676 | int dirfd, const char *de_name, struct got_blob_object *blob) | ||
1677 | { | ||
1678 | const struct got_error *err = NULL((void*)0); | ||
1679 | char target_path[PATH_MAX1024]; | ||
1680 | char etarget[PATH_MAX1024]; | ||
1681 | ssize_t elen; | ||
1682 | size_t len, target_len = 0; | ||
1683 | const uint8_t *buf = got_object_blob_get_read_buf(blob); | ||
1684 | size_t hdrlen = got_object_blob_get_hdrlen(blob); | ||
1685 | |||
1686 | *status = GOT_STATUS_NO_CHANGE' '; | ||
1687 | |||
1688 | /* Blob object content specifies the target path of the link. */ | ||
1689 | do { | ||
1690 | err = got_object_blob_read_block(&len, blob); | ||
1691 | if (err) | ||
1692 | return err; | ||
1693 | if (len + target_len >= sizeof(target_path)) { | ||
1694 | /* | ||
1695 | * Should not happen. The blob contents were OK | ||
1696 | * when this symlink was installed. | ||
1697 | */ | ||
1698 | return got_error(GOT_ERR_NO_SPACE9); | ||
1699 | } | ||
1700 | if (len > 0) { | ||
1701 | /* Skip blob object header first time around. */ | ||
1702 | memcpy(target_path + target_len, buf + hdrlen, | ||
1703 | len - hdrlen); | ||
1704 | target_len += len - hdrlen; | ||
1705 | hdrlen = 0; | ||
1706 | } | ||
1707 | } while (len != 0); | ||
1708 | target_path[target_len] = '\0'; | ||
1709 | |||
1710 | if (dirfd != -1) { | ||
1711 | elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget)); | ||
1712 | if (elen == -1) | ||
1713 | return got_error_from_errno2("readlinkat", abspath); | ||
1714 | } else { | ||
1715 | elen = readlink(abspath, etarget, sizeof(etarget)); | ||
1716 | if (elen == -1) | ||
1717 | return got_error_from_errno2("readlink", abspath); | ||
1718 | } | ||
1719 | |||
1720 | if (elen != target_len || memcmp(etarget, target_path, target_len) != 0) | ||
1721 | *status = GOT_STATUS_MODIFY'M'; | ||
1722 | |||
1723 | return NULL((void*)0); | ||
1724 | } | ||
1725 | |||
1726 | static const struct got_error * | ||
1727 | get_file_status(unsigned char *status, struct stat *sb, | ||
1728 | struct got_fileindex_entry *ie, const char *abspath, | ||
1729 | int dirfd, const char *de_name, struct got_repository *repo) | ||
1730 | { | ||
1731 | const struct got_error *err = NULL((void*)0); | ||
1732 | struct got_object_id id; | ||
1733 | size_t hdrlen; | ||
1734 | int fd = -1; | ||
1735 | FILE *f = NULL((void*)0); | ||
1736 | uint8_t fbuf[8192]; | ||
1737 | struct got_blob_object *blob = NULL((void*)0); | ||
1738 | size_t flen, blen; | ||
1739 | unsigned char staged_status = get_staged_status(ie); | ||
1740 | |||
1741 | *status = GOT_STATUS_NO_CHANGE' '; | ||
1742 | memset(sb, 0, sizeof(*sb)); | ||
1743 | |||
1744 | /* | ||
1745 | * Whenever the caller provides a directory descriptor and a | ||
1746 | * directory entry name for the file, use them! This prevents | ||
1747 | * race conditions if filesystem paths change beneath our feet. | ||
1748 | */ | ||
1749 | if (dirfd != -1) { | ||
1750 | if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW0x02) == -1) { | ||
1751 | if (errno(*__errno()) == ENOENT2) { | ||
1752 | if (got_fileindex_entry_has_file_on_disk(ie)) | ||
1753 | *status = GOT_STATUS_MISSING'!'; | ||
1754 | else | ||
1755 | *status = GOT_STATUS_DELETE'D'; | ||
1756 | goto done; | ||
1757 | } | ||
1758 | err = got_error_from_errno2("fstatat", abspath); | ||
1759 | goto done; | ||
1760 | } | ||
1761 | } else { | ||
1762 | fd = open(abspath, O_RDONLY0x0000 | O_NOFOLLOW0x0100); | ||
1763 | if (fd == -1 && errno(*__errno()) != ENOENT2 && errno(*__errno()) != ELOOP62) | ||
1764 | return got_error_from_errno2("open", abspath); | ||
1765 | else if (fd == -1 && errno(*__errno()) == ELOOP62) { | ||
1766 | if (lstat(abspath, sb) == -1) | ||
1767 | return got_error_from_errno2("lstat", abspath); | ||
1768 | } else if (fd == -1 || fstat(fd, sb) == -1) { | ||
1769 | if (errno(*__errno()) == ENOENT2) { | ||
1770 | if (got_fileindex_entry_has_file_on_disk(ie)) | ||
1771 | *status = GOT_STATUS_MISSING'!'; | ||
1772 | else | ||
1773 | *status = GOT_STATUS_DELETE'D'; | ||
1774 | goto done; | ||
1775 | } | ||
1776 | err = got_error_from_errno2("fstat", abspath); | ||
1777 | goto done; | ||
1778 | } | ||
1779 | } | ||
1780 | |||
1781 | if (!S_ISREG(sb->st_mode)((sb->st_mode & 0170000) == 0100000) && !S_ISLNK(sb->st_mode)((sb->st_mode & 0170000) == 0120000)) { | ||
1782 | *status = GOT_STATUS_OBSTRUCTED'~'; | ||
1783 | goto done; | ||
1784 | } | ||
1785 | |||
1786 | if (!got_fileindex_entry_has_file_on_disk(ie)) { | ||
1787 | *status = GOT_STATUS_DELETE'D'; | ||
1788 | goto done; | ||
1789 | } else if (!got_fileindex_entry_has_blob(ie) && | ||
1790 | staged_status != GOT_STATUS_ADD'A') { | ||
1791 | *status = GOT_STATUS_ADD'A'; | ||
1792 | goto done; | ||
1793 | } | ||
1794 | |||
1795 | if (!stat_info_differs(ie, sb)) | ||
1796 | goto done; | ||
1797 | |||
1798 | if (S_ISLNK(sb->st_mode)((sb->st_mode & 0170000) == 0120000) && | ||
1799 | got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK2) { | ||
1800 | *status = GOT_STATUS_MODIFY'M'; | ||
1801 | goto done; | ||
1802 | } | ||
1803 | |||
1804 | if (staged_status == GOT_STATUS_MODIFY'M' || | ||
1805 | staged_status == GOT_STATUS_ADD'A') | ||
1806 | memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1)); | ||
1807 | else | ||
1808 | memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1)); | ||
1809 | |||
1810 | err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf)); | ||
1811 | if (err) | ||
1812 | goto done; | ||
1813 | |||
1814 | if (S_ISLNK(sb->st_mode)((sb->st_mode & 0170000) == 0120000)) { | ||
1815 | err = get_symlink_modification_status(status, ie, | ||
1816 | abspath, dirfd, de_name, blob); | ||
1817 | goto done; | ||
1818 | } | ||
1819 | |||
1820 | if (dirfd != -1) { | ||
1821 | fd = openat(dirfd, de_name, O_RDONLY0x0000 | O_NOFOLLOW0x0100); | ||
1822 | if (fd == -1) { | ||
1823 | err = got_error_from_errno2("openat", abspath); | ||
1824 | goto done; | ||
1825 | } | ||
1826 | } | ||
1827 | |||
1828 | f = fdopen(fd, "r"); | ||
1829 | if (f == NULL((void*)0)) { | ||
1830 | err = got_error_from_errno2("fdopen", abspath); | ||
1831 | goto done; | ||
1832 | } | ||
1833 | fd = -1; | ||
1834 | hdrlen = got_object_blob_get_hdrlen(blob); | ||
1835 | for (;;) { | ||
1836 | const uint8_t *bbuf = got_object_blob_get_read_buf(blob); | ||
1837 | err = got_object_blob_read_block(&blen, blob); | ||
1838 | if (err) | ||
1839 | goto done; | ||
1840 | /* Skip length of blob object header first time around. */ | ||
1841 | flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f); | ||
1842 | if (flen == 0 && ferror(f)(!__isthreaded ? (((f)->_flags & 0x0040) != 0) : (ferror )(f))) { | ||
1843 | err = got_error_from_errno("fread"); | ||
1844 | goto done; | ||
1845 | } | ||
1846 | if (blen - hdrlen == 0) { | ||
1847 | if (flen != 0) | ||
1848 | *status = GOT_STATUS_MODIFY'M'; | ||
1849 | break; | ||
1850 | } else if (flen == 0) { | ||
1851 | if (blen - hdrlen != 0) | ||
1852 | *status = GOT_STATUS_MODIFY'M'; | ||
1853 | break; | ||
1854 | } else if (blen - hdrlen == flen) { | ||
1855 | /* Skip blob object header first time around. */ | ||
1856 | if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) { | ||
1857 | *status = GOT_STATUS_MODIFY'M'; | ||
1858 | break; | ||
1859 | } | ||
1860 | } else { | ||
1861 | *status = GOT_STATUS_MODIFY'M'; | ||
1862 | break; | ||
1863 | } | ||
1864 | hdrlen = 0; | ||
1865 | } | ||
1866 | |||
1867 | if (*status == GOT_STATUS_MODIFY'M') { | ||
1868 | rewind(f); | ||
1869 | err = get_modified_file_content_status(status, f); | ||
1870 | } else if (xbit_differs(ie, sb->st_mode)) | ||
1871 | *status = GOT_STATUS_MODE_CHANGE'm'; | ||
1872 | done: | ||
1873 | if (blob) | ||
1874 | got_object_blob_close(blob); | ||
1875 | if (f != NULL((void*)0) && fclose(f) == EOF(-1) && err == NULL((void*)0)) | ||
1876 | err = got_error_from_errno2("fclose", abspath); | ||
1877 | if (fd != -1 && close(fd) == -1 && err == NULL((void*)0)) | ||
1878 | err = got_error_from_errno2("close", abspath); | ||
1879 | return err; | ||
1880 | } | ||
1881 | |||
1882 | /* | ||
1883 | * Update timestamps in the file index if a file is unmodified and | ||
1884 | * we had to run a full content comparison to find out. | ||
1885 | */ | ||
1886 | static const struct got_error * | ||
1887 | sync_timestamps(int wt_fd, const char *path, unsigned char status, | ||
1888 | struct got_fileindex_entry *ie, struct stat *sb) | ||
1889 | { | ||
1890 | if (status == GOT_STATUS_NO_CHANGE' ' && stat_info_differs(ie, sb)) | ||
1891 | return got_fileindex_entry_update(ie, wt_fd, path, | ||
1892 | ie->blob_sha1, ie->commit_sha1, 1); | ||
1893 | |||
1894 | return NULL((void*)0); | ||
1895 | } | ||
1896 | |||
1897 | static const struct got_error * | ||
1898 | update_blob(struct got_worktree *worktree, | ||
1899 | struct got_fileindex *fileindex, struct got_fileindex_entry *ie, | ||
1900 | struct got_tree_entry *te, const char *path, | ||
1901 | struct got_repository *repo, got_worktree_checkout_cb progress_cb, | ||
1902 | void *progress_arg) | ||
1903 | { | ||
1904 | const struct got_error *err = NULL((void*)0); | ||
1905 | struct got_blob_object *blob = NULL((void*)0); | ||
1906 | char *ondisk_path; | ||
1907 | unsigned char status = GOT_STATUS_NO_CHANGE' '; | ||
1908 | struct stat sb; | ||
1909 | |||
1910 | if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1) | ||
1911 | return got_error_from_errno("asprintf"); | ||
1912 | |||
1913 | if (ie) { | ||
1914 | if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE' ') { | ||
1915 | err = got_error_path(ie->path, GOT_ERR_FILE_STAGED101); | ||
1916 | goto done; | ||
1917 | } | ||
1918 | err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL((void*)0), | ||
1919 | repo); | ||
1920 | if (err) | ||
1921 | goto done; | ||
1922 | if (status == GOT_STATUS_MISSING'!' || status == GOT_STATUS_DELETE'D') | ||
1923 | sb.st_mode = got_fileindex_perms_to_st(ie); | ||
1924 | } else { | ||
1925 | sb.st_mode = GOT_DEFAULT_FILE_MODE(0100000 | 0000400|0000200 | 0000040 | 0000004); | ||
1926 | status = GOT_STATUS_UNVERSIONED'?'; | ||
1927 | } | ||
1928 | |||
1929 | if (status == GOT_STATUS_OBSTRUCTED'~') { | ||
1930 | err = (*progress_cb)(progress_arg, status, path); | ||
1931 | goto done; | ||
1932 | } | ||
1933 | if (status == GOT_STATUS_CONFLICT'C') { | ||
1934 | err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE'#', | ||
1935 | path); | ||
1936 | goto done; | ||
1937 | } | ||
1938 | |||
1939 | if (ie && status != GOT_STATUS_MISSING'!' && S_ISREG(sb.st_mode)((sb.st_mode & 0170000) == 0100000) && | ||
1940 | (S_ISLNK(te->mode)((te->mode & 0170000) == 0120000) || | ||
1941 | (te->mode & S_IXUSR0000100) == (sb.st_mode & S_IXUSR0000100))) { | ||
1942 | /* | ||
1943 | * This is a regular file or an installed bad symlink. | ||
1944 | * If the file index indicates that this file is already | ||
1945 | * up-to-date with respect to the repository we can skip | ||
1946 | * updating contents of this file. | ||
1947 | */ | ||
1948 | if (got_fileindex_entry_has_commit(ie) && | ||
1949 | memcmp(ie->commit_sha1, worktree->base_commit_id->sha1, | ||
1950 | SHA1_DIGEST_LENGTH20) == 0) { | ||
1951 | /* Same commit. */ | ||
1952 | err = sync_timestamps(worktree->root_fd, | ||
1953 | path, status, ie, &sb); | ||
1954 | if (err) | ||
1955 | goto done; | ||
1956 | err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS'E', | ||
1957 | path); | ||
1958 | goto done; | ||
1959 | } | ||
1960 | if (got_fileindex_entry_has_blob(ie) && | ||
1961 | memcmp(ie->blob_sha1, te->id.sha1, | ||
1962 | SHA1_DIGEST_LENGTH20) == 0) { | ||
1963 | /* Different commit but the same blob. */ | ||
1964 | err = sync_timestamps(worktree->root_fd, | ||
1965 | path, status, ie, &sb); | ||
1966 | if (err) | ||
1967 | goto done; | ||
1968 | err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS'E', | ||
1969 | path); | ||
1970 | goto done; | ||
1971 | } | ||
1972 | } | ||
1973 | |||
1974 | err = got_object_open_as_blob(&blob, repo, &te->id, 8192); | ||
1975 | if (err) | ||
1976 | goto done; | ||
1977 | |||
1978 | if (status == GOT_STATUS_MODIFY'M' || status == GOT_STATUS_ADD'A') { | ||
1979 | int update_timestamps; | ||
1980 | struct got_blob_object *blob2 = NULL((void*)0); | ||
1981 | char *label_orig = NULL((void*)0); | ||
1982 | if (got_fileindex_entry_has_blob(ie)) { | ||
1983 | struct got_object_id id2; | ||
1984 | memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH20); | ||
1985 | err = got_object_open_as_blob(&blob2, repo, &id2, 8192); | ||
1986 | if (err) | ||
1987 | goto done; | ||
1988 | } | ||
1989 | if (got_fileindex_entry_has_commit(ie)) { | ||
1990 | char id_str[SHA1_DIGEST_STRING_LENGTH(20 * 2 + 1)]; | ||
1991 | if (got_sha1_digest_to_str(ie->commit_sha1, id_str, | ||
1992 | sizeof(id_str)) == NULL((void*)0)) { | ||
1993 | err = got_error_path(id_str, | ||
1994 | GOT_ERR_BAD_OBJ_ID_STR23); | ||
1995 | goto done; | ||
1996 | } | ||
1997 | if (asprintf(&label_orig, "%s: commit %s", | ||
1998 | GOT_MERGE_LABEL_BASE"3-way merge base", id_str) == -1) { | ||
1999 | err = got_error_from_errno("asprintf"); | ||
2000 | goto done; | ||
2001 | } | ||
2002 | } | ||
2003 | if (S_ISLNK(te->mode)((te->mode & 0170000) == 0120000) && S_ISLNK(sb.st_mode)((sb.st_mode & 0170000) == 0120000)) { | ||
2004 | char *link_target; | ||
2005 | err = got_object_blob_read_to_str(&link_target, blob); | ||
2006 | if (err) | ||
2007 | goto done; | ||
2008 | err = merge_symlink(worktree, blob2, ondisk_path, path, | ||
2009 | label_orig, link_target, worktree->base_commit_id, | ||
2010 | repo, progress_cb, progress_arg); | ||
2011 | free(link_target); | ||
2012 | } else { | ||
2013 | err = merge_blob(&update_timestamps, worktree, blob2, | ||
2014 | ondisk_path, path, sb.st_mode, label_orig, blob, | ||
2015 | worktree->base_commit_id, repo, | ||
2016 | progress_cb, progress_arg); | ||
2017 | } | ||
2018 | free(label_orig); | ||
2019 | if (blob2) | ||
2020 | got_object_blob_close(blob2); | ||
2021 | if (err) | ||
2022 | goto done; | ||
2023 | /* | ||
2024 | * Do not update timestamps of files with local changes. | ||
2025 | * Otherwise, a future status walk would treat them as | ||
2026 | * unmodified files again. | ||
2027 | */ | ||
2028 | err = got_fileindex_entry_update(ie, worktree->root_fd, path, | ||
2029 | blob->id.sha1, worktree->base_commit_id->sha1, | ||
2030 | update_timestamps); | ||
2031 | } else if (status == GOT_STATUS_MODE_CHANGE'm') { | ||
2032 | err = got_fileindex_entry_update(ie, worktree->root_fd, path, | ||
2033 | blob->id.sha1, worktree->base_commit_id->sha1, 0); | ||
2034 | } else if (status == GOT_STATUS_DELETE'D') { | ||
2035 | err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE'G', path); | ||
2036 | if (err) | ||
2037 | goto done; | ||
2038 | err = got_fileindex_entry_update(ie, worktree->root_fd, path, | ||
2039 | blob->id.sha1, worktree->base_commit_id->sha1, 0); | ||
2040 | if (err) | ||
2041 | goto done; | ||
2042 | } else { | ||
2043 | int is_bad_symlink = 0; | ||
2044 | if (S_ISLNK(te->mode)((te->mode & 0170000) == 0120000)) { | ||
2045 | err = install_symlink(&is_bad_symlink, worktree, | ||
2046 | ondisk_path, path, blob, | ||
2047 | status == GOT_STATUS_MISSING'!', 0, | ||
2048 | status == GOT_STATUS_UNVERSIONED'?', repo, | ||
2049 | progress_cb, progress_arg); | ||
2050 | } else { | ||
2051 | err = install_blob(worktree, ondisk_path, path, | ||
2052 | te->mode, sb.st_mode, blob, | ||
2053 | status == GOT_STATUS_MISSING'!', 0, 0, | ||
2054 | status == GOT_STATUS_UNVERSIONED'?', repo, | ||
2055 | progress_cb, progress_arg); | ||
2056 | } | ||
2057 | if (err) | ||
2058 | goto done; | ||
2059 | |||
2060 | if (ie) { | ||
2061 | err = got_fileindex_entry_update(ie, | ||
2062 | worktree->root_fd, path, blob->id.sha1, | ||
2063 | worktree->base_commit_id->sha1, 1); | ||
2064 | } else { | ||
2065 | err = create_fileindex_entry(&ie, fileindex, | ||
2066 | worktree->base_commit_id, worktree->root_fd, path, | ||
2067 | &blob->id); | ||
2068 | } | ||
2069 | if (err) | ||
2070 | goto done; | ||
2071 | |||
2072 | if (is_bad_symlink) { | ||
2073 | got_fileindex_entry_filetype_set(ie, | ||
2074 | GOT_FILEIDX_MODE_BAD_SYMLINK3); | ||
2075 | } | ||
2076 | } | ||
2077 | got_object_blob_close(blob); | ||
2078 | done: | ||
2079 | free(ondisk_path); | ||
2080 | return err; | ||
2081 | } | ||
2082 | |||
2083 | static const struct got_error * | ||
2084 | remove_ondisk_file(const char *root_path, const char *path) | ||
2085 | { | ||
2086 | const struct got_error *err = NULL((void*)0); | ||
2087 | char *ondisk_path = NULL((void*)0); | ||
2088 | |||
2089 | if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1) | ||
2090 | return got_error_from_errno("asprintf"); | ||
2091 | |||
2092 | if (unlink(ondisk_path) == -1) { | ||
2093 | if (errno(*__errno()) != ENOENT2) | ||
2094 | err = got_error_from_errno2("unlink", ondisk_path); | ||
2095 | } else { | ||
2096 | size_t root_len = strlen(root_path); | ||
2097 | do { | ||
2098 | char *parent; | ||
2099 | err = got_path_dirname(&parent, ondisk_path); | ||
2100 | if (err) | ||
2101 | break; | ||
2102 | free(ondisk_path); | ||
2103 | ondisk_path = parent; | ||
2104 | if (rmdir(ondisk_path) == -1) { | ||
2105 | if (errno(*__errno()) != ENOTEMPTY66) | ||
2106 | err = got_error_from_errno2("rmdir", | ||
2107 | ondisk_path); | ||
2108 | break; | ||
2109 | } | ||
2110 | } while (got_path_cmp(ondisk_path, root_path, | ||
2111 | strlen(ondisk_path), root_len) != 0); | ||
2112 | } | ||
2113 | free(ondisk_path); | ||
2114 | return err; | ||
2115 | } | ||
2116 | |||
2117 | static const struct got_error * | ||
2118 | delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex, | ||
2119 | struct got_fileindex_entry *ie, struct got_repository *repo, | ||
2120 | got_worktree_checkout_cb progress_cb, void *progress_arg) | ||
2121 | { | ||
2122 | const struct got_error *err = NULL((void*)0); | ||
2123 | unsigned char status; | ||
2124 | struct stat sb; | ||
2125 | char *ondisk_path; | ||
2126 | |||
2127 | if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE' ') | ||
2128 | return got_error_path(ie->path, GOT_ERR_FILE_STAGED101); | ||
2129 | |||
2130 | if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path) | ||
2131 | == -1) | ||
2132 | return got_error_from_errno("asprintf"); | ||
2133 | |||
2134 | err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL((void*)0), repo); | ||
2135 | if (err) | ||
2136 | goto done; | ||
2137 | |||
2138 | if (S_ISLNK(sb.st_mode)((sb.st_mode & 0170000) == 0120000) && status != GOT_STATUS_NO_CHANGE' ') { | ||
2139 | char ondisk_target[PATH_MAX1024]; | ||
2140 | ssize_t ondisk_len = readlink(ondisk_path, ondisk_target, | ||
2141 | sizeof(ondisk_target)); | ||
2142 | if (ondisk_len == -1) { | ||
2143 | err = got_error_from_errno2("readlink", ondisk_path); | ||
2144 | goto done; | ||
2145 | } | ||
2146 | ondisk_target[ondisk_len] = '\0'; | ||
2147 | err = install_symlink_conflict(NULL((void*)0), worktree->base_commit_id, | ||
2148 | NULL((void*)0), NULL((void*)0), /* XXX pass common ancestor info? */ | ||
2149 | ondisk_target, ondisk_path); | ||
2150 | if (err) | ||
2151 | goto done; | ||
2152 | err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT'C', | ||
2153 | ie->path); | ||
2154 | goto done; | ||
2155 | } | ||
2156 | |||
2157 | if (status == GOT_STATUS_MODIFY'M' || status == GOT_STATUS_CONFLICT'C' || | ||
2158 | status == GOT_STATUS_ADD'A') { | ||
2159 | err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE'G', ie->path); | ||
2160 | if (err) | ||
2161 | goto done; | ||
2162 | /* | ||
2163 | * Preserve the working file and change the deleted blob's | ||
2164 | * entry into a schedule-add entry. | ||
2165 | */ | ||
2166 | err = got_fileindex_entry_update(ie, worktree->root_fd, | ||
2167 | ie->path, NULL((void*)0), NULL((void*)0), 0); | ||
2168 | } else { | ||
2169 | err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE'D', ie->path); | ||
2170 | if (err) | ||
2171 | goto done; | ||
2172 | if (status == GOT_STATUS_NO_CHANGE' ') { | ||
2173 | err = remove_ondisk_file(worktree->root_path, ie->path); | ||
2174 | if (err) | ||
2175 | goto done; | ||
2176 | } | ||
2177 | got_fileindex_entry_remove(fileindex, ie); | ||
2178 | } | ||
2179 | done: | ||
2180 | free(ondisk_path); | ||
2181 | return err; | ||
2182 | } | ||
2183 | |||
2184 | struct diff_cb_arg { | ||
2185 | struct got_fileindex *fileindex; | ||
2186 | struct got_worktree *worktree; | ||
2187 | struct got_repository *repo; | ||
2188 | got_worktree_checkout_cb progress_cb; | ||
2189 | void *progress_arg; | ||
2190 | got_cancel_cb cancel_cb; | ||
2191 | void *cancel_arg; | ||
2192 | }; | ||
2193 | |||
2194 | static const struct got_error * | ||
2195 | diff_old_new(void *arg, struct got_fileindex_entry *ie, | ||
2196 | struct got_tree_entry *te, const char *parent_path) | ||
2197 | { | ||
2198 | struct diff_cb_arg *a = arg; | ||
2199 | |||
2200 | if (a->cancel_cb && a->cancel_cb(a->cancel_arg)) | ||
2201 | return got_error(GOT_ERR_CANCELLED49); | ||
2202 | |||
2203 | return update_blob(a->worktree, a->fileindex, ie, te, | ||
2204 | ie->path, a->repo, a->progress_cb, a->progress_arg); | ||
2205 | } | ||
2206 | |||
2207 | static const struct got_error * | ||
2208 | diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path) | ||
2209 | { | ||
2210 | struct diff_cb_arg *a = arg; | ||
2211 | |||
2212 | if (a->cancel_cb && a->cancel_cb(a->cancel_arg)) | ||
2213 | return got_error(GOT_ERR_CANCELLED49); | ||
2214 | |||
2215 | return delete_blob(a->worktree, a->fileindex, ie, | ||
2216 | a->repo, a->progress_cb, a->progress_arg); | ||
2217 | } | ||
2218 | |||
2219 | static const struct got_error * | ||
2220 | diff_new(void *arg, struct got_tree_entry *te, const char *parent_path) | ||
2221 | { | ||
2222 | struct diff_cb_arg *a = arg; | ||
2223 | const struct got_error *err; | ||
2224 | char *path; | ||
2225 | |||
2226 | if (a->cancel_cb && a->cancel_cb(a->cancel_arg)) | ||
2227 | return got_error(GOT_ERR_CANCELLED49); | ||
2228 | |||
2229 | if (got_object_tree_entry_is_submodule(te)) | ||
2230 | return NULL((void*)0); | ||
2231 | |||
2232 | if (asprintf(&path, "%s%s%s", parent_path, | ||
2233 | parent_path[0] ? "/" : "", te->name) | ||
2234 | == -1) | ||
2235 | return got_error_from_errno("asprintf"); | ||
2236 | |||
2237 | if (S_ISDIR(te->mode)((te->mode & 0170000) == 0040000)) | ||
2238 | err = add_dir_on_disk(a->worktree, path); | ||
2239 | else | ||
2240 | err = update_blob(a->worktree, a->fileindex, NULL((void*)0), te, path, | ||
2241 | a->repo, a->progress_cb, a->progress_arg); | ||
2242 | |||
2243 | free(path); | ||
2244 | return err; | ||
2245 | } | ||
2246 | |||
2247 | const struct got_error * | ||
2248 | got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree) | ||
2249 | { | ||
2250 | uint32_t uuid_status; | ||
2251 | |||
2252 | uuid_to_string(&worktree->uuid, uuidstr, &uuid_status); | ||
2253 | if (uuid_status != uuid_s_ok0) { | ||
2254 | *uuidstr = NULL((void*)0); | ||
2255 | return got_error_uuid(uuid_status, "uuid_to_string"); | ||
2256 | } | ||
2257 | |||
2258 | return NULL((void*)0); | ||
2259 | } | ||
2260 | |||
2261 | static const struct got_error * | ||
2262 | get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix) | ||
2263 | { | ||
2264 | const struct got_error *err = NULL((void*)0); | ||
2265 | char *uuidstr = NULL((void*)0); | ||
2266 | |||
2267 | *refname = NULL((void*)0); | ||
2268 | |||
2269 | err = got_worktree_get_uuid(&uuidstr, worktree); | ||
2270 | if (err
| ||
2271 | return err; | ||
2272 | |||
2273 | if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) { | ||
2274 | err = got_error_from_errno("asprintf"); | ||
2275 | *refname = NULL((void*)0); | ||
2276 | } | ||
2277 | free(uuidstr); | ||
2278 | return err; | ||
2279 | } | ||
2280 | |||
2281 | const struct got_error * | ||
2282 | got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree) | ||
2283 | { | ||
2284 | return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX"refs/got/worktree/base"); | ||
2285 | } | ||
2286 | |||
2287 | static const struct got_error * | ||
2288 | get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree) | ||
2289 | { | ||
2290 | return get_ref_name(refname, worktree, | ||
2291 | GOT_WORKTREE_REBASE_TMP_REF_PREFIX"refs/got/worktree/rebase/tmp"); | ||
2292 | } | ||
2293 | |||
2294 | static const struct got_error * | ||
2295 | get_newbase_symref_name(char **refname, struct got_worktree *worktree) | ||
2296 | { | ||
2297 | return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX"refs/got/worktree/rebase/newbase"); | ||
2298 | } | ||
2299 | |||
2300 | static const struct got_error * | ||
2301 | get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree) | ||
2302 | { | ||
2303 | return get_ref_name(refname, worktree, | ||
2304 | GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX"refs/got/worktree/rebase/branch"); | ||
2305 | } | ||
2306 | |||
2307 | static const struct got_error * | ||
2308 | get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree) | ||
2309 | { | ||
2310 | return get_ref_name(refname, worktree, | ||
2311 | GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX"refs/got/worktree/rebase/commit"); | ||
2312 | } | ||
2313 | |||
2314 | static const struct got_error * | ||
2315 | get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree) | ||
2316 | { | ||
2317 | return get_ref_name(refname, worktree, | ||
2318 | GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX"refs/got/worktree/histedit/tmp"); | ||
2319 | } | ||
2320 | |||
2321 | static const struct got_error * | ||
2322 | get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree) | ||
2323 | { | ||
2324 | return get_ref_name(refname, worktree, | ||
2325 | GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX"refs/got/worktree/histedit/branch"); | ||
2326 | } | ||
2327 | |||
2328 | static const struct got_error * | ||
2329 | get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree) | ||
2330 | { | ||
2331 | return get_ref_name(refname, worktree, | ||
2332 | GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX"refs/got/worktree/histedit/base-commit"); | ||
2333 | } | ||
2334 | |||
2335 | static const struct got_error * | ||
2336 | get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree) | ||
2337 | { | ||
2338 | return get_ref_name(refname, worktree, | ||
2339 | GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX"refs/got/worktree/histedit/commit"); | ||
2340 | } | ||
2341 | |||
2342 | const struct got_error * | ||
2343 | got_worktree_get_histedit_script_path(char **path, | ||
2344 | struct got_worktree *worktree) | ||
2345 | { | ||
2346 | if (asprintf(path, "%s/%s/%s", worktree->root_path, | ||
2347 | GOT_WORKTREE_GOT_DIR".got", GOT_WORKTREE_HISTEDIT_SCRIPT"histedit-script") == -1) { | ||
2348 | *path = NULL((void*)0); | ||
2349 | return got_error_from_errno("asprintf"); | ||
2350 | } | ||
2351 | return NULL((void*)0); | ||
2352 | } | ||
2353 | |||
2354 | /* | ||
2355 | * Prevent Git's garbage collector from deleting our base commit by | ||
2356 | * setting a reference to our base commit's ID. | ||
2357 | */ | ||
2358 | static const struct got_error * | ||
2359 | ref_base_commit(struct got_worktree *worktree, struct got_repository *repo) | ||
2360 | { | ||
2361 | const struct got_error *err = NULL((void*)0); | ||
2362 | struct got_reference *ref = NULL((void*)0); | ||
2363 | char *refname; | ||
2364 | |||
2365 | err = got_worktree_get_base_ref_name(&refname, worktree); | ||
2366 | if (err) | ||
2367 | return err; | ||
2368 | |||
2369 | err = got_ref_alloc(&ref, refname, worktree->base_commit_id); | ||
2370 | if (err) | ||
2371 | goto done; | ||
2372 | |||
2373 | err = got_ref_write(ref, repo); | ||
2374 | done: | ||
2375 | free(refname); | ||
2376 | if (ref) | ||
2377 | got_ref_close(ref); | ||
2378 | return err; | ||
2379 | } | ||
2380 | |||
2381 | static const struct got_error * | ||
2382 | get_fileindex_path(char **fileindex_path, struct got_worktree *worktree) | ||
2383 | { | ||
2384 | const struct got_error *err = NULL((void*)0); | ||
2385 | |||
2386 | if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path, | ||
2387 | GOT_WORKTREE_GOT_DIR".got", GOT_WORKTREE_FILE_INDEX"file-index") == -1) { | ||
2388 | err = got_error_from_errno("asprintf"); | ||
2389 | *fileindex_path = NULL((void*)0); | ||
2390 | } | ||
2391 | return err; | ||
2392 | } | ||
2393 | |||
2394 | |||
2395 | static const struct got_error * | ||
2396 | open_fileindex(struct got_fileindex **fileindex, char **fileindex_path, | ||
2397 | struct got_worktree *worktree) | ||
2398 | { | ||
2399 | const struct got_error *err = NULL((void*)0); | ||
2400 | FILE *index = NULL((void*)0); | ||
2401 | |||
2402 | *fileindex_path = NULL((void*)0); | ||
2403 | *fileindex = got_fileindex_alloc(); | ||
2404 | if (*fileindex == NULL((void*)0)) | ||
2405 | return got_error_from_errno("got_fileindex_alloc"); | ||
2406 | |||
2407 | err = get_fileindex_path(fileindex_path, worktree); | ||
2408 | if (err) | ||
2409 | goto done; | ||
2410 | |||
2411 | index = fopen(*fileindex_path, "rb"); | ||
2412 | if (index == NULL((void*)0)) { | ||
2413 | if (errno(*__errno()) != ENOENT2) | ||
2414 | err = got_error_from_errno2("fopen", *fileindex_path); | ||
2415 | } else { | ||
2416 | err = got_fileindex_read(*fileindex, index); | ||
2417 | if (fclose(index) == EOF(-1) && err == NULL((void*)0)) | ||
2418 | err = got_error_from_errno("fclose"); | ||
2419 | } | ||
2420 | done: | ||
2421 | if (err) { | ||
2422 | free(*fileindex_path); | ||
2423 | *fileindex_path = NULL((void*)0); | ||
2424 | got_fileindex_free(*fileindex); | ||
2425 | *fileindex = NULL((void*)0); | ||
2426 | } | ||
2427 | return err; | ||
2428 | } | ||
2429 | |||
2430 | struct bump_base_commit_id_arg { | ||
2431 | struct got_object_id *base_commit_id; | ||
2432 | const char *path; | ||
2433 | size_t path_len; | ||
2434 | const char *entry_name; | ||
2435 | got_worktree_checkout_cb progress_cb; | ||
2436 | void *progress_arg; | ||
2437 | }; | ||
2438 | |||
2439 | /* Bump base commit ID of all files within an updated part of the work tree. */ | ||
2440 | static const struct got_error * | ||
2441 | bump_base_commit_id(void *arg, struct got_fileindex_entry *ie) | ||
2442 | { | ||
2443 | const struct got_error *err; | ||
2444 | struct bump_base_commit_id_arg *a = arg; | ||
2445 | |||
2446 | if (a->entry_name) { | ||
2447 | if (strcmp(ie->path, a->path) != 0) | ||
2448 | return NULL((void*)0); | ||
2449 | } else if (!got_path_is_child(ie->path, a->path, a->path_len)) | ||
2450 | return NULL((void*)0); | ||
2451 | |||
2452 | if (memcmp(ie->commit_sha1, a->base_commit_id->sha1, | ||
2453 | SHA1_DIGEST_LENGTH20) == 0) | ||
2454 | return NULL((void*)0); | ||
2455 | |||
2456 | if (a->progress_cb) { | ||
2457 | err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE'b', | ||
2458 | ie->path); | ||
2459 | if (err) | ||
2460 | return err; | ||
2461 | } | ||
2462 | memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH20); | ||
2463 | return NULL((void*)0); | ||
2464 | } | ||
2465 | |||
2466 | static const struct got_error * | ||
2467 | bump_base_commit_id_everywhere(struct got_worktree *worktree, | ||
2468 | struct got_fileindex *fileindex, | ||
2469 | got_worktree_checkout_cb progress_cb, void *progress_arg) | ||
2470 | { | ||
2471 | struct bump_base_commit_id_arg bbc_arg; | ||
2472 | |||
2473 | bbc_arg.base_commit_id = worktree->base_commit_id; | ||
2474 | bbc_arg.entry_name = NULL((void*)0); | ||
2475 | bbc_arg.path = ""; | ||
2476 | bbc_arg.path_len = 0; | ||
2477 | bbc_arg.progress_cb = progress_cb; | ||
2478 | bbc_arg.progress_arg = progress_arg; | ||
2479 | |||
2480 | return got_fileindex_for_each_entry_safe(fileindex, | ||
2481 | bump_base_commit_id, &bbc_arg); | ||
2482 | } | ||
2483 | |||
2484 | static const struct got_error * | ||
2485 | sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path) | ||
2486 | { | ||
2487 | const struct got_error *err = NULL((void*)0); | ||
2488 | char *new_fileindex_path = NULL((void*)0); | ||
2489 | FILE *new_index = NULL((void*)0); | ||
2490 | struct timespec timeout; | ||
2491 | |||
2492 | err = got_opentemp_named(&new_fileindex_path, &new_index, | ||
2493 | fileindex_path); | ||
2494 | if (err) | ||
2495 | goto done; | ||
2496 | |||
2497 | err = got_fileindex_write(fileindex, new_index); | ||
2498 | if (err) | ||
2499 | goto done; | ||
2500 | |||
2501 | if (rename(new_fileindex_path, fileindex_path) != 0) { | ||
2502 | err = got_error_from_errno3("rename", new_fileindex_path, | ||
2503 | fileindex_path); | ||
2504 | unlink(new_fileindex_path); | ||
2505 | } | ||
2506 | |||
2507 | /* | ||
2508 | * Sleep for a short amount of time to ensure that files modified after | ||
2509 | * this program exits have a different time stamp from the one which | ||
2510 | * was recorded in the file index. | ||
2511 | */ | ||
2512 | timeout.tv_sec = 0; | ||
2513 | timeout.tv_nsec = 1; | ||
2514 | nanosleep(&timeout, NULL((void*)0)); | ||
2515 | done: | ||
2516 | if (new_index) | ||
2517 | fclose(new_index); | ||
2518 | free(new_fileindex_path); | ||
2519 | return err; | ||
2520 | } | ||
2521 | |||
2522 | static const struct got_error * | ||
2523 | find_tree_entry_for_checkout(int *entry_type, char **tree_relpath, | ||
2524 | struct got_object_id **tree_id, const char *wt_relpath, | ||
2525 | struct got_worktree *worktree, struct got_repository *repo) | ||
2526 | { | ||
2527 | const struct got_error *err = NULL((void*)0); | ||
2528 | struct got_object_id *id = NULL((void*)0); | ||
2529 | char *in_repo_path = NULL((void*)0); | ||
2530 | int is_root_wt = got_path_is_root_dir(worktree->path_prefix); | ||
2531 | |||
2532 | *entry_type = GOT_OBJ_TYPE_ANY0; | ||
2533 | *tree_relpath = NULL((void*)0); | ||
2534 | *tree_id = NULL((void*)0); | ||
2535 | |||
2536 | if (wt_relpath[0] == '\0') { | ||
2537 | /* Check out all files within the work tree. */ | ||
2538 | *entry_type = GOT_OBJ_TYPE_TREE2; | ||
2539 | *tree_relpath = strdup(""); | ||
2540 | if (*tree_relpath == NULL((void*)0)) { | ||
2541 | err = got_error_from_errno("strdup"); | ||
2542 | goto done; | ||
2543 | } | ||
2544 | err = got_object_id_by_path(tree_id, repo, | ||
2545 | worktree->base_commit_id, worktree->path_prefix); | ||
2546 | if (err) | ||
2547 | goto done; | ||
2548 | return NULL((void*)0); | ||
2549 | } | ||
2550 | |||
2551 | /* Check out a subset of files in the work tree. */ | ||
2552 | |||
2553 | if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix, | ||
2554 | is_root_wt ? "" : "/", wt_relpath) == -1) { | ||
2555 | err = got_error_from_errno("asprintf"); | ||
2556 | goto done; | ||
2557 | } | ||
2558 | |||
2559 | err = got_object_id_by_path(&id, repo, worktree->base_commit_id, | ||
2560 | in_repo_path); | ||
2561 | if (err) | ||
2562 | goto done; | ||
2563 | |||
2564 | free(in_repo_path); | ||
2565 | in_repo_path = NULL((void*)0); | ||
2566 | |||
2567 | err = got_object_get_type(entry_type, repo, id); | ||
2568 | if (err) | ||
2569 | goto done; | ||
2570 | |||
2571 | if (*entry_type == GOT_OBJ_TYPE_BLOB3) { | ||
2572 | /* Check out a single file. */ | ||
2573 | if (strchr(wt_relpath, '/') == NULL((void*)0)) { | ||
2574 | /* Check out a single file in work tree's root dir. */ | ||
2575 | in_repo_path = strdup(worktree->path_prefix); | ||
2576 | if (in_repo_path == NULL((void*)0)) { | ||
2577 | err = got_error_from_errno("strdup"); | ||
2578 | goto done; | ||
2579 | } | ||
2580 | *tree_relpath = strdup(""); | ||
2581 | if (*tree_relpath == NULL((void*)0)) { | ||
2582 | err = got_error_from_errno("strdup"); | ||
2583 | goto done; | ||
2584 | } | ||
2585 | } else { | ||
2586 | /* Check out a single file in a subdirectory. */ | ||
2587 | err = got_path_dirname(tree_relpath, wt_relpath); | ||
2588 | if (err) | ||
2589 | return err; | ||
2590 | if (asprintf(&in_repo_path, "%s%s%s", | ||
2591 | worktree->path_prefix, is_root_wt ? "" : "/", | ||
2592 | *tree_relpath) == -1) { | ||
2593 | err = got_error_from_errno("asprintf"); | ||
2594 | goto done; | ||
2595 | } | ||
2596 | } | ||
2597 | err = got_object_id_by_path(tree_id, repo, | ||
2598 | worktree->base_commit_id, in_repo_path); | ||
2599 | } else { | ||
2600 | /* Check out all files within a subdirectory. */ | ||
2601 | *tree_id = got_object_id_dup(id); | ||
2602 | if (*tree_id == NULL((void*)0)) { | ||
2603 | err = got_error_from_errno("got_object_id_dup"); | ||
2604 | goto done; | ||
2605 | } | ||
2606 | *tree_relpath = strdup(wt_relpath); | ||
2607 | if (*tree_relpath == NULL((void*)0)) { | ||
2608 | err = got_error_from_errno("strdup"); | ||
2609 | goto done; | ||
2610 | } | ||
2611 | } | ||
2612 | done: | ||
2613 | free(id); | ||
2614 | free(in_repo_path); | ||
2615 | if (err) { | ||
2616 | *entry_type = GOT_OBJ_TYPE_ANY0; | ||
2617 | free(*tree_relpath); | ||
2618 | *tree_relpath = NULL((void*)0); | ||
2619 | free(*tree_id); | ||
2620 | *tree_id = NULL((void*)0); | ||
2621 | } | ||
2622 | return err; | ||
2623 | } | ||
2624 | |||
2625 | static const struct got_error * | ||
2626 | checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex, | ||
2627 | const char *relpath, struct got_object_id *tree_id, const char *entry_name, | ||
2628 | struct got_repository *repo, got_worktree_checkout_cb progress_cb, | ||
2629 | void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg) | ||
2630 | { | ||
2631 | const struct got_error *err = NULL((void*)0); | ||
2632 | struct got_commit_object *commit = NULL((void*)0); | ||
2633 | struct got_tree_object *tree = NULL((void*)0); | ||
2634 | struct got_fileindex_diff_tree_cb diff_cb; | ||
2635 | struct diff_cb_arg arg; | ||
2636 | |||
2637 | err = ref_base_commit(worktree, repo); | ||
2638 | if (err) { | ||
2639 | if (!(err->code == GOT_ERR_ERRNO1 && | ||
2640 | (errno(*__errno()) == EACCES13 || errno(*__errno()) == EROFS30))) | ||
2641 | goto done; | ||
2642 | err = (*progress_cb)(progress_arg, | ||
2643 | GOT_STATUS_BASE_REF_ERR'B', worktree->root_path); | ||
2644 | if (err) | ||
2645 | return err; | ||
2646 | } | ||
2647 | |||
2648 | err = got_object_open_as_commit(&commit, repo, | ||
2649 | worktree->base_commit_id); | ||
2650 | if (err) | ||
2651 | goto done; | ||
2652 | |||
2653 | err = got_object_open_as_tree(&tree, repo, tree_id); | ||
2654 | if (err) | ||
2655 | goto done; | ||
2656 | |||
2657 | if (entry_name && | ||
2658 | got_object_tree_find_entry(tree, entry_name) == NULL((void*)0)) { | ||
2659 | err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY50); | ||
2660 | goto done; | ||
2661 | } | ||
2662 | |||
2663 | diff_cb.diff_old_new = diff_old_new; | ||
2664 | diff_cb.diff_old = diff_old; | ||
2665 | diff_cb.diff_new = diff_new; | ||
2666 | arg.fileindex = fileindex; | ||
2667 | arg.worktree = worktree; | ||
2668 | arg.repo = repo; | ||
2669 | arg.progress_cb = progress_cb; | ||
2670 | arg.progress_arg = progress_arg; | ||
2671 | arg.cancel_cb = cancel_cb; | ||
2672 | arg.cancel_arg = cancel_arg; | ||
2673 | err = got_fileindex_diff_tree(fileindex, tree, relpath, | ||
2674 | entry_name, repo, &diff_cb, &arg); | ||
2675 | done: | ||
2676 | if (tree) | ||
2677 | got_object_tree_close(tree); | ||
2678 | if (commit) | ||
2679 | got_object_commit_close(commit); | ||
2680 | return err; | ||
2681 | } | ||
2682 | |||
2683 | const struct got_error * | ||
2684 | got_worktree_checkout_files(struct got_worktree *worktree, | ||
2685 | struct got_pathlist_head *paths, struct got_repository *repo, | ||
2686 | got_worktree_checkout_cb progress_cb, void *progress_arg, | ||
2687 | got_cancel_cb cancel_cb, void *cancel_arg) | ||
2688 | { | ||
2689 | const struct got_error *err = NULL((void*)0), *sync_err, *unlockerr; | ||
2690 | struct got_commit_object *commit = NULL((void*)0); | ||
2691 | struct got_tree_object *tree = NULL((void*)0); | ||
2692 | struct got_fileindex *fileindex = NULL((void*)0); | ||
2693 | char *fileindex_path = NULL((void*)0); | ||
2694 | struct got_pathlist_entry *pe; | ||
2695 | struct tree_path_data { | ||
2696 | SIMPLEQ_ENTRY(tree_path_data)struct { struct tree_path_data *sqe_next; } entry; | ||
2697 | struct got_object_id *tree_id; | ||
2698 | int entry_type; | ||
2699 | char *relpath; | ||
2700 | char *entry_name; | ||
2701 | } *tpd = NULL((void*)0); | ||
2702 | SIMPLEQ_HEAD(tree_paths, tree_path_data)struct tree_paths { struct tree_path_data *sqh_first; struct tree_path_data **sqh_last; } tree_paths; | ||
2703 | |||
2704 | SIMPLEQ_INIT(&tree_paths)do { (&tree_paths)->sqh_first = ((void*)0); (&tree_paths )->sqh_last = &(&tree_paths)->sqh_first; } while (0); | ||
2705 | |||
2706 | err = lock_worktree(worktree, LOCK_EX0x02); | ||
2707 | if (err) | ||
2708 | return err; | ||
2709 | |||
2710 | /* Map all specified paths to in-repository trees. */ | ||
2711 | TAILQ_FOREACH(pe, paths, entry)for((pe) = ((paths)->tqh_first); (pe) != ((void*)0); (pe) = ((pe)->entry.tqe_next)) { | ||
2712 | tpd = malloc(sizeof(*tpd)); | ||
2713 | if (tpd == NULL((void*)0)) { | ||
2714 | err = got_error_from_errno("malloc"); | ||
2715 | goto done; | ||
2716 | } | ||
2717 | |||
2718 | err = find_tree_entry_for_checkout(&tpd->entry_type, | ||
2719 | &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo); | ||
2720 | if (err) { | ||
2721 | free(tpd); | ||
2722 | goto done; | ||
2723 | } | ||
2724 | |||
2725 | if (tpd->entry_type == GOT_OBJ_TYPE_BLOB3) { | ||
2726 | err = got_path_basename(&tpd->entry_name, pe->path); | ||
2727 | if (err) { | ||
2728 | free(tpd->relpath); | ||
2729 | free(tpd->tree_id); | ||
2730 | free(tpd); | ||
2731 | goto done; | ||
2732 | } | ||
2733 | } else | ||
2734 | tpd->entry_name = NULL((void*)0); | ||
2735 | |||
2736 | SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry)do { (tpd)->entry.sqe_next = ((void*)0); *(&tree_paths )->sqh_last = (tpd); (&tree_paths)->sqh_last = & (tpd)->entry.sqe_next; } while (0); | ||
2737 | } | ||
2738 | |||
2739 | /* | ||
2740 | * Read the file index. | ||
2741 | * Checking out files is supposed to be an idempotent operation. | ||
2742 | * If the on-disk file index is incomplete we will try to complete it. | ||
2743 | */ | ||
2744 | err = open_fileindex(&fileindex, &fileindex_path, worktree); | ||
2745 | if (err) | ||
2746 | goto done; | ||
2747 | |||
2748 | tpd = SIMPLEQ_FIRST(&tree_paths)((&tree_paths)->sqh_first); | ||
2749 | TAILQ_FOREACH(pe, paths, entry)for((pe) = ((paths)->tqh_first); (pe) != ((void*)0); (pe) = ((pe)->entry.tqe_next)) { | ||
2750 | struct bump_base_commit_id_arg bbc_arg; | ||
2751 | |||
2752 | err = checkout_files(worktree, fileindex, tpd->relpath, | ||
2753 | tpd->tree_id, tpd->entry_name, repo, | ||
2754 | progress_cb, progress_arg, cancel_cb, cancel_arg); | ||
2755 | if (err) | ||
2756 | break; | ||
2757 | |||
2758 | bbc_arg.base_commit_id = worktree->base_commit_id; | ||
2759 | bbc_arg.entry_name = tpd->entry_name; | ||
2760 | bbc_arg.path = pe->path; | ||
2761 | bbc_arg.path_len = pe->path_len; | ||
2762 | bbc_arg.progress_cb = progress_cb; | ||
2763 | bbc_arg.progress_arg = progress_arg; | ||
2764 | err = got_fileindex_for_each_entry_safe(fileindex, | ||
2765 | bump_base_commit_id, &bbc_arg); | ||
2766 | if (err) | ||
2767 | break; | ||
2768 | |||
2769 | tpd = SIMPLEQ_NEXT(tpd, entry)((tpd)->entry.sqe_next); | ||
2770 | } | ||
2771 | sync_err = sync_fileindex(fileindex, fileindex_path); | ||
2772 | if (sync_err && err == NULL((void*)0)) | ||
2773 | err = sync_err; | ||
2774 | done: | ||
2775 | free(fileindex_path); | ||
2776 | if (tree) | ||
2777 | got_object_tree_close(tree); | ||
2778 | if (commit) | ||
2779 | got_object_commit_close(commit); | ||
2780 | if (fileindex) | ||
2781 | got_fileindex_free(fileindex); | ||
2782 | while (!SIMPLEQ_EMPTY(&tree_paths)(((&tree_paths)->sqh_first) == ((void*)0))) { | ||
2783 | tpd = SIMPLEQ_FIRST(&tree_paths)((&tree_paths)->sqh_first); | ||
2784 | SIMPLEQ_REMOVE_HEAD(&tree_paths, entry)do { if (((&tree_paths)->sqh_first = (&tree_paths) ->sqh_first->entry.sqe_next) == ((void*)0)) (&tree_paths )->sqh_last = &(&tree_paths)->sqh_first; } while (0); | ||
2785 | free(tpd->relpath); | ||
2786 | free(tpd->tree_id); | ||
2787 | free(tpd); | ||
2788 | } | ||
2789 | unlockerr = lock_worktree(worktree, LOCK_SH0x01); | ||
2790 | if (unlockerr && err == NULL((void*)0)) | ||
2791 | err = unlockerr; | ||
2792 | return err; | ||
2793 | } | ||
2794 | |||
2795 | struct merge_file_cb_arg { | ||
2796 | struct got_worktree *worktree; | ||
2797 | struct got_fileindex *fileindex; | ||
2798 | got_worktree_checkout_cb progress_cb; | ||
2799 | void *progress_arg; | ||
2800 | got_cancel_cb cancel_cb; | ||
2801 | void *cancel_arg; | ||
2802 | const char *label_orig; | ||
2803 | struct got_object_id *commit_id2; | ||
2804 | }; | ||
2805 | |||
2806 | static const struct got_error * | ||
2807 | merge_file_cb(void *arg, struct got_blob_object *blob1, | ||
2808 | struct got_blob_object *blob2, struct got_object_id *id1, | ||
2809 | struct got_object_id *id2, const char *path1, const char *path2, | ||
2810 | mode_t mode1, mode_t mode2, struct got_repository *repo) | ||
2811 | { | ||
2812 | static const struct got_error *err = NULL((void*)0); | ||
2813 | struct merge_file_cb_arg *a = arg; | ||
2814 | struct got_fileindex_entry *ie; | ||
2815 | char *ondisk_path = NULL((void*)0); | ||
2816 | struct stat sb; | ||
2817 | unsigned char status; | ||
2818 | int local_changes_subsumed; | ||
2819 | |||
2820 | if (blob1 && blob2) { | ||
2821 | ie = got_fileindex_entry_get(a->fileindex, path2, | ||
2822 | strlen(path2)); | ||
2823 | if (ie == NULL((void*)0)) | ||
2824 | return (*a->progress_cb)(a->progress_arg, | ||
2825 | GOT_STATUS_MISSING'!', path2); | ||
2826 | |||
2827 | if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, | ||
2828 | path2) == -1) | ||
2829 | return got_error_from_errno("asprintf"); | ||
2830 | |||
2831 | err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL((void*)0), | ||
2832 | repo); | ||
2833 | if (err) | ||
2834 | goto done; | ||
2835 | |||
2836 | if (status == GOT_STATUS_DELETE'D') { | ||
2837 | err = (*a->progress_cb)(a->progress_arg, | ||
2838 | GOT_STATUS_MERGE'G', path2); | ||
2839 | goto done; | ||
2840 | } | ||
2841 | if (status != GOT_STATUS_NO_CHANGE' ' && | ||
2842 | status != GOT_STATUS_MODIFY'M' && | ||
2843 | status != GOT_STATUS_CONFLICT'C' && | ||
2844 | status != GOT_STATUS_ADD'A') { | ||
2845 | err = (*a->progress_cb)(a->progress_arg, status, path2); | ||
2846 | goto done; | ||
2847 | } | ||
2848 | |||
2849 | if (S_ISLNK(mode1)((mode1 & 0170000) == 0120000) && S_ISLNK(mode2)((mode2 & 0170000) == 0120000)) { | ||
2850 | char *link_target2; | ||
2851 | err = got_object_blob_read_to_str(&link_target2, blob2); | ||
2852 | if (err) | ||
2853 | goto done; | ||
2854 | err = merge_symlink(a->worktree, blob1, ondisk_path, | ||
2855 | path2, a->label_orig, link_target2, a->commit_id2, | ||
2856 | repo, a->progress_cb, a->progress_arg); | ||
2857 | free(link_target2); | ||
2858 | } else { | ||
2859 | err = merge_blob(&local_changes_subsumed, a->worktree, | ||
2860 | blob1, ondisk_path, path2, sb.st_mode, | ||
2861 | a->label_orig, blob2, a->commit_id2, repo, | ||
2862 | a->progress_cb, a->progress_arg); | ||
2863 | } | ||
2864 | } else if (blob1) { | ||
2865 | ie = got_fileindex_entry_get(a->fileindex, path1, | ||
2866 | strlen(path1)); | ||
2867 | if (ie == NULL((void*)0)) | ||
2868 | return (*a->progress_cb)(a->progress_arg, | ||
2869 | GOT_STATUS_MISSING'!', path1); | ||
2870 | |||
2871 | if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, | ||
2872 | path1) == -1) | ||
2873 | return got_error_from_errno("asprintf"); | ||
2874 | |||
2875 | err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL((void*)0), | ||
2876 | repo); | ||
2877 | if (err) | ||
2878 | goto done; | ||
2879 | |||
2880 | switch (status) { | ||
2881 | case GOT_STATUS_NO_CHANGE' ': | ||
2882 | err = (*a->progress_cb)(a->progress_arg, | ||
2883 | GOT_STATUS_DELETE'D', path1); | ||
2884 | if (err) | ||
2885 | goto done; | ||
2886 | err = remove_ondisk_file(a->worktree->root_path, path1); | ||
2887 | if (err) | ||
2888 | goto done; | ||
2889 | if (ie) | ||
2890 | got_fileindex_entry_mark_deleted_from_disk(ie); | ||
2891 | break; | ||
2892 | case GOT_STATUS_DELETE'D': | ||
2893 | case GOT_STATUS_MISSING'!': | ||
2894 | err = (*a->progress_cb)(a->progress_arg, | ||
2895 | GOT_STATUS_DELETE'D', path1); | ||
2896 | if (err) | ||
2897 | goto done; | ||
2898 | if (ie) | ||
2899 | got_fileindex_entry_mark_deleted_from_disk(ie); | ||
2900 | break; | ||
2901 | case GOT_STATUS_ADD'A': { | ||
2902 | struct got_object_id *id; | ||
2903 | FILE *blob1_f; | ||
2904 | /* | ||
2905 | * Delete the added file only if its content already | ||
2906 | * exists in the repository. | ||
2907 | */ | ||
2908 | err = got_object_blob_file_create(&id, &blob1_f, path1); | ||
2909 | if (err) | ||
2910 | goto done; | ||
2911 | if (got_object_id_cmp(id, id1) == 0) { | ||
2912 | err = (*a->progress_cb)(a->progress_arg, | ||
2913 | GOT_STATUS_DELETE'D', path1); | ||
2914 | if (err) | ||
2915 | goto done; | ||
2916 | err = remove_ondisk_file(a->worktree->root_path, | ||
2917 | path1); | ||
2918 | if (err) | ||
2919 | goto done; | ||
2920 | if (ie) | ||
2921 | got_fileindex_entry_remove(a->fileindex, | ||
2922 | ie); | ||
2923 | } else { | ||
2924 | err = (*a->progress_cb)(a->progress_arg, | ||
2925 | GOT_STATUS_CANNOT_DELETE'd', path1); | ||
2926 | } | ||
2927 | if (fclose(blob1_f) == EOF(-1) && err == NULL((void*)0)) | ||
2928 | err = got_error_from_errno("fclose"); | ||
2929 | free(id); | ||
2930 | if (err) | ||
2931 | goto done; | ||
2932 | break; | ||
2933 | } | ||
2934 | case GOT_STATUS_MODIFY'M': | ||
2935 | case GOT_STATUS_CONFLICT'C': | ||
2936 | err = (*a->progress_cb)(a->progress_arg, | ||
2937 | GOT_STATUS_CANNOT_DELETE'd', path1); | ||
2938 | if (err) | ||
2939 | goto done; | ||
2940 | break; | ||
2941 | case GOT_STATUS_OBSTRUCTED'~': | ||
2942 | err = (*a->progress_cb)(a->progress_arg, status, path1); | ||
2943 | if (err) | ||
2944 | goto done; | ||
2945 | break; | ||
2946 | default: | ||
2947 | break; | ||
2948 | } | ||
2949 | } else if (blob2) { | ||
2950 | if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, | ||
2951 | path2) == -1) | ||
2952 | return got_error_from_errno("asprintf"); | ||
2953 | ie = got_fileindex_entry_get(a->fileindex, path2, | ||
2954 | strlen(path2)); | ||
2955 | if (ie) { | ||
2956 | err = get_file_status(&status, &sb, ie, ondisk_path, | ||
2957 | -1, NULL((void*)0), repo); | ||
2958 | if (err) | ||
2959 | goto done; | ||
2960 | if (status != GOT_STATUS_NO_CHANGE' ' && | ||
2961 | status != GOT_STATUS_MODIFY'M' && | ||
2962 | status != GOT_STATUS_CONFLICT'C' && | ||
2963 | status != GOT_STATUS_ADD'A') { | ||
2964 | err = (*a->progress_cb)(a->progress_arg, | ||
2965 | status, path2); | ||
2966 | goto done; | ||
2967 | } | ||
2968 | if (S_ISLNK(mode2)((mode2 & 0170000) == 0120000) && S_ISLNK(sb.st_mode)((sb.st_mode & 0170000) == 0120000)) { | ||
2969 | char *link_target2; | ||
2970 | err = got_object_blob_read_to_str(&link_target2, | ||
2971 | blob2); | ||
2972 | if (err) | ||
2973 | goto done; | ||
2974 | err = merge_symlink(a->worktree, NULL((void*)0), | ||
2975 | ondisk_path, path2, a->label_orig, | ||
2976 | link_target2, a->commit_id2, repo, | ||
2977 | a->progress_cb, a->progress_arg); | ||
2978 | free(link_target2); | ||
2979 | } else if (S_ISREG(sb.st_mode)((sb.st_mode & 0170000) == 0100000)) { | ||
2980 | err = merge_blob(&local_changes_subsumed, | ||
2981 | a->worktree, NULL((void*)0), ondisk_path, path2, | ||
2982 | sb.st_mode, a->label_orig, blob2, | ||
2983 | a->commit_id2, repo, a->progress_cb, | ||
2984 | a->progress_arg); | ||
2985 | } else { | ||
2986 | err = got_error_path(ondisk_path, | ||
2987 | GOT_ERR_FILE_OBSTRUCTED31); | ||
2988 | } | ||
2989 | if (err) | ||
2990 | goto done; | ||
2991 | if (status == GOT_STATUS_DELETE'D') { | ||
2992 | err = got_fileindex_entry_update(ie, | ||
2993 | a->worktree->root_fd, path2, blob2->id.sha1, | ||
2994 | a->worktree->base_commit_id->sha1, 0); | ||
2995 | if (err) | ||
2996 | goto done; | ||
2997 | } | ||
2998 | } else { | ||
2999 | int is_bad_symlink = 0; | ||
3000 | sb.st_mode = GOT_DEFAULT_FILE_MODE(0100000 | 0000400|0000200 | 0000040 | 0000004); | ||
3001 | if (S_ISLNK(mode2)((mode2 & 0170000) == 0120000)) { | ||
3002 | err = install_symlink(&is_bad_symlink, | ||
3003 | a->worktree, ondisk_path, path2, blob2, 0, | ||
3004 | 0, 1, repo, a->progress_cb, a->progress_arg); | ||
3005 | } else { | ||
3006 | err = install_blob(a->worktree, ondisk_path, path2, | ||
3007 | mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo, | ||
3008 | a->progress_cb, a->progress_arg); | ||
3009 | } | ||
3010 | if (err) | ||
3011 | goto done; | ||
3012 | err = got_fileindex_entry_alloc(&ie, path2); | ||
3013 | if (err) | ||
3014 | goto done; | ||
3015 | err = got_fileindex_entry_update(ie, | ||
3016 | a->worktree->root_fd, path2, NULL((void*)0), NULL((void*)0), 1); | ||
3017 | if (err) { | ||
3018 | got_fileindex_entry_free(ie); | ||
3019 | goto done; | ||
3020 | } | ||
3021 | err = got_fileindex_entry_add(a->fileindex, ie); | ||
3022 | if (err) { | ||
3023 | got_fileindex_entry_free(ie); | ||
3024 | goto done; | ||
3025 | } | ||
3026 | if (is_bad_symlink) { | ||
3027 | got_fileindex_entry_filetype_set(ie, | ||
3028 | GOT_FILEIDX_MODE_BAD_SYMLINK3); | ||
3029 | } | ||
3030 | } | ||
3031 | } | ||
3032 | done: | ||
3033 | free(ondisk_path); | ||
3034 | return err; | ||
3035 | } | ||
3036 | |||
3037 | struct check_merge_ok_arg { | ||
3038 | struct got_worktree *worktree; | ||
3039 | struct got_repository *repo; | ||
3040 | }; | ||
3041 | |||
3042 | static const struct got_error * | ||
3043 | check_merge_ok(void *arg, struct got_fileindex_entry *ie) | ||
3044 | { | ||
3045 | const struct got_error *err = NULL((void*)0); | ||
3046 | struct check_merge_ok_arg *a = arg; | ||
3047 | unsigned char status; | ||
3048 | struct stat sb; | ||
3049 | char *ondisk_path; | ||
3050 | |||
3051 | /* Reject merges into a work tree with mixed base commits. */ | ||
3052 | if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1, | ||
3053 | SHA1_DIGEST_LENGTH20)) | ||
3054 | return got_error(GOT_ERR_MIXED_COMMITS81); | ||
3055 | |||
3056 | if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path) | ||
3057 | == -1) | ||
3058 | return got_error_from_errno("asprintf"); | ||
3059 | |||
3060 | /* Reject merges into a work tree with conflicted files. */ | ||
3061 | err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL((void*)0), a->repo); | ||
3062 | if (err) | ||
3063 | return err; | ||
3064 | if (status == GOT_STATUS_CONFLICT'C') | ||
3065 | return got_error(GOT_ERR_CONFLICTS82); | ||
3066 | |||
3067 | return NULL((void*)0); | ||
3068 | } | ||
3069 | |||
3070 | static const struct got_error * | ||
3071 | merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex, | ||
3072 | const char *fileindex_path, struct got_object_id *commit_id1, | ||
3073 | struct got_object_id *commit_id2, struct got_repository *repo, | ||
3074 | got_worktree_checkout_cb progress_cb, void *progress_arg, | ||
3075 | got_cancel_cb cancel_cb, void *cancel_arg) | ||
3076 | { | ||
3077 | const struct got_error *err = NULL((void*)0), *sync_err; | ||
3078 | struct got_object_id *tree_id1 = NULL((void*)0), *tree_id2 = NULL((void*)0); | ||
3079 | struct got_tree_object *tree1 = NULL((void*)0), *tree2 = NULL((void*)0); | ||
3080 | struct merge_file_cb_arg arg; | ||
3081 | char *label_orig = NULL((void*)0); | ||
3082 | |||
3083 | if (commit_id1) { | ||
3084 | err = got_object_id_by_path(&tree_id1, repo, commit_id1, | ||
3085 | worktree->path_prefix); | ||
3086 | if (err && err->code != GOT_ERR_NO_TREE_ENTRY50) | ||
3087 | goto done; | ||
3088 | } | ||
3089 | if (tree_id1) { | ||
3090 | char *id_str; | ||
3091 | |||
3092 | err = got_object_open_as_tree(&tree1, repo, tree_id1); | ||
3093 | if (err) | ||
3094 | goto done; | ||
3095 | |||
3096 | err = got_object_id_str(&id_str, commit_id1); | ||
3097 | if (err) | ||
3098 | goto done; | ||
3099 | |||
3100 | if (asprintf(&label_orig, "%s: commit %s", | ||
3101 | GOT_MERGE_LABEL_BASE"3-way merge base", id_str) == -1) { | ||
3102 | err = got_error_from_errno("asprintf"); | ||
3103 | free(id_str); | ||
3104 | goto done; | ||
3105 | } | ||
3106 | free(id_str); | ||
3107 | } | ||
3108 | |||
3109 | err = got_object_id_by_path(&tree_id2, repo, commit_id2, | ||
3110 | worktree->path_prefix); | ||
3111 | if (err) | ||
3112 | goto done; | ||
3113 | |||
3114 | err = got_object_open_as_tree(&tree2, repo, tree_id2); | ||
3115 | if (err) | ||
3116 | goto done; | ||
3117 | |||
3118 | arg.worktree = worktree; | ||
3119 | arg.fileindex = fileindex; | ||
3120 | arg.progress_cb = progress_cb; | ||
3121 | arg.progress_arg = progress_arg; | ||
3122 | arg.cancel_cb = cancel_cb; | ||
3123 | arg.cancel_arg = cancel_arg; | ||
3124 | arg.label_orig = label_orig; | ||
3125 | arg.commit_id2 = commit_id2; | ||
3126 | err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1); | ||
3127 | sync_err = sync_fileindex(fileindex, fileindex_path); | ||
3128 | if (sync_err && err == NULL((void*)0)) | ||
3129 | err = sync_err; | ||
3130 | done: | ||
3131 | if (tree1) | ||
3132 | got_object_tree_close(tree1); | ||
3133 | if (tree2) | ||
3134 | got_object_tree_close(tree2); | ||
3135 | free(label_orig); | ||
3136 | return err; | ||
3137 | } | ||
3138 | |||
3139 | const struct got_error * | ||
3140 | got_worktree_merge_files(struct got_worktree *worktree, | ||
3141 | struct got_object_id *commit_id1, struct got_object_id *commit_id2, | ||
3142 | struct got_repository *repo, got_worktree_checkout_cb progress_cb, | ||
3143 | void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg) | ||
3144 | { | ||
3145 | const struct got_error *err, *unlockerr; | ||
3146 | char *fileindex_path = NULL((void*)0); | ||
3147 | struct got_fileindex *fileindex = NULL((void*)0); | ||
3148 | struct check_merge_ok_arg mok_arg; | ||
3149 | |||
3150 | err = lock_worktree(worktree, LOCK_EX0x02); | ||
3151 | if (err) | ||
3152 | return err; | ||
3153 | |||
3154 | err = open_fileindex(&fileindex, &fileindex_path, worktree); | ||
3155 | if (err) | ||
3156 | goto done; | ||
3157 | |||
3158 | mok_arg.worktree = worktree; | ||
3159 | mok_arg.repo = repo; | ||
3160 | err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok, | ||
3161 | &mok_arg); | ||
3162 | if (err) | ||
3163 | goto done; | ||
3164 | |||
3165 | err = merge_files(worktree, fileindex, fileindex_path, commit_id1, | ||
3166 | commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg); | ||
3167 | done: | ||
3168 | if (fileindex) | ||
3169 | got_fileindex_free(fileindex); | ||
3170 | free(fileindex_path); | ||
3171 | unlockerr = lock_worktree(worktree, LOCK_SH0x01); | ||
3172 | if (unlockerr && err == NULL((void*)0)) | ||
3173 | err = unlockerr; | ||
3174 | return err; | ||
3175 | } | ||
3176 | |||
3177 | struct diff_dir_cb_arg { | ||
3178 | struct got_fileindex *fileindex; | ||
3179 | struct got_worktree *worktree; | ||
3180 | const char *status_path; | ||
3181 | size_t status_path_len; | ||
3182 | struct got_repository *repo; | ||
3183 | got_worktree_status_cb status_cb; | ||
3184 | void *status_arg; | ||
3185 | got_cancel_cb cancel_cb; | ||
3186 | void *cancel_arg; | ||
3187 | /* A pathlist containing per-directory pathlists of ignore patterns. */ | ||
3188 | struct got_pathlist_head ignores; | ||
3189 | int report_unchanged; | ||
3190 | int no_ignores; | ||
3191 | }; | ||
3192 | |||
3193 | static const struct got_error * | ||
3194 | report_file_status(struct got_fileindex_entry *ie, const char *abspath, | ||
3195 | int dirfd, const char *de_name, | ||
3196 | got_worktree_status_cb status_cb, void *status_arg, | ||
3197 | struct got_repository *repo, int report_unchanged) | ||
3198 | { | ||
3199 | const struct got_error *err = NULL((void*)0); | ||
3200 | unsigned char status = GOT_STATUS_NO_CHANGE' '; | ||
3201 | unsigned char staged_status = get_staged_status(ie); | ||
3202 | struct stat sb; | ||
3203 | struct got_object_id blob_id, commit_id, staged_blob_id; | ||
3204 | struct got_object_id *blob_idp = NULL((void*)0), *commit_idp = NULL((void*)0); | ||
3205 | struct got_object_id *staged_blob_idp = NULL((void*)0); | ||
3206 | |||
3207 | err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo); | ||
3208 | if (err) | ||
3209 | return err; | ||
3210 | |||
3211 | if (status == GOT_STATUS_NO_CHANGE' ' && | ||
3212 | staged_status == GOT_STATUS_NO_CHANGE' ' && !report_unchanged) | ||
3213 | return NULL((void*)0); | ||
3214 | |||
3215 | if (got_fileindex_entry_has_blob(ie)) { | ||
3216 | memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH20); | ||
3217 | blob_idp = &blob_id; | ||
3218 | } | ||
3219 | if (got_fileindex_entry_has_commit(ie)) { | ||
3220 | memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH20); | ||
3221 | commit_idp = &commit_id; | ||
3222 | } | ||
3223 | if (staged_status == GOT_STATUS_ADD'A' || | ||
3224 | staged_status == GOT_STATUS_MODIFY'M') { | ||
3225 | memcpy(staged_blob_id.sha1, ie->staged_blob_sha1, | ||
3226 | SHA1_DIGEST_LENGTH20); | ||
3227 | staged_blob_idp = &staged_blob_id; | ||
3228 | } | ||
3229 | |||
3230 | return (*status_cb)(status_arg, status, staged_status, | ||
3231 | ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name); | ||
3232 | } | ||
3233 | |||
3234 | static const struct got_error * | ||
3235 | status_old_new(void *arg, struct got_fileindex_entry *ie, | ||
3236 | struct dirent *de, const char *parent_path, int dirfd) | ||
3237 | { | ||
3238 | const struct got_error *err = NULL((void*)0); | ||
3239 | struct diff_dir_cb_arg *a = arg; | ||
3240 | char *abspath; | ||
3241 | |||
3242 | if (a->cancel_cb && a->cancel_cb(a->cancel_arg)) | ||
3243 | return got_error(GOT_ERR_CANCELLED49); | ||
3244 | |||
3245 | if (got_path_cmp(parent_path, a->status_path, | ||
3246 | strlen(parent_path), a->status_path_len) != 0 && | ||
3247 | !got_path_is_child(parent_path, a->status_path, a->status_path_len)) | ||
3248 | return NULL((void*)0); | ||
3249 | |||
3250 | if (parent_path[0]) { | ||
3251 | if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path, | ||
3252 | parent_path, de->d_name) == -1) | ||
3253 | return got_error_from_errno("asprintf"); | ||
3254 | } else { | ||
3255 | if (asprintf(&abspath, "%s/%s", a->worktree->root_path, | ||
3256 | de->d_name) == -1) | ||
3257 | return got_error_from_errno("asprintf"); | ||
3258 | } | ||
3259 | |||
3260 | err = report_file_status(ie, abspath, dirfd, de->d_name, | ||
3261 | a->status_cb, a->status_arg, a->repo, a->report_unchanged); | ||
3262 | free(abspath); | ||
3263 | return err; | ||
3264 | } | ||
3265 | |||
3266 | static const struct got_error * | ||
3267 | status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path) | ||
3268 | { | ||
3269 | struct diff_dir_cb_arg *a = arg; | ||
3270 | struct got_object_id blob_id, commit_id; | ||
3271 | unsigned char status; | ||
3272 | |||
3273 | if (a->cancel_cb && a->cancel_cb(a->cancel_arg)) | ||
3274 | return got_error(GOT_ERR_CANCELLED49); | ||
3275 | |||
3276 | if (!got_path_is_child(ie->path, a->status_path, a->status_path_len)) | ||
3277 | return NULL((void*)0); | ||
3278 | |||
3279 | memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH20); | ||
3280 | memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH20); | ||
3281 | if (got_fileindex_entry_has_file_on_disk(ie)) | ||
3282 | status = GOT_STATUS_MISSING'!'; | ||
3283 | else | ||
3284 | status = GOT_STATUS_DELETE'D'; | ||
3285 | return (*a->status_cb)(a->status_arg, status, get_staged_status(ie), | ||
3286 | ie->path, &blob_id, NULL((void*)0), &commit_id, -1, NULL((void*)0)); | ||
3287 | } | ||
3288 | |||
3289 | void | ||
3290 | free_ignorelist(struct got_pathlist_head *ignorelist) | ||
3291 | { | ||
3292 | struct got_pathlist_entry *pe; | ||
3293 | |||
3294 | TAILQ_FOREACH(pe, ignorelist, entry)for((pe) = ((ignorelist)->tqh_first); (pe) != ((void*)0); ( pe) = ((pe)->entry.tqe_next)) | ||
3295 | free((char *)pe->path); | ||
3296 | got_pathlist_free(ignorelist); | ||
3297 | } | ||
3298 | |||
3299 | void | ||
3300 | free_ignores(struct got_pathlist_head *ignores) | ||
3301 | { | ||
3302 | struct got_pathlist_entry *pe; | ||
3303 | |||
3304 | TAILQ_FOREACH(pe, ignores, entry)for((pe) = ((ignores)->tqh_first); (pe) != ((void*)0); (pe ) = ((pe)->entry.tqe_next)) { | ||
3305 | struct got_pathlist_head *ignorelist = pe->data; | ||
3306 | free_ignorelist(ignorelist); | ||
3307 | free((char *)pe->path); | ||
3308 | } | ||
3309 | got_pathlist_free(ignores); | ||
3310 | } | ||
3311 | |||
3312 | static const struct got_error * | ||
3313 | read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f) | ||
3314 | { | ||
3315 | const struct got_error *err = NULL((void*)0); | ||
3316 | struct got_pathlist_entry *pe = NULL((void*)0); | ||
3317 | struct got_pathlist_head *ignorelist; | ||
3318 | char *line = NULL((void*)0), *pattern, *dirpath = NULL((void*)0); | ||
3319 | size_t linesize = 0; | ||
3320 | ssize_t linelen; | ||
3321 | |||
3322 | ignorelist = calloc(1, sizeof(*ignorelist)); | ||
3323 | if (ignorelist == NULL((void*)0)) | ||
3324 | return got_error_from_errno("calloc"); | ||
3325 | TAILQ_INIT(ignorelist)do { (ignorelist)->tqh_first = ((void*)0); (ignorelist)-> tqh_last = &(ignorelist)->tqh_first; } while (0); | ||
3326 | |||
3327 | while ((linelen = getline(&line, &linesize, f)) != -1) { | ||
3328 | if (linelen > 0 && line[linelen - 1] == '\n') | ||
3329 | line[linelen - 1] = '\0'; | ||
3330 | |||
3331 | /* Git's ignores may contain comments. */ | ||
3332 | if (line[0] == '#') | ||
3333 | continue; | ||
3334 | |||
3335 | /* Git's negated patterns are not (yet?) supported. */ | ||
3336 | if (line[0] == '!') | ||
3337 | continue; | ||
3338 | |||
3339 | if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "", | ||
3340 | line) == -1) { | ||
3341 | err = got_error_from_errno("asprintf"); | ||
3342 | goto done; | ||
3343 | } | ||
3344 | err = got_pathlist_insert(NULL((void*)0), ignorelist, pattern, NULL((void*)0)); | ||
3345 | if (err) | ||
3346 | goto done; | ||
3347 | } | ||
3348 | if (ferror(f)(!__isthreaded ? (((f)->_flags & 0x0040) != 0) : (ferror )(f))) { | ||
3349 | err = got_error_from_errno("getline"); | ||
3350 | goto done; | ||
3351 | } | ||
3352 | |||
3353 | dirpath = strdup(path); | ||
3354 | if (dirpath == NULL((void*)0)) { | ||
3355 | err = got_error_from_errno("strdup"); | ||
3356 | goto done; | ||
3357 | } | ||
3358 | err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist); | ||
3359 | done: | ||
3360 | free(line); | ||
3361 | if (err || pe == NULL((void*)0)) { | ||
3362 | free(dirpath); | ||
3363 | free_ignorelist(ignorelist); | ||
3364 | } | ||
3365 | return err; | ||
3366 | } | ||
3367 | |||
3368 | int | ||
3369 | match_ignores(struct got_pathlist_head *ignores, const char *path) | ||
3370 | { | ||
3371 | struct got_pathlist_entry *pe; | ||
3372 | |||
3373 | /* Handle patterns which match in all directories. */ | ||
3374 | TAILQ_FOREACH(pe, ignores, entry)for((pe) = ((ignores)->tqh_first); (pe) != ((void*)0); (pe ) = ((pe)->entry.tqe_next)) { | ||
3375 | struct got_pathlist_head *ignorelist = pe->data; | ||
3376 | struct got_pathlist_entry *pi; | ||
3377 | |||
3378 | TAILQ_FOREACH(pi, ignorelist, entry)for((pi) = ((ignorelist)->tqh_first); (pi) != ((void*)0); ( pi) = ((pi)->entry.tqe_next)) { | ||
3379 | const char *p, *pattern = pi->path; | ||
3380 | |||
3381 | if (strncmp(pattern, "**/", 3) != 0) | ||
3382 | continue; | ||
3383 | pattern += 3; | ||
3384 | p = path; | ||
3385 | while (*p) { | ||
3386 | if (fnmatch(pattern, p, | ||
3387 | FNM_PATHNAME0x02 | FNM_LEADING_DIR0x08)) { | ||
3388 | /* Retry in next directory. */ | ||
3389 | while (*p && *p != '/') | ||
3390 | p++; | ||
3391 | while (*p == '/') | ||
3392 | p++; | ||
3393 | continue; | ||
3394 | } | ||
3395 | return 1; | ||
3396 | } | ||
3397 | } | ||
3398 | } | ||
3399 | |||
3400 | /* | ||
3401 | * The ignores pathlist contains ignore lists from children before | ||
3402 | * parents, so we can find the most specific ignorelist by walking | ||
3403 | * ignores backwards. | ||
3404 | */ | ||
3405 | pe = TAILQ_LAST(ignores, got_pathlist_head)(*(((struct got_pathlist_head *)((ignores)->tqh_last))-> tqh_last)); | ||
3406 | while (pe) { | ||
3407 | if (got_path_is_child(path, pe->path, pe->path_len)) { | ||
3408 | struct got_pathlist_head *ignorelist = pe->data; | ||
3409 | struct got_pathlist_entry *pi; | ||
3410 | TAILQ_FOREACH(pi, ignorelist, entry)for((pi) = ((ignorelist)->tqh_first); (pi) != ((void*)0); ( pi) = ((pi)->entry.tqe_next)) { | ||
3411 | const char *pattern = pi->path; | ||
3412 | int flags = FNM_LEADING_DIR0x08; | ||
3413 | if (strstr(pattern, "/**/") == NULL((void*)0)) | ||
3414 | flags |= FNM_PATHNAME0x02; | ||
3415 | if (fnmatch(pattern, path, flags)) | ||
3416 | continue; | ||
3417 | return 1; | ||
3418 | } | ||
3419 | } | ||
3420 | pe = TAILQ_PREV(pe, got_pathlist_head, entry)(*(((struct got_pathlist_head *)((pe)->entry.tqe_prev))-> tqh_last)); | ||
3421 | } | ||
3422 | |||
3423 | return 0; | ||
3424 | } | ||
3425 | |||
3426 | static const struct got_error * | ||
3427 | add_ignores(struct got_pathlist_head *ignores, const char *root_path, | ||
3428 | const char *path, int dirfd, const char *ignores_filename) | ||
3429 | { | ||
3430 | const struct got_error *err = NULL((void*)0); | ||
3431 | char *ignorespath; | ||
3432 | int fd = -1; | ||
3433 | FILE *ignoresfile = NULL((void*)0); | ||
3434 | |||
3435 | if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path, | ||
3436 | path[0] ? "/" : "", ignores_filename) == -1) | ||
3437 | return got_error_from_errno("asprintf"); | ||
3438 | |||
3439 | if (dirfd != -1) { | ||
3440 | fd = openat(dirfd, ignores_filename, O_RDONLY0x0000 | O_NOFOLLOW0x0100); | ||
3441 | if (fd == -1) { | ||
3442 | if (errno(*__errno()) != ENOENT2 && errno(*__errno()) != EACCES13) | ||
3443 | err = got_error_from_errno2("openat", | ||
3444 | ignorespath); | ||
3445 | } else { | ||
3446 | ignoresfile = fdopen(fd, "r"); | ||
3447 | if (ignoresfile == NULL((void*)0)) | ||
3448 | err = got_error_from_errno2("fdopen", | ||
3449 | ignorespath); | ||
3450 | else { | ||
3451 | fd = -1; | ||
3452 | err = read_ignores(ignores, path, ignoresfile); | ||
3453 | } | ||
3454 | } | ||
3455 | } else { | ||
3456 | ignoresfile = fopen(ignorespath, "r"); | ||
3457 | if (ignoresfile == NULL((void*)0)) { | ||
3458 | if (errno(*__errno()) != ENOENT2 && errno(*__errno()) != EACCES13) | ||
3459 | err = got_error_from_errno2("fopen", | ||
3460 | ignorespath); | ||
3461 | } else | ||
3462 | err = read_ignores(ignores, path, ignoresfile); | ||
3463 | } | ||
3464 | |||
3465 | if (ignoresfile && fclose(ignoresfile) == EOF(-1) && err == NULL((void*)0)) | ||
3466 | err = got_error_from_errno2("fclose", path); | ||
3467 | if (fd != -1 && close(fd) == -1 && err == NULL((void*)0)) | ||
3468 | err = got_error_from_errno2("close", path); | ||
3469 | free(ignorespath); | ||
3470 | return err; | ||
3471 | } | ||
3472 | |||
3473 | static const struct got_error * | ||
3474 | status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd) | ||
3475 | { | ||
3476 | const struct got_error *err = NULL((void*)0); | ||
3477 | struct diff_dir_cb_arg *a = arg; | ||
3478 | char *path = NULL((void*)0); | ||
3479 | |||
3480 | if (a->cancel_cb && a->cancel_cb(a->cancel_arg)) | ||
3481 | return got_error(GOT_ERR_CANCELLED49); | ||
3482 | |||
3483 | if (parent_path[0]) { | ||
3484 | if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1) | ||
3485 | return got_error_from_errno("asprintf"); | ||
3486 | } else { | ||
3487 | path = de->d_name; | ||
3488 | } | ||
3489 | |||
3490 | if (de->d_type != DT_DIR4 && | ||
3491 | got_path_is_child(path, a->status_path, a->status_path_len) | ||
3492 | && !match_ignores(&a->ignores, path)) | ||
3493 | err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED'?', | ||
3494 | GOT_STATUS_NO_CHANGE' ', path, NULL((void*)0), NULL((void*)0), NULL((void*)0), -1, NULL((void*)0)); | ||
3495 | if (parent_path[0]) | ||
3496 | free(path); | ||
3497 | return err; | ||
3498 | } | ||
3499 | |||
3500 | static const struct got_error * | ||
3501 | status_traverse(void *arg, const char *path, int dirfd) | ||
3502 | { | ||
3503 | const struct got_error *err = NULL((void*)0); | ||
3504 | struct diff_dir_cb_arg *a = arg; | ||
3505 | |||
3506 | if (a->no_ignores) | ||
3507 | return NULL((void*)0); | ||
3508 | |||
3509 | err = add_ignores(&a->ignores, a->worktree->root_path, | ||
3510 | path, dirfd, ".cvsignore"); | ||
3511 | if (err) | ||
3512 | return err; | ||
3513 | |||
3514 | err = add_ignores(&a->ignores, a->worktree->root_path, path, | ||
3515 | dirfd, ".gitignore"); | ||
3516 | |||
3517 | return err; | ||
3518 | } | ||
3519 | |||
3520 | static const struct got_error * | ||
3521 | report_single_file_status(const char *path, const char *ondisk_path, | ||
3522 | struct got_fileindex *fileindex, got_worktree_status_cb status_cb, | ||
3523 | void *status_arg, struct got_repository *repo, int report_unchanged) | ||
3524 | { | ||
3525 | struct got_fileindex_entry *ie; | ||
3526 | struct stat sb; | ||
3527 | |||
3528 | ie = got_fileindex_entry_get(fileindex, path, strlen(path)); | ||
3529 | if (ie) | ||
3530 | return report_file_status(ie, ondisk_path, -1, NULL((void*)0), | ||
3531 | status_cb, status_arg, repo, report_unchanged); | ||
3532 | |||
3533 | if (lstat(ondisk_path, &sb) == -1) { | ||
3534 | if (errno(*__errno()) != ENOENT2) | ||
3535 | return got_error_from_errno2("lstat", ondisk_path); | ||
3536 | return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT'N', | ||
3537 | GOT_STATUS_NO_CHANGE' ', path, NULL((void*)0), NULL((void*)0), NULL((void*)0), -1, NULL((void*)0)); | ||
3538 | return NULL((void*)0); | ||
3539 | } | ||
3540 | |||
3541 | if (S_ISREG(sb.st_mode)((sb.st_mode & 0170000) == 0100000) || S_ISLNK(sb.st_mode)((sb.st_mode & 0170000) == 0120000)) | ||
3542 | return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED'?', | ||
3543 | GOT_STATUS_NO_CHANGE' ', path, NULL((void*)0), NULL((void*)0), NULL((void*)0), -1, NULL((void*)0)); | ||
3544 | |||
3545 | return NULL((void*)0); | ||
3546 | } | ||
3547 | |||
3548 | static const struct got_error * | ||
3549 | add_ignores_from_parent_paths(struct got_pathlist_head *ignores, | ||
3550 | const char *root_path, const char *path) | ||
3551 | { | ||
3552 | const struct got_error *err; | ||
3553 | char *parent_path, *next_parent_path = NULL((void*)0); | ||
3554 | |||
3555 | err = add_ignores(ignores, root_path, "", -1, | ||
3556 | ".cvsignore"); | ||
3557 | if (err) | ||
3558 | return err; | ||
3559 | |||
3560 | err = add_ignores(ignores, root_path, "", -1, | ||
3561 | ".gitignore"); | ||
3562 | if (err) | ||
3563 | return err; | ||
3564 | |||
3565 | err = got_path_dirname(&parent_path, path); | ||
3566 | if (err) { | ||
3567 | if (err->code == GOT_ERR_BAD_PATH4) | ||
3568 | return NULL((void*)0); /* cannot traverse parent */ | ||
3569 | return err; | ||
3570 | } | ||
3571 | for (;;) { | ||
3572 | err = add_ignores(ignores, root_path, parent_path, -1, | ||
3573 | ".cvsignore"); | ||
3574 | if (err) | ||
3575 | break; | ||
3576 | err = add_ignores(ignores, root_path, parent_path, -1, | ||
3577 | ".gitignore"); | ||
3578 | if (err) | ||
3579 | break; | ||
3580 | err = got_path_dirname(&next_parent_path, parent_path); | ||
3581 | if (err) { | ||
3582 | if (err->code == GOT_ERR_BAD_PATH4) | ||
3583 | err = NULL((void*)0); /* traversed everything */ | ||
3584 | break; | ||
3585 | } | ||
3586 | free(parent_path); | ||
3587 | parent_path = next_parent_path; | ||
3588 | next_parent_path = NULL((void*)0); | ||
3589 | } | ||
3590 | |||
3591 | free(parent_path); | ||
3592 | free(next_parent_path); | ||
3593 | return err; | ||
3594 | } | ||
3595 | |||
3596 | static const struct got_error * | ||
3597 | worktree_status(struct got_worktree *worktree, const char *path, | ||
3598 | struct got_fileindex *fileindex, struct got_repository *repo, | ||
3599 | got_worktree_status_cb status_cb, void *status_arg, | ||
3600 | got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores, | ||
3601 | int report_unchanged) | ||
3602 | { | ||
3603 | const struct got_error *err = NULL((void*)0); | ||
3604 | int fd = -1; | ||
3605 | struct got_fileindex_diff_dir_cb fdiff_cb; | ||
3606 | struct diff_dir_cb_arg arg; | ||
3607 | char *ondisk_path = NULL((void*)0); | ||
3608 | |||
3609 | TAILQ_INIT(&arg.ignores)do { (&arg.ignores)->tqh_first = ((void*)0); (&arg .ignores)->tqh_last = &(&arg.ignores)->tqh_first ; } while (0); | ||
3610 | |||
3611 | if (asprintf(&ondisk_path, "%s%s%s", | ||
3612 | worktree->root_path, path[0] ? "/" : "", path) == -1) | ||
3613 | return got_error_from_errno("asprintf"); | ||
3614 | |||
3615 | fd = open(ondisk_path, O_RDONLY0x0000 | O_NOFOLLOW0x0100 | O_DIRECTORY0x20000); | ||
3616 | if (fd == -1) { | ||
3617 | if (errno(*__errno()) != ENOTDIR20 && errno(*__errno()) != ENOENT2 && errno(*__errno()) != EACCES13 && | ||
3618 | errno(*__errno()) != ELOOP62) | ||
3619 | err = got_error_from_errno2("open", ondisk_path); | ||
3620 | else | ||
3621 | err = report_single_file_status(path, ondisk_path, | ||
3622 | fileindex, status_cb, status_arg, repo, | ||
3623 | report_unchanged); | ||
3624 | } else { | ||
3625 | fdiff_cb.diff_old_new = status_old_new; | ||
3626 | fdiff_cb.diff_old = status_old; | ||
3627 | fdiff_cb.diff_new = status_new; | ||
3628 | fdiff_cb.diff_traverse = status_traverse; | ||
3629 | arg.fileindex = fileindex; | ||
3630 | arg.worktree = worktree; | ||
3631 | arg.status_path = path; | ||
3632 | arg.status_path_len = strlen(path); | ||
3633 | arg.repo = repo; | ||
3634 | arg.status_cb = status_cb; | ||
3635 | arg.status_arg = status_arg; | ||
3636 | arg.cancel_cb = cancel_cb; | ||
3637 | arg.cancel_arg = cancel_arg; | ||
3638 | arg.report_unchanged = report_unchanged; | ||
3639 | arg.no_ignores = no_ignores; | ||
3640 | if (!no_ignores) { | ||
3641 | err = add_ignores_from_parent_paths(&arg.ignores, | ||
3642 | worktree->root_path, path); | ||
3643 | if (err) | ||
3644 | goto done; | ||
3645 | } | ||
3646 | err = got_fileindex_diff_dir(fileindex, fd, | ||
3647 | worktree->root_path, path, repo, &fdiff_cb, &arg); | ||
3648 | } | ||
3649 | done: | ||
3650 | free_ignores(&arg.ignores); | ||
3651 | if (fd != -1 && close(fd) == -1 && err == NULL((void*)0)) | ||
3652 | err = got_error_from_errno("close"); | ||
3653 | free(ondisk_path); | ||
3654 | return err; | ||
3655 | } | ||
3656 | |||
3657 | const struct got_error * | ||
3658 | got_worktree_status(struct got_worktree *worktree, | ||
3659 | struct got_pathlist_head *paths, struct got_repository *repo, | ||
3660 | got_worktree_status_cb status_cb, void *status_arg, | ||
3661 | got_cancel_cb cancel_cb, void *cancel_arg) | ||
3662 | { | ||
3663 | const struct got_error *err = NULL((void*)0); | ||
3664 | char *fileindex_path = NULL((void*)0); | ||
3665 | struct got_fileindex *fileindex = NULL((void*)0); | ||
3666 | struct got_pathlist_entry *pe; | ||
3667 | |||
3668 | err = open_fileindex(&fileindex, &fileindex_path, worktree); | ||
3669 | if (err) | ||
3670 | return err; | ||
3671 | |||
3672 | TAILQ_FOREACH(pe, paths, entry)for((pe) = ((paths)->tqh_first); (pe) != ((void*)0); (pe) = ((pe)->entry.tqe_next)) { | ||
3673 | err = worktree_status(worktree, pe->path, fileindex, repo, | ||
3674 | status_cb, status_arg, cancel_cb, cancel_arg, 0, 0); | ||
3675 | if (err) | ||
3676 | break; | ||
3677 | } | ||
3678 | free(fileindex_path); | ||
3679 | got_fileindex_free(fileindex); | ||
3680 | return err; | ||
3681 | } | ||
3682 | |||
3683 | const struct got_error * | ||
3684 | got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree, | ||
3685 | const char *arg) | ||
3686 | { | ||
3687 | const struct got_error *err = NULL((void*)0); | ||
3688 | char *resolved = NULL((void*)0), *cwd = NULL((void*)0), *path = NULL((void*)0); | ||
3689 | size_t len; | ||
3690 | struct stat sb; | ||
3691 | char *abspath = NULL((void*)0); | ||
3692 | char canonpath[PATH_MAX1024]; | ||
3693 | |||
3694 | *wt_path = NULL((void*)0); | ||
3695 | |||
3696 | cwd = getcwd(NULL((void*)0), 0); | ||
3697 | if (cwd == NULL((void*)0)) | ||
3698 | return got_error_from_errno("getcwd"); | ||
3699 | |||
3700 | if (lstat(arg, &sb) == -1) { | ||
3701 | if (errno(*__errno()) != ENOENT2) { | ||
3702 | err = got_error_from_errno2("lstat", arg); | ||
3703 | goto done; | ||
3704 | } | ||
3705 | sb.st_mode = 0; | ||
3706 | } | ||
3707 | if (S_ISLNK(sb.st_mode)((sb.st_mode & 0170000) == 0120000)) { | ||
3708 | /* | ||
3709 | * We cannot use realpath(3) with symlinks since we want to | ||
3710 | * operate on the symlink itself. | ||
3711 | * But we can make the path absolute, assuming it is relative | ||
3712 | * to the current working directory, and then canonicalize it. | ||
3713 | */ | ||
3714 | if (!got_path_is_absolute(arg)) { | ||
3715 | if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) { | ||
3716 | err = got_error_from_errno("asprintf"); | ||
3717 | goto done; | ||
3718 | } | ||
3719 | |||
3720 | } | ||
3721 | err = got_canonpath(abspath ? abspath : arg, canonpath, | ||
3722 | sizeof(canonpath)); | ||
3723 | if (err) | ||
3724 | goto done; | ||
3725 | resolved = strdup(canonpath); | ||
3726 | if (resolved == NULL((void*)0)) { | ||
3727 | err = got_error_from_errno("strdup"); | ||
3728 | goto done; | ||
3729 | } | ||
3730 | } else { | ||
3731 | resolved = realpath(arg, NULL((void*)0)); | ||
3732 | if (resolved == NULL((void*)0)) { | ||
3733 | if (errno(*__errno()) != ENOENT2) { | ||
3734 | err = got_error_from_errno2("realpath", arg); | ||
3735 | goto done; | ||
3736 | } | ||
3737 | if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) { | ||
3738 | err = got_error_from_errno("asprintf"); | ||
3739 | goto done; | ||
3740 | } | ||
3741 | err = got_canonpath(abspath, canonpath, | ||
3742 | sizeof(canonpath)); | ||
3743 | if (err) | ||
3744 | goto done; | ||
3745 | resolved = strdup(canonpath); | ||
3746 | if (resolved == NULL((void*)0)) { | ||
3747 | err = got_error_from_errno("strdup"); | ||
3748 | goto done; | ||
3749 | } | ||
3750 | } | ||
3751 | } | ||
3752 | |||
3753 | if (strncmp(got_worktree_get_root_path(worktree), resolved, | ||
3754 | strlen(got_worktree_get_root_path(worktree)))) { | ||
3755 | err = got_error_path(resolved, GOT_ERR_BAD_PATH4); | ||
3756 | goto done; | ||
3757 | } | ||
3758 | |||
3759 | if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) { | ||
3760 | err = got_path_skip_common_ancestor(&path, | ||
3761 | got_worktree_get_root_path(worktree), resolved); | ||
3762 | if (err) | ||
3763 | goto done; | ||
3764 | } else { | ||
3765 | path = strdup(""); | ||
3766 | if (path == NULL((void*)0)) { | ||
3767 | err = got_error_from_errno("strdup"); | ||
3768 | goto done; | ||
3769 | } | ||
3770 | } | ||
3771 | |||
3772 | /* XXX status walk can't deal with trailing slash! */ | ||
3773 | len = strlen(path); | ||
3774 | while (len > 0 && path[len - 1] == '/') { | ||
3775 | path[len - 1] = '\0'; | ||
3776 | len--; | ||
3777 | } | ||
3778 | done: | ||
3779 | free(abspath); | ||
3780 | free(resolved); | ||
3781 | free(cwd); | ||
3782 | if (err == NULL((void*)0)) | ||
3783 | *wt_path = path; | ||
3784 | else | ||
3785 | free(path); | ||
3786 | return err; | ||
3787 | } | ||
3788 | |||
3789 | struct schedule_addition_args { | ||
3790 | struct got_worktree *worktree; | ||
3791 | struct got_fileindex *fileindex; | ||
3792 | got_worktree_checkout_cb progress_cb; | ||
3793 | void *progress_arg; | ||
3794 | struct got_repository *repo; | ||
3795 | }; | ||
3796 | |||
3797 | static const struct got_error * | ||
3798 | schedule_addition(void *arg, unsigned char status, unsigned char staged_status, | ||
3799 | const char *relpath, struct got_object_id *blob_id, | ||
3800 | struct got_object_id *staged_blob_id, struct got_object_id *commit_id, | ||
3801 | int dirfd, const char *de_name) | ||
3802 | { | ||
3803 | struct schedule_addition_args *a = arg; | ||
3804 | const struct got_error *err = NULL((void*)0); | ||
3805 | struct got_fileindex_entry *ie; | ||
3806 | struct stat sb; | ||
3807 | char *ondisk_path; | ||
3808 | |||
3809 | if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, | ||
3810 | relpath) == -1) | ||
3811 | return got_error_from_errno("asprintf"); | ||
3812 | |||
3813 | ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath)); | ||
3814 | if (ie) { | ||
3815 | err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, | ||
3816 | de_name, a->repo); | ||
3817 | if (err) | ||
3818 | goto done; | ||
3819 | /* Re-adding an existing entry is a no-op. */ | ||
3820 | if (status == GOT_STATUS_ADD'A') | ||
3821 | goto done; | ||
3822 | err = got_error_path(relpath, GOT_ERR_FILE_STATUS68); | ||
3823 | if (err) | ||
3824 | goto done; | ||
3825 | } | ||
3826 | |||
3827 | if (status != GOT_STATUS_UNVERSIONED'?') { | ||
3828 | err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS68); | ||
3829 | goto done; | ||
3830 | } | ||
3831 | |||
3832 | err = got_fileindex_entry_alloc(&ie, relpath); | ||
3833 | if (err) | ||
3834 | goto done; | ||
3835 | err = got_fileindex_entry_update(ie, a->worktree->root_fd, | ||
3836 | relpath, NULL((void*)0), NULL((void*)0), 1); | ||
3837 | if (err) { | ||
3838 | got_fileindex_entry_free(ie); | ||
3839 | goto done; | ||
3840 | } | ||
3841 | err = got_fileindex_entry_add(a->fileindex, ie); | ||
3842 | if (err) { | ||
3843 | got_fileindex_entry_free(ie); | ||
3844 | goto done; | ||
3845 | } | ||
3846 | done: | ||
3847 | free(ondisk_path); | ||
3848 | if (err) | ||
3849 | return err; | ||
3850 | if (status == GOT_STATUS_ADD'A') | ||
3851 | return NULL((void*)0); | ||
3852 | return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD'A', relpath); | ||
3853 | } | ||
3854 | |||
3855 | const struct got_error * | ||
3856 | got_worktree_schedule_add(struct got_worktree *worktree, | ||
3857 | struct got_pathlist_head *paths, | ||
3858 | got_worktree_checkout_cb progress_cb, void *progress_arg, | ||
3859 | struct got_repository *repo, int no_ignores) | ||
3860 | { | ||
3861 | struct got_fileindex *fileindex = NULL((void*)0); | ||
3862 | char *fileindex_path = NULL((void*)0); | ||
3863 | const struct got_error *err = NULL((void*)0), *sync_err, *unlockerr; | ||
3864 | struct got_pathlist_entry *pe; | ||
3865 | struct schedule_addition_args saa; | ||
3866 | |||
3867 | err = lock_worktree(worktree, LOCK_EX0x02); | ||
3868 | if (err) | ||
3869 | return err; | ||
3870 | |||
3871 | err = open_fileindex(&fileindex, &fileindex_path, worktree); | ||
3872 | if (err) | ||
3873 | goto done; | ||
3874 | |||
3875 | saa.worktree = worktree; | ||
3876 | saa.fileindex = fileindex; | ||
3877 | saa.progress_cb = progress_cb; | ||
3878 | saa.progress_arg = progress_arg; | ||
3879 | saa.repo = repo; | ||
3880 | |||
3881 | TAILQ_FOREACH(pe, paths, entry)for((pe) = ((paths)->tqh_first); (pe) != ((void*)0); (pe) = ((pe)->entry.tqe_next)) { | ||
3882 | err = worktree_status(worktree, pe->path, fileindex, repo, | ||
3883 | schedule_addition, &saa, NULL((void*)0), NULL((void*)0), no_ignores, 0); | ||
3884 | if (err) | ||
3885 | break; | ||
3886 | } | ||
3887 | sync_err = sync_fileindex(fileindex, fileindex_path); | ||
3888 | if (sync_err && err == NULL((void*)0)) | ||
3889 | err = sync_err; | ||
3890 | done: | ||
3891 | free(fileindex_path); | ||
3892 | if (fileindex) | ||
3893 | got_fileindex_free(fileindex); | ||
3894 | unlockerr = lock_worktree(worktree, LOCK_SH0x01); | ||
3895 | if (unlockerr && err == NULL((void*)0)) | ||
3896 | err = unlockerr; | ||
3897 | return err; | ||
3898 | } | ||
3899 | |||
3900 | struct schedule_deletion_args { | ||
3901 | struct got_worktree *worktree; | ||
3902 | struct got_fileindex *fileindex; | ||
3903 | got_worktree_delete_cb progress_cb; | ||
3904 | void *progress_arg; | ||
3905 | struct got_repository *repo; | ||
3906 | int delete_local_mods; | ||
3907 | int keep_on_disk; | ||
3908 | const char *status_codes; | ||
3909 | }; | ||
3910 | |||
3911 | static const struct got_error * | ||
3912 | schedule_for_deletion(void *arg, unsigned char status, | ||
3913 | unsigned char staged_status, const char *relpath, | ||
3914 | struct got_object_id *blob_id, struct got_object_id *staged_blob_id, | ||
3915 | struct got_object_id *commit_id, int dirfd, const char *de_name) | ||
3916 | { | ||
3917 | struct schedule_deletion_args *a = arg; | ||
3918 | const struct got_error *err = NULL((void*)0); | ||
3919 | struct got_fileindex_entry *ie = NULL((void*)0); | ||
3920 | struct stat sb; | ||
3921 | char *ondisk_path; | ||
3922 | |||
3923 | ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath)); | ||
3924 | if (ie == NULL((void*)0)) | ||
3925 | return got_error_path(relpath, GOT_ERR_BAD_PATH4); | ||
3926 | |||
3927 | staged_status = get_staged_status(ie); | ||
3928 | if (staged_status != GOT_STATUS_NO_CHANGE' ') { | ||
3929 | if (staged_status == GOT_STATUS_DELETE'D') | ||
3930 | return NULL((void*)0); | ||
3931 | return got_error_path(relpath, GOT_ERR_FILE_STAGED101); | ||
3932 | } | ||
3933 | |||
3934 | if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, | ||
3935 | relpath) == -1) | ||
3936 | return got_error_from_errno("asprintf"); | ||
3937 | |||
3938 | err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name, | ||
3939 | a->repo); | ||
3940 | if (err) | ||
3941 | goto done; | ||
3942 | |||
3943 | if (a->status_codes) { | ||
3944 | size_t ncodes = strlen(a->status_codes); | ||
3945 | int i; | ||
3946 | for (i = 0; i < ncodes ; i++) { | ||
3947 | if (status == a->status_codes[i]) | ||
3948 | break; | ||
3949 | } | ||
3950 | if (i == ncodes) { | ||
3951 | /* Do not delete files in non-matching status. */ | ||
3952 | free(ondisk_path); | ||
3953 | return NULL((void*)0); | ||
3954 | } | ||
3955 | if (a->status_codes[i] != GOT_STATUS_MODIFY'M' && | ||
3956 | a->status_codes[i] != GOT_STATUS_MISSING'!') { | ||
3957 | static char msg[64]; | ||
3958 | snprintf(msg, sizeof(msg), | ||
3959 | "invalid status code '%c'", a->status_codes[i]); | ||
3960 | err = got_error_msg(GOT_ERR_FILE_STATUS68, msg); | ||
3961 | goto done; | ||
3962 | } | ||
3963 | } | ||
3964 | |||
3965 | if (status != GOT_STATUS_NO_CHANGE' ') { | ||
3966 | if (status == GOT_STATUS_DELETE'D') | ||
3967 | goto done; | ||
3968 | if (status == GOT_STATUS_MODIFY'M' && !a->delete_local_mods) { | ||
3969 | err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED67); | ||
3970 | goto done; | ||
3971 | } | ||
3972 | if (status != GOT_STATUS_MODIFY'M' && | ||
3973 | status != GOT_STATUS_MISSING'!') { | ||
3974 | err = got_error_path(relpath, GOT_ERR_FILE_STATUS68); | ||
3975 | goto done; | ||
3976 | } | ||
3977 | } | ||
3978 | |||
3979 | if (!a->keep_on_disk && status != GOT_STATUS_MISSING'!') { | ||
3980 | size_t root_len; | ||
3981 | |||
3982 | if (dirfd != -1) { | ||
3983 | if (unlinkat(dirfd, de_name, 0) != 0) { | ||
3984 | err = got_error_from_errno2("unlinkat", | ||
3985 | ondisk_path); | ||
3986 | goto done; | ||
3987 | } | ||
3988 | } else if (unlink(ondisk_path) != 0) { | ||
3989 | err = got_error_from_errno2("unlink", ondisk_path); | ||
3990 | goto done; | ||
3991 | } | ||
3992 | |||
3993 | root_len = strlen(a->worktree->root_path); | ||
3994 | do { | ||
3995 | char *parent; | ||
3996 | err = got_path_dirname(&parent, ondisk_path); | ||
3997 | if (err) | ||
3998 | goto done; | ||
3999 | free(ondisk_path); | ||
4000 | ondisk_path = parent; | ||
4001 | if (rmdir(ondisk_path) == -1) { | ||
4002 | if (errno(*__errno()) != ENOTEMPTY66) | ||
4003 | err = got_error_from_errno2("rmdir", | ||
4004 | ondisk_path); | ||
4005 | break; | ||
4006 | } | ||
4007 | } while (got_path_cmp(ondisk_path, a->worktree->root_path, | ||
4008 | strlen(ondisk_path), root_len) != 0); | ||
4009 | } | ||
4010 | |||
4011 | got_fileindex_entry_mark_deleted_from_disk(ie); | ||
4012 | done: | ||
4013 | free(ondisk_path); | ||
4014 | if (err) | ||
4015 | return err; | ||
4016 | if (status == GOT_STATUS_DELETE'D') | ||
4017 | return NULL((void*)0); | ||
4018 | return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE'D', | ||
4019 | staged_status, relpath); | ||
4020 | } | ||
4021 | |||
4022 | const struct got_error * | ||
4023 | got_worktree_schedule_delete(struct got_worktree *worktree, | ||
4024 | struct got_pathlist_head *paths, int delete_local_mods, | ||
4025 | const char *status_codes, | ||
4026 | got_worktree_delete_cb progress_cb, void *progress_arg, | ||
4027 | struct got_repository *repo, int keep_on_disk) | ||
4028 | { | ||
4029 | struct got_fileindex *fileindex = NULL((void*)0); | ||
4030 | char *fileindex_path = NULL((void*)0); | ||
4031 | const struct got_error *err = NULL((void*)0), *sync_err, *unlockerr; | ||
4032 | struct got_pathlist_entry *pe; | ||
4033 | struct schedule_deletion_args sda; | ||
4034 | |||
4035 | err = lock_worktree(worktree, LOCK_EX0x02); | ||
4036 | if (err) | ||
4037 | return err; | ||
4038 | |||
4039 | err = open_fileindex(&fileindex, &fileindex_path, worktree); | ||
4040 | if (err) | ||
4041 | goto done; | ||
4042 | |||
4043 | sda.worktree = worktree; | ||
4044 | sda.fileindex = fileindex; | ||
4045 | sda.progress_cb = progress_cb; | ||
4046 | sda.progress_arg = progress_arg; | ||
4047 | sda.repo = repo; | ||
4048 | sda.delete_local_mods = delete_local_mods; | ||
4049 | sda.keep_on_disk = keep_on_disk; | ||
4050 | sda.status_codes = status_codes; | ||
4051 | |||
4052 | TAILQ_FOREACH(pe, paths, entry)for((pe) = ((paths)->tqh_first); (pe) != ((void*)0); (pe) = ((pe)->entry.tqe_next)) { | ||
4053 | err = worktree_status(worktree, pe->path, fileindex, repo, | ||
4054 | schedule_for_deletion, &sda, NULL((void*)0), NULL((void*)0), 0, 1); | ||
4055 | if (err) | ||
4056 | break; | ||
4057 | } | ||
4058 | sync_err = sync_fileindex(fileindex, fileindex_path); | ||
4059 | if (sync_err && err == NULL((void*)0)) | ||
4060 | err = sync_err; | ||
4061 | done: | ||
4062 | free(fileindex_path); | ||
4063 | if (fileindex) | ||
4064 | got_fileindex_free(fileindex); | ||
4065 | unlockerr = lock_worktree(worktree, LOCK_SH0x01); | ||
4066 | if (unlockerr && err == NULL((void*)0)) | ||
4067 | err = unlockerr; | ||
4068 | return err; | ||
4069 | } | ||
4070 | |||
4071 | static const struct got_error * | ||
4072 | copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile) | ||
4073 | { | ||
4074 | const struct got_error *err = NULL((void*)0); | ||
4075 | char *line = NULL((void*)0); | ||
4076 | size_t linesize = 0, n; | ||
4077 | ssize_t linelen; | ||
4078 | |||
4079 | linelen = getline(&line, &linesize, infile); | ||
4080 | if (linelen == -1) { | ||
4081 | if (ferror(infile)(!__isthreaded ? (((infile)->_flags & 0x0040) != 0) : ( ferror)(infile))) { | ||
4082 | err = got_error_from_errno("getline"); | ||
4083 | goto done; | ||
4084 | } | ||
4085 | return NULL((void*)0); | ||
4086 | } | ||
4087 | if (outfile) { | ||
4088 | n = fwrite(line, 1, linelen, outfile); | ||
4089 | if (n != linelen) { | ||
4090 | err = got_ferror(outfile, GOT_ERR_IO6); | ||
4091 | goto done; | ||
4092 | } | ||
4093 | } | ||
4094 | if (rejectfile) { | ||
4095 | n = fwrite(line, 1, linelen, rejectfile); | ||
4096 | if (n != linelen) | ||
4097 | err = got_ferror(outfile, GOT_ERR_IO6); | ||
4098 | } | ||
4099 | done: | ||
4100 | free(line); | ||
4101 | return err; | ||
4102 | } | ||
4103 | |||
4104 | static const struct got_error * | ||
4105 | skip_one_line(FILE *f) | ||
4106 | { | ||
4107 | char *line = NULL((void*)0); | ||
4108 | size_t linesize = 0; | ||
4109 | ssize_t linelen; | ||
4110 | |||
4111 | linelen = getline(&line, &linesize, f); | ||
4112 | if (linelen == -1) { | ||
4113 | if (ferror(f)(!__isthreaded ? (((f)->_flags & 0x0040) != 0) : (ferror )(f))) | ||
4114 | return got_error_from_errno("getline"); | ||
4115 | return NULL((void*)0); | ||
4116 | } | ||
4117 | free(line); | ||
4118 | return NULL((void*)0); | ||
4119 | } | ||
4120 | |||
4121 | static const struct got_error * | ||
4122 | copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2, | ||
4123 | int start_old, int end_old, int start_new, int end_new, | ||
4124 | FILE *outfile, FILE *rejectfile) | ||
4125 | { | ||
4126 | const struct got_error *err; | ||
4127 | |||
4128 | /* Copy old file's lines leading up to patch. */ | ||
4129 | while (!feof(f1)(!__isthreaded ? (((f1)->_flags & 0x0020) != 0) : (feof )(f1)) && *line_cur1 < start_old) { | ||
4130 | err = copy_one_line(f1, outfile, NULL((void*)0)); | ||
4131 | if (err) | ||
4132 | return err; | ||
4133 | (*line_cur1)++; | ||
4134 | } | ||
4135 | /* Skip new file's lines leading up to patch. */ | ||
4136 | while (!feof(f2)(!__isthreaded ? (((f2)->_flags & 0x0020) != 0) : (feof )(f2)) && *line_cur2 < start_new) { | ||
4137 | if (rejectfile) | ||
4138 | err = copy_one_line(f2, NULL((void*)0), rejectfile); | ||
4139 | else | ||
4140 | err = skip_one_line(f2); | ||
4141 | if (err) | ||
4142 | return err; | ||
4143 | (*line_cur2)++; | ||
4144 | } | ||
4145 | /* Copy patched lines. */ | ||
4146 | while (!feof(f2)(!__isthreaded ? (((f2)->_flags & 0x0020) != 0) : (feof )(f2)) && *line_cur2 <= end_new) { | ||
4147 | err = copy_one_line(f2, outfile, NULL((void*)0)); | ||
4148 | if (err) | ||
4149 | return err; | ||
4150 | (*line_cur2)++; | ||
4151 | } | ||
4152 | /* Skip over old file's replaced lines. */ | ||
4153 | while (!feof(f1)(!__isthreaded ? (((f1)->_flags & 0x0020) != 0) : (feof )(f1)) && *line_cur1 <= end_old) { | ||
4154 | if (rejectfile) | ||
4155 | err = copy_one_line(f1, NULL((void*)0), rejectfile); | ||
4156 | else | ||
4157 | err = skip_one_line(f1); | ||
4158 | if (err) | ||
4159 | return err; | ||
4160 | (*line_cur1)++; | ||
4161 | } | ||
4162 | |||
4163 | return NULL((void*)0); | ||
4164 | } | ||
4165 | |||
4166 | static const struct got_error * | ||
4167 | copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2, | ||
4168 | FILE *outfile, FILE *rejectfile) | ||
4169 | { | ||
4170 | const struct got_error *err; | ||
4171 | |||
4172 | if (outfile) { | ||
4173 | /* Copy old file's lines until EOF. */ | ||
4174 | while (!feof(f1)(!__isthreaded ? (((f1)->_flags & 0x0020) != 0) : (feof )(f1))) { | ||
4175 | err = copy_one_line(f1, outfile, NULL((void*)0)); | ||
4176 | if (err) | ||
4177 | return err; | ||
4178 | (*line_cur1)++; | ||
4179 | } | ||
4180 | } | ||
4181 | if (rejectfile) { | ||
4182 | /* Copy new file's lines until EOF. */ | ||
4183 | while (!feof(f2)(!__isthreaded ? (((f2)->_flags & 0x0020) != 0) : (feof )(f2))) { | ||
4184 | err = copy_one_line(f2, NULL((void*)0), rejectfile); | ||
4185 | if (err) | ||
4186 | return err; | ||
4187 | (*line_cur2)++; | ||
4188 | } | ||
4189 | } | ||
4190 | |||
4191 | return NULL((void*)0); | ||
4192 | } | ||
4193 | |||
4194 | static const struct got_error * | ||
4195 | apply_or_reject_change(int *choice, int *nchunks_used, | ||
4196 | struct diff_result *diff_result, int n, | ||
4197 | const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2, | ||
4198 | FILE *outfile, FILE *rejectfile, int changeno, int nchanges, | ||
4199 | got_worktree_patch_cb patch_cb, void *patch_arg) | ||
4200 | { | ||
4201 | const struct got_error *err = NULL((void*)0); | ||
4202 | struct diff_chunk_context cc = {}; | ||
4203 | int start_old, end_old, start_new, end_new; | ||
4204 | FILE *hunkfile; | ||
4205 | struct diff_output_unidiff_state *diff_state; | ||
4206 | struct diff_input_info diff_info; | ||
4207 | int rc; | ||
4208 | |||
4209 | *choice = GOT_PATCH_CHOICE_NONE0; | ||
4210 | |||
4211 | /* Get changed line numbers without context lines for copy_change(). */ | ||
4212 | diff_chunk_context_load_change(&cc, NULL((void*)0), diff_result, n, 0); | ||
4213 | start_old = cc.left.start; | ||
4214 | end_old = cc.left.end; | ||
4215 | start_new = cc.right.start; | ||
4216 | end_new = cc.right.end; | ||
4217 | |||
4218 | /* Get the same change with context lines for display. */ | ||
4219 | memset(&cc, 0, sizeof(cc)); | ||
4220 | diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3); | ||
4221 | |||
4222 | memset(&diff_info, 0, sizeof(diff_info)); | ||
4223 | diff_info.left_path = relpath; | ||
4224 | diff_info.right_path = relpath; | ||
4225 | |||
4226 | diff_state = diff_output_unidiff_state_alloc(); | ||
4227 | if (diff_state == NULL((void*)0)) | ||
4228 | return got_error_set_errno(ENOMEM12, | ||
4229 | "diff_output_unidiff_state_alloc"); | ||
4230 | |||
4231 | hunkfile = got_opentemp(); | ||
4232 | if (hunkfile == NULL((void*)0)) { | ||
4233 | err = got_error_from_errno("got_opentemp"); | ||
4234 | goto done; | ||
4235 | } | ||
4236 | |||
4237 | rc = diff_output_unidiff_chunk(NULL((void*)0), hunkfile, diff_state, &diff_info, | ||
4238 | diff_result, &cc); | ||
4239 | if (rc != DIFF_RC_OK0) { | ||
4240 | err = got_error_set_errno(rc, "diff_output_unidiff_chunk"); | ||
4241 | goto done; | ||
4242 | } | ||
4243 | |||
4244 | if (fseek(hunkfile, 0L, SEEK_SET0) == -1) { | ||
4245 | err = got_ferror(hunkfile, GOT_ERR_IO6); | ||
4246 | goto done; | ||
4247 | } | ||
4248 | |||
4249 | err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY'M', relpath, | ||
4250 | hunkfile, changeno, nchanges); | ||
4251 | if (err) | ||
4252 | goto done; | ||
4253 | |||
4254 | switch (*choice) { | ||
4255 | case GOT_PATCH_CHOICE_YES1: | ||
4256 | err = copy_change(f1, f2, line_cur1, line_cur2, start_old, | ||
4257 | end_old, start_new, end_new, outfile, rejectfile); | ||
4258 | break; | ||
4259 | case GOT_PATCH_CHOICE_NO2: | ||
4260 | err = copy_change(f1, f2, line_cur1, line_cur2, start_old, | ||
4261 | end_old, start_new, end_new, rejectfile, outfile); | ||
4262 | break; | ||
4263 | case GOT_PATCH_CHOICE_QUIT3: | ||
4264 | break; | ||
4265 | default: | ||
4266 | err = got_error(GOT_ERR_PATCH_CHOICE107); | ||
4267 | break; | ||
4268 | } | ||
4269 | done: | ||
4270 | diff_output_unidiff_state_free(diff_state); | ||
4271 | if (hunkfile && fclose(hunkfile) == EOF(-1) && err == NULL((void*)0)) | ||
4272 | err = got_error_from_errno("fclose"); | ||
4273 | return err; | ||
4274 | } | ||
4275 | |||
4276 | struct revert_file_args { | ||
4277 | struct got_worktree *worktree; | ||
4278 | struct got_fileindex *fileindex; | ||
4279 | got_worktree_checkout_cb progress_cb; | ||
4280 | void *progress_arg; | ||
4281 | got_worktree_patch_cb patch_cb; | ||
4282 | void *patch_arg; | ||
4283 | struct got_repository *repo; | ||
4284 | }; | ||
4285 | |||
4286 | static const struct got_error * | ||
4287 | create_patched_content(char **path_outfile, int reverse_patch, | ||
4288 | struct got_object_id *blob_id, const char *path2, | ||
4289 | int dirfd2, const char *de_name2, | ||
4290 | const char *relpath, struct got_repository *repo, | ||
4291 | got_worktree_patch_cb patch_cb, void *patch_arg) | ||
4292 | { | ||
4293 | const struct got_error *err, *free_err; | ||
4294 | struct got_blob_object *blob = NULL((void*)0); | ||
4295 | FILE *f1 = NULL((void*)0), *f2 = NULL((void*)0), *outfile = NULL((void*)0); | ||
4296 | int fd2 = -1; | ||
4297 | char link_target[PATH_MAX1024]; | ||
4298 | ssize_t link_len = 0; | ||
4299 | char *path1 = NULL((void*)0), *id_str = NULL((void*)0); | ||
4300 | struct stat sb2; | ||
4301 | struct got_diffreg_result *diffreg_result = NULL((void*)0); | ||
4302 | int line_cur1 = 1, line_cur2 = 1, have_content = 0; | ||
4303 | int i = 0, n = 0, nchunks_used = 0, nchanges = 0; | ||
4304 | |||
4305 | *path_outfile = NULL((void*)0); | ||
4306 | |||
4307 | err = got_object_id_str(&id_str, blob_id); | ||
4308 | if (err) | ||
4309 | return err; | ||
4310 | |||
4311 | if (dirfd2 != -1) { | ||
4312 | fd2 = openat(dirfd2, de_name2, O_RDONLY0x0000 | O_NOFOLLOW0x0100); | ||
4313 | if (fd2 == -1) { | ||
4314 | if (errno(*__errno()) != ELOOP62) { | ||
4315 | err = got_error_from_errno2("openat", path2); | ||
4316 | goto done; | ||
4317 | } | ||
4318 | link_len = readlinkat(dirfd2, de_name2, | ||
4319 | link_target, sizeof(link_target)); | ||
4320 | if (link_len == -1) | ||
4321 | return got_error_from_errno2("readlinkat", path2); | ||
4322 | sb2.st_mode = S_IFLNK0120000; | ||
4323 | sb2.st_size = link_len; | ||
4324 | } | ||
4325 | } else { | ||
4326 | fd2 = open(path2, O_RDONLY0x0000 | O_NOFOLLOW0x0100); | ||
4327 | if (fd2 == -1) { | ||
4328 | if (errno(*__errno()) != ELOOP62) { | ||
4329 | err = got_error_from_errno2("open", path2); | ||
4330 | goto done; | ||
4331 | } | ||
4332 | link_len = readlink(path2, link_target, | ||
4333 | sizeof(link_target)); | ||
4334 | if (link_len == -1) | ||
4335 | return got_error_from_errno2("readlink", path2); | ||
4336 | sb2.st_mode = S_IFLNK0120000; | ||
4337 | sb2.st_size = link_len; | ||
4338 | } | ||
4339 | } | ||
4340 | if (fd2 != -1) { | ||
4341 | if (fstat(fd2, &sb2) == -1) { | ||
4342 | err = got_error_from_errno2("fstat", path2); | ||
4343 | goto done; | ||
4344 | } | ||
4345 | |||
4346 | f2 = fdopen(fd2, "r"); | ||
4347 | if (f2 == NULL((void*)0)) { | ||
4348 | err = got_error_from_errno2("fdopen", path2); | ||
4349 | goto done; | ||
4350 | } | ||
4351 | fd2 = -1; | ||
4352 | } else { | ||
4353 | size_t n; | ||
4354 | f2 = got_opentemp(); | ||
4355 | if (f2 == NULL((void*)0)) { | ||
4356 | err = got_error_from_errno2("got_opentemp", path2); | ||
4357 | goto done; | ||
4358 | } | ||
4359 | n = fwrite(link_target, 1, link_len, f2); | ||
4360 | if (n != link_len) { | ||
4361 | err = got_ferror(f2, GOT_ERR_IO6); | ||
4362 | goto done; | ||
4363 | } | ||
4364 | if (fflush(f2) == EOF(-1)) { | ||
4365 | err = got_error_from_errno("fflush"); | ||
4366 | goto done; | ||
4367 | } | ||
4368 | rewind(f2); | ||
4369 | } | ||
4370 | |||
4371 | err = got_object_open_as_blob(&blob, repo, blob_id, 8192); | ||
4372 | if (err) | ||
4373 | goto done; | ||
4374 | |||
4375 | err = got_opentemp_named(&path1, &f1, "got-patched-blob"); | ||
4376 | if (err) | ||
4377 | goto done; | ||
4378 | |||
4379 | err = got_object_blob_dump_to_file(NULL((void*)0), NULL((void*)0), NULL((void*)0), f1, blob); | ||
4380 | if (err) | ||
4381 | goto done; | ||
4382 | |||
4383 | err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1, | ||
4384 | NULL((void*)0)); | ||
4385 | if (err) | ||
4386 | goto done; | ||
4387 | |||
4388 | err = got_opentemp_named(path_outfile, &outfile, "got-patched-content"); | ||
4389 | if (err) | ||
4390 | goto done; | ||
4391 | |||
4392 | if (fseek(f1, 0L, SEEK_SET0) == -1) | ||
4393 | return got_ferror(f1, GOT_ERR_IO6); | ||
4394 | if (fseek(f2, 0L, SEEK_SET0) == -1) | ||
4395 | return got_ferror(f2, GOT_ERR_IO6); | ||
4396 | |||
4397 | /* Count the number of actual changes in the diff result. */ | ||
4398 | for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) { | ||
4399 | struct diff_chunk_context cc = {}; | ||
4400 | diff_chunk_context_load_change(&cc, &nchunks_used, | ||
4401 | diffreg_result->result, n, 0); | ||
4402 | nchanges++; | ||
4403 | } | ||
4404 | for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) { | ||
4405 | int choice; | ||
4406 | err = apply_or_reject_change(&choice, &nchunks_used, | ||
4407 | diffreg_result->result, n, relpath, f1, f2, | ||
4408 | &line_cur1, &line_cur2, | ||
4409 | reverse_patch ? NULL((void*)0) : outfile, | ||
4410 | reverse_patch ? outfile : NULL((void*)0), | ||
4411 | ++i, nchanges, patch_cb, patch_arg); | ||
4412 | if (err) | ||
4413 | goto done; | ||
4414 | if (choice == GOT_PATCH_CHOICE_YES1) | ||
4415 | have_content = 1; | ||
4416 | else if (choice == GOT_PATCH_CHOICE_QUIT3) | ||
4417 | break; | ||
4418 | } | ||
4419 | if (have_content) { | ||
4420 | err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2, | ||
4421 | reverse_patch ? NULL((void*)0) : outfile, | ||
4422 | reverse_patch ? outfile : NULL((void*)0)); | ||
4423 | if (err) | ||
4424 | goto done; | ||
4425 | |||
4426 | if (!S_ISLNK(sb2.st_mode)((sb2.st_mode & 0170000) == 0120000)) { | ||
4427 | if (fchmod(fileno(outfile)(!__isthreaded ? ((outfile)->_file) : (fileno)(outfile)), sb2.st_mode) == -1) { | ||
4428 | err = got_error_from_errno2("fchmod", path2); | ||
4429 | goto done; | ||
4430 | } | ||
4431 | } | ||
4432 | } | ||
4433 | done: | ||
4434 | free(id_str); | ||
4435 | if (blob) | ||
4436 | got_object_blob_close(blob); | ||
4437 | free_err = got_diffreg_result_free(diffreg_result); | ||
4438 | if (err == NULL((void*)0)) | ||
4439 | err = free_err; | ||
4440 | if (f1 && fclose(f1) == EOF(-1) && err == NULL((void*)0)) | ||
4441 | err = got_error_from_errno2("fclose", path1); | ||
4442 | if (f2 && fclose(f2) == EOF(-1) && err == NULL((void*)0)) | ||
4443 | err = got_error_from_errno2("fclose", path2); | ||
4444 | if (fd2 != -1 && close(fd2) == -1 && err == NULL((void*)0)) | ||
4445 | err = got_error_from_errno2("close", path2); | ||
4446 | if (outfile && fclose(outfile) == EOF(-1) && err == NULL((void*)0)) | ||
4447 | err = got_error_from_errno2("fclose", *path_outfile); | ||
4448 | if (path1 && unlink(path1) == -1 && err == NULL((void*)0)) | ||
4449 | err = got_error_from_errno2("unlink", path1); | ||
4450 | if (err || !have_content) { | ||
4451 | if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL((void*)0)) | ||
4452 | err = got_error_from_errno2("unlink", *path_outfile); | ||
4453 | free(*path_outfile); | ||
4454 | *path_outfile = NULL((void*)0); | ||
4455 | } | ||
4456 | free(path1); | ||
4457 | return err; | ||
4458 | } | ||
4459 | |||
4460 | static const struct got_error * | ||
4461 | revert_file(void *arg, unsigned char status, unsigned char staged_status, | ||
4462 | const char *relpath, struct got_object_id *blob_id, | ||
4463 | struct got_object_id *staged_blob_id, struct got_object_id *commit_id, | ||
4464 | int dirfd, const char *de_name) | ||
4465 | { | ||
4466 | struct revert_file_args *a = arg; | ||
4467 | const struct got_error *err = NULL((void*)0); | ||
4468 | char *parent_path = NULL((void*)0); | ||
4469 | struct got_fileindex_entry *ie; | ||
4470 | struct got_tree_object *tree = NULL((void*)0); | ||
4471 | struct got_object_id *tree_id = NULL((void*)0); | ||
4472 | const struct got_tree_entry *te = NULL((void*)0); | ||
4473 | char *tree_path = NULL((void*)0), *te_name; | ||
4474 | char *ondisk_path = NULL((void*)0), *path_content = NULL((void*)0); | ||
4475 | struct got_blob_object *blob = NULL((void*)0); | ||
4476 | |||
4477 | /* Reverting a staged deletion is a no-op. */ | ||
4478 | if (status == GOT_STATUS_DELETE'D' && | ||
4479 | staged_status != GOT_STATUS_NO_CHANGE' ') | ||
4480 | return NULL((void*)0); | ||
4481 | |||
4482 | if (status == GOT_STATUS_UNVERSIONED'?') | ||
4483 | return (*a->progress_cb)(a->progress_arg, | ||
4484 | GOT_STATUS_UNVERSIONED'?', relpath); | ||
4485 | |||
4486 | ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath)); | ||
4487 | if (ie == NULL((void*)0)) | ||
4488 | return got_error_path(relpath, GOT_ERR_BAD_PATH4); | ||
4489 | |||
4490 | /* Construct in-repository path of tree which contains this blob. */ | ||
4491 | err = got_path_dirname(&parent_path, ie->path); | ||
4492 | if (err) { | ||
4493 | if (err->code != GOT_ERR_BAD_PATH4) | ||
4494 | goto done; | ||
4495 | parent_path = strdup("/"); | ||
4496 | if (parent_path == NULL((void*)0)) { | ||
4497 | err = got_error_from_errno("strdup"); | ||
4498 | goto done; | ||
4499 | } | ||
4500 | } | ||
4501 | if (got_path_is_root_dir(a->worktree->path_prefix)) { | ||
4502 | tree_path = strdup(parent_path); | ||
4503 | if (tree_path == NULL((void*)0)) { | ||
4504 | err = got_error_from_errno("strdup"); | ||
4505 | goto done; | ||
4506 | } | ||
4507 | } else { | ||
4508 | if (got_path_is_root_dir(parent_path)) { | ||
4509 | tree_path = strdup(a->worktree->path_prefix); | ||
4510 | if (tree_path == NULL((void*)0)) { | ||
4511 | err = got_error_from_errno("strdup"); | ||
4512 | goto done; | ||
4513 | } | ||
4514 | } else { | ||
4515 | if (asprintf(&tree_path, "%s/%s", | ||
4516 | a->worktree->path_prefix, parent_path) == -1) { | ||
4517 | err = got_error_from_errno("asprintf"); | ||
4518 | goto done; | ||
4519 | } | ||
4520 | } | ||
4521 | } | ||
4522 | |||
4523 | err = got_object_id_by_path(&tree_id, a->repo, | ||
4524 | a->worktree->base_commit_id, tree_path); | ||
4525 | if (err) { | ||
4526 | if (!(err->code == GOT_ERR_NO_TREE_ENTRY50 && | ||
4527 | (status == GOT_STATUS_ADD'A' || | ||
4528 | staged_status == GOT_STATUS_ADD'A'))) | ||
4529 | goto done; | ||
4530 | } else { | ||
4531 | err = got_object_open_as_tree(&tree, a->repo, tree_id); | ||
4532 | if (err) | ||
4533 | goto done; | ||
4534 | |||
4535 | err = got_path_basename(&te_name, ie->path); | ||
4536 | if (err) | ||
4537 | goto done; | ||
4538 | |||
4539 | te = got_object_tree_find_entry(tree, te_name); | ||
4540 | free(te_name); | ||
4541 | if (te == NULL((void*)0) && status != GOT_STATUS_ADD'A' && | ||
4542 | staged_status != GOT_STATUS_ADD'A') { | ||
4543 | err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY50); | ||
4544 | goto done; | ||
4545 | } | ||
4546 | } | ||
4547 | |||
4548 | switch (status) { | ||
4549 | case GOT_STATUS_ADD'A': | ||
4550 | if (a->patch_cb) { | ||
4551 | int choice = GOT_PATCH_CHOICE_NONE0; | ||
4552 | err = (*a->patch_cb)(&choice, a->patch_arg, | ||
4553 | status, ie->path, NULL((void*)0), 1, 1); | ||
4554 | if (err) | ||
4555 | goto done; | ||
4556 | if (choice != GOT_PATCH_CHOICE_YES1) | ||
4557 | break; | ||
4558 | } | ||
4559 | err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT'R', | ||
4560 | ie->path); | ||
4561 | if (err) | ||
4562 | goto done; | ||
4563 | got_fileindex_entry_remove(a->fileindex, ie); | ||
4564 | break; | ||
4565 | case GOT_STATUS_DELETE'D': | ||
4566 | if (a->patch_cb) { | ||
4567 | int choice = GOT_PATCH_CHOICE_NONE0; | ||
4568 | err = (*a->patch_cb)(&choice, a->patch_arg, | ||
4569 | status, ie->path, NULL((void*)0), 1, 1); | ||
4570 | if (err) | ||
4571 | goto done; | ||
4572 | if (choice != GOT_PATCH_CHOICE_YES1) | ||
4573 | break; | ||
4574 | } | ||
4575 | /* fall through */ | ||
4576 | case GOT_STATUS_MODIFY'M': | ||
4577 | case GOT_STATUS_MODE_CHANGE'm': | ||
4578 | case GOT_STATUS_CONFLICT'C': | ||
4579 | case GOT_STATUS_MISSING'!': { | ||
4580 | struct got_object_id id; | ||
4581 | if (staged_status == GOT_STATUS_ADD'A' || | ||
4582 | staged_status == GOT_STATUS_MODIFY'M') { | ||
4583 | memcpy(id.sha1, ie->staged_blob_sha1, | ||
4584 | SHA1_DIGEST_LENGTH20); | ||
4585 | } else | ||
4586 | memcpy(id.sha1, ie->blob_sha1, | ||
4587 | SHA1_DIGEST_LENGTH20); | ||
4588 | err = got_object_open_as_blob(&blob, a->repo, &id, 8192); | ||
4589 | if (err) | ||
4590 | goto done; | ||
4591 | |||
4592 | if (asprintf(&ondisk_path, "%s/%s", | ||
4593 | got_worktree_get_root_path(a->worktree), relpath) == -1) { | ||
4594 | err = got_error_from_errno("asprintf"); | ||
4595 | goto done; | ||
4596 | } | ||
4597 | |||
4598 | if (a->patch_cb && (status == GOT_STATUS_MODIFY'M' || | ||
4599 | status == GOT_STATUS_CONFLICT'C')) { | ||
4600 | int is_bad_symlink = 0; | ||
4601 | err = create_patched_content(&path_content, 1, &id, | ||
4602 | ondisk_path, dirfd, de_name, ie->path, a->repo, | ||
4603 | a->patch_cb, a->patch_arg); | ||
4604 | if (err || path_content == NULL((void*)0)) | ||
4605 | break; | ||
4606 | if (te && S_ISLNK(te->mode)((te->mode & 0170000) == 0120000)) { | ||
4607 | if (unlink(path_content) == -1) { | ||
4608 | err = got_error_from_errno2("unlink", | ||
4609 | path_content); | ||
4610 | break; | ||
4611 | } | ||
4612 | err = install_symlink(&is_bad_symlink, | ||
4613 | a->worktree, ondisk_path, ie->path, | ||
4614 | blob, 0, 1, 0, a->repo, | ||
4615 | a->progress_cb, a->progress_arg); | ||
4616 | } else { | ||
4617 | if (rename(path_content, ondisk_path) == -1) { | ||
4618 | err = got_error_from_errno3("rename", | ||
4619 | path_content, ondisk_path); | ||
4620 | goto done; | ||
4621 | } | ||
4622 | } | ||
4623 | } else { | ||
4624 | int is_bad_symlink = 0; | ||
4625 | if (te && S_ISLNK(te->mode)((te->mode & 0170000) == 0120000)) { | ||
4626 | err = install_symlink(&is_bad_symlink, | ||
4627 | a->worktree, ondisk_path, ie->path, | ||
4628 | blob, 0, 1, 0, a->repo, | ||
4629 | a->progress_cb, a->progress_arg); | ||
4630 | } else { | ||
4631 | err = install_blob(a->worktree, ondisk_path, | ||
4632 | ie->path, | ||
4633 | te ? te->mode : GOT_DEFAULT_FILE_MODE(0100000 | 0000400|0000200 | 0000040 | 0000004), | ||
4634 | got_fileindex_perms_to_st(ie), blob, | ||
4635 | 0, 1, 0, 0, a->repo, | ||
4636 | a->progress_cb, a->progress_arg); | ||
4637 | } | ||
4638 | if (err) | ||
4639 | goto done; | ||
4640 | if (status == GOT_STATUS_DELETE'D' || | ||
4641 | status == GOT_STATUS_MODE_CHANGE'm') { | ||
4642 | err = got_fileindex_entry_update(ie, | ||
4643 | a->worktree->root_fd, relpath, | ||
4644 | blob->id.sha1, | ||
4645 | a->worktree->base_commit_id->sha1, 1); | ||
4646 | if (err) | ||
4647 | goto done; | ||
4648 | } | ||
4649 | if (is_bad_symlink) { | ||
4650 | got_fileindex_entry_filetype_set(ie, | ||
4651 | GOT_FILEIDX_MODE_BAD_SYMLINK3); | ||
4652 | } | ||
4653 | } | ||
4654 | break; | ||
4655 | } | ||
4656 | default: | ||
4657 | break; | ||
4658 | } | ||
4659 | done: | ||
4660 | free(ondisk_path); | ||
4661 | free(path_content); | ||
4662 | free(parent_path); | ||
4663 | free(tree_path); | ||
4664 | if (blob) | ||
4665 | got_object_blob_close(blob); | ||
4666 | if (tree) | ||
4667 | got_object_tree_close(tree); | ||
4668 | free(tree_id); | ||
4669 | return err; | ||
4670 | } | ||
4671 | |||
4672 | const struct got_error * | ||
4673 | got_worktree_revert(struct got_worktree *worktree, | ||
4674 | struct got_pathlist_head *paths, | ||
4675 | got_worktree_checkout_cb progress_cb, void *progress_arg, | ||
4676 | got_worktree_patch_cb patch_cb, void *patch_arg, | ||
4677 | struct got_repository *repo) | ||
4678 | { | ||
4679 | struct got_fileindex *fileindex = NULL((void*)0); | ||
4680 | char *fileindex_path = NULL((void*)0); | ||
4681 | const struct got_error *err = NULL((void*)0), *unlockerr = NULL((void*)0); | ||
4682 | const struct got_error *sync_err = NULL((void*)0); | ||
4683 | struct got_pathlist_entry *pe; | ||
4684 | struct revert_file_args rfa; | ||
4685 | |||
4686 | err = lock_worktree(worktree, LOCK_EX0x02); | ||
4687 | if (err) | ||
4688 | return err; | ||
4689 | |||
4690 | err = open_fileindex(&fileindex, &fileindex_path, worktree); | ||
4691 | if (err) | ||
4692 | goto done; | ||
4693 | |||
4694 | rfa.worktree = worktree; | ||
4695 | rfa.fileindex = fileindex; | ||
4696 | rfa.progress_cb = progress_cb; | ||
4697 | rfa.progress_arg = progress_arg; | ||
4698 | rfa.patch_cb = patch_cb; | ||
4699 | rfa.patch_arg = patch_arg; | ||
4700 | rfa.repo = repo; | ||
4701 | TAILQ_FOREACH(pe, paths, entry)for((pe) = ((paths)->tqh_first); (pe) != ((void*)0); (pe) = ((pe)->entry.tqe_next)) { | ||
4702 | err = worktree_status(worktree, pe->path, fileindex, repo, | ||
4703 | revert_file, &rfa, NULL((void*)0), NULL((void*)0), 0, 0); | ||
4704 | if (err) | ||
4705 | break; | ||
4706 | } | ||
4707 | sync_err = sync_fileindex(fileindex, fileindex_path); | ||
4708 | if (sync_err && err == NULL((void*)0)) | ||
4709 | err = sync_err; | ||
4710 | done: | ||
4711 | free(fileindex_path); | ||
4712 | if (fileindex) | ||
4713 | got_fileindex_free(fileindex); | ||
4714 | unlockerr = lock_worktree(worktree, LOCK_SH0x01); | ||
4715 | if (unlockerr && err == NULL((void*)0)) | ||
4716 | err = unlockerr; | ||
4717 | return err; | ||
4718 | } | ||
4719 | |||
4720 | static void | ||
4721 | free_commitable(struct got_commitable *ct) | ||
4722 | { | ||
4723 | free(ct->path); | ||
4724 | free(ct->in_repo_path); | ||
4725 | free(ct->ondisk_path); | ||
4726 | free(ct->blob_id); | ||
4727 | free(ct->base_blob_id); | ||
4728 | free(ct->staged_blob_id); | ||
4729 | free(ct->base_commit_id); | ||
4730 | free(ct); | ||
4731 | } | ||
4732 | |||
4733 | struct collect_commitables_arg { | ||
4734 | struct got_pathlist_head *commitable_paths; | ||
4735 | struct got_repository *repo; | ||
4736 | struct got_worktree *worktree; | ||
4737 | struct got_fileindex *fileindex; | ||
4738 | int have_staged_files; | ||
4739 | int allow_bad_symlinks; | ||
4740 | }; | ||
4741 | |||
4742 | static const struct got_error * | ||
4743 | collect_commitables(void *arg, unsigned char status, | ||
4744 | unsigned char staged_status, const char *relpath, | ||
4745 | struct got_object_id *blob_id, struct got_object_id *staged_blob_id, | ||
4746 | struct got_object_id *commit_id, int dirfd, const char *de_name) | ||
4747 | { | ||
4748 | struct collect_commitables_arg *a = arg; | ||
4749 | const struct got_error *err = NULL((void*)0); | ||
4750 | struct got_commitable *ct = NULL((void*)0); | ||
4751 | struct got_pathlist_entry *new = NULL((void*)0); | ||
4752 | char *parent_path = NULL((void*)0), *path = NULL((void*)0); | ||
4753 | struct stat sb; | ||
4754 | |||
4755 | if (a->have_staged_files) { | ||
4756 | if (staged_status != GOT_STATUS_MODIFY'M' && | ||
4757 | staged_status != GOT_STATUS_ADD'A' && | ||
4758 | staged_status != GOT_STATUS_DELETE'D') | ||
4759 | return NULL((void*)0); | ||
4760 | } else { | ||
4761 | if (status == GOT_STATUS_CONFLICT'C') | ||
4762 | return got_error(GOT_ERR_COMMIT_CONFLICT69); | ||
4763 | |||
4764 | if (status != GOT_STATUS_MODIFY'M' && | ||
4765 | status != GOT_STATUS_MODE_CHANGE'm' && | ||
4766 | status != GOT_STATUS_ADD'A' && | ||
4767 | status != GOT_STATUS_DELETE'D') | ||
4768 | return NULL((void*)0); | ||
4769 | } | ||
4770 | |||
4771 | if (asprintf(&path, "/%s", relpath) == -1) { | ||
4772 | err = got_error_from_errno("asprintf"); | ||
4773 | goto done; | ||
4774 | } | ||
4775 | if (strcmp(path, "/") == 0) { | ||
4776 | parent_path = strdup(""); | ||
4777 | if (parent_path == NULL((void*)0)) | ||
4778 | return got_error_from_errno("strdup"); | ||
4779 | } else { | ||
4780 | err = got_path_dirname(&parent_path, path); | ||
4781 | if (err) | ||
4782 | return err; | ||
4783 | } | ||
4784 | |||
4785 | ct = calloc(1, sizeof(*ct)); | ||
4786 | if (ct == NULL((void*)0)) { | ||
4787 | err = got_error_from_errno("calloc"); | ||
4788 | goto done; | ||
4789 | } | ||
4790 | |||
4791 | if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path, | ||
4792 | relpath) == -1) { | ||
4793 | err = got_error_from_errno("asprintf"); | ||
4794 | goto done; | ||
4795 | } | ||
4796 | |||
4797 | if (staged_status == GOT_STATUS_ADD'A' || | ||
4798 | staged_status == GOT_STATUS_MODIFY'M') { | ||
4799 | struct got_fileindex_entry *ie; | ||
4800 | ie = got_fileindex_entry_get(a->fileindex, path, strlen(path)); | ||
4801 | switch (got_fileindex_entry_staged_filetype_get(ie)) { | ||
4802 | case GOT_FILEIDX_MODE_REGULAR_FILE1: | ||
4803 | case GOT_FILEIDX_MODE_BAD_SYMLINK3: | ||
4804 | ct->mode = S_IFREG0100000; | ||
4805 | break; | ||
4806 | case GOT_FILEIDX_MODE_SYMLINK2: | ||
4807 | ct->mode = S_IFLNK0120000; | ||
4808 | break; | ||
4809 | default: | ||
4810 | err = got_error_path(path, GOT_ERR_BAD_FILETYPE3); | ||
4811 | goto done; | ||
4812 | } | ||
4813 | ct->mode |= got_fileindex_entry_perms_get(ie); | ||
4814 | } else if (status != GOT_STATUS_DELETE'D' && | ||
4815 | staged_status != GOT_STATUS_DELETE'D') { | ||
4816 | if (dirfd != -1) { | ||
4817 | if (fstatat(dirfd, de_name, &sb, | ||
4818 | AT_SYMLINK_NOFOLLOW0x02) == -1) { | ||
4819 | err = got_error_from_errno2("fstatat", | ||
4820 | ct->ondisk_path); | ||
4821 | goto done; | ||
4822 | } | ||
4823 | } else if (lstat(ct->ondisk_path, &sb) == -1) { | ||
4824 | err = got_error_from_errno2("lstat", ct->ondisk_path); | ||
4825 | goto done; | ||
4826 | } | ||
4827 | ct->mode = sb.st_mode; | ||
4828 | } | ||
4829 | |||
4830 | if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix, | ||
4831 | got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/", | ||
4832 | relpath) == -1) { | ||
4833 | err = got_error_from_errno("asprintf"); | ||
4834 | goto done; | ||
4835 | } | ||
4836 | |||
4837 | if (S_ISLNK(ct->mode)((ct->mode & 0170000) == 0120000) && staged_status == GOT_STATUS_NO_CHANGE' ' && | ||
4838 | status == GOT_STATUS_ADD'A' && !a->allow_bad_symlinks) { | ||
4839 | int is_bad_symlink; | ||
4840 | char target_path[PATH_MAX1024]; | ||
4841 | ssize_t target_len; | ||
4842 | target_len = readlink(ct->ondisk_path, target_path, | ||
4843 | sizeof(target_path)); | ||
4844 | if (target_len == -1) { | ||
4845 | err = got_error_from_errno2("readlink", | ||
4846 | ct->ondisk_path); | ||
4847 | goto done; | ||
4848 | } | ||
4849 | err = is_bad_symlink_target(&is_bad_symlink, target_path, | ||
4850 | target_len, ct->ondisk_path, a->worktree->root_path); | ||
4851 | if (err) | ||
4852 | goto done; | ||
4853 | if (is_bad_symlink) { | ||
4854 | err = got_error_path(ct->ondisk_path, | ||
4855 | GOT_ERR_BAD_SYMLINK129); | ||
4856 | goto done; | ||
4857 | } | ||
4858 | } | ||
4859 | |||
4860 | |||
4861 | ct->status = status; | ||
4862 | ct->staged_status = staged_status; | ||
4863 | ct->blob_id = NULL((void*)0); /* will be filled in when blob gets created */ | ||
4864 | if (ct->status != GOT_STATUS_ADD'A' && | ||
4865 | ct->staged_status != GOT_STATUS_ADD'A') { | ||
4866 | ct->base_blob_id = got_object_id_dup(blob_id); | ||
4867 | if (ct->base_blob_id == NULL((void*)0)) { | ||
4868 | err = got_error_from_errno("got_object_id_dup"); | ||
4869 | goto done; | ||
4870 | } | ||
4871 | ct->base_commit_id = got_object_id_dup(commit_id); | ||
4872 | if (ct->base_commit_id == NULL((void*)0)) { | ||
4873 | err = got_error_from_errno("got_object_id_dup"); | ||
4874 | goto done; | ||
4875 | } | ||
4876 | } | ||
4877 | if (ct->staged_status == GOT_STATUS_ADD'A' || | ||
4878 | ct->staged_status == GOT_STATUS_MODIFY'M') { | ||
4879 | ct->staged_blob_id = got_object_id_dup(staged_blob_id); | ||
4880 | if (ct->staged_blob_id == NULL((void*)0)) { | ||
4881 | err = got_error_from_errno("got_object_id_dup"); | ||
4882 | goto done; | ||
4883 | } | ||
4884 | } | ||
4885 | ct->path = strdup(path); | ||
4886 | if (ct->path == NULL((void*)0)) { | ||
4887 | err = got_error_from_errno("strdup"); | ||
4888 | goto done; | ||
4889 | } | ||
4890 | err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct); | ||
4891 | done: | ||
4892 | if (ct && (err || new == NULL((void*)0))) | ||
4893 | free_commitable(ct); | ||
4894 | free(parent_path); | ||
4895 | free(path); | ||
4896 | return err; | ||
4897 | } | ||
4898 | |||
4899 | static const struct got_error *write_tree(struct got_object_id **, int *, | ||
4900 | struct got_tree_object *, const char *, struct got_pathlist_head *, | ||
4901 | got_worktree_status_cb status_cb, void *status_arg, | ||
4902 | struct got_repository *); | ||
4903 | |||
4904 | static const struct got_error * | ||
4905 | write_subtree(struct got_object_id **new_subtree_id, int *nentries, | ||
4906 | struct got_tree_entry *te, const char *parent_path, | ||
4907 | struct got_pathlist_head *commitable_paths, | ||
4908 | got_worktree_status_cb status_cb, void *status_arg, | ||
4909 | struct got_repository *repo) | ||
4910 | { | ||
4911 | const struct got_error *err = NULL((void*)0); | ||
4912 | struct got_tree_object *subtree; | ||
4913 | char *subpath; | ||
4914 | |||
4915 | if (asprintf(&subpath, "%s%s%s", parent_path, | ||
4916 | got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1) | ||
4917 | return got_error_from_errno("asprintf"); | ||
4918 | |||
4919 | err = got_object_open_as_tree(&subtree, repo, &te->id); | ||
4920 | if (err) | ||
4921 | return err; | ||
4922 | |||
4923 | err = write_tree(new_subtree_id, nentries, subtree, subpath, | ||
4924 | commitable_paths, status_cb, status_arg, repo); | ||
4925 | got_object_tree_close(subtree); | ||
4926 | free(subpath); | ||
4927 | return err; | ||
4928 | } | ||
4929 | |||
4930 | static const struct got_error * | ||
4931 | match_ct_parent_path(int *match, struct got_commitable *ct, const char *path) | ||
4932 | { | ||
4933 | const struct got_error *err = NULL((void*)0); | ||
4934 | char *ct_parent_path = NULL((void*)0); | ||
4935 | |||
4936 | *match = 0; | ||
4937 | |||
4938 | if (strchr(ct->in_repo_path, '/') == NULL((void*)0)) { | ||
4939 | *match = got_path_is_root_dir(path); | ||
4940 | return NULL((void*)0); | ||
4941 | } | ||
4942 | |||
4943 | err = got_path_dirname(&ct_parent_path, ct->in_repo_path); | ||
4944 | if (err) | ||
4945 | return err; | ||
4946 | *match = (strcmp(path, ct_parent_path) == 0); | ||
4947 | free(ct_parent_path); | ||
4948 | return err; | ||
4949 | } | ||
4950 | |||
4951 | static mode_t | ||
4952 | get_ct_file_mode(struct got_commitable *ct) | ||
4953 | { | ||
4954 | if (S_ISLNK(ct->mode)((ct->mode & 0170000) == 0120000)) | ||
4955 | return S_IFLNK0120000; | ||
4956 | |||
4957 | return S_IFREG0100000 | (ct->mode & ((S_IRWXU0000700 | S_IRWXG0000070 | S_IRWXO0000007))); | ||
4958 | } | ||
4959 | |||
4960 | static const struct got_error * | ||
4961 | alloc_modified_blob_tree_entry(struct got_tree_entry **new_te, | ||
4962 | struct got_tree_entry *te, struct got_commitable *ct) | ||
4963 | { | ||
4964 | const struct got_error *err = NULL((void*)0); | ||
4965 | |||
4966 | *new_te = NULL((void*)0); | ||
4967 | |||
4968 | err = got_object_tree_entry_dup(new_te, te); | ||
4969 | if (err) | ||
4970 | goto done; | ||
4971 | |||
4972 | (*new_te)->mode = get_ct_file_mode(ct); | ||
4973 | |||
4974 | if (ct->staged_status == GOT_STATUS_MODIFY'M') | ||
4975 | memcpy(&(*new_te)->id, ct->staged_blob_id, | ||
4976 | sizeof((*new_te)->id)); | ||
4977 | else | ||
4978 | memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id)); | ||
4979 | done: | ||
4980 | if (err && *new_te) { | ||
4981 | free(*new_te); | ||
4982 | *new_te = NULL((void*)0); | ||
4983 | } | ||
4984 | return err; | ||
4985 | } | ||
4986 | |||
4987 | static const struct got_error * | ||
4988 | alloc_added_blob_tree_entry(struct got_tree_entry **new_te, | ||
4989 | struct got_commitable *ct) | ||
4990 | { | ||
4991 | const struct got_error *err = NULL((void*)0); | ||
4992 | char *ct_name = NULL((void*)0); | ||
4993 | |||
4994 | *new_te = NULL((void*)0); | ||
4995 | |||
4996 | *new_te = calloc(1, sizeof(**new_te)); | ||
4997 | if (*new_te == NULL((void*)0)) | ||
4998 | return got_error_from_errno("calloc"); | ||
4999 | |||
5000 | err = got_path_basename(&ct_name, ct->path); | ||
5001 | if (err) | ||
5002 | goto done; | ||
5003 | if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >= | ||
5004 | sizeof((*new_te)->name)) { | ||
5005 | err = got_error(GOT_ERR_NO_SPACE9); | ||
5006 | goto done; | ||
5007 | } | ||
5008 | |||
5009 | (*new_te)->mode = get_ct_file_mode(ct); | ||
5010 | |||
5011 | if (ct->staged_status == GOT_STATUS_ADD'A') | ||
5012 | memcpy(&(*new_te)->id, ct->staged_blob_id, | ||
5013 | sizeof((*new_te)->id)); | ||
5014 | else | ||
5015 | memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id)); | ||
5016 | done: | ||
5017 | free(ct_name); | ||
5018 | if (err && *new_te) { | ||
5019 | free(*new_te); | ||
5020 | *new_te = NULL((void*)0); | ||
5021 | } | ||
5022 | return err; | ||
5023 | } | ||
5024 | |||
5025 | static const struct got_error * | ||
5026 | insert_tree_entry(struct got_tree_entry *new_te, | ||
5027 | struct got_pathlist_head *paths) | ||
5028 | { | ||
5029 | const struct got_error *err = NULL((void*)0); | ||
5030 | struct got_pathlist_entry *new_pe; | ||
5031 | |||
5032 | err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te); | ||
5033 | if (err) | ||
5034 | return err; | ||
5035 | if (new_pe == NULL((void*)0)) | ||
5036 | return got_error(GOT_ERR_TREE_DUP_ENTRY58); | ||
5037 | return NULL((void*)0); | ||
5038 | } | ||
5039 | |||
5040 | static const struct got_error * | ||
5041 | report_ct_status(struct got_commitable *ct, | ||
5042 | got_worktree_status_cb status_cb, void *status_arg) | ||
5043 | { | ||
5044 | const char *ct_path = ct->path; | ||
5045 | unsigned char status; | ||
5046 | |||
5047 | while (ct_path[0] == '/') | ||
5048 | ct_path++; | ||
5049 | |||
5050 | if (ct->staged_status != GOT_STATUS_NO_CHANGE' ') | ||
5051 | status = ct->staged_status; | ||
5052 | else | ||
5053 | status = ct->status; | ||
5054 | |||
5055 | return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE' ', | ||
5056 | ct_path, ct->blob_id, NULL((void*)0), NULL((void*)0), -1, NULL((void*)0)); | ||
5057 | } | ||
5058 | |||
5059 | static const struct got_error * | ||
5060 | match_modified_subtree(int *modified, struct got_tree_entry *te, | ||
5061 | const char *base_tree_path, struct got_pathlist_head *commitable_paths) | ||
5062 | { | ||
5063 | const struct got_error *err = NULL((void*)0); | ||
5064 | struct got_pathlist_entry *pe; | ||
5065 | char *te_path; | ||
5066 | |||
5067 | *modified = 0; | ||
5068 | |||
5069 | if (asprintf(&te_path, "%s%s%s", base_tree_path, | ||
5070 | got_path_is_root_dir(base_tree_path) ? "" : "/", | ||
5071 | te->name) == -1) | ||
5072 | return got_error_from_errno("asprintf"); | ||
5073 | |||
5074 | TAILQ_FOREACH(pe, commitable_paths, entry)for((pe) = ((commitable_paths)->tqh_first); (pe) != ((void *)0); (pe) = ((pe)->entry.tqe_next)) { | ||
5075 | struct got_commitable *ct = pe->data; | ||
5076 | *modified = got_path_is_child(ct->in_repo_path, te_path, | ||
5077 | strlen(te_path)); | ||
5078 | if (*modified) | ||
5079 | break; | ||
5080 | } | ||
5081 | |||
5082 | free(te_path); | ||
5083 | return err; | ||
5084 | } | ||
5085 | |||
5086 | static const struct got_error * | ||
5087 | match_deleted_or_modified_ct(struct got_commitable **ctp, | ||
5088 | struct got_tree_entry *te, const char *base_tree_path, | ||
5089 | struct got_pathlist_head *commitable_paths) | ||
5090 | { | ||
5091 | const struct got_error *err = NULL((void*)0); | ||
5092 | struct got_pathlist_entry *pe; | ||
5093 | |||
5094 | *ctp = NULL((void*)0); | ||
5095 | |||
5096 | TAILQ_FOREACH(pe, commitable_paths, entry)for((pe) = ((commitable_paths)->tqh_first); (pe) != ((void *)0); (pe) = ((pe)->entry.tqe_next)) { | ||
5097 | struct got_commitable *ct = pe->data; | ||
5098 | char *ct_name = NULL((void*)0); | ||
5099 | int path_matches; | ||
5100 | |||
5101 | if (ct->staged_status == GOT_STATUS_NO_CHANGE' ') { | ||
5102 | if (ct->status != GOT_STATUS_MODIFY'M' && | ||
5103 | ct->status != GOT_STATUS_MODE_CHANGE'm' && | ||
5104 | ct->status != GOT_STATUS_DELETE'D') | ||
5105 | continue; | ||
5106 | } else { | ||
5107 | if (ct->staged_status != GOT_STATUS_MODIFY'M' && | ||
5108 | ct->staged_status != GOT_STATUS_DELETE'D') | ||
5109 | continue; | ||
5110 | } | ||
5111 | |||
5112 | if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0) | ||
5113 | continue; | ||
5114 | |||
5115 | err = match_ct_parent_path(&path_matches, ct, base_tree_path); | ||
5116 | if (err) | ||
5117 | return err; | ||
5118 | if (!path_matches) | ||
5119 | continue; | ||
5120 | |||
5121 | err = got_path_basename(&ct_name, pe->path); | ||
5122 | if (err) | ||
5123 | return err; | ||
5124 | |||
5125 | if (strcmp(te->name, ct_name) != 0) { | ||
5126 | free(ct_name); | ||
5127 | continue; | ||
5128 | } | ||
5129 | free(ct_name); | ||
5130 | |||
5131 | *ctp = ct; | ||
5132 | break; | ||
5133 | } | ||
5134 | |||
5135 | return err; | ||
5136 | } | ||
5137 | |||
5138 | static const struct got_error * | ||
5139 | make_subtree_for_added_blob(struct got_tree_entry **new_tep, | ||
5140 | const char *child_path, const char *path_base_tree, | ||
5141 | struct got_pathlist_head *commitable_paths, | ||
5142 | got_worktree_status_cb status_cb, void *status_arg, | ||
5143 | struct got_repository *repo) | ||
5144 | { | ||
5145 | const struct got_error *err = NULL((void*)0); | ||
5146 | struct got_tree_entry *new_te; | ||
5147 | char *subtree_path; | ||
5148 | struct got_object_id *id = NULL((void*)0); | ||
5149 | int nentries; | ||
5150 | |||
5151 | *new_tep = NULL((void*)0); | ||
5152 | |||
5153 | if (asprintf(&subtree_path, "%s%s%s", path_base_tree, | ||
5154 | got_path_is_root_dir(path_base_tree) ? "" : "/", | ||
5155 | child_path) == -1) | ||
5156 | return got_error_from_errno("asprintf"); | ||
5157 | |||
5158 | new_te = calloc(1, sizeof(*new_te)); | ||
5159 | if (new_te == NULL((void*)0)) | ||
5160 | return got_error_from_errno("calloc"); | ||
5161 | new_te->mode = S_IFDIR0040000; | ||
5162 | |||
5163 | if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >= | ||
5164 | sizeof(new_te->name)) { | ||
5165 | err = got_error(GOT_ERR_NO_SPACE9); | ||
5166 | goto done; | ||
5167 | } | ||
5168 | err = write_tree(&id, &nentries, NULL((void*)0), subtree_path, | ||
5169 | commitable_paths, status_cb, status_arg, repo); | ||
5170 | if (err) { | ||
5171 | free(new_te); | ||
5172 | goto done; | ||
5173 | } | ||
5174 | memcpy(&new_te->id, id, sizeof(new_te->id)); | ||
5175 | done: | ||
5176 | free(id); | ||
5177 | free(subtree_path); | ||
5178 | if (err == NULL((void*)0)) | ||
5179 | *new_tep = new_te; | ||
5180 | return err; | ||
5181 | } | ||
5182 | |||
5183 | static const struct got_error * | ||
5184 | write_tree(struct got_object_id **new_tree_id, int *nentries, | ||
5185 | struct got_tree_object *base_tree, const char *path_base_tree, | ||
5186 | struct got_pathlist_head *commitable_paths, | ||
5187 | got_worktree_status_cb status_cb, void *status_arg, | ||
5188 | struct got_repository *repo) | ||
5189 | { | ||
5190 | const struct got_error *err = NULL((void*)0); | ||
5191 | struct got_pathlist_head paths; | ||
5192 | struct got_tree_entry *te, *new_te = NULL((void*)0); | ||
5193 | struct got_pathlist_entry *pe; | ||
5194 | |||
5195 | TAILQ_INIT(&paths)do { (&paths)->tqh_first = ((void*)0); (&paths)-> tqh_last = &(&paths)->tqh_first; } while (0); | ||
5196 | *nentries = 0; | ||
5197 | |||
5198 | /* Insert, and recurse into, newly added entries first. */ | ||
5199 | TAILQ_FOREACH(pe, commitable_paths, entry)for((pe) = ((commitable_paths)->tqh_first); (pe) != ((void *)0); (pe) = ((pe)->entry.tqe_next)) { | ||
5200 | struct got_commitable *ct = pe->data; | ||
5201 | char *child_path = NULL((void*)0), *slash; | ||
5202 | |||
5203 | if ((ct->status != GOT_STATUS_ADD'A' && | ||
5204 | ct->staged_status != GOT_STATUS_ADD'A') || | ||
5205 | (ct->flags & GOT_COMMITABLE_ADDED0x01)) | ||
5206 | continue; | ||
5207 | |||
5208 | if (!got_path_is_child(ct->in_repo_path, path_base_tree, | ||
5209 | strlen(path_base_tree))) | ||
5210 | continue; | ||
5211 | |||
5212 | err = got_path_skip_common_ancestor(&child_path, path_base_tree, | ||
5213 | ct->in_repo_path); | ||
5214 | if (err) | ||
5215 | goto done; | ||
5216 | |||
5217 | slash = strchr(child_path, '/'); | ||
5218 | if (slash == NULL((void*)0)) { | ||
5219 | err = alloc_added_blob_tree_entry(&new_te, ct); | ||
5220 | if (err) | ||
5221 | goto done; | ||
5222 | err = report_ct_status(ct, status_cb, status_arg); | ||
5223 | if (err) | ||
5224 | goto done; | ||
5225 | ct->flags |= GOT_COMMITABLE_ADDED0x01; | ||
5226 | err = insert_tree_entry(new_te, &paths); | ||
5227 | if (err) | ||
5228 | goto done; | ||
5229 | (*nentries)++; | ||
5230 | } else { | ||
5231 | *slash = '\0'; /* trim trailing path components */ | ||
5232 | if (base_tree == NULL((void*)0) || | ||
5233 | got_object_tree_find_entry(base_tree, child_path) | ||
5234 | == NULL((void*)0)) { | ||
5235 | err = make_subtree_for_added_blob(&new_te, | ||
5236 | child_path, path_base_tree, | ||
5237 | commitable_paths, status_cb, status_arg, | ||
5238 | repo); | ||
5239 | if (err) | ||
5240 | goto done; | ||
5241 | err = insert_tree_entry(new_te, &paths); | ||
5242 | if (err) | ||
5243 | goto done; | ||
5244 | (*nentries)++; | ||
5245 | } | ||
5246 | } | ||
5247 | } | ||
5248 | |||
5249 | if (base_tree) { | ||
5250 | int i, nbase_entries; | ||
5251 | /* Handle modified and deleted entries. */ | ||
5252 | nbase_entries = got_object_tree_get_nentries(base_tree); | ||
5253 | for (i = 0; i < nbase_entries; i++) { | ||
5254 | struct got_commitable *ct = NULL((void*)0); | ||
5255 | |||
5256 | te = got_object_tree_get_entry(base_tree, i); | ||
5257 | if (got_object_tree_entry_is_submodule(te)) { | ||
5258 | /* Entry is a submodule; just copy it. */ | ||
5259 | err = got_object_tree_entry_dup(&new_te, te); | ||
5260 | if (err) | ||
5261 | goto done; | ||
5262 | err = insert_tree_entry(new_te, &paths); | ||
5263 | if (err) | ||
5264 | goto done; | ||
5265 | (*nentries)++; | ||
5266 | continue; | ||
5267 | } | ||
5268 | |||
5269 | if (S_ISDIR(te->mode)((te->mode & 0170000) == 0040000)) { | ||
5270 | int modified; | ||
5271 | err = got_object_tree_entry_dup(&new_te, te); | ||
5272 | if (err) | ||
5273 | goto done; | ||
5274 | err = match_modified_subtree(&modified, te, | ||
5275 | path_base_tree, commitable_paths); | ||
5276 | if (err) | ||
5277 | goto done; | ||
5278 | /* Avoid recursion into unmodified subtrees. */ | ||
5279 | if (modified) { | ||
5280 | struct got_object_id *new_id; | ||
5281 | int nsubentries; | ||
5282 | err = write_subtree(&new_id, | ||
5283 | &nsubentries, te, | ||
5284 | path_base_tree, commitable_paths, | ||
5285 | status_cb, status_arg, repo); | ||
5286 | if (err) | ||
5287 | goto done; | ||
5288 | if (nsubentries == 0) { | ||
5289 | /* All entries were deleted. */ | ||
5290 | free(new_id); | ||
5291 | continue; | ||
5292 | } | ||
5293 | memcpy(&new_te->id, new_id, | ||
5294 | sizeof(new_te->id)); | ||
5295 | free(new_id); | ||
5296 | } | ||
5297 | err = insert_tree_entry(new_te, &paths); | ||
5298 | if (err) | ||
5299 | goto done; | ||
5300 | (*nentries)++; | ||
5301 | continue; | ||
5302 | } | ||
5303 | |||
5304 | err = match_deleted_or_modified_ct(&ct, te, | ||
5305 | path_base_tree, commitable_paths); | ||
5306 | if (err) | ||
5307 | goto done; | ||
5308 | if (ct) { | ||
5309 | /* NB: Deleted entries get dropped here. */ | ||
5310 | if (ct->status == GOT_STATUS_MODIFY'M' || | ||
5311 | ct->status == GOT_STATUS_MODE_CHANGE'm' || | ||
5312 | ct->staged_status == GOT_STATUS_MODIFY'M') { | ||
5313 | err = alloc_modified_blob_tree_entry( | ||
5314 | &new_te, te, ct); | ||
5315 | if (err) | ||
5316 | goto done; | ||
5317 | err = insert_tree_entry(new_te, &paths); | ||
5318 | if (err) | ||
5319 | goto done; | ||
5320 | (*nentries)++; | ||
5321 | } | ||
5322 | err = report_ct_status(ct, status_cb, | ||
5323 | status_arg); | ||
5324 | if (err) | ||
5325 | goto done; | ||
5326 | } else { | ||
5327 | /* Entry is unchanged; just copy it. */ | ||
5328 | err = got_object_tree_entry_dup(&new_te, te); | ||
5329 | if (err) | ||
5330 | goto done; | ||
5331 | err = insert_tree_entry(new_te, &paths); | ||
5332 | if (err) | ||
5333 | goto done; | ||
5334 | (*nentries)++; | ||
5335 | } | ||
5336 | } | ||
5337 | } | ||
5338 | |||
5339 | /* Write new list of entries; deleted entries have been dropped. */ | ||
5340 | err = got_object_tree_create(new_tree_id, &paths, *nentries, repo); | ||
5341 | done: | ||
5342 | got_pathlist_free(&paths); | ||
5343 | return err; | ||
5344 | } | ||
5345 | |||
5346 | static const struct got_error * | ||
5347 | update_fileindex_after_commit(struct got_worktree *worktree, | ||
5348 | struct got_pathlist_head *commitable_paths, | ||
5349 | struct got_object_id *new_base_commit_id, | ||
5350 | struct got_fileindex *fileindex, int have_staged_files) | ||
5351 | { | ||
5352 | const struct got_error *err = NULL((void*)0); | ||
5353 | struct got_pathlist_entry *pe; | ||
5354 | char *relpath = NULL((void*)0); | ||
5355 | |||
5356 | TAILQ_FOREACH(pe, commitable_paths, entry)for((pe) = ((commitable_paths)->tqh_first); (pe) != ((void *)0); (pe) = ((pe)->entry.tqe_next)) { | ||
5357 | struct got_fileindex_entry *ie; | ||
5358 | struct got_commitable *ct = pe->data; | ||
5359 | |||
5360 | ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len); | ||
5361 | |||
5362 | err = got_path_skip_common_ancestor(&relpath, | ||
5363 | worktree->root_path, ct->ondisk_path); | ||
5364 | if (err) | ||
5365 | goto done; | ||
5366 | |||
5367 | if (ie) { | ||
5368 | if (ct->status == GOT_STATUS_DELETE'D' || | ||
5369 | ct->staged_status == GOT_STATUS_DELETE'D') { | ||
5370 | got_fileindex_entry_remove(fileindex, ie); | ||
5371 | } else if (ct->staged_status == GOT_STATUS_ADD'A' || | ||
5372 | ct->staged_status == GOT_STATUS_MODIFY'M') { | ||
5373 | got_fileindex_entry_stage_set(ie, | ||
5374 | GOT_FILEIDX_STAGE_NONE0); | ||
5375 | got_fileindex_entry_staged_filetype_set(ie, 0); | ||
5376 | |||
5377 | err = got_fileindex_entry_update(ie, | ||
5378 | worktree->root_fd, relpath, | ||
5379 | ct->staged_blob_id->sha1, | ||
5380 | new_base_commit_id->sha1, | ||
5381 | !have_staged_files); | ||
5382 | } else | ||
5383 | err = got_fileindex_entry_update(ie, | ||
5384 | worktree->root_fd, relpath, | ||
5385 | ct->blob_id->sha1, | ||
5386 | new_base_commit_id->sha1, | ||
5387 | !have_staged_files); | ||
5388 | } else { | ||
5389 | err = got_fileindex_entry_alloc(&ie, pe->path); | ||
5390 | if (err) | ||
5391 | goto done; | ||
5392 | err = got_fileindex_entry_update(ie, | ||
5393 | worktree->root_fd, relpath, ct->blob_id->sha1, | ||
5394 | new_base_commit_id->sha1, 1); | ||
5395 | if (err) { | ||
5396 | got_fileindex_entry_free(ie); | ||
5397 | goto done; | ||
5398 | } | ||
5399 | err = got_fileindex_entry_add(fileindex, ie); | ||
5400 | if (err) { | ||
5401 | got_fileindex_entry_free(ie); | ||
5402 | goto done; | ||
5403 | } | ||
5404 | } | ||
5405 | free(relpath); | ||
5406 | relpath = NULL((void*)0); | ||
5407 | } | ||
5408 | done: | ||
5409 | free(relpath); | ||
5410 | return err; | ||
5411 | } | ||
5412 | |||
5413 | |||
5414 | static const struct got_error * | ||
5415 | check_out_of_date(const char *in_repo_path, unsigned char status, | ||
5416 | unsigned char staged_status, struct got_object_id *base_blob_id, | ||
5417 | struct got_object_id *base_commit_id, | ||
5418 | struct got_object_id *head_commit_id, struct got_repository *repo, | ||
5419 | int ood_errcode) | ||
5420 | { | ||
5421 | const struct got_error *err = NULL((void*)0); | ||
5422 | struct got_object_id *id = NULL((void*)0); | ||
5423 | |||
5424 | if (status != GOT_STATUS_ADD'A' && staged_status != GOT_STATUS_ADD'A') { | ||
5425 | /* Trivial case: base commit == head commit */ | ||
5426 | if (got_object_id_cmp(base_commit_id, head_commit_id) == 0) | ||
5427 | return NULL((void*)0); | ||
5428 | /* | ||
5429 | * Ensure file content which local changes were based | ||
5430 | * on matches file content in the branch head. | ||
5431 | */ | ||
5432 | err = got_object_id_by_path(&id, repo, head_commit_id, | ||
5433 | in_repo_path); | ||
5434 | if (err) { | ||
5435 | if (err->code == GOT_ERR_NO_TREE_ENTRY50) | ||
5436 | err = got_error(ood_errcode); | ||
5437 | goto done; | ||
5438 | } else if (got_object_id_cmp(id, base_blob_id) != 0) | ||
5439 | err = got_error(ood_errcode); | ||
5440 | } else { | ||
5441 | /* Require that added files don't exist in the branch head. */ | ||
5442 | err = got_object_id_by_path(&id, repo, head_commit_id, | ||
5443 | in_repo_path); | ||
5444 | if (err && err->code != GOT_ERR_NO_TREE_ENTRY50) | ||
5445 | goto done; | ||
5446 | err = id ? got_error(ood_errcode) : NULL((void*)0); | ||
5447 | } | ||
5448 | done: | ||
5449 | free(id); | ||
5450 | return err; | ||
5451 | } | ||
5452 | |||
5453 | const struct got_error * | ||
5454 | commit_worktree(struct got_object_id **new_commit_id, | ||
5455 | struct got_pathlist_head *commitable_paths, | ||
5456 | struct got_object_id *head_commit_id, struct got_worktree *worktree, | ||
5457 | const char *author, const char *committer, | ||
5458 | got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg, | ||
5459 | got_worktree_status_cb status_cb, void *status_arg, | ||
5460 | struct got_repository *repo) | ||
5461 | { | ||
5462 | const struct got_error *err = NULL((void*)0), *unlockerr = NULL((void*)0); | ||
5463 | struct got_pathlist_entry *pe; | ||
5464 | const char *head_ref_name = NULL((void*)0); | ||
5465 | struct got_commit_object *head_commit = NULL((void*)0); | ||
5466 | struct got_reference *head_ref2 = NULL((void*)0); | ||
5467 | struct got_object_id *head_commit_id2 = NULL((void*)0); | ||
5468 | struct got_tree_object *head_tree = NULL((void*)0); | ||
5469 | struct got_object_id *new_tree_id = NULL((void*)0); | ||
5470 | int nentries; | ||
5471 | struct got_object_id_queue parent_ids; | ||
5472 | struct got_object_qid *pid = NULL((void*)0); | ||
5473 | char *logmsg = NULL((void*)0); | ||
5474 | |||
5475 | *new_commit_id = NULL((void*)0); | ||
5476 | |||
5477 | SIMPLEQ_INIT(&parent_ids)do { (&parent_ids)->sqh_first = ((void*)0); (&parent_ids )->sqh_last = &(&parent_ids)->sqh_first; } while (0); | ||
5478 | |||
5479 | err = got_object_open_as_commit(&head_commit, repo, head_commit_id); | ||
5480 | if (err) | ||
5481 | goto done; | ||
5482 | |||
5483 | err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id); | ||
5484 | if (err) | ||
5485 | goto done; | ||
5486 | |||
5487 | if (commit_msg_cb != NULL((void*)0)) { | ||
5488 | err = commit_msg_cb(commitable_paths, &logmsg, commit_arg); | ||
5489 | if (err) | ||
5490 | goto done; | ||
5491 | } | ||
5492 | |||
5493 | if (logmsg == NULL((void*)0) || strlen(logmsg) == 0) { | ||
5494 | err = got_error(GOT_ERR_COMMIT_MSG_EMPTY74); | ||
5495 | goto done; | ||
5496 | } | ||
5497 | |||
5498 | /* Create blobs from added and modified files and record their IDs. */ | ||
5499 | TAILQ_FOREACH(pe, commitable_paths, entry)for((pe) = ((commitable_paths)->tqh_first); (pe) != ((void *)0); (pe) = ((pe)->entry.tqe_next)) { | ||
5500 | struct got_commitable *ct = pe->data; | ||
5501 | char *ondisk_path; | ||
5502 | |||
5503 | /* Blobs for staged files already exist. */ | ||
5504 | if (ct->staged_status == GOT_STATUS_ADD'A' || | ||
5505 | ct->staged_status == GOT_STATUS_MODIFY'M') | ||
5506 | continue; | ||
5507 | |||
5508 | if (ct->status != GOT_STATUS_ADD'A' && | ||
5509 | ct->status != GOT_STATUS_MODIFY'M' && | ||
5510 | ct->status != GOT_STATUS_MODE_CHANGE'm') | ||
5511 | continue; | ||
5512 | |||
5513 | if (asprintf(&ondisk_path, "%s/%s", | ||
5514 | worktree->root_path, pe->path) == -1) { | ||
5515 | err = got_error_from_errno("asprintf"); | ||
5516 | goto done; | ||
5517 | } | ||
5518 | err = got_object_blob_create(&ct->blob_id, ondisk_path, repo); | ||
5519 | free(ondisk_path); | ||
5520 | if (err) | ||
5521 | goto done; | ||
5522 | } | ||
5523 | |||
5524 | /* Recursively write new tree objects. */ | ||
5525 | err = write_tree(&new_tree_id, &nentries, head_tree, "/", | ||
5526 | commitable_paths, status_cb, status_arg, repo); | ||
5527 | if (err) | ||
5528 | goto done; | ||
5529 | |||
5530 | err = got_object_qid_alloc(&pid, worktree->base_commit_id); | ||
5531 | if (err) | ||
5532 | goto done; | ||
5533 | SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry)do { (pid)->entry.sqe_next = ((void*)0); *(&parent_ids )->sqh_last = (pid); (&parent_ids)->sqh_last = & (pid)->entry.sqe_next; } while (0); | ||
5534 | err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids, | ||
5535 | 1, author, time(NULL((void*)0)), committer, time(NULL((void*)0)), logmsg, repo); | ||
5536 | got_object_qid_free(pid); | ||
5537 | if (logmsg != NULL((void*)0)) | ||
5538 | free(logmsg); | ||
5539 | if (err) | ||
5540 | goto done; | ||
5541 | |||
5542 | /* Check if a concurrent commit to our branch has occurred. */ | ||
5543 | head_ref_name = got_worktree_get_head_ref_name(worktree); | ||
5544 | if (head_ref_name == NULL((void*)0)) { | ||
5545 | err = got_error_from_errno("got_worktree_get_head_ref_name"); | ||
5546 | goto done; | ||
5547 | } | ||
5548 | /* Lock the reference here to prevent concurrent modification. */ | ||
5549 | err = got_ref_open(&head_ref2, repo, head_ref_name, 1); | ||
5550 | if (err) | ||
5551 | goto done; | ||
5552 | err = got_ref_resolve(&head_commit_id2, repo, head_ref2); | ||
5553 | if (err) | ||
5554 | goto done; | ||
5555 | if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) { | ||
5556 | err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED72); | ||
5557 | goto done; | ||
5558 | } | ||
5559 | /* Update branch head in repository. */ | ||
5560 | err = got_ref_change_ref(head_ref2, *new_commit_id); | ||
5561 | if (err) | ||
5562 | goto done; | ||
5563 | err = got_ref_write(head_ref2, repo); | ||
5564 | if (err) | ||
5565 | goto done; | ||
5566 | |||
5567 | err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id); | ||
5568 | if (err) | ||
5569 | goto done; | ||
5570 | |||
5571 | err = ref_base_commit(worktree, repo); | ||
5572 | if (err) | ||
5573 | goto done; | ||
5574 | done: | ||
5575 | if (head_tree) | ||
5576 | got_object_tree_close(head_tree); | ||
5577 | if (head_commit) | ||
5578 | got_object_commit_close(head_commit); | ||
5579 | free(head_commit_id2); | ||
5580 | if (head_ref2) { | ||
5581 | unlockerr = got_ref_unlock(head_ref2); | ||
5582 | if (unlockerr && err == NULL((void*)0)) | ||
5583 | err = unlockerr; | ||
5584 | got_ref_close(head_ref2); | ||
5585 | } | ||
5586 | return err; | ||
5587 | } | ||
5588 | |||
5589 | static const struct got_error * | ||
5590 | check_path_is_commitable(const char *path, | ||
5591 | struct got_pathlist_head *commitable_paths) | ||
5592 | { | ||
5593 | struct got_pathlist_entry *cpe = NULL((void*)0); | ||
5594 | size_t path_len = strlen(path); | ||
5595 | |||
5596 | TAILQ_FOREACH(cpe, commitable_paths, entry)for((cpe) = ((commitable_paths)->tqh_first); (cpe) != ((void *)0); (cpe) = ((cpe)->entry.tqe_next)) { | ||
5597 | struct got_commitable *ct = cpe->data; | ||
5598 | const char *ct_path = ct->path; | ||
5599 | |||
5600 | while (ct_path[0] == '/') | ||
5601 | ct_path++; | ||
5602 | |||
5603 | if (strcmp(path, ct_path) == 0 || | ||
5604 | got_path_is_child(ct_path, path, path_len)) | ||
5605 | break; | ||
5606 | } | ||
5607 | |||
5608 | if (cpe == NULL((void*)0)) | ||
5609 | return got_error_path(path, GOT_ERR_BAD_PATH4); | ||
5610 | |||
5611 | return NULL((void*)0); | ||
5612 | } | ||
5613 | |||
5614 | static const struct got_error * | ||
5615 | check_staged_file(void *arg, struct got_fileindex_entry *ie) | ||
5616 | { | ||
5617 | int *have_staged_files = arg; | ||
5618 | |||
5619 | if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE0) { | ||
5620 | *have_staged_files = 1; | ||
5621 | return got_error(GOT_ERR_CANCELLED49); | ||
5622 | } | ||
5623 | |||
5624 | return NULL((void*)0); | ||
5625 | } | ||
5626 | |||
5627 | static const struct got_error * | ||
5628 | check_non_staged_files(struct got_fileindex *fileindex, | ||
5629 | struct got_pathlist_head *paths) | ||
5630 | { | ||
5631 | struct got_pathlist_entry *pe; | ||
5632 | struct got_fileindex_entry *ie; | ||
5633 | |||
5634 | TAILQ_FOREACH(pe, paths, entry)for((pe) = ((paths)->tqh_first); (pe) != ((void*)0); (pe) = ((pe)->entry.tqe_next)) { | ||
5635 | if (pe->path[0] == '\0') | ||
5636 | continue; | ||
5637 | ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len); | ||
5638 | if (ie == NULL((void*)0)) | ||
5639 | return got_error_path(pe->path, GOT_ERR_BAD_PATH4); | ||
5640 | if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE0) | ||
5641 | return got_error_path(pe->path, | ||
5642 | GOT_ERR_FILE_NOT_STAGED105); | ||
5643 | } | ||
5644 | |||
5645 | return NULL((void*)0); | ||
5646 | } | ||
5647 | |||
5648 | const struct got_error * | ||
5649 | got_worktree_commit(struct got_object_id **new_commit_id, | ||
5650 | struct got_worktree *worktree, struct got_pathlist_head *paths, | ||
5651 | const char *author, const char *committer, int allow_bad_symlinks, | ||
5652 | got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg, | ||
5653 | got_worktree_status_cb status_cb, void *status_arg, | ||
5654 | struct got_repository *repo) | ||
5655 | { | ||
5656 | const struct got_error *err = NULL((void*)0), *unlockerr = NULL((void*)0), *sync_err; | ||
5657 | struct got_fileindex *fileindex = NULL((void*)0); | ||
5658 | char *fileindex_path = NULL((void*)0); | ||
5659 | struct got_pathlist_head commitable_paths; | ||
5660 | struct collect_commitables_arg cc_arg; | ||
5661 | struct got_pathlist_entry *pe; | ||
5662 | struct got_reference *head_ref = NULL((void*)0); | ||
5663 | struct got_object_id *head_commit_id = NULL((void*)0); | ||
5664 | int have_staged_files = 0; | ||
5665 | |||
5666 | *new_commit_id = NULL((void*)0); | ||
5667 | |||
5668 | TAILQ_INIT(&commitable_paths)do { (&commitable_paths)->tqh_first = ((void*)0); (& commitable_paths)->tqh_last = &(&commitable_paths) ->tqh_first; } while (0); | ||
5669 | |||
5670 | err = lock_worktree(worktree, LOCK_EX0x02); | ||
5671 | if (err) | ||
5672 | goto done; | ||
5673 | |||
5674 | err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0); | ||
5675 | if (err) | ||
5676 | goto done; | ||
5677 | |||
5678 | err = got_ref_resolve(&head_commit_id, repo, head_ref); | ||
5679 | if (err) | ||
5680 | goto done; | ||
5681 | |||
5682 | err = open_fileindex(&fileindex, &fileindex_path, worktree); | ||
5683 | if (err) | ||
5684 | goto done; | ||
5685 | |||
5686 | err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file, | ||
5687 | &have_staged_files); | ||
5688 | if (err && err->code != GOT_ERR_CANCELLED49) | ||
5689 | goto done; | ||
5690 | if (have_staged_files) { | ||
5691 | err = check_non_staged_files(fileindex, paths); | ||
5692 | if (err) | ||
5693 | goto done; | ||
5694 | } | ||
5695 | |||
5696 | cc_arg.commitable_paths = &commitable_paths; | ||
5697 | cc_arg.worktree = worktree; | ||
5698 | cc_arg.fileindex = fileindex; | ||
5699 | cc_arg.repo = repo; | ||
5700 | cc_arg.have_staged_files = have_staged_files; | ||
5701 | cc_arg.allow_bad_symlinks = allow_bad_symlinks; | ||
5702 | TAILQ_FOREACH(pe, paths, entry)for((pe) = ((paths)->tqh_first); (pe) != ((void*)0); (pe) = ((pe)->entry.tqe_next)) { | ||
5703 | err = worktree_status(worktree, pe->path, fileindex, repo, | ||
5704 | collect_commitables, &cc_arg, NULL((void*)0), NULL((void*)0), 0, 0); | ||
5705 | if (err) | ||
5706 | goto done; | ||
5707 | } | ||
5708 | |||
5709 | if (TAILQ_EMPTY(&commitable_paths)(((&commitable_paths)->tqh_first) == ((void*)0))) { | ||
5710 | err = got_error(GOT_ERR_COMMIT_NO_CHANGES76); | ||
5711 | goto done; | ||
5712 | } | ||
5713 | |||
5714 | TAILQ_FOREACH(pe, paths, entry)for((pe) = ((paths)->tqh_first); (pe) != ((void*)0); (pe) = ((pe)->entry.tqe_next)) { | ||
5715 | err = check_path_is_commitable(pe->path, &commitable_paths); | ||
5716 | if (err) | ||
5717 | goto done; | ||
5718 | } | ||
5719 | |||
5720 | TAILQ_FOREACH(pe, &commitable_paths, entry)for((pe) = ((&commitable_paths)->tqh_first); (pe) != ( (void*)0); (pe) = ((pe)->entry.tqe_next)) { | ||
5721 | struct got_commitable *ct = pe->data; | ||
5722 | const char *ct_path = ct->in_repo_path; | ||
5723 | |||
5724 | while (ct_path[0] == '/') | ||
5725 | ct_path++; | ||
5726 | err = check_out_of_date(ct_path, ct->status, | ||
5727 | ct->staged_status, ct->base_blob_id, ct->base_commit_id, | ||
5728 | head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE73); | ||
5729 | if (err) | ||
5730 | goto done; | ||
5731 | |||
5732 | } | ||
5733 | |||
5734 | err = commit_worktree(new_commit_id, &commitable_paths, | ||
5735 | head_commit_id, worktree, author, committer, | ||
5736 | commit_msg_cb, commit_arg, status_cb, status_arg, repo); | ||
5737 | if (err) | ||
5738 | goto done; | ||
5739 | |||
5740 | err = update_fileindex_after_commit(worktree, &commitable_paths, | ||
5741 | *new_commit_id, fileindex, have_staged_files); | ||
5742 | sync_err = sync_fileindex(fileindex, fileindex_path); | ||
5743 | if (sync_err && err == NULL((void*)0)) | ||
5744 | err = sync_err; | ||
5745 | done: | ||
5746 | if (fileindex) | ||
5747 | got_fileindex_free(fileindex); | ||
5748 | free(fileindex_path); | ||
5749 | unlockerr = lock_worktree(worktree, LOCK_SH0x01); | ||
5750 | if (unlockerr && err == NULL((void*)0)) | ||
5751 | err = unlockerr; | ||
5752 | TAILQ_FOREACH(pe, &commitable_paths, entry)for((pe) = ((&commitable_paths)->tqh_first); (pe) != ( (void*)0); (pe) = ((pe)->entry.tqe_next)) { | ||
5753 | struct got_commitable *ct = pe->data; | ||
5754 | free_commitable(ct); | ||
5755 | } | ||
5756 | got_pathlist_free(&commitable_paths); | ||
5757 | return err; | ||
5758 | } | ||
5759 | |||
5760 | const char * | ||
5761 | got_commitable_get_path(struct got_commitable *ct) | ||
5762 | { | ||
5763 | return ct->path; | ||
5764 | } | ||
5765 | |||
5766 | unsigned int | ||
5767 | got_commitable_get_status(struct got_commitable *ct) | ||
5768 | { | ||
5769 | return ct->status; | ||
5770 | } | ||
5771 | |||
5772 | struct check_rebase_ok_arg { | ||
5773 | struct got_worktree *worktree; | ||
5774 | struct got_repository *repo; | ||
5775 | }; | ||
5776 | |||
5777 | static const struct got_error * | ||
5778 | check_rebase_ok(void *arg, struct got_fileindex_entry *ie) | ||
5779 | { | ||
5780 | const struct got_error *err = NULL((void*)0); | ||
5781 | struct check_rebase_ok_arg *a = arg; | ||
5782 | unsigned char status; | ||
5783 | struct stat sb; | ||
5784 | char *ondisk_path; | ||
5785 | |||
5786 | /* Reject rebase of a work tree with mixed base commits. */ | ||
5787 | if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1, | ||
5788 | SHA1_DIGEST_LENGTH20)) | ||
5789 | return got_error(GOT_ERR_MIXED_COMMITS81); | ||
5790 | |||
5791 | if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path) | ||
5792 | == -1) | ||
5793 | return got_error_from_errno("asprintf"); | ||
5794 | |||
5795 | /* Reject rebase of a work tree with modified or staged files. */ | ||
5796 | err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL((void*)0), a->repo); | ||
5797 | free(ondisk_path); | ||
5798 | if (err) | ||
5799 | return err; | ||
5800 | |||
5801 | if (status != GOT_STATUS_NO_CHANGE' ') | ||
5802 | return got_error(GOT_ERR_MODIFIED84); | ||
5803 | if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE' ') | ||
5804 | return got_error_path(ie->path, GOT_ERR_FILE_STAGED101); | ||
5805 | |||
5806 | return NULL((void*)0); | ||
5807 | } | ||
5808 | |||
5809 | const struct got_error * | ||
5810 | got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref, | ||
5811 | struct got_reference **tmp_branch, struct got_fileindex **fileindex, | ||
5812 | struct got_worktree *worktree, struct got_reference *branch, | ||
5813 | struct got_repository *repo) | ||
5814 | { | ||
5815 | const struct got_error *err = NULL((void*)0); | ||
5816 | char *tmp_branch_name = NULL((void*)0), *new_base_branch_ref_name = NULL((void*)0); | ||
5817 | char *branch_ref_name = NULL((void*)0); | ||
5818 | char *fileindex_path = NULL((void*)0); | ||
5819 | struct check_rebase_ok_arg ok_arg; | ||
5820 | struct got_reference *wt_branch = NULL((void*)0), *branch_ref = NULL((void*)0); | ||
5821 | struct got_object_id *wt_branch_tip = NULL((void*)0); | ||
5822 | |||
5823 | *new_base_branch_ref = NULL((void*)0); | ||
5824 | *tmp_branch = NULL((void*)0); | ||
5825 | *fileindex = NULL((void*)0); | ||
5826 | |||
5827 | err = lock_worktree(worktree, LOCK_EX0x02); | ||
5828 | if (err) | ||
5829 | return err; | ||
5830 | |||
5831 | err = open_fileindex(fileindex, &fileindex_path, worktree); | ||
5832 | if (err) | ||
5833 | goto done; | ||
5834 | |||
5835 | ok_arg.worktree = worktree; | ||
5836 | ok_arg.repo = repo; | ||
5837 | err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok, | ||
5838 | &ok_arg); | ||
5839 | if (err) | ||
5840 | goto done; | ||
5841 | |||
5842 | err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree); | ||
5843 | if (err) | ||
5844 | goto done; | ||
5845 | |||
5846 | err = get_newbase_symref_name(&new_base_branch_ref_name, worktree); | ||
5847 | if (err) | ||
5848 | goto done; | ||
5849 | |||
5850 | err = get_rebase_branch_symref_name(&branch_ref_name, worktree); | ||
5851 | if (err) | ||
5852 | goto done; | ||
5853 | |||
5854 | err = got_ref_open(&wt_branch, repo, worktree->head_ref_name, | ||
5855 | 0); | ||
5856 | if (err) | ||
5857 | goto done; | ||
5858 | |||
5859 | err = got_ref_resolve(&wt_branch_tip, repo, wt_branch); | ||
5860 | if (err) | ||
5861 | goto done; | ||
5862 | if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) { | ||
5863 | err = got_error(GOT_ERR_REBASE_OUT_OF_DATE115); | ||
5864 | goto done; | ||
5865 | } | ||
5866 | |||
5867 | err = got_ref_alloc_symref(new_base_branch_ref, | ||
5868 | new_base_branch_ref_name, wt_branch); | ||
5869 | if (err) | ||
5870 | goto done; | ||
5871 | err = got_ref_write(*new_base_branch_ref, repo); | ||
5872 | if (err) | ||
5873 | goto done; | ||
5874 | |||
5875 | /* TODO Lock original branch's ref while rebasing? */ | ||
5876 | |||
5877 | err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch); | ||
5878 | if (err) | ||
5879 | goto done; | ||
5880 | |||
5881 | err = got_ref_write(branch_ref, repo); | ||
5882 | if (err) | ||
5883 | goto done; | ||
5884 | |||
5885 | err = got_ref_alloc(tmp_branch, tmp_branch_name, | ||
5886 | worktree->base_commit_id); | ||
5887 | if (err) | ||
5888 | goto done; | ||
5889 | err = got_ref_write(*tmp_branch, repo); | ||
5890 | if (err) | ||
5891 | goto done; | ||
5892 | |||
5893 | err = got_worktree_set_head_ref(worktree, *tmp_branch); | ||
5894 | if (err) | ||
5895 | goto done; | ||
5896 | done: | ||
5897 | free(fileindex_path); | ||
5898 | free(tmp_branch_name); | ||
5899 | free(new_base_branch_ref_name); | ||
5900 | free(branch_ref_name); | ||
5901 | if (branch_ref) | ||
5902 | got_ref_close(branch_ref); | ||
5903 | if (wt_branch) | ||
5904 | got_ref_close(wt_branch); | ||
5905 | free(wt_branch_tip); | ||
5906 | if (err) { | ||
5907 | if (*new_base_branch_ref) { | ||
5908 | got_ref_close(*new_base_branch_ref); | ||
5909 | *new_base_branch_ref = NULL((void*)0); | ||
5910 | } | ||
5911 | if (*tmp_branch) { | ||
5912 | got_ref_close(*tmp_branch); | ||
5913 | *tmp_branch = NULL((void*)0); | ||
5914 | } | ||
5915 | if (*fileindex) { | ||
5916 | got_fileindex_free(*fileindex); | ||
5917 | *fileindex = NULL((void*)0); | ||
5918 | } | ||
5919 | lock_worktree(worktree, LOCK_SH0x01); | ||
5920 | } | ||
5921 | return err; | ||
5922 | } | ||
5923 | |||
5924 | const struct got_error * | ||
5925 | got_worktree_rebase_continue(struct got_object_id **commit_id, | ||
5926 | struct got_reference **new_base_branch, struct got_reference **tmp_branch, | ||
5927 | struct got_reference **branch, struct got_fileindex **fileindex, | ||
5928 | struct got_worktree *worktree, struct got_repository *repo) | ||
5929 | { | ||
5930 | const struct got_error *err; | ||
5931 | char *commit_ref_name = NULL((void*)0), *new_base_branch_ref_name = NULL((void*)0); | ||
5932 | char *tmp_branch_name = NULL((void*)0), *branch_ref_name = NULL((void*)0); | ||
5933 | struct got_reference *commit_ref = NULL((void*)0), *branch_ref = NULL((void*)0); | ||
5934 | char *fileindex_path = NULL((void*)0); | ||
5935 | int have_staged_files = 0; | ||
5936 | |||
5937 | *commit_id = NULL((void*)0); | ||
5938 | *new_base_branch = NULL((void*)0); | ||
5939 | *tmp_branch = NULL((void*)0); | ||
5940 | *branch = NULL((void*)0); | ||
5941 | *fileindex = NULL((void*)0); | ||
5942 | |||
5943 | err = lock_worktree(worktree, LOCK_EX0x02); | ||
5944 | if (err) | ||
5945 | return err; | ||
5946 | |||
5947 | err = open_fileindex(fileindex, &fileindex_path, worktree); | ||
5948 | if (err) | ||
5949 | goto done; | ||
5950 | |||
5951 | err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file, | ||
5952 | &have_staged_files); | ||
5953 | if (err && err->code != GOT_ERR_CANCELLED49) | ||
5954 | goto done; | ||
5955 | if (have_staged_files) { | ||
5956 | err = got_error(GOT_ERR_STAGED_PATHS106); | ||
5957 | goto done; | ||
5958 | } | ||
5959 | |||
5960 | err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree); | ||
5961 | if (err) | ||
5962 | goto done; | ||
5963 | |||
5964 | err = get_rebase_branch_symref_name(&branch_ref_name, worktree); | ||
5965 | if (err) | ||
5966 | goto done; | ||
5967 | |||
5968 | err = get_rebase_commit_ref_name(&commit_ref_name, worktree); | ||
5969 | if (err) | ||
5970 | goto done; | ||
5971 | |||
5972 | err = get_newbase_symref_name(&new_base_branch_ref_name, worktree); | ||
5973 | if (err) | ||
5974 | goto done; | ||
5975 | |||
5976 | err = got_ref_open(&branch_ref, repo, branch_ref_name, 0); | ||
5977 | if (err) | ||
5978 | goto done; | ||
5979 | |||
5980 | err = got_ref_open(branch, repo, | ||
5981 | got_ref_get_symref_target(branch_ref), 0); | ||
5982 | if (err) | ||
5983 | goto done; | ||
5984 | |||
5985 | err = got_ref_open(&commit_ref, repo, commit_ref_name, 0); | ||
5986 | if (err) | ||
5987 | goto done; | ||
5988 | |||
5989 | err = got_ref_resolve(commit_id, repo, commit_ref); | ||
5990 | if (err) | ||
5991 | goto done; | ||
5992 | |||
5993 | err = got_ref_open(new_base_branch, repo, | ||
5994 | new_base_branch_ref_name, 0); | ||
5995 | if (err) | ||
5996 | goto done; | ||
5997 | |||
5998 | err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0); | ||
5999 | if (err) | ||
6000 | goto done; | ||
6001 | done: | ||
6002 | free(commit_ref_name); | ||
6003 | free(branch_ref_name); | ||
6004 | free(fileindex_path); | ||
6005 | if (commit_ref) | ||
6006 | got_ref_close(commit_ref); | ||
6007 | if (branch_ref) | ||
6008 | got_ref_close(branch_ref); | ||
6009 | if (err) { | ||
6010 | free(*commit_id); | ||
6011 | *commit_id = NULL((void*)0); | ||
6012 | if (*tmp_branch) { | ||
6013 | got_ref_close(*tmp_branch); | ||
6014 | *tmp_branch = NULL((void*)0); | ||
6015 | } | ||
6016 | if (*new_base_branch) { | ||
6017 | got_ref_close(*new_base_branch); | ||
6018 | *new_base_branch = NULL((void*)0); | ||
6019 | } | ||
6020 | if (*branch) { | ||
6021 | got_ref_close(*branch); | ||
6022 | *branch = NULL((void*)0); | ||
6023 | } | ||
6024 | if (*fileindex) { | ||
6025 | got_fileindex_free(*fileindex); | ||
6026 | *fileindex = NULL((void*)0); | ||
6027 | } | ||
6028 | lock_worktree(worktree, LOCK_SH0x01); | ||
6029 | } | ||
6030 | return err; | ||
6031 | } | ||
6032 | |||
6033 | const struct got_error * | ||
6034 | got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree) | ||
6035 | { | ||
6036 | const struct got_error *err; | ||
6037 | char *tmp_branch_name = NULL((void*)0); | ||
6038 | |||
6039 | err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree); | ||
6040 | if (err) | ||
6041 | return err; | ||
6042 | |||
6043 | *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0); | ||
6044 | free(tmp_branch_name); | ||
6045 | return NULL((void*)0); | ||
6046 | } | ||
6047 | |||
6048 | static const struct got_error * | ||
6049 | collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths, | ||
6050 | char **logmsg, void *arg) | ||
6051 | { | ||
6052 | *logmsg = arg; | ||
6053 | return NULL((void*)0); | ||
6054 | } | ||
6055 | |||
6056 | static const struct got_error * | ||
6057 | rebase_status(void *arg, unsigned char status, unsigned char staged_status, | ||
6058 | const char *path, struct got_object_id *blob_id, | ||
6059 | struct got_object_id *staged_blob_id, struct got_object_id *commit_id, | ||
6060 | int dirfd, const char *de_name) | ||
6061 | { | ||
6062 | return NULL((void*)0); | ||
6063 | } | ||
6064 | |||
6065 | struct collect_merged_paths_arg { | ||
6066 | got_worktree_checkout_cb progress_cb; | ||
6067 | void *progress_arg; | ||
6068 | struct got_pathlist_head *merged_paths; | ||
6069 | }; | ||
6070 | |||
6071 | static const struct got_error * | ||
6072 | collect_merged_paths(void *arg, unsigned char status, const char *path) | ||
6073 | { | ||
6074 | const struct got_error *err; | ||
6075 | struct collect_merged_paths_arg *a = arg; | ||
6076 | char *p; | ||
6077 | struct got_pathlist_entry *new; | ||
6078 | |||
6079 | err = (*a->progress_cb)(a->progress_arg, status, path); | ||
6080 | if (err) | ||
6081 | return err; | ||
6082 | |||
6083 | if (status != GOT_STATUS_MERGE'G' && | ||
6084 | status != GOT_STATUS_ADD'A' && | ||
6085 | status != GOT_STATUS_DELETE'D' && | ||
6086 | status != GOT_STATUS_CONFLICT'C') | ||
6087 | return NULL((void*)0); | ||
6088 | |||
6089 | p = strdup(path); | ||
6090 | if (p == NULL((void*)0)) | ||
6091 | return got_error_from_errno("strdup"); | ||
6092 | |||
6093 | err = got_pathlist_insert(&new, a->merged_paths, p, NULL((void*)0)); | ||
6094 | if (err || new == NULL((void*)0)) | ||
6095 | free(p); | ||