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) {
1950 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1951 b8f41171 2019-02-10 stsp goto done;
1953 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1954 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1956 5036ab18 2020-04-18 stsp goto done;
1959 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1960 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1961 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1963 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1964 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1965 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1966 c6e8a826 2021-04-05 stsp * updating contents of this file.
1968 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1969 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1970 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1971 c6e8a826 2021-04-05 stsp /* Same commit. */
1972 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1973 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1975 e2b1e152 2019-07-13 stsp goto done;
1976 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1978 6353ad76 2019-02-08 stsp goto done;
1980 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1981 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1982 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1983 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
1984 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1985 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1987 0f58026f 2021-04-05 stsp goto done;
1988 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1990 b8f41171 2019-02-10 stsp goto done;
1994 56e0773d 2019-11-28 stsp err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1996 6353ad76 2019-02-08 stsp goto done;
1998 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1999 234035bc 2019-06-01 stsp int update_timestamps;
2000 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
2001 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2002 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
2003 234035bc 2019-06-01 stsp struct got_object_id id2;
2004 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2005 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
2007 f69721c3 2019-10-21 stsp goto done;
2009 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
2010 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
2011 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2012 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
2013 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
2014 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
2015 f69721c3 2019-10-21 stsp goto done;
2017 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2018 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2019 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2020 234035bc 2019-06-01 stsp goto done;
2023 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2024 36bf999c 2020-07-23 stsp char *link_target;
2025 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
2027 36bf999c 2020-07-23 stsp goto done;
2028 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
2029 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
2030 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
2031 36bf999c 2020-07-23 stsp free(link_target);
2033 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
2034 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
2035 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
2036 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
2038 f69721c3 2019-10-21 stsp free(label_orig);
2039 234035bc 2019-06-01 stsp if (blob2)
2040 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
2042 909d120e 2019-09-22 stsp goto done;
2044 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
2045 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2046 234035bc 2019-06-01 stsp * unmodified files again.
2048 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2049 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2050 234035bc 2019-06-01 stsp update_timestamps);
2051 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2052 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2053 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2054 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2055 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2057 1ee397ad 2019-07-12 stsp goto done;
2058 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2059 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2061 13d9040b 2019-03-27 stsp goto done;
2063 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2064 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2065 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2066 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2067 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2068 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2069 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2071 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2072 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2073 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2074 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2075 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2078 13d9040b 2019-03-27 stsp goto done;
2081 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2082 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2083 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2085 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2086 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2087 054041d0 2020-06-24 stsp &blob->id);
2090 13d9040b 2019-03-27 stsp goto done;
2092 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2093 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2094 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2097 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2099 6353ad76 2019-02-08 stsp free(ondisk_path);
2100 8da9e5f4 2019-01-12 stsp return err;
2103 90285c3b 2019-01-08 stsp static const struct got_error *
2104 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2106 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2107 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
2109 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2110 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2112 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2113 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2114 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2116 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2118 fddefe3b 2020-10-20 stsp char *parent;
2119 fddefe3b 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
2122 fddefe3b 2020-10-20 stsp free(ondisk_path);
2123 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2124 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2125 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2126 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2127 fddefe3b 2020-10-20 stsp ondisk_path);
2130 fddefe3b 2020-10-20 stsp } while (got_path_cmp(ondisk_path, root_path,
2131 fddefe3b 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
2133 90285c3b 2019-01-08 stsp free(ondisk_path);
2134 90285c3b 2019-01-08 stsp return err;
2137 708d8e67 2019-03-27 stsp static const struct got_error *
2138 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2139 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2140 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2142 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2143 708d8e67 2019-03-27 stsp unsigned char status;
2144 708d8e67 2019-03-27 stsp struct stat sb;
2145 708d8e67 2019-03-27 stsp char *ondisk_path;
2147 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2148 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2150 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2152 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2154 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2156 5a58a424 2020-06-23 stsp goto done;
2158 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2159 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2160 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2161 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2162 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2163 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2164 993e2a1b 2020-07-23 stsp goto done;
2166 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2167 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2168 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2169 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2171 993e2a1b 2020-07-23 stsp goto done;
2172 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2173 993e2a1b 2020-07-23 stsp ie->path);
2174 993e2a1b 2020-07-23 stsp goto done;
2177 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2178 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2179 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2181 5a58a424 2020-06-23 stsp goto done;
2183 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2184 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2186 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2187 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2189 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2191 5a58a424 2020-06-23 stsp goto done;
2192 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2193 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2195 5a58a424 2020-06-23 stsp goto done;
2197 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2200 5a58a424 2020-06-23 stsp free(ondisk_path);
2201 fc6346c4 2019-03-27 stsp return err;
2204 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2205 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2206 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2207 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2208 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2209 8da9e5f4 2019-01-12 stsp void *progress_arg;
2210 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2211 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2214 512f0d0e 2019-01-02 stsp static const struct got_error *
2215 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2216 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2218 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2220 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2221 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2223 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2224 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2227 8da9e5f4 2019-01-12 stsp static const struct got_error *
2228 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2230 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2232 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2233 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2235 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2236 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2239 9d31a1d8 2018-03-11 stsp static const struct got_error *
2240 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2242 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2243 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2244 8da9e5f4 2019-01-12 stsp char *path;
2246 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2247 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2249 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2250 63c5ca5d 2019-08-24 stsp return NULL;
2252 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2253 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2255 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2257 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2258 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2260 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2261 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2263 8da9e5f4 2019-01-12 stsp free(path);
2264 0cd1c46a 2019-03-11 stsp return err;
2267 b2118c49 2020-07-28 stsp const struct got_error *
2268 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2270 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2272 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2273 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2274 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2275 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2278 b2118c49 2020-07-28 stsp return NULL;
2281 818c7501 2019-07-11 stsp static const struct got_error *
2282 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2284 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2285 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2287 517bab73 2019-03-11 stsp *refname = NULL;
2289 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2291 b2118c49 2020-07-28 stsp return err;
2293 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2294 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2295 0647c563 2019-03-11 stsp *refname = NULL;
2297 517bab73 2019-03-11 stsp free(uuidstr);
2298 517bab73 2019-03-11 stsp return err;
2301 818c7501 2019-07-11 stsp const struct got_error *
2302 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2304 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2307 818c7501 2019-07-11 stsp static const struct got_error *
2308 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2310 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2311 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2314 818c7501 2019-07-11 stsp static const struct got_error *
2315 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2317 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2320 818c7501 2019-07-11 stsp static const struct got_error *
2321 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2323 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2324 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2327 818c7501 2019-07-11 stsp static const struct got_error *
2328 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2330 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2331 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2334 0ebf8283 2019-07-24 stsp static const struct got_error *
2335 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2337 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2338 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2341 0ebf8283 2019-07-24 stsp static const struct got_error *
2342 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2344 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2345 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2348 0ebf8283 2019-07-24 stsp static const struct got_error *
2349 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2351 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2352 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2355 0ebf8283 2019-07-24 stsp static const struct got_error *
2356 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2358 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2359 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2362 0ebf8283 2019-07-24 stsp const struct got_error *
2363 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2364 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2366 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2367 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2368 0ebf8283 2019-07-24 stsp *path = NULL;
2369 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2371 0ebf8283 2019-07-24 stsp return NULL;
2375 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2376 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2378 517bab73 2019-03-11 stsp static const struct got_error *
2379 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2381 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2382 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2383 517bab73 2019-03-11 stsp char *refname;
2385 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2387 517bab73 2019-03-11 stsp return err;
2389 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2391 0cd1c46a 2019-03-11 stsp goto done;
2393 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2395 0cd1c46a 2019-03-11 stsp free(refname);
2397 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2398 3e3a69f1 2019-07-25 stsp return err;
2401 3e3a69f1 2019-07-25 stsp static const struct got_error *
2402 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2404 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2406 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2407 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2408 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2409 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2411 9d31a1d8 2018-03-11 stsp return err;
2415 ebf99748 2019-05-09 stsp static const struct got_error *
2416 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2417 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2419 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2420 ebf99748 2019-05-09 stsp FILE *index = NULL;
2422 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2423 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2424 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2425 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2427 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2429 ebf99748 2019-05-09 stsp goto done;
2431 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
2432 ebf99748 2019-05-09 stsp if (index == NULL) {
2433 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2434 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2436 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2437 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2438 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2441 ebf99748 2019-05-09 stsp if (err) {
2442 ebf99748 2019-05-09 stsp free(*fileindex_path);
2443 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2444 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2445 ebf99748 2019-05-09 stsp *fileindex = NULL;
2447 ebf99748 2019-05-09 stsp return err;
2450 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2451 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2452 c932eeeb 2019-05-22 stsp const char *path;
2453 c932eeeb 2019-05-22 stsp size_t path_len;
2454 c932eeeb 2019-05-22 stsp const char *entry_name;
2455 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2456 a484d721 2019-06-10 stsp void *progress_arg;
2459 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2460 c932eeeb 2019-05-22 stsp static const struct got_error *
2461 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2463 1ee397ad 2019-07-12 stsp const struct got_error *err;
2464 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2466 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2467 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2468 c932eeeb 2019-05-22 stsp return NULL;
2469 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2470 102ff934 2019-06-11 stsp return NULL;
2472 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2473 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2474 c932eeeb 2019-05-22 stsp return NULL;
2476 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2477 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2478 edd02c5e 2019-07-11 stsp ie->path);
2480 1ee397ad 2019-07-12 stsp return err;
2482 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2483 c932eeeb 2019-05-22 stsp return NULL;
2486 a615e0e7 2020-12-16 stsp static const struct got_error *
2487 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2488 a615e0e7 2020-12-16 stsp struct got_fileindex *fileindex,
2489 a615e0e7 2020-12-16 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2491 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2493 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2494 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2495 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2496 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2497 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2498 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2500 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2501 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2504 9c6338c4 2019-06-02 stsp static const struct got_error *
2505 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2507 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2508 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2509 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2510 867630bb 2020-01-17 stsp struct timespec timeout;
2512 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2513 9c6338c4 2019-06-02 stsp fileindex_path);
2515 9c6338c4 2019-06-02 stsp goto done;
2517 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2519 9c6338c4 2019-06-02 stsp goto done;
2521 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2522 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2523 9c6338c4 2019-06-02 stsp fileindex_path);
2524 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2528 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2529 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2530 867630bb 2020-01-17 stsp * was recorded in the file index.
2532 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2533 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2534 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2536 9c6338c4 2019-06-02 stsp if (new_index)
2537 9c6338c4 2019-06-02 stsp fclose(new_index);
2538 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2539 9c6338c4 2019-06-02 stsp return err;
2542 6ced4176 2019-07-12 stsp static const struct got_error *
2543 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2544 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2545 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
2547 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2548 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2549 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2550 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2552 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2553 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2554 6ced4176 2019-07-12 stsp *tree_id = NULL;
2556 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2557 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2558 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2559 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2560 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2561 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2562 6ced4176 2019-07-12 stsp goto done;
2564 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2565 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
2567 6ced4176 2019-07-12 stsp goto done;
2568 6ced4176 2019-07-12 stsp return NULL;
2571 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2573 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2574 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2575 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2576 6ced4176 2019-07-12 stsp goto done;
2579 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2580 6ced4176 2019-07-12 stsp in_repo_path);
2582 6ced4176 2019-07-12 stsp goto done;
2584 6ced4176 2019-07-12 stsp free(in_repo_path);
2585 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2587 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2589 6ced4176 2019-07-12 stsp goto done;
2591 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2592 6ced4176 2019-07-12 stsp /* Check out a single file. */
2593 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2594 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2595 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2596 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2597 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2598 6ced4176 2019-07-12 stsp goto done;
2600 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2601 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2602 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2603 6ced4176 2019-07-12 stsp goto done;
2606 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2607 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2609 6ced4176 2019-07-12 stsp return err;
2610 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2611 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2612 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2613 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2614 6ced4176 2019-07-12 stsp goto done;
2617 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2618 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
2620 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2621 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2622 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2623 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2624 6ced4176 2019-07-12 stsp goto done;
2626 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2627 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2628 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2629 6ced4176 2019-07-12 stsp goto done;
2634 6ced4176 2019-07-12 stsp free(in_repo_path);
2635 6ced4176 2019-07-12 stsp if (err) {
2636 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2637 6ced4176 2019-07-12 stsp free(*tree_relpath);
2638 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2639 6ced4176 2019-07-12 stsp free(*tree_id);
2640 6ced4176 2019-07-12 stsp *tree_id = NULL;
2642 07ed472d 2019-07-12 stsp return err;
2645 07ed472d 2019-07-12 stsp static const struct got_error *
2646 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2647 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2648 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2649 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2651 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2652 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2653 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2654 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2655 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2657 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2658 7f47418f 2019-12-20 stsp if (err) {
2659 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2660 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2661 7f47418f 2019-12-20 stsp goto done;
2662 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2663 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2665 7f47418f 2019-12-20 stsp return err;
2668 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2669 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2671 07ed472d 2019-07-12 stsp goto done;
2673 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2675 07ed472d 2019-07-12 stsp goto done;
2677 07ed472d 2019-07-12 stsp if (entry_name &&
2678 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2679 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2680 07ed472d 2019-07-12 stsp goto done;
2683 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2684 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2685 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2686 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2687 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2688 07ed472d 2019-07-12 stsp arg.repo = repo;
2689 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2690 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2691 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2692 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2693 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2694 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2697 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2698 07ed472d 2019-07-12 stsp if (commit)
2699 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2700 6ced4176 2019-07-12 stsp return err;
2703 9d31a1d8 2018-03-11 stsp const struct got_error *
2704 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2705 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2706 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2707 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2709 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2710 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2711 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2712 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2713 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2714 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2715 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2716 f2ea84fa 2019-07-27 stsp SIMPLEQ_ENTRY(tree_path_data) entry;
2717 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2718 f2ea84fa 2019-07-27 stsp int entry_type;
2719 f2ea84fa 2019-07-27 stsp char *relpath;
2720 f2ea84fa 2019-07-27 stsp char *entry_name;
2721 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2722 f2ea84fa 2019-07-27 stsp SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2724 f2ea84fa 2019-07-27 stsp SIMPLEQ_INIT(&tree_paths);
2726 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2728 9d31a1d8 2018-03-11 stsp return err;
2730 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2731 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2732 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2733 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2734 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2735 f2ea84fa 2019-07-27 stsp goto done;
2738 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2739 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2740 f2ea84fa 2019-07-27 stsp if (err) {
2741 f2ea84fa 2019-07-27 stsp free(tpd);
2742 6ced4176 2019-07-12 stsp goto done;
2745 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2746 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2747 f2ea84fa 2019-07-27 stsp if (err) {
2748 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2749 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2750 f2ea84fa 2019-07-27 stsp free(tpd);
2751 f2ea84fa 2019-07-27 stsp goto done;
2754 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2756 f2ea84fa 2019-07-27 stsp SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2760 51514078 2018-12-25 stsp * Read the file index.
2761 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2762 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2764 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2766 c4cdcb68 2019-04-03 stsp goto done;
2768 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2769 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2770 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2772 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2773 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2774 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2778 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2779 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2780 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2781 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2782 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2783 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2784 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2785 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2789 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_NEXT(tpd, entry);
2791 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2792 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2793 af12c6b9 2019-06-04 stsp err = sync_err;
2795 9c6338c4 2019-06-02 stsp free(fileindex_path);
2797 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2798 9d31a1d8 2018-03-11 stsp if (commit)
2799 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2800 5ade8233 2019-07-12 stsp if (fileindex)
2801 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2802 f2ea84fa 2019-07-27 stsp while (!SIMPLEQ_EMPTY(&tree_paths)) {
2803 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2804 f2ea84fa 2019-07-27 stsp SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2805 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2806 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2807 f2ea84fa 2019-07-27 stsp free(tpd);
2809 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2810 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2811 9d31a1d8 2018-03-11 stsp err = unlockerr;
2812 f8d1f275 2019-02-04 stsp return err;
2815 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2816 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2817 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2818 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2819 234035bc 2019-06-01 stsp void *progress_arg;
2820 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2821 234035bc 2019-06-01 stsp void *cancel_arg;
2822 f69721c3 2019-10-21 stsp const char *label_orig;
2823 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2826 234035bc 2019-06-01 stsp static const struct got_error *
2827 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2828 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
2829 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
2830 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2832 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2833 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2834 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2835 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2836 234035bc 2019-06-01 stsp struct stat sb;
2837 234035bc 2019-06-01 stsp unsigned char status;
2838 234035bc 2019-06-01 stsp int local_changes_subsumed;
2839 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2840 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2842 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2843 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2844 d6c87207 2019-08-02 stsp strlen(path2));
2845 1ee397ad 2019-07-12 stsp if (ie == NULL)
2846 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2847 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2849 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2850 234035bc 2019-06-01 stsp path2) == -1)
2851 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2853 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2856 234035bc 2019-06-01 stsp goto done;
2858 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2859 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2860 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2861 234035bc 2019-06-01 stsp goto done;
2863 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2864 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2865 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2866 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2867 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2868 234035bc 2019-06-01 stsp goto done;
2871 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2872 36bf999c 2020-07-23 stsp char *link_target2;
2873 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2875 36bf999c 2020-07-23 stsp goto done;
2876 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2877 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2878 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2879 36bf999c 2020-07-23 stsp free(link_target2);
2883 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
2884 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
2885 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
2886 1af628f4 2021-06-03 stsp goto done;
2888 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2889 1af628f4 2021-06-03 stsp f_orig, blob1);
2891 1af628f4 2021-06-03 stsp goto done;
2893 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
2894 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
2895 1af628f4 2021-06-03 stsp goto done;
2896 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2897 54d5be07 2021-06-03 stsp f_deriv2, blob2);
2899 1af628f4 2021-06-03 stsp goto done;
2901 1af628f4 2021-06-03 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
2902 1af628f4 2021-06-03 stsp if (fd == -1) {
2903 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
2904 1af628f4 2021-06-03 stsp ondisk_path);
2905 1af628f4 2021-06-03 stsp goto done;
2907 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
2908 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
2909 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
2910 1af628f4 2021-06-03 stsp ondisk_path);
2911 1af628f4 2021-06-03 stsp close(fd);
2912 1af628f4 2021-06-03 stsp goto done;
2914 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
2916 1af628f4 2021-06-03 stsp goto done;
2917 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
2918 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2919 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
2920 1af628f4 2021-06-03 stsp goto done;
2922 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
2923 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2924 54d5be07 2021-06-03 stsp sb.st_mode, a->label_orig, NULL, label_deriv2,
2925 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
2926 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
2928 234035bc 2019-06-01 stsp } else if (blob1) {
2929 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2930 d6c87207 2019-08-02 stsp strlen(path1));
2931 1ee397ad 2019-07-12 stsp if (ie == NULL)
2932 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2933 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2935 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2936 2b92fad7 2019-06-02 stsp path1) == -1)
2937 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2939 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2942 2b92fad7 2019-06-02 stsp goto done;
2944 2b92fad7 2019-06-02 stsp switch (status) {
2945 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2946 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2947 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2949 1ee397ad 2019-07-12 stsp goto done;
2950 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2952 2b92fad7 2019-06-02 stsp goto done;
2954 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2956 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2957 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
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;
2963 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2965 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
2966 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
2967 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
2969 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
2970 0a22ca1a 2020-09-23 stsp * exists in the repository.
2972 0a22ca1a 2020-09-23 stsp err = got_object_blob_file_create(&id, &blob1_f, path1);
2974 0a22ca1a 2020-09-23 stsp goto done;
2975 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
2976 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2977 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
2979 0a22ca1a 2020-09-23 stsp goto done;
2980 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
2983 0a22ca1a 2020-09-23 stsp goto done;
2985 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
2988 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2989 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
2991 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
2992 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
2995 0a22ca1a 2020-09-23 stsp goto done;
2998 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2999 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
3000 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3001 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
3003 1ee397ad 2019-07-12 stsp goto done;
3005 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
3006 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
3008 1ee397ad 2019-07-12 stsp goto done;
3013 234035bc 2019-06-01 stsp } else if (blob2) {
3014 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3015 234035bc 2019-06-01 stsp path2) == -1)
3016 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3017 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3018 d6c87207 2019-08-02 stsp strlen(path2));
3020 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
3021 7f91a133 2019-12-13 stsp -1, NULL, repo);
3023 234035bc 2019-06-01 stsp goto done;
3024 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3025 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3026 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3027 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
3028 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3029 1ee397ad 2019-07-12 stsp status, path2);
3030 234035bc 2019-06-01 stsp goto done;
3032 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3033 36bf999c 2020-07-23 stsp char *link_target2;
3034 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3037 36bf999c 2020-07-23 stsp goto done;
3038 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3039 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3040 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3041 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3042 36bf999c 2020-07-23 stsp free(link_target2);
3043 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3044 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3045 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3046 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3047 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3048 526a746f 2020-07-23 stsp a->progress_arg);
3050 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3051 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3054 dfe9fba0 2020-07-23 stsp goto done;
3055 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3056 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
3057 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, blob2->id.sha1,
3058 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
3060 234035bc 2019-06-01 stsp goto done;
3063 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
3064 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
3065 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
3066 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
3067 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
3068 c90c8ce3 2020-07-23 stsp 0, 1, repo, a->progress_cb, a->progress_arg);
3070 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
3071 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3072 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
3075 234035bc 2019-06-01 stsp goto done;
3076 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
3078 234035bc 2019-06-01 stsp goto done;
3079 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
3080 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, NULL, NULL, 1);
3081 3969253a 2020-03-07 stsp if (err) {
3082 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3083 3969253a 2020-03-07 stsp goto done;
3085 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3086 2b92fad7 2019-06-02 stsp if (err) {
3087 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
3088 2b92fad7 2019-06-02 stsp goto done;
3090 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
3091 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
3092 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
3097 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3098 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3099 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3100 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3101 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3102 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3103 1af628f4 2021-06-03 stsp free(id_str);
3104 54d5be07 2021-06-03 stsp free(label_deriv2);
3105 234035bc 2019-06-01 stsp free(ondisk_path);
3106 234035bc 2019-06-01 stsp return err;
3109 234035bc 2019-06-01 stsp struct check_merge_ok_arg {
3110 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3111 234035bc 2019-06-01 stsp struct got_repository *repo;
3114 234035bc 2019-06-01 stsp static const struct got_error *
3115 234035bc 2019-06-01 stsp check_merge_ok(void *arg, struct got_fileindex_entry *ie)
3117 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3118 234035bc 2019-06-01 stsp struct check_merge_ok_arg *a = arg;
3119 234035bc 2019-06-01 stsp unsigned char status;
3120 234035bc 2019-06-01 stsp struct stat sb;
3121 234035bc 2019-06-01 stsp char *ondisk_path;
3123 234035bc 2019-06-01 stsp /* Reject merges into a work tree with mixed base commits. */
3124 234035bc 2019-06-01 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3125 234035bc 2019-06-01 stsp SHA1_DIGEST_LENGTH))
3126 234035bc 2019-06-01 stsp return got_error(GOT_ERR_MIXED_COMMITS);
3128 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3130 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3132 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3133 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3135 234035bc 2019-06-01 stsp return err;
3136 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3137 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3139 234035bc 2019-06-01 stsp return NULL;
3142 818c7501 2019-07-11 stsp static const struct got_error *
3143 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3144 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3145 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3146 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3147 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3149 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3150 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3151 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3152 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3153 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3155 03415a1a 2019-06-02 stsp if (commit_id1) {
3156 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3157 03415a1a 2019-06-02 stsp worktree->path_prefix);
3158 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3159 03415a1a 2019-06-02 stsp goto done;
3161 69d57f3d 2020-07-31 stsp if (tree_id1) {
3162 69d57f3d 2020-07-31 stsp char *id_str;
3164 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3166 03415a1a 2019-06-02 stsp goto done;
3168 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3170 f69721c3 2019-10-21 stsp goto done;
3172 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3173 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3174 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3175 f69721c3 2019-10-21 stsp free(id_str);
3176 f69721c3 2019-10-21 stsp goto done;
3178 f69721c3 2019-10-21 stsp free(id_str);
3181 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3182 234035bc 2019-06-01 stsp worktree->path_prefix);
3184 234035bc 2019-06-01 stsp goto done;
3186 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3188 234035bc 2019-06-01 stsp goto done;
3190 234035bc 2019-06-01 stsp arg.worktree = worktree;
3191 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3192 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3193 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3194 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3195 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3196 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3197 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3198 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3199 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3200 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3201 af12c6b9 2019-06-04 stsp err = sync_err;
3203 234035bc 2019-06-01 stsp if (tree1)
3204 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3205 234035bc 2019-06-01 stsp if (tree2)
3206 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3207 f69721c3 2019-10-21 stsp free(label_orig);
3208 818c7501 2019-07-11 stsp return err;
3211 818c7501 2019-07-11 stsp const struct got_error *
3212 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3213 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3214 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3215 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3217 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3218 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3219 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3220 818c7501 2019-07-11 stsp struct check_merge_ok_arg mok_arg;
3222 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3224 818c7501 2019-07-11 stsp return err;
3226 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3228 818c7501 2019-07-11 stsp goto done;
3230 818c7501 2019-07-11 stsp mok_arg.worktree = worktree;
3231 818c7501 2019-07-11 stsp mok_arg.repo = repo;
3232 818c7501 2019-07-11 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3233 818c7501 2019-07-11 stsp &mok_arg);
3235 818c7501 2019-07-11 stsp goto done;
3237 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3238 818c7501 2019-07-11 stsp commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3240 818c7501 2019-07-11 stsp if (fileindex)
3241 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3242 818c7501 2019-07-11 stsp free(fileindex_path);
3243 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3244 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3245 234035bc 2019-06-01 stsp err = unlockerr;
3246 234035bc 2019-06-01 stsp return err;
3249 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3250 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3251 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3252 927df6b7 2019-02-10 stsp const char *status_path;
3253 927df6b7 2019-02-10 stsp size_t status_path_len;
3254 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3255 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3256 f8d1f275 2019-02-04 stsp void *status_arg;
3257 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3258 f8d1f275 2019-02-04 stsp void *cancel_arg;
3259 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3260 6841da00 2019-08-08 stsp struct got_pathlist_head ignores;
3261 f2a9dc41 2019-12-13 tracey int report_unchanged;
3262 3143d852 2020-06-25 stsp int no_ignores;
3265 f8d1f275 2019-02-04 stsp static const struct got_error *
3266 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3267 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3268 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3269 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3271 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3272 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3273 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3274 927df6b7 2019-02-10 stsp struct stat sb;
3275 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3276 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3277 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3279 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3281 98eaaa12 2019-08-03 stsp return err;
3283 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3284 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3285 98eaaa12 2019-08-03 stsp return NULL;
3287 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3288 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3289 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3291 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3292 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3293 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3295 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3296 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3297 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3298 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3299 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3302 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3303 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3306 927df6b7 2019-02-10 stsp static const struct got_error *
3307 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3308 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3310 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3311 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3312 f8d1f275 2019-02-04 stsp char *abspath;
3314 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3315 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3317 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3318 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3319 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3320 927df6b7 2019-02-10 stsp return NULL;
3322 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3323 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3324 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3325 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3327 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3328 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3329 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3332 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3333 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3334 f8d1f275 2019-02-04 stsp free(abspath);
3335 9d31a1d8 2018-03-11 stsp return err;
3338 f8d1f275 2019-02-04 stsp static const struct got_error *
3339 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3341 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3342 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3343 2ec1f75b 2019-03-26 stsp unsigned char status;
3345 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3346 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3348 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3349 927df6b7 2019-02-10 stsp return NULL;
3351 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3352 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3353 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3354 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3356 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3357 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3358 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3362 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3364 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3366 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3367 6841da00 2019-08-08 stsp free((char *)pe->path);
3368 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3372 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3374 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3376 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3377 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3378 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3379 6841da00 2019-08-08 stsp free((char *)pe->path);
3381 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3384 6841da00 2019-08-08 stsp static const struct got_error *
3385 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3387 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3388 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3389 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3390 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3391 6841da00 2019-08-08 stsp size_t linesize = 0;
3392 6841da00 2019-08-08 stsp ssize_t linelen;
3394 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3395 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3396 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3397 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3399 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3400 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3401 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3403 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3404 bd8de430 2019-10-04 stsp if (line[0] == '#')
3407 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3408 bd8de430 2019-10-04 stsp if (line[0] == '!')
3411 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3412 6841da00 2019-08-08 stsp line) == -1) {
3413 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3414 6841da00 2019-08-08 stsp goto done;
3416 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3418 6841da00 2019-08-08 stsp goto done;
3420 6841da00 2019-08-08 stsp if (ferror(f)) {
3421 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3422 6841da00 2019-08-08 stsp goto done;
3425 6841da00 2019-08-08 stsp dirpath = strdup(path);
3426 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3427 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3428 6841da00 2019-08-08 stsp goto done;
3430 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3432 6841da00 2019-08-08 stsp free(line);
3433 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3434 6841da00 2019-08-08 stsp free(dirpath);
3435 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3437 6841da00 2019-08-08 stsp return err;
3441 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3443 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3445 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3446 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3447 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3448 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3450 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3451 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3453 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3455 bd8de430 2019-10-04 stsp pattern += 3;
3457 bd8de430 2019-10-04 stsp while (*p) {
3458 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3459 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3460 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3461 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3463 bd8de430 2019-10-04 stsp while (*p == '/')
3473 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3474 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3475 6841da00 2019-08-08 stsp * ignores backwards.
3477 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3478 6841da00 2019-08-08 stsp while (pe) {
3479 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3480 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3481 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3482 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3483 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3484 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3485 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3486 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3487 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3492 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3498 6841da00 2019-08-08 stsp static const struct got_error *
3499 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3500 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3502 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3503 6841da00 2019-08-08 stsp char *ignorespath;
3504 886cec17 2019-12-15 stsp int fd = -1;
3505 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3507 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3508 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3509 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3511 886cec17 2019-12-15 stsp if (dirfd != -1) {
3512 886cec17 2019-12-15 stsp fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3513 886cec17 2019-12-15 stsp if (fd == -1) {
3514 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3515 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3516 886cec17 2019-12-15 stsp ignorespath);
3518 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3519 886cec17 2019-12-15 stsp if (ignoresfile == NULL)
3520 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3521 886cec17 2019-12-15 stsp ignorespath);
3524 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3528 886cec17 2019-12-15 stsp ignoresfile = fopen(ignorespath, "r");
3529 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3530 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3531 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3532 886cec17 2019-12-15 stsp ignorespath);
3534 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3537 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3538 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3539 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3540 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3541 6841da00 2019-08-08 stsp free(ignorespath);
3542 6841da00 2019-08-08 stsp return err;
3545 f8d1f275 2019-02-04 stsp static const struct got_error *
3546 7f91a133 2019-12-13 stsp status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3548 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3549 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3550 f8d1f275 2019-02-04 stsp char *path = NULL;
3552 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3553 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3555 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3556 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3557 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3559 f8d1f275 2019-02-04 stsp path = de->d_name;
3562 3143d852 2020-06-25 stsp if (de->d_type != DT_DIR &&
3563 3143d852 2020-06-25 stsp got_path_is_child(path, a->status_path, a->status_path_len)
3564 6841da00 2019-08-08 stsp && !match_ignores(&a->ignores, path))
3565 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3566 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3567 f8d1f275 2019-02-04 stsp if (parent_path[0])
3568 f8d1f275 2019-02-04 stsp free(path);
3569 b72f483a 2019-02-05 stsp return err;
3572 347d1d3e 2019-07-12 stsp static const struct got_error *
3573 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3575 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3576 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3578 3143d852 2020-06-25 stsp if (a->no_ignores)
3579 3143d852 2020-06-25 stsp return NULL;
3581 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path,
3582 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3584 3143d852 2020-06-25 stsp return err;
3586 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path, path,
3587 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3589 3143d852 2020-06-25 stsp return err;
3592 3143d852 2020-06-25 stsp static const struct got_error *
3593 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3594 abb4604f 2019-07-27 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3595 f2a9dc41 2019-12-13 tracey void *status_arg, struct got_repository *repo, int report_unchanged)
3597 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3598 abb4604f 2019-07-27 stsp struct stat sb;
3600 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3602 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3603 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3605 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3606 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3607 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3608 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3609 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3610 abb4604f 2019-07-27 stsp return NULL;
3613 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3614 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3615 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3617 abb4604f 2019-07-27 stsp return NULL;
3620 3143d852 2020-06-25 stsp static const struct got_error *
3621 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3622 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3624 3143d852 2020-06-25 stsp const struct got_error *err;
3625 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3627 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3628 3143d852 2020-06-25 stsp ".cvsignore");
3630 3143d852 2020-06-25 stsp return err;
3632 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3633 3143d852 2020-06-25 stsp ".gitignore");
3635 3143d852 2020-06-25 stsp return err;
3637 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3638 3143d852 2020-06-25 stsp if (err) {
3639 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3640 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3641 3143d852 2020-06-25 stsp return err;
3643 3143d852 2020-06-25 stsp for (;;) {
3644 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3645 3143d852 2020-06-25 stsp ".cvsignore");
3648 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3649 3143d852 2020-06-25 stsp ".gitignore");
3652 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3653 3143d852 2020-06-25 stsp if (err) {
3654 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3655 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3658 b737c85a 2020-06-26 stsp free(parent_path);
3659 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3660 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3663 b737c85a 2020-06-26 stsp free(parent_path);
3664 b737c85a 2020-06-26 stsp free(next_parent_path);
3665 3143d852 2020-06-25 stsp return err;
3668 abb4604f 2019-07-27 stsp static const struct got_error *
3669 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3670 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3671 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3672 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3673 f2a9dc41 2019-12-13 tracey int report_unchanged)
3675 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3676 6fc93f37 2019-12-13 stsp int fd = -1;
3677 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3678 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3679 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3681 3143d852 2020-06-25 stsp TAILQ_INIT(&arg.ignores);
3683 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3684 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3685 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3687 6fc93f37 2019-12-13 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3688 6fc93f37 2019-12-13 stsp if (fd == -1) {
3689 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3690 00bb5ea0 2020-07-23 stsp errno != ELOOP)
3691 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3693 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3694 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3695 f2a9dc41 2019-12-13 tracey report_unchanged);
3697 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3698 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3699 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3700 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3701 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3702 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3703 abb4604f 2019-07-27 stsp arg.status_path = path;
3704 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3705 abb4604f 2019-07-27 stsp arg.repo = repo;
3706 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3707 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3708 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3709 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3710 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3711 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3712 022fae89 2019-12-06 tracey if (!no_ignores) {
3713 3143d852 2020-06-25 stsp err = add_ignores_from_parent_paths(&arg.ignores,
3714 3143d852 2020-06-25 stsp worktree->root_path, path);
3716 3143d852 2020-06-25 stsp goto done;
3718 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3719 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3722 3143d852 2020-06-25 stsp free_ignores(&arg.ignores);
3723 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3724 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3725 927df6b7 2019-02-10 stsp free(ondisk_path);
3726 347d1d3e 2019-07-12 stsp return err;
3729 347d1d3e 2019-07-12 stsp const struct got_error *
3730 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3731 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3732 72ea6654 2019-07-27 stsp got_worktree_status_cb status_cb, void *status_arg,
3733 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3735 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3736 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3737 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3738 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3740 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3742 347d1d3e 2019-07-12 stsp return err;
3744 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3745 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3746 f2a9dc41 2019-12-13 tracey status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3750 f8d1f275 2019-02-04 stsp free(fileindex_path);
3751 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3752 f8d1f275 2019-02-04 stsp return err;
3755 6c7ab921 2019-03-18 stsp const struct got_error *
3756 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3757 6c7ab921 2019-03-18 stsp const char *arg)
3759 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3760 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3761 6c7ab921 2019-03-18 stsp size_t len;
3762 00bb5ea0 2020-07-23 stsp struct stat sb;
3763 01740607 2020-11-04 stsp char *abspath = NULL;
3764 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3766 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3768 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3769 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3770 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3772 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3773 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3774 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3775 00bb5ea0 2020-07-23 stsp goto done;
3777 727173c3 2020-11-06 stsp sb.st_mode = 0;
3779 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3781 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3782 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3783 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3784 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3786 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3787 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3788 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3789 00bb5ea0 2020-07-23 stsp goto done;
3793 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3794 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3796 d0710d08 2019-07-22 stsp goto done;
3797 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3798 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3799 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3800 00bb5ea0 2020-07-23 stsp goto done;
3803 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3804 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3805 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3806 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3807 00bb5ea0 2020-07-23 stsp goto done;
3809 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3810 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3811 01740607 2020-11-04 stsp goto done;
3813 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
3814 01740607 2020-11-04 stsp sizeof(canonpath));
3816 00bb5ea0 2020-07-23 stsp goto done;
3817 01740607 2020-11-04 stsp resolved = strdup(canonpath);
3818 01740607 2020-11-04 stsp if (resolved == NULL) {
3819 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
3820 01740607 2020-11-04 stsp goto done;
3825 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3826 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3827 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3828 6c7ab921 2019-03-18 stsp goto done;
3831 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3832 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3833 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3835 f6d88e1a 2019-05-29 stsp goto done;
3837 f6d88e1a 2019-05-29 stsp path = strdup("");
3838 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3839 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3840 f6d88e1a 2019-05-29 stsp goto done;
3844 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3845 6c7ab921 2019-03-18 stsp len = strlen(path);
3846 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3847 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3851 01740607 2020-11-04 stsp free(abspath);
3852 6c7ab921 2019-03-18 stsp free(resolved);
3853 d0710d08 2019-07-22 stsp free(cwd);
3854 6c7ab921 2019-03-18 stsp if (err == NULL)
3855 6c7ab921 2019-03-18 stsp *wt_path = path;
3857 6c7ab921 2019-03-18 stsp free(path);
3858 d00136be 2019-03-26 stsp return err;
3861 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3862 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3863 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3864 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3865 4e68cba3 2019-11-23 stsp void *progress_arg;
3866 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3869 4e68cba3 2019-11-23 stsp static const struct got_error *
3870 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3871 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3872 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3873 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3875 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3876 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3877 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3878 6d022e97 2019-08-04 stsp struct stat sb;
3879 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3881 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3882 dbb83fbd 2019-12-12 stsp relpath) == -1)
3883 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3885 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3887 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3888 12463d8b 2019-12-13 stsp de_name, a->repo);
3890 dbb83fbd 2019-12-12 stsp goto done;
3891 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3892 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3893 dbb83fbd 2019-12-12 stsp goto done;
3894 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3896 dbb83fbd 2019-12-12 stsp goto done;
3899 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3900 dbb83fbd 2019-12-12 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3901 dbb83fbd 2019-12-12 stsp goto done;
3904 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3906 4e68cba3 2019-11-23 stsp goto done;
3907 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3908 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
3909 3969253a 2020-03-07 stsp if (err) {
3910 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3911 3969253a 2020-03-07 stsp goto done;
3913 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3914 07f5b47a 2019-06-02 stsp if (err) {
3915 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
3916 4e68cba3 2019-11-23 stsp goto done;
3919 dbb83fbd 2019-12-12 stsp free(ondisk_path);
3921 dbb83fbd 2019-12-12 stsp return err;
3922 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
3923 dbb83fbd 2019-12-12 stsp return NULL;
3924 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3927 d00136be 2019-03-26 stsp const struct got_error *
3928 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
3929 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
3930 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3931 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
3933 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3934 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
3935 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3936 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
3937 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
3939 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3941 d00136be 2019-03-26 stsp return err;
3943 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3945 d00136be 2019-03-26 stsp goto done;
3947 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
3948 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
3949 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
3950 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
3951 4e68cba3 2019-11-23 stsp saa.repo = repo;
3953 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3954 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3955 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3959 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3960 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3961 af12c6b9 2019-06-04 stsp err = sync_err;
3963 fb399478 2019-07-12 stsp free(fileindex_path);
3964 d00136be 2019-03-26 stsp if (fileindex)
3965 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
3966 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3967 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
3968 d00136be 2019-03-26 stsp err = unlockerr;
3969 6c7ab921 2019-03-18 stsp return err;
3972 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
3973 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
3974 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
3975 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
3976 f2a9dc41 2019-12-13 tracey void *progress_arg;
3977 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
3978 f2a9dc41 2019-12-13 tracey int delete_local_mods;
3979 70e3e7f5 2019-12-13 tracey int keep_on_disk;
3980 766841c2 2020-08-13 stsp const char *status_codes;
3983 f2a9dc41 2019-12-13 tracey static const struct got_error *
3984 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
3985 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
3986 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3987 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
3989 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
3990 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
3991 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
3992 17ed4618 2019-06-02 stsp struct stat sb;
3993 20a2ad1c 2020-10-20 stsp char *ondisk_path;
3995 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3996 17ed4618 2019-06-02 stsp if (ie == NULL)
3997 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
3999 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4000 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4001 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4002 9acbc4fa 2019-08-03 stsp return NULL;
4003 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4006 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4007 f2a9dc41 2019-12-13 tracey relpath) == -1)
4008 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4010 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4013 f2a9dc41 2019-12-13 tracey goto done;
4015 766841c2 2020-08-13 stsp if (a->status_codes) {
4016 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4018 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4019 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4022 766841c2 2020-08-13 stsp if (i == ncodes) {
4023 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4024 766841c2 2020-08-13 stsp free(ondisk_path);
4025 766841c2 2020-08-13 stsp return NULL;
4027 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4028 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4029 766841c2 2020-08-13 stsp static char msg[64];
4030 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4031 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4032 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4033 766841c2 2020-08-13 stsp goto done;
4037 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4038 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4039 f2a9dc41 2019-12-13 tracey goto done;
4040 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4041 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4042 f2a9dc41 2019-12-13 tracey goto done;
4044 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4045 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4046 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4047 f2a9dc41 2019-12-13 tracey goto done;
4051 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4052 20a2ad1c 2020-10-20 stsp size_t root_len;
4054 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4055 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
4056 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4057 12463d8b 2019-12-13 stsp ondisk_path);
4058 12463d8b 2019-12-13 stsp goto done;
4060 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
4061 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4062 12463d8b 2019-12-13 stsp goto done;
4065 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4067 20a2ad1c 2020-10-20 stsp char *parent;
4068 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4070 2513f20a 2020-10-20 stsp goto done;
4071 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4072 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4073 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4074 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4075 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4076 20a2ad1c 2020-10-20 stsp ondisk_path);
4079 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4080 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4083 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4085 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4086 f2a9dc41 2019-12-13 tracey if (err)
4087 f2a9dc41 2019-12-13 tracey return err;
4088 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4089 f2a9dc41 2019-12-13 tracey return NULL;
4090 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4091 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4094 2ec1f75b 2019-03-26 stsp const struct got_error *
4095 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4096 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4097 766841c2 2020-08-13 stsp const char *status_codes,
4098 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4099 70e3e7f5 2019-12-13 tracey struct got_repository *repo, int keep_on_disk)
4101 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4102 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4103 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4104 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4105 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4107 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4109 2ec1f75b 2019-03-26 stsp return err;
4111 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4113 2ec1f75b 2019-03-26 stsp goto done;
4115 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4116 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4117 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4118 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4119 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4120 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4121 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4122 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4124 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4125 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4126 f2a9dc41 2019-12-13 tracey schedule_for_deletion, &sda, NULL, NULL, 0, 1);
4130 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4131 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4132 af12c6b9 2019-06-04 stsp err = sync_err;
4134 fb399478 2019-07-12 stsp free(fileindex_path);
4135 2ec1f75b 2019-03-26 stsp if (fileindex)
4136 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4137 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4138 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4139 2ec1f75b 2019-03-26 stsp err = unlockerr;
4140 33aa809d 2019-08-08 stsp return err;
4143 33aa809d 2019-08-08 stsp static const struct got_error *
4144 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4146 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4147 33aa809d 2019-08-08 stsp char *line = NULL;
4148 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4149 33aa809d 2019-08-08 stsp ssize_t linelen;
4151 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4152 33aa809d 2019-08-08 stsp if (linelen == -1) {
4153 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4154 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4155 33aa809d 2019-08-08 stsp goto done;
4157 33aa809d 2019-08-08 stsp return NULL;
4159 33aa809d 2019-08-08 stsp if (outfile) {
4160 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4161 33aa809d 2019-08-08 stsp if (n != linelen) {
4162 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4163 33aa809d 2019-08-08 stsp goto done;
4166 33aa809d 2019-08-08 stsp if (rejectfile) {
4167 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4168 33aa809d 2019-08-08 stsp if (n != linelen)
4169 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4172 33aa809d 2019-08-08 stsp free(line);
4173 2ec1f75b 2019-03-26 stsp return err;
4176 33aa809d 2019-08-08 stsp static const struct got_error *
4177 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4179 33aa809d 2019-08-08 stsp char *line = NULL;
4180 33aa809d 2019-08-08 stsp size_t linesize = 0;
4181 33aa809d 2019-08-08 stsp ssize_t linelen;
4183 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4184 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4185 500467ff 2019-09-25 hiltjo if (ferror(f))
4186 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4187 500467ff 2019-09-25 hiltjo return NULL;
4189 33aa809d 2019-08-08 stsp free(line);
4190 33aa809d 2019-08-08 stsp return NULL;
4193 33aa809d 2019-08-08 stsp static const struct got_error *
4194 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4195 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4196 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4198 33aa809d 2019-08-08 stsp const struct got_error *err;
4200 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4201 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4202 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4204 33aa809d 2019-08-08 stsp return err;
4205 33aa809d 2019-08-08 stsp (*line_cur1)++;
4207 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4208 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4209 33aa809d 2019-08-08 stsp if (rejectfile)
4210 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4212 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4214 33aa809d 2019-08-08 stsp return err;
4215 33aa809d 2019-08-08 stsp (*line_cur2)++;
4217 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4218 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4219 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4221 33aa809d 2019-08-08 stsp return err;
4222 33aa809d 2019-08-08 stsp (*line_cur2)++;
4224 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4225 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4226 33aa809d 2019-08-08 stsp if (rejectfile)
4227 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4229 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4231 33aa809d 2019-08-08 stsp return err;
4232 33aa809d 2019-08-08 stsp (*line_cur1)++;
4235 f1e81a05 2019-08-10 stsp return NULL;
4238 f1e81a05 2019-08-10 stsp static const struct got_error *
4239 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4240 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4242 f1e81a05 2019-08-10 stsp const struct got_error *err;
4244 f1e81a05 2019-08-10 stsp if (outfile) {
4245 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4246 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4247 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4249 f1e81a05 2019-08-10 stsp return err;
4250 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4253 f1e81a05 2019-08-10 stsp if (rejectfile) {
4254 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4255 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4256 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4258 f1e81a05 2019-08-10 stsp return err;
4259 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4263 33aa809d 2019-08-08 stsp return NULL;
4266 33aa809d 2019-08-08 stsp static const struct got_error *
4267 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4268 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4269 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4270 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4271 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4273 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4274 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4275 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4276 33aa809d 2019-08-08 stsp FILE *hunkfile;
4277 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4278 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4281 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4283 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4284 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4285 fe621944 2020-11-10 stsp start_old = cc.left.start;
4286 fe621944 2020-11-10 stsp end_old = cc.left.end;
4287 fe621944 2020-11-10 stsp start_new = cc.right.start;
4288 fe621944 2020-11-10 stsp end_new = cc.right.end;
4290 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4291 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4292 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4294 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4295 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4296 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4298 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4299 fe621944 2020-11-10 stsp if (diff_state == NULL)
4300 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4301 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4303 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4304 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4305 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4306 33aa809d 2019-08-08 stsp goto done;
4309 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4310 fe621944 2020-11-10 stsp diff_result, &cc);
4311 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4312 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4313 33aa809d 2019-08-08 stsp goto done;
4316 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4317 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4318 33aa809d 2019-08-08 stsp goto done;
4321 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4322 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4324 33aa809d 2019-08-08 stsp goto done;
4326 33aa809d 2019-08-08 stsp switch (*choice) {
4327 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4328 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4329 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4331 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4332 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4333 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4335 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4338 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4342 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4343 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4344 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4345 33aa809d 2019-08-08 stsp return err;
4348 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4349 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4350 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4351 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4352 1f1abb7e 2019-08-08 stsp void *progress_arg;
4353 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4354 33aa809d 2019-08-08 stsp void *patch_arg;
4355 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4358 e20a8b6f 2019-06-04 stsp static const struct got_error *
4359 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4360 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4361 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4362 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4363 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4365 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4366 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4367 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4368 1ebedb77 2019-10-19 stsp int fd2 = -1;
4369 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4370 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4371 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4372 fe621944 2020-11-10 stsp struct stat sb2;
4373 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4374 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4375 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4377 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4379 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4381 33aa809d 2019-08-08 stsp return err;
4383 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4384 12463d8b 2019-12-13 stsp fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4385 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4386 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4387 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4388 fa3cef63 2020-07-23 stsp goto done;
4390 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4391 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4392 fa3cef63 2020-07-23 stsp if (link_len == -1)
4393 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlinkat", path2);
4394 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4395 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4398 12463d8b 2019-12-13 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4399 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4400 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4401 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4402 fa3cef63 2020-07-23 stsp goto done;
4404 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4405 fa3cef63 2020-07-23 stsp sizeof(link_target));
4406 fa3cef63 2020-07-23 stsp if (link_len == -1)
4407 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4408 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4409 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4412 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4413 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4414 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4415 fa3cef63 2020-07-23 stsp goto done;
4418 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4419 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4420 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4421 fa3cef63 2020-07-23 stsp goto done;
4426 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4427 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4428 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4429 fa3cef63 2020-07-23 stsp goto done;
4431 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4432 fa3cef63 2020-07-23 stsp if (n != link_len) {
4433 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4434 fa3cef63 2020-07-23 stsp goto done;
4436 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4437 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4438 fa3cef63 2020-07-23 stsp goto done;
4440 fa3cef63 2020-07-23 stsp rewind(f2);
4443 33aa809d 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4445 33aa809d 2019-08-08 stsp goto done;
4447 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4449 33aa809d 2019-08-08 stsp goto done;
4451 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4453 33aa809d 2019-08-08 stsp goto done;
4455 64453f7e 2020-11-21 stsp err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4458 33aa809d 2019-08-08 stsp goto done;
4460 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4462 33aa809d 2019-08-08 stsp goto done;
4464 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4465 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4466 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4467 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4469 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4470 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4471 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4472 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4473 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4474 fe621944 2020-11-10 stsp nchanges++;
4476 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4477 33aa809d 2019-08-08 stsp int choice;
4478 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4479 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4480 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4481 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4482 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4483 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4485 33aa809d 2019-08-08 stsp goto done;
4486 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4487 33aa809d 2019-08-08 stsp have_content = 1;
4488 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4491 1ebedb77 2019-10-19 stsp if (have_content) {
4492 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4493 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4494 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4496 1ebedb77 2019-10-19 stsp goto done;
4498 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4499 3818e3c4 2020-11-01 naddy if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4500 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4501 fa3cef63 2020-07-23 stsp goto done;
4506 33aa809d 2019-08-08 stsp free(id_str);
4508 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4509 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4510 fe621944 2020-11-10 stsp if (err == NULL)
4511 fe621944 2020-11-10 stsp err = free_err;
4512 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4513 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4514 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4515 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4516 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4517 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4518 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4519 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4520 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4521 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4522 33aa809d 2019-08-08 stsp if (err || !have_content) {
4523 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4524 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4525 33aa809d 2019-08-08 stsp free(*path_outfile);
4526 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4528 33aa809d 2019-08-08 stsp free(path1);
4529 33aa809d 2019-08-08 stsp return err;
4532 33aa809d 2019-08-08 stsp static const struct got_error *
4533 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4534 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4535 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4536 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4538 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4539 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4540 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4541 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4542 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4543 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4544 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4545 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4546 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4547 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4549 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4550 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4551 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4552 d3bcc3d1 2019-08-08 stsp return NULL;
4554 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4555 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4556 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4558 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4559 65084dad 2019-08-08 stsp if (ie == NULL)
4560 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4562 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4563 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4564 a129376b 2019-03-28 stsp if (err) {
4565 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4566 a129376b 2019-03-28 stsp goto done;
4567 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4568 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4569 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4570 e20a8b6f 2019-06-04 stsp goto done;
4573 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4574 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4575 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4576 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4577 a129376b 2019-03-28 stsp goto done;
4580 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4581 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4582 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4583 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4584 a129376b 2019-03-28 stsp goto done;
4587 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4588 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4589 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4590 a129376b 2019-03-28 stsp goto done;
4595 1f1abb7e 2019-08-08 stsp err = got_object_id_by_path(&tree_id, a->repo,
4596 1f1abb7e 2019-08-08 stsp a->worktree->base_commit_id, tree_path);
4597 a9fa2909 2019-07-27 stsp if (err) {
4598 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4599 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4600 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4601 a9fa2909 2019-07-27 stsp goto done;
4603 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4605 a9fa2909 2019-07-27 stsp goto done;
4607 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4609 a9fa2909 2019-07-27 stsp goto done;
4611 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4612 1233e6b6 2020-10-19 stsp free(te_name);
4613 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4614 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4615 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4616 a9fa2909 2019-07-27 stsp goto done;
4620 a129376b 2019-03-28 stsp switch (status) {
4621 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4622 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4623 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4624 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4625 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4627 33aa809d 2019-08-08 stsp goto done;
4628 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4631 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4632 1f1abb7e 2019-08-08 stsp ie->path);
4634 1ee397ad 2019-07-12 stsp goto done;
4635 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4637 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4638 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4639 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4640 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4641 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4643 33aa809d 2019-08-08 stsp goto done;
4644 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4647 33aa809d 2019-08-08 stsp /* fall through */
4648 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4649 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4650 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4651 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4652 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4653 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4654 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4655 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4656 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4658 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4659 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4660 1f1abb7e 2019-08-08 stsp err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4662 65084dad 2019-08-08 stsp goto done;
4664 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4665 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4666 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4667 a129376b 2019-03-28 stsp goto done;
4670 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4671 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4672 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4673 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4674 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4675 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4676 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4678 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4679 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4680 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4681 369fd7e5 2020-07-23 stsp path_content);
4684 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4685 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4686 369fd7e5 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4687 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4689 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4690 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4691 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4692 369fd7e5 2020-07-23 stsp goto done;
4696 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4697 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4698 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4699 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4700 c90c8ce3 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4701 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4703 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4705 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4706 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4707 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4708 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4711 a129376b 2019-03-28 stsp goto done;
4712 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4713 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4714 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4715 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
4716 437adc9d 2020-12-10 yzhong blob->id.sha1,
4717 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4719 2e1fa222 2020-07-23 stsp goto done;
4721 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4722 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4723 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4732 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4733 33aa809d 2019-08-08 stsp free(path_content);
4734 e20a8b6f 2019-06-04 stsp free(parent_path);
4735 a129376b 2019-03-28 stsp free(tree_path);
4737 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4739 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4740 a129376b 2019-03-28 stsp free(tree_id);
4741 e20a8b6f 2019-06-04 stsp return err;
4744 e20a8b6f 2019-06-04 stsp const struct got_error *
4745 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4746 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4747 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4748 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4749 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4751 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4752 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4753 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4754 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4755 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4756 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4758 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4760 e20a8b6f 2019-06-04 stsp return err;
4762 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4764 e20a8b6f 2019-06-04 stsp goto done;
4766 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4767 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4768 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4769 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4770 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4771 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4772 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4773 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4774 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4775 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
4779 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4780 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4781 e20a8b6f 2019-06-04 stsp err = sync_err;
4783 fb399478 2019-07-12 stsp free(fileindex_path);
4784 a129376b 2019-03-28 stsp if (fileindex)
4785 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4786 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4787 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4788 a129376b 2019-03-28 stsp err = unlockerr;
4789 c4296144 2019-05-09 stsp return err;
4792 cf066bf8 2019-05-09 stsp static void
4793 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4795 24519714 2019-05-09 stsp free(ct->path);
4796 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4797 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4798 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4799 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4800 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4801 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4805 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4806 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4807 24519714 2019-05-09 stsp struct got_repository *repo;
4808 24519714 2019-05-09 stsp struct got_worktree *worktree;
4809 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4810 5f8a88c6 2019-08-03 stsp int have_staged_files;
4811 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4814 c4296144 2019-05-09 stsp static const struct got_error *
4815 88d0e355 2019-08-03 stsp collect_commitables(void *arg, unsigned char status,
4816 88d0e355 2019-08-03 stsp unsigned char staged_status, const char *relpath,
4817 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4818 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4820 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
4821 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4822 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4823 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4824 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4825 768aea60 2019-05-09 stsp struct stat sb;
4827 5f8a88c6 2019-08-03 stsp if (a->have_staged_files) {
4828 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4829 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4830 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4831 5f8a88c6 2019-08-03 stsp return NULL;
4833 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4834 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4836 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4837 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4838 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4839 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4840 5f8a88c6 2019-08-03 stsp return NULL;
4843 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4844 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4845 036813ee 2019-05-09 stsp goto done;
4847 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4848 036813ee 2019-05-09 stsp parent_path = strdup("");
4849 69960a46 2019-05-09 stsp if (parent_path == NULL)
4850 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4852 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4854 69960a46 2019-05-09 stsp return err;
4857 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4858 cf066bf8 2019-05-09 stsp if (ct == NULL) {
4859 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4860 c4296144 2019-05-09 stsp goto done;
4863 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4864 768aea60 2019-05-09 stsp relpath) == -1) {
4865 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4866 768aea60 2019-05-09 stsp goto done;
4869 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
4870 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
4871 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
4872 0aeb8099 2020-07-23 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4873 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
4874 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
4875 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
4876 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
4878 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
4879 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
4882 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4883 0aeb8099 2020-07-23 stsp goto done;
4885 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
4886 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
4887 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
4888 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4889 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
4890 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
4891 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
4892 12463d8b 2019-12-13 stsp ct->ondisk_path);
4893 12463d8b 2019-12-13 stsp goto done;
4895 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
4896 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
4897 768aea60 2019-05-09 stsp goto done;
4899 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
4902 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4903 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4904 44d03001 2019-05-09 stsp relpath) == -1) {
4905 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4906 44d03001 2019-05-09 stsp goto done;
4909 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4910 35213c7c 2020-07-23 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4911 35213c7c 2020-07-23 stsp int is_bad_symlink;
4912 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
4913 35213c7c 2020-07-23 stsp ssize_t target_len;
4914 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
4915 35213c7c 2020-07-23 stsp sizeof(target_path));
4916 35213c7c 2020-07-23 stsp if (target_len == -1) {
4917 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
4918 35213c7c 2020-07-23 stsp ct->ondisk_path);
4919 35213c7c 2020-07-23 stsp goto done;
4921 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
4922 35213c7c 2020-07-23 stsp target_len, ct->ondisk_path, a->worktree->root_path);
4924 35213c7c 2020-07-23 stsp goto done;
4925 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
4926 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
4927 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
4928 35213c7c 2020-07-23 stsp goto done;
4933 cf066bf8 2019-05-09 stsp ct->status = status;
4934 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
4935 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
4936 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
4937 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
4938 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
4939 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
4940 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4941 b416585c 2019-05-13 stsp goto done;
4943 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
4944 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
4945 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
4946 f0b75401 2019-08-03 stsp goto done;
4949 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
4950 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
4951 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4952 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
4953 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4954 036813ee 2019-05-09 stsp goto done;
4957 24519714 2019-05-09 stsp ct->path = strdup(path);
4958 24519714 2019-05-09 stsp if (ct->path == NULL) {
4959 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4960 24519714 2019-05-09 stsp goto done;
4962 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4964 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
4965 ed175427 2019-05-09 stsp free_commitable(ct);
4966 24519714 2019-05-09 stsp free(parent_path);
4967 036813ee 2019-05-09 stsp free(path);
4968 a129376b 2019-03-28 stsp return err;
4971 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
4972 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
4973 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4974 036813ee 2019-05-09 stsp struct got_repository *);
4976 ed175427 2019-05-09 stsp static const struct got_error *
4977 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4978 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
4979 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
4980 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4981 afa376bf 2019-05-09 stsp struct got_repository *repo)
4983 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
4984 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
4985 036813ee 2019-05-09 stsp char *subpath;
4987 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
4988 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4989 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4991 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
4993 ed175427 2019-05-09 stsp return err;
4995 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
4996 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
4997 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
4998 036813ee 2019-05-09 stsp free(subpath);
4999 036813ee 2019-05-09 stsp return err;
5002 036813ee 2019-05-09 stsp static const struct got_error *
5003 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5005 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5006 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5008 036813ee 2019-05-09 stsp *match = 0;
5010 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5011 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5012 0f63689d 2019-05-10 stsp return NULL;
5015 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5017 0f63689d 2019-05-10 stsp return err;
5018 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5019 036813ee 2019-05-09 stsp free(ct_parent_path);
5020 036813ee 2019-05-09 stsp return err;
5023 768aea60 2019-05-09 stsp static mode_t
5024 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5026 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5027 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5029 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5032 036813ee 2019-05-09 stsp static const struct got_error *
5033 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5034 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5036 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5038 036813ee 2019-05-09 stsp *new_te = NULL;
5040 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5042 036813ee 2019-05-09 stsp goto done;
5044 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5046 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5047 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5048 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5050 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5052 036813ee 2019-05-09 stsp if (err && *new_te) {
5053 56e0773d 2019-11-28 stsp free(*new_te);
5054 036813ee 2019-05-09 stsp *new_te = NULL;
5056 036813ee 2019-05-09 stsp return err;
5059 036813ee 2019-05-09 stsp static const struct got_error *
5060 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5061 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5063 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5064 102b254e 2020-10-19 stsp char *ct_name = NULL;
5066 036813ee 2019-05-09 stsp *new_te = NULL;
5068 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5069 036813ee 2019-05-09 stsp if (*new_te == NULL)
5070 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5072 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5074 036813ee 2019-05-09 stsp goto done;
5075 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5076 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5077 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5078 036813ee 2019-05-09 stsp goto done;
5081 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5083 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5084 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5085 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5087 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5089 102b254e 2020-10-19 stsp free(ct_name);
5090 036813ee 2019-05-09 stsp if (err && *new_te) {
5091 56e0773d 2019-11-28 stsp free(*new_te);
5092 036813ee 2019-05-09 stsp *new_te = NULL;
5094 036813ee 2019-05-09 stsp return err;
5097 036813ee 2019-05-09 stsp static const struct got_error *
5098 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5099 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5101 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5102 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5104 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5106 036813ee 2019-05-09 stsp return err;
5107 036813ee 2019-05-09 stsp if (new_pe == NULL)
5108 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5109 036813ee 2019-05-09 stsp return NULL;
5112 afa376bf 2019-05-09 stsp static const struct got_error *
5113 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5114 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5116 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5117 5f8a88c6 2019-08-03 stsp unsigned char status;
5119 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5120 afa376bf 2019-05-09 stsp ct_path++;
5122 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5123 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5125 5f8a88c6 2019-08-03 stsp status = ct->status;
5127 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5128 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5131 036813ee 2019-05-09 stsp static const struct got_error *
5132 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5133 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5135 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5136 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5137 44d03001 2019-05-09 stsp char *te_path;
5139 44d03001 2019-05-09 stsp *modified = 0;
5141 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5142 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5143 44d03001 2019-05-09 stsp te->name) == -1)
5144 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5146 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5147 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5148 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5149 44d03001 2019-05-09 stsp strlen(te_path));
5150 62d463ca 2020-10-20 naddy if (*modified)
5154 44d03001 2019-05-09 stsp free(te_path);
5155 44d03001 2019-05-09 stsp return err;
5158 44d03001 2019-05-09 stsp static const struct got_error *
5159 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5160 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5161 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5163 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5164 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5166 036813ee 2019-05-09 stsp *ctp = NULL;
5168 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5169 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5170 036813ee 2019-05-09 stsp char *ct_name = NULL;
5171 036813ee 2019-05-09 stsp int path_matches;
5173 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5174 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5175 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5176 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
5179 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5180 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5184 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5187 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5189 036813ee 2019-05-09 stsp return err;
5190 036813ee 2019-05-09 stsp if (!path_matches)
5193 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5195 d34b633e 2020-10-19 stsp return err;
5197 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5198 d34b633e 2020-10-19 stsp free(ct_name);
5201 d34b633e 2020-10-19 stsp free(ct_name);
5203 036813ee 2019-05-09 stsp *ctp = ct;
5207 2b496619 2019-07-10 stsp return err;
5210 2b496619 2019-07-10 stsp static const struct got_error *
5211 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5212 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5213 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5214 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5215 2b496619 2019-07-10 stsp struct got_repository *repo)
5217 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5218 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5219 2b496619 2019-07-10 stsp char *subtree_path;
5220 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5221 ba580f68 2020-03-22 stsp int nentries;
5223 2b496619 2019-07-10 stsp *new_tep = NULL;
5225 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5226 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5227 2b496619 2019-07-10 stsp child_path) == -1)
5228 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5230 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5231 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5232 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5233 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5235 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5236 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5237 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5238 2b496619 2019-07-10 stsp goto done;
5240 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5241 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5242 2b496619 2019-07-10 stsp if (err) {
5243 56e0773d 2019-11-28 stsp free(new_te);
5244 2b496619 2019-07-10 stsp goto done;
5246 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5249 2b496619 2019-07-10 stsp free(subtree_path);
5250 2b496619 2019-07-10 stsp if (err == NULL)
5251 2b496619 2019-07-10 stsp *new_tep = new_te;
5252 036813ee 2019-05-09 stsp return err;
5255 036813ee 2019-05-09 stsp static const struct got_error *
5256 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5257 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5258 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5259 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5260 036813ee 2019-05-09 stsp struct got_repository *repo)
5262 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5263 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5264 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5265 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5267 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5268 ba580f68 2020-03-22 stsp *nentries = 0;
5270 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5271 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5272 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5273 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5275 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5276 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5277 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5280 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5281 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5284 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5285 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5287 036813ee 2019-05-09 stsp goto done;
5289 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5290 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5291 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5293 036813ee 2019-05-09 stsp goto done;
5294 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5296 afa376bf 2019-05-09 stsp goto done;
5297 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5298 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5300 2f51b5b3 2019-05-09 stsp goto done;
5301 ba580f68 2020-03-22 stsp (*nentries)++;
5303 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5304 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5305 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5306 2b496619 2019-07-10 stsp == NULL) {
5307 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5308 2b496619 2019-07-10 stsp child_path, path_base_tree,
5309 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5312 2b496619 2019-07-10 stsp goto done;
5313 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5315 2b496619 2019-07-10 stsp goto done;
5316 ba580f68 2020-03-22 stsp (*nentries)++;
5321 2f51b5b3 2019-05-09 stsp if (base_tree) {
5322 56e0773d 2019-11-28 stsp int i, nbase_entries;
5323 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5324 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5325 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5326 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5328 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5329 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5330 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5331 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5333 63c5ca5d 2019-08-24 stsp goto done;
5334 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5336 63c5ca5d 2019-08-24 stsp goto done;
5337 ba580f68 2020-03-22 stsp (*nentries)++;
5341 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5342 44d03001 2019-05-09 stsp int modified;
5343 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5345 036813ee 2019-05-09 stsp goto done;
5346 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5347 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5349 2f51b5b3 2019-05-09 stsp goto done;
5350 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5351 44d03001 2019-05-09 stsp if (modified) {
5352 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5353 ba580f68 2020-03-22 stsp int nsubentries;
5354 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5355 ba580f68 2020-03-22 stsp &nsubentries, te,
5356 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5357 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5359 44d03001 2019-05-09 stsp goto done;
5360 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5361 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5362 ba580f68 2020-03-22 stsp free(new_id);
5365 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5366 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5367 56e0773d 2019-11-28 stsp free(new_id);
5369 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5371 036813ee 2019-05-09 stsp goto done;
5372 ba580f68 2020-03-22 stsp (*nentries)++;
5376 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5377 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5379 f66c734c 2019-09-22 stsp goto done;
5381 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5382 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5383 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5384 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5385 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5386 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5388 2f51b5b3 2019-05-09 stsp goto done;
5389 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5391 2f51b5b3 2019-05-09 stsp goto done;
5392 ba580f68 2020-03-22 stsp (*nentries)++;
5394 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5395 b416585c 2019-05-13 stsp status_arg);
5397 afa376bf 2019-05-09 stsp goto done;
5399 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5400 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5402 2f51b5b3 2019-05-09 stsp goto done;
5403 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5405 2f51b5b3 2019-05-09 stsp goto done;
5406 ba580f68 2020-03-22 stsp (*nentries)++;
5411 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5412 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5414 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
5415 2e1fa222 2020-07-23 stsp return err;
5418 2e1fa222 2020-07-23 stsp static const struct got_error *
5419 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5420 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5421 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5422 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5424 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5425 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5426 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5428 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5429 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5430 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5432 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5434 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5435 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5436 437adc9d 2020-12-10 yzhong if (err)
5437 437adc9d 2020-12-10 yzhong goto done;
5440 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5441 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5442 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5443 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5444 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5445 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5446 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5447 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5449 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5450 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5451 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
5452 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5453 72fd46fa 2019-09-06 stsp !have_staged_files);
5455 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5456 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5457 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
5458 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5459 72fd46fa 2019-09-06 stsp !have_staged_files);
5461 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5463 437adc9d 2020-12-10 yzhong goto done;
5464 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5465 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
5466 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
5467 3969253a 2020-03-07 stsp if (err) {
5468 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5469 437adc9d 2020-12-10 yzhong goto done;
5471 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5472 3969253a 2020-03-07 stsp if (err) {
5473 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5474 437adc9d 2020-12-10 yzhong goto done;
5477 437adc9d 2020-12-10 yzhong free(relpath);
5478 437adc9d 2020-12-10 yzhong relpath = NULL;
5481 437adc9d 2020-12-10 yzhong free(relpath);
5482 d56d26ce 2019-05-10 stsp return err;
5486 d56d26ce 2019-05-10 stsp static const struct got_error *
5487 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5488 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5489 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5490 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5491 735ef5ac 2019-08-03 stsp int ood_errcode)
5493 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5494 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5496 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5497 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5498 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5499 1a36436d 2019-06-10 stsp return NULL;
5501 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5502 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5504 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5505 735ef5ac 2019-08-03 stsp in_repo_path);
5506 9bead371 2019-07-28 stsp if (err) {
5507 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5508 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5509 1a36436d 2019-06-10 stsp goto done;
5510 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5511 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5513 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5514 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5515 735ef5ac 2019-08-03 stsp in_repo_path);
5516 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5517 1a36436d 2019-06-10 stsp goto done;
5518 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5522 1a36436d 2019-06-10 stsp return err;
5525 c4296144 2019-05-09 stsp const struct got_error *
5526 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5527 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5528 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id, struct got_worktree *worktree,
5529 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
5530 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5531 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5532 afa376bf 2019-05-09 stsp struct got_repository *repo)
5534 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5535 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5536 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5537 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5538 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5539 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5540 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5541 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5542 ba580f68 2020-03-22 stsp int nentries;
5543 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5544 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5545 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5547 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5549 de18fc63 2019-05-09 stsp SIMPLEQ_INIT(&parent_ids);
5551 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5553 588edf97 2019-05-10 stsp goto done;
5555 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5557 588edf97 2019-05-10 stsp goto done;
5559 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5560 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5565 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5566 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5570 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5571 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5572 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5573 cf066bf8 2019-05-09 stsp char *ondisk_path;
5575 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5576 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5577 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5580 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5581 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5582 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5585 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5586 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5587 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5588 cf066bf8 2019-05-09 stsp goto done;
5590 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5591 2e1fa222 2020-07-23 stsp free(ondisk_path);
5593 cf066bf8 2019-05-09 stsp goto done;
5596 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5597 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5598 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5600 036813ee 2019-05-09 stsp goto done;
5602 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5604 de18fc63 2019-05-09 stsp goto done;
5605 de18fc63 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5606 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5607 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5608 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
5609 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5610 33ad4cbe 2019-05-12 jcs free(logmsg);
5612 9d40349a 2019-05-09 stsp goto done;
5614 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5615 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5616 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5617 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5618 09f5bd90 2019-05-09 stsp goto done;
5620 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
5621 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5623 09f5bd90 2019-05-09 stsp goto done;
5624 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5626 ebf99748 2019-05-09 stsp goto done;
5627 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5628 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5629 09f5bd90 2019-05-09 stsp goto done;
5631 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
5632 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
5634 f2c16586 2019-05-09 stsp goto done;
5635 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
5637 f2c16586 2019-05-09 stsp goto done;
5639 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5641 09f5bd90 2019-05-09 stsp goto done;
5643 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
5645 ebf99748 2019-05-09 stsp goto done;
5647 588edf97 2019-05-10 stsp if (head_tree)
5648 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
5649 588edf97 2019-05-10 stsp if (head_commit)
5650 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
5651 09f5bd90 2019-05-09 stsp free(head_commit_id2);
5652 2f17228e 2019-05-12 stsp if (head_ref2) {
5653 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
5654 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
5655 2f17228e 2019-05-12 stsp err = unlockerr;
5656 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
5658 39cd0ff6 2019-07-12 stsp return err;
5661 5c1e53bc 2019-07-28 stsp static const struct got_error *
5662 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
5663 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
5665 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
5666 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
5668 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
5669 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
5670 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
5672 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
5673 5c1e53bc 2019-07-28 stsp ct_path++;
5675 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
5676 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
5680 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
5681 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
5683 5c1e53bc 2019-07-28 stsp return NULL;
5686 f0b75401 2019-08-03 stsp static const struct got_error *
5687 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
5689 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
5691 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5692 f0b75401 2019-08-03 stsp *have_staged_files = 1;
5693 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
5696 f0b75401 2019-08-03 stsp return NULL;
5699 5f8a88c6 2019-08-03 stsp static const struct got_error *
5700 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
5701 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
5703 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
5704 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
5706 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5707 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
5709 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5710 5f8a88c6 2019-08-03 stsp if (ie == NULL)
5711 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5712 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5713 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
5714 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
5717 5f8a88c6 2019-08-03 stsp return NULL;
5720 39cd0ff6 2019-07-12 stsp const struct got_error *
5721 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
5722 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
5723 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
5724 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5725 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
5726 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
5728 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5729 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
5730 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
5731 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5732 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5733 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
5734 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
5735 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5736 f0b75401 2019-08-03 stsp int have_staged_files = 0;
5738 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
5740 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5742 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
5744 39cd0ff6 2019-07-12 stsp goto done;
5746 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5748 39cd0ff6 2019-07-12 stsp goto done;
5750 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5752 39cd0ff6 2019-07-12 stsp goto done;
5754 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5756 39cd0ff6 2019-07-12 stsp goto done;
5758 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5759 5f8a88c6 2019-08-03 stsp &have_staged_files);
5760 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
5761 5f8a88c6 2019-08-03 stsp goto done;
5762 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
5763 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
5765 5f8a88c6 2019-08-03 stsp goto done;
5768 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5769 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
5770 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
5771 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
5772 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
5773 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5774 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5775 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5776 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5778 5c1e53bc 2019-07-28 stsp goto done;
5781 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5782 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5783 39cd0ff6 2019-07-12 stsp goto done;
5786 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5787 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
5789 5c1e53bc 2019-07-28 stsp goto done;
5792 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5793 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5794 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5796 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5797 f0b75401 2019-08-03 stsp ct_path++;
5798 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5799 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5800 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5802 b50cabdf 2019-07-12 stsp goto done;
5806 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5807 5c1e53bc 2019-07-28 stsp head_commit_id, worktree, author, committer,
5808 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5810 39cd0ff6 2019-07-12 stsp goto done;
5812 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
5813 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
5814 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5815 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5816 39cd0ff6 2019-07-12 stsp err = sync_err;
5818 39cd0ff6 2019-07-12 stsp if (fileindex)
5819 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5820 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5821 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5822 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5823 39cd0ff6 2019-07-12 stsp err = unlockerr;
5824 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5825 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5826 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5828 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5829 c4296144 2019-05-09 stsp return err;
5832 8656d6c4 2019-05-20 stsp const char *
5833 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5835 8656d6c4 2019-05-20 stsp return ct->path;
5838 8656d6c4 2019-05-20 stsp unsigned int
5839 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
5841 8656d6c4 2019-05-20 stsp return ct->status;
5844 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
5845 818c7501 2019-07-11 stsp struct got_worktree *worktree;
5846 818c7501 2019-07-11 stsp struct got_repository *repo;
5849 818c7501 2019-07-11 stsp static const struct got_error *
5850 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5852 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5853 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
5854 818c7501 2019-07-11 stsp unsigned char status;
5855 818c7501 2019-07-11 stsp struct stat sb;
5856 818c7501 2019-07-11 stsp char *ondisk_path;
5858 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
5859 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5860 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
5861 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
5863 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5865 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
5867 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
5868 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5869 818c7501 2019-07-11 stsp free(ondisk_path);
5871 818c7501 2019-07-11 stsp return err;
5873 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
5874 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
5875 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5876 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5878 818c7501 2019-07-11 stsp return NULL;
5881 818c7501 2019-07-11 stsp const struct got_error *
5882 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5883 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5884 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
5885 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5887 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5888 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5889 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
5890 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
5891 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
5892 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5893 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
5895 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5896 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5897 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5899 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
5901 818c7501 2019-07-11 stsp return err;
5903 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5905 818c7501 2019-07-11 stsp goto done;
5907 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
5908 818c7501 2019-07-11 stsp ok_arg.repo = repo;
5909 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5912 818c7501 2019-07-11 stsp goto done;
5914 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5916 818c7501 2019-07-11 stsp goto done;
5918 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5920 818c7501 2019-07-11 stsp goto done;
5922 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5924 818c7501 2019-07-11 stsp goto done;
5926 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5929 e51d7b55 2020-01-04 stsp goto done;
5931 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5933 818c7501 2019-07-11 stsp goto done;
5934 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5935 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5936 e51d7b55 2020-01-04 stsp goto done;
5939 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
5940 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
5942 818c7501 2019-07-11 stsp goto done;
5943 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
5945 818c7501 2019-07-11 stsp goto done;
5947 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
5949 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5951 818c7501 2019-07-11 stsp goto done;
5953 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
5955 818c7501 2019-07-11 stsp goto done;
5957 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
5958 818c7501 2019-07-11 stsp worktree->base_commit_id);
5960 818c7501 2019-07-11 stsp goto done;
5961 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
5963 818c7501 2019-07-11 stsp goto done;
5965 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
5967 818c7501 2019-07-11 stsp goto done;
5969 818c7501 2019-07-11 stsp free(fileindex_path);
5970 818c7501 2019-07-11 stsp free(tmp_branch_name);
5971 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
5972 818c7501 2019-07-11 stsp free(branch_ref_name);
5973 818c7501 2019-07-11 stsp if (branch_ref)
5974 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5975 818c7501 2019-07-11 stsp if (wt_branch)
5976 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
5977 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
5978 818c7501 2019-07-11 stsp if (err) {
5979 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
5980 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
5981 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5983 818c7501 2019-07-11 stsp if (*tmp_branch) {
5984 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5985 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5987 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5988 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5989 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5991 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
5993 818c7501 2019-07-11 stsp return err;
5996 818c7501 2019-07-11 stsp const struct got_error *
5997 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
5998 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5999 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6000 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6002 818c7501 2019-07-11 stsp const struct got_error *err;
6003 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6004 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6005 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6006 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6007 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6009 818c7501 2019-07-11 stsp *commit_id = NULL;
6010 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6011 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6012 3e3a69f1 2019-07-25 stsp *branch = NULL;
6013 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6015 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6017 3e3a69f1 2019-07-25 stsp return err;
6019 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6021 f032f1f7 2019-08-04 stsp goto done;
6023 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6024 f032f1f7 2019-08-04 stsp &have_staged_files);
6025 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6026 f032f1f7 2019-08-04 stsp goto done;
6027 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6028 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6029 3e3a69f1 2019-07-25 stsp goto done;
6032 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6034 f032f1f7 2019-08-04 stsp goto done;
6036 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6038 818c7501 2019-07-11 stsp goto done;
6040 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6042 818c7501 2019-07-11 stsp goto done;
6044 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6046 818c7501 2019-07-11 stsp goto done;
6048 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6050 818c7501 2019-07-11 stsp goto done;
6052 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6053 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6055 818c7501 2019-07-11 stsp goto done;
6057 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6059 818c7501 2019-07-11 stsp goto done;
6061 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6063 818c7501 2019-07-11 stsp goto done;
6065 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6066 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6068 818c7501 2019-07-11 stsp goto done;
6070 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6072 818c7501 2019-07-11 stsp goto done;
6074 818c7501 2019-07-11 stsp free(commit_ref_name);
6075 818c7501 2019-07-11 stsp free(branch_ref_name);
6076 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6077 818c7501 2019-07-11 stsp if (commit_ref)
6078 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6079 818c7501 2019-07-11 stsp if (branch_ref)
6080 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6081 818c7501 2019-07-11 stsp if (err) {
6082 818c7501 2019-07-11 stsp free(*commit_id);
6083 818c7501 2019-07-11 stsp *commit_id = NULL;
6084 818c7501 2019-07-11 stsp if (*tmp_branch) {
6085 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6086 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6088 818c7501 2019-07-11 stsp if (*new_base_branch) {
6089 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6090 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6092 818c7501 2019-07-11 stsp if (*branch) {
6093 818c7501 2019-07-11 stsp got_ref_close(*branch);
6094 818c7501 2019-07-11 stsp *branch = NULL;
6096 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6097 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6098 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6100 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6102 818c7501 2019-07-11 stsp return err;
6105 818c7501 2019-07-11 stsp const struct got_error *
6106 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6108 818c7501 2019-07-11 stsp const struct got_error *err;
6109 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6111 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6113 818c7501 2019-07-11 stsp return err;
6115 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6116 818c7501 2019-07-11 stsp free(tmp_branch_name);
6117 818c7501 2019-07-11 stsp return NULL;
6120 818c7501 2019-07-11 stsp static const struct got_error *
6121 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6122 818c7501 2019-07-11 stsp char **logmsg, void *arg)
6124 0ebf8283 2019-07-24 stsp *logmsg = arg;
6125 818c7501 2019-07-11 stsp return NULL;
6128 818c7501 2019-07-11 stsp static const struct got_error *
6129 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6130 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6131 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6132 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6134 818c7501 2019-07-11 stsp return NULL;
6137 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6138 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6139 01757395 2019-07-12 stsp void *progress_arg;
6140 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6143 01757395 2019-07-12 stsp static const struct got_error *
6144 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6146 01757395 2019-07-12 stsp const struct got_error *err;
6147 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6149 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6151 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6153 01757395 2019-07-12 stsp return err;
6155 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6156 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6157 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6158 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6159 01757395 2019-07-12 stsp return NULL;
6161 01757395 2019-07-12 stsp p = strdup(path);
6162 01757395 2019-07-12 stsp if (p == NULL)
6163 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6165 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6166 01757395 2019-07-12 stsp if (err || new == NULL)
6168 01757395 2019-07-12 stsp return err;
6172 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6174 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6176 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
6177 01757395 2019-07-12 stsp free((char *)pe->path);
6179 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
6182 0ebf8283 2019-07-24 stsp static const struct got_error *
6183 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6184 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6186 818c7501 2019-07-11 stsp const struct got_error *err;
6187 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6189 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6190 818c7501 2019-07-11 stsp if (err) {
6191 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6192 818c7501 2019-07-11 stsp goto done;
6193 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6195 818c7501 2019-07-11 stsp goto done;
6196 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6198 818c7501 2019-07-11 stsp goto done;
6199 de05890f 2020-03-05 stsp } else if (is_rebase) {
6200 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6203 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6205 818c7501 2019-07-11 stsp goto done;
6206 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6207 818c7501 2019-07-11 stsp free(stored_id);
6208 818c7501 2019-07-11 stsp if (cmp != 0) {
6209 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6210 818c7501 2019-07-11 stsp goto done;
6214 0ebf8283 2019-07-24 stsp if (commit_ref)
6215 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6216 0ebf8283 2019-07-24 stsp return err;
6219 0ebf8283 2019-07-24 stsp static const struct got_error *
6220 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6221 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6222 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6223 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6224 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6225 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6227 0ebf8283 2019-07-24 stsp const struct got_error *err;
6228 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6229 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6230 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6232 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6234 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6236 0ebf8283 2019-07-24 stsp return err;
6238 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6239 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6240 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6241 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6242 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6243 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6244 818c7501 2019-07-11 stsp if (commit_ref)
6245 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6246 818c7501 2019-07-11 stsp return err;
6249 818c7501 2019-07-11 stsp const struct got_error *
6250 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6251 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6252 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6253 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6254 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6255 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6257 0ebf8283 2019-07-24 stsp const struct got_error *err;
6258 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6260 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6262 0ebf8283 2019-07-24 stsp return err;
6264 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6266 0ebf8283 2019-07-24 stsp goto done;
6268 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6269 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6270 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6272 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6273 0ebf8283 2019-07-24 stsp return err;
6276 0ebf8283 2019-07-24 stsp const struct got_error *
6277 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6278 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6279 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6280 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6281 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6282 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6284 0ebf8283 2019-07-24 stsp const struct got_error *err;
6285 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6287 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6289 0ebf8283 2019-07-24 stsp return err;
6291 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6293 0ebf8283 2019-07-24 stsp goto done;
6295 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6296 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6297 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6299 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6300 0ebf8283 2019-07-24 stsp return err;
6303 0ebf8283 2019-07-24 stsp static const struct got_error *
6304 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6305 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6306 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6307 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6308 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
6310 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6311 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6312 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6313 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6314 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6315 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6316 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6318 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6319 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6321 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6323 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6325 3e3a69f1 2019-07-25 stsp return err;
6327 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6328 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6329 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6330 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6332 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6333 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6334 01757395 2019-07-12 stsp * TODO: Ideally, merged_paths would contain a list of commitables
6335 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6337 01757395 2019-07-12 stsp if (merged_paths) {
6338 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6339 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6340 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6341 f2a9dc41 2019-12-13 tracey repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6344 01757395 2019-07-12 stsp goto done;
6347 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6348 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6350 01757395 2019-07-12 stsp goto done;
6353 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6354 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6355 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6357 a0e95631 2019-07-12 stsp goto done;
6358 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6359 a0e95631 2019-07-12 stsp goto done;
6362 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6364 edd02c5e 2019-07-11 stsp goto done;
6366 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6368 818c7501 2019-07-11 stsp goto done;
6370 5943eee2 2019-08-13 stsp if (new_logmsg) {
6371 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6372 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6373 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6374 5943eee2 2019-08-13 stsp goto done;
6377 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6379 5943eee2 2019-08-13 stsp goto done;
6382 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6383 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6384 5c1e53bc 2019-07-28 stsp worktree, got_object_commit_get_author(orig_commit),
6385 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
6386 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6388 a0e95631 2019-07-12 stsp goto done;
6390 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6392 a0e95631 2019-07-12 stsp goto done;
6394 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6396 a0e95631 2019-07-12 stsp goto done;
6398 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6399 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6400 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6401 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6402 a0e95631 2019-07-12 stsp err = sync_err;
6404 a0e95631 2019-07-12 stsp free(fileindex_path);
6405 a0e95631 2019-07-12 stsp free(head_commit_id);
6406 a0e95631 2019-07-12 stsp if (head_ref)
6407 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6408 818c7501 2019-07-11 stsp if (err) {
6409 818c7501 2019-07-11 stsp free(*new_commit_id);
6410 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6412 818c7501 2019-07-11 stsp return err;
6415 818c7501 2019-07-11 stsp const struct got_error *
6416 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6417 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6418 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6419 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6420 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6422 0ebf8283 2019-07-24 stsp const struct got_error *err;
6423 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6424 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6425 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6427 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6429 0ebf8283 2019-07-24 stsp return err;
6431 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6433 0ebf8283 2019-07-24 stsp goto done;
6434 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6436 0ebf8283 2019-07-24 stsp goto done;
6437 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6438 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6439 0ebf8283 2019-07-24 stsp goto done;
6442 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6443 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6445 0ebf8283 2019-07-24 stsp if (commit_ref)
6446 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6447 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6448 0ebf8283 2019-07-24 stsp free(commit_id);
6449 0ebf8283 2019-07-24 stsp return err;
6452 0ebf8283 2019-07-24 stsp const struct got_error *
6453 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6454 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6455 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6456 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6457 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6458 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6460 0ebf8283 2019-07-24 stsp const struct got_error *err;
6461 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6462 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6464 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6466 0ebf8283 2019-07-24 stsp return err;
6468 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6470 0ebf8283 2019-07-24 stsp goto done;
6472 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6473 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6475 0ebf8283 2019-07-24 stsp if (commit_ref)
6476 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6477 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6478 0ebf8283 2019-07-24 stsp return err;
6481 0ebf8283 2019-07-24 stsp const struct got_error *
6482 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6483 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6485 3e3a69f1 2019-07-25 stsp if (fileindex)
6486 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6487 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6490 69844fba 2019-07-11 stsp static const struct got_error *
6491 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6493 69844fba 2019-07-11 stsp const struct got_error *err;
6494 69844fba 2019-07-11 stsp struct got_reference *ref;
6496 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6497 69844fba 2019-07-11 stsp if (err) {
6498 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6499 69844fba 2019-07-11 stsp return NULL;
6500 69844fba 2019-07-11 stsp return err;
6503 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6504 69844fba 2019-07-11 stsp got_ref_close(ref);
6505 69844fba 2019-07-11 stsp return err;
6508 69844fba 2019-07-11 stsp static const struct got_error *
6509 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6511 69844fba 2019-07-11 stsp const struct got_error *err;
6512 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6513 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6515 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6517 69844fba 2019-07-11 stsp goto done;
6518 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6520 69844fba 2019-07-11 stsp goto done;
6522 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6524 69844fba 2019-07-11 stsp goto done;
6525 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6527 69844fba 2019-07-11 stsp goto done;
6529 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6531 69844fba 2019-07-11 stsp goto done;
6532 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6534 69844fba 2019-07-11 stsp goto done;
6536 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6538 69844fba 2019-07-11 stsp goto done;
6539 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6541 69844fba 2019-07-11 stsp goto done;
6544 69844fba 2019-07-11 stsp free(tmp_branch_name);
6545 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6546 69844fba 2019-07-11 stsp free(branch_ref_name);
6547 69844fba 2019-07-11 stsp free(commit_ref_name);
6548 e600f124 2021-03-21 stsp return err;
6551 e600f124 2021-03-21 stsp const struct got_error *
6552 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6553 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
6555 e600f124 2021-03-21 stsp const struct got_error *err;
6556 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
6557 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
6558 e600f124 2021-03-21 stsp const char *branch_name = NULL;
6559 e600f124 2021-03-21 stsp char *new_id_str = NULL;
6560 e600f124 2021-03-21 stsp char *refname = NULL;
6562 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
6563 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
6564 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6565 e600f124 2021-03-21 stsp branch_name += 11;
6567 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
6569 e600f124 2021-03-21 stsp return err;
6571 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6572 e600f124 2021-03-21 stsp new_id_str) == -1) {
6573 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
6574 e600f124 2021-03-21 stsp goto done;
6577 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
6579 e600f124 2021-03-21 stsp goto done;
6581 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
6583 e600f124 2021-03-21 stsp goto done;
6585 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
6587 e600f124 2021-03-21 stsp free(new_id_str);
6588 e600f124 2021-03-21 stsp free(refname);
6589 e600f124 2021-03-21 stsp free(old_commit_id);
6591 e600f124 2021-03-21 stsp got_ref_close(ref);
6592 69844fba 2019-07-11 stsp return err;
6595 818c7501 2019-07-11 stsp const struct got_error *
6596 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
6597 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6598 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6599 e600f124 2021-03-21 stsp struct got_repository *repo, int create_backup)
6601 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
6602 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
6603 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
6605 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6607 818c7501 2019-07-11 stsp return err;
6609 e600f124 2021-03-21 stsp if (create_backup) {
6610 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6611 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
6613 e600f124 2021-03-21 stsp goto done;
6616 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6618 818c7501 2019-07-11 stsp goto done;
6620 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
6622 818c7501 2019-07-11 stsp goto done;
6624 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
6626 818c7501 2019-07-11 stsp goto done;
6628 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
6630 a615e0e7 2020-12-16 stsp goto done;
6632 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
6634 a615e0e7 2020-12-16 stsp goto done;
6635 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6636 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6637 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
6638 a615e0e7 2020-12-16 stsp err = sync_err;
6640 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
6641 a615e0e7 2020-12-16 stsp free(fileindex_path);
6642 818c7501 2019-07-11 stsp free(new_head_commit_id);
6643 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6644 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6645 818c7501 2019-07-11 stsp err = unlockerr;
6646 818c7501 2019-07-11 stsp return err;
6649 818c7501 2019-07-11 stsp const struct got_error *
6650 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
6651 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6652 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
6653 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6655 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
6656 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
6657 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
6658 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6659 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6660 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
6662 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6664 818c7501 2019-07-11 stsp return err;
6666 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
6667 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
6669 818c7501 2019-07-11 stsp goto done;
6671 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
6673 818c7501 2019-07-11 stsp goto done;
6676 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
6677 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
6678 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
6680 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
6682 818c7501 2019-07-11 stsp goto done;
6684 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6686 818c7501 2019-07-11 stsp goto done;
6688 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
6689 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
6691 ca355955 2019-07-12 stsp goto done;
6693 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
6695 ca355955 2019-07-12 stsp goto done;
6697 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6699 818c7501 2019-07-11 stsp goto done;
6701 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6702 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6703 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6704 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6705 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6706 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6707 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6708 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6709 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6711 1f1abb7e 2019-08-08 stsp goto sync;
6713 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6714 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
6716 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6717 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
6718 ca355955 2019-07-12 stsp err = sync_err;
6720 818c7501 2019-07-11 stsp got_ref_close(resolved);
6721 a3a2faf2 2019-07-12 stsp free(tree_id);
6722 818c7501 2019-07-11 stsp free(commit_id);
6723 0ebf8283 2019-07-24 stsp if (fileindex)
6724 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
6725 0ebf8283 2019-07-24 stsp free(fileindex_path);
6727 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6728 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6729 0ebf8283 2019-07-24 stsp err = unlockerr;
6730 0ebf8283 2019-07-24 stsp return err;
6733 0ebf8283 2019-07-24 stsp const struct got_error *
6734 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6735 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6736 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
6737 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6739 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6740 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6741 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
6742 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
6743 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6744 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
6745 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
6746 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6748 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6749 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6750 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6751 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6753 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6755 0ebf8283 2019-07-24 stsp return err;
6757 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6759 0ebf8283 2019-07-24 stsp goto done;
6761 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
6762 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
6763 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6766 0ebf8283 2019-07-24 stsp goto done;
6768 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6770 0ebf8283 2019-07-24 stsp goto done;
6772 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6774 0ebf8283 2019-07-24 stsp goto done;
6776 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6777 0ebf8283 2019-07-24 stsp worktree);
6779 0ebf8283 2019-07-24 stsp goto done;
6781 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6784 0ebf8283 2019-07-24 stsp goto done;
6786 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6788 0ebf8283 2019-07-24 stsp goto done;
6790 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
6792 0ebf8283 2019-07-24 stsp goto done;
6794 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6795 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6797 0ebf8283 2019-07-24 stsp goto done;
6798 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
6800 0ebf8283 2019-07-24 stsp goto done;
6801 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6802 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
6803 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
6804 0ebf8283 2019-07-24 stsp goto done;
6807 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6808 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6810 0ebf8283 2019-07-24 stsp goto done;
6811 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
6813 0ebf8283 2019-07-24 stsp goto done;
6815 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6817 0ebf8283 2019-07-24 stsp goto done;
6819 0ebf8283 2019-07-24 stsp free(fileindex_path);
6820 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6821 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6822 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6823 0ebf8283 2019-07-24 stsp if (wt_branch)
6824 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
6825 0ebf8283 2019-07-24 stsp if (err) {
6826 0ebf8283 2019-07-24 stsp if (*branch_ref) {
6827 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
6828 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6830 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6831 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6832 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6834 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6835 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6836 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6837 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6839 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
6841 0ebf8283 2019-07-24 stsp return err;
6844 0ebf8283 2019-07-24 stsp const struct got_error *
6845 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
6846 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6848 3e3a69f1 2019-07-25 stsp if (fileindex)
6849 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6850 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
6853 0ebf8283 2019-07-24 stsp const struct got_error *
6854 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
6855 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
6857 0ebf8283 2019-07-24 stsp const struct got_error *err;
6858 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6860 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6862 0ebf8283 2019-07-24 stsp return err;
6864 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6865 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6866 0ebf8283 2019-07-24 stsp return NULL;
6869 0ebf8283 2019-07-24 stsp const struct got_error *
6870 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
6871 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
6872 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6873 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6875 0ebf8283 2019-07-24 stsp const struct got_error *err;
6876 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6877 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6878 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6879 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6880 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6881 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6883 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6884 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6885 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6886 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6888 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6890 3e3a69f1 2019-07-25 stsp return err;
6892 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6894 f032f1f7 2019-08-04 stsp goto done;
6896 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6897 f032f1f7 2019-08-04 stsp &have_staged_files);
6898 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6899 f032f1f7 2019-08-04 stsp goto done;
6900 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6901 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6902 3e3a69f1 2019-07-25 stsp goto done;
6905 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6907 f032f1f7 2019-08-04 stsp goto done;
6909 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6911 0ebf8283 2019-07-24 stsp goto done;
6913 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6915 0ebf8283 2019-07-24 stsp goto done;
6917 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6918 0ebf8283 2019-07-24 stsp worktree);
6920 0ebf8283 2019-07-24 stsp goto done;
6922 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6924 0ebf8283 2019-07-24 stsp goto done;
6926 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6928 0ebf8283 2019-07-24 stsp goto done;
6929 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6931 0ebf8283 2019-07-24 stsp goto done;
6933 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6935 0ebf8283 2019-07-24 stsp goto done;
6936 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6938 0ebf8283 2019-07-24 stsp goto done;
6940 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6942 0ebf8283 2019-07-24 stsp goto done;
6944 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6945 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6946 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6947 0ebf8283 2019-07-24 stsp if (commit_ref)
6948 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6949 0ebf8283 2019-07-24 stsp if (base_commit_ref)
6950 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
6951 0ebf8283 2019-07-24 stsp if (err) {
6952 0ebf8283 2019-07-24 stsp free(*commit_id);
6953 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6954 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6955 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6956 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6957 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6958 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6960 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6961 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6962 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6964 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
6966 0ebf8283 2019-07-24 stsp return err;
6969 0ebf8283 2019-07-24 stsp static const struct got_error *
6970 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6972 0ebf8283 2019-07-24 stsp const struct got_error *err;
6973 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6974 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6976 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6978 0ebf8283 2019-07-24 stsp goto done;
6979 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
6981 0ebf8283 2019-07-24 stsp goto done;
6983 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6984 0ebf8283 2019-07-24 stsp worktree);
6986 0ebf8283 2019-07-24 stsp goto done;
6987 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
6989 0ebf8283 2019-07-24 stsp goto done;
6991 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6993 0ebf8283 2019-07-24 stsp goto done;
6994 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
6996 0ebf8283 2019-07-24 stsp goto done;
6998 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7000 0ebf8283 2019-07-24 stsp goto done;
7001 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7003 0ebf8283 2019-07-24 stsp goto done;
7005 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7006 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7007 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7008 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7009 0ebf8283 2019-07-24 stsp return err;
7012 0ebf8283 2019-07-24 stsp const struct got_error *
7013 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7014 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7015 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7016 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7018 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7019 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7020 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7021 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7022 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7024 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7026 0ebf8283 2019-07-24 stsp return err;
7028 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7029 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7031 0ebf8283 2019-07-24 stsp goto done;
7033 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7035 0ebf8283 2019-07-24 stsp goto done;
7037 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7039 0ebf8283 2019-07-24 stsp goto done;
7041 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7042 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7044 0ebf8283 2019-07-24 stsp goto done;
7046 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7048 0ebf8283 2019-07-24 stsp goto done;
7050 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7052 0ebf8283 2019-07-24 stsp goto done;
7054 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7055 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7056 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7057 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7058 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7059 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7060 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7061 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7062 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
7064 1f1abb7e 2019-08-08 stsp goto sync;
7066 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7067 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7069 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7070 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
7071 0ebf8283 2019-07-24 stsp err = sync_err;
7073 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
7074 0ebf8283 2019-07-24 stsp free(tree_id);
7075 818c7501 2019-07-11 stsp free(fileindex_path);
7077 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7078 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7079 818c7501 2019-07-11 stsp err = unlockerr;
7080 818c7501 2019-07-11 stsp return err;
7083 0ebf8283 2019-07-24 stsp const struct got_error *
7084 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
7085 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7086 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
7088 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7089 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
7090 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7091 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7093 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7095 0ebf8283 2019-07-24 stsp return err;
7097 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7098 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
7100 e600f124 2021-03-21 stsp goto done;
7102 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7103 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
7105 0ebf8283 2019-07-24 stsp goto done;
7107 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
7109 0ebf8283 2019-07-24 stsp goto done;
7111 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
7113 0ebf8283 2019-07-24 stsp goto done;
7115 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7117 0ebf8283 2019-07-24 stsp goto done;
7119 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7121 a615e0e7 2020-12-16 stsp goto done;
7123 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7125 a615e0e7 2020-12-16 stsp goto done;
7126 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7127 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7128 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7129 a615e0e7 2020-12-16 stsp err = sync_err;
7131 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7132 a615e0e7 2020-12-16 stsp free(fileindex_path);
7133 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
7134 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7135 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7136 0ebf8283 2019-07-24 stsp err = unlockerr;
7137 0ebf8283 2019-07-24 stsp return err;
7140 0ebf8283 2019-07-24 stsp const struct got_error *
7141 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7142 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7144 0ebf8283 2019-07-24 stsp const struct got_error *err;
7145 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7147 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7149 0ebf8283 2019-07-24 stsp return err;
7151 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7153 0ebf8283 2019-07-24 stsp goto done;
7155 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7157 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7158 2822a352 2019-10-15 stsp return err;
7161 2822a352 2019-10-15 stsp const struct got_error *
7162 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7163 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7164 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7165 2822a352 2019-10-15 stsp struct got_repository *repo)
7167 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7168 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7169 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7171 2822a352 2019-10-15 stsp *fileindex = NULL;
7172 2822a352 2019-10-15 stsp *branch_ref = NULL;
7173 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7175 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7177 2822a352 2019-10-15 stsp return err;
7179 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7180 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7181 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7182 2822a352 2019-10-15 stsp "update -b or different branch name required");
7183 2822a352 2019-10-15 stsp goto done;
7186 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7188 2822a352 2019-10-15 stsp goto done;
7190 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
7191 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7192 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7193 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7196 2822a352 2019-10-15 stsp goto done;
7198 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
7200 2822a352 2019-10-15 stsp goto done;
7202 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
7203 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7205 2822a352 2019-10-15 stsp if (err) {
7206 2822a352 2019-10-15 stsp if (*branch_ref) {
7207 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7208 2822a352 2019-10-15 stsp *branch_ref = NULL;
7210 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7211 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7212 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7214 2822a352 2019-10-15 stsp if (*fileindex) {
7215 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7216 2822a352 2019-10-15 stsp *fileindex = NULL;
7218 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7220 0cb83759 2019-08-03 stsp return err;
7223 2822a352 2019-10-15 stsp const struct got_error *
7224 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7225 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7226 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7227 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7228 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7230 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7231 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7232 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7234 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7236 2822a352 2019-10-15 stsp goto done;
7238 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7240 2822a352 2019-10-15 stsp goto done;
7242 2822a352 2019-10-15 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
7243 2822a352 2019-10-15 stsp worktree->path_prefix);
7245 2822a352 2019-10-15 stsp goto done;
7247 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7249 2822a352 2019-10-15 stsp goto done;
7251 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7252 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7254 2822a352 2019-10-15 stsp goto sync;
7256 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7258 2822a352 2019-10-15 stsp goto sync;
7260 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7262 6e210706 2021-01-22 stsp goto sync;
7264 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7266 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7267 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7268 2822a352 2019-10-15 stsp err = sync_err;
7271 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7272 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7273 2822a352 2019-10-15 stsp err = unlockerr;
7274 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7276 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7277 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7278 2822a352 2019-10-15 stsp err = unlockerr;
7279 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7281 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7282 2822a352 2019-10-15 stsp free(fileindex_path);
7283 2822a352 2019-10-15 stsp free(tree_id);
7285 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7286 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7287 2822a352 2019-10-15 stsp err = unlockerr;
7288 2822a352 2019-10-15 stsp return err;
7291 2822a352 2019-10-15 stsp const struct got_error *
7292 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7293 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7294 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7296 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7298 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7300 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7302 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7303 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7304 8b692cd0 2019-10-21 stsp err = unlockerr;
7305 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7307 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7308 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7309 8b692cd0 2019-10-21 stsp err = unlockerr;
7310 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7312 8b692cd0 2019-10-21 stsp return err;
7315 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
7316 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
7317 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7318 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7319 2db2652d 2019-08-07 stsp struct got_repository *repo;
7320 2db2652d 2019-08-07 stsp int have_changes;
7323 2db2652d 2019-08-07 stsp const struct got_error *
7324 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
7325 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7326 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7327 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7329 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
7330 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
7331 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
7332 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
7333 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
7334 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
7336 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
7337 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
7338 8b13ce36 2019-08-08 stsp return NULL;
7339 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
7340 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
7342 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7343 735ef5ac 2019-08-03 stsp if (ie == NULL)
7344 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7346 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7347 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7348 735ef5ac 2019-08-03 stsp relpath) == -1)
7349 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
7351 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
7352 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
7353 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7354 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
7357 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
7358 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7359 3aa5969e 2019-08-06 stsp goto done;
7360 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
7361 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
7362 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
7363 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7364 735ef5ac 2019-08-03 stsp goto done;
7367 2db2652d 2019-08-07 stsp a->have_changes = 1;
7369 735ef5ac 2019-08-03 stsp p = in_repo_path;
7370 735ef5ac 2019-08-03 stsp while (p[0] == '/')
7372 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
7373 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
7374 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
7376 735ef5ac 2019-08-03 stsp free(in_repo_path);
7377 dc424a06 2019-08-07 stsp return err;
7380 2db2652d 2019-08-07 stsp struct stage_path_arg {
7381 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7382 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7383 2db2652d 2019-08-07 stsp struct got_repository *repo;
7384 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
7385 2db2652d 2019-08-07 stsp void *status_arg;
7386 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
7387 2db2652d 2019-08-07 stsp void *patch_arg;
7388 7b5dc508 2019-10-28 stsp int staged_something;
7389 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
7392 2db2652d 2019-08-07 stsp static const struct got_error *
7393 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
7394 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7395 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7396 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7398 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
7399 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
7400 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
7401 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
7402 0cb83759 2019-08-03 stsp uint32_t stage;
7403 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
7404 0aeb8099 2020-07-23 stsp struct stat sb;
7406 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
7407 8b13ce36 2019-08-08 stsp return NULL;
7409 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7410 d3e7c587 2019-08-03 stsp if (ie == NULL)
7411 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7413 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7414 2db2652d 2019-08-07 stsp relpath)== -1)
7415 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
7417 0cb83759 2019-08-03 stsp switch (status) {
7418 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
7419 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
7420 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
7421 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
7422 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
7425 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7426 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
7427 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7428 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7429 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
7432 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
7435 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
7436 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
7437 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
7438 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
7439 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
7443 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
7444 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
7447 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7448 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7449 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7450 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
7452 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
7453 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7454 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
7455 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
7456 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
7457 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
7458 35213c7c 2020-07-23 stsp ssize_t target_len;
7459 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
7460 35213c7c 2020-07-23 stsp sizeof(target_path));
7461 35213c7c 2020-07-23 stsp if (target_len == -1) {
7462 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
7463 35213c7c 2020-07-23 stsp ondisk_path);
7466 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
7467 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
7468 35213c7c 2020-07-23 stsp a->worktree->root_path);
7471 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
7472 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
7473 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
7477 35213c7c 2020-07-23 stsp if (is_bad_symlink)
7478 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7479 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
7481 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7482 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
7484 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7485 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
7487 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7488 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7490 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7491 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
7492 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
7494 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
7495 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
7497 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7498 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7499 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
7500 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
7503 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7505 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7506 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7510 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
7511 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7512 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7513 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7515 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7516 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7519 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
7521 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
7522 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7524 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
7525 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
7528 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7532 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
7533 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
7534 dc424a06 2019-08-07 stsp free(path_content);
7535 2db2652d 2019-08-07 stsp free(ondisk_path);
7536 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
7537 0ebf8283 2019-07-24 stsp return err;
7540 0cb83759 2019-08-03 stsp const struct got_error *
7541 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
7542 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
7543 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
7544 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7545 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
7547 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7548 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
7549 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7550 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
7551 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
7552 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
7553 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
7554 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
7556 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7558 0cb83759 2019-08-03 stsp return err;
7560 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
7561 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
7563 735ef5ac 2019-08-03 stsp goto done;
7564 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7566 735ef5ac 2019-08-03 stsp goto done;
7567 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7569 0cb83759 2019-08-03 stsp goto done;
7571 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
7572 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
7573 2db2652d 2019-08-07 stsp oka.worktree = worktree;
7574 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
7575 2db2652d 2019-08-07 stsp oka.repo = repo;
7576 2db2652d 2019-08-07 stsp oka.have_changes = 0;
7577 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7578 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7579 f2a9dc41 2019-12-13 tracey check_stage_ok, &oka, NULL, NULL, 0, 0);
7581 735ef5ac 2019-08-03 stsp goto done;
7583 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
7584 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7585 2db2652d 2019-08-07 stsp goto done;
7588 2db2652d 2019-08-07 stsp spa.worktree = worktree;
7589 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
7590 2db2652d 2019-08-07 stsp spa.repo = repo;
7591 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
7592 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
7593 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
7594 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
7595 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
7596 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
7597 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7598 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7599 f2a9dc41 2019-12-13 tracey stage_path, &spa, NULL, NULL, 0, 0);
7601 2db2652d 2019-08-07 stsp goto done;
7603 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
7604 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7605 7b5dc508 2019-10-28 stsp goto done;
7608 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7609 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
7610 0cb83759 2019-08-03 stsp err = sync_err;
7612 735ef5ac 2019-08-03 stsp if (head_ref)
7613 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
7614 735ef5ac 2019-08-03 stsp free(head_commit_id);
7615 0cb83759 2019-08-03 stsp free(fileindex_path);
7616 0cb83759 2019-08-03 stsp if (fileindex)
7617 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
7618 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7619 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
7620 0cb83759 2019-08-03 stsp err = unlockerr;
7621 0cb83759 2019-08-03 stsp return err;
7624 ad493afc 2019-08-03 stsp struct unstage_path_arg {
7625 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
7626 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
7627 ad493afc 2019-08-03 stsp struct got_repository *repo;
7628 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
7629 ad493afc 2019-08-03 stsp void *progress_arg;
7630 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
7631 2e1f37b0 2019-08-08 stsp void *patch_arg;
7634 2e1f37b0 2019-08-08 stsp static const struct got_error *
7635 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
7636 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
7637 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
7638 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
7639 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
7641 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
7642 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
7643 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7644 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7645 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
7646 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
7647 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
7649 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7650 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7652 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
7654 2e1f37b0 2019-08-08 stsp return err;
7655 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7657 2e1f37b0 2019-08-08 stsp goto done;
7659 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7661 2e1f37b0 2019-08-08 stsp goto done;
7663 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7665 2e1f37b0 2019-08-08 stsp goto done;
7667 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7669 2e1f37b0 2019-08-08 stsp goto done;
7671 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7673 2e1f37b0 2019-08-08 stsp goto done;
7675 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7677 2e1f37b0 2019-08-08 stsp goto done;
7679 fe621944 2020-11-10 stsp err = got_diff_files(&diffreg_result, f1, label1, f2,
7680 64453f7e 2020-11-21 stsp path2, 3, 0, 1, NULL);
7682 2e1f37b0 2019-08-08 stsp goto done;
7684 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
7685 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
7687 2e1f37b0 2019-08-08 stsp goto done;
7688 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
7689 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
7691 2e1f37b0 2019-08-08 stsp goto done;
7693 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
7694 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
7695 2e1f37b0 2019-08-08 stsp goto done;
7697 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
7698 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
7699 2e1f37b0 2019-08-08 stsp goto done;
7701 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
7702 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7703 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
7704 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
7705 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
7706 fe621944 2020-11-10 stsp nchanges++;
7708 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7709 2e1f37b0 2019-08-08 stsp int choice;
7710 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
7711 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
7712 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
7713 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
7715 2e1f37b0 2019-08-08 stsp goto done;
7716 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
7717 2e1f37b0 2019-08-08 stsp have_content = 1;
7719 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
7720 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
7723 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
7724 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7725 f1e81a05 2019-08-10 stsp outfile, rejectfile);
7727 2e1f37b0 2019-08-08 stsp free(label1);
7729 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
7730 2e1f37b0 2019-08-08 stsp if (staged_blob)
7731 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
7732 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
7733 fe621944 2020-11-10 stsp if (free_err && err == NULL)
7734 fe621944 2020-11-10 stsp err = free_err;
7735 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
7736 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
7737 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
7738 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
7739 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
7740 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
7741 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7742 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
7743 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
7744 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
7745 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
7746 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
7747 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
7748 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
7749 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
7750 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7751 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
7752 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
7753 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7755 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
7756 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
7757 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
7758 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7759 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
7760 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
7761 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7763 2e1f37b0 2019-08-08 stsp free(path1);
7764 2e1f37b0 2019-08-08 stsp free(path2);
7765 fda8017d 2020-07-23 stsp return err;
7768 fda8017d 2020-07-23 stsp static const struct got_error *
7769 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
7770 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
7771 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7772 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
7773 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
7774 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7775 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7777 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
7778 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
7779 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
7780 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
7781 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
7782 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
7783 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
7784 fda8017d 2020-07-23 stsp struct stat sb;
7786 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
7787 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
7788 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
7790 fda8017d 2020-07-23 stsp return err;
7792 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
7793 fda8017d 2020-07-23 stsp return NULL;
7795 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
7796 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
7797 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
7799 fda8017d 2020-07-23 stsp goto done;
7802 fda8017d 2020-07-23 stsp f = fopen(path_unstaged_content, "r");
7803 fda8017d 2020-07-23 stsp if (f == NULL) {
7804 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
7805 fda8017d 2020-07-23 stsp path_unstaged_content);
7806 fda8017d 2020-07-23 stsp goto done;
7808 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
7809 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
7810 fda8017d 2020-07-23 stsp goto done;
7812 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
7813 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7814 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
7816 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
7817 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
7818 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
7819 fda8017d 2020-07-23 stsp goto done;
7821 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
7822 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7823 fda8017d 2020-07-23 stsp goto done;
7825 fda8017d 2020-07-23 stsp link_target[r] = '\0';
7826 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
7827 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
7828 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
7829 fda8017d 2020-07-23 stsp progress_arg);
7831 fda8017d 2020-07-23 stsp int local_changes_subsumed;
7833 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
7835 67a66647 2021-05-31 stsp return err;
7837 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
7838 67a66647 2021-05-31 stsp parent) == -1) {
7839 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
7840 67a66647 2021-05-31 stsp base_path = NULL;
7841 67a66647 2021-05-31 stsp goto done;
7844 67a66647 2021-05-31 stsp err = got_opentemp_named(&blob_base_path, &f_base, base_path);
7846 67a66647 2021-05-31 stsp goto done;
7847 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
7848 67a66647 2021-05-31 stsp blob_base);
7850 67a66647 2021-05-31 stsp goto done;
7853 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
7854 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
7856 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7857 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
7858 eec2f5a9 2021-06-03 stsp ondisk_path);
7860 eec2f5a9 2021-06-03 stsp goto done;
7863 eec2f5a9 2021-06-03 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
7864 eec2f5a9 2021-06-03 stsp if (fd == -1) {
7865 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
7866 eec2f5a9 2021-06-03 stsp goto done;
7868 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
7869 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
7870 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
7871 eec2f5a9 2021-06-03 stsp close(fd);
7872 eec2f5a9 2021-06-03 stsp goto done;
7876 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
7877 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
7878 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
7879 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
7880 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
7883 fda8017d 2020-07-23 stsp goto done;
7885 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
7886 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7887 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
7889 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7890 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
7893 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
7894 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
7895 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
7896 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
7897 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
7898 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
7899 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
7900 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
7901 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
7902 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
7903 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
7904 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
7905 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
7906 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
7907 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
7908 fda8017d 2020-07-23 stsp free(path_unstaged_content);
7909 fda8017d 2020-07-23 stsp free(path_new_staged_content);
7910 67a66647 2021-05-31 stsp free(blob_base_path);
7911 67a66647 2021-05-31 stsp free(parent);
7912 67a66647 2021-05-31 stsp free(base_path);
7913 2e1f37b0 2019-08-08 stsp return err;
7916 ad493afc 2019-08-03 stsp static const struct got_error *
7917 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
7918 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
7919 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7920 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7922 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
7923 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
7924 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
7925 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7926 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
7927 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
7928 ad493afc 2019-08-03 stsp int local_changes_subsumed;
7929 9bc94a15 2019-08-03 stsp struct stat sb;
7931 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
7932 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
7933 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
7934 2e1f37b0 2019-08-08 stsp return NULL;
7936 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7937 ad493afc 2019-08-03 stsp if (ie == NULL)
7938 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7940 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7942 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
7944 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
7945 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
7947 f69721c3 2019-10-21 stsp goto done;
7948 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7949 f69721c3 2019-10-21 stsp id_str) == -1) {
7950 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
7951 f69721c3 2019-10-21 stsp goto done;
7954 ad493afc 2019-08-03 stsp switch (staged_status) {
7955 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
7956 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
7957 ad493afc 2019-08-03 stsp blob_id, 8192);
7960 ad493afc 2019-08-03 stsp /* fall through */
7961 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
7962 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7963 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
7964 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7965 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7966 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7969 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
7972 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
7973 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
7974 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
7975 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
7976 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
7977 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
7980 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
7981 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
7984 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
7985 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
7986 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
7987 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
7988 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
7989 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
7990 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
7991 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
7992 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
7994 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
7995 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7996 36bf999c 2020-07-23 stsp char *staged_target;
7997 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
7998 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
8000 36bf999c 2020-07-23 stsp goto done;
8001 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
8002 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
8003 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
8004 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
8005 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
8006 36bf999c 2020-07-23 stsp free(staged_target);
8008 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
8009 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
8010 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
8011 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
8012 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
8013 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8014 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
8018 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8021 2ac8aa02 2020-11-09 stsp if (err == NULL) {
8022 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
8023 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
8024 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8027 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
8028 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8029 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8030 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8031 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8034 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8036 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8037 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8041 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8042 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8043 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
8044 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
8047 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
8051 ad493afc 2019-08-03 stsp free(ondisk_path);
8052 ad493afc 2019-08-03 stsp if (blob_base)
8053 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
8054 ad493afc 2019-08-03 stsp if (blob_staged)
8055 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
8056 f69721c3 2019-10-21 stsp free(id_str);
8057 f69721c3 2019-10-21 stsp free(label_orig);
8058 ad493afc 2019-08-03 stsp return err;
8061 ad493afc 2019-08-03 stsp const struct got_error *
8062 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
8063 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
8064 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
8065 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8066 ad493afc 2019-08-03 stsp struct got_repository *repo)
8068 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8069 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
8070 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8071 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
8072 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
8074 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8076 ad493afc 2019-08-03 stsp return err;
8078 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8080 ad493afc 2019-08-03 stsp goto done;
8082 ad493afc 2019-08-03 stsp upa.worktree = worktree;
8083 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
8084 ad493afc 2019-08-03 stsp upa.repo = repo;
8085 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
8086 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
8087 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
8088 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
8089 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8090 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8091 f2a9dc41 2019-12-13 tracey unstage_path, &upa, NULL, NULL, 0, 0);
8093 ad493afc 2019-08-03 stsp goto done;
8096 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8097 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
8098 ad493afc 2019-08-03 stsp err = sync_err;
8100 ad493afc 2019-08-03 stsp free(fileindex_path);
8101 ad493afc 2019-08-03 stsp if (fileindex)
8102 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
8103 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8104 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
8105 ad493afc 2019-08-03 stsp err = unlockerr;
8106 ad493afc 2019-08-03 stsp return err;
8109 b2118c49 2020-07-28 stsp struct report_file_info_arg {
8110 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
8111 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
8112 b2118c49 2020-07-28 stsp void *info_arg;
8113 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
8114 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
8115 b2118c49 2020-07-28 stsp void *cancel_arg;
8118 b2118c49 2020-07-28 stsp static const struct got_error *
8119 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
8121 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
8122 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
8123 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
8124 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8125 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
8126 b2118c49 2020-07-28 stsp int stage;
8128 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8129 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
8131 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
8132 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8133 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
8136 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
8137 b2118c49 2020-07-28 stsp return NULL;
8139 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_blob(ie)) {
8140 b2118c49 2020-07-28 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8141 b2118c49 2020-07-28 stsp blob_idp = &blob_id;
8143 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
8144 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8145 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
8146 b2118c49 2020-07-28 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8147 b2118c49 2020-07-28 stsp SHA1_DIGEST_LENGTH);
8148 b2118c49 2020-07-28 stsp staged_blob_idp = &staged_blob_id;
8151 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_commit(ie)) {
8152 b2118c49 2020-07-28 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8153 b2118c49 2020-07-28 stsp commit_idp = &commit_id;
8156 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8157 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8160 b2118c49 2020-07-28 stsp const struct got_error *
8161 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
8162 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
8163 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
8164 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8167 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
8168 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
8169 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
8170 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
8172 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
8174 b2118c49 2020-07-28 stsp return err;
8176 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8178 b2118c49 2020-07-28 stsp goto done;
8180 b2118c49 2020-07-28 stsp arg.worktree = worktree;
8181 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
8182 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
8183 b2118c49 2020-07-28 stsp arg.paths = paths;
8184 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
8185 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
8186 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8189 b2118c49 2020-07-28 stsp free(fileindex_path);
8190 b2118c49 2020-07-28 stsp if (fileindex)
8191 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
8192 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
8193 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
8194 b2118c49 2020-07-28 stsp err = unlockerr;
8195 b2118c49 2020-07-28 stsp return err;