2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 86c3caaf 2018-03-09 stsp * Permission to use, copy, modify, and distribute this software for any
5 86c3caaf 2018-03-09 stsp * purpose with or without fee is hereby granted, provided that the above
6 86c3caaf 2018-03-09 stsp * copyright notice and this permission notice appear in all copies.
8 86c3caaf 2018-03-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 86c3caaf 2018-03-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 86c3caaf 2018-03-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 86c3caaf 2018-03-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 86c3caaf 2018-03-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 86c3caaf 2018-03-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 86c3caaf 2018-03-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 86c3caaf 2018-03-09 stsp #include <sys/stat.h>
18 6d9d28c3 2018-03-11 stsp #include <sys/limits.h>
19 9d31a1d8 2018-03-11 stsp #include <sys/queue.h>
20 133d2798 2019-01-08 stsp #include <sys/tree.h>
22 d1f6d47b 2019-02-04 stsp #include <dirent.h>
23 f5c49f82 2019-01-06 stsp #include <stddef.h>
24 86c3caaf 2018-03-09 stsp #include <string.h>
25 86c3caaf 2018-03-09 stsp #include <stdio.h>
26 86c3caaf 2018-03-09 stsp #include <stdlib.h>
27 86c3caaf 2018-03-09 stsp #include <fcntl.h>
28 86c3caaf 2018-03-09 stsp #include <errno.h>
29 86c3caaf 2018-03-09 stsp #include <unistd.h>
30 9d31a1d8 2018-03-11 stsp #include <sha1.h>
31 9d31a1d8 2018-03-11 stsp #include <zlib.h>
32 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
33 512f0d0e 2019-01-02 stsp #include <libgen.h>
34 ec22038e 2019-03-10 stsp #include <uuid.h>
36 86c3caaf 2018-03-09 stsp #include "got_error.h"
37 86c3caaf 2018-03-09 stsp #include "got_repository.h"
38 5261c201 2018-04-01 stsp #include "got_reference.h"
39 9d31a1d8 2018-03-11 stsp #include "got_object.h"
40 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
41 511a516b 2018-05-19 stsp #include "got_opentemp.h"
43 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
44 718b3ab0 2018-03-17 stsp #include "got_lib_path.h"
45 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
46 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
47 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
48 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
49 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
50 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
53 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
56 99724ed4 2018-03-10 stsp static const struct got_error *
57 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
59 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
61 99724ed4 2018-03-10 stsp int fd = -1;
63 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
64 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
65 99724ed4 2018-03-10 stsp path = NULL;
69 73a5ef67 2018-03-11 stsp fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
70 99724ed4 2018-03-10 stsp GOT_DEFAULT_FILE_MODE);
71 99724ed4 2018-03-10 stsp if (fd == -1) {
72 99724ed4 2018-03-10 stsp err = got_error_from_errno();
76 99724ed4 2018-03-10 stsp if (content) {
77 99724ed4 2018-03-10 stsp int len = dprintf(fd, "%s\n", content);
78 99724ed4 2018-03-10 stsp if (len != strlen(content) + 1) {
79 99724ed4 2018-03-10 stsp err = got_error_from_errno();
85 99724ed4 2018-03-10 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
86 99724ed4 2018-03-10 stsp err = got_error_from_errno();
91 507dc3bb 2018-12-29 stsp static const struct got_error *
92 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
94 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
95 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
96 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
97 507dc3bb 2018-12-29 stsp char *path = NULL;
99 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
100 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
101 507dc3bb 2018-12-29 stsp path = NULL;
105 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
109 507dc3bb 2018-12-29 stsp if (content) {
110 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
111 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
112 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
117 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
118 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
119 2a57020b 2019-02-20 stsp unlink(tmppath);
124 507dc3bb 2018-12-29 stsp free(tmppath);
125 fb43ecf1 2019-02-11 stsp if (fclose(tmpfile) != 0 && err == NULL)
126 fb43ecf1 2019-02-11 stsp err = got_error_from_errno();
127 99724ed4 2018-03-10 stsp return err;
130 09fe317a 2018-03-11 stsp static const struct got_error *
131 7ac97322 2018-03-11 stsp read_meta_file(char **content, const char *path_got, const char *name)
133 09fe317a 2018-03-11 stsp const struct got_error *err = NULL;
134 09fe317a 2018-03-11 stsp char *path;
135 09fe317a 2018-03-11 stsp int fd = -1;
137 09fe317a 2018-03-11 stsp struct stat sb;
139 09fe317a 2018-03-11 stsp *content = NULL;
141 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
142 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
143 09fe317a 2018-03-11 stsp path = NULL;
147 ef99fdb1 2018-03-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
148 09fe317a 2018-03-11 stsp if (fd == -1) {
149 f02eaa22 2019-03-11 stsp if (errno == ENOENT)
150 f02eaa22 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
152 f02eaa22 2019-03-11 stsp err = got_error_from_errno();
155 ef99fdb1 2018-03-11 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
156 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
157 73a5ef67 2018-03-11 stsp : got_error_from_errno());
161 d10c9b58 2019-02-19 stsp if (lstat(path, &sb) != 0) {
162 d10c9b58 2019-02-19 stsp err = got_error_from_errno();
165 09fe317a 2018-03-11 stsp *content = calloc(1, sb.st_size);
166 09fe317a 2018-03-11 stsp if (*content == NULL) {
167 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
171 09fe317a 2018-03-11 stsp n = read(fd, *content, sb.st_size);
172 09fe317a 2018-03-11 stsp if (n != sb.st_size) {
173 0605801d 2018-03-11 stsp err = (n == -1 ? got_error_from_errno() :
174 0605801d 2018-03-11 stsp got_error(GOT_ERR_WORKTREE_META));
177 09fe317a 2018-03-11 stsp if ((*content)[sb.st_size - 1] != '\n') {
178 09fe317a 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
181 09fe317a 2018-03-11 stsp (*content)[sb.st_size - 1] = '\0';
184 09fe317a 2018-03-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
185 09fe317a 2018-03-11 stsp err = got_error_from_errno();
186 09fe317a 2018-03-11 stsp free(path);
188 09fe317a 2018-03-11 stsp free(*content);
189 09fe317a 2018-03-11 stsp *content = NULL;
191 09fe317a 2018-03-11 stsp return err;
194 86c3caaf 2018-03-09 stsp const struct got_error *
195 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
196 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
198 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
199 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
200 ec22038e 2019-03-10 stsp uuid_t uuid;
201 ec22038e 2019-03-10 stsp uint32_t uuid_status;
202 65596e15 2018-12-24 stsp int obj_type;
203 7ac97322 2018-03-11 stsp char *path_got = NULL;
204 08d425ea 2018-12-24 stsp char *refstr = NULL;
205 1451e70d 2018-03-10 stsp char *formatstr = NULL;
206 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
207 65596e15 2018-12-24 stsp char *basestr = NULL;
208 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
210 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
211 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
215 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
217 65596e15 2018-12-24 stsp return err;
218 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
220 65596e15 2018-12-24 stsp return err;
221 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
222 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
224 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
225 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
226 0a585a0d 2018-03-17 stsp return got_error_from_errno();
229 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
230 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
231 86c3caaf 2018-03-09 stsp err = got_error_from_errno();
235 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
236 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
237 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
240 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
241 86c3caaf 2018-03-09 stsp err = got_error_from_errno();
245 056e7441 2018-03-11 stsp /* Create an empty lock file. */
246 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
250 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
251 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
255 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
256 08d425ea 2018-12-24 stsp refstr = got_ref_to_str(head_ref);
257 08d425ea 2018-12-24 stsp if (refstr == NULL) {
258 a1a7858a 2018-12-24 stsp err = got_error_from_errno();
261 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
265 65596e15 2018-12-24 stsp /* Record our base commit. */
266 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
269 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
273 1451e70d 2018-03-10 stsp /* Store path to repository. */
274 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
275 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
279 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
280 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
281 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
285 ec22038e 2019-03-10 stsp /* Generate UUID. */
286 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
287 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
288 ec22038e 2019-03-10 stsp err = got_error_uuid(uuid_status);
291 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
292 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
293 ec22038e 2019-03-10 stsp err = got_error_uuid(uuid_status);
296 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
300 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
301 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
302 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
305 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
310 65596e15 2018-12-24 stsp free(commit_id);
311 7ac97322 2018-03-11 stsp free(path_got);
312 1451e70d 2018-03-10 stsp free(formatstr);
313 08d425ea 2018-12-24 stsp free(refstr);
314 0bb8a95e 2018-03-12 stsp free(absprefix);
315 65596e15 2018-12-24 stsp free(basestr);
316 ec22038e 2019-03-10 stsp free(uuidstr);
317 86c3caaf 2018-03-09 stsp return err;
320 247140b2 2019-02-05 stsp static const struct got_error *
321 247140b2 2019-02-05 stsp open_worktree(struct got_worktree **worktree, const char *path)
323 6d9d28c3 2018-03-11 stsp const struct got_error *err = NULL;
324 7ac97322 2018-03-11 stsp char *path_got;
325 6d9d28c3 2018-03-11 stsp char *formatstr = NULL;
326 c442a90d 2019-03-10 stsp char *uuidstr = NULL;
327 056e7441 2018-03-11 stsp char *path_lock = NULL;
328 eaccb85f 2018-12-25 stsp char *base_commit_id_str = NULL;
329 271d2a38 2018-12-25 stsp char *head_ref_str = NULL;
330 6d9d28c3 2018-03-11 stsp int version, fd = -1;
331 6d9d28c3 2018-03-11 stsp const char *errstr;
332 eaccb85f 2018-12-25 stsp struct got_repository *repo = NULL;
333 c442a90d 2019-03-10 stsp uint32_t uuid_status;
335 6d9d28c3 2018-03-11 stsp *worktree = NULL;
337 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
338 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
339 7ac97322 2018-03-11 stsp path_got = NULL;
343 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
344 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
345 056e7441 2018-03-11 stsp path_lock = NULL;
349 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
350 6d9d28c3 2018-03-11 stsp if (fd == -1) {
351 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
352 73a5ef67 2018-03-11 stsp : got_error_from_errno());
356 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
360 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
361 6d9d28c3 2018-03-11 stsp if (errstr) {
362 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
365 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
366 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
370 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
371 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
372 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
375 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
377 0647c563 2019-03-11 stsp (*worktree)->root_path = strdup(path);
378 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
379 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
382 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
383 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
387 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
388 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
392 eaccb85f 2018-12-25 stsp err = read_meta_file(&base_commit_id_str, path_got,
393 0f92850e 2018-12-25 stsp GOT_WORKTREE_BASE_COMMIT);
397 c442a90d 2019-03-10 stsp err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
400 c442a90d 2019-03-10 stsp uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
401 c442a90d 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
402 c442a90d 2019-03-10 stsp err = got_error_uuid(uuid_status);
406 eaccb85f 2018-12-25 stsp err = got_repo_open(&repo, (*worktree)->repo_path);
410 eaccb85f 2018-12-25 stsp err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
411 eaccb85f 2018-12-25 stsp base_commit_id_str);
415 271d2a38 2018-12-25 stsp err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
419 271d2a38 2018-12-25 stsp err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
422 eaccb85f 2018-12-25 stsp got_repo_close(repo);
423 7ac97322 2018-03-11 stsp free(path_got);
424 056e7441 2018-03-11 stsp free(path_lock);
425 271d2a38 2018-12-25 stsp free(head_ref_str);
426 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
427 c442a90d 2019-03-10 stsp free(uuidstr);
428 bd165944 2019-03-10 stsp free(formatstr);
430 6d9d28c3 2018-03-11 stsp if (fd != -1)
432 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
433 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
434 6d9d28c3 2018-03-11 stsp *worktree = NULL;
436 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
438 6d9d28c3 2018-03-11 stsp return err;
441 247140b2 2019-02-05 stsp const struct got_error *
442 247140b2 2019-02-05 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
444 247140b2 2019-02-05 stsp const struct got_error *err = NULL;
447 247140b2 2019-02-05 stsp err = open_worktree(worktree, path);
448 f02eaa22 2019-03-11 stsp if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
449 247140b2 2019-02-05 stsp return err;
450 247140b2 2019-02-05 stsp if (*worktree)
451 247140b2 2019-02-05 stsp return NULL;
452 247140b2 2019-02-05 stsp path = dirname(path);
453 d1542a27 2019-02-05 stsp if (path == NULL)
454 d1542a27 2019-02-05 stsp return got_error_from_errno();
455 d1542a27 2019-02-05 stsp } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
457 247140b2 2019-02-05 stsp return got_error(GOT_ERR_NOT_WORKTREE);
460 3a6ce05a 2019-02-11 stsp const struct got_error *
461 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
463 3a6ce05a 2019-02-11 stsp const struct got_error *err = NULL;
464 c88eb298 2018-03-11 stsp free(worktree->root_path);
465 cde76477 2018-03-11 stsp free(worktree->repo_path);
466 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
467 eaccb85f 2018-12-25 stsp free(worktree->base_commit_id);
468 271d2a38 2018-12-25 stsp if (worktree->head_ref)
469 271d2a38 2018-12-25 stsp got_ref_close(worktree->head_ref);
470 056e7441 2018-03-11 stsp if (worktree->lockfd != -1)
471 3a6ce05a 2019-02-11 stsp if (close(worktree->lockfd) != 0)
472 3a6ce05a 2019-02-11 stsp err = got_error_from_errno();
473 6d9d28c3 2018-03-11 stsp free(worktree);
474 3a6ce05a 2019-02-11 stsp return err;
477 2fbdb5ae 2018-12-29 stsp const char *
478 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
480 c7f4312f 2019-02-05 stsp return worktree->root_path;
483 c7f4312f 2019-02-05 stsp const char *
484 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
486 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
489 49520a32 2018-12-29 stsp const char *
490 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
492 f609be2e 2018-12-29 stsp return worktree->path_prefix;
495 e5dc7198 2018-12-29 stsp const struct got_error *
496 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
497 e5dc7198 2018-12-29 stsp const char *path_prefix)
499 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
501 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
502 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
503 e5dc7198 2018-12-29 stsp return got_error_from_errno();
505 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
506 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
507 e5dc7198 2018-12-29 stsp free(absprefix);
508 e5dc7198 2018-12-29 stsp return NULL;
512 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
514 271d2a38 2018-12-25 stsp return got_ref_to_str(worktree->head_ref);
517 be7061eb 2018-12-30 stsp struct got_reference *
518 be7061eb 2018-12-30 stsp got_worktree_get_head_ref(struct got_worktree *worktree)
520 be7061eb 2018-12-30 stsp return got_ref_dup(worktree->head_ref);
523 b72f483a 2019-02-05 stsp struct got_object_id *
524 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
526 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
529 507dc3bb 2018-12-29 stsp const struct got_error *
530 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
531 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
533 507dc3bb 2018-12-29 stsp const struct got_error *err;
534 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
535 507dc3bb 2018-12-29 stsp char *id_str = NULL;
536 507dc3bb 2018-12-29 stsp char *path_got = NULL;
538 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
539 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
540 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
541 507dc3bb 2018-12-29 stsp path_got = NULL;
545 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
547 507dc3bb 2018-12-29 stsp return err;
549 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
550 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
554 507dc3bb 2018-12-29 stsp /* Record our base commit. */
555 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
558 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
562 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
563 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
564 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
565 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
570 507dc3bb 2018-12-29 stsp got_object_close(obj);
571 507dc3bb 2018-12-29 stsp free(id_str);
572 507dc3bb 2018-12-29 stsp free(path_got);
573 507dc3bb 2018-12-29 stsp return err;
576 9d31a1d8 2018-03-11 stsp static const struct got_error *
577 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
579 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
580 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
581 9d31a1d8 2018-03-11 stsp : got_error_from_errno());
582 86c3caaf 2018-03-09 stsp return NULL;
585 21908da4 2019-01-13 stsp static const struct got_error *
586 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
588 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
589 4a1ddfc2 2019-01-12 stsp char *abspath;
591 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
592 4a1ddfc2 2019-01-12 stsp return got_error_from_errno();
594 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
595 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
596 ddcd8544 2019-03-11 stsp struct stat sb;
597 ddcd8544 2019-03-11 stsp err = NULL;
598 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
599 ddcd8544 2019-03-11 stsp err = got_error_from_errno();
600 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
601 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
602 ddcd8544 2019-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
605 4a1ddfc2 2019-01-12 stsp free(abspath);
606 68c76935 2019-02-19 stsp return err;
609 68c76935 2019-02-19 stsp static const struct got_error *
610 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
612 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
613 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
614 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
615 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
619 68c76935 2019-02-19 stsp while (1) {
620 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
621 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
622 68c76935 2019-02-19 stsp err = got_error_from_errno();
625 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
626 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
627 68c76935 2019-02-19 stsp err = got_error_from_errno();
630 68c76935 2019-02-19 stsp if (flen1 == 0) {
631 68c76935 2019-02-19 stsp if (flen2 != 0)
634 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
635 68c76935 2019-02-19 stsp if (flen1 != 0)
638 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
639 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
649 68c76935 2019-02-19 stsp return err;
652 68c76935 2019-02-19 stsp static const struct got_error *
653 68c76935 2019-02-19 stsp check_files_equal(int *same, const char *f1_path, const char *f2_path)
655 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
656 68c76935 2019-02-19 stsp struct stat sb;
657 68c76935 2019-02-19 stsp size_t size1, size2;
658 68c76935 2019-02-19 stsp FILE *f1 = NULL, *f2 = NULL;
662 68c76935 2019-02-19 stsp if (lstat(f1_path, &sb) != 0) {
663 68c76935 2019-02-19 stsp err = got_error_from_errno();
666 68c76935 2019-02-19 stsp size1 = sb.st_size;
668 68c76935 2019-02-19 stsp if (lstat(f2_path, &sb) != 0) {
669 68c76935 2019-02-19 stsp err = got_error_from_errno();
672 68c76935 2019-02-19 stsp size2 = sb.st_size;
674 68c76935 2019-02-19 stsp if (size1 != size2) {
676 68c76935 2019-02-19 stsp return NULL;
679 68c76935 2019-02-19 stsp f1 = fopen(f1_path, "r");
680 68c76935 2019-02-19 stsp if (f1 == NULL)
681 68c76935 2019-02-19 stsp return got_error_from_errno();
683 68c76935 2019-02-19 stsp f2 = fopen(f2_path, "r");
684 68c76935 2019-02-19 stsp if (f2 == NULL) {
685 68c76935 2019-02-19 stsp err = got_error_from_errno();
689 68c76935 2019-02-19 stsp err = check_file_contents_equal(same, f1, f2);
691 68c76935 2019-02-19 stsp if (f1 && fclose(f1) != 0 && err == NULL)
692 68c76935 2019-02-19 stsp err = got_error_from_errno();
693 68c76935 2019-02-19 stsp if (f2 && fclose(f2) != 0 && err == NULL)
694 68c76935 2019-02-19 stsp err = got_error_from_errno();
696 6353ad76 2019-02-08 stsp return err;
700 6353ad76 2019-02-08 stsp * Perform a 3-way merge where the file's version in the file index (blob2)
701 6353ad76 2019-02-08 stsp * acts as the common ancestor, the incoming blob (blob1) acts as the first
702 6353ad76 2019-02-08 stsp * derived version, and the file on disk acts as the second derived version.
704 6353ad76 2019-02-08 stsp static const struct got_error *
705 6353ad76 2019-02-08 stsp merge_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
706 6353ad76 2019-02-08 stsp struct got_fileindex_entry *ie, const char *ondisk_path, const char *path,
707 b8f41171 2019-02-10 stsp uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob1,
708 b8f41171 2019-02-10 stsp struct got_repository *repo,
709 6353ad76 2019-02-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
711 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
712 6353ad76 2019-02-08 stsp int merged_fd = -1;
713 6353ad76 2019-02-08 stsp struct got_blob_object *blob2 = NULL;
714 6353ad76 2019-02-08 stsp FILE *f1 = NULL, *f2 = NULL;
715 6353ad76 2019-02-08 stsp char *blob1_path = NULL, *blob2_path = NULL;
716 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
717 6353ad76 2019-02-08 stsp struct got_object_id id2;
718 6353ad76 2019-02-08 stsp char *id_str = NULL;
719 6353ad76 2019-02-08 stsp char *label1 = NULL;
720 68c76935 2019-02-19 stsp int overlapcnt = 0, update_timestamps = 0;
721 af54ae4a 2019-02-19 stsp char *parent;
723 af54ae4a 2019-02-19 stsp parent = dirname(ondisk_path);
724 af54ae4a 2019-02-19 stsp if (parent == NULL)
725 af54ae4a 2019-02-19 stsp return got_error_from_errno();
727 af54ae4a 2019-02-19 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1)
728 af54ae4a 2019-02-19 stsp return got_error_from_errno();
730 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
734 af54ae4a 2019-02-19 stsp free(base_path);
735 af54ae4a 2019-02-19 stsp if (asprintf(&base_path, "%s/got-merge-blob1", parent) == -1) {
736 af54ae4a 2019-02-19 stsp err = got_error_from_errno();
737 af54ae4a 2019-02-19 stsp base_path = NULL;
741 af54ae4a 2019-02-19 stsp err = got_opentemp_named(&blob1_path, &f1, base_path);
744 6353ad76 2019-02-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, f1, blob1);
748 af54ae4a 2019-02-19 stsp free(base_path);
749 af54ae4a 2019-02-19 stsp if (asprintf(&base_path, "%s/got-merge-blob2", parent) == -1) {
750 af54ae4a 2019-02-19 stsp err = got_error_from_errno();
751 af54ae4a 2019-02-19 stsp base_path = NULL;
755 af54ae4a 2019-02-19 stsp err = got_opentemp_named(&blob2_path, &f2, base_path);
759 6353ad76 2019-02-08 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
760 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
763 6353ad76 2019-02-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, f2, blob2);
767 6353ad76 2019-02-08 stsp err = got_object_id_str(&id_str, worktree->base_commit_id);
770 6353ad76 2019-02-08 stsp if (asprintf(&label1, "commit %s", id_str) == -1) {
771 6353ad76 2019-02-08 stsp err = got_error_from_errno();
775 6353ad76 2019-02-08 stsp err = got_merge_diff3(&overlapcnt, merged_fd, blob1_path,
776 6353ad76 2019-02-08 stsp blob2_path, ondisk_path, label1, path);
780 6353ad76 2019-02-08 stsp (*progress_cb)(progress_arg,
781 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
784 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
785 816dc654 2019-02-16 stsp err = got_error_from_errno();
789 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
790 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
791 68c76935 2019-02-19 stsp err = check_files_equal(&update_timestamps, blob1_path,
792 68c76935 2019-02-19 stsp merged_path);
797 70a0c8ec 2019-02-20 stsp if (chmod(merged_path, st_mode) != 0) {
798 70a0c8ec 2019-02-20 stsp err = got_error_from_errno();
802 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
803 6353ad76 2019-02-08 stsp err = got_error_from_errno();
804 2a57020b 2019-02-20 stsp unlink(merged_path);
809 02c07007 2019-02-10 stsp * Do not update timestamps of already modified files. Otherwise,
810 65ad4e61 2019-02-20 stsp * a future status walk would treat them as unmodified files again.
812 6353ad76 2019-02-08 stsp err = got_fileindex_entry_update(ie, ondisk_path,
813 68c76935 2019-02-19 stsp blob1->id.sha1, worktree->base_commit_id->sha1, update_timestamps);
815 3a6ce05a 2019-02-11 stsp if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
816 3a6ce05a 2019-02-11 stsp err = got_error_from_errno();
817 3a6ce05a 2019-02-11 stsp if (f1 && fclose(f1) != 0 && err == NULL)
818 3a6ce05a 2019-02-11 stsp err = got_error_from_errno();
819 3a6ce05a 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
820 3a6ce05a 2019-02-11 stsp err = got_error_from_errno();
822 6353ad76 2019-02-08 stsp got_object_blob_close(blob2);
823 6353ad76 2019-02-08 stsp free(merged_path);
824 af54ae4a 2019-02-19 stsp free(base_path);
825 6353ad76 2019-02-08 stsp if (blob1_path) {
826 6353ad76 2019-02-08 stsp unlink(blob1_path);
827 6353ad76 2019-02-08 stsp free(blob1_path);
829 6353ad76 2019-02-08 stsp if (blob2_path) {
830 6353ad76 2019-02-08 stsp unlink(blob2_path);
831 6353ad76 2019-02-08 stsp free(blob2_path);
833 6353ad76 2019-02-08 stsp free(id_str);
834 6353ad76 2019-02-08 stsp free(label1);
835 4a1ddfc2 2019-01-12 stsp return err;
838 4a1ddfc2 2019-01-12 stsp static const struct got_error *
839 8da9e5f4 2019-01-12 stsp install_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
840 6353ad76 2019-02-08 stsp struct got_fileindex_entry *entry, const char *ondisk_path, const char *path,
841 b8f41171 2019-02-10 stsp uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob,
842 b8f41171 2019-02-10 stsp int restoring_missing_file, struct got_repository *repo,
843 b8f41171 2019-02-10 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
845 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
846 507dc3bb 2018-12-29 stsp int fd = -1;
847 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
848 507dc3bb 2018-12-29 stsp int update = 0;
849 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
851 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
852 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
853 9d31a1d8 2018-03-11 stsp if (fd == -1) {
854 21908da4 2019-01-13 stsp if (errno == ENOENT) {
855 21908da4 2019-01-13 stsp char *parent = dirname(path);
856 21908da4 2019-01-13 stsp if (parent == NULL)
857 21908da4 2019-01-13 stsp return got_error_from_errno();
858 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
860 21908da4 2019-01-13 stsp return err;
861 21908da4 2019-01-13 stsp fd = open(ondisk_path,
862 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
863 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
864 21908da4 2019-01-13 stsp if (fd == -1)
865 21908da4 2019-01-13 stsp return got_error_from_errno();
866 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
867 b8f41171 2019-02-10 stsp if (!S_ISREG(st_mode)) {
868 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
869 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
872 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
873 507dc3bb 2018-12-29 stsp ondisk_path);
876 507dc3bb 2018-12-29 stsp update = 1;
879 7e7c1e4c 2019-01-12 stsp return got_error_from_errno();
882 a378724f 2019-02-10 stsp if (restoring_missing_file)
883 a378724f 2019-02-10 stsp (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
885 a378724f 2019-02-10 stsp (*progress_cb)(progress_arg,
886 a378724f 2019-02-10 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
888 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
890 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
891 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
894 9d31a1d8 2018-03-11 stsp if (len > 0) {
895 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
896 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
897 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
898 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
900 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
901 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
904 61d6eaa3 2018-12-24 stsp hdrlen = 0;
906 9d31a1d8 2018-03-11 stsp } while (len != 0);
908 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
909 816dc654 2019-02-16 stsp err = got_error_from_errno();
913 507dc3bb 2018-12-29 stsp if (update) {
914 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
915 68ed9ba5 2019-02-10 stsp err = got_error_from_errno();
916 2a57020b 2019-02-20 stsp unlink(tmppath);
921 b8f41171 2019-02-10 stsp if (te_mode & S_IXUSR) {
922 b8f41171 2019-02-10 stsp if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
923 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
927 b8f41171 2019-02-10 stsp if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
928 68ed9ba5 2019-02-10 stsp err = got_error_from_errno();
933 5f9fef37 2019-01-13 stsp if (entry == NULL)
934 5f9fef37 2019-01-13 stsp entry = got_fileindex_entry_get(fileindex, path);
936 51514078 2018-12-25 stsp err = got_fileindex_entry_update(entry, ondisk_path,
937 02c07007 2019-02-10 stsp blob->id.sha1, worktree->base_commit_id->sha1, 1);
939 51514078 2018-12-25 stsp err = got_fileindex_entry_alloc(&entry, ondisk_path,
940 8da9e5f4 2019-01-12 stsp path, blob->id.sha1, worktree->base_commit_id->sha1);
943 51514078 2018-12-25 stsp err = got_fileindex_entry_add(fileindex, entry);
946 3a6ce05a 2019-02-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
947 3a6ce05a 2019-02-11 stsp err = got_error_from_errno();
948 507dc3bb 2018-12-29 stsp free(tmppath);
949 6353ad76 2019-02-08 stsp return err;
952 6353ad76 2019-02-08 stsp static const struct got_error *
953 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
954 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
955 b8f41171 2019-02-10 stsp struct got_repository *repo)
957 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
958 6353ad76 2019-02-08 stsp struct got_object_id id;
959 6353ad76 2019-02-08 stsp size_t hdrlen;
960 6353ad76 2019-02-08 stsp FILE *f = NULL;
961 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
962 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
963 6353ad76 2019-02-08 stsp size_t flen, blen;
965 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
967 b8f41171 2019-02-10 stsp if (lstat(abspath, sb) == -1) {
968 a378724f 2019-02-10 stsp if (errno == ENOENT) {
970 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
971 2ec1f75b 2019-03-26 stsp *status = GOT_STATUS_MISSING;
973 2ec1f75b 2019-03-26 stsp *status = GOT_STATUS_DELETE;
974 b8f41171 2019-02-10 stsp sb->st_mode =
975 b8f41171 2019-02-10 stsp ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
976 b8f41171 2019-02-10 stsp & (S_IRWXU | S_IRWXG | S_IRWXO));
978 b8f41171 2019-02-10 stsp sb->st_mode = GOT_DEFAULT_FILE_MODE;
979 a378724f 2019-02-10 stsp return NULL;
981 6353ad76 2019-02-08 stsp return got_error_from_errno();
984 b8f41171 2019-02-10 stsp if (!S_ISREG(sb->st_mode)) {
985 6353ad76 2019-02-08 stsp *status = GOT_STATUS_OBSTRUCTED;
986 6353ad76 2019-02-08 stsp return NULL;
989 b8f41171 2019-02-10 stsp if (ie == NULL)
990 d00136be 2019-03-26 stsp return NULL;
992 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
993 2ec1f75b 2019-03-26 stsp *status = GOT_STATUS_DELETE;
994 2ec1f75b 2019-03-26 stsp return NULL;
995 2ec1f75b 2019-03-26 stsp } else if (!got_fileindex_entry_has_blob(ie)) {
996 d00136be 2019-03-26 stsp *status = GOT_STATUS_ADD;
997 b8f41171 2019-02-10 stsp return NULL;
1000 b8f41171 2019-02-10 stsp if (ie->ctime_sec == sb->st_ctime &&
1001 b8f41171 2019-02-10 stsp ie->ctime_nsec == sb->st_ctimensec &&
1002 b8f41171 2019-02-10 stsp ie->mtime_sec == sb->st_mtime &&
1003 b8f41171 2019-02-10 stsp ie->mtime_sec == sb->st_mtime &&
1004 b8f41171 2019-02-10 stsp ie->mtime_nsec == sb->st_mtimensec &&
1005 b8f41171 2019-02-10 stsp ie->size == (sb->st_size & 0xffffffff))
1006 6353ad76 2019-02-08 stsp return NULL;
1008 6353ad76 2019-02-08 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1009 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1011 6353ad76 2019-02-08 stsp return err;
1013 6353ad76 2019-02-08 stsp f = fopen(abspath, "r");
1014 6353ad76 2019-02-08 stsp if (f == NULL) {
1015 6353ad76 2019-02-08 stsp err = got_error_from_errno();
1016 6353ad76 2019-02-08 stsp goto done;
1018 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1019 6353ad76 2019-02-08 stsp while (1) {
1020 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1021 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1024 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1025 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1026 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1027 80c5c120 2019-02-19 stsp err = got_error_from_errno();
1030 6353ad76 2019-02-08 stsp if (blen == 0) {
1031 6353ad76 2019-02-08 stsp if (flen != 0)
1032 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1034 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1035 6353ad76 2019-02-08 stsp if (blen != 0)
1036 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1038 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1039 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1040 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1041 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1045 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1048 6353ad76 2019-02-08 stsp hdrlen = 0;
1052 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1054 6353ad76 2019-02-08 stsp fclose(f);
1055 9d31a1d8 2018-03-11 stsp return err;
1058 9d31a1d8 2018-03-11 stsp static const struct got_error *
1059 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1060 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1061 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1062 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1063 8da9e5f4 2019-01-12 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1065 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1066 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1067 6353ad76 2019-02-08 stsp char *ondisk_path;
1068 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1069 b8f41171 2019-02-10 stsp struct stat sb;
1071 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1072 6353ad76 2019-02-08 stsp return got_error_from_errno();
1074 b8f41171 2019-02-10 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1076 b8f41171 2019-02-10 stsp goto done;
1078 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1079 b8f41171 2019-02-10 stsp (*progress_cb)(progress_arg, status, path);
1080 b8f41171 2019-02-10 stsp goto done;
1083 b8f41171 2019-02-10 stsp if (ie && status != GOT_STATUS_MISSING) {
1084 b8f41171 2019-02-10 stsp if (memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1085 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1086 b8f41171 2019-02-10 stsp (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1088 6353ad76 2019-02-08 stsp goto done;
1090 b8f41171 2019-02-10 stsp if (memcmp(ie->blob_sha1,
1091 b8f41171 2019-02-10 stsp te->id->sha1, SHA1_DIGEST_LENGTH) == 0)
1092 b8f41171 2019-02-10 stsp goto done;
1095 8da9e5f4 2019-01-12 stsp err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1097 6353ad76 2019-02-08 stsp goto done;
1099 276262e8 2019-02-08 stsp if (status == GOT_STATUS_MODIFY)
1100 6353ad76 2019-02-08 stsp err = merge_blob(worktree, fileindex, ie, ondisk_path, path,
1101 b8f41171 2019-02-10 stsp te->mode, sb.st_mode, blob, repo, progress_cb,
1102 b8f41171 2019-02-10 stsp progress_arg);
1104 6353ad76 2019-02-08 stsp err = install_blob(worktree, fileindex, ie, ondisk_path, path,
1105 b8f41171 2019-02-10 stsp te->mode, sb.st_mode, blob, status == GOT_STATUS_MISSING,
1106 b8f41171 2019-02-10 stsp repo, progress_cb, progress_arg);
1108 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
1110 6353ad76 2019-02-08 stsp free(ondisk_path);
1111 8da9e5f4 2019-01-12 stsp return err;
1114 90285c3b 2019-01-08 stsp static const struct got_error *
1115 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
1117 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
1118 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
1120 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1121 90285c3b 2019-01-08 stsp return got_error_from_errno();
1123 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
1124 c97665ae 2019-01-13 stsp if (errno != ENOENT)
1125 c97665ae 2019-01-13 stsp err = got_error_from_errno();
1127 90285c3b 2019-01-08 stsp char *parent = dirname(ondisk_path);
1128 7a9df742 2019-01-08 stsp while (parent && strcmp(parent, root_path) != 0) {
1129 26c4ac4d 2019-01-08 stsp if (rmdir(parent) == -1) {
1130 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
1131 26c4ac4d 2019-01-08 stsp err = got_error_from_errno();
1134 90285c3b 2019-01-08 stsp parent = dirname(parent);
1137 90285c3b 2019-01-08 stsp free(ondisk_path);
1138 90285c3b 2019-01-08 stsp return err;
1141 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
1142 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
1143 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
1144 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
1145 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
1146 8da9e5f4 2019-01-12 stsp void *progress_arg;
1147 8da9e5f4 2019-01-12 stsp got_worktree_cancel_cb cancel_cb;
1148 8da9e5f4 2019-01-12 stsp void *cancel_arg;
1151 512f0d0e 2019-01-02 stsp static const struct got_error *
1152 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
1153 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
1155 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1157 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
1158 8da9e5f4 2019-01-12 stsp ie->path, a->repo, a->progress_cb, a->progress_arg,
1159 8da9e5f4 2019-01-12 stsp a->cancel_cb, a->cancel_arg);
1162 8da9e5f4 2019-01-12 stsp static const struct got_error *
1163 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1165 8da9e5f4 2019-01-12 stsp const struct got_error *err;
1166 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1168 8da9e5f4 2019-01-12 stsp (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE, ie->path);
1170 8da9e5f4 2019-01-12 stsp err = remove_ondisk_file(a->worktree->root_path, ie->path);
1172 8da9e5f4 2019-01-12 stsp return err;
1173 8da9e5f4 2019-01-12 stsp got_fileindex_entry_remove(a->fileindex, ie);
1174 8da9e5f4 2019-01-12 stsp return NULL;
1177 9d31a1d8 2018-03-11 stsp static const struct got_error *
1178 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1180 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1181 8da9e5f4 2019-01-12 stsp const struct got_error *err;
1182 8da9e5f4 2019-01-12 stsp char *path;
1184 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
1185 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
1187 8da9e5f4 2019-01-12 stsp return got_error_from_errno();
1189 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
1190 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
1192 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1193 8da9e5f4 2019-01-12 stsp a->repo, a->progress_cb, a->progress_arg,
1194 8da9e5f4 2019-01-12 stsp a->cancel_cb, a->cancel_arg);
1196 8da9e5f4 2019-01-12 stsp free(path);
1197 0cd1c46a 2019-03-11 stsp return err;
1200 517bab73 2019-03-11 stsp const struct got_error *
1201 517bab73 2019-03-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1203 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
1204 0647c563 2019-03-11 stsp char *uuidstr = NULL;
1205 0cd1c46a 2019-03-11 stsp uint32_t uuid_status;
1207 517bab73 2019-03-11 stsp *refname = NULL;
1209 0cd1c46a 2019-03-11 stsp uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1210 0cd1c46a 2019-03-11 stsp if (uuid_status != uuid_s_ok)
1211 0cd1c46a 2019-03-11 stsp return got_error_uuid(uuid_status);
1213 0647c563 2019-03-11 stsp if (asprintf(refname, "%s-%s", GOT_WORKTREE_BASE_REF_PREFIX, uuidstr)
1215 0cd1c46a 2019-03-11 stsp err = got_error_from_errno();
1216 0647c563 2019-03-11 stsp *refname = NULL;
1218 517bab73 2019-03-11 stsp free(uuidstr);
1219 517bab73 2019-03-11 stsp return err;
1223 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
1224 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
1226 517bab73 2019-03-11 stsp static const struct got_error *
1227 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1229 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
1230 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
1231 517bab73 2019-03-11 stsp char *refname;
1233 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
1235 517bab73 2019-03-11 stsp return err;
1237 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1239 0cd1c46a 2019-03-11 stsp goto done;
1241 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
1243 0cd1c46a 2019-03-11 stsp free(refname);
1245 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
1246 9d31a1d8 2018-03-11 stsp return err;
1250 9d31a1d8 2018-03-11 stsp const struct got_error *
1251 9d31a1d8 2018-03-11 stsp got_worktree_checkout_files(struct got_worktree *worktree,
1252 93a30277 2018-12-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1253 93a30277 2018-12-24 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1255 a143fb78 2018-12-25 stsp const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
1256 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
1257 8da9e5f4 2019-01-12 stsp struct got_object_id *tree_id = NULL;
1258 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
1259 9d31a1d8 2018-03-11 stsp char *fileindex_path = NULL, *new_fileindex_path = NULL;
1260 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
1261 51514078 2018-12-25 stsp FILE *index = NULL, *new_index = NULL;
1262 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb diff_cb;
1263 8da9e5f4 2019-01-12 stsp struct diff_cb_arg arg;
1265 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
1267 9d31a1d8 2018-03-11 stsp return err;
1269 133d2798 2019-01-08 stsp fileindex = got_fileindex_alloc();
1270 133d2798 2019-01-08 stsp if (fileindex == NULL) {
1271 133d2798 2019-01-08 stsp err = got_error_from_errno();
1272 9d31a1d8 2018-03-11 stsp goto done;
1275 9d31a1d8 2018-03-11 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1276 9d31a1d8 2018-03-11 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1277 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
1278 9d31a1d8 2018-03-11 stsp fileindex_path = NULL;
1279 9d31a1d8 2018-03-11 stsp goto done;
1283 51514078 2018-12-25 stsp * Read the file index.
1284 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
1285 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
1287 51514078 2018-12-25 stsp index = fopen(fileindex_path, "rb");
1288 51514078 2018-12-25 stsp if (index == NULL) {
1289 cf61d818 2019-01-12 stsp if (errno != ENOENT) {
1290 cf61d818 2019-01-12 stsp err = got_error_from_errno();
1291 cf61d818 2019-01-12 stsp goto done;
1294 cf61d818 2019-01-12 stsp err = got_fileindex_read(fileindex, index);
1295 cf61d818 2019-01-12 stsp fclose(index);
1297 cf61d818 2019-01-12 stsp goto done;
1300 b8bdcc21 2018-12-24 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
1301 b8bdcc21 2018-12-24 stsp fileindex_path);
1303 51664889 2018-03-12 stsp goto done;
1305 0cd1c46a 2019-03-11 stsp err = ref_base_commit(worktree, repo);
1307 0cd1c46a 2019-03-11 stsp goto done;
1309 eaccb85f 2018-12-25 stsp err = got_object_open_as_commit(&commit, repo,
1310 eaccb85f 2018-12-25 stsp worktree->base_commit_id);
1312 9d31a1d8 2018-03-11 stsp goto done;
1314 8da9e5f4 2019-01-12 stsp err = got_object_id_by_path(&tree_id, repo,
1315 8da9e5f4 2019-01-12 stsp worktree->base_commit_id, worktree->path_prefix);
1317 9d31a1d8 2018-03-11 stsp goto done;
1319 8da9e5f4 2019-01-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1321 8da9e5f4 2019-01-12 stsp goto done;
1323 8da9e5f4 2019-01-12 stsp diff_cb.diff_old_new = diff_old_new;
1324 8da9e5f4 2019-01-12 stsp diff_cb.diff_old = diff_old;
1325 8da9e5f4 2019-01-12 stsp diff_cb.diff_new = diff_new;
1326 8da9e5f4 2019-01-12 stsp arg.fileindex = fileindex;
1327 8da9e5f4 2019-01-12 stsp arg.worktree = worktree;
1328 8da9e5f4 2019-01-12 stsp arg.repo = repo;
1329 8da9e5f4 2019-01-12 stsp arg.progress_cb = progress_cb;
1330 8da9e5f4 2019-01-12 stsp arg.progress_arg = progress_arg;
1331 8da9e5f4 2019-01-12 stsp arg.cancel_cb = cancel_cb;
1332 8da9e5f4 2019-01-12 stsp arg.cancel_arg = cancel_arg;
1333 8da9e5f4 2019-01-12 stsp checkout_err = got_fileindex_diff_tree(fileindex, tree, repo,
1334 8da9e5f4 2019-01-12 stsp &diff_cb, &arg);
1336 a143fb78 2018-12-25 stsp /* Try to sync the fileindex back to disk in any case. */
1337 b8bdcc21 2018-12-24 stsp err = got_fileindex_write(fileindex, new_index);
1339 9d31a1d8 2018-03-11 stsp goto done;
1341 9d31a1d8 2018-03-11 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
1342 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
1343 2a57020b 2019-02-20 stsp unlink(new_fileindex_path);
1344 9d31a1d8 2018-03-11 stsp goto done;
1347 9d31a1d8 2018-03-11 stsp free(new_fileindex_path);
1348 9d31a1d8 2018-03-11 stsp new_fileindex_path = NULL;
1352 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
1353 9d31a1d8 2018-03-11 stsp if (commit)
1354 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
1355 9d31a1d8 2018-03-11 stsp if (new_fileindex_path)
1356 9d31a1d8 2018-03-11 stsp unlink(new_fileindex_path);
1357 b8bdcc21 2018-12-24 stsp if (new_index)
1358 b8bdcc21 2018-12-24 stsp fclose(new_index);
1359 9d31a1d8 2018-03-11 stsp free(new_fileindex_path);
1360 9d31a1d8 2018-03-11 stsp free(fileindex_path);
1361 7426bbfd 2018-12-24 stsp got_fileindex_free(fileindex);
1362 a143fb78 2018-12-25 stsp if (checkout_err)
1363 a143fb78 2018-12-25 stsp err = checkout_err;
1364 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
1365 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
1366 9d31a1d8 2018-03-11 stsp err = unlockerr;
1367 f8d1f275 2019-02-04 stsp return err;
1370 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
1371 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
1372 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
1373 927df6b7 2019-02-10 stsp const char *status_path;
1374 927df6b7 2019-02-10 stsp size_t status_path_len;
1375 f8d1f275 2019-02-04 stsp struct got_repository *repo;
1376 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
1377 f8d1f275 2019-02-04 stsp void *status_arg;
1378 f8d1f275 2019-02-04 stsp got_worktree_cancel_cb cancel_cb;
1379 f8d1f275 2019-02-04 stsp void *cancel_arg;
1382 f8d1f275 2019-02-04 stsp static const struct got_error *
1383 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1384 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
1385 927df6b7 2019-02-10 stsp struct got_repository *repo)
1387 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
1388 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1389 927df6b7 2019-02-10 stsp struct stat sb;
1390 927df6b7 2019-02-10 stsp struct got_object_id id;
1392 927df6b7 2019-02-10 stsp err = get_file_status(&status, &sb, ie, abspath, repo);
1393 927df6b7 2019-02-10 stsp if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1394 927df6b7 2019-02-10 stsp memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1395 927df6b7 2019-02-10 stsp err = (*status_cb)(status_arg, status, ie->path, &id);
1397 927df6b7 2019-02-10 stsp return err;
1400 927df6b7 2019-02-10 stsp static const struct got_error *
1401 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
1402 f8d1f275 2019-02-04 stsp struct dirent *de, const char *parent_path)
1404 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
1405 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
1406 f8d1f275 2019-02-04 stsp char *abspath;
1408 927df6b7 2019-02-10 stsp if (got_path_cmp(parent_path, a->status_path) != 0 &&
1409 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1410 927df6b7 2019-02-10 stsp return NULL;
1412 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
1413 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1414 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
1415 f8d1f275 2019-02-04 stsp return got_error_from_errno();
1417 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1418 f8d1f275 2019-02-04 stsp de->d_name) == -1)
1419 f8d1f275 2019-02-04 stsp return got_error_from_errno();
1422 927df6b7 2019-02-10 stsp err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1424 f8d1f275 2019-02-04 stsp free(abspath);
1425 9d31a1d8 2018-03-11 stsp return err;
1428 f8d1f275 2019-02-04 stsp static const struct got_error *
1429 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1431 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
1432 b72f483a 2019-02-05 stsp struct got_object_id id;
1433 2ec1f75b 2019-03-26 stsp unsigned char status;
1435 927df6b7 2019-02-10 stsp if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1436 927df6b7 2019-02-10 stsp return NULL;
1438 b72f483a 2019-02-05 stsp memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1439 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1440 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
1442 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
1443 2ec1f75b 2019-03-26 stsp return (*a->status_cb)(a->status_arg, status, ie->path, &id);
1446 f8d1f275 2019-02-04 stsp static const struct got_error *
1447 f8d1f275 2019-02-04 stsp status_new(void *arg, struct dirent *de, const char *parent_path)
1449 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
1450 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
1451 f8d1f275 2019-02-04 stsp char *path = NULL;
1453 f8d1f275 2019-02-04 stsp if (de->d_type == DT_DIR)
1454 2c201a36 2019-02-10 stsp return NULL;
1456 2c201a36 2019-02-10 stsp /* XXX ignore symlinks for now */
1457 2c201a36 2019-02-10 stsp if (de->d_type == DT_LNK)
1458 f8d1f275 2019-02-04 stsp return NULL;
1460 927df6b7 2019-02-10 stsp if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1461 927df6b7 2019-02-10 stsp return NULL;
1463 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
1464 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1465 f8d1f275 2019-02-04 stsp return got_error_from_errno();
1467 f8d1f275 2019-02-04 stsp path = de->d_name;
1470 b72f483a 2019-02-05 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
1472 f8d1f275 2019-02-04 stsp if (parent_path[0])
1473 f8d1f275 2019-02-04 stsp free(path);
1474 b72f483a 2019-02-05 stsp return err;
1477 f8d1f275 2019-02-04 stsp const struct got_error *
1478 927df6b7 2019-02-10 stsp got_worktree_status(struct got_worktree *worktree, const char *path,
1479 f8d1f275 2019-02-04 stsp struct got_repository *repo, got_worktree_status_cb status_cb,
1480 f8d1f275 2019-02-04 stsp void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1482 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
1483 f8d1f275 2019-02-04 stsp DIR *workdir = NULL;
1484 f8d1f275 2019-02-04 stsp char *fileindex_path = NULL;
1485 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex = NULL;
1486 f8d1f275 2019-02-04 stsp FILE *index = NULL;
1487 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
1488 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
1489 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
1491 f8d1f275 2019-02-04 stsp fileindex = got_fileindex_alloc();
1492 f8d1f275 2019-02-04 stsp if (fileindex == NULL) {
1493 f8d1f275 2019-02-04 stsp err = got_error_from_errno();
1494 f8d1f275 2019-02-04 stsp goto done;
1497 f8d1f275 2019-02-04 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1498 f8d1f275 2019-02-04 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1499 f8d1f275 2019-02-04 stsp err = got_error_from_errno();
1500 f8d1f275 2019-02-04 stsp fileindex_path = NULL;
1501 f8d1f275 2019-02-04 stsp goto done;
1504 f8d1f275 2019-02-04 stsp index = fopen(fileindex_path, "rb");
1505 f8d1f275 2019-02-04 stsp if (index == NULL) {
1506 f8d1f275 2019-02-04 stsp if (errno != ENOENT) {
1507 f8d1f275 2019-02-04 stsp err = got_error_from_errno();
1508 f8d1f275 2019-02-04 stsp goto done;
1511 f8d1f275 2019-02-04 stsp err = got_fileindex_read(fileindex, index);
1512 f8d1f275 2019-02-04 stsp fclose(index);
1514 f8d1f275 2019-02-04 stsp goto done;
1517 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
1518 927df6b7 2019-02-10 stsp worktree->root_path, path[0] ? "/" : "", path) == -1) {
1519 c513d110 2019-02-05 stsp err = got_error_from_errno();
1520 c513d110 2019-02-05 stsp goto done;
1522 927df6b7 2019-02-10 stsp workdir = opendir(ondisk_path);
1523 927df6b7 2019-02-10 stsp if (workdir == NULL) {
1524 2ec1f75b 2019-03-26 stsp if (errno == ENOTDIR || errno == ENOENT) {
1525 927df6b7 2019-02-10 stsp struct got_fileindex_entry *ie;
1526 927df6b7 2019-02-10 stsp ie = got_fileindex_entry_get(fileindex, path);
1527 927df6b7 2019-02-10 stsp if (ie == NULL) {
1528 927df6b7 2019-02-10 stsp err = got_error(GOT_ERR_BAD_PATH);
1529 927df6b7 2019-02-10 stsp goto done;
1531 927df6b7 2019-02-10 stsp err = report_file_status(ie, ondisk_path,
1532 927df6b7 2019-02-10 stsp status_cb, status_arg, repo);
1533 927df6b7 2019-02-10 stsp goto done;
1535 927df6b7 2019-02-10 stsp err = got_error_from_errno();
1536 927df6b7 2019-02-10 stsp goto done;
1539 d43a8a88 2019-02-05 stsp fdiff_cb.diff_old_new = status_old_new;
1540 d43a8a88 2019-02-05 stsp fdiff_cb.diff_old = status_old;
1541 d43a8a88 2019-02-05 stsp fdiff_cb.diff_new = status_new;
1542 f8d1f275 2019-02-04 stsp arg.fileindex = fileindex;
1543 f8d1f275 2019-02-04 stsp arg.worktree = worktree;
1544 927df6b7 2019-02-10 stsp arg.status_path = path;
1545 927df6b7 2019-02-10 stsp arg.status_path_len = strlen(path);
1546 f8d1f275 2019-02-04 stsp arg.repo = repo;
1547 f8d1f275 2019-02-04 stsp arg.status_cb = status_cb;
1548 f8d1f275 2019-02-04 stsp arg.status_arg = status_arg;
1549 f8d1f275 2019-02-04 stsp arg.cancel_cb = cancel_cb;
1550 f8d1f275 2019-02-04 stsp arg.cancel_arg = cancel_arg;
1551 c7f4312f 2019-02-05 stsp err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1552 927df6b7 2019-02-10 stsp path, repo, &fdiff_cb, &arg);
1554 f8d1f275 2019-02-04 stsp if (workdir)
1555 f8d1f275 2019-02-04 stsp closedir(workdir);
1556 927df6b7 2019-02-10 stsp free(ondisk_path);
1557 f8d1f275 2019-02-04 stsp free(fileindex_path);
1558 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
1559 f8d1f275 2019-02-04 stsp return err;
1562 6c7ab921 2019-03-18 stsp const struct got_error *
1563 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
1564 6c7ab921 2019-03-18 stsp const char *arg)
1566 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
1567 6c7ab921 2019-03-18 stsp char *resolved, *path = NULL;
1568 6c7ab921 2019-03-18 stsp size_t len;
1570 6c7ab921 2019-03-18 stsp *wt_path = NULL;
1572 6c7ab921 2019-03-18 stsp resolved = realpath(arg, NULL);
1573 6c7ab921 2019-03-18 stsp if (resolved == NULL)
1574 6c7ab921 2019-03-18 stsp return got_error_from_errno();
1576 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
1577 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
1578 6c7ab921 2019-03-18 stsp err = got_error(GOT_ERR_BAD_PATH);
1579 6c7ab921 2019-03-18 stsp goto done;
1582 6c7ab921 2019-03-18 stsp path = strdup(resolved + strlen(got_worktree_get_root_path(worktree)));
1583 6c7ab921 2019-03-18 stsp if (path == NULL) {
1584 6c7ab921 2019-03-18 stsp err = got_error_from_errno();
1585 6c7ab921 2019-03-18 stsp goto done;
1588 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
1589 6c7ab921 2019-03-18 stsp len = strlen(path);
1590 6c7ab921 2019-03-18 stsp while (path[len - 1] == '/') {
1591 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
1595 6c7ab921 2019-03-18 stsp free(resolved);
1596 6c7ab921 2019-03-18 stsp if (err == NULL)
1597 6c7ab921 2019-03-18 stsp *wt_path = path;
1599 6c7ab921 2019-03-18 stsp free(path);
1600 d00136be 2019-03-26 stsp return err;
1603 d00136be 2019-03-26 stsp const struct got_error *
1604 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
1605 031a5338 2019-03-26 stsp const char *ondisk_path, got_worktree_status_cb status_cb, void *status_arg,
1606 031a5338 2019-03-26 stsp struct got_repository *repo)
1608 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
1609 d00136be 2019-03-26 stsp struct got_fileindex_entry *ie = NULL;
1610 031a5338 2019-03-26 stsp char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
1611 d00136be 2019-03-26 stsp FILE *index = NULL, *new_index = NULL;
1612 d00136be 2019-03-26 stsp const struct got_error *err = NULL, *unlockerr = NULL;
1613 031a5338 2019-03-26 stsp int ie_added = 0;
1615 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
1617 d00136be 2019-03-26 stsp return err;
1619 031a5338 2019-03-26 stsp err = got_path_skip_common_ancestor(&relpath,
1620 d00136be 2019-03-26 stsp got_worktree_get_root_path(worktree), ondisk_path);
1622 d00136be 2019-03-26 stsp goto done;
1624 d00136be 2019-03-26 stsp fileindex = got_fileindex_alloc();
1625 d00136be 2019-03-26 stsp if (fileindex == NULL) {
1626 d00136be 2019-03-26 stsp err = got_error_from_errno();
1627 d00136be 2019-03-26 stsp goto done;
1630 d00136be 2019-03-26 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1631 d00136be 2019-03-26 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1632 d00136be 2019-03-26 stsp err = got_error_from_errno();
1633 d00136be 2019-03-26 stsp fileindex_path = NULL;
1634 d00136be 2019-03-26 stsp goto done;
1637 d00136be 2019-03-26 stsp index = fopen(fileindex_path, "rb");
1638 d00136be 2019-03-26 stsp if (index == NULL) {
1639 d00136be 2019-03-26 stsp err = got_error_from_errno();
1640 d00136be 2019-03-26 stsp goto done;
1643 d00136be 2019-03-26 stsp err = got_fileindex_read(fileindex, index);
1645 d00136be 2019-03-26 stsp goto done;
1647 5c99ca9f 2019-03-27 stsp if (got_fileindex_entry_get(fileindex, relpath) != NULL) {
1648 5c99ca9f 2019-03-27 stsp err = got_error_set_errno(EEXIST);
1649 5c99ca9f 2019-03-27 stsp goto done;
1652 5c99ca9f 2019-03-27 stsp err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
1654 5c99ca9f 2019-03-27 stsp goto done;
1656 d00136be 2019-03-26 stsp err = got_fileindex_entry_add(fileindex, ie);
1658 d00136be 2019-03-26 stsp goto done;
1659 031a5338 2019-03-26 stsp ie_added = 1; /* now owned by fileindex; don't free separately */
1661 d00136be 2019-03-26 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
1662 d00136be 2019-03-26 stsp fileindex_path);
1664 d00136be 2019-03-26 stsp goto done;
1666 d00136be 2019-03-26 stsp err = got_fileindex_write(fileindex, new_index);
1668 d00136be 2019-03-26 stsp goto done;
1670 d00136be 2019-03-26 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
1671 d00136be 2019-03-26 stsp err = got_error_from_errno();
1672 d00136be 2019-03-26 stsp goto done;
1675 d00136be 2019-03-26 stsp free(new_fileindex_path);
1676 d00136be 2019-03-26 stsp new_fileindex_path = NULL;
1678 031a5338 2019-03-26 stsp err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
1680 d00136be 2019-03-26 stsp if (index) {
1681 d00136be 2019-03-26 stsp if (fclose(index) != 0 && err == NULL)
1682 d00136be 2019-03-26 stsp err = got_error_from_errno();
1684 d00136be 2019-03-26 stsp if (new_fileindex_path) {
1685 d00136be 2019-03-26 stsp if (unlink(new_fileindex_path) != 0 && err == NULL)
1686 d00136be 2019-03-26 stsp err = got_error_from_errno();
1687 d00136be 2019-03-26 stsp free(new_fileindex_path);
1689 5c99ca9f 2019-03-27 stsp if (ie && !ie_added)
1690 d00136be 2019-03-26 stsp got_fileindex_entry_free(ie);
1691 d00136be 2019-03-26 stsp if (fileindex)
1692 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
1693 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
1694 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
1695 d00136be 2019-03-26 stsp err = unlockerr;
1696 031a5338 2019-03-26 stsp free(relpath);
1697 6c7ab921 2019-03-18 stsp return err;
1700 2ec1f75b 2019-03-26 stsp const struct got_error *
1701 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
1702 2ec1f75b 2019-03-26 stsp const char *ondisk_path, int delete_local_mods,
1703 2ec1f75b 2019-03-26 stsp got_worktree_status_cb status_cb, void *status_arg,
1704 2ec1f75b 2019-03-26 stsp struct got_repository *repo)
1706 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
1707 2ec1f75b 2019-03-26 stsp struct got_fileindex_entry *ie = NULL;
1708 2ec1f75b 2019-03-26 stsp char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
1709 2ec1f75b 2019-03-26 stsp FILE *index = NULL, *new_index = NULL;
1710 2ec1f75b 2019-03-26 stsp const struct got_error *err = NULL, *unlockerr = NULL;
1711 2ec1f75b 2019-03-26 stsp unsigned char status;
1712 2ec1f75b 2019-03-26 stsp struct stat sb;
1714 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
1716 2ec1f75b 2019-03-26 stsp return err;
1718 2ec1f75b 2019-03-26 stsp err = got_path_skip_common_ancestor(&relpath,
1719 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(worktree), ondisk_path);
1721 2ec1f75b 2019-03-26 stsp goto done;
1723 2ec1f75b 2019-03-26 stsp fileindex = got_fileindex_alloc();
1724 2ec1f75b 2019-03-26 stsp if (fileindex == NULL) {
1725 2ec1f75b 2019-03-26 stsp err = got_error_from_errno();
1726 2ec1f75b 2019-03-26 stsp goto done;
1729 2ec1f75b 2019-03-26 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1730 2ec1f75b 2019-03-26 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1731 2ec1f75b 2019-03-26 stsp err = got_error_from_errno();
1732 2ec1f75b 2019-03-26 stsp fileindex_path = NULL;
1733 2ec1f75b 2019-03-26 stsp goto done;
1736 2ec1f75b 2019-03-26 stsp index = fopen(fileindex_path, "rb");
1737 2ec1f75b 2019-03-26 stsp if (index == NULL) {
1738 2ec1f75b 2019-03-26 stsp err = got_error_from_errno();
1739 2ec1f75b 2019-03-26 stsp goto done;
1742 2ec1f75b 2019-03-26 stsp err = got_fileindex_read(fileindex, index);
1744 2ec1f75b 2019-03-26 stsp goto done;
1746 2ec1f75b 2019-03-26 stsp ie = got_fileindex_entry_get(fileindex, relpath);
1747 2ec1f75b 2019-03-26 stsp if (ie == NULL) {
1748 2ec1f75b 2019-03-26 stsp err = got_error(GOT_ERR_BAD_PATH);
1749 2ec1f75b 2019-03-26 stsp goto done;
1752 2ec1f75b 2019-03-26 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1754 2ec1f75b 2019-03-26 stsp goto done;
1756 2ec1f75b 2019-03-26 stsp if (status != GOT_STATUS_NO_CHANGE) {
1757 71a29355 2019-03-27 stsp if (status == GOT_STATUS_DELETE) {
1758 71a29355 2019-03-27 stsp err = got_error_set_errno(ENOENT);
1759 71a29355 2019-03-27 stsp goto done;
1761 2ec1f75b 2019-03-26 stsp if (status != GOT_STATUS_MODIFY) {
1762 2ec1f75b 2019-03-26 stsp err = got_error(GOT_ERR_FILE_STATUS);
1763 2ec1f75b 2019-03-26 stsp goto done;
1765 2ec1f75b 2019-03-26 stsp if (!delete_local_mods) {
1766 2ec1f75b 2019-03-26 stsp err = got_error(GOT_ERR_FILE_MODIFIED);
1767 2ec1f75b 2019-03-26 stsp goto done;
1771 2ec1f75b 2019-03-26 stsp if (unlink(ondisk_path) != 0) {
1772 2ec1f75b 2019-03-26 stsp err = got_error_from_errno();
1773 2ec1f75b 2019-03-26 stsp goto done;
1776 2ec1f75b 2019-03-26 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
1778 2ec1f75b 2019-03-26 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
1779 2ec1f75b 2019-03-26 stsp fileindex_path);
1781 2ec1f75b 2019-03-26 stsp goto done;
1783 2ec1f75b 2019-03-26 stsp err = got_fileindex_write(fileindex, new_index);
1785 2ec1f75b 2019-03-26 stsp goto done;
1787 2ec1f75b 2019-03-26 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
1788 2ec1f75b 2019-03-26 stsp err = got_error_from_errno();
1789 2ec1f75b 2019-03-26 stsp goto done;
1792 2ec1f75b 2019-03-26 stsp free(new_fileindex_path);
1793 2ec1f75b 2019-03-26 stsp new_fileindex_path = NULL;
1795 2ec1f75b 2019-03-26 stsp err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
1797 2ec1f75b 2019-03-26 stsp free(relpath);
1798 2ec1f75b 2019-03-26 stsp if (index) {
1799 2ec1f75b 2019-03-26 stsp if (fclose(index) != 0 && err == NULL)
1800 2ec1f75b 2019-03-26 stsp err = got_error_from_errno();
1802 2ec1f75b 2019-03-26 stsp if (new_fileindex_path) {
1803 2ec1f75b 2019-03-26 stsp if (unlink(new_fileindex_path) != 0 && err == NULL)
1804 2ec1f75b 2019-03-26 stsp err = got_error_from_errno();
1805 2ec1f75b 2019-03-26 stsp free(new_fileindex_path);
1807 2ec1f75b 2019-03-26 stsp if (fileindex)
1808 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
1809 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
1810 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
1811 2ec1f75b 2019-03-26 stsp err = unlockerr;
1812 2ec1f75b 2019-03-26 stsp return err;