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;
746 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
748 af54ae4a 2019-02-19 stsp parent = dirname(ondisk_path);
749 af54ae4a 2019-02-19 stsp if (parent == NULL)
750 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", ondisk_path);
752 af54ae4a 2019-02-19 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1)
753 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
755 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
759 af54ae4a 2019-02-19 stsp free(base_path);
760 46b6ee73 2019-06-04 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
761 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
762 af54ae4a 2019-02-19 stsp base_path = NULL;
766 46b6ee73 2019-06-04 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
769 46b6ee73 2019-06-04 stsp if (blob_orig) {
770 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
771 46b6ee73 2019-06-04 stsp blob_orig);
776 1430b4e0 2019-03-27 stsp * If the file has no blob, this is an "add vs add" conflict,
777 1430b4e0 2019-03-27 stsp * and we simply use an empty ancestor file to make both files
778 1430b4e0 2019-03-27 stsp * appear in the merged result in their entirety.
782 14c901f1 2019-08-08 stsp err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
783 f69721c3 2019-10-21 stsp blob_orig_path, ondisk_path, label_deriv, label_orig, NULL);
787 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
788 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
792 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
793 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
797 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
798 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
799 14c901f1 2019-08-08 stsp err = check_files_equal(local_changes_subsumed, deriv_path,
800 68c76935 2019-02-19 stsp merged_path);
805 2ad902c0 2019-12-15 stsp if (fchmod(merged_fd, st_mode) != 0) {
806 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
810 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
811 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
812 230a42bd 2019-05-11 jcs ondisk_path);
817 fdcb7daf 2019-12-15 stsp if (merged_path)
818 fdcb7daf 2019-12-15 stsp unlink(merged_path);
820 3a6ce05a 2019-02-11 stsp if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
821 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
822 46b6ee73 2019-06-04 stsp if (f_orig && fclose(f_orig) != 0 && err == NULL)
823 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
824 6353ad76 2019-02-08 stsp free(merged_path);
825 af54ae4a 2019-02-19 stsp free(base_path);
826 46b6ee73 2019-06-04 stsp if (blob_orig_path) {
827 46b6ee73 2019-06-04 stsp unlink(blob_orig_path);
828 46b6ee73 2019-06-04 stsp free(blob_orig_path);
830 af57b12a 2020-07-23 stsp return err;
833 af57b12a 2020-07-23 stsp static const struct got_error *
834 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
835 af57b12a 2020-07-23 stsp size_t target_len)
837 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
838 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
839 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
840 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
841 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
842 af57b12a 2020-07-23 stsp ondisk_path);
843 af57b12a 2020-07-23 stsp return NULL;
847 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
848 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
849 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
850 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
852 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
853 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
855 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
856 993e2a1b 2020-07-23 stsp * has deleted this symlink.
858 11cc08c1 2020-07-23 stsp static const struct got_error *
859 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
860 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
861 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
863 11cc08c1 2020-07-23 stsp const struct got_error *err;
864 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
865 11cc08c1 2020-07-23 stsp FILE *f = NULL;
867 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
869 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
871 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
872 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
873 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
877 11cc08c1 2020-07-23 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict");
881 11cc08c1 2020-07-23 stsp if (fprintf(f, "%s: Could not install symbolic link because of merge "
882 11cc08c1 2020-07-23 stsp "conflict.\nln(1) may be used to fix the situation. If this is "
883 11cc08c1 2020-07-23 stsp "intended to be a\nregular file instead then its expected "
884 11cc08c1 2020-07-23 stsp "contents may be filled in.\nThe following conflicting symlink "
885 11cc08c1 2020-07-23 stsp "target paths were found:\n"
886 11cc08c1 2020-07-23 stsp "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n", getprogname(),
887 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
888 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
889 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
890 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
891 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
892 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
893 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
894 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
895 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
899 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
900 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
903 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
904 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
907 11cc08c1 2020-07-23 stsp if (chmod(ondisk_path, GOT_DEFAULT_FILE_MODE) == -1) {
908 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("chmod", ondisk_path);
912 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
913 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
914 11cc08c1 2020-07-23 stsp free(path);
915 11cc08c1 2020-07-23 stsp free(id_str);
916 11cc08c1 2020-07-23 stsp free(label_deriv);
917 11cc08c1 2020-07-23 stsp return err;
920 d219f183 2020-07-23 stsp /* forward declaration */
921 d219f183 2020-07-23 stsp static const struct got_error *
922 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
923 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
924 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
925 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
928 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
929 af57b12a 2020-07-23 stsp * ancestor, blob_deriv acts as the first derived version, and the symlink
930 af57b12a 2020-07-23 stsp * on disk acts as the second derived version.
931 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
933 af57b12a 2020-07-23 stsp static const struct got_error *
934 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
935 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
936 af57b12a 2020-07-23 stsp const char *path, uint16_t st_mode, const char *label_orig,
937 af57b12a 2020-07-23 stsp struct got_blob_object *blob_deriv,
938 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
939 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
941 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
942 af57b12a 2020-07-23 stsp char *ancestor_target = NULL, *deriv_target = NULL;
943 af57b12a 2020-07-23 stsp struct stat sb;
944 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
945 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
946 11cc08c1 2020-07-23 stsp int have_local_change = 0;
947 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
949 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
950 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
952 af57b12a 2020-07-23 stsp if (!S_ISLNK(sb.st_mode)) {
954 d219f183 2020-07-23 stsp * If there is a regular file on disk, merge the symlink
955 d219f183 2020-07-23 stsp * target path into this file, which will usually cause
956 d219f183 2020-07-23 stsp * a merge conflict.
958 d219f183 2020-07-23 stsp if (S_ISREG(sb.st_mode)) {
959 d219f183 2020-07-23 stsp int local_changes_subsumed;
960 d219f183 2020-07-23 stsp return merge_blob(&local_changes_subsumed, worktree,
961 d219f183 2020-07-23 stsp NULL, ondisk_path, path, sb.st_mode, label_orig,
962 d219f183 2020-07-23 stsp blob_deriv, deriv_base_commit_id,
963 d219f183 2020-07-23 stsp repo, progress_cb, progress_arg);
966 af57b12a 2020-07-23 stsp /* TODO symlink is obstructed; do something */
967 af57b12a 2020-07-23 stsp return got_error_path(ondisk_path, GOT_ERR_FILE_OBSTRUCTED);
970 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
971 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
972 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
973 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
974 af57b12a 2020-07-23 stsp ondisk_path);
977 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
979 526a746f 2020-07-23 stsp if (blob_orig) {
980 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
985 af57b12a 2020-07-23 stsp err = got_object_blob_read_to_str(&deriv_target, blob_deriv);
989 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
990 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
991 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
992 11cc08c1 2020-07-23 stsp have_local_change = 1;
994 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
995 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
996 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
997 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
998 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
1000 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
1001 11cc08c1 2020-07-23 stsp if (ancestor_target) {
1002 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
1003 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1005 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
1006 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1007 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
1008 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1011 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
1012 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1013 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
1014 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
1016 11cc08c1 2020-07-23 stsp goto done;
1017 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1020 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
1021 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
1022 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
1023 af57b12a 2020-07-23 stsp strlen(deriv_target));
1025 af57b12a 2020-07-23 stsp goto done;
1026 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1027 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
1028 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1029 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
1030 11cc08c1 2020-07-23 stsp ondisk_target, ondisk_path);
1032 11cc08c1 2020-07-23 stsp goto done;
1033 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1038 af57b12a 2020-07-23 stsp free(ancestor_target);
1039 af57b12a 2020-07-23 stsp free(deriv_target);
1040 14c901f1 2019-08-08 stsp return err;
1044 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
1045 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
1046 14c901f1 2019-08-08 stsp * acts as the second derived version.
1048 14c901f1 2019-08-08 stsp static const struct got_error *
1049 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1050 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
1051 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
1052 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
1053 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1054 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1056 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
1057 14c901f1 2019-08-08 stsp FILE *f_deriv = NULL;
1058 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1059 14c901f1 2019-08-08 stsp char *label_deriv = NULL, *parent;
1061 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
1063 14c901f1 2019-08-08 stsp parent = dirname(ondisk_path);
1064 14c901f1 2019-08-08 stsp if (parent == NULL)
1065 14c901f1 2019-08-08 stsp return got_error_from_errno2("dirname", ondisk_path);
1067 14c901f1 2019-08-08 stsp free(base_path);
1068 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1069 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1070 14c901f1 2019-08-08 stsp base_path = NULL;
1071 14c901f1 2019-08-08 stsp goto done;
1074 14c901f1 2019-08-08 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1076 14c901f1 2019-08-08 stsp goto done;
1077 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1078 14c901f1 2019-08-08 stsp blob_deriv);
1080 14c901f1 2019-08-08 stsp goto done;
1082 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1084 14c901f1 2019-08-08 stsp goto done;
1085 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1086 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1087 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1088 14c901f1 2019-08-08 stsp goto done;
1091 14c901f1 2019-08-08 stsp err = merge_file(local_changes_subsumed, worktree, blob_orig,
1092 f69721c3 2019-10-21 stsp ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1093 f69721c3 2019-10-21 stsp label_deriv, repo, progress_cb, progress_arg);
1095 14c901f1 2019-08-08 stsp if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1096 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1097 14c901f1 2019-08-08 stsp free(base_path);
1098 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1099 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1100 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1102 6353ad76 2019-02-08 stsp free(id_str);
1103 818c7501 2019-07-11 stsp free(label_deriv);
1104 4a1ddfc2 2019-01-12 stsp return err;
1107 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1108 054041d0 2020-06-24 stsp create_fileindex_entry(struct got_fileindex *fileindex,
1109 054041d0 2020-06-24 stsp struct got_object_id *base_commit_id, const char *ondisk_path,
1110 054041d0 2020-06-24 stsp const char *path, struct got_object_id *blob_id)
1112 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1113 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1115 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1117 054041d0 2020-06-24 stsp return err;
1119 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(new_ie, ondisk_path,
1120 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1122 054041d0 2020-06-24 stsp goto done;
1124 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1127 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1128 13d9040b 2019-03-27 stsp return err;
1131 1ebedb77 2019-10-19 stsp static mode_t
1132 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1134 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1136 1ebedb77 2019-10-19 stsp if (executable) {
1137 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1138 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1139 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1140 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1141 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1142 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1145 1ebedb77 2019-10-19 stsp return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1148 8ba819a3 2020-07-23 stsp /* forward declaration */
1149 13d9040b 2019-03-27 stsp static const struct got_error *
1150 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1151 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1152 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1153 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1154 bd6aa359 2020-07-23 stsp struct got_repository *repo,
1155 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1158 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1159 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1161 8ba819a3 2020-07-23 stsp static const struct got_error *
1162 5a1fbc73 2020-07-23 stsp replace_existing_symlink(const char *ondisk_path, const char *target_path,
1163 5a1fbc73 2020-07-23 stsp size_t target_len)
1165 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1166 5a1fbc73 2020-07-23 stsp ssize_t elen;
1167 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1171 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1172 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1173 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1174 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1175 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1176 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1178 5a1fbc73 2020-07-23 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1179 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1180 5a1fbc73 2020-07-23 stsp if (errno != ELOOP)
1181 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1183 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1184 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1185 5a1fbc73 2020-07-23 stsp if (elen == -1)
1186 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1188 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1189 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1190 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1193 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1194 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1195 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1196 5a1fbc73 2020-07-23 stsp return err;
1199 5a1fbc73 2020-07-23 stsp static const struct got_error *
1200 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1201 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1202 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1203 2e1fa222 2020-07-23 stsp struct got_repository *repo,
1204 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1206 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1207 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1208 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1209 8ba819a3 2020-07-23 stsp char *resolved_path = NULL, *abspath = NULL;
1210 906c123b 2020-07-23 stsp char *path_got = NULL;
1211 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1212 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1214 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1217 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1218 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1219 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1220 8ba819a3 2020-07-23 stsp * in the blob object.
1223 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1224 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1225 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1226 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1227 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1228 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1229 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1230 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1231 bd6aa359 2020-07-23 stsp 1, repo, progress_cb, progress_arg);
1233 8ba819a3 2020-07-23 stsp if (len > 0) {
1234 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1235 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1236 8ba819a3 2020-07-23 stsp len - hdrlen);
1237 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1238 8ba819a3 2020-07-23 stsp hdrlen = 0;
1240 8ba819a3 2020-07-23 stsp } while (len != 0);
1241 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1244 8ba819a3 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1245 8ba819a3 2020-07-23 stsp * in which the blob object is being installed.
1247 8ba819a3 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1248 8ba819a3 2020-07-23 stsp char *parent = dirname(ondisk_path);
1249 ef68ca6f 2020-07-23 stsp if (parent == NULL) {
1250 ef68ca6f 2020-07-23 stsp err = got_error_from_errno2("dirname", ondisk_path);
1251 ef68ca6f 2020-07-23 stsp goto done;
1253 8ba819a3 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1254 8ba819a3 2020-07-23 stsp err = got_error_from_errno("asprintf");
1255 8ba819a3 2020-07-23 stsp goto done;
1260 8ba819a3 2020-07-23 stsp * unveil(2) restricts our view of paths in the filesystem.
1261 8ba819a3 2020-07-23 stsp * ENOENT will occur if a link target path does not exist or
1262 8ba819a3 2020-07-23 stsp * if it points outside our unveiled path space.
1264 8ba819a3 2020-07-23 stsp resolved_path = realpath(abspath ? abspath : target_path, NULL);
1265 8ba819a3 2020-07-23 stsp if (resolved_path == NULL) {
1266 b15bc87b 2020-07-23 stsp if (errno != ENOENT) {
1267 b15bc87b 2020-07-23 stsp err = got_error_from_errno2("realpath", target_path);
1268 b15bc87b 2020-07-23 stsp goto done;
1272 8ba819a3 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1273 0ab20ee9 2020-07-23 stsp if (!got_path_is_child(resolved_path ? resolved_path : (abspath ?
1274 0ab20ee9 2020-07-23 stsp abspath : target_path), worktree->root_path,
1275 0ab20ee9 2020-07-23 stsp strlen(worktree->root_path))) {
1276 8ba819a3 2020-07-23 stsp /* install as a regular file */
1277 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1278 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1279 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1280 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1281 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1282 8ba819a3 2020-07-23 stsp repo, progress_cb, progress_arg);
1283 8ba819a3 2020-07-23 stsp goto done;
1286 906c123b 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1287 906c123b 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
1288 906c123b 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1) {
1289 906c123b 2020-07-23 stsp err = got_error_from_errno("asprintf");
1290 906c123b 2020-07-23 stsp goto done;
1292 906c123b 2020-07-23 stsp if (got_path_is_child(resolved_path ? resolved_path : (abspath ?
1293 906c123b 2020-07-23 stsp abspath : target_path), path_got, strlen(path_got))) {
1294 906c123b 2020-07-23 stsp /* install as a regular file */
1295 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1296 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1297 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1298 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1299 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1300 906c123b 2020-07-23 stsp repo, progress_cb, progress_arg);
1301 906c123b 2020-07-23 stsp goto done;
1304 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1305 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1306 5a1fbc73 2020-07-23 stsp err = replace_existing_symlink(ondisk_path,
1307 5a1fbc73 2020-07-23 stsp target_path, target_len);
1309 f35fa46a 2020-07-23 stsp goto done;
1310 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1311 5a1fbc73 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1312 5a1fbc73 2020-07-23 stsp GOT_STATUS_UPDATE, path);
1314 5a1fbc73 2020-07-23 stsp goto done; /* Nothing else to do. */
1317 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1318 f35fa46a 2020-07-23 stsp char *parent = dirname(ondisk_path);
1319 f35fa46a 2020-07-23 stsp if (parent == NULL) {
1320 f35fa46a 2020-07-23 stsp err = got_error_from_errno2("dirname",
1321 f35fa46a 2020-07-23 stsp ondisk_path);
1322 f35fa46a 2020-07-23 stsp goto done;
1324 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1326 f35fa46a 2020-07-23 stsp goto done;
1328 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1329 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1331 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1332 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1333 f35fa46a 2020-07-23 stsp goto done;
1337 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1338 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1339 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1340 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1341 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1342 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1343 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1344 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1345 8ba819a3 2020-07-23 stsp repo, progress_cb, progress_arg);
1346 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1347 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1348 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1350 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1351 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1353 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1354 f35fa46a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_ADD, path);
1356 8ba819a3 2020-07-23 stsp free(resolved_path);
1357 8ba819a3 2020-07-23 stsp free(abspath);
1358 906c123b 2020-07-23 stsp free(path_got);
1359 8ba819a3 2020-07-23 stsp return err;
1362 8ba819a3 2020-07-23 stsp static const struct got_error *
1363 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1364 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1365 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1366 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1367 bd6aa359 2020-07-23 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1368 bd6aa359 2020-07-23 stsp void *progress_arg)
1370 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1371 507dc3bb 2018-12-29 stsp int fd = -1;
1372 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1373 507dc3bb 2018-12-29 stsp int update = 0;
1374 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1376 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1377 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
1378 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1379 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1380 21908da4 2019-01-13 stsp char *parent = dirname(path);
1381 21908da4 2019-01-13 stsp if (parent == NULL)
1382 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
1383 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1385 21908da4 2019-01-13 stsp return err;
1386 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1387 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1388 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
1389 21908da4 2019-01-13 stsp if (fd == -1)
1390 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1391 230a42bd 2019-05-11 jcs ondisk_path);
1392 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1393 bd6aa359 2020-07-23 stsp if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1394 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1395 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1396 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1397 507dc3bb 2018-12-29 stsp goto done;
1399 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1400 507dc3bb 2018-12-29 stsp ondisk_path);
1402 507dc3bb 2018-12-29 stsp goto done;
1403 507dc3bb 2018-12-29 stsp update = 1;
1406 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1409 bd6aa359 2020-07-23 stsp if (progress_cb) {
1410 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1411 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1413 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1414 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1417 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1418 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1420 bd6aa359 2020-07-23 stsp goto done;
1423 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1425 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1426 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1429 9d31a1d8 2018-03-11 stsp if (len > 0) {
1430 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1431 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1432 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1433 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1434 b87c6f83 2018-12-24 stsp goto done;
1435 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1436 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1437 b87c6f83 2018-12-24 stsp goto done;
1439 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1441 9d31a1d8 2018-03-11 stsp } while (len != 0);
1443 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1444 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1445 816dc654 2019-02-16 stsp goto done;
1448 507dc3bb 2018-12-29 stsp if (update) {
1449 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1450 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1451 230a42bd 2019-05-11 jcs ondisk_path);
1452 2a57020b 2019-02-20 stsp unlink(tmppath);
1453 68ed9ba5 2019-02-10 stsp goto done;
1457 1ebedb77 2019-10-19 stsp if (chmod(ondisk_path,
1458 1ebedb77 2019-10-19 stsp get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1459 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("chmod", ondisk_path);
1460 1ebedb77 2019-10-19 stsp goto done;
1464 3a6ce05a 2019-02-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
1465 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1466 507dc3bb 2018-12-29 stsp free(tmppath);
1467 6353ad76 2019-02-08 stsp return err;
1470 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1471 6353ad76 2019-02-08 stsp static const struct got_error *
1472 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1474 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1475 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1476 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1477 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1478 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1480 7154f6ce 2019-03-27 stsp int i = 0;
1481 7154f6ce 2019-03-27 stsp char *line;
1482 7154f6ce 2019-03-27 stsp size_t len;
1483 7154f6ce 2019-03-27 stsp const char delim[3] = {'\0', '\0', '\0'};
1485 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1486 7154f6ce 2019-03-27 stsp line = fparseln(f, &len, NULL, delim, 0);
1487 7154f6ce 2019-03-27 stsp if (line == NULL) {
1488 7154f6ce 2019-03-27 stsp if (feof(f))
1490 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1494 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1495 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1497 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1503 7154f6ce 2019-03-27 stsp return err;
1506 e2b1e152 2019-07-13 stsp static int
1507 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1509 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1510 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1513 1ebedb77 2019-10-19 stsp static int
1514 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1516 e2b1e152 2019-07-13 stsp return !(ie->ctime_sec == sb->st_ctime &&
1517 e2b1e152 2019-07-13 stsp ie->ctime_nsec == sb->st_ctimensec &&
1518 e2b1e152 2019-07-13 stsp ie->mtime_sec == sb->st_mtime &&
1519 e2b1e152 2019-07-13 stsp ie->mtime_nsec == sb->st_mtimensec &&
1520 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1521 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1524 c363b2c1 2019-08-03 stsp static unsigned char
1525 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1527 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1528 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1529 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1530 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1531 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1532 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1533 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1535 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1539 a919d5c4 2020-07-23 stsp static const struct got_error *
1540 a919d5c4 2020-07-23 stsp get_symlink_status(unsigned char *status, struct stat *sb,
1541 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1542 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1544 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1545 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1546 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1547 a919d5c4 2020-07-23 stsp ssize_t elen;
1548 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1549 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1550 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1552 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1554 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1556 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1558 a919d5c4 2020-07-23 stsp return err;
1559 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1561 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1562 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1564 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1566 a919d5c4 2020-07-23 stsp if (len > 0) {
1567 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1568 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1569 a919d5c4 2020-07-23 stsp len - hdrlen);
1570 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1571 a919d5c4 2020-07-23 stsp hdrlen = 0;
1573 a919d5c4 2020-07-23 stsp } while (len != 0);
1574 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1576 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1577 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1578 a919d5c4 2020-07-23 stsp if (elen == -1)
1579 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1581 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1582 a919d5c4 2020-07-23 stsp if (elen == -1)
1583 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1586 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1587 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1589 a919d5c4 2020-07-23 stsp return NULL;
1592 7154f6ce 2019-03-27 stsp static const struct got_error *
1593 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1594 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1595 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1597 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1598 6353ad76 2019-02-08 stsp struct got_object_id id;
1599 6353ad76 2019-02-08 stsp size_t hdrlen;
1600 1338848f 2019-12-13 stsp int fd = -1;
1601 6353ad76 2019-02-08 stsp FILE *f = NULL;
1602 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1603 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1604 6353ad76 2019-02-08 stsp size_t flen, blen;
1605 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1607 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1610 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1611 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1612 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1614 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1615 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1616 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1617 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1618 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1620 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1621 882ef1b9 2019-12-13 stsp goto done;
1623 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1624 3d35a492 2019-12-13 stsp goto done;
1627 7f91a133 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1628 a919d5c4 2020-07-23 stsp if (fd == -1 && errno != ENOENT && errno != ELOOP)
1629 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1630 a919d5c4 2020-07-23 stsp else if (fd == -1 && errno == ELOOP) {
1631 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1632 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1633 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1634 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1635 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1636 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1638 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1639 3d35a492 2019-12-13 stsp goto done;
1641 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1642 1338848f 2019-12-13 stsp goto done;
1646 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1647 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1648 1338848f 2019-12-13 stsp goto done;
1651 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1652 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1653 1338848f 2019-12-13 stsp goto done;
1654 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1655 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1656 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1657 1338848f 2019-12-13 stsp goto done;
1660 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1661 1338848f 2019-12-13 stsp goto done;
1663 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1664 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1665 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1667 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1669 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1671 1338848f 2019-12-13 stsp goto done;
1673 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1674 a919d5c4 2020-07-23 stsp /* Staging changes to symlinks is not yet(?) supported. */
1675 a919d5c4 2020-07-23 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
1676 a919d5c4 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_FILE_STATUS);
1677 a919d5c4 2020-07-23 stsp goto done;
1679 a919d5c4 2020-07-23 stsp err = get_symlink_status(status, sb, ie, abspath, dirfd,
1680 a919d5c4 2020-07-23 stsp de_name, blob);
1681 a919d5c4 2020-07-23 stsp goto done;
1685 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1686 3d35a492 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1687 ab0d4361 2019-12-13 stsp if (fd == -1) {
1688 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1689 ab0d4361 2019-12-13 stsp goto done;
1693 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1694 6353ad76 2019-02-08 stsp if (f == NULL) {
1695 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1696 6353ad76 2019-02-08 stsp goto done;
1699 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1701 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1702 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1704 7154f6ce 2019-03-27 stsp goto done;
1705 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1706 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1707 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1708 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1709 7154f6ce 2019-03-27 stsp goto done;
1711 6353ad76 2019-02-08 stsp if (blen == 0) {
1712 6353ad76 2019-02-08 stsp if (flen != 0)
1713 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1715 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1716 6353ad76 2019-02-08 stsp if (blen != 0)
1717 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1719 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1720 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1721 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1722 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1726 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1729 6353ad76 2019-02-08 stsp hdrlen = 0;
1732 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1733 7154f6ce 2019-03-27 stsp rewind(f);
1734 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1735 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1736 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1739 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1740 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1741 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1742 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1743 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1744 9d31a1d8 2018-03-11 stsp return err;
1748 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1749 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1751 e2b1e152 2019-07-13 stsp static const struct got_error *
1752 e2b1e152 2019-07-13 stsp sync_timestamps(char *ondisk_path, unsigned char status,
1753 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1755 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1756 e2b1e152 2019-07-13 stsp return got_fileindex_entry_update(ie, ondisk_path,
1757 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1759 e2b1e152 2019-07-13 stsp return NULL;
1762 9d31a1d8 2018-03-11 stsp static const struct got_error *
1763 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1764 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1765 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1766 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1767 0584f854 2019-04-06 stsp void *progress_arg)
1769 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1770 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1771 6353ad76 2019-02-08 stsp char *ondisk_path;
1772 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1773 b8f41171 2019-02-10 stsp struct stat sb;
1775 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1776 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1779 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1780 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1781 a76c42e6 2019-08-03 stsp goto done;
1783 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1786 abb4604f 2019-07-27 stsp goto done;
1787 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1788 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1790 abb4604f 2019-07-27 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1792 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1793 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1794 b8f41171 2019-02-10 stsp goto done;
1796 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1797 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1799 5036ab18 2020-04-18 stsp goto done;
1802 523b8417 2019-10-19 stsp if (ie && status != GOT_STATUS_MISSING &&
1803 523b8417 2019-10-19 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1804 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1805 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1806 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1807 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1809 e2b1e152 2019-07-13 stsp goto done;
1810 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1812 6353ad76 2019-02-08 stsp goto done;
1814 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1815 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1816 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1817 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1818 b8f41171 2019-02-10 stsp goto done;
1822 56e0773d 2019-11-28 stsp err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1824 6353ad76 2019-02-08 stsp goto done;
1826 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1827 234035bc 2019-06-01 stsp int update_timestamps;
1828 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1829 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1830 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1831 234035bc 2019-06-01 stsp struct got_object_id id2;
1832 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1833 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1835 f69721c3 2019-10-21 stsp goto done;
1837 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
1838 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
1839 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1840 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
1841 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
1842 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
1843 f69721c3 2019-10-21 stsp goto done;
1845 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
1846 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
1847 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
1848 234035bc 2019-06-01 stsp goto done;
1851 993e2a1b 2020-07-23 stsp if (S_ISLNK(te->mode)) {
1852 993e2a1b 2020-07-23 stsp err = merge_symlink(worktree, blob2,
1853 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig,
1854 993e2a1b 2020-07-23 stsp blob, worktree->base_commit_id, repo,
1855 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
1857 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1858 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
1859 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
1860 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
1862 f69721c3 2019-10-21 stsp free(label_orig);
1863 234035bc 2019-06-01 stsp if (blob2)
1864 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1866 909d120e 2019-09-22 stsp goto done;
1868 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1869 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1870 234035bc 2019-06-01 stsp * unmodified files again.
1872 234035bc 2019-06-01 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1873 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1874 234035bc 2019-06-01 stsp update_timestamps);
1875 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
1876 1ebedb77 2019-10-19 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1877 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1878 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1879 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1881 1ee397ad 2019-07-12 stsp goto done;
1882 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1883 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1885 13d9040b 2019-03-27 stsp goto done;
1887 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
1888 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
1889 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
1890 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
1891 2e1fa222 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, repo,
1892 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
1894 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1895 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
1896 2e1fa222 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0, repo,
1897 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
1900 13d9040b 2019-03-27 stsp goto done;
1902 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1903 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 1);
1905 054041d0 2020-06-24 stsp err = create_fileindex_entry(fileindex,
1906 054041d0 2020-06-24 stsp worktree->base_commit_id, ondisk_path, path,
1907 054041d0 2020-06-24 stsp &blob->id);
1910 13d9040b 2019-03-27 stsp goto done;
1911 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
1912 2e1fa222 2020-07-23 stsp if (ie == NULL)
1913 2e1fa222 2020-07-23 stsp ie = got_fileindex_entry_get(fileindex, path,
1914 2e1fa222 2020-07-23 stsp strlen(path));
1915 2e1fa222 2020-07-23 stsp err = got_fileindex_entry_filetype_set(ie,
1916 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
1918 2e1fa222 2020-07-23 stsp goto done;
1921 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
1923 6353ad76 2019-02-08 stsp free(ondisk_path);
1924 8da9e5f4 2019-01-12 stsp return err;
1927 90285c3b 2019-01-08 stsp static const struct got_error *
1928 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
1930 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
1931 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
1933 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1934 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1936 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
1937 c97665ae 2019-01-13 stsp if (errno != ENOENT)
1938 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
1940 90285c3b 2019-01-08 stsp char *parent = dirname(ondisk_path);
1941 7a9df742 2019-01-08 stsp while (parent && strcmp(parent, root_path) != 0) {
1942 26c4ac4d 2019-01-08 stsp if (rmdir(parent) == -1) {
1943 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
1944 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
1948 90285c3b 2019-01-08 stsp parent = dirname(parent);
1951 90285c3b 2019-01-08 stsp free(ondisk_path);
1952 90285c3b 2019-01-08 stsp return err;
1955 708d8e67 2019-03-27 stsp static const struct got_error *
1956 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1957 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
1958 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1960 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
1961 708d8e67 2019-03-27 stsp unsigned char status;
1962 708d8e67 2019-03-27 stsp struct stat sb;
1963 708d8e67 2019-03-27 stsp char *ondisk_path;
1965 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1966 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1968 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1970 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1972 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1974 5a58a424 2020-06-23 stsp goto done;
1976 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
1977 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
1978 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
1979 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
1980 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
1981 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
1982 993e2a1b 2020-07-23 stsp goto done;
1984 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
1985 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
1986 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
1987 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
1989 993e2a1b 2020-07-23 stsp goto done;
1990 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1991 993e2a1b 2020-07-23 stsp ie->path);
1992 993e2a1b 2020-07-23 stsp goto done;
1995 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1996 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
1997 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1999 5a58a424 2020-06-23 stsp goto done;
2001 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2002 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2004 fc6346c4 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2007 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2009 5a58a424 2020-06-23 stsp goto done;
2010 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2011 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2013 5a58a424 2020-06-23 stsp goto done;
2015 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2018 5a58a424 2020-06-23 stsp free(ondisk_path);
2019 fc6346c4 2019-03-27 stsp return err;
2022 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2023 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2024 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2025 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2026 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2027 8da9e5f4 2019-01-12 stsp void *progress_arg;
2028 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2029 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2032 512f0d0e 2019-01-02 stsp static const struct got_error *
2033 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2034 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2036 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2038 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2039 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2041 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2042 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2045 8da9e5f4 2019-01-12 stsp static const struct got_error *
2046 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2048 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2050 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2051 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2053 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2054 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2057 9d31a1d8 2018-03-11 stsp static const struct got_error *
2058 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2060 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2061 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2062 8da9e5f4 2019-01-12 stsp char *path;
2064 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2065 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2067 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2068 63c5ca5d 2019-08-24 stsp return NULL;
2070 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2071 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2073 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2075 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2076 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2078 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2079 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2081 8da9e5f4 2019-01-12 stsp free(path);
2082 0cd1c46a 2019-03-11 stsp return err;
2085 818c7501 2019-07-11 stsp static const struct got_error *
2086 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2088 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2089 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2090 0cd1c46a 2019-03-11 stsp uint32_t uuid_status;
2092 517bab73 2019-03-11 stsp *refname = NULL;
2094 0cd1c46a 2019-03-11 stsp uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
2095 0cd1c46a 2019-03-11 stsp if (uuid_status != uuid_s_ok)
2096 cc483380 2019-09-01 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2098 818c7501 2019-07-11 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr)
2100 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2101 0647c563 2019-03-11 stsp *refname = NULL;
2103 517bab73 2019-03-11 stsp free(uuidstr);
2104 517bab73 2019-03-11 stsp return err;
2107 818c7501 2019-07-11 stsp const struct got_error *
2108 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2110 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2113 818c7501 2019-07-11 stsp static const struct got_error *
2114 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2116 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2117 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2120 818c7501 2019-07-11 stsp static const struct got_error *
2121 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2123 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2126 818c7501 2019-07-11 stsp static const struct got_error *
2127 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2129 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2130 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2133 818c7501 2019-07-11 stsp static const struct got_error *
2134 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2136 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2137 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2140 0ebf8283 2019-07-24 stsp static const struct got_error *
2141 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2143 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2144 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2147 0ebf8283 2019-07-24 stsp static const struct got_error *
2148 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2150 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2151 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2154 0ebf8283 2019-07-24 stsp static const struct got_error *
2155 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2157 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2158 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2161 0ebf8283 2019-07-24 stsp static const struct got_error *
2162 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2164 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2165 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2168 0ebf8283 2019-07-24 stsp const struct got_error *
2169 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2170 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2172 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2173 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2174 0ebf8283 2019-07-24 stsp *path = NULL;
2175 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2177 0ebf8283 2019-07-24 stsp return NULL;
2181 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2182 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2184 517bab73 2019-03-11 stsp static const struct got_error *
2185 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2187 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2188 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2189 517bab73 2019-03-11 stsp char *refname;
2191 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2193 517bab73 2019-03-11 stsp return err;
2195 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2197 0cd1c46a 2019-03-11 stsp goto done;
2199 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2201 0cd1c46a 2019-03-11 stsp free(refname);
2203 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2204 3e3a69f1 2019-07-25 stsp return err;
2207 3e3a69f1 2019-07-25 stsp static const struct got_error *
2208 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2210 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2212 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2213 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2214 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2215 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2217 9d31a1d8 2018-03-11 stsp return err;
2221 ebf99748 2019-05-09 stsp static const struct got_error *
2222 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2223 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2225 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2226 ebf99748 2019-05-09 stsp FILE *index = NULL;
2228 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2229 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2230 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2231 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2233 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2235 ebf99748 2019-05-09 stsp goto done;
2237 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
2238 ebf99748 2019-05-09 stsp if (index == NULL) {
2239 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2240 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2242 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2243 ebf99748 2019-05-09 stsp if (fclose(index) != 0 && err == NULL)
2244 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2247 ebf99748 2019-05-09 stsp if (err) {
2248 ebf99748 2019-05-09 stsp free(*fileindex_path);
2249 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2250 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2251 ebf99748 2019-05-09 stsp *fileindex = NULL;
2253 ebf99748 2019-05-09 stsp return err;
2256 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2257 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2258 c932eeeb 2019-05-22 stsp const char *path;
2259 c932eeeb 2019-05-22 stsp size_t path_len;
2260 c932eeeb 2019-05-22 stsp const char *entry_name;
2261 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2262 a484d721 2019-06-10 stsp void *progress_arg;
2265 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2266 c932eeeb 2019-05-22 stsp static const struct got_error *
2267 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2269 1ee397ad 2019-07-12 stsp const struct got_error *err;
2270 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2272 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2273 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2274 c932eeeb 2019-05-22 stsp return NULL;
2275 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2276 102ff934 2019-06-11 stsp return NULL;
2278 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2279 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2280 c932eeeb 2019-05-22 stsp return NULL;
2282 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2283 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2284 edd02c5e 2019-07-11 stsp ie->path);
2286 1ee397ad 2019-07-12 stsp return err;
2288 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2289 c932eeeb 2019-05-22 stsp return NULL;
2292 9c6338c4 2019-06-02 stsp static const struct got_error *
2293 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2295 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2296 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2297 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2298 867630bb 2020-01-17 stsp struct timespec timeout;
2300 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2301 9c6338c4 2019-06-02 stsp fileindex_path);
2303 9c6338c4 2019-06-02 stsp goto done;
2305 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2307 9c6338c4 2019-06-02 stsp goto done;
2309 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2310 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2311 9c6338c4 2019-06-02 stsp fileindex_path);
2312 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2316 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2317 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2318 867630bb 2020-01-17 stsp * was recorded in the file index.
2320 867630bb 2020-01-17 stsp timeout.tv_sec = 0;
2321 867630bb 2020-01-17 stsp timeout.tv_nsec = 1;
2322 867630bb 2020-01-17 stsp nanosleep(&timeout, NULL);
2324 9c6338c4 2019-06-02 stsp if (new_index)
2325 9c6338c4 2019-06-02 stsp fclose(new_index);
2326 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2327 9c6338c4 2019-06-02 stsp return err;
2330 6ced4176 2019-07-12 stsp static const struct got_error *
2331 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2332 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2333 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
2335 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2336 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2337 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2338 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2340 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2341 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2342 6ced4176 2019-07-12 stsp *tree_id = NULL;
2344 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2345 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2346 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2347 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2348 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2349 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2350 6ced4176 2019-07-12 stsp goto done;
2352 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2353 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
2355 6ced4176 2019-07-12 stsp goto done;
2356 6ced4176 2019-07-12 stsp return NULL;
2359 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2361 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2362 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2363 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2364 6ced4176 2019-07-12 stsp goto done;
2367 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2368 6ced4176 2019-07-12 stsp in_repo_path);
2370 6ced4176 2019-07-12 stsp goto done;
2372 6ced4176 2019-07-12 stsp free(in_repo_path);
2373 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2375 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2377 6ced4176 2019-07-12 stsp goto done;
2379 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2380 6ced4176 2019-07-12 stsp /* Check out a single file. */
2381 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2382 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2383 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2384 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2385 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2386 6ced4176 2019-07-12 stsp goto done;
2388 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2389 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2390 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2391 6ced4176 2019-07-12 stsp goto done;
2394 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2395 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2397 6ced4176 2019-07-12 stsp return err;
2398 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2399 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2400 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2401 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2402 6ced4176 2019-07-12 stsp goto done;
2405 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2406 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
2408 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2409 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2410 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2411 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2412 6ced4176 2019-07-12 stsp goto done;
2414 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2415 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2416 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2417 6ced4176 2019-07-12 stsp goto done;
2422 6ced4176 2019-07-12 stsp free(in_repo_path);
2423 6ced4176 2019-07-12 stsp if (err) {
2424 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2425 6ced4176 2019-07-12 stsp free(*tree_relpath);
2426 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2427 6ced4176 2019-07-12 stsp free(*tree_id);
2428 6ced4176 2019-07-12 stsp *tree_id = NULL;
2430 07ed472d 2019-07-12 stsp return err;
2433 07ed472d 2019-07-12 stsp static const struct got_error *
2434 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2435 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2436 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2437 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2439 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2440 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2441 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2442 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2443 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2445 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2446 7f47418f 2019-12-20 stsp if (err) {
2447 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2448 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2449 7f47418f 2019-12-20 stsp goto done;
2450 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2451 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2453 7f47418f 2019-12-20 stsp return err;
2456 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2457 07ed472d 2019-07-12 stsp worktree->base_commit_id);
2459 07ed472d 2019-07-12 stsp goto done;
2461 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2463 07ed472d 2019-07-12 stsp goto done;
2465 07ed472d 2019-07-12 stsp if (entry_name &&
2466 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2467 07ed472d 2019-07-12 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
2468 07ed472d 2019-07-12 stsp goto done;
2471 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2472 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2473 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2474 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2475 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2476 07ed472d 2019-07-12 stsp arg.repo = repo;
2477 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2478 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2479 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2480 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2481 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2482 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2485 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2486 07ed472d 2019-07-12 stsp if (commit)
2487 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2488 6ced4176 2019-07-12 stsp return err;
2491 9d31a1d8 2018-03-11 stsp const struct got_error *
2492 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2493 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2494 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2495 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2497 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2498 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2499 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2500 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2501 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2502 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2503 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2504 f2ea84fa 2019-07-27 stsp SIMPLEQ_ENTRY(tree_path_data) entry;
2505 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2506 f2ea84fa 2019-07-27 stsp int entry_type;
2507 f2ea84fa 2019-07-27 stsp char *relpath;
2508 f2ea84fa 2019-07-27 stsp char *entry_name;
2509 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2510 f2ea84fa 2019-07-27 stsp SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2512 f2ea84fa 2019-07-27 stsp SIMPLEQ_INIT(&tree_paths);
2514 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2516 9d31a1d8 2018-03-11 stsp return err;
2518 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2519 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2520 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2521 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2522 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2523 f2ea84fa 2019-07-27 stsp goto done;
2526 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2527 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2528 f2ea84fa 2019-07-27 stsp if (err) {
2529 f2ea84fa 2019-07-27 stsp free(tpd);
2530 6ced4176 2019-07-12 stsp goto done;
2533 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2534 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2535 f2ea84fa 2019-07-27 stsp if (err) {
2536 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2537 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2538 f2ea84fa 2019-07-27 stsp free(tpd);
2539 f2ea84fa 2019-07-27 stsp goto done;
2542 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2544 f2ea84fa 2019-07-27 stsp SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2548 51514078 2018-12-25 stsp * Read the file index.
2549 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2550 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2552 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2554 c4cdcb68 2019-04-03 stsp goto done;
2556 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2557 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2558 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2560 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2561 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2562 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2566 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2567 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2568 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2569 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2570 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2571 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2572 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2573 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2577 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_NEXT(tpd, entry);
2579 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2580 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2581 af12c6b9 2019-06-04 stsp err = sync_err;
2583 9c6338c4 2019-06-02 stsp free(fileindex_path);
2585 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2586 9d31a1d8 2018-03-11 stsp if (commit)
2587 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2588 5ade8233 2019-07-12 stsp if (fileindex)
2589 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2590 f2ea84fa 2019-07-27 stsp while (!SIMPLEQ_EMPTY(&tree_paths)) {
2591 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2592 f2ea84fa 2019-07-27 stsp SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2593 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2594 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2595 f2ea84fa 2019-07-27 stsp free(tpd);
2597 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2598 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2599 9d31a1d8 2018-03-11 stsp err = unlockerr;
2600 f8d1f275 2019-02-04 stsp return err;
2603 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2604 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2605 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2606 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2607 234035bc 2019-06-01 stsp void *progress_arg;
2608 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2609 234035bc 2019-06-01 stsp void *cancel_arg;
2610 f69721c3 2019-10-21 stsp const char *label_orig;
2611 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2614 234035bc 2019-06-01 stsp static const struct got_error *
2615 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2616 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
2617 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
2618 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2620 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2621 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2622 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2623 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2624 234035bc 2019-06-01 stsp struct stat sb;
2625 234035bc 2019-06-01 stsp unsigned char status;
2626 234035bc 2019-06-01 stsp int local_changes_subsumed;
2628 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2629 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2630 d6c87207 2019-08-02 stsp strlen(path2));
2631 1ee397ad 2019-07-12 stsp if (ie == NULL)
2632 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2633 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2635 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2636 234035bc 2019-06-01 stsp path2) == -1)
2637 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2639 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2642 234035bc 2019-06-01 stsp goto done;
2644 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2645 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2646 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2647 234035bc 2019-06-01 stsp goto done;
2649 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2650 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2651 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2652 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2653 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2654 234035bc 2019-06-01 stsp goto done;
2657 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2658 af57b12a 2020-07-23 stsp err = merge_symlink(a->worktree, blob1,
2659 af57b12a 2020-07-23 stsp ondisk_path, path2, sb.st_mode, a->label_orig,
2660 af57b12a 2020-07-23 stsp blob2, a->commit_id2, repo, a->progress_cb,
2661 af57b12a 2020-07-23 stsp a->progress_arg);
2663 af57b12a 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
2664 af57b12a 2020-07-23 stsp blob1, ondisk_path, path2, sb.st_mode,
2665 af57b12a 2020-07-23 stsp a->label_orig, blob2, a->commit_id2, repo,
2666 af57b12a 2020-07-23 stsp a->progress_cb, a->progress_arg);
2668 234035bc 2019-06-01 stsp } else if (blob1) {
2669 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2670 d6c87207 2019-08-02 stsp strlen(path1));
2671 1ee397ad 2019-07-12 stsp if (ie == NULL)
2672 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2673 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2675 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2676 2b92fad7 2019-06-02 stsp path1) == -1)
2677 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2679 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2682 2b92fad7 2019-06-02 stsp goto done;
2684 2b92fad7 2019-06-02 stsp switch (status) {
2685 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2686 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2687 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2689 1ee397ad 2019-07-12 stsp goto done;
2690 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2692 2b92fad7 2019-06-02 stsp goto done;
2694 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2696 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2697 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2698 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2699 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2701 1ee397ad 2019-07-12 stsp goto done;
2703 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2705 2b92fad7 2019-06-02 stsp case GOT_STATUS_ADD:
2706 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2707 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2708 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2709 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2711 1ee397ad 2019-07-12 stsp goto done;
2713 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2714 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2716 1ee397ad 2019-07-12 stsp goto done;
2721 234035bc 2019-06-01 stsp } else if (blob2) {
2722 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2723 234035bc 2019-06-01 stsp path2) == -1)
2724 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2725 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2726 d6c87207 2019-08-02 stsp strlen(path2));
2728 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2729 7f91a133 2019-12-13 stsp -1, NULL, repo);
2731 234035bc 2019-06-01 stsp goto done;
2732 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2733 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2734 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2735 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2736 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2737 1ee397ad 2019-07-12 stsp status, path2);
2738 234035bc 2019-06-01 stsp goto done;
2740 526a746f 2020-07-23 stsp if (S_ISLNK(mode2)) {
2741 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
2742 526a746f 2020-07-23 stsp ondisk_path, path2, sb.st_mode,
2743 526a746f 2020-07-23 stsp a->label_orig, blob2, a->commit_id2,
2744 526a746f 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2746 526a746f 2020-07-23 stsp goto done;
2748 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
2749 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
2750 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
2751 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
2752 526a746f 2020-07-23 stsp a->progress_arg);
2754 526a746f 2020-07-23 stsp goto done;
2756 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2757 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
2758 054041d0 2020-06-24 stsp ondisk_path, blob2->id.sha1,
2759 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
2761 234035bc 2019-06-01 stsp goto done;
2764 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2765 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2766 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
2767 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
2768 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
2769 2e1fa222 2020-07-23 stsp 0, repo, a->progress_cb, a->progress_arg);
2771 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
2772 2e1fa222 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, repo,
2773 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
2776 234035bc 2019-06-01 stsp goto done;
2777 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
2779 234035bc 2019-06-01 stsp goto done;
2780 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ondisk_path,
2781 3969253a 2020-03-07 stsp NULL, NULL, 1);
2782 3969253a 2020-03-07 stsp if (err) {
2783 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
2784 3969253a 2020-03-07 stsp goto done;
2786 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
2787 2b92fad7 2019-06-02 stsp if (err) {
2788 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
2789 2b92fad7 2019-06-02 stsp goto done;
2791 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2792 2e1fa222 2020-07-23 stsp err = got_fileindex_entry_filetype_set(ie,
2793 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2795 2e1fa222 2020-07-23 stsp goto done;
2800 234035bc 2019-06-01 stsp free(ondisk_path);
2801 234035bc 2019-06-01 stsp return err;
2804 234035bc 2019-06-01 stsp struct check_merge_ok_arg {
2805 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2806 234035bc 2019-06-01 stsp struct got_repository *repo;
2809 234035bc 2019-06-01 stsp static const struct got_error *
2810 234035bc 2019-06-01 stsp check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2812 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
2813 234035bc 2019-06-01 stsp struct check_merge_ok_arg *a = arg;
2814 234035bc 2019-06-01 stsp unsigned char status;
2815 234035bc 2019-06-01 stsp struct stat sb;
2816 234035bc 2019-06-01 stsp char *ondisk_path;
2818 234035bc 2019-06-01 stsp /* Reject merges into a work tree with mixed base commits. */
2819 234035bc 2019-06-01 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2820 234035bc 2019-06-01 stsp SHA1_DIGEST_LENGTH))
2821 234035bc 2019-06-01 stsp return got_error(GOT_ERR_MIXED_COMMITS);
2823 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2825 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2827 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
2828 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2830 234035bc 2019-06-01 stsp return err;
2831 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
2832 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
2834 234035bc 2019-06-01 stsp return NULL;
2837 818c7501 2019-07-11 stsp static const struct got_error *
2838 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2839 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
2840 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
2841 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2842 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2844 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
2845 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2846 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2847 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
2848 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2850 03415a1a 2019-06-02 stsp if (commit_id1) {
2851 f69721c3 2019-10-21 stsp char *id_str;
2853 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2854 03415a1a 2019-06-02 stsp worktree->path_prefix);
2856 03415a1a 2019-06-02 stsp goto done;
2858 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2860 03415a1a 2019-06-02 stsp goto done;
2862 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
2864 f69721c3 2019-10-21 stsp goto done;
2866 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2867 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2868 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2869 f69721c3 2019-10-21 stsp free(id_str);
2870 f69721c3 2019-10-21 stsp goto done;
2872 f69721c3 2019-10-21 stsp free(id_str);
2875 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2876 234035bc 2019-06-01 stsp worktree->path_prefix);
2878 234035bc 2019-06-01 stsp goto done;
2880 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2882 234035bc 2019-06-01 stsp goto done;
2884 234035bc 2019-06-01 stsp arg.worktree = worktree;
2885 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
2886 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
2887 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
2888 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
2889 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
2890 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
2891 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
2892 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2893 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2894 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2895 af12c6b9 2019-06-04 stsp err = sync_err;
2897 234035bc 2019-06-01 stsp if (tree1)
2898 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
2899 234035bc 2019-06-01 stsp if (tree2)
2900 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
2901 f69721c3 2019-10-21 stsp free(label_orig);
2902 818c7501 2019-07-11 stsp return err;
2905 818c7501 2019-07-11 stsp const struct got_error *
2906 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
2907 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2908 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2909 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2911 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
2912 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
2913 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
2914 818c7501 2019-07-11 stsp struct check_merge_ok_arg mok_arg;
2916 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
2918 818c7501 2019-07-11 stsp return err;
2920 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2922 818c7501 2019-07-11 stsp goto done;
2924 818c7501 2019-07-11 stsp mok_arg.worktree = worktree;
2925 818c7501 2019-07-11 stsp mok_arg.repo = repo;
2926 818c7501 2019-07-11 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2927 818c7501 2019-07-11 stsp &mok_arg);
2929 818c7501 2019-07-11 stsp goto done;
2931 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2932 818c7501 2019-07-11 stsp commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2934 818c7501 2019-07-11 stsp if (fileindex)
2935 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
2936 818c7501 2019-07-11 stsp free(fileindex_path);
2937 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2938 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
2939 234035bc 2019-06-01 stsp err = unlockerr;
2940 234035bc 2019-06-01 stsp return err;
2943 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
2944 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
2945 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
2946 927df6b7 2019-02-10 stsp const char *status_path;
2947 927df6b7 2019-02-10 stsp size_t status_path_len;
2948 f8d1f275 2019-02-04 stsp struct got_repository *repo;
2949 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
2950 f8d1f275 2019-02-04 stsp void *status_arg;
2951 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2952 f8d1f275 2019-02-04 stsp void *cancel_arg;
2953 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
2954 6841da00 2019-08-08 stsp struct got_pathlist_head ignores;
2955 f2a9dc41 2019-12-13 tracey int report_unchanged;
2956 3143d852 2020-06-25 stsp int no_ignores;
2959 f8d1f275 2019-02-04 stsp static const struct got_error *
2960 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2961 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
2962 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
2963 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
2965 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
2966 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
2967 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
2968 927df6b7 2019-02-10 stsp struct stat sb;
2969 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
2970 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2971 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
2973 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2975 98eaaa12 2019-08-03 stsp return err;
2977 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
2978 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2979 98eaaa12 2019-08-03 stsp return NULL;
2981 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
2982 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2983 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
2985 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
2986 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2987 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
2989 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
2990 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
2991 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2992 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
2993 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
2996 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
2997 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3000 927df6b7 2019-02-10 stsp static const struct got_error *
3001 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3002 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3004 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3005 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3006 f8d1f275 2019-02-04 stsp char *abspath;
3008 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3009 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3011 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3012 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3013 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3014 927df6b7 2019-02-10 stsp return NULL;
3016 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3017 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3018 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3019 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3021 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3022 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3023 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3026 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3027 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3028 f8d1f275 2019-02-04 stsp free(abspath);
3029 9d31a1d8 2018-03-11 stsp return err;
3032 f8d1f275 2019-02-04 stsp static const struct got_error *
3033 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3035 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3036 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3037 2ec1f75b 2019-03-26 stsp unsigned char status;
3039 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3040 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3042 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3043 927df6b7 2019-02-10 stsp return NULL;
3045 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3046 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3047 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3048 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3050 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3051 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3052 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3056 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3058 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3060 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3061 6841da00 2019-08-08 stsp free((char *)pe->path);
3062 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3066 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3068 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3070 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3071 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3072 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3073 6841da00 2019-08-08 stsp free((char *)pe->path);
3075 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3078 6841da00 2019-08-08 stsp static const struct got_error *
3079 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3081 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3082 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3083 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3084 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3085 6841da00 2019-08-08 stsp size_t linesize = 0;
3086 6841da00 2019-08-08 stsp ssize_t linelen;
3088 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3089 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3090 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3091 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3093 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3094 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3095 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3097 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3098 bd8de430 2019-10-04 stsp if (line[0] == '#')
3101 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3102 bd8de430 2019-10-04 stsp if (line[0] == '!')
3105 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3106 6841da00 2019-08-08 stsp line) == -1) {
3107 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3108 6841da00 2019-08-08 stsp goto done;
3110 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3112 6841da00 2019-08-08 stsp goto done;
3114 6841da00 2019-08-08 stsp if (ferror(f)) {
3115 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3116 6841da00 2019-08-08 stsp goto done;
3119 6841da00 2019-08-08 stsp dirpath = strdup(path);
3120 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3121 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3122 6841da00 2019-08-08 stsp goto done;
3124 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3126 6841da00 2019-08-08 stsp free(line);
3127 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3128 6841da00 2019-08-08 stsp free(dirpath);
3129 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3131 6841da00 2019-08-08 stsp return err;
3135 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3137 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3139 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3140 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3141 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3142 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3144 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3145 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3147 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3149 bd8de430 2019-10-04 stsp pattern += 3;
3151 bd8de430 2019-10-04 stsp while (*p) {
3152 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3153 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3154 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3155 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3157 bd8de430 2019-10-04 stsp while (*p == '/')
3167 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3168 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3169 6841da00 2019-08-08 stsp * ignores backwards.
3171 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3172 6841da00 2019-08-08 stsp while (pe) {
3173 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3174 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3175 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3176 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3177 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3178 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3179 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3180 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3181 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3186 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3192 6841da00 2019-08-08 stsp static const struct got_error *
3193 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3194 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3196 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3197 6841da00 2019-08-08 stsp char *ignorespath;
3198 886cec17 2019-12-15 stsp int fd = -1;
3199 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3201 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3202 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3203 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3205 886cec17 2019-12-15 stsp if (dirfd != -1) {
3206 886cec17 2019-12-15 stsp fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3207 886cec17 2019-12-15 stsp if (fd == -1) {
3208 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3209 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3210 886cec17 2019-12-15 stsp ignorespath);
3212 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3213 886cec17 2019-12-15 stsp if (ignoresfile == NULL)
3214 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3215 886cec17 2019-12-15 stsp ignorespath);
3218 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3222 886cec17 2019-12-15 stsp ignoresfile = fopen(ignorespath, "r");
3223 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3224 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3225 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3226 886cec17 2019-12-15 stsp ignorespath);
3228 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3231 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3232 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3233 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3234 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3235 6841da00 2019-08-08 stsp free(ignorespath);
3236 6841da00 2019-08-08 stsp return err;
3239 f8d1f275 2019-02-04 stsp static const struct got_error *
3240 7f91a133 2019-12-13 stsp status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3242 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3243 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3244 f8d1f275 2019-02-04 stsp char *path = NULL;
3246 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3247 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3249 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3250 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3251 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3253 f8d1f275 2019-02-04 stsp path = de->d_name;
3256 3143d852 2020-06-25 stsp if (de->d_type != DT_DIR &&
3257 3143d852 2020-06-25 stsp got_path_is_child(path, a->status_path, a->status_path_len)
3258 6841da00 2019-08-08 stsp && !match_ignores(&a->ignores, path))
3259 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3260 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3261 f8d1f275 2019-02-04 stsp if (parent_path[0])
3262 f8d1f275 2019-02-04 stsp free(path);
3263 b72f483a 2019-02-05 stsp return err;
3266 347d1d3e 2019-07-12 stsp static const struct got_error *
3267 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3269 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3270 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3272 3143d852 2020-06-25 stsp if (a->no_ignores)
3273 3143d852 2020-06-25 stsp return NULL;
3275 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path,
3276 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3278 3143d852 2020-06-25 stsp return err;
3280 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path, path,
3281 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3283 3143d852 2020-06-25 stsp return err;
3286 3143d852 2020-06-25 stsp static const struct got_error *
3287 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3288 abb4604f 2019-07-27 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3289 f2a9dc41 2019-12-13 tracey void *status_arg, struct got_repository *repo, int report_unchanged)
3291 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3292 abb4604f 2019-07-27 stsp struct stat sb;
3294 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3296 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3297 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3299 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3300 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3301 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3302 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3303 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3304 abb4604f 2019-07-27 stsp return NULL;
3307 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3308 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3309 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3311 abb4604f 2019-07-27 stsp return NULL;
3314 3143d852 2020-06-25 stsp static const struct got_error *
3315 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3316 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3318 3143d852 2020-06-25 stsp const struct got_error *err;
3319 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3321 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3322 3143d852 2020-06-25 stsp ".cvsignore");
3324 3143d852 2020-06-25 stsp return err;
3326 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3327 3143d852 2020-06-25 stsp ".gitignore");
3329 3143d852 2020-06-25 stsp return err;
3331 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3332 3143d852 2020-06-25 stsp if (err) {
3333 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3334 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3335 3143d852 2020-06-25 stsp return err;
3337 3143d852 2020-06-25 stsp for (;;) {
3338 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3339 3143d852 2020-06-25 stsp ".cvsignore");
3342 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3343 3143d852 2020-06-25 stsp ".gitignore");
3346 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3347 3143d852 2020-06-25 stsp if (err) {
3348 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3349 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3352 b737c85a 2020-06-26 stsp free(parent_path);
3353 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3354 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3357 b737c85a 2020-06-26 stsp free(parent_path);
3358 b737c85a 2020-06-26 stsp free(next_parent_path);
3359 3143d852 2020-06-25 stsp return err;
3362 abb4604f 2019-07-27 stsp static const struct got_error *
3363 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3364 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3365 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3366 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3367 f2a9dc41 2019-12-13 tracey int report_unchanged)
3369 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3370 6fc93f37 2019-12-13 stsp int fd = -1;
3371 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3372 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3373 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3375 3143d852 2020-06-25 stsp TAILQ_INIT(&arg.ignores);
3377 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3378 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3379 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3381 6fc93f37 2019-12-13 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3382 6fc93f37 2019-12-13 stsp if (fd == -1) {
3383 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3384 00bb5ea0 2020-07-23 stsp errno != ELOOP)
3385 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3387 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3388 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3389 f2a9dc41 2019-12-13 tracey report_unchanged);
3391 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3392 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3393 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3394 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3395 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3396 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3397 abb4604f 2019-07-27 stsp arg.status_path = path;
3398 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3399 abb4604f 2019-07-27 stsp arg.repo = repo;
3400 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3401 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3402 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3403 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3404 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3405 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3406 022fae89 2019-12-06 tracey if (!no_ignores) {
3407 3143d852 2020-06-25 stsp err = add_ignores_from_parent_paths(&arg.ignores,
3408 3143d852 2020-06-25 stsp worktree->root_path, path);
3410 3143d852 2020-06-25 stsp goto done;
3412 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3413 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3416 3143d852 2020-06-25 stsp free_ignores(&arg.ignores);
3417 6fc93f37 2019-12-13 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
3418 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3419 927df6b7 2019-02-10 stsp free(ondisk_path);
3420 347d1d3e 2019-07-12 stsp return err;
3423 347d1d3e 2019-07-12 stsp const struct got_error *
3424 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3425 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3426 72ea6654 2019-07-27 stsp got_worktree_status_cb status_cb, void *status_arg,
3427 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3429 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3430 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3431 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3432 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3434 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3436 347d1d3e 2019-07-12 stsp return err;
3438 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3439 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3440 f2a9dc41 2019-12-13 tracey status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3444 f8d1f275 2019-02-04 stsp free(fileindex_path);
3445 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3446 f8d1f275 2019-02-04 stsp return err;
3449 6c7ab921 2019-03-18 stsp const struct got_error *
3450 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3451 6c7ab921 2019-03-18 stsp const char *arg)
3453 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3454 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3455 6c7ab921 2019-03-18 stsp size_t len;
3456 00bb5ea0 2020-07-23 stsp struct stat sb;
3458 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3460 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3461 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3462 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3464 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3465 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3466 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3467 00bb5ea0 2020-07-23 stsp goto done;
3470 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3472 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3473 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3474 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3475 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3477 00bb5ea0 2020-07-23 stsp char *abspath = NULL;
3478 00bb5ea0 2020-07-23 stsp char canonpath[PATH_MAX];
3479 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3480 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3481 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3482 00bb5ea0 2020-07-23 stsp goto done;
3486 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3487 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3489 d0710d08 2019-07-22 stsp goto done;
3490 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3491 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3492 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3493 00bb5ea0 2020-07-23 stsp goto done;
3496 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3497 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3498 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3499 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3500 00bb5ea0 2020-07-23 stsp goto done;
3502 00bb5ea0 2020-07-23 stsp if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3503 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3504 00bb5ea0 2020-07-23 stsp goto done;
3509 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3510 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3511 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3512 6c7ab921 2019-03-18 stsp goto done;
3515 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3516 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3517 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3519 f6d88e1a 2019-05-29 stsp goto done;
3521 f6d88e1a 2019-05-29 stsp path = strdup("");
3522 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3523 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3524 f6d88e1a 2019-05-29 stsp goto done;
3528 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3529 6c7ab921 2019-03-18 stsp len = strlen(path);
3530 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3531 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3535 6c7ab921 2019-03-18 stsp free(resolved);
3536 d0710d08 2019-07-22 stsp free(cwd);
3537 6c7ab921 2019-03-18 stsp if (err == NULL)
3538 6c7ab921 2019-03-18 stsp *wt_path = path;
3540 6c7ab921 2019-03-18 stsp free(path);
3541 d00136be 2019-03-26 stsp return err;
3544 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3545 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3546 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3547 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3548 4e68cba3 2019-11-23 stsp void *progress_arg;
3549 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3552 4e68cba3 2019-11-23 stsp static const struct got_error *
3553 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3554 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3555 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3556 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3558 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3559 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3560 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3561 6d022e97 2019-08-04 stsp struct stat sb;
3562 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3564 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3565 dbb83fbd 2019-12-12 stsp relpath) == -1)
3566 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3568 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3570 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3571 12463d8b 2019-12-13 stsp de_name, a->repo);
3573 dbb83fbd 2019-12-12 stsp goto done;
3574 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3575 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3576 dbb83fbd 2019-12-12 stsp goto done;
3577 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3579 dbb83fbd 2019-12-12 stsp goto done;
3582 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3583 dbb83fbd 2019-12-12 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3584 dbb83fbd 2019-12-12 stsp goto done;
3587 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3589 4e68cba3 2019-11-23 stsp goto done;
3590 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3591 3969253a 2020-03-07 stsp if (err) {
3592 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3593 3969253a 2020-03-07 stsp goto done;
3595 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3596 07f5b47a 2019-06-02 stsp if (err) {
3597 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
3598 4e68cba3 2019-11-23 stsp goto done;
3601 dbb83fbd 2019-12-12 stsp free(ondisk_path);
3603 dbb83fbd 2019-12-12 stsp return err;
3604 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
3605 dbb83fbd 2019-12-12 stsp return NULL;
3606 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3609 d00136be 2019-03-26 stsp const struct got_error *
3610 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
3611 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
3612 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3613 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
3615 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3616 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
3617 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3618 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
3619 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
3621 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3623 d00136be 2019-03-26 stsp return err;
3625 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3627 d00136be 2019-03-26 stsp goto done;
3629 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
3630 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
3631 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
3632 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
3633 4e68cba3 2019-11-23 stsp saa.repo = repo;
3635 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3636 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3637 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3641 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3642 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3643 af12c6b9 2019-06-04 stsp err = sync_err;
3645 fb399478 2019-07-12 stsp free(fileindex_path);
3646 d00136be 2019-03-26 stsp if (fileindex)
3647 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
3648 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3649 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
3650 d00136be 2019-03-26 stsp err = unlockerr;
3651 6c7ab921 2019-03-18 stsp return err;
3654 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
3655 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
3656 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
3657 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
3658 f2a9dc41 2019-12-13 tracey void *progress_arg;
3659 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
3660 f2a9dc41 2019-12-13 tracey int delete_local_mods;
3661 70e3e7f5 2019-12-13 tracey int keep_on_disk;
3664 f2a9dc41 2019-12-13 tracey static const struct got_error *
3665 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
3666 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
3667 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3668 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
3670 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
3671 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
3672 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
3673 17ed4618 2019-06-02 stsp struct stat sb;
3674 15341bfd 2020-03-05 tracey char *ondisk_path, *parent = NULL;
3676 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3677 17ed4618 2019-06-02 stsp if (ie == NULL)
3678 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
3680 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
3681 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
3682 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3683 9acbc4fa 2019-08-03 stsp return NULL;
3684 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3687 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3688 f2a9dc41 2019-12-13 tracey relpath) == -1)
3689 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
3691 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3694 f2a9dc41 2019-12-13 tracey goto done;
3696 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
3697 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
3698 f2a9dc41 2019-12-13 tracey goto done;
3699 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3700 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3701 f2a9dc41 2019-12-13 tracey goto done;
3703 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
3704 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
3705 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3706 f2a9dc41 2019-12-13 tracey goto done;
3710 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3711 12463d8b 2019-12-13 stsp if (dirfd != -1) {
3712 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
3713 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
3714 12463d8b 2019-12-13 stsp ondisk_path);
3715 12463d8b 2019-12-13 stsp goto done;
3717 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
3718 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
3719 12463d8b 2019-12-13 stsp goto done;
3722 15341bfd 2020-03-05 tracey parent = dirname(ondisk_path);
3724 15341bfd 2020-03-05 tracey if (parent == NULL) {
3725 15341bfd 2020-03-05 tracey err = got_error_from_errno2("dirname", ondisk_path);
3726 15341bfd 2020-03-05 tracey goto done;
3728 15341bfd 2020-03-05 tracey while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3729 15341bfd 2020-03-05 tracey if (rmdir(parent) == -1) {
3730 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
3731 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
3732 15341bfd 2020-03-05 tracey parent);
3735 15341bfd 2020-03-05 tracey parent = dirname(parent);
3736 15341bfd 2020-03-05 tracey if (parent == NULL) {
3737 15341bfd 2020-03-05 tracey err = got_error_from_errno2("dirname", parent);
3738 15341bfd 2020-03-05 tracey goto done;
3743 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3745 f2a9dc41 2019-12-13 tracey free(ondisk_path);
3746 f2a9dc41 2019-12-13 tracey if (err)
3747 f2a9dc41 2019-12-13 tracey return err;
3748 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
3749 f2a9dc41 2019-12-13 tracey return NULL;
3750 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3751 f2a9dc41 2019-12-13 tracey staged_status, relpath);
3754 2ec1f75b 2019-03-26 stsp const struct got_error *
3755 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
3756 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
3757 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
3758 70e3e7f5 2019-12-13 tracey struct got_repository *repo, int keep_on_disk)
3760 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3761 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
3762 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3763 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
3764 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
3766 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3768 2ec1f75b 2019-03-26 stsp return err;
3770 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3772 2ec1f75b 2019-03-26 stsp goto done;
3774 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
3775 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
3776 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
3777 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
3778 f2a9dc41 2019-12-13 tracey sda.repo = repo;
3779 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
3780 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
3782 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3783 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
3784 f2a9dc41 2019-12-13 tracey schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3788 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3789 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3790 af12c6b9 2019-06-04 stsp err = sync_err;
3792 fb399478 2019-07-12 stsp free(fileindex_path);
3793 2ec1f75b 2019-03-26 stsp if (fileindex)
3794 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
3795 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3796 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
3797 2ec1f75b 2019-03-26 stsp err = unlockerr;
3798 33aa809d 2019-08-08 stsp return err;
3801 33aa809d 2019-08-08 stsp static const struct got_error *
3802 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3804 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
3805 33aa809d 2019-08-08 stsp char *line = NULL;
3806 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
3807 33aa809d 2019-08-08 stsp ssize_t linelen;
3809 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
3810 33aa809d 2019-08-08 stsp if (linelen == -1) {
3811 33aa809d 2019-08-08 stsp if (ferror(infile)) {
3812 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
3813 33aa809d 2019-08-08 stsp goto done;
3815 33aa809d 2019-08-08 stsp return NULL;
3817 33aa809d 2019-08-08 stsp if (outfile) {
3818 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
3819 33aa809d 2019-08-08 stsp if (n != linelen) {
3820 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
3821 33aa809d 2019-08-08 stsp goto done;
3824 33aa809d 2019-08-08 stsp if (rejectfile) {
3825 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
3826 33aa809d 2019-08-08 stsp if (n != linelen)
3827 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
3830 33aa809d 2019-08-08 stsp free(line);
3831 2ec1f75b 2019-03-26 stsp return err;
3834 33aa809d 2019-08-08 stsp static const struct got_error *
3835 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
3837 33aa809d 2019-08-08 stsp char *line = NULL;
3838 33aa809d 2019-08-08 stsp size_t linesize = 0;
3839 33aa809d 2019-08-08 stsp ssize_t linelen;
3841 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
3842 500467ff 2019-09-25 hiltjo if (linelen == -1) {
3843 500467ff 2019-09-25 hiltjo if (ferror(f))
3844 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
3845 500467ff 2019-09-25 hiltjo return NULL;
3847 33aa809d 2019-08-08 stsp free(line);
3848 33aa809d 2019-08-08 stsp return NULL;
3851 33aa809d 2019-08-08 stsp static const struct got_error *
3852 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3853 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
3854 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
3856 33aa809d 2019-08-08 stsp const struct got_error *err;
3858 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
3859 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
3860 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
3862 33aa809d 2019-08-08 stsp return err;
3863 33aa809d 2019-08-08 stsp (*line_cur1)++;
3865 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
3866 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
3867 33aa809d 2019-08-08 stsp if (rejectfile)
3868 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
3870 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
3872 33aa809d 2019-08-08 stsp return err;
3873 33aa809d 2019-08-08 stsp (*line_cur2)++;
3875 33aa809d 2019-08-08 stsp /* Copy patched lines. */
3876 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
3877 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
3879 33aa809d 2019-08-08 stsp return err;
3880 33aa809d 2019-08-08 stsp (*line_cur2)++;
3882 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
3883 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
3884 33aa809d 2019-08-08 stsp if (rejectfile)
3885 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
3887 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
3889 33aa809d 2019-08-08 stsp return err;
3890 33aa809d 2019-08-08 stsp (*line_cur1)++;
3893 f1e81a05 2019-08-10 stsp return NULL;
3896 f1e81a05 2019-08-10 stsp static const struct got_error *
3897 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3898 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
3900 f1e81a05 2019-08-10 stsp const struct got_error *err;
3902 f1e81a05 2019-08-10 stsp if (outfile) {
3903 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
3904 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
3905 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
3907 f1e81a05 2019-08-10 stsp return err;
3908 f1e81a05 2019-08-10 stsp (*line_cur1)++;
3911 f1e81a05 2019-08-10 stsp if (rejectfile) {
3912 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
3913 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
3914 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
3916 f1e81a05 2019-08-10 stsp return err;
3917 f1e81a05 2019-08-10 stsp (*line_cur2)++;
3921 33aa809d 2019-08-08 stsp return NULL;
3924 33aa809d 2019-08-08 stsp static const struct got_error *
3925 33aa809d 2019-08-08 stsp apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3926 33aa809d 2019-08-08 stsp int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3927 33aa809d 2019-08-08 stsp int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3928 33aa809d 2019-08-08 stsp int *line_cur2, FILE *outfile, FILE *rejectfile,
3929 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
3931 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
3932 33aa809d 2019-08-08 stsp int start_old = change->cv.a;
3933 33aa809d 2019-08-08 stsp int end_old = change->cv.b;
3934 33aa809d 2019-08-08 stsp int start_new = change->cv.c;
3935 33aa809d 2019-08-08 stsp int end_new = change->cv.d;
3936 33aa809d 2019-08-08 stsp long pos1, pos2;
3937 33aa809d 2019-08-08 stsp FILE *hunkfile;
3939 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
3941 33aa809d 2019-08-08 stsp hunkfile = got_opentemp();
3942 33aa809d 2019-08-08 stsp if (hunkfile == NULL)
3943 33aa809d 2019-08-08 stsp return got_error_from_errno("got_opentemp");
3945 33aa809d 2019-08-08 stsp pos1 = ftell(f1);
3946 33aa809d 2019-08-08 stsp pos2 = ftell(f2);
3948 33aa809d 2019-08-08 stsp /* XXX TODO needs error checking */
3949 33aa809d 2019-08-08 stsp got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3951 33aa809d 2019-08-08 stsp if (fseek(f1, pos1, SEEK_SET) == -1) {
3952 33aa809d 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
3953 33aa809d 2019-08-08 stsp goto done;
3955 33aa809d 2019-08-08 stsp if (fseek(f2, pos2, SEEK_SET) == -1) {
3956 33aa809d 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
3957 33aa809d 2019-08-08 stsp goto done;
3959 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3960 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
3961 33aa809d 2019-08-08 stsp goto done;
3964 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3965 33aa809d 2019-08-08 stsp hunkfile, n, nchanges);
3967 33aa809d 2019-08-08 stsp goto done;
3969 33aa809d 2019-08-08 stsp switch (*choice) {
3970 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
3971 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3972 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
3974 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
3975 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3976 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
3978 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
3981 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
3985 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3986 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
3987 33aa809d 2019-08-08 stsp return err;
3990 1f1abb7e 2019-08-08 stsp struct revert_file_args {
3991 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
3992 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
3993 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
3994 1f1abb7e 2019-08-08 stsp void *progress_arg;
3995 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
3996 33aa809d 2019-08-08 stsp void *patch_arg;
3997 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4000 e20a8b6f 2019-06-04 stsp static const struct got_error *
4001 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4002 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4003 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4004 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4005 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4007 33aa809d 2019-08-08 stsp const struct got_error *err;
4008 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4009 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4010 1ebedb77 2019-10-19 stsp int fd2 = -1;
4011 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4012 33aa809d 2019-08-08 stsp struct stat sb1, sb2;
4013 33aa809d 2019-08-08 stsp struct got_diff_changes *changes = NULL;
4014 33aa809d 2019-08-08 stsp struct got_diff_state *ds = NULL;
4015 33aa809d 2019-08-08 stsp struct got_diff_args *args = NULL;
4016 33aa809d 2019-08-08 stsp struct got_diff_change *change;
4017 33aa809d 2019-08-08 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4018 33aa809d 2019-08-08 stsp int n = 0;
4020 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4022 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4024 33aa809d 2019-08-08 stsp return err;
4026 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4027 12463d8b 2019-12-13 stsp fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4028 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4029 12463d8b 2019-12-13 stsp err = got_error_from_errno2("openat", path2);
4030 12463d8b 2019-12-13 stsp goto done;
4033 12463d8b 2019-12-13 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4034 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4035 12463d8b 2019-12-13 stsp err = got_error_from_errno2("open", path2);
4036 12463d8b 2019-12-13 stsp goto done;
4039 1ebedb77 2019-10-19 stsp if (fstat(fd2, &sb2) == -1) {
4040 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("fstat", path2);
4041 1ebedb77 2019-10-19 stsp goto done;
4044 1ebedb77 2019-10-19 stsp f2 = fdopen(fd2, "r");
4045 33aa809d 2019-08-08 stsp if (f2 == NULL) {
4046 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", path2);
4047 33aa809d 2019-08-08 stsp goto done;
4051 33aa809d 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4053 33aa809d 2019-08-08 stsp goto done;
4055 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4057 33aa809d 2019-08-08 stsp goto done;
4059 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4061 33aa809d 2019-08-08 stsp goto done;
4063 33aa809d 2019-08-08 stsp if (stat(path1, &sb1) == -1) {
4064 33aa809d 2019-08-08 stsp err = got_error_from_errno2("stat", path1);
4065 33aa809d 2019-08-08 stsp goto done;
4068 33aa809d 2019-08-08 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
4069 33aa809d 2019-08-08 stsp f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4071 33aa809d 2019-08-08 stsp goto done;
4073 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4075 33aa809d 2019-08-08 stsp goto done;
4077 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4078 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4079 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4080 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4081 33aa809d 2019-08-08 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4082 33aa809d 2019-08-08 stsp int choice;
4083 33aa809d 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
4084 33aa809d 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
4085 33aa809d 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
4086 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4087 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4088 e635744c 2019-08-08 stsp patch_cb, patch_arg);
4090 33aa809d 2019-08-08 stsp goto done;
4091 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4092 33aa809d 2019-08-08 stsp have_content = 1;
4093 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4096 1ebedb77 2019-10-19 stsp if (have_content) {
4097 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4098 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4099 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4101 1ebedb77 2019-10-19 stsp goto done;
4103 1ebedb77 2019-10-19 stsp if (chmod(*path_outfile, sb2.st_mode) == -1) {
4104 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("chmod", path2);
4105 1ebedb77 2019-10-19 stsp goto done;
4109 33aa809d 2019-08-08 stsp free(id_str);
4111 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4112 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4113 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4114 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4115 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4116 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4117 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4118 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4119 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4120 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4121 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4122 33aa809d 2019-08-08 stsp if (err || !have_content) {
4123 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4124 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4125 33aa809d 2019-08-08 stsp free(*path_outfile);
4126 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4128 33aa809d 2019-08-08 stsp free(args);
4130 33aa809d 2019-08-08 stsp got_diff_state_free(ds);
4133 33aa809d 2019-08-08 stsp if (changes)
4134 33aa809d 2019-08-08 stsp got_diff_free_changes(changes);
4135 33aa809d 2019-08-08 stsp free(path1);
4136 33aa809d 2019-08-08 stsp return err;
4139 33aa809d 2019-08-08 stsp static const struct got_error *
4140 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4141 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4142 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4143 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4145 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4146 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4147 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4148 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4149 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4150 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4151 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4152 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4153 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4154 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4156 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4157 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4158 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4159 d3bcc3d1 2019-08-08 stsp return NULL;
4161 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4162 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4163 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4165 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4166 65084dad 2019-08-08 stsp if (ie == NULL)
4167 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4169 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4170 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4171 a129376b 2019-03-28 stsp if (err) {
4172 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4173 a129376b 2019-03-28 stsp goto done;
4174 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4175 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4176 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4177 e20a8b6f 2019-06-04 stsp goto done;
4180 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4181 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4182 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4183 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4184 a129376b 2019-03-28 stsp goto done;
4187 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4188 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4189 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4190 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4191 a129376b 2019-03-28 stsp goto done;
4194 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4195 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4196 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4197 a129376b 2019-03-28 stsp goto done;
4202 1f1abb7e 2019-08-08 stsp err = got_object_id_by_path(&tree_id, a->repo,
4203 1f1abb7e 2019-08-08 stsp a->worktree->base_commit_id, tree_path);
4204 a9fa2909 2019-07-27 stsp if (err) {
4205 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4206 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4207 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4208 a9fa2909 2019-07-27 stsp goto done;
4210 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4212 a9fa2909 2019-07-27 stsp goto done;
4214 a9fa2909 2019-07-27 stsp te_name = basename(ie->path);
4215 a9fa2909 2019-07-27 stsp if (te_name == NULL) {
4216 a9fa2909 2019-07-27 stsp err = got_error_from_errno2("basename", ie->path);
4217 a9fa2909 2019-07-27 stsp goto done;
4220 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4221 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4222 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4223 a9fa2909 2019-07-27 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
4224 a9fa2909 2019-07-27 stsp goto done;
4228 a129376b 2019-03-28 stsp switch (status) {
4229 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4230 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4231 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4232 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4233 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4235 33aa809d 2019-08-08 stsp goto done;
4236 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4239 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4240 1f1abb7e 2019-08-08 stsp ie->path);
4242 1ee397ad 2019-07-12 stsp goto done;
4243 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4245 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4246 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4247 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4248 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4249 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4251 33aa809d 2019-08-08 stsp goto done;
4252 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4255 33aa809d 2019-08-08 stsp /* fall through */
4256 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4257 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4258 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4259 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4260 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4261 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4262 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4263 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4264 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4266 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4267 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4268 1f1abb7e 2019-08-08 stsp err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4270 65084dad 2019-08-08 stsp goto done;
4272 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4273 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4274 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4275 a129376b 2019-03-28 stsp goto done;
4278 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4279 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4280 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4281 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4282 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4283 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4285 33aa809d 2019-08-08 stsp if (rename(path_content, ondisk_path) == -1) {
4286 33aa809d 2019-08-08 stsp err = got_error_from_errno3("rename",
4287 33aa809d 2019-08-08 stsp path_content, ondisk_path);
4288 33aa809d 2019-08-08 stsp goto done;
4291 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4292 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4293 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4294 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4295 2e1fa222 2020-07-23 stsp blob, 0, 1, a->repo,
4296 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4298 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4300 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4301 2e1fa222 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob, 0, 1, 0,
4302 2e1fa222 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
4305 a129376b 2019-03-28 stsp goto done;
4306 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4307 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4308 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4309 054041d0 2020-06-24 stsp ondisk_path, blob->id.sha1,
4310 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4312 2e1fa222 2020-07-23 stsp goto done;
4314 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4315 2e1fa222 2020-07-23 stsp err = got_fileindex_entry_filetype_set(ie,
4316 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4318 33aa809d 2019-08-08 stsp goto done;
4327 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4328 33aa809d 2019-08-08 stsp free(path_content);
4329 e20a8b6f 2019-06-04 stsp free(parent_path);
4330 a129376b 2019-03-28 stsp free(tree_path);
4332 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4334 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4335 a129376b 2019-03-28 stsp free(tree_id);
4336 e20a8b6f 2019-06-04 stsp return err;
4339 e20a8b6f 2019-06-04 stsp const struct got_error *
4340 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4341 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4342 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4343 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4344 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4346 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4347 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4348 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4349 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4350 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4351 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4353 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4355 e20a8b6f 2019-06-04 stsp return err;
4357 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4359 e20a8b6f 2019-06-04 stsp goto done;
4361 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4362 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4363 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4364 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4365 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4366 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4367 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4368 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4369 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4370 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
4374 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4375 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4376 e20a8b6f 2019-06-04 stsp err = sync_err;
4378 fb399478 2019-07-12 stsp free(fileindex_path);
4379 a129376b 2019-03-28 stsp if (fileindex)
4380 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4381 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4382 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4383 a129376b 2019-03-28 stsp err = unlockerr;
4384 c4296144 2019-05-09 stsp return err;
4387 cf066bf8 2019-05-09 stsp static void
4388 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4390 24519714 2019-05-09 stsp free(ct->path);
4391 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4392 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4393 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4394 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4395 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4396 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4400 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4401 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4402 24519714 2019-05-09 stsp struct got_repository *repo;
4403 24519714 2019-05-09 stsp struct got_worktree *worktree;
4404 5f8a88c6 2019-08-03 stsp int have_staged_files;
4407 c4296144 2019-05-09 stsp static const struct got_error *
4408 88d0e355 2019-08-03 stsp collect_commitables(void *arg, unsigned char status,
4409 88d0e355 2019-08-03 stsp unsigned char staged_status, const char *relpath,
4410 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4411 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4413 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
4414 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4415 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4416 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4417 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4418 768aea60 2019-05-09 stsp struct stat sb;
4420 5f8a88c6 2019-08-03 stsp if (a->have_staged_files) {
4421 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4422 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4423 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4424 5f8a88c6 2019-08-03 stsp return NULL;
4426 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4427 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4429 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4430 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4431 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4432 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4433 5f8a88c6 2019-08-03 stsp return NULL;
4436 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4437 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4438 036813ee 2019-05-09 stsp goto done;
4440 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4441 036813ee 2019-05-09 stsp parent_path = strdup("");
4442 69960a46 2019-05-09 stsp if (parent_path == NULL)
4443 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4445 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4447 69960a46 2019-05-09 stsp return err;
4450 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4451 cf066bf8 2019-05-09 stsp if (ct == NULL) {
4452 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4453 c4296144 2019-05-09 stsp goto done;
4456 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4457 768aea60 2019-05-09 stsp relpath) == -1) {
4458 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4459 768aea60 2019-05-09 stsp goto done;
4461 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
4462 768aea60 2019-05-09 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
4464 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4465 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
4466 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
4467 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
4468 12463d8b 2019-12-13 stsp ct->ondisk_path);
4469 12463d8b 2019-12-13 stsp goto done;
4471 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
4472 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
4473 768aea60 2019-05-09 stsp goto done;
4475 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
4478 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4479 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4480 44d03001 2019-05-09 stsp relpath) == -1) {
4481 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4482 44d03001 2019-05-09 stsp goto done;
4485 cf066bf8 2019-05-09 stsp ct->status = status;
4486 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
4487 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
4488 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
4489 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
4490 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
4491 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
4492 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4493 b416585c 2019-05-13 stsp goto done;
4495 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
4496 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
4497 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
4498 f0b75401 2019-08-03 stsp goto done;
4501 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
4502 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
4503 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4504 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
4505 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4506 036813ee 2019-05-09 stsp goto done;
4509 24519714 2019-05-09 stsp ct->path = strdup(path);
4510 24519714 2019-05-09 stsp if (ct->path == NULL) {
4511 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4512 24519714 2019-05-09 stsp goto done;
4514 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4516 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
4517 ed175427 2019-05-09 stsp free_commitable(ct);
4518 24519714 2019-05-09 stsp free(parent_path);
4519 036813ee 2019-05-09 stsp free(path);
4520 a129376b 2019-03-28 stsp return err;
4523 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
4524 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
4525 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4526 036813ee 2019-05-09 stsp struct got_repository *);
4528 ed175427 2019-05-09 stsp static const struct got_error *
4529 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4530 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
4531 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
4532 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4533 afa376bf 2019-05-09 stsp struct got_repository *repo)
4535 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
4536 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
4537 036813ee 2019-05-09 stsp char *subpath;
4539 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
4540 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4541 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4543 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
4545 ed175427 2019-05-09 stsp return err;
4547 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
4548 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
4549 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
4550 036813ee 2019-05-09 stsp free(subpath);
4551 036813ee 2019-05-09 stsp return err;
4554 036813ee 2019-05-09 stsp static const struct got_error *
4555 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4557 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4558 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
4560 036813ee 2019-05-09 stsp *match = 0;
4562 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
4563 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
4564 0f63689d 2019-05-10 stsp return NULL;
4567 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4569 0f63689d 2019-05-10 stsp return err;
4570 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
4571 036813ee 2019-05-09 stsp free(ct_parent_path);
4572 036813ee 2019-05-09 stsp return err;
4575 768aea60 2019-05-09 stsp static mode_t
4576 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
4578 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
4579 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
4581 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4584 036813ee 2019-05-09 stsp static const struct got_error *
4585 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4586 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
4588 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4590 036813ee 2019-05-09 stsp *new_te = NULL;
4592 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
4594 036813ee 2019-05-09 stsp goto done;
4596 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4598 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
4599 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4600 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4602 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4604 036813ee 2019-05-09 stsp if (err && *new_te) {
4605 56e0773d 2019-11-28 stsp free(*new_te);
4606 036813ee 2019-05-09 stsp *new_te = NULL;
4608 036813ee 2019-05-09 stsp return err;
4611 036813ee 2019-05-09 stsp static const struct got_error *
4612 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4613 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
4615 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4616 036813ee 2019-05-09 stsp char *ct_name;
4618 036813ee 2019-05-09 stsp *new_te = NULL;
4620 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
4621 036813ee 2019-05-09 stsp if (*new_te == NULL)
4622 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
4624 036813ee 2019-05-09 stsp ct_name = basename(ct->path);
4625 036813ee 2019-05-09 stsp if (ct_name == NULL) {
4626 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", ct->path);
4627 036813ee 2019-05-09 stsp goto done;
4629 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4630 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
4631 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
4632 036813ee 2019-05-09 stsp goto done;
4635 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4637 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
4638 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4639 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4641 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4643 036813ee 2019-05-09 stsp if (err && *new_te) {
4644 56e0773d 2019-11-28 stsp free(*new_te);
4645 036813ee 2019-05-09 stsp *new_te = NULL;
4647 036813ee 2019-05-09 stsp return err;
4650 036813ee 2019-05-09 stsp static const struct got_error *
4651 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
4652 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
4654 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4655 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
4657 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4659 036813ee 2019-05-09 stsp return err;
4660 036813ee 2019-05-09 stsp if (new_pe == NULL)
4661 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
4662 036813ee 2019-05-09 stsp return NULL;
4665 afa376bf 2019-05-09 stsp static const struct got_error *
4666 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
4667 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
4669 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
4670 5f8a88c6 2019-08-03 stsp unsigned char status;
4672 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
4673 afa376bf 2019-05-09 stsp ct_path++;
4675 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4676 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
4678 5f8a88c6 2019-08-03 stsp status = ct->status;
4680 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4681 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4684 036813ee 2019-05-09 stsp static const struct got_error *
4685 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
4686 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4688 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
4689 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
4690 44d03001 2019-05-09 stsp char *te_path;
4692 44d03001 2019-05-09 stsp *modified = 0;
4694 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
4695 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
4696 44d03001 2019-05-09 stsp te->name) == -1)
4697 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4699 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4700 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4701 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
4702 44d03001 2019-05-09 stsp strlen(te_path));
4703 44d03001 2019-05-09 stsp if (*modified)
4707 44d03001 2019-05-09 stsp free(te_path);
4708 44d03001 2019-05-09 stsp return err;
4711 44d03001 2019-05-09 stsp static const struct got_error *
4712 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
4713 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
4714 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
4716 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4717 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
4719 036813ee 2019-05-09 stsp *ctp = NULL;
4721 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4722 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4723 036813ee 2019-05-09 stsp char *ct_name = NULL;
4724 036813ee 2019-05-09 stsp int path_matches;
4726 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4727 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
4728 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
4729 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
4732 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
4733 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
4737 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4740 036813ee 2019-05-09 stsp err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4742 036813ee 2019-05-09 stsp return err;
4743 036813ee 2019-05-09 stsp if (!path_matches)
4746 036813ee 2019-05-09 stsp ct_name = basename(pe->path);
4747 036813ee 2019-05-09 stsp if (ct_name == NULL)
4748 638f9024 2019-05-13 stsp return got_error_from_errno2("basename", pe->path);
4750 036813ee 2019-05-09 stsp if (strcmp(te->name, ct_name) != 0)
4753 036813ee 2019-05-09 stsp *ctp = ct;
4757 2b496619 2019-07-10 stsp return err;
4760 2b496619 2019-07-10 stsp static const struct got_error *
4761 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4762 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
4763 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
4764 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
4765 2b496619 2019-07-10 stsp struct got_repository *repo)
4767 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
4768 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
4769 2b496619 2019-07-10 stsp char *subtree_path;
4770 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
4771 ba580f68 2020-03-22 stsp int nentries;
4773 2b496619 2019-07-10 stsp *new_tep = NULL;
4775 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4776 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
4777 2b496619 2019-07-10 stsp child_path) == -1)
4778 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
4780 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
4781 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
4782 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
4783 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
4785 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4786 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
4787 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
4788 2b496619 2019-07-10 stsp goto done;
4790 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
4791 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
4792 2b496619 2019-07-10 stsp if (err) {
4793 56e0773d 2019-11-28 stsp free(new_te);
4794 2b496619 2019-07-10 stsp goto done;
4796 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
4799 2b496619 2019-07-10 stsp free(subtree_path);
4800 2b496619 2019-07-10 stsp if (err == NULL)
4801 2b496619 2019-07-10 stsp *new_tep = new_te;
4802 036813ee 2019-05-09 stsp return err;
4805 036813ee 2019-05-09 stsp static const struct got_error *
4806 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
4807 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
4808 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
4809 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4810 036813ee 2019-05-09 stsp struct got_repository *repo)
4812 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4813 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
4814 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
4815 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
4817 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
4818 ba580f68 2020-03-22 stsp *nentries = 0;
4820 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
4821 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4822 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4823 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
4825 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
4826 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
4827 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
4830 036813ee 2019-05-09 stsp if (!got_path_is_child(pe->path, path_base_tree,
4831 2f51b5b3 2019-05-09 stsp strlen(path_base_tree)))
4834 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4835 036813ee 2019-05-09 stsp pe->path);
4837 036813ee 2019-05-09 stsp goto done;
4839 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
4840 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
4841 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
4843 036813ee 2019-05-09 stsp goto done;
4844 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
4846 afa376bf 2019-05-09 stsp goto done;
4847 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
4848 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
4850 2f51b5b3 2019-05-09 stsp goto done;
4851 ba580f68 2020-03-22 stsp (*nentries)++;
4853 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
4854 2b496619 2019-07-10 stsp if (base_tree == NULL ||
4855 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
4856 2b496619 2019-07-10 stsp == NULL) {
4857 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
4858 2b496619 2019-07-10 stsp child_path, path_base_tree,
4859 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
4862 2b496619 2019-07-10 stsp goto done;
4863 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
4865 2b496619 2019-07-10 stsp goto done;
4866 ba580f68 2020-03-22 stsp (*nentries)++;
4871 2f51b5b3 2019-05-09 stsp if (base_tree) {
4872 56e0773d 2019-11-28 stsp int i, nbase_entries;
4873 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
4874 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
4875 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
4876 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4878 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
4879 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
4880 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
4881 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
4883 63c5ca5d 2019-08-24 stsp goto done;
4884 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
4886 63c5ca5d 2019-08-24 stsp goto done;
4887 ba580f68 2020-03-22 stsp (*nentries)++;
4891 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
4892 44d03001 2019-05-09 stsp int modified;
4893 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
4895 036813ee 2019-05-09 stsp goto done;
4896 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
4897 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
4899 2f51b5b3 2019-05-09 stsp goto done;
4900 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
4901 44d03001 2019-05-09 stsp if (modified) {
4902 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
4903 ba580f68 2020-03-22 stsp int nsubentries;
4904 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
4905 ba580f68 2020-03-22 stsp &nsubentries, te,
4906 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
4907 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
4909 44d03001 2019-05-09 stsp goto done;
4910 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
4911 ba580f68 2020-03-22 stsp /* All entries were deleted. */
4912 ba580f68 2020-03-22 stsp free(new_id);
4915 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
4916 56e0773d 2019-11-28 stsp sizeof(new_te->id));
4917 56e0773d 2019-11-28 stsp free(new_id);
4919 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
4921 036813ee 2019-05-09 stsp goto done;
4922 ba580f68 2020-03-22 stsp (*nentries)++;
4926 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
4927 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
4929 f66c734c 2019-09-22 stsp goto done;
4931 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
4932 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
4933 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
4934 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
4935 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
4936 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
4938 2f51b5b3 2019-05-09 stsp goto done;
4939 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
4941 2f51b5b3 2019-05-09 stsp goto done;
4942 ba580f68 2020-03-22 stsp (*nentries)++;
4944 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
4945 b416585c 2019-05-13 stsp status_arg);
4947 afa376bf 2019-05-09 stsp goto done;
4949 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
4950 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
4952 2f51b5b3 2019-05-09 stsp goto done;
4953 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
4955 2f51b5b3 2019-05-09 stsp goto done;
4956 ba580f68 2020-03-22 stsp (*nentries)++;
4961 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
4962 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
4964 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
4965 ed175427 2019-05-09 stsp return err;
4968 ebf99748 2019-05-09 stsp static const struct got_error *
4969 2e1fa222 2020-07-23 stsp reinstall_symlink_after_commit(int *is_bad_symlink, struct got_commitable *ct,
4970 2e1fa222 2020-07-23 stsp struct got_object_id *new_base_commit_id, struct got_worktree *worktree,
4971 2e1fa222 2020-07-23 stsp struct got_repository *repo)
4973 2e1fa222 2020-07-23 stsp const struct got_error *err = NULL;
4974 2e1fa222 2020-07-23 stsp struct got_blob_object *blob = NULL;
4975 2e1fa222 2020-07-23 stsp struct got_object_id *tree_id = NULL;
4976 2e1fa222 2020-07-23 stsp char *tree_path = NULL;
4977 2e1fa222 2020-07-23 stsp struct got_tree_object *tree = NULL;
4978 2e1fa222 2020-07-23 stsp struct got_tree_entry *te;
4979 2e1fa222 2020-07-23 stsp char *entry_name;
4981 2e1fa222 2020-07-23 stsp err = got_object_open_as_blob(&blob, repo, ct->blob_id, PATH_MAX);
4983 2e1fa222 2020-07-23 stsp return err;
4985 2e1fa222 2020-07-23 stsp err = got_path_dirname(&tree_path, ct->in_repo_path);
4986 2e1fa222 2020-07-23 stsp if (err) {
4987 2e1fa222 2020-07-23 stsp if (err->code != GOT_ERR_BAD_PATH)
4988 2e1fa222 2020-07-23 stsp goto done;
4989 2e1fa222 2020-07-23 stsp err = got_object_id_by_path(&tree_id, repo,
4990 2e1fa222 2020-07-23 stsp new_base_commit_id, "");
4992 2e1fa222 2020-07-23 stsp goto done;
4994 2e1fa222 2020-07-23 stsp err = got_object_id_by_path(&tree_id, repo,
4995 2e1fa222 2020-07-23 stsp new_base_commit_id, tree_path);
4997 2e1fa222 2020-07-23 stsp goto done;
5000 2e1fa222 2020-07-23 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
5002 2e1fa222 2020-07-23 stsp goto done;
5004 2e1fa222 2020-07-23 stsp entry_name = basename(ct->path);
5005 2e1fa222 2020-07-23 stsp if (entry_name == NULL) {
5006 2e1fa222 2020-07-23 stsp err = got_error_from_errno2("basename", ct->path);
5007 2e1fa222 2020-07-23 stsp goto done;
5010 2e1fa222 2020-07-23 stsp te = got_object_tree_find_entry(tree, entry_name);
5011 2e1fa222 2020-07-23 stsp if (te == NULL) {
5012 2e1fa222 2020-07-23 stsp err = got_error_path(ct->path, GOT_ERR_NO_TREE_ENTRY);
5013 2e1fa222 2020-07-23 stsp goto done;
5016 2e1fa222 2020-07-23 stsp err = install_symlink(is_bad_symlink, worktree, ct->ondisk_path,
5017 2e1fa222 2020-07-23 stsp ct->path, blob, 0, 0, repo, NULL, NULL);
5020 2e1fa222 2020-07-23 stsp got_object_blob_close(blob);
5022 2e1fa222 2020-07-23 stsp got_object_tree_close(tree);
5023 2e1fa222 2020-07-23 stsp free(tree_id);
5024 2e1fa222 2020-07-23 stsp free(tree_path);
5025 2e1fa222 2020-07-23 stsp return err;
5029 2e1fa222 2020-07-23 stsp * After comitting a symlink we have a chance to convert "bad" symlinks
5030 2e1fa222 2020-07-23 stsp * (those which point outside the work tree or into .got) to regular files.
5031 2e1fa222 2020-07-23 stsp * This way, the post-commit work tree state matches a fresh checkout of
5032 2e1fa222 2020-07-23 stsp * the tree which was just committed. We also mark such newly committed
5033 2e1fa222 2020-07-23 stsp * symlinks as "bad" in the work tree's fileindex.
5035 2e1fa222 2020-07-23 stsp static const struct got_error *
5036 2e1fa222 2020-07-23 stsp reinstall_symlinks_after_commit(struct got_pathlist_head *commitable_paths,
5037 2e1fa222 2020-07-23 stsp struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5038 2e1fa222 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo)
5040 2e1fa222 2020-07-23 stsp const struct got_error *err = NULL;
5041 2e1fa222 2020-07-23 stsp struct got_pathlist_entry *pe;
5043 2e1fa222 2020-07-23 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5044 2e1fa222 2020-07-23 stsp struct got_commitable *ct = pe->data;
5045 2e1fa222 2020-07-23 stsp struct got_fileindex_entry *ie;
5046 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
5048 2e1fa222 2020-07-23 stsp if (!S_ISLNK(get_ct_file_mode(ct)))
5051 2e1fa222 2020-07-23 stsp err = reinstall_symlink_after_commit(&is_bad_symlink,
5052 2e1fa222 2020-07-23 stsp ct, new_base_commit_id, worktree, repo);
5055 2e1fa222 2020-07-23 stsp ie = got_fileindex_entry_get(fileindex, ct->path,
5056 2e1fa222 2020-07-23 stsp strlen(ct->path));
5057 2e1fa222 2020-07-23 stsp if (ie && is_bad_symlink) {
5058 2e1fa222 2020-07-23 stsp err = got_fileindex_entry_filetype_set(ie,
5059 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
5065 2e1fa222 2020-07-23 stsp return err;
5068 2e1fa222 2020-07-23 stsp static const struct got_error *
5069 ebf99748 2019-05-09 stsp update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5070 72fd46fa 2019-09-06 stsp struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5071 72fd46fa 2019-09-06 stsp int have_staged_files)
5073 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5074 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5076 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5077 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5078 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5080 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5082 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5083 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5084 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5085 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5086 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5087 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5088 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5089 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5090 5f8a88c6 2019-08-03 stsp ct->ondisk_path, ct->staged_blob_id->sha1,
5091 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5092 72fd46fa 2019-09-06 stsp !have_staged_files);
5094 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5095 e75eb4da 2019-05-10 stsp ct->ondisk_path, ct->blob_id->sha1,
5096 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5097 72fd46fa 2019-09-06 stsp !have_staged_files);
5099 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5102 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ct->ondisk_path,
5103 3969253a 2020-03-07 stsp ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5104 3969253a 2020-03-07 stsp if (err) {
5105 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5108 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5109 3969253a 2020-03-07 stsp if (err) {
5110 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5115 d56d26ce 2019-05-10 stsp return err;
5119 d56d26ce 2019-05-10 stsp static const struct got_error *
5120 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5121 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5122 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5123 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5124 735ef5ac 2019-08-03 stsp int ood_errcode)
5126 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5127 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5129 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5130 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5131 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5132 1a36436d 2019-06-10 stsp return NULL;
5134 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5135 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5137 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5138 735ef5ac 2019-08-03 stsp in_repo_path);
5139 9bead371 2019-07-28 stsp if (err) {
5140 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5141 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5142 1a36436d 2019-06-10 stsp goto done;
5143 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5144 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5146 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5147 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5148 735ef5ac 2019-08-03 stsp in_repo_path);
5149 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5150 1a36436d 2019-06-10 stsp goto done;
5151 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5155 1a36436d 2019-06-10 stsp return err;
5158 c4296144 2019-05-09 stsp const struct got_error *
5159 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5160 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5161 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id, struct got_worktree *worktree,
5162 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
5163 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5164 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5165 afa376bf 2019-05-09 stsp struct got_repository *repo)
5167 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5168 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5169 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5170 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5171 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5172 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5173 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5174 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5175 ba580f68 2020-03-22 stsp int nentries;
5176 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5177 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5178 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5180 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5182 de18fc63 2019-05-09 stsp SIMPLEQ_INIT(&parent_ids);
5184 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5186 588edf97 2019-05-10 stsp goto done;
5188 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5190 588edf97 2019-05-10 stsp goto done;
5192 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5193 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5198 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5199 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5203 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5204 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5205 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5206 cf066bf8 2019-05-09 stsp char *ondisk_path;
5208 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5209 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5210 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5213 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5214 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5215 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5218 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5219 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5220 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5221 cf066bf8 2019-05-09 stsp goto done;
5223 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5224 2e1fa222 2020-07-23 stsp free(ondisk_path);
5226 cf066bf8 2019-05-09 stsp goto done;
5229 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5230 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5231 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5233 036813ee 2019-05-09 stsp goto done;
5235 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5237 de18fc63 2019-05-09 stsp goto done;
5238 de18fc63 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5239 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5240 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5241 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
5242 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5243 33ad4cbe 2019-05-12 jcs free(logmsg);
5245 9d40349a 2019-05-09 stsp goto done;
5247 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5248 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5249 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5250 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5251 09f5bd90 2019-05-09 stsp goto done;
5253 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
5254 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5256 09f5bd90 2019-05-09 stsp goto done;
5257 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5259 ebf99748 2019-05-09 stsp goto done;
5260 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5261 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5262 09f5bd90 2019-05-09 stsp goto done;
5264 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
5265 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
5267 f2c16586 2019-05-09 stsp goto done;
5268 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
5270 f2c16586 2019-05-09 stsp goto done;
5272 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5274 09f5bd90 2019-05-09 stsp goto done;
5276 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
5278 ebf99748 2019-05-09 stsp goto done;
5280 588edf97 2019-05-10 stsp if (head_tree)
5281 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
5282 588edf97 2019-05-10 stsp if (head_commit)
5283 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
5284 09f5bd90 2019-05-09 stsp free(head_commit_id2);
5285 2f17228e 2019-05-12 stsp if (head_ref2) {
5286 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
5287 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
5288 2f17228e 2019-05-12 stsp err = unlockerr;
5289 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
5291 39cd0ff6 2019-07-12 stsp return err;
5294 5c1e53bc 2019-07-28 stsp static const struct got_error *
5295 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
5296 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
5298 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
5299 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
5301 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
5302 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
5303 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
5305 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
5306 5c1e53bc 2019-07-28 stsp ct_path++;
5308 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
5309 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
5313 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
5314 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
5316 5c1e53bc 2019-07-28 stsp return NULL;
5319 f0b75401 2019-08-03 stsp static const struct got_error *
5320 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
5322 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
5324 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5325 f0b75401 2019-08-03 stsp *have_staged_files = 1;
5326 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
5329 f0b75401 2019-08-03 stsp return NULL;
5332 5f8a88c6 2019-08-03 stsp static const struct got_error *
5333 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
5334 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
5336 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
5337 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
5339 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5340 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
5342 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5343 5f8a88c6 2019-08-03 stsp if (ie == NULL)
5344 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5345 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5346 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
5347 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
5350 5f8a88c6 2019-08-03 stsp return NULL;
5353 39cd0ff6 2019-07-12 stsp const struct got_error *
5354 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
5355 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
5356 39cd0ff6 2019-07-12 stsp const char *author, const char *committer,
5357 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5358 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
5359 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
5361 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5362 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
5363 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
5364 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5365 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5366 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
5367 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
5368 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5369 f0b75401 2019-08-03 stsp int have_staged_files = 0;
5371 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
5373 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5375 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
5377 39cd0ff6 2019-07-12 stsp goto done;
5379 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5381 39cd0ff6 2019-07-12 stsp goto done;
5383 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5385 39cd0ff6 2019-07-12 stsp goto done;
5387 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5389 39cd0ff6 2019-07-12 stsp goto done;
5391 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5392 5f8a88c6 2019-08-03 stsp &have_staged_files);
5393 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
5394 5f8a88c6 2019-08-03 stsp goto done;
5395 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
5396 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
5398 5f8a88c6 2019-08-03 stsp goto done;
5401 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5402 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
5403 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
5404 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
5405 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5406 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5407 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5409 5c1e53bc 2019-07-28 stsp goto done;
5412 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5413 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5414 39cd0ff6 2019-07-12 stsp goto done;
5417 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5418 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
5420 5c1e53bc 2019-07-28 stsp goto done;
5423 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5424 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5425 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5427 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5428 f0b75401 2019-08-03 stsp ct_path++;
5429 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5430 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5431 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5433 b50cabdf 2019-07-12 stsp goto done;
5437 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5438 5c1e53bc 2019-07-28 stsp head_commit_id, worktree, author, committer,
5439 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5441 39cd0ff6 2019-07-12 stsp goto done;
5443 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5444 72fd46fa 2019-09-06 stsp fileindex, have_staged_files);
5445 2e1fa222 2020-07-23 stsp if (err == NULL) {
5446 2e1fa222 2020-07-23 stsp err = reinstall_symlinks_after_commit(&commitable_paths,
5447 2e1fa222 2020-07-23 stsp *new_commit_id, fileindex, worktree, repo);
5449 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5450 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5451 39cd0ff6 2019-07-12 stsp err = sync_err;
5453 39cd0ff6 2019-07-12 stsp if (fileindex)
5454 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5455 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5456 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5457 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5458 39cd0ff6 2019-07-12 stsp err = unlockerr;
5459 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5460 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5461 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5463 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5464 c4296144 2019-05-09 stsp return err;
5467 8656d6c4 2019-05-20 stsp const char *
5468 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5470 8656d6c4 2019-05-20 stsp return ct->path;
5473 8656d6c4 2019-05-20 stsp unsigned int
5474 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
5476 8656d6c4 2019-05-20 stsp return ct->status;
5479 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
5480 818c7501 2019-07-11 stsp struct got_worktree *worktree;
5481 818c7501 2019-07-11 stsp struct got_repository *repo;
5484 818c7501 2019-07-11 stsp static const struct got_error *
5485 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5487 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5488 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
5489 818c7501 2019-07-11 stsp unsigned char status;
5490 818c7501 2019-07-11 stsp struct stat sb;
5491 818c7501 2019-07-11 stsp char *ondisk_path;
5493 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
5494 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5495 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
5496 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
5498 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5500 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
5502 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
5503 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5504 818c7501 2019-07-11 stsp free(ondisk_path);
5506 818c7501 2019-07-11 stsp return err;
5508 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
5509 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
5510 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5511 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5513 818c7501 2019-07-11 stsp return NULL;
5516 818c7501 2019-07-11 stsp const struct got_error *
5517 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5518 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5519 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
5520 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5522 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5523 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5524 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
5525 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
5526 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
5527 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5528 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
5530 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5531 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5532 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5534 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
5536 818c7501 2019-07-11 stsp return err;
5538 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5540 818c7501 2019-07-11 stsp goto done;
5542 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
5543 818c7501 2019-07-11 stsp ok_arg.repo = repo;
5544 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5547 818c7501 2019-07-11 stsp goto done;
5549 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5551 818c7501 2019-07-11 stsp goto done;
5553 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5555 818c7501 2019-07-11 stsp goto done;
5557 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5559 818c7501 2019-07-11 stsp goto done;
5561 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5564 e51d7b55 2020-01-04 stsp goto done;
5566 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5568 818c7501 2019-07-11 stsp goto done;
5569 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5570 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5571 e51d7b55 2020-01-04 stsp goto done;
5574 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
5575 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
5577 818c7501 2019-07-11 stsp goto done;
5578 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
5580 818c7501 2019-07-11 stsp goto done;
5582 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
5584 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5586 818c7501 2019-07-11 stsp goto done;
5588 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
5590 818c7501 2019-07-11 stsp goto done;
5592 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
5593 818c7501 2019-07-11 stsp worktree->base_commit_id);
5595 818c7501 2019-07-11 stsp goto done;
5596 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
5598 818c7501 2019-07-11 stsp goto done;
5600 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
5602 818c7501 2019-07-11 stsp goto done;
5604 818c7501 2019-07-11 stsp free(fileindex_path);
5605 818c7501 2019-07-11 stsp free(tmp_branch_name);
5606 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
5607 818c7501 2019-07-11 stsp free(branch_ref_name);
5608 818c7501 2019-07-11 stsp if (branch_ref)
5609 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5610 818c7501 2019-07-11 stsp if (wt_branch)
5611 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
5612 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
5613 818c7501 2019-07-11 stsp if (err) {
5614 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
5615 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
5616 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5618 818c7501 2019-07-11 stsp if (*tmp_branch) {
5619 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5620 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5622 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5623 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5624 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5626 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
5628 818c7501 2019-07-11 stsp return err;
5631 818c7501 2019-07-11 stsp const struct got_error *
5632 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
5633 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5634 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
5635 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
5637 818c7501 2019-07-11 stsp const struct got_error *err;
5638 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5639 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5640 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5641 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
5642 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
5644 818c7501 2019-07-11 stsp *commit_id = NULL;
5645 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
5646 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
5647 3e3a69f1 2019-07-25 stsp *branch = NULL;
5648 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5650 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
5652 3e3a69f1 2019-07-25 stsp return err;
5654 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5656 f032f1f7 2019-08-04 stsp goto done;
5658 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5659 f032f1f7 2019-08-04 stsp &have_staged_files);
5660 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
5661 f032f1f7 2019-08-04 stsp goto done;
5662 f032f1f7 2019-08-04 stsp if (have_staged_files) {
5663 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
5664 3e3a69f1 2019-07-25 stsp goto done;
5667 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5669 f032f1f7 2019-08-04 stsp goto done;
5671 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5673 818c7501 2019-07-11 stsp goto done;
5675 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5677 818c7501 2019-07-11 stsp goto done;
5679 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5681 818c7501 2019-07-11 stsp goto done;
5683 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5685 818c7501 2019-07-11 stsp goto done;
5687 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
5688 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
5690 818c7501 2019-07-11 stsp goto done;
5692 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5694 818c7501 2019-07-11 stsp goto done;
5696 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
5698 818c7501 2019-07-11 stsp goto done;
5700 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
5701 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
5703 818c7501 2019-07-11 stsp goto done;
5705 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5707 818c7501 2019-07-11 stsp goto done;
5709 818c7501 2019-07-11 stsp free(commit_ref_name);
5710 818c7501 2019-07-11 stsp free(branch_ref_name);
5711 3e3a69f1 2019-07-25 stsp free(fileindex_path);
5712 818c7501 2019-07-11 stsp if (commit_ref)
5713 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
5714 818c7501 2019-07-11 stsp if (branch_ref)
5715 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5716 818c7501 2019-07-11 stsp if (err) {
5717 818c7501 2019-07-11 stsp free(*commit_id);
5718 818c7501 2019-07-11 stsp *commit_id = NULL;
5719 818c7501 2019-07-11 stsp if (*tmp_branch) {
5720 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5721 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5723 818c7501 2019-07-11 stsp if (*new_base_branch) {
5724 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
5725 818c7501 2019-07-11 stsp *new_base_branch = NULL;
5727 818c7501 2019-07-11 stsp if (*branch) {
5728 818c7501 2019-07-11 stsp got_ref_close(*branch);
5729 818c7501 2019-07-11 stsp *branch = NULL;
5731 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5732 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5733 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5735 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
5737 818c7501 2019-07-11 stsp return err;
5740 818c7501 2019-07-11 stsp const struct got_error *
5741 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5743 818c7501 2019-07-11 stsp const struct got_error *err;
5744 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
5746 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5748 818c7501 2019-07-11 stsp return err;
5750 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5751 818c7501 2019-07-11 stsp free(tmp_branch_name);
5752 818c7501 2019-07-11 stsp return NULL;
5755 818c7501 2019-07-11 stsp static const struct got_error *
5756 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5757 818c7501 2019-07-11 stsp char **logmsg, void *arg)
5759 0ebf8283 2019-07-24 stsp *logmsg = arg;
5760 818c7501 2019-07-11 stsp return NULL;
5763 818c7501 2019-07-11 stsp static const struct got_error *
5764 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5765 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
5766 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5767 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5769 818c7501 2019-07-11 stsp return NULL;
5772 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
5773 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
5774 01757395 2019-07-12 stsp void *progress_arg;
5775 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
5778 01757395 2019-07-12 stsp static const struct got_error *
5779 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
5781 01757395 2019-07-12 stsp const struct got_error *err;
5782 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
5784 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
5786 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
5788 01757395 2019-07-12 stsp return err;
5790 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
5791 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
5792 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
5793 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
5794 01757395 2019-07-12 stsp return NULL;
5796 01757395 2019-07-12 stsp p = strdup(path);
5797 01757395 2019-07-12 stsp if (p == NULL)
5798 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
5800 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5801 01757395 2019-07-12 stsp if (err || new == NULL)
5803 01757395 2019-07-12 stsp return err;
5807 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5809 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
5811 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
5812 01757395 2019-07-12 stsp free((char *)pe->path);
5814 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
5817 0ebf8283 2019-07-24 stsp static const struct got_error *
5818 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5819 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
5821 818c7501 2019-07-11 stsp const struct got_error *err;
5822 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
5824 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5825 818c7501 2019-07-11 stsp if (err) {
5826 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
5827 818c7501 2019-07-11 stsp goto done;
5828 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5830 818c7501 2019-07-11 stsp goto done;
5831 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
5833 818c7501 2019-07-11 stsp goto done;
5834 de05890f 2020-03-05 stsp } else if (is_rebase) {
5835 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
5838 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
5840 818c7501 2019-07-11 stsp goto done;
5841 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
5842 818c7501 2019-07-11 stsp free(stored_id);
5843 818c7501 2019-07-11 stsp if (cmp != 0) {
5844 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
5845 818c7501 2019-07-11 stsp goto done;
5849 0ebf8283 2019-07-24 stsp if (commit_ref)
5850 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
5851 0ebf8283 2019-07-24 stsp return err;
5854 0ebf8283 2019-07-24 stsp static const struct got_error *
5855 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
5856 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
5857 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5858 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
5859 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5860 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
5862 0ebf8283 2019-07-24 stsp const struct got_error *err;
5863 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
5864 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
5865 3e3a69f1 2019-07-25 stsp char *fileindex_path;
5867 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
5869 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
5871 0ebf8283 2019-07-24 stsp return err;
5873 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
5874 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
5875 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
5876 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
5877 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
5878 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
5879 818c7501 2019-07-11 stsp if (commit_ref)
5880 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
5881 818c7501 2019-07-11 stsp return err;
5884 818c7501 2019-07-11 stsp const struct got_error *
5885 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5886 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5887 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5888 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
5889 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5890 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
5892 0ebf8283 2019-07-24 stsp const struct got_error *err;
5893 0ebf8283 2019-07-24 stsp char *commit_ref_name;
5895 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5897 0ebf8283 2019-07-24 stsp return err;
5899 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5901 0ebf8283 2019-07-24 stsp goto done;
5903 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5904 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
5905 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
5907 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5908 0ebf8283 2019-07-24 stsp return err;
5911 0ebf8283 2019-07-24 stsp const struct got_error *
5912 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5913 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5914 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5915 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
5916 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5917 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
5919 0ebf8283 2019-07-24 stsp const struct got_error *err;
5920 0ebf8283 2019-07-24 stsp char *commit_ref_name;
5922 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5924 0ebf8283 2019-07-24 stsp return err;
5926 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5928 0ebf8283 2019-07-24 stsp goto done;
5930 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5931 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
5932 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
5934 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5935 0ebf8283 2019-07-24 stsp return err;
5938 0ebf8283 2019-07-24 stsp static const struct got_error *
5939 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
5940 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5941 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5942 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5943 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
5945 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
5946 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5947 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5948 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
5949 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
5950 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5951 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
5953 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5954 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
5956 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
5958 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
5960 3e3a69f1 2019-07-25 stsp return err;
5962 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5963 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
5964 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
5965 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
5967 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
5968 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
5969 01757395 2019-07-12 stsp * TODO: Ideally, merged_paths would contain a list of commitables
5970 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
5972 01757395 2019-07-12 stsp if (merged_paths) {
5973 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
5974 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
5975 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
5976 f2a9dc41 2019-12-13 tracey repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5979 01757395 2019-07-12 stsp goto done;
5982 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
5983 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5985 01757395 2019-07-12 stsp goto done;
5988 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5989 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
5990 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
5992 a0e95631 2019-07-12 stsp goto done;
5993 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5994 a0e95631 2019-07-12 stsp goto done;
5997 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5999 edd02c5e 2019-07-11 stsp goto done;
6001 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6003 818c7501 2019-07-11 stsp goto done;
6005 5943eee2 2019-08-13 stsp if (new_logmsg) {
6006 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6007 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6008 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6009 5943eee2 2019-08-13 stsp goto done;
6012 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6014 5943eee2 2019-08-13 stsp goto done;
6017 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6018 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6019 5c1e53bc 2019-07-28 stsp worktree, got_object_commit_get_author(orig_commit),
6020 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
6021 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6023 a0e95631 2019-07-12 stsp goto done;
6025 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6027 a0e95631 2019-07-12 stsp goto done;
6029 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6031 a0e95631 2019-07-12 stsp goto done;
6033 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6034 72fd46fa 2019-09-06 stsp fileindex, 0);
6035 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6036 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6037 a0e95631 2019-07-12 stsp err = sync_err;
6039 a0e95631 2019-07-12 stsp free(fileindex_path);
6040 a0e95631 2019-07-12 stsp free(head_commit_id);
6041 a0e95631 2019-07-12 stsp if (head_ref)
6042 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6043 818c7501 2019-07-11 stsp if (err) {
6044 818c7501 2019-07-11 stsp free(*new_commit_id);
6045 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6047 818c7501 2019-07-11 stsp return err;
6050 818c7501 2019-07-11 stsp const struct got_error *
6051 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6052 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6053 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6054 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6055 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6057 0ebf8283 2019-07-24 stsp const struct got_error *err;
6058 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6059 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6060 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6062 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6064 0ebf8283 2019-07-24 stsp return err;
6066 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6068 0ebf8283 2019-07-24 stsp goto done;
6069 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6071 0ebf8283 2019-07-24 stsp goto done;
6072 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6073 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6074 0ebf8283 2019-07-24 stsp goto done;
6077 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6078 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6080 0ebf8283 2019-07-24 stsp if (commit_ref)
6081 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6082 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6083 0ebf8283 2019-07-24 stsp free(commit_id);
6084 0ebf8283 2019-07-24 stsp return err;
6087 0ebf8283 2019-07-24 stsp const struct got_error *
6088 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6089 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6090 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6091 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6092 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6093 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6095 0ebf8283 2019-07-24 stsp const struct got_error *err;
6096 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6097 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6099 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6101 0ebf8283 2019-07-24 stsp return err;
6103 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6105 0ebf8283 2019-07-24 stsp goto done;
6107 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6108 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6110 0ebf8283 2019-07-24 stsp if (commit_ref)
6111 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6112 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6113 0ebf8283 2019-07-24 stsp return err;
6116 0ebf8283 2019-07-24 stsp const struct got_error *
6117 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6118 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6120 3e3a69f1 2019-07-25 stsp if (fileindex)
6121 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6122 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6125 69844fba 2019-07-11 stsp static const struct got_error *
6126 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6128 69844fba 2019-07-11 stsp const struct got_error *err;
6129 69844fba 2019-07-11 stsp struct got_reference *ref;
6131 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6132 69844fba 2019-07-11 stsp if (err) {
6133 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6134 69844fba 2019-07-11 stsp return NULL;
6135 69844fba 2019-07-11 stsp return err;
6138 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6139 69844fba 2019-07-11 stsp got_ref_close(ref);
6140 69844fba 2019-07-11 stsp return err;
6143 69844fba 2019-07-11 stsp static const struct got_error *
6144 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6146 69844fba 2019-07-11 stsp const struct got_error *err;
6147 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6148 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6150 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6152 69844fba 2019-07-11 stsp goto done;
6153 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6155 69844fba 2019-07-11 stsp goto done;
6157 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6159 69844fba 2019-07-11 stsp goto done;
6160 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6162 69844fba 2019-07-11 stsp goto done;
6164 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6166 69844fba 2019-07-11 stsp goto done;
6167 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6169 69844fba 2019-07-11 stsp goto done;
6171 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6173 69844fba 2019-07-11 stsp goto done;
6174 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6176 69844fba 2019-07-11 stsp goto done;
6179 69844fba 2019-07-11 stsp free(tmp_branch_name);
6180 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6181 69844fba 2019-07-11 stsp free(branch_ref_name);
6182 69844fba 2019-07-11 stsp free(commit_ref_name);
6183 69844fba 2019-07-11 stsp return err;
6186 818c7501 2019-07-11 stsp const struct got_error *
6187 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
6188 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6189 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6190 818c7501 2019-07-11 stsp struct got_repository *repo)
6192 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
6193 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
6195 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6197 818c7501 2019-07-11 stsp return err;
6199 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6201 818c7501 2019-07-11 stsp goto done;
6203 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
6205 818c7501 2019-07-11 stsp goto done;
6207 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
6209 818c7501 2019-07-11 stsp goto done;
6211 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
6213 3e3a69f1 2019-07-25 stsp if (fileindex)
6214 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6215 818c7501 2019-07-11 stsp free(new_head_commit_id);
6216 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6217 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6218 818c7501 2019-07-11 stsp err = unlockerr;
6219 818c7501 2019-07-11 stsp return err;
6222 818c7501 2019-07-11 stsp const struct got_error *
6223 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
6224 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6225 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
6226 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6228 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
6229 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
6230 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
6231 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6232 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6233 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
6235 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6237 818c7501 2019-07-11 stsp return err;
6239 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
6240 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
6242 818c7501 2019-07-11 stsp goto done;
6244 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
6246 818c7501 2019-07-11 stsp goto done;
6249 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
6250 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
6251 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
6253 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
6255 818c7501 2019-07-11 stsp goto done;
6257 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6259 818c7501 2019-07-11 stsp goto done;
6261 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
6262 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
6264 ca355955 2019-07-12 stsp goto done;
6266 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
6268 ca355955 2019-07-12 stsp goto done;
6270 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6272 818c7501 2019-07-11 stsp goto done;
6274 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6275 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6276 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6277 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6278 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6279 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6280 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6281 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6282 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6284 1f1abb7e 2019-08-08 stsp goto sync;
6286 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6287 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
6289 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6290 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
6291 ca355955 2019-07-12 stsp err = sync_err;
6293 818c7501 2019-07-11 stsp got_ref_close(resolved);
6294 a3a2faf2 2019-07-12 stsp free(tree_id);
6295 818c7501 2019-07-11 stsp free(commit_id);
6296 0ebf8283 2019-07-24 stsp if (fileindex)
6297 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
6298 0ebf8283 2019-07-24 stsp free(fileindex_path);
6300 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6301 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6302 0ebf8283 2019-07-24 stsp err = unlockerr;
6303 0ebf8283 2019-07-24 stsp return err;
6306 0ebf8283 2019-07-24 stsp const struct got_error *
6307 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6308 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6309 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
6310 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6312 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6313 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6314 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
6315 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
6316 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6317 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
6318 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
6319 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6321 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6322 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6323 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6324 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6326 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6328 0ebf8283 2019-07-24 stsp return err;
6330 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6332 0ebf8283 2019-07-24 stsp goto done;
6334 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
6335 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
6336 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6339 0ebf8283 2019-07-24 stsp goto done;
6341 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6343 0ebf8283 2019-07-24 stsp goto done;
6345 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6347 0ebf8283 2019-07-24 stsp goto done;
6349 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6350 0ebf8283 2019-07-24 stsp worktree);
6352 0ebf8283 2019-07-24 stsp goto done;
6354 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6357 0ebf8283 2019-07-24 stsp goto done;
6359 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6361 0ebf8283 2019-07-24 stsp goto done;
6363 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
6365 0ebf8283 2019-07-24 stsp goto done;
6367 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6368 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6370 0ebf8283 2019-07-24 stsp goto done;
6371 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
6373 0ebf8283 2019-07-24 stsp goto done;
6374 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6375 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
6376 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
6377 0ebf8283 2019-07-24 stsp goto done;
6380 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6381 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6383 0ebf8283 2019-07-24 stsp goto done;
6384 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
6386 0ebf8283 2019-07-24 stsp goto done;
6388 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6390 0ebf8283 2019-07-24 stsp goto done;
6392 0ebf8283 2019-07-24 stsp free(fileindex_path);
6393 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6394 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6395 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6396 0ebf8283 2019-07-24 stsp if (wt_branch)
6397 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
6398 0ebf8283 2019-07-24 stsp if (err) {
6399 0ebf8283 2019-07-24 stsp if (*branch_ref) {
6400 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
6401 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6403 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6404 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6405 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6407 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6408 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6409 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6410 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6412 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
6414 0ebf8283 2019-07-24 stsp return err;
6417 0ebf8283 2019-07-24 stsp const struct got_error *
6418 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
6419 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6421 3e3a69f1 2019-07-25 stsp if (fileindex)
6422 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6423 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
6426 0ebf8283 2019-07-24 stsp const struct got_error *
6427 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
6428 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
6430 0ebf8283 2019-07-24 stsp const struct got_error *err;
6431 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6433 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6435 0ebf8283 2019-07-24 stsp return err;
6437 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6438 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6439 0ebf8283 2019-07-24 stsp return NULL;
6442 0ebf8283 2019-07-24 stsp const struct got_error *
6443 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
6444 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
6445 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6446 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6448 0ebf8283 2019-07-24 stsp const struct got_error *err;
6449 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6450 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6451 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6452 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6453 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6454 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6456 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6457 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6458 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6459 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6461 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6463 3e3a69f1 2019-07-25 stsp return err;
6465 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6467 f032f1f7 2019-08-04 stsp goto done;
6469 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6470 f032f1f7 2019-08-04 stsp &have_staged_files);
6471 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6472 f032f1f7 2019-08-04 stsp goto done;
6473 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6474 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6475 3e3a69f1 2019-07-25 stsp goto done;
6478 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6480 f032f1f7 2019-08-04 stsp goto done;
6482 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6484 0ebf8283 2019-07-24 stsp goto done;
6486 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6488 0ebf8283 2019-07-24 stsp goto done;
6490 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6491 0ebf8283 2019-07-24 stsp worktree);
6493 0ebf8283 2019-07-24 stsp goto done;
6495 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6497 0ebf8283 2019-07-24 stsp goto done;
6499 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6501 0ebf8283 2019-07-24 stsp goto done;
6502 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6504 0ebf8283 2019-07-24 stsp goto done;
6506 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6508 0ebf8283 2019-07-24 stsp goto done;
6509 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6511 0ebf8283 2019-07-24 stsp goto done;
6513 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6515 0ebf8283 2019-07-24 stsp goto done;
6517 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6518 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6519 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6520 0ebf8283 2019-07-24 stsp if (commit_ref)
6521 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6522 0ebf8283 2019-07-24 stsp if (base_commit_ref)
6523 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
6524 0ebf8283 2019-07-24 stsp if (err) {
6525 0ebf8283 2019-07-24 stsp free(*commit_id);
6526 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6527 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6528 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6529 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6530 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6531 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6533 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6534 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6535 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6537 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
6539 0ebf8283 2019-07-24 stsp return err;
6542 0ebf8283 2019-07-24 stsp static const struct got_error *
6543 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6545 0ebf8283 2019-07-24 stsp const struct got_error *err;
6546 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6547 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6549 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6551 0ebf8283 2019-07-24 stsp goto done;
6552 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
6554 0ebf8283 2019-07-24 stsp goto done;
6556 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6557 0ebf8283 2019-07-24 stsp worktree);
6559 0ebf8283 2019-07-24 stsp goto done;
6560 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
6562 0ebf8283 2019-07-24 stsp goto done;
6564 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6566 0ebf8283 2019-07-24 stsp goto done;
6567 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
6569 0ebf8283 2019-07-24 stsp goto done;
6571 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6573 0ebf8283 2019-07-24 stsp goto done;
6574 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
6576 0ebf8283 2019-07-24 stsp goto done;
6578 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6579 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6580 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6581 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6582 0ebf8283 2019-07-24 stsp return err;
6585 0ebf8283 2019-07-24 stsp const struct got_error *
6586 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
6587 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6588 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
6589 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6591 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
6592 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6593 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6594 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
6595 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6597 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6599 0ebf8283 2019-07-24 stsp return err;
6601 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
6602 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
6604 0ebf8283 2019-07-24 stsp goto done;
6606 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
6608 0ebf8283 2019-07-24 stsp goto done;
6610 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6612 0ebf8283 2019-07-24 stsp goto done;
6614 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6615 0ebf8283 2019-07-24 stsp worktree->path_prefix);
6617 0ebf8283 2019-07-24 stsp goto done;
6619 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
6621 0ebf8283 2019-07-24 stsp goto done;
6623 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6625 0ebf8283 2019-07-24 stsp goto done;
6627 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6628 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6629 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6630 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6631 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6632 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6633 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6634 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
6635 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6637 1f1abb7e 2019-08-08 stsp goto sync;
6639 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6640 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
6642 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6643 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
6644 0ebf8283 2019-07-24 stsp err = sync_err;
6646 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
6647 0ebf8283 2019-07-24 stsp free(tree_id);
6648 818c7501 2019-07-11 stsp free(fileindex_path);
6650 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6651 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6652 818c7501 2019-07-11 stsp err = unlockerr;
6653 818c7501 2019-07-11 stsp return err;
6656 0ebf8283 2019-07-24 stsp const struct got_error *
6657 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
6658 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6659 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
6661 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr;
6662 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
6663 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6665 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6667 0ebf8283 2019-07-24 stsp return err;
6669 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
6670 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
6672 0ebf8283 2019-07-24 stsp goto done;
6674 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
6676 0ebf8283 2019-07-24 stsp goto done;
6678 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
6680 0ebf8283 2019-07-24 stsp goto done;
6682 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
6684 0ebf8283 2019-07-24 stsp goto done;
6686 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
6688 3e3a69f1 2019-07-25 stsp if (fileindex)
6689 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6690 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
6691 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6692 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6693 0ebf8283 2019-07-24 stsp err = unlockerr;
6694 0ebf8283 2019-07-24 stsp return err;
6697 0ebf8283 2019-07-24 stsp const struct got_error *
6698 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6699 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6701 0ebf8283 2019-07-24 stsp const struct got_error *err;
6702 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6704 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6706 0ebf8283 2019-07-24 stsp return err;
6708 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6710 0ebf8283 2019-07-24 stsp goto done;
6712 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
6714 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6715 2822a352 2019-10-15 stsp return err;
6718 2822a352 2019-10-15 stsp const struct got_error *
6719 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6720 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6721 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
6722 2822a352 2019-10-15 stsp struct got_repository *repo)
6724 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
6725 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
6726 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
6728 2822a352 2019-10-15 stsp *fileindex = NULL;
6729 2822a352 2019-10-15 stsp *branch_ref = NULL;
6730 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
6732 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
6734 2822a352 2019-10-15 stsp return err;
6736 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6737 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
6738 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
6739 2822a352 2019-10-15 stsp "update -b or different branch name required");
6740 2822a352 2019-10-15 stsp goto done;
6743 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6745 2822a352 2019-10-15 stsp goto done;
6747 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
6748 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
6749 2822a352 2019-10-15 stsp ok_arg.repo = repo;
6750 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6753 2822a352 2019-10-15 stsp goto done;
6755 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
6757 2822a352 2019-10-15 stsp goto done;
6759 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
6760 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
6762 2822a352 2019-10-15 stsp if (err) {
6763 2822a352 2019-10-15 stsp if (*branch_ref) {
6764 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
6765 2822a352 2019-10-15 stsp *branch_ref = NULL;
6767 2822a352 2019-10-15 stsp if (*base_branch_ref) {
6768 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
6769 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
6771 2822a352 2019-10-15 stsp if (*fileindex) {
6772 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
6773 2822a352 2019-10-15 stsp *fileindex = NULL;
6775 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
6777 0cb83759 2019-08-03 stsp return err;
6780 2822a352 2019-10-15 stsp const struct got_error *
6781 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
6782 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6783 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6784 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6785 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6787 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
6788 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
6789 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
6791 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
6793 2822a352 2019-10-15 stsp goto done;
6795 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
6797 2822a352 2019-10-15 stsp goto done;
6799 2822a352 2019-10-15 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
6800 2822a352 2019-10-15 stsp worktree->path_prefix);
6802 2822a352 2019-10-15 stsp goto done;
6804 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6806 2822a352 2019-10-15 stsp goto done;
6808 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6809 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
6811 2822a352 2019-10-15 stsp goto sync;
6813 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
6815 2822a352 2019-10-15 stsp goto sync;
6817 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
6819 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6820 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
6821 2822a352 2019-10-15 stsp err = sync_err;
6824 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
6825 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6826 2822a352 2019-10-15 stsp err = unlockerr;
6827 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
6829 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
6830 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6831 2822a352 2019-10-15 stsp err = unlockerr;
6832 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
6834 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
6835 2822a352 2019-10-15 stsp free(fileindex_path);
6836 2822a352 2019-10-15 stsp free(tree_id);
6838 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6839 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6840 2822a352 2019-10-15 stsp err = unlockerr;
6841 2822a352 2019-10-15 stsp return err;
6844 2822a352 2019-10-15 stsp const struct got_error *
6845 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
6846 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6847 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6849 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
6851 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
6853 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
6855 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
6856 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
6857 8b692cd0 2019-10-21 stsp err = unlockerr;
6858 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
6860 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
6861 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
6862 8b692cd0 2019-10-21 stsp err = unlockerr;
6863 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
6865 8b692cd0 2019-10-21 stsp return err;
6868 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
6869 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
6870 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
6871 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
6872 2db2652d 2019-08-07 stsp struct got_repository *repo;
6873 2db2652d 2019-08-07 stsp int have_changes;
6876 2db2652d 2019-08-07 stsp const struct got_error *
6877 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
6878 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
6879 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6880 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
6882 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
6883 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
6884 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
6885 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
6886 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
6887 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
6889 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
6890 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
6891 8b13ce36 2019-08-08 stsp return NULL;
6892 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
6893 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
6895 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6896 735ef5ac 2019-08-03 stsp if (ie == NULL)
6897 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6899 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6900 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6901 735ef5ac 2019-08-03 stsp relpath) == -1)
6902 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
6904 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
6905 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
6906 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
6907 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
6910 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
6911 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6912 3aa5969e 2019-08-06 stsp goto done;
6913 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
6914 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
6915 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
6916 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6917 735ef5ac 2019-08-03 stsp goto done;
6920 2db2652d 2019-08-07 stsp a->have_changes = 1;
6922 735ef5ac 2019-08-03 stsp p = in_repo_path;
6923 735ef5ac 2019-08-03 stsp while (p[0] == '/')
6925 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
6926 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
6927 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
6929 735ef5ac 2019-08-03 stsp free(in_repo_path);
6930 dc424a06 2019-08-07 stsp return err;
6933 2db2652d 2019-08-07 stsp struct stage_path_arg {
6934 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
6935 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
6936 2db2652d 2019-08-07 stsp struct got_repository *repo;
6937 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
6938 2db2652d 2019-08-07 stsp void *status_arg;
6939 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
6940 2db2652d 2019-08-07 stsp void *patch_arg;
6941 7b5dc508 2019-10-28 stsp int staged_something;
6944 2db2652d 2019-08-07 stsp static const struct got_error *
6945 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
6946 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
6947 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6948 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
6950 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
6951 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
6952 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
6953 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
6954 0cb83759 2019-08-03 stsp uint32_t stage;
6955 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
6957 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
6958 8b13ce36 2019-08-08 stsp return NULL;
6960 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6961 d3e7c587 2019-08-03 stsp if (ie == NULL)
6962 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6964 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6965 2db2652d 2019-08-07 stsp relpath)== -1)
6966 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
6968 0cb83759 2019-08-03 stsp switch (status) {
6969 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
6970 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
6971 2db2652d 2019-08-07 stsp if (a->patch_cb) {
6972 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
6973 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
6974 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
6975 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
6978 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
6981 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
6982 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
6983 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
6984 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
6985 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
6989 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
6990 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
6993 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6994 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
6995 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6996 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
6998 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
6999 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7000 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7001 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7003 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7004 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
7005 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
7007 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
7008 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
7010 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7011 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7012 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
7013 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
7016 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7018 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7019 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7023 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
7024 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7025 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7026 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7028 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7029 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7032 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
7034 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
7035 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7037 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
7038 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
7041 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7045 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
7046 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
7047 dc424a06 2019-08-07 stsp free(path_content);
7048 2db2652d 2019-08-07 stsp free(ondisk_path);
7049 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
7050 0ebf8283 2019-07-24 stsp return err;
7053 0cb83759 2019-08-03 stsp const struct got_error *
7054 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
7055 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
7056 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
7057 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7058 1e71573e 2019-08-03 stsp struct got_repository *repo)
7060 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7061 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
7062 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7063 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
7064 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
7065 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
7066 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
7067 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
7069 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7071 0cb83759 2019-08-03 stsp return err;
7073 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
7074 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
7076 735ef5ac 2019-08-03 stsp goto done;
7077 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7079 735ef5ac 2019-08-03 stsp goto done;
7080 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7082 0cb83759 2019-08-03 stsp goto done;
7084 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
7085 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
7086 2db2652d 2019-08-07 stsp oka.worktree = worktree;
7087 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
7088 2db2652d 2019-08-07 stsp oka.repo = repo;
7089 2db2652d 2019-08-07 stsp oka.have_changes = 0;
7090 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7091 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7092 f2a9dc41 2019-12-13 tracey check_stage_ok, &oka, NULL, NULL, 0, 0);
7094 735ef5ac 2019-08-03 stsp goto done;
7096 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
7097 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7098 2db2652d 2019-08-07 stsp goto done;
7101 2db2652d 2019-08-07 stsp spa.worktree = worktree;
7102 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
7103 2db2652d 2019-08-07 stsp spa.repo = repo;
7104 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
7105 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
7106 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
7107 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
7108 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
7109 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7110 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7111 f2a9dc41 2019-12-13 tracey stage_path, &spa, NULL, NULL, 0, 0);
7113 2db2652d 2019-08-07 stsp goto done;
7115 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
7116 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7117 7b5dc508 2019-10-28 stsp goto done;
7120 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7121 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
7122 0cb83759 2019-08-03 stsp err = sync_err;
7124 735ef5ac 2019-08-03 stsp if (head_ref)
7125 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
7126 735ef5ac 2019-08-03 stsp free(head_commit_id);
7127 0cb83759 2019-08-03 stsp free(fileindex_path);
7128 0cb83759 2019-08-03 stsp if (fileindex)
7129 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
7130 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7131 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
7132 0cb83759 2019-08-03 stsp err = unlockerr;
7133 0cb83759 2019-08-03 stsp return err;
7136 ad493afc 2019-08-03 stsp struct unstage_path_arg {
7137 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
7138 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
7139 ad493afc 2019-08-03 stsp struct got_repository *repo;
7140 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
7141 ad493afc 2019-08-03 stsp void *progress_arg;
7142 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
7143 2e1f37b0 2019-08-08 stsp void *patch_arg;
7146 2e1f37b0 2019-08-08 stsp static const struct got_error *
7147 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
7148 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
7149 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
7150 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
7151 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
7153 2e1f37b0 2019-08-08 stsp const struct got_error *err;
7154 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
7155 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7156 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7157 2e1f37b0 2019-08-08 stsp struct stat sb1, sb2;
7158 2e1f37b0 2019-08-08 stsp struct got_diff_changes *changes = NULL;
7159 2e1f37b0 2019-08-08 stsp struct got_diff_state *ds = NULL;
7160 2e1f37b0 2019-08-08 stsp struct got_diff_args *args = NULL;
7161 2e1f37b0 2019-08-08 stsp struct got_diff_change *change;
7162 2e1f37b0 2019-08-08 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7163 2e1f37b0 2019-08-08 stsp int have_content = 0, have_rejected_content = 0;
7165 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7166 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7168 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
7170 2e1f37b0 2019-08-08 stsp return err;
7171 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7173 2e1f37b0 2019-08-08 stsp goto done;
7175 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7177 2e1f37b0 2019-08-08 stsp goto done;
7179 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7181 2e1f37b0 2019-08-08 stsp goto done;
7183 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7185 2e1f37b0 2019-08-08 stsp goto done;
7187 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7189 2e1f37b0 2019-08-08 stsp goto done;
7191 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7193 2e1f37b0 2019-08-08 stsp goto done;
7195 2e1f37b0 2019-08-08 stsp if (stat(path1, &sb1) == -1) {
7196 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path1);
7197 2e1f37b0 2019-08-08 stsp goto done;
7200 2e1f37b0 2019-08-08 stsp if (stat(path2, &sb2) == -1) {
7201 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path2);
7202 2e1f37b0 2019-08-08 stsp goto done;
7205 2e1f37b0 2019-08-08 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
7206 2e1f37b0 2019-08-08 stsp f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7208 2e1f37b0 2019-08-08 stsp goto done;
7210 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
7211 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
7213 2e1f37b0 2019-08-08 stsp goto done;
7214 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
7215 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
7217 2e1f37b0 2019-08-08 stsp goto done;
7219 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
7220 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
7221 2e1f37b0 2019-08-08 stsp goto done;
7223 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
7224 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
7225 2e1f37b0 2019-08-08 stsp goto done;
7227 2e1f37b0 2019-08-08 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7228 2e1f37b0 2019-08-08 stsp int choice;
7229 2e1f37b0 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
7230 2e1f37b0 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
7231 2e1f37b0 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
7232 2e1f37b0 2019-08-08 stsp outfile, rejectfile, patch_cb, patch_arg);
7234 2e1f37b0 2019-08-08 stsp goto done;
7235 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
7236 2e1f37b0 2019-08-08 stsp have_content = 1;
7238 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
7239 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
7242 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
7243 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7244 f1e81a05 2019-08-10 stsp outfile, rejectfile);
7246 2e1f37b0 2019-08-08 stsp free(label1);
7248 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
7249 2e1f37b0 2019-08-08 stsp if (staged_blob)
7250 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
7251 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
7252 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
7253 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
7254 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
7255 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
7256 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
7257 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7258 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
7259 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
7260 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
7261 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
7262 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
7263 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
7264 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
7265 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
7266 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7267 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
7268 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
7269 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7271 2e1f37b0 2019-08-08 stsp if (err || !have_rejected_content) {
7272 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
7273 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
7274 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7275 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
7276 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
7277 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7279 2e1f37b0 2019-08-08 stsp free(args);
7281 2e1f37b0 2019-08-08 stsp got_diff_state_free(ds);
7284 2e1f37b0 2019-08-08 stsp if (changes)
7285 2e1f37b0 2019-08-08 stsp got_diff_free_changes(changes);
7286 2e1f37b0 2019-08-08 stsp free(path1);
7287 2e1f37b0 2019-08-08 stsp free(path2);
7288 2e1f37b0 2019-08-08 stsp return err;
7291 ad493afc 2019-08-03 stsp static const struct got_error *
7292 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
7293 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
7294 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7295 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7297 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
7298 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
7299 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
7300 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7301 2e1f37b0 2019-08-08 stsp char *ondisk_path = NULL, *path_unstaged_content = NULL;
7302 2e1f37b0 2019-08-08 stsp char *path_new_staged_content = NULL;
7303 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
7304 ad493afc 2019-08-03 stsp int local_changes_subsumed;
7305 9bc94a15 2019-08-03 stsp struct stat sb;
7307 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
7308 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
7309 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
7310 2e1f37b0 2019-08-08 stsp return NULL;
7312 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7313 ad493afc 2019-08-03 stsp if (ie == NULL)
7314 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7316 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7318 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
7320 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
7321 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
7323 f69721c3 2019-10-21 stsp goto done;
7324 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7325 f69721c3 2019-10-21 stsp id_str) == -1) {
7326 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
7327 f69721c3 2019-10-21 stsp goto done;
7330 ad493afc 2019-08-03 stsp switch (staged_status) {
7331 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
7332 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
7333 ad493afc 2019-08-03 stsp blob_id, 8192);
7336 ad493afc 2019-08-03 stsp /* fall through */
7337 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
7338 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7339 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
7340 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7341 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7342 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7345 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
7348 2e1f37b0 2019-08-08 stsp err = create_unstaged_content(
7349 2e1f37b0 2019-08-08 stsp &path_unstaged_content,
7350 2e1f37b0 2019-08-08 stsp &path_new_staged_content, blob_id,
7351 2e1f37b0 2019-08-08 stsp staged_blob_id, ie->path, a->repo,
7352 2e1f37b0 2019-08-08 stsp a->patch_cb, a->patch_arg);
7353 2e1f37b0 2019-08-08 stsp if (err || path_unstaged_content == NULL)
7355 2e1f37b0 2019-08-08 stsp if (path_new_staged_content) {
7356 2e1f37b0 2019-08-08 stsp err = got_object_blob_create(
7357 2e1f37b0 2019-08-08 stsp &staged_blob_id,
7358 2e1f37b0 2019-08-08 stsp path_new_staged_content,
7362 2e1f37b0 2019-08-08 stsp memcpy(ie->staged_blob_sha1,
7363 2e1f37b0 2019-08-08 stsp staged_blob_id->sha1,
7364 2e1f37b0 2019-08-08 stsp SHA1_DIGEST_LENGTH);
7366 2e1f37b0 2019-08-08 stsp err = merge_file(&local_changes_subsumed,
7367 2e1f37b0 2019-08-08 stsp a->worktree, blob_base, ondisk_path,
7368 2e1f37b0 2019-08-08 stsp relpath, got_fileindex_perms_to_st(ie),
7369 f69721c3 2019-10-21 stsp path_unstaged_content, label_orig,
7370 f69721c3 2019-10-21 stsp "unstaged", a->repo, a->progress_cb,
7371 f69721c3 2019-10-21 stsp a->progress_arg);
7372 2e1f37b0 2019-08-08 stsp if (err == NULL &&
7373 2e1f37b0 2019-08-08 stsp path_new_staged_content == NULL)
7374 2e1f37b0 2019-08-08 stsp got_fileindex_entry_stage_set(ie,
7375 2e1f37b0 2019-08-08 stsp GOT_FILEIDX_STAGE_NONE);
7376 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
7379 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
7380 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
7383 ad493afc 2019-08-03 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
7384 ad493afc 2019-08-03 stsp blob_base, ondisk_path, relpath,
7385 f69721c3 2019-10-21 stsp got_fileindex_perms_to_st(ie), label_orig, blob_staged,
7386 ad493afc 2019-08-03 stsp commit_id ? commit_id : a->worktree->base_commit_id,
7387 ad493afc 2019-08-03 stsp a->repo, a->progress_cb, a->progress_arg);
7388 ad493afc 2019-08-03 stsp if (err == NULL)
7389 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
7390 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
7392 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
7393 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7394 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7395 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7396 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7399 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7401 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7402 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7406 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7407 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
7408 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
7411 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
7415 ad493afc 2019-08-03 stsp free(ondisk_path);
7416 2e1f37b0 2019-08-08 stsp if (path_unstaged_content &&
7417 2e1f37b0 2019-08-08 stsp unlink(path_unstaged_content) == -1 && err == NULL)
7418 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
7419 2e1f37b0 2019-08-08 stsp if (path_new_staged_content &&
7420 2e1f37b0 2019-08-08 stsp unlink(path_new_staged_content) == -1 && err == NULL)
7421 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
7422 2e1f37b0 2019-08-08 stsp free(path_unstaged_content);
7423 2e1f37b0 2019-08-08 stsp free(path_new_staged_content);
7424 ad493afc 2019-08-03 stsp if (blob_base)
7425 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
7426 ad493afc 2019-08-03 stsp if (blob_staged)
7427 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
7428 f69721c3 2019-10-21 stsp free(id_str);
7429 f69721c3 2019-10-21 stsp free(label_orig);
7430 ad493afc 2019-08-03 stsp return err;
7433 ad493afc 2019-08-03 stsp const struct got_error *
7434 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
7435 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
7436 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7437 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7438 ad493afc 2019-08-03 stsp struct got_repository *repo)
7440 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7441 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
7442 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7443 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
7444 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
7446 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7448 ad493afc 2019-08-03 stsp return err;
7450 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7452 ad493afc 2019-08-03 stsp goto done;
7454 ad493afc 2019-08-03 stsp upa.worktree = worktree;
7455 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
7456 ad493afc 2019-08-03 stsp upa.repo = repo;
7457 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
7458 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
7459 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
7460 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
7461 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7462 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7463 f2a9dc41 2019-12-13 tracey unstage_path, &upa, NULL, NULL, 0, 0);
7465 ad493afc 2019-08-03 stsp goto done;
7468 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7469 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
7470 ad493afc 2019-08-03 stsp err = sync_err;
7472 ad493afc 2019-08-03 stsp free(fileindex_path);
7473 ad493afc 2019-08-03 stsp if (fileindex)
7474 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
7475 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7476 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
7477 ad493afc 2019-08-03 stsp err = unlockerr;
7478 ad493afc 2019-08-03 stsp return err;