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"
58 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
61 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
65 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
67 99724ed4 2018-03-10 stsp static const struct got_error *
68 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
70 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
73 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
76 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
81 507dc3bb 2018-12-29 stsp static const struct got_error *
82 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
84 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
85 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
86 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
87 507dc3bb 2018-12-29 stsp char *path = NULL;
89 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
91 507dc3bb 2018-12-29 stsp path = NULL;
95 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
99 507dc3bb 2018-12-29 stsp if (content) {
100 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
101 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
102 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
107 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
108 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
109 2a57020b 2019-02-20 stsp unlink(tmppath);
114 56b63ca4 2021-01-22 stsp if (fclose(tmpfile) == EOF && err == NULL)
115 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
116 230a42bd 2019-05-11 jcs free(tmppath);
117 99724ed4 2018-03-10 stsp return err;
120 09fe317a 2018-03-11 stsp static const struct got_error *
121 7ac97322 2018-03-11 stsp read_meta_file(char **content, const char *path_got, const char *name)
123 09fe317a 2018-03-11 stsp const struct got_error *err = NULL;
124 09fe317a 2018-03-11 stsp char *path;
125 09fe317a 2018-03-11 stsp int fd = -1;
127 09fe317a 2018-03-11 stsp struct stat sb;
129 09fe317a 2018-03-11 stsp *content = NULL;
131 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
132 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
133 09fe317a 2018-03-11 stsp path = NULL;
137 ef99fdb1 2018-03-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
138 09fe317a 2018-03-11 stsp if (fd == -1) {
139 f02eaa22 2019-03-11 stsp if (errno == ENOENT)
140 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
142 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", path);
145 ef99fdb1 2018-03-11 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
146 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
147 638f9024 2019-05-13 stsp : got_error_from_errno2("flock", path));
151 e4b9a50c 2019-07-27 stsp if (fstat(fd, &sb) != 0) {
152 e4b9a50c 2019-07-27 stsp err = got_error_from_errno2("fstat", path);
155 09fe317a 2018-03-11 stsp *content = calloc(1, sb.st_size);
156 09fe317a 2018-03-11 stsp if (*content == NULL) {
157 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
161 09fe317a 2018-03-11 stsp n = read(fd, *content, sb.st_size);
162 09fe317a 2018-03-11 stsp if (n != sb.st_size) {
163 638f9024 2019-05-13 stsp err = (n == -1 ? got_error_from_errno2("read", path) :
164 a2e6d162 2019-07-27 stsp got_error_path(path, GOT_ERR_WORKTREE_META));
167 09fe317a 2018-03-11 stsp if ((*content)[sb.st_size - 1] != '\n') {
168 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
171 09fe317a 2018-03-11 stsp (*content)[sb.st_size - 1] = '\0';
174 09fe317a 2018-03-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
175 638f9024 2019-05-13 stsp err = got_error_from_errno2("close", path_got);
176 09fe317a 2018-03-11 stsp free(path);
178 09fe317a 2018-03-11 stsp free(*content);
179 09fe317a 2018-03-11 stsp *content = NULL;
181 09fe317a 2018-03-11 stsp return err;
184 024e9686 2019-05-14 stsp static const struct got_error *
185 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
187 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
188 024e9686 2019-05-14 stsp char *refstr = NULL;
190 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
191 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
192 024e9686 2019-05-14 stsp if (refstr == NULL)
193 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
195 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
196 024e9686 2019-05-14 stsp if (refstr == NULL)
197 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
199 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
200 024e9686 2019-05-14 stsp free(refstr);
201 024e9686 2019-05-14 stsp return err;
204 86c3caaf 2018-03-09 stsp const struct got_error *
205 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
206 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
208 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
209 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
210 ec22038e 2019-03-10 stsp uuid_t uuid;
211 ec22038e 2019-03-10 stsp uint32_t uuid_status;
212 65596e15 2018-12-24 stsp int obj_type;
213 7ac97322 2018-03-11 stsp char *path_got = NULL;
214 1451e70d 2018-03-10 stsp char *formatstr = NULL;
215 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
216 65596e15 2018-12-24 stsp char *basestr = NULL;
217 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
219 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
220 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
224 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
226 65596e15 2018-12-24 stsp return err;
227 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
229 65596e15 2018-12-24 stsp return err;
230 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
231 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
233 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
234 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
235 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
238 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
239 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
240 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
244 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
245 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
246 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
249 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
250 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
254 056e7441 2018-03-11 stsp /* Create an empty lock file. */
255 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
259 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
260 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
264 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
265 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
269 65596e15 2018-12-24 stsp /* Record our base commit. */
270 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
273 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
277 1451e70d 2018-03-10 stsp /* Store path to repository. */
278 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
279 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
283 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
284 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
285 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
289 ec22038e 2019-03-10 stsp /* Generate UUID. */
290 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
291 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
292 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
295 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
296 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
297 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
300 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
304 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
305 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
306 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
309 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
314 65596e15 2018-12-24 stsp free(commit_id);
315 7ac97322 2018-03-11 stsp free(path_got);
316 1451e70d 2018-03-10 stsp free(formatstr);
317 0bb8a95e 2018-03-12 stsp free(absprefix);
318 65596e15 2018-12-24 stsp free(basestr);
319 ec22038e 2019-03-10 stsp free(uuidstr);
320 86c3caaf 2018-03-09 stsp return err;
323 247140b2 2019-02-05 stsp static const struct got_error *
324 247140b2 2019-02-05 stsp open_worktree(struct got_worktree **worktree, const char *path)
326 6d9d28c3 2018-03-11 stsp const struct got_error *err = NULL;
327 7ac97322 2018-03-11 stsp char *path_got;
328 6d9d28c3 2018-03-11 stsp char *formatstr = NULL;
329 c442a90d 2019-03-10 stsp char *uuidstr = NULL;
330 056e7441 2018-03-11 stsp char *path_lock = NULL;
331 eaccb85f 2018-12-25 stsp char *base_commit_id_str = NULL;
332 6d9d28c3 2018-03-11 stsp int version, fd = -1;
333 6d9d28c3 2018-03-11 stsp const char *errstr;
334 eaccb85f 2018-12-25 stsp struct got_repository *repo = NULL;
335 c442a90d 2019-03-10 stsp uint32_t uuid_status;
337 6d9d28c3 2018-03-11 stsp *worktree = NULL;
339 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
340 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
341 7ac97322 2018-03-11 stsp path_got = NULL;
345 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
346 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
347 056e7441 2018-03-11 stsp path_lock = NULL;
351 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
352 6d9d28c3 2018-03-11 stsp if (fd == -1) {
353 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
354 638f9024 2019-05-13 stsp : got_error_from_errno2("open", path_lock));
358 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
362 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
363 6d9d28c3 2018-03-11 stsp if (errstr) {
364 a2e6d162 2019-07-27 stsp err = got_error_msg(GOT_ERR_WORKTREE_META,
365 a2e6d162 2019-07-27 stsp "could not parse work tree format version number");
368 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
369 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
373 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
374 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
375 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
378 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
380 7d61d891 2020-07-23 stsp (*worktree)->root_path = realpath(path, NULL);
381 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
382 7d61d891 2020-07-23 stsp err = got_error_from_errno2("realpath", path);
385 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
386 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
390 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
391 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
395 eaccb85f 2018-12-25 stsp err = read_meta_file(&base_commit_id_str, path_got,
396 0f92850e 2018-12-25 stsp GOT_WORKTREE_BASE_COMMIT);
400 c442a90d 2019-03-10 stsp err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
403 c442a90d 2019-03-10 stsp uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
404 c442a90d 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
405 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_from_string");
409 c9956ddf 2019-09-08 stsp err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
413 eaccb85f 2018-12-25 stsp err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
414 eaccb85f 2018-12-25 stsp base_commit_id_str);
418 36a38700 2019-05-10 stsp err = read_meta_file(&(*worktree)->head_ref_name, path_got,
419 36a38700 2019-05-10 stsp GOT_WORKTREE_HEAD_REF);
423 50b0790e 2020-09-11 stsp if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s",
424 50b0790e 2020-09-11 stsp (*worktree)->root_path,
425 50b0790e 2020-09-11 stsp GOT_WORKTREE_GOT_DIR, GOT_GOTCONFIG_FILENAME) == -1) {
426 50b0790e 2020-09-11 stsp err = got_error_from_errno("asprintf");
430 50b0790e 2020-09-11 stsp err = got_gotconfig_read(&(*worktree)->gotconfig,
431 50b0790e 2020-09-11 stsp (*worktree)->gotconfig_path);
433 437adc9d 2020-12-10 yzhong (*worktree)->root_fd = open((*worktree)->root_path, O_DIRECTORY);
434 437adc9d 2020-12-10 yzhong if ((*worktree)->root_fd == -1) {
435 437adc9d 2020-12-10 yzhong err = got_error_from_errno2("open", (*worktree)->root_path);
436 437adc9d 2020-12-10 yzhong goto done;
439 1d0f4054 2021-06-17 stsp if (repo) {
440 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
441 1d0f4054 2021-06-17 stsp if (err == NULL)
442 1d0f4054 2021-06-17 stsp err = close_err;
444 7ac97322 2018-03-11 stsp free(path_got);
445 056e7441 2018-03-11 stsp free(path_lock);
446 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
447 c442a90d 2019-03-10 stsp free(uuidstr);
448 bd165944 2019-03-10 stsp free(formatstr);
450 6d9d28c3 2018-03-11 stsp if (fd != -1)
452 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
453 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
454 6d9d28c3 2018-03-11 stsp *worktree = NULL;
456 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
458 6d9d28c3 2018-03-11 stsp return err;
461 247140b2 2019-02-05 stsp const struct got_error *
462 247140b2 2019-02-05 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
464 247140b2 2019-02-05 stsp const struct got_error *err = NULL;
465 aedea96d 2020-10-20 stsp char *worktree_path;
467 aedea96d 2020-10-20 stsp worktree_path = strdup(path);
468 aedea96d 2020-10-20 stsp if (worktree_path == NULL)
469 aedea96d 2020-10-20 stsp return got_error_from_errno("strdup");
472 aedea96d 2020-10-20 stsp char *parent_path;
474 aedea96d 2020-10-20 stsp err = open_worktree(worktree, worktree_path);
475 aedea96d 2020-10-20 stsp if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
476 aedea96d 2020-10-20 stsp free(worktree_path);
477 247140b2 2019-02-05 stsp return err;
479 aedea96d 2020-10-20 stsp if (*worktree) {
480 aedea96d 2020-10-20 stsp free(worktree_path);
481 aedea96d 2020-10-20 stsp return NULL;
483 aedea96d 2020-10-20 stsp if (worktree_path[0] == '/' && worktree_path[1] == '\0')
485 aedea96d 2020-10-20 stsp err = got_path_dirname(&parent_path, worktree_path);
487 aedea96d 2020-10-20 stsp if (err->code != GOT_ERR_BAD_PATH) {
488 aedea96d 2020-10-20 stsp free(worktree_path);
489 aedea96d 2020-10-20 stsp return err;
493 aedea96d 2020-10-20 stsp free(worktree_path);
494 aedea96d 2020-10-20 stsp worktree_path = parent_path;
497 aedea96d 2020-10-20 stsp free(worktree_path);
498 247140b2 2019-02-05 stsp return got_error(GOT_ERR_NOT_WORKTREE);
501 3a6ce05a 2019-02-11 stsp const struct got_error *
502 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
504 3a6ce05a 2019-02-11 stsp const struct got_error *err = NULL;
506 a6b21eef 2021-01-21 stsp if (worktree->lockfd != -1) {
507 08578a35 2021-01-22 stsp if (close(worktree->lockfd) == -1)
508 638f9024 2019-05-13 stsp err = got_error_from_errno2("close",
509 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree));
511 e7abd6b6 2021-01-22 stsp if (close(worktree->root_fd) == -1 && err == NULL)
512 e7abd6b6 2021-01-22 stsp err = got_error_from_errno2("close",
513 e7abd6b6 2021-01-22 stsp got_worktree_get_root_path(worktree));
514 60e40e95 2021-01-22 stsp free(worktree->repo_path);
515 60e40e95 2021-01-22 stsp free(worktree->path_prefix);
516 60e40e95 2021-01-22 stsp free(worktree->base_commit_id);
517 60e40e95 2021-01-22 stsp free(worktree->head_ref_name);
518 60e40e95 2021-01-22 stsp free(worktree->root_path);
519 60e40e95 2021-01-22 stsp free(worktree->gotconfig_path);
520 60e40e95 2021-01-22 stsp got_gotconfig_free(worktree->gotconfig);
521 a06ff56f 2021-01-21 naddy free(worktree);
522 3a6ce05a 2019-02-11 stsp return err;
525 2fbdb5ae 2018-12-29 stsp const char *
526 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
528 c7f4312f 2019-02-05 stsp return worktree->root_path;
531 c7f4312f 2019-02-05 stsp const char *
532 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
534 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
536 49520a32 2018-12-29 stsp const char *
537 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
539 f609be2e 2018-12-29 stsp return worktree->path_prefix;
542 e5dc7198 2018-12-29 stsp const struct got_error *
543 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
544 e5dc7198 2018-12-29 stsp const char *path_prefix)
546 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
548 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
549 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
550 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
552 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
553 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
554 e5dc7198 2018-12-29 stsp free(absprefix);
555 e5dc7198 2018-12-29 stsp return NULL;
558 bc70eb79 2019-05-09 stsp const char *
559 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
561 36a38700 2019-05-10 stsp return worktree->head_ref_name;
564 024e9686 2019-05-14 stsp const struct got_error *
565 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
566 024e9686 2019-05-14 stsp struct got_reference *head_ref)
568 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
569 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
571 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
572 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
573 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
574 024e9686 2019-05-14 stsp path_got = NULL;
578 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
579 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
580 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
584 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
588 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
589 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
591 024e9686 2019-05-14 stsp free(path_got);
593 024e9686 2019-05-14 stsp free(head_ref_name);
594 024e9686 2019-05-14 stsp return err;
597 b72f483a 2019-02-05 stsp struct got_object_id *
598 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
600 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
603 507dc3bb 2018-12-29 stsp const struct got_error *
604 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
605 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
607 507dc3bb 2018-12-29 stsp const struct got_error *err;
608 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
609 507dc3bb 2018-12-29 stsp char *id_str = NULL;
610 507dc3bb 2018-12-29 stsp char *path_got = NULL;
612 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
613 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
614 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
615 507dc3bb 2018-12-29 stsp path_got = NULL;
619 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
621 507dc3bb 2018-12-29 stsp return err;
623 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
624 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
628 507dc3bb 2018-12-29 stsp /* Record our base commit. */
629 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
632 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
636 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
637 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
638 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
639 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
644 507dc3bb 2018-12-29 stsp got_object_close(obj);
645 507dc3bb 2018-12-29 stsp free(id_str);
646 507dc3bb 2018-12-29 stsp free(path_got);
647 507dc3bb 2018-12-29 stsp return err;
650 50b0790e 2020-09-11 stsp const struct got_gotconfig *
651 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
653 50b0790e 2020-09-11 stsp return worktree->gotconfig;
656 9d31a1d8 2018-03-11 stsp static const struct got_error *
657 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
659 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
660 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
661 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
662 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
663 86c3caaf 2018-03-09 stsp return NULL;
666 21908da4 2019-01-13 stsp static const struct got_error *
667 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
669 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
670 4a1ddfc2 2019-01-12 stsp char *abspath;
672 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
673 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
675 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
676 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
677 ddcd8544 2019-03-11 stsp struct stat sb;
678 ddcd8544 2019-03-11 stsp err = NULL;
679 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
680 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
681 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
682 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
683 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
686 4a1ddfc2 2019-01-12 stsp free(abspath);
687 68c76935 2019-02-19 stsp return err;
690 68c76935 2019-02-19 stsp static const struct got_error *
691 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
693 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
694 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
695 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
696 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
701 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
702 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
703 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
706 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
707 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
708 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
711 68c76935 2019-02-19 stsp if (flen1 == 0) {
712 68c76935 2019-02-19 stsp if (flen2 != 0)
715 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
716 68c76935 2019-02-19 stsp if (flen1 != 0)
719 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
720 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
730 68c76935 2019-02-19 stsp return err;
733 68c76935 2019-02-19 stsp static const struct got_error *
734 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
736 68c76935 2019-02-19 stsp struct stat sb;
737 68c76935 2019-02-19 stsp size_t size1, size2;
741 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
742 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
743 68c76935 2019-02-19 stsp size1 = sb.st_size;
745 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
746 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
747 68c76935 2019-02-19 stsp size2 = sb.st_size;
749 68c76935 2019-02-19 stsp if (size1 != size2) {
751 68c76935 2019-02-19 stsp return NULL;
754 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
755 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
756 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
757 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
759 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
763 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
764 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
765 eec2f5a9 2021-06-03 stsp * and the file f_deriv2 acts as the second derived version.
766 eec2f5a9 2021-06-03 stsp * The merge result will be written to a new file at ondisk_path; any
767 eec2f5a9 2021-06-03 stsp * existing file at this path will be replaced.
769 6353ad76 2019-02-08 stsp static const struct got_error *
770 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
771 eec2f5a9 2021-06-03 stsp FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
772 07bb0f93 2021-06-02 stsp const char *path, uint16_t st_mode,
773 54d5be07 2021-06-03 stsp const char *label_orig, const char *label_deriv, const char *label_deriv2,
774 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo, struct got_repository *repo,
775 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
777 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
778 6353ad76 2019-02-08 stsp int merged_fd = -1;
779 db590691 2021-06-02 stsp FILE *f_merged = NULL;
780 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
781 234035bc 2019-06-01 stsp int overlapcnt = 0;
782 3524bbf9 2020-10-20 stsp char *parent = NULL;
784 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
786 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
788 3524bbf9 2020-10-20 stsp return err;
790 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
791 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
795 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
799 eec2f5a9 2021-06-03 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
800 fdf3c2d3 2021-06-17 stsp f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
804 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
805 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
809 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
810 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
814 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
815 db590691 2021-06-02 stsp if (f_merged == NULL) {
816 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
819 db590691 2021-06-02 stsp merged_fd = -1;
821 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
822 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
823 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
829 db590691 2021-06-02 stsp if (fchmod(fileno(f_merged), st_mode) != 0) {
830 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
834 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
835 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
836 230a42bd 2019-05-11 jcs ondisk_path);
841 fdcb7daf 2019-12-15 stsp if (merged_path)
842 fdcb7daf 2019-12-15 stsp unlink(merged_path);
844 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
845 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
846 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
847 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
848 6353ad76 2019-02-08 stsp free(merged_path);
849 af54ae4a 2019-02-19 stsp free(base_path);
850 3524bbf9 2020-10-20 stsp free(parent);
851 af57b12a 2020-07-23 stsp return err;
854 af57b12a 2020-07-23 stsp static const struct got_error *
855 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
856 af57b12a 2020-07-23 stsp size_t target_len)
858 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
859 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
860 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
861 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
862 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
863 af57b12a 2020-07-23 stsp ondisk_path);
864 af57b12a 2020-07-23 stsp return NULL;
868 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
869 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
870 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
871 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
873 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
874 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
876 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
877 993e2a1b 2020-07-23 stsp * has deleted this symlink.
879 11cc08c1 2020-07-23 stsp static const struct got_error *
880 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
881 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
882 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
884 11cc08c1 2020-07-23 stsp const struct got_error *err;
885 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
886 11cc08c1 2020-07-23 stsp FILE *f = NULL;
888 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
890 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
892 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
893 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
894 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
898 11cc08c1 2020-07-23 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict");
900 3818e3c4 2020-11-01 naddy goto done;
902 3818e3c4 2020-11-01 naddy if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
903 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
907 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
908 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
909 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
910 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
911 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
912 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
913 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
914 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
915 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
916 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
920 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
921 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
924 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
925 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
929 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
930 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
931 11cc08c1 2020-07-23 stsp free(path);
932 11cc08c1 2020-07-23 stsp free(id_str);
933 11cc08c1 2020-07-23 stsp free(label_deriv);
934 11cc08c1 2020-07-23 stsp return err;
937 d219f183 2020-07-23 stsp /* forward declaration */
938 d219f183 2020-07-23 stsp static const struct got_error *
939 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
940 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
941 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
942 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
945 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
946 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
947 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
948 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
950 af57b12a 2020-07-23 stsp static const struct got_error *
951 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
952 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
953 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
954 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
955 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
957 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
958 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
959 af57b12a 2020-07-23 stsp struct stat sb;
960 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
961 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
962 11cc08c1 2020-07-23 stsp int have_local_change = 0;
963 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
965 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
966 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
968 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
969 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
970 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
971 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
972 af57b12a 2020-07-23 stsp ondisk_path);
975 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
977 526a746f 2020-07-23 stsp if (blob_orig) {
978 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
983 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
984 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
985 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
986 11cc08c1 2020-07-23 stsp have_local_change = 1;
988 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
989 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
990 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
991 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
992 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
994 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
995 11cc08c1 2020-07-23 stsp if (ancestor_target) {
996 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
997 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
999 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
1000 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1001 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
1002 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1005 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
1006 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1007 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
1008 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
1010 11cc08c1 2020-07-23 stsp goto done;
1011 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1014 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
1015 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
1016 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
1017 af57b12a 2020-07-23 stsp strlen(deriv_target));
1019 af57b12a 2020-07-23 stsp goto done;
1020 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1021 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
1022 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
1023 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1024 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
1025 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1028 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1029 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
1030 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
1032 ea7786be 2020-07-23 stsp goto done;
1033 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1039 af57b12a 2020-07-23 stsp free(ancestor_target);
1040 eec2f5a9 2021-06-03 stsp return err;
1043 eec2f5a9 2021-06-03 stsp static const struct got_error *
1044 eec2f5a9 2021-06-03 stsp dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
1046 eec2f5a9 2021-06-03 stsp const struct got_error *err = NULL;
1047 eec2f5a9 2021-06-03 stsp char target_path[PATH_MAX];
1048 eec2f5a9 2021-06-03 stsp ssize_t target_len;
1052 eec2f5a9 2021-06-03 stsp *outfile = NULL;
1054 eec2f5a9 2021-06-03 stsp f = got_opentemp();
1055 eec2f5a9 2021-06-03 stsp if (f == NULL)
1056 eec2f5a9 2021-06-03 stsp return got_error_from_errno("got_opentemp");
1057 eec2f5a9 2021-06-03 stsp target_len = readlink(ondisk_path, target_path, sizeof(target_path));
1058 eec2f5a9 2021-06-03 stsp if (target_len == -1) {
1059 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("readlink", ondisk_path);
1060 eec2f5a9 2021-06-03 stsp goto done;
1062 eec2f5a9 2021-06-03 stsp n = fwrite(target_path, 1, target_len, f);
1063 eec2f5a9 2021-06-03 stsp if (n != target_len) {
1064 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
1065 eec2f5a9 2021-06-03 stsp goto done;
1067 eec2f5a9 2021-06-03 stsp if (fflush(f) == EOF) {
1068 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fflush");
1069 eec2f5a9 2021-06-03 stsp goto done;
1071 eec2f5a9 2021-06-03 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
1072 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
1073 eec2f5a9 2021-06-03 stsp goto done;
1077 eec2f5a9 2021-06-03 stsp fclose(f);
1079 eec2f5a9 2021-06-03 stsp *outfile = f;
1080 14c901f1 2019-08-08 stsp return err;
1084 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
1085 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
1086 14c901f1 2019-08-08 stsp * acts as the second derived version.
1088 14c901f1 2019-08-08 stsp static const struct got_error *
1089 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1090 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
1091 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
1092 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
1093 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1094 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1096 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
1097 eec2f5a9 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
1098 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
1099 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1100 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
1102 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
1104 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1106 ed6b5030 2020-10-20 stsp return err;
1108 67a66647 2021-05-31 stsp if (blob_orig) {
1109 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
1110 67a66647 2021-05-31 stsp parent) == -1) {
1111 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
1112 67a66647 2021-05-31 stsp base_path = NULL;
1113 67a66647 2021-05-31 stsp goto done;
1116 67a66647 2021-05-31 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
1118 67a66647 2021-05-31 stsp goto done;
1119 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1120 67a66647 2021-05-31 stsp blob_orig);
1122 67a66647 2021-05-31 stsp goto done;
1123 67a66647 2021-05-31 stsp free(base_path);
1126 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1127 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1128 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1130 db590691 2021-06-02 stsp f_orig = got_opentemp();
1131 db590691 2021-06-02 stsp if (f_orig == NULL) {
1132 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1133 db590691 2021-06-02 stsp goto done;
1137 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1138 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1139 14c901f1 2019-08-08 stsp base_path = NULL;
1140 14c901f1 2019-08-08 stsp goto done;
1143 14c901f1 2019-08-08 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1145 14c901f1 2019-08-08 stsp goto done;
1146 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1147 14c901f1 2019-08-08 stsp blob_deriv);
1149 14c901f1 2019-08-08 stsp goto done;
1151 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1153 14c901f1 2019-08-08 stsp goto done;
1154 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1155 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1156 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1157 14c901f1 2019-08-08 stsp goto done;
1161 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1162 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1164 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1165 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1167 eec2f5a9 2021-06-03 stsp goto done;
1170 eec2f5a9 2021-06-03 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
1171 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1172 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1173 eec2f5a9 2021-06-03 stsp goto done;
1175 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1176 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1177 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1178 eec2f5a9 2021-06-03 stsp close(fd);
1179 eec2f5a9 2021-06-03 stsp goto done;
1183 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1184 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1185 fdf3c2d3 2021-06-17 stsp NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1187 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1188 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1189 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1190 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1191 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1192 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1193 14c901f1 2019-08-08 stsp free(base_path);
1194 67a66647 2021-05-31 stsp if (blob_orig_path) {
1195 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1196 67a66647 2021-05-31 stsp free(blob_orig_path);
1198 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1199 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1200 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1202 6353ad76 2019-02-08 stsp free(id_str);
1203 818c7501 2019-07-11 stsp free(label_deriv);
1204 ed6b5030 2020-10-20 stsp free(parent);
1205 4a1ddfc2 2019-01-12 stsp return err;
1208 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1209 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1210 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1211 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1213 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1214 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1216 65b05cec 2020-07-23 stsp *new_iep = NULL;
1218 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1220 054041d0 2020-06-24 stsp return err;
1222 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1223 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1225 054041d0 2020-06-24 stsp goto done;
1227 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1230 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1232 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1233 13d9040b 2019-03-27 stsp return err;
1236 1ebedb77 2019-10-19 stsp static mode_t
1237 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1239 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1241 1ebedb77 2019-10-19 stsp if (executable) {
1242 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1243 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1244 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1245 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1246 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1247 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1250 1ebedb77 2019-10-19 stsp return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1253 8ba819a3 2020-07-23 stsp /* forward declaration */
1254 13d9040b 2019-03-27 stsp static const struct got_error *
1255 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1256 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1257 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1258 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1259 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1260 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1263 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1264 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1266 8ba819a3 2020-07-23 stsp static const struct got_error *
1267 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1268 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1270 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1271 5a1fbc73 2020-07-23 stsp ssize_t elen;
1272 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1275 c6e8a826 2021-04-05 stsp *did_something = 0;
1278 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1279 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1280 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1281 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1282 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1283 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1285 5a1fbc73 2020-07-23 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1286 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1287 5a1fbc73 2020-07-23 stsp if (errno != ELOOP)
1288 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1290 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1291 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1292 5a1fbc73 2020-07-23 stsp if (elen == -1)
1293 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1295 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1296 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1297 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1300 c6e8a826 2021-04-05 stsp *did_something = 1;
1301 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1302 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1303 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1304 5a1fbc73 2020-07-23 stsp return err;
1307 3c1ec782 2020-07-23 stsp static const struct got_error *
1308 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1309 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1311 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1312 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1313 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1315 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1317 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1318 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1319 3c1ec782 2020-07-23 stsp return NULL;
1323 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1324 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1325 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1326 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1327 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1329 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1330 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1331 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1333 ce031e9e 2020-10-20 stsp return err;
1334 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1335 ce031e9e 2020-10-20 stsp free(parent);
1336 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1338 ce031e9e 2020-10-20 stsp free(parent);
1339 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1340 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1341 3c1ec782 2020-07-23 stsp free(abspath);
1342 3c1ec782 2020-07-23 stsp return err;
1344 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1345 3c1ec782 2020-07-23 stsp free(abspath);
1347 3c1ec782 2020-07-23 stsp return err;
1349 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1351 3c1ec782 2020-07-23 stsp return err;
1354 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1355 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1356 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1357 3c1ec782 2020-07-23 stsp return NULL;
1360 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1361 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1362 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1363 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1364 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1365 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1367 3c1ec782 2020-07-23 stsp free(path_got);
1368 3c1ec782 2020-07-23 stsp return NULL;
1371 5a1fbc73 2020-07-23 stsp static const struct got_error *
1372 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1373 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1374 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1375 c90c8ce3 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1376 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1378 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1379 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1380 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1381 906c123b 2020-07-23 stsp char *path_got = NULL;
1382 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1383 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1385 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1388 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1389 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1390 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1391 8ba819a3 2020-07-23 stsp * in the blob object.
1394 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1395 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1396 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1397 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1398 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1399 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1400 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1401 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1402 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1403 3b9f0f87 2020-07-23 stsp progress_arg);
1405 8ba819a3 2020-07-23 stsp if (len > 0) {
1406 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1407 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1408 8ba819a3 2020-07-23 stsp len - hdrlen);
1409 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1410 8ba819a3 2020-07-23 stsp hdrlen = 0;
1412 8ba819a3 2020-07-23 stsp } while (len != 0);
1413 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1415 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1416 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1418 3c1ec782 2020-07-23 stsp return err;
1420 3c1ec782 2020-07-23 stsp if (*is_bad_symlink) {
1421 906c123b 2020-07-23 stsp /* install as a regular file */
1422 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1423 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1424 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1425 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1426 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1427 906c123b 2020-07-23 stsp goto done;
1430 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1431 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1432 c6e8a826 2021-04-05 stsp int symlink_replaced;
1433 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1434 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1435 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1436 c90c8ce3 2020-07-23 stsp goto done;
1438 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1439 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1441 f35fa46a 2020-07-23 stsp goto done;
1442 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1443 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1444 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1445 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1446 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1447 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1449 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1450 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1453 5a1fbc73 2020-07-23 stsp goto done; /* Nothing else to do. */
1456 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1457 f4994adc 2020-10-20 stsp char *parent;
1458 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1460 f4994adc 2020-10-20 stsp goto done;
1461 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1462 f4994adc 2020-10-20 stsp free(parent);
1464 f35fa46a 2020-07-23 stsp goto done;
1466 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1467 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1469 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1470 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1471 f35fa46a 2020-07-23 stsp goto done;
1475 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1476 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1477 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1478 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1479 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1480 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1481 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1482 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1483 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1484 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1485 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1486 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1487 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1489 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1490 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1492 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1493 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1494 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1496 906c123b 2020-07-23 stsp free(path_got);
1497 8ba819a3 2020-07-23 stsp return err;
1500 8ba819a3 2020-07-23 stsp static const struct got_error *
1501 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1502 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1503 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1504 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1505 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1506 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1508 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1509 507dc3bb 2018-12-29 stsp int fd = -1;
1510 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1511 507dc3bb 2018-12-29 stsp int update = 0;
1512 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1514 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1515 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
1516 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1517 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1518 f5375317 2020-10-20 stsp char *parent;
1519 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1521 f5375317 2020-10-20 stsp return err;
1522 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1523 f5375317 2020-10-20 stsp free(parent);
1525 21908da4 2019-01-13 stsp return err;
1526 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1527 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1528 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
1529 21908da4 2019-01-13 stsp if (fd == -1)
1530 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1531 230a42bd 2019-05-11 jcs ondisk_path);
1532 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1533 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1534 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1535 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1536 3b9f0f87 2020-07-23 stsp goto done;
1538 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1539 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1540 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1541 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1542 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1543 507dc3bb 2018-12-29 stsp goto done;
1545 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1546 507dc3bb 2018-12-29 stsp ondisk_path);
1548 507dc3bb 2018-12-29 stsp goto done;
1549 507dc3bb 2018-12-29 stsp update = 1;
1552 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1555 3818e3c4 2020-11-01 naddy if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1556 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod",
1557 3818e3c4 2020-11-01 naddy update ? tmppath : ondisk_path);
1558 3818e3c4 2020-11-01 naddy goto done;
1561 bd6aa359 2020-07-23 stsp if (progress_cb) {
1562 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1563 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1565 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1566 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1569 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1570 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1572 bd6aa359 2020-07-23 stsp goto done;
1575 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1577 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1578 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1581 9d31a1d8 2018-03-11 stsp if (len > 0) {
1582 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1583 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1584 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1585 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1586 b87c6f83 2018-12-24 stsp goto done;
1587 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1588 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1589 b87c6f83 2018-12-24 stsp goto done;
1591 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1593 9d31a1d8 2018-03-11 stsp } while (len != 0);
1595 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1596 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1597 816dc654 2019-02-16 stsp goto done;
1600 507dc3bb 2018-12-29 stsp if (update) {
1601 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1602 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1603 f6d8c0ac 2020-11-09 stsp goto done;
1605 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1606 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1607 230a42bd 2019-05-11 jcs ondisk_path);
1608 68ed9ba5 2019-02-10 stsp goto done;
1610 63df146d 2020-11-09 stsp free(tmppath);
1611 63df146d 2020-11-09 stsp tmppath = NULL;
1615 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1616 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1617 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1618 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1619 507dc3bb 2018-12-29 stsp free(tmppath);
1620 6353ad76 2019-02-08 stsp return err;
1623 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1624 6353ad76 2019-02-08 stsp static const struct got_error *
1625 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1627 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1628 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1629 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1630 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1631 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1633 7154f6ce 2019-03-27 stsp int i = 0;
1634 9bdd68dd 2020-01-02 naddy char *line = NULL;
1635 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
1636 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1638 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1639 9bdd68dd 2020-01-02 naddy linelen = getline(&line, &linesize, f);
1640 9bdd68dd 2020-01-02 naddy if (linelen == -1) {
1641 7154f6ce 2019-03-27 stsp if (feof(f))
1643 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1647 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1648 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1650 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1655 9bdd68dd 2020-01-02 naddy free(line);
1657 7154f6ce 2019-03-27 stsp return err;
1660 e2b1e152 2019-07-13 stsp static int
1661 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1663 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1664 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1667 1ebedb77 2019-10-19 stsp static int
1668 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1670 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1671 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1672 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1673 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1674 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1675 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1678 c363b2c1 2019-08-03 stsp static unsigned char
1679 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1681 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1682 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1683 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1684 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1685 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1686 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1687 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1689 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1693 a919d5c4 2020-07-23 stsp static const struct got_error *
1694 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1695 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1696 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1698 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1699 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1700 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1701 a919d5c4 2020-07-23 stsp ssize_t elen;
1702 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1703 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1704 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1706 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1708 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1710 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1712 a919d5c4 2020-07-23 stsp return err;
1713 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1715 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1716 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1718 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1720 a919d5c4 2020-07-23 stsp if (len > 0) {
1721 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1722 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1723 a919d5c4 2020-07-23 stsp len - hdrlen);
1724 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1725 a919d5c4 2020-07-23 stsp hdrlen = 0;
1727 a919d5c4 2020-07-23 stsp } while (len != 0);
1728 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1730 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1731 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1732 a919d5c4 2020-07-23 stsp if (elen == -1)
1733 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1735 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1736 a919d5c4 2020-07-23 stsp if (elen == -1)
1737 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1740 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1741 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1743 a919d5c4 2020-07-23 stsp return NULL;
1746 7154f6ce 2019-03-27 stsp static const struct got_error *
1747 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1748 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1749 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1751 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1752 6353ad76 2019-02-08 stsp struct got_object_id id;
1753 6353ad76 2019-02-08 stsp size_t hdrlen;
1754 1338848f 2019-12-13 stsp int fd = -1;
1755 6353ad76 2019-02-08 stsp FILE *f = NULL;
1756 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1757 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1758 6353ad76 2019-02-08 stsp size_t flen, blen;
1759 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1761 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1762 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1765 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1766 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1767 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1769 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1770 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1771 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1772 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1773 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1775 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1776 882ef1b9 2019-12-13 stsp goto done;
1778 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1779 3d35a492 2019-12-13 stsp goto done;
1782 7f91a133 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1783 a919d5c4 2020-07-23 stsp if (fd == -1 && errno != ENOENT && errno != ELOOP)
1784 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1785 a919d5c4 2020-07-23 stsp else if (fd == -1 && errno == ELOOP) {
1786 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1787 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1788 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1789 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1790 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1791 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1793 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1794 3d35a492 2019-12-13 stsp goto done;
1796 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1797 1338848f 2019-12-13 stsp goto done;
1801 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1802 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1803 1338848f 2019-12-13 stsp goto done;
1806 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1807 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1808 1338848f 2019-12-13 stsp goto done;
1809 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1810 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1811 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1812 1338848f 2019-12-13 stsp goto done;
1815 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1816 f179e66d 2020-07-23 stsp goto done;
1818 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1819 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1820 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1821 1338848f 2019-12-13 stsp goto done;
1824 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1825 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1826 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1828 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1830 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1832 1338848f 2019-12-13 stsp goto done;
1834 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1835 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1836 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1837 a919d5c4 2020-07-23 stsp goto done;
1840 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1841 3d35a492 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1842 ab0d4361 2019-12-13 stsp if (fd == -1) {
1843 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1844 ab0d4361 2019-12-13 stsp goto done;
1848 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1849 6353ad76 2019-02-08 stsp if (f == NULL) {
1850 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1851 6353ad76 2019-02-08 stsp goto done;
1854 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1856 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1857 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1859 7154f6ce 2019-03-27 stsp goto done;
1860 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1861 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1862 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1863 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1864 7154f6ce 2019-03-27 stsp goto done;
1866 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1867 6353ad76 2019-02-08 stsp if (flen != 0)
1868 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1870 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1871 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1872 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1874 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1875 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1876 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1877 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1881 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1884 6353ad76 2019-02-08 stsp hdrlen = 0;
1887 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1888 7154f6ce 2019-03-27 stsp rewind(f);
1889 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1890 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1891 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1894 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1895 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1896 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1897 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1898 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1899 9d31a1d8 2018-03-11 stsp return err;
1903 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1904 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1906 e2b1e152 2019-07-13 stsp static const struct got_error *
1907 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1908 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1910 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1911 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1912 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1914 e2b1e152 2019-07-13 stsp return NULL;
1917 9d31a1d8 2018-03-11 stsp static const struct got_error *
1918 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1919 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1920 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1921 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1922 0584f854 2019-04-06 stsp void *progress_arg)
1924 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1925 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1926 6353ad76 2019-02-08 stsp char *ondisk_path;
1927 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1928 b8f41171 2019-02-10 stsp struct stat sb;
1930 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1931 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1934 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1935 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1936 a76c42e6 2019-08-03 stsp goto done;
1938 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1941 abb4604f 2019-07-27 stsp goto done;
1942 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1943 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1945 abb4604f 2019-07-27 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1946 c90c8ce3 2020-07-23 stsp status = GOT_STATUS_UNVERSIONED;
1949 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1951 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1952 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1953 b8f41171 2019-02-10 stsp goto done;
1955 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1957 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1958 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1960 5036ab18 2020-04-18 stsp goto done;
1963 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1964 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1965 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1967 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1968 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1969 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1970 c6e8a826 2021-04-05 stsp * updating contents of this file.
1972 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1973 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1974 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1975 c6e8a826 2021-04-05 stsp /* Same commit. */
1976 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1977 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1979 e2b1e152 2019-07-13 stsp goto done;
1980 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1982 6353ad76 2019-02-08 stsp goto done;
1984 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1985 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1986 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1987 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
1988 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1989 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1991 0f58026f 2021-04-05 stsp goto done;
1992 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1994 b8f41171 2019-02-10 stsp goto done;
1998 56e0773d 2019-11-28 stsp err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
2000 6353ad76 2019-02-08 stsp goto done;
2002 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2003 234035bc 2019-06-01 stsp int update_timestamps;
2004 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
2005 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2006 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
2007 234035bc 2019-06-01 stsp struct got_object_id id2;
2008 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2009 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
2011 f69721c3 2019-10-21 stsp goto done;
2013 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
2014 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
2015 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2016 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
2017 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
2018 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
2019 f69721c3 2019-10-21 stsp goto done;
2021 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2022 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2023 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2024 234035bc 2019-06-01 stsp goto done;
2027 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2028 36bf999c 2020-07-23 stsp char *link_target;
2029 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
2031 36bf999c 2020-07-23 stsp goto done;
2032 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
2033 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
2034 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
2035 36bf999c 2020-07-23 stsp free(link_target);
2037 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
2038 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
2039 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
2040 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
2042 f69721c3 2019-10-21 stsp free(label_orig);
2043 234035bc 2019-06-01 stsp if (blob2)
2044 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
2046 909d120e 2019-09-22 stsp goto done;
2048 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
2049 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2050 234035bc 2019-06-01 stsp * unmodified files again.
2052 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2053 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2054 234035bc 2019-06-01 stsp update_timestamps);
2055 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2056 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2057 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2058 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2059 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2061 1ee397ad 2019-07-12 stsp goto done;
2062 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2063 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2065 13d9040b 2019-03-27 stsp goto done;
2067 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2068 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2069 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2070 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2071 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2072 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2073 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2075 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2076 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2077 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2078 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2079 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2082 13d9040b 2019-03-27 stsp goto done;
2085 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2086 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2087 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2089 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2090 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2091 054041d0 2020-06-24 stsp &blob->id);
2094 13d9040b 2019-03-27 stsp goto done;
2096 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2097 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2098 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2101 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2103 6353ad76 2019-02-08 stsp free(ondisk_path);
2104 8da9e5f4 2019-01-12 stsp return err;
2107 90285c3b 2019-01-08 stsp static const struct got_error *
2108 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2110 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2111 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2113 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2114 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2116 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2117 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2118 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2120 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2121 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2123 1c4cdd89 2021-06-20 stsp goto done;
2124 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2125 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2126 fddefe3b 2020-10-20 stsp free(ondisk_path);
2127 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2128 1c4cdd89 2021-06-20 stsp parent = NULL;
2129 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2130 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2131 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2132 fddefe3b 2020-10-20 stsp ondisk_path);
2135 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2141 90285c3b 2019-01-08 stsp free(ondisk_path);
2142 1c4cdd89 2021-06-20 stsp free(parent);
2143 90285c3b 2019-01-08 stsp return err;
2146 708d8e67 2019-03-27 stsp static const struct got_error *
2147 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2148 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2149 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2151 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2152 708d8e67 2019-03-27 stsp unsigned char status;
2153 708d8e67 2019-03-27 stsp struct stat sb;
2154 708d8e67 2019-03-27 stsp char *ondisk_path;
2156 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2157 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2159 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2161 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2163 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2165 5a58a424 2020-06-23 stsp goto done;
2167 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2168 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2169 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2170 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2171 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2172 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2173 993e2a1b 2020-07-23 stsp goto done;
2175 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2176 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2177 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2178 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2180 993e2a1b 2020-07-23 stsp goto done;
2181 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2182 993e2a1b 2020-07-23 stsp ie->path);
2183 993e2a1b 2020-07-23 stsp goto done;
2186 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2187 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2188 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2190 5a58a424 2020-06-23 stsp goto done;
2192 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2193 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2195 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2196 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2198 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2200 5a58a424 2020-06-23 stsp goto done;
2201 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2202 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2204 5a58a424 2020-06-23 stsp goto done;
2206 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2209 5a58a424 2020-06-23 stsp free(ondisk_path);
2210 fc6346c4 2019-03-27 stsp return err;
2213 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2214 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2215 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2216 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2217 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2218 8da9e5f4 2019-01-12 stsp void *progress_arg;
2219 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2220 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2223 512f0d0e 2019-01-02 stsp static const struct got_error *
2224 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2225 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2227 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2229 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2230 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2232 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2233 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2236 8da9e5f4 2019-01-12 stsp static const struct got_error *
2237 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2239 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2241 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2242 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2244 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2245 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2248 9d31a1d8 2018-03-11 stsp static const struct got_error *
2249 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2251 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2252 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2253 8da9e5f4 2019-01-12 stsp char *path;
2255 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2256 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2258 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2259 63c5ca5d 2019-08-24 stsp return NULL;
2261 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2262 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2264 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2266 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2267 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2269 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2270 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2272 8da9e5f4 2019-01-12 stsp free(path);
2273 0cd1c46a 2019-03-11 stsp return err;
2276 b2118c49 2020-07-28 stsp const struct got_error *
2277 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2279 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2281 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2282 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2283 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2284 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2287 b2118c49 2020-07-28 stsp return NULL;
2290 818c7501 2019-07-11 stsp static const struct got_error *
2291 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2293 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2294 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2296 517bab73 2019-03-11 stsp *refname = NULL;
2298 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2300 b2118c49 2020-07-28 stsp return err;
2302 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2303 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2304 0647c563 2019-03-11 stsp *refname = NULL;
2306 517bab73 2019-03-11 stsp free(uuidstr);
2307 517bab73 2019-03-11 stsp return err;
2310 818c7501 2019-07-11 stsp const struct got_error *
2311 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2313 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2316 818c7501 2019-07-11 stsp static const struct got_error *
2317 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2319 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2320 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2323 818c7501 2019-07-11 stsp static const struct got_error *
2324 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2326 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2329 818c7501 2019-07-11 stsp static const struct got_error *
2330 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2332 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2333 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2336 818c7501 2019-07-11 stsp static const struct got_error *
2337 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2339 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2340 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2343 0ebf8283 2019-07-24 stsp static const struct got_error *
2344 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2346 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2347 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2350 0ebf8283 2019-07-24 stsp static const struct got_error *
2351 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2353 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2354 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2357 0ebf8283 2019-07-24 stsp static const struct got_error *
2358 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2360 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2361 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2364 0ebf8283 2019-07-24 stsp static const struct got_error *
2365 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2367 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2368 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2371 0ebf8283 2019-07-24 stsp const struct got_error *
2372 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2373 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2375 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2376 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2377 0ebf8283 2019-07-24 stsp *path = NULL;
2378 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2380 0ebf8283 2019-07-24 stsp return NULL;
2384 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2385 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2387 517bab73 2019-03-11 stsp static const struct got_error *
2388 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2390 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2391 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2392 517bab73 2019-03-11 stsp char *refname;
2394 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2396 517bab73 2019-03-11 stsp return err;
2398 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2400 0cd1c46a 2019-03-11 stsp goto done;
2402 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2404 0cd1c46a 2019-03-11 stsp free(refname);
2406 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2407 3e3a69f1 2019-07-25 stsp return err;
2410 3e3a69f1 2019-07-25 stsp static const struct got_error *
2411 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2413 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2415 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2416 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2417 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2418 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2420 9d31a1d8 2018-03-11 stsp return err;
2424 ebf99748 2019-05-09 stsp static const struct got_error *
2425 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2426 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2428 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2429 ebf99748 2019-05-09 stsp FILE *index = NULL;
2431 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2432 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2433 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2434 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2436 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2438 ebf99748 2019-05-09 stsp goto done;
2440 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
2441 ebf99748 2019-05-09 stsp if (index == NULL) {
2442 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2443 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2445 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2446 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2447 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2450 ebf99748 2019-05-09 stsp if (err) {
2451 ebf99748 2019-05-09 stsp free(*fileindex_path);
2452 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2453 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2454 ebf99748 2019-05-09 stsp *fileindex = NULL;
2456 ebf99748 2019-05-09 stsp return err;
2459 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2460 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2461 c932eeeb 2019-05-22 stsp const char *path;
2462 c932eeeb 2019-05-22 stsp size_t path_len;
2463 c932eeeb 2019-05-22 stsp const char *entry_name;
2464 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2465 a484d721 2019-06-10 stsp void *progress_arg;
2468 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2469 c932eeeb 2019-05-22 stsp static const struct got_error *
2470 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2472 1ee397ad 2019-07-12 stsp const struct got_error *err;
2473 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2475 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2476 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2477 c932eeeb 2019-05-22 stsp return NULL;
2478 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2479 102ff934 2019-06-11 stsp return NULL;
2481 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2482 a769b60b 2021-06-27 stsp return NULL;
2484 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2485 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2486 c932eeeb 2019-05-22 stsp return NULL;
2488 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2489 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2490 edd02c5e 2019-07-11 stsp ie->path);
2492 1ee397ad 2019-07-12 stsp return err;
2494 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2495 c932eeeb 2019-05-22 stsp return NULL;
2498 a615e0e7 2020-12-16 stsp static const struct got_error *
2499 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2500 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2501 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2503 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2505 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2506 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2507 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2508 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2509 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2510 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2512 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2513 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2516 9c6338c4 2019-06-02 stsp static const struct got_error *
2517 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2519 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2520 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2521 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2522 867630bb 2020-01-17 stsp struct timespec timeout;
2524 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2525 9c6338c4 2019-06-02 stsp fileindex_path);
2527 9c6338c4 2019-06-02 stsp goto done;
2529 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2531 9c6338c4 2019-06-02 stsp goto done;
2533 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2534 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2535 9c6338c4 2019-06-02 stsp fileindex_path);
2536 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2540 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2541 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2542 867630bb 2020-01-17 stsp * was recorded in the file index.
2544 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2545 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2546 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2548 9c6338c4 2019-06-02 stsp if (new_index)
2549 9c6338c4 2019-06-02 stsp fclose(new_index);
2550 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2551 9c6338c4 2019-06-02 stsp return err;
2554 6ced4176 2019-07-12 stsp static const struct got_error *
2555 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2556 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2557 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
2559 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2560 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2561 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2562 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2564 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2565 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2566 6ced4176 2019-07-12 stsp *tree_id = NULL;
2568 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2569 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2570 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2571 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2572 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2573 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2574 6ced4176 2019-07-12 stsp goto done;
2576 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2577 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
2579 6ced4176 2019-07-12 stsp goto done;
2580 6ced4176 2019-07-12 stsp return NULL;
2583 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2585 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2586 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2587 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2588 6ced4176 2019-07-12 stsp goto done;
2591 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2592 6ced4176 2019-07-12 stsp in_repo_path);
2594 6ced4176 2019-07-12 stsp goto done;
2596 6ced4176 2019-07-12 stsp free(in_repo_path);
2597 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2599 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2601 6ced4176 2019-07-12 stsp goto done;
2603 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2604 6ced4176 2019-07-12 stsp /* Check out a single file. */
2605 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2606 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2607 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2608 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2609 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2610 6ced4176 2019-07-12 stsp goto done;
2612 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2613 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2614 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2615 6ced4176 2019-07-12 stsp goto done;
2618 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2619 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2621 6ced4176 2019-07-12 stsp return err;
2622 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2623 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2624 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2625 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2626 6ced4176 2019-07-12 stsp goto done;
2629 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2630 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
2632 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2633 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2634 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2635 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2636 6ced4176 2019-07-12 stsp goto done;
2638 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2639 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2640 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2641 6ced4176 2019-07-12 stsp goto done;
2646 6ced4176 2019-07-12 stsp free(in_repo_path);
2647 6ced4176 2019-07-12 stsp if (err) {
2648 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2649 6ced4176 2019-07-12 stsp free(*tree_relpath);
2650 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2651 6ced4176 2019-07-12 stsp free(*tree_id);
2652 6ced4176 2019-07-12 stsp *tree_id = NULL;
2654 07ed472d 2019-07-12 stsp return err;
2657 07ed472d 2019-07-12 stsp static const struct got_error *
2658 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2659 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2660 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2661 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2663 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2664 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2665 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2666 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2667 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2669 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2670 7f47418f 2019-12-20 stsp if (err) {
2671 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2672 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2673 7f47418f 2019-12-20 stsp goto done;
2674 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2675 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2677 7f47418f 2019-12-20 stsp return err;
2680 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2681 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2683 07ed472d 2019-07-12 stsp goto done;
2685 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2687 07ed472d 2019-07-12 stsp goto done;
2689 07ed472d 2019-07-12 stsp if (entry_name &&
2690 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2691 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2692 07ed472d 2019-07-12 stsp goto done;
2695 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2696 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2697 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2698 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2699 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2700 07ed472d 2019-07-12 stsp arg.repo = repo;
2701 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2702 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2703 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2704 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2705 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2706 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2709 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2710 07ed472d 2019-07-12 stsp if (commit)
2711 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2712 6ced4176 2019-07-12 stsp return err;
2715 9d31a1d8 2018-03-11 stsp const struct got_error *
2716 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2717 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2718 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2719 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2721 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2722 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2723 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2724 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2725 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2726 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2727 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2728 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2729 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2730 f2ea84fa 2019-07-27 stsp int entry_type;
2731 f2ea84fa 2019-07-27 stsp char *relpath;
2732 f2ea84fa 2019-07-27 stsp char *entry_name;
2733 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2734 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2736 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2738 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2740 9d31a1d8 2018-03-11 stsp return err;
2742 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2743 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2744 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2745 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2746 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2747 f2ea84fa 2019-07-27 stsp goto done;
2750 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2751 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2752 f2ea84fa 2019-07-27 stsp if (err) {
2753 f2ea84fa 2019-07-27 stsp free(tpd);
2754 6ced4176 2019-07-12 stsp goto done;
2757 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2758 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2759 f2ea84fa 2019-07-27 stsp if (err) {
2760 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2761 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2762 f2ea84fa 2019-07-27 stsp free(tpd);
2763 f2ea84fa 2019-07-27 stsp goto done;
2766 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2768 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2772 51514078 2018-12-25 stsp * Read the file index.
2773 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2774 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2776 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2778 c4cdcb68 2019-04-03 stsp goto done;
2780 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2781 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2782 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2784 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2785 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2786 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2790 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2791 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2792 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2793 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2794 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2795 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2796 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2797 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2801 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2803 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2804 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2805 af12c6b9 2019-06-04 stsp err = sync_err;
2807 9c6338c4 2019-06-02 stsp free(fileindex_path);
2809 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2810 9d31a1d8 2018-03-11 stsp if (commit)
2811 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2812 5ade8233 2019-07-12 stsp if (fileindex)
2813 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2814 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2815 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2816 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2817 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2818 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2819 f2ea84fa 2019-07-27 stsp free(tpd);
2821 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2822 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2823 9d31a1d8 2018-03-11 stsp err = unlockerr;
2824 f8d1f275 2019-02-04 stsp return err;
2827 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2828 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2829 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2830 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2831 234035bc 2019-06-01 stsp void *progress_arg;
2832 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2833 234035bc 2019-06-01 stsp void *cancel_arg;
2834 f69721c3 2019-10-21 stsp const char *label_orig;
2835 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2838 234035bc 2019-06-01 stsp static const struct got_error *
2839 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2840 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
2841 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
2842 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2844 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2845 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2846 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2847 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2848 234035bc 2019-06-01 stsp struct stat sb;
2849 234035bc 2019-06-01 stsp unsigned char status;
2850 234035bc 2019-06-01 stsp int local_changes_subsumed;
2851 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2852 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2854 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2855 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2856 d6c87207 2019-08-02 stsp strlen(path2));
2857 1ee397ad 2019-07-12 stsp if (ie == NULL)
2858 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2859 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2861 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2862 234035bc 2019-06-01 stsp path2) == -1)
2863 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2865 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2868 234035bc 2019-06-01 stsp goto done;
2870 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2871 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2872 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2873 234035bc 2019-06-01 stsp goto done;
2875 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2876 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2877 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2878 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2879 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2880 234035bc 2019-06-01 stsp goto done;
2883 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2884 36bf999c 2020-07-23 stsp char *link_target2;
2885 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2887 36bf999c 2020-07-23 stsp goto done;
2888 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2889 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2890 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2891 36bf999c 2020-07-23 stsp free(link_target2);
2895 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
2896 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
2897 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
2898 1af628f4 2021-06-03 stsp goto done;
2900 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2901 1af628f4 2021-06-03 stsp f_orig, blob1);
2903 1af628f4 2021-06-03 stsp goto done;
2905 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
2906 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
2907 1af628f4 2021-06-03 stsp goto done;
2908 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2909 54d5be07 2021-06-03 stsp f_deriv2, blob2);
2911 1af628f4 2021-06-03 stsp goto done;
2913 1af628f4 2021-06-03 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
2914 1af628f4 2021-06-03 stsp if (fd == -1) {
2915 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
2916 1af628f4 2021-06-03 stsp ondisk_path);
2917 1af628f4 2021-06-03 stsp goto done;
2919 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
2920 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
2921 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
2922 1af628f4 2021-06-03 stsp ondisk_path);
2923 1af628f4 2021-06-03 stsp close(fd);
2924 1af628f4 2021-06-03 stsp goto done;
2926 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
2928 1af628f4 2021-06-03 stsp goto done;
2929 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
2930 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2931 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
2932 1af628f4 2021-06-03 stsp goto done;
2934 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
2935 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2936 54d5be07 2021-06-03 stsp sb.st_mode, a->label_orig, NULL, label_deriv2,
2937 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
2938 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
2940 234035bc 2019-06-01 stsp } else if (blob1) {
2941 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2942 d6c87207 2019-08-02 stsp strlen(path1));
2943 1ee397ad 2019-07-12 stsp if (ie == NULL)
2944 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2945 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2947 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2948 2b92fad7 2019-06-02 stsp path1) == -1)
2949 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2951 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2954 2b92fad7 2019-06-02 stsp goto done;
2956 2b92fad7 2019-06-02 stsp switch (status) {
2957 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2958 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2959 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2961 1ee397ad 2019-07-12 stsp goto done;
2962 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2964 2b92fad7 2019-06-02 stsp goto done;
2966 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2968 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2969 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2970 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2971 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2973 1ee397ad 2019-07-12 stsp goto done;
2975 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2977 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
2978 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
2979 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
2981 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
2982 0a22ca1a 2020-09-23 stsp * exists in the repository.
2984 0a22ca1a 2020-09-23 stsp err = got_object_blob_file_create(&id, &blob1_f, path1);
2986 0a22ca1a 2020-09-23 stsp goto done;
2987 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
2988 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2989 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
2991 0a22ca1a 2020-09-23 stsp goto done;
2992 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
2995 0a22ca1a 2020-09-23 stsp goto done;
2997 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
3000 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3001 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
3003 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
3004 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
3007 0a22ca1a 2020-09-23 stsp goto done;
3010 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
3011 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
3012 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3013 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
3015 1ee397ad 2019-07-12 stsp goto done;
3017 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
3018 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
3020 1ee397ad 2019-07-12 stsp goto done;
3025 234035bc 2019-06-01 stsp } else if (blob2) {
3026 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3027 234035bc 2019-06-01 stsp path2) == -1)
3028 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3029 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3030 d6c87207 2019-08-02 stsp strlen(path2));
3032 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
3033 7f91a133 2019-12-13 stsp -1, NULL, repo);
3035 234035bc 2019-06-01 stsp goto done;
3036 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3037 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3038 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3039 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
3040 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3041 1ee397ad 2019-07-12 stsp status, path2);
3042 234035bc 2019-06-01 stsp goto done;
3044 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3045 36bf999c 2020-07-23 stsp char *link_target2;
3046 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3049 36bf999c 2020-07-23 stsp goto done;
3050 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3051 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3052 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3053 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3054 36bf999c 2020-07-23 stsp free(link_target2);
3055 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3056 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3057 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3058 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3059 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3060 526a746f 2020-07-23 stsp a->progress_arg);
3062 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3063 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3066 dfe9fba0 2020-07-23 stsp goto done;
3067 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3068 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
3069 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, blob2->id.sha1,
3070 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
3072 234035bc 2019-06-01 stsp goto done;
3075 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
3076 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
3077 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
3078 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
3079 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
3080 c90c8ce3 2020-07-23 stsp 0, 1, repo, a->progress_cb, a->progress_arg);
3082 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
3083 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3084 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
3087 234035bc 2019-06-01 stsp goto done;
3088 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
3090 234035bc 2019-06-01 stsp goto done;
3091 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
3092 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, NULL, NULL, 1);
3093 3969253a 2020-03-07 stsp if (err) {
3094 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3095 3969253a 2020-03-07 stsp goto done;
3097 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3098 2b92fad7 2019-06-02 stsp if (err) {
3099 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
3100 2b92fad7 2019-06-02 stsp goto done;
3102 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
3103 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
3104 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
3109 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3110 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3111 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3112 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3113 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3114 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3115 1af628f4 2021-06-03 stsp free(id_str);
3116 54d5be07 2021-06-03 stsp free(label_deriv2);
3117 234035bc 2019-06-01 stsp free(ondisk_path);
3118 234035bc 2019-06-01 stsp return err;
3121 69de9dd4 2021-09-03 stsp static const struct got_error *
3122 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3124 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3126 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3127 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3128 69de9dd4 2021-09-03 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3129 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3130 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3132 69de9dd4 2021-09-03 stsp return NULL;
3135 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3136 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3137 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3138 234035bc 2019-06-01 stsp struct got_repository *repo;
3141 234035bc 2019-06-01 stsp static const struct got_error *
3142 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3143 69de9dd4 2021-09-03 stsp struct got_blob_object *blob2, struct got_object_id *id1,
3144 69de9dd4 2021-09-03 stsp struct got_object_id *id2, const char *path1, const char *path2,
3145 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3147 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3148 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3149 234035bc 2019-06-01 stsp unsigned char status;
3150 234035bc 2019-06-01 stsp struct stat sb;
3151 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3152 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3153 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3154 234035bc 2019-06-01 stsp char *ondisk_path;
3156 69de9dd4 2021-09-03 stsp if (id == NULL)
3157 69de9dd4 2021-09-03 stsp return NULL;
3159 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3160 69de9dd4 2021-09-03 stsp if (ie == NULL)
3161 69de9dd4 2021-09-03 stsp return NULL;
3163 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3165 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3167 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3168 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3169 5546d466 2021-09-02 stsp free(ondisk_path);
3171 234035bc 2019-06-01 stsp return err;
3172 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3173 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3175 234035bc 2019-06-01 stsp return NULL;
3178 818c7501 2019-07-11 stsp static const struct got_error *
3179 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3180 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3181 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3182 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3183 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3185 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3186 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3187 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3188 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3189 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3190 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3192 03415a1a 2019-06-02 stsp if (commit_id1) {
3193 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3194 03415a1a 2019-06-02 stsp worktree->path_prefix);
3195 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3196 03415a1a 2019-06-02 stsp goto done;
3198 69d57f3d 2020-07-31 stsp if (tree_id1) {
3199 69d57f3d 2020-07-31 stsp char *id_str;
3201 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3203 03415a1a 2019-06-02 stsp goto done;
3205 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3207 f69721c3 2019-10-21 stsp goto done;
3209 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3210 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3211 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3212 f69721c3 2019-10-21 stsp free(id_str);
3213 f69721c3 2019-10-21 stsp goto done;
3215 f69721c3 2019-10-21 stsp free(id_str);
3218 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3219 234035bc 2019-06-01 stsp worktree->path_prefix);
3221 234035bc 2019-06-01 stsp goto done;
3223 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3225 234035bc 2019-06-01 stsp goto done;
3227 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3228 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3229 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3230 69de9dd4 2021-09-03 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3231 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3233 69de9dd4 2021-09-03 stsp goto done;
3235 234035bc 2019-06-01 stsp arg.worktree = worktree;
3236 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3237 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3238 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3239 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3240 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3241 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3242 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3243 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3244 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3245 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3246 af12c6b9 2019-06-04 stsp err = sync_err;
3248 234035bc 2019-06-01 stsp if (tree1)
3249 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3250 234035bc 2019-06-01 stsp if (tree2)
3251 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3252 f69721c3 2019-10-21 stsp free(label_orig);
3253 818c7501 2019-07-11 stsp return err;
3256 818c7501 2019-07-11 stsp const struct got_error *
3257 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3258 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3259 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3260 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3262 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3263 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3264 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3266 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3268 818c7501 2019-07-11 stsp return err;
3270 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3272 818c7501 2019-07-11 stsp goto done;
3274 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3275 69de9dd4 2021-09-03 stsp worktree);
3277 818c7501 2019-07-11 stsp goto done;
3279 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3280 818c7501 2019-07-11 stsp commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3282 818c7501 2019-07-11 stsp if (fileindex)
3283 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3284 818c7501 2019-07-11 stsp free(fileindex_path);
3285 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3286 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3287 234035bc 2019-06-01 stsp err = unlockerr;
3288 234035bc 2019-06-01 stsp return err;
3291 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3292 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3293 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3294 927df6b7 2019-02-10 stsp const char *status_path;
3295 927df6b7 2019-02-10 stsp size_t status_path_len;
3296 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3297 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3298 f8d1f275 2019-02-04 stsp void *status_arg;
3299 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3300 f8d1f275 2019-02-04 stsp void *cancel_arg;
3301 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3302 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3303 f2a9dc41 2019-12-13 tracey int report_unchanged;
3304 3143d852 2020-06-25 stsp int no_ignores;
3307 f8d1f275 2019-02-04 stsp static const struct got_error *
3308 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3309 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3310 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3311 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3313 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3314 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3315 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3316 927df6b7 2019-02-10 stsp struct stat sb;
3317 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3318 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3319 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3321 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3323 98eaaa12 2019-08-03 stsp return err;
3325 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3326 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3327 98eaaa12 2019-08-03 stsp return NULL;
3329 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3330 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3331 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3333 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3334 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3335 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3337 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3338 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3339 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3340 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3341 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3344 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3345 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3348 927df6b7 2019-02-10 stsp static const struct got_error *
3349 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3350 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3352 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3353 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3354 f8d1f275 2019-02-04 stsp char *abspath;
3356 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3357 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3359 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3360 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3361 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3362 927df6b7 2019-02-10 stsp return NULL;
3364 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3365 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3366 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3367 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3369 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3370 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3371 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3374 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3375 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3376 f8d1f275 2019-02-04 stsp free(abspath);
3377 9d31a1d8 2018-03-11 stsp return err;
3380 f8d1f275 2019-02-04 stsp static const struct got_error *
3381 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3383 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3384 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3385 2ec1f75b 2019-03-26 stsp unsigned char status;
3387 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3388 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3390 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3391 927df6b7 2019-02-10 stsp return NULL;
3393 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3394 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3395 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3396 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3398 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3399 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3400 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3404 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3406 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3408 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3409 6841da00 2019-08-08 stsp free((char *)pe->path);
3410 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3414 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3416 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3418 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3419 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3420 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3421 6841da00 2019-08-08 stsp free((char *)pe->path);
3423 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3426 6841da00 2019-08-08 stsp static const struct got_error *
3427 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3429 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3430 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3431 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3432 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3433 6841da00 2019-08-08 stsp size_t linesize = 0;
3434 6841da00 2019-08-08 stsp ssize_t linelen;
3436 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3437 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3438 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3439 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3441 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3442 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3443 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3445 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3446 bd8de430 2019-10-04 stsp if (line[0] == '#')
3449 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3450 bd8de430 2019-10-04 stsp if (line[0] == '!')
3453 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3454 6841da00 2019-08-08 stsp line) == -1) {
3455 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3456 6841da00 2019-08-08 stsp goto done;
3458 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3460 6841da00 2019-08-08 stsp goto done;
3462 6841da00 2019-08-08 stsp if (ferror(f)) {
3463 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3464 6841da00 2019-08-08 stsp goto done;
3467 6841da00 2019-08-08 stsp dirpath = strdup(path);
3468 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3469 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3470 6841da00 2019-08-08 stsp goto done;
3472 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3474 6841da00 2019-08-08 stsp free(line);
3475 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3476 6841da00 2019-08-08 stsp free(dirpath);
3477 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3479 6841da00 2019-08-08 stsp return err;
3483 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3485 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3487 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3488 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3489 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3490 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3492 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3493 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3495 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3497 bd8de430 2019-10-04 stsp pattern += 3;
3499 bd8de430 2019-10-04 stsp while (*p) {
3500 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3501 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3502 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3503 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3505 bd8de430 2019-10-04 stsp while (*p == '/')
3515 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3516 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3517 6841da00 2019-08-08 stsp * ignores backwards.
3519 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3520 6841da00 2019-08-08 stsp while (pe) {
3521 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3522 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3523 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3524 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3525 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3526 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3527 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3528 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3529 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3534 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3540 6841da00 2019-08-08 stsp static const struct got_error *
3541 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3542 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3544 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3545 6841da00 2019-08-08 stsp char *ignorespath;
3546 886cec17 2019-12-15 stsp int fd = -1;
3547 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3549 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3550 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3551 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3553 886cec17 2019-12-15 stsp if (dirfd != -1) {
3554 886cec17 2019-12-15 stsp fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3555 886cec17 2019-12-15 stsp if (fd == -1) {
3556 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3557 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3558 886cec17 2019-12-15 stsp ignorespath);
3560 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3561 886cec17 2019-12-15 stsp if (ignoresfile == NULL)
3562 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3563 886cec17 2019-12-15 stsp ignorespath);
3566 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3570 886cec17 2019-12-15 stsp ignoresfile = fopen(ignorespath, "r");
3571 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3572 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3573 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3574 886cec17 2019-12-15 stsp ignorespath);
3576 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3579 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3580 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3581 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3582 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3583 6841da00 2019-08-08 stsp free(ignorespath);
3584 6841da00 2019-08-08 stsp return err;
3587 f8d1f275 2019-02-04 stsp static const struct got_error *
3588 7f91a133 2019-12-13 stsp status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3590 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3591 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3592 f8d1f275 2019-02-04 stsp char *path = NULL;
3594 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3595 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3597 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3598 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3599 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3601 f8d1f275 2019-02-04 stsp path = de->d_name;
3604 3143d852 2020-06-25 stsp if (de->d_type != DT_DIR &&
3605 3143d852 2020-06-25 stsp got_path_is_child(path, a->status_path, a->status_path_len)
3606 ff56836b 2021-07-08 stsp && !match_ignores(a->ignores, path))
3607 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3608 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3609 f8d1f275 2019-02-04 stsp if (parent_path[0])
3610 f8d1f275 2019-02-04 stsp free(path);
3611 b72f483a 2019-02-05 stsp return err;
3614 347d1d3e 2019-07-12 stsp static const struct got_error *
3615 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3617 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3618 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3620 3143d852 2020-06-25 stsp if (a->no_ignores)
3621 3143d852 2020-06-25 stsp return NULL;
3623 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3624 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3626 3143d852 2020-06-25 stsp return err;
3628 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3629 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3631 3143d852 2020-06-25 stsp return err;
3634 3143d852 2020-06-25 stsp static const struct got_error *
3635 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3636 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3637 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3638 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3640 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3641 abb4604f 2019-07-27 stsp struct stat sb;
3643 0e33f8e0 2021-09-01 stsp if (!no_ignores && match_ignores(ignores, path))
3644 ff56836b 2021-07-08 stsp return NULL;
3646 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3648 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3649 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3651 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3652 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3653 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3654 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3655 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3656 abb4604f 2019-07-27 stsp return NULL;
3659 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3660 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3661 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3663 abb4604f 2019-07-27 stsp return NULL;
3666 3143d852 2020-06-25 stsp static const struct got_error *
3667 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3668 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3670 3143d852 2020-06-25 stsp const struct got_error *err;
3671 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3673 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3674 3143d852 2020-06-25 stsp ".cvsignore");
3676 3143d852 2020-06-25 stsp return err;
3678 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3679 3143d852 2020-06-25 stsp ".gitignore");
3681 3143d852 2020-06-25 stsp return err;
3683 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3684 3143d852 2020-06-25 stsp if (err) {
3685 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3686 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3687 3143d852 2020-06-25 stsp return err;
3689 3143d852 2020-06-25 stsp for (;;) {
3690 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3691 3143d852 2020-06-25 stsp ".cvsignore");
3694 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3695 3143d852 2020-06-25 stsp ".gitignore");
3698 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3699 3143d852 2020-06-25 stsp if (err) {
3700 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3701 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3704 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3706 b737c85a 2020-06-26 stsp free(parent_path);
3707 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3708 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3711 b737c85a 2020-06-26 stsp free(parent_path);
3712 b737c85a 2020-06-26 stsp free(next_parent_path);
3713 3143d852 2020-06-25 stsp return err;
3716 abb4604f 2019-07-27 stsp static const struct got_error *
3717 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3718 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3719 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3720 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3721 f2a9dc41 2019-12-13 tracey int report_unchanged)
3723 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3724 6fc93f37 2019-12-13 stsp int fd = -1;
3725 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3726 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3727 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3728 ff56836b 2021-07-08 stsp struct got_pathlist_head ignores;
3730 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3732 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3733 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3734 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3736 6fc93f37 2019-12-13 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3737 6fc93f37 2019-12-13 stsp if (fd == -1) {
3738 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3739 00bb5ea0 2020-07-23 stsp errno != ELOOP)
3740 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3742 ff56836b 2021-07-08 stsp if (!no_ignores) {
3743 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3744 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
3746 ff56836b 2021-07-08 stsp goto done;
3748 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3749 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3750 0e33f8e0 2021-09-01 stsp report_unchanged, &ignores, no_ignores);
3753 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3754 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3755 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3756 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3757 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3758 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3759 abb4604f 2019-07-27 stsp arg.status_path = path;
3760 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3761 abb4604f 2019-07-27 stsp arg.repo = repo;
3762 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3763 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3764 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3765 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3766 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3767 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3768 022fae89 2019-12-06 tracey if (!no_ignores) {
3769 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3770 3143d852 2020-06-25 stsp worktree->root_path, path);
3772 3143d852 2020-06-25 stsp goto done;
3774 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
3775 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3776 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3779 ff56836b 2021-07-08 stsp free_ignores(&ignores);
3780 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3781 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3782 927df6b7 2019-02-10 stsp free(ondisk_path);
3783 347d1d3e 2019-07-12 stsp return err;
3786 347d1d3e 2019-07-12 stsp const struct got_error *
3787 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3788 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3789 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3790 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3792 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3793 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3794 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3795 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3797 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3799 347d1d3e 2019-07-12 stsp return err;
3801 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3802 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3803 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
3804 f6343036 2021-06-22 stsp no_ignores, 0);
3808 f8d1f275 2019-02-04 stsp free(fileindex_path);
3809 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3810 f8d1f275 2019-02-04 stsp return err;
3813 6c7ab921 2019-03-18 stsp const struct got_error *
3814 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3815 6c7ab921 2019-03-18 stsp const char *arg)
3817 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3818 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3819 6c7ab921 2019-03-18 stsp size_t len;
3820 00bb5ea0 2020-07-23 stsp struct stat sb;
3821 01740607 2020-11-04 stsp char *abspath = NULL;
3822 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3824 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3826 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3827 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3828 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3830 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3831 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3832 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3833 00bb5ea0 2020-07-23 stsp goto done;
3835 727173c3 2020-11-06 stsp sb.st_mode = 0;
3837 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3839 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3840 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3841 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3842 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3844 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3845 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3846 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3847 00bb5ea0 2020-07-23 stsp goto done;
3851 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3852 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3854 d0710d08 2019-07-22 stsp goto done;
3855 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3856 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3857 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3858 00bb5ea0 2020-07-23 stsp goto done;
3861 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3862 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3863 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3864 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3865 00bb5ea0 2020-07-23 stsp goto done;
3867 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3868 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3869 01740607 2020-11-04 stsp goto done;
3871 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
3872 01740607 2020-11-04 stsp sizeof(canonpath));
3874 00bb5ea0 2020-07-23 stsp goto done;
3875 01740607 2020-11-04 stsp resolved = strdup(canonpath);
3876 01740607 2020-11-04 stsp if (resolved == NULL) {
3877 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
3878 01740607 2020-11-04 stsp goto done;
3883 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3884 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3885 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3886 6c7ab921 2019-03-18 stsp goto done;
3889 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3890 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3891 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3893 f6d88e1a 2019-05-29 stsp goto done;
3895 f6d88e1a 2019-05-29 stsp path = strdup("");
3896 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3897 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3898 f6d88e1a 2019-05-29 stsp goto done;
3902 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3903 6c7ab921 2019-03-18 stsp len = strlen(path);
3904 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3905 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3909 01740607 2020-11-04 stsp free(abspath);
3910 6c7ab921 2019-03-18 stsp free(resolved);
3911 d0710d08 2019-07-22 stsp free(cwd);
3912 6c7ab921 2019-03-18 stsp if (err == NULL)
3913 6c7ab921 2019-03-18 stsp *wt_path = path;
3915 6c7ab921 2019-03-18 stsp free(path);
3916 d00136be 2019-03-26 stsp return err;
3919 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3920 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3921 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3922 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3923 4e68cba3 2019-11-23 stsp void *progress_arg;
3924 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3927 4e68cba3 2019-11-23 stsp static const struct got_error *
3928 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3929 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3930 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3931 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3933 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3934 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3935 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3936 6d022e97 2019-08-04 stsp struct stat sb;
3937 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3939 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3940 dbb83fbd 2019-12-12 stsp relpath) == -1)
3941 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3943 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3945 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3946 12463d8b 2019-12-13 stsp de_name, a->repo);
3948 dbb83fbd 2019-12-12 stsp goto done;
3949 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3950 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3951 dbb83fbd 2019-12-12 stsp goto done;
3952 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3954 dbb83fbd 2019-12-12 stsp goto done;
3957 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3958 dbb83fbd 2019-12-12 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3959 dbb83fbd 2019-12-12 stsp goto done;
3962 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3964 4e68cba3 2019-11-23 stsp goto done;
3965 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3966 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
3967 3969253a 2020-03-07 stsp if (err) {
3968 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3969 3969253a 2020-03-07 stsp goto done;
3971 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3972 07f5b47a 2019-06-02 stsp if (err) {
3973 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
3974 4e68cba3 2019-11-23 stsp goto done;
3977 dbb83fbd 2019-12-12 stsp free(ondisk_path);
3979 dbb83fbd 2019-12-12 stsp return err;
3980 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
3981 dbb83fbd 2019-12-12 stsp return NULL;
3982 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3985 d00136be 2019-03-26 stsp const struct got_error *
3986 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
3987 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
3988 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3989 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
3991 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3992 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
3993 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3994 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
3995 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
3997 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3999 d00136be 2019-03-26 stsp return err;
4001 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4003 d00136be 2019-03-26 stsp goto done;
4005 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4006 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4007 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4008 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4009 4e68cba3 2019-11-23 stsp saa.repo = repo;
4011 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4012 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4013 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4017 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4018 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4019 af12c6b9 2019-06-04 stsp err = sync_err;
4021 fb399478 2019-07-12 stsp free(fileindex_path);
4022 d00136be 2019-03-26 stsp if (fileindex)
4023 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4024 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4025 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4026 d00136be 2019-03-26 stsp err = unlockerr;
4027 6c7ab921 2019-03-18 stsp return err;
4030 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4031 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4032 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4033 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4034 f2a9dc41 2019-12-13 tracey void *progress_arg;
4035 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4036 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4037 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4038 766841c2 2020-08-13 stsp const char *status_codes;
4041 f2a9dc41 2019-12-13 tracey static const struct got_error *
4042 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4043 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4044 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4045 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4047 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4048 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4049 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4050 17ed4618 2019-06-02 stsp struct stat sb;
4051 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4053 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4054 17ed4618 2019-06-02 stsp if (ie == NULL)
4055 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4057 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4058 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4059 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4060 9acbc4fa 2019-08-03 stsp return NULL;
4061 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4064 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4065 f2a9dc41 2019-12-13 tracey relpath) == -1)
4066 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4068 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4071 f2a9dc41 2019-12-13 tracey goto done;
4073 766841c2 2020-08-13 stsp if (a->status_codes) {
4074 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4076 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4077 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4080 766841c2 2020-08-13 stsp if (i == ncodes) {
4081 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4082 766841c2 2020-08-13 stsp free(ondisk_path);
4083 766841c2 2020-08-13 stsp return NULL;
4085 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4086 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4087 766841c2 2020-08-13 stsp static char msg[64];
4088 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4089 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4090 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4091 766841c2 2020-08-13 stsp goto done;
4095 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4096 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4097 f2a9dc41 2019-12-13 tracey goto done;
4098 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4099 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4100 f2a9dc41 2019-12-13 tracey goto done;
4102 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4103 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4104 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4105 f2a9dc41 2019-12-13 tracey goto done;
4109 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4110 20a2ad1c 2020-10-20 stsp size_t root_len;
4112 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4113 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
4114 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4115 12463d8b 2019-12-13 stsp ondisk_path);
4116 12463d8b 2019-12-13 stsp goto done;
4118 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
4119 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4120 12463d8b 2019-12-13 stsp goto done;
4123 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4125 20a2ad1c 2020-10-20 stsp char *parent;
4126 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4128 2513f20a 2020-10-20 stsp goto done;
4129 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4130 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4131 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4132 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4133 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4134 20a2ad1c 2020-10-20 stsp ondisk_path);
4137 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4138 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4141 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4143 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4144 f2a9dc41 2019-12-13 tracey if (err)
4145 f2a9dc41 2019-12-13 tracey return err;
4146 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4147 f2a9dc41 2019-12-13 tracey return NULL;
4148 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4149 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4152 2ec1f75b 2019-03-26 stsp const struct got_error *
4153 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4154 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4155 766841c2 2020-08-13 stsp const char *status_codes,
4156 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4157 70e3e7f5 2019-12-13 tracey struct got_repository *repo, int keep_on_disk)
4159 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4160 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4161 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4162 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4163 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4165 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4167 2ec1f75b 2019-03-26 stsp return err;
4169 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4171 2ec1f75b 2019-03-26 stsp goto done;
4173 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4174 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4175 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4176 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4177 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4178 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4179 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4180 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4182 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4183 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4184 f2a9dc41 2019-12-13 tracey schedule_for_deletion, &sda, NULL, NULL, 0, 1);
4188 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4189 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4190 af12c6b9 2019-06-04 stsp err = sync_err;
4192 fb399478 2019-07-12 stsp free(fileindex_path);
4193 2ec1f75b 2019-03-26 stsp if (fileindex)
4194 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4195 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4196 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4197 2ec1f75b 2019-03-26 stsp err = unlockerr;
4198 33aa809d 2019-08-08 stsp return err;
4201 33aa809d 2019-08-08 stsp static const struct got_error *
4202 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4204 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4205 33aa809d 2019-08-08 stsp char *line = NULL;
4206 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4207 33aa809d 2019-08-08 stsp ssize_t linelen;
4209 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4210 33aa809d 2019-08-08 stsp if (linelen == -1) {
4211 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4212 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4213 33aa809d 2019-08-08 stsp goto done;
4215 33aa809d 2019-08-08 stsp return NULL;
4217 33aa809d 2019-08-08 stsp if (outfile) {
4218 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4219 33aa809d 2019-08-08 stsp if (n != linelen) {
4220 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4221 33aa809d 2019-08-08 stsp goto done;
4224 33aa809d 2019-08-08 stsp if (rejectfile) {
4225 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4226 33aa809d 2019-08-08 stsp if (n != linelen)
4227 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4230 33aa809d 2019-08-08 stsp free(line);
4231 2ec1f75b 2019-03-26 stsp return err;
4234 33aa809d 2019-08-08 stsp static const struct got_error *
4235 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4237 33aa809d 2019-08-08 stsp char *line = NULL;
4238 33aa809d 2019-08-08 stsp size_t linesize = 0;
4239 33aa809d 2019-08-08 stsp ssize_t linelen;
4241 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4242 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4243 500467ff 2019-09-25 hiltjo if (ferror(f))
4244 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4245 500467ff 2019-09-25 hiltjo return NULL;
4247 33aa809d 2019-08-08 stsp free(line);
4248 33aa809d 2019-08-08 stsp return NULL;
4251 33aa809d 2019-08-08 stsp static const struct got_error *
4252 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4253 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4254 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4256 33aa809d 2019-08-08 stsp const struct got_error *err;
4258 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4259 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4260 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4262 33aa809d 2019-08-08 stsp return err;
4263 33aa809d 2019-08-08 stsp (*line_cur1)++;
4265 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4266 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4267 33aa809d 2019-08-08 stsp if (rejectfile)
4268 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4270 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4272 33aa809d 2019-08-08 stsp return err;
4273 33aa809d 2019-08-08 stsp (*line_cur2)++;
4275 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4276 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4277 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4279 33aa809d 2019-08-08 stsp return err;
4280 33aa809d 2019-08-08 stsp (*line_cur2)++;
4282 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4283 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4284 33aa809d 2019-08-08 stsp if (rejectfile)
4285 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4287 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4289 33aa809d 2019-08-08 stsp return err;
4290 33aa809d 2019-08-08 stsp (*line_cur1)++;
4293 f1e81a05 2019-08-10 stsp return NULL;
4296 f1e81a05 2019-08-10 stsp static const struct got_error *
4297 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4298 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4300 f1e81a05 2019-08-10 stsp const struct got_error *err;
4302 f1e81a05 2019-08-10 stsp if (outfile) {
4303 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4304 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4305 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4307 f1e81a05 2019-08-10 stsp return err;
4308 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4311 f1e81a05 2019-08-10 stsp if (rejectfile) {
4312 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4313 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4314 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4316 f1e81a05 2019-08-10 stsp return err;
4317 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4321 33aa809d 2019-08-08 stsp return NULL;
4324 33aa809d 2019-08-08 stsp static const struct got_error *
4325 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4326 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4327 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4328 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4329 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4331 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4332 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4333 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4334 33aa809d 2019-08-08 stsp FILE *hunkfile;
4335 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4336 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4339 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4341 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4342 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4343 fe621944 2020-11-10 stsp start_old = cc.left.start;
4344 fe621944 2020-11-10 stsp end_old = cc.left.end;
4345 fe621944 2020-11-10 stsp start_new = cc.right.start;
4346 fe621944 2020-11-10 stsp end_new = cc.right.end;
4348 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4349 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4350 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4352 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4353 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4354 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4356 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4357 fe621944 2020-11-10 stsp if (diff_state == NULL)
4358 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4359 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4361 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4362 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4363 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4364 33aa809d 2019-08-08 stsp goto done;
4367 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4368 fe621944 2020-11-10 stsp diff_result, &cc);
4369 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4370 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4371 33aa809d 2019-08-08 stsp goto done;
4374 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4375 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4376 33aa809d 2019-08-08 stsp goto done;
4379 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4380 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4382 33aa809d 2019-08-08 stsp goto done;
4384 33aa809d 2019-08-08 stsp switch (*choice) {
4385 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4386 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4387 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4389 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4390 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4391 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4393 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4396 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4400 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4401 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4402 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4403 33aa809d 2019-08-08 stsp return err;
4406 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4407 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4408 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4409 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4410 1f1abb7e 2019-08-08 stsp void *progress_arg;
4411 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4412 33aa809d 2019-08-08 stsp void *patch_arg;
4413 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4416 e20a8b6f 2019-06-04 stsp static const struct got_error *
4417 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4418 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4419 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4420 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4421 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4423 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4424 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4425 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4426 1ebedb77 2019-10-19 stsp int fd2 = -1;
4427 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4428 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4429 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4430 fe621944 2020-11-10 stsp struct stat sb2;
4431 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4432 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4433 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4435 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4437 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4439 33aa809d 2019-08-08 stsp return err;
4441 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4442 12463d8b 2019-12-13 stsp fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4443 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4444 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4445 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4446 fa3cef63 2020-07-23 stsp goto done;
4448 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4449 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4450 fa3cef63 2020-07-23 stsp if (link_len == -1)
4451 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlinkat", path2);
4452 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4453 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4456 12463d8b 2019-12-13 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4457 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4458 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4459 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4460 fa3cef63 2020-07-23 stsp goto done;
4462 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4463 fa3cef63 2020-07-23 stsp sizeof(link_target));
4464 fa3cef63 2020-07-23 stsp if (link_len == -1)
4465 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4466 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4467 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4470 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4471 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4472 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4473 fa3cef63 2020-07-23 stsp goto done;
4476 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4477 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4478 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4479 fa3cef63 2020-07-23 stsp goto done;
4484 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4485 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4486 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4487 fa3cef63 2020-07-23 stsp goto done;
4489 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4490 fa3cef63 2020-07-23 stsp if (n != link_len) {
4491 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4492 fa3cef63 2020-07-23 stsp goto done;
4494 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4495 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4496 fa3cef63 2020-07-23 stsp goto done;
4498 fa3cef63 2020-07-23 stsp rewind(f2);
4501 33aa809d 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4503 33aa809d 2019-08-08 stsp goto done;
4505 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4507 33aa809d 2019-08-08 stsp goto done;
4509 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4511 33aa809d 2019-08-08 stsp goto done;
4513 64453f7e 2020-11-21 stsp err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4516 33aa809d 2019-08-08 stsp goto done;
4518 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4520 33aa809d 2019-08-08 stsp goto done;
4522 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4523 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4524 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4525 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4527 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4528 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4529 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4530 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4531 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4532 fe621944 2020-11-10 stsp nchanges++;
4534 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4535 33aa809d 2019-08-08 stsp int choice;
4536 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4537 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4538 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4539 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4540 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4541 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4543 33aa809d 2019-08-08 stsp goto done;
4544 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4545 33aa809d 2019-08-08 stsp have_content = 1;
4546 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4549 1ebedb77 2019-10-19 stsp if (have_content) {
4550 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4551 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4552 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4554 1ebedb77 2019-10-19 stsp goto done;
4556 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4557 3818e3c4 2020-11-01 naddy if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4558 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4559 fa3cef63 2020-07-23 stsp goto done;
4564 33aa809d 2019-08-08 stsp free(id_str);
4566 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4567 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4568 fe621944 2020-11-10 stsp if (err == NULL)
4569 fe621944 2020-11-10 stsp err = free_err;
4570 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4571 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4572 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4573 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4574 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4575 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4576 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4577 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4578 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4579 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4580 33aa809d 2019-08-08 stsp if (err || !have_content) {
4581 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4582 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4583 33aa809d 2019-08-08 stsp free(*path_outfile);
4584 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4586 33aa809d 2019-08-08 stsp free(path1);
4587 33aa809d 2019-08-08 stsp return err;
4590 33aa809d 2019-08-08 stsp static const struct got_error *
4591 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4592 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4593 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4594 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4596 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4597 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4598 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4599 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4600 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4601 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4602 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4603 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4604 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4605 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4607 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4608 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4609 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4610 d3bcc3d1 2019-08-08 stsp return NULL;
4612 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4613 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4614 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4616 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4617 65084dad 2019-08-08 stsp if (ie == NULL)
4618 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4620 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4621 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4622 a129376b 2019-03-28 stsp if (err) {
4623 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4624 a129376b 2019-03-28 stsp goto done;
4625 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4626 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4627 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4628 e20a8b6f 2019-06-04 stsp goto done;
4631 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4632 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4633 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4634 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4635 a129376b 2019-03-28 stsp goto done;
4638 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4639 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4640 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4641 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4642 a129376b 2019-03-28 stsp goto done;
4645 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4646 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4647 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4648 a129376b 2019-03-28 stsp goto done;
4653 1f1abb7e 2019-08-08 stsp err = got_object_id_by_path(&tree_id, a->repo,
4654 1f1abb7e 2019-08-08 stsp a->worktree->base_commit_id, tree_path);
4655 a9fa2909 2019-07-27 stsp if (err) {
4656 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4657 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4658 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4659 a9fa2909 2019-07-27 stsp goto done;
4661 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4663 a9fa2909 2019-07-27 stsp goto done;
4665 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4667 a9fa2909 2019-07-27 stsp goto done;
4669 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4670 1233e6b6 2020-10-19 stsp free(te_name);
4671 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4672 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4673 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4674 a9fa2909 2019-07-27 stsp goto done;
4678 a129376b 2019-03-28 stsp switch (status) {
4679 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4680 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4681 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4682 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4683 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4685 33aa809d 2019-08-08 stsp goto done;
4686 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4689 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4690 1f1abb7e 2019-08-08 stsp ie->path);
4692 1ee397ad 2019-07-12 stsp goto done;
4693 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4695 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4696 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4697 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4698 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4699 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4701 33aa809d 2019-08-08 stsp goto done;
4702 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4705 33aa809d 2019-08-08 stsp /* fall through */
4706 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4707 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4708 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4709 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4710 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4711 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4712 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4713 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4714 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4716 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4717 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4718 1f1abb7e 2019-08-08 stsp err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4720 65084dad 2019-08-08 stsp goto done;
4722 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4723 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4724 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4725 a129376b 2019-03-28 stsp goto done;
4728 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4729 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4730 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4731 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4732 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4733 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4734 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4736 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4737 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4738 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4739 369fd7e5 2020-07-23 stsp path_content);
4742 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4743 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4744 369fd7e5 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4745 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4747 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4748 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4749 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4750 369fd7e5 2020-07-23 stsp goto done;
4754 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4755 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4756 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4757 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4758 c90c8ce3 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4759 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4761 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4763 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4764 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4765 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4766 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4769 a129376b 2019-03-28 stsp goto done;
4770 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4771 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4772 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4773 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
4774 437adc9d 2020-12-10 yzhong blob->id.sha1,
4775 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4777 2e1fa222 2020-07-23 stsp goto done;
4779 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4780 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4781 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4790 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4791 33aa809d 2019-08-08 stsp free(path_content);
4792 e20a8b6f 2019-06-04 stsp free(parent_path);
4793 a129376b 2019-03-28 stsp free(tree_path);
4795 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4797 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4798 a129376b 2019-03-28 stsp free(tree_id);
4799 e20a8b6f 2019-06-04 stsp return err;
4802 e20a8b6f 2019-06-04 stsp const struct got_error *
4803 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4804 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4805 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4806 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4807 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4809 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4810 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4811 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4812 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4813 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4814 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4816 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4818 e20a8b6f 2019-06-04 stsp return err;
4820 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4822 e20a8b6f 2019-06-04 stsp goto done;
4824 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4825 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4826 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4827 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4828 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4829 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4830 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4831 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4832 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4833 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
4837 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4838 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4839 e20a8b6f 2019-06-04 stsp err = sync_err;
4841 fb399478 2019-07-12 stsp free(fileindex_path);
4842 a129376b 2019-03-28 stsp if (fileindex)
4843 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4844 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4845 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4846 a129376b 2019-03-28 stsp err = unlockerr;
4847 c4296144 2019-05-09 stsp return err;
4850 cf066bf8 2019-05-09 stsp static void
4851 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4853 24519714 2019-05-09 stsp free(ct->path);
4854 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4855 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4856 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4857 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4858 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4859 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4863 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4864 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4865 24519714 2019-05-09 stsp struct got_repository *repo;
4866 24519714 2019-05-09 stsp struct got_worktree *worktree;
4867 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4868 5f8a88c6 2019-08-03 stsp int have_staged_files;
4869 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4872 c4296144 2019-05-09 stsp static const struct got_error *
4873 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
4874 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
4875 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4876 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4878 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
4879 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4880 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4881 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4882 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4883 768aea60 2019-05-09 stsp struct stat sb;
4885 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
4886 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4887 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4888 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4889 5f8a88c6 2019-08-03 stsp return NULL;
4891 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4892 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4894 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4895 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4896 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4897 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4898 5f8a88c6 2019-08-03 stsp return NULL;
4901 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4902 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4903 036813ee 2019-05-09 stsp goto done;
4905 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4906 036813ee 2019-05-09 stsp parent_path = strdup("");
4907 69960a46 2019-05-09 stsp if (parent_path == NULL)
4908 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4910 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4912 69960a46 2019-05-09 stsp return err;
4915 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4916 cf066bf8 2019-05-09 stsp if (ct == NULL) {
4917 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4918 c4296144 2019-05-09 stsp goto done;
4921 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4922 768aea60 2019-05-09 stsp relpath) == -1) {
4923 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4924 768aea60 2019-05-09 stsp goto done;
4927 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
4928 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
4929 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
4930 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4931 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
4932 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
4933 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
4934 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
4936 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
4937 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
4940 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4941 0aeb8099 2020-07-23 stsp goto done;
4943 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
4944 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
4945 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
4946 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4947 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
4948 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
4949 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
4950 12463d8b 2019-12-13 stsp ct->ondisk_path);
4951 12463d8b 2019-12-13 stsp goto done;
4953 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
4954 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
4955 768aea60 2019-05-09 stsp goto done;
4957 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
4960 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4961 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4962 44d03001 2019-05-09 stsp relpath) == -1) {
4963 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4964 44d03001 2019-05-09 stsp goto done;
4967 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4968 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4969 35213c7c 2020-07-23 stsp int is_bad_symlink;
4970 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
4971 35213c7c 2020-07-23 stsp ssize_t target_len;
4972 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
4973 35213c7c 2020-07-23 stsp sizeof(target_path));
4974 35213c7c 2020-07-23 stsp if (target_len == -1) {
4975 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
4976 35213c7c 2020-07-23 stsp ct->ondisk_path);
4977 35213c7c 2020-07-23 stsp goto done;
4979 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
4980 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
4982 35213c7c 2020-07-23 stsp goto done;
4983 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
4984 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
4985 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
4986 35213c7c 2020-07-23 stsp goto done;
4991 cf066bf8 2019-05-09 stsp ct->status = status;
4992 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
4993 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
4994 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
4995 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
4996 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
4997 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
4998 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4999 b416585c 2019-05-13 stsp goto done;
5001 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5002 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5003 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5004 f0b75401 2019-08-03 stsp goto done;
5007 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5008 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5009 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5010 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5011 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5012 036813ee 2019-05-09 stsp goto done;
5015 24519714 2019-05-09 stsp ct->path = strdup(path);
5016 24519714 2019-05-09 stsp if (ct->path == NULL) {
5017 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5018 24519714 2019-05-09 stsp goto done;
5020 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5022 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5023 ed175427 2019-05-09 stsp free_commitable(ct);
5024 24519714 2019-05-09 stsp free(parent_path);
5025 036813ee 2019-05-09 stsp free(path);
5026 a129376b 2019-03-28 stsp return err;
5029 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5030 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5031 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5032 036813ee 2019-05-09 stsp struct got_repository *);
5034 ed175427 2019-05-09 stsp static const struct got_error *
5035 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5036 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5037 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5038 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5039 afa376bf 2019-05-09 stsp struct got_repository *repo)
5041 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5042 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5043 036813ee 2019-05-09 stsp char *subpath;
5045 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5046 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5047 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5049 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5051 ed175427 2019-05-09 stsp return err;
5053 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5054 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5055 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5056 036813ee 2019-05-09 stsp free(subpath);
5057 036813ee 2019-05-09 stsp return err;
5060 036813ee 2019-05-09 stsp static const struct got_error *
5061 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5063 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5064 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5066 036813ee 2019-05-09 stsp *match = 0;
5068 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5069 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5070 0f63689d 2019-05-10 stsp return NULL;
5073 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5075 0f63689d 2019-05-10 stsp return err;
5076 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5077 036813ee 2019-05-09 stsp free(ct_parent_path);
5078 036813ee 2019-05-09 stsp return err;
5081 768aea60 2019-05-09 stsp static mode_t
5082 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5084 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5085 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5087 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5090 036813ee 2019-05-09 stsp static const struct got_error *
5091 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5092 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5094 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5096 036813ee 2019-05-09 stsp *new_te = NULL;
5098 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5100 036813ee 2019-05-09 stsp goto done;
5102 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5104 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5105 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5106 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5108 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5110 036813ee 2019-05-09 stsp if (err && *new_te) {
5111 56e0773d 2019-11-28 stsp free(*new_te);
5112 036813ee 2019-05-09 stsp *new_te = NULL;
5114 036813ee 2019-05-09 stsp return err;
5117 036813ee 2019-05-09 stsp static const struct got_error *
5118 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5119 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5121 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5122 102b254e 2020-10-19 stsp char *ct_name = NULL;
5124 036813ee 2019-05-09 stsp *new_te = NULL;
5126 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5127 036813ee 2019-05-09 stsp if (*new_te == NULL)
5128 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5130 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5132 036813ee 2019-05-09 stsp goto done;
5133 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5134 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5135 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5136 036813ee 2019-05-09 stsp goto done;
5139 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5141 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5142 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5143 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5145 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5147 102b254e 2020-10-19 stsp free(ct_name);
5148 036813ee 2019-05-09 stsp if (err && *new_te) {
5149 56e0773d 2019-11-28 stsp free(*new_te);
5150 036813ee 2019-05-09 stsp *new_te = NULL;
5152 036813ee 2019-05-09 stsp return err;
5155 036813ee 2019-05-09 stsp static const struct got_error *
5156 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5157 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5159 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5160 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5162 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5164 036813ee 2019-05-09 stsp return err;
5165 036813ee 2019-05-09 stsp if (new_pe == NULL)
5166 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5167 036813ee 2019-05-09 stsp return NULL;
5170 afa376bf 2019-05-09 stsp static const struct got_error *
5171 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5172 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5174 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5175 5f8a88c6 2019-08-03 stsp unsigned char status;
5177 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5178 afa376bf 2019-05-09 stsp ct_path++;
5180 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5181 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5183 5f8a88c6 2019-08-03 stsp status = ct->status;
5185 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5186 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5189 036813ee 2019-05-09 stsp static const struct got_error *
5190 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5191 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5193 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5194 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5195 44d03001 2019-05-09 stsp char *te_path;
5197 44d03001 2019-05-09 stsp *modified = 0;
5199 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5200 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5201 44d03001 2019-05-09 stsp te->name) == -1)
5202 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5204 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5205 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5206 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5207 44d03001 2019-05-09 stsp strlen(te_path));
5208 62d463ca 2020-10-20 naddy if (*modified)
5212 44d03001 2019-05-09 stsp free(te_path);
5213 44d03001 2019-05-09 stsp return err;
5216 44d03001 2019-05-09 stsp static const struct got_error *
5217 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5218 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5219 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5221 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5222 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5224 036813ee 2019-05-09 stsp *ctp = NULL;
5226 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5227 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5228 036813ee 2019-05-09 stsp char *ct_name = NULL;
5229 036813ee 2019-05-09 stsp int path_matches;
5231 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5232 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5233 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5234 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
5237 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5238 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5242 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5245 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5247 036813ee 2019-05-09 stsp return err;
5248 036813ee 2019-05-09 stsp if (!path_matches)
5251 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5253 d34b633e 2020-10-19 stsp return err;
5255 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5256 d34b633e 2020-10-19 stsp free(ct_name);
5259 d34b633e 2020-10-19 stsp free(ct_name);
5261 036813ee 2019-05-09 stsp *ctp = ct;
5265 2b496619 2019-07-10 stsp return err;
5268 2b496619 2019-07-10 stsp static const struct got_error *
5269 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5270 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5271 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5272 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5273 2b496619 2019-07-10 stsp struct got_repository *repo)
5275 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5276 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5277 2b496619 2019-07-10 stsp char *subtree_path;
5278 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5279 ba580f68 2020-03-22 stsp int nentries;
5281 2b496619 2019-07-10 stsp *new_tep = NULL;
5283 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5284 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5285 2b496619 2019-07-10 stsp child_path) == -1)
5286 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5288 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5289 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5290 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5291 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5293 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5294 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5295 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5296 2b496619 2019-07-10 stsp goto done;
5298 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5299 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5300 2b496619 2019-07-10 stsp if (err) {
5301 56e0773d 2019-11-28 stsp free(new_te);
5302 2b496619 2019-07-10 stsp goto done;
5304 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5307 2b496619 2019-07-10 stsp free(subtree_path);
5308 2b496619 2019-07-10 stsp if (err == NULL)
5309 2b496619 2019-07-10 stsp *new_tep = new_te;
5310 036813ee 2019-05-09 stsp return err;
5313 036813ee 2019-05-09 stsp static const struct got_error *
5314 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5315 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5316 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5317 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5318 036813ee 2019-05-09 stsp struct got_repository *repo)
5320 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5321 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5322 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5323 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5325 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5326 ba580f68 2020-03-22 stsp *nentries = 0;
5328 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5329 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5330 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5331 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5333 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5334 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5335 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5338 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5339 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5342 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5343 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5345 036813ee 2019-05-09 stsp goto done;
5347 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5348 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5349 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5351 036813ee 2019-05-09 stsp goto done;
5352 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5354 afa376bf 2019-05-09 stsp goto done;
5355 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5356 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5358 2f51b5b3 2019-05-09 stsp goto done;
5359 ba580f68 2020-03-22 stsp (*nentries)++;
5361 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5362 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5363 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5364 2b496619 2019-07-10 stsp == NULL) {
5365 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5366 2b496619 2019-07-10 stsp child_path, path_base_tree,
5367 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5370 2b496619 2019-07-10 stsp goto done;
5371 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5373 2b496619 2019-07-10 stsp goto done;
5374 ba580f68 2020-03-22 stsp (*nentries)++;
5379 2f51b5b3 2019-05-09 stsp if (base_tree) {
5380 56e0773d 2019-11-28 stsp int i, nbase_entries;
5381 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5382 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5383 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5384 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5386 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5387 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5388 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5389 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5391 63c5ca5d 2019-08-24 stsp goto done;
5392 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5394 63c5ca5d 2019-08-24 stsp goto done;
5395 ba580f68 2020-03-22 stsp (*nentries)++;
5399 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5400 44d03001 2019-05-09 stsp int modified;
5401 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5403 036813ee 2019-05-09 stsp goto done;
5404 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5405 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5407 2f51b5b3 2019-05-09 stsp goto done;
5408 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5409 44d03001 2019-05-09 stsp if (modified) {
5410 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5411 ba580f68 2020-03-22 stsp int nsubentries;
5412 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5413 ba580f68 2020-03-22 stsp &nsubentries, te,
5414 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5415 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5417 44d03001 2019-05-09 stsp goto done;
5418 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5419 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5420 ba580f68 2020-03-22 stsp free(new_id);
5423 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5424 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5425 56e0773d 2019-11-28 stsp free(new_id);
5427 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5429 036813ee 2019-05-09 stsp goto done;
5430 ba580f68 2020-03-22 stsp (*nentries)++;
5434 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5435 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5437 f66c734c 2019-09-22 stsp goto done;
5439 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5440 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5441 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5442 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5443 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5444 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5446 2f51b5b3 2019-05-09 stsp goto done;
5447 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5449 2f51b5b3 2019-05-09 stsp goto done;
5450 ba580f68 2020-03-22 stsp (*nentries)++;
5452 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5453 b416585c 2019-05-13 stsp status_arg);
5455 afa376bf 2019-05-09 stsp goto done;
5457 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5458 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5460 2f51b5b3 2019-05-09 stsp goto done;
5461 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5463 2f51b5b3 2019-05-09 stsp goto done;
5464 ba580f68 2020-03-22 stsp (*nentries)++;
5469 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5470 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5472 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
5473 2e1fa222 2020-07-23 stsp return err;
5476 2e1fa222 2020-07-23 stsp static const struct got_error *
5477 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5478 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5479 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5480 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5482 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5483 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5484 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5486 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5487 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5488 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5490 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5492 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5493 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5494 437adc9d 2020-12-10 yzhong if (err)
5495 437adc9d 2020-12-10 yzhong goto done;
5498 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5499 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5500 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5501 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5502 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5503 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5504 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5505 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5507 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5508 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5509 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
5510 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5511 72fd46fa 2019-09-06 stsp !have_staged_files);
5513 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5514 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5515 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
5516 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5517 72fd46fa 2019-09-06 stsp !have_staged_files);
5519 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5521 437adc9d 2020-12-10 yzhong goto done;
5522 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5523 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
5524 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
5525 3969253a 2020-03-07 stsp if (err) {
5526 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5527 437adc9d 2020-12-10 yzhong goto done;
5529 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5530 3969253a 2020-03-07 stsp if (err) {
5531 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5532 437adc9d 2020-12-10 yzhong goto done;
5535 437adc9d 2020-12-10 yzhong free(relpath);
5536 437adc9d 2020-12-10 yzhong relpath = NULL;
5539 437adc9d 2020-12-10 yzhong free(relpath);
5540 d56d26ce 2019-05-10 stsp return err;
5544 d56d26ce 2019-05-10 stsp static const struct got_error *
5545 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5546 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5547 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5548 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5549 735ef5ac 2019-08-03 stsp int ood_errcode)
5551 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5552 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5554 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5555 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5556 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5557 1a36436d 2019-06-10 stsp return NULL;
5559 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5560 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5562 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5563 735ef5ac 2019-08-03 stsp in_repo_path);
5564 9bead371 2019-07-28 stsp if (err) {
5565 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5566 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5567 1a36436d 2019-06-10 stsp goto done;
5568 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5569 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5571 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5572 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5573 735ef5ac 2019-08-03 stsp in_repo_path);
5574 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5575 1a36436d 2019-06-10 stsp goto done;
5576 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5580 1a36436d 2019-06-10 stsp return err;
5583 c4296144 2019-05-09 stsp const struct got_error *
5584 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5585 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5586 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id, struct got_worktree *worktree,
5587 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
5588 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5589 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5590 afa376bf 2019-05-09 stsp struct got_repository *repo)
5592 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5593 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5594 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5595 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5596 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5597 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5598 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5599 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5600 ba580f68 2020-03-22 stsp int nentries;
5601 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5602 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5603 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5605 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5607 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
5609 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5611 588edf97 2019-05-10 stsp goto done;
5613 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5615 588edf97 2019-05-10 stsp goto done;
5617 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5618 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5623 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5624 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5628 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5629 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5630 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5631 cf066bf8 2019-05-09 stsp char *ondisk_path;
5633 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5634 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5635 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5638 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5639 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5640 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5643 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5644 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5645 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5646 cf066bf8 2019-05-09 stsp goto done;
5648 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5649 2e1fa222 2020-07-23 stsp free(ondisk_path);
5651 cf066bf8 2019-05-09 stsp goto done;
5654 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5655 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5656 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5658 036813ee 2019-05-09 stsp goto done;
5660 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5662 de18fc63 2019-05-09 stsp goto done;
5663 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5664 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5665 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5666 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
5667 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5668 33ad4cbe 2019-05-12 jcs free(logmsg);
5670 9d40349a 2019-05-09 stsp goto done;
5672 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5673 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5674 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5675 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5676 09f5bd90 2019-05-09 stsp goto done;
5678 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
5679 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5681 09f5bd90 2019-05-09 stsp goto done;
5682 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5684 ebf99748 2019-05-09 stsp goto done;
5685 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5686 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5687 09f5bd90 2019-05-09 stsp goto done;
5689 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
5690 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
5692 f2c16586 2019-05-09 stsp goto done;
5693 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
5695 f2c16586 2019-05-09 stsp goto done;
5697 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5699 09f5bd90 2019-05-09 stsp goto done;
5701 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
5703 ebf99748 2019-05-09 stsp goto done;
5705 588edf97 2019-05-10 stsp if (head_tree)
5706 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
5707 588edf97 2019-05-10 stsp if (head_commit)
5708 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
5709 09f5bd90 2019-05-09 stsp free(head_commit_id2);
5710 2f17228e 2019-05-12 stsp if (head_ref2) {
5711 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
5712 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
5713 2f17228e 2019-05-12 stsp err = unlockerr;
5714 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
5716 39cd0ff6 2019-07-12 stsp return err;
5719 5c1e53bc 2019-07-28 stsp static const struct got_error *
5720 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
5721 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
5723 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
5724 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
5726 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
5727 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
5728 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
5730 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
5731 5c1e53bc 2019-07-28 stsp ct_path++;
5733 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
5734 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
5738 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
5739 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
5741 5c1e53bc 2019-07-28 stsp return NULL;
5744 f0b75401 2019-08-03 stsp static const struct got_error *
5745 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
5747 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
5749 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5750 f0b75401 2019-08-03 stsp *have_staged_files = 1;
5751 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
5754 f0b75401 2019-08-03 stsp return NULL;
5757 5f8a88c6 2019-08-03 stsp static const struct got_error *
5758 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
5759 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
5761 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
5762 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
5764 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5765 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
5767 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5768 5f8a88c6 2019-08-03 stsp if (ie == NULL)
5769 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5770 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5771 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
5772 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
5775 5f8a88c6 2019-08-03 stsp return NULL;
5778 39cd0ff6 2019-07-12 stsp const struct got_error *
5779 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
5780 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
5781 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
5782 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5783 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
5784 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
5786 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5787 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
5788 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
5789 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5790 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5791 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
5792 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
5793 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5794 f0b75401 2019-08-03 stsp int have_staged_files = 0;
5796 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
5798 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5800 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
5802 39cd0ff6 2019-07-12 stsp goto done;
5804 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5806 39cd0ff6 2019-07-12 stsp goto done;
5808 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5810 39cd0ff6 2019-07-12 stsp goto done;
5812 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5814 39cd0ff6 2019-07-12 stsp goto done;
5816 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5817 5f8a88c6 2019-08-03 stsp &have_staged_files);
5818 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
5819 5f8a88c6 2019-08-03 stsp goto done;
5820 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
5821 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
5823 5f8a88c6 2019-08-03 stsp goto done;
5826 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5827 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
5828 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
5829 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
5830 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
5831 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5832 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5833 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5834 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5836 5c1e53bc 2019-07-28 stsp goto done;
5839 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5840 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5841 39cd0ff6 2019-07-12 stsp goto done;
5844 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5845 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
5847 5c1e53bc 2019-07-28 stsp goto done;
5850 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5851 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5852 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5854 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5855 f0b75401 2019-08-03 stsp ct_path++;
5856 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5857 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5858 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5860 b50cabdf 2019-07-12 stsp goto done;
5864 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5865 5c1e53bc 2019-07-28 stsp head_commit_id, worktree, author, committer,
5866 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5868 39cd0ff6 2019-07-12 stsp goto done;
5870 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
5871 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
5872 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5873 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5874 39cd0ff6 2019-07-12 stsp err = sync_err;
5876 39cd0ff6 2019-07-12 stsp if (fileindex)
5877 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5878 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5879 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5880 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5881 39cd0ff6 2019-07-12 stsp err = unlockerr;
5882 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5883 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5884 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5886 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5887 c4296144 2019-05-09 stsp return err;
5890 8656d6c4 2019-05-20 stsp const char *
5891 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5893 8656d6c4 2019-05-20 stsp return ct->path;
5896 8656d6c4 2019-05-20 stsp unsigned int
5897 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
5899 8656d6c4 2019-05-20 stsp return ct->status;
5902 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
5903 818c7501 2019-07-11 stsp struct got_worktree *worktree;
5904 818c7501 2019-07-11 stsp struct got_repository *repo;
5907 818c7501 2019-07-11 stsp static const struct got_error *
5908 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5910 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5911 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
5912 818c7501 2019-07-11 stsp unsigned char status;
5913 818c7501 2019-07-11 stsp struct stat sb;
5914 818c7501 2019-07-11 stsp char *ondisk_path;
5916 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
5917 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5918 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
5919 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
5921 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5923 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
5925 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
5926 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5927 818c7501 2019-07-11 stsp free(ondisk_path);
5929 818c7501 2019-07-11 stsp return err;
5931 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
5932 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
5933 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5934 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5936 818c7501 2019-07-11 stsp return NULL;
5939 818c7501 2019-07-11 stsp const struct got_error *
5940 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5941 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5942 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
5943 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5945 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5946 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5947 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
5948 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
5949 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
5950 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5951 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
5953 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5954 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5955 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5957 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
5959 818c7501 2019-07-11 stsp return err;
5961 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5963 818c7501 2019-07-11 stsp goto done;
5965 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
5966 818c7501 2019-07-11 stsp ok_arg.repo = repo;
5967 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5970 818c7501 2019-07-11 stsp goto done;
5972 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5974 818c7501 2019-07-11 stsp goto done;
5976 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5978 818c7501 2019-07-11 stsp goto done;
5980 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5982 818c7501 2019-07-11 stsp goto done;
5984 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5987 e51d7b55 2020-01-04 stsp goto done;
5989 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5991 818c7501 2019-07-11 stsp goto done;
5992 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5993 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5994 e51d7b55 2020-01-04 stsp goto done;
5997 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
5998 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6000 818c7501 2019-07-11 stsp goto done;
6001 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6003 818c7501 2019-07-11 stsp goto done;
6005 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6007 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6009 818c7501 2019-07-11 stsp goto done;
6011 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6013 818c7501 2019-07-11 stsp goto done;
6015 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6016 818c7501 2019-07-11 stsp worktree->base_commit_id);
6018 818c7501 2019-07-11 stsp goto done;
6019 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6021 818c7501 2019-07-11 stsp goto done;
6023 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6025 818c7501 2019-07-11 stsp goto done;
6027 818c7501 2019-07-11 stsp free(fileindex_path);
6028 818c7501 2019-07-11 stsp free(tmp_branch_name);
6029 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6030 818c7501 2019-07-11 stsp free(branch_ref_name);
6031 818c7501 2019-07-11 stsp if (branch_ref)
6032 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6033 818c7501 2019-07-11 stsp if (wt_branch)
6034 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6035 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6036 818c7501 2019-07-11 stsp if (err) {
6037 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6038 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6039 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6041 818c7501 2019-07-11 stsp if (*tmp_branch) {
6042 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6043 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6045 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6046 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6047 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6049 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6051 818c7501 2019-07-11 stsp return err;
6054 818c7501 2019-07-11 stsp const struct got_error *
6055 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6056 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6057 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6058 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6060 818c7501 2019-07-11 stsp const struct got_error *err;
6061 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6062 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6063 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6064 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6065 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6067 818c7501 2019-07-11 stsp *commit_id = NULL;
6068 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6069 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6070 3e3a69f1 2019-07-25 stsp *branch = NULL;
6071 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6073 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6075 3e3a69f1 2019-07-25 stsp return err;
6077 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6079 f032f1f7 2019-08-04 stsp goto done;
6081 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6082 f032f1f7 2019-08-04 stsp &have_staged_files);
6083 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6084 f032f1f7 2019-08-04 stsp goto done;
6085 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6086 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6087 3e3a69f1 2019-07-25 stsp goto done;
6090 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6092 f032f1f7 2019-08-04 stsp goto done;
6094 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6096 818c7501 2019-07-11 stsp goto done;
6098 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6100 818c7501 2019-07-11 stsp goto done;
6102 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6104 818c7501 2019-07-11 stsp goto done;
6106 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6108 818c7501 2019-07-11 stsp goto done;
6110 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6111 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6113 818c7501 2019-07-11 stsp goto done;
6115 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6117 818c7501 2019-07-11 stsp goto done;
6119 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6121 818c7501 2019-07-11 stsp goto done;
6123 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6124 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6126 818c7501 2019-07-11 stsp goto done;
6128 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6130 818c7501 2019-07-11 stsp goto done;
6132 818c7501 2019-07-11 stsp free(commit_ref_name);
6133 818c7501 2019-07-11 stsp free(branch_ref_name);
6134 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6135 818c7501 2019-07-11 stsp if (commit_ref)
6136 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6137 818c7501 2019-07-11 stsp if (branch_ref)
6138 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6139 818c7501 2019-07-11 stsp if (err) {
6140 818c7501 2019-07-11 stsp free(*commit_id);
6141 818c7501 2019-07-11 stsp *commit_id = NULL;
6142 818c7501 2019-07-11 stsp if (*tmp_branch) {
6143 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6144 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6146 818c7501 2019-07-11 stsp if (*new_base_branch) {
6147 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6148 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6150 818c7501 2019-07-11 stsp if (*branch) {
6151 818c7501 2019-07-11 stsp got_ref_close(*branch);
6152 818c7501 2019-07-11 stsp *branch = NULL;
6154 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6155 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6156 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6158 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6160 818c7501 2019-07-11 stsp return err;
6163 818c7501 2019-07-11 stsp const struct got_error *
6164 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6166 818c7501 2019-07-11 stsp const struct got_error *err;
6167 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6169 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6171 818c7501 2019-07-11 stsp return err;
6173 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6174 818c7501 2019-07-11 stsp free(tmp_branch_name);
6175 818c7501 2019-07-11 stsp return NULL;
6178 818c7501 2019-07-11 stsp static const struct got_error *
6179 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6180 818c7501 2019-07-11 stsp char **logmsg, void *arg)
6182 0ebf8283 2019-07-24 stsp *logmsg = arg;
6183 818c7501 2019-07-11 stsp return NULL;
6186 818c7501 2019-07-11 stsp static const struct got_error *
6187 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6188 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6189 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6190 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6192 818c7501 2019-07-11 stsp return NULL;
6195 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6196 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6197 01757395 2019-07-12 stsp void *progress_arg;
6198 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6201 01757395 2019-07-12 stsp static const struct got_error *
6202 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6204 01757395 2019-07-12 stsp const struct got_error *err;
6205 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6207 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6209 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6211 01757395 2019-07-12 stsp return err;
6213 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6214 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6215 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6216 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6217 01757395 2019-07-12 stsp return NULL;
6219 01757395 2019-07-12 stsp p = strdup(path);
6220 01757395 2019-07-12 stsp if (p == NULL)
6221 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6223 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6224 01757395 2019-07-12 stsp if (err || new == NULL)
6226 01757395 2019-07-12 stsp return err;
6230 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6232 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6234 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
6235 01757395 2019-07-12 stsp free((char *)pe->path);
6237 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
6240 0ebf8283 2019-07-24 stsp static const struct got_error *
6241 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6242 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6244 818c7501 2019-07-11 stsp const struct got_error *err;
6245 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6247 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6248 818c7501 2019-07-11 stsp if (err) {
6249 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6250 818c7501 2019-07-11 stsp goto done;
6251 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6253 818c7501 2019-07-11 stsp goto done;
6254 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6256 818c7501 2019-07-11 stsp goto done;
6257 de05890f 2020-03-05 stsp } else if (is_rebase) {
6258 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6261 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6263 818c7501 2019-07-11 stsp goto done;
6264 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6265 818c7501 2019-07-11 stsp free(stored_id);
6266 818c7501 2019-07-11 stsp if (cmp != 0) {
6267 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6268 818c7501 2019-07-11 stsp goto done;
6272 0ebf8283 2019-07-24 stsp if (commit_ref)
6273 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6274 0ebf8283 2019-07-24 stsp return err;
6277 0ebf8283 2019-07-24 stsp static const struct got_error *
6278 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6279 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6280 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6281 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6282 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6283 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6285 0ebf8283 2019-07-24 stsp const struct got_error *err;
6286 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6287 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6288 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6290 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6292 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6294 0ebf8283 2019-07-24 stsp return err;
6296 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6297 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6298 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6299 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6300 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6301 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6302 818c7501 2019-07-11 stsp if (commit_ref)
6303 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6304 818c7501 2019-07-11 stsp return err;
6307 818c7501 2019-07-11 stsp const struct got_error *
6308 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6309 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6310 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6311 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6312 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6313 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6315 0ebf8283 2019-07-24 stsp const struct got_error *err;
6316 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6318 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6320 0ebf8283 2019-07-24 stsp return err;
6322 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6324 0ebf8283 2019-07-24 stsp goto done;
6326 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6327 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6328 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6330 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6331 0ebf8283 2019-07-24 stsp return err;
6334 0ebf8283 2019-07-24 stsp const struct got_error *
6335 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6336 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6337 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6338 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6339 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6340 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6342 0ebf8283 2019-07-24 stsp const struct got_error *err;
6343 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6345 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6347 0ebf8283 2019-07-24 stsp return err;
6349 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6351 0ebf8283 2019-07-24 stsp goto done;
6353 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6354 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6355 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6357 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6358 0ebf8283 2019-07-24 stsp return err;
6361 0ebf8283 2019-07-24 stsp static const struct got_error *
6362 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6363 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6364 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6365 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6366 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
6368 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6369 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6370 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6371 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6372 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6373 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6374 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6376 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6377 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6379 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6381 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6383 3e3a69f1 2019-07-25 stsp return err;
6385 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6386 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6387 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6388 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6390 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6391 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6393 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
6394 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6395 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
6396 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
6397 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
6398 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
6400 01757395 2019-07-12 stsp if (merged_paths) {
6401 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6402 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6403 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6404 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6407 01757395 2019-07-12 stsp goto done;
6410 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6411 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6413 01757395 2019-07-12 stsp goto done;
6416 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6417 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6418 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6420 a0e95631 2019-07-12 stsp goto done;
6421 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6422 a0e95631 2019-07-12 stsp goto done;
6425 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6427 edd02c5e 2019-07-11 stsp goto done;
6429 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6431 818c7501 2019-07-11 stsp goto done;
6433 5943eee2 2019-08-13 stsp if (new_logmsg) {
6434 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6435 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6436 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6437 5943eee2 2019-08-13 stsp goto done;
6440 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6442 5943eee2 2019-08-13 stsp goto done;
6445 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6446 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6447 5c1e53bc 2019-07-28 stsp worktree, got_object_commit_get_author(orig_commit),
6448 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
6449 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6451 a0e95631 2019-07-12 stsp goto done;
6453 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6455 a0e95631 2019-07-12 stsp goto done;
6457 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6459 a0e95631 2019-07-12 stsp goto done;
6461 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6462 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6463 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6464 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6465 a0e95631 2019-07-12 stsp err = sync_err;
6467 a0e95631 2019-07-12 stsp free(fileindex_path);
6468 a0e95631 2019-07-12 stsp free(head_commit_id);
6469 a0e95631 2019-07-12 stsp if (head_ref)
6470 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6471 818c7501 2019-07-11 stsp if (err) {
6472 818c7501 2019-07-11 stsp free(*new_commit_id);
6473 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6475 818c7501 2019-07-11 stsp return err;
6478 818c7501 2019-07-11 stsp const struct got_error *
6479 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6480 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6481 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6482 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6483 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6485 0ebf8283 2019-07-24 stsp const struct got_error *err;
6486 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6487 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6488 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6490 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6492 0ebf8283 2019-07-24 stsp return err;
6494 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6496 0ebf8283 2019-07-24 stsp goto done;
6497 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6499 0ebf8283 2019-07-24 stsp goto done;
6500 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6501 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6502 0ebf8283 2019-07-24 stsp goto done;
6505 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6506 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6508 0ebf8283 2019-07-24 stsp if (commit_ref)
6509 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6510 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6511 0ebf8283 2019-07-24 stsp free(commit_id);
6512 0ebf8283 2019-07-24 stsp return err;
6515 0ebf8283 2019-07-24 stsp const struct got_error *
6516 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6517 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6518 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6519 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6520 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6521 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6523 0ebf8283 2019-07-24 stsp const struct got_error *err;
6524 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6525 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6527 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6529 0ebf8283 2019-07-24 stsp return err;
6531 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6533 0ebf8283 2019-07-24 stsp goto done;
6535 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6536 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6538 0ebf8283 2019-07-24 stsp if (commit_ref)
6539 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6540 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6541 0ebf8283 2019-07-24 stsp return err;
6544 0ebf8283 2019-07-24 stsp const struct got_error *
6545 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6546 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6548 3e3a69f1 2019-07-25 stsp if (fileindex)
6549 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6550 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6553 69844fba 2019-07-11 stsp static const struct got_error *
6554 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6556 69844fba 2019-07-11 stsp const struct got_error *err;
6557 69844fba 2019-07-11 stsp struct got_reference *ref;
6559 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6560 69844fba 2019-07-11 stsp if (err) {
6561 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6562 69844fba 2019-07-11 stsp return NULL;
6563 69844fba 2019-07-11 stsp return err;
6566 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6567 69844fba 2019-07-11 stsp got_ref_close(ref);
6568 69844fba 2019-07-11 stsp return err;
6571 69844fba 2019-07-11 stsp static const struct got_error *
6572 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6574 69844fba 2019-07-11 stsp const struct got_error *err;
6575 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6576 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6578 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6580 69844fba 2019-07-11 stsp goto done;
6581 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6583 69844fba 2019-07-11 stsp goto done;
6585 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6587 69844fba 2019-07-11 stsp goto done;
6588 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6590 69844fba 2019-07-11 stsp goto done;
6592 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6594 69844fba 2019-07-11 stsp goto done;
6595 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6597 69844fba 2019-07-11 stsp goto done;
6599 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6601 69844fba 2019-07-11 stsp goto done;
6602 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6604 69844fba 2019-07-11 stsp goto done;
6607 69844fba 2019-07-11 stsp free(tmp_branch_name);
6608 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6609 69844fba 2019-07-11 stsp free(branch_ref_name);
6610 69844fba 2019-07-11 stsp free(commit_ref_name);
6611 e600f124 2021-03-21 stsp return err;
6614 e600f124 2021-03-21 stsp const struct got_error *
6615 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6616 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
6618 e600f124 2021-03-21 stsp const struct got_error *err;
6619 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
6620 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
6621 e600f124 2021-03-21 stsp const char *branch_name = NULL;
6622 e600f124 2021-03-21 stsp char *new_id_str = NULL;
6623 e600f124 2021-03-21 stsp char *refname = NULL;
6625 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
6626 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
6627 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6628 e600f124 2021-03-21 stsp branch_name += 11;
6630 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
6632 e600f124 2021-03-21 stsp return err;
6634 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6635 e600f124 2021-03-21 stsp new_id_str) == -1) {
6636 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
6637 e600f124 2021-03-21 stsp goto done;
6640 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
6642 e600f124 2021-03-21 stsp goto done;
6644 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
6646 e600f124 2021-03-21 stsp goto done;
6648 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
6650 e600f124 2021-03-21 stsp free(new_id_str);
6651 e600f124 2021-03-21 stsp free(refname);
6652 e600f124 2021-03-21 stsp free(old_commit_id);
6654 e600f124 2021-03-21 stsp got_ref_close(ref);
6655 69844fba 2019-07-11 stsp return err;
6658 818c7501 2019-07-11 stsp const struct got_error *
6659 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
6660 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6661 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6662 e600f124 2021-03-21 stsp struct got_repository *repo, int create_backup)
6664 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
6665 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
6666 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
6668 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6670 818c7501 2019-07-11 stsp return err;
6672 e600f124 2021-03-21 stsp if (create_backup) {
6673 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6674 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
6676 e600f124 2021-03-21 stsp goto done;
6679 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6681 818c7501 2019-07-11 stsp goto done;
6683 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
6685 818c7501 2019-07-11 stsp goto done;
6687 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
6689 818c7501 2019-07-11 stsp goto done;
6691 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
6693 a615e0e7 2020-12-16 stsp goto done;
6695 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
6697 a615e0e7 2020-12-16 stsp goto done;
6698 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6699 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6700 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
6701 a615e0e7 2020-12-16 stsp err = sync_err;
6703 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
6704 a615e0e7 2020-12-16 stsp free(fileindex_path);
6705 818c7501 2019-07-11 stsp free(new_head_commit_id);
6706 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6707 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6708 818c7501 2019-07-11 stsp err = unlockerr;
6709 818c7501 2019-07-11 stsp return err;
6712 818c7501 2019-07-11 stsp const struct got_error *
6713 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
6714 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6715 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
6716 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
6718 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
6719 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
6720 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
6721 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6722 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6723 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
6725 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6727 818c7501 2019-07-11 stsp return err;
6729 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
6730 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
6732 818c7501 2019-07-11 stsp goto done;
6734 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
6736 818c7501 2019-07-11 stsp goto done;
6739 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
6740 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
6741 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
6743 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
6745 818c7501 2019-07-11 stsp goto done;
6747 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6749 818c7501 2019-07-11 stsp goto done;
6751 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
6752 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
6754 ca355955 2019-07-12 stsp goto done;
6756 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
6758 ca355955 2019-07-12 stsp goto done;
6760 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6762 818c7501 2019-07-11 stsp goto done;
6764 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6765 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6766 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6767 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6768 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6769 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6770 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6771 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6772 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6774 1f1abb7e 2019-08-08 stsp goto sync;
6776 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6777 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
6779 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6780 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
6781 ca355955 2019-07-12 stsp err = sync_err;
6783 818c7501 2019-07-11 stsp got_ref_close(resolved);
6784 a3a2faf2 2019-07-12 stsp free(tree_id);
6785 818c7501 2019-07-11 stsp free(commit_id);
6786 0ebf8283 2019-07-24 stsp if (fileindex)
6787 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
6788 0ebf8283 2019-07-24 stsp free(fileindex_path);
6790 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6791 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6792 0ebf8283 2019-07-24 stsp err = unlockerr;
6793 0ebf8283 2019-07-24 stsp return err;
6796 0ebf8283 2019-07-24 stsp const struct got_error *
6797 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6798 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6799 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
6800 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6802 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6803 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6804 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
6805 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
6806 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6807 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
6808 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
6809 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6811 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6812 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6813 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6814 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6816 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6818 0ebf8283 2019-07-24 stsp return err;
6820 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6822 0ebf8283 2019-07-24 stsp goto done;
6824 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
6825 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
6826 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6829 0ebf8283 2019-07-24 stsp goto done;
6831 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6833 0ebf8283 2019-07-24 stsp goto done;
6835 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6837 0ebf8283 2019-07-24 stsp goto done;
6839 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6840 0ebf8283 2019-07-24 stsp worktree);
6842 0ebf8283 2019-07-24 stsp goto done;
6844 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6847 0ebf8283 2019-07-24 stsp goto done;
6849 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6851 0ebf8283 2019-07-24 stsp goto done;
6853 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
6855 0ebf8283 2019-07-24 stsp goto done;
6857 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6858 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6860 0ebf8283 2019-07-24 stsp goto done;
6861 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
6863 0ebf8283 2019-07-24 stsp goto done;
6864 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6865 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
6866 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
6867 0ebf8283 2019-07-24 stsp goto done;
6870 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6871 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6873 0ebf8283 2019-07-24 stsp goto done;
6874 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
6876 0ebf8283 2019-07-24 stsp goto done;
6878 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6880 0ebf8283 2019-07-24 stsp goto done;
6882 0ebf8283 2019-07-24 stsp free(fileindex_path);
6883 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6884 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6885 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6886 0ebf8283 2019-07-24 stsp if (wt_branch)
6887 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
6888 0ebf8283 2019-07-24 stsp if (err) {
6889 0ebf8283 2019-07-24 stsp if (*branch_ref) {
6890 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
6891 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6893 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6894 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6895 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6897 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6898 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6899 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6900 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6902 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
6904 0ebf8283 2019-07-24 stsp return err;
6907 0ebf8283 2019-07-24 stsp const struct got_error *
6908 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
6909 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6911 3e3a69f1 2019-07-25 stsp if (fileindex)
6912 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6913 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
6916 0ebf8283 2019-07-24 stsp const struct got_error *
6917 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
6918 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
6920 0ebf8283 2019-07-24 stsp const struct got_error *err;
6921 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6923 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6925 0ebf8283 2019-07-24 stsp return err;
6927 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6928 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6929 0ebf8283 2019-07-24 stsp return NULL;
6932 0ebf8283 2019-07-24 stsp const struct got_error *
6933 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
6934 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
6935 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6936 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6938 0ebf8283 2019-07-24 stsp const struct got_error *err;
6939 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6940 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6941 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6942 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6943 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6944 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6946 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6947 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6948 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6949 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6951 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6953 3e3a69f1 2019-07-25 stsp return err;
6955 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6957 f032f1f7 2019-08-04 stsp goto done;
6959 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6960 f032f1f7 2019-08-04 stsp &have_staged_files);
6961 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6962 f032f1f7 2019-08-04 stsp goto done;
6963 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6964 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6965 3e3a69f1 2019-07-25 stsp goto done;
6968 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6970 f032f1f7 2019-08-04 stsp goto done;
6972 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6974 0ebf8283 2019-07-24 stsp goto done;
6976 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6978 0ebf8283 2019-07-24 stsp goto done;
6980 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6981 0ebf8283 2019-07-24 stsp worktree);
6983 0ebf8283 2019-07-24 stsp goto done;
6985 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6987 0ebf8283 2019-07-24 stsp goto done;
6989 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6991 0ebf8283 2019-07-24 stsp goto done;
6992 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6994 0ebf8283 2019-07-24 stsp goto done;
6996 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6998 0ebf8283 2019-07-24 stsp goto done;
6999 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7001 0ebf8283 2019-07-24 stsp goto done;
7003 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7005 0ebf8283 2019-07-24 stsp goto done;
7007 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7008 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7009 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7010 0ebf8283 2019-07-24 stsp if (commit_ref)
7011 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7012 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7013 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7014 0ebf8283 2019-07-24 stsp if (err) {
7015 0ebf8283 2019-07-24 stsp free(*commit_id);
7016 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7017 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7018 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7019 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7020 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7021 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7023 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7024 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7025 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7027 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
7029 0ebf8283 2019-07-24 stsp return err;
7032 0ebf8283 2019-07-24 stsp static const struct got_error *
7033 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7035 0ebf8283 2019-07-24 stsp const struct got_error *err;
7036 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7037 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7039 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7041 0ebf8283 2019-07-24 stsp goto done;
7042 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
7044 0ebf8283 2019-07-24 stsp goto done;
7046 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7047 0ebf8283 2019-07-24 stsp worktree);
7049 0ebf8283 2019-07-24 stsp goto done;
7050 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7052 0ebf8283 2019-07-24 stsp goto done;
7054 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7056 0ebf8283 2019-07-24 stsp goto done;
7057 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7059 0ebf8283 2019-07-24 stsp goto done;
7061 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7063 0ebf8283 2019-07-24 stsp goto done;
7064 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7066 0ebf8283 2019-07-24 stsp goto done;
7068 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7069 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7070 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7071 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7072 0ebf8283 2019-07-24 stsp return err;
7075 0ebf8283 2019-07-24 stsp const struct got_error *
7076 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7077 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7078 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7079 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7081 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7082 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7083 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7084 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7085 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7087 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7089 0ebf8283 2019-07-24 stsp return err;
7091 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7092 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7094 0ebf8283 2019-07-24 stsp goto done;
7096 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7098 0ebf8283 2019-07-24 stsp goto done;
7100 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7102 0ebf8283 2019-07-24 stsp goto done;
7104 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7105 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7107 0ebf8283 2019-07-24 stsp goto done;
7109 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7111 0ebf8283 2019-07-24 stsp goto done;
7113 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7115 0ebf8283 2019-07-24 stsp goto done;
7117 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7118 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7119 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7120 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7121 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7122 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7123 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7124 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7125 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
7127 1f1abb7e 2019-08-08 stsp goto sync;
7129 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7130 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7132 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7133 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
7134 0ebf8283 2019-07-24 stsp err = sync_err;
7136 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
7137 0ebf8283 2019-07-24 stsp free(tree_id);
7138 818c7501 2019-07-11 stsp free(fileindex_path);
7140 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7141 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7142 818c7501 2019-07-11 stsp err = unlockerr;
7143 818c7501 2019-07-11 stsp return err;
7146 0ebf8283 2019-07-24 stsp const struct got_error *
7147 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
7148 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7149 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
7151 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7152 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
7153 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7154 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7156 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7158 0ebf8283 2019-07-24 stsp return err;
7160 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7161 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
7163 e600f124 2021-03-21 stsp goto done;
7165 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7166 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
7168 0ebf8283 2019-07-24 stsp goto done;
7170 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
7172 0ebf8283 2019-07-24 stsp goto done;
7174 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
7176 0ebf8283 2019-07-24 stsp goto done;
7178 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7180 0ebf8283 2019-07-24 stsp goto done;
7182 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7184 a615e0e7 2020-12-16 stsp goto done;
7186 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7188 a615e0e7 2020-12-16 stsp goto done;
7189 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7190 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7191 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7192 a615e0e7 2020-12-16 stsp err = sync_err;
7194 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7195 a615e0e7 2020-12-16 stsp free(fileindex_path);
7196 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
7197 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7198 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7199 0ebf8283 2019-07-24 stsp err = unlockerr;
7200 0ebf8283 2019-07-24 stsp return err;
7203 0ebf8283 2019-07-24 stsp const struct got_error *
7204 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7205 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7207 0ebf8283 2019-07-24 stsp const struct got_error *err;
7208 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7210 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7212 0ebf8283 2019-07-24 stsp return err;
7214 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7216 0ebf8283 2019-07-24 stsp goto done;
7218 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7220 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7221 2822a352 2019-10-15 stsp return err;
7224 2822a352 2019-10-15 stsp const struct got_error *
7225 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7226 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7227 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7228 2822a352 2019-10-15 stsp struct got_repository *repo)
7230 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7231 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7232 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7234 2822a352 2019-10-15 stsp *fileindex = NULL;
7235 2822a352 2019-10-15 stsp *branch_ref = NULL;
7236 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7238 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7240 2822a352 2019-10-15 stsp return err;
7242 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7243 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7244 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7245 2822a352 2019-10-15 stsp "update -b or different branch name required");
7246 2822a352 2019-10-15 stsp goto done;
7249 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7251 2822a352 2019-10-15 stsp goto done;
7253 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
7254 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7255 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7256 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7259 2822a352 2019-10-15 stsp goto done;
7261 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
7263 2822a352 2019-10-15 stsp goto done;
7265 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
7266 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7268 2822a352 2019-10-15 stsp if (err) {
7269 2822a352 2019-10-15 stsp if (*branch_ref) {
7270 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7271 2822a352 2019-10-15 stsp *branch_ref = NULL;
7273 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7274 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7275 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7277 2822a352 2019-10-15 stsp if (*fileindex) {
7278 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7279 2822a352 2019-10-15 stsp *fileindex = NULL;
7281 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7283 0cb83759 2019-08-03 stsp return err;
7286 2822a352 2019-10-15 stsp const struct got_error *
7287 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7288 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7289 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7290 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7291 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7293 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7294 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7295 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7297 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7299 2822a352 2019-10-15 stsp goto done;
7301 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7303 2822a352 2019-10-15 stsp goto done;
7305 2822a352 2019-10-15 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
7306 2822a352 2019-10-15 stsp worktree->path_prefix);
7308 2822a352 2019-10-15 stsp goto done;
7310 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7312 2822a352 2019-10-15 stsp goto done;
7314 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7315 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7317 2822a352 2019-10-15 stsp goto sync;
7319 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7321 2822a352 2019-10-15 stsp goto sync;
7323 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7325 6e210706 2021-01-22 stsp goto sync;
7327 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7329 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7330 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7331 2822a352 2019-10-15 stsp err = sync_err;
7334 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7335 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7336 2822a352 2019-10-15 stsp err = unlockerr;
7337 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7339 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7340 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7341 2822a352 2019-10-15 stsp err = unlockerr;
7342 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7344 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7345 2822a352 2019-10-15 stsp free(fileindex_path);
7346 2822a352 2019-10-15 stsp free(tree_id);
7348 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7349 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7350 2822a352 2019-10-15 stsp err = unlockerr;
7351 2822a352 2019-10-15 stsp return err;
7354 2822a352 2019-10-15 stsp const struct got_error *
7355 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7356 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7357 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7359 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7361 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7363 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7365 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7366 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7367 8b692cd0 2019-10-21 stsp err = unlockerr;
7368 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7370 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7371 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7372 8b692cd0 2019-10-21 stsp err = unlockerr;
7373 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7375 8b692cd0 2019-10-21 stsp return err;
7378 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
7379 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
7380 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7381 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7382 2db2652d 2019-08-07 stsp struct got_repository *repo;
7383 2db2652d 2019-08-07 stsp int have_changes;
7386 2db2652d 2019-08-07 stsp const struct got_error *
7387 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
7388 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7389 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7390 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7392 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
7393 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
7394 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
7395 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
7396 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
7397 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
7399 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
7400 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
7401 8b13ce36 2019-08-08 stsp return NULL;
7402 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
7403 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
7405 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7406 735ef5ac 2019-08-03 stsp if (ie == NULL)
7407 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7409 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7410 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7411 735ef5ac 2019-08-03 stsp relpath) == -1)
7412 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
7414 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
7415 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
7416 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7417 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
7420 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
7421 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7422 3aa5969e 2019-08-06 stsp goto done;
7423 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
7424 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
7425 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
7426 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7427 735ef5ac 2019-08-03 stsp goto done;
7430 2db2652d 2019-08-07 stsp a->have_changes = 1;
7432 735ef5ac 2019-08-03 stsp p = in_repo_path;
7433 735ef5ac 2019-08-03 stsp while (p[0] == '/')
7435 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
7436 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
7437 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
7439 735ef5ac 2019-08-03 stsp free(in_repo_path);
7440 dc424a06 2019-08-07 stsp return err;
7443 2db2652d 2019-08-07 stsp struct stage_path_arg {
7444 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7445 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7446 2db2652d 2019-08-07 stsp struct got_repository *repo;
7447 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
7448 2db2652d 2019-08-07 stsp void *status_arg;
7449 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
7450 2db2652d 2019-08-07 stsp void *patch_arg;
7451 7b5dc508 2019-10-28 stsp int staged_something;
7452 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
7455 2db2652d 2019-08-07 stsp static const struct got_error *
7456 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
7457 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7458 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7459 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7461 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
7462 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
7463 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
7464 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
7465 0cb83759 2019-08-03 stsp uint32_t stage;
7466 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
7467 0aeb8099 2020-07-23 stsp struct stat sb;
7469 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
7470 8b13ce36 2019-08-08 stsp return NULL;
7472 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7473 d3e7c587 2019-08-03 stsp if (ie == NULL)
7474 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7476 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7477 2db2652d 2019-08-07 stsp relpath)== -1)
7478 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
7480 0cb83759 2019-08-03 stsp switch (status) {
7481 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
7482 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
7483 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
7484 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
7485 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
7488 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7489 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
7490 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7491 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7492 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
7495 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
7498 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
7499 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
7500 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
7501 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
7502 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
7506 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
7507 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
7510 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7511 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7512 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7513 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
7515 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
7516 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7517 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
7518 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
7519 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
7520 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
7521 35213c7c 2020-07-23 stsp ssize_t target_len;
7522 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
7523 35213c7c 2020-07-23 stsp sizeof(target_path));
7524 35213c7c 2020-07-23 stsp if (target_len == -1) {
7525 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
7526 35213c7c 2020-07-23 stsp ondisk_path);
7529 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
7530 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
7531 35213c7c 2020-07-23 stsp a->worktree->root_path);
7534 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
7535 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
7536 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
7540 35213c7c 2020-07-23 stsp if (is_bad_symlink)
7541 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7542 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
7544 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7545 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
7547 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7548 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
7550 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7551 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7553 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7554 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
7555 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
7557 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
7558 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
7560 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7561 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7562 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
7563 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
7566 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7568 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7569 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7573 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
7574 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7575 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7576 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7578 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7579 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7582 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
7584 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
7585 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7587 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
7588 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
7591 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7595 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
7596 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
7597 dc424a06 2019-08-07 stsp free(path_content);
7598 2db2652d 2019-08-07 stsp free(ondisk_path);
7599 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
7600 0ebf8283 2019-07-24 stsp return err;
7603 0cb83759 2019-08-03 stsp const struct got_error *
7604 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
7605 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
7606 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
7607 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7608 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
7610 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7611 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
7612 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7613 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
7614 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
7615 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
7616 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
7617 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
7619 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7621 0cb83759 2019-08-03 stsp return err;
7623 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
7624 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
7626 735ef5ac 2019-08-03 stsp goto done;
7627 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7629 735ef5ac 2019-08-03 stsp goto done;
7630 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7632 0cb83759 2019-08-03 stsp goto done;
7634 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
7635 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
7636 2db2652d 2019-08-07 stsp oka.worktree = worktree;
7637 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
7638 2db2652d 2019-08-07 stsp oka.repo = repo;
7639 2db2652d 2019-08-07 stsp oka.have_changes = 0;
7640 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7641 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7642 f2a9dc41 2019-12-13 tracey check_stage_ok, &oka, NULL, NULL, 0, 0);
7644 735ef5ac 2019-08-03 stsp goto done;
7646 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
7647 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7648 2db2652d 2019-08-07 stsp goto done;
7651 2db2652d 2019-08-07 stsp spa.worktree = worktree;
7652 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
7653 2db2652d 2019-08-07 stsp spa.repo = repo;
7654 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
7655 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
7656 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
7657 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
7658 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
7659 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
7660 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7661 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7662 f2a9dc41 2019-12-13 tracey stage_path, &spa, NULL, NULL, 0, 0);
7664 2db2652d 2019-08-07 stsp goto done;
7666 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
7667 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7668 7b5dc508 2019-10-28 stsp goto done;
7671 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7672 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
7673 0cb83759 2019-08-03 stsp err = sync_err;
7675 735ef5ac 2019-08-03 stsp if (head_ref)
7676 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
7677 735ef5ac 2019-08-03 stsp free(head_commit_id);
7678 0cb83759 2019-08-03 stsp free(fileindex_path);
7679 0cb83759 2019-08-03 stsp if (fileindex)
7680 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
7681 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7682 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
7683 0cb83759 2019-08-03 stsp err = unlockerr;
7684 0cb83759 2019-08-03 stsp return err;
7687 ad493afc 2019-08-03 stsp struct unstage_path_arg {
7688 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
7689 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
7690 ad493afc 2019-08-03 stsp struct got_repository *repo;
7691 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
7692 ad493afc 2019-08-03 stsp void *progress_arg;
7693 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
7694 2e1f37b0 2019-08-08 stsp void *patch_arg;
7697 2e1f37b0 2019-08-08 stsp static const struct got_error *
7698 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
7699 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
7700 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
7701 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
7702 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
7704 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
7705 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
7706 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7707 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7708 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
7709 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
7710 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
7712 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7713 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7715 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
7717 2e1f37b0 2019-08-08 stsp return err;
7718 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7720 2e1f37b0 2019-08-08 stsp goto done;
7722 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7724 2e1f37b0 2019-08-08 stsp goto done;
7726 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7728 2e1f37b0 2019-08-08 stsp goto done;
7730 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7732 2e1f37b0 2019-08-08 stsp goto done;
7734 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7736 2e1f37b0 2019-08-08 stsp goto done;
7738 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7740 2e1f37b0 2019-08-08 stsp goto done;
7742 fe621944 2020-11-10 stsp err = got_diff_files(&diffreg_result, f1, label1, f2,
7743 64453f7e 2020-11-21 stsp path2, 3, 0, 1, NULL);
7745 2e1f37b0 2019-08-08 stsp goto done;
7747 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
7748 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
7750 2e1f37b0 2019-08-08 stsp goto done;
7751 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
7752 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
7754 2e1f37b0 2019-08-08 stsp goto done;
7756 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
7757 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
7758 2e1f37b0 2019-08-08 stsp goto done;
7760 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
7761 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
7762 2e1f37b0 2019-08-08 stsp goto done;
7764 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
7765 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7766 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
7767 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
7768 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
7769 fe621944 2020-11-10 stsp nchanges++;
7771 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7772 2e1f37b0 2019-08-08 stsp int choice;
7773 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
7774 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
7775 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
7776 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
7778 2e1f37b0 2019-08-08 stsp goto done;
7779 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
7780 2e1f37b0 2019-08-08 stsp have_content = 1;
7782 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
7783 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
7786 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
7787 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7788 f1e81a05 2019-08-10 stsp outfile, rejectfile);
7790 2e1f37b0 2019-08-08 stsp free(label1);
7792 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
7793 2e1f37b0 2019-08-08 stsp if (staged_blob)
7794 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
7795 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
7796 fe621944 2020-11-10 stsp if (free_err && err == NULL)
7797 fe621944 2020-11-10 stsp err = free_err;
7798 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
7799 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
7800 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
7801 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
7802 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
7803 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
7804 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7805 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
7806 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
7807 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
7808 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
7809 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
7810 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
7811 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
7812 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
7813 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7814 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
7815 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
7816 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7818 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
7819 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
7820 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
7821 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7822 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
7823 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
7824 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7826 2e1f37b0 2019-08-08 stsp free(path1);
7827 2e1f37b0 2019-08-08 stsp free(path2);
7828 fda8017d 2020-07-23 stsp return err;
7831 fda8017d 2020-07-23 stsp static const struct got_error *
7832 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
7833 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
7834 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7835 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
7836 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
7837 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7838 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7840 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
7841 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
7842 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
7843 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
7844 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
7845 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
7846 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
7847 fda8017d 2020-07-23 stsp struct stat sb;
7849 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
7850 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
7851 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
7853 fda8017d 2020-07-23 stsp return err;
7855 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
7856 fda8017d 2020-07-23 stsp return NULL;
7858 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
7859 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
7860 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
7862 fda8017d 2020-07-23 stsp goto done;
7865 fda8017d 2020-07-23 stsp f = fopen(path_unstaged_content, "r");
7866 fda8017d 2020-07-23 stsp if (f == NULL) {
7867 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
7868 fda8017d 2020-07-23 stsp path_unstaged_content);
7869 fda8017d 2020-07-23 stsp goto done;
7871 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
7872 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
7873 fda8017d 2020-07-23 stsp goto done;
7875 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
7876 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7877 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
7879 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
7880 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
7881 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
7882 fda8017d 2020-07-23 stsp goto done;
7884 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
7885 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7886 fda8017d 2020-07-23 stsp goto done;
7888 fda8017d 2020-07-23 stsp link_target[r] = '\0';
7889 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
7890 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
7891 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
7892 fda8017d 2020-07-23 stsp progress_arg);
7894 fda8017d 2020-07-23 stsp int local_changes_subsumed;
7896 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
7898 67a66647 2021-05-31 stsp return err;
7900 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
7901 67a66647 2021-05-31 stsp parent) == -1) {
7902 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
7903 67a66647 2021-05-31 stsp base_path = NULL;
7904 67a66647 2021-05-31 stsp goto done;
7907 67a66647 2021-05-31 stsp err = got_opentemp_named(&blob_base_path, &f_base, base_path);
7909 67a66647 2021-05-31 stsp goto done;
7910 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
7911 67a66647 2021-05-31 stsp blob_base);
7913 67a66647 2021-05-31 stsp goto done;
7916 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
7917 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
7919 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7920 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
7921 eec2f5a9 2021-06-03 stsp ondisk_path);
7923 eec2f5a9 2021-06-03 stsp goto done;
7926 eec2f5a9 2021-06-03 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
7927 eec2f5a9 2021-06-03 stsp if (fd == -1) {
7928 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
7929 eec2f5a9 2021-06-03 stsp goto done;
7931 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
7932 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
7933 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
7934 eec2f5a9 2021-06-03 stsp close(fd);
7935 eec2f5a9 2021-06-03 stsp goto done;
7939 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
7940 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
7941 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
7942 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
7943 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
7946 fda8017d 2020-07-23 stsp goto done;
7948 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
7949 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7950 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
7952 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7953 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
7956 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
7957 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
7958 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
7959 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
7960 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
7961 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
7962 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
7963 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
7964 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
7965 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
7966 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
7967 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
7968 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
7969 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
7970 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
7971 fda8017d 2020-07-23 stsp free(path_unstaged_content);
7972 fda8017d 2020-07-23 stsp free(path_new_staged_content);
7973 67a66647 2021-05-31 stsp free(blob_base_path);
7974 67a66647 2021-05-31 stsp free(parent);
7975 67a66647 2021-05-31 stsp free(base_path);
7976 2e1f37b0 2019-08-08 stsp return err;
7979 ad493afc 2019-08-03 stsp static const struct got_error *
7980 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
7981 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
7982 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7983 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7985 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
7986 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
7987 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
7988 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7989 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
7990 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
7991 ad493afc 2019-08-03 stsp int local_changes_subsumed;
7992 9bc94a15 2019-08-03 stsp struct stat sb;
7994 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
7995 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
7996 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
7997 2e1f37b0 2019-08-08 stsp return NULL;
7999 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8000 ad493afc 2019-08-03 stsp if (ie == NULL)
8001 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8003 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8005 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
8007 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
8008 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
8010 f69721c3 2019-10-21 stsp goto done;
8011 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8012 f69721c3 2019-10-21 stsp id_str) == -1) {
8013 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
8014 f69721c3 2019-10-21 stsp goto done;
8017 ad493afc 2019-08-03 stsp switch (staged_status) {
8018 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
8019 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
8020 ad493afc 2019-08-03 stsp blob_id, 8192);
8023 ad493afc 2019-08-03 stsp /* fall through */
8024 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
8025 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8026 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
8027 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8028 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8029 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8032 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
8035 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
8036 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
8037 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
8038 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
8039 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
8040 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
8043 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
8044 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
8047 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
8048 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
8049 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
8050 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
8051 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
8052 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
8053 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
8054 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8055 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
8057 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
8058 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8059 36bf999c 2020-07-23 stsp char *staged_target;
8060 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
8061 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
8063 36bf999c 2020-07-23 stsp goto done;
8064 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
8065 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
8066 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
8067 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
8068 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
8069 36bf999c 2020-07-23 stsp free(staged_target);
8071 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
8072 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
8073 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
8074 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
8075 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
8076 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8077 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
8081 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8084 2ac8aa02 2020-11-09 stsp if (err == NULL) {
8085 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
8086 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
8087 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8090 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
8091 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8092 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8093 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8094 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8097 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8099 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8100 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8104 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8105 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8106 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
8107 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
8110 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
8114 ad493afc 2019-08-03 stsp free(ondisk_path);
8115 ad493afc 2019-08-03 stsp if (blob_base)
8116 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
8117 ad493afc 2019-08-03 stsp if (blob_staged)
8118 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
8119 f69721c3 2019-10-21 stsp free(id_str);
8120 f69721c3 2019-10-21 stsp free(label_orig);
8121 ad493afc 2019-08-03 stsp return err;
8124 ad493afc 2019-08-03 stsp const struct got_error *
8125 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
8126 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
8127 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
8128 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8129 ad493afc 2019-08-03 stsp struct got_repository *repo)
8131 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8132 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
8133 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8134 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
8135 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
8137 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8139 ad493afc 2019-08-03 stsp return err;
8141 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8143 ad493afc 2019-08-03 stsp goto done;
8145 ad493afc 2019-08-03 stsp upa.worktree = worktree;
8146 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
8147 ad493afc 2019-08-03 stsp upa.repo = repo;
8148 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
8149 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
8150 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
8151 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
8152 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8153 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8154 f2a9dc41 2019-12-13 tracey unstage_path, &upa, NULL, NULL, 0, 0);
8156 ad493afc 2019-08-03 stsp goto done;
8159 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8160 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
8161 ad493afc 2019-08-03 stsp err = sync_err;
8163 ad493afc 2019-08-03 stsp free(fileindex_path);
8164 ad493afc 2019-08-03 stsp if (fileindex)
8165 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
8166 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8167 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
8168 ad493afc 2019-08-03 stsp err = unlockerr;
8169 ad493afc 2019-08-03 stsp return err;
8172 b2118c49 2020-07-28 stsp struct report_file_info_arg {
8173 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
8174 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
8175 b2118c49 2020-07-28 stsp void *info_arg;
8176 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
8177 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
8178 b2118c49 2020-07-28 stsp void *cancel_arg;
8181 b2118c49 2020-07-28 stsp static const struct got_error *
8182 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
8184 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
8185 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
8186 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
8187 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8188 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
8189 b2118c49 2020-07-28 stsp int stage;
8191 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8192 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
8194 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
8195 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8196 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
8199 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
8200 b2118c49 2020-07-28 stsp return NULL;
8202 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_blob(ie)) {
8203 b2118c49 2020-07-28 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8204 b2118c49 2020-07-28 stsp blob_idp = &blob_id;
8206 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
8207 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8208 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
8209 b2118c49 2020-07-28 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8210 b2118c49 2020-07-28 stsp SHA1_DIGEST_LENGTH);
8211 b2118c49 2020-07-28 stsp staged_blob_idp = &staged_blob_id;
8214 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_commit(ie)) {
8215 b2118c49 2020-07-28 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8216 b2118c49 2020-07-28 stsp commit_idp = &commit_id;
8219 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8220 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8223 b2118c49 2020-07-28 stsp const struct got_error *
8224 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
8225 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
8226 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
8227 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8230 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
8231 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
8232 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
8233 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
8235 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
8237 b2118c49 2020-07-28 stsp return err;
8239 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8241 b2118c49 2020-07-28 stsp goto done;
8243 b2118c49 2020-07-28 stsp arg.worktree = worktree;
8244 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
8245 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
8246 b2118c49 2020-07-28 stsp arg.paths = paths;
8247 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
8248 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
8249 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8252 b2118c49 2020-07-28 stsp free(fileindex_path);
8253 b2118c49 2020-07-28 stsp if (fileindex)
8254 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
8255 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
8256 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
8257 b2118c49 2020-07-28 stsp err = unlockerr;
8258 b2118c49 2020-07-28 stsp return err;