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;
440 eaccb85f 2018-12-25 stsp got_repo_close(repo);
441 7ac97322 2018-03-11 stsp free(path_got);
442 056e7441 2018-03-11 stsp free(path_lock);
443 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
444 c442a90d 2019-03-10 stsp free(uuidstr);
445 bd165944 2019-03-10 stsp free(formatstr);
447 6d9d28c3 2018-03-11 stsp if (fd != -1)
449 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
450 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
451 6d9d28c3 2018-03-11 stsp *worktree = NULL;
453 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
455 6d9d28c3 2018-03-11 stsp return err;
458 247140b2 2019-02-05 stsp const struct got_error *
459 247140b2 2019-02-05 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
461 247140b2 2019-02-05 stsp const struct got_error *err = NULL;
462 aedea96d 2020-10-20 stsp char *worktree_path;
464 aedea96d 2020-10-20 stsp worktree_path = strdup(path);
465 aedea96d 2020-10-20 stsp if (worktree_path == NULL)
466 aedea96d 2020-10-20 stsp return got_error_from_errno("strdup");
469 aedea96d 2020-10-20 stsp char *parent_path;
471 aedea96d 2020-10-20 stsp err = open_worktree(worktree, worktree_path);
472 aedea96d 2020-10-20 stsp if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
473 aedea96d 2020-10-20 stsp free(worktree_path);
474 247140b2 2019-02-05 stsp return err;
476 aedea96d 2020-10-20 stsp if (*worktree) {
477 aedea96d 2020-10-20 stsp free(worktree_path);
478 aedea96d 2020-10-20 stsp return NULL;
480 aedea96d 2020-10-20 stsp if (worktree_path[0] == '/' && worktree_path[1] == '\0')
482 aedea96d 2020-10-20 stsp err = got_path_dirname(&parent_path, worktree_path);
484 aedea96d 2020-10-20 stsp if (err->code != GOT_ERR_BAD_PATH) {
485 aedea96d 2020-10-20 stsp free(worktree_path);
486 aedea96d 2020-10-20 stsp return err;
490 aedea96d 2020-10-20 stsp free(worktree_path);
491 aedea96d 2020-10-20 stsp worktree_path = parent_path;
494 aedea96d 2020-10-20 stsp free(worktree_path);
495 247140b2 2019-02-05 stsp return got_error(GOT_ERR_NOT_WORKTREE);
498 3a6ce05a 2019-02-11 stsp const struct got_error *
499 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
501 3a6ce05a 2019-02-11 stsp const struct got_error *err = NULL;
503 a6b21eef 2021-01-21 stsp if (worktree->lockfd != -1) {
504 08578a35 2021-01-22 stsp if (close(worktree->lockfd) == -1)
505 638f9024 2019-05-13 stsp err = got_error_from_errno2("close",
506 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree));
508 e7abd6b6 2021-01-22 stsp if (close(worktree->root_fd) == -1 && err == NULL)
509 e7abd6b6 2021-01-22 stsp err = got_error_from_errno2("close",
510 e7abd6b6 2021-01-22 stsp got_worktree_get_root_path(worktree));
511 60e40e95 2021-01-22 stsp free(worktree->repo_path);
512 60e40e95 2021-01-22 stsp free(worktree->path_prefix);
513 60e40e95 2021-01-22 stsp free(worktree->base_commit_id);
514 60e40e95 2021-01-22 stsp free(worktree->head_ref_name);
515 60e40e95 2021-01-22 stsp free(worktree->root_path);
516 60e40e95 2021-01-22 stsp free(worktree->gotconfig_path);
517 60e40e95 2021-01-22 stsp got_gotconfig_free(worktree->gotconfig);
518 a06ff56f 2021-01-21 naddy free(worktree);
519 3a6ce05a 2019-02-11 stsp return err;
522 2fbdb5ae 2018-12-29 stsp const char *
523 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
525 c7f4312f 2019-02-05 stsp return worktree->root_path;
528 c7f4312f 2019-02-05 stsp const char *
529 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
531 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
533 49520a32 2018-12-29 stsp const char *
534 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
536 f609be2e 2018-12-29 stsp return worktree->path_prefix;
539 e5dc7198 2018-12-29 stsp const struct got_error *
540 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
541 e5dc7198 2018-12-29 stsp const char *path_prefix)
543 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
545 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
546 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
547 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
549 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
550 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
551 e5dc7198 2018-12-29 stsp free(absprefix);
552 e5dc7198 2018-12-29 stsp return NULL;
555 bc70eb79 2019-05-09 stsp const char *
556 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
558 36a38700 2019-05-10 stsp return worktree->head_ref_name;
561 024e9686 2019-05-14 stsp const struct got_error *
562 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
563 024e9686 2019-05-14 stsp struct got_reference *head_ref)
565 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
566 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
568 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
569 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
570 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
571 024e9686 2019-05-14 stsp path_got = NULL;
575 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
576 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
577 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
581 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
585 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
586 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
588 024e9686 2019-05-14 stsp free(path_got);
590 024e9686 2019-05-14 stsp free(head_ref_name);
591 024e9686 2019-05-14 stsp return err;
594 b72f483a 2019-02-05 stsp struct got_object_id *
595 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
597 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
600 507dc3bb 2018-12-29 stsp const struct got_error *
601 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
602 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
604 507dc3bb 2018-12-29 stsp const struct got_error *err;
605 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
606 507dc3bb 2018-12-29 stsp char *id_str = NULL;
607 507dc3bb 2018-12-29 stsp char *path_got = NULL;
609 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
610 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
611 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
612 507dc3bb 2018-12-29 stsp path_got = NULL;
616 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
618 507dc3bb 2018-12-29 stsp return err;
620 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
621 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
625 507dc3bb 2018-12-29 stsp /* Record our base commit. */
626 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
629 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
633 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
634 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
635 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
636 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
641 507dc3bb 2018-12-29 stsp got_object_close(obj);
642 507dc3bb 2018-12-29 stsp free(id_str);
643 507dc3bb 2018-12-29 stsp free(path_got);
644 507dc3bb 2018-12-29 stsp return err;
647 50b0790e 2020-09-11 stsp const struct got_gotconfig *
648 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
650 50b0790e 2020-09-11 stsp return worktree->gotconfig;
653 9d31a1d8 2018-03-11 stsp static const struct got_error *
654 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
656 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
657 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
658 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
659 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
660 86c3caaf 2018-03-09 stsp return NULL;
663 21908da4 2019-01-13 stsp static const struct got_error *
664 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
666 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
667 4a1ddfc2 2019-01-12 stsp char *abspath;
669 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
670 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
672 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
673 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
674 ddcd8544 2019-03-11 stsp struct stat sb;
675 ddcd8544 2019-03-11 stsp err = NULL;
676 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
677 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
678 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
679 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
680 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
683 4a1ddfc2 2019-01-12 stsp free(abspath);
684 68c76935 2019-02-19 stsp return err;
687 68c76935 2019-02-19 stsp static const struct got_error *
688 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
690 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
691 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
692 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
693 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
698 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
699 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
700 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
703 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
704 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
705 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
708 68c76935 2019-02-19 stsp if (flen1 == 0) {
709 68c76935 2019-02-19 stsp if (flen2 != 0)
712 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
713 68c76935 2019-02-19 stsp if (flen1 != 0)
716 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
717 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
727 68c76935 2019-02-19 stsp return err;
730 68c76935 2019-02-19 stsp static const struct got_error *
731 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
733 68c76935 2019-02-19 stsp struct stat sb;
734 68c76935 2019-02-19 stsp size_t size1, size2;
738 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
739 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
740 68c76935 2019-02-19 stsp size1 = sb.st_size;
742 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
743 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
744 68c76935 2019-02-19 stsp size2 = sb.st_size;
746 68c76935 2019-02-19 stsp if (size1 != size2) {
748 68c76935 2019-02-19 stsp return NULL;
751 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
752 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
753 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
754 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
756 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
760 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
761 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
762 eec2f5a9 2021-06-03 stsp * and the file f_deriv2 acts as the second derived version.
763 eec2f5a9 2021-06-03 stsp * The merge result will be written to a new file at ondisk_path; any
764 eec2f5a9 2021-06-03 stsp * existing file at this path will be replaced.
766 6353ad76 2019-02-08 stsp static const struct got_error *
767 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
768 eec2f5a9 2021-06-03 stsp FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
769 07bb0f93 2021-06-02 stsp const char *path, uint16_t st_mode,
770 54d5be07 2021-06-03 stsp const char *label_orig, const char *label_deriv, const char *label_deriv2,
771 f69721c3 2019-10-21 stsp struct got_repository *repo,
772 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
774 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
775 6353ad76 2019-02-08 stsp int merged_fd = -1;
776 db590691 2021-06-02 stsp FILE *f_merged = NULL;
777 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
778 234035bc 2019-06-01 stsp int overlapcnt = 0;
779 3524bbf9 2020-10-20 stsp char *parent = NULL;
781 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
783 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
785 3524bbf9 2020-10-20 stsp return err;
787 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
788 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
792 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
796 eec2f5a9 2021-06-03 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
797 54d5be07 2021-06-03 stsp f_deriv2, label_deriv, label_orig, label_deriv2);
801 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
802 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
806 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
807 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
811 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
812 db590691 2021-06-02 stsp if (f_merged == NULL) {
813 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
816 db590691 2021-06-02 stsp merged_fd = -1;
818 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
819 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
820 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
826 db590691 2021-06-02 stsp if (fchmod(fileno(f_merged), st_mode) != 0) {
827 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
831 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
832 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
833 230a42bd 2019-05-11 jcs ondisk_path);
838 fdcb7daf 2019-12-15 stsp if (merged_path)
839 fdcb7daf 2019-12-15 stsp unlink(merged_path);
841 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
842 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
843 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
844 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
845 6353ad76 2019-02-08 stsp free(merged_path);
846 af54ae4a 2019-02-19 stsp free(base_path);
847 3524bbf9 2020-10-20 stsp free(parent);
848 af57b12a 2020-07-23 stsp return err;
851 af57b12a 2020-07-23 stsp static const struct got_error *
852 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
853 af57b12a 2020-07-23 stsp size_t target_len)
855 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
856 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
857 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
858 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
859 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
860 af57b12a 2020-07-23 stsp ondisk_path);
861 af57b12a 2020-07-23 stsp return NULL;
865 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
866 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
867 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
868 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
870 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
871 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
873 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
874 993e2a1b 2020-07-23 stsp * has deleted this symlink.
876 11cc08c1 2020-07-23 stsp static const struct got_error *
877 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
878 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
879 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
881 11cc08c1 2020-07-23 stsp const struct got_error *err;
882 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
883 11cc08c1 2020-07-23 stsp FILE *f = NULL;
885 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
887 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
889 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
890 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
891 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
895 11cc08c1 2020-07-23 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict");
897 3818e3c4 2020-11-01 naddy goto done;
899 3818e3c4 2020-11-01 naddy if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
900 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
904 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
905 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
906 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
907 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
908 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
909 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
910 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
911 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
912 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
913 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
917 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
918 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
921 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
922 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
926 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
927 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
928 11cc08c1 2020-07-23 stsp free(path);
929 11cc08c1 2020-07-23 stsp free(id_str);
930 11cc08c1 2020-07-23 stsp free(label_deriv);
931 11cc08c1 2020-07-23 stsp return err;
934 d219f183 2020-07-23 stsp /* forward declaration */
935 d219f183 2020-07-23 stsp static const struct got_error *
936 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
937 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
938 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
939 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
942 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
943 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
944 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
945 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
947 af57b12a 2020-07-23 stsp static const struct got_error *
948 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
949 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
950 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
951 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
952 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
954 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
955 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
956 af57b12a 2020-07-23 stsp struct stat sb;
957 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
958 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
959 11cc08c1 2020-07-23 stsp int have_local_change = 0;
960 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
962 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
963 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
965 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
966 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
967 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
968 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
969 af57b12a 2020-07-23 stsp ondisk_path);
972 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
974 526a746f 2020-07-23 stsp if (blob_orig) {
975 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
980 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
981 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
982 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
983 11cc08c1 2020-07-23 stsp have_local_change = 1;
985 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
986 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
987 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
988 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
989 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
991 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
992 11cc08c1 2020-07-23 stsp if (ancestor_target) {
993 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
994 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
996 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
997 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
998 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
999 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1002 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
1003 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1004 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
1005 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
1007 11cc08c1 2020-07-23 stsp goto done;
1008 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1011 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
1012 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
1013 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
1014 af57b12a 2020-07-23 stsp strlen(deriv_target));
1016 af57b12a 2020-07-23 stsp goto done;
1017 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1018 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
1019 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
1020 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1021 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
1022 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1025 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1026 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
1027 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
1029 ea7786be 2020-07-23 stsp goto done;
1030 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1036 af57b12a 2020-07-23 stsp free(ancestor_target);
1037 eec2f5a9 2021-06-03 stsp return err;
1040 eec2f5a9 2021-06-03 stsp static const struct got_error *
1041 eec2f5a9 2021-06-03 stsp dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
1043 eec2f5a9 2021-06-03 stsp const struct got_error *err = NULL;
1044 eec2f5a9 2021-06-03 stsp char target_path[PATH_MAX];
1045 eec2f5a9 2021-06-03 stsp ssize_t target_len;
1049 eec2f5a9 2021-06-03 stsp *outfile = NULL;
1051 eec2f5a9 2021-06-03 stsp f = got_opentemp();
1052 eec2f5a9 2021-06-03 stsp if (f == NULL)
1053 eec2f5a9 2021-06-03 stsp return got_error_from_errno("got_opentemp");
1054 eec2f5a9 2021-06-03 stsp target_len = readlink(ondisk_path, target_path, sizeof(target_path));
1055 eec2f5a9 2021-06-03 stsp if (target_len == -1) {
1056 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("readlink", ondisk_path);
1057 eec2f5a9 2021-06-03 stsp goto done;
1059 eec2f5a9 2021-06-03 stsp n = fwrite(target_path, 1, target_len, f);
1060 eec2f5a9 2021-06-03 stsp if (n != target_len) {
1061 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
1062 eec2f5a9 2021-06-03 stsp goto done;
1064 eec2f5a9 2021-06-03 stsp if (fflush(f) == EOF) {
1065 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fflush");
1066 eec2f5a9 2021-06-03 stsp goto done;
1068 eec2f5a9 2021-06-03 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
1069 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
1070 eec2f5a9 2021-06-03 stsp goto done;
1074 eec2f5a9 2021-06-03 stsp fclose(f);
1076 eec2f5a9 2021-06-03 stsp *outfile = f;
1077 14c901f1 2019-08-08 stsp return err;
1081 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
1082 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
1083 14c901f1 2019-08-08 stsp * acts as the second derived version.
1085 14c901f1 2019-08-08 stsp static const struct got_error *
1086 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1087 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
1088 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
1089 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
1090 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1091 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1093 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
1094 eec2f5a9 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
1095 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
1096 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1097 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
1099 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
1101 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1103 ed6b5030 2020-10-20 stsp return err;
1105 67a66647 2021-05-31 stsp if (blob_orig) {
1106 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
1107 67a66647 2021-05-31 stsp parent) == -1) {
1108 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
1109 67a66647 2021-05-31 stsp base_path = NULL;
1110 67a66647 2021-05-31 stsp goto done;
1113 67a66647 2021-05-31 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
1115 67a66647 2021-05-31 stsp goto done;
1116 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1117 67a66647 2021-05-31 stsp blob_orig);
1119 67a66647 2021-05-31 stsp goto done;
1120 67a66647 2021-05-31 stsp free(base_path);
1123 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1124 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1125 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1127 db590691 2021-06-02 stsp f_orig = got_opentemp();
1128 db590691 2021-06-02 stsp if (f_orig == NULL) {
1129 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1130 db590691 2021-06-02 stsp goto done;
1134 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1135 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1136 14c901f1 2019-08-08 stsp base_path = NULL;
1137 14c901f1 2019-08-08 stsp goto done;
1140 14c901f1 2019-08-08 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1142 14c901f1 2019-08-08 stsp goto done;
1143 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1144 14c901f1 2019-08-08 stsp blob_deriv);
1146 14c901f1 2019-08-08 stsp goto done;
1148 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1150 14c901f1 2019-08-08 stsp goto done;
1151 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1152 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1153 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1154 14c901f1 2019-08-08 stsp goto done;
1158 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1159 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1161 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1162 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1164 eec2f5a9 2021-06-03 stsp goto done;
1167 eec2f5a9 2021-06-03 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
1168 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1169 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1170 eec2f5a9 2021-06-03 stsp goto done;
1172 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1173 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1174 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1175 eec2f5a9 2021-06-03 stsp close(fd);
1176 eec2f5a9 2021-06-03 stsp goto done;
1180 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1181 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1182 54d5be07 2021-06-03 stsp NULL, repo, progress_cb, progress_arg);
1184 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1185 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1186 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1187 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1188 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1189 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1190 14c901f1 2019-08-08 stsp free(base_path);
1191 67a66647 2021-05-31 stsp if (blob_orig_path) {
1192 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1193 67a66647 2021-05-31 stsp free(blob_orig_path);
1195 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1196 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1197 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1199 6353ad76 2019-02-08 stsp free(id_str);
1200 818c7501 2019-07-11 stsp free(label_deriv);
1201 ed6b5030 2020-10-20 stsp free(parent);
1202 4a1ddfc2 2019-01-12 stsp return err;
1205 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1206 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1207 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1208 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1210 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1211 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1213 65b05cec 2020-07-23 stsp *new_iep = NULL;
1215 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1217 054041d0 2020-06-24 stsp return err;
1219 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1220 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1222 054041d0 2020-06-24 stsp goto done;
1224 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1227 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1229 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1230 13d9040b 2019-03-27 stsp return err;
1233 1ebedb77 2019-10-19 stsp static mode_t
1234 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1236 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1238 1ebedb77 2019-10-19 stsp if (executable) {
1239 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1240 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1241 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1242 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1243 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1244 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1247 1ebedb77 2019-10-19 stsp return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1250 8ba819a3 2020-07-23 stsp /* forward declaration */
1251 13d9040b 2019-03-27 stsp static const struct got_error *
1252 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1253 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1254 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1255 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1256 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1257 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1260 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1261 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1263 8ba819a3 2020-07-23 stsp static const struct got_error *
1264 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1265 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1267 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1268 5a1fbc73 2020-07-23 stsp ssize_t elen;
1269 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1272 c6e8a826 2021-04-05 stsp *did_something = 0;
1275 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1276 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1277 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1278 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1279 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1280 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1282 5a1fbc73 2020-07-23 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1283 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1284 5a1fbc73 2020-07-23 stsp if (errno != ELOOP)
1285 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1287 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1288 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1289 5a1fbc73 2020-07-23 stsp if (elen == -1)
1290 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1292 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1293 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1294 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1297 c6e8a826 2021-04-05 stsp *did_something = 1;
1298 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1299 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1300 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1301 5a1fbc73 2020-07-23 stsp return err;
1304 3c1ec782 2020-07-23 stsp static const struct got_error *
1305 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1306 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1308 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1309 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1310 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1312 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1314 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1315 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1316 3c1ec782 2020-07-23 stsp return NULL;
1320 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1321 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1322 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1323 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1324 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1326 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1327 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1328 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1330 ce031e9e 2020-10-20 stsp return err;
1331 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1332 ce031e9e 2020-10-20 stsp free(parent);
1333 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1335 ce031e9e 2020-10-20 stsp free(parent);
1336 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1337 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1338 3c1ec782 2020-07-23 stsp free(abspath);
1339 3c1ec782 2020-07-23 stsp return err;
1341 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1342 3c1ec782 2020-07-23 stsp free(abspath);
1344 3c1ec782 2020-07-23 stsp return err;
1346 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1348 3c1ec782 2020-07-23 stsp return err;
1351 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1352 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1353 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1354 3c1ec782 2020-07-23 stsp return NULL;
1357 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1358 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1359 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1360 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1361 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1362 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1364 3c1ec782 2020-07-23 stsp free(path_got);
1365 3c1ec782 2020-07-23 stsp return NULL;
1368 5a1fbc73 2020-07-23 stsp static const struct got_error *
1369 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1370 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1371 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1372 c90c8ce3 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1373 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1375 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1376 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1377 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1378 906c123b 2020-07-23 stsp char *path_got = NULL;
1379 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1380 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1382 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1385 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1386 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1387 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1388 8ba819a3 2020-07-23 stsp * in the blob object.
1391 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1392 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1393 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1394 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1395 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1396 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1397 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1398 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1399 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1400 3b9f0f87 2020-07-23 stsp progress_arg);
1402 8ba819a3 2020-07-23 stsp if (len > 0) {
1403 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1404 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1405 8ba819a3 2020-07-23 stsp len - hdrlen);
1406 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1407 8ba819a3 2020-07-23 stsp hdrlen = 0;
1409 8ba819a3 2020-07-23 stsp } while (len != 0);
1410 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1412 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1413 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1415 3c1ec782 2020-07-23 stsp return err;
1417 3c1ec782 2020-07-23 stsp if (*is_bad_symlink) {
1418 906c123b 2020-07-23 stsp /* install as a regular file */
1419 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1420 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1421 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1422 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1423 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1424 906c123b 2020-07-23 stsp goto done;
1427 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1428 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1429 c6e8a826 2021-04-05 stsp int symlink_replaced;
1430 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1431 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1432 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1433 c90c8ce3 2020-07-23 stsp goto done;
1435 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1436 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1438 f35fa46a 2020-07-23 stsp goto done;
1439 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1440 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1441 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1442 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1443 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1444 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1446 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1447 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1450 5a1fbc73 2020-07-23 stsp goto done; /* Nothing else to do. */
1453 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1454 f4994adc 2020-10-20 stsp char *parent;
1455 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1457 f4994adc 2020-10-20 stsp goto done;
1458 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1459 f4994adc 2020-10-20 stsp free(parent);
1461 f35fa46a 2020-07-23 stsp goto done;
1463 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1464 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1466 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1467 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1468 f35fa46a 2020-07-23 stsp goto done;
1472 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1473 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1474 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1475 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1476 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1477 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1478 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1479 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1480 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1481 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1482 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1483 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1484 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1486 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1487 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1489 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1490 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1491 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1493 906c123b 2020-07-23 stsp free(path_got);
1494 8ba819a3 2020-07-23 stsp return err;
1497 8ba819a3 2020-07-23 stsp static const struct got_error *
1498 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1499 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1500 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1501 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1502 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1503 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1505 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1506 507dc3bb 2018-12-29 stsp int fd = -1;
1507 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1508 507dc3bb 2018-12-29 stsp int update = 0;
1509 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1511 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1512 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
1513 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1514 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1515 f5375317 2020-10-20 stsp char *parent;
1516 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1518 f5375317 2020-10-20 stsp return err;
1519 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1520 f5375317 2020-10-20 stsp free(parent);
1522 21908da4 2019-01-13 stsp return err;
1523 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1524 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1525 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
1526 21908da4 2019-01-13 stsp if (fd == -1)
1527 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1528 230a42bd 2019-05-11 jcs ondisk_path);
1529 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1530 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1531 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1532 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1533 3b9f0f87 2020-07-23 stsp goto done;
1535 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1536 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1537 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1538 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1539 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1540 507dc3bb 2018-12-29 stsp goto done;
1542 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1543 507dc3bb 2018-12-29 stsp ondisk_path);
1545 507dc3bb 2018-12-29 stsp goto done;
1546 507dc3bb 2018-12-29 stsp update = 1;
1549 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1552 3818e3c4 2020-11-01 naddy if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1553 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod",
1554 3818e3c4 2020-11-01 naddy update ? tmppath : ondisk_path);
1555 3818e3c4 2020-11-01 naddy goto done;
1558 bd6aa359 2020-07-23 stsp if (progress_cb) {
1559 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1560 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1562 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1563 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1566 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1567 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1569 bd6aa359 2020-07-23 stsp goto done;
1572 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1574 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1575 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1578 9d31a1d8 2018-03-11 stsp if (len > 0) {
1579 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1580 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1581 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1582 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1583 b87c6f83 2018-12-24 stsp goto done;
1584 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1585 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1586 b87c6f83 2018-12-24 stsp goto done;
1588 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1590 9d31a1d8 2018-03-11 stsp } while (len != 0);
1592 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1593 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1594 816dc654 2019-02-16 stsp goto done;
1597 507dc3bb 2018-12-29 stsp if (update) {
1598 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1599 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1600 f6d8c0ac 2020-11-09 stsp goto done;
1602 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1603 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1604 230a42bd 2019-05-11 jcs ondisk_path);
1605 68ed9ba5 2019-02-10 stsp goto done;
1607 63df146d 2020-11-09 stsp free(tmppath);
1608 63df146d 2020-11-09 stsp tmppath = NULL;
1612 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1613 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1614 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1615 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1616 507dc3bb 2018-12-29 stsp free(tmppath);
1617 6353ad76 2019-02-08 stsp return err;
1620 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1621 6353ad76 2019-02-08 stsp static const struct got_error *
1622 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1624 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1625 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1626 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1627 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1628 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1630 7154f6ce 2019-03-27 stsp int i = 0;
1631 9bdd68dd 2020-01-02 naddy char *line = NULL;
1632 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
1633 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1635 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1636 9bdd68dd 2020-01-02 naddy linelen = getline(&line, &linesize, f);
1637 9bdd68dd 2020-01-02 naddy if (linelen == -1) {
1638 7154f6ce 2019-03-27 stsp if (feof(f))
1640 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1644 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1645 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1647 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1652 9bdd68dd 2020-01-02 naddy free(line);
1654 7154f6ce 2019-03-27 stsp return err;
1657 e2b1e152 2019-07-13 stsp static int
1658 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1660 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1661 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1664 1ebedb77 2019-10-19 stsp static int
1665 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1667 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1668 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1669 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1670 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1671 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1672 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1675 c363b2c1 2019-08-03 stsp static unsigned char
1676 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1678 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1679 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1680 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1681 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1682 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1683 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1684 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1686 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1690 a919d5c4 2020-07-23 stsp static const struct got_error *
1691 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1692 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1693 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1695 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1696 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1697 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1698 a919d5c4 2020-07-23 stsp ssize_t elen;
1699 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1700 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1701 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1703 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1705 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1707 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1709 a919d5c4 2020-07-23 stsp return err;
1710 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1712 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1713 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1715 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1717 a919d5c4 2020-07-23 stsp if (len > 0) {
1718 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1719 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1720 a919d5c4 2020-07-23 stsp len - hdrlen);
1721 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1722 a919d5c4 2020-07-23 stsp hdrlen = 0;
1724 a919d5c4 2020-07-23 stsp } while (len != 0);
1725 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1727 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1728 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1729 a919d5c4 2020-07-23 stsp if (elen == -1)
1730 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1732 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1733 a919d5c4 2020-07-23 stsp if (elen == -1)
1734 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1737 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1738 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1740 a919d5c4 2020-07-23 stsp return NULL;
1743 7154f6ce 2019-03-27 stsp static const struct got_error *
1744 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1745 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1746 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1748 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1749 6353ad76 2019-02-08 stsp struct got_object_id id;
1750 6353ad76 2019-02-08 stsp size_t hdrlen;
1751 1338848f 2019-12-13 stsp int fd = -1;
1752 6353ad76 2019-02-08 stsp FILE *f = NULL;
1753 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1754 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1755 6353ad76 2019-02-08 stsp size_t flen, blen;
1756 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1758 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1759 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1762 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1763 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1764 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1766 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1767 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1768 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1769 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1770 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1772 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1773 882ef1b9 2019-12-13 stsp goto done;
1775 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1776 3d35a492 2019-12-13 stsp goto done;
1779 7f91a133 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1780 a919d5c4 2020-07-23 stsp if (fd == -1 && errno != ENOENT && errno != ELOOP)
1781 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1782 a919d5c4 2020-07-23 stsp else if (fd == -1 && errno == ELOOP) {
1783 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1784 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1785 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1786 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1787 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1788 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1790 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1791 3d35a492 2019-12-13 stsp goto done;
1793 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1794 1338848f 2019-12-13 stsp goto done;
1798 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1799 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1800 1338848f 2019-12-13 stsp goto done;
1803 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1804 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1805 1338848f 2019-12-13 stsp goto done;
1806 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1807 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1808 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1809 1338848f 2019-12-13 stsp goto done;
1812 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1813 f179e66d 2020-07-23 stsp goto done;
1815 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1816 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1817 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1818 1338848f 2019-12-13 stsp goto done;
1821 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1822 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1823 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1825 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1827 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1829 1338848f 2019-12-13 stsp goto done;
1831 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1832 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1833 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1834 a919d5c4 2020-07-23 stsp goto done;
1837 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1838 3d35a492 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1839 ab0d4361 2019-12-13 stsp if (fd == -1) {
1840 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1841 ab0d4361 2019-12-13 stsp goto done;
1845 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1846 6353ad76 2019-02-08 stsp if (f == NULL) {
1847 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1848 6353ad76 2019-02-08 stsp goto done;
1851 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1853 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1854 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1856 7154f6ce 2019-03-27 stsp goto done;
1857 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1858 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1859 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1860 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1861 7154f6ce 2019-03-27 stsp goto done;
1863 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1864 6353ad76 2019-02-08 stsp if (flen != 0)
1865 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1867 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1868 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1869 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1871 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1872 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1873 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1874 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1878 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1881 6353ad76 2019-02-08 stsp hdrlen = 0;
1884 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1885 7154f6ce 2019-03-27 stsp rewind(f);
1886 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1887 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1888 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1891 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1892 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1893 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1894 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1895 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1896 9d31a1d8 2018-03-11 stsp return err;
1900 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1901 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1903 e2b1e152 2019-07-13 stsp static const struct got_error *
1904 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1905 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1907 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1908 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1909 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1911 e2b1e152 2019-07-13 stsp return NULL;
1914 9d31a1d8 2018-03-11 stsp static const struct got_error *
1915 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1916 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1917 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1918 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1919 0584f854 2019-04-06 stsp void *progress_arg)
1921 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1922 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1923 6353ad76 2019-02-08 stsp char *ondisk_path;
1924 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1925 b8f41171 2019-02-10 stsp struct stat sb;
1927 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1928 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1931 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1932 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1933 a76c42e6 2019-08-03 stsp goto done;
1935 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1938 abb4604f 2019-07-27 stsp goto done;
1939 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1940 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1942 abb4604f 2019-07-27 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1943 c90c8ce3 2020-07-23 stsp status = GOT_STATUS_UNVERSIONED;
1946 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1947 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1948 b8f41171 2019-02-10 stsp goto done;
1950 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1951 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1953 5036ab18 2020-04-18 stsp goto done;
1956 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1957 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1958 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1960 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1961 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1962 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1963 c6e8a826 2021-04-05 stsp * updating contents of this file.
1965 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1966 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1967 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1968 c6e8a826 2021-04-05 stsp /* Same commit. */
1969 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1970 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1972 e2b1e152 2019-07-13 stsp goto done;
1973 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1975 6353ad76 2019-02-08 stsp goto done;
1977 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1978 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1979 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1980 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
1981 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1982 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1984 0f58026f 2021-04-05 stsp goto done;
1985 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1987 b8f41171 2019-02-10 stsp goto done;
1991 56e0773d 2019-11-28 stsp err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1993 6353ad76 2019-02-08 stsp goto done;
1995 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1996 234035bc 2019-06-01 stsp int update_timestamps;
1997 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1998 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1999 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
2000 234035bc 2019-06-01 stsp struct got_object_id id2;
2001 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2002 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
2004 f69721c3 2019-10-21 stsp goto done;
2006 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
2007 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
2008 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2009 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
2010 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
2011 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
2012 f69721c3 2019-10-21 stsp goto done;
2014 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2015 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2016 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2017 234035bc 2019-06-01 stsp goto done;
2020 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2021 36bf999c 2020-07-23 stsp char *link_target;
2022 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
2024 36bf999c 2020-07-23 stsp goto done;
2025 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
2026 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
2027 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
2028 36bf999c 2020-07-23 stsp free(link_target);
2030 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
2031 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
2032 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
2033 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
2035 f69721c3 2019-10-21 stsp free(label_orig);
2036 234035bc 2019-06-01 stsp if (blob2)
2037 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
2039 909d120e 2019-09-22 stsp goto done;
2041 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
2042 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2043 234035bc 2019-06-01 stsp * unmodified files again.
2045 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2046 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2047 234035bc 2019-06-01 stsp update_timestamps);
2048 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2049 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2050 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2051 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2052 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2054 1ee397ad 2019-07-12 stsp goto done;
2055 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2056 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2058 13d9040b 2019-03-27 stsp goto done;
2060 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2061 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2062 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2063 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2064 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2065 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2066 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2068 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2069 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2070 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2071 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2072 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2075 13d9040b 2019-03-27 stsp goto done;
2078 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2079 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2080 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2082 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2083 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2084 054041d0 2020-06-24 stsp &blob->id);
2087 13d9040b 2019-03-27 stsp goto done;
2089 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2090 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2091 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2094 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2096 6353ad76 2019-02-08 stsp free(ondisk_path);
2097 8da9e5f4 2019-01-12 stsp return err;
2100 90285c3b 2019-01-08 stsp static const struct got_error *
2101 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2103 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2104 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
2106 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2107 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2109 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2110 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2111 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2113 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2115 fddefe3b 2020-10-20 stsp char *parent;
2116 fddefe3b 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
2119 fddefe3b 2020-10-20 stsp free(ondisk_path);
2120 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2121 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2122 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2123 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2124 fddefe3b 2020-10-20 stsp ondisk_path);
2127 fddefe3b 2020-10-20 stsp } while (got_path_cmp(ondisk_path, root_path,
2128 fddefe3b 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
2130 90285c3b 2019-01-08 stsp free(ondisk_path);
2131 90285c3b 2019-01-08 stsp return err;
2134 708d8e67 2019-03-27 stsp static const struct got_error *
2135 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2136 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2137 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2139 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2140 708d8e67 2019-03-27 stsp unsigned char status;
2141 708d8e67 2019-03-27 stsp struct stat sb;
2142 708d8e67 2019-03-27 stsp char *ondisk_path;
2144 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2145 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2147 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2149 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2151 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2153 5a58a424 2020-06-23 stsp goto done;
2155 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2156 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2157 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2158 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2159 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2160 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2161 993e2a1b 2020-07-23 stsp goto done;
2163 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2164 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2165 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2166 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2168 993e2a1b 2020-07-23 stsp goto done;
2169 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2170 993e2a1b 2020-07-23 stsp ie->path);
2171 993e2a1b 2020-07-23 stsp goto done;
2174 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2175 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2176 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2178 5a58a424 2020-06-23 stsp goto done;
2180 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2181 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2183 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2184 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2186 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2188 5a58a424 2020-06-23 stsp goto done;
2189 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2190 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2192 5a58a424 2020-06-23 stsp goto done;
2194 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2197 5a58a424 2020-06-23 stsp free(ondisk_path);
2198 fc6346c4 2019-03-27 stsp return err;
2201 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2202 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2203 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2204 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2205 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2206 8da9e5f4 2019-01-12 stsp void *progress_arg;
2207 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2208 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2211 512f0d0e 2019-01-02 stsp static const struct got_error *
2212 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2213 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2215 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2217 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2218 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2220 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2221 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2224 8da9e5f4 2019-01-12 stsp static const struct got_error *
2225 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2227 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2229 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2230 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2232 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2233 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2236 9d31a1d8 2018-03-11 stsp static const struct got_error *
2237 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2239 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2240 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2241 8da9e5f4 2019-01-12 stsp char *path;
2243 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2244 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2246 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2247 63c5ca5d 2019-08-24 stsp return NULL;
2249 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2250 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2252 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2254 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2255 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2257 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2258 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2260 8da9e5f4 2019-01-12 stsp free(path);
2261 0cd1c46a 2019-03-11 stsp return err;
2264 b2118c49 2020-07-28 stsp const struct got_error *
2265 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2267 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2269 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2270 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2271 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2272 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2275 b2118c49 2020-07-28 stsp return NULL;
2278 818c7501 2019-07-11 stsp static const struct got_error *
2279 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2281 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2282 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2284 517bab73 2019-03-11 stsp *refname = NULL;
2286 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2288 b2118c49 2020-07-28 stsp return err;
2290 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2291 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2292 0647c563 2019-03-11 stsp *refname = NULL;
2294 517bab73 2019-03-11 stsp free(uuidstr);
2295 517bab73 2019-03-11 stsp return err;
2298 818c7501 2019-07-11 stsp const struct got_error *
2299 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2301 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2304 818c7501 2019-07-11 stsp static const struct got_error *
2305 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2307 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2308 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2311 818c7501 2019-07-11 stsp static const struct got_error *
2312 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2314 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2317 818c7501 2019-07-11 stsp static const struct got_error *
2318 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2320 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2321 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2324 818c7501 2019-07-11 stsp static const struct got_error *
2325 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2327 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2328 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2331 0ebf8283 2019-07-24 stsp static const struct got_error *
2332 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2334 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2335 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2338 0ebf8283 2019-07-24 stsp static const struct got_error *
2339 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2341 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2342 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2345 0ebf8283 2019-07-24 stsp static const struct got_error *
2346 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2348 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2349 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2352 0ebf8283 2019-07-24 stsp static const struct got_error *
2353 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2355 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2356 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2359 0ebf8283 2019-07-24 stsp const struct got_error *
2360 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2361 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2363 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2364 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2365 0ebf8283 2019-07-24 stsp *path = NULL;
2366 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2368 0ebf8283 2019-07-24 stsp return NULL;
2372 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2373 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2375 517bab73 2019-03-11 stsp static const struct got_error *
2376 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2378 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2379 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2380 517bab73 2019-03-11 stsp char *refname;
2382 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2384 517bab73 2019-03-11 stsp return err;
2386 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2388 0cd1c46a 2019-03-11 stsp goto done;
2390 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2392 0cd1c46a 2019-03-11 stsp free(refname);
2394 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2395 3e3a69f1 2019-07-25 stsp return err;
2398 3e3a69f1 2019-07-25 stsp static const struct got_error *
2399 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2401 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2403 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2404 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2405 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2406 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2408 9d31a1d8 2018-03-11 stsp return err;
2412 ebf99748 2019-05-09 stsp static const struct got_error *
2413 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2414 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2416 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2417 ebf99748 2019-05-09 stsp FILE *index = NULL;
2419 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2420 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2421 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2422 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2424 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2426 ebf99748 2019-05-09 stsp goto done;
2428 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
2429 ebf99748 2019-05-09 stsp if (index == NULL) {
2430 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2431 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2433 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2434 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2435 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2438 ebf99748 2019-05-09 stsp if (err) {
2439 ebf99748 2019-05-09 stsp free(*fileindex_path);
2440 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2441 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2442 ebf99748 2019-05-09 stsp *fileindex = NULL;
2444 ebf99748 2019-05-09 stsp return err;
2447 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2448 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2449 c932eeeb 2019-05-22 stsp const char *path;
2450 c932eeeb 2019-05-22 stsp size_t path_len;
2451 c932eeeb 2019-05-22 stsp const char *entry_name;
2452 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2453 a484d721 2019-06-10 stsp void *progress_arg;
2456 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2457 c932eeeb 2019-05-22 stsp static const struct got_error *
2458 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2460 1ee397ad 2019-07-12 stsp const struct got_error *err;
2461 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2463 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2464 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2465 c932eeeb 2019-05-22 stsp return NULL;
2466 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2467 102ff934 2019-06-11 stsp return NULL;
2469 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2470 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2471 c932eeeb 2019-05-22 stsp return NULL;
2473 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2474 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2475 edd02c5e 2019-07-11 stsp ie->path);
2477 1ee397ad 2019-07-12 stsp return err;
2479 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2480 c932eeeb 2019-05-22 stsp return NULL;
2483 a615e0e7 2020-12-16 stsp static const struct got_error *
2484 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2485 a615e0e7 2020-12-16 stsp struct got_fileindex *fileindex,
2486 a615e0e7 2020-12-16 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2488 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2490 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2491 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2492 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2493 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2494 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2495 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2497 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2498 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2501 9c6338c4 2019-06-02 stsp static const struct got_error *
2502 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2504 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2505 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2506 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2507 867630bb 2020-01-17 stsp struct timespec timeout;
2509 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2510 9c6338c4 2019-06-02 stsp fileindex_path);
2512 9c6338c4 2019-06-02 stsp goto done;
2514 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2516 9c6338c4 2019-06-02 stsp goto done;
2518 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2519 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2520 9c6338c4 2019-06-02 stsp fileindex_path);
2521 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2525 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2526 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2527 867630bb 2020-01-17 stsp * was recorded in the file index.
2529 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2530 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2531 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2533 9c6338c4 2019-06-02 stsp if (new_index)
2534 9c6338c4 2019-06-02 stsp fclose(new_index);
2535 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2536 9c6338c4 2019-06-02 stsp return err;
2539 6ced4176 2019-07-12 stsp static const struct got_error *
2540 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2541 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2542 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
2544 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2545 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2546 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2547 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2549 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2550 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2551 6ced4176 2019-07-12 stsp *tree_id = NULL;
2553 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2554 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2555 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2556 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2557 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2558 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2559 6ced4176 2019-07-12 stsp goto done;
2561 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2562 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
2564 6ced4176 2019-07-12 stsp goto done;
2565 6ced4176 2019-07-12 stsp return NULL;
2568 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2570 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2571 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2572 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2573 6ced4176 2019-07-12 stsp goto done;
2576 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2577 6ced4176 2019-07-12 stsp in_repo_path);
2579 6ced4176 2019-07-12 stsp goto done;
2581 6ced4176 2019-07-12 stsp free(in_repo_path);
2582 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2584 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2586 6ced4176 2019-07-12 stsp goto done;
2588 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2589 6ced4176 2019-07-12 stsp /* Check out a single file. */
2590 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2591 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2592 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2593 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2594 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2595 6ced4176 2019-07-12 stsp goto done;
2597 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2598 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2599 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2600 6ced4176 2019-07-12 stsp goto done;
2603 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2604 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2606 6ced4176 2019-07-12 stsp return err;
2607 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2608 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2609 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2610 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2611 6ced4176 2019-07-12 stsp goto done;
2614 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2615 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
2617 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2618 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2619 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2620 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2621 6ced4176 2019-07-12 stsp goto done;
2623 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2624 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2625 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2626 6ced4176 2019-07-12 stsp goto done;
2631 6ced4176 2019-07-12 stsp free(in_repo_path);
2632 6ced4176 2019-07-12 stsp if (err) {
2633 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2634 6ced4176 2019-07-12 stsp free(*tree_relpath);
2635 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2636 6ced4176 2019-07-12 stsp free(*tree_id);
2637 6ced4176 2019-07-12 stsp *tree_id = NULL;
2639 07ed472d 2019-07-12 stsp return err;
2642 07ed472d 2019-07-12 stsp static const struct got_error *
2643 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2644 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2645 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2646 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2648 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2649 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2650 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2651 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2652 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2654 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2655 7f47418f 2019-12-20 stsp if (err) {
2656 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2657 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2658 7f47418f 2019-12-20 stsp goto done;
2659 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2660 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2662 7f47418f 2019-12-20 stsp return err;
2665 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2666 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2668 07ed472d 2019-07-12 stsp goto done;
2670 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2672 07ed472d 2019-07-12 stsp goto done;
2674 07ed472d 2019-07-12 stsp if (entry_name &&
2675 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2676 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2677 07ed472d 2019-07-12 stsp goto done;
2680 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2681 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2682 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2683 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2684 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2685 07ed472d 2019-07-12 stsp arg.repo = repo;
2686 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2687 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2688 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2689 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2690 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2691 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2694 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2695 07ed472d 2019-07-12 stsp if (commit)
2696 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2697 6ced4176 2019-07-12 stsp return err;
2700 9d31a1d8 2018-03-11 stsp const struct got_error *
2701 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2702 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2703 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2704 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2706 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2707 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2708 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2709 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2710 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2711 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2712 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2713 f2ea84fa 2019-07-27 stsp SIMPLEQ_ENTRY(tree_path_data) entry;
2714 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2715 f2ea84fa 2019-07-27 stsp int entry_type;
2716 f2ea84fa 2019-07-27 stsp char *relpath;
2717 f2ea84fa 2019-07-27 stsp char *entry_name;
2718 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2719 f2ea84fa 2019-07-27 stsp SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2721 f2ea84fa 2019-07-27 stsp SIMPLEQ_INIT(&tree_paths);
2723 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2725 9d31a1d8 2018-03-11 stsp return err;
2727 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2728 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2729 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2730 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2731 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2732 f2ea84fa 2019-07-27 stsp goto done;
2735 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2736 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2737 f2ea84fa 2019-07-27 stsp if (err) {
2738 f2ea84fa 2019-07-27 stsp free(tpd);
2739 6ced4176 2019-07-12 stsp goto done;
2742 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2743 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2744 f2ea84fa 2019-07-27 stsp if (err) {
2745 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2746 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2747 f2ea84fa 2019-07-27 stsp free(tpd);
2748 f2ea84fa 2019-07-27 stsp goto done;
2751 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2753 f2ea84fa 2019-07-27 stsp SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2757 51514078 2018-12-25 stsp * Read the file index.
2758 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2759 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2761 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2763 c4cdcb68 2019-04-03 stsp goto done;
2765 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2766 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2767 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2769 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2770 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2771 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2775 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2776 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2777 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2778 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2779 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2780 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2781 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2782 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2786 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_NEXT(tpd, entry);
2788 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2789 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2790 af12c6b9 2019-06-04 stsp err = sync_err;
2792 9c6338c4 2019-06-02 stsp free(fileindex_path);
2794 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2795 9d31a1d8 2018-03-11 stsp if (commit)
2796 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2797 5ade8233 2019-07-12 stsp if (fileindex)
2798 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2799 f2ea84fa 2019-07-27 stsp while (!SIMPLEQ_EMPTY(&tree_paths)) {
2800 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2801 f2ea84fa 2019-07-27 stsp SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2802 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2803 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2804 f2ea84fa 2019-07-27 stsp free(tpd);
2806 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2807 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2808 9d31a1d8 2018-03-11 stsp err = unlockerr;
2809 f8d1f275 2019-02-04 stsp return err;
2812 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2813 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2814 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2815 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2816 234035bc 2019-06-01 stsp void *progress_arg;
2817 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2818 234035bc 2019-06-01 stsp void *cancel_arg;
2819 f69721c3 2019-10-21 stsp const char *label_orig;
2820 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2823 234035bc 2019-06-01 stsp static const struct got_error *
2824 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2825 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
2826 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
2827 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2829 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2830 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2831 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2832 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2833 234035bc 2019-06-01 stsp struct stat sb;
2834 234035bc 2019-06-01 stsp unsigned char status;
2835 234035bc 2019-06-01 stsp int local_changes_subsumed;
2836 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2837 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2839 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2840 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2841 d6c87207 2019-08-02 stsp strlen(path2));
2842 1ee397ad 2019-07-12 stsp if (ie == NULL)
2843 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2844 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2846 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2847 234035bc 2019-06-01 stsp path2) == -1)
2848 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2850 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2853 234035bc 2019-06-01 stsp goto done;
2855 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2856 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2857 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2858 234035bc 2019-06-01 stsp goto done;
2860 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2861 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2862 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2863 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2864 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2865 234035bc 2019-06-01 stsp goto done;
2868 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2869 36bf999c 2020-07-23 stsp char *link_target2;
2870 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2872 36bf999c 2020-07-23 stsp goto done;
2873 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2874 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2875 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2876 36bf999c 2020-07-23 stsp free(link_target2);
2880 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
2881 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
2882 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
2883 1af628f4 2021-06-03 stsp goto done;
2885 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2886 1af628f4 2021-06-03 stsp f_orig, blob1);
2888 1af628f4 2021-06-03 stsp goto done;
2890 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
2891 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
2892 1af628f4 2021-06-03 stsp goto done;
2893 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2894 54d5be07 2021-06-03 stsp f_deriv2, blob2);
2896 1af628f4 2021-06-03 stsp goto done;
2898 1af628f4 2021-06-03 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
2899 1af628f4 2021-06-03 stsp if (fd == -1) {
2900 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
2901 1af628f4 2021-06-03 stsp ondisk_path);
2902 1af628f4 2021-06-03 stsp goto done;
2904 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
2905 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
2906 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
2907 1af628f4 2021-06-03 stsp ondisk_path);
2908 1af628f4 2021-06-03 stsp close(fd);
2909 1af628f4 2021-06-03 stsp goto done;
2911 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
2913 1af628f4 2021-06-03 stsp goto done;
2914 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
2915 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2916 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
2917 1af628f4 2021-06-03 stsp goto done;
2919 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
2920 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2921 54d5be07 2021-06-03 stsp sb.st_mode, a->label_orig, NULL, label_deriv2,
2922 54d5be07 2021-06-03 stsp repo, a->progress_cb, a->progress_arg);
2924 234035bc 2019-06-01 stsp } else if (blob1) {
2925 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2926 d6c87207 2019-08-02 stsp strlen(path1));
2927 1ee397ad 2019-07-12 stsp if (ie == NULL)
2928 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2929 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2931 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2932 2b92fad7 2019-06-02 stsp path1) == -1)
2933 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2935 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2938 2b92fad7 2019-06-02 stsp goto done;
2940 2b92fad7 2019-06-02 stsp switch (status) {
2941 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2942 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2943 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2945 1ee397ad 2019-07-12 stsp goto done;
2946 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2948 2b92fad7 2019-06-02 stsp goto done;
2950 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2952 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2953 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2954 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2955 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2957 1ee397ad 2019-07-12 stsp goto done;
2959 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2961 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
2962 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
2963 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
2965 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
2966 0a22ca1a 2020-09-23 stsp * exists in the repository.
2968 0a22ca1a 2020-09-23 stsp err = got_object_blob_file_create(&id, &blob1_f, path1);
2970 0a22ca1a 2020-09-23 stsp goto done;
2971 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
2972 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2973 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
2975 0a22ca1a 2020-09-23 stsp goto done;
2976 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
2979 0a22ca1a 2020-09-23 stsp goto done;
2981 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
2984 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2985 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
2987 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
2988 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
2991 0a22ca1a 2020-09-23 stsp goto done;
2994 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2995 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2996 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2997 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2999 1ee397ad 2019-07-12 stsp goto done;
3001 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
3002 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
3004 1ee397ad 2019-07-12 stsp goto done;
3009 234035bc 2019-06-01 stsp } else if (blob2) {
3010 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3011 234035bc 2019-06-01 stsp path2) == -1)
3012 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3013 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3014 d6c87207 2019-08-02 stsp strlen(path2));
3016 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
3017 7f91a133 2019-12-13 stsp -1, NULL, repo);
3019 234035bc 2019-06-01 stsp goto done;
3020 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3021 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3022 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3023 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
3024 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3025 1ee397ad 2019-07-12 stsp status, path2);
3026 234035bc 2019-06-01 stsp goto done;
3028 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3029 36bf999c 2020-07-23 stsp char *link_target2;
3030 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3033 36bf999c 2020-07-23 stsp goto done;
3034 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3035 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3036 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3037 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3038 36bf999c 2020-07-23 stsp free(link_target2);
3039 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3040 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3041 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3042 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3043 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3044 526a746f 2020-07-23 stsp a->progress_arg);
3046 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3047 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3050 dfe9fba0 2020-07-23 stsp goto done;
3051 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3052 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
3053 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, blob2->id.sha1,
3054 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
3056 234035bc 2019-06-01 stsp goto done;
3059 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
3060 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
3061 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
3062 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
3063 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
3064 c90c8ce3 2020-07-23 stsp 0, 1, repo, a->progress_cb, a->progress_arg);
3066 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
3067 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3068 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
3071 234035bc 2019-06-01 stsp goto done;
3072 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
3074 234035bc 2019-06-01 stsp goto done;
3075 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
3076 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, NULL, NULL, 1);
3077 3969253a 2020-03-07 stsp if (err) {
3078 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3079 3969253a 2020-03-07 stsp goto done;
3081 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3082 2b92fad7 2019-06-02 stsp if (err) {
3083 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
3084 2b92fad7 2019-06-02 stsp goto done;
3086 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
3087 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
3088 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
3093 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3094 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3095 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3096 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3097 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3098 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3099 1af628f4 2021-06-03 stsp free(id_str);
3100 54d5be07 2021-06-03 stsp free(label_deriv2);
3101 234035bc 2019-06-01 stsp free(ondisk_path);
3102 234035bc 2019-06-01 stsp return err;
3105 234035bc 2019-06-01 stsp struct check_merge_ok_arg {
3106 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3107 234035bc 2019-06-01 stsp struct got_repository *repo;
3110 234035bc 2019-06-01 stsp static const struct got_error *
3111 234035bc 2019-06-01 stsp check_merge_ok(void *arg, struct got_fileindex_entry *ie)
3113 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3114 234035bc 2019-06-01 stsp struct check_merge_ok_arg *a = arg;
3115 234035bc 2019-06-01 stsp unsigned char status;
3116 234035bc 2019-06-01 stsp struct stat sb;
3117 234035bc 2019-06-01 stsp char *ondisk_path;
3119 234035bc 2019-06-01 stsp /* Reject merges into a work tree with mixed base commits. */
3120 234035bc 2019-06-01 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3121 234035bc 2019-06-01 stsp SHA1_DIGEST_LENGTH))
3122 234035bc 2019-06-01 stsp return got_error(GOT_ERR_MIXED_COMMITS);
3124 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3126 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3128 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3129 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3131 234035bc 2019-06-01 stsp return err;
3132 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3133 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3135 234035bc 2019-06-01 stsp return NULL;
3138 818c7501 2019-07-11 stsp static const struct got_error *
3139 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3140 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3141 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3142 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3143 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3145 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3146 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3147 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3148 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3149 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3151 03415a1a 2019-06-02 stsp if (commit_id1) {
3152 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3153 03415a1a 2019-06-02 stsp worktree->path_prefix);
3154 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3155 03415a1a 2019-06-02 stsp goto done;
3157 69d57f3d 2020-07-31 stsp if (tree_id1) {
3158 69d57f3d 2020-07-31 stsp char *id_str;
3160 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3162 03415a1a 2019-06-02 stsp goto done;
3164 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3166 f69721c3 2019-10-21 stsp goto done;
3168 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3169 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3170 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3171 f69721c3 2019-10-21 stsp free(id_str);
3172 f69721c3 2019-10-21 stsp goto done;
3174 f69721c3 2019-10-21 stsp free(id_str);
3177 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3178 234035bc 2019-06-01 stsp worktree->path_prefix);
3180 234035bc 2019-06-01 stsp goto done;
3182 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3184 234035bc 2019-06-01 stsp goto done;
3186 234035bc 2019-06-01 stsp arg.worktree = worktree;
3187 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3188 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3189 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3190 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3191 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3192 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3193 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3194 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3195 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3196 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3197 af12c6b9 2019-06-04 stsp err = sync_err;
3199 234035bc 2019-06-01 stsp if (tree1)
3200 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3201 234035bc 2019-06-01 stsp if (tree2)
3202 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3203 f69721c3 2019-10-21 stsp free(label_orig);
3204 818c7501 2019-07-11 stsp return err;
3207 818c7501 2019-07-11 stsp const struct got_error *
3208 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3209 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3210 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3211 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3213 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3214 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3215 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3216 818c7501 2019-07-11 stsp struct check_merge_ok_arg mok_arg;
3218 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3220 818c7501 2019-07-11 stsp return err;
3222 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3224 818c7501 2019-07-11 stsp goto done;
3226 818c7501 2019-07-11 stsp mok_arg.worktree = worktree;
3227 818c7501 2019-07-11 stsp mok_arg.repo = repo;
3228 818c7501 2019-07-11 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3229 818c7501 2019-07-11 stsp &mok_arg);
3231 818c7501 2019-07-11 stsp goto done;
3233 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3234 818c7501 2019-07-11 stsp commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3236 818c7501 2019-07-11 stsp if (fileindex)
3237 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3238 818c7501 2019-07-11 stsp free(fileindex_path);
3239 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3240 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3241 234035bc 2019-06-01 stsp err = unlockerr;
3242 234035bc 2019-06-01 stsp return err;
3245 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3246 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3247 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3248 927df6b7 2019-02-10 stsp const char *status_path;
3249 927df6b7 2019-02-10 stsp size_t status_path_len;
3250 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3251 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3252 f8d1f275 2019-02-04 stsp void *status_arg;
3253 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3254 f8d1f275 2019-02-04 stsp void *cancel_arg;
3255 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3256 6841da00 2019-08-08 stsp struct got_pathlist_head ignores;
3257 f2a9dc41 2019-12-13 tracey int report_unchanged;
3258 3143d852 2020-06-25 stsp int no_ignores;
3261 f8d1f275 2019-02-04 stsp static const struct got_error *
3262 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3263 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3264 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3265 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3267 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3268 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3269 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3270 927df6b7 2019-02-10 stsp struct stat sb;
3271 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3272 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3273 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3275 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3277 98eaaa12 2019-08-03 stsp return err;
3279 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3280 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3281 98eaaa12 2019-08-03 stsp return NULL;
3283 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3284 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3285 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3287 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3288 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3289 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3291 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3292 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3293 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3294 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3295 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3298 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3299 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3302 927df6b7 2019-02-10 stsp static const struct got_error *
3303 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3304 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3306 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3307 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3308 f8d1f275 2019-02-04 stsp char *abspath;
3310 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3311 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3313 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3314 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3315 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3316 927df6b7 2019-02-10 stsp return NULL;
3318 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3319 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3320 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3321 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3323 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3324 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3325 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3328 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3329 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3330 f8d1f275 2019-02-04 stsp free(abspath);
3331 9d31a1d8 2018-03-11 stsp return err;
3334 f8d1f275 2019-02-04 stsp static const struct got_error *
3335 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3337 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3338 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3339 2ec1f75b 2019-03-26 stsp unsigned char status;
3341 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3342 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3344 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3345 927df6b7 2019-02-10 stsp return NULL;
3347 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3348 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3349 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3350 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3352 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3353 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3354 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3358 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3360 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3362 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3363 6841da00 2019-08-08 stsp free((char *)pe->path);
3364 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3368 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3370 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3372 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3373 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3374 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3375 6841da00 2019-08-08 stsp free((char *)pe->path);
3377 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3380 6841da00 2019-08-08 stsp static const struct got_error *
3381 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3383 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3384 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3385 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3386 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3387 6841da00 2019-08-08 stsp size_t linesize = 0;
3388 6841da00 2019-08-08 stsp ssize_t linelen;
3390 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3391 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3392 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3393 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3395 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3396 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3397 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3399 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3400 bd8de430 2019-10-04 stsp if (line[0] == '#')
3403 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3404 bd8de430 2019-10-04 stsp if (line[0] == '!')
3407 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3408 6841da00 2019-08-08 stsp line) == -1) {
3409 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3410 6841da00 2019-08-08 stsp goto done;
3412 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3414 6841da00 2019-08-08 stsp goto done;
3416 6841da00 2019-08-08 stsp if (ferror(f)) {
3417 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3418 6841da00 2019-08-08 stsp goto done;
3421 6841da00 2019-08-08 stsp dirpath = strdup(path);
3422 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3423 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3424 6841da00 2019-08-08 stsp goto done;
3426 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3428 6841da00 2019-08-08 stsp free(line);
3429 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3430 6841da00 2019-08-08 stsp free(dirpath);
3431 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3433 6841da00 2019-08-08 stsp return err;
3437 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3439 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3441 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3442 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3443 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3444 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3446 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3447 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3449 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3451 bd8de430 2019-10-04 stsp pattern += 3;
3453 bd8de430 2019-10-04 stsp while (*p) {
3454 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3455 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3456 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3457 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3459 bd8de430 2019-10-04 stsp while (*p == '/')
3469 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3470 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3471 6841da00 2019-08-08 stsp * ignores backwards.
3473 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3474 6841da00 2019-08-08 stsp while (pe) {
3475 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3476 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3477 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3478 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3479 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3480 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3481 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3482 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3483 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3488 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3494 6841da00 2019-08-08 stsp static const struct got_error *
3495 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3496 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3498 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3499 6841da00 2019-08-08 stsp char *ignorespath;
3500 886cec17 2019-12-15 stsp int fd = -1;
3501 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3503 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3504 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3505 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3507 886cec17 2019-12-15 stsp if (dirfd != -1) {
3508 886cec17 2019-12-15 stsp fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3509 886cec17 2019-12-15 stsp if (fd == -1) {
3510 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3511 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3512 886cec17 2019-12-15 stsp ignorespath);
3514 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3515 886cec17 2019-12-15 stsp if (ignoresfile == NULL)
3516 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3517 886cec17 2019-12-15 stsp ignorespath);
3520 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3524 886cec17 2019-12-15 stsp ignoresfile = fopen(ignorespath, "r");
3525 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3526 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3527 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3528 886cec17 2019-12-15 stsp ignorespath);
3530 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3533 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3534 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3535 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3536 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3537 6841da00 2019-08-08 stsp free(ignorespath);
3538 6841da00 2019-08-08 stsp return err;
3541 f8d1f275 2019-02-04 stsp static const struct got_error *
3542 7f91a133 2019-12-13 stsp status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3544 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3545 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3546 f8d1f275 2019-02-04 stsp char *path = NULL;
3548 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3549 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3551 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3552 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3553 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3555 f8d1f275 2019-02-04 stsp path = de->d_name;
3558 3143d852 2020-06-25 stsp if (de->d_type != DT_DIR &&
3559 3143d852 2020-06-25 stsp got_path_is_child(path, a->status_path, a->status_path_len)
3560 6841da00 2019-08-08 stsp && !match_ignores(&a->ignores, path))
3561 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3562 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3563 f8d1f275 2019-02-04 stsp if (parent_path[0])
3564 f8d1f275 2019-02-04 stsp free(path);
3565 b72f483a 2019-02-05 stsp return err;
3568 347d1d3e 2019-07-12 stsp static const struct got_error *
3569 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3571 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3572 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3574 3143d852 2020-06-25 stsp if (a->no_ignores)
3575 3143d852 2020-06-25 stsp return NULL;
3577 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path,
3578 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3580 3143d852 2020-06-25 stsp return err;
3582 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path, path,
3583 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3585 3143d852 2020-06-25 stsp return err;
3588 3143d852 2020-06-25 stsp static const struct got_error *
3589 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3590 abb4604f 2019-07-27 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3591 f2a9dc41 2019-12-13 tracey void *status_arg, struct got_repository *repo, int report_unchanged)
3593 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3594 abb4604f 2019-07-27 stsp struct stat sb;
3596 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3598 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3599 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3601 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3602 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3603 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3604 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3605 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3606 abb4604f 2019-07-27 stsp return NULL;
3609 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3610 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3611 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3613 abb4604f 2019-07-27 stsp return NULL;
3616 3143d852 2020-06-25 stsp static const struct got_error *
3617 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3618 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3620 3143d852 2020-06-25 stsp const struct got_error *err;
3621 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3623 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3624 3143d852 2020-06-25 stsp ".cvsignore");
3626 3143d852 2020-06-25 stsp return err;
3628 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3629 3143d852 2020-06-25 stsp ".gitignore");
3631 3143d852 2020-06-25 stsp return err;
3633 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3634 3143d852 2020-06-25 stsp if (err) {
3635 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3636 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3637 3143d852 2020-06-25 stsp return err;
3639 3143d852 2020-06-25 stsp for (;;) {
3640 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3641 3143d852 2020-06-25 stsp ".cvsignore");
3644 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3645 3143d852 2020-06-25 stsp ".gitignore");
3648 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3649 3143d852 2020-06-25 stsp if (err) {
3650 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3651 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3654 b737c85a 2020-06-26 stsp free(parent_path);
3655 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3656 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3659 b737c85a 2020-06-26 stsp free(parent_path);
3660 b737c85a 2020-06-26 stsp free(next_parent_path);
3661 3143d852 2020-06-25 stsp return err;
3664 abb4604f 2019-07-27 stsp static const struct got_error *
3665 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3666 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3667 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3668 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3669 f2a9dc41 2019-12-13 tracey int report_unchanged)
3671 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3672 6fc93f37 2019-12-13 stsp int fd = -1;
3673 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3674 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3675 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3677 3143d852 2020-06-25 stsp TAILQ_INIT(&arg.ignores);
3679 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3680 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3681 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3683 6fc93f37 2019-12-13 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3684 6fc93f37 2019-12-13 stsp if (fd == -1) {
3685 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3686 00bb5ea0 2020-07-23 stsp errno != ELOOP)
3687 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3689 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3690 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3691 f2a9dc41 2019-12-13 tracey report_unchanged);
3693 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3694 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3695 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3696 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3697 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3698 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3699 abb4604f 2019-07-27 stsp arg.status_path = path;
3700 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3701 abb4604f 2019-07-27 stsp arg.repo = repo;
3702 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3703 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3704 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3705 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3706 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3707 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3708 022fae89 2019-12-06 tracey if (!no_ignores) {
3709 3143d852 2020-06-25 stsp err = add_ignores_from_parent_paths(&arg.ignores,
3710 3143d852 2020-06-25 stsp worktree->root_path, path);
3712 3143d852 2020-06-25 stsp goto done;
3714 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3715 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3718 3143d852 2020-06-25 stsp free_ignores(&arg.ignores);
3719 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3720 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3721 927df6b7 2019-02-10 stsp free(ondisk_path);
3722 347d1d3e 2019-07-12 stsp return err;
3725 347d1d3e 2019-07-12 stsp const struct got_error *
3726 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3727 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3728 72ea6654 2019-07-27 stsp got_worktree_status_cb status_cb, void *status_arg,
3729 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3731 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3732 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3733 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3734 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3736 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3738 347d1d3e 2019-07-12 stsp return err;
3740 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3741 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3742 f2a9dc41 2019-12-13 tracey status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3746 f8d1f275 2019-02-04 stsp free(fileindex_path);
3747 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3748 f8d1f275 2019-02-04 stsp return err;
3751 6c7ab921 2019-03-18 stsp const struct got_error *
3752 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3753 6c7ab921 2019-03-18 stsp const char *arg)
3755 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3756 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3757 6c7ab921 2019-03-18 stsp size_t len;
3758 00bb5ea0 2020-07-23 stsp struct stat sb;
3759 01740607 2020-11-04 stsp char *abspath = NULL;
3760 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3762 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3764 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3765 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3766 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3768 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3769 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3770 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3771 00bb5ea0 2020-07-23 stsp goto done;
3773 727173c3 2020-11-06 stsp sb.st_mode = 0;
3775 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3777 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3778 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3779 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3780 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3782 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3783 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3784 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3785 00bb5ea0 2020-07-23 stsp goto done;
3789 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3790 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3792 d0710d08 2019-07-22 stsp goto done;
3793 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3794 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3795 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3796 00bb5ea0 2020-07-23 stsp goto done;
3799 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3800 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3801 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3802 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3803 00bb5ea0 2020-07-23 stsp goto done;
3805 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3806 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3807 01740607 2020-11-04 stsp goto done;
3809 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
3810 01740607 2020-11-04 stsp sizeof(canonpath));
3812 00bb5ea0 2020-07-23 stsp goto done;
3813 01740607 2020-11-04 stsp resolved = strdup(canonpath);
3814 01740607 2020-11-04 stsp if (resolved == NULL) {
3815 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
3816 01740607 2020-11-04 stsp goto done;
3821 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3822 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3823 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3824 6c7ab921 2019-03-18 stsp goto done;
3827 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3828 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3829 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3831 f6d88e1a 2019-05-29 stsp goto done;
3833 f6d88e1a 2019-05-29 stsp path = strdup("");
3834 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3835 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3836 f6d88e1a 2019-05-29 stsp goto done;
3840 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3841 6c7ab921 2019-03-18 stsp len = strlen(path);
3842 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3843 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3847 01740607 2020-11-04 stsp free(abspath);
3848 6c7ab921 2019-03-18 stsp free(resolved);
3849 d0710d08 2019-07-22 stsp free(cwd);
3850 6c7ab921 2019-03-18 stsp if (err == NULL)
3851 6c7ab921 2019-03-18 stsp *wt_path = path;
3853 6c7ab921 2019-03-18 stsp free(path);
3854 d00136be 2019-03-26 stsp return err;
3857 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3858 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3859 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3860 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3861 4e68cba3 2019-11-23 stsp void *progress_arg;
3862 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3865 4e68cba3 2019-11-23 stsp static const struct got_error *
3866 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3867 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3868 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3869 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3871 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3872 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3873 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3874 6d022e97 2019-08-04 stsp struct stat sb;
3875 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3877 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3878 dbb83fbd 2019-12-12 stsp relpath) == -1)
3879 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3881 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3883 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3884 12463d8b 2019-12-13 stsp de_name, a->repo);
3886 dbb83fbd 2019-12-12 stsp goto done;
3887 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3888 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3889 dbb83fbd 2019-12-12 stsp goto done;
3890 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3892 dbb83fbd 2019-12-12 stsp goto done;
3895 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3896 dbb83fbd 2019-12-12 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3897 dbb83fbd 2019-12-12 stsp goto done;
3900 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3902 4e68cba3 2019-11-23 stsp goto done;
3903 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3904 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
3905 3969253a 2020-03-07 stsp if (err) {
3906 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3907 3969253a 2020-03-07 stsp goto done;
3909 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3910 07f5b47a 2019-06-02 stsp if (err) {
3911 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
3912 4e68cba3 2019-11-23 stsp goto done;
3915 dbb83fbd 2019-12-12 stsp free(ondisk_path);
3917 dbb83fbd 2019-12-12 stsp return err;
3918 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
3919 dbb83fbd 2019-12-12 stsp return NULL;
3920 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3923 d00136be 2019-03-26 stsp const struct got_error *
3924 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
3925 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
3926 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3927 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
3929 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3930 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
3931 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3932 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
3933 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
3935 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3937 d00136be 2019-03-26 stsp return err;
3939 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3941 d00136be 2019-03-26 stsp goto done;
3943 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
3944 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
3945 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
3946 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
3947 4e68cba3 2019-11-23 stsp saa.repo = repo;
3949 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3950 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3951 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3955 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3956 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3957 af12c6b9 2019-06-04 stsp err = sync_err;
3959 fb399478 2019-07-12 stsp free(fileindex_path);
3960 d00136be 2019-03-26 stsp if (fileindex)
3961 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
3962 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3963 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
3964 d00136be 2019-03-26 stsp err = unlockerr;
3965 6c7ab921 2019-03-18 stsp return err;
3968 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
3969 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
3970 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
3971 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
3972 f2a9dc41 2019-12-13 tracey void *progress_arg;
3973 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
3974 f2a9dc41 2019-12-13 tracey int delete_local_mods;
3975 70e3e7f5 2019-12-13 tracey int keep_on_disk;
3976 766841c2 2020-08-13 stsp const char *status_codes;
3979 f2a9dc41 2019-12-13 tracey static const struct got_error *
3980 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
3981 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
3982 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3983 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
3985 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
3986 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
3987 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
3988 17ed4618 2019-06-02 stsp struct stat sb;
3989 20a2ad1c 2020-10-20 stsp char *ondisk_path;
3991 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3992 17ed4618 2019-06-02 stsp if (ie == NULL)
3993 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
3995 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
3996 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
3997 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3998 9acbc4fa 2019-08-03 stsp return NULL;
3999 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4002 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4003 f2a9dc41 2019-12-13 tracey relpath) == -1)
4004 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4006 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4009 f2a9dc41 2019-12-13 tracey goto done;
4011 766841c2 2020-08-13 stsp if (a->status_codes) {
4012 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4014 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4015 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4018 766841c2 2020-08-13 stsp if (i == ncodes) {
4019 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4020 766841c2 2020-08-13 stsp free(ondisk_path);
4021 766841c2 2020-08-13 stsp return NULL;
4023 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4024 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4025 766841c2 2020-08-13 stsp static char msg[64];
4026 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4027 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4028 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4029 766841c2 2020-08-13 stsp goto done;
4033 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4034 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4035 f2a9dc41 2019-12-13 tracey goto done;
4036 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4037 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4038 f2a9dc41 2019-12-13 tracey goto done;
4040 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4041 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4042 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4043 f2a9dc41 2019-12-13 tracey goto done;
4047 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4048 20a2ad1c 2020-10-20 stsp size_t root_len;
4050 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4051 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
4052 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4053 12463d8b 2019-12-13 stsp ondisk_path);
4054 12463d8b 2019-12-13 stsp goto done;
4056 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
4057 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4058 12463d8b 2019-12-13 stsp goto done;
4061 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4063 20a2ad1c 2020-10-20 stsp char *parent;
4064 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4066 2513f20a 2020-10-20 stsp goto done;
4067 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4068 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4069 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4070 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4071 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4072 20a2ad1c 2020-10-20 stsp ondisk_path);
4075 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4076 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4079 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4081 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4082 f2a9dc41 2019-12-13 tracey if (err)
4083 f2a9dc41 2019-12-13 tracey return err;
4084 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4085 f2a9dc41 2019-12-13 tracey return NULL;
4086 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4087 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4090 2ec1f75b 2019-03-26 stsp const struct got_error *
4091 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4092 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4093 766841c2 2020-08-13 stsp const char *status_codes,
4094 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4095 70e3e7f5 2019-12-13 tracey struct got_repository *repo, int keep_on_disk)
4097 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4098 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4099 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4100 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4101 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4103 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4105 2ec1f75b 2019-03-26 stsp return err;
4107 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4109 2ec1f75b 2019-03-26 stsp goto done;
4111 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4112 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4113 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4114 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4115 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4116 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4117 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4118 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4120 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4121 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4122 f2a9dc41 2019-12-13 tracey schedule_for_deletion, &sda, NULL, NULL, 0, 1);
4126 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4127 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4128 af12c6b9 2019-06-04 stsp err = sync_err;
4130 fb399478 2019-07-12 stsp free(fileindex_path);
4131 2ec1f75b 2019-03-26 stsp if (fileindex)
4132 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4133 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4134 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4135 2ec1f75b 2019-03-26 stsp err = unlockerr;
4136 33aa809d 2019-08-08 stsp return err;
4139 33aa809d 2019-08-08 stsp static const struct got_error *
4140 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4142 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4143 33aa809d 2019-08-08 stsp char *line = NULL;
4144 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4145 33aa809d 2019-08-08 stsp ssize_t linelen;
4147 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4148 33aa809d 2019-08-08 stsp if (linelen == -1) {
4149 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4150 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4151 33aa809d 2019-08-08 stsp goto done;
4153 33aa809d 2019-08-08 stsp return NULL;
4155 33aa809d 2019-08-08 stsp if (outfile) {
4156 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4157 33aa809d 2019-08-08 stsp if (n != linelen) {
4158 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4159 33aa809d 2019-08-08 stsp goto done;
4162 33aa809d 2019-08-08 stsp if (rejectfile) {
4163 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4164 33aa809d 2019-08-08 stsp if (n != linelen)
4165 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4168 33aa809d 2019-08-08 stsp free(line);
4169 2ec1f75b 2019-03-26 stsp return err;
4172 33aa809d 2019-08-08 stsp static const struct got_error *
4173 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4175 33aa809d 2019-08-08 stsp char *line = NULL;
4176 33aa809d 2019-08-08 stsp size_t linesize = 0;
4177 33aa809d 2019-08-08 stsp ssize_t linelen;
4179 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4180 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4181 500467ff 2019-09-25 hiltjo if (ferror(f))
4182 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4183 500467ff 2019-09-25 hiltjo return NULL;
4185 33aa809d 2019-08-08 stsp free(line);
4186 33aa809d 2019-08-08 stsp return NULL;
4189 33aa809d 2019-08-08 stsp static const struct got_error *
4190 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4191 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4192 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4194 33aa809d 2019-08-08 stsp const struct got_error *err;
4196 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4197 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4198 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4200 33aa809d 2019-08-08 stsp return err;
4201 33aa809d 2019-08-08 stsp (*line_cur1)++;
4203 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4204 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4205 33aa809d 2019-08-08 stsp if (rejectfile)
4206 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4208 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4210 33aa809d 2019-08-08 stsp return err;
4211 33aa809d 2019-08-08 stsp (*line_cur2)++;
4213 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4214 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4215 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4217 33aa809d 2019-08-08 stsp return err;
4218 33aa809d 2019-08-08 stsp (*line_cur2)++;
4220 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4221 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4222 33aa809d 2019-08-08 stsp if (rejectfile)
4223 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4225 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4227 33aa809d 2019-08-08 stsp return err;
4228 33aa809d 2019-08-08 stsp (*line_cur1)++;
4231 f1e81a05 2019-08-10 stsp return NULL;
4234 f1e81a05 2019-08-10 stsp static const struct got_error *
4235 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4236 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4238 f1e81a05 2019-08-10 stsp const struct got_error *err;
4240 f1e81a05 2019-08-10 stsp if (outfile) {
4241 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4242 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4243 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4245 f1e81a05 2019-08-10 stsp return err;
4246 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4249 f1e81a05 2019-08-10 stsp if (rejectfile) {
4250 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4251 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4252 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4254 f1e81a05 2019-08-10 stsp return err;
4255 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4259 33aa809d 2019-08-08 stsp return NULL;
4262 33aa809d 2019-08-08 stsp static const struct got_error *
4263 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4264 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4265 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4266 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4267 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4269 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4270 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4271 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4272 33aa809d 2019-08-08 stsp FILE *hunkfile;
4273 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4274 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4277 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4279 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4280 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4281 fe621944 2020-11-10 stsp start_old = cc.left.start;
4282 fe621944 2020-11-10 stsp end_old = cc.left.end;
4283 fe621944 2020-11-10 stsp start_new = cc.right.start;
4284 fe621944 2020-11-10 stsp end_new = cc.right.end;
4286 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4287 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4288 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4290 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4291 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4292 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4294 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4295 fe621944 2020-11-10 stsp if (diff_state == NULL)
4296 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4297 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4299 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4300 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4301 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4302 33aa809d 2019-08-08 stsp goto done;
4305 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4306 fe621944 2020-11-10 stsp diff_result, &cc);
4307 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4308 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4309 33aa809d 2019-08-08 stsp goto done;
4312 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4313 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4314 33aa809d 2019-08-08 stsp goto done;
4317 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4318 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4320 33aa809d 2019-08-08 stsp goto done;
4322 33aa809d 2019-08-08 stsp switch (*choice) {
4323 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4324 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4325 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4327 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
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, rejectfile, outfile);
4331 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4334 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4338 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4339 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4340 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4341 33aa809d 2019-08-08 stsp return err;
4344 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4345 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4346 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4347 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4348 1f1abb7e 2019-08-08 stsp void *progress_arg;
4349 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4350 33aa809d 2019-08-08 stsp void *patch_arg;
4351 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4354 e20a8b6f 2019-06-04 stsp static const struct got_error *
4355 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4356 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4357 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4358 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4359 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4361 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4362 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4363 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4364 1ebedb77 2019-10-19 stsp int fd2 = -1;
4365 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4366 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4367 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4368 fe621944 2020-11-10 stsp struct stat sb2;
4369 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4370 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4371 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4373 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4375 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4377 33aa809d 2019-08-08 stsp return err;
4379 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4380 12463d8b 2019-12-13 stsp fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4381 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4382 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4383 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4384 fa3cef63 2020-07-23 stsp goto done;
4386 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4387 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4388 fa3cef63 2020-07-23 stsp if (link_len == -1)
4389 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlinkat", path2);
4390 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4391 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4394 12463d8b 2019-12-13 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4395 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4396 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4397 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4398 fa3cef63 2020-07-23 stsp goto done;
4400 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4401 fa3cef63 2020-07-23 stsp sizeof(link_target));
4402 fa3cef63 2020-07-23 stsp if (link_len == -1)
4403 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4404 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4405 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4408 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4409 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4410 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4411 fa3cef63 2020-07-23 stsp goto done;
4414 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4415 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4416 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4417 fa3cef63 2020-07-23 stsp goto done;
4422 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4423 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4424 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4425 fa3cef63 2020-07-23 stsp goto done;
4427 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4428 fa3cef63 2020-07-23 stsp if (n != link_len) {
4429 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4430 fa3cef63 2020-07-23 stsp goto done;
4432 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4433 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4434 fa3cef63 2020-07-23 stsp goto done;
4436 fa3cef63 2020-07-23 stsp rewind(f2);
4439 33aa809d 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4441 33aa809d 2019-08-08 stsp goto done;
4443 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4445 33aa809d 2019-08-08 stsp goto done;
4447 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4449 33aa809d 2019-08-08 stsp goto done;
4451 64453f7e 2020-11-21 stsp err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4454 33aa809d 2019-08-08 stsp goto done;
4456 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4458 33aa809d 2019-08-08 stsp goto done;
4460 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4461 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4462 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4463 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4465 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4466 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4467 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4468 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4469 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4470 fe621944 2020-11-10 stsp nchanges++;
4472 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4473 33aa809d 2019-08-08 stsp int choice;
4474 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4475 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4476 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4477 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4478 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4479 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4481 33aa809d 2019-08-08 stsp goto done;
4482 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4483 33aa809d 2019-08-08 stsp have_content = 1;
4484 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4487 1ebedb77 2019-10-19 stsp if (have_content) {
4488 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4489 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4490 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4492 1ebedb77 2019-10-19 stsp goto done;
4494 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4495 3818e3c4 2020-11-01 naddy if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4496 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4497 fa3cef63 2020-07-23 stsp goto done;
4502 33aa809d 2019-08-08 stsp free(id_str);
4504 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4505 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4506 fe621944 2020-11-10 stsp if (err == NULL)
4507 fe621944 2020-11-10 stsp err = free_err;
4508 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4509 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4510 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4511 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4512 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4513 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4514 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4515 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4516 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4517 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4518 33aa809d 2019-08-08 stsp if (err || !have_content) {
4519 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4520 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4521 33aa809d 2019-08-08 stsp free(*path_outfile);
4522 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4524 33aa809d 2019-08-08 stsp free(path1);
4525 33aa809d 2019-08-08 stsp return err;
4528 33aa809d 2019-08-08 stsp static const struct got_error *
4529 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4530 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4531 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4532 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4534 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4535 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4536 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4537 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4538 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4539 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4540 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4541 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4542 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4543 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4545 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4546 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4547 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4548 d3bcc3d1 2019-08-08 stsp return NULL;
4550 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4551 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4552 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4554 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4555 65084dad 2019-08-08 stsp if (ie == NULL)
4556 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4558 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4559 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4560 a129376b 2019-03-28 stsp if (err) {
4561 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4562 a129376b 2019-03-28 stsp goto done;
4563 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4564 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4565 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4566 e20a8b6f 2019-06-04 stsp goto done;
4569 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4570 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4571 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4572 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4573 a129376b 2019-03-28 stsp goto done;
4576 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4577 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4578 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4579 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4580 a129376b 2019-03-28 stsp goto done;
4583 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4584 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4585 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4586 a129376b 2019-03-28 stsp goto done;
4591 1f1abb7e 2019-08-08 stsp err = got_object_id_by_path(&tree_id, a->repo,
4592 1f1abb7e 2019-08-08 stsp a->worktree->base_commit_id, tree_path);
4593 a9fa2909 2019-07-27 stsp if (err) {
4594 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4595 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4596 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4597 a9fa2909 2019-07-27 stsp goto done;
4599 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4601 a9fa2909 2019-07-27 stsp goto done;
4603 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4605 a9fa2909 2019-07-27 stsp goto done;
4607 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4608 1233e6b6 2020-10-19 stsp free(te_name);
4609 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4610 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4611 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4612 a9fa2909 2019-07-27 stsp goto done;
4616 a129376b 2019-03-28 stsp switch (status) {
4617 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4618 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4619 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4620 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4621 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4623 33aa809d 2019-08-08 stsp goto done;
4624 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4627 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4628 1f1abb7e 2019-08-08 stsp ie->path);
4630 1ee397ad 2019-07-12 stsp goto done;
4631 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4633 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4634 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4635 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4636 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4637 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4639 33aa809d 2019-08-08 stsp goto done;
4640 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4643 33aa809d 2019-08-08 stsp /* fall through */
4644 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4645 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4646 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4647 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4648 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4649 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4650 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4651 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4652 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4654 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4655 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4656 1f1abb7e 2019-08-08 stsp err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4658 65084dad 2019-08-08 stsp goto done;
4660 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4661 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4662 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4663 a129376b 2019-03-28 stsp goto done;
4666 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4667 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4668 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4669 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4670 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4671 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4672 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4674 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4675 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4676 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4677 369fd7e5 2020-07-23 stsp path_content);
4680 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4681 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4682 369fd7e5 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4683 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4685 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4686 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4687 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4688 369fd7e5 2020-07-23 stsp goto done;
4692 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4693 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4694 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4695 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4696 c90c8ce3 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4697 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4699 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4701 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4702 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4703 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4704 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4707 a129376b 2019-03-28 stsp goto done;
4708 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4709 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4710 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4711 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
4712 437adc9d 2020-12-10 yzhong blob->id.sha1,
4713 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4715 2e1fa222 2020-07-23 stsp goto done;
4717 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4718 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4719 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4728 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4729 33aa809d 2019-08-08 stsp free(path_content);
4730 e20a8b6f 2019-06-04 stsp free(parent_path);
4731 a129376b 2019-03-28 stsp free(tree_path);
4733 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4735 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4736 a129376b 2019-03-28 stsp free(tree_id);
4737 e20a8b6f 2019-06-04 stsp return err;
4740 e20a8b6f 2019-06-04 stsp const struct got_error *
4741 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4742 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4743 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4744 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4745 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4747 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4748 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4749 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4750 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4751 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4752 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4754 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4756 e20a8b6f 2019-06-04 stsp return err;
4758 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4760 e20a8b6f 2019-06-04 stsp goto done;
4762 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4763 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4764 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4765 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4766 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4767 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4768 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4769 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4770 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4771 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
4775 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4776 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4777 e20a8b6f 2019-06-04 stsp err = sync_err;
4779 fb399478 2019-07-12 stsp free(fileindex_path);
4780 a129376b 2019-03-28 stsp if (fileindex)
4781 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4782 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4783 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4784 a129376b 2019-03-28 stsp err = unlockerr;
4785 c4296144 2019-05-09 stsp return err;
4788 cf066bf8 2019-05-09 stsp static void
4789 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4791 24519714 2019-05-09 stsp free(ct->path);
4792 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4793 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4794 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4795 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4796 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4797 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4801 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4802 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4803 24519714 2019-05-09 stsp struct got_repository *repo;
4804 24519714 2019-05-09 stsp struct got_worktree *worktree;
4805 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4806 5f8a88c6 2019-08-03 stsp int have_staged_files;
4807 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4810 c4296144 2019-05-09 stsp static const struct got_error *
4811 88d0e355 2019-08-03 stsp collect_commitables(void *arg, unsigned char status,
4812 88d0e355 2019-08-03 stsp unsigned char staged_status, const char *relpath,
4813 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4814 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4816 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
4817 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4818 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4819 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4820 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4821 768aea60 2019-05-09 stsp struct stat sb;
4823 5f8a88c6 2019-08-03 stsp if (a->have_staged_files) {
4824 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4825 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4826 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4827 5f8a88c6 2019-08-03 stsp return NULL;
4829 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4830 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4832 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4833 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4834 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4835 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4836 5f8a88c6 2019-08-03 stsp return NULL;
4839 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4840 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4841 036813ee 2019-05-09 stsp goto done;
4843 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4844 036813ee 2019-05-09 stsp parent_path = strdup("");
4845 69960a46 2019-05-09 stsp if (parent_path == NULL)
4846 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4848 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4850 69960a46 2019-05-09 stsp return err;
4853 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4854 cf066bf8 2019-05-09 stsp if (ct == NULL) {
4855 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4856 c4296144 2019-05-09 stsp goto done;
4859 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4860 768aea60 2019-05-09 stsp relpath) == -1) {
4861 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4862 768aea60 2019-05-09 stsp goto done;
4865 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
4866 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
4867 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
4868 0aeb8099 2020-07-23 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4869 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
4870 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
4871 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
4872 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
4874 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
4875 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
4878 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4879 0aeb8099 2020-07-23 stsp goto done;
4881 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
4882 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
4883 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
4884 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4885 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
4886 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
4887 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
4888 12463d8b 2019-12-13 stsp ct->ondisk_path);
4889 12463d8b 2019-12-13 stsp goto done;
4891 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
4892 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
4893 768aea60 2019-05-09 stsp goto done;
4895 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
4898 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4899 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4900 44d03001 2019-05-09 stsp relpath) == -1) {
4901 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4902 44d03001 2019-05-09 stsp goto done;
4905 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4906 35213c7c 2020-07-23 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4907 35213c7c 2020-07-23 stsp int is_bad_symlink;
4908 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
4909 35213c7c 2020-07-23 stsp ssize_t target_len;
4910 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
4911 35213c7c 2020-07-23 stsp sizeof(target_path));
4912 35213c7c 2020-07-23 stsp if (target_len == -1) {
4913 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
4914 35213c7c 2020-07-23 stsp ct->ondisk_path);
4915 35213c7c 2020-07-23 stsp goto done;
4917 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
4918 35213c7c 2020-07-23 stsp target_len, ct->ondisk_path, a->worktree->root_path);
4920 35213c7c 2020-07-23 stsp goto done;
4921 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
4922 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
4923 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
4924 35213c7c 2020-07-23 stsp goto done;
4929 cf066bf8 2019-05-09 stsp ct->status = status;
4930 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
4931 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
4932 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
4933 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
4934 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
4935 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
4936 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4937 b416585c 2019-05-13 stsp goto done;
4939 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
4940 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
4941 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
4942 f0b75401 2019-08-03 stsp goto done;
4945 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
4946 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
4947 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4948 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
4949 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4950 036813ee 2019-05-09 stsp goto done;
4953 24519714 2019-05-09 stsp ct->path = strdup(path);
4954 24519714 2019-05-09 stsp if (ct->path == NULL) {
4955 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4956 24519714 2019-05-09 stsp goto done;
4958 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4960 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
4961 ed175427 2019-05-09 stsp free_commitable(ct);
4962 24519714 2019-05-09 stsp free(parent_path);
4963 036813ee 2019-05-09 stsp free(path);
4964 a129376b 2019-03-28 stsp return err;
4967 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
4968 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
4969 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4970 036813ee 2019-05-09 stsp struct got_repository *);
4972 ed175427 2019-05-09 stsp static const struct got_error *
4973 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4974 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
4975 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
4976 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4977 afa376bf 2019-05-09 stsp struct got_repository *repo)
4979 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
4980 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
4981 036813ee 2019-05-09 stsp char *subpath;
4983 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
4984 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4985 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4987 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
4989 ed175427 2019-05-09 stsp return err;
4991 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
4992 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
4993 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
4994 036813ee 2019-05-09 stsp free(subpath);
4995 036813ee 2019-05-09 stsp return err;
4998 036813ee 2019-05-09 stsp static const struct got_error *
4999 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5001 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5002 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5004 036813ee 2019-05-09 stsp *match = 0;
5006 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5007 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5008 0f63689d 2019-05-10 stsp return NULL;
5011 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5013 0f63689d 2019-05-10 stsp return err;
5014 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5015 036813ee 2019-05-09 stsp free(ct_parent_path);
5016 036813ee 2019-05-09 stsp return err;
5019 768aea60 2019-05-09 stsp static mode_t
5020 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5022 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5023 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5025 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5028 036813ee 2019-05-09 stsp static const struct got_error *
5029 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5030 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5032 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5034 036813ee 2019-05-09 stsp *new_te = NULL;
5036 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5038 036813ee 2019-05-09 stsp goto done;
5040 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5042 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5043 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5044 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5046 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5048 036813ee 2019-05-09 stsp if (err && *new_te) {
5049 56e0773d 2019-11-28 stsp free(*new_te);
5050 036813ee 2019-05-09 stsp *new_te = NULL;
5052 036813ee 2019-05-09 stsp return err;
5055 036813ee 2019-05-09 stsp static const struct got_error *
5056 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5057 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5059 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5060 102b254e 2020-10-19 stsp char *ct_name = NULL;
5062 036813ee 2019-05-09 stsp *new_te = NULL;
5064 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5065 036813ee 2019-05-09 stsp if (*new_te == NULL)
5066 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5068 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5070 036813ee 2019-05-09 stsp goto done;
5071 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5072 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5073 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5074 036813ee 2019-05-09 stsp goto done;
5077 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5079 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5080 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5081 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5083 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5085 102b254e 2020-10-19 stsp free(ct_name);
5086 036813ee 2019-05-09 stsp if (err && *new_te) {
5087 56e0773d 2019-11-28 stsp free(*new_te);
5088 036813ee 2019-05-09 stsp *new_te = NULL;
5090 036813ee 2019-05-09 stsp return err;
5093 036813ee 2019-05-09 stsp static const struct got_error *
5094 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5095 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5097 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5098 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5100 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5102 036813ee 2019-05-09 stsp return err;
5103 036813ee 2019-05-09 stsp if (new_pe == NULL)
5104 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5105 036813ee 2019-05-09 stsp return NULL;
5108 afa376bf 2019-05-09 stsp static const struct got_error *
5109 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5110 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5112 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5113 5f8a88c6 2019-08-03 stsp unsigned char status;
5115 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5116 afa376bf 2019-05-09 stsp ct_path++;
5118 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5119 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5121 5f8a88c6 2019-08-03 stsp status = ct->status;
5123 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5124 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5127 036813ee 2019-05-09 stsp static const struct got_error *
5128 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5129 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5131 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5132 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5133 44d03001 2019-05-09 stsp char *te_path;
5135 44d03001 2019-05-09 stsp *modified = 0;
5137 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5138 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5139 44d03001 2019-05-09 stsp te->name) == -1)
5140 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5142 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5143 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5144 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5145 44d03001 2019-05-09 stsp strlen(te_path));
5146 62d463ca 2020-10-20 naddy if (*modified)
5150 44d03001 2019-05-09 stsp free(te_path);
5151 44d03001 2019-05-09 stsp return err;
5154 44d03001 2019-05-09 stsp static const struct got_error *
5155 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5156 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5157 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5159 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5160 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5162 036813ee 2019-05-09 stsp *ctp = NULL;
5164 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5165 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5166 036813ee 2019-05-09 stsp char *ct_name = NULL;
5167 036813ee 2019-05-09 stsp int path_matches;
5169 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5170 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5171 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5172 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
5175 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5176 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5180 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5183 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5185 036813ee 2019-05-09 stsp return err;
5186 036813ee 2019-05-09 stsp if (!path_matches)
5189 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5191 d34b633e 2020-10-19 stsp return err;
5193 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5194 d34b633e 2020-10-19 stsp free(ct_name);
5197 d34b633e 2020-10-19 stsp free(ct_name);
5199 036813ee 2019-05-09 stsp *ctp = ct;
5203 2b496619 2019-07-10 stsp return err;
5206 2b496619 2019-07-10 stsp static const struct got_error *
5207 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5208 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5209 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5210 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5211 2b496619 2019-07-10 stsp struct got_repository *repo)
5213 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5214 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5215 2b496619 2019-07-10 stsp char *subtree_path;
5216 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5217 ba580f68 2020-03-22 stsp int nentries;
5219 2b496619 2019-07-10 stsp *new_tep = NULL;
5221 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5222 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5223 2b496619 2019-07-10 stsp child_path) == -1)
5224 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5226 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5227 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5228 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5229 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5231 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5232 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5233 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5234 2b496619 2019-07-10 stsp goto done;
5236 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5237 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5238 2b496619 2019-07-10 stsp if (err) {
5239 56e0773d 2019-11-28 stsp free(new_te);
5240 2b496619 2019-07-10 stsp goto done;
5242 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5245 2b496619 2019-07-10 stsp free(subtree_path);
5246 2b496619 2019-07-10 stsp if (err == NULL)
5247 2b496619 2019-07-10 stsp *new_tep = new_te;
5248 036813ee 2019-05-09 stsp return err;
5251 036813ee 2019-05-09 stsp static const struct got_error *
5252 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5253 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5254 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5255 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5256 036813ee 2019-05-09 stsp struct got_repository *repo)
5258 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5259 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5260 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5261 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5263 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5264 ba580f68 2020-03-22 stsp *nentries = 0;
5266 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5267 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5268 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5269 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5271 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5272 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5273 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5276 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5277 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5280 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5281 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5283 036813ee 2019-05-09 stsp goto done;
5285 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5286 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5287 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5289 036813ee 2019-05-09 stsp goto done;
5290 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5292 afa376bf 2019-05-09 stsp goto done;
5293 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5294 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5296 2f51b5b3 2019-05-09 stsp goto done;
5297 ba580f68 2020-03-22 stsp (*nentries)++;
5299 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5300 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5301 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5302 2b496619 2019-07-10 stsp == NULL) {
5303 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5304 2b496619 2019-07-10 stsp child_path, path_base_tree,
5305 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5308 2b496619 2019-07-10 stsp goto done;
5309 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5311 2b496619 2019-07-10 stsp goto done;
5312 ba580f68 2020-03-22 stsp (*nentries)++;
5317 2f51b5b3 2019-05-09 stsp if (base_tree) {
5318 56e0773d 2019-11-28 stsp int i, nbase_entries;
5319 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5320 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5321 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5322 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5324 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5325 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5326 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5327 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5329 63c5ca5d 2019-08-24 stsp goto done;
5330 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5332 63c5ca5d 2019-08-24 stsp goto done;
5333 ba580f68 2020-03-22 stsp (*nentries)++;
5337 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5338 44d03001 2019-05-09 stsp int modified;
5339 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5341 036813ee 2019-05-09 stsp goto done;
5342 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5343 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5345 2f51b5b3 2019-05-09 stsp goto done;
5346 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5347 44d03001 2019-05-09 stsp if (modified) {
5348 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5349 ba580f68 2020-03-22 stsp int nsubentries;
5350 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5351 ba580f68 2020-03-22 stsp &nsubentries, te,
5352 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5353 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5355 44d03001 2019-05-09 stsp goto done;
5356 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5357 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5358 ba580f68 2020-03-22 stsp free(new_id);
5361 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5362 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5363 56e0773d 2019-11-28 stsp free(new_id);
5365 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5367 036813ee 2019-05-09 stsp goto done;
5368 ba580f68 2020-03-22 stsp (*nentries)++;
5372 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5373 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5375 f66c734c 2019-09-22 stsp goto done;
5377 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5378 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5379 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5380 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5381 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5382 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5384 2f51b5b3 2019-05-09 stsp goto done;
5385 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5387 2f51b5b3 2019-05-09 stsp goto done;
5388 ba580f68 2020-03-22 stsp (*nentries)++;
5390 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5391 b416585c 2019-05-13 stsp status_arg);
5393 afa376bf 2019-05-09 stsp goto done;
5395 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5396 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5398 2f51b5b3 2019-05-09 stsp goto done;
5399 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5401 2f51b5b3 2019-05-09 stsp goto done;
5402 ba580f68 2020-03-22 stsp (*nentries)++;
5407 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5408 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5410 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
5411 2e1fa222 2020-07-23 stsp return err;
5414 2e1fa222 2020-07-23 stsp static const struct got_error *
5415 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5416 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5417 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5418 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5420 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5421 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5422 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5424 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5425 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5426 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5428 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5430 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5431 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5432 437adc9d 2020-12-10 yzhong if (err)
5433 437adc9d 2020-12-10 yzhong goto done;
5436 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5437 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5438 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5439 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5440 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5441 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5442 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5443 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5445 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5446 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5447 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
5448 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5449 72fd46fa 2019-09-06 stsp !have_staged_files);
5451 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5452 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5453 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
5454 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5455 72fd46fa 2019-09-06 stsp !have_staged_files);
5457 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5459 437adc9d 2020-12-10 yzhong goto done;
5460 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5461 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
5462 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
5463 3969253a 2020-03-07 stsp if (err) {
5464 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5465 437adc9d 2020-12-10 yzhong goto done;
5467 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5468 3969253a 2020-03-07 stsp if (err) {
5469 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5470 437adc9d 2020-12-10 yzhong goto done;
5473 437adc9d 2020-12-10 yzhong free(relpath);
5474 437adc9d 2020-12-10 yzhong relpath = NULL;
5477 437adc9d 2020-12-10 yzhong free(relpath);
5478 d56d26ce 2019-05-10 stsp return err;
5482 d56d26ce 2019-05-10 stsp static const struct got_error *
5483 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5484 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5485 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5486 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5487 735ef5ac 2019-08-03 stsp int ood_errcode)
5489 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5490 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5492 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5493 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5494 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5495 1a36436d 2019-06-10 stsp return NULL;
5497 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5498 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5500 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5501 735ef5ac 2019-08-03 stsp in_repo_path);
5502 9bead371 2019-07-28 stsp if (err) {
5503 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5504 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5505 1a36436d 2019-06-10 stsp goto done;
5506 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5507 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5509 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5510 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5511 735ef5ac 2019-08-03 stsp in_repo_path);
5512 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5513 1a36436d 2019-06-10 stsp goto done;
5514 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5518 1a36436d 2019-06-10 stsp return err;
5521 c4296144 2019-05-09 stsp const struct got_error *
5522 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5523 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5524 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id, struct got_worktree *worktree,
5525 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
5526 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5527 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5528 afa376bf 2019-05-09 stsp struct got_repository *repo)
5530 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5531 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5532 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5533 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5534 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5535 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5536 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5537 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5538 ba580f68 2020-03-22 stsp int nentries;
5539 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5540 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5541 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5543 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5545 de18fc63 2019-05-09 stsp SIMPLEQ_INIT(&parent_ids);
5547 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5549 588edf97 2019-05-10 stsp goto done;
5551 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5553 588edf97 2019-05-10 stsp goto done;
5555 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5556 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5561 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5562 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5566 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5567 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5568 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5569 cf066bf8 2019-05-09 stsp char *ondisk_path;
5571 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5572 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5573 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5576 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5577 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5578 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5581 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5582 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5583 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5584 cf066bf8 2019-05-09 stsp goto done;
5586 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5587 2e1fa222 2020-07-23 stsp free(ondisk_path);
5589 cf066bf8 2019-05-09 stsp goto done;
5592 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5593 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5594 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5596 036813ee 2019-05-09 stsp goto done;
5598 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5600 de18fc63 2019-05-09 stsp goto done;
5601 de18fc63 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5602 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5603 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5604 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
5605 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5606 33ad4cbe 2019-05-12 jcs free(logmsg);
5608 9d40349a 2019-05-09 stsp goto done;
5610 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5611 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5612 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5613 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5614 09f5bd90 2019-05-09 stsp goto done;
5616 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
5617 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5619 09f5bd90 2019-05-09 stsp goto done;
5620 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5622 ebf99748 2019-05-09 stsp goto done;
5623 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5624 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5625 09f5bd90 2019-05-09 stsp goto done;
5627 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
5628 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
5630 f2c16586 2019-05-09 stsp goto done;
5631 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
5633 f2c16586 2019-05-09 stsp goto done;
5635 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5637 09f5bd90 2019-05-09 stsp goto done;
5639 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
5641 ebf99748 2019-05-09 stsp goto done;
5643 588edf97 2019-05-10 stsp if (head_tree)
5644 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
5645 588edf97 2019-05-10 stsp if (head_commit)
5646 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
5647 09f5bd90 2019-05-09 stsp free(head_commit_id2);
5648 2f17228e 2019-05-12 stsp if (head_ref2) {
5649 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
5650 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
5651 2f17228e 2019-05-12 stsp err = unlockerr;
5652 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
5654 39cd0ff6 2019-07-12 stsp return err;
5657 5c1e53bc 2019-07-28 stsp static const struct got_error *
5658 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
5659 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
5661 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
5662 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
5664 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
5665 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
5666 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
5668 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
5669 5c1e53bc 2019-07-28 stsp ct_path++;
5671 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
5672 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
5676 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
5677 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
5679 5c1e53bc 2019-07-28 stsp return NULL;
5682 f0b75401 2019-08-03 stsp static const struct got_error *
5683 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
5685 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
5687 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5688 f0b75401 2019-08-03 stsp *have_staged_files = 1;
5689 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
5692 f0b75401 2019-08-03 stsp return NULL;
5695 5f8a88c6 2019-08-03 stsp static const struct got_error *
5696 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
5697 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
5699 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
5700 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
5702 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5703 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
5705 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5706 5f8a88c6 2019-08-03 stsp if (ie == NULL)
5707 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5708 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5709 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
5710 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
5713 5f8a88c6 2019-08-03 stsp return NULL;
5716 39cd0ff6 2019-07-12 stsp const struct got_error *
5717 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
5718 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
5719 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
5720 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5721 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
5722 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
5724 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5725 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
5726 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
5727 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5728 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5729 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
5730 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
5731 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5732 f0b75401 2019-08-03 stsp int have_staged_files = 0;
5734 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
5736 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5738 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
5740 39cd0ff6 2019-07-12 stsp goto done;
5742 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5744 39cd0ff6 2019-07-12 stsp goto done;
5746 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5748 39cd0ff6 2019-07-12 stsp goto done;
5750 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5752 39cd0ff6 2019-07-12 stsp goto done;
5754 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5755 5f8a88c6 2019-08-03 stsp &have_staged_files);
5756 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
5757 5f8a88c6 2019-08-03 stsp goto done;
5758 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
5759 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
5761 5f8a88c6 2019-08-03 stsp goto done;
5764 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5765 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
5766 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
5767 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
5768 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
5769 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5770 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5771 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5772 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5774 5c1e53bc 2019-07-28 stsp goto done;
5777 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5778 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5779 39cd0ff6 2019-07-12 stsp goto done;
5782 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5783 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
5785 5c1e53bc 2019-07-28 stsp goto done;
5788 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5789 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5790 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5792 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5793 f0b75401 2019-08-03 stsp ct_path++;
5794 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5795 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5796 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5798 b50cabdf 2019-07-12 stsp goto done;
5802 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5803 5c1e53bc 2019-07-28 stsp head_commit_id, worktree, author, committer,
5804 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5806 39cd0ff6 2019-07-12 stsp goto done;
5808 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
5809 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
5810 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5811 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5812 39cd0ff6 2019-07-12 stsp err = sync_err;
5814 39cd0ff6 2019-07-12 stsp if (fileindex)
5815 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5816 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5817 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5818 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5819 39cd0ff6 2019-07-12 stsp err = unlockerr;
5820 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5821 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5822 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5824 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5825 c4296144 2019-05-09 stsp return err;
5828 8656d6c4 2019-05-20 stsp const char *
5829 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5831 8656d6c4 2019-05-20 stsp return ct->path;
5834 8656d6c4 2019-05-20 stsp unsigned int
5835 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
5837 8656d6c4 2019-05-20 stsp return ct->status;
5840 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
5841 818c7501 2019-07-11 stsp struct got_worktree *worktree;
5842 818c7501 2019-07-11 stsp struct got_repository *repo;
5845 818c7501 2019-07-11 stsp static const struct got_error *
5846 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5848 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5849 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
5850 818c7501 2019-07-11 stsp unsigned char status;
5851 818c7501 2019-07-11 stsp struct stat sb;
5852 818c7501 2019-07-11 stsp char *ondisk_path;
5854 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
5855 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5856 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
5857 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
5859 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5861 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
5863 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
5864 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5865 818c7501 2019-07-11 stsp free(ondisk_path);
5867 818c7501 2019-07-11 stsp return err;
5869 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
5870 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
5871 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5872 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5874 818c7501 2019-07-11 stsp return NULL;
5877 818c7501 2019-07-11 stsp const struct got_error *
5878 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5879 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5880 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
5881 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5883 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5884 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5885 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
5886 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
5887 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
5888 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5889 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
5891 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5892 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5893 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5895 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
5897 818c7501 2019-07-11 stsp return err;
5899 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5901 818c7501 2019-07-11 stsp goto done;
5903 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
5904 818c7501 2019-07-11 stsp ok_arg.repo = repo;
5905 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5908 818c7501 2019-07-11 stsp goto done;
5910 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5912 818c7501 2019-07-11 stsp goto done;
5914 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5916 818c7501 2019-07-11 stsp goto done;
5918 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5920 818c7501 2019-07-11 stsp goto done;
5922 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5925 e51d7b55 2020-01-04 stsp goto done;
5927 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5929 818c7501 2019-07-11 stsp goto done;
5930 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5931 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5932 e51d7b55 2020-01-04 stsp goto done;
5935 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
5936 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
5938 818c7501 2019-07-11 stsp goto done;
5939 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
5941 818c7501 2019-07-11 stsp goto done;
5943 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
5945 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5947 818c7501 2019-07-11 stsp goto done;
5949 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
5951 818c7501 2019-07-11 stsp goto done;
5953 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
5954 818c7501 2019-07-11 stsp worktree->base_commit_id);
5956 818c7501 2019-07-11 stsp goto done;
5957 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
5959 818c7501 2019-07-11 stsp goto done;
5961 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
5963 818c7501 2019-07-11 stsp goto done;
5965 818c7501 2019-07-11 stsp free(fileindex_path);
5966 818c7501 2019-07-11 stsp free(tmp_branch_name);
5967 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
5968 818c7501 2019-07-11 stsp free(branch_ref_name);
5969 818c7501 2019-07-11 stsp if (branch_ref)
5970 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5971 818c7501 2019-07-11 stsp if (wt_branch)
5972 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
5973 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
5974 818c7501 2019-07-11 stsp if (err) {
5975 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
5976 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
5977 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5979 818c7501 2019-07-11 stsp if (*tmp_branch) {
5980 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5981 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5983 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5984 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5985 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5987 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
5989 818c7501 2019-07-11 stsp return err;
5992 818c7501 2019-07-11 stsp const struct got_error *
5993 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
5994 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5995 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
5996 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
5998 818c7501 2019-07-11 stsp const struct got_error *err;
5999 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6000 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6001 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6002 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6003 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6005 818c7501 2019-07-11 stsp *commit_id = NULL;
6006 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6007 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6008 3e3a69f1 2019-07-25 stsp *branch = NULL;
6009 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6011 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6013 3e3a69f1 2019-07-25 stsp return err;
6015 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6017 f032f1f7 2019-08-04 stsp goto done;
6019 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6020 f032f1f7 2019-08-04 stsp &have_staged_files);
6021 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6022 f032f1f7 2019-08-04 stsp goto done;
6023 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6024 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6025 3e3a69f1 2019-07-25 stsp goto done;
6028 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6030 f032f1f7 2019-08-04 stsp goto done;
6032 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6034 818c7501 2019-07-11 stsp goto done;
6036 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6038 818c7501 2019-07-11 stsp goto done;
6040 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6042 818c7501 2019-07-11 stsp goto done;
6044 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6046 818c7501 2019-07-11 stsp goto done;
6048 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6049 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6051 818c7501 2019-07-11 stsp goto done;
6053 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6055 818c7501 2019-07-11 stsp goto done;
6057 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6059 818c7501 2019-07-11 stsp goto done;
6061 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6062 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6064 818c7501 2019-07-11 stsp goto done;
6066 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6068 818c7501 2019-07-11 stsp goto done;
6070 818c7501 2019-07-11 stsp free(commit_ref_name);
6071 818c7501 2019-07-11 stsp free(branch_ref_name);
6072 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6073 818c7501 2019-07-11 stsp if (commit_ref)
6074 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6075 818c7501 2019-07-11 stsp if (branch_ref)
6076 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6077 818c7501 2019-07-11 stsp if (err) {
6078 818c7501 2019-07-11 stsp free(*commit_id);
6079 818c7501 2019-07-11 stsp *commit_id = NULL;
6080 818c7501 2019-07-11 stsp if (*tmp_branch) {
6081 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6082 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6084 818c7501 2019-07-11 stsp if (*new_base_branch) {
6085 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6086 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6088 818c7501 2019-07-11 stsp if (*branch) {
6089 818c7501 2019-07-11 stsp got_ref_close(*branch);
6090 818c7501 2019-07-11 stsp *branch = NULL;
6092 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6093 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6094 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6096 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6098 818c7501 2019-07-11 stsp return err;
6101 818c7501 2019-07-11 stsp const struct got_error *
6102 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6104 818c7501 2019-07-11 stsp const struct got_error *err;
6105 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6107 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6109 818c7501 2019-07-11 stsp return err;
6111 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6112 818c7501 2019-07-11 stsp free(tmp_branch_name);
6113 818c7501 2019-07-11 stsp return NULL;
6116 818c7501 2019-07-11 stsp static const struct got_error *
6117 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6118 818c7501 2019-07-11 stsp char **logmsg, void *arg)
6120 0ebf8283 2019-07-24 stsp *logmsg = arg;
6121 818c7501 2019-07-11 stsp return NULL;
6124 818c7501 2019-07-11 stsp static const struct got_error *
6125 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6126 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6127 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6128 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6130 818c7501 2019-07-11 stsp return NULL;
6133 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6134 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6135 01757395 2019-07-12 stsp void *progress_arg;
6136 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6139 01757395 2019-07-12 stsp static const struct got_error *
6140 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6142 01757395 2019-07-12 stsp const struct got_error *err;
6143 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6145 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6147 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6149 01757395 2019-07-12 stsp return err;
6151 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6152 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6153 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6154 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6155 01757395 2019-07-12 stsp return NULL;
6157 01757395 2019-07-12 stsp p = strdup(path);
6158 01757395 2019-07-12 stsp if (p == NULL)
6159 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6161 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6162 01757395 2019-07-12 stsp if (err || new == NULL)
6164 01757395 2019-07-12 stsp return err;
6168 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6170 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6172 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
6173 01757395 2019-07-12 stsp free((char *)pe->path);
6175 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
6178 0ebf8283 2019-07-24 stsp static const struct got_error *
6179 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6180 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6182 818c7501 2019-07-11 stsp const struct got_error *err;
6183 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6185 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6186 818c7501 2019-07-11 stsp if (err) {
6187 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6188 818c7501 2019-07-11 stsp goto done;
6189 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6191 818c7501 2019-07-11 stsp goto done;
6192 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6194 818c7501 2019-07-11 stsp goto done;
6195 de05890f 2020-03-05 stsp } else if (is_rebase) {
6196 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6199 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6201 818c7501 2019-07-11 stsp goto done;
6202 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6203 818c7501 2019-07-11 stsp free(stored_id);
6204 818c7501 2019-07-11 stsp if (cmp != 0) {
6205 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6206 818c7501 2019-07-11 stsp goto done;
6210 0ebf8283 2019-07-24 stsp if (commit_ref)
6211 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6212 0ebf8283 2019-07-24 stsp return err;
6215 0ebf8283 2019-07-24 stsp static const struct got_error *
6216 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6217 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6218 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6219 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6220 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6221 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6223 0ebf8283 2019-07-24 stsp const struct got_error *err;
6224 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6225 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6226 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6228 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6230 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6232 0ebf8283 2019-07-24 stsp return err;
6234 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6235 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6236 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6237 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6238 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6239 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6240 818c7501 2019-07-11 stsp if (commit_ref)
6241 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6242 818c7501 2019-07-11 stsp return err;
6245 818c7501 2019-07-11 stsp const struct got_error *
6246 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6247 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6248 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6249 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6250 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6251 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6253 0ebf8283 2019-07-24 stsp const struct got_error *err;
6254 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6256 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6258 0ebf8283 2019-07-24 stsp return err;
6260 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6262 0ebf8283 2019-07-24 stsp goto done;
6264 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6265 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6266 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6268 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6269 0ebf8283 2019-07-24 stsp return err;
6272 0ebf8283 2019-07-24 stsp const struct got_error *
6273 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6274 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6275 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6276 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6277 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6278 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6280 0ebf8283 2019-07-24 stsp const struct got_error *err;
6281 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6283 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6285 0ebf8283 2019-07-24 stsp return err;
6287 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6289 0ebf8283 2019-07-24 stsp goto done;
6291 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6292 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6293 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6295 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6296 0ebf8283 2019-07-24 stsp return err;
6299 0ebf8283 2019-07-24 stsp static const struct got_error *
6300 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6301 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6302 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6303 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6304 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
6306 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6307 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6308 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6309 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6310 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6311 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6312 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6314 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6315 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6317 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6319 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6321 3e3a69f1 2019-07-25 stsp return err;
6323 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6324 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6325 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6326 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6328 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6329 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6330 01757395 2019-07-12 stsp * TODO: Ideally, merged_paths would contain a list of commitables
6331 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6333 01757395 2019-07-12 stsp if (merged_paths) {
6334 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6335 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6336 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6337 f2a9dc41 2019-12-13 tracey repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6340 01757395 2019-07-12 stsp goto done;
6343 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6344 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6346 01757395 2019-07-12 stsp goto done;
6349 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6350 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6351 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6353 a0e95631 2019-07-12 stsp goto done;
6354 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6355 a0e95631 2019-07-12 stsp goto done;
6358 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6360 edd02c5e 2019-07-11 stsp goto done;
6362 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6364 818c7501 2019-07-11 stsp goto done;
6366 5943eee2 2019-08-13 stsp if (new_logmsg) {
6367 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6368 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6369 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6370 5943eee2 2019-08-13 stsp goto done;
6373 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6375 5943eee2 2019-08-13 stsp goto done;
6378 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6379 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6380 5c1e53bc 2019-07-28 stsp worktree, got_object_commit_get_author(orig_commit),
6381 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
6382 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6384 a0e95631 2019-07-12 stsp goto done;
6386 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6388 a0e95631 2019-07-12 stsp goto done;
6390 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6392 a0e95631 2019-07-12 stsp goto done;
6394 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6395 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6396 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6397 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6398 a0e95631 2019-07-12 stsp err = sync_err;
6400 a0e95631 2019-07-12 stsp free(fileindex_path);
6401 a0e95631 2019-07-12 stsp free(head_commit_id);
6402 a0e95631 2019-07-12 stsp if (head_ref)
6403 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6404 818c7501 2019-07-11 stsp if (err) {
6405 818c7501 2019-07-11 stsp free(*new_commit_id);
6406 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6408 818c7501 2019-07-11 stsp return err;
6411 818c7501 2019-07-11 stsp const struct got_error *
6412 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6413 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6414 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6415 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6416 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6418 0ebf8283 2019-07-24 stsp const struct got_error *err;
6419 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6420 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6421 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6423 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6425 0ebf8283 2019-07-24 stsp return err;
6427 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6429 0ebf8283 2019-07-24 stsp goto done;
6430 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6432 0ebf8283 2019-07-24 stsp goto done;
6433 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6434 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6435 0ebf8283 2019-07-24 stsp goto done;
6438 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6439 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6441 0ebf8283 2019-07-24 stsp if (commit_ref)
6442 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6443 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6444 0ebf8283 2019-07-24 stsp free(commit_id);
6445 0ebf8283 2019-07-24 stsp return err;
6448 0ebf8283 2019-07-24 stsp const struct got_error *
6449 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6450 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6451 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6452 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6453 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6454 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6456 0ebf8283 2019-07-24 stsp const struct got_error *err;
6457 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6458 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6460 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6462 0ebf8283 2019-07-24 stsp return err;
6464 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6466 0ebf8283 2019-07-24 stsp goto done;
6468 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6469 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6471 0ebf8283 2019-07-24 stsp if (commit_ref)
6472 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6473 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6474 0ebf8283 2019-07-24 stsp return err;
6477 0ebf8283 2019-07-24 stsp const struct got_error *
6478 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6479 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6481 3e3a69f1 2019-07-25 stsp if (fileindex)
6482 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6483 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6486 69844fba 2019-07-11 stsp static const struct got_error *
6487 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6489 69844fba 2019-07-11 stsp const struct got_error *err;
6490 69844fba 2019-07-11 stsp struct got_reference *ref;
6492 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6493 69844fba 2019-07-11 stsp if (err) {
6494 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6495 69844fba 2019-07-11 stsp return NULL;
6496 69844fba 2019-07-11 stsp return err;
6499 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6500 69844fba 2019-07-11 stsp got_ref_close(ref);
6501 69844fba 2019-07-11 stsp return err;
6504 69844fba 2019-07-11 stsp static const struct got_error *
6505 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6507 69844fba 2019-07-11 stsp const struct got_error *err;
6508 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6509 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6511 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6513 69844fba 2019-07-11 stsp goto done;
6514 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6516 69844fba 2019-07-11 stsp goto done;
6518 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6520 69844fba 2019-07-11 stsp goto done;
6521 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6523 69844fba 2019-07-11 stsp goto done;
6525 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6527 69844fba 2019-07-11 stsp goto done;
6528 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6530 69844fba 2019-07-11 stsp goto done;
6532 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6534 69844fba 2019-07-11 stsp goto done;
6535 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6537 69844fba 2019-07-11 stsp goto done;
6540 69844fba 2019-07-11 stsp free(tmp_branch_name);
6541 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6542 69844fba 2019-07-11 stsp free(branch_ref_name);
6543 69844fba 2019-07-11 stsp free(commit_ref_name);
6544 e600f124 2021-03-21 stsp return err;
6547 e600f124 2021-03-21 stsp const struct got_error *
6548 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6549 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
6551 e600f124 2021-03-21 stsp const struct got_error *err;
6552 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
6553 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
6554 e600f124 2021-03-21 stsp const char *branch_name = NULL;
6555 e600f124 2021-03-21 stsp char *new_id_str = NULL;
6556 e600f124 2021-03-21 stsp char *refname = NULL;
6558 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
6559 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
6560 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6561 e600f124 2021-03-21 stsp branch_name += 11;
6563 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
6565 e600f124 2021-03-21 stsp return err;
6567 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6568 e600f124 2021-03-21 stsp new_id_str) == -1) {
6569 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
6570 e600f124 2021-03-21 stsp goto done;
6573 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
6575 e600f124 2021-03-21 stsp goto done;
6577 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
6579 e600f124 2021-03-21 stsp goto done;
6581 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
6583 e600f124 2021-03-21 stsp free(new_id_str);
6584 e600f124 2021-03-21 stsp free(refname);
6585 e600f124 2021-03-21 stsp free(old_commit_id);
6587 e600f124 2021-03-21 stsp got_ref_close(ref);
6588 69844fba 2019-07-11 stsp return err;
6591 818c7501 2019-07-11 stsp const struct got_error *
6592 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
6593 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6594 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6595 e600f124 2021-03-21 stsp struct got_repository *repo, int create_backup)
6597 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
6598 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
6599 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
6601 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6603 818c7501 2019-07-11 stsp return err;
6605 e600f124 2021-03-21 stsp if (create_backup) {
6606 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6607 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
6609 e600f124 2021-03-21 stsp goto done;
6612 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6614 818c7501 2019-07-11 stsp goto done;
6616 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
6618 818c7501 2019-07-11 stsp goto done;
6620 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
6622 818c7501 2019-07-11 stsp goto done;
6624 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
6626 a615e0e7 2020-12-16 stsp goto done;
6628 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
6630 a615e0e7 2020-12-16 stsp goto done;
6631 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6632 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6633 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
6634 a615e0e7 2020-12-16 stsp err = sync_err;
6636 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
6637 a615e0e7 2020-12-16 stsp free(fileindex_path);
6638 818c7501 2019-07-11 stsp free(new_head_commit_id);
6639 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6640 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6641 818c7501 2019-07-11 stsp err = unlockerr;
6642 818c7501 2019-07-11 stsp return err;
6645 818c7501 2019-07-11 stsp const struct got_error *
6646 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
6647 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6648 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
6649 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6651 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
6652 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
6653 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
6654 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6655 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6656 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
6658 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6660 818c7501 2019-07-11 stsp return err;
6662 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
6663 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
6665 818c7501 2019-07-11 stsp goto done;
6667 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
6669 818c7501 2019-07-11 stsp goto done;
6672 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
6673 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
6674 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
6676 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
6678 818c7501 2019-07-11 stsp goto done;
6680 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6682 818c7501 2019-07-11 stsp goto done;
6684 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
6685 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
6687 ca355955 2019-07-12 stsp goto done;
6689 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
6691 ca355955 2019-07-12 stsp goto done;
6693 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6695 818c7501 2019-07-11 stsp goto done;
6697 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6698 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6699 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6700 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6701 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6702 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6703 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6704 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6705 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6707 1f1abb7e 2019-08-08 stsp goto sync;
6709 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6710 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
6712 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6713 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
6714 ca355955 2019-07-12 stsp err = sync_err;
6716 818c7501 2019-07-11 stsp got_ref_close(resolved);
6717 a3a2faf2 2019-07-12 stsp free(tree_id);
6718 818c7501 2019-07-11 stsp free(commit_id);
6719 0ebf8283 2019-07-24 stsp if (fileindex)
6720 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
6721 0ebf8283 2019-07-24 stsp free(fileindex_path);
6723 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6724 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6725 0ebf8283 2019-07-24 stsp err = unlockerr;
6726 0ebf8283 2019-07-24 stsp return err;
6729 0ebf8283 2019-07-24 stsp const struct got_error *
6730 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6731 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6732 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
6733 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6735 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6736 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6737 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
6738 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
6739 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6740 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
6741 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
6742 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6744 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6745 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6746 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6747 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6749 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6751 0ebf8283 2019-07-24 stsp return err;
6753 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6755 0ebf8283 2019-07-24 stsp goto done;
6757 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
6758 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
6759 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6762 0ebf8283 2019-07-24 stsp goto done;
6764 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6766 0ebf8283 2019-07-24 stsp goto done;
6768 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6770 0ebf8283 2019-07-24 stsp goto done;
6772 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6773 0ebf8283 2019-07-24 stsp worktree);
6775 0ebf8283 2019-07-24 stsp goto done;
6777 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6780 0ebf8283 2019-07-24 stsp goto done;
6782 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6784 0ebf8283 2019-07-24 stsp goto done;
6786 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
6788 0ebf8283 2019-07-24 stsp goto done;
6790 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6791 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6793 0ebf8283 2019-07-24 stsp goto done;
6794 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
6796 0ebf8283 2019-07-24 stsp goto done;
6797 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6798 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
6799 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
6800 0ebf8283 2019-07-24 stsp goto done;
6803 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6804 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6806 0ebf8283 2019-07-24 stsp goto done;
6807 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
6809 0ebf8283 2019-07-24 stsp goto done;
6811 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6813 0ebf8283 2019-07-24 stsp goto done;
6815 0ebf8283 2019-07-24 stsp free(fileindex_path);
6816 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6817 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6818 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6819 0ebf8283 2019-07-24 stsp if (wt_branch)
6820 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
6821 0ebf8283 2019-07-24 stsp if (err) {
6822 0ebf8283 2019-07-24 stsp if (*branch_ref) {
6823 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
6824 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6826 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6827 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6828 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6830 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6831 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6832 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6833 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6835 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
6837 0ebf8283 2019-07-24 stsp return err;
6840 0ebf8283 2019-07-24 stsp const struct got_error *
6841 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
6842 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6844 3e3a69f1 2019-07-25 stsp if (fileindex)
6845 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6846 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
6849 0ebf8283 2019-07-24 stsp const struct got_error *
6850 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
6851 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
6853 0ebf8283 2019-07-24 stsp const struct got_error *err;
6854 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6856 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6858 0ebf8283 2019-07-24 stsp return err;
6860 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6861 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6862 0ebf8283 2019-07-24 stsp return NULL;
6865 0ebf8283 2019-07-24 stsp const struct got_error *
6866 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
6867 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
6868 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6869 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6871 0ebf8283 2019-07-24 stsp const struct got_error *err;
6872 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6873 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6874 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6875 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6876 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6877 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6879 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6880 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6881 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6882 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6884 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6886 3e3a69f1 2019-07-25 stsp return err;
6888 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6890 f032f1f7 2019-08-04 stsp goto done;
6892 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6893 f032f1f7 2019-08-04 stsp &have_staged_files);
6894 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6895 f032f1f7 2019-08-04 stsp goto done;
6896 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6897 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6898 3e3a69f1 2019-07-25 stsp goto done;
6901 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6903 f032f1f7 2019-08-04 stsp goto done;
6905 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6907 0ebf8283 2019-07-24 stsp goto done;
6909 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6911 0ebf8283 2019-07-24 stsp goto done;
6913 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6914 0ebf8283 2019-07-24 stsp worktree);
6916 0ebf8283 2019-07-24 stsp goto done;
6918 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6920 0ebf8283 2019-07-24 stsp goto done;
6922 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6924 0ebf8283 2019-07-24 stsp goto done;
6925 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6927 0ebf8283 2019-07-24 stsp goto done;
6929 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6931 0ebf8283 2019-07-24 stsp goto done;
6932 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6934 0ebf8283 2019-07-24 stsp goto done;
6936 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6938 0ebf8283 2019-07-24 stsp goto done;
6940 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6941 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6942 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6943 0ebf8283 2019-07-24 stsp if (commit_ref)
6944 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6945 0ebf8283 2019-07-24 stsp if (base_commit_ref)
6946 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
6947 0ebf8283 2019-07-24 stsp if (err) {
6948 0ebf8283 2019-07-24 stsp free(*commit_id);
6949 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6950 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6951 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6952 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6953 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6954 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6956 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6957 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6958 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6960 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
6962 0ebf8283 2019-07-24 stsp return err;
6965 0ebf8283 2019-07-24 stsp static const struct got_error *
6966 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6968 0ebf8283 2019-07-24 stsp const struct got_error *err;
6969 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6970 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6972 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6974 0ebf8283 2019-07-24 stsp goto done;
6975 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
6977 0ebf8283 2019-07-24 stsp goto done;
6979 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6980 0ebf8283 2019-07-24 stsp worktree);
6982 0ebf8283 2019-07-24 stsp goto done;
6983 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
6985 0ebf8283 2019-07-24 stsp goto done;
6987 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6989 0ebf8283 2019-07-24 stsp goto done;
6990 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
6992 0ebf8283 2019-07-24 stsp goto done;
6994 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6996 0ebf8283 2019-07-24 stsp goto done;
6997 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
6999 0ebf8283 2019-07-24 stsp goto done;
7001 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7002 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7003 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7004 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7005 0ebf8283 2019-07-24 stsp return err;
7008 0ebf8283 2019-07-24 stsp const struct got_error *
7009 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7010 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7011 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7012 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7014 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7015 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7016 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7017 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7018 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7020 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7022 0ebf8283 2019-07-24 stsp return err;
7024 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7025 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7027 0ebf8283 2019-07-24 stsp goto done;
7029 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7031 0ebf8283 2019-07-24 stsp goto done;
7033 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7035 0ebf8283 2019-07-24 stsp goto done;
7037 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7038 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7040 0ebf8283 2019-07-24 stsp goto done;
7042 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7044 0ebf8283 2019-07-24 stsp goto done;
7046 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7048 0ebf8283 2019-07-24 stsp goto done;
7050 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7051 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7052 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7053 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7054 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7055 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7056 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7057 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7058 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
7060 1f1abb7e 2019-08-08 stsp goto sync;
7062 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7063 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7065 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7066 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
7067 0ebf8283 2019-07-24 stsp err = sync_err;
7069 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
7070 0ebf8283 2019-07-24 stsp free(tree_id);
7071 818c7501 2019-07-11 stsp free(fileindex_path);
7073 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7074 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7075 818c7501 2019-07-11 stsp err = unlockerr;
7076 818c7501 2019-07-11 stsp return err;
7079 0ebf8283 2019-07-24 stsp const struct got_error *
7080 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
7081 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7082 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
7084 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7085 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
7086 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7087 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7089 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7091 0ebf8283 2019-07-24 stsp return err;
7093 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7094 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
7096 e600f124 2021-03-21 stsp goto done;
7098 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7099 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
7101 0ebf8283 2019-07-24 stsp goto done;
7103 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
7105 0ebf8283 2019-07-24 stsp goto done;
7107 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
7109 0ebf8283 2019-07-24 stsp goto done;
7111 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7113 0ebf8283 2019-07-24 stsp goto done;
7115 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7117 a615e0e7 2020-12-16 stsp goto done;
7119 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7121 a615e0e7 2020-12-16 stsp goto done;
7122 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7123 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7124 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7125 a615e0e7 2020-12-16 stsp err = sync_err;
7127 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7128 a615e0e7 2020-12-16 stsp free(fileindex_path);
7129 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
7130 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7131 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7132 0ebf8283 2019-07-24 stsp err = unlockerr;
7133 0ebf8283 2019-07-24 stsp return err;
7136 0ebf8283 2019-07-24 stsp const struct got_error *
7137 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7138 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7140 0ebf8283 2019-07-24 stsp const struct got_error *err;
7141 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7143 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7145 0ebf8283 2019-07-24 stsp return err;
7147 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7149 0ebf8283 2019-07-24 stsp goto done;
7151 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7153 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7154 2822a352 2019-10-15 stsp return err;
7157 2822a352 2019-10-15 stsp const struct got_error *
7158 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7159 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7160 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7161 2822a352 2019-10-15 stsp struct got_repository *repo)
7163 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7164 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7165 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7167 2822a352 2019-10-15 stsp *fileindex = NULL;
7168 2822a352 2019-10-15 stsp *branch_ref = NULL;
7169 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7171 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7173 2822a352 2019-10-15 stsp return err;
7175 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7176 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7177 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7178 2822a352 2019-10-15 stsp "update -b or different branch name required");
7179 2822a352 2019-10-15 stsp goto done;
7182 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7184 2822a352 2019-10-15 stsp goto done;
7186 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
7187 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7188 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7189 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7192 2822a352 2019-10-15 stsp goto done;
7194 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
7196 2822a352 2019-10-15 stsp goto done;
7198 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
7199 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7201 2822a352 2019-10-15 stsp if (err) {
7202 2822a352 2019-10-15 stsp if (*branch_ref) {
7203 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7204 2822a352 2019-10-15 stsp *branch_ref = NULL;
7206 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7207 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7208 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7210 2822a352 2019-10-15 stsp if (*fileindex) {
7211 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7212 2822a352 2019-10-15 stsp *fileindex = NULL;
7214 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7216 0cb83759 2019-08-03 stsp return err;
7219 2822a352 2019-10-15 stsp const struct got_error *
7220 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7221 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7222 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7223 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7224 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7226 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7227 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7228 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7230 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7232 2822a352 2019-10-15 stsp goto done;
7234 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7236 2822a352 2019-10-15 stsp goto done;
7238 2822a352 2019-10-15 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
7239 2822a352 2019-10-15 stsp worktree->path_prefix);
7241 2822a352 2019-10-15 stsp goto done;
7243 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7245 2822a352 2019-10-15 stsp goto done;
7247 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7248 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7250 2822a352 2019-10-15 stsp goto sync;
7252 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7254 2822a352 2019-10-15 stsp goto sync;
7256 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7258 6e210706 2021-01-22 stsp goto sync;
7260 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7262 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7263 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7264 2822a352 2019-10-15 stsp err = sync_err;
7267 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7268 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7269 2822a352 2019-10-15 stsp err = unlockerr;
7270 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7272 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7273 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7274 2822a352 2019-10-15 stsp err = unlockerr;
7275 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7277 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7278 2822a352 2019-10-15 stsp free(fileindex_path);
7279 2822a352 2019-10-15 stsp free(tree_id);
7281 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7282 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7283 2822a352 2019-10-15 stsp err = unlockerr;
7284 2822a352 2019-10-15 stsp return err;
7287 2822a352 2019-10-15 stsp const struct got_error *
7288 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7289 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7290 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7292 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7294 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7296 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7298 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7299 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7300 8b692cd0 2019-10-21 stsp err = unlockerr;
7301 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7303 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7304 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7305 8b692cd0 2019-10-21 stsp err = unlockerr;
7306 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7308 8b692cd0 2019-10-21 stsp return err;
7311 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
7312 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
7313 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7314 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7315 2db2652d 2019-08-07 stsp struct got_repository *repo;
7316 2db2652d 2019-08-07 stsp int have_changes;
7319 2db2652d 2019-08-07 stsp const struct got_error *
7320 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
7321 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7322 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7323 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7325 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
7326 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
7327 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
7328 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
7329 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
7330 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
7332 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
7333 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
7334 8b13ce36 2019-08-08 stsp return NULL;
7335 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
7336 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
7338 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7339 735ef5ac 2019-08-03 stsp if (ie == NULL)
7340 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7342 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7343 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7344 735ef5ac 2019-08-03 stsp relpath) == -1)
7345 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
7347 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
7348 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
7349 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7350 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
7353 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
7354 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7355 3aa5969e 2019-08-06 stsp goto done;
7356 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
7357 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
7358 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
7359 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7360 735ef5ac 2019-08-03 stsp goto done;
7363 2db2652d 2019-08-07 stsp a->have_changes = 1;
7365 735ef5ac 2019-08-03 stsp p = in_repo_path;
7366 735ef5ac 2019-08-03 stsp while (p[0] == '/')
7368 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
7369 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
7370 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
7372 735ef5ac 2019-08-03 stsp free(in_repo_path);
7373 dc424a06 2019-08-07 stsp return err;
7376 2db2652d 2019-08-07 stsp struct stage_path_arg {
7377 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7378 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7379 2db2652d 2019-08-07 stsp struct got_repository *repo;
7380 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
7381 2db2652d 2019-08-07 stsp void *status_arg;
7382 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
7383 2db2652d 2019-08-07 stsp void *patch_arg;
7384 7b5dc508 2019-10-28 stsp int staged_something;
7385 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
7388 2db2652d 2019-08-07 stsp static const struct got_error *
7389 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
7390 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7391 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7392 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7394 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
7395 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
7396 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
7397 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
7398 0cb83759 2019-08-03 stsp uint32_t stage;
7399 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
7400 0aeb8099 2020-07-23 stsp struct stat sb;
7402 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
7403 8b13ce36 2019-08-08 stsp return NULL;
7405 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7406 d3e7c587 2019-08-03 stsp if (ie == NULL)
7407 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7409 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7410 2db2652d 2019-08-07 stsp relpath)== -1)
7411 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
7413 0cb83759 2019-08-03 stsp switch (status) {
7414 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
7415 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
7416 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
7417 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
7418 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
7421 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7422 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
7423 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7424 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7425 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
7428 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
7431 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
7432 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
7433 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
7434 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
7435 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
7439 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
7440 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
7443 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7444 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7445 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7446 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
7448 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
7449 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7450 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
7451 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
7452 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
7453 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
7454 35213c7c 2020-07-23 stsp ssize_t target_len;
7455 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
7456 35213c7c 2020-07-23 stsp sizeof(target_path));
7457 35213c7c 2020-07-23 stsp if (target_len == -1) {
7458 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
7459 35213c7c 2020-07-23 stsp ondisk_path);
7462 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
7463 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
7464 35213c7c 2020-07-23 stsp a->worktree->root_path);
7467 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
7468 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
7469 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
7473 35213c7c 2020-07-23 stsp if (is_bad_symlink)
7474 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7475 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
7477 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7478 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
7480 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7481 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
7483 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7484 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7486 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7487 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
7488 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
7490 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
7491 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
7493 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7494 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7495 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
7496 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
7499 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7501 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7502 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7506 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
7507 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7508 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7509 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7511 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7512 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7515 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
7517 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
7518 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7520 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
7521 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
7524 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7528 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
7529 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
7530 dc424a06 2019-08-07 stsp free(path_content);
7531 2db2652d 2019-08-07 stsp free(ondisk_path);
7532 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
7533 0ebf8283 2019-07-24 stsp return err;
7536 0cb83759 2019-08-03 stsp const struct got_error *
7537 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
7538 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
7539 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
7540 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7541 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
7543 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7544 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
7545 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7546 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
7547 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
7548 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
7549 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
7550 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
7552 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7554 0cb83759 2019-08-03 stsp return err;
7556 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
7557 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
7559 735ef5ac 2019-08-03 stsp goto done;
7560 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7562 735ef5ac 2019-08-03 stsp goto done;
7563 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7565 0cb83759 2019-08-03 stsp goto done;
7567 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
7568 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
7569 2db2652d 2019-08-07 stsp oka.worktree = worktree;
7570 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
7571 2db2652d 2019-08-07 stsp oka.repo = repo;
7572 2db2652d 2019-08-07 stsp oka.have_changes = 0;
7573 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7574 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7575 f2a9dc41 2019-12-13 tracey check_stage_ok, &oka, NULL, NULL, 0, 0);
7577 735ef5ac 2019-08-03 stsp goto done;
7579 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
7580 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7581 2db2652d 2019-08-07 stsp goto done;
7584 2db2652d 2019-08-07 stsp spa.worktree = worktree;
7585 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
7586 2db2652d 2019-08-07 stsp spa.repo = repo;
7587 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
7588 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
7589 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
7590 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
7591 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
7592 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
7593 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7594 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7595 f2a9dc41 2019-12-13 tracey stage_path, &spa, NULL, NULL, 0, 0);
7597 2db2652d 2019-08-07 stsp goto done;
7599 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
7600 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7601 7b5dc508 2019-10-28 stsp goto done;
7604 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7605 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
7606 0cb83759 2019-08-03 stsp err = sync_err;
7608 735ef5ac 2019-08-03 stsp if (head_ref)
7609 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
7610 735ef5ac 2019-08-03 stsp free(head_commit_id);
7611 0cb83759 2019-08-03 stsp free(fileindex_path);
7612 0cb83759 2019-08-03 stsp if (fileindex)
7613 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
7614 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7615 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
7616 0cb83759 2019-08-03 stsp err = unlockerr;
7617 0cb83759 2019-08-03 stsp return err;
7620 ad493afc 2019-08-03 stsp struct unstage_path_arg {
7621 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
7622 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
7623 ad493afc 2019-08-03 stsp struct got_repository *repo;
7624 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
7625 ad493afc 2019-08-03 stsp void *progress_arg;
7626 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
7627 2e1f37b0 2019-08-08 stsp void *patch_arg;
7630 2e1f37b0 2019-08-08 stsp static const struct got_error *
7631 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
7632 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
7633 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
7634 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
7635 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
7637 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
7638 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
7639 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7640 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7641 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
7642 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
7643 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
7645 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7646 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7648 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
7650 2e1f37b0 2019-08-08 stsp return err;
7651 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7653 2e1f37b0 2019-08-08 stsp goto done;
7655 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7657 2e1f37b0 2019-08-08 stsp goto done;
7659 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7661 2e1f37b0 2019-08-08 stsp goto done;
7663 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7665 2e1f37b0 2019-08-08 stsp goto done;
7667 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7669 2e1f37b0 2019-08-08 stsp goto done;
7671 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7673 2e1f37b0 2019-08-08 stsp goto done;
7675 fe621944 2020-11-10 stsp err = got_diff_files(&diffreg_result, f1, label1, f2,
7676 64453f7e 2020-11-21 stsp path2, 3, 0, 1, NULL);
7678 2e1f37b0 2019-08-08 stsp goto done;
7680 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
7681 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
7683 2e1f37b0 2019-08-08 stsp goto done;
7684 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
7685 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
7687 2e1f37b0 2019-08-08 stsp goto done;
7689 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
7690 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
7691 2e1f37b0 2019-08-08 stsp goto done;
7693 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
7694 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
7695 2e1f37b0 2019-08-08 stsp goto done;
7697 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
7698 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7699 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
7700 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
7701 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
7702 fe621944 2020-11-10 stsp nchanges++;
7704 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7705 2e1f37b0 2019-08-08 stsp int choice;
7706 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
7707 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
7708 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
7709 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
7711 2e1f37b0 2019-08-08 stsp goto done;
7712 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
7713 2e1f37b0 2019-08-08 stsp have_content = 1;
7715 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
7716 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
7719 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
7720 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7721 f1e81a05 2019-08-10 stsp outfile, rejectfile);
7723 2e1f37b0 2019-08-08 stsp free(label1);
7725 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
7726 2e1f37b0 2019-08-08 stsp if (staged_blob)
7727 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
7728 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
7729 fe621944 2020-11-10 stsp if (free_err && err == NULL)
7730 fe621944 2020-11-10 stsp err = free_err;
7731 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
7732 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
7733 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
7734 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
7735 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
7736 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
7737 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7738 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
7739 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
7740 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
7741 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
7742 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
7743 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
7744 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
7745 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
7746 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7747 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
7748 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
7749 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7751 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
7752 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
7753 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
7754 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7755 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
7756 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
7757 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7759 2e1f37b0 2019-08-08 stsp free(path1);
7760 2e1f37b0 2019-08-08 stsp free(path2);
7761 fda8017d 2020-07-23 stsp return err;
7764 fda8017d 2020-07-23 stsp static const struct got_error *
7765 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
7766 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
7767 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7768 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
7769 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
7770 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7771 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7773 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
7774 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
7775 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
7776 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
7777 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
7778 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
7779 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
7780 fda8017d 2020-07-23 stsp struct stat sb;
7782 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
7783 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
7784 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
7786 fda8017d 2020-07-23 stsp return err;
7788 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
7789 fda8017d 2020-07-23 stsp return NULL;
7791 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
7792 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
7793 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
7795 fda8017d 2020-07-23 stsp goto done;
7798 fda8017d 2020-07-23 stsp f = fopen(path_unstaged_content, "r");
7799 fda8017d 2020-07-23 stsp if (f == NULL) {
7800 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
7801 fda8017d 2020-07-23 stsp path_unstaged_content);
7802 fda8017d 2020-07-23 stsp goto done;
7804 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
7805 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
7806 fda8017d 2020-07-23 stsp goto done;
7808 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
7809 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7810 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
7812 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
7813 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
7814 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
7815 fda8017d 2020-07-23 stsp goto done;
7817 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
7818 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7819 fda8017d 2020-07-23 stsp goto done;
7821 fda8017d 2020-07-23 stsp link_target[r] = '\0';
7822 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
7823 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
7824 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
7825 fda8017d 2020-07-23 stsp progress_arg);
7827 fda8017d 2020-07-23 stsp int local_changes_subsumed;
7829 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
7831 67a66647 2021-05-31 stsp return err;
7833 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
7834 67a66647 2021-05-31 stsp parent) == -1) {
7835 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
7836 67a66647 2021-05-31 stsp base_path = NULL;
7837 67a66647 2021-05-31 stsp goto done;
7840 67a66647 2021-05-31 stsp err = got_opentemp_named(&blob_base_path, &f_base, base_path);
7842 67a66647 2021-05-31 stsp goto done;
7843 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
7844 67a66647 2021-05-31 stsp blob_base);
7846 67a66647 2021-05-31 stsp goto done;
7849 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
7850 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
7852 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7853 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
7854 eec2f5a9 2021-06-03 stsp ondisk_path);
7856 eec2f5a9 2021-06-03 stsp goto done;
7859 eec2f5a9 2021-06-03 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
7860 eec2f5a9 2021-06-03 stsp if (fd == -1) {
7861 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
7862 eec2f5a9 2021-06-03 stsp goto done;
7864 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
7865 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
7866 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
7867 eec2f5a9 2021-06-03 stsp close(fd);
7868 eec2f5a9 2021-06-03 stsp goto done;
7872 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
7873 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
7874 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
7875 54d5be07 2021-06-03 stsp label_orig, "unstaged", NULL,
7876 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
7879 fda8017d 2020-07-23 stsp goto done;
7881 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
7882 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7883 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
7885 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7886 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
7889 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
7890 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
7891 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
7892 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
7893 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
7894 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
7895 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
7896 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
7897 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
7898 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
7899 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
7900 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
7901 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
7902 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
7903 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
7904 fda8017d 2020-07-23 stsp free(path_unstaged_content);
7905 fda8017d 2020-07-23 stsp free(path_new_staged_content);
7906 67a66647 2021-05-31 stsp free(blob_base_path);
7907 67a66647 2021-05-31 stsp free(parent);
7908 67a66647 2021-05-31 stsp free(base_path);
7909 2e1f37b0 2019-08-08 stsp return err;
7912 ad493afc 2019-08-03 stsp static const struct got_error *
7913 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
7914 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
7915 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7916 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7918 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
7919 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
7920 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
7921 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7922 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
7923 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
7924 ad493afc 2019-08-03 stsp int local_changes_subsumed;
7925 9bc94a15 2019-08-03 stsp struct stat sb;
7927 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
7928 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
7929 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
7930 2e1f37b0 2019-08-08 stsp return NULL;
7932 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7933 ad493afc 2019-08-03 stsp if (ie == NULL)
7934 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7936 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7938 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
7940 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
7941 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
7943 f69721c3 2019-10-21 stsp goto done;
7944 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7945 f69721c3 2019-10-21 stsp id_str) == -1) {
7946 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
7947 f69721c3 2019-10-21 stsp goto done;
7950 ad493afc 2019-08-03 stsp switch (staged_status) {
7951 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
7952 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
7953 ad493afc 2019-08-03 stsp blob_id, 8192);
7956 ad493afc 2019-08-03 stsp /* fall through */
7957 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
7958 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7959 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
7960 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7961 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7962 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7965 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
7968 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
7969 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
7970 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
7971 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
7972 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
7973 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
7976 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
7977 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
7980 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
7981 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
7982 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
7983 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
7984 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
7985 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
7986 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
7987 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
7988 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
7990 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
7991 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7992 36bf999c 2020-07-23 stsp char *staged_target;
7993 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
7994 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
7996 36bf999c 2020-07-23 stsp goto done;
7997 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
7998 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
7999 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
8000 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
8001 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
8002 36bf999c 2020-07-23 stsp free(staged_target);
8004 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
8005 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
8006 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
8007 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
8008 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
8009 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8010 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
8014 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8017 2ac8aa02 2020-11-09 stsp if (err == NULL) {
8018 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
8019 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
8020 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8023 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
8024 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8025 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8026 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8027 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8030 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8032 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8033 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8037 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8038 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8039 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
8040 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
8043 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
8047 ad493afc 2019-08-03 stsp free(ondisk_path);
8048 ad493afc 2019-08-03 stsp if (blob_base)
8049 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
8050 ad493afc 2019-08-03 stsp if (blob_staged)
8051 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
8052 f69721c3 2019-10-21 stsp free(id_str);
8053 f69721c3 2019-10-21 stsp free(label_orig);
8054 ad493afc 2019-08-03 stsp return err;
8057 ad493afc 2019-08-03 stsp const struct got_error *
8058 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
8059 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
8060 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
8061 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8062 ad493afc 2019-08-03 stsp struct got_repository *repo)
8064 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8065 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
8066 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8067 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
8068 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
8070 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8072 ad493afc 2019-08-03 stsp return err;
8074 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8076 ad493afc 2019-08-03 stsp goto done;
8078 ad493afc 2019-08-03 stsp upa.worktree = worktree;
8079 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
8080 ad493afc 2019-08-03 stsp upa.repo = repo;
8081 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
8082 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
8083 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
8084 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
8085 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8086 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8087 f2a9dc41 2019-12-13 tracey unstage_path, &upa, NULL, NULL, 0, 0);
8089 ad493afc 2019-08-03 stsp goto done;
8092 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8093 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
8094 ad493afc 2019-08-03 stsp err = sync_err;
8096 ad493afc 2019-08-03 stsp free(fileindex_path);
8097 ad493afc 2019-08-03 stsp if (fileindex)
8098 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
8099 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8100 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
8101 ad493afc 2019-08-03 stsp err = unlockerr;
8102 ad493afc 2019-08-03 stsp return err;
8105 b2118c49 2020-07-28 stsp struct report_file_info_arg {
8106 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
8107 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
8108 b2118c49 2020-07-28 stsp void *info_arg;
8109 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
8110 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
8111 b2118c49 2020-07-28 stsp void *cancel_arg;
8114 b2118c49 2020-07-28 stsp static const struct got_error *
8115 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
8117 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
8118 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
8119 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
8120 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8121 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
8122 b2118c49 2020-07-28 stsp int stage;
8124 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8125 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
8127 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
8128 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8129 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
8132 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
8133 b2118c49 2020-07-28 stsp return NULL;
8135 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_blob(ie)) {
8136 b2118c49 2020-07-28 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8137 b2118c49 2020-07-28 stsp blob_idp = &blob_id;
8139 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
8140 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8141 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
8142 b2118c49 2020-07-28 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8143 b2118c49 2020-07-28 stsp SHA1_DIGEST_LENGTH);
8144 b2118c49 2020-07-28 stsp staged_blob_idp = &staged_blob_id;
8147 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_commit(ie)) {
8148 b2118c49 2020-07-28 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8149 b2118c49 2020-07-28 stsp commit_idp = &commit_id;
8152 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8153 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8156 b2118c49 2020-07-28 stsp const struct got_error *
8157 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
8158 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
8159 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
8160 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8163 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
8164 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
8165 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
8166 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
8168 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
8170 b2118c49 2020-07-28 stsp return err;
8172 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8174 b2118c49 2020-07-28 stsp goto done;
8176 b2118c49 2020-07-28 stsp arg.worktree = worktree;
8177 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
8178 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
8179 b2118c49 2020-07-28 stsp arg.paths = paths;
8180 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
8181 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
8182 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8185 b2118c49 2020-07-28 stsp free(fileindex_path);
8186 b2118c49 2020-07-28 stsp if (fileindex)
8187 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
8188 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
8189 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
8190 b2118c49 2020-07-28 stsp err = unlockerr;
8191 b2118c49 2020-07-28 stsp return err;