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 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
928 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
929 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
930 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
931 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
932 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
933 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
934 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
935 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
936 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
940 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
941 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
944 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
945 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
948 11cc08c1 2020-07-23 stsp if (chmod(ondisk_path, GOT_DEFAULT_FILE_MODE) == -1) {
949 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("chmod", ondisk_path);
953 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
954 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
955 11cc08c1 2020-07-23 stsp free(path);
956 11cc08c1 2020-07-23 stsp free(id_str);
957 11cc08c1 2020-07-23 stsp free(label_deriv);
958 11cc08c1 2020-07-23 stsp return err;
961 d219f183 2020-07-23 stsp /* forward declaration */
962 d219f183 2020-07-23 stsp static const struct got_error *
963 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
964 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
965 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
966 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
969 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
970 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
971 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
972 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
974 af57b12a 2020-07-23 stsp static const struct got_error *
975 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
976 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
977 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
978 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
979 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
981 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
982 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
983 af57b12a 2020-07-23 stsp struct stat sb;
984 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
985 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
986 11cc08c1 2020-07-23 stsp int have_local_change = 0;
987 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
989 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
990 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
992 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
993 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
994 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
995 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
996 af57b12a 2020-07-23 stsp ondisk_path);
999 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
1001 526a746f 2020-07-23 stsp if (blob_orig) {
1002 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1004 526a746f 2020-07-23 stsp goto done;
1007 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
1008 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
1009 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1010 11cc08c1 2020-07-23 stsp have_local_change = 1;
1012 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
1013 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
1014 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
1015 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1016 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
1018 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
1019 11cc08c1 2020-07-23 stsp if (ancestor_target) {
1020 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
1021 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1023 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
1024 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1025 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
1026 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1029 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
1030 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1031 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
1032 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
1034 11cc08c1 2020-07-23 stsp goto done;
1035 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1038 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
1039 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
1040 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
1041 af57b12a 2020-07-23 stsp strlen(deriv_target));
1043 af57b12a 2020-07-23 stsp goto done;
1044 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1045 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
1046 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
1047 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1048 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
1049 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1052 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1053 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
1054 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
1056 ea7786be 2020-07-23 stsp goto done;
1057 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1063 af57b12a 2020-07-23 stsp free(ancestor_target);
1064 14c901f1 2019-08-08 stsp return err;
1068 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
1069 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
1070 14c901f1 2019-08-08 stsp * acts as the second derived version.
1072 14c901f1 2019-08-08 stsp static const struct got_error *
1073 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1074 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
1075 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
1076 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
1077 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1078 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1080 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
1081 14c901f1 2019-08-08 stsp FILE *f_deriv = NULL;
1082 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1083 14c901f1 2019-08-08 stsp char *label_deriv = NULL, *parent;
1085 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
1087 14c901f1 2019-08-08 stsp parent = dirname(ondisk_path);
1088 14c901f1 2019-08-08 stsp if (parent == NULL)
1089 14c901f1 2019-08-08 stsp return got_error_from_errno2("dirname", ondisk_path);
1091 14c901f1 2019-08-08 stsp free(base_path);
1092 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1093 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1094 14c901f1 2019-08-08 stsp base_path = NULL;
1095 14c901f1 2019-08-08 stsp goto done;
1098 14c901f1 2019-08-08 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1100 14c901f1 2019-08-08 stsp goto done;
1101 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1102 14c901f1 2019-08-08 stsp blob_deriv);
1104 14c901f1 2019-08-08 stsp goto done;
1106 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1108 14c901f1 2019-08-08 stsp goto done;
1109 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1110 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1111 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1112 14c901f1 2019-08-08 stsp goto done;
1115 14c901f1 2019-08-08 stsp err = merge_file(local_changes_subsumed, worktree, blob_orig,
1116 f69721c3 2019-10-21 stsp ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1117 f69721c3 2019-10-21 stsp label_deriv, repo, progress_cb, progress_arg);
1119 14c901f1 2019-08-08 stsp if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1120 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1121 14c901f1 2019-08-08 stsp free(base_path);
1122 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1123 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1124 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1126 6353ad76 2019-02-08 stsp free(id_str);
1127 818c7501 2019-07-11 stsp free(label_deriv);
1128 4a1ddfc2 2019-01-12 stsp return err;
1131 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1132 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1133 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1134 65b05cec 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_object_id *blob_id)
1136 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1137 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1139 65b05cec 2020-07-23 stsp *new_iep = NULL;
1141 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1143 054041d0 2020-06-24 stsp return err;
1145 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(new_ie, ondisk_path,
1146 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1148 054041d0 2020-06-24 stsp goto done;
1150 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1153 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1155 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1156 13d9040b 2019-03-27 stsp return err;
1159 1ebedb77 2019-10-19 stsp static mode_t
1160 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1162 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1164 1ebedb77 2019-10-19 stsp if (executable) {
1165 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1166 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1167 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1168 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1169 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1170 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1173 1ebedb77 2019-10-19 stsp return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1176 8ba819a3 2020-07-23 stsp /* forward declaration */
1177 13d9040b 2019-03-27 stsp static const struct got_error *
1178 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1179 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1180 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1181 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1182 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1183 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1186 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1187 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1189 8ba819a3 2020-07-23 stsp static const struct got_error *
1190 5a1fbc73 2020-07-23 stsp replace_existing_symlink(const char *ondisk_path, const char *target_path,
1191 5a1fbc73 2020-07-23 stsp size_t target_len)
1193 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1194 5a1fbc73 2020-07-23 stsp ssize_t elen;
1195 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1199 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1200 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1201 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1202 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1203 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1204 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1206 5a1fbc73 2020-07-23 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1207 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1208 5a1fbc73 2020-07-23 stsp if (errno != ELOOP)
1209 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1211 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1212 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1213 5a1fbc73 2020-07-23 stsp if (elen == -1)
1214 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1216 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1217 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1218 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1221 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1222 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1223 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1224 5a1fbc73 2020-07-23 stsp return err;
1227 3c1ec782 2020-07-23 stsp static const struct got_error *
1228 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1229 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1231 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1232 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1233 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1235 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1237 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1238 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1239 3c1ec782 2020-07-23 stsp return NULL;
1243 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1244 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1245 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1246 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1247 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1249 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1250 3c1ec782 2020-07-23 stsp char *abspath;
1251 3c1ec782 2020-07-23 stsp char *parent = dirname(ondisk_path);
1252 3c1ec782 2020-07-23 stsp if (parent == NULL)
1253 3c1ec782 2020-07-23 stsp return got_error_from_errno2("dirname", ondisk_path);
1254 3c1ec782 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1)
1255 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1256 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1257 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1258 3c1ec782 2020-07-23 stsp free(abspath);
1259 3c1ec782 2020-07-23 stsp return err;
1261 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1262 3c1ec782 2020-07-23 stsp free(abspath);
1264 3c1ec782 2020-07-23 stsp return err;
1266 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1268 3c1ec782 2020-07-23 stsp return err;
1271 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1272 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1273 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1274 3c1ec782 2020-07-23 stsp return NULL;
1277 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1278 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1279 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1280 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1281 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1282 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1284 3c1ec782 2020-07-23 stsp free(path_got);
1285 3c1ec782 2020-07-23 stsp return NULL;
1288 5a1fbc73 2020-07-23 stsp static const struct got_error *
1289 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1290 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1291 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1292 c90c8ce3 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1293 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1295 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1296 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1297 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1298 906c123b 2020-07-23 stsp char *path_got = NULL;
1299 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1300 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1302 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1305 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1306 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1307 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1308 8ba819a3 2020-07-23 stsp * in the blob object.
1311 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1312 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1313 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1314 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1315 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1316 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1317 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1318 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1319 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1320 3b9f0f87 2020-07-23 stsp progress_arg);
1322 8ba819a3 2020-07-23 stsp if (len > 0) {
1323 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1324 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1325 8ba819a3 2020-07-23 stsp len - hdrlen);
1326 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1327 8ba819a3 2020-07-23 stsp hdrlen = 0;
1329 8ba819a3 2020-07-23 stsp } while (len != 0);
1330 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1332 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1333 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1335 3c1ec782 2020-07-23 stsp return err;
1337 3c1ec782 2020-07-23 stsp if (*is_bad_symlink) {
1338 906c123b 2020-07-23 stsp /* install as a regular file */
1339 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1340 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1341 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1342 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1343 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1344 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1345 906c123b 2020-07-23 stsp goto done;
1348 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1349 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1350 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1351 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1352 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1353 c90c8ce3 2020-07-23 stsp goto done;
1355 5a1fbc73 2020-07-23 stsp err = replace_existing_symlink(ondisk_path,
1356 5a1fbc73 2020-07-23 stsp target_path, target_len);
1358 f35fa46a 2020-07-23 stsp goto done;
1359 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1360 5a1fbc73 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1361 6e1eade5 2020-07-23 stsp reverting_versioned_file ?
1362 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1365 5a1fbc73 2020-07-23 stsp goto done; /* Nothing else to do. */
1368 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1369 f35fa46a 2020-07-23 stsp char *parent = dirname(ondisk_path);
1370 f35fa46a 2020-07-23 stsp if (parent == NULL) {
1371 f35fa46a 2020-07-23 stsp err = got_error_from_errno2("dirname",
1372 f35fa46a 2020-07-23 stsp ondisk_path);
1373 f35fa46a 2020-07-23 stsp goto done;
1375 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1377 f35fa46a 2020-07-23 stsp goto done;
1379 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1380 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1382 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1383 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1384 f35fa46a 2020-07-23 stsp goto done;
1388 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1389 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1390 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1391 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1392 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1393 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1394 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1395 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1396 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1397 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1398 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1399 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1400 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1402 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1403 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1405 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1406 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1407 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1409 906c123b 2020-07-23 stsp free(path_got);
1410 8ba819a3 2020-07-23 stsp return err;
1413 8ba819a3 2020-07-23 stsp static const struct got_error *
1414 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1415 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1416 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1417 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1418 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1419 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1421 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1422 507dc3bb 2018-12-29 stsp int fd = -1;
1423 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1424 507dc3bb 2018-12-29 stsp int update = 0;
1425 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1427 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1428 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
1429 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1430 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1431 21908da4 2019-01-13 stsp char *parent = dirname(path);
1432 21908da4 2019-01-13 stsp if (parent == NULL)
1433 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
1434 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1436 21908da4 2019-01-13 stsp return err;
1437 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1438 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1439 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
1440 21908da4 2019-01-13 stsp if (fd == -1)
1441 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1442 230a42bd 2019-05-11 jcs ondisk_path);
1443 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1444 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1445 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1446 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1447 3b9f0f87 2020-07-23 stsp goto done;
1449 bd6aa359 2020-07-23 stsp if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1450 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1451 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1452 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1453 507dc3bb 2018-12-29 stsp goto done;
1455 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1456 507dc3bb 2018-12-29 stsp ondisk_path);
1458 507dc3bb 2018-12-29 stsp goto done;
1459 507dc3bb 2018-12-29 stsp update = 1;
1462 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1465 bd6aa359 2020-07-23 stsp if (progress_cb) {
1466 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1467 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1469 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1470 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1473 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1474 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1476 bd6aa359 2020-07-23 stsp goto done;
1479 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1481 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1482 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1485 9d31a1d8 2018-03-11 stsp if (len > 0) {
1486 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1487 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1488 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1489 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1490 b87c6f83 2018-12-24 stsp goto done;
1491 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1492 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1493 b87c6f83 2018-12-24 stsp goto done;
1495 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1497 9d31a1d8 2018-03-11 stsp } while (len != 0);
1499 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1500 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1501 816dc654 2019-02-16 stsp goto done;
1504 507dc3bb 2018-12-29 stsp if (update) {
1505 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1506 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1507 230a42bd 2019-05-11 jcs ondisk_path);
1508 2a57020b 2019-02-20 stsp unlink(tmppath);
1509 68ed9ba5 2019-02-10 stsp goto done;
1513 1ebedb77 2019-10-19 stsp if (chmod(ondisk_path,
1514 1ebedb77 2019-10-19 stsp get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1515 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("chmod", ondisk_path);
1516 1ebedb77 2019-10-19 stsp goto done;
1520 3a6ce05a 2019-02-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
1521 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1522 507dc3bb 2018-12-29 stsp free(tmppath);
1523 6353ad76 2019-02-08 stsp return err;
1526 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1527 6353ad76 2019-02-08 stsp static const struct got_error *
1528 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1530 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1531 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1532 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1533 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1534 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1536 7154f6ce 2019-03-27 stsp int i = 0;
1537 7154f6ce 2019-03-27 stsp char *line;
1538 7154f6ce 2019-03-27 stsp size_t len;
1539 7154f6ce 2019-03-27 stsp const char delim[3] = {'\0', '\0', '\0'};
1541 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1542 7154f6ce 2019-03-27 stsp line = fparseln(f, &len, NULL, delim, 0);
1543 7154f6ce 2019-03-27 stsp if (line == NULL) {
1544 7154f6ce 2019-03-27 stsp if (feof(f))
1546 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1550 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1551 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1553 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1559 7154f6ce 2019-03-27 stsp return err;
1562 e2b1e152 2019-07-13 stsp static int
1563 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1565 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1566 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1569 1ebedb77 2019-10-19 stsp static int
1570 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1572 e2b1e152 2019-07-13 stsp return !(ie->ctime_sec == sb->st_ctime &&
1573 e2b1e152 2019-07-13 stsp ie->ctime_nsec == sb->st_ctimensec &&
1574 e2b1e152 2019-07-13 stsp ie->mtime_sec == sb->st_mtime &&
1575 e2b1e152 2019-07-13 stsp ie->mtime_nsec == sb->st_mtimensec &&
1576 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1577 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1580 c363b2c1 2019-08-03 stsp static unsigned char
1581 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1583 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1584 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1585 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1586 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1587 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1588 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1589 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1591 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1595 a919d5c4 2020-07-23 stsp static const struct got_error *
1596 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1597 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1598 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1600 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1601 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1602 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1603 a919d5c4 2020-07-23 stsp ssize_t elen;
1604 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1605 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1606 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1608 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1610 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1612 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1614 a919d5c4 2020-07-23 stsp return err;
1615 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1617 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1618 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1620 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1622 a919d5c4 2020-07-23 stsp if (len > 0) {
1623 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1624 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1625 a919d5c4 2020-07-23 stsp len - hdrlen);
1626 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1627 a919d5c4 2020-07-23 stsp hdrlen = 0;
1629 a919d5c4 2020-07-23 stsp } while (len != 0);
1630 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1632 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1633 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1634 a919d5c4 2020-07-23 stsp if (elen == -1)
1635 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1637 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1638 a919d5c4 2020-07-23 stsp if (elen == -1)
1639 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1642 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1643 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1645 a919d5c4 2020-07-23 stsp return NULL;
1648 7154f6ce 2019-03-27 stsp static const struct got_error *
1649 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1650 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1651 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1653 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1654 6353ad76 2019-02-08 stsp struct got_object_id id;
1655 6353ad76 2019-02-08 stsp size_t hdrlen;
1656 1338848f 2019-12-13 stsp int fd = -1;
1657 6353ad76 2019-02-08 stsp FILE *f = NULL;
1658 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1659 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1660 6353ad76 2019-02-08 stsp size_t flen, blen;
1661 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1663 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1666 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1667 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1668 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1670 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1671 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1672 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1673 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1674 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1676 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1677 882ef1b9 2019-12-13 stsp goto done;
1679 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1680 3d35a492 2019-12-13 stsp goto done;
1683 7f91a133 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1684 a919d5c4 2020-07-23 stsp if (fd == -1 && errno != ENOENT && errno != ELOOP)
1685 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1686 a919d5c4 2020-07-23 stsp else if (fd == -1 && errno == ELOOP) {
1687 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1688 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1689 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1690 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1691 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1692 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1694 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1695 3d35a492 2019-12-13 stsp goto done;
1697 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1698 1338848f 2019-12-13 stsp goto done;
1702 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1703 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1704 1338848f 2019-12-13 stsp goto done;
1707 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1708 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1709 1338848f 2019-12-13 stsp goto done;
1710 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1711 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1712 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1713 1338848f 2019-12-13 stsp goto done;
1716 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1717 f179e66d 2020-07-23 stsp goto done;
1719 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1720 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1721 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1722 1338848f 2019-12-13 stsp goto done;
1725 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1726 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1727 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1729 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1731 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1733 1338848f 2019-12-13 stsp goto done;
1735 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1736 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1737 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1738 a919d5c4 2020-07-23 stsp goto done;
1741 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1742 3d35a492 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1743 ab0d4361 2019-12-13 stsp if (fd == -1) {
1744 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1745 ab0d4361 2019-12-13 stsp goto done;
1749 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1750 6353ad76 2019-02-08 stsp if (f == NULL) {
1751 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1752 6353ad76 2019-02-08 stsp goto done;
1755 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1757 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1758 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1760 7154f6ce 2019-03-27 stsp goto done;
1761 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1762 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1763 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1764 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1765 7154f6ce 2019-03-27 stsp goto done;
1767 6353ad76 2019-02-08 stsp if (blen == 0) {
1768 6353ad76 2019-02-08 stsp if (flen != 0)
1769 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1771 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1772 6353ad76 2019-02-08 stsp if (blen != 0)
1773 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1775 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1776 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1777 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1778 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1782 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1785 6353ad76 2019-02-08 stsp hdrlen = 0;
1788 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1789 7154f6ce 2019-03-27 stsp rewind(f);
1790 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1791 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1792 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1795 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1796 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1797 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1798 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1799 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1800 9d31a1d8 2018-03-11 stsp return err;
1804 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1805 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1807 e2b1e152 2019-07-13 stsp static const struct got_error *
1808 e2b1e152 2019-07-13 stsp sync_timestamps(char *ondisk_path, unsigned char status,
1809 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1811 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1812 e2b1e152 2019-07-13 stsp return got_fileindex_entry_update(ie, ondisk_path,
1813 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1815 e2b1e152 2019-07-13 stsp return NULL;
1818 9d31a1d8 2018-03-11 stsp static const struct got_error *
1819 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1820 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1821 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1822 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1823 0584f854 2019-04-06 stsp void *progress_arg)
1825 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1826 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1827 6353ad76 2019-02-08 stsp char *ondisk_path;
1828 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1829 b8f41171 2019-02-10 stsp struct stat sb;
1831 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1832 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1835 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1836 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1837 a76c42e6 2019-08-03 stsp goto done;
1839 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1842 abb4604f 2019-07-27 stsp goto done;
1843 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1844 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1846 abb4604f 2019-07-27 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1847 c90c8ce3 2020-07-23 stsp status = GOT_STATUS_UNVERSIONED;
1850 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1851 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1852 b8f41171 2019-02-10 stsp goto done;
1854 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1855 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1857 5036ab18 2020-04-18 stsp goto done;
1860 523b8417 2019-10-19 stsp if (ie && status != GOT_STATUS_MISSING &&
1861 523b8417 2019-10-19 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1862 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1863 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1864 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1865 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1867 e2b1e152 2019-07-13 stsp goto done;
1868 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1870 6353ad76 2019-02-08 stsp goto done;
1872 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1873 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1874 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1875 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1876 b8f41171 2019-02-10 stsp goto done;
1880 56e0773d 2019-11-28 stsp err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1882 6353ad76 2019-02-08 stsp goto done;
1884 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1885 234035bc 2019-06-01 stsp int update_timestamps;
1886 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1887 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1888 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1889 234035bc 2019-06-01 stsp struct got_object_id id2;
1890 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1891 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1893 f69721c3 2019-10-21 stsp goto done;
1895 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
1896 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
1897 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1898 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
1899 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
1900 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
1901 f69721c3 2019-10-21 stsp goto done;
1903 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
1904 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
1905 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
1906 234035bc 2019-06-01 stsp goto done;
1909 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1910 36bf999c 2020-07-23 stsp char *link_target;
1911 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
1913 36bf999c 2020-07-23 stsp goto done;
1914 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
1915 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
1916 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
1917 36bf999c 2020-07-23 stsp free(link_target);
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 b2118c49 2020-07-28 stsp const struct got_error *
2147 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2149 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2151 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2152 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2153 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2154 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2157 b2118c49 2020-07-28 stsp return NULL;
2160 818c7501 2019-07-11 stsp static const struct got_error *
2161 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2163 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2164 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2166 517bab73 2019-03-11 stsp *refname = NULL;
2168 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2170 b2118c49 2020-07-28 stsp return err;
2172 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2173 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2174 0647c563 2019-03-11 stsp *refname = NULL;
2176 517bab73 2019-03-11 stsp free(uuidstr);
2177 517bab73 2019-03-11 stsp return err;
2180 818c7501 2019-07-11 stsp const struct got_error *
2181 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2183 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2186 818c7501 2019-07-11 stsp static const struct got_error *
2187 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2189 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2190 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2193 818c7501 2019-07-11 stsp static const struct got_error *
2194 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2196 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2199 818c7501 2019-07-11 stsp static const struct got_error *
2200 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2202 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2203 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2206 818c7501 2019-07-11 stsp static const struct got_error *
2207 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2209 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2210 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2213 0ebf8283 2019-07-24 stsp static const struct got_error *
2214 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2216 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2217 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2220 0ebf8283 2019-07-24 stsp static const struct got_error *
2221 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2223 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2224 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2227 0ebf8283 2019-07-24 stsp static const struct got_error *
2228 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2230 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2231 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2234 0ebf8283 2019-07-24 stsp static const struct got_error *
2235 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2237 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2238 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2241 0ebf8283 2019-07-24 stsp const struct got_error *
2242 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2243 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2245 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2246 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2247 0ebf8283 2019-07-24 stsp *path = NULL;
2248 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2250 0ebf8283 2019-07-24 stsp return NULL;
2254 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2255 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2257 517bab73 2019-03-11 stsp static const struct got_error *
2258 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2260 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2261 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2262 517bab73 2019-03-11 stsp char *refname;
2264 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2266 517bab73 2019-03-11 stsp return err;
2268 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2270 0cd1c46a 2019-03-11 stsp goto done;
2272 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2274 0cd1c46a 2019-03-11 stsp free(refname);
2276 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2277 3e3a69f1 2019-07-25 stsp return err;
2280 3e3a69f1 2019-07-25 stsp static const struct got_error *
2281 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2283 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2285 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2286 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2287 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2288 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2290 9d31a1d8 2018-03-11 stsp return err;
2294 ebf99748 2019-05-09 stsp static const struct got_error *
2295 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2296 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2298 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2299 ebf99748 2019-05-09 stsp FILE *index = NULL;
2301 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2302 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2303 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2304 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2306 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2308 ebf99748 2019-05-09 stsp goto done;
2310 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
2311 ebf99748 2019-05-09 stsp if (index == NULL) {
2312 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2313 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2315 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2316 ebf99748 2019-05-09 stsp if (fclose(index) != 0 && err == NULL)
2317 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2320 ebf99748 2019-05-09 stsp if (err) {
2321 ebf99748 2019-05-09 stsp free(*fileindex_path);
2322 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2323 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2324 ebf99748 2019-05-09 stsp *fileindex = NULL;
2326 ebf99748 2019-05-09 stsp return err;
2329 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2330 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2331 c932eeeb 2019-05-22 stsp const char *path;
2332 c932eeeb 2019-05-22 stsp size_t path_len;
2333 c932eeeb 2019-05-22 stsp const char *entry_name;
2334 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2335 a484d721 2019-06-10 stsp void *progress_arg;
2338 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2339 c932eeeb 2019-05-22 stsp static const struct got_error *
2340 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2342 1ee397ad 2019-07-12 stsp const struct got_error *err;
2343 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2345 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2346 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2347 c932eeeb 2019-05-22 stsp return NULL;
2348 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2349 102ff934 2019-06-11 stsp return NULL;
2351 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2352 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2353 c932eeeb 2019-05-22 stsp return NULL;
2355 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2356 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2357 edd02c5e 2019-07-11 stsp ie->path);
2359 1ee397ad 2019-07-12 stsp return err;
2361 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2362 c932eeeb 2019-05-22 stsp return NULL;
2365 9c6338c4 2019-06-02 stsp static const struct got_error *
2366 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2368 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2369 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2370 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2371 867630bb 2020-01-17 stsp struct timespec timeout;
2373 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2374 9c6338c4 2019-06-02 stsp fileindex_path);
2376 9c6338c4 2019-06-02 stsp goto done;
2378 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2380 9c6338c4 2019-06-02 stsp goto done;
2382 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2383 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2384 9c6338c4 2019-06-02 stsp fileindex_path);
2385 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2389 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2390 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2391 867630bb 2020-01-17 stsp * was recorded in the file index.
2393 867630bb 2020-01-17 stsp timeout.tv_sec = 0;
2394 867630bb 2020-01-17 stsp timeout.tv_nsec = 1;
2395 867630bb 2020-01-17 stsp nanosleep(&timeout, NULL);
2397 9c6338c4 2019-06-02 stsp if (new_index)
2398 9c6338c4 2019-06-02 stsp fclose(new_index);
2399 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2400 9c6338c4 2019-06-02 stsp return err;
2403 6ced4176 2019-07-12 stsp static const struct got_error *
2404 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2405 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2406 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
2408 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2409 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2410 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2411 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2413 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2414 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2415 6ced4176 2019-07-12 stsp *tree_id = NULL;
2417 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2418 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2419 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2420 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2421 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2422 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2423 6ced4176 2019-07-12 stsp goto done;
2425 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2426 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
2428 6ced4176 2019-07-12 stsp goto done;
2429 6ced4176 2019-07-12 stsp return NULL;
2432 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2434 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2435 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2436 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2437 6ced4176 2019-07-12 stsp goto done;
2440 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2441 6ced4176 2019-07-12 stsp in_repo_path);
2443 6ced4176 2019-07-12 stsp goto done;
2445 6ced4176 2019-07-12 stsp free(in_repo_path);
2446 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2448 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2450 6ced4176 2019-07-12 stsp goto done;
2452 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2453 6ced4176 2019-07-12 stsp /* Check out a single file. */
2454 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2455 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2456 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2457 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2458 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2459 6ced4176 2019-07-12 stsp goto done;
2461 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2462 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2463 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2464 6ced4176 2019-07-12 stsp goto done;
2467 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2468 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2470 6ced4176 2019-07-12 stsp return err;
2471 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2472 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2473 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2474 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2475 6ced4176 2019-07-12 stsp goto done;
2478 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2479 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
2481 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2482 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2483 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2484 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2485 6ced4176 2019-07-12 stsp goto done;
2487 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2488 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2489 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2490 6ced4176 2019-07-12 stsp goto done;
2495 6ced4176 2019-07-12 stsp free(in_repo_path);
2496 6ced4176 2019-07-12 stsp if (err) {
2497 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2498 6ced4176 2019-07-12 stsp free(*tree_relpath);
2499 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2500 6ced4176 2019-07-12 stsp free(*tree_id);
2501 6ced4176 2019-07-12 stsp *tree_id = NULL;
2503 07ed472d 2019-07-12 stsp return err;
2506 07ed472d 2019-07-12 stsp static const struct got_error *
2507 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2508 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2509 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2510 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2512 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2513 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2514 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2515 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2516 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2518 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2519 7f47418f 2019-12-20 stsp if (err) {
2520 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2521 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2522 7f47418f 2019-12-20 stsp goto done;
2523 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2524 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2526 7f47418f 2019-12-20 stsp return err;
2529 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2530 07ed472d 2019-07-12 stsp worktree->base_commit_id);
2532 07ed472d 2019-07-12 stsp goto done;
2534 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2536 07ed472d 2019-07-12 stsp goto done;
2538 07ed472d 2019-07-12 stsp if (entry_name &&
2539 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2540 07ed472d 2019-07-12 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
2541 07ed472d 2019-07-12 stsp goto done;
2544 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2545 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2546 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2547 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2548 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2549 07ed472d 2019-07-12 stsp arg.repo = repo;
2550 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2551 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2552 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2553 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2554 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2555 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2558 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2559 07ed472d 2019-07-12 stsp if (commit)
2560 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2561 6ced4176 2019-07-12 stsp return err;
2564 9d31a1d8 2018-03-11 stsp const struct got_error *
2565 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2566 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2567 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2568 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2570 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2571 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2572 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2573 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2574 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2575 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2576 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2577 f2ea84fa 2019-07-27 stsp SIMPLEQ_ENTRY(tree_path_data) entry;
2578 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2579 f2ea84fa 2019-07-27 stsp int entry_type;
2580 f2ea84fa 2019-07-27 stsp char *relpath;
2581 f2ea84fa 2019-07-27 stsp char *entry_name;
2582 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2583 f2ea84fa 2019-07-27 stsp SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2585 f2ea84fa 2019-07-27 stsp SIMPLEQ_INIT(&tree_paths);
2587 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2589 9d31a1d8 2018-03-11 stsp return err;
2591 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2592 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2593 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2594 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2595 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2596 f2ea84fa 2019-07-27 stsp goto done;
2599 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2600 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2601 f2ea84fa 2019-07-27 stsp if (err) {
2602 f2ea84fa 2019-07-27 stsp free(tpd);
2603 6ced4176 2019-07-12 stsp goto done;
2606 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2607 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2608 f2ea84fa 2019-07-27 stsp if (err) {
2609 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2610 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2611 f2ea84fa 2019-07-27 stsp free(tpd);
2612 f2ea84fa 2019-07-27 stsp goto done;
2615 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2617 f2ea84fa 2019-07-27 stsp SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2621 51514078 2018-12-25 stsp * Read the file index.
2622 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2623 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2625 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2627 c4cdcb68 2019-04-03 stsp goto done;
2629 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2630 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2631 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2633 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2634 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2635 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2639 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2640 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2641 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2642 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2643 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2644 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2645 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2646 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2650 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_NEXT(tpd, entry);
2652 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2653 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2654 af12c6b9 2019-06-04 stsp err = sync_err;
2656 9c6338c4 2019-06-02 stsp free(fileindex_path);
2658 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2659 9d31a1d8 2018-03-11 stsp if (commit)
2660 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2661 5ade8233 2019-07-12 stsp if (fileindex)
2662 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2663 f2ea84fa 2019-07-27 stsp while (!SIMPLEQ_EMPTY(&tree_paths)) {
2664 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2665 f2ea84fa 2019-07-27 stsp SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2666 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2667 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2668 f2ea84fa 2019-07-27 stsp free(tpd);
2670 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2671 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2672 9d31a1d8 2018-03-11 stsp err = unlockerr;
2673 f8d1f275 2019-02-04 stsp return err;
2676 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2677 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2678 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2679 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2680 234035bc 2019-06-01 stsp void *progress_arg;
2681 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2682 234035bc 2019-06-01 stsp void *cancel_arg;
2683 f69721c3 2019-10-21 stsp const char *label_orig;
2684 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2687 234035bc 2019-06-01 stsp static const struct got_error *
2688 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2689 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
2690 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
2691 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2693 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2694 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2695 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2696 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2697 234035bc 2019-06-01 stsp struct stat sb;
2698 234035bc 2019-06-01 stsp unsigned char status;
2699 234035bc 2019-06-01 stsp int local_changes_subsumed;
2701 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2702 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2703 d6c87207 2019-08-02 stsp strlen(path2));
2704 1ee397ad 2019-07-12 stsp if (ie == NULL)
2705 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2706 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2708 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2709 234035bc 2019-06-01 stsp path2) == -1)
2710 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2712 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2715 234035bc 2019-06-01 stsp goto done;
2717 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2718 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2719 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2720 234035bc 2019-06-01 stsp goto done;
2722 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2723 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2724 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2725 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2726 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2727 234035bc 2019-06-01 stsp goto done;
2730 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2731 36bf999c 2020-07-23 stsp char *link_target2;
2732 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2734 36bf999c 2020-07-23 stsp goto done;
2735 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2736 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2737 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2738 36bf999c 2020-07-23 stsp free(link_target2);
2740 af57b12a 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
2741 af57b12a 2020-07-23 stsp blob1, ondisk_path, path2, sb.st_mode,
2742 af57b12a 2020-07-23 stsp a->label_orig, blob2, a->commit_id2, repo,
2743 af57b12a 2020-07-23 stsp a->progress_cb, a->progress_arg);
2745 234035bc 2019-06-01 stsp } else if (blob1) {
2746 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2747 d6c87207 2019-08-02 stsp strlen(path1));
2748 1ee397ad 2019-07-12 stsp if (ie == NULL)
2749 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2750 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2752 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2753 2b92fad7 2019-06-02 stsp path1) == -1)
2754 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2756 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2759 2b92fad7 2019-06-02 stsp goto done;
2761 2b92fad7 2019-06-02 stsp switch (status) {
2762 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2763 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2764 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2766 1ee397ad 2019-07-12 stsp goto done;
2767 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2769 2b92fad7 2019-06-02 stsp goto done;
2771 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2773 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2774 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2775 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2776 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2778 1ee397ad 2019-07-12 stsp goto done;
2780 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2782 2b92fad7 2019-06-02 stsp case GOT_STATUS_ADD:
2783 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2784 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2785 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2786 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2788 1ee397ad 2019-07-12 stsp goto done;
2790 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2791 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2793 1ee397ad 2019-07-12 stsp goto done;
2798 234035bc 2019-06-01 stsp } else if (blob2) {
2799 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2800 234035bc 2019-06-01 stsp path2) == -1)
2801 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2802 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2803 d6c87207 2019-08-02 stsp strlen(path2));
2805 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2806 7f91a133 2019-12-13 stsp -1, NULL, repo);
2808 234035bc 2019-06-01 stsp goto done;
2809 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2810 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2811 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2812 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2813 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2814 1ee397ad 2019-07-12 stsp status, path2);
2815 234035bc 2019-06-01 stsp goto done;
2817 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2818 36bf999c 2020-07-23 stsp char *link_target2;
2819 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
2822 36bf999c 2020-07-23 stsp goto done;
2823 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
2824 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
2825 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
2826 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
2827 36bf999c 2020-07-23 stsp free(link_target2);
2828 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
2829 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
2830 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
2831 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
2832 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
2833 526a746f 2020-07-23 stsp a->progress_arg);
2835 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
2836 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
2839 dfe9fba0 2020-07-23 stsp goto done;
2840 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2841 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
2842 054041d0 2020-06-24 stsp ondisk_path, blob2->id.sha1,
2843 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
2845 234035bc 2019-06-01 stsp goto done;
2848 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2849 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2850 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
2851 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
2852 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
2853 c90c8ce3 2020-07-23 stsp 0, 1, repo, a->progress_cb, a->progress_arg);
2855 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
2856 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2857 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
2860 234035bc 2019-06-01 stsp goto done;
2861 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
2863 234035bc 2019-06-01 stsp goto done;
2864 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ondisk_path,
2865 3969253a 2020-03-07 stsp NULL, NULL, 1);
2866 3969253a 2020-03-07 stsp if (err) {
2867 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
2868 3969253a 2020-03-07 stsp goto done;
2870 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
2871 2b92fad7 2019-06-02 stsp if (err) {
2872 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
2873 2b92fad7 2019-06-02 stsp goto done;
2875 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2876 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2877 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2882 234035bc 2019-06-01 stsp free(ondisk_path);
2883 234035bc 2019-06-01 stsp return err;
2886 234035bc 2019-06-01 stsp struct check_merge_ok_arg {
2887 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2888 234035bc 2019-06-01 stsp struct got_repository *repo;
2891 234035bc 2019-06-01 stsp static const struct got_error *
2892 234035bc 2019-06-01 stsp check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2894 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
2895 234035bc 2019-06-01 stsp struct check_merge_ok_arg *a = arg;
2896 234035bc 2019-06-01 stsp unsigned char status;
2897 234035bc 2019-06-01 stsp struct stat sb;
2898 234035bc 2019-06-01 stsp char *ondisk_path;
2900 234035bc 2019-06-01 stsp /* Reject merges into a work tree with mixed base commits. */
2901 234035bc 2019-06-01 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2902 234035bc 2019-06-01 stsp SHA1_DIGEST_LENGTH))
2903 234035bc 2019-06-01 stsp return got_error(GOT_ERR_MIXED_COMMITS);
2905 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2907 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2909 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
2910 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2912 234035bc 2019-06-01 stsp return err;
2913 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
2914 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
2916 234035bc 2019-06-01 stsp return NULL;
2919 818c7501 2019-07-11 stsp static const struct got_error *
2920 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2921 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
2922 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
2923 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2924 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2926 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
2927 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2928 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2929 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
2930 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2932 03415a1a 2019-06-02 stsp if (commit_id1) {
2933 f69721c3 2019-10-21 stsp char *id_str;
2935 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2936 03415a1a 2019-06-02 stsp worktree->path_prefix);
2938 03415a1a 2019-06-02 stsp goto done;
2940 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2942 03415a1a 2019-06-02 stsp goto done;
2944 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
2946 f69721c3 2019-10-21 stsp goto done;
2948 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2949 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2950 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2951 f69721c3 2019-10-21 stsp free(id_str);
2952 f69721c3 2019-10-21 stsp goto done;
2954 f69721c3 2019-10-21 stsp free(id_str);
2957 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2958 234035bc 2019-06-01 stsp worktree->path_prefix);
2960 234035bc 2019-06-01 stsp goto done;
2962 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2964 234035bc 2019-06-01 stsp goto done;
2966 234035bc 2019-06-01 stsp arg.worktree = worktree;
2967 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
2968 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
2969 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
2970 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
2971 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
2972 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
2973 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
2974 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2975 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2976 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2977 af12c6b9 2019-06-04 stsp err = sync_err;
2979 234035bc 2019-06-01 stsp if (tree1)
2980 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
2981 234035bc 2019-06-01 stsp if (tree2)
2982 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
2983 f69721c3 2019-10-21 stsp free(label_orig);
2984 818c7501 2019-07-11 stsp return err;
2987 818c7501 2019-07-11 stsp const struct got_error *
2988 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
2989 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2990 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2991 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2993 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
2994 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
2995 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
2996 818c7501 2019-07-11 stsp struct check_merge_ok_arg mok_arg;
2998 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3000 818c7501 2019-07-11 stsp return err;
3002 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3004 818c7501 2019-07-11 stsp goto done;
3006 818c7501 2019-07-11 stsp mok_arg.worktree = worktree;
3007 818c7501 2019-07-11 stsp mok_arg.repo = repo;
3008 818c7501 2019-07-11 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3009 818c7501 2019-07-11 stsp &mok_arg);
3011 818c7501 2019-07-11 stsp goto done;
3013 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3014 818c7501 2019-07-11 stsp commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3016 818c7501 2019-07-11 stsp if (fileindex)
3017 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3018 818c7501 2019-07-11 stsp free(fileindex_path);
3019 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3020 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3021 234035bc 2019-06-01 stsp err = unlockerr;
3022 234035bc 2019-06-01 stsp return err;
3025 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3026 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3027 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3028 927df6b7 2019-02-10 stsp const char *status_path;
3029 927df6b7 2019-02-10 stsp size_t status_path_len;
3030 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3031 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3032 f8d1f275 2019-02-04 stsp void *status_arg;
3033 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3034 f8d1f275 2019-02-04 stsp void *cancel_arg;
3035 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3036 6841da00 2019-08-08 stsp struct got_pathlist_head ignores;
3037 f2a9dc41 2019-12-13 tracey int report_unchanged;
3038 3143d852 2020-06-25 stsp int no_ignores;
3041 f8d1f275 2019-02-04 stsp static const struct got_error *
3042 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3043 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3044 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3045 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3047 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3048 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3049 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3050 927df6b7 2019-02-10 stsp struct stat sb;
3051 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3052 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3053 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3055 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3057 98eaaa12 2019-08-03 stsp return err;
3059 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3060 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3061 98eaaa12 2019-08-03 stsp return NULL;
3063 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3064 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3065 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3067 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3068 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3069 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3071 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3072 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3073 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3074 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3075 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3078 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3079 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3082 927df6b7 2019-02-10 stsp static const struct got_error *
3083 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3084 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3086 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3087 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3088 f8d1f275 2019-02-04 stsp char *abspath;
3090 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3091 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3093 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3094 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3095 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3096 927df6b7 2019-02-10 stsp return NULL;
3098 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3099 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3100 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3101 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3103 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3104 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3105 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3108 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3109 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3110 f8d1f275 2019-02-04 stsp free(abspath);
3111 9d31a1d8 2018-03-11 stsp return err;
3114 f8d1f275 2019-02-04 stsp static const struct got_error *
3115 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3117 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3118 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3119 2ec1f75b 2019-03-26 stsp unsigned char status;
3121 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3122 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3124 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3125 927df6b7 2019-02-10 stsp return NULL;
3127 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3128 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3129 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3130 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3132 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3133 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3134 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3138 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3140 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3142 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3143 6841da00 2019-08-08 stsp free((char *)pe->path);
3144 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3148 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3150 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3152 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3153 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3154 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3155 6841da00 2019-08-08 stsp free((char *)pe->path);
3157 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3160 6841da00 2019-08-08 stsp static const struct got_error *
3161 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3163 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3164 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3165 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3166 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3167 6841da00 2019-08-08 stsp size_t linesize = 0;
3168 6841da00 2019-08-08 stsp ssize_t linelen;
3170 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3171 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3172 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3173 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3175 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3176 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3177 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3179 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3180 bd8de430 2019-10-04 stsp if (line[0] == '#')
3183 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3184 bd8de430 2019-10-04 stsp if (line[0] == '!')
3187 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3188 6841da00 2019-08-08 stsp line) == -1) {
3189 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3190 6841da00 2019-08-08 stsp goto done;
3192 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3194 6841da00 2019-08-08 stsp goto done;
3196 6841da00 2019-08-08 stsp if (ferror(f)) {
3197 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3198 6841da00 2019-08-08 stsp goto done;
3201 6841da00 2019-08-08 stsp dirpath = strdup(path);
3202 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3203 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3204 6841da00 2019-08-08 stsp goto done;
3206 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3208 6841da00 2019-08-08 stsp free(line);
3209 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3210 6841da00 2019-08-08 stsp free(dirpath);
3211 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3213 6841da00 2019-08-08 stsp return err;
3217 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3219 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3221 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3222 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3223 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3224 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3226 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3227 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3229 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3231 bd8de430 2019-10-04 stsp pattern += 3;
3233 bd8de430 2019-10-04 stsp while (*p) {
3234 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3235 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3236 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3237 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3239 bd8de430 2019-10-04 stsp while (*p == '/')
3249 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3250 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3251 6841da00 2019-08-08 stsp * ignores backwards.
3253 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3254 6841da00 2019-08-08 stsp while (pe) {
3255 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3256 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3257 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3258 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3259 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3260 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3261 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3262 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3263 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3268 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3274 6841da00 2019-08-08 stsp static const struct got_error *
3275 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3276 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3278 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3279 6841da00 2019-08-08 stsp char *ignorespath;
3280 886cec17 2019-12-15 stsp int fd = -1;
3281 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3283 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3284 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3285 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3287 886cec17 2019-12-15 stsp if (dirfd != -1) {
3288 886cec17 2019-12-15 stsp fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3289 886cec17 2019-12-15 stsp if (fd == -1) {
3290 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3291 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3292 886cec17 2019-12-15 stsp ignorespath);
3294 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3295 886cec17 2019-12-15 stsp if (ignoresfile == NULL)
3296 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3297 886cec17 2019-12-15 stsp ignorespath);
3300 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3304 886cec17 2019-12-15 stsp ignoresfile = fopen(ignorespath, "r");
3305 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3306 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3307 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3308 886cec17 2019-12-15 stsp ignorespath);
3310 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3313 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3314 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3315 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3316 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3317 6841da00 2019-08-08 stsp free(ignorespath);
3318 6841da00 2019-08-08 stsp return err;
3321 f8d1f275 2019-02-04 stsp static const struct got_error *
3322 7f91a133 2019-12-13 stsp status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3324 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3325 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3326 f8d1f275 2019-02-04 stsp char *path = NULL;
3328 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3329 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3331 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3332 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3333 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3335 f8d1f275 2019-02-04 stsp path = de->d_name;
3338 3143d852 2020-06-25 stsp if (de->d_type != DT_DIR &&
3339 3143d852 2020-06-25 stsp got_path_is_child(path, a->status_path, a->status_path_len)
3340 6841da00 2019-08-08 stsp && !match_ignores(&a->ignores, path))
3341 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3342 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3343 f8d1f275 2019-02-04 stsp if (parent_path[0])
3344 f8d1f275 2019-02-04 stsp free(path);
3345 b72f483a 2019-02-05 stsp return err;
3348 347d1d3e 2019-07-12 stsp static const struct got_error *
3349 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3351 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3352 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3354 3143d852 2020-06-25 stsp if (a->no_ignores)
3355 3143d852 2020-06-25 stsp return NULL;
3357 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path,
3358 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3360 3143d852 2020-06-25 stsp return err;
3362 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path, path,
3363 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3365 3143d852 2020-06-25 stsp return err;
3368 3143d852 2020-06-25 stsp static const struct got_error *
3369 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3370 abb4604f 2019-07-27 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3371 f2a9dc41 2019-12-13 tracey void *status_arg, struct got_repository *repo, int report_unchanged)
3373 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3374 abb4604f 2019-07-27 stsp struct stat sb;
3376 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3378 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3379 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3381 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3382 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3383 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3384 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3385 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3386 abb4604f 2019-07-27 stsp return NULL;
3389 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3390 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3391 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3393 abb4604f 2019-07-27 stsp return NULL;
3396 3143d852 2020-06-25 stsp static const struct got_error *
3397 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3398 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3400 3143d852 2020-06-25 stsp const struct got_error *err;
3401 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3403 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3404 3143d852 2020-06-25 stsp ".cvsignore");
3406 3143d852 2020-06-25 stsp return err;
3408 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3409 3143d852 2020-06-25 stsp ".gitignore");
3411 3143d852 2020-06-25 stsp return err;
3413 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3414 3143d852 2020-06-25 stsp if (err) {
3415 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3416 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3417 3143d852 2020-06-25 stsp return err;
3419 3143d852 2020-06-25 stsp for (;;) {
3420 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3421 3143d852 2020-06-25 stsp ".cvsignore");
3424 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3425 3143d852 2020-06-25 stsp ".gitignore");
3428 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3429 3143d852 2020-06-25 stsp if (err) {
3430 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3431 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3434 b737c85a 2020-06-26 stsp free(parent_path);
3435 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3436 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3439 b737c85a 2020-06-26 stsp free(parent_path);
3440 b737c85a 2020-06-26 stsp free(next_parent_path);
3441 3143d852 2020-06-25 stsp return err;
3444 abb4604f 2019-07-27 stsp static const struct got_error *
3445 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3446 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3447 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3448 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3449 f2a9dc41 2019-12-13 tracey int report_unchanged)
3451 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3452 6fc93f37 2019-12-13 stsp int fd = -1;
3453 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3454 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3455 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3457 3143d852 2020-06-25 stsp TAILQ_INIT(&arg.ignores);
3459 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3460 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3461 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3463 6fc93f37 2019-12-13 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3464 6fc93f37 2019-12-13 stsp if (fd == -1) {
3465 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3466 00bb5ea0 2020-07-23 stsp errno != ELOOP)
3467 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3469 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3470 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3471 f2a9dc41 2019-12-13 tracey report_unchanged);
3473 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3474 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3475 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3476 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3477 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3478 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3479 abb4604f 2019-07-27 stsp arg.status_path = path;
3480 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3481 abb4604f 2019-07-27 stsp arg.repo = repo;
3482 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3483 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3484 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3485 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3486 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3487 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3488 022fae89 2019-12-06 tracey if (!no_ignores) {
3489 3143d852 2020-06-25 stsp err = add_ignores_from_parent_paths(&arg.ignores,
3490 3143d852 2020-06-25 stsp worktree->root_path, path);
3492 3143d852 2020-06-25 stsp goto done;
3494 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3495 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3498 3143d852 2020-06-25 stsp free_ignores(&arg.ignores);
3499 6fc93f37 2019-12-13 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
3500 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3501 927df6b7 2019-02-10 stsp free(ondisk_path);
3502 347d1d3e 2019-07-12 stsp return err;
3505 347d1d3e 2019-07-12 stsp const struct got_error *
3506 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3507 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3508 72ea6654 2019-07-27 stsp got_worktree_status_cb status_cb, void *status_arg,
3509 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3511 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3512 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3513 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3514 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3516 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3518 347d1d3e 2019-07-12 stsp return err;
3520 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3521 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3522 f2a9dc41 2019-12-13 tracey status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3526 f8d1f275 2019-02-04 stsp free(fileindex_path);
3527 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3528 f8d1f275 2019-02-04 stsp return err;
3531 6c7ab921 2019-03-18 stsp const struct got_error *
3532 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3533 6c7ab921 2019-03-18 stsp const char *arg)
3535 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3536 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3537 6c7ab921 2019-03-18 stsp size_t len;
3538 00bb5ea0 2020-07-23 stsp struct stat sb;
3540 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3542 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3543 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3544 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3546 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3547 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3548 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3549 00bb5ea0 2020-07-23 stsp goto done;
3552 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3554 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3555 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3556 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3557 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3559 00bb5ea0 2020-07-23 stsp char *abspath = NULL;
3560 00bb5ea0 2020-07-23 stsp char canonpath[PATH_MAX];
3561 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3562 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3563 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3564 00bb5ea0 2020-07-23 stsp goto done;
3568 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3569 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3571 d0710d08 2019-07-22 stsp goto done;
3572 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3573 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3574 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3575 00bb5ea0 2020-07-23 stsp goto done;
3578 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3579 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3580 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3581 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3582 00bb5ea0 2020-07-23 stsp goto done;
3584 00bb5ea0 2020-07-23 stsp if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3585 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3586 00bb5ea0 2020-07-23 stsp goto done;
3591 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3592 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3593 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3594 6c7ab921 2019-03-18 stsp goto done;
3597 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3598 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3599 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3601 f6d88e1a 2019-05-29 stsp goto done;
3603 f6d88e1a 2019-05-29 stsp path = strdup("");
3604 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3605 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3606 f6d88e1a 2019-05-29 stsp goto done;
3610 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3611 6c7ab921 2019-03-18 stsp len = strlen(path);
3612 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3613 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3617 6c7ab921 2019-03-18 stsp free(resolved);
3618 d0710d08 2019-07-22 stsp free(cwd);
3619 6c7ab921 2019-03-18 stsp if (err == NULL)
3620 6c7ab921 2019-03-18 stsp *wt_path = path;
3622 6c7ab921 2019-03-18 stsp free(path);
3623 d00136be 2019-03-26 stsp return err;
3626 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3627 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3628 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3629 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3630 4e68cba3 2019-11-23 stsp void *progress_arg;
3631 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3634 4e68cba3 2019-11-23 stsp static const struct got_error *
3635 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3636 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3637 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3638 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3640 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3641 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3642 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3643 6d022e97 2019-08-04 stsp struct stat sb;
3644 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3646 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3647 dbb83fbd 2019-12-12 stsp relpath) == -1)
3648 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3650 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3652 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3653 12463d8b 2019-12-13 stsp de_name, a->repo);
3655 dbb83fbd 2019-12-12 stsp goto done;
3656 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3657 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3658 dbb83fbd 2019-12-12 stsp goto done;
3659 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3661 dbb83fbd 2019-12-12 stsp goto done;
3664 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3665 dbb83fbd 2019-12-12 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3666 dbb83fbd 2019-12-12 stsp goto done;
3669 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3671 4e68cba3 2019-11-23 stsp goto done;
3672 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3673 3969253a 2020-03-07 stsp if (err) {
3674 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3675 3969253a 2020-03-07 stsp goto done;
3677 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3678 07f5b47a 2019-06-02 stsp if (err) {
3679 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
3680 4e68cba3 2019-11-23 stsp goto done;
3683 dbb83fbd 2019-12-12 stsp free(ondisk_path);
3685 dbb83fbd 2019-12-12 stsp return err;
3686 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
3687 dbb83fbd 2019-12-12 stsp return NULL;
3688 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3691 d00136be 2019-03-26 stsp const struct got_error *
3692 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
3693 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
3694 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3695 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
3697 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3698 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
3699 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3700 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
3701 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
3703 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3705 d00136be 2019-03-26 stsp return err;
3707 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3709 d00136be 2019-03-26 stsp goto done;
3711 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
3712 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
3713 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
3714 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
3715 4e68cba3 2019-11-23 stsp saa.repo = repo;
3717 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3718 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3719 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3723 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3724 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3725 af12c6b9 2019-06-04 stsp err = sync_err;
3727 fb399478 2019-07-12 stsp free(fileindex_path);
3728 d00136be 2019-03-26 stsp if (fileindex)
3729 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
3730 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3731 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
3732 d00136be 2019-03-26 stsp err = unlockerr;
3733 6c7ab921 2019-03-18 stsp return err;
3736 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
3737 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
3738 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
3739 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
3740 f2a9dc41 2019-12-13 tracey void *progress_arg;
3741 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
3742 f2a9dc41 2019-12-13 tracey int delete_local_mods;
3743 70e3e7f5 2019-12-13 tracey int keep_on_disk;
3746 f2a9dc41 2019-12-13 tracey static const struct got_error *
3747 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
3748 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
3749 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3750 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
3752 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
3753 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
3754 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
3755 17ed4618 2019-06-02 stsp struct stat sb;
3756 15341bfd 2020-03-05 tracey char *ondisk_path, *parent = NULL;
3758 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3759 17ed4618 2019-06-02 stsp if (ie == NULL)
3760 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
3762 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
3763 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
3764 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3765 9acbc4fa 2019-08-03 stsp return NULL;
3766 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3769 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3770 f2a9dc41 2019-12-13 tracey relpath) == -1)
3771 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
3773 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3776 f2a9dc41 2019-12-13 tracey goto done;
3778 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
3779 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
3780 f2a9dc41 2019-12-13 tracey goto done;
3781 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3782 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3783 f2a9dc41 2019-12-13 tracey goto done;
3785 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
3786 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
3787 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3788 f2a9dc41 2019-12-13 tracey goto done;
3792 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3793 12463d8b 2019-12-13 stsp if (dirfd != -1) {
3794 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
3795 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
3796 12463d8b 2019-12-13 stsp ondisk_path);
3797 12463d8b 2019-12-13 stsp goto done;
3799 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
3800 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
3801 12463d8b 2019-12-13 stsp goto done;
3804 15341bfd 2020-03-05 tracey parent = dirname(ondisk_path);
3806 15341bfd 2020-03-05 tracey if (parent == NULL) {
3807 15341bfd 2020-03-05 tracey err = got_error_from_errno2("dirname", ondisk_path);
3808 15341bfd 2020-03-05 tracey goto done;
3810 15341bfd 2020-03-05 tracey while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3811 15341bfd 2020-03-05 tracey if (rmdir(parent) == -1) {
3812 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
3813 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
3814 15341bfd 2020-03-05 tracey parent);
3817 15341bfd 2020-03-05 tracey parent = dirname(parent);
3818 15341bfd 2020-03-05 tracey if (parent == NULL) {
3819 15341bfd 2020-03-05 tracey err = got_error_from_errno2("dirname", parent);
3820 15341bfd 2020-03-05 tracey goto done;
3825 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3827 f2a9dc41 2019-12-13 tracey free(ondisk_path);
3828 f2a9dc41 2019-12-13 tracey if (err)
3829 f2a9dc41 2019-12-13 tracey return err;
3830 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
3831 f2a9dc41 2019-12-13 tracey return NULL;
3832 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3833 f2a9dc41 2019-12-13 tracey staged_status, relpath);
3836 2ec1f75b 2019-03-26 stsp const struct got_error *
3837 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
3838 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
3839 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
3840 70e3e7f5 2019-12-13 tracey struct got_repository *repo, int keep_on_disk)
3842 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3843 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
3844 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3845 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
3846 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
3848 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3850 2ec1f75b 2019-03-26 stsp return err;
3852 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3854 2ec1f75b 2019-03-26 stsp goto done;
3856 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
3857 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
3858 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
3859 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
3860 f2a9dc41 2019-12-13 tracey sda.repo = repo;
3861 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
3862 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
3864 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3865 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
3866 f2a9dc41 2019-12-13 tracey schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3870 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3871 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3872 af12c6b9 2019-06-04 stsp err = sync_err;
3874 fb399478 2019-07-12 stsp free(fileindex_path);
3875 2ec1f75b 2019-03-26 stsp if (fileindex)
3876 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
3877 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3878 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
3879 2ec1f75b 2019-03-26 stsp err = unlockerr;
3880 33aa809d 2019-08-08 stsp return err;
3883 33aa809d 2019-08-08 stsp static const struct got_error *
3884 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3886 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
3887 33aa809d 2019-08-08 stsp char *line = NULL;
3888 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
3889 33aa809d 2019-08-08 stsp ssize_t linelen;
3891 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
3892 33aa809d 2019-08-08 stsp if (linelen == -1) {
3893 33aa809d 2019-08-08 stsp if (ferror(infile)) {
3894 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
3895 33aa809d 2019-08-08 stsp goto done;
3897 33aa809d 2019-08-08 stsp return NULL;
3899 33aa809d 2019-08-08 stsp if (outfile) {
3900 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
3901 33aa809d 2019-08-08 stsp if (n != linelen) {
3902 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
3903 33aa809d 2019-08-08 stsp goto done;
3906 33aa809d 2019-08-08 stsp if (rejectfile) {
3907 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
3908 33aa809d 2019-08-08 stsp if (n != linelen)
3909 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
3912 33aa809d 2019-08-08 stsp free(line);
3913 2ec1f75b 2019-03-26 stsp return err;
3916 33aa809d 2019-08-08 stsp static const struct got_error *
3917 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
3919 33aa809d 2019-08-08 stsp char *line = NULL;
3920 33aa809d 2019-08-08 stsp size_t linesize = 0;
3921 33aa809d 2019-08-08 stsp ssize_t linelen;
3923 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
3924 500467ff 2019-09-25 hiltjo if (linelen == -1) {
3925 500467ff 2019-09-25 hiltjo if (ferror(f))
3926 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
3927 500467ff 2019-09-25 hiltjo return NULL;
3929 33aa809d 2019-08-08 stsp free(line);
3930 33aa809d 2019-08-08 stsp return NULL;
3933 33aa809d 2019-08-08 stsp static const struct got_error *
3934 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3935 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
3936 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
3938 33aa809d 2019-08-08 stsp const struct got_error *err;
3940 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
3941 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
3942 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
3944 33aa809d 2019-08-08 stsp return err;
3945 33aa809d 2019-08-08 stsp (*line_cur1)++;
3947 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
3948 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
3949 33aa809d 2019-08-08 stsp if (rejectfile)
3950 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
3952 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
3954 33aa809d 2019-08-08 stsp return err;
3955 33aa809d 2019-08-08 stsp (*line_cur2)++;
3957 33aa809d 2019-08-08 stsp /* Copy patched lines. */
3958 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
3959 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
3961 33aa809d 2019-08-08 stsp return err;
3962 33aa809d 2019-08-08 stsp (*line_cur2)++;
3964 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
3965 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
3966 33aa809d 2019-08-08 stsp if (rejectfile)
3967 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
3969 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
3971 33aa809d 2019-08-08 stsp return err;
3972 33aa809d 2019-08-08 stsp (*line_cur1)++;
3975 f1e81a05 2019-08-10 stsp return NULL;
3978 f1e81a05 2019-08-10 stsp static const struct got_error *
3979 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3980 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
3982 f1e81a05 2019-08-10 stsp const struct got_error *err;
3984 f1e81a05 2019-08-10 stsp if (outfile) {
3985 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
3986 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
3987 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
3989 f1e81a05 2019-08-10 stsp return err;
3990 f1e81a05 2019-08-10 stsp (*line_cur1)++;
3993 f1e81a05 2019-08-10 stsp if (rejectfile) {
3994 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
3995 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
3996 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
3998 f1e81a05 2019-08-10 stsp return err;
3999 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4003 33aa809d 2019-08-08 stsp return NULL;
4006 33aa809d 2019-08-08 stsp static const struct got_error *
4007 33aa809d 2019-08-08 stsp apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
4008 33aa809d 2019-08-08 stsp int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
4009 33aa809d 2019-08-08 stsp int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
4010 33aa809d 2019-08-08 stsp int *line_cur2, FILE *outfile, FILE *rejectfile,
4011 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4013 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4014 33aa809d 2019-08-08 stsp int start_old = change->cv.a;
4015 33aa809d 2019-08-08 stsp int end_old = change->cv.b;
4016 33aa809d 2019-08-08 stsp int start_new = change->cv.c;
4017 33aa809d 2019-08-08 stsp int end_new = change->cv.d;
4018 33aa809d 2019-08-08 stsp long pos1, pos2;
4019 33aa809d 2019-08-08 stsp FILE *hunkfile;
4021 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4023 33aa809d 2019-08-08 stsp hunkfile = got_opentemp();
4024 33aa809d 2019-08-08 stsp if (hunkfile == NULL)
4025 33aa809d 2019-08-08 stsp return got_error_from_errno("got_opentemp");
4027 33aa809d 2019-08-08 stsp pos1 = ftell(f1);
4028 33aa809d 2019-08-08 stsp pos2 = ftell(f2);
4030 33aa809d 2019-08-08 stsp /* XXX TODO needs error checking */
4031 33aa809d 2019-08-08 stsp got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4033 33aa809d 2019-08-08 stsp if (fseek(f1, pos1, SEEK_SET) == -1) {
4034 33aa809d 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
4035 33aa809d 2019-08-08 stsp goto done;
4037 33aa809d 2019-08-08 stsp if (fseek(f2, pos2, SEEK_SET) == -1) {
4038 33aa809d 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
4039 33aa809d 2019-08-08 stsp goto done;
4041 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4042 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4043 33aa809d 2019-08-08 stsp goto done;
4046 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4047 33aa809d 2019-08-08 stsp hunkfile, n, nchanges);
4049 33aa809d 2019-08-08 stsp goto done;
4051 33aa809d 2019-08-08 stsp switch (*choice) {
4052 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4053 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4054 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4056 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4057 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4058 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4060 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4063 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4067 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4068 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4069 33aa809d 2019-08-08 stsp return err;
4072 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4073 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4074 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4075 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4076 1f1abb7e 2019-08-08 stsp void *progress_arg;
4077 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4078 33aa809d 2019-08-08 stsp void *patch_arg;
4079 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4082 e20a8b6f 2019-06-04 stsp static const struct got_error *
4083 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4084 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4085 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4086 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4087 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4089 33aa809d 2019-08-08 stsp const struct got_error *err;
4090 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4091 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4092 1ebedb77 2019-10-19 stsp int fd2 = -1;
4093 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4094 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4095 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4096 33aa809d 2019-08-08 stsp struct stat sb1, sb2;
4097 33aa809d 2019-08-08 stsp struct got_diff_changes *changes = NULL;
4098 33aa809d 2019-08-08 stsp struct got_diff_state *ds = NULL;
4099 33aa809d 2019-08-08 stsp struct got_diff_args *args = NULL;
4100 33aa809d 2019-08-08 stsp struct got_diff_change *change;
4101 33aa809d 2019-08-08 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4102 33aa809d 2019-08-08 stsp int n = 0;
4104 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4106 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4108 33aa809d 2019-08-08 stsp return err;
4110 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4111 12463d8b 2019-12-13 stsp fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4112 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4113 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4114 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4115 fa3cef63 2020-07-23 stsp goto done;
4117 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4118 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4119 fa3cef63 2020-07-23 stsp if (link_len == -1)
4120 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlinkat", path2);
4121 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4122 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4125 12463d8b 2019-12-13 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4126 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4127 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4128 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4129 fa3cef63 2020-07-23 stsp goto done;
4131 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4132 fa3cef63 2020-07-23 stsp sizeof(link_target));
4133 fa3cef63 2020-07-23 stsp if (link_len == -1)
4134 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4135 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4136 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4139 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4140 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4141 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4142 fa3cef63 2020-07-23 stsp goto done;
4145 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4146 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4147 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4148 fa3cef63 2020-07-23 stsp goto done;
4153 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4154 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4155 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4156 fa3cef63 2020-07-23 stsp goto done;
4158 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4159 fa3cef63 2020-07-23 stsp if (n != link_len) {
4160 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4161 fa3cef63 2020-07-23 stsp goto done;
4163 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4164 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4165 fa3cef63 2020-07-23 stsp goto done;
4167 fa3cef63 2020-07-23 stsp rewind(f2);
4170 33aa809d 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4172 33aa809d 2019-08-08 stsp goto done;
4174 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4176 33aa809d 2019-08-08 stsp goto done;
4178 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4180 33aa809d 2019-08-08 stsp goto done;
4182 33aa809d 2019-08-08 stsp if (stat(path1, &sb1) == -1) {
4183 33aa809d 2019-08-08 stsp err = got_error_from_errno2("stat", path1);
4184 33aa809d 2019-08-08 stsp goto done;
4187 33aa809d 2019-08-08 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
4188 33aa809d 2019-08-08 stsp f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4190 33aa809d 2019-08-08 stsp goto done;
4192 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4194 33aa809d 2019-08-08 stsp goto done;
4196 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4197 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4198 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4199 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4200 33aa809d 2019-08-08 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4201 33aa809d 2019-08-08 stsp int choice;
4202 33aa809d 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
4203 33aa809d 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
4204 33aa809d 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
4205 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4206 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4207 e635744c 2019-08-08 stsp patch_cb, patch_arg);
4209 33aa809d 2019-08-08 stsp goto done;
4210 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4211 33aa809d 2019-08-08 stsp have_content = 1;
4212 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4215 1ebedb77 2019-10-19 stsp if (have_content) {
4216 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4217 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4218 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4220 1ebedb77 2019-10-19 stsp goto done;
4222 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4223 fa3cef63 2020-07-23 stsp if (chmod(*path_outfile, sb2.st_mode) == -1) {
4224 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("chmod", path2);
4225 fa3cef63 2020-07-23 stsp goto done;
4230 33aa809d 2019-08-08 stsp free(id_str);
4232 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4233 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4234 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4235 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4236 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4237 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4238 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4239 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4240 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4241 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4242 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4243 33aa809d 2019-08-08 stsp if (err || !have_content) {
4244 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4245 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4246 33aa809d 2019-08-08 stsp free(*path_outfile);
4247 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4249 33aa809d 2019-08-08 stsp free(args);
4251 33aa809d 2019-08-08 stsp got_diff_state_free(ds);
4254 33aa809d 2019-08-08 stsp if (changes)
4255 33aa809d 2019-08-08 stsp got_diff_free_changes(changes);
4256 33aa809d 2019-08-08 stsp free(path1);
4257 33aa809d 2019-08-08 stsp return err;
4260 33aa809d 2019-08-08 stsp static const struct got_error *
4261 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4262 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4263 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4264 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4266 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4267 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4268 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4269 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4270 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4271 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4272 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4273 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4274 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4275 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4277 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4278 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4279 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4280 d3bcc3d1 2019-08-08 stsp return NULL;
4282 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4283 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4284 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4286 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4287 65084dad 2019-08-08 stsp if (ie == NULL)
4288 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4290 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4291 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4292 a129376b 2019-03-28 stsp if (err) {
4293 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4294 a129376b 2019-03-28 stsp goto done;
4295 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4296 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4297 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4298 e20a8b6f 2019-06-04 stsp goto done;
4301 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4302 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4303 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4304 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4305 a129376b 2019-03-28 stsp goto done;
4308 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4309 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4310 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4311 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4312 a129376b 2019-03-28 stsp goto done;
4315 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4316 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4317 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4318 a129376b 2019-03-28 stsp goto done;
4323 1f1abb7e 2019-08-08 stsp err = got_object_id_by_path(&tree_id, a->repo,
4324 1f1abb7e 2019-08-08 stsp a->worktree->base_commit_id, tree_path);
4325 a9fa2909 2019-07-27 stsp if (err) {
4326 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4327 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4328 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4329 a9fa2909 2019-07-27 stsp goto done;
4331 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4333 a9fa2909 2019-07-27 stsp goto done;
4335 a9fa2909 2019-07-27 stsp te_name = basename(ie->path);
4336 a9fa2909 2019-07-27 stsp if (te_name == NULL) {
4337 a9fa2909 2019-07-27 stsp err = got_error_from_errno2("basename", ie->path);
4338 a9fa2909 2019-07-27 stsp goto done;
4341 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4342 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4343 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4344 a9fa2909 2019-07-27 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
4345 a9fa2909 2019-07-27 stsp goto done;
4349 a129376b 2019-03-28 stsp switch (status) {
4350 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4351 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4352 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4353 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4354 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4356 33aa809d 2019-08-08 stsp goto done;
4357 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4360 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4361 1f1abb7e 2019-08-08 stsp ie->path);
4363 1ee397ad 2019-07-12 stsp goto done;
4364 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4366 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4367 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4368 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4369 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4370 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4372 33aa809d 2019-08-08 stsp goto done;
4373 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4376 33aa809d 2019-08-08 stsp /* fall through */
4377 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4378 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4379 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4380 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4381 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4382 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4383 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4384 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4385 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4387 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4388 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4389 1f1abb7e 2019-08-08 stsp err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4391 65084dad 2019-08-08 stsp goto done;
4393 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4394 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4395 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4396 a129376b 2019-03-28 stsp goto done;
4399 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4400 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4401 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4402 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4403 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4404 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4405 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4407 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4408 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4409 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4410 369fd7e5 2020-07-23 stsp path_content);
4413 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4414 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4415 369fd7e5 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4416 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4418 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4419 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4420 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4421 369fd7e5 2020-07-23 stsp goto done;
4425 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4426 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4427 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4428 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4429 c90c8ce3 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4430 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4432 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4434 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4435 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4436 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4437 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4440 a129376b 2019-03-28 stsp goto done;
4441 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4442 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4443 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4444 054041d0 2020-06-24 stsp ondisk_path, blob->id.sha1,
4445 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4447 2e1fa222 2020-07-23 stsp goto done;
4449 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4450 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4451 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4460 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4461 33aa809d 2019-08-08 stsp free(path_content);
4462 e20a8b6f 2019-06-04 stsp free(parent_path);
4463 a129376b 2019-03-28 stsp free(tree_path);
4465 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4467 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4468 a129376b 2019-03-28 stsp free(tree_id);
4469 e20a8b6f 2019-06-04 stsp return err;
4472 e20a8b6f 2019-06-04 stsp const struct got_error *
4473 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4474 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4475 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4476 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4477 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4479 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4480 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4481 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4482 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4483 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4484 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4486 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4488 e20a8b6f 2019-06-04 stsp return err;
4490 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4492 e20a8b6f 2019-06-04 stsp goto done;
4494 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4495 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4496 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4497 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4498 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4499 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4500 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4501 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4502 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4503 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
4507 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4508 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4509 e20a8b6f 2019-06-04 stsp err = sync_err;
4511 fb399478 2019-07-12 stsp free(fileindex_path);
4512 a129376b 2019-03-28 stsp if (fileindex)
4513 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4514 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4515 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4516 a129376b 2019-03-28 stsp err = unlockerr;
4517 c4296144 2019-05-09 stsp return err;
4520 cf066bf8 2019-05-09 stsp static void
4521 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4523 24519714 2019-05-09 stsp free(ct->path);
4524 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4525 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4526 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4527 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4528 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4529 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4533 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4534 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4535 24519714 2019-05-09 stsp struct got_repository *repo;
4536 24519714 2019-05-09 stsp struct got_worktree *worktree;
4537 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4538 5f8a88c6 2019-08-03 stsp int have_staged_files;
4539 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4542 c4296144 2019-05-09 stsp static const struct got_error *
4543 88d0e355 2019-08-03 stsp collect_commitables(void *arg, unsigned char status,
4544 88d0e355 2019-08-03 stsp unsigned char staged_status, const char *relpath,
4545 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4546 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4548 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
4549 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4550 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4551 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4552 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4553 768aea60 2019-05-09 stsp struct stat sb;
4555 5f8a88c6 2019-08-03 stsp if (a->have_staged_files) {
4556 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4557 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4558 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4559 5f8a88c6 2019-08-03 stsp return NULL;
4561 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4562 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4564 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4565 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4566 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4567 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4568 5f8a88c6 2019-08-03 stsp return NULL;
4571 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4572 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4573 036813ee 2019-05-09 stsp goto done;
4575 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4576 036813ee 2019-05-09 stsp parent_path = strdup("");
4577 69960a46 2019-05-09 stsp if (parent_path == NULL)
4578 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4580 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4582 69960a46 2019-05-09 stsp return err;
4585 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4586 cf066bf8 2019-05-09 stsp if (ct == NULL) {
4587 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4588 c4296144 2019-05-09 stsp goto done;
4591 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4592 768aea60 2019-05-09 stsp relpath) == -1) {
4593 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4594 768aea60 2019-05-09 stsp goto done;
4597 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
4598 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
4599 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
4600 0aeb8099 2020-07-23 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4601 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
4602 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
4603 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
4604 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
4606 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
4607 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
4610 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4611 0aeb8099 2020-07-23 stsp goto done;
4613 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
4614 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
4615 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
4616 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4617 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
4618 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
4619 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
4620 12463d8b 2019-12-13 stsp ct->ondisk_path);
4621 12463d8b 2019-12-13 stsp goto done;
4623 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
4624 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
4625 768aea60 2019-05-09 stsp goto done;
4627 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
4630 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4631 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4632 44d03001 2019-05-09 stsp relpath) == -1) {
4633 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4634 44d03001 2019-05-09 stsp goto done;
4637 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4638 35213c7c 2020-07-23 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4639 35213c7c 2020-07-23 stsp int is_bad_symlink;
4640 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
4641 35213c7c 2020-07-23 stsp ssize_t target_len;
4642 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
4643 35213c7c 2020-07-23 stsp sizeof(target_path));
4644 35213c7c 2020-07-23 stsp if (target_len == -1) {
4645 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
4646 35213c7c 2020-07-23 stsp ct->ondisk_path);
4647 35213c7c 2020-07-23 stsp goto done;
4649 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
4650 35213c7c 2020-07-23 stsp target_len, ct->ondisk_path, a->worktree->root_path);
4652 35213c7c 2020-07-23 stsp goto done;
4653 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
4654 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
4655 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
4656 35213c7c 2020-07-23 stsp goto done;
4661 cf066bf8 2019-05-09 stsp ct->status = status;
4662 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
4663 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
4664 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
4665 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
4666 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
4667 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
4668 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4669 b416585c 2019-05-13 stsp goto done;
4671 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
4672 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
4673 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
4674 f0b75401 2019-08-03 stsp goto done;
4677 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
4678 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
4679 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4680 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
4681 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4682 036813ee 2019-05-09 stsp goto done;
4685 24519714 2019-05-09 stsp ct->path = strdup(path);
4686 24519714 2019-05-09 stsp if (ct->path == NULL) {
4687 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4688 24519714 2019-05-09 stsp goto done;
4690 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4692 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
4693 ed175427 2019-05-09 stsp free_commitable(ct);
4694 24519714 2019-05-09 stsp free(parent_path);
4695 036813ee 2019-05-09 stsp free(path);
4696 a129376b 2019-03-28 stsp return err;
4699 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
4700 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
4701 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4702 036813ee 2019-05-09 stsp struct got_repository *);
4704 ed175427 2019-05-09 stsp static const struct got_error *
4705 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4706 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
4707 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
4708 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4709 afa376bf 2019-05-09 stsp struct got_repository *repo)
4711 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
4712 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
4713 036813ee 2019-05-09 stsp char *subpath;
4715 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
4716 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4717 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4719 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
4721 ed175427 2019-05-09 stsp return err;
4723 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
4724 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
4725 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
4726 036813ee 2019-05-09 stsp free(subpath);
4727 036813ee 2019-05-09 stsp return err;
4730 036813ee 2019-05-09 stsp static const struct got_error *
4731 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4733 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4734 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
4736 036813ee 2019-05-09 stsp *match = 0;
4738 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
4739 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
4740 0f63689d 2019-05-10 stsp return NULL;
4743 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4745 0f63689d 2019-05-10 stsp return err;
4746 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
4747 036813ee 2019-05-09 stsp free(ct_parent_path);
4748 036813ee 2019-05-09 stsp return err;
4751 768aea60 2019-05-09 stsp static mode_t
4752 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
4754 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
4755 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
4757 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4760 036813ee 2019-05-09 stsp static const struct got_error *
4761 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4762 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
4764 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4766 036813ee 2019-05-09 stsp *new_te = NULL;
4768 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
4770 036813ee 2019-05-09 stsp goto done;
4772 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4774 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
4775 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4776 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4778 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4780 036813ee 2019-05-09 stsp if (err && *new_te) {
4781 56e0773d 2019-11-28 stsp free(*new_te);
4782 036813ee 2019-05-09 stsp *new_te = NULL;
4784 036813ee 2019-05-09 stsp return err;
4787 036813ee 2019-05-09 stsp static const struct got_error *
4788 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4789 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
4791 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4792 036813ee 2019-05-09 stsp char *ct_name;
4794 036813ee 2019-05-09 stsp *new_te = NULL;
4796 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
4797 036813ee 2019-05-09 stsp if (*new_te == NULL)
4798 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
4800 036813ee 2019-05-09 stsp ct_name = basename(ct->path);
4801 036813ee 2019-05-09 stsp if (ct_name == NULL) {
4802 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", ct->path);
4803 036813ee 2019-05-09 stsp goto done;
4805 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4806 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
4807 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
4808 036813ee 2019-05-09 stsp goto done;
4811 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4813 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
4814 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4815 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4817 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4819 036813ee 2019-05-09 stsp if (err && *new_te) {
4820 56e0773d 2019-11-28 stsp free(*new_te);
4821 036813ee 2019-05-09 stsp *new_te = NULL;
4823 036813ee 2019-05-09 stsp return err;
4826 036813ee 2019-05-09 stsp static const struct got_error *
4827 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
4828 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
4830 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4831 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
4833 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4835 036813ee 2019-05-09 stsp return err;
4836 036813ee 2019-05-09 stsp if (new_pe == NULL)
4837 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
4838 036813ee 2019-05-09 stsp return NULL;
4841 afa376bf 2019-05-09 stsp static const struct got_error *
4842 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
4843 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
4845 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
4846 5f8a88c6 2019-08-03 stsp unsigned char status;
4848 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
4849 afa376bf 2019-05-09 stsp ct_path++;
4851 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4852 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
4854 5f8a88c6 2019-08-03 stsp status = ct->status;
4856 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4857 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4860 036813ee 2019-05-09 stsp static const struct got_error *
4861 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
4862 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4864 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
4865 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
4866 44d03001 2019-05-09 stsp char *te_path;
4868 44d03001 2019-05-09 stsp *modified = 0;
4870 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
4871 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
4872 44d03001 2019-05-09 stsp te->name) == -1)
4873 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4875 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4876 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4877 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
4878 44d03001 2019-05-09 stsp strlen(te_path));
4879 44d03001 2019-05-09 stsp if (*modified)
4883 44d03001 2019-05-09 stsp free(te_path);
4884 44d03001 2019-05-09 stsp return err;
4887 44d03001 2019-05-09 stsp static const struct got_error *
4888 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
4889 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
4890 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
4892 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4893 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
4895 036813ee 2019-05-09 stsp *ctp = NULL;
4897 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4898 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4899 036813ee 2019-05-09 stsp char *ct_name = NULL;
4900 036813ee 2019-05-09 stsp int path_matches;
4902 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4903 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
4904 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
4905 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
4908 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
4909 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
4913 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4916 036813ee 2019-05-09 stsp err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4918 036813ee 2019-05-09 stsp return err;
4919 036813ee 2019-05-09 stsp if (!path_matches)
4922 036813ee 2019-05-09 stsp ct_name = basename(pe->path);
4923 036813ee 2019-05-09 stsp if (ct_name == NULL)
4924 638f9024 2019-05-13 stsp return got_error_from_errno2("basename", pe->path);
4926 036813ee 2019-05-09 stsp if (strcmp(te->name, ct_name) != 0)
4929 036813ee 2019-05-09 stsp *ctp = ct;
4933 2b496619 2019-07-10 stsp return err;
4936 2b496619 2019-07-10 stsp static const struct got_error *
4937 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4938 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
4939 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
4940 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
4941 2b496619 2019-07-10 stsp struct got_repository *repo)
4943 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
4944 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
4945 2b496619 2019-07-10 stsp char *subtree_path;
4946 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
4947 ba580f68 2020-03-22 stsp int nentries;
4949 2b496619 2019-07-10 stsp *new_tep = NULL;
4951 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4952 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
4953 2b496619 2019-07-10 stsp child_path) == -1)
4954 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
4956 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
4957 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
4958 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
4959 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
4961 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4962 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
4963 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
4964 2b496619 2019-07-10 stsp goto done;
4966 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
4967 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
4968 2b496619 2019-07-10 stsp if (err) {
4969 56e0773d 2019-11-28 stsp free(new_te);
4970 2b496619 2019-07-10 stsp goto done;
4972 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
4975 2b496619 2019-07-10 stsp free(subtree_path);
4976 2b496619 2019-07-10 stsp if (err == NULL)
4977 2b496619 2019-07-10 stsp *new_tep = new_te;
4978 036813ee 2019-05-09 stsp return err;
4981 036813ee 2019-05-09 stsp static const struct got_error *
4982 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
4983 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
4984 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
4985 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4986 036813ee 2019-05-09 stsp struct got_repository *repo)
4988 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4989 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
4990 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
4991 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
4993 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
4994 ba580f68 2020-03-22 stsp *nentries = 0;
4996 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
4997 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4998 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4999 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5001 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5002 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5003 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5006 036813ee 2019-05-09 stsp if (!got_path_is_child(pe->path, path_base_tree,
5007 2f51b5b3 2019-05-09 stsp strlen(path_base_tree)))
5010 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5011 036813ee 2019-05-09 stsp pe->path);
5013 036813ee 2019-05-09 stsp goto done;
5015 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5016 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5017 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5019 036813ee 2019-05-09 stsp goto done;
5020 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5022 afa376bf 2019-05-09 stsp goto done;
5023 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5024 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5026 2f51b5b3 2019-05-09 stsp goto done;
5027 ba580f68 2020-03-22 stsp (*nentries)++;
5029 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5030 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5031 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5032 2b496619 2019-07-10 stsp == NULL) {
5033 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5034 2b496619 2019-07-10 stsp child_path, path_base_tree,
5035 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5038 2b496619 2019-07-10 stsp goto done;
5039 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5041 2b496619 2019-07-10 stsp goto done;
5042 ba580f68 2020-03-22 stsp (*nentries)++;
5047 2f51b5b3 2019-05-09 stsp if (base_tree) {
5048 56e0773d 2019-11-28 stsp int i, nbase_entries;
5049 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5050 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5051 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5052 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5054 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5055 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5056 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5057 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5059 63c5ca5d 2019-08-24 stsp goto done;
5060 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5062 63c5ca5d 2019-08-24 stsp goto done;
5063 ba580f68 2020-03-22 stsp (*nentries)++;
5067 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5068 44d03001 2019-05-09 stsp int modified;
5069 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5071 036813ee 2019-05-09 stsp goto done;
5072 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5073 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5075 2f51b5b3 2019-05-09 stsp goto done;
5076 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5077 44d03001 2019-05-09 stsp if (modified) {
5078 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5079 ba580f68 2020-03-22 stsp int nsubentries;
5080 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5081 ba580f68 2020-03-22 stsp &nsubentries, te,
5082 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5083 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5085 44d03001 2019-05-09 stsp goto done;
5086 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5087 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5088 ba580f68 2020-03-22 stsp free(new_id);
5091 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5092 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5093 56e0773d 2019-11-28 stsp free(new_id);
5095 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5097 036813ee 2019-05-09 stsp goto done;
5098 ba580f68 2020-03-22 stsp (*nentries)++;
5102 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5103 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5105 f66c734c 2019-09-22 stsp goto done;
5107 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5108 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5109 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5110 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5111 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5112 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5114 2f51b5b3 2019-05-09 stsp goto done;
5115 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5117 2f51b5b3 2019-05-09 stsp goto done;
5118 ba580f68 2020-03-22 stsp (*nentries)++;
5120 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5121 b416585c 2019-05-13 stsp status_arg);
5123 afa376bf 2019-05-09 stsp goto done;
5125 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5126 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5128 2f51b5b3 2019-05-09 stsp goto done;
5129 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5131 2f51b5b3 2019-05-09 stsp goto done;
5132 ba580f68 2020-03-22 stsp (*nentries)++;
5137 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5138 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5140 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
5141 2e1fa222 2020-07-23 stsp return err;
5144 2e1fa222 2020-07-23 stsp static const struct got_error *
5145 ebf99748 2019-05-09 stsp update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5146 72fd46fa 2019-09-06 stsp struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5147 72fd46fa 2019-09-06 stsp int have_staged_files)
5149 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5150 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5152 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5153 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5154 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5156 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5158 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5159 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5160 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5161 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5162 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5163 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5164 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5165 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5166 5f8a88c6 2019-08-03 stsp ct->ondisk_path, ct->staged_blob_id->sha1,
5167 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5168 72fd46fa 2019-09-06 stsp !have_staged_files);
5170 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5171 e75eb4da 2019-05-10 stsp ct->ondisk_path, ct->blob_id->sha1,
5172 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5173 72fd46fa 2019-09-06 stsp !have_staged_files);
5175 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5178 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ct->ondisk_path,
5179 3969253a 2020-03-07 stsp ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5180 3969253a 2020-03-07 stsp if (err) {
5181 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5184 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5185 3969253a 2020-03-07 stsp if (err) {
5186 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5191 d56d26ce 2019-05-10 stsp return err;
5195 d56d26ce 2019-05-10 stsp static const struct got_error *
5196 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5197 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5198 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5199 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5200 735ef5ac 2019-08-03 stsp int ood_errcode)
5202 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5203 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5205 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5206 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5207 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5208 1a36436d 2019-06-10 stsp return NULL;
5210 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5211 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5213 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5214 735ef5ac 2019-08-03 stsp in_repo_path);
5215 9bead371 2019-07-28 stsp if (err) {
5216 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5217 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5218 1a36436d 2019-06-10 stsp goto done;
5219 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5220 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5222 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5223 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5224 735ef5ac 2019-08-03 stsp in_repo_path);
5225 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5226 1a36436d 2019-06-10 stsp goto done;
5227 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5231 1a36436d 2019-06-10 stsp return err;
5234 c4296144 2019-05-09 stsp const struct got_error *
5235 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5236 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5237 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id, struct got_worktree *worktree,
5238 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
5239 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5240 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5241 afa376bf 2019-05-09 stsp struct got_repository *repo)
5243 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5244 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5245 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5246 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5247 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5248 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5249 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5250 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5251 ba580f68 2020-03-22 stsp int nentries;
5252 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5253 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5254 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5256 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5258 de18fc63 2019-05-09 stsp SIMPLEQ_INIT(&parent_ids);
5260 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5262 588edf97 2019-05-10 stsp goto done;
5264 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5266 588edf97 2019-05-10 stsp goto done;
5268 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5269 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5274 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5275 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5279 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5280 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5281 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5282 cf066bf8 2019-05-09 stsp char *ondisk_path;
5284 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5285 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5286 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5289 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5290 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5291 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5294 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5295 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5296 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5297 cf066bf8 2019-05-09 stsp goto done;
5299 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5300 2e1fa222 2020-07-23 stsp free(ondisk_path);
5302 cf066bf8 2019-05-09 stsp goto done;
5305 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5306 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5307 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5309 036813ee 2019-05-09 stsp goto done;
5311 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5313 de18fc63 2019-05-09 stsp goto done;
5314 de18fc63 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5315 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5316 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5317 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
5318 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5319 33ad4cbe 2019-05-12 jcs free(logmsg);
5321 9d40349a 2019-05-09 stsp goto done;
5323 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5324 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5325 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5326 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5327 09f5bd90 2019-05-09 stsp goto done;
5329 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
5330 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5332 09f5bd90 2019-05-09 stsp goto done;
5333 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5335 ebf99748 2019-05-09 stsp goto done;
5336 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5337 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5338 09f5bd90 2019-05-09 stsp goto done;
5340 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
5341 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
5343 f2c16586 2019-05-09 stsp goto done;
5344 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
5346 f2c16586 2019-05-09 stsp goto done;
5348 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5350 09f5bd90 2019-05-09 stsp goto done;
5352 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
5354 ebf99748 2019-05-09 stsp goto done;
5356 588edf97 2019-05-10 stsp if (head_tree)
5357 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
5358 588edf97 2019-05-10 stsp if (head_commit)
5359 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
5360 09f5bd90 2019-05-09 stsp free(head_commit_id2);
5361 2f17228e 2019-05-12 stsp if (head_ref2) {
5362 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
5363 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
5364 2f17228e 2019-05-12 stsp err = unlockerr;
5365 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
5367 39cd0ff6 2019-07-12 stsp return err;
5370 5c1e53bc 2019-07-28 stsp static const struct got_error *
5371 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
5372 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
5374 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
5375 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
5377 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
5378 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
5379 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
5381 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
5382 5c1e53bc 2019-07-28 stsp ct_path++;
5384 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
5385 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
5389 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
5390 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
5392 5c1e53bc 2019-07-28 stsp return NULL;
5395 f0b75401 2019-08-03 stsp static const struct got_error *
5396 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
5398 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
5400 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5401 f0b75401 2019-08-03 stsp *have_staged_files = 1;
5402 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
5405 f0b75401 2019-08-03 stsp return NULL;
5408 5f8a88c6 2019-08-03 stsp static const struct got_error *
5409 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
5410 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
5412 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
5413 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
5415 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5416 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
5418 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5419 5f8a88c6 2019-08-03 stsp if (ie == NULL)
5420 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5421 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5422 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
5423 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
5426 5f8a88c6 2019-08-03 stsp return NULL;
5429 39cd0ff6 2019-07-12 stsp const struct got_error *
5430 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
5431 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
5432 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
5433 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5434 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
5435 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
5437 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5438 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
5439 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
5440 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5441 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5442 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
5443 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
5444 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5445 f0b75401 2019-08-03 stsp int have_staged_files = 0;
5447 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
5449 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5451 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
5453 39cd0ff6 2019-07-12 stsp goto done;
5455 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5457 39cd0ff6 2019-07-12 stsp goto done;
5459 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5461 39cd0ff6 2019-07-12 stsp goto done;
5463 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5465 39cd0ff6 2019-07-12 stsp goto done;
5467 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5468 5f8a88c6 2019-08-03 stsp &have_staged_files);
5469 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
5470 5f8a88c6 2019-08-03 stsp goto done;
5471 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
5472 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
5474 5f8a88c6 2019-08-03 stsp goto done;
5477 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5478 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
5479 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
5480 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
5481 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
5482 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5483 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5484 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5485 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5487 5c1e53bc 2019-07-28 stsp goto done;
5490 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5491 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5492 39cd0ff6 2019-07-12 stsp goto done;
5495 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5496 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
5498 5c1e53bc 2019-07-28 stsp goto done;
5501 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5502 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5503 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5505 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5506 f0b75401 2019-08-03 stsp ct_path++;
5507 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5508 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5509 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5511 b50cabdf 2019-07-12 stsp goto done;
5515 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5516 5c1e53bc 2019-07-28 stsp head_commit_id, worktree, author, committer,
5517 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5519 39cd0ff6 2019-07-12 stsp goto done;
5521 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5522 72fd46fa 2019-09-06 stsp fileindex, have_staged_files);
5523 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5524 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5525 39cd0ff6 2019-07-12 stsp err = sync_err;
5527 39cd0ff6 2019-07-12 stsp if (fileindex)
5528 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5529 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5530 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5531 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5532 39cd0ff6 2019-07-12 stsp err = unlockerr;
5533 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5534 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5535 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5537 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5538 c4296144 2019-05-09 stsp return err;
5541 8656d6c4 2019-05-20 stsp const char *
5542 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5544 8656d6c4 2019-05-20 stsp return ct->path;
5547 8656d6c4 2019-05-20 stsp unsigned int
5548 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
5550 8656d6c4 2019-05-20 stsp return ct->status;
5553 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
5554 818c7501 2019-07-11 stsp struct got_worktree *worktree;
5555 818c7501 2019-07-11 stsp struct got_repository *repo;
5558 818c7501 2019-07-11 stsp static const struct got_error *
5559 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5561 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5562 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
5563 818c7501 2019-07-11 stsp unsigned char status;
5564 818c7501 2019-07-11 stsp struct stat sb;
5565 818c7501 2019-07-11 stsp char *ondisk_path;
5567 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
5568 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5569 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
5570 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
5572 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5574 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
5576 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
5577 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5578 818c7501 2019-07-11 stsp free(ondisk_path);
5580 818c7501 2019-07-11 stsp return err;
5582 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
5583 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
5584 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5585 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5587 818c7501 2019-07-11 stsp return NULL;
5590 818c7501 2019-07-11 stsp const struct got_error *
5591 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5592 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5593 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
5594 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5596 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5597 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5598 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
5599 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
5600 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
5601 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5602 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
5604 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5605 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5606 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5608 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
5610 818c7501 2019-07-11 stsp return err;
5612 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5614 818c7501 2019-07-11 stsp goto done;
5616 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
5617 818c7501 2019-07-11 stsp ok_arg.repo = repo;
5618 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5621 818c7501 2019-07-11 stsp goto done;
5623 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5625 818c7501 2019-07-11 stsp goto done;
5627 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5629 818c7501 2019-07-11 stsp goto done;
5631 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5633 818c7501 2019-07-11 stsp goto done;
5635 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5638 e51d7b55 2020-01-04 stsp goto done;
5640 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5642 818c7501 2019-07-11 stsp goto done;
5643 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5644 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5645 e51d7b55 2020-01-04 stsp goto done;
5648 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
5649 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
5651 818c7501 2019-07-11 stsp goto done;
5652 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
5654 818c7501 2019-07-11 stsp goto done;
5656 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
5658 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5660 818c7501 2019-07-11 stsp goto done;
5662 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
5664 818c7501 2019-07-11 stsp goto done;
5666 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
5667 818c7501 2019-07-11 stsp worktree->base_commit_id);
5669 818c7501 2019-07-11 stsp goto done;
5670 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
5672 818c7501 2019-07-11 stsp goto done;
5674 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
5676 818c7501 2019-07-11 stsp goto done;
5678 818c7501 2019-07-11 stsp free(fileindex_path);
5679 818c7501 2019-07-11 stsp free(tmp_branch_name);
5680 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
5681 818c7501 2019-07-11 stsp free(branch_ref_name);
5682 818c7501 2019-07-11 stsp if (branch_ref)
5683 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5684 818c7501 2019-07-11 stsp if (wt_branch)
5685 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
5686 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
5687 818c7501 2019-07-11 stsp if (err) {
5688 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
5689 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
5690 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5692 818c7501 2019-07-11 stsp if (*tmp_branch) {
5693 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5694 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5696 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5697 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5698 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5700 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
5702 818c7501 2019-07-11 stsp return err;
5705 818c7501 2019-07-11 stsp const struct got_error *
5706 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
5707 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5708 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
5709 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
5711 818c7501 2019-07-11 stsp const struct got_error *err;
5712 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5713 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5714 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5715 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
5716 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
5718 818c7501 2019-07-11 stsp *commit_id = NULL;
5719 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
5720 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
5721 3e3a69f1 2019-07-25 stsp *branch = NULL;
5722 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5724 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
5726 3e3a69f1 2019-07-25 stsp return err;
5728 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5730 f032f1f7 2019-08-04 stsp goto done;
5732 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5733 f032f1f7 2019-08-04 stsp &have_staged_files);
5734 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
5735 f032f1f7 2019-08-04 stsp goto done;
5736 f032f1f7 2019-08-04 stsp if (have_staged_files) {
5737 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
5738 3e3a69f1 2019-07-25 stsp goto done;
5741 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5743 f032f1f7 2019-08-04 stsp goto done;
5745 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5747 818c7501 2019-07-11 stsp goto done;
5749 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5751 818c7501 2019-07-11 stsp goto done;
5753 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5755 818c7501 2019-07-11 stsp goto done;
5757 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5759 818c7501 2019-07-11 stsp goto done;
5761 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
5762 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
5764 818c7501 2019-07-11 stsp goto done;
5766 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5768 818c7501 2019-07-11 stsp goto done;
5770 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
5772 818c7501 2019-07-11 stsp goto done;
5774 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
5775 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
5777 818c7501 2019-07-11 stsp goto done;
5779 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5781 818c7501 2019-07-11 stsp goto done;
5783 818c7501 2019-07-11 stsp free(commit_ref_name);
5784 818c7501 2019-07-11 stsp free(branch_ref_name);
5785 3e3a69f1 2019-07-25 stsp free(fileindex_path);
5786 818c7501 2019-07-11 stsp if (commit_ref)
5787 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
5788 818c7501 2019-07-11 stsp if (branch_ref)
5789 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5790 818c7501 2019-07-11 stsp if (err) {
5791 818c7501 2019-07-11 stsp free(*commit_id);
5792 818c7501 2019-07-11 stsp *commit_id = NULL;
5793 818c7501 2019-07-11 stsp if (*tmp_branch) {
5794 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5795 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5797 818c7501 2019-07-11 stsp if (*new_base_branch) {
5798 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
5799 818c7501 2019-07-11 stsp *new_base_branch = NULL;
5801 818c7501 2019-07-11 stsp if (*branch) {
5802 818c7501 2019-07-11 stsp got_ref_close(*branch);
5803 818c7501 2019-07-11 stsp *branch = NULL;
5805 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5806 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5807 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5809 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
5811 818c7501 2019-07-11 stsp return err;
5814 818c7501 2019-07-11 stsp const struct got_error *
5815 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5817 818c7501 2019-07-11 stsp const struct got_error *err;
5818 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
5820 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5822 818c7501 2019-07-11 stsp return err;
5824 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5825 818c7501 2019-07-11 stsp free(tmp_branch_name);
5826 818c7501 2019-07-11 stsp return NULL;
5829 818c7501 2019-07-11 stsp static const struct got_error *
5830 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5831 818c7501 2019-07-11 stsp char **logmsg, void *arg)
5833 0ebf8283 2019-07-24 stsp *logmsg = arg;
5834 818c7501 2019-07-11 stsp return NULL;
5837 818c7501 2019-07-11 stsp static const struct got_error *
5838 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5839 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
5840 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5841 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5843 818c7501 2019-07-11 stsp return NULL;
5846 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
5847 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
5848 01757395 2019-07-12 stsp void *progress_arg;
5849 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
5852 01757395 2019-07-12 stsp static const struct got_error *
5853 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
5855 01757395 2019-07-12 stsp const struct got_error *err;
5856 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
5858 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
5860 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
5862 01757395 2019-07-12 stsp return err;
5864 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
5865 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
5866 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
5867 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
5868 01757395 2019-07-12 stsp return NULL;
5870 01757395 2019-07-12 stsp p = strdup(path);
5871 01757395 2019-07-12 stsp if (p == NULL)
5872 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
5874 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5875 01757395 2019-07-12 stsp if (err || new == NULL)
5877 01757395 2019-07-12 stsp return err;
5881 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5883 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
5885 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
5886 01757395 2019-07-12 stsp free((char *)pe->path);
5888 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
5891 0ebf8283 2019-07-24 stsp static const struct got_error *
5892 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5893 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
5895 818c7501 2019-07-11 stsp const struct got_error *err;
5896 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
5898 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5899 818c7501 2019-07-11 stsp if (err) {
5900 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
5901 818c7501 2019-07-11 stsp goto done;
5902 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5904 818c7501 2019-07-11 stsp goto done;
5905 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
5907 818c7501 2019-07-11 stsp goto done;
5908 de05890f 2020-03-05 stsp } else if (is_rebase) {
5909 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
5912 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
5914 818c7501 2019-07-11 stsp goto done;
5915 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
5916 818c7501 2019-07-11 stsp free(stored_id);
5917 818c7501 2019-07-11 stsp if (cmp != 0) {
5918 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
5919 818c7501 2019-07-11 stsp goto done;
5923 0ebf8283 2019-07-24 stsp if (commit_ref)
5924 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
5925 0ebf8283 2019-07-24 stsp return err;
5928 0ebf8283 2019-07-24 stsp static const struct got_error *
5929 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
5930 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
5931 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5932 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
5933 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5934 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
5936 0ebf8283 2019-07-24 stsp const struct got_error *err;
5937 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
5938 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
5939 3e3a69f1 2019-07-25 stsp char *fileindex_path;
5941 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
5943 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
5945 0ebf8283 2019-07-24 stsp return err;
5947 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
5948 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
5949 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
5950 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
5951 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
5952 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
5953 818c7501 2019-07-11 stsp if (commit_ref)
5954 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
5955 818c7501 2019-07-11 stsp return err;
5958 818c7501 2019-07-11 stsp const struct got_error *
5959 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5960 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5961 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5962 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
5963 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5964 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
5966 0ebf8283 2019-07-24 stsp const struct got_error *err;
5967 0ebf8283 2019-07-24 stsp char *commit_ref_name;
5969 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5971 0ebf8283 2019-07-24 stsp return err;
5973 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5975 0ebf8283 2019-07-24 stsp goto done;
5977 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5978 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
5979 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
5981 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5982 0ebf8283 2019-07-24 stsp return err;
5985 0ebf8283 2019-07-24 stsp const struct got_error *
5986 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5987 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5988 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5989 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
5990 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5991 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
5993 0ebf8283 2019-07-24 stsp const struct got_error *err;
5994 0ebf8283 2019-07-24 stsp char *commit_ref_name;
5996 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5998 0ebf8283 2019-07-24 stsp return err;
6000 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6002 0ebf8283 2019-07-24 stsp goto done;
6004 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6005 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6006 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6008 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6009 0ebf8283 2019-07-24 stsp return err;
6012 0ebf8283 2019-07-24 stsp static const struct got_error *
6013 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6014 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6015 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6016 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6017 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
6019 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6020 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6021 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6022 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6023 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6024 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6025 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6027 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6028 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6030 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6032 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6034 3e3a69f1 2019-07-25 stsp return err;
6036 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6037 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6038 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6039 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6041 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6042 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6043 01757395 2019-07-12 stsp * TODO: Ideally, merged_paths would contain a list of commitables
6044 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6046 01757395 2019-07-12 stsp if (merged_paths) {
6047 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6048 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6049 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6050 f2a9dc41 2019-12-13 tracey repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6053 01757395 2019-07-12 stsp goto done;
6056 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6057 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6059 01757395 2019-07-12 stsp goto done;
6062 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6063 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6064 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6066 a0e95631 2019-07-12 stsp goto done;
6067 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6068 a0e95631 2019-07-12 stsp goto done;
6071 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6073 edd02c5e 2019-07-11 stsp goto done;
6075 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6077 818c7501 2019-07-11 stsp goto done;
6079 5943eee2 2019-08-13 stsp if (new_logmsg) {
6080 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6081 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6082 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6083 5943eee2 2019-08-13 stsp goto done;
6086 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6088 5943eee2 2019-08-13 stsp goto done;
6091 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6092 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6093 5c1e53bc 2019-07-28 stsp worktree, got_object_commit_get_author(orig_commit),
6094 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
6095 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6097 a0e95631 2019-07-12 stsp goto done;
6099 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6101 a0e95631 2019-07-12 stsp goto done;
6103 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6105 a0e95631 2019-07-12 stsp goto done;
6107 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6108 72fd46fa 2019-09-06 stsp fileindex, 0);
6109 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6110 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6111 a0e95631 2019-07-12 stsp err = sync_err;
6113 a0e95631 2019-07-12 stsp free(fileindex_path);
6114 a0e95631 2019-07-12 stsp free(head_commit_id);
6115 a0e95631 2019-07-12 stsp if (head_ref)
6116 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6117 818c7501 2019-07-11 stsp if (err) {
6118 818c7501 2019-07-11 stsp free(*new_commit_id);
6119 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6121 818c7501 2019-07-11 stsp return err;
6124 818c7501 2019-07-11 stsp const struct got_error *
6125 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6126 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6127 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6128 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6129 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6131 0ebf8283 2019-07-24 stsp const struct got_error *err;
6132 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6133 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6134 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6136 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6138 0ebf8283 2019-07-24 stsp return err;
6140 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6142 0ebf8283 2019-07-24 stsp goto done;
6143 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6145 0ebf8283 2019-07-24 stsp goto done;
6146 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6147 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6148 0ebf8283 2019-07-24 stsp goto done;
6151 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6152 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6154 0ebf8283 2019-07-24 stsp if (commit_ref)
6155 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6156 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6157 0ebf8283 2019-07-24 stsp free(commit_id);
6158 0ebf8283 2019-07-24 stsp return err;
6161 0ebf8283 2019-07-24 stsp const struct got_error *
6162 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6163 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6164 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6165 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6166 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6167 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6169 0ebf8283 2019-07-24 stsp const struct got_error *err;
6170 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6171 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6173 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6175 0ebf8283 2019-07-24 stsp return err;
6177 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6179 0ebf8283 2019-07-24 stsp goto done;
6181 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6182 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6184 0ebf8283 2019-07-24 stsp if (commit_ref)
6185 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6186 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6187 0ebf8283 2019-07-24 stsp return err;
6190 0ebf8283 2019-07-24 stsp const struct got_error *
6191 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6192 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6194 3e3a69f1 2019-07-25 stsp if (fileindex)
6195 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6196 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6199 69844fba 2019-07-11 stsp static const struct got_error *
6200 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6202 69844fba 2019-07-11 stsp const struct got_error *err;
6203 69844fba 2019-07-11 stsp struct got_reference *ref;
6205 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6206 69844fba 2019-07-11 stsp if (err) {
6207 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6208 69844fba 2019-07-11 stsp return NULL;
6209 69844fba 2019-07-11 stsp return err;
6212 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6213 69844fba 2019-07-11 stsp got_ref_close(ref);
6214 69844fba 2019-07-11 stsp return err;
6217 69844fba 2019-07-11 stsp static const struct got_error *
6218 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6220 69844fba 2019-07-11 stsp const struct got_error *err;
6221 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6222 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6224 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6226 69844fba 2019-07-11 stsp goto done;
6227 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6229 69844fba 2019-07-11 stsp goto done;
6231 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6233 69844fba 2019-07-11 stsp goto done;
6234 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6236 69844fba 2019-07-11 stsp goto done;
6238 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6240 69844fba 2019-07-11 stsp goto done;
6241 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6243 69844fba 2019-07-11 stsp goto done;
6245 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6247 69844fba 2019-07-11 stsp goto done;
6248 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6250 69844fba 2019-07-11 stsp goto done;
6253 69844fba 2019-07-11 stsp free(tmp_branch_name);
6254 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6255 69844fba 2019-07-11 stsp free(branch_ref_name);
6256 69844fba 2019-07-11 stsp free(commit_ref_name);
6257 69844fba 2019-07-11 stsp return err;
6260 818c7501 2019-07-11 stsp const struct got_error *
6261 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
6262 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6263 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6264 818c7501 2019-07-11 stsp struct got_repository *repo)
6266 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
6267 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
6269 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6271 818c7501 2019-07-11 stsp return err;
6273 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6275 818c7501 2019-07-11 stsp goto done;
6277 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
6279 818c7501 2019-07-11 stsp goto done;
6281 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
6283 818c7501 2019-07-11 stsp goto done;
6285 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
6287 3e3a69f1 2019-07-25 stsp if (fileindex)
6288 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6289 818c7501 2019-07-11 stsp free(new_head_commit_id);
6290 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6291 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6292 818c7501 2019-07-11 stsp err = unlockerr;
6293 818c7501 2019-07-11 stsp return err;
6296 818c7501 2019-07-11 stsp const struct got_error *
6297 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
6298 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6299 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
6300 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6302 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
6303 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
6304 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
6305 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6306 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6307 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
6309 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6311 818c7501 2019-07-11 stsp return err;
6313 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
6314 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
6316 818c7501 2019-07-11 stsp goto done;
6318 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
6320 818c7501 2019-07-11 stsp goto done;
6323 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
6324 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
6325 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
6327 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
6329 818c7501 2019-07-11 stsp goto done;
6331 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6333 818c7501 2019-07-11 stsp goto done;
6335 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
6336 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
6338 ca355955 2019-07-12 stsp goto done;
6340 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
6342 ca355955 2019-07-12 stsp goto done;
6344 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6346 818c7501 2019-07-11 stsp goto done;
6348 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6349 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6350 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6351 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6352 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6353 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6354 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6355 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6356 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6358 1f1abb7e 2019-08-08 stsp goto sync;
6360 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6361 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
6363 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6364 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
6365 ca355955 2019-07-12 stsp err = sync_err;
6367 818c7501 2019-07-11 stsp got_ref_close(resolved);
6368 a3a2faf2 2019-07-12 stsp free(tree_id);
6369 818c7501 2019-07-11 stsp free(commit_id);
6370 0ebf8283 2019-07-24 stsp if (fileindex)
6371 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
6372 0ebf8283 2019-07-24 stsp free(fileindex_path);
6374 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6375 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6376 0ebf8283 2019-07-24 stsp err = unlockerr;
6377 0ebf8283 2019-07-24 stsp return err;
6380 0ebf8283 2019-07-24 stsp const struct got_error *
6381 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6382 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6383 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
6384 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6386 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6387 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6388 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
6389 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
6390 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6391 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
6392 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
6393 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6395 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6396 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6397 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6398 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6400 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6402 0ebf8283 2019-07-24 stsp return err;
6404 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6406 0ebf8283 2019-07-24 stsp goto done;
6408 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
6409 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
6410 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6413 0ebf8283 2019-07-24 stsp goto done;
6415 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6417 0ebf8283 2019-07-24 stsp goto done;
6419 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6421 0ebf8283 2019-07-24 stsp goto done;
6423 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6424 0ebf8283 2019-07-24 stsp worktree);
6426 0ebf8283 2019-07-24 stsp goto done;
6428 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6431 0ebf8283 2019-07-24 stsp goto done;
6433 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6435 0ebf8283 2019-07-24 stsp goto done;
6437 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
6439 0ebf8283 2019-07-24 stsp goto done;
6441 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6442 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6444 0ebf8283 2019-07-24 stsp goto done;
6445 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
6447 0ebf8283 2019-07-24 stsp goto done;
6448 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6449 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
6450 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
6451 0ebf8283 2019-07-24 stsp goto done;
6454 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6455 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6457 0ebf8283 2019-07-24 stsp goto done;
6458 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
6460 0ebf8283 2019-07-24 stsp goto done;
6462 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6464 0ebf8283 2019-07-24 stsp goto done;
6466 0ebf8283 2019-07-24 stsp free(fileindex_path);
6467 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6468 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6469 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6470 0ebf8283 2019-07-24 stsp if (wt_branch)
6471 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
6472 0ebf8283 2019-07-24 stsp if (err) {
6473 0ebf8283 2019-07-24 stsp if (*branch_ref) {
6474 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
6475 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6477 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6478 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6479 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6481 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6482 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6483 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6484 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6486 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
6488 0ebf8283 2019-07-24 stsp return err;
6491 0ebf8283 2019-07-24 stsp const struct got_error *
6492 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
6493 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6495 3e3a69f1 2019-07-25 stsp if (fileindex)
6496 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6497 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
6500 0ebf8283 2019-07-24 stsp const struct got_error *
6501 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
6502 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
6504 0ebf8283 2019-07-24 stsp const struct got_error *err;
6505 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6507 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6509 0ebf8283 2019-07-24 stsp return err;
6511 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6512 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6513 0ebf8283 2019-07-24 stsp return NULL;
6516 0ebf8283 2019-07-24 stsp const struct got_error *
6517 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
6518 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
6519 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6520 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6522 0ebf8283 2019-07-24 stsp const struct got_error *err;
6523 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6524 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6525 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6526 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6527 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6528 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6530 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6531 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6532 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6533 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6535 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6537 3e3a69f1 2019-07-25 stsp return err;
6539 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6541 f032f1f7 2019-08-04 stsp goto done;
6543 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6544 f032f1f7 2019-08-04 stsp &have_staged_files);
6545 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6546 f032f1f7 2019-08-04 stsp goto done;
6547 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6548 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6549 3e3a69f1 2019-07-25 stsp goto done;
6552 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6554 f032f1f7 2019-08-04 stsp goto done;
6556 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6558 0ebf8283 2019-07-24 stsp goto done;
6560 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6562 0ebf8283 2019-07-24 stsp goto done;
6564 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6565 0ebf8283 2019-07-24 stsp worktree);
6567 0ebf8283 2019-07-24 stsp goto done;
6569 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6571 0ebf8283 2019-07-24 stsp goto done;
6573 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6575 0ebf8283 2019-07-24 stsp goto done;
6576 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6578 0ebf8283 2019-07-24 stsp goto done;
6580 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6582 0ebf8283 2019-07-24 stsp goto done;
6583 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6585 0ebf8283 2019-07-24 stsp goto done;
6587 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6589 0ebf8283 2019-07-24 stsp goto done;
6591 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6592 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6593 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6594 0ebf8283 2019-07-24 stsp if (commit_ref)
6595 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6596 0ebf8283 2019-07-24 stsp if (base_commit_ref)
6597 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
6598 0ebf8283 2019-07-24 stsp if (err) {
6599 0ebf8283 2019-07-24 stsp free(*commit_id);
6600 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6601 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6602 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6603 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6604 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6605 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6607 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6608 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6609 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6611 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
6613 0ebf8283 2019-07-24 stsp return err;
6616 0ebf8283 2019-07-24 stsp static const struct got_error *
6617 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6619 0ebf8283 2019-07-24 stsp const struct got_error *err;
6620 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6621 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6623 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6625 0ebf8283 2019-07-24 stsp goto done;
6626 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
6628 0ebf8283 2019-07-24 stsp goto done;
6630 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6631 0ebf8283 2019-07-24 stsp worktree);
6633 0ebf8283 2019-07-24 stsp goto done;
6634 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
6636 0ebf8283 2019-07-24 stsp goto done;
6638 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6640 0ebf8283 2019-07-24 stsp goto done;
6641 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
6643 0ebf8283 2019-07-24 stsp goto done;
6645 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6647 0ebf8283 2019-07-24 stsp goto done;
6648 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
6650 0ebf8283 2019-07-24 stsp goto done;
6652 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6653 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6654 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6655 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6656 0ebf8283 2019-07-24 stsp return err;
6659 0ebf8283 2019-07-24 stsp const struct got_error *
6660 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
6661 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6662 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
6663 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6665 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
6666 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6667 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6668 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
6669 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6671 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6673 0ebf8283 2019-07-24 stsp return err;
6675 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
6676 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
6678 0ebf8283 2019-07-24 stsp goto done;
6680 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
6682 0ebf8283 2019-07-24 stsp goto done;
6684 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6686 0ebf8283 2019-07-24 stsp goto done;
6688 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6689 0ebf8283 2019-07-24 stsp worktree->path_prefix);
6691 0ebf8283 2019-07-24 stsp goto done;
6693 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
6695 0ebf8283 2019-07-24 stsp goto done;
6697 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6699 0ebf8283 2019-07-24 stsp goto done;
6701 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6702 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6703 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6704 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6705 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6706 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6707 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6708 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
6709 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6711 1f1abb7e 2019-08-08 stsp goto sync;
6713 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6714 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
6716 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6717 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
6718 0ebf8283 2019-07-24 stsp err = sync_err;
6720 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
6721 0ebf8283 2019-07-24 stsp free(tree_id);
6722 818c7501 2019-07-11 stsp free(fileindex_path);
6724 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6725 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6726 818c7501 2019-07-11 stsp err = unlockerr;
6727 818c7501 2019-07-11 stsp return err;
6730 0ebf8283 2019-07-24 stsp const struct got_error *
6731 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
6732 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6733 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
6735 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr;
6736 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
6737 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6739 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6741 0ebf8283 2019-07-24 stsp return err;
6743 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
6744 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
6746 0ebf8283 2019-07-24 stsp goto done;
6748 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
6750 0ebf8283 2019-07-24 stsp goto done;
6752 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
6754 0ebf8283 2019-07-24 stsp goto done;
6756 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
6758 0ebf8283 2019-07-24 stsp goto done;
6760 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
6762 3e3a69f1 2019-07-25 stsp if (fileindex)
6763 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6764 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
6765 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6766 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6767 0ebf8283 2019-07-24 stsp err = unlockerr;
6768 0ebf8283 2019-07-24 stsp return err;
6771 0ebf8283 2019-07-24 stsp const struct got_error *
6772 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6773 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6775 0ebf8283 2019-07-24 stsp const struct got_error *err;
6776 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6778 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6780 0ebf8283 2019-07-24 stsp return err;
6782 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6784 0ebf8283 2019-07-24 stsp goto done;
6786 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
6788 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6789 2822a352 2019-10-15 stsp return err;
6792 2822a352 2019-10-15 stsp const struct got_error *
6793 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6794 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6795 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
6796 2822a352 2019-10-15 stsp struct got_repository *repo)
6798 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
6799 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
6800 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
6802 2822a352 2019-10-15 stsp *fileindex = NULL;
6803 2822a352 2019-10-15 stsp *branch_ref = NULL;
6804 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
6806 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
6808 2822a352 2019-10-15 stsp return err;
6810 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6811 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
6812 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
6813 2822a352 2019-10-15 stsp "update -b or different branch name required");
6814 2822a352 2019-10-15 stsp goto done;
6817 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6819 2822a352 2019-10-15 stsp goto done;
6821 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
6822 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
6823 2822a352 2019-10-15 stsp ok_arg.repo = repo;
6824 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6827 2822a352 2019-10-15 stsp goto done;
6829 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
6831 2822a352 2019-10-15 stsp goto done;
6833 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
6834 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
6836 2822a352 2019-10-15 stsp if (err) {
6837 2822a352 2019-10-15 stsp if (*branch_ref) {
6838 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
6839 2822a352 2019-10-15 stsp *branch_ref = NULL;
6841 2822a352 2019-10-15 stsp if (*base_branch_ref) {
6842 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
6843 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
6845 2822a352 2019-10-15 stsp if (*fileindex) {
6846 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
6847 2822a352 2019-10-15 stsp *fileindex = NULL;
6849 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
6851 0cb83759 2019-08-03 stsp return err;
6854 2822a352 2019-10-15 stsp const struct got_error *
6855 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
6856 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6857 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6858 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6859 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6861 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
6862 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
6863 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
6865 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
6867 2822a352 2019-10-15 stsp goto done;
6869 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
6871 2822a352 2019-10-15 stsp goto done;
6873 2822a352 2019-10-15 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
6874 2822a352 2019-10-15 stsp worktree->path_prefix);
6876 2822a352 2019-10-15 stsp goto done;
6878 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6880 2822a352 2019-10-15 stsp goto done;
6882 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6883 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
6885 2822a352 2019-10-15 stsp goto sync;
6887 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
6889 2822a352 2019-10-15 stsp goto sync;
6891 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
6893 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6894 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
6895 2822a352 2019-10-15 stsp err = sync_err;
6898 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
6899 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6900 2822a352 2019-10-15 stsp err = unlockerr;
6901 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
6903 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
6904 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6905 2822a352 2019-10-15 stsp err = unlockerr;
6906 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
6908 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
6909 2822a352 2019-10-15 stsp free(fileindex_path);
6910 2822a352 2019-10-15 stsp free(tree_id);
6912 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6913 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6914 2822a352 2019-10-15 stsp err = unlockerr;
6915 2822a352 2019-10-15 stsp return err;
6918 2822a352 2019-10-15 stsp const struct got_error *
6919 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
6920 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6921 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6923 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
6925 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
6927 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
6929 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
6930 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
6931 8b692cd0 2019-10-21 stsp err = unlockerr;
6932 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
6934 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
6935 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
6936 8b692cd0 2019-10-21 stsp err = unlockerr;
6937 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
6939 8b692cd0 2019-10-21 stsp return err;
6942 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
6943 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
6944 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
6945 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
6946 2db2652d 2019-08-07 stsp struct got_repository *repo;
6947 2db2652d 2019-08-07 stsp int have_changes;
6950 2db2652d 2019-08-07 stsp const struct got_error *
6951 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
6952 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
6953 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6954 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
6956 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
6957 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
6958 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
6959 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
6960 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
6961 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
6963 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
6964 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
6965 8b13ce36 2019-08-08 stsp return NULL;
6966 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
6967 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
6969 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6970 735ef5ac 2019-08-03 stsp if (ie == NULL)
6971 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6973 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6974 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6975 735ef5ac 2019-08-03 stsp relpath) == -1)
6976 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
6978 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
6979 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
6980 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
6981 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
6984 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
6985 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6986 3aa5969e 2019-08-06 stsp goto done;
6987 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
6988 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
6989 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
6990 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6991 735ef5ac 2019-08-03 stsp goto done;
6994 2db2652d 2019-08-07 stsp a->have_changes = 1;
6996 735ef5ac 2019-08-03 stsp p = in_repo_path;
6997 735ef5ac 2019-08-03 stsp while (p[0] == '/')
6999 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
7000 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
7001 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
7003 735ef5ac 2019-08-03 stsp free(in_repo_path);
7004 dc424a06 2019-08-07 stsp return err;
7007 2db2652d 2019-08-07 stsp struct stage_path_arg {
7008 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7009 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7010 2db2652d 2019-08-07 stsp struct got_repository *repo;
7011 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
7012 2db2652d 2019-08-07 stsp void *status_arg;
7013 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
7014 2db2652d 2019-08-07 stsp void *patch_arg;
7015 7b5dc508 2019-10-28 stsp int staged_something;
7016 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
7019 2db2652d 2019-08-07 stsp static const struct got_error *
7020 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
7021 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7022 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7023 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7025 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
7026 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
7027 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
7028 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
7029 0cb83759 2019-08-03 stsp uint32_t stage;
7030 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
7031 0aeb8099 2020-07-23 stsp struct stat sb;
7033 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
7034 8b13ce36 2019-08-08 stsp return NULL;
7036 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7037 d3e7c587 2019-08-03 stsp if (ie == NULL)
7038 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7040 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7041 2db2652d 2019-08-07 stsp relpath)== -1)
7042 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
7044 0cb83759 2019-08-03 stsp switch (status) {
7045 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
7046 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
7047 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
7048 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
7049 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
7052 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7053 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
7054 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7055 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7056 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
7059 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
7062 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
7063 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
7064 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
7065 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
7066 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
7070 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
7071 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
7074 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7075 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7076 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7077 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
7079 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
7080 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7081 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
7082 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
7083 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
7084 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
7085 35213c7c 2020-07-23 stsp ssize_t target_len;
7086 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
7087 35213c7c 2020-07-23 stsp sizeof(target_path));
7088 35213c7c 2020-07-23 stsp if (target_len == -1) {
7089 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
7090 35213c7c 2020-07-23 stsp ondisk_path);
7093 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
7094 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
7095 35213c7c 2020-07-23 stsp a->worktree->root_path);
7098 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
7099 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
7100 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
7104 35213c7c 2020-07-23 stsp if (is_bad_symlink)
7105 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7106 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
7108 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7109 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
7111 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7112 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
7114 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7115 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7117 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7118 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
7119 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
7121 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
7122 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
7124 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7125 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7126 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
7127 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
7130 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7132 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7133 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7137 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
7138 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7139 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7140 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7142 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7143 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7146 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
7148 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
7149 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7151 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
7152 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
7155 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7159 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
7160 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
7161 dc424a06 2019-08-07 stsp free(path_content);
7162 2db2652d 2019-08-07 stsp free(ondisk_path);
7163 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
7164 0ebf8283 2019-07-24 stsp return err;
7167 0cb83759 2019-08-03 stsp const struct got_error *
7168 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
7169 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
7170 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
7171 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7172 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
7174 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7175 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
7176 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7177 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
7178 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
7179 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
7180 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
7181 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
7183 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7185 0cb83759 2019-08-03 stsp return err;
7187 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
7188 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
7190 735ef5ac 2019-08-03 stsp goto done;
7191 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7193 735ef5ac 2019-08-03 stsp goto done;
7194 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7196 0cb83759 2019-08-03 stsp goto done;
7198 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
7199 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
7200 2db2652d 2019-08-07 stsp oka.worktree = worktree;
7201 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
7202 2db2652d 2019-08-07 stsp oka.repo = repo;
7203 2db2652d 2019-08-07 stsp oka.have_changes = 0;
7204 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7205 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7206 f2a9dc41 2019-12-13 tracey check_stage_ok, &oka, NULL, NULL, 0, 0);
7208 735ef5ac 2019-08-03 stsp goto done;
7210 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
7211 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7212 2db2652d 2019-08-07 stsp goto done;
7215 2db2652d 2019-08-07 stsp spa.worktree = worktree;
7216 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
7217 2db2652d 2019-08-07 stsp spa.repo = repo;
7218 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
7219 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
7220 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
7221 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
7222 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
7223 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
7224 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7225 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7226 f2a9dc41 2019-12-13 tracey stage_path, &spa, NULL, NULL, 0, 0);
7228 2db2652d 2019-08-07 stsp goto done;
7230 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
7231 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7232 7b5dc508 2019-10-28 stsp goto done;
7235 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7236 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
7237 0cb83759 2019-08-03 stsp err = sync_err;
7239 735ef5ac 2019-08-03 stsp if (head_ref)
7240 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
7241 735ef5ac 2019-08-03 stsp free(head_commit_id);
7242 0cb83759 2019-08-03 stsp free(fileindex_path);
7243 0cb83759 2019-08-03 stsp if (fileindex)
7244 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
7245 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7246 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
7247 0cb83759 2019-08-03 stsp err = unlockerr;
7248 0cb83759 2019-08-03 stsp return err;
7251 ad493afc 2019-08-03 stsp struct unstage_path_arg {
7252 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
7253 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
7254 ad493afc 2019-08-03 stsp struct got_repository *repo;
7255 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
7256 ad493afc 2019-08-03 stsp void *progress_arg;
7257 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
7258 2e1f37b0 2019-08-08 stsp void *patch_arg;
7261 2e1f37b0 2019-08-08 stsp static const struct got_error *
7262 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
7263 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
7264 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
7265 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
7266 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
7268 2e1f37b0 2019-08-08 stsp const struct got_error *err;
7269 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
7270 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7271 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7272 2e1f37b0 2019-08-08 stsp struct stat sb1, sb2;
7273 2e1f37b0 2019-08-08 stsp struct got_diff_changes *changes = NULL;
7274 2e1f37b0 2019-08-08 stsp struct got_diff_state *ds = NULL;
7275 2e1f37b0 2019-08-08 stsp struct got_diff_args *args = NULL;
7276 2e1f37b0 2019-08-08 stsp struct got_diff_change *change;
7277 2e1f37b0 2019-08-08 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7278 2e1f37b0 2019-08-08 stsp int have_content = 0, have_rejected_content = 0;
7280 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7281 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7283 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
7285 2e1f37b0 2019-08-08 stsp return err;
7286 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7288 2e1f37b0 2019-08-08 stsp goto done;
7290 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7292 2e1f37b0 2019-08-08 stsp goto done;
7294 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7296 2e1f37b0 2019-08-08 stsp goto done;
7298 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7300 2e1f37b0 2019-08-08 stsp goto done;
7302 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7304 2e1f37b0 2019-08-08 stsp goto done;
7306 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7308 2e1f37b0 2019-08-08 stsp goto done;
7310 2e1f37b0 2019-08-08 stsp if (stat(path1, &sb1) == -1) {
7311 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path1);
7312 2e1f37b0 2019-08-08 stsp goto done;
7315 2e1f37b0 2019-08-08 stsp if (stat(path2, &sb2) == -1) {
7316 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path2);
7317 2e1f37b0 2019-08-08 stsp goto done;
7320 2e1f37b0 2019-08-08 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
7321 2e1f37b0 2019-08-08 stsp f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7323 2e1f37b0 2019-08-08 stsp goto done;
7325 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
7326 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
7328 2e1f37b0 2019-08-08 stsp goto done;
7329 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
7330 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
7332 2e1f37b0 2019-08-08 stsp goto done;
7334 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
7335 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
7336 2e1f37b0 2019-08-08 stsp goto done;
7338 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
7339 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
7340 2e1f37b0 2019-08-08 stsp goto done;
7342 2e1f37b0 2019-08-08 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7343 2e1f37b0 2019-08-08 stsp int choice;
7344 2e1f37b0 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
7345 2e1f37b0 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
7346 2e1f37b0 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
7347 2e1f37b0 2019-08-08 stsp outfile, rejectfile, patch_cb, patch_arg);
7349 2e1f37b0 2019-08-08 stsp goto done;
7350 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
7351 2e1f37b0 2019-08-08 stsp have_content = 1;
7353 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
7354 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
7357 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
7358 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7359 f1e81a05 2019-08-10 stsp outfile, rejectfile);
7361 2e1f37b0 2019-08-08 stsp free(label1);
7363 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
7364 2e1f37b0 2019-08-08 stsp if (staged_blob)
7365 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
7366 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
7367 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
7368 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
7369 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
7370 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
7371 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
7372 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7373 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
7374 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
7375 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
7376 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
7377 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
7378 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
7379 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
7380 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
7381 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7382 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
7383 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
7384 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7386 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
7387 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
7388 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
7389 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7390 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
7391 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
7392 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7394 2e1f37b0 2019-08-08 stsp free(args);
7396 2e1f37b0 2019-08-08 stsp got_diff_state_free(ds);
7399 2e1f37b0 2019-08-08 stsp if (changes)
7400 2e1f37b0 2019-08-08 stsp got_diff_free_changes(changes);
7401 2e1f37b0 2019-08-08 stsp free(path1);
7402 2e1f37b0 2019-08-08 stsp free(path2);
7403 fda8017d 2020-07-23 stsp return err;
7406 fda8017d 2020-07-23 stsp static const struct got_error *
7407 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
7408 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
7409 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7410 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
7411 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
7412 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7413 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7415 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
7416 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
7417 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
7418 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
7419 fda8017d 2020-07-23 stsp FILE *f = NULL;
7420 fda8017d 2020-07-23 stsp struct stat sb;
7422 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
7423 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
7424 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
7426 fda8017d 2020-07-23 stsp return err;
7428 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
7429 fda8017d 2020-07-23 stsp return NULL;
7431 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
7432 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
7433 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
7435 fda8017d 2020-07-23 stsp goto done;
7438 fda8017d 2020-07-23 stsp f = fopen(path_unstaged_content, "r");
7439 fda8017d 2020-07-23 stsp if (f == NULL) {
7440 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
7441 fda8017d 2020-07-23 stsp path_unstaged_content);
7442 fda8017d 2020-07-23 stsp goto done;
7444 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
7445 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
7446 fda8017d 2020-07-23 stsp goto done;
7448 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
7449 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7450 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
7452 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
7453 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
7454 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
7455 fda8017d 2020-07-23 stsp goto done;
7457 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
7458 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7459 fda8017d 2020-07-23 stsp goto done;
7461 fda8017d 2020-07-23 stsp link_target[r] = '\0';
7462 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
7463 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
7464 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
7465 fda8017d 2020-07-23 stsp progress_arg);
7467 fda8017d 2020-07-23 stsp int local_changes_subsumed;
7468 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
7469 fda8017d 2020-07-23 stsp blob_base, ondisk_path, ie->path,
7470 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
7471 fda8017d 2020-07-23 stsp path_unstaged_content, label_orig, "unstaged",
7472 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
7475 fda8017d 2020-07-23 stsp goto done;
7477 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
7478 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7479 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
7481 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7483 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
7484 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
7485 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
7486 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
7487 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
7488 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
7489 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
7490 fda8017d 2020-07-23 stsp if (f && fclose(f) != 0 && err == NULL)
7491 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
7492 fda8017d 2020-07-23 stsp free(path_unstaged_content);
7493 fda8017d 2020-07-23 stsp free(path_new_staged_content);
7494 2e1f37b0 2019-08-08 stsp return err;
7497 ad493afc 2019-08-03 stsp static const struct got_error *
7498 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
7499 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
7500 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7501 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7503 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
7504 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
7505 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
7506 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7507 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
7508 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
7509 ad493afc 2019-08-03 stsp int local_changes_subsumed;
7510 9bc94a15 2019-08-03 stsp struct stat sb;
7512 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
7513 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
7514 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
7515 2e1f37b0 2019-08-08 stsp return NULL;
7517 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7518 ad493afc 2019-08-03 stsp if (ie == NULL)
7519 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7521 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7523 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
7525 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
7526 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
7528 f69721c3 2019-10-21 stsp goto done;
7529 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7530 f69721c3 2019-10-21 stsp id_str) == -1) {
7531 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
7532 f69721c3 2019-10-21 stsp goto done;
7535 ad493afc 2019-08-03 stsp switch (staged_status) {
7536 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
7537 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
7538 ad493afc 2019-08-03 stsp blob_id, 8192);
7541 ad493afc 2019-08-03 stsp /* fall through */
7542 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
7543 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7544 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
7545 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7546 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7547 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7550 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
7553 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
7554 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
7555 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
7556 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
7557 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
7558 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
7561 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
7562 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
7565 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
7566 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
7567 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
7568 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
7569 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
7570 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
7571 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
7572 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
7573 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
7575 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
7576 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7577 36bf999c 2020-07-23 stsp char *staged_target;
7578 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
7579 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
7581 36bf999c 2020-07-23 stsp goto done;
7582 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
7583 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
7584 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
7585 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
7586 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
7587 36bf999c 2020-07-23 stsp free(staged_target);
7589 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
7590 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
7591 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
7592 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
7593 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
7594 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
7595 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
7599 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7602 ad493afc 2019-08-03 stsp if (err == NULL)
7603 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
7604 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
7606 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
7607 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7608 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7609 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7610 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7613 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7615 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7616 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7620 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7621 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
7622 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
7625 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
7629 ad493afc 2019-08-03 stsp free(ondisk_path);
7630 ad493afc 2019-08-03 stsp if (blob_base)
7631 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
7632 ad493afc 2019-08-03 stsp if (blob_staged)
7633 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
7634 f69721c3 2019-10-21 stsp free(id_str);
7635 f69721c3 2019-10-21 stsp free(label_orig);
7636 ad493afc 2019-08-03 stsp return err;
7639 ad493afc 2019-08-03 stsp const struct got_error *
7640 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
7641 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
7642 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7643 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7644 ad493afc 2019-08-03 stsp struct got_repository *repo)
7646 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7647 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
7648 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7649 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
7650 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
7652 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7654 ad493afc 2019-08-03 stsp return err;
7656 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7658 ad493afc 2019-08-03 stsp goto done;
7660 ad493afc 2019-08-03 stsp upa.worktree = worktree;
7661 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
7662 ad493afc 2019-08-03 stsp upa.repo = repo;
7663 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
7664 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
7665 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
7666 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
7667 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7668 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7669 f2a9dc41 2019-12-13 tracey unstage_path, &upa, NULL, NULL, 0, 0);
7671 ad493afc 2019-08-03 stsp goto done;
7674 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7675 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
7676 ad493afc 2019-08-03 stsp err = sync_err;
7678 ad493afc 2019-08-03 stsp free(fileindex_path);
7679 ad493afc 2019-08-03 stsp if (fileindex)
7680 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
7681 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7682 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
7683 ad493afc 2019-08-03 stsp err = unlockerr;
7684 ad493afc 2019-08-03 stsp return err;
7687 b2118c49 2020-07-28 stsp struct report_file_info_arg {
7688 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
7689 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
7690 b2118c49 2020-07-28 stsp void *info_arg;
7691 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
7692 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
7693 b2118c49 2020-07-28 stsp void *cancel_arg;
7696 b2118c49 2020-07-28 stsp static const struct got_error *
7697 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
7699 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
7700 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
7701 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
7702 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7703 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
7704 b2118c49 2020-07-28 stsp int stage;
7706 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7707 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
7709 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
7710 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7711 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
7714 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
7715 b2118c49 2020-07-28 stsp return NULL;
7717 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_blob(ie)) {
7718 b2118c49 2020-07-28 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7719 b2118c49 2020-07-28 stsp blob_idp = &blob_id;
7721 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
7722 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7723 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
7724 b2118c49 2020-07-28 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7725 b2118c49 2020-07-28 stsp SHA1_DIGEST_LENGTH);
7726 b2118c49 2020-07-28 stsp staged_blob_idp = &staged_blob_id;
7729 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_commit(ie)) {
7730 b2118c49 2020-07-28 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
7731 b2118c49 2020-07-28 stsp commit_idp = &commit_id;
7734 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
7735 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
7738 b2118c49 2020-07-28 stsp const struct got_error *
7739 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
7740 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
7741 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
7742 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7745 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
7746 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
7747 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
7748 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
7750 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
7752 b2118c49 2020-07-28 stsp return err;
7754 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7756 b2118c49 2020-07-28 stsp goto done;
7758 b2118c49 2020-07-28 stsp arg.worktree = worktree;
7759 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
7760 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
7761 b2118c49 2020-07-28 stsp arg.paths = paths;
7762 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
7763 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
7764 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
7767 b2118c49 2020-07-28 stsp free(fileindex_path);
7768 b2118c49 2020-07-28 stsp if (fileindex)
7769 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
7770 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
7771 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
7772 b2118c49 2020-07-28 stsp err = unlockerr;
7773 b2118c49 2020-07-28 stsp return err;