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;
502 cde76477 2018-03-11 stsp free(worktree->repo_path);
503 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
504 eaccb85f 2018-12-25 stsp free(worktree->base_commit_id);
505 36a38700 2019-05-10 stsp free(worktree->head_ref_name);
506 a6b21eef 2021-01-21 stsp if (worktree->lockfd != -1) {
507 3a6ce05a 2019-02-11 stsp if (close(worktree->lockfd) != 0)
508 638f9024 2019-05-13 stsp err = got_error_from_errno2("close",
509 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree));
511 7f11502c 2019-08-28 hiltjo free(worktree->root_path);
512 50b0790e 2020-09-11 stsp free(worktree->gotconfig_path);
513 50b0790e 2020-09-11 stsp got_gotconfig_free(worktree->gotconfig);
514 437adc9d 2020-12-10 yzhong close(worktree->root_fd);
515 a06ff56f 2021-01-21 naddy free(worktree);
516 3a6ce05a 2019-02-11 stsp return err;
519 2fbdb5ae 2018-12-29 stsp const char *
520 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
522 c7f4312f 2019-02-05 stsp return worktree->root_path;
525 c7f4312f 2019-02-05 stsp const char *
526 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
528 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
530 49520a32 2018-12-29 stsp const char *
531 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
533 f609be2e 2018-12-29 stsp return worktree->path_prefix;
536 e5dc7198 2018-12-29 stsp const struct got_error *
537 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
538 e5dc7198 2018-12-29 stsp const char *path_prefix)
540 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
542 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
543 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
544 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
546 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
547 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
548 e5dc7198 2018-12-29 stsp free(absprefix);
549 e5dc7198 2018-12-29 stsp return NULL;
552 bc70eb79 2019-05-09 stsp const char *
553 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
555 36a38700 2019-05-10 stsp return worktree->head_ref_name;
558 024e9686 2019-05-14 stsp const struct got_error *
559 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
560 024e9686 2019-05-14 stsp struct got_reference *head_ref)
562 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
563 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
565 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
566 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
567 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
568 024e9686 2019-05-14 stsp path_got = NULL;
572 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
573 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
574 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
578 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
582 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
583 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
585 024e9686 2019-05-14 stsp free(path_got);
587 024e9686 2019-05-14 stsp free(head_ref_name);
588 024e9686 2019-05-14 stsp return err;
591 b72f483a 2019-02-05 stsp struct got_object_id *
592 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
594 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
597 507dc3bb 2018-12-29 stsp const struct got_error *
598 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
599 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
601 507dc3bb 2018-12-29 stsp const struct got_error *err;
602 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
603 507dc3bb 2018-12-29 stsp char *id_str = NULL;
604 507dc3bb 2018-12-29 stsp char *path_got = NULL;
606 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
607 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
608 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
609 507dc3bb 2018-12-29 stsp path_got = NULL;
613 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
615 507dc3bb 2018-12-29 stsp return err;
617 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
618 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
622 507dc3bb 2018-12-29 stsp /* Record our base commit. */
623 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
626 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
630 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
631 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
632 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
633 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
638 507dc3bb 2018-12-29 stsp got_object_close(obj);
639 507dc3bb 2018-12-29 stsp free(id_str);
640 507dc3bb 2018-12-29 stsp free(path_got);
641 507dc3bb 2018-12-29 stsp return err;
644 50b0790e 2020-09-11 stsp const struct got_gotconfig *
645 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
647 50b0790e 2020-09-11 stsp return worktree->gotconfig;
650 9d31a1d8 2018-03-11 stsp static const struct got_error *
651 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
653 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
654 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
655 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
656 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
657 86c3caaf 2018-03-09 stsp return NULL;
660 21908da4 2019-01-13 stsp static const struct got_error *
661 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
663 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
664 4a1ddfc2 2019-01-12 stsp char *abspath;
666 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
667 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
669 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
670 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
671 ddcd8544 2019-03-11 stsp struct stat sb;
672 ddcd8544 2019-03-11 stsp err = NULL;
673 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
674 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
675 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
676 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
677 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
680 4a1ddfc2 2019-01-12 stsp free(abspath);
681 68c76935 2019-02-19 stsp return err;
684 68c76935 2019-02-19 stsp static const struct got_error *
685 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
687 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
688 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
689 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
690 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
695 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
696 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
697 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
700 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
701 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
702 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
705 68c76935 2019-02-19 stsp if (flen1 == 0) {
706 68c76935 2019-02-19 stsp if (flen2 != 0)
709 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
710 68c76935 2019-02-19 stsp if (flen1 != 0)
713 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
714 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
724 68c76935 2019-02-19 stsp return err;
727 68c76935 2019-02-19 stsp static const struct got_error *
728 68c76935 2019-02-19 stsp check_files_equal(int *same, const char *f1_path, const char *f2_path)
730 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
731 68c76935 2019-02-19 stsp struct stat sb;
732 68c76935 2019-02-19 stsp size_t size1, size2;
733 68c76935 2019-02-19 stsp FILE *f1 = NULL, *f2 = NULL;
737 68c76935 2019-02-19 stsp if (lstat(f1_path, &sb) != 0) {
738 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f1_path);
741 68c76935 2019-02-19 stsp size1 = sb.st_size;
743 68c76935 2019-02-19 stsp if (lstat(f2_path, &sb) != 0) {
744 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f2_path);
747 68c76935 2019-02-19 stsp size2 = sb.st_size;
749 68c76935 2019-02-19 stsp if (size1 != size2) {
751 68c76935 2019-02-19 stsp return NULL;
754 68c76935 2019-02-19 stsp f1 = fopen(f1_path, "r");
755 68c76935 2019-02-19 stsp if (f1 == NULL)
756 60522982 2019-12-15 stsp return got_error_from_errno2("fopen", f1_path);
758 68c76935 2019-02-19 stsp f2 = fopen(f2_path, "r");
759 68c76935 2019-02-19 stsp if (f2 == NULL) {
760 60522982 2019-12-15 stsp err = got_error_from_errno2("fopen", f2_path);
764 68c76935 2019-02-19 stsp err = check_file_contents_equal(same, f1, f2);
766 56b63ca4 2021-01-22 stsp if (f1 && fclose(f1) == EOF && err == NULL)
767 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
768 56b63ca4 2021-01-22 stsp if (f2 && fclose(f2) == EOF && err == NULL)
769 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
771 6353ad76 2019-02-08 stsp return err;
775 46b6ee73 2019-06-04 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
776 14c901f1 2019-08-08 stsp * the file at deriv_path acts as the first derived version, and the
777 14c901f1 2019-08-08 stsp * file on disk acts as the second derived version.
779 6353ad76 2019-02-08 stsp static const struct got_error *
780 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
781 46b6ee73 2019-06-04 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
782 14c901f1 2019-08-08 stsp const char *path, uint16_t st_mode, const char *deriv_path,
783 f69721c3 2019-10-21 stsp const char *label_orig, const char *label_deriv,
784 f69721c3 2019-10-21 stsp struct got_repository *repo,
785 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
787 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
788 6353ad76 2019-02-08 stsp int merged_fd = -1;
789 14c901f1 2019-08-08 stsp FILE *f_orig = NULL;
790 14c901f1 2019-08-08 stsp char *blob_orig_path = NULL;
791 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
792 234035bc 2019-06-01 stsp int overlapcnt = 0;
793 3524bbf9 2020-10-20 stsp char *parent = NULL;
794 3b9f0f87 2020-07-23 stsp char *symlink_path = NULL;
795 3b9f0f87 2020-07-23 stsp FILE *symlinkf = NULL;
797 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
799 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
801 3524bbf9 2020-10-20 stsp return err;
803 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
804 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
808 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
812 af54ae4a 2019-02-19 stsp free(base_path);
813 46b6ee73 2019-06-04 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
814 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
815 af54ae4a 2019-02-19 stsp base_path = NULL;
819 46b6ee73 2019-06-04 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
822 46b6ee73 2019-06-04 stsp if (blob_orig) {
823 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
824 46b6ee73 2019-06-04 stsp blob_orig);
829 1430b4e0 2019-03-27 stsp * If the file has no blob, this is an "add vs add" conflict,
830 1430b4e0 2019-03-27 stsp * and we simply use an empty ancestor file to make both files
831 1430b4e0 2019-03-27 stsp * appear in the merged result in their entirety.
836 3b9f0f87 2020-07-23 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
837 3b9f0f87 2020-07-23 stsp * target path into a temporary file and use that file with diff3.
839 3b9f0f87 2020-07-23 stsp if (S_ISLNK(st_mode)) {
840 3b9f0f87 2020-07-23 stsp char target_path[PATH_MAX];
841 3b9f0f87 2020-07-23 stsp ssize_t target_len;
844 3b9f0f87 2020-07-23 stsp free(base_path);
845 3b9f0f87 2020-07-23 stsp if (asprintf(&base_path, "%s/got-symlink-merge",
846 3b9f0f87 2020-07-23 stsp parent) == -1) {
847 3b9f0f87 2020-07-23 stsp err = got_error_from_errno("asprintf");
848 3b9f0f87 2020-07-23 stsp base_path = NULL;
851 3b9f0f87 2020-07-23 stsp err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
854 3b9f0f87 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
855 3b9f0f87 2020-07-23 stsp sizeof(target_path));
856 3b9f0f87 2020-07-23 stsp if (target_len == -1) {
857 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
860 3b9f0f87 2020-07-23 stsp n = fwrite(target_path, 1, target_len, symlinkf);
861 3b9f0f87 2020-07-23 stsp if (n != target_len) {
862 3b9f0f87 2020-07-23 stsp err = got_ferror(symlinkf, GOT_ERR_IO);
865 3b9f0f87 2020-07-23 stsp if (fflush(symlinkf) == EOF) {
866 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("fflush", symlink_path);
871 14c901f1 2019-08-08 stsp err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
872 3b9f0f87 2020-07-23 stsp blob_orig_path, symlink_path ? symlink_path : ondisk_path,
873 3b9f0f87 2020-07-23 stsp label_deriv, label_orig, NULL);
877 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
878 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
882 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
883 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
887 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
888 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
889 14c901f1 2019-08-08 stsp err = check_files_equal(local_changes_subsumed, deriv_path,
890 68c76935 2019-02-19 stsp merged_path);
895 2ad902c0 2019-12-15 stsp if (fchmod(merged_fd, st_mode) != 0) {
896 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
900 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
901 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
902 230a42bd 2019-05-11 jcs ondisk_path);
907 fdcb7daf 2019-12-15 stsp if (merged_path)
908 fdcb7daf 2019-12-15 stsp unlink(merged_path);
910 3b9f0f87 2020-07-23 stsp if (symlink_path) {
911 3b9f0f87 2020-07-23 stsp if (unlink(symlink_path) == -1 && err == NULL)
912 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("unlink", symlink_path);
914 3b9f0f87 2020-07-23 stsp if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
915 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("fclose", symlink_path);
916 3b9f0f87 2020-07-23 stsp free(symlink_path);
917 3a6ce05a 2019-02-11 stsp if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
918 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
919 56b63ca4 2021-01-22 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
920 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
921 6353ad76 2019-02-08 stsp free(merged_path);
922 af54ae4a 2019-02-19 stsp free(base_path);
923 46b6ee73 2019-06-04 stsp if (blob_orig_path) {
924 46b6ee73 2019-06-04 stsp unlink(blob_orig_path);
925 46b6ee73 2019-06-04 stsp free(blob_orig_path);
927 3524bbf9 2020-10-20 stsp free(parent);
928 af57b12a 2020-07-23 stsp return err;
931 af57b12a 2020-07-23 stsp static const struct got_error *
932 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
933 af57b12a 2020-07-23 stsp size_t target_len)
935 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
936 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
937 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
938 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
939 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
940 af57b12a 2020-07-23 stsp ondisk_path);
941 af57b12a 2020-07-23 stsp return NULL;
945 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
946 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
947 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
948 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
950 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
951 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
953 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
954 993e2a1b 2020-07-23 stsp * has deleted this symlink.
956 11cc08c1 2020-07-23 stsp static const struct got_error *
957 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
958 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
959 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
961 11cc08c1 2020-07-23 stsp const struct got_error *err;
962 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
963 11cc08c1 2020-07-23 stsp FILE *f = NULL;
965 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
967 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
969 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
970 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
971 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
975 11cc08c1 2020-07-23 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict");
977 3818e3c4 2020-11-01 naddy goto done;
979 3818e3c4 2020-11-01 naddy if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
980 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
984 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
985 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
986 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
987 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
988 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
989 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
990 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
991 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
992 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
993 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
997 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
998 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
1001 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
1002 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
1003 11cc08c1 2020-07-23 stsp goto done;
1006 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1007 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
1008 11cc08c1 2020-07-23 stsp free(path);
1009 11cc08c1 2020-07-23 stsp free(id_str);
1010 11cc08c1 2020-07-23 stsp free(label_deriv);
1011 11cc08c1 2020-07-23 stsp return err;
1014 d219f183 2020-07-23 stsp /* forward declaration */
1015 d219f183 2020-07-23 stsp static const struct got_error *
1016 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
1017 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
1018 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
1019 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
1022 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
1023 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
1024 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
1025 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
1027 af57b12a 2020-07-23 stsp static const struct got_error *
1028 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
1029 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
1030 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
1031 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1032 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1034 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
1035 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
1036 af57b12a 2020-07-23 stsp struct stat sb;
1037 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
1038 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
1039 11cc08c1 2020-07-23 stsp int have_local_change = 0;
1040 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
1042 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
1043 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
1045 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
1046 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
1047 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
1048 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
1049 af57b12a 2020-07-23 stsp ondisk_path);
1050 af57b12a 2020-07-23 stsp goto done;
1052 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
1054 526a746f 2020-07-23 stsp if (blob_orig) {
1055 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1057 526a746f 2020-07-23 stsp goto done;
1060 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
1061 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
1062 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1063 11cc08c1 2020-07-23 stsp have_local_change = 1;
1065 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
1066 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
1067 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
1068 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1069 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
1071 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
1072 11cc08c1 2020-07-23 stsp if (ancestor_target) {
1073 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
1074 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1076 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
1077 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1078 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
1079 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1082 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
1083 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1084 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
1085 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
1087 11cc08c1 2020-07-23 stsp goto done;
1088 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1091 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
1092 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
1093 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
1094 af57b12a 2020-07-23 stsp strlen(deriv_target));
1096 af57b12a 2020-07-23 stsp goto done;
1097 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1098 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
1099 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
1100 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1101 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
1102 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1105 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1106 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
1107 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
1109 ea7786be 2020-07-23 stsp goto done;
1110 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1116 af57b12a 2020-07-23 stsp free(ancestor_target);
1117 14c901f1 2019-08-08 stsp return err;
1121 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
1122 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
1123 14c901f1 2019-08-08 stsp * acts as the second derived version.
1125 14c901f1 2019-08-08 stsp static const struct got_error *
1126 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1127 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
1128 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
1129 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
1130 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1131 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1133 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
1134 14c901f1 2019-08-08 stsp FILE *f_deriv = NULL;
1135 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1136 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
1138 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
1140 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1142 ed6b5030 2020-10-20 stsp return err;
1144 14c901f1 2019-08-08 stsp free(base_path);
1145 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1146 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1147 14c901f1 2019-08-08 stsp base_path = NULL;
1148 14c901f1 2019-08-08 stsp goto done;
1151 14c901f1 2019-08-08 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1153 14c901f1 2019-08-08 stsp goto done;
1154 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1155 14c901f1 2019-08-08 stsp blob_deriv);
1157 14c901f1 2019-08-08 stsp goto done;
1159 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1161 14c901f1 2019-08-08 stsp goto done;
1162 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1163 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1164 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1165 14c901f1 2019-08-08 stsp goto done;
1168 14c901f1 2019-08-08 stsp err = merge_file(local_changes_subsumed, worktree, blob_orig,
1169 f69721c3 2019-10-21 stsp ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1170 f69721c3 2019-10-21 stsp label_deriv, repo, progress_cb, progress_arg);
1172 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1173 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1174 14c901f1 2019-08-08 stsp free(base_path);
1175 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1176 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1177 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1179 6353ad76 2019-02-08 stsp free(id_str);
1180 818c7501 2019-07-11 stsp free(label_deriv);
1181 ed6b5030 2020-10-20 stsp free(parent);
1182 4a1ddfc2 2019-01-12 stsp return err;
1185 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1186 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1187 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1188 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1190 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1191 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1193 65b05cec 2020-07-23 stsp *new_iep = NULL;
1195 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1197 054041d0 2020-06-24 stsp return err;
1199 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1200 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1202 054041d0 2020-06-24 stsp goto done;
1204 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1207 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1209 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1210 13d9040b 2019-03-27 stsp return err;
1213 1ebedb77 2019-10-19 stsp static mode_t
1214 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1216 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1218 1ebedb77 2019-10-19 stsp if (executable) {
1219 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1220 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1221 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1222 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1223 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1224 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1227 1ebedb77 2019-10-19 stsp return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1230 8ba819a3 2020-07-23 stsp /* forward declaration */
1231 13d9040b 2019-03-27 stsp static const struct got_error *
1232 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1233 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1234 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1235 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1236 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1237 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1240 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1241 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1243 8ba819a3 2020-07-23 stsp static const struct got_error *
1244 5a1fbc73 2020-07-23 stsp replace_existing_symlink(const char *ondisk_path, const char *target_path,
1245 5a1fbc73 2020-07-23 stsp size_t target_len)
1247 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1248 5a1fbc73 2020-07-23 stsp ssize_t elen;
1249 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1253 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1254 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1255 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1256 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1257 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1258 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1260 5a1fbc73 2020-07-23 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1261 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1262 5a1fbc73 2020-07-23 stsp if (errno != ELOOP)
1263 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1265 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1266 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1267 5a1fbc73 2020-07-23 stsp if (elen == -1)
1268 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1270 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1271 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1272 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1275 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1276 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1277 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1278 5a1fbc73 2020-07-23 stsp return err;
1281 3c1ec782 2020-07-23 stsp static const struct got_error *
1282 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1283 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1285 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1286 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1287 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1289 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1291 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1292 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1293 3c1ec782 2020-07-23 stsp return NULL;
1297 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1298 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1299 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1300 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1301 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1303 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1304 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1305 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1307 ce031e9e 2020-10-20 stsp return err;
1308 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1309 ce031e9e 2020-10-20 stsp free(parent);
1310 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1312 ce031e9e 2020-10-20 stsp free(parent);
1313 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1314 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1315 3c1ec782 2020-07-23 stsp free(abspath);
1316 3c1ec782 2020-07-23 stsp return err;
1318 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1319 3c1ec782 2020-07-23 stsp free(abspath);
1321 3c1ec782 2020-07-23 stsp return err;
1323 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1325 3c1ec782 2020-07-23 stsp return err;
1328 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1329 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1330 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1331 3c1ec782 2020-07-23 stsp return NULL;
1334 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1335 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1336 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1337 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1338 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1339 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1341 3c1ec782 2020-07-23 stsp free(path_got);
1342 3c1ec782 2020-07-23 stsp return NULL;
1345 5a1fbc73 2020-07-23 stsp static const struct got_error *
1346 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1347 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1348 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1349 c90c8ce3 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1350 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1352 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1353 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1354 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1355 906c123b 2020-07-23 stsp char *path_got = NULL;
1356 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1357 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1359 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1362 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1363 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1364 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1365 8ba819a3 2020-07-23 stsp * in the blob object.
1368 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1369 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1370 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1371 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1372 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1373 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1374 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1375 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1376 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1377 3b9f0f87 2020-07-23 stsp progress_arg);
1379 8ba819a3 2020-07-23 stsp if (len > 0) {
1380 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1381 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1382 8ba819a3 2020-07-23 stsp len - hdrlen);
1383 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1384 8ba819a3 2020-07-23 stsp hdrlen = 0;
1386 8ba819a3 2020-07-23 stsp } while (len != 0);
1387 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1389 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1390 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1392 3c1ec782 2020-07-23 stsp return err;
1394 3c1ec782 2020-07-23 stsp if (*is_bad_symlink) {
1395 906c123b 2020-07-23 stsp /* install as a regular file */
1396 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1397 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1398 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1399 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1400 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1401 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1402 906c123b 2020-07-23 stsp goto done;
1405 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1406 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1407 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1408 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1409 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1410 c90c8ce3 2020-07-23 stsp goto done;
1412 5a1fbc73 2020-07-23 stsp err = replace_existing_symlink(ondisk_path,
1413 5a1fbc73 2020-07-23 stsp target_path, target_len);
1415 f35fa46a 2020-07-23 stsp goto done;
1416 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1417 5a1fbc73 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1418 6e1eade5 2020-07-23 stsp reverting_versioned_file ?
1419 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1422 5a1fbc73 2020-07-23 stsp goto done; /* Nothing else to do. */
1425 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1426 f4994adc 2020-10-20 stsp char *parent;
1427 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1429 f4994adc 2020-10-20 stsp goto done;
1430 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1431 f4994adc 2020-10-20 stsp free(parent);
1433 f35fa46a 2020-07-23 stsp goto done;
1435 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1436 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1438 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1439 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1440 f35fa46a 2020-07-23 stsp goto done;
1444 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1445 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1446 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1447 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1448 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1449 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1450 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1451 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1452 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1453 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1454 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1455 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1456 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1458 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1459 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1461 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1462 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1463 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1465 906c123b 2020-07-23 stsp free(path_got);
1466 8ba819a3 2020-07-23 stsp return err;
1469 8ba819a3 2020-07-23 stsp static const struct got_error *
1470 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1471 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1472 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1473 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1474 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1475 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1477 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1478 507dc3bb 2018-12-29 stsp int fd = -1;
1479 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1480 507dc3bb 2018-12-29 stsp int update = 0;
1481 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1483 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1484 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
1485 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1486 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1487 f5375317 2020-10-20 stsp char *parent;
1488 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1490 f5375317 2020-10-20 stsp return err;
1491 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1492 f5375317 2020-10-20 stsp free(parent);
1494 21908da4 2019-01-13 stsp return err;
1495 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1496 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1497 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
1498 21908da4 2019-01-13 stsp if (fd == -1)
1499 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1500 230a42bd 2019-05-11 jcs ondisk_path);
1501 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1502 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1503 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1504 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1505 3b9f0f87 2020-07-23 stsp goto done;
1507 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1508 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1509 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1510 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1511 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1512 507dc3bb 2018-12-29 stsp goto done;
1514 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1515 507dc3bb 2018-12-29 stsp ondisk_path);
1517 507dc3bb 2018-12-29 stsp goto done;
1518 507dc3bb 2018-12-29 stsp update = 1;
1521 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1524 3818e3c4 2020-11-01 naddy if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1525 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod",
1526 3818e3c4 2020-11-01 naddy update ? tmppath : ondisk_path);
1527 3818e3c4 2020-11-01 naddy goto done;
1530 bd6aa359 2020-07-23 stsp if (progress_cb) {
1531 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1532 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1534 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1535 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1538 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1539 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1541 bd6aa359 2020-07-23 stsp goto done;
1544 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1546 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1547 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1550 9d31a1d8 2018-03-11 stsp if (len > 0) {
1551 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1552 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1553 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1554 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1555 b87c6f83 2018-12-24 stsp goto done;
1556 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1557 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1558 b87c6f83 2018-12-24 stsp goto done;
1560 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1562 9d31a1d8 2018-03-11 stsp } while (len != 0);
1564 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1565 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1566 816dc654 2019-02-16 stsp goto done;
1569 507dc3bb 2018-12-29 stsp if (update) {
1570 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1571 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1572 f6d8c0ac 2020-11-09 stsp goto done;
1574 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1575 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1576 230a42bd 2019-05-11 jcs ondisk_path);
1577 68ed9ba5 2019-02-10 stsp goto done;
1579 63df146d 2020-11-09 stsp free(tmppath);
1580 63df146d 2020-11-09 stsp tmppath = NULL;
1584 3a6ce05a 2019-02-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
1585 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1586 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1587 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1588 507dc3bb 2018-12-29 stsp free(tmppath);
1589 6353ad76 2019-02-08 stsp return err;
1592 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1593 6353ad76 2019-02-08 stsp static const struct got_error *
1594 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1596 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1597 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1598 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1599 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1600 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1602 7154f6ce 2019-03-27 stsp int i = 0;
1603 9bdd68dd 2021-01-02 naddy char *line = NULL;
1604 9bdd68dd 2021-01-02 naddy size_t linesize = 0;
1605 9bdd68dd 2021-01-02 naddy ssize_t linelen;
1607 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1608 9bdd68dd 2021-01-02 naddy linelen = getline(&line, &linesize, f);
1609 9bdd68dd 2021-01-02 naddy if (linelen == -1) {
1610 7154f6ce 2019-03-27 stsp if (feof(f))
1612 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1616 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1617 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1619 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1624 9bdd68dd 2021-01-02 naddy free(line);
1626 7154f6ce 2019-03-27 stsp return err;
1629 e2b1e152 2019-07-13 stsp static int
1630 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1632 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1633 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1636 1ebedb77 2019-10-19 stsp static int
1637 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1639 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1640 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1641 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1642 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1643 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1644 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1647 c363b2c1 2019-08-03 stsp static unsigned char
1648 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1650 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1651 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1652 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1653 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1654 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1655 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1656 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1658 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1662 a919d5c4 2020-07-23 stsp static const struct got_error *
1663 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1664 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1665 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1667 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1668 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1669 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1670 a919d5c4 2020-07-23 stsp ssize_t elen;
1671 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1672 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1673 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1675 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1677 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1679 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1681 a919d5c4 2020-07-23 stsp return err;
1682 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1684 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1685 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1687 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1689 a919d5c4 2020-07-23 stsp if (len > 0) {
1690 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1691 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1692 a919d5c4 2020-07-23 stsp len - hdrlen);
1693 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1694 a919d5c4 2020-07-23 stsp hdrlen = 0;
1696 a919d5c4 2020-07-23 stsp } while (len != 0);
1697 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1699 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1700 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1701 a919d5c4 2020-07-23 stsp if (elen == -1)
1702 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1704 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1705 a919d5c4 2020-07-23 stsp if (elen == -1)
1706 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1709 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1710 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1712 a919d5c4 2020-07-23 stsp return NULL;
1715 7154f6ce 2019-03-27 stsp static const struct got_error *
1716 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1717 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1718 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1720 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1721 6353ad76 2019-02-08 stsp struct got_object_id id;
1722 6353ad76 2019-02-08 stsp size_t hdrlen;
1723 1338848f 2019-12-13 stsp int fd = -1;
1724 6353ad76 2019-02-08 stsp FILE *f = NULL;
1725 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1726 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1727 6353ad76 2019-02-08 stsp size_t flen, blen;
1728 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1730 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1733 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1734 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1735 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1737 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1738 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1739 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1740 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1741 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1743 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1744 882ef1b9 2019-12-13 stsp goto done;
1746 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1747 3d35a492 2019-12-13 stsp goto done;
1750 7f91a133 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1751 a919d5c4 2020-07-23 stsp if (fd == -1 && errno != ENOENT && errno != ELOOP)
1752 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1753 a919d5c4 2020-07-23 stsp else if (fd == -1 && errno == ELOOP) {
1754 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1755 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1756 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1757 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1758 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1759 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1761 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1762 3d35a492 2019-12-13 stsp goto done;
1764 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1765 1338848f 2019-12-13 stsp goto done;
1769 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1770 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1771 1338848f 2019-12-13 stsp goto done;
1774 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1775 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1776 1338848f 2019-12-13 stsp goto done;
1777 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1778 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1779 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1780 1338848f 2019-12-13 stsp goto done;
1783 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1784 f179e66d 2020-07-23 stsp goto done;
1786 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1787 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1788 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1789 1338848f 2019-12-13 stsp goto done;
1792 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1793 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1794 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1796 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1798 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1800 1338848f 2019-12-13 stsp goto done;
1802 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1803 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1804 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1805 a919d5c4 2020-07-23 stsp goto done;
1808 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1809 3d35a492 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1810 ab0d4361 2019-12-13 stsp if (fd == -1) {
1811 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1812 ab0d4361 2019-12-13 stsp goto done;
1816 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1817 6353ad76 2019-02-08 stsp if (f == NULL) {
1818 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1819 6353ad76 2019-02-08 stsp goto done;
1822 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1824 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1825 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1827 7154f6ce 2019-03-27 stsp goto done;
1828 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1829 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1830 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1831 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1832 7154f6ce 2019-03-27 stsp goto done;
1834 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1835 6353ad76 2019-02-08 stsp if (flen != 0)
1836 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1838 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1839 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1840 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1842 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1843 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1844 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1845 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1849 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1852 6353ad76 2019-02-08 stsp hdrlen = 0;
1855 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1856 7154f6ce 2019-03-27 stsp rewind(f);
1857 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1858 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1859 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1862 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1863 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1864 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1865 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1866 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1867 9d31a1d8 2018-03-11 stsp return err;
1871 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1872 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1874 e2b1e152 2019-07-13 stsp static const struct got_error *
1875 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1876 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1878 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1879 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1880 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1882 e2b1e152 2019-07-13 stsp return NULL;
1885 9d31a1d8 2018-03-11 stsp static const struct got_error *
1886 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1887 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1888 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1889 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1890 0584f854 2019-04-06 stsp void *progress_arg)
1892 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1893 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1894 6353ad76 2019-02-08 stsp char *ondisk_path;
1895 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1896 b8f41171 2019-02-10 stsp struct stat sb;
1898 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1899 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1902 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1903 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1904 a76c42e6 2019-08-03 stsp goto done;
1906 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1909 abb4604f 2019-07-27 stsp goto done;
1910 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1911 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1913 abb4604f 2019-07-27 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1914 c90c8ce3 2020-07-23 stsp status = GOT_STATUS_UNVERSIONED;
1917 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1918 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1919 b8f41171 2019-02-10 stsp goto done;
1921 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1922 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1924 5036ab18 2020-04-18 stsp goto done;
1927 523b8417 2019-10-19 stsp if (ie && status != GOT_STATUS_MISSING &&
1928 523b8417 2019-10-19 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1929 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1930 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1931 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1932 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1933 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1935 e2b1e152 2019-07-13 stsp goto done;
1936 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1938 6353ad76 2019-02-08 stsp goto done;
1940 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1941 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1942 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1943 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1944 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1945 b8f41171 2019-02-10 stsp goto done;
1949 56e0773d 2019-11-28 stsp err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1951 6353ad76 2019-02-08 stsp goto done;
1953 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1954 234035bc 2019-06-01 stsp int update_timestamps;
1955 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1956 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1957 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1958 234035bc 2019-06-01 stsp struct got_object_id id2;
1959 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1960 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1962 f69721c3 2019-10-21 stsp goto done;
1964 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
1965 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
1966 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1967 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
1968 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
1969 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
1970 f69721c3 2019-10-21 stsp goto done;
1972 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
1973 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
1974 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
1975 234035bc 2019-06-01 stsp goto done;
1978 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1979 36bf999c 2020-07-23 stsp char *link_target;
1980 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
1982 36bf999c 2020-07-23 stsp goto done;
1983 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
1984 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
1985 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
1986 36bf999c 2020-07-23 stsp free(link_target);
1988 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1989 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
1990 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
1991 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
1993 f69721c3 2019-10-21 stsp free(label_orig);
1994 234035bc 2019-06-01 stsp if (blob2)
1995 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1997 909d120e 2019-09-22 stsp goto done;
1999 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
2000 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2001 234035bc 2019-06-01 stsp * unmodified files again.
2003 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2004 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2005 234035bc 2019-06-01 stsp update_timestamps);
2006 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2007 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2008 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2009 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2010 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2012 1ee397ad 2019-07-12 stsp goto done;
2013 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2014 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2016 13d9040b 2019-03-27 stsp goto done;
2018 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2019 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2020 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2021 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2022 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2023 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2024 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2026 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2027 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2028 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2029 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2030 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2033 13d9040b 2019-03-27 stsp goto done;
2036 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2037 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2038 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2040 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2041 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2042 054041d0 2020-06-24 stsp &blob->id);
2045 13d9040b 2019-03-27 stsp goto done;
2047 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2048 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2049 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2052 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2054 6353ad76 2019-02-08 stsp free(ondisk_path);
2055 8da9e5f4 2019-01-12 stsp return err;
2058 90285c3b 2019-01-08 stsp static const struct got_error *
2059 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2061 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2062 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
2064 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2065 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2067 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2068 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2069 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2071 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2073 fddefe3b 2020-10-20 stsp char *parent;
2074 fddefe3b 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
2077 fddefe3b 2020-10-20 stsp free(ondisk_path);
2078 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2079 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2080 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2081 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2082 fddefe3b 2020-10-20 stsp ondisk_path);
2085 fddefe3b 2020-10-20 stsp } while (got_path_cmp(ondisk_path, root_path,
2086 fddefe3b 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
2088 90285c3b 2019-01-08 stsp free(ondisk_path);
2089 90285c3b 2019-01-08 stsp return err;
2092 708d8e67 2019-03-27 stsp static const struct got_error *
2093 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2094 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2095 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2097 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2098 708d8e67 2019-03-27 stsp unsigned char status;
2099 708d8e67 2019-03-27 stsp struct stat sb;
2100 708d8e67 2019-03-27 stsp char *ondisk_path;
2102 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2103 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2105 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2107 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2109 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2111 5a58a424 2020-06-23 stsp goto done;
2113 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2114 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2115 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2116 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2117 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2118 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2119 993e2a1b 2020-07-23 stsp goto done;
2121 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2122 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2123 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2124 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2126 993e2a1b 2020-07-23 stsp goto done;
2127 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2128 993e2a1b 2020-07-23 stsp ie->path);
2129 993e2a1b 2020-07-23 stsp goto done;
2132 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2133 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2134 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2136 5a58a424 2020-06-23 stsp goto done;
2138 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2139 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2141 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2142 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2144 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2146 5a58a424 2020-06-23 stsp goto done;
2147 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2148 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2150 5a58a424 2020-06-23 stsp goto done;
2152 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2155 5a58a424 2020-06-23 stsp free(ondisk_path);
2156 fc6346c4 2019-03-27 stsp return err;
2159 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2160 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2161 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2162 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2163 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2164 8da9e5f4 2019-01-12 stsp void *progress_arg;
2165 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2166 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2169 512f0d0e 2019-01-02 stsp static const struct got_error *
2170 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2171 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2173 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2175 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2176 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2178 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2179 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2182 8da9e5f4 2019-01-12 stsp static const struct got_error *
2183 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2185 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2187 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2188 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2190 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2191 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2194 9d31a1d8 2018-03-11 stsp static const struct got_error *
2195 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2197 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2198 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2199 8da9e5f4 2019-01-12 stsp char *path;
2201 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2202 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2204 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2205 63c5ca5d 2019-08-24 stsp return NULL;
2207 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2208 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2210 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2212 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2213 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2215 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2216 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2218 8da9e5f4 2019-01-12 stsp free(path);
2219 0cd1c46a 2019-03-11 stsp return err;
2222 b2118c49 2020-07-28 stsp const struct got_error *
2223 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2225 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2227 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2228 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2229 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2230 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2233 b2118c49 2020-07-28 stsp return NULL;
2236 818c7501 2019-07-11 stsp static const struct got_error *
2237 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2239 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2240 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2242 517bab73 2019-03-11 stsp *refname = NULL;
2244 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2246 b2118c49 2020-07-28 stsp return err;
2248 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2249 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2250 0647c563 2019-03-11 stsp *refname = NULL;
2252 517bab73 2019-03-11 stsp free(uuidstr);
2253 517bab73 2019-03-11 stsp return err;
2256 818c7501 2019-07-11 stsp const struct got_error *
2257 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2259 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2262 818c7501 2019-07-11 stsp static const struct got_error *
2263 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2265 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2266 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2269 818c7501 2019-07-11 stsp static const struct got_error *
2270 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2272 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2275 818c7501 2019-07-11 stsp static const struct got_error *
2276 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2278 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2279 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2282 818c7501 2019-07-11 stsp static const struct got_error *
2283 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2285 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2286 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2289 0ebf8283 2019-07-24 stsp static const struct got_error *
2290 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2292 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2293 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2296 0ebf8283 2019-07-24 stsp static const struct got_error *
2297 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2299 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2300 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2303 0ebf8283 2019-07-24 stsp static const struct got_error *
2304 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2306 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2307 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2310 0ebf8283 2019-07-24 stsp static const struct got_error *
2311 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2313 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2314 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2317 0ebf8283 2019-07-24 stsp const struct got_error *
2318 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2319 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2321 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2322 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2323 0ebf8283 2019-07-24 stsp *path = NULL;
2324 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2326 0ebf8283 2019-07-24 stsp return NULL;
2330 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2331 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2333 517bab73 2019-03-11 stsp static const struct got_error *
2334 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2336 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2337 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2338 517bab73 2019-03-11 stsp char *refname;
2340 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2342 517bab73 2019-03-11 stsp return err;
2344 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2346 0cd1c46a 2019-03-11 stsp goto done;
2348 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2350 0cd1c46a 2019-03-11 stsp free(refname);
2352 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2353 3e3a69f1 2019-07-25 stsp return err;
2356 3e3a69f1 2019-07-25 stsp static const struct got_error *
2357 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2359 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2361 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2362 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2363 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2364 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2366 9d31a1d8 2018-03-11 stsp return err;
2370 ebf99748 2019-05-09 stsp static const struct got_error *
2371 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2372 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2374 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2375 ebf99748 2019-05-09 stsp FILE *index = NULL;
2377 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2378 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2379 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2380 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2382 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2384 ebf99748 2019-05-09 stsp goto done;
2386 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
2387 ebf99748 2019-05-09 stsp if (index == NULL) {
2388 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2389 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2391 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2392 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2393 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2396 ebf99748 2019-05-09 stsp if (err) {
2397 ebf99748 2019-05-09 stsp free(*fileindex_path);
2398 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2399 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2400 ebf99748 2019-05-09 stsp *fileindex = NULL;
2402 ebf99748 2019-05-09 stsp return err;
2405 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2406 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2407 c932eeeb 2019-05-22 stsp const char *path;
2408 c932eeeb 2019-05-22 stsp size_t path_len;
2409 c932eeeb 2019-05-22 stsp const char *entry_name;
2410 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2411 a484d721 2019-06-10 stsp void *progress_arg;
2414 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2415 c932eeeb 2019-05-22 stsp static const struct got_error *
2416 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2418 1ee397ad 2019-07-12 stsp const struct got_error *err;
2419 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2421 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2422 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2423 c932eeeb 2019-05-22 stsp return NULL;
2424 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2425 102ff934 2019-06-11 stsp return NULL;
2427 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2428 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2429 c932eeeb 2019-05-22 stsp return NULL;
2431 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2432 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2433 edd02c5e 2019-07-11 stsp ie->path);
2435 1ee397ad 2019-07-12 stsp return err;
2437 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2438 c932eeeb 2019-05-22 stsp return NULL;
2441 a615e0e7 2020-12-16 stsp static const struct got_error *
2442 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2443 a615e0e7 2020-12-16 stsp struct got_fileindex *fileindex,
2444 a615e0e7 2020-12-16 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2446 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2448 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2449 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2450 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2451 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2452 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2453 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2455 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2456 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2459 9c6338c4 2019-06-02 stsp static const struct got_error *
2460 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2462 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2463 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2464 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2465 867630bb 2020-01-17 stsp struct timespec timeout;
2467 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2468 9c6338c4 2019-06-02 stsp fileindex_path);
2470 9c6338c4 2019-06-02 stsp goto done;
2472 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2474 9c6338c4 2019-06-02 stsp goto done;
2476 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2477 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2478 9c6338c4 2019-06-02 stsp fileindex_path);
2479 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2483 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2484 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2485 867630bb 2020-01-17 stsp * was recorded in the file index.
2487 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2488 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2489 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2491 9c6338c4 2019-06-02 stsp if (new_index)
2492 9c6338c4 2019-06-02 stsp fclose(new_index);
2493 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2494 9c6338c4 2019-06-02 stsp return err;
2497 6ced4176 2019-07-12 stsp static const struct got_error *
2498 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2499 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2500 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
2502 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2503 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2504 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2505 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2507 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2508 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2509 6ced4176 2019-07-12 stsp *tree_id = NULL;
2511 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2512 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2513 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2514 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2515 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2516 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2517 6ced4176 2019-07-12 stsp goto done;
2519 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2520 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
2522 6ced4176 2019-07-12 stsp goto done;
2523 6ced4176 2019-07-12 stsp return NULL;
2526 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2528 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2529 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2530 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2531 6ced4176 2019-07-12 stsp goto done;
2534 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2535 6ced4176 2019-07-12 stsp in_repo_path);
2537 6ced4176 2019-07-12 stsp goto done;
2539 6ced4176 2019-07-12 stsp free(in_repo_path);
2540 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2542 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2544 6ced4176 2019-07-12 stsp goto done;
2546 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2547 6ced4176 2019-07-12 stsp /* Check out a single file. */
2548 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2549 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2550 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2551 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2552 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2553 6ced4176 2019-07-12 stsp goto done;
2555 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2556 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2557 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2558 6ced4176 2019-07-12 stsp goto done;
2561 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2562 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2564 6ced4176 2019-07-12 stsp return err;
2565 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2566 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2567 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2568 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2569 6ced4176 2019-07-12 stsp goto done;
2572 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2573 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
2575 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2576 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2577 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2578 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2579 6ced4176 2019-07-12 stsp goto done;
2581 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2582 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2583 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2584 6ced4176 2019-07-12 stsp goto done;
2589 6ced4176 2019-07-12 stsp free(in_repo_path);
2590 6ced4176 2019-07-12 stsp if (err) {
2591 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2592 6ced4176 2019-07-12 stsp free(*tree_relpath);
2593 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2594 6ced4176 2019-07-12 stsp free(*tree_id);
2595 6ced4176 2019-07-12 stsp *tree_id = NULL;
2597 07ed472d 2019-07-12 stsp return err;
2600 07ed472d 2019-07-12 stsp static const struct got_error *
2601 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2602 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2603 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2604 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2606 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2607 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2608 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2609 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2610 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2612 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2613 7f47418f 2019-12-20 stsp if (err) {
2614 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2615 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2616 7f47418f 2019-12-20 stsp goto done;
2617 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2618 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2620 7f47418f 2019-12-20 stsp return err;
2623 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2624 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2626 07ed472d 2019-07-12 stsp goto done;
2628 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2630 07ed472d 2019-07-12 stsp goto done;
2632 07ed472d 2019-07-12 stsp if (entry_name &&
2633 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2634 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2635 07ed472d 2019-07-12 stsp goto done;
2638 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2639 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2640 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2641 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2642 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2643 07ed472d 2019-07-12 stsp arg.repo = repo;
2644 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2645 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2646 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2647 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2648 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2649 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2652 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2653 07ed472d 2019-07-12 stsp if (commit)
2654 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2655 6ced4176 2019-07-12 stsp return err;
2658 9d31a1d8 2018-03-11 stsp const struct got_error *
2659 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2660 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2661 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2662 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2664 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2665 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2666 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2667 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2668 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2669 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2670 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2671 f2ea84fa 2019-07-27 stsp SIMPLEQ_ENTRY(tree_path_data) entry;
2672 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2673 f2ea84fa 2019-07-27 stsp int entry_type;
2674 f2ea84fa 2019-07-27 stsp char *relpath;
2675 f2ea84fa 2019-07-27 stsp char *entry_name;
2676 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2677 f2ea84fa 2019-07-27 stsp SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2679 f2ea84fa 2019-07-27 stsp SIMPLEQ_INIT(&tree_paths);
2681 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2683 9d31a1d8 2018-03-11 stsp return err;
2685 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2686 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2687 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2688 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2689 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2690 f2ea84fa 2019-07-27 stsp goto done;
2693 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2694 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2695 f2ea84fa 2019-07-27 stsp if (err) {
2696 f2ea84fa 2019-07-27 stsp free(tpd);
2697 6ced4176 2019-07-12 stsp goto done;
2700 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2701 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2702 f2ea84fa 2019-07-27 stsp if (err) {
2703 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2704 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2705 f2ea84fa 2019-07-27 stsp free(tpd);
2706 f2ea84fa 2019-07-27 stsp goto done;
2709 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2711 f2ea84fa 2019-07-27 stsp SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2715 51514078 2018-12-25 stsp * Read the file index.
2716 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2717 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2719 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2721 c4cdcb68 2019-04-03 stsp goto done;
2723 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2724 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2725 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2727 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2728 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2729 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2733 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2734 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2735 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2736 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2737 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2738 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2739 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2740 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2744 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_NEXT(tpd, entry);
2746 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2747 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2748 af12c6b9 2019-06-04 stsp err = sync_err;
2750 9c6338c4 2019-06-02 stsp free(fileindex_path);
2752 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2753 9d31a1d8 2018-03-11 stsp if (commit)
2754 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2755 5ade8233 2019-07-12 stsp if (fileindex)
2756 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2757 f2ea84fa 2019-07-27 stsp while (!SIMPLEQ_EMPTY(&tree_paths)) {
2758 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2759 f2ea84fa 2019-07-27 stsp SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2760 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2761 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2762 f2ea84fa 2019-07-27 stsp free(tpd);
2764 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2765 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2766 9d31a1d8 2018-03-11 stsp err = unlockerr;
2767 f8d1f275 2019-02-04 stsp return err;
2770 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2771 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2772 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2773 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2774 234035bc 2019-06-01 stsp void *progress_arg;
2775 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2776 234035bc 2019-06-01 stsp void *cancel_arg;
2777 f69721c3 2019-10-21 stsp const char *label_orig;
2778 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2781 234035bc 2019-06-01 stsp static const struct got_error *
2782 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2783 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
2784 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
2785 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2787 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2788 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2789 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2790 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2791 234035bc 2019-06-01 stsp struct stat sb;
2792 234035bc 2019-06-01 stsp unsigned char status;
2793 234035bc 2019-06-01 stsp int local_changes_subsumed;
2795 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2796 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2797 d6c87207 2019-08-02 stsp strlen(path2));
2798 1ee397ad 2019-07-12 stsp if (ie == NULL)
2799 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2800 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2802 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2803 234035bc 2019-06-01 stsp path2) == -1)
2804 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2806 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2809 234035bc 2019-06-01 stsp goto done;
2811 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2812 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2813 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2814 234035bc 2019-06-01 stsp goto done;
2816 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2817 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2818 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2819 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2820 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2821 234035bc 2019-06-01 stsp goto done;
2824 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2825 36bf999c 2020-07-23 stsp char *link_target2;
2826 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2828 36bf999c 2020-07-23 stsp goto done;
2829 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2830 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2831 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2832 36bf999c 2020-07-23 stsp free(link_target2);
2834 af57b12a 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
2835 af57b12a 2020-07-23 stsp blob1, ondisk_path, path2, sb.st_mode,
2836 af57b12a 2020-07-23 stsp a->label_orig, blob2, a->commit_id2, repo,
2837 af57b12a 2020-07-23 stsp a->progress_cb, a->progress_arg);
2839 234035bc 2019-06-01 stsp } else if (blob1) {
2840 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2841 d6c87207 2019-08-02 stsp strlen(path1));
2842 1ee397ad 2019-07-12 stsp if (ie == NULL)
2843 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2844 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2846 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2847 2b92fad7 2019-06-02 stsp path1) == -1)
2848 2b92fad7 2019-06-02 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 2b92fad7 2019-06-02 stsp goto done;
2855 2b92fad7 2019-06-02 stsp switch (status) {
2856 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2857 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2858 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2860 1ee397ad 2019-07-12 stsp goto done;
2861 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2863 2b92fad7 2019-06-02 stsp goto done;
2865 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2867 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2868 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2869 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2870 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2872 1ee397ad 2019-07-12 stsp goto done;
2874 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2876 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
2877 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
2878 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
2880 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
2881 0a22ca1a 2020-09-23 stsp * exists in the repository.
2883 0a22ca1a 2020-09-23 stsp err = got_object_blob_file_create(&id, &blob1_f, path1);
2885 0a22ca1a 2020-09-23 stsp goto done;
2886 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
2887 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2888 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
2890 0a22ca1a 2020-09-23 stsp goto done;
2891 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
2894 0a22ca1a 2020-09-23 stsp goto done;
2896 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
2899 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2900 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
2902 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
2903 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
2906 0a22ca1a 2020-09-23 stsp goto done;
2909 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2910 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2911 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2912 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2914 1ee397ad 2019-07-12 stsp goto done;
2916 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2917 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2919 1ee397ad 2019-07-12 stsp goto done;
2924 234035bc 2019-06-01 stsp } else if (blob2) {
2925 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2926 234035bc 2019-06-01 stsp path2) == -1)
2927 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2928 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2929 d6c87207 2019-08-02 stsp strlen(path2));
2931 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2932 7f91a133 2019-12-13 stsp -1, NULL, repo);
2934 234035bc 2019-06-01 stsp goto done;
2935 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2936 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2937 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2938 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2939 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2940 1ee397ad 2019-07-12 stsp status, path2);
2941 234035bc 2019-06-01 stsp goto done;
2943 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2944 36bf999c 2020-07-23 stsp char *link_target2;
2945 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
2948 36bf999c 2020-07-23 stsp goto done;
2949 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
2950 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
2951 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
2952 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
2953 36bf999c 2020-07-23 stsp free(link_target2);
2954 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
2955 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
2956 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
2957 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
2958 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
2959 526a746f 2020-07-23 stsp a->progress_arg);
2961 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
2962 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
2965 dfe9fba0 2020-07-23 stsp goto done;
2966 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2967 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
2968 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, blob2->id.sha1,
2969 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
2971 234035bc 2019-06-01 stsp goto done;
2974 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2975 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2976 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
2977 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
2978 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
2979 c90c8ce3 2020-07-23 stsp 0, 1, repo, a->progress_cb, a->progress_arg);
2981 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
2982 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2983 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
2986 234035bc 2019-06-01 stsp goto done;
2987 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
2989 234035bc 2019-06-01 stsp goto done;
2990 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2991 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, NULL, NULL, 1);
2992 3969253a 2020-03-07 stsp if (err) {
2993 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
2994 3969253a 2020-03-07 stsp goto done;
2996 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
2997 2b92fad7 2019-06-02 stsp if (err) {
2998 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
2999 2b92fad7 2019-06-02 stsp goto done;
3001 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
3002 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
3003 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
3008 234035bc 2019-06-01 stsp free(ondisk_path);
3009 234035bc 2019-06-01 stsp return err;
3012 234035bc 2019-06-01 stsp struct check_merge_ok_arg {
3013 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3014 234035bc 2019-06-01 stsp struct got_repository *repo;
3017 234035bc 2019-06-01 stsp static const struct got_error *
3018 234035bc 2019-06-01 stsp check_merge_ok(void *arg, struct got_fileindex_entry *ie)
3020 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3021 234035bc 2019-06-01 stsp struct check_merge_ok_arg *a = arg;
3022 234035bc 2019-06-01 stsp unsigned char status;
3023 234035bc 2019-06-01 stsp struct stat sb;
3024 234035bc 2019-06-01 stsp char *ondisk_path;
3026 234035bc 2019-06-01 stsp /* Reject merges into a work tree with mixed base commits. */
3027 234035bc 2019-06-01 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3028 234035bc 2019-06-01 stsp SHA1_DIGEST_LENGTH))
3029 234035bc 2019-06-01 stsp return got_error(GOT_ERR_MIXED_COMMITS);
3031 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3033 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3035 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3036 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3038 234035bc 2019-06-01 stsp return err;
3039 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3040 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3042 234035bc 2019-06-01 stsp return NULL;
3045 818c7501 2019-07-11 stsp static const struct got_error *
3046 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3047 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3048 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3049 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3050 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3052 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3053 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3054 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3055 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3056 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3058 03415a1a 2019-06-02 stsp if (commit_id1) {
3059 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3060 03415a1a 2019-06-02 stsp worktree->path_prefix);
3061 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3062 03415a1a 2019-06-02 stsp goto done;
3064 69d57f3d 2020-07-31 stsp if (tree_id1) {
3065 69d57f3d 2020-07-31 stsp char *id_str;
3067 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3069 03415a1a 2019-06-02 stsp goto done;
3071 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3073 f69721c3 2019-10-21 stsp goto done;
3075 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3076 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3077 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3078 f69721c3 2019-10-21 stsp free(id_str);
3079 f69721c3 2019-10-21 stsp goto done;
3081 f69721c3 2019-10-21 stsp free(id_str);
3084 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3085 234035bc 2019-06-01 stsp worktree->path_prefix);
3087 234035bc 2019-06-01 stsp goto done;
3089 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3091 234035bc 2019-06-01 stsp goto done;
3093 234035bc 2019-06-01 stsp arg.worktree = worktree;
3094 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3095 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3096 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3097 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3098 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3099 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3100 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3101 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3102 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3103 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3104 af12c6b9 2019-06-04 stsp err = sync_err;
3106 234035bc 2019-06-01 stsp if (tree1)
3107 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3108 234035bc 2019-06-01 stsp if (tree2)
3109 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3110 f69721c3 2019-10-21 stsp free(label_orig);
3111 818c7501 2019-07-11 stsp return err;
3114 818c7501 2019-07-11 stsp const struct got_error *
3115 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3116 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3117 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3118 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3120 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3121 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3122 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3123 818c7501 2019-07-11 stsp struct check_merge_ok_arg mok_arg;
3125 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3127 818c7501 2019-07-11 stsp return err;
3129 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3131 818c7501 2019-07-11 stsp goto done;
3133 818c7501 2019-07-11 stsp mok_arg.worktree = worktree;
3134 818c7501 2019-07-11 stsp mok_arg.repo = repo;
3135 818c7501 2019-07-11 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3136 818c7501 2019-07-11 stsp &mok_arg);
3138 818c7501 2019-07-11 stsp goto done;
3140 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3141 818c7501 2019-07-11 stsp commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3143 818c7501 2019-07-11 stsp if (fileindex)
3144 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3145 818c7501 2019-07-11 stsp free(fileindex_path);
3146 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3147 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3148 234035bc 2019-06-01 stsp err = unlockerr;
3149 234035bc 2019-06-01 stsp return err;
3152 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3153 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3154 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3155 927df6b7 2019-02-10 stsp const char *status_path;
3156 927df6b7 2019-02-10 stsp size_t status_path_len;
3157 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3158 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3159 f8d1f275 2019-02-04 stsp void *status_arg;
3160 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3161 f8d1f275 2019-02-04 stsp void *cancel_arg;
3162 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3163 6841da00 2019-08-08 stsp struct got_pathlist_head ignores;
3164 f2a9dc41 2019-12-13 tracey int report_unchanged;
3165 3143d852 2020-06-25 stsp int no_ignores;
3168 f8d1f275 2019-02-04 stsp static const struct got_error *
3169 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3170 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3171 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3172 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3174 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3175 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3176 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3177 927df6b7 2019-02-10 stsp struct stat sb;
3178 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3179 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3180 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3182 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3184 98eaaa12 2019-08-03 stsp return err;
3186 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3187 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3188 98eaaa12 2019-08-03 stsp return NULL;
3190 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3191 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3192 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3194 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3195 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3196 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3198 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3199 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3200 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3201 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3202 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3205 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3206 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3209 927df6b7 2019-02-10 stsp static const struct got_error *
3210 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3211 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3213 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3214 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3215 f8d1f275 2019-02-04 stsp char *abspath;
3217 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3218 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3220 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3221 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3222 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3223 927df6b7 2019-02-10 stsp return NULL;
3225 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3226 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3227 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3228 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3230 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3231 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3232 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3235 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3236 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3237 f8d1f275 2019-02-04 stsp free(abspath);
3238 9d31a1d8 2018-03-11 stsp return err;
3241 f8d1f275 2019-02-04 stsp static const struct got_error *
3242 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3244 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3245 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3246 2ec1f75b 2019-03-26 stsp unsigned char status;
3248 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3249 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3251 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3252 927df6b7 2019-02-10 stsp return NULL;
3254 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3255 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3256 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3257 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3259 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3260 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3261 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3265 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3267 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3269 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3270 6841da00 2019-08-08 stsp free((char *)pe->path);
3271 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3275 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3277 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3279 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3280 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3281 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3282 6841da00 2019-08-08 stsp free((char *)pe->path);
3284 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3287 6841da00 2019-08-08 stsp static const struct got_error *
3288 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3290 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3291 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3292 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3293 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3294 6841da00 2019-08-08 stsp size_t linesize = 0;
3295 6841da00 2019-08-08 stsp ssize_t linelen;
3297 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3298 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3299 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3300 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3302 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3303 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3304 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3306 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3307 bd8de430 2019-10-04 stsp if (line[0] == '#')
3310 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3311 bd8de430 2019-10-04 stsp if (line[0] == '!')
3314 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3315 6841da00 2019-08-08 stsp line) == -1) {
3316 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3317 6841da00 2019-08-08 stsp goto done;
3319 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3321 6841da00 2019-08-08 stsp goto done;
3323 6841da00 2019-08-08 stsp if (ferror(f)) {
3324 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3325 6841da00 2019-08-08 stsp goto done;
3328 6841da00 2019-08-08 stsp dirpath = strdup(path);
3329 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3330 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3331 6841da00 2019-08-08 stsp goto done;
3333 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3335 6841da00 2019-08-08 stsp free(line);
3336 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3337 6841da00 2019-08-08 stsp free(dirpath);
3338 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3340 6841da00 2019-08-08 stsp return err;
3344 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3346 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3348 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3349 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3350 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3351 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3353 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3354 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3356 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3358 bd8de430 2019-10-04 stsp pattern += 3;
3360 bd8de430 2019-10-04 stsp while (*p) {
3361 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3362 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3363 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3364 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3366 bd8de430 2019-10-04 stsp while (*p == '/')
3376 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3377 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3378 6841da00 2019-08-08 stsp * ignores backwards.
3380 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3381 6841da00 2019-08-08 stsp while (pe) {
3382 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3383 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3384 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3385 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3386 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3387 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3388 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3389 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3390 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3395 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3401 6841da00 2019-08-08 stsp static const struct got_error *
3402 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3403 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3405 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3406 6841da00 2019-08-08 stsp char *ignorespath;
3407 886cec17 2019-12-15 stsp int fd = -1;
3408 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3410 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3411 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3412 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3414 886cec17 2019-12-15 stsp if (dirfd != -1) {
3415 886cec17 2019-12-15 stsp fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3416 886cec17 2019-12-15 stsp if (fd == -1) {
3417 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3418 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3419 886cec17 2019-12-15 stsp ignorespath);
3421 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3422 886cec17 2019-12-15 stsp if (ignoresfile == NULL)
3423 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3424 886cec17 2019-12-15 stsp ignorespath);
3427 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3431 886cec17 2019-12-15 stsp ignoresfile = fopen(ignorespath, "r");
3432 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3433 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3434 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3435 886cec17 2019-12-15 stsp ignorespath);
3437 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3440 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3441 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3442 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3443 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3444 6841da00 2019-08-08 stsp free(ignorespath);
3445 6841da00 2019-08-08 stsp return err;
3448 f8d1f275 2019-02-04 stsp static const struct got_error *
3449 7f91a133 2019-12-13 stsp status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3451 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3452 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3453 f8d1f275 2019-02-04 stsp char *path = NULL;
3455 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3456 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3458 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3459 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3460 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3462 f8d1f275 2019-02-04 stsp path = de->d_name;
3465 3143d852 2020-06-25 stsp if (de->d_type != DT_DIR &&
3466 3143d852 2020-06-25 stsp got_path_is_child(path, a->status_path, a->status_path_len)
3467 6841da00 2019-08-08 stsp && !match_ignores(&a->ignores, path))
3468 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3469 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3470 f8d1f275 2019-02-04 stsp if (parent_path[0])
3471 f8d1f275 2019-02-04 stsp free(path);
3472 b72f483a 2019-02-05 stsp return err;
3475 347d1d3e 2019-07-12 stsp static const struct got_error *
3476 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3478 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3479 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3481 3143d852 2020-06-25 stsp if (a->no_ignores)
3482 3143d852 2020-06-25 stsp return NULL;
3484 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path,
3485 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3487 3143d852 2020-06-25 stsp return err;
3489 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path, path,
3490 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3492 3143d852 2020-06-25 stsp return err;
3495 3143d852 2020-06-25 stsp static const struct got_error *
3496 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3497 abb4604f 2019-07-27 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3498 f2a9dc41 2019-12-13 tracey void *status_arg, struct got_repository *repo, int report_unchanged)
3500 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3501 abb4604f 2019-07-27 stsp struct stat sb;
3503 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3505 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3506 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3508 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3509 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3510 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3511 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3512 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3513 abb4604f 2019-07-27 stsp return NULL;
3516 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3517 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3518 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3520 abb4604f 2019-07-27 stsp return NULL;
3523 3143d852 2020-06-25 stsp static const struct got_error *
3524 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3525 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3527 3143d852 2020-06-25 stsp const struct got_error *err;
3528 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3530 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3531 3143d852 2020-06-25 stsp ".cvsignore");
3533 3143d852 2020-06-25 stsp return err;
3535 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3536 3143d852 2020-06-25 stsp ".gitignore");
3538 3143d852 2020-06-25 stsp return err;
3540 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3541 3143d852 2020-06-25 stsp if (err) {
3542 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3543 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3544 3143d852 2020-06-25 stsp return err;
3546 3143d852 2020-06-25 stsp for (;;) {
3547 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3548 3143d852 2020-06-25 stsp ".cvsignore");
3551 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3552 3143d852 2020-06-25 stsp ".gitignore");
3555 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3556 3143d852 2020-06-25 stsp if (err) {
3557 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3558 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3561 b737c85a 2020-06-26 stsp free(parent_path);
3562 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3563 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3566 b737c85a 2020-06-26 stsp free(parent_path);
3567 b737c85a 2020-06-26 stsp free(next_parent_path);
3568 3143d852 2020-06-25 stsp return err;
3571 abb4604f 2019-07-27 stsp static const struct got_error *
3572 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3573 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3574 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3575 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3576 f2a9dc41 2019-12-13 tracey int report_unchanged)
3578 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3579 6fc93f37 2019-12-13 stsp int fd = -1;
3580 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3581 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3582 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3584 3143d852 2020-06-25 stsp TAILQ_INIT(&arg.ignores);
3586 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3587 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3588 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3590 6fc93f37 2019-12-13 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3591 6fc93f37 2019-12-13 stsp if (fd == -1) {
3592 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3593 00bb5ea0 2020-07-23 stsp errno != ELOOP)
3594 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3596 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3597 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3598 f2a9dc41 2019-12-13 tracey report_unchanged);
3600 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3601 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3602 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3603 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3604 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3605 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3606 abb4604f 2019-07-27 stsp arg.status_path = path;
3607 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3608 abb4604f 2019-07-27 stsp arg.repo = repo;
3609 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3610 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3611 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3612 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3613 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3614 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3615 022fae89 2019-12-06 tracey if (!no_ignores) {
3616 3143d852 2020-06-25 stsp err = add_ignores_from_parent_paths(&arg.ignores,
3617 3143d852 2020-06-25 stsp worktree->root_path, path);
3619 3143d852 2020-06-25 stsp goto done;
3621 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3622 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3625 3143d852 2020-06-25 stsp free_ignores(&arg.ignores);
3626 6fc93f37 2019-12-13 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
3627 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3628 927df6b7 2019-02-10 stsp free(ondisk_path);
3629 347d1d3e 2019-07-12 stsp return err;
3632 347d1d3e 2019-07-12 stsp const struct got_error *
3633 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3634 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3635 72ea6654 2019-07-27 stsp got_worktree_status_cb status_cb, void *status_arg,
3636 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3638 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3639 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3640 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3641 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3643 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3645 347d1d3e 2019-07-12 stsp return err;
3647 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3648 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3649 f2a9dc41 2019-12-13 tracey status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3653 f8d1f275 2019-02-04 stsp free(fileindex_path);
3654 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3655 f8d1f275 2019-02-04 stsp return err;
3658 6c7ab921 2019-03-18 stsp const struct got_error *
3659 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3660 6c7ab921 2019-03-18 stsp const char *arg)
3662 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3663 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3664 6c7ab921 2019-03-18 stsp size_t len;
3665 00bb5ea0 2020-07-23 stsp struct stat sb;
3666 01740607 2020-11-04 stsp char *abspath = NULL;
3667 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3669 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3671 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3672 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3673 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3675 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3676 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3677 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3678 00bb5ea0 2020-07-23 stsp goto done;
3680 727173c3 2020-11-06 stsp sb.st_mode = 0;
3682 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3684 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3685 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3686 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3687 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3689 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3690 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3691 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3692 00bb5ea0 2020-07-23 stsp goto done;
3696 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3697 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3699 d0710d08 2019-07-22 stsp goto done;
3700 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3701 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3702 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3703 00bb5ea0 2020-07-23 stsp goto done;
3706 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3707 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3708 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3709 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3710 00bb5ea0 2020-07-23 stsp goto done;
3712 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3713 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3714 01740607 2020-11-04 stsp goto done;
3716 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
3717 01740607 2020-11-04 stsp sizeof(canonpath));
3719 00bb5ea0 2020-07-23 stsp goto done;
3720 01740607 2020-11-04 stsp resolved = strdup(canonpath);
3721 01740607 2020-11-04 stsp if (resolved == NULL) {
3722 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
3723 01740607 2020-11-04 stsp goto done;
3728 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3729 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3730 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3731 6c7ab921 2019-03-18 stsp goto done;
3734 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3735 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3736 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3738 f6d88e1a 2019-05-29 stsp goto done;
3740 f6d88e1a 2019-05-29 stsp path = strdup("");
3741 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3742 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3743 f6d88e1a 2019-05-29 stsp goto done;
3747 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3748 6c7ab921 2019-03-18 stsp len = strlen(path);
3749 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3750 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3754 01740607 2020-11-04 stsp free(abspath);
3755 6c7ab921 2019-03-18 stsp free(resolved);
3756 d0710d08 2019-07-22 stsp free(cwd);
3757 6c7ab921 2019-03-18 stsp if (err == NULL)
3758 6c7ab921 2019-03-18 stsp *wt_path = path;
3760 6c7ab921 2019-03-18 stsp free(path);
3761 d00136be 2019-03-26 stsp return err;
3764 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3765 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3766 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3767 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3768 4e68cba3 2019-11-23 stsp void *progress_arg;
3769 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3772 4e68cba3 2019-11-23 stsp static const struct got_error *
3773 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3774 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3775 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3776 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3778 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3779 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3780 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3781 6d022e97 2019-08-04 stsp struct stat sb;
3782 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3784 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3785 dbb83fbd 2019-12-12 stsp relpath) == -1)
3786 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3788 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3790 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3791 12463d8b 2019-12-13 stsp de_name, a->repo);
3793 dbb83fbd 2019-12-12 stsp goto done;
3794 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3795 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3796 dbb83fbd 2019-12-12 stsp goto done;
3797 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3799 dbb83fbd 2019-12-12 stsp goto done;
3802 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3803 dbb83fbd 2019-12-12 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3804 dbb83fbd 2019-12-12 stsp goto done;
3807 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3809 4e68cba3 2019-11-23 stsp goto done;
3810 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3811 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
3812 3969253a 2020-03-07 stsp if (err) {
3813 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3814 3969253a 2020-03-07 stsp goto done;
3816 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3817 07f5b47a 2019-06-02 stsp if (err) {
3818 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
3819 4e68cba3 2019-11-23 stsp goto done;
3822 dbb83fbd 2019-12-12 stsp free(ondisk_path);
3824 dbb83fbd 2019-12-12 stsp return err;
3825 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
3826 dbb83fbd 2019-12-12 stsp return NULL;
3827 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3830 d00136be 2019-03-26 stsp const struct got_error *
3831 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
3832 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
3833 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3834 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
3836 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3837 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
3838 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3839 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
3840 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
3842 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3844 d00136be 2019-03-26 stsp return err;
3846 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3848 d00136be 2019-03-26 stsp goto done;
3850 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
3851 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
3852 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
3853 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
3854 4e68cba3 2019-11-23 stsp saa.repo = repo;
3856 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3857 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3858 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3862 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3863 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3864 af12c6b9 2019-06-04 stsp err = sync_err;
3866 fb399478 2019-07-12 stsp free(fileindex_path);
3867 d00136be 2019-03-26 stsp if (fileindex)
3868 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
3869 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3870 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
3871 d00136be 2019-03-26 stsp err = unlockerr;
3872 6c7ab921 2019-03-18 stsp return err;
3875 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
3876 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
3877 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
3878 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
3879 f2a9dc41 2019-12-13 tracey void *progress_arg;
3880 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
3881 f2a9dc41 2019-12-13 tracey int delete_local_mods;
3882 70e3e7f5 2019-12-13 tracey int keep_on_disk;
3883 766841c2 2020-08-13 stsp const char *status_codes;
3886 f2a9dc41 2019-12-13 tracey static const struct got_error *
3887 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
3888 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
3889 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3890 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
3892 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
3893 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
3894 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
3895 17ed4618 2019-06-02 stsp struct stat sb;
3896 20a2ad1c 2020-10-20 stsp char *ondisk_path;
3898 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3899 17ed4618 2019-06-02 stsp if (ie == NULL)
3900 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
3902 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
3903 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
3904 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3905 9acbc4fa 2019-08-03 stsp return NULL;
3906 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3909 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3910 f2a9dc41 2019-12-13 tracey relpath) == -1)
3911 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
3913 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3916 f2a9dc41 2019-12-13 tracey goto done;
3918 766841c2 2020-08-13 stsp if (a->status_codes) {
3919 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
3921 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
3922 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
3925 766841c2 2020-08-13 stsp if (i == ncodes) {
3926 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
3927 766841c2 2020-08-13 stsp free(ondisk_path);
3928 766841c2 2020-08-13 stsp return NULL;
3930 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3931 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
3932 766841c2 2020-08-13 stsp static char msg[64];
3933 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
3934 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
3935 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3936 766841c2 2020-08-13 stsp goto done;
3940 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
3941 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
3942 f2a9dc41 2019-12-13 tracey goto done;
3943 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3944 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3945 f2a9dc41 2019-12-13 tracey goto done;
3947 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
3948 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
3949 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3950 f2a9dc41 2019-12-13 tracey goto done;
3954 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3955 20a2ad1c 2020-10-20 stsp size_t root_len;
3957 12463d8b 2019-12-13 stsp if (dirfd != -1) {
3958 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
3959 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
3960 12463d8b 2019-12-13 stsp ondisk_path);
3961 12463d8b 2019-12-13 stsp goto done;
3963 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
3964 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
3965 12463d8b 2019-12-13 stsp goto done;
3968 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
3970 20a2ad1c 2020-10-20 stsp char *parent;
3971 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
3973 2513f20a 2020-10-20 stsp goto done;
3974 20a2ad1c 2020-10-20 stsp free(ondisk_path);
3975 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
3976 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
3977 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
3978 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
3979 20a2ad1c 2020-10-20 stsp ondisk_path);
3982 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
3983 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
3986 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3988 f2a9dc41 2019-12-13 tracey free(ondisk_path);
3989 f2a9dc41 2019-12-13 tracey if (err)
3990 f2a9dc41 2019-12-13 tracey return err;
3991 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
3992 f2a9dc41 2019-12-13 tracey return NULL;
3993 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3994 f2a9dc41 2019-12-13 tracey staged_status, relpath);
3997 2ec1f75b 2019-03-26 stsp const struct got_error *
3998 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
3999 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4000 766841c2 2020-08-13 stsp const char *status_codes,
4001 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4002 70e3e7f5 2019-12-13 tracey struct got_repository *repo, int keep_on_disk)
4004 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4005 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4006 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4007 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4008 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4010 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4012 2ec1f75b 2019-03-26 stsp return err;
4014 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4016 2ec1f75b 2019-03-26 stsp goto done;
4018 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4019 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4020 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4021 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4022 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4023 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4024 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4025 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4027 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4028 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4029 f2a9dc41 2019-12-13 tracey schedule_for_deletion, &sda, NULL, NULL, 0, 1);
4033 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4034 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4035 af12c6b9 2019-06-04 stsp err = sync_err;
4037 fb399478 2019-07-12 stsp free(fileindex_path);
4038 2ec1f75b 2019-03-26 stsp if (fileindex)
4039 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4040 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4041 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4042 2ec1f75b 2019-03-26 stsp err = unlockerr;
4043 33aa809d 2019-08-08 stsp return err;
4046 33aa809d 2019-08-08 stsp static const struct got_error *
4047 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4049 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4050 33aa809d 2019-08-08 stsp char *line = NULL;
4051 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4052 33aa809d 2019-08-08 stsp ssize_t linelen;
4054 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4055 33aa809d 2019-08-08 stsp if (linelen == -1) {
4056 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4057 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4058 33aa809d 2019-08-08 stsp goto done;
4060 33aa809d 2019-08-08 stsp return NULL;
4062 33aa809d 2019-08-08 stsp if (outfile) {
4063 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4064 33aa809d 2019-08-08 stsp if (n != linelen) {
4065 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4066 33aa809d 2019-08-08 stsp goto done;
4069 33aa809d 2019-08-08 stsp if (rejectfile) {
4070 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4071 33aa809d 2019-08-08 stsp if (n != linelen)
4072 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4075 33aa809d 2019-08-08 stsp free(line);
4076 2ec1f75b 2019-03-26 stsp return err;
4079 33aa809d 2019-08-08 stsp static const struct got_error *
4080 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4082 33aa809d 2019-08-08 stsp char *line = NULL;
4083 33aa809d 2019-08-08 stsp size_t linesize = 0;
4084 33aa809d 2019-08-08 stsp ssize_t linelen;
4086 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4087 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4088 500467ff 2019-09-25 hiltjo if (ferror(f))
4089 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4090 500467ff 2019-09-25 hiltjo return NULL;
4092 33aa809d 2019-08-08 stsp free(line);
4093 33aa809d 2019-08-08 stsp return NULL;
4096 33aa809d 2019-08-08 stsp static const struct got_error *
4097 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4098 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4099 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4101 33aa809d 2019-08-08 stsp const struct got_error *err;
4103 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4104 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4105 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4107 33aa809d 2019-08-08 stsp return err;
4108 33aa809d 2019-08-08 stsp (*line_cur1)++;
4110 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4111 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4112 33aa809d 2019-08-08 stsp if (rejectfile)
4113 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4115 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4117 33aa809d 2019-08-08 stsp return err;
4118 33aa809d 2019-08-08 stsp (*line_cur2)++;
4120 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4121 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4122 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4124 33aa809d 2019-08-08 stsp return err;
4125 33aa809d 2019-08-08 stsp (*line_cur2)++;
4127 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4128 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4129 33aa809d 2019-08-08 stsp if (rejectfile)
4130 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4132 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4134 33aa809d 2019-08-08 stsp return err;
4135 33aa809d 2019-08-08 stsp (*line_cur1)++;
4138 f1e81a05 2019-08-10 stsp return NULL;
4141 f1e81a05 2019-08-10 stsp static const struct got_error *
4142 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4143 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4145 f1e81a05 2019-08-10 stsp const struct got_error *err;
4147 f1e81a05 2019-08-10 stsp if (outfile) {
4148 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4149 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4150 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4152 f1e81a05 2019-08-10 stsp return err;
4153 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4156 f1e81a05 2019-08-10 stsp if (rejectfile) {
4157 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4158 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4159 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4161 f1e81a05 2019-08-10 stsp return err;
4162 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4166 33aa809d 2019-08-08 stsp return NULL;
4169 33aa809d 2019-08-08 stsp static const struct got_error *
4170 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4171 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4172 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4173 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4174 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4176 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4177 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4178 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4179 33aa809d 2019-08-08 stsp FILE *hunkfile;
4180 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4181 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4184 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4186 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4187 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4188 fe621944 2020-11-10 stsp start_old = cc.left.start;
4189 fe621944 2020-11-10 stsp end_old = cc.left.end;
4190 fe621944 2020-11-10 stsp start_new = cc.right.start;
4191 fe621944 2020-11-10 stsp end_new = cc.right.end;
4193 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4194 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4195 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4197 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4198 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4199 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4201 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4202 fe621944 2020-11-10 stsp if (diff_state == NULL)
4203 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4204 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4206 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4207 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4208 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4209 33aa809d 2019-08-08 stsp goto done;
4212 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4213 fe621944 2020-11-10 stsp diff_result, &cc);
4214 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4215 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4216 33aa809d 2019-08-08 stsp goto done;
4219 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4220 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4221 33aa809d 2019-08-08 stsp goto done;
4224 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4225 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4227 33aa809d 2019-08-08 stsp goto done;
4229 33aa809d 2019-08-08 stsp switch (*choice) {
4230 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4231 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4232 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4234 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4235 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4236 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4238 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4241 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4245 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4246 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4247 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4248 33aa809d 2019-08-08 stsp return err;
4251 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4252 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4253 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4254 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4255 1f1abb7e 2019-08-08 stsp void *progress_arg;
4256 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4257 33aa809d 2019-08-08 stsp void *patch_arg;
4258 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4261 e20a8b6f 2019-06-04 stsp static const struct got_error *
4262 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4263 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4264 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4265 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4266 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4268 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4269 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4270 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4271 1ebedb77 2019-10-19 stsp int fd2 = -1;
4272 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4273 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4274 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4275 fe621944 2020-11-10 stsp struct stat sb2;
4276 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4277 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4278 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4280 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4282 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4284 33aa809d 2019-08-08 stsp return err;
4286 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4287 12463d8b 2019-12-13 stsp fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4288 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4289 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4290 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4291 fa3cef63 2020-07-23 stsp goto done;
4293 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4294 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4295 fa3cef63 2020-07-23 stsp if (link_len == -1)
4296 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlinkat", path2);
4297 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4298 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4301 12463d8b 2019-12-13 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4302 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4303 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4304 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4305 fa3cef63 2020-07-23 stsp goto done;
4307 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4308 fa3cef63 2020-07-23 stsp sizeof(link_target));
4309 fa3cef63 2020-07-23 stsp if (link_len == -1)
4310 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4311 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4312 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4315 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4316 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4317 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4318 fa3cef63 2020-07-23 stsp goto done;
4321 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4322 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4323 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4324 fa3cef63 2020-07-23 stsp goto done;
4329 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4330 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4331 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4332 fa3cef63 2020-07-23 stsp goto done;
4334 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4335 fa3cef63 2020-07-23 stsp if (n != link_len) {
4336 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4337 fa3cef63 2020-07-23 stsp goto done;
4339 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4340 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4341 fa3cef63 2020-07-23 stsp goto done;
4343 fa3cef63 2020-07-23 stsp rewind(f2);
4346 33aa809d 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4348 33aa809d 2019-08-08 stsp goto done;
4350 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4352 33aa809d 2019-08-08 stsp goto done;
4354 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4356 33aa809d 2019-08-08 stsp goto done;
4358 64453f7e 2020-11-21 stsp err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4361 33aa809d 2019-08-08 stsp goto done;
4363 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4365 33aa809d 2019-08-08 stsp goto done;
4367 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4368 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4369 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4370 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4372 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4373 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4374 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4375 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4376 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4377 fe621944 2020-11-10 stsp nchanges++;
4379 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4380 33aa809d 2019-08-08 stsp int choice;
4381 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4382 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4383 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4384 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4385 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4386 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4388 33aa809d 2019-08-08 stsp goto done;
4389 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4390 33aa809d 2019-08-08 stsp have_content = 1;
4391 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4394 1ebedb77 2019-10-19 stsp if (have_content) {
4395 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4396 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4397 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4399 1ebedb77 2019-10-19 stsp goto done;
4401 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4402 3818e3c4 2020-11-01 naddy if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4403 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4404 fa3cef63 2020-07-23 stsp goto done;
4409 33aa809d 2019-08-08 stsp free(id_str);
4411 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4412 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4413 fe621944 2020-11-10 stsp if (err == NULL)
4414 fe621944 2020-11-10 stsp err = free_err;
4415 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4416 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4417 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4418 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4419 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4420 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4421 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4422 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4423 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4424 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4425 33aa809d 2019-08-08 stsp if (err || !have_content) {
4426 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4427 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4428 33aa809d 2019-08-08 stsp free(*path_outfile);
4429 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4431 33aa809d 2019-08-08 stsp free(path1);
4432 33aa809d 2019-08-08 stsp return err;
4435 33aa809d 2019-08-08 stsp static const struct got_error *
4436 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4437 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4438 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4439 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4441 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4442 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4443 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4444 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4445 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4446 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4447 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4448 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4449 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4450 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4452 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4453 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4454 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4455 d3bcc3d1 2019-08-08 stsp return NULL;
4457 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4458 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4459 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4461 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4462 65084dad 2019-08-08 stsp if (ie == NULL)
4463 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4465 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4466 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4467 a129376b 2019-03-28 stsp if (err) {
4468 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4469 a129376b 2019-03-28 stsp goto done;
4470 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4471 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4472 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4473 e20a8b6f 2019-06-04 stsp goto done;
4476 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4477 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4478 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4479 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4480 a129376b 2019-03-28 stsp goto done;
4483 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4484 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4485 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4486 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4487 a129376b 2019-03-28 stsp goto done;
4490 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4491 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4492 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4493 a129376b 2019-03-28 stsp goto done;
4498 1f1abb7e 2019-08-08 stsp err = got_object_id_by_path(&tree_id, a->repo,
4499 1f1abb7e 2019-08-08 stsp a->worktree->base_commit_id, tree_path);
4500 a9fa2909 2019-07-27 stsp if (err) {
4501 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4502 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4503 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4504 a9fa2909 2019-07-27 stsp goto done;
4506 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4508 a9fa2909 2019-07-27 stsp goto done;
4510 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4512 a9fa2909 2019-07-27 stsp goto done;
4514 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4515 1233e6b6 2020-10-19 stsp free(te_name);
4516 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4517 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4518 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4519 a9fa2909 2019-07-27 stsp goto done;
4523 a129376b 2019-03-28 stsp switch (status) {
4524 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4525 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4526 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4527 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4528 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4530 33aa809d 2019-08-08 stsp goto done;
4531 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4534 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4535 1f1abb7e 2019-08-08 stsp ie->path);
4537 1ee397ad 2019-07-12 stsp goto done;
4538 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4540 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4541 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4542 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4543 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4544 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4546 33aa809d 2019-08-08 stsp goto done;
4547 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4550 33aa809d 2019-08-08 stsp /* fall through */
4551 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4552 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4553 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4554 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4555 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4556 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4557 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4558 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4559 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4561 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4562 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4563 1f1abb7e 2019-08-08 stsp err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4565 65084dad 2019-08-08 stsp goto done;
4567 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4568 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4569 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4570 a129376b 2019-03-28 stsp goto done;
4573 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4574 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4575 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4576 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4577 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4578 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4579 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4581 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4582 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4583 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4584 369fd7e5 2020-07-23 stsp path_content);
4587 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4588 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4589 369fd7e5 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4590 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4592 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4593 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4594 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4595 369fd7e5 2020-07-23 stsp goto done;
4599 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4600 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4601 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4602 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4603 c90c8ce3 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4604 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4606 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4608 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4609 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4610 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4611 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4614 a129376b 2019-03-28 stsp goto done;
4615 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4616 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4617 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4618 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
4619 437adc9d 2020-12-10 yzhong blob->id.sha1,
4620 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4622 2e1fa222 2020-07-23 stsp goto done;
4624 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4625 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4626 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4635 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4636 33aa809d 2019-08-08 stsp free(path_content);
4637 e20a8b6f 2019-06-04 stsp free(parent_path);
4638 a129376b 2019-03-28 stsp free(tree_path);
4640 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4642 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4643 a129376b 2019-03-28 stsp free(tree_id);
4644 e20a8b6f 2019-06-04 stsp return err;
4647 e20a8b6f 2019-06-04 stsp const struct got_error *
4648 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4649 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4650 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4651 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4652 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4654 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4655 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4656 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4657 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4658 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4659 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4661 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4663 e20a8b6f 2019-06-04 stsp return err;
4665 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4667 e20a8b6f 2019-06-04 stsp goto done;
4669 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4670 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4671 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4672 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4673 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4674 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4675 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4676 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4677 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4678 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
4682 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4683 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4684 e20a8b6f 2019-06-04 stsp err = sync_err;
4686 fb399478 2019-07-12 stsp free(fileindex_path);
4687 a129376b 2019-03-28 stsp if (fileindex)
4688 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4689 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4690 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4691 a129376b 2019-03-28 stsp err = unlockerr;
4692 c4296144 2019-05-09 stsp return err;
4695 cf066bf8 2019-05-09 stsp static void
4696 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4698 24519714 2019-05-09 stsp free(ct->path);
4699 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4700 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4701 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4702 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4703 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4704 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4708 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4709 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4710 24519714 2019-05-09 stsp struct got_repository *repo;
4711 24519714 2019-05-09 stsp struct got_worktree *worktree;
4712 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4713 5f8a88c6 2019-08-03 stsp int have_staged_files;
4714 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4717 c4296144 2019-05-09 stsp static const struct got_error *
4718 88d0e355 2019-08-03 stsp collect_commitables(void *arg, unsigned char status,
4719 88d0e355 2019-08-03 stsp unsigned char staged_status, const char *relpath,
4720 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4721 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4723 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
4724 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4725 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4726 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4727 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4728 768aea60 2019-05-09 stsp struct stat sb;
4730 5f8a88c6 2019-08-03 stsp if (a->have_staged_files) {
4731 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4732 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4733 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4734 5f8a88c6 2019-08-03 stsp return NULL;
4736 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4737 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4739 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4740 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4741 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4742 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4743 5f8a88c6 2019-08-03 stsp return NULL;
4746 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4747 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4748 036813ee 2019-05-09 stsp goto done;
4750 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4751 036813ee 2019-05-09 stsp parent_path = strdup("");
4752 69960a46 2019-05-09 stsp if (parent_path == NULL)
4753 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4755 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4757 69960a46 2019-05-09 stsp return err;
4760 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4761 cf066bf8 2019-05-09 stsp if (ct == NULL) {
4762 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4763 c4296144 2019-05-09 stsp goto done;
4766 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4767 768aea60 2019-05-09 stsp relpath) == -1) {
4768 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4769 768aea60 2019-05-09 stsp goto done;
4772 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
4773 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
4774 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
4775 0aeb8099 2020-07-23 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4776 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
4777 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
4778 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
4779 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
4781 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
4782 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
4785 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4786 0aeb8099 2020-07-23 stsp goto done;
4788 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
4789 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
4790 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
4791 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4792 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
4793 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
4794 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
4795 12463d8b 2019-12-13 stsp ct->ondisk_path);
4796 12463d8b 2019-12-13 stsp goto done;
4798 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
4799 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
4800 768aea60 2019-05-09 stsp goto done;
4802 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
4805 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4806 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4807 44d03001 2019-05-09 stsp relpath) == -1) {
4808 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4809 44d03001 2019-05-09 stsp goto done;
4812 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4813 35213c7c 2020-07-23 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4814 35213c7c 2020-07-23 stsp int is_bad_symlink;
4815 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
4816 35213c7c 2020-07-23 stsp ssize_t target_len;
4817 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
4818 35213c7c 2020-07-23 stsp sizeof(target_path));
4819 35213c7c 2020-07-23 stsp if (target_len == -1) {
4820 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
4821 35213c7c 2020-07-23 stsp ct->ondisk_path);
4822 35213c7c 2020-07-23 stsp goto done;
4824 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
4825 35213c7c 2020-07-23 stsp target_len, ct->ondisk_path, a->worktree->root_path);
4827 35213c7c 2020-07-23 stsp goto done;
4828 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
4829 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
4830 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
4831 35213c7c 2020-07-23 stsp goto done;
4836 cf066bf8 2019-05-09 stsp ct->status = status;
4837 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
4838 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
4839 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
4840 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
4841 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
4842 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
4843 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4844 b416585c 2019-05-13 stsp goto done;
4846 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
4847 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
4848 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
4849 f0b75401 2019-08-03 stsp goto done;
4852 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
4853 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
4854 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4855 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
4856 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4857 036813ee 2019-05-09 stsp goto done;
4860 24519714 2019-05-09 stsp ct->path = strdup(path);
4861 24519714 2019-05-09 stsp if (ct->path == NULL) {
4862 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4863 24519714 2019-05-09 stsp goto done;
4865 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4867 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
4868 ed175427 2019-05-09 stsp free_commitable(ct);
4869 24519714 2019-05-09 stsp free(parent_path);
4870 036813ee 2019-05-09 stsp free(path);
4871 a129376b 2019-03-28 stsp return err;
4874 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
4875 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
4876 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4877 036813ee 2019-05-09 stsp struct got_repository *);
4879 ed175427 2019-05-09 stsp static const struct got_error *
4880 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4881 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
4882 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
4883 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4884 afa376bf 2019-05-09 stsp struct got_repository *repo)
4886 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
4887 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
4888 036813ee 2019-05-09 stsp char *subpath;
4890 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
4891 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4892 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4894 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
4896 ed175427 2019-05-09 stsp return err;
4898 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
4899 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
4900 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
4901 036813ee 2019-05-09 stsp free(subpath);
4902 036813ee 2019-05-09 stsp return err;
4905 036813ee 2019-05-09 stsp static const struct got_error *
4906 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4908 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4909 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
4911 036813ee 2019-05-09 stsp *match = 0;
4913 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
4914 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
4915 0f63689d 2019-05-10 stsp return NULL;
4918 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4920 0f63689d 2019-05-10 stsp return err;
4921 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
4922 036813ee 2019-05-09 stsp free(ct_parent_path);
4923 036813ee 2019-05-09 stsp return err;
4926 768aea60 2019-05-09 stsp static mode_t
4927 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
4929 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
4930 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
4932 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4935 036813ee 2019-05-09 stsp static const struct got_error *
4936 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4937 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
4939 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4941 036813ee 2019-05-09 stsp *new_te = NULL;
4943 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
4945 036813ee 2019-05-09 stsp goto done;
4947 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4949 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
4950 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4951 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4953 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4955 036813ee 2019-05-09 stsp if (err && *new_te) {
4956 56e0773d 2019-11-28 stsp free(*new_te);
4957 036813ee 2019-05-09 stsp *new_te = NULL;
4959 036813ee 2019-05-09 stsp return err;
4962 036813ee 2019-05-09 stsp static const struct got_error *
4963 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4964 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
4966 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4967 102b254e 2020-10-19 stsp char *ct_name = NULL;
4969 036813ee 2019-05-09 stsp *new_te = NULL;
4971 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
4972 036813ee 2019-05-09 stsp if (*new_te == NULL)
4973 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
4975 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
4977 036813ee 2019-05-09 stsp goto done;
4978 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4979 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
4980 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
4981 036813ee 2019-05-09 stsp goto done;
4984 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4986 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
4987 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4988 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4990 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4992 102b254e 2020-10-19 stsp free(ct_name);
4993 036813ee 2019-05-09 stsp if (err && *new_te) {
4994 56e0773d 2019-11-28 stsp free(*new_te);
4995 036813ee 2019-05-09 stsp *new_te = NULL;
4997 036813ee 2019-05-09 stsp return err;
5000 036813ee 2019-05-09 stsp static const struct got_error *
5001 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5002 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5004 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5005 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5007 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5009 036813ee 2019-05-09 stsp return err;
5010 036813ee 2019-05-09 stsp if (new_pe == NULL)
5011 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5012 036813ee 2019-05-09 stsp return NULL;
5015 afa376bf 2019-05-09 stsp static const struct got_error *
5016 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5017 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5019 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5020 5f8a88c6 2019-08-03 stsp unsigned char status;
5022 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5023 afa376bf 2019-05-09 stsp ct_path++;
5025 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5026 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5028 5f8a88c6 2019-08-03 stsp status = ct->status;
5030 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5031 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5034 036813ee 2019-05-09 stsp static const struct got_error *
5035 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5036 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5038 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5039 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5040 44d03001 2019-05-09 stsp char *te_path;
5042 44d03001 2019-05-09 stsp *modified = 0;
5044 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5045 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5046 44d03001 2019-05-09 stsp te->name) == -1)
5047 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5049 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5050 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5051 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5052 44d03001 2019-05-09 stsp strlen(te_path));
5053 62d463ca 2020-10-20 naddy if (*modified)
5057 44d03001 2019-05-09 stsp free(te_path);
5058 44d03001 2019-05-09 stsp return err;
5061 44d03001 2019-05-09 stsp static const struct got_error *
5062 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5063 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5064 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5066 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5067 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5069 036813ee 2019-05-09 stsp *ctp = NULL;
5071 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5072 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5073 036813ee 2019-05-09 stsp char *ct_name = NULL;
5074 036813ee 2019-05-09 stsp int path_matches;
5076 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5077 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5078 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5079 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
5082 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5083 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5087 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5090 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5092 036813ee 2019-05-09 stsp return err;
5093 036813ee 2019-05-09 stsp if (!path_matches)
5096 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5098 d34b633e 2020-10-19 stsp return err;
5100 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5101 d34b633e 2020-10-19 stsp free(ct_name);
5104 d34b633e 2020-10-19 stsp free(ct_name);
5106 036813ee 2019-05-09 stsp *ctp = ct;
5110 2b496619 2019-07-10 stsp return err;
5113 2b496619 2019-07-10 stsp static const struct got_error *
5114 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5115 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5116 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5117 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5118 2b496619 2019-07-10 stsp struct got_repository *repo)
5120 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5121 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5122 2b496619 2019-07-10 stsp char *subtree_path;
5123 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5124 ba580f68 2020-03-22 stsp int nentries;
5126 2b496619 2019-07-10 stsp *new_tep = NULL;
5128 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5129 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5130 2b496619 2019-07-10 stsp child_path) == -1)
5131 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5133 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5134 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5135 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5136 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5138 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5139 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5140 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5141 2b496619 2019-07-10 stsp goto done;
5143 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5144 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5145 2b496619 2019-07-10 stsp if (err) {
5146 56e0773d 2019-11-28 stsp free(new_te);
5147 2b496619 2019-07-10 stsp goto done;
5149 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5152 2b496619 2019-07-10 stsp free(subtree_path);
5153 2b496619 2019-07-10 stsp if (err == NULL)
5154 2b496619 2019-07-10 stsp *new_tep = new_te;
5155 036813ee 2019-05-09 stsp return err;
5158 036813ee 2019-05-09 stsp static const struct got_error *
5159 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5160 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5161 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5162 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5163 036813ee 2019-05-09 stsp struct got_repository *repo)
5165 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5166 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5167 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5168 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5170 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5171 ba580f68 2020-03-22 stsp *nentries = 0;
5173 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5174 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5175 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5176 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5178 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5179 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5180 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5183 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5184 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5187 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5188 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5190 036813ee 2019-05-09 stsp goto done;
5192 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5193 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5194 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5196 036813ee 2019-05-09 stsp goto done;
5197 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5199 afa376bf 2019-05-09 stsp goto done;
5200 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5201 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5203 2f51b5b3 2019-05-09 stsp goto done;
5204 ba580f68 2020-03-22 stsp (*nentries)++;
5206 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5207 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5208 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5209 2b496619 2019-07-10 stsp == NULL) {
5210 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5211 2b496619 2019-07-10 stsp child_path, path_base_tree,
5212 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5215 2b496619 2019-07-10 stsp goto done;
5216 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5218 2b496619 2019-07-10 stsp goto done;
5219 ba580f68 2020-03-22 stsp (*nentries)++;
5224 2f51b5b3 2019-05-09 stsp if (base_tree) {
5225 56e0773d 2019-11-28 stsp int i, nbase_entries;
5226 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5227 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5228 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5229 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5231 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5232 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5233 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5234 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5236 63c5ca5d 2019-08-24 stsp goto done;
5237 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5239 63c5ca5d 2019-08-24 stsp goto done;
5240 ba580f68 2020-03-22 stsp (*nentries)++;
5244 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5245 44d03001 2019-05-09 stsp int modified;
5246 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5248 036813ee 2019-05-09 stsp goto done;
5249 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5250 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5252 2f51b5b3 2019-05-09 stsp goto done;
5253 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5254 44d03001 2019-05-09 stsp if (modified) {
5255 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5256 ba580f68 2020-03-22 stsp int nsubentries;
5257 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5258 ba580f68 2020-03-22 stsp &nsubentries, te,
5259 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5260 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5262 44d03001 2019-05-09 stsp goto done;
5263 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5264 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5265 ba580f68 2020-03-22 stsp free(new_id);
5268 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5269 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5270 56e0773d 2019-11-28 stsp free(new_id);
5272 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5274 036813ee 2019-05-09 stsp goto done;
5275 ba580f68 2020-03-22 stsp (*nentries)++;
5279 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5280 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5282 f66c734c 2019-09-22 stsp goto done;
5284 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5285 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5286 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5287 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5288 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5289 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5291 2f51b5b3 2019-05-09 stsp goto done;
5292 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5294 2f51b5b3 2019-05-09 stsp goto done;
5295 ba580f68 2020-03-22 stsp (*nentries)++;
5297 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5298 b416585c 2019-05-13 stsp status_arg);
5300 afa376bf 2019-05-09 stsp goto done;
5302 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5303 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5305 2f51b5b3 2019-05-09 stsp goto done;
5306 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5308 2f51b5b3 2019-05-09 stsp goto done;
5309 ba580f68 2020-03-22 stsp (*nentries)++;
5314 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5315 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5317 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
5318 2e1fa222 2020-07-23 stsp return err;
5321 2e1fa222 2020-07-23 stsp static const struct got_error *
5322 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5323 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5324 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5325 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5327 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5328 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5329 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5331 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5332 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5333 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5335 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5337 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5338 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5339 437adc9d 2020-12-10 yzhong if (err)
5340 437adc9d 2020-12-10 yzhong goto done;
5343 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5344 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5345 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5346 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5347 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5348 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5349 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5350 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5352 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5353 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5354 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
5355 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5356 72fd46fa 2019-09-06 stsp !have_staged_files);
5358 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5359 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5360 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
5361 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5362 72fd46fa 2019-09-06 stsp !have_staged_files);
5364 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5366 437adc9d 2020-12-10 yzhong goto done;
5367 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5368 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
5369 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
5370 3969253a 2020-03-07 stsp if (err) {
5371 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5372 437adc9d 2020-12-10 yzhong goto done;
5374 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5375 3969253a 2020-03-07 stsp if (err) {
5376 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5377 437adc9d 2020-12-10 yzhong goto done;
5380 437adc9d 2020-12-10 yzhong free(relpath);
5381 437adc9d 2020-12-10 yzhong relpath = NULL;
5384 437adc9d 2020-12-10 yzhong free(relpath);
5385 d56d26ce 2019-05-10 stsp return err;
5389 d56d26ce 2019-05-10 stsp static const struct got_error *
5390 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5391 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5392 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5393 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5394 735ef5ac 2019-08-03 stsp int ood_errcode)
5396 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5397 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5399 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5400 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5401 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5402 1a36436d 2019-06-10 stsp return NULL;
5404 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5405 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5407 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5408 735ef5ac 2019-08-03 stsp in_repo_path);
5409 9bead371 2019-07-28 stsp if (err) {
5410 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5411 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5412 1a36436d 2019-06-10 stsp goto done;
5413 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5414 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5416 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5417 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5418 735ef5ac 2019-08-03 stsp in_repo_path);
5419 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5420 1a36436d 2019-06-10 stsp goto done;
5421 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5425 1a36436d 2019-06-10 stsp return err;
5428 c4296144 2019-05-09 stsp const struct got_error *
5429 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5430 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5431 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id, struct got_worktree *worktree,
5432 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
5433 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5434 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5435 afa376bf 2019-05-09 stsp struct got_repository *repo)
5437 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5438 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5439 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5440 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5441 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5442 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5443 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5444 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5445 ba580f68 2020-03-22 stsp int nentries;
5446 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5447 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5448 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5450 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5452 de18fc63 2019-05-09 stsp SIMPLEQ_INIT(&parent_ids);
5454 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5456 588edf97 2019-05-10 stsp goto done;
5458 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5460 588edf97 2019-05-10 stsp goto done;
5462 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5463 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5468 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5469 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5473 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5474 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5475 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5476 cf066bf8 2019-05-09 stsp char *ondisk_path;
5478 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5479 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5480 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5483 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5484 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5485 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5488 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5489 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5490 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5491 cf066bf8 2019-05-09 stsp goto done;
5493 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5494 2e1fa222 2020-07-23 stsp free(ondisk_path);
5496 cf066bf8 2019-05-09 stsp goto done;
5499 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5500 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5501 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5503 036813ee 2019-05-09 stsp goto done;
5505 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5507 de18fc63 2019-05-09 stsp goto done;
5508 de18fc63 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5509 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5510 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5511 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
5512 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5513 33ad4cbe 2019-05-12 jcs free(logmsg);
5515 9d40349a 2019-05-09 stsp goto done;
5517 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5518 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5519 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5520 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5521 09f5bd90 2019-05-09 stsp goto done;
5523 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
5524 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5526 09f5bd90 2019-05-09 stsp goto done;
5527 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5529 ebf99748 2019-05-09 stsp goto done;
5530 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5531 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5532 09f5bd90 2019-05-09 stsp goto done;
5534 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
5535 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
5537 f2c16586 2019-05-09 stsp goto done;
5538 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
5540 f2c16586 2019-05-09 stsp goto done;
5542 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5544 09f5bd90 2019-05-09 stsp goto done;
5546 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
5548 ebf99748 2019-05-09 stsp goto done;
5550 588edf97 2019-05-10 stsp if (head_tree)
5551 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
5552 588edf97 2019-05-10 stsp if (head_commit)
5553 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
5554 09f5bd90 2019-05-09 stsp free(head_commit_id2);
5555 2f17228e 2019-05-12 stsp if (head_ref2) {
5556 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
5557 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
5558 2f17228e 2019-05-12 stsp err = unlockerr;
5559 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
5561 39cd0ff6 2019-07-12 stsp return err;
5564 5c1e53bc 2019-07-28 stsp static const struct got_error *
5565 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
5566 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
5568 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
5569 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
5571 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
5572 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
5573 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
5575 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
5576 5c1e53bc 2019-07-28 stsp ct_path++;
5578 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
5579 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
5583 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
5584 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
5586 5c1e53bc 2019-07-28 stsp return NULL;
5589 f0b75401 2019-08-03 stsp static const struct got_error *
5590 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
5592 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
5594 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5595 f0b75401 2019-08-03 stsp *have_staged_files = 1;
5596 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
5599 f0b75401 2019-08-03 stsp return NULL;
5602 5f8a88c6 2019-08-03 stsp static const struct got_error *
5603 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
5604 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
5606 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
5607 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
5609 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5610 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
5612 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5613 5f8a88c6 2019-08-03 stsp if (ie == NULL)
5614 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5615 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5616 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
5617 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
5620 5f8a88c6 2019-08-03 stsp return NULL;
5623 39cd0ff6 2019-07-12 stsp const struct got_error *
5624 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
5625 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
5626 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
5627 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5628 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
5629 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
5631 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5632 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
5633 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
5634 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5635 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5636 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
5637 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
5638 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5639 f0b75401 2019-08-03 stsp int have_staged_files = 0;
5641 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
5643 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5645 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
5647 39cd0ff6 2019-07-12 stsp goto done;
5649 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5651 39cd0ff6 2019-07-12 stsp goto done;
5653 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5655 39cd0ff6 2019-07-12 stsp goto done;
5657 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5659 39cd0ff6 2019-07-12 stsp goto done;
5661 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5662 5f8a88c6 2019-08-03 stsp &have_staged_files);
5663 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
5664 5f8a88c6 2019-08-03 stsp goto done;
5665 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
5666 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
5668 5f8a88c6 2019-08-03 stsp goto done;
5671 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5672 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
5673 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
5674 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
5675 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
5676 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5677 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5678 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5679 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5681 5c1e53bc 2019-07-28 stsp goto done;
5684 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5685 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5686 39cd0ff6 2019-07-12 stsp goto done;
5689 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5690 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
5692 5c1e53bc 2019-07-28 stsp goto done;
5695 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5696 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5697 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5699 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5700 f0b75401 2019-08-03 stsp ct_path++;
5701 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5702 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5703 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5705 b50cabdf 2019-07-12 stsp goto done;
5709 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5710 5c1e53bc 2019-07-28 stsp head_commit_id, worktree, author, committer,
5711 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5713 39cd0ff6 2019-07-12 stsp goto done;
5715 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
5716 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
5717 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5718 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5719 39cd0ff6 2019-07-12 stsp err = sync_err;
5721 39cd0ff6 2019-07-12 stsp if (fileindex)
5722 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5723 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5724 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5725 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5726 39cd0ff6 2019-07-12 stsp err = unlockerr;
5727 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5728 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5729 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5731 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5732 c4296144 2019-05-09 stsp return err;
5735 8656d6c4 2019-05-20 stsp const char *
5736 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5738 8656d6c4 2019-05-20 stsp return ct->path;
5741 8656d6c4 2019-05-20 stsp unsigned int
5742 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
5744 8656d6c4 2019-05-20 stsp return ct->status;
5747 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
5748 818c7501 2019-07-11 stsp struct got_worktree *worktree;
5749 818c7501 2019-07-11 stsp struct got_repository *repo;
5752 818c7501 2019-07-11 stsp static const struct got_error *
5753 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5755 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5756 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
5757 818c7501 2019-07-11 stsp unsigned char status;
5758 818c7501 2019-07-11 stsp struct stat sb;
5759 818c7501 2019-07-11 stsp char *ondisk_path;
5761 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
5762 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5763 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
5764 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
5766 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5768 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
5770 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
5771 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5772 818c7501 2019-07-11 stsp free(ondisk_path);
5774 818c7501 2019-07-11 stsp return err;
5776 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
5777 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
5778 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5779 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5781 818c7501 2019-07-11 stsp return NULL;
5784 818c7501 2019-07-11 stsp const struct got_error *
5785 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5786 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5787 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
5788 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5790 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5791 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5792 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
5793 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
5794 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
5795 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5796 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
5798 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5799 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5800 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5802 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
5804 818c7501 2019-07-11 stsp return err;
5806 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5808 818c7501 2019-07-11 stsp goto done;
5810 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
5811 818c7501 2019-07-11 stsp ok_arg.repo = repo;
5812 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5815 818c7501 2019-07-11 stsp goto done;
5817 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5819 818c7501 2019-07-11 stsp goto done;
5821 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5823 818c7501 2019-07-11 stsp goto done;
5825 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5827 818c7501 2019-07-11 stsp goto done;
5829 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5832 e51d7b55 2020-01-04 stsp goto done;
5834 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5836 818c7501 2019-07-11 stsp goto done;
5837 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5838 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5839 e51d7b55 2020-01-04 stsp goto done;
5842 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
5843 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
5845 818c7501 2019-07-11 stsp goto done;
5846 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
5848 818c7501 2019-07-11 stsp goto done;
5850 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
5852 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5854 818c7501 2019-07-11 stsp goto done;
5856 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
5858 818c7501 2019-07-11 stsp goto done;
5860 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
5861 818c7501 2019-07-11 stsp worktree->base_commit_id);
5863 818c7501 2019-07-11 stsp goto done;
5864 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
5866 818c7501 2019-07-11 stsp goto done;
5868 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
5870 818c7501 2019-07-11 stsp goto done;
5872 818c7501 2019-07-11 stsp free(fileindex_path);
5873 818c7501 2019-07-11 stsp free(tmp_branch_name);
5874 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
5875 818c7501 2019-07-11 stsp free(branch_ref_name);
5876 818c7501 2019-07-11 stsp if (branch_ref)
5877 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5878 818c7501 2019-07-11 stsp if (wt_branch)
5879 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
5880 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
5881 818c7501 2019-07-11 stsp if (err) {
5882 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
5883 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
5884 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5886 818c7501 2019-07-11 stsp if (*tmp_branch) {
5887 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5888 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5890 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5891 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5892 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5894 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
5896 818c7501 2019-07-11 stsp return err;
5899 818c7501 2019-07-11 stsp const struct got_error *
5900 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
5901 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5902 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
5903 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
5905 818c7501 2019-07-11 stsp const struct got_error *err;
5906 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5907 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5908 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5909 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
5910 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
5912 818c7501 2019-07-11 stsp *commit_id = NULL;
5913 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
5914 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
5915 3e3a69f1 2019-07-25 stsp *branch = NULL;
5916 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5918 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
5920 3e3a69f1 2019-07-25 stsp return err;
5922 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5924 f032f1f7 2019-08-04 stsp goto done;
5926 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5927 f032f1f7 2019-08-04 stsp &have_staged_files);
5928 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
5929 f032f1f7 2019-08-04 stsp goto done;
5930 f032f1f7 2019-08-04 stsp if (have_staged_files) {
5931 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
5932 3e3a69f1 2019-07-25 stsp goto done;
5935 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5937 f032f1f7 2019-08-04 stsp goto done;
5939 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5941 818c7501 2019-07-11 stsp goto done;
5943 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5945 818c7501 2019-07-11 stsp goto done;
5947 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5949 818c7501 2019-07-11 stsp goto done;
5951 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5953 818c7501 2019-07-11 stsp goto done;
5955 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
5956 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
5958 818c7501 2019-07-11 stsp goto done;
5960 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5962 818c7501 2019-07-11 stsp goto done;
5964 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
5966 818c7501 2019-07-11 stsp goto done;
5968 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
5969 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
5971 818c7501 2019-07-11 stsp goto done;
5973 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5975 818c7501 2019-07-11 stsp goto done;
5977 818c7501 2019-07-11 stsp free(commit_ref_name);
5978 818c7501 2019-07-11 stsp free(branch_ref_name);
5979 3e3a69f1 2019-07-25 stsp free(fileindex_path);
5980 818c7501 2019-07-11 stsp if (commit_ref)
5981 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
5982 818c7501 2019-07-11 stsp if (branch_ref)
5983 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5984 818c7501 2019-07-11 stsp if (err) {
5985 818c7501 2019-07-11 stsp free(*commit_id);
5986 818c7501 2019-07-11 stsp *commit_id = NULL;
5987 818c7501 2019-07-11 stsp if (*tmp_branch) {
5988 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5989 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5991 818c7501 2019-07-11 stsp if (*new_base_branch) {
5992 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
5993 818c7501 2019-07-11 stsp *new_base_branch = NULL;
5995 818c7501 2019-07-11 stsp if (*branch) {
5996 818c7501 2019-07-11 stsp got_ref_close(*branch);
5997 818c7501 2019-07-11 stsp *branch = NULL;
5999 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6000 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6001 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6003 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6005 818c7501 2019-07-11 stsp return err;
6008 818c7501 2019-07-11 stsp const struct got_error *
6009 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6011 818c7501 2019-07-11 stsp const struct got_error *err;
6012 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6014 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6016 818c7501 2019-07-11 stsp return err;
6018 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6019 818c7501 2019-07-11 stsp free(tmp_branch_name);
6020 818c7501 2019-07-11 stsp return NULL;
6023 818c7501 2019-07-11 stsp static const struct got_error *
6024 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6025 818c7501 2019-07-11 stsp char **logmsg, void *arg)
6027 0ebf8283 2019-07-24 stsp *logmsg = arg;
6028 818c7501 2019-07-11 stsp return NULL;
6031 818c7501 2019-07-11 stsp static const struct got_error *
6032 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6033 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6034 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6035 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6037 818c7501 2019-07-11 stsp return NULL;
6040 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6041 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6042 01757395 2019-07-12 stsp void *progress_arg;
6043 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6046 01757395 2019-07-12 stsp static const struct got_error *
6047 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6049 01757395 2019-07-12 stsp const struct got_error *err;
6050 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6052 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6054 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6056 01757395 2019-07-12 stsp return err;
6058 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6059 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6060 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6061 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6062 01757395 2019-07-12 stsp return NULL;
6064 01757395 2019-07-12 stsp p = strdup(path);
6065 01757395 2019-07-12 stsp if (p == NULL)
6066 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6068 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6069 01757395 2019-07-12 stsp if (err || new == NULL)
6071 01757395 2019-07-12 stsp return err;
6075 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6077 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6079 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
6080 01757395 2019-07-12 stsp free((char *)pe->path);
6082 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
6085 0ebf8283 2019-07-24 stsp static const struct got_error *
6086 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6087 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6089 818c7501 2019-07-11 stsp const struct got_error *err;
6090 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6092 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6093 818c7501 2019-07-11 stsp if (err) {
6094 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6095 818c7501 2019-07-11 stsp goto done;
6096 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6098 818c7501 2019-07-11 stsp goto done;
6099 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6101 818c7501 2019-07-11 stsp goto done;
6102 de05890f 2020-03-05 stsp } else if (is_rebase) {
6103 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6106 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6108 818c7501 2019-07-11 stsp goto done;
6109 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6110 818c7501 2019-07-11 stsp free(stored_id);
6111 818c7501 2019-07-11 stsp if (cmp != 0) {
6112 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6113 818c7501 2019-07-11 stsp goto done;
6117 0ebf8283 2019-07-24 stsp if (commit_ref)
6118 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6119 0ebf8283 2019-07-24 stsp return err;
6122 0ebf8283 2019-07-24 stsp static const struct got_error *
6123 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6124 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6125 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6126 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6127 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6128 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6130 0ebf8283 2019-07-24 stsp const struct got_error *err;
6131 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6132 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6133 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6135 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6137 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6139 0ebf8283 2019-07-24 stsp return err;
6141 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6142 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6143 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6144 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6145 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6146 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6147 818c7501 2019-07-11 stsp if (commit_ref)
6148 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6149 818c7501 2019-07-11 stsp return err;
6152 818c7501 2019-07-11 stsp const struct got_error *
6153 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6154 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6155 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6156 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6157 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6158 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6160 0ebf8283 2019-07-24 stsp const struct got_error *err;
6161 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6163 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6165 0ebf8283 2019-07-24 stsp return err;
6167 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6169 0ebf8283 2019-07-24 stsp goto done;
6171 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6172 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6173 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6175 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6176 0ebf8283 2019-07-24 stsp return err;
6179 0ebf8283 2019-07-24 stsp const struct got_error *
6180 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6181 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6182 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6183 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6184 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6185 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6187 0ebf8283 2019-07-24 stsp const struct got_error *err;
6188 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6190 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6192 0ebf8283 2019-07-24 stsp return err;
6194 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6196 0ebf8283 2019-07-24 stsp goto done;
6198 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6199 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6200 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6202 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6203 0ebf8283 2019-07-24 stsp return err;
6206 0ebf8283 2019-07-24 stsp static const struct got_error *
6207 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6208 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6209 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6210 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6211 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
6213 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6214 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6215 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6216 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6217 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6218 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6219 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6221 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6222 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6224 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6226 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6228 3e3a69f1 2019-07-25 stsp return err;
6230 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6231 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6232 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6233 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6235 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6236 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6237 01757395 2019-07-12 stsp * TODO: Ideally, merged_paths would contain a list of commitables
6238 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6240 01757395 2019-07-12 stsp if (merged_paths) {
6241 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6242 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6243 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6244 f2a9dc41 2019-12-13 tracey repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6247 01757395 2019-07-12 stsp goto done;
6250 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6251 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6253 01757395 2019-07-12 stsp goto done;
6256 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6257 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6258 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6260 a0e95631 2019-07-12 stsp goto done;
6261 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6262 a0e95631 2019-07-12 stsp goto done;
6265 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6267 edd02c5e 2019-07-11 stsp goto done;
6269 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6271 818c7501 2019-07-11 stsp goto done;
6273 5943eee2 2019-08-13 stsp if (new_logmsg) {
6274 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6275 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6276 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6277 5943eee2 2019-08-13 stsp goto done;
6280 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6282 5943eee2 2019-08-13 stsp goto done;
6285 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6286 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6287 5c1e53bc 2019-07-28 stsp worktree, got_object_commit_get_author(orig_commit),
6288 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
6289 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6291 a0e95631 2019-07-12 stsp goto done;
6293 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6295 a0e95631 2019-07-12 stsp goto done;
6297 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6299 a0e95631 2019-07-12 stsp goto done;
6301 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6302 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6303 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6304 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6305 a0e95631 2019-07-12 stsp err = sync_err;
6307 a0e95631 2019-07-12 stsp free(fileindex_path);
6308 a0e95631 2019-07-12 stsp free(head_commit_id);
6309 a0e95631 2019-07-12 stsp if (head_ref)
6310 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6311 818c7501 2019-07-11 stsp if (err) {
6312 818c7501 2019-07-11 stsp free(*new_commit_id);
6313 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6315 818c7501 2019-07-11 stsp return err;
6318 818c7501 2019-07-11 stsp const struct got_error *
6319 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6320 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6321 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6322 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6323 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6325 0ebf8283 2019-07-24 stsp const struct got_error *err;
6326 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6327 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6328 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6330 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6332 0ebf8283 2019-07-24 stsp return err;
6334 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6336 0ebf8283 2019-07-24 stsp goto done;
6337 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6339 0ebf8283 2019-07-24 stsp goto done;
6340 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6341 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6342 0ebf8283 2019-07-24 stsp goto done;
6345 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6346 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6348 0ebf8283 2019-07-24 stsp if (commit_ref)
6349 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6350 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6351 0ebf8283 2019-07-24 stsp free(commit_id);
6352 0ebf8283 2019-07-24 stsp return err;
6355 0ebf8283 2019-07-24 stsp const struct got_error *
6356 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6357 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6358 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6359 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6360 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6361 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6363 0ebf8283 2019-07-24 stsp const struct got_error *err;
6364 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6365 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6367 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6369 0ebf8283 2019-07-24 stsp return err;
6371 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6373 0ebf8283 2019-07-24 stsp goto done;
6375 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6376 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6378 0ebf8283 2019-07-24 stsp if (commit_ref)
6379 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6380 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6381 0ebf8283 2019-07-24 stsp return err;
6384 0ebf8283 2019-07-24 stsp const struct got_error *
6385 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6386 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6388 3e3a69f1 2019-07-25 stsp if (fileindex)
6389 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6390 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6393 69844fba 2019-07-11 stsp static const struct got_error *
6394 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6396 69844fba 2019-07-11 stsp const struct got_error *err;
6397 69844fba 2019-07-11 stsp struct got_reference *ref;
6399 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6400 69844fba 2019-07-11 stsp if (err) {
6401 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6402 69844fba 2019-07-11 stsp return NULL;
6403 69844fba 2019-07-11 stsp return err;
6406 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6407 69844fba 2019-07-11 stsp got_ref_close(ref);
6408 69844fba 2019-07-11 stsp return err;
6411 69844fba 2019-07-11 stsp static const struct got_error *
6412 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6414 69844fba 2019-07-11 stsp const struct got_error *err;
6415 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6416 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6418 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6420 69844fba 2019-07-11 stsp goto done;
6421 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6423 69844fba 2019-07-11 stsp goto done;
6425 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6427 69844fba 2019-07-11 stsp goto done;
6428 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6430 69844fba 2019-07-11 stsp goto done;
6432 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6434 69844fba 2019-07-11 stsp goto done;
6435 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6437 69844fba 2019-07-11 stsp goto done;
6439 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6441 69844fba 2019-07-11 stsp goto done;
6442 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6444 69844fba 2019-07-11 stsp goto done;
6447 69844fba 2019-07-11 stsp free(tmp_branch_name);
6448 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6449 69844fba 2019-07-11 stsp free(branch_ref_name);
6450 69844fba 2019-07-11 stsp free(commit_ref_name);
6451 69844fba 2019-07-11 stsp return err;
6454 818c7501 2019-07-11 stsp const struct got_error *
6455 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
6456 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6457 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6458 818c7501 2019-07-11 stsp struct got_repository *repo)
6460 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
6461 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
6462 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
6464 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6466 818c7501 2019-07-11 stsp return err;
6468 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6470 818c7501 2019-07-11 stsp goto done;
6472 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
6474 818c7501 2019-07-11 stsp goto done;
6476 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
6478 818c7501 2019-07-11 stsp goto done;
6480 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
6482 a615e0e7 2020-12-16 stsp goto done;
6484 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
6486 a615e0e7 2020-12-16 stsp goto done;
6487 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6488 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6489 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
6490 a615e0e7 2020-12-16 stsp err = sync_err;
6492 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
6493 a615e0e7 2020-12-16 stsp free(fileindex_path);
6494 818c7501 2019-07-11 stsp free(new_head_commit_id);
6495 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6496 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6497 818c7501 2019-07-11 stsp err = unlockerr;
6498 818c7501 2019-07-11 stsp return err;
6501 818c7501 2019-07-11 stsp const struct got_error *
6502 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
6503 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6504 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
6505 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6507 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
6508 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
6509 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
6510 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6511 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6512 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
6514 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6516 818c7501 2019-07-11 stsp return err;
6518 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
6519 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
6521 818c7501 2019-07-11 stsp goto done;
6523 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
6525 818c7501 2019-07-11 stsp goto done;
6528 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
6529 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
6530 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
6532 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
6534 818c7501 2019-07-11 stsp goto done;
6536 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6538 818c7501 2019-07-11 stsp goto done;
6540 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
6541 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
6543 ca355955 2019-07-12 stsp goto done;
6545 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
6547 ca355955 2019-07-12 stsp goto done;
6549 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6551 818c7501 2019-07-11 stsp goto done;
6553 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6554 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6555 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6556 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6557 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6558 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6559 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6560 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6561 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6563 1f1abb7e 2019-08-08 stsp goto sync;
6565 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6566 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
6568 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6569 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
6570 ca355955 2019-07-12 stsp err = sync_err;
6572 818c7501 2019-07-11 stsp got_ref_close(resolved);
6573 a3a2faf2 2019-07-12 stsp free(tree_id);
6574 818c7501 2019-07-11 stsp free(commit_id);
6575 0ebf8283 2019-07-24 stsp if (fileindex)
6576 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
6577 0ebf8283 2019-07-24 stsp free(fileindex_path);
6579 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6580 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6581 0ebf8283 2019-07-24 stsp err = unlockerr;
6582 0ebf8283 2019-07-24 stsp return err;
6585 0ebf8283 2019-07-24 stsp const struct got_error *
6586 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6587 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6588 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
6589 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6591 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6592 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6593 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
6594 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
6595 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6596 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
6597 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
6598 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6600 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6601 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6602 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6603 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6605 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6607 0ebf8283 2019-07-24 stsp return err;
6609 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6611 0ebf8283 2019-07-24 stsp goto done;
6613 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
6614 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
6615 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6618 0ebf8283 2019-07-24 stsp goto done;
6620 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6622 0ebf8283 2019-07-24 stsp goto done;
6624 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6626 0ebf8283 2019-07-24 stsp goto done;
6628 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6629 0ebf8283 2019-07-24 stsp worktree);
6631 0ebf8283 2019-07-24 stsp goto done;
6633 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6636 0ebf8283 2019-07-24 stsp goto done;
6638 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6640 0ebf8283 2019-07-24 stsp goto done;
6642 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
6644 0ebf8283 2019-07-24 stsp goto done;
6646 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6647 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6649 0ebf8283 2019-07-24 stsp goto done;
6650 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
6652 0ebf8283 2019-07-24 stsp goto done;
6653 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6654 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
6655 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
6656 0ebf8283 2019-07-24 stsp goto done;
6659 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6660 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6662 0ebf8283 2019-07-24 stsp goto done;
6663 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
6665 0ebf8283 2019-07-24 stsp goto done;
6667 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6669 0ebf8283 2019-07-24 stsp goto done;
6671 0ebf8283 2019-07-24 stsp free(fileindex_path);
6672 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6673 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6674 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6675 0ebf8283 2019-07-24 stsp if (wt_branch)
6676 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
6677 0ebf8283 2019-07-24 stsp if (err) {
6678 0ebf8283 2019-07-24 stsp if (*branch_ref) {
6679 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
6680 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6682 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6683 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6684 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6686 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6687 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6688 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6689 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6691 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
6693 0ebf8283 2019-07-24 stsp return err;
6696 0ebf8283 2019-07-24 stsp const struct got_error *
6697 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
6698 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6700 3e3a69f1 2019-07-25 stsp if (fileindex)
6701 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6702 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
6705 0ebf8283 2019-07-24 stsp const struct got_error *
6706 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
6707 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
6709 0ebf8283 2019-07-24 stsp const struct got_error *err;
6710 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6712 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6714 0ebf8283 2019-07-24 stsp return err;
6716 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6717 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6718 0ebf8283 2019-07-24 stsp return NULL;
6721 0ebf8283 2019-07-24 stsp const struct got_error *
6722 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
6723 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
6724 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6725 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6727 0ebf8283 2019-07-24 stsp const struct got_error *err;
6728 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6729 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6730 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6731 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6732 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6733 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6735 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6736 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6737 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6738 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6740 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6742 3e3a69f1 2019-07-25 stsp return err;
6744 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6746 f032f1f7 2019-08-04 stsp goto done;
6748 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6749 f032f1f7 2019-08-04 stsp &have_staged_files);
6750 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6751 f032f1f7 2019-08-04 stsp goto done;
6752 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6753 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6754 3e3a69f1 2019-07-25 stsp goto done;
6757 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6759 f032f1f7 2019-08-04 stsp goto done;
6761 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6763 0ebf8283 2019-07-24 stsp goto done;
6765 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6767 0ebf8283 2019-07-24 stsp goto done;
6769 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6770 0ebf8283 2019-07-24 stsp worktree);
6772 0ebf8283 2019-07-24 stsp goto done;
6774 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6776 0ebf8283 2019-07-24 stsp goto done;
6778 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6780 0ebf8283 2019-07-24 stsp goto done;
6781 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6783 0ebf8283 2019-07-24 stsp goto done;
6785 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6787 0ebf8283 2019-07-24 stsp goto done;
6788 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6790 0ebf8283 2019-07-24 stsp goto done;
6792 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6794 0ebf8283 2019-07-24 stsp goto done;
6796 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6797 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6798 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6799 0ebf8283 2019-07-24 stsp if (commit_ref)
6800 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6801 0ebf8283 2019-07-24 stsp if (base_commit_ref)
6802 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
6803 0ebf8283 2019-07-24 stsp if (err) {
6804 0ebf8283 2019-07-24 stsp free(*commit_id);
6805 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6806 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6807 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6808 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6809 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6810 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6812 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6813 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6814 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6816 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
6818 0ebf8283 2019-07-24 stsp return err;
6821 0ebf8283 2019-07-24 stsp static const struct got_error *
6822 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6824 0ebf8283 2019-07-24 stsp const struct got_error *err;
6825 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6826 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6828 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6830 0ebf8283 2019-07-24 stsp goto done;
6831 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
6833 0ebf8283 2019-07-24 stsp goto done;
6835 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6836 0ebf8283 2019-07-24 stsp worktree);
6838 0ebf8283 2019-07-24 stsp goto done;
6839 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
6841 0ebf8283 2019-07-24 stsp goto done;
6843 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6845 0ebf8283 2019-07-24 stsp goto done;
6846 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
6848 0ebf8283 2019-07-24 stsp goto done;
6850 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6852 0ebf8283 2019-07-24 stsp goto done;
6853 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
6855 0ebf8283 2019-07-24 stsp goto done;
6857 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6858 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6859 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6860 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6861 0ebf8283 2019-07-24 stsp return err;
6864 0ebf8283 2019-07-24 stsp const struct got_error *
6865 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
6866 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6867 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
6868 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6870 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
6871 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6872 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6873 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
6874 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6876 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6878 0ebf8283 2019-07-24 stsp return err;
6880 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
6881 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
6883 0ebf8283 2019-07-24 stsp goto done;
6885 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
6887 0ebf8283 2019-07-24 stsp goto done;
6889 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6891 0ebf8283 2019-07-24 stsp goto done;
6893 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6894 0ebf8283 2019-07-24 stsp worktree->path_prefix);
6896 0ebf8283 2019-07-24 stsp goto done;
6898 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
6900 0ebf8283 2019-07-24 stsp goto done;
6902 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6904 0ebf8283 2019-07-24 stsp goto done;
6906 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6907 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6908 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6909 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6910 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6911 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6912 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6913 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
6914 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6916 1f1abb7e 2019-08-08 stsp goto sync;
6918 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6919 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
6921 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6922 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
6923 0ebf8283 2019-07-24 stsp err = sync_err;
6925 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
6926 0ebf8283 2019-07-24 stsp free(tree_id);
6927 818c7501 2019-07-11 stsp free(fileindex_path);
6929 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6930 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6931 818c7501 2019-07-11 stsp err = unlockerr;
6932 818c7501 2019-07-11 stsp return err;
6935 0ebf8283 2019-07-24 stsp const struct got_error *
6936 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
6937 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6938 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
6940 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
6941 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
6942 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6943 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
6945 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6947 0ebf8283 2019-07-24 stsp return err;
6949 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
6950 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
6952 0ebf8283 2019-07-24 stsp goto done;
6954 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
6956 0ebf8283 2019-07-24 stsp goto done;
6958 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
6960 0ebf8283 2019-07-24 stsp goto done;
6962 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
6964 0ebf8283 2019-07-24 stsp goto done;
6966 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
6968 a615e0e7 2020-12-16 stsp goto done;
6970 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
6972 a615e0e7 2020-12-16 stsp goto done;
6973 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6974 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6975 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
6976 a615e0e7 2020-12-16 stsp err = sync_err;
6978 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
6979 a615e0e7 2020-12-16 stsp free(fileindex_path);
6980 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
6981 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6982 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6983 0ebf8283 2019-07-24 stsp err = unlockerr;
6984 0ebf8283 2019-07-24 stsp return err;
6987 0ebf8283 2019-07-24 stsp const struct got_error *
6988 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6989 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6991 0ebf8283 2019-07-24 stsp const struct got_error *err;
6992 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6994 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6996 0ebf8283 2019-07-24 stsp return err;
6998 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7000 0ebf8283 2019-07-24 stsp goto done;
7002 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7004 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7005 2822a352 2019-10-15 stsp return err;
7008 2822a352 2019-10-15 stsp const struct got_error *
7009 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7010 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7011 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7012 2822a352 2019-10-15 stsp struct got_repository *repo)
7014 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7015 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7016 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7018 2822a352 2019-10-15 stsp *fileindex = NULL;
7019 2822a352 2019-10-15 stsp *branch_ref = NULL;
7020 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7022 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7024 2822a352 2019-10-15 stsp return err;
7026 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7027 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7028 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7029 2822a352 2019-10-15 stsp "update -b or different branch name required");
7030 2822a352 2019-10-15 stsp goto done;
7033 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7035 2822a352 2019-10-15 stsp goto done;
7037 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
7038 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7039 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7040 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7043 2822a352 2019-10-15 stsp goto done;
7045 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
7047 2822a352 2019-10-15 stsp goto done;
7049 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
7050 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7052 2822a352 2019-10-15 stsp if (err) {
7053 2822a352 2019-10-15 stsp if (*branch_ref) {
7054 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7055 2822a352 2019-10-15 stsp *branch_ref = NULL;
7057 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7058 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7059 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7061 2822a352 2019-10-15 stsp if (*fileindex) {
7062 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7063 2822a352 2019-10-15 stsp *fileindex = NULL;
7065 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7067 0cb83759 2019-08-03 stsp return err;
7070 2822a352 2019-10-15 stsp const struct got_error *
7071 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7072 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7073 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7074 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7075 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7077 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7078 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7079 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7081 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7083 2822a352 2019-10-15 stsp goto done;
7085 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7087 2822a352 2019-10-15 stsp goto done;
7089 2822a352 2019-10-15 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
7090 2822a352 2019-10-15 stsp worktree->path_prefix);
7092 2822a352 2019-10-15 stsp goto done;
7094 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7096 2822a352 2019-10-15 stsp goto done;
7098 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7099 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7101 2822a352 2019-10-15 stsp goto sync;
7103 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7105 2822a352 2019-10-15 stsp goto sync;
7107 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7109 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7110 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7111 2822a352 2019-10-15 stsp err = sync_err;
7114 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7115 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7116 2822a352 2019-10-15 stsp err = unlockerr;
7117 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7119 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7120 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7121 2822a352 2019-10-15 stsp err = unlockerr;
7122 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7124 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7125 2822a352 2019-10-15 stsp free(fileindex_path);
7126 2822a352 2019-10-15 stsp free(tree_id);
7128 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7129 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7130 2822a352 2019-10-15 stsp err = unlockerr;
7131 2822a352 2019-10-15 stsp return err;
7134 2822a352 2019-10-15 stsp const struct got_error *
7135 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7136 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7137 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7139 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7141 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7143 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7145 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7146 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7147 8b692cd0 2019-10-21 stsp err = unlockerr;
7148 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7150 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7151 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7152 8b692cd0 2019-10-21 stsp err = unlockerr;
7153 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7155 8b692cd0 2019-10-21 stsp return err;
7158 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
7159 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
7160 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7161 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7162 2db2652d 2019-08-07 stsp struct got_repository *repo;
7163 2db2652d 2019-08-07 stsp int have_changes;
7166 2db2652d 2019-08-07 stsp const struct got_error *
7167 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
7168 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7169 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7170 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7172 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
7173 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
7174 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
7175 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
7176 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
7177 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
7179 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
7180 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
7181 8b13ce36 2019-08-08 stsp return NULL;
7182 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
7183 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
7185 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7186 735ef5ac 2019-08-03 stsp if (ie == NULL)
7187 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7189 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7190 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7191 735ef5ac 2019-08-03 stsp relpath) == -1)
7192 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
7194 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
7195 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
7196 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7197 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
7200 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
7201 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7202 3aa5969e 2019-08-06 stsp goto done;
7203 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
7204 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
7205 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
7206 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7207 735ef5ac 2019-08-03 stsp goto done;
7210 2db2652d 2019-08-07 stsp a->have_changes = 1;
7212 735ef5ac 2019-08-03 stsp p = in_repo_path;
7213 735ef5ac 2019-08-03 stsp while (p[0] == '/')
7215 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
7216 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
7217 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
7219 735ef5ac 2019-08-03 stsp free(in_repo_path);
7220 dc424a06 2019-08-07 stsp return err;
7223 2db2652d 2019-08-07 stsp struct stage_path_arg {
7224 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7225 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7226 2db2652d 2019-08-07 stsp struct got_repository *repo;
7227 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
7228 2db2652d 2019-08-07 stsp void *status_arg;
7229 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
7230 2db2652d 2019-08-07 stsp void *patch_arg;
7231 7b5dc508 2019-10-28 stsp int staged_something;
7232 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
7235 2db2652d 2019-08-07 stsp static const struct got_error *
7236 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
7237 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7238 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7239 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7241 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
7242 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
7243 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
7244 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
7245 0cb83759 2019-08-03 stsp uint32_t stage;
7246 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
7247 0aeb8099 2020-07-23 stsp struct stat sb;
7249 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
7250 8b13ce36 2019-08-08 stsp return NULL;
7252 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7253 d3e7c587 2019-08-03 stsp if (ie == NULL)
7254 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7256 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7257 2db2652d 2019-08-07 stsp relpath)== -1)
7258 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
7260 0cb83759 2019-08-03 stsp switch (status) {
7261 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
7262 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
7263 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
7264 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
7265 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
7268 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7269 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
7270 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7271 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7272 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
7275 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
7278 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
7279 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
7280 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
7281 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
7282 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
7286 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
7287 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
7290 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7291 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7292 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7293 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
7295 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
7296 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7297 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
7298 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
7299 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
7300 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
7301 35213c7c 2020-07-23 stsp ssize_t target_len;
7302 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
7303 35213c7c 2020-07-23 stsp sizeof(target_path));
7304 35213c7c 2020-07-23 stsp if (target_len == -1) {
7305 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
7306 35213c7c 2020-07-23 stsp ondisk_path);
7309 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
7310 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
7311 35213c7c 2020-07-23 stsp a->worktree->root_path);
7314 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
7315 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
7316 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
7320 35213c7c 2020-07-23 stsp if (is_bad_symlink)
7321 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7322 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
7324 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7325 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
7327 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7328 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
7330 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7331 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7333 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7334 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
7335 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
7337 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
7338 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
7340 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7341 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7342 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
7343 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
7346 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7348 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7349 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7353 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
7354 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7355 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7356 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7358 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7359 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7362 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
7364 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
7365 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7367 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
7368 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
7371 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7375 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
7376 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
7377 dc424a06 2019-08-07 stsp free(path_content);
7378 2db2652d 2019-08-07 stsp free(ondisk_path);
7379 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
7380 0ebf8283 2019-07-24 stsp return err;
7383 0cb83759 2019-08-03 stsp const struct got_error *
7384 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
7385 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
7386 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
7387 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7388 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
7390 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7391 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
7392 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7393 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
7394 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
7395 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
7396 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
7397 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
7399 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7401 0cb83759 2019-08-03 stsp return err;
7403 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
7404 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
7406 735ef5ac 2019-08-03 stsp goto done;
7407 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7409 735ef5ac 2019-08-03 stsp goto done;
7410 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7412 0cb83759 2019-08-03 stsp goto done;
7414 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
7415 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
7416 2db2652d 2019-08-07 stsp oka.worktree = worktree;
7417 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
7418 2db2652d 2019-08-07 stsp oka.repo = repo;
7419 2db2652d 2019-08-07 stsp oka.have_changes = 0;
7420 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7421 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7422 f2a9dc41 2019-12-13 tracey check_stage_ok, &oka, NULL, NULL, 0, 0);
7424 735ef5ac 2019-08-03 stsp goto done;
7426 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
7427 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7428 2db2652d 2019-08-07 stsp goto done;
7431 2db2652d 2019-08-07 stsp spa.worktree = worktree;
7432 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
7433 2db2652d 2019-08-07 stsp spa.repo = repo;
7434 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
7435 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
7436 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
7437 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
7438 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
7439 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
7440 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7441 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7442 f2a9dc41 2019-12-13 tracey stage_path, &spa, NULL, NULL, 0, 0);
7444 2db2652d 2019-08-07 stsp goto done;
7446 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
7447 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7448 7b5dc508 2019-10-28 stsp goto done;
7451 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7452 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
7453 0cb83759 2019-08-03 stsp err = sync_err;
7455 735ef5ac 2019-08-03 stsp if (head_ref)
7456 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
7457 735ef5ac 2019-08-03 stsp free(head_commit_id);
7458 0cb83759 2019-08-03 stsp free(fileindex_path);
7459 0cb83759 2019-08-03 stsp if (fileindex)
7460 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
7461 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7462 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
7463 0cb83759 2019-08-03 stsp err = unlockerr;
7464 0cb83759 2019-08-03 stsp return err;
7467 ad493afc 2019-08-03 stsp struct unstage_path_arg {
7468 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
7469 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
7470 ad493afc 2019-08-03 stsp struct got_repository *repo;
7471 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
7472 ad493afc 2019-08-03 stsp void *progress_arg;
7473 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
7474 2e1f37b0 2019-08-08 stsp void *patch_arg;
7477 2e1f37b0 2019-08-08 stsp static const struct got_error *
7478 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
7479 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
7480 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
7481 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
7482 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
7484 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
7485 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
7486 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7487 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7488 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
7489 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
7490 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
7492 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7493 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7495 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
7497 2e1f37b0 2019-08-08 stsp return err;
7498 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7500 2e1f37b0 2019-08-08 stsp goto done;
7502 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7504 2e1f37b0 2019-08-08 stsp goto done;
7506 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7508 2e1f37b0 2019-08-08 stsp goto done;
7510 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7512 2e1f37b0 2019-08-08 stsp goto done;
7514 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7516 2e1f37b0 2019-08-08 stsp goto done;
7518 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7520 2e1f37b0 2019-08-08 stsp goto done;
7522 fe621944 2020-11-10 stsp err = got_diff_files(&diffreg_result, f1, label1, f2,
7523 64453f7e 2020-11-21 stsp path2, 3, 0, 1, NULL);
7525 2e1f37b0 2019-08-08 stsp goto done;
7527 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
7528 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
7530 2e1f37b0 2019-08-08 stsp goto done;
7531 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
7532 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
7534 2e1f37b0 2019-08-08 stsp goto done;
7536 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
7537 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
7538 2e1f37b0 2019-08-08 stsp goto done;
7540 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
7541 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
7542 2e1f37b0 2019-08-08 stsp goto done;
7544 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
7545 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7546 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
7547 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
7548 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
7549 fe621944 2020-11-10 stsp nchanges++;
7551 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7552 2e1f37b0 2019-08-08 stsp int choice;
7553 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
7554 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
7555 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
7556 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
7558 2e1f37b0 2019-08-08 stsp goto done;
7559 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
7560 2e1f37b0 2019-08-08 stsp have_content = 1;
7562 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
7563 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
7566 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
7567 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7568 f1e81a05 2019-08-10 stsp outfile, rejectfile);
7570 2e1f37b0 2019-08-08 stsp free(label1);
7572 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
7573 2e1f37b0 2019-08-08 stsp if (staged_blob)
7574 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
7575 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
7576 fe621944 2020-11-10 stsp if (free_err && err == NULL)
7577 fe621944 2020-11-10 stsp err = free_err;
7578 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
7579 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
7580 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
7581 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
7582 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
7583 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
7584 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7585 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
7586 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
7587 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
7588 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
7589 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
7590 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
7591 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
7592 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
7593 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7594 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
7595 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
7596 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7598 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
7599 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
7600 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
7601 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7602 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
7603 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
7604 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7606 2e1f37b0 2019-08-08 stsp free(path1);
7607 2e1f37b0 2019-08-08 stsp free(path2);
7608 fda8017d 2020-07-23 stsp return err;
7611 fda8017d 2020-07-23 stsp static const struct got_error *
7612 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
7613 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
7614 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7615 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
7616 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
7617 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7618 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7620 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
7621 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
7622 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
7623 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
7624 fda8017d 2020-07-23 stsp FILE *f = NULL;
7625 fda8017d 2020-07-23 stsp struct stat sb;
7627 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
7628 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
7629 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
7631 fda8017d 2020-07-23 stsp return err;
7633 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
7634 fda8017d 2020-07-23 stsp return NULL;
7636 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
7637 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
7638 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
7640 fda8017d 2020-07-23 stsp goto done;
7643 fda8017d 2020-07-23 stsp f = fopen(path_unstaged_content, "r");
7644 fda8017d 2020-07-23 stsp if (f == NULL) {
7645 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
7646 fda8017d 2020-07-23 stsp path_unstaged_content);
7647 fda8017d 2020-07-23 stsp goto done;
7649 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
7650 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
7651 fda8017d 2020-07-23 stsp goto done;
7653 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
7654 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7655 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
7657 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
7658 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
7659 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
7660 fda8017d 2020-07-23 stsp goto done;
7662 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
7663 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7664 fda8017d 2020-07-23 stsp goto done;
7666 fda8017d 2020-07-23 stsp link_target[r] = '\0';
7667 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
7668 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
7669 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
7670 fda8017d 2020-07-23 stsp progress_arg);
7672 fda8017d 2020-07-23 stsp int local_changes_subsumed;
7673 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
7674 fda8017d 2020-07-23 stsp blob_base, ondisk_path, ie->path,
7675 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
7676 fda8017d 2020-07-23 stsp path_unstaged_content, label_orig, "unstaged",
7677 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
7680 fda8017d 2020-07-23 stsp goto done;
7682 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
7683 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7684 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
7686 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7687 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
7690 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
7691 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
7692 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
7693 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
7694 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
7695 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
7696 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
7697 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
7698 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
7699 fda8017d 2020-07-23 stsp free(path_unstaged_content);
7700 fda8017d 2020-07-23 stsp free(path_new_staged_content);
7701 2e1f37b0 2019-08-08 stsp return err;
7704 ad493afc 2019-08-03 stsp static const struct got_error *
7705 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
7706 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
7707 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7708 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7710 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
7711 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
7712 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
7713 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7714 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
7715 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
7716 ad493afc 2019-08-03 stsp int local_changes_subsumed;
7717 9bc94a15 2019-08-03 stsp struct stat sb;
7719 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
7720 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
7721 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
7722 2e1f37b0 2019-08-08 stsp return NULL;
7724 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7725 ad493afc 2019-08-03 stsp if (ie == NULL)
7726 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7728 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7730 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
7732 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
7733 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
7735 f69721c3 2019-10-21 stsp goto done;
7736 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7737 f69721c3 2019-10-21 stsp id_str) == -1) {
7738 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
7739 f69721c3 2019-10-21 stsp goto done;
7742 ad493afc 2019-08-03 stsp switch (staged_status) {
7743 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
7744 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
7745 ad493afc 2019-08-03 stsp blob_id, 8192);
7748 ad493afc 2019-08-03 stsp /* fall through */
7749 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
7750 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7751 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
7752 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7753 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7754 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7757 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
7760 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
7761 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
7762 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
7763 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
7764 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
7765 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
7768 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
7769 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
7772 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
7773 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
7774 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
7775 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
7776 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
7777 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
7778 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
7779 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
7780 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
7782 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
7783 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7784 36bf999c 2020-07-23 stsp char *staged_target;
7785 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
7786 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
7788 36bf999c 2020-07-23 stsp goto done;
7789 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
7790 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
7791 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
7792 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
7793 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
7794 36bf999c 2020-07-23 stsp free(staged_target);
7796 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
7797 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
7798 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
7799 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
7800 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
7801 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
7802 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
7806 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7809 2ac8aa02 2020-11-09 stsp if (err == NULL) {
7810 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
7811 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
7812 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
7815 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
7816 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7817 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7818 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7819 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7822 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7824 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7825 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7829 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7830 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
7831 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
7832 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
7835 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
7839 ad493afc 2019-08-03 stsp free(ondisk_path);
7840 ad493afc 2019-08-03 stsp if (blob_base)
7841 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
7842 ad493afc 2019-08-03 stsp if (blob_staged)
7843 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
7844 f69721c3 2019-10-21 stsp free(id_str);
7845 f69721c3 2019-10-21 stsp free(label_orig);
7846 ad493afc 2019-08-03 stsp return err;
7849 ad493afc 2019-08-03 stsp const struct got_error *
7850 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
7851 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
7852 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7853 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7854 ad493afc 2019-08-03 stsp struct got_repository *repo)
7856 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7857 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
7858 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7859 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
7860 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
7862 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7864 ad493afc 2019-08-03 stsp return err;
7866 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7868 ad493afc 2019-08-03 stsp goto done;
7870 ad493afc 2019-08-03 stsp upa.worktree = worktree;
7871 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
7872 ad493afc 2019-08-03 stsp upa.repo = repo;
7873 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
7874 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
7875 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
7876 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
7877 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7878 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7879 f2a9dc41 2019-12-13 tracey unstage_path, &upa, NULL, NULL, 0, 0);
7881 ad493afc 2019-08-03 stsp goto done;
7884 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7885 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
7886 ad493afc 2019-08-03 stsp err = sync_err;
7888 ad493afc 2019-08-03 stsp free(fileindex_path);
7889 ad493afc 2019-08-03 stsp if (fileindex)
7890 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
7891 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7892 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
7893 ad493afc 2019-08-03 stsp err = unlockerr;
7894 ad493afc 2019-08-03 stsp return err;
7897 b2118c49 2020-07-28 stsp struct report_file_info_arg {
7898 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
7899 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
7900 b2118c49 2020-07-28 stsp void *info_arg;
7901 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
7902 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
7903 b2118c49 2020-07-28 stsp void *cancel_arg;
7906 b2118c49 2020-07-28 stsp static const struct got_error *
7907 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
7909 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
7910 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
7911 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
7912 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7913 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
7914 b2118c49 2020-07-28 stsp int stage;
7916 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7917 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
7919 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
7920 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7921 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
7924 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
7925 b2118c49 2020-07-28 stsp return NULL;
7927 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_blob(ie)) {
7928 b2118c49 2020-07-28 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7929 b2118c49 2020-07-28 stsp blob_idp = &blob_id;
7931 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
7932 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7933 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
7934 b2118c49 2020-07-28 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7935 b2118c49 2020-07-28 stsp SHA1_DIGEST_LENGTH);
7936 b2118c49 2020-07-28 stsp staged_blob_idp = &staged_blob_id;
7939 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_commit(ie)) {
7940 b2118c49 2020-07-28 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
7941 b2118c49 2020-07-28 stsp commit_idp = &commit_id;
7944 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
7945 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
7948 b2118c49 2020-07-28 stsp const struct got_error *
7949 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
7950 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
7951 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
7952 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7955 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
7956 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
7957 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
7958 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
7960 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
7962 b2118c49 2020-07-28 stsp return err;
7964 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7966 b2118c49 2020-07-28 stsp goto done;
7968 b2118c49 2020-07-28 stsp arg.worktree = worktree;
7969 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
7970 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
7971 b2118c49 2020-07-28 stsp arg.paths = paths;
7972 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
7973 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
7974 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
7977 b2118c49 2020-07-28 stsp free(fileindex_path);
7978 b2118c49 2020-07-28 stsp if (fileindex)
7979 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
7980 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
7981 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
7982 b2118c49 2020-07-28 stsp err = unlockerr;
7983 b2118c49 2020-07-28 stsp return err;