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 fb43ecf1 2019-02-11 stsp if (fclose(tmpfile) != 0 && 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);
434 eaccb85f 2018-12-25 stsp got_repo_close(repo);
435 7ac97322 2018-03-11 stsp free(path_got);
436 056e7441 2018-03-11 stsp free(path_lock);
437 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
438 c442a90d 2019-03-10 stsp free(uuidstr);
439 bd165944 2019-03-10 stsp free(formatstr);
441 6d9d28c3 2018-03-11 stsp if (fd != -1)
443 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
444 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
445 6d9d28c3 2018-03-11 stsp *worktree = NULL;
447 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
449 6d9d28c3 2018-03-11 stsp return err;
452 247140b2 2019-02-05 stsp const struct got_error *
453 247140b2 2019-02-05 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
455 247140b2 2019-02-05 stsp const struct got_error *err = NULL;
456 aedea96d 2020-10-20 stsp char *worktree_path;
458 aedea96d 2020-10-20 stsp worktree_path = strdup(path);
459 aedea96d 2020-10-20 stsp if (worktree_path == NULL)
460 aedea96d 2020-10-20 stsp return got_error_from_errno("strdup");
463 aedea96d 2020-10-20 stsp char *parent_path;
465 aedea96d 2020-10-20 stsp err = open_worktree(worktree, worktree_path);
466 aedea96d 2020-10-20 stsp if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
467 aedea96d 2020-10-20 stsp free(worktree_path);
468 247140b2 2019-02-05 stsp return err;
470 aedea96d 2020-10-20 stsp if (*worktree) {
471 aedea96d 2020-10-20 stsp free(worktree_path);
472 aedea96d 2020-10-20 stsp return NULL;
474 aedea96d 2020-10-20 stsp if (worktree_path[0] == '/' && worktree_path[1] == '\0')
476 aedea96d 2020-10-20 stsp err = got_path_dirname(&parent_path, worktree_path);
478 aedea96d 2020-10-20 stsp if (err->code != GOT_ERR_BAD_PATH) {
479 aedea96d 2020-10-20 stsp free(worktree_path);
480 aedea96d 2020-10-20 stsp return err;
484 aedea96d 2020-10-20 stsp free(worktree_path);
485 aedea96d 2020-10-20 stsp worktree_path = parent_path;
488 aedea96d 2020-10-20 stsp free(worktree_path);
489 247140b2 2019-02-05 stsp return got_error(GOT_ERR_NOT_WORKTREE);
492 3a6ce05a 2019-02-11 stsp const struct got_error *
493 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
495 3a6ce05a 2019-02-11 stsp const struct got_error *err = NULL;
496 cde76477 2018-03-11 stsp free(worktree->repo_path);
497 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
498 eaccb85f 2018-12-25 stsp free(worktree->base_commit_id);
499 36a38700 2019-05-10 stsp free(worktree->head_ref_name);
500 056e7441 2018-03-11 stsp if (worktree->lockfd != -1)
501 3a6ce05a 2019-02-11 stsp if (close(worktree->lockfd) != 0)
502 638f9024 2019-05-13 stsp err = got_error_from_errno2("close",
503 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree));
504 7f11502c 2019-08-28 hiltjo free(worktree->root_path);
505 50b0790e 2020-09-11 stsp free(worktree->gotconfig_path);
506 50b0790e 2020-09-11 stsp got_gotconfig_free(worktree->gotconfig);
507 6d9d28c3 2018-03-11 stsp free(worktree);
508 3a6ce05a 2019-02-11 stsp return err;
511 2fbdb5ae 2018-12-29 stsp const char *
512 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
514 c7f4312f 2019-02-05 stsp return worktree->root_path;
517 c7f4312f 2019-02-05 stsp const char *
518 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
520 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
522 49520a32 2018-12-29 stsp const char *
523 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
525 f609be2e 2018-12-29 stsp return worktree->path_prefix;
528 e5dc7198 2018-12-29 stsp const struct got_error *
529 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
530 e5dc7198 2018-12-29 stsp const char *path_prefix)
532 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
534 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
535 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
536 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
538 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
539 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
540 e5dc7198 2018-12-29 stsp free(absprefix);
541 e5dc7198 2018-12-29 stsp return NULL;
544 bc70eb79 2019-05-09 stsp const char *
545 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
547 36a38700 2019-05-10 stsp return worktree->head_ref_name;
550 024e9686 2019-05-14 stsp const struct got_error *
551 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
552 024e9686 2019-05-14 stsp struct got_reference *head_ref)
554 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
555 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
557 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
558 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
559 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
560 024e9686 2019-05-14 stsp path_got = NULL;
564 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
565 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
566 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
570 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
574 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
575 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
577 024e9686 2019-05-14 stsp free(path_got);
579 024e9686 2019-05-14 stsp free(head_ref_name);
580 024e9686 2019-05-14 stsp return err;
583 b72f483a 2019-02-05 stsp struct got_object_id *
584 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
586 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
589 507dc3bb 2018-12-29 stsp const struct got_error *
590 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
591 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
593 507dc3bb 2018-12-29 stsp const struct got_error *err;
594 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
595 507dc3bb 2018-12-29 stsp char *id_str = NULL;
596 507dc3bb 2018-12-29 stsp char *path_got = NULL;
598 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
599 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
600 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
601 507dc3bb 2018-12-29 stsp path_got = NULL;
605 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
607 507dc3bb 2018-12-29 stsp return err;
609 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
610 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
614 507dc3bb 2018-12-29 stsp /* Record our base commit. */
615 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
618 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
622 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
623 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
624 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
625 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
630 507dc3bb 2018-12-29 stsp got_object_close(obj);
631 507dc3bb 2018-12-29 stsp free(id_str);
632 507dc3bb 2018-12-29 stsp free(path_got);
633 507dc3bb 2018-12-29 stsp return err;
636 50b0790e 2020-09-11 stsp const struct got_gotconfig *
637 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
639 50b0790e 2020-09-11 stsp return worktree->gotconfig;
642 9d31a1d8 2018-03-11 stsp static const struct got_error *
643 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
645 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
646 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
647 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
648 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
649 86c3caaf 2018-03-09 stsp return NULL;
652 21908da4 2019-01-13 stsp static const struct got_error *
653 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
655 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
656 4a1ddfc2 2019-01-12 stsp char *abspath;
658 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
659 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
661 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
662 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
663 ddcd8544 2019-03-11 stsp struct stat sb;
664 ddcd8544 2019-03-11 stsp err = NULL;
665 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
666 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
667 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
668 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
669 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
672 4a1ddfc2 2019-01-12 stsp free(abspath);
673 68c76935 2019-02-19 stsp return err;
676 68c76935 2019-02-19 stsp static const struct got_error *
677 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
679 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
680 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
681 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
682 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
687 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
688 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
689 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
692 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
693 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
694 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
697 68c76935 2019-02-19 stsp if (flen1 == 0) {
698 68c76935 2019-02-19 stsp if (flen2 != 0)
701 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
702 68c76935 2019-02-19 stsp if (flen1 != 0)
705 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
706 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
716 68c76935 2019-02-19 stsp return err;
719 68c76935 2019-02-19 stsp static const struct got_error *
720 68c76935 2019-02-19 stsp check_files_equal(int *same, const char *f1_path, const char *f2_path)
722 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
723 68c76935 2019-02-19 stsp struct stat sb;
724 68c76935 2019-02-19 stsp size_t size1, size2;
725 68c76935 2019-02-19 stsp FILE *f1 = NULL, *f2 = NULL;
729 68c76935 2019-02-19 stsp if (lstat(f1_path, &sb) != 0) {
730 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f1_path);
733 68c76935 2019-02-19 stsp size1 = sb.st_size;
735 68c76935 2019-02-19 stsp if (lstat(f2_path, &sb) != 0) {
736 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f2_path);
739 68c76935 2019-02-19 stsp size2 = sb.st_size;
741 68c76935 2019-02-19 stsp if (size1 != size2) {
743 68c76935 2019-02-19 stsp return NULL;
746 68c76935 2019-02-19 stsp f1 = fopen(f1_path, "r");
747 68c76935 2019-02-19 stsp if (f1 == NULL)
748 60522982 2019-12-15 stsp return got_error_from_errno2("fopen", f1_path);
750 68c76935 2019-02-19 stsp f2 = fopen(f2_path, "r");
751 68c76935 2019-02-19 stsp if (f2 == NULL) {
752 60522982 2019-12-15 stsp err = got_error_from_errno2("fopen", f2_path);
756 68c76935 2019-02-19 stsp err = check_file_contents_equal(same, f1, f2);
758 68c76935 2019-02-19 stsp if (f1 && fclose(f1) != 0 && err == NULL)
759 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
760 68c76935 2019-02-19 stsp if (f2 && fclose(f2) != 0 && err == NULL)
761 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
763 6353ad76 2019-02-08 stsp return err;
767 46b6ee73 2019-06-04 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
768 14c901f1 2019-08-08 stsp * the file at deriv_path acts as the first derived version, and the
769 14c901f1 2019-08-08 stsp * file on disk acts as the second derived version.
771 6353ad76 2019-02-08 stsp static const struct got_error *
772 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
773 46b6ee73 2019-06-04 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
774 14c901f1 2019-08-08 stsp const char *path, uint16_t st_mode, const char *deriv_path,
775 f69721c3 2019-10-21 stsp const char *label_orig, const char *label_deriv,
776 f69721c3 2019-10-21 stsp struct got_repository *repo,
777 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
779 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
780 6353ad76 2019-02-08 stsp int merged_fd = -1;
781 14c901f1 2019-08-08 stsp FILE *f_orig = NULL;
782 14c901f1 2019-08-08 stsp char *blob_orig_path = NULL;
783 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
784 234035bc 2019-06-01 stsp int overlapcnt = 0;
785 af54ae4a 2019-02-19 stsp char *parent;
786 3b9f0f87 2020-07-23 stsp char *symlink_path = NULL;
787 3b9f0f87 2020-07-23 stsp FILE *symlinkf = NULL;
789 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
791 af54ae4a 2019-02-19 stsp parent = dirname(ondisk_path);
792 af54ae4a 2019-02-19 stsp if (parent == NULL)
793 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", ondisk_path);
795 af54ae4a 2019-02-19 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1)
796 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
798 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
802 af54ae4a 2019-02-19 stsp free(base_path);
803 46b6ee73 2019-06-04 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
804 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
805 af54ae4a 2019-02-19 stsp base_path = NULL;
809 46b6ee73 2019-06-04 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
812 46b6ee73 2019-06-04 stsp if (blob_orig) {
813 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
814 46b6ee73 2019-06-04 stsp blob_orig);
819 1430b4e0 2019-03-27 stsp * If the file has no blob, this is an "add vs add" conflict,
820 1430b4e0 2019-03-27 stsp * and we simply use an empty ancestor file to make both files
821 1430b4e0 2019-03-27 stsp * appear in the merged result in their entirety.
826 3b9f0f87 2020-07-23 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
827 3b9f0f87 2020-07-23 stsp * target path into a temporary file and use that file with diff3.
829 3b9f0f87 2020-07-23 stsp if (S_ISLNK(st_mode)) {
830 3b9f0f87 2020-07-23 stsp char target_path[PATH_MAX];
831 3b9f0f87 2020-07-23 stsp ssize_t target_len;
834 3b9f0f87 2020-07-23 stsp free(base_path);
835 3b9f0f87 2020-07-23 stsp if (asprintf(&base_path, "%s/got-symlink-merge",
836 3b9f0f87 2020-07-23 stsp parent) == -1) {
837 3b9f0f87 2020-07-23 stsp err = got_error_from_errno("asprintf");
838 3b9f0f87 2020-07-23 stsp base_path = NULL;
841 3b9f0f87 2020-07-23 stsp err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
844 3b9f0f87 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
845 3b9f0f87 2020-07-23 stsp sizeof(target_path));
846 3b9f0f87 2020-07-23 stsp if (target_len == -1) {
847 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
850 3b9f0f87 2020-07-23 stsp n = fwrite(target_path, 1, target_len, symlinkf);
851 3b9f0f87 2020-07-23 stsp if (n != target_len) {
852 3b9f0f87 2020-07-23 stsp err = got_ferror(symlinkf, GOT_ERR_IO);
855 3b9f0f87 2020-07-23 stsp if (fflush(symlinkf) == EOF) {
856 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("fflush", symlink_path);
861 14c901f1 2019-08-08 stsp err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
862 3b9f0f87 2020-07-23 stsp blob_orig_path, symlink_path ? symlink_path : ondisk_path,
863 3b9f0f87 2020-07-23 stsp label_deriv, label_orig, NULL);
867 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
868 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
872 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
873 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
877 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
878 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
879 14c901f1 2019-08-08 stsp err = check_files_equal(local_changes_subsumed, deriv_path,
880 68c76935 2019-02-19 stsp merged_path);
885 2ad902c0 2019-12-15 stsp if (fchmod(merged_fd, st_mode) != 0) {
886 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
890 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
891 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
892 230a42bd 2019-05-11 jcs ondisk_path);
897 fdcb7daf 2019-12-15 stsp if (merged_path)
898 fdcb7daf 2019-12-15 stsp unlink(merged_path);
900 3b9f0f87 2020-07-23 stsp if (symlink_path) {
901 3b9f0f87 2020-07-23 stsp if (unlink(symlink_path) == -1 && err == NULL)
902 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("unlink", symlink_path);
904 3b9f0f87 2020-07-23 stsp if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
905 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("fclose", symlink_path);
906 3b9f0f87 2020-07-23 stsp free(symlink_path);
907 3a6ce05a 2019-02-11 stsp if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
908 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
909 46b6ee73 2019-06-04 stsp if (f_orig && fclose(f_orig) != 0 && err == NULL)
910 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
911 6353ad76 2019-02-08 stsp free(merged_path);
912 af54ae4a 2019-02-19 stsp free(base_path);
913 46b6ee73 2019-06-04 stsp if (blob_orig_path) {
914 46b6ee73 2019-06-04 stsp unlink(blob_orig_path);
915 46b6ee73 2019-06-04 stsp free(blob_orig_path);
917 af57b12a 2020-07-23 stsp return err;
920 af57b12a 2020-07-23 stsp static const struct got_error *
921 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
922 af57b12a 2020-07-23 stsp size_t target_len)
924 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
925 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
926 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
927 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
928 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
929 af57b12a 2020-07-23 stsp ondisk_path);
930 af57b12a 2020-07-23 stsp return NULL;
934 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
935 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
936 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
937 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
939 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
940 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
942 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
943 993e2a1b 2020-07-23 stsp * has deleted this symlink.
945 11cc08c1 2020-07-23 stsp static const struct got_error *
946 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
947 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
948 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
950 11cc08c1 2020-07-23 stsp const struct got_error *err;
951 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
952 11cc08c1 2020-07-23 stsp FILE *f = NULL;
954 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
956 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
958 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
959 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
960 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
964 11cc08c1 2020-07-23 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict");
968 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
969 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
970 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
971 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
972 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
973 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
974 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
975 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
976 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
977 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
981 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
982 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
985 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
986 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
989 11cc08c1 2020-07-23 stsp if (chmod(ondisk_path, GOT_DEFAULT_FILE_MODE) == -1) {
990 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("chmod", ondisk_path);
994 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
995 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
996 11cc08c1 2020-07-23 stsp free(path);
997 11cc08c1 2020-07-23 stsp free(id_str);
998 11cc08c1 2020-07-23 stsp free(label_deriv);
999 11cc08c1 2020-07-23 stsp return err;
1002 d219f183 2020-07-23 stsp /* forward declaration */
1003 d219f183 2020-07-23 stsp static const struct got_error *
1004 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
1005 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
1006 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
1007 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
1010 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
1011 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
1012 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
1013 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
1015 af57b12a 2020-07-23 stsp static const struct got_error *
1016 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
1017 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
1018 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
1019 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1020 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1022 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
1023 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
1024 af57b12a 2020-07-23 stsp struct stat sb;
1025 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
1026 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
1027 11cc08c1 2020-07-23 stsp int have_local_change = 0;
1028 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
1030 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
1031 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
1033 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
1034 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
1035 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
1036 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
1037 af57b12a 2020-07-23 stsp ondisk_path);
1038 af57b12a 2020-07-23 stsp goto done;
1040 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
1042 526a746f 2020-07-23 stsp if (blob_orig) {
1043 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1045 526a746f 2020-07-23 stsp goto done;
1048 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
1049 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
1050 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1051 11cc08c1 2020-07-23 stsp have_local_change = 1;
1053 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
1054 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
1055 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
1056 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1057 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
1059 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
1060 11cc08c1 2020-07-23 stsp if (ancestor_target) {
1061 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
1062 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1064 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
1065 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1066 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
1067 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1070 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
1071 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1072 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
1073 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
1075 11cc08c1 2020-07-23 stsp goto done;
1076 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1079 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
1080 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
1081 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
1082 af57b12a 2020-07-23 stsp strlen(deriv_target));
1084 af57b12a 2020-07-23 stsp goto done;
1085 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1086 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
1087 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
1088 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1089 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
1090 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1093 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1094 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
1095 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
1097 ea7786be 2020-07-23 stsp goto done;
1098 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1104 af57b12a 2020-07-23 stsp free(ancestor_target);
1105 14c901f1 2019-08-08 stsp return err;
1109 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
1110 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
1111 14c901f1 2019-08-08 stsp * acts as the second derived version.
1113 14c901f1 2019-08-08 stsp static const struct got_error *
1114 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1115 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
1116 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
1117 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
1118 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1119 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1121 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
1122 14c901f1 2019-08-08 stsp FILE *f_deriv = NULL;
1123 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1124 14c901f1 2019-08-08 stsp char *label_deriv = NULL, *parent;
1126 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
1128 14c901f1 2019-08-08 stsp parent = dirname(ondisk_path);
1129 14c901f1 2019-08-08 stsp if (parent == NULL)
1130 14c901f1 2019-08-08 stsp return got_error_from_errno2("dirname", ondisk_path);
1132 14c901f1 2019-08-08 stsp free(base_path);
1133 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1134 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1135 14c901f1 2019-08-08 stsp base_path = NULL;
1136 14c901f1 2019-08-08 stsp goto done;
1139 14c901f1 2019-08-08 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1141 14c901f1 2019-08-08 stsp goto done;
1142 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1143 14c901f1 2019-08-08 stsp blob_deriv);
1145 14c901f1 2019-08-08 stsp goto done;
1147 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1149 14c901f1 2019-08-08 stsp goto done;
1150 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1151 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1152 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1153 14c901f1 2019-08-08 stsp goto done;
1156 14c901f1 2019-08-08 stsp err = merge_file(local_changes_subsumed, worktree, blob_orig,
1157 f69721c3 2019-10-21 stsp ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1158 f69721c3 2019-10-21 stsp label_deriv, repo, progress_cb, progress_arg);
1160 14c901f1 2019-08-08 stsp if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1161 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1162 14c901f1 2019-08-08 stsp free(base_path);
1163 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1164 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1165 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1167 6353ad76 2019-02-08 stsp free(id_str);
1168 818c7501 2019-07-11 stsp free(label_deriv);
1169 4a1ddfc2 2019-01-12 stsp return err;
1172 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1173 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1174 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1175 65b05cec 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_object_id *blob_id)
1177 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1178 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1180 65b05cec 2020-07-23 stsp *new_iep = NULL;
1182 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1184 054041d0 2020-06-24 stsp return err;
1186 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(new_ie, ondisk_path,
1187 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1189 054041d0 2020-06-24 stsp goto done;
1191 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1194 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1196 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1197 13d9040b 2019-03-27 stsp return err;
1200 1ebedb77 2019-10-19 stsp static mode_t
1201 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1203 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1205 1ebedb77 2019-10-19 stsp if (executable) {
1206 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1207 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1208 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1209 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1210 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1211 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1214 1ebedb77 2019-10-19 stsp return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1217 8ba819a3 2020-07-23 stsp /* forward declaration */
1218 13d9040b 2019-03-27 stsp static const struct got_error *
1219 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1220 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1221 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1222 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1223 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1224 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1227 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1228 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1230 8ba819a3 2020-07-23 stsp static const struct got_error *
1231 5a1fbc73 2020-07-23 stsp replace_existing_symlink(const char *ondisk_path, const char *target_path,
1232 5a1fbc73 2020-07-23 stsp size_t target_len)
1234 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1235 5a1fbc73 2020-07-23 stsp ssize_t elen;
1236 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1240 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1241 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1242 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1243 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1244 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1245 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1247 5a1fbc73 2020-07-23 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1248 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1249 5a1fbc73 2020-07-23 stsp if (errno != ELOOP)
1250 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1252 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1253 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1254 5a1fbc73 2020-07-23 stsp if (elen == -1)
1255 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1257 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1258 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1259 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1262 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1263 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1264 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1265 5a1fbc73 2020-07-23 stsp return err;
1268 3c1ec782 2020-07-23 stsp static const struct got_error *
1269 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1270 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1272 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1273 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1274 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1276 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1278 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1279 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1280 3c1ec782 2020-07-23 stsp return NULL;
1284 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1285 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1286 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1287 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1288 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1290 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1291 3c1ec782 2020-07-23 stsp char *abspath;
1292 3c1ec782 2020-07-23 stsp char *parent = dirname(ondisk_path);
1293 3c1ec782 2020-07-23 stsp if (parent == NULL)
1294 3c1ec782 2020-07-23 stsp return got_error_from_errno2("dirname", ondisk_path);
1295 3c1ec782 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1)
1296 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1297 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1298 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1299 3c1ec782 2020-07-23 stsp free(abspath);
1300 3c1ec782 2020-07-23 stsp return err;
1302 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1303 3c1ec782 2020-07-23 stsp free(abspath);
1305 3c1ec782 2020-07-23 stsp return err;
1307 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1309 3c1ec782 2020-07-23 stsp return err;
1312 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1313 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1314 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1315 3c1ec782 2020-07-23 stsp return NULL;
1318 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1319 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1320 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1321 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1322 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1323 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1325 3c1ec782 2020-07-23 stsp free(path_got);
1326 3c1ec782 2020-07-23 stsp return NULL;
1329 5a1fbc73 2020-07-23 stsp static const struct got_error *
1330 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1331 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1332 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1333 c90c8ce3 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1334 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1336 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1337 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1338 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1339 906c123b 2020-07-23 stsp char *path_got = NULL;
1340 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1341 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1343 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1346 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1347 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1348 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1349 8ba819a3 2020-07-23 stsp * in the blob object.
1352 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1353 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1354 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1355 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1356 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1357 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1358 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1359 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1360 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1361 3b9f0f87 2020-07-23 stsp progress_arg);
1363 8ba819a3 2020-07-23 stsp if (len > 0) {
1364 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1365 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1366 8ba819a3 2020-07-23 stsp len - hdrlen);
1367 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1368 8ba819a3 2020-07-23 stsp hdrlen = 0;
1370 8ba819a3 2020-07-23 stsp } while (len != 0);
1371 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1373 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1374 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1376 3c1ec782 2020-07-23 stsp return err;
1378 3c1ec782 2020-07-23 stsp if (*is_bad_symlink) {
1379 906c123b 2020-07-23 stsp /* install as a regular file */
1380 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1381 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1382 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1383 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1384 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1385 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1386 906c123b 2020-07-23 stsp goto done;
1389 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1390 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1391 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1392 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1393 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1394 c90c8ce3 2020-07-23 stsp goto done;
1396 5a1fbc73 2020-07-23 stsp err = replace_existing_symlink(ondisk_path,
1397 5a1fbc73 2020-07-23 stsp target_path, target_len);
1399 f35fa46a 2020-07-23 stsp goto done;
1400 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1401 5a1fbc73 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1402 6e1eade5 2020-07-23 stsp reverting_versioned_file ?
1403 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1406 5a1fbc73 2020-07-23 stsp goto done; /* Nothing else to do. */
1409 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1410 f35fa46a 2020-07-23 stsp char *parent = dirname(ondisk_path);
1411 f35fa46a 2020-07-23 stsp if (parent == NULL) {
1412 f35fa46a 2020-07-23 stsp err = got_error_from_errno2("dirname",
1413 f35fa46a 2020-07-23 stsp ondisk_path);
1414 f35fa46a 2020-07-23 stsp goto done;
1416 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1418 f35fa46a 2020-07-23 stsp goto done;
1420 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1421 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1423 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1424 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1425 f35fa46a 2020-07-23 stsp goto done;
1429 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1430 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1431 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1432 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1433 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1434 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1435 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1436 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1437 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1438 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1439 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1440 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1441 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1443 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1444 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1446 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1447 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1448 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1450 906c123b 2020-07-23 stsp free(path_got);
1451 8ba819a3 2020-07-23 stsp return err;
1454 8ba819a3 2020-07-23 stsp static const struct got_error *
1455 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1456 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1457 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1458 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1459 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1460 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1462 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1463 507dc3bb 2018-12-29 stsp int fd = -1;
1464 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1465 507dc3bb 2018-12-29 stsp int update = 0;
1466 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1468 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1469 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
1470 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1471 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1472 21908da4 2019-01-13 stsp char *parent = dirname(path);
1473 21908da4 2019-01-13 stsp if (parent == NULL)
1474 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
1475 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1477 21908da4 2019-01-13 stsp return err;
1478 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1479 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1480 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
1481 21908da4 2019-01-13 stsp if (fd == -1)
1482 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1483 230a42bd 2019-05-11 jcs ondisk_path);
1484 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1485 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1486 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1487 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1488 3b9f0f87 2020-07-23 stsp goto done;
1490 bd6aa359 2020-07-23 stsp if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1491 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1492 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1493 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1494 507dc3bb 2018-12-29 stsp goto done;
1496 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1497 507dc3bb 2018-12-29 stsp ondisk_path);
1499 507dc3bb 2018-12-29 stsp goto done;
1500 507dc3bb 2018-12-29 stsp update = 1;
1503 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1506 bd6aa359 2020-07-23 stsp if (progress_cb) {
1507 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1508 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1510 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1511 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1514 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1515 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1517 bd6aa359 2020-07-23 stsp goto done;
1520 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1522 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1523 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1526 9d31a1d8 2018-03-11 stsp if (len > 0) {
1527 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1528 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1529 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1530 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1531 b87c6f83 2018-12-24 stsp goto done;
1532 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1533 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1534 b87c6f83 2018-12-24 stsp goto done;
1536 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1538 9d31a1d8 2018-03-11 stsp } while (len != 0);
1540 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1541 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1542 816dc654 2019-02-16 stsp goto done;
1545 507dc3bb 2018-12-29 stsp if (update) {
1546 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1547 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1548 230a42bd 2019-05-11 jcs ondisk_path);
1549 2a57020b 2019-02-20 stsp unlink(tmppath);
1550 68ed9ba5 2019-02-10 stsp goto done;
1554 1ebedb77 2019-10-19 stsp if (chmod(ondisk_path,
1555 1ebedb77 2019-10-19 stsp get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1556 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("chmod", ondisk_path);
1557 1ebedb77 2019-10-19 stsp goto done;
1561 3a6ce05a 2019-02-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
1562 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1563 507dc3bb 2018-12-29 stsp free(tmppath);
1564 6353ad76 2019-02-08 stsp return err;
1567 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1568 6353ad76 2019-02-08 stsp static const struct got_error *
1569 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1571 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1572 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1573 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1574 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1575 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1577 7154f6ce 2019-03-27 stsp int i = 0;
1578 7154f6ce 2019-03-27 stsp char *line;
1579 7154f6ce 2019-03-27 stsp size_t len;
1580 7154f6ce 2019-03-27 stsp const char delim[3] = {'\0', '\0', '\0'};
1582 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1583 7154f6ce 2019-03-27 stsp line = fparseln(f, &len, NULL, delim, 0);
1584 7154f6ce 2019-03-27 stsp if (line == NULL) {
1585 7154f6ce 2019-03-27 stsp if (feof(f))
1587 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1591 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1592 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1594 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1600 7154f6ce 2019-03-27 stsp return err;
1603 e2b1e152 2019-07-13 stsp static int
1604 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1606 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1607 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1610 1ebedb77 2019-10-19 stsp static int
1611 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1613 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1614 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1615 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1616 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1617 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1618 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1621 c363b2c1 2019-08-03 stsp static unsigned char
1622 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1624 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1625 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1626 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1627 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1628 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1629 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1630 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1632 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1636 a919d5c4 2020-07-23 stsp static const struct got_error *
1637 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1638 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1639 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1641 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1642 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1643 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1644 a919d5c4 2020-07-23 stsp ssize_t elen;
1645 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1646 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1647 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1649 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1651 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1653 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1655 a919d5c4 2020-07-23 stsp return err;
1656 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1658 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1659 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1661 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1663 a919d5c4 2020-07-23 stsp if (len > 0) {
1664 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1665 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1666 a919d5c4 2020-07-23 stsp len - hdrlen);
1667 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1668 a919d5c4 2020-07-23 stsp hdrlen = 0;
1670 a919d5c4 2020-07-23 stsp } while (len != 0);
1671 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1673 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1674 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1675 a919d5c4 2020-07-23 stsp if (elen == -1)
1676 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1678 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1679 a919d5c4 2020-07-23 stsp if (elen == -1)
1680 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1683 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1684 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1686 a919d5c4 2020-07-23 stsp return NULL;
1689 7154f6ce 2019-03-27 stsp static const struct got_error *
1690 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1691 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1692 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1694 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1695 6353ad76 2019-02-08 stsp struct got_object_id id;
1696 6353ad76 2019-02-08 stsp size_t hdrlen;
1697 1338848f 2019-12-13 stsp int fd = -1;
1698 6353ad76 2019-02-08 stsp FILE *f = NULL;
1699 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1700 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1701 6353ad76 2019-02-08 stsp size_t flen, blen;
1702 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1704 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1707 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1708 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1709 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1711 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1712 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1713 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1714 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1715 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1717 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1718 882ef1b9 2019-12-13 stsp goto done;
1720 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1721 3d35a492 2019-12-13 stsp goto done;
1724 7f91a133 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1725 a919d5c4 2020-07-23 stsp if (fd == -1 && errno != ENOENT && errno != ELOOP)
1726 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1727 a919d5c4 2020-07-23 stsp else if (fd == -1 && errno == ELOOP) {
1728 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1729 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1730 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1731 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1732 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1733 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1735 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1736 3d35a492 2019-12-13 stsp goto done;
1738 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1739 1338848f 2019-12-13 stsp goto done;
1743 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1744 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1745 1338848f 2019-12-13 stsp goto done;
1748 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1749 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1750 1338848f 2019-12-13 stsp goto done;
1751 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1752 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1753 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1754 1338848f 2019-12-13 stsp goto done;
1757 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1758 f179e66d 2020-07-23 stsp goto done;
1760 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1761 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1762 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1763 1338848f 2019-12-13 stsp goto done;
1766 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1767 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1768 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1770 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1772 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1774 1338848f 2019-12-13 stsp goto done;
1776 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1777 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1778 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1779 a919d5c4 2020-07-23 stsp goto done;
1782 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1783 3d35a492 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1784 ab0d4361 2019-12-13 stsp if (fd == -1) {
1785 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1786 ab0d4361 2019-12-13 stsp goto done;
1790 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1791 6353ad76 2019-02-08 stsp if (f == NULL) {
1792 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1793 6353ad76 2019-02-08 stsp goto done;
1796 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1798 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1799 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1801 7154f6ce 2019-03-27 stsp goto done;
1802 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1803 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1804 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1805 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1806 7154f6ce 2019-03-27 stsp goto done;
1808 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1809 6353ad76 2019-02-08 stsp if (flen != 0)
1810 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1812 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1813 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1814 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1816 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1817 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1818 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1819 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1823 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1826 6353ad76 2019-02-08 stsp hdrlen = 0;
1829 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1830 7154f6ce 2019-03-27 stsp rewind(f);
1831 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1832 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1833 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1836 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1837 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1838 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1839 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1840 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1841 9d31a1d8 2018-03-11 stsp return err;
1845 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1846 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1848 e2b1e152 2019-07-13 stsp static const struct got_error *
1849 e2b1e152 2019-07-13 stsp sync_timestamps(char *ondisk_path, unsigned char status,
1850 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1852 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1853 e2b1e152 2019-07-13 stsp return got_fileindex_entry_update(ie, ondisk_path,
1854 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1856 e2b1e152 2019-07-13 stsp return NULL;
1859 9d31a1d8 2018-03-11 stsp static const struct got_error *
1860 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1861 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1862 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1863 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1864 0584f854 2019-04-06 stsp void *progress_arg)
1866 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1867 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1868 6353ad76 2019-02-08 stsp char *ondisk_path;
1869 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1870 b8f41171 2019-02-10 stsp struct stat sb;
1872 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1873 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1876 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1877 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1878 a76c42e6 2019-08-03 stsp goto done;
1880 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1883 abb4604f 2019-07-27 stsp goto done;
1884 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1885 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1887 abb4604f 2019-07-27 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1888 c90c8ce3 2020-07-23 stsp status = GOT_STATUS_UNVERSIONED;
1891 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1892 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1893 b8f41171 2019-02-10 stsp goto done;
1895 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1896 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1898 5036ab18 2020-04-18 stsp goto done;
1901 523b8417 2019-10-19 stsp if (ie && status != GOT_STATUS_MISSING &&
1902 523b8417 2019-10-19 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1903 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1904 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1905 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1906 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1908 e2b1e152 2019-07-13 stsp goto done;
1909 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1911 6353ad76 2019-02-08 stsp goto done;
1913 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1914 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1915 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1916 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1917 b8f41171 2019-02-10 stsp goto done;
1921 56e0773d 2019-11-28 stsp err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1923 6353ad76 2019-02-08 stsp goto done;
1925 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1926 234035bc 2019-06-01 stsp int update_timestamps;
1927 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1928 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1929 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1930 234035bc 2019-06-01 stsp struct got_object_id id2;
1931 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1932 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1934 f69721c3 2019-10-21 stsp goto done;
1936 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
1937 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
1938 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1939 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
1940 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
1941 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
1942 f69721c3 2019-10-21 stsp goto done;
1944 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
1945 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
1946 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
1947 234035bc 2019-06-01 stsp goto done;
1950 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1951 36bf999c 2020-07-23 stsp char *link_target;
1952 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
1954 36bf999c 2020-07-23 stsp goto done;
1955 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
1956 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
1957 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
1958 36bf999c 2020-07-23 stsp free(link_target);
1960 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1961 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
1962 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
1963 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
1965 f69721c3 2019-10-21 stsp free(label_orig);
1966 234035bc 2019-06-01 stsp if (blob2)
1967 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1969 909d120e 2019-09-22 stsp goto done;
1971 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1972 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1973 234035bc 2019-06-01 stsp * unmodified files again.
1975 234035bc 2019-06-01 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1976 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1977 234035bc 2019-06-01 stsp update_timestamps);
1978 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
1979 1ebedb77 2019-10-19 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1980 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1981 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1982 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1984 1ee397ad 2019-07-12 stsp goto done;
1985 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1986 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1988 13d9040b 2019-03-27 stsp goto done;
1990 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
1991 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
1992 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
1993 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
1994 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
1995 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
1996 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
1998 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1999 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2000 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2001 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2002 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2005 13d9040b 2019-03-27 stsp goto done;
2008 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie, ondisk_path,
2009 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 1);
2011 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2012 054041d0 2020-06-24 stsp worktree->base_commit_id, ondisk_path, path,
2013 054041d0 2020-06-24 stsp &blob->id);
2016 13d9040b 2019-03-27 stsp goto done;
2018 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2019 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2020 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2023 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2025 6353ad76 2019-02-08 stsp free(ondisk_path);
2026 8da9e5f4 2019-01-12 stsp return err;
2029 90285c3b 2019-01-08 stsp static const struct got_error *
2030 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2032 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2033 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
2035 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2036 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2038 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2039 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2040 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2042 90285c3b 2019-01-08 stsp char *parent = dirname(ondisk_path);
2043 7a9df742 2019-01-08 stsp while (parent && strcmp(parent, root_path) != 0) {
2044 26c4ac4d 2019-01-08 stsp if (rmdir(parent) == -1) {
2045 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2046 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2050 90285c3b 2019-01-08 stsp parent = dirname(parent);
2053 90285c3b 2019-01-08 stsp free(ondisk_path);
2054 90285c3b 2019-01-08 stsp return err;
2057 708d8e67 2019-03-27 stsp static const struct got_error *
2058 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2059 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2060 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2062 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2063 708d8e67 2019-03-27 stsp unsigned char status;
2064 708d8e67 2019-03-27 stsp struct stat sb;
2065 708d8e67 2019-03-27 stsp char *ondisk_path;
2067 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2068 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2070 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2072 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2074 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2076 5a58a424 2020-06-23 stsp goto done;
2078 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2079 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2080 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2081 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2082 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2083 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2084 993e2a1b 2020-07-23 stsp goto done;
2086 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2087 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2088 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2089 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2091 993e2a1b 2020-07-23 stsp goto done;
2092 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2093 993e2a1b 2020-07-23 stsp ie->path);
2094 993e2a1b 2020-07-23 stsp goto done;
2097 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2098 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2099 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2101 5a58a424 2020-06-23 stsp goto done;
2103 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2104 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2106 fc6346c4 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2109 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2111 5a58a424 2020-06-23 stsp goto done;
2112 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2113 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2115 5a58a424 2020-06-23 stsp goto done;
2117 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2120 5a58a424 2020-06-23 stsp free(ondisk_path);
2121 fc6346c4 2019-03-27 stsp return err;
2124 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2125 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2126 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2127 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2128 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2129 8da9e5f4 2019-01-12 stsp void *progress_arg;
2130 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2131 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2134 512f0d0e 2019-01-02 stsp static const struct got_error *
2135 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2136 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2138 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2140 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2141 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2143 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2144 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2147 8da9e5f4 2019-01-12 stsp static const struct got_error *
2148 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2150 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2152 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2153 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2155 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2156 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2159 9d31a1d8 2018-03-11 stsp static const struct got_error *
2160 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2162 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2163 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2164 8da9e5f4 2019-01-12 stsp char *path;
2166 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2167 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2169 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2170 63c5ca5d 2019-08-24 stsp return NULL;
2172 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2173 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2175 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2177 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2178 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2180 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2181 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2183 8da9e5f4 2019-01-12 stsp free(path);
2184 0cd1c46a 2019-03-11 stsp return err;
2187 b2118c49 2020-07-28 stsp const struct got_error *
2188 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2190 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2192 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2193 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2194 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2195 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2198 b2118c49 2020-07-28 stsp return NULL;
2201 818c7501 2019-07-11 stsp static const struct got_error *
2202 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2204 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2205 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2207 517bab73 2019-03-11 stsp *refname = NULL;
2209 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2211 b2118c49 2020-07-28 stsp return err;
2213 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2214 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2215 0647c563 2019-03-11 stsp *refname = NULL;
2217 517bab73 2019-03-11 stsp free(uuidstr);
2218 517bab73 2019-03-11 stsp return err;
2221 818c7501 2019-07-11 stsp const struct got_error *
2222 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2224 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2227 818c7501 2019-07-11 stsp static const struct got_error *
2228 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2230 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2231 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2234 818c7501 2019-07-11 stsp static const struct got_error *
2235 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2237 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2240 818c7501 2019-07-11 stsp static const struct got_error *
2241 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2243 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2244 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2247 818c7501 2019-07-11 stsp static const struct got_error *
2248 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2250 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2251 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2254 0ebf8283 2019-07-24 stsp static const struct got_error *
2255 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2257 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2258 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2261 0ebf8283 2019-07-24 stsp static const struct got_error *
2262 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2264 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2265 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2268 0ebf8283 2019-07-24 stsp static const struct got_error *
2269 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2271 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2272 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2275 0ebf8283 2019-07-24 stsp static const struct got_error *
2276 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2278 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2279 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2282 0ebf8283 2019-07-24 stsp const struct got_error *
2283 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2284 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2286 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2287 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2288 0ebf8283 2019-07-24 stsp *path = NULL;
2289 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2291 0ebf8283 2019-07-24 stsp return NULL;
2295 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2296 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2298 517bab73 2019-03-11 stsp static const struct got_error *
2299 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2301 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2302 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2303 517bab73 2019-03-11 stsp char *refname;
2305 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2307 517bab73 2019-03-11 stsp return err;
2309 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2311 0cd1c46a 2019-03-11 stsp goto done;
2313 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2315 0cd1c46a 2019-03-11 stsp free(refname);
2317 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2318 3e3a69f1 2019-07-25 stsp return err;
2321 3e3a69f1 2019-07-25 stsp static const struct got_error *
2322 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2324 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2326 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2327 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2328 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2329 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2331 9d31a1d8 2018-03-11 stsp return err;
2335 ebf99748 2019-05-09 stsp static const struct got_error *
2336 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2337 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2339 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2340 ebf99748 2019-05-09 stsp FILE *index = NULL;
2342 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2343 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2344 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2345 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2347 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2349 ebf99748 2019-05-09 stsp goto done;
2351 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
2352 ebf99748 2019-05-09 stsp if (index == NULL) {
2353 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2354 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2356 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2357 ebf99748 2019-05-09 stsp if (fclose(index) != 0 && err == NULL)
2358 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2361 ebf99748 2019-05-09 stsp if (err) {
2362 ebf99748 2019-05-09 stsp free(*fileindex_path);
2363 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2364 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2365 ebf99748 2019-05-09 stsp *fileindex = NULL;
2367 ebf99748 2019-05-09 stsp return err;
2370 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2371 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2372 c932eeeb 2019-05-22 stsp const char *path;
2373 c932eeeb 2019-05-22 stsp size_t path_len;
2374 c932eeeb 2019-05-22 stsp const char *entry_name;
2375 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2376 a484d721 2019-06-10 stsp void *progress_arg;
2379 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2380 c932eeeb 2019-05-22 stsp static const struct got_error *
2381 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2383 1ee397ad 2019-07-12 stsp const struct got_error *err;
2384 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2386 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2387 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2388 c932eeeb 2019-05-22 stsp return NULL;
2389 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2390 102ff934 2019-06-11 stsp return NULL;
2392 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2393 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2394 c932eeeb 2019-05-22 stsp return NULL;
2396 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2397 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2398 edd02c5e 2019-07-11 stsp ie->path);
2400 1ee397ad 2019-07-12 stsp return err;
2402 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2403 c932eeeb 2019-05-22 stsp return NULL;
2406 9c6338c4 2019-06-02 stsp static const struct got_error *
2407 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2409 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2410 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2411 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2412 867630bb 2020-01-17 stsp struct timespec timeout;
2414 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2415 9c6338c4 2019-06-02 stsp fileindex_path);
2417 9c6338c4 2019-06-02 stsp goto done;
2419 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2421 9c6338c4 2019-06-02 stsp goto done;
2423 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2424 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2425 9c6338c4 2019-06-02 stsp fileindex_path);
2426 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2430 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2431 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2432 867630bb 2020-01-17 stsp * was recorded in the file index.
2434 867630bb 2020-01-17 stsp timeout.tv_sec = 0;
2435 867630bb 2020-01-17 stsp timeout.tv_nsec = 1;
2436 867630bb 2020-01-17 stsp nanosleep(&timeout, NULL);
2438 9c6338c4 2019-06-02 stsp if (new_index)
2439 9c6338c4 2019-06-02 stsp fclose(new_index);
2440 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2441 9c6338c4 2019-06-02 stsp return err;
2444 6ced4176 2019-07-12 stsp static const struct got_error *
2445 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2446 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2447 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
2449 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2450 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2451 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2452 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2454 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2455 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2456 6ced4176 2019-07-12 stsp *tree_id = NULL;
2458 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2459 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2460 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2461 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2462 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2463 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2464 6ced4176 2019-07-12 stsp goto done;
2466 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2467 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
2469 6ced4176 2019-07-12 stsp goto done;
2470 6ced4176 2019-07-12 stsp return NULL;
2473 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2475 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2476 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2477 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2478 6ced4176 2019-07-12 stsp goto done;
2481 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2482 6ced4176 2019-07-12 stsp in_repo_path);
2484 6ced4176 2019-07-12 stsp goto done;
2486 6ced4176 2019-07-12 stsp free(in_repo_path);
2487 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2489 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2491 6ced4176 2019-07-12 stsp goto done;
2493 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2494 6ced4176 2019-07-12 stsp /* Check out a single file. */
2495 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2496 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2497 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2498 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2499 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2500 6ced4176 2019-07-12 stsp goto done;
2502 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2503 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2504 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2505 6ced4176 2019-07-12 stsp goto done;
2508 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2509 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2511 6ced4176 2019-07-12 stsp return err;
2512 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2513 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2514 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2515 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2516 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, in_repo_path);
2522 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2523 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2524 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2525 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2526 6ced4176 2019-07-12 stsp goto done;
2528 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2529 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2530 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2531 6ced4176 2019-07-12 stsp goto done;
2536 6ced4176 2019-07-12 stsp free(in_repo_path);
2537 6ced4176 2019-07-12 stsp if (err) {
2538 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2539 6ced4176 2019-07-12 stsp free(*tree_relpath);
2540 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2541 6ced4176 2019-07-12 stsp free(*tree_id);
2542 6ced4176 2019-07-12 stsp *tree_id = NULL;
2544 07ed472d 2019-07-12 stsp return err;
2547 07ed472d 2019-07-12 stsp static const struct got_error *
2548 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2549 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2550 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2551 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2553 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2554 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2555 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2556 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2557 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2559 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2560 7f47418f 2019-12-20 stsp if (err) {
2561 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2562 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2563 7f47418f 2019-12-20 stsp goto done;
2564 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2565 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2567 7f47418f 2019-12-20 stsp return err;
2570 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2571 07ed472d 2019-07-12 stsp worktree->base_commit_id);
2573 07ed472d 2019-07-12 stsp goto done;
2575 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2577 07ed472d 2019-07-12 stsp goto done;
2579 07ed472d 2019-07-12 stsp if (entry_name &&
2580 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2581 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2582 07ed472d 2019-07-12 stsp goto done;
2585 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2586 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2587 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2588 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2589 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2590 07ed472d 2019-07-12 stsp arg.repo = repo;
2591 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2592 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2593 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2594 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2595 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2596 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2599 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2600 07ed472d 2019-07-12 stsp if (commit)
2601 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2602 6ced4176 2019-07-12 stsp return err;
2605 9d31a1d8 2018-03-11 stsp const struct got_error *
2606 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2607 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2608 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2609 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2611 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2612 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2613 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2614 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2615 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2616 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2617 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2618 f2ea84fa 2019-07-27 stsp SIMPLEQ_ENTRY(tree_path_data) entry;
2619 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2620 f2ea84fa 2019-07-27 stsp int entry_type;
2621 f2ea84fa 2019-07-27 stsp char *relpath;
2622 f2ea84fa 2019-07-27 stsp char *entry_name;
2623 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2624 f2ea84fa 2019-07-27 stsp SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2626 f2ea84fa 2019-07-27 stsp SIMPLEQ_INIT(&tree_paths);
2628 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2630 9d31a1d8 2018-03-11 stsp return err;
2632 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2633 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2634 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2635 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2636 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2637 f2ea84fa 2019-07-27 stsp goto done;
2640 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2641 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2642 f2ea84fa 2019-07-27 stsp if (err) {
2643 f2ea84fa 2019-07-27 stsp free(tpd);
2644 6ced4176 2019-07-12 stsp goto done;
2647 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2648 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2649 f2ea84fa 2019-07-27 stsp if (err) {
2650 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2651 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2652 f2ea84fa 2019-07-27 stsp free(tpd);
2653 f2ea84fa 2019-07-27 stsp goto done;
2656 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2658 f2ea84fa 2019-07-27 stsp SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2662 51514078 2018-12-25 stsp * Read the file index.
2663 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2664 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2666 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2668 c4cdcb68 2019-04-03 stsp goto done;
2670 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2671 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2672 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2674 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2675 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2676 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2680 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2681 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2682 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2683 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2684 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2685 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2686 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2687 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2691 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_NEXT(tpd, entry);
2693 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2694 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2695 af12c6b9 2019-06-04 stsp err = sync_err;
2697 9c6338c4 2019-06-02 stsp free(fileindex_path);
2699 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2700 9d31a1d8 2018-03-11 stsp if (commit)
2701 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2702 5ade8233 2019-07-12 stsp if (fileindex)
2703 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2704 f2ea84fa 2019-07-27 stsp while (!SIMPLEQ_EMPTY(&tree_paths)) {
2705 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2706 f2ea84fa 2019-07-27 stsp SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2707 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2708 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2709 f2ea84fa 2019-07-27 stsp free(tpd);
2711 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2712 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2713 9d31a1d8 2018-03-11 stsp err = unlockerr;
2714 f8d1f275 2019-02-04 stsp return err;
2717 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2718 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2719 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2720 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2721 234035bc 2019-06-01 stsp void *progress_arg;
2722 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2723 234035bc 2019-06-01 stsp void *cancel_arg;
2724 f69721c3 2019-10-21 stsp const char *label_orig;
2725 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2728 234035bc 2019-06-01 stsp static const struct got_error *
2729 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2730 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
2731 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
2732 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2734 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2735 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2736 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2737 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2738 234035bc 2019-06-01 stsp struct stat sb;
2739 234035bc 2019-06-01 stsp unsigned char status;
2740 234035bc 2019-06-01 stsp int local_changes_subsumed;
2742 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2743 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2744 d6c87207 2019-08-02 stsp strlen(path2));
2745 1ee397ad 2019-07-12 stsp if (ie == NULL)
2746 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2747 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2749 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2750 234035bc 2019-06-01 stsp path2) == -1)
2751 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2753 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2756 234035bc 2019-06-01 stsp goto done;
2758 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2759 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2760 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2761 234035bc 2019-06-01 stsp goto done;
2763 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2764 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2765 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2766 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2767 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2768 234035bc 2019-06-01 stsp goto done;
2771 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2772 36bf999c 2020-07-23 stsp char *link_target2;
2773 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2775 36bf999c 2020-07-23 stsp goto done;
2776 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2777 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2778 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2779 36bf999c 2020-07-23 stsp free(link_target2);
2781 af57b12a 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
2782 af57b12a 2020-07-23 stsp blob1, ondisk_path, path2, sb.st_mode,
2783 af57b12a 2020-07-23 stsp a->label_orig, blob2, a->commit_id2, repo,
2784 af57b12a 2020-07-23 stsp a->progress_cb, a->progress_arg);
2786 234035bc 2019-06-01 stsp } else if (blob1) {
2787 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2788 d6c87207 2019-08-02 stsp strlen(path1));
2789 1ee397ad 2019-07-12 stsp if (ie == NULL)
2790 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2791 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2793 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2794 2b92fad7 2019-06-02 stsp path1) == -1)
2795 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2797 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2800 2b92fad7 2019-06-02 stsp goto done;
2802 2b92fad7 2019-06-02 stsp switch (status) {
2803 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2804 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2805 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2807 1ee397ad 2019-07-12 stsp goto done;
2808 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2810 2b92fad7 2019-06-02 stsp goto done;
2812 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2814 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2815 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2816 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2817 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2819 1ee397ad 2019-07-12 stsp goto done;
2821 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2823 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
2824 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
2825 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
2827 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
2828 0a22ca1a 2020-09-23 stsp * exists in the repository.
2830 0a22ca1a 2020-09-23 stsp err = got_object_blob_file_create(&id, &blob1_f, path1);
2832 0a22ca1a 2020-09-23 stsp goto done;
2833 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
2834 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2835 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
2837 0a22ca1a 2020-09-23 stsp goto done;
2838 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
2841 0a22ca1a 2020-09-23 stsp goto done;
2843 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
2846 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2847 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
2849 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
2850 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
2853 0a22ca1a 2020-09-23 stsp goto done;
2856 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2857 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2858 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2859 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2861 1ee397ad 2019-07-12 stsp goto done;
2863 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2864 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2866 1ee397ad 2019-07-12 stsp goto done;
2871 234035bc 2019-06-01 stsp } else if (blob2) {
2872 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2873 234035bc 2019-06-01 stsp path2) == -1)
2874 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2875 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2876 d6c87207 2019-08-02 stsp strlen(path2));
2878 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2879 7f91a133 2019-12-13 stsp -1, NULL, repo);
2881 234035bc 2019-06-01 stsp goto done;
2882 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2883 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2884 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2885 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2886 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2887 1ee397ad 2019-07-12 stsp status, path2);
2888 234035bc 2019-06-01 stsp goto done;
2890 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2891 36bf999c 2020-07-23 stsp char *link_target2;
2892 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
2895 36bf999c 2020-07-23 stsp goto done;
2896 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
2897 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
2898 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
2899 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
2900 36bf999c 2020-07-23 stsp free(link_target2);
2901 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
2902 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
2903 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
2904 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
2905 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
2906 526a746f 2020-07-23 stsp a->progress_arg);
2908 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
2909 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
2912 dfe9fba0 2020-07-23 stsp goto done;
2913 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2914 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
2915 054041d0 2020-06-24 stsp ondisk_path, blob2->id.sha1,
2916 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
2918 234035bc 2019-06-01 stsp goto done;
2921 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2922 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2923 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
2924 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
2925 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
2926 c90c8ce3 2020-07-23 stsp 0, 1, repo, a->progress_cb, a->progress_arg);
2928 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
2929 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2930 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
2933 234035bc 2019-06-01 stsp goto done;
2934 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
2936 234035bc 2019-06-01 stsp goto done;
2937 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ondisk_path,
2938 3969253a 2020-03-07 stsp NULL, NULL, 1);
2939 3969253a 2020-03-07 stsp if (err) {
2940 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
2941 3969253a 2020-03-07 stsp goto done;
2943 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
2944 2b92fad7 2019-06-02 stsp if (err) {
2945 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
2946 2b92fad7 2019-06-02 stsp goto done;
2948 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2949 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2950 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2955 234035bc 2019-06-01 stsp free(ondisk_path);
2956 234035bc 2019-06-01 stsp return err;
2959 234035bc 2019-06-01 stsp struct check_merge_ok_arg {
2960 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2961 234035bc 2019-06-01 stsp struct got_repository *repo;
2964 234035bc 2019-06-01 stsp static const struct got_error *
2965 234035bc 2019-06-01 stsp check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2967 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
2968 234035bc 2019-06-01 stsp struct check_merge_ok_arg *a = arg;
2969 234035bc 2019-06-01 stsp unsigned char status;
2970 234035bc 2019-06-01 stsp struct stat sb;
2971 234035bc 2019-06-01 stsp char *ondisk_path;
2973 234035bc 2019-06-01 stsp /* Reject merges into a work tree with mixed base commits. */
2974 234035bc 2019-06-01 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2975 234035bc 2019-06-01 stsp SHA1_DIGEST_LENGTH))
2976 234035bc 2019-06-01 stsp return got_error(GOT_ERR_MIXED_COMMITS);
2978 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2980 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2982 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
2983 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2985 234035bc 2019-06-01 stsp return err;
2986 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
2987 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
2989 234035bc 2019-06-01 stsp return NULL;
2992 818c7501 2019-07-11 stsp static const struct got_error *
2993 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2994 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
2995 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
2996 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2997 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2999 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3000 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3001 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3002 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3003 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3005 03415a1a 2019-06-02 stsp if (commit_id1) {
3006 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3007 03415a1a 2019-06-02 stsp worktree->path_prefix);
3008 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3009 03415a1a 2019-06-02 stsp goto done;
3011 69d57f3d 2020-07-31 stsp if (tree_id1) {
3012 69d57f3d 2020-07-31 stsp char *id_str;
3014 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3016 03415a1a 2019-06-02 stsp goto done;
3018 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3020 f69721c3 2019-10-21 stsp goto done;
3022 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3023 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3024 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3025 f69721c3 2019-10-21 stsp free(id_str);
3026 f69721c3 2019-10-21 stsp goto done;
3028 f69721c3 2019-10-21 stsp free(id_str);
3031 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3032 234035bc 2019-06-01 stsp worktree->path_prefix);
3034 234035bc 2019-06-01 stsp goto done;
3036 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3038 234035bc 2019-06-01 stsp goto done;
3040 234035bc 2019-06-01 stsp arg.worktree = worktree;
3041 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3042 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3043 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3044 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3045 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3046 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3047 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3048 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3049 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3050 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3051 af12c6b9 2019-06-04 stsp err = sync_err;
3053 234035bc 2019-06-01 stsp if (tree1)
3054 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3055 234035bc 2019-06-01 stsp if (tree2)
3056 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3057 f69721c3 2019-10-21 stsp free(label_orig);
3058 818c7501 2019-07-11 stsp return err;
3061 818c7501 2019-07-11 stsp const struct got_error *
3062 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3063 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3064 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3065 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3067 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3068 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3069 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3070 818c7501 2019-07-11 stsp struct check_merge_ok_arg mok_arg;
3072 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3074 818c7501 2019-07-11 stsp return err;
3076 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3078 818c7501 2019-07-11 stsp goto done;
3080 818c7501 2019-07-11 stsp mok_arg.worktree = worktree;
3081 818c7501 2019-07-11 stsp mok_arg.repo = repo;
3082 818c7501 2019-07-11 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3083 818c7501 2019-07-11 stsp &mok_arg);
3085 818c7501 2019-07-11 stsp goto done;
3087 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3088 818c7501 2019-07-11 stsp commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3090 818c7501 2019-07-11 stsp if (fileindex)
3091 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3092 818c7501 2019-07-11 stsp free(fileindex_path);
3093 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3094 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3095 234035bc 2019-06-01 stsp err = unlockerr;
3096 234035bc 2019-06-01 stsp return err;
3099 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3100 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3101 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3102 927df6b7 2019-02-10 stsp const char *status_path;
3103 927df6b7 2019-02-10 stsp size_t status_path_len;
3104 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3105 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3106 f8d1f275 2019-02-04 stsp void *status_arg;
3107 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3108 f8d1f275 2019-02-04 stsp void *cancel_arg;
3109 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3110 6841da00 2019-08-08 stsp struct got_pathlist_head ignores;
3111 f2a9dc41 2019-12-13 tracey int report_unchanged;
3112 3143d852 2020-06-25 stsp int no_ignores;
3115 f8d1f275 2019-02-04 stsp static const struct got_error *
3116 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3117 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3118 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3119 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3121 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3122 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3123 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3124 927df6b7 2019-02-10 stsp struct stat sb;
3125 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3126 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3127 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3129 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3131 98eaaa12 2019-08-03 stsp return err;
3133 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3134 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3135 98eaaa12 2019-08-03 stsp return NULL;
3137 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3138 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3139 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3141 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3142 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3143 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3145 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3146 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3147 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3148 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3149 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3152 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3153 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3156 927df6b7 2019-02-10 stsp static const struct got_error *
3157 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3158 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3160 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3161 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3162 f8d1f275 2019-02-04 stsp char *abspath;
3164 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3165 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3167 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3168 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3169 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3170 927df6b7 2019-02-10 stsp return NULL;
3172 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3173 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3174 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3175 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3177 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3178 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3179 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3182 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3183 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3184 f8d1f275 2019-02-04 stsp free(abspath);
3185 9d31a1d8 2018-03-11 stsp return err;
3188 f8d1f275 2019-02-04 stsp static const struct got_error *
3189 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3191 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3192 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3193 2ec1f75b 2019-03-26 stsp unsigned char status;
3195 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3196 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3198 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3199 927df6b7 2019-02-10 stsp return NULL;
3201 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3202 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3203 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3204 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3206 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3207 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3208 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3212 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3214 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3216 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3217 6841da00 2019-08-08 stsp free((char *)pe->path);
3218 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3222 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3224 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3226 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3227 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3228 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3229 6841da00 2019-08-08 stsp free((char *)pe->path);
3231 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3234 6841da00 2019-08-08 stsp static const struct got_error *
3235 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3237 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3238 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3239 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3240 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3241 6841da00 2019-08-08 stsp size_t linesize = 0;
3242 6841da00 2019-08-08 stsp ssize_t linelen;
3244 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3245 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3246 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3247 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3249 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3250 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3251 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3253 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3254 bd8de430 2019-10-04 stsp if (line[0] == '#')
3257 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3258 bd8de430 2019-10-04 stsp if (line[0] == '!')
3261 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3262 6841da00 2019-08-08 stsp line) == -1) {
3263 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3264 6841da00 2019-08-08 stsp goto done;
3266 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3268 6841da00 2019-08-08 stsp goto done;
3270 6841da00 2019-08-08 stsp if (ferror(f)) {
3271 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3272 6841da00 2019-08-08 stsp goto done;
3275 6841da00 2019-08-08 stsp dirpath = strdup(path);
3276 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3277 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3278 6841da00 2019-08-08 stsp goto done;
3280 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3282 6841da00 2019-08-08 stsp free(line);
3283 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3284 6841da00 2019-08-08 stsp free(dirpath);
3285 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3287 6841da00 2019-08-08 stsp return err;
3291 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3293 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3295 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3296 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3297 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3298 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3300 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3301 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3303 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3305 bd8de430 2019-10-04 stsp pattern += 3;
3307 bd8de430 2019-10-04 stsp while (*p) {
3308 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3309 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3310 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3311 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3313 bd8de430 2019-10-04 stsp while (*p == '/')
3323 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3324 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3325 6841da00 2019-08-08 stsp * ignores backwards.
3327 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3328 6841da00 2019-08-08 stsp while (pe) {
3329 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3330 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3331 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3332 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3333 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3334 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3335 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3336 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3337 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3342 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3348 6841da00 2019-08-08 stsp static const struct got_error *
3349 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3350 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3352 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3353 6841da00 2019-08-08 stsp char *ignorespath;
3354 886cec17 2019-12-15 stsp int fd = -1;
3355 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3357 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3358 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3359 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3361 886cec17 2019-12-15 stsp if (dirfd != -1) {
3362 886cec17 2019-12-15 stsp fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3363 886cec17 2019-12-15 stsp if (fd == -1) {
3364 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3365 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3366 886cec17 2019-12-15 stsp ignorespath);
3368 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3369 886cec17 2019-12-15 stsp if (ignoresfile == NULL)
3370 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3371 886cec17 2019-12-15 stsp ignorespath);
3374 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3378 886cec17 2019-12-15 stsp ignoresfile = fopen(ignorespath, "r");
3379 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3380 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3381 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3382 886cec17 2019-12-15 stsp ignorespath);
3384 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3387 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3388 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3389 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3390 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3391 6841da00 2019-08-08 stsp free(ignorespath);
3392 6841da00 2019-08-08 stsp return err;
3395 f8d1f275 2019-02-04 stsp static const struct got_error *
3396 7f91a133 2019-12-13 stsp status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3398 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3399 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3400 f8d1f275 2019-02-04 stsp char *path = NULL;
3402 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3403 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3405 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3406 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3407 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3409 f8d1f275 2019-02-04 stsp path = de->d_name;
3412 3143d852 2020-06-25 stsp if (de->d_type != DT_DIR &&
3413 3143d852 2020-06-25 stsp got_path_is_child(path, a->status_path, a->status_path_len)
3414 6841da00 2019-08-08 stsp && !match_ignores(&a->ignores, path))
3415 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3416 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3417 f8d1f275 2019-02-04 stsp if (parent_path[0])
3418 f8d1f275 2019-02-04 stsp free(path);
3419 b72f483a 2019-02-05 stsp return err;
3422 347d1d3e 2019-07-12 stsp static const struct got_error *
3423 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3425 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3426 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3428 3143d852 2020-06-25 stsp if (a->no_ignores)
3429 3143d852 2020-06-25 stsp return NULL;
3431 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path,
3432 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3434 3143d852 2020-06-25 stsp return err;
3436 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path, path,
3437 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3439 3143d852 2020-06-25 stsp return err;
3442 3143d852 2020-06-25 stsp static const struct got_error *
3443 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3444 abb4604f 2019-07-27 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3445 f2a9dc41 2019-12-13 tracey void *status_arg, struct got_repository *repo, int report_unchanged)
3447 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3448 abb4604f 2019-07-27 stsp struct stat sb;
3450 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3452 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3453 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3455 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3456 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3457 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3458 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3459 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3460 abb4604f 2019-07-27 stsp return NULL;
3463 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3464 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3465 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3467 abb4604f 2019-07-27 stsp return NULL;
3470 3143d852 2020-06-25 stsp static const struct got_error *
3471 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3472 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3474 3143d852 2020-06-25 stsp const struct got_error *err;
3475 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3477 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3478 3143d852 2020-06-25 stsp ".cvsignore");
3480 3143d852 2020-06-25 stsp return err;
3482 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3483 3143d852 2020-06-25 stsp ".gitignore");
3485 3143d852 2020-06-25 stsp return err;
3487 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3488 3143d852 2020-06-25 stsp if (err) {
3489 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3490 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3491 3143d852 2020-06-25 stsp return err;
3493 3143d852 2020-06-25 stsp for (;;) {
3494 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3495 3143d852 2020-06-25 stsp ".cvsignore");
3498 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3499 3143d852 2020-06-25 stsp ".gitignore");
3502 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3503 3143d852 2020-06-25 stsp if (err) {
3504 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3505 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3508 b737c85a 2020-06-26 stsp free(parent_path);
3509 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3510 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3513 b737c85a 2020-06-26 stsp free(parent_path);
3514 b737c85a 2020-06-26 stsp free(next_parent_path);
3515 3143d852 2020-06-25 stsp return err;
3518 abb4604f 2019-07-27 stsp static const struct got_error *
3519 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3520 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3521 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3522 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3523 f2a9dc41 2019-12-13 tracey int report_unchanged)
3525 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3526 6fc93f37 2019-12-13 stsp int fd = -1;
3527 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3528 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3529 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3531 3143d852 2020-06-25 stsp TAILQ_INIT(&arg.ignores);
3533 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3534 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3535 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3537 6fc93f37 2019-12-13 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3538 6fc93f37 2019-12-13 stsp if (fd == -1) {
3539 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3540 00bb5ea0 2020-07-23 stsp errno != ELOOP)
3541 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3543 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3544 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3545 f2a9dc41 2019-12-13 tracey report_unchanged);
3547 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3548 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3549 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3550 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3551 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3552 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3553 abb4604f 2019-07-27 stsp arg.status_path = path;
3554 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3555 abb4604f 2019-07-27 stsp arg.repo = repo;
3556 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3557 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3558 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3559 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3560 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3561 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3562 022fae89 2019-12-06 tracey if (!no_ignores) {
3563 3143d852 2020-06-25 stsp err = add_ignores_from_parent_paths(&arg.ignores,
3564 3143d852 2020-06-25 stsp worktree->root_path, path);
3566 3143d852 2020-06-25 stsp goto done;
3568 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3569 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3572 3143d852 2020-06-25 stsp free_ignores(&arg.ignores);
3573 6fc93f37 2019-12-13 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
3574 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3575 927df6b7 2019-02-10 stsp free(ondisk_path);
3576 347d1d3e 2019-07-12 stsp return err;
3579 347d1d3e 2019-07-12 stsp const struct got_error *
3580 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3581 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3582 72ea6654 2019-07-27 stsp got_worktree_status_cb status_cb, void *status_arg,
3583 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3585 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3586 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3587 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3588 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3590 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3592 347d1d3e 2019-07-12 stsp return err;
3594 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3595 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3596 f2a9dc41 2019-12-13 tracey status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3600 f8d1f275 2019-02-04 stsp free(fileindex_path);
3601 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3602 f8d1f275 2019-02-04 stsp return err;
3605 6c7ab921 2019-03-18 stsp const struct got_error *
3606 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3607 6c7ab921 2019-03-18 stsp const char *arg)
3609 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3610 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3611 6c7ab921 2019-03-18 stsp size_t len;
3612 00bb5ea0 2020-07-23 stsp struct stat sb;
3614 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3616 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3617 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3618 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3620 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3621 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3622 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3623 00bb5ea0 2020-07-23 stsp goto done;
3626 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3628 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3629 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3630 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3631 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3633 00bb5ea0 2020-07-23 stsp char *abspath = NULL;
3634 00bb5ea0 2020-07-23 stsp char canonpath[PATH_MAX];
3635 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3636 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3637 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3638 00bb5ea0 2020-07-23 stsp goto done;
3642 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3643 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3645 d0710d08 2019-07-22 stsp goto done;
3646 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3647 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3648 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3649 00bb5ea0 2020-07-23 stsp goto done;
3652 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3653 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3654 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3655 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3656 00bb5ea0 2020-07-23 stsp goto done;
3658 00bb5ea0 2020-07-23 stsp if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3659 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3660 00bb5ea0 2020-07-23 stsp goto done;
3665 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3666 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3667 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3668 6c7ab921 2019-03-18 stsp goto done;
3671 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3672 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3673 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3675 f6d88e1a 2019-05-29 stsp goto done;
3677 f6d88e1a 2019-05-29 stsp path = strdup("");
3678 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3679 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3680 f6d88e1a 2019-05-29 stsp goto done;
3684 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3685 6c7ab921 2019-03-18 stsp len = strlen(path);
3686 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3687 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3691 6c7ab921 2019-03-18 stsp free(resolved);
3692 d0710d08 2019-07-22 stsp free(cwd);
3693 6c7ab921 2019-03-18 stsp if (err == NULL)
3694 6c7ab921 2019-03-18 stsp *wt_path = path;
3696 6c7ab921 2019-03-18 stsp free(path);
3697 d00136be 2019-03-26 stsp return err;
3700 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3701 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3702 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3703 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3704 4e68cba3 2019-11-23 stsp void *progress_arg;
3705 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3708 4e68cba3 2019-11-23 stsp static const struct got_error *
3709 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3710 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3711 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3712 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3714 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3715 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3716 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3717 6d022e97 2019-08-04 stsp struct stat sb;
3718 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3720 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3721 dbb83fbd 2019-12-12 stsp relpath) == -1)
3722 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3724 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3726 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3727 12463d8b 2019-12-13 stsp de_name, a->repo);
3729 dbb83fbd 2019-12-12 stsp goto done;
3730 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3731 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3732 dbb83fbd 2019-12-12 stsp goto done;
3733 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3735 dbb83fbd 2019-12-12 stsp goto done;
3738 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3739 dbb83fbd 2019-12-12 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3740 dbb83fbd 2019-12-12 stsp goto done;
3743 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3745 4e68cba3 2019-11-23 stsp goto done;
3746 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3747 3969253a 2020-03-07 stsp if (err) {
3748 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3749 3969253a 2020-03-07 stsp goto done;
3751 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3752 07f5b47a 2019-06-02 stsp if (err) {
3753 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
3754 4e68cba3 2019-11-23 stsp goto done;
3757 dbb83fbd 2019-12-12 stsp free(ondisk_path);
3759 dbb83fbd 2019-12-12 stsp return err;
3760 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
3761 dbb83fbd 2019-12-12 stsp return NULL;
3762 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3765 d00136be 2019-03-26 stsp const struct got_error *
3766 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
3767 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
3768 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3769 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
3771 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3772 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
3773 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3774 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
3775 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
3777 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3779 d00136be 2019-03-26 stsp return err;
3781 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3783 d00136be 2019-03-26 stsp goto done;
3785 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
3786 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
3787 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
3788 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
3789 4e68cba3 2019-11-23 stsp saa.repo = repo;
3791 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3792 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3793 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3797 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3798 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3799 af12c6b9 2019-06-04 stsp err = sync_err;
3801 fb399478 2019-07-12 stsp free(fileindex_path);
3802 d00136be 2019-03-26 stsp if (fileindex)
3803 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
3804 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3805 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
3806 d00136be 2019-03-26 stsp err = unlockerr;
3807 6c7ab921 2019-03-18 stsp return err;
3810 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
3811 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
3812 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
3813 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
3814 f2a9dc41 2019-12-13 tracey void *progress_arg;
3815 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
3816 f2a9dc41 2019-12-13 tracey int delete_local_mods;
3817 70e3e7f5 2019-12-13 tracey int keep_on_disk;
3818 766841c2 2020-08-13 stsp const char *status_codes;
3821 f2a9dc41 2019-12-13 tracey static const struct got_error *
3822 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
3823 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
3824 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3825 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
3827 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
3828 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
3829 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
3830 17ed4618 2019-06-02 stsp struct stat sb;
3831 15341bfd 2020-03-05 tracey char *ondisk_path, *parent = NULL;
3833 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3834 17ed4618 2019-06-02 stsp if (ie == NULL)
3835 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
3837 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
3838 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
3839 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3840 9acbc4fa 2019-08-03 stsp return NULL;
3841 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3844 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3845 f2a9dc41 2019-12-13 tracey relpath) == -1)
3846 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
3848 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3851 f2a9dc41 2019-12-13 tracey goto done;
3853 766841c2 2020-08-13 stsp if (a->status_codes) {
3854 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
3856 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
3857 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
3860 766841c2 2020-08-13 stsp if (i == ncodes) {
3861 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
3862 766841c2 2020-08-13 stsp free(ondisk_path);
3863 766841c2 2020-08-13 stsp return NULL;
3865 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3866 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
3867 766841c2 2020-08-13 stsp static char msg[64];
3868 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
3869 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
3870 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3871 766841c2 2020-08-13 stsp goto done;
3875 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
3876 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
3877 f2a9dc41 2019-12-13 tracey goto done;
3878 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3879 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3880 f2a9dc41 2019-12-13 tracey goto done;
3882 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
3883 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
3884 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3885 f2a9dc41 2019-12-13 tracey goto done;
3889 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3890 12463d8b 2019-12-13 stsp if (dirfd != -1) {
3891 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
3892 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
3893 12463d8b 2019-12-13 stsp ondisk_path);
3894 12463d8b 2019-12-13 stsp goto done;
3896 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
3897 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
3898 12463d8b 2019-12-13 stsp goto done;
3901 15341bfd 2020-03-05 tracey parent = dirname(ondisk_path);
3903 15341bfd 2020-03-05 tracey if (parent == NULL) {
3904 15341bfd 2020-03-05 tracey err = got_error_from_errno2("dirname", ondisk_path);
3905 15341bfd 2020-03-05 tracey goto done;
3907 15341bfd 2020-03-05 tracey while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3908 15341bfd 2020-03-05 tracey if (rmdir(parent) == -1) {
3909 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
3910 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
3911 15341bfd 2020-03-05 tracey parent);
3914 15341bfd 2020-03-05 tracey parent = dirname(parent);
3915 15341bfd 2020-03-05 tracey if (parent == NULL) {
3916 15341bfd 2020-03-05 tracey err = got_error_from_errno2("dirname", parent);
3917 15341bfd 2020-03-05 tracey goto done;
3922 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3924 f2a9dc41 2019-12-13 tracey free(ondisk_path);
3925 f2a9dc41 2019-12-13 tracey if (err)
3926 f2a9dc41 2019-12-13 tracey return err;
3927 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
3928 f2a9dc41 2019-12-13 tracey return NULL;
3929 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3930 f2a9dc41 2019-12-13 tracey staged_status, relpath);
3933 2ec1f75b 2019-03-26 stsp const struct got_error *
3934 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
3935 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
3936 766841c2 2020-08-13 stsp const char *status_codes,
3937 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
3938 70e3e7f5 2019-12-13 tracey struct got_repository *repo, int keep_on_disk)
3940 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3941 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
3942 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3943 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
3944 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
3946 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3948 2ec1f75b 2019-03-26 stsp return err;
3950 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3952 2ec1f75b 2019-03-26 stsp goto done;
3954 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
3955 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
3956 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
3957 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
3958 f2a9dc41 2019-12-13 tracey sda.repo = repo;
3959 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
3960 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
3961 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
3963 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3964 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
3965 f2a9dc41 2019-12-13 tracey schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3969 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3970 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3971 af12c6b9 2019-06-04 stsp err = sync_err;
3973 fb399478 2019-07-12 stsp free(fileindex_path);
3974 2ec1f75b 2019-03-26 stsp if (fileindex)
3975 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
3976 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3977 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
3978 2ec1f75b 2019-03-26 stsp err = unlockerr;
3979 33aa809d 2019-08-08 stsp return err;
3982 33aa809d 2019-08-08 stsp static const struct got_error *
3983 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3985 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
3986 33aa809d 2019-08-08 stsp char *line = NULL;
3987 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
3988 33aa809d 2019-08-08 stsp ssize_t linelen;
3990 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
3991 33aa809d 2019-08-08 stsp if (linelen == -1) {
3992 33aa809d 2019-08-08 stsp if (ferror(infile)) {
3993 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
3994 33aa809d 2019-08-08 stsp goto done;
3996 33aa809d 2019-08-08 stsp return NULL;
3998 33aa809d 2019-08-08 stsp if (outfile) {
3999 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4000 33aa809d 2019-08-08 stsp if (n != linelen) {
4001 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4002 33aa809d 2019-08-08 stsp goto done;
4005 33aa809d 2019-08-08 stsp if (rejectfile) {
4006 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4007 33aa809d 2019-08-08 stsp if (n != linelen)
4008 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4011 33aa809d 2019-08-08 stsp free(line);
4012 2ec1f75b 2019-03-26 stsp return err;
4015 33aa809d 2019-08-08 stsp static const struct got_error *
4016 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4018 33aa809d 2019-08-08 stsp char *line = NULL;
4019 33aa809d 2019-08-08 stsp size_t linesize = 0;
4020 33aa809d 2019-08-08 stsp ssize_t linelen;
4022 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4023 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4024 500467ff 2019-09-25 hiltjo if (ferror(f))
4025 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4026 500467ff 2019-09-25 hiltjo return NULL;
4028 33aa809d 2019-08-08 stsp free(line);
4029 33aa809d 2019-08-08 stsp return NULL;
4032 33aa809d 2019-08-08 stsp static const struct got_error *
4033 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4034 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4035 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4037 33aa809d 2019-08-08 stsp const struct got_error *err;
4039 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4040 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4041 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4043 33aa809d 2019-08-08 stsp return err;
4044 33aa809d 2019-08-08 stsp (*line_cur1)++;
4046 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4047 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4048 33aa809d 2019-08-08 stsp if (rejectfile)
4049 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4051 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4053 33aa809d 2019-08-08 stsp return err;
4054 33aa809d 2019-08-08 stsp (*line_cur2)++;
4056 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4057 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4058 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4060 33aa809d 2019-08-08 stsp return err;
4061 33aa809d 2019-08-08 stsp (*line_cur2)++;
4063 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4064 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4065 33aa809d 2019-08-08 stsp if (rejectfile)
4066 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4068 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4070 33aa809d 2019-08-08 stsp return err;
4071 33aa809d 2019-08-08 stsp (*line_cur1)++;
4074 f1e81a05 2019-08-10 stsp return NULL;
4077 f1e81a05 2019-08-10 stsp static const struct got_error *
4078 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4079 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4081 f1e81a05 2019-08-10 stsp const struct got_error *err;
4083 f1e81a05 2019-08-10 stsp if (outfile) {
4084 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4085 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4086 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4088 f1e81a05 2019-08-10 stsp return err;
4089 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4092 f1e81a05 2019-08-10 stsp if (rejectfile) {
4093 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4094 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4095 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4097 f1e81a05 2019-08-10 stsp return err;
4098 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4102 33aa809d 2019-08-08 stsp return NULL;
4105 33aa809d 2019-08-08 stsp static const struct got_error *
4106 33aa809d 2019-08-08 stsp apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
4107 33aa809d 2019-08-08 stsp int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
4108 33aa809d 2019-08-08 stsp int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
4109 33aa809d 2019-08-08 stsp int *line_cur2, FILE *outfile, FILE *rejectfile,
4110 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4112 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4113 33aa809d 2019-08-08 stsp int start_old = change->cv.a;
4114 33aa809d 2019-08-08 stsp int end_old = change->cv.b;
4115 33aa809d 2019-08-08 stsp int start_new = change->cv.c;
4116 33aa809d 2019-08-08 stsp int end_new = change->cv.d;
4117 33aa809d 2019-08-08 stsp long pos1, pos2;
4118 33aa809d 2019-08-08 stsp FILE *hunkfile;
4120 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4122 33aa809d 2019-08-08 stsp hunkfile = got_opentemp();
4123 33aa809d 2019-08-08 stsp if (hunkfile == NULL)
4124 33aa809d 2019-08-08 stsp return got_error_from_errno("got_opentemp");
4126 33aa809d 2019-08-08 stsp pos1 = ftell(f1);
4127 33aa809d 2019-08-08 stsp pos2 = ftell(f2);
4129 33aa809d 2019-08-08 stsp /* XXX TODO needs error checking */
4130 33aa809d 2019-08-08 stsp got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4132 33aa809d 2019-08-08 stsp if (fseek(f1, pos1, SEEK_SET) == -1) {
4133 33aa809d 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
4134 33aa809d 2019-08-08 stsp goto done;
4136 33aa809d 2019-08-08 stsp if (fseek(f2, pos2, SEEK_SET) == -1) {
4137 33aa809d 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
4138 33aa809d 2019-08-08 stsp goto done;
4140 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4141 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4142 33aa809d 2019-08-08 stsp goto done;
4145 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4146 33aa809d 2019-08-08 stsp hunkfile, n, nchanges);
4148 33aa809d 2019-08-08 stsp goto done;
4150 33aa809d 2019-08-08 stsp switch (*choice) {
4151 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4152 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4153 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4155 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4156 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4157 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4159 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4162 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4166 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4167 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4168 33aa809d 2019-08-08 stsp return err;
4171 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4172 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4173 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4174 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4175 1f1abb7e 2019-08-08 stsp void *progress_arg;
4176 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4177 33aa809d 2019-08-08 stsp void *patch_arg;
4178 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4181 e20a8b6f 2019-06-04 stsp static const struct got_error *
4182 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4183 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4184 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4185 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4186 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4188 33aa809d 2019-08-08 stsp const struct got_error *err;
4189 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4190 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4191 1ebedb77 2019-10-19 stsp int fd2 = -1;
4192 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4193 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4194 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4195 33aa809d 2019-08-08 stsp struct stat sb1, sb2;
4196 33aa809d 2019-08-08 stsp struct got_diff_changes *changes = NULL;
4197 33aa809d 2019-08-08 stsp struct got_diff_state *ds = NULL;
4198 33aa809d 2019-08-08 stsp struct got_diff_args *args = NULL;
4199 33aa809d 2019-08-08 stsp struct got_diff_change *change;
4200 33aa809d 2019-08-08 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4201 33aa809d 2019-08-08 stsp int n = 0;
4203 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4205 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4207 33aa809d 2019-08-08 stsp return err;
4209 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4210 12463d8b 2019-12-13 stsp fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4211 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4212 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4213 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4214 fa3cef63 2020-07-23 stsp goto done;
4216 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4217 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4218 fa3cef63 2020-07-23 stsp if (link_len == -1)
4219 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlinkat", path2);
4220 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4221 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4224 12463d8b 2019-12-13 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4225 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4226 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4227 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4228 fa3cef63 2020-07-23 stsp goto done;
4230 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4231 fa3cef63 2020-07-23 stsp sizeof(link_target));
4232 fa3cef63 2020-07-23 stsp if (link_len == -1)
4233 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4234 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4235 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4238 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4239 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4240 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4241 fa3cef63 2020-07-23 stsp goto done;
4244 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4245 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4246 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4247 fa3cef63 2020-07-23 stsp goto done;
4252 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4253 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4254 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4255 fa3cef63 2020-07-23 stsp goto done;
4257 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4258 fa3cef63 2020-07-23 stsp if (n != link_len) {
4259 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4260 fa3cef63 2020-07-23 stsp goto done;
4262 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4263 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4264 fa3cef63 2020-07-23 stsp goto done;
4266 fa3cef63 2020-07-23 stsp rewind(f2);
4269 33aa809d 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4271 33aa809d 2019-08-08 stsp goto done;
4273 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4275 33aa809d 2019-08-08 stsp goto done;
4277 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4279 33aa809d 2019-08-08 stsp goto done;
4281 33aa809d 2019-08-08 stsp if (stat(path1, &sb1) == -1) {
4282 33aa809d 2019-08-08 stsp err = got_error_from_errno2("stat", path1);
4283 33aa809d 2019-08-08 stsp goto done;
4286 33aa809d 2019-08-08 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
4287 33aa809d 2019-08-08 stsp f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4289 33aa809d 2019-08-08 stsp goto done;
4291 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4293 33aa809d 2019-08-08 stsp goto done;
4295 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4296 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4297 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4298 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4299 33aa809d 2019-08-08 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4300 33aa809d 2019-08-08 stsp int choice;
4301 33aa809d 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
4302 33aa809d 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
4303 33aa809d 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
4304 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4305 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4306 e635744c 2019-08-08 stsp patch_cb, patch_arg);
4308 33aa809d 2019-08-08 stsp goto done;
4309 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4310 33aa809d 2019-08-08 stsp have_content = 1;
4311 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4314 1ebedb77 2019-10-19 stsp if (have_content) {
4315 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4316 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4317 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4319 1ebedb77 2019-10-19 stsp goto done;
4321 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4322 fa3cef63 2020-07-23 stsp if (chmod(*path_outfile, sb2.st_mode) == -1) {
4323 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("chmod", path2);
4324 fa3cef63 2020-07-23 stsp goto done;
4329 33aa809d 2019-08-08 stsp free(id_str);
4331 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4332 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4333 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4334 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4335 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4336 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4337 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4338 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4339 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4340 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4341 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4342 33aa809d 2019-08-08 stsp if (err || !have_content) {
4343 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4344 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4345 33aa809d 2019-08-08 stsp free(*path_outfile);
4346 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4348 33aa809d 2019-08-08 stsp free(args);
4350 33aa809d 2019-08-08 stsp got_diff_state_free(ds);
4353 33aa809d 2019-08-08 stsp if (changes)
4354 33aa809d 2019-08-08 stsp got_diff_free_changes(changes);
4355 33aa809d 2019-08-08 stsp free(path1);
4356 33aa809d 2019-08-08 stsp return err;
4359 33aa809d 2019-08-08 stsp static const struct got_error *
4360 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4361 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4362 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4363 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4365 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4366 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4367 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4368 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4369 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4370 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4371 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4372 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4373 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4374 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4376 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4377 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4378 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4379 d3bcc3d1 2019-08-08 stsp return NULL;
4381 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4382 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4383 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4385 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4386 65084dad 2019-08-08 stsp if (ie == NULL)
4387 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4389 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4390 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4391 a129376b 2019-03-28 stsp if (err) {
4392 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4393 a129376b 2019-03-28 stsp goto done;
4394 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4395 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4396 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4397 e20a8b6f 2019-06-04 stsp goto done;
4400 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4401 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4402 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4403 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4404 a129376b 2019-03-28 stsp goto done;
4407 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4408 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4409 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4410 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4411 a129376b 2019-03-28 stsp goto done;
4414 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4415 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4416 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4417 a129376b 2019-03-28 stsp goto done;
4422 1f1abb7e 2019-08-08 stsp err = got_object_id_by_path(&tree_id, a->repo,
4423 1f1abb7e 2019-08-08 stsp a->worktree->base_commit_id, tree_path);
4424 a9fa2909 2019-07-27 stsp if (err) {
4425 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4426 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4427 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4428 a9fa2909 2019-07-27 stsp goto done;
4430 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4432 a9fa2909 2019-07-27 stsp goto done;
4434 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4436 a9fa2909 2019-07-27 stsp goto done;
4438 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4439 1233e6b6 2020-10-19 stsp free(te_name);
4440 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4441 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4442 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4443 a9fa2909 2019-07-27 stsp goto done;
4447 a129376b 2019-03-28 stsp switch (status) {
4448 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4449 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4450 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4451 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4452 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4454 33aa809d 2019-08-08 stsp goto done;
4455 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4458 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4459 1f1abb7e 2019-08-08 stsp ie->path);
4461 1ee397ad 2019-07-12 stsp goto done;
4462 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4464 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4465 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4466 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4467 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4468 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4470 33aa809d 2019-08-08 stsp goto done;
4471 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4474 33aa809d 2019-08-08 stsp /* fall through */
4475 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4476 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4477 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4478 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4479 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4480 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4481 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4482 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4483 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4485 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4486 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4487 1f1abb7e 2019-08-08 stsp err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4489 65084dad 2019-08-08 stsp goto done;
4491 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4492 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4493 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4494 a129376b 2019-03-28 stsp goto done;
4497 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4498 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4499 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4500 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4501 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4502 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4503 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4505 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4506 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4507 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4508 369fd7e5 2020-07-23 stsp path_content);
4511 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4512 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4513 369fd7e5 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4514 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4516 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4517 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4518 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4519 369fd7e5 2020-07-23 stsp goto done;
4523 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4524 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4525 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4526 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4527 c90c8ce3 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4528 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4530 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4532 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4533 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4534 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4535 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4538 a129376b 2019-03-28 stsp goto done;
4539 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4540 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4541 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4542 054041d0 2020-06-24 stsp ondisk_path, blob->id.sha1,
4543 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4545 2e1fa222 2020-07-23 stsp goto done;
4547 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4548 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4549 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4558 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4559 33aa809d 2019-08-08 stsp free(path_content);
4560 e20a8b6f 2019-06-04 stsp free(parent_path);
4561 a129376b 2019-03-28 stsp free(tree_path);
4563 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4565 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4566 a129376b 2019-03-28 stsp free(tree_id);
4567 e20a8b6f 2019-06-04 stsp return err;
4570 e20a8b6f 2019-06-04 stsp const struct got_error *
4571 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4572 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4573 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4574 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4575 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4577 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4578 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4579 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4580 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4581 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4582 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4584 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4586 e20a8b6f 2019-06-04 stsp return err;
4588 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4590 e20a8b6f 2019-06-04 stsp goto done;
4592 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4593 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4594 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4595 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4596 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4597 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4598 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4599 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4600 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4601 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
4605 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4606 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4607 e20a8b6f 2019-06-04 stsp err = sync_err;
4609 fb399478 2019-07-12 stsp free(fileindex_path);
4610 a129376b 2019-03-28 stsp if (fileindex)
4611 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4612 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4613 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4614 a129376b 2019-03-28 stsp err = unlockerr;
4615 c4296144 2019-05-09 stsp return err;
4618 cf066bf8 2019-05-09 stsp static void
4619 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4621 24519714 2019-05-09 stsp free(ct->path);
4622 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4623 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4624 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4625 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4626 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4627 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4631 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4632 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4633 24519714 2019-05-09 stsp struct got_repository *repo;
4634 24519714 2019-05-09 stsp struct got_worktree *worktree;
4635 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4636 5f8a88c6 2019-08-03 stsp int have_staged_files;
4637 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4640 c4296144 2019-05-09 stsp static const struct got_error *
4641 88d0e355 2019-08-03 stsp collect_commitables(void *arg, unsigned char status,
4642 88d0e355 2019-08-03 stsp unsigned char staged_status, const char *relpath,
4643 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4644 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4646 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
4647 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4648 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4649 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4650 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4651 768aea60 2019-05-09 stsp struct stat sb;
4653 5f8a88c6 2019-08-03 stsp if (a->have_staged_files) {
4654 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4655 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4656 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4657 5f8a88c6 2019-08-03 stsp return NULL;
4659 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4660 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4662 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4663 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4664 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4665 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4666 5f8a88c6 2019-08-03 stsp return NULL;
4669 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4670 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4671 036813ee 2019-05-09 stsp goto done;
4673 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4674 036813ee 2019-05-09 stsp parent_path = strdup("");
4675 69960a46 2019-05-09 stsp if (parent_path == NULL)
4676 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4678 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4680 69960a46 2019-05-09 stsp return err;
4683 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4684 cf066bf8 2019-05-09 stsp if (ct == NULL) {
4685 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4686 c4296144 2019-05-09 stsp goto done;
4689 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4690 768aea60 2019-05-09 stsp relpath) == -1) {
4691 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4692 768aea60 2019-05-09 stsp goto done;
4695 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
4696 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
4697 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
4698 0aeb8099 2020-07-23 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4699 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
4700 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
4701 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
4702 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
4704 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
4705 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
4708 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4709 0aeb8099 2020-07-23 stsp goto done;
4711 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
4712 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
4713 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
4714 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4715 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
4716 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
4717 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
4718 12463d8b 2019-12-13 stsp ct->ondisk_path);
4719 12463d8b 2019-12-13 stsp goto done;
4721 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
4722 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
4723 768aea60 2019-05-09 stsp goto done;
4725 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
4728 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4729 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4730 44d03001 2019-05-09 stsp relpath) == -1) {
4731 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4732 44d03001 2019-05-09 stsp goto done;
4735 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4736 35213c7c 2020-07-23 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4737 35213c7c 2020-07-23 stsp int is_bad_symlink;
4738 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
4739 35213c7c 2020-07-23 stsp ssize_t target_len;
4740 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
4741 35213c7c 2020-07-23 stsp sizeof(target_path));
4742 35213c7c 2020-07-23 stsp if (target_len == -1) {
4743 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
4744 35213c7c 2020-07-23 stsp ct->ondisk_path);
4745 35213c7c 2020-07-23 stsp goto done;
4747 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
4748 35213c7c 2020-07-23 stsp target_len, ct->ondisk_path, a->worktree->root_path);
4750 35213c7c 2020-07-23 stsp goto done;
4751 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
4752 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
4753 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
4754 35213c7c 2020-07-23 stsp goto done;
4759 cf066bf8 2019-05-09 stsp ct->status = status;
4760 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
4761 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
4762 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
4763 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
4764 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
4765 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
4766 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4767 b416585c 2019-05-13 stsp goto done;
4769 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
4770 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
4771 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
4772 f0b75401 2019-08-03 stsp goto done;
4775 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
4776 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
4777 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4778 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
4779 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4780 036813ee 2019-05-09 stsp goto done;
4783 24519714 2019-05-09 stsp ct->path = strdup(path);
4784 24519714 2019-05-09 stsp if (ct->path == NULL) {
4785 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4786 24519714 2019-05-09 stsp goto done;
4788 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4790 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
4791 ed175427 2019-05-09 stsp free_commitable(ct);
4792 24519714 2019-05-09 stsp free(parent_path);
4793 036813ee 2019-05-09 stsp free(path);
4794 a129376b 2019-03-28 stsp return err;
4797 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
4798 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
4799 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4800 036813ee 2019-05-09 stsp struct got_repository *);
4802 ed175427 2019-05-09 stsp static const struct got_error *
4803 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4804 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
4805 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
4806 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4807 afa376bf 2019-05-09 stsp struct got_repository *repo)
4809 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
4810 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
4811 036813ee 2019-05-09 stsp char *subpath;
4813 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
4814 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4815 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4817 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
4819 ed175427 2019-05-09 stsp return err;
4821 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
4822 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
4823 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
4824 036813ee 2019-05-09 stsp free(subpath);
4825 036813ee 2019-05-09 stsp return err;
4828 036813ee 2019-05-09 stsp static const struct got_error *
4829 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4831 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4832 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
4834 036813ee 2019-05-09 stsp *match = 0;
4836 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
4837 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
4838 0f63689d 2019-05-10 stsp return NULL;
4841 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4843 0f63689d 2019-05-10 stsp return err;
4844 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
4845 036813ee 2019-05-09 stsp free(ct_parent_path);
4846 036813ee 2019-05-09 stsp return err;
4849 768aea60 2019-05-09 stsp static mode_t
4850 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
4852 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
4853 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
4855 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4858 036813ee 2019-05-09 stsp static const struct got_error *
4859 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4860 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
4862 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4864 036813ee 2019-05-09 stsp *new_te = NULL;
4866 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
4868 036813ee 2019-05-09 stsp goto done;
4870 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4872 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
4873 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4874 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4876 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4878 036813ee 2019-05-09 stsp if (err && *new_te) {
4879 56e0773d 2019-11-28 stsp free(*new_te);
4880 036813ee 2019-05-09 stsp *new_te = NULL;
4882 036813ee 2019-05-09 stsp return err;
4885 036813ee 2019-05-09 stsp static const struct got_error *
4886 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4887 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
4889 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4890 102b254e 2020-10-19 stsp char *ct_name = NULL;
4892 036813ee 2019-05-09 stsp *new_te = NULL;
4894 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
4895 036813ee 2019-05-09 stsp if (*new_te == NULL)
4896 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
4898 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
4900 036813ee 2019-05-09 stsp goto done;
4901 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4902 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
4903 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
4904 036813ee 2019-05-09 stsp goto done;
4907 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4909 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
4910 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4911 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4913 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4915 102b254e 2020-10-19 stsp free(ct_name);
4916 036813ee 2019-05-09 stsp if (err && *new_te) {
4917 56e0773d 2019-11-28 stsp free(*new_te);
4918 036813ee 2019-05-09 stsp *new_te = NULL;
4920 036813ee 2019-05-09 stsp return err;
4923 036813ee 2019-05-09 stsp static const struct got_error *
4924 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
4925 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
4927 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4928 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
4930 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4932 036813ee 2019-05-09 stsp return err;
4933 036813ee 2019-05-09 stsp if (new_pe == NULL)
4934 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
4935 036813ee 2019-05-09 stsp return NULL;
4938 afa376bf 2019-05-09 stsp static const struct got_error *
4939 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
4940 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
4942 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
4943 5f8a88c6 2019-08-03 stsp unsigned char status;
4945 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
4946 afa376bf 2019-05-09 stsp ct_path++;
4948 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4949 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
4951 5f8a88c6 2019-08-03 stsp status = ct->status;
4953 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4954 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4957 036813ee 2019-05-09 stsp static const struct got_error *
4958 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
4959 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4961 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
4962 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
4963 44d03001 2019-05-09 stsp char *te_path;
4965 44d03001 2019-05-09 stsp *modified = 0;
4967 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
4968 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
4969 44d03001 2019-05-09 stsp te->name) == -1)
4970 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4972 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4973 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4974 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
4975 44d03001 2019-05-09 stsp strlen(te_path));
4976 44d03001 2019-05-09 stsp if (*modified)
4980 44d03001 2019-05-09 stsp free(te_path);
4981 44d03001 2019-05-09 stsp return err;
4984 44d03001 2019-05-09 stsp static const struct got_error *
4985 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
4986 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
4987 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
4989 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4990 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
4992 036813ee 2019-05-09 stsp *ctp = NULL;
4994 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4995 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4996 036813ee 2019-05-09 stsp char *ct_name = NULL;
4997 036813ee 2019-05-09 stsp int path_matches;
4999 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5000 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5001 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5002 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
5005 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5006 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5010 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5013 036813ee 2019-05-09 stsp err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5015 036813ee 2019-05-09 stsp return err;
5016 036813ee 2019-05-09 stsp if (!path_matches)
5019 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5021 d34b633e 2020-10-19 stsp return err;
5023 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5024 d34b633e 2020-10-19 stsp free(ct_name);
5027 d34b633e 2020-10-19 stsp free(ct_name);
5029 036813ee 2019-05-09 stsp *ctp = ct;
5033 2b496619 2019-07-10 stsp return err;
5036 2b496619 2019-07-10 stsp static const struct got_error *
5037 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5038 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5039 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5040 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5041 2b496619 2019-07-10 stsp struct got_repository *repo)
5043 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5044 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5045 2b496619 2019-07-10 stsp char *subtree_path;
5046 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5047 ba580f68 2020-03-22 stsp int nentries;
5049 2b496619 2019-07-10 stsp *new_tep = NULL;
5051 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5052 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5053 2b496619 2019-07-10 stsp child_path) == -1)
5054 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5056 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5057 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5058 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5059 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5061 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5062 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5063 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5064 2b496619 2019-07-10 stsp goto done;
5066 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5067 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5068 2b496619 2019-07-10 stsp if (err) {
5069 56e0773d 2019-11-28 stsp free(new_te);
5070 2b496619 2019-07-10 stsp goto done;
5072 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5075 2b496619 2019-07-10 stsp free(subtree_path);
5076 2b496619 2019-07-10 stsp if (err == NULL)
5077 2b496619 2019-07-10 stsp *new_tep = new_te;
5078 036813ee 2019-05-09 stsp return err;
5081 036813ee 2019-05-09 stsp static const struct got_error *
5082 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5083 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5084 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5085 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5086 036813ee 2019-05-09 stsp struct got_repository *repo)
5088 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5089 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5090 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5091 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5093 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5094 ba580f68 2020-03-22 stsp *nentries = 0;
5096 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5097 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5098 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5099 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5101 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5102 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5103 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5106 f2b0a8b0 2020-07-31 stsp if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5107 2f51b5b3 2019-05-09 stsp strlen(path_base_tree)))
5110 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5111 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5113 036813ee 2019-05-09 stsp goto done;
5115 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5116 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5117 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5119 036813ee 2019-05-09 stsp goto done;
5120 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5122 afa376bf 2019-05-09 stsp goto done;
5123 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5124 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5126 2f51b5b3 2019-05-09 stsp goto done;
5127 ba580f68 2020-03-22 stsp (*nentries)++;
5129 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5130 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5131 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5132 2b496619 2019-07-10 stsp == NULL) {
5133 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5134 2b496619 2019-07-10 stsp child_path, path_base_tree,
5135 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5138 2b496619 2019-07-10 stsp goto done;
5139 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5141 2b496619 2019-07-10 stsp goto done;
5142 ba580f68 2020-03-22 stsp (*nentries)++;
5147 2f51b5b3 2019-05-09 stsp if (base_tree) {
5148 56e0773d 2019-11-28 stsp int i, nbase_entries;
5149 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5150 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5151 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5152 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5154 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5155 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5156 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5157 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5159 63c5ca5d 2019-08-24 stsp goto done;
5160 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5162 63c5ca5d 2019-08-24 stsp goto done;
5163 ba580f68 2020-03-22 stsp (*nentries)++;
5167 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5168 44d03001 2019-05-09 stsp int modified;
5169 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5171 036813ee 2019-05-09 stsp goto done;
5172 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5173 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5175 2f51b5b3 2019-05-09 stsp goto done;
5176 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5177 44d03001 2019-05-09 stsp if (modified) {
5178 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5179 ba580f68 2020-03-22 stsp int nsubentries;
5180 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5181 ba580f68 2020-03-22 stsp &nsubentries, te,
5182 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5183 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5185 44d03001 2019-05-09 stsp goto done;
5186 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5187 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5188 ba580f68 2020-03-22 stsp free(new_id);
5191 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5192 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5193 56e0773d 2019-11-28 stsp free(new_id);
5195 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5197 036813ee 2019-05-09 stsp goto done;
5198 ba580f68 2020-03-22 stsp (*nentries)++;
5202 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5203 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5205 f66c734c 2019-09-22 stsp goto done;
5207 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5208 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5209 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5210 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5211 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5212 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5214 2f51b5b3 2019-05-09 stsp goto done;
5215 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5217 2f51b5b3 2019-05-09 stsp goto done;
5218 ba580f68 2020-03-22 stsp (*nentries)++;
5220 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5221 b416585c 2019-05-13 stsp status_arg);
5223 afa376bf 2019-05-09 stsp goto done;
5225 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5226 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5228 2f51b5b3 2019-05-09 stsp goto done;
5229 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5231 2f51b5b3 2019-05-09 stsp goto done;
5232 ba580f68 2020-03-22 stsp (*nentries)++;
5237 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5238 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5240 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
5241 2e1fa222 2020-07-23 stsp return err;
5244 2e1fa222 2020-07-23 stsp static const struct got_error *
5245 ebf99748 2019-05-09 stsp update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5246 72fd46fa 2019-09-06 stsp struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5247 72fd46fa 2019-09-06 stsp int have_staged_files)
5249 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5250 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5252 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5253 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5254 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5256 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5258 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5259 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5260 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5261 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5262 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5263 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5264 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5265 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5266 5f8a88c6 2019-08-03 stsp ct->ondisk_path, ct->staged_blob_id->sha1,
5267 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5268 72fd46fa 2019-09-06 stsp !have_staged_files);
5270 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5271 e75eb4da 2019-05-10 stsp ct->ondisk_path, ct->blob_id->sha1,
5272 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5273 72fd46fa 2019-09-06 stsp !have_staged_files);
5275 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5278 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ct->ondisk_path,
5279 3969253a 2020-03-07 stsp ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5280 3969253a 2020-03-07 stsp if (err) {
5281 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5284 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5285 3969253a 2020-03-07 stsp if (err) {
5286 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5291 d56d26ce 2019-05-10 stsp return err;
5295 d56d26ce 2019-05-10 stsp static const struct got_error *
5296 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5297 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5298 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5299 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5300 735ef5ac 2019-08-03 stsp int ood_errcode)
5302 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5303 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5305 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5306 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5307 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5308 1a36436d 2019-06-10 stsp return NULL;
5310 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5311 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5313 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5314 735ef5ac 2019-08-03 stsp in_repo_path);
5315 9bead371 2019-07-28 stsp if (err) {
5316 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5317 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5318 1a36436d 2019-06-10 stsp goto done;
5319 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5320 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5322 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5323 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5324 735ef5ac 2019-08-03 stsp in_repo_path);
5325 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5326 1a36436d 2019-06-10 stsp goto done;
5327 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5331 1a36436d 2019-06-10 stsp return err;
5334 c4296144 2019-05-09 stsp const struct got_error *
5335 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5336 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5337 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id, struct got_worktree *worktree,
5338 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
5339 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5340 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5341 afa376bf 2019-05-09 stsp struct got_repository *repo)
5343 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5344 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5345 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5346 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5347 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5348 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5349 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5350 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5351 ba580f68 2020-03-22 stsp int nentries;
5352 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5353 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5354 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5356 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5358 de18fc63 2019-05-09 stsp SIMPLEQ_INIT(&parent_ids);
5360 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5362 588edf97 2019-05-10 stsp goto done;
5364 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5366 588edf97 2019-05-10 stsp goto done;
5368 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5369 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5374 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5375 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5379 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5380 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5381 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5382 cf066bf8 2019-05-09 stsp char *ondisk_path;
5384 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5385 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5386 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5389 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5390 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5391 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5394 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5395 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5396 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5397 cf066bf8 2019-05-09 stsp goto done;
5399 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5400 2e1fa222 2020-07-23 stsp free(ondisk_path);
5402 cf066bf8 2019-05-09 stsp goto done;
5405 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5406 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5407 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5409 036813ee 2019-05-09 stsp goto done;
5411 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5413 de18fc63 2019-05-09 stsp goto done;
5414 de18fc63 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5415 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5416 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5417 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
5418 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5419 33ad4cbe 2019-05-12 jcs free(logmsg);
5421 9d40349a 2019-05-09 stsp goto done;
5423 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5424 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5425 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5426 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5427 09f5bd90 2019-05-09 stsp goto done;
5429 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
5430 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5432 09f5bd90 2019-05-09 stsp goto done;
5433 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5435 ebf99748 2019-05-09 stsp goto done;
5436 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5437 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5438 09f5bd90 2019-05-09 stsp goto done;
5440 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
5441 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
5443 f2c16586 2019-05-09 stsp goto done;
5444 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
5446 f2c16586 2019-05-09 stsp goto done;
5448 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5450 09f5bd90 2019-05-09 stsp goto done;
5452 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
5454 ebf99748 2019-05-09 stsp goto done;
5456 588edf97 2019-05-10 stsp if (head_tree)
5457 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
5458 588edf97 2019-05-10 stsp if (head_commit)
5459 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
5460 09f5bd90 2019-05-09 stsp free(head_commit_id2);
5461 2f17228e 2019-05-12 stsp if (head_ref2) {
5462 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
5463 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
5464 2f17228e 2019-05-12 stsp err = unlockerr;
5465 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
5467 39cd0ff6 2019-07-12 stsp return err;
5470 5c1e53bc 2019-07-28 stsp static const struct got_error *
5471 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
5472 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
5474 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
5475 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
5477 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
5478 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
5479 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
5481 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
5482 5c1e53bc 2019-07-28 stsp ct_path++;
5484 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
5485 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
5489 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
5490 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
5492 5c1e53bc 2019-07-28 stsp return NULL;
5495 f0b75401 2019-08-03 stsp static const struct got_error *
5496 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
5498 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
5500 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5501 f0b75401 2019-08-03 stsp *have_staged_files = 1;
5502 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
5505 f0b75401 2019-08-03 stsp return NULL;
5508 5f8a88c6 2019-08-03 stsp static const struct got_error *
5509 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
5510 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
5512 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
5513 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
5515 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5516 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
5518 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5519 5f8a88c6 2019-08-03 stsp if (ie == NULL)
5520 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5521 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5522 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
5523 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
5526 5f8a88c6 2019-08-03 stsp return NULL;
5529 39cd0ff6 2019-07-12 stsp const struct got_error *
5530 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
5531 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
5532 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
5533 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5534 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
5535 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
5537 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5538 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
5539 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
5540 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5541 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5542 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
5543 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
5544 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5545 f0b75401 2019-08-03 stsp int have_staged_files = 0;
5547 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
5549 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5551 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
5553 39cd0ff6 2019-07-12 stsp goto done;
5555 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5557 39cd0ff6 2019-07-12 stsp goto done;
5559 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5561 39cd0ff6 2019-07-12 stsp goto done;
5563 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5565 39cd0ff6 2019-07-12 stsp goto done;
5567 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5568 5f8a88c6 2019-08-03 stsp &have_staged_files);
5569 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
5570 5f8a88c6 2019-08-03 stsp goto done;
5571 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
5572 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
5574 5f8a88c6 2019-08-03 stsp goto done;
5577 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5578 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
5579 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
5580 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
5581 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
5582 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5583 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5584 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5585 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5587 5c1e53bc 2019-07-28 stsp goto done;
5590 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5591 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5592 39cd0ff6 2019-07-12 stsp goto done;
5595 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5596 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
5598 5c1e53bc 2019-07-28 stsp goto done;
5601 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5602 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5603 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5605 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5606 f0b75401 2019-08-03 stsp ct_path++;
5607 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5608 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5609 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5611 b50cabdf 2019-07-12 stsp goto done;
5615 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5616 5c1e53bc 2019-07-28 stsp head_commit_id, worktree, author, committer,
5617 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5619 39cd0ff6 2019-07-12 stsp goto done;
5621 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5622 72fd46fa 2019-09-06 stsp fileindex, have_staged_files);
5623 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5624 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5625 39cd0ff6 2019-07-12 stsp err = sync_err;
5627 39cd0ff6 2019-07-12 stsp if (fileindex)
5628 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5629 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5630 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5631 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5632 39cd0ff6 2019-07-12 stsp err = unlockerr;
5633 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5634 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5635 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5637 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5638 c4296144 2019-05-09 stsp return err;
5641 8656d6c4 2019-05-20 stsp const char *
5642 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5644 8656d6c4 2019-05-20 stsp return ct->path;
5647 8656d6c4 2019-05-20 stsp unsigned int
5648 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
5650 8656d6c4 2019-05-20 stsp return ct->status;
5653 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
5654 818c7501 2019-07-11 stsp struct got_worktree *worktree;
5655 818c7501 2019-07-11 stsp struct got_repository *repo;
5658 818c7501 2019-07-11 stsp static const struct got_error *
5659 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5661 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5662 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
5663 818c7501 2019-07-11 stsp unsigned char status;
5664 818c7501 2019-07-11 stsp struct stat sb;
5665 818c7501 2019-07-11 stsp char *ondisk_path;
5667 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
5668 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5669 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
5670 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
5672 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5674 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
5676 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
5677 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5678 818c7501 2019-07-11 stsp free(ondisk_path);
5680 818c7501 2019-07-11 stsp return err;
5682 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
5683 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
5684 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5685 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5687 818c7501 2019-07-11 stsp return NULL;
5690 818c7501 2019-07-11 stsp const struct got_error *
5691 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5692 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5693 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
5694 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5696 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5697 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5698 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
5699 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
5700 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
5701 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5702 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
5704 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5705 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5706 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5708 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
5710 818c7501 2019-07-11 stsp return err;
5712 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5714 818c7501 2019-07-11 stsp goto done;
5716 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
5717 818c7501 2019-07-11 stsp ok_arg.repo = repo;
5718 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5721 818c7501 2019-07-11 stsp goto done;
5723 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5725 818c7501 2019-07-11 stsp goto done;
5727 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5729 818c7501 2019-07-11 stsp goto done;
5731 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5733 818c7501 2019-07-11 stsp goto done;
5735 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5738 e51d7b55 2020-01-04 stsp goto done;
5740 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5742 818c7501 2019-07-11 stsp goto done;
5743 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5744 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5745 e51d7b55 2020-01-04 stsp goto done;
5748 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
5749 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
5751 818c7501 2019-07-11 stsp goto done;
5752 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
5754 818c7501 2019-07-11 stsp goto done;
5756 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
5758 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5760 818c7501 2019-07-11 stsp goto done;
5762 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
5764 818c7501 2019-07-11 stsp goto done;
5766 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
5767 818c7501 2019-07-11 stsp worktree->base_commit_id);
5769 818c7501 2019-07-11 stsp goto done;
5770 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
5772 818c7501 2019-07-11 stsp goto done;
5774 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
5776 818c7501 2019-07-11 stsp goto done;
5778 818c7501 2019-07-11 stsp free(fileindex_path);
5779 818c7501 2019-07-11 stsp free(tmp_branch_name);
5780 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
5781 818c7501 2019-07-11 stsp free(branch_ref_name);
5782 818c7501 2019-07-11 stsp if (branch_ref)
5783 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5784 818c7501 2019-07-11 stsp if (wt_branch)
5785 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
5786 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
5787 818c7501 2019-07-11 stsp if (err) {
5788 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
5789 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
5790 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5792 818c7501 2019-07-11 stsp if (*tmp_branch) {
5793 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5794 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5796 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5797 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5798 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5800 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
5802 818c7501 2019-07-11 stsp return err;
5805 818c7501 2019-07-11 stsp const struct got_error *
5806 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
5807 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5808 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
5809 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
5811 818c7501 2019-07-11 stsp const struct got_error *err;
5812 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5813 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5814 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5815 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
5816 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
5818 818c7501 2019-07-11 stsp *commit_id = NULL;
5819 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
5820 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
5821 3e3a69f1 2019-07-25 stsp *branch = NULL;
5822 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5824 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
5826 3e3a69f1 2019-07-25 stsp return err;
5828 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5830 f032f1f7 2019-08-04 stsp goto done;
5832 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5833 f032f1f7 2019-08-04 stsp &have_staged_files);
5834 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
5835 f032f1f7 2019-08-04 stsp goto done;
5836 f032f1f7 2019-08-04 stsp if (have_staged_files) {
5837 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
5838 3e3a69f1 2019-07-25 stsp goto done;
5841 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5843 f032f1f7 2019-08-04 stsp goto done;
5845 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5847 818c7501 2019-07-11 stsp goto done;
5849 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5851 818c7501 2019-07-11 stsp goto done;
5853 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5855 818c7501 2019-07-11 stsp goto done;
5857 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5859 818c7501 2019-07-11 stsp goto done;
5861 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
5862 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
5864 818c7501 2019-07-11 stsp goto done;
5866 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5868 818c7501 2019-07-11 stsp goto done;
5870 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
5872 818c7501 2019-07-11 stsp goto done;
5874 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
5875 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
5877 818c7501 2019-07-11 stsp goto done;
5879 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5881 818c7501 2019-07-11 stsp goto done;
5883 818c7501 2019-07-11 stsp free(commit_ref_name);
5884 818c7501 2019-07-11 stsp free(branch_ref_name);
5885 3e3a69f1 2019-07-25 stsp free(fileindex_path);
5886 818c7501 2019-07-11 stsp if (commit_ref)
5887 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
5888 818c7501 2019-07-11 stsp if (branch_ref)
5889 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5890 818c7501 2019-07-11 stsp if (err) {
5891 818c7501 2019-07-11 stsp free(*commit_id);
5892 818c7501 2019-07-11 stsp *commit_id = NULL;
5893 818c7501 2019-07-11 stsp if (*tmp_branch) {
5894 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5895 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5897 818c7501 2019-07-11 stsp if (*new_base_branch) {
5898 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
5899 818c7501 2019-07-11 stsp *new_base_branch = NULL;
5901 818c7501 2019-07-11 stsp if (*branch) {
5902 818c7501 2019-07-11 stsp got_ref_close(*branch);
5903 818c7501 2019-07-11 stsp *branch = NULL;
5905 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5906 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5907 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5909 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
5911 818c7501 2019-07-11 stsp return err;
5914 818c7501 2019-07-11 stsp const struct got_error *
5915 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5917 818c7501 2019-07-11 stsp const struct got_error *err;
5918 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
5920 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5922 818c7501 2019-07-11 stsp return err;
5924 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5925 818c7501 2019-07-11 stsp free(tmp_branch_name);
5926 818c7501 2019-07-11 stsp return NULL;
5929 818c7501 2019-07-11 stsp static const struct got_error *
5930 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5931 818c7501 2019-07-11 stsp char **logmsg, void *arg)
5933 0ebf8283 2019-07-24 stsp *logmsg = arg;
5934 818c7501 2019-07-11 stsp return NULL;
5937 818c7501 2019-07-11 stsp static const struct got_error *
5938 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5939 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
5940 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5941 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5943 818c7501 2019-07-11 stsp return NULL;
5946 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
5947 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
5948 01757395 2019-07-12 stsp void *progress_arg;
5949 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
5952 01757395 2019-07-12 stsp static const struct got_error *
5953 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
5955 01757395 2019-07-12 stsp const struct got_error *err;
5956 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
5958 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
5960 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
5962 01757395 2019-07-12 stsp return err;
5964 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
5965 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
5966 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
5967 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
5968 01757395 2019-07-12 stsp return NULL;
5970 01757395 2019-07-12 stsp p = strdup(path);
5971 01757395 2019-07-12 stsp if (p == NULL)
5972 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
5974 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5975 01757395 2019-07-12 stsp if (err || new == NULL)
5977 01757395 2019-07-12 stsp return err;
5981 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5983 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
5985 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
5986 01757395 2019-07-12 stsp free((char *)pe->path);
5988 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
5991 0ebf8283 2019-07-24 stsp static const struct got_error *
5992 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5993 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
5995 818c7501 2019-07-11 stsp const struct got_error *err;
5996 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
5998 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5999 818c7501 2019-07-11 stsp if (err) {
6000 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6001 818c7501 2019-07-11 stsp goto done;
6002 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6004 818c7501 2019-07-11 stsp goto done;
6005 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6007 818c7501 2019-07-11 stsp goto done;
6008 de05890f 2020-03-05 stsp } else if (is_rebase) {
6009 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6012 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6014 818c7501 2019-07-11 stsp goto done;
6015 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6016 818c7501 2019-07-11 stsp free(stored_id);
6017 818c7501 2019-07-11 stsp if (cmp != 0) {
6018 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6019 818c7501 2019-07-11 stsp goto done;
6023 0ebf8283 2019-07-24 stsp if (commit_ref)
6024 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6025 0ebf8283 2019-07-24 stsp return err;
6028 0ebf8283 2019-07-24 stsp static const struct got_error *
6029 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6030 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6031 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6032 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6033 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6034 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6036 0ebf8283 2019-07-24 stsp const struct got_error *err;
6037 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6038 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6039 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6041 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6043 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6045 0ebf8283 2019-07-24 stsp return err;
6047 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6048 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6049 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6050 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6051 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6052 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6053 818c7501 2019-07-11 stsp if (commit_ref)
6054 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6055 818c7501 2019-07-11 stsp return err;
6058 818c7501 2019-07-11 stsp const struct got_error *
6059 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6060 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6061 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6062 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6063 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6064 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6066 0ebf8283 2019-07-24 stsp const struct got_error *err;
6067 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6069 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6071 0ebf8283 2019-07-24 stsp return err;
6073 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6075 0ebf8283 2019-07-24 stsp goto done;
6077 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6078 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6079 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6081 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6082 0ebf8283 2019-07-24 stsp return err;
6085 0ebf8283 2019-07-24 stsp const struct got_error *
6086 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6087 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6088 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6089 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6090 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6091 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6093 0ebf8283 2019-07-24 stsp const struct got_error *err;
6094 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6096 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6098 0ebf8283 2019-07-24 stsp return err;
6100 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6102 0ebf8283 2019-07-24 stsp goto done;
6104 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6105 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6106 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6108 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6109 0ebf8283 2019-07-24 stsp return err;
6112 0ebf8283 2019-07-24 stsp static const struct got_error *
6113 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6114 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6115 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6116 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6117 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
6119 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6120 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6121 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6122 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6123 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6124 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6125 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6127 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6128 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6130 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6132 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6134 3e3a69f1 2019-07-25 stsp return err;
6136 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6137 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6138 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6139 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6141 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6142 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6143 01757395 2019-07-12 stsp * TODO: Ideally, merged_paths would contain a list of commitables
6144 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6146 01757395 2019-07-12 stsp if (merged_paths) {
6147 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6148 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6149 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6150 f2a9dc41 2019-12-13 tracey repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6153 01757395 2019-07-12 stsp goto done;
6156 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6157 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6159 01757395 2019-07-12 stsp goto done;
6162 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6163 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6164 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6166 a0e95631 2019-07-12 stsp goto done;
6167 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6168 a0e95631 2019-07-12 stsp goto done;
6171 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6173 edd02c5e 2019-07-11 stsp goto done;
6175 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6177 818c7501 2019-07-11 stsp goto done;
6179 5943eee2 2019-08-13 stsp if (new_logmsg) {
6180 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6181 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6182 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6183 5943eee2 2019-08-13 stsp goto done;
6186 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6188 5943eee2 2019-08-13 stsp goto done;
6191 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6192 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6193 5c1e53bc 2019-07-28 stsp worktree, got_object_commit_get_author(orig_commit),
6194 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
6195 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6197 a0e95631 2019-07-12 stsp goto done;
6199 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6201 a0e95631 2019-07-12 stsp goto done;
6203 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6205 a0e95631 2019-07-12 stsp goto done;
6207 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6208 72fd46fa 2019-09-06 stsp fileindex, 0);
6209 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6210 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6211 a0e95631 2019-07-12 stsp err = sync_err;
6213 a0e95631 2019-07-12 stsp free(fileindex_path);
6214 a0e95631 2019-07-12 stsp free(head_commit_id);
6215 a0e95631 2019-07-12 stsp if (head_ref)
6216 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6217 818c7501 2019-07-11 stsp if (err) {
6218 818c7501 2019-07-11 stsp free(*new_commit_id);
6219 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6221 818c7501 2019-07-11 stsp return err;
6224 818c7501 2019-07-11 stsp const struct got_error *
6225 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6226 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6227 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6228 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6229 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6231 0ebf8283 2019-07-24 stsp const struct got_error *err;
6232 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6233 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6234 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6236 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6238 0ebf8283 2019-07-24 stsp return err;
6240 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6242 0ebf8283 2019-07-24 stsp goto done;
6243 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6245 0ebf8283 2019-07-24 stsp goto done;
6246 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6247 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6248 0ebf8283 2019-07-24 stsp goto done;
6251 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6252 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6254 0ebf8283 2019-07-24 stsp if (commit_ref)
6255 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6256 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6257 0ebf8283 2019-07-24 stsp free(commit_id);
6258 0ebf8283 2019-07-24 stsp return err;
6261 0ebf8283 2019-07-24 stsp const struct got_error *
6262 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6263 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6264 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6265 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6266 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6267 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6269 0ebf8283 2019-07-24 stsp const struct got_error *err;
6270 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6271 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6273 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6275 0ebf8283 2019-07-24 stsp return err;
6277 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6279 0ebf8283 2019-07-24 stsp goto done;
6281 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6282 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6284 0ebf8283 2019-07-24 stsp if (commit_ref)
6285 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6286 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6287 0ebf8283 2019-07-24 stsp return err;
6290 0ebf8283 2019-07-24 stsp const struct got_error *
6291 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6292 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6294 3e3a69f1 2019-07-25 stsp if (fileindex)
6295 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6296 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6299 69844fba 2019-07-11 stsp static const struct got_error *
6300 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6302 69844fba 2019-07-11 stsp const struct got_error *err;
6303 69844fba 2019-07-11 stsp struct got_reference *ref;
6305 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6306 69844fba 2019-07-11 stsp if (err) {
6307 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6308 69844fba 2019-07-11 stsp return NULL;
6309 69844fba 2019-07-11 stsp return err;
6312 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6313 69844fba 2019-07-11 stsp got_ref_close(ref);
6314 69844fba 2019-07-11 stsp return err;
6317 69844fba 2019-07-11 stsp static const struct got_error *
6318 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6320 69844fba 2019-07-11 stsp const struct got_error *err;
6321 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6322 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6324 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6326 69844fba 2019-07-11 stsp goto done;
6327 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6329 69844fba 2019-07-11 stsp goto done;
6331 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6333 69844fba 2019-07-11 stsp goto done;
6334 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6336 69844fba 2019-07-11 stsp goto done;
6338 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6340 69844fba 2019-07-11 stsp goto done;
6341 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6343 69844fba 2019-07-11 stsp goto done;
6345 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6347 69844fba 2019-07-11 stsp goto done;
6348 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6350 69844fba 2019-07-11 stsp goto done;
6353 69844fba 2019-07-11 stsp free(tmp_branch_name);
6354 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6355 69844fba 2019-07-11 stsp free(branch_ref_name);
6356 69844fba 2019-07-11 stsp free(commit_ref_name);
6357 69844fba 2019-07-11 stsp return err;
6360 818c7501 2019-07-11 stsp const struct got_error *
6361 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
6362 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6363 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6364 818c7501 2019-07-11 stsp struct got_repository *repo)
6366 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
6367 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
6369 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6371 818c7501 2019-07-11 stsp return err;
6373 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6375 818c7501 2019-07-11 stsp goto done;
6377 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
6379 818c7501 2019-07-11 stsp goto done;
6381 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
6383 818c7501 2019-07-11 stsp goto done;
6385 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
6387 3e3a69f1 2019-07-25 stsp if (fileindex)
6388 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6389 818c7501 2019-07-11 stsp free(new_head_commit_id);
6390 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6391 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6392 818c7501 2019-07-11 stsp err = unlockerr;
6393 818c7501 2019-07-11 stsp return err;
6396 818c7501 2019-07-11 stsp const struct got_error *
6397 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
6398 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6399 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
6400 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6402 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
6403 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
6404 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
6405 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6406 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6407 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
6409 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6411 818c7501 2019-07-11 stsp return err;
6413 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
6414 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
6416 818c7501 2019-07-11 stsp goto done;
6418 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
6420 818c7501 2019-07-11 stsp goto done;
6423 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
6424 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
6425 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
6427 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
6429 818c7501 2019-07-11 stsp goto done;
6431 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6433 818c7501 2019-07-11 stsp goto done;
6435 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
6436 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
6438 ca355955 2019-07-12 stsp goto done;
6440 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
6442 ca355955 2019-07-12 stsp goto done;
6444 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6446 818c7501 2019-07-11 stsp goto done;
6448 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6449 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6450 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6451 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6452 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6453 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6454 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6455 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6456 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6458 1f1abb7e 2019-08-08 stsp goto sync;
6460 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6461 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
6463 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6464 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
6465 ca355955 2019-07-12 stsp err = sync_err;
6467 818c7501 2019-07-11 stsp got_ref_close(resolved);
6468 a3a2faf2 2019-07-12 stsp free(tree_id);
6469 818c7501 2019-07-11 stsp free(commit_id);
6470 0ebf8283 2019-07-24 stsp if (fileindex)
6471 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
6472 0ebf8283 2019-07-24 stsp free(fileindex_path);
6474 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6475 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6476 0ebf8283 2019-07-24 stsp err = unlockerr;
6477 0ebf8283 2019-07-24 stsp return err;
6480 0ebf8283 2019-07-24 stsp const struct got_error *
6481 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6482 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6483 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
6484 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6486 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6487 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6488 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
6489 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
6490 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6491 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
6492 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
6493 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6495 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6496 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6497 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6498 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6500 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6502 0ebf8283 2019-07-24 stsp return err;
6504 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6506 0ebf8283 2019-07-24 stsp goto done;
6508 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
6509 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
6510 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6513 0ebf8283 2019-07-24 stsp goto done;
6515 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6517 0ebf8283 2019-07-24 stsp goto done;
6519 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6521 0ebf8283 2019-07-24 stsp goto done;
6523 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6524 0ebf8283 2019-07-24 stsp worktree);
6526 0ebf8283 2019-07-24 stsp goto done;
6528 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6531 0ebf8283 2019-07-24 stsp goto done;
6533 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6535 0ebf8283 2019-07-24 stsp goto done;
6537 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
6539 0ebf8283 2019-07-24 stsp goto done;
6541 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6542 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6544 0ebf8283 2019-07-24 stsp goto done;
6545 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
6547 0ebf8283 2019-07-24 stsp goto done;
6548 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6549 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
6550 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
6551 0ebf8283 2019-07-24 stsp goto done;
6554 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6555 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6557 0ebf8283 2019-07-24 stsp goto done;
6558 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
6560 0ebf8283 2019-07-24 stsp goto done;
6562 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6564 0ebf8283 2019-07-24 stsp goto done;
6566 0ebf8283 2019-07-24 stsp free(fileindex_path);
6567 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6568 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6569 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6570 0ebf8283 2019-07-24 stsp if (wt_branch)
6571 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
6572 0ebf8283 2019-07-24 stsp if (err) {
6573 0ebf8283 2019-07-24 stsp if (*branch_ref) {
6574 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
6575 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6577 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6578 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6579 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6581 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6582 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6583 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6584 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6586 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
6588 0ebf8283 2019-07-24 stsp return err;
6591 0ebf8283 2019-07-24 stsp const struct got_error *
6592 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
6593 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6595 3e3a69f1 2019-07-25 stsp if (fileindex)
6596 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6597 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
6600 0ebf8283 2019-07-24 stsp const struct got_error *
6601 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
6602 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
6604 0ebf8283 2019-07-24 stsp const struct got_error *err;
6605 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6607 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6609 0ebf8283 2019-07-24 stsp return err;
6611 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6612 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6613 0ebf8283 2019-07-24 stsp return NULL;
6616 0ebf8283 2019-07-24 stsp const struct got_error *
6617 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
6618 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
6619 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6620 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6622 0ebf8283 2019-07-24 stsp const struct got_error *err;
6623 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6624 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6625 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6626 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6627 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6628 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6630 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6631 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6632 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6633 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6635 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6637 3e3a69f1 2019-07-25 stsp return err;
6639 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6641 f032f1f7 2019-08-04 stsp goto done;
6643 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6644 f032f1f7 2019-08-04 stsp &have_staged_files);
6645 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6646 f032f1f7 2019-08-04 stsp goto done;
6647 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6648 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6649 3e3a69f1 2019-07-25 stsp goto done;
6652 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6654 f032f1f7 2019-08-04 stsp goto done;
6656 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6658 0ebf8283 2019-07-24 stsp goto done;
6660 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6662 0ebf8283 2019-07-24 stsp goto done;
6664 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6665 0ebf8283 2019-07-24 stsp worktree);
6667 0ebf8283 2019-07-24 stsp goto done;
6669 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6671 0ebf8283 2019-07-24 stsp goto done;
6673 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6675 0ebf8283 2019-07-24 stsp goto done;
6676 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6678 0ebf8283 2019-07-24 stsp goto done;
6680 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6682 0ebf8283 2019-07-24 stsp goto done;
6683 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6685 0ebf8283 2019-07-24 stsp goto done;
6687 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6689 0ebf8283 2019-07-24 stsp goto done;
6691 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6692 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6693 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6694 0ebf8283 2019-07-24 stsp if (commit_ref)
6695 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6696 0ebf8283 2019-07-24 stsp if (base_commit_ref)
6697 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
6698 0ebf8283 2019-07-24 stsp if (err) {
6699 0ebf8283 2019-07-24 stsp free(*commit_id);
6700 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6701 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6702 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6703 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6704 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6705 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6707 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6708 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6709 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6711 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
6713 0ebf8283 2019-07-24 stsp return err;
6716 0ebf8283 2019-07-24 stsp static const struct got_error *
6717 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6719 0ebf8283 2019-07-24 stsp const struct got_error *err;
6720 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6721 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6723 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6725 0ebf8283 2019-07-24 stsp goto done;
6726 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
6728 0ebf8283 2019-07-24 stsp goto done;
6730 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6731 0ebf8283 2019-07-24 stsp worktree);
6733 0ebf8283 2019-07-24 stsp goto done;
6734 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
6736 0ebf8283 2019-07-24 stsp goto done;
6738 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6740 0ebf8283 2019-07-24 stsp goto done;
6741 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
6743 0ebf8283 2019-07-24 stsp goto done;
6745 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6747 0ebf8283 2019-07-24 stsp goto done;
6748 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
6750 0ebf8283 2019-07-24 stsp goto done;
6752 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6753 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6754 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6755 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6756 0ebf8283 2019-07-24 stsp return err;
6759 0ebf8283 2019-07-24 stsp const struct got_error *
6760 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
6761 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6762 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
6763 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6765 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
6766 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6767 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6768 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
6769 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6771 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6773 0ebf8283 2019-07-24 stsp return err;
6775 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
6776 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
6778 0ebf8283 2019-07-24 stsp goto done;
6780 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
6782 0ebf8283 2019-07-24 stsp goto done;
6784 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6786 0ebf8283 2019-07-24 stsp goto done;
6788 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6789 0ebf8283 2019-07-24 stsp worktree->path_prefix);
6791 0ebf8283 2019-07-24 stsp goto done;
6793 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
6795 0ebf8283 2019-07-24 stsp goto done;
6797 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6799 0ebf8283 2019-07-24 stsp goto done;
6801 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6802 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6803 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6804 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6805 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6806 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6807 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6808 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
6809 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6811 1f1abb7e 2019-08-08 stsp goto sync;
6813 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6814 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
6816 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6817 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
6818 0ebf8283 2019-07-24 stsp err = sync_err;
6820 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
6821 0ebf8283 2019-07-24 stsp free(tree_id);
6822 818c7501 2019-07-11 stsp free(fileindex_path);
6824 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6825 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6826 818c7501 2019-07-11 stsp err = unlockerr;
6827 818c7501 2019-07-11 stsp return err;
6830 0ebf8283 2019-07-24 stsp const struct got_error *
6831 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
6832 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6833 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
6835 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr;
6836 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
6837 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6839 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6841 0ebf8283 2019-07-24 stsp return err;
6843 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
6844 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
6846 0ebf8283 2019-07-24 stsp goto done;
6848 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
6850 0ebf8283 2019-07-24 stsp goto done;
6852 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
6854 0ebf8283 2019-07-24 stsp goto done;
6856 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
6858 0ebf8283 2019-07-24 stsp goto done;
6860 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
6862 3e3a69f1 2019-07-25 stsp if (fileindex)
6863 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6864 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
6865 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6866 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6867 0ebf8283 2019-07-24 stsp err = unlockerr;
6868 0ebf8283 2019-07-24 stsp return err;
6871 0ebf8283 2019-07-24 stsp const struct got_error *
6872 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6873 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6875 0ebf8283 2019-07-24 stsp const struct got_error *err;
6876 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6878 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6880 0ebf8283 2019-07-24 stsp return err;
6882 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6884 0ebf8283 2019-07-24 stsp goto done;
6886 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
6888 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6889 2822a352 2019-10-15 stsp return err;
6892 2822a352 2019-10-15 stsp const struct got_error *
6893 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6894 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6895 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
6896 2822a352 2019-10-15 stsp struct got_repository *repo)
6898 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
6899 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
6900 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
6902 2822a352 2019-10-15 stsp *fileindex = NULL;
6903 2822a352 2019-10-15 stsp *branch_ref = NULL;
6904 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
6906 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
6908 2822a352 2019-10-15 stsp return err;
6910 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6911 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
6912 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
6913 2822a352 2019-10-15 stsp "update -b or different branch name required");
6914 2822a352 2019-10-15 stsp goto done;
6917 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6919 2822a352 2019-10-15 stsp goto done;
6921 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
6922 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
6923 2822a352 2019-10-15 stsp ok_arg.repo = repo;
6924 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6927 2822a352 2019-10-15 stsp goto done;
6929 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
6931 2822a352 2019-10-15 stsp goto done;
6933 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
6934 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
6936 2822a352 2019-10-15 stsp if (err) {
6937 2822a352 2019-10-15 stsp if (*branch_ref) {
6938 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
6939 2822a352 2019-10-15 stsp *branch_ref = NULL;
6941 2822a352 2019-10-15 stsp if (*base_branch_ref) {
6942 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
6943 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
6945 2822a352 2019-10-15 stsp if (*fileindex) {
6946 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
6947 2822a352 2019-10-15 stsp *fileindex = NULL;
6949 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
6951 0cb83759 2019-08-03 stsp return err;
6954 2822a352 2019-10-15 stsp const struct got_error *
6955 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
6956 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6957 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6958 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6959 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6961 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
6962 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
6963 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
6965 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
6967 2822a352 2019-10-15 stsp goto done;
6969 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
6971 2822a352 2019-10-15 stsp goto done;
6973 2822a352 2019-10-15 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
6974 2822a352 2019-10-15 stsp worktree->path_prefix);
6976 2822a352 2019-10-15 stsp goto done;
6978 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6980 2822a352 2019-10-15 stsp goto done;
6982 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6983 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
6985 2822a352 2019-10-15 stsp goto sync;
6987 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
6989 2822a352 2019-10-15 stsp goto sync;
6991 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
6993 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6994 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
6995 2822a352 2019-10-15 stsp err = sync_err;
6998 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
6999 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7000 2822a352 2019-10-15 stsp err = unlockerr;
7001 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7003 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7004 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7005 2822a352 2019-10-15 stsp err = unlockerr;
7006 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7008 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7009 2822a352 2019-10-15 stsp free(fileindex_path);
7010 2822a352 2019-10-15 stsp free(tree_id);
7012 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7013 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7014 2822a352 2019-10-15 stsp err = unlockerr;
7015 2822a352 2019-10-15 stsp return err;
7018 2822a352 2019-10-15 stsp const struct got_error *
7019 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7020 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7021 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7023 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7025 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7027 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7029 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7030 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7031 8b692cd0 2019-10-21 stsp err = unlockerr;
7032 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7034 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7035 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7036 8b692cd0 2019-10-21 stsp err = unlockerr;
7037 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7039 8b692cd0 2019-10-21 stsp return err;
7042 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
7043 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
7044 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7045 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7046 2db2652d 2019-08-07 stsp struct got_repository *repo;
7047 2db2652d 2019-08-07 stsp int have_changes;
7050 2db2652d 2019-08-07 stsp const struct got_error *
7051 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
7052 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7053 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7054 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7056 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
7057 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
7058 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
7059 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
7060 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
7061 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
7063 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
7064 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
7065 8b13ce36 2019-08-08 stsp return NULL;
7066 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
7067 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
7069 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7070 735ef5ac 2019-08-03 stsp if (ie == NULL)
7071 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7073 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7074 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7075 735ef5ac 2019-08-03 stsp relpath) == -1)
7076 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
7078 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
7079 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
7080 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7081 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
7084 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
7085 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7086 3aa5969e 2019-08-06 stsp goto done;
7087 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
7088 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
7089 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
7090 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7091 735ef5ac 2019-08-03 stsp goto done;
7094 2db2652d 2019-08-07 stsp a->have_changes = 1;
7096 735ef5ac 2019-08-03 stsp p = in_repo_path;
7097 735ef5ac 2019-08-03 stsp while (p[0] == '/')
7099 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
7100 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
7101 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
7103 735ef5ac 2019-08-03 stsp free(in_repo_path);
7104 dc424a06 2019-08-07 stsp return err;
7107 2db2652d 2019-08-07 stsp struct stage_path_arg {
7108 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7109 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7110 2db2652d 2019-08-07 stsp struct got_repository *repo;
7111 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
7112 2db2652d 2019-08-07 stsp void *status_arg;
7113 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
7114 2db2652d 2019-08-07 stsp void *patch_arg;
7115 7b5dc508 2019-10-28 stsp int staged_something;
7116 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
7119 2db2652d 2019-08-07 stsp static const struct got_error *
7120 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
7121 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7122 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7123 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7125 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
7126 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
7127 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
7128 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
7129 0cb83759 2019-08-03 stsp uint32_t stage;
7130 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
7131 0aeb8099 2020-07-23 stsp struct stat sb;
7133 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
7134 8b13ce36 2019-08-08 stsp return NULL;
7136 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7137 d3e7c587 2019-08-03 stsp if (ie == NULL)
7138 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7140 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7141 2db2652d 2019-08-07 stsp relpath)== -1)
7142 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
7144 0cb83759 2019-08-03 stsp switch (status) {
7145 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
7146 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
7147 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
7148 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
7149 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
7152 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7153 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
7154 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7155 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7156 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
7159 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
7162 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
7163 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
7164 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
7165 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
7166 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
7170 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
7171 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
7174 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7175 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7176 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7177 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
7179 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
7180 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7181 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
7182 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
7183 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
7184 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
7185 35213c7c 2020-07-23 stsp ssize_t target_len;
7186 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
7187 35213c7c 2020-07-23 stsp sizeof(target_path));
7188 35213c7c 2020-07-23 stsp if (target_len == -1) {
7189 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
7190 35213c7c 2020-07-23 stsp ondisk_path);
7193 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
7194 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
7195 35213c7c 2020-07-23 stsp a->worktree->root_path);
7198 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
7199 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
7200 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
7204 35213c7c 2020-07-23 stsp if (is_bad_symlink)
7205 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7206 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
7208 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7209 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
7211 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7212 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
7214 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7215 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7217 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7218 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
7219 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
7221 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
7222 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
7224 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7225 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7226 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
7227 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
7230 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7232 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7233 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7237 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
7238 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7239 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7240 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7242 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7243 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7246 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
7248 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
7249 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7251 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
7252 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
7255 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7259 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
7260 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
7261 dc424a06 2019-08-07 stsp free(path_content);
7262 2db2652d 2019-08-07 stsp free(ondisk_path);
7263 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
7264 0ebf8283 2019-07-24 stsp return err;
7267 0cb83759 2019-08-03 stsp const struct got_error *
7268 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
7269 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
7270 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
7271 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7272 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
7274 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7275 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
7276 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7277 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
7278 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
7279 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
7280 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
7281 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
7283 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7285 0cb83759 2019-08-03 stsp return err;
7287 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
7288 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
7290 735ef5ac 2019-08-03 stsp goto done;
7291 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7293 735ef5ac 2019-08-03 stsp goto done;
7294 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7296 0cb83759 2019-08-03 stsp goto done;
7298 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
7299 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
7300 2db2652d 2019-08-07 stsp oka.worktree = worktree;
7301 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
7302 2db2652d 2019-08-07 stsp oka.repo = repo;
7303 2db2652d 2019-08-07 stsp oka.have_changes = 0;
7304 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7305 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7306 f2a9dc41 2019-12-13 tracey check_stage_ok, &oka, NULL, NULL, 0, 0);
7308 735ef5ac 2019-08-03 stsp goto done;
7310 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
7311 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7312 2db2652d 2019-08-07 stsp goto done;
7315 2db2652d 2019-08-07 stsp spa.worktree = worktree;
7316 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
7317 2db2652d 2019-08-07 stsp spa.repo = repo;
7318 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
7319 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
7320 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
7321 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
7322 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
7323 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
7324 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7325 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7326 f2a9dc41 2019-12-13 tracey stage_path, &spa, NULL, NULL, 0, 0);
7328 2db2652d 2019-08-07 stsp goto done;
7330 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
7331 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7332 7b5dc508 2019-10-28 stsp goto done;
7335 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7336 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
7337 0cb83759 2019-08-03 stsp err = sync_err;
7339 735ef5ac 2019-08-03 stsp if (head_ref)
7340 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
7341 735ef5ac 2019-08-03 stsp free(head_commit_id);
7342 0cb83759 2019-08-03 stsp free(fileindex_path);
7343 0cb83759 2019-08-03 stsp if (fileindex)
7344 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
7345 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7346 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
7347 0cb83759 2019-08-03 stsp err = unlockerr;
7348 0cb83759 2019-08-03 stsp return err;
7351 ad493afc 2019-08-03 stsp struct unstage_path_arg {
7352 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
7353 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
7354 ad493afc 2019-08-03 stsp struct got_repository *repo;
7355 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
7356 ad493afc 2019-08-03 stsp void *progress_arg;
7357 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
7358 2e1f37b0 2019-08-08 stsp void *patch_arg;
7361 2e1f37b0 2019-08-08 stsp static const struct got_error *
7362 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
7363 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
7364 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
7365 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
7366 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
7368 2e1f37b0 2019-08-08 stsp const struct got_error *err;
7369 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
7370 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7371 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7372 2e1f37b0 2019-08-08 stsp struct stat sb1, sb2;
7373 2e1f37b0 2019-08-08 stsp struct got_diff_changes *changes = NULL;
7374 2e1f37b0 2019-08-08 stsp struct got_diff_state *ds = NULL;
7375 2e1f37b0 2019-08-08 stsp struct got_diff_args *args = NULL;
7376 2e1f37b0 2019-08-08 stsp struct got_diff_change *change;
7377 2e1f37b0 2019-08-08 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7378 2e1f37b0 2019-08-08 stsp int have_content = 0, have_rejected_content = 0;
7380 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7381 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7383 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
7385 2e1f37b0 2019-08-08 stsp return err;
7386 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7388 2e1f37b0 2019-08-08 stsp goto done;
7390 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7392 2e1f37b0 2019-08-08 stsp goto done;
7394 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7396 2e1f37b0 2019-08-08 stsp goto done;
7398 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7400 2e1f37b0 2019-08-08 stsp goto done;
7402 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7404 2e1f37b0 2019-08-08 stsp goto done;
7406 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7408 2e1f37b0 2019-08-08 stsp goto done;
7410 2e1f37b0 2019-08-08 stsp if (stat(path1, &sb1) == -1) {
7411 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path1);
7412 2e1f37b0 2019-08-08 stsp goto done;
7415 2e1f37b0 2019-08-08 stsp if (stat(path2, &sb2) == -1) {
7416 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path2);
7417 2e1f37b0 2019-08-08 stsp goto done;
7420 2e1f37b0 2019-08-08 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
7421 2e1f37b0 2019-08-08 stsp f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7423 2e1f37b0 2019-08-08 stsp goto done;
7425 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
7426 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
7428 2e1f37b0 2019-08-08 stsp goto done;
7429 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
7430 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
7432 2e1f37b0 2019-08-08 stsp goto done;
7434 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
7435 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
7436 2e1f37b0 2019-08-08 stsp goto done;
7438 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
7439 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
7440 2e1f37b0 2019-08-08 stsp goto done;
7442 2e1f37b0 2019-08-08 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7443 2e1f37b0 2019-08-08 stsp int choice;
7444 2e1f37b0 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
7445 2e1f37b0 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
7446 2e1f37b0 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
7447 2e1f37b0 2019-08-08 stsp outfile, rejectfile, patch_cb, patch_arg);
7449 2e1f37b0 2019-08-08 stsp goto done;
7450 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
7451 2e1f37b0 2019-08-08 stsp have_content = 1;
7453 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
7454 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
7457 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
7458 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7459 f1e81a05 2019-08-10 stsp outfile, rejectfile);
7461 2e1f37b0 2019-08-08 stsp free(label1);
7463 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
7464 2e1f37b0 2019-08-08 stsp if (staged_blob)
7465 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
7466 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
7467 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
7468 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
7469 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
7470 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
7471 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
7472 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7473 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
7474 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
7475 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
7476 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
7477 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
7478 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
7479 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
7480 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
7481 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7482 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
7483 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
7484 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7486 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
7487 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
7488 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
7489 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7490 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
7491 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
7492 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7494 2e1f37b0 2019-08-08 stsp free(args);
7496 2e1f37b0 2019-08-08 stsp got_diff_state_free(ds);
7499 2e1f37b0 2019-08-08 stsp if (changes)
7500 2e1f37b0 2019-08-08 stsp got_diff_free_changes(changes);
7501 2e1f37b0 2019-08-08 stsp free(path1);
7502 2e1f37b0 2019-08-08 stsp free(path2);
7503 fda8017d 2020-07-23 stsp return err;
7506 fda8017d 2020-07-23 stsp static const struct got_error *
7507 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
7508 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
7509 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7510 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
7511 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
7512 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7513 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7515 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
7516 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
7517 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
7518 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
7519 fda8017d 2020-07-23 stsp FILE *f = NULL;
7520 fda8017d 2020-07-23 stsp struct stat sb;
7522 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
7523 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
7524 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
7526 fda8017d 2020-07-23 stsp return err;
7528 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
7529 fda8017d 2020-07-23 stsp return NULL;
7531 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
7532 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
7533 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
7535 fda8017d 2020-07-23 stsp goto done;
7538 fda8017d 2020-07-23 stsp f = fopen(path_unstaged_content, "r");
7539 fda8017d 2020-07-23 stsp if (f == NULL) {
7540 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
7541 fda8017d 2020-07-23 stsp path_unstaged_content);
7542 fda8017d 2020-07-23 stsp goto done;
7544 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
7545 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
7546 fda8017d 2020-07-23 stsp goto done;
7548 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
7549 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7550 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
7552 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
7553 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
7554 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
7555 fda8017d 2020-07-23 stsp goto done;
7557 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
7558 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7559 fda8017d 2020-07-23 stsp goto done;
7561 fda8017d 2020-07-23 stsp link_target[r] = '\0';
7562 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
7563 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
7564 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
7565 fda8017d 2020-07-23 stsp progress_arg);
7567 fda8017d 2020-07-23 stsp int local_changes_subsumed;
7568 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
7569 fda8017d 2020-07-23 stsp blob_base, ondisk_path, ie->path,
7570 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
7571 fda8017d 2020-07-23 stsp path_unstaged_content, label_orig, "unstaged",
7572 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
7575 fda8017d 2020-07-23 stsp goto done;
7577 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
7578 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7579 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
7581 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7583 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
7584 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
7585 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
7586 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
7587 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
7588 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
7589 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
7590 fda8017d 2020-07-23 stsp if (f && fclose(f) != 0 && err == NULL)
7591 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
7592 fda8017d 2020-07-23 stsp free(path_unstaged_content);
7593 fda8017d 2020-07-23 stsp free(path_new_staged_content);
7594 2e1f37b0 2019-08-08 stsp return err;
7597 ad493afc 2019-08-03 stsp static const struct got_error *
7598 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
7599 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
7600 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7601 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7603 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
7604 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
7605 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
7606 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7607 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
7608 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
7609 ad493afc 2019-08-03 stsp int local_changes_subsumed;
7610 9bc94a15 2019-08-03 stsp struct stat sb;
7612 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
7613 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
7614 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
7615 2e1f37b0 2019-08-08 stsp return NULL;
7617 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7618 ad493afc 2019-08-03 stsp if (ie == NULL)
7619 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7621 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7623 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
7625 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
7626 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
7628 f69721c3 2019-10-21 stsp goto done;
7629 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7630 f69721c3 2019-10-21 stsp id_str) == -1) {
7631 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
7632 f69721c3 2019-10-21 stsp goto done;
7635 ad493afc 2019-08-03 stsp switch (staged_status) {
7636 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
7637 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
7638 ad493afc 2019-08-03 stsp blob_id, 8192);
7641 ad493afc 2019-08-03 stsp /* fall through */
7642 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
7643 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7644 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
7645 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7646 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7647 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7650 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
7653 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
7654 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
7655 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
7656 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
7657 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
7658 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
7661 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
7662 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
7665 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
7666 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
7667 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
7668 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
7669 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
7670 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
7671 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
7672 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
7673 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
7675 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
7676 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7677 36bf999c 2020-07-23 stsp char *staged_target;
7678 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
7679 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
7681 36bf999c 2020-07-23 stsp goto done;
7682 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
7683 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
7684 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
7685 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
7686 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
7687 36bf999c 2020-07-23 stsp free(staged_target);
7689 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
7690 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
7691 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
7692 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
7693 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
7694 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
7695 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
7699 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7702 ad493afc 2019-08-03 stsp if (err == NULL)
7703 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
7704 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
7706 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
7707 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7708 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7709 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7710 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7713 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7715 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7716 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7720 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7721 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
7722 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
7725 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
7729 ad493afc 2019-08-03 stsp free(ondisk_path);
7730 ad493afc 2019-08-03 stsp if (blob_base)
7731 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
7732 ad493afc 2019-08-03 stsp if (blob_staged)
7733 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
7734 f69721c3 2019-10-21 stsp free(id_str);
7735 f69721c3 2019-10-21 stsp free(label_orig);
7736 ad493afc 2019-08-03 stsp return err;
7739 ad493afc 2019-08-03 stsp const struct got_error *
7740 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
7741 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
7742 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7743 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7744 ad493afc 2019-08-03 stsp struct got_repository *repo)
7746 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7747 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
7748 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7749 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
7750 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
7752 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7754 ad493afc 2019-08-03 stsp return err;
7756 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7758 ad493afc 2019-08-03 stsp goto done;
7760 ad493afc 2019-08-03 stsp upa.worktree = worktree;
7761 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
7762 ad493afc 2019-08-03 stsp upa.repo = repo;
7763 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
7764 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
7765 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
7766 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
7767 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7768 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7769 f2a9dc41 2019-12-13 tracey unstage_path, &upa, NULL, NULL, 0, 0);
7771 ad493afc 2019-08-03 stsp goto done;
7774 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7775 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
7776 ad493afc 2019-08-03 stsp err = sync_err;
7778 ad493afc 2019-08-03 stsp free(fileindex_path);
7779 ad493afc 2019-08-03 stsp if (fileindex)
7780 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
7781 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7782 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
7783 ad493afc 2019-08-03 stsp err = unlockerr;
7784 ad493afc 2019-08-03 stsp return err;
7787 b2118c49 2020-07-28 stsp struct report_file_info_arg {
7788 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
7789 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
7790 b2118c49 2020-07-28 stsp void *info_arg;
7791 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
7792 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
7793 b2118c49 2020-07-28 stsp void *cancel_arg;
7796 b2118c49 2020-07-28 stsp static const struct got_error *
7797 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
7799 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
7800 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
7801 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
7802 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7803 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
7804 b2118c49 2020-07-28 stsp int stage;
7806 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7807 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
7809 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
7810 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7811 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
7814 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
7815 b2118c49 2020-07-28 stsp return NULL;
7817 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_blob(ie)) {
7818 b2118c49 2020-07-28 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7819 b2118c49 2020-07-28 stsp blob_idp = &blob_id;
7821 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
7822 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7823 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
7824 b2118c49 2020-07-28 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7825 b2118c49 2020-07-28 stsp SHA1_DIGEST_LENGTH);
7826 b2118c49 2020-07-28 stsp staged_blob_idp = &staged_blob_id;
7829 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_commit(ie)) {
7830 b2118c49 2020-07-28 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
7831 b2118c49 2020-07-28 stsp commit_idp = &commit_id;
7834 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
7835 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
7838 b2118c49 2020-07-28 stsp const struct got_error *
7839 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
7840 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
7841 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
7842 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7845 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
7846 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
7847 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
7848 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
7850 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
7852 b2118c49 2020-07-28 stsp return err;
7854 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7856 b2118c49 2020-07-28 stsp goto done;
7858 b2118c49 2020-07-28 stsp arg.worktree = worktree;
7859 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
7860 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
7861 b2118c49 2020-07-28 stsp arg.paths = paths;
7862 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
7863 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
7864 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
7867 b2118c49 2020-07-28 stsp free(fileindex_path);
7868 b2118c49 2020-07-28 stsp if (fileindex)
7869 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
7870 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
7871 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
7872 b2118c49 2020-07-28 stsp err = unlockerr;
7873 b2118c49 2020-07-28 stsp return err;