2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 86c3caaf 2018-03-09 stsp * Permission to use, copy, modify, and distribute this software for any
5 86c3caaf 2018-03-09 stsp * purpose with or without fee is hereby granted, provided that the above
6 86c3caaf 2018-03-09 stsp * copyright notice and this permission notice appear in all copies.
8 86c3caaf 2018-03-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 86c3caaf 2018-03-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 86c3caaf 2018-03-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 86c3caaf 2018-03-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 86c3caaf 2018-03-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 86c3caaf 2018-03-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 86c3caaf 2018-03-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 86c3caaf 2018-03-09 stsp #include <sys/stat.h>
18 9d31a1d8 2018-03-11 stsp #include <sys/queue.h>
19 133d2798 2019-01-08 stsp #include <sys/tree.h>
21 d1f6d47b 2019-02-04 stsp #include <dirent.h>
22 12ce7a6c 2019-08-12 stsp #include <limits.h>
23 f5c49f82 2019-01-06 stsp #include <stddef.h>
24 86c3caaf 2018-03-09 stsp #include <string.h>
25 86c3caaf 2018-03-09 stsp #include <stdio.h>
26 86c3caaf 2018-03-09 stsp #include <stdlib.h>
27 303e14b5 2019-09-23 stsp #include <time.h>
28 86c3caaf 2018-03-09 stsp #include <fcntl.h>
29 86c3caaf 2018-03-09 stsp #include <errno.h>
30 86c3caaf 2018-03-09 stsp #include <unistd.h>
31 9d31a1d8 2018-03-11 stsp #include <sha1.h>
32 9d31a1d8 2018-03-11 stsp #include <zlib.h>
33 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
34 512f0d0e 2019-01-02 stsp #include <libgen.h>
35 ec22038e 2019-03-10 stsp #include <uuid.h>
36 7154f6ce 2019-03-27 stsp #include <util.h>
38 86c3caaf 2018-03-09 stsp #include "got_error.h"
39 86c3caaf 2018-03-09 stsp #include "got_repository.h"
40 5261c201 2018-04-01 stsp #include "got_reference.h"
41 9d31a1d8 2018-03-11 stsp #include "got_object.h"
42 1dd54920 2019-05-11 stsp #include "got_path.h"
43 e6209546 2019-08-22 stsp #include "got_cancel.h"
44 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
45 511a516b 2018-05-19 stsp #include "got_opentemp.h"
46 234035bc 2019-06-01 stsp #include "got_diff.h"
48 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
49 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
51 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
52 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
53 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
54 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
55 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
56 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
57 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
58 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
61 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
65 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
67 99724ed4 2018-03-10 stsp static const struct got_error *
68 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
70 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
73 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
76 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
81 507dc3bb 2018-12-29 stsp static const struct got_error *
82 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
84 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
85 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
86 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
87 507dc3bb 2018-12-29 stsp char *path = NULL;
89 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
91 507dc3bb 2018-12-29 stsp path = NULL;
95 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
99 507dc3bb 2018-12-29 stsp if (content) {
100 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
101 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
102 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
107 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
108 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
109 2a57020b 2019-02-20 stsp unlink(tmppath);
114 fb43ecf1 2019-02-11 stsp if (fclose(tmpfile) != 0 && err == NULL)
115 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
116 230a42bd 2019-05-11 jcs free(tmppath);
117 99724ed4 2018-03-10 stsp return err;
120 09fe317a 2018-03-11 stsp static const struct got_error *
121 7ac97322 2018-03-11 stsp read_meta_file(char **content, const char *path_got, const char *name)
123 09fe317a 2018-03-11 stsp const struct got_error *err = NULL;
124 09fe317a 2018-03-11 stsp char *path;
125 09fe317a 2018-03-11 stsp int fd = -1;
127 09fe317a 2018-03-11 stsp struct stat sb;
129 09fe317a 2018-03-11 stsp *content = NULL;
131 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
132 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
133 09fe317a 2018-03-11 stsp path = NULL;
137 ef99fdb1 2018-03-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
138 09fe317a 2018-03-11 stsp if (fd == -1) {
139 f02eaa22 2019-03-11 stsp if (errno == ENOENT)
140 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
142 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", path);
145 ef99fdb1 2018-03-11 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
146 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
147 638f9024 2019-05-13 stsp : got_error_from_errno2("flock", path));
151 e4b9a50c 2019-07-27 stsp if (fstat(fd, &sb) != 0) {
152 e4b9a50c 2019-07-27 stsp err = got_error_from_errno2("fstat", path);
155 09fe317a 2018-03-11 stsp *content = calloc(1, sb.st_size);
156 09fe317a 2018-03-11 stsp if (*content == NULL) {
157 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
161 09fe317a 2018-03-11 stsp n = read(fd, *content, sb.st_size);
162 09fe317a 2018-03-11 stsp if (n != sb.st_size) {
163 638f9024 2019-05-13 stsp err = (n == -1 ? got_error_from_errno2("read", path) :
164 a2e6d162 2019-07-27 stsp got_error_path(path, GOT_ERR_WORKTREE_META));
167 09fe317a 2018-03-11 stsp if ((*content)[sb.st_size - 1] != '\n') {
168 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
171 09fe317a 2018-03-11 stsp (*content)[sb.st_size - 1] = '\0';
174 09fe317a 2018-03-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
175 638f9024 2019-05-13 stsp err = got_error_from_errno2("close", path_got);
176 09fe317a 2018-03-11 stsp free(path);
178 09fe317a 2018-03-11 stsp free(*content);
179 09fe317a 2018-03-11 stsp *content = NULL;
181 09fe317a 2018-03-11 stsp return err;
184 024e9686 2019-05-14 stsp static const struct got_error *
185 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
187 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
188 024e9686 2019-05-14 stsp char *refstr = NULL;
190 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
191 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
192 024e9686 2019-05-14 stsp if (refstr == NULL)
193 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
195 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
196 024e9686 2019-05-14 stsp if (refstr == NULL)
197 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
199 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
200 024e9686 2019-05-14 stsp free(refstr);
201 024e9686 2019-05-14 stsp return err;
204 86c3caaf 2018-03-09 stsp const struct got_error *
205 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
206 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
208 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
209 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
210 ec22038e 2019-03-10 stsp uuid_t uuid;
211 ec22038e 2019-03-10 stsp uint32_t uuid_status;
212 65596e15 2018-12-24 stsp int obj_type;
213 7ac97322 2018-03-11 stsp char *path_got = NULL;
214 1451e70d 2018-03-10 stsp char *formatstr = NULL;
215 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
216 65596e15 2018-12-24 stsp char *basestr = NULL;
217 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
219 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
220 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
224 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
226 65596e15 2018-12-24 stsp return err;
227 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
229 65596e15 2018-12-24 stsp return err;
230 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
231 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
233 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
234 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
235 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
238 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
239 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
240 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
244 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
245 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
246 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
249 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
250 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
254 056e7441 2018-03-11 stsp /* Create an empty lock file. */
255 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
259 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
260 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
264 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
265 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
269 65596e15 2018-12-24 stsp /* Record our base commit. */
270 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
273 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
277 1451e70d 2018-03-10 stsp /* Store path to repository. */
278 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
279 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
283 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
284 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
285 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
289 ec22038e 2019-03-10 stsp /* Generate UUID. */
290 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
291 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
292 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
295 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
296 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
297 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
300 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
304 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
305 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
306 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
309 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
314 65596e15 2018-12-24 stsp free(commit_id);
315 7ac97322 2018-03-11 stsp free(path_got);
316 1451e70d 2018-03-10 stsp free(formatstr);
317 0bb8a95e 2018-03-12 stsp free(absprefix);
318 65596e15 2018-12-24 stsp free(basestr);
319 ec22038e 2019-03-10 stsp free(uuidstr);
320 86c3caaf 2018-03-09 stsp return err;
323 247140b2 2019-02-05 stsp static const struct got_error *
324 247140b2 2019-02-05 stsp open_worktree(struct got_worktree **worktree, const char *path)
326 6d9d28c3 2018-03-11 stsp const struct got_error *err = NULL;
327 7ac97322 2018-03-11 stsp char *path_got;
328 6d9d28c3 2018-03-11 stsp char *formatstr = NULL;
329 c442a90d 2019-03-10 stsp char *uuidstr = NULL;
330 056e7441 2018-03-11 stsp char *path_lock = NULL;
331 eaccb85f 2018-12-25 stsp char *base_commit_id_str = NULL;
332 6d9d28c3 2018-03-11 stsp int version, fd = -1;
333 6d9d28c3 2018-03-11 stsp const char *errstr;
334 eaccb85f 2018-12-25 stsp struct got_repository *repo = NULL;
335 c442a90d 2019-03-10 stsp uint32_t uuid_status;
337 6d9d28c3 2018-03-11 stsp *worktree = NULL;
339 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
340 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
341 7ac97322 2018-03-11 stsp path_got = NULL;
345 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
346 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
347 056e7441 2018-03-11 stsp path_lock = NULL;
351 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
352 6d9d28c3 2018-03-11 stsp if (fd == -1) {
353 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
354 638f9024 2019-05-13 stsp : got_error_from_errno2("open", path_lock));
358 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
362 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
363 6d9d28c3 2018-03-11 stsp if (errstr) {
364 a2e6d162 2019-07-27 stsp err = got_error_msg(GOT_ERR_WORKTREE_META,
365 a2e6d162 2019-07-27 stsp "could not parse work tree format version number");
368 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
369 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
373 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
374 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
375 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
378 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
380 7d61d891 2020-07-23 stsp (*worktree)->root_path = realpath(path, NULL);
381 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
382 7d61d891 2020-07-23 stsp err = got_error_from_errno2("realpath", path);
385 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
386 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
390 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
391 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
395 eaccb85f 2018-12-25 stsp err = read_meta_file(&base_commit_id_str, path_got,
396 0f92850e 2018-12-25 stsp GOT_WORKTREE_BASE_COMMIT);
400 c442a90d 2019-03-10 stsp err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
403 c442a90d 2019-03-10 stsp uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
404 c442a90d 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
405 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_from_string");
409 c9956ddf 2019-09-08 stsp err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
413 eaccb85f 2018-12-25 stsp err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
414 eaccb85f 2018-12-25 stsp base_commit_id_str);
418 36a38700 2019-05-10 stsp err = read_meta_file(&(*worktree)->head_ref_name, path_got,
419 36a38700 2019-05-10 stsp GOT_WORKTREE_HEAD_REF);
423 50b0790e 2020-09-11 stsp if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s",
424 50b0790e 2020-09-11 stsp (*worktree)->root_path,
425 50b0790e 2020-09-11 stsp GOT_WORKTREE_GOT_DIR, GOT_GOTCONFIG_FILENAME) == -1) {
426 50b0790e 2020-09-11 stsp err = got_error_from_errno("asprintf");
430 50b0790e 2020-09-11 stsp err = got_gotconfig_read(&(*worktree)->gotconfig,
431 50b0790e 2020-09-11 stsp (*worktree)->gotconfig_path);
434 eaccb85f 2018-12-25 stsp got_repo_close(repo);
435 7ac97322 2018-03-11 stsp free(path_got);
436 056e7441 2018-03-11 stsp free(path_lock);
437 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
438 c442a90d 2019-03-10 stsp free(uuidstr);
439 bd165944 2019-03-10 stsp free(formatstr);
441 6d9d28c3 2018-03-11 stsp if (fd != -1)
443 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
444 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
445 6d9d28c3 2018-03-11 stsp *worktree = NULL;
447 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
449 6d9d28c3 2018-03-11 stsp return err;
452 247140b2 2019-02-05 stsp const struct got_error *
453 247140b2 2019-02-05 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
455 247140b2 2019-02-05 stsp const struct got_error *err = NULL;
456 aedea96d 2020-10-20 stsp char *worktree_path;
458 aedea96d 2020-10-20 stsp worktree_path = strdup(path);
459 aedea96d 2020-10-20 stsp if (worktree_path == NULL)
460 aedea96d 2020-10-20 stsp return got_error_from_errno("strdup");
463 aedea96d 2020-10-20 stsp char *parent_path;
465 aedea96d 2020-10-20 stsp err = open_worktree(worktree, worktree_path);
466 aedea96d 2020-10-20 stsp if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
467 aedea96d 2020-10-20 stsp free(worktree_path);
468 247140b2 2019-02-05 stsp return err;
470 aedea96d 2020-10-20 stsp if (*worktree) {
471 aedea96d 2020-10-20 stsp free(worktree_path);
472 aedea96d 2020-10-20 stsp return NULL;
474 aedea96d 2020-10-20 stsp if (worktree_path[0] == '/' && worktree_path[1] == '\0')
476 aedea96d 2020-10-20 stsp err = got_path_dirname(&parent_path, worktree_path);
478 aedea96d 2020-10-20 stsp if (err->code != GOT_ERR_BAD_PATH) {
479 aedea96d 2020-10-20 stsp free(worktree_path);
480 aedea96d 2020-10-20 stsp return err;
484 aedea96d 2020-10-20 stsp free(worktree_path);
485 aedea96d 2020-10-20 stsp worktree_path = parent_path;
488 aedea96d 2020-10-20 stsp free(worktree_path);
489 247140b2 2019-02-05 stsp return got_error(GOT_ERR_NOT_WORKTREE);
492 3a6ce05a 2019-02-11 stsp const struct got_error *
493 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
495 3a6ce05a 2019-02-11 stsp const struct got_error *err = NULL;
496 cde76477 2018-03-11 stsp free(worktree->repo_path);
497 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
498 eaccb85f 2018-12-25 stsp free(worktree->base_commit_id);
499 36a38700 2019-05-10 stsp free(worktree->head_ref_name);
500 056e7441 2018-03-11 stsp if (worktree->lockfd != -1)
501 3a6ce05a 2019-02-11 stsp if (close(worktree->lockfd) != 0)
502 638f9024 2019-05-13 stsp err = got_error_from_errno2("close",
503 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree));
504 7f11502c 2019-08-28 hiltjo free(worktree->root_path);
505 50b0790e 2020-09-11 stsp free(worktree->gotconfig_path);
506 50b0790e 2020-09-11 stsp got_gotconfig_free(worktree->gotconfig);
507 6d9d28c3 2018-03-11 stsp free(worktree);
508 3a6ce05a 2019-02-11 stsp return err;
511 2fbdb5ae 2018-12-29 stsp const char *
512 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
514 c7f4312f 2019-02-05 stsp return worktree->root_path;
517 c7f4312f 2019-02-05 stsp const char *
518 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
520 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
522 49520a32 2018-12-29 stsp const char *
523 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
525 f609be2e 2018-12-29 stsp return worktree->path_prefix;
528 e5dc7198 2018-12-29 stsp const struct got_error *
529 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
530 e5dc7198 2018-12-29 stsp const char *path_prefix)
532 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
534 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
535 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
536 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
538 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
539 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
540 e5dc7198 2018-12-29 stsp free(absprefix);
541 e5dc7198 2018-12-29 stsp return NULL;
544 bc70eb79 2019-05-09 stsp const char *
545 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
547 36a38700 2019-05-10 stsp return worktree->head_ref_name;
550 024e9686 2019-05-14 stsp const struct got_error *
551 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
552 024e9686 2019-05-14 stsp struct got_reference *head_ref)
554 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
555 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
557 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
558 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
559 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
560 024e9686 2019-05-14 stsp path_got = NULL;
564 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
565 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
566 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
570 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
574 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
575 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
577 024e9686 2019-05-14 stsp free(path_got);
579 024e9686 2019-05-14 stsp free(head_ref_name);
580 024e9686 2019-05-14 stsp return err;
583 b72f483a 2019-02-05 stsp struct got_object_id *
584 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
586 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
589 507dc3bb 2018-12-29 stsp const struct got_error *
590 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
591 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
593 507dc3bb 2018-12-29 stsp const struct got_error *err;
594 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
595 507dc3bb 2018-12-29 stsp char *id_str = NULL;
596 507dc3bb 2018-12-29 stsp char *path_got = NULL;
598 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
599 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
600 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
601 507dc3bb 2018-12-29 stsp path_got = NULL;
605 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
607 507dc3bb 2018-12-29 stsp return err;
609 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
610 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
614 507dc3bb 2018-12-29 stsp /* Record our base commit. */
615 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
618 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
622 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
623 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
624 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
625 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
630 507dc3bb 2018-12-29 stsp got_object_close(obj);
631 507dc3bb 2018-12-29 stsp free(id_str);
632 507dc3bb 2018-12-29 stsp free(path_got);
633 507dc3bb 2018-12-29 stsp return err;
636 50b0790e 2020-09-11 stsp const struct got_gotconfig *
637 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
639 50b0790e 2020-09-11 stsp return worktree->gotconfig;
642 9d31a1d8 2018-03-11 stsp static const struct got_error *
643 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
645 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
646 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
647 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
648 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
649 86c3caaf 2018-03-09 stsp return NULL;
652 21908da4 2019-01-13 stsp static const struct got_error *
653 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
655 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
656 4a1ddfc2 2019-01-12 stsp char *abspath;
658 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
659 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
661 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
662 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
663 ddcd8544 2019-03-11 stsp struct stat sb;
664 ddcd8544 2019-03-11 stsp err = NULL;
665 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
666 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
667 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
668 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
669 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
672 4a1ddfc2 2019-01-12 stsp free(abspath);
673 68c76935 2019-02-19 stsp return err;
676 68c76935 2019-02-19 stsp static const struct got_error *
677 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
679 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
680 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
681 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
682 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
687 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
688 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
689 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
692 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
693 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
694 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
697 68c76935 2019-02-19 stsp if (flen1 == 0) {
698 68c76935 2019-02-19 stsp if (flen2 != 0)
701 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
702 68c76935 2019-02-19 stsp if (flen1 != 0)
705 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
706 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
716 68c76935 2019-02-19 stsp return err;
719 68c76935 2019-02-19 stsp static const struct got_error *
720 68c76935 2019-02-19 stsp check_files_equal(int *same, const char *f1_path, const char *f2_path)
722 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
723 68c76935 2019-02-19 stsp struct stat sb;
724 68c76935 2019-02-19 stsp size_t size1, size2;
725 68c76935 2019-02-19 stsp FILE *f1 = NULL, *f2 = NULL;
729 68c76935 2019-02-19 stsp if (lstat(f1_path, &sb) != 0) {
730 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f1_path);
733 68c76935 2019-02-19 stsp size1 = sb.st_size;
735 68c76935 2019-02-19 stsp if (lstat(f2_path, &sb) != 0) {
736 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f2_path);
739 68c76935 2019-02-19 stsp size2 = sb.st_size;
741 68c76935 2019-02-19 stsp if (size1 != size2) {
743 68c76935 2019-02-19 stsp return NULL;
746 68c76935 2019-02-19 stsp f1 = fopen(f1_path, "r");
747 68c76935 2019-02-19 stsp if (f1 == NULL)
748 60522982 2019-12-15 stsp return got_error_from_errno2("fopen", f1_path);
750 68c76935 2019-02-19 stsp f2 = fopen(f2_path, "r");
751 68c76935 2019-02-19 stsp if (f2 == NULL) {
752 60522982 2019-12-15 stsp err = got_error_from_errno2("fopen", f2_path);
756 68c76935 2019-02-19 stsp err = check_file_contents_equal(same, f1, f2);
758 68c76935 2019-02-19 stsp if (f1 && fclose(f1) != 0 && err == NULL)
759 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
760 68c76935 2019-02-19 stsp if (f2 && fclose(f2) != 0 && err == NULL)
761 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
763 6353ad76 2019-02-08 stsp return err;
767 46b6ee73 2019-06-04 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
768 14c901f1 2019-08-08 stsp * the file at deriv_path acts as the first derived version, and the
769 14c901f1 2019-08-08 stsp * file on disk acts as the second derived version.
771 6353ad76 2019-02-08 stsp static const struct got_error *
772 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
773 46b6ee73 2019-06-04 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
774 14c901f1 2019-08-08 stsp const char *path, uint16_t st_mode, const char *deriv_path,
775 f69721c3 2019-10-21 stsp const char *label_orig, const char *label_deriv,
776 f69721c3 2019-10-21 stsp struct got_repository *repo,
777 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
779 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
780 6353ad76 2019-02-08 stsp int merged_fd = -1;
781 14c901f1 2019-08-08 stsp FILE *f_orig = NULL;
782 14c901f1 2019-08-08 stsp char *blob_orig_path = NULL;
783 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
784 234035bc 2019-06-01 stsp int overlapcnt = 0;
785 3524bbf9 2020-10-20 stsp char *parent = NULL;
786 3b9f0f87 2020-07-23 stsp char *symlink_path = NULL;
787 3b9f0f87 2020-07-23 stsp FILE *symlinkf = NULL;
789 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
791 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
793 3524bbf9 2020-10-20 stsp return err;
795 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
796 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
800 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
804 af54ae4a 2019-02-19 stsp free(base_path);
805 46b6ee73 2019-06-04 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
806 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
807 af54ae4a 2019-02-19 stsp base_path = NULL;
811 46b6ee73 2019-06-04 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
814 46b6ee73 2019-06-04 stsp if (blob_orig) {
815 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
816 46b6ee73 2019-06-04 stsp blob_orig);
821 1430b4e0 2019-03-27 stsp * If the file has no blob, this is an "add vs add" conflict,
822 1430b4e0 2019-03-27 stsp * and we simply use an empty ancestor file to make both files
823 1430b4e0 2019-03-27 stsp * appear in the merged result in their entirety.
828 3b9f0f87 2020-07-23 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
829 3b9f0f87 2020-07-23 stsp * target path into a temporary file and use that file with diff3.
831 3b9f0f87 2020-07-23 stsp if (S_ISLNK(st_mode)) {
832 3b9f0f87 2020-07-23 stsp char target_path[PATH_MAX];
833 3b9f0f87 2020-07-23 stsp ssize_t target_len;
836 3b9f0f87 2020-07-23 stsp free(base_path);
837 3b9f0f87 2020-07-23 stsp if (asprintf(&base_path, "%s/got-symlink-merge",
838 3b9f0f87 2020-07-23 stsp parent) == -1) {
839 3b9f0f87 2020-07-23 stsp err = got_error_from_errno("asprintf");
840 3b9f0f87 2020-07-23 stsp base_path = NULL;
843 3b9f0f87 2020-07-23 stsp err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
846 3b9f0f87 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
847 3b9f0f87 2020-07-23 stsp sizeof(target_path));
848 3b9f0f87 2020-07-23 stsp if (target_len == -1) {
849 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
852 3b9f0f87 2020-07-23 stsp n = fwrite(target_path, 1, target_len, symlinkf);
853 3b9f0f87 2020-07-23 stsp if (n != target_len) {
854 3b9f0f87 2020-07-23 stsp err = got_ferror(symlinkf, GOT_ERR_IO);
857 3b9f0f87 2020-07-23 stsp if (fflush(symlinkf) == EOF) {
858 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("fflush", symlink_path);
863 14c901f1 2019-08-08 stsp err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
864 3b9f0f87 2020-07-23 stsp blob_orig_path, symlink_path ? symlink_path : ondisk_path,
865 3b9f0f87 2020-07-23 stsp label_deriv, label_orig, NULL);
869 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
870 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
874 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
875 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
879 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
880 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
881 14c901f1 2019-08-08 stsp err = check_files_equal(local_changes_subsumed, deriv_path,
882 68c76935 2019-02-19 stsp merged_path);
887 2ad902c0 2019-12-15 stsp if (fchmod(merged_fd, st_mode) != 0) {
888 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
892 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
893 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
894 230a42bd 2019-05-11 jcs ondisk_path);
899 fdcb7daf 2019-12-15 stsp if (merged_path)
900 fdcb7daf 2019-12-15 stsp unlink(merged_path);
902 3b9f0f87 2020-07-23 stsp if (symlink_path) {
903 3b9f0f87 2020-07-23 stsp if (unlink(symlink_path) == -1 && err == NULL)
904 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("unlink", symlink_path);
906 3b9f0f87 2020-07-23 stsp if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
907 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("fclose", symlink_path);
908 3b9f0f87 2020-07-23 stsp free(symlink_path);
909 3a6ce05a 2019-02-11 stsp if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
910 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
911 46b6ee73 2019-06-04 stsp if (f_orig && fclose(f_orig) != 0 && err == NULL)
912 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
913 6353ad76 2019-02-08 stsp free(merged_path);
914 af54ae4a 2019-02-19 stsp free(base_path);
915 46b6ee73 2019-06-04 stsp if (blob_orig_path) {
916 46b6ee73 2019-06-04 stsp unlink(blob_orig_path);
917 46b6ee73 2019-06-04 stsp free(blob_orig_path);
919 3524bbf9 2020-10-20 stsp free(parent);
920 af57b12a 2020-07-23 stsp return err;
923 af57b12a 2020-07-23 stsp static const struct got_error *
924 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
925 af57b12a 2020-07-23 stsp size_t target_len)
927 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
928 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
929 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
930 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
931 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
932 af57b12a 2020-07-23 stsp ondisk_path);
933 af57b12a 2020-07-23 stsp return NULL;
937 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
938 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
939 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
940 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
942 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
943 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
945 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
946 993e2a1b 2020-07-23 stsp * has deleted this symlink.
948 11cc08c1 2020-07-23 stsp static const struct got_error *
949 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
950 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
951 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
953 11cc08c1 2020-07-23 stsp const struct got_error *err;
954 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
955 11cc08c1 2020-07-23 stsp FILE *f = NULL;
957 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
959 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
961 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
962 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
963 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
967 11cc08c1 2020-07-23 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict");
969 3818e3c4 2020-11-01 naddy goto done;
971 3818e3c4 2020-11-01 naddy if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
972 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
976 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
977 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
978 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
979 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
980 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
981 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
982 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
983 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
984 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
985 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
989 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
990 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
993 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
994 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
998 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
999 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
1000 11cc08c1 2020-07-23 stsp free(path);
1001 11cc08c1 2020-07-23 stsp free(id_str);
1002 11cc08c1 2020-07-23 stsp free(label_deriv);
1003 11cc08c1 2020-07-23 stsp return err;
1006 d219f183 2020-07-23 stsp /* forward declaration */
1007 d219f183 2020-07-23 stsp static const struct got_error *
1008 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
1009 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
1010 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
1011 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
1014 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
1015 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
1016 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
1017 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
1019 af57b12a 2020-07-23 stsp static const struct got_error *
1020 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
1021 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
1022 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
1023 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1024 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1026 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
1027 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
1028 af57b12a 2020-07-23 stsp struct stat sb;
1029 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
1030 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
1031 11cc08c1 2020-07-23 stsp int have_local_change = 0;
1032 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
1034 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
1035 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
1037 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
1038 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
1039 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
1040 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
1041 af57b12a 2020-07-23 stsp ondisk_path);
1042 af57b12a 2020-07-23 stsp goto done;
1044 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
1046 526a746f 2020-07-23 stsp if (blob_orig) {
1047 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1049 526a746f 2020-07-23 stsp goto done;
1052 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
1053 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
1054 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1055 11cc08c1 2020-07-23 stsp have_local_change = 1;
1057 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
1058 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
1059 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
1060 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1061 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
1063 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
1064 11cc08c1 2020-07-23 stsp if (ancestor_target) {
1065 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
1066 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1068 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
1069 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1070 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
1071 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1074 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
1075 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1076 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
1077 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
1079 11cc08c1 2020-07-23 stsp goto done;
1080 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1083 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
1084 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
1085 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
1086 af57b12a 2020-07-23 stsp strlen(deriv_target));
1088 af57b12a 2020-07-23 stsp goto done;
1089 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1090 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
1091 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
1092 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1093 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
1094 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1097 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1098 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
1099 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
1101 ea7786be 2020-07-23 stsp goto done;
1102 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1108 af57b12a 2020-07-23 stsp free(ancestor_target);
1109 14c901f1 2019-08-08 stsp return err;
1113 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
1114 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
1115 14c901f1 2019-08-08 stsp * acts as the second derived version.
1117 14c901f1 2019-08-08 stsp static const struct got_error *
1118 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1119 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
1120 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
1121 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
1122 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1123 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1125 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
1126 14c901f1 2019-08-08 stsp FILE *f_deriv = NULL;
1127 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1128 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
1130 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
1132 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1134 ed6b5030 2020-10-20 stsp return err;
1136 14c901f1 2019-08-08 stsp free(base_path);
1137 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1138 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1139 14c901f1 2019-08-08 stsp base_path = NULL;
1140 14c901f1 2019-08-08 stsp goto done;
1143 14c901f1 2019-08-08 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1145 14c901f1 2019-08-08 stsp goto done;
1146 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1147 14c901f1 2019-08-08 stsp blob_deriv);
1149 14c901f1 2019-08-08 stsp goto done;
1151 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1153 14c901f1 2019-08-08 stsp goto done;
1154 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1155 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1156 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1157 14c901f1 2019-08-08 stsp goto done;
1160 14c901f1 2019-08-08 stsp err = merge_file(local_changes_subsumed, worktree, blob_orig,
1161 f69721c3 2019-10-21 stsp ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1162 f69721c3 2019-10-21 stsp label_deriv, repo, progress_cb, progress_arg);
1164 14c901f1 2019-08-08 stsp if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1165 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1166 14c901f1 2019-08-08 stsp free(base_path);
1167 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1168 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1169 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1171 6353ad76 2019-02-08 stsp free(id_str);
1172 818c7501 2019-07-11 stsp free(label_deriv);
1173 ed6b5030 2020-10-20 stsp free(parent);
1174 4a1ddfc2 2019-01-12 stsp return err;
1177 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1178 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1179 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1180 65b05cec 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_object_id *blob_id)
1182 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1183 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1185 65b05cec 2020-07-23 stsp *new_iep = NULL;
1187 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1189 054041d0 2020-06-24 stsp return err;
1191 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(new_ie, ondisk_path,
1192 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1194 054041d0 2020-06-24 stsp goto done;
1196 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1199 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1201 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1202 13d9040b 2019-03-27 stsp return err;
1205 1ebedb77 2019-10-19 stsp static mode_t
1206 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1208 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1210 1ebedb77 2019-10-19 stsp if (executable) {
1211 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1212 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1213 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1214 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1215 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1216 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1219 1ebedb77 2019-10-19 stsp return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1222 8ba819a3 2020-07-23 stsp /* forward declaration */
1223 13d9040b 2019-03-27 stsp static const struct got_error *
1224 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1225 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1226 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1227 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1228 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1229 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1232 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1233 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1235 8ba819a3 2020-07-23 stsp static const struct got_error *
1236 5a1fbc73 2020-07-23 stsp replace_existing_symlink(const char *ondisk_path, const char *target_path,
1237 5a1fbc73 2020-07-23 stsp size_t target_len)
1239 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1240 5a1fbc73 2020-07-23 stsp ssize_t elen;
1241 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1245 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1246 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1247 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1248 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1249 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1250 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1252 5a1fbc73 2020-07-23 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1253 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1254 5a1fbc73 2020-07-23 stsp if (errno != ELOOP)
1255 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1257 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1258 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1259 5a1fbc73 2020-07-23 stsp if (elen == -1)
1260 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1262 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1263 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1264 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1267 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1268 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1269 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1270 5a1fbc73 2020-07-23 stsp return err;
1273 3c1ec782 2020-07-23 stsp static const struct got_error *
1274 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1275 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1277 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1278 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1279 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1281 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1283 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1284 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1285 3c1ec782 2020-07-23 stsp return NULL;
1289 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1290 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1291 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1292 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1293 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1295 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1296 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1297 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1299 ce031e9e 2020-10-20 stsp return err;
1300 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1301 ce031e9e 2020-10-20 stsp free(parent);
1302 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1304 ce031e9e 2020-10-20 stsp free(parent);
1305 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1306 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1307 3c1ec782 2020-07-23 stsp free(abspath);
1308 3c1ec782 2020-07-23 stsp return err;
1310 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1311 3c1ec782 2020-07-23 stsp free(abspath);
1313 3c1ec782 2020-07-23 stsp return err;
1315 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1317 3c1ec782 2020-07-23 stsp return err;
1320 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1321 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1322 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1323 3c1ec782 2020-07-23 stsp return NULL;
1326 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1327 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1328 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1329 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1330 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1331 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1333 3c1ec782 2020-07-23 stsp free(path_got);
1334 3c1ec782 2020-07-23 stsp return NULL;
1337 5a1fbc73 2020-07-23 stsp static const struct got_error *
1338 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1339 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1340 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1341 c90c8ce3 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1342 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1344 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1345 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1346 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1347 906c123b 2020-07-23 stsp char *path_got = NULL;
1348 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1349 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1351 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1354 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1355 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1356 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1357 8ba819a3 2020-07-23 stsp * in the blob object.
1360 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1361 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1362 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1363 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1364 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1365 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1366 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1367 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1368 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1369 3b9f0f87 2020-07-23 stsp progress_arg);
1371 8ba819a3 2020-07-23 stsp if (len > 0) {
1372 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1373 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1374 8ba819a3 2020-07-23 stsp len - hdrlen);
1375 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1376 8ba819a3 2020-07-23 stsp hdrlen = 0;
1378 8ba819a3 2020-07-23 stsp } while (len != 0);
1379 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1381 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1382 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1384 3c1ec782 2020-07-23 stsp return err;
1386 3c1ec782 2020-07-23 stsp if (*is_bad_symlink) {
1387 906c123b 2020-07-23 stsp /* install as a regular file */
1388 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1389 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1390 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1391 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1392 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1393 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1394 906c123b 2020-07-23 stsp goto done;
1397 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1398 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1399 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1400 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1401 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1402 c90c8ce3 2020-07-23 stsp goto done;
1404 5a1fbc73 2020-07-23 stsp err = replace_existing_symlink(ondisk_path,
1405 5a1fbc73 2020-07-23 stsp target_path, target_len);
1407 f35fa46a 2020-07-23 stsp goto done;
1408 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1409 5a1fbc73 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1410 6e1eade5 2020-07-23 stsp reverting_versioned_file ?
1411 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1414 5a1fbc73 2020-07-23 stsp goto done; /* Nothing else to do. */
1417 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1418 f4994adc 2020-10-20 stsp char *parent;
1419 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1421 f4994adc 2020-10-20 stsp goto done;
1422 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1423 f4994adc 2020-10-20 stsp free(parent);
1425 f35fa46a 2020-07-23 stsp goto done;
1427 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1428 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1430 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1431 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1432 f35fa46a 2020-07-23 stsp goto done;
1436 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1437 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1438 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1439 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1440 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1441 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1442 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1443 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1444 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1445 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1446 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1447 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1448 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1450 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1451 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1453 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1454 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1455 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1457 906c123b 2020-07-23 stsp free(path_got);
1458 8ba819a3 2020-07-23 stsp return err;
1461 8ba819a3 2020-07-23 stsp static const struct got_error *
1462 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1463 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1464 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1465 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1466 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1467 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1469 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1470 507dc3bb 2018-12-29 stsp int fd = -1;
1471 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1472 507dc3bb 2018-12-29 stsp int update = 0;
1473 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1475 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1476 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
1477 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1478 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1479 f5375317 2020-10-20 stsp char *parent;
1480 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1482 f5375317 2020-10-20 stsp return err;
1483 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1484 f5375317 2020-10-20 stsp free(parent);
1486 21908da4 2019-01-13 stsp return err;
1487 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1488 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1489 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
1490 21908da4 2019-01-13 stsp if (fd == -1)
1491 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1492 230a42bd 2019-05-11 jcs ondisk_path);
1493 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1494 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1495 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1496 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1497 3b9f0f87 2020-07-23 stsp goto done;
1499 bd6aa359 2020-07-23 stsp if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1500 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1501 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1502 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1503 507dc3bb 2018-12-29 stsp goto done;
1505 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1506 507dc3bb 2018-12-29 stsp ondisk_path);
1508 507dc3bb 2018-12-29 stsp goto done;
1509 507dc3bb 2018-12-29 stsp update = 1;
1512 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1515 3818e3c4 2020-11-01 naddy if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1516 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod",
1517 3818e3c4 2020-11-01 naddy update ? tmppath : ondisk_path);
1518 3818e3c4 2020-11-01 naddy goto done;
1521 bd6aa359 2020-07-23 stsp if (progress_cb) {
1522 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1523 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1525 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1526 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1529 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1530 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1532 bd6aa359 2020-07-23 stsp goto done;
1535 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1537 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1538 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1541 9d31a1d8 2018-03-11 stsp if (len > 0) {
1542 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1543 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1544 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1545 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1546 b87c6f83 2018-12-24 stsp goto done;
1547 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1548 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1549 b87c6f83 2018-12-24 stsp goto done;
1551 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1553 9d31a1d8 2018-03-11 stsp } while (len != 0);
1555 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1556 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1557 816dc654 2019-02-16 stsp goto done;
1560 507dc3bb 2018-12-29 stsp if (update) {
1561 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1562 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1563 230a42bd 2019-05-11 jcs ondisk_path);
1564 2a57020b 2019-02-20 stsp unlink(tmppath);
1565 68ed9ba5 2019-02-10 stsp goto done;
1570 3a6ce05a 2019-02-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
1571 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1572 507dc3bb 2018-12-29 stsp free(tmppath);
1573 6353ad76 2019-02-08 stsp return err;
1576 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1577 6353ad76 2019-02-08 stsp static const struct got_error *
1578 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1580 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1581 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1582 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1583 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1584 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1586 7154f6ce 2019-03-27 stsp int i = 0;
1587 7154f6ce 2019-03-27 stsp char *line;
1588 7154f6ce 2019-03-27 stsp size_t len;
1589 7154f6ce 2019-03-27 stsp const char delim[3] = {'\0', '\0', '\0'};
1591 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1592 7154f6ce 2019-03-27 stsp line = fparseln(f, &len, NULL, delim, 0);
1593 7154f6ce 2019-03-27 stsp if (line == NULL) {
1594 7154f6ce 2019-03-27 stsp if (feof(f))
1596 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1600 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1601 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1603 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1609 7154f6ce 2019-03-27 stsp return err;
1612 e2b1e152 2019-07-13 stsp static int
1613 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1615 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1616 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1619 1ebedb77 2019-10-19 stsp static int
1620 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1622 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1623 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1624 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1625 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1626 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1627 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1630 c363b2c1 2019-08-03 stsp static unsigned char
1631 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1633 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1634 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1635 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1636 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1637 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1638 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1639 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1641 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1645 a919d5c4 2020-07-23 stsp static const struct got_error *
1646 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1647 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1648 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1650 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1651 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1652 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1653 a919d5c4 2020-07-23 stsp ssize_t elen;
1654 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1655 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1656 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1658 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1660 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1662 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1664 a919d5c4 2020-07-23 stsp return err;
1665 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1667 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1668 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1670 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1672 a919d5c4 2020-07-23 stsp if (len > 0) {
1673 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1674 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1675 a919d5c4 2020-07-23 stsp len - hdrlen);
1676 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1677 a919d5c4 2020-07-23 stsp hdrlen = 0;
1679 a919d5c4 2020-07-23 stsp } while (len != 0);
1680 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1682 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1683 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1684 a919d5c4 2020-07-23 stsp if (elen == -1)
1685 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1687 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1688 a919d5c4 2020-07-23 stsp if (elen == -1)
1689 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1692 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1693 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1695 a919d5c4 2020-07-23 stsp return NULL;
1698 7154f6ce 2019-03-27 stsp static const struct got_error *
1699 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1700 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1701 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1703 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1704 6353ad76 2019-02-08 stsp struct got_object_id id;
1705 6353ad76 2019-02-08 stsp size_t hdrlen;
1706 1338848f 2019-12-13 stsp int fd = -1;
1707 6353ad76 2019-02-08 stsp FILE *f = NULL;
1708 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1709 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1710 6353ad76 2019-02-08 stsp size_t flen, blen;
1711 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1713 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1716 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1717 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1718 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1720 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1721 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1722 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1723 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1724 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1726 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1727 882ef1b9 2019-12-13 stsp goto done;
1729 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1730 3d35a492 2019-12-13 stsp goto done;
1733 7f91a133 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1734 a919d5c4 2020-07-23 stsp if (fd == -1 && errno != ENOENT && errno != ELOOP)
1735 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1736 a919d5c4 2020-07-23 stsp else if (fd == -1 && errno == ELOOP) {
1737 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1738 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1739 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1740 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1741 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1742 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1744 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1745 3d35a492 2019-12-13 stsp goto done;
1747 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1748 1338848f 2019-12-13 stsp goto done;
1752 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1753 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1754 1338848f 2019-12-13 stsp goto done;
1757 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1758 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1759 1338848f 2019-12-13 stsp goto done;
1760 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1761 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1762 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1763 1338848f 2019-12-13 stsp goto done;
1766 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1767 f179e66d 2020-07-23 stsp goto done;
1769 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1770 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1771 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1772 1338848f 2019-12-13 stsp goto done;
1775 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1776 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1777 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1779 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1781 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1783 1338848f 2019-12-13 stsp goto done;
1785 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1786 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1787 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1788 a919d5c4 2020-07-23 stsp goto done;
1791 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1792 3d35a492 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1793 ab0d4361 2019-12-13 stsp if (fd == -1) {
1794 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1795 ab0d4361 2019-12-13 stsp goto done;
1799 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1800 6353ad76 2019-02-08 stsp if (f == NULL) {
1801 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1802 6353ad76 2019-02-08 stsp goto done;
1805 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1807 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1808 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1810 7154f6ce 2019-03-27 stsp goto done;
1811 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1812 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1813 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1814 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1815 7154f6ce 2019-03-27 stsp goto done;
1817 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1818 6353ad76 2019-02-08 stsp if (flen != 0)
1819 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1821 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1822 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1823 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1825 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1826 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1827 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1828 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1832 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1835 6353ad76 2019-02-08 stsp hdrlen = 0;
1838 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1839 7154f6ce 2019-03-27 stsp rewind(f);
1840 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1841 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1842 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1845 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1846 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1847 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1848 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1849 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1850 9d31a1d8 2018-03-11 stsp return err;
1854 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1855 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1857 e2b1e152 2019-07-13 stsp static const struct got_error *
1858 e2b1e152 2019-07-13 stsp sync_timestamps(char *ondisk_path, unsigned char status,
1859 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1861 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1862 e2b1e152 2019-07-13 stsp return got_fileindex_entry_update(ie, ondisk_path,
1863 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1865 e2b1e152 2019-07-13 stsp return NULL;
1868 9d31a1d8 2018-03-11 stsp static const struct got_error *
1869 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1870 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1871 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1872 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1873 0584f854 2019-04-06 stsp void *progress_arg)
1875 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1876 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1877 6353ad76 2019-02-08 stsp char *ondisk_path;
1878 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1879 b8f41171 2019-02-10 stsp struct stat sb;
1881 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1882 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1885 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1886 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1887 a76c42e6 2019-08-03 stsp goto done;
1889 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1892 abb4604f 2019-07-27 stsp goto done;
1893 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1894 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1896 abb4604f 2019-07-27 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1897 c90c8ce3 2020-07-23 stsp status = GOT_STATUS_UNVERSIONED;
1900 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1901 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1902 b8f41171 2019-02-10 stsp goto done;
1904 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1905 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1907 5036ab18 2020-04-18 stsp goto done;
1910 523b8417 2019-10-19 stsp if (ie && status != GOT_STATUS_MISSING &&
1911 523b8417 2019-10-19 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1912 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1913 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1914 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1915 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1917 e2b1e152 2019-07-13 stsp goto done;
1918 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1920 6353ad76 2019-02-08 stsp goto done;
1922 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1923 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1924 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1925 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1926 b8f41171 2019-02-10 stsp goto done;
1930 56e0773d 2019-11-28 stsp err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1932 6353ad76 2019-02-08 stsp goto done;
1934 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1935 234035bc 2019-06-01 stsp int update_timestamps;
1936 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1937 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1938 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1939 234035bc 2019-06-01 stsp struct got_object_id id2;
1940 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1941 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1943 f69721c3 2019-10-21 stsp goto done;
1945 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
1946 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
1947 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1948 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
1949 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
1950 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
1951 f69721c3 2019-10-21 stsp goto done;
1953 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
1954 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
1955 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
1956 234035bc 2019-06-01 stsp goto done;
1959 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1960 36bf999c 2020-07-23 stsp char *link_target;
1961 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
1963 36bf999c 2020-07-23 stsp goto done;
1964 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
1965 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
1966 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
1967 36bf999c 2020-07-23 stsp free(link_target);
1969 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1970 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
1971 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
1972 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
1974 f69721c3 2019-10-21 stsp free(label_orig);
1975 234035bc 2019-06-01 stsp if (blob2)
1976 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1978 909d120e 2019-09-22 stsp goto done;
1980 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1981 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1982 234035bc 2019-06-01 stsp * unmodified files again.
1984 234035bc 2019-06-01 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1985 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1986 234035bc 2019-06-01 stsp update_timestamps);
1987 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
1988 1ebedb77 2019-10-19 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1989 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1990 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1991 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1993 1ee397ad 2019-07-12 stsp goto done;
1994 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1995 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1997 13d9040b 2019-03-27 stsp goto done;
1999 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2000 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2001 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2002 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2003 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2004 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2005 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2007 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2008 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2009 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2010 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2011 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2014 13d9040b 2019-03-27 stsp goto done;
2017 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie, ondisk_path,
2018 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 1);
2020 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2021 054041d0 2020-06-24 stsp worktree->base_commit_id, ondisk_path, path,
2022 054041d0 2020-06-24 stsp &blob->id);
2025 13d9040b 2019-03-27 stsp goto done;
2027 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2028 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2029 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2032 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2034 6353ad76 2019-02-08 stsp free(ondisk_path);
2035 8da9e5f4 2019-01-12 stsp return err;
2038 90285c3b 2019-01-08 stsp static const struct got_error *
2039 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2041 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2042 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
2044 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2045 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2047 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2048 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2049 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2051 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2053 fddefe3b 2020-10-20 stsp char *parent;
2054 fddefe3b 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
2057 fddefe3b 2020-10-20 stsp free(ondisk_path);
2058 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2059 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2060 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2061 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2062 fddefe3b 2020-10-20 stsp ondisk_path);
2065 fddefe3b 2020-10-20 stsp } while (got_path_cmp(ondisk_path, root_path,
2066 fddefe3b 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
2068 90285c3b 2019-01-08 stsp free(ondisk_path);
2069 90285c3b 2019-01-08 stsp return err;
2072 708d8e67 2019-03-27 stsp static const struct got_error *
2073 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2074 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2075 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2077 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2078 708d8e67 2019-03-27 stsp unsigned char status;
2079 708d8e67 2019-03-27 stsp struct stat sb;
2080 708d8e67 2019-03-27 stsp char *ondisk_path;
2082 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2083 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2085 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2087 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2089 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2091 5a58a424 2020-06-23 stsp goto done;
2093 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2094 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2095 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2096 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2097 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2098 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2099 993e2a1b 2020-07-23 stsp goto done;
2101 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2102 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2103 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2104 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2106 993e2a1b 2020-07-23 stsp goto done;
2107 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2108 993e2a1b 2020-07-23 stsp ie->path);
2109 993e2a1b 2020-07-23 stsp goto done;
2112 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2113 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2114 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2116 5a58a424 2020-06-23 stsp goto done;
2118 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2119 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2121 fc6346c4 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2124 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2126 5a58a424 2020-06-23 stsp goto done;
2127 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2128 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2130 5a58a424 2020-06-23 stsp goto done;
2132 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2135 5a58a424 2020-06-23 stsp free(ondisk_path);
2136 fc6346c4 2019-03-27 stsp return err;
2139 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2140 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2141 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2142 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2143 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2144 8da9e5f4 2019-01-12 stsp void *progress_arg;
2145 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2146 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2149 512f0d0e 2019-01-02 stsp static const struct got_error *
2150 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2151 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2153 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2155 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2156 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2158 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2159 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2162 8da9e5f4 2019-01-12 stsp static const struct got_error *
2163 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2165 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2167 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2168 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2170 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2171 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2174 9d31a1d8 2018-03-11 stsp static const struct got_error *
2175 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2177 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2178 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2179 8da9e5f4 2019-01-12 stsp char *path;
2181 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2182 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2184 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2185 63c5ca5d 2019-08-24 stsp return NULL;
2187 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2188 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2190 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2192 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2193 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2195 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2196 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2198 8da9e5f4 2019-01-12 stsp free(path);
2199 0cd1c46a 2019-03-11 stsp return err;
2202 b2118c49 2020-07-28 stsp const struct got_error *
2203 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2205 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2207 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2208 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2209 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2210 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2213 b2118c49 2020-07-28 stsp return NULL;
2216 818c7501 2019-07-11 stsp static const struct got_error *
2217 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2219 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2220 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2222 517bab73 2019-03-11 stsp *refname = NULL;
2224 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2226 b2118c49 2020-07-28 stsp return err;
2228 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2229 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2230 0647c563 2019-03-11 stsp *refname = NULL;
2232 517bab73 2019-03-11 stsp free(uuidstr);
2233 517bab73 2019-03-11 stsp return err;
2236 818c7501 2019-07-11 stsp const struct got_error *
2237 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2239 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2242 818c7501 2019-07-11 stsp static const struct got_error *
2243 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2245 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2246 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2249 818c7501 2019-07-11 stsp static const struct got_error *
2250 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2252 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2255 818c7501 2019-07-11 stsp static const struct got_error *
2256 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2258 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2259 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2262 818c7501 2019-07-11 stsp static const struct got_error *
2263 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2265 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2266 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2269 0ebf8283 2019-07-24 stsp static const struct got_error *
2270 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2272 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2273 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2276 0ebf8283 2019-07-24 stsp static const struct got_error *
2277 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2279 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2280 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2283 0ebf8283 2019-07-24 stsp static const struct got_error *
2284 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2286 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2287 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2290 0ebf8283 2019-07-24 stsp static const struct got_error *
2291 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2293 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2294 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2297 0ebf8283 2019-07-24 stsp const struct got_error *
2298 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2299 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2301 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2302 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2303 0ebf8283 2019-07-24 stsp *path = NULL;
2304 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2306 0ebf8283 2019-07-24 stsp return NULL;
2310 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2311 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2313 517bab73 2019-03-11 stsp static const struct got_error *
2314 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2316 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2317 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2318 517bab73 2019-03-11 stsp char *refname;
2320 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2322 517bab73 2019-03-11 stsp return err;
2324 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2326 0cd1c46a 2019-03-11 stsp goto done;
2328 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2330 0cd1c46a 2019-03-11 stsp free(refname);
2332 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2333 3e3a69f1 2019-07-25 stsp return err;
2336 3e3a69f1 2019-07-25 stsp static const struct got_error *
2337 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2339 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2341 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2342 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2343 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2344 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2346 9d31a1d8 2018-03-11 stsp return err;
2350 ebf99748 2019-05-09 stsp static const struct got_error *
2351 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2352 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2354 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2355 ebf99748 2019-05-09 stsp FILE *index = NULL;
2357 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2358 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2359 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2360 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2362 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2364 ebf99748 2019-05-09 stsp goto done;
2366 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
2367 ebf99748 2019-05-09 stsp if (index == NULL) {
2368 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2369 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2371 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2372 ebf99748 2019-05-09 stsp if (fclose(index) != 0 && err == NULL)
2373 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2376 ebf99748 2019-05-09 stsp if (err) {
2377 ebf99748 2019-05-09 stsp free(*fileindex_path);
2378 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2379 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2380 ebf99748 2019-05-09 stsp *fileindex = NULL;
2382 ebf99748 2019-05-09 stsp return err;
2385 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2386 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2387 c932eeeb 2019-05-22 stsp const char *path;
2388 c932eeeb 2019-05-22 stsp size_t path_len;
2389 c932eeeb 2019-05-22 stsp const char *entry_name;
2390 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2391 a484d721 2019-06-10 stsp void *progress_arg;
2394 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2395 c932eeeb 2019-05-22 stsp static const struct got_error *
2396 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2398 1ee397ad 2019-07-12 stsp const struct got_error *err;
2399 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2401 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2402 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2403 c932eeeb 2019-05-22 stsp return NULL;
2404 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2405 102ff934 2019-06-11 stsp return NULL;
2407 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2408 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2409 c932eeeb 2019-05-22 stsp return NULL;
2411 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2412 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2413 edd02c5e 2019-07-11 stsp ie->path);
2415 1ee397ad 2019-07-12 stsp return err;
2417 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2418 c932eeeb 2019-05-22 stsp return NULL;
2421 9c6338c4 2019-06-02 stsp static const struct got_error *
2422 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2424 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2425 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2426 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2427 867630bb 2020-01-17 stsp struct timespec timeout;
2429 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2430 9c6338c4 2019-06-02 stsp fileindex_path);
2432 9c6338c4 2019-06-02 stsp goto done;
2434 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2436 9c6338c4 2019-06-02 stsp goto done;
2438 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2439 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2440 9c6338c4 2019-06-02 stsp fileindex_path);
2441 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2445 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2446 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2447 867630bb 2020-01-17 stsp * was recorded in the file index.
2449 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2450 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2451 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2453 9c6338c4 2019-06-02 stsp if (new_index)
2454 9c6338c4 2019-06-02 stsp fclose(new_index);
2455 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2456 9c6338c4 2019-06-02 stsp return err;
2459 6ced4176 2019-07-12 stsp static const struct got_error *
2460 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2461 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2462 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
2464 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2465 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2466 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2467 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2469 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2470 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2471 6ced4176 2019-07-12 stsp *tree_id = NULL;
2473 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2474 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2475 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2476 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2477 6ced4176 2019-07-12 stsp if (*tree_relpath == 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 err = got_object_id_by_path(tree_id, repo,
2482 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
2484 6ced4176 2019-07-12 stsp goto done;
2485 6ced4176 2019-07-12 stsp return NULL;
2488 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2490 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2491 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2492 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2493 6ced4176 2019-07-12 stsp goto done;
2496 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2497 6ced4176 2019-07-12 stsp in_repo_path);
2499 6ced4176 2019-07-12 stsp goto done;
2501 6ced4176 2019-07-12 stsp free(in_repo_path);
2502 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2504 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2506 6ced4176 2019-07-12 stsp goto done;
2508 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2509 6ced4176 2019-07-12 stsp /* Check out a single file. */
2510 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2511 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2512 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2513 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2514 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2515 6ced4176 2019-07-12 stsp goto done;
2517 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2518 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2519 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2520 6ced4176 2019-07-12 stsp goto done;
2523 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2524 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2526 6ced4176 2019-07-12 stsp return err;
2527 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2528 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2529 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2530 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2531 6ced4176 2019-07-12 stsp goto done;
2534 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2535 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
2537 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2538 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2539 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2540 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2541 6ced4176 2019-07-12 stsp goto done;
2543 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2544 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2545 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2546 6ced4176 2019-07-12 stsp goto done;
2551 6ced4176 2019-07-12 stsp free(in_repo_path);
2552 6ced4176 2019-07-12 stsp if (err) {
2553 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2554 6ced4176 2019-07-12 stsp free(*tree_relpath);
2555 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2556 6ced4176 2019-07-12 stsp free(*tree_id);
2557 6ced4176 2019-07-12 stsp *tree_id = NULL;
2559 07ed472d 2019-07-12 stsp return err;
2562 07ed472d 2019-07-12 stsp static const struct got_error *
2563 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2564 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2565 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2566 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2568 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2569 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2570 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2571 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2572 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2574 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2575 7f47418f 2019-12-20 stsp if (err) {
2576 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2577 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2578 7f47418f 2019-12-20 stsp goto done;
2579 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2580 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2582 7f47418f 2019-12-20 stsp return err;
2585 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2586 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2588 07ed472d 2019-07-12 stsp goto done;
2590 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2592 07ed472d 2019-07-12 stsp goto done;
2594 07ed472d 2019-07-12 stsp if (entry_name &&
2595 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2596 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2597 07ed472d 2019-07-12 stsp goto done;
2600 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2601 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2602 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2603 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2604 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2605 07ed472d 2019-07-12 stsp arg.repo = repo;
2606 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2607 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2608 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2609 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2610 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2611 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2614 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2615 07ed472d 2019-07-12 stsp if (commit)
2616 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2617 6ced4176 2019-07-12 stsp return err;
2620 9d31a1d8 2018-03-11 stsp const struct got_error *
2621 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2622 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2623 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2624 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2626 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2627 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2628 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2629 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2630 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2631 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2632 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2633 f2ea84fa 2019-07-27 stsp SIMPLEQ_ENTRY(tree_path_data) entry;
2634 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2635 f2ea84fa 2019-07-27 stsp int entry_type;
2636 f2ea84fa 2019-07-27 stsp char *relpath;
2637 f2ea84fa 2019-07-27 stsp char *entry_name;
2638 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2639 f2ea84fa 2019-07-27 stsp SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2641 f2ea84fa 2019-07-27 stsp SIMPLEQ_INIT(&tree_paths);
2643 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2645 9d31a1d8 2018-03-11 stsp return err;
2647 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2648 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2649 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2650 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2651 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2652 f2ea84fa 2019-07-27 stsp goto done;
2655 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2656 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2657 f2ea84fa 2019-07-27 stsp if (err) {
2658 f2ea84fa 2019-07-27 stsp free(tpd);
2659 6ced4176 2019-07-12 stsp goto done;
2662 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2663 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2664 f2ea84fa 2019-07-27 stsp if (err) {
2665 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2666 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2667 f2ea84fa 2019-07-27 stsp free(tpd);
2668 f2ea84fa 2019-07-27 stsp goto done;
2671 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2673 f2ea84fa 2019-07-27 stsp SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2677 51514078 2018-12-25 stsp * Read the file index.
2678 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2679 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2681 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2683 c4cdcb68 2019-04-03 stsp goto done;
2685 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2686 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2687 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2689 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2690 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2691 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2695 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2696 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2697 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2698 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2699 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2700 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2701 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2702 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2706 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_NEXT(tpd, entry);
2708 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2709 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2710 af12c6b9 2019-06-04 stsp err = sync_err;
2712 9c6338c4 2019-06-02 stsp free(fileindex_path);
2714 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2715 9d31a1d8 2018-03-11 stsp if (commit)
2716 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2717 5ade8233 2019-07-12 stsp if (fileindex)
2718 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2719 f2ea84fa 2019-07-27 stsp while (!SIMPLEQ_EMPTY(&tree_paths)) {
2720 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2721 f2ea84fa 2019-07-27 stsp SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2722 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2723 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2724 f2ea84fa 2019-07-27 stsp free(tpd);
2726 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2727 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2728 9d31a1d8 2018-03-11 stsp err = unlockerr;
2729 f8d1f275 2019-02-04 stsp return err;
2732 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2733 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2734 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2735 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2736 234035bc 2019-06-01 stsp void *progress_arg;
2737 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2738 234035bc 2019-06-01 stsp void *cancel_arg;
2739 f69721c3 2019-10-21 stsp const char *label_orig;
2740 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2743 234035bc 2019-06-01 stsp static const struct got_error *
2744 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2745 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
2746 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
2747 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2749 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2750 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2751 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2752 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2753 234035bc 2019-06-01 stsp struct stat sb;
2754 234035bc 2019-06-01 stsp unsigned char status;
2755 234035bc 2019-06-01 stsp int local_changes_subsumed;
2757 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2758 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2759 d6c87207 2019-08-02 stsp strlen(path2));
2760 1ee397ad 2019-07-12 stsp if (ie == NULL)
2761 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2762 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2764 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2765 234035bc 2019-06-01 stsp path2) == -1)
2766 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2768 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2771 234035bc 2019-06-01 stsp goto done;
2773 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2774 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2775 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2776 234035bc 2019-06-01 stsp goto done;
2778 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2779 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2780 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2781 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2782 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2783 234035bc 2019-06-01 stsp goto done;
2786 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2787 36bf999c 2020-07-23 stsp char *link_target2;
2788 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2790 36bf999c 2020-07-23 stsp goto done;
2791 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2792 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2793 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2794 36bf999c 2020-07-23 stsp free(link_target2);
2796 af57b12a 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
2797 af57b12a 2020-07-23 stsp blob1, ondisk_path, path2, sb.st_mode,
2798 af57b12a 2020-07-23 stsp a->label_orig, blob2, a->commit_id2, repo,
2799 af57b12a 2020-07-23 stsp a->progress_cb, a->progress_arg);
2801 234035bc 2019-06-01 stsp } else if (blob1) {
2802 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2803 d6c87207 2019-08-02 stsp strlen(path1));
2804 1ee397ad 2019-07-12 stsp if (ie == NULL)
2805 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2806 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2808 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2809 2b92fad7 2019-06-02 stsp path1) == -1)
2810 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2812 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2815 2b92fad7 2019-06-02 stsp goto done;
2817 2b92fad7 2019-06-02 stsp switch (status) {
2818 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2819 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2820 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2822 1ee397ad 2019-07-12 stsp goto done;
2823 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2825 2b92fad7 2019-06-02 stsp goto done;
2827 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2829 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2830 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2831 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2832 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2834 1ee397ad 2019-07-12 stsp goto done;
2836 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2838 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
2839 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
2840 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
2842 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
2843 0a22ca1a 2020-09-23 stsp * exists in the repository.
2845 0a22ca1a 2020-09-23 stsp err = got_object_blob_file_create(&id, &blob1_f, path1);
2847 0a22ca1a 2020-09-23 stsp goto done;
2848 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
2849 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2850 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
2852 0a22ca1a 2020-09-23 stsp goto done;
2853 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
2856 0a22ca1a 2020-09-23 stsp goto done;
2858 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
2861 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2862 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
2864 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
2865 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
2868 0a22ca1a 2020-09-23 stsp goto done;
2871 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2872 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2873 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2874 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2876 1ee397ad 2019-07-12 stsp goto done;
2878 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2879 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2881 1ee397ad 2019-07-12 stsp goto done;
2886 234035bc 2019-06-01 stsp } else if (blob2) {
2887 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2888 234035bc 2019-06-01 stsp path2) == -1)
2889 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2890 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2891 d6c87207 2019-08-02 stsp strlen(path2));
2893 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2894 7f91a133 2019-12-13 stsp -1, NULL, repo);
2896 234035bc 2019-06-01 stsp goto done;
2897 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2898 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2899 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2900 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2901 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2902 1ee397ad 2019-07-12 stsp status, path2);
2903 234035bc 2019-06-01 stsp goto done;
2905 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2906 36bf999c 2020-07-23 stsp char *link_target2;
2907 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
2910 36bf999c 2020-07-23 stsp goto done;
2911 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
2912 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
2913 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
2914 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
2915 36bf999c 2020-07-23 stsp free(link_target2);
2916 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
2917 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
2918 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
2919 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
2920 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
2921 526a746f 2020-07-23 stsp a->progress_arg);
2923 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
2924 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
2927 dfe9fba0 2020-07-23 stsp goto done;
2928 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2929 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
2930 054041d0 2020-06-24 stsp ondisk_path, blob2->id.sha1,
2931 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
2933 234035bc 2019-06-01 stsp goto done;
2936 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2937 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2938 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
2939 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
2940 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
2941 c90c8ce3 2020-07-23 stsp 0, 1, repo, a->progress_cb, a->progress_arg);
2943 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
2944 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2945 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
2948 234035bc 2019-06-01 stsp goto done;
2949 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
2951 234035bc 2019-06-01 stsp goto done;
2952 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ondisk_path,
2953 3969253a 2020-03-07 stsp NULL, NULL, 1);
2954 3969253a 2020-03-07 stsp if (err) {
2955 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
2956 3969253a 2020-03-07 stsp goto done;
2958 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
2959 2b92fad7 2019-06-02 stsp if (err) {
2960 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
2961 2b92fad7 2019-06-02 stsp goto done;
2963 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2964 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2965 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2970 234035bc 2019-06-01 stsp free(ondisk_path);
2971 234035bc 2019-06-01 stsp return err;
2974 234035bc 2019-06-01 stsp struct check_merge_ok_arg {
2975 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2976 234035bc 2019-06-01 stsp struct got_repository *repo;
2979 234035bc 2019-06-01 stsp static const struct got_error *
2980 234035bc 2019-06-01 stsp check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2982 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
2983 234035bc 2019-06-01 stsp struct check_merge_ok_arg *a = arg;
2984 234035bc 2019-06-01 stsp unsigned char status;
2985 234035bc 2019-06-01 stsp struct stat sb;
2986 234035bc 2019-06-01 stsp char *ondisk_path;
2988 234035bc 2019-06-01 stsp /* Reject merges into a work tree with mixed base commits. */
2989 234035bc 2019-06-01 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2990 234035bc 2019-06-01 stsp SHA1_DIGEST_LENGTH))
2991 234035bc 2019-06-01 stsp return got_error(GOT_ERR_MIXED_COMMITS);
2993 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2995 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2997 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
2998 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3000 234035bc 2019-06-01 stsp return err;
3001 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3002 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3004 234035bc 2019-06-01 stsp return NULL;
3007 818c7501 2019-07-11 stsp static const struct got_error *
3008 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3009 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3010 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3011 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3012 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3014 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3015 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3016 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3017 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3018 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3020 03415a1a 2019-06-02 stsp if (commit_id1) {
3021 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3022 03415a1a 2019-06-02 stsp worktree->path_prefix);
3023 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3024 03415a1a 2019-06-02 stsp goto done;
3026 69d57f3d 2020-07-31 stsp if (tree_id1) {
3027 69d57f3d 2020-07-31 stsp char *id_str;
3029 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3031 03415a1a 2019-06-02 stsp goto done;
3033 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3035 f69721c3 2019-10-21 stsp goto done;
3037 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3038 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3039 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3040 f69721c3 2019-10-21 stsp free(id_str);
3041 f69721c3 2019-10-21 stsp goto done;
3043 f69721c3 2019-10-21 stsp free(id_str);
3046 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3047 234035bc 2019-06-01 stsp worktree->path_prefix);
3049 234035bc 2019-06-01 stsp goto done;
3051 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3053 234035bc 2019-06-01 stsp goto done;
3055 234035bc 2019-06-01 stsp arg.worktree = worktree;
3056 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3057 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3058 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3059 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3060 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3061 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3062 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3063 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3064 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3065 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3066 af12c6b9 2019-06-04 stsp err = sync_err;
3068 234035bc 2019-06-01 stsp if (tree1)
3069 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3070 234035bc 2019-06-01 stsp if (tree2)
3071 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3072 f69721c3 2019-10-21 stsp free(label_orig);
3073 818c7501 2019-07-11 stsp return err;
3076 818c7501 2019-07-11 stsp const struct got_error *
3077 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3078 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3079 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3080 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3082 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3083 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3084 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3085 818c7501 2019-07-11 stsp struct check_merge_ok_arg mok_arg;
3087 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3089 818c7501 2019-07-11 stsp return err;
3091 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3093 818c7501 2019-07-11 stsp goto done;
3095 818c7501 2019-07-11 stsp mok_arg.worktree = worktree;
3096 818c7501 2019-07-11 stsp mok_arg.repo = repo;
3097 818c7501 2019-07-11 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3098 818c7501 2019-07-11 stsp &mok_arg);
3100 818c7501 2019-07-11 stsp goto done;
3102 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3103 818c7501 2019-07-11 stsp commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3105 818c7501 2019-07-11 stsp if (fileindex)
3106 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3107 818c7501 2019-07-11 stsp free(fileindex_path);
3108 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3109 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3110 234035bc 2019-06-01 stsp err = unlockerr;
3111 234035bc 2019-06-01 stsp return err;
3114 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3115 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3116 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3117 927df6b7 2019-02-10 stsp const char *status_path;
3118 927df6b7 2019-02-10 stsp size_t status_path_len;
3119 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3120 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3121 f8d1f275 2019-02-04 stsp void *status_arg;
3122 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3123 f8d1f275 2019-02-04 stsp void *cancel_arg;
3124 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3125 6841da00 2019-08-08 stsp struct got_pathlist_head ignores;
3126 f2a9dc41 2019-12-13 tracey int report_unchanged;
3127 3143d852 2020-06-25 stsp int no_ignores;
3130 f8d1f275 2019-02-04 stsp static const struct got_error *
3131 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3132 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3133 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3134 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3136 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3137 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3138 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3139 927df6b7 2019-02-10 stsp struct stat sb;
3140 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3141 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3142 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3144 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3146 98eaaa12 2019-08-03 stsp return err;
3148 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3149 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3150 98eaaa12 2019-08-03 stsp return NULL;
3152 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3153 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3154 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3156 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3157 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3158 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3160 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3161 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3162 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3163 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3164 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3167 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3168 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3171 927df6b7 2019-02-10 stsp static const struct got_error *
3172 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3173 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3175 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3176 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3177 f8d1f275 2019-02-04 stsp char *abspath;
3179 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3180 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3182 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3183 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3184 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3185 927df6b7 2019-02-10 stsp return NULL;
3187 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3188 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3189 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3190 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3192 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3193 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3194 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3197 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3198 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3199 f8d1f275 2019-02-04 stsp free(abspath);
3200 9d31a1d8 2018-03-11 stsp return err;
3203 f8d1f275 2019-02-04 stsp static const struct got_error *
3204 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3206 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3207 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3208 2ec1f75b 2019-03-26 stsp unsigned char status;
3210 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3211 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3213 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3214 927df6b7 2019-02-10 stsp return NULL;
3216 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3217 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3218 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3219 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3221 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3222 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3223 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3227 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3229 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3231 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3232 6841da00 2019-08-08 stsp free((char *)pe->path);
3233 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3237 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3239 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3241 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3242 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3243 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3244 6841da00 2019-08-08 stsp free((char *)pe->path);
3246 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3249 6841da00 2019-08-08 stsp static const struct got_error *
3250 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3252 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3253 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3254 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3255 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3256 6841da00 2019-08-08 stsp size_t linesize = 0;
3257 6841da00 2019-08-08 stsp ssize_t linelen;
3259 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3260 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3261 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3262 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3264 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3265 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3266 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3268 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3269 bd8de430 2019-10-04 stsp if (line[0] == '#')
3272 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3273 bd8de430 2019-10-04 stsp if (line[0] == '!')
3276 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3277 6841da00 2019-08-08 stsp line) == -1) {
3278 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3279 6841da00 2019-08-08 stsp goto done;
3281 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3283 6841da00 2019-08-08 stsp goto done;
3285 6841da00 2019-08-08 stsp if (ferror(f)) {
3286 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3287 6841da00 2019-08-08 stsp goto done;
3290 6841da00 2019-08-08 stsp dirpath = strdup(path);
3291 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3292 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3293 6841da00 2019-08-08 stsp goto done;
3295 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3297 6841da00 2019-08-08 stsp free(line);
3298 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3299 6841da00 2019-08-08 stsp free(dirpath);
3300 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3302 6841da00 2019-08-08 stsp return err;
3306 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3308 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3310 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3311 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3312 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3313 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3315 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3316 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3318 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3320 bd8de430 2019-10-04 stsp pattern += 3;
3322 bd8de430 2019-10-04 stsp while (*p) {
3323 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3324 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3325 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3326 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3328 bd8de430 2019-10-04 stsp while (*p == '/')
3338 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3339 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3340 6841da00 2019-08-08 stsp * ignores backwards.
3342 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3343 6841da00 2019-08-08 stsp while (pe) {
3344 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3345 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3346 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3347 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3348 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3349 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3350 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3351 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3352 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3357 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3363 6841da00 2019-08-08 stsp static const struct got_error *
3364 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3365 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3367 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3368 6841da00 2019-08-08 stsp char *ignorespath;
3369 886cec17 2019-12-15 stsp int fd = -1;
3370 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3372 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3373 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3374 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3376 886cec17 2019-12-15 stsp if (dirfd != -1) {
3377 886cec17 2019-12-15 stsp fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3378 886cec17 2019-12-15 stsp if (fd == -1) {
3379 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3380 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3381 886cec17 2019-12-15 stsp ignorespath);
3383 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3384 886cec17 2019-12-15 stsp if (ignoresfile == NULL)
3385 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3386 886cec17 2019-12-15 stsp ignorespath);
3389 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3393 886cec17 2019-12-15 stsp ignoresfile = fopen(ignorespath, "r");
3394 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3395 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3396 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3397 886cec17 2019-12-15 stsp ignorespath);
3399 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3402 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3403 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3404 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3405 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3406 6841da00 2019-08-08 stsp free(ignorespath);
3407 6841da00 2019-08-08 stsp return err;
3410 f8d1f275 2019-02-04 stsp static const struct got_error *
3411 7f91a133 2019-12-13 stsp status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3413 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3414 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3415 f8d1f275 2019-02-04 stsp char *path = NULL;
3417 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3418 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3420 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3421 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3422 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3424 f8d1f275 2019-02-04 stsp path = de->d_name;
3427 3143d852 2020-06-25 stsp if (de->d_type != DT_DIR &&
3428 3143d852 2020-06-25 stsp got_path_is_child(path, a->status_path, a->status_path_len)
3429 6841da00 2019-08-08 stsp && !match_ignores(&a->ignores, path))
3430 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3431 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3432 f8d1f275 2019-02-04 stsp if (parent_path[0])
3433 f8d1f275 2019-02-04 stsp free(path);
3434 b72f483a 2019-02-05 stsp return err;
3437 347d1d3e 2019-07-12 stsp static const struct got_error *
3438 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3440 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3441 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3443 3143d852 2020-06-25 stsp if (a->no_ignores)
3444 3143d852 2020-06-25 stsp return NULL;
3446 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path,
3447 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3449 3143d852 2020-06-25 stsp return err;
3451 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path, path,
3452 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3454 3143d852 2020-06-25 stsp return err;
3457 3143d852 2020-06-25 stsp static const struct got_error *
3458 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3459 abb4604f 2019-07-27 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3460 f2a9dc41 2019-12-13 tracey void *status_arg, struct got_repository *repo, int report_unchanged)
3462 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3463 abb4604f 2019-07-27 stsp struct stat sb;
3465 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3467 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3468 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3470 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3471 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3472 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3473 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3474 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3475 abb4604f 2019-07-27 stsp return NULL;
3478 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3479 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3480 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3482 abb4604f 2019-07-27 stsp return NULL;
3485 3143d852 2020-06-25 stsp static const struct got_error *
3486 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3487 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3489 3143d852 2020-06-25 stsp const struct got_error *err;
3490 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3492 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3493 3143d852 2020-06-25 stsp ".cvsignore");
3495 3143d852 2020-06-25 stsp return err;
3497 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3498 3143d852 2020-06-25 stsp ".gitignore");
3500 3143d852 2020-06-25 stsp return err;
3502 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3503 3143d852 2020-06-25 stsp if (err) {
3504 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3505 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3506 3143d852 2020-06-25 stsp return err;
3508 3143d852 2020-06-25 stsp for (;;) {
3509 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3510 3143d852 2020-06-25 stsp ".cvsignore");
3513 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3514 3143d852 2020-06-25 stsp ".gitignore");
3517 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3518 3143d852 2020-06-25 stsp if (err) {
3519 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3520 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3523 b737c85a 2020-06-26 stsp free(parent_path);
3524 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3525 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3528 b737c85a 2020-06-26 stsp free(parent_path);
3529 b737c85a 2020-06-26 stsp free(next_parent_path);
3530 3143d852 2020-06-25 stsp return err;
3533 abb4604f 2019-07-27 stsp static const struct got_error *
3534 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3535 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3536 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3537 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3538 f2a9dc41 2019-12-13 tracey int report_unchanged)
3540 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3541 6fc93f37 2019-12-13 stsp int fd = -1;
3542 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3543 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3544 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3546 3143d852 2020-06-25 stsp TAILQ_INIT(&arg.ignores);
3548 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3549 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3550 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3552 6fc93f37 2019-12-13 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3553 6fc93f37 2019-12-13 stsp if (fd == -1) {
3554 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3555 00bb5ea0 2020-07-23 stsp errno != ELOOP)
3556 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3558 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3559 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3560 f2a9dc41 2019-12-13 tracey report_unchanged);
3562 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3563 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3564 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3565 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3566 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3567 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3568 abb4604f 2019-07-27 stsp arg.status_path = path;
3569 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3570 abb4604f 2019-07-27 stsp arg.repo = repo;
3571 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3572 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3573 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3574 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3575 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3576 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3577 022fae89 2019-12-06 tracey if (!no_ignores) {
3578 3143d852 2020-06-25 stsp err = add_ignores_from_parent_paths(&arg.ignores,
3579 3143d852 2020-06-25 stsp worktree->root_path, path);
3581 3143d852 2020-06-25 stsp goto done;
3583 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3584 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3587 3143d852 2020-06-25 stsp free_ignores(&arg.ignores);
3588 6fc93f37 2019-12-13 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
3589 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3590 927df6b7 2019-02-10 stsp free(ondisk_path);
3591 347d1d3e 2019-07-12 stsp return err;
3594 347d1d3e 2019-07-12 stsp const struct got_error *
3595 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3596 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3597 72ea6654 2019-07-27 stsp got_worktree_status_cb status_cb, void *status_arg,
3598 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3600 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3601 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3602 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3603 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3605 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3607 347d1d3e 2019-07-12 stsp return err;
3609 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3610 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3611 f2a9dc41 2019-12-13 tracey status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3615 f8d1f275 2019-02-04 stsp free(fileindex_path);
3616 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3617 f8d1f275 2019-02-04 stsp return err;
3620 6c7ab921 2019-03-18 stsp const struct got_error *
3621 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3622 6c7ab921 2019-03-18 stsp const char *arg)
3624 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3625 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3626 6c7ab921 2019-03-18 stsp size_t len;
3627 00bb5ea0 2020-07-23 stsp struct stat sb;
3628 01740607 2020-11-04 stsp char *abspath = NULL;
3629 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3631 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3633 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3634 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3635 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3637 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3638 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3639 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3640 00bb5ea0 2020-07-23 stsp goto done;
3642 727173c3 2020-11-06 stsp sb.st_mode = 0;
3644 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3646 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3647 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3648 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3649 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3651 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3652 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3653 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3654 00bb5ea0 2020-07-23 stsp goto done;
3658 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3659 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3661 d0710d08 2019-07-22 stsp goto done;
3662 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3663 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3664 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3665 00bb5ea0 2020-07-23 stsp goto done;
3668 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3669 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3670 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3671 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3672 00bb5ea0 2020-07-23 stsp goto done;
3674 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3675 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3676 01740607 2020-11-04 stsp goto done;
3678 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
3679 01740607 2020-11-04 stsp sizeof(canonpath));
3681 00bb5ea0 2020-07-23 stsp goto done;
3682 01740607 2020-11-04 stsp resolved = strdup(canonpath);
3683 01740607 2020-11-04 stsp if (resolved == NULL) {
3684 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
3685 01740607 2020-11-04 stsp goto done;
3690 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3691 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3692 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3693 6c7ab921 2019-03-18 stsp goto done;
3696 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3697 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3698 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3700 f6d88e1a 2019-05-29 stsp goto done;
3702 f6d88e1a 2019-05-29 stsp path = strdup("");
3703 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3704 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3705 f6d88e1a 2019-05-29 stsp goto done;
3709 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3710 6c7ab921 2019-03-18 stsp len = strlen(path);
3711 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3712 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3716 01740607 2020-11-04 stsp free(abspath);
3717 6c7ab921 2019-03-18 stsp free(resolved);
3718 d0710d08 2019-07-22 stsp free(cwd);
3719 6c7ab921 2019-03-18 stsp if (err == NULL)
3720 6c7ab921 2019-03-18 stsp *wt_path = path;
3722 6c7ab921 2019-03-18 stsp free(path);
3723 d00136be 2019-03-26 stsp return err;
3726 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3727 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3728 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3729 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3730 4e68cba3 2019-11-23 stsp void *progress_arg;
3731 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3734 4e68cba3 2019-11-23 stsp static const struct got_error *
3735 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3736 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3737 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3738 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3740 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3741 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3742 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3743 6d022e97 2019-08-04 stsp struct stat sb;
3744 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3746 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3747 dbb83fbd 2019-12-12 stsp relpath) == -1)
3748 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3750 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3752 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3753 12463d8b 2019-12-13 stsp de_name, a->repo);
3755 dbb83fbd 2019-12-12 stsp goto done;
3756 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3757 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3758 dbb83fbd 2019-12-12 stsp goto done;
3759 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3761 dbb83fbd 2019-12-12 stsp goto done;
3764 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3765 dbb83fbd 2019-12-12 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3766 dbb83fbd 2019-12-12 stsp goto done;
3769 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3771 4e68cba3 2019-11-23 stsp goto done;
3772 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3773 3969253a 2020-03-07 stsp if (err) {
3774 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3775 3969253a 2020-03-07 stsp goto done;
3777 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3778 07f5b47a 2019-06-02 stsp if (err) {
3779 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
3780 4e68cba3 2019-11-23 stsp goto done;
3783 dbb83fbd 2019-12-12 stsp free(ondisk_path);
3785 dbb83fbd 2019-12-12 stsp return err;
3786 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
3787 dbb83fbd 2019-12-12 stsp return NULL;
3788 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3791 d00136be 2019-03-26 stsp const struct got_error *
3792 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
3793 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
3794 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3795 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
3797 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3798 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
3799 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3800 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
3801 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
3803 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3805 d00136be 2019-03-26 stsp return err;
3807 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3809 d00136be 2019-03-26 stsp goto done;
3811 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
3812 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
3813 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
3814 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
3815 4e68cba3 2019-11-23 stsp saa.repo = repo;
3817 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3818 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3819 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3823 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3824 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3825 af12c6b9 2019-06-04 stsp err = sync_err;
3827 fb399478 2019-07-12 stsp free(fileindex_path);
3828 d00136be 2019-03-26 stsp if (fileindex)
3829 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
3830 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3831 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
3832 d00136be 2019-03-26 stsp err = unlockerr;
3833 6c7ab921 2019-03-18 stsp return err;
3836 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
3837 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
3838 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
3839 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
3840 f2a9dc41 2019-12-13 tracey void *progress_arg;
3841 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
3842 f2a9dc41 2019-12-13 tracey int delete_local_mods;
3843 70e3e7f5 2019-12-13 tracey int keep_on_disk;
3844 766841c2 2020-08-13 stsp const char *status_codes;
3847 f2a9dc41 2019-12-13 tracey static const struct got_error *
3848 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
3849 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
3850 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3851 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
3853 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
3854 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
3855 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
3856 17ed4618 2019-06-02 stsp struct stat sb;
3857 20a2ad1c 2020-10-20 stsp char *ondisk_path;
3859 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3860 17ed4618 2019-06-02 stsp if (ie == NULL)
3861 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
3863 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
3864 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
3865 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3866 9acbc4fa 2019-08-03 stsp return NULL;
3867 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3870 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3871 f2a9dc41 2019-12-13 tracey relpath) == -1)
3872 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
3874 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3877 f2a9dc41 2019-12-13 tracey goto done;
3879 766841c2 2020-08-13 stsp if (a->status_codes) {
3880 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
3882 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
3883 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
3886 766841c2 2020-08-13 stsp if (i == ncodes) {
3887 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
3888 766841c2 2020-08-13 stsp free(ondisk_path);
3889 766841c2 2020-08-13 stsp return NULL;
3891 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3892 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
3893 766841c2 2020-08-13 stsp static char msg[64];
3894 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
3895 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
3896 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3897 766841c2 2020-08-13 stsp goto done;
3901 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
3902 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
3903 f2a9dc41 2019-12-13 tracey goto done;
3904 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3905 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3906 f2a9dc41 2019-12-13 tracey goto done;
3908 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
3909 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
3910 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3911 f2a9dc41 2019-12-13 tracey goto done;
3915 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3916 20a2ad1c 2020-10-20 stsp size_t root_len;
3918 12463d8b 2019-12-13 stsp if (dirfd != -1) {
3919 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
3920 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
3921 12463d8b 2019-12-13 stsp ondisk_path);
3922 12463d8b 2019-12-13 stsp goto done;
3924 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
3925 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
3926 12463d8b 2019-12-13 stsp goto done;
3929 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
3931 20a2ad1c 2020-10-20 stsp char *parent;
3932 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
3934 2513f20a 2020-10-20 stsp goto done;
3935 20a2ad1c 2020-10-20 stsp free(ondisk_path);
3936 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
3937 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
3938 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
3939 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
3940 20a2ad1c 2020-10-20 stsp ondisk_path);
3943 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
3944 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
3947 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3949 f2a9dc41 2019-12-13 tracey free(ondisk_path);
3950 f2a9dc41 2019-12-13 tracey if (err)
3951 f2a9dc41 2019-12-13 tracey return err;
3952 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
3953 f2a9dc41 2019-12-13 tracey return NULL;
3954 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3955 f2a9dc41 2019-12-13 tracey staged_status, relpath);
3958 2ec1f75b 2019-03-26 stsp const struct got_error *
3959 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
3960 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
3961 766841c2 2020-08-13 stsp const char *status_codes,
3962 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
3963 70e3e7f5 2019-12-13 tracey struct got_repository *repo, int keep_on_disk)
3965 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3966 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
3967 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3968 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
3969 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
3971 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3973 2ec1f75b 2019-03-26 stsp return err;
3975 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3977 2ec1f75b 2019-03-26 stsp goto done;
3979 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
3980 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
3981 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
3982 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
3983 f2a9dc41 2019-12-13 tracey sda.repo = repo;
3984 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
3985 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
3986 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
3988 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3989 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
3990 f2a9dc41 2019-12-13 tracey schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3994 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3995 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3996 af12c6b9 2019-06-04 stsp err = sync_err;
3998 fb399478 2019-07-12 stsp free(fileindex_path);
3999 2ec1f75b 2019-03-26 stsp if (fileindex)
4000 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4001 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4002 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4003 2ec1f75b 2019-03-26 stsp err = unlockerr;
4004 33aa809d 2019-08-08 stsp return err;
4007 33aa809d 2019-08-08 stsp static const struct got_error *
4008 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4010 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4011 33aa809d 2019-08-08 stsp char *line = NULL;
4012 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4013 33aa809d 2019-08-08 stsp ssize_t linelen;
4015 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4016 33aa809d 2019-08-08 stsp if (linelen == -1) {
4017 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4018 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4019 33aa809d 2019-08-08 stsp goto done;
4021 33aa809d 2019-08-08 stsp return NULL;
4023 33aa809d 2019-08-08 stsp if (outfile) {
4024 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4025 33aa809d 2019-08-08 stsp if (n != linelen) {
4026 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4027 33aa809d 2019-08-08 stsp goto done;
4030 33aa809d 2019-08-08 stsp if (rejectfile) {
4031 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4032 33aa809d 2019-08-08 stsp if (n != linelen)
4033 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4036 33aa809d 2019-08-08 stsp free(line);
4037 2ec1f75b 2019-03-26 stsp return err;
4040 33aa809d 2019-08-08 stsp static const struct got_error *
4041 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4043 33aa809d 2019-08-08 stsp char *line = NULL;
4044 33aa809d 2019-08-08 stsp size_t linesize = 0;
4045 33aa809d 2019-08-08 stsp ssize_t linelen;
4047 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4048 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4049 500467ff 2019-09-25 hiltjo if (ferror(f))
4050 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4051 500467ff 2019-09-25 hiltjo return NULL;
4053 33aa809d 2019-08-08 stsp free(line);
4054 33aa809d 2019-08-08 stsp return NULL;
4057 33aa809d 2019-08-08 stsp static const struct got_error *
4058 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4059 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4060 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4062 33aa809d 2019-08-08 stsp const struct got_error *err;
4064 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4065 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4066 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4068 33aa809d 2019-08-08 stsp return err;
4069 33aa809d 2019-08-08 stsp (*line_cur1)++;
4071 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4072 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4073 33aa809d 2019-08-08 stsp if (rejectfile)
4074 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4076 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4078 33aa809d 2019-08-08 stsp return err;
4079 33aa809d 2019-08-08 stsp (*line_cur2)++;
4081 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4082 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4083 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4085 33aa809d 2019-08-08 stsp return err;
4086 33aa809d 2019-08-08 stsp (*line_cur2)++;
4088 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4089 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4090 33aa809d 2019-08-08 stsp if (rejectfile)
4091 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4093 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4095 33aa809d 2019-08-08 stsp return err;
4096 33aa809d 2019-08-08 stsp (*line_cur1)++;
4099 f1e81a05 2019-08-10 stsp return NULL;
4102 f1e81a05 2019-08-10 stsp static const struct got_error *
4103 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4104 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4106 f1e81a05 2019-08-10 stsp const struct got_error *err;
4108 f1e81a05 2019-08-10 stsp if (outfile) {
4109 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4110 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4111 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4113 f1e81a05 2019-08-10 stsp return err;
4114 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4117 f1e81a05 2019-08-10 stsp if (rejectfile) {
4118 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4119 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4120 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4122 f1e81a05 2019-08-10 stsp return err;
4123 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4127 33aa809d 2019-08-08 stsp return NULL;
4130 33aa809d 2019-08-08 stsp static const struct got_error *
4131 33aa809d 2019-08-08 stsp apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
4132 33aa809d 2019-08-08 stsp int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
4133 33aa809d 2019-08-08 stsp int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
4134 33aa809d 2019-08-08 stsp int *line_cur2, FILE *outfile, FILE *rejectfile,
4135 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4137 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4138 33aa809d 2019-08-08 stsp int start_old = change->cv.a;
4139 33aa809d 2019-08-08 stsp int end_old = change->cv.b;
4140 33aa809d 2019-08-08 stsp int start_new = change->cv.c;
4141 33aa809d 2019-08-08 stsp int end_new = change->cv.d;
4142 33aa809d 2019-08-08 stsp long pos1, pos2;
4143 33aa809d 2019-08-08 stsp FILE *hunkfile;
4145 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4147 33aa809d 2019-08-08 stsp hunkfile = got_opentemp();
4148 33aa809d 2019-08-08 stsp if (hunkfile == NULL)
4149 33aa809d 2019-08-08 stsp return got_error_from_errno("got_opentemp");
4151 33aa809d 2019-08-08 stsp pos1 = ftell(f1);
4152 33aa809d 2019-08-08 stsp pos2 = ftell(f2);
4154 33aa809d 2019-08-08 stsp /* XXX TODO needs error checking */
4155 33aa809d 2019-08-08 stsp got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4157 33aa809d 2019-08-08 stsp if (fseek(f1, pos1, SEEK_SET) == -1) {
4158 33aa809d 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
4159 33aa809d 2019-08-08 stsp goto done;
4161 33aa809d 2019-08-08 stsp if (fseek(f2, pos2, SEEK_SET) == -1) {
4162 33aa809d 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
4163 33aa809d 2019-08-08 stsp goto done;
4165 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4166 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4167 33aa809d 2019-08-08 stsp goto done;
4170 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4171 33aa809d 2019-08-08 stsp hunkfile, n, nchanges);
4173 33aa809d 2019-08-08 stsp goto done;
4175 33aa809d 2019-08-08 stsp switch (*choice) {
4176 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4177 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4178 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4180 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4181 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4182 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4184 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4187 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4191 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4192 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4193 33aa809d 2019-08-08 stsp return err;
4196 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4197 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4198 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4199 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4200 1f1abb7e 2019-08-08 stsp void *progress_arg;
4201 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4202 33aa809d 2019-08-08 stsp void *patch_arg;
4203 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4206 e20a8b6f 2019-06-04 stsp static const struct got_error *
4207 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4208 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4209 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4210 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4211 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4213 33aa809d 2019-08-08 stsp const struct got_error *err;
4214 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4215 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4216 1ebedb77 2019-10-19 stsp int fd2 = -1;
4217 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4218 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4219 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4220 33aa809d 2019-08-08 stsp struct stat sb1, sb2;
4221 33aa809d 2019-08-08 stsp struct got_diff_changes *changes = NULL;
4222 33aa809d 2019-08-08 stsp struct got_diff_state *ds = NULL;
4223 33aa809d 2019-08-08 stsp struct got_diff_args *args = NULL;
4224 33aa809d 2019-08-08 stsp struct got_diff_change *change;
4225 33aa809d 2019-08-08 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4226 33aa809d 2019-08-08 stsp int n = 0;
4228 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4230 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4232 33aa809d 2019-08-08 stsp return err;
4234 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4235 12463d8b 2019-12-13 stsp fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4236 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4237 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4238 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4239 fa3cef63 2020-07-23 stsp goto done;
4241 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4242 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4243 fa3cef63 2020-07-23 stsp if (link_len == -1)
4244 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlinkat", path2);
4245 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4246 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4249 12463d8b 2019-12-13 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4250 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4251 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4252 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4253 fa3cef63 2020-07-23 stsp goto done;
4255 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4256 fa3cef63 2020-07-23 stsp sizeof(link_target));
4257 fa3cef63 2020-07-23 stsp if (link_len == -1)
4258 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4259 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4260 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4263 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4264 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4265 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4266 fa3cef63 2020-07-23 stsp goto done;
4269 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4270 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4271 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4272 fa3cef63 2020-07-23 stsp goto done;
4277 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4278 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4279 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4280 fa3cef63 2020-07-23 stsp goto done;
4282 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4283 fa3cef63 2020-07-23 stsp if (n != link_len) {
4284 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4285 fa3cef63 2020-07-23 stsp goto done;
4287 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4288 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4289 fa3cef63 2020-07-23 stsp goto done;
4291 fa3cef63 2020-07-23 stsp rewind(f2);
4294 33aa809d 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4296 33aa809d 2019-08-08 stsp goto done;
4298 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4300 33aa809d 2019-08-08 stsp goto done;
4302 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4304 33aa809d 2019-08-08 stsp goto done;
4306 33aa809d 2019-08-08 stsp if (stat(path1, &sb1) == -1) {
4307 33aa809d 2019-08-08 stsp err = got_error_from_errno2("stat", path1);
4308 33aa809d 2019-08-08 stsp goto done;
4311 33aa809d 2019-08-08 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
4312 33aa809d 2019-08-08 stsp f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4314 33aa809d 2019-08-08 stsp goto done;
4316 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4318 33aa809d 2019-08-08 stsp goto done;
4320 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4321 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4322 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4323 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4324 33aa809d 2019-08-08 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4325 33aa809d 2019-08-08 stsp int choice;
4326 33aa809d 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
4327 33aa809d 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
4328 33aa809d 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
4329 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4330 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4331 e635744c 2019-08-08 stsp patch_cb, patch_arg);
4333 33aa809d 2019-08-08 stsp goto done;
4334 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4335 33aa809d 2019-08-08 stsp have_content = 1;
4336 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4339 1ebedb77 2019-10-19 stsp if (have_content) {
4340 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4341 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4342 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4344 1ebedb77 2019-10-19 stsp goto done;
4346 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4347 3818e3c4 2020-11-01 naddy if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4348 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4349 fa3cef63 2020-07-23 stsp goto done;
4354 33aa809d 2019-08-08 stsp free(id_str);
4356 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4357 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4358 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4359 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4360 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4361 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4362 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4363 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4364 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4365 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4366 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4367 33aa809d 2019-08-08 stsp if (err || !have_content) {
4368 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4369 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4370 33aa809d 2019-08-08 stsp free(*path_outfile);
4371 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4373 33aa809d 2019-08-08 stsp free(args);
4375 33aa809d 2019-08-08 stsp got_diff_state_free(ds);
4378 33aa809d 2019-08-08 stsp if (changes)
4379 33aa809d 2019-08-08 stsp got_diff_free_changes(changes);
4380 33aa809d 2019-08-08 stsp free(path1);
4381 33aa809d 2019-08-08 stsp return err;
4384 33aa809d 2019-08-08 stsp static const struct got_error *
4385 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4386 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4387 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4388 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4390 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4391 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4392 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4393 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4394 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4395 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4396 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4397 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4398 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4399 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4401 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4402 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4403 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4404 d3bcc3d1 2019-08-08 stsp return NULL;
4406 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4407 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4408 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4410 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4411 65084dad 2019-08-08 stsp if (ie == NULL)
4412 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4414 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4415 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4416 a129376b 2019-03-28 stsp if (err) {
4417 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4418 a129376b 2019-03-28 stsp goto done;
4419 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4420 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4421 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4422 e20a8b6f 2019-06-04 stsp goto done;
4425 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4426 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4427 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4428 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4429 a129376b 2019-03-28 stsp goto done;
4432 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4433 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4434 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4435 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4436 a129376b 2019-03-28 stsp goto done;
4439 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4440 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4441 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4442 a129376b 2019-03-28 stsp goto done;
4447 1f1abb7e 2019-08-08 stsp err = got_object_id_by_path(&tree_id, a->repo,
4448 1f1abb7e 2019-08-08 stsp a->worktree->base_commit_id, tree_path);
4449 a9fa2909 2019-07-27 stsp if (err) {
4450 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4451 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4452 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4453 a9fa2909 2019-07-27 stsp goto done;
4455 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4457 a9fa2909 2019-07-27 stsp goto done;
4459 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4461 a9fa2909 2019-07-27 stsp goto done;
4463 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4464 1233e6b6 2020-10-19 stsp free(te_name);
4465 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4466 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4467 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4468 a9fa2909 2019-07-27 stsp goto done;
4472 a129376b 2019-03-28 stsp switch (status) {
4473 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4474 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4475 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4476 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4477 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4479 33aa809d 2019-08-08 stsp goto done;
4480 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4483 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4484 1f1abb7e 2019-08-08 stsp ie->path);
4486 1ee397ad 2019-07-12 stsp goto done;
4487 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4489 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4490 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4491 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4492 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4493 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4495 33aa809d 2019-08-08 stsp goto done;
4496 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4499 33aa809d 2019-08-08 stsp /* fall through */
4500 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4501 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4502 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4503 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4504 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4505 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4506 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4507 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4508 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4510 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4511 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4512 1f1abb7e 2019-08-08 stsp err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4514 65084dad 2019-08-08 stsp goto done;
4516 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4517 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4518 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4519 a129376b 2019-03-28 stsp goto done;
4522 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4523 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4524 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4525 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4526 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4527 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4528 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4530 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4531 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4532 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4533 369fd7e5 2020-07-23 stsp path_content);
4536 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4537 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4538 369fd7e5 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4539 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4541 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4542 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4543 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4544 369fd7e5 2020-07-23 stsp goto done;
4548 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4549 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4550 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4551 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4552 c90c8ce3 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4553 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4555 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4557 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4558 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4559 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4560 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4563 a129376b 2019-03-28 stsp goto done;
4564 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4565 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4566 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4567 054041d0 2020-06-24 stsp ondisk_path, blob->id.sha1,
4568 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4570 2e1fa222 2020-07-23 stsp goto done;
4572 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4573 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4574 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4583 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4584 33aa809d 2019-08-08 stsp free(path_content);
4585 e20a8b6f 2019-06-04 stsp free(parent_path);
4586 a129376b 2019-03-28 stsp free(tree_path);
4588 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4590 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4591 a129376b 2019-03-28 stsp free(tree_id);
4592 e20a8b6f 2019-06-04 stsp return err;
4595 e20a8b6f 2019-06-04 stsp const struct got_error *
4596 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4597 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4598 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4599 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4600 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4602 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4603 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4604 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4605 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4606 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4607 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4609 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4611 e20a8b6f 2019-06-04 stsp return err;
4613 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4615 e20a8b6f 2019-06-04 stsp goto done;
4617 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4618 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4619 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4620 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4621 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4622 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4623 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4624 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4625 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4626 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
4630 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4631 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4632 e20a8b6f 2019-06-04 stsp err = sync_err;
4634 fb399478 2019-07-12 stsp free(fileindex_path);
4635 a129376b 2019-03-28 stsp if (fileindex)
4636 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4637 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4638 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4639 a129376b 2019-03-28 stsp err = unlockerr;
4640 c4296144 2019-05-09 stsp return err;
4643 cf066bf8 2019-05-09 stsp static void
4644 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4646 24519714 2019-05-09 stsp free(ct->path);
4647 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4648 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4649 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4650 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4651 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4652 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4656 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4657 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4658 24519714 2019-05-09 stsp struct got_repository *repo;
4659 24519714 2019-05-09 stsp struct got_worktree *worktree;
4660 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4661 5f8a88c6 2019-08-03 stsp int have_staged_files;
4662 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4665 c4296144 2019-05-09 stsp static const struct got_error *
4666 88d0e355 2019-08-03 stsp collect_commitables(void *arg, unsigned char status,
4667 88d0e355 2019-08-03 stsp unsigned char staged_status, const char *relpath,
4668 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4669 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4671 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
4672 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4673 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4674 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4675 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4676 768aea60 2019-05-09 stsp struct stat sb;
4678 5f8a88c6 2019-08-03 stsp if (a->have_staged_files) {
4679 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4680 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4681 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4682 5f8a88c6 2019-08-03 stsp return NULL;
4684 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4685 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4687 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4688 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4689 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4690 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4691 5f8a88c6 2019-08-03 stsp return NULL;
4694 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4695 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4696 036813ee 2019-05-09 stsp goto done;
4698 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4699 036813ee 2019-05-09 stsp parent_path = strdup("");
4700 69960a46 2019-05-09 stsp if (parent_path == NULL)
4701 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4703 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4705 69960a46 2019-05-09 stsp return err;
4708 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4709 cf066bf8 2019-05-09 stsp if (ct == NULL) {
4710 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4711 c4296144 2019-05-09 stsp goto done;
4714 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4715 768aea60 2019-05-09 stsp relpath) == -1) {
4716 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4717 768aea60 2019-05-09 stsp goto done;
4720 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
4721 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
4722 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
4723 0aeb8099 2020-07-23 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4724 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
4725 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
4726 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
4727 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
4729 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
4730 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
4733 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4734 0aeb8099 2020-07-23 stsp goto done;
4736 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
4737 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
4738 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
4739 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4740 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
4741 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
4742 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
4743 12463d8b 2019-12-13 stsp ct->ondisk_path);
4744 12463d8b 2019-12-13 stsp goto done;
4746 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
4747 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
4748 768aea60 2019-05-09 stsp goto done;
4750 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
4753 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4754 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4755 44d03001 2019-05-09 stsp relpath) == -1) {
4756 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4757 44d03001 2019-05-09 stsp goto done;
4760 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4761 35213c7c 2020-07-23 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4762 35213c7c 2020-07-23 stsp int is_bad_symlink;
4763 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
4764 35213c7c 2020-07-23 stsp ssize_t target_len;
4765 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
4766 35213c7c 2020-07-23 stsp sizeof(target_path));
4767 35213c7c 2020-07-23 stsp if (target_len == -1) {
4768 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
4769 35213c7c 2020-07-23 stsp ct->ondisk_path);
4770 35213c7c 2020-07-23 stsp goto done;
4772 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
4773 35213c7c 2020-07-23 stsp target_len, ct->ondisk_path, a->worktree->root_path);
4775 35213c7c 2020-07-23 stsp goto done;
4776 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
4777 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
4778 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
4779 35213c7c 2020-07-23 stsp goto done;
4784 cf066bf8 2019-05-09 stsp ct->status = status;
4785 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
4786 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
4787 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
4788 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
4789 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
4790 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
4791 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4792 b416585c 2019-05-13 stsp goto done;
4794 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
4795 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
4796 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
4797 f0b75401 2019-08-03 stsp goto done;
4800 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
4801 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
4802 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4803 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
4804 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4805 036813ee 2019-05-09 stsp goto done;
4808 24519714 2019-05-09 stsp ct->path = strdup(path);
4809 24519714 2019-05-09 stsp if (ct->path == NULL) {
4810 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4811 24519714 2019-05-09 stsp goto done;
4813 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4815 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
4816 ed175427 2019-05-09 stsp free_commitable(ct);
4817 24519714 2019-05-09 stsp free(parent_path);
4818 036813ee 2019-05-09 stsp free(path);
4819 a129376b 2019-03-28 stsp return err;
4822 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
4823 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
4824 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4825 036813ee 2019-05-09 stsp struct got_repository *);
4827 ed175427 2019-05-09 stsp static const struct got_error *
4828 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4829 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
4830 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
4831 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4832 afa376bf 2019-05-09 stsp struct got_repository *repo)
4834 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
4835 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
4836 036813ee 2019-05-09 stsp char *subpath;
4838 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
4839 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4840 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4842 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
4844 ed175427 2019-05-09 stsp return err;
4846 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
4847 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
4848 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
4849 036813ee 2019-05-09 stsp free(subpath);
4850 036813ee 2019-05-09 stsp return err;
4853 036813ee 2019-05-09 stsp static const struct got_error *
4854 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4856 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4857 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
4859 036813ee 2019-05-09 stsp *match = 0;
4861 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
4862 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
4863 0f63689d 2019-05-10 stsp return NULL;
4866 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4868 0f63689d 2019-05-10 stsp return err;
4869 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
4870 036813ee 2019-05-09 stsp free(ct_parent_path);
4871 036813ee 2019-05-09 stsp return err;
4874 768aea60 2019-05-09 stsp static mode_t
4875 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
4877 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
4878 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
4880 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4883 036813ee 2019-05-09 stsp static const struct got_error *
4884 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4885 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
4887 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4889 036813ee 2019-05-09 stsp *new_te = NULL;
4891 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
4893 036813ee 2019-05-09 stsp goto done;
4895 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4897 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
4898 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4899 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4901 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4903 036813ee 2019-05-09 stsp if (err && *new_te) {
4904 56e0773d 2019-11-28 stsp free(*new_te);
4905 036813ee 2019-05-09 stsp *new_te = NULL;
4907 036813ee 2019-05-09 stsp return err;
4910 036813ee 2019-05-09 stsp static const struct got_error *
4911 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4912 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
4914 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4915 102b254e 2020-10-19 stsp char *ct_name = NULL;
4917 036813ee 2019-05-09 stsp *new_te = NULL;
4919 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
4920 036813ee 2019-05-09 stsp if (*new_te == NULL)
4921 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
4923 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
4925 036813ee 2019-05-09 stsp goto done;
4926 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4927 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
4928 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
4929 036813ee 2019-05-09 stsp goto done;
4932 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4934 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
4935 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4936 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4938 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4940 102b254e 2020-10-19 stsp free(ct_name);
4941 036813ee 2019-05-09 stsp if (err && *new_te) {
4942 56e0773d 2019-11-28 stsp free(*new_te);
4943 036813ee 2019-05-09 stsp *new_te = NULL;
4945 036813ee 2019-05-09 stsp return err;
4948 036813ee 2019-05-09 stsp static const struct got_error *
4949 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
4950 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
4952 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4953 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
4955 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4957 036813ee 2019-05-09 stsp return err;
4958 036813ee 2019-05-09 stsp if (new_pe == NULL)
4959 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
4960 036813ee 2019-05-09 stsp return NULL;
4963 afa376bf 2019-05-09 stsp static const struct got_error *
4964 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
4965 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
4967 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
4968 5f8a88c6 2019-08-03 stsp unsigned char status;
4970 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
4971 afa376bf 2019-05-09 stsp ct_path++;
4973 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4974 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
4976 5f8a88c6 2019-08-03 stsp status = ct->status;
4978 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4979 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4982 036813ee 2019-05-09 stsp static const struct got_error *
4983 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
4984 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4986 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
4987 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
4988 44d03001 2019-05-09 stsp char *te_path;
4990 44d03001 2019-05-09 stsp *modified = 0;
4992 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
4993 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
4994 44d03001 2019-05-09 stsp te->name) == -1)
4995 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4997 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4998 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4999 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5000 44d03001 2019-05-09 stsp strlen(te_path));
5001 62d463ca 2020-10-20 naddy if (*modified)
5005 44d03001 2019-05-09 stsp free(te_path);
5006 44d03001 2019-05-09 stsp return err;
5009 44d03001 2019-05-09 stsp static const struct got_error *
5010 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5011 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5012 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5014 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5015 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5017 036813ee 2019-05-09 stsp *ctp = NULL;
5019 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5020 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5021 036813ee 2019-05-09 stsp char *ct_name = NULL;
5022 036813ee 2019-05-09 stsp int path_matches;
5024 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5025 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5026 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5027 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
5030 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5031 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5035 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5038 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5040 036813ee 2019-05-09 stsp return err;
5041 036813ee 2019-05-09 stsp if (!path_matches)
5044 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5046 d34b633e 2020-10-19 stsp return err;
5048 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5049 d34b633e 2020-10-19 stsp free(ct_name);
5052 d34b633e 2020-10-19 stsp free(ct_name);
5054 036813ee 2019-05-09 stsp *ctp = ct;
5058 2b496619 2019-07-10 stsp return err;
5061 2b496619 2019-07-10 stsp static const struct got_error *
5062 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5063 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5064 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5065 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5066 2b496619 2019-07-10 stsp struct got_repository *repo)
5068 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5069 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5070 2b496619 2019-07-10 stsp char *subtree_path;
5071 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5072 ba580f68 2020-03-22 stsp int nentries;
5074 2b496619 2019-07-10 stsp *new_tep = NULL;
5076 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5077 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5078 2b496619 2019-07-10 stsp child_path) == -1)
5079 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5081 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5082 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5083 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5084 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5086 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5087 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5088 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5089 2b496619 2019-07-10 stsp goto done;
5091 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5092 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5093 2b496619 2019-07-10 stsp if (err) {
5094 56e0773d 2019-11-28 stsp free(new_te);
5095 2b496619 2019-07-10 stsp goto done;
5097 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5100 2b496619 2019-07-10 stsp free(subtree_path);
5101 2b496619 2019-07-10 stsp if (err == NULL)
5102 2b496619 2019-07-10 stsp *new_tep = new_te;
5103 036813ee 2019-05-09 stsp return err;
5106 036813ee 2019-05-09 stsp static const struct got_error *
5107 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5108 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5109 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5110 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5111 036813ee 2019-05-09 stsp struct got_repository *repo)
5113 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5114 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5115 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5116 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5118 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5119 ba580f68 2020-03-22 stsp *nentries = 0;
5121 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5122 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5123 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5124 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5126 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5127 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5128 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5131 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5132 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5135 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5136 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5138 036813ee 2019-05-09 stsp goto done;
5140 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5141 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5142 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5144 036813ee 2019-05-09 stsp goto done;
5145 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5147 afa376bf 2019-05-09 stsp goto done;
5148 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5149 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5151 2f51b5b3 2019-05-09 stsp goto done;
5152 ba580f68 2020-03-22 stsp (*nentries)++;
5154 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5155 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5156 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5157 2b496619 2019-07-10 stsp == NULL) {
5158 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5159 2b496619 2019-07-10 stsp child_path, path_base_tree,
5160 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5163 2b496619 2019-07-10 stsp goto done;
5164 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5166 2b496619 2019-07-10 stsp goto done;
5167 ba580f68 2020-03-22 stsp (*nentries)++;
5172 2f51b5b3 2019-05-09 stsp if (base_tree) {
5173 56e0773d 2019-11-28 stsp int i, nbase_entries;
5174 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5175 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5176 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5177 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5179 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5180 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5181 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5182 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5184 63c5ca5d 2019-08-24 stsp goto done;
5185 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5187 63c5ca5d 2019-08-24 stsp goto done;
5188 ba580f68 2020-03-22 stsp (*nentries)++;
5192 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5193 44d03001 2019-05-09 stsp int modified;
5194 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5196 036813ee 2019-05-09 stsp goto done;
5197 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5198 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5200 2f51b5b3 2019-05-09 stsp goto done;
5201 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5202 44d03001 2019-05-09 stsp if (modified) {
5203 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5204 ba580f68 2020-03-22 stsp int nsubentries;
5205 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5206 ba580f68 2020-03-22 stsp &nsubentries, te,
5207 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5208 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5210 44d03001 2019-05-09 stsp goto done;
5211 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5212 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5213 ba580f68 2020-03-22 stsp free(new_id);
5216 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5217 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5218 56e0773d 2019-11-28 stsp free(new_id);
5220 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5222 036813ee 2019-05-09 stsp goto done;
5223 ba580f68 2020-03-22 stsp (*nentries)++;
5227 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5228 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5230 f66c734c 2019-09-22 stsp goto done;
5232 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5233 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5234 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5235 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5236 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5237 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5239 2f51b5b3 2019-05-09 stsp goto done;
5240 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5242 2f51b5b3 2019-05-09 stsp goto done;
5243 ba580f68 2020-03-22 stsp (*nentries)++;
5245 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5246 b416585c 2019-05-13 stsp status_arg);
5248 afa376bf 2019-05-09 stsp goto done;
5250 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5251 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5253 2f51b5b3 2019-05-09 stsp goto done;
5254 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5256 2f51b5b3 2019-05-09 stsp goto done;
5257 ba580f68 2020-03-22 stsp (*nentries)++;
5262 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5263 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5265 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
5266 2e1fa222 2020-07-23 stsp return err;
5269 2e1fa222 2020-07-23 stsp static const struct got_error *
5270 ebf99748 2019-05-09 stsp update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5271 72fd46fa 2019-09-06 stsp struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5272 72fd46fa 2019-09-06 stsp int have_staged_files)
5274 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5275 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5277 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5278 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5279 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5281 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5283 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5284 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5285 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5286 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5287 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5288 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5289 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5290 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5291 5f8a88c6 2019-08-03 stsp ct->ondisk_path, ct->staged_blob_id->sha1,
5292 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5293 72fd46fa 2019-09-06 stsp !have_staged_files);
5295 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5296 e75eb4da 2019-05-10 stsp ct->ondisk_path, ct->blob_id->sha1,
5297 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5298 72fd46fa 2019-09-06 stsp !have_staged_files);
5300 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5303 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ct->ondisk_path,
5304 3969253a 2020-03-07 stsp ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5305 3969253a 2020-03-07 stsp if (err) {
5306 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5309 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5310 3969253a 2020-03-07 stsp if (err) {
5311 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5316 d56d26ce 2019-05-10 stsp return err;
5320 d56d26ce 2019-05-10 stsp static const struct got_error *
5321 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5322 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5323 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5324 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5325 735ef5ac 2019-08-03 stsp int ood_errcode)
5327 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5328 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5330 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5331 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5332 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5333 1a36436d 2019-06-10 stsp return NULL;
5335 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5336 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5338 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5339 735ef5ac 2019-08-03 stsp in_repo_path);
5340 9bead371 2019-07-28 stsp if (err) {
5341 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5342 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5343 1a36436d 2019-06-10 stsp goto done;
5344 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5345 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5347 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5348 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5349 735ef5ac 2019-08-03 stsp in_repo_path);
5350 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5351 1a36436d 2019-06-10 stsp goto done;
5352 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5356 1a36436d 2019-06-10 stsp return err;
5359 c4296144 2019-05-09 stsp const struct got_error *
5360 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5361 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5362 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id, struct got_worktree *worktree,
5363 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
5364 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5365 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5366 afa376bf 2019-05-09 stsp struct got_repository *repo)
5368 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5369 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5370 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5371 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5372 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5373 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5374 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5375 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5376 ba580f68 2020-03-22 stsp int nentries;
5377 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5378 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5379 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5381 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5383 de18fc63 2019-05-09 stsp SIMPLEQ_INIT(&parent_ids);
5385 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5387 588edf97 2019-05-10 stsp goto done;
5389 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5391 588edf97 2019-05-10 stsp goto done;
5393 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5394 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5399 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5400 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5404 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5405 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5406 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5407 cf066bf8 2019-05-09 stsp char *ondisk_path;
5409 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5410 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5411 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5414 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5415 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5416 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5419 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5420 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5421 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5422 cf066bf8 2019-05-09 stsp goto done;
5424 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5425 2e1fa222 2020-07-23 stsp free(ondisk_path);
5427 cf066bf8 2019-05-09 stsp goto done;
5430 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5431 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5432 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5434 036813ee 2019-05-09 stsp goto done;
5436 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5438 de18fc63 2019-05-09 stsp goto done;
5439 de18fc63 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5440 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5441 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5442 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
5443 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5444 33ad4cbe 2019-05-12 jcs free(logmsg);
5446 9d40349a 2019-05-09 stsp goto done;
5448 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5449 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5450 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5451 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5452 09f5bd90 2019-05-09 stsp goto done;
5454 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
5455 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5457 09f5bd90 2019-05-09 stsp goto done;
5458 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5460 ebf99748 2019-05-09 stsp goto done;
5461 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5462 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5463 09f5bd90 2019-05-09 stsp goto done;
5465 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
5466 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
5468 f2c16586 2019-05-09 stsp goto done;
5469 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
5471 f2c16586 2019-05-09 stsp goto done;
5473 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5475 09f5bd90 2019-05-09 stsp goto done;
5477 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
5479 ebf99748 2019-05-09 stsp goto done;
5481 588edf97 2019-05-10 stsp if (head_tree)
5482 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
5483 588edf97 2019-05-10 stsp if (head_commit)
5484 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
5485 09f5bd90 2019-05-09 stsp free(head_commit_id2);
5486 2f17228e 2019-05-12 stsp if (head_ref2) {
5487 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
5488 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
5489 2f17228e 2019-05-12 stsp err = unlockerr;
5490 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
5492 39cd0ff6 2019-07-12 stsp return err;
5495 5c1e53bc 2019-07-28 stsp static const struct got_error *
5496 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
5497 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
5499 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
5500 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
5502 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
5503 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
5504 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
5506 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
5507 5c1e53bc 2019-07-28 stsp ct_path++;
5509 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
5510 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
5514 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
5515 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
5517 5c1e53bc 2019-07-28 stsp return NULL;
5520 f0b75401 2019-08-03 stsp static const struct got_error *
5521 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
5523 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
5525 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5526 f0b75401 2019-08-03 stsp *have_staged_files = 1;
5527 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
5530 f0b75401 2019-08-03 stsp return NULL;
5533 5f8a88c6 2019-08-03 stsp static const struct got_error *
5534 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
5535 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
5537 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
5538 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
5540 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5541 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
5543 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5544 5f8a88c6 2019-08-03 stsp if (ie == NULL)
5545 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5546 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5547 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
5548 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
5551 5f8a88c6 2019-08-03 stsp return NULL;
5554 39cd0ff6 2019-07-12 stsp const struct got_error *
5555 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
5556 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
5557 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
5558 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5559 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
5560 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
5562 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5563 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
5564 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
5565 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5566 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5567 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
5568 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
5569 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5570 f0b75401 2019-08-03 stsp int have_staged_files = 0;
5572 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
5574 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5576 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
5578 39cd0ff6 2019-07-12 stsp goto done;
5580 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5582 39cd0ff6 2019-07-12 stsp goto done;
5584 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5586 39cd0ff6 2019-07-12 stsp goto done;
5588 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5590 39cd0ff6 2019-07-12 stsp goto done;
5592 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5593 5f8a88c6 2019-08-03 stsp &have_staged_files);
5594 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
5595 5f8a88c6 2019-08-03 stsp goto done;
5596 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
5597 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
5599 5f8a88c6 2019-08-03 stsp goto done;
5602 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5603 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
5604 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
5605 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
5606 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
5607 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5608 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5609 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5610 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5612 5c1e53bc 2019-07-28 stsp goto done;
5615 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5616 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5617 39cd0ff6 2019-07-12 stsp goto done;
5620 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5621 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
5623 5c1e53bc 2019-07-28 stsp goto done;
5626 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5627 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5628 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5630 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5631 f0b75401 2019-08-03 stsp ct_path++;
5632 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5633 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5634 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5636 b50cabdf 2019-07-12 stsp goto done;
5640 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5641 5c1e53bc 2019-07-28 stsp head_commit_id, worktree, author, committer,
5642 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5644 39cd0ff6 2019-07-12 stsp goto done;
5646 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5647 72fd46fa 2019-09-06 stsp fileindex, have_staged_files);
5648 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5649 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5650 39cd0ff6 2019-07-12 stsp err = sync_err;
5652 39cd0ff6 2019-07-12 stsp if (fileindex)
5653 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5654 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5655 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5656 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5657 39cd0ff6 2019-07-12 stsp err = unlockerr;
5658 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5659 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5660 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5662 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5663 c4296144 2019-05-09 stsp return err;
5666 8656d6c4 2019-05-20 stsp const char *
5667 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5669 8656d6c4 2019-05-20 stsp return ct->path;
5672 8656d6c4 2019-05-20 stsp unsigned int
5673 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
5675 8656d6c4 2019-05-20 stsp return ct->status;
5678 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
5679 818c7501 2019-07-11 stsp struct got_worktree *worktree;
5680 818c7501 2019-07-11 stsp struct got_repository *repo;
5683 818c7501 2019-07-11 stsp static const struct got_error *
5684 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5686 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5687 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
5688 818c7501 2019-07-11 stsp unsigned char status;
5689 818c7501 2019-07-11 stsp struct stat sb;
5690 818c7501 2019-07-11 stsp char *ondisk_path;
5692 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
5693 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5694 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
5695 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
5697 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5699 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
5701 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
5702 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5703 818c7501 2019-07-11 stsp free(ondisk_path);
5705 818c7501 2019-07-11 stsp return err;
5707 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
5708 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
5709 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5710 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5712 818c7501 2019-07-11 stsp return NULL;
5715 818c7501 2019-07-11 stsp const struct got_error *
5716 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5717 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5718 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
5719 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5721 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5722 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5723 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
5724 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
5725 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
5726 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5727 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
5729 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5730 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5731 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5733 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
5735 818c7501 2019-07-11 stsp return err;
5737 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5739 818c7501 2019-07-11 stsp goto done;
5741 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
5742 818c7501 2019-07-11 stsp ok_arg.repo = repo;
5743 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5746 818c7501 2019-07-11 stsp goto done;
5748 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5750 818c7501 2019-07-11 stsp goto done;
5752 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5754 818c7501 2019-07-11 stsp goto done;
5756 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5758 818c7501 2019-07-11 stsp goto done;
5760 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5763 e51d7b55 2020-01-04 stsp goto done;
5765 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5767 818c7501 2019-07-11 stsp goto done;
5768 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5769 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5770 e51d7b55 2020-01-04 stsp goto done;
5773 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
5774 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
5776 818c7501 2019-07-11 stsp goto done;
5777 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
5779 818c7501 2019-07-11 stsp goto done;
5781 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
5783 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5785 818c7501 2019-07-11 stsp goto done;
5787 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
5789 818c7501 2019-07-11 stsp goto done;
5791 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
5792 818c7501 2019-07-11 stsp worktree->base_commit_id);
5794 818c7501 2019-07-11 stsp goto done;
5795 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
5797 818c7501 2019-07-11 stsp goto done;
5799 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
5801 818c7501 2019-07-11 stsp goto done;
5803 818c7501 2019-07-11 stsp free(fileindex_path);
5804 818c7501 2019-07-11 stsp free(tmp_branch_name);
5805 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
5806 818c7501 2019-07-11 stsp free(branch_ref_name);
5807 818c7501 2019-07-11 stsp if (branch_ref)
5808 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5809 818c7501 2019-07-11 stsp if (wt_branch)
5810 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
5811 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
5812 818c7501 2019-07-11 stsp if (err) {
5813 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
5814 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
5815 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5817 818c7501 2019-07-11 stsp if (*tmp_branch) {
5818 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5819 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5821 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5822 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5823 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5825 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
5827 818c7501 2019-07-11 stsp return err;
5830 818c7501 2019-07-11 stsp const struct got_error *
5831 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
5832 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5833 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
5834 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
5836 818c7501 2019-07-11 stsp const struct got_error *err;
5837 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5838 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5839 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5840 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
5841 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
5843 818c7501 2019-07-11 stsp *commit_id = NULL;
5844 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
5845 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
5846 3e3a69f1 2019-07-25 stsp *branch = NULL;
5847 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5849 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
5851 3e3a69f1 2019-07-25 stsp return err;
5853 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5855 f032f1f7 2019-08-04 stsp goto done;
5857 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5858 f032f1f7 2019-08-04 stsp &have_staged_files);
5859 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
5860 f032f1f7 2019-08-04 stsp goto done;
5861 f032f1f7 2019-08-04 stsp if (have_staged_files) {
5862 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
5863 3e3a69f1 2019-07-25 stsp goto done;
5866 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5868 f032f1f7 2019-08-04 stsp goto done;
5870 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5872 818c7501 2019-07-11 stsp goto done;
5874 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5876 818c7501 2019-07-11 stsp goto done;
5878 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5880 818c7501 2019-07-11 stsp goto done;
5882 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5884 818c7501 2019-07-11 stsp goto done;
5886 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
5887 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
5889 818c7501 2019-07-11 stsp goto done;
5891 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5893 818c7501 2019-07-11 stsp goto done;
5895 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
5897 818c7501 2019-07-11 stsp goto done;
5899 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
5900 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
5902 818c7501 2019-07-11 stsp goto done;
5904 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5906 818c7501 2019-07-11 stsp goto done;
5908 818c7501 2019-07-11 stsp free(commit_ref_name);
5909 818c7501 2019-07-11 stsp free(branch_ref_name);
5910 3e3a69f1 2019-07-25 stsp free(fileindex_path);
5911 818c7501 2019-07-11 stsp if (commit_ref)
5912 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
5913 818c7501 2019-07-11 stsp if (branch_ref)
5914 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5915 818c7501 2019-07-11 stsp if (err) {
5916 818c7501 2019-07-11 stsp free(*commit_id);
5917 818c7501 2019-07-11 stsp *commit_id = NULL;
5918 818c7501 2019-07-11 stsp if (*tmp_branch) {
5919 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5920 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5922 818c7501 2019-07-11 stsp if (*new_base_branch) {
5923 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
5924 818c7501 2019-07-11 stsp *new_base_branch = NULL;
5926 818c7501 2019-07-11 stsp if (*branch) {
5927 818c7501 2019-07-11 stsp got_ref_close(*branch);
5928 818c7501 2019-07-11 stsp *branch = NULL;
5930 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5931 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5932 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5934 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
5936 818c7501 2019-07-11 stsp return err;
5939 818c7501 2019-07-11 stsp const struct got_error *
5940 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5942 818c7501 2019-07-11 stsp const struct got_error *err;
5943 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
5945 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5947 818c7501 2019-07-11 stsp return err;
5949 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5950 818c7501 2019-07-11 stsp free(tmp_branch_name);
5951 818c7501 2019-07-11 stsp return NULL;
5954 818c7501 2019-07-11 stsp static const struct got_error *
5955 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5956 818c7501 2019-07-11 stsp char **logmsg, void *arg)
5958 0ebf8283 2019-07-24 stsp *logmsg = arg;
5959 818c7501 2019-07-11 stsp return NULL;
5962 818c7501 2019-07-11 stsp static const struct got_error *
5963 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5964 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
5965 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5966 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5968 818c7501 2019-07-11 stsp return NULL;
5971 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
5972 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
5973 01757395 2019-07-12 stsp void *progress_arg;
5974 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
5977 01757395 2019-07-12 stsp static const struct got_error *
5978 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
5980 01757395 2019-07-12 stsp const struct got_error *err;
5981 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
5983 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
5985 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
5987 01757395 2019-07-12 stsp return err;
5989 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
5990 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
5991 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
5992 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
5993 01757395 2019-07-12 stsp return NULL;
5995 01757395 2019-07-12 stsp p = strdup(path);
5996 01757395 2019-07-12 stsp if (p == NULL)
5997 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
5999 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6000 01757395 2019-07-12 stsp if (err || new == NULL)
6002 01757395 2019-07-12 stsp return err;
6006 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6008 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6010 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
6011 01757395 2019-07-12 stsp free((char *)pe->path);
6013 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
6016 0ebf8283 2019-07-24 stsp static const struct got_error *
6017 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6018 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6020 818c7501 2019-07-11 stsp const struct got_error *err;
6021 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6023 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6024 818c7501 2019-07-11 stsp if (err) {
6025 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6026 818c7501 2019-07-11 stsp goto done;
6027 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6029 818c7501 2019-07-11 stsp goto done;
6030 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6032 818c7501 2019-07-11 stsp goto done;
6033 de05890f 2020-03-05 stsp } else if (is_rebase) {
6034 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6037 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6039 818c7501 2019-07-11 stsp goto done;
6040 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6041 818c7501 2019-07-11 stsp free(stored_id);
6042 818c7501 2019-07-11 stsp if (cmp != 0) {
6043 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6044 818c7501 2019-07-11 stsp goto done;
6048 0ebf8283 2019-07-24 stsp if (commit_ref)
6049 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6050 0ebf8283 2019-07-24 stsp return err;
6053 0ebf8283 2019-07-24 stsp static const struct got_error *
6054 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6055 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6056 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6057 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6058 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6059 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6061 0ebf8283 2019-07-24 stsp const struct got_error *err;
6062 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6063 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6064 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6066 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6068 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6070 0ebf8283 2019-07-24 stsp return err;
6072 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6073 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6074 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6075 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6076 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6077 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6078 818c7501 2019-07-11 stsp if (commit_ref)
6079 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6080 818c7501 2019-07-11 stsp return err;
6083 818c7501 2019-07-11 stsp const struct got_error *
6084 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6085 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6086 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6087 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6088 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6089 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6091 0ebf8283 2019-07-24 stsp const struct got_error *err;
6092 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6094 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6096 0ebf8283 2019-07-24 stsp return err;
6098 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6100 0ebf8283 2019-07-24 stsp goto done;
6102 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6103 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6104 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6106 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6107 0ebf8283 2019-07-24 stsp return err;
6110 0ebf8283 2019-07-24 stsp const struct got_error *
6111 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6112 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6113 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6114 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6115 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6116 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6118 0ebf8283 2019-07-24 stsp const struct got_error *err;
6119 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6121 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6123 0ebf8283 2019-07-24 stsp return err;
6125 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6127 0ebf8283 2019-07-24 stsp goto done;
6129 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6130 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6131 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6133 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6134 0ebf8283 2019-07-24 stsp return err;
6137 0ebf8283 2019-07-24 stsp static const struct got_error *
6138 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6139 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6140 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6141 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6142 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
6144 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6145 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6146 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6147 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6148 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6149 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6150 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6152 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6153 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6155 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6157 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6159 3e3a69f1 2019-07-25 stsp return err;
6161 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6162 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6163 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6164 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6166 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6167 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6168 01757395 2019-07-12 stsp * TODO: Ideally, merged_paths would contain a list of commitables
6169 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6171 01757395 2019-07-12 stsp if (merged_paths) {
6172 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6173 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6174 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6175 f2a9dc41 2019-12-13 tracey repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6178 01757395 2019-07-12 stsp goto done;
6181 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6182 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6184 01757395 2019-07-12 stsp goto done;
6187 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6188 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6189 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6191 a0e95631 2019-07-12 stsp goto done;
6192 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6193 a0e95631 2019-07-12 stsp goto done;
6196 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6198 edd02c5e 2019-07-11 stsp goto done;
6200 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6202 818c7501 2019-07-11 stsp goto done;
6204 5943eee2 2019-08-13 stsp if (new_logmsg) {
6205 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6206 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6207 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6208 5943eee2 2019-08-13 stsp goto done;
6211 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6213 5943eee2 2019-08-13 stsp goto done;
6216 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6217 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6218 5c1e53bc 2019-07-28 stsp worktree, got_object_commit_get_author(orig_commit),
6219 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
6220 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6222 a0e95631 2019-07-12 stsp goto done;
6224 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6226 a0e95631 2019-07-12 stsp goto done;
6228 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6230 a0e95631 2019-07-12 stsp goto done;
6232 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6233 72fd46fa 2019-09-06 stsp fileindex, 0);
6234 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6235 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6236 a0e95631 2019-07-12 stsp err = sync_err;
6238 a0e95631 2019-07-12 stsp free(fileindex_path);
6239 a0e95631 2019-07-12 stsp free(head_commit_id);
6240 a0e95631 2019-07-12 stsp if (head_ref)
6241 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6242 818c7501 2019-07-11 stsp if (err) {
6243 818c7501 2019-07-11 stsp free(*new_commit_id);
6244 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6246 818c7501 2019-07-11 stsp return err;
6249 818c7501 2019-07-11 stsp const struct got_error *
6250 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6251 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6252 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6253 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6254 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6256 0ebf8283 2019-07-24 stsp const struct got_error *err;
6257 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6258 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6259 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6261 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6263 0ebf8283 2019-07-24 stsp return err;
6265 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6267 0ebf8283 2019-07-24 stsp goto done;
6268 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6270 0ebf8283 2019-07-24 stsp goto done;
6271 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6272 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6273 0ebf8283 2019-07-24 stsp goto done;
6276 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6277 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6279 0ebf8283 2019-07-24 stsp if (commit_ref)
6280 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6281 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6282 0ebf8283 2019-07-24 stsp free(commit_id);
6283 0ebf8283 2019-07-24 stsp return err;
6286 0ebf8283 2019-07-24 stsp const struct got_error *
6287 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6288 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6289 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6290 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6291 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6292 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6294 0ebf8283 2019-07-24 stsp const struct got_error *err;
6295 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6296 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6298 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6300 0ebf8283 2019-07-24 stsp return err;
6302 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6304 0ebf8283 2019-07-24 stsp goto done;
6306 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6307 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6309 0ebf8283 2019-07-24 stsp if (commit_ref)
6310 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6311 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6312 0ebf8283 2019-07-24 stsp return err;
6315 0ebf8283 2019-07-24 stsp const struct got_error *
6316 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6317 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6319 3e3a69f1 2019-07-25 stsp if (fileindex)
6320 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6321 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6324 69844fba 2019-07-11 stsp static const struct got_error *
6325 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6327 69844fba 2019-07-11 stsp const struct got_error *err;
6328 69844fba 2019-07-11 stsp struct got_reference *ref;
6330 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6331 69844fba 2019-07-11 stsp if (err) {
6332 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6333 69844fba 2019-07-11 stsp return NULL;
6334 69844fba 2019-07-11 stsp return err;
6337 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6338 69844fba 2019-07-11 stsp got_ref_close(ref);
6339 69844fba 2019-07-11 stsp return err;
6342 69844fba 2019-07-11 stsp static const struct got_error *
6343 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6345 69844fba 2019-07-11 stsp const struct got_error *err;
6346 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6347 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6349 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6351 69844fba 2019-07-11 stsp goto done;
6352 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6354 69844fba 2019-07-11 stsp goto done;
6356 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6358 69844fba 2019-07-11 stsp goto done;
6359 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6361 69844fba 2019-07-11 stsp goto done;
6363 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6365 69844fba 2019-07-11 stsp goto done;
6366 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6368 69844fba 2019-07-11 stsp goto done;
6370 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6372 69844fba 2019-07-11 stsp goto done;
6373 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6375 69844fba 2019-07-11 stsp goto done;
6378 69844fba 2019-07-11 stsp free(tmp_branch_name);
6379 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6380 69844fba 2019-07-11 stsp free(branch_ref_name);
6381 69844fba 2019-07-11 stsp free(commit_ref_name);
6382 69844fba 2019-07-11 stsp return err;
6385 818c7501 2019-07-11 stsp const struct got_error *
6386 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
6387 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6388 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6389 818c7501 2019-07-11 stsp struct got_repository *repo)
6391 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
6392 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
6394 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6396 818c7501 2019-07-11 stsp return err;
6398 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6400 818c7501 2019-07-11 stsp goto done;
6402 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
6404 818c7501 2019-07-11 stsp goto done;
6406 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
6408 818c7501 2019-07-11 stsp goto done;
6410 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
6412 3e3a69f1 2019-07-25 stsp if (fileindex)
6413 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6414 818c7501 2019-07-11 stsp free(new_head_commit_id);
6415 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6416 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6417 818c7501 2019-07-11 stsp err = unlockerr;
6418 818c7501 2019-07-11 stsp return err;
6421 818c7501 2019-07-11 stsp const struct got_error *
6422 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
6423 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6424 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
6425 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6427 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
6428 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
6429 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
6430 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6431 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6432 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
6434 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6436 818c7501 2019-07-11 stsp return err;
6438 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
6439 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
6441 818c7501 2019-07-11 stsp goto done;
6443 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
6445 818c7501 2019-07-11 stsp goto done;
6448 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
6449 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
6450 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
6452 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
6454 818c7501 2019-07-11 stsp goto done;
6456 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6458 818c7501 2019-07-11 stsp goto done;
6460 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
6461 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
6463 ca355955 2019-07-12 stsp goto done;
6465 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
6467 ca355955 2019-07-12 stsp goto done;
6469 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6471 818c7501 2019-07-11 stsp goto done;
6473 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6474 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6475 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6476 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6477 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6478 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6479 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6480 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6481 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6483 1f1abb7e 2019-08-08 stsp goto sync;
6485 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6486 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
6488 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6489 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
6490 ca355955 2019-07-12 stsp err = sync_err;
6492 818c7501 2019-07-11 stsp got_ref_close(resolved);
6493 a3a2faf2 2019-07-12 stsp free(tree_id);
6494 818c7501 2019-07-11 stsp free(commit_id);
6495 0ebf8283 2019-07-24 stsp if (fileindex)
6496 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
6497 0ebf8283 2019-07-24 stsp free(fileindex_path);
6499 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6500 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6501 0ebf8283 2019-07-24 stsp err = unlockerr;
6502 0ebf8283 2019-07-24 stsp return err;
6505 0ebf8283 2019-07-24 stsp const struct got_error *
6506 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6507 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6508 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
6509 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6511 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6512 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6513 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
6514 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
6515 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6516 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
6517 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
6518 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6520 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6521 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6522 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6523 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6525 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6527 0ebf8283 2019-07-24 stsp return err;
6529 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6531 0ebf8283 2019-07-24 stsp goto done;
6533 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
6534 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
6535 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6538 0ebf8283 2019-07-24 stsp goto done;
6540 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6542 0ebf8283 2019-07-24 stsp goto done;
6544 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6546 0ebf8283 2019-07-24 stsp goto done;
6548 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6549 0ebf8283 2019-07-24 stsp worktree);
6551 0ebf8283 2019-07-24 stsp goto done;
6553 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6556 0ebf8283 2019-07-24 stsp goto done;
6558 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6560 0ebf8283 2019-07-24 stsp goto done;
6562 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
6564 0ebf8283 2019-07-24 stsp goto done;
6566 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6567 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6569 0ebf8283 2019-07-24 stsp goto done;
6570 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
6572 0ebf8283 2019-07-24 stsp goto done;
6573 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6574 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
6575 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
6576 0ebf8283 2019-07-24 stsp goto done;
6579 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6580 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6582 0ebf8283 2019-07-24 stsp goto done;
6583 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
6585 0ebf8283 2019-07-24 stsp goto done;
6587 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6589 0ebf8283 2019-07-24 stsp goto done;
6591 0ebf8283 2019-07-24 stsp free(fileindex_path);
6592 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6593 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6594 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6595 0ebf8283 2019-07-24 stsp if (wt_branch)
6596 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
6597 0ebf8283 2019-07-24 stsp if (err) {
6598 0ebf8283 2019-07-24 stsp if (*branch_ref) {
6599 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
6600 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6602 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6603 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6604 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6606 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6607 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6608 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6609 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6611 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
6613 0ebf8283 2019-07-24 stsp return err;
6616 0ebf8283 2019-07-24 stsp const struct got_error *
6617 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
6618 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6620 3e3a69f1 2019-07-25 stsp if (fileindex)
6621 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6622 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
6625 0ebf8283 2019-07-24 stsp const struct got_error *
6626 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
6627 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
6629 0ebf8283 2019-07-24 stsp const struct got_error *err;
6630 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6632 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6634 0ebf8283 2019-07-24 stsp return err;
6636 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6637 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6638 0ebf8283 2019-07-24 stsp return NULL;
6641 0ebf8283 2019-07-24 stsp const struct got_error *
6642 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
6643 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
6644 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6645 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6647 0ebf8283 2019-07-24 stsp const struct got_error *err;
6648 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6649 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6650 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6651 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6652 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6653 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6655 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6656 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6657 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6658 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6660 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6662 3e3a69f1 2019-07-25 stsp return err;
6664 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6666 f032f1f7 2019-08-04 stsp goto done;
6668 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6669 f032f1f7 2019-08-04 stsp &have_staged_files);
6670 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6671 f032f1f7 2019-08-04 stsp goto done;
6672 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6673 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6674 3e3a69f1 2019-07-25 stsp goto done;
6677 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6679 f032f1f7 2019-08-04 stsp goto done;
6681 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6683 0ebf8283 2019-07-24 stsp goto done;
6685 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6687 0ebf8283 2019-07-24 stsp goto done;
6689 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6690 0ebf8283 2019-07-24 stsp worktree);
6692 0ebf8283 2019-07-24 stsp goto done;
6694 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6696 0ebf8283 2019-07-24 stsp goto done;
6698 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6700 0ebf8283 2019-07-24 stsp goto done;
6701 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6703 0ebf8283 2019-07-24 stsp goto done;
6705 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6707 0ebf8283 2019-07-24 stsp goto done;
6708 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6710 0ebf8283 2019-07-24 stsp goto done;
6712 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6714 0ebf8283 2019-07-24 stsp goto done;
6716 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6717 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6718 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6719 0ebf8283 2019-07-24 stsp if (commit_ref)
6720 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6721 0ebf8283 2019-07-24 stsp if (base_commit_ref)
6722 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
6723 0ebf8283 2019-07-24 stsp if (err) {
6724 0ebf8283 2019-07-24 stsp free(*commit_id);
6725 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6726 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6727 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6728 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6729 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6730 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6732 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6733 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6734 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6736 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
6738 0ebf8283 2019-07-24 stsp return err;
6741 0ebf8283 2019-07-24 stsp static const struct got_error *
6742 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6744 0ebf8283 2019-07-24 stsp const struct got_error *err;
6745 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6746 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6748 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6750 0ebf8283 2019-07-24 stsp goto done;
6751 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
6753 0ebf8283 2019-07-24 stsp goto done;
6755 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6756 0ebf8283 2019-07-24 stsp worktree);
6758 0ebf8283 2019-07-24 stsp goto done;
6759 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
6761 0ebf8283 2019-07-24 stsp goto done;
6763 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6765 0ebf8283 2019-07-24 stsp goto done;
6766 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
6768 0ebf8283 2019-07-24 stsp goto done;
6770 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6772 0ebf8283 2019-07-24 stsp goto done;
6773 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
6775 0ebf8283 2019-07-24 stsp goto done;
6777 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6778 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6779 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6780 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6781 0ebf8283 2019-07-24 stsp return err;
6784 0ebf8283 2019-07-24 stsp const struct got_error *
6785 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
6786 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6787 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
6788 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6790 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
6791 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6792 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6793 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
6794 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6796 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6798 0ebf8283 2019-07-24 stsp return err;
6800 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
6801 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
6803 0ebf8283 2019-07-24 stsp goto done;
6805 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
6807 0ebf8283 2019-07-24 stsp goto done;
6809 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6811 0ebf8283 2019-07-24 stsp goto done;
6813 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6814 0ebf8283 2019-07-24 stsp worktree->path_prefix);
6816 0ebf8283 2019-07-24 stsp goto done;
6818 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
6820 0ebf8283 2019-07-24 stsp goto done;
6822 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6824 0ebf8283 2019-07-24 stsp goto done;
6826 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6827 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6828 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6829 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6830 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6831 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6832 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6833 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
6834 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6836 1f1abb7e 2019-08-08 stsp goto sync;
6838 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6839 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
6841 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6842 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
6843 0ebf8283 2019-07-24 stsp err = sync_err;
6845 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
6846 0ebf8283 2019-07-24 stsp free(tree_id);
6847 818c7501 2019-07-11 stsp free(fileindex_path);
6849 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6850 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6851 818c7501 2019-07-11 stsp err = unlockerr;
6852 818c7501 2019-07-11 stsp return err;
6855 0ebf8283 2019-07-24 stsp const struct got_error *
6856 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
6857 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6858 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
6860 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr;
6861 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
6862 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6864 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6866 0ebf8283 2019-07-24 stsp return err;
6868 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
6869 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
6871 0ebf8283 2019-07-24 stsp goto done;
6873 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
6875 0ebf8283 2019-07-24 stsp goto done;
6877 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
6879 0ebf8283 2019-07-24 stsp goto done;
6881 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
6883 0ebf8283 2019-07-24 stsp goto done;
6885 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
6887 3e3a69f1 2019-07-25 stsp if (fileindex)
6888 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6889 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
6890 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6891 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6892 0ebf8283 2019-07-24 stsp err = unlockerr;
6893 0ebf8283 2019-07-24 stsp return err;
6896 0ebf8283 2019-07-24 stsp const struct got_error *
6897 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6898 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6900 0ebf8283 2019-07-24 stsp const struct got_error *err;
6901 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6903 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6905 0ebf8283 2019-07-24 stsp return err;
6907 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6909 0ebf8283 2019-07-24 stsp goto done;
6911 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
6913 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6914 2822a352 2019-10-15 stsp return err;
6917 2822a352 2019-10-15 stsp const struct got_error *
6918 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6919 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6920 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
6921 2822a352 2019-10-15 stsp struct got_repository *repo)
6923 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
6924 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
6925 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
6927 2822a352 2019-10-15 stsp *fileindex = NULL;
6928 2822a352 2019-10-15 stsp *branch_ref = NULL;
6929 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
6931 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
6933 2822a352 2019-10-15 stsp return err;
6935 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6936 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
6937 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
6938 2822a352 2019-10-15 stsp "update -b or different branch name required");
6939 2822a352 2019-10-15 stsp goto done;
6942 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6944 2822a352 2019-10-15 stsp goto done;
6946 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
6947 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
6948 2822a352 2019-10-15 stsp ok_arg.repo = repo;
6949 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6952 2822a352 2019-10-15 stsp goto done;
6954 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
6956 2822a352 2019-10-15 stsp goto done;
6958 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
6959 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
6961 2822a352 2019-10-15 stsp if (err) {
6962 2822a352 2019-10-15 stsp if (*branch_ref) {
6963 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
6964 2822a352 2019-10-15 stsp *branch_ref = NULL;
6966 2822a352 2019-10-15 stsp if (*base_branch_ref) {
6967 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
6968 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
6970 2822a352 2019-10-15 stsp if (*fileindex) {
6971 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
6972 2822a352 2019-10-15 stsp *fileindex = NULL;
6974 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
6976 0cb83759 2019-08-03 stsp return err;
6979 2822a352 2019-10-15 stsp const struct got_error *
6980 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
6981 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6982 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6983 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6984 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6986 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
6987 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
6988 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
6990 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
6992 2822a352 2019-10-15 stsp goto done;
6994 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
6996 2822a352 2019-10-15 stsp goto done;
6998 2822a352 2019-10-15 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
6999 2822a352 2019-10-15 stsp worktree->path_prefix);
7001 2822a352 2019-10-15 stsp goto done;
7003 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7005 2822a352 2019-10-15 stsp goto done;
7007 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7008 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7010 2822a352 2019-10-15 stsp goto sync;
7012 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7014 2822a352 2019-10-15 stsp goto sync;
7016 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7018 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7019 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7020 2822a352 2019-10-15 stsp err = sync_err;
7023 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7024 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7025 2822a352 2019-10-15 stsp err = unlockerr;
7026 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7028 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7029 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7030 2822a352 2019-10-15 stsp err = unlockerr;
7031 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7033 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7034 2822a352 2019-10-15 stsp free(fileindex_path);
7035 2822a352 2019-10-15 stsp free(tree_id);
7037 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7038 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7039 2822a352 2019-10-15 stsp err = unlockerr;
7040 2822a352 2019-10-15 stsp return err;
7043 2822a352 2019-10-15 stsp const struct got_error *
7044 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7045 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7046 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7048 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7050 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7052 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7054 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7055 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7056 8b692cd0 2019-10-21 stsp err = unlockerr;
7057 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7059 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7060 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7061 8b692cd0 2019-10-21 stsp err = unlockerr;
7062 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7064 8b692cd0 2019-10-21 stsp return err;
7067 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
7068 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
7069 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7070 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7071 2db2652d 2019-08-07 stsp struct got_repository *repo;
7072 2db2652d 2019-08-07 stsp int have_changes;
7075 2db2652d 2019-08-07 stsp const struct got_error *
7076 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
7077 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7078 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7079 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7081 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
7082 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
7083 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
7084 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
7085 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
7086 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
7088 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
7089 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
7090 8b13ce36 2019-08-08 stsp return NULL;
7091 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
7092 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
7094 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7095 735ef5ac 2019-08-03 stsp if (ie == NULL)
7096 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7098 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7099 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7100 735ef5ac 2019-08-03 stsp relpath) == -1)
7101 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
7103 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
7104 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
7105 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7106 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
7109 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
7110 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7111 3aa5969e 2019-08-06 stsp goto done;
7112 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
7113 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
7114 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
7115 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7116 735ef5ac 2019-08-03 stsp goto done;
7119 2db2652d 2019-08-07 stsp a->have_changes = 1;
7121 735ef5ac 2019-08-03 stsp p = in_repo_path;
7122 735ef5ac 2019-08-03 stsp while (p[0] == '/')
7124 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
7125 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
7126 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
7128 735ef5ac 2019-08-03 stsp free(in_repo_path);
7129 dc424a06 2019-08-07 stsp return err;
7132 2db2652d 2019-08-07 stsp struct stage_path_arg {
7133 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7134 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7135 2db2652d 2019-08-07 stsp struct got_repository *repo;
7136 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
7137 2db2652d 2019-08-07 stsp void *status_arg;
7138 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
7139 2db2652d 2019-08-07 stsp void *patch_arg;
7140 7b5dc508 2019-10-28 stsp int staged_something;
7141 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
7144 2db2652d 2019-08-07 stsp static const struct got_error *
7145 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
7146 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7147 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7148 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7150 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
7151 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
7152 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
7153 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
7154 0cb83759 2019-08-03 stsp uint32_t stage;
7155 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
7156 0aeb8099 2020-07-23 stsp struct stat sb;
7158 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
7159 8b13ce36 2019-08-08 stsp return NULL;
7161 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7162 d3e7c587 2019-08-03 stsp if (ie == NULL)
7163 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7165 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7166 2db2652d 2019-08-07 stsp relpath)== -1)
7167 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
7169 0cb83759 2019-08-03 stsp switch (status) {
7170 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
7171 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
7172 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
7173 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
7174 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
7177 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7178 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
7179 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7180 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7181 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
7184 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
7187 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
7188 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
7189 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
7190 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
7191 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
7195 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
7196 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
7199 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7200 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7201 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7202 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
7204 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
7205 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7206 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
7207 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
7208 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
7209 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
7210 35213c7c 2020-07-23 stsp ssize_t target_len;
7211 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
7212 35213c7c 2020-07-23 stsp sizeof(target_path));
7213 35213c7c 2020-07-23 stsp if (target_len == -1) {
7214 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
7215 35213c7c 2020-07-23 stsp ondisk_path);
7218 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
7219 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
7220 35213c7c 2020-07-23 stsp a->worktree->root_path);
7223 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
7224 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
7225 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
7229 35213c7c 2020-07-23 stsp if (is_bad_symlink)
7230 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7231 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
7233 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7234 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
7236 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7237 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
7239 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7240 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7242 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7243 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
7244 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
7246 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
7247 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
7249 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7250 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7251 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
7252 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
7255 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7257 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7258 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7262 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
7263 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7264 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7265 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7267 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7268 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7271 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
7273 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
7274 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7276 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
7277 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
7280 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7284 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
7285 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
7286 dc424a06 2019-08-07 stsp free(path_content);
7287 2db2652d 2019-08-07 stsp free(ondisk_path);
7288 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
7289 0ebf8283 2019-07-24 stsp return err;
7292 0cb83759 2019-08-03 stsp const struct got_error *
7293 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
7294 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
7295 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
7296 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7297 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
7299 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7300 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
7301 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7302 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
7303 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
7304 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
7305 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
7306 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
7308 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7310 0cb83759 2019-08-03 stsp return err;
7312 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
7313 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
7315 735ef5ac 2019-08-03 stsp goto done;
7316 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7318 735ef5ac 2019-08-03 stsp goto done;
7319 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7321 0cb83759 2019-08-03 stsp goto done;
7323 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
7324 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
7325 2db2652d 2019-08-07 stsp oka.worktree = worktree;
7326 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
7327 2db2652d 2019-08-07 stsp oka.repo = repo;
7328 2db2652d 2019-08-07 stsp oka.have_changes = 0;
7329 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7330 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7331 f2a9dc41 2019-12-13 tracey check_stage_ok, &oka, NULL, NULL, 0, 0);
7333 735ef5ac 2019-08-03 stsp goto done;
7335 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
7336 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7337 2db2652d 2019-08-07 stsp goto done;
7340 2db2652d 2019-08-07 stsp spa.worktree = worktree;
7341 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
7342 2db2652d 2019-08-07 stsp spa.repo = repo;
7343 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
7344 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
7345 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
7346 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
7347 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
7348 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
7349 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7350 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7351 f2a9dc41 2019-12-13 tracey stage_path, &spa, NULL, NULL, 0, 0);
7353 2db2652d 2019-08-07 stsp goto done;
7355 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
7356 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7357 7b5dc508 2019-10-28 stsp goto done;
7360 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7361 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
7362 0cb83759 2019-08-03 stsp err = sync_err;
7364 735ef5ac 2019-08-03 stsp if (head_ref)
7365 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
7366 735ef5ac 2019-08-03 stsp free(head_commit_id);
7367 0cb83759 2019-08-03 stsp free(fileindex_path);
7368 0cb83759 2019-08-03 stsp if (fileindex)
7369 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
7370 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7371 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
7372 0cb83759 2019-08-03 stsp err = unlockerr;
7373 0cb83759 2019-08-03 stsp return err;
7376 ad493afc 2019-08-03 stsp struct unstage_path_arg {
7377 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
7378 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
7379 ad493afc 2019-08-03 stsp struct got_repository *repo;
7380 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
7381 ad493afc 2019-08-03 stsp void *progress_arg;
7382 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
7383 2e1f37b0 2019-08-08 stsp void *patch_arg;
7386 2e1f37b0 2019-08-08 stsp static const struct got_error *
7387 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
7388 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
7389 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
7390 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
7391 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
7393 2e1f37b0 2019-08-08 stsp const struct got_error *err;
7394 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
7395 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7396 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7397 2e1f37b0 2019-08-08 stsp struct stat sb1, sb2;
7398 2e1f37b0 2019-08-08 stsp struct got_diff_changes *changes = NULL;
7399 2e1f37b0 2019-08-08 stsp struct got_diff_state *ds = NULL;
7400 2e1f37b0 2019-08-08 stsp struct got_diff_args *args = NULL;
7401 2e1f37b0 2019-08-08 stsp struct got_diff_change *change;
7402 2e1f37b0 2019-08-08 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7403 2e1f37b0 2019-08-08 stsp int have_content = 0, have_rejected_content = 0;
7405 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7406 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7408 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
7410 2e1f37b0 2019-08-08 stsp return err;
7411 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7413 2e1f37b0 2019-08-08 stsp goto done;
7415 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7417 2e1f37b0 2019-08-08 stsp goto done;
7419 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7421 2e1f37b0 2019-08-08 stsp goto done;
7423 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7425 2e1f37b0 2019-08-08 stsp goto done;
7427 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7429 2e1f37b0 2019-08-08 stsp goto done;
7431 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7433 2e1f37b0 2019-08-08 stsp goto done;
7435 2e1f37b0 2019-08-08 stsp if (stat(path1, &sb1) == -1) {
7436 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path1);
7437 2e1f37b0 2019-08-08 stsp goto done;
7440 2e1f37b0 2019-08-08 stsp if (stat(path2, &sb2) == -1) {
7441 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path2);
7442 2e1f37b0 2019-08-08 stsp goto done;
7445 2e1f37b0 2019-08-08 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
7446 2e1f37b0 2019-08-08 stsp f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7448 2e1f37b0 2019-08-08 stsp goto done;
7450 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
7451 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
7453 2e1f37b0 2019-08-08 stsp goto done;
7454 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
7455 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
7457 2e1f37b0 2019-08-08 stsp goto done;
7459 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
7460 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
7461 2e1f37b0 2019-08-08 stsp goto done;
7463 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
7464 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
7465 2e1f37b0 2019-08-08 stsp goto done;
7467 2e1f37b0 2019-08-08 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7468 2e1f37b0 2019-08-08 stsp int choice;
7469 2e1f37b0 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
7470 2e1f37b0 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
7471 2e1f37b0 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
7472 2e1f37b0 2019-08-08 stsp outfile, rejectfile, patch_cb, patch_arg);
7474 2e1f37b0 2019-08-08 stsp goto done;
7475 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
7476 2e1f37b0 2019-08-08 stsp have_content = 1;
7478 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
7479 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
7482 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
7483 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7484 f1e81a05 2019-08-10 stsp outfile, rejectfile);
7486 2e1f37b0 2019-08-08 stsp free(label1);
7488 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
7489 2e1f37b0 2019-08-08 stsp if (staged_blob)
7490 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
7491 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
7492 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
7493 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
7494 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
7495 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
7496 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
7497 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7498 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
7499 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
7500 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
7501 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
7502 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
7503 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
7504 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
7505 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
7506 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7507 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
7508 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
7509 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7511 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
7512 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
7513 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
7514 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7515 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
7516 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
7517 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7519 2e1f37b0 2019-08-08 stsp free(args);
7521 2e1f37b0 2019-08-08 stsp got_diff_state_free(ds);
7524 2e1f37b0 2019-08-08 stsp if (changes)
7525 2e1f37b0 2019-08-08 stsp got_diff_free_changes(changes);
7526 2e1f37b0 2019-08-08 stsp free(path1);
7527 2e1f37b0 2019-08-08 stsp free(path2);
7528 fda8017d 2020-07-23 stsp return err;
7531 fda8017d 2020-07-23 stsp static const struct got_error *
7532 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
7533 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
7534 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7535 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
7536 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
7537 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7538 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7540 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
7541 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
7542 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
7543 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
7544 fda8017d 2020-07-23 stsp FILE *f = NULL;
7545 fda8017d 2020-07-23 stsp struct stat sb;
7547 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
7548 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
7549 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
7551 fda8017d 2020-07-23 stsp return err;
7553 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
7554 fda8017d 2020-07-23 stsp return NULL;
7556 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
7557 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
7558 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
7560 fda8017d 2020-07-23 stsp goto done;
7563 fda8017d 2020-07-23 stsp f = fopen(path_unstaged_content, "r");
7564 fda8017d 2020-07-23 stsp if (f == NULL) {
7565 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
7566 fda8017d 2020-07-23 stsp path_unstaged_content);
7567 fda8017d 2020-07-23 stsp goto done;
7569 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
7570 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
7571 fda8017d 2020-07-23 stsp goto done;
7573 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
7574 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7575 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
7577 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
7578 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
7579 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
7580 fda8017d 2020-07-23 stsp goto done;
7582 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
7583 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7584 fda8017d 2020-07-23 stsp goto done;
7586 fda8017d 2020-07-23 stsp link_target[r] = '\0';
7587 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
7588 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
7589 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
7590 fda8017d 2020-07-23 stsp progress_arg);
7592 fda8017d 2020-07-23 stsp int local_changes_subsumed;
7593 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
7594 fda8017d 2020-07-23 stsp blob_base, ondisk_path, ie->path,
7595 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
7596 fda8017d 2020-07-23 stsp path_unstaged_content, label_orig, "unstaged",
7597 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
7600 fda8017d 2020-07-23 stsp goto done;
7602 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
7603 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7604 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
7606 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7608 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
7609 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
7610 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
7611 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
7612 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
7613 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
7614 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
7615 fda8017d 2020-07-23 stsp if (f && fclose(f) != 0 && err == NULL)
7616 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
7617 fda8017d 2020-07-23 stsp free(path_unstaged_content);
7618 fda8017d 2020-07-23 stsp free(path_new_staged_content);
7619 2e1f37b0 2019-08-08 stsp return err;
7622 ad493afc 2019-08-03 stsp static const struct got_error *
7623 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
7624 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
7625 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7626 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7628 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
7629 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
7630 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
7631 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7632 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
7633 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
7634 ad493afc 2019-08-03 stsp int local_changes_subsumed;
7635 9bc94a15 2019-08-03 stsp struct stat sb;
7637 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
7638 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
7639 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
7640 2e1f37b0 2019-08-08 stsp return NULL;
7642 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7643 ad493afc 2019-08-03 stsp if (ie == NULL)
7644 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7646 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7648 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
7650 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
7651 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
7653 f69721c3 2019-10-21 stsp goto done;
7654 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7655 f69721c3 2019-10-21 stsp id_str) == -1) {
7656 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
7657 f69721c3 2019-10-21 stsp goto done;
7660 ad493afc 2019-08-03 stsp switch (staged_status) {
7661 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
7662 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
7663 ad493afc 2019-08-03 stsp blob_id, 8192);
7666 ad493afc 2019-08-03 stsp /* fall through */
7667 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
7668 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7669 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
7670 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7671 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7672 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7675 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
7678 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
7679 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
7680 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
7681 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
7682 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
7683 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
7686 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
7687 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
7690 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
7691 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
7692 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
7693 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
7694 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
7695 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
7696 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
7697 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
7698 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
7700 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
7701 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7702 36bf999c 2020-07-23 stsp char *staged_target;
7703 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
7704 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
7706 36bf999c 2020-07-23 stsp goto done;
7707 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
7708 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
7709 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
7710 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
7711 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
7712 36bf999c 2020-07-23 stsp free(staged_target);
7714 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
7715 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
7716 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
7717 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
7718 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
7719 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
7720 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
7724 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7727 ad493afc 2019-08-03 stsp if (err == NULL)
7728 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
7729 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
7731 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
7732 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7733 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7734 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7735 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7738 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7740 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7741 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7745 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7746 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
7747 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
7750 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
7754 ad493afc 2019-08-03 stsp free(ondisk_path);
7755 ad493afc 2019-08-03 stsp if (blob_base)
7756 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
7757 ad493afc 2019-08-03 stsp if (blob_staged)
7758 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
7759 f69721c3 2019-10-21 stsp free(id_str);
7760 f69721c3 2019-10-21 stsp free(label_orig);
7761 ad493afc 2019-08-03 stsp return err;
7764 ad493afc 2019-08-03 stsp const struct got_error *
7765 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
7766 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
7767 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7768 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7769 ad493afc 2019-08-03 stsp struct got_repository *repo)
7771 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7772 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
7773 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7774 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
7775 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
7777 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7779 ad493afc 2019-08-03 stsp return err;
7781 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7783 ad493afc 2019-08-03 stsp goto done;
7785 ad493afc 2019-08-03 stsp upa.worktree = worktree;
7786 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
7787 ad493afc 2019-08-03 stsp upa.repo = repo;
7788 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
7789 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
7790 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
7791 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
7792 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7793 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7794 f2a9dc41 2019-12-13 tracey unstage_path, &upa, NULL, NULL, 0, 0);
7796 ad493afc 2019-08-03 stsp goto done;
7799 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7800 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
7801 ad493afc 2019-08-03 stsp err = sync_err;
7803 ad493afc 2019-08-03 stsp free(fileindex_path);
7804 ad493afc 2019-08-03 stsp if (fileindex)
7805 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
7806 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7807 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
7808 ad493afc 2019-08-03 stsp err = unlockerr;
7809 ad493afc 2019-08-03 stsp return err;
7812 b2118c49 2020-07-28 stsp struct report_file_info_arg {
7813 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
7814 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
7815 b2118c49 2020-07-28 stsp void *info_arg;
7816 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
7817 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
7818 b2118c49 2020-07-28 stsp void *cancel_arg;
7821 b2118c49 2020-07-28 stsp static const struct got_error *
7822 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
7824 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
7825 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
7826 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
7827 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7828 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
7829 b2118c49 2020-07-28 stsp int stage;
7831 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7832 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
7834 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
7835 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7836 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
7839 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
7840 b2118c49 2020-07-28 stsp return NULL;
7842 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_blob(ie)) {
7843 b2118c49 2020-07-28 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7844 b2118c49 2020-07-28 stsp blob_idp = &blob_id;
7846 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
7847 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7848 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
7849 b2118c49 2020-07-28 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7850 b2118c49 2020-07-28 stsp SHA1_DIGEST_LENGTH);
7851 b2118c49 2020-07-28 stsp staged_blob_idp = &staged_blob_id;
7854 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_commit(ie)) {
7855 b2118c49 2020-07-28 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
7856 b2118c49 2020-07-28 stsp commit_idp = &commit_id;
7859 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
7860 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
7863 b2118c49 2020-07-28 stsp const struct got_error *
7864 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
7865 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
7866 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
7867 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7870 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
7871 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
7872 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
7873 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
7875 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
7877 b2118c49 2020-07-28 stsp return err;
7879 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7881 b2118c49 2020-07-28 stsp goto done;
7883 b2118c49 2020-07-28 stsp arg.worktree = worktree;
7884 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
7885 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
7886 b2118c49 2020-07-28 stsp arg.paths = paths;
7887 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
7888 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
7889 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
7892 b2118c49 2020-07-28 stsp free(fileindex_path);
7893 b2118c49 2020-07-28 stsp if (fileindex)
7894 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
7895 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
7896 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
7897 b2118c49 2020-07-28 stsp err = unlockerr;
7898 b2118c49 2020-07-28 stsp return err;