2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 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 9d31a1d8 2018-03-11 stsp #include <sys/queue.h>
19 133d2798 2019-01-08 stsp #include <sys/tree.h>
21 d1f6d47b 2019-02-04 stsp #include <dirent.h>
22 12ce7a6c 2019-08-12 stsp #include <limits.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 303e14b5 2019-09-23 stsp #include <time.h>
28 86c3caaf 2018-03-09 stsp #include <fcntl.h>
29 86c3caaf 2018-03-09 stsp #include <errno.h>
30 86c3caaf 2018-03-09 stsp #include <unistd.h>
31 9d31a1d8 2018-03-11 stsp #include <sha1.h>
32 9d31a1d8 2018-03-11 stsp #include <zlib.h>
33 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
34 512f0d0e 2019-01-02 stsp #include <libgen.h>
35 ec22038e 2019-03-10 stsp #include <uuid.h>
36 7154f6ce 2019-03-27 stsp #include <util.h>
38 86c3caaf 2018-03-09 stsp #include "got_error.h"
39 86c3caaf 2018-03-09 stsp #include "got_repository.h"
40 5261c201 2018-04-01 stsp #include "got_reference.h"
41 9d31a1d8 2018-03-11 stsp #include "got_object.h"
42 1dd54920 2019-05-11 stsp #include "got_path.h"
43 e6209546 2019-08-22 stsp #include "got_cancel.h"
44 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
45 511a516b 2018-05-19 stsp #include "got_opentemp.h"
46 234035bc 2019-06-01 stsp #include "got_diff.h"
48 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
49 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
51 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
52 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
53 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
54 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
55 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
56 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
57 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
60 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
64 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
66 99724ed4 2018-03-10 stsp static const struct got_error *
67 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
69 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
72 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
73 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
75 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
80 507dc3bb 2018-12-29 stsp static const struct got_error *
81 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
83 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
84 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
85 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
86 507dc3bb 2018-12-29 stsp char *path = NULL;
88 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
89 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
90 507dc3bb 2018-12-29 stsp path = NULL;
94 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
98 507dc3bb 2018-12-29 stsp if (content) {
99 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
100 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
101 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
106 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
107 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
108 2a57020b 2019-02-20 stsp unlink(tmppath);
113 fb43ecf1 2019-02-11 stsp if (fclose(tmpfile) != 0 && err == NULL)
114 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
115 230a42bd 2019-05-11 jcs free(tmppath);
116 99724ed4 2018-03-10 stsp return err;
119 09fe317a 2018-03-11 stsp static const struct got_error *
120 7ac97322 2018-03-11 stsp read_meta_file(char **content, const char *path_got, const char *name)
122 09fe317a 2018-03-11 stsp const struct got_error *err = NULL;
123 09fe317a 2018-03-11 stsp char *path;
124 09fe317a 2018-03-11 stsp int fd = -1;
126 09fe317a 2018-03-11 stsp struct stat sb;
128 09fe317a 2018-03-11 stsp *content = NULL;
130 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
131 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
132 09fe317a 2018-03-11 stsp path = NULL;
136 ef99fdb1 2018-03-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
137 09fe317a 2018-03-11 stsp if (fd == -1) {
138 f02eaa22 2019-03-11 stsp if (errno == ENOENT)
139 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
141 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", path);
144 ef99fdb1 2018-03-11 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
145 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
146 638f9024 2019-05-13 stsp : got_error_from_errno2("flock", path));
150 e4b9a50c 2019-07-27 stsp if (fstat(fd, &sb) != 0) {
151 e4b9a50c 2019-07-27 stsp err = got_error_from_errno2("fstat", path);
154 09fe317a 2018-03-11 stsp *content = calloc(1, sb.st_size);
155 09fe317a 2018-03-11 stsp if (*content == NULL) {
156 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
160 09fe317a 2018-03-11 stsp n = read(fd, *content, sb.st_size);
161 09fe317a 2018-03-11 stsp if (n != sb.st_size) {
162 638f9024 2019-05-13 stsp err = (n == -1 ? got_error_from_errno2("read", path) :
163 a2e6d162 2019-07-27 stsp got_error_path(path, GOT_ERR_WORKTREE_META));
166 09fe317a 2018-03-11 stsp if ((*content)[sb.st_size - 1] != '\n') {
167 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
170 09fe317a 2018-03-11 stsp (*content)[sb.st_size - 1] = '\0';
173 09fe317a 2018-03-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
174 638f9024 2019-05-13 stsp err = got_error_from_errno2("close", path_got);
175 09fe317a 2018-03-11 stsp free(path);
177 09fe317a 2018-03-11 stsp free(*content);
178 09fe317a 2018-03-11 stsp *content = NULL;
180 09fe317a 2018-03-11 stsp return err;
183 024e9686 2019-05-14 stsp static const struct got_error *
184 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
186 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
187 024e9686 2019-05-14 stsp char *refstr = NULL;
189 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
190 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
191 024e9686 2019-05-14 stsp if (refstr == NULL)
192 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
194 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
195 024e9686 2019-05-14 stsp if (refstr == NULL)
196 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
198 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
199 024e9686 2019-05-14 stsp free(refstr);
200 024e9686 2019-05-14 stsp return err;
203 86c3caaf 2018-03-09 stsp const struct got_error *
204 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
205 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
207 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
208 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
209 ec22038e 2019-03-10 stsp uuid_t uuid;
210 ec22038e 2019-03-10 stsp uint32_t uuid_status;
211 65596e15 2018-12-24 stsp int obj_type;
212 7ac97322 2018-03-11 stsp char *path_got = NULL;
213 1451e70d 2018-03-10 stsp char *formatstr = NULL;
214 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
215 65596e15 2018-12-24 stsp char *basestr = NULL;
216 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
218 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
219 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
223 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
225 65596e15 2018-12-24 stsp return err;
226 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
228 65596e15 2018-12-24 stsp return err;
229 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
230 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
232 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
233 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
234 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
237 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
238 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
239 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
243 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
244 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
245 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
248 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
249 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
253 056e7441 2018-03-11 stsp /* Create an empty lock file. */
254 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
258 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
259 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
263 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
264 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
268 65596e15 2018-12-24 stsp /* Record our base commit. */
269 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
272 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
276 1451e70d 2018-03-10 stsp /* Store path to repository. */
277 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
278 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
282 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
283 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
284 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
288 ec22038e 2019-03-10 stsp /* Generate UUID. */
289 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
290 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
291 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
294 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
295 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
296 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
299 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
303 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
304 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
305 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
308 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
313 65596e15 2018-12-24 stsp free(commit_id);
314 7ac97322 2018-03-11 stsp free(path_got);
315 1451e70d 2018-03-10 stsp free(formatstr);
316 0bb8a95e 2018-03-12 stsp free(absprefix);
317 65596e15 2018-12-24 stsp free(basestr);
318 ec22038e 2019-03-10 stsp free(uuidstr);
319 86c3caaf 2018-03-09 stsp return err;
322 247140b2 2019-02-05 stsp static const struct got_error *
323 247140b2 2019-02-05 stsp open_worktree(struct got_worktree **worktree, const char *path)
325 6d9d28c3 2018-03-11 stsp const struct got_error *err = NULL;
326 7ac97322 2018-03-11 stsp char *path_got;
327 6d9d28c3 2018-03-11 stsp char *formatstr = NULL;
328 c442a90d 2019-03-10 stsp char *uuidstr = NULL;
329 056e7441 2018-03-11 stsp char *path_lock = NULL;
330 eaccb85f 2018-12-25 stsp char *base_commit_id_str = NULL;
331 6d9d28c3 2018-03-11 stsp int version, fd = -1;
332 6d9d28c3 2018-03-11 stsp const char *errstr;
333 eaccb85f 2018-12-25 stsp struct got_repository *repo = NULL;
334 c442a90d 2019-03-10 stsp uint32_t uuid_status;
336 6d9d28c3 2018-03-11 stsp *worktree = NULL;
338 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
339 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
340 7ac97322 2018-03-11 stsp path_got = NULL;
344 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
345 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
346 056e7441 2018-03-11 stsp path_lock = NULL;
350 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
351 6d9d28c3 2018-03-11 stsp if (fd == -1) {
352 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
353 638f9024 2019-05-13 stsp : got_error_from_errno2("open", path_lock));
357 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
361 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
362 6d9d28c3 2018-03-11 stsp if (errstr) {
363 a2e6d162 2019-07-27 stsp err = got_error_msg(GOT_ERR_WORKTREE_META,
364 a2e6d162 2019-07-27 stsp "could not parse work tree format version number");
367 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
368 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
372 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
373 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
374 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
377 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
379 7d61d891 2020-07-23 stsp (*worktree)->root_path = realpath(path, NULL);
380 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
381 7d61d891 2020-07-23 stsp err = got_error_from_errno2("realpath", path);
384 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
385 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
389 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
390 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
394 eaccb85f 2018-12-25 stsp err = read_meta_file(&base_commit_id_str, path_got,
395 0f92850e 2018-12-25 stsp GOT_WORKTREE_BASE_COMMIT);
399 c442a90d 2019-03-10 stsp err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
402 c442a90d 2019-03-10 stsp uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
403 c442a90d 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
404 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_from_string");
408 c9956ddf 2019-09-08 stsp err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
412 eaccb85f 2018-12-25 stsp err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
413 eaccb85f 2018-12-25 stsp base_commit_id_str);
417 36a38700 2019-05-10 stsp err = read_meta_file(&(*worktree)->head_ref_name, path_got,
418 36a38700 2019-05-10 stsp GOT_WORKTREE_HEAD_REF);
421 eaccb85f 2018-12-25 stsp got_repo_close(repo);
422 7ac97322 2018-03-11 stsp free(path_got);
423 056e7441 2018-03-11 stsp free(path_lock);
424 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
425 c442a90d 2019-03-10 stsp free(uuidstr);
426 bd165944 2019-03-10 stsp free(formatstr);
428 6d9d28c3 2018-03-11 stsp if (fd != -1)
430 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
431 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
432 6d9d28c3 2018-03-11 stsp *worktree = NULL;
434 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
436 6d9d28c3 2018-03-11 stsp return err;
439 247140b2 2019-02-05 stsp const struct got_error *
440 247140b2 2019-02-05 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
442 247140b2 2019-02-05 stsp const struct got_error *err = NULL;
445 247140b2 2019-02-05 stsp err = open_worktree(worktree, path);
446 f02eaa22 2019-03-11 stsp if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
447 247140b2 2019-02-05 stsp return err;
448 247140b2 2019-02-05 stsp if (*worktree)
449 247140b2 2019-02-05 stsp return NULL;
450 247140b2 2019-02-05 stsp path = dirname(path);
451 d1542a27 2019-02-05 stsp if (path == NULL)
452 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
453 d1542a27 2019-02-05 stsp } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
455 247140b2 2019-02-05 stsp return got_error(GOT_ERR_NOT_WORKTREE);
458 3a6ce05a 2019-02-11 stsp const struct got_error *
459 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
461 3a6ce05a 2019-02-11 stsp const struct got_error *err = NULL;
462 cde76477 2018-03-11 stsp free(worktree->repo_path);
463 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
464 eaccb85f 2018-12-25 stsp free(worktree->base_commit_id);
465 36a38700 2019-05-10 stsp free(worktree->head_ref_name);
466 056e7441 2018-03-11 stsp if (worktree->lockfd != -1)
467 3a6ce05a 2019-02-11 stsp if (close(worktree->lockfd) != 0)
468 638f9024 2019-05-13 stsp err = got_error_from_errno2("close",
469 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree));
470 7f11502c 2019-08-28 hiltjo free(worktree->root_path);
471 6d9d28c3 2018-03-11 stsp free(worktree);
472 3a6ce05a 2019-02-11 stsp return err;
475 2fbdb5ae 2018-12-29 stsp const char *
476 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
478 c7f4312f 2019-02-05 stsp return worktree->root_path;
481 c7f4312f 2019-02-05 stsp const char *
482 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
484 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
487 49520a32 2018-12-29 stsp const char *
488 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
490 f609be2e 2018-12-29 stsp return worktree->path_prefix;
493 e5dc7198 2018-12-29 stsp const struct got_error *
494 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
495 e5dc7198 2018-12-29 stsp const char *path_prefix)
497 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
499 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
500 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
501 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
503 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
504 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
505 e5dc7198 2018-12-29 stsp free(absprefix);
506 e5dc7198 2018-12-29 stsp return NULL;
509 bc70eb79 2019-05-09 stsp const char *
510 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
512 36a38700 2019-05-10 stsp return worktree->head_ref_name;
515 024e9686 2019-05-14 stsp const struct got_error *
516 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
517 024e9686 2019-05-14 stsp struct got_reference *head_ref)
519 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
520 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
522 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
523 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
524 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
525 024e9686 2019-05-14 stsp path_got = NULL;
529 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
530 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
531 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
535 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
539 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
540 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
542 024e9686 2019-05-14 stsp free(path_got);
544 024e9686 2019-05-14 stsp free(head_ref_name);
545 024e9686 2019-05-14 stsp return err;
548 b72f483a 2019-02-05 stsp struct got_object_id *
549 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
551 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
554 507dc3bb 2018-12-29 stsp const struct got_error *
555 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
556 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
558 507dc3bb 2018-12-29 stsp const struct got_error *err;
559 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
560 507dc3bb 2018-12-29 stsp char *id_str = NULL;
561 507dc3bb 2018-12-29 stsp char *path_got = NULL;
563 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
564 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
565 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
566 507dc3bb 2018-12-29 stsp path_got = NULL;
570 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
572 507dc3bb 2018-12-29 stsp return err;
574 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
575 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
579 507dc3bb 2018-12-29 stsp /* Record our base commit. */
580 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
583 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
587 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
588 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
589 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
590 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
595 507dc3bb 2018-12-29 stsp got_object_close(obj);
596 507dc3bb 2018-12-29 stsp free(id_str);
597 507dc3bb 2018-12-29 stsp free(path_got);
598 507dc3bb 2018-12-29 stsp return err;
601 9d31a1d8 2018-03-11 stsp static const struct got_error *
602 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
604 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
605 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
606 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
607 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
608 86c3caaf 2018-03-09 stsp return NULL;
611 21908da4 2019-01-13 stsp static const struct got_error *
612 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
614 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
615 4a1ddfc2 2019-01-12 stsp char *abspath;
617 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
618 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
620 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
621 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
622 ddcd8544 2019-03-11 stsp struct stat sb;
623 ddcd8544 2019-03-11 stsp err = NULL;
624 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
625 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
626 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
627 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
628 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
631 4a1ddfc2 2019-01-12 stsp free(abspath);
632 68c76935 2019-02-19 stsp return err;
635 68c76935 2019-02-19 stsp static const struct got_error *
636 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
638 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
639 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
640 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
641 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
646 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
647 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
648 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
651 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
652 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
653 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
656 68c76935 2019-02-19 stsp if (flen1 == 0) {
657 68c76935 2019-02-19 stsp if (flen2 != 0)
660 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
661 68c76935 2019-02-19 stsp if (flen1 != 0)
664 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
665 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
675 68c76935 2019-02-19 stsp return err;
678 68c76935 2019-02-19 stsp static const struct got_error *
679 68c76935 2019-02-19 stsp check_files_equal(int *same, const char *f1_path, const char *f2_path)
681 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
682 68c76935 2019-02-19 stsp struct stat sb;
683 68c76935 2019-02-19 stsp size_t size1, size2;
684 68c76935 2019-02-19 stsp FILE *f1 = NULL, *f2 = NULL;
688 68c76935 2019-02-19 stsp if (lstat(f1_path, &sb) != 0) {
689 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f1_path);
692 68c76935 2019-02-19 stsp size1 = sb.st_size;
694 68c76935 2019-02-19 stsp if (lstat(f2_path, &sb) != 0) {
695 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f2_path);
698 68c76935 2019-02-19 stsp size2 = sb.st_size;
700 68c76935 2019-02-19 stsp if (size1 != size2) {
702 68c76935 2019-02-19 stsp return NULL;
705 68c76935 2019-02-19 stsp f1 = fopen(f1_path, "r");
706 68c76935 2019-02-19 stsp if (f1 == NULL)
707 60522982 2019-12-15 stsp return got_error_from_errno2("fopen", f1_path);
709 68c76935 2019-02-19 stsp f2 = fopen(f2_path, "r");
710 68c76935 2019-02-19 stsp if (f2 == NULL) {
711 60522982 2019-12-15 stsp err = got_error_from_errno2("fopen", f2_path);
715 68c76935 2019-02-19 stsp err = check_file_contents_equal(same, f1, f2);
717 68c76935 2019-02-19 stsp if (f1 && fclose(f1) != 0 && err == NULL)
718 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
719 68c76935 2019-02-19 stsp if (f2 && fclose(f2) != 0 && err == NULL)
720 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
722 6353ad76 2019-02-08 stsp return err;
726 46b6ee73 2019-06-04 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
727 14c901f1 2019-08-08 stsp * the file at deriv_path acts as the first derived version, and the
728 14c901f1 2019-08-08 stsp * file on disk acts as the second derived version.
730 6353ad76 2019-02-08 stsp static const struct got_error *
731 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
732 46b6ee73 2019-06-04 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
733 14c901f1 2019-08-08 stsp const char *path, uint16_t st_mode, const char *deriv_path,
734 f69721c3 2019-10-21 stsp const char *label_orig, const char *label_deriv,
735 f69721c3 2019-10-21 stsp struct got_repository *repo,
736 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
738 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
739 6353ad76 2019-02-08 stsp int merged_fd = -1;
740 14c901f1 2019-08-08 stsp FILE *f_orig = NULL;
741 14c901f1 2019-08-08 stsp char *blob_orig_path = NULL;
742 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
743 234035bc 2019-06-01 stsp int overlapcnt = 0;
744 af54ae4a 2019-02-19 stsp char *parent;
745 3b9f0f87 2020-07-23 stsp char *symlink_path = NULL;
746 3b9f0f87 2020-07-23 stsp FILE *symlinkf = NULL;
748 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
750 af54ae4a 2019-02-19 stsp parent = dirname(ondisk_path);
751 af54ae4a 2019-02-19 stsp if (parent == NULL)
752 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", ondisk_path);
754 af54ae4a 2019-02-19 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1)
755 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
757 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
761 af54ae4a 2019-02-19 stsp free(base_path);
762 46b6ee73 2019-06-04 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
763 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
764 af54ae4a 2019-02-19 stsp base_path = NULL;
768 46b6ee73 2019-06-04 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
771 46b6ee73 2019-06-04 stsp if (blob_orig) {
772 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
773 46b6ee73 2019-06-04 stsp blob_orig);
778 1430b4e0 2019-03-27 stsp * If the file has no blob, this is an "add vs add" conflict,
779 1430b4e0 2019-03-27 stsp * and we simply use an empty ancestor file to make both files
780 1430b4e0 2019-03-27 stsp * appear in the merged result in their entirety.
785 3b9f0f87 2020-07-23 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
786 3b9f0f87 2020-07-23 stsp * target path into a temporary file and use that file with diff3.
788 3b9f0f87 2020-07-23 stsp if (S_ISLNK(st_mode)) {
789 3b9f0f87 2020-07-23 stsp char target_path[PATH_MAX];
790 3b9f0f87 2020-07-23 stsp ssize_t target_len;
793 3b9f0f87 2020-07-23 stsp free(base_path);
794 3b9f0f87 2020-07-23 stsp if (asprintf(&base_path, "%s/got-symlink-merge",
795 3b9f0f87 2020-07-23 stsp parent) == -1) {
796 3b9f0f87 2020-07-23 stsp err = got_error_from_errno("asprintf");
797 3b9f0f87 2020-07-23 stsp base_path = NULL;
800 3b9f0f87 2020-07-23 stsp err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
803 3b9f0f87 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
804 3b9f0f87 2020-07-23 stsp sizeof(target_path));
805 3b9f0f87 2020-07-23 stsp if (target_len == -1) {
806 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
809 3b9f0f87 2020-07-23 stsp n = fwrite(target_path, 1, target_len, symlinkf);
810 3b9f0f87 2020-07-23 stsp if (n != target_len) {
811 3b9f0f87 2020-07-23 stsp err = got_ferror(symlinkf, GOT_ERR_IO);
814 3b9f0f87 2020-07-23 stsp if (fflush(symlinkf) == EOF) {
815 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("fflush", symlink_path);
820 14c901f1 2019-08-08 stsp err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
821 3b9f0f87 2020-07-23 stsp blob_orig_path, symlink_path ? symlink_path : ondisk_path,
822 3b9f0f87 2020-07-23 stsp label_deriv, label_orig, NULL);
826 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
827 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
831 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
832 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
836 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
837 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
838 14c901f1 2019-08-08 stsp err = check_files_equal(local_changes_subsumed, deriv_path,
839 68c76935 2019-02-19 stsp merged_path);
844 2ad902c0 2019-12-15 stsp if (fchmod(merged_fd, st_mode) != 0) {
845 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
849 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
850 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
851 230a42bd 2019-05-11 jcs ondisk_path);
856 fdcb7daf 2019-12-15 stsp if (merged_path)
857 fdcb7daf 2019-12-15 stsp unlink(merged_path);
859 3b9f0f87 2020-07-23 stsp if (symlink_path) {
860 3b9f0f87 2020-07-23 stsp if (unlink(symlink_path) == -1 && err == NULL)
861 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("unlink", symlink_path);
863 3b9f0f87 2020-07-23 stsp if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
864 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("fclose", symlink_path);
865 3b9f0f87 2020-07-23 stsp free(symlink_path);
866 3a6ce05a 2019-02-11 stsp if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
867 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
868 46b6ee73 2019-06-04 stsp if (f_orig && fclose(f_orig) != 0 && err == NULL)
869 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
870 6353ad76 2019-02-08 stsp free(merged_path);
871 af54ae4a 2019-02-19 stsp free(base_path);
872 46b6ee73 2019-06-04 stsp if (blob_orig_path) {
873 46b6ee73 2019-06-04 stsp unlink(blob_orig_path);
874 46b6ee73 2019-06-04 stsp free(blob_orig_path);
876 af57b12a 2020-07-23 stsp return err;
879 af57b12a 2020-07-23 stsp static const struct got_error *
880 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
881 af57b12a 2020-07-23 stsp size_t target_len)
883 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
884 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
885 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
886 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
887 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
888 af57b12a 2020-07-23 stsp ondisk_path);
889 af57b12a 2020-07-23 stsp return NULL;
893 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
894 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
895 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
896 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
898 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
899 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
901 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
902 993e2a1b 2020-07-23 stsp * has deleted this symlink.
904 11cc08c1 2020-07-23 stsp static const struct got_error *
905 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
906 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
907 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
909 11cc08c1 2020-07-23 stsp const struct got_error *err;
910 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
911 11cc08c1 2020-07-23 stsp FILE *f = NULL;
913 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
915 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
917 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
918 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
919 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
923 11cc08c1 2020-07-23 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict");
927 11cc08c1 2020-07-23 stsp if (fprintf(f, "%s: Could not install symbolic link because of merge "
928 11cc08c1 2020-07-23 stsp "conflict.\nln(1) may be used to fix the situation. If this is "
929 11cc08c1 2020-07-23 stsp "intended to be a\nregular file instead then its expected "
930 11cc08c1 2020-07-23 stsp "contents may be filled in.\nThe following conflicting symlink "
931 11cc08c1 2020-07-23 stsp "target paths were found:\n"
932 11cc08c1 2020-07-23 stsp "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n", getprogname(),
933 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
934 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
935 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
936 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
937 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
938 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
939 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
940 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
941 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
945 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
946 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
949 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
950 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
953 11cc08c1 2020-07-23 stsp if (chmod(ondisk_path, GOT_DEFAULT_FILE_MODE) == -1) {
954 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("chmod", ondisk_path);
958 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
959 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
960 11cc08c1 2020-07-23 stsp free(path);
961 11cc08c1 2020-07-23 stsp free(id_str);
962 11cc08c1 2020-07-23 stsp free(label_deriv);
963 11cc08c1 2020-07-23 stsp return err;
966 d219f183 2020-07-23 stsp /* forward declaration */
967 d219f183 2020-07-23 stsp static const struct got_error *
968 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
969 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
970 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
971 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
974 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
975 af57b12a 2020-07-23 stsp * ancestor, blob_deriv acts as the first derived version, and the symlink
976 af57b12a 2020-07-23 stsp * on disk acts as the second derived version.
977 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
979 af57b12a 2020-07-23 stsp static const struct got_error *
980 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
981 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
982 af57b12a 2020-07-23 stsp const char *path, uint16_t st_mode, const char *label_orig,
983 af57b12a 2020-07-23 stsp struct got_blob_object *blob_deriv,
984 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
985 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
987 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
988 af57b12a 2020-07-23 stsp char *ancestor_target = NULL, *deriv_target = NULL;
989 af57b12a 2020-07-23 stsp struct stat sb;
990 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
991 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
992 11cc08c1 2020-07-23 stsp int have_local_change = 0;
993 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
995 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
996 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
998 af57b12a 2020-07-23 stsp if (!S_ISLNK(sb.st_mode)) {
1000 d219f183 2020-07-23 stsp * If there is a regular file on disk, merge the symlink
1001 d219f183 2020-07-23 stsp * target path into this file, which will usually cause
1002 d219f183 2020-07-23 stsp * a merge conflict.
1004 d219f183 2020-07-23 stsp if (S_ISREG(sb.st_mode)) {
1005 d219f183 2020-07-23 stsp int local_changes_subsumed;
1006 d219f183 2020-07-23 stsp return merge_blob(&local_changes_subsumed, worktree,
1007 d219f183 2020-07-23 stsp NULL, ondisk_path, path, sb.st_mode, label_orig,
1008 d219f183 2020-07-23 stsp blob_deriv, deriv_base_commit_id,
1009 d219f183 2020-07-23 stsp repo, progress_cb, progress_arg);
1012 af57b12a 2020-07-23 stsp /* TODO symlink is obstructed; do something */
1013 af57b12a 2020-07-23 stsp return got_error_path(ondisk_path, GOT_ERR_FILE_OBSTRUCTED);
1016 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
1017 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
1018 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
1019 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
1020 af57b12a 2020-07-23 stsp ondisk_path);
1021 af57b12a 2020-07-23 stsp goto done;
1023 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
1025 526a746f 2020-07-23 stsp if (blob_orig) {
1026 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1028 526a746f 2020-07-23 stsp goto done;
1031 af57b12a 2020-07-23 stsp err = got_object_blob_read_to_str(&deriv_target, blob_deriv);
1033 af57b12a 2020-07-23 stsp goto done;
1035 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
1036 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
1037 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1038 11cc08c1 2020-07-23 stsp have_local_change = 1;
1040 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
1041 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
1042 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
1043 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1044 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
1046 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
1047 11cc08c1 2020-07-23 stsp if (ancestor_target) {
1048 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
1049 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1051 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
1052 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1053 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
1054 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1057 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
1058 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1059 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
1060 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
1062 11cc08c1 2020-07-23 stsp goto done;
1063 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1066 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
1067 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
1068 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
1069 af57b12a 2020-07-23 stsp strlen(deriv_target));
1071 af57b12a 2020-07-23 stsp goto done;
1072 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1073 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
1074 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1075 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
1076 11cc08c1 2020-07-23 stsp ondisk_target, ondisk_path);
1078 11cc08c1 2020-07-23 stsp goto done;
1079 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1084 af57b12a 2020-07-23 stsp free(ancestor_target);
1085 af57b12a 2020-07-23 stsp free(deriv_target);
1086 14c901f1 2019-08-08 stsp return err;
1090 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
1091 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
1092 14c901f1 2019-08-08 stsp * acts as the second derived version.
1094 14c901f1 2019-08-08 stsp static const struct got_error *
1095 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1096 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
1097 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
1098 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
1099 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1100 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1102 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
1103 14c901f1 2019-08-08 stsp FILE *f_deriv = NULL;
1104 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1105 14c901f1 2019-08-08 stsp char *label_deriv = NULL, *parent;
1107 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
1109 14c901f1 2019-08-08 stsp parent = dirname(ondisk_path);
1110 14c901f1 2019-08-08 stsp if (parent == NULL)
1111 14c901f1 2019-08-08 stsp return got_error_from_errno2("dirname", ondisk_path);
1113 14c901f1 2019-08-08 stsp free(base_path);
1114 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1115 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1116 14c901f1 2019-08-08 stsp base_path = NULL;
1117 14c901f1 2019-08-08 stsp goto done;
1120 14c901f1 2019-08-08 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1122 14c901f1 2019-08-08 stsp goto done;
1123 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1124 14c901f1 2019-08-08 stsp blob_deriv);
1126 14c901f1 2019-08-08 stsp goto done;
1128 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1130 14c901f1 2019-08-08 stsp goto done;
1131 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1132 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1133 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1134 14c901f1 2019-08-08 stsp goto done;
1137 14c901f1 2019-08-08 stsp err = merge_file(local_changes_subsumed, worktree, blob_orig,
1138 f69721c3 2019-10-21 stsp ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1139 f69721c3 2019-10-21 stsp label_deriv, repo, progress_cb, progress_arg);
1141 14c901f1 2019-08-08 stsp if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1142 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1143 14c901f1 2019-08-08 stsp free(base_path);
1144 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1145 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1146 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1148 6353ad76 2019-02-08 stsp free(id_str);
1149 818c7501 2019-07-11 stsp free(label_deriv);
1150 4a1ddfc2 2019-01-12 stsp return err;
1153 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1154 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1155 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1156 65b05cec 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_object_id *blob_id)
1158 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1159 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1161 65b05cec 2020-07-23 stsp *new_iep = NULL;
1163 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1165 054041d0 2020-06-24 stsp return err;
1167 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(new_ie, ondisk_path,
1168 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1170 054041d0 2020-06-24 stsp goto done;
1172 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1175 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1177 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1178 13d9040b 2019-03-27 stsp return err;
1181 1ebedb77 2019-10-19 stsp static mode_t
1182 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1184 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1186 1ebedb77 2019-10-19 stsp if (executable) {
1187 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1188 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1189 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1190 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1191 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1192 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1195 1ebedb77 2019-10-19 stsp return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1198 8ba819a3 2020-07-23 stsp /* forward declaration */
1199 13d9040b 2019-03-27 stsp static const struct got_error *
1200 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1201 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1202 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1203 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1204 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1205 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1208 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1209 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1211 8ba819a3 2020-07-23 stsp static const struct got_error *
1212 5a1fbc73 2020-07-23 stsp replace_existing_symlink(const char *ondisk_path, const char *target_path,
1213 5a1fbc73 2020-07-23 stsp size_t target_len)
1215 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1216 5a1fbc73 2020-07-23 stsp ssize_t elen;
1217 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1221 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1222 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1223 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1224 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1225 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1226 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1228 5a1fbc73 2020-07-23 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1229 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1230 5a1fbc73 2020-07-23 stsp if (errno != ELOOP)
1231 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1233 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1234 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1235 5a1fbc73 2020-07-23 stsp if (elen == -1)
1236 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1238 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1239 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1240 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1243 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1244 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1245 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1246 5a1fbc73 2020-07-23 stsp return err;
1249 5a1fbc73 2020-07-23 stsp static const struct got_error *
1250 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1251 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1252 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1253 c90c8ce3 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1254 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1256 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1257 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1258 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1259 8ba819a3 2020-07-23 stsp char *resolved_path = NULL, *abspath = NULL;
1260 906c123b 2020-07-23 stsp char *path_got = NULL;
1261 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1262 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1264 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1267 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1268 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1269 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1270 8ba819a3 2020-07-23 stsp * in the blob object.
1273 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1274 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1275 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1276 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1277 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1278 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1279 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1280 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1281 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1282 3b9f0f87 2020-07-23 stsp progress_arg);
1284 8ba819a3 2020-07-23 stsp if (len > 0) {
1285 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1286 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1287 8ba819a3 2020-07-23 stsp len - hdrlen);
1288 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1289 8ba819a3 2020-07-23 stsp hdrlen = 0;
1291 8ba819a3 2020-07-23 stsp } while (len != 0);
1292 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1295 8ba819a3 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1296 8ba819a3 2020-07-23 stsp * in which the blob object is being installed.
1298 8ba819a3 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1299 8ba819a3 2020-07-23 stsp char *parent = dirname(ondisk_path);
1300 ef68ca6f 2020-07-23 stsp if (parent == NULL) {
1301 ef68ca6f 2020-07-23 stsp err = got_error_from_errno2("dirname", ondisk_path);
1302 ef68ca6f 2020-07-23 stsp goto done;
1304 8ba819a3 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1305 8ba819a3 2020-07-23 stsp err = got_error_from_errno("asprintf");
1306 8ba819a3 2020-07-23 stsp goto done;
1311 8ba819a3 2020-07-23 stsp * unveil(2) restricts our view of paths in the filesystem.
1312 8ba819a3 2020-07-23 stsp * ENOENT will occur if a link target path does not exist or
1313 8ba819a3 2020-07-23 stsp * if it points outside our unveiled path space.
1315 8ba819a3 2020-07-23 stsp resolved_path = realpath(abspath ? abspath : target_path, NULL);
1316 8ba819a3 2020-07-23 stsp if (resolved_path == NULL) {
1317 b15bc87b 2020-07-23 stsp if (errno != ENOENT) {
1318 b15bc87b 2020-07-23 stsp err = got_error_from_errno2("realpath", target_path);
1319 b15bc87b 2020-07-23 stsp goto done;
1323 8ba819a3 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1324 0ab20ee9 2020-07-23 stsp if (!got_path_is_child(resolved_path ? resolved_path : (abspath ?
1325 0ab20ee9 2020-07-23 stsp abspath : target_path), worktree->root_path,
1326 0ab20ee9 2020-07-23 stsp strlen(worktree->root_path))) {
1327 8ba819a3 2020-07-23 stsp /* install as a regular file */
1328 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1329 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1330 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1331 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1332 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1333 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1334 8ba819a3 2020-07-23 stsp goto done;
1337 906c123b 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1338 906c123b 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
1339 906c123b 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1) {
1340 906c123b 2020-07-23 stsp err = got_error_from_errno("asprintf");
1341 906c123b 2020-07-23 stsp goto done;
1343 906c123b 2020-07-23 stsp if (got_path_is_child(resolved_path ? resolved_path : (abspath ?
1344 906c123b 2020-07-23 stsp abspath : target_path), path_got, strlen(path_got))) {
1345 906c123b 2020-07-23 stsp /* install as a regular file */
1346 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1347 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1348 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1349 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1350 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1351 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1352 906c123b 2020-07-23 stsp goto done;
1355 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1356 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1357 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1358 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1359 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1360 c90c8ce3 2020-07-23 stsp goto done;
1362 5a1fbc73 2020-07-23 stsp err = replace_existing_symlink(ondisk_path,
1363 5a1fbc73 2020-07-23 stsp target_path, target_len);
1365 f35fa46a 2020-07-23 stsp goto done;
1366 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1367 5a1fbc73 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1368 6e1eade5 2020-07-23 stsp reverting_versioned_file ?
1369 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1372 5a1fbc73 2020-07-23 stsp goto done; /* Nothing else to do. */
1375 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1376 f35fa46a 2020-07-23 stsp char *parent = dirname(ondisk_path);
1377 f35fa46a 2020-07-23 stsp if (parent == NULL) {
1378 f35fa46a 2020-07-23 stsp err = got_error_from_errno2("dirname",
1379 f35fa46a 2020-07-23 stsp ondisk_path);
1380 f35fa46a 2020-07-23 stsp goto done;
1382 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1384 f35fa46a 2020-07-23 stsp goto done;
1386 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1387 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1389 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1390 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1391 f35fa46a 2020-07-23 stsp goto done;
1395 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1396 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1397 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1398 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1399 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1400 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1401 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1402 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1403 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1404 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1405 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1406 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1407 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1409 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1410 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1412 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1413 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1414 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1416 8ba819a3 2020-07-23 stsp free(resolved_path);
1417 8ba819a3 2020-07-23 stsp free(abspath);
1418 906c123b 2020-07-23 stsp free(path_got);
1419 8ba819a3 2020-07-23 stsp return err;
1422 8ba819a3 2020-07-23 stsp static const struct got_error *
1423 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1424 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1425 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1426 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1427 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1428 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1430 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1431 507dc3bb 2018-12-29 stsp int fd = -1;
1432 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1433 507dc3bb 2018-12-29 stsp int update = 0;
1434 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1436 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1437 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
1438 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1439 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1440 21908da4 2019-01-13 stsp char *parent = dirname(path);
1441 21908da4 2019-01-13 stsp if (parent == NULL)
1442 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
1443 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1445 21908da4 2019-01-13 stsp return err;
1446 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1447 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1448 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
1449 21908da4 2019-01-13 stsp if (fd == -1)
1450 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1451 230a42bd 2019-05-11 jcs ondisk_path);
1452 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1453 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1454 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1455 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1456 3b9f0f87 2020-07-23 stsp goto done;
1458 bd6aa359 2020-07-23 stsp if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1459 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1460 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1461 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1462 507dc3bb 2018-12-29 stsp goto done;
1464 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1465 507dc3bb 2018-12-29 stsp ondisk_path);
1467 507dc3bb 2018-12-29 stsp goto done;
1468 507dc3bb 2018-12-29 stsp update = 1;
1471 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1474 bd6aa359 2020-07-23 stsp if (progress_cb) {
1475 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1476 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1478 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1479 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1482 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1483 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1485 bd6aa359 2020-07-23 stsp goto done;
1488 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1490 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1491 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1494 9d31a1d8 2018-03-11 stsp if (len > 0) {
1495 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1496 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1497 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1498 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1499 b87c6f83 2018-12-24 stsp goto done;
1500 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1501 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1502 b87c6f83 2018-12-24 stsp goto done;
1504 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1506 9d31a1d8 2018-03-11 stsp } while (len != 0);
1508 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1509 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1510 816dc654 2019-02-16 stsp goto done;
1513 507dc3bb 2018-12-29 stsp if (update) {
1514 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1515 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1516 230a42bd 2019-05-11 jcs ondisk_path);
1517 2a57020b 2019-02-20 stsp unlink(tmppath);
1518 68ed9ba5 2019-02-10 stsp goto done;
1522 1ebedb77 2019-10-19 stsp if (chmod(ondisk_path,
1523 1ebedb77 2019-10-19 stsp get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1524 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("chmod", ondisk_path);
1525 1ebedb77 2019-10-19 stsp goto done;
1529 3a6ce05a 2019-02-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
1530 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1531 507dc3bb 2018-12-29 stsp free(tmppath);
1532 6353ad76 2019-02-08 stsp return err;
1535 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1536 6353ad76 2019-02-08 stsp static const struct got_error *
1537 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1539 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1540 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1541 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1542 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1543 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1545 7154f6ce 2019-03-27 stsp int i = 0;
1546 7154f6ce 2019-03-27 stsp char *line;
1547 7154f6ce 2019-03-27 stsp size_t len;
1548 7154f6ce 2019-03-27 stsp const char delim[3] = {'\0', '\0', '\0'};
1550 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1551 7154f6ce 2019-03-27 stsp line = fparseln(f, &len, NULL, delim, 0);
1552 7154f6ce 2019-03-27 stsp if (line == NULL) {
1553 7154f6ce 2019-03-27 stsp if (feof(f))
1555 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1559 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1560 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1562 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1568 7154f6ce 2019-03-27 stsp return err;
1571 e2b1e152 2019-07-13 stsp static int
1572 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1574 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1575 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1578 1ebedb77 2019-10-19 stsp static int
1579 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1581 e2b1e152 2019-07-13 stsp return !(ie->ctime_sec == sb->st_ctime &&
1582 e2b1e152 2019-07-13 stsp ie->ctime_nsec == sb->st_ctimensec &&
1583 e2b1e152 2019-07-13 stsp ie->mtime_sec == sb->st_mtime &&
1584 e2b1e152 2019-07-13 stsp ie->mtime_nsec == sb->st_mtimensec &&
1585 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1586 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1589 c363b2c1 2019-08-03 stsp static unsigned char
1590 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1592 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1593 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1594 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1595 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1596 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1597 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1598 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1600 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1604 a919d5c4 2020-07-23 stsp static const struct got_error *
1605 a919d5c4 2020-07-23 stsp get_symlink_status(unsigned char *status, struct stat *sb,
1606 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1607 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1609 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1610 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1611 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1612 a919d5c4 2020-07-23 stsp ssize_t elen;
1613 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1614 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1615 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1617 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1619 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1621 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1623 a919d5c4 2020-07-23 stsp return err;
1624 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1626 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1627 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1629 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1631 a919d5c4 2020-07-23 stsp if (len > 0) {
1632 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1633 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1634 a919d5c4 2020-07-23 stsp len - hdrlen);
1635 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1636 a919d5c4 2020-07-23 stsp hdrlen = 0;
1638 a919d5c4 2020-07-23 stsp } while (len != 0);
1639 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1641 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1642 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1643 a919d5c4 2020-07-23 stsp if (elen == -1)
1644 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1646 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1647 a919d5c4 2020-07-23 stsp if (elen == -1)
1648 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1651 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1652 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1654 a919d5c4 2020-07-23 stsp return NULL;
1657 7154f6ce 2019-03-27 stsp static const struct got_error *
1658 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1659 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1660 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1662 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1663 6353ad76 2019-02-08 stsp struct got_object_id id;
1664 6353ad76 2019-02-08 stsp size_t hdrlen;
1665 1338848f 2019-12-13 stsp int fd = -1;
1666 6353ad76 2019-02-08 stsp FILE *f = NULL;
1667 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1668 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1669 6353ad76 2019-02-08 stsp size_t flen, blen;
1670 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1672 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1675 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1676 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1677 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1679 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1680 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1681 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1682 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1683 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1685 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1686 882ef1b9 2019-12-13 stsp goto done;
1688 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1689 3d35a492 2019-12-13 stsp goto done;
1692 7f91a133 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1693 a919d5c4 2020-07-23 stsp if (fd == -1 && errno != ENOENT && errno != ELOOP)
1694 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1695 a919d5c4 2020-07-23 stsp else if (fd == -1 && errno == ELOOP) {
1696 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1697 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1698 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1699 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1700 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1701 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1703 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1704 3d35a492 2019-12-13 stsp goto done;
1706 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1707 1338848f 2019-12-13 stsp goto done;
1711 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1712 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1713 1338848f 2019-12-13 stsp goto done;
1716 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1717 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1718 1338848f 2019-12-13 stsp goto done;
1719 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1720 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1721 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1722 1338848f 2019-12-13 stsp goto done;
1725 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1726 1338848f 2019-12-13 stsp goto done;
1728 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1729 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1730 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1732 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1734 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1736 1338848f 2019-12-13 stsp goto done;
1738 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1739 a919d5c4 2020-07-23 stsp err = get_symlink_status(status, sb, ie, abspath, dirfd,
1740 a919d5c4 2020-07-23 stsp de_name, blob);
1741 a919d5c4 2020-07-23 stsp goto done;
1745 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1746 3d35a492 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1747 ab0d4361 2019-12-13 stsp if (fd == -1) {
1748 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1749 ab0d4361 2019-12-13 stsp goto done;
1753 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1754 6353ad76 2019-02-08 stsp if (f == NULL) {
1755 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1756 6353ad76 2019-02-08 stsp goto done;
1759 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1761 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1762 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1764 7154f6ce 2019-03-27 stsp goto done;
1765 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1766 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1767 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1768 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1769 7154f6ce 2019-03-27 stsp goto done;
1771 6353ad76 2019-02-08 stsp if (blen == 0) {
1772 6353ad76 2019-02-08 stsp if (flen != 0)
1773 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1775 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1776 6353ad76 2019-02-08 stsp if (blen != 0)
1777 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1779 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1780 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1781 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1782 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1786 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1789 6353ad76 2019-02-08 stsp hdrlen = 0;
1792 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1793 7154f6ce 2019-03-27 stsp rewind(f);
1794 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1795 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1796 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1799 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1800 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1801 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1802 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1803 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1804 9d31a1d8 2018-03-11 stsp return err;
1808 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1809 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1811 e2b1e152 2019-07-13 stsp static const struct got_error *
1812 e2b1e152 2019-07-13 stsp sync_timestamps(char *ondisk_path, unsigned char status,
1813 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1815 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1816 e2b1e152 2019-07-13 stsp return got_fileindex_entry_update(ie, ondisk_path,
1817 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1819 e2b1e152 2019-07-13 stsp return NULL;
1822 9d31a1d8 2018-03-11 stsp static const struct got_error *
1823 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1824 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1825 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1826 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1827 0584f854 2019-04-06 stsp void *progress_arg)
1829 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1830 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1831 6353ad76 2019-02-08 stsp char *ondisk_path;
1832 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1833 b8f41171 2019-02-10 stsp struct stat sb;
1835 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1836 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1839 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1840 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1841 a76c42e6 2019-08-03 stsp goto done;
1843 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1846 abb4604f 2019-07-27 stsp goto done;
1847 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1848 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1850 abb4604f 2019-07-27 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1851 c90c8ce3 2020-07-23 stsp status = GOT_STATUS_UNVERSIONED;
1854 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1855 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1856 b8f41171 2019-02-10 stsp goto done;
1858 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1859 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1861 5036ab18 2020-04-18 stsp goto done;
1864 523b8417 2019-10-19 stsp if (ie && status != GOT_STATUS_MISSING &&
1865 523b8417 2019-10-19 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1866 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1867 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1868 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1869 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1871 e2b1e152 2019-07-13 stsp goto done;
1872 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1874 6353ad76 2019-02-08 stsp goto done;
1876 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1877 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1878 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1879 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1880 b8f41171 2019-02-10 stsp goto done;
1884 56e0773d 2019-11-28 stsp err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1886 6353ad76 2019-02-08 stsp goto done;
1888 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1889 234035bc 2019-06-01 stsp int update_timestamps;
1890 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1891 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1892 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1893 234035bc 2019-06-01 stsp struct got_object_id id2;
1894 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1895 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1897 f69721c3 2019-10-21 stsp goto done;
1899 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
1900 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
1901 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1902 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
1903 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
1904 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
1905 f69721c3 2019-10-21 stsp goto done;
1907 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
1908 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
1909 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
1910 234035bc 2019-06-01 stsp goto done;
1913 993e2a1b 2020-07-23 stsp if (S_ISLNK(te->mode)) {
1914 993e2a1b 2020-07-23 stsp err = merge_symlink(worktree, blob2,
1915 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig,
1916 993e2a1b 2020-07-23 stsp blob, worktree->base_commit_id, repo,
1917 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
1919 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1920 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
1921 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
1922 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
1924 f69721c3 2019-10-21 stsp free(label_orig);
1925 234035bc 2019-06-01 stsp if (blob2)
1926 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1928 909d120e 2019-09-22 stsp goto done;
1930 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1931 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1932 234035bc 2019-06-01 stsp * unmodified files again.
1934 234035bc 2019-06-01 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1935 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1936 234035bc 2019-06-01 stsp update_timestamps);
1937 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
1938 1ebedb77 2019-10-19 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1939 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1940 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1941 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1943 1ee397ad 2019-07-12 stsp goto done;
1944 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1945 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1947 13d9040b 2019-03-27 stsp goto done;
1949 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
1950 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
1951 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
1952 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
1953 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
1954 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
1955 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
1957 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1958 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
1959 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
1960 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
1961 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
1964 13d9040b 2019-03-27 stsp goto done;
1967 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1968 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 1);
1970 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
1971 054041d0 2020-06-24 stsp worktree->base_commit_id, ondisk_path, path,
1972 054041d0 2020-06-24 stsp &blob->id);
1975 13d9040b 2019-03-27 stsp goto done;
1977 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
1978 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
1979 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
1982 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
1984 6353ad76 2019-02-08 stsp free(ondisk_path);
1985 8da9e5f4 2019-01-12 stsp return err;
1988 90285c3b 2019-01-08 stsp static const struct got_error *
1989 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
1991 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
1992 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
1994 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1995 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1997 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
1998 c97665ae 2019-01-13 stsp if (errno != ENOENT)
1999 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2001 90285c3b 2019-01-08 stsp char *parent = dirname(ondisk_path);
2002 7a9df742 2019-01-08 stsp while (parent && strcmp(parent, root_path) != 0) {
2003 26c4ac4d 2019-01-08 stsp if (rmdir(parent) == -1) {
2004 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2005 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2009 90285c3b 2019-01-08 stsp parent = dirname(parent);
2012 90285c3b 2019-01-08 stsp free(ondisk_path);
2013 90285c3b 2019-01-08 stsp return err;
2016 708d8e67 2019-03-27 stsp static const struct got_error *
2017 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2018 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2019 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2021 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2022 708d8e67 2019-03-27 stsp unsigned char status;
2023 708d8e67 2019-03-27 stsp struct stat sb;
2024 708d8e67 2019-03-27 stsp char *ondisk_path;
2026 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2027 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2029 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2031 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2033 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2035 5a58a424 2020-06-23 stsp goto done;
2037 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2038 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2039 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2040 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2041 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2042 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2043 993e2a1b 2020-07-23 stsp goto done;
2045 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2046 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2047 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2048 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2050 993e2a1b 2020-07-23 stsp goto done;
2051 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2052 993e2a1b 2020-07-23 stsp ie->path);
2053 993e2a1b 2020-07-23 stsp goto done;
2056 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2057 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2058 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2060 5a58a424 2020-06-23 stsp goto done;
2062 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2063 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2065 fc6346c4 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2068 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2070 5a58a424 2020-06-23 stsp goto done;
2071 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2072 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2074 5a58a424 2020-06-23 stsp goto done;
2076 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2079 5a58a424 2020-06-23 stsp free(ondisk_path);
2080 fc6346c4 2019-03-27 stsp return err;
2083 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2084 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2085 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2086 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2087 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2088 8da9e5f4 2019-01-12 stsp void *progress_arg;
2089 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2090 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2093 512f0d0e 2019-01-02 stsp static const struct got_error *
2094 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2095 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2097 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2099 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2100 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2102 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2103 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2106 8da9e5f4 2019-01-12 stsp static const struct got_error *
2107 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2109 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2111 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2112 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2114 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2115 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2118 9d31a1d8 2018-03-11 stsp static const struct got_error *
2119 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2121 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2122 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2123 8da9e5f4 2019-01-12 stsp char *path;
2125 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2126 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2128 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2129 63c5ca5d 2019-08-24 stsp return NULL;
2131 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2132 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2134 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2136 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2137 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2139 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2140 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2142 8da9e5f4 2019-01-12 stsp free(path);
2143 0cd1c46a 2019-03-11 stsp return err;
2146 818c7501 2019-07-11 stsp static const struct got_error *
2147 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2149 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2150 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2151 0cd1c46a 2019-03-11 stsp uint32_t uuid_status;
2153 517bab73 2019-03-11 stsp *refname = NULL;
2155 0cd1c46a 2019-03-11 stsp uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
2156 0cd1c46a 2019-03-11 stsp if (uuid_status != uuid_s_ok)
2157 cc483380 2019-09-01 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2159 818c7501 2019-07-11 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr)
2161 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2162 0647c563 2019-03-11 stsp *refname = NULL;
2164 517bab73 2019-03-11 stsp free(uuidstr);
2165 517bab73 2019-03-11 stsp return err;
2168 818c7501 2019-07-11 stsp const struct got_error *
2169 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2171 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2174 818c7501 2019-07-11 stsp static const struct got_error *
2175 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2177 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2178 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2181 818c7501 2019-07-11 stsp static const struct got_error *
2182 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2184 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2187 818c7501 2019-07-11 stsp static const struct got_error *
2188 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2190 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2191 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2194 818c7501 2019-07-11 stsp static const struct got_error *
2195 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2197 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2198 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2201 0ebf8283 2019-07-24 stsp static const struct got_error *
2202 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2204 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2205 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2208 0ebf8283 2019-07-24 stsp static const struct got_error *
2209 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2211 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2212 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2215 0ebf8283 2019-07-24 stsp static const struct got_error *
2216 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2218 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2219 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2222 0ebf8283 2019-07-24 stsp static const struct got_error *
2223 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2225 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2226 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2229 0ebf8283 2019-07-24 stsp const struct got_error *
2230 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2231 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2233 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2234 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2235 0ebf8283 2019-07-24 stsp *path = NULL;
2236 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2238 0ebf8283 2019-07-24 stsp return NULL;
2242 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2243 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2245 517bab73 2019-03-11 stsp static const struct got_error *
2246 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2248 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2249 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2250 517bab73 2019-03-11 stsp char *refname;
2252 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2254 517bab73 2019-03-11 stsp return err;
2256 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2258 0cd1c46a 2019-03-11 stsp goto done;
2260 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2262 0cd1c46a 2019-03-11 stsp free(refname);
2264 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2265 3e3a69f1 2019-07-25 stsp return err;
2268 3e3a69f1 2019-07-25 stsp static const struct got_error *
2269 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2271 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2273 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2274 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2275 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2276 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2278 9d31a1d8 2018-03-11 stsp return err;
2282 ebf99748 2019-05-09 stsp static const struct got_error *
2283 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2284 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2286 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2287 ebf99748 2019-05-09 stsp FILE *index = NULL;
2289 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2290 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2291 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2292 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2294 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2296 ebf99748 2019-05-09 stsp goto done;
2298 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
2299 ebf99748 2019-05-09 stsp if (index == NULL) {
2300 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2301 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2303 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2304 ebf99748 2019-05-09 stsp if (fclose(index) != 0 && err == NULL)
2305 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2308 ebf99748 2019-05-09 stsp if (err) {
2309 ebf99748 2019-05-09 stsp free(*fileindex_path);
2310 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2311 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2312 ebf99748 2019-05-09 stsp *fileindex = NULL;
2314 ebf99748 2019-05-09 stsp return err;
2317 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2318 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2319 c932eeeb 2019-05-22 stsp const char *path;
2320 c932eeeb 2019-05-22 stsp size_t path_len;
2321 c932eeeb 2019-05-22 stsp const char *entry_name;
2322 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2323 a484d721 2019-06-10 stsp void *progress_arg;
2326 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2327 c932eeeb 2019-05-22 stsp static const struct got_error *
2328 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2330 1ee397ad 2019-07-12 stsp const struct got_error *err;
2331 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2333 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2334 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2335 c932eeeb 2019-05-22 stsp return NULL;
2336 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2337 102ff934 2019-06-11 stsp return NULL;
2339 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2340 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2341 c932eeeb 2019-05-22 stsp return NULL;
2343 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2344 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2345 edd02c5e 2019-07-11 stsp ie->path);
2347 1ee397ad 2019-07-12 stsp return err;
2349 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2350 c932eeeb 2019-05-22 stsp return NULL;
2353 9c6338c4 2019-06-02 stsp static const struct got_error *
2354 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2356 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2357 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2358 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2359 867630bb 2020-01-17 stsp struct timespec timeout;
2361 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2362 9c6338c4 2019-06-02 stsp fileindex_path);
2364 9c6338c4 2019-06-02 stsp goto done;
2366 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2368 9c6338c4 2019-06-02 stsp goto done;
2370 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2371 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2372 9c6338c4 2019-06-02 stsp fileindex_path);
2373 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2377 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2378 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2379 867630bb 2020-01-17 stsp * was recorded in the file index.
2381 867630bb 2020-01-17 stsp timeout.tv_sec = 0;
2382 867630bb 2020-01-17 stsp timeout.tv_nsec = 1;
2383 867630bb 2020-01-17 stsp nanosleep(&timeout, NULL);
2385 9c6338c4 2019-06-02 stsp if (new_index)
2386 9c6338c4 2019-06-02 stsp fclose(new_index);
2387 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2388 9c6338c4 2019-06-02 stsp return err;
2391 6ced4176 2019-07-12 stsp static const struct got_error *
2392 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2393 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2394 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
2396 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2397 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2398 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2399 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2401 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2402 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2403 6ced4176 2019-07-12 stsp *tree_id = NULL;
2405 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2406 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2407 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2408 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2409 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2410 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2411 6ced4176 2019-07-12 stsp goto done;
2413 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2414 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
2416 6ced4176 2019-07-12 stsp goto done;
2417 6ced4176 2019-07-12 stsp return NULL;
2420 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2422 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2423 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2424 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2425 6ced4176 2019-07-12 stsp goto done;
2428 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2429 6ced4176 2019-07-12 stsp in_repo_path);
2431 6ced4176 2019-07-12 stsp goto done;
2433 6ced4176 2019-07-12 stsp free(in_repo_path);
2434 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2436 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2438 6ced4176 2019-07-12 stsp goto done;
2440 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2441 6ced4176 2019-07-12 stsp /* Check out a single file. */
2442 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2443 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2444 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2445 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2446 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2447 6ced4176 2019-07-12 stsp goto done;
2449 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2450 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2451 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2452 6ced4176 2019-07-12 stsp goto done;
2455 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2456 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2458 6ced4176 2019-07-12 stsp return err;
2459 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2460 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2461 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2462 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2463 6ced4176 2019-07-12 stsp goto done;
2466 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2467 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
2469 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2470 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2471 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2472 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2473 6ced4176 2019-07-12 stsp goto done;
2475 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2476 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2477 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2478 6ced4176 2019-07-12 stsp goto done;
2483 6ced4176 2019-07-12 stsp free(in_repo_path);
2484 6ced4176 2019-07-12 stsp if (err) {
2485 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2486 6ced4176 2019-07-12 stsp free(*tree_relpath);
2487 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2488 6ced4176 2019-07-12 stsp free(*tree_id);
2489 6ced4176 2019-07-12 stsp *tree_id = NULL;
2491 07ed472d 2019-07-12 stsp return err;
2494 07ed472d 2019-07-12 stsp static const struct got_error *
2495 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2496 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2497 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2498 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2500 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2501 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2502 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2503 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2504 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2506 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2507 7f47418f 2019-12-20 stsp if (err) {
2508 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2509 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2510 7f47418f 2019-12-20 stsp goto done;
2511 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2512 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2514 7f47418f 2019-12-20 stsp return err;
2517 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2518 07ed472d 2019-07-12 stsp worktree->base_commit_id);
2520 07ed472d 2019-07-12 stsp goto done;
2522 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2524 07ed472d 2019-07-12 stsp goto done;
2526 07ed472d 2019-07-12 stsp if (entry_name &&
2527 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2528 07ed472d 2019-07-12 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
2529 07ed472d 2019-07-12 stsp goto done;
2532 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2533 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2534 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2535 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2536 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2537 07ed472d 2019-07-12 stsp arg.repo = repo;
2538 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2539 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2540 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2541 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2542 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2543 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2546 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2547 07ed472d 2019-07-12 stsp if (commit)
2548 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2549 6ced4176 2019-07-12 stsp return err;
2552 9d31a1d8 2018-03-11 stsp const struct got_error *
2553 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2554 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2555 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2556 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2558 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2559 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2560 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2561 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2562 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2563 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2564 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2565 f2ea84fa 2019-07-27 stsp SIMPLEQ_ENTRY(tree_path_data) entry;
2566 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2567 f2ea84fa 2019-07-27 stsp int entry_type;
2568 f2ea84fa 2019-07-27 stsp char *relpath;
2569 f2ea84fa 2019-07-27 stsp char *entry_name;
2570 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2571 f2ea84fa 2019-07-27 stsp SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2573 f2ea84fa 2019-07-27 stsp SIMPLEQ_INIT(&tree_paths);
2575 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2577 9d31a1d8 2018-03-11 stsp return err;
2579 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2580 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2581 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2582 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2583 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2584 f2ea84fa 2019-07-27 stsp goto done;
2587 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2588 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2589 f2ea84fa 2019-07-27 stsp if (err) {
2590 f2ea84fa 2019-07-27 stsp free(tpd);
2591 6ced4176 2019-07-12 stsp goto done;
2594 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2595 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2596 f2ea84fa 2019-07-27 stsp if (err) {
2597 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2598 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2599 f2ea84fa 2019-07-27 stsp free(tpd);
2600 f2ea84fa 2019-07-27 stsp goto done;
2603 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2605 f2ea84fa 2019-07-27 stsp SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2609 51514078 2018-12-25 stsp * Read the file index.
2610 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2611 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2613 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2615 c4cdcb68 2019-04-03 stsp goto done;
2617 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2618 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2619 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2621 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2622 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2623 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2627 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2628 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2629 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2630 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2631 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2632 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2633 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2634 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2638 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_NEXT(tpd, entry);
2640 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2641 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2642 af12c6b9 2019-06-04 stsp err = sync_err;
2644 9c6338c4 2019-06-02 stsp free(fileindex_path);
2646 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2647 9d31a1d8 2018-03-11 stsp if (commit)
2648 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2649 5ade8233 2019-07-12 stsp if (fileindex)
2650 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2651 f2ea84fa 2019-07-27 stsp while (!SIMPLEQ_EMPTY(&tree_paths)) {
2652 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2653 f2ea84fa 2019-07-27 stsp SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2654 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2655 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2656 f2ea84fa 2019-07-27 stsp free(tpd);
2658 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2659 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2660 9d31a1d8 2018-03-11 stsp err = unlockerr;
2661 f8d1f275 2019-02-04 stsp return err;
2664 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2665 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2666 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2667 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2668 234035bc 2019-06-01 stsp void *progress_arg;
2669 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2670 234035bc 2019-06-01 stsp void *cancel_arg;
2671 f69721c3 2019-10-21 stsp const char *label_orig;
2672 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2675 234035bc 2019-06-01 stsp static const struct got_error *
2676 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2677 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
2678 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
2679 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2681 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2682 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2683 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2684 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2685 234035bc 2019-06-01 stsp struct stat sb;
2686 234035bc 2019-06-01 stsp unsigned char status;
2687 234035bc 2019-06-01 stsp int local_changes_subsumed;
2689 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2690 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2691 d6c87207 2019-08-02 stsp strlen(path2));
2692 1ee397ad 2019-07-12 stsp if (ie == NULL)
2693 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2694 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2696 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2697 234035bc 2019-06-01 stsp path2) == -1)
2698 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2700 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2703 234035bc 2019-06-01 stsp goto done;
2705 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2706 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2707 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2708 234035bc 2019-06-01 stsp goto done;
2710 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2711 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2712 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2713 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2714 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2715 234035bc 2019-06-01 stsp goto done;
2718 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2719 af57b12a 2020-07-23 stsp err = merge_symlink(a->worktree, blob1,
2720 af57b12a 2020-07-23 stsp ondisk_path, path2, sb.st_mode, a->label_orig,
2721 af57b12a 2020-07-23 stsp blob2, a->commit_id2, repo, a->progress_cb,
2722 af57b12a 2020-07-23 stsp a->progress_arg);
2724 af57b12a 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
2725 af57b12a 2020-07-23 stsp blob1, ondisk_path, path2, sb.st_mode,
2726 af57b12a 2020-07-23 stsp a->label_orig, blob2, a->commit_id2, repo,
2727 af57b12a 2020-07-23 stsp a->progress_cb, a->progress_arg);
2729 234035bc 2019-06-01 stsp } else if (blob1) {
2730 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2731 d6c87207 2019-08-02 stsp strlen(path1));
2732 1ee397ad 2019-07-12 stsp if (ie == NULL)
2733 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2734 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2736 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2737 2b92fad7 2019-06-02 stsp path1) == -1)
2738 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2740 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2743 2b92fad7 2019-06-02 stsp goto done;
2745 2b92fad7 2019-06-02 stsp switch (status) {
2746 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2747 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2748 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2750 1ee397ad 2019-07-12 stsp goto done;
2751 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2753 2b92fad7 2019-06-02 stsp goto done;
2755 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2757 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2758 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2759 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2760 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2762 1ee397ad 2019-07-12 stsp goto done;
2764 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2766 2b92fad7 2019-06-02 stsp case GOT_STATUS_ADD:
2767 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2768 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2769 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2770 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2772 1ee397ad 2019-07-12 stsp goto done;
2774 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2775 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2777 1ee397ad 2019-07-12 stsp goto done;
2782 234035bc 2019-06-01 stsp } else if (blob2) {
2783 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2784 234035bc 2019-06-01 stsp path2) == -1)
2785 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2786 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2787 d6c87207 2019-08-02 stsp strlen(path2));
2789 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2790 7f91a133 2019-12-13 stsp -1, NULL, repo);
2792 234035bc 2019-06-01 stsp goto done;
2793 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2794 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2795 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2796 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2797 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2798 1ee397ad 2019-07-12 stsp status, path2);
2799 234035bc 2019-06-01 stsp goto done;
2801 526a746f 2020-07-23 stsp if (S_ISLNK(mode2)) {
2802 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
2803 526a746f 2020-07-23 stsp ondisk_path, path2, sb.st_mode,
2804 526a746f 2020-07-23 stsp a->label_orig, blob2, a->commit_id2,
2805 526a746f 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2807 526a746f 2020-07-23 stsp goto done;
2809 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
2810 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
2811 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
2812 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
2813 526a746f 2020-07-23 stsp a->progress_arg);
2815 526a746f 2020-07-23 stsp goto done;
2817 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2818 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
2819 054041d0 2020-06-24 stsp ondisk_path, blob2->id.sha1,
2820 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
2822 234035bc 2019-06-01 stsp goto done;
2825 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2826 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2827 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
2828 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
2829 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
2830 c90c8ce3 2020-07-23 stsp 0, 1, repo, a->progress_cb, a->progress_arg);
2832 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
2833 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2834 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
2837 234035bc 2019-06-01 stsp goto done;
2838 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
2840 234035bc 2019-06-01 stsp goto done;
2841 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ondisk_path,
2842 3969253a 2020-03-07 stsp NULL, NULL, 1);
2843 3969253a 2020-03-07 stsp if (err) {
2844 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
2845 3969253a 2020-03-07 stsp goto done;
2847 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
2848 2b92fad7 2019-06-02 stsp if (err) {
2849 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
2850 2b92fad7 2019-06-02 stsp goto done;
2852 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2853 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2854 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2859 234035bc 2019-06-01 stsp free(ondisk_path);
2860 234035bc 2019-06-01 stsp return err;
2863 234035bc 2019-06-01 stsp struct check_merge_ok_arg {
2864 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2865 234035bc 2019-06-01 stsp struct got_repository *repo;
2868 234035bc 2019-06-01 stsp static const struct got_error *
2869 234035bc 2019-06-01 stsp check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2871 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
2872 234035bc 2019-06-01 stsp struct check_merge_ok_arg *a = arg;
2873 234035bc 2019-06-01 stsp unsigned char status;
2874 234035bc 2019-06-01 stsp struct stat sb;
2875 234035bc 2019-06-01 stsp char *ondisk_path;
2877 234035bc 2019-06-01 stsp /* Reject merges into a work tree with mixed base commits. */
2878 234035bc 2019-06-01 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2879 234035bc 2019-06-01 stsp SHA1_DIGEST_LENGTH))
2880 234035bc 2019-06-01 stsp return got_error(GOT_ERR_MIXED_COMMITS);
2882 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2884 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2886 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
2887 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2889 234035bc 2019-06-01 stsp return err;
2890 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
2891 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
2893 234035bc 2019-06-01 stsp return NULL;
2896 818c7501 2019-07-11 stsp static const struct got_error *
2897 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2898 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
2899 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
2900 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2901 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2903 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
2904 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2905 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2906 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
2907 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2909 03415a1a 2019-06-02 stsp if (commit_id1) {
2910 f69721c3 2019-10-21 stsp char *id_str;
2912 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2913 03415a1a 2019-06-02 stsp worktree->path_prefix);
2915 03415a1a 2019-06-02 stsp goto done;
2917 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2919 03415a1a 2019-06-02 stsp goto done;
2921 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
2923 f69721c3 2019-10-21 stsp goto done;
2925 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2926 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2927 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2928 f69721c3 2019-10-21 stsp free(id_str);
2929 f69721c3 2019-10-21 stsp goto done;
2931 f69721c3 2019-10-21 stsp free(id_str);
2934 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2935 234035bc 2019-06-01 stsp worktree->path_prefix);
2937 234035bc 2019-06-01 stsp goto done;
2939 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2941 234035bc 2019-06-01 stsp goto done;
2943 234035bc 2019-06-01 stsp arg.worktree = worktree;
2944 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
2945 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
2946 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
2947 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
2948 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
2949 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
2950 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
2951 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2952 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2953 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2954 af12c6b9 2019-06-04 stsp err = sync_err;
2956 234035bc 2019-06-01 stsp if (tree1)
2957 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
2958 234035bc 2019-06-01 stsp if (tree2)
2959 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
2960 f69721c3 2019-10-21 stsp free(label_orig);
2961 818c7501 2019-07-11 stsp return err;
2964 818c7501 2019-07-11 stsp const struct got_error *
2965 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
2966 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2967 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2968 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2970 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
2971 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
2972 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
2973 818c7501 2019-07-11 stsp struct check_merge_ok_arg mok_arg;
2975 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
2977 818c7501 2019-07-11 stsp return err;
2979 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2981 818c7501 2019-07-11 stsp goto done;
2983 818c7501 2019-07-11 stsp mok_arg.worktree = worktree;
2984 818c7501 2019-07-11 stsp mok_arg.repo = repo;
2985 818c7501 2019-07-11 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2986 818c7501 2019-07-11 stsp &mok_arg);
2988 818c7501 2019-07-11 stsp goto done;
2990 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2991 818c7501 2019-07-11 stsp commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2993 818c7501 2019-07-11 stsp if (fileindex)
2994 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
2995 818c7501 2019-07-11 stsp free(fileindex_path);
2996 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2997 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
2998 234035bc 2019-06-01 stsp err = unlockerr;
2999 234035bc 2019-06-01 stsp return err;
3002 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3003 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3004 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3005 927df6b7 2019-02-10 stsp const char *status_path;
3006 927df6b7 2019-02-10 stsp size_t status_path_len;
3007 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3008 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3009 f8d1f275 2019-02-04 stsp void *status_arg;
3010 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3011 f8d1f275 2019-02-04 stsp void *cancel_arg;
3012 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3013 6841da00 2019-08-08 stsp struct got_pathlist_head ignores;
3014 f2a9dc41 2019-12-13 tracey int report_unchanged;
3015 3143d852 2020-06-25 stsp int no_ignores;
3018 f8d1f275 2019-02-04 stsp static const struct got_error *
3019 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3020 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3021 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3022 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3024 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3025 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3026 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3027 927df6b7 2019-02-10 stsp struct stat sb;
3028 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3029 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3030 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3032 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3034 98eaaa12 2019-08-03 stsp return err;
3036 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3037 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3038 98eaaa12 2019-08-03 stsp return NULL;
3040 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3041 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3042 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3044 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3045 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3046 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3048 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3049 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3050 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3051 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3052 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3055 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3056 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3059 927df6b7 2019-02-10 stsp static const struct got_error *
3060 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3061 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3063 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3064 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3065 f8d1f275 2019-02-04 stsp char *abspath;
3067 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3068 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3070 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3071 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3072 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3073 927df6b7 2019-02-10 stsp return NULL;
3075 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3076 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3077 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3078 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3080 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3081 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3082 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3085 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3086 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3087 f8d1f275 2019-02-04 stsp free(abspath);
3088 9d31a1d8 2018-03-11 stsp return err;
3091 f8d1f275 2019-02-04 stsp static const struct got_error *
3092 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3094 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3095 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3096 2ec1f75b 2019-03-26 stsp unsigned char status;
3098 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3099 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3101 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3102 927df6b7 2019-02-10 stsp return NULL;
3104 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3105 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3106 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3107 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3109 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3110 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3111 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3115 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3117 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3119 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3120 6841da00 2019-08-08 stsp free((char *)pe->path);
3121 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3125 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3127 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3129 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3130 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3131 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3132 6841da00 2019-08-08 stsp free((char *)pe->path);
3134 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3137 6841da00 2019-08-08 stsp static const struct got_error *
3138 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3140 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3141 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3142 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3143 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3144 6841da00 2019-08-08 stsp size_t linesize = 0;
3145 6841da00 2019-08-08 stsp ssize_t linelen;
3147 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3148 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3149 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3150 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3152 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3153 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3154 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3156 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3157 bd8de430 2019-10-04 stsp if (line[0] == '#')
3160 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3161 bd8de430 2019-10-04 stsp if (line[0] == '!')
3164 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3165 6841da00 2019-08-08 stsp line) == -1) {
3166 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3167 6841da00 2019-08-08 stsp goto done;
3169 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3171 6841da00 2019-08-08 stsp goto done;
3173 6841da00 2019-08-08 stsp if (ferror(f)) {
3174 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3175 6841da00 2019-08-08 stsp goto done;
3178 6841da00 2019-08-08 stsp dirpath = strdup(path);
3179 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3180 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3181 6841da00 2019-08-08 stsp goto done;
3183 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3185 6841da00 2019-08-08 stsp free(line);
3186 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3187 6841da00 2019-08-08 stsp free(dirpath);
3188 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3190 6841da00 2019-08-08 stsp return err;
3194 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3196 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3198 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3199 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3200 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3201 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3203 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3204 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3206 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3208 bd8de430 2019-10-04 stsp pattern += 3;
3210 bd8de430 2019-10-04 stsp while (*p) {
3211 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3212 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3213 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3214 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3216 bd8de430 2019-10-04 stsp while (*p == '/')
3226 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3227 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3228 6841da00 2019-08-08 stsp * ignores backwards.
3230 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3231 6841da00 2019-08-08 stsp while (pe) {
3232 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3233 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3234 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3235 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3236 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3237 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3238 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3239 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3240 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3245 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3251 6841da00 2019-08-08 stsp static const struct got_error *
3252 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3253 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3255 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3256 6841da00 2019-08-08 stsp char *ignorespath;
3257 886cec17 2019-12-15 stsp int fd = -1;
3258 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3260 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3261 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3262 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3264 886cec17 2019-12-15 stsp if (dirfd != -1) {
3265 886cec17 2019-12-15 stsp fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3266 886cec17 2019-12-15 stsp if (fd == -1) {
3267 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3268 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3269 886cec17 2019-12-15 stsp ignorespath);
3271 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3272 886cec17 2019-12-15 stsp if (ignoresfile == NULL)
3273 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3274 886cec17 2019-12-15 stsp ignorespath);
3277 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3281 886cec17 2019-12-15 stsp ignoresfile = fopen(ignorespath, "r");
3282 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3283 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3284 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3285 886cec17 2019-12-15 stsp ignorespath);
3287 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3290 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3291 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3292 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3293 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3294 6841da00 2019-08-08 stsp free(ignorespath);
3295 6841da00 2019-08-08 stsp return err;
3298 f8d1f275 2019-02-04 stsp static const struct got_error *
3299 7f91a133 2019-12-13 stsp status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3301 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3302 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3303 f8d1f275 2019-02-04 stsp char *path = NULL;
3305 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3306 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3308 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3309 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3310 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3312 f8d1f275 2019-02-04 stsp path = de->d_name;
3315 3143d852 2020-06-25 stsp if (de->d_type != DT_DIR &&
3316 3143d852 2020-06-25 stsp got_path_is_child(path, a->status_path, a->status_path_len)
3317 6841da00 2019-08-08 stsp && !match_ignores(&a->ignores, path))
3318 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3319 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3320 f8d1f275 2019-02-04 stsp if (parent_path[0])
3321 f8d1f275 2019-02-04 stsp free(path);
3322 b72f483a 2019-02-05 stsp return err;
3325 347d1d3e 2019-07-12 stsp static const struct got_error *
3326 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3328 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3329 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3331 3143d852 2020-06-25 stsp if (a->no_ignores)
3332 3143d852 2020-06-25 stsp return NULL;
3334 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path,
3335 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3337 3143d852 2020-06-25 stsp return err;
3339 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path, path,
3340 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3342 3143d852 2020-06-25 stsp return err;
3345 3143d852 2020-06-25 stsp static const struct got_error *
3346 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3347 abb4604f 2019-07-27 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3348 f2a9dc41 2019-12-13 tracey void *status_arg, struct got_repository *repo, int report_unchanged)
3350 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3351 abb4604f 2019-07-27 stsp struct stat sb;
3353 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3355 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3356 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3358 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3359 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3360 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3361 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3362 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3363 abb4604f 2019-07-27 stsp return NULL;
3366 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3367 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3368 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3370 abb4604f 2019-07-27 stsp return NULL;
3373 3143d852 2020-06-25 stsp static const struct got_error *
3374 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3375 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3377 3143d852 2020-06-25 stsp const struct got_error *err;
3378 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3380 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3381 3143d852 2020-06-25 stsp ".cvsignore");
3383 3143d852 2020-06-25 stsp return err;
3385 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3386 3143d852 2020-06-25 stsp ".gitignore");
3388 3143d852 2020-06-25 stsp return err;
3390 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3391 3143d852 2020-06-25 stsp if (err) {
3392 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3393 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3394 3143d852 2020-06-25 stsp return err;
3396 3143d852 2020-06-25 stsp for (;;) {
3397 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3398 3143d852 2020-06-25 stsp ".cvsignore");
3401 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3402 3143d852 2020-06-25 stsp ".gitignore");
3405 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3406 3143d852 2020-06-25 stsp if (err) {
3407 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3408 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3411 b737c85a 2020-06-26 stsp free(parent_path);
3412 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3413 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3416 b737c85a 2020-06-26 stsp free(parent_path);
3417 b737c85a 2020-06-26 stsp free(next_parent_path);
3418 3143d852 2020-06-25 stsp return err;
3421 abb4604f 2019-07-27 stsp static const struct got_error *
3422 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3423 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3424 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3425 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3426 f2a9dc41 2019-12-13 tracey int report_unchanged)
3428 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3429 6fc93f37 2019-12-13 stsp int fd = -1;
3430 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3431 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3432 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3434 3143d852 2020-06-25 stsp TAILQ_INIT(&arg.ignores);
3436 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3437 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3438 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3440 6fc93f37 2019-12-13 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3441 6fc93f37 2019-12-13 stsp if (fd == -1) {
3442 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3443 00bb5ea0 2020-07-23 stsp errno != ELOOP)
3444 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3446 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3447 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3448 f2a9dc41 2019-12-13 tracey report_unchanged);
3450 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3451 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3452 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3453 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3454 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3455 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3456 abb4604f 2019-07-27 stsp arg.status_path = path;
3457 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3458 abb4604f 2019-07-27 stsp arg.repo = repo;
3459 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3460 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3461 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3462 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3463 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3464 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3465 022fae89 2019-12-06 tracey if (!no_ignores) {
3466 3143d852 2020-06-25 stsp err = add_ignores_from_parent_paths(&arg.ignores,
3467 3143d852 2020-06-25 stsp worktree->root_path, path);
3469 3143d852 2020-06-25 stsp goto done;
3471 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3472 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3475 3143d852 2020-06-25 stsp free_ignores(&arg.ignores);
3476 6fc93f37 2019-12-13 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
3477 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3478 927df6b7 2019-02-10 stsp free(ondisk_path);
3479 347d1d3e 2019-07-12 stsp return err;
3482 347d1d3e 2019-07-12 stsp const struct got_error *
3483 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3484 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3485 72ea6654 2019-07-27 stsp got_worktree_status_cb status_cb, void *status_arg,
3486 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3488 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3489 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3490 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3491 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3493 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3495 347d1d3e 2019-07-12 stsp return err;
3497 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3498 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3499 f2a9dc41 2019-12-13 tracey status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3503 f8d1f275 2019-02-04 stsp free(fileindex_path);
3504 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3505 f8d1f275 2019-02-04 stsp return err;
3508 6c7ab921 2019-03-18 stsp const struct got_error *
3509 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3510 6c7ab921 2019-03-18 stsp const char *arg)
3512 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3513 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3514 6c7ab921 2019-03-18 stsp size_t len;
3515 00bb5ea0 2020-07-23 stsp struct stat sb;
3517 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3519 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3520 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3521 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3523 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3524 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3525 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3526 00bb5ea0 2020-07-23 stsp goto done;
3529 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3531 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3532 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3533 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3534 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3536 00bb5ea0 2020-07-23 stsp char *abspath = NULL;
3537 00bb5ea0 2020-07-23 stsp char canonpath[PATH_MAX];
3538 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3539 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3540 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3541 00bb5ea0 2020-07-23 stsp goto done;
3545 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3546 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3548 d0710d08 2019-07-22 stsp goto done;
3549 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3550 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3551 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3552 00bb5ea0 2020-07-23 stsp goto done;
3555 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3556 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3557 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3558 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3559 00bb5ea0 2020-07-23 stsp goto done;
3561 00bb5ea0 2020-07-23 stsp if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3562 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3563 00bb5ea0 2020-07-23 stsp goto done;
3568 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3569 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3570 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3571 6c7ab921 2019-03-18 stsp goto done;
3574 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3575 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3576 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3578 f6d88e1a 2019-05-29 stsp goto done;
3580 f6d88e1a 2019-05-29 stsp path = strdup("");
3581 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3582 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3583 f6d88e1a 2019-05-29 stsp goto done;
3587 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3588 6c7ab921 2019-03-18 stsp len = strlen(path);
3589 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3590 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3594 6c7ab921 2019-03-18 stsp free(resolved);
3595 d0710d08 2019-07-22 stsp free(cwd);
3596 6c7ab921 2019-03-18 stsp if (err == NULL)
3597 6c7ab921 2019-03-18 stsp *wt_path = path;
3599 6c7ab921 2019-03-18 stsp free(path);
3600 d00136be 2019-03-26 stsp return err;
3603 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3604 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3605 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3606 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3607 4e68cba3 2019-11-23 stsp void *progress_arg;
3608 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3611 4e68cba3 2019-11-23 stsp static const struct got_error *
3612 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3613 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3614 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3615 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3617 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3618 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3619 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3620 6d022e97 2019-08-04 stsp struct stat sb;
3621 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3623 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3624 dbb83fbd 2019-12-12 stsp relpath) == -1)
3625 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3627 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3629 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3630 12463d8b 2019-12-13 stsp de_name, a->repo);
3632 dbb83fbd 2019-12-12 stsp goto done;
3633 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3634 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3635 dbb83fbd 2019-12-12 stsp goto done;
3636 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3638 dbb83fbd 2019-12-12 stsp goto done;
3641 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3642 dbb83fbd 2019-12-12 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3643 dbb83fbd 2019-12-12 stsp goto done;
3646 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3648 4e68cba3 2019-11-23 stsp goto done;
3649 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3650 3969253a 2020-03-07 stsp if (err) {
3651 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3652 3969253a 2020-03-07 stsp goto done;
3654 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3655 07f5b47a 2019-06-02 stsp if (err) {
3656 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
3657 4e68cba3 2019-11-23 stsp goto done;
3660 dbb83fbd 2019-12-12 stsp free(ondisk_path);
3662 dbb83fbd 2019-12-12 stsp return err;
3663 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
3664 dbb83fbd 2019-12-12 stsp return NULL;
3665 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3668 d00136be 2019-03-26 stsp const struct got_error *
3669 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
3670 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
3671 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3672 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
3674 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3675 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
3676 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3677 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
3678 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
3680 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3682 d00136be 2019-03-26 stsp return err;
3684 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3686 d00136be 2019-03-26 stsp goto done;
3688 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
3689 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
3690 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
3691 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
3692 4e68cba3 2019-11-23 stsp saa.repo = repo;
3694 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3695 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3696 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3700 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3701 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3702 af12c6b9 2019-06-04 stsp err = sync_err;
3704 fb399478 2019-07-12 stsp free(fileindex_path);
3705 d00136be 2019-03-26 stsp if (fileindex)
3706 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
3707 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3708 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
3709 d00136be 2019-03-26 stsp err = unlockerr;
3710 6c7ab921 2019-03-18 stsp return err;
3713 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
3714 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
3715 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
3716 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
3717 f2a9dc41 2019-12-13 tracey void *progress_arg;
3718 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
3719 f2a9dc41 2019-12-13 tracey int delete_local_mods;
3720 70e3e7f5 2019-12-13 tracey int keep_on_disk;
3723 f2a9dc41 2019-12-13 tracey static const struct got_error *
3724 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
3725 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
3726 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3727 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
3729 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
3730 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
3731 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
3732 17ed4618 2019-06-02 stsp struct stat sb;
3733 15341bfd 2020-03-05 tracey char *ondisk_path, *parent = NULL;
3735 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3736 17ed4618 2019-06-02 stsp if (ie == NULL)
3737 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
3739 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
3740 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
3741 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3742 9acbc4fa 2019-08-03 stsp return NULL;
3743 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3746 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3747 f2a9dc41 2019-12-13 tracey relpath) == -1)
3748 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
3750 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3753 f2a9dc41 2019-12-13 tracey goto done;
3755 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
3756 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
3757 f2a9dc41 2019-12-13 tracey goto done;
3758 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3759 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3760 f2a9dc41 2019-12-13 tracey goto done;
3762 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
3763 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
3764 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3765 f2a9dc41 2019-12-13 tracey goto done;
3769 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3770 12463d8b 2019-12-13 stsp if (dirfd != -1) {
3771 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
3772 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
3773 12463d8b 2019-12-13 stsp ondisk_path);
3774 12463d8b 2019-12-13 stsp goto done;
3776 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
3777 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
3778 12463d8b 2019-12-13 stsp goto done;
3781 15341bfd 2020-03-05 tracey parent = dirname(ondisk_path);
3783 15341bfd 2020-03-05 tracey if (parent == NULL) {
3784 15341bfd 2020-03-05 tracey err = got_error_from_errno2("dirname", ondisk_path);
3785 15341bfd 2020-03-05 tracey goto done;
3787 15341bfd 2020-03-05 tracey while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3788 15341bfd 2020-03-05 tracey if (rmdir(parent) == -1) {
3789 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
3790 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
3791 15341bfd 2020-03-05 tracey parent);
3794 15341bfd 2020-03-05 tracey parent = dirname(parent);
3795 15341bfd 2020-03-05 tracey if (parent == NULL) {
3796 15341bfd 2020-03-05 tracey err = got_error_from_errno2("dirname", parent);
3797 15341bfd 2020-03-05 tracey goto done;
3802 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3804 f2a9dc41 2019-12-13 tracey free(ondisk_path);
3805 f2a9dc41 2019-12-13 tracey if (err)
3806 f2a9dc41 2019-12-13 tracey return err;
3807 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
3808 f2a9dc41 2019-12-13 tracey return NULL;
3809 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3810 f2a9dc41 2019-12-13 tracey staged_status, relpath);
3813 2ec1f75b 2019-03-26 stsp const struct got_error *
3814 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
3815 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
3816 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
3817 70e3e7f5 2019-12-13 tracey struct got_repository *repo, int keep_on_disk)
3819 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3820 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
3821 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3822 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
3823 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
3825 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3827 2ec1f75b 2019-03-26 stsp return err;
3829 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3831 2ec1f75b 2019-03-26 stsp goto done;
3833 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
3834 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
3835 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
3836 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
3837 f2a9dc41 2019-12-13 tracey sda.repo = repo;
3838 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
3839 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
3841 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3842 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
3843 f2a9dc41 2019-12-13 tracey schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3847 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3848 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3849 af12c6b9 2019-06-04 stsp err = sync_err;
3851 fb399478 2019-07-12 stsp free(fileindex_path);
3852 2ec1f75b 2019-03-26 stsp if (fileindex)
3853 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
3854 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3855 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
3856 2ec1f75b 2019-03-26 stsp err = unlockerr;
3857 33aa809d 2019-08-08 stsp return err;
3860 33aa809d 2019-08-08 stsp static const struct got_error *
3861 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3863 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
3864 33aa809d 2019-08-08 stsp char *line = NULL;
3865 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
3866 33aa809d 2019-08-08 stsp ssize_t linelen;
3868 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
3869 33aa809d 2019-08-08 stsp if (linelen == -1) {
3870 33aa809d 2019-08-08 stsp if (ferror(infile)) {
3871 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
3872 33aa809d 2019-08-08 stsp goto done;
3874 33aa809d 2019-08-08 stsp return NULL;
3876 33aa809d 2019-08-08 stsp if (outfile) {
3877 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
3878 33aa809d 2019-08-08 stsp if (n != linelen) {
3879 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
3880 33aa809d 2019-08-08 stsp goto done;
3883 33aa809d 2019-08-08 stsp if (rejectfile) {
3884 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
3885 33aa809d 2019-08-08 stsp if (n != linelen)
3886 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
3889 33aa809d 2019-08-08 stsp free(line);
3890 2ec1f75b 2019-03-26 stsp return err;
3893 33aa809d 2019-08-08 stsp static const struct got_error *
3894 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
3896 33aa809d 2019-08-08 stsp char *line = NULL;
3897 33aa809d 2019-08-08 stsp size_t linesize = 0;
3898 33aa809d 2019-08-08 stsp ssize_t linelen;
3900 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
3901 500467ff 2019-09-25 hiltjo if (linelen == -1) {
3902 500467ff 2019-09-25 hiltjo if (ferror(f))
3903 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
3904 500467ff 2019-09-25 hiltjo return NULL;
3906 33aa809d 2019-08-08 stsp free(line);
3907 33aa809d 2019-08-08 stsp return NULL;
3910 33aa809d 2019-08-08 stsp static const struct got_error *
3911 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3912 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
3913 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
3915 33aa809d 2019-08-08 stsp const struct got_error *err;
3917 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
3918 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
3919 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
3921 33aa809d 2019-08-08 stsp return err;
3922 33aa809d 2019-08-08 stsp (*line_cur1)++;
3924 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
3925 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
3926 33aa809d 2019-08-08 stsp if (rejectfile)
3927 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
3929 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
3931 33aa809d 2019-08-08 stsp return err;
3932 33aa809d 2019-08-08 stsp (*line_cur2)++;
3934 33aa809d 2019-08-08 stsp /* Copy patched lines. */
3935 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
3936 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
3938 33aa809d 2019-08-08 stsp return err;
3939 33aa809d 2019-08-08 stsp (*line_cur2)++;
3941 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
3942 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
3943 33aa809d 2019-08-08 stsp if (rejectfile)
3944 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
3946 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
3948 33aa809d 2019-08-08 stsp return err;
3949 33aa809d 2019-08-08 stsp (*line_cur1)++;
3952 f1e81a05 2019-08-10 stsp return NULL;
3955 f1e81a05 2019-08-10 stsp static const struct got_error *
3956 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3957 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
3959 f1e81a05 2019-08-10 stsp const struct got_error *err;
3961 f1e81a05 2019-08-10 stsp if (outfile) {
3962 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
3963 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
3964 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
3966 f1e81a05 2019-08-10 stsp return err;
3967 f1e81a05 2019-08-10 stsp (*line_cur1)++;
3970 f1e81a05 2019-08-10 stsp if (rejectfile) {
3971 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
3972 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
3973 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
3975 f1e81a05 2019-08-10 stsp return err;
3976 f1e81a05 2019-08-10 stsp (*line_cur2)++;
3980 33aa809d 2019-08-08 stsp return NULL;
3983 33aa809d 2019-08-08 stsp static const struct got_error *
3984 33aa809d 2019-08-08 stsp apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3985 33aa809d 2019-08-08 stsp int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3986 33aa809d 2019-08-08 stsp int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3987 33aa809d 2019-08-08 stsp int *line_cur2, FILE *outfile, FILE *rejectfile,
3988 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
3990 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
3991 33aa809d 2019-08-08 stsp int start_old = change->cv.a;
3992 33aa809d 2019-08-08 stsp int end_old = change->cv.b;
3993 33aa809d 2019-08-08 stsp int start_new = change->cv.c;
3994 33aa809d 2019-08-08 stsp int end_new = change->cv.d;
3995 33aa809d 2019-08-08 stsp long pos1, pos2;
3996 33aa809d 2019-08-08 stsp FILE *hunkfile;
3998 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4000 33aa809d 2019-08-08 stsp hunkfile = got_opentemp();
4001 33aa809d 2019-08-08 stsp if (hunkfile == NULL)
4002 33aa809d 2019-08-08 stsp return got_error_from_errno("got_opentemp");
4004 33aa809d 2019-08-08 stsp pos1 = ftell(f1);
4005 33aa809d 2019-08-08 stsp pos2 = ftell(f2);
4007 33aa809d 2019-08-08 stsp /* XXX TODO needs error checking */
4008 33aa809d 2019-08-08 stsp got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4010 33aa809d 2019-08-08 stsp if (fseek(f1, pos1, SEEK_SET) == -1) {
4011 33aa809d 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
4012 33aa809d 2019-08-08 stsp goto done;
4014 33aa809d 2019-08-08 stsp if (fseek(f2, pos2, SEEK_SET) == -1) {
4015 33aa809d 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
4016 33aa809d 2019-08-08 stsp goto done;
4018 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4019 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4020 33aa809d 2019-08-08 stsp goto done;
4023 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4024 33aa809d 2019-08-08 stsp hunkfile, n, nchanges);
4026 33aa809d 2019-08-08 stsp goto done;
4028 33aa809d 2019-08-08 stsp switch (*choice) {
4029 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4030 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4031 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4033 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4034 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4035 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4037 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4040 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4044 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4045 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4046 33aa809d 2019-08-08 stsp return err;
4049 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4050 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4051 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4052 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4053 1f1abb7e 2019-08-08 stsp void *progress_arg;
4054 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4055 33aa809d 2019-08-08 stsp void *patch_arg;
4056 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4059 e20a8b6f 2019-06-04 stsp static const struct got_error *
4060 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4061 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4062 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4063 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4064 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4066 33aa809d 2019-08-08 stsp const struct got_error *err;
4067 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4068 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4069 1ebedb77 2019-10-19 stsp int fd2 = -1;
4070 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4071 33aa809d 2019-08-08 stsp struct stat sb1, sb2;
4072 33aa809d 2019-08-08 stsp struct got_diff_changes *changes = NULL;
4073 33aa809d 2019-08-08 stsp struct got_diff_state *ds = NULL;
4074 33aa809d 2019-08-08 stsp struct got_diff_args *args = NULL;
4075 33aa809d 2019-08-08 stsp struct got_diff_change *change;
4076 33aa809d 2019-08-08 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4077 33aa809d 2019-08-08 stsp int n = 0;
4079 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4081 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4083 33aa809d 2019-08-08 stsp return err;
4085 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4086 12463d8b 2019-12-13 stsp fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4087 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4088 12463d8b 2019-12-13 stsp err = got_error_from_errno2("openat", path2);
4089 12463d8b 2019-12-13 stsp goto done;
4092 12463d8b 2019-12-13 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4093 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4094 12463d8b 2019-12-13 stsp err = got_error_from_errno2("open", path2);
4095 12463d8b 2019-12-13 stsp goto done;
4098 1ebedb77 2019-10-19 stsp if (fstat(fd2, &sb2) == -1) {
4099 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("fstat", path2);
4100 1ebedb77 2019-10-19 stsp goto done;
4103 1ebedb77 2019-10-19 stsp f2 = fdopen(fd2, "r");
4104 33aa809d 2019-08-08 stsp if (f2 == NULL) {
4105 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", path2);
4106 33aa809d 2019-08-08 stsp goto done;
4110 33aa809d 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4112 33aa809d 2019-08-08 stsp goto done;
4114 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4116 33aa809d 2019-08-08 stsp goto done;
4118 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4120 33aa809d 2019-08-08 stsp goto done;
4122 33aa809d 2019-08-08 stsp if (stat(path1, &sb1) == -1) {
4123 33aa809d 2019-08-08 stsp err = got_error_from_errno2("stat", path1);
4124 33aa809d 2019-08-08 stsp goto done;
4127 33aa809d 2019-08-08 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
4128 33aa809d 2019-08-08 stsp f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4130 33aa809d 2019-08-08 stsp goto done;
4132 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4134 33aa809d 2019-08-08 stsp goto done;
4136 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4137 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4138 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4139 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4140 33aa809d 2019-08-08 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4141 33aa809d 2019-08-08 stsp int choice;
4142 33aa809d 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
4143 33aa809d 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
4144 33aa809d 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
4145 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4146 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4147 e635744c 2019-08-08 stsp patch_cb, patch_arg);
4149 33aa809d 2019-08-08 stsp goto done;
4150 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4151 33aa809d 2019-08-08 stsp have_content = 1;
4152 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4155 1ebedb77 2019-10-19 stsp if (have_content) {
4156 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4157 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4158 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4160 1ebedb77 2019-10-19 stsp goto done;
4162 1ebedb77 2019-10-19 stsp if (chmod(*path_outfile, sb2.st_mode) == -1) {
4163 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("chmod", path2);
4164 1ebedb77 2019-10-19 stsp goto done;
4168 33aa809d 2019-08-08 stsp free(id_str);
4170 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4171 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4172 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4173 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4174 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4175 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4176 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4177 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4178 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4179 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4180 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4181 33aa809d 2019-08-08 stsp if (err || !have_content) {
4182 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4183 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4184 33aa809d 2019-08-08 stsp free(*path_outfile);
4185 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4187 33aa809d 2019-08-08 stsp free(args);
4189 33aa809d 2019-08-08 stsp got_diff_state_free(ds);
4192 33aa809d 2019-08-08 stsp if (changes)
4193 33aa809d 2019-08-08 stsp got_diff_free_changes(changes);
4194 33aa809d 2019-08-08 stsp free(path1);
4195 33aa809d 2019-08-08 stsp return err;
4198 33aa809d 2019-08-08 stsp static const struct got_error *
4199 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4200 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4201 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4202 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4204 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4205 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4206 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4207 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4208 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4209 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4210 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4211 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4212 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4213 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4215 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4216 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4217 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4218 d3bcc3d1 2019-08-08 stsp return NULL;
4220 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4221 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4222 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4224 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4225 65084dad 2019-08-08 stsp if (ie == NULL)
4226 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4228 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4229 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4230 a129376b 2019-03-28 stsp if (err) {
4231 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4232 a129376b 2019-03-28 stsp goto done;
4233 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4234 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4235 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4236 e20a8b6f 2019-06-04 stsp goto done;
4239 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4240 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4241 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4242 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4243 a129376b 2019-03-28 stsp goto done;
4246 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4247 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4248 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4249 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4250 a129376b 2019-03-28 stsp goto done;
4253 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4254 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4255 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4256 a129376b 2019-03-28 stsp goto done;
4261 1f1abb7e 2019-08-08 stsp err = got_object_id_by_path(&tree_id, a->repo,
4262 1f1abb7e 2019-08-08 stsp a->worktree->base_commit_id, tree_path);
4263 a9fa2909 2019-07-27 stsp if (err) {
4264 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4265 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4266 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4267 a9fa2909 2019-07-27 stsp goto done;
4269 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4271 a9fa2909 2019-07-27 stsp goto done;
4273 a9fa2909 2019-07-27 stsp te_name = basename(ie->path);
4274 a9fa2909 2019-07-27 stsp if (te_name == NULL) {
4275 a9fa2909 2019-07-27 stsp err = got_error_from_errno2("basename", ie->path);
4276 a9fa2909 2019-07-27 stsp goto done;
4279 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4280 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4281 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4282 a9fa2909 2019-07-27 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
4283 a9fa2909 2019-07-27 stsp goto done;
4287 a129376b 2019-03-28 stsp switch (status) {
4288 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4289 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4290 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4291 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4292 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4294 33aa809d 2019-08-08 stsp goto done;
4295 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4298 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4299 1f1abb7e 2019-08-08 stsp ie->path);
4301 1ee397ad 2019-07-12 stsp goto done;
4302 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4304 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4305 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4306 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4307 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4308 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4310 33aa809d 2019-08-08 stsp goto done;
4311 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4314 33aa809d 2019-08-08 stsp /* fall through */
4315 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4316 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4317 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4318 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4319 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4320 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4321 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4322 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4323 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4325 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4326 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4327 1f1abb7e 2019-08-08 stsp err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4329 65084dad 2019-08-08 stsp goto done;
4331 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4332 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4333 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4334 a129376b 2019-03-28 stsp goto done;
4337 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4338 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4339 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4340 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4341 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4342 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4344 33aa809d 2019-08-08 stsp if (rename(path_content, ondisk_path) == -1) {
4345 33aa809d 2019-08-08 stsp err = got_error_from_errno3("rename",
4346 33aa809d 2019-08-08 stsp path_content, ondisk_path);
4347 33aa809d 2019-08-08 stsp goto done;
4350 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4351 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4352 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4353 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4354 c90c8ce3 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4355 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4357 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4359 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4360 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4361 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4362 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4365 a129376b 2019-03-28 stsp goto done;
4366 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4367 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4368 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4369 054041d0 2020-06-24 stsp ondisk_path, blob->id.sha1,
4370 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4372 2e1fa222 2020-07-23 stsp goto done;
4374 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4375 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4376 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4385 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4386 33aa809d 2019-08-08 stsp free(path_content);
4387 e20a8b6f 2019-06-04 stsp free(parent_path);
4388 a129376b 2019-03-28 stsp free(tree_path);
4390 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4392 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4393 a129376b 2019-03-28 stsp free(tree_id);
4394 e20a8b6f 2019-06-04 stsp return err;
4397 e20a8b6f 2019-06-04 stsp const struct got_error *
4398 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4399 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4400 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4401 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4402 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4404 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4405 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4406 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4407 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4408 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4409 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4411 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4413 e20a8b6f 2019-06-04 stsp return err;
4415 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4417 e20a8b6f 2019-06-04 stsp goto done;
4419 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4420 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4421 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4422 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4423 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4424 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4425 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4426 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4427 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4428 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
4432 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4433 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4434 e20a8b6f 2019-06-04 stsp err = sync_err;
4436 fb399478 2019-07-12 stsp free(fileindex_path);
4437 a129376b 2019-03-28 stsp if (fileindex)
4438 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4439 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4440 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4441 a129376b 2019-03-28 stsp err = unlockerr;
4442 c4296144 2019-05-09 stsp return err;
4445 cf066bf8 2019-05-09 stsp static void
4446 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4448 24519714 2019-05-09 stsp free(ct->path);
4449 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4450 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4451 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4452 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4453 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4454 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4458 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4459 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4460 24519714 2019-05-09 stsp struct got_repository *repo;
4461 24519714 2019-05-09 stsp struct got_worktree *worktree;
4462 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4463 5f8a88c6 2019-08-03 stsp int have_staged_files;
4466 c4296144 2019-05-09 stsp static const struct got_error *
4467 88d0e355 2019-08-03 stsp collect_commitables(void *arg, unsigned char status,
4468 88d0e355 2019-08-03 stsp unsigned char staged_status, const char *relpath,
4469 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4470 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4472 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
4473 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4474 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4475 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4476 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4477 768aea60 2019-05-09 stsp struct stat sb;
4479 5f8a88c6 2019-08-03 stsp if (a->have_staged_files) {
4480 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4481 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4482 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4483 5f8a88c6 2019-08-03 stsp return NULL;
4485 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4486 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4488 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4489 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4490 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4491 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4492 5f8a88c6 2019-08-03 stsp return NULL;
4495 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4496 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4497 036813ee 2019-05-09 stsp goto done;
4499 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4500 036813ee 2019-05-09 stsp parent_path = strdup("");
4501 69960a46 2019-05-09 stsp if (parent_path == NULL)
4502 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4504 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4506 69960a46 2019-05-09 stsp return err;
4509 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4510 cf066bf8 2019-05-09 stsp if (ct == NULL) {
4511 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4512 c4296144 2019-05-09 stsp goto done;
4515 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4516 768aea60 2019-05-09 stsp relpath) == -1) {
4517 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4518 768aea60 2019-05-09 stsp goto done;
4521 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
4522 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
4523 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
4524 0aeb8099 2020-07-23 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4525 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
4526 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
4527 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
4528 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
4530 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
4531 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
4534 0aeb8099 2020-07-23 stsp fprintf(stderr, "got: ie mode is 0x%x\n", ie->mode);
4535 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4536 0aeb8099 2020-07-23 stsp goto done;
4538 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
4539 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
4540 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
4541 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4542 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
4543 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
4544 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
4545 12463d8b 2019-12-13 stsp ct->ondisk_path);
4546 12463d8b 2019-12-13 stsp goto done;
4548 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
4549 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
4550 768aea60 2019-05-09 stsp goto done;
4552 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
4555 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4556 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4557 44d03001 2019-05-09 stsp relpath) == -1) {
4558 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4559 44d03001 2019-05-09 stsp goto done;
4562 cf066bf8 2019-05-09 stsp ct->status = status;
4563 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
4564 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
4565 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
4566 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
4567 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
4568 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
4569 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4570 b416585c 2019-05-13 stsp goto done;
4572 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
4573 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
4574 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
4575 f0b75401 2019-08-03 stsp goto done;
4578 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
4579 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
4580 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4581 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
4582 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4583 036813ee 2019-05-09 stsp goto done;
4586 24519714 2019-05-09 stsp ct->path = strdup(path);
4587 24519714 2019-05-09 stsp if (ct->path == NULL) {
4588 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4589 24519714 2019-05-09 stsp goto done;
4591 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4593 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
4594 ed175427 2019-05-09 stsp free_commitable(ct);
4595 24519714 2019-05-09 stsp free(parent_path);
4596 036813ee 2019-05-09 stsp free(path);
4597 a129376b 2019-03-28 stsp return err;
4600 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
4601 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
4602 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4603 036813ee 2019-05-09 stsp struct got_repository *);
4605 ed175427 2019-05-09 stsp static const struct got_error *
4606 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4607 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
4608 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
4609 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4610 afa376bf 2019-05-09 stsp struct got_repository *repo)
4612 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
4613 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
4614 036813ee 2019-05-09 stsp char *subpath;
4616 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
4617 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4618 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4620 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
4622 ed175427 2019-05-09 stsp return err;
4624 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
4625 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
4626 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
4627 036813ee 2019-05-09 stsp free(subpath);
4628 036813ee 2019-05-09 stsp return err;
4631 036813ee 2019-05-09 stsp static const struct got_error *
4632 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4634 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4635 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
4637 036813ee 2019-05-09 stsp *match = 0;
4639 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
4640 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
4641 0f63689d 2019-05-10 stsp return NULL;
4644 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4646 0f63689d 2019-05-10 stsp return err;
4647 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
4648 036813ee 2019-05-09 stsp free(ct_parent_path);
4649 036813ee 2019-05-09 stsp return err;
4652 768aea60 2019-05-09 stsp static mode_t
4653 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
4655 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
4656 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
4658 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4661 036813ee 2019-05-09 stsp static const struct got_error *
4662 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4663 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
4665 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4667 036813ee 2019-05-09 stsp *new_te = NULL;
4669 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
4671 036813ee 2019-05-09 stsp goto done;
4673 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4675 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
4676 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4677 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4679 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4681 036813ee 2019-05-09 stsp if (err && *new_te) {
4682 56e0773d 2019-11-28 stsp free(*new_te);
4683 036813ee 2019-05-09 stsp *new_te = NULL;
4685 036813ee 2019-05-09 stsp return err;
4688 036813ee 2019-05-09 stsp static const struct got_error *
4689 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4690 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
4692 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4693 036813ee 2019-05-09 stsp char *ct_name;
4695 036813ee 2019-05-09 stsp *new_te = NULL;
4697 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
4698 036813ee 2019-05-09 stsp if (*new_te == NULL)
4699 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
4701 036813ee 2019-05-09 stsp ct_name = basename(ct->path);
4702 036813ee 2019-05-09 stsp if (ct_name == NULL) {
4703 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", ct->path);
4704 036813ee 2019-05-09 stsp goto done;
4706 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4707 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
4708 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
4709 036813ee 2019-05-09 stsp goto done;
4712 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4714 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
4715 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4716 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4718 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4720 036813ee 2019-05-09 stsp if (err && *new_te) {
4721 56e0773d 2019-11-28 stsp free(*new_te);
4722 036813ee 2019-05-09 stsp *new_te = NULL;
4724 036813ee 2019-05-09 stsp return err;
4727 036813ee 2019-05-09 stsp static const struct got_error *
4728 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
4729 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
4731 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4732 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
4734 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4736 036813ee 2019-05-09 stsp return err;
4737 036813ee 2019-05-09 stsp if (new_pe == NULL)
4738 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
4739 036813ee 2019-05-09 stsp return NULL;
4742 afa376bf 2019-05-09 stsp static const struct got_error *
4743 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
4744 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
4746 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
4747 5f8a88c6 2019-08-03 stsp unsigned char status;
4749 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
4750 afa376bf 2019-05-09 stsp ct_path++;
4752 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4753 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
4755 5f8a88c6 2019-08-03 stsp status = ct->status;
4757 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4758 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4761 036813ee 2019-05-09 stsp static const struct got_error *
4762 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
4763 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4765 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
4766 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
4767 44d03001 2019-05-09 stsp char *te_path;
4769 44d03001 2019-05-09 stsp *modified = 0;
4771 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
4772 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
4773 44d03001 2019-05-09 stsp te->name) == -1)
4774 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4776 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4777 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4778 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
4779 44d03001 2019-05-09 stsp strlen(te_path));
4780 44d03001 2019-05-09 stsp if (*modified)
4784 44d03001 2019-05-09 stsp free(te_path);
4785 44d03001 2019-05-09 stsp return err;
4788 44d03001 2019-05-09 stsp static const struct got_error *
4789 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
4790 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
4791 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
4793 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4794 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
4796 036813ee 2019-05-09 stsp *ctp = NULL;
4798 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4799 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4800 036813ee 2019-05-09 stsp char *ct_name = NULL;
4801 036813ee 2019-05-09 stsp int path_matches;
4803 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4804 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
4805 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
4806 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
4809 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
4810 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
4814 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4817 036813ee 2019-05-09 stsp err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4819 036813ee 2019-05-09 stsp return err;
4820 036813ee 2019-05-09 stsp if (!path_matches)
4823 036813ee 2019-05-09 stsp ct_name = basename(pe->path);
4824 036813ee 2019-05-09 stsp if (ct_name == NULL)
4825 638f9024 2019-05-13 stsp return got_error_from_errno2("basename", pe->path);
4827 036813ee 2019-05-09 stsp if (strcmp(te->name, ct_name) != 0)
4830 036813ee 2019-05-09 stsp *ctp = ct;
4834 2b496619 2019-07-10 stsp return err;
4837 2b496619 2019-07-10 stsp static const struct got_error *
4838 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4839 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
4840 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
4841 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
4842 2b496619 2019-07-10 stsp struct got_repository *repo)
4844 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
4845 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
4846 2b496619 2019-07-10 stsp char *subtree_path;
4847 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
4848 ba580f68 2020-03-22 stsp int nentries;
4850 2b496619 2019-07-10 stsp *new_tep = NULL;
4852 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4853 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
4854 2b496619 2019-07-10 stsp child_path) == -1)
4855 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
4857 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
4858 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
4859 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
4860 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
4862 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4863 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
4864 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
4865 2b496619 2019-07-10 stsp goto done;
4867 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
4868 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
4869 2b496619 2019-07-10 stsp if (err) {
4870 56e0773d 2019-11-28 stsp free(new_te);
4871 2b496619 2019-07-10 stsp goto done;
4873 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
4876 2b496619 2019-07-10 stsp free(subtree_path);
4877 2b496619 2019-07-10 stsp if (err == NULL)
4878 2b496619 2019-07-10 stsp *new_tep = new_te;
4879 036813ee 2019-05-09 stsp return err;
4882 036813ee 2019-05-09 stsp static const struct got_error *
4883 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
4884 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
4885 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
4886 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4887 036813ee 2019-05-09 stsp struct got_repository *repo)
4889 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4890 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
4891 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
4892 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
4894 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
4895 ba580f68 2020-03-22 stsp *nentries = 0;
4897 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
4898 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4899 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4900 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
4902 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
4903 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
4904 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
4907 036813ee 2019-05-09 stsp if (!got_path_is_child(pe->path, path_base_tree,
4908 2f51b5b3 2019-05-09 stsp strlen(path_base_tree)))
4911 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4912 036813ee 2019-05-09 stsp pe->path);
4914 036813ee 2019-05-09 stsp goto done;
4916 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
4917 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
4918 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
4920 036813ee 2019-05-09 stsp goto done;
4921 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
4923 afa376bf 2019-05-09 stsp goto done;
4924 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
4925 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
4927 2f51b5b3 2019-05-09 stsp goto done;
4928 ba580f68 2020-03-22 stsp (*nentries)++;
4930 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
4931 2b496619 2019-07-10 stsp if (base_tree == NULL ||
4932 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
4933 2b496619 2019-07-10 stsp == NULL) {
4934 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
4935 2b496619 2019-07-10 stsp child_path, path_base_tree,
4936 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
4939 2b496619 2019-07-10 stsp goto done;
4940 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
4942 2b496619 2019-07-10 stsp goto done;
4943 ba580f68 2020-03-22 stsp (*nentries)++;
4948 2f51b5b3 2019-05-09 stsp if (base_tree) {
4949 56e0773d 2019-11-28 stsp int i, nbase_entries;
4950 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
4951 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
4952 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
4953 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4955 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
4956 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
4957 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
4958 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
4960 63c5ca5d 2019-08-24 stsp goto done;
4961 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
4963 63c5ca5d 2019-08-24 stsp goto done;
4964 ba580f68 2020-03-22 stsp (*nentries)++;
4968 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
4969 44d03001 2019-05-09 stsp int modified;
4970 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
4972 036813ee 2019-05-09 stsp goto done;
4973 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
4974 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
4976 2f51b5b3 2019-05-09 stsp goto done;
4977 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
4978 44d03001 2019-05-09 stsp if (modified) {
4979 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
4980 ba580f68 2020-03-22 stsp int nsubentries;
4981 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
4982 ba580f68 2020-03-22 stsp &nsubentries, te,
4983 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
4984 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
4986 44d03001 2019-05-09 stsp goto done;
4987 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
4988 ba580f68 2020-03-22 stsp /* All entries were deleted. */
4989 ba580f68 2020-03-22 stsp free(new_id);
4992 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
4993 56e0773d 2019-11-28 stsp sizeof(new_te->id));
4994 56e0773d 2019-11-28 stsp free(new_id);
4996 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
4998 036813ee 2019-05-09 stsp goto done;
4999 ba580f68 2020-03-22 stsp (*nentries)++;
5003 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5004 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5006 f66c734c 2019-09-22 stsp goto done;
5008 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5009 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5010 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5011 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5012 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5013 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5015 2f51b5b3 2019-05-09 stsp goto done;
5016 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5018 2f51b5b3 2019-05-09 stsp goto done;
5019 ba580f68 2020-03-22 stsp (*nentries)++;
5021 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5022 b416585c 2019-05-13 stsp status_arg);
5024 afa376bf 2019-05-09 stsp goto done;
5026 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5027 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5029 2f51b5b3 2019-05-09 stsp goto done;
5030 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5032 2f51b5b3 2019-05-09 stsp goto done;
5033 ba580f68 2020-03-22 stsp (*nentries)++;
5038 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5039 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5041 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
5042 ed175427 2019-05-09 stsp return err;
5045 ebf99748 2019-05-09 stsp static const struct got_error *
5046 2e1fa222 2020-07-23 stsp reinstall_symlink_after_commit(int *is_bad_symlink, struct got_commitable *ct,
5047 2e1fa222 2020-07-23 stsp struct got_object_id *new_base_commit_id, struct got_worktree *worktree,
5048 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie, struct got_repository *repo)
5050 2e1fa222 2020-07-23 stsp const struct got_error *err = NULL;
5051 2e1fa222 2020-07-23 stsp struct got_blob_object *blob = NULL;
5052 2e1fa222 2020-07-23 stsp struct got_object_id *tree_id = NULL;
5053 2e1fa222 2020-07-23 stsp char *tree_path = NULL;
5054 2e1fa222 2020-07-23 stsp struct got_tree_object *tree = NULL;
5055 2e1fa222 2020-07-23 stsp struct got_tree_entry *te;
5056 2e1fa222 2020-07-23 stsp char *entry_name;
5057 0aeb8099 2020-07-23 stsp unsigned char status;
5058 0aeb8099 2020-07-23 stsp struct stat sb;
5060 0aeb8099 2020-07-23 stsp err = get_file_status(&status, &sb, ie, ct->ondisk_path,
5061 0aeb8099 2020-07-23 stsp -1, NULL, repo);
5063 0aeb8099 2020-07-23 stsp return err;
5064 0aeb8099 2020-07-23 stsp if (status != GOT_STATUS_NO_CHANGE)
5065 0aeb8099 2020-07-23 stsp return NULL;
5067 c631b115 2020-07-23 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5068 c631b115 2020-07-23 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5069 c631b115 2020-07-23 stsp err = got_object_open_as_blob(&blob, repo, ct->staged_blob_id,
5070 c631b115 2020-07-23 stsp PATH_MAX);
5072 c631b115 2020-07-23 stsp return err;
5074 c631b115 2020-07-23 stsp err = got_object_open_as_blob(&blob, repo, ct->blob_id,
5075 c631b115 2020-07-23 stsp PATH_MAX);
5077 c631b115 2020-07-23 stsp return err;
5080 2e1fa222 2020-07-23 stsp err = got_path_dirname(&tree_path, ct->in_repo_path);
5081 2e1fa222 2020-07-23 stsp if (err) {
5082 2e1fa222 2020-07-23 stsp if (err->code != GOT_ERR_BAD_PATH)
5083 2e1fa222 2020-07-23 stsp goto done;
5084 2e1fa222 2020-07-23 stsp err = got_object_id_by_path(&tree_id, repo,
5085 2e1fa222 2020-07-23 stsp new_base_commit_id, "");
5087 2e1fa222 2020-07-23 stsp goto done;
5089 2e1fa222 2020-07-23 stsp err = got_object_id_by_path(&tree_id, repo,
5090 2e1fa222 2020-07-23 stsp new_base_commit_id, tree_path);
5092 2e1fa222 2020-07-23 stsp goto done;
5095 2e1fa222 2020-07-23 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
5097 2e1fa222 2020-07-23 stsp goto done;
5099 2e1fa222 2020-07-23 stsp entry_name = basename(ct->path);
5100 2e1fa222 2020-07-23 stsp if (entry_name == NULL) {
5101 2e1fa222 2020-07-23 stsp err = got_error_from_errno2("basename", ct->path);
5102 2e1fa222 2020-07-23 stsp goto done;
5105 2e1fa222 2020-07-23 stsp te = got_object_tree_find_entry(tree, entry_name);
5106 2e1fa222 2020-07-23 stsp if (te == NULL) {
5107 2e1fa222 2020-07-23 stsp err = got_error_path(ct->path, GOT_ERR_NO_TREE_ENTRY);
5108 2e1fa222 2020-07-23 stsp goto done;
5111 2e1fa222 2020-07-23 stsp err = install_symlink(is_bad_symlink, worktree, ct->ondisk_path,
5112 c90c8ce3 2020-07-23 stsp ct->path, blob, 0, 0, 0, repo, NULL, NULL);
5115 2e1fa222 2020-07-23 stsp got_object_blob_close(blob);
5117 2e1fa222 2020-07-23 stsp got_object_tree_close(tree);
5118 2e1fa222 2020-07-23 stsp free(tree_id);
5119 2e1fa222 2020-07-23 stsp free(tree_path);
5120 2e1fa222 2020-07-23 stsp return err;
5124 2e1fa222 2020-07-23 stsp * After comitting a symlink we have a chance to convert "bad" symlinks
5125 2e1fa222 2020-07-23 stsp * (those which point outside the work tree or into .got) to regular files.
5126 2e1fa222 2020-07-23 stsp * This way, the post-commit work tree state matches a fresh checkout of
5127 2e1fa222 2020-07-23 stsp * the tree which was just committed. We also mark such newly committed
5128 2e1fa222 2020-07-23 stsp * symlinks as "bad" in the work tree's fileindex.
5130 2e1fa222 2020-07-23 stsp static const struct got_error *
5131 2e1fa222 2020-07-23 stsp reinstall_symlinks_after_commit(struct got_pathlist_head *commitable_paths,
5132 2e1fa222 2020-07-23 stsp struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5133 2e1fa222 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo)
5135 2e1fa222 2020-07-23 stsp const struct got_error *err = NULL;
5136 2e1fa222 2020-07-23 stsp struct got_pathlist_entry *pe;
5138 2e1fa222 2020-07-23 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5139 2e1fa222 2020-07-23 stsp struct got_commitable *ct = pe->data;
5140 2e1fa222 2020-07-23 stsp struct got_fileindex_entry *ie;
5141 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
5143 c631b115 2020-07-23 stsp if (!S_ISLNK(get_ct_file_mode(ct)) ||
5144 c631b115 2020-07-23 stsp ct->staged_status == GOT_STATUS_DELETE ||
5145 c631b115 2020-07-23 stsp ct->status == GOT_STATUS_DELETE)
5148 0aeb8099 2020-07-23 stsp ie = got_fileindex_entry_get(fileindex, ct->path,
5149 0aeb8099 2020-07-23 stsp strlen(ct->path));
5150 0aeb8099 2020-07-23 stsp if (ie == NULL) {
5151 0aeb8099 2020-07-23 stsp err = got_error_path(ct->path, GOT_ERR_BAD_PATH);
5154 2e1fa222 2020-07-23 stsp err = reinstall_symlink_after_commit(&is_bad_symlink,
5155 0aeb8099 2020-07-23 stsp ct, new_base_commit_id, worktree, ie, repo);
5158 2e1fa222 2020-07-23 stsp if (ie && is_bad_symlink) {
5159 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
5160 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
5164 2e1fa222 2020-07-23 stsp return err;
5167 2e1fa222 2020-07-23 stsp static const struct got_error *
5168 ebf99748 2019-05-09 stsp update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5169 72fd46fa 2019-09-06 stsp struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5170 72fd46fa 2019-09-06 stsp int have_staged_files)
5172 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5173 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5175 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5176 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5177 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5179 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5181 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5182 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5183 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5184 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5185 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5186 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5187 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5188 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5189 5f8a88c6 2019-08-03 stsp ct->ondisk_path, ct->staged_blob_id->sha1,
5190 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5191 72fd46fa 2019-09-06 stsp !have_staged_files);
5193 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5194 e75eb4da 2019-05-10 stsp ct->ondisk_path, ct->blob_id->sha1,
5195 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5196 72fd46fa 2019-09-06 stsp !have_staged_files);
5198 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5201 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ct->ondisk_path,
5202 3969253a 2020-03-07 stsp ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5203 3969253a 2020-03-07 stsp if (err) {
5204 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5207 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5208 3969253a 2020-03-07 stsp if (err) {
5209 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5214 d56d26ce 2019-05-10 stsp return err;
5218 d56d26ce 2019-05-10 stsp static const struct got_error *
5219 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5220 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5221 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5222 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5223 735ef5ac 2019-08-03 stsp int ood_errcode)
5225 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5226 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5228 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5229 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5230 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5231 1a36436d 2019-06-10 stsp return NULL;
5233 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5234 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5236 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5237 735ef5ac 2019-08-03 stsp in_repo_path);
5238 9bead371 2019-07-28 stsp if (err) {
5239 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5240 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5241 1a36436d 2019-06-10 stsp goto done;
5242 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5243 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5245 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5246 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5247 735ef5ac 2019-08-03 stsp in_repo_path);
5248 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5249 1a36436d 2019-06-10 stsp goto done;
5250 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5254 1a36436d 2019-06-10 stsp return err;
5257 c4296144 2019-05-09 stsp const struct got_error *
5258 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5259 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5260 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id, struct got_worktree *worktree,
5261 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
5262 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5263 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5264 afa376bf 2019-05-09 stsp struct got_repository *repo)
5266 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5267 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5268 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5269 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5270 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5271 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5272 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5273 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5274 ba580f68 2020-03-22 stsp int nentries;
5275 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5276 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5277 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5279 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5281 de18fc63 2019-05-09 stsp SIMPLEQ_INIT(&parent_ids);
5283 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5285 588edf97 2019-05-10 stsp goto done;
5287 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5289 588edf97 2019-05-10 stsp goto done;
5291 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5292 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5297 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5298 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5302 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5303 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5304 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5305 cf066bf8 2019-05-09 stsp char *ondisk_path;
5307 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5308 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5309 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5312 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5313 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5314 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5317 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5318 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5319 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5320 cf066bf8 2019-05-09 stsp goto done;
5322 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5323 2e1fa222 2020-07-23 stsp free(ondisk_path);
5325 cf066bf8 2019-05-09 stsp goto done;
5328 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5329 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5330 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5332 036813ee 2019-05-09 stsp goto done;
5334 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5336 de18fc63 2019-05-09 stsp goto done;
5337 de18fc63 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5338 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5339 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5340 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
5341 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5342 33ad4cbe 2019-05-12 jcs free(logmsg);
5344 9d40349a 2019-05-09 stsp goto done;
5346 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5347 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5348 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5349 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5350 09f5bd90 2019-05-09 stsp goto done;
5352 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
5353 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5355 09f5bd90 2019-05-09 stsp goto done;
5356 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5358 ebf99748 2019-05-09 stsp goto done;
5359 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5360 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5361 09f5bd90 2019-05-09 stsp goto done;
5363 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
5364 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
5366 f2c16586 2019-05-09 stsp goto done;
5367 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
5369 f2c16586 2019-05-09 stsp goto done;
5371 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5373 09f5bd90 2019-05-09 stsp goto done;
5375 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
5377 ebf99748 2019-05-09 stsp goto done;
5379 588edf97 2019-05-10 stsp if (head_tree)
5380 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
5381 588edf97 2019-05-10 stsp if (head_commit)
5382 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
5383 09f5bd90 2019-05-09 stsp free(head_commit_id2);
5384 2f17228e 2019-05-12 stsp if (head_ref2) {
5385 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
5386 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
5387 2f17228e 2019-05-12 stsp err = unlockerr;
5388 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
5390 39cd0ff6 2019-07-12 stsp return err;
5393 5c1e53bc 2019-07-28 stsp static const struct got_error *
5394 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
5395 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
5397 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
5398 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
5400 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
5401 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
5402 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
5404 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
5405 5c1e53bc 2019-07-28 stsp ct_path++;
5407 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
5408 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
5412 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
5413 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
5415 5c1e53bc 2019-07-28 stsp return NULL;
5418 f0b75401 2019-08-03 stsp static const struct got_error *
5419 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
5421 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
5423 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5424 f0b75401 2019-08-03 stsp *have_staged_files = 1;
5425 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
5428 f0b75401 2019-08-03 stsp return NULL;
5431 5f8a88c6 2019-08-03 stsp static const struct got_error *
5432 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
5433 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
5435 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
5436 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
5438 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5439 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
5441 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5442 5f8a88c6 2019-08-03 stsp if (ie == NULL)
5443 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5444 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5445 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
5446 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
5449 5f8a88c6 2019-08-03 stsp return NULL;
5452 39cd0ff6 2019-07-12 stsp const struct got_error *
5453 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
5454 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
5455 39cd0ff6 2019-07-12 stsp const char *author, const char *committer,
5456 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5457 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
5458 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
5460 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5461 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
5462 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
5463 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5464 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5465 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
5466 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
5467 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5468 f0b75401 2019-08-03 stsp int have_staged_files = 0;
5470 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
5472 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5474 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
5476 39cd0ff6 2019-07-12 stsp goto done;
5478 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5480 39cd0ff6 2019-07-12 stsp goto done;
5482 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5484 39cd0ff6 2019-07-12 stsp goto done;
5486 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5488 39cd0ff6 2019-07-12 stsp goto done;
5490 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5491 5f8a88c6 2019-08-03 stsp &have_staged_files);
5492 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
5493 5f8a88c6 2019-08-03 stsp goto done;
5494 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
5495 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
5497 5f8a88c6 2019-08-03 stsp goto done;
5500 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5501 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
5502 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
5503 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
5504 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
5505 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5506 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5507 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5509 5c1e53bc 2019-07-28 stsp goto done;
5512 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5513 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5514 39cd0ff6 2019-07-12 stsp goto done;
5517 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5518 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
5520 5c1e53bc 2019-07-28 stsp goto done;
5523 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5524 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5525 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5527 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5528 f0b75401 2019-08-03 stsp ct_path++;
5529 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5530 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5531 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5533 b50cabdf 2019-07-12 stsp goto done;
5537 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5538 5c1e53bc 2019-07-28 stsp head_commit_id, worktree, author, committer,
5539 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5541 39cd0ff6 2019-07-12 stsp goto done;
5543 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5544 72fd46fa 2019-09-06 stsp fileindex, have_staged_files);
5545 2e1fa222 2020-07-23 stsp if (err == NULL) {
5546 2e1fa222 2020-07-23 stsp err = reinstall_symlinks_after_commit(&commitable_paths,
5547 2e1fa222 2020-07-23 stsp *new_commit_id, fileindex, worktree, repo);
5549 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5550 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5551 39cd0ff6 2019-07-12 stsp err = sync_err;
5553 39cd0ff6 2019-07-12 stsp if (fileindex)
5554 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5555 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5556 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5557 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5558 39cd0ff6 2019-07-12 stsp err = unlockerr;
5559 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5560 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5561 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5563 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5564 c4296144 2019-05-09 stsp return err;
5567 8656d6c4 2019-05-20 stsp const char *
5568 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5570 8656d6c4 2019-05-20 stsp return ct->path;
5573 8656d6c4 2019-05-20 stsp unsigned int
5574 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
5576 8656d6c4 2019-05-20 stsp return ct->status;
5579 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
5580 818c7501 2019-07-11 stsp struct got_worktree *worktree;
5581 818c7501 2019-07-11 stsp struct got_repository *repo;
5584 818c7501 2019-07-11 stsp static const struct got_error *
5585 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5587 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5588 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
5589 818c7501 2019-07-11 stsp unsigned char status;
5590 818c7501 2019-07-11 stsp struct stat sb;
5591 818c7501 2019-07-11 stsp char *ondisk_path;
5593 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
5594 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5595 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
5596 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
5598 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5600 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
5602 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
5603 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5604 818c7501 2019-07-11 stsp free(ondisk_path);
5606 818c7501 2019-07-11 stsp return err;
5608 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
5609 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
5610 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5611 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5613 818c7501 2019-07-11 stsp return NULL;
5616 818c7501 2019-07-11 stsp const struct got_error *
5617 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5618 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5619 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
5620 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5622 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5623 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5624 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
5625 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
5626 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
5627 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5628 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
5630 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5631 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5632 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5634 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
5636 818c7501 2019-07-11 stsp return err;
5638 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5640 818c7501 2019-07-11 stsp goto done;
5642 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
5643 818c7501 2019-07-11 stsp ok_arg.repo = repo;
5644 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5647 818c7501 2019-07-11 stsp goto done;
5649 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5651 818c7501 2019-07-11 stsp goto done;
5653 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5655 818c7501 2019-07-11 stsp goto done;
5657 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5659 818c7501 2019-07-11 stsp goto done;
5661 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5664 e51d7b55 2020-01-04 stsp goto done;
5666 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5668 818c7501 2019-07-11 stsp goto done;
5669 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5670 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5671 e51d7b55 2020-01-04 stsp goto done;
5674 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
5675 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
5677 818c7501 2019-07-11 stsp goto done;
5678 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
5680 818c7501 2019-07-11 stsp goto done;
5682 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
5684 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5686 818c7501 2019-07-11 stsp goto done;
5688 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
5690 818c7501 2019-07-11 stsp goto done;
5692 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
5693 818c7501 2019-07-11 stsp worktree->base_commit_id);
5695 818c7501 2019-07-11 stsp goto done;
5696 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
5698 818c7501 2019-07-11 stsp goto done;
5700 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
5702 818c7501 2019-07-11 stsp goto done;
5704 818c7501 2019-07-11 stsp free(fileindex_path);
5705 818c7501 2019-07-11 stsp free(tmp_branch_name);
5706 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
5707 818c7501 2019-07-11 stsp free(branch_ref_name);
5708 818c7501 2019-07-11 stsp if (branch_ref)
5709 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5710 818c7501 2019-07-11 stsp if (wt_branch)
5711 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
5712 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
5713 818c7501 2019-07-11 stsp if (err) {
5714 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
5715 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
5716 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5718 818c7501 2019-07-11 stsp if (*tmp_branch) {
5719 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5720 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5722 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5723 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5724 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5726 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
5728 818c7501 2019-07-11 stsp return err;
5731 818c7501 2019-07-11 stsp const struct got_error *
5732 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
5733 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5734 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
5735 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
5737 818c7501 2019-07-11 stsp const struct got_error *err;
5738 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5739 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5740 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5741 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
5742 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
5744 818c7501 2019-07-11 stsp *commit_id = NULL;
5745 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
5746 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
5747 3e3a69f1 2019-07-25 stsp *branch = NULL;
5748 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5750 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
5752 3e3a69f1 2019-07-25 stsp return err;
5754 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5756 f032f1f7 2019-08-04 stsp goto done;
5758 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5759 f032f1f7 2019-08-04 stsp &have_staged_files);
5760 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
5761 f032f1f7 2019-08-04 stsp goto done;
5762 f032f1f7 2019-08-04 stsp if (have_staged_files) {
5763 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
5764 3e3a69f1 2019-07-25 stsp goto done;
5767 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5769 f032f1f7 2019-08-04 stsp goto done;
5771 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5773 818c7501 2019-07-11 stsp goto done;
5775 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5777 818c7501 2019-07-11 stsp goto done;
5779 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5781 818c7501 2019-07-11 stsp goto done;
5783 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5785 818c7501 2019-07-11 stsp goto done;
5787 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
5788 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
5790 818c7501 2019-07-11 stsp goto done;
5792 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5794 818c7501 2019-07-11 stsp goto done;
5796 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
5798 818c7501 2019-07-11 stsp goto done;
5800 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
5801 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
5803 818c7501 2019-07-11 stsp goto done;
5805 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5807 818c7501 2019-07-11 stsp goto done;
5809 818c7501 2019-07-11 stsp free(commit_ref_name);
5810 818c7501 2019-07-11 stsp free(branch_ref_name);
5811 3e3a69f1 2019-07-25 stsp free(fileindex_path);
5812 818c7501 2019-07-11 stsp if (commit_ref)
5813 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
5814 818c7501 2019-07-11 stsp if (branch_ref)
5815 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5816 818c7501 2019-07-11 stsp if (err) {
5817 818c7501 2019-07-11 stsp free(*commit_id);
5818 818c7501 2019-07-11 stsp *commit_id = NULL;
5819 818c7501 2019-07-11 stsp if (*tmp_branch) {
5820 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5821 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5823 818c7501 2019-07-11 stsp if (*new_base_branch) {
5824 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
5825 818c7501 2019-07-11 stsp *new_base_branch = NULL;
5827 818c7501 2019-07-11 stsp if (*branch) {
5828 818c7501 2019-07-11 stsp got_ref_close(*branch);
5829 818c7501 2019-07-11 stsp *branch = NULL;
5831 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5832 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5833 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5835 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
5837 818c7501 2019-07-11 stsp return err;
5840 818c7501 2019-07-11 stsp const struct got_error *
5841 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5843 818c7501 2019-07-11 stsp const struct got_error *err;
5844 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
5846 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5848 818c7501 2019-07-11 stsp return err;
5850 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5851 818c7501 2019-07-11 stsp free(tmp_branch_name);
5852 818c7501 2019-07-11 stsp return NULL;
5855 818c7501 2019-07-11 stsp static const struct got_error *
5856 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5857 818c7501 2019-07-11 stsp char **logmsg, void *arg)
5859 0ebf8283 2019-07-24 stsp *logmsg = arg;
5860 818c7501 2019-07-11 stsp return NULL;
5863 818c7501 2019-07-11 stsp static const struct got_error *
5864 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5865 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
5866 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5867 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5869 818c7501 2019-07-11 stsp return NULL;
5872 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
5873 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
5874 01757395 2019-07-12 stsp void *progress_arg;
5875 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
5878 01757395 2019-07-12 stsp static const struct got_error *
5879 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
5881 01757395 2019-07-12 stsp const struct got_error *err;
5882 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
5884 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
5886 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
5888 01757395 2019-07-12 stsp return err;
5890 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
5891 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
5892 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
5893 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
5894 01757395 2019-07-12 stsp return NULL;
5896 01757395 2019-07-12 stsp p = strdup(path);
5897 01757395 2019-07-12 stsp if (p == NULL)
5898 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
5900 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5901 01757395 2019-07-12 stsp if (err || new == NULL)
5903 01757395 2019-07-12 stsp return err;
5907 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5909 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
5911 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
5912 01757395 2019-07-12 stsp free((char *)pe->path);
5914 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
5917 0ebf8283 2019-07-24 stsp static const struct got_error *
5918 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5919 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
5921 818c7501 2019-07-11 stsp const struct got_error *err;
5922 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
5924 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5925 818c7501 2019-07-11 stsp if (err) {
5926 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
5927 818c7501 2019-07-11 stsp goto done;
5928 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5930 818c7501 2019-07-11 stsp goto done;
5931 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
5933 818c7501 2019-07-11 stsp goto done;
5934 de05890f 2020-03-05 stsp } else if (is_rebase) {
5935 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
5938 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
5940 818c7501 2019-07-11 stsp goto done;
5941 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
5942 818c7501 2019-07-11 stsp free(stored_id);
5943 818c7501 2019-07-11 stsp if (cmp != 0) {
5944 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
5945 818c7501 2019-07-11 stsp goto done;
5949 0ebf8283 2019-07-24 stsp if (commit_ref)
5950 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
5951 0ebf8283 2019-07-24 stsp return err;
5954 0ebf8283 2019-07-24 stsp static const struct got_error *
5955 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
5956 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
5957 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5958 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
5959 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5960 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
5962 0ebf8283 2019-07-24 stsp const struct got_error *err;
5963 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
5964 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
5965 3e3a69f1 2019-07-25 stsp char *fileindex_path;
5967 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
5969 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
5971 0ebf8283 2019-07-24 stsp return err;
5973 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
5974 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
5975 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
5976 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
5977 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
5978 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
5979 818c7501 2019-07-11 stsp if (commit_ref)
5980 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
5981 818c7501 2019-07-11 stsp return err;
5984 818c7501 2019-07-11 stsp const struct got_error *
5985 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5986 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5987 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5988 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
5989 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5990 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
5992 0ebf8283 2019-07-24 stsp const struct got_error *err;
5993 0ebf8283 2019-07-24 stsp char *commit_ref_name;
5995 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5997 0ebf8283 2019-07-24 stsp return err;
5999 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6001 0ebf8283 2019-07-24 stsp goto done;
6003 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6004 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6005 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6007 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6008 0ebf8283 2019-07-24 stsp return err;
6011 0ebf8283 2019-07-24 stsp const struct got_error *
6012 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6013 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6014 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6015 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6016 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6017 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6019 0ebf8283 2019-07-24 stsp const struct got_error *err;
6020 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6022 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6024 0ebf8283 2019-07-24 stsp return err;
6026 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6028 0ebf8283 2019-07-24 stsp goto done;
6030 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6031 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6032 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6034 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6035 0ebf8283 2019-07-24 stsp return err;
6038 0ebf8283 2019-07-24 stsp static const struct got_error *
6039 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6040 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6041 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6042 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6043 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
6045 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6046 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6047 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6048 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6049 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6050 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6051 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6053 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6054 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6056 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6058 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6060 3e3a69f1 2019-07-25 stsp return err;
6062 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6063 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6064 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6065 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6067 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6068 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6069 01757395 2019-07-12 stsp * TODO: Ideally, merged_paths would contain a list of commitables
6070 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6072 01757395 2019-07-12 stsp if (merged_paths) {
6073 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6074 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6075 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6076 f2a9dc41 2019-12-13 tracey repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6079 01757395 2019-07-12 stsp goto done;
6082 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6083 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6085 01757395 2019-07-12 stsp goto done;
6088 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6089 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6090 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6092 a0e95631 2019-07-12 stsp goto done;
6093 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6094 a0e95631 2019-07-12 stsp goto done;
6097 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6099 edd02c5e 2019-07-11 stsp goto done;
6101 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6103 818c7501 2019-07-11 stsp goto done;
6105 5943eee2 2019-08-13 stsp if (new_logmsg) {
6106 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6107 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6108 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6109 5943eee2 2019-08-13 stsp goto done;
6112 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6114 5943eee2 2019-08-13 stsp goto done;
6117 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6118 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6119 5c1e53bc 2019-07-28 stsp worktree, got_object_commit_get_author(orig_commit),
6120 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
6121 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6123 a0e95631 2019-07-12 stsp goto done;
6125 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6127 a0e95631 2019-07-12 stsp goto done;
6129 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6131 a0e95631 2019-07-12 stsp goto done;
6133 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6134 72fd46fa 2019-09-06 stsp fileindex, 0);
6135 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6136 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6137 a0e95631 2019-07-12 stsp err = sync_err;
6139 a0e95631 2019-07-12 stsp free(fileindex_path);
6140 a0e95631 2019-07-12 stsp free(head_commit_id);
6141 a0e95631 2019-07-12 stsp if (head_ref)
6142 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6143 818c7501 2019-07-11 stsp if (err) {
6144 818c7501 2019-07-11 stsp free(*new_commit_id);
6145 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6147 818c7501 2019-07-11 stsp return err;
6150 818c7501 2019-07-11 stsp const struct got_error *
6151 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6152 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6153 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6154 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6155 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6157 0ebf8283 2019-07-24 stsp const struct got_error *err;
6158 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6159 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6160 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6162 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6164 0ebf8283 2019-07-24 stsp return err;
6166 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6168 0ebf8283 2019-07-24 stsp goto done;
6169 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6171 0ebf8283 2019-07-24 stsp goto done;
6172 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6173 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6174 0ebf8283 2019-07-24 stsp goto done;
6177 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6178 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6180 0ebf8283 2019-07-24 stsp if (commit_ref)
6181 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6182 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6183 0ebf8283 2019-07-24 stsp free(commit_id);
6184 0ebf8283 2019-07-24 stsp return err;
6187 0ebf8283 2019-07-24 stsp const struct got_error *
6188 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6189 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6190 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6191 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6192 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6193 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6195 0ebf8283 2019-07-24 stsp const struct got_error *err;
6196 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6197 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6199 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6201 0ebf8283 2019-07-24 stsp return err;
6203 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6205 0ebf8283 2019-07-24 stsp goto done;
6207 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6208 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6210 0ebf8283 2019-07-24 stsp if (commit_ref)
6211 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6212 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6213 0ebf8283 2019-07-24 stsp return err;
6216 0ebf8283 2019-07-24 stsp const struct got_error *
6217 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6218 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6220 3e3a69f1 2019-07-25 stsp if (fileindex)
6221 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6222 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6225 69844fba 2019-07-11 stsp static const struct got_error *
6226 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6228 69844fba 2019-07-11 stsp const struct got_error *err;
6229 69844fba 2019-07-11 stsp struct got_reference *ref;
6231 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6232 69844fba 2019-07-11 stsp if (err) {
6233 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6234 69844fba 2019-07-11 stsp return NULL;
6235 69844fba 2019-07-11 stsp return err;
6238 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6239 69844fba 2019-07-11 stsp got_ref_close(ref);
6240 69844fba 2019-07-11 stsp return err;
6243 69844fba 2019-07-11 stsp static const struct got_error *
6244 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6246 69844fba 2019-07-11 stsp const struct got_error *err;
6247 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6248 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6250 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6252 69844fba 2019-07-11 stsp goto done;
6253 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6255 69844fba 2019-07-11 stsp goto done;
6257 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6259 69844fba 2019-07-11 stsp goto done;
6260 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6262 69844fba 2019-07-11 stsp goto done;
6264 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6266 69844fba 2019-07-11 stsp goto done;
6267 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6269 69844fba 2019-07-11 stsp goto done;
6271 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6273 69844fba 2019-07-11 stsp goto done;
6274 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6276 69844fba 2019-07-11 stsp goto done;
6279 69844fba 2019-07-11 stsp free(tmp_branch_name);
6280 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6281 69844fba 2019-07-11 stsp free(branch_ref_name);
6282 69844fba 2019-07-11 stsp free(commit_ref_name);
6283 69844fba 2019-07-11 stsp return err;
6286 818c7501 2019-07-11 stsp const struct got_error *
6287 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
6288 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6289 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6290 818c7501 2019-07-11 stsp struct got_repository *repo)
6292 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
6293 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
6295 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6297 818c7501 2019-07-11 stsp return err;
6299 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6301 818c7501 2019-07-11 stsp goto done;
6303 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
6305 818c7501 2019-07-11 stsp goto done;
6307 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
6309 818c7501 2019-07-11 stsp goto done;
6311 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
6313 3e3a69f1 2019-07-25 stsp if (fileindex)
6314 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6315 818c7501 2019-07-11 stsp free(new_head_commit_id);
6316 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6317 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6318 818c7501 2019-07-11 stsp err = unlockerr;
6319 818c7501 2019-07-11 stsp return err;
6322 818c7501 2019-07-11 stsp const struct got_error *
6323 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
6324 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6325 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
6326 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6328 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
6329 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
6330 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
6331 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6332 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6333 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
6335 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6337 818c7501 2019-07-11 stsp return err;
6339 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
6340 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
6342 818c7501 2019-07-11 stsp goto done;
6344 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
6346 818c7501 2019-07-11 stsp goto done;
6349 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
6350 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
6351 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
6353 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
6355 818c7501 2019-07-11 stsp goto done;
6357 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6359 818c7501 2019-07-11 stsp goto done;
6361 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
6362 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
6364 ca355955 2019-07-12 stsp goto done;
6366 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
6368 ca355955 2019-07-12 stsp goto done;
6370 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6372 818c7501 2019-07-11 stsp goto done;
6374 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6375 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6376 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6377 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6378 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6379 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6380 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6381 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6382 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6384 1f1abb7e 2019-08-08 stsp goto sync;
6386 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6387 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
6389 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6390 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
6391 ca355955 2019-07-12 stsp err = sync_err;
6393 818c7501 2019-07-11 stsp got_ref_close(resolved);
6394 a3a2faf2 2019-07-12 stsp free(tree_id);
6395 818c7501 2019-07-11 stsp free(commit_id);
6396 0ebf8283 2019-07-24 stsp if (fileindex)
6397 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
6398 0ebf8283 2019-07-24 stsp free(fileindex_path);
6400 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6401 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6402 0ebf8283 2019-07-24 stsp err = unlockerr;
6403 0ebf8283 2019-07-24 stsp return err;
6406 0ebf8283 2019-07-24 stsp const struct got_error *
6407 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6408 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6409 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
6410 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6412 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6413 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6414 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
6415 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
6416 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6417 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
6418 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
6419 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6421 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6422 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6423 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6424 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6426 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6428 0ebf8283 2019-07-24 stsp return err;
6430 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6432 0ebf8283 2019-07-24 stsp goto done;
6434 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
6435 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
6436 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6439 0ebf8283 2019-07-24 stsp goto done;
6441 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6443 0ebf8283 2019-07-24 stsp goto done;
6445 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6447 0ebf8283 2019-07-24 stsp goto done;
6449 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6450 0ebf8283 2019-07-24 stsp worktree);
6452 0ebf8283 2019-07-24 stsp goto done;
6454 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6457 0ebf8283 2019-07-24 stsp goto done;
6459 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6461 0ebf8283 2019-07-24 stsp goto done;
6463 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
6465 0ebf8283 2019-07-24 stsp goto done;
6467 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6468 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6470 0ebf8283 2019-07-24 stsp goto done;
6471 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
6473 0ebf8283 2019-07-24 stsp goto done;
6474 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6475 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
6476 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
6477 0ebf8283 2019-07-24 stsp goto done;
6480 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6481 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6483 0ebf8283 2019-07-24 stsp goto done;
6484 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
6486 0ebf8283 2019-07-24 stsp goto done;
6488 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6490 0ebf8283 2019-07-24 stsp goto done;
6492 0ebf8283 2019-07-24 stsp free(fileindex_path);
6493 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6494 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6495 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6496 0ebf8283 2019-07-24 stsp if (wt_branch)
6497 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
6498 0ebf8283 2019-07-24 stsp if (err) {
6499 0ebf8283 2019-07-24 stsp if (*branch_ref) {
6500 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
6501 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6503 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6504 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6505 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6507 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6508 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6509 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6510 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6512 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
6514 0ebf8283 2019-07-24 stsp return err;
6517 0ebf8283 2019-07-24 stsp const struct got_error *
6518 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
6519 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6521 3e3a69f1 2019-07-25 stsp if (fileindex)
6522 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6523 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
6526 0ebf8283 2019-07-24 stsp const struct got_error *
6527 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
6528 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
6530 0ebf8283 2019-07-24 stsp const struct got_error *err;
6531 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6533 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6535 0ebf8283 2019-07-24 stsp return err;
6537 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6538 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6539 0ebf8283 2019-07-24 stsp return NULL;
6542 0ebf8283 2019-07-24 stsp const struct got_error *
6543 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
6544 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
6545 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6546 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6548 0ebf8283 2019-07-24 stsp const struct got_error *err;
6549 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6550 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6551 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6552 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6553 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6554 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6556 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6557 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6558 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6559 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6561 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6563 3e3a69f1 2019-07-25 stsp return err;
6565 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6567 f032f1f7 2019-08-04 stsp goto done;
6569 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6570 f032f1f7 2019-08-04 stsp &have_staged_files);
6571 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6572 f032f1f7 2019-08-04 stsp goto done;
6573 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6574 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6575 3e3a69f1 2019-07-25 stsp goto done;
6578 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6580 f032f1f7 2019-08-04 stsp goto done;
6582 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6584 0ebf8283 2019-07-24 stsp goto done;
6586 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6588 0ebf8283 2019-07-24 stsp goto done;
6590 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6591 0ebf8283 2019-07-24 stsp worktree);
6593 0ebf8283 2019-07-24 stsp goto done;
6595 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6597 0ebf8283 2019-07-24 stsp goto done;
6599 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6601 0ebf8283 2019-07-24 stsp goto done;
6602 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6604 0ebf8283 2019-07-24 stsp goto done;
6606 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6608 0ebf8283 2019-07-24 stsp goto done;
6609 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6611 0ebf8283 2019-07-24 stsp goto done;
6613 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6615 0ebf8283 2019-07-24 stsp goto done;
6617 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6618 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6619 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6620 0ebf8283 2019-07-24 stsp if (commit_ref)
6621 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6622 0ebf8283 2019-07-24 stsp if (base_commit_ref)
6623 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
6624 0ebf8283 2019-07-24 stsp if (err) {
6625 0ebf8283 2019-07-24 stsp free(*commit_id);
6626 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6627 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6628 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6629 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6630 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6631 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6633 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6634 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6635 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6637 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
6639 0ebf8283 2019-07-24 stsp return err;
6642 0ebf8283 2019-07-24 stsp static const struct got_error *
6643 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6645 0ebf8283 2019-07-24 stsp const struct got_error *err;
6646 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6647 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6649 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6651 0ebf8283 2019-07-24 stsp goto done;
6652 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
6654 0ebf8283 2019-07-24 stsp goto done;
6656 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6657 0ebf8283 2019-07-24 stsp worktree);
6659 0ebf8283 2019-07-24 stsp goto done;
6660 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
6662 0ebf8283 2019-07-24 stsp goto done;
6664 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6666 0ebf8283 2019-07-24 stsp goto done;
6667 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
6669 0ebf8283 2019-07-24 stsp goto done;
6671 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6673 0ebf8283 2019-07-24 stsp goto done;
6674 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
6676 0ebf8283 2019-07-24 stsp goto done;
6678 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6679 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6680 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6681 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6682 0ebf8283 2019-07-24 stsp return err;
6685 0ebf8283 2019-07-24 stsp const struct got_error *
6686 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
6687 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6688 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
6689 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6691 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
6692 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6693 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6694 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
6695 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6697 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6699 0ebf8283 2019-07-24 stsp return err;
6701 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
6702 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
6704 0ebf8283 2019-07-24 stsp goto done;
6706 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
6708 0ebf8283 2019-07-24 stsp goto done;
6710 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6712 0ebf8283 2019-07-24 stsp goto done;
6714 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6715 0ebf8283 2019-07-24 stsp worktree->path_prefix);
6717 0ebf8283 2019-07-24 stsp goto done;
6719 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
6721 0ebf8283 2019-07-24 stsp goto done;
6723 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6725 0ebf8283 2019-07-24 stsp goto done;
6727 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6728 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6729 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6730 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6731 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6732 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6733 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6734 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
6735 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6737 1f1abb7e 2019-08-08 stsp goto sync;
6739 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6740 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
6742 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6743 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
6744 0ebf8283 2019-07-24 stsp err = sync_err;
6746 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
6747 0ebf8283 2019-07-24 stsp free(tree_id);
6748 818c7501 2019-07-11 stsp free(fileindex_path);
6750 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6751 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6752 818c7501 2019-07-11 stsp err = unlockerr;
6753 818c7501 2019-07-11 stsp return err;
6756 0ebf8283 2019-07-24 stsp const struct got_error *
6757 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
6758 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6759 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
6761 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr;
6762 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
6763 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6765 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6767 0ebf8283 2019-07-24 stsp return err;
6769 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
6770 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
6772 0ebf8283 2019-07-24 stsp goto done;
6774 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
6776 0ebf8283 2019-07-24 stsp goto done;
6778 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
6780 0ebf8283 2019-07-24 stsp goto done;
6782 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
6784 0ebf8283 2019-07-24 stsp goto done;
6786 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
6788 3e3a69f1 2019-07-25 stsp if (fileindex)
6789 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6790 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
6791 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6792 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6793 0ebf8283 2019-07-24 stsp err = unlockerr;
6794 0ebf8283 2019-07-24 stsp return err;
6797 0ebf8283 2019-07-24 stsp const struct got_error *
6798 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6799 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6801 0ebf8283 2019-07-24 stsp const struct got_error *err;
6802 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6804 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6806 0ebf8283 2019-07-24 stsp return err;
6808 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6810 0ebf8283 2019-07-24 stsp goto done;
6812 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
6814 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6815 2822a352 2019-10-15 stsp return err;
6818 2822a352 2019-10-15 stsp const struct got_error *
6819 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6820 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6821 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
6822 2822a352 2019-10-15 stsp struct got_repository *repo)
6824 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
6825 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
6826 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
6828 2822a352 2019-10-15 stsp *fileindex = NULL;
6829 2822a352 2019-10-15 stsp *branch_ref = NULL;
6830 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
6832 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
6834 2822a352 2019-10-15 stsp return err;
6836 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6837 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
6838 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
6839 2822a352 2019-10-15 stsp "update -b or different branch name required");
6840 2822a352 2019-10-15 stsp goto done;
6843 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6845 2822a352 2019-10-15 stsp goto done;
6847 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
6848 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
6849 2822a352 2019-10-15 stsp ok_arg.repo = repo;
6850 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6853 2822a352 2019-10-15 stsp goto done;
6855 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
6857 2822a352 2019-10-15 stsp goto done;
6859 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
6860 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
6862 2822a352 2019-10-15 stsp if (err) {
6863 2822a352 2019-10-15 stsp if (*branch_ref) {
6864 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
6865 2822a352 2019-10-15 stsp *branch_ref = NULL;
6867 2822a352 2019-10-15 stsp if (*base_branch_ref) {
6868 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
6869 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
6871 2822a352 2019-10-15 stsp if (*fileindex) {
6872 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
6873 2822a352 2019-10-15 stsp *fileindex = NULL;
6875 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
6877 0cb83759 2019-08-03 stsp return err;
6880 2822a352 2019-10-15 stsp const struct got_error *
6881 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
6882 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6883 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6884 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6885 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6887 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
6888 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
6889 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
6891 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
6893 2822a352 2019-10-15 stsp goto done;
6895 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
6897 2822a352 2019-10-15 stsp goto done;
6899 2822a352 2019-10-15 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
6900 2822a352 2019-10-15 stsp worktree->path_prefix);
6902 2822a352 2019-10-15 stsp goto done;
6904 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6906 2822a352 2019-10-15 stsp goto done;
6908 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6909 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
6911 2822a352 2019-10-15 stsp goto sync;
6913 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
6915 2822a352 2019-10-15 stsp goto sync;
6917 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
6919 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6920 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
6921 2822a352 2019-10-15 stsp err = sync_err;
6924 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
6925 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6926 2822a352 2019-10-15 stsp err = unlockerr;
6927 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
6929 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
6930 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6931 2822a352 2019-10-15 stsp err = unlockerr;
6932 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
6934 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
6935 2822a352 2019-10-15 stsp free(fileindex_path);
6936 2822a352 2019-10-15 stsp free(tree_id);
6938 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6939 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6940 2822a352 2019-10-15 stsp err = unlockerr;
6941 2822a352 2019-10-15 stsp return err;
6944 2822a352 2019-10-15 stsp const struct got_error *
6945 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
6946 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6947 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6949 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
6951 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
6953 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
6955 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
6956 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
6957 8b692cd0 2019-10-21 stsp err = unlockerr;
6958 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
6960 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
6961 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
6962 8b692cd0 2019-10-21 stsp err = unlockerr;
6963 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
6965 8b692cd0 2019-10-21 stsp return err;
6968 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
6969 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
6970 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
6971 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
6972 2db2652d 2019-08-07 stsp struct got_repository *repo;
6973 2db2652d 2019-08-07 stsp int have_changes;
6976 2db2652d 2019-08-07 stsp const struct got_error *
6977 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
6978 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
6979 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6980 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
6982 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
6983 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
6984 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
6985 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
6986 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
6987 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
6989 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
6990 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
6991 8b13ce36 2019-08-08 stsp return NULL;
6992 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
6993 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
6995 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6996 735ef5ac 2019-08-03 stsp if (ie == NULL)
6997 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6999 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7000 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7001 735ef5ac 2019-08-03 stsp relpath) == -1)
7002 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
7004 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
7005 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
7006 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7007 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
7010 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
7011 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7012 3aa5969e 2019-08-06 stsp goto done;
7013 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
7014 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
7015 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
7016 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7017 735ef5ac 2019-08-03 stsp goto done;
7020 2db2652d 2019-08-07 stsp a->have_changes = 1;
7022 735ef5ac 2019-08-03 stsp p = in_repo_path;
7023 735ef5ac 2019-08-03 stsp while (p[0] == '/')
7025 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
7026 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
7027 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
7029 735ef5ac 2019-08-03 stsp free(in_repo_path);
7030 dc424a06 2019-08-07 stsp return err;
7033 2db2652d 2019-08-07 stsp struct stage_path_arg {
7034 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7035 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7036 2db2652d 2019-08-07 stsp struct got_repository *repo;
7037 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
7038 2db2652d 2019-08-07 stsp void *status_arg;
7039 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
7040 2db2652d 2019-08-07 stsp void *patch_arg;
7041 7b5dc508 2019-10-28 stsp int staged_something;
7044 2db2652d 2019-08-07 stsp static const struct got_error *
7045 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
7046 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7047 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7048 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7050 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
7051 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
7052 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
7053 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
7054 0cb83759 2019-08-03 stsp uint32_t stage;
7055 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
7056 0aeb8099 2020-07-23 stsp struct stat sb;
7058 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
7059 8b13ce36 2019-08-08 stsp return NULL;
7061 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7062 d3e7c587 2019-08-03 stsp if (ie == NULL)
7063 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7065 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7066 2db2652d 2019-08-07 stsp relpath)== -1)
7067 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
7069 0cb83759 2019-08-03 stsp switch (status) {
7070 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
7071 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
7072 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
7073 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
7074 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
7077 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7078 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
7079 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7080 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7081 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
7084 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
7087 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
7088 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
7089 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
7090 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
7091 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
7095 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
7096 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
7099 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7100 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7101 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7102 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
7104 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
7105 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7106 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
7107 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7108 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
7110 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7111 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
7113 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7114 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7116 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7117 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
7118 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
7120 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
7121 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
7123 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7124 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7125 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
7126 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
7129 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7131 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7132 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7136 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
7137 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7138 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7139 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7141 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7142 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7145 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
7147 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
7148 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7150 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
7151 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
7154 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7158 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
7159 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
7160 dc424a06 2019-08-07 stsp free(path_content);
7161 2db2652d 2019-08-07 stsp free(ondisk_path);
7162 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
7163 0ebf8283 2019-07-24 stsp return err;
7166 0cb83759 2019-08-03 stsp const struct got_error *
7167 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
7168 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
7169 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
7170 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7171 1e71573e 2019-08-03 stsp struct got_repository *repo)
7173 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7174 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
7175 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7176 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
7177 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
7178 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
7179 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
7180 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
7182 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7184 0cb83759 2019-08-03 stsp return err;
7186 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
7187 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
7189 735ef5ac 2019-08-03 stsp goto done;
7190 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7192 735ef5ac 2019-08-03 stsp goto done;
7193 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7195 0cb83759 2019-08-03 stsp goto done;
7197 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
7198 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
7199 2db2652d 2019-08-07 stsp oka.worktree = worktree;
7200 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
7201 2db2652d 2019-08-07 stsp oka.repo = repo;
7202 2db2652d 2019-08-07 stsp oka.have_changes = 0;
7203 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7204 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7205 f2a9dc41 2019-12-13 tracey check_stage_ok, &oka, NULL, NULL, 0, 0);
7207 735ef5ac 2019-08-03 stsp goto done;
7209 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
7210 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7211 2db2652d 2019-08-07 stsp goto done;
7214 2db2652d 2019-08-07 stsp spa.worktree = worktree;
7215 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
7216 2db2652d 2019-08-07 stsp spa.repo = repo;
7217 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
7218 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
7219 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
7220 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
7221 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
7222 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7223 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7224 f2a9dc41 2019-12-13 tracey stage_path, &spa, NULL, NULL, 0, 0);
7226 2db2652d 2019-08-07 stsp goto done;
7228 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
7229 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7230 7b5dc508 2019-10-28 stsp goto done;
7233 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7234 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
7235 0cb83759 2019-08-03 stsp err = sync_err;
7237 735ef5ac 2019-08-03 stsp if (head_ref)
7238 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
7239 735ef5ac 2019-08-03 stsp free(head_commit_id);
7240 0cb83759 2019-08-03 stsp free(fileindex_path);
7241 0cb83759 2019-08-03 stsp if (fileindex)
7242 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
7243 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7244 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
7245 0cb83759 2019-08-03 stsp err = unlockerr;
7246 0cb83759 2019-08-03 stsp return err;
7249 ad493afc 2019-08-03 stsp struct unstage_path_arg {
7250 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
7251 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
7252 ad493afc 2019-08-03 stsp struct got_repository *repo;
7253 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
7254 ad493afc 2019-08-03 stsp void *progress_arg;
7255 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
7256 2e1f37b0 2019-08-08 stsp void *patch_arg;
7259 2e1f37b0 2019-08-08 stsp static const struct got_error *
7260 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
7261 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
7262 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
7263 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
7264 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
7266 2e1f37b0 2019-08-08 stsp const struct got_error *err;
7267 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
7268 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7269 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7270 2e1f37b0 2019-08-08 stsp struct stat sb1, sb2;
7271 2e1f37b0 2019-08-08 stsp struct got_diff_changes *changes = NULL;
7272 2e1f37b0 2019-08-08 stsp struct got_diff_state *ds = NULL;
7273 2e1f37b0 2019-08-08 stsp struct got_diff_args *args = NULL;
7274 2e1f37b0 2019-08-08 stsp struct got_diff_change *change;
7275 2e1f37b0 2019-08-08 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7276 2e1f37b0 2019-08-08 stsp int have_content = 0, have_rejected_content = 0;
7278 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7279 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7281 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
7283 2e1f37b0 2019-08-08 stsp return err;
7284 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7286 2e1f37b0 2019-08-08 stsp goto done;
7288 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7290 2e1f37b0 2019-08-08 stsp goto done;
7292 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7294 2e1f37b0 2019-08-08 stsp goto done;
7296 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7298 2e1f37b0 2019-08-08 stsp goto done;
7300 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7302 2e1f37b0 2019-08-08 stsp goto done;
7304 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7306 2e1f37b0 2019-08-08 stsp goto done;
7308 2e1f37b0 2019-08-08 stsp if (stat(path1, &sb1) == -1) {
7309 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path1);
7310 2e1f37b0 2019-08-08 stsp goto done;
7313 2e1f37b0 2019-08-08 stsp if (stat(path2, &sb2) == -1) {
7314 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path2);
7315 2e1f37b0 2019-08-08 stsp goto done;
7318 2e1f37b0 2019-08-08 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
7319 2e1f37b0 2019-08-08 stsp f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7321 2e1f37b0 2019-08-08 stsp goto done;
7323 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
7324 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
7326 2e1f37b0 2019-08-08 stsp goto done;
7327 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
7328 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
7330 2e1f37b0 2019-08-08 stsp goto done;
7332 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
7333 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
7334 2e1f37b0 2019-08-08 stsp goto done;
7336 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
7337 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
7338 2e1f37b0 2019-08-08 stsp goto done;
7340 2e1f37b0 2019-08-08 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7341 2e1f37b0 2019-08-08 stsp int choice;
7342 2e1f37b0 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
7343 2e1f37b0 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
7344 2e1f37b0 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
7345 2e1f37b0 2019-08-08 stsp outfile, rejectfile, patch_cb, patch_arg);
7347 2e1f37b0 2019-08-08 stsp goto done;
7348 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
7349 2e1f37b0 2019-08-08 stsp have_content = 1;
7351 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
7352 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
7355 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
7356 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7357 f1e81a05 2019-08-10 stsp outfile, rejectfile);
7359 2e1f37b0 2019-08-08 stsp free(label1);
7361 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
7362 2e1f37b0 2019-08-08 stsp if (staged_blob)
7363 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
7364 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
7365 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
7366 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
7367 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
7368 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
7369 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
7370 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7371 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
7372 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
7373 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
7374 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
7375 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
7376 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
7377 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
7378 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
7379 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7380 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
7381 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
7382 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7384 2e1f37b0 2019-08-08 stsp if (err || !have_rejected_content) {
7385 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
7386 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
7387 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7388 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
7389 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
7390 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7392 2e1f37b0 2019-08-08 stsp free(args);
7394 2e1f37b0 2019-08-08 stsp got_diff_state_free(ds);
7397 2e1f37b0 2019-08-08 stsp if (changes)
7398 2e1f37b0 2019-08-08 stsp got_diff_free_changes(changes);
7399 2e1f37b0 2019-08-08 stsp free(path1);
7400 2e1f37b0 2019-08-08 stsp free(path2);
7401 2e1f37b0 2019-08-08 stsp return err;
7404 ad493afc 2019-08-03 stsp static const struct got_error *
7405 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
7406 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
7407 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7408 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7410 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
7411 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
7412 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
7413 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7414 2e1f37b0 2019-08-08 stsp char *ondisk_path = NULL, *path_unstaged_content = NULL;
7415 2e1f37b0 2019-08-08 stsp char *path_new_staged_content = NULL;
7416 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
7417 ad493afc 2019-08-03 stsp int local_changes_subsumed;
7418 9bc94a15 2019-08-03 stsp struct stat sb;
7420 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
7421 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
7422 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
7423 2e1f37b0 2019-08-08 stsp return NULL;
7425 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7426 ad493afc 2019-08-03 stsp if (ie == NULL)
7427 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7429 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7431 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
7433 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
7434 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
7436 f69721c3 2019-10-21 stsp goto done;
7437 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7438 f69721c3 2019-10-21 stsp id_str) == -1) {
7439 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
7440 f69721c3 2019-10-21 stsp goto done;
7443 ad493afc 2019-08-03 stsp switch (staged_status) {
7444 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
7445 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
7446 ad493afc 2019-08-03 stsp blob_id, 8192);
7449 ad493afc 2019-08-03 stsp /* fall through */
7450 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
7451 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7452 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
7453 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7454 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7455 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7458 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
7461 2e1f37b0 2019-08-08 stsp err = create_unstaged_content(
7462 2e1f37b0 2019-08-08 stsp &path_unstaged_content,
7463 2e1f37b0 2019-08-08 stsp &path_new_staged_content, blob_id,
7464 2e1f37b0 2019-08-08 stsp staged_blob_id, ie->path, a->repo,
7465 2e1f37b0 2019-08-08 stsp a->patch_cb, a->patch_arg);
7466 2e1f37b0 2019-08-08 stsp if (err || path_unstaged_content == NULL)
7468 2e1f37b0 2019-08-08 stsp if (path_new_staged_content) {
7469 2e1f37b0 2019-08-08 stsp err = got_object_blob_create(
7470 2e1f37b0 2019-08-08 stsp &staged_blob_id,
7471 2e1f37b0 2019-08-08 stsp path_new_staged_content,
7475 2e1f37b0 2019-08-08 stsp memcpy(ie->staged_blob_sha1,
7476 2e1f37b0 2019-08-08 stsp staged_blob_id->sha1,
7477 2e1f37b0 2019-08-08 stsp SHA1_DIGEST_LENGTH);
7479 2e1f37b0 2019-08-08 stsp err = merge_file(&local_changes_subsumed,
7480 2e1f37b0 2019-08-08 stsp a->worktree, blob_base, ondisk_path,
7481 2e1f37b0 2019-08-08 stsp relpath, got_fileindex_perms_to_st(ie),
7482 f69721c3 2019-10-21 stsp path_unstaged_content, label_orig,
7483 f69721c3 2019-10-21 stsp "unstaged", a->repo, a->progress_cb,
7484 f69721c3 2019-10-21 stsp a->progress_arg);
7485 2e1f37b0 2019-08-08 stsp if (err == NULL &&
7486 2e1f37b0 2019-08-08 stsp path_new_staged_content == NULL)
7487 2e1f37b0 2019-08-08 stsp got_fileindex_entry_stage_set(ie,
7488 2e1f37b0 2019-08-08 stsp GOT_FILEIDX_STAGE_NONE);
7489 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
7492 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
7493 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
7496 ad493afc 2019-08-03 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
7497 ad493afc 2019-08-03 stsp blob_base, ondisk_path, relpath,
7498 f69721c3 2019-10-21 stsp got_fileindex_perms_to_st(ie), label_orig, blob_staged,
7499 ad493afc 2019-08-03 stsp commit_id ? commit_id : a->worktree->base_commit_id,
7500 ad493afc 2019-08-03 stsp a->repo, a->progress_cb, a->progress_arg);
7501 ad493afc 2019-08-03 stsp if (err == NULL)
7502 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
7503 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
7505 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
7506 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7507 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7508 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7509 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7512 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7514 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7515 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7519 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7520 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
7521 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
7524 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
7528 ad493afc 2019-08-03 stsp free(ondisk_path);
7529 2e1f37b0 2019-08-08 stsp if (path_unstaged_content &&
7530 2e1f37b0 2019-08-08 stsp unlink(path_unstaged_content) == -1 && err == NULL)
7531 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
7532 2e1f37b0 2019-08-08 stsp if (path_new_staged_content &&
7533 2e1f37b0 2019-08-08 stsp unlink(path_new_staged_content) == -1 && err == NULL)
7534 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
7535 2e1f37b0 2019-08-08 stsp free(path_unstaged_content);
7536 2e1f37b0 2019-08-08 stsp free(path_new_staged_content);
7537 ad493afc 2019-08-03 stsp if (blob_base)
7538 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
7539 ad493afc 2019-08-03 stsp if (blob_staged)
7540 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
7541 f69721c3 2019-10-21 stsp free(id_str);
7542 f69721c3 2019-10-21 stsp free(label_orig);
7543 ad493afc 2019-08-03 stsp return err;
7546 ad493afc 2019-08-03 stsp const struct got_error *
7547 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
7548 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
7549 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7550 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7551 ad493afc 2019-08-03 stsp struct got_repository *repo)
7553 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7554 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
7555 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7556 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
7557 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
7559 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7561 ad493afc 2019-08-03 stsp return err;
7563 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7565 ad493afc 2019-08-03 stsp goto done;
7567 ad493afc 2019-08-03 stsp upa.worktree = worktree;
7568 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
7569 ad493afc 2019-08-03 stsp upa.repo = repo;
7570 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
7571 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
7572 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
7573 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
7574 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7575 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7576 f2a9dc41 2019-12-13 tracey unstage_path, &upa, NULL, NULL, 0, 0);
7578 ad493afc 2019-08-03 stsp goto done;
7581 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7582 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
7583 ad493afc 2019-08-03 stsp err = sync_err;
7585 ad493afc 2019-08-03 stsp free(fileindex_path);
7586 ad493afc 2019-08-03 stsp if (fileindex)
7587 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
7588 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7589 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
7590 ad493afc 2019-08-03 stsp err = unlockerr;
7591 ad493afc 2019-08-03 stsp return err;