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;
458 247140b2 2019-02-05 stsp err = open_worktree(worktree, path);
459 f02eaa22 2019-03-11 stsp if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
460 247140b2 2019-02-05 stsp return err;
461 247140b2 2019-02-05 stsp if (*worktree)
462 247140b2 2019-02-05 stsp return NULL;
463 247140b2 2019-02-05 stsp path = dirname(path);
464 d1542a27 2019-02-05 stsp if (path == NULL)
465 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
466 d1542a27 2019-02-05 stsp } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
468 247140b2 2019-02-05 stsp return got_error(GOT_ERR_NOT_WORKTREE);
471 3a6ce05a 2019-02-11 stsp const struct got_error *
472 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
474 3a6ce05a 2019-02-11 stsp const struct got_error *err = NULL;
475 cde76477 2018-03-11 stsp free(worktree->repo_path);
476 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
477 eaccb85f 2018-12-25 stsp free(worktree->base_commit_id);
478 36a38700 2019-05-10 stsp free(worktree->head_ref_name);
479 056e7441 2018-03-11 stsp if (worktree->lockfd != -1)
480 3a6ce05a 2019-02-11 stsp if (close(worktree->lockfd) != 0)
481 638f9024 2019-05-13 stsp err = got_error_from_errno2("close",
482 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree));
483 7f11502c 2019-08-28 hiltjo free(worktree->root_path);
484 50b0790e 2020-09-11 stsp free(worktree->gotconfig_path);
485 50b0790e 2020-09-11 stsp got_gotconfig_free(worktree->gotconfig);
486 6d9d28c3 2018-03-11 stsp free(worktree);
487 3a6ce05a 2019-02-11 stsp return err;
490 2fbdb5ae 2018-12-29 stsp const char *
491 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
493 c7f4312f 2019-02-05 stsp return worktree->root_path;
496 c7f4312f 2019-02-05 stsp const char *
497 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
499 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
501 49520a32 2018-12-29 stsp const char *
502 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
504 f609be2e 2018-12-29 stsp return worktree->path_prefix;
507 e5dc7198 2018-12-29 stsp const struct got_error *
508 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
509 e5dc7198 2018-12-29 stsp const char *path_prefix)
511 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
513 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
514 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
515 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
517 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
518 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
519 e5dc7198 2018-12-29 stsp free(absprefix);
520 e5dc7198 2018-12-29 stsp return NULL;
523 bc70eb79 2019-05-09 stsp const char *
524 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
526 36a38700 2019-05-10 stsp return worktree->head_ref_name;
529 024e9686 2019-05-14 stsp const struct got_error *
530 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
531 024e9686 2019-05-14 stsp struct got_reference *head_ref)
533 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
534 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
536 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
537 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
538 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
539 024e9686 2019-05-14 stsp path_got = NULL;
543 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
544 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
545 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
549 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
553 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
554 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
556 024e9686 2019-05-14 stsp free(path_got);
558 024e9686 2019-05-14 stsp free(head_ref_name);
559 024e9686 2019-05-14 stsp return err;
562 b72f483a 2019-02-05 stsp struct got_object_id *
563 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
565 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
568 507dc3bb 2018-12-29 stsp const struct got_error *
569 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
570 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
572 507dc3bb 2018-12-29 stsp const struct got_error *err;
573 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
574 507dc3bb 2018-12-29 stsp char *id_str = NULL;
575 507dc3bb 2018-12-29 stsp char *path_got = NULL;
577 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
578 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
579 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
580 507dc3bb 2018-12-29 stsp path_got = NULL;
584 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
586 507dc3bb 2018-12-29 stsp return err;
588 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
589 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
593 507dc3bb 2018-12-29 stsp /* Record our base commit. */
594 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
597 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
601 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
602 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
603 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
604 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
609 507dc3bb 2018-12-29 stsp got_object_close(obj);
610 507dc3bb 2018-12-29 stsp free(id_str);
611 507dc3bb 2018-12-29 stsp free(path_got);
612 507dc3bb 2018-12-29 stsp return err;
615 50b0790e 2020-09-11 stsp const struct got_gotconfig *
616 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
618 50b0790e 2020-09-11 stsp return worktree->gotconfig;
621 9d31a1d8 2018-03-11 stsp static const struct got_error *
622 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
624 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
625 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
626 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
627 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
628 86c3caaf 2018-03-09 stsp return NULL;
631 21908da4 2019-01-13 stsp static const struct got_error *
632 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
634 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
635 4a1ddfc2 2019-01-12 stsp char *abspath;
637 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
638 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
640 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
641 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
642 ddcd8544 2019-03-11 stsp struct stat sb;
643 ddcd8544 2019-03-11 stsp err = NULL;
644 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
645 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
646 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
647 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
648 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
651 4a1ddfc2 2019-01-12 stsp free(abspath);
652 68c76935 2019-02-19 stsp return err;
655 68c76935 2019-02-19 stsp static const struct got_error *
656 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
658 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
659 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
660 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
661 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
666 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
667 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
668 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
671 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
672 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
673 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
676 68c76935 2019-02-19 stsp if (flen1 == 0) {
677 68c76935 2019-02-19 stsp if (flen2 != 0)
680 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
681 68c76935 2019-02-19 stsp if (flen1 != 0)
684 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
685 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
695 68c76935 2019-02-19 stsp return err;
698 68c76935 2019-02-19 stsp static const struct got_error *
699 68c76935 2019-02-19 stsp check_files_equal(int *same, const char *f1_path, const char *f2_path)
701 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
702 68c76935 2019-02-19 stsp struct stat sb;
703 68c76935 2019-02-19 stsp size_t size1, size2;
704 68c76935 2019-02-19 stsp FILE *f1 = NULL, *f2 = NULL;
708 68c76935 2019-02-19 stsp if (lstat(f1_path, &sb) != 0) {
709 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f1_path);
712 68c76935 2019-02-19 stsp size1 = sb.st_size;
714 68c76935 2019-02-19 stsp if (lstat(f2_path, &sb) != 0) {
715 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f2_path);
718 68c76935 2019-02-19 stsp size2 = sb.st_size;
720 68c76935 2019-02-19 stsp if (size1 != size2) {
722 68c76935 2019-02-19 stsp return NULL;
725 68c76935 2019-02-19 stsp f1 = fopen(f1_path, "r");
726 68c76935 2019-02-19 stsp if (f1 == NULL)
727 60522982 2019-12-15 stsp return got_error_from_errno2("fopen", f1_path);
729 68c76935 2019-02-19 stsp f2 = fopen(f2_path, "r");
730 68c76935 2019-02-19 stsp if (f2 == NULL) {
731 60522982 2019-12-15 stsp err = got_error_from_errno2("fopen", f2_path);
735 68c76935 2019-02-19 stsp err = check_file_contents_equal(same, f1, f2);
737 68c76935 2019-02-19 stsp if (f1 && fclose(f1) != 0 && err == NULL)
738 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
739 68c76935 2019-02-19 stsp if (f2 && fclose(f2) != 0 && err == NULL)
740 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
742 6353ad76 2019-02-08 stsp return err;
746 46b6ee73 2019-06-04 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
747 14c901f1 2019-08-08 stsp * the file at deriv_path acts as the first derived version, and the
748 14c901f1 2019-08-08 stsp * file on disk acts as the second derived version.
750 6353ad76 2019-02-08 stsp static const struct got_error *
751 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
752 46b6ee73 2019-06-04 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
753 14c901f1 2019-08-08 stsp const char *path, uint16_t st_mode, const char *deriv_path,
754 f69721c3 2019-10-21 stsp const char *label_orig, const char *label_deriv,
755 f69721c3 2019-10-21 stsp struct got_repository *repo,
756 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
758 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
759 6353ad76 2019-02-08 stsp int merged_fd = -1;
760 14c901f1 2019-08-08 stsp FILE *f_orig = NULL;
761 14c901f1 2019-08-08 stsp char *blob_orig_path = NULL;
762 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
763 234035bc 2019-06-01 stsp int overlapcnt = 0;
764 af54ae4a 2019-02-19 stsp char *parent;
765 3b9f0f87 2020-07-23 stsp char *symlink_path = NULL;
766 3b9f0f87 2020-07-23 stsp FILE *symlinkf = NULL;
768 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
770 af54ae4a 2019-02-19 stsp parent = dirname(ondisk_path);
771 af54ae4a 2019-02-19 stsp if (parent == NULL)
772 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", ondisk_path);
774 af54ae4a 2019-02-19 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1)
775 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
777 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
781 af54ae4a 2019-02-19 stsp free(base_path);
782 46b6ee73 2019-06-04 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
783 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
784 af54ae4a 2019-02-19 stsp base_path = NULL;
788 46b6ee73 2019-06-04 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
791 46b6ee73 2019-06-04 stsp if (blob_orig) {
792 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
793 46b6ee73 2019-06-04 stsp blob_orig);
798 1430b4e0 2019-03-27 stsp * If the file has no blob, this is an "add vs add" conflict,
799 1430b4e0 2019-03-27 stsp * and we simply use an empty ancestor file to make both files
800 1430b4e0 2019-03-27 stsp * appear in the merged result in their entirety.
805 3b9f0f87 2020-07-23 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
806 3b9f0f87 2020-07-23 stsp * target path into a temporary file and use that file with diff3.
808 3b9f0f87 2020-07-23 stsp if (S_ISLNK(st_mode)) {
809 3b9f0f87 2020-07-23 stsp char target_path[PATH_MAX];
810 3b9f0f87 2020-07-23 stsp ssize_t target_len;
813 3b9f0f87 2020-07-23 stsp free(base_path);
814 3b9f0f87 2020-07-23 stsp if (asprintf(&base_path, "%s/got-symlink-merge",
815 3b9f0f87 2020-07-23 stsp parent) == -1) {
816 3b9f0f87 2020-07-23 stsp err = got_error_from_errno("asprintf");
817 3b9f0f87 2020-07-23 stsp base_path = NULL;
820 3b9f0f87 2020-07-23 stsp err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
823 3b9f0f87 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
824 3b9f0f87 2020-07-23 stsp sizeof(target_path));
825 3b9f0f87 2020-07-23 stsp if (target_len == -1) {
826 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
829 3b9f0f87 2020-07-23 stsp n = fwrite(target_path, 1, target_len, symlinkf);
830 3b9f0f87 2020-07-23 stsp if (n != target_len) {
831 3b9f0f87 2020-07-23 stsp err = got_ferror(symlinkf, GOT_ERR_IO);
834 3b9f0f87 2020-07-23 stsp if (fflush(symlinkf) == EOF) {
835 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("fflush", symlink_path);
840 14c901f1 2019-08-08 stsp err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
841 3b9f0f87 2020-07-23 stsp blob_orig_path, symlink_path ? symlink_path : ondisk_path,
842 3b9f0f87 2020-07-23 stsp label_deriv, label_orig, NULL);
846 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
847 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
851 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
852 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
856 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
857 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
858 14c901f1 2019-08-08 stsp err = check_files_equal(local_changes_subsumed, deriv_path,
859 68c76935 2019-02-19 stsp merged_path);
864 2ad902c0 2019-12-15 stsp if (fchmod(merged_fd, st_mode) != 0) {
865 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
869 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
870 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
871 230a42bd 2019-05-11 jcs ondisk_path);
876 fdcb7daf 2019-12-15 stsp if (merged_path)
877 fdcb7daf 2019-12-15 stsp unlink(merged_path);
879 3b9f0f87 2020-07-23 stsp if (symlink_path) {
880 3b9f0f87 2020-07-23 stsp if (unlink(symlink_path) == -1 && err == NULL)
881 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("unlink", symlink_path);
883 3b9f0f87 2020-07-23 stsp if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
884 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("fclose", symlink_path);
885 3b9f0f87 2020-07-23 stsp free(symlink_path);
886 3a6ce05a 2019-02-11 stsp if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
887 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
888 46b6ee73 2019-06-04 stsp if (f_orig && fclose(f_orig) != 0 && err == NULL)
889 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
890 6353ad76 2019-02-08 stsp free(merged_path);
891 af54ae4a 2019-02-19 stsp free(base_path);
892 46b6ee73 2019-06-04 stsp if (blob_orig_path) {
893 46b6ee73 2019-06-04 stsp unlink(blob_orig_path);
894 46b6ee73 2019-06-04 stsp free(blob_orig_path);
896 af57b12a 2020-07-23 stsp return err;
899 af57b12a 2020-07-23 stsp static const struct got_error *
900 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
901 af57b12a 2020-07-23 stsp size_t target_len)
903 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
904 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
905 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
906 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
907 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
908 af57b12a 2020-07-23 stsp ondisk_path);
909 af57b12a 2020-07-23 stsp return NULL;
913 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
914 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
915 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
916 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
918 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
919 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
921 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
922 993e2a1b 2020-07-23 stsp * has deleted this symlink.
924 11cc08c1 2020-07-23 stsp static const struct got_error *
925 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
926 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
927 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
929 11cc08c1 2020-07-23 stsp const struct got_error *err;
930 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
931 11cc08c1 2020-07-23 stsp FILE *f = NULL;
933 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
935 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
937 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
938 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
939 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
943 11cc08c1 2020-07-23 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict");
947 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
948 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
949 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
950 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
951 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
952 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
953 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
954 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
955 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
956 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
960 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
961 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
964 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
965 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
968 11cc08c1 2020-07-23 stsp if (chmod(ondisk_path, GOT_DEFAULT_FILE_MODE) == -1) {
969 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("chmod", ondisk_path);
973 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
974 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
975 11cc08c1 2020-07-23 stsp free(path);
976 11cc08c1 2020-07-23 stsp free(id_str);
977 11cc08c1 2020-07-23 stsp free(label_deriv);
978 11cc08c1 2020-07-23 stsp return err;
981 d219f183 2020-07-23 stsp /* forward declaration */
982 d219f183 2020-07-23 stsp static const struct got_error *
983 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
984 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
985 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
986 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
989 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
990 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
991 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
992 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
994 af57b12a 2020-07-23 stsp static const struct got_error *
995 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
996 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
997 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
998 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
999 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1001 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
1002 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
1003 af57b12a 2020-07-23 stsp struct stat sb;
1004 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
1005 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
1006 11cc08c1 2020-07-23 stsp int have_local_change = 0;
1007 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
1009 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
1010 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
1012 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
1013 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
1014 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
1015 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
1016 af57b12a 2020-07-23 stsp ondisk_path);
1017 af57b12a 2020-07-23 stsp goto done;
1019 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
1021 526a746f 2020-07-23 stsp if (blob_orig) {
1022 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1024 526a746f 2020-07-23 stsp goto done;
1027 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
1028 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
1029 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1030 11cc08c1 2020-07-23 stsp have_local_change = 1;
1032 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
1033 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
1034 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
1035 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1036 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
1038 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
1039 11cc08c1 2020-07-23 stsp if (ancestor_target) {
1040 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
1041 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1043 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
1044 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1045 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
1046 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1049 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
1050 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1051 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
1052 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
1054 11cc08c1 2020-07-23 stsp goto done;
1055 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1058 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
1059 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
1060 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
1061 af57b12a 2020-07-23 stsp strlen(deriv_target));
1063 af57b12a 2020-07-23 stsp goto done;
1064 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1065 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
1066 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
1067 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1068 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
1069 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1072 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1073 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
1074 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
1076 ea7786be 2020-07-23 stsp goto done;
1077 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1083 af57b12a 2020-07-23 stsp free(ancestor_target);
1084 14c901f1 2019-08-08 stsp return err;
1088 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
1089 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
1090 14c901f1 2019-08-08 stsp * acts as the second derived version.
1092 14c901f1 2019-08-08 stsp static const struct got_error *
1093 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1094 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
1095 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
1096 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
1097 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1098 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1100 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
1101 14c901f1 2019-08-08 stsp FILE *f_deriv = NULL;
1102 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1103 14c901f1 2019-08-08 stsp char *label_deriv = NULL, *parent;
1105 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
1107 14c901f1 2019-08-08 stsp parent = dirname(ondisk_path);
1108 14c901f1 2019-08-08 stsp if (parent == NULL)
1109 14c901f1 2019-08-08 stsp return got_error_from_errno2("dirname", ondisk_path);
1111 14c901f1 2019-08-08 stsp free(base_path);
1112 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1113 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1114 14c901f1 2019-08-08 stsp base_path = NULL;
1115 14c901f1 2019-08-08 stsp goto done;
1118 14c901f1 2019-08-08 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1120 14c901f1 2019-08-08 stsp goto done;
1121 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1122 14c901f1 2019-08-08 stsp blob_deriv);
1124 14c901f1 2019-08-08 stsp goto done;
1126 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1128 14c901f1 2019-08-08 stsp goto done;
1129 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1130 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1131 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1132 14c901f1 2019-08-08 stsp goto done;
1135 14c901f1 2019-08-08 stsp err = merge_file(local_changes_subsumed, worktree, blob_orig,
1136 f69721c3 2019-10-21 stsp ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1137 f69721c3 2019-10-21 stsp label_deriv, repo, progress_cb, progress_arg);
1139 14c901f1 2019-08-08 stsp if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1140 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1141 14c901f1 2019-08-08 stsp free(base_path);
1142 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1143 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1144 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1146 6353ad76 2019-02-08 stsp free(id_str);
1147 818c7501 2019-07-11 stsp free(label_deriv);
1148 4a1ddfc2 2019-01-12 stsp return err;
1151 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1152 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1153 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1154 65b05cec 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_object_id *blob_id)
1156 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1157 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1159 65b05cec 2020-07-23 stsp *new_iep = NULL;
1161 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1163 054041d0 2020-06-24 stsp return err;
1165 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(new_ie, ondisk_path,
1166 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1168 054041d0 2020-06-24 stsp goto done;
1170 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1173 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1175 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1176 13d9040b 2019-03-27 stsp return err;
1179 1ebedb77 2019-10-19 stsp static mode_t
1180 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1182 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1184 1ebedb77 2019-10-19 stsp if (executable) {
1185 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1186 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1187 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1188 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1189 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1190 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1193 1ebedb77 2019-10-19 stsp return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1196 8ba819a3 2020-07-23 stsp /* forward declaration */
1197 13d9040b 2019-03-27 stsp static const struct got_error *
1198 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1199 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1200 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1201 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1202 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1203 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1206 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1207 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1209 8ba819a3 2020-07-23 stsp static const struct got_error *
1210 5a1fbc73 2020-07-23 stsp replace_existing_symlink(const char *ondisk_path, const char *target_path,
1211 5a1fbc73 2020-07-23 stsp size_t target_len)
1213 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1214 5a1fbc73 2020-07-23 stsp ssize_t elen;
1215 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1219 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1220 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1221 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1222 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1223 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1224 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1226 5a1fbc73 2020-07-23 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1227 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1228 5a1fbc73 2020-07-23 stsp if (errno != ELOOP)
1229 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1231 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1232 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1233 5a1fbc73 2020-07-23 stsp if (elen == -1)
1234 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1236 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1237 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1238 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1241 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1242 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1243 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1244 5a1fbc73 2020-07-23 stsp return err;
1247 3c1ec782 2020-07-23 stsp static const struct got_error *
1248 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1249 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1251 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1252 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1253 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1255 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1257 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1258 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1259 3c1ec782 2020-07-23 stsp return NULL;
1263 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1264 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1265 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1266 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1267 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1269 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1270 3c1ec782 2020-07-23 stsp char *abspath;
1271 3c1ec782 2020-07-23 stsp char *parent = dirname(ondisk_path);
1272 3c1ec782 2020-07-23 stsp if (parent == NULL)
1273 3c1ec782 2020-07-23 stsp return got_error_from_errno2("dirname", ondisk_path);
1274 3c1ec782 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1)
1275 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1276 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1277 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1278 3c1ec782 2020-07-23 stsp free(abspath);
1279 3c1ec782 2020-07-23 stsp return err;
1281 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1282 3c1ec782 2020-07-23 stsp free(abspath);
1284 3c1ec782 2020-07-23 stsp return err;
1286 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1288 3c1ec782 2020-07-23 stsp return err;
1291 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1292 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1293 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1294 3c1ec782 2020-07-23 stsp return NULL;
1297 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1298 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1299 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1300 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1301 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1302 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1304 3c1ec782 2020-07-23 stsp free(path_got);
1305 3c1ec782 2020-07-23 stsp return NULL;
1308 5a1fbc73 2020-07-23 stsp static const struct got_error *
1309 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1310 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1311 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1312 c90c8ce3 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1313 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1315 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1316 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1317 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1318 906c123b 2020-07-23 stsp char *path_got = NULL;
1319 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1320 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1322 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1325 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1326 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1327 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1328 8ba819a3 2020-07-23 stsp * in the blob object.
1331 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1332 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1333 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1334 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1335 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1336 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1337 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1338 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1339 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1340 3b9f0f87 2020-07-23 stsp progress_arg);
1342 8ba819a3 2020-07-23 stsp if (len > 0) {
1343 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1344 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1345 8ba819a3 2020-07-23 stsp len - hdrlen);
1346 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1347 8ba819a3 2020-07-23 stsp hdrlen = 0;
1349 8ba819a3 2020-07-23 stsp } while (len != 0);
1350 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1352 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1353 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1355 3c1ec782 2020-07-23 stsp return err;
1357 3c1ec782 2020-07-23 stsp if (*is_bad_symlink) {
1358 906c123b 2020-07-23 stsp /* install as a regular file */
1359 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1360 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1361 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1362 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1363 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1364 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1365 906c123b 2020-07-23 stsp goto done;
1368 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1369 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1370 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1371 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1372 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1373 c90c8ce3 2020-07-23 stsp goto done;
1375 5a1fbc73 2020-07-23 stsp err = replace_existing_symlink(ondisk_path,
1376 5a1fbc73 2020-07-23 stsp target_path, target_len);
1378 f35fa46a 2020-07-23 stsp goto done;
1379 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1380 5a1fbc73 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1381 6e1eade5 2020-07-23 stsp reverting_versioned_file ?
1382 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1385 5a1fbc73 2020-07-23 stsp goto done; /* Nothing else to do. */
1388 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1389 f35fa46a 2020-07-23 stsp char *parent = dirname(ondisk_path);
1390 f35fa46a 2020-07-23 stsp if (parent == NULL) {
1391 f35fa46a 2020-07-23 stsp err = got_error_from_errno2("dirname",
1392 f35fa46a 2020-07-23 stsp ondisk_path);
1393 f35fa46a 2020-07-23 stsp goto done;
1395 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1397 f35fa46a 2020-07-23 stsp goto done;
1399 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1400 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1402 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1403 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1404 f35fa46a 2020-07-23 stsp goto done;
1408 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1409 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1410 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1411 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1412 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1413 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1414 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1415 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1416 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1417 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1418 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1419 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1420 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1422 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1423 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1425 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1426 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1427 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1429 906c123b 2020-07-23 stsp free(path_got);
1430 8ba819a3 2020-07-23 stsp return err;
1433 8ba819a3 2020-07-23 stsp static const struct got_error *
1434 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1435 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1436 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1437 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1438 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1439 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1441 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1442 507dc3bb 2018-12-29 stsp int fd = -1;
1443 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1444 507dc3bb 2018-12-29 stsp int update = 0;
1445 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1447 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1448 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
1449 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1450 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1451 21908da4 2019-01-13 stsp char *parent = dirname(path);
1452 21908da4 2019-01-13 stsp if (parent == NULL)
1453 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
1454 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1456 21908da4 2019-01-13 stsp return err;
1457 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1458 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1459 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
1460 21908da4 2019-01-13 stsp if (fd == -1)
1461 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1462 230a42bd 2019-05-11 jcs ondisk_path);
1463 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1464 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1465 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1466 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1467 3b9f0f87 2020-07-23 stsp goto done;
1469 bd6aa359 2020-07-23 stsp if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1470 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1471 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1472 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1473 507dc3bb 2018-12-29 stsp goto done;
1475 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1476 507dc3bb 2018-12-29 stsp ondisk_path);
1478 507dc3bb 2018-12-29 stsp goto done;
1479 507dc3bb 2018-12-29 stsp update = 1;
1482 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1485 bd6aa359 2020-07-23 stsp if (progress_cb) {
1486 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1487 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1489 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1490 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1493 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1494 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1496 bd6aa359 2020-07-23 stsp goto done;
1499 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1501 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1502 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1505 9d31a1d8 2018-03-11 stsp if (len > 0) {
1506 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1507 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1508 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1509 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1510 b87c6f83 2018-12-24 stsp goto done;
1511 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1512 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1513 b87c6f83 2018-12-24 stsp goto done;
1515 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1517 9d31a1d8 2018-03-11 stsp } while (len != 0);
1519 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1520 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1521 816dc654 2019-02-16 stsp goto done;
1524 507dc3bb 2018-12-29 stsp if (update) {
1525 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1526 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1527 230a42bd 2019-05-11 jcs ondisk_path);
1528 2a57020b 2019-02-20 stsp unlink(tmppath);
1529 68ed9ba5 2019-02-10 stsp goto done;
1533 1ebedb77 2019-10-19 stsp if (chmod(ondisk_path,
1534 1ebedb77 2019-10-19 stsp get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1535 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("chmod", ondisk_path);
1536 1ebedb77 2019-10-19 stsp goto done;
1540 3a6ce05a 2019-02-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
1541 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1542 507dc3bb 2018-12-29 stsp free(tmppath);
1543 6353ad76 2019-02-08 stsp return err;
1546 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1547 6353ad76 2019-02-08 stsp static const struct got_error *
1548 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1550 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1551 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1552 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1553 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1554 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1556 7154f6ce 2019-03-27 stsp int i = 0;
1557 7154f6ce 2019-03-27 stsp char *line;
1558 7154f6ce 2019-03-27 stsp size_t len;
1559 7154f6ce 2019-03-27 stsp const char delim[3] = {'\0', '\0', '\0'};
1561 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1562 7154f6ce 2019-03-27 stsp line = fparseln(f, &len, NULL, delim, 0);
1563 7154f6ce 2019-03-27 stsp if (line == NULL) {
1564 7154f6ce 2019-03-27 stsp if (feof(f))
1566 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1570 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1571 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1573 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1579 7154f6ce 2019-03-27 stsp return err;
1582 e2b1e152 2019-07-13 stsp static int
1583 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1585 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1586 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1589 1ebedb77 2019-10-19 stsp static int
1590 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1592 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1593 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1594 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1595 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1596 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1597 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1600 c363b2c1 2019-08-03 stsp static unsigned char
1601 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1603 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1604 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1605 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1606 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1607 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1608 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1609 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1611 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1615 a919d5c4 2020-07-23 stsp static const struct got_error *
1616 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1617 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1618 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1620 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1621 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1622 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1623 a919d5c4 2020-07-23 stsp ssize_t elen;
1624 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1625 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1626 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1628 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1630 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1632 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1634 a919d5c4 2020-07-23 stsp return err;
1635 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1637 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1638 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1640 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1642 a919d5c4 2020-07-23 stsp if (len > 0) {
1643 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1644 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1645 a919d5c4 2020-07-23 stsp len - hdrlen);
1646 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1647 a919d5c4 2020-07-23 stsp hdrlen = 0;
1649 a919d5c4 2020-07-23 stsp } while (len != 0);
1650 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1652 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1653 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1654 a919d5c4 2020-07-23 stsp if (elen == -1)
1655 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1657 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1658 a919d5c4 2020-07-23 stsp if (elen == -1)
1659 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1662 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1663 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1665 a919d5c4 2020-07-23 stsp return NULL;
1668 7154f6ce 2019-03-27 stsp static const struct got_error *
1669 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1670 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1671 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1673 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1674 6353ad76 2019-02-08 stsp struct got_object_id id;
1675 6353ad76 2019-02-08 stsp size_t hdrlen;
1676 1338848f 2019-12-13 stsp int fd = -1;
1677 6353ad76 2019-02-08 stsp FILE *f = NULL;
1678 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1679 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1680 6353ad76 2019-02-08 stsp size_t flen, blen;
1681 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1683 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1686 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1687 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1688 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1690 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1691 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1692 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1693 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1694 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1696 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1697 882ef1b9 2019-12-13 stsp goto done;
1699 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1700 3d35a492 2019-12-13 stsp goto done;
1703 7f91a133 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1704 a919d5c4 2020-07-23 stsp if (fd == -1 && errno != ENOENT && errno != ELOOP)
1705 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1706 a919d5c4 2020-07-23 stsp else if (fd == -1 && errno == ELOOP) {
1707 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1708 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1709 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1710 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1711 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1712 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1714 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1715 3d35a492 2019-12-13 stsp goto done;
1717 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1718 1338848f 2019-12-13 stsp goto done;
1722 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1723 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1724 1338848f 2019-12-13 stsp goto done;
1727 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1728 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1729 1338848f 2019-12-13 stsp goto done;
1730 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1731 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1732 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1733 1338848f 2019-12-13 stsp goto done;
1736 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1737 f179e66d 2020-07-23 stsp goto done;
1739 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1740 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1741 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1742 1338848f 2019-12-13 stsp goto done;
1745 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1746 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1747 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1749 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1751 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1753 1338848f 2019-12-13 stsp goto done;
1755 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1756 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1757 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1758 a919d5c4 2020-07-23 stsp goto done;
1761 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1762 3d35a492 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1763 ab0d4361 2019-12-13 stsp if (fd == -1) {
1764 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1765 ab0d4361 2019-12-13 stsp goto done;
1769 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1770 6353ad76 2019-02-08 stsp if (f == NULL) {
1771 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1772 6353ad76 2019-02-08 stsp goto done;
1775 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1777 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1778 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1780 7154f6ce 2019-03-27 stsp goto done;
1781 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1782 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1783 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1784 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1785 7154f6ce 2019-03-27 stsp goto done;
1787 6353ad76 2019-02-08 stsp if (blen == 0) {
1788 6353ad76 2019-02-08 stsp if (flen != 0)
1789 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1791 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1792 6353ad76 2019-02-08 stsp if (blen != 0)
1793 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1795 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1796 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1797 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1798 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1802 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1805 6353ad76 2019-02-08 stsp hdrlen = 0;
1808 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1809 7154f6ce 2019-03-27 stsp rewind(f);
1810 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1811 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1812 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1815 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1816 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1817 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1818 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1819 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1820 9d31a1d8 2018-03-11 stsp return err;
1824 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1825 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1827 e2b1e152 2019-07-13 stsp static const struct got_error *
1828 e2b1e152 2019-07-13 stsp sync_timestamps(char *ondisk_path, unsigned char status,
1829 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1831 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1832 e2b1e152 2019-07-13 stsp return got_fileindex_entry_update(ie, ondisk_path,
1833 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1835 e2b1e152 2019-07-13 stsp return NULL;
1838 9d31a1d8 2018-03-11 stsp static const struct got_error *
1839 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1840 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1841 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1842 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1843 0584f854 2019-04-06 stsp void *progress_arg)
1845 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1846 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1847 6353ad76 2019-02-08 stsp char *ondisk_path;
1848 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1849 b8f41171 2019-02-10 stsp struct stat sb;
1851 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1852 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1855 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1856 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1857 a76c42e6 2019-08-03 stsp goto done;
1859 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1862 abb4604f 2019-07-27 stsp goto done;
1863 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1864 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1866 abb4604f 2019-07-27 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1867 c90c8ce3 2020-07-23 stsp status = GOT_STATUS_UNVERSIONED;
1870 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1871 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1872 b8f41171 2019-02-10 stsp goto done;
1874 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1875 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1877 5036ab18 2020-04-18 stsp goto done;
1880 523b8417 2019-10-19 stsp if (ie && status != GOT_STATUS_MISSING &&
1881 523b8417 2019-10-19 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1882 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1883 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1884 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1885 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1887 e2b1e152 2019-07-13 stsp goto done;
1888 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1890 6353ad76 2019-02-08 stsp goto done;
1892 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1893 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1894 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1895 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1896 b8f41171 2019-02-10 stsp goto done;
1900 56e0773d 2019-11-28 stsp err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1902 6353ad76 2019-02-08 stsp goto done;
1904 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1905 234035bc 2019-06-01 stsp int update_timestamps;
1906 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1907 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1908 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1909 234035bc 2019-06-01 stsp struct got_object_id id2;
1910 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1911 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1913 f69721c3 2019-10-21 stsp goto done;
1915 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
1916 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
1917 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1918 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
1919 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
1920 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
1921 f69721c3 2019-10-21 stsp goto done;
1923 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
1924 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
1925 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
1926 234035bc 2019-06-01 stsp goto done;
1929 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1930 36bf999c 2020-07-23 stsp char *link_target;
1931 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
1933 36bf999c 2020-07-23 stsp goto done;
1934 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
1935 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
1936 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
1937 36bf999c 2020-07-23 stsp free(link_target);
1939 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1940 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
1941 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
1942 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
1944 f69721c3 2019-10-21 stsp free(label_orig);
1945 234035bc 2019-06-01 stsp if (blob2)
1946 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1948 909d120e 2019-09-22 stsp goto done;
1950 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1951 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1952 234035bc 2019-06-01 stsp * unmodified files again.
1954 234035bc 2019-06-01 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1955 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1956 234035bc 2019-06-01 stsp update_timestamps);
1957 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
1958 1ebedb77 2019-10-19 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1959 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1960 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1961 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1963 1ee397ad 2019-07-12 stsp goto done;
1964 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1965 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1967 13d9040b 2019-03-27 stsp goto done;
1969 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
1970 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
1971 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
1972 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
1973 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
1974 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
1975 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
1977 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1978 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
1979 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
1980 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
1981 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
1984 13d9040b 2019-03-27 stsp goto done;
1987 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1988 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 1);
1990 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
1991 054041d0 2020-06-24 stsp worktree->base_commit_id, ondisk_path, path,
1992 054041d0 2020-06-24 stsp &blob->id);
1995 13d9040b 2019-03-27 stsp goto done;
1997 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
1998 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
1999 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2002 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2004 6353ad76 2019-02-08 stsp free(ondisk_path);
2005 8da9e5f4 2019-01-12 stsp return err;
2008 90285c3b 2019-01-08 stsp static const struct got_error *
2009 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2011 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2012 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
2014 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2015 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2017 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2018 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2019 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2021 90285c3b 2019-01-08 stsp char *parent = dirname(ondisk_path);
2022 7a9df742 2019-01-08 stsp while (parent && strcmp(parent, root_path) != 0) {
2023 26c4ac4d 2019-01-08 stsp if (rmdir(parent) == -1) {
2024 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2025 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2029 90285c3b 2019-01-08 stsp parent = dirname(parent);
2032 90285c3b 2019-01-08 stsp free(ondisk_path);
2033 90285c3b 2019-01-08 stsp return err;
2036 708d8e67 2019-03-27 stsp static const struct got_error *
2037 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2038 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2039 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2041 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2042 708d8e67 2019-03-27 stsp unsigned char status;
2043 708d8e67 2019-03-27 stsp struct stat sb;
2044 708d8e67 2019-03-27 stsp char *ondisk_path;
2046 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2047 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2049 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2051 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2053 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2055 5a58a424 2020-06-23 stsp goto done;
2057 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2058 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2059 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2060 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2061 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2062 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2063 993e2a1b 2020-07-23 stsp goto done;
2065 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2066 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2067 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2068 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2070 993e2a1b 2020-07-23 stsp goto done;
2071 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2072 993e2a1b 2020-07-23 stsp ie->path);
2073 993e2a1b 2020-07-23 stsp goto done;
2076 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2077 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2078 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2080 5a58a424 2020-06-23 stsp goto done;
2082 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2083 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2085 fc6346c4 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2088 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2090 5a58a424 2020-06-23 stsp goto done;
2091 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2092 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2094 5a58a424 2020-06-23 stsp goto done;
2096 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2099 5a58a424 2020-06-23 stsp free(ondisk_path);
2100 fc6346c4 2019-03-27 stsp return err;
2103 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2104 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2105 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2106 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2107 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2108 8da9e5f4 2019-01-12 stsp void *progress_arg;
2109 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2110 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2113 512f0d0e 2019-01-02 stsp static const struct got_error *
2114 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2115 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2117 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2119 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2120 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2122 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2123 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2126 8da9e5f4 2019-01-12 stsp static const struct got_error *
2127 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2129 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2131 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2132 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2134 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2135 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2138 9d31a1d8 2018-03-11 stsp static const struct got_error *
2139 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2141 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2142 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2143 8da9e5f4 2019-01-12 stsp char *path;
2145 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2146 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2148 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2149 63c5ca5d 2019-08-24 stsp return NULL;
2151 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2152 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2154 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2156 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2157 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2159 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2160 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2162 8da9e5f4 2019-01-12 stsp free(path);
2163 0cd1c46a 2019-03-11 stsp return err;
2166 b2118c49 2020-07-28 stsp const struct got_error *
2167 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2169 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2171 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2172 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2173 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2174 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2177 b2118c49 2020-07-28 stsp return NULL;
2180 818c7501 2019-07-11 stsp static const struct got_error *
2181 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2183 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2184 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2186 517bab73 2019-03-11 stsp *refname = NULL;
2188 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2190 b2118c49 2020-07-28 stsp return err;
2192 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2193 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2194 0647c563 2019-03-11 stsp *refname = NULL;
2196 517bab73 2019-03-11 stsp free(uuidstr);
2197 517bab73 2019-03-11 stsp return err;
2200 818c7501 2019-07-11 stsp const struct got_error *
2201 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2203 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2206 818c7501 2019-07-11 stsp static const struct got_error *
2207 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2209 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2210 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2213 818c7501 2019-07-11 stsp static const struct got_error *
2214 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2216 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2219 818c7501 2019-07-11 stsp static const struct got_error *
2220 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2222 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2223 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2226 818c7501 2019-07-11 stsp static const struct got_error *
2227 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2229 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2230 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2233 0ebf8283 2019-07-24 stsp static const struct got_error *
2234 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2236 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2237 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2240 0ebf8283 2019-07-24 stsp static const struct got_error *
2241 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2243 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2244 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2247 0ebf8283 2019-07-24 stsp static const struct got_error *
2248 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2250 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2251 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2254 0ebf8283 2019-07-24 stsp static const struct got_error *
2255 0ebf8283 2019-07-24 stsp get_histedit_commit_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_COMMIT_REF_PREFIX);
2261 0ebf8283 2019-07-24 stsp const struct got_error *
2262 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2263 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2265 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2266 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2267 0ebf8283 2019-07-24 stsp *path = NULL;
2268 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2270 0ebf8283 2019-07-24 stsp return NULL;
2274 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2275 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2277 517bab73 2019-03-11 stsp static const struct got_error *
2278 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2280 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2281 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2282 517bab73 2019-03-11 stsp char *refname;
2284 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2286 517bab73 2019-03-11 stsp return err;
2288 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2290 0cd1c46a 2019-03-11 stsp goto done;
2292 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2294 0cd1c46a 2019-03-11 stsp free(refname);
2296 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2297 3e3a69f1 2019-07-25 stsp return err;
2300 3e3a69f1 2019-07-25 stsp static const struct got_error *
2301 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2303 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2305 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2306 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2307 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2308 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2310 9d31a1d8 2018-03-11 stsp return err;
2314 ebf99748 2019-05-09 stsp static const struct got_error *
2315 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2316 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2318 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2319 ebf99748 2019-05-09 stsp FILE *index = NULL;
2321 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2322 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2323 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2324 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2326 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2328 ebf99748 2019-05-09 stsp goto done;
2330 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
2331 ebf99748 2019-05-09 stsp if (index == NULL) {
2332 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2333 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2335 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2336 ebf99748 2019-05-09 stsp if (fclose(index) != 0 && err == NULL)
2337 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2340 ebf99748 2019-05-09 stsp if (err) {
2341 ebf99748 2019-05-09 stsp free(*fileindex_path);
2342 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2343 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2344 ebf99748 2019-05-09 stsp *fileindex = NULL;
2346 ebf99748 2019-05-09 stsp return err;
2349 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2350 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2351 c932eeeb 2019-05-22 stsp const char *path;
2352 c932eeeb 2019-05-22 stsp size_t path_len;
2353 c932eeeb 2019-05-22 stsp const char *entry_name;
2354 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2355 a484d721 2019-06-10 stsp void *progress_arg;
2358 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2359 c932eeeb 2019-05-22 stsp static const struct got_error *
2360 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2362 1ee397ad 2019-07-12 stsp const struct got_error *err;
2363 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2365 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2366 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2367 c932eeeb 2019-05-22 stsp return NULL;
2368 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2369 102ff934 2019-06-11 stsp return NULL;
2371 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2372 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2373 c932eeeb 2019-05-22 stsp return NULL;
2375 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2376 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2377 edd02c5e 2019-07-11 stsp ie->path);
2379 1ee397ad 2019-07-12 stsp return err;
2381 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2382 c932eeeb 2019-05-22 stsp return NULL;
2385 9c6338c4 2019-06-02 stsp static const struct got_error *
2386 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2388 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2389 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2390 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2391 867630bb 2020-01-17 stsp struct timespec timeout;
2393 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2394 9c6338c4 2019-06-02 stsp fileindex_path);
2396 9c6338c4 2019-06-02 stsp goto done;
2398 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2400 9c6338c4 2019-06-02 stsp goto done;
2402 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2403 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2404 9c6338c4 2019-06-02 stsp fileindex_path);
2405 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2409 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2410 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2411 867630bb 2020-01-17 stsp * was recorded in the file index.
2413 867630bb 2020-01-17 stsp timeout.tv_sec = 0;
2414 867630bb 2020-01-17 stsp timeout.tv_nsec = 1;
2415 867630bb 2020-01-17 stsp nanosleep(&timeout, NULL);
2417 9c6338c4 2019-06-02 stsp if (new_index)
2418 9c6338c4 2019-06-02 stsp fclose(new_index);
2419 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2420 9c6338c4 2019-06-02 stsp return err;
2423 6ced4176 2019-07-12 stsp static const struct got_error *
2424 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2425 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2426 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
2428 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2429 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2430 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2431 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2433 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2434 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2435 6ced4176 2019-07-12 stsp *tree_id = NULL;
2437 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2438 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2439 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2440 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2441 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2442 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2443 6ced4176 2019-07-12 stsp goto done;
2445 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2446 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
2448 6ced4176 2019-07-12 stsp goto done;
2449 6ced4176 2019-07-12 stsp return NULL;
2452 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2454 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2455 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2456 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2457 6ced4176 2019-07-12 stsp goto done;
2460 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2461 6ced4176 2019-07-12 stsp in_repo_path);
2463 6ced4176 2019-07-12 stsp goto done;
2465 6ced4176 2019-07-12 stsp free(in_repo_path);
2466 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2468 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2470 6ced4176 2019-07-12 stsp goto done;
2472 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2473 6ced4176 2019-07-12 stsp /* Check out a single file. */
2474 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2475 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2476 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2477 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2478 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2479 6ced4176 2019-07-12 stsp goto done;
2481 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2482 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2483 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2484 6ced4176 2019-07-12 stsp goto done;
2487 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2488 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2490 6ced4176 2019-07-12 stsp return err;
2491 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2492 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2493 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2494 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2495 6ced4176 2019-07-12 stsp goto done;
2498 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2499 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
2501 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2502 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2503 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2504 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2505 6ced4176 2019-07-12 stsp goto done;
2507 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2508 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2509 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2510 6ced4176 2019-07-12 stsp goto done;
2515 6ced4176 2019-07-12 stsp free(in_repo_path);
2516 6ced4176 2019-07-12 stsp if (err) {
2517 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2518 6ced4176 2019-07-12 stsp free(*tree_relpath);
2519 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2520 6ced4176 2019-07-12 stsp free(*tree_id);
2521 6ced4176 2019-07-12 stsp *tree_id = NULL;
2523 07ed472d 2019-07-12 stsp return err;
2526 07ed472d 2019-07-12 stsp static const struct got_error *
2527 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2528 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2529 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2530 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2532 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2533 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2534 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2535 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2536 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2538 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2539 7f47418f 2019-12-20 stsp if (err) {
2540 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2541 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2542 7f47418f 2019-12-20 stsp goto done;
2543 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2544 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2546 7f47418f 2019-12-20 stsp return err;
2549 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2550 07ed472d 2019-07-12 stsp worktree->base_commit_id);
2552 07ed472d 2019-07-12 stsp goto done;
2554 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2556 07ed472d 2019-07-12 stsp goto done;
2558 07ed472d 2019-07-12 stsp if (entry_name &&
2559 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2560 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2561 07ed472d 2019-07-12 stsp goto done;
2564 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2565 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2566 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2567 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2568 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2569 07ed472d 2019-07-12 stsp arg.repo = repo;
2570 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2571 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2572 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2573 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2574 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2575 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2578 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2579 07ed472d 2019-07-12 stsp if (commit)
2580 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2581 6ced4176 2019-07-12 stsp return err;
2584 9d31a1d8 2018-03-11 stsp const struct got_error *
2585 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2586 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2587 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2588 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2590 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2591 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2592 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2593 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2594 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2595 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2596 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2597 f2ea84fa 2019-07-27 stsp SIMPLEQ_ENTRY(tree_path_data) entry;
2598 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2599 f2ea84fa 2019-07-27 stsp int entry_type;
2600 f2ea84fa 2019-07-27 stsp char *relpath;
2601 f2ea84fa 2019-07-27 stsp char *entry_name;
2602 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2603 f2ea84fa 2019-07-27 stsp SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2605 f2ea84fa 2019-07-27 stsp SIMPLEQ_INIT(&tree_paths);
2607 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2609 9d31a1d8 2018-03-11 stsp return err;
2611 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2612 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2613 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2614 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2615 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2616 f2ea84fa 2019-07-27 stsp goto done;
2619 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2620 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2621 f2ea84fa 2019-07-27 stsp if (err) {
2622 f2ea84fa 2019-07-27 stsp free(tpd);
2623 6ced4176 2019-07-12 stsp goto done;
2626 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2627 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2628 f2ea84fa 2019-07-27 stsp if (err) {
2629 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2630 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2631 f2ea84fa 2019-07-27 stsp free(tpd);
2632 f2ea84fa 2019-07-27 stsp goto done;
2635 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2637 f2ea84fa 2019-07-27 stsp SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2641 51514078 2018-12-25 stsp * Read the file index.
2642 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2643 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2645 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2647 c4cdcb68 2019-04-03 stsp goto done;
2649 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2650 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2651 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2653 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2654 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2655 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2659 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2660 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2661 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2662 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2663 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2664 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2665 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2666 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2670 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_NEXT(tpd, entry);
2672 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2673 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2674 af12c6b9 2019-06-04 stsp err = sync_err;
2676 9c6338c4 2019-06-02 stsp free(fileindex_path);
2678 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2679 9d31a1d8 2018-03-11 stsp if (commit)
2680 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2681 5ade8233 2019-07-12 stsp if (fileindex)
2682 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2683 f2ea84fa 2019-07-27 stsp while (!SIMPLEQ_EMPTY(&tree_paths)) {
2684 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2685 f2ea84fa 2019-07-27 stsp SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2686 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2687 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2688 f2ea84fa 2019-07-27 stsp free(tpd);
2690 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2691 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2692 9d31a1d8 2018-03-11 stsp err = unlockerr;
2693 f8d1f275 2019-02-04 stsp return err;
2696 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2697 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2698 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2699 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2700 234035bc 2019-06-01 stsp void *progress_arg;
2701 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2702 234035bc 2019-06-01 stsp void *cancel_arg;
2703 f69721c3 2019-10-21 stsp const char *label_orig;
2704 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2707 234035bc 2019-06-01 stsp static const struct got_error *
2708 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2709 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
2710 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
2711 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2713 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2714 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2715 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2716 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2717 234035bc 2019-06-01 stsp struct stat sb;
2718 234035bc 2019-06-01 stsp unsigned char status;
2719 234035bc 2019-06-01 stsp int local_changes_subsumed;
2721 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2722 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2723 d6c87207 2019-08-02 stsp strlen(path2));
2724 1ee397ad 2019-07-12 stsp if (ie == NULL)
2725 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2726 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2728 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2729 234035bc 2019-06-01 stsp path2) == -1)
2730 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2732 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2735 234035bc 2019-06-01 stsp goto done;
2737 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2738 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2739 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2740 234035bc 2019-06-01 stsp goto done;
2742 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2743 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2744 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2745 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2746 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2747 234035bc 2019-06-01 stsp goto done;
2750 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2751 36bf999c 2020-07-23 stsp char *link_target2;
2752 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2754 36bf999c 2020-07-23 stsp goto done;
2755 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2756 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2757 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2758 36bf999c 2020-07-23 stsp free(link_target2);
2760 af57b12a 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
2761 af57b12a 2020-07-23 stsp blob1, ondisk_path, path2, sb.st_mode,
2762 af57b12a 2020-07-23 stsp a->label_orig, blob2, a->commit_id2, repo,
2763 af57b12a 2020-07-23 stsp a->progress_cb, a->progress_arg);
2765 234035bc 2019-06-01 stsp } else if (blob1) {
2766 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2767 d6c87207 2019-08-02 stsp strlen(path1));
2768 1ee397ad 2019-07-12 stsp if (ie == NULL)
2769 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2770 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2772 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2773 2b92fad7 2019-06-02 stsp path1) == -1)
2774 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2776 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2779 2b92fad7 2019-06-02 stsp goto done;
2781 2b92fad7 2019-06-02 stsp switch (status) {
2782 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2783 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2784 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2786 1ee397ad 2019-07-12 stsp goto done;
2787 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2789 2b92fad7 2019-06-02 stsp goto done;
2791 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2793 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2794 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2795 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2796 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2798 1ee397ad 2019-07-12 stsp goto done;
2800 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2802 2b92fad7 2019-06-02 stsp case GOT_STATUS_ADD:
2803 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2804 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2805 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2806 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2808 1ee397ad 2019-07-12 stsp goto done;
2810 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2811 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2813 1ee397ad 2019-07-12 stsp goto done;
2818 234035bc 2019-06-01 stsp } else if (blob2) {
2819 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2820 234035bc 2019-06-01 stsp path2) == -1)
2821 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2822 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2823 d6c87207 2019-08-02 stsp strlen(path2));
2825 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2826 7f91a133 2019-12-13 stsp -1, NULL, repo);
2828 234035bc 2019-06-01 stsp goto done;
2829 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2830 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2831 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2832 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2833 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2834 1ee397ad 2019-07-12 stsp status, path2);
2835 234035bc 2019-06-01 stsp goto done;
2837 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2838 36bf999c 2020-07-23 stsp char *link_target2;
2839 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
2842 36bf999c 2020-07-23 stsp goto done;
2843 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
2844 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
2845 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
2846 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
2847 36bf999c 2020-07-23 stsp free(link_target2);
2848 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
2849 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
2850 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
2851 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
2852 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
2853 526a746f 2020-07-23 stsp a->progress_arg);
2855 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
2856 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
2859 dfe9fba0 2020-07-23 stsp goto done;
2860 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2861 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
2862 054041d0 2020-06-24 stsp ondisk_path, blob2->id.sha1,
2863 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
2865 234035bc 2019-06-01 stsp goto done;
2868 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2869 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2870 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
2871 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
2872 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
2873 c90c8ce3 2020-07-23 stsp 0, 1, repo, a->progress_cb, a->progress_arg);
2875 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
2876 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2877 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
2880 234035bc 2019-06-01 stsp goto done;
2881 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
2883 234035bc 2019-06-01 stsp goto done;
2884 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ondisk_path,
2885 3969253a 2020-03-07 stsp NULL, NULL, 1);
2886 3969253a 2020-03-07 stsp if (err) {
2887 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
2888 3969253a 2020-03-07 stsp goto done;
2890 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
2891 2b92fad7 2019-06-02 stsp if (err) {
2892 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
2893 2b92fad7 2019-06-02 stsp goto done;
2895 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2896 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2897 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2902 234035bc 2019-06-01 stsp free(ondisk_path);
2903 234035bc 2019-06-01 stsp return err;
2906 234035bc 2019-06-01 stsp struct check_merge_ok_arg {
2907 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2908 234035bc 2019-06-01 stsp struct got_repository *repo;
2911 234035bc 2019-06-01 stsp static const struct got_error *
2912 234035bc 2019-06-01 stsp check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2914 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
2915 234035bc 2019-06-01 stsp struct check_merge_ok_arg *a = arg;
2916 234035bc 2019-06-01 stsp unsigned char status;
2917 234035bc 2019-06-01 stsp struct stat sb;
2918 234035bc 2019-06-01 stsp char *ondisk_path;
2920 234035bc 2019-06-01 stsp /* Reject merges into a work tree with mixed base commits. */
2921 234035bc 2019-06-01 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2922 234035bc 2019-06-01 stsp SHA1_DIGEST_LENGTH))
2923 234035bc 2019-06-01 stsp return got_error(GOT_ERR_MIXED_COMMITS);
2925 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2927 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2929 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
2930 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2932 234035bc 2019-06-01 stsp return err;
2933 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
2934 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
2936 234035bc 2019-06-01 stsp return NULL;
2939 818c7501 2019-07-11 stsp static const struct got_error *
2940 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2941 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
2942 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
2943 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2944 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2946 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
2947 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2948 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2949 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
2950 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2952 03415a1a 2019-06-02 stsp if (commit_id1) {
2953 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2954 03415a1a 2019-06-02 stsp worktree->path_prefix);
2955 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
2956 03415a1a 2019-06-02 stsp goto done;
2958 69d57f3d 2020-07-31 stsp if (tree_id1) {
2959 69d57f3d 2020-07-31 stsp char *id_str;
2961 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2963 03415a1a 2019-06-02 stsp goto done;
2965 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
2967 f69721c3 2019-10-21 stsp goto done;
2969 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2970 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2971 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2972 f69721c3 2019-10-21 stsp free(id_str);
2973 f69721c3 2019-10-21 stsp goto done;
2975 f69721c3 2019-10-21 stsp free(id_str);
2978 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2979 234035bc 2019-06-01 stsp worktree->path_prefix);
2981 234035bc 2019-06-01 stsp goto done;
2983 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2985 234035bc 2019-06-01 stsp goto done;
2987 234035bc 2019-06-01 stsp arg.worktree = worktree;
2988 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
2989 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
2990 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
2991 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
2992 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
2993 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
2994 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
2995 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2996 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2997 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2998 af12c6b9 2019-06-04 stsp err = sync_err;
3000 234035bc 2019-06-01 stsp if (tree1)
3001 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3002 234035bc 2019-06-01 stsp if (tree2)
3003 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3004 f69721c3 2019-10-21 stsp free(label_orig);
3005 818c7501 2019-07-11 stsp return err;
3008 818c7501 2019-07-11 stsp const struct got_error *
3009 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3010 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3011 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3012 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3014 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3015 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3016 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3017 818c7501 2019-07-11 stsp struct check_merge_ok_arg mok_arg;
3019 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3021 818c7501 2019-07-11 stsp return err;
3023 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3025 818c7501 2019-07-11 stsp goto done;
3027 818c7501 2019-07-11 stsp mok_arg.worktree = worktree;
3028 818c7501 2019-07-11 stsp mok_arg.repo = repo;
3029 818c7501 2019-07-11 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3030 818c7501 2019-07-11 stsp &mok_arg);
3032 818c7501 2019-07-11 stsp goto done;
3034 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3035 818c7501 2019-07-11 stsp commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3037 818c7501 2019-07-11 stsp if (fileindex)
3038 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3039 818c7501 2019-07-11 stsp free(fileindex_path);
3040 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3041 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3042 234035bc 2019-06-01 stsp err = unlockerr;
3043 234035bc 2019-06-01 stsp return err;
3046 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3047 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3048 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3049 927df6b7 2019-02-10 stsp const char *status_path;
3050 927df6b7 2019-02-10 stsp size_t status_path_len;
3051 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3052 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3053 f8d1f275 2019-02-04 stsp void *status_arg;
3054 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3055 f8d1f275 2019-02-04 stsp void *cancel_arg;
3056 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3057 6841da00 2019-08-08 stsp struct got_pathlist_head ignores;
3058 f2a9dc41 2019-12-13 tracey int report_unchanged;
3059 3143d852 2020-06-25 stsp int no_ignores;
3062 f8d1f275 2019-02-04 stsp static const struct got_error *
3063 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3064 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3065 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3066 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3068 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3069 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3070 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3071 927df6b7 2019-02-10 stsp struct stat sb;
3072 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3073 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3074 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3076 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3078 98eaaa12 2019-08-03 stsp return err;
3080 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3081 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3082 98eaaa12 2019-08-03 stsp return NULL;
3084 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3085 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3086 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3088 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3089 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3090 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3092 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3093 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3094 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3095 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3096 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3099 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3100 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3103 927df6b7 2019-02-10 stsp static const struct got_error *
3104 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3105 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3107 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3108 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3109 f8d1f275 2019-02-04 stsp char *abspath;
3111 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3112 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3114 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3115 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3116 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3117 927df6b7 2019-02-10 stsp return NULL;
3119 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3120 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3121 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3122 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3124 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3125 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3126 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3129 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3130 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3131 f8d1f275 2019-02-04 stsp free(abspath);
3132 9d31a1d8 2018-03-11 stsp return err;
3135 f8d1f275 2019-02-04 stsp static const struct got_error *
3136 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3138 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3139 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3140 2ec1f75b 2019-03-26 stsp unsigned char status;
3142 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3143 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3145 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3146 927df6b7 2019-02-10 stsp return NULL;
3148 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3149 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3150 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3151 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3153 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3154 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3155 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3159 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3161 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3163 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3164 6841da00 2019-08-08 stsp free((char *)pe->path);
3165 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3169 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3171 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3173 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3174 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3175 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3176 6841da00 2019-08-08 stsp free((char *)pe->path);
3178 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3181 6841da00 2019-08-08 stsp static const struct got_error *
3182 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3184 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3185 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3186 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3187 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3188 6841da00 2019-08-08 stsp size_t linesize = 0;
3189 6841da00 2019-08-08 stsp ssize_t linelen;
3191 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3192 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3193 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3194 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3196 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3197 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3198 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3200 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3201 bd8de430 2019-10-04 stsp if (line[0] == '#')
3204 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3205 bd8de430 2019-10-04 stsp if (line[0] == '!')
3208 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3209 6841da00 2019-08-08 stsp line) == -1) {
3210 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3211 6841da00 2019-08-08 stsp goto done;
3213 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3215 6841da00 2019-08-08 stsp goto done;
3217 6841da00 2019-08-08 stsp if (ferror(f)) {
3218 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3219 6841da00 2019-08-08 stsp goto done;
3222 6841da00 2019-08-08 stsp dirpath = strdup(path);
3223 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3224 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3225 6841da00 2019-08-08 stsp goto done;
3227 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3229 6841da00 2019-08-08 stsp free(line);
3230 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3231 6841da00 2019-08-08 stsp free(dirpath);
3232 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3234 6841da00 2019-08-08 stsp return err;
3238 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3240 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3242 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3243 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3244 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3245 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3247 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3248 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3250 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3252 bd8de430 2019-10-04 stsp pattern += 3;
3254 bd8de430 2019-10-04 stsp while (*p) {
3255 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3256 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3257 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3258 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3260 bd8de430 2019-10-04 stsp while (*p == '/')
3270 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3271 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3272 6841da00 2019-08-08 stsp * ignores backwards.
3274 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3275 6841da00 2019-08-08 stsp while (pe) {
3276 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3277 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3278 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3279 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3280 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3281 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3282 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3283 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3284 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3289 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3295 6841da00 2019-08-08 stsp static const struct got_error *
3296 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3297 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3299 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3300 6841da00 2019-08-08 stsp char *ignorespath;
3301 886cec17 2019-12-15 stsp int fd = -1;
3302 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3304 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3305 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3306 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3308 886cec17 2019-12-15 stsp if (dirfd != -1) {
3309 886cec17 2019-12-15 stsp fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3310 886cec17 2019-12-15 stsp if (fd == -1) {
3311 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3312 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3313 886cec17 2019-12-15 stsp ignorespath);
3315 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3316 886cec17 2019-12-15 stsp if (ignoresfile == NULL)
3317 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3318 886cec17 2019-12-15 stsp ignorespath);
3321 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3325 886cec17 2019-12-15 stsp ignoresfile = fopen(ignorespath, "r");
3326 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3327 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3328 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3329 886cec17 2019-12-15 stsp ignorespath);
3331 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3334 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3335 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3336 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3337 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3338 6841da00 2019-08-08 stsp free(ignorespath);
3339 6841da00 2019-08-08 stsp return err;
3342 f8d1f275 2019-02-04 stsp static const struct got_error *
3343 7f91a133 2019-12-13 stsp status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3345 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3346 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3347 f8d1f275 2019-02-04 stsp char *path = NULL;
3349 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3350 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3352 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3353 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3354 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3356 f8d1f275 2019-02-04 stsp path = de->d_name;
3359 3143d852 2020-06-25 stsp if (de->d_type != DT_DIR &&
3360 3143d852 2020-06-25 stsp got_path_is_child(path, a->status_path, a->status_path_len)
3361 6841da00 2019-08-08 stsp && !match_ignores(&a->ignores, path))
3362 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3363 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3364 f8d1f275 2019-02-04 stsp if (parent_path[0])
3365 f8d1f275 2019-02-04 stsp free(path);
3366 b72f483a 2019-02-05 stsp return err;
3369 347d1d3e 2019-07-12 stsp static const struct got_error *
3370 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3372 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3373 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3375 3143d852 2020-06-25 stsp if (a->no_ignores)
3376 3143d852 2020-06-25 stsp return NULL;
3378 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path,
3379 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3381 3143d852 2020-06-25 stsp return err;
3383 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path, path,
3384 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3386 3143d852 2020-06-25 stsp return err;
3389 3143d852 2020-06-25 stsp static const struct got_error *
3390 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3391 abb4604f 2019-07-27 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3392 f2a9dc41 2019-12-13 tracey void *status_arg, struct got_repository *repo, int report_unchanged)
3394 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3395 abb4604f 2019-07-27 stsp struct stat sb;
3397 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3399 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3400 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3402 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3403 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3404 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3405 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3406 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3407 abb4604f 2019-07-27 stsp return NULL;
3410 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3411 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3412 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3414 abb4604f 2019-07-27 stsp return NULL;
3417 3143d852 2020-06-25 stsp static const struct got_error *
3418 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3419 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3421 3143d852 2020-06-25 stsp const struct got_error *err;
3422 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3424 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3425 3143d852 2020-06-25 stsp ".cvsignore");
3427 3143d852 2020-06-25 stsp return err;
3429 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3430 3143d852 2020-06-25 stsp ".gitignore");
3432 3143d852 2020-06-25 stsp return err;
3434 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3435 3143d852 2020-06-25 stsp if (err) {
3436 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3437 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3438 3143d852 2020-06-25 stsp return err;
3440 3143d852 2020-06-25 stsp for (;;) {
3441 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3442 3143d852 2020-06-25 stsp ".cvsignore");
3445 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3446 3143d852 2020-06-25 stsp ".gitignore");
3449 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3450 3143d852 2020-06-25 stsp if (err) {
3451 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3452 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3455 b737c85a 2020-06-26 stsp free(parent_path);
3456 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3457 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3460 b737c85a 2020-06-26 stsp free(parent_path);
3461 b737c85a 2020-06-26 stsp free(next_parent_path);
3462 3143d852 2020-06-25 stsp return err;
3465 abb4604f 2019-07-27 stsp static const struct got_error *
3466 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3467 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3468 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3469 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3470 f2a9dc41 2019-12-13 tracey int report_unchanged)
3472 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3473 6fc93f37 2019-12-13 stsp int fd = -1;
3474 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3475 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3476 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3478 3143d852 2020-06-25 stsp TAILQ_INIT(&arg.ignores);
3480 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3481 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3482 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3484 6fc93f37 2019-12-13 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3485 6fc93f37 2019-12-13 stsp if (fd == -1) {
3486 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3487 00bb5ea0 2020-07-23 stsp errno != ELOOP)
3488 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3490 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3491 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3492 f2a9dc41 2019-12-13 tracey report_unchanged);
3494 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3495 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3496 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3497 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3498 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3499 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3500 abb4604f 2019-07-27 stsp arg.status_path = path;
3501 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3502 abb4604f 2019-07-27 stsp arg.repo = repo;
3503 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3504 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3505 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3506 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3507 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3508 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3509 022fae89 2019-12-06 tracey if (!no_ignores) {
3510 3143d852 2020-06-25 stsp err = add_ignores_from_parent_paths(&arg.ignores,
3511 3143d852 2020-06-25 stsp worktree->root_path, path);
3513 3143d852 2020-06-25 stsp goto done;
3515 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3516 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3519 3143d852 2020-06-25 stsp free_ignores(&arg.ignores);
3520 6fc93f37 2019-12-13 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
3521 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3522 927df6b7 2019-02-10 stsp free(ondisk_path);
3523 347d1d3e 2019-07-12 stsp return err;
3526 347d1d3e 2019-07-12 stsp const struct got_error *
3527 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3528 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3529 72ea6654 2019-07-27 stsp got_worktree_status_cb status_cb, void *status_arg,
3530 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3532 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3533 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3534 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3535 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3537 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3539 347d1d3e 2019-07-12 stsp return err;
3541 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3542 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3543 f2a9dc41 2019-12-13 tracey status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3547 f8d1f275 2019-02-04 stsp free(fileindex_path);
3548 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3549 f8d1f275 2019-02-04 stsp return err;
3552 6c7ab921 2019-03-18 stsp const struct got_error *
3553 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3554 6c7ab921 2019-03-18 stsp const char *arg)
3556 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3557 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3558 6c7ab921 2019-03-18 stsp size_t len;
3559 00bb5ea0 2020-07-23 stsp struct stat sb;
3561 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3563 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3564 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3565 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3567 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3568 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3569 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3570 00bb5ea0 2020-07-23 stsp goto done;
3573 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3575 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3576 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3577 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3578 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3580 00bb5ea0 2020-07-23 stsp char *abspath = NULL;
3581 00bb5ea0 2020-07-23 stsp char canonpath[PATH_MAX];
3582 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3583 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3584 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3585 00bb5ea0 2020-07-23 stsp goto done;
3589 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3590 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3592 d0710d08 2019-07-22 stsp goto done;
3593 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3594 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3595 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3596 00bb5ea0 2020-07-23 stsp goto done;
3599 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3600 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3601 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3602 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3603 00bb5ea0 2020-07-23 stsp goto done;
3605 00bb5ea0 2020-07-23 stsp if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3606 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3607 00bb5ea0 2020-07-23 stsp goto done;
3612 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3613 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3614 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3615 6c7ab921 2019-03-18 stsp goto done;
3618 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3619 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3620 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3622 f6d88e1a 2019-05-29 stsp goto done;
3624 f6d88e1a 2019-05-29 stsp path = strdup("");
3625 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3626 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3627 f6d88e1a 2019-05-29 stsp goto done;
3631 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3632 6c7ab921 2019-03-18 stsp len = strlen(path);
3633 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3634 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3638 6c7ab921 2019-03-18 stsp free(resolved);
3639 d0710d08 2019-07-22 stsp free(cwd);
3640 6c7ab921 2019-03-18 stsp if (err == NULL)
3641 6c7ab921 2019-03-18 stsp *wt_path = path;
3643 6c7ab921 2019-03-18 stsp free(path);
3644 d00136be 2019-03-26 stsp return err;
3647 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3648 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3649 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3650 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3651 4e68cba3 2019-11-23 stsp void *progress_arg;
3652 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3655 4e68cba3 2019-11-23 stsp static const struct got_error *
3656 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3657 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3658 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3659 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3661 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3662 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3663 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3664 6d022e97 2019-08-04 stsp struct stat sb;
3665 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3667 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3668 dbb83fbd 2019-12-12 stsp relpath) == -1)
3669 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3671 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3673 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3674 12463d8b 2019-12-13 stsp de_name, a->repo);
3676 dbb83fbd 2019-12-12 stsp goto done;
3677 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3678 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3679 dbb83fbd 2019-12-12 stsp goto done;
3680 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3682 dbb83fbd 2019-12-12 stsp goto done;
3685 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3686 dbb83fbd 2019-12-12 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3687 dbb83fbd 2019-12-12 stsp goto done;
3690 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3692 4e68cba3 2019-11-23 stsp goto done;
3693 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3694 3969253a 2020-03-07 stsp if (err) {
3695 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3696 3969253a 2020-03-07 stsp goto done;
3698 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3699 07f5b47a 2019-06-02 stsp if (err) {
3700 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
3701 4e68cba3 2019-11-23 stsp goto done;
3704 dbb83fbd 2019-12-12 stsp free(ondisk_path);
3706 dbb83fbd 2019-12-12 stsp return err;
3707 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
3708 dbb83fbd 2019-12-12 stsp return NULL;
3709 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3712 d00136be 2019-03-26 stsp const struct got_error *
3713 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
3714 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
3715 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3716 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
3718 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3719 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
3720 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3721 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
3722 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
3724 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3726 d00136be 2019-03-26 stsp return err;
3728 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3730 d00136be 2019-03-26 stsp goto done;
3732 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
3733 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
3734 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
3735 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
3736 4e68cba3 2019-11-23 stsp saa.repo = repo;
3738 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3739 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3740 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3744 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3745 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3746 af12c6b9 2019-06-04 stsp err = sync_err;
3748 fb399478 2019-07-12 stsp free(fileindex_path);
3749 d00136be 2019-03-26 stsp if (fileindex)
3750 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
3751 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3752 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
3753 d00136be 2019-03-26 stsp err = unlockerr;
3754 6c7ab921 2019-03-18 stsp return err;
3757 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
3758 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
3759 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
3760 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
3761 f2a9dc41 2019-12-13 tracey void *progress_arg;
3762 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
3763 f2a9dc41 2019-12-13 tracey int delete_local_mods;
3764 70e3e7f5 2019-12-13 tracey int keep_on_disk;
3765 766841c2 2020-08-13 stsp const char *status_codes;
3768 f2a9dc41 2019-12-13 tracey static const struct got_error *
3769 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
3770 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
3771 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3772 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
3774 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
3775 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
3776 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
3777 17ed4618 2019-06-02 stsp struct stat sb;
3778 15341bfd 2020-03-05 tracey char *ondisk_path, *parent = NULL;
3780 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3781 17ed4618 2019-06-02 stsp if (ie == NULL)
3782 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
3784 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
3785 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
3786 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3787 9acbc4fa 2019-08-03 stsp return NULL;
3788 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3791 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3792 f2a9dc41 2019-12-13 tracey relpath) == -1)
3793 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
3795 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3798 f2a9dc41 2019-12-13 tracey goto done;
3800 766841c2 2020-08-13 stsp if (a->status_codes) {
3801 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
3803 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
3804 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
3807 766841c2 2020-08-13 stsp if (i == ncodes) {
3808 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
3809 766841c2 2020-08-13 stsp free(ondisk_path);
3810 766841c2 2020-08-13 stsp return NULL;
3812 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3813 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
3814 766841c2 2020-08-13 stsp static char msg[64];
3815 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
3816 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
3817 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3818 766841c2 2020-08-13 stsp goto done;
3822 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
3823 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
3824 f2a9dc41 2019-12-13 tracey goto done;
3825 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3826 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3827 f2a9dc41 2019-12-13 tracey goto done;
3829 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
3830 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
3831 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3832 f2a9dc41 2019-12-13 tracey goto done;
3836 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3837 12463d8b 2019-12-13 stsp if (dirfd != -1) {
3838 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
3839 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
3840 12463d8b 2019-12-13 stsp ondisk_path);
3841 12463d8b 2019-12-13 stsp goto done;
3843 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
3844 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
3845 12463d8b 2019-12-13 stsp goto done;
3848 15341bfd 2020-03-05 tracey parent = dirname(ondisk_path);
3850 15341bfd 2020-03-05 tracey if (parent == NULL) {
3851 15341bfd 2020-03-05 tracey err = got_error_from_errno2("dirname", ondisk_path);
3852 15341bfd 2020-03-05 tracey goto done;
3854 15341bfd 2020-03-05 tracey while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3855 15341bfd 2020-03-05 tracey if (rmdir(parent) == -1) {
3856 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
3857 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
3858 15341bfd 2020-03-05 tracey parent);
3861 15341bfd 2020-03-05 tracey parent = dirname(parent);
3862 15341bfd 2020-03-05 tracey if (parent == NULL) {
3863 15341bfd 2020-03-05 tracey err = got_error_from_errno2("dirname", parent);
3864 15341bfd 2020-03-05 tracey goto done;
3869 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3871 f2a9dc41 2019-12-13 tracey free(ondisk_path);
3872 f2a9dc41 2019-12-13 tracey if (err)
3873 f2a9dc41 2019-12-13 tracey return err;
3874 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
3875 f2a9dc41 2019-12-13 tracey return NULL;
3876 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3877 f2a9dc41 2019-12-13 tracey staged_status, relpath);
3880 2ec1f75b 2019-03-26 stsp const struct got_error *
3881 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
3882 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
3883 766841c2 2020-08-13 stsp const char *status_codes,
3884 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
3885 70e3e7f5 2019-12-13 tracey struct got_repository *repo, int keep_on_disk)
3887 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3888 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
3889 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3890 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
3891 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
3893 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3895 2ec1f75b 2019-03-26 stsp return err;
3897 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3899 2ec1f75b 2019-03-26 stsp goto done;
3901 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
3902 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
3903 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
3904 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
3905 f2a9dc41 2019-12-13 tracey sda.repo = repo;
3906 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
3907 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
3908 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
3910 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3911 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
3912 f2a9dc41 2019-12-13 tracey schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3916 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3917 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3918 af12c6b9 2019-06-04 stsp err = sync_err;
3920 fb399478 2019-07-12 stsp free(fileindex_path);
3921 2ec1f75b 2019-03-26 stsp if (fileindex)
3922 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
3923 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3924 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
3925 2ec1f75b 2019-03-26 stsp err = unlockerr;
3926 33aa809d 2019-08-08 stsp return err;
3929 33aa809d 2019-08-08 stsp static const struct got_error *
3930 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3932 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
3933 33aa809d 2019-08-08 stsp char *line = NULL;
3934 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
3935 33aa809d 2019-08-08 stsp ssize_t linelen;
3937 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
3938 33aa809d 2019-08-08 stsp if (linelen == -1) {
3939 33aa809d 2019-08-08 stsp if (ferror(infile)) {
3940 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
3941 33aa809d 2019-08-08 stsp goto done;
3943 33aa809d 2019-08-08 stsp return NULL;
3945 33aa809d 2019-08-08 stsp if (outfile) {
3946 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
3947 33aa809d 2019-08-08 stsp if (n != linelen) {
3948 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
3949 33aa809d 2019-08-08 stsp goto done;
3952 33aa809d 2019-08-08 stsp if (rejectfile) {
3953 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
3954 33aa809d 2019-08-08 stsp if (n != linelen)
3955 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
3958 33aa809d 2019-08-08 stsp free(line);
3959 2ec1f75b 2019-03-26 stsp return err;
3962 33aa809d 2019-08-08 stsp static const struct got_error *
3963 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
3965 33aa809d 2019-08-08 stsp char *line = NULL;
3966 33aa809d 2019-08-08 stsp size_t linesize = 0;
3967 33aa809d 2019-08-08 stsp ssize_t linelen;
3969 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
3970 500467ff 2019-09-25 hiltjo if (linelen == -1) {
3971 500467ff 2019-09-25 hiltjo if (ferror(f))
3972 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
3973 500467ff 2019-09-25 hiltjo return NULL;
3975 33aa809d 2019-08-08 stsp free(line);
3976 33aa809d 2019-08-08 stsp return NULL;
3979 33aa809d 2019-08-08 stsp static const struct got_error *
3980 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3981 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
3982 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
3984 33aa809d 2019-08-08 stsp const struct got_error *err;
3986 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
3987 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
3988 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
3990 33aa809d 2019-08-08 stsp return err;
3991 33aa809d 2019-08-08 stsp (*line_cur1)++;
3993 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
3994 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
3995 33aa809d 2019-08-08 stsp if (rejectfile)
3996 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
3998 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4000 33aa809d 2019-08-08 stsp return err;
4001 33aa809d 2019-08-08 stsp (*line_cur2)++;
4003 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4004 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4005 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4007 33aa809d 2019-08-08 stsp return err;
4008 33aa809d 2019-08-08 stsp (*line_cur2)++;
4010 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4011 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4012 33aa809d 2019-08-08 stsp if (rejectfile)
4013 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4015 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4017 33aa809d 2019-08-08 stsp return err;
4018 33aa809d 2019-08-08 stsp (*line_cur1)++;
4021 f1e81a05 2019-08-10 stsp return NULL;
4024 f1e81a05 2019-08-10 stsp static const struct got_error *
4025 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4026 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4028 f1e81a05 2019-08-10 stsp const struct got_error *err;
4030 f1e81a05 2019-08-10 stsp if (outfile) {
4031 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4032 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4033 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4035 f1e81a05 2019-08-10 stsp return err;
4036 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4039 f1e81a05 2019-08-10 stsp if (rejectfile) {
4040 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4041 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4042 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4044 f1e81a05 2019-08-10 stsp return err;
4045 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4049 33aa809d 2019-08-08 stsp return NULL;
4052 33aa809d 2019-08-08 stsp static const struct got_error *
4053 33aa809d 2019-08-08 stsp apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
4054 33aa809d 2019-08-08 stsp int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
4055 33aa809d 2019-08-08 stsp int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
4056 33aa809d 2019-08-08 stsp int *line_cur2, FILE *outfile, FILE *rejectfile,
4057 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4059 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4060 33aa809d 2019-08-08 stsp int start_old = change->cv.a;
4061 33aa809d 2019-08-08 stsp int end_old = change->cv.b;
4062 33aa809d 2019-08-08 stsp int start_new = change->cv.c;
4063 33aa809d 2019-08-08 stsp int end_new = change->cv.d;
4064 33aa809d 2019-08-08 stsp long pos1, pos2;
4065 33aa809d 2019-08-08 stsp FILE *hunkfile;
4067 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4069 33aa809d 2019-08-08 stsp hunkfile = got_opentemp();
4070 33aa809d 2019-08-08 stsp if (hunkfile == NULL)
4071 33aa809d 2019-08-08 stsp return got_error_from_errno("got_opentemp");
4073 33aa809d 2019-08-08 stsp pos1 = ftell(f1);
4074 33aa809d 2019-08-08 stsp pos2 = ftell(f2);
4076 33aa809d 2019-08-08 stsp /* XXX TODO needs error checking */
4077 33aa809d 2019-08-08 stsp got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4079 33aa809d 2019-08-08 stsp if (fseek(f1, pos1, SEEK_SET) == -1) {
4080 33aa809d 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
4081 33aa809d 2019-08-08 stsp goto done;
4083 33aa809d 2019-08-08 stsp if (fseek(f2, pos2, SEEK_SET) == -1) {
4084 33aa809d 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
4085 33aa809d 2019-08-08 stsp goto done;
4087 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4088 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4089 33aa809d 2019-08-08 stsp goto done;
4092 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4093 33aa809d 2019-08-08 stsp hunkfile, n, nchanges);
4095 33aa809d 2019-08-08 stsp goto done;
4097 33aa809d 2019-08-08 stsp switch (*choice) {
4098 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4099 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4100 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4102 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4103 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4104 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4106 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4109 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4113 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4114 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4115 33aa809d 2019-08-08 stsp return err;
4118 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4119 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4120 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4121 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4122 1f1abb7e 2019-08-08 stsp void *progress_arg;
4123 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4124 33aa809d 2019-08-08 stsp void *patch_arg;
4125 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4128 e20a8b6f 2019-06-04 stsp static const struct got_error *
4129 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4130 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4131 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4132 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4133 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4135 33aa809d 2019-08-08 stsp const struct got_error *err;
4136 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4137 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4138 1ebedb77 2019-10-19 stsp int fd2 = -1;
4139 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4140 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4141 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4142 33aa809d 2019-08-08 stsp struct stat sb1, sb2;
4143 33aa809d 2019-08-08 stsp struct got_diff_changes *changes = NULL;
4144 33aa809d 2019-08-08 stsp struct got_diff_state *ds = NULL;
4145 33aa809d 2019-08-08 stsp struct got_diff_args *args = NULL;
4146 33aa809d 2019-08-08 stsp struct got_diff_change *change;
4147 33aa809d 2019-08-08 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4148 33aa809d 2019-08-08 stsp int n = 0;
4150 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4152 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4154 33aa809d 2019-08-08 stsp return err;
4156 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4157 12463d8b 2019-12-13 stsp fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4158 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4159 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4160 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4161 fa3cef63 2020-07-23 stsp goto done;
4163 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4164 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4165 fa3cef63 2020-07-23 stsp if (link_len == -1)
4166 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlinkat", path2);
4167 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4168 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4171 12463d8b 2019-12-13 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4172 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4173 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4174 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4175 fa3cef63 2020-07-23 stsp goto done;
4177 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4178 fa3cef63 2020-07-23 stsp sizeof(link_target));
4179 fa3cef63 2020-07-23 stsp if (link_len == -1)
4180 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4181 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4182 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4185 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4186 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4187 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4188 fa3cef63 2020-07-23 stsp goto done;
4191 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4192 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4193 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4194 fa3cef63 2020-07-23 stsp goto done;
4199 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4200 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4201 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4202 fa3cef63 2020-07-23 stsp goto done;
4204 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4205 fa3cef63 2020-07-23 stsp if (n != link_len) {
4206 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4207 fa3cef63 2020-07-23 stsp goto done;
4209 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4210 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4211 fa3cef63 2020-07-23 stsp goto done;
4213 fa3cef63 2020-07-23 stsp rewind(f2);
4216 33aa809d 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4218 33aa809d 2019-08-08 stsp goto done;
4220 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4222 33aa809d 2019-08-08 stsp goto done;
4224 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4226 33aa809d 2019-08-08 stsp goto done;
4228 33aa809d 2019-08-08 stsp if (stat(path1, &sb1) == -1) {
4229 33aa809d 2019-08-08 stsp err = got_error_from_errno2("stat", path1);
4230 33aa809d 2019-08-08 stsp goto done;
4233 33aa809d 2019-08-08 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
4234 33aa809d 2019-08-08 stsp f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4236 33aa809d 2019-08-08 stsp goto done;
4238 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4240 33aa809d 2019-08-08 stsp goto done;
4242 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4243 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4244 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4245 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4246 33aa809d 2019-08-08 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4247 33aa809d 2019-08-08 stsp int choice;
4248 33aa809d 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
4249 33aa809d 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
4250 33aa809d 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
4251 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4252 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4253 e635744c 2019-08-08 stsp patch_cb, patch_arg);
4255 33aa809d 2019-08-08 stsp goto done;
4256 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4257 33aa809d 2019-08-08 stsp have_content = 1;
4258 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4261 1ebedb77 2019-10-19 stsp if (have_content) {
4262 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4263 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4264 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4266 1ebedb77 2019-10-19 stsp goto done;
4268 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4269 fa3cef63 2020-07-23 stsp if (chmod(*path_outfile, sb2.st_mode) == -1) {
4270 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("chmod", path2);
4271 fa3cef63 2020-07-23 stsp goto done;
4276 33aa809d 2019-08-08 stsp free(id_str);
4278 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4279 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4280 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4281 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4282 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4283 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4284 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4285 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4286 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4287 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4288 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4289 33aa809d 2019-08-08 stsp if (err || !have_content) {
4290 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4291 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4292 33aa809d 2019-08-08 stsp free(*path_outfile);
4293 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4295 33aa809d 2019-08-08 stsp free(args);
4297 33aa809d 2019-08-08 stsp got_diff_state_free(ds);
4300 33aa809d 2019-08-08 stsp if (changes)
4301 33aa809d 2019-08-08 stsp got_diff_free_changes(changes);
4302 33aa809d 2019-08-08 stsp free(path1);
4303 33aa809d 2019-08-08 stsp return err;
4306 33aa809d 2019-08-08 stsp static const struct got_error *
4307 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4308 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4309 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4310 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4312 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4313 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4314 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4315 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4316 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4317 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4318 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4319 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4320 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4321 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4323 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4324 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4325 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4326 d3bcc3d1 2019-08-08 stsp return NULL;
4328 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4329 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4330 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4332 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4333 65084dad 2019-08-08 stsp if (ie == NULL)
4334 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4336 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4337 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4338 a129376b 2019-03-28 stsp if (err) {
4339 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4340 a129376b 2019-03-28 stsp goto done;
4341 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4342 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4343 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4344 e20a8b6f 2019-06-04 stsp goto done;
4347 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4348 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4349 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4350 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4351 a129376b 2019-03-28 stsp goto done;
4354 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4355 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4356 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4357 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4358 a129376b 2019-03-28 stsp goto done;
4361 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4362 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4363 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4364 a129376b 2019-03-28 stsp goto done;
4369 1f1abb7e 2019-08-08 stsp err = got_object_id_by_path(&tree_id, a->repo,
4370 1f1abb7e 2019-08-08 stsp a->worktree->base_commit_id, tree_path);
4371 a9fa2909 2019-07-27 stsp if (err) {
4372 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4373 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4374 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4375 a9fa2909 2019-07-27 stsp goto done;
4377 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4379 a9fa2909 2019-07-27 stsp goto done;
4381 a9fa2909 2019-07-27 stsp te_name = basename(ie->path);
4382 a9fa2909 2019-07-27 stsp if (te_name == NULL) {
4383 a9fa2909 2019-07-27 stsp err = got_error_from_errno2("basename", ie->path);
4384 a9fa2909 2019-07-27 stsp goto done;
4387 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4388 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4389 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4390 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4391 a9fa2909 2019-07-27 stsp goto done;
4395 a129376b 2019-03-28 stsp switch (status) {
4396 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4397 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4398 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4399 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4400 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4402 33aa809d 2019-08-08 stsp goto done;
4403 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4406 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4407 1f1abb7e 2019-08-08 stsp ie->path);
4409 1ee397ad 2019-07-12 stsp goto done;
4410 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4412 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4413 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4414 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4415 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4416 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4418 33aa809d 2019-08-08 stsp goto done;
4419 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4422 33aa809d 2019-08-08 stsp /* fall through */
4423 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4424 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4425 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4426 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4427 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4428 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4429 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4430 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4431 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4433 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4434 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4435 1f1abb7e 2019-08-08 stsp err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4437 65084dad 2019-08-08 stsp goto done;
4439 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4440 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4441 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4442 a129376b 2019-03-28 stsp goto done;
4445 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4446 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4447 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4448 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4449 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4450 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4451 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4453 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4454 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4455 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4456 369fd7e5 2020-07-23 stsp path_content);
4459 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4460 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4461 369fd7e5 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4462 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4464 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4465 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4466 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4467 369fd7e5 2020-07-23 stsp goto done;
4471 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4472 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4473 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4474 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4475 c90c8ce3 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4476 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4478 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4480 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4481 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4482 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4483 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4486 a129376b 2019-03-28 stsp goto done;
4487 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4488 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4489 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4490 054041d0 2020-06-24 stsp ondisk_path, blob->id.sha1,
4491 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4493 2e1fa222 2020-07-23 stsp goto done;
4495 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4496 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4497 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4506 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4507 33aa809d 2019-08-08 stsp free(path_content);
4508 e20a8b6f 2019-06-04 stsp free(parent_path);
4509 a129376b 2019-03-28 stsp free(tree_path);
4511 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4513 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4514 a129376b 2019-03-28 stsp free(tree_id);
4515 e20a8b6f 2019-06-04 stsp return err;
4518 e20a8b6f 2019-06-04 stsp const struct got_error *
4519 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4520 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4521 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4522 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4523 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4525 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4526 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4527 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4528 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4529 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4530 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4532 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4534 e20a8b6f 2019-06-04 stsp return err;
4536 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4538 e20a8b6f 2019-06-04 stsp goto done;
4540 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4541 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4542 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4543 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4544 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4545 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4546 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4547 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4548 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4549 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
4553 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4554 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4555 e20a8b6f 2019-06-04 stsp err = sync_err;
4557 fb399478 2019-07-12 stsp free(fileindex_path);
4558 a129376b 2019-03-28 stsp if (fileindex)
4559 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4560 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4561 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4562 a129376b 2019-03-28 stsp err = unlockerr;
4563 c4296144 2019-05-09 stsp return err;
4566 cf066bf8 2019-05-09 stsp static void
4567 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4569 24519714 2019-05-09 stsp free(ct->path);
4570 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4571 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4572 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4573 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4574 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4575 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4579 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4580 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4581 24519714 2019-05-09 stsp struct got_repository *repo;
4582 24519714 2019-05-09 stsp struct got_worktree *worktree;
4583 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4584 5f8a88c6 2019-08-03 stsp int have_staged_files;
4585 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4588 c4296144 2019-05-09 stsp static const struct got_error *
4589 88d0e355 2019-08-03 stsp collect_commitables(void *arg, unsigned char status,
4590 88d0e355 2019-08-03 stsp unsigned char staged_status, const char *relpath,
4591 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4592 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4594 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
4595 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4596 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4597 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4598 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4599 768aea60 2019-05-09 stsp struct stat sb;
4601 5f8a88c6 2019-08-03 stsp if (a->have_staged_files) {
4602 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4603 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4604 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4605 5f8a88c6 2019-08-03 stsp return NULL;
4607 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4608 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4610 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4611 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4612 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4613 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4614 5f8a88c6 2019-08-03 stsp return NULL;
4617 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4618 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4619 036813ee 2019-05-09 stsp goto done;
4621 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4622 036813ee 2019-05-09 stsp parent_path = strdup("");
4623 69960a46 2019-05-09 stsp if (parent_path == NULL)
4624 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4626 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4628 69960a46 2019-05-09 stsp return err;
4631 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4632 cf066bf8 2019-05-09 stsp if (ct == NULL) {
4633 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4634 c4296144 2019-05-09 stsp goto done;
4637 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4638 768aea60 2019-05-09 stsp relpath) == -1) {
4639 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4640 768aea60 2019-05-09 stsp goto done;
4643 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
4644 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
4645 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
4646 0aeb8099 2020-07-23 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4647 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
4648 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
4649 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
4650 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
4652 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
4653 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
4656 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4657 0aeb8099 2020-07-23 stsp goto done;
4659 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
4660 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
4661 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
4662 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4663 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
4664 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
4665 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
4666 12463d8b 2019-12-13 stsp ct->ondisk_path);
4667 12463d8b 2019-12-13 stsp goto done;
4669 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
4670 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
4671 768aea60 2019-05-09 stsp goto done;
4673 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
4676 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4677 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4678 44d03001 2019-05-09 stsp relpath) == -1) {
4679 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4680 44d03001 2019-05-09 stsp goto done;
4683 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4684 35213c7c 2020-07-23 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4685 35213c7c 2020-07-23 stsp int is_bad_symlink;
4686 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
4687 35213c7c 2020-07-23 stsp ssize_t target_len;
4688 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
4689 35213c7c 2020-07-23 stsp sizeof(target_path));
4690 35213c7c 2020-07-23 stsp if (target_len == -1) {
4691 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
4692 35213c7c 2020-07-23 stsp ct->ondisk_path);
4693 35213c7c 2020-07-23 stsp goto done;
4695 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
4696 35213c7c 2020-07-23 stsp target_len, ct->ondisk_path, a->worktree->root_path);
4698 35213c7c 2020-07-23 stsp goto done;
4699 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
4700 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
4701 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
4702 35213c7c 2020-07-23 stsp goto done;
4707 cf066bf8 2019-05-09 stsp ct->status = status;
4708 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
4709 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
4710 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
4711 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
4712 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
4713 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
4714 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4715 b416585c 2019-05-13 stsp goto done;
4717 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
4718 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
4719 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
4720 f0b75401 2019-08-03 stsp goto done;
4723 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
4724 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
4725 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4726 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
4727 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4728 036813ee 2019-05-09 stsp goto done;
4731 24519714 2019-05-09 stsp ct->path = strdup(path);
4732 24519714 2019-05-09 stsp if (ct->path == NULL) {
4733 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4734 24519714 2019-05-09 stsp goto done;
4736 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4738 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
4739 ed175427 2019-05-09 stsp free_commitable(ct);
4740 24519714 2019-05-09 stsp free(parent_path);
4741 036813ee 2019-05-09 stsp free(path);
4742 a129376b 2019-03-28 stsp return err;
4745 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
4746 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
4747 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4748 036813ee 2019-05-09 stsp struct got_repository *);
4750 ed175427 2019-05-09 stsp static const struct got_error *
4751 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4752 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
4753 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
4754 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4755 afa376bf 2019-05-09 stsp struct got_repository *repo)
4757 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
4758 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
4759 036813ee 2019-05-09 stsp char *subpath;
4761 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
4762 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4763 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4765 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
4767 ed175427 2019-05-09 stsp return err;
4769 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
4770 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
4771 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
4772 036813ee 2019-05-09 stsp free(subpath);
4773 036813ee 2019-05-09 stsp return err;
4776 036813ee 2019-05-09 stsp static const struct got_error *
4777 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4779 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4780 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
4782 036813ee 2019-05-09 stsp *match = 0;
4784 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
4785 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
4786 0f63689d 2019-05-10 stsp return NULL;
4789 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4791 0f63689d 2019-05-10 stsp return err;
4792 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
4793 036813ee 2019-05-09 stsp free(ct_parent_path);
4794 036813ee 2019-05-09 stsp return err;
4797 768aea60 2019-05-09 stsp static mode_t
4798 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
4800 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
4801 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
4803 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4806 036813ee 2019-05-09 stsp static const struct got_error *
4807 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4808 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
4810 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4812 036813ee 2019-05-09 stsp *new_te = NULL;
4814 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
4816 036813ee 2019-05-09 stsp goto done;
4818 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4820 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
4821 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4822 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4824 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4826 036813ee 2019-05-09 stsp if (err && *new_te) {
4827 56e0773d 2019-11-28 stsp free(*new_te);
4828 036813ee 2019-05-09 stsp *new_te = NULL;
4830 036813ee 2019-05-09 stsp return err;
4833 036813ee 2019-05-09 stsp static const struct got_error *
4834 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4835 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
4837 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4838 036813ee 2019-05-09 stsp char *ct_name;
4840 036813ee 2019-05-09 stsp *new_te = NULL;
4842 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
4843 036813ee 2019-05-09 stsp if (*new_te == NULL)
4844 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
4846 036813ee 2019-05-09 stsp ct_name = basename(ct->path);
4847 036813ee 2019-05-09 stsp if (ct_name == NULL) {
4848 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", ct->path);
4849 036813ee 2019-05-09 stsp goto done;
4851 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4852 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
4853 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
4854 036813ee 2019-05-09 stsp goto done;
4857 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4859 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
4860 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4861 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4863 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4865 036813ee 2019-05-09 stsp if (err && *new_te) {
4866 56e0773d 2019-11-28 stsp free(*new_te);
4867 036813ee 2019-05-09 stsp *new_te = NULL;
4869 036813ee 2019-05-09 stsp return err;
4872 036813ee 2019-05-09 stsp static const struct got_error *
4873 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
4874 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
4876 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4877 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
4879 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4881 036813ee 2019-05-09 stsp return err;
4882 036813ee 2019-05-09 stsp if (new_pe == NULL)
4883 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
4884 036813ee 2019-05-09 stsp return NULL;
4887 afa376bf 2019-05-09 stsp static const struct got_error *
4888 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
4889 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
4891 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
4892 5f8a88c6 2019-08-03 stsp unsigned char status;
4894 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
4895 afa376bf 2019-05-09 stsp ct_path++;
4897 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4898 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
4900 5f8a88c6 2019-08-03 stsp status = ct->status;
4902 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4903 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4906 036813ee 2019-05-09 stsp static const struct got_error *
4907 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
4908 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4910 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
4911 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
4912 44d03001 2019-05-09 stsp char *te_path;
4914 44d03001 2019-05-09 stsp *modified = 0;
4916 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
4917 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
4918 44d03001 2019-05-09 stsp te->name) == -1)
4919 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4921 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4922 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4923 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
4924 44d03001 2019-05-09 stsp strlen(te_path));
4925 44d03001 2019-05-09 stsp if (*modified)
4929 44d03001 2019-05-09 stsp free(te_path);
4930 44d03001 2019-05-09 stsp return err;
4933 44d03001 2019-05-09 stsp static const struct got_error *
4934 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
4935 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
4936 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
4938 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4939 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
4941 036813ee 2019-05-09 stsp *ctp = NULL;
4943 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4944 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4945 036813ee 2019-05-09 stsp char *ct_name = NULL;
4946 036813ee 2019-05-09 stsp int path_matches;
4948 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4949 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
4950 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
4951 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
4954 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
4955 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
4959 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4962 036813ee 2019-05-09 stsp err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4964 036813ee 2019-05-09 stsp return err;
4965 036813ee 2019-05-09 stsp if (!path_matches)
4968 036813ee 2019-05-09 stsp ct_name = basename(pe->path);
4969 036813ee 2019-05-09 stsp if (ct_name == NULL)
4970 638f9024 2019-05-13 stsp return got_error_from_errno2("basename", pe->path);
4972 036813ee 2019-05-09 stsp if (strcmp(te->name, ct_name) != 0)
4975 036813ee 2019-05-09 stsp *ctp = ct;
4979 2b496619 2019-07-10 stsp return err;
4982 2b496619 2019-07-10 stsp static const struct got_error *
4983 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4984 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
4985 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
4986 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
4987 2b496619 2019-07-10 stsp struct got_repository *repo)
4989 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
4990 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
4991 2b496619 2019-07-10 stsp char *subtree_path;
4992 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
4993 ba580f68 2020-03-22 stsp int nentries;
4995 2b496619 2019-07-10 stsp *new_tep = NULL;
4997 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4998 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
4999 2b496619 2019-07-10 stsp child_path) == -1)
5000 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5002 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5003 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5004 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5005 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5007 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5008 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5009 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5010 2b496619 2019-07-10 stsp goto done;
5012 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5013 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5014 2b496619 2019-07-10 stsp if (err) {
5015 56e0773d 2019-11-28 stsp free(new_te);
5016 2b496619 2019-07-10 stsp goto done;
5018 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5021 2b496619 2019-07-10 stsp free(subtree_path);
5022 2b496619 2019-07-10 stsp if (err == NULL)
5023 2b496619 2019-07-10 stsp *new_tep = new_te;
5024 036813ee 2019-05-09 stsp return err;
5027 036813ee 2019-05-09 stsp static const struct got_error *
5028 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5029 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5030 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5031 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5032 036813ee 2019-05-09 stsp struct got_repository *repo)
5034 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5035 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5036 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5037 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5039 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5040 ba580f68 2020-03-22 stsp *nentries = 0;
5042 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5043 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5044 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5045 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5047 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5048 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5049 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5052 f2b0a8b0 2020-07-31 stsp if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5053 2f51b5b3 2019-05-09 stsp strlen(path_base_tree)))
5056 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5057 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5059 036813ee 2019-05-09 stsp goto done;
5061 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5062 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5063 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5065 036813ee 2019-05-09 stsp goto done;
5066 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5068 afa376bf 2019-05-09 stsp goto done;
5069 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5070 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5072 2f51b5b3 2019-05-09 stsp goto done;
5073 ba580f68 2020-03-22 stsp (*nentries)++;
5075 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5076 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5077 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5078 2b496619 2019-07-10 stsp == NULL) {
5079 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5080 2b496619 2019-07-10 stsp child_path, path_base_tree,
5081 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5084 2b496619 2019-07-10 stsp goto done;
5085 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5087 2b496619 2019-07-10 stsp goto done;
5088 ba580f68 2020-03-22 stsp (*nentries)++;
5093 2f51b5b3 2019-05-09 stsp if (base_tree) {
5094 56e0773d 2019-11-28 stsp int i, nbase_entries;
5095 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5096 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5097 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5098 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5100 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5101 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5102 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5103 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5105 63c5ca5d 2019-08-24 stsp goto done;
5106 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5108 63c5ca5d 2019-08-24 stsp goto done;
5109 ba580f68 2020-03-22 stsp (*nentries)++;
5113 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5114 44d03001 2019-05-09 stsp int modified;
5115 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5117 036813ee 2019-05-09 stsp goto done;
5118 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5119 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5121 2f51b5b3 2019-05-09 stsp goto done;
5122 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5123 44d03001 2019-05-09 stsp if (modified) {
5124 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5125 ba580f68 2020-03-22 stsp int nsubentries;
5126 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5127 ba580f68 2020-03-22 stsp &nsubentries, te,
5128 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5129 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5131 44d03001 2019-05-09 stsp goto done;
5132 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5133 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5134 ba580f68 2020-03-22 stsp free(new_id);
5137 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5138 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5139 56e0773d 2019-11-28 stsp free(new_id);
5141 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5143 036813ee 2019-05-09 stsp goto done;
5144 ba580f68 2020-03-22 stsp (*nentries)++;
5148 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5149 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5151 f66c734c 2019-09-22 stsp goto done;
5153 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5154 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5155 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5156 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5157 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5158 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5160 2f51b5b3 2019-05-09 stsp goto done;
5161 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5163 2f51b5b3 2019-05-09 stsp goto done;
5164 ba580f68 2020-03-22 stsp (*nentries)++;
5166 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5167 b416585c 2019-05-13 stsp status_arg);
5169 afa376bf 2019-05-09 stsp goto done;
5171 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5172 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5174 2f51b5b3 2019-05-09 stsp goto done;
5175 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5177 2f51b5b3 2019-05-09 stsp goto done;
5178 ba580f68 2020-03-22 stsp (*nentries)++;
5183 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5184 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5186 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
5187 2e1fa222 2020-07-23 stsp return err;
5190 2e1fa222 2020-07-23 stsp static const struct got_error *
5191 ebf99748 2019-05-09 stsp update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5192 72fd46fa 2019-09-06 stsp struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5193 72fd46fa 2019-09-06 stsp int have_staged_files)
5195 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5196 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5198 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5199 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5200 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5202 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5204 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5205 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5206 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5207 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5208 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5209 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5210 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5211 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5212 5f8a88c6 2019-08-03 stsp ct->ondisk_path, ct->staged_blob_id->sha1,
5213 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5214 72fd46fa 2019-09-06 stsp !have_staged_files);
5216 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5217 e75eb4da 2019-05-10 stsp ct->ondisk_path, ct->blob_id->sha1,
5218 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5219 72fd46fa 2019-09-06 stsp !have_staged_files);
5221 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5224 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ct->ondisk_path,
5225 3969253a 2020-03-07 stsp ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5226 3969253a 2020-03-07 stsp if (err) {
5227 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5230 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5231 3969253a 2020-03-07 stsp if (err) {
5232 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5237 d56d26ce 2019-05-10 stsp return err;
5241 d56d26ce 2019-05-10 stsp static const struct got_error *
5242 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5243 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5244 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5245 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5246 735ef5ac 2019-08-03 stsp int ood_errcode)
5248 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5249 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5251 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5252 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5253 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5254 1a36436d 2019-06-10 stsp return NULL;
5256 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5257 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5259 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5260 735ef5ac 2019-08-03 stsp in_repo_path);
5261 9bead371 2019-07-28 stsp if (err) {
5262 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5263 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5264 1a36436d 2019-06-10 stsp goto done;
5265 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5266 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5268 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5269 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5270 735ef5ac 2019-08-03 stsp in_repo_path);
5271 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5272 1a36436d 2019-06-10 stsp goto done;
5273 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5277 1a36436d 2019-06-10 stsp return err;
5280 c4296144 2019-05-09 stsp const struct got_error *
5281 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5282 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5283 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id, struct got_worktree *worktree,
5284 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
5285 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5286 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5287 afa376bf 2019-05-09 stsp struct got_repository *repo)
5289 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5290 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5291 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5292 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5293 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5294 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5295 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5296 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5297 ba580f68 2020-03-22 stsp int nentries;
5298 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5299 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5300 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5302 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5304 de18fc63 2019-05-09 stsp SIMPLEQ_INIT(&parent_ids);
5306 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5308 588edf97 2019-05-10 stsp goto done;
5310 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5312 588edf97 2019-05-10 stsp goto done;
5314 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5315 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5320 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5321 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5325 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5326 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5327 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5328 cf066bf8 2019-05-09 stsp char *ondisk_path;
5330 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5331 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5332 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5335 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5336 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5337 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5340 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5341 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5342 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5343 cf066bf8 2019-05-09 stsp goto done;
5345 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5346 2e1fa222 2020-07-23 stsp free(ondisk_path);
5348 cf066bf8 2019-05-09 stsp goto done;
5351 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5352 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5353 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5355 036813ee 2019-05-09 stsp goto done;
5357 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5359 de18fc63 2019-05-09 stsp goto done;
5360 de18fc63 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5361 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5362 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5363 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
5364 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5365 33ad4cbe 2019-05-12 jcs free(logmsg);
5367 9d40349a 2019-05-09 stsp goto done;
5369 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5370 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5371 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5372 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5373 09f5bd90 2019-05-09 stsp goto done;
5375 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
5376 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5378 09f5bd90 2019-05-09 stsp goto done;
5379 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5381 ebf99748 2019-05-09 stsp goto done;
5382 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5383 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5384 09f5bd90 2019-05-09 stsp goto done;
5386 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
5387 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
5389 f2c16586 2019-05-09 stsp goto done;
5390 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
5392 f2c16586 2019-05-09 stsp goto done;
5394 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5396 09f5bd90 2019-05-09 stsp goto done;
5398 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
5400 ebf99748 2019-05-09 stsp goto done;
5402 588edf97 2019-05-10 stsp if (head_tree)
5403 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
5404 588edf97 2019-05-10 stsp if (head_commit)
5405 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
5406 09f5bd90 2019-05-09 stsp free(head_commit_id2);
5407 2f17228e 2019-05-12 stsp if (head_ref2) {
5408 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
5409 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
5410 2f17228e 2019-05-12 stsp err = unlockerr;
5411 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
5413 39cd0ff6 2019-07-12 stsp return err;
5416 5c1e53bc 2019-07-28 stsp static const struct got_error *
5417 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
5418 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
5420 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
5421 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
5423 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
5424 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
5425 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
5427 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
5428 5c1e53bc 2019-07-28 stsp ct_path++;
5430 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
5431 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
5435 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
5436 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
5438 5c1e53bc 2019-07-28 stsp return NULL;
5441 f0b75401 2019-08-03 stsp static const struct got_error *
5442 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
5444 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
5446 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5447 f0b75401 2019-08-03 stsp *have_staged_files = 1;
5448 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
5451 f0b75401 2019-08-03 stsp return NULL;
5454 5f8a88c6 2019-08-03 stsp static const struct got_error *
5455 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
5456 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
5458 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
5459 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
5461 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5462 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
5464 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5465 5f8a88c6 2019-08-03 stsp if (ie == NULL)
5466 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5467 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5468 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
5469 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
5472 5f8a88c6 2019-08-03 stsp return NULL;
5475 39cd0ff6 2019-07-12 stsp const struct got_error *
5476 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
5477 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
5478 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
5479 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5480 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
5481 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
5483 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5484 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
5485 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
5486 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5487 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5488 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
5489 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
5490 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5491 f0b75401 2019-08-03 stsp int have_staged_files = 0;
5493 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
5495 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5497 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
5499 39cd0ff6 2019-07-12 stsp goto done;
5501 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5503 39cd0ff6 2019-07-12 stsp goto done;
5505 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5507 39cd0ff6 2019-07-12 stsp goto done;
5509 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5511 39cd0ff6 2019-07-12 stsp goto done;
5513 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5514 5f8a88c6 2019-08-03 stsp &have_staged_files);
5515 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
5516 5f8a88c6 2019-08-03 stsp goto done;
5517 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
5518 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
5520 5f8a88c6 2019-08-03 stsp goto done;
5523 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5524 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
5525 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
5526 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
5527 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
5528 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5529 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5530 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5531 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5533 5c1e53bc 2019-07-28 stsp goto done;
5536 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5537 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5538 39cd0ff6 2019-07-12 stsp goto done;
5541 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5542 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
5544 5c1e53bc 2019-07-28 stsp goto done;
5547 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5548 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5549 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5551 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5552 f0b75401 2019-08-03 stsp ct_path++;
5553 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5554 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5555 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5557 b50cabdf 2019-07-12 stsp goto done;
5561 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5562 5c1e53bc 2019-07-28 stsp head_commit_id, worktree, author, committer,
5563 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5565 39cd0ff6 2019-07-12 stsp goto done;
5567 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5568 72fd46fa 2019-09-06 stsp fileindex, have_staged_files);
5569 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5570 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5571 39cd0ff6 2019-07-12 stsp err = sync_err;
5573 39cd0ff6 2019-07-12 stsp if (fileindex)
5574 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5575 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5576 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5577 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5578 39cd0ff6 2019-07-12 stsp err = unlockerr;
5579 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5580 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5581 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5583 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5584 c4296144 2019-05-09 stsp return err;
5587 8656d6c4 2019-05-20 stsp const char *
5588 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5590 8656d6c4 2019-05-20 stsp return ct->path;
5593 8656d6c4 2019-05-20 stsp unsigned int
5594 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
5596 8656d6c4 2019-05-20 stsp return ct->status;
5599 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
5600 818c7501 2019-07-11 stsp struct got_worktree *worktree;
5601 818c7501 2019-07-11 stsp struct got_repository *repo;
5604 818c7501 2019-07-11 stsp static const struct got_error *
5605 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5607 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5608 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
5609 818c7501 2019-07-11 stsp unsigned char status;
5610 818c7501 2019-07-11 stsp struct stat sb;
5611 818c7501 2019-07-11 stsp char *ondisk_path;
5613 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
5614 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5615 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
5616 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
5618 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5620 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
5622 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
5623 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5624 818c7501 2019-07-11 stsp free(ondisk_path);
5626 818c7501 2019-07-11 stsp return err;
5628 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
5629 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
5630 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5631 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5633 818c7501 2019-07-11 stsp return NULL;
5636 818c7501 2019-07-11 stsp const struct got_error *
5637 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5638 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5639 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
5640 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5642 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5643 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5644 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
5645 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
5646 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
5647 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5648 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
5650 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5651 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5652 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5654 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
5656 818c7501 2019-07-11 stsp return err;
5658 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5660 818c7501 2019-07-11 stsp goto done;
5662 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
5663 818c7501 2019-07-11 stsp ok_arg.repo = repo;
5664 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5667 818c7501 2019-07-11 stsp goto done;
5669 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5671 818c7501 2019-07-11 stsp goto done;
5673 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5675 818c7501 2019-07-11 stsp goto done;
5677 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5679 818c7501 2019-07-11 stsp goto done;
5681 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5684 e51d7b55 2020-01-04 stsp goto done;
5686 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5688 818c7501 2019-07-11 stsp goto done;
5689 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5690 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5691 e51d7b55 2020-01-04 stsp goto done;
5694 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
5695 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
5697 818c7501 2019-07-11 stsp goto done;
5698 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
5700 818c7501 2019-07-11 stsp goto done;
5702 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
5704 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5706 818c7501 2019-07-11 stsp goto done;
5708 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
5710 818c7501 2019-07-11 stsp goto done;
5712 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
5713 818c7501 2019-07-11 stsp worktree->base_commit_id);
5715 818c7501 2019-07-11 stsp goto done;
5716 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
5718 818c7501 2019-07-11 stsp goto done;
5720 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
5722 818c7501 2019-07-11 stsp goto done;
5724 818c7501 2019-07-11 stsp free(fileindex_path);
5725 818c7501 2019-07-11 stsp free(tmp_branch_name);
5726 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
5727 818c7501 2019-07-11 stsp free(branch_ref_name);
5728 818c7501 2019-07-11 stsp if (branch_ref)
5729 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5730 818c7501 2019-07-11 stsp if (wt_branch)
5731 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
5732 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
5733 818c7501 2019-07-11 stsp if (err) {
5734 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
5735 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
5736 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5738 818c7501 2019-07-11 stsp if (*tmp_branch) {
5739 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5740 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5742 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5743 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5744 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5746 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
5748 818c7501 2019-07-11 stsp return err;
5751 818c7501 2019-07-11 stsp const struct got_error *
5752 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
5753 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5754 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
5755 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
5757 818c7501 2019-07-11 stsp const struct got_error *err;
5758 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5759 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5760 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5761 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
5762 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
5764 818c7501 2019-07-11 stsp *commit_id = NULL;
5765 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
5766 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
5767 3e3a69f1 2019-07-25 stsp *branch = NULL;
5768 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5770 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
5772 3e3a69f1 2019-07-25 stsp return err;
5774 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5776 f032f1f7 2019-08-04 stsp goto done;
5778 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5779 f032f1f7 2019-08-04 stsp &have_staged_files);
5780 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
5781 f032f1f7 2019-08-04 stsp goto done;
5782 f032f1f7 2019-08-04 stsp if (have_staged_files) {
5783 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
5784 3e3a69f1 2019-07-25 stsp goto done;
5787 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5789 f032f1f7 2019-08-04 stsp goto done;
5791 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5793 818c7501 2019-07-11 stsp goto done;
5795 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5797 818c7501 2019-07-11 stsp goto done;
5799 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5801 818c7501 2019-07-11 stsp goto done;
5803 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5805 818c7501 2019-07-11 stsp goto done;
5807 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
5808 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
5810 818c7501 2019-07-11 stsp goto done;
5812 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5814 818c7501 2019-07-11 stsp goto done;
5816 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
5818 818c7501 2019-07-11 stsp goto done;
5820 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
5821 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
5823 818c7501 2019-07-11 stsp goto done;
5825 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5827 818c7501 2019-07-11 stsp goto done;
5829 818c7501 2019-07-11 stsp free(commit_ref_name);
5830 818c7501 2019-07-11 stsp free(branch_ref_name);
5831 3e3a69f1 2019-07-25 stsp free(fileindex_path);
5832 818c7501 2019-07-11 stsp if (commit_ref)
5833 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
5834 818c7501 2019-07-11 stsp if (branch_ref)
5835 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5836 818c7501 2019-07-11 stsp if (err) {
5837 818c7501 2019-07-11 stsp free(*commit_id);
5838 818c7501 2019-07-11 stsp *commit_id = NULL;
5839 818c7501 2019-07-11 stsp if (*tmp_branch) {
5840 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5841 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5843 818c7501 2019-07-11 stsp if (*new_base_branch) {
5844 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
5845 818c7501 2019-07-11 stsp *new_base_branch = NULL;
5847 818c7501 2019-07-11 stsp if (*branch) {
5848 818c7501 2019-07-11 stsp got_ref_close(*branch);
5849 818c7501 2019-07-11 stsp *branch = NULL;
5851 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5852 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5853 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5855 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
5857 818c7501 2019-07-11 stsp return err;
5860 818c7501 2019-07-11 stsp const struct got_error *
5861 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5863 818c7501 2019-07-11 stsp const struct got_error *err;
5864 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
5866 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5868 818c7501 2019-07-11 stsp return err;
5870 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5871 818c7501 2019-07-11 stsp free(tmp_branch_name);
5872 818c7501 2019-07-11 stsp return NULL;
5875 818c7501 2019-07-11 stsp static const struct got_error *
5876 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5877 818c7501 2019-07-11 stsp char **logmsg, void *arg)
5879 0ebf8283 2019-07-24 stsp *logmsg = arg;
5880 818c7501 2019-07-11 stsp return NULL;
5883 818c7501 2019-07-11 stsp static const struct got_error *
5884 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5885 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
5886 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5887 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5889 818c7501 2019-07-11 stsp return NULL;
5892 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
5893 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
5894 01757395 2019-07-12 stsp void *progress_arg;
5895 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
5898 01757395 2019-07-12 stsp static const struct got_error *
5899 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
5901 01757395 2019-07-12 stsp const struct got_error *err;
5902 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
5904 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
5906 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
5908 01757395 2019-07-12 stsp return err;
5910 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
5911 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
5912 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
5913 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
5914 01757395 2019-07-12 stsp return NULL;
5916 01757395 2019-07-12 stsp p = strdup(path);
5917 01757395 2019-07-12 stsp if (p == NULL)
5918 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
5920 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5921 01757395 2019-07-12 stsp if (err || new == NULL)
5923 01757395 2019-07-12 stsp return err;
5927 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5929 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
5931 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
5932 01757395 2019-07-12 stsp free((char *)pe->path);
5934 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
5937 0ebf8283 2019-07-24 stsp static const struct got_error *
5938 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5939 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
5941 818c7501 2019-07-11 stsp const struct got_error *err;
5942 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
5944 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5945 818c7501 2019-07-11 stsp if (err) {
5946 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
5947 818c7501 2019-07-11 stsp goto done;
5948 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5950 818c7501 2019-07-11 stsp goto done;
5951 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
5953 818c7501 2019-07-11 stsp goto done;
5954 de05890f 2020-03-05 stsp } else if (is_rebase) {
5955 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
5958 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
5960 818c7501 2019-07-11 stsp goto done;
5961 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
5962 818c7501 2019-07-11 stsp free(stored_id);
5963 818c7501 2019-07-11 stsp if (cmp != 0) {
5964 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
5965 818c7501 2019-07-11 stsp goto done;
5969 0ebf8283 2019-07-24 stsp if (commit_ref)
5970 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
5971 0ebf8283 2019-07-24 stsp return err;
5974 0ebf8283 2019-07-24 stsp static const struct got_error *
5975 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
5976 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
5977 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5978 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
5979 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5980 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
5982 0ebf8283 2019-07-24 stsp const struct got_error *err;
5983 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
5984 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
5985 3e3a69f1 2019-07-25 stsp char *fileindex_path;
5987 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
5989 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
5991 0ebf8283 2019-07-24 stsp return err;
5993 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
5994 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
5995 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
5996 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
5997 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
5998 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
5999 818c7501 2019-07-11 stsp if (commit_ref)
6000 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6001 818c7501 2019-07-11 stsp return err;
6004 818c7501 2019-07-11 stsp const struct got_error *
6005 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6006 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6007 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6008 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6009 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6010 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6012 0ebf8283 2019-07-24 stsp const struct got_error *err;
6013 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6015 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6017 0ebf8283 2019-07-24 stsp return err;
6019 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6021 0ebf8283 2019-07-24 stsp goto done;
6023 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6024 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6025 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6027 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6028 0ebf8283 2019-07-24 stsp return err;
6031 0ebf8283 2019-07-24 stsp const struct got_error *
6032 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6033 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6034 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6035 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6036 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6037 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6039 0ebf8283 2019-07-24 stsp const struct got_error *err;
6040 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6042 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6044 0ebf8283 2019-07-24 stsp return err;
6046 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6048 0ebf8283 2019-07-24 stsp goto done;
6050 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6051 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6052 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6054 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6055 0ebf8283 2019-07-24 stsp return err;
6058 0ebf8283 2019-07-24 stsp static const struct got_error *
6059 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6060 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6061 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6062 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6063 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
6065 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6066 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6067 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6068 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6069 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6070 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6071 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6073 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6074 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6076 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6078 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6080 3e3a69f1 2019-07-25 stsp return err;
6082 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6083 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6084 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6085 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6087 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6088 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6089 01757395 2019-07-12 stsp * TODO: Ideally, merged_paths would contain a list of commitables
6090 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6092 01757395 2019-07-12 stsp if (merged_paths) {
6093 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6094 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6095 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6096 f2a9dc41 2019-12-13 tracey repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6099 01757395 2019-07-12 stsp goto done;
6102 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6103 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6105 01757395 2019-07-12 stsp goto done;
6108 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6109 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6110 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6112 a0e95631 2019-07-12 stsp goto done;
6113 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6114 a0e95631 2019-07-12 stsp goto done;
6117 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6119 edd02c5e 2019-07-11 stsp goto done;
6121 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6123 818c7501 2019-07-11 stsp goto done;
6125 5943eee2 2019-08-13 stsp if (new_logmsg) {
6126 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6127 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6128 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6129 5943eee2 2019-08-13 stsp goto done;
6132 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6134 5943eee2 2019-08-13 stsp goto done;
6137 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6138 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6139 5c1e53bc 2019-07-28 stsp worktree, got_object_commit_get_author(orig_commit),
6140 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
6141 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6143 a0e95631 2019-07-12 stsp goto done;
6145 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6147 a0e95631 2019-07-12 stsp goto done;
6149 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6151 a0e95631 2019-07-12 stsp goto done;
6153 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6154 72fd46fa 2019-09-06 stsp fileindex, 0);
6155 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6156 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6157 a0e95631 2019-07-12 stsp err = sync_err;
6159 a0e95631 2019-07-12 stsp free(fileindex_path);
6160 a0e95631 2019-07-12 stsp free(head_commit_id);
6161 a0e95631 2019-07-12 stsp if (head_ref)
6162 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6163 818c7501 2019-07-11 stsp if (err) {
6164 818c7501 2019-07-11 stsp free(*new_commit_id);
6165 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6167 818c7501 2019-07-11 stsp return err;
6170 818c7501 2019-07-11 stsp const struct got_error *
6171 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6172 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6173 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6174 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6175 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6177 0ebf8283 2019-07-24 stsp const struct got_error *err;
6178 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6179 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6180 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6182 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6184 0ebf8283 2019-07-24 stsp return err;
6186 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6188 0ebf8283 2019-07-24 stsp goto done;
6189 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6191 0ebf8283 2019-07-24 stsp goto done;
6192 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6193 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6194 0ebf8283 2019-07-24 stsp goto done;
6197 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6198 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6200 0ebf8283 2019-07-24 stsp if (commit_ref)
6201 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6202 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6203 0ebf8283 2019-07-24 stsp free(commit_id);
6204 0ebf8283 2019-07-24 stsp return err;
6207 0ebf8283 2019-07-24 stsp const struct got_error *
6208 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6209 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6210 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6211 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6212 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6213 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6215 0ebf8283 2019-07-24 stsp const struct got_error *err;
6216 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6217 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6219 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6221 0ebf8283 2019-07-24 stsp return err;
6223 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6225 0ebf8283 2019-07-24 stsp goto done;
6227 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6228 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6230 0ebf8283 2019-07-24 stsp if (commit_ref)
6231 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6232 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6233 0ebf8283 2019-07-24 stsp return err;
6236 0ebf8283 2019-07-24 stsp const struct got_error *
6237 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6238 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6240 3e3a69f1 2019-07-25 stsp if (fileindex)
6241 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6242 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6245 69844fba 2019-07-11 stsp static const struct got_error *
6246 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6248 69844fba 2019-07-11 stsp const struct got_error *err;
6249 69844fba 2019-07-11 stsp struct got_reference *ref;
6251 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6252 69844fba 2019-07-11 stsp if (err) {
6253 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6254 69844fba 2019-07-11 stsp return NULL;
6255 69844fba 2019-07-11 stsp return err;
6258 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6259 69844fba 2019-07-11 stsp got_ref_close(ref);
6260 69844fba 2019-07-11 stsp return err;
6263 69844fba 2019-07-11 stsp static const struct got_error *
6264 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6266 69844fba 2019-07-11 stsp const struct got_error *err;
6267 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6268 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6270 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6272 69844fba 2019-07-11 stsp goto done;
6273 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6275 69844fba 2019-07-11 stsp goto done;
6277 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6279 69844fba 2019-07-11 stsp goto done;
6280 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6282 69844fba 2019-07-11 stsp goto done;
6284 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6286 69844fba 2019-07-11 stsp goto done;
6287 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6289 69844fba 2019-07-11 stsp goto done;
6291 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6293 69844fba 2019-07-11 stsp goto done;
6294 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6296 69844fba 2019-07-11 stsp goto done;
6299 69844fba 2019-07-11 stsp free(tmp_branch_name);
6300 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6301 69844fba 2019-07-11 stsp free(branch_ref_name);
6302 69844fba 2019-07-11 stsp free(commit_ref_name);
6303 69844fba 2019-07-11 stsp return err;
6306 818c7501 2019-07-11 stsp const struct got_error *
6307 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
6308 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6309 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6310 818c7501 2019-07-11 stsp struct got_repository *repo)
6312 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
6313 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
6315 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6317 818c7501 2019-07-11 stsp return err;
6319 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6321 818c7501 2019-07-11 stsp goto done;
6323 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
6325 818c7501 2019-07-11 stsp goto done;
6327 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
6329 818c7501 2019-07-11 stsp goto done;
6331 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
6333 3e3a69f1 2019-07-25 stsp if (fileindex)
6334 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6335 818c7501 2019-07-11 stsp free(new_head_commit_id);
6336 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6337 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6338 818c7501 2019-07-11 stsp err = unlockerr;
6339 818c7501 2019-07-11 stsp return err;
6342 818c7501 2019-07-11 stsp const struct got_error *
6343 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
6344 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6345 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
6346 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6348 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
6349 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
6350 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
6351 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6352 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6353 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
6355 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6357 818c7501 2019-07-11 stsp return err;
6359 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
6360 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
6362 818c7501 2019-07-11 stsp goto done;
6364 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
6366 818c7501 2019-07-11 stsp goto done;
6369 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
6370 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
6371 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
6373 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
6375 818c7501 2019-07-11 stsp goto done;
6377 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6379 818c7501 2019-07-11 stsp goto done;
6381 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
6382 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
6384 ca355955 2019-07-12 stsp goto done;
6386 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
6388 ca355955 2019-07-12 stsp goto done;
6390 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6392 818c7501 2019-07-11 stsp goto done;
6394 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6395 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6396 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6397 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6398 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6399 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6400 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6401 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6402 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6404 1f1abb7e 2019-08-08 stsp goto sync;
6406 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6407 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
6409 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6410 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
6411 ca355955 2019-07-12 stsp err = sync_err;
6413 818c7501 2019-07-11 stsp got_ref_close(resolved);
6414 a3a2faf2 2019-07-12 stsp free(tree_id);
6415 818c7501 2019-07-11 stsp free(commit_id);
6416 0ebf8283 2019-07-24 stsp if (fileindex)
6417 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
6418 0ebf8283 2019-07-24 stsp free(fileindex_path);
6420 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6421 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6422 0ebf8283 2019-07-24 stsp err = unlockerr;
6423 0ebf8283 2019-07-24 stsp return err;
6426 0ebf8283 2019-07-24 stsp const struct got_error *
6427 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6428 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6429 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
6430 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6432 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6433 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6434 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
6435 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
6436 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6437 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
6438 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
6439 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6441 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6442 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6443 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6444 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6446 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6448 0ebf8283 2019-07-24 stsp return err;
6450 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6452 0ebf8283 2019-07-24 stsp goto done;
6454 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
6455 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
6456 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6459 0ebf8283 2019-07-24 stsp goto done;
6461 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6463 0ebf8283 2019-07-24 stsp goto done;
6465 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6467 0ebf8283 2019-07-24 stsp goto done;
6469 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6470 0ebf8283 2019-07-24 stsp worktree);
6472 0ebf8283 2019-07-24 stsp goto done;
6474 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6477 0ebf8283 2019-07-24 stsp goto done;
6479 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6481 0ebf8283 2019-07-24 stsp goto done;
6483 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
6485 0ebf8283 2019-07-24 stsp goto done;
6487 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6488 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6490 0ebf8283 2019-07-24 stsp goto done;
6491 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
6493 0ebf8283 2019-07-24 stsp goto done;
6494 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6495 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
6496 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
6497 0ebf8283 2019-07-24 stsp goto done;
6500 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6501 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6503 0ebf8283 2019-07-24 stsp goto done;
6504 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
6506 0ebf8283 2019-07-24 stsp goto done;
6508 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6510 0ebf8283 2019-07-24 stsp goto done;
6512 0ebf8283 2019-07-24 stsp free(fileindex_path);
6513 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6514 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6515 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6516 0ebf8283 2019-07-24 stsp if (wt_branch)
6517 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
6518 0ebf8283 2019-07-24 stsp if (err) {
6519 0ebf8283 2019-07-24 stsp if (*branch_ref) {
6520 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
6521 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6523 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6524 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6525 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6527 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6528 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6529 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6530 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6532 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
6534 0ebf8283 2019-07-24 stsp return err;
6537 0ebf8283 2019-07-24 stsp const struct got_error *
6538 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
6539 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6541 3e3a69f1 2019-07-25 stsp if (fileindex)
6542 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6543 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
6546 0ebf8283 2019-07-24 stsp const struct got_error *
6547 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
6548 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
6550 0ebf8283 2019-07-24 stsp const struct got_error *err;
6551 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6553 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6555 0ebf8283 2019-07-24 stsp return err;
6557 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6558 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6559 0ebf8283 2019-07-24 stsp return NULL;
6562 0ebf8283 2019-07-24 stsp const struct got_error *
6563 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
6564 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
6565 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6566 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6568 0ebf8283 2019-07-24 stsp const struct got_error *err;
6569 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6570 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6571 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6572 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6573 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6574 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6576 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6577 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6578 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6579 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6581 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6583 3e3a69f1 2019-07-25 stsp return err;
6585 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6587 f032f1f7 2019-08-04 stsp goto done;
6589 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6590 f032f1f7 2019-08-04 stsp &have_staged_files);
6591 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6592 f032f1f7 2019-08-04 stsp goto done;
6593 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6594 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6595 3e3a69f1 2019-07-25 stsp goto done;
6598 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6600 f032f1f7 2019-08-04 stsp goto done;
6602 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6604 0ebf8283 2019-07-24 stsp goto done;
6606 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6608 0ebf8283 2019-07-24 stsp goto done;
6610 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6611 0ebf8283 2019-07-24 stsp worktree);
6613 0ebf8283 2019-07-24 stsp goto done;
6615 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6617 0ebf8283 2019-07-24 stsp goto done;
6619 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6621 0ebf8283 2019-07-24 stsp goto done;
6622 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6624 0ebf8283 2019-07-24 stsp goto done;
6626 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6628 0ebf8283 2019-07-24 stsp goto done;
6629 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6631 0ebf8283 2019-07-24 stsp goto done;
6633 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6635 0ebf8283 2019-07-24 stsp goto done;
6637 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6638 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6639 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6640 0ebf8283 2019-07-24 stsp if (commit_ref)
6641 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6642 0ebf8283 2019-07-24 stsp if (base_commit_ref)
6643 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
6644 0ebf8283 2019-07-24 stsp if (err) {
6645 0ebf8283 2019-07-24 stsp free(*commit_id);
6646 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6647 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6648 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6649 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6650 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6651 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6653 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6654 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6655 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6657 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
6659 0ebf8283 2019-07-24 stsp return err;
6662 0ebf8283 2019-07-24 stsp static const struct got_error *
6663 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6665 0ebf8283 2019-07-24 stsp const struct got_error *err;
6666 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6667 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6669 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6671 0ebf8283 2019-07-24 stsp goto done;
6672 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
6674 0ebf8283 2019-07-24 stsp goto done;
6676 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6677 0ebf8283 2019-07-24 stsp worktree);
6679 0ebf8283 2019-07-24 stsp goto done;
6680 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
6682 0ebf8283 2019-07-24 stsp goto done;
6684 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6686 0ebf8283 2019-07-24 stsp goto done;
6687 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
6689 0ebf8283 2019-07-24 stsp goto done;
6691 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6693 0ebf8283 2019-07-24 stsp goto done;
6694 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
6696 0ebf8283 2019-07-24 stsp goto done;
6698 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6699 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6700 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6701 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6702 0ebf8283 2019-07-24 stsp return err;
6705 0ebf8283 2019-07-24 stsp const struct got_error *
6706 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
6707 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6708 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
6709 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6711 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
6712 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6713 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6714 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
6715 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6717 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6719 0ebf8283 2019-07-24 stsp return err;
6721 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
6722 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
6724 0ebf8283 2019-07-24 stsp goto done;
6726 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
6728 0ebf8283 2019-07-24 stsp goto done;
6730 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6732 0ebf8283 2019-07-24 stsp goto done;
6734 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6735 0ebf8283 2019-07-24 stsp worktree->path_prefix);
6737 0ebf8283 2019-07-24 stsp goto done;
6739 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
6741 0ebf8283 2019-07-24 stsp goto done;
6743 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6745 0ebf8283 2019-07-24 stsp goto done;
6747 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6748 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6749 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6750 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6751 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6752 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6753 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6754 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
6755 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6757 1f1abb7e 2019-08-08 stsp goto sync;
6759 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6760 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
6762 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6763 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
6764 0ebf8283 2019-07-24 stsp err = sync_err;
6766 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
6767 0ebf8283 2019-07-24 stsp free(tree_id);
6768 818c7501 2019-07-11 stsp free(fileindex_path);
6770 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6771 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6772 818c7501 2019-07-11 stsp err = unlockerr;
6773 818c7501 2019-07-11 stsp return err;
6776 0ebf8283 2019-07-24 stsp const struct got_error *
6777 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
6778 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6779 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
6781 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr;
6782 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
6783 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6785 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6787 0ebf8283 2019-07-24 stsp return err;
6789 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
6790 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
6792 0ebf8283 2019-07-24 stsp goto done;
6794 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
6796 0ebf8283 2019-07-24 stsp goto done;
6798 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
6800 0ebf8283 2019-07-24 stsp goto done;
6802 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
6804 0ebf8283 2019-07-24 stsp goto done;
6806 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
6808 3e3a69f1 2019-07-25 stsp if (fileindex)
6809 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6810 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
6811 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6812 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6813 0ebf8283 2019-07-24 stsp err = unlockerr;
6814 0ebf8283 2019-07-24 stsp return err;
6817 0ebf8283 2019-07-24 stsp const struct got_error *
6818 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6819 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6821 0ebf8283 2019-07-24 stsp const struct got_error *err;
6822 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6824 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6826 0ebf8283 2019-07-24 stsp return err;
6828 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6830 0ebf8283 2019-07-24 stsp goto done;
6832 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
6834 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6835 2822a352 2019-10-15 stsp return err;
6838 2822a352 2019-10-15 stsp const struct got_error *
6839 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6840 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6841 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
6842 2822a352 2019-10-15 stsp struct got_repository *repo)
6844 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
6845 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
6846 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
6848 2822a352 2019-10-15 stsp *fileindex = NULL;
6849 2822a352 2019-10-15 stsp *branch_ref = NULL;
6850 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
6852 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
6854 2822a352 2019-10-15 stsp return err;
6856 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6857 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
6858 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
6859 2822a352 2019-10-15 stsp "update -b or different branch name required");
6860 2822a352 2019-10-15 stsp goto done;
6863 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6865 2822a352 2019-10-15 stsp goto done;
6867 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
6868 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
6869 2822a352 2019-10-15 stsp ok_arg.repo = repo;
6870 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6873 2822a352 2019-10-15 stsp goto done;
6875 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
6877 2822a352 2019-10-15 stsp goto done;
6879 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
6880 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
6882 2822a352 2019-10-15 stsp if (err) {
6883 2822a352 2019-10-15 stsp if (*branch_ref) {
6884 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
6885 2822a352 2019-10-15 stsp *branch_ref = NULL;
6887 2822a352 2019-10-15 stsp if (*base_branch_ref) {
6888 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
6889 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
6891 2822a352 2019-10-15 stsp if (*fileindex) {
6892 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
6893 2822a352 2019-10-15 stsp *fileindex = NULL;
6895 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
6897 0cb83759 2019-08-03 stsp return err;
6900 2822a352 2019-10-15 stsp const struct got_error *
6901 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
6902 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6903 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6904 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6905 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6907 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
6908 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
6909 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
6911 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
6913 2822a352 2019-10-15 stsp goto done;
6915 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
6917 2822a352 2019-10-15 stsp goto done;
6919 2822a352 2019-10-15 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
6920 2822a352 2019-10-15 stsp worktree->path_prefix);
6922 2822a352 2019-10-15 stsp goto done;
6924 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6926 2822a352 2019-10-15 stsp goto done;
6928 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6929 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
6931 2822a352 2019-10-15 stsp goto sync;
6933 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
6935 2822a352 2019-10-15 stsp goto sync;
6937 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
6939 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6940 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
6941 2822a352 2019-10-15 stsp err = sync_err;
6944 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
6945 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6946 2822a352 2019-10-15 stsp err = unlockerr;
6947 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
6949 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
6950 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6951 2822a352 2019-10-15 stsp err = unlockerr;
6952 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
6954 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
6955 2822a352 2019-10-15 stsp free(fileindex_path);
6956 2822a352 2019-10-15 stsp free(tree_id);
6958 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6959 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6960 2822a352 2019-10-15 stsp err = unlockerr;
6961 2822a352 2019-10-15 stsp return err;
6964 2822a352 2019-10-15 stsp const struct got_error *
6965 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
6966 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6967 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6969 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
6971 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
6973 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
6975 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
6976 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
6977 8b692cd0 2019-10-21 stsp err = unlockerr;
6978 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
6980 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
6981 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
6982 8b692cd0 2019-10-21 stsp err = unlockerr;
6983 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
6985 8b692cd0 2019-10-21 stsp return err;
6988 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
6989 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
6990 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
6991 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
6992 2db2652d 2019-08-07 stsp struct got_repository *repo;
6993 2db2652d 2019-08-07 stsp int have_changes;
6996 2db2652d 2019-08-07 stsp const struct got_error *
6997 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
6998 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
6999 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7000 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7002 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
7003 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
7004 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
7005 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
7006 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
7007 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
7009 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
7010 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
7011 8b13ce36 2019-08-08 stsp return NULL;
7012 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
7013 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
7015 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7016 735ef5ac 2019-08-03 stsp if (ie == NULL)
7017 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7019 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7020 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7021 735ef5ac 2019-08-03 stsp relpath) == -1)
7022 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
7024 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
7025 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
7026 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7027 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
7030 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
7031 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7032 3aa5969e 2019-08-06 stsp goto done;
7033 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
7034 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
7035 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
7036 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7037 735ef5ac 2019-08-03 stsp goto done;
7040 2db2652d 2019-08-07 stsp a->have_changes = 1;
7042 735ef5ac 2019-08-03 stsp p = in_repo_path;
7043 735ef5ac 2019-08-03 stsp while (p[0] == '/')
7045 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
7046 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
7047 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
7049 735ef5ac 2019-08-03 stsp free(in_repo_path);
7050 dc424a06 2019-08-07 stsp return err;
7053 2db2652d 2019-08-07 stsp struct stage_path_arg {
7054 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7055 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7056 2db2652d 2019-08-07 stsp struct got_repository *repo;
7057 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
7058 2db2652d 2019-08-07 stsp void *status_arg;
7059 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
7060 2db2652d 2019-08-07 stsp void *patch_arg;
7061 7b5dc508 2019-10-28 stsp int staged_something;
7062 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
7065 2db2652d 2019-08-07 stsp static const struct got_error *
7066 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
7067 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7068 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7069 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7071 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
7072 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
7073 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
7074 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
7075 0cb83759 2019-08-03 stsp uint32_t stage;
7076 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
7077 0aeb8099 2020-07-23 stsp struct stat sb;
7079 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
7080 8b13ce36 2019-08-08 stsp return NULL;
7082 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7083 d3e7c587 2019-08-03 stsp if (ie == NULL)
7084 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7086 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7087 2db2652d 2019-08-07 stsp relpath)== -1)
7088 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
7090 0cb83759 2019-08-03 stsp switch (status) {
7091 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
7092 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
7093 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
7094 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
7095 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
7098 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7099 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
7100 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7101 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7102 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
7105 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
7108 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
7109 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
7110 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
7111 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
7112 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
7116 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
7117 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
7120 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7121 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7122 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7123 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
7125 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
7126 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7127 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
7128 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
7129 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
7130 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
7131 35213c7c 2020-07-23 stsp ssize_t target_len;
7132 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
7133 35213c7c 2020-07-23 stsp sizeof(target_path));
7134 35213c7c 2020-07-23 stsp if (target_len == -1) {
7135 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
7136 35213c7c 2020-07-23 stsp ondisk_path);
7139 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
7140 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
7141 35213c7c 2020-07-23 stsp a->worktree->root_path);
7144 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
7145 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
7146 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
7150 35213c7c 2020-07-23 stsp if (is_bad_symlink)
7151 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7152 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
7154 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7155 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
7157 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7158 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
7160 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7161 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7163 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7164 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
7165 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
7167 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
7168 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
7170 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7171 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7172 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
7173 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
7176 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7178 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7179 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7183 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
7184 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7185 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7186 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7188 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7189 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7192 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
7194 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
7195 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7197 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
7198 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
7201 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7205 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
7206 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
7207 dc424a06 2019-08-07 stsp free(path_content);
7208 2db2652d 2019-08-07 stsp free(ondisk_path);
7209 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
7210 0ebf8283 2019-07-24 stsp return err;
7213 0cb83759 2019-08-03 stsp const struct got_error *
7214 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
7215 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
7216 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
7217 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7218 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
7220 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7221 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
7222 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7223 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
7224 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
7225 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
7226 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
7227 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
7229 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7231 0cb83759 2019-08-03 stsp return err;
7233 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
7234 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
7236 735ef5ac 2019-08-03 stsp goto done;
7237 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7239 735ef5ac 2019-08-03 stsp goto done;
7240 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7242 0cb83759 2019-08-03 stsp goto done;
7244 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
7245 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
7246 2db2652d 2019-08-07 stsp oka.worktree = worktree;
7247 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
7248 2db2652d 2019-08-07 stsp oka.repo = repo;
7249 2db2652d 2019-08-07 stsp oka.have_changes = 0;
7250 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7251 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7252 f2a9dc41 2019-12-13 tracey check_stage_ok, &oka, NULL, NULL, 0, 0);
7254 735ef5ac 2019-08-03 stsp goto done;
7256 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
7257 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7258 2db2652d 2019-08-07 stsp goto done;
7261 2db2652d 2019-08-07 stsp spa.worktree = worktree;
7262 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
7263 2db2652d 2019-08-07 stsp spa.repo = repo;
7264 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
7265 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
7266 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
7267 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
7268 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
7269 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
7270 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7271 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7272 f2a9dc41 2019-12-13 tracey stage_path, &spa, NULL, NULL, 0, 0);
7274 2db2652d 2019-08-07 stsp goto done;
7276 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
7277 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7278 7b5dc508 2019-10-28 stsp goto done;
7281 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7282 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
7283 0cb83759 2019-08-03 stsp err = sync_err;
7285 735ef5ac 2019-08-03 stsp if (head_ref)
7286 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
7287 735ef5ac 2019-08-03 stsp free(head_commit_id);
7288 0cb83759 2019-08-03 stsp free(fileindex_path);
7289 0cb83759 2019-08-03 stsp if (fileindex)
7290 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
7291 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7292 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
7293 0cb83759 2019-08-03 stsp err = unlockerr;
7294 0cb83759 2019-08-03 stsp return err;
7297 ad493afc 2019-08-03 stsp struct unstage_path_arg {
7298 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
7299 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
7300 ad493afc 2019-08-03 stsp struct got_repository *repo;
7301 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
7302 ad493afc 2019-08-03 stsp void *progress_arg;
7303 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
7304 2e1f37b0 2019-08-08 stsp void *patch_arg;
7307 2e1f37b0 2019-08-08 stsp static const struct got_error *
7308 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
7309 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
7310 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
7311 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
7312 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
7314 2e1f37b0 2019-08-08 stsp const struct got_error *err;
7315 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
7316 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7317 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7318 2e1f37b0 2019-08-08 stsp struct stat sb1, sb2;
7319 2e1f37b0 2019-08-08 stsp struct got_diff_changes *changes = NULL;
7320 2e1f37b0 2019-08-08 stsp struct got_diff_state *ds = NULL;
7321 2e1f37b0 2019-08-08 stsp struct got_diff_args *args = NULL;
7322 2e1f37b0 2019-08-08 stsp struct got_diff_change *change;
7323 2e1f37b0 2019-08-08 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7324 2e1f37b0 2019-08-08 stsp int have_content = 0, have_rejected_content = 0;
7326 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7327 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7329 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
7331 2e1f37b0 2019-08-08 stsp return err;
7332 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7334 2e1f37b0 2019-08-08 stsp goto done;
7336 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7338 2e1f37b0 2019-08-08 stsp goto done;
7340 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7342 2e1f37b0 2019-08-08 stsp goto done;
7344 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7346 2e1f37b0 2019-08-08 stsp goto done;
7348 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7350 2e1f37b0 2019-08-08 stsp goto done;
7352 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7354 2e1f37b0 2019-08-08 stsp goto done;
7356 2e1f37b0 2019-08-08 stsp if (stat(path1, &sb1) == -1) {
7357 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path1);
7358 2e1f37b0 2019-08-08 stsp goto done;
7361 2e1f37b0 2019-08-08 stsp if (stat(path2, &sb2) == -1) {
7362 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path2);
7363 2e1f37b0 2019-08-08 stsp goto done;
7366 2e1f37b0 2019-08-08 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
7367 2e1f37b0 2019-08-08 stsp f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7369 2e1f37b0 2019-08-08 stsp goto done;
7371 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
7372 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
7374 2e1f37b0 2019-08-08 stsp goto done;
7375 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
7376 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
7378 2e1f37b0 2019-08-08 stsp goto done;
7380 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
7381 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
7382 2e1f37b0 2019-08-08 stsp goto done;
7384 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
7385 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
7386 2e1f37b0 2019-08-08 stsp goto done;
7388 2e1f37b0 2019-08-08 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7389 2e1f37b0 2019-08-08 stsp int choice;
7390 2e1f37b0 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
7391 2e1f37b0 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
7392 2e1f37b0 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
7393 2e1f37b0 2019-08-08 stsp outfile, rejectfile, patch_cb, patch_arg);
7395 2e1f37b0 2019-08-08 stsp goto done;
7396 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
7397 2e1f37b0 2019-08-08 stsp have_content = 1;
7399 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
7400 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
7403 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
7404 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7405 f1e81a05 2019-08-10 stsp outfile, rejectfile);
7407 2e1f37b0 2019-08-08 stsp free(label1);
7409 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
7410 2e1f37b0 2019-08-08 stsp if (staged_blob)
7411 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
7412 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
7413 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
7414 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
7415 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
7416 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
7417 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
7418 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7419 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
7420 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
7421 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
7422 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
7423 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
7424 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
7425 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
7426 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
7427 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7428 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
7429 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
7430 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7432 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
7433 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
7434 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
7435 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7436 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
7437 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
7438 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7440 2e1f37b0 2019-08-08 stsp free(args);
7442 2e1f37b0 2019-08-08 stsp got_diff_state_free(ds);
7445 2e1f37b0 2019-08-08 stsp if (changes)
7446 2e1f37b0 2019-08-08 stsp got_diff_free_changes(changes);
7447 2e1f37b0 2019-08-08 stsp free(path1);
7448 2e1f37b0 2019-08-08 stsp free(path2);
7449 fda8017d 2020-07-23 stsp return err;
7452 fda8017d 2020-07-23 stsp static const struct got_error *
7453 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
7454 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
7455 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7456 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
7457 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
7458 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7459 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7461 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
7462 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
7463 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
7464 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
7465 fda8017d 2020-07-23 stsp FILE *f = NULL;
7466 fda8017d 2020-07-23 stsp struct stat sb;
7468 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
7469 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
7470 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
7472 fda8017d 2020-07-23 stsp return err;
7474 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
7475 fda8017d 2020-07-23 stsp return NULL;
7477 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
7478 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
7479 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
7481 fda8017d 2020-07-23 stsp goto done;
7484 fda8017d 2020-07-23 stsp f = fopen(path_unstaged_content, "r");
7485 fda8017d 2020-07-23 stsp if (f == NULL) {
7486 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
7487 fda8017d 2020-07-23 stsp path_unstaged_content);
7488 fda8017d 2020-07-23 stsp goto done;
7490 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
7491 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
7492 fda8017d 2020-07-23 stsp goto done;
7494 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
7495 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7496 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
7498 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
7499 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
7500 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
7501 fda8017d 2020-07-23 stsp goto done;
7503 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
7504 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7505 fda8017d 2020-07-23 stsp goto done;
7507 fda8017d 2020-07-23 stsp link_target[r] = '\0';
7508 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
7509 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
7510 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
7511 fda8017d 2020-07-23 stsp progress_arg);
7513 fda8017d 2020-07-23 stsp int local_changes_subsumed;
7514 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
7515 fda8017d 2020-07-23 stsp blob_base, ondisk_path, ie->path,
7516 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
7517 fda8017d 2020-07-23 stsp path_unstaged_content, label_orig, "unstaged",
7518 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
7521 fda8017d 2020-07-23 stsp goto done;
7523 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
7524 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7525 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
7527 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7529 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
7530 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
7531 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
7532 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
7533 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
7534 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
7535 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
7536 fda8017d 2020-07-23 stsp if (f && fclose(f) != 0 && err == NULL)
7537 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
7538 fda8017d 2020-07-23 stsp free(path_unstaged_content);
7539 fda8017d 2020-07-23 stsp free(path_new_staged_content);
7540 2e1f37b0 2019-08-08 stsp return err;
7543 ad493afc 2019-08-03 stsp static const struct got_error *
7544 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
7545 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
7546 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7547 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7549 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
7550 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
7551 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
7552 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7553 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
7554 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
7555 ad493afc 2019-08-03 stsp int local_changes_subsumed;
7556 9bc94a15 2019-08-03 stsp struct stat sb;
7558 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
7559 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
7560 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
7561 2e1f37b0 2019-08-08 stsp return NULL;
7563 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7564 ad493afc 2019-08-03 stsp if (ie == NULL)
7565 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7567 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7569 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
7571 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
7572 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
7574 f69721c3 2019-10-21 stsp goto done;
7575 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7576 f69721c3 2019-10-21 stsp id_str) == -1) {
7577 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
7578 f69721c3 2019-10-21 stsp goto done;
7581 ad493afc 2019-08-03 stsp switch (staged_status) {
7582 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
7583 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
7584 ad493afc 2019-08-03 stsp blob_id, 8192);
7587 ad493afc 2019-08-03 stsp /* fall through */
7588 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
7589 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7590 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
7591 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7592 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7593 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7596 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
7599 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
7600 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
7601 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
7602 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
7603 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
7604 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
7607 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
7608 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
7611 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
7612 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
7613 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
7614 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
7615 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
7616 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
7617 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
7618 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
7619 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
7621 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
7622 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7623 36bf999c 2020-07-23 stsp char *staged_target;
7624 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
7625 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
7627 36bf999c 2020-07-23 stsp goto done;
7628 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
7629 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
7630 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
7631 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
7632 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
7633 36bf999c 2020-07-23 stsp free(staged_target);
7635 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
7636 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
7637 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
7638 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
7639 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
7640 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
7641 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
7645 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7648 ad493afc 2019-08-03 stsp if (err == NULL)
7649 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
7650 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
7652 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
7653 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7654 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7655 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7656 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7659 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7661 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7662 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7666 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7667 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
7668 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
7671 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
7675 ad493afc 2019-08-03 stsp free(ondisk_path);
7676 ad493afc 2019-08-03 stsp if (blob_base)
7677 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
7678 ad493afc 2019-08-03 stsp if (blob_staged)
7679 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
7680 f69721c3 2019-10-21 stsp free(id_str);
7681 f69721c3 2019-10-21 stsp free(label_orig);
7682 ad493afc 2019-08-03 stsp return err;
7685 ad493afc 2019-08-03 stsp const struct got_error *
7686 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
7687 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
7688 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7689 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7690 ad493afc 2019-08-03 stsp struct got_repository *repo)
7692 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7693 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
7694 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7695 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
7696 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
7698 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7700 ad493afc 2019-08-03 stsp return err;
7702 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7704 ad493afc 2019-08-03 stsp goto done;
7706 ad493afc 2019-08-03 stsp upa.worktree = worktree;
7707 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
7708 ad493afc 2019-08-03 stsp upa.repo = repo;
7709 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
7710 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
7711 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
7712 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
7713 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7714 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7715 f2a9dc41 2019-12-13 tracey unstage_path, &upa, NULL, NULL, 0, 0);
7717 ad493afc 2019-08-03 stsp goto done;
7720 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7721 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
7722 ad493afc 2019-08-03 stsp err = sync_err;
7724 ad493afc 2019-08-03 stsp free(fileindex_path);
7725 ad493afc 2019-08-03 stsp if (fileindex)
7726 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
7727 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7728 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
7729 ad493afc 2019-08-03 stsp err = unlockerr;
7730 ad493afc 2019-08-03 stsp return err;
7733 b2118c49 2020-07-28 stsp struct report_file_info_arg {
7734 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
7735 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
7736 b2118c49 2020-07-28 stsp void *info_arg;
7737 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
7738 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
7739 b2118c49 2020-07-28 stsp void *cancel_arg;
7742 b2118c49 2020-07-28 stsp static const struct got_error *
7743 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
7745 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
7746 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
7747 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
7748 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7749 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
7750 b2118c49 2020-07-28 stsp int stage;
7752 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7753 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
7755 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
7756 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7757 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
7760 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
7761 b2118c49 2020-07-28 stsp return NULL;
7763 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_blob(ie)) {
7764 b2118c49 2020-07-28 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7765 b2118c49 2020-07-28 stsp blob_idp = &blob_id;
7767 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
7768 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7769 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
7770 b2118c49 2020-07-28 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7771 b2118c49 2020-07-28 stsp SHA1_DIGEST_LENGTH);
7772 b2118c49 2020-07-28 stsp staged_blob_idp = &staged_blob_id;
7775 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_commit(ie)) {
7776 b2118c49 2020-07-28 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
7777 b2118c49 2020-07-28 stsp commit_idp = &commit_id;
7780 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
7781 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
7784 b2118c49 2020-07-28 stsp const struct got_error *
7785 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
7786 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
7787 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
7788 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7791 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
7792 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
7793 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
7794 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
7796 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
7798 b2118c49 2020-07-28 stsp return err;
7800 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7802 b2118c49 2020-07-28 stsp goto done;
7804 b2118c49 2020-07-28 stsp arg.worktree = worktree;
7805 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
7806 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
7807 b2118c49 2020-07-28 stsp arg.paths = paths;
7808 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
7809 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
7810 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
7813 b2118c49 2020-07-28 stsp free(fileindex_path);
7814 b2118c49 2020-07-28 stsp if (fileindex)
7815 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
7816 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
7817 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
7818 b2118c49 2020-07-28 stsp err = unlockerr;
7819 b2118c49 2020-07-28 stsp return err;